]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Refactor method parameter and result code generation
authorJürg Billeter <j@bitron.ch>
Fri, 23 Jan 2009 16:28:52 +0000 (16:28 +0000)
committerJürg Billeter <juergbi@src.gnome.org>
Fri, 23 Jan 2009 16:28:52 +0000 (16:28 +0000)
2009-01-23  Jürg Billeter  <j@bitron.ch>

* ccode/valaccodefunction.vala:
* gobject/valaccodebasemodule.vala:
* gobject/valaccodemethodmodule.vala:
* gobject/valaccodemodule.vala:
* gobject/valadbusclientmodule.vala:
* gobject/valagasyncmodule.vala:
* gobject/valagobjectmodule.vala:

Refactor method parameter and result code generation

svn path=/trunk/; revision=2398

ChangeLog
ccode/valaccodefunction.vala
gobject/valaccodebasemodule.vala
gobject/valaccodemethodmodule.vala
gobject/valaccodemodule.vala
gobject/valadbusclientmodule.vala
gobject/valagasyncmodule.vala
gobject/valagobjectmodule.vala

index 182b0d9b8ac08bc98080b16fe9f4ebc2a80c89ab..8a7ff1568b828a7c479b12fb1422c79aad81bb97 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2009-01-23  Jürg Billeter  <j@bitron.ch>
+
+       * ccode/valaccodefunction.vala:
+       * gobject/valaccodebasemodule.vala:
+       * gobject/valaccodemethodmodule.vala:
+       * gobject/valaccodemodule.vala:
+       * gobject/valadbusclientmodule.vala:
+       * gobject/valagasyncmodule.vala:
+       * gobject/valagobjectmodule.vala:
+
+       Refactor method parameter and result code generation
+
 2009-01-23  Jürg Billeter  <j@bitron.ch>
 
        * gobject/valaccodearraymodule.vala:
