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