summaryrefslogtreecommitdiff
path: root/vala/valatypeparameter.vala
blob: dd96dcd50b199ddea4617b9d3f3cac6d2c28fdd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* valatypeparameter.vala
 *
 * Copyright (C) 2006-2009  Jürg Billeter
 *
 * 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:
 * 	Jürg Billeter <j@bitron.ch>
 */

using GLib;

/**
 * Represents a generic type parameter in the source code.
 */
public class Vala.TypeParameter : TypeSymbol {
	List<DataType> type_constraint_list;
	static List<DataType> _empty_type_list;

	/**
	 * Creates a new generic type parameter.
	 *
	 * @param name              parameter name
	 * @param source_reference  reference to source code
	 * @return                  newly created generic type parameter
	 */
	public TypeParameter (string name, SourceReference? source_reference = null) {
		base (name, source_reference);
		access = SymbolAccessibility.PUBLIC;
	}

	/**
	 * Appends the specified type as generic type constraint.
	 *
	 * @param arg a type reference
	 */
	public void add_type_constraint (DataType arg) {
		if (type_constraint_list == null) {
			type_constraint_list = new ArrayList<DataType> ();
		}
		type_constraint_list.add (arg);
		arg.parent_node = this;
	}

	/**
	 * Returns the list of generic type constraints.
	 *
	 * @return type constraint list
	 */
	public unowned List<DataType> get_type_constraints () {
		if (type_constraint_list != null) {
			if (type_constraint_list.size > 0 && type_constraint_list[0] is GenericType) {
				return ((GenericType) type_constraint_list[0]).type_parameter.get_type_constraints ();
			}
			return type_constraint_list;
		}
		if (_empty_type_list == null) {
			_empty_type_list = new ArrayList<DataType> ();
		}
		return _empty_type_list;
	}

	public bool has_type_constraints () {
		if (type_constraint_list == null) {
			return false;
		}

		return type_constraint_list.size > 0;
	}

	public DataType? get_constrained_type () {
		if (!has_type_constraints ()) {
			return null;
		}

		unowned List<DataType> type_constraints = get_type_constraints ();
		if (type_constraints.size == 1) {
			return type_constraints[0].copy ();
		}
		foreach (DataType type_constraint in type_constraints) {
			if (type_constraint is ClassType) {
				return type_constraint.copy ();
			} else if (type_constraint is InterfaceType) {
				//FIXME Represent all given interfaces
				return type_constraint.copy ();
			}
		}

		return null;
	}

	public override void accept (CodeVisitor visitor) {
		visitor.visit_type_parameter (this);
	}

	public override void accept_children (CodeVisitor visitor) {
		if (type_constraint_list != null && type_constraint_list.size > 0) {
			foreach (DataType type_constraint in type_constraint_list) {
				type_constraint.accept (visitor);
			}
		}
	}

	public override void replace_type (DataType old_type, DataType new_type) {
		if (type_constraint_list != null) {
			for (int i = 0; i < type_constraint_list.size; i++) {
				if (type_constraint_list[i] == old_type) {
					type_constraint_list[i] = new_type;
					return;
				}
			}
		}
	}

	/**
	 * Checks two type parameters for equality.
	 *
	 * @param param2 a type parameter
	 * @return      true if this type parameter is equal to param2, false
	 *              otherwise
	 */
	public bool equals (TypeParameter param2) {
		/* only type parameters with a common scope are comparable */
		if (!owner.is_subscope_of (param2.owner) && !param2.owner.is_subscope_of (owner)) {
			Report.error (source_reference, "internal error: comparing type parameters from different scopes");
			return false;
		}

		return name == param2.name && parent_symbol == param2.parent_symbol;
	}
}