[vala/staging: 4/4] valaparser: Generate correct ccode for fixed-length array parameters



commit 21e21fbb75b1a807904448595096c3654273a8b5
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 |   12 ++++++++++--
 tests/Makefile.am                 |    1 +
 tests/basic-types/arrays.vala     |   18 ++++++++++++++++++
 tests/basic-types/bug641308.test  |    7 +++++++
 vala/valaparser.vala              |    7 ++++++-
 5 files changed, 42 insertions(+), 3 deletions(-)
---
diff --git a/codegen/valaccodearraymodule.vala b/codegen/valaccodearraymodule.vala
index 8922464..f8974c4 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);
 
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..ce12879 100644
--- a/tests/basic-types/arrays.vala
+++ b/tests/basic-types/arrays.vala
@@ -191,6 +191,24 @@ 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;
+       give_fixed_array (out j);
+       assert (j.length == 3);
+       assert (j[1] == 4);
+}
+
 void main () {
        test_integer_array ();
        test_string_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]