[vala/0.48] vala: Move type-argument/-parameter count check to DataType.check_type_arguments()



commit 7480eb33d541ef5158a39b8129a42887ee90c2ad
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Sun Mar 7 13:42:16 2021 +0100

    vala: Move type-argument/-parameter count check to DataType.check_type_arguments()

 vala/valaclass.vala                    | 10 ++------
 vala/valadatatype.vala                 | 45 ++++++++++++++++++++++++++++++++++
 vala/valadelegatetype.vala             | 18 ++------------
 vala/valaobjectcreationexpression.vala | 15 ++----------
 vala/valaobjecttype.vala               | 16 ++----------
 5 files changed, 53 insertions(+), 51 deletions(-)
---
diff --git a/vala/valaclass.vala b/vala/valaclass.vala
index dcd3ecacc..3fffdb352 100644
--- a/vala/valaclass.vala
+++ b/vala/valaclass.vala
@@ -555,15 +555,9 @@ public class Vala.Class : ObjectTypeSymbol {
                                return false;
                        }
 
-                       int n_type_args = base_type_reference.get_type_arguments ().size;
-                       int n_type_params = ((ObjectTypeSymbol) 
base_type_reference.type_symbol).get_type_parameters ().size;
-                       if (n_type_args < n_type_params) {
+                       // check whether there is the expected amount of type-arguments
+                       if (!base_type_reference.check_type_arguments (context)) {
                                error = true;
-                               Report.error (base_type_reference.source_reference, "too few type arguments");
-                               return false;
-                       } else if (n_type_args > n_type_params) {
-                               error = true;
-                               Report.error (base_type_reference.source_reference, "too many type 
arguments");
                                return false;
                        }
                }
diff --git a/vala/valadatatype.vala b/vala/valadatatype.vala
index 2980877cc..9fa6f7395 100644
--- a/vala/valadatatype.vala
+++ b/vala/valadatatype.vala
@@ -617,4 +617,49 @@ public abstract class Vala.DataType : CodeNode {
                        return null;
                }
        }
+
+       /**
+        * Returns whether the given amount of type-argument matches the symbol's count of type-parameters
+        *
+        * @param context a CodeContext
+        * @param allow_none whether no type-argments are allowed
+        * @return true if successful
+        */
+       public bool check_type_arguments (CodeContext context, bool allow_none = false) {
+               int n_type_args = get_type_arguments ().size;
+               int expected_n_type_args = 0;
+
+               if (type_symbol is ObjectTypeSymbol) {
+                       expected_n_type_args = ((ObjectTypeSymbol) type_symbol).get_type_parameters ().size;
+               } else if (type_symbol is Struct) {
+                       expected_n_type_args = ((Struct) type_symbol).get_type_parameters ().size;
+               } else if (type_symbol is Delegate) {
+                       expected_n_type_args = ((Delegate) type_symbol).get_type_parameters ().size;
+               } else if (n_type_args > 0) {
+                       Report.error (source_reference, "`%s' does not support type arguments".printf 
(type_symbol.get_full_name ()));
+                       error = true;
+                       return false;
+               } else {
+                       // nothing to do here
+                       return true;
+               }
+
+               if ((!allow_none || n_type_args > 0) && n_type_args < expected_n_type_args) {
+                       error = true;
+                       Report.error (source_reference, "too few type arguments for `%s'".printf 
(type_symbol.get_full_name ()));
+                       return false;
+               } else if ((!allow_none || n_type_args > 0) && n_type_args > expected_n_type_args) {
+                       error = true;
+                       Report.error (source_reference, "too many type arguments for `%s'".printf 
(type_symbol.get_full_name ()));
+                       return false;
+               }
+
+               foreach (DataType type in get_type_arguments ()) {
+                       if (!type.check (context)) {
+                               return false;
+                       }
+               }
+
+               return true;
+       }
 }
