[vala] codegen: Support deprecating properties and their accessors



commit ad87c4eb227c438a3bcadda9ee9b87aacb77adae
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Thu Oct 13 09:39:47 2016 +0200

    codegen: Support deprecating properties and their accessors
    
    Use G_PARAM_DEPRECATED for properties and in addition their accessor
    methods will be marked as deprecated.
    
    Guard internal accessors calls with G_GNUC_BEGIN/END_IGNORED_DEPRECATIONS
    to silence unavoidable warnings.
    
    Based on patch by Simon Werbeck <simon werbeck gmail com>
    
    https://bugzilla.gnome.org/show_bug.cgi?id=732449

 ccode/Makefile.am                |    1 +
 ccode/valaccodeggnucsection.vala |   64 ++++++++++++++++++++++++++++++++++++++
 codegen/valaccodebasemodule.vala |    4 ++
 codegen/valagobjectmodule.vala   |   19 +++++++++--
 codegen/valagtypemodule.vala     |    3 ++
 5 files changed, 87 insertions(+), 4 deletions(-)
---
diff --git a/ccode/Makefile.am b/ccode/Makefile.am
index fd3186a..0402f62 100644
--- a/ccode/Makefile.am
+++ b/ccode/Makefile.am
@@ -40,6 +40,7 @@ libvalaccode_la_VALASOURCES = \
        valaccodefunction.vala \
        valaccodefunctioncall.vala \
        valaccodefunctiondeclarator.vala \
+       valaccodeggnucsection.vala \
        valaccodegotostatement.vala \
        valaccodeidentifier.vala \
        valaccodeifstatement.vala \
diff --git a/ccode/valaccodeggnucsection.vala b/ccode/valaccodeggnucsection.vala
new file mode 100644
index 0000000..deb969c
--- /dev/null
+++ b/ccode/valaccodeggnucsection.vala
@@ -0,0 +1,64 @@
+/* valaccodeggnucsection.vala
+ *
+ * Copyright (C) 2016  Rico Tzschichholz
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+ *
+ * Author:
+ *     Rico Tzschichholz <ricotz ubuntu com>
+ */
+
+using GLib;
+
+/**
+ * Represents a section that should be processed on condition.
+ */
+public class Vala.CCodeGGnucSection : CCodeFragment {
+       /**
+        * The expression
+        */
+       public GGnucSectionType section_type { get; set; }
+
+       public CCodeGGnucSection (GGnucSectionType t) {
+               section_type = t;
+       }
+
+       public override void write (CCodeWriter writer) {
+               writer.write_string ("G_GNUC_BEGIN_");
+               writer.write_string (section_type.to_string ());
+               foreach (CCodeNode node in get_children ()) {
+                       node.write_combined (writer);
+               }
+               writer.write_string ("G_GNUC_END_");
+               writer.write_string (section_type.to_string ());
+               writer.write_newline ();
+       }
+
+       public override void write_declaration (CCodeWriter writer) {
+       }
+}
+
+public enum Vala.GGnucSectionType {
+       IGNORE_DEPRECATIONS;
+
+       public unowned string to_string () {
+               switch (this) {
+                       case IGNORE_DEPRECATIONS:
+                               return "IGNORE_DEPRECATIONS";
+                       default:
+                               assert_not_reached ();
+               }
+       }
+}
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 2f983d0..60fd6bf 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -1526,6 +1526,10 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                        }
                }
 
+               if (prop.version.deprecated) {
+                       function.modifiers |= CCodeModifiers.DEPRECATED;
+               }
+
                if (prop.is_private_symbol () || (!acc.readable && !acc.writable) || acc.access == 
SymbolAccessibility.PRIVATE) {
                        function.modifiers |= CCodeModifiers.STATIC;
                } else if (context.hide_internal && (prop.is_internal_symbol () || acc.access == 
SymbolAccessibility.INTERNAL)) {
diff --git a/codegen/valagobjectmodule.vala b/codegen/valagobjectmodule.vala
index cc14136..d4b8887 100644
--- a/codegen/valagobjectmodule.vala
+++ b/codegen/valagobjectmodule.vala
@@ -166,6 +166,17 @@ public class Vala.GObjectModule : GTypeModule {
                return false;
        }
 
+       private void add_guarded_expression (Symbol sym, CCodeExpression expression) {
+               // prevent deprecation warnings
+               if (sym.version.deprecated) {
+                       var guard = new CCodeGGnucSection (GGnucSectionType.IGNORE_DEPRECATIONS);
+                       ccode.add_statement (guard);
+                       guard.append (new CCodeExpressionStatement (expression));
+               } else {
+                       ccode.add_expression (expression);
+               }
+       }
+
        private void add_get_property_function (Class cl) {
                var get_prop = new CCodeFunction ("_vala_%s_get_property".printf (get_ccode_lower_case_name 
(cl, null)), "void");
                get_prop.modifiers = CCodeModifiers.STATIC;
@@ -231,7 +242,7 @@ public class Vala.GObjectModule : GTypeModule {
                                csetcall.call = get_value_setter_function (prop.property_type);
                                csetcall.add_argument (new CCodeIdentifier ("value"));
                                csetcall.add_argument (boxed_addr);
-                               ccode.add_expression (csetcall);
+                               add_guarded_expression (prop, csetcall);
 
                                if (requires_destroy (prop.get_accessor.value_type)) {
                                        ccode.add_expression (destroy_value (new GLibValue 
(prop.get_accessor.value_type, new CCodeIdentifier ("boxed"), true)));
@@ -255,7 +266,7 @@ public class Vala.GObjectModule : GTypeModule {
                                }
                                csetcall.add_argument (new CCodeIdentifier ("value"));
                                csetcall.add_argument (ccall);
-                               ccode.add_expression (csetcall);
+                               add_guarded_expression (prop, csetcall);
                                if (array_type != null && array_type.element_type.data_type == 
string_type.data_type) {
                                        ccode.close ();
                                }
@@ -339,7 +350,7 @@ public class Vala.GObjectModule : GTypeModule {
                                var ccond = new CCodeConditionalExpression (cisnull, new CCodeConstant ("0"), 
cstrvlen);
 
                                ccall.add_argument (ccond);
-                               ccode.add_expression (ccall);
+                               add_guarded_expression (prop, ccall);
                                ccode.close ();
                        } else {
                                var cgetcall = new CCodeFunctionCall ();
@@ -350,7 +361,7 @@ public class Vala.GObjectModule : GTypeModule {
                                }
                                cgetcall.add_argument (new CCodeIdentifier ("value"));
                                ccall.add_argument (cgetcall);
-                               ccode.add_expression (ccall);
+                               add_guarded_expression (prop, ccall);
                        }
                        ccode.add_break ();
                }
diff --git a/codegen/valagtypemodule.vala b/codegen/valagtypemodule.vala
index f46465f..4bbfab6 100644
--- a/codegen/valagtypemodule.vala
+++ b/codegen/valagtypemodule.vala
@@ -1893,6 +1893,9 @@ public class Vala.GTypeModule : GErrorModule {
                                }
                        }
                }
+               if (prop.version.deprecated) {
+                       pflags = "%s%s".printf (pflags, " | G_PARAM_DEPRECATED");
+               }
                cspec.add_argument (new CCodeConstant (pflags));
 
                return cspec;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]