[vala/wip/limited-generics: 1/2] codegen: Resolve generics in sizeof-expression of parameter initializer



commit 11cfdc0346a3f5e9bf33f2793cee162442daff1e
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Mon Mar 11 10:31:10 2019 +0100

    codegen: Resolve generics in sizeof-expression of parameter initializer

 tests/Makefile.am                                |  1 +
 tests/generics/parameter-sizeof-initializer.vala | 28 ++++++++++++++++++++++++
 vala/valamethodcall.vala                         |  9 ++++++++
 vala/valaobjectcreationexpression.vala           |  9 ++++++++
 4 files changed, 47 insertions(+)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 6c1653bda..1f1306985 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -442,6 +442,7 @@ TESTS = \
        asynchronous/yield.vala \
        generics/constructor-chain-up.vala \
        generics/inference-static-function.vala \
+       generics/parameter-sizeof-initializer.vala \
        generics/bug640330.test \
        generics/bug640330.vala \
        generics/bug694765-1.vala \
diff --git a/tests/generics/parameter-sizeof-initializer.vala 
b/tests/generics/parameter-sizeof-initializer.vala
new file mode 100644
index 000000000..237bdf972
--- /dev/null
+++ b/tests/generics/parameter-sizeof-initializer.vala
@@ -0,0 +1,28 @@
+[Compact]
+class Foo<G> {
+       public Foo (ulong real_foo, ulong foo = sizeof (G)) {
+               assert (foo == real_foo);
+       }
+
+       public void bar (ulong real_foo, ulong foo = sizeof (G)) {
+               assert (foo == real_foo);
+       }
+}
+
+void main () {
+       {
+               var garray = new GLib.Array<uint32> ();
+       }
+       {
+               var foo = new Foo<int64> (sizeof (int64));
+               foo.bar (8);
+       }
+       {
+               var foo = new Foo<uint32> (sizeof (uint32));
+               foo.bar (4);
+       }
+       {
+               var foo = new Foo<uint8> (sizeof (uint8));
+               foo.bar (1);
+       }
+}
diff --git a/vala/valamethodcall.vala b/vala/valamethodcall.vala
index 80d0d772f..c1b034fa3 100644
--- a/vala/valamethodcall.vala
+++ b/vala/valamethodcall.vala
@@ -632,6 +632,15 @@ public class Vala.MethodCall : Expression {
                        return false;
                }
 
+               //Resolve possible generic-type in SizeofExpression used as parameter default-value
+               foreach (Expression arg in get_argument_list ()) {
+                       unowned SizeofExpression sizeof_expr = arg as SizeofExpression;
+                       if (sizeof_expr != null && sizeof_expr.type_reference is GenericType) {
+                               var sizeof_type = sizeof_expr.type_reference.get_actual_type 
(target_object_type, method_type_args, this);
+                               replace_expression (arg, new SizeofExpression (sizeof_type, 
source_reference));
+                       }
+               }
+
                /* Check for constructv chain up */
                if (base_cm != null && base_cm.is_variadic () && args.size == base_cm.get_parameters ().size) 
{
                        var this_last_arg = args[args.size-1];
diff --git a/vala/valaobjectcreationexpression.vala b/vala/valaobjectcreationexpression.vala
index 0b6a29391..5f4fce753 100644
--- a/vala/valaobjectcreationexpression.vala
+++ b/vala/valaobjectcreationexpression.vala
@@ -477,6 +477,15 @@ public class Vala.ObjectCreationExpression : Expression {
                        }
                }
 
+               //Resolve possible generic-type in SizeofExpression used as parameter default-value
+               foreach (Expression arg in get_argument_list ()) {
+                       unowned SizeofExpression sizeof_expr = arg as SizeofExpression;
+                       if (sizeof_expr != null && sizeof_expr.type_reference is GenericType) {
+                               var sizeof_type = sizeof_expr.type_reference.get_actual_type (type_reference, 
type_reference.get_type_arguments (), this);
+                               replace_expression (arg, new SizeofExpression (sizeof_type, 
source_reference));
+                       }
+               }
+
                foreach (MemberInitializer init in get_object_initializer ()) {
                        context.analyzer.visit_member_initializer (init, type_reference);
                }


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