Browse Source

Allow for NO_DEFAULT_STRUCT_VALUES
(will decide later if I want them or not)

gingerBill 7 years ago
parent
commit
780b81a59f
4 changed files with 13 additions and 68 deletions
  1. 5 63
      examples/demo.odin
  2. 1 1
      src/ir.cpp
  3. 1 0
      src/main.cpp
  4. 6 4
      src/parser.cpp

+ 5 - 63
examples/demo.odin

@@ -111,67 +111,6 @@ general_stuff :: proc() {
 	}
 	}
 }
 }
 
 
-default_struct_values :: proc() {
-	fmt.println("# default_struct_values");
-	{
-		Vector3 :: struct {
-			x: f32,
-			y: f32,
-			z: f32,
-		}
-		v: Vector3;
-		fmt.println(v);
-	}
-	{
-		// Default values must be constants
-		Vector3 :: struct {
-			x: f32 = 1,
-			y: f32 = 4,
-			z: f32 = 9,
-		}
-		v: Vector3;
-		fmt.println(v);
-
-		v = Vector3{};
-		fmt.println(v);
-
-		// Uses the same semantics as a default values in a procedure
-		v = Vector3{137};
-		fmt.println(v);
-
-		v = Vector3{z = 137};
-		fmt.println(v);
-	}
-
-	{
-		Vector3 :: struct {
-			x := 1.0,
-			y := 4.0,
-			z := 9.0,
-		}
-		stack_default: Vector3;
-		stack_literal := Vector3{};
-		heap_one      := new(Vector3);         defer free(heap_one);
-		heap_two      := new_clone(Vector3{}); defer free(heap_two);
-
-		fmt.println("stack_default  - ", stack_default);
-		fmt.println("stack_literal  - ", stack_literal);
-		fmt.println("heap_one       - ", heap_one^);
-		fmt.println("heap_two       - ", heap_two^);
-
-
-		N :: 4;
-		stack_array: [N]Vector3;
-		heap_array := new([N]Vector3);    defer free(heap_array);
-		heap_slice := make([]Vector3, N); defer free(heap_slice);
-		fmt.println("stack_array[1] - ", stack_array[1]);
-		fmt.println("heap_array[1]  - ", heap_array[1]);
-		fmt.println("heap_slice[1]  - ", heap_slice[1]);
-	}
-}
-
-
-
 
 
 union_type :: proc() {
 union_type :: proc() {
 	fmt.println("\n# union_type");
 	fmt.println("\n# union_type");
@@ -384,7 +323,11 @@ parametric_polymorphism :: proc() {
 	}
 	}
 
 
 	copy_slice :: proc(dst, src: []$T) -> int {
 	copy_slice :: proc(dst, src: []$T) -> int {
-		return mem.copy(&dst[0], &src[0], n*size_of(T));
+		n := min(len(dst), len(src));
+		if n > 0 {
+			mem.copy(&dst[0], &src[0], n*size_of(T));
+		}
+		return n;
 	}
 	}
 
 
 	double_params :: proc(a: $A, b: $B) -> A {
 	double_params :: proc(a: $A, b: $B) -> A {
@@ -795,7 +738,6 @@ deprecated_attribute :: proc() {
 main :: proc() {
 main :: proc() {
 	when true {
 	when true {
 		general_stuff();
 		general_stuff();
-		default_struct_values();
 		union_type();
 		union_type();
 		parametric_polymorphism();
 		parametric_polymorphism();
 		threading_example();
 		threading_example();

+ 1 - 1
src/ir.cpp

@@ -684,7 +684,7 @@ Type *ir_type(irValue *value) {
 
 
 
 
 bool ir_type_has_default_values(Type *t) {
 bool ir_type_has_default_values(Type *t) {
-#if 1
+#if !defined(NO_DEFAULT_STRUCT_VALUES)
 	switch (t->kind) {
 	switch (t->kind) {
 	case Type_Named:
 	case Type_Named:
 		return ir_type_has_default_values(t->Named.base);
 		return ir_type_has_default_values(t->Named.base);

+ 1 - 0
src/main.cpp

@@ -1,5 +1,6 @@
 // #define NO_ARRAY_BOUNDS_CHECK
 // #define NO_ARRAY_BOUNDS_CHECK
 #define NO_POINTER_ARITHMETIC
 #define NO_POINTER_ARITHMETIC
+// #define NO_DEFAULT_STRUCT_VALUES
 
 
 #include "common.cpp"
 #include "common.cpp"
 #include "timings.cpp"
 #include "timings.cpp"

+ 6 - 4
src/parser.cpp

@@ -2959,10 +2959,12 @@ AstNode *parse_field_list(AstFile *f, isize *name_count_, u32 allowed_flags, Tok
 			syntax_error(f->curr_token, "Default parameters can only be applied to single values");
 			syntax_error(f->curr_token, "Default parameters can only be applied to single values");
 		}
 		}
 
 
-		// if (allowed_flags == FieldFlag_Struct && default_value != nullptr) {
-		// 	syntax_error(default_value, "Default parameters are not allowed for structs");
-		// 	default_value = nullptr;
-		// }
+	#if defined(NO_DEFAULT_STRUCT_VALUES)
+		if (allowed_flags == FieldFlag_Struct && default_value != nullptr) {
+			syntax_error(default_value, "Default parameters are not allowed for structs");
+			default_value = nullptr;
+		}
+	#endif
 
 
 		if (type != nullptr && type->kind == AstNode_Ellipsis) {
 		if (type != nullptr && type->kind == AstNode_Ellipsis) {
 			if (seen_ellipsis) syntax_error(type, "Extra variadic parameter after ellipsis");
 			if (seen_ellipsis) syntax_error(type, "Extra variadic parameter after ellipsis");