]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Additionally look for "length" property for "foreach with index"
authorRico Tzschichholz <ricotz@ubuntu.com>
Mon, 9 Nov 2020 14:06:20 +0000 (15:06 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Tue, 10 Nov 2020 17:14:55 +0000 (18:14 +0100)
vala/valaforeachstatement.vala

index 2c81b202a55010b0f389a5d31594917080454697..493540f20a8575931626cf50756907d72c40f348 100644 (file)
@@ -206,6 +206,9 @@ public class Vala.ForeachStatement : Block {
                }
                var size_property = collection_type.get_member ("size") as Property;
                if (size_property == null) {
+                       size_property = collection_type.get_member ("length") as Property;
+               }
+               if (size_property == null || !(size_property.property_type is IntegerType)) {
                        return false;
                }
 
@@ -214,7 +217,7 @@ public class Vala.ForeachStatement : Block {
                        list_type.value_owned = false;
                }
                add_statement (new DeclarationStatement (new LocalVariable (list_type, "_%s_list".printf (variable_name), collection, source_reference), source_reference));
-               add_statement (new DeclarationStatement (new LocalVariable (null, "_%s_size".printf (variable_name), new MemberAccess (new MemberAccess.simple ("_%s_list".printf (variable_name), source_reference), "size", source_reference), source_reference), source_reference));
+               add_statement (new DeclarationStatement (new LocalVariable (null, "_%s_size".printf (variable_name), new MemberAccess (new MemberAccess.simple ("_%s_list".printf (variable_name), source_reference), size_property.name, source_reference), source_reference), source_reference));
                add_statement (new DeclarationStatement (new LocalVariable (null, "_%s_index".printf (variable_name), new UnaryExpression (UnaryOperator.MINUS, new IntegerLiteral ("1", source_reference), source_reference), source_reference), source_reference));
                var next = new UnaryExpression (UnaryOperator.INCREMENT, new MemberAccess.simple ("_%s_index".printf (variable_name), source_reference), source_reference);
                var conditional = new BinaryExpression (BinaryOperator.LESS_THAN, next, new MemberAccess.simple ("_%s_size".printf (variable_name), source_reference), source_reference);