From 37c1a07f385bfeb48bdba872e3418a54b28744e4 Mon Sep 17 00:00:00 2001 From: Simon Werbeck Date: Mon, 30 Dec 2019 20:19:21 +0100 Subject: [PATCH] codegen: Refactor generation of fields for ccode structs Introduce a common method to add field including its possible composite fields for array and delegate types. --- codegen/valaccodebasemodule.vala | 31 +++++++++++++++ codegen/valaccodestructmodule.vala | 30 +-------------- codegen/valagtypemodule.vala | 60 +----------------------------- 3 files changed, 34 insertions(+), 87 deletions(-) diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala index 668d3cee2..932228eb5 100644 --- a/codegen/valaccodebasemodule.vala +++ b/codegen/valaccodebasemodule.vala @@ -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; diff --git a/codegen/valaccodestructmodule.vala b/codegen/valaccodestructmodule.vala index e432e6060..2cd56bb67 100644 --- a/codegen/valaccodestructmodule.vala +++ b/codegen/valaccodestructmodule.vala @@ -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); } } diff --git a/codegen/valagtypemodule.vala b/codegen/valagtypemodule.vala index 17c0690b3..a67b8c68c 100644 --- a/codegen/valagtypemodule.vala +++ b/codegen/valagtypemodule.vala @@ -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) { -- 2.47.2