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



commit 4c5a5afc456dfcf173c3096d11a6409c2a8d60f3
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.c-expected | 9 +++++----
 tests/chainup/struct-base-foo.vala       | 2 +-
 tests/chainup/struct-base.c-expected     | 1 -
 tests/chainup/struct-this-foo.c-expected | 9 +++++----
 tests/chainup/struct-this-foo.vala       | 2 +-
 tests/chainup/struct-this.c-expected     | 1 -
 vala/valamethodcall.vala                 | 1 +
 vala/valaobjectcreationexpression.vala   | 2 ++
 10 files changed, 23 insertions(+), 14 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 7f17d8d39..29b09c567 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -4977,6 +4977,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);
@@ -5033,7 +5035,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 4bc754bb7..9da195128 100644
--- a/codegen/valaccodemethodmodule.vala
+++ b/codegen/valaccodemethodmodule.vala
@@ -662,7 +662,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.c-expected b/tests/chainup/struct-base-foo.c-expected
index 635f0104e..970b3b938 100644
--- a/tests/chainup/struct-base-foo.c-expected
+++ b/tests/chainup/struct-base-foo.c-expected
@@ -85,9 +85,7 @@ foo_get_type (void)
 void
 bar_init (Bar *self)
 {
-       Foo _tmp0_ = {0};
-       memset (self, 0, sizeof (Bar));
-       foo_init_foo (&_tmp0_);
+       foo_init_foo (self);
        (*self).j = 1;
 }
 
@@ -131,9 +129,12 @@ _vala_main (void)
 {
        Bar bar = {0};
        Bar _tmp0_;
+       Bar _tmp1_;
        bar_init (&bar);
        _tmp0_ = bar;
-       _vala_assert (_tmp0_.j == 1, "bar.j == 1");
+       _vala_assert (_tmp0_.i == 1, "bar.i == 1");
+       _tmp1_ = bar;
+       _vala_assert (_tmp1_.j == 1, "bar.j == 1");
 }
 
 int
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-base.c-expected b/tests/chainup/struct-base.c-expected
index 0a89acf54..99f8f9bb6 100644
--- a/tests/chainup/struct-base.c-expected
+++ b/tests/chainup/struct-base.c-expected
@@ -85,7 +85,6 @@ foo_get_type (void)
 void
 bar_init (Bar *self)
 {
-       memset (self, 0, sizeof (Bar));
        foo_init (self);
        (*self).j = 1;
 }
diff --git a/tests/chainup/struct-this-foo.c-expected b/tests/chainup/struct-this-foo.c-expected
index 8ee82754f..9c2eb3956 100644
--- a/tests/chainup/struct-this-foo.c-expected
+++ b/tests/chainup/struct-this-foo.c-expected
@@ -37,9 +37,7 @@ static void _vala_main (void);
 void
 foo_init (Foo *self)
 {
-       Foo _tmp0_ = {0};
-       memset (self, 0, sizeof (Foo));
-       foo_init_foo (&_tmp0_);
+       foo_init_foo (self);
        (*self).j = 1;
 }
 
@@ -90,9 +88,12 @@ _vala_main (void)
 {
        Foo foo = {0};
        Foo _tmp0_;
+       Foo _tmp1_;
        foo_init (&foo);
        _tmp0_ = foo;
-       _vala_assert (_tmp0_.j == 1, "foo.j == 1");
+       _vala_assert (_tmp0_.i == 1, "foo.i == 1");
+       _tmp1_ = foo;
+       _vala_assert (_tmp1_.j == 1, "foo.j == 1");
 }
 
 int
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/tests/chainup/struct-this.c-expected b/tests/chainup/struct-this.c-expected
index 4414aaf7a..b7092ea9a 100644
--- a/tests/chainup/struct-this.c-expected
+++ b/tests/chainup/struct-this.c-expected
@@ -44,7 +44,6 @@ foo_init (Foo *self)
 void
 foo_init_bar (Foo *self)
 {
-       memset (self, 0, sizeof (Foo));
        foo_init (self);
        (*self).i = 1;
 }
diff --git a/vala/valamethodcall.vala b/vala/valamethodcall.vala
index 5b5cb50e2..36f82f93b 100644
--- a/vala/valamethodcall.vala
+++ b/vala/valamethodcall.vala
@@ -320,6 +320,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]