[vala/emit-j] codegen: Add destroy_local, destroy_parameter, and destroy_field



commit 8bdc01af37d50ff70c36c19a80b10b7395cf2539
Author: Jürg Billeter <j bitron ch>
Date:   Wed Feb 16 16:56:00 2011 +0100

    codegen: Add destroy_local, destroy_parameter, and destroy_field
    
    This is in line with load_* and store_* methods.

 codegen/valaccodearraymodule.vala        |    5 +-
 codegen/valaccodebasemodule.vala         |   83 ++++++++++++++++++-----------
 codegen/valaccodecontrolflowmodule.vala  |    2 +-
 codegen/valaccodememberaccessmodule.vala |   17 +-----
 codegen/valadbusclientmodule.vala        |    2 +-
 codegen/valadbusservermodule.vala        |    2 +-
 codegen/valagasyncmodule.vala            |    4 +-
 codegen/valagdbusclientmodule.vala       |    2 +-
 codegen/valagdbusservermodule.vala       |    2 +-
 9 files changed, 63 insertions(+), 56 deletions(-)
---
diff --git a/codegen/valaccodearraymodule.vala b/codegen/valaccodearraymodule.vala
index 9a998d6..3d9b0e9 100644
--- a/codegen/valaccodearraymodule.vala
+++ b/codegen/valaccodearraymodule.vala
@@ -449,13 +449,12 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
 		}
 	}
 
-	public override CCodeExpression destroy_variable (Variable variable, CCodeExpression? inner = null) {
+	public override CCodeExpression destroy_variable (Variable variable, TargetValue value) {
 		var array_type = variable.variable_type as ArrayType;
 		if (array_type != null && array_type.fixed_length) {
 			requires_array_free = true;
 
 			var ccall = new CCodeFunctionCall (get_destroy_func_expression (variable.variable_type));
-			var value = get_variable_cvalue (variable);
 
 			ccall = new CCodeFunctionCall (new CCodeIdentifier ("_vala_array_destroy"));
 			ccall.add_argument (get_cvalue_ (value));
@@ -465,7 +464,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
 			return ccall;
 		}
 
-		return base.destroy_variable (variable, inner);
+		return base.destroy_variable (variable, value);
 	}
 
 	public override CCodeExpression destroy_value (TargetValue value, bool is_macro_definition = false) {
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 35d098b..4bba12c 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -991,7 +991,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 				}
 
 				foreach (LocalVariable local in temp_ref_vars) {
-					ccode.add_expression (destroy_variable (local));
+					ccode.add_expression (destroy_local (local));
 				}
 
 				temp_ref_vars.clear ();
@@ -1045,7 +1045,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 				ccode.add_assignment (lhs, rhs);
 
 				foreach (LocalVariable local in temp_ref_vars) {
-					ccode.add_expression (destroy_variable (local));
+					ccode.add_expression (destroy_local (local));
 				}
 
 				temp_ref_vars.clear ();
@@ -1896,7 +1896,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 							current_method.coroutine = false;
 						}
 
-						ccode.add_expression (destroy_variable (local));
+						ccode.add_expression (destroy_local (local));
 
 						if (old_coroutine) {
 							current_method.coroutine = true;
@@ -1923,7 +1923,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 								m.coroutine = false;
 							}
 
-							ccode.add_expression (destroy_variable (param));
+							ccode.add_expression (destroy_parameter (param));
 
 							if (old_coroutine) {
 								m.coroutine = true;
@@ -1941,7 +1941,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 					bool is_unowned_delegate = acc.value_parameter.variable_type is DelegateType && !acc.value_parameter.variable_type.value_owned;
 
 					if (requires_destroy (param_type) && !is_unowned_delegate) {
-						ccode.add_expression (destroy_variable (acc.value_parameter));
+						ccode.add_expression (destroy_parameter (acc.value_parameter));
 					}
 				}
 			}
@@ -1967,7 +1967,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 			var local = local_vars[i];
 			local.active = false;
 			if (!local.unreachable && !local.floating && !local.captured && requires_destroy (local.variable_type)) {
-				ccode.add_expression (destroy_variable (local));
+				ccode.add_expression (destroy_local (local));
 			}
 		}
 
