author | mcimadamore |
Fri, 14 Jan 2011 09:46:44 +0000 | |
changeset 8038 | 4a2973290d71 |
parent 8036 | 17b976649c61 |
child 8044 | 7fd529d4472c |
permissions | -rw-r--r-- |
10 | 1 |
/* |
7681 | 2 |
* Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. |
10 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5520 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.comp; |
|
27 |
||
6592
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
28 |
import com.sun.tools.javac.tree.JCTree; |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
29 |
import com.sun.tools.javac.tree.JCTree.JCTypeCast; |
8036
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
30 |
import com.sun.tools.javac.tree.TreeInfo; |
10 | 31 |
import com.sun.tools.javac.util.*; |
32 |
import com.sun.tools.javac.util.List; |
|
33 |
import com.sun.tools.javac.code.*; |
|
34 |
import com.sun.tools.javac.code.Type.*; |
|
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
35 |
import com.sun.tools.javac.code.Type.ForAll.ConstraintKind; |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
36 |
import com.sun.tools.javac.code.Symbol.*; |
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
37 |
import com.sun.tools.javac.util.JCDiagnostic; |
10 | 38 |
|
39 |
import static com.sun.tools.javac.code.TypeTags.*; |
|
40 |
||
41 |
/** Helper class for type parameter inference, used by the attribution phase. |
|
42 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5846
diff
changeset
|
43 |
* <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
|
44 |
* If you write code that depends on this, you do so at your own risk. |
10 | 45 |
* This code and its internal interfaces are subject to change or |
46 |
* deletion without notice.</b> |
|
47 |
*/ |
|
48 |
public class Infer { |
|
49 |
protected static final Context.Key<Infer> inferKey = |
|
50 |
new Context.Key<Infer>(); |
|
51 |
||
52 |
/** A value for prototypes that admit any type, including polymorphic ones. */ |
|
53 |
public static final Type anyPoly = new Type(NONE, null); |
|
54 |
||
55 |
Symtab syms; |
|
56 |
Types types; |
|
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
57 |
Check chk; |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
58 |
Resolve rs; |
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
59 |
JCDiagnostic.Factory diags; |
10 | 60 |
|
61 |
public static Infer instance(Context context) { |
|
62 |
Infer instance = context.get(inferKey); |
|
63 |
if (instance == null) |
|
64 |
instance = new Infer(context); |
|
65 |
return instance; |
|
66 |
} |
|
67 |
||
68 |
protected Infer(Context context) { |
|
69 |
context.put(inferKey, this); |
|
70 |
syms = Symtab.instance(context); |
|
71 |
types = Types.instance(context); |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
72 |
rs = Resolve.instance(context); |
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
73 |
chk = Check.instance(context); |
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
74 |
diags = JCDiagnostic.Factory.instance(context); |
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
75 |
ambiguousNoInstanceException = |
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
76 |
new NoInstanceException(true, diags); |
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
77 |
unambiguousNoInstanceException = |
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
78 |
new NoInstanceException(false, diags); |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
79 |
invalidInstanceException = |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
80 |
new InvalidInstanceException(diags); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
81 |
|
10 | 82 |
} |
83 |
||
6710
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
84 |
public static class InferenceException extends Resolve.InapplicableMethodException { |
10 | 85 |
private static final long serialVersionUID = 0; |
86 |
||
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
87 |
InferenceException(JCDiagnostic.Factory diags) { |
6710
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
88 |
super(diags); |
10 | 89 |
} |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
90 |
} |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
91 |
|
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
92 |
public static class NoInstanceException extends InferenceException { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
93 |
private static final long serialVersionUID = 1; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
94 |
|
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
95 |
boolean isAmbiguous; // exist several incomparable best instances? |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
96 |
|
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
97 |
NoInstanceException(boolean isAmbiguous, JCDiagnostic.Factory diags) { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
98 |
super(diags); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
99 |
this.isAmbiguous = isAmbiguous; |
10 | 100 |
} |
101 |
} |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
102 |
|
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
103 |
public static class InvalidInstanceException extends InferenceException { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
104 |
private static final long serialVersionUID = 2; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
105 |
|
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
106 |
InvalidInstanceException(JCDiagnostic.Factory diags) { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
107 |
super(diags); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
108 |
} |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
109 |
} |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
110 |
|
1040
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
111 |
private final NoInstanceException ambiguousNoInstanceException; |
c0f5acfd9d15
6730423: Diagnostic formatter should be an instance field of JCDiagnostic
mcimadamore
parents:
735
diff
changeset
|
112 |
private final NoInstanceException unambiguousNoInstanceException; |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
113 |
private final InvalidInstanceException invalidInstanceException; |
10 | 114 |
|
115 |
/*************************************************************************** |
|
116 |
* Auxiliary type values and classes |
|
117 |
***************************************************************************/ |
|
118 |
||
119 |
/** A mapping that turns type variables into undetermined type variables. |
|
120 |
*/ |
|
121 |
Mapping fromTypeVarFun = new Mapping("fromTypeVarFun") { |
|
122 |
public Type apply(Type t) { |
|
123 |
if (t.tag == TYPEVAR) return new UndetVar(t); |
|
124 |
else return t.map(this); |
|
125 |
} |
|
126 |
}; |
|
127 |
||
128 |
/** A mapping that returns its type argument with every UndetVar replaced |
|
129 |
* by its `inst' field. Throws a NoInstanceException |
|
130 |
* if this not possible because an `inst' field is null. |
|
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
131 |
* Note: mutually referring undertvars will be left uninstantiated |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
132 |
* (that is, they will be replaced by the underlying type-variable). |
10 | 133 |
*/ |
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
134 |
|
10 | 135 |
Mapping getInstFun = new Mapping("getInstFun") { |
136 |
public Type apply(Type t) { |
|
137 |
switch (t.tag) { |
|
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
138 |
case UNKNOWN: |
10 | 139 |
throw ambiguousNoInstanceException |
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
140 |
.setMessage("undetermined.type"); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
141 |
case UNDETVAR: |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
142 |
UndetVar that = (UndetVar) t; |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
143 |
if (that.inst == null) |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
144 |
throw ambiguousNoInstanceException |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
145 |
.setMessage("type.variable.has.undetermined.type", |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
146 |
that.qtype); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
147 |
return isConstraintCyclic(that) ? |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
148 |
that.qtype : |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
149 |
apply(that.inst); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
150 |
default: |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
151 |
return t.map(this); |
10 | 152 |
} |
153 |
} |
|
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
154 |
|
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
155 |
private boolean isConstraintCyclic(UndetVar uv) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
156 |
Types.UnaryVisitor<Boolean> constraintScanner = |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
157 |
new Types.UnaryVisitor<Boolean>() { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
158 |
|
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
159 |
List<Type> seen = List.nil(); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
160 |
|
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
161 |
Boolean visit(List<Type> ts) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
162 |
for (Type t : ts) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
163 |
if (visit(t)) return true; |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
164 |
} |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
165 |
return false; |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
166 |
} |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
167 |
|
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
168 |
public Boolean visitType(Type t, Void ignored) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
169 |
return false; |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
170 |
} |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
171 |
|
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
172 |
@Override |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
173 |
public Boolean visitClassType(ClassType t, Void ignored) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
174 |
if (t.isCompound()) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
175 |
return visit(types.supertype(t)) || |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
176 |
visit(types.interfaces(t)); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
177 |
} else { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
178 |
return visit(t.getTypeArguments()); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
179 |
} |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
180 |
} |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
181 |
@Override |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
182 |
public Boolean visitWildcardType(WildcardType t, Void ignored) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
183 |
return visit(t.type); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
184 |
} |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
185 |
|
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
186 |
@Override |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
187 |
public Boolean visitUndetVar(UndetVar t, Void ignored) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
188 |
if (seen.contains(t)) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
189 |
return true; |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
190 |
} else { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
191 |
seen = seen.prepend(t); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
192 |
return visit(t.inst); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
193 |
} |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
194 |
} |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
195 |
}; |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
196 |
return constraintScanner.visit(uv); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
197 |
} |
10 | 198 |
}; |
199 |
||
200 |
/*************************************************************************** |
|
201 |
* Mini/Maximization of UndetVars |
|
202 |
***************************************************************************/ |
|
203 |
||
204 |
/** Instantiate undetermined type variable to its minimal upper bound. |
|
205 |
* Throw a NoInstanceException if this not possible. |
|
206 |
*/ |
|
207 |
void maximizeInst(UndetVar that, Warner warn) throws NoInstanceException { |
|
208 |
if (that.inst == null) { |
|
209 |
if (that.hibounds.isEmpty()) |
|
210 |
that.inst = syms.objectType; |
|
211 |
else if (that.hibounds.tail.isEmpty()) |
|
212 |
that.inst = that.hibounds.head; |
|
1991
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1257
diff
changeset
|
213 |
else |
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1257
diff
changeset
|
214 |
that.inst = types.glb(that.hibounds); |
10 | 215 |
} |
1991
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1257
diff
changeset
|
216 |
if (that.inst == null || |
3139
01d76d311f8e
6835428: regression: return-type inference rejects valid code
mcimadamore
parents:
2212
diff
changeset
|
217 |
that.inst.isErroneous()) |
1991
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1257
diff
changeset
|
218 |
throw ambiguousNoInstanceException |
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1257
diff
changeset
|
219 |
.setMessage("no.unique.maximal.instance.exists", |
aafb4bf914ee
6315770: javac inference allows creation of strange types: Integer & Runnable
mcimadamore
parents:
1257
diff
changeset
|
220 |
that.qtype, that.hibounds); |
10 | 221 |
} |
222 |
//where |
|
223 |
private boolean isSubClass(Type t, final List<Type> ts) { |
|
224 |
t = t.baseType(); |
|
225 |
if (t.tag == TYPEVAR) { |
|
226 |
List<Type> bounds = types.getBounds((TypeVar)t); |
|
227 |
for (Type s : ts) { |
|
228 |
if (!types.isSameType(t, s.baseType())) { |
|
229 |
for (Type bound : bounds) { |
|
230 |
if (!isSubClass(bound, List.of(s.baseType()))) |
|
231 |
return false; |
|
232 |
} |
|
233 |
} |
|
234 |
} |
|
235 |
} else { |
|
236 |
for (Type s : ts) { |
|
237 |
if (!t.tsym.isSubClass(s.baseType().tsym, types)) |
|
238 |
return false; |
|
239 |
} |
|
240 |
} |
|
241 |
return true; |
|
242 |
} |
|
243 |
||
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1206
diff
changeset
|
244 |
/** Instantiate undetermined type variable to the lub of all its lower bounds. |
10 | 245 |
* Throw a NoInstanceException if this not possible. |
246 |
*/ |
|
247 |
void minimizeInst(UndetVar that, Warner warn) throws NoInstanceException { |
|
248 |
if (that.inst == null) { |
|
249 |
if (that.lobounds.isEmpty()) |
|
250 |
that.inst = syms.botType; |
|
251 |
else if (that.lobounds.tail.isEmpty()) |
|
162
6620f2a8e265
6611449: Internal Error thrown during generic method/constructor invocation
mcimadamore
parents:
10
diff
changeset
|
252 |
that.inst = that.lobounds.head.isPrimitive() ? syms.errType : that.lobounds.head; |
10 | 253 |
else { |
254 |
that.inst = types.lub(that.lobounds); |
|
162
6620f2a8e265
6611449: Internal Error thrown during generic method/constructor invocation
mcimadamore
parents:
10
diff
changeset
|
255 |
} |
1257
873b053bf757
6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents:
1206
diff
changeset
|
256 |
if (that.inst == null || that.inst.tag == ERROR) |
10 | 257 |
throw ambiguousNoInstanceException |
258 |
.setMessage("no.unique.minimal.instance.exists", |
|
259 |
that.qtype, that.lobounds); |
|
260 |
// VGJ: sort of inlined maximizeInst() below. Adding |
|
261 |
// bounds can cause lobounds that are above hibounds. |
|
262 |
if (that.hibounds.isEmpty()) |
|
263 |
return; |
|
264 |
Type hb = null; |
|
265 |
if (that.hibounds.tail.isEmpty()) |
|
266 |
hb = that.hibounds.head; |
|
267 |
else for (List<Type> bs = that.hibounds; |
|
268 |
bs.nonEmpty() && hb == null; |
|
269 |
bs = bs.tail) { |
|
270 |
if (isSubClass(bs.head, that.hibounds)) |
|
271 |
hb = types.fromUnknownFun.apply(bs.head); |
|
272 |
} |
|
273 |
if (hb == null || |
|
274 |
!types.isSubtypeUnchecked(hb, that.hibounds, warn) || |
|
275 |
!types.isSubtypeUnchecked(that.inst, hb, warn)) |
|
276 |
throw ambiguousNoInstanceException; |
|
277 |
} |
|
278 |
} |
|
279 |
||
280 |
/*************************************************************************** |
|
281 |
* Exported Methods |
|
282 |
***************************************************************************/ |
|
283 |
||
284 |
/** Try to instantiate expression type `that' to given type `to'. |
|
285 |
* If a maximal instantiation exists which makes this type |
|
286 |
* a subtype of type `to', return the instantiated type. |
|
287 |
* If no instantiation exists, or if several incomparable |
|
288 |
* best instantiations exist throw a NoInstanceException. |
|
289 |
*/ |
|
290 |
public Type instantiateExpr(ForAll that, |
|
291 |
Type to, |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
292 |
Warner warn) throws InferenceException { |
10 | 293 |
List<Type> undetvars = Type.map(that.tvars, fromTypeVarFun); |
294 |
for (List<Type> l = undetvars; l.nonEmpty(); l = l.tail) { |
|
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
295 |
UndetVar uv = (UndetVar) l.head; |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
296 |
TypeVar tv = (TypeVar)uv.qtype; |
10 | 297 |
ListBuffer<Type> hibounds = new ListBuffer<Type>(); |
6154
c1b0c6641b92
6938454: Unable to determine generic type in program that compiles under Java 6
mcimadamore
parents:
5847
diff
changeset
|
298 |
for (Type t : that.getConstraints(tv, ConstraintKind.EXTENDS)) { |
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
299 |
hibounds.append(types.subst(t, that.tvars, undetvars)); |
10 | 300 |
} |
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
301 |
|
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
302 |
List<Type> inst = that.getConstraints(tv, ConstraintKind.EQUAL); |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
303 |
if (inst.nonEmpty() && inst.head.tag != BOT) { |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
304 |
uv.inst = inst.head; |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
305 |
} |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
306 |
uv.hibounds = hibounds.toList(); |
10 | 307 |
} |
308 |
Type qtype1 = types.subst(that.qtype, that.tvars, undetvars); |
|
7331
02ffc087c654
6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents:
6934
diff
changeset
|
309 |
if (!types.isSubtype(qtype1, |
02ffc087c654
6995200: JDK 7 compiler crashes when type-variable is inferred from expected primitive type
mcimadamore
parents:
6934
diff
changeset
|
310 |
qtype1.tag == UNDETVAR ? types.boxedTypeOrType(to) : to)) { |
10 | 311 |
throw unambiguousNoInstanceException |
6710
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
312 |
.setMessage("infer.no.conforming.instance.exists", |
10 | 313 |
that.tvars, that.qtype, to); |
314 |
} |
|
315 |
for (List<Type> l = undetvars; l.nonEmpty(); l = l.tail) |
|
316 |
maximizeInst((UndetVar) l.head, warn); |
|
317 |
// System.out.println(" = " + qtype1.map(getInstFun));//DEBUG |
|
318 |
||
319 |
// check bounds |
|
320 |
List<Type> targs = Type.map(undetvars, getInstFun); |
|
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
321 |
if (Type.containsAny(targs, that.tvars)) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
322 |
//replace uninferred type-vars |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
323 |
targs = types.subst(targs, |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
324 |
that.tvars, |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
325 |
instaniateAsUninferredVars(undetvars, that.tvars)); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
326 |
} |
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
327 |
return chk.checkType(warn.pos(), that.inst(targs, types), to); |
10 | 328 |
} |
6348
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
329 |
//where |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
330 |
private List<Type> instaniateAsUninferredVars(List<Type> undetvars, List<Type> tvars) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
331 |
ListBuffer<Type> new_targs = ListBuffer.lb(); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
332 |
//step 1 - create syntethic captured vars |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
333 |
for (Type t : undetvars) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
334 |
UndetVar uv = (UndetVar)t; |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
335 |
Type newArg = new CapturedType(t.tsym.name, t.tsym, uv.inst, syms.botType, null); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
336 |
new_targs = new_targs.append(newArg); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
337 |
} |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
338 |
//step 2 - replace synthetic vars in their bounds |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
339 |
for (Type t : new_targs.toList()) { |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
340 |
CapturedType ct = (CapturedType)t; |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
341 |
ct.bound = types.subst(ct.bound, tvars, new_targs.toList()); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
342 |
WildcardType wt = new WildcardType(ct.bound, BoundKind.EXTENDS, syms.boundClass); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
343 |
ct.wildcard = wt; |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
344 |
} |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
345 |
return new_targs.toList(); |
6c9b14d4a438
6369605: Unconstrained type variables fails to include bounds
mcimadamore
parents:
6154
diff
changeset
|
346 |
} |
10 | 347 |
|
348 |
/** Instantiate method type `mt' by finding instantiations of |
|
349 |
* `tvars' so that method can be applied to `argtypes'. |
|
350 |
*/ |
|
5489
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
3778
diff
changeset
|
351 |
public Type instantiateMethod(final Env<AttrContext> env, |
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
3778
diff
changeset
|
352 |
List<Type> tvars, |
10 | 353 |
MethodType mt, |
5846
6df0e6bcb388
6945418: Project Coin: Simplified Varargs Method Invocation
mcimadamore
parents:
5520
diff
changeset
|
354 |
final Symbol msym, |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
355 |
final List<Type> argtypes, |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
356 |
final boolean allowBoxing, |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
357 |
final boolean useVarargs, |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
358 |
final Warner warn) throws InferenceException { |
10 | 359 |
//-System.err.println("instantiateMethod(" + tvars + ", " + mt + ", " + argtypes + ")"); //DEBUG |
360 |
List<Type> undetvars = Type.map(tvars, fromTypeVarFun); |
|
361 |
List<Type> formals = mt.argtypes; |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
362 |
//need to capture exactly once - otherwise subsequent |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
363 |
//applicability checks might fail |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
364 |
final List<Type> capturedArgs = types.capture(argtypes); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
365 |
List<Type> actuals = capturedArgs; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
366 |
List<Type> actualsNoCapture = argtypes; |
10 | 367 |
// instantiate all polymorphic argument types and |
368 |
// set up lower bounds constraints for undetvars |
|
369 |
Type varargsFormal = useVarargs ? formals.last() : null; |
|
6710
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
370 |
if (varargsFormal == null && |
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
371 |
actuals.size() != formals.size()) { |
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
372 |
throw unambiguousNoInstanceException |
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
373 |
.setMessage("infer.arg.length.mismatch"); |
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
374 |
} |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
375 |
while (actuals.nonEmpty() && formals.head != varargsFormal) { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
376 |
Type formal = formals.head; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
377 |
Type actual = actuals.head.baseType(); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
378 |
Type actualNoCapture = actualsNoCapture.head.baseType(); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
379 |
if (actual.tag == FORALL) |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
380 |
actual = instantiateArg((ForAll)actual, formal, tvars, warn); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
381 |
Type undetFormal = types.subst(formal, tvars, undetvars); |
10 | 382 |
boolean works = allowBoxing |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
383 |
? types.isConvertible(actual, undetFormal, warn) |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
384 |
: types.isSubtypeUnchecked(actual, undetFormal, warn); |
10 | 385 |
if (!works) { |
386 |
throw unambiguousNoInstanceException |
|
6710
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
387 |
.setMessage("infer.no.conforming.assignment.exists", |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
388 |
tvars, actualNoCapture, formal); |
10 | 389 |
} |
390 |
formals = formals.tail; |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
391 |
actuals = actuals.tail; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
392 |
actualsNoCapture = actualsNoCapture.tail; |
10 | 393 |
} |
6710
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
394 |
|
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
395 |
if (formals.head != varargsFormal) // not enough args |
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
396 |
throw unambiguousNoInstanceException.setMessage("infer.arg.length.mismatch"); |
10 | 397 |
|
398 |
// for varargs arguments as well |
|
399 |
if (useVarargs) { |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
400 |
Type elemType = types.elemtype(varargsFormal); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
401 |
Type elemUndet = types.subst(elemType, tvars, undetvars); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
402 |
while (actuals.nonEmpty()) { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
403 |
Type actual = actuals.head.baseType(); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
404 |
Type actualNoCapture = actualsNoCapture.head.baseType(); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
405 |
if (actual.tag == FORALL) |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
406 |
actual = instantiateArg((ForAll)actual, elemType, tvars, warn); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
407 |
boolean works = types.isConvertible(actual, elemUndet, warn); |
10 | 408 |
if (!works) { |
409 |
throw unambiguousNoInstanceException |
|
6710
b14e6fe7b290
5088624: cannot find symbol message should be more intelligent
mcimadamore
parents:
6592
diff
changeset
|
410 |
.setMessage("infer.no.conforming.assignment.exists", |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
411 |
tvars, actualNoCapture, elemType); |
10 | 412 |
} |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
413 |
actuals = actuals.tail; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
414 |
actualsNoCapture = actualsNoCapture.tail; |
10 | 415 |
} |
416 |
} |
|
417 |
||
418 |
// minimize as yet undetermined type variables |
|
419 |
for (Type t : undetvars) |
|
420 |
minimizeInst((UndetVar) t, warn); |
|
421 |
||
422 |
/** Type variables instantiated to bottom */ |
|
423 |
ListBuffer<Type> restvars = new ListBuffer<Type>(); |
|
424 |
||
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
425 |
/** Undet vars instantiated to bottom */ |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
426 |
final ListBuffer<Type> restundet = new ListBuffer<Type>(); |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
427 |
|
10 | 428 |
/** Instantiated types or TypeVars if under-constrained */ |
429 |
ListBuffer<Type> insttypes = new ListBuffer<Type>(); |
|
430 |
||
431 |
/** Instantiated types or UndetVars if under-constrained */ |
|
432 |
ListBuffer<Type> undettypes = new ListBuffer<Type>(); |
|
433 |
||
434 |
for (Type t : undetvars) { |
|
435 |
UndetVar uv = (UndetVar)t; |
|
436 |
if (uv.inst.tag == BOT) { |
|
437 |
restvars.append(uv.qtype); |
|
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
438 |
restundet.append(uv); |
10 | 439 |
insttypes.append(uv.qtype); |
440 |
undettypes.append(uv); |
|
441 |
uv.inst = null; |
|
442 |
} else { |
|
443 |
insttypes.append(uv.inst); |
|
444 |
undettypes.append(uv.inst); |
|
445 |
} |
|
446 |
} |
|
447 |
checkWithinBounds(tvars, undettypes.toList(), warn); |
|
448 |
||
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
449 |
mt = (MethodType)types.subst(mt, tvars, insttypes.toList()); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
450 |
|
10 | 451 |
if (!restvars.isEmpty()) { |
452 |
// if there are uninstantiated variables, |
|
453 |
// quantify result type with them |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
454 |
final List<Type> inferredTypes = insttypes.toList(); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
455 |
final List<Type> all_tvars = tvars; //this is the wrong tvars |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
456 |
final MethodType mt2 = new MethodType(mt.argtypes, null, mt.thrown, syms.methodClass); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
457 |
mt2.restype = new ForAll(restvars.toList(), mt.restype) { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
458 |
@Override |
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
459 |
public List<Type> getConstraints(TypeVar tv, ConstraintKind ck) { |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
460 |
for (Type t : restundet.toList()) { |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
461 |
UndetVar uv = (UndetVar)t; |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
462 |
if (uv.qtype == tv) { |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
463 |
switch (ck) { |
6154
c1b0c6641b92
6938454: Unable to determine generic type in program that compiles under Java 6
mcimadamore
parents:
5847
diff
changeset
|
464 |
case EXTENDS: return uv.hibounds.appendList(types.subst(types.getBounds(tv), all_tvars, inferredTypes)); |
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
465 |
case SUPER: return uv.lobounds; |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
466 |
case EQUAL: return uv.inst != null ? List.of(uv.inst) : List.<Type>nil(); |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
467 |
} |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
468 |
} |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
469 |
} |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
470 |
return List.nil(); |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
471 |
} |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
472 |
|
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
473 |
@Override |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
474 |
public Type inst(List<Type> inferred, Types types) throws NoInstanceException { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
475 |
List<Type> formals = types.subst(mt2.argtypes, tvars, inferred); |
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
476 |
if (!rs.argumentsAcceptable(capturedArgs, formals, |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
477 |
allowBoxing, useVarargs, warn)) { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
478 |
// inferred method is not applicable |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
479 |
throw invalidInstanceException.setMessage("inferred.do.not.conform.to.params", formals, argtypes); |
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
480 |
} |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
481 |
// check that inferred bounds conform to their bounds |
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
482 |
checkWithinBounds(all_tvars, |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
483 |
types.subst(inferredTypes, tvars, inferred), warn); |
5489
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
3778
diff
changeset
|
484 |
if (useVarargs) { |
7643
a067a0cda531
6993978: Project Coin: Compiler support of annotation to reduce varargs warnings
mcimadamore
parents:
7331
diff
changeset
|
485 |
chk.checkVararg(env.tree.pos(), formals, msym); |
5489
e7af65bf7577
6730476: invalid "unchecked generic array" warning
mcimadamore
parents:
3778
diff
changeset
|
486 |
} |
3778
38a70273507b
6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents:
3140
diff
changeset
|
487 |
return super.inst(inferred, types); |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
488 |
}}; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
489 |
return mt2; |
10 | 490 |
} |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
491 |
else if (!rs.argumentsAcceptable(capturedArgs, mt.getParameterTypes(), allowBoxing, useVarargs, warn)) { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
492 |
// inferred method is not applicable |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
493 |
throw invalidInstanceException.setMessage("inferred.do.not.conform.to.params", mt.getParameterTypes(), argtypes); |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
494 |
} |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
495 |
else { |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
496 |
// return instantiated version of method type |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
497 |
return mt; |
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
498 |
} |
10 | 499 |
} |
500 |
//where |
|
501 |
||
502 |
/** Try to instantiate argument type `that' to given type `to'. |
|
503 |
* If this fails, try to insantiate `that' to `to' where |
|
504 |
* every occurrence of a type variable in `tvars' is replaced |
|
505 |
* by an unknown type. |
|
506 |
*/ |
|
507 |
private Type instantiateArg(ForAll that, |
|
508 |
Type to, |
|
509 |
List<Type> tvars, |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
510 |
Warner warn) throws InferenceException { |
10 | 511 |
List<Type> targs; |
512 |
try { |
|
513 |
return instantiateExpr(that, to, warn); |
|
514 |
} catch (NoInstanceException ex) { |
|
515 |
Type to1 = to; |
|
516 |
for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) |
|
517 |
to1 = types.subst(to1, List.of(l.head), List.of(syms.unknownType)); |
|
518 |
return instantiateExpr(that, to1, warn); |
|
519 |
} |
|
520 |
} |
|
521 |
||
522 |
/** check that type parameters are within their bounds. |
|
523 |
*/ |
|
6154
c1b0c6641b92
6938454: Unable to determine generic type in program that compiles under Java 6
mcimadamore
parents:
5847
diff
changeset
|
524 |
void checkWithinBounds(List<Type> tvars, |
10 | 525 |
List<Type> arguments, |
526 |
Warner warn) |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
527 |
throws InvalidInstanceException { |
10 | 528 |
for (List<Type> tvs = tvars, args = arguments; |
529 |
tvs.nonEmpty(); |
|
530 |
tvs = tvs.tail, args = args.tail) { |
|
531 |
if (args.head instanceof UndetVar) continue; |
|
532 |
List<Type> bounds = types.subst(types.getBounds((TypeVar)tvs.head), tvars, arguments); |
|
533 |
if (!types.isSubtypeUnchecked(args.head, bounds, warn)) |
|
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
534 |
throw invalidInstanceException |
10 | 535 |
.setMessage("inferred.do.not.conform.to.bounds", |
3140
15a274b13051
6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents:
3139
diff
changeset
|
536 |
args.head, bounds); |
10 | 537 |
} |
538 |
} |
|
6592
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
539 |
|
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
540 |
/** |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
541 |
* 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
|
542 |
* 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
|
543 |
* 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
|
544 |
*/ |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
545 |
Type instantiatePolymorphicSignatureInstance(Env<AttrContext> env, Type site, |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
546 |
Name name, |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
547 |
MethodSymbol spMethod, // sig. poly. method or null if none |
8036
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
548 |
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
|
549 |
final Type restype; |
8036
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
550 |
|
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
551 |
//The return type for a polymorphic signature call is computed from |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
552 |
//the enclosing tree E, as follows: if E is a cast, then use the |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
553 |
//target type of the cast expression as a return type; if E is an |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
554 |
//expression statement, the return type is 'void' - otherwise the |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
555 |
//return type is simply 'Object'. A correctness check ensures that |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
556 |
//env.next refers to the lexically enclosing environment in which |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
557 |
//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
|
558 |
|
8036
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
559 |
switch (env.next.tree.getTag()) { |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
560 |
case JCTree.TYPECAST: |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
561 |
JCTypeCast castTree = (JCTypeCast)env.next.tree; |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
562 |
restype = (TreeInfo.skipParens(castTree.expr) == env.tree) ? |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
563 |
castTree.clazz.type : |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
564 |
syms.objectType; |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
565 |
break; |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
566 |
case JCTree.EXEC: |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
567 |
JCTree.JCExpressionStatement execTree = |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
568 |
(JCTree.JCExpressionStatement)env.next.tree; |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
569 |
restype = (TreeInfo.skipParens(execTree.expr) == env.tree) ? |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
570 |
syms.voidType : |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
571 |
syms.objectType; |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
572 |
break; |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
573 |
default: |
17b976649c61
6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents:
7681
diff
changeset
|
574 |
restype = syms.objectType; |
6592
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
575 |
} |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
576 |
|
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
577 |
List<Type> paramtypes = Type.map(argtypes, implicitArgType); |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
578 |
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
|
579 |
spMethod.getThrownTypes() : |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
580 |
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
|
581 |
|
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
582 |
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
|
583 |
restype, |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
584 |
exType, |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
585 |
syms.methodClass); |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
586 |
return mtype; |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
587 |
} |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
588 |
//where |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
589 |
Mapping implicitArgType = new Mapping ("implicitArgType") { |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
590 |
public Type apply(Type t) { |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
591 |
t = types.erasure(t); |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
592 |
if (t.tag == BOT) |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
593 |
// nulls type as the marker type Null (which has no instances) |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
594 |
// infer as java.lang.Void for now |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
595 |
t = types.boxedClass(syms.voidType).type; |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
596 |
return t; |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
597 |
} |
dc56420a69bc
6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents:
6348
diff
changeset
|
598 |
}; |
10 | 599 |
} |