[vala] codegen: Add get_parameter_cvalue and load_parameter



commit 1291020fed75d925926b28206e935de8eb9cfc96
Author: Luca Bruno <lucabru src gnome org>
Date:   Sun Jan 16 10:15:59 2011 +0100

    codegen: Add get_parameter_cvalue and load_parameter

 codegen/valaccodebasemodule.vala         |   26 +---
 codegen/valaccodememberaccessmodule.vala |  237 +++++++++++++++++-------------
 codegen/valadbusclientmodule.vala        |    6 +-
 codegen/valadbusservermodule.vala        |    6 +-
 codegen/valagasyncmodule.vala            |    6 +-
 codegen/valagdbusclientmodule.vala       |    6 +-
 codegen/valagdbusservermodule.vala       |    6 +-
 7 files changed, 146 insertions(+), 147 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 23fa8e5..fc7df90 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -1706,11 +1706,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 				current_method.coroutine = false;
 			}
 
-			var ma = new MemberAccess.simple (param.name);
-			ma.symbol_reference = param;
-			ma.value_type = param_type.copy ();
-			visit_member_access (ma);
-			free_block.add_statement (new CCodeExpressionStatement (get_unref_expression (new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data%d_".printf (block_id)), get_variable_cname (param.name)), param.variable_type, ma)));
+			free_block.add_statement (new CCodeExpressionStatement (get_unref_expression_ (param)));
 
 			if (old_coroutine) {
 				current_method.coroutine = true;
@@ -1936,11 +1932,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) {
-					var ma = new MemberAccess.simple (param.name);
-					ma.symbol_reference = param;
-					ma.value_type = param.variable_type.copy ();
-					visit_member_access (ma);
-					ccode.add_expression (get_unref_expression (get_variable_cexpression (param.name), param.variable_type, ma));
+					ccode.add_expression (get_unref_expression_ (param));
 				} else if (param.direction == ParameterDirection.OUT && !m.coroutine) {
 					return_out_parameter (param);
 				}
@@ -2905,7 +2897,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 	}
 
 	public CCodeExpression get_unref_expression (CCodeExpression cvar, DataType type, Expression? expr, bool is_macro_definition = false) {
-		if (expr != null && expr.symbol_reference is LocalVariable) {
+		if (expr != null && (expr.symbol_reference is LocalVariable || expr.symbol_reference is Parameter)) {
 			return get_unref_expression_ ((Variable) expr.symbol_reference);
 		}
 		var value = new GLibValue (type, cvar);
@@ -3221,11 +3213,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) {
-				var ma = new MemberAccess.simple (param.name);
-				ma.symbol_reference = param;
-				ma.value_type = param.variable_type.copy ();
-				visit_member_access (ma);
-				ccode.add_expression (get_unref_expression (get_variable_cexpression (param.name), param.variable_type, ma));
+				ccode.add_expression (get_unref_expression_ (param));
 			}
 		}
 	}
@@ -3265,11 +3253,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 
 		if (param.variable_type.is_disposable ()){
 			ccode.add_else ();
-			var ma = new MemberAccess (null, param.name);
-			ma.symbol_reference = param;
-			ma.value_type = param.variable_type.copy ();
-			visit_member_access (ma);
-			ccode.add_expression (get_unref_expression (get_variable_cexpression ("_" + param.name), param.variable_type, ma));
+			ccode.add_expression (get_unref_expression_ (param));
 		}
 		ccode.close ();
 
diff --git a/codegen/valaccodememberaccessmodule.vala b/codegen/valaccodememberaccessmodule.vala
index 82108e9..1f7a1ff 100644
--- a/codegen/valaccodememberaccessmodule.vala
+++ b/codegen/valaccodememberaccessmodule.vala
@@ -512,108 +512,11 @@ public abstract class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
 				local.active = false;
 			}
 		} else if (expr.symbol_reference is Parameter) {
-			var p = (Parameter) expr.symbol_reference;
-			if (p.name == "this") {
-				if (current_method != null && current_method.coroutine) {
-					// use closure
-					set_cvalue (expr, new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), "self"));
-				} else {
-					var st = current_type_symbol as Struct;
-					if (st != null && !st.is_simple_type ()) {
-						set_cvalue (expr, new CCodeIdentifier ("(*self)"));
-					} else {
-						set_cvalue (expr, new CCodeIdentifier ("self"));
-					}
-				}
+			var param = (Parameter) expr.symbol_reference;
+			if (expr.lvalue) {
+				expr.target_value = get_parameter_cvalue (param);
 			} else {
-				string name = p.name;
-
-				if (p.captured) {
-					// captured variables are stored on the heap
-					var block = p.parent_symbol as Block;
-					if (block == null) {
-						block = ((Method) p.parent_symbol).body;
-					}
-					set_cvalue (expr, new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (get_block_id (block))), get_variable_cname (p.name)));
-					if (array_type != null) {
-						for (int dim = 1; dim <= array_type.rank; dim++) {
-							append_array_size (expr, new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (get_block_id (block))), get_parameter_array_length_cname (p, dim)));
-						}
-					} else if (delegate_type != null && delegate_type.delegate_symbol.has_target) {
-						set_delegate_target (expr, new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (get_block_id (block))), get_delegate_target_cname (get_variable_cname (p.name))));
-						set_delegate_target_destroy_notify (expr, new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (get_block_id (block))), get_delegate_target_destroy_notify_cname (get_variable_cname (p.name))));
-					}
-				} else if (current_method != null && current_method.coroutine) {
-					// use closure
-					set_cvalue (expr, get_variable_cexpression (p.name));
-					if (delegate_type != null && delegate_type.delegate_symbol.has_target) {
-						set_delegate_target (expr, new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), get_delegate_target_cname (get_variable_cname (p.name))));
-						set_delegate_target_destroy_notify (expr, new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), get_delegate_target_destroy_notify_cname (get_variable_cname (p.name))));
-					}
-				} else {
-					var type_as_struct = p.variable_type.data_type as Struct;
-
-					if (p.direction == ParameterDirection.OUT) {
-						name = "_" + name;
-					}
-
-					if (p.direction == ParameterDirection.REF
-					    || (p.direction == ParameterDirection.IN && type_as_struct != null && !type_as_struct.is_simple_type () && !p.variable_type.nullable)) {
-						set_cvalue (expr, new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, new CCodeIdentifier (get_variable_cname (name))));
-					} else {
-						// Property setters of non simple structs shall replace all occurences
-						// of the "value" formal parameter with a dereferencing version of that
-						// parameter.
-						if (current_property_accessor != null &&
-						    current_property_accessor.writable &&
-						    current_property_accessor.value_parameter == p &&
-						    current_property_accessor.prop.property_type.is_real_struct_type () &&
-						    !current_property_accessor.prop.property_type.nullable) {
-							set_cvalue (expr, new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, new CCodeIdentifier ("value")));
-						} else {
-							set_cvalue (expr, get_variable_cexpression (name));
-						}
-					}
-					if (delegate_type != null && delegate_type.delegate_symbol.has_target) {
-						CCodeExpression target_expr = new CCodeIdentifier (get_delegate_target_cname (get_variable_cname (name)));
-						CCodeExpression delegate_target_destroy_notify = new CCodeIdentifier (get_delegate_target_destroy_notify_cname (get_variable_cname (name)));
-						if (p.direction == ParameterDirection.REF) {
-							// accessing argument of ref param
-							target_expr = new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, target_expr);
-							delegate_target_destroy_notify = new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, delegate_target_destroy_notify);
-						}
-						set_delegate_target (expr, target_expr);
-						if (expr.value_type.value_owned) {
-							set_delegate_target_destroy_notify (expr, delegate_target_destroy_notify);
-						} else {
-							set_delegate_target_destroy_notify (expr, new CCodeConstant ("NULL"));
-						}
-					}
-				}
-				if (!p.captured && array_type != null) {
-					if (p.array_null_terminated) {
-						var carray_expr = get_variable_cexpression (name);
-						requires_array_length = true;
-						var len_call = new CCodeFunctionCall (new CCodeIdentifier ("_vala_array_length"));
-						len_call.add_argument (carray_expr);
-						append_array_size (expr, len_call);
-					} else if (!p.no_array_length) {
-						for (int dim = 1; dim <= array_type.rank; dim++) {
-							CCodeExpression length_expr = get_variable_cexpression (get_parameter_array_length_cname (p, dim));
-							if (p.direction == ParameterDirection.OUT) {
-								length_expr = get_variable_cexpression (get_array_length_cname (get_variable_cname (name), dim));
-							} else if (p.direction == ParameterDirection.REF) {
-								// accessing argument of ref param
-								length_expr = new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, length_expr);
-							}
-							append_array_size (expr, length_expr);
-						}
-					} else {
-						for (int dim = 1; dim <= array_type.rank; dim++) {
-							append_array_size (expr, new CCodeConstant ("-1"));
-						}
-					}
-				}
+				expr.target_value = load_parameter (param);
 			}
 		}
 	}
