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,...@@ -13411,7 +13411,6 @@ static IrInstruction *ir_analyze_container_member_access_inner(IrAnalyze *ira,
13411 return ira->codegen->invalid_instruction;13411 return ira->codegen->invalid_instruction;
13412}13412}
1341313413
13414
13415static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_name,13414static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_name,
13416 IrInstruction *source_instr, IrInstruction *container_ptr, TypeTableEntry *container_type)13415 IrInstruction *source_instr, IrInstruction *container_ptr, TypeTableEntry *container_type)
13417{13416{
...@@ -15738,6 +15737,37 @@ static TypeTableEntry *ir_analyze_instruction_offset_of(IrAnalyze *ira,...@@ -15738,6 +15737,37 @@ static TypeTableEntry *ir_analyze_instruction_offset_of(IrAnalyze *ira,
15738 return ira->codegen->builtin_types.entry_num_lit_int;15737 return ira->codegen->builtin_types.entry_num_lit_int;
15739}15738}
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
15741static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *parent, TypeTableEntry *type_entry)15771static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *parent, TypeTableEntry *type_entry)
15742{15772{
15743 assert(type_entry != nullptr);15773 assert(type_entry != nullptr);
...@@ -15772,26 +15802,15 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p...@@ -15772,26 +15802,15 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
15772 ConstExprValue *fields = create_const_vals(2);15802 ConstExprValue *fields = create_const_vals(2);
15773 payload->data.x_struct.fields = fields;15803 payload->data.x_struct.fields = fields;
1577415804
15775 if (parent->type->id == TypeTableEntryIdStruct)15805 ir_type_info_struct_set_parent(payload, parent);
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 }
1578915806
15790 // is_signed: bool15807 // is_signed: bool
15808 ensure_field_index(payload->type, "is_signed", 0);
15791 fields[0].special = ConstValSpecialStatic;15809 fields[0].special = ConstValSpecialStatic;
15792 fields[0].type = ira->codegen->builtin_types.entry_bool;15810 fields[0].type = ira->codegen->builtin_types.entry_bool;
15793 fields[0].data.x_bool = type_entry->data.integral.is_signed;15811 fields[0].data.x_bool = type_entry->data.integral.is_signed;
15794 // bits: u815812 // bits: u8
15813 ensure_field_index(payload->type, "bits", 1);
15795 fields[1].special = ConstValSpecialStatic;15814 fields[1].special = ConstValSpecialStatic;
15796 fields[1].type = ira->codegen->builtin_types.entry_u8;15815 fields[1].type = ira->codegen->builtin_types.entry_u8;
15797 bigint_init_unsigned(&fields[1].data.x_bigint, type_entry->data.integral.bit_count);15816 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...@@ -15810,21 +15829,10 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
15810 ConstExprValue *fields = create_const_vals(1);15829 ConstExprValue *fields = create_const_vals(1);
15811 payload->data.x_struct.fields = fields;15830 payload->data.x_struct.fields = fields;
1581215831
15813 if (parent->type->id == TypeTableEntryIdStruct)15832 ir_type_info_struct_set_parent(payload, parent);
15814 {15833
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 }
15827 // bits: u815834 // bits: u8
15835 ensure_field_index(payload->type, "bits", 0);
15828 fields[0].special = ConstValSpecialStatic;15836 fields[0].special = ConstValSpecialStatic;
15829 fields[0].type = ira->codegen->builtin_types.entry_u8;15837 fields[0].type = ira->codegen->builtin_types.entry_u8;
15830 bigint_init_unsigned(&fields->data.x_bigint, type_entry->data.floating.bit_count);15838 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...@@ -15843,33 +15851,25 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
15843 ConstExprValue *fields = create_const_vals(4);15851 ConstExprValue *fields = create_const_vals(4);
15844 payload->data.x_struct.fields = fields;15852 payload->data.x_struct.fields = fields;
1584515853
15846 if (parent->type->id == TypeTableEntryIdStruct)15854 ir_type_info_struct_set_parent(payload, parent);
15847 {15855
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 }
15860 // is_const: bool15856 // is_const: bool
15857 ensure_field_index(payload->type, "is_const", 0);
15861 fields[0].special = ConstValSpecialStatic;15858 fields[0].special = ConstValSpecialStatic;
15862 fields[0].type = ira->codegen->builtin_types.entry_bool;15859 fields[0].type = ira->codegen->builtin_types.entry_bool;
15863 fields[0].data.x_bool = type_entry->data.pointer.is_const;15860 fields[0].data.x_bool = type_entry->data.pointer.is_const;
15864 // is_volatile: bool15861 // is_volatile: bool
15862 ensure_field_index(payload->type, "is_volatile", 1);
15865 fields[1].special = ConstValSpecialStatic;15863 fields[1].special = ConstValSpecialStatic;
15866 fields[1].type = ira->codegen->builtin_types.entry_bool;15864 fields[1].type = ira->codegen->builtin_types.entry_bool;
15867 fields[1].data.x_bool = type_entry->data.pointer.is_volatile;15865 fields[1].data.x_bool = type_entry->data.pointer.is_volatile;
15868 // alignment: u3215866 // alignment: u32
15867 ensure_field_index(payload->type, "alignment", 2);
15869 fields[2].special = ConstValSpecialStatic;15868 fields[2].special = ConstValSpecialStatic;
15870 fields[2].type = ira->codegen->builtin_types.entry_u32;15869 fields[2].type = ira->codegen->builtin_types.entry_u32;
15871 bigint_init_unsigned(&fields[2].data.x_bigint, type_entry->data.pointer.alignment);15870 bigint_init_unsigned(&fields[2].data.x_bigint, type_entry->data.pointer.alignment);
15872 // child: &TypeInfo15871 // child: &TypeInfo
15872 ensure_field_index(payload->type, "child", 3);
15873 ConstExprValue *type_info_type = get_builtin_value(ira->codegen, "TypeInfo");15873 ConstExprValue *type_info_type = get_builtin_value(ira->codegen, "TypeInfo");
15874 assert(type_info_type->type->id == TypeTableEntryIdMetaType);15874 assert(type_info_type->type->id == TypeTableEntryIdMetaType);
15875 fields[3].special = ConstValSpecialStatic;15875 fields[3].special = ConstValSpecialStatic;
...@@ -15896,25 +15896,15 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p...@@ -15896,25 +15896,15 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
15896 ConstExprValue *fields = create_const_vals(2);15896 ConstExprValue *fields = create_const_vals(2);
15897 payload->data.x_struct.fields = fields;15897 payload->data.x_struct.fields = fields;
1589815898
15899 if (parent->type->id == TypeTableEntryIdStruct)15899 ir_type_info_struct_set_parent(payload, parent);
15900 {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 }
15913 // len: usize15901 // len: usize
15902 ensure_field_index(payload->type, "len", 0);
15914 fields[0].special = ConstValSpecialStatic;15903 fields[0].special = ConstValSpecialStatic;
15915 fields[0].type = ira->codegen->builtin_types.entry_usize;15904 fields[0].type = ira->codegen->builtin_types.entry_usize;
15916 bigint_init_unsigned(&fields[0].data.x_bigint, type_entry->data.array.len);15905 bigint_init_unsigned(&fields[0].data.x_bigint, type_entry->data.array.len);
15917 // child: &TypeInfo15906 // child: &TypeInfo
15907 ensure_field_index(payload->type, "child", 1);
15918 ConstExprValue *type_info_type = get_builtin_value(ira->codegen, "TypeInfo");15908 ConstExprValue *type_info_type = get_builtin_value(ira->codegen, "TypeInfo");
15919 assert(type_info_type->type->id == TypeTableEntryIdMetaType);15909 assert(type_info_type->type->id == TypeTableEntryIdMetaType);
15920 fields[1].special = ConstValSpecialStatic;15910 fields[1].special = ConstValSpecialStatic;
...@@ -15941,21 +15931,10 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p...@@ -15941,21 +15931,10 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
15941 ConstExprValue *fields = create_const_vals(1);15931 ConstExprValue *fields = create_const_vals(1);
15942 payload->data.x_struct.fields = fields;15932 payload->data.x_struct.fields = fields;
1594315933
15944 if (parent->type->id == TypeTableEntryIdStruct)15934 ir_type_info_struct_set_parent(payload, parent);
15945 {15935
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 }
15958 // child: &TypeInfo15936 // child: &TypeInfo
15937 ensure_field_index(payload->type, "child", 0);
15959 ConstExprValue *type_info_type = get_builtin_value(ira->codegen, "TypeInfo");15938 ConstExprValue *type_info_type = get_builtin_value(ira->codegen, "TypeInfo");
15960 assert(type_info_type->type->id == TypeTableEntryIdMetaType);15939 assert(type_info_type->type->id == TypeTableEntryIdMetaType);
15961 fields[0].special = ConstValSpecialStatic;15940 fields[0].special = ConstValSpecialStatic;
...@@ -15982,21 +15961,10 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p...@@ -15982,21 +15961,10 @@ static ConstExprValue *ir_make_type_info_value(IrAnalyze *ira, ConstExprValue *p
15982 ConstExprValue *fields = create_const_vals(1);15961 ConstExprValue *fields = create_const_vals(1);
15983 payload->data.x_struct.fields = fields;15962 payload->data.x_struct.fields = fields;
1598415963
15985 if (parent->type->id == TypeTableEntryIdStruct)15964 ir_type_info_struct_set_parent(payload, parent);
15986 {15965
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 }
15999 // child: ?&TypeInfo15966 // child: ?&TypeInfo
15967 ensure_field_index(payload->type, "child", 0);
16000 ConstExprValue *type_info_type = get_builtin_value(ira->codegen, "TypeInfo");15968 ConstExprValue *type_info_type = get_builtin_value(ira->codegen, "TypeInfo");
16001 assert(type_info_type->type->id == TypeTableEntryIdMetaType);15969 assert(type_info_type->type->id == TypeTableEntryIdMetaType);
1600215970
...@@ -16046,7 +16014,6 @@ static TypeTableEntry *ir_analyze_instruction_type_info(IrAnalyze *ira,...@@ -16046,7 +16014,6 @@ static TypeTableEntry *ir_analyze_instruction_type_info(IrAnalyze *ira,
16046 assert(var_value->type->id == TypeTableEntryIdMetaType);16014 assert(var_value->type->id == TypeTableEntryIdMetaType);
16047 TypeTableEntry *result_type = var_value->data.x_type;16015 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.
16050 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);16017 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
16051 out_val->type = result_type;16018 out_val->type = result_type;
16052 bigint_init_unsigned(&out_val->data.x_union.tag, type_id_index(type_entry->id));16019 bigint_init_unsigned(&out_val->data.x_union.tag, type_id_index(type_entry->id));