[vala/0.46] codegen: Handle different type-symbols in visit_base_access()



commit 588f7b2347b4c12ce28e89c79e604b9d4897382a
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 3fd15f02b..e9e51e742 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -4284,7 +4284,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.data_type));
+               unowned Class? cl = expr.value_type.data_type 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.data_type);
+               }
        }
 
        public override void visit_postfix_expression (PostfixExpression expr) {
diff --git a/tests/Makefile.am b/tests/Makefile.am
index facea05e5..63ab63688 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]