Browse Source

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

Ginger Bill 8 years ago
parent
commit
277ef1a68f
6 changed files with 37 additions and 36 deletions
  1. 3 22
      code/demo.odin
  2. 11 6
      src/check_expr.cpp
  3. 3 1
      src/checker.cpp
  4. 1 0
      src/entity.cpp
  5. 2 0
      src/ir.cpp
  6. 17 7
      src/ir_print.cpp

+ 3 - 22
code/demo.odin

@@ -110,7 +110,7 @@ get_hash :: proc(s: string) -> u32 {
 }
 }
 
 
 
 
-/*
+
 Vector :: struct(N: int, T: type) {
 Vector :: struct(N: int, T: type) {
 	using _: raw_union {
 	using _: raw_union {
 		using e: [N]T;
 		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{}); }
 foo3 :: proc(a: type/Vector(3, $T))  {fmt.println("foo3", a{}); }
 // foo4 :: proc(a: type/Vector3)        {}
 // 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));
+main :: proc() {
+	foo1(Vector(3, f32));
 	foo1(Vector3);
 	foo1(Vector3);
 	foo3(Vector(3, f32));
 	foo3(Vector(3, f32));
 	foo3(Vector3);
 	foo3(Vector3);
@@ -179,9 +163,7 @@ main :: proc() {
 
 
 	v := add(a, b);
 	v := add(a, b);
 	fmt.println(v.v);
 	fmt.println(v.v);
-*/
 
 
-/*
 	table: Table(string, int);
 	table: Table(string, int);
 
 
 	for i in 0..36 do put(&table, "Hellope", i);
 	for i in 0..36 do put(&table, "Hellope", i);
@@ -193,5 +175,4 @@ main :: proc() {
 
 
 	found, _ = find(&table, "World!");
 	found, _ = find(&table, "World!");
 	fmt.printf("found is %v\n", found);
 	fmt.printf("found is %v\n", found);
-*/
 }
 }

+ 11 - 6
src/check_expr.cpp

@@ -879,15 +879,20 @@ void check_record_field_decl(Checker *c, AstNode *decl, Array<Entity *> *fields,
 		e->identifier = name;
 		e->identifier = name;
 
 
 		if (name_field_index < default_values.count) {
 		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;
 				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 {
 			} else {
-				e->Variable.default_value = op.value;
+				e->Variable.default_value = b.value;
 			}
 			}
+
+			name_field_index++;
 		} else {
 		} else {
 			GB_ASSERT(type != nullptr);
 			GB_ASSERT(type != nullptr);
 		}
 		}

+ 3 - 1
src/checker.cpp

@@ -166,7 +166,9 @@ bool is_operand_value(Operand o) {
 bool is_operand_nil(Operand o) {
 bool is_operand_nil(Operand o) {
 	return o.mode == Addressing_Value && o.type == t_untyped_nil;
 	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 {
 struct BlockLabel {
 	String   name;
 	String   name;

+ 1 - 0
src/entity.cpp

@@ -87,6 +87,7 @@ struct Entity {
 			i32        field_src_index;
 			i32        field_src_index;
 			ExactValue default_value;
 			ExactValue default_value;
 			bool       default_is_nil;
 			bool       default_is_nil;
+			bool       default_is_undef;
 			bool       default_is_location;
 			bool       default_is_location;
 			bool       is_immutable;
 			bool       is_immutable;
 			bool       is_thread_local;
 			bool       is_thread_local;

+ 2 - 0
src/ir.cpp

@@ -671,6 +671,8 @@ bool ir_type_has_default_values(Type *t) {
 					continue;
 					continue;
 				} else if (f->Variable.default_value.kind != ExactValue_Invalid) {
 				} else if (f->Variable.default_value.kind != ExactValue_Invalid) {
 					return true;
 					return true;
+				} else if (f->Variable.default_is_undef) {
+					return true;
 				}
 				}
 			}
 			}
 		}
 		}

+ 17 - 7
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++) {
 			for (isize i = 0; i < value_count; i++) {
 				if (i > 0) ir_fprintf(f, ", ");
 				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++) {
 				for (isize i = 0; i < value_count; i++) {
 					if (i > 0) ir_fprintf(f, ", ");
 					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, "}");
 				ir_fprintf(f, "}");