@@ -672,6 +575,110 @@ public abstract class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
 		return result;
 	}
 
+	/* Returns access values to the given parameter */
+	public TargetValue get_parameter_cvalue (Parameter param) {
+		var result = new GLibValue (param.variable_type.copy ());
+		if (param.captured || current_method != null && current_method.coroutine) {
+			result.value_type.value_owned = true;
+		}
+
+		var array_type = result.value_type as ArrayType;
+		var delegate_type = result.value_type as DelegateType;
+
+		if (param.name == "this") {
+			if (current_method != null && current_method.coroutine) {
+				// use closure
+				result.cvalue = new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), "self");
+			} else {
+				var st = current_type_symbol as Struct;
+				if (st != null && !st.is_simple_type ()) {
+					result.cvalue = new CCodeIdentifier ("(*self)");
+				} else {
+					result.cvalue = new CCodeIdentifier ("self");
+				}
+			}
+		} else {
+			string name = param.name;
+
+			if (param.captured) {
+				// captured variables are stored on the heap
+				var block = param.parent_symbol as Block;
+				if (block == null) {
+					block = ((Method) param.parent_symbol).body;
+				}
+				result.cvalue = new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (get_block_id (block))), get_variable_cname (param.name));
+				if (array_type != null) {
+					for (int dim = 1; dim <= array_type.rank; dim++) {
+						result.append_array_length_cvalue (new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (get_block_id (block))), get_parameter_array_length_cname (param, dim)));
+					}
+				} else if (delegate_type != null && delegate_type.delegate_symbol.has_target) {
+					result.delegate_target_cvalue = new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (get_block_id (block))), get_delegate_target_cname (get_variable_cname (param.name)));
+					result.delegate_target_destroy_notify_cvalue = new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (get_block_id (block))), get_delegate_target_destroy_notify_cname (get_variable_cname (param.name)));
+				}
+			} else if (current_method != null && current_method.coroutine) {
+				// use closure
+				result.cvalue = get_variable_cexpression (param.name);
+				if (delegate_type != null && delegate_type.delegate_symbol.has_target) {
+					result.delegate_target_cvalue = new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), get_delegate_target_cname (get_variable_cname (param.name)));
+					result.delegate_target_destroy_notify_cvalue = new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), get_delegate_target_destroy_notify_cname (get_variable_cname (param.name)));
+				}
+			} else {
+				var type_as_struct = result.value_type.data_type as Struct;
+
+				if (param.direction == ParameterDirection.OUT) {
+					name = "_" + name;
+				}
+
+				if (param.direction == ParameterDirection.REF ||
+					(param.direction == ParameterDirection.IN && type_as_struct != null && !type_as_struct.is_simple_type () && !result.value_type.nullable)) {
+					result.cvalue = new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, new CCodeIdentifier (get_variable_cname (name)));
+				} else {
+					// Property setters of non simple structs shall replace all occurences
+					// of the "value" formal parameter with a dereferencing version of that
+					// parameter.
+					if (current_property_accessor != null &&
+						current_property_accessor.writable &&
+						current_property_accessor.value_parameter == param &&
+						current_property_accessor.prop.property_type.is_real_struct_type () &&
+						!current_property_accessor.prop.property_type.nullable) {
+						result.cvalue = new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, new CCodeIdentifier ("value"));
+					} else {
+						result.cvalue = get_variable_cexpression (name);
+					}
+				}
+				if (delegate_type != null && delegate_type.delegate_symbol.has_target) {
+					CCodeExpression target_expr = new CCodeIdentifier (get_delegate_target_cname (get_variable_cname (name)));
+					CCodeExpression delegate_target_destroy_notify = new CCodeIdentifier (get_delegate_target_destroy_notify_cname (get_variable_cname (name)));
+					if (param.direction == ParameterDirection.REF) {
+						// accessing argument of ref param
+						target_expr = new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, target_expr);
+						delegate_target_destroy_notify = new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, delegate_target_destroy_notify);
+					}
+					result.delegate_target_cvalue = target_expr;
+					if (result.value_type.value_owned) {
+						result.delegate_target_destroy_notify_cvalue = delegate_target_destroy_notify;
+					}
+				}
+			}
+			if (!param.captured && array_type != null) {
+				if (!param.no_array_length && !param.array_null_terminated) {
+					for (int dim = 1; dim <= array_type.rank; dim++) {
+						CCodeExpression length_expr = get_variable_cexpression (get_parameter_array_length_cname (param, dim));
+						if (param.direction == ParameterDirection.OUT) {
+							length_expr = get_variable_cexpression (get_array_length_cname (get_variable_cname (name), dim));
+						} else if (param.direction == ParameterDirection.REF) {
+							// accessing argument of ref param
+							length_expr = new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, length_expr);
+						}
+						result.append_array_length_cvalue (length_expr);
+					}
+				}
+			}
+		}
+
+		return result;
+	}
+
 	TargetValue load_variable (Variable variable, TargetValue value) {
 		return value;
 	}
