@@ -88,6 +88,7 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out
88
88
g->exported_symbol_names .init (8 );
89
89
g->external_prototypes .init (8 );
90
90
g->string_literals_table .init (16 );
91
+ g->type_info_cache .init (32 );
91
92
g->is_test_build = false ;
92
93
g->want_h_file = (out_type == OutTypeObj || out_type == OutTypeLib);
93
94
buf_resize (&g->global_asm , 0 );
@@ -4502,6 +4503,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
4502
4503
case IrInstructionIdDeclRef:
4503
4504
case IrInstructionIdSwitchVar:
4504
4505
case IrInstructionIdOffsetOf:
4506
+ case IrInstructionIdTypeInfo:
4505
4507
case IrInstructionIdTypeId:
4506
4508
case IrInstructionIdSetEvalBranchQuota:
4507
4509
case IrInstructionIdPtrTypeOf:
@@ -6125,6 +6127,7 @@ static void define_builtin_fns(CodeGen *g) {
6125
6127
create_builtin_fn (g, BuiltinFnIdMemberType, " memberType" , 2 );
6126
6128
create_builtin_fn (g, BuiltinFnIdMemberName, " memberName" , 2 );
6127
6129
create_builtin_fn (g, BuiltinFnIdField, " field" , 2 );
6130
+ create_builtin_fn (g, BuiltinFnIdTypeInfo, " typeInfo" , 1 );
6128
6131
create_builtin_fn (g, BuiltinFnIdTypeof, " typeOf" , 1 ); // TODO rename to TypeOf
6129
6132
create_builtin_fn (g, BuiltinFnIdAddWithOverflow, " addWithOverflow" , 4 );
6130
6133
create_builtin_fn (g, BuiltinFnIdSubWithOverflow, " subWithOverflow" , 4 );
@@ -6344,6 +6347,190 @@ static void define_builtin_compile_vars(CodeGen *g) {
6344
6347
}
6345
6348
buf_appendf (contents, " };\n\n " );
6346
6349
}
6350
+ {
6351
+ buf_appendf (contents,
6352
+ " pub const TypeInfo = union(TypeId) {\n "
6353
+ " Type: void,\n "
6354
+ " Void: void,\n "
6355
+ " Bool: void,\n "
6356
+ " NoReturn: void,\n "
6357
+ " Int: Int,\n "
6358
+ " Float: Float,\n "
6359
+ " Pointer: Pointer,\n "
6360
+ " Array: Array,\n "
6361
+ " Struct: Struct,\n "
6362
+ " FloatLiteral: void,\n "
6363
+ " IntLiteral: void,\n "
6364
+ " UndefinedLiteral: void,\n "
6365
+ " NullLiteral: void,\n "
6366
+ " Nullable: Nullable,\n "
6367
+ " ErrorUnion: ErrorUnion,\n "
6368
+ " ErrorSet: ErrorSet,\n "
6369
+ " Enum: Enum,\n "
6370
+ " Union: Union,\n "
6371
+ " Fn: Fn,\n "
6372
+ " Namespace: void,\n "
6373
+ " Block: void,\n "
6374
+ " BoundFn: Fn,\n "
6375
+ " ArgTuple: void,\n "
6376
+ " Opaque: void,\n "
6377
+ " Promise: Promise,\n "
6378
+ " \n\n "
6379
+ " pub const Int = struct {\n "
6380
+ " is_signed: bool,\n "
6381
+ " bits: u8,\n "
6382
+ " };\n "
6383
+ " \n "
6384
+ " pub const Float = struct {\n "
6385
+ " bits: u8,\n "
6386
+ " };\n "
6387
+ " \n "
6388
+ " pub const Pointer = struct {\n "
6389
+ " is_const: bool,\n "
6390
+ " is_volatile: bool,\n "
6391
+ " alignment: u32,\n "
6392
+ " child: type,\n "
6393
+ " };\n "
6394
+ " \n "
6395
+ " pub const Array = struct {\n "
6396
+ " len: usize,\n "
6397
+ " child: type,\n "
6398
+ " };\n "
6399
+ " \n "
6400
+ " pub const ContainerLayout = enum {\n "
6401
+ " Auto,\n "
6402
+ " Extern,\n "
6403
+ " Packed,\n "
6404
+ " };\n "
6405
+ " \n "
6406
+ " pub const StructField = struct {\n "
6407
+ " name: []const u8,\n "
6408
+ " offset: ?usize,\n "
6409
+ " field_type: type,\n "
6410
+ " };\n "
6411
+ " \n "
6412
+ " pub const Struct = struct {\n "
6413
+ " layout: ContainerLayout,\n "
6414
+ " fields: []StructField,\n "
6415
+ " defs: []Definition,\n "
6416
+ " };\n "
6417
+ " \n "
6418
+ " pub const Nullable = struct {\n "
6419
+ " child: type,\n "
6420
+ " };\n "
6421
+ " \n "
6422
+ " pub const ErrorUnion = struct {\n "
6423
+ " error_set: type,\n "
6424
+ " payload: type,\n "
6425
+ " };\n "
6426
+ " \n "
6427
+ " pub const Error = struct {\n "
6428
+ " name: []const u8,\n "
6429
+ " value: usize,\n "
6430
+ " };\n "
6431
+ " \n "
6432
+ " pub const ErrorSet = struct {\n "
6433
+ " errors: []Error,\n "
6434
+ " };\n "
6435
+ " \n "
6436
+ " pub const EnumField = struct {\n "
6437
+ " name: []const u8,\n "
6438
+ " value: usize,\n "
6439
+ " };\n "
6440
+ " \n "
6441
+ " pub const Enum = struct {\n "
6442
+ " layout: ContainerLayout,\n "
6443
+ " tag_type: type,\n "
6444
+ " fields: []EnumField,\n "
6445
+ " defs: []Definition,\n "
6446
+ " };\n "
6447
+ " \n "
6448
+ " pub const UnionField = struct {\n "
6449
+ " name: []const u8,\n "
6450
+ " enum_field: ?EnumField,\n "
6451
+ " field_type: type,\n "
6452
+ " };\n "
6453
+ " \n "
6454
+ " pub const Union = struct {\n "
6455
+ " layout: ContainerLayout,\n "
6456
+ " tag_type: type,\n "
6457
+ " fields: []UnionField,\n "
6458
+ " defs: []Definition,\n "
6459
+ " };\n "
6460
+ " \n "
6461
+ " pub const CallingConvention = enum {\n "
6462
+ " Unspecified,\n "
6463
+ " C,\n "
6464
+ " Cold,\n "
6465
+ " Naked,\n "
6466
+ " Stdcall,\n "
6467
+ " Async,\n "
6468
+ " };\n "
6469
+ " \n "
6470
+ " pub const FnArg = struct {\n "
6471
+ " is_generic: bool,\n "
6472
+ " is_noalias: bool,\n "
6473
+ " arg_type: type,\n "
6474
+ " };\n "
6475
+ " \n "
6476
+ " pub const Fn = struct {\n "
6477
+ " calling_convention: CallingConvention,\n "
6478
+ " is_generic: bool,\n "
6479
+ " is_var_args: bool,\n "
6480
+ " return_type: type,\n "
6481
+ " async_allocator_type: type,\n "
6482
+ " args: []FnArg,\n "
6483
+ " };\n "
6484
+ " \n "
6485
+ " pub const Promise = struct {\n "
6486
+ " child: type,\n "
6487
+ " };\n "
6488
+ " \n "
6489
+ " pub const Definition = struct {\n "
6490
+ " name: []const u8,\n "
6491
+ " is_pub: bool,\n "
6492
+ " data: Data,\n "
6493
+ " \n "
6494
+ " pub const Data = union(enum) {\n "
6495
+ " Type: type,\n "
6496
+ " Var: type,\n "
6497
+ " Fn: FnDef,\n "
6498
+ " \n "
6499
+ " pub const FnDef = struct {\n "
6500
+ " fn_type: type,\n "
6501
+ " inline_type: Inline,\n "
6502
+ " calling_convention: CallingConvention,\n "
6503
+ " is_var_args: bool,\n "
6504
+ " is_extern: bool,\n "
6505
+ " is_export: bool,\n "
6506
+ " lib_name: ?[]const u8,\n "
6507
+ " return_type: type,\n "
6508
+ " arg_names: [][] const u8,\n "
6509
+ " \n "
6510
+ " pub const Inline = enum {\n "
6511
+ " Auto,\n "
6512
+ " Always,\n "
6513
+ " Never,\n "
6514
+ " };\n "
6515
+ " };\n "
6516
+ " };\n "
6517
+ " };\n "
6518
+ " };\n\n " );
6519
+ assert (ContainerLayoutAuto == 0 );
6520
+ assert (ContainerLayoutExtern == 1 );
6521
+ assert (ContainerLayoutPacked == 2 );
6522
+
6523
+ assert (CallingConventionUnspecified == 0 );
6524
+ assert (CallingConventionC == 1 );
6525
+ assert (CallingConventionCold == 2 );
6526
+ assert (CallingConventionNaked == 3 );
6527
+ assert (CallingConventionStdcall == 4 );
6528
+ assert (CallingConventionAsync == 5 );
6529
+
6530
+ assert (FnInlineAuto == 0 );
6531
+ assert (FnInlineAlways == 1 );
6532
+ assert (FnInlineNever == 2 );
6533
+ }
6347
6534
{
6348
6535
buf_appendf (contents,
6349
6536
" pub const FloatMode = enum {\n "
0 commit comments