authorgravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2018-04-24 01:49:22+03:00
committergravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2018-04-24 01:49:22+03:00
log7eab62325b539be4aacb17fd64f5b4445c8409e7
tree5f1d10a1ecca8f1c5568f64c5c878affdb6880ad
parent89a4c373d3756baeac9d2780b1249ada37961d16

One step towards @typeInfo


4 files changed, 217 insertions(+), 0 deletions(-)

src/all_types.hpp+8
......@@ -1293,6 +1293,7 @@ enum BuiltinFnId {
12931293 BuiltinFnIdMemberType,
12941294 BuiltinFnIdMemberName,
12951295 BuiltinFnIdField,
1296 BuiltinFnIdTypeInfo,
12961297 BuiltinFnIdTypeof,
12971298 BuiltinFnIdAddWithOverflow,
12981299 BuiltinFnIdSubWithOverflow,
......@@ -2037,6 +2038,7 @@ enum IrInstructionId {
20372038 IrInstructionIdTagType,
20382039 IrInstructionIdFieldParentPtr,
20392040 IrInstructionIdOffsetOf,
2041 IrInstructionIdTypeInfo,
20402042 IrInstructionIdTypeId,
20412043 IrInstructionIdSetEvalBranchQuota,
20422044 IrInstructionIdPtrTypeOf,
......@@ -2858,6 +2860,12 @@ struct IrInstructionOffsetOf {
28582860 IrInstruction *field_name;
28592861};
28602862
2863struct IrInstructionTypeInfo {
2864 IrInstruction base;
2865
2866 IrInstruction *type_value;
2867};
2868
28612869struct IrInstructionTypeId {
28622870 IrInstruction base;
28632871
src/codegen.cpp+153
......@@ -4502,6 +4502,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
45024502 case IrInstructionIdDeclRef:
45034503 case IrInstructionIdSwitchVar:
45044504 case IrInstructionIdOffsetOf:
4505 case IrInstructionIdTypeInfo:
45054506 case IrInstructionIdTypeId:
45064507 case IrInstructionIdSetEvalBranchQuota:
45074508 case IrInstructionIdPtrTypeOf:
......@@ -6125,6 +6126,7 @@ static void define_builtin_fns(CodeGen *g) {
61256126 create_builtin_fn(g, BuiltinFnIdMemberType, "memberType", 2);
61266127 create_builtin_fn(g, BuiltinFnIdMemberName, "memberName", 2);
61276128 create_builtin_fn(g, BuiltinFnIdField, "field", 2);
6129 create_builtin_fn(g, BuiltinFnIdTypeInfo, "typeInfo", 1);
61286130 create_builtin_fn(g, BuiltinFnIdTypeof, "typeOf", 1); // TODO rename to TypeOf
61296131 create_builtin_fn(g, BuiltinFnIdAddWithOverflow, "addWithOverflow", 4);
61306132 create_builtin_fn(g, BuiltinFnIdSubWithOverflow, "subWithOverflow", 4);
......@@ -6344,6 +6346,157 @@ static void define_builtin_compile_vars(CodeGen *g) {
63446346 }
63456347 buf_appendf(contents, "};\n\n");
63466348 }
6349 {
6350 buf_appendf(contents,
6351 "pub const IntInfo = struct {\n"
6352 " is_signed: bool,\n"
6353 " bits: u8,\n"
6354 "};\n"
6355 "\n"
6356 "pub const FloatInfo = struct {\n"
6357 " bits: u8,\n"
6358 "};\n"
6359 "\n"
6360 "pub const PointerInfo = struct {\n"
6361 " is_const: bool,\n"
6362 " is_volatile: bool,\n"
6363 " alignment: u32,\n"
6364 " child: &TypeInfo,\n"
6365 "};\n"
6366 "\n"
6367 "pub const ArrayInfo = struct {\n"
6368 " len: u64,\n"
6369 " child: &TypeInfo,\n"
6370 "};\n"
6371 "\n"
6372 "pub const ContainerLayout = enum {\n"
6373 " Auto,\n"
6374 " Extern,\n"
6375 " Packed,\n"
6376 "};\n"
6377 "\n"
6378 "pub const StructFieldInfo = struct {\n"
6379 " name: []const u8,\n"
6380 " offset: usize,\n"
6381 " type_info: TypeInfo,\n"
6382 "};\n"
6383 "\n"
6384 "pub const StructInfo = struct {\n"
6385 " layout: ContainerLayout,\n"
6386 " fields: []StructFieldInfo,\n"
6387 "};\n"
6388 "\n"
6389 "pub const NullableInfo = struct {\n"
6390 " child: &TypeInfo,\n"
6391 "};\n"
6392 "\n"
6393 "pub const ErrorUnionInfo = struct {\n"
6394 " error_set: ErrorSetInfo,\n"
6395 " payload: &TypeInfo,\n"
6396 "};\n"
6397 "\n"
6398 "pub const ErrorInfo = struct {\n"
6399 " name: []const u8,\n"
6400 " value: usize,\n"
6401 "};\n"
6402 "\n"
6403 "pub const ErrorSetInfo = struct {\n"
6404 " errors: []ErrorInfo,\n"
6405 "};\n"
6406 "\n"
6407 "pub const EnumFieldInfo = struct {\n"
6408 " name: []const u8,\n"
6409 " value: usize,\n"
6410 "};\n"
6411 "\n"
6412 "pub const EnumInfo = struct {\n"
6413 " layout: ContainerLayout,\n"
6414 " tag_type: IntInfo,\n"
6415 " fields: []EnumFieldInfo,\n"
6416 "};\n"
6417 "\n"
6418 "pub const UnionFieldInfo = struct {\n"
6419 " name: []const u8,\n"
6420 " enum_field: EnumFieldInfo,\n"
6421 " type_info: TypeInfo,\n"
6422 "};\n"
6423 "\n"
6424 "pub const UnionInfo = struct {\n"
6425 " layout: ContainerLayout,\n"
6426 " tag_type: ?EnumInfo,\n"
6427 " fields: []UnionFieldInfo,\n"
6428 "};\n"
6429 "\n"
6430 "pub const CallingConvention = enum {\n"
6431 " Unspecified,\n"
6432 " C,\n"
6433 " Cold,\n"
6434 " Naked,\n"
6435 " Stdcall,\n"
6436 " Async,\n"
6437 "};\n"
6438 "\n"
6439 "pub const FnArgInfo = struct {\n"
6440 " is_comptime: bool,\n"
6441 " name: []const u8,\n"
6442 " type_info: TypeInfo,\n"
6443 "};\n"
6444 "\n"
6445 "pub const FnInfo = struct {\n"
6446 " calling_convention: CallingConvention,\n"
6447 " is_generic: bool,\n"
6448 " is_varargs: bool,\n"
6449 " return_type: &TypeInfo,\n"
6450 " args: []FnArgInfo,\n"
6451 "};\n"
6452 "\n"
6453 "pub const BoundFnInfo = struct {\n"
6454 " bound_type: &TypeInfo,\n"
6455 " fn_info: FnInfo,\n"
6456 "};\n"
6457 "\n"
6458 "pub const PromiseInfo = struct {\n"
6459 " child: ?&TypeInfo,\n"
6460 "};\n"
6461 "\n"
6462 "pub const TypeInfo = union(TypeId) {\n"
6463 " Type: void,\n"
6464 " Void: void,\n"
6465 " Bool: void,\n"
6466 " NoReturn: void,\n"
6467 " Int: IntInfo,\n"
6468 " Float: FloatInfo,\n"
6469 " Pointer: PointerInfo,\n"
6470 " Array: ArrayInfo,\n"
6471 " Struct: StructInfo,\n"
6472 " FloatLiteral: void,\n"
6473 " IntLiteral: void,\n"
6474 " UndefinedLiteral: void,\n"
6475 " NullLiteral: void,\n"
6476 " Nullable: NullableInfo,\n"
6477 " ErrorUnion: ErrorUnionInfo,\n"
6478 " ErrorSet: ErrorSetInfo,\n"
6479 " Enum: EnumInfo,\n"
6480 " Union: UnionInfo,\n"
6481 " Fn: FnInfo,\n"
6482 " Namespace: void,\n"
6483 " Block: void,\n"
6484 " BoundFn: BoundFnInfo,\n"
6485 " ArgTuple: void,\n"
6486 " Opaque: void,\n"
6487 " Promise: PromiseInfo,\n"
6488 "};\n\n");
6489 assert(ContainerLayoutAuto == 0);
6490 assert(ContainerLayoutExtern == 1);
6491 assert(ContainerLayoutPacked == 2);
6492
6493 assert(CallingConventionUnspecified == 0);
6494 assert(CallingConventionC == 1);
6495 assert(CallingConventionCold == 2);
6496 assert(CallingConventionNaked == 3);
6497 assert(CallingConventionStdcall == 4);
6498 assert(CallingConventionAsync == 5);
6499 }
63476500 {
63486501 buf_appendf(contents,
63496502 "pub const FloatMode = enum {\n"
src/ir.cpp+47
......@@ -615,6 +615,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionOffsetOf *) {
615615 return IrInstructionIdOffsetOf;
616616}
617617
618static constexpr IrInstructionId ir_instruction_id(IrInstructionTypeInfo *) {
619 return IrInstructionIdTypeInfo;
620}
621
618622static constexpr IrInstructionId ir_instruction_id(IrInstructionTypeId *) {
619623 return IrInstructionIdTypeId;
620624}
......@@ -2440,6 +2444,16 @@ static IrInstruction *ir_build_offset_of(IrBuilder *irb, Scope *scope, AstNode *
24402444 return &instruction->base;
24412445}
24422446
2447static IrInstruction *ir_build_type_info(IrBuilder *irb, Scope *scope, AstNode *source_node,
2448 IrInstruction *type_value) {
2449 IrInstructionTypeInfo *instruction = ir_build_instruction<IrInstructionTypeInfo>(irb, scope, source_node);
2450 instruction->type_value = type_value;
2451
2452 ir_ref_instruction(type_value, irb->current_basic_block);
2453
2454 return &instruction->base;
2455}
2456
24432457static IrInstruction *ir_build_type_id(IrBuilder *irb, Scope *scope, AstNode *source_node,
24442458 IrInstruction *type_value)
24452459{
......@@ -4080,6 +4094,16 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
40804094
40814095 return ir_build_load_ptr(irb, scope, node, ptr_instruction);
40824096 }
4097 case BuiltinFnIdTypeInfo:
4098 {
4099 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
4100 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
4101 if (arg0_value == irb->codegen->invalid_instruction)
4102 return arg0_value;
4103
4104 IrInstruction *type_info = ir_build_type_info(irb, scope, node, arg0_value);
4105 return ir_lval_wrap(irb, scope, type_info, lval);
4106 }
40834107 case BuiltinFnIdBreakpoint:
40844108 return ir_lval_wrap(irb, scope, ir_build_breakpoint(irb, scope, node), lval);
40854109 case BuiltinFnIdReturnAddress:
......@@ -15669,6 +15693,26 @@ static TypeTableEntry *ir_analyze_instruction_offset_of(IrAnalyze *ira,
1566915693 return ira->codegen->builtin_types.entry_num_lit_int;
1567015694}
1567115695
15696static TypeTableEntry *ir_analyze_instruction_type_info(IrAnalyze *ira,
15697 IrInstructionTypeInfo *instruction)
15698{
15699 IrInstruction *type_value = instruction->type_value->other;
15700 TypeTableEntry *type_entry = ir_resolve_type(ira, type_value);
15701 if (type_is_invalid(type_entry))
15702 return ira->codegen->builtin_types.entry_invalid;
15703
15704 ConstExprValue *var_value = get_builtin_value(ira->codegen, "TypeInfo");
15705 assert(var_value->type->id == TypeTableEntryIdMetaType);
15706 TypeTableEntry *result_type = var_value->data.x_type;
15707
15708 // TODO: Check if we need to explicitely make a &const TypeInfo here, I think we don't.
15709 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
15710 out_val->data.x_struct.fields = create_const_vals(1);
15711 // TODO: Fill the struct
15712 zig_panic("Building TypeInfo...");
15713 return result_type;
15714}
15715
1567215716static TypeTableEntry *ir_analyze_instruction_type_id(IrAnalyze *ira,
1567315717 IrInstructionTypeId *instruction)
1567415718{
......@@ -18555,6 +18599,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
1855518599 return ir_analyze_instruction_field_parent_ptr(ira, (IrInstructionFieldParentPtr *)instruction);
1855618600 case IrInstructionIdOffsetOf:
1855718601 return ir_analyze_instruction_offset_of(ira, (IrInstructionOffsetOf *)instruction);
18602 case IrInstructionIdTypeInfo:
18603 return ir_analyze_instruction_type_info(ira, (IrInstructionTypeInfo *) instruction);
1855818604 case IrInstructionIdTypeId:
1855918605 return ir_analyze_instruction_type_id(ira, (IrInstructionTypeId *)instruction);
1856018606 case IrInstructionIdSetEvalBranchQuota:
......@@ -18821,6 +18867,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
1882118867 case IrInstructionIdTagName:
1882218868 case IrInstructionIdFieldParentPtr:
1882318869 case IrInstructionIdOffsetOf:
18870 case IrInstructionIdTypeInfo:
1882418871 case IrInstructionIdTypeId:
1882518872 case IrInstructionIdAlignCast:
1882618873 case IrInstructionIdOpaqueType:
src/ir_print.cpp+9
......@@ -966,6 +966,12 @@ static void ir_print_offset_of(IrPrint *irp, IrInstructionOffsetOf *instruction)
966966 fprintf(irp->f, ")");
967967}
968968
969static void ir_print_type_info(IrPrint *irp, IrInstructionTypeInfo *instruction) {
970 fprintf(irp->f, "@typeInfo(");
971 ir_print_other_instruction(irp, instruction->type_value);
972 fprintf(irp->f, ")");
973}
974
969975static void ir_print_type_id(IrPrint *irp, IrInstructionTypeId *instruction) {
970976 fprintf(irp->f, "@typeId(");
971977 ir_print_other_instruction(irp, instruction->type_value);
......@@ -1536,6 +1542,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
15361542 case IrInstructionIdOffsetOf:
15371543 ir_print_offset_of(irp, (IrInstructionOffsetOf *)instruction);
15381544 break;
1545 case IrInstructionIdTypeInfo:
1546 ir_print_type_info(irp, (IrInstructionTypeInfo *)instruction);
1547 break;
15391548 case IrInstructionIdTypeId:
15401549 ir_print_type_id(irp, (IrInstructionTypeId *)instruction);
15411550 break;