From 25761570f1bb4413020ebbfc959caa3429453517 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 26 Feb 2017 23:50:04 -0500 Subject: more robust const struct values --- src/analyze.cpp | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'src/analyze.cpp') diff --git a/src/analyze.cpp b/src/analyze.cpp index 842ddbdf48..03da4a26c7 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -3485,10 +3485,11 @@ void init_const_undefined(CodeGen *g, ConstExprValue *const_val) { ConstExprValue *element_val = &const_val->data.x_array.elements[i]; element_val->type = canon_wanted_type->data.array.child_type; init_const_undefined(g, element_val); - if (get_underlying_type(element_val->type)->id == TypeTableEntryIdArray) { - element_val->data.x_array.parent.id = ConstParentIdArray; - element_val->data.x_array.parent.data.p_array.array_val = const_val; - element_val->data.x_array.parent.data.p_array.elem_index = i; + ConstParent *parent = get_const_val_parent(element_val); + if (parent != nullptr) { + parent->id = ConstParentIdArray; + parent->data.p_array.array_val = const_val; + parent->data.p_array.elem_index = i; } } } else if (canon_wanted_type->id == TypeTableEntryIdStruct) { @@ -3502,6 +3503,12 @@ void init_const_undefined(CodeGen *g, ConstExprValue *const_val) { field_val->type = canon_wanted_type->data.structure.fields[i].type_entry; assert(field_val->type); init_const_undefined(g, field_val); + ConstParent *parent = get_const_val_parent(field_val); + if (parent != nullptr) { + parent->id = ConstParentIdStruct; + parent->data.p_struct.struct_val = const_val; + parent->data.p_struct.field_index = i; + } } } else { const_val->special = ConstValSpecialUndef; @@ -4068,3 +4075,14 @@ bool zig_llvm_fn_key_eql(ZigLLVMFnKey a, ZigLLVMFnKey b) { } zig_unreachable(); } + +ConstParent *get_const_val_parent(ConstExprValue *value) { + assert(value->type); + TypeTableEntry *canon_type = get_underlying_type(value->type); + if (canon_type->id == TypeTableEntryIdArray) { + return &value->data.x_array.parent; + } else if (canon_type->id == TypeTableEntryIdStruct) { + return &value->data.x_struct.parent; + } + return nullptr; +} -- cgit v1.2.3