]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Create alternative for G_N_ELEMENTS in POSIX profile
authorRico Tzschichholz <ricotz@ubuntu.com>
Sat, 19 Oct 2019 13:15:42 +0000 (15:15 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Sat, 19 Oct 2019 13:15:42 +0000 (15:15 +0200)
codegen/valaccodebasemodule.vala
codegen/valaccodememberaccessmodule.vala

index 849e3ac2b39f815daaa3c28267deee5b75e5eb98..873f1ad3409e9b909d05a86d2e5db1706b196d99 100644 (file)
@@ -344,6 +344,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
        public bool requires_array_free;
        public bool requires_array_move;
        public bool requires_array_length;
+       public bool requires_array_n_elements;
        public bool requires_clear_mutex;
 
        public Set<string> wrappers;
@@ -773,6 +774,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                requires_array_free = false;
                requires_array_move = false;
                requires_array_length = false;
+               requires_array_n_elements = false;
                requires_clear_mutex = false;
 
                wrappers = new HashSet<string> (str_hash, str_equal);
@@ -807,6 +809,9 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                if (requires_array_length) {
                        append_vala_array_length ();
                }
+               if (requires_array_n_elements) {
+                       cfile.add_type_declaration (new CCodeMacroReplacement.with_expression ("VALA_N_ELEMENTS(arr)", new CCodeConstant ("(sizeof (arr) / sizeof ((arr)[0]))")));
+               }
                if (requires_clear_mutex) {
                        append_vala_clear_mutex ("GMutex", "g_mutex");
                        append_vala_clear_mutex ("GRecMutex", "g_rec_mutex");
index 3b8933d04a5b293560c4623fd7575260d6490f4f..7674402ca3d741157afd056d88fd395bd1b0cc41 100644 (file)
@@ -165,7 +165,13 @@ public abstract class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
                        if (array_type != null) {
                                string sub = "";
                                for (int i = 0; i < array_type.rank; i++) {
-                                       var ccall = new CCodeFunctionCall (new CCodeIdentifier ("G_N_ELEMENTS"));
+                                       CCodeFunctionCall ccall;
+                                       if (context.profile == Profile.POSIX) {
+                                               requires_array_n_elements = true;
+                                               ccall = new CCodeFunctionCall (new CCodeIdentifier ("VALA_N_ELEMENTS"));
+                                       } else {
+                                               ccall = new CCodeFunctionCall (new CCodeIdentifier ("G_N_ELEMENTS"));
+                                       }
                                        ccall.add_argument (new CCodeIdentifier (get_ccode_name (c) + sub));
                                        append_array_length (expr, ccall);
                                        sub += "[0]";