[vala/0.42] codegen: Add destroy function for GLib.Array



commit 4f66d15510aafb5f63f8c33b5b02eb550773bd0c
Author: Corentin Noël <corentin elementary io>
Date:   Fri Nov 16 08:19:55 2018 +0100

    codegen: Add destroy function for GLib.Array
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/572

 codegen/valaccodebasemodule.vala | 45 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 44 insertions(+), 1 deletion(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index e1b75ab3c..db822e136 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -3080,6 +3080,37 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                return destroy_func;
        }
 
+       protected string generate_destroy_function_content_of_wrapper (DataType type) {
+               // g_array_set_clear_func has a specific GDestroyNotify where the content of an element is 
given
+               string destroy_func = "_vala_%s_free_function_content_of".printf (get_ccode_name 
(type.data_type));
+
+               if (!add_wrapper (destroy_func)) {
+                       // wrapper already defined
+                       return destroy_func;
+               }
+
+               var function = new CCodeFunction (destroy_func, "void");
+               function.modifiers = CCodeModifiers.STATIC;
+               function.add_parameter (new CCodeParameter ("data", "gpointer"));
+               push_function (function);
+
+               ccode.add_declaration (get_ccode_name (type), new CCodeVariableDeclarator ("self"));
+               var cast = new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, new 
CCodeCastExpression (new CCodeIdentifier ("data"), get_ccode_name (type) + "*"));
+               ccode.add_assignment (new CCodeIdentifier ("self"), cast);
+
+               var free_call = new CCodeFunctionCall (get_destroy0_func_expression (type));
+               free_call.add_argument (new CCodeIdentifier ("self"));
+
+               ccode.add_expression (free_call);
+
+               pop_function ();
+
+               cfile.add_function_declaration (function);
+               cfile.add_function (function);
+
+               return destroy_func;
+       }
+
        protected string generate_free_func_wrapper (DataType type) {
                string destroy_func = "_vala_%s_free".printf (get_ccode_name (type.data_type));
 
@@ -5022,10 +5053,10 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                        ccode.add_assignment (get_cvalue_ (temp_value), creation_expr);
                        expr.target_value = temp_value;
 
+                       var cl = expr.type_reference.data_type as Class;
                        if (context.gobject_tracing) {
                                // GObject creation tracing enabled
 
-                               var cl = expr.type_reference.data_type as Class;
                                if (cl != null && cl.is_subtype_of (gobject_type)) {
                                        // creating GObject
 
@@ -5050,6 +5081,18 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                                        ccode.close ();
                                }
                        }
+
+                       // create a special GDestroyNotify for created GArray and set with 
g_array_set_clear_func (since glib 2.32)
+                       if (cl == garray_type) {
+                               var type_arg = expr.type_reference.get_type_arguments ().get (0);
+                               if (requires_destroy (type_arg)) {
+                                       var free_wrapper = generate_destroy_function_content_of_wrapper 
(type_arg);
+                                       var clear_func = new CCodeFunctionCall (new CCodeIdentifier 
("g_array_set_clear_func"));
+                                       clear_func.add_argument (get_cvalue_ (expr.target_value));
+                                       clear_func.add_argument (new CCodeIdentifier (free_wrapper));
+                                       ccode.add_expression (clear_func);
+                               }
+                       }
                }
 
                ((GLibValue) expr.target_value).lvalue = true;


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