summaryrefslogtreecommitdiff
path: root/vala/valadatatype.vala
diff options
context:
space:
mode:
authorRico Tzschichholz <ricotz@ubuntu.com>2021-03-07 13:42:16 +0100
committerRico Tzschichholz <ricotz@ubuntu.com>2021-03-07 15:36:07 +0100
commit865bafbc7e3aadbc13193919873686407af68a78 (patch)
tree2a69cc92eb36145822073e531193baa2b0003ca1 /vala/valadatatype.vala
parent0c87614330856836d79f9f69a0bb373cf54eca29 (diff)
downloadvala-865bafbc7e3aadbc13193919873686407af68a78.tar.gz
vala: Move type-argument/-parameter count check to DataType.check_type_arguments()
Diffstat (limited to 'vala/valadatatype.vala')
-rw-r--r--vala/valadatatype.vala45
1 files changed, 45 insertions, 0 deletions
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;
+ }
}