[vala/wip/fixed-arrays] valaparser: Generate correct ccode for fixed-length array parameters
- From: Rico Tzschichholz <ricotz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/wip/fixed-arrays] valaparser: Generate correct ccode for fixed-length array parameters
- Date: Tue, 11 Oct 2016 14:57:41 +0000 (UTC)
commit acac0e9c16faedd681673f3bb0bef8885f7c3b80
Author: Rico Tzschichholz <ricotz ubuntu com>
Date: Tue Oct 11 11:05:23 2016 +0200
valaparser: Generate correct ccode for fixed-length array parameters
Additionally don't allow to declare array parameter using "type array[]".
https://bugzilla.gnome.org/show_bug.cgi?id=641308
codegen/valaccodearraymodule.vala | 14 +++++++++++---
codegen/valaccodemethodcallmodule.vala | 4 ++--
codegen/valaccodemethodmodule.vala | 2 +-
tests/Makefile.am | 1 +
tests/basic-types/arrays.vala | 18 ++++++++++++++++++
tests/basic-types/bug641308.test | 7 +++++++
vala/valaparser.vala | 7 ++++++-
7 files changed, 46 insertions(+), 7 deletions(-)
---
diff --git a/codegen/valaccodearraymodule.vala b/codegen/valaccodearraymodule.vala
index 8922464..bcf9ea4 100644
--- a/codegen/valaccodearraymodule.vala
+++ b/codegen/valaccodearraymodule.vala
@@ -704,14 +704,22 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
}
string ctypename = get_ccode_name (param.variable_type);
+ string name = get_variable_cname (param.name);
+ var array_type = (ArrayType) param.variable_type;
if (param.direction != ParameterDirection.IN) {
ctypename += "*";
}
- var main_cparam = new CCodeParameter (get_variable_cname (param.name), ctypename);
+ if (array_type.inline_allocated) {
+ if (param.direction != ParameterDirection.IN) {
+ ctypename += "*";
+ } else {
+ name += "[]";
+ }
+ }
- var array_type = (ArrayType) param.variable_type;
+ var main_cparam = new CCodeParameter (name, ctypename);
generate_type_declaration (array_type.element_type, decl_space);
@@ -720,7 +728,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
carg_map.set (get_param_pos (get_ccode_pos (param)), get_variable_cexpression
(param.name));
}
- if (get_ccode_array_length (param)) {
+ if (!array_type.fixed_length && get_ccode_array_length (param)) {
string length_ctype = "int";
if (get_ccode_array_length_type (param) != null) {
length_ctype = get_ccode_array_length_type (param);
diff --git a/codegen/valaccodemethodcallmodule.vala b/codegen/valaccodemethodcallmodule.vala
index 8e923ea..9085cef 100644
--- a/codegen/valaccodemethodcallmodule.vala
+++ b/codegen/valaccodemethodcallmodule.vala
@@ -343,7 +343,7 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
var unary = arg as UnaryExpression;
if (unary == null || unary.operator != UnaryOperator.OUT) {
- if (get_ccode_array_length (param) && param.variable_type is
ArrayType) {
+ if (get_ccode_array_length (param) && param.variable_type is
ArrayType && !((ArrayType) param.variable_type).fixed_length) {
var array_type = (ArrayType) param.variable_type;
for (int dim = 1; dim <= array_type.rank; dim++) {
CCodeExpression? array_length_expr = null;
@@ -406,7 +406,7 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
cexpr = new CCodeUnaryExpression
(CCodeUnaryOperator.ADDRESS_OF, get_cvalue (arg));
- if (get_ccode_array_length (param) && param.variable_type is
ArrayType) {
+ if (get_ccode_array_length (param) && param.variable_type is
ArrayType && !((ArrayType) param.variable_type).fixed_length) {
var array_type = (ArrayType) param.variable_type;
var array_length_type = int_type;
if (get_ccode_array_length_type (param) != null) {
diff --git a/codegen/valaccodemethodmodule.vala b/codegen/valaccodemethodmodule.vala
index 9c8e921..8e70f84 100644
--- a/codegen/valaccodemethodmodule.vala
+++ b/codegen/valaccodemethodmodule.vala
@@ -558,7 +558,7 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
}
} else if (!m.coroutine) {
// declare local variable for out parameter to allow
assignment even when caller passes NULL
- var vardecl = new CCodeVariableDeclarator.zero
(get_variable_cname ("_vala_" + param.name), default_value_for_type (param.variable_type, true));
+ var vardecl = new CCodeVariableDeclarator.zero
(get_variable_cname ("_vala_" + param.name), default_value_for_type (param.variable_type, true),
get_ccode_declarator_suffix (param.variable_type));
ccode.add_declaration (get_ccode_name (param.variable_type),
vardecl);
if (param.variable_type is ArrayType) {
diff --git a/tests/Makefile.am b/tests/Makefile.am
index a9d0c18..d7a97c1 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -28,6 +28,7 @@ TESTS = \
basic-types/bug596637.vala \
basic-types/bug596785.vala \
basic-types/bug632322.vala \
+ basic-types/bug641308.test \
basic-types/bug643612.vala \
basic-types/bug644046.vala \
basic-types/bug647222.vala \
diff --git a/tests/basic-types/arrays.vala b/tests/basic-types/arrays.vala
index fece749..e33b0ef 100644
--- a/tests/basic-types/arrays.vala
+++ b/tests/basic-types/arrays.vala
@@ -191,6 +191,23 @@ void test_void_array () {
assert ((void*) null in a);
}
+void give_fixed_array (out int i[3]) {
+ i = { 3, 4, 5 };
+}
+
+void take_fixed_array (int i[3]) {
+ assert (i.length == 3);
+ assert (i[1] == 2);
+}
+
+void test_fixed_array () {
+ int i[3] = { 1, 2, 3 };
+ take_fixed_array (i);
+ int j[3];
+ give_fixed_array (out j);
+ assert (j[1] == 4);
+}
+
void main () {
test_integer_array ();
test_string_array ();
@@ -202,4 +219,5 @@ void main () {
test_delegate_array ();
test_generics_array ();
test_void_array ();
+ test_fixed_array ();
}
diff --git a/tests/basic-types/bug641308.test b/tests/basic-types/bug641308.test
new file mode 100644
index 0000000..b530077
--- /dev/null
+++ b/tests/basic-types/bug641308.test
@@ -0,0 +1,7 @@
+Invalid Code
+
+void foo (int i[]) {
+}
+
+void main () {
+}
diff --git a/vala/valaparser.vala b/vala/valaparser.vala
index 3cbc561..43376df 100644
--- a/vala/valaparser.vala
+++ b/vala/valaparser.vala
@@ -3266,7 +3266,12 @@ public class Vala.Parser : CodeVisitor {
}
string id = parse_identifier ();
- type = parse_inline_array_type (type);
+ var array_type = parse_inline_array_type (type);
+ if (!(type is ArrayType) && (array_type is ArrayType) && !((ArrayType)
array_type).fixed_length) {
+ throw new ParseError.SYNTAX (get_error ("invalid array parameter declaration"));
+ } else {
+ type = array_type;
+ }
var param = new Parameter (id, type, get_src (begin));
set_attributes (param, attrs);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]