[vala] codegen: Use store_local in visit_local_variable



commit fccb973576e493f71d3a5833be0af878fff8f7c6
Author: Jürg Billeter <j bitron ch>
Date:   Wed Jan 19 21:37:47 2011 +0100

    codegen: Use store_local in visit_local_variable
    
    Based on patch by Luca Bruno.

 codegen/valaccodeassignmentmodule.vala |   44 +++++++++++-----
 codegen/valaccodebasemodule.vala       |   84 ++------------------------------
 codegen/valadovaassignmentmodule.vala  |    8 ++--
 vala/valacodegenerator.vala            |    2 +-
 4 files changed, 39 insertions(+), 99 deletions(-)
---
diff --git a/codegen/valaccodeassignmentmodule.vala b/codegen/valaccodeassignmentmodule.vala
index 9c53c5a..cf8e217 100644
--- a/codegen/valaccodeassignmentmodule.vala
+++ b/codegen/valaccodeassignmentmodule.vala
@@ -171,8 +171,8 @@ public class Vala.CCodeAssignmentModule : CCodeMemberAccessModule {
 		}
 	}
 
-	void store_variable (Variable variable, TargetValue lvalue, TargetValue value) {
-		if (requires_destroy (variable.variable_type)) {
+	void store_variable (Variable variable, TargetValue lvalue, TargetValue value, bool initializer) {
+		if (!initializer && requires_destroy (variable.variable_type)) {
 			/* unref old value */
 			ccode.add_expression (destroy_value (lvalue));
 		}
@@ -181,17 +181,33 @@ public class Vala.CCodeAssignmentModule : CCodeMemberAccessModule {
 
 		var array_type = variable.variable_type as ArrayType;
 		if (array_type != null) {
-			for (int dim = 1; dim <= array_type.rank; dim++) {
-				if (get_array_length_cvalue (lvalue, dim) != null) {
-					ccode.add_assignment (get_array_length_cvalue (lvalue, dim), get_array_length_cvalue (value, dim));
+			if (array_type.fixed_length) {
+				cfile.add_include ("string.h");
+
+				// it is necessary to use memcpy for fixed-length (stack-allocated) arrays
+				// simple assignments do not work in C
+				var sizeof_call = new CCodeFunctionCall (new CCodeIdentifier ("sizeof"));
+				sizeof_call.add_argument (new CCodeIdentifier (array_type.element_type.get_cname ()));
+				var size = new CCodeBinaryExpression (CCodeBinaryOperator.MUL, new CCodeConstant ("%d".printf (array_type.length)), sizeof_call);
+
+				var ccopy = new CCodeFunctionCall (new CCodeIdentifier ("memcpy"));
+				ccopy.add_argument (get_cvalue_ (lvalue));
+				ccopy.add_argument (get_cvalue_ (value));
+				ccopy.add_argument (size);
+				ccode.add_expression (ccopy);
+			} else {
+				for (int dim = 1; dim <= array_type.rank; dim++) {
+					if (get_array_length_cvalue (lvalue, dim) != null) {
+						ccode.add_assignment (get_array_length_cvalue (lvalue, dim), get_array_length_cvalue (value, dim));
+					}
 				}
-			}
-			if (array_type.rank == 1) {
-				if (get_array_size_cvalue (lvalue) != null) {
-					if (get_array_size_cvalue (value) != null) {
-						ccode.add_assignment (get_array_size_cvalue (lvalue), get_array_size_cvalue (value));
-					} else {
-						ccode.add_assignment (get_array_size_cvalue (lvalue), get_array_length_cvalue (value, 1));
+				if (array_type.rank == 1) {
+					if (get_array_size_cvalue (lvalue) != null) {
+						if (get_array_size_cvalue (value) != null) {
+							ccode.add_assignment (get_array_size_cvalue (lvalue), get_array_size_cvalue (value));
+						} else {
+							ccode.add_assignment (get_array_size_cvalue (lvalue), get_array_length_cvalue (value, 1));
+						}
 					}
 				}
 			}
@@ -208,7 +224,7 @@ public class Vala.CCodeAssignmentModule : CCodeMemberAccessModule {
 		}
 	}
 
-	public override void store_local (LocalVariable local, TargetValue value) {
-		store_variable (local, get_local_cvalue (local), value);
+	public override void store_local (LocalVariable local, TargetValue value, bool initializer) {
+		store_variable (local, get_local_cvalue (local), value, initializer);
 	}
 }
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 09fc4e5..c0c2490 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -2066,86 +2066,10 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 		}
 	
 		if (rhs != null) {
-			var target_value = get_variable_cvalue (local);
-
-			if (local.variable_type is ArrayType) {
-				var array_type = (ArrayType) local.variable_type;
-
-				if (array_type.fixed_length) {
-					rhs = null;
-				} else {
-					for (int dim = 1; dim <= array_type.rank; dim++) {
-						var lhs_array_len = get_array_length_cvalue (target_value, dim);
-						var rhs_array_len = get_array_length_cexpression (local.initializer, dim);
-						ccode.add_assignment (lhs_array_len, rhs_array_len);
-					}
-					if (array_type.rank == 1 && !local.captured) {
-						var lhs_array_size = get_array_size_cvalue (target_value);
-						var rhs_array_len = get_array_length_cvalue (target_value, 1);
-						ccode.add_assignment (lhs_array_size, rhs_array_len);
-					}
-				}
-			} else if (local.variable_type is DelegateType) {
-				var deleg_type = (DelegateType) local.variable_type;
-				var d = deleg_type.delegate_symbol;
-				if (d.has_target) {
-					var lhs_delegate_target = get_delegate_target_cvalue (target_value);
-					var lhs_delegate_target_destroy_notify = get_delegate_target_destroy_notify_cvalue (target_value);
-
-					CCodeExpression rhs_delegate_target_destroy_notify;
-					var rhs_delegate_target = get_delegate_target_cexpression (local.initializer, out rhs_delegate_target_destroy_notify);
-					ccode.add_assignment (lhs_delegate_target, rhs_delegate_target);
-
-					if (deleg_type.value_owned) {
-						ccode.add_assignment (lhs_delegate_target_destroy_notify, rhs_delegate_target_destroy_notify);
-					}
-				}
-			}
-		}
-
-		if (local.captured) {
-			if (local.initializer != null) {
-				if (has_simple_struct_initializer (local)) {
-					ccode.add_expression (rhs);
-				} else {
-					ccode.add_assignment (new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (get_block_id ((Block) local.parent_symbol))), get_variable_cname (local.name)), rhs);
-				}
-			}
-		} else if (current_method != null && current_method.coroutine) {
-			if (local.initializer != null) {
-				if (has_simple_struct_initializer (local)) {
-					ccode.add_expression (rhs);
-				} else {
-					ccode.add_assignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), get_variable_cname (local.name)), rhs);
-				}
-			}
-		} else {
-			if (rhs != null) {
-				if (has_simple_struct_initializer (local)) {
-					ccode.add_expression (rhs);
-				} else {
-					ccode.add_assignment (get_variable_cexpression (local.name), rhs);
-				}
-			}
-		}
-
-		if (local.initializer != null && local.variable_type is ArrayType) {
-			var array_type = (ArrayType) local.variable_type;
-
-			if (array_type.fixed_length) {
-				cfile.add_include ("string.h");
-
-				// it is necessary to use memcpy for fixed-length (stack-allocated) arrays
-				// simple assignments do not work in C
-				var sizeof_call = new CCodeFunctionCall (new CCodeIdentifier ("sizeof"));
-				sizeof_call.add_argument (new CCodeIdentifier (array_type.element_type.get_cname ()));
-				var size = new CCodeBinaryExpression (CCodeBinaryOperator.MUL, new CCodeConstant ("%d".printf (array_type.length)), sizeof_call);
-
-				var ccopy = new CCodeFunctionCall (new CCodeIdentifier ("memcpy"));
-				ccopy.add_argument (get_variable_cexpression (local.name));
-				ccopy.add_argument (get_cvalue (local.initializer));
-				ccopy.add_argument (size);
-				ccode.add_expression (ccopy);
+			if (has_simple_struct_initializer (local)) {
+				ccode.add_expression (rhs);
+			} else {
+				store_local (local, local.initializer.target_value, true);
 			}
 		}
 
diff --git a/codegen/valadovaassignmentmodule.vala b/codegen/valadovaassignmentmodule.vala
index cf94b79..837c873 100644
--- a/codegen/valadovaassignmentmodule.vala
+++ b/codegen/valadovaassignmentmodule.vala
@@ -132,8 +132,8 @@ public class Vala.DovaAssignmentModule : DovaMemberAccessModule {
 		}
 	}
 
-	void store_variable (Variable variable, TargetValue lvalue, TargetValue value) {
-		if (requires_destroy (variable.variable_type)) {
+	void store_variable (Variable variable, TargetValue lvalue, TargetValue value, bool initializer) {
+		if (!initializer && requires_destroy (variable.variable_type)) {
 			/* unref old value */
 			ccode.add_expression (destroy_value (lvalue));
 		}
@@ -141,7 +141,7 @@ public class Vala.DovaAssignmentModule : DovaMemberAccessModule {
 		ccode.add_assignment (get_cvalue_ (lvalue), get_cvalue_ (value));
 	}
 
-	public override void store_local (LocalVariable local, TargetValue value) {
-		store_variable (local, get_local_cvalue (local), value);
+	public override void store_local (LocalVariable local, TargetValue value, bool initializer) {
+		store_variable (local, get_local_cvalue (local), value, initializer);
 	}
 }
diff --git a/vala/valacodegenerator.vala b/vala/valacodegenerator.vala
index 9c33fcd..65624f4 100644
--- a/vala/valacodegenerator.vala
+++ b/vala/valacodegenerator.vala
@@ -36,5 +36,5 @@ public abstract class Vala.CodeGenerator : CodeVisitor {
 
 	public abstract TargetValue load_local (LocalVariable local);
 
-	public abstract void store_local (LocalVariable local, TargetValue value);
+	public abstract void store_local (LocalVariable local, TargetValue value, bool initializer);
 }



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