]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Move type-argument checks to SemanticAnalyzer wip/type-args
authorRico Tzschichholz <ricotz@ubuntu.com>
Wed, 8 Aug 2018 13:25:51 +0000 (15:25 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Mon, 7 Jan 2019 12:13:47 +0000 (13:13 +0100)
codegen/valaccodebasemodule.vala
codegen/valaccodemethodcallmodule.vala
codegen/valaccodemethodmodule.vala
vala/valafield.vala
vala/valalocalvariable.vala
vala/valamethod.vala
vala/valaobjectcreationexpression.vala
vala/valaparameter.vala
vala/valaproperty.vala
vala/valasemanticanalyzer.vala

index ae1ad0c2c295942f0ba064ad0bc9ec5ba65fb78b..a28c3d9f1fb1b933c93ea26a625647dbd095dc54 100644 (file)
@@ -1136,8 +1136,6 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                push_line (f.source_reference);
                visit_member (f);
 
-               check_type (f.variable_type);
-
                var cl = f.parent_symbol as Class;
                bool is_gtypeinstance = (cl != null && !cl.is_compact);
 
@@ -1459,17 +1457,9 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                return false;
        }
 
-       public override void visit_formal_parameter (Parameter p) {
-               if (!p.ellipsis) {
-                       check_type (p.variable_type);
-               }
-       }
-
        public override void visit_property (Property prop) {
                visit_member (prop);
 
-               check_type (prop.property_type);
-
                if (prop.get_accessor != null) {
                        prop.get_accessor.accept (this);
                }
@@ -2431,8 +2421,6 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
        }
 
        public override void visit_local_variable (LocalVariable local) {
-               check_type (local.variable_type);
-
                /* Declaration */
 
                generate_type_declaration (local.variable_type, cfile);
@@ -4549,124 +4537,6 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                }
        }
 
