]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Handle different type-symbols in visit_base_access() 1d1733f907bff52f67435a563e2d9fbcc3de3f8d
authorRico Tzschichholz <ricotz@ubuntu.com>
Fri, 24 Jan 2020 21:48:06 +0000 (22:48 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Sat, 25 Jan 2020 17:52:59 +0000 (18:52 +0100)
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
tests/Makefile.am
tests/chainup/class-compact-base.vala [new file with mode: 0644]
tests/chainup/struct-no-gtype-base.vala [new file with mode: 0644]
tests/chainup/struct-simple-no-gtype-base.vala [new file with mode: 0644]

index 1e0bfb4e23aafc9dd459ee5ed3d8ad740da56acc..31ac415d90c90220dbe6fb46b88cb327057682e7 100644 (file)
@@ -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) {
index 81cde2fe9e093a6829d40a034eb0191153d239a6..29c9e7a8dd903a94cb2712a86229c27e83b78f20 100644 (file)
@@ -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 (file)
index 0000000..1359c3b
--- /dev/null
@@ -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 (file)
index 0000000..c0e39b8
--- /dev/null
@@ -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 (file)
index 0000000..ee19bc2
--- /dev/null
@@ -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);
+}