[vala/wip/array-length-type: 4/10] vala: Add ArrayType.length_type and ArrayCreationExpression.length_type



commit 36671ae5def89b46384e627a467247c834948254
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Tue Oct 30 23:24:32 2018 +0100

    vala: Add ArrayType.length_type and ArrayCreationExpression.length_type
    
    https://gitlab.gnome.org/GNOME/vala/issues/607

 vala/valaarraycreationexpression.vala | 43 ++++++++++++++++++++++++
 vala/valaarraytype.vala               | 63 +++++++++++++++++++++++++++--------
 vala/valainitializerlist.vala         |  1 +
 vala/valasliceexpression.vala         |  5 +--
 4 files changed, 96 insertions(+), 16 deletions(-)
---
diff --git a/vala/valaarraycreationexpression.vala b/vala/valaarraycreationexpression.vala
index d3aebc9dc..b792c3acb 100644
--- a/vala/valaarraycreationexpression.vala
+++ b/vala/valaarraycreationexpression.vala
@@ -39,6 +39,19 @@ public class Vala.ArrayCreationExpression : Expression {
                }
        }
 
+       /**
+        * The length type.
+        */
+       public DataType? length_type {
+               get { return _length_type; }
+               set {
+                       _length_type = value;
+                       if (_length_type != null) {
+                               _length_type.parent_node = this;
+                       }
+               }
+       }
+
        /**
         * The rank of the array.
         */
@@ -63,6 +76,7 @@ public class Vala.ArrayCreationExpression : Expression {
        }
 
        private DataType _element_type;
+       private DataType _length_type;
        private InitializerList? _initializer_list;
 
        /**
@@ -94,6 +108,10 @@ public class Vala.ArrayCreationExpression : Expression {
                        element_type.accept (visitor);
                }
 
+               if (length_type != null) {
+                       length_type.accept (visitor);
+               }
+
                foreach (Expression e in sizes) {
                        e.accept (visitor);
                }
@@ -114,6 +132,14 @@ public class Vala.ArrayCreationExpression : Expression {
        }
 
        public override bool is_accessible (Symbol sym) {
+               if (element_type != null && !element_type.is_accessible (sym)) {
+                       return false;
+               }
+
+               if (length_type != null && !length_type.is_accessible (sym)) {
+                       return false;
+               }
+
                foreach (Expression e in sizes) {
                        if (!e.is_accessible (sym)) {
                                return false;
@@ -159,6 +185,9 @@ public class Vala.ArrayCreationExpression : Expression {
                if (element_type == old_type) {
                        element_type = new_type;
                }
+               if (length_type == old_type) {
+                       length_type = new_type;
+               }
        }
 
        private int create_sizes_from_initializer_list (CodeContext context, InitializerList il, int rank, 
List<Literal> sl) {
@@ -215,6 +244,17 @@ public class Vala.ArrayCreationExpression : Expression {
                        element_type.check (context);
                }
 
+               if (length_type == null) {
+                       // Make sure that "int" is still picked up as default
+                       length_type = context.analyzer.int_type.copy ();
+               } else {
+                       length_type.check (context);
+                       if (!(length_type is IntegerType)) {
+                               error = true;
+                               Report.error (length_type.source_reference, "Expected integer type as length 
type of array");
+                       }
+               }
+
                foreach (Expression e in sizes) {
                        e.check (context);
                }
@@ -222,6 +262,7 @@ public class Vala.ArrayCreationExpression : Expression {
                var calc_sizes = new ArrayList<Literal> ();
                if (initlist != null) {
                        initlist.target_type = new ArrayType (element_type, rank, source_reference);
+                       ((ArrayType) initlist.target_type).length_type = length_type.copy ();
 
                        if (!initlist.check (context)) {
                                error = true;
@@ -235,6 +276,7 @@ public class Vala.ArrayCreationExpression : Expression {
                        if (calc_sizes.size != rank) {
                                error = true;
                                var actual_type = new ArrayType (element_type, calc_sizes.size, 
source_reference);
+                               ((ArrayType) actual_type).length_type = length_type;
                                Report.error (initlist.source_reference, "Expected initializer for `%s' but 
got `%s'".printf (target_type.to_string (), actual_type.to_string ()));
                        }
                }
@@ -279,6 +321,7 @@ public class Vala.ArrayCreationExpression : Expression {
                }
 
                value_type = new ArrayType (element_type, rank, source_reference);
+               ((ArrayType) value_type).length_type = length_type.copy ();
                value_type.value_owned = true;
 
                if (!value_type.check (context)) {
diff --git a/vala/valaarraytype.vala b/vala/valaarraytype.vala
index e6987f043..7f4b45dda 100644
--- a/vala/valaarraytype.vala
+++ b/vala/valaarraytype.vala
@@ -37,6 +37,19 @@ public class Vala.ArrayType : ReferenceType {
                }
        }
 
+       /**
+        * The length type.
+        */
+       public DataType? length_type {
+               get { return _length_type; }
+               set {
+                       _length_type = value;
+                       if (_length_type != null) {
+                               _length_type.parent_node = this;
+                       }
+               }
+       }
+
        public bool invalid_syntax { get; set; }
 
        public bool inline_allocated { get; set; }
