authorgravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2018-04-25 11:35:46+03:00
committergravatar for alex_naskos@hotmail.comAlexandros Naskos <alex_naskos@hotmail.com> 2018-04-25 11:35:46+03:00
logd68aea4f35fe935f3bf80c86dac4da7c4b88dc5b
treea38c2cfefb4be2c5afca58da7ef3356e700fc43f
parent778b931bf33c9b19502857af918fc159c0cd8e83

Added checks for field name/index mapping in TypeInfo generation. Abstracted the parent setting out.


1 files changed, 53 insertions(+), 86 deletions(-)

src/ir.cpp+53-86
......@@ -13411,7 +13411,6 @@ static IrInstruction *ir_analyze_container_member_access_inner(IrAnalyze *ira,
1341113411 return ira->codegen->invalid_instruction;
1341213412}
1341313413
13414
1341513414static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_name,
1341613415 IrInstruction *source_instr, IrInstruction *container_ptr, TypeTableEntry *container_type)
1341713416{
......@@ -15738,6 +15737,37 @@ static TypeTableEntry *ir_analyze_instruction_offset_of(IrAnalyze *ira,
1573815737 return ira->codegen->builtin_types.entry_num_lit_int;
1573915738}
1574015739
15740static void ensure_field_index(TypeTableEntry *type, const char *field_name, size_t index) {
15741 Buf *field_name_buf;
15742
15743 assert(type != nullptr && !type_is_invalid(type));
15744 // Check for our field by creating a buffer in place then using the comma operator to free it so that we don't
15745 // leak memory in debug mode.
15746 assert(find_struct_type_field(type, field_name_buf = buf_create_from_str(field_name))->src_index == index &&
15747 (buf_deinit(field_name_buf), true));
15748}
15749
15750static void ir_type_info_struct_set_parent(ConstExprValue *struct_val, ConstExprValue *parent) {
15751 assert(struct_val->type->id == TypeTableEntryIdStruct);
15752 assert(parent->type != nullptr && !type_is_invalid(parent->type));
15753
15754 switch (parent->type->id)
15755 {
15756 case TypeTableEntryIdArray:
15757 zig_panic("TODO - Only expected struct or union parent.");
15758 case TypeTableEntryIdStruct:
15759 struct_val->data.x_struct.parent.id = ConstParentIdStruct;
15760 struct_val->data.x_struct.parent.data.p_union.union_val = parent;
15761 break;
15762 case TypeTableEntryIdUnion:
15763 struct_val->data.x_struct.parent.id = ConstParentIdUnion;
15764 struct_val->data.x_struct.parent.data.p_union.union_val = parent;
15765 break;
15766 default:
15767 struct_val->data.x_struct.parent.id = ConstParentIdNone;
15768 }
15769}
15770
1574115771static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *parent, TypeTableEntry *type_entry)
1574215772{
1574315773 assert(type_entry != nullptr);
......@@ -15772,26 +15802,15 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1577215802 ConstExprValue *fields = create_const_vals(2);
1577315803 payload->data.x_struct.fields = fields;
1577415804
15775 if (parent->type->id == TypeTableEntryIdStruct)
15776 {
15777 payload->data.x_struct.parent.id = ConstParentIdStruct;
15778 payload->data.x_struct.parent.data.p_union.union_val = parent;
15779 }
15780 else if (parent->type->id == TypeTableEntryIdUnion)
15781 {
15782 payload->data.x_struct.parent.id = ConstParentIdUnion;
15783 payload->data.x_struct.parent.data.p_union.union_val = parent;
15784 }
15785 else
15786 {
15787 payload->data.x_struct.parent.id = ConstParentIdNone;
15788 }
15805 ir_type_info_struct_set_parent(payload, parent);
1578915806
1579015807 // is_signed: bool
15808 ensure_field_index(payload->type, "is_signed", 0);
1579115809 fields[0].special = ConstValSpecialStatic;
1579215810 fields[0].type = ira->codegen->builtin_types.entry_bool;
1579315811 fields[0].data.x_bool = type_entry->data.integral.is_signed;
1579415812 // bits: u8
15813 ensure_field_index(payload->type, "bits", 1);
1579515814 fields[1].special = ConstValSpecialStatic;
1579615815 fields[1].type = ira->codegen->builtin_types.entry_u8;
1579715816 bigint_init_unsigned(&fields[1].data.x_bigint, type_entry->data.integral.bit_count);
......@@ -15810,21 +15829,10 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1581015829 ConstExprValue *fields = create_const_vals(1);
1581115830 payload->data.x_struct.fields = fields;
1581215831
15813 if (parent->type->id == TypeTableEntryIdStruct)
15814 {
15815 payload->data.x_struct.parent.id = ConstParentIdStruct;
15816 payload->data.x_struct.parent.data.p_union.union_val = parent;
15817 }
15818 else if (parent->type->id == TypeTableEntryIdUnion)
15819 {
15820 payload->data.x_struct.parent.id = ConstParentIdUnion;
15821 payload->data.x_struct.parent.data.p_union.union_val = parent;
15822 }
15823 else
15824 {
15825 payload->data.x_struct.parent.id = ConstParentIdNone;
15826 }
15832 ir_type_info_struct_set_parent(payload, parent);
15833
1582715834 // bits: u8
15835 ensure_field_index(payload->type, "bits", 0);
1582815836 fields[0].special = ConstValSpecialStatic;
1582915837 fields[0].type = ira->codegen->builtin_types.entry_u8;
1583015838 bigint_init_unsigned(&fields->data.x_bigint, type_entry->data.floating.bit_count);
......@@ -15843,33 +15851,25 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1584315851 ConstExprValue *fields = create_const_vals(4);
1584415852 payload->data.x_struct.fields = fields;
1584515853
15846 if (parent->type->id == TypeTableEntryIdStruct)
15847 {
15848 payload->data.x_struct.parent.id = ConstParentIdStruct;
15849 payload->data.x_struct.parent.data.p_union.union_val = parent;
15850 }
15851 else if (parent->type->id == TypeTableEntryIdUnion)
15852 {
15853 payload->data.x_struct.parent.id = ConstParentIdUnion;
15854 payload->data.x_struct.parent.data.p_union.union_val = parent;
15855 }
15856 else
15857 {
15858 payload->data.x_struct.parent.id = ConstParentIdNone;
15859 }
15854 ir_type_info_struct_set_parent(payload, parent);
15855
1586015856 // is_const: bool
15857 ensure_field_index(payload->type, "is_const", 0);
1586115858 fields[0].special = ConstValSpecialStatic;
1586215859 fields[0].type = ira->codegen->builtin_types.entry_bool;
1586315860 fields[0].data.x_bool = type_entry->data.pointer.is_const;
1586415861 // is_volatile: bool
15862 ensure_field_index(payload->type, "is_volatile", 1);
1586515863 fields[1].special = ConstValSpecialStatic;
1586615864 fields[1].type = ira->codegen->builtin_types.entry_bool;
1586715865 fields[1].data.x_bool = type_entry->data.pointer.is_volatile;
1586815866 // alignment: u32
15867 ensure_field_index(payload->type, "alignment", 2);
1586915868 fields[2].special = ConstValSpecialStatic;
1587015869 fields[2].type = ira->codegen->builtin_types.entry_u32;
1587115870 bigint_init_unsigned(&fields[2].data.x_bigint, type_entry->data.pointer.alignment);
1587215871 // child: &TypeInfo
15872 ensure_field_index(payload->type, "child", 3);
1587315873 ConstExprValue *type_info_type = get_builtin_value(ira->codegen, "TypeInfo");
1587415874 assert(type_info_type->type->id == TypeTableEntryIdMetaType);
1587515875 fields[3].special = ConstValSpecialStatic;
......@@ -15896,25 +15896,15 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1589615896 ConstExprValue *fields = create_const_vals(2);
1589715897 payload->data.x_struct.fields = fields;
1589815898
15899 if (parent->type->id == TypeTableEntryIdStruct)
15900 {
15901 payload->data.x_struct.parent.id = ConstParentIdStruct;
15902 payload->data.x_struct.parent.data.p_union.union_val = parent;
15903 }
15904 else if (parent->type->id == TypeTableEntryIdUnion)
15905 {
15906 payload->data.x_struct.parent.id = ConstParentIdUnion;
15907 payload->data.x_struct.parent.data.p_union.union_val = parent;
15908 }
15909 else
15910 {
15911 payload->data.x_struct.parent.id = ConstParentIdNone;
15912 }
15899 ir_type_info_struct_set_parent(payload, parent);
15900
1591315901 // len: usize
15902 ensure_field_index(payload->type, "len", 0);
1591415903 fields[0].special = ConstValSpecialStatic;
1591515904 fields[0].type = ira->codegen->builtin_types.entry_usize;
1591615905 bigint_init_unsigned(&fields[0].data.x_bigint, type_entry->data.array.len);
1591715906 // child: &TypeInfo
15907 ensure_field_index(payload->type, "child", 1);
1591815908 ConstExprValue *type_info_type = get_builtin_value(ira->codegen, "TypeInfo");
1591915909 assert(type_info_type->type->id == TypeTableEntryIdMetaType);
1592015910 fields[1].special = ConstValSpecialStatic;
......@@ -15941,21 +15931,10 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1594115931 ConstExprValue *fields = create_const_vals(1);
1594215932 payload->data.x_struct.fields = fields;
1594315933
15944 if (parent->type->id == TypeTableEntryIdStruct)
15945 {
15946 payload->data.x_struct.parent.id = ConstParentIdStruct;
15947 payload->data.x_struct.parent.data.p_union.union_val = parent;
15948 }
15949 else if (parent->type->id == TypeTableEntryIdUnion)
15950 {
15951 payload->data.x_struct.parent.id = ConstParentIdUnion;
15952 payload->data.x_struct.parent.data.p_union.union_val = parent;
15953 }
15954 else
15955 {
15956 payload->data.x_struct.parent.id = ConstParentIdNone;
15957 }
15934 ir_type_info_struct_set_parent(payload, parent);
15935
1595815936 // child: &TypeInfo
15937 ensure_field_index(payload->type, "child", 0);
1595915938 ConstExprValue *type_info_type = get_builtin_value(ira->codegen, "TypeInfo");
1596015939 assert(type_info_type->type->id == TypeTableEntryIdMetaType);
1596115940 fields[0].special = ConstValSpecialStatic;
......@@ -15982,21 +15961,10 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
1598215961 ConstExprValue *fields = create_const_vals(1);
1598315962 payload->data.x_struct.fields = fields;
1598415963
15985 if (parent->type->id == TypeTableEntryIdStruct)
15986 {
15987 payload->data.x_struct.parent.id = ConstParentIdStruct;
15988 payload->data.x_struct.parent.data.p_union.union_val = parent;
15989 }
15990 else if (parent->type->id == TypeTableEntryIdUnion)
15991 {
15992 payload->data.x_struct.parent.id = ConstParentIdUnion;
15993 payload->data.x_struct.parent.data.p_union.union_val = parent;
15994 }
15995 else
15996 {
15997 payload->data.x_struct.parent.id = ConstParentIdNone;
15998 }
15964 ir_type_info_struct_set_parent(payload, parent);
15965
1599915966 // child: ?&TypeInfo
15967 ensure_field_index(payload->type, "child", 0);
1600015968 ConstExprValue *type_info_type = get_builtin_value(ira->codegen, "TypeInfo");
1600115969 assert(type_info_type->type->id == TypeTableEntryIdMetaType);
1600215970
......@@ -16046,7 +16014,6 @@ static TypeTableEntry *ir_analyze_instruction_type_info(IrAnalyze *ira,
1604616014 assert(var_value->type->id == TypeTableEntryIdMetaType);
1604716015 TypeTableEntry *result_type = var_value->data.x_type;
1604816016
16049 // TODO: We need to return a const pointer to the typeinfo, not the typeinfo by value.
1605016017 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
1605116018 out_val->type = result_type;
1605216019 bigint_init_unsigned(&out_val->data.x_union.tag, type_id_index(type_entry->id));