[vala/wip/effectfree: 20/43] codegen: Make copy_value be effect-free



commit 03dc11a55e140fded511c988a5610bf96063c212
Author: Luca Bruno <lucabru src gnome org>
Date:   Fri Jun 17 15:07:55 2011 +0200

    codegen: Make copy_value be effect-free
    
    Fixes bug 634782.

 codegen/valaccodebasemodule.vala |   38 +++++++++++++++-----------------------
 tests/Makefile.am                |    1 +
 tests/objects/bug634782.vala     |    9 +++++++++
 3 files changed, 25 insertions(+), 23 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 4793122..f47d516 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -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;
 		}
 	}
diff --git a/tests/Makefile.am b/tests/Makefile.am
index b22848f..e89ce5d 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -93,6 +93,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
index 0000000..5ffaa47
--- /dev/null
+++ b/tests/objects/bug634782.vala
@@ -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;
+}



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