[vala/0.52] codegen: Correctly handle chain up of struct creation methods



commit 4cab72e472f45743cf8ae888a54e4a8a1ec5db78
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Sat Dec 11 10:34:03 2021 +0100

    codegen: Correctly handle chain up of struct creation methods
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/1264

 codegen/valaccodebasemodule.vala       | 8 +++++++-
 codegen/valaccodemethodmodule.vala     | 2 +-
 tests/chainup/struct-base-foo.vala     | 2 +-
 tests/chainup/struct-this-foo.vala     | 2 +-
 vala/valamethodcall.vala               | 1 +
 vala/valaobjectcreationexpression.vala | 2 ++
 6 files changed, 13 insertions(+), 4 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index b8be34a62..77eff5877 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -4912,6 +4912,8 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                                } else if (param != null) {
                                        instance = get_cvalue_ (get_parameter_cvalue (param));
                                }
+                       } else if (expr.is_chainup) {
+                               instance = get_this_cexpression ();
                        } else {
                                var temp_value = create_temp_value (expr.type_reference, true, expr);
                                instance = get_cvalue_ (temp_value);
@@ -4956,7 +4958,11 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                        }
 
                        if ((st != null && !st.is_simple_type ()) && !(get_ccode_instance_pos (m) < 0)) {
-                               creation_call.add_argument (new CCodeUnaryExpression 
(CCodeUnaryOperator.ADDRESS_OF, instance));
+                               if (expr.is_chainup) {
+                                       creation_call.add_argument (instance);
+                               } else {
+                                       creation_call.add_argument (new CCodeUnaryExpression 
(CCodeUnaryOperator.ADDRESS_OF, instance));
+                               }
                        } else if (st != null && get_ccode_name (st) == "va_list") {
                                creation_call.add_argument (instance);
                                if (get_ccode_name (m) == "va_start") {
diff --git a/codegen/valaccodemethodmodule.vala b/codegen/valaccodemethodmodule.vala
index b9e56f909..0b1d69c02 100644
--- a/codegen/valaccodemethodmodule.vala
+++ b/codegen/valaccodemethodmodule.vala
@@ -663,7 +663,7 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
                                                        var vardecl = new CCodeVariableDeclarator ("self", 
default_value_for_type (creturn_type, true));
                                                        vardecl.init0 = true;
                                                        ccode.add_declaration (get_ccode_name (creturn_type), 
vardecl);
-                                               } else {
+                                               } else if (!((CreationMethod) m).chain_up) {
                                                        // memset needs string.h
                                                        cfile.add_include ("string.h");
                                                        var czero = new CCodeFunctionCall (new 
CCodeIdentifier ("memset"));
diff --git a/tests/chainup/struct-base-foo.vala b/tests/chainup/struct-base-foo.vala
index 44aeb3bb1..b54bd654d 100644
--- a/tests/chainup/struct-base-foo.vala
+++ b/tests/chainup/struct-base-foo.vala
@@ -15,6 +15,6 @@ public struct Bar : Foo {
 
 void main () {
        var bar = Bar ();
-       //FIXME assert (bar.i == 1);
+       assert (bar.i == 1);
        assert (bar.j == 1);
 }
diff --git a/tests/chainup/struct-this-foo.vala b/tests/chainup/struct-this-foo.vala
index c0e53beed..b4b835629 100644
--- a/tests/chainup/struct-this-foo.vala
+++ b/tests/chainup/struct-this-foo.vala
@@ -12,6 +12,6 @@ public struct Foo {
 
 void main () {
        var foo = Foo ();
-       //FIXME assert (foo.i == 1);
+       assert (foo.i == 1);
        assert (foo.j == 1);
 }
diff --git a/vala/valamethodcall.vala b/vala/valamethodcall.vala
index e6e3d15f9..53bb8c7e5 100644
--- a/vala/valamethodcall.vala
+++ b/vala/valamethodcall.vala
@@ -315,6 +315,7 @@ public class Vala.MethodCall : Expression, CallableExpression {
 
                        var struct_creation_expression = new ObjectCreationExpression ((MemberAccess) call, 
source_reference);
                        struct_creation_expression.struct_creation = true;
+                       struct_creation_expression.is_chainup = is_chainup;
                        foreach (Expression arg in argument_list) {
                                struct_creation_expression.add_argument (arg);
                        }
diff --git a/vala/valaobjectcreationexpression.vala b/vala/valaobjectcreationexpression.vala
index d0cebe6e4..4e6b7bfec 100644
--- a/vala/valaobjectcreationexpression.vala
+++ b/vala/valaobjectcreationexpression.vala
@@ -53,6 +53,8 @@ public class Vala.ObjectCreationExpression : Expression, CallableExpression {
 
        public bool is_yield_expression { get; set; }
 
+       public bool is_chainup { get; set; }
+
        public bool struct_creation { get; set; }
 
        private List<Expression> argument_list = new ArrayList<Expression> ();


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