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 {...@@ -1293,6 +1293,7 @@ enum BuiltinFnId {
1293 BuiltinFnIdMemberType,1293 BuiltinFnIdMemberType,
1294 BuiltinFnIdMemberName,1294 BuiltinFnIdMemberName,
1295 BuiltinFnIdField,1295 BuiltinFnIdField,
1296 BuiltinFnIdTypeInfo,
1296 BuiltinFnIdTypeof,1297 BuiltinFnIdTypeof,
1297 BuiltinFnIdAddWithOverflow,1298 BuiltinFnIdAddWithOverflow,
1298 BuiltinFnIdSubWithOverflow,1299 BuiltinFnIdSubWithOverflow,
...@@ -2037,6 +2038,7 @@ enum IrInstructionId {...@@ -2037,6 +2038,7 @@ enum IrInstructionId {
2037 IrInstructionIdTagType,2038 IrInstructionIdTagType,
2038 IrInstructionIdFieldParentPtr,2039 IrInstructionIdFieldParentPtr,
2039 IrInstructionIdOffsetOf,2040 IrInstructionIdOffsetOf,
2041 IrInstructionIdTypeInfo,
2040 IrInstructionIdTypeId,2042 IrInstructionIdTypeId,
2041 IrInstructionIdSetEvalBranchQuota,2043 IrInstructionIdSetEvalBranchQuota,
2042 IrInstructionIdPtrTypeOf,2044 IrInstructionIdPtrTypeOf,
...@@ -2858,6 +2860,12 @@ struct IrInstructionOffsetOf {...@@ -2858,6 +2860,12 @@ struct IrInstructionOffsetOf {
2858 IrInstruction *field_name;2860 IrInstruction *field_name;
2859};2861};
28602862
2863struct IrInstructionTypeInfo {
2864 IrInstruction base;
2865
2866 IrInstruction *type_value;
2867};
2868
2861struct IrInstructionTypeId {2869struct IrInstructionTypeId {
2862 IrInstruction base;2870 IrInstruction base;
28632871
src/codegen.cpp+153
...@@ -4502,6 +4502,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,...@@ -4502,6 +4502,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
4502 case IrInstructionIdDeclRef:4502 case IrInstructionIdDeclRef:
4503 case IrInstructionIdSwitchVar:4503 case IrInstructionIdSwitchVar:
4504 case IrInstructionIdOffsetOf:4504 case IrInstructionIdOffsetOf:
4505 case IrInstructionIdTypeInfo:
4505 case IrInstructionIdTypeId:4506 case IrInstructionIdTypeId:
4506 case IrInstructionIdSetEvalBranchQuota:4507 case IrInstructionIdSetEvalBranchQuota:
4507 case IrInstructionIdPtrTypeOf:4508 case IrInstructionIdPtrTypeOf:
...@@ -6125,6 +6126,7 @@ static void define_builtin_fns(CodeGen *g) {...@@ -6125,6 +6126,7 @@ static void define_builtin_fns(CodeGen *g) {
6125 create_builtin_fn(g, BuiltinFnIdMemberType, "memberType", 2);6126 create_builtin_fn(g, BuiltinFnIdMemberType, "memberType", 2);
6126 create_builtin_fn(g, BuiltinFnIdMemberName, "memberName", 2);6127 create_builtin_fn(g, BuiltinFnIdMemberName, "memberName", 2);
6127 create_builtin_fn(g, BuiltinFnIdField, "field", 2);6128 create_builtin_fn(g, BuiltinFnIdField, "field", 2);
6129 create_builtin_fn(g, BuiltinFnIdTypeInfo, "typeInfo", 1);
6128 create_builtin_fn(g, BuiltinFnIdTypeof, "typeOf", 1); // TODO rename to TypeOf6130 create_builtin_fn(g, BuiltinFnIdTypeof, "typeOf", 1); // TODO rename to TypeOf
6129 create_builtin_fn(g, BuiltinFnIdAddWithOverflow, "addWithOverflow", 4);6131 create_builtin_fn(g, BuiltinFnIdAddWithOverflow, "addWithOverflow", 4);
6130 create_builtin_fn(g, BuiltinFnIdSubWithOverflow, "subWithOverflow", 4);6132 create_builtin_fn(g, BuiltinFnIdSubWithOverflow, "subWithOverflow", 4);
...@@ -6344,6 +6346,157 @@ static void define_builtin_compile_vars(CodeGen *g) {...@@ -6344,6 +6346,157 @@ static void define_builtin_compile_vars(CodeGen *g) {
6344 }6346 }
6345 buf_appendf(contents, "};\n\n");6347 buf_appendf(contents, "};\n\n");
6346 }6348 }
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 }
6347 {6500 {
6348 buf_appendf(contents,6501 buf_appendf(contents,
6349 "pub const FloatMode = enum {\n"6502 "pub const FloatMode = enum {\n"
src/ir.cpp+47
...@@ -615,6 +615,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionOffsetOf *) {...@@ -615,6 +615,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionOffsetOf *) {
615 return IrInstructionIdOffsetOf;615 return IrInstructionIdOffsetOf;
616}616}
617617
618static constexpr IrInstructionId ir_instruction_id(IrInstructionTypeInfo *) {
619 return IrInstructionIdTypeInfo;
620}
621
618static constexpr IrInstructionId ir_instruction_id(IrInstructionTypeId *) {622static constexpr IrInstructionId ir_instruction_id(IrInstructionTypeId *) {
619 return IrInstructionIdTypeId;623 return IrInstructionIdTypeId;
620}624}
...@@ -2440,6 +2444,16 @@ static IrInstruction *ir_build_offset_of(IrBuilder *irb, Scope *scope, AstNode *...@@ -2440,6 +2444,16 @@ static IrInstruction *ir_build_offset_of(IrBuilder *irb, Scope *scope, AstNode *
2440 return &instruction->base;2444 return &instruction->base;
2441}2445}
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
2443static IrInstruction *ir_build_type_id(IrBuilder *irb, Scope *scope, AstNode *source_node,2457static IrInstruction *ir_build_type_id(IrBuilder *irb, Scope *scope, AstNode *source_node,
2444 IrInstruction *type_value)2458 IrInstruction *type_value)
2445{2459{
...@@ -4080,6 +4094,16 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo...@@ -4080,6 +4094,16 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
40804094
4081 return ir_build_load_ptr(irb, scope, node, ptr_instruction);4095 return ir_build_load_ptr(irb, scope, node, ptr_instruction);
4082 }4096 }
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 }
4083 case BuiltinFnIdBreakpoint:4107 case BuiltinFnIdBreakpoint:
4084 return ir_lval_wrap(irb, scope, ir_build_breakpoint(irb, scope, node), lval);4108 return ir_lval_wrap(irb, scope, ir_build_breakpoint(irb, scope, node), lval);
4085 case BuiltinFnIdReturnAddress:4109 case BuiltinFnIdReturnAddress:
...@@ -15669,6 +15693,26 @@ static TypeTableEntry *ir_analyze_instruction_offset_of(IrAnalyze *ira,...@@ -15669,6 +15693,26 @@ static TypeTableEntry *ir_analyze_instruction_offset_of(IrAnalyze *ira,
15669 return ira->codegen->builtin_types.entry_num_lit_int;15693 return ira->codegen->builtin_types.entry_num_lit_int;
15670}15694}
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
15672static TypeTableEntry *ir_analyze_instruction_type_id(IrAnalyze *ira,15716static TypeTableEntry *ir_analyze_instruction_type_id(IrAnalyze *ira,
15673 IrInstructionTypeId *instruction)15717 IrInstructionTypeId *instruction)
15674{15718{
...@@ -18555,6 +18599,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi...@@ -18555,6 +18599,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
18555 return ir_analyze_instruction_field_parent_ptr(ira, (IrInstructionFieldParentPtr *)instruction);18599 return ir_analyze_instruction_field_parent_ptr(ira, (IrInstructionFieldParentPtr *)instruction);
18556 case IrInstructionIdOffsetOf:18600 case IrInstructionIdOffsetOf:
18557 return ir_analyze_instruction_offset_of(ira, (IrInstructionOffsetOf *)instruction);18601 return ir_analyze_instruction_offset_of(ira, (IrInstructionOffsetOf *)instruction);
18602 case IrInstructionIdTypeInfo:
18603 return ir_analyze_instruction_type_info(ira, (IrInstructionTypeInfo *) instruction);
18558 case IrInstructionIdTypeId:18604 case IrInstructionIdTypeId:
18559 return ir_analyze_instruction_type_id(ira, (IrInstructionTypeId *)instruction);18605 return ir_analyze_instruction_type_id(ira, (IrInstructionTypeId *)instruction);
18560 case IrInstructionIdSetEvalBranchQuota:18606 case IrInstructionIdSetEvalBranchQuota:
...@@ -18821,6 +18867,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {...@@ -18821,6 +18867,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
18821 case IrInstructionIdTagName:18867 case IrInstructionIdTagName:
18822 case IrInstructionIdFieldParentPtr:18868 case IrInstructionIdFieldParentPtr:
18823 case IrInstructionIdOffsetOf:18869 case IrInstructionIdOffsetOf:
18870 case IrInstructionIdTypeInfo:
18824 case IrInstructionIdTypeId:18871 case IrInstructionIdTypeId:
18825 case IrInstructionIdAlignCast:18872 case IrInstructionIdAlignCast:
18826 case IrInstructionIdOpaqueType:18873 case IrInstructionIdOpaqueType:
src/ir_print.cpp+9
...@@ -966,6 +966,12 @@ static void ir_print_offset_of(IrPrint *irp, IrInstructionOffsetOf *instruction)...@@ -966,6 +966,12 @@ static void ir_print_offset_of(IrPrint *irp, IrInstructionOffsetOf *instruction)
966 fprintf(irp->f, ")");966 fprintf(irp->f, ")");
967}967}
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
969static void ir_print_type_id(IrPrint *irp, IrInstructionTypeId *instruction) {975static void ir_print_type_id(IrPrint *irp, IrInstructionTypeId *instruction) {
970 fprintf(irp->f, "@typeId(");976 fprintf(irp->f, "@typeId(");
971 ir_print_other_instruction(irp, instruction->type_value);977 ir_print_other_instruction(irp, instruction->type_value);
...@@ -1536,6 +1542,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {...@@ -1536,6 +1542,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
1536 case IrInstructionIdOffsetOf:1542 case IrInstructionIdOffsetOf:
1537 ir_print_offset_of(irp, (IrInstructionOffsetOf *)instruction);1543 ir_print_offset_of(irp, (IrInstructionOffsetOf *)instruction);
1538 break;1544 break;
1545 case IrInstructionIdTypeInfo:
1546 ir_print_type_info(irp, (IrInstructionTypeInfo *)instruction);
1547 break;
1539 case IrInstructionIdTypeId:1548 case IrInstructionIdTypeId:
1540 ir_print_type_id(irp, (IrInstructionTypeId *)instruction);1549 ir_print_type_id(irp, (IrInstructionTypeId *)instruction);
1541 break;1550 break;