diff options
author | Rico Tzschichholz <ricotz@ubuntu.com> | 2023-04-10 17:13:35 +0200 |
---|---|---|
committer | Rico Tzschichholz <ricotz@ubuntu.com> | 2023-04-11 09:31:39 +0200 |
commit | bedd5126ceba2522fcce9b7a6e3ecc7c3978b808 (patch) | |
tree | 9aaefe97fc89f61e725c9fabcc92901762d90adb | |
parent | 8a81ec9046e39182f46bec19ef03998b5e55ff33 (diff) | |
download | vala-bedd5126ceba2522fcce9b7a6e3ecc7c3978b808.tar.gz |
vala: Add GenericSymbol interface for symbols supporting type-parameters
-rw-r--r-- | vala/Makefile.am | 1 | ||||
-rw-r--r-- | vala/valadatatype.vala | 8 | ||||
-rw-r--r-- | vala/valadelegate.vala | 2 | ||||
-rw-r--r-- | vala/valagenericsymbol.vala | 56 | ||||
-rw-r--r-- | vala/valamethod.vala | 2 | ||||
-rw-r--r-- | vala/valaobjecttypesymbol.vala | 2 | ||||
-rw-r--r-- | vala/valastruct.vala | 6 | ||||
-rw-r--r-- | vala/valasymbol.vala | 10 |
8 files changed, 69 insertions, 18 deletions
diff --git a/vala/Makefile.am b/vala/Makefile.am index 09f3c0672..8f9f81cf2 100644 --- a/vala/Makefile.am +++ b/vala/Makefile.am @@ -93,6 +93,7 @@ libvala_la_VALASOURCES = \ valagirparser.vala \ valagenericdestroyfield.vala \ valagenericdupfield.vala \ + valagenericsymbol.vala \ valagenerictype.vala \ valagenieparser.vala \ valageniescanner.vala \ diff --git a/vala/valadatatype.vala b/vala/valadatatype.vala index a7ba03856..fec7e9010 100644 --- a/vala/valadatatype.vala +++ b/vala/valadatatype.vala @@ -676,12 +676,8 @@ public abstract class Vala.DataType : CodeNode { 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; + if (type_symbol is GenericSymbol) { + expected_n_type_args = ((GenericSymbol) 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; diff --git a/vala/valadelegate.vala b/vala/valadelegate.vala index cb53d2e91..97d61c010 100644 --- a/vala/valadelegate.vala +++ b/vala/valadelegate.vala @@ -25,7 +25,7 @@ using GLib; /** * Represents a function callback type. */ -public class Vala.Delegate : TypeSymbol, Callable { +public class Vala.Delegate : TypeSymbol, Callable, GenericSymbol { /** * The return type of this callback. */ diff --git a/vala/valagenericsymbol.vala b/vala/valagenericsymbol.vala new file mode 100644 index 000000000..54de02145 --- /dev/null +++ b/vala/valagenericsymbol.vala @@ -0,0 +1,56 @@ +/* valagenericsymbol.vala + * + * Copyright (C) 2023 Rico Tzschichholz + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Author: + * Rico Tzschichholz <ricotz@ubuntu.com> + */ + +using GLib; + +/** + * The interface for symbols which support type parameters. + */ +public interface Vala.GenericSymbol : Symbol { + /** + * Appends the specified parameter to the list of type parameters. + * + * @param p a type parameter + */ + public abstract void add_type_parameter (TypeParameter p); + + /** + * Returns the type parameter list. + * + * @return list of type parameters + */ + public abstract unowned List<TypeParameter> get_type_parameters (); + + /** + * Returns whether this symbol has type parameters. + * + * @return true if there are type parameters + */ + public abstract bool has_type_parameters (); + + /** + * Returns the index of the type parameter with the given name. + * + * @return index of a type parameter, or -1 + */ + public abstract int get_type_parameter_index (string name); +} diff --git a/vala/valamethod.vala b/vala/valamethod.vala index 8c5c3af56..c95d3ae43 100644 --- a/vala/valamethod.vala +++ b/vala/valamethod.vala @@ -27,7 +27,7 @@ using GLib; /** * Represents a type or namespace method. */ -public class Vala.Method : Subroutine, Callable { +public class Vala.Method : Subroutine, Callable, GenericSymbol { List<TypeParameter> type_parameters; /** diff --git a/vala/valaobjecttypesymbol.vala b/vala/valaobjecttypesymbol.vala index 97f1f0f2c..7594d4657 100644 --- a/vala/valaobjecttypesymbol.vala +++ b/vala/valaobjecttypesymbol.vala @@ -28,7 +28,7 @@ * be defined in Vala source code or imported from an external library with a * Vala API file. */ -public abstract class Vala.ObjectTypeSymbol : TypeSymbol { +public abstract class Vala.ObjectTypeSymbol : TypeSymbol, GenericSymbol { private List<TypeParameter> type_parameters = new ArrayList<TypeParameter> (); private List<Symbol> members = new ArrayList<Symbol> (); diff --git a/vala/valastruct.vala b/vala/valastruct.vala index 5cce4b24b..198ff3462 100644 --- a/vala/valastruct.vala +++ b/vala/valastruct.vala @@ -25,7 +25,7 @@ using GLib; /** * Represents a struct declaration in the source code. */ -public class Vala.Struct : TypeSymbol { +public class Vala.Struct : TypeSymbol, GenericSymbol { private List<TypeParameter> type_parameters = new ArrayList<TypeParameter> (); private List<Constant> constants = new ArrayList<Constant> (); private List<Field> fields = new ArrayList<Field> (); @@ -187,6 +187,10 @@ public class Vala.Struct : TypeSymbol { return type_parameters; } + public bool has_type_parameters () { + return (type_parameters != null && type_parameters.size > 0); + } + /** * Adds the specified constant as a member to this struct. * diff --git a/vala/valasymbol.vala b/vala/valasymbol.vala index ebee414ec..3dc9ea7c5 100644 --- a/vala/valasymbol.vala +++ b/vala/valasymbol.vala @@ -511,14 +511,8 @@ public abstract class Vala.Symbol : CodeNode { var builder = new StringBuilder (get_full_name ()); unowned List<TypeParameter>? type_params = null; - if (this is Delegate) { - type_params = ((Delegate) this).get_type_parameters (); - } else if (this is Method) { - type_params = ((Method) this).get_type_parameters (); - } else if (this is ObjectTypeSymbol) { - type_params = ((ObjectTypeSymbol) this).get_type_parameters (); - } else if (this is Struct) { - type_params = ((Struct) this).get_type_parameters (); + if (this is GenericSymbol) { + type_params = ((GenericSymbol) this).get_type_parameters (); } if (type_params != null && type_params.size > 0) { builder.append_c ('<'); |