index 38bdada30167be0ae467f840f64c6460d03e2585..1a9d2407200011f39f5332713feb8277a36b5a87 100644 (file)
@@ -1,6 +1,6 @@
 /* valaccodefunction.vala
  *
- * Copyright (C) 2006-2007  Jürg Billeter
+ * Copyright (C) 2006-2009  Jürg Billeter
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -49,9 +49,9 @@ public class Vala.CCodeFunction : CCodeNode {
 
        private Gee.List<CCodeFormalParameter> parameters = new ArrayList<CCodeFormalParameter> ();
        
-       public CCodeFunction (string name, string return_type) {
-               this.return_type = return_type;
+       public CCodeFunction (string name, string return_type = "void") {
                this.name = name;
+               this.return_type = return_type;
        }
        
        /**
index c45d7ea1e28382ed6a3ef0f8cc14d5fa817814ab..ac39f212ac6835a2def95c80a8a8ac1eff1f86dd 100644 (file)
@@ -3648,8 +3648,12 @@ public class Vala.CCodeBaseModule : CCodeModule {
                        var cnonnull = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, new CCodeIdentifier (var_name), new CCodeConstant ("NULL"));
                        ccheck.add_argument (cnonnull);
                }
-               
-               if (ret_type is VoidType) {
+
+               var cm = method_node as CreationMethod;
+               if (cm != null && cm.parent_symbol is ObjectTypeSymbol) {
+                       ccheck.call = new CCodeIdentifier ("g_return_val_if_fail");
+                       ccheck.add_argument (new CCodeConstant ("NULL"));
+               } else if (ret_type is VoidType) {
                        /* void function */
                        ccheck.call = new CCodeIdentifier ("g_return_if_fail");
                } else {
index a0d8603d38a697c1a29fdf5cb8b100ff98c087b3..55d92f5b29e28708b5b36cc2646bb65f4fff0351 100644 (file)
@@ -55,6 +55,62 @@ public class Vala.CCodeMethodModule : CCodeStructModule {
                return type;
        }
 
+       bool is_gtypeinstance_creation_method (Method m) {
+               bool result = false;
+
+               var cl = m.parent_symbol as Class;
+               if (m is CreationMethod && cl != null && !cl.is_compact) {
+                       result = true;
+               }
+
+               return result;
+       }
+
+       public virtual void generate_method_result_declaration (Method m, CCodeFunction cfunc, Map<int,CCodeFormalParameter> cparam_map, Map<int,CCodeExpression>? carg_map) {
+               var creturn_type = m.return_type;
+               if (m is CreationMethod) {
+                       var cl = current_type_symbol as Class;
+                       if (cl != null) {
+                               // object creation methods return the new object in C
+                               // in Vala they have no return type
+                               creturn_type = new ObjectType (cl);
+                       }
+               }
+               cfunc.return_type = get_creturn_type (m, creturn_type.get_cname ());
+
+               if (!m.no_array_length && m.return_type is ArrayType) {
+                       // return array length if appropriate
+                       var array_type = (ArrayType) m.return_type;
+
+                       for (int dim = 1; dim <= array_type.rank; dim++) {
+                               var cparam = new CCodeFormalParameter (head.get_array_length_cname ("result", dim), "int*");
+                               cparam_map.set (get_param_pos (m.carray_length_parameter_position + 0.01 * dim), cparam);
+                               if (carg_map != null) {
+                                       carg_map.set (get_param_pos (m.carray_length_parameter_position + 0.01 * dim), new CCodeIdentifier (cparam.name));
+                               }
+                       }
+               } else if (m.return_type is DelegateType) {
+                       // return delegate target if appropriate
+                       var deleg_type = (DelegateType) m.return_type;
+                       var d = deleg_type.delegate_symbol;
+                       if (d.has_target) {
+                               var cparam = new CCodeFormalParameter (get_delegate_target_cname ("result"), "void*");
+                               cparam_map.set (get_param_pos (m.cdelegate_target_parameter_position), cparam);
+                               if (carg_map != null) {
+                                       carg_map.set (get_param_pos (m.cdelegate_target_parameter_position), new CCodeIdentifier (cparam.name));
+                               }
+                       }
+               }
+
+               if (m.get_error_types ().size > 0) {
+                       var cparam = new CCodeFormalParameter ("error", "GError**");
+                       cparam_map.set (get_param_pos (-1), cparam);
+                       if (carg_map != null) {
+                               carg_map.set (get_param_pos (-1), new CCodeIdentifier (cparam.name));
+                       }
+               }
+       }
+
        public override void visit_method (Method m) {
                var old_type_symbol = current_type_symbol;
                var old_symbol = current_symbol;
@@ -76,7 +132,6 @@ public class Vala.CCodeMethodModule : CCodeStructModule {
                variable_name_map = new HashMap<string,string> (str_hash, str_equal);
                current_try = null;
 
-               bool in_gtypeinstance_creation_method = false;
                bool in_gobject_creation_method = false;
                bool in_fundamental_creation_method = false;
 
@@ -86,17 +141,12 @@ public class Vala.CCodeMethodModule : CCodeStructModule {
                        in_creation_method = true;
                        var cl = current_type_symbol as Class;
                        if (cl != null && !cl.is_compact) {
-                               in_gtypeinstance_creation_method = true;
                                if (cl.base_class == null) {
                                        in_fundamental_creation_method = true;
                                } else if (cl.is_subtype_of (gobject_type)) {
                                        in_gobject_creation_method = true;
                                }
                        }
-
-                       if (cl != null) {
-                               current_return_type = new ObjectType (cl);
-                       }
                } else {
                        in_creation_method = false;
                }
@@ -170,7 +220,7 @@ public class Vala.CCodeMethodModule : CCodeStructModule {
                variable_name_map = old_variable_name_map;
                current_try = old_try;
 
-               function = new CCodeFunction (m.get_real_cname (), get_creturn_type (m, creturn_type.get_cname ()));
+               function = new CCodeFunction (m.get_real_cname ());
                m.ccodenode = function;
 
                if (m.is_inline) {
@@ -187,7 +237,7 @@ public class Vala.CCodeMethodModule : CCodeStructModule {
                        type_struct.add_declaration (vdecl);
                }
 
-               generate_cparameters (m, creturn_type, in_gtypeinstance_creation_method, cparam_map, function, vdeclarator);
+               generate_cparameters (m, cparam_map, function, vdeclarator);
 
                bool visible = !m.is_internal_symbol ();
 
@@ -362,7 +412,7 @@ public class Vala.CCodeMethodModule : CCodeStructModule {
                                                        param_name = new CCodeIdentifier ("%s_destroy_func".printf (type_param.name.down ()));
                                                        cinit.append (new CCodeExpressionStatement (get_construct_property_assignment (prop_name, new PointerType (new VoidType ()), param_name)));
                                                }
-                                       } else if (in_gtypeinstance_creation_method) {
+                                       } else if (is_gtypeinstance_creation_method (m)) {
                                                var cl = (Class) m.parent_symbol;
                                                var cdeclaration = new CCodeDeclaration (cl.get_cname () + "*");
                                                var cdecl = new CCodeVariableDeclarator ("self");
@@ -522,7 +572,7 @@ public class Vala.CCodeMethodModule : CCodeStructModule {
                }
        }
 
-       public override void generate_cparameters (Method m, DataType creturn_type, bool in_gtypeinstance_creation_method, Map<int,CCodeFormalParameter> cparam_map, CCodeFunction func, CCodeFunctionDeclarator? vdeclarator = null, Map<int,CCodeExpression>? carg_map = null, CCodeFunctionCall? vcall = null, int direction = 3) {
+       public override void generate_cparameters (Method m, Map<int,CCodeFormalParameter> cparam_map, CCodeFunction func, CCodeFunctionDeclarator? vdeclarator = null, Map<int,CCodeExpression>? carg_map = null, CCodeFunctionCall? vcall = null, int direction = 3) {
                if (m.parent_symbol is Class && m is CreationMethod) {
                        var cl = (Class) m.parent_symbol;
                        if (!cl.is_compact && vcall == null) {
@@ -566,7 +616,7 @@ public class Vala.CCodeMethodModule : CCodeStructModule {
                        cparam_map.set (get_param_pos (m.cinstance_parameter_position), class_param);
                }
 
-               if (in_gtypeinstance_creation_method) {
+               if (is_gtypeinstance_creation_method (m)) {
                        // memory management for generic types
                        int type_param_index = 0;
                        foreach (TypeParameter type_param in current_class.get_type_parameters ()) {
@@ -599,37 +649,7 @@ public class Vala.CCodeMethodModule : CCodeStructModule {
                }
 
                if ((direction & 2) != 0) {
-                       if (!m.no_array_length && creturn_type is ArrayType) {
-                               // return array length if appropriate
-                               var array_type = (ArrayType) creturn_type;
-
-                               for (int dim = 1; dim <= array_type.rank; dim++) {
-                                       var cparam = new CCodeFormalParameter (head.get_array_length_cname ("result", dim), "int*");
-                                       cparam_map.set (get_param_pos (m.carray_length_parameter_position + 0.01 * dim), cparam);
-                                       if (carg_map != null) {
-                                               carg_map.set (get_param_pos (m.carray_length_parameter_position + 0.01 * dim), new CCodeIdentifier (cparam.name));
-                                       }
-                               }
-                       } else if (creturn_type is DelegateType) {
-                               // return delegate target if appropriate
-                               var deleg_type = (DelegateType) creturn_type;
-                               var d = deleg_type.delegate_symbol;
-                               if (d.has_target) {
-                                       var cparam = new CCodeFormalParameter (get_delegate_target_cname ("result"), "void*");
-                                       cparam_map.set (get_param_pos (m.cdelegate_target_parameter_position), cparam);
-                                       if (carg_map != null) {
-                                               carg_map.set (get_param_pos (m.cdelegate_target_parameter_position), new CCodeIdentifier (cparam.name));
-                                       }
-                               }
-                       }
-
-                       if (m.get_error_types ().size > 0) {
-                               var cparam = new CCodeFormalParameter ("error", "GError**");
-                               cparam_map.set (get_param_pos (-1), cparam);
-                               if (carg_map != null) {
-                                       carg_map.set (get_param_pos (-1), new CCodeIdentifier (cparam.name));
-                               }
-                       }
+                       generate_method_result_declaration (m, func, cparam_map, carg_map);
                }
 
                // append C parameters in the right order
@@ -659,7 +679,7 @@ public class Vala.CCodeMethodModule : CCodeStructModule {
        public void generate_vfunc (Method m, DataType return_type, Map<int,CCodeFormalParameter> cparam_map, Map<int,CCodeExpression> carg_map, string suffix = "", int direction = 3) {
                bool visible = !m.is_internal_symbol ();
 
-               var vfunc = new CCodeFunction (m.get_cname () + suffix, return_type.get_cname ());
+               var vfunc = new CCodeFunction (m.get_cname () + suffix);
                vfunc.line = function.line;
 
                var vblock = new CCodeBlock ();
@@ -686,7 +706,7 @@ public class Vala.CCodeMethodModule : CCodeStructModule {
                var vcall = new CCodeFunctionCall (new CCodeMemberAccess.pointer (vcast, m.vfunc_name + suffix));
                carg_map.set (get_param_pos (m.cinstance_parameter_position), new CCodeIdentifier ("self"));
 
-               generate_cparameters (m, return_type, false, cparam_map, vfunc, null, carg_map, vcall, direction);
+               generate_cparameters (m, cparam_map, vfunc, null, carg_map, vcall, direction);
 
                CCodeStatement cstmt;
                if (return_type is VoidType) {
@@ -849,7 +869,7 @@ public class Vala.CCodeMethodModule : CCodeStructModule {
                }
 
                if (current_type_symbol is Class && !current_class.is_compact) {
-                       var vfunc = new CCodeFunction (m.get_cname (), creturn_type.get_cname ());
+                       var vfunc = new CCodeFunction (m.get_cname ());
                        vfunc.line = function.line;
 
                        var cparam_map = new HashMap<int,CCodeFormalParameter> (direct_hash, direct_equal);
@@ -860,7 +880,7 @@ public class Vala.CCodeMethodModule : CCodeStructModule {
                        var vcall = new CCodeFunctionCall (new CCodeIdentifier (m.get_real_cname ()));
                        vcall.add_argument (new CCodeIdentifier (current_class.get_type_id ()));
 
-                       generate_cparameters (m, creturn_type, true, cparam_map, vfunc, null, carg_map, vcall);
+                       generate_cparameters (m, cparam_map, vfunc, null, carg_map, vcall);
                        CCodeStatement cstmt = new CCodeReturnStatement (vcall);
                        cstmt.line = vfunc.line;
                        vblock.add_statement (cstmt);
index 5b49cf8737daf76aba9c06592957bdc05d404d13..37f30a42830e374d7b014f3582528f2dacf10c9b 100644 (file)
@@ -332,8 +332,8 @@ public abstract class Vala.CCodeModule {
                next.visit_assignment (a);
        }
 
-       public virtual void generate_cparameters (Method m, DataType creturn_type, bool in_gtypeinstance_creation_method, Map<int,CCodeFormalParameter> cparam_map, CCodeFunction func, CCodeFunctionDeclarator? vdeclarator = null, Map<int,CCodeExpression>? carg_map = null, CCodeFunctionCall? vcall = null, int direction = 3) {
-               next.generate_cparameters (m, creturn_type, in_gtypeinstance_creation_method, cparam_map, func, vdeclarator, carg_map, vcall, direction);
+       public virtual void generate_cparameters (Method m, Map<int,CCodeFormalParameter> cparam_map, CCodeFunction func, CCodeFunctionDeclarator? vdeclarator = null, Map<int,CCodeExpression>? carg_map = null, CCodeFunctionCall? vcall = null, int direction = 3) {
+               next.generate_cparameters (m, cparam_map, func, vdeclarator, carg_map, vcall, direction);
        }
 
        public virtual string? get_custom_creturn_type (Method m) {
index 313ac95541d67204b08ed296d5f2e327321d2505..f5ca985e472bdbb9954ff254b9dfc217c0d66afb 100644 (file)
@@ -48,11 +48,11 @@ public class Vala.DBusClientModule : DBusModule {
        public override void generate_dynamic_method_wrapper (DynamicMethod method) {
                var dynamic_method = (DynamicMethod) method;
 
-               var func = new CCodeFunction (method.get_cname (), method.return_type.get_cname ());
+               var func = new CCodeFunction (method.get_cname ());
 
                var cparam_map = new HashMap<int,CCodeFormalParameter> (direct_hash, direct_equal);
 
-               generate_cparameters (method, method.return_type, false, cparam_map, func);
+               generate_cparameters (method, cparam_map, func);
 
                var block = new CCodeBlock ();
                if (dynamic_method.dynamic_type.data_type == dbus_object_type) {
@@ -1039,12 +1039,12 @@ public class Vala.DBusClientModule : DBusModule {
 
                CCodeDeclaration cdecl;
 
-               var function = new CCodeFunction (proxy_name, m.return_type.get_cname ());
+               var function = new CCodeFunction (proxy_name);
                function.modifiers = CCodeModifiers.STATIC;
 
                var cparam_map = new HashMap<int,CCodeFormalParameter> (direct_hash, direct_equal);
 
-               generate_cparameters (m, m.return_type, false, cparam_map, function);
+               generate_cparameters (m, cparam_map, function);
 
                var block = new CCodeBlock ();
                var prefragment = new CCodeFragment ();
@@ -1140,7 +1140,7 @@ public class Vala.DBusClientModule : DBusModule {
                cparam_map.set (get_param_pos (-1), new CCodeFormalParameter ("callback", "GAsyncReadyCallback"));
                cparam_map.set (get_param_pos (-0.9), new CCodeFormalParameter ("user_data", "gpointer"));
 
-               generate_cparameters (m, m.return_type, false, cparam_map, function, null, null, null, 1);
+               generate_cparameters (m, cparam_map, function, null, null, null, 1);
 
                var block = new CCodeBlock ();
                var prefragment = new CCodeFragment ();
@@ -1261,14 +1261,14 @@ public class Vala.DBusClientModule : DBusModule {
 
                CCodeDeclaration cdecl;
 
-               var function = new CCodeFunction (proxy_name, m.return_type.get_cname ());
+               var function = new CCodeFunction (proxy_name);
                function.modifiers = CCodeModifiers.STATIC;
 
                var cparam_map = new HashMap<int,CCodeFormalParameter> (direct_hash, direct_equal);
 
                cparam_map.set (get_param_pos (0.1), new CCodeFormalParameter ("res", "GAsyncResult*"));
 
-               generate_cparameters (m, m.return_type, false, cparam_map, function, null, null, null, 2);
+               generate_cparameters (m, cparam_map, function, null, null, null, 2);
 
                var block = new CCodeBlock ();
                var prefragment = new CCodeFragment ();
index 9504d61582677612601d75fc88b1989031776d9d..f356f522fa7a14fdfa9c5445cfb85474e1c400cb 100644 (file)
@@ -1,6 +1,6 @@
 /* valagasyncmodule.vala
  *
- * Copyright (C) 2008  Jürg Billeter
+ * Copyright (C) 2008-2009  Jürg Billeter
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -110,7 +110,7 @@ public class Vala.GAsyncModule : GSignalModule {
                        type_struct.add_declaration (vdecl);
                }
 
-               generate_cparameters (m, creturn_type, false, cparam_map, asyncfunc, vdeclarator, null, null, 1);
+               generate_cparameters (m, cparam_map, asyncfunc, vdeclarator, null, null, 1);
 
                if (!m.is_abstract) {
                        if (visible && m.base_method == null && m.base_interface_method == null) {
@@ -126,7 +126,7 @@ public class Vala.GAsyncModule : GSignalModule {
                }
 
                // generate finish function
-               var finishfunc = new CCodeFunction (m.get_real_cname () + "_finish", creturn_type.get_cname ());
+               var finishfunc = new CCodeFunction (m.get_real_cname () + "_finish");
                finishfunc.line = function.line;
 
                cparam_map = new HashMap<int,CCodeFormalParameter> (direct_hash, direct_equal);
@@ -142,7 +142,7 @@ public class Vala.GAsyncModule : GSignalModule {
                        type_struct.add_declaration (vdecl);
                }
 
-               generate_cparameters (m, creturn_type, false, cparam_map, finishfunc, vdeclarator, null, null, 2);
+               generate_cparameters (m, cparam_map, finishfunc, vdeclarator, null, null, 2);
 
                if (!m.is_abstract) {
                        if (visible && m.base_method == null && m.base_interface_method == null) {
index e06b589095a32acc6c08b7b43029e817b19a58d6..e572cf968b2b392bf2d73962427a689711dabbb7 100644 (file)
@@ -1,6 +1,7 @@
 /* valagobjectmodule.vala
  *
- * Copyright (C) 2006-2008  Jürg Billeter, Raffaele Sandrini
+ * Copyright (C) 2006-2009  Jürg Billeter
+ * Copyright (C) 2006-2008  Raffaele Sandrini
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -21,7 +22,7 @@
  *     Raffaele Sandrini <raffaele@sandrini.ch>
  */
 
-using GLib;
+using Gee;
 
 public class Vala.GObjectModule : GTypeModule {
        int dynamic_property_id;
@@ -31,6 +32,18 @@ public class Vala.GObjectModule : GTypeModule {
                base (codegen, next);
        }
 
+       public override void generate_parameter (FormalParameter param, Map<int,CCodeFormalParameter> cparam_map, Map<int,CCodeExpression>? carg_map) {
+               if (!(param.parameter_type is ObjectType)) {
+                       base.generate_parameter (param, cparam_map, carg_map);
+                       return;
+               }
+
+               cparam_map.set (get_param_pos (param.cparameter_position), (CCodeFormalParameter) param.ccodenode);
+               if (carg_map != null) {
+                       carg_map.set (get_param_pos (param.cparameter_position), new CCodeIdentifier (param.name));
+               }
+       }
+
        public override void visit_class (Class cl) {
                var old_symbol = current_symbol;
                var old_type_symbol = current_type_symbol;