Add basic support for owned delegates, fixes bug 533484
authorJürg Billeter <j@bitron.ch>
Mon, 2 Jun 2008 19:23:34 +0000 (19:23 +0000)
committerJürg Billeter <juergbi@src.gnome.org>
Mon, 2 Jun 2008 19:23:34 +0000 (19:23 +0000)
2008-06-02  Jürg Billeter  <j@bitron.ch>

* gobject/valaccodegenerator.vala:
* gobject/valaccodeinvocationexpressionbinding.vala:
* gobject/valaccodemethodbinding.vala:

Add basic support for owned delegates, fixes bug 533484

svn path=/trunk/; revision=1544

ChangeLog
gobject/valaccodegenerator.vala
gobject/valaccodeinvocationexpressionbinding.vala
gobject/valaccodemethodbinding.vala

index 0fe5c13..f14dd09 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2008-06-02  Jürg Billeter  <j@bitron.ch>
 
+       * gobject/valaccodegenerator.vala:
+       * gobject/valaccodeinvocationexpressionbinding.vala:
+       * gobject/valaccodemethodbinding.vala:
+
+       Add basic support for owned delegates, fixes bug 533484
+
+2008-06-02  Jürg Billeter  <j@bitron.ch>
+
        * vapi/gmodule-2.0.vapi:
        * vapi/sdl-gfx.vapi:
        * vapi/sdl-mixer.vapi:
index 91076f6..08ad198 100644 (file)
@@ -1274,7 +1274,7 @@ public class Vala.CCodeGenerator : CodeGenerator {
                }
        }
 
-       private CCodeExpression? get_dup_func_expression (DataType type, SourceReference? source_reference) {
+       public CCodeExpression? get_dup_func_expression (DataType type, SourceReference? source_reference) {
                var cl = type.data_type as Class;
                if (type.data_type != null) {
                        string dup_function;
@@ -1349,7 +1349,7 @@ public class Vala.CCodeGenerator : CodeGenerator {
                return dup_func;
        }
 
-       private CCodeExpression? get_destroy_func_expression (DataType type) {
+       public CCodeExpression? get_destroy_func_expression (DataType type) {
                if (type.data_type == glist_type || type.data_type == gslist_type) {
                        // create wrapper function to free list elements if necessary
 
@@ -2817,6 +2817,10 @@ public class Vala.CCodeGenerator : CodeGenerator {
                return new CCodeConstant ("NULL");
        }
 
+       public string get_delegate_target_destroy_notify_cname (string delegate_cname) {
+               return "%s_target_destroy_notify".printf (delegate_cname);
+       }
+
        public override void visit_element_access (ElementAccess expr) {
                code_binding (expr).emit ();
        }
index 1d52c63..c500405 100644 (file)
@@ -170,7 +170,24 @@ public class Vala.CCodeInvocationExpressionBinding : CCodeExpressionBinding {
                                                var deleg_type = (DelegateType) param.parameter_type;
                                                var d = deleg_type.delegate_symbol;
                                                if (d.has_target) {
-                                                       carg_map.set (codegen.get_param_pos (param.cdelegate_target_parameter_position), codegen.get_delegate_target_cexpression (arg));
+                                                       var delegate_target = codegen.get_delegate_target_cexpression (arg);
+                                                       if (deleg_type.value_owned) {
+                                                               CCodeExpression delegate_target_destroy_notify;
+                                                               var delegate_method = arg.symbol_reference as Method;
+                                                               var ma = arg as MemberAccess;
+                                                               if (delegate_method != null && delegate_method.binding == MemberBinding.INSTANCE
+                                                                   && ma.inner != null && ma.inner.value_type.data_type != null
+                                                                   && ma.inner.value_type.data_type.is_reference_counting ()) {
+                                                                       var ref_call = new CCodeFunctionCall (codegen.get_dup_func_expression (ma.inner.value_type, arg.source_reference));
+                                                                       ref_call.add_argument (delegate_target);
+                                                                       delegate_target = ref_call;
+                                                                       delegate_target_destroy_notify = codegen.get_destroy_func_expression (ma.inner.value_type);
+                                                               } else {
+                                                                       delegate_target_destroy_notify = new CCodeConstant ("NULL");
+                                                               }
+                                                               carg_map.set (codegen.get_param_pos (param.cdelegate_target_parameter_position + 0.01), delegate_target_destroy_notify);
+                                                       }
+                                                       carg_map.set (codegen.get_param_pos (param.cdelegate_target_parameter_position), delegate_target);
                                                        multiple_cargs = true;
                                                }
                                        } else if (param.parameter_type is MethodType) {
index 538ba58..b113591 100644 (file)
@@ -436,6 +436,11 @@ public class Vala.CCodeMethodBinding : CCodeBinding {
                                                var cparam = new CCodeFormalParameter (codegen.get_delegate_target_cname (param.name), "void*");
                                                cparam_map.set (codegen.get_param_pos (param.cdelegate_target_parameter_position), cparam);
                                                carg_map.set (codegen.get_param_pos (param.cdelegate_target_parameter_position), new CCodeIdentifier (cparam.name));
+                                               if (deleg_type.value_owned) {
+                                                       cparam = new CCodeFormalParameter (codegen.get_delegate_target_destroy_notify_cname (param.name), "GDestroyNotify");
+                                                       cparam_map.set (codegen.get_param_pos (param.cdelegate_target_parameter_position + 0.01), cparam);
+                                                       carg_map.set (codegen.get_param_pos (param.cdelegate_target_parameter_position + 0.01), new CCodeIdentifier (cparam.name));
+                                               }
                                        }
                                }
                        }
@@ -618,6 +623,10 @@ public class Vala.CCodeMethodBinding : CCodeBinding {
                                if (d.has_target) {
                                        var cparam = new CCodeFormalParameter (codegen.get_delegate_target_cname (param.name), "void*");
                                        cparam_map.set (codegen.get_param_pos (param.cdelegate_target_parameter_position), cparam);
+                                       if (deleg_type.value_owned) {
+                                               cparam = new CCodeFormalParameter (codegen.get_delegate_target_destroy_notify_cname (param.name), "GDestroyNotify");
+                                               cparam_map.set (codegen.get_param_pos (param.cdelegate_target_parameter_position + 0.01), cparam);
+                                       }
                                }
                        } else if (param.parameter_type is MethodType) {
                                var cparam = new CCodeFormalParameter (codegen.get_delegate_target_cname (param.name), "void*");