]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Respect array_length_cname attribute for global fields
authorRico Tzschichholz <ricotz@ubuntu.com>
Sun, 24 Feb 2019 20:44:48 +0000 (21:44 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Sun, 24 Feb 2019 22:13:14 +0000 (23:13 +0100)
codegen/valaccodebasemodule.vala
tests/Makefile.am
tests/arrays/class-field-length-cname.vala [new file with mode: 0644]
tests/arrays/field-global-length-cname.vala [new file with mode: 0644]
tests/arrays/struct-field-length-cname.vala [new file with mode: 0644]

index 533d230fd686fd1a1ceddaa35a843b9aa1351e3f..612ac79476fa7fc001b519c675e83d4d2ef5f131 100644 (file)
@@ -1099,7 +1099,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 
                                for (int dim = 1; dim <= array_type.rank; dim++) {
                                        cdecl = new CCodeDeclaration (length_ctype);
-                                       cdecl.add_declarator (new CCodeVariableDeclarator (get_array_length_cname (get_ccode_name (f), dim)));
+                                       cdecl.add_declarator (new CCodeVariableDeclarator (get_variable_array_length_cname (f, dim)));
                                        if (f.is_private_symbol ()) {
                                                cdecl.modifiers = CCodeModifiers.STATIC;
                                        } else if (context.hide_internal && f.is_internal_symbol ()) {
@@ -1306,7 +1306,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 
                                                for (int dim = 1; dim <= array_type.rank; dim++) {
                                                        var len_def = new CCodeDeclaration (length_ctype);
-                                                       len_def.add_declarator (new CCodeVariableDeclarator (get_array_length_cname (get_ccode_name (f), dim), new CCodeConstant ("0")));
+                                                       len_def.add_declarator (new CCodeVariableDeclarator (get_variable_array_length_cname (f, dim), new CCodeConstant ("0")));
                                                        if (!f.is_private_symbol ()) {
                                                                len_def.modifiers = CCodeModifiers.EXTERN;
                                                        } else {
index b8288288e824c21ff1df8828a54d826a61c243b4..62b56163c690816df84d233e3d56427566350efe 100644 (file)
@@ -67,6 +67,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 (file)
index 0000000..39f54f7
--- /dev/null
@@ -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 (file)
index 0000000..2422bef
--- /dev/null
@@ -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 (file)
index 0000000..dc7320d
--- /dev/null
@@ -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);
+}