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