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