[vala/0.10] Add array_length_cname attribute for parameters



commit ed0938dcb9e6408877e11bc9a3d3beb91f235b33
Author: Jürg Billeter <j bitron ch>
Date:   Wed Sep 15 20:00:37 2010 +0200

    Add array_length_cname attribute for parameters

 codegen/valaccodearraymodule.vala    |   14 +++++++++++---
 codegen/valaccodebasemodule.vala     |    8 ++++++--
 codegen/valaccodedelegatemodule.vala |    4 ++--
 codegen/valadbusclientmodule.vala    |   10 +++++-----
 codegen/valadbusservermodule.vala    |    4 ++--
 codegen/valagasyncmodule.vala        |    4 ++--
 codegen/valagdbusclientmodule.vala   |    2 +-
 codegen/valagdbusservermodule.vala   |    4 ++--
 vala/valacodewriter.vala             |    4 ++++
 vala/valaformalparameter.vala        |   31 +++++++++++++++++++++++++++++++
 10 files changed, 66 insertions(+), 19 deletions(-)
---
diff --git a/codegen/valaccodearraymodule.vala b/codegen/valaccodearraymodule.vala
index 707dc83..316c894 100644
--- a/codegen/valaccodearraymodule.vala
+++ b/codegen/valaccodearraymodule.vala
@@ -130,6 +130,14 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
 		return "%s_length%d".printf (array_cname, dim);
 	}
 
+	public override string get_parameter_array_length_cname (FormalParameter param, int dim) {
+		if (param.has_array_length_cname) {
+			return param.get_array_length_cname ();
+		} else {
+			return get_array_length_cname (get_variable_cname (param.name), dim);
+		}
+	}
+
 	public override CCodeExpression get_array_length_cexpression (Expression array_expr, int dim = -1) {
 		var array_type = array_expr.value_type as ArrayType;
 
@@ -178,7 +186,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
 				if (param.captured) {
 					// captured variables are stored on the heap
 					var block = ((Method) param.parent_symbol).body;
-					var length_expr = new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (get_block_id (block))), get_array_length_cname (get_variable_cname (param.name), dim));
+					var length_expr = new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (get_block_id (block))), get_parameter_array_length_cname (param, dim));
 					if (is_out) {
 						// passing array as out/ref
 						return new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, length_expr);
@@ -193,7 +201,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
 						len_call.add_argument (carray_expr);
 						return len_call;
 					} else if (!param.no_array_length) {
-						CCodeExpression length_expr = get_variable_cexpression (get_array_length_cname (get_variable_cname (param.name), dim));
+						CCodeExpression length_expr = get_variable_cexpression (get_parameter_array_length_cname (param, dim));
 						if (param.direction != ParameterDirection.IN) {
 							// accessing argument of out/ref param
 							length_expr = new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, length_expr);
@@ -1041,7 +1049,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
 			}
 			
 			for (int dim = 1; dim <= array_type.rank; dim++) {
-				var cparam = new CCodeFormalParameter (get_array_length_cname (get_variable_cname (param.name), dim), length_ctype);
+				var cparam = new CCodeFormalParameter (get_parameter_array_length_cname (param, dim), length_ctype);
 				cparam_map.set (get_param_pos (param.carray_length_parameter_position + 0.01 * dim), cparam);
 				if (carg_map != null) {
 					carg_map.set (get_param_pos (param.carray_length_parameter_position + 0.01 * dim), get_variable_cexpression (cparam.name));
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index ae19c49..91789d0 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -1731,8 +1731,8 @@ public class Vala.CCodeBaseModule : CodeGenerator {
 		if (param.variable_type is ArrayType) {
 			var array_type = (ArrayType) param.variable_type;
 			for (int dim = 1; dim <= array_type.rank; dim++) {
-				data.add_field ("gint", get_array_length_cname (get_variable_cname (param.name), dim));
-				cblock.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (block_id)), get_array_length_cname (get_variable_cname (param.name), dim)), new CCodeIdentifier (get_array_length_cname (get_variable_cname (param.name), dim)))));
+				data.add_field ("gint", get_parameter_array_length_cname (param, dim));
+				cblock.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (block_id)), get_parameter_array_length_cname (param, dim)), new CCodeIdentifier (get_parameter_array_length_cname (param, dim)))));
 			}
 		} else if (param.variable_type is DelegateType) {
 			data.add_field ("gpointer", get_delegate_target_cname (get_variable_cname (param.name)));
@@ -5865,6 +5865,10 @@ public class Vala.CCodeBaseModule : CodeGenerator {
 		return "";
 	}
 
+	public virtual string get_parameter_array_length_cname (FormalParameter param, int dim) {
+		return "";
+	}
+
 	public virtual CCodeExpression get_array_length_cexpression (Expression array_expr, int dim = -1) {
 		return new CCodeConstant ("");
 	}
diff --git a/codegen/valaccodedelegatemodule.vala b/codegen/valaccodedelegatemodule.vala
index e18b1fb..843e2ca 100644
--- a/codegen/valaccodedelegatemodule.vala
+++ b/codegen/valaccodedelegatemodule.vala
@@ -62,7 +62,7 @@ public class Vala.CCodeDelegateModule : CCodeArrayModule {
 				}
 				
 				for (int dim = 1; dim <= array_type.rank; dim++) {
-					var cparam = new CCodeFormalParameter (get_array_length_cname (get_variable_cname (param.name), dim), length_ctype);
+					var cparam = new CCodeFormalParameter (get_parameter_array_length_cname (param, dim), length_ctype);
 					cfundecl.add_parameter (cparam);
 				}
 			}