@@ -62,6 +75,7 @@ public class Vala.ArrayType : ReferenceType {
        public int rank { get; set; }
 
        private DataType _element_type;
+       private DataType _length_type;
        private Expression _length;
 
        private ArrayLengthField length_field;
@@ -97,13 +111,11 @@ public class Vala.ArrayType : ReferenceType {
 
                        length_field.access = SymbolAccessibility.PUBLIC;
 
-                       var root_symbol = source_reference.file.context.root;
                        if (rank > 1) {
                                // length is an int[] containing the dimensions of the array, starting at 0
-                               ValueType integer = new IntegerType ((Struct) root_symbol.scope.lookup 
("int"));
-                               length_field.variable_type = new ArrayType (integer, 1, source_reference);
+                               length_field.variable_type = new ArrayType (length_type.copy (), 1, 
source_reference);
                        } else {
-                               length_field.variable_type = new IntegerType ((Struct) 
root_symbol.scope.lookup ("int"));
+                               length_field.variable_type = length_type.copy ();
                        }
 
                }
@@ -119,10 +131,7 @@ public class Vala.ArrayType : ReferenceType {
 
                        resize_method.set_attribute_string ("CCode", "cname", "g_renew");
 
-                       var root_symbol = source_reference.file.context.root;
-                       var int_type = new IntegerType ((Struct) root_symbol.scope.lookup ("int"));
-
-                       resize_method.add_parameter (new Parameter ("length", int_type));
+                       resize_method.add_parameter (new Parameter ("length", length_type));
 
                        resize_method.returns_modified_pointer = true;
                }
@@ -138,12 +147,9 @@ public class Vala.ArrayType : ReferenceType {
 
                        move_method.set_attribute_string ("CCode", "cname", "_vala_array_move");
 
-                       var root_symbol = source_reference.file.context.root;
-                       var int_type = new IntegerType ((Struct) root_symbol.scope.lookup ("int"));
-
-                       move_method.add_parameter (new Parameter ("src", int_type));
-                       move_method.add_parameter (new Parameter ("dest", int_type));
-                       move_method.add_parameter (new Parameter ("length", int_type));
+                       move_method.add_parameter (new Parameter ("src", length_type));
+                       move_method.add_parameter (new Parameter ("dest", length_type));
+                       move_method.add_parameter (new Parameter ("length", length_type));
                }
                return move_method;
        }
@@ -163,6 +169,10 @@ public class Vala.ArrayType : ReferenceType {
 
        public override DataType copy () {
                var result = new ArrayType (element_type.copy (), rank, source_reference);
+               if (length_type != null) {
+                       result.length_type = length_type.copy ();
+               }
+
                result.value_owned = value_owned;
                result.nullable = nullable;
                result.floating_reference = floating_reference;
@@ -230,6 +240,10 @@ public class Vala.ArrayType : ReferenceType {
                        return true;
                }
 
+               if (length_type.compatible (target_array_type.length_type)) {
+                       return true;
+               }
+
                return false;
        }
 
@@ -239,15 +253,24 @@ public class Vala.ArrayType : ReferenceType {
 
        public override void accept_children (CodeVisitor visitor) {
                element_type.accept (visitor);
+               if (length_type != null) {
+                       length_type.accept (visitor);
+               }
        }
 
        public override void replace_type (DataType old_type, DataType new_type) {
                if (element_type == old_type) {
                        element_type = new_type;
                }
+               if (length_type == old_type) {
+                       length_type = new_type;
+               }
        }
 
        public override bool is_accessible (Symbol sym) {
+               if (length_type != null && !length_type.is_accessible (sym)) {
+                       return false;
+               }
                return element_type.is_accessible (sym);
        }
 
@@ -278,6 +301,18 @@ public class Vala.ArrayType : ReferenceType {
                        }
                }
 
+               if (length_type == null) {
+                       // Make sure that "int" is still picked up as default
+                       length_type = context.analyzer.int_type.copy ();
+               } else {
+                       length_type.check (context);
+                       if (!(length_type is IntegerType)) {
+                               error = true;
+                               Report.error (length_type.source_reference, "Expected integer type as length 
type of array");
+                               return false;
+                       }
+               }
+
                return element_type.check (context);
        }
 
diff --git a/vala/valainitializerlist.vala b/vala/valainitializerlist.vala
index ec4315e0b..6d170f4ec 100644
--- a/vala/valainitializerlist.vala
+++ b/vala/valainitializerlist.vala
@@ -164,6 +164,7 @@ public class Vala.InitializerList : Expression {
                                var old_parent_node = parent_node;
 
                                var array_creation = new ArrayCreationExpression 
(array_type.element_type.copy (), array_type.rank, this, source_reference);
+                               array_creation.length_type = array_type.length_type.copy ();
                                array_creation.target_type = target_type;
                                old_parent_node.replace_expression (this, array_creation);
 
diff --git a/vala/valasliceexpression.vala b/vala/valasliceexpression.vala
index 6621b41d8..4d3e84489 100644
--- a/vala/valasliceexpression.vala
+++ b/vala/valasliceexpression.vala
@@ -115,8 +115,9 @@ public class Vala.SliceExpression : Expression {
                }
 
                if (container.value_type is ArrayType) {
-                       start.target_type = context.analyzer.int_type.copy ();
-                       stop.target_type = context.analyzer.int_type.copy ();
+                       var array_type = (ArrayType) container.value_type;
+                       start.target_type = array_type.length_type.copy ();
+                       stop.target_type = array_type.length_type.copy ();
                }
 
                if (!start.check (context)) {


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