diff --git a/vala/valadelegatetype.vala b/vala/valadelegatetype.vala
index 4029f00af..9821a9e70 100644
--- a/vala/valadelegatetype.vala
+++ b/vala/valadelegatetype.vala
@@ -99,23 +99,9 @@ public class Vala.DelegateType : CallableType {
                        return false;
                }
 
-               var n_type_params = delegate_symbol.get_type_parameters ().size;
-               var n_type_args = get_type_arguments ().size;
-               if (n_type_args > 0 && n_type_args < n_type_params) {
-                       error = true;
-                       Report.error (source_reference, "too few type arguments");
+               // check whether there is the expected amount of type-arguments
+               if (!check_type_arguments (context, true)) {
                        return false;
-               } else if (n_type_args > 0 && n_type_args > n_type_params) {
-                       error = true;
-                       Report.error (source_reference, "too many type arguments");
-                       return false;
-               }
-
-               foreach (DataType type in get_type_arguments ()) {
-                       if (!type.check (context)) {
-                               error = true;
-                               return false;
-                       }
                }
 
                return true;
diff --git a/vala/valaobjectcreationexpression.vala b/vala/valaobjectcreationexpression.vala
index ea9565755..321058ae6 100644
--- a/vala/valaobjectcreationexpression.vala
+++ b/vala/valaobjectcreationexpression.vala
@@ -256,14 +256,9 @@ public class Vala.ObjectCreationExpression : Expression {
                value_type = type_reference.copy ();
                value_type.value_owned = true;
 
-               int given_num_type_args = type_reference.get_type_arguments ().size;
-               int expected_num_type_args = 0;
-
                if (type is Class) {
                        var cl = (Class) type;
 
-                       expected_num_type_args = cl.get_type_parameters ().size;
-
                        if (struct_creation) {
                                error = true;
                                Report.error (source_reference, "syntax error, use `new' to create new 
objects");
@@ -320,8 +315,6 @@ public class Vala.ObjectCreationExpression : Expression {
                } else if (type is Struct) {
                        var st = (Struct) type;
 
-                       expected_num_type_args = st.get_type_parameters ().size;
-
                        if (!struct_creation && !context.deprecated) {
                                Report.warning (source_reference, "deprecated syntax, don't use `new' to 
initialize structs");
                        }
@@ -337,13 +330,9 @@ public class Vala.ObjectCreationExpression : Expression {
                        }
                }
 
-               if (expected_num_type_args > given_num_type_args) {
-                       error = true;
-                       Report.error (source_reference, "too few type arguments");
-                       return false;
-               } else if (expected_num_type_args < given_num_type_args) {
+               // check whether there is the expected amount of type-arguments
+               if (!type_reference.check_type_arguments (context)) {
                        error = true;
-                       Report.error (source_reference, "too many type arguments");
                        return false;
                }
 
diff --git a/vala/valaobjecttype.vala b/vala/valaobjecttype.vala
index 53101926c..8e08ade76 100644
--- a/vala/valaobjecttype.vala
+++ b/vala/valaobjecttype.vala
@@ -103,21 +103,9 @@ public class Vala.ObjectType : ReferenceType {
                        return false;
                }
 
-               int n_type_args = get_type_arguments ().size;
-               if (n_type_args > 0 && n_type_args < object_type_symbol.get_type_parameters ().size) {
-                       error = true;
-                       Report.error (source_reference, "too few type arguments");
+               // check whether there is the expected amount of type-arguments
+               if (!check_type_arguments (context, true)) {
                        return false;
-               } else if (n_type_args > 0 && n_type_args > object_type_symbol.get_type_parameters ().size) {
-                       error = true;
-                       Report.error (source_reference, "too many type arguments");
-                       return false;
-               }
-
-               foreach (DataType type in get_type_arguments ()) {
-                       if (!type.check (context)) {
-                               return false;
-                       }
                }
 
                return true;


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