[vala/wip/issue/901: 4/4] codegen: Handle different type-symbols in visit_base_access()



commit 1d1733f907bff52f67435a563e2d9fbcc3de3f8d
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Fri Jan 24 22:48:06 2020 +0100

    codegen: Handle different type-symbols in visit_base_access()
    
    It is required to distinguish between classes, compact classes, structs
    and simple-type structs.
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/901

 codegen/valaccodebasemodule.vala               |  7 ++++-
 tests/Makefile.am                              |  3 +++
 tests/chainup/class-compact-base.vala          | 37 ++++++++++++++++++++++++++
 tests/chainup/struct-no-gtype-base.vala        | 27 +++++++++++++++++++
 tests/chainup/struct-simple-no-gtype-base.vala | 25 +++++++++++++++++
 5 files changed, 98 insertions(+), 1 deletion(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 1e0bfb4e2..31ac415d9 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -4355,7 +4355,12 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
        }
 
        public override void visit_base_access (BaseAccess expr) {
-               set_cvalue (expr, generate_instance_cast (get_this_cexpression (), 
expr.value_type.type_symbol));
+               unowned Class? cl = expr.value_type.type_symbol as Class;
+               if (cl != null && !cl.is_compact) {
+                       set_cvalue (expr, generate_instance_cast (get_this_cexpression (), cl));
+               } else {
+                       expr.target_value = load_this_parameter (expr.value_type.type_symbol);
+               }
        }
 
        public override void visit_postfix_expression (PostfixExpression expr) {
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 81cde2fe9..29c9e7a8d 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -90,6 +90,7 @@ TESTS = \
        chainup/base-struct-invalid.test \
        chainup/class-base.vala \
        chainup/class-base-foo.vala \
+       chainup/class-compact-base.vala \
        chainup/class-object.vala \
        chainup/class-this.vala \
        chainup/class-this-foo.vala \
@@ -98,6 +99,8 @@ TESTS = \
        chainup/signal-default-handler.vala \
        chainup/struct-base.vala \
        chainup/struct-base-foo.vala \
+       chainup/struct-no-gtype-base.vala \
+       chainup/struct-simple-no-gtype-base.vala \
        chainup/struct-this.vala \
        chainup/struct-this-foo.vala \
        chainup/bug791785.vala \
diff --git a/tests/chainup/class-compact-base.vala b/tests/chainup/class-compact-base.vala
new file mode 100644
index 000000000..1359c3be7
--- /dev/null
+++ b/tests/chainup/class-compact-base.vala
@@ -0,0 +1,37 @@
+[Compact]
+public class Foo {
+       public int a;
+       public int b;
+
+       public Foo () {
+               a = 23;
+       }
+
+       public int sum () {
+               return this.a + this.b;
+       }
+}
+
+public class Bar : Foo {
+       public Bar () {
+               base ();
+               this.b = 42;
+       }
+
+       public int mul () {
+               return this.a * this.b;
+       }
+
+       public int mul2 () {
+               return base.a * base.b;
+       }
+}
+
+void main () {
+       var bar = new Bar ();
+       assert (bar.a == 23);
+       assert (bar.b == 42);
+       assert (bar.sum () == 65);
+       assert (bar.mul () == 966);
+       assert (bar.mul2 () == 966);
+}
diff --git a/tests/chainup/struct-no-gtype-base.vala b/tests/chainup/struct-no-gtype-base.vala
new file mode 100644
index 000000000..c0e39b894
--- /dev/null
+++ b/tests/chainup/struct-no-gtype-base.vala
@@ -0,0 +1,27 @@
+[CCode (has_type_id = false)]
+struct Foo {
+       public int a;
+       public int b;
+
+       public int sum () {
+               return this.a + this.b;
+       }
+}
+
+[CCode (has_type_id = false)]
+struct Bar : Foo {
+       public int mul () {
+               return this.a * this.b;
+       }
+
+       public int mul2 () {
+               return base.a * base.b;
+       }
+}
+
+void main () {
+       Bar bar = { 23, 42 };
+       assert (bar.sum () == 65);
+       assert (bar.mul () == 966);
+       assert (bar.mul2 () == 966);
+}
diff --git a/tests/chainup/struct-simple-no-gtype-base.vala b/tests/chainup/struct-simple-no-gtype-base.vala
new file mode 100644
index 000000000..ee19bc223
--- /dev/null
+++ b/tests/chainup/struct-simple-no-gtype-base.vala
@@ -0,0 +1,25 @@
+[IntegerType (rank = 6, signed = true, width = 32)]
+[SimpleType]
+[CCode (has_type_id = false)]
+struct foo_t {
+       public int sum (foo_t b) {
+               return this + b;
+       }
+}
+
+[CCode (has_type_id = false)]
+struct bar_t : foo_t {
+       public int mul (bar_t b) {
+               return this * b;
+       }
+       public int mul2 (bar_t b) {
+               return base * b;
+       }
+}
+
+void main () {
+       bar_t bar = 23;
+       assert (bar.sum (42) == 65);
+       assert (bar.mul (42) == 966);
+       assert (bar.mul2 (42) == 966);
+}


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