author | emc |
Fri, 09 May 2014 09:28:47 -0400 | |
changeset 24396 | 3c36c6afcbca |
parent 24293 | 0d889f759fac |
child 24397 | 32aef69ee177 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
22442
8fd30fc4e3a3
8029690: Move symbol creation from ClassReader to Symtab
jfranck
parents:
22167
diff
changeset
|
2 |
* Copyright (c) 2003, 2014, 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 |
||
3539
3c265cb6f4e0
6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents:
3140
diff
changeset
|
28 |
import java.lang.ref.SoftReference; |
14719
cd65a651beb3
8004068: Fix build problems caused by on-demand imports
jjg
parents:
14547
diff
changeset
|
29 |
import java.util.HashSet; |
cd65a651beb3
8004068: Fix build problems caused by on-demand imports
jjg
parents:
14547
diff
changeset
|
30 |
import java.util.HashMap; |
cd65a651beb3
8004068: Fix build problems caused by on-demand imports
jjg
parents:
14547
diff
changeset
|
31 |
import java.util.Locale; |
cd65a651beb3
8004068: Fix build problems caused by on-demand imports
jjg
parents:
14547
diff
changeset
|
32 |
import java.util.Map; |
cd65a651beb3
8004068: Fix build problems caused by on-demand imports
jjg
parents:
14547
diff
changeset
|
33 |
import java.util.Set; |
cd65a651beb3
8004068: Fix build problems caused by on-demand imports
jjg
parents:
14547
diff
changeset
|
34 |
import java.util.WeakHashMap; |
10 | 35 |
|
18730
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
36 |
import javax.tools.JavaFileObject; |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
37 |
|
6575
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6353
diff
changeset
|
38 |
import com.sun.tools.javac.code.Attribute.RetentionPolicy; |
7643
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7637
diff
changeset
|
39 |
import com.sun.tools.javac.code.Lint.LintCategory; |
14048
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13689
diff
changeset
|
40 |
import com.sun.tools.javac.code.Type.UndetVar.InferenceBound; |
18730
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
41 |
import com.sun.tools.javac.comp.AttrContext; |
10 | 42 |
import com.sun.tools.javac.comp.Check; |
18730
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
43 |
import com.sun.tools.javac.comp.Enter; |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
44 |
import com.sun.tools.javac.comp.Env; |
14258 | 45 |
import com.sun.tools.javac.jvm.ClassReader; |
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
23810
diff
changeset
|
46 |
import com.sun.tools.javac.tree.JCTree; |
14258 | 47 |
import com.sun.tools.javac.util.*; |
48 |
import static com.sun.tools.javac.code.BoundKind.*; |
|
49 |
import static com.sun.tools.javac.code.Flags.*; |
|
8242
3873b4aaf4a8
7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents:
8230
diff
changeset
|
50 |
import static com.sun.tools.javac.code.Scope.*; |
14258 | 51 |
import static com.sun.tools.javac.code.Symbol.*; |
10 | 52 |
import static com.sun.tools.javac.code.Type.*; |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
53 |
import static com.sun.tools.javac.code.TypeTag.*; |
16300 | 54 |
import static com.sun.tools.javac.jvm.ClassFile.externalize; |
10 | 55 |
|
56 |
/** |
|
57 |
* Utility class containing various operations on types. |
|
58 |
* |
|
59 |
* <p>Unless other names are more illustrative, the following naming |
|
60 |
* conventions should be observed in this file: |
|
61 |
* |
|
62 |
* <dl> |
|
63 |
* <dt>t</dt> |
|
64 |
* <dd>If the first argument to an operation is a type, it should be named t.</dd> |
|
65 |
* <dt>s</dt> |
|
66 |
* <dd>Similarly, if the second argument to an operation is a type, it should be named s.</dd> |
|
67 |
* <dt>ts</dt> |
|
68 |
* <dd>If an operations takes a list of types, the first should be named ts.</dd> |
|
69 |
* <dt>ss</dt> |
|
70 |
* <dd>A second list of types should be named ss.</dd> |
|
71 |
* </dl> |
|
72 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5654
diff
changeset
|
73 |
* <p><b>This is NOT part of any supported API. |
10 | 74 |
* If you write code that depends on this, you do so at your own risk. |
75 |
* This code and its internal interfaces are subject to change or |
|
76 |
* deletion without notice.</b> |
|
77 |
*/ |
|
78 |
public class Types { |
|
22163 | 79 |
protected static final Context.Key<Types> typesKey = new Context.Key<>(); |
10 | 80 |
|
81 |
final Symtab syms; |
|
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1469
diff
changeset
|
82 |
final JavacMessages messages; |
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1257
diff
changeset
|
83 |
final Names names; |
10 | 84 |
final boolean allowBoxing; |
9595
a7dea6cbd5f7
7038363: cast from object to primitive should be for source >= 1.7
jjg
parents:
9303
diff
changeset
|
85 |
final boolean allowCovariantReturns; |
a7dea6cbd5f7
7038363: cast from object to primitive should be for source >= 1.7
jjg
parents:
9303
diff
changeset
|
86 |
final boolean allowObjectToPrimitiveCast; |
10 | 87 |
final ClassReader reader; |
88 |
final Check chk; |
|
18730
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
89 |
final Enter enter; |
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
90 |
JCDiagnostic.Factory diags; |
10 | 91 |
List<Warner> warnStack = List.nil(); |
92 |
final Name capturedName; |
|
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
93 |
private final FunctionDescriptorLookupError functionDescriptorLookupError; |
10 | 94 |
|
14547 | 95 |
public final Warner noWarnings; |
96 |
||
10 | 97 |
// <editor-fold defaultstate="collapsed" desc="Instantiating"> |
98 |
public static Types instance(Context context) { |
|
99 |
Types instance = context.get(typesKey); |
|
100 |
if (instance == null) |
|
101 |
instance = new Types(context); |
|
102 |
return instance; |
|
103 |
} |
|
104 |
||
105 |
protected Types(Context context) { |
|
106 |
context.put(typesKey, this); |
|
107 |
syms = Symtab.instance(context); |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1257
diff
changeset
|
108 |
names = Names.instance(context); |
9595
a7dea6cbd5f7
7038363: cast from object to primitive should be for source >= 1.7
jjg
parents:
9303
diff
changeset
|
109 |
Source source = Source.instance(context); |
a7dea6cbd5f7
7038363: cast from object to primitive should be for source >= 1.7
jjg
parents:
9303
diff
changeset
|
110 |
allowBoxing = source.allowBoxing(); |
a7dea6cbd5f7
7038363: cast from object to primitive should be for source >= 1.7
jjg
parents:
9303
diff
changeset
|
111 |
allowCovariantReturns = source.allowCovariantReturns(); |
a7dea6cbd5f7
7038363: cast from object to primitive should be for source >= 1.7
jjg
parents:
9303
diff
changeset
|
112 |
allowObjectToPrimitiveCast = source.allowObjectToPrimitiveCast(); |
10 | 113 |
reader = ClassReader.instance(context); |
114 |
chk = Check.instance(context); |
|
18730
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
115 |
enter = Enter.instance(context); |
10 | 116 |
capturedName = names.fromString("<captured wildcard>"); |
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1469
diff
changeset
|
117 |
messages = JavacMessages.instance(context); |
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
118 |
diags = JCDiagnostic.Factory.instance(context); |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
119 |
functionDescriptorLookupError = new FunctionDescriptorLookupError(); |
14547 | 120 |
noWarnings = new Warner(null); |
10 | 121 |
} |
122 |
// </editor-fold> |
|
123 |
||
124 |
// <editor-fold defaultstate="collapsed" desc="upperBound"> |
|
125 |
/** |
|
126 |
* The "rvalue conversion".<br> |
|
127 |
* The upper bound of most types is the type |
|
128 |
* itself. Wildcards, on the other hand have upper |
|
129 |
* and lower bounds. |
|
130 |
* @param t a type |
|
131 |
* @return the upper bound of the given type |
|
132 |
*/ |
|
133 |
public Type upperBound(Type t) { |
|
24396 | 134 |
return upperBound.visit(t); |
10 | 135 |
} |
136 |
// where |
|
137 |
private final MapVisitor<Void> upperBound = new MapVisitor<Void>() { |
|
138 |
||
139 |
@Override |
|
140 |
public Type visitWildcardType(WildcardType t, Void ignored) { |
|
141 |
if (t.isSuperBound()) |
|
142 |
return t.bound == null ? syms.objectType : t.bound.bound; |
|
143 |
else |
|
144 |
return visit(t.type); |
|
145 |
} |
|
146 |
||
147 |
@Override |
|
148 |
public Type visitCapturedType(CapturedType t, Void ignored) { |
|
149 |
return visit(t.bound); |
|
150 |
} |
|
151 |
}; |
|
152 |
// </editor-fold> |
|
153 |
||
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
154 |
// <editor-fold defaultstate="collapsed" desc="wildLowerBound"> |
10 | 155 |
/** |
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
156 |
* Get a wildcard's lower bound, returning non-wildcards unchanged. |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
157 |
* @param t a type argument, either a wildcard or a type |
10 | 158 |
*/ |
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
159 |
public Type wildLowerBound(Type t) { |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
160 |
if (t.hasTag(WILDCARD)) { |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
161 |
WildcardType w = (WildcardType) t; |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
162 |
return w.isExtendsBound() ? syms.botType : wildLowerBound(w.type); |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
163 |
} |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
164 |
else return t; |
10 | 165 |
} |
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
166 |
// </editor-fold> |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
167 |
|
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
168 |
// <editor-fold defaultstate="collapsed" desc="cvarLowerBound"> |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
169 |
/** |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
170 |
* Get a capture variable's lower bound, returning other types unchanged. |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
171 |
* @param t a type |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
172 |
*/ |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
173 |
public Type cvarLowerBound(Type t) { |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
174 |
if (t.hasTag(TYPEVAR) && ((TypeVar) t).isCaptured()) { |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
175 |
return cvarLowerBound(t.getLowerBound()); |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
176 |
} |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
177 |
else return t; |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
178 |
} |
10 | 179 |
// </editor-fold> |
180 |
||
181 |
// <editor-fold defaultstate="collapsed" desc="isUnbounded"> |
|
182 |
/** |
|
183 |
* Checks that all the arguments to a class are unbounded |
|
184 |
* wildcards or something else that doesn't make any restrictions |
|
185 |
* on the arguments. If a class isUnbounded, a raw super- or |
|
186 |
* subclass can be cast to it without a warning. |
|
187 |
* @param t a type |
|
188 |
* @return true iff the given type is unbounded or raw |
|
189 |
*/ |
|
190 |
public boolean isUnbounded(Type t) { |
|
191 |
return isUnbounded.visit(t); |
|
192 |
} |
|
193 |
// where |
|
194 |
private final UnaryVisitor<Boolean> isUnbounded = new UnaryVisitor<Boolean>() { |
|
195 |
||
196 |
public Boolean visitType(Type t, Void ignored) { |
|
197 |
return true; |
|
198 |
} |
|
199 |
||
200 |
@Override |
|
201 |
public Boolean visitClassType(ClassType t, Void ignored) { |
|
202 |
List<Type> parms = t.tsym.type.allparams(); |
|
203 |
List<Type> args = t.allparams(); |
|
204 |
while (parms.nonEmpty()) { |
|
205 |
WildcardType unb = new WildcardType(syms.objectType, |
|
206 |
BoundKind.UNBOUND, |
|
207 |
syms.boundClass, |
|
24396 | 208 |
(TypeVar)parms.head); |
10 | 209 |
if (!containsType(args.head, unb)) |
210 |
return false; |
|
211 |
parms = parms.tail; |
|
212 |
args = args.tail; |
|
213 |
} |
|
214 |
return true; |
|
215 |
} |
|
216 |
}; |
|
217 |
// </editor-fold> |
|
218 |
||
219 |
// <editor-fold defaultstate="collapsed" desc="asSub"> |
|
220 |
/** |
|
221 |
* Return the least specific subtype of t that starts with symbol |
|
222 |
* sym. If none exists, return null. The least specific subtype |
|
223 |
* is determined as follows: |
|
224 |
* |
|
225 |
* <p>If there is exactly one parameterized instance of sym that is a |
|
226 |
* subtype of t, that parameterized instance is returned.<br> |
|
227 |
* Otherwise, if the plain type or raw type `sym' is a subtype of |
|
228 |
* type t, the type `sym' itself is returned. Otherwise, null is |
|
229 |
* returned. |
|
230 |
*/ |
|
231 |
public Type asSub(Type t, Symbol sym) { |
|
232 |
return asSub.visit(t, sym); |
|
233 |
} |
|
234 |
// where |
|
235 |
private final SimpleVisitor<Type,Symbol> asSub = new SimpleVisitor<Type,Symbol>() { |
|
236 |
||
237 |
public Type visitType(Type t, Symbol sym) { |
|
238 |
return null; |
|
239 |
} |
|
240 |
||
241 |
@Override |
|
242 |
public Type visitClassType(ClassType t, Symbol sym) { |
|
243 |
if (t.tsym == sym) |
|
244 |
return t; |
|
21712
d363f367d4c0
8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents:
21041
diff
changeset
|
245 |
Type base = asSuper(sym.type, t.tsym); |
10 | 246 |
if (base == null) |
247 |
return null; |
|
22163 | 248 |
ListBuffer<Type> from = new ListBuffer<>(); |
249 |
ListBuffer<Type> to = new ListBuffer<>(); |
|
10 | 250 |
try { |
251 |
adapt(base, t, from, to); |
|
252 |
} catch (AdaptFailure ex) { |
|
253 |
return null; |
|
254 |
} |
|
255 |
Type res = subst(sym.type, from.toList(), to.toList()); |
|
256 |
if (!isSubtype(res, t)) |
|
257 |
return null; |
|
22163 | 258 |
ListBuffer<Type> openVars = new ListBuffer<>(); |
10 | 259 |
for (List<Type> l = sym.type.allparams(); |
260 |
l.nonEmpty(); l = l.tail) |
|
261 |
if (res.contains(l.head) && !t.contains(l.head)) |
|
262 |
openVars.append(l.head); |
|
263 |
if (openVars.nonEmpty()) { |
|
264 |
if (t.isRaw()) { |
|
265 |
// The subtype of a raw type is raw |
|
266 |
res = erasure(res); |
|
267 |
} else { |
|
268 |
// Unbound type arguments default to ? |
|
269 |
List<Type> opens = openVars.toList(); |
|
22163 | 270 |
ListBuffer<Type> qs = new ListBuffer<>(); |
10 | 271 |
for (List<Type> iter = opens; iter.nonEmpty(); iter = iter.tail) { |
24396 | 272 |
qs.append(new WildcardType(syms.objectType, BoundKind.UNBOUND, |
273 |
syms.boundClass, (TypeVar) iter.head, |
|
274 |
Type.noAnnotations)); |
|
10 | 275 |
} |
276 |
res = subst(res, opens, qs.toList()); |
|
277 |
} |
|
278 |
} |
|
279 |
return res; |
|
280 |
} |
|
281 |
||
282 |
@Override |
|
283 |
public Type visitErrorType(ErrorType t, Symbol sym) { |
|
284 |
return t; |
|
285 |
} |
|
286 |
}; |
|
287 |
// </editor-fold> |
|
288 |
||
289 |
// <editor-fold defaultstate="collapsed" desc="isConvertible"> |
|
290 |
/** |
|
10452
c8f32009b413
7085024: internal error; cannot instantiate Foo
mcimadamore
parents:
9812
diff
changeset
|
291 |
* Is t a subtype of or convertible via boxing/unboxing |
c8f32009b413
7085024: internal error; cannot instantiate Foo
mcimadamore
parents:
9812
diff
changeset
|
292 |
* conversion to s? |
10 | 293 |
*/ |
294 |
public boolean isConvertible(Type t, Type s, Warner warn) { |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
295 |
if (t.hasTag(ERROR)) { |
10452
c8f32009b413
7085024: internal error; cannot instantiate Foo
mcimadamore
parents:
9812
diff
changeset
|
296 |
return true; |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
297 |
} |
10 | 298 |
boolean tPrimitive = t.isPrimitive(); |
299 |
boolean sPrimitive = s.isPrimitive(); |
|
7643
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7637
diff
changeset
|
300 |
if (tPrimitive == sPrimitive) { |
10 | 301 |
return isSubtypeUnchecked(t, s, warn); |
7643
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7637
diff
changeset
|
302 |
} |
10 | 303 |
if (!allowBoxing) return false; |
304 |
return tPrimitive |
|
305 |
? isSubtype(boxedClass(t).type, s) |
|
306 |
: isSubtype(unboxedType(t), s); |
|
307 |
} |
|
308 |
||
309 |
/** |
|
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
23810
diff
changeset
|
310 |
* Is t a subtype of or convertible via boxing/unboxing |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
23810
diff
changeset
|
311 |
* conversions to s? |
10 | 312 |
*/ |
313 |
public boolean isConvertible(Type t, Type s) { |
|
14547 | 314 |
return isConvertible(t, s, noWarnings); |
10 | 315 |
} |
316 |
// </editor-fold> |
|
317 |
||
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
318 |
// <editor-fold defaultstate="collapsed" desc="findSam"> |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
319 |
|
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
320 |
/** |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
321 |
* Exception used to report a function descriptor lookup failure. The exception |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
322 |
* wraps a diagnostic that can be used to generate more details error |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
323 |
* messages. |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
324 |
*/ |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
325 |
public static class FunctionDescriptorLookupError extends RuntimeException { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
326 |
private static final long serialVersionUID = 0; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
327 |
|
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
328 |
JCDiagnostic diagnostic; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
329 |
|
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
330 |
FunctionDescriptorLookupError() { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
331 |
this.diagnostic = null; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
332 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
333 |
|
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
334 |
FunctionDescriptorLookupError setMessage(JCDiagnostic diag) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
335 |
this.diagnostic = diag; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
336 |
return this; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
337 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
338 |
|
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
339 |
public JCDiagnostic getDiagnostic() { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
340 |
return diagnostic; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
341 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
342 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
343 |
|
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
344 |
/** |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
345 |
* A cache that keeps track of function descriptors associated with given |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
346 |
* functional interfaces. |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
347 |
*/ |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
348 |
class DescriptorCache { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
349 |
|
22163 | 350 |
private WeakHashMap<TypeSymbol, Entry> _map = new WeakHashMap<>(); |
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
351 |
|
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
352 |
class FunctionDescriptor { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
353 |
Symbol descSym; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
354 |
|
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
355 |
FunctionDescriptor(Symbol descSym) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
356 |
this.descSym = descSym; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
357 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
358 |
|
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
359 |
public Symbol getSymbol() { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
360 |
return descSym; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
361 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
362 |
|
15374
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
15361
diff
changeset
|
363 |
public Type getType(Type site) { |
16292
3be2cdacb3b3
8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents:
15718
diff
changeset
|
364 |
site = removeWildcards(site); |
3be2cdacb3b3
8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents:
15718
diff
changeset
|
365 |
if (!chk.checkValidGenericType(site)) { |
3be2cdacb3b3
8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents:
15718
diff
changeset
|
366 |
//if the inferred functional interface type is not well-formed, |
3be2cdacb3b3
8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents:
15718
diff
changeset
|
367 |
//or if it's not a subtype of the original target, issue an error |
3be2cdacb3b3
8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents:
15718
diff
changeset
|
368 |
throw failure(diags.fragment("no.suitable.functional.intf.inst", site)); |
15374
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
15361
diff
changeset
|
369 |
} |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
15361
diff
changeset
|
370 |
return memberType(site, descSym); |
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
371 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
372 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
373 |
|
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
374 |
class Entry { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
375 |
final FunctionDescriptor cachedDescRes; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
376 |
final int prevMark; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
377 |
|
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
378 |
public Entry(FunctionDescriptor cachedDescRes, |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
379 |
int prevMark) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
380 |
this.cachedDescRes = cachedDescRes; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
381 |
this.prevMark = prevMark; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
382 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
383 |
|
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
384 |
boolean matches(int mark) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
385 |
return this.prevMark == mark; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
386 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
387 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
388 |
|
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
389 |
FunctionDescriptor get(TypeSymbol origin) throws FunctionDescriptorLookupError { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
390 |
Entry e = _map.get(origin); |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
391 |
CompoundScope members = membersClosure(origin.type, false); |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
392 |
if (e == null || |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
393 |
!e.matches(members.getMark())) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
394 |
FunctionDescriptor descRes = findDescriptorInternal(origin, members); |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
395 |
_map.put(origin, new Entry(descRes, members.getMark())); |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
396 |
return descRes; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
397 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
398 |
else { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
399 |
return e.cachedDescRes; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
400 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
401 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
402 |
|
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
403 |
/** |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
404 |
* Compute the function descriptor associated with a given functional interface |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
405 |
*/ |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
406 |
public FunctionDescriptor findDescriptorInternal(TypeSymbol origin, |
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
407 |
CompoundScope membersCache) throws FunctionDescriptorLookupError { |
15361
01f1828683e6
8005299: Add FunctionalInterface checking to javac
mcimadamore
parents:
14949
diff
changeset
|
408 |
if (!origin.isInterface() || (origin.flags() & ANNOTATION) != 0) { |
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
409 |
//t must be an interface |
15361
01f1828683e6
8005299: Add FunctionalInterface checking to javac
mcimadamore
parents:
14949
diff
changeset
|
410 |
throw failure("not.a.functional.intf", origin); |
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
411 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
412 |
|
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19914
diff
changeset
|
413 |
final ListBuffer<Symbol> abstracts = new ListBuffer<>(); |
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
414 |
for (Symbol sym : membersCache.getElements(new DescriptorFilter(origin))) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
415 |
Type mtype = memberType(origin.type, sym); |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
416 |
if (abstracts.isEmpty() || |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
417 |
(sym.name == abstracts.first().name && |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
418 |
overrideEquivalent(mtype, memberType(origin.type, abstracts.first())))) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
419 |
abstracts.append(sym); |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
420 |
} else { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
421 |
//the target method(s) should be the only abstract members of t |
15361
01f1828683e6
8005299: Add FunctionalInterface checking to javac
mcimadamore
parents:
14949
diff
changeset
|
422 |
throw failure("not.a.functional.intf.1", origin, |
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
423 |
diags.fragment("incompatible.abstracts", Kinds.kindName(origin), origin)); |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
424 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
425 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
426 |
if (abstracts.isEmpty()) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
427 |
//t must define a suitable non-generic method |
15361
01f1828683e6
8005299: Add FunctionalInterface checking to javac
mcimadamore
parents:
14949
diff
changeset
|
428 |
throw failure("not.a.functional.intf.1", origin, |
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
429 |
diags.fragment("no.abstracts", Kinds.kindName(origin), origin)); |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
430 |
} else if (abstracts.size() == 1) { |
14723
46aa71a5e4e0
8004102: Add support for generic functional descriptors
mcimadamore
parents:
14719
diff
changeset
|
431 |
return new FunctionDescriptor(abstracts.first()); |
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
432 |
} else { // size > 1 |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
433 |
FunctionDescriptor descRes = mergeDescriptors(origin, abstracts.toList()); |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
434 |
if (descRes == null) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
435 |
//we can get here if the functional interface is ill-formed |
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19914
diff
changeset
|
436 |
ListBuffer<JCDiagnostic> descriptors = new ListBuffer<>(); |
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
437 |
for (Symbol desc : abstracts) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
438 |
String key = desc.type.getThrownTypes().nonEmpty() ? |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
439 |
"descriptor.throws" : "descriptor"; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
440 |
descriptors.append(diags.fragment(key, desc.name, |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
441 |
desc.type.getParameterTypes(), |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
442 |
desc.type.getReturnType(), |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
443 |
desc.type.getThrownTypes())); |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
444 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
445 |
JCDiagnostic.MultilineDiagnostic incompatibleDescriptors = |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
446 |
new JCDiagnostic.MultilineDiagnostic(diags.fragment("incompatible.descs.in.functional.intf", |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
447 |
Kinds.kindName(origin), origin), descriptors.toList()); |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
448 |
throw failure(incompatibleDescriptors); |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
449 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
450 |
return descRes; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
451 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
452 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
453 |
|
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
454 |
/** |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
455 |
* Compute a synthetic type for the target descriptor given a list |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
456 |
* of override-equivalent methods in the functional interface type. |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
457 |
* The resulting method type is a method type that is override-equivalent |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
458 |
* and return-type substitutable with each method in the original list. |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
459 |
*/ |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
460 |
private FunctionDescriptor mergeDescriptors(TypeSymbol origin, List<Symbol> methodSyms) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
461 |
//pick argument types - simply take the signature that is a |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
462 |
//subsignature of all other signatures in the list (as per JLS 8.4.2) |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
463 |
List<Symbol> mostSpecific = List.nil(); |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
464 |
outer: for (Symbol msym1 : methodSyms) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
465 |
Type mt1 = memberType(origin.type, msym1); |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
466 |
for (Symbol msym2 : methodSyms) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
467 |
Type mt2 = memberType(origin.type, msym2); |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
468 |
if (!isSubSignature(mt1, mt2)) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
469 |
continue outer; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
470 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
471 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
472 |
mostSpecific = mostSpecific.prepend(msym1); |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
473 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
474 |
if (mostSpecific.isEmpty()) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
475 |
return null; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
476 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
477 |
|
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
478 |
|
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
479 |
//pick return types - this is done in two phases: (i) first, the most |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
480 |
//specific return type is chosen using strict subtyping; if this fails, |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
481 |
//a second attempt is made using return type substitutability (see JLS 8.4.5) |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
482 |
boolean phase2 = false; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
483 |
Symbol bestSoFar = null; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
484 |
while (bestSoFar == null) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
485 |
outer: for (Symbol msym1 : mostSpecific) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
486 |
Type mt1 = memberType(origin.type, msym1); |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
487 |
for (Symbol msym2 : methodSyms) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
488 |
Type mt2 = memberType(origin.type, msym2); |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
489 |
if (phase2 ? |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
490 |
!returnTypeSubstitutable(mt1, mt2) : |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
491 |
!isSubtypeInternal(mt1.getReturnType(), mt2.getReturnType())) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
492 |
continue outer; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
493 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
494 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
495 |
bestSoFar = msym1; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
496 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
497 |
if (phase2) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
498 |
break; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
499 |
} else { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
500 |
phase2 = true; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
501 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
502 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
503 |
if (bestSoFar == null) return null; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
504 |
|
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
505 |
//merge thrown types - form the intersection of all the thrown types in |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
506 |
//all the signatures in the list |
19655
39cf56516b13
8022316: Generic throws, overriding and method reference
vromero
parents:
19127
diff
changeset
|
507 |
boolean toErase = !bestSoFar.type.hasTag(FORALL); |
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
508 |
List<Type> thrown = null; |
19655
39cf56516b13
8022316: Generic throws, overriding and method reference
vromero
parents:
19127
diff
changeset
|
509 |
Type mt1 = memberType(origin.type, bestSoFar); |
39cf56516b13
8022316: Generic throws, overriding and method reference
vromero
parents:
19127
diff
changeset
|
510 |
for (Symbol msym2 : methodSyms) { |
39cf56516b13
8022316: Generic throws, overriding and method reference
vromero
parents:
19127
diff
changeset
|
511 |
Type mt2 = memberType(origin.type, msym2); |
39cf56516b13
8022316: Generic throws, overriding and method reference
vromero
parents:
19127
diff
changeset
|
512 |
List<Type> thrown_mt2 = mt2.getThrownTypes(); |
39cf56516b13
8022316: Generic throws, overriding and method reference
vromero
parents:
19127
diff
changeset
|
513 |
if (toErase) { |
39cf56516b13
8022316: Generic throws, overriding and method reference
vromero
parents:
19127
diff
changeset
|
514 |
thrown_mt2 = erasure(thrown_mt2); |
39cf56516b13
8022316: Generic throws, overriding and method reference
vromero
parents:
19127
diff
changeset
|
515 |
} else { |
39cf56516b13
8022316: Generic throws, overriding and method reference
vromero
parents:
19127
diff
changeset
|
516 |
/* If bestSoFar is generic then all the methods are generic. |
39cf56516b13
8022316: Generic throws, overriding and method reference
vromero
parents:
19127
diff
changeset
|
517 |
* The opposite is not true: a non generic method can override |
39cf56516b13
8022316: Generic throws, overriding and method reference
vromero
parents:
19127
diff
changeset
|
518 |
* a generic method (raw override) so it's safe to cast mt1 and |
39cf56516b13
8022316: Generic throws, overriding and method reference
vromero
parents:
19127
diff
changeset
|
519 |
* mt2 to ForAll. |
39cf56516b13
8022316: Generic throws, overriding and method reference
vromero
parents:
19127
diff
changeset
|
520 |
*/ |
39cf56516b13
8022316: Generic throws, overriding and method reference
vromero
parents:
19127
diff
changeset
|
521 |
ForAll fa1 = (ForAll)mt1; |
39cf56516b13
8022316: Generic throws, overriding and method reference
vromero
parents:
19127
diff
changeset
|
522 |
ForAll fa2 = (ForAll)mt2; |
39cf56516b13
8022316: Generic throws, overriding and method reference
vromero
parents:
19127
diff
changeset
|
523 |
thrown_mt2 = subst(thrown_mt2, fa2.tvars, fa1.tvars); |
39cf56516b13
8022316: Generic throws, overriding and method reference
vromero
parents:
19127
diff
changeset
|
524 |
} |
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
525 |
thrown = (thrown == null) ? |
19655
39cf56516b13
8022316: Generic throws, overriding and method reference
vromero
parents:
19127
diff
changeset
|
526 |
thrown_mt2 : |
39cf56516b13
8022316: Generic throws, overriding and method reference
vromero
parents:
19127
diff
changeset
|
527 |
chk.intersect(thrown_mt2, thrown); |
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
528 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
529 |
|
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
530 |
final List<Type> thrown1 = thrown; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
531 |
return new FunctionDescriptor(bestSoFar) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
532 |
@Override |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
533 |
public Type getType(Type origin) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
534 |
Type mt = memberType(origin, getSymbol()); |
16313
ca91cf83b25c
8008444: Inherited generic functional descriptors are merged incorrectly
mcimadamore
parents:
16311
diff
changeset
|
535 |
return createMethodTypeWithThrown(mt, thrown1); |
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
536 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
537 |
}; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
538 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
539 |
|
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
540 |
boolean isSubtypeInternal(Type s, Type t) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
541 |
return (s.isPrimitive() && t.isPrimitive()) ? |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
542 |
isSameType(t, s) : |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
543 |
isSubtype(s, t); |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
544 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
545 |
|
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
546 |
FunctionDescriptorLookupError failure(String msg, Object... args) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
547 |
return failure(diags.fragment(msg, args)); |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
548 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
549 |
|
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
550 |
FunctionDescriptorLookupError failure(JCDiagnostic diag) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
551 |
return functionDescriptorLookupError.setMessage(diag); |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
552 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
553 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
554 |
|
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
555 |
private DescriptorCache descCache = new DescriptorCache(); |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
556 |
|
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
557 |
/** |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
558 |
* Find the method descriptor associated to this class symbol - if the |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
559 |
* symbol 'origin' is not a functional interface, an exception is thrown. |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
560 |
*/ |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
561 |
public Symbol findDescriptorSymbol(TypeSymbol origin) throws FunctionDescriptorLookupError { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
562 |
return descCache.get(origin).getSymbol(); |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
563 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
564 |
|
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
565 |
/** |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
566 |
* Find the type of the method descriptor associated to this class symbol - |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
567 |
* if the symbol 'origin' is not a functional interface, an exception is thrown. |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
568 |
*/ |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
569 |
public Type findDescriptorType(Type origin) throws FunctionDescriptorLookupError { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
570 |
return descCache.get(origin.tsym).getType(origin); |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
571 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
572 |
|
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
573 |
/** |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
574 |
* Is given type a functional interface? |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
575 |
*/ |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
576 |
public boolean isFunctionalInterface(TypeSymbol tsym) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
577 |
try { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
578 |
findDescriptorSymbol(tsym); |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
579 |
return true; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
580 |
} catch (FunctionDescriptorLookupError ex) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
581 |
return false; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
582 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
583 |
} |
15374
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
15361
diff
changeset
|
584 |
|
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
15361
diff
changeset
|
585 |
public boolean isFunctionalInterface(Type site) { |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
15361
diff
changeset
|
586 |
try { |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
15361
diff
changeset
|
587 |
findDescriptorType(site); |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
15361
diff
changeset
|
588 |
return true; |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
15361
diff
changeset
|
589 |
} catch (FunctionDescriptorLookupError ex) { |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
15361
diff
changeset
|
590 |
return false; |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
15361
diff
changeset
|
591 |
} |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
15361
diff
changeset
|
592 |
} |
16292
3be2cdacb3b3
8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents:
15718
diff
changeset
|
593 |
|
3be2cdacb3b3
8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents:
15718
diff
changeset
|
594 |
public Type removeWildcards(Type site) { |
16318
14906f8d3a42
8008708: Regression: separate compilation causes crash in wildcards inference logic
mcimadamore
parents:
16313
diff
changeset
|
595 |
Type capturedSite = capture(site); |
14906f8d3a42
8008708: Regression: separate compilation causes crash in wildcards inference logic
mcimadamore
parents:
16313
diff
changeset
|
596 |
if (capturedSite != site) { |
16292
3be2cdacb3b3
8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents:
15718
diff
changeset
|
597 |
Type formalInterface = site.tsym.type; |
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19914
diff
changeset
|
598 |
ListBuffer<Type> typeargs = new ListBuffer<>(); |
16292
3be2cdacb3b3
8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents:
15718
diff
changeset
|
599 |
List<Type> actualTypeargs = site.getTypeArguments(); |
16318
14906f8d3a42
8008708: Regression: separate compilation causes crash in wildcards inference logic
mcimadamore
parents:
16313
diff
changeset
|
600 |
List<Type> capturedTypeargs = capturedSite.getTypeArguments(); |
16292
3be2cdacb3b3
8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents:
15718
diff
changeset
|
601 |
//simply replace the wildcards with its bound |
3be2cdacb3b3
8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents:
15718
diff
changeset
|
602 |
for (Type t : formalInterface.getTypeArguments()) { |
3be2cdacb3b3
8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents:
15718
diff
changeset
|
603 |
if (actualTypeargs.head.hasTag(WILDCARD)) { |
24396 | 604 |
WildcardType wt = (WildcardType)actualTypeargs.head; |
16311
1090c7ac6ae2
8008293: Declared bounds not considered when functional interface containing unbound wildcards is instantiated
mcimadamore
parents:
16300
diff
changeset
|
605 |
Type bound; |
1090c7ac6ae2
8008293: Declared bounds not considered when functional interface containing unbound wildcards is instantiated
mcimadamore
parents:
16300
diff
changeset
|
606 |
switch (wt.kind) { |
16318
14906f8d3a42
8008708: Regression: separate compilation causes crash in wildcards inference logic
mcimadamore
parents:
16313
diff
changeset
|
607 |
case EXTENDS: |
16311
1090c7ac6ae2
8008293: Declared bounds not considered when functional interface containing unbound wildcards is instantiated
mcimadamore
parents:
16300
diff
changeset
|
608 |
case UNBOUND: |
24396 | 609 |
CapturedType capVar = (CapturedType)capturedTypeargs.head; |
16311
1090c7ac6ae2
8008293: Declared bounds not considered when functional interface containing unbound wildcards is instantiated
mcimadamore
parents:
16300
diff
changeset
|
610 |
//use declared bound if it doesn't depend on formal type-args |
16318
14906f8d3a42
8008708: Regression: separate compilation causes crash in wildcards inference logic
mcimadamore
parents:
16313
diff
changeset
|
611 |
bound = capVar.bound.containsAny(capturedSite.getTypeArguments()) ? |
16973
8f424142e725
8011028: lang/INFR/infr001/infr00101md/infr00101md.java fails to compile after switch to JDK8-b82
mcimadamore
parents:
16809
diff
changeset
|
612 |
wt.type : capVar.bound; |
16311
1090c7ac6ae2
8008293: Declared bounds not considered when functional interface containing unbound wildcards is instantiated
mcimadamore
parents:
16300
diff
changeset
|
613 |
break; |
1090c7ac6ae2
8008293: Declared bounds not considered when functional interface containing unbound wildcards is instantiated
mcimadamore
parents:
16300
diff
changeset
|
614 |
default: |
1090c7ac6ae2
8008293: Declared bounds not considered when functional interface containing unbound wildcards is instantiated
mcimadamore
parents:
16300
diff
changeset
|
615 |
bound = wt.type; |
1090c7ac6ae2
8008293: Declared bounds not considered when functional interface containing unbound wildcards is instantiated
mcimadamore
parents:
16300
diff
changeset
|
616 |
} |
1090c7ac6ae2
8008293: Declared bounds not considered when functional interface containing unbound wildcards is instantiated
mcimadamore
parents:
16300
diff
changeset
|
617 |
typeargs.append(bound); |
16292
3be2cdacb3b3
8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents:
15718
diff
changeset
|
618 |
} else { |
3be2cdacb3b3
8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents:
15718
diff
changeset
|
619 |
typeargs.append(actualTypeargs.head); |
3be2cdacb3b3
8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents:
15718
diff
changeset
|
620 |
} |
3be2cdacb3b3
8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents:
15718
diff
changeset
|
621 |
actualTypeargs = actualTypeargs.tail; |
16318
14906f8d3a42
8008708: Regression: separate compilation causes crash in wildcards inference logic
mcimadamore
parents:
16313
diff
changeset
|
622 |
capturedTypeargs = capturedTypeargs.tail; |
16292
3be2cdacb3b3
8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents:
15718
diff
changeset
|
623 |
} |
3be2cdacb3b3
8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents:
15718
diff
changeset
|
624 |
return subst(formalInterface, formalInterface.getTypeArguments(), typeargs.toList()); |
3be2cdacb3b3
8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents:
15718
diff
changeset
|
625 |
} else { |
3be2cdacb3b3
8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents:
15718
diff
changeset
|
626 |
return site; |
3be2cdacb3b3
8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents:
15718
diff
changeset
|
627 |
} |
3be2cdacb3b3
8006749: compiler does not allow Object protected methods to be used in lambda
mcimadamore
parents:
15718
diff
changeset
|
628 |
} |
18730
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
629 |
|
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
630 |
/** |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
631 |
* Create a symbol for a class that implements a given functional interface |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
632 |
* and overrides its functional descriptor. This routine is used for two |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
633 |
* main purposes: (i) checking well-formedness of a functional interface; |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
634 |
* (ii) perform functional interface bridge calculation. |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
635 |
*/ |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
636 |
public ClassSymbol makeFunctionalInterfaceClass(Env<AttrContext> env, Name name, List<Type> targets, long cflags) { |
19127
8a0cbd5cb055
8020804: javac crashes when speculative attribution infers intersection type with array component
mcimadamore
parents:
19124
diff
changeset
|
637 |
if (targets.isEmpty() || !isFunctionalInterface(targets.head)) { |
8a0cbd5cb055
8020804: javac crashes when speculative attribution infers intersection type with array component
mcimadamore
parents:
19124
diff
changeset
|
638 |
return null; |
8a0cbd5cb055
8020804: javac crashes when speculative attribution infers intersection type with array component
mcimadamore
parents:
19124
diff
changeset
|
639 |
} |
18730
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
640 |
Symbol descSym = findDescriptorSymbol(targets.head.tsym); |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
641 |
Type descType = findDescriptorType(targets.head); |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
642 |
ClassSymbol csym = new ClassSymbol(cflags, name, env.enclClass.sym.outermostClass()); |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
643 |
csym.completer = null; |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
644 |
csym.members_field = new Scope(csym); |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
645 |
MethodSymbol instDescSym = new MethodSymbol(descSym.flags(), descSym.name, descType, csym); |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
646 |
csym.members_field.enter(instDescSym); |
24396 | 647 |
Type.ClassType ctype = new Type.ClassType(Type.noType, List.<Type>nil(), csym, |
648 |
Type.noAnnotations); |
|
18730
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
649 |
ctype.supertype_field = syms.objectType; |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
650 |
ctype.interfaces_field = targets; |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
651 |
csym.type = ctype; |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
652 |
csym.sourcefile = ((ClassSymbol)csym.owner).sourcefile; |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
653 |
return csym; |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
654 |
} |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
655 |
|
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
656 |
/** |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
657 |
* Find the minimal set of methods that are overridden by the functional |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
658 |
* descriptor in 'origin'. All returned methods are assumed to have different |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
659 |
* erased signatures. |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
660 |
*/ |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
661 |
public List<Symbol> functionalInterfaceBridges(TypeSymbol origin) { |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
662 |
Assert.check(isFunctionalInterface(origin)); |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
663 |
Symbol descSym = findDescriptorSymbol(origin); |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
664 |
CompoundScope members = membersClosure(origin.type, false); |
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19914
diff
changeset
|
665 |
ListBuffer<Symbol> overridden = new ListBuffer<>(); |
18730
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
666 |
outer: for (Symbol m2 : members.getElementsByName(descSym.name, bridgeFilter)) { |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
667 |
if (m2 == descSym) continue; |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
668 |
else if (descSym.overrides(m2, origin, Types.this, false)) { |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
669 |
for (Symbol m3 : overridden) { |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
670 |
if (isSameType(m3.erasure(Types.this), m2.erasure(Types.this)) || |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
671 |
(m3.overrides(m2, origin, Types.this, false) && |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
672 |
(pendingBridges((ClassSymbol)origin, m3.enclClass()) || |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
673 |
(((MethodSymbol)m2).binaryImplementation((ClassSymbol)m3.owner, Types.this) != null)))) { |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
674 |
continue outer; |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
675 |
} |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
676 |
} |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
677 |
overridden.add(m2); |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
678 |
} |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
679 |
} |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
680 |
return overridden.toList(); |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
681 |
} |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
682 |
//where |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
683 |
private Filter<Symbol> bridgeFilter = new Filter<Symbol>() { |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
684 |
public boolean accepts(Symbol t) { |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
685 |
return t.kind == Kinds.MTH && |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
686 |
t.name != names.init && |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
687 |
t.name != names.clinit && |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
688 |
(t.flags() & SYNTHETIC) == 0; |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
689 |
} |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
690 |
}; |
21712
d363f367d4c0
8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents:
21041
diff
changeset
|
691 |
private boolean pendingBridges(ClassSymbol origin, TypeSymbol s) { |
18730
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
692 |
//a symbol will be completed from a classfile if (a) symbol has |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
693 |
//an associated file object with CLASS kind and (b) the symbol has |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
694 |
//not been entered |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
695 |
if (origin.classfile != null && |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
696 |
origin.classfile.getKind() == JavaFileObject.Kind.CLASS && |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
697 |
enter.getEnv(origin) == null) { |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
698 |
return false; |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
699 |
} |
21712
d363f367d4c0
8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents:
21041
diff
changeset
|
700 |
if (origin == s) { |
18730
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
701 |
return true; |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
702 |
} |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
703 |
for (Type t : interfaces(origin.type)) { |
21712
d363f367d4c0
8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents:
21041
diff
changeset
|
704 |
if (pendingBridges((ClassSymbol)t.tsym, s)) { |
18730
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
705 |
return true; |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
706 |
} |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
707 |
} |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
708 |
return false; |
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
709 |
} |
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
710 |
// </editor-fold> |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
711 |
|
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
712 |
/** |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
713 |
* Scope filter used to skip methods that should be ignored (such as methods |
16809
5acfcb821d65
8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents:
16567
diff
changeset
|
714 |
* overridden by j.l.Object) during function interface conversion interface check |
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
715 |
*/ |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
716 |
class DescriptorFilter implements Filter<Symbol> { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
717 |
|
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
718 |
TypeSymbol origin; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
719 |
|
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
720 |
DescriptorFilter(TypeSymbol origin) { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
721 |
this.origin = origin; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
722 |
} |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
723 |
|
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
724 |
@Override |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
725 |
public boolean accepts(Symbol sym) { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
726 |
return sym.kind == Kinds.MTH && |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
727 |
(sym.flags() & (ABSTRACT | DEFAULT)) == ABSTRACT && |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
728 |
!overridesObjectMethod(origin, sym) && |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
729 |
(interfaceCandidates(origin.type, (MethodSymbol)sym).head.flags() & DEFAULT) == 0; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
730 |
} |
22163 | 731 |
} |
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
732 |
|
10 | 733 |
// <editor-fold defaultstate="collapsed" desc="isSubtype"> |
734 |
/** |
|
735 |
* Is t an unchecked subtype of s? |
|
736 |
*/ |
|
737 |
public boolean isSubtypeUnchecked(Type t, Type s) { |
|
14547 | 738 |
return isSubtypeUnchecked(t, s, noWarnings); |
10 | 739 |
} |
740 |
/** |
|
741 |
* Is t an unchecked subtype of s? |
|
742 |
*/ |
|
743 |
public boolean isSubtypeUnchecked(Type t, Type s, Warner warn) { |
|
10810
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
744 |
boolean result = isSubtypeUncheckedInternal(t, s, warn); |
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
745 |
if (result) { |
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
746 |
checkUnsafeVarargsConversion(t, s, warn); |
660
16c478b6af1d
6507024: casting an array to a generic type results in a 'capture#69 of ?' type error
mcimadamore
parents:
514
diff
changeset
|
747 |
} |
10810
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
748 |
return result; |
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
749 |
} |
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
750 |
//where |
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
751 |
private boolean isSubtypeUncheckedInternal(Type t, Type s, Warner warn) { |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
752 |
if (t.hasTag(ARRAY) && s.hasTag(ARRAY)) { |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
753 |
if (((ArrayType)t).elemtype.isPrimitive()) { |
10810
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
754 |
return isSameType(elemtype(t), elemtype(s)); |
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
755 |
} else { |
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
756 |
return isSubtypeUnchecked(elemtype(t), elemtype(s), warn); |
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
757 |
} |
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
758 |
} else if (isSubtype(t, s)) { |
10 | 759 |
return true; |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
760 |
} else if (t.hasTag(TYPEVAR)) { |
10810
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
761 |
return isSubtypeUnchecked(t.getUpperBound(), s, warn); |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
762 |
} else if (!s.isRaw()) { |
21712
d363f367d4c0
8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents:
21041
diff
changeset
|
763 |
Type t2 = asSuper(t, s.tsym); |
10810
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
764 |
if (t2 != null && t2.isRaw()) { |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
765 |
if (isReifiable(s)) { |
10810
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
766 |
warn.silentWarn(LintCategory.UNCHECKED); |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
767 |
} else { |
10810
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
768 |
warn.warn(LintCategory.UNCHECKED); |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
769 |
} |
10810
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
770 |
return true; |
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
771 |
} |
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
772 |
} |
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
773 |
return false; |
10 | 774 |
} |
10810
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
775 |
|
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
776 |
private void checkUnsafeVarargsConversion(Type t, Type s, Warner warn) { |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
777 |
if (!t.hasTag(ARRAY) || isReifiable(t)) { |
15385 | 778 |
return; |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
779 |
} |
10810
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
780 |
ArrayType from = (ArrayType)t; |
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
781 |
boolean shouldWarn = false; |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
782 |
switch (s.getTag()) { |
10810
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
783 |
case ARRAY: |
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
784 |
ArrayType to = (ArrayType)s; |
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
785 |
shouldWarn = from.isVarargs() && |
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
786 |
!to.isVarargs() && |
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
787 |
!isReifiable(from); |
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
788 |
break; |
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
789 |
case CLASS: |
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
790 |
shouldWarn = from.isVarargs(); |
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
791 |
break; |
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
792 |
} |
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
793 |
if (shouldWarn) { |
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
794 |
warn.warn(LintCategory.VARARGS); |
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
795 |
} |
9444a714fdaf
7097436: Project Coin: duplicate varargs warnings on method annotated with @SafeVarargs
mcimadamore
parents:
10634
diff
changeset
|
796 |
} |
10 | 797 |
|
798 |
/** |
|
799 |
* Is t a subtype of s?<br> |
|
800 |
* (not defined for Method and ForAll types) |
|
801 |
*/ |
|
802 |
final public boolean isSubtype(Type t, Type s) { |
|
803 |
return isSubtype(t, s, true); |
|
804 |
} |
|
805 |
final public boolean isSubtypeNoCapture(Type t, Type s) { |
|
806 |
return isSubtype(t, s, false); |
|
807 |
} |
|
808 |
public boolean isSubtype(Type t, Type s, boolean capture) { |
|
809 |
if (t == s) |
|
810 |
return true; |
|
811 |
||
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
812 |
if (s.isPartial()) |
10 | 813 |
return isSuperType(s, t); |
814 |
||
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
2223
diff
changeset
|
815 |
if (s.isCompound()) { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
2223
diff
changeset
|
816 |
for (Type s2 : interfaces(s).prepend(supertype(s))) { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
2223
diff
changeset
|
817 |
if (!isSubtype(t, s2, capture)) |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
2223
diff
changeset
|
818 |
return false; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
2223
diff
changeset
|
819 |
} |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
2223
diff
changeset
|
820 |
return true; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
2223
diff
changeset
|
821 |
} |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
2223
diff
changeset
|
822 |
|
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
823 |
// Generally, if 's' is a type variable, recur on lower bound; but |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
824 |
// for alpha <: CAP, alpha should get upper bound CAP |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
825 |
if (!t.hasTag(UNDETVAR)) { |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
826 |
// TODO: JDK-8039198, bounds checking sometimes passes in a wildcard as s |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
827 |
Type lower = cvarLowerBound(wildLowerBound(s)); |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
828 |
if (s != lower) |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
829 |
return isSubtype(capture ? capture(t) : t, lower, false); |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
830 |
} |
10 | 831 |
|
832 |
return isSubtype.visit(capture ? capture(t) : t, s); |
|
833 |
} |
|
834 |
// where |
|
835 |
private TypeRelation isSubtype = new TypeRelation() |
|
836 |
{ |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
837 |
@Override |
10 | 838 |
public Boolean visitType(Type t, Type s) { |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
839 |
switch (t.getTag()) { |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
840 |
case BYTE: |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
841 |
return (!s.hasTag(CHAR) && t.getTag().isSubRangeOf(s.getTag())); |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
842 |
case CHAR: |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
843 |
return (!s.hasTag(SHORT) && t.getTag().isSubRangeOf(s.getTag())); |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
844 |
case SHORT: case INT: case LONG: |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
845 |
case FLOAT: case DOUBLE: |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
846 |
return t.getTag().isSubRangeOf(s.getTag()); |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
847 |
case BOOLEAN: case VOID: |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
848 |
return t.hasTag(s.getTag()); |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
849 |
case TYPEVAR: |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
850 |
return isSubtypeNoCapture(t.getUpperBound(), s); |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
851 |
case BOT: |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
852 |
return |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
853 |
s.hasTag(BOT) || s.hasTag(CLASS) || |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
854 |
s.hasTag(ARRAY) || s.hasTag(TYPEVAR); |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
855 |
case WILDCARD: //we shouldn't be here - avoids crash (see 7034495) |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
856 |
case NONE: |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
857 |
return false; |
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
858 |
default: |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
859 |
throw new AssertionError("isSubtype " + t.getTag()); |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
860 |
} |
10 | 861 |
} |
862 |
||
22163 | 863 |
private Set<TypePair> cache = new HashSet<>(); |
10 | 864 |
|
865 |
private boolean containsTypeRecursive(Type t, Type s) { |
|
866 |
TypePair pair = new TypePair(t, s); |
|
867 |
if (cache.add(pair)) { |
|
868 |
try { |
|
869 |
return containsType(t.getTypeArguments(), |
|
870 |
s.getTypeArguments()); |
|
871 |
} finally { |
|
872 |
cache.remove(pair); |
|
873 |
} |
|
874 |
} else { |
|
875 |
return containsType(t.getTypeArguments(), |
|
876 |
rewriteSupers(s).getTypeArguments()); |
|
877 |
} |
|
878 |
} |
|
879 |
||
880 |
private Type rewriteSupers(Type t) { |
|
881 |
if (!t.isParameterized()) |
|
882 |
return t; |
|
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19914
diff
changeset
|
883 |
ListBuffer<Type> from = new ListBuffer<>(); |
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19914
diff
changeset
|
884 |
ListBuffer<Type> to = new ListBuffer<>(); |
10 | 885 |
adaptSelf(t, from, to); |
886 |
if (from.isEmpty()) |
|
887 |
return t; |
|
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19914
diff
changeset
|
888 |
ListBuffer<Type> rewrite = new ListBuffer<>(); |
10 | 889 |
boolean changed = false; |
890 |
for (Type orig : to.toList()) { |
|
891 |
Type s = rewriteSupers(orig); |
|
892 |
if (s.isSuperBound() && !s.isExtendsBound()) { |
|
893 |
s = new WildcardType(syms.objectType, |
|
894 |
BoundKind.UNBOUND, |
|
24396 | 895 |
syms.boundClass, |
896 |
s.getAnnotationMirrors()); |
|
10 | 897 |
changed = true; |
898 |
} else if (s != orig) { |
|
899 |
s = new WildcardType(upperBound(s), |
|
900 |
BoundKind.EXTENDS, |
|
24396 | 901 |
syms.boundClass, |
902 |
s.getAnnotationMirrors()); |
|
10 | 903 |
changed = true; |
904 |
} |
|
905 |
rewrite.append(s); |
|
906 |
} |
|
907 |
if (changed) |
|
908 |
return subst(t.tsym.type, from.toList(), rewrite.toList()); |
|
909 |
else |
|
910 |
return t; |
|
911 |
} |
|
912 |
||
913 |
@Override |
|
914 |
public Boolean visitClassType(ClassType t, Type s) { |
|
21712
d363f367d4c0
8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents:
21041
diff
changeset
|
915 |
Type sup = asSuper(t, s.tsym); |
10 | 916 |
return sup != null |
917 |
&& sup.tsym == s.tsym |
|
918 |
// You're not allowed to write |
|
919 |
// Vector<Object> vec = new Vector<String>(); |
|
920 |
// But with wildcards you can write |
|
921 |
// Vector<? extends Object> vec = new Vector<String>(); |
|
922 |
// which means that subtype checking must be done |
|
923 |
// here instead of same-type checking (via containsType). |
|
924 |
&& (!s.isParameterized() || containsTypeRecursive(s, sup)) |
|
925 |
&& isSubtypeNoCapture(sup.getEnclosingType(), |
|
926 |
s.getEnclosingType()); |
|
927 |
} |
|
928 |
||
929 |
@Override |
|
930 |
public Boolean visitArrayType(ArrayType t, Type s) { |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
931 |
if (s.hasTag(ARRAY)) { |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
932 |
if (t.elemtype.isPrimitive()) |
10 | 933 |
return isSameType(t.elemtype, elemtype(s)); |
934 |
else |
|
935 |
return isSubtypeNoCapture(t.elemtype, elemtype(s)); |
|
936 |
} |
|
937 |
||
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
938 |
if (s.hasTag(CLASS)) { |
10 | 939 |
Name sname = s.tsym.getQualifiedName(); |
940 |
return sname == names.java_lang_Object |
|
941 |
|| sname == names.java_lang_Cloneable |
|
942 |
|| sname == names.java_io_Serializable; |
|
943 |
} |
|
944 |
||
945 |
return false; |
|
946 |
} |
|
947 |
||
948 |
@Override |
|
949 |
public Boolean visitUndetVar(UndetVar t, Type s) { |
|
950 |
//todo: test against origin needed? or replace with substitution? |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
951 |
if (t == s || t.qtype == s || s.hasTag(ERROR) || s.hasTag(UNKNOWN)) { |
10 | 952 |
return true; |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
953 |
} else if (s.hasTag(BOT)) { |
10634
7f15f5a11ae9
7086586: Inference producing null type argument
mcimadamore
parents:
10453
diff
changeset
|
954 |
//if 's' is 'null' there's no instantiated type U for which |
7f15f5a11ae9
7086586: Inference producing null type argument
mcimadamore
parents:
10453
diff
changeset
|
955 |
//U <: s (but 'null' itself, which is not a valid type) |
7f15f5a11ae9
7086586: Inference producing null type argument
mcimadamore
parents:
10453
diff
changeset
|
956 |
return false; |
7f15f5a11ae9
7086586: Inference producing null type argument
mcimadamore
parents:
10453
diff
changeset
|
957 |
} |
10 | 958 |
|
14048
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13689
diff
changeset
|
959 |
t.addBound(InferenceBound.UPPER, s, Types.this); |
10 | 960 |
return true; |
961 |
} |
|
962 |
||
963 |
@Override |
|
964 |
public Boolean visitErrorType(ErrorType t, Type s) { |
|
965 |
return true; |
|
966 |
} |
|
967 |
}; |
|
968 |
||
969 |
/** |
|
970 |
* Is t a subtype of every type in given list `ts'?<br> |
|
971 |
* (not defined for Method and ForAll types)<br> |
|
972 |
* Allows unchecked conversions. |
|
973 |
*/ |
|
974 |
public boolean isSubtypeUnchecked(Type t, List<Type> ts, Warner warn) { |
|
975 |
for (List<Type> l = ts; l.nonEmpty(); l = l.tail) |
|
976 |
if (!isSubtypeUnchecked(t, l.head, warn)) |
|
977 |
return false; |
|
978 |
return true; |
|
979 |
} |
|
980 |
||
981 |
/** |
|
982 |
* Are corresponding elements of ts subtypes of ss? If lists are |
|
983 |
* of different length, return false. |
|
984 |
*/ |
|
985 |
public boolean isSubtypes(List<Type> ts, List<Type> ss) { |
|
986 |
while (ts.tail != null && ss.tail != null |
|
987 |
/*inlined: ts.nonEmpty() && ss.nonEmpty()*/ && |
|
988 |
isSubtype(ts.head, ss.head)) { |
|
989 |
ts = ts.tail; |
|
990 |
ss = ss.tail; |
|
991 |
} |
|
992 |
return ts.tail == null && ss.tail == null; |
|
993 |
/*inlined: ts.isEmpty() && ss.isEmpty();*/ |
|
994 |
} |
|
995 |
||
996 |
/** |
|
997 |
* Are corresponding elements of ts subtypes of ss, allowing |
|
998 |
* unchecked conversions? If lists are of different length, |
|
999 |
* return false. |
|
1000 |
**/ |
|
1001 |
public boolean isSubtypesUnchecked(List<Type> ts, List<Type> ss, Warner warn) { |
|
1002 |
while (ts.tail != null && ss.tail != null |
|
1003 |
/*inlined: ts.nonEmpty() && ss.nonEmpty()*/ && |
|
1004 |
isSubtypeUnchecked(ts.head, ss.head, warn)) { |
|
1005 |
ts = ts.tail; |
|
1006 |
ss = ss.tail; |
|
1007 |
} |
|
1008 |
return ts.tail == null && ss.tail == null; |
|
1009 |
/*inlined: ts.isEmpty() && ss.isEmpty();*/ |
|
1010 |
} |
|
1011 |
// </editor-fold> |
|
1012 |
||
1013 |
// <editor-fold defaultstate="collapsed" desc="isSuperType"> |
|
1014 |
/** |
|
1015 |
* Is t a supertype of s? |
|
1016 |
*/ |
|
1017 |
public boolean isSuperType(Type t, Type s) { |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1018 |
switch (t.getTag()) { |
10 | 1019 |
case ERROR: |
1020 |
return true; |
|
1021 |
case UNDETVAR: { |
|
1022 |
UndetVar undet = (UndetVar)t; |
|
1023 |
if (t == s || |
|
1024 |
undet.qtype == s || |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1025 |
s.hasTag(ERROR) || |
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1026 |
s.hasTag(BOT)) { |
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1027 |
return true; |
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1028 |
} |
14048
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13689
diff
changeset
|
1029 |
undet.addBound(InferenceBound.LOWER, s, this); |
10 | 1030 |
return true; |
1031 |
} |
|
1032 |
default: |
|
1033 |
return isSubtype(s, t); |
|
1034 |
} |
|
1035 |
} |
|
1036 |
// </editor-fold> |
|
1037 |
||
1038 |
// <editor-fold defaultstate="collapsed" desc="isSameType"> |
|
1039 |
/** |
|
1040 |
* Are corresponding elements of the lists the same type? If |
|
1041 |
* lists are of different length, return false. |
|
1042 |
*/ |
|
1043 |
public boolean isSameTypes(List<Type> ts, List<Type> ss) { |
|
15705 | 1044 |
return isSameTypes(ts, ss, false); |
1045 |
} |
|
1046 |
public boolean isSameTypes(List<Type> ts, List<Type> ss, boolean strict) { |
|
10 | 1047 |
while (ts.tail != null && ss.tail != null |
1048 |
/*inlined: ts.nonEmpty() && ss.nonEmpty()*/ && |
|
15705 | 1049 |
isSameType(ts.head, ss.head, strict)) { |
10 | 1050 |
ts = ts.tail; |
1051 |
ss = ss.tail; |
|
1052 |
} |
|
1053 |
return ts.tail == null && ss.tail == null; |
|
1054 |
/*inlined: ts.isEmpty() && ss.isEmpty();*/ |
|
1055 |
} |
|
1056 |
||
1057 |
/** |
|
18389
a425d0819f36
8016569: javac, add new flag for polymorphic method signatures
vromero
parents:
17578
diff
changeset
|
1058 |
* A polymorphic signature method (JLS SE 7, 8.4.1) is a method that |
a425d0819f36
8016569: javac, add new flag for polymorphic method signatures
vromero
parents:
17578
diff
changeset
|
1059 |
* (i) is declared in the java.lang.invoke.MethodHandle class, (ii) takes |
a425d0819f36
8016569: javac, add new flag for polymorphic method signatures
vromero
parents:
17578
diff
changeset
|
1060 |
* a single variable arity parameter (iii) whose declared type is Object[], |
a425d0819f36
8016569: javac, add new flag for polymorphic method signatures
vromero
parents:
17578
diff
changeset
|
1061 |
* (iv) has a return type of Object and (v) is native. |
a425d0819f36
8016569: javac, add new flag for polymorphic method signatures
vromero
parents:
17578
diff
changeset
|
1062 |
*/ |
a425d0819f36
8016569: javac, add new flag for polymorphic method signatures
vromero
parents:
17578
diff
changeset
|
1063 |
public boolean isSignaturePolymorphic(MethodSymbol msym) { |
a425d0819f36
8016569: javac, add new flag for polymorphic method signatures
vromero
parents:
17578
diff
changeset
|
1064 |
List<Type> argtypes = msym.type.getParameterTypes(); |
a425d0819f36
8016569: javac, add new flag for polymorphic method signatures
vromero
parents:
17578
diff
changeset
|
1065 |
return (msym.flags_field & NATIVE) != 0 && |
a425d0819f36
8016569: javac, add new flag for polymorphic method signatures
vromero
parents:
17578
diff
changeset
|
1066 |
msym.owner == syms.methodHandleType.tsym && |
a425d0819f36
8016569: javac, add new flag for polymorphic method signatures
vromero
parents:
17578
diff
changeset
|
1067 |
argtypes.tail.tail == null && |
a425d0819f36
8016569: javac, add new flag for polymorphic method signatures
vromero
parents:
17578
diff
changeset
|
1068 |
argtypes.head.hasTag(TypeTag.ARRAY) && |
a425d0819f36
8016569: javac, add new flag for polymorphic method signatures
vromero
parents:
17578
diff
changeset
|
1069 |
msym.type.getReturnType().tsym == syms.objectType.tsym && |
a425d0819f36
8016569: javac, add new flag for polymorphic method signatures
vromero
parents:
17578
diff
changeset
|
1070 |
((ArrayType)argtypes.head).elemtype.tsym == syms.objectType.tsym; |
a425d0819f36
8016569: javac, add new flag for polymorphic method signatures
vromero
parents:
17578
diff
changeset
|
1071 |
} |
a425d0819f36
8016569: javac, add new flag for polymorphic method signatures
vromero
parents:
17578
diff
changeset
|
1072 |
|
a425d0819f36
8016569: javac, add new flag for polymorphic method signatures
vromero
parents:
17578
diff
changeset
|
1073 |
/** |
10 | 1074 |
* Is t the same type as s? |
1075 |
*/ |
|
1076 |
public boolean isSameType(Type t, Type s) { |
|
15705 | 1077 |
return isSameType(t, s, false); |
1078 |
} |
|
1079 |
public boolean isSameType(Type t, Type s, boolean strict) { |
|
1080 |
return strict ? |
|
1081 |
isSameTypeStrict.visit(t, s) : |
|
1082 |
isSameTypeLoose.visit(t, s); |
|
10 | 1083 |
} |
17578 | 1084 |
public boolean isSameAnnotatedType(Type t, Type s) { |
1085 |
return isSameAnnotatedType.visit(t, s); |
|
1086 |
} |
|
10 | 1087 |
// where |
15705 | 1088 |
abstract class SameTypeVisitor extends TypeRelation { |
10 | 1089 |
|
1090 |
public Boolean visitType(Type t, Type s) { |
|
1091 |
if (t == s) |
|
1092 |
return true; |
|
1093 |
||
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
1094 |
if (s.isPartial()) |
10 | 1095 |
return visit(s, t); |
1096 |
||
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1097 |
switch (t.getTag()) { |
10 | 1098 |
case BYTE: case CHAR: case SHORT: case INT: case LONG: case FLOAT: |
1099 |
case DOUBLE: case BOOLEAN: case VOID: case BOT: case NONE: |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1100 |
return t.hasTag(s.getTag()); |
5648
e8aa492874b5
6880344: Recursive type parameters do not compile
mcimadamore
parents:
5004
diff
changeset
|
1101 |
case TYPEVAR: { |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1102 |
if (s.hasTag(TYPEVAR)) { |
5648
e8aa492874b5
6880344: Recursive type parameters do not compile
mcimadamore
parents:
5004
diff
changeset
|
1103 |
//type-substitution does not preserve type-var types |
e8aa492874b5
6880344: Recursive type parameters do not compile
mcimadamore
parents:
5004
diff
changeset
|
1104 |
//check that type var symbols and bounds are indeed the same |
24396 | 1105 |
return sameTypeVars((TypeVar)t, (TypeVar)s); |
5648
e8aa492874b5
6880344: Recursive type parameters do not compile
mcimadamore
parents:
5004
diff
changeset
|
1106 |
} |
e8aa492874b5
6880344: Recursive type parameters do not compile
mcimadamore
parents:
5004
diff
changeset
|
1107 |
else { |
e8aa492874b5
6880344: Recursive type parameters do not compile
mcimadamore
parents:
5004
diff
changeset
|
1108 |
//special case for s == ? super X, where upper(s) = u |
e8aa492874b5
6880344: Recursive type parameters do not compile
mcimadamore
parents:
5004
diff
changeset
|
1109 |
//check that u == t, where u has been set by Type.withTypeVar |
e8aa492874b5
6880344: Recursive type parameters do not compile
mcimadamore
parents:
5004
diff
changeset
|
1110 |
return s.isSuperBound() && |
e8aa492874b5
6880344: Recursive type parameters do not compile
mcimadamore
parents:
5004
diff
changeset
|
1111 |
!s.isExtendsBound() && |
e8aa492874b5
6880344: Recursive type parameters do not compile
mcimadamore
parents:
5004
diff
changeset
|
1112 |
visit(t, upperBound(s)); |
e8aa492874b5
6880344: Recursive type parameters do not compile
mcimadamore
parents:
5004
diff
changeset
|
1113 |
} |
e8aa492874b5
6880344: Recursive type parameters do not compile
mcimadamore
parents:
5004
diff
changeset
|
1114 |
} |
10 | 1115 |
default: |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1116 |
throw new AssertionError("isSameType " + t.getTag()); |
10 | 1117 |
} |
1118 |
} |
|
1119 |
||
15705 | 1120 |
abstract boolean sameTypeVars(TypeVar tv1, TypeVar tv2); |
1121 |
||
10 | 1122 |
@Override |
1123 |
public Boolean visitWildcardType(WildcardType t, Type s) { |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
1124 |
if (s.isPartial()) |
10 | 1125 |
return visit(s, t); |
1126 |
else |
|
1127 |
return false; |
|
1128 |
} |
|
1129 |
||
1130 |
@Override |
|
1131 |
public Boolean visitClassType(ClassType t, Type s) { |
|
1132 |
if (t == s) |
|
1133 |
return true; |
|
1134 |
||
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
1135 |
if (s.isPartial()) |
10 | 1136 |
return visit(s, t); |
1137 |
||
1138 |
if (s.isSuperBound() && !s.isExtendsBound()) |
|
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
1139 |
return visit(t, upperBound(s)) && visit(t, wildLowerBound(s)); |
10 | 1140 |
|
1141 |
if (t.isCompound() && s.isCompound()) { |
|
1142 |
if (!visit(supertype(t), supertype(s))) |
|
1143 |
return false; |
|
1144 |
||
22163 | 1145 |
HashSet<UniqueType> set = new HashSet<>(); |
10 | 1146 |
for (Type x : interfaces(t)) |
24396 | 1147 |
set.add(new UniqueType(x, Types.this)); |
10 | 1148 |
for (Type x : interfaces(s)) { |
24396 | 1149 |
if (!set.remove(new UniqueType(x, Types.this))) |
10 | 1150 |
return false; |
1151 |
} |
|
7637 | 1152 |
return (set.isEmpty()); |
10 | 1153 |
} |
1154 |
return t.tsym == s.tsym |
|
1155 |
&& visit(t.getEnclosingType(), s.getEnclosingType()) |
|
15705 | 1156 |
&& containsTypes(t.getTypeArguments(), s.getTypeArguments()); |
10 | 1157 |
} |
1158 |
||
15705 | 1159 |
abstract protected boolean containsTypes(List<Type> ts1, List<Type> ts2); |
1160 |
||
10 | 1161 |
@Override |
1162 |
public Boolean visitArrayType(ArrayType t, Type s) { |
|
1163 |
if (t == s) |
|
1164 |
return true; |
|
1165 |
||
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
1166 |
if (s.isPartial()) |
10 | 1167 |
return visit(s, t); |
1168 |
||
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
1169 |
return s.hasTag(ARRAY) |
10 | 1170 |
&& containsTypeEquivalent(t.elemtype, elemtype(s)); |
1171 |
} |
|
1172 |
||
1173 |
@Override |
|
1174 |
public Boolean visitMethodType(MethodType t, Type s) { |
|
1175 |
// isSameType for methods does not take thrown |
|
1176 |
// exceptions into account! |
|
1177 |
return hasSameArgs(t, s) && visit(t.getReturnType(), s.getReturnType()); |
|
1178 |
} |
|
1179 |
||
1180 |
@Override |
|
1181 |
public Boolean visitPackageType(PackageType t, Type s) { |
|
1182 |
return t == s; |
|
1183 |
} |
|
1184 |
||
1185 |
@Override |
|
1186 |
public Boolean visitForAll(ForAll t, Type s) { |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1187 |
if (!s.hasTag(FORALL)) { |
10 | 1188 |
return false; |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1189 |
} |
10 | 1190 |
|
1191 |
ForAll forAll = (ForAll)s; |
|
1192 |
return hasSameBounds(t, forAll) |
|
1193 |
&& visit(t.qtype, subst(forAll.qtype, forAll.tvars, t.tvars)); |
|
1194 |
} |
|
1195 |
||
1196 |
@Override |
|
1197 |
public Boolean visitUndetVar(UndetVar t, Type s) { |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1198 |
if (s.hasTag(WILDCARD)) { |
10 | 1199 |
// FIXME, this might be leftovers from before capture conversion |
1200 |
return false; |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1201 |
} |
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1202 |
|
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1203 |
if (t == s || t.qtype == s || s.hasTag(ERROR) || s.hasTag(UNKNOWN)) { |
10 | 1204 |
return true; |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1205 |
} |
10 | 1206 |
|
14048
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13689
diff
changeset
|
1207 |
t.addBound(InferenceBound.EQ, s, Types.this); |
12468
1100643c0209
7154127: Inference cleanup: remove bound check analysis from visitors in Types.java
mcimadamore
parents:
11549
diff
changeset
|
1208 |
|
10 | 1209 |
return true; |
1210 |
} |
|
1211 |
||
1212 |
@Override |
|
1213 |
public Boolean visitErrorType(ErrorType t, Type s) { |
|
1214 |
return true; |
|
1215 |
} |
|
15705 | 1216 |
} |
1217 |
||
1218 |
/** |
|
1219 |
* Standard type-equality relation - type variables are considered |
|
1220 |
* equals if they share the same type symbol. |
|
1221 |
*/ |
|
17578 | 1222 |
TypeRelation isSameTypeLoose = new LooseSameTypeVisitor(); |
1223 |
||
1224 |
private class LooseSameTypeVisitor extends SameTypeVisitor { |
|
15705 | 1225 |
@Override |
1226 |
boolean sameTypeVars(TypeVar tv1, TypeVar tv2) { |
|
1227 |
return tv1.tsym == tv2.tsym && visit(tv1.getUpperBound(), tv2.getUpperBound()); |
|
1228 |
} |
|
1229 |
@Override |
|
1230 |
protected boolean containsTypes(List<Type> ts1, List<Type> ts2) { |
|
1231 |
return containsTypeEquivalent(ts1, ts2); |
|
1232 |
} |
|
22163 | 1233 |
} |
1234 |
||
1235 |
/** |
|
15705 | 1236 |
* Strict type-equality relation - type variables are considered |
1237 |
* equals if they share the same object identity. |
|
1238 |
*/ |
|
1239 |
TypeRelation isSameTypeStrict = new SameTypeVisitor() { |
|
1240 |
@Override |
|
1241 |
boolean sameTypeVars(TypeVar tv1, TypeVar tv2) { |
|
1242 |
return tv1 == tv2; |
|
1243 |
} |
|
1244 |
@Override |
|
1245 |
protected boolean containsTypes(List<Type> ts1, List<Type> ts2) { |
|
1246 |
return isSameTypes(ts1, ts2, true); |
|
1247 |
} |
|
16567
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16565
diff
changeset
|
1248 |
|
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16565
diff
changeset
|
1249 |
@Override |
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16565
diff
changeset
|
1250 |
public Boolean visitWildcardType(WildcardType t, Type s) { |
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16565
diff
changeset
|
1251 |
if (!s.hasTag(WILDCARD)) { |
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16565
diff
changeset
|
1252 |
return false; |
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16565
diff
changeset
|
1253 |
} else { |
24396 | 1254 |
WildcardType t2 = (WildcardType)s; |
16567
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16565
diff
changeset
|
1255 |
return t.kind == t2.kind && |
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16565
diff
changeset
|
1256 |
isSameType(t.type, t2.type, true); |
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16565
diff
changeset
|
1257 |
} |
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16565
diff
changeset
|
1258 |
} |
10 | 1259 |
}; |
17578 | 1260 |
|
24396 | 1261 |
// </editor-fold> |
1262 |
||
1263 |
TypeRelation isSameAnnotatedType = new LooseSameTypeVisitor() { |
|
1264 |
private Boolean compareAnnotations(Type t1, Type t2) { |
|
1265 |
List<Attribute.TypeCompound> annos1 = t1.getAnnotationMirrors(); |
|
1266 |
List<Attribute.TypeCompound> annos2 = t2.getAnnotationMirrors(); |
|
1267 |
return annos1.containsAll(annos2) && annos2.containsAll(annos1); |
|
1268 |
} |
|
1269 |
||
1270 |
@Override |
|
1271 |
public Boolean visitType(Type t, Type s) { |
|
1272 |
return compareAnnotations(t, s) && super.visitType(t, s); |
|
1273 |
} |
|
1274 |
||
17578 | 1275 |
@Override |
24396 | 1276 |
public Boolean visitWildcardType(WildcardType t, Type s) { |
1277 |
return compareAnnotations(t, s) && super.visitWildcardType(t, s); |
|
1278 |
} |
|
1279 |
||
1280 |
@Override |
|
1281 |
public Boolean visitClassType(ClassType t, Type s) { |
|
1282 |
return compareAnnotations(t, s) && super.visitClassType(t, s); |
|
1283 |
} |
|
1284 |
||
1285 |
@Override |
|
1286 |
public Boolean visitArrayType(ArrayType t, Type s) { |
|
1287 |
return compareAnnotations(t, s) && super.visitArrayType(t, s); |
|
1288 |
} |
|
1289 |
||
1290 |
@Override |
|
1291 |
public Boolean visitForAll(ForAll t, Type s) { |
|
1292 |
return compareAnnotations(t, s) && super.visitForAll(t, s); |
|
17578 | 1293 |
} |
1294 |
}; |
|
10 | 1295 |
|
1296 |
// <editor-fold defaultstate="collapsed" desc="Contains Type"> |
|
1297 |
public boolean containedBy(Type t, Type s) { |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1298 |
switch (t.getTag()) { |
10 | 1299 |
case UNDETVAR: |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1300 |
if (s.hasTag(WILDCARD)) { |
10 | 1301 |
UndetVar undetvar = (UndetVar)t; |
24396 | 1302 |
WildcardType wt = (WildcardType)s; |
1991
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1874
diff
changeset
|
1303 |
switch(wt.kind) { |
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1874
diff
changeset
|
1304 |
case UNBOUND: //similar to ? extends Object |
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1874
diff
changeset
|
1305 |
case EXTENDS: { |
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1874
diff
changeset
|
1306 |
Type bound = upperBound(s); |
14048
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13689
diff
changeset
|
1307 |
undetvar.addBound(InferenceBound.UPPER, bound, this); |
1991
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1874
diff
changeset
|
1308 |
break; |
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1874
diff
changeset
|
1309 |
} |
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1874
diff
changeset
|
1310 |
case SUPER: { |
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
1311 |
Type bound = wildLowerBound(s); |
14048
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
13689
diff
changeset
|
1312 |
undetvar.addBound(InferenceBound.LOWER, bound, this); |
1991
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1874
diff
changeset
|
1313 |
break; |
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1874
diff
changeset
|
1314 |
} |
1535
a64c289fe413
6762569: Javac crashes with AssertionError in Types.containedBy
mcimadamore
parents:
1530
diff
changeset
|
1315 |
} |
10 | 1316 |
return true; |
1317 |
} else { |
|
1318 |
return isSameType(t, s); |
|
1319 |
} |
|
1320 |
case ERROR: |
|
1321 |
return true; |
|
1322 |
default: |
|
1323 |
return containsType(s, t); |
|
1324 |
} |
|
1325 |
} |
|
1326 |
||
1327 |
boolean containsType(List<Type> ts, List<Type> ss) { |
|
1328 |
while (ts.nonEmpty() && ss.nonEmpty() |
|
1329 |
&& containsType(ts.head, ss.head)) { |
|
1330 |
ts = ts.tail; |
|
1331 |
ss = ss.tail; |
|
1332 |
} |
|
1333 |
return ts.isEmpty() && ss.isEmpty(); |
|
1334 |
} |
|
1335 |
||
1336 |
/** |
|
1337 |
* Check if t contains s. |
|
1338 |
* |
|
1339 |
* <p>T contains S if: |
|
1340 |
* |
|
1341 |
* <p>{@code L(T) <: L(S) && U(S) <: U(T)} |
|
1342 |
* |
|
1343 |
* <p>This relation is only used by ClassType.isSubtype(), that |
|
1344 |
* is, |
|
1345 |
* |
|
1346 |
* <p>{@code C<S> <: C<T> if T contains S.} |
|
1347 |
* |
|
1348 |
* <p>Because of F-bounds, this relation can lead to infinite |
|
1349 |
* recursion. Thus we must somehow break that recursion. Notice |
|
1350 |
* that containsType() is only called from ClassType.isSubtype(). |
|
1351 |
* Since the arguments have already been checked against their |
|
1352 |
* bounds, we know: |
|
1353 |
* |
|
1354 |
* <p>{@code U(S) <: U(T) if T is "super" bound (U(T) *is* the bound)} |
|
1355 |
* |
|
1356 |
* <p>{@code L(T) <: L(S) if T is "extends" bound (L(T) is bottom)} |
|
1357 |
* |
|
1358 |
* @param t a type |
|
1359 |
* @param s a type |
|
1360 |
*/ |
|
1361 |
public boolean containsType(Type t, Type s) { |
|
1362 |
return containsType.visit(t, s); |
|
1363 |
} |
|
1364 |
// where |
|
1365 |
private TypeRelation containsType = new TypeRelation() { |
|
1366 |
||
1367 |
private Type U(Type t) { |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1368 |
while (t.hasTag(WILDCARD)) { |
24396 | 1369 |
WildcardType w = (WildcardType)t; |
10 | 1370 |
if (w.isSuperBound()) |
1371 |
return w.bound == null ? syms.objectType : w.bound.bound; |
|
1372 |
else |
|
1373 |
t = w.type; |
|
1374 |
} |
|
1375 |
return t; |
|
1376 |
} |
|
1377 |
||
1378 |
private Type L(Type t) { |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1379 |
while (t.hasTag(WILDCARD)) { |
24396 | 1380 |
WildcardType w = (WildcardType)t; |
10 | 1381 |
if (w.isExtendsBound()) |
1382 |
return syms.botType; |
|
1383 |
else |
|
1384 |
t = w.type; |
|
1385 |
} |
|
1386 |
return t; |
|
1387 |
} |
|
1388 |
||
1389 |
public Boolean visitType(Type t, Type s) { |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
1390 |
if (s.isPartial()) |
10 | 1391 |
return containedBy(s, t); |
1392 |
else |
|
1393 |
return isSameType(t, s); |
|
1394 |
} |
|
1395 |
||
7637 | 1396 |
// void debugContainsType(WildcardType t, Type s) { |
1397 |
// System.err.println(); |
|
1398 |
// System.err.format(" does %s contain %s?%n", t, s); |
|
1399 |
// System.err.format(" %s U(%s) <: U(%s) %s = %s%n", |
|
1400 |
// upperBound(s), s, t, U(t), |
|
1401 |
// t.isSuperBound() |
|
1402 |
// || isSubtypeNoCapture(upperBound(s), U(t))); |
|
1403 |
// System.err.format(" %s L(%s) <: L(%s) %s = %s%n", |
|
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
1404 |
// L(t), t, s, wildLowerBound(s), |
7637 | 1405 |
// t.isExtendsBound() |
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
1406 |
// || isSubtypeNoCapture(L(t), wildLowerBound(s))); |
7637 | 1407 |
// System.err.println(); |
1408 |
// } |
|
10 | 1409 |
|
1410 |
@Override |
|
1411 |
public Boolean visitWildcardType(WildcardType t, Type s) { |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
1412 |
if (s.isPartial()) |
10 | 1413 |
return containedBy(s, t); |
1414 |
else { |
|
7637 | 1415 |
// debugContainsType(t, s); |
10 | 1416 |
return isSameWildcard(t, s) |
1417 |
|| isCaptureOf(s, t) |
|
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
1418 |
|| ((t.isExtendsBound() || isSubtypeNoCapture(L(t), wildLowerBound(s))) && |
10 | 1419 |
(t.isSuperBound() || isSubtypeNoCapture(upperBound(s), U(t)))); |
1420 |
} |
|
1421 |
} |
|
1422 |
||
1423 |
@Override |
|
1424 |
public Boolean visitUndetVar(UndetVar t, Type s) { |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1425 |
if (!s.hasTag(WILDCARD)) { |
10 | 1426 |
return isSameType(t, s); |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1427 |
} else { |
10 | 1428 |
return false; |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1429 |
} |
10 | 1430 |
} |
1431 |
||
1432 |
@Override |
|
1433 |
public Boolean visitErrorType(ErrorType t, Type s) { |
|
1434 |
return true; |
|
1435 |
} |
|
1436 |
}; |
|
1437 |
||
1438 |
public boolean isCaptureOf(Type s, WildcardType t) { |
|
24396 | 1439 |
if (!s.hasTag(TYPEVAR) || !((TypeVar)s).isCaptured()) |
10 | 1440 |
return false; |
24396 | 1441 |
return isSameWildcard(t, ((CapturedType)s).wildcard); |
10 | 1442 |
} |
1443 |
||
1444 |
public boolean isSameWildcard(WildcardType t, Type s) { |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1445 |
if (!s.hasTag(WILDCARD)) |
10 | 1446 |
return false; |
24396 | 1447 |
WildcardType w = (WildcardType)s; |
10 | 1448 |
return w.kind == t.kind && w.type == t.type; |
1449 |
} |
|
1450 |
||
1451 |
public boolean containsTypeEquivalent(List<Type> ts, List<Type> ss) { |
|
1452 |
while (ts.nonEmpty() && ss.nonEmpty() |
|
1453 |
&& containsTypeEquivalent(ts.head, ss.head)) { |
|
1454 |
ts = ts.tail; |
|
1455 |
ss = ss.tail; |
|
1456 |
} |
|
1457 |
return ts.isEmpty() && ss.isEmpty(); |
|
1458 |
} |
|
1459 |
// </editor-fold> |
|
1460 |
||
18662
1cac45e71eb9
8013357: javac accepts erroneous binary comparison operations
emc
parents:
18646
diff
changeset
|
1461 |
/** |
1cac45e71eb9
8013357: javac accepts erroneous binary comparison operations
emc
parents:
18646
diff
changeset
|
1462 |
* Can t and s be compared for equality? Any primitive == |
1cac45e71eb9
8013357: javac accepts erroneous binary comparison operations
emc
parents:
18646
diff
changeset
|
1463 |
* primitive or primitive == object comparisons here are an error. |
1cac45e71eb9
8013357: javac accepts erroneous binary comparison operations
emc
parents:
18646
diff
changeset
|
1464 |
* Unboxing and correct primitive == primitive comparisons are |
1cac45e71eb9
8013357: javac accepts erroneous binary comparison operations
emc
parents:
18646
diff
changeset
|
1465 |
* already dealt with in Attr.visitBinary. |
1cac45e71eb9
8013357: javac accepts erroneous binary comparison operations
emc
parents:
18646
diff
changeset
|
1466 |
* |
1cac45e71eb9
8013357: javac accepts erroneous binary comparison operations
emc
parents:
18646
diff
changeset
|
1467 |
*/ |
1cac45e71eb9
8013357: javac accepts erroneous binary comparison operations
emc
parents:
18646
diff
changeset
|
1468 |
public boolean isEqualityComparable(Type s, Type t, Warner warn) { |
1cac45e71eb9
8013357: javac accepts erroneous binary comparison operations
emc
parents:
18646
diff
changeset
|
1469 |
if (t.isNumeric() && s.isNumeric()) |
1cac45e71eb9
8013357: javac accepts erroneous binary comparison operations
emc
parents:
18646
diff
changeset
|
1470 |
return true; |
1cac45e71eb9
8013357: javac accepts erroneous binary comparison operations
emc
parents:
18646
diff
changeset
|
1471 |
|
1cac45e71eb9
8013357: javac accepts erroneous binary comparison operations
emc
parents:
18646
diff
changeset
|
1472 |
boolean tPrimitive = t.isPrimitive(); |
1cac45e71eb9
8013357: javac accepts erroneous binary comparison operations
emc
parents:
18646
diff
changeset
|
1473 |
boolean sPrimitive = s.isPrimitive(); |
1cac45e71eb9
8013357: javac accepts erroneous binary comparison operations
emc
parents:
18646
diff
changeset
|
1474 |
if (!tPrimitive && !sPrimitive) { |
1cac45e71eb9
8013357: javac accepts erroneous binary comparison operations
emc
parents:
18646
diff
changeset
|
1475 |
return isCastable(s, t, warn) || isCastable(t, s, warn); |
1cac45e71eb9
8013357: javac accepts erroneous binary comparison operations
emc
parents:
18646
diff
changeset
|
1476 |
} else { |
1cac45e71eb9
8013357: javac accepts erroneous binary comparison operations
emc
parents:
18646
diff
changeset
|
1477 |
return false; |
1cac45e71eb9
8013357: javac accepts erroneous binary comparison operations
emc
parents:
18646
diff
changeset
|
1478 |
} |
1cac45e71eb9
8013357: javac accepts erroneous binary comparison operations
emc
parents:
18646
diff
changeset
|
1479 |
} |
1cac45e71eb9
8013357: javac accepts erroneous binary comparison operations
emc
parents:
18646
diff
changeset
|
1480 |
|
10 | 1481 |
// <editor-fold defaultstate="collapsed" desc="isCastable"> |
1482 |
public boolean isCastable(Type t, Type s) { |
|
14547 | 1483 |
return isCastable(t, s, noWarnings); |
10 | 1484 |
} |
1485 |
||
1486 |
/** |
|
1487 |
* Is t is castable to s?<br> |
|
1488 |
* s is assumed to be an erased type.<br> |
|
1489 |
* (not defined for Method and ForAll types). |
|
1490 |
*/ |
|
1491 |
public boolean isCastable(Type t, Type s, Warner warn) { |
|
1492 |
if (t == s) |
|
1493 |
return true; |
|
1494 |
||
1495 |
if (t.isPrimitive() != s.isPrimitive()) |
|
9595
a7dea6cbd5f7
7038363: cast from object to primitive should be for source >= 1.7
jjg
parents:
9303
diff
changeset
|
1496 |
return allowBoxing && ( |
a7dea6cbd5f7
7038363: cast from object to primitive should be for source >= 1.7
jjg
parents:
9303
diff
changeset
|
1497 |
isConvertible(t, s, warn) |
9721
da048be191c2
7041730: Regression: compiler accepts invalid cast from int to Byte
mcimadamore
parents:
9607
diff
changeset
|
1498 |
|| (allowObjectToPrimitiveCast && |
da048be191c2
7041730: Regression: compiler accepts invalid cast from int to Byte
mcimadamore
parents:
9607
diff
changeset
|
1499 |
s.isPrimitive() && |
da048be191c2
7041730: Regression: compiler accepts invalid cast from int to Byte
mcimadamore
parents:
9607
diff
changeset
|
1500 |
isSubtype(boxedClass(s).type, t))); |
10 | 1501 |
if (warn != warnStack.head) { |
1502 |
try { |
|
1503 |
warnStack = warnStack.prepend(warn); |
|
7643
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7637
diff
changeset
|
1504 |
checkUnsafeVarargsConversion(t, s, warn); |
1790
7182011ee8a6
6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents:
1535
diff
changeset
|
1505 |
return isCastable.visit(t,s); |
10 | 1506 |
} finally { |
1507 |
warnStack = warnStack.tail; |
|
1508 |
} |
|
1509 |
} else { |
|
1790
7182011ee8a6
6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents:
1535
diff
changeset
|
1510 |
return isCastable.visit(t,s); |
10 | 1511 |
} |
1512 |
} |
|
1513 |
// where |
|
1514 |
private TypeRelation isCastable = new TypeRelation() { |
|
1515 |
||
1516 |
public Boolean visitType(Type t, Type s) { |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1517 |
if (s.hasTag(ERROR)) |
10 | 1518 |
return true; |
1519 |
||
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1520 |
switch (t.getTag()) { |
10 | 1521 |
case BYTE: case CHAR: case SHORT: case INT: case LONG: case FLOAT: |
1522 |
case DOUBLE: |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
1523 |
return s.isNumeric(); |
10 | 1524 |
case BOOLEAN: |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1525 |
return s.hasTag(BOOLEAN); |
10 | 1526 |
case VOID: |
1527 |
return false; |
|
1528 |
case BOT: |
|
1529 |
return isSubtype(t, s); |
|
1530 |
default: |
|
1531 |
throw new AssertionError(); |
|
1532 |
} |
|
1533 |
} |
|
1534 |
||
1535 |
@Override |
|
1536 |
public Boolean visitWildcardType(WildcardType t, Type s) { |
|
1537 |
return isCastable(upperBound(t), s, warnStack.head); |
|
1538 |
} |
|
1539 |
||
1540 |
@Override |
|
1541 |
public Boolean visitClassType(ClassType t, Type s) { |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1542 |
if (s.hasTag(ERROR) || s.hasTag(BOT)) |
10 | 1543 |
return true; |
1544 |
||
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1545 |
if (s.hasTag(TYPEVAR)) { |
14547 | 1546 |
if (isCastable(t, s.getUpperBound(), noWarnings)) { |
7643
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7637
diff
changeset
|
1547 |
warnStack.head.warn(LintCategory.UNCHECKED); |
10 | 1548 |
return true; |
1549 |
} else { |
|
1550 |
return false; |
|
1551 |
} |
|
1552 |
} |
|
1553 |
||
16565
aee0d0aaec44
8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents:
16556
diff
changeset
|
1554 |
if (t.isCompound() || s.isCompound()) { |
aee0d0aaec44
8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents:
16556
diff
changeset
|
1555 |
return !t.isCompound() ? |
24396 | 1556 |
visitIntersectionType((IntersectionClassType)s, t, true) : |
1557 |
visitIntersectionType((IntersectionClassType)t, s, false); |
|
10 | 1558 |
} |
1559 |
||
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1560 |
if (s.hasTag(CLASS) || s.hasTag(ARRAY)) { |
10 | 1561 |
boolean upcast; |
1562 |
if ((upcast = isSubtype(erasure(t), erasure(s))) |
|
1563 |
|| isSubtype(erasure(s), erasure(t))) { |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1564 |
if (!upcast && s.hasTag(ARRAY)) { |
10 | 1565 |
if (!isReifiable(s)) |
7643
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7637
diff
changeset
|
1566 |
warnStack.head.warn(LintCategory.UNCHECKED); |
10 | 1567 |
return true; |
1568 |
} else if (s.isRaw()) { |
|
1569 |
return true; |
|
1570 |
} else if (t.isRaw()) { |
|
1571 |
if (!isUnbounded(s)) |
|
7643
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7637
diff
changeset
|
1572 |
warnStack.head.warn(LintCategory.UNCHECKED); |
10 | 1573 |
return true; |
1574 |
} |
|
1575 |
// Assume |a| <: |b| |
|
1576 |
final Type a = upcast ? t : s; |
|
1577 |
final Type b = upcast ? s : t; |
|
1578 |
final boolean HIGH = true; |
|
1579 |
final boolean LOW = false; |
|
1580 |
final boolean DONT_REWRITE_TYPEVARS = false; |
|
1581 |
Type aHigh = rewriteQuantifiers(a, HIGH, DONT_REWRITE_TYPEVARS); |
|
1582 |
Type aLow = rewriteQuantifiers(a, LOW, DONT_REWRITE_TYPEVARS); |
|
1583 |
Type bHigh = rewriteQuantifiers(b, HIGH, DONT_REWRITE_TYPEVARS); |
|
1584 |
Type bLow = rewriteQuantifiers(b, LOW, DONT_REWRITE_TYPEVARS); |
|
1585 |
Type lowSub = asSub(bLow, aLow.tsym); |
|
1586 |
Type highSub = (lowSub == null) ? null : asSub(bHigh, aHigh.tsym); |
|
1587 |
if (highSub == null) { |
|
1588 |
final boolean REWRITE_TYPEVARS = true; |
|
1589 |
aHigh = rewriteQuantifiers(a, HIGH, REWRITE_TYPEVARS); |
|
1590 |
aLow = rewriteQuantifiers(a, LOW, REWRITE_TYPEVARS); |
|
1591 |
bHigh = rewriteQuantifiers(b, HIGH, REWRITE_TYPEVARS); |
|
1592 |
bLow = rewriteQuantifiers(b, LOW, REWRITE_TYPEVARS); |
|
1593 |
lowSub = asSub(bLow, aLow.tsym); |
|
1594 |
highSub = (lowSub == null) ? null : asSub(bHigh, aHigh.tsym); |
|
1595 |
} |
|
1596 |
if (highSub != null) { |
|
8032 | 1597 |
if (!(a.tsym == highSub.tsym && a.tsym == lowSub.tsym)) { |
1598 |
Assert.error(a.tsym + " != " + highSub.tsym + " != " + lowSub.tsym); |
|
1599 |
} |
|
1790
7182011ee8a6
6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents:
1535
diff
changeset
|
1600 |
if (!disjointTypes(aHigh.allparams(), highSub.allparams()) |
7182011ee8a6
6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents:
1535
diff
changeset
|
1601 |
&& !disjointTypes(aHigh.allparams(), lowSub.allparams()) |
7182011ee8a6
6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents:
1535
diff
changeset
|
1602 |
&& !disjointTypes(aLow.allparams(), highSub.allparams()) |
7182011ee8a6
6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents:
1535
diff
changeset
|
1603 |
&& !disjointTypes(aLow.allparams(), lowSub.allparams())) { |
7627
de2a86da165e
7005095: Cast: compile reject sensible cast from final class to interface
mcimadamore
parents:
7331
diff
changeset
|
1604 |
if (upcast ? giveWarning(a, b) : |
2218
a8ec0baae870
6467183: javac fails to raise unchecked warning on cast of parameterized generic subclass
mcimadamore
parents:
1993
diff
changeset
|
1605 |
giveWarning(b, a)) |
7643
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7637
diff
changeset
|
1606 |
warnStack.head.warn(LintCategory.UNCHECKED); |
10 | 1607 |
return true; |
1608 |
} |
|
1609 |
} |
|
1610 |
if (isReifiable(s)) |
|
1611 |
return isSubtypeUnchecked(a, b); |
|
1612 |
else |
|
1613 |
return isSubtypeUnchecked(a, b, warnStack.head); |
|
1614 |
} |
|
1615 |
||
1616 |
// Sidecast |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1617 |
if (s.hasTag(CLASS)) { |
10 | 1618 |
if ((s.tsym.flags() & INTERFACE) != 0) { |
1619 |
return ((t.tsym.flags() & FINAL) == 0) |
|
1620 |
? sideCast(t, s, warnStack.head) |
|
1621 |
: sideCastFinal(t, s, warnStack.head); |
|
1622 |
} else if ((t.tsym.flags() & INTERFACE) != 0) { |
|
1623 |
return ((s.tsym.flags() & FINAL) == 0) |
|
1624 |
? sideCast(t, s, warnStack.head) |
|
1625 |
: sideCastFinal(t, s, warnStack.head); |
|
1626 |
} else { |
|
1627 |
// unrelated class types |
|
1628 |
return false; |
|
1629 |
} |
|
1630 |
} |
|
1631 |
} |
|
1632 |
return false; |
|
1633 |
} |
|
1634 |
||
16565
aee0d0aaec44
8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents:
16556
diff
changeset
|
1635 |
boolean visitIntersectionType(IntersectionClassType ict, Type s, boolean reverse) { |
aee0d0aaec44
8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents:
16556
diff
changeset
|
1636 |
Warner warn = noWarnings; |
aee0d0aaec44
8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents:
16556
diff
changeset
|
1637 |
for (Type c : ict.getComponents()) { |
aee0d0aaec44
8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents:
16556
diff
changeset
|
1638 |
warn.clear(); |
aee0d0aaec44
8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents:
16556
diff
changeset
|
1639 |
if (reverse ? !isCastable(s, c, warn) : !isCastable(c, s, warn)) |
aee0d0aaec44
8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents:
16556
diff
changeset
|
1640 |
return false; |
aee0d0aaec44
8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents:
16556
diff
changeset
|
1641 |
} |
aee0d0aaec44
8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents:
16556
diff
changeset
|
1642 |
if (warn.hasLint(LintCategory.UNCHECKED)) |
aee0d0aaec44
8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents:
16556
diff
changeset
|
1643 |
warnStack.head.warn(LintCategory.UNCHECKED); |
aee0d0aaec44
8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents:
16556
diff
changeset
|
1644 |
return true; |
aee0d0aaec44
8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents:
16556
diff
changeset
|
1645 |
} |
aee0d0aaec44
8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents:
16556
diff
changeset
|
1646 |
|
10 | 1647 |
@Override |
1648 |
public Boolean visitArrayType(ArrayType t, Type s) { |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1649 |
switch (s.getTag()) { |
10 | 1650 |
case ERROR: |
1651 |
case BOT: |
|
1652 |
return true; |
|
1653 |
case TYPEVAR: |
|
14547 | 1654 |
if (isCastable(s, t, noWarnings)) { |
7643
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7637
diff
changeset
|
1655 |
warnStack.head.warn(LintCategory.UNCHECKED); |
10 | 1656 |
return true; |
1657 |
} else { |
|
1658 |
return false; |
|
1659 |
} |
|
1660 |
case CLASS: |
|
1661 |
return isSubtype(t, s); |
|
1662 |
case ARRAY: |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
1663 |
if (elemtype(t).isPrimitive() || elemtype(s).isPrimitive()) { |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1664 |
return elemtype(t).hasTag(elemtype(s).getTag()); |
10 | 1665 |
} else { |
1666 |
return visit(elemtype(t), elemtype(s)); |
|
1667 |
} |
|
1668 |
default: |
|
1669 |
return false; |
|
1670 |
} |
|
1671 |
} |
|
1672 |
||
1673 |
@Override |
|
1674 |
public Boolean visitTypeVar(TypeVar t, Type s) { |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1675 |
switch (s.getTag()) { |
10 | 1676 |
case ERROR: |
1677 |
case BOT: |
|
1678 |
return true; |
|
1679 |
case TYPEVAR: |
|
1680 |
if (isSubtype(t, s)) { |
|
1681 |
return true; |
|
14547 | 1682 |
} else if (isCastable(t.bound, s, noWarnings)) { |
7643
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7637
diff
changeset
|
1683 |
warnStack.head.warn(LintCategory.UNCHECKED); |
10 | 1684 |
return true; |
1685 |
} else { |
|
1686 |
return false; |
|
1687 |
} |
|
1688 |
default: |
|
1689 |
return isCastable(t.bound, s, warnStack.head); |
|
1690 |
} |
|
1691 |
} |
|
1692 |
||
1693 |
@Override |
|
1694 |
public Boolean visitErrorType(ErrorType t, Type s) { |
|
1695 |
return true; |
|
1696 |
} |
|
1697 |
}; |
|
1698 |
// </editor-fold> |
|
1699 |
||
1700 |
// <editor-fold defaultstate="collapsed" desc="disjointTypes"> |
|
1701 |
public boolean disjointTypes(List<Type> ts, List<Type> ss) { |
|
1702 |
while (ts.tail != null && ss.tail != null) { |
|
1703 |
if (disjointType(ts.head, ss.head)) return true; |
|
1704 |
ts = ts.tail; |
|
1705 |
ss = ss.tail; |
|
1706 |
} |
|
1707 |
return false; |
|
1708 |
} |
|
1709 |
||
1710 |
/** |
|
1711 |
* Two types or wildcards are considered disjoint if it can be |
|
1712 |
* proven that no type can be contained in both. It is |
|
1713 |
* conservative in that it is allowed to say that two types are |
|
1714 |
* not disjoint, even though they actually are. |
|
1715 |
* |
|
14259 | 1716 |
* The type {@code C<X>} is castable to {@code C<Y>} exactly if |
1717 |
* {@code X} and {@code Y} are not disjoint. |
|
10 | 1718 |
*/ |
1719 |
public boolean disjointType(Type t, Type s) { |
|
1720 |
return disjointType.visit(t, s); |
|
1721 |
} |
|
1722 |
// where |
|
1723 |
private TypeRelation disjointType = new TypeRelation() { |
|
1724 |
||
22163 | 1725 |
private Set<TypePair> cache = new HashSet<>(); |
10 | 1726 |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1727 |
@Override |
10 | 1728 |
public Boolean visitType(Type t, Type s) { |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1729 |
if (s.hasTag(WILDCARD)) |
10 | 1730 |
return visit(s, t); |
1731 |
else |
|
1732 |
return notSoftSubtypeRecursive(t, s) || notSoftSubtypeRecursive(s, t); |
|
1733 |
} |
|
1734 |
||
1735 |
private boolean isCastableRecursive(Type t, Type s) { |
|
1736 |
TypePair pair = new TypePair(t, s); |
|
1737 |
if (cache.add(pair)) { |
|
1738 |
try { |
|
1739 |
return Types.this.isCastable(t, s); |
|
1740 |
} finally { |
|
1741 |
cache.remove(pair); |
|
1742 |
} |
|
1743 |
} else { |
|
1744 |
return true; |
|
1745 |
} |
|
1746 |
} |
|
1747 |
||
1748 |
private boolean notSoftSubtypeRecursive(Type t, Type s) { |
|
1749 |
TypePair pair = new TypePair(t, s); |
|
1750 |
if (cache.add(pair)) { |
|
1751 |
try { |
|
1752 |
return Types.this.notSoftSubtype(t, s); |
|
1753 |
} finally { |
|
1754 |
cache.remove(pair); |
|
1755 |
} |
|
1756 |
} else { |
|
1757 |
return false; |
|
1758 |
} |
|
1759 |
} |
|
1760 |
||
1761 |
@Override |
|
1762 |
public Boolean visitWildcardType(WildcardType t, Type s) { |
|
1763 |
if (t.isUnbound()) |
|
1764 |
return false; |
|
1765 |
||
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1766 |
if (!s.hasTag(WILDCARD)) { |
10 | 1767 |
if (t.isExtendsBound()) |
1768 |
return notSoftSubtypeRecursive(s, t.type); |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1769 |
else |
10 | 1770 |
return notSoftSubtypeRecursive(t.type, s); |
1771 |
} |
|
1772 |
||
1773 |
if (s.isUnbound()) |
|
1774 |
return false; |
|
1775 |
||
1776 |
if (t.isExtendsBound()) { |
|
1777 |
if (s.isExtendsBound()) |
|
1778 |
return !isCastableRecursive(t.type, upperBound(s)); |
|
1779 |
else if (s.isSuperBound()) |
|
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
1780 |
return notSoftSubtypeRecursive(wildLowerBound(s), t.type); |
10 | 1781 |
} else if (t.isSuperBound()) { |
1782 |
if (s.isExtendsBound()) |
|
1783 |
return notSoftSubtypeRecursive(t.type, upperBound(s)); |
|
1784 |
} |
|
1785 |
return false; |
|
1786 |
} |
|
1787 |
}; |
|
1788 |
// </editor-fold> |
|
1789 |
||
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
1790 |
// <editor-fold defaultstate="collapsed" desc="cvarLowerBounds"> |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
1791 |
public List<Type> cvarLowerBounds(List<Type> ts) { |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
1792 |
return map(ts, cvarLowerBoundMapping); |
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
1793 |
} |
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
1794 |
private final Mapping cvarLowerBoundMapping = new Mapping("cvarLowerBound") { |
10 | 1795 |
public Type apply(Type t) { |
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
1796 |
return cvarLowerBound(t); |
10 | 1797 |
} |
1798 |
}; |
|
1799 |
// </editor-fold> |
|
1800 |
||
1801 |
// <editor-fold defaultstate="collapsed" desc="notSoftSubtype"> |
|
1802 |
/** |
|
1803 |
* This relation answers the question: is impossible that |
|
1804 |
* something of type `t' can be a subtype of `s'? This is |
|
1805 |
* different from the question "is `t' not a subtype of `s'?" |
|
1806 |
* when type variables are involved: Integer is not a subtype of T |
|
14259 | 1807 |
* where {@code <T extends Number>} but it is not true that Integer cannot |
10 | 1808 |
* possibly be a subtype of T. |
1809 |
*/ |
|
1810 |
public boolean notSoftSubtype(Type t, Type s) { |
|
1811 |
if (t == s) return false; |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1812 |
if (t.hasTag(TYPEVAR)) { |
10 | 1813 |
TypeVar tv = (TypeVar) t; |
1814 |
return !isCastable(tv.bound, |
|
6353
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
1815 |
relaxBound(s), |
14547 | 1816 |
noWarnings); |
10 | 1817 |
} |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1818 |
if (!s.hasTag(WILDCARD)) |
10 | 1819 |
s = upperBound(s); |
6353
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
1820 |
|
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
1821 |
return !isSubtype(t, relaxBound(s)); |
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
1822 |
} |
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
1823 |
|
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
1824 |
private Type relaxBound(Type t) { |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1825 |
if (t.hasTag(TYPEVAR)) { |
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1826 |
while (t.hasTag(TYPEVAR)) |
6353
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
1827 |
t = t.getUpperBound(); |
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
1828 |
t = rewriteQuantifiers(t, true, true); |
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
1829 |
} |
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
1830 |
return t; |
10 | 1831 |
} |
1832 |
// </editor-fold> |
|
1833 |
||
1834 |
// <editor-fold defaultstate="collapsed" desc="isReifiable"> |
|
1835 |
public boolean isReifiable(Type t) { |
|
1836 |
return isReifiable.visit(t); |
|
1837 |
} |
|
1838 |
// where |
|
1839 |
private UnaryVisitor<Boolean> isReifiable = new UnaryVisitor<Boolean>() { |
|
1840 |
||
1841 |
public Boolean visitType(Type t, Void ignored) { |
|
1842 |
return true; |
|
1843 |
} |
|
1844 |
||
1845 |
@Override |
|
1846 |
public Boolean visitClassType(ClassType t, Void ignored) { |
|
3554
a0a7f7f2a6e5
6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents:
3539
diff
changeset
|
1847 |
if (t.isCompound()) |
a0a7f7f2a6e5
6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents:
3539
diff
changeset
|
1848 |
return false; |
a0a7f7f2a6e5
6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents:
3539
diff
changeset
|
1849 |
else { |
a0a7f7f2a6e5
6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents:
3539
diff
changeset
|
1850 |
if (!t.isParameterized()) |
a0a7f7f2a6e5
6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents:
3539
diff
changeset
|
1851 |
return true; |
a0a7f7f2a6e5
6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents:
3539
diff
changeset
|
1852 |
|
a0a7f7f2a6e5
6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents:
3539
diff
changeset
|
1853 |
for (Type param : t.allparams()) { |
a0a7f7f2a6e5
6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents:
3539
diff
changeset
|
1854 |
if (!param.isUnbound()) |
a0a7f7f2a6e5
6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents:
3539
diff
changeset
|
1855 |
return false; |
a0a7f7f2a6e5
6806876: ClassCastException occurs in assignment expressions without any heap pollutions
mcimadamore
parents:
3539
diff
changeset
|
1856 |
} |
10 | 1857 |
return true; |
1858 |
} |
|
1859 |
} |
|
1860 |
||
1861 |
@Override |
|
1862 |
public Boolean visitArrayType(ArrayType t, Void ignored) { |
|
1863 |
return visit(t.elemtype); |
|
1864 |
} |
|
1865 |
||
1866 |
@Override |
|
1867 |
public Boolean visitTypeVar(TypeVar t, Void ignored) { |
|
1868 |
return false; |
|
1869 |
} |
|
1870 |
}; |
|
1871 |
// </editor-fold> |
|
1872 |
||
1873 |
// <editor-fold defaultstate="collapsed" desc="Array Utils"> |
|
1874 |
public boolean isArray(Type t) { |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1875 |
while (t.hasTag(WILDCARD)) |
10 | 1876 |
t = upperBound(t); |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1877 |
return t.hasTag(ARRAY); |
10 | 1878 |
} |
1879 |
||
1880 |
/** |
|
1881 |
* The element type of an array. |
|
1882 |
*/ |
|
1883 |
public Type elemtype(Type t) { |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1884 |
switch (t.getTag()) { |
10 | 1885 |
case WILDCARD: |
1886 |
return elemtype(upperBound(t)); |
|
1887 |
case ARRAY: |
|
1888 |
return ((ArrayType)t).elemtype; |
|
1889 |
case FORALL: |
|
1890 |
return elemtype(((ForAll)t).qtype); |
|
1891 |
case ERROR: |
|
1892 |
return t; |
|
1893 |
default: |
|
1894 |
return null; |
|
1895 |
} |
|
1896 |
} |
|
1897 |
||
7635
e0a3aeefe4e7
6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents:
7634
diff
changeset
|
1898 |
public Type elemtypeOrType(Type t) { |
e0a3aeefe4e7
6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents:
7634
diff
changeset
|
1899 |
Type elemtype = elemtype(t); |
e0a3aeefe4e7
6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents:
7634
diff
changeset
|
1900 |
return elemtype != null ? |
e0a3aeefe4e7
6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents:
7634
diff
changeset
|
1901 |
elemtype : |
e0a3aeefe4e7
6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents:
7634
diff
changeset
|
1902 |
t; |
e0a3aeefe4e7
6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents:
7634
diff
changeset
|
1903 |
} |
e0a3aeefe4e7
6199075: Unambiguous varargs method calls flagged as ambiguous
mcimadamore
parents:
7634
diff
changeset
|
1904 |
|
10 | 1905 |
/** |
1906 |
* Mapping to take element type of an arraytype |
|
1907 |
*/ |
|
1908 |
private Mapping elemTypeFun = new Mapping ("elemTypeFun") { |
|
1909 |
public Type apply(Type t) { return elemtype(t); } |
|
1910 |
}; |
|
1911 |
||
1912 |
/** |
|
1913 |
* The number of dimensions of an array type. |
|
1914 |
*/ |
|
1915 |
public int dimensions(Type t) { |
|
1916 |
int result = 0; |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1917 |
while (t.hasTag(ARRAY)) { |
10 | 1918 |
result++; |
1919 |
t = elemtype(t); |
|
1920 |
} |
|
1921 |
return result; |
|
1922 |
} |
|
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
13633
diff
changeset
|
1923 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
13633
diff
changeset
|
1924 |
/** |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
13633
diff
changeset
|
1925 |
* Returns an ArrayType with the component type t |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
13633
diff
changeset
|
1926 |
* |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
13633
diff
changeset
|
1927 |
* @param t The component type of the ArrayType |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
13633
diff
changeset
|
1928 |
* @return the ArrayType for the given component |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
13633
diff
changeset
|
1929 |
*/ |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
13633
diff
changeset
|
1930 |
public ArrayType makeArrayType(Type t) { |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
1931 |
if (t.hasTag(VOID) || t.hasTag(PACKAGE)) { |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
1932 |
Assert.error("Type t must not be a VOID or PACKAGE type, " + t.toString()); |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
13633
diff
changeset
|
1933 |
} |
24396 | 1934 |
return new ArrayType(t, syms.arrayClass, Type.noAnnotations); |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
13633
diff
changeset
|
1935 |
} |
10 | 1936 |
// </editor-fold> |
1937 |
||
1938 |
// <editor-fold defaultstate="collapsed" desc="asSuper"> |
|
1939 |
/** |
|
1940 |
* Return the (most specific) base type of t that starts with the |
|
1941 |
* given symbol. If none exists, return null. |
|
1942 |
* |
|
1943 |
* @param t a type |
|
1944 |
* @param sym a symbol |
|
1945 |
*/ |
|
21712
d363f367d4c0
8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents:
21041
diff
changeset
|
1946 |
public Type asSuper(Type t, Symbol sym) { |
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
23810
diff
changeset
|
1947 |
/* Some examples: |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
23810
diff
changeset
|
1948 |
* |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
23810
diff
changeset
|
1949 |
* (Enum<E>, Comparable) => Comparable<E> |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
23810
diff
changeset
|
1950 |
* (c.s.s.d.AttributeTree.ValueKind, Enum) => Enum<c.s.s.d.AttributeTree.ValueKind> |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
23810
diff
changeset
|
1951 |
* (c.s.s.t.ExpressionTree, c.s.s.t.Tree) => c.s.s.t.Tree |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
23810
diff
changeset
|
1952 |
* (j.u.List<capture#160 of ? extends c.s.s.d.DocTree>, Iterable) => |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
23810
diff
changeset
|
1953 |
* Iterable<capture#160 of ? extends c.s.s.d.DocTree> |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
23810
diff
changeset
|
1954 |
*/ |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
23810
diff
changeset
|
1955 |
if (sym.type == syms.objectType) { //optimization |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
23810
diff
changeset
|
1956 |
return syms.objectType; |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
23810
diff
changeset
|
1957 |
} |
21712
d363f367d4c0
8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents:
21041
diff
changeset
|
1958 |
return asSuper.visit(t, sym); |
10 | 1959 |
} |
1960 |
// where |
|
21712
d363f367d4c0
8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents:
21041
diff
changeset
|
1961 |
private SimpleVisitor<Type,Symbol> asSuper = new SimpleVisitor<Type,Symbol>() { |
d363f367d4c0
8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents:
21041
diff
changeset
|
1962 |
|
d363f367d4c0
8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents:
21041
diff
changeset
|
1963 |
public Type visitType(Type t, Symbol sym) { |
10 | 1964 |
return null; |
1965 |
} |
|
1966 |
||
1967 |
@Override |
|
21712
d363f367d4c0
8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents:
21041
diff
changeset
|
1968 |
public Type visitClassType(ClassType t, Symbol sym) { |
d363f367d4c0
8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents:
21041
diff
changeset
|
1969 |
if (t.tsym == sym) |
10 | 1970 |
return t; |
1971 |
||
1972 |
Type st = supertype(t); |
|
21712
d363f367d4c0
8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents:
21041
diff
changeset
|
1973 |
if (st.hasTag(CLASS) || st.hasTag(TYPEVAR) || st.hasTag(ERROR)) { |
d363f367d4c0
8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents:
21041
diff
changeset
|
1974 |
Type x = asSuper(st, sym); |
10 | 1975 |
if (x != null) |
1976 |
return x; |
|
1977 |
} |
|
21712
d363f367d4c0
8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents:
21041
diff
changeset
|
1978 |
if ((sym.flags() & INTERFACE) != 0) { |
10 | 1979 |
for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail) { |
21712
d363f367d4c0
8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents:
21041
diff
changeset
|
1980 |
Type x = asSuper(l.head, sym); |
10 | 1981 |
if (x != null) |
1982 |
return x; |
|
1983 |
} |
|
1984 |
} |
|
1985 |
return null; |
|
1986 |
} |
|
1987 |
||
1988 |
@Override |
|
21712
d363f367d4c0
8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents:
21041
diff
changeset
|
1989 |
public Type visitArrayType(ArrayType t, Symbol sym) { |
d363f367d4c0
8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents:
21041
diff
changeset
|
1990 |
return isSubtype(t, sym.type) ? sym.type : null; |
10 | 1991 |
} |
1992 |
||
1993 |
@Override |
|
21712
d363f367d4c0
8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents:
21041
diff
changeset
|
1994 |
public Type visitTypeVar(TypeVar t, Symbol sym) { |
d363f367d4c0
8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents:
21041
diff
changeset
|
1995 |
if (t.tsym == sym) |
326
d51f30ce6796
6531090: Cannot access methods/fields of a captured type belonging to an intersection type
mcimadamore
parents:
162
diff
changeset
|
1996 |
return t; |
d51f30ce6796
6531090: Cannot access methods/fields of a captured type belonging to an intersection type
mcimadamore
parents:
162
diff
changeset
|
1997 |
else |
21712
d363f367d4c0
8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents:
21041
diff
changeset
|
1998 |
return asSuper(t.bound, sym); |
10 | 1999 |
} |
2000 |
||
2001 |
@Override |
|
21712
d363f367d4c0
8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents:
21041
diff
changeset
|
2002 |
public Type visitErrorType(ErrorType t, Symbol sym) { |
d363f367d4c0
8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents:
21041
diff
changeset
|
2003 |
return t; |
d363f367d4c0
8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents:
21041
diff
changeset
|
2004 |
} |
10 | 2005 |
}; |
2006 |
||
2007 |
/** |
|
2008 |
* Return the base type of t or any of its outer types that starts |
|
2009 |
* with the given symbol. If none exists, return null. |
|
2010 |
* |
|
2011 |
* @param t a type |
|
2012 |
* @param sym a symbol |
|
2013 |
*/ |
|
2014 |
public Type asOuterSuper(Type t, Symbol sym) { |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
2015 |
switch (t.getTag()) { |
10 | 2016 |
case CLASS: |
2017 |
do { |
|
2018 |
Type s = asSuper(t, sym); |
|
2019 |
if (s != null) return s; |
|
2020 |
t = t.getEnclosingType(); |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
2021 |
} while (t.hasTag(CLASS)); |
10 | 2022 |
return null; |
2023 |
case ARRAY: |
|
2024 |
return isSubtype(t, sym.type) ? sym.type : null; |
|
2025 |
case TYPEVAR: |
|
2026 |
return asSuper(t, sym); |
|
2027 |
case ERROR: |
|
2028 |
return t; |
|
2029 |
default: |
|
2030 |
return null; |
|
2031 |
} |
|
2032 |
} |
|
2033 |
||
2034 |
/** |
|
2035 |
* Return the base type of t or any of its enclosing types that |
|
2036 |
* starts with the given symbol. If none exists, return null. |
|
2037 |
* |
|
2038 |
* @param t a type |
|
2039 |
* @param sym a symbol |
|
2040 |
*/ |
|
2041 |
public Type asEnclosingSuper(Type t, Symbol sym) { |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
2042 |
switch (t.getTag()) { |
10 | 2043 |
case CLASS: |
2044 |
do { |
|
2045 |
Type s = asSuper(t, sym); |
|
2046 |
if (s != null) return s; |
|
2047 |
Type outer = t.getEnclosingType(); |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
2048 |
t = (outer.hasTag(CLASS)) ? outer : |
10 | 2049 |
(t.tsym.owner.enclClass() != null) ? t.tsym.owner.enclClass().type : |
2050 |
Type.noType; |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
2051 |
} while (t.hasTag(CLASS)); |
10 | 2052 |
return null; |
2053 |
case ARRAY: |
|
2054 |
return isSubtype(t, sym.type) ? sym.type : null; |
|
2055 |
case TYPEVAR: |
|
2056 |
return asSuper(t, sym); |
|
2057 |
case ERROR: |
|
2058 |
return t; |
|
2059 |
default: |
|
2060 |
return null; |
|
2061 |
} |
|
2062 |
} |
|
2063 |
// </editor-fold> |
|
2064 |
||
2065 |
// <editor-fold defaultstate="collapsed" desc="memberType"> |
|
2066 |
/** |
|
2067 |
* The type of given symbol, seen as a member of t. |
|
2068 |
* |
|
2069 |
* @param t a type |
|
2070 |
* @param sym a symbol |
|
2071 |
*/ |
|
2072 |
public Type memberType(Type t, Symbol sym) { |
|
2073 |
return (sym.flags() & STATIC) != 0 |
|
2074 |
? sym.type |
|
2075 |
: memberType.visit(t, sym); |
|
3539
3c265cb6f4e0
6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents:
3140
diff
changeset
|
2076 |
} |
10 | 2077 |
// where |
2078 |
private SimpleVisitor<Type,Symbol> memberType = new SimpleVisitor<Type,Symbol>() { |
|
2079 |
||
2080 |
public Type visitType(Type t, Symbol sym) { |
|
2081 |
return sym.type; |
|
2082 |
} |
|
2083 |
||
2084 |
@Override |
|
2085 |
public Type visitWildcardType(WildcardType t, Symbol sym) { |
|
2086 |
return memberType(upperBound(t), sym); |
|
2087 |
} |
|
2088 |
||
2089 |
@Override |
|
2090 |
public Type visitClassType(ClassType t, Symbol sym) { |
|
2091 |
Symbol owner = sym.owner; |
|
2092 |
long flags = sym.flags(); |
|
2093 |
if (((flags & STATIC) == 0) && owner.type.isParameterized()) { |
|
2094 |
Type base = asOuterSuper(t, owner); |
|
1469
9c381121f301
6738538: javac crashes when using a type parameter as a covariant method return type
mcimadamore
parents:
1357
diff
changeset
|
2095 |
//if t is an intersection type T = CT & I1 & I2 ... & In |
9c381121f301
6738538: javac crashes when using a type parameter as a covariant method return type
mcimadamore
parents:
1357
diff
changeset
|
2096 |
//its supertypes CT, I1, ... In might contain wildcards |
9c381121f301
6738538: javac crashes when using a type parameter as a covariant method return type
mcimadamore
parents:
1357
diff
changeset
|
2097 |
//so we need to go through capture conversion |
9c381121f301
6738538: javac crashes when using a type parameter as a covariant method return type
mcimadamore
parents:
1357
diff
changeset
|
2098 |
base = t.isCompound() ? capture(base) : base; |
10 | 2099 |
if (base != null) { |
2100 |
List<Type> ownerParams = owner.type.allparams(); |
|
2101 |
List<Type> baseParams = base.allparams(); |
|
2102 |
if (ownerParams.nonEmpty()) { |
|
2103 |
if (baseParams.isEmpty()) { |
|
2104 |
// then base is a raw type |
|
2105 |
return erasure(sym.type); |
|
2106 |
} else { |
|
2107 |
return subst(sym.type, ownerParams, baseParams); |
|
2108 |
} |
|
2109 |
} |
|
2110 |
} |
|
2111 |
} |
|
2112 |
return sym.type; |
|
2113 |
} |
|
2114 |
||
2115 |
@Override |
|
2116 |
public Type visitTypeVar(TypeVar t, Symbol sym) { |
|
2117 |
return memberType(t.bound, sym); |
|
2118 |
} |
|
2119 |
||
2120 |
@Override |
|
2121 |
public Type visitErrorType(ErrorType t, Symbol sym) { |
|
2122 |
return t; |
|
2123 |
} |
|
2124 |
}; |
|
2125 |
// </editor-fold> |
|
2126 |
||
2127 |
// <editor-fold defaultstate="collapsed" desc="isAssignable"> |
|
2128 |
public boolean isAssignable(Type t, Type s) { |
|
14547 | 2129 |
return isAssignable(t, s, noWarnings); |
10 | 2130 |
} |
2131 |
||
2132 |
/** |
|
2133 |
* Is t assignable to s?<br> |
|
2134 |
* Equivalent to subtype except for constant values and raw |
|
2135 |
* types.<br> |
|
2136 |
* (not defined for Method and ForAll types) |
|
2137 |
*/ |
|
2138 |
public boolean isAssignable(Type t, Type s, Warner warn) { |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
2139 |
if (t.hasTag(ERROR)) |
10 | 2140 |
return true; |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
2141 |
if (t.getTag().isSubRangeOf(INT) && t.constValue() != null) { |
10 | 2142 |
int value = ((Number)t.constValue()).intValue(); |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
2143 |
switch (s.getTag()) { |
10 | 2144 |
case BYTE: |
2145 |
if (Byte.MIN_VALUE <= value && value <= Byte.MAX_VALUE) |
|
2146 |
return true; |
|
2147 |
break; |
|
2148 |
case CHAR: |
|
2149 |
if (Character.MIN_VALUE <= value && value <= Character.MAX_VALUE) |
|
2150 |
return true; |
|
2151 |
break; |
|
2152 |
case SHORT: |
|
2153 |
if (Short.MIN_VALUE <= value && value <= Short.MAX_VALUE) |
|
2154 |
return true; |
|
2155 |
break; |
|
2156 |
case INT: |
|
2157 |
return true; |
|
2158 |
case CLASS: |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
2159 |
switch (unboxedType(s).getTag()) { |
10 | 2160 |
case BYTE: |
2161 |
case CHAR: |
|
2162 |
case SHORT: |
|
2163 |
return isAssignable(t, unboxedType(s), warn); |
|
2164 |
} |
|
2165 |
break; |
|
2166 |
} |
|
2167 |
} |
|
2168 |
return isConvertible(t, s, warn); |
|
2169 |
} |
|
2170 |
// </editor-fold> |
|
2171 |
||
2172 |
// <editor-fold defaultstate="collapsed" desc="erasure"> |
|
2173 |
/** |
|
2174 |
* The erasure of t {@code |t|} -- the type that results when all |
|
2175 |
* type parameters in t are deleted. |
|
2176 |
*/ |
|
2177 |
public Type erasure(Type t) { |
|
13633
0ea4283af723
7181320: javac NullPointerException for switch labels with cast to String expressions
sundar
parents:
12468
diff
changeset
|
2178 |
return eraseNotNeeded(t)? t : erasure(t, false); |
514
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2179 |
} |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2180 |
//where |
13633
0ea4283af723
7181320: javac NullPointerException for switch labels with cast to String expressions
sundar
parents:
12468
diff
changeset
|
2181 |
private boolean eraseNotNeeded(Type t) { |
0ea4283af723
7181320: javac NullPointerException for switch labels with cast to String expressions
sundar
parents:
12468
diff
changeset
|
2182 |
// We don't want to erase primitive types and String type as that |
0ea4283af723
7181320: javac NullPointerException for switch labels with cast to String expressions
sundar
parents:
12468
diff
changeset
|
2183 |
// operation is idempotent. Also, erasing these could result in loss |
0ea4283af723
7181320: javac NullPointerException for switch labels with cast to String expressions
sundar
parents:
12468
diff
changeset
|
2184 |
// of information such as constant values attached to such types. |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
2185 |
return (t.isPrimitive()) || (syms.stringType.tsym == t.tsym); |
13633
0ea4283af723
7181320: javac NullPointerException for switch labels with cast to String expressions
sundar
parents:
12468
diff
changeset
|
2186 |
} |
0ea4283af723
7181320: javac NullPointerException for switch labels with cast to String expressions
sundar
parents:
12468
diff
changeset
|
2187 |
|
514
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2188 |
private Type erasure(Type t, boolean recurse) { |
24396 | 2189 |
if (t.isPrimitive()) { |
10 | 2190 |
return t; /* fast special case */ |
24396 | 2191 |
} else { |
2192 |
Type out = erasure.visit(t, recurse); |
|
2193 |
return out; |
|
2194 |
} |
|
3539
3c265cb6f4e0
6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents:
3140
diff
changeset
|
2195 |
} |
10 | 2196 |
// where |
514
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2197 |
private SimpleVisitor<Type, Boolean> erasure = new SimpleVisitor<Type, Boolean>() { |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2198 |
public Type visitType(Type t, Boolean recurse) { |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14259
diff
changeset
|
2199 |
if (t.isPrimitive()) |
10 | 2200 |
return t; /*fast special case*/ |
24396 | 2201 |
else { |
2202 |
final List<Attribute.TypeCompound> annos = t.getAnnotationMirrors(); |
|
2203 |
Type erased = t.map(recurse ? erasureRecFun : erasureFun); |
|
2204 |
if (!annos.isEmpty()) { |
|
2205 |
erased = erased.annotatedType(annos); |
|
2206 |
} |
|
2207 |
return erased; |
|
2208 |
} |
|
10 | 2209 |
} |
2210 |
||
2211 |
@Override |
|
514
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2212 |
public Type visitWildcardType(WildcardType t, Boolean recurse) { |
24396 | 2213 |
final List<Attribute.TypeCompound> annos = t.getAnnotationMirrors(); |
2214 |
Type erased = erasure(upperBound(t), recurse); |
|
2215 |
if (!annos.isEmpty()) { |
|
2216 |
erased = erased.annotatedType(annos); |
|
2217 |
} |
|
2218 |
return erased; |
|
10 | 2219 |
} |
2220 |
||
2221 |
@Override |
|
514
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2222 |
public Type visitClassType(ClassType t, Boolean recurse) { |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2223 |
Type erased = t.tsym.erasure(Types.this); |
24396 | 2224 |
List<Attribute.TypeCompound> annos = t.getAnnotationMirrors(); |
514
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2225 |
if (recurse) { |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2226 |
erased = new ErasedClassType(erased.getEnclosingType(),erased.tsym); |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2227 |
} |
24396 | 2228 |
if (!annos.isEmpty()) { |
2229 |
erased = erased.annotatedType(annos); |
|
2230 |
} |
|
514
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2231 |
return erased; |
10 | 2232 |
} |
2233 |
||
2234 |
@Override |
|
514
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2235 |
public Type visitTypeVar(TypeVar t, Boolean recurse) { |
24396 | 2236 |
final List<Attribute.TypeCompound> annos = t.getAnnotationMirrors(); |
2237 |
Type erased = erasure(t.bound, recurse); |
|
2238 |
if (!annos.isEmpty()) { |
|
2239 |
erased = erased.annotatedType(annos); |
|
2240 |
} |
|
2241 |
return erased; |
|
514
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2242 |
} |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2243 |
|
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2244 |
@Override |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2245 |
public Type visitErrorType(ErrorType t, Boolean recurse) { |
10 | 2246 |
return t; |
2247 |
} |
|
2248 |
}; |
|
514
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2249 |
|
10 | 2250 |
private Mapping erasureFun = new Mapping ("erasure") { |
2251 |
public Type apply(Type t) { return erasure(t); } |
|
2252 |
}; |
|
2253 |
||
514
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2254 |
private Mapping erasureRecFun = new Mapping ("erasureRecursive") { |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2255 |
public Type apply(Type t) { return erasureRecursive(t); } |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2256 |
}; |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2257 |
|
10 | 2258 |
public List<Type> erasure(List<Type> ts) { |
2259 |
return Type.map(ts, erasureFun); |
|
2260 |
} |
|
514
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2261 |
|
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2262 |
public Type erasureRecursive(Type t) { |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2263 |
return erasure(t, true); |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2264 |
} |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2265 |
|
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2266 |
public List<Type> erasureRecursive(List<Type> ts) { |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2267 |
return Type.map(ts, erasureRecFun); |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2268 |
} |
10 | 2269 |
// </editor-fold> |
2270 |
||
2271 |
// <editor-fold defaultstate="collapsed" desc="makeCompoundType"> |
|
2272 |
/** |
|
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
2273 |
* Make a compound type from non-empty list of types. The list should be |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
2274 |
* ordered according to {@link Symbol#precedes(TypeSymbol,Types)}. |
10 | 2275 |
* |
2276 |
* @param bounds the types from which the compound type is formed |
|
2277 |
* @param supertype is objectType if all bounds are interfaces, |
|
2278 |
* null otherwise. |
|
2279 |
*/ |
|
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
2280 |
public Type makeCompoundType(List<Type> bounds) { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
2281 |
return makeCompoundType(bounds, bounds.head.tsym.isInterface()); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
2282 |
} |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
2283 |
public Type makeCompoundType(List<Type> bounds, boolean allInterfaces) { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
2284 |
Assert.check(bounds.nonEmpty()); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
2285 |
Type firstExplicitBound = bounds.head; |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
2286 |
if (allInterfaces) { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
2287 |
bounds = bounds.prepend(syms.objectType); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
2288 |
} |
10 | 2289 |
ClassSymbol bc = |
2290 |
new ClassSymbol(ABSTRACT|PUBLIC|SYNTHETIC|COMPOUND|ACYCLIC, |
|
2291 |
Type.moreInfo |
|
2292 |
? names.fromString(bounds.toString()) |
|
2293 |
: names.empty, |
|
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
2294 |
null, |
10 | 2295 |
syms.noSymbol); |
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
2296 |
bc.type = new IntersectionClassType(bounds, bc, allInterfaces); |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
2297 |
bc.erasure_field = (bounds.head.hasTag(TYPEVAR)) ? |
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
2298 |
syms.objectType : // error condition, recover |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
2299 |
erasure(firstExplicitBound); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
2300 |
bc.members_field = new Scope(bc); |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
2301 |
return bc.type; |
10 | 2302 |
} |
2303 |
||
2304 |
/** |
|
2305 |
* A convenience wrapper for {@link #makeCompoundType(List)}; the |
|
2306 |
* arguments are converted to a list and passed to the other |
|
2307 |
* method. Note that this might cause a symbol completion. |
|
2308 |
* Hence, this version of makeCompoundType may not be called |
|
2309 |
* during a classfile read. |
|
2310 |
*/ |
|
2311 |
public Type makeCompoundType(Type bound1, Type bound2) { |
|
2312 |
return makeCompoundType(List.of(bound1, bound2)); |
|
2313 |
} |
|
2314 |
// </editor-fold> |
|
2315 |
||
2316 |
// <editor-fold defaultstate="collapsed" desc="supertype"> |
|
2317 |
public Type supertype(Type t) { |
|
2318 |
return supertype.visit(t); |
|
2319 |
} |
|
2320 |
// where |
|
2321 |
private UnaryVisitor<Type> supertype = new UnaryVisitor<Type>() { |
|
2322 |
||
2323 |
public Type visitType(Type t, Void ignored) { |
|
2324 |
// A note on wildcards: there is no good way to |
|
2325 |
// determine a supertype for a super bounded wildcard. |
|
2326 |
return null; |
|
2327 |
} |
|
2328 |
||
2329 |
@Override |
|
2330 |
public Type visitClassType(ClassType t, Void ignored) { |
|
2331 |
if (t.supertype_field == null) { |
|
2332 |
Type supertype = ((ClassSymbol)t.tsym).getSuperclass(); |
|
2333 |
// An interface has no superclass; its supertype is Object. |
|
2334 |
if (t.isInterface()) |
|
2335 |
supertype = ((ClassType)t.tsym.type).supertype_field; |
|
2336 |
if (t.supertype_field == null) { |
|
2337 |
List<Type> actuals = classBound(t).allparams(); |
|
2338 |
List<Type> formals = t.tsym.type.allparams(); |
|
514
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2339 |
if (t.hasErasedSupertypes()) { |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2340 |
t.supertype_field = erasureRecursive(supertype); |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2341 |
} else if (formals.nonEmpty()) { |
10 | 2342 |
t.supertype_field = subst(supertype, formals, actuals); |
2343 |
} |
|
514
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2344 |
else { |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2345 |
t.supertype_field = supertype; |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2346 |
} |
10 | 2347 |
} |
2348 |
} |
|
2349 |
return t.supertype_field; |
|
2350 |
} |
|
2351 |
||
2352 |
/** |
|
2353 |
* The supertype is always a class type. If the type |
|
2354 |
* variable's bounds start with a class type, this is also |
|
2355 |
* the supertype. Otherwise, the supertype is |
|
2356 |
* java.lang.Object. |
|
2357 |
*/ |
|
2358 |
@Override |
|
2359 |
public Type visitTypeVar(TypeVar t, Void ignored) { |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
2360 |
if (t.bound.hasTag(TYPEVAR) || |
10 | 2361 |
(!t.bound.isCompound() && !t.bound.isInterface())) { |
2362 |
return t.bound; |
|
2363 |
} else { |
|
2364 |
return supertype(t.bound); |
|
2365 |
} |
|
2366 |
} |
|
2367 |
||
2368 |
@Override |
|
2369 |
public Type visitArrayType(ArrayType t, Void ignored) { |
|
2370 |
if (t.elemtype.isPrimitive() || isSameType(t.elemtype, syms.objectType)) |
|
2371 |
return arraySuperType(); |
|
2372 |
else |
|
2373 |
return new ArrayType(supertype(t.elemtype), t.tsym); |
|
2374 |
} |
|
2375 |
||
2376 |
@Override |
|
2377 |
public Type visitErrorType(ErrorType t, Void ignored) { |
|
18915
dcc9c8265f65
8016640: compiler hangs if the generics arity of a base class is wrong
mcimadamore
parents:
18914
diff
changeset
|
2378 |
return Type.noType; |
10 | 2379 |
} |
2380 |
}; |
|
2381 |
// </editor-fold> |
|
2382 |
||
2383 |
// <editor-fold defaultstate="collapsed" desc="interfaces"> |
|
2384 |
/** |
|
2385 |
* Return the interfaces implemented by this class. |
|
2386 |
*/ |
|
2387 |
public List<Type> interfaces(Type t) { |
|
2388 |
return interfaces.visit(t); |
|
2389 |
} |
|
2390 |
// where |
|
2391 |
private UnaryVisitor<List<Type>> interfaces = new UnaryVisitor<List<Type>>() { |
|
2392 |
||
2393 |
public List<Type> visitType(Type t, Void ignored) { |
|
2394 |
return List.nil(); |
|
2395 |
} |
|
2396 |
||
2397 |
@Override |
|
2398 |
public List<Type> visitClassType(ClassType t, Void ignored) { |
|
2399 |
if (t.interfaces_field == null) { |
|
2400 |
List<Type> interfaces = ((ClassSymbol)t.tsym).getInterfaces(); |
|
2401 |
if (t.interfaces_field == null) { |
|
2402 |
// If t.interfaces_field is null, then t must |
|
2403 |
// be a parameterized type (not to be confused |
|
2404 |
// with a generic type declaration). |
|
2405 |
// Terminology: |
|
2406 |
// Parameterized type: List<String> |
|
2407 |
// Generic type declaration: class List<E> { ... } |
|
2408 |
// So t corresponds to List<String> and |
|
2409 |
// t.tsym.type corresponds to List<E>. |
|
2410 |
// The reason t must be parameterized type is |
|
2411 |
// that completion will happen as a side |
|
2412 |
// effect of calling |
|
2413 |
// ClassSymbol.getInterfaces. Since |
|
2414 |
// t.interfaces_field is null after |
|
2415 |
// completion, we can assume that t is not the |
|
2416 |
// type of a class/interface declaration. |
|
8032 | 2417 |
Assert.check(t != t.tsym.type, t); |
10 | 2418 |
List<Type> actuals = t.allparams(); |
2419 |
List<Type> formals = t.tsym.type.allparams(); |
|
514
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2420 |
if (t.hasErasedSupertypes()) { |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2421 |
t.interfaces_field = erasureRecursive(interfaces); |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2422 |
} else if (formals.nonEmpty()) { |
10 | 2423 |
t.interfaces_field = |
2424 |
upperBounds(subst(interfaces, formals, actuals)); |
|
2425 |
} |
|
514
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2426 |
else { |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2427 |
t.interfaces_field = interfaces; |
3942d9cdc81c
6559182: Cast from a raw type with non-generic supertype to a raw type fails unexpectedly
mcimadamore
parents:
326
diff
changeset
|
2428 |
} |
10 | 2429 |
} |
2430 |
} |
|
2431 |
return t.interfaces_field; |
|
2432 |
} |
|
2433 |
||
2434 |
@Override |
|
2435 |
public List<Type> visitTypeVar(TypeVar t, Void ignored) { |
|
2436 |
if (t.bound.isCompound()) |
|
2437 |
return interfaces(t.bound); |
|
2438 |
||
2439 |
if (t.bound.isInterface()) |
|
2440 |
return List.of(t.bound); |
|
2441 |
||
2442 |
return List.nil(); |
|
2443 |
} |
|
2444 |
}; |
|
14443
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2445 |
|
20252
fae5c47ba7a4
6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents:
20249
diff
changeset
|
2446 |
public List<Type> directSupertypes(Type t) { |
fae5c47ba7a4
6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents:
20249
diff
changeset
|
2447 |
return directSupertypes.visit(t); |
fae5c47ba7a4
6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents:
20249
diff
changeset
|
2448 |
} |
fae5c47ba7a4
6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents:
20249
diff
changeset
|
2449 |
// where |
fae5c47ba7a4
6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents:
20249
diff
changeset
|
2450 |
private final UnaryVisitor<List<Type>> directSupertypes = new UnaryVisitor<List<Type>>() { |
fae5c47ba7a4
6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents:
20249
diff
changeset
|
2451 |
|
fae5c47ba7a4
6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents:
20249
diff
changeset
|
2452 |
public List<Type> visitType(final Type type, final Void ignored) { |
fae5c47ba7a4
6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents:
20249
diff
changeset
|
2453 |
if (!type.isCompound()) { |
fae5c47ba7a4
6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents:
20249
diff
changeset
|
2454 |
final Type sup = supertype(type); |
fae5c47ba7a4
6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents:
20249
diff
changeset
|
2455 |
return (sup == Type.noType || sup == type || sup == null) |
fae5c47ba7a4
6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents:
20249
diff
changeset
|
2456 |
? interfaces(type) |
fae5c47ba7a4
6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents:
20249
diff
changeset
|
2457 |
: interfaces(type).prepend(sup); |
fae5c47ba7a4
6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents:
20249
diff
changeset
|
2458 |
} else { |
fae5c47ba7a4
6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents:
20249
diff
changeset
|
2459 |
return visitIntersectionType((IntersectionClassType) type); |
fae5c47ba7a4
6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents:
20249
diff
changeset
|
2460 |
} |
fae5c47ba7a4
6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents:
20249
diff
changeset
|
2461 |
} |
fae5c47ba7a4
6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents:
20249
diff
changeset
|
2462 |
|
fae5c47ba7a4
6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents:
20249
diff
changeset
|
2463 |
private List<Type> visitIntersectionType(final IntersectionClassType it) { |
fae5c47ba7a4
6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents:
20249
diff
changeset
|
2464 |
return it.getExplicitComponents(); |
fae5c47ba7a4
6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents:
20249
diff
changeset
|
2465 |
} |
fae5c47ba7a4
6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents:
20249
diff
changeset
|
2466 |
|
fae5c47ba7a4
6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents:
20249
diff
changeset
|
2467 |
}; |
fae5c47ba7a4
6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents:
20249
diff
changeset
|
2468 |
|
14547 | 2469 |
public boolean isDirectSuperInterface(TypeSymbol isym, TypeSymbol origin) { |
2470 |
for (Type i2 : interfaces(origin.type)) { |
|
2471 |
if (isym == i2.tsym) return true; |
|
14443
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2472 |
} |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2473 |
return false; |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2474 |
} |
10 | 2475 |
// </editor-fold> |
2476 |
||
2477 |
// <editor-fold defaultstate="collapsed" desc="isDerivedRaw"> |
|
22163 | 2478 |
Map<Type,Boolean> isDerivedRawCache = new HashMap<>(); |
10 | 2479 |
|
2480 |
public boolean isDerivedRaw(Type t) { |
|
2481 |
Boolean result = isDerivedRawCache.get(t); |
|
2482 |
if (result == null) { |
|
2483 |
result = isDerivedRawInternal(t); |
|
2484 |
isDerivedRawCache.put(t, result); |
|
2485 |
} |
|
2486 |
return result; |
|
2487 |
} |
|
2488 |
||
2489 |
public boolean isDerivedRawInternal(Type t) { |
|
2490 |
if (t.isErroneous()) |
|
2491 |
return false; |
|
2492 |
return |
|
2493 |
t.isRaw() || |
|
2494 |
supertype(t) != null && isDerivedRaw(supertype(t)) || |
|
2495 |
isDerivedRaw(interfaces(t)); |
|
2496 |
} |
|
2497 |
||
2498 |
public boolean isDerivedRaw(List<Type> ts) { |
|
2499 |
List<Type> l = ts; |
|
2500 |
while (l.nonEmpty() && !isDerivedRaw(l.head)) l = l.tail; |
|
2501 |
return l.nonEmpty(); |
|
2502 |
} |
|
2503 |
// </editor-fold> |
|
2504 |
||
2505 |
// <editor-fold defaultstate="collapsed" desc="setBounds"> |
|
2506 |
/** |
|
2507 |
* Set the bounds field of the given type variable to reflect a |
|
2508 |
* (possibly multiple) list of bounds. |
|
2509 |
* @param t a type variable |
|
2510 |
* @param bounds the bounds, must be nonempty |
|
2511 |
* @param supertype is objectType if all bounds are interfaces, |
|
2512 |
* null otherwise. |
|
2513 |
*/ |
|
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
2514 |
public void setBounds(TypeVar t, List<Type> bounds) { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
2515 |
setBounds(t, bounds, bounds.head.tsym.isInterface()); |
10 | 2516 |
} |
2517 |
||
2518 |
/** |
|
2519 |
* Same as {@link #setBounds(Type.TypeVar,List,Type)}, except that |
|
5650
313b819dbcc2
6948381: javac Null Pointer Exception in Types.makeCompoundType
mcimadamore
parents:
5648
diff
changeset
|
2520 |
* third parameter is computed directly, as follows: if all |
313b819dbcc2
6948381: javac Null Pointer Exception in Types.makeCompoundType
mcimadamore
parents:
5648
diff
changeset
|
2521 |
* all bounds are interface types, the computed supertype is Object, |
313b819dbcc2
6948381: javac Null Pointer Exception in Types.makeCompoundType
mcimadamore
parents:
5648
diff
changeset
|
2522 |
* otherwise the supertype is simply left null (in this case, the supertype |
313b819dbcc2
6948381: javac Null Pointer Exception in Types.makeCompoundType
mcimadamore
parents:
5648
diff
changeset
|
2523 |
* is assumed to be the head of the bound list passed as second argument). |
313b819dbcc2
6948381: javac Null Pointer Exception in Types.makeCompoundType
mcimadamore
parents:
5648
diff
changeset
|
2524 |
* Note that this check might cause a symbol completion. Hence, this version of |
10 | 2525 |
* setBounds may not be called during a classfile read. |
2526 |
*/ |
|
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
2527 |
public void setBounds(TypeVar t, List<Type> bounds, boolean allInterfaces) { |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
2528 |
t.bound = bounds.tail.isEmpty() ? |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
2529 |
bounds.head : |
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
2530 |
makeCompoundType(bounds, allInterfaces); |
10 | 2531 |
t.rank_field = -1; |
2532 |
} |
|
2533 |
// </editor-fold> |
|
2534 |
||
2535 |
// <editor-fold defaultstate="collapsed" desc="getBounds"> |
|
2536 |
/** |
|
2537 |
* Return list of bounds of the given type variable. |
|
2538 |
*/ |
|
2539 |
public List<Type> getBounds(TypeVar t) { |
|
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
2540 |
if (t.bound.hasTag(NONE)) |
14547 | 2541 |
return List.nil(); |
2542 |
else if (t.bound.isErroneous() || !t.bound.isCompound()) |
|
10 | 2543 |
return List.of(t.bound); |
2544 |
else if ((erasure(t).tsym.flags() & INTERFACE) == 0) |
|
2545 |
return interfaces(t).prepend(supertype(t)); |
|
2546 |
else |
|
2547 |
// No superclass was given in bounds. |
|
2548 |
// In this case, supertype is Object, erasure is first interface. |
|
2549 |
return interfaces(t); |
|
2550 |
} |
|
2551 |
// </editor-fold> |
|
2552 |
||
2553 |
// <editor-fold defaultstate="collapsed" desc="classBound"> |
|
2554 |
/** |
|
2555 |
* If the given type is a (possibly selected) type variable, |
|
2556 |
* return the bounding class of this type, otherwise return the |
|
2557 |
* type itself. |
|
2558 |
*/ |
|
2559 |
public Type classBound(Type t) { |
|
2560 |
return classBound.visit(t); |
|
2561 |
} |
|
2562 |
// where |
|
2563 |
private UnaryVisitor<Type> classBound = new UnaryVisitor<Type>() { |
|
2564 |
||
2565 |
public Type visitType(Type t, Void ignored) { |
|
2566 |
return t; |
|
2567 |
} |
|
2568 |
||
2569 |
@Override |
|
2570 |
public Type visitClassType(ClassType t, Void ignored) { |
|
2571 |
Type outer1 = classBound(t.getEnclosingType()); |
|
2572 |
if (outer1 != t.getEnclosingType()) |
|
24396 | 2573 |
return new ClassType(outer1, t.getTypeArguments(), t.tsym, |
2574 |
t.getAnnotationMirrors()); |
|
10 | 2575 |
else |
2576 |
return t; |
|
2577 |
} |
|
2578 |
||
2579 |
@Override |
|
2580 |
public Type visitTypeVar(TypeVar t, Void ignored) { |
|
2581 |
return classBound(supertype(t)); |
|
2582 |
} |
|
2583 |
||
2584 |
@Override |
|
2585 |
public Type visitErrorType(ErrorType t, Void ignored) { |
|
2586 |
return t; |
|
2587 |
} |
|
2588 |
}; |
|
2589 |
// </editor-fold> |
|
2590 |
||
2591 |
// <editor-fold defaultstate="collapsed" desc="sub signature / override equivalence"> |
|
2592 |
/** |
|
2593 |
* Returns true iff the first signature is a <em>sub |
|
2594 |
* signature</em> of the other. This is <b>not</b> an equivalence |
|
2595 |
* relation. |
|
2596 |
* |
|
9303
eae35c201e19
7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents:
9301
diff
changeset
|
2597 |
* @jls section 8.4.2. |
10 | 2598 |
* @see #overrideEquivalent(Type t, Type s) |
2599 |
* @param t first signature (possibly raw). |
|
2600 |
* @param s second signature (could be subjected to erasure). |
|
2601 |
* @return true if t is a sub signature of s. |
|
2602 |
*/ |
|
2603 |
public boolean isSubSignature(Type t, Type s) { |
|
8628
9e31f1c01bd4
7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents:
8625
diff
changeset
|
2604 |
return isSubSignature(t, s, true); |
9e31f1c01bd4
7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents:
8625
diff
changeset
|
2605 |
} |
9e31f1c01bd4
7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents:
8625
diff
changeset
|
2606 |
|
9e31f1c01bd4
7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents:
8625
diff
changeset
|
2607 |
public boolean isSubSignature(Type t, Type s, boolean strict) { |
9e31f1c01bd4
7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents:
8625
diff
changeset
|
2608 |
return hasSameArgs(t, s, strict) || hasSameArgs(t, erasure(s), strict); |
10 | 2609 |
} |
2610 |
||
2611 |
/** |
|
2612 |
* Returns true iff these signatures are related by <em>override |
|
2613 |
* equivalence</em>. This is the natural extension of |
|
2614 |
* isSubSignature to an equivalence relation. |
|
2615 |
* |
|
9303
eae35c201e19
7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents:
9301
diff
changeset
|
2616 |
* @jls section 8.4.2. |
10 | 2617 |
* @see #isSubSignature(Type t, Type s) |
2618 |
* @param t a signature (possible raw, could be subjected to |
|
2619 |
* erasure). |
|
2620 |
* @param s a signature (possible raw, could be subjected to |
|
2621 |
* erasure). |
|
2622 |
* @return true if either argument is a sub signature of the other. |
|
2623 |
*/ |
|
2624 |
public boolean overrideEquivalent(Type t, Type s) { |
|
2625 |
return hasSameArgs(t, s) || |
|
2626 |
hasSameArgs(t, erasure(s)) || hasSameArgs(erasure(t), s); |
|
2627 |
} |
|
2628 |
||
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
2629 |
public boolean overridesObjectMethod(TypeSymbol origin, Symbol msym) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
2630 |
for (Scope.Entry e = syms.objectType.tsym.members().lookup(msym.name) ; e.scope != null ; e = e.next()) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
2631 |
if (msym.overrides(e.sym, origin, Types.this, true)) { |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
2632 |
return true; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
2633 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
2634 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
2635 |
return false; |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
2636 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
2637 |
|
6591
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2638 |
// <editor-fold defaultstate="collapsed" desc="Determining method implementation in given site"> |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2639 |
class ImplementationCache { |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2640 |
|
22163 | 2641 |
private WeakHashMap<MethodSymbol, SoftReference<Map<TypeSymbol, Entry>>> _map = new WeakHashMap<>(); |
6591
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2642 |
|
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2643 |
class Entry { |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2644 |
final MethodSymbol cachedImpl; |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2645 |
final Filter<Symbol> implFilter; |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2646 |
final boolean checkResult; |
8427
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
2647 |
final int prevMark; |
6591
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2648 |
|
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2649 |
public Entry(MethodSymbol cachedImpl, |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2650 |
Filter<Symbol> scopeFilter, |
8427
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
2651 |
boolean checkResult, |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
2652 |
int prevMark) { |
6591
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2653 |
this.cachedImpl = cachedImpl; |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2654 |
this.implFilter = scopeFilter; |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2655 |
this.checkResult = checkResult; |
8427
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
2656 |
this.prevMark = prevMark; |
6591
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2657 |
} |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2658 |
|
8427
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
2659 |
boolean matches(Filter<Symbol> scopeFilter, boolean checkResult, int mark) { |
6591
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2660 |
return this.implFilter == scopeFilter && |
8427
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
2661 |
this.checkResult == checkResult && |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
2662 |
this.prevMark == mark; |
6591
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2663 |
} |
3539
3c265cb6f4e0
6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents:
3140
diff
changeset
|
2664 |
} |
6591
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2665 |
|
8242
3873b4aaf4a8
7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents:
8230
diff
changeset
|
2666 |
MethodSymbol get(MethodSymbol ms, TypeSymbol origin, boolean checkResult, Filter<Symbol> implFilter) { |
6591
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2667 |
SoftReference<Map<TypeSymbol, Entry>> ref_cache = _map.get(ms); |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2668 |
Map<TypeSymbol, Entry> cache = ref_cache != null ? ref_cache.get() : null; |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2669 |
if (cache == null) { |
22163 | 2670 |
cache = new HashMap<>(); |
2671 |
_map.put(ms, new SoftReference<>(cache)); |
|
6591
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2672 |
} |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2673 |
Entry e = cache.get(origin); |
9812
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2674 |
CompoundScope members = membersClosure(origin.type, true); |
6591
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2675 |
if (e == null || |
8427
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
2676 |
!e.matches(implFilter, checkResult, members.getMark())) { |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
2677 |
MethodSymbol impl = implementationInternal(ms, origin, checkResult, implFilter); |
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
2678 |
cache.put(origin, new Entry(impl, implFilter, checkResult, members.getMark())); |
6591
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2679 |
return impl; |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2680 |
} |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2681 |
else { |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2682 |
return e.cachedImpl; |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2683 |
} |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2684 |
} |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2685 |
|
8427
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
2686 |
private MethodSymbol implementationInternal(MethodSymbol ms, TypeSymbol origin, boolean checkResult, Filter<Symbol> implFilter) { |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
2687 |
for (Type t = origin.type; t.hasTag(CLASS) || t.hasTag(TYPEVAR); t = supertype(t)) { |
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
2688 |
while (t.hasTag(TYPEVAR)) |
3539
3c265cb6f4e0
6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents:
3140
diff
changeset
|
2689 |
t = t.getUpperBound(); |
3c265cb6f4e0
6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents:
3140
diff
changeset
|
2690 |
TypeSymbol c = t.tsym; |
6591
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2691 |
for (Scope.Entry e = c.members().lookup(ms.name, implFilter); |
3539
3c265cb6f4e0
6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents:
3140
diff
changeset
|
2692 |
e.scope != null; |
7628
e7baeb97d164
6476118: compiler bug causes runtime ClassCastException for generics overloading
mcimadamore
parents:
7627
diff
changeset
|
2693 |
e = e.next(implFilter)) { |
6591
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2694 |
if (e.sym != null && |
8427
703181b01773
7017664: Add listeners infrastracture to javac scopes
mcimadamore
parents:
8242
diff
changeset
|
2695 |
e.sym.overrides(ms, origin, Types.this, checkResult)) |
6591
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2696 |
return (MethodSymbol)e.sym; |
3539
3c265cb6f4e0
6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents:
3140
diff
changeset
|
2697 |
} |
3c265cb6f4e0
6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents:
3140
diff
changeset
|
2698 |
} |
6591
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2699 |
return null; |
3539
3c265cb6f4e0
6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents:
3140
diff
changeset
|
2700 |
} |
3c265cb6f4e0
6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents:
3140
diff
changeset
|
2701 |
} |
3c265cb6f4e0
6827648: Extremely slow compilation time for visitor pattern code + generics
mcimadamore
parents:
3140
diff
changeset
|
2702 |
|
6591
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2703 |
private ImplementationCache implCache = new ImplementationCache(); |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2704 |
|
8242
3873b4aaf4a8
7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents:
8230
diff
changeset
|
2705 |
public MethodSymbol implementation(MethodSymbol ms, TypeSymbol origin, boolean checkResult, Filter<Symbol> implFilter) { |
3873b4aaf4a8
7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents:
8230
diff
changeset
|
2706 |
return implCache.get(ms, origin, checkResult, implFilter); |
3873b4aaf4a8
7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents:
8230
diff
changeset
|
2707 |
} |
3873b4aaf4a8
7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents:
8230
diff
changeset
|
2708 |
// </editor-fold> |
3873b4aaf4a8
7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents:
8230
diff
changeset
|
2709 |
|
3873b4aaf4a8
7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents:
8230
diff
changeset
|
2710 |
// <editor-fold defaultstate="collapsed" desc="compute transitive closure of all members in given site"> |
9812
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2711 |
class MembersClosureCache extends SimpleVisitor<CompoundScope, Boolean> { |
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2712 |
|
22163 | 2713 |
private WeakHashMap<TypeSymbol, Entry> _map = new WeakHashMap<>(); |
9812
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2714 |
|
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2715 |
class Entry { |
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2716 |
final boolean skipInterfaces; |
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2717 |
final CompoundScope compoundScope; |
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2718 |
|
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2719 |
public Entry(boolean skipInterfaces, CompoundScope compoundScope) { |
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2720 |
this.skipInterfaces = skipInterfaces; |
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2721 |
this.compoundScope = compoundScope; |
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2722 |
} |
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2723 |
|
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2724 |
boolean matches(boolean skipInterfaces) { |
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2725 |
return this.skipInterfaces == skipInterfaces; |
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2726 |
} |
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2727 |
} |
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2728 |
|
10453
41fe3e16698a
7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents:
10452
diff
changeset
|
2729 |
List<TypeSymbol> seenTypes = List.nil(); |
41fe3e16698a
7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents:
10452
diff
changeset
|
2730 |
|
9812
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2731 |
/** members closure visitor methods **/ |
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2732 |
|
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2733 |
public CompoundScope visitType(Type t, Boolean skipInterface) { |
8242
3873b4aaf4a8
7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents:
8230
diff
changeset
|
2734 |
return null; |
3873b4aaf4a8
7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents:
8230
diff
changeset
|
2735 |
} |
3873b4aaf4a8
7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents:
8230
diff
changeset
|
2736 |
|
3873b4aaf4a8
7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents:
8230
diff
changeset
|
2737 |
@Override |
9812
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2738 |
public CompoundScope visitClassType(ClassType t, Boolean skipInterface) { |
10453
41fe3e16698a
7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents:
10452
diff
changeset
|
2739 |
if (seenTypes.contains(t.tsym)) { |
41fe3e16698a
7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents:
10452
diff
changeset
|
2740 |
//this is possible when an interface is implemented in multiple |
41fe3e16698a
7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents:
10452
diff
changeset
|
2741 |
//superclasses, or when a classs hierarchy is circular - in such |
41fe3e16698a
7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents:
10452
diff
changeset
|
2742 |
//cases we don't need to recurse (empty scope is returned) |
41fe3e16698a
7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents:
10452
diff
changeset
|
2743 |
return new CompoundScope(t.tsym); |
41fe3e16698a
7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents:
10452
diff
changeset
|
2744 |
} |
41fe3e16698a
7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents:
10452
diff
changeset
|
2745 |
try { |
41fe3e16698a
7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents:
10452
diff
changeset
|
2746 |
seenTypes = seenTypes.prepend(t.tsym); |
41fe3e16698a
7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents:
10452
diff
changeset
|
2747 |
ClassSymbol csym = (ClassSymbol)t.tsym; |
41fe3e16698a
7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents:
10452
diff
changeset
|
2748 |
Entry e = _map.get(csym); |
41fe3e16698a
7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents:
10452
diff
changeset
|
2749 |
if (e == null || !e.matches(skipInterface)) { |
41fe3e16698a
7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents:
10452
diff
changeset
|
2750 |
CompoundScope membersClosure = new CompoundScope(csym); |
41fe3e16698a
7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents:
10452
diff
changeset
|
2751 |
if (!skipInterface) { |
41fe3e16698a
7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents:
10452
diff
changeset
|
2752 |
for (Type i : interfaces(t)) { |
41fe3e16698a
7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents:
10452
diff
changeset
|
2753 |
membersClosure.addSubScope(visit(i, skipInterface)); |
41fe3e16698a
7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents:
10452
diff
changeset
|
2754 |
} |
9812
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2755 |
} |
10453
41fe3e16698a
7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents:
10452
diff
changeset
|
2756 |
membersClosure.addSubScope(visit(supertype(t), skipInterface)); |
41fe3e16698a
7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents:
10452
diff
changeset
|
2757 |
membersClosure.addSubScope(csym.members()); |
41fe3e16698a
7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents:
10452
diff
changeset
|
2758 |
e = new Entry(skipInterface, membersClosure); |
41fe3e16698a
7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents:
10452
diff
changeset
|
2759 |
_map.put(csym, e); |
8242
3873b4aaf4a8
7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents:
8230
diff
changeset
|
2760 |
} |
10453
41fe3e16698a
7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents:
10452
diff
changeset
|
2761 |
return e.compoundScope; |
8242
3873b4aaf4a8
7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents:
8230
diff
changeset
|
2762 |
} |
10453
41fe3e16698a
7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents:
10452
diff
changeset
|
2763 |
finally { |
41fe3e16698a
7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents:
10452
diff
changeset
|
2764 |
seenTypes = seenTypes.tail; |
41fe3e16698a
7079713: javac hangs when compiling a class that references a cyclically inherited class
mcimadamore
parents:
10452
diff
changeset
|
2765 |
} |
8242
3873b4aaf4a8
7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents:
8230
diff
changeset
|
2766 |
} |
3873b4aaf4a8
7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents:
8230
diff
changeset
|
2767 |
|
3873b4aaf4a8
7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents:
8230
diff
changeset
|
2768 |
@Override |
9812
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2769 |
public CompoundScope visitTypeVar(TypeVar t, Boolean skipInterface) { |
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2770 |
return visit(t.getUpperBound(), skipInterface); |
8242
3873b4aaf4a8
7007615: java_util/generics/phase2/NameClashTest02 fails since jdk7/pit/b123.
mcimadamore
parents:
8230
diff
changeset
|
2771 |
} |
9812
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2772 |
} |
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2773 |
|
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2774 |
private MembersClosureCache membersCache = new MembersClosureCache(); |
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2775 |
|
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2776 |
public CompoundScope membersClosure(Type site, boolean skipInterface) { |
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2777 |
return membersCache.visit(site, skipInterface); |
f716e42cb230
7046348: Regression: javac complains of missing classfile for a seemingly unrelated interface
mcimadamore
parents:
9721
diff
changeset
|
2778 |
} |
6591
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2779 |
// </editor-fold> |
a953c8c6b85e
6337171: javac should create bridge methods when type variable bounds restricted
mcimadamore
parents:
6583
diff
changeset
|
2780 |
|
14443
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2781 |
|
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2782 |
//where |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2783 |
public List<MethodSymbol> interfaceCandidates(Type site, MethodSymbol ms) { |
14547 | 2784 |
Filter<Symbol> filter = new MethodFilter(ms, site); |
14443
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2785 |
List<MethodSymbol> candidates = List.nil(); |
16809
5acfcb821d65
8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents:
16567
diff
changeset
|
2786 |
for (Symbol s : membersClosure(site, false).getElements(filter)) { |
5acfcb821d65
8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents:
16567
diff
changeset
|
2787 |
if (!site.tsym.isInterface() && !s.owner.isInterface()) { |
5acfcb821d65
8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents:
16567
diff
changeset
|
2788 |
return List.of((MethodSymbol)s); |
5acfcb821d65
8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents:
16567
diff
changeset
|
2789 |
} else if (!candidates.contains(s)) { |
5acfcb821d65
8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents:
16567
diff
changeset
|
2790 |
candidates = candidates.prepend((MethodSymbol)s); |
5acfcb821d65
8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents:
16567
diff
changeset
|
2791 |
} |
14443
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2792 |
} |
16809
5acfcb821d65
8010822: Intersection type cast for functional expressions does not follow spec EDR
mcimadamore
parents:
16567
diff
changeset
|
2793 |
return prune(candidates); |
14443
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2794 |
} |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2795 |
|
16295
8b76e466f3cb
8007535: Compiler crashes on @FunctionalInterface used on interface with two inherited methods with same signatures
mcimadamore
parents:
16292
diff
changeset
|
2796 |
public List<MethodSymbol> prune(List<MethodSymbol> methods) { |
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19914
diff
changeset
|
2797 |
ListBuffer<MethodSymbol> methodsMin = new ListBuffer<>(); |
14443
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2798 |
for (MethodSymbol m1 : methods) { |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2799 |
boolean isMin_m1 = true; |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2800 |
for (MethodSymbol m2 : methods) { |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2801 |
if (m1 == m2) continue; |
16295
8b76e466f3cb
8007535: Compiler crashes on @FunctionalInterface used on interface with two inherited methods with same signatures
mcimadamore
parents:
16292
diff
changeset
|
2802 |
if (m2.owner != m1.owner && |
8b76e466f3cb
8007535: Compiler crashes on @FunctionalInterface used on interface with two inherited methods with same signatures
mcimadamore
parents:
16292
diff
changeset
|
2803 |
asSuper(m2.owner.type, m1.owner) != null) { |
14443
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2804 |
isMin_m1 = false; |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2805 |
break; |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2806 |
} |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2807 |
} |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2808 |
if (isMin_m1) |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2809 |
methodsMin.append(m1); |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2810 |
} |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2811 |
return methodsMin.toList(); |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2812 |
} |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2813 |
// where |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2814 |
private class MethodFilter implements Filter<Symbol> { |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2815 |
|
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2816 |
Symbol msym; |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2817 |
Type site; |
14547 | 2818 |
|
2819 |
MethodFilter(Symbol msym, Type site) { |
|
14443
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2820 |
this.msym = msym; |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2821 |
this.site = site; |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2822 |
} |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2823 |
|
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2824 |
public boolean accepts(Symbol s) { |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2825 |
return s.kind == Kinds.MTH && |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2826 |
s.name == msym.name && |
18730
95354d510139
8016281: The SAM method should be passed to the metafactory as a MethodType not a MethodHandle
mcimadamore
parents:
18662
diff
changeset
|
2827 |
(s.flags() & SYNTHETIC) == 0 && |
14443
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2828 |
s.isInheritedIn(site.tsym, Types.this) && |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2829 |
overrideEquivalent(memberType(site, s), memberType(site, msym)); |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2830 |
} |
22163 | 2831 |
} |
14443
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2832 |
// </editor-fold> |
91c05eb19277
7192246: Add type-checking support for default methods
mcimadamore
parents:
14359
diff
changeset
|
2833 |
|
10 | 2834 |
/** |
2835 |
* Does t have the same arguments as s? It is assumed that both |
|
2836 |
* types are (possibly polymorphic) method types. Monomorphic |
|
2837 |
* method types "have the same arguments", if their argument lists |
|
2838 |
* are equal. Polymorphic method types "have the same arguments", |
|
2839 |
* if they have the same arguments after renaming all type |
|
2840 |
* variables of one to corresponding type variables in the other, |
|
2841 |
* where correspondence is by position in the type parameter list. |
|
2842 |
*/ |
|
2843 |
public boolean hasSameArgs(Type t, Type s) { |
|
8628
9e31f1c01bd4
7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents:
8625
diff
changeset
|
2844 |
return hasSameArgs(t, s, true); |
9e31f1c01bd4
7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents:
8625
diff
changeset
|
2845 |
} |
9e31f1c01bd4
7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents:
8625
diff
changeset
|
2846 |
|
9e31f1c01bd4
7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents:
8625
diff
changeset
|
2847 |
public boolean hasSameArgs(Type t, Type s, boolean strict) { |
9e31f1c01bd4
7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents:
8625
diff
changeset
|
2848 |
return hasSameArgs(t, s, strict ? hasSameArgs_strict : hasSameArgs_nonstrict); |
9e31f1c01bd4
7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents:
8625
diff
changeset
|
2849 |
} |
9e31f1c01bd4
7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents:
8625
diff
changeset
|
2850 |
|
9e31f1c01bd4
7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents:
8625
diff
changeset
|
2851 |
private boolean hasSameArgs(Type t, Type s, TypeRelation hasSameArgs) { |
10 | 2852 |
return hasSameArgs.visit(t, s); |
2853 |
} |
|
2854 |
// where |
|
8628
9e31f1c01bd4
7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents:
8625
diff
changeset
|
2855 |
private class HasSameArgs extends TypeRelation { |
9e31f1c01bd4
7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents:
8625
diff
changeset
|
2856 |
|
9e31f1c01bd4
7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents:
8625
diff
changeset
|
2857 |
boolean strict; |
9e31f1c01bd4
7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents:
8625
diff
changeset
|
2858 |
|
9e31f1c01bd4
7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents:
8625
diff
changeset
|
2859 |
public HasSameArgs(boolean strict) { |
9e31f1c01bd4
7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents:
8625
diff
changeset
|
2860 |
this.strict = strict; |
9e31f1c01bd4
7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents:
8625
diff
changeset
|
2861 |
} |
10 | 2862 |
|
2863 |
public Boolean visitType(Type t, Type s) { |
|
2864 |
throw new AssertionError(); |
|
2865 |
} |
|
2866 |
||
2867 |
@Override |
|
2868 |
public Boolean visitMethodType(MethodType t, Type s) { |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
2869 |
return s.hasTag(METHOD) |
10 | 2870 |
&& containsTypeEquivalent(t.argtypes, s.getParameterTypes()); |
2871 |
} |
|
2872 |
||
2873 |
@Override |
|
2874 |
public Boolean visitForAll(ForAll t, Type s) { |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
2875 |
if (!s.hasTag(FORALL)) |
8628
9e31f1c01bd4
7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents:
8625
diff
changeset
|
2876 |
return strict ? false : visitMethodType(t.asMethodType(), s); |
10 | 2877 |
|
2878 |
ForAll forAll = (ForAll)s; |
|
2879 |
return hasSameBounds(t, forAll) |
|
2880 |
&& visit(t.qtype, subst(forAll.qtype, forAll.tvars, t.tvars)); |
|
2881 |
} |
|
2882 |
||
2883 |
@Override |
|
2884 |
public Boolean visitErrorType(ErrorType t, Type s) { |
|
2885 |
return false; |
|
2886 |
} |
|
22163 | 2887 |
} |
2888 |
||
2889 |
TypeRelation hasSameArgs_strict = new HasSameArgs(true); |
|
8628
9e31f1c01bd4
7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents:
8625
diff
changeset
|
2890 |
TypeRelation hasSameArgs_nonstrict = new HasSameArgs(false); |
9e31f1c01bd4
7022054: Invalid compiler error on covariant overriding methods with the same erasure
mcimadamore
parents:
8625
diff
changeset
|
2891 |
|
10 | 2892 |
// </editor-fold> |
2893 |
||
2894 |
// <editor-fold defaultstate="collapsed" desc="subst"> |
|
2895 |
public List<Type> subst(List<Type> ts, |
|
2896 |
List<Type> from, |
|
2897 |
List<Type> to) { |
|
2898 |
return new Subst(from, to).subst(ts); |
|
2899 |
} |
|
2900 |
||
2901 |
/** |
|
2902 |
* Substitute all occurrences of a type in `from' with the |
|
2903 |
* corresponding type in `to' in 't'. Match lists `from' and `to' |
|
2904 |
* from the right: If lists have different length, discard leading |
|
2905 |
* elements of the longer list. |
|
2906 |
*/ |
|
2907 |
public Type subst(Type t, List<Type> from, List<Type> to) { |
|
2908 |
return new Subst(from, to).subst(t); |
|
2909 |
} |
|
2910 |
||
2911 |
private class Subst extends UnaryVisitor<Type> { |
|
2912 |
List<Type> from; |
|
2913 |
List<Type> to; |
|
2914 |
||
2915 |
public Subst(List<Type> from, List<Type> to) { |
|
2916 |
int fromLength = from.length(); |
|
2917 |
int toLength = to.length(); |
|
2918 |
while (fromLength > toLength) { |
|
2919 |
fromLength--; |
|
2920 |
from = from.tail; |
|
2921 |
} |
|
2922 |
while (fromLength < toLength) { |
|
2923 |
toLength--; |
|
2924 |
to = to.tail; |
|
2925 |
} |
|
2926 |
this.from = from; |
|
2927 |
this.to = to; |
|
2928 |
} |
|
2929 |
||
2930 |
Type subst(Type t) { |
|
2931 |
if (from.tail == null) |
|
2932 |
return t; |
|
2933 |
else |
|
2934 |
return visit(t); |
|
2221
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2218
diff
changeset
|
2935 |
} |
10 | 2936 |
|
2937 |
List<Type> subst(List<Type> ts) { |
|
2938 |
if (from.tail == null) |
|
2939 |
return ts; |
|
2940 |
boolean wild = false; |
|
2941 |
if (ts.nonEmpty() && from.nonEmpty()) { |
|
2942 |
Type head1 = subst(ts.head); |
|
2943 |
List<Type> tail1 = subst(ts.tail); |
|
2944 |
if (head1 != ts.head || tail1 != ts.tail) |
|
2945 |
return tail1.prepend(head1); |
|
2946 |
} |
|
2947 |
return ts; |
|
2948 |
} |
|
2949 |
||
2950 |
public Type visitType(Type t, Void ignored) { |
|
2951 |
return t; |
|
2952 |
} |
|
2953 |
||
2954 |
@Override |
|
2955 |
public Type visitMethodType(MethodType t, Void ignored) { |
|
2956 |
List<Type> argtypes = subst(t.argtypes); |
|
2957 |
Type restype = subst(t.restype); |
|
2958 |
List<Type> thrown = subst(t.thrown); |
|
2959 |
if (argtypes == t.argtypes && |
|
2960 |
restype == t.restype && |
|
2961 |
thrown == t.thrown) |
|
2962 |
return t; |
|
2963 |
else |
|
2964 |
return new MethodType(argtypes, restype, thrown, t.tsym); |
|
2965 |
} |
|
2966 |
||
2967 |
@Override |
|
2968 |
public Type visitTypeVar(TypeVar t, Void ignored) { |
|
2969 |
for (List<Type> from = this.from, to = this.to; |
|
2970 |
from.nonEmpty(); |
|
2971 |
from = from.tail, to = to.tail) { |
|
2972 |
if (t == from.head) { |
|
2973 |
return to.head.withTypeVar(t); |
|
2974 |
} |
|
2975 |
} |
|
2976 |
return t; |
|
2977 |
} |
|
2978 |
||
2979 |
@Override |
|
2980 |
public Type visitClassType(ClassType t, Void ignored) { |
|
2981 |
if (!t.isCompound()) { |
|
2982 |
List<Type> typarams = t.getTypeArguments(); |
|
2983 |
List<Type> typarams1 = subst(typarams); |
|
2984 |
Type outer = t.getEnclosingType(); |
|
2985 |
Type outer1 = subst(outer); |
|
2986 |
if (typarams1 == typarams && outer1 == outer) |
|
2987 |
return t; |
|
2988 |
else |
|
24396 | 2989 |
return new ClassType(outer1, typarams1, t.tsym, |
2990 |
t.getAnnotationMirrors()); |
|
10 | 2991 |
} else { |
2992 |
Type st = subst(supertype(t)); |
|
2993 |
List<Type> is = upperBounds(subst(interfaces(t))); |
|
2994 |
if (st == supertype(t) && is == interfaces(t)) |
|
2995 |
return t; |
|
2996 |
else |
|
2997 |
return makeCompoundType(is.prepend(st)); |
|
2998 |
} |
|
2999 |
} |
|
3000 |
||
3001 |
@Override |
|
3002 |
public Type visitWildcardType(WildcardType t, Void ignored) { |
|
3003 |
Type bound = t.type; |
|
3004 |
if (t.kind != BoundKind.UNBOUND) |
|
3005 |
bound = subst(bound); |
|
3006 |
if (bound == t.type) { |
|
3007 |
return t; |
|
3008 |
} else { |
|
3009 |
if (t.isExtendsBound() && bound.isExtendsBound()) |
|
3010 |
bound = upperBound(bound); |
|
24396 | 3011 |
return new WildcardType(bound, t.kind, syms.boundClass, |
3012 |
t.bound, t.getAnnotationMirrors()); |
|
10 | 3013 |
} |
3014 |
} |
|
3015 |
||
3016 |
@Override |
|
3017 |
public Type visitArrayType(ArrayType t, Void ignored) { |
|
3018 |
Type elemtype = subst(t.elemtype); |
|
3019 |
if (elemtype == t.elemtype) |
|
3020 |
return t; |
|
3021 |
else |
|
24396 | 3022 |
return new ArrayType(elemtype, t.tsym, t.getAnnotationMirrors()); |
10 | 3023 |
} |
3024 |
||
3025 |
@Override |
|
3026 |
public Type visitForAll(ForAll t, Void ignored) { |
|
8230
b761a23e78d8
6838943: inference: javac is not handling type-variable substitution properly
mcimadamore
parents:
8032
diff
changeset
|
3027 |
if (Type.containsAny(to, t.tvars)) { |
b761a23e78d8
6838943: inference: javac is not handling type-variable substitution properly
mcimadamore
parents:
8032
diff
changeset
|
3028 |
//perform alpha-renaming of free-variables in 't' |
b761a23e78d8
6838943: inference: javac is not handling type-variable substitution properly
mcimadamore
parents:
8032
diff
changeset
|
3029 |
//if 'to' types contain variables that are free in 't' |
b761a23e78d8
6838943: inference: javac is not handling type-variable substitution properly
mcimadamore
parents:
8032
diff
changeset
|
3030 |
List<Type> freevars = newInstances(t.tvars); |
b761a23e78d8
6838943: inference: javac is not handling type-variable substitution properly
mcimadamore
parents:
8032
diff
changeset
|
3031 |
t = new ForAll(freevars, |
24396 | 3032 |
Types.this.subst(t.qtype, t.tvars, freevars)); |
8230
b761a23e78d8
6838943: inference: javac is not handling type-variable substitution properly
mcimadamore
parents:
8032
diff
changeset
|
3033 |
} |
10 | 3034 |
List<Type> tvars1 = substBounds(t.tvars, from, to); |
3035 |
Type qtype1 = subst(t.qtype); |
|
3036 |
if (tvars1 == t.tvars && qtype1 == t.qtype) { |
|
3037 |
return t; |
|
3038 |
} else if (tvars1 == t.tvars) { |
|
3039 |
return new ForAll(tvars1, qtype1); |
|
3040 |
} else { |
|
24396 | 3041 |
return new ForAll(tvars1, |
3042 |
Types.this.subst(qtype1, t.tvars, tvars1)); |
|
10 | 3043 |
} |
3044 |
} |
|
3045 |
||
3046 |
@Override |
|
3047 |
public Type visitErrorType(ErrorType t, Void ignored) { |
|
3048 |
return t; |
|
3049 |
} |
|
3050 |
} |
|
3051 |
||
3052 |
public List<Type> substBounds(List<Type> tvars, |
|
3053 |
List<Type> from, |
|
3054 |
List<Type> to) { |
|
3055 |
if (tvars.isEmpty()) |
|
3056 |
return tvars; |
|
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19914
diff
changeset
|
3057 |
ListBuffer<Type> newBoundsBuf = new ListBuffer<>(); |
10 | 3058 |
boolean changed = false; |
3059 |
// calculate new bounds |
|
3060 |
for (Type t : tvars) { |
|
3061 |
TypeVar tv = (TypeVar) t; |
|
3062 |
Type bound = subst(tv.bound, from, to); |
|
3063 |
if (bound != tv.bound) |
|
3064 |
changed = true; |
|
3065 |
newBoundsBuf.append(bound); |
|
3066 |
} |
|
3067 |
if (!changed) |
|
3068 |
return tvars; |
|
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19914
diff
changeset
|
3069 |
ListBuffer<Type> newTvars = new ListBuffer<>(); |
10 | 3070 |
// create new type variables without bounds |
3071 |
for (Type t : tvars) { |
|
24396 | 3072 |
newTvars.append(new TypeVar(t.tsym, null, syms.botType, |
3073 |
t.getAnnotationMirrors())); |
|
10 | 3074 |
} |
3075 |
// the new bounds should use the new type variables in place |
|
3076 |
// of the old |
|
3077 |
List<Type> newBounds = newBoundsBuf.toList(); |
|
3078 |
from = tvars; |
|
3079 |
to = newTvars.toList(); |
|
3080 |
for (; !newBounds.isEmpty(); newBounds = newBounds.tail) { |
|
3081 |
newBounds.head = subst(newBounds.head, from, to); |
|
3082 |
} |
|
3083 |
newBounds = newBoundsBuf.toList(); |
|
3084 |
// set the bounds of new type variables to the new bounds |
|
3085 |
for (Type t : newTvars.toList()) { |
|
3086 |
TypeVar tv = (TypeVar) t; |
|
3087 |
tv.bound = newBounds.head; |
|
3088 |
newBounds = newBounds.tail; |
|
3089 |
} |
|
3090 |
return newTvars.toList(); |
|
3091 |
} |
|
3092 |
||
3093 |
public TypeVar substBound(TypeVar t, List<Type> from, List<Type> to) { |
|
3094 |
Type bound1 = subst(t.bound, from, to); |
|
3095 |
if (bound1 == t.bound) |
|
3096 |
return t; |
|
1993
9b37ef07ba64
6729401: Compiler error when using F-bounded generics with free type variables
mcimadamore
parents:
1992
diff
changeset
|
3097 |
else { |
9b37ef07ba64
6729401: Compiler error when using F-bounded generics with free type variables
mcimadamore
parents:
1992
diff
changeset
|
3098 |
// create new type variable without bounds |
24396 | 3099 |
TypeVar tv = new TypeVar(t.tsym, null, syms.botType, |
3100 |
t.getAnnotationMirrors()); |
|
1993
9b37ef07ba64
6729401: Compiler error when using F-bounded generics with free type variables
mcimadamore
parents:
1992
diff
changeset
|
3101 |
// the new bound should use the new type variable in place |
9b37ef07ba64
6729401: Compiler error when using F-bounded generics with free type variables
mcimadamore
parents:
1992
diff
changeset
|
3102 |
// of the old |
9b37ef07ba64
6729401: Compiler error when using F-bounded generics with free type variables
mcimadamore
parents:
1992
diff
changeset
|
3103 |
tv.bound = subst(bound1, List.<Type>of(t), List.<Type>of(tv)); |
9b37ef07ba64
6729401: Compiler error when using F-bounded generics with free type variables
mcimadamore
parents:
1992
diff
changeset
|
3104 |
return tv; |
9b37ef07ba64
6729401: Compiler error when using F-bounded generics with free type variables
mcimadamore
parents:
1992
diff
changeset
|
3105 |
} |
10 | 3106 |
} |
3107 |
// </editor-fold> |
|
3108 |
||
3109 |
// <editor-fold defaultstate="collapsed" desc="hasSameBounds"> |
|
3110 |
/** |
|
3111 |
* Does t have the same bounds for quantified variables as s? |
|
3112 |
*/ |
|
19914 | 3113 |
public boolean hasSameBounds(ForAll t, ForAll s) { |
10 | 3114 |
List<Type> l1 = t.tvars; |
3115 |
List<Type> l2 = s.tvars; |
|
3116 |
while (l1.nonEmpty() && l2.nonEmpty() && |
|
3117 |
isSameType(l1.head.getUpperBound(), |
|
3118 |
subst(l2.head.getUpperBound(), |
|
3119 |
s.tvars, |
|
3120 |
t.tvars))) { |
|
3121 |
l1 = l1.tail; |
|
3122 |
l2 = l2.tail; |
|
3123 |
} |
|
3124 |
return l1.isEmpty() && l2.isEmpty(); |
|
3125 |
} |
|
3126 |
// </editor-fold> |
|
3127 |
||
3128 |
// <editor-fold defaultstate="collapsed" desc="newInstances"> |
|
3129 |
/** Create new vector of type variables from list of variables |
|
3130 |
* changing all recursive bounds from old to new list. |
|
3131 |
*/ |
|
3132 |
public List<Type> newInstances(List<Type> tvars) { |
|
3133 |
List<Type> tvars1 = Type.map(tvars, newInstanceFun); |
|
3134 |
for (List<Type> l = tvars1; l.nonEmpty(); l = l.tail) { |
|
3135 |
TypeVar tv = (TypeVar) l.head; |
|
3136 |
tv.bound = subst(tv.bound, tvars, tvars1); |
|
3137 |
} |
|
3138 |
return tvars1; |
|
3139 |
} |
|
14801
d66cab4ef397
8003967: detect and remove all mutable implicit static enum fields in langtools
vromero
parents:
14725
diff
changeset
|
3140 |
private static final Mapping newInstanceFun = new Mapping("newInstanceFun") { |
24396 | 3141 |
public Type apply(Type t) { return new TypeVar(t.tsym, t.getUpperBound(), t.getLowerBound(), t.getAnnotationMirrors()); } |
10 | 3142 |
}; |
3143 |
// </editor-fold> |
|
3144 |
||
8430 | 3145 |
public Type createMethodTypeWithParameters(Type original, List<Type> newParams) { |
3146 |
return original.accept(methodWithParameters, newParams); |
|
3147 |
} |
|
3148 |
// where |
|
3149 |
private final MapVisitor<List<Type>> methodWithParameters = new MapVisitor<List<Type>>() { |
|
3150 |
public Type visitType(Type t, List<Type> newParams) { |
|
3151 |
throw new IllegalArgumentException("Not a method type: " + t); |
|
3152 |
} |
|
3153 |
public Type visitMethodType(MethodType t, List<Type> newParams) { |
|
3154 |
return new MethodType(newParams, t.restype, t.thrown, t.tsym); |
|
3155 |
} |
|
3156 |
public Type visitForAll(ForAll t, List<Type> newParams) { |
|
3157 |
return new ForAll(t.tvars, t.qtype.accept(this, newParams)); |
|
3158 |
} |
|
3159 |
}; |
|
3160 |
||
3161 |
public Type createMethodTypeWithThrown(Type original, List<Type> newThrown) { |
|
3162 |
return original.accept(methodWithThrown, newThrown); |
|
3163 |
} |
|
3164 |
// where |
|
3165 |
private final MapVisitor<List<Type>> methodWithThrown = new MapVisitor<List<Type>>() { |
|
3166 |
public Type visitType(Type t, List<Type> newThrown) { |
|
3167 |
throw new IllegalArgumentException("Not a method type: " + t); |
|
3168 |
} |
|
3169 |
public Type visitMethodType(MethodType t, List<Type> newThrown) { |
|
3170 |
return new MethodType(t.argtypes, t.restype, newThrown, t.tsym); |
|
3171 |
} |
|
3172 |
public Type visitForAll(ForAll t, List<Type> newThrown) { |
|
3173 |
return new ForAll(t.tvars, t.qtype.accept(this, newThrown)); |
|
3174 |
} |
|
3175 |
}; |
|
3176 |
||
9075
cba34854a40e
7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents:
8628
diff
changeset
|
3177 |
public Type createMethodTypeWithReturn(Type original, Type newReturn) { |
cba34854a40e
7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents:
8628
diff
changeset
|
3178 |
return original.accept(methodWithReturn, newReturn); |
cba34854a40e
7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents:
8628
diff
changeset
|
3179 |
} |
cba34854a40e
7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents:
8628
diff
changeset
|
3180 |
// where |
cba34854a40e
7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents:
8628
diff
changeset
|
3181 |
private final MapVisitor<Type> methodWithReturn = new MapVisitor<Type>() { |
cba34854a40e
7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents:
8628
diff
changeset
|
3182 |
public Type visitType(Type t, Type newReturn) { |
cba34854a40e
7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents:
8628
diff
changeset
|
3183 |
throw new IllegalArgumentException("Not a method type: " + t); |
cba34854a40e
7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents:
8628
diff
changeset
|
3184 |
} |
cba34854a40e
7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents:
8628
diff
changeset
|
3185 |
public Type visitMethodType(MethodType t, Type newReturn) { |
cba34854a40e
7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents:
8628
diff
changeset
|
3186 |
return new MethodType(t.argtypes, newReturn, t.thrown, t.tsym); |
cba34854a40e
7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents:
8628
diff
changeset
|
3187 |
} |
cba34854a40e
7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents:
8628
diff
changeset
|
3188 |
public Type visitForAll(ForAll t, Type newReturn) { |
cba34854a40e
7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents:
8628
diff
changeset
|
3189 |
return new ForAll(t.tvars, t.qtype.accept(this, newReturn)); |
cba34854a40e
7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents:
8628
diff
changeset
|
3190 |
} |
cba34854a40e
7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents:
8628
diff
changeset
|
3191 |
}; |
cba34854a40e
7030150: Type inference for generic instance creation failed for formal type parameter
mcimadamore
parents:
8628
diff
changeset
|
3192 |
|
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
|
3193 |
// <editor-fold defaultstate="collapsed" desc="createErrorType"> |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1206
diff
changeset
|
3194 |
public Type createErrorType(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
|
3195 |
return new ErrorType(originalType, syms.errSymbol); |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1206
diff
changeset
|
3196 |
} |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1206
diff
changeset
|
3197 |
|
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1206
diff
changeset
|
3198 |
public Type createErrorType(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
|
3199 |
return new ErrorType(c, 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
|
3200 |
} |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1206
diff
changeset
|
3201 |
|
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1206
diff
changeset
|
3202 |
public Type createErrorType(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
|
3203 |
return new ErrorType(name, container, 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
|
3204 |
} |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1206
diff
changeset
|
3205 |
// </editor-fold> |
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1206
diff
changeset
|
3206 |
|
10 | 3207 |
// <editor-fold defaultstate="collapsed" desc="rank"> |
3208 |
/** |
|
3209 |
* The rank of a class is the length of the longest path between |
|
3210 |
* the class and java.lang.Object in the class inheritance |
|
3211 |
* graph. Undefined for all but reference types. |
|
3212 |
*/ |
|
3213 |
public int rank(Type t) { |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
3214 |
switch(t.getTag()) { |
10 | 3215 |
case CLASS: { |
3216 |
ClassType cls = (ClassType)t; |
|
3217 |
if (cls.rank_field < 0) { |
|
3218 |
Name fullname = cls.tsym.getQualifiedName(); |
|
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
1257
diff
changeset
|
3219 |
if (fullname == names.java_lang_Object) |
10 | 3220 |
cls.rank_field = 0; |
3221 |
else { |
|
3222 |
int r = rank(supertype(cls)); |
|
3223 |
for (List<Type> l = interfaces(cls); |
|
3224 |
l.nonEmpty(); |
|
3225 |
l = l.tail) { |
|
3226 |
if (rank(l.head) > r) |
|
3227 |
r = rank(l.head); |
|
3228 |
} |
|
3229 |
cls.rank_field = r + 1; |
|
3230 |
} |
|
3231 |
} |
|
3232 |
return cls.rank_field; |
|
3233 |
} |
|
3234 |
case TYPEVAR: { |
|
3235 |
TypeVar tvar = (TypeVar)t; |
|
3236 |
if (tvar.rank_field < 0) { |
|
3237 |
int r = rank(supertype(tvar)); |
|
3238 |
for (List<Type> l = interfaces(tvar); |
|
3239 |
l.nonEmpty(); |
|
3240 |
l = l.tail) { |
|
3241 |
if (rank(l.head) > r) r = rank(l.head); |
|
3242 |
} |
|
3243 |
tvar.rank_field = r + 1; |
|
3244 |
} |
|
3245 |
return tvar.rank_field; |
|
3246 |
} |
|
3247 |
case ERROR: |
|
23399
a7f649f8e860
8036007: javac crashes when encountering an unresolvable interface
vromero
parents:
22442
diff
changeset
|
3248 |
case NONE: |
10 | 3249 |
return 0; |
3250 |
default: |
|
3251 |
throw new AssertionError(); |
|
3252 |
} |
|
3253 |
} |
|
3254 |
// </editor-fold> |
|
3255 |
||
1357 | 3256 |
/** |
2221
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2218
diff
changeset
|
3257 |
* Helper method for generating a string representation of a given type |
1357 | 3258 |
* accordingly to a given locale |
3259 |
*/ |
|
3260 |
public String toString(Type t, Locale locale) { |
|
2221
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2218
diff
changeset
|
3261 |
return Printer.createStandardPrinter(messages).visit(t, locale); |
1357 | 3262 |
} |
3263 |
||
3264 |
/** |
|
2221
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2218
diff
changeset
|
3265 |
* Helper method for generating a string representation of a given type |
1357 | 3266 |
* accordingly to a given locale |
3267 |
*/ |
|
3268 |
public String toString(Symbol t, Locale locale) { |
|
2221
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2218
diff
changeset
|
3269 |
return Printer.createStandardPrinter(messages).visit(t, locale); |
1357 | 3270 |
} |
3271 |
||
10 | 3272 |
// <editor-fold defaultstate="collapsed" desc="toString"> |
3273 |
/** |
|
3274 |
* This toString is slightly more descriptive than the one on Type. |
|
1357 | 3275 |
* |
3276 |
* @deprecated Types.toString(Type t, Locale l) provides better support |
|
3277 |
* for localization |
|
10 | 3278 |
*/ |
1357 | 3279 |
@Deprecated |
10 | 3280 |
public String toString(Type t) { |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
3281 |
if (t.hasTag(FORALL)) { |
10 | 3282 |
ForAll forAll = (ForAll)t; |
3283 |
return typaramsString(forAll.tvars) + forAll.qtype; |
|
3284 |
} |
|
3285 |
return "" + t; |
|
3286 |
} |
|
3287 |
// where |
|
3288 |
private String typaramsString(List<Type> tvars) { |
|
8625
6b51ef804d49
6639645: Modeling type implementing missing interfaces
jjg
parents:
8617
diff
changeset
|
3289 |
StringBuilder s = new StringBuilder(); |
10 | 3290 |
s.append('<'); |
3291 |
boolean first = true; |
|
3292 |
for (Type t : tvars) { |
|
3293 |
if (!first) s.append(", "); |
|
3294 |
first = false; |
|
24396 | 3295 |
appendTyparamString(((TypeVar)t), s); |
10 | 3296 |
} |
3297 |
s.append('>'); |
|
3298 |
return s.toString(); |
|
3299 |
} |
|
8625
6b51ef804d49
6639645: Modeling type implementing missing interfaces
jjg
parents:
8617
diff
changeset
|
3300 |
private void appendTyparamString(TypeVar t, StringBuilder buf) { |
10 | 3301 |
buf.append(t); |
3302 |
if (t.bound == null || |
|
3303 |
t.bound.tsym.getQualifiedName() == names.java_lang_Object) |
|
3304 |
return; |
|
3305 |
buf.append(" extends "); // Java syntax; no need for i18n |
|
3306 |
Type bound = t.bound; |
|
3307 |
if (!bound.isCompound()) { |
|
3308 |
buf.append(bound); |
|
3309 |
} else if ((erasure(t).tsym.flags() & INTERFACE) == 0) { |
|
3310 |
buf.append(supertype(t)); |
|
3311 |
for (Type intf : interfaces(t)) { |
|
3312 |
buf.append('&'); |
|
3313 |
buf.append(intf); |
|
3314 |
} |
|
3315 |
} else { |
|
3316 |
// No superclass was given in bounds. |
|
3317 |
// In this case, supertype is Object, erasure is first interface. |
|
3318 |
boolean first = true; |
|
3319 |
for (Type intf : interfaces(t)) { |
|
3320 |
if (!first) buf.append('&'); |
|
3321 |
first = false; |
|
3322 |
buf.append(intf); |
|
3323 |
} |
|
3324 |
} |
|
3325 |
} |
|
3326 |
// </editor-fold> |
|
3327 |
||
3328 |
// <editor-fold defaultstate="collapsed" desc="Determining least upper bounds of types"> |
|
3329 |
/** |
|
3330 |
* A cache for closures. |
|
3331 |
* |
|
3332 |
* <p>A closure is a list of all the supertypes and interfaces of |
|
3333 |
* a class or interface type, ordered by ClassSymbol.precedes |
|
3334 |
* (that is, subclasses come first, arbitrary but fixed |
|
3335 |
* otherwise). |
|
3336 |
*/ |
|
22163 | 3337 |
private Map<Type,List<Type>> closureCache = new HashMap<>(); |
10 | 3338 |
|
3339 |
/** |
|
3340 |
* Returns the closure of a class or interface type. |
|
3341 |
*/ |
|
3342 |
public List<Type> closure(Type t) { |
|
3343 |
List<Type> cl = closureCache.get(t); |
|
3344 |
if (cl == null) { |
|
3345 |
Type st = supertype(t); |
|
3346 |
if (!t.isCompound()) { |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
3347 |
if (st.hasTag(CLASS)) { |
10 | 3348 |
cl = insert(closure(st), t); |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
3349 |
} else if (st.hasTag(TYPEVAR)) { |
10 | 3350 |
cl = closure(st).prepend(t); |
3351 |
} else { |
|
3352 |
cl = List.of(t); |
|
3353 |
} |
|
3354 |
} else { |
|
3355 |
cl = closure(supertype(t)); |
|
3356 |
} |
|
3357 |
for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail) |
|
3358 |
cl = union(cl, closure(l.head)); |
|
3359 |
closureCache.put(t, cl); |
|
3360 |
} |
|
3361 |
return cl; |
|
3362 |
} |
|
3363 |
||
3364 |
/** |
|
3365 |
* Insert a type in a closure |
|
3366 |
*/ |
|
3367 |
public List<Type> insert(List<Type> cl, Type t) { |
|
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3368 |
if (cl.isEmpty()) { |
10 | 3369 |
return cl.prepend(t); |
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3370 |
} else if (t.tsym == cl.head.tsym) { |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3371 |
return cl; |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3372 |
} else if (t.tsym.precedes(cl.head.tsym, this)) { |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3373 |
return cl.prepend(t); |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3374 |
} else { |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3375 |
// t comes after head, or the two are unrelated |
10 | 3376 |
return insert(cl.tail, t).prepend(cl.head); |
3377 |
} |
|
3378 |
} |
|
3379 |
||
3380 |
/** |
|
3381 |
* Form the union of two closures |
|
3382 |
*/ |
|
3383 |
public List<Type> union(List<Type> cl1, List<Type> cl2) { |
|
3384 |
if (cl1.isEmpty()) { |
|
3385 |
return cl2; |
|
3386 |
} else if (cl2.isEmpty()) { |
|
3387 |
return cl1; |
|
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3388 |
} else if (cl1.head.tsym == cl2.head.tsym) { |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3389 |
return union(cl1.tail, cl2.tail).prepend(cl1.head); |
10 | 3390 |
} else if (cl1.head.tsym.precedes(cl2.head.tsym, this)) { |
3391 |
return union(cl1.tail, cl2).prepend(cl1.head); |
|
3392 |
} else if (cl2.head.tsym.precedes(cl1.head.tsym, this)) { |
|
3393 |
return union(cl1, cl2.tail).prepend(cl2.head); |
|
3394 |
} else { |
|
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3395 |
// unrelated types |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3396 |
return union(cl1.tail, cl2).prepend(cl1.head); |
10 | 3397 |
} |
3398 |
} |
|
3399 |
||
3400 |
/** |
|
3401 |
* Intersect two closures |
|
3402 |
*/ |
|
3403 |
public List<Type> intersect(List<Type> cl1, List<Type> cl2) { |
|
3404 |
if (cl1 == cl2) |
|
3405 |
return cl1; |
|
3406 |
if (cl1.isEmpty() || cl2.isEmpty()) |
|
3407 |
return List.nil(); |
|
3408 |
if (cl1.head.tsym.precedes(cl2.head.tsym, this)) |
|
3409 |
return intersect(cl1.tail, cl2); |
|
3410 |
if (cl2.head.tsym.precedes(cl1.head.tsym, this)) |
|
3411 |
return intersect(cl1, cl2.tail); |
|
3412 |
if (isSameType(cl1.head, cl2.head)) |
|
3413 |
return intersect(cl1.tail, cl2.tail).prepend(cl1.head); |
|
3414 |
if (cl1.head.tsym == cl2.head.tsym && |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
3415 |
cl1.head.hasTag(CLASS) && cl2.head.hasTag(CLASS)) { |
10 | 3416 |
if (cl1.head.isParameterized() && cl2.head.isParameterized()) { |
3417 |
Type merge = merge(cl1.head,cl2.head); |
|
3418 |
return intersect(cl1.tail, cl2.tail).prepend(merge); |
|
3419 |
} |
|
3420 |
if (cl1.head.isRaw() || cl2.head.isRaw()) |
|
3421 |
return intersect(cl1.tail, cl2.tail).prepend(erasure(cl1.head)); |
|
3422 |
} |
|
3423 |
return intersect(cl1.tail, cl2.tail); |
|
3424 |
} |
|
3425 |
// where |
|
3426 |
class TypePair { |
|
3427 |
final Type t1; |
|
3428 |
final Type t2; |
|
3429 |
TypePair(Type t1, Type t2) { |
|
3430 |
this.t1 = t1; |
|
3431 |
this.t2 = t2; |
|
3432 |
} |
|
3433 |
@Override |
|
3434 |
public int hashCode() { |
|
14949
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14801
diff
changeset
|
3435 |
return 127 * Types.this.hashCode(t1) + Types.this.hashCode(t2); |
10 | 3436 |
} |
3437 |
@Override |
|
3438 |
public boolean equals(Object obj) { |
|
3439 |
if (!(obj instanceof TypePair)) |
|
3440 |
return false; |
|
3441 |
TypePair typePair = (TypePair)obj; |
|
3442 |
return isSameType(t1, typePair.t1) |
|
3443 |
&& isSameType(t2, typePair.t2); |
|
3444 |
} |
|
3445 |
} |
|
22163 | 3446 |
Set<TypePair> mergeCache = new HashSet<>(); |
10 | 3447 |
private Type merge(Type c1, Type c2) { |
3448 |
ClassType class1 = (ClassType) c1; |
|
3449 |
List<Type> act1 = class1.getTypeArguments(); |
|
3450 |
ClassType class2 = (ClassType) c2; |
|
3451 |
List<Type> act2 = class2.getTypeArguments(); |
|
22163 | 3452 |
ListBuffer<Type> merged = new ListBuffer<>(); |
10 | 3453 |
List<Type> typarams = class1.tsym.type.getTypeArguments(); |
3454 |
||
3455 |
while (act1.nonEmpty() && act2.nonEmpty() && typarams.nonEmpty()) { |
|
3456 |
if (containsType(act1.head, act2.head)) { |
|
3457 |
merged.append(act1.head); |
|
3458 |
} else if (containsType(act2.head, act1.head)) { |
|
3459 |
merged.append(act2.head); |
|
3460 |
} else { |
|
3461 |
TypePair pair = new TypePair(c1, c2); |
|
3462 |
Type m; |
|
3463 |
if (mergeCache.add(pair)) { |
|
3464 |
m = new WildcardType(lub(upperBound(act1.head), |
|
3465 |
upperBound(act2.head)), |
|
3466 |
BoundKind.EXTENDS, |
|
24396 | 3467 |
syms.boundClass, |
3468 |
Type.noAnnotations); |
|
10 | 3469 |
mergeCache.remove(pair); |
3470 |
} else { |
|
3471 |
m = new WildcardType(syms.objectType, |
|
3472 |
BoundKind.UNBOUND, |
|
24396 | 3473 |
syms.boundClass, |
3474 |
Type.noAnnotations); |
|
10 | 3475 |
} |
3476 |
merged.append(m.withTypeVar(typarams.head)); |
|
3477 |
} |
|
3478 |
act1 = act1.tail; |
|
3479 |
act2 = act2.tail; |
|
3480 |
typarams = typarams.tail; |
|
3481 |
} |
|
8032 | 3482 |
Assert.check(act1.isEmpty() && act2.isEmpty() && typarams.isEmpty()); |
24396 | 3483 |
// There is no spec detailing how type annotations are to |
3484 |
// be inherited. So set it to noAnnotations for now |
|
3485 |
return new ClassType(class1.getEnclosingType(), merged.toList(), |
|
3486 |
class1.tsym, Type.noAnnotations); |
|
10 | 3487 |
} |
3488 |
||
3489 |
/** |
|
3490 |
* Return the minimum type of a closure, a compound type if no |
|
3491 |
* unique minimum exists. |
|
3492 |
*/ |
|
3493 |
private Type compoundMin(List<Type> cl) { |
|
3494 |
if (cl.isEmpty()) return syms.objectType; |
|
3495 |
List<Type> compound = closureMin(cl); |
|
3496 |
if (compound.isEmpty()) |
|
3497 |
return null; |
|
3498 |
else if (compound.tail.isEmpty()) |
|
3499 |
return compound.head; |
|
3500 |
else |
|
3501 |
return makeCompoundType(compound); |
|
3502 |
} |
|
3503 |
||
3504 |
/** |
|
3505 |
* Return the minimum types of a closure, suitable for computing |
|
3506 |
* compoundMin or glb. |
|
3507 |
*/ |
|
3508 |
private List<Type> closureMin(List<Type> cl) { |
|
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19914
diff
changeset
|
3509 |
ListBuffer<Type> classes = new ListBuffer<>(); |
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19914
diff
changeset
|
3510 |
ListBuffer<Type> interfaces = new ListBuffer<>(); |
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3511 |
Set<Type> toSkip = new HashSet<>(); |
10 | 3512 |
while (!cl.isEmpty()) { |
3513 |
Type current = cl.head; |
|
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3514 |
boolean keep = !toSkip.contains(current); |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3515 |
if (keep && current.hasTag(TYPEVAR)) { |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3516 |
// skip lower-bounded variables with a subtype in cl.tail |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3517 |
for (Type t : cl.tail) { |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3518 |
if (isSubtypeNoCapture(t, current)) { |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3519 |
keep = false; |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3520 |
break; |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3521 |
} |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3522 |
} |
10 | 3523 |
} |
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3524 |
if (keep) { |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3525 |
if (current.isInterface()) |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3526 |
interfaces.append(current); |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3527 |
else |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3528 |
classes.append(current); |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3529 |
for (Type t : cl.tail) { |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3530 |
// skip supertypes of 'current' in cl.tail |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3531 |
if (isSubtypeNoCapture(current, t)) |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3532 |
toSkip.add(t); |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3533 |
} |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3534 |
} |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3535 |
cl = cl.tail; |
10 | 3536 |
} |
3537 |
return classes.appendList(interfaces).toList(); |
|
3538 |
} |
|
3539 |
||
3540 |
/** |
|
3541 |
* Return the least upper bound of pair of types. if the lub does |
|
3542 |
* not exist return null. |
|
3543 |
*/ |
|
3544 |
public Type lub(Type t1, Type t2) { |
|
3545 |
return lub(List.of(t1, t2)); |
|
3546 |
} |
|
3547 |
||
3548 |
/** |
|
3549 |
* Return the least upper bound (lub) of set of types. If the lub |
|
3550 |
* does not exist return the type of null (bottom). |
|
3551 |
*/ |
|
3552 |
public Type lub(List<Type> ts) { |
|
3553 |
final int ARRAY_BOUND = 1; |
|
3554 |
final int CLASS_BOUND = 2; |
|
3555 |
int boundkind = 0; |
|
3556 |
for (Type t : ts) { |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
3557 |
switch (t.getTag()) { |
10 | 3558 |
case CLASS: |
3559 |
boundkind |= CLASS_BOUND; |
|
3560 |
break; |
|
3561 |
case ARRAY: |
|
3562 |
boundkind |= ARRAY_BOUND; |
|
3563 |
break; |
|
3564 |
case TYPEVAR: |
|
3565 |
do { |
|
3566 |
t = t.getUpperBound(); |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
3567 |
} while (t.hasTag(TYPEVAR)); |
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
3568 |
if (t.hasTag(ARRAY)) { |
10 | 3569 |
boundkind |= ARRAY_BOUND; |
3570 |
} else { |
|
3571 |
boundkind |= CLASS_BOUND; |
|
3572 |
} |
|
3573 |
break; |
|
3574 |
default: |
|
3575 |
if (t.isPrimitive()) |
|
162
6620f2a8e265
6611449: Internal Error thrown during generic method/constructor invocation
mcimadamore
parents:
10
diff
changeset
|
3576 |
return syms.errType; |
10 | 3577 |
} |
3578 |
} |
|
3579 |
switch (boundkind) { |
|
3580 |
case 0: |
|
3581 |
return syms.botType; |
|
3582 |
||
3583 |
case ARRAY_BOUND: |
|
3584 |
// calculate lub(A[], B[]) |
|
3585 |
List<Type> elements = Type.map(ts, elemTypeFun); |
|
3586 |
for (Type t : elements) { |
|
3587 |
if (t.isPrimitive()) { |
|
3588 |
// if a primitive type is found, then return |
|
3589 |
// arraySuperType unless all the types are the |
|
3590 |
// same |
|
3591 |
Type first = ts.head; |
|
3592 |
for (Type s : ts.tail) { |
|
3593 |
if (!isSameType(first, s)) { |
|
3594 |
// lub(int[], B[]) is Cloneable & Serializable |
|
3595 |
return arraySuperType(); |
|
3596 |
} |
|
3597 |
} |
|
3598 |
// all the array types are the same, return one |
|
3599 |
// lub(int[], int[]) is int[] |
|
3600 |
return first; |
|
3601 |
} |
|
3602 |
} |
|
3603 |
// lub(A[], B[]) is lub(A, B)[] |
|
24396 | 3604 |
return new ArrayType(lub(elements), syms.arrayClass, |
3605 |
Type.noAnnotations); |
|
10 | 3606 |
|
3607 |
case CLASS_BOUND: |
|
3608 |
// calculate lub(A, B) |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
3609 |
while (!ts.head.hasTag(CLASS) && !ts.head.hasTag(TYPEVAR)) { |
10 | 3610 |
ts = ts.tail; |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
3611 |
} |
8032 | 3612 |
Assert.check(!ts.isEmpty()); |
8617
6a05d1669247
7015715: lub gets stuck on type with complex supertype
mcimadamore
parents:
8430
diff
changeset
|
3613 |
//step 1 - compute erased candidate set (EC) |
6a05d1669247
7015715: lub gets stuck on type with complex supertype
mcimadamore
parents:
8430
diff
changeset
|
3614 |
List<Type> cl = erasedSupertypes(ts.head); |
10 | 3615 |
for (Type t : ts.tail) { |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
3616 |
if (t.hasTag(CLASS) || t.hasTag(TYPEVAR)) |
8617
6a05d1669247
7015715: lub gets stuck on type with complex supertype
mcimadamore
parents:
8430
diff
changeset
|
3617 |
cl = intersect(cl, erasedSupertypes(t)); |
10 | 3618 |
} |
8617
6a05d1669247
7015715: lub gets stuck on type with complex supertype
mcimadamore
parents:
8430
diff
changeset
|
3619 |
//step 2 - compute minimal erased candidate set (MEC) |
6a05d1669247
7015715: lub gets stuck on type with complex supertype
mcimadamore
parents:
8430
diff
changeset
|
3620 |
List<Type> mec = closureMin(cl); |
6a05d1669247
7015715: lub gets stuck on type with complex supertype
mcimadamore
parents:
8430
diff
changeset
|
3621 |
//step 3 - for each element G in MEC, compute lci(Inv(G)) |
6a05d1669247
7015715: lub gets stuck on type with complex supertype
mcimadamore
parents:
8430
diff
changeset
|
3622 |
List<Type> candidates = List.nil(); |
6a05d1669247
7015715: lub gets stuck on type with complex supertype
mcimadamore
parents:
8430
diff
changeset
|
3623 |
for (Type erasedSupertype : mec) { |
21712
d363f367d4c0
8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents:
21041
diff
changeset
|
3624 |
List<Type> lci = List.of(asSuper(ts.head, erasedSupertype.tsym)); |
8617
6a05d1669247
7015715: lub gets stuck on type with complex supertype
mcimadamore
parents:
8430
diff
changeset
|
3625 |
for (Type t : ts) { |
21712
d363f367d4c0
8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents:
21041
diff
changeset
|
3626 |
lci = intersect(lci, List.of(asSuper(t, erasedSupertype.tsym))); |
8617
6a05d1669247
7015715: lub gets stuck on type with complex supertype
mcimadamore
parents:
8430
diff
changeset
|
3627 |
} |
6a05d1669247
7015715: lub gets stuck on type with complex supertype
mcimadamore
parents:
8430
diff
changeset
|
3628 |
candidates = candidates.appendList(lci); |
6a05d1669247
7015715: lub gets stuck on type with complex supertype
mcimadamore
parents:
8430
diff
changeset
|
3629 |
} |
6a05d1669247
7015715: lub gets stuck on type with complex supertype
mcimadamore
parents:
8430
diff
changeset
|
3630 |
//step 4 - let MEC be { G1, G2 ... Gn }, then we have that |
6a05d1669247
7015715: lub gets stuck on type with complex supertype
mcimadamore
parents:
8430
diff
changeset
|
3631 |
//lub = lci(Inv(G1)) & lci(Inv(G2)) & ... & lci(Inv(Gn)) |
6a05d1669247
7015715: lub gets stuck on type with complex supertype
mcimadamore
parents:
8430
diff
changeset
|
3632 |
return compoundMin(candidates); |
10 | 3633 |
|
3634 |
default: |
|
3635 |
// calculate lub(A, B[]) |
|
3636 |
List<Type> classes = List.of(arraySuperType()); |
|
3637 |
for (Type t : ts) { |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
3638 |
if (!t.hasTag(ARRAY)) // Filter out any arrays |
10 | 3639 |
classes = classes.prepend(t); |
3640 |
} |
|
3641 |
// lub(A, B[]) is lub(A, arraySuperType) |
|
3642 |
return lub(classes); |
|
3643 |
} |
|
3644 |
} |
|
3645 |
// where |
|
8617
6a05d1669247
7015715: lub gets stuck on type with complex supertype
mcimadamore
parents:
8430
diff
changeset
|
3646 |
List<Type> erasedSupertypes(Type t) { |
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19914
diff
changeset
|
3647 |
ListBuffer<Type> buf = new ListBuffer<>(); |
8617
6a05d1669247
7015715: lub gets stuck on type with complex supertype
mcimadamore
parents:
8430
diff
changeset
|
3648 |
for (Type sup : closure(t)) { |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
3649 |
if (sup.hasTag(TYPEVAR)) { |
8617
6a05d1669247
7015715: lub gets stuck on type with complex supertype
mcimadamore
parents:
8430
diff
changeset
|
3650 |
buf.append(sup); |
6a05d1669247
7015715: lub gets stuck on type with complex supertype
mcimadamore
parents:
8430
diff
changeset
|
3651 |
} else { |
6a05d1669247
7015715: lub gets stuck on type with complex supertype
mcimadamore
parents:
8430
diff
changeset
|
3652 |
buf.append(erasure(sup)); |
6a05d1669247
7015715: lub gets stuck on type with complex supertype
mcimadamore
parents:
8430
diff
changeset
|
3653 |
} |
6a05d1669247
7015715: lub gets stuck on type with complex supertype
mcimadamore
parents:
8430
diff
changeset
|
3654 |
} |
6a05d1669247
7015715: lub gets stuck on type with complex supertype
mcimadamore
parents:
8430
diff
changeset
|
3655 |
return buf.toList(); |
6a05d1669247
7015715: lub gets stuck on type with complex supertype
mcimadamore
parents:
8430
diff
changeset
|
3656 |
} |
6a05d1669247
7015715: lub gets stuck on type with complex supertype
mcimadamore
parents:
8430
diff
changeset
|
3657 |
|
10 | 3658 |
private Type arraySuperType = null; |
3659 |
private Type arraySuperType() { |
|
3660 |
// initialized lazily to avoid problems during compiler startup |
|
3661 |
if (arraySuperType == null) { |
|
3662 |
synchronized (this) { |
|
3663 |
if (arraySuperType == null) { |
|
3664 |
// JLS 10.8: all arrays implement Cloneable and Serializable. |
|
3665 |
arraySuperType = makeCompoundType(List.of(syms.serializableType, |
|
14725
65836e833f59
8002099: Add support for intersection types in cast expression
mcimadamore
parents:
14723
diff
changeset
|
3666 |
syms.cloneableType), true); |
10 | 3667 |
} |
3668 |
} |
|
3669 |
} |
|
3670 |
return arraySuperType; |
|
3671 |
} |
|
3672 |
// </editor-fold> |
|
3673 |
||
3674 |
// <editor-fold defaultstate="collapsed" desc="Greatest lower bound"> |
|
1991
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1874
diff
changeset
|
3675 |
public Type glb(List<Type> ts) { |
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1874
diff
changeset
|
3676 |
Type t1 = ts.head; |
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1874
diff
changeset
|
3677 |
for (Type t2 : ts.tail) { |
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1874
diff
changeset
|
3678 |
if (t1.isErroneous()) |
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1874
diff
changeset
|
3679 |
return t1; |
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1874
diff
changeset
|
3680 |
t1 = glb(t1, t2); |
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1874
diff
changeset
|
3681 |
} |
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1874
diff
changeset
|
3682 |
return t1; |
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1874
diff
changeset
|
3683 |
} |
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1874
diff
changeset
|
3684 |
//where |
10 | 3685 |
public Type glb(Type t, Type s) { |
3686 |
if (s == null) |
|
3687 |
return t; |
|
7331
02ffc087c654
6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents:
7204
diff
changeset
|
3688 |
else if (t.isPrimitive() || s.isPrimitive()) |
02ffc087c654
6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents:
7204
diff
changeset
|
3689 |
return syms.errType; |
10 | 3690 |
else if (isSubtypeNoCapture(t, s)) |
3691 |
return t; |
|
3692 |
else if (isSubtypeNoCapture(s, t)) |
|
3693 |
return s; |
|
3694 |
||
3695 |
List<Type> closure = union(closure(t), closure(s)); |
|
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3696 |
return glbFlattened(closure, t); |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3697 |
} |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3698 |
//where |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3699 |
/** |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3700 |
* Perform glb for a list of non-primitive, non-error, non-compound types; |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3701 |
* redundant elements are removed. Bounds should be ordered according to |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3702 |
* {@link Symbol#precedes(TypeSymbol,Types)}. |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3703 |
* |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3704 |
* @param flatBounds List of type to glb |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3705 |
* @param errT Original type to use if the result is an error type |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3706 |
*/ |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3707 |
private Type glbFlattened(List<Type> flatBounds, Type errT) { |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3708 |
List<Type> bounds = closureMin(flatBounds); |
10 | 3709 |
|
3710 |
if (bounds.isEmpty()) { // length == 0 |
|
3711 |
return syms.objectType; |
|
3712 |
} else if (bounds.tail.isEmpty()) { // length == 1 |
|
3713 |
return bounds.head; |
|
3714 |
} else { // length > 1 |
|
3715 |
int classCount = 0; |
|
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3716 |
List<Type> lowers = List.nil(); |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3717 |
for (Type bound : bounds) { |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3718 |
if (!bound.isInterface()) { |
10 | 3719 |
classCount++; |
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3720 |
Type lower = cvarLowerBound(bound); |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3721 |
if (bound != lower && !lower.hasTag(BOT)) |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3722 |
lowers = insert(lowers, lower); |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3723 |
} |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3724 |
} |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3725 |
if (classCount > 1) { |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3726 |
if (lowers.isEmpty()) |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3727 |
return createErrorType(errT); |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3728 |
else |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3729 |
return glbFlattened(union(bounds, lowers), errT); |
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
3730 |
} |
10 | 3731 |
} |
3732 |
return makeCompoundType(bounds); |
|
3733 |
} |
|
3734 |
// </editor-fold> |
|
3735 |
||
3736 |
// <editor-fold defaultstate="collapsed" desc="hashCode"> |
|
3737 |
/** |
|
3738 |
* Compute a hash code on a type. |
|
3739 |
*/ |
|
14949
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14801
diff
changeset
|
3740 |
public int hashCode(Type t) { |
10 | 3741 |
return hashCode.visit(t); |
3742 |
} |
|
3743 |
// where |
|
3744 |
private static final UnaryVisitor<Integer> hashCode = new UnaryVisitor<Integer>() { |
|
3745 |
||
3746 |
public Integer visitType(Type t, Void ignored) { |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
3747 |
return t.getTag().ordinal(); |
10 | 3748 |
} |
3749 |
||
3750 |
@Override |
|
3751 |
public Integer visitClassType(ClassType t, Void ignored) { |
|
3752 |
int result = visit(t.getEnclosingType()); |
|
3753 |
result *= 127; |
|
3754 |
result += t.tsym.flatName().hashCode(); |
|
3755 |
for (Type s : t.getTypeArguments()) { |
|
3756 |
result *= 127; |
|
3757 |
result += visit(s); |
|
3758 |
} |
|
3759 |
return result; |
|
3760 |
} |
|
3761 |
||
3762 |
@Override |
|
14949
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14801
diff
changeset
|
3763 |
public Integer visitMethodType(MethodType t, Void ignored) { |
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14801
diff
changeset
|
3764 |
int h = METHOD.ordinal(); |
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14801
diff
changeset
|
3765 |
for (List<Type> thisargs = t.argtypes; |
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14801
diff
changeset
|
3766 |
thisargs.tail != null; |
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14801
diff
changeset
|
3767 |
thisargs = thisargs.tail) |
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14801
diff
changeset
|
3768 |
h = (h << 5) + visit(thisargs.head); |
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14801
diff
changeset
|
3769 |
return (h << 5) + visit(t.restype); |
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14801
diff
changeset
|
3770 |
} |
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14801
diff
changeset
|
3771 |
|
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14801
diff
changeset
|
3772 |
@Override |
10 | 3773 |
public Integer visitWildcardType(WildcardType t, Void ignored) { |
3774 |
int result = t.kind.hashCode(); |
|
3775 |
if (t.type != null) { |
|
3776 |
result *= 127; |
|
3777 |
result += visit(t.type); |
|
3778 |
} |
|
3779 |
return result; |
|
3780 |
} |
|
3781 |
||
3782 |
@Override |
|
3783 |
public Integer visitArrayType(ArrayType t, Void ignored) { |
|
3784 |
return visit(t.elemtype) + 12; |
|
3785 |
} |
|
3786 |
||
3787 |
@Override |
|
3788 |
public Integer visitTypeVar(TypeVar t, Void ignored) { |
|
3789 |
return System.identityHashCode(t.tsym); |
|
3790 |
} |
|
3791 |
||
3792 |
@Override |
|
3793 |
public Integer visitUndetVar(UndetVar t, Void ignored) { |
|
3794 |
return System.identityHashCode(t); |
|
3795 |
} |
|
3796 |
||
3797 |
@Override |
|
3798 |
public Integer visitErrorType(ErrorType t, Void ignored) { |
|
3799 |
return 0; |
|
3800 |
} |
|
3801 |
}; |
|
3802 |
// </editor-fold> |
|
3803 |
||
3804 |
// <editor-fold defaultstate="collapsed" desc="Return-Type-Substitutable"> |
|
3805 |
/** |
|
3806 |
* Does t have a result that is a subtype of the result type of s, |
|
3807 |
* suitable for covariant returns? It is assumed that both types |
|
3808 |
* are (possibly polymorphic) method types. Monomorphic method |
|
3809 |
* types are handled in the obvious way. Polymorphic method types |
|
3810 |
* require renaming all type variables of one to corresponding |
|
3811 |
* type variables in the other, where correspondence is by |
|
3812 |
* position in the type parameter list. */ |
|
3813 |
public boolean resultSubtype(Type t, Type s, Warner warner) { |
|
3814 |
List<Type> tvars = t.getTypeArguments(); |
|
3815 |
List<Type> svars = s.getTypeArguments(); |
|
3816 |
Type tres = t.getReturnType(); |
|
3817 |
Type sres = subst(s.getReturnType(), svars, tvars); |
|
3818 |
return covariantReturnType(tres, sres, warner); |
|
3819 |
} |
|
3820 |
||
3821 |
/** |
|
3822 |
* Return-Type-Substitutable. |
|
9303
eae35c201e19
7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents:
9301
diff
changeset
|
3823 |
* @jls section 8.4.5 |
10 | 3824 |
*/ |
3825 |
public boolean returnTypeSubstitutable(Type r1, Type r2) { |
|
3826 |
if (hasSameArgs(r1, r2)) |
|
14547 | 3827 |
return resultSubtype(r1, r2, noWarnings); |
10 | 3828 |
else |
3829 |
return covariantReturnType(r1.getReturnType(), |
|
1874 | 3830 |
erasure(r2.getReturnType()), |
14547 | 3831 |
noWarnings); |
1874 | 3832 |
} |
3833 |
||
3834 |
public boolean returnTypeSubstitutable(Type r1, |
|
3835 |
Type r2, Type r2res, |
|
3836 |
Warner warner) { |
|
3837 |
if (isSameType(r1.getReturnType(), r2res)) |
|
3838 |
return true; |
|
3839 |
if (r1.getReturnType().isPrimitive() || r2res.isPrimitive()) |
|
3840 |
return false; |
|
3841 |
||
3842 |
if (hasSameArgs(r1, r2)) |
|
3843 |
return covariantReturnType(r1.getReturnType(), r2res, warner); |
|
9595
a7dea6cbd5f7
7038363: cast from object to primitive should be for source >= 1.7
jjg
parents:
9303
diff
changeset
|
3844 |
if (!allowCovariantReturns) |
1874 | 3845 |
return false; |
3846 |
if (isSubtypeUnchecked(r1.getReturnType(), r2res, warner)) |
|
3847 |
return true; |
|
3848 |
if (!isSubtype(r1.getReturnType(), erasure(r2res))) |
|
3849 |
return false; |
|
7643
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7637
diff
changeset
|
3850 |
warner.warn(LintCategory.UNCHECKED); |
1874 | 3851 |
return true; |
10 | 3852 |
} |
3853 |
||
3854 |
/** |
|
3855 |
* Is t an appropriate return type in an overrider for a |
|
3856 |
* method that returns s? |
|
3857 |
*/ |
|
3858 |
public boolean covariantReturnType(Type t, Type s, Warner warner) { |
|
1874 | 3859 |
return |
3860 |
isSameType(t, s) || |
|
9595
a7dea6cbd5f7
7038363: cast from object to primitive should be for source >= 1.7
jjg
parents:
9303
diff
changeset
|
3861 |
allowCovariantReturns && |
10 | 3862 |
!t.isPrimitive() && |
1874 | 3863 |
!s.isPrimitive() && |
3864 |
isAssignable(t, s, warner); |
|
10 | 3865 |
} |
3866 |
// </editor-fold> |
|
3867 |
||
3868 |
// <editor-fold defaultstate="collapsed" desc="Box/unbox support"> |
|
3869 |
/** |
|
3870 |
* Return the class that boxes the given primitive. |
|
3871 |
*/ |
|
3872 |
public ClassSymbol boxedClass(Type t) { |
|
22442
8fd30fc4e3a3
8029690: Move symbol creation from ClassReader to Symtab
jfranck
parents:
22167
diff
changeset
|
3873 |
return syms.enterClass(syms.boxedName[t.getTag().ordinal()]); |
10 | 3874 |
} |
3875 |
||
3876 |
/** |
|
7331
02ffc087c654
6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents:
7204
diff
changeset
|
3877 |
* Return the boxed type if 't' is primitive, otherwise return 't' itself. |
02ffc087c654
6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents:
7204
diff
changeset
|
3878 |
*/ |
02ffc087c654
6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents:
7204
diff
changeset
|
3879 |
public Type boxedTypeOrType(Type t) { |
02ffc087c654
6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents:
7204
diff
changeset
|
3880 |
return t.isPrimitive() ? |
02ffc087c654
6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents:
7204
diff
changeset
|
3881 |
boxedClass(t).type : |
02ffc087c654
6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents:
7204
diff
changeset
|
3882 |
t; |
02ffc087c654
6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents:
7204
diff
changeset
|
3883 |
} |
02ffc087c654
6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents:
7204
diff
changeset
|
3884 |
|
02ffc087c654
6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents:
7204
diff
changeset
|
3885 |
/** |
10 | 3886 |
* Return the primitive type corresponding to a boxed type. |
3887 |
*/ |
|
3888 |
public Type unboxedType(Type t) { |
|
3889 |
if (allowBoxing) { |
|
3890 |
for (int i=0; i<syms.boxedName.length; i++) { |
|
3891 |
Name box = syms.boxedName[i]; |
|
3892 |
if (box != null && |
|
22442
8fd30fc4e3a3
8029690: Move symbol creation from ClassReader to Symtab
jfranck
parents:
22167
diff
changeset
|
3893 |
asSuper(t, syms.enterClass(box)) != null) |
10 | 3894 |
return syms.typeOfTag[i]; |
3895 |
} |
|
3896 |
} |
|
3897 |
return Type.noType; |
|
3898 |
} |
|
14057
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
3899 |
|
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
3900 |
/** |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
3901 |
* Return the unboxed type if 't' is a boxed class, otherwise return 't' itself. |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
3902 |
*/ |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
3903 |
public Type unboxedTypeOrType(Type t) { |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
3904 |
Type unboxedType = unboxedType(t); |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
3905 |
return unboxedType.hasTag(NONE) ? t : unboxedType; |
14057
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
3906 |
} |
10 | 3907 |
// </editor-fold> |
3908 |
||
3909 |
// <editor-fold defaultstate="collapsed" desc="Capture conversion"> |
|
3910 |
/* |
|
9303
eae35c201e19
7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents:
9301
diff
changeset
|
3911 |
* JLS 5.1.10 Capture Conversion: |
10 | 3912 |
* |
3913 |
* Let G name a generic type declaration with n formal type |
|
3914 |
* parameters A1 ... An with corresponding bounds U1 ... Un. There |
|
3915 |
* exists a capture conversion from G<T1 ... Tn> to G<S1 ... Sn>, |
|
3916 |
* where, for 1 <= i <= n: |
|
3917 |
* |
|
3918 |
* + If Ti is a wildcard type argument (4.5.1) of the form ? then |
|
3919 |
* Si is a fresh type variable whose upper bound is |
|
3920 |
* Ui[A1 := S1, ..., An := Sn] and whose lower bound is the null |
|
3921 |
* type. |
|
3922 |
* |
|
3923 |
* + If Ti is a wildcard type argument of the form ? extends Bi, |
|
3924 |
* then Si is a fresh type variable whose upper bound is |
|
3925 |
* glb(Bi, Ui[A1 := S1, ..., An := Sn]) and whose lower bound is |
|
3926 |
* the null type, where glb(V1,... ,Vm) is V1 & ... & Vm. It is |
|
3927 |
* a compile-time error if for any two classes (not interfaces) |
|
3928 |
* Vi and Vj,Vi is not a subclass of Vj or vice versa. |
|
3929 |
* |
|
3930 |
* + If Ti is a wildcard type argument of the form ? super Bi, |
|
3931 |
* then Si is a fresh type variable whose upper bound is |
|
3932 |
* Ui[A1 := S1, ..., An := Sn] and whose lower bound is Bi. |
|
3933 |
* |
|
3934 |
* + Otherwise, Si = Ti. |
|
3935 |
* |
|
3936 |
* Capture conversion on any type other than a parameterized type |
|
3937 |
* (4.5) acts as an identity conversion (5.1.1). Capture |
|
3938 |
* conversions never require a special action at run time and |
|
3939 |
* therefore never throw an exception at run time. |
|
3940 |
* |
|
3941 |
* Capture conversion is not applied recursively. |
|
3942 |
*/ |
|
3943 |
/** |
|
9303
eae35c201e19
7032975: API files in javax.annotation.processing need to be updated for references to JLS
jjh
parents:
9301
diff
changeset
|
3944 |
* Capture conversion as specified by the JLS. |
10 | 3945 |
*/ |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
2223
diff
changeset
|
3946 |
|
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
2223
diff
changeset
|
3947 |
public List<Type> capture(List<Type> ts) { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
2223
diff
changeset
|
3948 |
List<Type> buf = List.nil(); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
2223
diff
changeset
|
3949 |
for (Type t : ts) { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
2223
diff
changeset
|
3950 |
buf = buf.prepend(capture(t)); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
2223
diff
changeset
|
3951 |
} |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
2223
diff
changeset
|
3952 |
return buf.reverse(); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
2223
diff
changeset
|
3953 |
} |
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
23810
diff
changeset
|
3954 |
|
10 | 3955 |
public Type capture(Type t) { |
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
23810
diff
changeset
|
3956 |
if (!t.hasTag(CLASS)) { |
10 | 3957 |
return t; |
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
23810
diff
changeset
|
3958 |
} |
6350
7070b0722ebb
6886247: regression: javac crashes with an assertion error in Attr.java
mcimadamore
parents:
5847
diff
changeset
|
3959 |
if (t.getEnclosingType() != Type.noType) { |
7070b0722ebb
6886247: regression: javac crashes with an assertion error in Attr.java
mcimadamore
parents:
5847
diff
changeset
|
3960 |
Type capturedEncl = capture(t.getEnclosingType()); |
7070b0722ebb
6886247: regression: javac crashes with an assertion error in Attr.java
mcimadamore
parents:
5847
diff
changeset
|
3961 |
if (capturedEncl != t.getEnclosingType()) { |
7070b0722ebb
6886247: regression: javac crashes with an assertion error in Attr.java
mcimadamore
parents:
5847
diff
changeset
|
3962 |
Type type1 = memberType(capturedEncl, t.tsym); |
7070b0722ebb
6886247: regression: javac crashes with an assertion error in Attr.java
mcimadamore
parents:
5847
diff
changeset
|
3963 |
t = subst(type1, t.tsym.type.getTypeArguments(), t.getTypeArguments()); |
7070b0722ebb
6886247: regression: javac crashes with an assertion error in Attr.java
mcimadamore
parents:
5847
diff
changeset
|
3964 |
} |
7070b0722ebb
6886247: regression: javac crashes with an assertion error in Attr.java
mcimadamore
parents:
5847
diff
changeset
|
3965 |
} |
10 | 3966 |
ClassType cls = (ClassType)t; |
3967 |
if (cls.isRaw() || !cls.isParameterized()) |
|
3968 |
return cls; |
|
3969 |
||
3970 |
ClassType G = (ClassType)cls.asElement().asType(); |
|
3971 |
List<Type> A = G.getTypeArguments(); |
|
3972 |
List<Type> T = cls.getTypeArguments(); |
|
3973 |
List<Type> S = freshTypeVariables(T); |
|
3974 |
||
3975 |
List<Type> currentA = A; |
|
3976 |
List<Type> currentT = T; |
|
3977 |
List<Type> currentS = S; |
|
3978 |
boolean captured = false; |
|
3979 |
while (!currentA.isEmpty() && |
|
3980 |
!currentT.isEmpty() && |
|
3981 |
!currentS.isEmpty()) { |
|
3982 |
if (currentS.head != currentT.head) { |
|
3983 |
captured = true; |
|
24396 | 3984 |
WildcardType Ti = (WildcardType)currentT.head; |
10 | 3985 |
Type Ui = currentA.head.getUpperBound(); |
24396 | 3986 |
CapturedType Si = (CapturedType)currentS.head; |
10 | 3987 |
if (Ui == null) |
3988 |
Ui = syms.objectType; |
|
3989 |
switch (Ti.kind) { |
|
3990 |
case UNBOUND: |
|
3991 |
Si.bound = subst(Ui, A, S); |
|
3992 |
Si.lower = syms.botType; |
|
3993 |
break; |
|
3994 |
case EXTENDS: |
|
3995 |
Si.bound = glb(Ti.getExtendsBound(), subst(Ui, A, S)); |
|
3996 |
Si.lower = syms.botType; |
|
3997 |
break; |
|
3998 |
case SUPER: |
|
3999 |
Si.bound = subst(Ui, A, S); |
|
4000 |
Si.lower = Ti.getSuperBound(); |
|
4001 |
break; |
|
4002 |
} |
|
24291
440746208be7
8033437: javac, inconsistent generic types behaviour when compiling together vs. separate
pgovereau
parents:
24226
diff
changeset
|
4003 |
Type tmpBound = Si.bound.hasTag(UNDETVAR) ? ((UndetVar)Si.bound).qtype : Si.bound; |
440746208be7
8033437: javac, inconsistent generic types behaviour when compiling together vs. separate
pgovereau
parents:
24226
diff
changeset
|
4004 |
Type tmpLower = Si.lower.hasTag(UNDETVAR) ? ((UndetVar)Si.lower).qtype : Si.lower; |
440746208be7
8033437: javac, inconsistent generic types behaviour when compiling together vs. separate
pgovereau
parents:
24226
diff
changeset
|
4005 |
if (!Si.bound.hasTag(ERROR) && |
440746208be7
8033437: javac, inconsistent generic types behaviour when compiling together vs. separate
pgovereau
parents:
24226
diff
changeset
|
4006 |
!Si.lower.hasTag(ERROR) && |
440746208be7
8033437: javac, inconsistent generic types behaviour when compiling together vs. separate
pgovereau
parents:
24226
diff
changeset
|
4007 |
isSameType(tmpBound, tmpLower, false)) { |
10 | 4008 |
currentS.head = Si.bound; |
24291
440746208be7
8033437: javac, inconsistent generic types behaviour when compiling together vs. separate
pgovereau
parents:
24226
diff
changeset
|
4009 |
} |
10 | 4010 |
} |
4011 |
currentA = currentA.tail; |
|
4012 |
currentT = currentT.tail; |
|
4013 |
currentS = currentS.tail; |
|
4014 |
} |
|
4015 |
if (!currentA.isEmpty() || !currentT.isEmpty() || !currentS.isEmpty()) |
|
4016 |
return erasure(t); // some "rare" type involved |
|
4017 |
||
4018 |
if (captured) |
|
24396 | 4019 |
return new ClassType(cls.getEnclosingType(), S, cls.tsym, |
4020 |
cls.getAnnotationMirrors()); |
|
10 | 4021 |
else |
4022 |
return t; |
|
4023 |
} |
|
4024 |
// where |
|
2221
cd6557bcaa0a
6799605: Basic/Raw formatters should use type/symbol printer instead of toString()
mcimadamore
parents:
2218
diff
changeset
|
4025 |
public List<Type> freshTypeVariables(List<Type> types) { |
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19914
diff
changeset
|
4026 |
ListBuffer<Type> result = new ListBuffer<>(); |
10 | 4027 |
for (Type t : types) { |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
4028 |
if (t.hasTag(WILDCARD)) { |
10 | 4029 |
Type bound = ((WildcardType)t).getExtendsBound(); |
4030 |
if (bound == null) |
|
4031 |
bound = syms.objectType; |
|
4032 |
result.append(new CapturedType(capturedName, |
|
4033 |
syms.noSymbol, |
|
4034 |
bound, |
|
4035 |
syms.botType, |
|
24396 | 4036 |
(WildcardType)t, |
4037 |
Type.noAnnotations)); |
|
10 | 4038 |
} else { |
4039 |
result.append(t); |
|
4040 |
} |
|
4041 |
} |
|
4042 |
return result.toList(); |
|
4043 |
} |
|
4044 |
// </editor-fold> |
|
4045 |
||
4046 |
// <editor-fold defaultstate="collapsed" desc="Internal utility methods"> |
|
4047 |
private List<Type> upperBounds(List<Type> ss) { |
|
4048 |
if (ss.isEmpty()) return ss; |
|
4049 |
Type head = upperBound(ss.head); |
|
4050 |
List<Type> tail = upperBounds(ss.tail); |
|
4051 |
if (head != ss.head || tail != ss.tail) |
|
4052 |
return tail.prepend(head); |
|
4053 |
else |
|
4054 |
return ss; |
|
4055 |
} |
|
4056 |
||
4057 |
private boolean sideCast(Type from, Type to, Warner warn) { |
|
4058 |
// We are casting from type $from$ to type $to$, which are |
|
4059 |
// non-final unrelated types. This method |
|
4060 |
// tries to reject a cast by transferring type parameters |
|
4061 |
// from $to$ to $from$ by common superinterfaces. |
|
4062 |
boolean reverse = false; |
|
4063 |
Type target = to; |
|
4064 |
if ((to.tsym.flags() & INTERFACE) == 0) { |
|
8032 | 4065 |
Assert.check((from.tsym.flags() & INTERFACE) != 0); |
10 | 4066 |
reverse = true; |
4067 |
to = from; |
|
4068 |
from = target; |
|
4069 |
} |
|
4070 |
List<Type> commonSupers = superClosure(to, erasure(from)); |
|
4071 |
boolean giveWarning = commonSupers.isEmpty(); |
|
4072 |
// The arguments to the supers could be unified here to |
|
4073 |
// get a more accurate analysis |
|
4074 |
while (commonSupers.nonEmpty()) { |
|
21712
d363f367d4c0
8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents:
21041
diff
changeset
|
4075 |
Type t1 = asSuper(from, commonSupers.head.tsym); |
10 | 4076 |
Type t2 = commonSupers.head; // same as asSuper(to, commonSupers.head.tsym); |
4077 |
if (disjointTypes(t1.getTypeArguments(), t2.getTypeArguments())) |
|
4078 |
return false; |
|
4079 |
giveWarning = giveWarning || (reverse ? giveWarning(t2, t1) : giveWarning(t1, t2)); |
|
4080 |
commonSupers = commonSupers.tail; |
|
4081 |
} |
|
1792 | 4082 |
if (giveWarning && !isReifiable(reverse ? from : to)) |
7643
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7637
diff
changeset
|
4083 |
warn.warn(LintCategory.UNCHECKED); |
9595
a7dea6cbd5f7
7038363: cast from object to primitive should be for source >= 1.7
jjg
parents:
9303
diff
changeset
|
4084 |
if (!allowCovariantReturns) |
10 | 4085 |
// reject if there is a common method signature with |
4086 |
// incompatible return types. |
|
4087 |
chk.checkCompatibleAbstracts(warn.pos(), from, to); |
|
4088 |
return true; |
|
4089 |
} |
|
4090 |
||
4091 |
private boolean sideCastFinal(Type from, Type to, Warner warn) { |
|
4092 |
// We are casting from type $from$ to type $to$, which are |
|
4093 |
// unrelated types one of which is final and the other of |
|
4094 |
// which is an interface. This method |
|
4095 |
// tries to reject a cast by transferring type parameters |
|
4096 |
// from the final class to the interface. |
|
4097 |
boolean reverse = false; |
|
4098 |
Type target = to; |
|
4099 |
if ((to.tsym.flags() & INTERFACE) == 0) { |
|
8032 | 4100 |
Assert.check((from.tsym.flags() & INTERFACE) != 0); |
10 | 4101 |
reverse = true; |
4102 |
to = from; |
|
4103 |
from = target; |
|
4104 |
} |
|
8032 | 4105 |
Assert.check((from.tsym.flags() & FINAL) != 0); |
21712
d363f367d4c0
8027439: Compile-time error in the case of ((Integer[] & Serializable)new Integer[1]).getClass()
emc
parents:
21041
diff
changeset
|
4106 |
Type t1 = asSuper(from, to.tsym); |
10 | 4107 |
if (t1 == null) return false; |
4108 |
Type t2 = to; |
|
4109 |
if (disjointTypes(t1.getTypeArguments(), t2.getTypeArguments())) |
|
4110 |
return false; |
|
9595
a7dea6cbd5f7
7038363: cast from object to primitive should be for source >= 1.7
jjg
parents:
9303
diff
changeset
|
4111 |
if (!allowCovariantReturns) |
10 | 4112 |
// reject if there is a common method signature with |
4113 |
// incompatible return types. |
|
4114 |
chk.checkCompatibleAbstracts(warn.pos(), from, to); |
|
4115 |
if (!isReifiable(target) && |
|
4116 |
(reverse ? giveWarning(t2, t1) : giveWarning(t1, t2))) |
|
7643
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7637
diff
changeset
|
4117 |
warn.warn(LintCategory.UNCHECKED); |
10 | 4118 |
return true; |
4119 |
} |
|
4120 |
||
4121 |
private boolean giveWarning(Type from, Type to) { |
|
16565
aee0d0aaec44
8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents:
16556
diff
changeset
|
4122 |
List<Type> bounds = to.isCompound() ? |
24396 | 4123 |
((IntersectionClassType)to).getComponents() : List.of(to); |
16565
aee0d0aaec44
8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents:
16556
diff
changeset
|
4124 |
for (Type b : bounds) { |
aee0d0aaec44
8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents:
16556
diff
changeset
|
4125 |
Type subFrom = asSub(from, b.tsym); |
aee0d0aaec44
8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents:
16556
diff
changeset
|
4126 |
if (b.isParameterized() && |
aee0d0aaec44
8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents:
16556
diff
changeset
|
4127 |
(!(isUnbounded(b) || |
aee0d0aaec44
8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents:
16556
diff
changeset
|
4128 |
isSubtype(from, b) || |
aee0d0aaec44
8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents:
16556
diff
changeset
|
4129 |
((subFrom != null) && containsType(b.allparams(), subFrom.allparams()))))) { |
aee0d0aaec44
8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents:
16556
diff
changeset
|
4130 |
return true; |
aee0d0aaec44
8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents:
16556
diff
changeset
|
4131 |
} |
aee0d0aaec44
8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents:
16556
diff
changeset
|
4132 |
} |
aee0d0aaec44
8010101: Intersection type cast issues redundant unchecked warning
mcimadamore
parents:
16556
diff
changeset
|
4133 |
return false; |
10 | 4134 |
} |
4135 |
||
4136 |
private List<Type> superClosure(Type t, Type s) { |
|
4137 |
List<Type> cl = List.nil(); |
|
4138 |
for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail) { |
|
4139 |
if (isSubtype(s, erasure(l.head))) { |
|
4140 |
cl = insert(cl, l.head); |
|
4141 |
} else { |
|
4142 |
cl = union(cl, superClosure(l.head, s)); |
|
4143 |
} |
|
4144 |
} |
|
4145 |
return cl; |
|
4146 |
} |
|
4147 |
||
4148 |
private boolean containsTypeEquivalent(Type t, Type s) { |
|
4149 |
return |
|
4150 |
isSameType(t, s) || // shortcut |
|
4151 |
containsType(t, s) && containsType(s, t); |
|
4152 |
} |
|
4153 |
||
1473
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4154 |
// <editor-fold defaultstate="collapsed" desc="adapt"> |
10 | 4155 |
/** |
4156 |
* Adapt a type by computing a substitution which maps a source |
|
4157 |
* type to a target type. |
|
4158 |
* |
|
4159 |
* @param source the source type |
|
4160 |
* @param target the target type |
|
4161 |
* @param from the type variables of the computed substitution |
|
4162 |
* @param to the types of the computed substitution. |
|
4163 |
*/ |
|
4164 |
public void adapt(Type source, |
|
4165 |
Type target, |
|
4166 |
ListBuffer<Type> from, |
|
4167 |
ListBuffer<Type> to) throws AdaptFailure { |
|
1473
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4168 |
new Adapter(from, to).adapt(source, target); |
10 | 4169 |
} |
1473
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4170 |
|
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4171 |
class Adapter extends SimpleVisitor<Void, Type> { |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4172 |
|
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4173 |
ListBuffer<Type> from; |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4174 |
ListBuffer<Type> to; |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4175 |
Map<Symbol,Type> mapping; |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4176 |
|
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4177 |
Adapter(ListBuffer<Type> from, ListBuffer<Type> to) { |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4178 |
this.from = from; |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4179 |
this.to = to; |
22163 | 4180 |
mapping = new HashMap<>(); |
1473
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4181 |
} |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4182 |
|
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4183 |
public void adapt(Type source, Type target) throws AdaptFailure { |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4184 |
visit(source, target); |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4185 |
List<Type> fromList = from.toList(); |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4186 |
List<Type> toList = to.toList(); |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4187 |
while (!fromList.isEmpty()) { |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4188 |
Type val = mapping.get(fromList.head.tsym); |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4189 |
if (toList.head != val) |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4190 |
toList.head = val; |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4191 |
fromList = fromList.tail; |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4192 |
toList = toList.tail; |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4193 |
} |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4194 |
} |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4195 |
|
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4196 |
@Override |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4197 |
public Void visitClassType(ClassType source, Type target) throws AdaptFailure { |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
4198 |
if (target.hasTag(CLASS)) |
1473
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4199 |
adaptRecursive(source.allparams(), target.allparams()); |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4200 |
return null; |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4201 |
} |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4202 |
|
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4203 |
@Override |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4204 |
public Void visitArrayType(ArrayType source, Type target) throws AdaptFailure { |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
4205 |
if (target.hasTag(ARRAY)) |
1473
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4206 |
adaptRecursive(elemtype(source), elemtype(target)); |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4207 |
return null; |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4208 |
} |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4209 |
|
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4210 |
@Override |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4211 |
public Void visitWildcardType(WildcardType source, Type target) throws AdaptFailure { |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4212 |
if (source.isExtendsBound()) |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4213 |
adaptRecursive(upperBound(source), upperBound(target)); |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4214 |
else if (source.isSuperBound()) |
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
4215 |
adaptRecursive(wildLowerBound(source), wildLowerBound(target)); |
1473
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4216 |
return null; |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4217 |
} |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4218 |
|
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4219 |
@Override |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4220 |
public Void visitTypeVar(TypeVar source, Type target) throws AdaptFailure { |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4221 |
// Check to see if there is |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4222 |
// already a mapping for $source$, in which case |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4223 |
// the old mapping will be merged with the new |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4224 |
Type val = mapping.get(source.tsym); |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4225 |
if (val != null) { |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4226 |
if (val.isSuperBound() && target.isSuperBound()) { |
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
4227 |
val = isSubtype(wildLowerBound(val), wildLowerBound(target)) |
1473
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4228 |
? target : val; |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4229 |
} else if (val.isExtendsBound() && target.isExtendsBound()) { |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4230 |
val = isSubtype(upperBound(val), upperBound(target)) |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4231 |
? val : target; |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4232 |
} else if (!isSameType(val, target)) { |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4233 |
throw new AdaptFailure(); |
10 | 4234 |
} |
1473
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4235 |
} else { |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4236 |
val = target; |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4237 |
from.append(source); |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4238 |
to.append(target); |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4239 |
} |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4240 |
mapping.put(source.tsym, val); |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4241 |
return null; |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4242 |
} |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4243 |
|
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4244 |
@Override |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4245 |
public Void visitType(Type source, Type target) { |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4246 |
return null; |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4247 |
} |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4248 |
|
22163 | 4249 |
private Set<TypePair> cache = new HashSet<>(); |
1473
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4250 |
|
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4251 |
private void adaptRecursive(Type source, Type target) { |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4252 |
TypePair pair = new TypePair(source, target); |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4253 |
if (cache.add(pair)) { |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4254 |
try { |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4255 |
visit(source, target); |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4256 |
} finally { |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4257 |
cache.remove(pair); |
10 | 4258 |
} |
4259 |
} |
|
4260 |
} |
|
1473
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4261 |
|
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4262 |
private void adaptRecursive(List<Type> source, List<Type> target) { |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4263 |
if (source.length() == target.length()) { |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4264 |
while (source.nonEmpty()) { |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4265 |
adaptRecursive(source.head, target.head); |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4266 |
source = source.tail; |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4267 |
target = target.tail; |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4268 |
} |
10 | 4269 |
} |
4270 |
} |
|
4271 |
} |
|
4272 |
||
1473
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4273 |
public static class AdaptFailure extends RuntimeException { |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4274 |
static final long serialVersionUID = -7490231548272701566L; |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4275 |
} |
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4276 |
|
10 | 4277 |
private void adaptSelf(Type t, |
4278 |
ListBuffer<Type> from, |
|
4279 |
ListBuffer<Type> to) { |
|
4280 |
try { |
|
4281 |
//if (t.tsym.type != t) |
|
4282 |
adapt(t.tsym.type, t, from, to); |
|
4283 |
} catch (AdaptFailure ex) { |
|
4284 |
// Adapt should never fail calculating a mapping from |
|
4285 |
// t.tsym.type to t as there can be no merge problem. |
|
4286 |
throw new AssertionError(ex); |
|
4287 |
} |
|
4288 |
} |
|
1473
16ba23bb25e6
6586091: javac crashes with StackOverflowError
mcimadamore
parents:
1471
diff
changeset
|
4289 |
// </editor-fold> |
10 | 4290 |
|
4291 |
/** |
|
4292 |
* Rewrite all type variables (universal quantifiers) in the given |
|
4293 |
* type to wildcards (existential quantifiers). This is used to |
|
4294 |
* determine if a cast is allowed. For example, if high is true |
|
4295 |
* and {@code T <: Number}, then {@code List<T>} is rewritten to |
|
4296 |
* {@code List<? extends Number>}. Since {@code List<Integer> <: |
|
4297 |
* List<? extends Number>} a {@code List<T>} can be cast to {@code |
|
4298 |
* List<Integer>} with a warning. |
|
4299 |
* @param t a type |
|
4300 |
* @param high if true return an upper bound; otherwise a lower |
|
4301 |
* bound |
|
4302 |
* @param rewriteTypeVars only rewrite captured wildcards if false; |
|
4303 |
* otherwise rewrite all type variables |
|
4304 |
* @return the type rewritten with wildcards (existential |
|
4305 |
* quantifiers) only |
|
4306 |
*/ |
|
4307 |
private Type rewriteQuantifiers(Type t, boolean high, boolean rewriteTypeVars) { |
|
6353
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
4308 |
return new Rewriter(high, rewriteTypeVars).visit(t); |
1530 | 4309 |
} |
4310 |
||
4311 |
class Rewriter extends UnaryVisitor<Type> { |
|
4312 |
||
4313 |
boolean high; |
|
4314 |
boolean rewriteTypeVars; |
|
4315 |
||
4316 |
Rewriter(boolean high, boolean rewriteTypeVars) { |
|
4317 |
this.high = high; |
|
4318 |
this.rewriteTypeVars = rewriteTypeVars; |
|
4319 |
} |
|
4320 |
||
6353
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
4321 |
@Override |
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
4322 |
public Type visitClassType(ClassType t, Void s) { |
22163 | 4323 |
ListBuffer<Type> rewritten = new ListBuffer<>(); |
1530 | 4324 |
boolean changed = false; |
6353
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
4325 |
for (Type arg : t.allparams()) { |
1530 | 4326 |
Type bound = visit(arg); |
4327 |
if (arg != bound) { |
|
4328 |
changed = true; |
|
4329 |
} |
|
4330 |
rewritten.append(bound); |
|
10 | 4331 |
} |
1530 | 4332 |
if (changed) |
6353
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
4333 |
return subst(t.tsym.type, |
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
4334 |
t.tsym.type.allparams(), |
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
4335 |
rewritten.toList()); |
1530 | 4336 |
else |
4337 |
return t; |
|
4338 |
} |
|
4339 |
||
4340 |
public Type visitType(Type t, Void s) { |
|
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24291
diff
changeset
|
4341 |
return high ? upperBound(t) : t; |
1530 | 4342 |
} |
4343 |
||
4344 |
@Override |
|
4345 |
public Type visitCapturedType(CapturedType t, Void s) { |
|
11549
cc901a28adc3
7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents:
10810
diff
changeset
|
4346 |
Type w_bound = t.wildcard.type; |
cc901a28adc3
7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents:
10810
diff
changeset
|
4347 |
Type bound = w_bound.contains(t) ? |
cc901a28adc3
7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents:
10810
diff
changeset
|
4348 |
erasure(w_bound) : |
cc901a28adc3
7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents:
10810
diff
changeset
|
4349 |
visit(w_bound); |
cc901a28adc3
7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents:
10810
diff
changeset
|
4350 |
return rewriteAsWildcardType(visit(bound), t.wildcard.bound, t.wildcard.kind); |
10 | 4351 |
} |
1530 | 4352 |
|
4353 |
@Override |
|
4354 |
public Type visitTypeVar(TypeVar t, Void s) { |
|
6353
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
4355 |
if (rewriteTypeVars) { |
11549
cc901a28adc3
7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents:
10810
diff
changeset
|
4356 |
Type bound = t.bound.contains(t) ? |
7627
de2a86da165e
7005095: Cast: compile reject sensible cast from final class to interface
mcimadamore
parents:
7331
diff
changeset
|
4357 |
erasure(t.bound) : |
11549
cc901a28adc3
7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents:
10810
diff
changeset
|
4358 |
visit(t.bound); |
cc901a28adc3
7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents:
10810
diff
changeset
|
4359 |
return rewriteAsWildcardType(bound, t, EXTENDS); |
cc901a28adc3
7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents:
10810
diff
changeset
|
4360 |
} else { |
cc901a28adc3
7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents:
10810
diff
changeset
|
4361 |
return t; |
6353
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
4362 |
} |
1530 | 4363 |
} |
4364 |
||
4365 |
@Override |
|
4366 |
public Type visitWildcardType(WildcardType t, Void s) { |
|
11549
cc901a28adc3
7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents:
10810
diff
changeset
|
4367 |
Type bound2 = visit(t.type); |
cc901a28adc3
7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents:
10810
diff
changeset
|
4368 |
return t.type == bound2 ? t : rewriteAsWildcardType(bound2, t.bound, t.kind); |
6353
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
4369 |
} |
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
4370 |
|
11549
cc901a28adc3
7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents:
10810
diff
changeset
|
4371 |
private Type rewriteAsWildcardType(Type bound, TypeVar formal, BoundKind bk) { |
cc901a28adc3
7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents:
10810
diff
changeset
|
4372 |
switch (bk) { |
cc901a28adc3
7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents:
10810
diff
changeset
|
4373 |
case EXTENDS: return high ? |
cc901a28adc3
7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents:
10810
diff
changeset
|
4374 |
makeExtendsWildcard(B(bound), formal) : |
cc901a28adc3
7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents:
10810
diff
changeset
|
4375 |
makeExtendsWildcard(syms.objectType, formal); |
cc901a28adc3
7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents:
10810
diff
changeset
|
4376 |
case SUPER: return high ? |
cc901a28adc3
7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents:
10810
diff
changeset
|
4377 |
makeSuperWildcard(syms.botType, formal) : |
cc901a28adc3
7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents:
10810
diff
changeset
|
4378 |
makeSuperWildcard(B(bound), formal); |
cc901a28adc3
7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents:
10810
diff
changeset
|
4379 |
case UNBOUND: return makeExtendsWildcard(syms.objectType, formal); |
cc901a28adc3
7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents:
10810
diff
changeset
|
4380 |
default: |
cc901a28adc3
7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents:
10810
diff
changeset
|
4381 |
Assert.error("Invalid bound kind " + bk); |
cc901a28adc3
7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents:
10810
diff
changeset
|
4382 |
return null; |
cc901a28adc3
7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
mcimadamore
parents:
10810
diff
changeset
|
4383 |
} |
6353
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
4384 |
} |
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
4385 |
|
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
4386 |
Type B(Type t) { |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
4387 |
while (t.hasTag(WILDCARD)) { |
24396 | 4388 |
WildcardType w = (WildcardType)t; |
6353
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
4389 |
t = high ? |
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
4390 |
w.getExtendsBound() : |
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
4391 |
w.getSuperBound(); |
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
4392 |
if (t == null) { |
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
4393 |
t = high ? syms.objectType : syms.botType; |
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
4394 |
} |
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
4395 |
} |
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
4396 |
return t; |
1530 | 4397 |
} |
10 | 4398 |
} |
4399 |
||
6353
bb5c39054bf3
6932571: Compiling Generics causing Inconvertible types
mcimadamore
parents:
6350
diff
changeset
|
4400 |
|
10 | 4401 |
/** |
4402 |
* Create a wildcard with the given upper (extends) bound; create |
|
4403 |
* an unbounded wildcard if bound is Object. |
|
4404 |
* |
|
4405 |
* @param bound the upper bound |
|
4406 |
* @param formal the formal type parameter that will be |
|
4407 |
* substituted by the wildcard |
|
4408 |
*/ |
|
4409 |
private WildcardType makeExtendsWildcard(Type bound, TypeVar formal) { |
|
4410 |
if (bound == syms.objectType) { |
|
4411 |
return new WildcardType(syms.objectType, |
|
4412 |
BoundKind.UNBOUND, |
|
4413 |
syms.boundClass, |
|
24396 | 4414 |
formal, |
4415 |
Type.noAnnotations); |
|
10 | 4416 |
} else { |
4417 |
return new WildcardType(bound, |
|
4418 |
BoundKind.EXTENDS, |
|
4419 |
syms.boundClass, |
|
24396 | 4420 |
formal, |
4421 |
Type.noAnnotations); |
|
10 | 4422 |
} |
4423 |
} |
|
4424 |
||
4425 |
/** |
|
4426 |
* Create a wildcard with the given lower (super) bound; create an |
|
4427 |
* unbounded wildcard if bound is bottom (type of {@code null}). |
|
4428 |
* |
|
4429 |
* @param bound the lower bound |
|
4430 |
* @param formal the formal type parameter that will be |
|
4431 |
* substituted by the wildcard |
|
4432 |
*/ |
|
4433 |
private WildcardType makeSuperWildcard(Type bound, TypeVar formal) { |
|
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18412
diff
changeset
|
4434 |
if (bound.hasTag(BOT)) { |
10 | 4435 |
return new WildcardType(syms.objectType, |
4436 |
BoundKind.UNBOUND, |
|
4437 |
syms.boundClass, |
|
24396 | 4438 |
formal, |
4439 |
Type.noAnnotations); |
|
10 | 4440 |
} else { |
4441 |
return new WildcardType(bound, |
|
4442 |
BoundKind.SUPER, |
|
4443 |
syms.boundClass, |
|
24396 | 4444 |
formal, |
4445 |
Type.noAnnotations); |
|
10 | 4446 |
} |
4447 |
} |
|
4448 |
||
4449 |
/** |
|
4450 |
* A wrapper for a type that allows use in sets. |
|
4451 |
*/ |
|
14949
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14801
diff
changeset
|
4452 |
public static class UniqueType { |
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14801
diff
changeset
|
4453 |
public final Type type; |
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14801
diff
changeset
|
4454 |
final Types types; |
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14801
diff
changeset
|
4455 |
|
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14801
diff
changeset
|
4456 |
public UniqueType(Type type, Types types) { |
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14801
diff
changeset
|
4457 |
this.type = type; |
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14801
diff
changeset
|
4458 |
this.types = types; |
10 | 4459 |
} |
14949
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14801
diff
changeset
|
4460 |
|
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14801
diff
changeset
|
4461 |
public int hashCode() { |
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14801
diff
changeset
|
4462 |
return types.hashCode(type); |
10 | 4463 |
} |
14949
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14801
diff
changeset
|
4464 |
|
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14801
diff
changeset
|
4465 |
public boolean equals(Object obj) { |
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14801
diff
changeset
|
4466 |
return (obj instanceof UniqueType) && |
24396 | 4467 |
types.isSameType(type, ((UniqueType)obj).type); |
14949
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14801
diff
changeset
|
4468 |
} |
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14801
diff
changeset
|
4469 |
|
10 | 4470 |
public String toString() { |
14949
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14801
diff
changeset
|
4471 |
return type.toString(); |
10 | 4472 |
} |
14949
45f43822bbde
8000518: Javac generates duplicate name_and_type constant pool entry for class BinaryOpValueExp.java
vromero
parents:
14801
diff
changeset
|
4473 |
|
10 | 4474 |
} |
4475 |
// </editor-fold> |
|
4476 |
||
4477 |
// <editor-fold defaultstate="collapsed" desc="Visitors"> |
|
4478 |
/** |
|
4479 |
* A default visitor for types. All visitor methods except |
|
4480 |
* visitType are implemented by delegating to visitType. Concrete |
|
4481 |
* subclasses must provide an implementation of visitType and can |
|
4482 |
* override other methods as needed. |
|
4483 |
* |
|
4484 |
* @param <R> the return type of the operation implemented by this |
|
4485 |
* visitor; use Void if no return type is needed. |
|
4486 |
* @param <S> the type of the second argument (the first being the |
|
4487 |
* type itself) of the operation implemented by this visitor; use |
|
4488 |
* Void if a second argument is not needed. |
|
4489 |
*/ |
|
4490 |
public static abstract class DefaultTypeVisitor<R,S> implements Type.Visitor<R,S> { |
|
4491 |
final public R visit(Type t, S s) { return t.accept(this, s); } |
|
4492 |
public R visitClassType(ClassType t, S s) { return visitType(t, s); } |
|
4493 |
public R visitWildcardType(WildcardType t, S s) { return visitType(t, s); } |
|
4494 |
public R visitArrayType(ArrayType t, S s) { return visitType(t, s); } |
|
4495 |
public R visitMethodType(MethodType t, S s) { return visitType(t, s); } |
|
4496 |
public R visitPackageType(PackageType t, S s) { return visitType(t, s); } |
|
4497 |
public R visitTypeVar(TypeVar t, S s) { return visitType(t, s); } |
|
4498 |
public R visitCapturedType(CapturedType t, S s) { return visitType(t, s); } |
|
4499 |
public R visitForAll(ForAll t, S s) { return visitType(t, s); } |
|
4500 |
public R visitUndetVar(UndetVar t, S s) { return visitType(t, s); } |
|
4501 |
public R visitErrorType(ErrorType t, S s) { return visitType(t, s); } |
|
4502 |
} |
|
4503 |
||
4504 |
/** |
|
1357 | 4505 |
* A default visitor for symbols. All visitor methods except |
4506 |
* visitSymbol are implemented by delegating to visitSymbol. Concrete |
|
4507 |
* subclasses must provide an implementation of visitSymbol and can |
|
4508 |
* override other methods as needed. |
|
4509 |
* |
|
4510 |
* @param <R> the return type of the operation implemented by this |
|
4511 |
* visitor; use Void if no return type is needed. |
|
4512 |
* @param <S> the type of the second argument (the first being the |
|
4513 |
* symbol itself) of the operation implemented by this visitor; use |
|
4514 |
* Void if a second argument is not needed. |
|
4515 |
*/ |
|
4516 |
public static abstract class DefaultSymbolVisitor<R,S> implements Symbol.Visitor<R,S> { |
|
4517 |
final public R visit(Symbol s, S arg) { return s.accept(this, arg); } |
|
4518 |
public R visitClassSymbol(ClassSymbol s, S arg) { return visitSymbol(s, arg); } |
|
4519 |
public R visitMethodSymbol(MethodSymbol s, S arg) { return visitSymbol(s, arg); } |
|
4520 |
public R visitOperatorSymbol(OperatorSymbol s, S arg) { return visitSymbol(s, arg); } |
|
4521 |
public R visitPackageSymbol(PackageSymbol s, S arg) { return visitSymbol(s, arg); } |
|
4522 |
public R visitTypeSymbol(TypeSymbol s, S arg) { return visitSymbol(s, arg); } |
|
4523 |
public R visitVarSymbol(VarSymbol s, S arg) { return visitSymbol(s, arg); } |
|
4524 |
} |
|
4525 |
||
4526 |
/** |
|
10 | 4527 |
* A <em>simple</em> visitor for types. This visitor is simple as |
4528 |
* captured wildcards, for-all types (generic methods), and |
|
4529 |
* undetermined type variables (part of inference) are hidden. |
|
4530 |
* Captured wildcards are hidden by treating them as type |
|
4531 |
* variables and the rest are hidden by visiting their qtypes. |
|
4532 |
* |
|
4533 |
* @param <R> the return type of the operation implemented by this |
|
4534 |
* visitor; use Void if no return type is needed. |
|
4535 |
* @param <S> the type of the second argument (the first being the |
|
4536 |
* type itself) of the operation implemented by this visitor; use |
|
4537 |
* Void if a second argument is not needed. |
|
4538 |
*/ |
|
4539 |
public static abstract class SimpleVisitor<R,S> extends DefaultTypeVisitor<R,S> { |
|
4540 |
@Override |
|
4541 |
public R visitCapturedType(CapturedType t, S s) { |
|
4542 |
return visitTypeVar(t, s); |
|
4543 |
} |
|
4544 |
@Override |
|
4545 |
public R visitForAll(ForAll t, S s) { |
|
4546 |
return visit(t.qtype, s); |
|
4547 |
} |
|
4548 |
@Override |
|
4549 |
public R visitUndetVar(UndetVar t, S s) { |
|
4550 |
return visit(t.qtype, s); |
|
4551 |
} |
|
4552 |
} |
|
4553 |
||
4554 |
/** |
|
4555 |
* A plain relation on types. That is a 2-ary function on the |
|
4556 |
* form Type × Type → Boolean. |
|
4557 |
* <!-- In plain text: Type x Type -> Boolean --> |
|
4558 |
*/ |
|
4559 |
public static abstract class TypeRelation extends SimpleVisitor<Boolean,Type> {} |
|
4560 |
||
4561 |
/** |
|
4562 |
* A convenience visitor for implementing operations that only |
|
4563 |
* require one argument (the type itself), that is, unary |
|
4564 |
* operations. |
|
4565 |
* |
|
4566 |
* @param <R> the return type of the operation implemented by this |
|
4567 |
* visitor; use Void if no return type is needed. |
|
4568 |
*/ |
|
4569 |
public static abstract class UnaryVisitor<R> extends SimpleVisitor<R,Void> { |
|
4570 |
final public R visit(Type t) { return t.accept(this, null); } |
|
4571 |
} |
|
4572 |
||
4573 |
/** |
|
4574 |
* A visitor for implementing a mapping from types to types. The |
|
4575 |
* default behavior of this class is to implement the identity |
|
4576 |
* mapping (mapping a type to itself). This can be overridden in |
|
4577 |
* subclasses. |
|
4578 |
* |
|
4579 |
* @param <S> the type of the second argument (the first being the |
|
4580 |
* type itself) of this mapping; use Void if a second argument is |
|
4581 |
* not needed. |
|
4582 |
*/ |
|
4583 |
public static class MapVisitor<S> extends DefaultTypeVisitor<Type,S> { |
|
4584 |
final public Type visit(Type t) { return t.accept(this, null); } |
|
4585 |
public Type visitType(Type t, S s) { return t; } |
|
4586 |
} |
|
4587 |
// </editor-fold> |
|
6575
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6353
diff
changeset
|
4588 |
|
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6353
diff
changeset
|
4589 |
|
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6353
diff
changeset
|
4590 |
// <editor-fold defaultstate="collapsed" desc="Annotation support"> |
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6353
diff
changeset
|
4591 |
|
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6353
diff
changeset
|
4592 |
public RetentionPolicy getRetention(Attribute.Compound a) { |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
13633
diff
changeset
|
4593 |
return getRetention(a.type.tsym); |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
13633
diff
changeset
|
4594 |
} |
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
13633
diff
changeset
|
4595 |
|
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
13633
diff
changeset
|
4596 |
public RetentionPolicy getRetention(Symbol sym) { |
6575
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6353
diff
changeset
|
4597 |
RetentionPolicy vis = RetentionPolicy.CLASS; // the default |
13689
4d519199a6aa
7151010: Add compiler support for repeating annotations
jfranck
parents:
13633
diff
changeset
|
4598 |
Attribute.Compound c = sym.attribute(syms.retentionType.tsym); |
6575
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6353
diff
changeset
|
4599 |
if (c != null) { |
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6353
diff
changeset
|
4600 |
Attribute value = c.member(names.value); |
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6353
diff
changeset
|
4601 |
if (value != null && value instanceof Attribute.Enum) { |
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6353
diff
changeset
|
4602 |
Name levelName = ((Attribute.Enum)value).value.name; |
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6353
diff
changeset
|
4603 |
if (levelName == names.SOURCE) vis = RetentionPolicy.SOURCE; |
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6353
diff
changeset
|
4604 |
else if (levelName == names.CLASS) vis = RetentionPolicy.CLASS; |
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6353
diff
changeset
|
4605 |
else if (levelName == names.RUNTIME) vis = RetentionPolicy.RUNTIME; |
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6353
diff
changeset
|
4606 |
else ;// /* fail soft */ throw new AssertionError(levelName); |
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6353
diff
changeset
|
4607 |
} |
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6353
diff
changeset
|
4608 |
} |
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6353
diff
changeset
|
4609 |
return vis; |
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6353
diff
changeset
|
4610 |
} |
ae1798028008
6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents:
6353
diff
changeset
|
4611 |
// </editor-fold> |
16300 | 4612 |
|
4613 |
// <editor-fold defaultstate="collapsed" desc="Signature Generation"> |
|
4614 |
||
4615 |
public static abstract class SignatureGenerator { |
|
4616 |
||
4617 |
private final Types types; |
|
4618 |
||
4619 |
protected abstract void append(char ch); |
|
4620 |
protected abstract void append(byte[] ba); |
|
4621 |
protected abstract void append(Name name); |
|
4622 |
protected void classReference(ClassSymbol c) { /* by default: no-op */ } |
|
4623 |
||
4624 |
protected SignatureGenerator(Types types) { |
|
4625 |
this.types = types; |
|
4626 |
} |
|
4627 |
||
4628 |
/** |
|
4629 |
* Assemble signature of given type in string buffer. |
|
4630 |
*/ |
|
4631 |
public void assembleSig(Type type) { |
|
4632 |
switch (type.getTag()) { |
|
4633 |
case BYTE: |
|
4634 |
append('B'); |
|
4635 |
break; |
|
4636 |
case SHORT: |
|
4637 |
append('S'); |
|
4638 |
break; |
|
4639 |
case CHAR: |
|
4640 |
append('C'); |
|
4641 |
break; |
|
4642 |
case INT: |
|
4643 |
append('I'); |
|
4644 |
break; |
|
4645 |
case LONG: |
|
4646 |
append('J'); |
|
4647 |
break; |
|
4648 |
case FLOAT: |
|
4649 |
append('F'); |
|
4650 |
break; |
|
4651 |
case DOUBLE: |
|
4652 |
append('D'); |
|
4653 |
break; |
|
4654 |
case BOOLEAN: |
|
4655 |
append('Z'); |
|
4656 |
break; |
|
4657 |
case VOID: |
|
4658 |
append('V'); |
|
4659 |
break; |
|
4660 |
case CLASS: |
|
4661 |
append('L'); |
|
4662 |
assembleClassSig(type); |
|
4663 |
append(';'); |
|
4664 |
break; |
|
4665 |
case ARRAY: |
|
4666 |
ArrayType at = (ArrayType) type; |
|
4667 |
append('['); |
|
4668 |
assembleSig(at.elemtype); |
|
4669 |
break; |
|
4670 |
case METHOD: |
|
4671 |
MethodType mt = (MethodType) type; |
|
4672 |
append('('); |
|
4673 |
assembleSig(mt.argtypes); |
|
4674 |
append(')'); |
|
4675 |
assembleSig(mt.restype); |
|
4676 |
if (hasTypeVar(mt.thrown)) { |
|
4677 |
for (List<Type> l = mt.thrown; l.nonEmpty(); l = l.tail) { |
|
4678 |
append('^'); |
|
4679 |
assembleSig(l.head); |
|
4680 |
} |
|
4681 |
} |
|
4682 |
break; |
|
4683 |
case WILDCARD: { |
|
4684 |
Type.WildcardType ta = (Type.WildcardType) type; |
|
4685 |
switch (ta.kind) { |
|
4686 |
case SUPER: |
|
4687 |
append('-'); |
|
4688 |
assembleSig(ta.type); |
|
4689 |
break; |
|
4690 |
case EXTENDS: |
|
4691 |
append('+'); |
|
4692 |
assembleSig(ta.type); |
|
4693 |
break; |
|
4694 |
case UNBOUND: |
|
4695 |
append('*'); |
|
4696 |
break; |
|
4697 |
default: |
|
4698 |
throw new AssertionError(ta.kind); |
|
4699 |
} |
|
4700 |
break; |
|
4701 |
} |
|
4702 |
case TYPEVAR: |
|
4703 |
append('T'); |
|
4704 |
append(type.tsym.name); |
|
4705 |
append(';'); |
|
4706 |
break; |
|
4707 |
case FORALL: |
|
4708 |
Type.ForAll ft = (Type.ForAll) type; |
|
4709 |
assembleParamsSig(ft.tvars); |
|
4710 |
assembleSig(ft.qtype); |
|
4711 |
break; |
|
4712 |
default: |
|
4713 |
throw new AssertionError("typeSig " + type.getTag()); |
|
4714 |
} |
|
4715 |
} |
|
4716 |
||
4717 |
public boolean hasTypeVar(List<Type> l) { |
|
4718 |
while (l.nonEmpty()) { |
|
4719 |
if (l.head.hasTag(TypeTag.TYPEVAR)) { |
|
4720 |
return true; |
|
4721 |
} |
|
4722 |
l = l.tail; |
|
4723 |
} |
|
4724 |
return false; |
|
4725 |
} |
|
4726 |
||
4727 |
public void assembleClassSig(Type type) { |
|
4728 |
ClassType ct = (ClassType) type; |
|
4729 |
ClassSymbol c = (ClassSymbol) ct.tsym; |
|
4730 |
classReference(c); |
|
4731 |
Type outer = ct.getEnclosingType(); |
|
4732 |
if (outer.allparams().nonEmpty()) { |
|
4733 |
boolean rawOuter = |
|
4734 |
c.owner.kind == Kinds.MTH || // either a local class |
|
4735 |
c.name == types.names.empty; // or anonymous |
|
4736 |
assembleClassSig(rawOuter |
|
4737 |
? types.erasure(outer) |
|
4738 |
: outer); |
|
4739 |
append('.'); |
|
4740 |
Assert.check(c.flatname.startsWith(c.owner.enclClass().flatname)); |
|
4741 |
append(rawOuter |
|
4742 |
? c.flatname.subName(c.owner.enclClass().flatname.getByteLength() + 1, c.flatname.getByteLength()) |
|
4743 |
: c.name); |
|
4744 |
} else { |
|
4745 |
append(externalize(c.flatname)); |
|
4746 |
} |
|
4747 |
if (ct.getTypeArguments().nonEmpty()) { |
|
4748 |
append('<'); |
|
4749 |
assembleSig(ct.getTypeArguments()); |
|
4750 |
append('>'); |
|
4751 |
} |
|
4752 |
} |
|
4753 |
||
4754 |
public void assembleParamsSig(List<Type> typarams) { |
|
4755 |
append('<'); |
|
4756 |
for (List<Type> ts = typarams; ts.nonEmpty(); ts = ts.tail) { |
|
4757 |
Type.TypeVar tvar = (Type.TypeVar) ts.head; |
|
4758 |
append(tvar.tsym.name); |
|
4759 |
List<Type> bounds = types.getBounds(tvar); |
|
4760 |
if ((bounds.head.tsym.flags() & INTERFACE) != 0) { |
|
4761 |
append(':'); |
|
4762 |
} |
|
4763 |
for (List<Type> l = bounds; l.nonEmpty(); l = l.tail) { |
|
4764 |
append(':'); |
|
4765 |
assembleSig(l.head); |
|
4766 |
} |
|
4767 |
} |
|
4768 |
append('>'); |
|
4769 |
} |
|
4770 |
||
4771 |
private void assembleSig(List<Type> types) { |
|
4772 |
for (List<Type> ts = types; ts.nonEmpty(); ts = ts.tail) { |
|
4773 |
assembleSig(ts.head); |
|
4774 |
} |
|
4775 |
} |
|
4776 |
} |
|
4777 |
// </editor-fold> |
|
23810
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
23399
diff
changeset
|
4778 |
|
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
23399
diff
changeset
|
4779 |
public void newRound() { |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
23399
diff
changeset
|
4780 |
descCache._map.clear(); |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
23399
diff
changeset
|
4781 |
isDerivedRawCache.clear(); |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
23399
diff
changeset
|
4782 |
implCache._map.clear(); |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
23399
diff
changeset
|
4783 |
membersCache._map.clear(); |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
23399
diff
changeset
|
4784 |
closureCache.clear(); |
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
23399
diff
changeset
|
4785 |
} |
10 | 4786 |
} |