]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Add GenericSymbol interface for symbols supporting type-parameters
authorRico Tzschichholz <ricotz@ubuntu.com>
Mon, 10 Apr 2023 15:13:35 +0000 (17:13 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Tue, 11 Apr 2023 07:31:39 +0000 (09:31 +0200)
vala/Makefile.am
vala/valadatatype.vala
vala/valadelegate.vala
vala/valagenericsymbol.vala [new file with mode: 0644]
vala/valamethod.vala
vala/valaobjecttypesymbol.vala
vala/valastruct.vala
vala/valasymbol.vala

index 09f3c0672d136736fa78b9b4b248df4b4e2d6053..8f9f81cf288bfc1368764a71448aaf42cca714dc 100644 (file)
@@ -93,6 +93,7 @@ libvala_la_VALASOURCES = \
        valagirparser.vala \
        valagenericdestroyfield.vala \
        valagenericdupfield.vala \
+       valagenericsymbol.vala \
        valagenerictype.vala \
        valagenieparser.vala \
        valageniescanner.vala \
index a7ba03856c76248657d72b7cd3f7fa4c2fa410c8..fec7e901018276e2f152b205cb1f1425accfd37e 100644 (file)
@@ -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;
index cb53d2e9194954d60160f4f9880af83fd12b534b..97d61c0109114ec5531437dfaaa97c3225ebb88c 100644 (file)
@@ -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 (file)
index 0000000..54de021
--- /dev/null
@@ -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);
+}
index 8c5c3af560c08b0c2133659c30b3bcdacc645a69..c95d3ae434a37699ffd1035919fee50f770b2e28 100644 (file)
@@ -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;
 
        /**
index 97f1f0f2c978f2d45def9fd32decf0b3d2d07ad4..7594d465723b9bb204a6f9d1e2767315723b2a62 100644 (file)
@@ -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> ();
index 5cce4b24b57a05ae6e0096bec9ae8881620d7908..198ff3462343f807d54fac7b055b597bc3381b61 100644 (file)
@@ -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.
         *
index ebee414ec399b745c25ec517230512b7a5419615..3dc9ea7c5e6c8f928e4babee47046aa39666bd84 100644 (file)
@@ -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 ('<');