[vala/wip/housekeeping: 4/4] Fix deprecation of class/struct fields



commit b0e7db934c829061833a49eabc7a106a2d29c7a1
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Wed Nov 2 15:36:33 2016 +0100

    Fix deprecation of class/struct fields
    
    Respect the "deprecated" attributes given to class and struct fields, while
    actually adding G_GNUC_DEPRECATED to their declaration.

 ccode/valaccodedeclaration.vala        |    7 +++++++
 ccode/valaccodestruct.vala             |    3 ++-
 ccode/valaccodevariabledeclarator.vala |    9 ++-------
 codegen/valaccodebasemodule.vala       |   27 +++++++++++++--------------
 codegen/valaccodestructmodule.vala     |   14 ++------------
 codegen/valagtypemodule.vala           |   20 ++++++--------------
 tests/Makefile.am                      |    1 +
 tests/annotations/deprecated.vala      |   32 ++++++++++++++++++++++++++++++++
 vala/valamethodcall.vala               |    2 +-
 9 files changed, 66 insertions(+), 49 deletions(-)
---
diff --git a/ccode/valaccodedeclaration.vala b/ccode/valaccodedeclaration.vala
index 24bda72..634165a 100644
--- a/ccode/valaccodedeclaration.vala
+++ b/ccode/valaccodedeclaration.vala
@@ -114,6 +114,9 @@ public class Vala.CCodeDeclaration : CCodeStatement {
                if ((modifiers & CCodeModifiers.REGISTER) == CCodeModifiers.REGISTER) {
                        writer.write_string ("register ");
                }
+               if ((modifiers & CCodeModifiers.VOLATILE) != 0) {
+                       writer.write_string ("volatile ");
+               }
                writer.write_string (type_name);
                writer.write_string (" ");
        
@@ -127,6 +130,10 @@ public class Vala.CCodeDeclaration : CCodeStatement {
                        decl.write_declaration (writer);
                }
 
+               if (CCodeModifiers.DEPRECATED in modifiers) {
+                       writer.write_string (" G_GNUC_DEPRECATED");
+               }
+
                writer.write_string (";");
                writer.write_newline ();
        }
