[vala/0.36] codegen: Respect array_length_cname attribute for global fields



commit fd7846e623b2dbf6b63f4ffeafbc4813dae68105
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Sun Feb 24 21:44:48 2019 +0100

    codegen: Respect array_length_cname attribute for global fields

 codegen/valaccodebasemodule.vala            | 12 ++++++++++--
 tests/Makefile.am                           |  3 +++
 tests/arrays/class-field-length-cname.vala  | 13 +++++++++++++
 tests/arrays/field-global-length-cname.vala | 10 ++++++++++
 tests/arrays/struct-field-length-cname.vala | 12 ++++++++++++
 5 files changed, 48 insertions(+), 2 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 504b01ee0..617849f59 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -1050,9 +1050,13 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                        if (!array_type.fixed_length) {
                                for (int dim = 1; dim <= array_type.rank; dim++) {
                                        var len_type = int_type.copy ();
+                                       string? length_cname = get_ccode_array_length_name (f);
+                                       if (length_cname == null) {
+                                               length_cname = get_array_length_cname (get_ccode_name (f), 
dim);
+                                       }
 
                                        cdecl = new CCodeDeclaration (get_ccode_name (len_type));
-                                       cdecl.add_declarator (new CCodeVariableDeclarator 
(get_array_length_cname (get_ccode_name (f), dim)));
+                                       cdecl.add_declarator (new CCodeVariableDeclarator (length_cname));
                                        if (f.is_private_symbol ()) {
                                                cdecl.modifiers = CCodeModifiers.STATIC;
                                        } else if (context.hide_internal && f.is_internal_symbol ()) {
@@ -1263,9 +1267,13 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                                        if (!array_type.fixed_length) {
                                                for (int dim = 1; dim <= array_type.rank; dim++) {
                                                        var len_type = int_type.copy ();
+                                                       string? length_cname = get_ccode_array_length_name 
(f);
+                                                       if (length_cname == null) {
+                                                               length_cname = get_array_length_cname 
(get_ccode_name (f), dim);
+                                                       }
 
                                                        var len_def = new CCodeDeclaration (get_ccode_name 
(len_type));
-                                                       len_def.add_declarator (new CCodeVariableDeclarator 
(get_array_length_cname (get_ccode_name (f), dim), new CCodeConstant ("0")));
+                                                       len_def.add_declarator (new CCodeVariableDeclarator 
(length_cname, new CCodeConstant ("0")));
                                                        if (!f.is_private_symbol ()) {
                                                                len_def.modifiers = CCodeModifiers.EXTERN;
                                                        } else {
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 707246aaf..51783c723 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -55,6 +55,9 @@ TESTS = \
        basic-types/bug777697.test \
        basic-types/bug787152.vala \
        basic-types/bug788775.vala \
+       arrays/class-field-length-cname.vala \
+       arrays/field-global-length-cname.vala \
+       arrays/struct-field-length-cname.vala \
        chainup/base-class-invalid.test \
        chainup/base-enum-invalid.test \
        chainup/base-invalid.test \
diff --git a/tests/arrays/class-field-length-cname.vala b/tests/arrays/class-field-length-cname.vala
new file mode 100644
index 000000000..39f54f7e8
--- /dev/null
+++ b/tests/arrays/class-field-length-cname.vala
@@ -0,0 +1,13 @@
+class Bar {
+       [CCode (array_length_cname = "foo_len")]
+       public int[] foo;
+
+       // would cause a symbol clash
+       public int foo_length1;
+}
+
+void main () {
+       var bar = new Bar ();
+       bar.foo = { 23, 42 };
+       assert (bar.foo.length == 2);
+}
diff --git a/tests/arrays/field-global-length-cname.vala b/tests/arrays/field-global-length-cname.vala
new file mode 100644
index 000000000..2422befb2
--- /dev/null
+++ b/tests/arrays/field-global-length-cname.vala
@@ -0,0 +1,10 @@
+[CCode (array_length_cname = "foo_len")]
+public int[] foo;
+
+// would cause a symbol clash
+int foo_length1;
+
+void main () {
+       foo = { 23, 42 };
+       assert (foo.length == 2);
+}
diff --git a/tests/arrays/struct-field-length-cname.vala b/tests/arrays/struct-field-length-cname.vala
new file mode 100644
index 000000000..dc7320d0d
--- /dev/null
+++ b/tests/arrays/struct-field-length-cname.vala
@@ -0,0 +1,12 @@
+struct Bar {
+       [CCode (array_length_cname = "foo_len")]
+       public int[] foo;
+
+       // would cause a symbol clash
+       public int foo_length1;
+}
+
+void main () {
+       Bar bar = {{ 23, 42 }, -1};
+       assert (bar.foo.length == 2);
+}


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