[vala/staging: 2/2] codegen: Handle property initializers of non-gobject classes



commit 2bd54c30e0feb843e6ff41f93a3f1f601d0c2d08
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Sat Apr 14 12:35:53 2018 +0200

    codegen: Handle property initializers of non-gobject classes
    
    73e553ac3488d641fb08b275bcf2636e3cf0de67 introduced this for GObject classes only
    
    Add a warning for property default values in structs and compact classes.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=795225

 codegen/valagobjectmodule.vala |   21 ---------------------
 codegen/valagtypemodule.vala   |   26 ++++++++++++++++++++++++++
 tests/objects/bug701978.vala   |   13 +++++++++++++
 3 files changed, 39 insertions(+), 21 deletions(-)
---
diff --git a/codegen/valagobjectmodule.vala b/codegen/valagobjectmodule.vala
index 82eba07..11aa6e6 100644
--- a/codegen/valagobjectmodule.vala
+++ b/codegen/valagobjectmodule.vala
@@ -688,27 +688,6 @@ public class Vala.GObjectModule : GTypeModule {
 
                if (is_gobject_property (prop) && prop.parent_symbol is Class) {
                        prop_enum.add_value (new CCodeEnumValue ("%s_PROPERTY".printf 
(get_ccode_upper_case_name (prop))));
-
-                       if (prop.initializer != null && prop.set_accessor != null && 
!prop.set_accessor.automatic_body) {
-                               // generate a custom initializer if it couldn't be done at class_init time
-                               bool has_spec_initializer = prop.property_type.data_type is Enum;
-                               if (!has_spec_initializer && prop.property_type.data_type is Struct) {
-                                       var param_spec_func = get_ccode_param_spec_function 
(prop.property_type.data_type);
-                                       has_spec_initializer = param_spec_func != "g_param_spec_boxed";
-                               }
-                               if (!has_spec_initializer) {
-                                       push_context (instance_init_context);
-
-                                       prop.initializer.emit (this);
-
-                                       var inst_ma = new MemberAccess.simple ("this");
-                                       inst_ma.target_value = new GLibValue (get_data_type_for_symbol 
((Class) prop.parent_symbol), new CCodeIdentifier ("self"), true);
-                                       store_property (prop, inst_ma, prop.initializer.target_value);
-
-                                       temp_ref_values.clear ();
-                                       pop_context ();
-                               }
-                       }
                }
        }
 
diff --git a/codegen/valagtypemodule.vala b/codegen/valagtypemodule.vala
index daad49c..5785896 100644
--- a/codegen/valagtypemodule.vala
+++ b/codegen/valagtypemodule.vala
@@ -2392,6 +2392,32 @@ public class Vala.GTypeModule : GErrorModule {
                        Report.error (prop.source_reference, "Property 'type' not allowed");
                        return;
                }
+
+               if (prop.initializer != null) {
+                       if (cl == null || cl.is_compact) {
+                               Report.warning (prop.source_reference, "Only properties in non-compact 
classes are allowed to have initializers");
+                       } else if (prop.set_accessor != null && !prop.set_accessor.automatic_body) {
+                               // generate a custom initializer if it couldn't be done at class_init time
+                               bool has_spec_initializer = prop.property_type.data_type is Enum;
+                               if (!has_spec_initializer && prop.property_type.data_type is Struct) {
+                                       var param_spec_func = get_ccode_param_spec_function 
(prop.property_type.data_type);
+                                       has_spec_initializer = param_spec_func != "g_param_spec_boxed";
+                               }
+                               if (!has_spec_initializer) {
+                                       push_context (instance_init_context);
+
+                                       prop.initializer.emit (this);
+
+                                       var inst_ma = new MemberAccess.simple ("this");
+                                       inst_ma.target_value = new GLibValue (get_data_type_for_symbol (cl), 
new CCodeIdentifier ("self"), true);
+                                       store_property (prop, inst_ma, prop.initializer.target_value);
+
+                                       temp_ref_values.clear ();
+                                       pop_context ();
+                               }
+                       }
+               }
+
                base.visit_property (prop);
        }
 
diff --git a/tests/objects/bug701978.vala b/tests/objects/bug701978.vala
index 22fcfaf..aa809c1 100644
--- a/tests/objects/bug701978.vala
+++ b/tests/objects/bug701978.vala
@@ -16,7 +16,20 @@ public class Bar : Object {
        }
 }
 
+public class Baz {
+       private Foo _foo;
+
+       public Foo foo {
+               get { return _foo; }
+               set { _foo = value; }
+               default = Foo ();
+       }
+}
+
 void main () {
        var bar = new Bar();
        assert (bar.foo.val == 55);
+
+       var baz = new Baz();
+       assert (baz.foo.val == 55);
 }


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