@@ -497,7 +497,7 @@ public class Vala.CCodeDelegateModule : CCodeArrayModule {
 					} else if (d_params.get (i).no_array_length) {
 						clength = new CCodeConstant ("-1");
 					} else {
-						clength = new CCodeIdentifier (get_array_length_cname (d_params.get (i).name, dim));
+						clength = new CCodeIdentifier (get_parameter_array_length_cname (d_params.get (i), dim));
 					}
 					carg_map.set (get_param_pos (param.carray_length_parameter_position + 0.01 * dim), clength);
 				}
diff --git a/codegen/valadbusclientmodule.vala b/codegen/valadbusclientmodule.vala
index 3d8703a..957e3ec 100644
--- a/codegen/valadbusclientmodule.vala
+++ b/codegen/valadbusclientmodule.vala
@@ -266,7 +266,7 @@ public class Vala.DBusClientModule : DBusModule {
 						cdecl = new CCodeDeclaration ("GPtrArray*");
 
 						array_construct = new CCodeFunctionCall (new CCodeIdentifier ("g_ptr_array_sized_new"));
-						array_construct.add_argument (new CCodeIdentifier (get_array_length_cname (param.name, 1)));
+						array_construct.add_argument (new CCodeIdentifier (get_parameter_array_length_cname (param, 1)));
 					} else {
 						cdecl = new CCodeDeclaration ("GArray*");
 
@@ -285,16 +285,16 @@ public class Vala.DBusClientModule : DBusModule {
 						var memcpy_call = new CCodeFunctionCall (new CCodeIdentifier ("memcpy"));
 						memcpy_call.add_argument (new CCodeMemberAccess.pointer (new CCodeIdentifier ("dbus_%s".printf (param.name)), "pdata"));
 						memcpy_call.add_argument (new CCodeIdentifier (param.name));
-						memcpy_call.add_argument (new CCodeBinaryExpression (CCodeBinaryOperator.MUL, new CCodeIdentifier (get_array_length_cname (param.name, 1)), sizeof_call));
+						memcpy_call.add_argument (new CCodeBinaryExpression (CCodeBinaryOperator.MUL, new CCodeIdentifier (get_parameter_array_length_cname (param, 1)), sizeof_call));
 						block.add_statement (new CCodeExpressionStatement (memcpy_call));
 
-						var len_assignment = new CCodeAssignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("dbus_%s".printf (param.name)), "len"), new CCodeIdentifier (get_array_length_cname (param.name, 1)));
+						var len_assignment = new CCodeAssignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("dbus_%s".printf (param.name)), "len"), new CCodeIdentifier (get_parameter_array_length_cname (param, 1)));
 						block.add_statement (new CCodeExpressionStatement (len_assignment));
 					} else {
 						var cappend_call = new CCodeFunctionCall (new CCodeIdentifier ("g_array_append_vals"));
 						cappend_call.add_argument (new CCodeIdentifier ("dbus_%s".printf (param.name)));
 						cappend_call.add_argument (new CCodeIdentifier (param.name));
-						cappend_call.add_argument (new CCodeIdentifier (get_array_length_cname (param.name, 1)));
+						cappend_call.add_argument (new CCodeIdentifier (get_parameter_array_length_cname (param, 1)));
 						block.add_statement (new CCodeExpressionStatement (cappend_call));
 					}
 