@@ -680,6 +687,8 @@ public abstract class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
 	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 ();
 		}
@@ -694,4 +703,30 @@ public abstract class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
 		result.value_type.value_owned = false;
 		return load_variable (local, result);
 	}
+
+	public TargetValue load_parameter (Parameter param) {
+		var result = (GLibValue) get_parameter_cvalue (param);
+		if (result.value_type is DelegateType) {
+			result.delegate_target_destroy_notify_cvalue = new CCodeConstant ("NULL");
+		}
+		if (result.value_type is ArrayType) {
+			if (param.array_null_terminated) {
+				string name = param.name;
+				if (param.direction == ParameterDirection.OUT) {
+					name = "_" + name;
+				}
+				var carray_expr = get_variable_cexpression (name);
+				requires_array_length = true;
+				var len_call = new CCodeFunctionCall (new CCodeIdentifier ("_vala_array_length"));
+				len_call.add_argument (carray_expr);
+				result.append_array_length_cvalue (len_call);
+			} else if (param.no_array_length) {
+				for (int dim = 1; dim <= ((ArrayType) result.value_type).rank; dim++) {
+					result.append_array_length_cvalue (new CCodeConstant ("-1"));
+				}
+			}
+		}
+		result.value_type.value_owned = false;
+		return load_variable (param, result);
+	}
 }
