[vala] Only move initializer to declaration when reordering is safe



commit 3af113306cbe504c75cd1ec4a81fb0084b1b6dff
Author: Jürg Billeter <j bitron ch>
Date:   Sun Sep 27 10:03:24 2009 +0200

    Only move initializer to declaration when reordering is safe
    
    In general it is not safe to move non-constant initializer lists
    to the variable declaration at the beginning of the block.

 ccode/valaccodevariabledeclarator.vala |   18 +++++++++++++++---
 codegen/valaccodebasemodule.vala       |    3 +++
 codegen/valadbusclientmodule.vala      |    6 +++---
 codegen/valadbusmodule.vala            |    2 +-
 4 files changed, 22 insertions(+), 7 deletions(-)
---
diff --git a/ccode/valaccodevariabledeclarator.vala b/ccode/valaccodevariabledeclarator.vala
index fdb9dfa..cf3a9c2 100644
--- a/ccode/valaccodevariabledeclarator.vala
+++ b/ccode/valaccodevariabledeclarator.vala
@@ -41,12 +41,25 @@ public class Vala.CCodeVariableDeclarator : CCodeDeclarator {
 	 */
 	public string? declarator_suffix { get; set; }
 
+	/**
+	 * Initializer only used to zero memory, safe to initialize as part
+	 * of declaration at beginning of block instead of separate assignment.
+	 */
+	public bool init0 { get; set; }
+
 	public CCodeVariableDeclarator (string name, CCodeExpression? initializer = null, string? declarator_suffix = null) {
 		this.name = name;
 		this.initializer = initializer;
 		this.declarator_suffix = declarator_suffix;
 	}
 
+	public CCodeVariableDeclarator.zero (string name, CCodeExpression initializer, string? declarator_suffix = null) {
+		this.name = name;
+		this.initializer = initializer;
+		this.declarator_suffix = declarator_suffix;
+		this.init0 = true;
+	}
+
 	public override void write (CCodeWriter writer) {
 		writer.write_string (name);
 		if (declarator_suffix != null) {
@@ -65,15 +78,14 @@ public class Vala.CCodeVariableDeclarator : CCodeDeclarator {
 			writer.write_string (declarator_suffix);
 		}
 
-		// initializer lists can't be moved to a separate statement
-		if (initializer is CCodeInitializerList) {
+		if (init0) {
 			writer.write_string (" = ");
 			initializer.write (writer);
 		}
 	}
 
 	public override void write_initialization (CCodeWriter writer) {
-		if (initializer != null && !(initializer is CCodeInitializerList)) {
+		if (initializer != null && !init0) {
 			writer.write_indent (line);
 
 			writer.write_string (name);
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index d20c8fa..96835b2 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -2091,6 +2091,7 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 			// initialization not necessary for variables stored in closure
 			if (cvar.initializer == null) {
 				cvar.initializer = default_value_for_type (local.variable_type, true);
+				cvar.init0 = true;
 			}
 		}
 
@@ -2724,9 +2725,11 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 					clist.append (new CCodeConstant ("0"));
 
 					vardecl.initializer = clist;
+					vardecl.init0 = true;
 				} else if (local.variable_type.is_reference_type_or_type_parameter () ||
 				       local.variable_type.nullable) {
 					vardecl.initializer = new CCodeConstant ("NULL");
+					vardecl.init0 = true;
 				}
 			
 				cfrag.append (cdecl);
diff --git a/codegen/valadbusclientmodule.vala b/codegen/valadbusclientmodule.vala
index 0de4e17..2315853 100644
--- a/codegen/valadbusclientmodule.vala
+++ b/codegen/valadbusclientmodule.vala
@@ -293,7 +293,7 @@ internal class Vala.DBusClientModule : DBusModule {
 					cvalinit.append (new CCodeConstant ("0"));
 
 					var cval_decl = new CCodeDeclaration ("GValue");
-					cval_decl.add_declarator (new CCodeVariableDeclarator (val_name, cvalinit));
+					cval_decl.add_declarator (new CCodeVariableDeclarator.zero (val_name, cvalinit));
 					block.add_statement (cval_decl);
 
 					var val_ptr = new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier (val_name));
@@ -624,7 +624,7 @@ internal class Vala.DBusClientModule : DBusModule {
 		cvalinit.append (new CCodeConstant ("0"));
 
 		var cval_decl = new CCodeDeclaration ("GValue");
-		cval_decl.add_declarator (new CCodeVariableDeclarator ("gvalue", cvalinit));
+		cval_decl.add_declarator (new CCodeVariableDeclarator.zero ("gvalue", cvalinit));
 		block.add_statement (cval_decl);
 
 		var val_ptr = new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier ("gvalue"));
@@ -679,7 +679,7 @@ internal class Vala.DBusClientModule : DBusModule {
 		cvalinit.append (new CCodeConstant ("0"));
 
 		var cval_decl = new CCodeDeclaration ("GValue");
-		cval_decl.add_declarator (new CCodeVariableDeclarator ("gvalue", cvalinit));
+		cval_decl.add_declarator (new CCodeVariableDeclarator.zero ("gvalue", cvalinit));
 		block.add_statement (cval_decl);
 
 		var val_ptr = new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier ("gvalue"));
diff --git a/codegen/valadbusmodule.vala b/codegen/valadbusmodule.vala
index 1fbd473..ac6007d 100644
--- a/codegen/valadbusmodule.vala
+++ b/codegen/valadbusmodule.vala
@@ -373,7 +373,7 @@ internal class Vala.DBusModule : GAsyncModule {
 		cvalinit.append (new CCodeConstant ("0"));
 
 		var cdecl = new CCodeDeclaration ("GValue");
-		cdecl.add_declarator (new CCodeVariableDeclarator (temp_name, cvalinit));
+		cdecl.add_declarator (new CCodeVariableDeclarator.zero (temp_name, cvalinit));
 		fragment.append (cdecl);
 
 		cdecl = new CCodeDeclaration ("DBusMessageIter");



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