[vala/tintou/ptr-array-foreach] vala: Add foreach statement support for GPtrArray




commit 4e742c747976d881c2e6e81794b3dabfea642de2
Author: Corentin Noël <corentin noel collabora com>
Date:   Thu Oct 15 10:35:25 2020 +0200

    vala: Add foreach statement support for GPtrArray
    
    It is now possible to use foreach with a GLib.PtrArray or GLib.GenericArray type

 codegen/valaccodebasemodule.vala                   |  5 ++-
 codegen/valaccodecontrolflowmodule.vala            | 40 ++++++++++++++++++++++
 tests/Makefile.am                                  |  1 +
 tests/basic-types/gptrarray.vala                   |  7 ++++
 tests/control-flow/foreach.vala                    | 36 +++++++++++++++++++
 tests/control-flow/gptrarray-foreach-variable.test |  7 ++++
 vala/valaforeachstatement.vala                     |  2 +-
 vala/valasemanticanalyzer.vala                     |  4 +++
 8 files changed, 100 insertions(+), 2 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index b11992e17..09fca17b7 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -337,6 +337,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
        public TypeSymbol garray_type;
        public TypeSymbol gbytearray_type;
        public TypeSymbol gptrarray_type;
+       public TypeSymbol genericarray_type;
        public TypeSymbol gthreadpool_type;
        public DataType gquark_type;
        public Struct gvalue_type;
@@ -502,6 +503,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                        garray_type = (TypeSymbol) glib_ns.scope.lookup ("Array");
                        gbytearray_type = (TypeSymbol) glib_ns.scope.lookup ("ByteArray");
                        gptrarray_type = (TypeSymbol) glib_ns.scope.lookup ("PtrArray");
+                       genericarray_type = (TypeSymbol) glib_ns.scope.lookup ("GenericArray");
                        gthreadpool_type = (TypeSymbol) glib_ns.scope.lookup ("ThreadPool");
 
                        gerror = (Class) root_symbol.scope.lookup ("GLib").scope.lookup ("Error");