@@ -1975,7 +1975,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 			var m = (Method) b.parent_symbol;
 			foreach (Parameter param in m.get_parameters ()) {
 				if (!param.captured && !param.ellipsis && requires_destroy (param.variable_type) && param.direction == ParameterDirection.IN) {
-					ccode.add_expression (destroy_variable (param));
+					ccode.add_expression (destroy_parameter (param));
 				} else if (param.direction == ParameterDirection.OUT && !m.coroutine) {
 					return_out_parameter (param);
 				}
@@ -2824,9 +2824,8 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 
 	// logic in this method is temporarily duplicated in destroy_value
 	// apply changes to both methods
-	public virtual CCodeExpression destroy_variable (Variable variable, CCodeExpression? inner = null) {
+	public virtual CCodeExpression destroy_variable (Variable variable, TargetValue target_lvalue) {
 		var type = variable.variable_type;
-		var target_lvalue = get_variable_cvalue (variable, inner);
 		var cvar = get_cvalue_ (target_lvalue);
 
 		if (type is DelegateType) {
@@ -2931,19 +2930,21 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 				var array_type = (ArrayType) type;
 				if (requires_destroy (array_type.element_type) && !variable.no_array_length) {
 					CCodeExpression csizeexpr = null;
-					TargetValue access_value = null;
-					if (variable is LocalVariable) {
-						access_value = load_local ((LocalVariable) variable);
-					} else if (variable is Parameter) {
-						access_value = load_parameter ((Parameter) variable);
-					}
-					bool first = true;
-					for (int dim = 1; dim <= array_type.rank; dim++) {
-						if (first) {
-							csizeexpr = get_array_length_cvalue (access_value, dim);
-							first = false;
-						} else {
-							csizeexpr = new CCodeBinaryExpression (CCodeBinaryOperator.MUL, csizeexpr, get_array_length_cvalue (access_value, dim));
+					if (variable.array_null_terminated) {
+						var len_call = new CCodeFunctionCall (new CCodeIdentifier ("_vala_array_length"));
+						len_call.add_argument (cvar);
+						csizeexpr = len_call;
+					} else if (variable.has_array_length_cexpr) {
+						csizeexpr = new CCodeConstant (variable.get_array_length_cexpr ());
+					} else {
+						bool first = true;
+						for (int dim = 1; dim <= array_type.rank; dim++) {
+							if (first) {
+								csizeexpr = get_array_length_cvalue (target_lvalue, dim);
+								first = false;
+							} else {
+								csizeexpr = new CCodeBinaryExpression (CCodeBinaryOperator.MUL, csizeexpr, get_array_length_cvalue (target_lvalue, dim));
+							}
 						}
 					}
 
@@ -2976,9 +2977,25 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 		return new CCodeConditionalExpression (cisnull, new CCodeConstant ("NULL"), cassign);
 	}
 
+	public CCodeExpression destroy_local (LocalVariable local) {
+		return destroy_variable (local, get_local_cvalue (local));
+	}
+
+	public CCodeExpression destroy_parameter (Parameter param) {
+		return destroy_variable (param, get_parameter_cvalue (param));
+	}
+
+	public CCodeExpression destroy_field (Field field, Expression? instance) {
+		return destroy_variable (field, get_field_cvalue (field, instance));
+	}
+
 	public CCodeExpression get_unref_expression (CCodeExpression cvar, DataType type, Expression? expr, bool is_macro_definition = false) {
-		if (expr != null && (expr.symbol_reference is LocalVariable || expr.symbol_reference is Parameter)) {
-			return destroy_variable ((Variable) expr.symbol_reference);
+		if (expr != null) {
+			if (expr.symbol_reference is LocalVariable) {
+				return destroy_local ((LocalVariable) expr.symbol_reference);
+			} else if (expr.symbol_reference is Parameter) {
+				return destroy_parameter ((Parameter) expr.symbol_reference);
+			}
 		}
 		var value = new GLibValue (type, cvar);
 		if (expr != null && expr.target_value != null) {
@@ -3165,7 +3182,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 		}
 		
 		foreach (LocalVariable local in temp_ref_vars) {
-			ccode.add_expression (destroy_variable (local));
+			ccode.add_expression (destroy_local (local));
 		}
 
 		if (full_expr_var != null) {
@@ -3239,7 +3256,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 		/* free temporary objects and handle errors */
 
 		foreach (LocalVariable local in temp_ref_vars) {
-			ccode.add_expression (destroy_variable (local));
+			ccode.add_expression (destroy_local (local));
 		}
 
 		if (stmt.tree_can_fail && stmt.expression.tree_can_fail) {
@@ -3258,7 +3275,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 		for (int i = local_vars.size - 1; i >= 0; i--) {
 			var local = local_vars[i];
 			if (!local.unreachable && local.active && !local.floating && !local.captured && requires_destroy (local.variable_type)) {
-				ccode.add_expression (destroy_variable (local));
+				ccode.add_expression (destroy_local (local));
 			}
 		}
 
@@ -3293,7 +3310,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 	private void append_param_free (Method m) {
 		foreach (Parameter param in m.get_parameters ()) {
 			if (!param.ellipsis && requires_destroy (param.variable_type) && param.direction == ParameterDirection.IN) {
-				ccode.add_expression (destroy_variable (param));
+				ccode.add_expression (destroy_parameter (param));
 			}
 		}
 	}
@@ -3333,7 +3350,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 
 		if (param.variable_type.is_disposable ()){
 			ccode.add_else ();
-			ccode.add_expression (destroy_variable (param));
+			ccode.add_expression (destroy_parameter (param));
 		}
 		ccode.close ();
 
@@ -3702,9 +3719,11 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 		}
 	}
 
-	public virtual TargetValue get_variable_cvalue (Variable variable, CCodeExpression? inner = null) {
-		assert_not_reached ();
-	}
+	public abstract TargetValue get_local_cvalue (LocalVariable local);
+
+	public abstract TargetValue get_parameter_cvalue (Parameter param);
+
+	public abstract TargetValue get_field_cvalue (Field field, Expression? instance);
 
 	public virtual string get_delegate_target_cname (string delegate_cname) {
 		assert_not_reached ();
diff --git a/codegen/valaccodecontrolflowmodule.vala b/codegen/valaccodecontrolflowmodule.vala
index 8602372..70f7a5a 100644
--- a/codegen/valaccodecontrolflowmodule.vala
+++ b/codegen/valaccodecontrolflowmodule.vala
@@ -380,7 +380,7 @@ public abstract class Vala.CCodeControlFlowModule : CCodeMethodModule {
 
 		foreach (LocalVariable local in stmt.get_local_variables ()) {
 			if (requires_destroy (local.variable_type)) {
-				ccode.add_expression (destroy_variable (local));
+				ccode.add_expression (destroy_local (local));
 			}
 		}
 
diff --git a/codegen/valaccodememberaccessmodule.vala b/codegen/valaccodememberaccessmodule.vala
index 6b2918a..97fedd8 100644
--- a/codegen/valaccodememberaccessmodule.vala
+++ b/codegen/valaccodememberaccessmodule.vala
@@ -341,7 +341,7 @@ public abstract class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
 	}
 
 	/* Returns lvalue access to the given local variable */
-	public TargetValue get_local_cvalue (LocalVariable local) {
+	public override TargetValue get_local_cvalue (LocalVariable local) {
 		var result = new GLibValue (local.variable_type.copy ());
 
 		var array_type = local.variable_type as ArrayType;
@@ -395,7 +395,7 @@ public abstract class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
 	}
 
 	/* Returns access values to the given parameter */
-	public TargetValue get_parameter_cvalue (Parameter param) {
+	public override TargetValue get_parameter_cvalue (Parameter param) {
 		var result = new GLibValue (param.variable_type.copy ());
 		if (param.captured || is_in_coroutine ()) {
 			result.value_type.value_owned = true;
@@ -499,7 +499,7 @@ public abstract class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
 	}
 
 	/* Returns lvalue access to the given field */
-	public TargetValue get_field_cvalue (Field field, Expression? instance) {
+	public override TargetValue get_field_cvalue (Field field, Expression? instance) {
 		var result = new GLibValue (field.variable_type.copy ());
 
 		var array_type = field.variable_type as ArrayType;
@@ -683,17 +683,6 @@ public abstract class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
 		return result;
 	}
 
-	/* Returns lvalue access to the given symbol */
-	public override TargetValue get_variable_cvalue (Variable variable, CCodeExpression? inner = null) {
-		if (variable is LocalVariable) {
-			return get_local_cvalue ((LocalVariable) variable);
-		} else if (variable is Parameter) {
-			return get_parameter_cvalue ((Parameter) variable);
-		} else {
-			assert_not_reached ();
-		}
-	}
-
 	/* Returns unowned access to the given local variable */
 	public override TargetValue load_local (LocalVariable local) {
 		return load_variable (local, get_local_cvalue (local));
diff --git a/codegen/valadbusclientmodule.vala b/codegen/valadbusclientmodule.vala
index 0744152..8102171 100644
--- a/codegen/valadbusclientmodule.vala
+++ b/codegen/valadbusclientmodule.vala
@@ -1577,7 +1577,7 @@ public class Vala.DBusClientModule : DBusModule {
 			if (requires_destroy (owned_type)) {
 				// keep local alive (symbol_reference is weak)
 				var local = new LocalVariable (owned_type, param.name);
-				var stmt = new CCodeExpressionStatement (destroy_variable (local));
+				var stmt = new CCodeExpressionStatement (destroy_local (local));
 				postfragment.append (stmt);
 			}
 		}
diff --git a/codegen/valadbusservermodule.vala b/codegen/valadbusservermodule.vala
index cbae8cf..544b0b5 100644
--- a/codegen/valadbusservermodule.vala
+++ b/codegen/valadbusservermodule.vala
@@ -252,7 +252,7 @@ public class Vala.DBusServerModule : DBusClientModule {
 			if (requires_destroy (owned_type)) {
 				// keep local alive (symbol_reference is weak)
 				var local = new LocalVariable (owned_type, param.name);
-				var stmt = new CCodeExpressionStatement (destroy_variable (local));
+				var stmt = new CCodeExpressionStatement (destroy_local (local));
 				if (param.direction == ParameterDirection.IN) {
 					in_postfragment.append (stmt);
 				} else {
diff --git a/codegen/valagasyncmodule.vala b/codegen/valagasyncmodule.vala
index 5aafe8d..fa0e4a7 100644
--- a/codegen/valagasyncmodule.vala
+++ b/codegen/valagasyncmodule.vala
@@ -111,7 +111,7 @@ public class Vala.GAsyncModule : GSignalModule {
 					bool old_captured = param.captured;
 					param.captured = false;
 
-					freeblock.add_statement (new CCodeExpressionStatement (destroy_variable (param)));
+					freeblock.add_statement (new CCodeExpressionStatement (destroy_parameter (param)));
 
 					param.captured = old_captured;
 				}
@@ -593,7 +593,7 @@ public class Vala.GAsyncModule : GSignalModule {
 		/* free temporary objects */
 
 		foreach (LocalVariable local in temp_ref_vars) {
-			ccode.add_expression (destroy_variable (local));
+			ccode.add_expression (destroy_local (local));
 		}
 
 		temp_ref_vars.clear ();
diff --git a/codegen/valagdbusclientmodule.vala b/codegen/valagdbusclientmodule.vala
index 054047a..153add8 100644
--- a/codegen/valagdbusclientmodule.vala
+++ b/codegen/valagdbusclientmodule.vala
@@ -395,7 +395,7 @@ public class Vala.GDBusClientModule : GDBusModule {
 			if (requires_destroy (owned_type)) {
 				// keep local alive (symbol_reference is weak)
 				var local = new LocalVariable (owned_type, param.name);
-				ccode.add_expression (destroy_variable (local));
+				ccode.add_expression (destroy_local (local));
 			}
 		}
 
diff --git a/codegen/valagdbusservermodule.vala b/codegen/valagdbusservermodule.vala
index 2f71857..8899e21 100644
--- a/codegen/valagdbusservermodule.vala
+++ b/codegen/valagdbusservermodule.vala
@@ -389,7 +389,7 @@ public class Vala.GDBusServerModule : GDBusClientModule {
 				if (requires_destroy (owned_type)) {
 					// keep local alive (symbol_reference is weak)
 					var local = new LocalVariable (owned_type, param.name);
-					ccode.add_expression (destroy_variable (local));
+					ccode.add_expression (destroy_local (local));
 				}
 			}
 		}



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