Allow undefined --- as a struct field default value.

This commit is contained in:
Ginger Bill
2017-07-18 15:09:24 +01:00
parent 193c7c82c8
commit 277ef1a68f
6 changed files with 37 additions and 36 deletions
+11 -6
View File
@@ -879,15 +879,20 @@ void check_record_field_decl(Checker *c, AstNode *decl, Array<Entity *> *fields,
e->identifier = name;
if (name_field_index < default_values.count) {
Operand op = default_values[name_field_index++];
check_init_variable(c, e, &op, str_lit("struct field assignment"));
if (is_operand_nil(op)) {
Operand a = default_values[name_field_index];
Operand b = default_values[name_field_index];
check_init_variable(c, e, &b, str_lit("struct field assignment"));
if (is_operand_nil(a)) {
e->Variable.default_is_nil = true;
} else if (op.mode != Addressing_Constant) {
error(op.expr, "Default field parameter must be a constant");
} else if (is_operand_undef(a)) {
e->Variable.default_is_undef = true;
} else if (b.mode != Addressing_Constant) {
error(b.expr, "Default field parameter must be a constant");
} else {
e->Variable.default_value = op.value;
e->Variable.default_value = b.value;
}
name_field_index++;
} else {
GB_ASSERT(type != nullptr);
}
+3 -1
View File
@@ -166,7 +166,9 @@ bool is_operand_value(Operand o) {
bool is_operand_nil(Operand o) {
return o.mode == Addressing_Value && o.type == t_untyped_nil;
}
bool is_operand_undef(Operand o) {
return o.mode == Addressing_Value && o.type == t_untyped_undef;
}
struct BlockLabel {
String name;
+1
View File
@@ -87,6 +87,7 @@ struct Entity {
i32 field_src_index;
ExactValue default_value;
bool default_is_nil;
bool default_is_undef;
bool default_is_location;
bool is_immutable;
bool is_thread_local;
+2
View File
@@ -671,6 +671,8 @@ bool ir_type_has_default_values(Type *t) {
continue;
} else if (f->Variable.default_value.kind != ExactValue_Invalid) {
return true;
} else if (f->Variable.default_is_undef) {
return true;
}
}
}
+17 -7
View File
@@ -648,9 +648,14 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type *
for (isize i = 0; i < value_count; i++) {
if (i > 0) ir_fprintf(f, ", ");
Type *elem_type = type->Record.fields[i]->type;
Entity *e = type->Record.fields[i];
ir_print_compound_element(f, m, values[i], elem_type);
if (!visited[i] && e->Variable.default_is_undef) {
ir_print_type(f, m, e->type);
ir_fprintf(f, " undef");
} else {
ir_print_compound_element(f, m, values[i], e->type);
}
}
@@ -682,12 +687,17 @@ void ir_print_exact_value(irFileBuffer *f, irModule *m, ExactValue value, Type *
for (isize i = 0; i < value_count; i++) {
if (i > 0) ir_fprintf(f, ", ");
Entity *field = type->Record.fields[i];
ExactValue value = {};
if (!field->Variable.default_is_nil) {
value = field->Variable.default_value;
Entity *e = type->Record.fields[i];
if (e->Variable.default_is_undef) {
ir_print_type(f, m, e->type);
ir_fprintf(f, " undef");
} else {
ExactValue value = {};
if (!e->Variable.default_is_nil) {
value = e->Variable.default_value;
}
ir_print_compound_element(f, m, value, e->type);
}
ir_print_compound_element(f, m, value, field->type);
}
ir_fprintf(f, "}");