]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Always chain up to base constructor when possible, add
authorJürg Billeter <j@bitron.ch>
Sat, 11 Oct 2008 11:08:00 +0000 (11:08 +0000)
committerJürg Billeter <juergbi@src.gnome.org>
Sat, 11 Oct 2008 11:08:00 +0000 (11:08 +0000)
2008-10-11  Jürg Billeter  <j@bitron.ch>

* vala/valainterfacewriter.vala:
* vala/valamethod.vala:
* gobject/valaccodemethodbinding.vala:
* vapigen/valagidlparser.vala:

Always chain up to base constructor when possible,
add has_construct_function attribute

svn path=/trunk/; revision=1830

ChangeLog
gobject/valaccodemethodbinding.vala
vala/valainterfacewriter.vala
vala/valamethod.vala
vapigen/valagidlparser.vala

index 00ffbb35cbe84fedc5201ada9d92e5ab0320ea67..cdceb55c87a2cde2a8e249c86f74f1048612779e 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2008-10-11  Jürg Billeter  <j@bitron.ch>
+
+       * vala/valainterfacewriter.vala:
+       * vala/valamethod.vala:
+       * gobject/valaccodemethodbinding.vala:
+       * vapigen/valagidlparser.vala:
+
+       Always chain up to base constructor when possible,
+       add has_construct_function attribute
+
 2008-10-11  Jürg Billeter  <j@bitron.ch>
 
        * vala/valaclass.vala:
index e0b2316fe4e0202cbe5564634733d32e90948e65..4c8268f27b5647a5cec95631c99411c7de38c59f 100644 (file)
@@ -846,19 +846,41 @@ public class Vala.CCodeMethodBinding : CCodeBinding {
 
        private void add_object_creation (CCodeBlock b, bool has_params) {
                var cl = (Class) codegen.current_type_symbol;
-       
-               var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_object_newv"));
-               ccall.add_argument (new CCodeIdentifier ("object_type"));
-               if (has_params) {
-                       ccall.add_argument (new CCodeConstant ("__params_it - __params"));
-                       ccall.add_argument (new CCodeConstant ("__params"));
-               } else {
-                       ccall.add_argument (new CCodeConstant ("0"));
-                       ccall.add_argument (new CCodeConstant ("NULL"));
+
+               bool chain_up = false;
+               CreationMethod cm = null;
+               if (cl.base_class != null) {
+                       cm = cl.base_class.default_construction_method as CreationMethod;
+                       if (cm != null && cm.get_parameters ().size == 0
+                           && cm.has_construct_function) {
+                                if (!has_params) {
+                                       chain_up = true;
+                                }
+                       }
                }
-               
+
+               if (!has_params && !chain_up
+                   && cl.base_class != codegen.gobject_type) {
+                       // possibly report warning or error about missing base call
+               }
+
                var cdecl = new CCodeVariableDeclarator ("self");
-               cdecl.initializer = ccall;
+               if (chain_up) {
+                       var ccall = new CCodeFunctionCall (new CCodeIdentifier (cm.get_real_cname ()));
+                       ccall.add_argument (new CCodeIdentifier ("object_type"));
+                       cdecl.initializer = new CCodeCastExpression (ccall, "%s*".printf (cl.get_cname ()));
+               } else {
+                       var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_object_newv"));
+                       ccall.add_argument (new CCodeIdentifier ("object_type"));
+                       if (has_params) {
+                               ccall.add_argument (new CCodeConstant ("__params_it - __params"));
+                               ccall.add_argument (new CCodeConstant ("__params"));
+                       } else {
+                               ccall.add_argument (new CCodeConstant ("0"));
+                               ccall.add_argument (new CCodeConstant ("NULL"));
+                       }
+                       cdecl.initializer = ccall;
+               }
                
                var cdeclaration = new CCodeDeclaration ("%s *".printf (cl.get_cname ()));
                cdeclaration.add_declarator (cdecl);
index 3a83a255c302c8a80f8bc577a803d45ce2645497..8d6d7af74e39c0e563d0b3b699543db33bf0f5b5 100644 (file)
@@ -802,6 +802,11 @@ public class Vala.InterfaceWriter : CodeVisitor {
                }
                if (m is CreationMethod && ((CreationMethod)m).custom_return_type_cname != null) {
                        ccode_params.append_printf ("%stype = \"%s\"", separator, ((CreationMethod)m).custom_return_type_cname);
+                       separator = ", ";
+               }
+               if (m is CreationMethod && !m.has_construct_function) {
+                       ccode_params.append_printf ("%shas_construct_function = false", separator);
+                       separator = ", ";
                }
 
                if (ccode_params.len > 0) {
index 3f65ea7a6bab48bf66645dfaa926f2cec8260316..c0fa0fa4b210789080f34042ad123a595907f2f0 100644 (file)
@@ -194,6 +194,12 @@ public class Vala.Method : Member {
         */
        public bool printf_format { get; set; }
 
+       /**
+        * Specifies whether a construct function with a GType parameter is
+        * available. This is only applicable to creation methods.
+        */
+       public bool has_construct_function { get; set; default = true; }
+
        private Gee.List<FormalParameter> parameters = new ArrayList<FormalParameter> ();
        private string cname;
        private string _vfunc_name;
@@ -373,6 +379,9 @@ public class Vala.Method : Member {
                if (a.has_argument ("delegate_target_pos")) {
                        cdelegate_target_parameter_position = a.get_double ("delegate_target_pos");
                }
+               if (a.has_argument ("has_construct_function")) {
+                       has_construct_function = a.get_bool ("has_construct_function");
+               }
        }
        
        /**
index 79ff51f5d44ef1ff3213140e180169645e90ffc0..c74e2911e993d9a8fffc485573d0924867a85b52 100644 (file)
@@ -1264,6 +1264,7 @@ public class Vala.GIdlParser : CodeVisitor {
                Method m;
                if (!is_interface && (is_constructor || name.has_prefix ("new"))) {
                        m = new CreationMethod (null, name, current_source_reference);
+                       m.has_construct_function = false;
                        if (m.name == "new") {
                                m.name = null;
                        } else if (m.name.has_prefix ("new_")) {