author | vromero |
Sat, 22 Feb 2014 17:42:10 +0000 | |
changeset 23124 | 63050cb08583 |
parent 22165 | ec53c8946fc2 |
child 23132 | 00a059740d87 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
23124
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
2 |
* Copyright (c) 1999, 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.comp; |
|
27 |
||
6592
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
28 |
import com.sun.tools.javac.tree.JCTree; |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
29 |
import com.sun.tools.javac.tree.JCTree.JCTypeCast; |
8036
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
30 |
import com.sun.tools.javac.tree.TreeInfo; |
10 | 31 |
import com.sun.tools.javac.util.*; |
15717 | 32 |
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
10 | 33 |
import com.sun.tools.javac.util.List; |
15717 | 34 |
import com.sun.tools.javac.code.*; |
35 |
import com.sun.tools.javac.code.Type.*; |
|
36 |
import com.sun.tools.javac.code.Type.UndetVar.InferenceBound; |
|
37 |
import com.sun.tools.javac.code.Symbol.*; |
|
38 |
import com.sun.tools.javac.comp.DeferredAttr.AttrMode; |
|
39 |
import com.sun.tools.javac.comp.Infer.GraphSolver.InferenceGraph; |
|
40 |
import com.sun.tools.javac.comp.Infer.GraphSolver.InferenceGraph.Node; |
|
41 |
import com.sun.tools.javac.comp.Resolve.InapplicableMethodException; |
|
42 |
import com.sun.tools.javac.comp.Resolve.VerboseResolutionMode; |
|
19914 | 43 |
import com.sun.tools.javac.util.GraphUtils.TarjanNode; |
15717 | 44 |
|
45 |
import java.util.ArrayList; |
|
46 |
import java.util.Collections; |
|
19914 | 47 |
import java.util.EnumMap; |
15717 | 48 |
import java.util.EnumSet; |
19914 | 49 |
import java.util.HashMap; |
15717 | 50 |
import java.util.HashSet; |
19914 | 51 |
import java.util.LinkedHashSet; |
52 |
import java.util.Map; |
|
53 |
import java.util.Set; |
|
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
54 |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
55 |
import static com.sun.tools.javac.code.TypeTag.*; |
10 | 56 |
|
57 |
/** Helper class for type parameter inference, used by the attribution phase. |
|
58 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5846
diff
changeset
|
59 |
* <p><b>This is NOT part of any supported API. |
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5846
diff
changeset
|
60 |
* If you write code that depends on this, you do so at your own risk. |
10 | 61 |
* This code and its internal interfaces are subject to change or |
62 |
* deletion without notice.</b> |
|
63 |
*/ |
|
64 |
public class Infer { |
|
22163 | 65 |
protected static final Context.Key<Infer> inferKey = new Context.Key<>(); |
10 | 66 |
|
15717 | 67 |
Resolve rs; |
68 |
Check chk; |
|
10 | 69 |
Symtab syms; |
70 |
Types types; |
|
15717 | 71 |
JCDiagnostic.Factory diags; |
10816
ce8a7e9d8882
7098660: Write better overload resolution/inference tests
mcimadamore
parents:
10628
diff
changeset
|
72 |
Log log; |
10 | 73 |
|
15717 | 74 |
/** should the graph solver be used? */ |
75 |
boolean allowGraphInference; |
|
15374
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
15037
diff
changeset
|
76 |
|
10 | 77 |
public static Infer instance(Context context) { |
78 |
Infer instance = context.get(inferKey); |
|
79 |
if (instance == null) |
|
80 |
instance = new Infer(context); |
|
81 |
return instance; |
|
82 |
} |
|
83 |
||
84 |
protected Infer(Context context) { |
|
85 |
context.put(inferKey, this); |
|
15717 | 86 |
|
87 |
rs = Resolve.instance(context); |
|
88 |
chk = Check.instance(context); |
|
10 | 89 |
syms = Symtab.instance(context); |
90 |
types = Types.instance(context); |
|
15717 | 91 |
diags = JCDiagnostic.Factory.instance(context); |
10816
ce8a7e9d8882
7098660: Write better overload resolution/inference tests
mcimadamore
parents:
10628
diff
changeset
|
92 |
log = Log.instance(context); |
13440
1ea85052f98d
7187104: Inference cleanup: remove redundant exception classes in Infer.java
mcimadamore
parents:
13438
diff
changeset
|
93 |
inferenceException = new InferenceException(diags); |
15717 | 94 |
Options options = Options.instance(context); |
95 |
allowGraphInference = Source.instance(context).allowGraphInference() |
|
96 |
&& options.isUnset("useLegacyInference"); |
|
10 | 97 |
} |
98 |
||
15717 | 99 |
/** A value for prototypes that admit any type, including polymorphic ones. */ |
18646
e628560a86d1
8017104: javac should have a class for primitive types that inherits from Type
vromero
parents:
18395
diff
changeset
|
100 |
public static final Type anyPoly = new JCNoType(); |
15717 | 101 |
|
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
102 |
/** |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
103 |
* This exception class is design to store a list of diagnostics corresponding |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
104 |
* to inference errors that can arise during a method applicability check. |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
105 |
*/ |
11707
532f41763bc9
7129801: Merge the two method applicability routines
mcimadamore
parents:
11550
diff
changeset
|
106 |
public static class InferenceException extends InapplicableMethodException { |
10 | 107 |
private static final long serialVersionUID = 0; |
108 |
||
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
109 |
List<JCDiagnostic> messages = List.nil(); |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
110 |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
111 |
InferenceException(JCDiagnostic.Factory diags) { |
6710
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
112 |
super(diags); |
10 | 113 |
} |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
114 |
|
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
115 |
@Override |
19914 | 116 |
InapplicableMethodException setMessage() { |
117 |
//no message to set |
|
118 |
return this; |
|
119 |
} |
|
120 |
||
121 |
@Override |
|
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
122 |
InapplicableMethodException setMessage(JCDiagnostic diag) { |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
123 |
messages = messages.append(diag); |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
124 |
return this; |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
125 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
126 |
|
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
127 |
@Override |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
128 |
public JCDiagnostic getDiagnostic() { |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
129 |
return messages.head; |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
130 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
131 |
|
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
132 |
void clear() { |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
133 |
messages = List.nil(); |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
134 |
} |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
135 |
} |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
136 |
|
15717 | 137 |
protected final InferenceException inferenceException; |
10 | 138 |
|
15717 | 139 |
// <editor-fold defaultstate="collapsed" desc="Inference routines"> |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
140 |
/** |
15717 | 141 |
* Main inference entry point - instantiate a generic method type |
142 |
* using given argument types and (possibly) an expected target-type. |
|
10 | 143 |
*/ |
12915
28cf1e0dafdc
7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents:
12468
diff
changeset
|
144 |
public Type instantiateMethod(Env<AttrContext> env, |
5489
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
3778
diff
changeset
|
145 |
List<Type> tvars, |
10 | 146 |
MethodType mt, |
12915
28cf1e0dafdc
7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents:
12468
diff
changeset
|
147 |
Attr.ResultInfo resultInfo, |
28cf1e0dafdc
7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents:
12468
diff
changeset
|
148 |
Symbol msym, |
28cf1e0dafdc
7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents:
12468
diff
changeset
|
149 |
List<Type> argtypes, |
28cf1e0dafdc
7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents:
12468
diff
changeset
|
150 |
boolean allowBoxing, |
28cf1e0dafdc
7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents:
12468
diff
changeset
|
151 |
boolean useVarargs, |
14057
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
152 |
Resolve.MethodResolutionContext resolveContext, |
12915
28cf1e0dafdc
7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents:
12468
diff
changeset
|
153 |
Warner warn) throws InferenceException { |
10 | 154 |
//-System.err.println("instantiateMethod(" + tvars + ", " + mt + ", " + argtypes + ")"); //DEBUG |
15705 | 155 |
final InferenceContext inferenceContext = new InferenceContext(tvars); |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
156 |
inferenceException.clear(); |
15717 | 157 |
try { |
158 |
DeferredAttr.DeferredAttrContext deferredAttrContext = |
|
18910
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
159 |
resolveContext.deferredAttrContext(msym, inferenceContext, resultInfo, warn); |
6710
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
160 |
|
16805
b419336382fd
8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents:
16567
diff
changeset
|
161 |
resolveContext.methodCheck.argumentsAcceptable(env, deferredAttrContext, |
15717 | 162 |
argtypes, mt.getParameterTypes(), warn); |
15037
8ba16ac61b1a
8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents:
14547
diff
changeset
|
163 |
|
15717 | 164 |
if (allowGraphInference && |
165 |
resultInfo != null && |
|
15374
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
15037
diff
changeset
|
166 |
!warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED)) { |
15717 | 167 |
//inject return constraints earlier |
168 |
checkWithinBounds(inferenceContext, warn); //propagation |
|
18911 | 169 |
Type newRestype = generateReturnConstraints(resultInfo, mt, inferenceContext); |
170 |
mt = (MethodType)types.createMethodTypeWithReturn(mt, newRestype); |
|
15717 | 171 |
//propagate outwards if needed |
172 |
if (resultInfo.checkContext.inferenceContext().free(resultInfo.pt)) { |
|
173 |
//propagate inference context outwards and exit |
|
174 |
inferenceContext.dupTo(resultInfo.checkContext.inferenceContext()); |
|
175 |
deferredAttrContext.complete(); |
|
176 |
return mt; |
|
177 |
} |
|
15374
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
15037
diff
changeset
|
178 |
} |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
15037
diff
changeset
|
179 |
|
15037
8ba16ac61b1a
8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents:
14547
diff
changeset
|
180 |
deferredAttrContext.complete(); |
10 | 181 |
|
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
182 |
// minimize as yet undetermined type variables |
15717 | 183 |
if (allowGraphInference) { |
184 |
inferenceContext.solve(warn); |
|
185 |
} else { |
|
186 |
inferenceContext.solveLegacy(true, warn, LegacyInferenceSteps.EQ_LOWER.steps); //minimizeInst |
|
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
187 |
} |
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
188 |
|
15705 | 189 |
mt = (MethodType)inferenceContext.asInstType(mt); |
10 | 190 |
|
15717 | 191 |
if (!allowGraphInference && |
192 |
inferenceContext.restvars().nonEmpty() && |
|
193 |
resultInfo != null && |
|
194 |
!warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED)) { |
|
195 |
generateReturnConstraints(resultInfo, mt, inferenceContext); |
|
196 |
inferenceContext.solveLegacy(false, warn, LegacyInferenceSteps.EQ_UPPER.steps); //maximizeInst |
|
197 |
mt = (MethodType)inferenceContext.asInstType(mt); |
|
198 |
} |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
199 |
|
15717 | 200 |
if (resultInfo != null && rs.verboseResolutionMode.contains(VerboseResolutionMode.DEFERRED_INST)) { |
201 |
log.note(env.tree.pos, "deferred.method.inst", msym, mt, resultInfo.pt); |
|
12915
28cf1e0dafdc
7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents:
12468
diff
changeset
|
202 |
} |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
203 |
|
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
204 |
// return instantiated version of method type |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
205 |
return mt; |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
206 |
} finally { |
15717 | 207 |
if (resultInfo != null || !allowGraphInference) { |
208 |
inferenceContext.notifyChange(); |
|
209 |
} else { |
|
210 |
inferenceContext.notifyChange(inferenceContext.boundedVars()); |
|
14048
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
14047
diff
changeset
|
211 |
} |
10 | 212 |
} |
8616
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
213 |
} |
10 | 214 |
|
15717 | 215 |
/** |
216 |
* Generate constraints from the generic method's return type. If the method |
|
217 |
* call occurs in a context where a type T is expected, use the expected |
|
218 |
* type to derive more constraints on the generic method inference variables. |
|
219 |
*/ |
|
18911 | 220 |
Type generateReturnConstraints(Attr.ResultInfo resultInfo, |
15717 | 221 |
MethodType mt, InferenceContext inferenceContext) { |
18911 | 222 |
Type from = mt.getReturnType(); |
223 |
if (mt.getReturnType().containsAny(inferenceContext.inferencevars) && |
|
224 |
resultInfo.checkContext.inferenceContext() != emptyContext) { |
|
225 |
from = types.capture(from); |
|
226 |
//add synthetic captured ivars |
|
227 |
for (Type t : from.getTypeArguments()) { |
|
228 |
if (t.hasTag(TYPEVAR) && ((TypeVar)t).isCaptured()) { |
|
229 |
inferenceContext.addVar((TypeVar)t); |
|
230 |
} |
|
231 |
} |
|
232 |
} |
|
233 |
Type qtype1 = inferenceContext.asFree(from); |
|
15717 | 234 |
Type to = returnConstraintTarget(qtype1, resultInfo.pt); |
235 |
Assert.check(allowGraphInference || !resultInfo.checkContext.inferenceContext().free(to), |
|
236 |
"legacy inference engine cannot handle constraints on both sides of a subtyping assertion"); |
|
237 |
//we need to skip capture? |
|
238 |
Warner retWarn = new Warner(); |
|
239 |
if (!resultInfo.checkContext.compatible(qtype1, resultInfo.checkContext.inferenceContext().asFree(to), retWarn) || |
|
18008
6d75e3886bac
8015505: Spurious inference error when return type of generic method requires unchecked conversion to target
mcimadamore
parents:
16967
diff
changeset
|
240 |
//unchecked conversion is not allowed in source 7 mode |
6d75e3886bac
8015505: Spurious inference error when return type of generic method requires unchecked conversion to target
mcimadamore
parents:
16967
diff
changeset
|
241 |
(!allowGraphInference && retWarn.hasLint(Lint.LintCategory.UNCHECKED))) { |
15717 | 242 |
throw inferenceException |
243 |
.setMessage("infer.no.conforming.instance.exists", |
|
244 |
inferenceContext.restvars(), mt.getReturnType(), to); |
|
245 |
} |
|
18911 | 246 |
return from; |
15717 | 247 |
} |
18910
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
248 |
|
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
249 |
Type returnConstraintTarget(Type from, Type to) { |
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
250 |
if (from.hasTag(VOID)) { |
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
251 |
return syms.voidType; |
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
252 |
} else if (to.hasTag(NONE)) { |
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
253 |
return from.isPrimitive() ? from : syms.objectType; |
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
254 |
} else if (from.hasTag(UNDETVAR) && to.isPrimitive()) { |
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
255 |
if (!allowGraphInference) { |
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
256 |
//if legacy, just return boxed type |
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
257 |
return types.boxedClass(to).type; |
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
258 |
} |
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
259 |
//if graph inference we need to skip conflicting boxed bounds... |
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
260 |
UndetVar uv = (UndetVar)from; |
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
261 |
for (Type t : uv.getBounds(InferenceBound.EQ, InferenceBound.LOWER)) { |
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
262 |
Type boundAsPrimitive = types.unboxedType(t); |
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
263 |
if (boundAsPrimitive == null) continue; |
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
264 |
if (types.isConvertible(boundAsPrimitive, to)) { |
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
265 |
//effectively skip return-type constraint generation (compatibility) |
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
266 |
return syms.objectType; |
15717 | 267 |
} |
12468
1100643c0209
7154127: Inference cleanup: remove bound check analysis from visitors in Types.java
mcimadamore
parents:
12335
diff
changeset
|
268 |
} |
18910
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
269 |
return types.boxedClass(to).type; |
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
270 |
} else { |
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
271 |
return to; |
10 | 272 |
} |
18910
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
273 |
} |
12468
1100643c0209
7154127: Inference cleanup: remove bound check analysis from visitors in Types.java
mcimadamore
parents:
12335
diff
changeset
|
274 |
|
15717 | 275 |
/** |
276 |
* Infer cyclic inference variables as described in 15.12.2.8. |
|
277 |
*/ |
|
278 |
private void instantiateAsUninferredVars(List<Type> vars, InferenceContext inferenceContext) { |
|
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19914
diff
changeset
|
279 |
ListBuffer<Type> todo = new ListBuffer<>(); |
15717 | 280 |
//step 1 - create fresh tvars |
281 |
for (Type t : vars) { |
|
282 |
UndetVar uv = (UndetVar)inferenceContext.asFree(t); |
|
283 |
List<Type> upperBounds = uv.getBounds(InferenceBound.UPPER); |
|
284 |
if (Type.containsAny(upperBounds, vars)) { |
|
16967
79d444669f3f
7015104: use new subtype of TypeSymbol for type parameters
jfranck
parents:
16805
diff
changeset
|
285 |
TypeSymbol fresh_tvar = new TypeVariableSymbol(Flags.SYNTHETIC, uv.qtype.tsym.name, null, uv.qtype.tsym.owner); |
15717 | 286 |
fresh_tvar.type = new TypeVar(fresh_tvar, types.makeCompoundType(uv.getBounds(InferenceBound.UPPER)), null); |
287 |
todo.append(uv); |
|
288 |
uv.inst = fresh_tvar.type; |
|
289 |
} else if (upperBounds.nonEmpty()) { |
|
290 |
uv.inst = types.glb(upperBounds); |
|
291 |
} else { |
|
292 |
uv.inst = syms.objectType; |
|
12468
1100643c0209
7154127: Inference cleanup: remove bound check analysis from visitors in Types.java
mcimadamore
parents:
12335
diff
changeset
|
293 |
} |
15717 | 294 |
} |
295 |
//step 2 - replace fresh tvars in their bounds |
|
296 |
List<Type> formals = vars; |
|
297 |
for (Type t : todo) { |
|
298 |
UndetVar uv = (UndetVar)t; |
|
299 |
TypeVar ct = (TypeVar)uv.inst; |
|
300 |
ct.bound = types.glb(inferenceContext.asInstTypes(types.getBounds(ct))); |
|
301 |
if (ct.bound.isErroneous()) { |
|
302 |
//report inference error if glb fails |
|
303 |
reportBoundError(uv, BoundErrorKind.BAD_UPPER); |
|
15374
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
15037
diff
changeset
|
304 |
} |
15717 | 305 |
formals = formals.tail; |
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
306 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
307 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
308 |
|
6592
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
309 |
/** |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
310 |
* Compute a synthetic method type corresponding to the requested polymorphic |
8036
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
311 |
* method signature. The target return type is computed from the immediately |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
312 |
* enclosing scope surrounding the polymorphic-signature call. |
6592
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
313 |
*/ |
12335
4725d88691dd
7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents:
12334
diff
changeset
|
314 |
Type instantiatePolymorphicSignatureInstance(Env<AttrContext> env, |
6592
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
315 |
MethodSymbol spMethod, // sig. poly. method or null if none |
14057
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
316 |
Resolve.MethodResolutionContext resolveContext, |
8036
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
317 |
List<Type> argtypes) { |
6592
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
318 |
final Type restype; |
8036
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
319 |
|
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
320 |
//The return type for a polymorphic signature call is computed from |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
321 |
//the enclosing tree E, as follows: if E is a cast, then use the |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
322 |
//target type of the cast expression as a return type; if E is an |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
323 |
//expression statement, the return type is 'void' - otherwise the |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
324 |
//return type is simply 'Object'. A correctness check ensures that |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
325 |
//env.next refers to the lexically enclosing environment in which |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
326 |
//the polymorphic signature call environment is nested. |
6934
258e5f06880f
6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents:
6710
diff
changeset
|
327 |
|
8036
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
328 |
switch (env.next.tree.getTag()) { |
10950 | 329 |
case TYPECAST: |
8036
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
330 |
JCTypeCast castTree = (JCTypeCast)env.next.tree; |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
331 |
restype = (TreeInfo.skipParens(castTree.expr) == env.tree) ? |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
332 |
castTree.clazz.type : |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
333 |
syms.objectType; |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
334 |
break; |
10950 | 335 |
case EXEC: |
8036
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
336 |
JCTree.JCExpressionStatement execTree = |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
337 |
(JCTree.JCExpressionStatement)env.next.tree; |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
338 |
restype = (TreeInfo.skipParens(execTree.expr) == env.tree) ? |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
339 |
syms.voidType : |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
340 |
syms.objectType; |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
341 |
break; |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
342 |
default: |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
343 |
restype = syms.objectType; |
6592
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
344 |
} |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
345 |
|
14057
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
346 |
List<Type> paramtypes = Type.map(argtypes, new ImplicitArgType(spMethod, resolveContext.step)); |
6592
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
347 |
List<Type> exType = spMethod != null ? |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
348 |
spMethod.getThrownTypes() : |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
349 |
List.of(syms.throwableType); // make it throw all exceptions |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
350 |
|
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
351 |
MethodType mtype = new MethodType(paramtypes, |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
352 |
restype, |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
353 |
exType, |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
354 |
syms.methodClass); |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
355 |
return mtype; |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
356 |
} |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
357 |
//where |
14057
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
358 |
class ImplicitArgType extends DeferredAttr.DeferredTypeMap { |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
359 |
|
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
360 |
public ImplicitArgType(Symbol msym, Resolve.MethodResolutionPhase phase) { |
15717 | 361 |
rs.deferredAttr.super(AttrMode.SPECULATIVE, msym, phase); |
14057
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
362 |
} |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
363 |
|
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
364 |
public Type apply(Type t) { |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
365 |
t = types.erasure(super.apply(t)); |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
366 |
if (t.hasTag(BOT)) |
14057
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
367 |
// nulls type as the marker type Null (which has no instances) |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
368 |
// infer as java.lang.Void for now |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
369 |
t = types.boxedClass(syms.voidType).type; |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
370 |
return t; |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
371 |
} |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
372 |
} |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
373 |
|
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
374 |
/** |
15717 | 375 |
* This method is used to infer a suitable target SAM in case the original |
376 |
* SAM type contains one or more wildcards. An inference process is applied |
|
377 |
* so that wildcard bounds, as well as explicit lambda/method ref parameters |
|
378 |
* (where applicable) are used to constraint the solution. |
|
379 |
*/ |
|
380 |
public Type instantiateFunctionalInterface(DiagnosticPosition pos, Type funcInterface, |
|
381 |
List<Type> paramTypes, Check.CheckContext checkContext) { |
|
382 |
if (types.capture(funcInterface) == funcInterface) { |
|
383 |
//if capture doesn't change the type then return the target unchanged |
|
384 |
//(this means the target contains no wildcards!) |
|
385 |
return funcInterface; |
|
386 |
} else { |
|
387 |
Type formalInterface = funcInterface.tsym.type; |
|
388 |
InferenceContext funcInterfaceContext = |
|
389 |
new InferenceContext(funcInterface.tsym.type.getTypeArguments()); |
|
390 |
||
391 |
Assert.check(paramTypes != null); |
|
392 |
//get constraints from explicit params (this is done by |
|
393 |
//checking that explicit param types are equal to the ones |
|
394 |
//in the functional interface descriptors) |
|
395 |
List<Type> descParameterTypes = types.findDescriptorType(formalInterface).getParameterTypes(); |
|
396 |
if (descParameterTypes.size() != paramTypes.size()) { |
|
397 |
checkContext.report(pos, diags.fragment("incompatible.arg.types.in.lambda")); |
|
398 |
return types.createErrorType(funcInterface); |
|
399 |
} |
|
400 |
for (Type p : descParameterTypes) { |
|
401 |
if (!types.isSameType(funcInterfaceContext.asFree(p), paramTypes.head)) { |
|
402 |
checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface)); |
|
403 |
return types.createErrorType(funcInterface); |
|
404 |
} |
|
405 |
paramTypes = paramTypes.tail; |
|
406 |
} |
|
407 |
||
408 |
try { |
|
409 |
funcInterfaceContext.solve(funcInterfaceContext.boundedVars(), types.noWarnings); |
|
410 |
} catch (InferenceException ex) { |
|
411 |
checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface)); |
|
412 |
} |
|
413 |
||
414 |
List<Type> actualTypeargs = funcInterface.getTypeArguments(); |
|
415 |
for (Type t : funcInterfaceContext.undetvars) { |
|
416 |
UndetVar uv = (UndetVar)t; |
|
417 |
if (uv.inst == null) { |
|
418 |
uv.inst = actualTypeargs.head; |
|
419 |
} |
|
420 |
actualTypeargs = actualTypeargs.tail; |
|
421 |
} |
|
422 |
||
423 |
Type owntype = funcInterfaceContext.asInstType(formalInterface); |
|
424 |
if (!chk.checkValidGenericType(owntype)) { |
|
425 |
//if the inferred functional interface type is not well-formed, |
|
426 |
//or if it's not a subtype of the original target, issue an error |
|
427 |
checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface)); |
|
428 |
} |
|
429 |
return owntype; |
|
430 |
} |
|
431 |
} |
|
432 |
// </editor-fold> |
|
433 |
||
434 |
// <editor-fold defaultstate="collapsed" desc="Bound checking"> |
|
435 |
/** |
|
436 |
* Check bounds and perform incorporation |
|
437 |
*/ |
|
438 |
void checkWithinBounds(InferenceContext inferenceContext, |
|
439 |
Warner warn) throws InferenceException { |
|
440 |
MultiUndetVarListener mlistener = new MultiUndetVarListener(inferenceContext.undetvars); |
|
18904
3c4c6457ec5b
8019824: very long error messages on inference error
mcimadamore
parents:
18646
diff
changeset
|
441 |
List<Type> saved_undet = inferenceContext.save(); |
15717 | 442 |
try { |
443 |
while (true) { |
|
444 |
mlistener.reset(); |
|
445 |
if (!allowGraphInference) { |
|
446 |
//in legacy mode we lack of transitivity, so bound check |
|
447 |
//cannot be run in parallel with other incoprporation rounds |
|
448 |
for (Type t : inferenceContext.undetvars) { |
|
449 |
UndetVar uv = (UndetVar)t; |
|
450 |
IncorporationStep.CHECK_BOUNDS.apply(uv, inferenceContext, warn); |
|
451 |
} |
|
452 |
} |
|
453 |
for (Type t : inferenceContext.undetvars) { |
|
454 |
UndetVar uv = (UndetVar)t; |
|
455 |
//bound incorporation |
|
456 |
EnumSet<IncorporationStep> incorporationSteps = allowGraphInference ? |
|
457 |
incorporationStepsGraph : incorporationStepsLegacy; |
|
458 |
for (IncorporationStep is : incorporationSteps) { |
|
18911 | 459 |
if (is.accepts(uv, inferenceContext)) { |
460 |
is.apply(uv, inferenceContext, warn); |
|
461 |
} |
|
15717 | 462 |
} |
463 |
} |
|
464 |
if (!mlistener.changed || !allowGraphInference) break; |
|
465 |
} |
|
466 |
} |
|
467 |
finally { |
|
468 |
mlistener.detach(); |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
469 |
if (incorporationCache.size() == MAX_INCORPORATION_STEPS) { |
18904
3c4c6457ec5b
8019824: very long error messages on inference error
mcimadamore
parents:
18646
diff
changeset
|
470 |
inferenceContext.rollback(saved_undet); |
3c4c6457ec5b
8019824: very long error messages on inference error
mcimadamore
parents:
18646
diff
changeset
|
471 |
} |
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
472 |
incorporationCache.clear(); |
15717 | 473 |
} |
474 |
} |
|
475 |
//where |
|
476 |
/** |
|
477 |
* This listener keeps track of changes on a group of inference variable |
|
478 |
* bounds. Note: the listener must be detached (calling corresponding |
|
479 |
* method) to make sure that the underlying inference variable is |
|
480 |
* left in a clean state. |
|
481 |
*/ |
|
482 |
class MultiUndetVarListener implements UndetVar.UndetVarListener { |
|
483 |
||
484 |
boolean changed; |
|
485 |
List<Type> undetvars; |
|
486 |
||
487 |
public MultiUndetVarListener(List<Type> undetvars) { |
|
488 |
this.undetvars = undetvars; |
|
489 |
for (Type t : undetvars) { |
|
490 |
UndetVar uv = (UndetVar)t; |
|
491 |
uv.listener = this; |
|
492 |
} |
|
493 |
} |
|
494 |
||
495 |
public void varChanged(UndetVar uv, Set<InferenceBound> ibs) { |
|
496 |
//avoid non-termination |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
497 |
if (incorporationCache.size() < MAX_INCORPORATION_STEPS) { |
15717 | 498 |
changed = true; |
499 |
} |
|
500 |
} |
|
501 |
||
502 |
void reset() { |
|
503 |
changed = false; |
|
504 |
} |
|
505 |
||
506 |
void detach() { |
|
507 |
for (Type t : undetvars) { |
|
508 |
UndetVar uv = (UndetVar)t; |
|
509 |
uv.listener = null; |
|
510 |
} |
|
511 |
} |
|
22163 | 512 |
} |
15717 | 513 |
|
22163 | 514 |
/** max number of incorporation rounds */ |
15717 | 515 |
static final int MAX_INCORPORATION_STEPS = 100; |
516 |
||
517 |
/** |
|
518 |
* This enumeration defines an entry point for doing inference variable |
|
519 |
* bound incorporation - it can be used to inject custom incorporation |
|
520 |
* logic into the basic bound checking routine |
|
521 |
*/ |
|
522 |
enum IncorporationStep { |
|
523 |
/** |
|
524 |
* Performs basic bound checking - i.e. is the instantiated type for a given |
|
525 |
* inference variable compatible with its bounds? |
|
526 |
*/ |
|
527 |
CHECK_BOUNDS() { |
|
528 |
public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
|
529 |
Infer infer = inferenceContext.infer(); |
|
530 |
uv.substBounds(inferenceContext.inferenceVars(), inferenceContext.instTypes(), infer.types); |
|
531 |
infer.checkCompatibleUpperBounds(uv, inferenceContext); |
|
532 |
if (uv.inst != null) { |
|
533 |
Type inst = uv.inst; |
|
534 |
for (Type u : uv.getBounds(InferenceBound.UPPER)) { |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
535 |
if (!isSubtype(inst, inferenceContext.asFree(u), warn, infer)) { |
15717 | 536 |
infer.reportBoundError(uv, BoundErrorKind.UPPER); |
537 |
} |
|
538 |
} |
|
539 |
for (Type l : uv.getBounds(InferenceBound.LOWER)) { |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
540 |
if (!isSubtype(inferenceContext.asFree(l), inst, warn, infer)) { |
15717 | 541 |
infer.reportBoundError(uv, BoundErrorKind.LOWER); |
542 |
} |
|
543 |
} |
|
544 |
for (Type e : uv.getBounds(InferenceBound.EQ)) { |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
545 |
if (!isSameType(inst, inferenceContext.asFree(e), infer)) { |
15717 | 546 |
infer.reportBoundError(uv, BoundErrorKind.EQ); |
547 |
} |
|
548 |
} |
|
549 |
} |
|
550 |
} |
|
18911 | 551 |
@Override |
552 |
boolean accepts(UndetVar uv, InferenceContext inferenceContext) { |
|
553 |
//applies to all undetvars |
|
554 |
return true; |
|
555 |
} |
|
15717 | 556 |
}, |
557 |
/** |
|
558 |
* Check consistency of equality constraints. This is a slightly more aggressive |
|
559 |
* inference routine that is designed as to maximize compatibility with JDK 7. |
|
560 |
* Note: this is not used in graph mode. |
|
561 |
*/ |
|
562 |
EQ_CHECK_LEGACY() { |
|
563 |
public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
|
564 |
Infer infer = inferenceContext.infer(); |
|
565 |
Type eq = null; |
|
566 |
for (Type e : uv.getBounds(InferenceBound.EQ)) { |
|
567 |
Assert.check(!inferenceContext.free(e)); |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
568 |
if (eq != null && !isSameType(e, eq, infer)) { |
15717 | 569 |
infer.reportBoundError(uv, BoundErrorKind.EQ); |
570 |
} |
|
571 |
eq = e; |
|
572 |
for (Type l : uv.getBounds(InferenceBound.LOWER)) { |
|
573 |
Assert.check(!inferenceContext.free(l)); |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
574 |
if (!isSubtype(l, e, warn, infer)) { |
15717 | 575 |
infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_LOWER); |
576 |
} |
|
577 |
} |
|
578 |
for (Type u : uv.getBounds(InferenceBound.UPPER)) { |
|
579 |
if (inferenceContext.free(u)) continue; |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
580 |
if (!isSubtype(e, u, warn, infer)) { |
15717 | 581 |
infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_UPPER); |
582 |
} |
|
583 |
} |
|
584 |
} |
|
585 |
} |
|
23124
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
586 |
|
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
587 |
@Override |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
588 |
boolean accepts(UndetVar uv, InferenceContext inferenceContext) { |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
589 |
return !uv.isCaptured() && uv.getBounds(InferenceBound.EQ).nonEmpty(); |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
590 |
} |
15717 | 591 |
}, |
592 |
/** |
|
593 |
* Check consistency of equality constraints. |
|
594 |
*/ |
|
595 |
EQ_CHECK() { |
|
23124
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
596 |
@Override |
15717 | 597 |
public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
598 |
Infer infer = inferenceContext.infer(); |
|
599 |
for (Type e : uv.getBounds(InferenceBound.EQ)) { |
|
600 |
if (e.containsAny(inferenceContext.inferenceVars())) continue; |
|
601 |
for (Type u : uv.getBounds(InferenceBound.UPPER)) { |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
602 |
if (!isSubtype(e, inferenceContext.asFree(u), warn, infer)) { |
15717 | 603 |
infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_UPPER); |
604 |
} |
|
605 |
} |
|
606 |
for (Type l : uv.getBounds(InferenceBound.LOWER)) { |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
607 |
if (!isSubtype(inferenceContext.asFree(l), e, warn, infer)) { |
15717 | 608 |
infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_LOWER); |
609 |
} |
|
610 |
} |
|
611 |
} |
|
612 |
} |
|
23124
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
613 |
|
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
614 |
@Override |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
615 |
boolean accepts(UndetVar uv, InferenceContext inferenceContext) { |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
616 |
return !uv.isCaptured() && uv.getBounds(InferenceBound.EQ).nonEmpty(); |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
617 |
} |
15717 | 618 |
}, |
619 |
/** |
|
620 |
* Given a bound set containing {@code alpha <: T} and {@code alpha :> S} |
|
621 |
* perform {@code S <: T} (which could lead to new bounds). |
|
622 |
*/ |
|
623 |
CROSS_UPPER_LOWER() { |
|
624 |
public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
|
625 |
Infer infer = inferenceContext.infer(); |
|
626 |
for (Type b1 : uv.getBounds(InferenceBound.UPPER)) { |
|
627 |
for (Type b2 : uv.getBounds(InferenceBound.LOWER)) { |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
628 |
isSubtype(inferenceContext.asFree(b2), inferenceContext.asFree(b1), warn , infer); |
15717 | 629 |
} |
630 |
} |
|
631 |
} |
|
23124
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
632 |
|
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
633 |
@Override |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
634 |
boolean accepts(UndetVar uv, InferenceContext inferenceContext) { |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
635 |
return !uv.isCaptured() && |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
636 |
uv.getBounds(InferenceBound.UPPER).nonEmpty() && |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
637 |
uv.getBounds(InferenceBound.LOWER).nonEmpty(); |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
638 |
} |
15717 | 639 |
}, |
640 |
/** |
|
641 |
* Given a bound set containing {@code alpha <: T} and {@code alpha == S} |
|
642 |
* perform {@code S <: T} (which could lead to new bounds). |
|
643 |
*/ |
|
644 |
CROSS_UPPER_EQ() { |
|
645 |
public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
|
646 |
Infer infer = inferenceContext.infer(); |
|
647 |
for (Type b1 : uv.getBounds(InferenceBound.UPPER)) { |
|
648 |
for (Type b2 : uv.getBounds(InferenceBound.EQ)) { |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
649 |
isSubtype(inferenceContext.asFree(b2), inferenceContext.asFree(b1), warn, infer); |
15717 | 650 |
} |
651 |
} |
|
652 |
} |
|
23124
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
653 |
|
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
654 |
@Override |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
655 |
boolean accepts(UndetVar uv, InferenceContext inferenceContext) { |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
656 |
return !uv.isCaptured() && |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
657 |
uv.getBounds(InferenceBound.EQ).nonEmpty() && |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
658 |
uv.getBounds(InferenceBound.UPPER).nonEmpty(); |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
659 |
} |
15717 | 660 |
}, |
661 |
/** |
|
662 |
* Given a bound set containing {@code alpha :> S} and {@code alpha == T} |
|
663 |
* perform {@code S <: T} (which could lead to new bounds). |
|
664 |
*/ |
|
665 |
CROSS_EQ_LOWER() { |
|
666 |
public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
|
667 |
Infer infer = inferenceContext.infer(); |
|
668 |
for (Type b1 : uv.getBounds(InferenceBound.EQ)) { |
|
669 |
for (Type b2 : uv.getBounds(InferenceBound.LOWER)) { |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
670 |
isSubtype(inferenceContext.asFree(b2), inferenceContext.asFree(b1), warn, infer); |
16567
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16341
diff
changeset
|
671 |
} |
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16341
diff
changeset
|
672 |
} |
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16341
diff
changeset
|
673 |
} |
23124
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
674 |
|
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
675 |
@Override |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
676 |
boolean accepts(UndetVar uv, InferenceContext inferenceContext) { |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
677 |
return !uv.isCaptured() && |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
678 |
uv.getBounds(InferenceBound.EQ).nonEmpty() && |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
679 |
uv.getBounds(InferenceBound.LOWER).nonEmpty(); |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
680 |
} |
16567
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16341
diff
changeset
|
681 |
}, |
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16341
diff
changeset
|
682 |
/** |
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16341
diff
changeset
|
683 |
* Given a bound set containing {@code alpha == S} and {@code alpha == T} |
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16341
diff
changeset
|
684 |
* perform {@code S == T} (which could lead to new bounds). |
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16341
diff
changeset
|
685 |
*/ |
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16341
diff
changeset
|
686 |
CROSS_EQ_EQ() { |
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16341
diff
changeset
|
687 |
public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16341
diff
changeset
|
688 |
Infer infer = inferenceContext.infer(); |
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16341
diff
changeset
|
689 |
for (Type b1 : uv.getBounds(InferenceBound.EQ)) { |
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16341
diff
changeset
|
690 |
for (Type b2 : uv.getBounds(InferenceBound.EQ)) { |
946441667c02
8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents:
16341
diff
changeset
|
691 |
if (b1 != b2) { |
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
692 |
isSameType(inferenceContext.asFree(b2), inferenceContext.asFree(b1), infer); |
15717 | 693 |
} |
694 |
} |
|
695 |
} |
|
696 |
} |
|
23124
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
697 |
|
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
698 |
@Override |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
699 |
boolean accepts(UndetVar uv, InferenceContext inferenceContext) { |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
700 |
return !uv.isCaptured() && |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
701 |
uv.getBounds(InferenceBound.EQ).nonEmpty(); |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
702 |
} |
15717 | 703 |
}, |
704 |
/** |
|
705 |
* Given a bound set containing {@code alpha <: beta} propagate lower bounds |
|
706 |
* from alpha to beta; also propagate upper bounds from beta to alpha. |
|
707 |
*/ |
|
708 |
PROP_UPPER() { |
|
709 |
public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
|
710 |
Infer infer = inferenceContext.infer(); |
|
711 |
for (Type b : uv.getBounds(InferenceBound.UPPER)) { |
|
712 |
if (inferenceContext.inferenceVars().contains(b)) { |
|
713 |
UndetVar uv2 = (UndetVar)inferenceContext.asFree(b); |
|
18911 | 714 |
if (uv2.isCaptured()) continue; |
15717 | 715 |
//alpha <: beta |
16341
cc4b4e3c0f12
8009545: Graph inference: dependencies between inference variables should be set during incorporation
mcimadamore
parents:
16321
diff
changeset
|
716 |
//0. set beta :> alpha |
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
717 |
addBound(InferenceBound.LOWER, uv2, inferenceContext.asInstType(uv.qtype), infer); |
15717 | 718 |
//1. copy alpha's lower to beta's |
719 |
for (Type l : uv.getBounds(InferenceBound.LOWER)) { |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
720 |
addBound(InferenceBound.LOWER, uv2, inferenceContext.asInstType(l), infer); |
15717 | 721 |
} |
722 |
//2. copy beta's upper to alpha's |
|
723 |
for (Type u : uv2.getBounds(InferenceBound.UPPER)) { |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
724 |
addBound(InferenceBound.UPPER, uv, inferenceContext.asInstType(u), infer); |
15717 | 725 |
} |
726 |
} |
|
727 |
} |
|
728 |
} |
|
23124
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
729 |
|
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
730 |
@Override |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
731 |
boolean accepts(UndetVar uv, InferenceContext inferenceContext) { |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
732 |
return !uv.isCaptured() && |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
733 |
uv.getBounds(InferenceBound.UPPER).nonEmpty(); |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
734 |
} |
15717 | 735 |
}, |
736 |
/** |
|
737 |
* Given a bound set containing {@code alpha :> beta} propagate lower bounds |
|
738 |
* from beta to alpha; also propagate upper bounds from alpha to beta. |
|
739 |
*/ |
|
740 |
PROP_LOWER() { |
|
741 |
public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
|
742 |
Infer infer = inferenceContext.infer(); |
|
743 |
for (Type b : uv.getBounds(InferenceBound.LOWER)) { |
|
744 |
if (inferenceContext.inferenceVars().contains(b)) { |
|
745 |
UndetVar uv2 = (UndetVar)inferenceContext.asFree(b); |
|
18911 | 746 |
if (uv2.isCaptured()) continue; |
15717 | 747 |
//alpha :> beta |
16341
cc4b4e3c0f12
8009545: Graph inference: dependencies between inference variables should be set during incorporation
mcimadamore
parents:
16321
diff
changeset
|
748 |
//0. set beta <: alpha |
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
749 |
addBound(InferenceBound.UPPER, uv2, inferenceContext.asInstType(uv.qtype), infer); |
15717 | 750 |
//1. copy alpha's upper to beta's |
751 |
for (Type u : uv.getBounds(InferenceBound.UPPER)) { |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
752 |
addBound(InferenceBound.UPPER, uv2, inferenceContext.asInstType(u), infer); |
15717 | 753 |
} |
754 |
//2. copy beta's lower to alpha's |
|
755 |
for (Type l : uv2.getBounds(InferenceBound.LOWER)) { |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
756 |
addBound(InferenceBound.LOWER, uv, inferenceContext.asInstType(l), infer); |
15717 | 757 |
} |
758 |
} |
|
759 |
} |
|
760 |
} |
|
23124
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
761 |
|
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
762 |
@Override |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
763 |
boolean accepts(UndetVar uv, InferenceContext inferenceContext) { |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
764 |
return !uv.isCaptured() && |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
765 |
uv.getBounds(InferenceBound.LOWER).nonEmpty(); |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
766 |
} |
15717 | 767 |
}, |
768 |
/** |
|
769 |
* Given a bound set containing {@code alpha == beta} propagate lower/upper |
|
770 |
* bounds from alpha to beta and back. |
|
771 |
*/ |
|
772 |
PROP_EQ() { |
|
773 |
public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) { |
|
774 |
Infer infer = inferenceContext.infer(); |
|
775 |
for (Type b : uv.getBounds(InferenceBound.EQ)) { |
|
776 |
if (inferenceContext.inferenceVars().contains(b)) { |
|
777 |
UndetVar uv2 = (UndetVar)inferenceContext.asFree(b); |
|
18911 | 778 |
if (uv2.isCaptured()) continue; |
15717 | 779 |
//alpha == beta |
16341
cc4b4e3c0f12
8009545: Graph inference: dependencies between inference variables should be set during incorporation
mcimadamore
parents:
16321
diff
changeset
|
780 |
//0. set beta == alpha |
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
781 |
addBound(InferenceBound.EQ, uv2, inferenceContext.asInstType(uv.qtype), infer); |
15717 | 782 |
//1. copy all alpha's bounds to beta's |
783 |
for (InferenceBound ib : InferenceBound.values()) { |
|
784 |
for (Type b2 : uv.getBounds(ib)) { |
|
785 |
if (b2 != uv2) { |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
786 |
addBound(ib, uv2, inferenceContext.asInstType(b2), infer); |
15717 | 787 |
} |
788 |
} |
|
789 |
} |
|
790 |
//2. copy all beta's bounds to alpha's |
|
791 |
for (InferenceBound ib : InferenceBound.values()) { |
|
792 |
for (Type b2 : uv2.getBounds(ib)) { |
|
793 |
if (b2 != uv) { |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
794 |
addBound(ib, uv, inferenceContext.asInstType(b2), infer); |
15717 | 795 |
} |
796 |
} |
|
797 |
} |
|
798 |
} |
|
799 |
} |
|
800 |
} |
|
23124
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
801 |
|
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
802 |
@Override |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
803 |
boolean accepts(UndetVar uv, InferenceContext inferenceContext) { |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
804 |
return !uv.isCaptured() && |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
805 |
uv.getBounds(InferenceBound.EQ).nonEmpty(); |
63050cb08583
8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents:
22165
diff
changeset
|
806 |
} |
15717 | 807 |
}; |
808 |
||
809 |
abstract void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn); |
|
18911 | 810 |
|
811 |
boolean accepts(UndetVar uv, InferenceContext inferenceContext) { |
|
812 |
return !uv.isCaptured(); |
|
813 |
} |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
814 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
815 |
boolean isSubtype(Type s, Type t, Warner warn, Infer infer) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
816 |
return doIncorporationOp(IncorporationBinaryOpKind.IS_SUBTYPE, s, t, warn, infer); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
817 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
818 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
819 |
boolean isSameType(Type s, Type t, Infer infer) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
820 |
return doIncorporationOp(IncorporationBinaryOpKind.IS_SAME_TYPE, s, t, null, infer); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
821 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
822 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
823 |
void addBound(InferenceBound ib, UndetVar uv, Type b, Infer infer) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
824 |
doIncorporationOp(opFor(ib), uv, b, null, infer); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
825 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
826 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
827 |
IncorporationBinaryOpKind opFor(InferenceBound boundKind) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
828 |
switch (boundKind) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
829 |
case EQ: |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
830 |
return IncorporationBinaryOpKind.ADD_EQ_BOUND; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
831 |
case LOWER: |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
832 |
return IncorporationBinaryOpKind.ADD_LOWER_BOUND; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
833 |
case UPPER: |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
834 |
return IncorporationBinaryOpKind.ADD_UPPER_BOUND; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
835 |
default: |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
836 |
Assert.error("Can't get here!"); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
837 |
return null; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
838 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
839 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
840 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
841 |
boolean doIncorporationOp(IncorporationBinaryOpKind opKind, Type op1, Type op2, Warner warn, Infer infer) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
842 |
IncorporationBinaryOp newOp = infer.new IncorporationBinaryOp(opKind, op1, op2); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
843 |
Boolean res = infer.incorporationCache.get(newOp); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
844 |
if (res == null) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
845 |
infer.incorporationCache.put(newOp, res = newOp.apply(warn)); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
846 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
847 |
return res; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
848 |
} |
15717 | 849 |
} |
850 |
||
851 |
/** incorporation steps to be executed when running in legacy mode */ |
|
852 |
EnumSet<IncorporationStep> incorporationStepsLegacy = EnumSet.of(IncorporationStep.EQ_CHECK_LEGACY); |
|
853 |
||
854 |
/** incorporation steps to be executed when running in graph mode */ |
|
855 |
EnumSet<IncorporationStep> incorporationStepsGraph = |
|
856 |
EnumSet.complementOf(EnumSet.of(IncorporationStep.EQ_CHECK_LEGACY)); |
|
857 |
||
858 |
/** |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
859 |
* Three kinds of basic operation are supported as part of an incorporation step: |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
860 |
* (i) subtype check, (ii) same type check and (iii) bound addition (either |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
861 |
* upper/lower/eq bound). |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
862 |
*/ |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
863 |
enum IncorporationBinaryOpKind { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
864 |
IS_SUBTYPE() { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
865 |
@Override |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
866 |
boolean apply(Type op1, Type op2, Warner warn, Types types) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
867 |
return types.isSubtypeUnchecked(op1, op2, warn); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
868 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
869 |
}, |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
870 |
IS_SAME_TYPE() { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
871 |
@Override |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
872 |
boolean apply(Type op1, Type op2, Warner warn, Types types) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
873 |
return types.isSameType(op1, op2); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
874 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
875 |
}, |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
876 |
ADD_UPPER_BOUND() { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
877 |
@Override |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
878 |
boolean apply(Type op1, Type op2, Warner warn, Types types) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
879 |
UndetVar uv = (UndetVar)op1; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
880 |
uv.addBound(InferenceBound.UPPER, op2, types); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
881 |
return true; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
882 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
883 |
}, |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
884 |
ADD_LOWER_BOUND() { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
885 |
@Override |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
886 |
boolean apply(Type op1, Type op2, Warner warn, Types types) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
887 |
UndetVar uv = (UndetVar)op1; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
888 |
uv.addBound(InferenceBound.LOWER, op2, types); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
889 |
return true; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
890 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
891 |
}, |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
892 |
ADD_EQ_BOUND() { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
893 |
@Override |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
894 |
boolean apply(Type op1, Type op2, Warner warn, Types types) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
895 |
UndetVar uv = (UndetVar)op1; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
896 |
uv.addBound(InferenceBound.EQ, op2, types); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
897 |
return true; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
898 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
899 |
}; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
900 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
901 |
abstract boolean apply(Type op1, Type op2, Warner warn, Types types); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
902 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
903 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
904 |
/** |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
905 |
* This class encapsulates a basic incorporation operation; incorporation |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
906 |
* operations takes two type operands and a kind. Each operation performed |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
907 |
* during an incorporation round is stored in a cache, so that operations |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
908 |
* are not executed unnecessarily (which would potentially lead to adding |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
909 |
* same bounds over and over). |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
910 |
*/ |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
911 |
class IncorporationBinaryOp { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
912 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
913 |
IncorporationBinaryOpKind opKind; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
914 |
Type op1; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
915 |
Type op2; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
916 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
917 |
IncorporationBinaryOp(IncorporationBinaryOpKind opKind, Type op1, Type op2) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
918 |
this.opKind = opKind; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
919 |
this.op1 = op1; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
920 |
this.op2 = op2; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
921 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
922 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
923 |
@Override |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
924 |
public boolean equals(Object o) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
925 |
if (!(o instanceof IncorporationBinaryOp)) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
926 |
return false; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
927 |
} else { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
928 |
IncorporationBinaryOp that = (IncorporationBinaryOp)o; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
929 |
return opKind == that.opKind && |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
930 |
types.isSameType(op1, that.op1, true) && |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
931 |
types.isSameType(op2, that.op2, true); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
932 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
933 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
934 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
935 |
@Override |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
936 |
public int hashCode() { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
937 |
int result = opKind.hashCode(); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
938 |
result *= 127; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
939 |
result += types.hashCode(op1); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
940 |
result *= 127; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
941 |
result += types.hashCode(op2); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
942 |
return result; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
943 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
944 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
945 |
boolean apply(Warner warn) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
946 |
return opKind.apply(op1, op2, warn, types); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
947 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
948 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
949 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
950 |
/** an incorporation cache keeps track of all executed incorporation-related operations */ |
22163 | 951 |
Map<IncorporationBinaryOp, Boolean> incorporationCache = new HashMap<>(); |
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
952 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
953 |
/** |
15717 | 954 |
* Make sure that the upper bounds we got so far lead to a solvable inference |
955 |
* variable by making sure that a glb exists. |
|
956 |
*/ |
|
957 |
void checkCompatibleUpperBounds(UndetVar uv, InferenceContext inferenceContext) { |
|
958 |
List<Type> hibounds = |
|
959 |
Type.filter(uv.getBounds(InferenceBound.UPPER), new BoundFilter(inferenceContext)); |
|
960 |
Type hb = null; |
|
961 |
if (hibounds.isEmpty()) |
|
962 |
hb = syms.objectType; |
|
963 |
else if (hibounds.tail.isEmpty()) |
|
964 |
hb = hibounds.head; |
|
965 |
else |
|
966 |
hb = types.glb(hibounds); |
|
967 |
if (hb == null || hb.isErroneous()) |
|
968 |
reportBoundError(uv, BoundErrorKind.BAD_UPPER); |
|
969 |
} |
|
970 |
//where |
|
971 |
protected static class BoundFilter implements Filter<Type> { |
|
972 |
||
973 |
InferenceContext inferenceContext; |
|
974 |
||
975 |
public BoundFilter(InferenceContext inferenceContext) { |
|
976 |
this.inferenceContext = inferenceContext; |
|
977 |
} |
|
978 |
||
979 |
@Override |
|
980 |
public boolean accepts(Type t) { |
|
981 |
return !t.isErroneous() && !inferenceContext.free(t) && |
|
982 |
!t.hasTag(BOT); |
|
983 |
} |
|
22163 | 984 |
} |
15717 | 985 |
|
986 |
/** |
|
987 |
* This enumeration defines all possible bound-checking related errors. |
|
988 |
*/ |
|
989 |
enum BoundErrorKind { |
|
990 |
/** |
|
991 |
* The (uninstantiated) inference variable has incompatible upper bounds. |
|
992 |
*/ |
|
993 |
BAD_UPPER() { |
|
994 |
@Override |
|
995 |
InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { |
|
996 |
return ex.setMessage("incompatible.upper.bounds", uv.qtype, |
|
997 |
uv.getBounds(InferenceBound.UPPER)); |
|
998 |
} |
|
999 |
}, |
|
1000 |
/** |
|
1001 |
* An equality constraint is not compatible with an upper bound. |
|
1002 |
*/ |
|
1003 |
BAD_EQ_UPPER() { |
|
1004 |
@Override |
|
1005 |
InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { |
|
1006 |
return ex.setMessage("incompatible.eq.upper.bounds", uv.qtype, |
|
1007 |
uv.getBounds(InferenceBound.EQ), uv.getBounds(InferenceBound.UPPER)); |
|
1008 |
} |
|
1009 |
}, |
|
1010 |
/** |
|
1011 |
* An equality constraint is not compatible with a lower bound. |
|
1012 |
*/ |
|
1013 |
BAD_EQ_LOWER() { |
|
1014 |
@Override |
|
1015 |
InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { |
|
1016 |
return ex.setMessage("incompatible.eq.lower.bounds", uv.qtype, |
|
1017 |
uv.getBounds(InferenceBound.EQ), uv.getBounds(InferenceBound.LOWER)); |
|
1018 |
} |
|
1019 |
}, |
|
1020 |
/** |
|
1021 |
* Instantiated inference variable is not compatible with an upper bound. |
|
1022 |
*/ |
|
1023 |
UPPER() { |
|
1024 |
@Override |
|
1025 |
InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { |
|
1026 |
return ex.setMessage("inferred.do.not.conform.to.upper.bounds", uv.inst, |
|
1027 |
uv.getBounds(InferenceBound.UPPER)); |
|
1028 |
} |
|
1029 |
}, |
|
1030 |
/** |
|
1031 |
* Instantiated inference variable is not compatible with a lower bound. |
|
1032 |
*/ |
|
1033 |
LOWER() { |
|
1034 |
@Override |
|
1035 |
InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { |
|
1036 |
return ex.setMessage("inferred.do.not.conform.to.lower.bounds", uv.inst, |
|
1037 |
uv.getBounds(InferenceBound.LOWER)); |
|
1038 |
} |
|
1039 |
}, |
|
1040 |
/** |
|
1041 |
* Instantiated inference variable is not compatible with an equality constraint. |
|
1042 |
*/ |
|
1043 |
EQ() { |
|
1044 |
@Override |
|
1045 |
InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) { |
|
1046 |
return ex.setMessage("inferred.do.not.conform.to.eq.bounds", uv.inst, |
|
1047 |
uv.getBounds(InferenceBound.EQ)); |
|
1048 |
} |
|
1049 |
}; |
|
1050 |
||
1051 |
abstract InapplicableMethodException setMessage(InferenceException ex, UndetVar uv); |
|
1052 |
} |
|
1053 |
||
1054 |
/** |
|
1055 |
* Report a bound-checking error of given kind |
|
1056 |
*/ |
|
1057 |
void reportBoundError(UndetVar uv, BoundErrorKind bk) { |
|
1058 |
throw bk.setMessage(inferenceException, uv); |
|
1059 |
} |
|
1060 |
// </editor-fold> |
|
1061 |
||
1062 |
// <editor-fold defaultstate="collapsed" desc="Inference engine"> |
|
1063 |
/** |
|
1064 |
* Graph inference strategy - act as an input to the inference solver; a strategy is |
|
1065 |
* composed of two ingredients: (i) find a node to solve in the inference graph, |
|
1066 |
* and (ii) tell th engine when we are done fixing inference variables |
|
1067 |
*/ |
|
1068 |
interface GraphStrategy { |
|
19914 | 1069 |
|
1070 |
/** |
|
1071 |
* A NodeNotFoundException is thrown whenever an inference strategy fails |
|
1072 |
* to pick the next node to solve in the inference graph. |
|
1073 |
*/ |
|
1074 |
public static class NodeNotFoundException extends RuntimeException { |
|
1075 |
private static final long serialVersionUID = 0; |
|
1076 |
||
1077 |
InferenceGraph graph; |
|
1078 |
||
1079 |
public NodeNotFoundException(InferenceGraph graph) { |
|
1080 |
this.graph = graph; |
|
1081 |
} |
|
1082 |
} |
|
15717 | 1083 |
/** |
1084 |
* Pick the next node (leaf) to solve in the graph |
|
1085 |
*/ |
|
19914 | 1086 |
Node pickNode(InferenceGraph g) throws NodeNotFoundException; |
15717 | 1087 |
/** |
1088 |
* Is this the last step? |
|
1089 |
*/ |
|
1090 |
boolean done(); |
|
1091 |
} |
|
1092 |
||
1093 |
/** |
|
1094 |
* Simple solver strategy class that locates all leaves inside a graph |
|
1095 |
* and picks the first leaf as the next node to solve |
|
1096 |
*/ |
|
1097 |
abstract class LeafSolver implements GraphStrategy { |
|
1098 |
public Node pickNode(InferenceGraph g) { |
|
19914 | 1099 |
if (g.nodes.isEmpty()) { |
1100 |
//should not happen |
|
1101 |
throw new NodeNotFoundException(g); |
|
22163 | 1102 |
} |
15717 | 1103 |
return g.nodes.get(0); |
1104 |
} |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1105 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1106 |
boolean isSubtype(Type s, Type t, Warner warn, Infer infer) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1107 |
return doIncorporationOp(IncorporationBinaryOpKind.IS_SUBTYPE, s, t, warn, infer); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1108 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1109 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1110 |
boolean isSameType(Type s, Type t, Infer infer) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1111 |
return doIncorporationOp(IncorporationBinaryOpKind.IS_SAME_TYPE, s, t, null, infer); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1112 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1113 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1114 |
void addBound(InferenceBound ib, UndetVar uv, Type b, Infer infer) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1115 |
doIncorporationOp(opFor(ib), uv, b, null, infer); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1116 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1117 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1118 |
IncorporationBinaryOpKind opFor(InferenceBound boundKind) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1119 |
switch (boundKind) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1120 |
case EQ: |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1121 |
return IncorporationBinaryOpKind.ADD_EQ_BOUND; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1122 |
case LOWER: |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1123 |
return IncorporationBinaryOpKind.ADD_LOWER_BOUND; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1124 |
case UPPER: |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1125 |
return IncorporationBinaryOpKind.ADD_UPPER_BOUND; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1126 |
default: |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1127 |
Assert.error("Can't get here!"); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1128 |
return null; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1129 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1130 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1131 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1132 |
boolean doIncorporationOp(IncorporationBinaryOpKind opKind, Type op1, Type op2, Warner warn, Infer infer) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1133 |
IncorporationBinaryOp newOp = infer.new IncorporationBinaryOp(opKind, op1, op2); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1134 |
Boolean res = infer.incorporationCache.get(newOp); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1135 |
if (res == null) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1136 |
infer.incorporationCache.put(newOp, res = newOp.apply(warn)); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1137 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1138 |
return res; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1139 |
} |
15717 | 1140 |
} |
1141 |
||
1142 |
/** |
|
1143 |
* This solver uses an heuristic to pick the best leaf - the heuristic |
|
1144 |
* tries to select the node that has maximal probability to contain one |
|
1145 |
* or more inference variables in a given list |
|
1146 |
*/ |
|
1147 |
abstract class BestLeafSolver extends LeafSolver { |
|
1148 |
||
19914 | 1149 |
/** list of ivars of which at least one must be solved */ |
15717 | 1150 |
List<Type> varsToSolve; |
1151 |
||
1152 |
BestLeafSolver(List<Type> varsToSolve) { |
|
1153 |
this.varsToSolve = varsToSolve; |
|
1154 |
} |
|
1155 |
||
1156 |
/** |
|
19914 | 1157 |
* Computes a path that goes from a given node to the leafs in the graph. |
1158 |
* Typically this will start from a node containing a variable in |
|
1159 |
* {@code varsToSolve}. For any given path, the cost is computed as the total |
|
1160 |
* number of type-variables that should be eagerly instantiated across that path. |
|
15717 | 1161 |
*/ |
19914 | 1162 |
Pair<List<Node>, Integer> computeTreeToLeafs(Node n) { |
1163 |
Pair<List<Node>, Integer> cachedPath = treeCache.get(n); |
|
1164 |
if (cachedPath == null) { |
|
1165 |
//cache miss |
|
1166 |
if (n.isLeaf()) { |
|
1167 |
//if leaf, stop |
|
22163 | 1168 |
cachedPath = new Pair<>(List.of(n), n.data.length()); |
19914 | 1169 |
} else { |
1170 |
//if non-leaf, proceed recursively |
|
22163 | 1171 |
Pair<List<Node>, Integer> path = new Pair<>(List.of(n), n.data.length()); |
19914 | 1172 |
for (Node n2 : n.getAllDependencies()) { |
1173 |
if (n2 == n) continue; |
|
1174 |
Pair<List<Node>, Integer> subpath = computeTreeToLeafs(n2); |
|
22163 | 1175 |
path = new Pair<>(path.fst.prependList(subpath.fst), |
1176 |
path.snd + subpath.snd); |
|
19914 | 1177 |
} |
1178 |
cachedPath = path; |
|
1179 |
} |
|
1180 |
//save results in cache |
|
1181 |
treeCache.put(n, cachedPath); |
|
1182 |
} |
|
1183 |
return cachedPath; |
|
18916
d93bea397df9
8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents:
18911
diff
changeset
|
1184 |
} |
d93bea397df9
8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents:
18911
diff
changeset
|
1185 |
|
19914 | 1186 |
/** cache used to avoid redundant computation of tree costs */ |
22163 | 1187 |
final Map<Node, Pair<List<Node>, Integer>> treeCache = new HashMap<>(); |
19914 | 1188 |
|
1189 |
/** constant value used to mark non-existent paths */ |
|
22163 | 1190 |
final Pair<List<Node>, Integer> noPath = new Pair<>(null, Integer.MAX_VALUE); |
15717 | 1191 |
|
1192 |
/** |
|
1193 |
* Pick the leaf that minimize cost |
|
1194 |
*/ |
|
1195 |
@Override |
|
1196 |
public Node pickNode(final InferenceGraph g) { |
|
19914 | 1197 |
treeCache.clear(); //graph changes at every step - cache must be cleared |
1198 |
Pair<List<Node>, Integer> bestPath = noPath; |
|
15717 | 1199 |
for (Node n : g.nodes) { |
19914 | 1200 |
if (!Collections.disjoint(n.data, varsToSolve)) { |
1201 |
Pair<List<Node>, Integer> path = computeTreeToLeafs(n); |
|
1202 |
//discard all paths containing at least a node in the |
|
1203 |
//closure computed above |
|
1204 |
if (path.snd < bestPath.snd) { |
|
1205 |
bestPath = path; |
|
1206 |
} |
|
15717 | 1207 |
} |
1208 |
} |
|
19914 | 1209 |
if (bestPath == noPath) { |
1210 |
//no path leads there |
|
1211 |
throw new NodeNotFoundException(g); |
|
1212 |
} |
|
1213 |
return bestPath.fst.head; |
|
15717 | 1214 |
} |
1215 |
} |
|
1216 |
||
1217 |
/** |
|
1218 |
* The inference process can be thought of as a sequence of steps. Each step |
|
1219 |
* instantiates an inference variable using a subset of the inference variable |
|
1220 |
* bounds, if certain condition are met. Decisions such as the sequence in which |
|
1221 |
* steps are applied, or which steps are to be applied are left to the inference engine. |
|
1222 |
*/ |
|
1223 |
enum InferenceStep { |
|
1224 |
||
1225 |
/** |
|
1226 |
* Instantiate an inference variables using one of its (ground) equality |
|
1227 |
* constraints |
|
1228 |
*/ |
|
1229 |
EQ(InferenceBound.EQ) { |
|
1230 |
@Override |
|
1231 |
Type solve(UndetVar uv, InferenceContext inferenceContext) { |
|
1232 |
return filterBounds(uv, inferenceContext).head; |
|
1233 |
} |
|
1234 |
}, |
|
1235 |
/** |
|
1236 |
* Instantiate an inference variables using its (ground) lower bounds. Such |
|
1237 |
* bounds are merged together using lub(). |
|
1238 |
*/ |
|
1239 |
LOWER(InferenceBound.LOWER) { |
|
1240 |
@Override |
|
1241 |
Type solve(UndetVar uv, InferenceContext inferenceContext) { |
|
1242 |
Infer infer = inferenceContext.infer(); |
|
1243 |
List<Type> lobounds = filterBounds(uv, inferenceContext); |
|
18395
d56a5fbf0b32
8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents:
18008
diff
changeset
|
1244 |
//note: lobounds should have at least one element |
d56a5fbf0b32
8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents:
18008
diff
changeset
|
1245 |
Type owntype = lobounds.tail.tail == null ? lobounds.head : infer.types.lub(lobounds); |
d56a5fbf0b32
8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents:
18008
diff
changeset
|
1246 |
if (owntype.isPrimitive() || owntype.hasTag(ERROR)) { |
15717 | 1247 |
throw infer.inferenceException |
1248 |
.setMessage("no.unique.minimal.instance.exists", |
|
1249 |
uv.qtype, lobounds); |
|
1250 |
} else { |
|
1251 |
return owntype; |
|
1252 |
} |
|
1253 |
} |
|
1254 |
}, |
|
1255 |
/** |
|
18909
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1256 |
* Infer uninstantiated/unbound inference variables occurring in 'throws' |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1257 |
* clause as RuntimeException |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1258 |
*/ |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1259 |
THROWS(InferenceBound.UPPER) { |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1260 |
@Override |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1261 |
public boolean accepts(UndetVar t, InferenceContext inferenceContext) { |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1262 |
if ((t.qtype.tsym.flags() & Flags.THROWS) == 0) { |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1263 |
//not a throws undet var |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1264 |
return false; |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1265 |
} |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1266 |
if (t.getBounds(InferenceBound.EQ, InferenceBound.LOWER, InferenceBound.UPPER) |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1267 |
.diff(t.getDeclaredBounds()).nonEmpty()) { |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1268 |
//not an unbounded undet var |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1269 |
return false; |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1270 |
} |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1271 |
Infer infer = inferenceContext.infer(); |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1272 |
for (Type db : t.getDeclaredBounds()) { |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1273 |
if (t.isInterface()) continue; |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1274 |
if (infer.types.asSuper(infer.syms.runtimeExceptionType, db.tsym) != null) { |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1275 |
//declared bound is a supertype of RuntimeException |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1276 |
return true; |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1277 |
} |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1278 |
} |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1279 |
//declared bound is more specific then RuntimeException - give up |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1280 |
return false; |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1281 |
} |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1282 |
|
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1283 |
@Override |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1284 |
Type solve(UndetVar uv, InferenceContext inferenceContext) { |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1285 |
return inferenceContext.infer().syms.runtimeExceptionType; |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1286 |
} |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1287 |
}, |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1288 |
/** |
15717 | 1289 |
* Instantiate an inference variables using its (ground) upper bounds. Such |
1290 |
* bounds are merged together using glb(). |
|
1291 |
*/ |
|
1292 |
UPPER(InferenceBound.UPPER) { |
|
1293 |
@Override |
|
1294 |
Type solve(UndetVar uv, InferenceContext inferenceContext) { |
|
1295 |
Infer infer = inferenceContext.infer(); |
|
1296 |
List<Type> hibounds = filterBounds(uv, inferenceContext); |
|
18395
d56a5fbf0b32
8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents:
18008
diff
changeset
|
1297 |
//note: lobounds should have at least one element |
d56a5fbf0b32
8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents:
18008
diff
changeset
|
1298 |
Type owntype = hibounds.tail.tail == null ? hibounds.head : infer.types.glb(hibounds); |
d56a5fbf0b32
8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents:
18008
diff
changeset
|
1299 |
if (owntype.isPrimitive() || owntype.hasTag(ERROR)) { |
15717 | 1300 |
throw infer.inferenceException |
1301 |
.setMessage("no.unique.maximal.instance.exists", |
|
1302 |
uv.qtype, hibounds); |
|
1303 |
} else { |
|
1304 |
return owntype; |
|
1305 |
} |
|
1306 |
} |
|
1307 |
}, |
|
1308 |
/** |
|
1309 |
* Like the former; the only difference is that this step can only be applied |
|
1310 |
* if all upper bounds are ground. |
|
1311 |
*/ |
|
1312 |
UPPER_LEGACY(InferenceBound.UPPER) { |
|
1313 |
@Override |
|
1314 |
public boolean accepts(UndetVar t, InferenceContext inferenceContext) { |
|
18911 | 1315 |
return !inferenceContext.free(t.getBounds(ib)) && !t.isCaptured(); |
15717 | 1316 |
} |
1317 |
||
1318 |
@Override |
|
1319 |
Type solve(UndetVar uv, InferenceContext inferenceContext) { |
|
1320 |
return UPPER.solve(uv, inferenceContext); |
|
1321 |
} |
|
18911 | 1322 |
}, |
1323 |
/** |
|
1324 |
* Like the former; the only difference is that this step can only be applied |
|
1325 |
* if all upper/lower bounds are ground. |
|
1326 |
*/ |
|
1327 |
CAPTURED(InferenceBound.UPPER) { |
|
1328 |
@Override |
|
1329 |
public boolean accepts(UndetVar t, InferenceContext inferenceContext) { |
|
19127
8a0cbd5cb055
8020804: javac crashes when speculative attribution infers intersection type with array component
mcimadamore
parents:
18918
diff
changeset
|
1330 |
return t.isCaptured() && |
8a0cbd5cb055
8020804: javac crashes when speculative attribution infers intersection type with array component
mcimadamore
parents:
18918
diff
changeset
|
1331 |
!inferenceContext.free(t.getBounds(InferenceBound.UPPER, InferenceBound.LOWER)); |
18911 | 1332 |
} |
1333 |
||
1334 |
@Override |
|
1335 |
Type solve(UndetVar uv, InferenceContext inferenceContext) { |
|
1336 |
Infer infer = inferenceContext.infer(); |
|
1337 |
Type upper = UPPER.filterBounds(uv, inferenceContext).nonEmpty() ? |
|
1338 |
UPPER.solve(uv, inferenceContext) : |
|
1339 |
infer.syms.objectType; |
|
1340 |
Type lower = LOWER.filterBounds(uv, inferenceContext).nonEmpty() ? |
|
1341 |
LOWER.solve(uv, inferenceContext) : |
|
1342 |
infer.syms.botType; |
|
1343 |
CapturedType prevCaptured = (CapturedType)uv.qtype; |
|
1344 |
return new CapturedType(prevCaptured.tsym.name, prevCaptured.tsym.owner, upper, lower, prevCaptured.wildcard); |
|
1345 |
} |
|
15717 | 1346 |
}; |
1347 |
||
1348 |
final InferenceBound ib; |
|
1349 |
||
1350 |
InferenceStep(InferenceBound ib) { |
|
1351 |
this.ib = ib; |
|
1352 |
} |
|
1353 |
||
1354 |
/** |
|
1355 |
* Find an instantiated type for a given inference variable within |
|
1356 |
* a given inference context |
|
1357 |
*/ |
|
1358 |
abstract Type solve(UndetVar uv, InferenceContext inferenceContext); |
|
1359 |
||
1360 |
/** |
|
1361 |
* Can the inference variable be instantiated using this step? |
|
1362 |
*/ |
|
1363 |
public boolean accepts(UndetVar t, InferenceContext inferenceContext) { |
|
18911 | 1364 |
return filterBounds(t, inferenceContext).nonEmpty() && !t.isCaptured(); |
15717 | 1365 |
} |
1366 |
||
1367 |
/** |
|
1368 |
* Return the subset of ground bounds in a given bound set (i.e. eq/lower/upper) |
|
1369 |
*/ |
|
1370 |
List<Type> filterBounds(UndetVar uv, InferenceContext inferenceContext) { |
|
1371 |
return Type.filter(uv.getBounds(ib), new BoundFilter(inferenceContext)); |
|
1372 |
} |
|
1373 |
} |
|
1374 |
||
1375 |
/** |
|
1376 |
* This enumeration defines the sequence of steps to be applied when the |
|
1377 |
* solver works in legacy mode. The steps in this enumeration reflect |
|
1378 |
* the behavior of old inference routine (see JLS SE 7 15.12.2.7/15.12.2.8). |
|
1379 |
*/ |
|
1380 |
enum LegacyInferenceSteps { |
|
1381 |
||
1382 |
EQ_LOWER(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER)), |
|
1383 |
EQ_UPPER(EnumSet.of(InferenceStep.EQ, InferenceStep.UPPER_LEGACY)); |
|
1384 |
||
1385 |
final EnumSet<InferenceStep> steps; |
|
1386 |
||
1387 |
LegacyInferenceSteps(EnumSet<InferenceStep> steps) { |
|
1388 |
this.steps = steps; |
|
1389 |
} |
|
1390 |
} |
|
1391 |
||
1392 |
/** |
|
1393 |
* This enumeration defines the sequence of steps to be applied when the |
|
1394 |
* graph solver is used. This order is defined so as to maximize compatibility |
|
1395 |
* w.r.t. old inference routine (see JLS SE 7 15.12.2.7/15.12.2.8). |
|
1396 |
*/ |
|
1397 |
enum GraphInferenceSteps { |
|
1398 |
||
1399 |
EQ(EnumSet.of(InferenceStep.EQ)), |
|
1400 |
EQ_LOWER(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER)), |
|
18911 | 1401 |
EQ_LOWER_THROWS_UPPER_CAPTURED(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER, InferenceStep.UPPER, InferenceStep.THROWS, InferenceStep.CAPTURED)); |
15717 | 1402 |
|
1403 |
final EnumSet<InferenceStep> steps; |
|
1404 |
||
1405 |
GraphInferenceSteps(EnumSet<InferenceStep> steps) { |
|
1406 |
this.steps = steps; |
|
1407 |
} |
|
1408 |
} |
|
1409 |
||
1410 |
/** |
|
19914 | 1411 |
* There are two kinds of dependencies between inference variables. The basic |
1412 |
* kind of dependency (or bound dependency) arises when a variable mention |
|
1413 |
* another variable in one of its bounds. There's also a more subtle kind |
|
1414 |
* of dependency that arises when a variable 'might' lead to better constraints |
|
1415 |
* on another variable (this is typically the case with variables holding up |
|
1416 |
* stuck expressions). |
|
1417 |
*/ |
|
1418 |
enum DependencyKind implements GraphUtils.DependencyKind { |
|
1419 |
||
1420 |
/** bound dependency */ |
|
1421 |
BOUND("dotted"), |
|
1422 |
/** stuck dependency */ |
|
1423 |
STUCK("dashed"); |
|
1424 |
||
1425 |
final String dotSyle; |
|
1426 |
||
1427 |
private DependencyKind(String dotSyle) { |
|
1428 |
this.dotSyle = dotSyle; |
|
1429 |
} |
|
1430 |
||
1431 |
@Override |
|
1432 |
public String getDotStyle() { |
|
1433 |
return dotSyle; |
|
1434 |
} |
|
1435 |
} |
|
1436 |
||
1437 |
/** |
|
15717 | 1438 |
* This is the graph inference solver - the solver organizes all inference variables in |
1439 |
* a given inference context by bound dependencies - in the general case, such dependencies |
|
1440 |
* would lead to a cyclic directed graph (hence the name); the dependency info is used to build |
|
1441 |
* an acyclic graph, where all cyclic variables are bundled together. An inference |
|
1442 |
* step corresponds to solving a node in the acyclic graph - this is done by |
|
1443 |
* relying on a given strategy (see GraphStrategy). |
|
1444 |
*/ |
|
1445 |
class GraphSolver { |
|
1446 |
||
1447 |
InferenceContext inferenceContext; |
|
19914 | 1448 |
Map<Type, Set<Type>> stuckDeps; |
15717 | 1449 |
Warner warn; |
1450 |
||
19914 | 1451 |
GraphSolver(InferenceContext inferenceContext, Map<Type, Set<Type>> stuckDeps, Warner warn) { |
15717 | 1452 |
this.inferenceContext = inferenceContext; |
19914 | 1453 |
this.stuckDeps = stuckDeps; |
15717 | 1454 |
this.warn = warn; |
1455 |
} |
|
1456 |
||
1457 |
/** |
|
1458 |
* Solve variables in a given inference context. The amount of variables |
|
1459 |
* to be solved, and the way in which the underlying acyclic graph is explored |
|
1460 |
* depends on the selected solver strategy. |
|
1461 |
*/ |
|
1462 |
void solve(GraphStrategy sstrategy) { |
|
1463 |
checkWithinBounds(inferenceContext, warn); //initial propagation of bounds |
|
19914 | 1464 |
InferenceGraph inferenceGraph = new InferenceGraph(stuckDeps); |
15717 | 1465 |
while (!sstrategy.done()) { |
1466 |
InferenceGraph.Node nodeToSolve = sstrategy.pickNode(inferenceGraph); |
|
1467 |
List<Type> varsToSolve = List.from(nodeToSolve.data); |
|
18904
3c4c6457ec5b
8019824: very long error messages on inference error
mcimadamore
parents:
18646
diff
changeset
|
1468 |
List<Type> saved_undet = inferenceContext.save(); |
15717 | 1469 |
try { |
1470 |
//repeat until all variables are solved |
|
1471 |
outer: while (Type.containsAny(inferenceContext.restvars(), varsToSolve)) { |
|
1472 |
//for each inference phase |
|
1473 |
for (GraphInferenceSteps step : GraphInferenceSteps.values()) { |
|
1474 |
if (inferenceContext.solveBasic(varsToSolve, step.steps)) { |
|
1475 |
checkWithinBounds(inferenceContext, warn); |
|
1476 |
continue outer; |
|
1477 |
} |
|
1478 |
} |
|
1479 |
//no progress |
|
18395
d56a5fbf0b32
8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents:
18008
diff
changeset
|
1480 |
throw inferenceException.setMessage(); |
15717 | 1481 |
} |
1482 |
} |
|
1483 |
catch (InferenceException ex) { |
|
18395
d56a5fbf0b32
8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents:
18008
diff
changeset
|
1484 |
//did we fail because of interdependent ivars? |
18904
3c4c6457ec5b
8019824: very long error messages on inference error
mcimadamore
parents:
18646
diff
changeset
|
1485 |
inferenceContext.rollback(saved_undet); |
15717 | 1486 |
instantiateAsUninferredVars(varsToSolve, inferenceContext); |
1487 |
checkWithinBounds(inferenceContext, warn); |
|
1488 |
} |
|
1489 |
inferenceGraph.deleteNode(nodeToSolve); |
|
1490 |
} |
|
1491 |
} |
|
1492 |
||
1493 |
/** |
|
1494 |
* The dependencies between the inference variables that need to be solved |
|
1495 |
* form a (possibly cyclic) graph. This class reduces the original dependency graph |
|
1496 |
* to an acyclic version, where cyclic nodes are folded into a single 'super node'. |
|
1497 |
*/ |
|
1498 |
class InferenceGraph { |
|
1499 |
||
1500 |
/** |
|
1501 |
* This class represents a node in the graph. Each node corresponds |
|
1502 |
* to an inference variable and has edges (dependencies) on other |
|
1503 |
* nodes. The node defines an entry point that can be used to receive |
|
1504 |
* updates on the structure of the graph this node belongs to (used to |
|
1505 |
* keep dependencies in sync). |
|
1506 |
*/ |
|
1507 |
class Node extends GraphUtils.TarjanNode<ListBuffer<Type>> { |
|
1508 |
||
19914 | 1509 |
/** map listing all dependencies (grouped by kind) */ |
1510 |
EnumMap<DependencyKind, Set<Node>> deps; |
|
15717 | 1511 |
|
1512 |
Node(Type ivar) { |
|
1513 |
super(ListBuffer.of(ivar)); |
|
22163 | 1514 |
this.deps = new EnumMap<>(DependencyKind.class); |
19914 | 1515 |
} |
1516 |
||
1517 |
@Override |
|
1518 |
public GraphUtils.DependencyKind[] getSupportedDependencyKinds() { |
|
1519 |
return DependencyKind.values(); |
|
15717 | 1520 |
} |
1521 |
||
1522 |
@Override |
|
19914 | 1523 |
public String getDependencyName(GraphUtils.Node<ListBuffer<Type>> to, GraphUtils.DependencyKind dk) { |
1524 |
if (dk == DependencyKind.STUCK) return ""; |
|
1525 |
else { |
|
1526 |
StringBuilder buf = new StringBuilder(); |
|
1527 |
String sep = ""; |
|
1528 |
for (Type from : data) { |
|
1529 |
UndetVar uv = (UndetVar)inferenceContext.asFree(from); |
|
1530 |
for (Type bound : uv.getBounds(InferenceBound.values())) { |
|
1531 |
if (bound.containsAny(List.from(to.data))) { |
|
1532 |
buf.append(sep); |
|
1533 |
buf.append(bound); |
|
1534 |
sep = ","; |
|
1535 |
} |
|
1536 |
} |
|
1537 |
} |
|
1538 |
return buf.toString(); |
|
1539 |
} |
|
1540 |
} |
|
1541 |
||
1542 |
@Override |
|
1543 |
public Iterable<? extends Node> getAllDependencies() { |
|
1544 |
return getDependencies(DependencyKind.values()); |
|
15717 | 1545 |
} |
1546 |
||
1547 |
@Override |
|
19914 | 1548 |
public Iterable<? extends TarjanNode<ListBuffer<Type>>> getDependenciesByKind(GraphUtils.DependencyKind dk) { |
1549 |
return getDependencies((DependencyKind)dk); |
|
1550 |
} |
|
1551 |
||
1552 |
/** |
|
1553 |
* Retrieves all dependencies with given kind(s). |
|
1554 |
*/ |
|
1555 |
protected Set<Node> getDependencies(DependencyKind... depKinds) { |
|
22163 | 1556 |
Set<Node> buf = new LinkedHashSet<>(); |
19914 | 1557 |
for (DependencyKind dk : depKinds) { |
1558 |
Set<Node> depsByKind = deps.get(dk); |
|
1559 |
if (depsByKind != null) { |
|
1560 |
buf.addAll(depsByKind); |
|
15717 | 1561 |
} |
1562 |
} |
|
19914 | 1563 |
return buf; |
1564 |
} |
|
1565 |
||
1566 |
/** |
|
1567 |
* Adds dependency with given kind. |
|
1568 |
*/ |
|
1569 |
protected void addDependency(DependencyKind dk, Node depToAdd) { |
|
1570 |
Set<Node> depsByKind = deps.get(dk); |
|
1571 |
if (depsByKind == null) { |
|
22163 | 1572 |
depsByKind = new LinkedHashSet<>(); |
19914 | 1573 |
deps.put(dk, depsByKind); |
1574 |
} |
|
1575 |
depsByKind.add(depToAdd); |
|
1576 |
} |
|
1577 |
||
1578 |
/** |
|
1579 |
* Add multiple dependencies of same given kind. |
|
1580 |
*/ |
|
1581 |
protected void addDependencies(DependencyKind dk, Set<Node> depsToAdd) { |
|
1582 |
for (Node n : depsToAdd) { |
|
1583 |
addDependency(dk, n); |
|
1584 |
} |
|
1585 |
} |
|
1586 |
||
1587 |
/** |
|
1588 |
* Remove a dependency, regardless of its kind. |
|
1589 |
*/ |
|
1590 |
protected Set<DependencyKind> removeDependency(Node n) { |
|
1591 |
Set<DependencyKind> removedKinds = new HashSet<>(); |
|
1592 |
for (DependencyKind dk : DependencyKind.values()) { |
|
1593 |
Set<Node> depsByKind = deps.get(dk); |
|
1594 |
if (depsByKind == null) continue; |
|
1595 |
if (depsByKind.remove(n)) { |
|
1596 |
removedKinds.add(dk); |
|
1597 |
} |
|
1598 |
} |
|
1599 |
return removedKinds; |
|
15717 | 1600 |
} |
1601 |
||
19914 | 1602 |
/** |
1603 |
* Compute closure of a give node, by recursively walking |
|
1604 |
* through all its dependencies (of given kinds) |
|
1605 |
*/ |
|
1606 |
protected Set<Node> closure(DependencyKind... depKinds) { |
|
1607 |
boolean progress = true; |
|
22163 | 1608 |
Set<Node> closure = new HashSet<>(); |
19914 | 1609 |
closure.add(this); |
1610 |
while (progress) { |
|
1611 |
progress = false; |
|
22163 | 1612 |
for (Node n1 : new HashSet<>(closure)) { |
19914 | 1613 |
progress = closure.addAll(n1.getDependencies(depKinds)); |
1614 |
} |
|
1615 |
} |
|
1616 |
return closure; |
|
15717 | 1617 |
} |
1618 |
||
19914 | 1619 |
/** |
1620 |
* Is this node a leaf? This means either the node has no dependencies, |
|
1621 |
* or it just has self-dependencies. |
|
1622 |
*/ |
|
1623 |
protected boolean isLeaf() { |
|
1624 |
//no deps, or only one self dep |
|
1625 |
Set<Node> allDeps = getDependencies(DependencyKind.BOUND, DependencyKind.STUCK); |
|
1626 |
if (allDeps.isEmpty()) return true; |
|
1627 |
for (Node n : allDeps) { |
|
1628 |
if (n != this) { |
|
1629 |
return false; |
|
1630 |
} |
|
1631 |
} |
|
1632 |
return true; |
|
1633 |
} |
|
1634 |
||
1635 |
/** |
|
1636 |
* Merge this node with another node, acquiring its dependencies. |
|
1637 |
* This routine is used to merge all cyclic node together and |
|
1638 |
* form an acyclic graph. |
|
1639 |
*/ |
|
1640 |
protected void mergeWith(List<? extends Node> nodes) { |
|
15717 | 1641 |
for (Node n : nodes) { |
1642 |
Assert.check(n.data.length() == 1, "Attempt to merge a compound node!"); |
|
1643 |
data.appendList(n.data); |
|
19914 | 1644 |
for (DependencyKind dk : DependencyKind.values()) { |
1645 |
addDependencies(dk, n.getDependencies(dk)); |
|
1646 |
} |
|
15717 | 1647 |
} |
1648 |
//update deps |
|
22163 | 1649 |
EnumMap<DependencyKind, Set<Node>> deps2 = new EnumMap<>(DependencyKind.class); |
19914 | 1650 |
for (DependencyKind dk : DependencyKind.values()) { |
1651 |
for (Node d : getDependencies(dk)) { |
|
1652 |
Set<Node> depsByKind = deps2.get(dk); |
|
1653 |
if (depsByKind == null) { |
|
22163 | 1654 |
depsByKind = new LinkedHashSet<>(); |
19914 | 1655 |
deps2.put(dk, depsByKind); |
1656 |
} |
|
1657 |
if (data.contains(d.data.first())) { |
|
1658 |
depsByKind.add(this); |
|
1659 |
} else { |
|
1660 |
depsByKind.add(d); |
|
1661 |
} |
|
15717 | 1662 |
} |
1663 |
} |
|
1664 |
deps = deps2; |
|
1665 |
} |
|
1666 |
||
19914 | 1667 |
/** |
1668 |
* Notify all nodes that something has changed in the graph |
|
1669 |
* topology. |
|
1670 |
*/ |
|
1671 |
private void graphChanged(Node from, Node to) { |
|
1672 |
for (DependencyKind dk : removeDependency(from)) { |
|
15717 | 1673 |
if (to != null) { |
19914 | 1674 |
addDependency(dk, to); |
15717 | 1675 |
} |
1676 |
} |
|
1677 |
} |
|
1678 |
} |
|
1679 |
||
1680 |
/** the nodes in the inference graph */ |
|
1681 |
ArrayList<Node> nodes; |
|
1682 |
||
19914 | 1683 |
InferenceGraph(Map<Type, Set<Type>> optDeps) { |
1684 |
initNodes(optDeps); |
|
1685 |
} |
|
1686 |
||
1687 |
/** |
|
1688 |
* Basic lookup helper for retrieving a graph node given an inference |
|
1689 |
* variable type. |
|
1690 |
*/ |
|
1691 |
public Node findNode(Type t) { |
|
1692 |
for (Node n : nodes) { |
|
1693 |
if (n.data.contains(t)) { |
|
1694 |
return n; |
|
1695 |
} |
|
1696 |
} |
|
1697 |
return null; |
|
15717 | 1698 |
} |
1699 |
||
1700 |
/** |
|
1701 |
* Delete a node from the graph. This update the underlying structure |
|
1702 |
* of the graph (including dependencies) via listeners updates. |
|
1703 |
*/ |
|
1704 |
public void deleteNode(Node n) { |
|
1705 |
Assert.check(nodes.contains(n)); |
|
1706 |
nodes.remove(n); |
|
1707 |
notifyUpdate(n, null); |
|
1708 |
} |
|
1709 |
||
1710 |
/** |
|
1711 |
* Notify all nodes of a change in the graph. If the target node is |
|
1712 |
* {@code null} the source node is assumed to be removed. |
|
1713 |
*/ |
|
1714 |
void notifyUpdate(Node from, Node to) { |
|
1715 |
for (Node n : nodes) { |
|
1716 |
n.graphChanged(from, to); |
|
1717 |
} |
|
1718 |
} |
|
1719 |
||
1720 |
/** |
|
1721 |
* Create the graph nodes. First a simple node is created for every inference |
|
1722 |
* variables to be solved. Then Tarjan is used to found all connected components |
|
1723 |
* in the graph. For each component containing more than one node, a super node is |
|
19914 | 1724 |
* created, effectively replacing the original cyclic nodes. |
15717 | 1725 |
*/ |
19914 | 1726 |
void initNodes(Map<Type, Set<Type>> stuckDeps) { |
1727 |
//add nodes |
|
22163 | 1728 |
nodes = new ArrayList<>(); |
15717 | 1729 |
for (Type t : inferenceContext.restvars()) { |
1730 |
nodes.add(new Node(t)); |
|
1731 |
} |
|
19914 | 1732 |
//add dependencies |
15717 | 1733 |
for (Node n_i : nodes) { |
1734 |
Type i = n_i.data.first(); |
|
19914 | 1735 |
Set<Type> optDepsByNode = stuckDeps.get(i); |
15717 | 1736 |
for (Node n_j : nodes) { |
1737 |
Type j = n_j.data.first(); |
|
1738 |
UndetVar uv_i = (UndetVar)inferenceContext.asFree(i); |
|
1739 |
if (Type.containsAny(uv_i.getBounds(InferenceBound.values()), List.of(j))) { |
|
19914 | 1740 |
//update i's bound dependencies |
1741 |
n_i.addDependency(DependencyKind.BOUND, n_j); |
|
1742 |
} |
|
1743 |
if (optDepsByNode != null && optDepsByNode.contains(j)) { |
|
1744 |
//update i's stuck dependencies |
|
1745 |
n_i.addDependency(DependencyKind.STUCK, n_j); |
|
15717 | 1746 |
} |
1747 |
} |
|
1748 |
} |
|
19914 | 1749 |
//merge cyclic nodes |
22163 | 1750 |
ArrayList<Node> acyclicNodes = new ArrayList<>(); |
15717 | 1751 |
for (List<? extends Node> conSubGraph : GraphUtils.tarjan(nodes)) { |
1752 |
if (conSubGraph.length() > 1) { |
|
1753 |
Node root = conSubGraph.head; |
|
1754 |
root.mergeWith(conSubGraph.tail); |
|
1755 |
for (Node n : conSubGraph) { |
|
1756 |
notifyUpdate(n, root); |
|
1757 |
} |
|
1758 |
} |
|
16321
75e64bcd3ab0
8008723: Graph Inference: bad graph calculation leads to assertion error
mcimadamore
parents:
15717
diff
changeset
|
1759 |
acyclicNodes.add(conSubGraph.head); |
15717 | 1760 |
} |
16321
75e64bcd3ab0
8008723: Graph Inference: bad graph calculation leads to assertion error
mcimadamore
parents:
15717
diff
changeset
|
1761 |
nodes = acyclicNodes; |
15717 | 1762 |
} |
1763 |
||
1764 |
/** |
|
1765 |
* Debugging: dot representation of this graph |
|
1766 |
*/ |
|
1767 |
String toDot() { |
|
1768 |
StringBuilder buf = new StringBuilder(); |
|
1769 |
for (Type t : inferenceContext.undetvars) { |
|
1770 |
UndetVar uv = (UndetVar)t; |
|
1771 |
buf.append(String.format("var %s - upper bounds = %s, lower bounds = %s, eq bounds = %s\\n", |
|
1772 |
uv.qtype, uv.getBounds(InferenceBound.UPPER), uv.getBounds(InferenceBound.LOWER), |
|
1773 |
uv.getBounds(InferenceBound.EQ))); |
|
1774 |
} |
|
1775 |
return GraphUtils.toDot(nodes, "inferenceGraph" + hashCode(), buf.toString()); |
|
1776 |
} |
|
1777 |
} |
|
1778 |
} |
|
1779 |
// </editor-fold> |
|
1780 |
||
1781 |
// <editor-fold defaultstate="collapsed" desc="Inference context"> |
|
1782 |
/** |
|
15705 | 1783 |
* Functional interface for defining inference callbacks. Certain actions |
1784 |
* (i.e. subtyping checks) might need to be redone after all inference variables |
|
1785 |
* have been fixed. |
|
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1786 |
*/ |
15705 | 1787 |
interface FreeTypeListener { |
1788 |
void typesInferred(InferenceContext inferenceContext); |
|
1789 |
} |
|
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1790 |
|
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1791 |
/** |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1792 |
* An inference context keeps track of the set of variables that are free |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1793 |
* in the current context. It provides utility methods for opening/closing |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1794 |
* types to their corresponding free/closed forms. It also provide hooks for |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1795 |
* attaching deferred post-inference action (see PendingCheck). Finally, |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1796 |
* it can be used as an entry point for performing upper/lower bound inference |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1797 |
* (see InferenceKind). |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1798 |
*/ |
15717 | 1799 |
class InferenceContext { |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1800 |
|
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1801 |
/** list of inference vars as undet vars */ |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1802 |
List<Type> undetvars; |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1803 |
|
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1804 |
/** list of inference vars in this context */ |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1805 |
List<Type> inferencevars; |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1806 |
|
22163 | 1807 |
Map<FreeTypeListener, List<Type>> freeTypeListeners = new HashMap<>(); |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1808 |
|
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1809 |
List<FreeTypeListener> freetypeListeners = List.nil(); |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1810 |
|
15705 | 1811 |
public InferenceContext(List<Type> inferencevars) { |
1812 |
this.undetvars = Type.map(inferencevars, fromTypeVarFun); |
|
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1813 |
this.inferencevars = inferencevars; |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1814 |
} |
15705 | 1815 |
//where |
1816 |
Mapping fromTypeVarFun = new Mapping("fromTypeVarFunWithBounds") { |
|
1817 |
// mapping that turns inference variables into undet vars |
|
1818 |
public Type apply(Type t) { |
|
18911 | 1819 |
if (t.hasTag(TYPEVAR)) { |
1820 |
TypeVar tv = (TypeVar)t; |
|
21488
4a69e26aa999
8025290: javac implicit versus explicit lambda compilation error
vromero
parents:
20249
diff
changeset
|
1821 |
if (tv.isCaptured()) { |
4a69e26aa999
8025290: javac implicit versus explicit lambda compilation error
vromero
parents:
20249
diff
changeset
|
1822 |
return new CapturedUndetVar((CapturedType)tv, types); |
4a69e26aa999
8025290: javac implicit versus explicit lambda compilation error
vromero
parents:
20249
diff
changeset
|
1823 |
} else { |
4a69e26aa999
8025290: javac implicit versus explicit lambda compilation error
vromero
parents:
20249
diff
changeset
|
1824 |
return new UndetVar(tv, types); |
4a69e26aa999
8025290: javac implicit versus explicit lambda compilation error
vromero
parents:
20249
diff
changeset
|
1825 |
} |
18911 | 1826 |
} else { |
1827 |
return t.map(this); |
|
1828 |
} |
|
15705 | 1829 |
} |
1830 |
}; |
|
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1831 |
|
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1832 |
/** |
18911 | 1833 |
* add a new inference var to this inference context |
1834 |
*/ |
|
1835 |
void addVar(TypeVar t) { |
|
1836 |
this.undetvars = this.undetvars.prepend(fromTypeVarFun.apply(t)); |
|
1837 |
this.inferencevars = this.inferencevars.prepend(t); |
|
1838 |
} |
|
1839 |
||
1840 |
/** |
|
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1841 |
* returns the list of free variables (as type-variables) in this |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1842 |
* inference context |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1843 |
*/ |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1844 |
List<Type> inferenceVars() { |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1845 |
return inferencevars; |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1846 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1847 |
|
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1848 |
/** |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1849 |
* returns the list of uninstantiated variables (as type-variables) in this |
15705 | 1850 |
* inference context |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1851 |
*/ |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1852 |
List<Type> restvars() { |
15705 | 1853 |
return filterVars(new Filter<UndetVar>() { |
1854 |
public boolean accepts(UndetVar uv) { |
|
1855 |
return uv.inst == null; |
|
1856 |
} |
|
1857 |
}); |
|
1858 |
} |
|
1859 |
||
1860 |
/** |
|
1861 |
* returns the list of instantiated variables (as type-variables) in this |
|
1862 |
* inference context |
|
1863 |
*/ |
|
1864 |
List<Type> instvars() { |
|
1865 |
return filterVars(new Filter<UndetVar>() { |
|
1866 |
public boolean accepts(UndetVar uv) { |
|
1867 |
return uv.inst != null; |
|
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1868 |
} |
15705 | 1869 |
}); |
1870 |
} |
|
1871 |
||
1872 |
/** |
|
1873 |
* Get list of bounded inference variables (where bound is other than |
|
1874 |
* declared bounds). |
|
1875 |
*/ |
|
1876 |
final List<Type> boundedVars() { |
|
1877 |
return filterVars(new Filter<UndetVar>() { |
|
1878 |
public boolean accepts(UndetVar uv) { |
|
1879 |
return uv.getBounds(InferenceBound.UPPER) |
|
19914 | 1880 |
.diff(uv.getDeclaredBounds()) |
1881 |
.appendList(uv.getBounds(InferenceBound.EQ, InferenceBound.LOWER)).nonEmpty(); |
|
15705 | 1882 |
} |
1883 |
}); |
|
1884 |
} |
|
1885 |
||
1886 |
private List<Type> filterVars(Filter<UndetVar> fu) { |
|
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19914
diff
changeset
|
1887 |
ListBuffer<Type> res = new ListBuffer<>(); |
15705 | 1888 |
for (Type t : undetvars) { |
1889 |
UndetVar uv = (UndetVar)t; |
|
1890 |
if (fu.accepts(uv)) { |
|
1891 |
res.append(uv.qtype); |
|
1892 |
} |
|
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1893 |
} |
15705 | 1894 |
return res.toList(); |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1895 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1896 |
|
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1897 |
/** |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1898 |
* is this type free? |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1899 |
*/ |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1900 |
final boolean free(Type t) { |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1901 |
return t.containsAny(inferencevars); |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1902 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1903 |
|
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1904 |
final boolean free(List<Type> ts) { |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1905 |
for (Type t : ts) { |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1906 |
if (free(t)) return true; |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1907 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1908 |
return false; |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1909 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1910 |
|
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1911 |
/** |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1912 |
* Returns a list of free variables in a given type |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1913 |
*/ |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1914 |
final List<Type> freeVarsIn(Type t) { |
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19914
diff
changeset
|
1915 |
ListBuffer<Type> buf = new ListBuffer<>(); |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1916 |
for (Type iv : inferenceVars()) { |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1917 |
if (t.contains(iv)) { |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1918 |
buf.add(iv); |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1919 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1920 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1921 |
return buf.toList(); |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1922 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1923 |
|
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1924 |
final List<Type> freeVarsIn(List<Type> ts) { |
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19914
diff
changeset
|
1925 |
ListBuffer<Type> buf = new ListBuffer<>(); |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1926 |
for (Type t : ts) { |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1927 |
buf.appendList(freeVarsIn(t)); |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1928 |
} |
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19914
diff
changeset
|
1929 |
ListBuffer<Type> buf2 = new ListBuffer<>(); |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1930 |
for (Type t : buf) { |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1931 |
if (!buf2.contains(t)) { |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1932 |
buf2.add(t); |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1933 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1934 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1935 |
return buf2.toList(); |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1936 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1937 |
|
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1938 |
/** |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1939 |
* Replace all free variables in a given type with corresponding |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1940 |
* undet vars (used ahead of subtyping/compatibility checks to allow propagation |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1941 |
* of inference constraints). |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1942 |
*/ |
15705 | 1943 |
final Type asFree(Type t) { |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1944 |
return types.subst(t, inferencevars, undetvars); |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1945 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1946 |
|
15705 | 1947 |
final List<Type> asFree(List<Type> ts) { |
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19914
diff
changeset
|
1948 |
ListBuffer<Type> buf = new ListBuffer<>(); |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1949 |
for (Type t : ts) { |
15705 | 1950 |
buf.append(asFree(t)); |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1951 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1952 |
return buf.toList(); |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1953 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1954 |
|
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1955 |
List<Type> instTypes() { |
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19914
diff
changeset
|
1956 |
ListBuffer<Type> buf = new ListBuffer<>(); |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1957 |
for (Type t : undetvars) { |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1958 |
UndetVar uv = (UndetVar)t; |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1959 |
buf.append(uv.inst != null ? uv.inst : uv.qtype); |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1960 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1961 |
return buf.toList(); |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1962 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1963 |
|
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1964 |
/** |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1965 |
* Replace all free variables in a given type with corresponding |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1966 |
* instantiated types - if one or more free variable has not been |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1967 |
* fully instantiated, it will still be available in the resulting type. |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1968 |
*/ |
15705 | 1969 |
Type asInstType(Type t) { |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1970 |
return types.subst(t, inferencevars, instTypes()); |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1971 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1972 |
|
15705 | 1973 |
List<Type> asInstTypes(List<Type> ts) { |
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19914
diff
changeset
|
1974 |
ListBuffer<Type> buf = new ListBuffer<>(); |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1975 |
for (Type t : ts) { |
15705 | 1976 |
buf.append(asInstType(t)); |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1977 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1978 |
return buf.toList(); |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1979 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1980 |
|
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1981 |
/** |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1982 |
* Add custom hook for performing post-inference action |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1983 |
*/ |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1984 |
void addFreeTypeListener(List<Type> types, FreeTypeListener ftl) { |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1985 |
freeTypeListeners.put(ftl, freeVarsIn(types)); |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1986 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1987 |
|
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1988 |
/** |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1989 |
* Mark the inference context as complete and trigger evaluation |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1990 |
* of all deferred checks. |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1991 |
*/ |
15705 | 1992 |
void notifyChange() { |
15717 | 1993 |
notifyChange(inferencevars.diff(restvars())); |
1994 |
} |
|
1995 |
||
1996 |
void notifyChange(List<Type> inferredVars) { |
|
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1997 |
InferenceException thrownEx = null; |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1998 |
for (Map.Entry<FreeTypeListener, List<Type>> entry : |
22163 | 1999 |
new HashMap<>(freeTypeListeners).entrySet()) { |
15717 | 2000 |
if (!Type.containsAny(entry.getValue(), inferencevars.diff(inferredVars))) { |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
2001 |
try { |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
2002 |
entry.getKey().typesInferred(this); |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
2003 |
freeTypeListeners.remove(entry.getKey()); |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
2004 |
} catch (InferenceException ex) { |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
2005 |
if (thrownEx == null) { |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
2006 |
thrownEx = ex; |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
2007 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
2008 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
2009 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
2010 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
2011 |
//inference exception multiplexing - present any inference exception |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
2012 |
//thrown when processing listeners as a single one |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
2013 |
if (thrownEx != null) { |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
2014 |
throw thrownEx; |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
2015 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
2016 |
} |
14057
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
2017 |
|
15717 | 2018 |
/** |
2019 |
* Save the state of this inference context |
|
2020 |
*/ |
|
18904
3c4c6457ec5b
8019824: very long error messages on inference error
mcimadamore
parents:
18646
diff
changeset
|
2021 |
List<Type> save() { |
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19914
diff
changeset
|
2022 |
ListBuffer<Type> buf = new ListBuffer<>(); |
15717 | 2023 |
for (Type t : undetvars) { |
2024 |
UndetVar uv = (UndetVar)t; |
|
2025 |
UndetVar uv2 = new UndetVar((TypeVar)uv.qtype, types); |
|
2026 |
for (InferenceBound ib : InferenceBound.values()) { |
|
2027 |
for (Type b : uv.getBounds(ib)) { |
|
2028 |
uv2.addBound(ib, b, types); |
|
2029 |
} |
|
2030 |
} |
|
2031 |
uv2.inst = uv.inst; |
|
2032 |
buf.add(uv2); |
|
2033 |
} |
|
18904
3c4c6457ec5b
8019824: very long error messages on inference error
mcimadamore
parents:
18646
diff
changeset
|
2034 |
return buf.toList(); |
15717 | 2035 |
} |
2036 |
||
2037 |
/** |
|
2038 |
* Restore the state of this inference context to the previous known checkpoint |
|
2039 |
*/ |
|
18904
3c4c6457ec5b
8019824: very long error messages on inference error
mcimadamore
parents:
18646
diff
changeset
|
2040 |
void rollback(List<Type> saved_undet) { |
3c4c6457ec5b
8019824: very long error messages on inference error
mcimadamore
parents:
18646
diff
changeset
|
2041 |
Assert.check(saved_undet != null && saved_undet.length() == undetvars.length()); |
3c4c6457ec5b
8019824: very long error messages on inference error
mcimadamore
parents:
18646
diff
changeset
|
2042 |
//restore bounds (note: we need to preserve the old instances) |
3c4c6457ec5b
8019824: very long error messages on inference error
mcimadamore
parents:
18646
diff
changeset
|
2043 |
for (Type t : undetvars) { |
3c4c6457ec5b
8019824: very long error messages on inference error
mcimadamore
parents:
18646
diff
changeset
|
2044 |
UndetVar uv = (UndetVar)t; |
3c4c6457ec5b
8019824: very long error messages on inference error
mcimadamore
parents:
18646
diff
changeset
|
2045 |
UndetVar uv_saved = (UndetVar)saved_undet.head; |
3c4c6457ec5b
8019824: very long error messages on inference error
mcimadamore
parents:
18646
diff
changeset
|
2046 |
for (InferenceBound ib : InferenceBound.values()) { |
3c4c6457ec5b
8019824: very long error messages on inference error
mcimadamore
parents:
18646
diff
changeset
|
2047 |
uv.setBounds(ib, uv_saved.getBounds(ib)); |
3c4c6457ec5b
8019824: very long error messages on inference error
mcimadamore
parents:
18646
diff
changeset
|
2048 |
} |
3c4c6457ec5b
8019824: very long error messages on inference error
mcimadamore
parents:
18646
diff
changeset
|
2049 |
uv.inst = uv_saved.inst; |
3c4c6457ec5b
8019824: very long error messages on inference error
mcimadamore
parents:
18646
diff
changeset
|
2050 |
saved_undet = saved_undet.tail; |
3c4c6457ec5b
8019824: very long error messages on inference error
mcimadamore
parents:
18646
diff
changeset
|
2051 |
} |
15717 | 2052 |
} |
2053 |
||
2054 |
/** |
|
2055 |
* Copy variable in this inference context to the given context |
|
2056 |
*/ |
|
2057 |
void dupTo(final InferenceContext that) { |
|
2058 |
that.inferencevars = that.inferencevars.appendList(inferencevars); |
|
2059 |
that.undetvars = that.undetvars.appendList(undetvars); |
|
2060 |
//set up listeners to notify original inference contexts as |
|
2061 |
//propagated vars are inferred in new context |
|
2062 |
for (Type t : inferencevars) { |
|
2063 |
that.freeTypeListeners.put(new FreeTypeListener() { |
|
2064 |
public void typesInferred(InferenceContext inferenceContext) { |
|
2065 |
InferenceContext.this.notifyChange(); |
|
2066 |
} |
|
2067 |
}, List.of(t)); |
|
2068 |
} |
|
2069 |
} |
|
2070 |
||
19914 | 2071 |
private void solve(GraphStrategy ss, Warner warn) { |
22165
ec53c8946fc2
8030807: langtools should still build using jdk 7
vromero
parents:
22163
diff
changeset
|
2072 |
solve(ss, new HashMap<Type, Set<Type>>(), warn); |
19914 | 2073 |
} |
2074 |
||
15717 | 2075 |
/** |
2076 |
* Solve with given graph strategy. |
|
2077 |
*/ |
|
19914 | 2078 |
private void solve(GraphStrategy ss, Map<Type, Set<Type>> stuckDeps, Warner warn) { |
2079 |
GraphSolver s = new GraphSolver(this, stuckDeps, warn); |
|
15717 | 2080 |
s.solve(ss); |
2081 |
} |
|
2082 |
||
2083 |
/** |
|
2084 |
* Solve all variables in this context. |
|
2085 |
*/ |
|
2086 |
public void solve(Warner warn) { |
|
2087 |
solve(new LeafSolver() { |
|
2088 |
public boolean done() { |
|
2089 |
return restvars().isEmpty(); |
|
2090 |
} |
|
2091 |
}, warn); |
|
2092 |
} |
|
2093 |
||
2094 |
/** |
|
2095 |
* Solve all variables in the given list. |
|
2096 |
*/ |
|
2097 |
public void solve(final List<Type> vars, Warner warn) { |
|
2098 |
solve(new BestLeafSolver(vars) { |
|
2099 |
public boolean done() { |
|
2100 |
return !free(asInstTypes(vars)); |
|
2101 |
} |
|
2102 |
}, warn); |
|
2103 |
} |
|
2104 |
||
2105 |
/** |
|
2106 |
* Solve at least one variable in given list. |
|
2107 |
*/ |
|
19914 | 2108 |
public void solveAny(List<Type> varsToSolve, Map<Type, Set<Type>> optDeps, Warner warn) { |
2109 |
solve(new BestLeafSolver(varsToSolve.intersect(restvars())) { |
|
15717 | 2110 |
public boolean done() { |
2111 |
return instvars().intersect(varsToSolve).nonEmpty(); |
|
2112 |
} |
|
19914 | 2113 |
}, optDeps, warn); |
15717 | 2114 |
} |
2115 |
||
2116 |
/** |
|
2117 |
* Apply a set of inference steps |
|
2118 |
*/ |
|
2119 |
private boolean solveBasic(EnumSet<InferenceStep> steps) { |
|
2120 |
return solveBasic(inferencevars, steps); |
|
2121 |
} |
|
2122 |
||
2123 |
private boolean solveBasic(List<Type> varsToSolve, EnumSet<InferenceStep> steps) { |
|
2124 |
boolean changed = false; |
|
2125 |
for (Type t : varsToSolve.intersect(restvars())) { |
|
15705 | 2126 |
UndetVar uv = (UndetVar)asFree(t); |
15717 | 2127 |
for (InferenceStep step : steps) { |
2128 |
if (step.accepts(uv, this)) { |
|
2129 |
uv.inst = step.solve(uv, this); |
|
2130 |
changed = true; |
|
2131 |
break; |
|
14057
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
2132 |
} |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
2133 |
} |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
2134 |
} |
15717 | 2135 |
return changed; |
2136 |
} |
|
2137 |
||
2138 |
/** |
|
2139 |
* Instantiate inference variables in legacy mode (JLS 15.12.2.7, 15.12.2.8). |
|
2140 |
* During overload resolution, instantiation is done by doing a partial |
|
2141 |
* inference process using eq/lower bound instantiation. During check, |
|
2142 |
* we also instantiate any remaining vars by repeatedly using eq/upper |
|
2143 |
* instantiation, until all variables are solved. |
|
2144 |
*/ |
|
2145 |
public void solveLegacy(boolean partial, Warner warn, EnumSet<InferenceStep> steps) { |
|
2146 |
while (true) { |
|
2147 |
boolean stuck = !solveBasic(steps); |
|
2148 |
if (restvars().isEmpty() || partial) { |
|
2149 |
//all variables have been instantiated - exit |
|
2150 |
break; |
|
2151 |
} else if (stuck) { |
|
2152 |
//some variables could not be instantiated because of cycles in |
|
2153 |
//upper bounds - provide a (possibly recursive) default instantiation |
|
2154 |
instantiateAsUninferredVars(restvars(), this); |
|
2155 |
break; |
|
2156 |
} else { |
|
2157 |
//some variables have been instantiated - replace newly instantiated |
|
2158 |
//variables in remaining upper bounds and continue |
|
2159 |
for (Type t : undetvars) { |
|
2160 |
UndetVar uv = (UndetVar)t; |
|
2161 |
uv.substBounds(inferenceVars(), instTypes(), types); |
|
2162 |
} |
|
2163 |
} |
|
14057
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
2164 |
} |
15717 | 2165 |
checkWithinBounds(this, warn); |
2166 |
} |
|
2167 |
||
2168 |
private Infer infer() { |
|
2169 |
//back-door to infer |
|
2170 |
return Infer.this; |
|
14057
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
2171 |
} |
8616
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
2172 |
} |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
2173 |
|
15705 | 2174 |
final InferenceContext emptyContext = new InferenceContext(List.<Type>nil()); |
2175 |
// </editor-fold> |
|
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
2176 |
} |