]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Refactor generation of fields for ccode structs
authorSimon Werbeck <simon.werbeck@gmail.com>
Mon, 30 Dec 2019 19:19:21 +0000 (20:19 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Tue, 21 Jan 2020 08:40:30 +0000 (09:40 +0100)
Introduce a common method to add field including its possible composite
fields for array and delegate types.

codegen/valaccodebasemodule.vala
codegen/valaccodestructmodule.vala
codegen/valagtypemodule.vala

index 668d3cee22a55dfe91efb351d724de59ac455d5d..932228eb57b10f1e1d1ae78716d8a443d6cb672a 100644 (file)
@@ -1060,6 +1060,37 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                pop_line ();
        }
 
+       public void append_field (CCodeStruct ccode_struct, Field f, CCodeFile decl_space) {
+               generate_type_declaration (f.variable_type, decl_space);
+
+               CCodeModifiers modifiers = (f.is_volatile ? CCodeModifiers.VOLATILE : 0) | (f.version.deprecated ? CCodeModifiers.DEPRECATED : 0);
+               ccode_struct.add_field (get_ccode_name (f.variable_type), get_ccode_name (f), modifiers, get_ccode_declarator_suffix (f.variable_type));
+
+               if (f.variable_type is ArrayType && get_ccode_array_length (f)) {
+                       // create fields to store array dimensions
+                       var array_type = (ArrayType) f.variable_type;
+                       if (!array_type.fixed_length) {
+                               var length_ctype = get_ccode_array_length_type (array_type);
+                               for (int dim = 1; dim <= array_type.rank; dim++) {
+                                       string length_cname = get_variable_array_length_cname (f, dim);
+                                       ccode_struct.add_field (length_ctype, length_cname);
+                               }
+                               if (array_type.rank == 1 && f.is_internal_symbol ()) {
+                                       ccode_struct.add_field (length_ctype, get_array_size_cname (get_ccode_name (f)));
+                               }
+                       }
+               } else if (get_ccode_delegate_target (f)) {
+                       var delegate_type = (DelegateType) f.variable_type;
+                       if (delegate_type.delegate_symbol.has_target) {
+                               // create field to store delegate target
+                               ccode_struct.add_field (get_ccode_name (delegate_target_type), get_ccode_delegate_target_name (f));
+                               if (delegate_type.is_disposable ()) {
+                                       ccode_struct.add_field (get_ccode_name (delegate_target_destroy_type), get_ccode_delegate_target_destroy_notify_name (f));
+                               }
+                       }
+               }
+       }
+
        public void generate_field_declaration (Field f, CCodeFile decl_space) {
                if (add_symbol_declaration (decl_space, f, get_ccode_name (f))) {
                        return;
index e432e60606157452bcb9a5738c6ff9ee475827e0..2cd56bb67f56824d873cb5878850bbc25e0f74e7 100644 (file)
@@ -77,35 +77,7 @@ public abstract class Vala.CCodeStructModule : CCodeBaseModule {
 
                foreach (Field f in st.get_fields ()) {
                        if (f.binding == MemberBinding.INSTANCE)  {
-                               generate_type_declaration (f.variable_type, decl_space);
-                               CCodeModifiers modifiers = (f.is_volatile ? CCodeModifiers.VOLATILE : 0) | (f.version.deprecated ? CCodeModifiers.DEPRECATED : 0);
-                               instance_struct.add_field (get_ccode_name (f.variable_type), get_ccode_name (f), modifiers, get_ccode_declarator_suffix (f.variable_type));
-                               if (f.variable_type is ArrayType && get_ccode_array_length (f)) {
-                                       // create fields to store array dimensions
-                                       var array_type = (ArrayType) f.variable_type;
-
-                                       if (!array_type.fixed_length) {
-                                               var length_ctype = get_ccode_array_length_type (array_type);
-
-                                               for (int dim = 1; dim <= array_type.rank; dim++) {
-                                                       string length_cname = get_variable_array_length_cname (f, dim);
-                                                       instance_struct.add_field (length_ctype, length_cname);
-                                               }
-
-                                               if (array_type.rank == 1 && f.is_internal_symbol ()) {
-                                                       instance_struct.add_field (length_ctype, get_array_size_cname (get_ccode_name (f)));
-                                               }
-                                       }
-                               } else if (get_ccode_delegate_target (f)) {
-                                       var delegate_type = (DelegateType) f.variable_type;
-                                       if (delegate_type.delegate_symbol.has_target) {
-                                               // create field to store delegate target
-                                               instance_struct.add_field (get_ccode_name (delegate_target_type), get_ccode_delegate_target_name (f));
-                                               if (delegate_type.is_disposable ()) {
-                                                       instance_struct.add_field (get_ccode_name (delegate_target_destroy_type), get_ccode_delegate_target_destroy_notify_name (f));
-                                               }
-                                       }
-                               }
+                               append_field (instance_struct, f, decl_space);
                        }
                }
 
index 17c0690b3fc6c57e4fe749cb1d69b4b8fb2119b3..a67b8c68ce93a979aeb4659fa1fcc1e855cea20b 100644 (file)
@@ -410,36 +410,8 @@ public class Vala.GTypeModule : GErrorModule {
 
                CCodeModifiers modifiers = (f.is_volatile ? CCodeModifiers.VOLATILE : 0) | (f.version.deprecated ? CCodeModifiers.DEPRECATED : 0);
                if (f.binding == MemberBinding.INSTANCE) {
-                       generate_type_declaration (f.variable_type, decl_space);
-
-                       instance_struct.add_field (get_ccode_name (f.variable_type), get_ccode_name (f), modifiers, get_ccode_declarator_suffix (f.variable_type));
+                       append_field (instance_struct, f, decl_space);
                        has_struct_member = true;
-                       if (f.variable_type is ArrayType && get_ccode_array_length (f)) {
-                               // create fields to store array dimensions
-                               var array_type = (ArrayType) f.variable_type;
-
-                               if (!array_type.fixed_length) {
-                                       var length_ctype = get_ccode_array_length_type (array_type);
-
-                                       for (int dim = 1; dim <= array_type.rank; dim++) {
-                                               string length_cname = get_variable_array_length_cname (f, dim);
-                                               instance_struct.add_field (length_ctype, length_cname);
-                                       }
-
-                                       if (array_type.rank == 1 && f.is_internal_symbol ()) {
-                                               instance_struct.add_field (length_ctype, get_array_size_cname (get_ccode_name (f)));
-                                       }
-                               }
-                       } else if (get_ccode_delegate_target (f)) {
-                               var delegate_type = (DelegateType) f.variable_type;
-                               if (delegate_type.delegate_symbol.has_target) {
-                                       // create field to store delegate target
-                                       instance_struct.add_field (get_ccode_name (delegate_target_type), get_ccode_delegate_target_name (f));
-                                       if (delegate_type.is_disposable ()) {
-                                               instance_struct.add_field (get_ccode_name (delegate_target_destroy_type), get_ccode_delegate_target_destroy_notify_name (f));
-                                       }
-                               }
-                       }
                } else if (f.binding == MemberBinding.CLASS) {
                        type_struct.add_field (get_ccode_name (f.variable_type), get_ccode_name (f), modifiers);
                }
@@ -521,35 +493,7 @@ public class Vala.GTypeModule : GErrorModule {
                        CCodeModifiers modifiers = (f.is_volatile ? CCodeModifiers.VOLATILE : 0) | (f.version.deprecated ? CCodeModifiers.DEPRECATED : 0);
                        if (f.binding == MemberBinding.INSTANCE) {
                                if (f.access == SymbolAccessibility.PRIVATE)  {
-                                       generate_type_declaration (f.variable_type, decl_space);
-
-                                       instance_priv_struct.add_field (get_ccode_name (f.variable_type), get_ccode_name (f), modifiers, get_ccode_declarator_suffix (f.variable_type));
-                                       if (f.variable_type is ArrayType && get_ccode_array_length (f)) {
-                                               // create fields to store array dimensions
-                                               var array_type = (ArrayType) f.variable_type;
-
-                                               if (!array_type.fixed_length) {
-                                                       var length_ctype = get_ccode_array_length_type (array_type);
-
-                                                       for (int dim = 1; dim <= array_type.rank; dim++) {
-                                                               string length_cname = get_variable_array_length_cname (f, dim);
-                                                               instance_priv_struct.add_field (length_ctype, length_cname);
-                                                       }
-
-                                                       if (array_type.rank == 1 && f.is_internal_symbol ()) {
-                                                               instance_priv_struct.add_field (length_ctype, get_array_size_cname (get_ccode_name (f)));
-                                                       }
-                                               }
-                                       } else if (get_ccode_delegate_target (f)) {
-                                               var delegate_type = (DelegateType) f.variable_type;
-                                               if (delegate_type.delegate_symbol.has_target) {
-                                                       // create field to store delegate target
-                                                       instance_priv_struct.add_field (get_ccode_name (delegate_target_type), get_ccode_delegate_target_name (f));
-                                                       if (delegate_type.is_disposable ()) {
-                                                               instance_priv_struct.add_field (get_ccode_name (delegate_target_destroy_type), get_ccode_delegate_target_destroy_notify_name (f));
-                                                       }
-                                               }
-                                       }
+                                       append_field (instance_priv_struct, f, decl_space);
                                }
 
                                if (f.lock_used) {