author | mcimadamore |
Thu, 09 Feb 2017 15:19:05 +0000 | |
changeset 43761 | f50592785b30 |
parent 42828 | cce89649f958 |
child 44230 | 6f46f350a21b |
child 44186 | fe848a208b20 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
36157
fdbf6c9be2ab
8145472: replace remaining java.io.File with java.nio.file.Path
jjg
parents:
33917
diff
changeset
|
2 |
* Copyright (c) 1999, 2016, 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 |
||
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
28 |
import com.sun.tools.javac.code.Type.UndetVar.UndetVarListener; |
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; |
|
32709
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
41 |
import com.sun.tools.javac.comp.DeferredAttr.DeferredAttrContext; |
15717 | 42 |
import com.sun.tools.javac.comp.Infer.GraphSolver.InferenceGraph; |
43 |
import com.sun.tools.javac.comp.Infer.GraphSolver.InferenceGraph.Node; |
|
44 |
import com.sun.tools.javac.comp.Resolve.InapplicableMethodException; |
|
45 |
import com.sun.tools.javac.comp.Resolve.VerboseResolutionMode; |
|
46 |
||
26267
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
47 |
import java.io.IOException; |
36157
fdbf6c9be2ab
8145472: replace remaining java.io.File with java.nio.file.Path
jjg
parents:
33917
diff
changeset
|
48 |
import java.io.Writer; |
fdbf6c9be2ab
8145472: replace remaining java.io.File with java.nio.file.Path
jjg
parents:
33917
diff
changeset
|
49 |
import java.nio.file.Files; |
fdbf6c9be2ab
8145472: replace remaining java.io.File with java.nio.file.Path
jjg
parents:
33917
diff
changeset
|
50 |
import java.nio.file.Path; |
fdbf6c9be2ab
8145472: replace remaining java.io.File with java.nio.file.Path
jjg
parents:
33917
diff
changeset
|
51 |
import java.nio.file.Paths; |
15717 | 52 |
import java.util.ArrayList; |
25844
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
53 |
import java.util.Collection; |
15717 | 54 |
import java.util.Collections; |
55 |
import java.util.EnumSet; |
|
19914 | 56 |
import java.util.HashMap; |
15717 | 57 |
import java.util.HashSet; |
19914 | 58 |
import java.util.Map; |
39589
0cbaed77814c
8048543: JLS8 18.5.3: inference variable seems to be instantiated unexpectedly
vromero
parents:
38832
diff
changeset
|
59 |
import java.util.Optional; |
25844
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
60 |
import java.util.Properties; |
19914 | 61 |
import java.util.Set; |
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
62 |
import java.util.function.BiFunction; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
63 |
import java.util.function.BiPredicate; |
40594
8cbf5df9c503
8164399: inference of thrown variable does not work correctly
mcimadamore
parents:
39599
diff
changeset
|
64 |
import java.util.stream.Collectors; |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
65 |
|
39599 | 66 |
import com.sun.tools.javac.main.Option; |
67 |
||
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14258
diff
changeset
|
68 |
import static com.sun.tools.javac.code.TypeTag.*; |
10 | 69 |
|
70 |
/** Helper class for type parameter inference, used by the attribution phase. |
|
71 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5846
diff
changeset
|
72 |
* <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
|
73 |
* If you write code that depends on this, you do so at your own risk. |
10 | 74 |
* This code and its internal interfaces are subject to change or |
75 |
* deletion without notice.</b> |
|
76 |
*/ |
|
77 |
public class Infer { |
|
22163 | 78 |
protected static final Context.Key<Infer> inferKey = new Context.Key<>(); |
10 | 79 |
|
15717 | 80 |
Resolve rs; |
81 |
Check chk; |
|
10 | 82 |
Symtab syms; |
83 |
Types types; |
|
15717 | 84 |
JCDiagnostic.Factory diags; |
10816
ce8a7e9d8882
7098660: Write better overload resolution/inference tests
mcimadamore
parents:
10628
diff
changeset
|
85 |
Log log; |
10 | 86 |
|
15717 | 87 |
/** should the graph solver be used? */ |
88 |
boolean allowGraphInference; |
|
15374
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
15037
diff
changeset
|
89 |
|
26267
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 |
* folder in which the inference dependency graphs should be written. |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
92 |
*/ |
39599 | 93 |
private final String dependenciesFolder; |
26267
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
94 |
|
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
95 |
/** |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
96 |
* List of graphs awaiting to be dumped to a file. |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
97 |
*/ |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
98 |
private List<String> pendingGraphs; |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
99 |
|
10 | 100 |
public static Infer instance(Context context) { |
101 |
Infer instance = context.get(inferKey); |
|
102 |
if (instance == null) |
|
103 |
instance = new Infer(context); |
|
104 |
return instance; |
|
105 |
} |
|
106 |
||
107 |
protected Infer(Context context) { |
|
108 |
context.put(inferKey, this); |
|
15717 | 109 |
|
110 |
rs = Resolve.instance(context); |
|
111 |
chk = Check.instance(context); |
|
10 | 112 |
syms = Symtab.instance(context); |
113 |
types = Types.instance(context); |
|
15717 | 114 |
diags = JCDiagnostic.Factory.instance(context); |
10816
ce8a7e9d8882
7098660: Write better overload resolution/inference tests
mcimadamore
parents:
10628
diff
changeset
|
115 |
log = Log.instance(context); |
13440
1ea85052f98d
7187104: Inference cleanup: remove redundant exception classes in Infer.java
mcimadamore
parents:
13438
diff
changeset
|
116 |
inferenceException = new InferenceException(diags); |
15717 | 117 |
Options options = Options.instance(context); |
118 |
allowGraphInference = Source.instance(context).allowGraphInference() |
|
119 |
&& options.isUnset("useLegacyInference"); |
|
39599 | 120 |
dependenciesFolder = options.get("debug.dumpInferenceGraphsTo"); |
26267
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
121 |
pendingGraphs = List.nil(); |
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
122 |
|
42828
cce89649f958
8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents:
42416
diff
changeset
|
123 |
emptyContext = new InferenceContext(this, List.nil()); |
10 | 124 |
} |
125 |
||
15717 | 126 |
/** 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
|
127 |
public static final Type anyPoly = new JCNoType(); |
15717 | 128 |
|
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
129 |
/** |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
130 |
* 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
|
131 |
* 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
|
132 |
*/ |
11707
532f41763bc9
7129801: Merge the two method applicability routines
mcimadamore
parents:
11550
diff
changeset
|
133 |
public static class InferenceException extends InapplicableMethodException { |
10 | 134 |
private static final long serialVersionUID = 0; |
135 |
||
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
136 |
List<JCDiagnostic> messages = List.nil(); |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
137 |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
138 |
InferenceException(JCDiagnostic.Factory diags) { |
6710
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
139 |
super(diags); |
10 | 140 |
} |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
141 |
|
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
142 |
@Override |
19914 | 143 |
InapplicableMethodException setMessage() { |
144 |
//no message to set |
|
145 |
return this; |
|
146 |
} |
|
147 |
||
148 |
@Override |
|
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
149 |
InapplicableMethodException setMessage(JCDiagnostic diag) { |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
150 |
messages = messages.append(diag); |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
151 |
return this; |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
152 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
153 |
|
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
154 |
@Override |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
155 |
public JCDiagnostic getDiagnostic() { |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
156 |
return messages.head; |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
157 |
} |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
158 |
|
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
159 |
void clear() { |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
160 |
messages = List.nil(); |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
161 |
} |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
162 |
} |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
163 |
|
15717 | 164 |
protected final InferenceException inferenceException; |
10 | 165 |
|
15717 | 166 |
// <editor-fold defaultstate="collapsed" desc="Inference routines"> |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
167 |
/** |
15717 | 168 |
* Main inference entry point - instantiate a generic method type |
169 |
* using given argument types and (possibly) an expected target-type. |
|
10 | 170 |
*/ |
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
171 |
Type instantiateMethod( Env<AttrContext> env, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
172 |
List<Type> tvars, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
173 |
MethodType mt, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
174 |
Attr.ResultInfo resultInfo, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
175 |
MethodSymbol msym, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
176 |
List<Type> argtypes, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
177 |
boolean allowBoxing, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
178 |
boolean useVarargs, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
179 |
Resolve.MethodResolutionContext resolveContext, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
180 |
Warner warn) throws InferenceException { |
10 | 181 |
//-System.err.println("instantiateMethod(" + tvars + ", " + mt + ", " + argtypes + ")"); //DEBUG |
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
182 |
final InferenceContext inferenceContext = new InferenceContext(this, tvars); //B0 |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
183 |
inferenceException.clear(); |
15717 | 184 |
try { |
185 |
DeferredAttr.DeferredAttrContext deferredAttrContext = |
|
18910
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
186 |
resolveContext.deferredAttrContext(msym, inferenceContext, resultInfo, warn); |
6710
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
187 |
|
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
188 |
resolveContext.methodCheck.argumentsAcceptable(env, deferredAttrContext, //B2 |
15717 | 189 |
argtypes, mt.getParameterTypes(), warn); |
32709
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
190 |
|
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
191 |
if (allowGraphInference && resultInfo != null && resultInfo.pt == anyPoly) { |
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
192 |
doIncorporation(inferenceContext, warn); |
32709
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
193 |
//we are inside method attribution - just return a partially inferred type |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
194 |
return new PartiallyInferredMethodType(mt, inferenceContext, env, warn); |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
195 |
} else if (allowGraphInference && |
15717 | 196 |
resultInfo != null && |
15374
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
15037
diff
changeset
|
197 |
!warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED)) { |
15717 | 198 |
//inject return constraints earlier |
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
199 |
doIncorporation(inferenceContext, warn); //propagation |
33906
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
200 |
|
38538
8bdc63ff6961
8157149: Inference: weird propagation of thrown inference variables
mcimadamore
parents:
38516
diff
changeset
|
201 |
boolean shouldPropagate = shouldPropagate(mt.getReturnType(), resultInfo, inferenceContext); |
33906
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
202 |
|
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
203 |
InferenceContext minContext = shouldPropagate ? |
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
204 |
inferenceContext.min(roots(mt, deferredAttrContext), true, warn) : |
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
205 |
inferenceContext; |
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
206 |
|
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
207 |
Type newRestype = generateReturnConstraints(env.tree, resultInfo, //B3 |
33906
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
208 |
mt, minContext); |
18911 | 209 |
mt = (MethodType)types.createMethodTypeWithReturn(mt, newRestype); |
33906
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
210 |
|
15717 | 211 |
//propagate outwards if needed |
33906
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
212 |
if (shouldPropagate) { |
15717 | 213 |
//propagate inference context outwards and exit |
33906
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
214 |
minContext.dupTo(resultInfo.checkContext.inferenceContext()); |
15717 | 215 |
deferredAttrContext.complete(); |
216 |
return mt; |
|
217 |
} |
|
15374
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
15037
diff
changeset
|
218 |
} |
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
15037
diff
changeset
|
219 |
|
15037
8ba16ac61b1a
8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents:
14547
diff
changeset
|
220 |
deferredAttrContext.complete(); |
10 | 221 |
|
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
222 |
// minimize as yet undetermined type variables |
15717 | 223 |
if (allowGraphInference) { |
224 |
inferenceContext.solve(warn); |
|
225 |
} else { |
|
226 |
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
|
227 |
} |
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
228 |
|
15705 | 229 |
mt = (MethodType)inferenceContext.asInstType(mt); |
10 | 230 |
|
15717 | 231 |
if (!allowGraphInference && |
232 |
inferenceContext.restvars().nonEmpty() && |
|
233 |
resultInfo != null && |
|
234 |
!warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED)) { |
|
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
235 |
generateReturnConstraints(env.tree, resultInfo, mt, inferenceContext); |
15717 | 236 |
inferenceContext.solveLegacy(false, warn, LegacyInferenceSteps.EQ_UPPER.steps); //maximizeInst |
237 |
mt = (MethodType)inferenceContext.asInstType(mt); |
|
238 |
} |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
239 |
|
15717 | 240 |
if (resultInfo != null && rs.verboseResolutionMode.contains(VerboseResolutionMode.DEFERRED_INST)) { |
241 |
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
|
242 |
} |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
243 |
|
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
244 |
// return instantiated version of method type |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
245 |
return mt; |
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
246 |
} finally { |
15717 | 247 |
if (resultInfo != null || !allowGraphInference) { |
248 |
inferenceContext.notifyChange(); |
|
249 |
} else { |
|
250 |
inferenceContext.notifyChange(inferenceContext.boundedVars()); |
|
14048
308d1cf8fe46
7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents:
14047
diff
changeset
|
251 |
} |
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
252 |
if (resultInfo == null) { |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
253 |
/* 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
|
254 |
* 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
|
255 |
*/ |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
256 |
inferenceContext.captureTypeCache.clear(); |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
257 |
} |
26267
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
258 |
dumpGraphsIfNeeded(env.tree, msym, resolveContext); |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
259 |
} |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
260 |
} |
33906
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
261 |
//where |
38538
8bdc63ff6961
8157149: Inference: weird propagation of thrown inference variables
mcimadamore
parents:
38516
diff
changeset
|
262 |
private boolean shouldPropagate(Type restype, Attr.ResultInfo target, InferenceContext inferenceContext) { |
8bdc63ff6961
8157149: Inference: weird propagation of thrown inference variables
mcimadamore
parents:
38516
diff
changeset
|
263 |
return target.checkContext.inferenceContext() != emptyContext && //enclosing context is a generic method |
8bdc63ff6961
8157149: Inference: weird propagation of thrown inference variables
mcimadamore
parents:
38516
diff
changeset
|
264 |
inferenceContext.free(restype) && //return type contains inference vars |
8bdc63ff6961
8157149: Inference: weird propagation of thrown inference variables
mcimadamore
parents:
38516
diff
changeset
|
265 |
(!inferenceContext.inferencevars.contains(restype) || //no eager instantiation is required (as per 18.5.2) |
8bdc63ff6961
8157149: Inference: weird propagation of thrown inference variables
mcimadamore
parents:
38516
diff
changeset
|
266 |
!needsEagerInstantiation((UndetVar)inferenceContext.asUndetVar(restype), target.pt, inferenceContext)); |
8bdc63ff6961
8157149: Inference: weird propagation of thrown inference variables
mcimadamore
parents:
38516
diff
changeset
|
267 |
} |
8bdc63ff6961
8157149: Inference: weird propagation of thrown inference variables
mcimadamore
parents:
38516
diff
changeset
|
268 |
|
33906
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
269 |
private List<Type> roots(MethodType mt, DeferredAttrContext deferredAttrContext) { |
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
270 |
ListBuffer<Type> roots = new ListBuffer<>(); |
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
271 |
roots.add(mt.getReturnType()); |
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
272 |
if (deferredAttrContext != null && deferredAttrContext.mode == AttrMode.CHECK) { |
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
273 |
roots.addAll(mt.getThrownTypes()); |
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
274 |
for (DeferredAttr.DeferredAttrNode n : deferredAttrContext.deferredAttrNodes) { |
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
275 |
roots.addAll(n.deferredStuckPolicy.stuckVars()); |
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
276 |
roots.addAll(n.deferredStuckPolicy.depVars()); |
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
277 |
} |
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
278 |
} |
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
279 |
return roots.toList(); |
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
280 |
} |
26267
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
281 |
|
32709
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
282 |
/** |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
283 |
* A partially infered method/constructor type; such a type can be checked multiple times |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
284 |
* against different targets. |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
285 |
*/ |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
286 |
public class PartiallyInferredMethodType extends MethodType { |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
287 |
public PartiallyInferredMethodType(MethodType mtype, InferenceContext inferenceContext, Env<AttrContext> env, Warner warn) { |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
288 |
super(mtype.getParameterTypes(), mtype.getReturnType(), mtype.getThrownTypes(), mtype.tsym); |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
289 |
this.inferenceContext = inferenceContext; |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
290 |
this.env = env; |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
291 |
this.warn = warn; |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
292 |
} |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
293 |
|
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
294 |
/** The inference context. */ |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
295 |
final InferenceContext inferenceContext; |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
296 |
|
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
297 |
/** The attribution environment. */ |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
298 |
Env<AttrContext> env; |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
299 |
|
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
300 |
/** The warner. */ |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
301 |
final Warner warn; |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
302 |
|
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
303 |
@Override |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
304 |
public boolean isPartial() { |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
305 |
return true; |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
306 |
} |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
307 |
|
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
308 |
/** |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
309 |
* Checks this type against a target; this means generating return type constraints, solve |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
310 |
* and then roll back the results (to avoid poolluting the context). |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
311 |
*/ |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
312 |
Type check(Attr.ResultInfo resultInfo) { |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
313 |
Warner noWarnings = new Warner(null); |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
314 |
inferenceException.clear(); |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
315 |
List<Type> saved_undet = null; |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
316 |
try { |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
317 |
/** we need to save the inference context before generating target type constraints. |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
318 |
* This constraints may pollute the inference context and make it useless in case we |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
319 |
* need to use it several times: with several targets. |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
320 |
*/ |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
321 |
saved_undet = inferenceContext.save(); |
43761
f50592785b30
8174249: Regression in generic method unchecked calls
mcimadamore
parents:
42828
diff
changeset
|
322 |
boolean unchecked = warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED); |
f50592785b30
8174249: Regression in generic method unchecked calls
mcimadamore
parents:
42828
diff
changeset
|
323 |
if (allowGraphInference && !unchecked) { |
38538
8bdc63ff6961
8157149: Inference: weird propagation of thrown inference variables
mcimadamore
parents:
38516
diff
changeset
|
324 |
boolean shouldPropagate = shouldPropagate(getReturnType(), resultInfo, inferenceContext); |
33906
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
325 |
|
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
326 |
InferenceContext minContext = shouldPropagate ? |
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
327 |
inferenceContext.min(roots(asMethodType(), null), false, warn) : |
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
328 |
inferenceContext; |
32709
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
329 |
|
33906
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
330 |
MethodType other = (MethodType)minContext.update(asMethodType()); |
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
331 |
Type newRestype = generateReturnConstraints(env.tree, resultInfo, //B3 |
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
332 |
other, minContext); |
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
333 |
|
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
334 |
if (shouldPropagate) { |
32709
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
335 |
//propagate inference context outwards and exit |
33906
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
336 |
minContext.dupTo(resultInfo.checkContext.inferenceContext(), |
32709
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
337 |
resultInfo.checkContext.deferredAttrContext().insideOverloadPhase()); |
33906
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
338 |
return newRestype; |
32709
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
339 |
} |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
340 |
} |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
341 |
inferenceContext.solve(noWarnings); |
43761
f50592785b30
8174249: Regression in generic method unchecked calls
mcimadamore
parents:
42828
diff
changeset
|
342 |
Type ret = inferenceContext.asInstType(this).getReturnType(); |
f50592785b30
8174249: Regression in generic method unchecked calls
mcimadamore
parents:
42828
diff
changeset
|
343 |
//inline logic from Attr.checkMethod - if unchecked conversion was required, erase |
f50592785b30
8174249: Regression in generic method unchecked calls
mcimadamore
parents:
42828
diff
changeset
|
344 |
//return type _after_ resolution |
f50592785b30
8174249: Regression in generic method unchecked calls
mcimadamore
parents:
42828
diff
changeset
|
345 |
return unchecked ? types.erasure(ret) : ret; |
32709
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
346 |
} catch (InferenceException ex) { |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
347 |
resultInfo.checkContext.report(null, ex.getDiagnostic()); |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
348 |
Assert.error(); //cannot get here (the above should throw) |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
349 |
return null; |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
350 |
} finally { |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
351 |
if (saved_undet != null) { |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
352 |
inferenceContext.rollback(saved_undet); |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
353 |
} |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
354 |
} |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
355 |
} |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
356 |
} |
55d136799f79
8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents:
31937
diff
changeset
|
357 |
|
26267
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
358 |
private void dumpGraphsIfNeeded(DiagnosticPosition pos, Symbol msym, Resolve.MethodResolutionContext rsContext) { |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
359 |
int round = 0; |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
360 |
try { |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
361 |
for (String graph : pendingGraphs.reverse()) { |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
362 |
Assert.checkNonNull(dependenciesFolder); |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
363 |
Name name = msym.name == msym.name.table.names.init ? |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
364 |
msym.owner.name : msym.name; |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
365 |
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
|
366 |
name, |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
367 |
pos.getStartPosition(), |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
368 |
rsContext.attrMode(), |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
369 |
rsContext.step, |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
370 |
round); |
36157
fdbf6c9be2ab
8145472: replace remaining java.io.File with java.nio.file.Path
jjg
parents:
33917
diff
changeset
|
371 |
Path dotFile = Paths.get(dependenciesFolder, filename); |
fdbf6c9be2ab
8145472: replace remaining java.io.File with java.nio.file.Path
jjg
parents:
33917
diff
changeset
|
372 |
try (Writer w = Files.newBufferedWriter(dotFile)) { |
fdbf6c9be2ab
8145472: replace remaining java.io.File with java.nio.file.Path
jjg
parents:
33917
diff
changeset
|
373 |
w.append(graph); |
26267
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
374 |
} |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
375 |
round++; |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
376 |
} |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
377 |
} catch (IOException ex) { |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
378 |
Assert.error("Error occurred when dumping inference graph: " + ex.getMessage()); |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
379 |
} finally { |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
380 |
pendingGraphs = List.nil(); |
10 | 381 |
} |
8616
5a47f5535883
7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents:
8229
diff
changeset
|
382 |
} |
10 | 383 |
|
15717 | 384 |
/** |
385 |
* Generate constraints from the generic method's return type. If the method |
|
386 |
* call occurs in a context where a type T is expected, use the expected |
|
387 |
* type to derive more constraints on the generic method inference variables. |
|
388 |
*/ |
|
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
389 |
Type generateReturnConstraints(JCTree tree, Attr.ResultInfo resultInfo, |
15717 | 390 |
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
|
391 |
InferenceContext rsInfoInfContext = resultInfo.checkContext.inferenceContext(); |
18911 | 392 |
Type from = mt.getReturnType(); |
393 |
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
|
394 |
rsInfoInfContext != emptyContext) { |
18911 | 395 |
from = types.capture(from); |
396 |
//add synthetic captured ivars |
|
397 |
for (Type t : from.getTypeArguments()) { |
|
398 |
if (t.hasTag(TYPEVAR) && ((TypeVar)t).isCaptured()) { |
|
399 |
inferenceContext.addVar((TypeVar)t); |
|
400 |
} |
|
401 |
} |
|
402 |
} |
|
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
403 |
Type qtype = inferenceContext.asUndetVar(from); |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
404 |
Type to = resultInfo.pt; |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
405 |
|
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
406 |
if (qtype.hasTag(VOID)) { |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
407 |
to = syms.voidType; |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
408 |
} else if (to.hasTag(NONE)) { |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
409 |
to = from.isPrimitive() ? from : syms.objectType; |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
410 |
} else if (qtype.hasTag(UNDETVAR)) { |
41855
0d1a58e6beb7
8168134: Inference: javac incorrectly propagating inner constraint with primitive target
mcimadamore
parents:
40594
diff
changeset
|
411 |
if (needsEagerInstantiation((UndetVar)qtype, to, inferenceContext) && |
0d1a58e6beb7
8168134: Inference: javac incorrectly propagating inner constraint with primitive target
mcimadamore
parents:
40594
diff
changeset
|
412 |
(allowGraphInference || !to.isPrimitive())) { |
0d1a58e6beb7
8168134: Inference: javac incorrectly propagating inner constraint with primitive target
mcimadamore
parents:
40594
diff
changeset
|
413 |
to = generateReferenceToTargetConstraint(tree, (UndetVar)qtype, to, resultInfo, inferenceContext); |
0d1a58e6beb7
8168134: Inference: javac incorrectly propagating inner constraint with primitive target
mcimadamore
parents:
40594
diff
changeset
|
414 |
} else if (to.isPrimitive()) { |
0d1a58e6beb7
8168134: Inference: javac incorrectly propagating inner constraint with primitive target
mcimadamore
parents:
40594
diff
changeset
|
415 |
to = types.boxedClass(to).type; |
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
416 |
} |
33708
99085a01b536
8141613: Compiler fails to infer generic type
mcimadamore
parents:
32915
diff
changeset
|
417 |
} else if (rsInfoInfContext.free(resultInfo.pt)) { |
99085a01b536
8141613: Compiler fails to infer generic type
mcimadamore
parents:
32915
diff
changeset
|
418 |
//propagation - cache captured vars |
99085a01b536
8141613: Compiler fails to infer generic type
mcimadamore
parents:
32915
diff
changeset
|
419 |
qtype = inferenceContext.asUndetVar(rsInfoInfContext.cachedCapture(tree, from, false)); |
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
420 |
} |
23132
00a059740d87
8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents:
23124
diff
changeset
|
421 |
Assert.check(allowGraphInference || !rsInfoInfContext.free(to), |
15717 | 422 |
"legacy inference engine cannot handle constraints on both sides of a subtyping assertion"); |
423 |
//we need to skip capture? |
|
424 |
Warner retWarn = new Warner(); |
|
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
425 |
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
|
426 |
//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
|
427 |
(!allowGraphInference && retWarn.hasLint(Lint.LintCategory.UNCHECKED))) { |
15717 | 428 |
throw inferenceException |
429 |
.setMessage("infer.no.conforming.instance.exists", |
|
430 |
inferenceContext.restvars(), mt.getReturnType(), to); |
|
431 |
} |
|
18911 | 432 |
return from; |
15717 | 433 |
} |
18910
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
434 |
|
41855
0d1a58e6beb7
8168134: Inference: javac incorrectly propagating inner constraint with primitive target
mcimadamore
parents:
40594
diff
changeset
|
435 |
private boolean needsEagerInstantiation(UndetVar from, Type to, InferenceContext inferenceContext) { |
0d1a58e6beb7
8168134: Inference: javac incorrectly propagating inner constraint with primitive target
mcimadamore
parents:
40594
diff
changeset
|
436 |
if (to.isPrimitive()) { |
0d1a58e6beb7
8168134: Inference: javac incorrectly propagating inner constraint with primitive target
mcimadamore
parents:
40594
diff
changeset
|
437 |
/* T is a primitive type, and one of the primitive wrapper classes is an instantiation, |
0d1a58e6beb7
8168134: Inference: javac incorrectly propagating inner constraint with primitive target
mcimadamore
parents:
40594
diff
changeset
|
438 |
* upper bound, or lower bound for alpha in B2. |
0d1a58e6beb7
8168134: Inference: javac incorrectly propagating inner constraint with primitive target
mcimadamore
parents:
40594
diff
changeset
|
439 |
*/ |
0d1a58e6beb7
8168134: Inference: javac incorrectly propagating inner constraint with primitive target
mcimadamore
parents:
40594
diff
changeset
|
440 |
for (Type t : from.getBounds(InferenceBound.values())) { |
0d1a58e6beb7
8168134: Inference: javac incorrectly propagating inner constraint with primitive target
mcimadamore
parents:
40594
diff
changeset
|
441 |
Type boundAsPrimitive = types.unboxedType(t); |
0d1a58e6beb7
8168134: Inference: javac incorrectly propagating inner constraint with primitive target
mcimadamore
parents:
40594
diff
changeset
|
442 |
if (boundAsPrimitive == null || boundAsPrimitive.hasTag(NONE)) { |
0d1a58e6beb7
8168134: Inference: javac incorrectly propagating inner constraint with primitive target
mcimadamore
parents:
40594
diff
changeset
|
443 |
continue; |
0d1a58e6beb7
8168134: Inference: javac incorrectly propagating inner constraint with primitive target
mcimadamore
parents:
40594
diff
changeset
|
444 |
} |
0d1a58e6beb7
8168134: Inference: javac incorrectly propagating inner constraint with primitive target
mcimadamore
parents:
40594
diff
changeset
|
445 |
return true; |
0d1a58e6beb7
8168134: Inference: javac incorrectly propagating inner constraint with primitive target
mcimadamore
parents:
40594
diff
changeset
|
446 |
} |
0d1a58e6beb7
8168134: Inference: javac incorrectly propagating inner constraint with primitive target
mcimadamore
parents:
40594
diff
changeset
|
447 |
return false; |
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
448 |
} |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
449 |
|
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
450 |
Type captureOfTo = types.capture(to); |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
451 |
/* 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
|
452 |
*/ |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
453 |
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
|
454 |
/* 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
|
455 |
* 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
|
456 |
*/ |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
457 |
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
|
458 |
Type captureOfBound = types.capture(t); |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
459 |
if (captureOfBound != t) { |
38538
8bdc63ff6961
8157149: Inference: weird propagation of thrown inference variables
mcimadamore
parents:
38516
diff
changeset
|
460 |
return true; |
15717 | 461 |
} |
12468
1100643c0209
7154127: Inference cleanup: remove bound check analysis from visitors in Types.java
mcimadamore
parents:
12335
diff
changeset
|
462 |
} |
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
463 |
|
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
464 |
/* 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
|
465 |
* 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
|
466 |
* 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
|
467 |
*/ |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
468 |
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
|
469 |
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
|
470 |
if (aLowerBound != anotherLowerBound && |
26663
f2d8845a9bb2
8055963: Inference failure with nested invocation
mcimadamore
parents:
26267
diff
changeset
|
471 |
!inferenceContext.free(aLowerBound) && |
f2d8845a9bb2
8055963: Inference failure with nested invocation
mcimadamore
parents:
26267
diff
changeset
|
472 |
!inferenceContext.free(anotherLowerBound) && |
f2d8845a9bb2
8055963: Inference failure with nested invocation
mcimadamore
parents:
26267
diff
changeset
|
473 |
commonSuperWithDiffParameterization(aLowerBound, anotherLowerBound)) { |
38538
8bdc63ff6961
8157149: Inference: weird propagation of thrown inference variables
mcimadamore
parents:
38516
diff
changeset
|
474 |
return true; |
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
475 |
} |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
476 |
} |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
477 |
} |
10 | 478 |
} |
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
479 |
|
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
480 |
/* 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
|
481 |
* 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
|
482 |
* 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
|
483 |
* 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
|
484 |
*/ |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
485 |
if (to.isParameterized()) { |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
486 |
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
|
487 |
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
|
488 |
if (sup != null && sup.isRaw()) { |
38538
8bdc63ff6961
8157149: Inference: weird propagation of thrown inference variables
mcimadamore
parents:
38516
diff
changeset
|
489 |
return true; |
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
490 |
} |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
491 |
} |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
492 |
} |
38538
8bdc63ff6961
8157149: Inference: weird propagation of thrown inference variables
mcimadamore
parents:
38516
diff
changeset
|
493 |
return false; |
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
494 |
} |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
495 |
|
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
496 |
private boolean commonSuperWithDiffParameterization(Type t, Type s) { |
24795
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
497 |
for (Pair<Type, Type> supers : getParameterizedSupers(t, s)) { |
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
498 |
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
|
499 |
} |
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
500 |
return false; |
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
501 |
} |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
502 |
|
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
503 |
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
|
504 |
Type to, Attr.ResultInfo resultInfo, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
505 |
InferenceContext inferenceContext) { |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
506 |
inferenceContext.solve(List.of(from.qtype), new Warner()); |
25279 | 507 |
inferenceContext.notifyChange(); |
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
508 |
Type capturedType = resultInfo.checkContext.inferenceContext() |
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
509 |
.cachedCapture(tree, from.getInst(), false); |
24226
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
510 |
if (types.isConvertible(capturedType, |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
511 |
resultInfo.checkContext.inferenceContext().asUndetVar(to))) { |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
512 |
//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
|
513 |
return syms.objectType; |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
514 |
} |
08b586e22328
8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents:
24063
diff
changeset
|
515 |
return to; |
18910
c967bfda9283
8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents:
18909
diff
changeset
|
516 |
} |
12468
1100643c0209
7154127: Inference cleanup: remove bound check analysis from visitors in Types.java
mcimadamore
parents:
12335
diff
changeset
|
517 |
|
15717 | 518 |
/** |
519 |
* Infer cyclic inference variables as described in 15.12.2.8. |
|
520 |
*/ |
|
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
521 |
void instantiateAsUninferredVars(List<Type> vars, InferenceContext inferenceContext) { |
20249
93f8eae31092
6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents:
19914
diff
changeset
|
522 |
ListBuffer<Type> todo = new ListBuffer<>(); |
15717 | 523 |
//step 1 - create fresh tvars |
524 |
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
|
525 |
UndetVar uv = (UndetVar)inferenceContext.asUndetVar(t); |
15717 | 526 |
List<Type> upperBounds = uv.getBounds(InferenceBound.UPPER); |
527 |
if (Type.containsAny(upperBounds, vars)) { |
|
16967
79d444669f3f
7015104: use new subtype of TypeSymbol for type parameters
jfranck
parents:
16805
diff
changeset
|
528 |
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
|
529 |
fresh_tvar.type = new TypeVar(fresh_tvar, types.makeIntersectionType(uv.getBounds(InferenceBound.UPPER)), null); |
15717 | 530 |
todo.append(uv); |
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
531 |
uv.setInst(fresh_tvar.type); |
15717 | 532 |
} else if (upperBounds.nonEmpty()) { |
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
533 |
uv.setInst(types.glb(upperBounds)); |
15717 | 534 |
} else { |
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
535 |
uv.setInst(syms.objectType); |
12468
1100643c0209
7154127: Inference cleanup: remove bound check analysis from visitors in Types.java
mcimadamore
parents:
12335
diff
changeset
|
536 |
} |
15717 | 537 |
} |
538 |
//step 2 - replace fresh tvars in their bounds |
|
539 |
List<Type> formals = vars; |
|
540 |
for (Type t : todo) { |
|
541 |
UndetVar uv = (UndetVar)t; |
|
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
542 |
TypeVar ct = (TypeVar)uv.getInst(); |
15717 | 543 |
ct.bound = types.glb(inferenceContext.asInstTypes(types.getBounds(ct))); |
544 |
if (ct.bound.isErroneous()) { |
|
545 |
//report inference error if glb fails |
|
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
546 |
reportBoundError(uv, InferenceBound.UPPER); |
15374
fb8f6acf09cc
8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents:
15037
diff
changeset
|
547 |
} |
15717 | 548 |
formals = formals.tail; |
14058
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
549 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
550 |
} |
c7ec7facdd20
7177385: Add attribution support for lambda expressions
mcimadamore
parents:
14057
diff
changeset
|
551 |
|
6592
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
552 |
/** |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
553 |
* 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
|
554 |
* 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
|
555 |
* 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
|
556 |
*/ |
12335
4725d88691dd
7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents:
12334
diff
changeset
|
557 |
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
|
558 |
MethodSymbol spMethod, // sig. poly. method or null if none |
14057
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
559 |
Resolve.MethodResolutionContext resolveContext, |
8036
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
560 |
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
|
561 |
final Type restype; |
8036
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
562 |
|
36995 | 563 |
if (spMethod == null || types.isSameType(spMethod.getReturnType(), syms.objectType, true)) { |
564 |
// The return type of the polymorphic signature is polymorphic, |
|
565 |
// and is computed from the enclosing tree E, as follows: |
|
566 |
// if E is a cast, then use the target type of the cast expression |
|
567 |
// as a return type; if E is an expression statement, the return |
|
568 |
// type is 'void'; otherwise |
|
569 |
// the return type is simply 'Object'. A correctness check ensures |
|
570 |
// that env.next refers to the lexically enclosing environment in |
|
571 |
// which 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
|
572 |
|
36995 | 573 |
switch (env.next.tree.getTag()) { |
574 |
case TYPECAST: |
|
575 |
JCTypeCast castTree = (JCTypeCast)env.next.tree; |
|
576 |
restype = (TreeInfo.skipParens(castTree.expr) == env.tree) ? |
|
577 |
castTree.clazz.type : |
|
578 |
syms.objectType; |
|
579 |
break; |
|
580 |
case EXEC: |
|
581 |
JCTree.JCExpressionStatement execTree = |
|
582 |
(JCTree.JCExpressionStatement)env.next.tree; |
|
583 |
restype = (TreeInfo.skipParens(execTree.expr) == env.tree) ? |
|
584 |
syms.voidType : |
|
585 |
syms.objectType; |
|
586 |
break; |
|
587 |
default: |
|
588 |
restype = syms.objectType; |
|
589 |
} |
|
590 |
} else { |
|
591 |
// The return type of the polymorphic signature is fixed |
|
592 |
// (not polymorphic) |
|
593 |
restype = spMethod.getReturnType(); |
|
6592
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
594 |
} |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
595 |
|
29554
6d7957bd6866
8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents:
29147
diff
changeset
|
596 |
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
|
597 |
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
|
598 |
spMethod.getThrownTypes() : |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
599 |
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
|
600 |
|
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
601 |
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
|
602 |
restype, |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
603 |
exType, |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
604 |
syms.methodClass); |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
605 |
return mtype; |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
606 |
} |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
607 |
//where |
14057
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
608 |
class ImplicitArgType extends DeferredAttr.DeferredTypeMap { |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
609 |
|
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
610 |
public ImplicitArgType(Symbol msym, Resolve.MethodResolutionPhase phase) { |
25279 | 611 |
(rs.deferredAttr).super(AttrMode.SPECULATIVE, msym, phase); |
14057
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
612 |
} |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
613 |
|
29554
6d7957bd6866
8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents:
29147
diff
changeset
|
614 |
@Override |
6d7957bd6866
8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents:
29147
diff
changeset
|
615 |
public Type visitClassType(ClassType t, Void aVoid) { |
6d7957bd6866
8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents:
29147
diff
changeset
|
616 |
return types.erasure(t); |
6d7957bd6866
8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents:
29147
diff
changeset
|
617 |
} |
6d7957bd6866
8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents:
29147
diff
changeset
|
618 |
|
6d7957bd6866
8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents:
29147
diff
changeset
|
619 |
@Override |
6d7957bd6866
8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents:
29147
diff
changeset
|
620 |
public Type visitType(Type t, Void _unused) { |
6d7957bd6866
8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents:
29147
diff
changeset
|
621 |
if (t.hasTag(DEFERRED)) { |
6d7957bd6866
8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents:
29147
diff
changeset
|
622 |
return visit(super.visitType(t, null)); |
6d7957bd6866
8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents:
29147
diff
changeset
|
623 |
} else if (t.hasTag(BOT)) |
14057
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
624 |
// 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
|
625 |
// infer as java.lang.Void for now |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
626 |
t = types.boxedClass(syms.voidType).type; |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
627 |
return t; |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
628 |
} |
b4b0377b8dba
7177387: Add target-typing support in method context
mcimadamore
parents:
14048
diff
changeset
|
629 |
} |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
630 |
|
33906
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
631 |
TypeMapping<Void> fromTypeVarFun = new TypeMapping<Void>() { |
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
632 |
@Override |
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
633 |
public Type visitTypeVar(TypeVar tv, Void aVoid) { |
42416
1cfad0990b99
8170410: inference: javac doesn't implement 18.2.5 correctly
mcimadamore
parents:
41855
diff
changeset
|
634 |
UndetVar uv = new UndetVar(tv, incorporationEngine(), types); |
1cfad0990b99
8170410: inference: javac doesn't implement 18.2.5 correctly
mcimadamore
parents:
41855
diff
changeset
|
635 |
if ((tv.tsym.flags() & Flags.THROWS) != 0) { |
1cfad0990b99
8170410: inference: javac doesn't implement 18.2.5 correctly
mcimadamore
parents:
41855
diff
changeset
|
636 |
uv.setThrow(); |
1cfad0990b99
8170410: inference: javac doesn't implement 18.2.5 correctly
mcimadamore
parents:
41855
diff
changeset
|
637 |
} |
1cfad0990b99
8170410: inference: javac doesn't implement 18.2.5 correctly
mcimadamore
parents:
41855
diff
changeset
|
638 |
return uv; |
33906
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
639 |
} |
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
640 |
}; |
91b6bee06dc1
8046685: Uncompilable large expressions involving generics.
mcimadamore
parents:
33708
diff
changeset
|
641 |
|
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
642 |
/** |
15717 | 643 |
* This method is used to infer a suitable target SAM in case the original |
644 |
* SAM type contains one or more wildcards. An inference process is applied |
|
645 |
* so that wildcard bounds, as well as explicit lambda/method ref parameters |
|
646 |
* (where applicable) are used to constraint the solution. |
|
647 |
*/ |
|
648 |
public Type instantiateFunctionalInterface(DiagnosticPosition pos, Type funcInterface, |
|
649 |
List<Type> paramTypes, Check.CheckContext checkContext) { |
|
650 |
if (types.capture(funcInterface) == funcInterface) { |
|
651 |
//if capture doesn't change the type then return the target unchanged |
|
652 |
//(this means the target contains no wildcards!) |
|
653 |
return funcInterface; |
|
654 |
} else { |
|
655 |
Type formalInterface = funcInterface.tsym.type; |
|
656 |
InferenceContext funcInterfaceContext = |
|
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
657 |
new InferenceContext(this, funcInterface.tsym.type.getTypeArguments()); |
15717 | 658 |
|
659 |
Assert.check(paramTypes != null); |
|
660 |
//get constraints from explicit params (this is done by |
|
661 |
//checking that explicit param types are equal to the ones |
|
662 |
//in the functional interface descriptors) |
|
663 |
List<Type> descParameterTypes = types.findDescriptorType(formalInterface).getParameterTypes(); |
|
664 |
if (descParameterTypes.size() != paramTypes.size()) { |
|
665 |
checkContext.report(pos, diags.fragment("incompatible.arg.types.in.lambda")); |
|
666 |
return types.createErrorType(funcInterface); |
|
667 |
} |
|
668 |
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
|
669 |
if (!types.isSameType(funcInterfaceContext.asUndetVar(p), paramTypes.head)) { |
15717 | 670 |
checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface)); |
671 |
return types.createErrorType(funcInterface); |
|
672 |
} |
|
673 |
paramTypes = paramTypes.tail; |
|
674 |
} |
|
675 |
||
676 |
List<Type> actualTypeargs = funcInterface.getTypeArguments(); |
|
677 |
for (Type t : funcInterfaceContext.undetvars) { |
|
678 |
UndetVar uv = (UndetVar)t; |
|
39589
0cbaed77814c
8048543: JLS8 18.5.3: inference variable seems to be instantiated unexpectedly
vromero
parents:
38832
diff
changeset
|
679 |
Optional<Type> inst = uv.getBounds(InferenceBound.EQ).stream() |
0cbaed77814c
8048543: JLS8 18.5.3: inference variable seems to be instantiated unexpectedly
vromero
parents:
38832
diff
changeset
|
680 |
.filter(b -> !b.containsAny(formalInterface.getTypeArguments())).findFirst(); |
0cbaed77814c
8048543: JLS8 18.5.3: inference variable seems to be instantiated unexpectedly
vromero
parents:
38832
diff
changeset
|
681 |
uv.setInst(inst.orElse(actualTypeargs.head)); |
15717 | 682 |
actualTypeargs = actualTypeargs.tail; |
683 |
} |
|
684 |
||
685 |
Type owntype = funcInterfaceContext.asInstType(formalInterface); |
|
686 |
if (!chk.checkValidGenericType(owntype)) { |
|
687 |
//if the inferred functional interface type is not well-formed, |
|
688 |
//or if it's not a subtype of the original target, issue an error |
|
689 |
checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface)); |
|
690 |
} |
|
25279 | 691 |
//propagate constraints as per JLS 18.2.1 |
692 |
checkContext.compatible(owntype, funcInterface, types.noWarnings); |
|
15717 | 693 |
return owntype; |
694 |
} |
|
695 |
} |
|
696 |
// </editor-fold> |
|
697 |
||
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
698 |
// <editor-fold defaultstate="collapsed" desc="Incorporation"> |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
699 |
|
15717 | 700 |
/** |
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
701 |
* This class is the root of all incorporation actions. |
15717 | 702 |
*/ |
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
703 |
public abstract class IncorporationAction { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
704 |
UndetVar uv; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
705 |
Type t; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
706 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
707 |
IncorporationAction(UndetVar uv, Type t) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
708 |
this.uv = uv; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
709 |
this.t = t; |
15717 | 710 |
} |
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
711 |
|
37003 | 712 |
public abstract IncorporationAction dup(UndetVar that); |
713 |
||
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
714 |
/** |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
715 |
* Incorporation action entry-point. Subclasses should define the logic associated with |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
716 |
* this incorporation action. |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
717 |
*/ |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
718 |
abstract void apply(InferenceContext ic, Warner warn); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
719 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
720 |
/** |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
721 |
* Helper function: perform subtyping through incorporation cache. |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
722 |
*/ |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
723 |
boolean isSubtype(Type s, Type t, Warner warn) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
724 |
return doIncorporationOp(IncorporationBinaryOpKind.IS_SUBTYPE, s, t, warn); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
725 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
726 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
727 |
/** |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
728 |
* Helper function: perform type-equivalence through incorporation cache. |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
729 |
*/ |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
730 |
boolean isSameType(Type s, Type t) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
731 |
return doIncorporationOp(IncorporationBinaryOpKind.IS_SAME_TYPE, s, t, null); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
732 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
733 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
734 |
@Override |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
735 |
public String toString() { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
736 |
return String.format("%s[undet=%s,t=%s]", getClass().getSimpleName(), uv.qtype, t); |
15717 | 737 |
} |
738 |
} |
|
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
739 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
740 |
/** |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
741 |
* Bound-check incorporation action. A newly added bound is checked against existing bounds, |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
742 |
* to verify its compatibility; each bound is checked using either subtyping or type equivalence. |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
743 |
*/ |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
744 |
class CheckBounds extends IncorporationAction { |
15717 | 745 |
|
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
746 |
InferenceBound from; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
747 |
BiFunction<InferenceContext, Type, Type> typeFunc; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
748 |
BiPredicate<InferenceContext, Type> optFilter; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
749 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
750 |
CheckBounds(UndetVar uv, Type t, InferenceBound from) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
751 |
this(uv, t, InferenceContext::asUndetVar, null, from); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
752 |
} |
15717 | 753 |
|
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
754 |
CheckBounds(UndetVar uv, Type t, BiFunction<InferenceContext, Type, Type> typeFunc, |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
755 |
BiPredicate<InferenceContext, Type> typeFilter, InferenceBound from) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
756 |
super(uv, t); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
757 |
this.from = from; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
758 |
this.typeFunc = typeFunc; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
759 |
this.optFilter = typeFilter; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
760 |
} |
15717 | 761 |
|
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
762 |
@Override |
37003 | 763 |
public IncorporationAction dup(UndetVar that) { |
764 |
return new CheckBounds(that, t, typeFunc, optFilter, from); |
|
765 |
} |
|
766 |
||
767 |
@Override |
|
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
768 |
void apply(InferenceContext inferenceContext, Warner warn) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
769 |
t = typeFunc.apply(inferenceContext, t); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
770 |
if (optFilter != null && optFilter.test(inferenceContext, t)) return; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
771 |
for (InferenceBound to : boundsToCheck()) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
772 |
for (Type b : uv.getBounds(to)) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
773 |
b = typeFunc.apply(inferenceContext, b); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
774 |
if (optFilter != null && optFilter.test(inferenceContext, b)) continue; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
775 |
boolean success = checkBound(t, b, from, to, warn); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
776 |
if (!success) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
777 |
report(from, to); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
778 |
} |
15717 | 779 |
} |
780 |
} |
|
22163 | 781 |
} |
15717 | 782 |
|
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
783 |
/** |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
784 |
* The list of bound kinds to be checked. |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
785 |
*/ |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
786 |
EnumSet<InferenceBound> boundsToCheck() { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
787 |
return (from == InferenceBound.EQ) ? |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
788 |
EnumSet.allOf(InferenceBound.class) : |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
789 |
EnumSet.complementOf(EnumSet.of(from)); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
790 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
791 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
792 |
/** |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
793 |
* Is source type 's' compatible with target type 't' given source and target bound kinds? |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
794 |
*/ |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
795 |
boolean checkBound(Type s, Type t, InferenceBound ib_s, InferenceBound ib_t, Warner warn) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
796 |
if (ib_s.lessThan(ib_t)) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
797 |
return isSubtype(s, t, warn); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
798 |
} else if (ib_t.lessThan(ib_s)) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
799 |
return isSubtype(t, s, warn); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
800 |
} else { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
801 |
return isSameType(s, t); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
802 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
803 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
804 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
805 |
/** |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
806 |
* Report a bound check error. |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
807 |
*/ |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
808 |
void report(InferenceBound from, InferenceBound to) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
809 |
//this is a workaround to preserve compatibility with existing messages |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
810 |
if (from == to) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
811 |
reportBoundError(uv, from); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
812 |
} else if (from == InferenceBound.LOWER || to == InferenceBound.EQ) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
813 |
reportBoundError(uv, to, from); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
814 |
} else { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
815 |
reportBoundError(uv, from, to); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
816 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
817 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
818 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
819 |
@Override |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
820 |
public String toString() { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
821 |
return String.format("%s[undet=%s,t=%s,bound=%s]", getClass().getSimpleName(), uv.qtype, t, from); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
822 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
823 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
824 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
825 |
/** |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
826 |
* Custom check executed by the legacy incorporation engine. Newly added bounds are checked |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
827 |
* against existing eq bounds. |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
828 |
*/ |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
829 |
class EqCheckLegacy extends CheckBounds { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
830 |
EqCheckLegacy(UndetVar uv, Type t, InferenceBound from) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
831 |
super(uv, t, InferenceContext::asInstType, InferenceContext::free, from); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
832 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
833 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
834 |
@Override |
37003 | 835 |
public IncorporationAction dup(UndetVar that) { |
836 |
return new EqCheckLegacy(that, t, from); |
|
837 |
} |
|
838 |
||
839 |
@Override |
|
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
840 |
EnumSet<InferenceBound> boundsToCheck() { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
841 |
return (from == InferenceBound.EQ) ? |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
842 |
EnumSet.allOf(InferenceBound.class) : |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
843 |
EnumSet.of(InferenceBound.EQ); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
844 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
845 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
846 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
847 |
/** |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
848 |
* Check that the inferred type conforms to all bounds. |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
849 |
*/ |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
850 |
class CheckInst extends CheckBounds { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
851 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
852 |
EnumSet<InferenceBound> to; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
853 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
854 |
CheckInst(UndetVar uv, InferenceBound ib, InferenceBound... rest) { |
37003 | 855 |
this(uv, EnumSet.of(ib, rest)); |
856 |
} |
|
857 |
||
858 |
CheckInst(UndetVar uv, EnumSet<InferenceBound> to) { |
|
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
859 |
super(uv, uv.getInst(), InferenceBound.EQ); |
37003 | 860 |
this.to = to; |
861 |
} |
|
862 |
||
863 |
@Override |
|
864 |
public IncorporationAction dup(UndetVar that) { |
|
865 |
return new CheckInst(that, to); |
|
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
866 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
867 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
868 |
@Override |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
869 |
EnumSet<InferenceBound> boundsToCheck() { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
870 |
return to; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
871 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
872 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
873 |
@Override |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
874 |
void report(InferenceBound from, InferenceBound to) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
875 |
reportInstError(uv, to); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
876 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
877 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
878 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
879 |
/** |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
880 |
* Replace undetvars in bounds and check that the inferred type conforms to all bounds. |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
881 |
*/ |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
882 |
class SubstBounds extends CheckInst { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
883 |
SubstBounds(UndetVar uv) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
884 |
super(uv, InferenceBound.LOWER, InferenceBound.EQ, InferenceBound.UPPER); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
885 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
886 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
887 |
@Override |
37003 | 888 |
public IncorporationAction dup(UndetVar that) { |
889 |
return new SubstBounds(that); |
|
890 |
} |
|
891 |
||
892 |
@Override |
|
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
893 |
void apply(InferenceContext inferenceContext, Warner warn) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
894 |
for (Type undet : inferenceContext.undetvars) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
895 |
//we could filter out variables not mentioning uv2... |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
896 |
UndetVar uv2 = (UndetVar)undet; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
897 |
uv2.substBounds(List.of(uv.qtype), List.of(uv.getInst()), types); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
898 |
checkCompatibleUpperBounds(uv2, inferenceContext); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
899 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
900 |
super.apply(inferenceContext, warn); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
901 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
902 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
903 |
/** |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
904 |
* Make sure that the upper bounds we got so far lead to a solvable inference |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
905 |
* variable by making sure that a glb exists. |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
906 |
*/ |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
907 |
void checkCompatibleUpperBounds(UndetVar uv, InferenceContext inferenceContext) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
908 |
List<Type> hibounds = |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
909 |
Type.filter(uv.getBounds(InferenceBound.UPPER), new BoundFilter(inferenceContext)); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
910 |
final Type hb; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
911 |
if (hibounds.isEmpty()) |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
912 |
hb = syms.objectType; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
913 |
else if (hibounds.tail.isEmpty()) |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
914 |
hb = hibounds.head; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
915 |
else |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
916 |
hb = types.glb(hibounds); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
917 |
if (hb == null || hb.isErroneous()) |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
918 |
reportBoundError(uv, InferenceBound.UPPER); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
919 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
920 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
921 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
922 |
/** |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
923 |
* Perform pairwise comparison between common generic supertypes of two upper bounds. |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
924 |
*/ |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
925 |
class CheckUpperBounds extends IncorporationAction { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
926 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
927 |
public CheckUpperBounds(UndetVar uv, Type t) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
928 |
super(uv, t); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
929 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
930 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
931 |
@Override |
37003 | 932 |
public IncorporationAction dup(UndetVar that) { |
933 |
return new CheckUpperBounds(that, t); |
|
934 |
} |
|
935 |
||
936 |
@Override |
|
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
937 |
void apply(InferenceContext inferenceContext, Warner warn) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
938 |
List<Type> boundList = uv.getBounds(InferenceBound.UPPER).stream() |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
939 |
.collect(types.closureCollector(true, types::isSameType)); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
940 |
for (Type b2 : boundList) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
941 |
if (t == b2) continue; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
942 |
/* This wildcard check is temporary workaround. This code may need to be |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
943 |
* revisited once spec bug JDK-7034922 is fixed. |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
944 |
*/ |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
945 |
if (t != b2 && !t.hasTag(WILDCARD) && !b2.hasTag(WILDCARD)) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
946 |
for (Pair<Type, Type> commonSupers : getParameterizedSupers(t, b2)) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
947 |
List<Type> allParamsSuperBound1 = commonSupers.fst.allparams(); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
948 |
List<Type> allParamsSuperBound2 = commonSupers.snd.allparams(); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
949 |
while (allParamsSuperBound1.nonEmpty() && allParamsSuperBound2.nonEmpty()) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
950 |
//traverse the list of all params comparing them |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
951 |
if (!allParamsSuperBound1.head.hasTag(WILDCARD) && |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
952 |
!allParamsSuperBound2.head.hasTag(WILDCARD)) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
953 |
if (!isSameType(inferenceContext.asUndetVar(allParamsSuperBound1.head), |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
954 |
inferenceContext.asUndetVar(allParamsSuperBound2.head))) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
955 |
reportBoundError(uv, InferenceBound.UPPER); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
956 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
957 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
958 |
allParamsSuperBound1 = allParamsSuperBound1.tail; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
959 |
allParamsSuperBound2 = allParamsSuperBound2.tail; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
960 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
961 |
Assert.check(allParamsSuperBound1.isEmpty() && allParamsSuperBound2.isEmpty()); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
962 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
963 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
964 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
965 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
966 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
967 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
968 |
/** |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
969 |
* Perform propagation of bounds. Given a constraint of the kind {@code alpha <: T}, three |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
970 |
* kind of propagation occur: |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
971 |
* |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
972 |
* <li>T is copied into all matching bounds (i.e. lower/eq bounds) B of alpha such that B=beta (forward propagation)</li> |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
973 |
* <li>if T=beta, matching bounds (i.e. upper bounds) of beta are copied into alpha (backwards propagation)</li> |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
974 |
* <li>if T=beta, sets a symmetric bound on beta (i.e. beta :> alpha) (symmetric propagation) </li> |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
975 |
*/ |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
976 |
class PropagateBounds extends IncorporationAction { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
977 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
978 |
InferenceBound ib; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
979 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
980 |
public PropagateBounds(UndetVar uv, Type t, InferenceBound ib) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
981 |
super(uv, t); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
982 |
this.ib = ib; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
983 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
984 |
|
37003 | 985 |
@Override |
986 |
public IncorporationAction dup(UndetVar that) { |
|
987 |
return new PropagateBounds(that, t, ib); |
|
988 |
} |
|
989 |
||
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
990 |
void apply(InferenceContext inferenceContext, Warner warner) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
991 |
Type undetT = inferenceContext.asUndetVar(t); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
992 |
if (undetT.hasTag(UNDETVAR) && !((UndetVar)undetT).isCaptured()) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
993 |
UndetVar uv2 = (UndetVar)undetT; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
994 |
//symmetric propagation |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
995 |
uv2.addBound(ib.complement(), uv, types); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
996 |
//backwards propagation |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
997 |
for (InferenceBound ib2 : backwards()) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
998 |
for (Type b : uv2.getBounds(ib2)) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
999 |
uv.addBound(ib2, b, types); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1000 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1001 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1002 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1003 |
//forward propagation |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1004 |
for (InferenceBound ib2 : forward()) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1005 |
for (Type l : uv.getBounds(ib2)) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1006 |
Type undet = inferenceContext.asUndetVar(l); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1007 |
if (undet.hasTag(TypeTag.UNDETVAR) && !((UndetVar)undet).isCaptured()) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1008 |
UndetVar uv2 = (UndetVar)undet; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1009 |
uv2.addBound(ib, inferenceContext.asInstType(t), types); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1010 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1011 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1012 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1013 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1014 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1015 |
EnumSet<InferenceBound> forward() { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1016 |
return (ib == InferenceBound.EQ) ? |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1017 |
EnumSet.of(InferenceBound.EQ) : EnumSet.complementOf(EnumSet.of(ib)); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1018 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1019 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1020 |
EnumSet<InferenceBound> backwards() { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1021 |
return (ib == InferenceBound.EQ) ? |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1022 |
EnumSet.allOf(InferenceBound.class) : EnumSet.of(ib); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1023 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1024 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1025 |
@Override |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1026 |
public String toString() { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1027 |
return String.format("%s[undet=%s,t=%s,bound=%s]", getClass().getSimpleName(), uv.qtype, t, ib); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1028 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1029 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1030 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1031 |
/** |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1032 |
* This class models an incorporation engine. The engine is responsible for listening to |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1033 |
* changes in inference variables and register incorporation actions accordingly. |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1034 |
*/ |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1035 |
abstract class AbstractIncorporationEngine implements UndetVarListener { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1036 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1037 |
@Override |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1038 |
public void varInstantiated(UndetVar uv) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1039 |
uv.incorporationActions.addFirst(new SubstBounds(uv)); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1040 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1041 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1042 |
@Override |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1043 |
public void varBoundChanged(UndetVar uv, InferenceBound ib, Type bound, boolean update) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1044 |
if (uv.isCaptured()) return; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1045 |
uv.incorporationActions.addAll(getIncorporationActions(uv, ib, bound, update)); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1046 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1047 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1048 |
abstract List<IncorporationAction> getIncorporationActions(UndetVar uv, InferenceBound ib, Type t, boolean update); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1049 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1050 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1051 |
/** |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1052 |
* A legacy incorporation engine. Used for source <= 7. |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1053 |
*/ |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1054 |
AbstractIncorporationEngine legacyEngine = new AbstractIncorporationEngine() { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1055 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1056 |
List<IncorporationAction> getIncorporationActions(UndetVar uv, InferenceBound ib, Type t, boolean update) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1057 |
ListBuffer<IncorporationAction> actions = new ListBuffer<>(); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1058 |
Type inst = uv.getInst(); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1059 |
if (inst != null) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1060 |
actions.add(new CheckInst(uv, ib)); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1061 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1062 |
actions.add(new EqCheckLegacy(uv, t, ib)); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1063 |
return actions.toList(); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1064 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1065 |
}; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1066 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1067 |
/** |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1068 |
* The standard incorporation engine. Used for source >= 8. |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1069 |
*/ |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1070 |
AbstractIncorporationEngine graphEngine = new AbstractIncorporationEngine() { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1071 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1072 |
@Override |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1073 |
List<IncorporationAction> getIncorporationActions(UndetVar uv, InferenceBound ib, Type t, boolean update) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1074 |
ListBuffer<IncorporationAction> actions = new ListBuffer<>(); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1075 |
Type inst = uv.getInst(); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1076 |
if (inst != null) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1077 |
actions.add(new CheckInst(uv, ib)); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1078 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1079 |
actions.add(new CheckBounds(uv, t, ib)); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1080 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1081 |
if (update) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1082 |
return actions.toList(); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1083 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1084 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1085 |
if (ib == InferenceBound.UPPER) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1086 |
actions.add(new CheckUpperBounds(uv, t)); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1087 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1088 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1089 |
actions.add(new PropagateBounds(uv, t, ib)); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1090 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1091 |
return actions.toList(); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1092 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1093 |
}; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1094 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1095 |
/** |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1096 |
* Get the incorporation engine to be used in this compilation. |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1097 |
*/ |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1098 |
AbstractIncorporationEngine incorporationEngine() { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1099 |
return allowGraphInference ? graphEngine : legacyEngine; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1100 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1101 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1102 |
/** max number of incorporation rounds. */ |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1103 |
static final int MAX_INCORPORATION_STEPS = 10000; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1104 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1105 |
/** |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1106 |
* Check bounds and perform incorporation. |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1107 |
*/ |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1108 |
void doIncorporation(InferenceContext inferenceContext, Warner warn) throws InferenceException { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1109 |
try { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1110 |
boolean progress = true; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1111 |
int round = 0; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1112 |
while (progress && round < MAX_INCORPORATION_STEPS) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1113 |
progress = false; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1114 |
for (Type t : inferenceContext.undetvars) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1115 |
UndetVar uv = (UndetVar)t; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1116 |
if (!uv.incorporationActions.isEmpty()) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1117 |
progress = true; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1118 |
uv.incorporationActions.removeFirst().apply(inferenceContext, warn); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1119 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1120 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1121 |
round++; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1122 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1123 |
} finally { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1124 |
incorporationCache.clear(); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1125 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1126 |
} |
15717 | 1127 |
|
24795
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
1128 |
/* 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
|
1129 |
* 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
|
1130 |
* 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
|
1131 |
* 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
|
1132 |
* 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
|
1133 |
* |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
1134 |
* As an example for the following input: |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
1135 |
* |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
1136 |
* t = java.util.ArrayList<java.lang.String> |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
1137 |
* s = java.util.List<T> |
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
1138 |
* |
24795
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
1139 |
* we get this ouput (singleton list): |
24063
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
1140 |
* |
24795
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
1141 |
* [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
|
1142 |
*/ |
24795
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
1143 |
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
|
1144 |
Type lubResult = types.lub(t, s); |
24795
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
1145 |
if (lubResult == syms.errType || lubResult == syms.botType) { |
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
1146 |
return List.nil(); |
24063
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
1147 |
} |
29147
4cba0458106b
8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents:
27391
diff
changeset
|
1148 |
List<Type> supertypesToCheck = lubResult.isIntersection() ? |
24795
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
1149 |
((IntersectionClassType)lubResult).getComponents() : |
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
1150 |
List.of(lubResult); |
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
1151 |
ListBuffer<Pair<Type, Type>> commonSupertypes = new ListBuffer<>(); |
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
1152 |
for (Type sup : supertypesToCheck) { |
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
1153 |
if (sup.isParameterized()) { |
27391
17f7b6c40c6d
8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents:
27228
diff
changeset
|
1154 |
Type asSuperOfT = asSuper(t, sup); |
17f7b6c40c6d
8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents:
27228
diff
changeset
|
1155 |
Type asSuperOfS = asSuper(s, sup); |
24795
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
1156 |
commonSupertypes.add(new Pair<>(asSuperOfT, asSuperOfS)); |
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
1157 |
} |
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
1158 |
} |
ff348aab3b54
8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents:
24396
diff
changeset
|
1159 |
return commonSupertypes.toList(); |
24063
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
1160 |
} |
27391
17f7b6c40c6d
8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents:
27228
diff
changeset
|
1161 |
//where |
17f7b6c40c6d
8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents:
27228
diff
changeset
|
1162 |
private Type asSuper(Type t, Type sup) { |
17f7b6c40c6d
8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents:
27228
diff
changeset
|
1163 |
return (sup.hasTag(ARRAY)) ? |
17f7b6c40c6d
8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents:
27228
diff
changeset
|
1164 |
new ArrayType(asSuper(types.elemtype(t), types.elemtype(sup)), syms.arrayClass) : |
17f7b6c40c6d
8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents:
27228
diff
changeset
|
1165 |
types.asSuper(t, sup.tsym); |
17f7b6c40c6d
8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents:
27228
diff
changeset
|
1166 |
} |
24063
3e3c18700277
8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents:
23395
diff
changeset
|
1167 |
|
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1168 |
boolean doIncorporationOp(IncorporationBinaryOpKind opKind, Type op1, Type op2, Warner warn) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1169 |
IncorporationBinaryOp newOp = new IncorporationBinaryOp(opKind, op1, op2); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1170 |
Boolean res = incorporationCache.get(newOp); |
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1171 |
if (res == null) { |
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1172 |
incorporationCache.put(newOp, res = newOp.apply(warn)); |
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1173 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1174 |
return res; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1175 |
} |
15717 | 1176 |
|
1177 |
/** |
|
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1178 |
* 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
|
1179 |
* (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
|
1180 |
* upper/lower/eq bound). |
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 |
enum IncorporationBinaryOpKind { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1183 |
IS_SUBTYPE() { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1184 |
@Override |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1185 |
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
|
1186 |
return types.isSubtypeUnchecked(op1, op2, warn); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1187 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1188 |
}, |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1189 |
IS_SAME_TYPE() { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1190 |
@Override |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1191 |
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
|
1192 |
return types.isSameType(op1, op2); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1193 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1194 |
}; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1195 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1196 |
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
|
1197 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1198 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1199 |
/** |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1200 |
* This class encapsulates a basic incorporation operation; incorporation |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1201 |
* 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
|
1202 |
* 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
|
1203 |
* 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
|
1204 |
* same bounds over and over). |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1205 |
*/ |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1206 |
class IncorporationBinaryOp { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1207 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1208 |
IncorporationBinaryOpKind opKind; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1209 |
Type op1; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1210 |
Type op2; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1211 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1212 |
IncorporationBinaryOp(IncorporationBinaryOpKind opKind, Type op1, Type op2) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1213 |
this.opKind = opKind; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1214 |
this.op1 = op1; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1215 |
this.op2 = op2; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1216 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1217 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1218 |
@Override |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1219 |
public boolean equals(Object o) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1220 |
if (!(o instanceof IncorporationBinaryOp)) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1221 |
return false; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1222 |
} else { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1223 |
IncorporationBinaryOp that = (IncorporationBinaryOp)o; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1224 |
return opKind == that.opKind && |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1225 |
types.isSameType(op1, that.op1, true) && |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1226 |
types.isSameType(op2, that.op2, true); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1227 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1228 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1229 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1230 |
@Override |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1231 |
public int hashCode() { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1232 |
int result = opKind.hashCode(); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1233 |
result *= 127; |
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1234 |
result += types.hashCode(op1); |
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1235 |
result *= 127; |
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1236 |
result += types.hashCode(op2); |
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1237 |
return result; |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1238 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1239 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1240 |
boolean apply(Warner warn) { |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1241 |
return opKind.apply(op1, op2, warn, types); |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1242 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1243 |
} |
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1244 |
|
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1245 |
/** an incorporation cache keeps track of all executed incorporation-related operations */ |
22163 | 1246 |
Map<IncorporationBinaryOp, Boolean> incorporationCache = new HashMap<>(); |
18918
4e8769f15a95
8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents:
18916
diff
changeset
|
1247 |
|
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1248 |
protected static class BoundFilter implements Filter<Type> { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1249 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1250 |
InferenceContext inferenceContext; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1251 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1252 |
public BoundFilter(InferenceContext inferenceContext) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1253 |
this.inferenceContext = inferenceContext; |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1254 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1255 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1256 |
@Override |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1257 |
public boolean accepts(Type t) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1258 |
return !t.isErroneous() && !inferenceContext.free(t) && |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1259 |
!t.hasTag(BOT); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1260 |
} |
15717 | 1261 |
} |
1262 |
||
1263 |
/** |
|
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1264 |
* Incorporation error: mismatch between inferred type and given bound. |
15717 | 1265 |
*/ |
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1266 |
void reportInstError(UndetVar uv, InferenceBound ib) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1267 |
reportInferenceError( |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1268 |
String.format("inferred.do.not.conform.to.%s.bounds", StringUtils.toLowerCase(ib.name())), |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1269 |
uv.getInst(), |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1270 |
uv.getBounds(ib)); |
15717 | 1271 |
} |
1272 |
||
1273 |
/** |
|
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1274 |
* Incorporation error: mismatch between two (or more) bounds of same kind. |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1275 |
*/ |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1276 |
void reportBoundError(UndetVar uv, InferenceBound ib) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1277 |
reportInferenceError( |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1278 |
String.format("incompatible.%s.bounds", StringUtils.toLowerCase(ib.name())), |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1279 |
uv.qtype, |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1280 |
uv.getBounds(ib)); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1281 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1282 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1283 |
/** |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1284 |
* Incorporation error: mismatch between two (or more) bounds of different kinds. |
15717 | 1285 |
*/ |
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1286 |
void reportBoundError(UndetVar uv, InferenceBound ib1, InferenceBound ib2) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1287 |
reportInferenceError( |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1288 |
String.format("incompatible.%s.%s.bounds", |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1289 |
StringUtils.toLowerCase(ib1.name()), |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1290 |
StringUtils.toLowerCase(ib2.name())), |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1291 |
uv.qtype, |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1292 |
uv.getBounds(ib1), |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1293 |
uv.getBounds(ib2)); |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1294 |
} |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1295 |
|
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1296 |
/** |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1297 |
* Helper method: reports an inference error. |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1298 |
*/ |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1299 |
void reportInferenceError(String key, Object... args) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1300 |
throw inferenceException.setMessage(key, args); |
15717 | 1301 |
} |
1302 |
// </editor-fold> |
|
1303 |
||
1304 |
// <editor-fold defaultstate="collapsed" desc="Inference engine"> |
|
1305 |
/** |
|
1306 |
* Graph inference strategy - act as an input to the inference solver; a strategy is |
|
1307 |
* composed of two ingredients: (i) find a node to solve in the inference graph, |
|
1308 |
* and (ii) tell th engine when we are done fixing inference variables |
|
1309 |
*/ |
|
1310 |
interface GraphStrategy { |
|
19914 | 1311 |
|
1312 |
/** |
|
1313 |
* A NodeNotFoundException is thrown whenever an inference strategy fails |
|
1314 |
* to pick the next node to solve in the inference graph. |
|
1315 |
*/ |
|
1316 |
public static class NodeNotFoundException extends RuntimeException { |
|
1317 |
private static final long serialVersionUID = 0; |
|
1318 |
||
1319 |
InferenceGraph graph; |
|
1320 |
||
1321 |
public NodeNotFoundException(InferenceGraph graph) { |
|
1322 |
this.graph = graph; |
|
1323 |
} |
|
1324 |
} |
|
15717 | 1325 |
/** |
1326 |
* Pick the next node (leaf) to solve in the graph |
|
1327 |
*/ |
|
19914 | 1328 |
Node pickNode(InferenceGraph g) throws NodeNotFoundException; |
15717 | 1329 |
/** |
1330 |
* Is this the last step? |
|
1331 |
*/ |
|
1332 |
boolean done(); |
|
1333 |
} |
|
1334 |
||
1335 |
/** |
|
1336 |
* Simple solver strategy class that locates all leaves inside a graph |
|
1337 |
* and picks the first leaf as the next node to solve |
|
1338 |
*/ |
|
1339 |
abstract class LeafSolver implements GraphStrategy { |
|
1340 |
public Node pickNode(InferenceGraph g) { |
|
19914 | 1341 |
if (g.nodes.isEmpty()) { |
1342 |
//should not happen |
|
1343 |
throw new NodeNotFoundException(g); |
|
22163 | 1344 |
} |
15717 | 1345 |
return g.nodes.get(0); |
1346 |
} |
|
1347 |
} |
|
1348 |
||
1349 |
/** |
|
1350 |
* This solver uses an heuristic to pick the best leaf - the heuristic |
|
1351 |
* tries to select the node that has maximal probability to contain one |
|
1352 |
* or more inference variables in a given list |
|
1353 |
*/ |
|
1354 |
abstract class BestLeafSolver extends LeafSolver { |
|
1355 |
||
19914 | 1356 |
/** list of ivars of which at least one must be solved */ |
15717 | 1357 |
List<Type> varsToSolve; |
1358 |
||
1359 |
BestLeafSolver(List<Type> varsToSolve) { |
|
1360 |
this.varsToSolve = varsToSolve; |
|
1361 |
} |
|
1362 |
||
1363 |
/** |
|
19914 | 1364 |
* Computes a path that goes from a given node to the leafs in the graph. |
1365 |
* Typically this will start from a node containing a variable in |
|
1366 |
* {@code varsToSolve}. For any given path, the cost is computed as the total |
|
1367 |
* number of type-variables that should be eagerly instantiated across that path. |
|
15717 | 1368 |
*/ |
19914 | 1369 |
Pair<List<Node>, Integer> computeTreeToLeafs(Node n) { |
1370 |
Pair<List<Node>, Integer> cachedPath = treeCache.get(n); |
|
1371 |
if (cachedPath == null) { |
|
1372 |
//cache miss |
|
1373 |
if (n.isLeaf()) { |
|
1374 |
//if leaf, stop |
|
22163 | 1375 |
cachedPath = new Pair<>(List.of(n), n.data.length()); |
19914 | 1376 |
} else { |
1377 |
//if non-leaf, proceed recursively |
|
22163 | 1378 |
Pair<List<Node>, Integer> path = new Pair<>(List.of(n), n.data.length()); |
19914 | 1379 |
for (Node n2 : n.getAllDependencies()) { |
1380 |
if (n2 == n) continue; |
|
1381 |
Pair<List<Node>, Integer> subpath = computeTreeToLeafs(n2); |
|
22163 | 1382 |
path = new Pair<>(path.fst.prependList(subpath.fst), |
1383 |
path.snd + subpath.snd); |
|
19914 | 1384 |
} |
1385 |
cachedPath = path; |
|
1386 |
} |
|
1387 |
//save results in cache |
|
1388 |
treeCache.put(n, cachedPath); |
|
1389 |
} |
|
1390 |
return cachedPath; |
|
18916
d93bea397df9
8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents:
18911
diff
changeset
|
1391 |
} |
d93bea397df9
8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents:
18911
diff
changeset
|
1392 |
|
19914 | 1393 |
/** cache used to avoid redundant computation of tree costs */ |
22163 | 1394 |
final Map<Node, Pair<List<Node>, Integer>> treeCache = new HashMap<>(); |
19914 | 1395 |
|
1396 |
/** constant value used to mark non-existent paths */ |
|
22163 | 1397 |
final Pair<List<Node>, Integer> noPath = new Pair<>(null, Integer.MAX_VALUE); |
15717 | 1398 |
|
1399 |
/** |
|
1400 |
* Pick the leaf that minimize cost |
|
1401 |
*/ |
|
1402 |
@Override |
|
1403 |
public Node pickNode(final InferenceGraph g) { |
|
19914 | 1404 |
treeCache.clear(); //graph changes at every step - cache must be cleared |
1405 |
Pair<List<Node>, Integer> bestPath = noPath; |
|
15717 | 1406 |
for (Node n : g.nodes) { |
19914 | 1407 |
if (!Collections.disjoint(n.data, varsToSolve)) { |
1408 |
Pair<List<Node>, Integer> path = computeTreeToLeafs(n); |
|
1409 |
//discard all paths containing at least a node in the |
|
1410 |
//closure computed above |
|
1411 |
if (path.snd < bestPath.snd) { |
|
1412 |
bestPath = path; |
|
1413 |
} |
|
15717 | 1414 |
} |
1415 |
} |
|
19914 | 1416 |
if (bestPath == noPath) { |
1417 |
//no path leads there |
|
1418 |
throw new NodeNotFoundException(g); |
|
1419 |
} |
|
1420 |
return bestPath.fst.head; |
|
15717 | 1421 |
} |
1422 |
} |
|
1423 |
||
1424 |
/** |
|
1425 |
* The inference process can be thought of as a sequence of steps. Each step |
|
1426 |
* instantiates an inference variable using a subset of the inference variable |
|
1427 |
* bounds, if certain condition are met. Decisions such as the sequence in which |
|
1428 |
* steps are applied, or which steps are to be applied are left to the inference engine. |
|
1429 |
*/ |
|
1430 |
enum InferenceStep { |
|
1431 |
||
1432 |
/** |
|
1433 |
* Instantiate an inference variables using one of its (ground) equality |
|
1434 |
* constraints |
|
1435 |
*/ |
|
1436 |
EQ(InferenceBound.EQ) { |
|
1437 |
@Override |
|
1438 |
Type solve(UndetVar uv, InferenceContext inferenceContext) { |
|
1439 |
return filterBounds(uv, inferenceContext).head; |
|
1440 |
} |
|
1441 |
}, |
|
1442 |
/** |
|
1443 |
* Instantiate an inference variables using its (ground) lower bounds. Such |
|
1444 |
* bounds are merged together using lub(). |
|
1445 |
*/ |
|
1446 |
LOWER(InferenceBound.LOWER) { |
|
1447 |
@Override |
|
1448 |
Type solve(UndetVar uv, InferenceContext inferenceContext) { |
|
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
1449 |
Infer infer = inferenceContext.infer; |
15717 | 1450 |
List<Type> lobounds = filterBounds(uv, inferenceContext); |
18395
d56a5fbf0b32
8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents:
18008
diff
changeset
|
1451 |
//note: lobounds should have at least one element |
d56a5fbf0b32
8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents:
18008
diff
changeset
|
1452 |
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
|
1453 |
if (owntype.isPrimitive() || owntype.hasTag(ERROR)) { |
15717 | 1454 |
throw infer.inferenceException |
1455 |
.setMessage("no.unique.minimal.instance.exists", |
|
1456 |
uv.qtype, lobounds); |
|
1457 |
} else { |
|
1458 |
return owntype; |
|
1459 |
} |
|
1460 |
} |
|
1461 |
}, |
|
1462 |
/** |
|
18909
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1463 |
* Infer uninstantiated/unbound inference variables occurring in 'throws' |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1464 |
* clause as RuntimeException |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1465 |
*/ |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1466 |
THROWS(InferenceBound.UPPER) { |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1467 |
@Override |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1468 |
public boolean accepts(UndetVar t, InferenceContext inferenceContext) { |
42416
1cfad0990b99
8170410: inference: javac doesn't implement 18.2.5 correctly
mcimadamore
parents:
41855
diff
changeset
|
1469 |
if (!t.isThrows()) { |
18909
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1470 |
//not a throws undet var |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1471 |
return false; |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1472 |
} |
40594
8cbf5df9c503
8164399: inference of thrown variable does not work correctly
mcimadamore
parents:
39599
diff
changeset
|
1473 |
Types types = inferenceContext.types; |
8cbf5df9c503
8164399: inference of thrown variable does not work correctly
mcimadamore
parents:
39599
diff
changeset
|
1474 |
Symtab syms = inferenceContext.infer.syms; |
8cbf5df9c503
8164399: inference of thrown variable does not work correctly
mcimadamore
parents:
39599
diff
changeset
|
1475 |
return t.getBounds(InferenceBound.UPPER).stream() |
8cbf5df9c503
8164399: inference of thrown variable does not work correctly
mcimadamore
parents:
39599
diff
changeset
|
1476 |
.filter(b -> !inferenceContext.free(b)) |
8cbf5df9c503
8164399: inference of thrown variable does not work correctly
mcimadamore
parents:
39599
diff
changeset
|
1477 |
.allMatch(u -> types.isSubtype(syms.runtimeExceptionType, u)); |
18909
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1478 |
} |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1479 |
|
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1480 |
@Override |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1481 |
Type solve(UndetVar uv, InferenceContext inferenceContext) { |
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
1482 |
return inferenceContext.infer.syms.runtimeExceptionType; |
18909
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1483 |
} |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1484 |
}, |
8f9fc5d876e4
8012242: Lambda compatibility and checked exceptions
mcimadamore
parents:
18904
diff
changeset
|
1485 |
/** |
15717 | 1486 |
* Instantiate an inference variables using its (ground) upper bounds. Such |
1487 |
* bounds are merged together using glb(). |
|
1488 |
*/ |
|
1489 |
UPPER(InferenceBound.UPPER) { |
|
1490 |
@Override |
|
1491 |
Type solve(UndetVar uv, InferenceContext inferenceContext) { |
|
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
1492 |
Infer infer = inferenceContext.infer; |
15717 | 1493 |
List<Type> hibounds = filterBounds(uv, inferenceContext); |
24293
0d889f759fac
8033718: Inference ignores capture variable as upper bound
dlsmith
parents:
24226
diff
changeset
|
1494 |
//note: hibounds should have at least one element |
18395
d56a5fbf0b32
8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents:
18008
diff
changeset
|
1495 |
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
|
1496 |
if (owntype.isPrimitive() || owntype.hasTag(ERROR)) { |
15717 | 1497 |
throw infer.inferenceException |
1498 |
.setMessage("no.unique.maximal.instance.exists", |
|
1499 |
uv.qtype, hibounds); |
|
1500 |
} else { |
|
1501 |
return owntype; |
|
1502 |
} |
|
1503 |
} |
|
1504 |
}, |
|
1505 |
/** |
|
1506 |
* Like the former; the only difference is that this step can only be applied |
|
1507 |
* if all upper bounds are ground. |
|
1508 |
*/ |
|
1509 |
UPPER_LEGACY(InferenceBound.UPPER) { |
|
1510 |
@Override |
|
1511 |
public boolean accepts(UndetVar t, InferenceContext inferenceContext) { |
|
18911 | 1512 |
return !inferenceContext.free(t.getBounds(ib)) && !t.isCaptured(); |
15717 | 1513 |
} |
1514 |
||
1515 |
@Override |
|
1516 |
Type solve(UndetVar uv, InferenceContext inferenceContext) { |
|
1517 |
return UPPER.solve(uv, inferenceContext); |
|
1518 |
} |
|
18911 | 1519 |
}, |
1520 |
/** |
|
1521 |
* Like the former; the only difference is that this step can only be applied |
|
1522 |
* if all upper/lower bounds are ground. |
|
1523 |
*/ |
|
1524 |
CAPTURED(InferenceBound.UPPER) { |
|
1525 |
@Override |
|
1526 |
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
|
1527 |
return t.isCaptured() && |
8a0cbd5cb055
8020804: javac crashes when speculative attribution infers intersection type with array component
mcimadamore
parents:
18918
diff
changeset
|
1528 |
!inferenceContext.free(t.getBounds(InferenceBound.UPPER, InferenceBound.LOWER)); |
18911 | 1529 |
} |
1530 |
||
1531 |
@Override |
|
1532 |
Type solve(UndetVar uv, InferenceContext inferenceContext) { |
|
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
1533 |
Infer infer = inferenceContext.infer; |
18911 | 1534 |
Type upper = UPPER.filterBounds(uv, inferenceContext).nonEmpty() ? |
1535 |
UPPER.solve(uv, inferenceContext) : |
|
1536 |
infer.syms.objectType; |
|
1537 |
Type lower = LOWER.filterBounds(uv, inferenceContext).nonEmpty() ? |
|
1538 |
LOWER.solve(uv, inferenceContext) : |
|
1539 |
infer.syms.botType; |
|
1540 |
CapturedType prevCaptured = (CapturedType)uv.qtype; |
|
24396 | 1541 |
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
|
1542 |
upper, lower, prevCaptured.wildcard); |
18911 | 1543 |
} |
15717 | 1544 |
}; |
1545 |
||
1546 |
final InferenceBound ib; |
|
1547 |
||
1548 |
InferenceStep(InferenceBound ib) { |
|
1549 |
this.ib = ib; |
|
1550 |
} |
|
1551 |
||
1552 |
/** |
|
1553 |
* Find an instantiated type for a given inference variable within |
|
1554 |
* a given inference context |
|
1555 |
*/ |
|
1556 |
abstract Type solve(UndetVar uv, InferenceContext inferenceContext); |
|
1557 |
||
1558 |
/** |
|
1559 |
* Can the inference variable be instantiated using this step? |
|
1560 |
*/ |
|
1561 |
public boolean accepts(UndetVar t, InferenceContext inferenceContext) { |
|
18911 | 1562 |
return filterBounds(t, inferenceContext).nonEmpty() && !t.isCaptured(); |
15717 | 1563 |
} |
1564 |
||
1565 |
/** |
|
1566 |
* Return the subset of ground bounds in a given bound set (i.e. eq/lower/upper) |
|
1567 |
*/ |
|
1568 |
List<Type> filterBounds(UndetVar uv, InferenceContext inferenceContext) { |
|
1569 |
return Type.filter(uv.getBounds(ib), new BoundFilter(inferenceContext)); |
|
1570 |
} |
|
1571 |
} |
|
1572 |
||
1573 |
/** |
|
1574 |
* This enumeration defines the sequence of steps to be applied when the |
|
1575 |
* solver works in legacy mode. The steps in this enumeration reflect |
|
1576 |
* the behavior of old inference routine (see JLS SE 7 15.12.2.7/15.12.2.8). |
|
1577 |
*/ |
|
1578 |
enum LegacyInferenceSteps { |
|
1579 |
||
1580 |
EQ_LOWER(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER)), |
|
1581 |
EQ_UPPER(EnumSet.of(InferenceStep.EQ, InferenceStep.UPPER_LEGACY)); |
|
1582 |
||
1583 |
final EnumSet<InferenceStep> steps; |
|
1584 |
||
1585 |
LegacyInferenceSteps(EnumSet<InferenceStep> steps) { |
|
1586 |
this.steps = steps; |
|
1587 |
} |
|
1588 |
} |
|
1589 |
||
1590 |
/** |
|
1591 |
* This enumeration defines the sequence of steps to be applied when the |
|
1592 |
* graph solver is used. This order is defined so as to maximize compatibility |
|
1593 |
* w.r.t. old inference routine (see JLS SE 7 15.12.2.7/15.12.2.8). |
|
1594 |
*/ |
|
1595 |
enum GraphInferenceSteps { |
|
1596 |
||
1597 |
EQ(EnumSet.of(InferenceStep.EQ)), |
|
1598 |
EQ_LOWER(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER)), |
|
18911 | 1599 |
EQ_LOWER_THROWS_UPPER_CAPTURED(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER, InferenceStep.UPPER, InferenceStep.THROWS, InferenceStep.CAPTURED)); |
15717 | 1600 |
|
1601 |
final EnumSet<InferenceStep> steps; |
|
1602 |
||
1603 |
GraphInferenceSteps(EnumSet<InferenceStep> steps) { |
|
1604 |
this.steps = steps; |
|
1605 |
} |
|
1606 |
} |
|
1607 |
||
1608 |
/** |
|
19914 | 1609 |
* There are two kinds of dependencies between inference variables. The basic |
1610 |
* kind of dependency (or bound dependency) arises when a variable mention |
|
1611 |
* another variable in one of its bounds. There's also a more subtle kind |
|
1612 |
* of dependency that arises when a variable 'might' lead to better constraints |
|
1613 |
* on another variable (this is typically the case with variables holding up |
|
1614 |
* stuck expressions). |
|
1615 |
*/ |
|
1616 |
enum DependencyKind implements GraphUtils.DependencyKind { |
|
1617 |
||
1618 |
/** bound dependency */ |
|
1619 |
BOUND("dotted"), |
|
1620 |
/** stuck dependency */ |
|
1621 |
STUCK("dashed"); |
|
1622 |
||
1623 |
final String dotSyle; |
|
1624 |
||
1625 |
private DependencyKind(String dotSyle) { |
|
1626 |
this.dotSyle = dotSyle; |
|
1627 |
} |
|
1628 |
} |
|
1629 |
||
1630 |
/** |
|
15717 | 1631 |
* This is the graph inference solver - the solver organizes all inference variables in |
1632 |
* a given inference context by bound dependencies - in the general case, such dependencies |
|
1633 |
* would lead to a cyclic directed graph (hence the name); the dependency info is used to build |
|
1634 |
* an acyclic graph, where all cyclic variables are bundled together. An inference |
|
1635 |
* step corresponds to solving a node in the acyclic graph - this is done by |
|
1636 |
* relying on a given strategy (see GraphStrategy). |
|
1637 |
*/ |
|
1638 |
class GraphSolver { |
|
1639 |
||
1640 |
InferenceContext inferenceContext; |
|
1641 |
Warner warn; |
|
1642 |
||
38516
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1643 |
GraphSolver(InferenceContext inferenceContext, Warner warn) { |
15717 | 1644 |
this.inferenceContext = inferenceContext; |
1645 |
this.warn = warn; |
|
1646 |
} |
|
1647 |
||
1648 |
/** |
|
1649 |
* Solve variables in a given inference context. The amount of variables |
|
1650 |
* to be solved, and the way in which the underlying acyclic graph is explored |
|
1651 |
* depends on the selected solver strategy. |
|
1652 |
*/ |
|
1653 |
void solve(GraphStrategy sstrategy) { |
|
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1654 |
doIncorporation(inferenceContext, warn); //initial propagation of bounds |
38516
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1655 |
InferenceGraph inferenceGraph = new InferenceGraph(); |
15717 | 1656 |
while (!sstrategy.done()) { |
26267
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
1657 |
if (dependenciesFolder != null) { |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
1658 |
//add this graph to the pending queue |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
1659 |
pendingGraphs = pendingGraphs.prepend(inferenceGraph.toDot()); |
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
1660 |
} |
15717 | 1661 |
InferenceGraph.Node nodeToSolve = sstrategy.pickNode(inferenceGraph); |
1662 |
List<Type> varsToSolve = List.from(nodeToSolve.data); |
|
18904
3c4c6457ec5b
8019824: very long error messages on inference error
mcimadamore
parents:
18646
diff
changeset
|
1663 |
List<Type> saved_undet = inferenceContext.save(); |
15717 | 1664 |
try { |
1665 |
//repeat until all variables are solved |
|
1666 |
outer: while (Type.containsAny(inferenceContext.restvars(), varsToSolve)) { |
|
1667 |
//for each inference phase |
|
1668 |
for (GraphInferenceSteps step : GraphInferenceSteps.values()) { |
|
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1669 |
if (inferenceContext.solveBasic(varsToSolve, step.steps).nonEmpty()) { |
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1670 |
doIncorporation(inferenceContext, warn); |
15717 | 1671 |
continue outer; |
1672 |
} |
|
1673 |
} |
|
1674 |
//no progress |
|
18395
d56a5fbf0b32
8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents:
18008
diff
changeset
|
1675 |
throw inferenceException.setMessage(); |
15717 | 1676 |
} |
1677 |
} |
|
1678 |
catch (InferenceException ex) { |
|
18395
d56a5fbf0b32
8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents:
18008
diff
changeset
|
1679 |
//did we fail because of interdependent ivars? |
18904
3c4c6457ec5b
8019824: very long error messages on inference error
mcimadamore
parents:
18646
diff
changeset
|
1680 |
inferenceContext.rollback(saved_undet); |
15717 | 1681 |
instantiateAsUninferredVars(varsToSolve, inferenceContext); |
33917
45d04023e689
8067767: type inference performance regression
mcimadamore
parents:
33906
diff
changeset
|
1682 |
doIncorporation(inferenceContext, warn); |
15717 | 1683 |
} |
1684 |
inferenceGraph.deleteNode(nodeToSolve); |
|
1685 |
} |
|
1686 |
} |
|
1687 |
||
1688 |
/** |
|
1689 |
* The dependencies between the inference variables that need to be solved |
|
1690 |
* form a (possibly cyclic) graph. This class reduces the original dependency graph |
|
1691 |
* to an acyclic version, where cyclic nodes are folded into a single 'super node'. |
|
1692 |
*/ |
|
1693 |
class InferenceGraph { |
|
1694 |
||
1695 |
/** |
|
1696 |
* This class represents a node in the graph. Each node corresponds |
|
1697 |
* to an inference variable and has edges (dependencies) on other |
|
1698 |
* nodes. The node defines an entry point that can be used to receive |
|
1699 |
* updates on the structure of the graph this node belongs to (used to |
|
1700 |
* keep dependencies in sync). |
|
1701 |
*/ |
|
25844
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
1702 |
class Node extends GraphUtils.TarjanNode<ListBuffer<Type>, Node> implements DottableNode<ListBuffer<Type>, Node> { |
15717 | 1703 |
|
38516
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1704 |
/** node dependencies */ |
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1705 |
Set<Node> deps; |
15717 | 1706 |
|
1707 |
Node(Type ivar) { |
|
1708 |
super(ListBuffer.of(ivar)); |
|
38516
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1709 |
this.deps = new HashSet<>(); |
19914 | 1710 |
} |
1711 |
||
1712 |
@Override |
|
1713 |
public GraphUtils.DependencyKind[] getSupportedDependencyKinds() { |
|
38832 | 1714 |
return new GraphUtils.DependencyKind[] { DependencyKind.BOUND }; |
15717 | 1715 |
} |
1716 |
||
19914 | 1717 |
public Iterable<? extends Node> getAllDependencies() { |
38516
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1718 |
return deps; |
15717 | 1719 |
} |
1720 |
||
1721 |
@Override |
|
25844
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
1722 |
public Collection<? extends Node> getDependenciesByKind(GraphUtils.DependencyKind dk) { |
38516
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1723 |
if (dk == DependencyKind.BOUND) { |
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1724 |
return deps; |
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1725 |
} else { |
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1726 |
throw new IllegalStateException(); |
15717 | 1727 |
} |
19914 | 1728 |
} |
1729 |
||
1730 |
/** |
|
1731 |
* Adds dependency with given kind. |
|
1732 |
*/ |
|
38516
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1733 |
protected void addDependency(Node depToAdd) { |
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1734 |
deps.add(depToAdd); |
19914 | 1735 |
} |
1736 |
||
1737 |
/** |
|
1738 |
* Add multiple dependencies of same given kind. |
|
1739 |
*/ |
|
38516
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1740 |
protected void addDependencies(Set<Node> depsToAdd) { |
19914 | 1741 |
for (Node n : depsToAdd) { |
38516
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1742 |
addDependency(n); |
19914 | 1743 |
} |
1744 |
} |
|
1745 |
||
1746 |
/** |
|
1747 |
* Remove a dependency, regardless of its kind. |
|
1748 |
*/ |
|
38516
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1749 |
protected boolean removeDependency(Node n) { |
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1750 |
return deps.remove(n); |
15717 | 1751 |
} |
1752 |
||
19914 | 1753 |
/** |
1754 |
* Compute closure of a give node, by recursively walking |
|
1755 |
* through all its dependencies (of given kinds) |
|
1756 |
*/ |
|
38516
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1757 |
protected Set<Node> closure() { |
19914 | 1758 |
boolean progress = true; |
22163 | 1759 |
Set<Node> closure = new HashSet<>(); |
19914 | 1760 |
closure.add(this); |
1761 |
while (progress) { |
|
1762 |
progress = false; |
|
22163 | 1763 |
for (Node n1 : new HashSet<>(closure)) { |
38516
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1764 |
progress = closure.addAll(n1.deps); |
19914 | 1765 |
} |
1766 |
} |
|
1767 |
return closure; |
|
15717 | 1768 |
} |
1769 |
||
19914 | 1770 |
/** |
1771 |
* Is this node a leaf? This means either the node has no dependencies, |
|
1772 |
* or it just has self-dependencies. |
|
1773 |
*/ |
|
1774 |
protected boolean isLeaf() { |
|
1775 |
//no deps, or only one self dep |
|
38516
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1776 |
if (deps.isEmpty()) return true; |
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1777 |
for (Node n : deps) { |
19914 | 1778 |
if (n != this) { |
1779 |
return false; |
|
1780 |
} |
|
1781 |
} |
|
1782 |
return true; |
|
1783 |
} |
|
1784 |
||
1785 |
/** |
|
1786 |
* Merge this node with another node, acquiring its dependencies. |
|
1787 |
* This routine is used to merge all cyclic node together and |
|
1788 |
* form an acyclic graph. |
|
1789 |
*/ |
|
1790 |
protected void mergeWith(List<? extends Node> nodes) { |
|
15717 | 1791 |
for (Node n : nodes) { |
1792 |
Assert.check(n.data.length() == 1, "Attempt to merge a compound node!"); |
|
1793 |
data.appendList(n.data); |
|
38516
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1794 |
addDependencies(n.deps); |
15717 | 1795 |
} |
1796 |
//update deps |
|
38516
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1797 |
Set<Node> deps2 = new HashSet<>(); |
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1798 |
for (Node d : deps) { |
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1799 |
if (data.contains(d.data.first())) { |
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1800 |
deps2.add(this); |
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1801 |
} else { |
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1802 |
deps2.add(d); |
15717 | 1803 |
} |
1804 |
} |
|
1805 |
deps = deps2; |
|
1806 |
} |
|
1807 |
||
19914 | 1808 |
/** |
1809 |
* Notify all nodes that something has changed in the graph |
|
1810 |
* topology. |
|
1811 |
*/ |
|
1812 |
private void graphChanged(Node from, Node to) { |
|
38516
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1813 |
if (removeDependency(from)) { |
15717 | 1814 |
if (to != null) { |
38516
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1815 |
addDependency(to); |
15717 | 1816 |
} |
1817 |
} |
|
1818 |
} |
|
25844
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
1819 |
|
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
1820 |
@Override |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
1821 |
public Properties nodeAttributes() { |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
1822 |
Properties p = new Properties(); |
26267
4ebd9393b373
8056075: Add support for dumping inference dependency graphs
mcimadamore
parents:
25874
diff
changeset
|
1823 |
p.put("label", "\"" + toString() + "\""); |
25844
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
1824 |
return p; |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
1825 |
} |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
1826 |
|
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
1827 |
@Override |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
1828 |
public Properties dependencyAttributes(Node sink, GraphUtils.DependencyKind dk) { |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
1829 |
Properties p = new Properties(); |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
1830 |
p.put("style", ((DependencyKind)dk).dotSyle); |
38516
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1831 |
StringBuilder buf = new StringBuilder(); |
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1832 |
String sep = ""; |
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1833 |
for (Type from : data) { |
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1834 |
UndetVar uv = (UndetVar)inferenceContext.asUndetVar(from); |
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1835 |
for (Type bound : uv.getBounds(InferenceBound.values())) { |
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1836 |
if (bound.containsAny(List.from(sink.data))) { |
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1837 |
buf.append(sep); |
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1838 |
buf.append(bound); |
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1839 |
sep = ","; |
25844
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
1840 |
} |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
1841 |
} |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
1842 |
} |
38516
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1843 |
p.put("label", "\"" + buf.toString() + "\""); |
25844
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
1844 |
return p; |
48eab270456c
8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents:
25279
diff
changeset
|
1845 |
} |
15717 | 1846 |
} |
1847 |
||
1848 |
/** the nodes in the inference graph */ |
|
1849 |
ArrayList<Node> nodes; |
|
1850 |
||
38516
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1851 |
InferenceGraph() { |
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1852 |
initNodes(); |
19914 | 1853 |
} |
1854 |
||
1855 |
/** |
|
1856 |
* Basic lookup helper for retrieving a graph node given an inference |
|
1857 |
* variable type. |
|
1858 |
*/ |
|
1859 |
public Node findNode(Type t) { |
|
1860 |
for (Node n : nodes) { |
|
1861 |
if (n.data.contains(t)) { |
|
1862 |
return n; |
|
1863 |
} |
|
1864 |
} |
|
1865 |
return null; |
|
15717 | 1866 |
} |
1867 |
||
1868 |
/** |
|
1869 |
* Delete a node from the graph. This update the underlying structure |
|
1870 |
* of the graph (including dependencies) via listeners updates. |
|
1871 |
*/ |
|
1872 |
public void deleteNode(Node n) { |
|
1873 |
Assert.check(nodes.contains(n)); |
|
1874 |
nodes.remove(n); |
|
1875 |
notifyUpdate(n, null); |
|
1876 |
} |
|
1877 |
||
1878 |
/** |
|
1879 |
* Notify all nodes of a change in the graph. If the target node is |
|
1880 |
* {@code null} the source node is assumed to be removed. |
|
1881 |
*/ |
|
1882 |
void notifyUpdate(Node from, Node to) { |
|
1883 |
for (Node n : nodes) { |
|
1884 |
n.graphChanged(from, to); |
|
1885 |
} |
|
1886 |
} |
|
1887 |
||
1888 |
/** |
|
1889 |
* Create the graph nodes. First a simple node is created for every inference |
|
1890 |
* variables to be solved. Then Tarjan is used to found all connected components |
|
1891 |
* in the graph. For each component containing more than one node, a super node is |
|
19914 | 1892 |
* created, effectively replacing the original cyclic nodes. |
15717 | 1893 |
*/ |
38516
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1894 |
void initNodes() { |
19914 | 1895 |
//add nodes |
22163 | 1896 |
nodes = new ArrayList<>(); |
15717 | 1897 |
for (Type t : inferenceContext.restvars()) { |
1898 |
nodes.add(new Node(t)); |
|
1899 |
} |
|
19914 | 1900 |
//add dependencies |
15717 | 1901 |
for (Node n_i : nodes) { |
1902 |
Type i = n_i.data.first(); |
|
1903 |
for (Node n_j : nodes) { |
|
1904 |
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
|
1905 |
UndetVar uv_i = (UndetVar)inferenceContext.asUndetVar(i); |
15717 | 1906 |
if (Type.containsAny(uv_i.getBounds(InferenceBound.values()), List.of(j))) { |
19914 | 1907 |
//update i's bound dependencies |
38516
b643c42e9d25
8156954: javac incorrectly complains of incompatible types
mcimadamore
parents:
37003
diff
changeset
|
1908 |
n_i.addDependency(n_j); |
15717 | 1909 |
} |
1910 |
} |
|
1911 |
} |
|
19914 | 1912 |
//merge cyclic nodes |
22163 | 1913 |
ArrayList<Node> acyclicNodes = new ArrayList<>(); |
15717 | 1914 |
for (List<? extends Node> conSubGraph : GraphUtils.tarjan(nodes)) { |
1915 |
if (conSubGraph.length() > 1) { |
|
1916 |
Node root = conSubGraph.head; |
|
1917 |
root.mergeWith(conSubGraph.tail); |
|
1918 |
for (Node n : conSubGraph) { |
|
1919 |
notifyUpdate(n, root); |
|
1920 |
} |
|
1921 |
} |
|
16321
75e64bcd3ab0
8008723: Graph Inference: bad graph calculation leads to assertion error
mcimadamore
parents:
15717
diff
changeset
|
1922 |
acyclicNodes.add(conSubGraph.head); |
15717 | 1923 |
} |
16321
75e64bcd3ab0
8008723: Graph Inference: bad graph calculation leads to assertion error
mcimadamore
parents:
15717
diff
changeset
|
1924 |
nodes = acyclicNodes; |
15717 | 1925 |
} |
1926 |
||
1927 |
/** |
|
1928 |
* Debugging: dot representation of this graph |
|
1929 |
*/ |
|
1930 |
String toDot() { |
|
1931 |
StringBuilder buf = new StringBuilder(); |
|
1932 |
for (Type t : inferenceContext.undetvars) { |
|
1933 |
UndetVar uv = (UndetVar)t; |
|
1934 |
buf.append(String.format("var %s - upper bounds = %s, lower bounds = %s, eq bounds = %s\\n", |
|
1935 |
uv.qtype, uv.getBounds(InferenceBound.UPPER), uv.getBounds(InferenceBound.LOWER), |
|
1936 |
uv.getBounds(InferenceBound.EQ))); |
|
1937 |
} |
|
1938 |
return GraphUtils.toDot(nodes, "inferenceGraph" + hashCode(), buf.toString()); |
|
1939 |
} |
|
1940 |
} |
|
1941 |
} |
|
1942 |
// </editor-fold> |
|
1943 |
||
1944 |
// <editor-fold defaultstate="collapsed" desc="Inference context"> |
|
1945 |
/** |
|
15705 | 1946 |
* Functional interface for defining inference callbacks. Certain actions |
1947 |
* (i.e. subtyping checks) might need to be redone after all inference variables |
|
1948 |
* have been fixed. |
|
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1949 |
*/ |
15705 | 1950 |
interface FreeTypeListener { |
1951 |
void typesInferred(InferenceContext inferenceContext); |
|
1952 |
} |
|
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1953 |
|
31937
ad43a6639c4a
8132215: class InferenceContext should live in a separate file
vromero
parents:
30014
diff
changeset
|
1954 |
final InferenceContext emptyContext; |
15705 | 1955 |
// </editor-fold> |
14047
7c7a5611cf76
7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents:
13440
diff
changeset
|
1956 |
} |