]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Allow to use store_field() for initializations
authorRico Tzschichholz <ricotz@ubuntu.com>
Wed, 4 Jan 2023 21:47:01 +0000 (22:47 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Wed, 4 Jan 2023 21:47:01 +0000 (22:47 +0100)
codegen/valaccodeassignmentmodule.vala
codegen/valaccodebasemodule.vala
codegen/valaccodestructmodule.vala
vala/valaassignment.vala
vala/valacodegenerator.vala

index c77a168946b56809038671c828c3cb818693eab7..4f36e13eaa5709c879d4e8e8e8b865548710114a 100644 (file)
@@ -226,16 +226,22 @@ public class Vala.CCodeAssignmentModule : CCodeMemberAccessModule {
                store_value (get_parameter_cvalue (param), value, source_reference);
        }
 
-       public override void store_field (Field field, TargetValue? instance, TargetValue value, SourceReference? source_reference = null) {
+       public override void store_field (Field field, TargetValue? instance, TargetValue value, bool initializer, SourceReference? source_reference = null) {
                var lvalue = get_field_cvalue (field, instance);
                var type = lvalue.value_type;
                if (lvalue.actual_value_type != null) {
                        type = lvalue.actual_value_type;
                }
-               if ((!(field.variable_type is DelegateType) || get_ccode_delegate_target (field)) && requires_destroy (type)) {
+               if (!initializer && (!(field.variable_type is DelegateType) || get_ccode_delegate_target (field)) && requires_destroy (type)) {
                        /* unref old value */
                        ccode.add_expression (destroy_field (field, instance));
                }
+               if (initializer && instance != null && get_ccode_delegate_target (field) && get_delegate_target_cvalue (value) == null) {
+                       unowned DelegateType delegate_type = field.variable_type as DelegateType;
+                       if (delegate_type != null && delegate_type.delegate_symbol.has_target) {
+                               ((GLibValue) value).delegate_target_cvalue = get_cvalue_ (instance);
+                       }
+               }
 
                store_value (lvalue, value, source_reference);
        }
index c8301c82141af3a09bbafde45917e09a26d8034c..6d4d9cd2014563f4b2a809d67e4da790555a7829 100644 (file)
@@ -2999,7 +2999,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                                                }
                                        }
 
-                                       store_field (field, instance, expr.target_value, expr.source_reference);
+                                       store_field (field, instance, expr.target_value, false, expr.source_reference);
                                }
 
                                list.target_value = instance;
@@ -5336,7 +5336,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                                        var f = (Field) init.symbol_reference;
                                        var instance_target_type = SemanticAnalyzer.get_data_type_for_symbol ((TypeSymbol) f.parent_symbol);
                                        var typed_inst = transform_value (new GLibValue (expr.type_reference, instance, true), instance_target_type, init);
-                                       store_field (f, typed_inst, init.initializer.target_value, init.source_reference);
+                                       store_field (f, typed_inst, init.initializer.target_value, false, init.source_reference);
 
                                        var cl = f.parent_symbol as Class;
                                        if (cl != null) {
@@ -6791,7 +6791,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                                                continue;
                                        }
                                }
-                               store_field (f, dest_struct, value);
+                               store_field (f, dest_struct, value, false);
                        }
                }
 
index 19a61699943df29697b2e34d840e13e1423191a3..35779f8818d4bcb648cf85e507c7b2adb0ba4c0e 100644 (file)
@@ -339,7 +339,7 @@ public abstract class Vala.CCodeStructModule : CCodeBaseModule {
                                                continue;
                                        }
                                }
-                               store_field (f, dest_struct, value);
+                               store_field (f, dest_struct, value, false);
                        }
                }
 
index 929fa7b66cb94e7db4c16c769df9e2ccabba4345..1d62a17c0bc4346026e990b600e6e5ff6039710f 100644 (file)
@@ -530,7 +530,7 @@ public class Vala.Assignment : Expression {
                                } else if (param != null) {
                                        codegen.store_parameter (param, new_value, false, source_reference);
                                } else if (field != null) {
-                                       codegen.store_field (field, instance && ma.inner != null ? ma.inner.target_value : null, new_value, source_reference);
+                                       codegen.store_field (field, instance && ma.inner != null ? ma.inner.target_value : null, new_value, false, source_reference);
                                }
 
                                if (!(parent_node is ExpressionStatement)) {
index 9452b49a30fb4e590bb0bc145311d14d7a058c87..4377a07713f72eec66a1f7906a156a748e9e0027 100644 (file)
@@ -42,5 +42,5 @@ public abstract class Vala.CodeGenerator : CodeVisitor {
 
        public abstract TargetValue load_field (Field field, TargetValue? instance, Expression? expr = null);
 
-       public abstract void store_field (Field field, TargetValue? instance, TargetValue value, SourceReference? source_reference = null);
+       public abstract void store_field (Field field, TargetValue? instance, TargetValue value, bool initializer, SourceReference? source_reference = null);
 }