From 277ef1a68f932988cd59b40e496b2b1532907654 Mon Sep 17 00:00:00 2001 From: Ginger Bill Date: Tue, 18 Jul 2017 15:09:24 +0100 Subject: [PATCH] Allow undefined --- as a struct field default value. --- code/demo.odin | 25 +++---------------------- src/check_expr.cpp | 17 +++++++++++------ src/checker.cpp | 4 +++- src/entity.cpp | 1 + src/ir.cpp | 2 ++ src/ir_print.cpp | 24 +++++++++++++++++------- 6 files changed, 37 insertions(+), 36 deletions(-) diff --git a/code/demo.odin b/code/demo.odin index 0dc3448bf..cc3997435 100644 --- a/code/demo.odin +++ b/code/demo.odin @@ -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); -*/ } diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 47944e772..1dad15c22 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -879,15 +879,20 @@ void check_record_field_decl(Checker *c, AstNode *decl, Array *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); } diff --git a/src/checker.cpp b/src/checker.cpp index 1cbf5db40..cdc9bf3b4 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -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; diff --git a/src/entity.cpp b/src/entity.cpp index a9b083400..d2c29ca98 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -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; diff --git a/src/ir.cpp b/src/ir.cpp index e87e33559..c525419af 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -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; } } } diff --git a/src/ir_print.cpp b/src/ir_print.cpp index d6fa61a10..3a3eaaced 100644 --- a/src/ir_print.cpp +++ b/src/ir_print.cpp @@ -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, "}");