]> 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>
Thu, 2 Feb 2023 11:29:05 +0000 (12:29 +0100)
codegen/valaccodeassignmentmodule.vala
vala/valacodegenerator.vala

index c77a168946b56809038671c828c3cb818693eab7..2d9039832e4d0a739141f447958b30f58db2fc6e 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, SourceReference? source_reference = null,  bool initializer = false) {
                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 9452b49a30fb4e590bb0bc145311d14d7a058c87..fb27dd2c4dc1bae558544728e23cbc17bbdf2217 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, SourceReference? source_reference = null, bool initializer = false);
 }