[vala] Fix struct initialization with member initializers



commit eb15c9844769c26cd4bf2196424941772dfee0a7
Author: Jürg Billeter <j bitron ch>
Date:   Wed Mar 24 21:01:13 2010 +0100

    Fix struct initialization with member initializers
    
    Fixes bug 613825.

 codegen/valaccodebasemodule.vala |   24 ++++++++++++++++--------
 tests/Makefile.am                |    1 +
 tests/structs/bug613825.vala     |    8 ++++++++
 3 files changed, 25 insertions(+), 8 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 223d49a..c937531 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -2022,6 +2022,17 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 		}
 	}
 
+	bool has_simple_struct_initializer (LocalVariable local) {
+		var st = local.variable_type.data_type as Struct;
+		var initializer = local.initializer as ObjectCreationExpression;
+		if (st != null && (!st.is_simple_type () || st.get_cname () == "va_list") && !local.variable_type.nullable &&
+		    initializer != null && initializer.get_object_initializer ().size == 0) {
+			return true;
+		} else {
+			return false;
+		}
+	}
+
 	public override void visit_local_variable (LocalVariable local) {
 		check_type (local.variable_type);
 
@@ -2166,8 +2177,7 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 			closure_struct.add_field (local.variable_type.get_cname (), get_variable_cname (local.name) + local.variable_type.get_cdeclarator_suffix ());
 
 			if (local.initializer != null) {
-				var st = local.variable_type.data_type as Struct;
-				if (st != null && (!st.is_simple_type () || st.get_cname () == "va_list") && !local.variable_type.nullable && local.initializer is ObjectCreationExpression) {
+				if (has_simple_struct_initializer (local)) {
 					cfrag.append (new CCodeExpressionStatement (rhs));
 				} else {
 					cfrag.append (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), get_variable_cname (local.name)), rhs)));
@@ -2175,8 +2185,7 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 			}
 		} else {
 			CCodeStatement post_stmt = null;
-			var st = local.variable_type.data_type as Struct;
-			if (st != null && (!st.is_simple_type () || st.get_cname () == "va_list") && !local.variable_type.nullable && local.initializer is ObjectCreationExpression) {
+			if (has_simple_struct_initializer (local)) {
 				post_stmt = new CCodeExpressionStatement (rhs);
 				rhs = null;
 			}
@@ -2999,8 +3008,7 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 		LocalVariable full_expr_var = null;
 
 		var local_decl = expr.parent_node as LocalVariable;
-		var st = local_decl != null ? local_decl.variable_type.data_type as Struct : null;
-		if (st != null && !st.is_simple_type () && !local_decl.variable_type.nullable) {
+		if (local_decl != null && has_simple_struct_initializer (local_decl)) {
 			expr_list.append_expression ((CCodeExpression) expr.ccodenode);
 		} else {
 			var expr_type = expr.value_type;
@@ -4000,7 +4008,7 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 			// value-type initialization or object creation expression with object initializer
 
 			var local = expr.parent_node as LocalVariable;
-			if (local != null && !local.variable_type.nullable) {
+			if (local != null && has_simple_struct_initializer (local)) {
 				instance = get_variable_cexpression (get_variable_cname (local.name));
 			} else {
 				var temp_decl = get_temp_variable (expr.type_reference, false, expr);
@@ -4206,7 +4214,7 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 		}
 
 		var local = expr.parent_node as LocalVariable;
-		if (st != null && (!st.is_simple_type () || st.get_cname () == "va_list") && local != null && !local.variable_type.nullable) {
+		if (local != null && has_simple_struct_initializer (local)) {
 			// no comma expression necessary
 			expr.ccodenode = creation_expr;
 		} else if (instance != null) {
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 661de87..04e3980 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -46,6 +46,7 @@ TESTS = \
 	structs/bug595587.vala \
 	structs/bug606202.vala \
 	structs/bug613513.vala \
+	structs/bug613825.vala \
 	delegates/delegates.vala \
 	delegates/bug595610.vala \
 	delegates/bug595639.vala \
diff --git a/tests/structs/bug613825.vala b/tests/structs/bug613825.vala
new file mode 100644
index 0000000..fc6cdd0
--- /dev/null
+++ b/tests/structs/bug613825.vala
@@ -0,0 +1,8 @@
+struct Foo {
+	int i;
+}
+
+void main () {
+	var foo = Foo () { i = 42 };
+	assert (foo.i == 42);
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]