From: Rico Tzschichholz Date: Sun, 7 Mar 2021 12:42:16 +0000 (+0100) Subject: vala: Move type-argument/-parameter count check to DataType.check_type_arguments() X-Git-Tag: 0.51.91~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=865bafbc7e3aadbc13193919873686407af68a78;p=thirdparty%2Fvala.git vala: Move type-argument/-parameter count check to DataType.check_type_arguments() --- diff --git a/vala/valaclass.vala b/vala/valaclass.vala index 99e44c285..88339dec1 100644 --- a/vala/valaclass.vala +++ b/vala/valaclass.vala @@ -557,15 +557,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 cca88d205..59be21bd7 100644 --- a/vala/valadatatype.vala +++ b/vala/valadatatype.vala @@ -626,4 +626,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", 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'", 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'", 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 ca2731258..b8751eca3 100644 --- a/vala/valaobjectcreationexpression.vala +++ b/vala/valaobjectcreationexpression.vala @@ -256,14 +256,9 @@ public class Vala.ObjectCreationExpression : Expression, CallableExpression { 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, CallableExpression { } 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, CallableExpression { } } - 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;