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