author | mcimadamore |
Tue, 12 Mar 2013 16:02:43 +0000 | |
changeset 16341 | cc4b4e3c0f12 |
parent 15705 | c4124695db0c |
child 16556 | f4adc5bb4652 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
15385 | 2 |
* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. |
10 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5520 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.code; |
|
27 |
||
9599
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
28 |
import java.util.Collections; |
14048
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
29 |
import java.util.EnumMap; |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
30 |
import java.util.EnumSet; |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
31 |
import java.util.Map; |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
32 |
import java.util.Set; |
14052 | 33 |
|
15385 | 34 |
import javax.lang.model.element.AnnotationMirror; |
10 | 35 |
import javax.lang.model.type.*; |
36 |
||
14258 | 37 |
import com.sun.tools.javac.code.Symbol.*; |
38 |
import com.sun.tools.javac.util.*; |
|
14052 | 39 |
import static com.sun.tools.javac.code.BoundKind.*; |
10 | 40 |
import static com.sun.tools.javac.code.Flags.*; |
41 |
import static com.sun.tools.javac.code.Kinds.*; |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
42 |
import static com.sun.tools.javac.code.TypeTag.*; |
10 | 43 |
|
44 |
/** This class represents Java types. The class itself defines the behavior of |
|
45 |
* the following types: |
|
46 |
* <pre> |
|
47 |
* base types (tags: BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, BOOLEAN), |
|
48 |
* type `void' (tag: VOID), |
|
49 |
* the bottom type (tag: BOT), |
|
50 |
* the missing type (tag: NONE). |
|
51 |
* </pre> |
|
52 |
* <p>The behavior of the following types is defined in subclasses, which are |
|
53 |
* all static inner classes of this class: |
|
54 |
* <pre> |
|
55 |
* class types (tag: CLASS, class: ClassType), |
|
56 |
* array types (tag: ARRAY, class: ArrayType), |
|
57 |
* method types (tag: METHOD, class: MethodType), |
|
58 |
* package types (tag: PACKAGE, class: PackageType), |
|
59 |
* type variables (tag: TYPEVAR, class: TypeVar), |
|
60 |
* type arguments (tag: WILDCARD, class: WildcardType), |
|
12915
28cf1e0dafdc
7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents:
12468
diff
changeset
|
61 |
* generic method types (tag: FORALL, class: ForAll), |
10 | 62 |
* the error type (tag: ERROR, class: ErrorType). |
63 |
* </pre> |
|
64 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
65 |
* <p><b>This is NOT part of any supported API. |
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
66 |
* If you write code that depends on this, you do so at your own risk. |
10 | 67 |
* This code and its internal interfaces are subject to change or |
68 |
* deletion without notice.</b> |
|
69 |
* |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
70 |
* @see TypeTag |
10 | 71 |
*/ |
72 |
public class Type implements PrimitiveType { |
|
73 |
||
74 |
/** Constant type: no type at all. */ |
|
75 |
public static final JCNoType noType = new JCNoType(NONE); |
|
76 |
||
14057
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14052
diff
changeset
|
77 |
/** Constant type: special type to be used during recovery of deferred expressions. */ |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14052
diff
changeset
|
78 |
public static final JCNoType recoveryType = new JCNoType(NONE); |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14052
diff
changeset
|
79 |
|
10 | 80 |
/** If this switch is turned on, the names of type variables |
81 |
* and anonymous classes are printed with hashcodes appended. |
|
82 |
*/ |
|
83 |
public static boolean moreInfo = false; |
|
84 |
||
85 |
/** The tag of this type. |
|
86 |
* |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
87 |
* @see TypeTag |
10 | 88 |
*/ |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
89 |
protected TypeTag tag; |
10 | 90 |
|
15385 | 91 |
/** The defining class / interface / package / type variable. |
10 | 92 |
*/ |
93 |
public TypeSymbol tsym; |
|
94 |
||
95 |
/** |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
96 |
* Checks if the current type tag is equal to the given tag. |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
97 |
* @return true if tag is equal to the current type tag. |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
98 |
*/ |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
99 |
public boolean hasTag(TypeTag tag) { |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
100 |
return this.tag == tag; |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
101 |
} |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
102 |
|
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
103 |
/** |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
104 |
* Returns the current type tag. |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
105 |
* @return the value of the current type tag. |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
106 |
*/ |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
107 |
public TypeTag getTag() { |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
108 |
return tag; |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
109 |
} |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
110 |
|
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
111 |
public boolean isNumeric() { |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
112 |
switch (tag) { |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
113 |
case BYTE: case CHAR: |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
114 |
case SHORT: |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
115 |
case INT: case LONG: |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
116 |
case FLOAT: case DOUBLE: |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
117 |
return true; |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
118 |
default: |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
119 |
return false; |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
120 |
} |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
121 |
} |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
122 |
|
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
123 |
public boolean isPrimitive() { |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
124 |
return (isNumeric() || tag == BOOLEAN); |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
125 |
} |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
126 |
|
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
127 |
public boolean isPrimitiveOrVoid() { |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
128 |
return (isPrimitive() || tag == VOID); |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
129 |
} |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
130 |
|
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
131 |
public boolean isReference() { |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
132 |
switch (tag) { |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
133 |
case CLASS: |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
134 |
case ARRAY: |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
135 |
case TYPEVAR: |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
136 |
case WILDCARD: |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
137 |
case ERROR: |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
138 |
return true; |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
139 |
default: |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
140 |
return false; |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
141 |
} |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
142 |
} |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
143 |
|
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
144 |
public boolean isNullOrReference() { |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
145 |
return (tag == BOT || isReference()); |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
146 |
} |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
147 |
|
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
148 |
public boolean isPartial() { |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
149 |
switch(tag) { |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
150 |
case ERROR: case UNKNOWN: case UNDETVAR: |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
151 |
return true; |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
152 |
default: |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
153 |
return false; |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
154 |
} |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
155 |
} |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
156 |
|
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
157 |
/** |
10 | 158 |
* The constant value of this type, null if this type does not |
159 |
* have a constant value attribute. Only primitive types and |
|
160 |
* strings (ClassType) can have a constant value attribute. |
|
161 |
* @return the constant value attribute of this type |
|
162 |
*/ |
|
163 |
public Object constValue() { |
|
164 |
return null; |
|
165 |
} |
|
166 |
||
8625
6b51ef804d49
6639645: Modeling type implementing missing interfaces
jjg
parents:
8430
diff
changeset
|
167 |
/** |
6b51ef804d49
6639645: Modeling type implementing missing interfaces
jjg
parents:
8430
diff
changeset
|
168 |
* Get the representation of this type used for modelling purposes. |
6b51ef804d49
6639645: Modeling type implementing missing interfaces
jjg
parents:
8430
diff
changeset
|
169 |
* By default, this is itself. For ErrorType, a different value |
15385 | 170 |
* may be provided. |
8625
6b51ef804d49
6639645: Modeling type implementing missing interfaces
jjg
parents:
8430
diff
changeset
|
171 |
*/ |
6b51ef804d49
6639645: Modeling type implementing missing interfaces
jjg
parents:
8430
diff
changeset
|
172 |
public Type getModelType() { |
6b51ef804d49
6639645: Modeling type implementing missing interfaces
jjg
parents:
8430
diff
changeset
|
173 |
return this; |
6b51ef804d49
6639645: Modeling type implementing missing interfaces
jjg
parents:
8430
diff
changeset
|
174 |
} |
6b51ef804d49
6639645: Modeling type implementing missing interfaces
jjg
parents:
8430
diff
changeset
|
175 |
|
6b51ef804d49
6639645: Modeling type implementing missing interfaces
jjg
parents:
8430
diff
changeset
|
176 |
public static List<Type> getModelTypes(List<Type> ts) { |
6b51ef804d49
6639645: Modeling type implementing missing interfaces
jjg
parents:
8430
diff
changeset
|
177 |
ListBuffer<Type> lb = new ListBuffer<Type>(); |
6b51ef804d49
6639645: Modeling type implementing missing interfaces
jjg
parents:
8430
diff
changeset
|
178 |
for (Type t: ts) |
6b51ef804d49
6639645: Modeling type implementing missing interfaces
jjg
parents:
8430
diff
changeset
|
179 |
lb.append(t.getModelType()); |
6b51ef804d49
6639645: Modeling type implementing missing interfaces
jjg
parents:
8430
diff
changeset
|
180 |
return lb.toList(); |
6b51ef804d49
6639645: Modeling type implementing missing interfaces
jjg
parents:
8430
diff
changeset
|
181 |
} |
6b51ef804d49
6639645: Modeling type implementing missing interfaces
jjg
parents:
8430
diff
changeset
|
182 |
|
10 | 183 |
public <R,S> R accept(Type.Visitor<R,S> v, S s) { return v.visitType(this, s); } |
184 |
||
185 |
/** Define a type given its tag and type symbol |
|
186 |
*/ |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
187 |
public Type(TypeTag tag, TypeSymbol tsym) { |
10 | 188 |
this.tag = tag; |
189 |
this.tsym = tsym; |
|
190 |
} |
|
191 |
||
192 |
/** An abstract class for mappings from types to types |
|
193 |
*/ |
|
194 |
public static abstract class Mapping { |
|
195 |
private String name; |
|
196 |
public Mapping(String name) { |
|
197 |
this.name = name; |
|
198 |
} |
|
199 |
public abstract Type apply(Type t); |
|
200 |
public String toString() { |
|
201 |
return name; |
|
202 |
} |
|
203 |
} |
|
204 |
||
205 |
/** map a type function over all immediate descendants of this type |
|
206 |
*/ |
|
207 |
public Type map(Mapping f) { |
|
208 |
return this; |
|
209 |
} |
|
210 |
||
211 |
/** map a type function over a list of types |
|
212 |
*/ |
|
213 |
public static List<Type> map(List<Type> ts, Mapping f) { |
|
214 |
if (ts.nonEmpty()) { |
|
215 |
List<Type> tail1 = map(ts.tail, f); |
|
216 |
Type t = f.apply(ts.head); |
|
217 |
if (tail1 != ts.tail || t != ts.head) |
|
218 |
return tail1.prepend(t); |
|
219 |
} |
|
220 |
return ts; |
|
221 |
} |
|
222 |
||
223 |
/** Define a constant type, of the same kind as this type |
|
224 |
* and with given constant value |
|
225 |
*/ |
|
226 |
public Type constType(Object constValue) { |
|
227 |
final Object value = constValue; |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
228 |
Assert.check(isPrimitive()); |
10 | 229 |
return new Type(tag, tsym) { |
230 |
@Override |
|
231 |
public Object constValue() { |
|
232 |
return value; |
|
233 |
} |
|
234 |
@Override |
|
235 |
public Type baseType() { |
|
236 |
return tsym.type; |
|
237 |
} |
|
238 |
}; |
|
239 |
} |
|
240 |
||
241 |
/** |
|
242 |
* If this is a constant type, return its underlying type. |
|
243 |
* Otherwise, return the type itself. |
|
244 |
*/ |
|
245 |
public Type baseType() { |
|
246 |
return this; |
|
247 |
} |
|
248 |
||
15385 | 249 |
/** |
250 |
* If this is an annotated type, return the underlying type. |
|
251 |
* Otherwise, return the type itself. |
|
252 |
*/ |
|
253 |
public Type unannotatedType() { |
|
254 |
return this; |
|
255 |
} |
|
256 |
||
10 | 257 |
/** Return the base types of a list of types. |
258 |
*/ |
|
259 |
public static List<Type> baseTypes(List<Type> ts) { |
|
260 |
if (ts.nonEmpty()) { |
|
261 |
Type t = ts.head.baseType(); |
|
262 |
List<Type> baseTypes = baseTypes(ts.tail); |
|
263 |
if (t != ts.head || baseTypes != ts.tail) |
|
264 |
return baseTypes.prepend(t); |
|
265 |
} |
|
266 |
return ts; |
|
267 |
} |
|
268 |
||
269 |
/** The Java source which this type represents. |
|
270 |
*/ |
|
271 |
public String toString() { |
|
272 |
String s = (tsym == null || tsym.name == null) |
|
273 |
? "<none>" |
|
274 |
: tsym.name.toString(); |
|
275 |
if (moreInfo && tag == TYPEVAR) s = s + hashCode(); |
|
276 |
return s; |
|
277 |
} |
|
278 |
||
279 |
/** |
|
280 |
* The Java source which this type list represents. A List is |
|
281 |
* represented as a comma-spearated listing of the elements in |
|
282 |
* that list. |
|
283 |
*/ |
|
284 |
public static String toString(List<Type> ts) { |
|
285 |
if (ts.isEmpty()) { |
|
286 |
return ""; |
|
287 |
} else { |
|
8625
6b51ef804d49
6639645: Modeling type implementing missing interfaces
jjg
parents:
8430
diff
changeset
|
288 |
StringBuilder buf = new StringBuilder(); |
10 | 289 |
buf.append(ts.head.toString()); |
290 |
for (List<Type> l = ts.tail; l.nonEmpty(); l = l.tail) |
|
291 |
buf.append(",").append(l.head.toString()); |
|
292 |
return buf.toString(); |
|
293 |
} |
|
294 |
} |
|
295 |
||
296 |
/** |
|
297 |
* The constant value of this type, converted to String |
|
298 |
*/ |
|
299 |
public String stringValue() { |
|
8032 | 300 |
Object cv = Assert.checkNonNull(constValue()); |
10 | 301 |
if (tag == BOOLEAN) |
8032 | 302 |
return ((Integer) cv).intValue() == 0 ? "false" : "true"; |
10 | 303 |
else if (tag == CHAR) |
8032 | 304 |
return String.valueOf((char) ((Integer) cv).intValue()); |
10 | 305 |
else |
8032 | 306 |
return cv.toString(); |
10 | 307 |
} |
308 |
||
309 |
/** |
|
310 |
* This method is analogous to isSameType, but weaker, since we |
|
311 |
* never complete classes. Where isSameType would complete a |
|
312 |
* class, equals assumes that the two types are different. |
|
313 |
*/ |
|
14949
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14725
diff
changeset
|
314 |
@Override |
10 | 315 |
public boolean equals(Object t) { |
316 |
return super.equals(t); |
|
317 |
} |
|
318 |
||
14949
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14725
diff
changeset
|
319 |
@Override |
10 | 320 |
public int hashCode() { |
321 |
return super.hashCode(); |
|
322 |
} |
|
323 |
||
324 |
/** Is this a constant type whose value is false? |
|
325 |
*/ |
|
326 |
public boolean isFalse() { |
|
327 |
return |
|
328 |
tag == BOOLEAN && |
|
329 |
constValue() != null && |
|
330 |
((Integer)constValue()).intValue() == 0; |
|
331 |
} |
|
332 |
||
333 |
/** Is this a constant type whose value is true? |
|
334 |
*/ |
|
335 |
public boolean isTrue() { |
|
336 |
return |
|
337 |
tag == BOOLEAN && |
|
338 |
constValue() != null && |
|
339 |
((Integer)constValue()).intValue() != 0; |
|
340 |
} |
|
341 |
||
342 |
public String argtypes(boolean varargs) { |
|
343 |
List<Type> args = getParameterTypes(); |
|
344 |
if (!varargs) return args.toString(); |
|
7637 | 345 |
StringBuilder buf = new StringBuilder(); |
10 | 346 |
while (args.tail.nonEmpty()) { |
347 |
buf.append(args.head); |
|
348 |
args = args.tail; |
|
349 |
buf.append(','); |
|
350 |
} |
|
15385 | 351 |
if (args.head.unannotatedType().tag == ARRAY) { |
352 |
buf.append(((ArrayType)args.head.unannotatedType()).elemtype); |
|
353 |
if (args.head.getAnnotations().nonEmpty()) { |
|
354 |
buf.append(args.head.getAnnotations()); |
|
355 |
} |
|
10 | 356 |
buf.append("..."); |
357 |
} else { |
|
358 |
buf.append(args.head); |
|
359 |
} |
|
360 |
return buf.toString(); |
|
361 |
} |
|
362 |
||
363 |
/** Access methods. |
|
364 |
*/ |
|
15385 | 365 |
public List<? extends AnnotationMirror> getAnnotations() { return List.nil(); } |
10 | 366 |
public List<Type> getTypeArguments() { return List.nil(); } |
15385 | 367 |
public Type getEnclosingType() { return null; } |
10 | 368 |
public List<Type> getParameterTypes() { return List.nil(); } |
369 |
public Type getReturnType() { return null; } |
|
15385 | 370 |
public Type getReceiverType() { return null; } |
10 | 371 |
public List<Type> getThrownTypes() { return List.nil(); } |
372 |
public Type getUpperBound() { return null; } |
|
373 |
public Type getLowerBound() { return null; } |
|
374 |
||
375 |
/** Navigation methods, these will work for classes, type variables, |
|
376 |
* foralls, but will return null for arrays and methods. |
|
377 |
*/ |
|
378 |
||
379 |
/** Return all parameters of this type and all its outer types in order |
|
380 |
* outer (first) to inner (last). |
|
381 |
*/ |
|
382 |
public List<Type> allparams() { return List.nil(); } |
|
383 |
||
384 |
/** Does this type contain "error" elements? |
|
385 |
*/ |
|
386 |
public boolean isErroneous() { |
|
387 |
return false; |
|
388 |
} |
|
389 |
||
390 |
public static boolean isErroneous(List<Type> ts) { |
|
391 |
for (List<Type> l = ts; l.nonEmpty(); l = l.tail) |
|
392 |
if (l.head.isErroneous()) return true; |
|
393 |
return false; |
|
394 |
} |
|
395 |
||
396 |
/** Is this type parameterized? |
|
397 |
* A class type is parameterized if it has some parameters. |
|
398 |
* An array type is parameterized if its element type is parameterized. |
|
399 |
* All other types are not parameterized. |
|
400 |
*/ |
|
401 |
public boolean isParameterized() { |
|
402 |
return false; |
|
403 |
} |
|
404 |
||
405 |
/** Is this type a raw type? |
|
406 |
* A class type is a raw type if it misses some of its parameters. |
|
407 |
* An array type is a raw type if its element type is raw. |
|
408 |
* All other types are not raw. |
|
409 |
* Type validation will ensure that the only raw types |
|
410 |
* in a program are types that miss all their type variables. |
|
411 |
*/ |
|
412 |
public boolean isRaw() { |
|
413 |
return false; |
|
414 |
} |
|
415 |
||
416 |
public boolean isCompound() { |
|
417 |
return tsym.completer == null |
|
418 |
// Compound types can't have a completer. Calling |
|
419 |
// flags() will complete the symbol causing the |
|
420 |
// compiler to load classes unnecessarily. This led |
|
421 |
// to regression 6180021. |
|
422 |
&& (tsym.flags() & COMPOUND) != 0; |
|
423 |
} |
|
424 |
||
425 |
public boolean isInterface() { |
|
426 |
return (tsym.flags() & INTERFACE) != 0; |
|
427 |
} |
|
428 |
||
6353
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6348
diff
changeset
|
429 |
public boolean isFinal() { |
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6348
diff
changeset
|
430 |
return (tsym.flags() & FINAL) != 0; |
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6348
diff
changeset
|
431 |
} |
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6348
diff
changeset
|
432 |
|
10 | 433 |
/** |
434 |
* Does this type contain occurrences of type t? |
|
435 |
*/ |
|
436 |
public boolean contains(Type t) { |
|
437 |
return t == this; |
|
438 |
} |
|
439 |
||
440 |
public static boolean contains(List<Type> ts, Type t) { |
|
441 |
for (List<Type> l = ts; |
|
442 |
l.tail != null /*inlined: l.nonEmpty()*/; |
|
443 |
l = l.tail) |
|
444 |
if (l.head.contains(t)) return true; |
|
445 |
return false; |
|
446 |
} |
|
447 |
||
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
5847
diff
changeset
|
448 |
/** Does this type contain an occurrence of some type in 'ts'? |
10 | 449 |
*/ |
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
5847
diff
changeset
|
450 |
public boolean containsAny(List<Type> ts) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
5847
diff
changeset
|
451 |
for (Type t : ts) |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
5847
diff
changeset
|
452 |
if (this.contains(t)) return true; |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
5847
diff
changeset
|
453 |
return false; |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
5847
diff
changeset
|
454 |
} |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
5847
diff
changeset
|
455 |
|
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
5847
diff
changeset
|
456 |
public static boolean containsAny(List<Type> ts1, List<Type> ts2) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
5847
diff
changeset
|
457 |
for (Type t : ts1) |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
5847
diff
changeset
|
458 |
if (t.containsAny(ts2)) return true; |
10 | 459 |
return false; |
460 |
} |
|
461 |
||
8044
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8032
diff
changeset
|
462 |
public static List<Type> filter(List<Type> ts, Filter<Type> tf) { |
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8032
diff
changeset
|
463 |
ListBuffer<Type> buf = ListBuffer.lb(); |
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8032
diff
changeset
|
464 |
for (Type t : ts) { |
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8032
diff
changeset
|
465 |
if (tf.accepts(t)) { |
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8032
diff
changeset
|
466 |
buf.append(t); |
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8032
diff
changeset
|
467 |
} |
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8032
diff
changeset
|
468 |
} |
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8032
diff
changeset
|
469 |
return buf.toList(); |
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8032
diff
changeset
|
470 |
} |
7fd529d4472c
6943278: spurious error message for inference and type-variable with erroneous bound
mcimadamore
parents:
8032
diff
changeset
|
471 |
|
10 | 472 |
public boolean isSuperBound() { return false; } |
473 |
public boolean isExtendsBound() { return false; } |
|
474 |
public boolean isUnbound() { return false; } |
|
475 |
public Type withTypeVar(Type t) { return this; } |
|
476 |
||
477 |
/** The underlying method type of this type. |
|
478 |
*/ |
|
479 |
public MethodType asMethodType() { throw new AssertionError(); } |
|
480 |
||
481 |
/** Complete loading all classes in this type. |
|
482 |
*/ |
|
483 |
public void complete() {} |
|
484 |
||
485 |
public TypeSymbol asElement() { |
|
486 |
return tsym; |
|
487 |
} |
|
488 |
||
489 |
public TypeKind getKind() { |
|
490 |
switch (tag) { |
|
491 |
case BYTE: return TypeKind.BYTE; |
|
492 |
case CHAR: return TypeKind.CHAR; |
|
493 |
case SHORT: return TypeKind.SHORT; |
|
494 |
case INT: return TypeKind.INT; |
|
495 |
case LONG: return TypeKind.LONG; |
|
496 |
case FLOAT: return TypeKind.FLOAT; |
|
497 |
case DOUBLE: return TypeKind.DOUBLE; |
|
498 |
case BOOLEAN: return TypeKind.BOOLEAN; |
|
499 |
case VOID: return TypeKind.VOID; |
|
500 |
case BOT: return TypeKind.NULL; |
|
501 |
case NONE: return TypeKind.NONE; |
|
502 |
default: return TypeKind.OTHER; |
|
503 |
} |
|
504 |
} |
|
505 |
||
506 |
public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
|
507 |
if (isPrimitive()) |
|
508 |
return v.visitPrimitive(this, p); |
|
509 |
else |
|
510 |
throw new AssertionError(); |
|
511 |
} |
|
512 |
||
513 |
public static class WildcardType extends Type |
|
514 |
implements javax.lang.model.type.WildcardType { |
|
515 |
||
516 |
public Type type; |
|
517 |
public BoundKind kind; |
|
518 |
public TypeVar bound; |
|
519 |
||
520 |
@Override |
|
521 |
public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
|
522 |
return v.visitWildcardType(this, s); |
|
523 |
} |
|
524 |
||
525 |
public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) { |
|
526 |
super(WILDCARD, tsym); |
|
8032 | 527 |
this.type = Assert.checkNonNull(type); |
10 | 528 |
this.kind = kind; |
529 |
} |
|
530 |
public WildcardType(WildcardType t, TypeVar bound) { |
|
531 |
this(t.type, t.kind, t.tsym, bound); |
|
532 |
} |
|
533 |
||
534 |
public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, TypeVar bound) { |
|
535 |
this(type, kind, tsym); |
|
536 |
this.bound = bound; |
|
537 |
} |
|
538 |
||
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
5847
diff
changeset
|
539 |
public boolean contains(Type t) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
5847
diff
changeset
|
540 |
return kind != UNBOUND && type.contains(t); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
5847
diff
changeset
|
541 |
} |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
5847
diff
changeset
|
542 |
|
10 | 543 |
public boolean isSuperBound() { |
544 |
return kind == SUPER || |
|
545 |
kind == UNBOUND; |
|
546 |
} |
|
547 |
public boolean isExtendsBound() { |
|
548 |
return kind == EXTENDS || |
|
549 |
kind == UNBOUND; |
|
550 |
} |
|
551 |
public boolean isUnbound() { |
|
552 |
return kind == UNBOUND; |
|
553 |
} |
|
554 |
||
555 |
public Type withTypeVar(Type t) { |
|
556 |
//-System.err.println(this+".withTypeVar("+t+");");//DEBUG |
|
557 |
if (bound == t) |
|
558 |
return this; |
|
559 |
bound = (TypeVar)t; |
|
560 |
return this; |
|
561 |
} |
|
562 |
||
563 |
boolean isPrintingBound = false; |
|
564 |
public String toString() { |
|
8625
6b51ef804d49
6639645: Modeling type implementing missing interfaces
jjg
parents:
8430
diff
changeset
|
565 |
StringBuilder s = new StringBuilder(); |
10 | 566 |
s.append(kind.toString()); |
567 |
if (kind != UNBOUND) |
|
568 |
s.append(type); |
|
569 |
if (moreInfo && bound != null && !isPrintingBound) |
|
570 |
try { |
|
571 |
isPrintingBound = true; |
|
572 |
s.append("{:").append(bound.bound).append(":}"); |
|
573 |
} finally { |
|
574 |
isPrintingBound = false; |
|
575 |
} |
|
576 |
return s.toString(); |
|
577 |
} |
|
578 |
||
579 |
public Type map(Mapping f) { |
|
580 |
//- System.err.println(" (" + this + ").map(" + f + ")");//DEBUG |
|
581 |
Type t = type; |
|
582 |
if (t != null) |
|
583 |
t = f.apply(t); |
|
584 |
if (t == type) |
|
585 |
return this; |
|
586 |
else |
|
587 |
return new WildcardType(t, kind, tsym, bound); |
|
588 |
} |
|
589 |
||
590 |
public Type getExtendsBound() { |
|
591 |
if (kind == EXTENDS) |
|
592 |
return type; |
|
593 |
else |
|
594 |
return null; |
|
595 |
} |
|
596 |
||
597 |
public Type getSuperBound() { |
|
598 |
if (kind == SUPER) |
|
599 |
return type; |
|
600 |
else |
|
601 |
return null; |
|
602 |
} |
|
603 |
||
604 |
public TypeKind getKind() { |
|
605 |
return TypeKind.WILDCARD; |
|
606 |
} |
|
607 |
||
608 |
public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
|
609 |
return v.visitWildcard(this, p); |
|
610 |
} |
|
611 |
} |
|
612 |
||
613 |
public static class ClassType extends Type implements DeclaredType { |
|
614 |
||
615 |
/** The enclosing type of this type. If this is the type of an inner |
|
616 |
* class, outer_field refers to the type of its enclosing |
|
15385 | 617 |
* instance class, in all other cases it refers to noType. |
10 | 618 |
*/ |
619 |
private Type outer_field; |
|
620 |
||
621 |
/** The type parameters of this type (to be set once class is loaded). |
|
622 |
*/ |
|
623 |
public List<Type> typarams_field; |
|
624 |
||
625 |
/** A cache variable for the type parameters of this type, |
|
626 |
* appended to all parameters of its enclosing class. |
|
627 |
* @see #allparams |
|
628 |
*/ |
|
629 |
public List<Type> allparams_field; |
|
630 |
||
631 |
/** The supertype of this class (to be set once class is loaded). |
|
632 |
*/ |
|
633 |
public Type supertype_field; |
|
634 |
||
635 |
/** The interfaces of this class (to be set once class is loaded). |
|
636 |
*/ |
|
637 |
public List<Type> interfaces_field; |
|
638 |
||
8625
6b51ef804d49
6639645: Modeling type implementing missing interfaces
jjg
parents:
8430
diff
changeset
|
639 |
/** All the interfaces of this class, including missing ones. |
6b51ef804d49
6639645: Modeling type implementing missing interfaces
jjg
parents:
8430
diff
changeset
|
640 |
*/ |
6b51ef804d49
6639645: Modeling type implementing missing interfaces
jjg
parents:
8430
diff
changeset
|
641 |
public List<Type> all_interfaces_field; |
6b51ef804d49
6639645: Modeling type implementing missing interfaces
jjg
parents:
8430
diff
changeset
|
642 |
|
10 | 643 |
public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) { |
644 |
super(CLASS, tsym); |
|
645 |
this.outer_field = outer; |
|
646 |
this.typarams_field = typarams; |
|
647 |
this.allparams_field = null; |
|
648 |
this.supertype_field = null; |
|
649 |
this.interfaces_field = null; |
|
650 |
/* |
|
651 |
// this can happen during error recovery |
|
652 |
assert |
|
653 |
outer.isParameterized() ? |
|
654 |
typarams.length() == tsym.type.typarams().length() : |
|
655 |
outer.isRaw() ? |
|
656 |
typarams.length() == 0 : |
|
657 |
true; |
|
658 |
*/ |
|
659 |
} |
|
660 |
||
661 |
@Override |
|
662 |
public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
|
663 |
return v.visitClassType(this, s); |
|
664 |
} |
|
665 |
||
666 |
public Type constType(Object constValue) { |
|
667 |
final Object value = constValue; |
|
668 |
return new ClassType(getEnclosingType(), typarams_field, tsym) { |
|
669 |
@Override |
|
670 |
public Object constValue() { |
|
671 |
return value; |
|
672 |
} |
|
673 |
@Override |
|
674 |
public Type baseType() { |
|
675 |
return tsym.type; |
|
676 |
} |
|
677 |
}; |
|
678 |
} |
|
679 |
||
680 |
/** The Java source which this type represents. |
|
681 |
*/ |
|
682 |
public String toString() { |
|
8625
6b51ef804d49
6639645: Modeling type implementing missing interfaces
jjg
parents:
8430
diff
changeset
|
683 |
StringBuilder buf = new StringBuilder(); |
10 | 684 |
if (getEnclosingType().tag == CLASS && tsym.owner.kind == TYP) { |
685 |
buf.append(getEnclosingType().toString()); |
|
686 |
buf.append("."); |
|
687 |
buf.append(className(tsym, false)); |
|
688 |
} else { |
|
689 |
buf.append(className(tsym, true)); |
|
690 |
} |
|
691 |
if (getTypeArguments().nonEmpty()) { |
|
692 |
buf.append('<'); |
|
693 |
buf.append(getTypeArguments().toString()); |
|
694 |
buf.append(">"); |
|
695 |
} |
|
696 |
return buf.toString(); |
|
697 |
} |
|
698 |
//where |
|
699 |
private String className(Symbol sym, boolean longform) { |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1257
diff
changeset
|
700 |
if (sym.name.isEmpty() && (sym.flags() & COMPOUND) != 0) { |
8625
6b51ef804d49
6639645: Modeling type implementing missing interfaces
jjg
parents:
8430
diff
changeset
|
701 |
StringBuilder s = new StringBuilder(supertype_field.toString()); |
10 | 702 |
for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) { |
703 |
s.append("&"); |
|
704 |
s.append(is.head.toString()); |
|
705 |
} |
|
706 |
return s.toString(); |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1257
diff
changeset
|
707 |
} else if (sym.name.isEmpty()) { |
10 | 708 |
String s; |
709 |
ClassType norm = (ClassType) tsym.type; |
|
710 |
if (norm == null) { |
|
711 |
s = Log.getLocalizedString("anonymous.class", (Object)null); |
|
712 |
} else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) { |
|
713 |
s = Log.getLocalizedString("anonymous.class", |
|
714 |
norm.interfaces_field.head); |
|
715 |
} else { |
|
716 |
s = Log.getLocalizedString("anonymous.class", |
|
717 |
norm.supertype_field); |
|
718 |
} |
|
719 |
if (moreInfo) |
|
720 |
s += String.valueOf(sym.hashCode()); |
|
721 |
return s; |
|
722 |
} else if (longform) { |
|
723 |
return sym.getQualifiedName().toString(); |
|
724 |
} else { |
|
725 |
return sym.name.toString(); |
|
726 |
} |
|
727 |
} |
|
728 |
||
729 |
public List<Type> getTypeArguments() { |
|
730 |
if (typarams_field == null) { |
|
731 |
complete(); |
|
732 |
if (typarams_field == null) |
|
733 |
typarams_field = List.nil(); |
|
734 |
} |
|
735 |
return typarams_field; |
|
736 |
} |
|
737 |
||
514
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
738 |
public boolean hasErasedSupertypes() { |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
739 |
return isRaw(); |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
740 |
} |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
741 |
|
10 | 742 |
public Type getEnclosingType() { |
743 |
return outer_field; |
|
744 |
} |
|
745 |
||
746 |
public void setEnclosingType(Type outer) { |
|
747 |
outer_field = outer; |
|
748 |
} |
|
749 |
||
750 |
public List<Type> allparams() { |
|
751 |
if (allparams_field == null) { |
|
752 |
allparams_field = getTypeArguments().prependList(getEnclosingType().allparams()); |
|
753 |
} |
|
754 |
return allparams_field; |
|
755 |
} |
|
756 |
||
757 |
public boolean isErroneous() { |
|
758 |
return |
|
759 |
getEnclosingType().isErroneous() || |
|
760 |
isErroneous(getTypeArguments()) || |
|
761 |
this != tsym.type && tsym.type.isErroneous(); |
|
762 |
} |
|
763 |
||
764 |
public boolean isParameterized() { |
|
765 |
return allparams().tail != null; |
|
766 |
// optimization, was: allparams().nonEmpty(); |
|
767 |
} |
|
768 |
||
769 |
/** A cache for the rank. */ |
|
770 |
int rank_field = -1; |
|
771 |
||
772 |
/** A class type is raw if it misses some |
|
773 |
* of its type parameter sections. |
|
774 |
* After validation, this is equivalent to: |
|
13844 | 775 |
* {@code allparams.isEmpty() && tsym.type.allparams.nonEmpty(); } |
10 | 776 |
*/ |
777 |
public boolean isRaw() { |
|
778 |
return |
|
779 |
this != tsym.type && // necessary, but not sufficient condition |
|
780 |
tsym.type.allparams().nonEmpty() && |
|
781 |
allparams().isEmpty(); |
|
782 |
} |
|
783 |
||
784 |
public Type map(Mapping f) { |
|
785 |
Type outer = getEnclosingType(); |
|
786 |
Type outer1 = f.apply(outer); |
|
787 |
List<Type> typarams = getTypeArguments(); |
|
788 |
List<Type> typarams1 = map(typarams, f); |
|
789 |
if (outer1 == outer && typarams1 == typarams) return this; |
|
790 |
else return new ClassType(outer1, typarams1, tsym); |
|
791 |
} |
|
792 |
||
793 |
public boolean contains(Type elem) { |
|
794 |
return |
|
795 |
elem == this |
|
796 |
|| (isParameterized() |
|
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
5847
diff
changeset
|
797 |
&& (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem))) |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
5847
diff
changeset
|
798 |
|| (isCompound() |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
5847
diff
changeset
|
799 |
&& (supertype_field.contains(elem) || contains(interfaces_field, elem))); |
10 | 800 |
} |
801 |
||
802 |
public void complete() { |
|
803 |
if (tsym.completer != null) tsym.complete(); |
|
804 |
} |
|
805 |
||
806 |
public TypeKind getKind() { |
|
807 |
return TypeKind.DECLARED; |
|
808 |
} |
|
809 |
||
810 |
public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
|
811 |
return v.visitDeclared(this, p); |
|
812 |
} |
|
813 |
} |
|
814 |
||
514
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
815 |
public static class ErasedClassType extends ClassType { |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
816 |
public ErasedClassType(Type outer, TypeSymbol tsym) { |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
817 |
super(outer, List.<Type>nil(), tsym); |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
818 |
} |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
819 |
|
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
820 |
@Override |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
821 |
public boolean hasErasedSupertypes() { |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
822 |
return true; |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
823 |
} |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
824 |
} |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
10
diff
changeset
|
825 |
|
9599
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
826 |
// a clone of a ClassType that knows about the alternatives of a union type. |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
827 |
public static class UnionClassType extends ClassType implements UnionType { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
828 |
final List<? extends Type> alternatives_field; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
829 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
830 |
public UnionClassType(ClassType ct, List<? extends Type> alternatives) { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
831 |
super(ct.outer_field, ct.typarams_field, ct.tsym); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
832 |
allparams_field = ct.allparams_field; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
833 |
supertype_field = ct.supertype_field; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
834 |
interfaces_field = ct.interfaces_field; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
835 |
all_interfaces_field = ct.interfaces_field; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
836 |
alternatives_field = alternatives; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
837 |
} |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
838 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
839 |
public Type getLub() { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
840 |
return tsym.type; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
841 |
} |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
842 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
843 |
public java.util.List<? extends TypeMirror> getAlternatives() { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
844 |
return Collections.unmodifiableList(alternatives_field); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
845 |
} |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
846 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
847 |
@Override |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
848 |
public TypeKind getKind() { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
849 |
return TypeKind.UNION; |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
850 |
} |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
851 |
|
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
852 |
@Override |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
853 |
public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
854 |
return v.visitUnion(this, p); |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
855 |
} |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
856 |
} |
0996df19ea87
7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents:
8625
diff
changeset
|
857 |
|
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
858 |
// a clone of a ClassType that knows about the bounds of an intersection type. |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
859 |
public static class IntersectionClassType extends ClassType implements IntersectionType { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
860 |
|
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
861 |
public boolean allInterfaces; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
862 |
|
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
863 |
public enum IntersectionKind { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
864 |
EXPLICIT, |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
865 |
IMPLICT; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
866 |
} |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
867 |
|
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
868 |
public IntersectionKind intersectionKind; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
869 |
|
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
870 |
public IntersectionClassType(List<Type> bounds, ClassSymbol csym, boolean allInterfaces) { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
871 |
super(Type.noType, List.<Type>nil(), csym); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
872 |
this.allInterfaces = allInterfaces; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
873 |
Assert.check((csym.flags() & COMPOUND) != 0); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
874 |
supertype_field = bounds.head; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
875 |
interfaces_field = bounds.tail; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
876 |
Assert.check(supertype_field.tsym.completer != null || |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
877 |
!supertype_field.isInterface(), supertype_field); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
878 |
} |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
879 |
|
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
880 |
public java.util.List<? extends TypeMirror> getBounds() { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
881 |
return Collections.unmodifiableList(getComponents()); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
882 |
} |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
883 |
|
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
884 |
public List<Type> getComponents() { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
885 |
return interfaces_field.prepend(supertype_field); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
886 |
} |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
887 |
|
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
888 |
@Override |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
889 |
public TypeKind getKind() { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
890 |
return TypeKind.INTERSECTION; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
891 |
} |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
892 |
|
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
893 |
@Override |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
894 |
public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
895 |
return intersectionKind == IntersectionKind.EXPLICIT ? |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
896 |
v.visitIntersection(this, p) : |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
897 |
v.visitDeclared(this, p); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
898 |
} |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
899 |
} |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14359
diff
changeset
|
900 |
|
10 | 901 |
public static class ArrayType extends Type |
902 |
implements javax.lang.model.type.ArrayType { |
|
903 |
||
904 |
public Type elemtype; |
|
905 |
||
906 |
public ArrayType(Type elemtype, TypeSymbol arrayClass) { |
|
907 |
super(ARRAY, arrayClass); |
|
908 |
this.elemtype = elemtype; |
|
909 |
} |
|
910 |
||
911 |
@Override |
|
912 |
public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
|
913 |
return v.visitArrayType(this, s); |
|
914 |
} |
|
915 |
||
916 |
public String toString() { |
|
917 |
return elemtype + "[]"; |
|
918 |
} |
|
919 |
||
920 |
public boolean equals(Object obj) { |
|
921 |
return |
|
922 |
this == obj || |
|
923 |
(obj instanceof ArrayType && |
|
924 |
this.elemtype.equals(((ArrayType)obj).elemtype)); |
|
925 |
} |
|
926 |
||
927 |
public int hashCode() { |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
928 |
return (ARRAY.ordinal() << 5) + elemtype.hashCode(); |
10 | 929 |
} |
930 |
||
7643
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7637
diff
changeset
|
931 |
public boolean isVarargs() { |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7637
diff
changeset
|
932 |
return false; |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7637
diff
changeset
|
933 |
} |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7637
diff
changeset
|
934 |
|
10 | 935 |
public List<Type> allparams() { return elemtype.allparams(); } |
936 |
||
937 |
public boolean isErroneous() { |
|
938 |
return elemtype.isErroneous(); |
|
939 |
} |
|
940 |
||
941 |
public boolean isParameterized() { |
|
942 |
return elemtype.isParameterized(); |
|
943 |
} |
|
944 |
||
945 |
public boolean isRaw() { |
|
946 |
return elemtype.isRaw(); |
|
947 |
} |
|
948 |
||
7643
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7637
diff
changeset
|
949 |
public ArrayType makeVarargs() { |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7637
diff
changeset
|
950 |
return new ArrayType(elemtype, tsym) { |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7637
diff
changeset
|
951 |
@Override |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7637
diff
changeset
|
952 |
public boolean isVarargs() { |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7637
diff
changeset
|
953 |
return true; |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7637
diff
changeset
|
954 |
} |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7637
diff
changeset
|
955 |
}; |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7637
diff
changeset
|
956 |
} |
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7637
diff
changeset
|
957 |
|
10 | 958 |
public Type map(Mapping f) { |
959 |
Type elemtype1 = f.apply(elemtype); |
|
960 |
if (elemtype1 == elemtype) return this; |
|
961 |
else return new ArrayType(elemtype1, tsym); |
|
962 |
} |
|
963 |
||
964 |
public boolean contains(Type elem) { |
|
965 |
return elem == this || elemtype.contains(elem); |
|
966 |
} |
|
967 |
||
968 |
public void complete() { |
|
969 |
elemtype.complete(); |
|
970 |
} |
|
971 |
||
972 |
public Type getComponentType() { |
|
973 |
return elemtype; |
|
974 |
} |
|
975 |
||
976 |
public TypeKind getKind() { |
|
977 |
return TypeKind.ARRAY; |
|
978 |
} |
|
979 |
||
980 |
public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
|
981 |
return v.visitArray(this, p); |
|
982 |
} |
|
983 |
} |
|
984 |
||
8430 | 985 |
public static class MethodType extends Type implements ExecutableType { |
10 | 986 |
|
987 |
public List<Type> argtypes; |
|
988 |
public Type restype; |
|
989 |
public List<Type> thrown; |
|
990 |
||
15385 | 991 |
/** The type annotations on the method receiver. |
992 |
*/ |
|
993 |
public Type recvtype; |
|
994 |
||
10 | 995 |
public MethodType(List<Type> argtypes, |
996 |
Type restype, |
|
997 |
List<Type> thrown, |
|
998 |
TypeSymbol methodClass) { |
|
999 |
super(METHOD, methodClass); |
|
1000 |
this.argtypes = argtypes; |
|
1001 |
this.restype = restype; |
|
1002 |
this.thrown = thrown; |
|
1003 |
} |
|
1004 |
||
1005 |
@Override |
|
1006 |
public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
|
1007 |
return v.visitMethodType(this, s); |
|
1008 |
} |
|
1009 |
||
1010 |
/** The Java source which this type represents. |
|
1011 |
* |
|
1012 |
* XXX 06/09/99 iris This isn't correct Java syntax, but it probably |
|
1013 |
* should be. |
|
1014 |
*/ |
|
1015 |
public String toString() { |
|
1016 |
return "(" + argtypes + ")" + restype; |
|
1017 |
} |
|
1018 |
||
1019 |
public List<Type> getParameterTypes() { return argtypes; } |
|
1020 |
public Type getReturnType() { return restype; } |
|
15385 | 1021 |
public Type getReceiverType() { return recvtype; } |
10 | 1022 |
public List<Type> getThrownTypes() { return thrown; } |
1023 |
||
1024 |
public boolean isErroneous() { |
|
1025 |
return |
|
1026 |
isErroneous(argtypes) || |
|
1027 |
restype != null && restype.isErroneous(); |
|
1028 |
} |
|
1029 |
||
1030 |
public Type map(Mapping f) { |
|
1031 |
List<Type> argtypes1 = map(argtypes, f); |
|
1032 |
Type restype1 = f.apply(restype); |
|
1033 |
List<Type> thrown1 = map(thrown, f); |
|
1034 |
if (argtypes1 == argtypes && |
|
1035 |
restype1 == restype && |
|
1036 |
thrown1 == thrown) return this; |
|
1037 |
else return new MethodType(argtypes1, restype1, thrown1, tsym); |
|
1038 |
} |
|
1039 |
||
1040 |
public boolean contains(Type elem) { |
|
1041 |
return elem == this || contains(argtypes, elem) || restype.contains(elem); |
|
1042 |
} |
|
1043 |
||
1044 |
public MethodType asMethodType() { return this; } |
|
1045 |
||
1046 |
public void complete() { |
|
1047 |
for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail) |
|
1048 |
l.head.complete(); |
|
1049 |
restype.complete(); |
|
15385 | 1050 |
recvtype.complete(); |
10 | 1051 |
for (List<Type> l = thrown; l.nonEmpty(); l = l.tail) |
1052 |
l.head.complete(); |
|
1053 |
} |
|
1054 |
||
1055 |
public List<TypeVar> getTypeVariables() { |
|
1056 |
return List.nil(); |
|
1057 |
} |
|
1058 |
||
1059 |
public TypeSymbol asElement() { |
|
1060 |
return null; |
|
1061 |
} |
|
1062 |
||
1063 |
public TypeKind getKind() { |
|
1064 |
return TypeKind.EXECUTABLE; |
|
1065 |
} |
|
1066 |
||
1067 |
public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
|
1068 |
return v.visitExecutable(this, p); |
|
1069 |
} |
|
1070 |
} |
|
1071 |
||
1072 |
public static class PackageType extends Type implements NoType { |
|
1073 |
||
1074 |
PackageType(TypeSymbol tsym) { |
|
1075 |
super(PACKAGE, tsym); |
|
1076 |
} |
|
1077 |
||
1078 |
@Override |
|
1079 |
public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
|
1080 |
return v.visitPackageType(this, s); |
|
1081 |
} |
|
1082 |
||
1083 |
public String toString() { |
|
1084 |
return tsym.getQualifiedName().toString(); |
|
1085 |
} |
|
1086 |
||
1087 |
public TypeKind getKind() { |
|
1088 |
return TypeKind.PACKAGE; |
|
1089 |
} |
|
1090 |
||
1091 |
public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
|
1092 |
return v.visitNoType(this, p); |
|
1093 |
} |
|
1094 |
} |
|
1095 |
||
1096 |
public static class TypeVar extends Type implements TypeVariable { |
|
1097 |
||
7637 | 1098 |
/** The upper bound of this type variable; set from outside. |
10 | 1099 |
* Must be nonempty once it is set. |
1100 |
* For a bound, `bound' is the bound type itself. |
|
1101 |
* Multiple bounds are expressed as a single class type which has the |
|
1102 |
* individual bounds as superclass, respectively interfaces. |
|
1103 |
* The class type then has as `tsym' a compiler generated class `c', |
|
1104 |
* which has a flag COMPOUND and whose owner is the type variable |
|
1105 |
* itself. Furthermore, the erasure_field of the class |
|
1106 |
* points to the first class or interface bound. |
|
1107 |
*/ |
|
1108 |
public Type bound = null; |
|
7637 | 1109 |
|
1110 |
/** The lower bound of this type variable. |
|
1111 |
* TypeVars don't normally have a lower bound, so it is normally set |
|
1112 |
* to syms.botType. |
|
1113 |
* Subtypes, such as CapturedType, may provide a different value. |
|
1114 |
*/ |
|
10 | 1115 |
public Type lower; |
1116 |
||
1117 |
public TypeVar(Name name, Symbol owner, Type lower) { |
|
1118 |
super(TYPEVAR, null); |
|
1119 |
tsym = new TypeSymbol(0, name, this, owner); |
|
1120 |
this.lower = lower; |
|
1121 |
} |
|
1122 |
||
1123 |
public TypeVar(TypeSymbol tsym, Type bound, Type lower) { |
|
1124 |
super(TYPEVAR, tsym); |
|
1125 |
this.bound = bound; |
|
1126 |
this.lower = lower; |
|
1127 |
} |
|
1128 |
||
1129 |
@Override |
|
1130 |
public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
|
1131 |
return v.visitTypeVar(this, s); |
|
1132 |
} |
|
1133 |
||
7637 | 1134 |
@Override |
15385 | 1135 |
public Type getUpperBound() { |
1136 |
if ((bound == null || bound.tag == NONE) && this != tsym.type) |
|
1137 |
bound = tsym.type.getUpperBound(); |
|
1138 |
return bound; |
|
1139 |
} |
|
10 | 1140 |
|
1141 |
int rank_field = -1; |
|
1142 |
||
7637 | 1143 |
@Override |
10 | 1144 |
public Type getLowerBound() { |
1145 |
return lower; |
|
1146 |
} |
|
1147 |
||
1148 |
public TypeKind getKind() { |
|
1149 |
return TypeKind.TYPEVAR; |
|
1150 |
} |
|
1151 |
||
938
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
735
diff
changeset
|
1152 |
public boolean isCaptured() { |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
735
diff
changeset
|
1153 |
return false; |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
735
diff
changeset
|
1154 |
} |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
735
diff
changeset
|
1155 |
|
10 | 1156 |
public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
1157 |
return v.visitTypeVariable(this, p); |
|
1158 |
} |
|
1159 |
} |
|
1160 |
||
1161 |
/** A captured type variable comes from wildcards which can have |
|
1162 |
* both upper and lower bound. CapturedType extends TypeVar with |
|
1163 |
* a lower bound. |
|
1164 |
*/ |
|
1165 |
public static class CapturedType extends TypeVar { |
|
1166 |
||
1167 |
public WildcardType wildcard; |
|
1168 |
||
1169 |
public CapturedType(Name name, |
|
1170 |
Symbol owner, |
|
1171 |
Type upper, |
|
1172 |
Type lower, |
|
1173 |
WildcardType wildcard) { |
|
1174 |
super(name, owner, lower); |
|
8032 | 1175 |
this.lower = Assert.checkNonNull(lower); |
10 | 1176 |
this.bound = upper; |
1177 |
this.wildcard = wildcard; |
|
1178 |
} |
|
1179 |
||
1180 |
@Override |
|
1181 |
public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
|
1182 |
return v.visitCapturedType(this, s); |
|
1183 |
} |
|
1184 |
||
1185 |
@Override |
|
938
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
735
diff
changeset
|
1186 |
public boolean isCaptured() { |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
735
diff
changeset
|
1187 |
return true; |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
735
diff
changeset
|
1188 |
} |
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
735
diff
changeset
|
1189 |
|
13aae74ca013
6594284: NPE thrown when calling a method on an intersection type
mcimadamore
parents:
735
diff
changeset
|
1190 |
@Override |
10 | 1191 |
public String toString() { |
1192 |
return "capture#" |
|
2984
e15ff3a34054
6722234: javac diagnostics need better integration with the type-system
mcimadamore
parents:
1527
diff
changeset
|
1193 |
+ (hashCode() & 0xFFFFFFFFL) % Printer.PRIME |
10 | 1194 |
+ " of " |
1195 |
+ wildcard; |
|
1196 |
} |
|
1197 |
} |
|
1198 |
||
1199 |
public static abstract class DelegatedType extends Type { |
|
1200 |
public Type qtype; |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
1201 |
public DelegatedType(TypeTag tag, Type qtype) { |
10 | 1202 |
super(tag, qtype.tsym); |
1203 |
this.qtype = qtype; |
|
1204 |
} |
|
1205 |
public String toString() { return qtype.toString(); } |
|
1206 |
public List<Type> getTypeArguments() { return qtype.getTypeArguments(); } |
|
1207 |
public Type getEnclosingType() { return qtype.getEnclosingType(); } |
|
1208 |
public List<Type> getParameterTypes() { return qtype.getParameterTypes(); } |
|
1209 |
public Type getReturnType() { return qtype.getReturnType(); } |
|
15385 | 1210 |
public Type getReceiverType() { return qtype.getReceiverType(); } |
10 | 1211 |
public List<Type> getThrownTypes() { return qtype.getThrownTypes(); } |
1212 |
public List<Type> allparams() { return qtype.allparams(); } |
|
1213 |
public Type getUpperBound() { return qtype.getUpperBound(); } |
|
1214 |
public boolean isErroneous() { return qtype.isErroneous(); } |
|
1215 |
} |
|
1216 |
||
12915
28cf1e0dafdc
7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents:
12468
diff
changeset
|
1217 |
/** |
28cf1e0dafdc
7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents:
12468
diff
changeset
|
1218 |
* The type of a generic method type. It consists of a method type and |
28cf1e0dafdc
7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents:
12468
diff
changeset
|
1219 |
* a list of method type-parameters that are used within the method |
28cf1e0dafdc
7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents:
12468
diff
changeset
|
1220 |
* type. |
28cf1e0dafdc
7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents:
12468
diff
changeset
|
1221 |
*/ |
8430 | 1222 |
public static class ForAll extends DelegatedType implements ExecutableType { |
10 | 1223 |
public List<Type> tvars; |
1224 |
||
1225 |
public ForAll(List<Type> tvars, Type qtype) { |
|
12915
28cf1e0dafdc
7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents:
12468
diff
changeset
|
1226 |
super(FORALL, (MethodType)qtype); |
10 | 1227 |
this.tvars = tvars; |
1228 |
} |
|
1229 |
||
1230 |
@Override |
|
1231 |
public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
|
1232 |
return v.visitForAll(this, s); |
|
1233 |
} |
|
1234 |
||
1235 |
public String toString() { |
|
1236 |
return "<" + tvars + ">" + qtype; |
|
1237 |
} |
|
1238 |
||
1239 |
public List<Type> getTypeArguments() { return tvars; } |
|
1240 |
||
1241 |
public boolean isErroneous() { |
|
1242 |
return qtype.isErroneous(); |
|
1243 |
} |
|
1244 |
||
1245 |
public Type map(Mapping f) { |
|
1246 |
return f.apply(qtype); |
|
1247 |
} |
|
1248 |
||
1249 |
public boolean contains(Type elem) { |
|
1250 |
return qtype.contains(elem); |
|
1251 |
} |
|
1252 |
||
1253 |
public MethodType asMethodType() { |
|
12915
28cf1e0dafdc
7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents:
12468
diff
changeset
|
1254 |
return (MethodType)qtype; |
10 | 1255 |
} |
1256 |
||
1257 |
public void complete() { |
|
1258 |
for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) { |
|
1259 |
((TypeVar)l.head).bound.complete(); |
|
1260 |
} |
|
1261 |
qtype.complete(); |
|
1262 |
} |
|
1263 |
||
1264 |
public List<TypeVar> getTypeVariables() { |
|
1265 |
return List.convert(TypeVar.class, getTypeArguments()); |
|
1266 |
} |
|
1267 |
||
1268 |
public TypeKind getKind() { |
|
1269 |
return TypeKind.EXECUTABLE; |
|
1270 |
} |
|
1271 |
||
1272 |
public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
|
1273 |
return v.visitExecutable(this, p); |
|
1274 |
} |
|
1275 |
} |
|
1276 |
||
14048
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1277 |
/** A class for inference variables, for use during method/diamond type |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1278 |
* inference. An inference variable has upper/lower bounds and a set |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1279 |
* of equality constraints. Such bounds are set during subtyping, type-containment, |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1280 |
* type-equality checks, when the types being tested contain inference variables. |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1281 |
* A change listener can be attached to an inference variable, to receive notifications |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1282 |
* whenever the bounds of an inference variable change. |
10 | 1283 |
*/ |
1284 |
public static class UndetVar extends DelegatedType { |
|
14048
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1285 |
|
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1286 |
/** Inference variable change listener. The listener method is called |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1287 |
* whenever a change to the inference variable's bounds occurs |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1288 |
*/ |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1289 |
public interface UndetVarListener { |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1290 |
/** called when some inference variable bounds (of given kinds ibs) change */ |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1291 |
void varChanged(UndetVar uv, Set<InferenceBound> ibs); |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1292 |
} |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1293 |
|
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1294 |
/** |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1295 |
* Inference variable bound kinds |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1296 |
*/ |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1297 |
public enum InferenceBound { |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1298 |
/** upper bounds */ |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1299 |
UPPER, |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1300 |
/** lower bounds */ |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1301 |
LOWER, |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1302 |
/** equality constraints */ |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1303 |
EQ; |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1304 |
} |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1305 |
|
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1306 |
/** inference variable bounds */ |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1307 |
private Map<InferenceBound, List<Type>> bounds; |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1308 |
|
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1309 |
/** inference variable's inferred type (set from Infer.java) */ |
10 | 1310 |
public Type inst = null; |
1311 |
||
15705 | 1312 |
/** number of declared (upper) bounds */ |
1313 |
public int declaredCount; |
|
1314 |
||
14048
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1315 |
/** inference variable's change listener */ |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1316 |
public UndetVarListener listener = null; |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1317 |
|
10 | 1318 |
@Override |
1319 |
public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
|
1320 |
return v.visitUndetVar(this, s); |
|
1321 |
} |
|
1322 |
||
14048
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1323 |
public UndetVar(TypeVar origin, Types types) { |
10 | 1324 |
super(UNDETVAR, origin); |
14048
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1325 |
bounds = new EnumMap<InferenceBound, List<Type>>(InferenceBound.class); |
15705 | 1326 |
List<Type> declaredBounds = types.getBounds(origin); |
1327 |
declaredCount = declaredBounds.length(); |
|
1328 |
bounds.put(InferenceBound.UPPER, declaredBounds); |
|
14048
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1329 |
bounds.put(InferenceBound.LOWER, List.<Type>nil()); |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1330 |
bounds.put(InferenceBound.EQ, List.<Type>nil()); |
10 | 1331 |
} |
1332 |
||
1333 |
public String toString() { |
|
1334 |
if (inst != null) return inst.toString(); |
|
1335 |
else return qtype + "?"; |
|
1336 |
} |
|
1337 |
||
1338 |
public Type baseType() { |
|
1339 |
if (inst != null) return inst.baseType(); |
|
1340 |
else return this; |
|
1341 |
} |
|
14048
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1342 |
|
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1343 |
/** get all bounds of a given kind */ |
15705 | 1344 |
public List<Type> getBounds(InferenceBound... ibs) { |
1345 |
ListBuffer<Type> buf = ListBuffer.lb(); |
|
1346 |
for (InferenceBound ib : ibs) { |
|
1347 |
buf.appendList(bounds.get(ib)); |
|
1348 |
} |
|
1349 |
return buf.toList(); |
|
1350 |
} |
|
1351 |
||
1352 |
/** get the list of declared (upper) bounds */ |
|
1353 |
public List<Type> getDeclaredBounds() { |
|
1354 |
ListBuffer<Type> buf = ListBuffer.lb(); |
|
1355 |
int count = 0; |
|
1356 |
for (Type b : getBounds(InferenceBound.UPPER)) { |
|
1357 |
if (count++ == declaredCount) break; |
|
1358 |
buf.append(b); |
|
1359 |
} |
|
1360 |
return buf.toList(); |
|
14048
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1361 |
} |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1362 |
|
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1363 |
/** add a bound of a given kind - this might trigger listener notification */ |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1364 |
public void addBound(InferenceBound ib, Type bound, Types types) { |
15705 | 1365 |
Type bound2 = toTypeVarMap.apply(bound); |
14048
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1366 |
List<Type> prevBounds = bounds.get(ib); |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1367 |
for (Type b : prevBounds) { |
15705 | 1368 |
//check for redundancy - use strict version of isSameType on tvars |
1369 |
//(as the standard version will lead to false positives w.r.t. clones ivars) |
|
16341
cc4b4e3c0f12
8009545: Graph inference: dependencies between inference variables should be set during incorporation
mcimadamore
parents:
15705
diff
changeset
|
1370 |
if (types.isSameType(b, bound2, true) || bound == qtype) return; |
14048
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1371 |
} |
15705 | 1372 |
bounds.put(ib, prevBounds.prepend(bound2)); |
14048
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1373 |
notifyChange(EnumSet.of(ib)); |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1374 |
} |
15705 | 1375 |
//where |
1376 |
Type.Mapping toTypeVarMap = new Mapping("toTypeVarMap") { |
|
1377 |
@Override |
|
1378 |
public Type apply(Type t) { |
|
1379 |
if (t.hasTag(UNDETVAR)) { |
|
1380 |
UndetVar uv = (UndetVar)t; |
|
1381 |
return uv.qtype; |
|
1382 |
} else { |
|
1383 |
return t.map(this); |
|
1384 |
} |
|
1385 |
} |
|
1386 |
}; |
|
14048
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1387 |
|
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1388 |
/** replace types in all bounds - this might trigger listener notification */ |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1389 |
public void substBounds(List<Type> from, List<Type> to, Types types) { |
15705 | 1390 |
List<Type> instVars = from.diff(to); |
1391 |
//if set of instantiated ivars is empty, there's nothing to do! |
|
1392 |
if (instVars.isEmpty()) return; |
|
1393 |
final EnumSet<InferenceBound> boundsChanged = EnumSet.noneOf(InferenceBound.class); |
|
1394 |
UndetVarListener prevListener = listener; |
|
1395 |
try { |
|
1396 |
//setup new listener for keeping track of changed bounds |
|
1397 |
listener = new UndetVarListener() { |
|
1398 |
public void varChanged(UndetVar uv, Set<InferenceBound> ibs) { |
|
1399 |
boundsChanged.addAll(ibs); |
|
1400 |
} |
|
1401 |
}; |
|
1402 |
for (Map.Entry<InferenceBound, List<Type>> _entry : bounds.entrySet()) { |
|
1403 |
InferenceBound ib = _entry.getKey(); |
|
1404 |
List<Type> prevBounds = _entry.getValue(); |
|
1405 |
ListBuffer<Type> newBounds = ListBuffer.lb(); |
|
1406 |
ListBuffer<Type> deps = ListBuffer.lb(); |
|
1407 |
//step 1 - re-add bounds that are not dependent on ivars |
|
1408 |
for (Type t : prevBounds) { |
|
1409 |
if (!t.containsAny(instVars)) { |
|
1410 |
newBounds.append(t); |
|
1411 |
} else { |
|
1412 |
deps.append(t); |
|
1413 |
} |
|
1414 |
} |
|
1415 |
//step 2 - replace bounds |
|
1416 |
bounds.put(ib, newBounds.toList()); |
|
1417 |
//step 3 - for each dependency, add new replaced bound |
|
1418 |
for (Type dep : deps) { |
|
1419 |
addBound(ib, types.subst(dep, from, to), types); |
|
1420 |
} |
|
14048
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1421 |
} |
15705 | 1422 |
} finally { |
1423 |
listener = prevListener; |
|
1424 |
if (!boundsChanged.isEmpty()) { |
|
1425 |
notifyChange(boundsChanged); |
|
1426 |
} |
|
14048
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1427 |
} |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1428 |
} |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1429 |
|
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1430 |
private void notifyChange(EnumSet<InferenceBound> ibs) { |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1431 |
if (listener != null) { |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1432 |
listener.varChanged(this, ibs); |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1433 |
} |
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13844
diff
changeset
|
1434 |
} |
10 | 1435 |
} |
1436 |
||
1437 |
/** Represents VOID or NONE. |
|
1438 |
*/ |
|
1439 |
static class JCNoType extends Type implements NoType { |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
1440 |
public JCNoType(TypeTag tag) { |
10 | 1441 |
super(tag, null); |
1442 |
} |
|
1443 |
||
1444 |
@Override |
|
1445 |
public TypeKind getKind() { |
|
1446 |
switch (tag) { |
|
1447 |
case VOID: return TypeKind.VOID; |
|
1448 |
case NONE: return TypeKind.NONE; |
|
1449 |
default: |
|
1450 |
throw new AssertionError("Unexpected tag: " + tag); |
|
1451 |
} |
|
1452 |
} |
|
1453 |
||
1454 |
@Override |
|
1455 |
public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
|
1456 |
return v.visitNoType(this, p); |
|
1457 |
} |
|
1458 |
} |
|
1459 |
||
1460 |
static class BottomType extends Type implements NullType { |
|
1461 |
public BottomType() { |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
1462 |
super(BOT, null); |
10 | 1463 |
} |
1464 |
||
1465 |
@Override |
|
1466 |
public TypeKind getKind() { |
|
1467 |
return TypeKind.NULL; |
|
1468 |
} |
|
1469 |
||
1470 |
@Override |
|
1471 |
public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
|
1472 |
return v.visitNull(this, p); |
|
1473 |
} |
|
1474 |
||
1475 |
@Override |
|
1476 |
public Type constType(Object value) { |
|
1477 |
return this; |
|
1478 |
} |
|
1479 |
||
1480 |
@Override |
|
1481 |
public String stringValue() { |
|
1482 |
return "null"; |
|
1483 |
} |
|
1484 |
} |
|
1485 |
||
1486 |
public static class ErrorType extends ClassType |
|
1487 |
implements javax.lang.model.type.ErrorType { |
|
1488 |
||
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1206
diff
changeset
|
1489 |
private Type originalType = null; |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1206
diff
changeset
|
1490 |
|
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1206
diff
changeset
|
1491 |
public ErrorType(Type originalType, TypeSymbol tsym) { |
10 | 1492 |
super(noType, List.<Type>nil(), null); |
1493 |
tag = ERROR; |
|
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1206
diff
changeset
|
1494 |
this.tsym = tsym; |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1206
diff
changeset
|
1495 |
this.originalType = (originalType == null ? noType : originalType); |
10 | 1496 |
} |
1497 |
||
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1206
diff
changeset
|
1498 |
public ErrorType(ClassSymbol c, Type originalType) { |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1206
diff
changeset
|
1499 |
this(originalType, c); |
10 | 1500 |
c.type = this; |
1501 |
c.kind = ERR; |
|
1502 |
c.members_field = new Scope.ErrorScope(c); |
|
1503 |
} |
|
1504 |
||
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1206
diff
changeset
|
1505 |
public ErrorType(Name name, TypeSymbol container, Type originalType) { |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1206
diff
changeset
|
1506 |
this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container), originalType); |
10 | 1507 |
} |
1508 |
||
1509 |
@Override |
|
1510 |
public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
|
1511 |
return v.visitErrorType(this, s); |
|
1512 |
} |
|
1513 |
||
1514 |
public Type constType(Object constValue) { return this; } |
|
15385 | 1515 |
public Type getEnclosingType() { return this; } |
10 | 1516 |
public Type getReturnType() { return this; } |
1517 |
public Type asSub(Symbol sym) { return this; } |
|
1518 |
public Type map(Mapping f) { return this; } |
|
1519 |
||
1520 |
public boolean isGenType(Type t) { return true; } |
|
1521 |
public boolean isErroneous() { return true; } |
|
1522 |
public boolean isCompound() { return false; } |
|
1523 |
public boolean isInterface() { return false; } |
|
1524 |
||
1525 |
public List<Type> allparams() { return List.nil(); } |
|
1526 |
public List<Type> getTypeArguments() { return List.nil(); } |
|
1527 |
||
1528 |
public TypeKind getKind() { |
|
1529 |
return TypeKind.ERROR; |
|
1530 |
} |
|
1531 |
||
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1206
diff
changeset
|
1532 |
public Type getOriginalType() { |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1206
diff
changeset
|
1533 |
return originalType; |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1206
diff
changeset
|
1534 |
} |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1206
diff
changeset
|
1535 |
|
10 | 1536 |
public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
1537 |
return v.visitError(this, p); |
|
1538 |
} |
|
1539 |
} |
|
1540 |
||
15385 | 1541 |
public static class AnnotatedType extends Type |
1542 |
implements javax.lang.model.type.AnnotatedType { |
|
1543 |
/** The type annotations on this type. |
|
1544 |
*/ |
|
1545 |
public List<Attribute.TypeCompound> typeAnnotations; |
|
1546 |
||
1547 |
/** The underlying type that is annotated. |
|
1548 |
*/ |
|
1549 |
public Type underlyingType; |
|
1550 |
||
1551 |
public AnnotatedType(Type underlyingType) { |
|
1552 |
super(underlyingType.tag, underlyingType.tsym); |
|
1553 |
this.typeAnnotations = List.nil(); |
|
1554 |
this.underlyingType = underlyingType; |
|
1555 |
Assert.check(underlyingType.getKind() != TypeKind.ANNOTATED, |
|
1556 |
"Can't annotate already annotated type: " + underlyingType); |
|
1557 |
} |
|
1558 |
||
1559 |
public AnnotatedType(List<Attribute.TypeCompound> typeAnnotations, |
|
1560 |
Type underlyingType) { |
|
1561 |
super(underlyingType.tag, underlyingType.tsym); |
|
1562 |
this.typeAnnotations = typeAnnotations; |
|
1563 |
this.underlyingType = underlyingType; |
|
1564 |
Assert.check(underlyingType.getKind() != TypeKind.ANNOTATED, |
|
1565 |
"Can't annotate already annotated type: " + underlyingType + |
|
1566 |
"; adding: " + typeAnnotations); |
|
1567 |
} |
|
1568 |
||
1569 |
@Override |
|
1570 |
public TypeKind getKind() { |
|
1571 |
return TypeKind.ANNOTATED; |
|
1572 |
} |
|
1573 |
||
1574 |
@Override |
|
1575 |
public List<? extends AnnotationMirror> getAnnotations() { |
|
1576 |
return typeAnnotations; |
|
1577 |
} |
|
1578 |
||
1579 |
@Override |
|
1580 |
public TypeMirror getUnderlyingType() { |
|
1581 |
return underlyingType; |
|
1582 |
} |
|
1583 |
||
1584 |
@Override |
|
1585 |
public Type unannotatedType() { |
|
1586 |
return underlyingType; |
|
1587 |
} |
|
1588 |
||
1589 |
@Override |
|
1590 |
public <R,S> R accept(Type.Visitor<R,S> v, S s) { |
|
1591 |
return v.visitAnnotatedType(this, s); |
|
1592 |
} |
|
1593 |
||
1594 |
@Override |
|
1595 |
public <R, P> R accept(TypeVisitor<R, P> v, P p) { |
|
1596 |
return v.visitAnnotated(this, p); |
|
1597 |
} |
|
1598 |
||
1599 |
@Override |
|
1600 |
public Type map(Mapping f) { |
|
1601 |
underlyingType.map(f); |
|
1602 |
return this; |
|
1603 |
} |
|
1604 |
||
1605 |
@Override |
|
1606 |
public Type constType(Object constValue) { return underlyingType.constType(constValue); } |
|
1607 |
@Override |
|
1608 |
public Type getEnclosingType() { return underlyingType.getEnclosingType(); } |
|
1609 |
||
1610 |
@Override |
|
1611 |
public Type getReturnType() { return underlyingType.getReturnType(); } |
|
1612 |
@Override |
|
1613 |
public List<Type> getTypeArguments() { return underlyingType.getTypeArguments(); } |
|
1614 |
@Override |
|
1615 |
public List<Type> getParameterTypes() { return underlyingType.getParameterTypes(); } |
|
1616 |
@Override |
|
1617 |
public Type getReceiverType() { return underlyingType.getReceiverType(); } |
|
1618 |
@Override |
|
1619 |
public List<Type> getThrownTypes() { return underlyingType.getThrownTypes(); } |
|
1620 |
@Override |
|
1621 |
public Type getUpperBound() { return underlyingType.getUpperBound(); } |
|
1622 |
@Override |
|
1623 |
public Type getLowerBound() { return underlyingType.getLowerBound(); } |
|
1624 |
||
1625 |
@Override |
|
1626 |
public boolean isErroneous() { return underlyingType.isErroneous(); } |
|
1627 |
@Override |
|
1628 |
public boolean isCompound() { return underlyingType.isCompound(); } |
|
1629 |
@Override |
|
1630 |
public boolean isInterface() { return underlyingType.isInterface(); } |
|
1631 |
@Override |
|
1632 |
public List<Type> allparams() { return underlyingType.allparams(); } |
|
1633 |
@Override |
|
1634 |
public boolean isNumeric() { return underlyingType.isNumeric(); } |
|
1635 |
@Override |
|
1636 |
public boolean isReference() { return underlyingType.isReference(); } |
|
1637 |
@Override |
|
1638 |
public boolean isParameterized() { return underlyingType.isParameterized(); } |
|
1639 |
@Override |
|
1640 |
public boolean isRaw() { return underlyingType.isRaw(); } |
|
1641 |
@Override |
|
1642 |
public boolean isFinal() { return underlyingType.isFinal(); } |
|
1643 |
@Override |
|
1644 |
public boolean isSuperBound() { return underlyingType.isSuperBound(); } |
|
1645 |
@Override |
|
1646 |
public boolean isExtendsBound() { return underlyingType.isExtendsBound(); } |
|
1647 |
@Override |
|
1648 |
public boolean isUnbound() { return underlyingType.isUnbound(); } |
|
1649 |
||
1650 |
@Override |
|
1651 |
public String toString() { |
|
1652 |
// TODO more logic for arrays, etc. |
|
1653 |
if (typeAnnotations != null && |
|
1654 |
!typeAnnotations.isEmpty()) { |
|
1655 |
return "(" + typeAnnotations.toString() + " :: " + underlyingType.toString() + ")"; |
|
1656 |
} else { |
|
1657 |
return "({} :: " + underlyingType.toString() +")"; |
|
1658 |
} |
|
1659 |
} |
|
1660 |
||
1661 |
@Override |
|
1662 |
public boolean contains(Type t) { return underlyingType.contains(t); } |
|
1663 |
||
1664 |
// TODO: attach annotations? |
|
1665 |
@Override |
|
1666 |
public Type withTypeVar(Type t) { return underlyingType.withTypeVar(t); } |
|
1667 |
||
1668 |
// TODO: attach annotations? |
|
1669 |
@Override |
|
1670 |
public TypeSymbol asElement() { return underlyingType.asElement(); } |
|
1671 |
||
1672 |
// TODO: attach annotations? |
|
1673 |
@Override |
|
1674 |
public MethodType asMethodType() { return underlyingType.asMethodType(); } |
|
1675 |
||
1676 |
@Override |
|
1677 |
public void complete() { underlyingType.complete(); } |
|
1678 |
||
1679 |
@Override |
|
1680 |
public TypeMirror getComponentType() { return ((ArrayType)underlyingType).getComponentType(); } |
|
1681 |
||
1682 |
// The result is an ArrayType, but only in the model sense, not the Type sense. |
|
1683 |
public AnnotatedType makeVarargs() { |
|
1684 |
AnnotatedType atype = new AnnotatedType(((ArrayType)underlyingType).makeVarargs()); |
|
1685 |
atype.typeAnnotations = this.typeAnnotations; |
|
1686 |
return atype; |
|
1687 |
} |
|
1688 |
||
1689 |
@Override |
|
1690 |
public TypeMirror getExtendsBound() { return ((WildcardType)underlyingType).getExtendsBound(); } |
|
1691 |
@Override |
|
1692 |
public TypeMirror getSuperBound() { return ((WildcardType)underlyingType).getSuperBound(); } |
|
1693 |
} |
|
1694 |
||
10 | 1695 |
/** |
1696 |
* A visitor for types. A visitor is used to implement operations |
|
1697 |
* (or relations) on types. Most common operations on types are |
|
1698 |
* binary relations and this interface is designed for binary |
|
15385 | 1699 |
* relations, that is, operations of the form |
10 | 1700 |
* Type × S → R. |
1701 |
* <!-- In plain text: Type x S -> R --> |
|
1702 |
* |
|
1703 |
* @param <R> the return type of the operation implemented by this |
|
1704 |
* visitor; use Void if no return type is needed. |
|
1705 |
* @param <S> the type of the second argument (the first being the |
|
1706 |
* type itself) of the operation implemented by this visitor; use |
|
1707 |
* Void if a second argument is not needed. |
|
1708 |
*/ |
|
1709 |
public interface Visitor<R,S> { |
|
1710 |
R visitClassType(ClassType t, S s); |
|
1711 |
R visitWildcardType(WildcardType t, S s); |
|
1712 |
R visitArrayType(ArrayType t, S s); |
|
1713 |
R visitMethodType(MethodType t, S s); |
|
1714 |
R visitPackageType(PackageType t, S s); |
|
1715 |
R visitTypeVar(TypeVar t, S s); |
|
1716 |
R visitCapturedType(CapturedType t, S s); |
|
1717 |
R visitForAll(ForAll t, S s); |
|
1718 |
R visitUndetVar(UndetVar t, S s); |
|
1719 |
R visitErrorType(ErrorType t, S s); |
|
15385 | 1720 |
R visitAnnotatedType(AnnotatedType t, S s); |
10 | 1721 |
R visitType(Type t, S s); |
1722 |
} |
|
1723 |
} |