-       bool is_reference_type_argument (DataType type_arg) {
-               if (type_arg is ErrorType || (type_arg.data_type != null && type_arg.data_type.is_reference_type ())) {
-                       return true;
-               } else {
-                       return false;
-               }
-       }
-
-       bool is_nullable_value_type_argument (DataType type_arg) {
-               if (type_arg is ValueType && type_arg.nullable) {
-                       return true;
-               } else {
-                       return false;
-               }
-       }
-
-       bool is_signed_integer_type_argument (DataType type_arg) {
-               var st = type_arg.data_type as Struct;
-               if (type_arg is EnumValueType) {
-                       return true;
-               } else if (type_arg.nullable) {
-                       return false;
-               } else if (st == null) {
-                       return false;
-               } else if (st.is_subtype_of (bool_type.data_type)) {
-                       return true;
-               } else if (st.is_subtype_of (char_type.data_type)) {
-                       return true;
-               } else if (unichar_type != null && st.is_subtype_of (unichar_type.data_type)) {
-                       return true;
-               } else if (st.is_subtype_of (short_type.data_type)) {
-                       return true;
-               } else if (st.is_subtype_of (int_type.data_type)) {
-                       return true;
-               } else if (st.is_subtype_of (long_type.data_type)) {
-                       return true;
-               } else if (st.is_subtype_of (int8_type.data_type)) {
-                       return true;
-               } else if (st.is_subtype_of (int16_type.data_type)) {
-                       return true;
-               } else if (st.is_subtype_of (int32_type.data_type)) {
-                       return true;
-               } else if (st.is_subtype_of (gtype_type)) {
-                       return true;
-               } else {
-                       return false;
-               }
-       }
-
-       bool is_unsigned_integer_type_argument (DataType type_arg) {
-               var st = type_arg.data_type as Struct;
-               if (st == null) {
-                       return false;
-               } else if (type_arg.nullable) {
-                       return false;
-               } else if (st.is_subtype_of (uchar_type.data_type)) {
-                       return true;
-               } else if (st.is_subtype_of (ushort_type.data_type)) {
-                       return true;
-               } else if (st.is_subtype_of (uint_type.data_type)) {
-                       return true;
-               } else if (st.is_subtype_of (ulong_type.data_type)) {
-                       return true;
-               } else if (st.is_subtype_of (uint8_type.data_type)) {
-                       return true;
-               } else if (st.is_subtype_of (uint16_type.data_type)) {
-                       return true;
-               } else if (st.is_subtype_of (uint32_type.data_type)) {
-                       return true;
-               } else {
-                       return false;
-               }
-       }
-
-       public void check_type (DataType type) {
-               var array_type = type as ArrayType;
-               if (array_type != null) {
-                       check_type (array_type.element_type);
-                       if (array_type.element_type is ArrayType) {
-                               Report.error (type.source_reference, "Stacked arrays are not supported");
-                       } else if (array_type.element_type is DelegateType) {
-                               var delegate_type = (DelegateType) array_type.element_type;
-                               if (delegate_type.delegate_symbol.has_target) {
-                                       Report.error (type.source_reference, "Delegates with target are not supported as array element type");
-                               }
-                       }
-               }
-               foreach (var type_arg in type.get_type_arguments ()) {
-                       check_type (type_arg);
-                       check_type_argument (type_arg);
-               }
-       }
-
-       public void check_type_arguments (MemberAccess access) {
-               foreach (var type_arg in access.get_type_arguments ()) {
-                       check_type (type_arg);
-                       check_type_argument (type_arg);
-               }
-       }
-
-       void check_type_argument (DataType type_arg) {
-               if (type_arg is GenericType
-                   || type_arg is PointerType
-                   || is_reference_type_argument (type_arg)
-                   || is_nullable_value_type_argument (type_arg)
-                   || is_signed_integer_type_argument (type_arg)
-                   || is_unsigned_integer_type_argument (type_arg)) {
-                       // no error
-               } else if (type_arg is DelegateType) {
-                       var delegate_type = (DelegateType) type_arg;
-                       if (delegate_type.delegate_symbol.has_target) {
-                               Report.error (type_arg.source_reference, "Delegates with target are not supported as generic type arguments");
-                       }
-               } else {
-                       Report.error (type_arg.source_reference, "`%s' is not a supported generic type argument, use `?' to box value types".printf (type_arg.to_string ()));
-               }
-       }
-
        public virtual void generate_class_declaration (Class cl, CCodeFile decl_space) {
                if (add_symbol_declaration (decl_space, cl, get_ccode_name (cl))) {
                        return;
@@ -4714,8 +4584,6 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                CCodeExpression instance = null;
                CCodeExpression creation_expr = null;
 
-               check_type (expr.type_reference);
-
                var st = expr.type_reference.data_type as Struct;
                if ((st != null && (!st.is_simple_type () || get_ccode_name (st) == "va_list")) || expr.get_object_initializer ().size > 0) {
                        // value-type initialization or object creation expression with object initializer
@@ -5920,22 +5788,24 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
        }
 
        public CCodeExpression convert_from_generic_pointer (CCodeExpression cexpr, DataType actual_type) {
+               unowned SemanticAnalyzer analyzer = context.analyzer;
                var result = cexpr;
-               if (is_reference_type_argument (actual_type) || is_nullable_value_type_argument (actual_type)) {
+               if (analyzer.is_reference_type_argument (actual_type) || analyzer.is_nullable_value_type_argument (actual_type)) {
                        result = new CCodeCastExpression (cexpr, get_ccode_name (actual_type));
-               } else if (is_signed_integer_type_argument (actual_type)) {
+               } else if (analyzer.is_signed_integer_type_argument (actual_type)) {
                        result = new CCodeCastExpression (new CCodeCastExpression (cexpr, "gintptr"), get_ccode_name (actual_type));
-               } else if (is_unsigned_integer_type_argument (actual_type)) {
+               } else if (analyzer.is_unsigned_integer_type_argument (actual_type)) {
                        result = new CCodeCastExpression (new CCodeCastExpression (cexpr, "guintptr"), get_ccode_name (actual_type));
                }
                return result;
        }
 
        public CCodeExpression convert_to_generic_pointer (CCodeExpression cexpr, DataType actual_type) {
+               unowned SemanticAnalyzer analyzer = context.analyzer;
                var result = cexpr;
-               if (is_signed_integer_type_argument (actual_type)) {
+               if (analyzer.is_signed_integer_type_argument (actual_type)) {
                        result = new CCodeCastExpression (new CCodeCastExpression (cexpr, "gintptr"), "gpointer");
-               } else if (is_unsigned_integer_type_argument (actual_type)) {
+               } else if (analyzer.is_unsigned_integer_type_argument (actual_type)) {
                        result = new CCodeCastExpression (new CCodeCastExpression (cexpr, "guintptr"), "gpointer");
                }
                return result;
index 9086268e818876c848b1f3ad1136b0b3cbb26a31..f5a682532537e2837498b6bd21c3defc8498de41 100644 (file)
@@ -46,7 +46,7 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
                        m = ((MethodType) itype).method_symbol;
 
                        if (!get_ccode_simple_generics (m)) {
-                               check_type_arguments (ma);
+                               context.analyzer.check_type_arguments (ma);
                        }
 
                        if (ma.inner != null && ma.inner.value_type is EnumValueType && ((EnumValueType) ma.inner.value_type).get_to_string_method() == m) {
index fef66ef328aff83f0a681ea9a254b34bf767a500..607144a968481dab1dace19385102dcf5a30a28c 100644 (file)
@@ -329,8 +329,6 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
                bool in_gobject_creation_method = false;
                bool in_fundamental_creation_method = false;
 
-               check_type (m.return_type);
-
                bool profile = m.get_attribute ("Profile") != null;
 
                if (m is CreationMethod) {
index 6bd54ff95b376cae725b98a02658c029389d9a08..77c43ffcdab1af348247f14a5c329c4fa3d17dc4 100644 (file)
@@ -99,6 +99,7 @@ public class Vala.Field : Variable, Lockable {
                }
 
                variable_type.check (context);
+               context.analyzer.check_type (variable_type);
 
                // check whether field type is at least as accessible as the field
                if (!context.analyzer.is_type_accessible (this, variable_type)) {
index 67187ce764f3b31827d61f340321b2e084d38864..97401a3240ff167dfbc778bfa4b83e6cc64a82b2 100644 (file)
@@ -99,6 +99,7 @@ public class Vala.LocalVariable : Variable {
                                return false;
                        }
                        variable_type.check (context);
+                       context.analyzer.check_type (variable_type);
                }
 
                // Catch initializer list transformation:
index bd203ab363aa065e69e56f7e221de64c1a3b96c2..cb417d35a93c690f86e4d3f089f92cd813069e86 100644 (file)
@@ -781,6 +781,7 @@ public class Vala.Method : Subroutine, Callable {
 
                return_type.floating_reference = returns_floating_reference;
                return_type.check (context);
+               context.analyzer.check_type (return_type);
 
                var init_attr = get_attribute ("ModuleInit");
                if (init_attr != null) {
index 0b6a29391f1fb9ddb3b8b6a6dd94bbd098b4e1c3..620a3b2a6f9ddf0d770c9d29aecfcbae12dd941f 100644 (file)
@@ -477,6 +477,8 @@ public class Vala.ObjectCreationExpression : Expression {
                        }
                }
 
+               context.analyzer.check_type (type_reference);
+
                foreach (MemberInitializer init in get_object_initializer ()) {
                        context.analyzer.visit_member_initializer (init, type_reference);
                }
index 7c13a7c194922be56eab5877a2149e6e69483d9e..865dfbf89283760ef29c9b05f7a230cb3e393d2e 100644 (file)
@@ -186,6 +186,8 @@ public class Vala.Parameter : Variable {
                }
 
                if (!ellipsis) {
+                       context.analyzer.check_type (variable_type);
+
                        // check whether parameter type is at least as accessible as the method
                        if (!context.analyzer.is_type_accessible (this, variable_type)) {
                                error = true;
index cd1bcaff54692fd041759f090779cae2a2abbe86..d2c038ac91a093ae9bb0ba809f281d7c53f05970 100644 (file)
@@ -465,6 +465,7 @@ public class Vala.Property : Symbol, Lockable {
                }
 
                property_type.check (context);
+               context.analyzer.check_type (property_type);
 
                if (get_accessor == null && set_accessor == null) {
                        error = true;
index 485ab222dad6082b84bf73041bc82cb43d4e8509..f1028dc0a2f9db77b5b388357b9447e330f0898e 100644 (file)
@@ -134,8 +134,7 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
 
        public DataType void_type = new VoidType ();
        public DataType bool_type;
-       public DataType string_type;
-       public DataType regex_type;
+       public DataType char_type;
        public DataType uchar_type;
        public DataType short_type;
        public DataType ushort_type;
@@ -143,11 +142,18 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
        public DataType uint_type;
        public DataType long_type;
        public DataType ulong_type;
+       public DataType int8_type;
+       public DataType uint8_type;
+       public DataType int16_type;
+       public DataType uint16_type;
+       public DataType int32_type;
+       public DataType uint32_type;
        public DataType size_t_type;
        public DataType ssize_t_type;
-       public DataType int8_type;
        public DataType unichar_type;
        public DataType double_type;
+       public DataType string_type;
+       public DataType regex_type;
        public DataType type_type;
        public DataType va_list_type;
        public Class object_type;
@@ -180,19 +186,24 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
                var root_symbol = context.root;
 
                bool_type = new BooleanType ((Struct) root_symbol.scope.lookup ("bool"));
-               string_type = new ObjectType ((Class) root_symbol.scope.lookup ("string"));
-               int_type = new IntegerType ((Struct) root_symbol.scope.lookup ("int"));
-               uint_type = new IntegerType ((Struct) root_symbol.scope.lookup ("uint"));
-
+               char_type = new IntegerType ((Struct) root_symbol.scope.lookup ("char"));
                uchar_type = new IntegerType ((Struct) root_symbol.scope.lookup ("uchar"));
-               int8_type = new IntegerType ((Struct) root_symbol.scope.lookup ("int8"));
                short_type = new IntegerType ((Struct) root_symbol.scope.lookup ("short"));
                ushort_type = new IntegerType ((Struct) root_symbol.scope.lookup ("ushort"));
+               int_type = new IntegerType ((Struct) root_symbol.scope.lookup ("int"));
+               uint_type = new IntegerType ((Struct) root_symbol.scope.lookup ("uint"));
                long_type = new IntegerType ((Struct) root_symbol.scope.lookup ("long"));
                ulong_type = new IntegerType ((Struct) root_symbol.scope.lookup ("ulong"));
+               int8_type = new IntegerType ((Struct) root_symbol.scope.lookup ("int8"));
+               uint8_type = new IntegerType ((Struct) root_symbol.scope.lookup ("uint8"));
+               int16_type = new IntegerType ((Struct) root_symbol.scope.lookup ("int16"));
+               uint16_type = new IntegerType ((Struct) root_symbol.scope.lookup ("uint16"));
+               int32_type = new IntegerType ((Struct) root_symbol.scope.lookup ("int32"));
+               uint32_type = new IntegerType ((Struct) root_symbol.scope.lookup ("uint32"));
                size_t_type = new IntegerType ((Struct) root_symbol.scope.lookup ("size_t"));
                ssize_t_type = new IntegerType ((Struct) root_symbol.scope.lookup ("ssize_t"));
                double_type = new FloatingType ((Struct) root_symbol.scope.lookup ("double"));
+               string_type = new ObjectType ((Class) root_symbol.scope.lookup ("string"));
                va_list_type = new StructValueType ((Struct) root_symbol.scope.lookup ("va_list"));
 
                var unichar_struct = (Struct) root_symbol.scope.lookup ("unichar");
@@ -1073,4 +1084,110 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
                }
                return false;
        }
+
+       public bool is_reference_type_argument (DataType type_arg) {
+               if (type_arg is ErrorType || (type_arg.data_type != null && type_arg.data_type.is_reference_type ())) {
+                       return true;
+               } else {
+                       return false;
+               }
+       }
+
+       public bool is_nullable_value_type_argument (DataType type_arg) {
+               if (type_arg is ValueType && type_arg.nullable) {
+                       return true;
+               } else {
+                       return false;
+               }
+       }
+
+       public bool is_signed_integer_type_argument (DataType type_arg) {
+               var st = type_arg.data_type as Struct;
+               if (type_arg is EnumValueType) {
+                       return true;
+               } else if (type_arg.nullable) {
+                       return false;
+               } else if (st == null) {
+                       return false;
+               } else if (st.is_subtype_of (bool_type.data_type)) {
+                       return true;
+               } else if (st.is_subtype_of (char_type.data_type)) {
+                       return true;
+               } else if (unichar_type != null && st.is_subtype_of (unichar_type.data_type)) {
+                       return true;
+               } else if (st.is_subtype_of (short_type.data_type)) {
+                       return true;
+               } else if (st.is_subtype_of (int_type.data_type)) {
+                       return true;
+               } else if (st.is_subtype_of (long_type.data_type)) {
+                       return true;
+               } else if (st.is_subtype_of (int8_type.data_type)) {
+                       return true;
+               } else if (st.is_subtype_of (int16_type.data_type)) {
+                       return true;
+               } else if (st.is_subtype_of (int32_type.data_type)) {
+                       return true;
+               } else if (st.is_subtype_of (type_type.data_type)) {
+                       return true;
+               } else {
+                       return false;
+               }
+       }
+
+       public bool is_unsigned_integer_type_argument (DataType type_arg) {
+               var st = type_arg.data_type as Struct;
+               if (st == null) {
+                       return false;
+               } else if (type_arg.nullable) {
+                       return false;
+               } else if (st.is_subtype_of (uchar_type.data_type)) {
+                       return true;
+               } else if (st.is_subtype_of (ushort_type.data_type)) {
+                       return true;
+               } else if (st.is_subtype_of (uint_type.data_type)) {
+                       return true;
+               } else if (st.is_subtype_of (ulong_type.data_type)) {
+                       return true;
+               } else if (st.is_subtype_of (uint8_type.data_type)) {
+                       return true;
+               } else if (st.is_subtype_of (uint16_type.data_type)) {
+                       return true;
+               } else if (st.is_subtype_of (uint32_type.data_type)) {
+                       return true;
+               } else {
+                       return false;
+               }
+       }
+
+       public void check_type (DataType type) {
+               foreach (var type_arg in type.get_type_arguments ()) {
+                       check_type (type_arg);
+                       check_type_argument (type_arg);
+               }
+       }
+
+       public void check_type_arguments (MemberAccess access) {
+               foreach (var type_arg in access.get_type_arguments ()) {
+                       check_type (type_arg);
+                       check_type_argument (type_arg);
+               }
+       }
+
+       void check_type_argument (DataType type_arg) {
+               if (type_arg is GenericType
+                   || type_arg is PointerType
+                   || is_reference_type_argument (type_arg)
+                   || is_nullable_value_type_argument (type_arg)
+                   || is_signed_integer_type_argument (type_arg)
+                   || is_unsigned_integer_type_argument (type_arg)) {
+                       // no error
+               } else if (type_arg is DelegateType) {
+                       var delegate_type = (DelegateType) type_arg;
+                       if (delegate_type.delegate_symbol.has_target) {
+                               Report.error (type_arg.source_reference, "Delegates with target are not supported as generic type arguments");
+                       }
+               } else {
+                       Report.error (type_arg.source_reference, "`%s' is not a supported generic type argument, use `?' to box value types".printf (type_arg.to_string ()));
+               }
+       }
 }