diff --git a/codegen/valadbusclientmodule.vala b/codegen/valadbusclientmodule.vala
index 04004df..7428ac2 100644
--- a/codegen/valadbusclientmodule.vala
+++ b/codegen/valadbusclientmodule.vala
@@ -1577,11 +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 ma = new MemberAccess.simple (param.name);
-				ma.symbol_reference = local;
-				ma.value_type = owned_type.copy ();
-				visit_member_access (ma);
-				var stmt = new CCodeExpressionStatement (get_unref_expression (new CCodeIdentifier (param.name), owned_type, ma));
+				var stmt = new CCodeExpressionStatement (get_unref_expression_ (local));
 				postfragment.append (stmt);
 			}
 		}
diff --git a/codegen/valadbusservermodule.vala b/codegen/valadbusservermodule.vala
index c938489..3cc1c89 100644
--- a/codegen/valadbusservermodule.vala
+++ b/codegen/valadbusservermodule.vala
@@ -252,11 +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 ma = new MemberAccess.simple (param.name);
-				ma.symbol_reference = local;
-				ma.value_type = owned_type.copy ();
-				visit_member_access (ma);
-				var stmt = new CCodeExpressionStatement (get_unref_expression (new CCodeIdentifier (param.name), owned_type, ma));
+				var stmt = new CCodeExpressionStatement (get_unref_expression_ (local));
 				if (param.direction == ParameterDirection.IN) {
 					in_postfragment.append (stmt);
 				} else {
diff --git a/codegen/valagasyncmodule.vala b/codegen/valagasyncmodule.vala
index 12d6b1d..0e3ae10 100644
--- a/codegen/valagasyncmodule.vala
+++ b/codegen/valagasyncmodule.vala
@@ -107,11 +107,7 @@ public class Vala.GAsyncModule : GSignalModule {
 				param_type.value_owned = true;
 
 				if (requires_destroy (param_type) && !is_unowned_delegate) {
-					var ma = new MemberAccess.simple (param.name);
-					ma.symbol_reference = param;
-					ma.value_type = param.variable_type.copy ();
-					visit_member_access (ma);
-					freeblock.add_statement (new CCodeExpressionStatement (get_unref_expression (new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), get_variable_cname (param.name)), param.variable_type, ma)));
+					freeblock.add_statement (new CCodeExpressionStatement (get_unref_expression_ (param)));
 				}
 			}
 		}
diff --git a/codegen/valagdbusclientmodule.vala b/codegen/valagdbusclientmodule.vala
index acba1fc..3140339 100644
--- a/codegen/valagdbusclientmodule.vala
+++ b/codegen/valagdbusclientmodule.vala
@@ -395,11 +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);
-				var ma = new MemberAccess.simple (param.name);
-				ma.symbol_reference = local;
-				ma.value_type = local.variable_type.copy ();
-				visit_member_access (ma);
-				ccode.add_expression (get_unref_expression (new CCodeIdentifier (param.name), owned_type, ma));
+				ccode.add_expression (get_unref_expression_ (local));
 			}
 		}
 
diff --git a/codegen/valagdbusservermodule.vala b/codegen/valagdbusservermodule.vala
index c121728..a32100f 100644
--- a/codegen/valagdbusservermodule.vala
+++ b/codegen/valagdbusservermodule.vala
@@ -389,11 +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);
-					var ma = new MemberAccess.simple (param.name);
-					ma.symbol_reference = local;
-					ma.value_type = local.variable_type.copy ();
-					visit_member_access (ma);
-					ccode.add_expression (get_unref_expression (new CCodeIdentifier (param.name), owned_type, ma));
+					ccode.add_expression (get_unref_expression_ (local));
 				}
 			}
 		}



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