From: Rico Tzschichholz Date: Tue, 8 Nov 2016 11:00:48 +0000 (+0100) Subject: Add CCodeNode "modifiers" and transform CCodeFunction's "attributes" to it X-Git-Tag: 0.35.1~23 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dcac30061993d9625fc6ebb74ad9e42cafb9ff65;p=thirdparty%2Fvala.git Add CCodeNode "modifiers" and transform CCodeFunction's "attributes" to it --- diff --git a/ccode/valaccodedeclaration.vala b/ccode/valaccodedeclaration.vala index 634165a69..4d0fa7425 100644 --- a/ccode/valaccodedeclaration.vala +++ b/ccode/valaccodedeclaration.vala @@ -31,11 +31,6 @@ public class Vala.CCodeDeclaration : CCodeStatement { */ public string type_name { get; set; } - /** - * The declaration modifier. - */ - public CCodeModifiers modifiers { get; set; } - private List declarators = new ArrayList (); public CCodeDeclaration (string type_name) { diff --git a/ccode/valaccodefunction.vala b/ccode/valaccodefunction.vala index 44dd5b956..43bfb6cbb 100644 --- a/ccode/valaccodefunction.vala +++ b/ccode/valaccodefunction.vala @@ -31,18 +31,11 @@ public class Vala.CCodeFunction : CCodeNode { */ public string name { get; set; } - /** - * The function modifiers. - */ - public CCodeModifiers modifiers { get; set; } - /** * The function return type. */ public string return_type { get; set; } - public string attributes { get; set; } - public bool is_declaration { get; set; } /** @@ -96,7 +89,6 @@ public class Vala.CCodeFunction : CCodeNode { public CCodeFunction copy () { var func = new CCodeFunction (name, return_type); func.modifiers = modifiers; - func.attributes = attributes; /* no deep copy for lists available yet * func.parameters = parameters.copy (); @@ -153,9 +145,17 @@ public class Vala.CCodeFunction : CCodeNode { writer.write_string (" G_GNUC_FORMAT(%d)".printf (format_arg_index + 1)); } - if (attributes != null) { - writer.write_string (" "); - writer.write_string (attributes); + if (CCodeModifiers.CONST in modifiers) { + writer.write_string (" G_GNUC_CONST"); + } + if (CCodeModifiers.UNUSED in modifiers) { + writer.write_string (" G_GNUC_UNUSED"); + } + + if (CCodeModifiers.CONSTRUCTOR in modifiers) { + writer.write_string (" __attribute__((constructor))"); + } else if (CCodeModifiers.DESTRUCTOR in modifiers) { + writer.write_string (" __attribute__((destructor))"); } writer.write_string (";"); diff --git a/ccode/valaccodemodifiers.vala b/ccode/valaccodemodifiers.vala index 600751d6c..086056f67 100644 --- a/ccode/valaccodemodifiers.vala +++ b/ccode/valaccodemodifiers.vala @@ -34,5 +34,9 @@ public enum Vala.CCodeModifiers { VOLATILE = 1 << 4, DEPRECATED = 1 << 5, THREAD_LOCAL = 1 << 6, - INTERNAL = 1 << 7 + INTERNAL = 1 << 7, + CONST = 1 << 8, + UNUSED = 1 << 9, + CONSTRUCTOR = 1 << 10, + DESTRUCTOR = 1 << 11 } diff --git a/ccode/valaccodenode.vala b/ccode/valaccodenode.vala index 6ab095f60..d8c3e5cdf 100644 --- a/ccode/valaccodenode.vala +++ b/ccode/valaccodenode.vala @@ -32,6 +32,12 @@ public abstract class Vala.CCodeNode { */ public CCodeLineDirective line { get; set; } + /** + * The modifiers for this code node which will be handled as needed + * in every subclass. + */ + public CCodeModifiers modifiers { get; set; } + /** * Writes this code node and all children with the specified C code * writer. diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala index cdab6f1c8..adccba732 100644 --- a/codegen/valaccodebasemodule.vala +++ b/codegen/valaccodebasemodule.vala @@ -827,14 +827,13 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator { var fun_name = "%s_get_type".printf (get_ccode_lower_case_name (en, null)); var regfun = new CCodeFunction (fun_name, "GType"); - regfun.attributes = "G_GNUC_CONST"; + regfun.modifiers = CCodeModifiers.CONST; if (en.is_private_symbol ()) { - regfun.modifiers = CCodeModifiers.STATIC; // avoid C warning as this function is not always used - regfun.attributes += " G_GNUC_UNUSED"; + regfun.modifiers |= CCodeModifiers.STATIC | CCodeModifiers.UNUSED; } else if (context.hide_internal && en.is_internal_symbol ()) { - regfun.modifiers = CCodeModifiers.INTERNAL; + regfun.modifiers |= CCodeModifiers.INTERNAL; } decl_space.add_function_declaration (regfun); diff --git a/codegen/valaccodemethodmodule.vala b/codegen/valaccodemethodmodule.vala index b50380a9a..195592ff7 100644 --- a/codegen/valaccodemethodmodule.vala +++ b/codegen/valaccodemethodmodule.vala @@ -369,8 +369,7 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule { cfile.add_type_member_declaration (timer_decl); var constructor = new CCodeFunction (prefix + "_init"); - constructor.modifiers = CCodeModifiers.STATIC; - constructor.attributes = "__attribute__((constructor))"; + constructor.modifiers = CCodeModifiers.STATIC | CCodeModifiers.CONSTRUCTOR; cfile.add_function_declaration (constructor); push_function (constructor); @@ -385,8 +384,7 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule { var destructor = new CCodeFunction (prefix + "_exit"); - destructor.modifiers = CCodeModifiers.STATIC; - destructor.attributes = "__attribute__((destructor))"; + destructor.modifiers = CCodeModifiers.STATIC | CCodeModifiers.DESTRUCTOR; cfile.add_function_declaration (destructor); push_function (destructor); diff --git a/codegen/valagdbusclientmodule.vala b/codegen/valagdbusclientmodule.vala index 079ac52b8..20229fdea 100644 --- a/codegen/valagdbusclientmodule.vala +++ b/codegen/valagdbusclientmodule.vala @@ -163,7 +163,7 @@ public class Vala.GDBusClientModule : GDBusModule { // declare proxy_get_type function var proxy_get_type = new CCodeFunction (get_type_name, "GType"); - proxy_get_type.attributes = "G_GNUC_CONST"; + proxy_get_type.modifiers = CCodeModifiers.CONST; decl_space.add_function_declaration (proxy_get_type); if (in_plugin) { diff --git a/codegen/valagtypemodule.vala b/codegen/valagtypemodule.vala index 692a9bd33..d4313d163 100644 --- a/codegen/valagtypemodule.vala +++ b/codegen/valagtypemodule.vala @@ -119,9 +119,8 @@ public class Vala.GTypeModule : GErrorModule { function.add_parameter (new CCodeParameter ("flags", "GParamFlags")); if (cl.is_private_symbol ()) { - function.modifiers = CCodeModifiers.STATIC; // avoid C warning as this function is not always used - function.attributes = "G_GNUC_UNUSED"; + function.modifiers = CCodeModifiers.STATIC | CCodeModifiers.UNUSED; } else if (context.hide_internal && cl.is_internal_symbol ()) { function.modifiers = CCodeModifiers.INTERNAL; } @@ -133,13 +132,11 @@ public class Vala.GTypeModule : GErrorModule { function.add_parameter (new CCodeParameter ("v_object", "gpointer")); if (cl.is_private_symbol ()) { - function.modifiers = CCodeModifiers.STATIC; // avoid C warning as this function is not always used - function.attributes = "G_GNUC_UNUSED"; + function.modifiers = CCodeModifiers.STATIC | CCodeModifiers.UNUSED; } else if (context.hide_internal && cl.is_internal_symbol ()) { - function.modifiers = CCodeModifiers.INTERNAL; // avoid C warning as this function is not always used - function.attributes = "G_GNUC_UNUSED"; + function.modifiers = CCodeModifiers.INTERNAL | CCodeModifiers.UNUSED; } decl_space.add_function_declaration (function); @@ -149,9 +146,8 @@ public class Vala.GTypeModule : GErrorModule { function.add_parameter (new CCodeParameter ("v_object", "gpointer")); if (cl.is_private_symbol ()) { - function.modifiers = CCodeModifiers.STATIC; // avoid C warning as this function is not always used - function.attributes = "G_GNUC_UNUSED"; + function.modifiers = CCodeModifiers.STATIC | CCodeModifiers.UNUSED; } else if (context.hide_internal && cl.is_internal_symbol ()) { function.modifiers = CCodeModifiers.INTERNAL; } @@ -162,13 +158,11 @@ public class Vala.GTypeModule : GErrorModule { function.add_parameter (new CCodeParameter ("value", "const GValue*")); if (cl.is_private_symbol ()) { - function.modifiers = CCodeModifiers.STATIC; // avoid C warning as this function is not always used - function.attributes = "G_GNUC_UNUSED"; + function.modifiers = CCodeModifiers.STATIC | CCodeModifiers.UNUSED; } else if (context.hide_internal && cl.is_internal_symbol ()) { - function.modifiers = CCodeModifiers.INTERNAL; // avoid C warning as this function is not always used - function.attributes = "G_GNUC_UNUSED"; + function.modifiers = CCodeModifiers.INTERNAL | CCodeModifiers.UNUSED; } decl_space.add_function_declaration (function); diff --git a/codegen/valatyperegisterfunction.vala b/codegen/valatyperegisterfunction.vala index b7c18f8a2..9617f8240 100644 --- a/codegen/valatyperegisterfunction.vala +++ b/codegen/valatyperegisterfunction.vala @@ -68,24 +68,22 @@ public abstract class Vala.TypeRegisterFunction { CCodeFunction fun; if (!plugin) { fun = new CCodeFunction ("%s_get_type".printf (CCodeBaseModule.get_ccode_lower_case_name (get_type_declaration ())), "GType"); - fun.attributes = "G_GNUC_CONST"; + fun.modifiers = CCodeModifiers.CONST; /* Function will not be prototyped anyway */ if (get_accessibility () == SymbolAccessibility.PRIVATE) { - fun.modifiers = CCodeModifiers.STATIC; // avoid C warning as this function is not always used - fun.attributes += " G_GNUC_UNUSED"; + fun.modifiers |= CCodeModifiers.STATIC | CCodeModifiers.UNUSED; } else if (context.hide_internal && get_accessibility () == SymbolAccessibility.INTERNAL) { - fun.modifiers = CCodeModifiers.INTERNAL; // avoid C warning as this function is not always used - fun.attributes += " G_GNUC_UNUSED"; + fun.modifiers |= CCodeModifiers.INTERNAL | CCodeModifiers.UNUSED; } } else { fun = new CCodeFunction ("%s_register_type".printf (CCodeBaseModule.get_ccode_lower_case_name (get_type_declaration ())), "GType"); fun.add_parameter (new CCodeParameter ("module", "GTypeModule *")); var get_fun = new CCodeFunction ("%s_get_type".printf (CCodeBaseModule.get_ccode_lower_case_name (get_type_declaration ())), "GType"); - get_fun.attributes = "G_GNUC_CONST"; + get_fun.modifiers = CCodeModifiers.CONST; get_fun.is_declaration = true; declaration_fragment.append (get_fun.copy ());