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

View File

@@ -110,7 +110,7 @@ get_hash :: proc(s: string) -> u32 {
}
/*
Vector :: struct(N: int, T: type) {
using _: raw_union {
using e: [N]T;
@@ -140,29 +140,13 @@ foo1 :: proc(a: type/Vector) { fmt.println("foo1", a{}); }
foo3 :: proc(a: type/Vector(3, $T)) {fmt.println("foo3", a{}); }
// foo4 :: proc(a: type/Vector3) {}
*/
foo :: proc() -> (f32, f32) {
return 1, 2;
}
main :: proc() {
Vector3 :: struct {
x: f32 = 1;
y: f32 = 4;
z: f32 = 9;
}
v := make([dynamic]Vector3, 3);
array: [100]Vector3;
v2 := array[50];
fmt.println(v2);
/* foo1(Vector(3, f32));
foo1(Vector(3, f32));
foo1(Vector3);
foo3(Vector(3, f32));
foo3(Vector3);
@@ -179,9 +163,7 @@ main :: proc() {
v := add(a, b);
fmt.println(v.v);
*/
/*
table: Table(string, int);
for i in 0..36 do put(&table, "Hellope", i);
@@ -193,5 +175,4 @@ main :: proc() {
found, _ = find(&table, "World!");
fmt.printf("found is %v\n", found);
*/
}

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);
}

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;

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;

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;
}
}
}

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, "}");