diff --git a/ccode/valaccodestruct.vala b/ccode/valaccodestruct.vala
index 96836ed..1eadd02 100644
--- a/ccode/valaccodestruct.vala
+++ b/ccode/valaccodestruct.vala
@@ -59,9 +59,10 @@ public class Vala.CCodeStruct : CCodeNode {
         * @param type_name field type
         * @param name      member name
         */
-       public void add_field (string type_name, string name, CCodeDeclaratorSuffix? declarator_suffix = 
null) {
+       public void add_field (string type_name, string name, CCodeModifiers modifiers = 0, 
CCodeDeclaratorSuffix? declarator_suffix = null) {
                var decl = new CCodeDeclaration (type_name);
                decl.add_declarator (new CCodeVariableDeclarator (name, null, declarator_suffix));
+               decl.modifiers = modifiers;
                add_declaration (decl);
        }
        
diff --git a/ccode/valaccodevariabledeclarator.vala b/ccode/valaccodevariabledeclarator.vala
index c8dd874..43372bf 100644
--- a/ccode/valaccodevariabledeclarator.vala
+++ b/ccode/valaccodevariabledeclarator.vala
@@ -101,9 +101,8 @@ public class Vala.CCodeVariableDeclarator : CCodeDeclarator {
 }
 
 public class Vala.CCodeDeclaratorSuffix {
-       public bool array;
-       public CCodeExpression? array_length;
-       public bool deprecated;
+       bool array;
+       CCodeExpression? array_length;
 
        public CCodeDeclaratorSuffix.with_array (CCodeExpression? array_length = null) {
                this.array_length = array_length;
@@ -118,9 +117,5 @@ public class Vala.CCodeDeclaratorSuffix {
                        }
                        writer.write_string ("]");
                }
-
-               if (deprecated) {
-                       writer.write_string (" G_GNUC_DEPRECATED");
-               }
        }
 }
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index d375637..cdab6f1 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -990,12 +990,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 
                generate_type_declaration (f.variable_type, decl_space);
 
-               string field_ctype = get_ccode_name (f.variable_type);
-               if (f.is_volatile) {
-                       field_ctype = "volatile " + field_ctype;
-               }
-
-               var cdecl = new CCodeDeclaration (field_ctype);
+               var cdecl = new CCodeDeclaration (get_ccode_name (f.variable_type));
                cdecl.add_declarator (new CCodeVariableDeclarator (get_ccode_name (f), null, 
get_ccode_declarator_suffix (f.variable_type)));
                if (f.is_private_symbol ()) {
                        cdecl.modifiers = CCodeModifiers.STATIC;
@@ -1005,6 +1000,9 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                if (f.version.deprecated) {
                        cdecl.modifiers |= CCodeModifiers.DEPRECATED;
                }
+               if (f.is_volatile) {
+                       cdecl.modifiers |= CCodeModifiers.VOLATILE;
+               }
                decl_space.add_type_member_declaration (cdecl);
 
                if (f.get_lock_used ()) {
@@ -1085,11 +1083,6 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 
                CCodeExpression lhs = null;
 
-               string field_ctype = get_ccode_name (f.variable_type);
-               if (f.is_volatile) {
-                       field_ctype = "volatile " + field_ctype;
-               }
-
                if (f.binding == MemberBinding.INSTANCE)  {
                        if (is_gtypeinstance && f.access == SymbolAccessibility.PRIVATE) {
                                lhs = new CCodeMemberAccess.pointer (new CCodeMemberAccess.pointer (new 
CCodeIdentifier ("self"), "priv"), get_ccode_name (f));
@@ -1215,13 +1208,19 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                                        }
                                }
 
-                               var var_def = new CCodeDeclaration (field_ctype);
+                               var var_def = new CCodeDeclaration (get_ccode_name (f.variable_type));
                                var_def.add_declarator (var_decl);
                                if (!f.is_private_symbol ()) {
                                        var_def.modifiers = CCodeModifiers.EXTERN;
                                } else {
                                        var_def.modifiers = CCodeModifiers.STATIC;
                                }
+                               if (f.version.deprecated) {
+                                       var_def.modifiers |= CCodeModifiers.DEPRECATED;
+                               }
+                               if (f.is_volatile) {
+                                       var_def.modifiers |= CCodeModifiers.VOLATILE;
+                               }
                                cfile.add_type_member_declaration (var_def);
 
                                /* add array length fields where necessary */
@@ -1937,7 +1936,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                                if (local.captured) {
                                        generate_type_declaration (local.variable_type, cfile);
 
-                                       data.add_field (get_ccode_name (local.variable_type), get_local_cname 
(local), get_ccode_declarator_suffix (local.variable_type));
+                                       data.add_field (get_ccode_name (local.variable_type), get_local_cname 
(local), 0, get_ccode_declarator_suffix (local.variable_type));
 
                                        if (local.variable_type is ArrayType) {
                                                var array_type = (ArrayType) local.variable_type;
@@ -2354,7 +2353,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                                }
                                emit_context.closure_variable_count_map.set (local.name, count + 1);
 
-                               closure_struct.add_field (get_ccode_name (local.variable_type), 
get_local_cname (local), get_ccode_declarator_suffix (local.variable_type));
+                               closure_struct.add_field (get_ccode_name (local.variable_type), 
get_local_cname (local), 0, get_ccode_declarator_suffix (local.variable_type));
                        } else {
                                var cvar = new CCodeVariableDeclarator (get_local_cname (local), null, 
get_ccode_declarator_suffix (local.variable_type));
 
diff --git a/codegen/valaccodestructmodule.vala b/codegen/valaccodestructmodule.vala
index 3f226d5..6b6682b 100644
--- a/codegen/valaccodestructmodule.vala
+++ b/codegen/valaccodestructmodule.vala
@@ -67,20 +67,10 @@ public abstract class Vala.CCodeStructModule : CCodeBaseModule {
                instance_struct.deprecated = st.version.deprecated;
 
                foreach (Field f in st.get_fields ()) {
-                       string field_ctype = get_ccode_name (f.variable_type);
-                       if (f.is_volatile) {
-                               field_ctype = "volatile " + field_ctype;
-                       }
-
                        if (f.binding == MemberBinding.INSTANCE)  {
                                generate_type_declaration (f.variable_type, decl_space);
-
-                               var suffix = get_ccode_declarator_suffix (f.variable_type);
-                               if (suffix != null) {
-                                       suffix.deprecated = f.version.deprecated;
-                               }
-
-                               instance_struct.add_field (field_ctype, get_ccode_name (f), suffix);
+                               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;
diff --git a/codegen/valagtypemodule.vala b/codegen/valagtypemodule.vala
index ed25185..2407446 100644
--- a/codegen/valagtypemodule.vala
+++ b/codegen/valagtypemodule.vala
@@ -322,16 +322,12 @@ public class Vala.GTypeModule : GErrorModule {
                }
 
                foreach (Field f in cl.get_fields ()) {
-                       string field_ctype = get_ccode_name (f.variable_type);
-                       if (f.is_volatile) {
-                               field_ctype = "volatile %s".printf (field_ctype);
-                       }
-
                        if (f.access != SymbolAccessibility.PRIVATE) {
+                               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 (field_ctype, get_ccode_name (f), 
get_ccode_declarator_suffix (f.variable_type));
+                                       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;
@@ -364,7 +360,7 @@ public class Vala.GTypeModule : GErrorModule {
                                                }
                                        }
                                } else if (f.binding == MemberBinding.CLASS) {
-                                       type_struct.add_field (field_ctype, get_ccode_name (f));
+                                       type_struct.add_field (get_ccode_name (f.variable_type), 
get_ccode_name (f), modifiers);
                                }
                        }
                }
@@ -429,16 +425,12 @@ public class Vala.GTypeModule : GErrorModule {
                }
 
                foreach (Field f in cl.get_fields ()) {
-                       string field_ctype = get_ccode_name (f.variable_type);
-                       if (f.is_volatile) {
-                               field_ctype = "volatile %s".printf (field_ctype);
-                       }
-
+                       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 (field_ctype, get_ccode_name (f), 
get_ccode_declarator_suffix (f.variable_type));
+                                       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;
@@ -478,7 +470,7 @@ public class Vala.GTypeModule : GErrorModule {
                                }
                        } else if (f.binding == MemberBinding.CLASS) {
                                if (f.access == SymbolAccessibility.PRIVATE) {
-                                       type_priv_struct.add_field (field_ctype, get_ccode_name (f));
+                                       type_priv_struct.add_field (get_ccode_name (f.variable_type), 
get_ccode_name (f), modifiers);
                                }
 
                                if (f.get_lock_used ()) {
diff --git a/tests/Makefile.am b/tests/Makefile.am
index f5e99a2..2a2ea07 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -238,6 +238,7 @@ TESTS = \
        gir/bug651773.test \
        gir/bug667751.test \
        gir/bug742012.test \
+       annotations/deprecated.vala \
        $(NULL)
 
 check-TESTS: $(TESTS)
diff --git a/tests/annotations/deprecated.vala b/tests/annotations/deprecated.vala
new file mode 100644
index 0000000..c9f4a1a
--- /dev/null
+++ b/tests/annotations/deprecated.vala
@@ -0,0 +1,32 @@
+[Version (deprecated = true)]
+struct FooStruct {
+       [Version (deprecated = true)]
+       public int bar;
+}
+
+void test_struct_field () {
+       FooStruct foo = { 42 };
+       var i = foo.bar;
+       foo.bar = i;
+       assert (foo.bar == 42);
+}
+
+[Version (deprecated = true)]
+class FooClass : Object {
+       [Version (deprecated = true)]
+       public int bar { get; set; default = 42; }
+       [Version (deprecated = true)]
+       public int baz;
+}
+
+void test_class_property () {
+       var foo = new FooClass ();
+       var i = foo.bar;
+       foo.bar = i;
+       assert (foo.bar == 42);
+}
+
+void main () {
+       test_class_property ();
+       test_struct_field ();
+}
diff --git a/vala/valamethodcall.vala b/vala/valamethodcall.vala
index d3889eb..e604afe 100644
--- a/vala/valamethodcall.vala
+++ b/vala/valamethodcall.vala
@@ -46,7 +46,7 @@ public class Vala.MethodCall : Expression {
         */
        public bool is_constructv_chainup { get; private set; }
 
-       public Expression _call;
+       private Expression _call;
        
        private List<Expression> argument_list = new ArrayList<Expression> ();
 


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