]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Fix struct initialization with member initializers
authorJürg Billeter <j@bitron.ch>
Wed, 24 Mar 2010 20:01:13 +0000 (21:01 +0100)
committerJürg Billeter <j@bitron.ch>
Wed, 24 Mar 2010 20:01:13 +0000 (21:01 +0100)
Fixes bug 613825.

codegen/valaccodebasemodule.vala
tests/Makefile.am
tests/structs/bug613825.vala [new file with mode: 0644]

index 223d49a2128d9d68efb2846b6a62a32043b32363..c937531ea81f7c1c150204926faadbb8b44f671b 100644 (file)
@@ -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) {
index 661de874f173c6c59a49fc1b90d08301ecb8bceb..04e3980a84a8a7da85388bd6f5fd937ac4799fe0 100644 (file)
@@ -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 (file)
index 0000000..fc6cdd0
--- /dev/null
@@ -0,0 +1,8 @@
+struct Foo {
+       int i;
+}
+
+void main () {
+       var foo = Foo () { i = 42 };
+       assert (foo.i == 42);
+}