authorgravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2018-04-25 17:50:11+03:00
committergravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2018-04-25 17:50:11+03:00
logbc160821d33a9284cf8bb85cca6cbf161af71d3b
treef1f4423e2f85f651e77b3c1933f66ddbf44bd045
parent2606993cb48e69015a9152b860e48fca29d330e1

Changed TypeInfo layout.


2 files changed, 174 insertions(+), 151 deletions(-)

src/codegen.cpp+118-118
......@@ -6349,143 +6349,143 @@ static void define_builtin_compile_vars(CodeGen *g) {
63496349 {
63506350 // TODO: Add method info where methods are supported.
63516351 buf_appendf(contents,
6352 "pub const IntInfo = struct {\n"
6353 " is_signed: bool,\n"
6354 " bits: u8,\n"
6355 "};\n"
6356 "\n"
6357 "pub const FloatInfo = struct {\n"
6358 " bits: u8,\n"
6359 "};\n"
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: BoundFn,\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"
63606383 "\n"
6361 "pub const PointerInfo = struct {\n"
6362 " is_const: bool,\n"
6363 " is_volatile: bool,\n"
6364 " alignment: u32,\n"
6365 " child: &TypeInfo,\n"
6366 "};\n"
6384 " pub const Float = struct {\n"
6385 " bits: u8,\n"
6386 " };\n"
63676387 "\n"
6368 "pub const ArrayInfo = struct {\n"
6369 " len: usize,\n"
6370 " child: &TypeInfo,\n"
6371 "};\n"
6388 " pub const Pointer = struct {\n"
6389 " is_const: bool,\n"
6390 " is_volatile: bool,\n"
6391 " alignment: u32,\n"
6392 " child: &TypeInfo,\n"
6393 " };\n"
63726394 "\n"
6373 "pub const ContainerLayout = enum {\n"
6374 " Auto,\n"
6375 " Extern,\n"
6376 " Packed,\n"
6377 "};\n"
6395 " pub const Array = struct {\n"
6396 " len: usize,\n"
6397 " child: &TypeInfo,\n"
6398 " };\n"
63786399 "\n"
6379 "pub const StructFieldInfo = struct {\n"
6380 " name: []const u8,\n"
6381 " offset: usize,\n"
6382 " type_info: TypeInfo,\n"
6383 "};\n"
6400 " pub const ContainerLayout = enum {\n"
6401 " Auto,\n"
6402 " Extern,\n"
6403 " Packed,\n"
6404 " };\n"
63846405 "\n"
6385 "pub const StructInfo = struct {\n"
6386 " layout: ContainerLayout,\n"
6387 " fields: []StructFieldInfo,\n"
6388 "};\n"
6406 " pub const StructField = struct {\n"
6407 " name: []const u8,\n"
6408 " offset: usize,\n"
6409 " type_info: TypeInfo,\n"
6410 " };\n"
63896411 "\n"
6390 "pub const NullableInfo = struct {\n"
6391 " child: &TypeInfo,\n"
6392 "};\n"
6412 " pub const Struct = struct {\n"
6413 " layout: ContainerLayout,\n"
6414 " fields: []StructField,\n"
6415 " };\n"
63936416 "\n"
6394 "pub const ErrorUnionInfo = struct {\n"
6395 " error_set: ErrorSetInfo,\n"
6396 " payload: &TypeInfo,\n"
6397 "};\n"
6417 " pub const Nullable = struct {\n"
6418 " child: &TypeInfo,\n"
6419 " };\n"
63986420 "\n"
6399 "pub const ErrorInfo = struct {\n"
6400 " name: []const u8,\n"
6401 " value: usize,\n"
6402 "};\n"
6421 " pub const ErrorUnion = struct {\n"
6422 " error_set: ErrorSet,\n"
6423 " payload: &TypeInfo,\n"
6424 " };\n"
64036425 "\n"
6404 "pub const ErrorSetInfo = struct {\n"
6405 " errors: []ErrorInfo,\n"
6406 "};\n"
6426 " pub const Error = struct {\n"
6427 " name: []const u8,\n"
6428 " value: usize,\n"
6429 " };\n"
64076430 "\n"
6408 "pub const EnumFieldInfo = struct {\n"
6409 " name: []const u8,\n"
6410 " value: usize,\n"
6411 "};\n"
6431 " pub const ErrorSet = struct {\n"
6432 " errors: []Error,\n"
6433 " };\n"
64126434 "\n"
6413 "pub const EnumInfo = struct {\n"
6414 " layout: ContainerLayout,\n"
6415 " tag_type: IntInfo,\n"
6416 " fields: []EnumFieldInfo,\n"
6417 "};\n"
6435 " pub const EnumField = struct {\n"
6436 " name: []const u8,\n"
6437 " value: usize,\n"
6438 " };\n"
64186439 "\n"
6419 "pub const UnionFieldInfo = struct {\n"
6420 " name: []const u8,\n"
6421 " enum_field: EnumFieldInfo,\n"
6422 " type_info: TypeInfo,\n"
6423 "};\n"
6440 " pub const Enum = struct {\n"
6441 " layout: ContainerLayout,\n"
6442 " tag_type: Int,\n"
6443 " fields: []EnumField,\n"
6444 " };\n"
64246445 "\n"
6425 "pub const UnionInfo = struct {\n"
6426 " layout: ContainerLayout,\n"
6427 " tag_type: ?EnumInfo,\n"
6428 " fields: []UnionFieldInfo,\n"
6429 "};\n"
6446 " pub const UnionField = struct {\n"
6447 " name: []const u8,\n"
6448 " enum_field: EnumField,\n"
6449 " type_info: TypeInfo,\n"
6450 " };\n"
64306451 "\n"
6431 "pub const CallingConvention = enum {\n"
6432 " Unspecified,\n"
6433 " C,\n"
6434 " Cold,\n"
6435 " Naked,\n"
6436 " Stdcall,\n"
6437 " Async,\n"
6438 "};\n"
6452 " pub const Union = struct {\n"
6453 " layout: ContainerLayout,\n"
6454 " tag_type: ?Enum,\n"
6455 " fields: []UnionField,\n"
6456 " };\n"
64396457 "\n"
6440 "pub const FnArgInfo = struct {\n"
6441 " is_comptime: bool,\n"
6442 " name: []const u8,\n"
6443 " type_info: TypeInfo,\n"
6444 "};\n"
6458 " pub const CallingConvention = enum {\n"
6459 " Unspecified,\n"
6460 " C,\n"
6461 " Cold,\n"
6462 " Naked,\n"
6463 " Stdcall,\n"
6464 " Async,\n"
6465 " };\n"
64456466 "\n"
6446 "pub const FnInfo = struct {\n"
6447 " calling_convention: CallingConvention,\n"
6448 " is_generic: bool,\n"
6449 " is_varargs: bool,\n"
6450 " return_type: &TypeInfo,\n"
6451 " args: []FnArgInfo,\n"
6452 "};\n"
6467 " pub const FnArg = struct {\n"
6468 " is_comptime: bool,\n"
6469 " name: []const u8,\n"
6470 " type_info: TypeInfo,\n"
6471 " };\n"
64536472 "\n"
6454 "pub const BoundFnInfo = struct {\n"
6455 " bound_type: &TypeInfo,\n"
6456 " fn_info: FnInfo,\n"
6457 "};\n"
6473 " pub const Fn = struct {\n"
6474 " calling_convention: CallingConvention,\n"
6475 " is_generic: bool,\n"
6476 " is_varargs: bool,\n"
6477 " return_type: &TypeInfo,\n"
6478 " args: []FnArg,\n"
6479 " };\n"
64586480 "\n"
6459 "pub const PromiseInfo = struct {\n"
6460 " child: ?&TypeInfo,\n"
6461 "};\n"
6481 " pub const BoundFn = struct {\n"
6482 " bound_type: &TypeInfo,\n"
6483 " fn_info: Fn,\n"
6484 " };\n"
64626485 "\n"
6463 "pub const TypeInfo = union(TypeId) {\n"
6464 " Type: void,\n"
6465 " Void: void,\n"
6466 " Bool: void,\n"
6467 " NoReturn: void,\n"
6468 " Int: IntInfo,\n"
6469 " Float: FloatInfo,\n"
6470 " Pointer: PointerInfo,\n"
6471 " Array: ArrayInfo,\n"
6472 " Struct: StructInfo,\n"
6473 " FloatLiteral: void,\n"
6474 " IntLiteral: void,\n"
6475 " UndefinedLiteral: void,\n"
6476 " NullLiteral: void,\n"
6477 " Nullable: NullableInfo,\n"
6478 " ErrorUnion: ErrorUnionInfo,\n"
6479 " ErrorSet: ErrorSetInfo,\n"
6480 " Enum: EnumInfo,\n"
6481 " Union: UnionInfo,\n"
6482 " Fn: FnInfo,\n"
6483 " Namespace: void,\n"
6484 " Block: void,\n"
6485 " BoundFn: BoundFnInfo,\n"
6486 " ArgTuple: void,\n"
6487 " Opaque: void,\n"
6488 " Promise: PromiseInfo,\n"
6486 " pub const Promise = struct {\n"
6487 " child: ?&TypeInfo,\n"
6488 " };\n"
64896489 "};\n\n");
64906490 assert(ContainerLayoutAuto == 0);
64916491 assert(ContainerLayoutExtern == 1);
src/ir.cpp+56-33
......@@ -15737,7 +15737,8 @@ static TypeTableEntry *ir_analyze_instruction_offset_of(IrAnalyze *ira,
1573715737 return ira->codegen->builtin_types.entry_num_lit_int;
1573815738}
1573915739
15740static void ensure_field_index(TypeTableEntry *type, const char *field_name, size_t index) {
15740static void ensure_field_index(TypeTableEntry *type, const char *field_name, size_t index)
15741{
1574115742 Buf *field_name_buf;
1574215743
1574315744 assert(type != nullptr && !type_is_invalid(type));
......@@ -15747,7 +15748,8 @@ static void ensure_field_index(TypeTableEntry *type, const char *field_name, siz
1574715748 (buf_deinit(field_name_buf), true));
1574815749}
1574915750
15750static void ir_type_info_struct_set_parent(ConstExprValue *struct_val, ConstExprValue *parent, ssize_t parent_field_index) {
15751static void ir_type_info_struct_set_parent(ConstExprValue *struct_val, ConstExprValue *parent, ssize_t parent_field_index)
15752{
1575115753 assert(struct_val->type->id == TypeTableEntryIdStruct);
1575215754 assert(parent->type != nullptr && !type_is_invalid(parent->type));
1575315755
......@@ -15771,6 +15773,37 @@ static void ir_type_info_struct_set_parent(ConstExprValue *struct_val, ConstExpr
1577115773 }
1577215774}
1577315775
15776static TypeTableEntry *ir_type_info_get_type(IrAnalyze *ira, const char *type_name)
15777{
15778 ConstExprValue *type_info_var = get_builtin_value(ira->codegen, "TypeInfo");
15779 assert(type_info_var->type->id == TypeTableEntryIdMetaType);
15780
15781 TypeTableEntry *type_info_type = type_info_var->data.x_type;
15782 assert(type_info_type->id == TypeTableEntryIdUnion);
15783
15784 if (type_name == nullptr)
15785 return type_info_type;
15786
15787 // @TODO
15788
15789 ScopeDecls *type_info_scope = get_container_scope(type_info_type);
15790 assert(type_info_scope != nullptr);
15791
15792 Buf field_name = BUF_INIT;
15793 buf_init_from_str(&field_name, type_name);
15794 auto entry = type_info_scope->decl_table.maybe_get(&field_name);
15795 buf_deinit(&field_name);
15796 assert(entry != nullptr);
15797
15798 TldVar *tld = (TldVar *)entry->value;
15799 assert(tld->base.id == TldIdVar);
15800
15801 VariableTableEntry *var = tld->var;
15802
15803 assert(var->value->type->id == TypeTableEntryIdMetaType);
15804 return var->value->data.x_type;
15805}
15806
1577415807static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *parent,
1577515808 ssize_t parent_field_index, TypeTableEntry *type_entry)
1577615809{
......@@ -15798,10 +15831,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1579815831 {
1579915832 ConstExprValue *payload = create_const_vals(1);
1580015833 payload->special = ConstValSpecialStatic;
15801
15802 ConstExprValue *int_info_type = get_builtin_value(ira->codegen, "IntInfo");
15803 assert(int_info_type->type->id == TypeTableEntryIdMetaType);
15804 payload->type = int_info_type->data.x_type;
15834 payload->type = ir_type_info_get_type(ira, "Int");
1580515835
1580615836 ConstExprValue *fields = create_const_vals(2);
1580715837 payload->data.x_struct.fields = fields;
......@@ -15825,10 +15855,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1582515855 {
1582615856 ConstExprValue *payload = create_const_vals(1);
1582715857 payload->special = ConstValSpecialStatic;
15828
15829 ConstExprValue *float_info_type = get_builtin_value(ira->codegen, "FloatInfo");
15830 assert(float_info_type->type->id == TypeTableEntryIdMetaType);
15831 payload->type = float_info_type->data.x_type;
15858 payload->type = ir_type_info_get_type(ira, "Float");
1583215859
1583315860 ConstExprValue *fields = create_const_vals(1);
1583415861 payload->data.x_struct.fields = fields;
......@@ -15847,10 +15874,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1584715874 {
1584815875 ConstExprValue *payload = create_const_vals(1);
1584915876 payload->special = ConstValSpecialStatic;
15850
15851 ConstExprValue *pointer_info_type = get_builtin_value(ira->codegen, "PointerInfo");
15852 assert(pointer_info_type->type->id == TypeTableEntryIdMetaType);
15853 payload->type = pointer_info_type->data.x_type;
15877 payload->type = ir_type_info_get_type(ira, "Pointer");
1585415878
1585515879 ConstExprValue *fields = create_const_vals(4);
1585615880 payload->data.x_struct.fields = fields;
......@@ -15884,7 +15908,10 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1588415908 union_val->special = ConstValSpecialStatic;
1588515909 union_val->type = type_info_type->data.x_type;
1588615910 bigint_init_unsigned(&union_val->data.x_union.tag, type_id_index(type_entry->data.pointer.child_type->id));
15887 union_val->data.x_union.payload = ir_make_type_info_value(ira, union_val, -1, type_entry->data.pointer.child_type);
15911
15912 union_val->data.x_union.payload = ir_make_type_info_value(ira, union_val, -1,
15913 type_entry->data.pointer.child_type);
15914
1588815915 fields[3].data.x_ptr.data.ref.pointee = union_val;
1588915916 return payload;
1589015917 }
......@@ -15892,10 +15919,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1589215919 {
1589315920 ConstExprValue *payload = create_const_vals(1);
1589415921 payload->special = ConstValSpecialStatic;
15895
15896 ConstExprValue *pointer_info_type = get_builtin_value(ira->codegen, "ArrayInfo");
15897 assert(pointer_info_type->type->id == TypeTableEntryIdMetaType);
15898 payload->type = pointer_info_type->data.x_type;
15922 payload->type = ir_type_info_get_type(ira, "Array");
1589915923
1590015924 ConstExprValue *fields = create_const_vals(2);
1590115925 payload->data.x_struct.fields = fields;
......@@ -15919,7 +15943,10 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1591915943 union_val->special = ConstValSpecialStatic;
1592015944 union_val->type = type_info_type->data.x_type;
1592115945 bigint_init_unsigned(&union_val->data.x_union.tag, type_id_index(type_entry->data.array.child_type->id));
15922 union_val->data.x_union.payload = ir_make_type_info_value(ira, union_val, -1, type_entry->data.array.child_type);
15946
15947 union_val->data.x_union.payload = ir_make_type_info_value(ira, union_val, -1,
15948 type_entry->data.array.child_type);
15949
1592315950 fields[1].data.x_ptr.data.ref.pointee = union_val;
1592415951 return payload;
1592515952 }
......@@ -15927,10 +15954,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1592715954 {
1592815955 ConstExprValue *payload = create_const_vals(1);
1592915956 payload->special = ConstValSpecialStatic;
15930
15931 ConstExprValue *pointer_info_type = get_builtin_value(ira->codegen, "NullableInfo");
15932 assert(pointer_info_type->type->id == TypeTableEntryIdMetaType);
15933 payload->type = pointer_info_type->data.x_type;
15957 payload->type = ir_type_info_get_type(ira, "Nullable");
1593415958
1593515959 ConstExprValue *fields = create_const_vals(1);
1593615960 payload->data.x_struct.fields = fields;
......@@ -15949,7 +15973,10 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1594915973 union_val->special = ConstValSpecialStatic;
1595015974 union_val->type = type_info_type->data.x_type;
1595115975 bigint_init_unsigned(&union_val->data.x_union.tag, type_id_index(type_entry->data.maybe.child_type->id));
15952 union_val->data.x_union.payload = ir_make_type_info_value(ira, union_val, -1, type_entry->data.maybe.child_type);
15976
15977 union_val->data.x_union.payload = ir_make_type_info_value(ira, union_val, -1,
15978 type_entry->data.maybe.child_type);
15979
1595315980 fields[0].data.x_ptr.data.ref.pointee = union_val;
1595415981 return payload;
1595515982 }
......@@ -15957,10 +15984,7 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1595715984 {
1595815985 ConstExprValue *payload = create_const_vals(1);
1595915986 payload->special = ConstValSpecialStatic;
15960
15961 ConstExprValue *pointer_info_type = get_builtin_value(ira->codegen, "PromiseInfo");
15962 assert(pointer_info_type->type->id == TypeTableEntryIdMetaType);
15963 payload->type = pointer_info_type->data.x_type;
15987 payload->type = ir_type_info_get_type(ira, "Promise");
1596415988
1596515989 ConstExprValue *fields = create_const_vals(1);
1596615990 payload->data.x_struct.fields = fields;
......@@ -15994,7 +16018,8 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1599416018 union_val->special = ConstValSpecialStatic;
1599516019 union_val->type = type_info_type->data.x_type;
1599616020 bigint_init_unsigned(&union_val->data.x_union.tag, type_id_index(type_entry->data.promise.result_type->id));
15997 union_val->data.x_union.payload = ir_make_type_info_value(ira, union_val, -1, type_entry->data.promise.result_type);
16021 union_val->data.x_union.payload = ir_make_type_info_value(ira, union_val, -1,
16022 type_entry->data.promise.result_type);
1599816023
1599916024 maybe_value->data.x_ptr.data.ref.pointee = union_val;
1600016025 fields[0].data.x_maybe = maybe_value;
......@@ -16014,9 +16039,7 @@ static TypeTableEntry *ir_analyze_instruction_type_info(IrAnalyze *ira,
1601416039 if (type_is_invalid(type_entry))
1601516040 return ira->codegen->builtin_types.entry_invalid;
1601616041
16017 ConstExprValue *var_value = get_builtin_value(ira->codegen, "TypeInfo");
16018 assert(var_value->type->id == TypeTableEntryIdMetaType);
16019 TypeTableEntry *result_type = var_value->data.x_type;
16042 TypeTableEntry *result_type = ir_type_info_get_type(ira, nullptr);
1602016043
1602116044 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
1602216045 out_val->type = result_type;