[vala/emitlocal: 4/15] codegen: Use create_temp_value in copy_value



commit 6d407db39d35eaf5a0e9ecd958c1991cdb8d4d69
Author: Luca Bruno <lucabru src gnome org>
Date:   Sat Jun 11 11:28:00 2011 +0200

    codegen: Use create_temp_value in copy_value
    
    Fixes bug 634782.

 codegen/valaccodebasemodule.vala |   40 ++++++++++++++++---------------------
 tests/Makefile.am                |    1 +
 tests/objects/bug634782.vala     |    9 ++++++++
 3 files changed, 27 insertions(+), 23 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 5b56b8e..a132b9e 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -3789,9 +3789,8 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 		if (type is ValueType && !type.nullable) {
 			// normal value type, no null check
 
-			var decl = get_temp_variable (type, false, node);
-			emit_temp_var (decl);
-			var ctemp = get_variable_cexpression (decl.name);
+			var temp_value = create_temp_value (type, true, node, true);
+			var ctemp = get_cvalue_ (temp_value);
 
 			var vt = (ValueType) type;
 			var st = (Struct) vt.type_symbol;
@@ -3822,15 +3821,13 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 				ccode.add_else ();
 
 				// g_value_init/copy must not be called for uninitialized values
-				store_local (decl, value, true);
+				store_value (temp_value, value);
 				ccode.close ();
 			} else {
 				ccode.add_expression (copy_call);
 			}
 
-			result.value_type = decl.variable_type;
-			result.cvalue = ctemp;
-			return result;
+			return temp_value;
 		}
 
 		/* (temp = expr, temp == NULL ? NULL : ref (temp))
@@ -3892,16 +3889,14 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 			
 			return new GLibValue (type, ccall);
 		} else {
-			var decl = get_temp_variable (type, false, node, false);
-			emit_temp_var (decl);
-
-			var ctemp = get_variable_cexpression (decl.name);
+			var temp_value = create_temp_value (type, false, node, true);
+			var ctemp = get_cvalue_ (temp_value);
 			
-			var cisnull = new CCodeBinaryExpression (CCodeBinaryOperator.EQUALITY, ctemp, new CCodeConstant ("NULL"));
+			var cnotnull = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, ctemp, 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) {
@@ -3924,8 +3919,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 				}
 			}
 
-			var ccomma = new CCodeCommaExpression ();
-			ccomma.append_expression (new CCodeAssignment (ctemp, cexpr));
+			store_value (temp_value, value);
 
 			CCodeExpression cifnull;
 			if (type.data_type != null) {
@@ -3938,16 +3932,16 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 				// generic classes may not return gconstpointer
 				cifnull = new CCodeCastExpression (ctemp, "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
+			result = (GLibValue) temp_value;
 			if (is_ref_function_void (type)) {
-				ccomma.append_expression (ctemp);
+				// evaluate to the temp variable if the ref function returns void
+				ccode.open_if (cnotnull);
+				ccode.add_expression (ccall);
+				ccode.close ();
+			} else {
+				result.cvalue = new CCodeConditionalExpression (cnotnull, ccall, cifnull);
 			}
-
-			result.value_type = decl.variable_type;
-			result.cvalue = ccomma;
 			return result;
 		}
 	}
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 820f42b..2abeb05 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -85,6 +85,7 @@ TESTS = \
 	objects/bug620706.vala \
 	objects/bug624594.vala \
 	objects/bug628639.vala \
+	objects/bug634782.vala \
 	objects/bug642809.vala \
 	objects/bug643711.vala \
 	errors/errors.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]