[vala] Add array_length_cname attribute for parameters
- From: Jürg Billeter <juergbi src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala] Add array_length_cname attribute for parameters
- Date: Wed, 15 Sep 2010 21:05:16 +0000 (UTC)
commit 59fab5ad281924c102d083b64c31c3d84677ec3e
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 | 6 +++++-
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, 65 insertions(+), 18 deletions(-)
---
diff --git a/codegen/valaccodearraymodule.vala b/codegen/valaccodearraymodule.vala
index b75502a..255d2eb 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);
@@ -1005,7 +1013,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 454e94c..203262f 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -1633,7 +1633,7 @@ 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));
+ data.add_field ("gint", get_parameter_array_length_cname (param, dim));
ccode.add_expression (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))));
}
} else if (param.variable_type is DelegateType) {
@@ -5626,6 +5626,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 a176589..2102371 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 1d5cf8b..89bcc76 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 8f747c9..1a0ffd7 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 02846d2..d347f5b 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) {
@@ -255,7 +255,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++) {
- ccode.add_expression (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))));
+ ccode.add_expression (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 168acbd..4ce19b4 100644
--- a/codegen/valagdbusclientmodule.vala
+++ b/codegen/valagdbusclientmodule.vala
@@ -343,7 +343,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 1bbe7b9..acc9b88 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 231262f..9a07c83 100644
--- a/vala/valacodewriter.vala
+++ b/vala/valacodewriter.vala
@@ -865,6 +865,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]