[vala/0.36] codegen: Resolve generics in sizeof-expression of parameter initializer



commit da21661320f683e2c93960d87b913d7ff0e6ea2f
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 6fbf66e46..30fb2dc3d 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -359,6 +359,7 @@ TESTS = \
        asynchronous/variadic-invalid.test \
        asynchronous/variadic-invalid-2.test \
        asynchronous/yield.vala \
+       generics/parameter-sizeof-initializer.vala \
        generics/bug694765-1.vala \
        generics/bug694765-2.vala \
        generics/bug694765-3.vala \
diff --git a/tests/generics/parameter-sizeof-initializer.vala 
b/tests/generics/parameter-sizeof-initializer.vala
new file mode 100644
index 000000000..1dcc56e1a
--- /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<uint32> (sizeof (uint32));
+               foo.bar (4);
+       }
+       {
+               var foo = new Foo<int16> (sizeof (int16));
+               foo.bar (2);
+       }
+       {
+               var foo = new Foo<uint8> (sizeof (uint8));
+               foo.bar (1);
+       }
+}
diff --git a/vala/valamethodcall.vala b/vala/valamethodcall.vala
index 0be512675..f7c6ac12d 100644
--- a/vala/valamethodcall.vala
+++ b/vala/valamethodcall.vala
@@ -612,6 +612,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 0077c9193..17e38aeae 100644
--- a/vala/valaobjectcreationexpression.vala
+++ b/vala/valaobjectcreationexpression.vala
@@ -485,6 +485,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]