@@ -1559,7 +1559,7 @@ public class Vala.DBusClientModule : DBusModule {
 				var array_type = (ArrayType) param.variable_type;
 
 				for (int dim = 1; dim <= array_type.rank; dim++) {
-					string length_cname = get_array_length_cname (param.name, dim);
+					string length_cname = get_parameter_array_length_cname (param, dim);
 
 					cdecl = new CCodeDeclaration ("int");
 					cdecl.add_declarator (new CCodeVariableDeclarator (length_cname, new CCodeConstant ("0")));
diff --git a/codegen/valadbusservermodule.vala b/codegen/valadbusservermodule.vala
index 3986d97..938d292 100644
--- a/codegen/valadbusservermodule.vala
+++ b/codegen/valadbusservermodule.vala
@@ -220,7 +220,7 @@ public class Vala.DBusServerModule : DBusClientModule {
 				var array_type = (ArrayType) param.variable_type;
 
 				for (int dim = 1; dim <= array_type.rank; dim++) {
-					string length_cname = get_array_length_cname (param.name, dim);
+					string length_cname = get_parameter_array_length_cname (param, dim);
 
 					cdecl = new CCodeDeclaration ("int");
 					cdecl.add_declarator (new CCodeVariableDeclarator (length_cname, new CCodeConstant ("0")));
@@ -495,7 +495,7 @@ public class Vala.DBusServerModule : DBusClientModule {
 			if (param.variable_type is ArrayType) {
 				var array_type = (ArrayType) param.variable_type;
 				for (int dim = 1; dim <= array_type.rank; dim++) {
-					function.add_parameter (new CCodeFormalParameter (get_array_length_cname (param.name, dim), "int"));
+					function.add_parameter (new CCodeFormalParameter (get_parameter_array_length_cname (param, dim), "int"));
 				}
 			}
 		}
diff --git a/codegen/valagasyncmodule.vala b/codegen/valagasyncmodule.vala
index c8fb945..8e8cd3a 100644
--- a/codegen/valagasyncmodule.vala
+++ b/codegen/valagasyncmodule.vala
@@ -45,7 +45,7 @@ public class Vala.GAsyncModule : GSignalModule {
 				var array_type = (ArrayType) param.variable_type;
 				if (!param.no_array_length) {
 					for (int dim = 1; dim <= array_type.rank; dim++) {
-						data.add_field ("gint", get_array_length_cname (get_variable_cname (param.name), dim));
+						data.add_field ("gint", get_parameter_array_length_cname (param, dim));
 					}
 				}
 			} else if (param.variable_type is DelegateType) {
@@ -243,7 +243,7 @@ public class Vala.GAsyncModule : GSignalModule {
 					var array_type = (ArrayType) param.variable_type;
 					if (!param.no_array_length) {
 						for (int dim = 1; dim <= array_type.rank; dim++) {
-							asyncblock.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (data_var, get_array_length_cname (get_variable_cname (param.name), dim)), new CCodeIdentifier (get_array_length_cname (get_variable_cname (param.name), dim)))));
+							asyncblock.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (data_var, get_parameter_array_length_cname (param, dim)), new CCodeIdentifier (get_parameter_array_length_cname (param, dim)))));
 						}
 					}
 				} else if (param.variable_type is DelegateType) {
diff --git a/codegen/valagdbusclientmodule.vala b/codegen/valagdbusclientmodule.vala
index 45c18b6..06ddcc4 100644
--- a/codegen/valagdbusclientmodule.vala
+++ b/codegen/valagdbusclientmodule.vala
@@ -331,7 +331,7 @@ public class Vala.GDBusClientModule : GDBusModule {
 				var array_type = (ArrayType) param.variable_type;
 
 				for (int dim = 1; dim <= array_type.rank; dim++) {
-					string length_cname = get_array_length_cname (param.name, dim);
+					string length_cname = get_parameter_array_length_cname (param, dim);
 
 					cdecl = new CCodeDeclaration ("int");
 					cdecl.add_declarator (new CCodeVariableDeclarator (length_cname, new CCodeConstant ("0")));
diff --git a/codegen/valagdbusservermodule.vala b/codegen/valagdbusservermodule.vala
index 8d22467..f345993 100644
--- a/codegen/valagdbusservermodule.vala
+++ b/codegen/valagdbusservermodule.vala
@@ -160,7 +160,7 @@ public class Vala.GDBusServerModule : GDBusClientModule {
 				var array_type = (ArrayType) param.variable_type;
 
 				for (int dim = 1; dim <= array_type.rank; dim++) {
-					string length_cname = get_array_length_cname (param.name, dim);
+					string length_cname = get_parameter_array_length_cname (param, dim);
 
 					cdecl = new CCodeDeclaration ("int");
 					cdecl.add_declarator (new CCodeVariableDeclarator (length_cname, new CCodeConstant ("0")));
@@ -369,7 +369,7 @@ public class Vala.GDBusServerModule : GDBusClientModule {
 			if (param.variable_type is ArrayType) {
 				var array_type = (ArrayType) param.variable_type;
 				for (int dim = 1; dim <= array_type.rank; dim++) {
-					function.add_parameter (new CCodeFormalParameter (get_array_length_cname (param.name, dim), "int"));
+					function.add_parameter (new CCodeFormalParameter (get_parameter_array_length_cname (param, dim), "int"));
 				}
 			}
 		}
diff --git a/vala/valacodewriter.vala b/vala/valacodewriter.vala
index f84409b..f403039 100644
--- a/vala/valacodewriter.vala
+++ b/vala/valacodewriter.vala
@@ -809,6 +809,10 @@ public class Vala.CodeWriter : CodeVisitor {
 				ccode_params.append_printf ("%sarray_length_type = \"%s\"", separator, param.array_length_type);
 				separator = ", ";
 			}
+			if (param.get_array_length_cname () != null && param.variable_type is ArrayType) {
+				ccode_params.append_printf ("%sarray_length_cname = \"%s\"", separator, param.get_array_length_cname ());
+				separator = ", ";
+			}
 			if (!float_equal (param.carray_length_parameter_position, i + 0.1)) {
 				ccode_params.append_printf ("%sarray_length_pos = %g", separator, param.carray_length_parameter_position);
 				separator = ", ";
diff --git a/vala/valaformalparameter.vala b/vala/valaformalparameter.vala
index 605af03..4aefdf1 100644
--- a/vala/valaformalparameter.vala
+++ b/vala/valaformalparameter.vala
@@ -54,6 +54,13 @@ public class Vala.FormalParameter : Variable {
 	public bool array_null_terminated { get; set; }
 
 	/**
+	 * Specifies whether the array length parameter uses a custom name in C.
+	 */
+	public bool has_array_length_cname {
+		get { return (array_length_cname != null); }
+	}
+
+	/**
 	 * Specifies a custom type for the array length.
 	 */
 	public string? array_length_type { get; set; default = null; }
@@ -84,6 +91,8 @@ public class Vala.FormalParameter : Variable {
 
 	public bool captured { get; set; }
 
+	private string? array_length_cname;
+
 	/**
 	 * Creates a new formal parameter.
 	 *
@@ -135,6 +144,25 @@ public class Vala.FormalParameter : Variable {
 		}
 	}
 
+	/**
+	 * Returns the name of the array length parameter as it is used in C code
+	 *
+	 * @return the name of the array length parameter to be used in C code
+	 */
+	public string? get_array_length_cname () {
+		return this.array_length_cname;
+	}
+
+	/**
+	 * Sets the name of the array length parameter as it is used in C code
+	 *
+	 * @param array_length_cname the name of the array length parameter to be
+	 * used in C code
+	 */
+	public void set_array_length_cname (string? array_length_cname) {
+		this.array_length_cname = array_length_cname;
+	}
+
 	private void process_ccode_attribute (Attribute a) {
 		if (a.has_argument ("type")) {
 			ctype = a.get_string ("type");
@@ -154,6 +182,9 @@ public class Vala.FormalParameter : Variable {
 		if (a.has_argument ("array_length_pos")) {
 			carray_length_parameter_position = a.get_double ("array_length_pos");
 		}
+		if (a.has_argument ("array_length_cname")) {
+			set_array_length_cname (a.get_string ("array_length_cname"));
+		}
 		if (a.has_argument ("delegate_target_pos")) {
 			cdelegate_target_parameter_position = a.get_double ("delegate_target_pos");
 		}



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