]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Make copy_value be effect-free
authorLuca Bruno <lucabru@src.gnome.org>
Fri, 17 Jun 2011 13:07:55 +0000 (15:07 +0200)
committerLuca Bruno <lucabru@src.gnome.org>
Wed, 6 Jul 2011 20:32:30 +0000 (22:32 +0200)
Fixes bug 634782.

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

index 7c9a8acff29857e6b0c0be00b17d465a939469e8..85a8a2f0d8969f9a647e22f675e80a0d9417e69d 100644 (file)
@@ -3877,7 +3877,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                        var ccall = new CCodeFunctionCall (new CCodeIdentifier (dup0_func));
                        ccall.add_argument (cexpr);
                        result.cvalue = ccall;
-                       return result;
+                       return store_temp_value (result, node);
                }
 
                var ccall = new CCodeFunctionCall (dupexpr);
@@ -3886,25 +3886,20 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                        // expression is non-null
                        ccall.add_argument (cexpr);
                        
-                       return new GLibValue (type, ccall);
+                       return store_temp_value (new GLibValue (type, ccall), node);
                } else {
-                       var decl = get_temp_variable (type, false, node, false);
-                       emit_temp_var (decl);
-
-                       var ctemp = get_variable_cexpression (decl.name);
-                       
-                       var cisnull = new CCodeBinaryExpression (CCodeBinaryOperator.EQUALITY, ctemp, new CCodeConstant ("NULL"));
+                       var cnotnull = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, cexpr, new CCodeConstant ("NULL"));
                        if (type.type_parameter != null) {
                                // dup functions are optional for type parameters
-                               var cdupisnull = new CCodeBinaryExpression (CCodeBinaryOperator.EQUALITY, get_dup_func_expression (type, node.source_reference), new CCodeConstant ("NULL"));
-                               cisnull = new CCodeBinaryExpression (CCodeBinaryOperator.OR, cisnull, cdupisnull);
+                               var cdupnotnull = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, get_dup_func_expression (type, node.source_reference), new CCodeConstant ("NULL"));
+                               cnotnull = new CCodeBinaryExpression (CCodeBinaryOperator.AND, cnotnull, cdupnotnull);
                        }
 
                        if (type.type_parameter != null) {
                                // cast from gconstpointer to gpointer as GBoxedCopyFunc expects gpointer
-                               ccall.add_argument (new CCodeCastExpression (ctemp, "gpointer"));
+                               ccall.add_argument (new CCodeCastExpression (cexpr, "gpointer"));
                        } else {
-                               ccall.add_argument (ctemp);
+                               ccall.add_argument (cexpr);
                        }
 
                        if (type is ArrayType) {
@@ -3920,9 +3915,6 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                                }
                        }
 
-                       var ccomma = new CCodeCommaExpression ();
-                       ccomma.append_expression (new CCodeAssignment (ctemp, cexpr));
-
                        CCodeExpression cifnull;
                        if (type.data_type != null) {
                                cifnull = new CCodeConstant ("NULL");
@@ -3932,18 +3924,18 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 
                                // cast from gconstpointer to gpointer as methods in
                                // generic classes may not return gconstpointer
-                               cifnull = new CCodeCastExpression (ctemp, "gpointer");
+                               cifnull = new CCodeCastExpression (cexpr, "gpointer");
                        }
-                       ccomma.append_expression (new CCodeConditionalExpression (cisnull, cifnull, ccall));
 
-                       // repeat temp variable at the end of the comma expression
-                       // if the ref function returns void
                        if (is_ref_function_void (type)) {
-                               ccomma.append_expression (ctemp);
+                               ccode.open_if (cnotnull);
+                               ccode.add_expression (ccall);
+                               ccode.close ();
+                       } else {
+                               var ccond = new CCodeConditionalExpression (cnotnull, ccall, cifnull);
+                               result.cvalue = ccond;
+                               result = (GLibValue) store_temp_value (result, node, true);
                        }
-
-                       result.value_type = decl.variable_type;
-                       result.cvalue = ccomma;
                        return result;
                }
        }
index ccf1fa8f25e99640d6c43ff3c50d18ae23db8565..708132af0db64762d4526ca4ff18025611093d58 100644 (file)
@@ -94,6 +94,7 @@ TESTS = \
        objects/bug624594.vala \
        objects/bug626038.vala \
        objects/bug628639.vala \
+       objects/bug634782.vala \
        objects/bug642809.vala \
        objects/bug643711.vala \
        objects/bug653138.vala \
diff --git a/tests/objects/bug634782.vala b/tests/objects/bug634782.vala
new file mode 100644 (file)
index 0000000..5ffaa47
--- /dev/null
@@ -0,0 +1,9 @@
+public class Foo {
+        public uint8[] baz;
+        public Foo bar () { return new Foo (); }
+}
+
+void main () {
+        Foo foo = new Foo ();
+        var bar = foo.bar().baz;
+}