aboutsummaryrefslogtreecommitdiff
path: root/src/ir.cpp
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2019-12-08 11:40:17 -0500
committerAndrew Kelley <andrew@ziglang.org>2019-12-08 12:26:20 -0500
commit119ed128c05e95a4779a00cd87d3e2d5b01f9f17 (patch)
treed0623cbce915223027051e9c79495ec25aa0a81b /src/ir.cpp
parent19c1b5a33a21bdddfbbca3c65b1c0e6419c4629f (diff)
downloadzig-119ed128c05e95a4779a00cd87d3e2d5b01f9f17.tar.gz
zig-119ed128c05e95a4779a00cd87d3e2d5b01f9f17.zip
implement comptime struct fields
Diffstat (limited to 'src/ir.cpp')
-rw-r--r--src/ir.cpp20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/ir.cpp b/src/ir.cpp
index 9b99fed42c..5f398bb11a 100644
--- a/src/ir.cpp
+++ b/src/ir.cpp
@@ -655,6 +655,10 @@ static ZigValue *const_ptr_pointee_unchecked(CodeGen *g, ZigValue *const_val) {
if (isf != nullptr) {
TypeStructField *field = find_struct_type_field(isf->inferred_struct_type, isf->field_name);
assert(field != nullptr);
+ if (field->is_comptime) {
+ assert(field->init_val != nullptr);
+ return field->init_val;
+ }
assert(const_val->data.x_ptr.special == ConstPtrSpecialRef);
ZigValue *struct_val = const_val->data.x_ptr.data.ref.pointee;
return struct_val->data.x_struct.fields[field->src_index];
@@ -17373,6 +17377,13 @@ static IrInstruction *ir_analyze_store_ptr(IrAnalyze *ira, IrInstruction *source
field->type_val = create_const_type(ira->codegen, field->type_entry);
field->src_index = old_field_count;
field->decl_node = uncasted_value->source_node;
+ if (instr_is_comptime(uncasted_value)) {
+ ZigValue *uncasted_val = ir_resolve_const(ira, uncasted_value, UndefOk);
+ field->is_comptime = true;
+ field->init_val = create_const_vals(1);
+ copy_const_val(field->init_val, uncasted_val);
+ return ir_const_void(ira, source_instr);
+ }
ZigType *struct_ptr_type = get_pointer_to_type(ira->codegen, isf->inferred_struct_type, false);
IrInstruction *casted_ptr;
@@ -19510,6 +19521,11 @@ static IrInstruction *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInstruction
ZigType *field_type = resolve_struct_field_type(ira->codegen, field);
if (field_type == nullptr)
return ira->codegen->invalid_instruction;
+ if (field->is_comptime) {
+ IrInstruction *elem = ir_const(ira, source_instr, field_type);
+ copy_const_val(elem->value, field->init_val);
+ return ir_get_ref(ira, source_instr, elem, true, false);
+ }
switch (type_has_one_possible_value(ira->codegen, field_type)) {
case OnePossibleValueInvalid:
return ira->codegen->invalid_instruction;
@@ -21716,6 +21732,10 @@ static IrInstruction *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
for (size_t i = 0; i < const_ptrs.length; i += 1) {
IrInstruction *elem_result_loc = const_ptrs.at(i);
assert(elem_result_loc->value->special == ConstValSpecialStatic);
+ if (elem_result_loc->value->type->data.pointer.inferred_struct_field != nullptr) {
+ // This field will be generated comptime; no need to do this.
+ continue;
+ }
IrInstruction *deref = ir_get_deref(ira, elem_result_loc, elem_result_loc, nullptr);
elem_result_loc->value->special = ConstValSpecialRuntime;
ir_analyze_store_ptr(ira, elem_result_loc, elem_result_loc, deref, false);