@@ -3760,7 +3762,8 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                    (type.type_symbol.is_subtype_of (gstringbuilder_type)
                     || type.type_symbol.is_subtype_of (garray_type)
                     || type.type_symbol.is_subtype_of (gbytearray_type)
-                    || type.type_symbol.is_subtype_of (gptrarray_type))) {
+                    || type.type_symbol.is_subtype_of (gptrarray_type)
+                    || type.type_symbol.is_subtype_of (genericarray_type))) {
                        ccall.add_argument (new CCodeConstant ("TRUE"));
                } else if (context.profile == Profile.GOBJECT
                    && type.type_symbol == gthreadpool_type) {
diff --git a/codegen/valaccodecontrolflowmodule.vala b/codegen/valaccodecontrolflowmodule.vala
index df31fcef3..7736db71d 100644
--- a/codegen/valaccodecontrolflowmodule.vala
+++ b/codegen/valaccodecontrolflowmodule.vala
@@ -308,6 +308,42 @@ public abstract class Vala.CCodeControlFlowModule : CCodeMethodModule {
 
                        stmt.body.emit (this);
 
+                       ccode.close ();
+               } else if (stmt.collection.value_type.compatible (new ObjectType ((Class) gptrarray_type)) || 
stmt.collection.value_type.compatible (new ObjectType ((Class) genericarray_type))) {
+                       // iterating over a GPtrArray
+
+                       var iterator_variable = new LocalVariable (uint_type.copy (), "%s_index".printf 
(stmt.variable_name));
+                       visit_local_variable (iterator_variable);
+                       var arr_index = get_variable_cname (get_local_cname (iterator_variable));
+
+                       var ccond = new CCodeBinaryExpression (CCodeBinaryOperator.LESS_THAN, 
get_variable_cexpression (arr_index), new CCodeMemberAccess.pointer (get_variable_cexpression 
(get_local_cname (collection_backup)), "len"));
+
+                       ccode.open_for (new CCodeAssignment (get_variable_cexpression (arr_index), new 
CCodeConstant ("0")),
+                                          ccond,
+                                          new CCodeAssignment (get_variable_cexpression (arr_index), new 
CCodeBinaryExpression (CCodeBinaryOperator.PLUS, get_variable_cexpression (arr_index), new CCodeConstant 
("1"))));
+
+                       var get_item = new CCodeFunctionCall (new CCodeIdentifier ("g_ptr_array_index"));
+                       get_item.add_argument (get_variable_cexpression (get_local_cname 
(collection_backup)));
+                       get_item.add_argument (get_variable_cexpression (arr_index));
+
+                       CCodeExpression element_expr = get_item;
+
+                       if (collection_type.get_type_arguments ().size != 1) {
+                               Report.error (stmt.source_reference, "internal error: missing generic type 
argument");
+                               stmt.error = true;
+                               return;
+                       }
+
+                       var element_data_type = collection_type.get_type_arguments ().get (0).copy ();
+                       element_data_type.value_owned = false;
+                       element_expr = convert_from_generic_pointer (element_expr, element_data_type);
+                       element_expr = get_cvalue_ (transform_value (new GLibValue (element_data_type, 
element_expr), stmt.type_reference, stmt));
+
+                       visit_local_variable (stmt.element_variable);
+                       ccode.add_assignment (get_variable_cexpression (get_local_cname 
(stmt.element_variable)), element_expr);
+
+                       stmt.body.emit (this);
+
                        ccode.close ();
                } else if (stmt.collection.value_type.compatible (new ObjectType (gvaluearray_type))) {
                        // iterating over a GValueArray
@@ -338,6 +374,10 @@ public abstract class Vala.CCodeControlFlowModule : CCodeMethodModule {
                        stmt.body.emit (this);
 
                        ccode.close ();
+               } else {
+                       Report.error (stmt.source_reference, "internal error: unhandled collection type");
+                       stmt.error = true;
+                       return;
                }
 
                foreach (LocalVariable local in stmt.get_local_variables ()) {
diff --git a/tests/Makefile.am b/tests/Makefile.am
index c6865db09..06e5f44b2 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -217,6 +217,7 @@ TESTS = \
        control-flow/for.vala \
        control-flow/for-switch-continue.vala \
        control-flow/foreach.vala \
+       control-flow/gptrarray-foreach-variable.test \
        control-flow/local-clash-with-implicit-this.vala \
        control-flow/missing-break.test \
        control-flow/missing-return.test \
diff --git a/tests/basic-types/gptrarray.vala b/tests/basic-types/gptrarray.vala
index 6d8aa2d1a..95737cd38 100644
--- a/tests/basic-types/gptrarray.vala
+++ b/tests/basic-types/gptrarray.vala
@@ -73,6 +73,13 @@ void main () {
                assert (foo3.ref_count == 1);
                assert (array.length == 3);
 
+               int loop_size = 0;
+               foreach (weak Foo element in array) {
+                       loop_size++;
+                       assert (element == foo1 || element == foo2 || element == foo3);
+               }
+               assert (loop_size == 3);
+
                assert (foo2 == array.get (1));
                array.set (1, foo4);
                assert (foo4 == array.get (1));
diff --git a/tests/control-flow/foreach.vala b/tests/control-flow/foreach.vala
index 6a36453fd..da169f187 100644
--- a/tests/control-flow/foreach.vala
+++ b/tests/control-flow/foreach.vala
@@ -33,6 +33,41 @@ void test_foreach_gvaluearray () {
        test_unowned (array);
 }
 
+void test_generic_array_owned (GenericArray<Value?> array) {
+       uint i = 0;
+
+       foreach (Value item in array) {
+               i++;
+       }
+
+       assert (i == 3);
+}
+
+void test_generic_array_unowned (GenericArray<Value?> array) {
+       uint i = 0;
+
+       foreach (unowned Value item in array) {
+               i++;
+       }
+
+       assert (i == 3);
+}
+
+void test_foreach_genericarray () {
+       Value value;
+       var array = new GenericArray<Value?> ();
+
+       value = 1;
+       array.add (value);
+       value = 2.0;
+       array.add (value);
+       value = "three";
+       array.add (value);
+
+       test_generic_array_owned (array);
+       test_generic_array_unowned (array);
+}
+
 void test_foreach_multidim_array () {
        int[,] foo = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
        string result = "";
@@ -70,6 +105,7 @@ void test_foreach_slice_array () {
 
 void main () {
        test_foreach_gvaluearray ();
+       test_foreach_genericarray ();
        test_foreach_const_array ();
        test_foreach_multidim_array ();
        test_foreach_slice_array ();
diff --git a/tests/control-flow/gptrarray-foreach-variable.test 
b/tests/control-flow/gptrarray-foreach-variable.test
new file mode 100644
index 000000000..3cef84805
--- /dev/null
+++ b/tests/control-flow/gptrarray-foreach-variable.test
@@ -0,0 +1,7 @@
+Invalid Code
+
+void main () {
+       var array = new GenericArray<string> ();
+       foreach (int element in array) {
+       }
+}
diff --git a/vala/valaforeachstatement.vala b/vala/valaforeachstatement.vala
index 383814085..b3703b876 100644
--- a/vala/valaforeachstatement.vala
+++ b/vala/valaforeachstatement.vala
@@ -176,7 +176,7 @@ public class Vala.ForeachStatement : Block {
                        array_type.inline_allocated = false;
 
                        return check_without_iterator (context, collection_type, array_type.element_type);
-               } else if (context.profile == Profile.GOBJECT && (collection_type.compatible 
(context.analyzer.glist_type) || collection_type.compatible (context.analyzer.gslist_type))) {
+               } else if (context.profile == Profile.GOBJECT && (collection_type.compatible 
(context.analyzer.glist_type) || collection_type.compatible (context.analyzer.gslist_type) || 
collection_type.compatible (context.analyzer.gptrarray_type) || collection_type.compatible 
(context.analyzer.genericarray_type))) {
                        if (collection_type.get_type_arguments ().size != 1) {
                                error = true;
                                Report.error (collection.source_reference, "missing type argument for 
collection");
diff --git a/vala/valasemanticanalyzer.vala b/vala/valasemanticanalyzer.vala
index 8242efd35..711d0c1b4 100644
--- a/vala/valasemanticanalyzer.vala
+++ b/vala/valasemanticanalyzer.vala
@@ -163,6 +163,8 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
        public DataType gslist_type;
        public DataType garray_type;
        public DataType gvaluearray_type;
+       public DataType gptrarray_type;
+       public DataType genericarray_type;
        public Class gerror_type;
        public DataType list_type;
        public DataType tuple_type;
@@ -228,6 +230,8 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
                        gslist_type = new ObjectType ((Class) glib_ns.scope.lookup ("SList"));
                        garray_type = new ObjectType ((Class) glib_ns.scope.lookup ("Array"));
                        gvaluearray_type = new ObjectType ((Class) glib_ns.scope.lookup ("ValueArray"));
+                       gptrarray_type = new ObjectType ((Class) glib_ns.scope.lookup ("PtrArray"));
+                       genericarray_type = new ObjectType ((Class) glib_ns.scope.lookup ("GenericArray"));
 
                        gerror_type = (Class) glib_ns.scope.lookup ("Error");
                        regex_type = new ObjectType ((Class) root_symbol.scope.lookup ("GLib").scope.lookup 
("Regex"));


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