src/share/classes/com/sun/tools/javac/code/Types.java

changeset 2400
0e026d3f2786
parent 2398
7c925f35f81c
child 2407
8a5512cb5e9d
equal deleted inserted replaced
2399:f4254623c54e 2400:0e026d3f2786
120 functionDescriptorLookupError = new FunctionDescriptorLookupError(); 120 functionDescriptorLookupError = new FunctionDescriptorLookupError();
121 noWarnings = new Warner(null); 121 noWarnings = new Warner(null);
122 } 122 }
123 // </editor-fold> 123 // </editor-fold>
124 124
125 // <editor-fold defaultstate="collapsed" desc="upperBound"> 125 // <editor-fold defaultstate="collapsed" desc="bounds">
126 /** 126 /**
127 * The "rvalue conversion".<br> 127 * Get a wildcard's upper bound, returning non-wildcards unchanged.
128 * The upper bound of most types is the type 128 * @param t a type argument, either a wildcard or a type
129 * itself. Wildcards, on the other hand have upper 129 */
130 * and lower bounds. 130 public Type wildUpperBound(Type t) {
131 * @param t a type 131 if (t.hasTag(WILDCARD)) {
132 * @return the upper bound of the given type 132 WildcardType w = (WildcardType) t.unannotatedType();
133 */ 133 if (w.isSuperBound())
134 public Type upperBound(Type t) { 134 return w.bound == null ? syms.objectType : w.bound.bound;
135 return upperBound.visit(t).unannotatedType(); 135 else
136 } 136 return wildUpperBound(w.type);
137 // where 137 }
138 private final MapVisitor<Void> upperBound = new MapVisitor<Void>() { 138 else return t;
139 139 }
140 @Override 140
141 public Type visitWildcardType(WildcardType t, Void ignored) { 141 /**
142 if (t.isSuperBound()) 142 * Get a capture variable's upper bound, returning other types unchanged.
143 return t.bound == null ? syms.objectType : t.bound.bound; 143 * @param t a type
144 else 144 */
145 return visit(t.type); 145 public Type cvarUpperBound(Type t) {
146 } 146 if (t.hasTag(TYPEVAR)) {
147 147 TypeVar v = (TypeVar) t.unannotatedType();
148 @Override 148 return v.isCaptured() ? cvarUpperBound(v.bound) : v;
149 public Type visitCapturedType(CapturedType t, Void ignored) { 149 }
150 return visit(t.bound); 150 else return t;
151 } 151 }
152 }; 152
153 // </editor-fold>
154
155 // <editor-fold defaultstate="collapsed" desc="wildLowerBound">
156 /** 153 /**
157 * Get a wildcard's lower bound, returning non-wildcards unchanged. 154 * Get a wildcard's lower bound, returning non-wildcards unchanged.
158 * @param t a type argument, either a wildcard or a type 155 * @param t a type argument, either a wildcard or a type
159 */ 156 */
160 public Type wildLowerBound(Type t) { 157 public Type wildLowerBound(Type t) {
162 WildcardType w = (WildcardType) t; 159 WildcardType w = (WildcardType) t;
163 return w.isExtendsBound() ? syms.botType : wildLowerBound(w.type); 160 return w.isExtendsBound() ? syms.botType : wildLowerBound(w.type);
164 } 161 }
165 else return t; 162 else return t;
166 } 163 }
167 // </editor-fold> 164
168
169 // <editor-fold defaultstate="collapsed" desc="cvarLowerBound">
170 /** 165 /**
171 * Get a capture variable's lower bound, returning other types unchanged. 166 * Get a capture variable's lower bound, returning other types unchanged.
172 * @param t a type 167 * @param t a type
173 */ 168 */
174 public Type cvarLowerBound(Type t) { 169 public Type cvarLowerBound(Type t) {
902 s = new WildcardType(syms.objectType, 897 s = new WildcardType(syms.objectType,
903 BoundKind.UNBOUND, 898 BoundKind.UNBOUND,
904 syms.boundClass); 899 syms.boundClass);
905 changed = true; 900 changed = true;
906 } else if (s != orig) { 901 } else if (s != orig) {
907 s = new WildcardType(upperBound(s), 902 s = new WildcardType(wildUpperBound(s),
908 BoundKind.EXTENDS, 903 BoundKind.EXTENDS,
909 syms.boundClass); 904 syms.boundClass);
910 changed = true; 905 changed = true;
911 } 906 }
912 rewrite.append(s); 907 rewrite.append(s);
1111 else { 1106 else {
1112 //special case for s == ? super X, where upper(s) = u 1107 //special case for s == ? super X, where upper(s) = u
1113 //check that u == t, where u has been set by Type.withTypeVar 1108 //check that u == t, where u has been set by Type.withTypeVar
1114 return s.isSuperBound() && 1109 return s.isSuperBound() &&
1115 !s.isExtendsBound() && 1110 !s.isExtendsBound() &&
1116 visit(t, upperBound(s)); 1111 visit(t, wildUpperBound(s));
1117 } 1112 }
1118 } 1113 }
1119 default: 1114 default:
1120 throw new AssertionError("isSameType " + t.getTag()); 1115 throw new AssertionError("isSameType " + t.getTag());
1121 } 1116 }
1138 1133
1139 if (s.isPartial()) 1134 if (s.isPartial())
1140 return visit(s, t); 1135 return visit(s, t);
1141 1136
1142 if (s.isSuperBound() && !s.isExtendsBound()) 1137 if (s.isSuperBound() && !s.isExtendsBound())
1143 return visit(t, upperBound(s)) && visit(t, wildLowerBound(s)); 1138 return visit(t, wildUpperBound(s)) && visit(t, wildLowerBound(s));
1144 1139
1145 if (t.isCompound() && s.isCompound()) { 1140 if (t.isCompound() && s.isCompound()) {
1146 if (!visit(supertype(t), supertype(s))) 1141 if (!visit(supertype(t), supertype(s)))
1147 return false; 1142 return false;
1148 1143
1288 UndetVar undetvar = (UndetVar)t; 1283 UndetVar undetvar = (UndetVar)t;
1289 WildcardType wt = (WildcardType)s.unannotatedType(); 1284 WildcardType wt = (WildcardType)s.unannotatedType();
1290 switch(wt.kind) { 1285 switch(wt.kind) {
1291 case UNBOUND: //similar to ? extends Object 1286 case UNBOUND: //similar to ? extends Object
1292 case EXTENDS: { 1287 case EXTENDS: {
1293 Type bound = upperBound(s); 1288 Type bound = wildUpperBound(s);
1294 undetvar.addBound(InferenceBound.UPPER, bound, this); 1289 undetvar.addBound(InferenceBound.UPPER, bound, this);
1295 break; 1290 break;
1296 } 1291 }
1297 case SUPER: { 1292 case SUPER: {
1298 Type bound = wildLowerBound(s); 1293 Type bound = wildLowerBound(s);
1349 return containsType.visit(t, s); 1344 return containsType.visit(t, s);
1350 } 1345 }
1351 // where 1346 // where
1352 private TypeRelation containsType = new TypeRelation() { 1347 private TypeRelation containsType = new TypeRelation() {
1353 1348
1354 private Type U(Type t) {
1355 while (t.hasTag(WILDCARD)) {
1356 WildcardType w = (WildcardType)t.unannotatedType();
1357 if (w.isSuperBound())
1358 return w.bound == null ? syms.objectType : w.bound.bound;
1359 else
1360 t = w.type;
1361 }
1362 return t;
1363 }
1364
1365 private Type L(Type t) {
1366 while (t.hasTag(WILDCARD)) {
1367 WildcardType w = (WildcardType)t.unannotatedType();
1368 if (w.isExtendsBound())
1369 return syms.botType;
1370 else
1371 t = w.type;
1372 }
1373 return t;
1374 }
1375
1376 public Boolean visitType(Type t, Type s) { 1349 public Boolean visitType(Type t, Type s) {
1377 if (s.isPartial()) 1350 if (s.isPartial())
1378 return containedBy(s, t); 1351 return containedBy(s, t);
1379 else 1352 else
1380 return isSameType(t, s); 1353 return isSameType(t, s);
1382 1355
1383 // void debugContainsType(WildcardType t, Type s) { 1356 // void debugContainsType(WildcardType t, Type s) {
1384 // System.err.println(); 1357 // System.err.println();
1385 // System.err.format(" does %s contain %s?%n", t, s); 1358 // System.err.format(" does %s contain %s?%n", t, s);
1386 // System.err.format(" %s U(%s) <: U(%s) %s = %s%n", 1359 // System.err.format(" %s U(%s) <: U(%s) %s = %s%n",
1387 // upperBound(s), s, t, U(t), 1360 // wildUpperBound(s), s, t, wildUpperBound(t),
1388 // t.isSuperBound() 1361 // t.isSuperBound()
1389 // || isSubtypeNoCapture(upperBound(s), U(t))); 1362 // || isSubtypeNoCapture(wildUpperBound(s), wildUpperBound(t)));
1390 // System.err.format(" %s L(%s) <: L(%s) %s = %s%n", 1363 // System.err.format(" %s L(%s) <: L(%s) %s = %s%n",
1391 // L(t), t, s, wildLowerBound(s), 1364 // wildLowerBound(t), t, s, wildLowerBound(s),
1392 // t.isExtendsBound() 1365 // t.isExtendsBound()
1393 // || isSubtypeNoCapture(L(t), wildLowerBound(s))); 1366 // || isSubtypeNoCapture(wildLowerBound(t), wildLowerBound(s)));
1394 // System.err.println(); 1367 // System.err.println();
1395 // } 1368 // }
1396 1369
1397 @Override 1370 @Override
1398 public Boolean visitWildcardType(WildcardType t, Type s) { 1371 public Boolean visitWildcardType(WildcardType t, Type s) {
1400 return containedBy(s, t); 1373 return containedBy(s, t);
1401 else { 1374 else {
1402 // debugContainsType(t, s); 1375 // debugContainsType(t, s);
1403 return isSameWildcard(t, s) 1376 return isSameWildcard(t, s)
1404 || isCaptureOf(s, t) 1377 || isCaptureOf(s, t)
1405 || ((t.isExtendsBound() || isSubtypeNoCapture(L(t), wildLowerBound(s))) && 1378 || ((t.isExtendsBound() || isSubtypeNoCapture(wildLowerBound(t), wildLowerBound(s))) &&
1406 (t.isSuperBound() || isSubtypeNoCapture(upperBound(s), U(t)))); 1379 // TODO: JDK-8039214, cvarUpperBound call here is incorrect
1380 (t.isSuperBound() || isSubtypeNoCapture(cvarUpperBound(wildUpperBound(s)), wildUpperBound(t))));
1407 } 1381 }
1408 } 1382 }
1409 1383
1410 @Override 1384 @Override
1411 public Boolean visitUndetVar(UndetVar t, Type s) { 1385 public Boolean visitUndetVar(UndetVar t, Type s) {
1519 } 1493 }
1520 } 1494 }
1521 1495
1522 @Override 1496 @Override
1523 public Boolean visitWildcardType(WildcardType t, Type s) { 1497 public Boolean visitWildcardType(WildcardType t, Type s) {
1524 return isCastable(upperBound(t), s, warnStack.head); 1498 return isCastable(wildUpperBound(t), s, warnStack.head);
1525 } 1499 }
1526 1500
1527 @Override 1501 @Override
1528 public Boolean visitClassType(ClassType t, Type s) { 1502 public Boolean visitClassType(ClassType t, Type s) {
1529 if (s.hasTag(ERROR) || s.hasTag(BOT)) 1503 if (s.hasTag(ERROR) || s.hasTag(BOT))
1760 if (s.isUnbound()) 1734 if (s.isUnbound())
1761 return false; 1735 return false;
1762 1736
1763 if (t.isExtendsBound()) { 1737 if (t.isExtendsBound()) {
1764 if (s.isExtendsBound()) 1738 if (s.isExtendsBound())
1765 return !isCastableRecursive(t.type, upperBound(s)); 1739 return !isCastableRecursive(t.type, wildUpperBound(s));
1766 else if (s.isSuperBound()) 1740 else if (s.isSuperBound())
1767 return notSoftSubtypeRecursive(wildLowerBound(s), t.type); 1741 return notSoftSubtypeRecursive(wildLowerBound(s), t.type);
1768 } else if (t.isSuperBound()) { 1742 } else if (t.isSuperBound()) {
1769 if (s.isExtendsBound()) 1743 if (s.isExtendsBound())
1770 return notSoftSubtypeRecursive(t.type, upperBound(s)); 1744 return notSoftSubtypeRecursive(t.type, wildUpperBound(s));
1771 } 1745 }
1772 return false; 1746 return false;
1773 } 1747 }
1774 }; 1748 };
1775 // </editor-fold> 1749 // </editor-fold>
1801 return !isCastable(tv.bound, 1775 return !isCastable(tv.bound,
1802 relaxBound(s), 1776 relaxBound(s),
1803 noWarnings); 1777 noWarnings);
1804 } 1778 }
1805 if (!s.hasTag(WILDCARD)) 1779 if (!s.hasTag(WILDCARD))
1806 s = upperBound(s); 1780 s = cvarUpperBound(s);
1807 1781
1808 return !isSubtype(t, relaxBound(s)); 1782 return !isSubtype(t, relaxBound(s));
1809 } 1783 }
1810 1784
1811 private Type relaxBound(Type t) { 1785 private Type relaxBound(Type t) {
1858 // </editor-fold> 1832 // </editor-fold>
1859 1833
1860 // <editor-fold defaultstate="collapsed" desc="Array Utils"> 1834 // <editor-fold defaultstate="collapsed" desc="Array Utils">
1861 public boolean isArray(Type t) { 1835 public boolean isArray(Type t) {
1862 while (t.hasTag(WILDCARD)) 1836 while (t.hasTag(WILDCARD))
1863 t = upperBound(t); 1837 t = wildUpperBound(t);
1864 return t.hasTag(ARRAY); 1838 return t.hasTag(ARRAY);
1865 } 1839 }
1866 1840
1867 /** 1841 /**
1868 * The element type of an array. 1842 * The element type of an array.
1869 */ 1843 */
1870 public Type elemtype(Type t) { 1844 public Type elemtype(Type t) {
1871 switch (t.getTag()) { 1845 switch (t.getTag()) {
1872 case WILDCARD: 1846 case WILDCARD:
1873 return elemtype(upperBound(t)); 1847 return elemtype(wildUpperBound(t));
1874 case ARRAY: 1848 case ARRAY:
1875 t = t.unannotatedType(); 1849 t = t.unannotatedType();
1876 return ((ArrayType)t).elemtype; 1850 return ((ArrayType)t).elemtype;
1877 case FORALL: 1851 case FORALL:
1878 return elemtype(((ForAll)t).qtype); 1852 return elemtype(((ForAll)t).qtype);
2071 return sym.type; 2045 return sym.type;
2072 } 2046 }
2073 2047
2074 @Override 2048 @Override
2075 public Type visitWildcardType(WildcardType t, Symbol sym) { 2049 public Type visitWildcardType(WildcardType t, Symbol sym) {
2076 return memberType(upperBound(t), sym); 2050 return memberType(wildUpperBound(t), sym);
2077 } 2051 }
2078 2052
2079 @Override 2053 @Override
2080 public Type visitClassType(ClassType t, Symbol sym) { 2054 public Type visitClassType(ClassType t, Symbol sym) {
2081 Symbol owner = sym.owner; 2055 Symbol owner = sym.owner;
2190 return t.map(recurse ? erasureRecFun : erasureFun); 2164 return t.map(recurse ? erasureRecFun : erasureFun);
2191 } 2165 }
2192 2166
2193 @Override 2167 @Override
2194 public Type visitWildcardType(WildcardType t, Boolean recurse) { 2168 public Type visitWildcardType(WildcardType t, Boolean recurse) {
2195 return erasure(upperBound(t), recurse); 2169 return erasure(wildUpperBound(t), recurse);
2196 } 2170 }
2197 2171
2198 @Override 2172 @Override
2199 public Type visitClassType(ClassType t, Boolean recurse) { 2173 public Type visitClassType(ClassType t, Boolean recurse) {
2200 Type erased = t.tsym.erasure(Types.this); 2174 Type erased = t.tsym.erasure(Types.this);
2399 List<Type> actuals = t.allparams(); 2373 List<Type> actuals = t.allparams();
2400 List<Type> formals = t.tsym.type.allparams(); 2374 List<Type> formals = t.tsym.type.allparams();
2401 if (t.hasErasedSupertypes()) { 2375 if (t.hasErasedSupertypes()) {
2402 t.interfaces_field = erasureRecursive(interfaces); 2376 t.interfaces_field = erasureRecursive(interfaces);
2403 } else if (formals.nonEmpty()) { 2377 } else if (formals.nonEmpty()) {
2404 t.interfaces_field = 2378 t.interfaces_field = subst(interfaces, formals, actuals);
2405 upperBounds(subst(interfaces, formals, actuals));
2406 } 2379 }
2407 else { 2380 else {
2408 t.interfaces_field = interfaces; 2381 t.interfaces_field = interfaces;
2409 } 2382 }
2410 } 2383 }
2969 return t; 2942 return t;
2970 else 2943 else
2971 return new ClassType(outer1, typarams1, t.tsym); 2944 return new ClassType(outer1, typarams1, t.tsym);
2972 } else { 2945 } else {
2973 Type st = subst(supertype(t)); 2946 Type st = subst(supertype(t));
2974 List<Type> is = upperBounds(subst(interfaces(t))); 2947 List<Type> is = subst(interfaces(t));
2975 if (st == supertype(t) && is == interfaces(t)) 2948 if (st == supertype(t) && is == interfaces(t))
2976 return t; 2949 return t;
2977 else 2950 else
2978 return makeCompoundType(is.prepend(st)); 2951 return makeCompoundType(is.prepend(st));
2979 } 2952 }
2986 bound = subst(bound); 2959 bound = subst(bound);
2987 if (bound == t.type) { 2960 if (bound == t.type) {
2988 return t; 2961 return t;
2989 } else { 2962 } else {
2990 if (t.isExtendsBound() && bound.isExtendsBound()) 2963 if (t.isExtendsBound() && bound.isExtendsBound())
2991 bound = upperBound(bound); 2964 bound = wildUpperBound(bound);
2992 return new WildcardType(bound, t.kind, syms.boundClass, t.bound); 2965 return new WildcardType(bound, t.kind, syms.boundClass, t.bound);
2993 } 2966 }
2994 } 2967 }
2995 2968
2996 @Override 2969 @Override
3436 merged.append(act2.head); 3409 merged.append(act2.head);
3437 } else { 3410 } else {
3438 TypePair pair = new TypePair(c1, c2); 3411 TypePair pair = new TypePair(c1, c2);
3439 Type m; 3412 Type m;
3440 if (mergeCache.add(pair)) { 3413 if (mergeCache.add(pair)) {
3441 m = new WildcardType(lub(upperBound(act1.head), 3414 m = new WildcardType(lub(wildUpperBound(act1.head),
3442 upperBound(act2.head)), 3415 wildUpperBound(act2.head)),
3443 BoundKind.EXTENDS, 3416 BoundKind.EXTENDS,
3444 syms.boundClass); 3417 syms.boundClass);
3445 mergeCache.remove(pair); 3418 mergeCache.remove(pair);
3446 } else { 3419 } else {
3447 m = new WildcardType(syms.objectType, 3420 m = new WildcardType(syms.objectType,
4013 return result.toList(); 3986 return result.toList();
4014 } 3987 }
4015 // </editor-fold> 3988 // </editor-fold>
4016 3989
4017 // <editor-fold defaultstate="collapsed" desc="Internal utility methods"> 3990 // <editor-fold defaultstate="collapsed" desc="Internal utility methods">
4018 private List<Type> upperBounds(List<Type> ss) {
4019 if (ss.isEmpty()) return ss;
4020 Type head = upperBound(ss.head);
4021 List<Type> tail = upperBounds(ss.tail);
4022 if (head != ss.head || tail != ss.tail)
4023 return tail.prepend(head);
4024 else
4025 return ss;
4026 }
4027
4028 private boolean sideCast(Type from, Type to, Warner warn) { 3991 private boolean sideCast(Type from, Type to, Warner warn) {
4029 // We are casting from type $from$ to type $to$, which are 3992 // We are casting from type $from$ to type $to$, which are
4030 // non-final unrelated types. This method 3993 // non-final unrelated types. This method
4031 // tries to reject a cast by transferring type parameters 3994 // tries to reject a cast by transferring type parameters
4032 // from $to$ to $from$ by common superinterfaces. 3995 // from $to$ to $from$ by common superinterfaces.
4179 } 4142 }
4180 4143
4181 @Override 4144 @Override
4182 public Void visitWildcardType(WildcardType source, Type target) throws AdaptFailure { 4145 public Void visitWildcardType(WildcardType source, Type target) throws AdaptFailure {
4183 if (source.isExtendsBound()) 4146 if (source.isExtendsBound())
4184 adaptRecursive(upperBound(source), upperBound(target)); 4147 adaptRecursive(wildUpperBound(source), wildUpperBound(target));
4185 else if (source.isSuperBound()) 4148 else if (source.isSuperBound())
4186 adaptRecursive(wildLowerBound(source), wildLowerBound(target)); 4149 adaptRecursive(wildLowerBound(source), wildLowerBound(target));
4187 return null; 4150 return null;
4188 } 4151 }
4189 4152
4196 if (val != null) { 4159 if (val != null) {
4197 if (val.isSuperBound() && target.isSuperBound()) { 4160 if (val.isSuperBound() && target.isSuperBound()) {
4198 val = isSubtype(wildLowerBound(val), wildLowerBound(target)) 4161 val = isSubtype(wildLowerBound(val), wildLowerBound(target))
4199 ? target : val; 4162 ? target : val;
4200 } else if (val.isExtendsBound() && target.isExtendsBound()) { 4163 } else if (val.isExtendsBound() && target.isExtendsBound()) {
4201 val = isSubtype(upperBound(val), upperBound(target)) 4164 val = isSubtype(wildUpperBound(val), wildUpperBound(target))
4202 ? val : target; 4165 ? val : target;
4203 } else if (!isSameType(val, target)) { 4166 } else if (!isSameType(val, target)) {
4204 throw new AdaptFailure(); 4167 throw new AdaptFailure();
4205 } 4168 }
4206 } else { 4169 } else {
4307 else 4270 else
4308 return t; 4271 return t;
4309 } 4272 }
4310 4273
4311 public Type visitType(Type t, Void s) { 4274 public Type visitType(Type t, Void s) {
4312 return high ? upperBound(t) : t; 4275 return t;
4313 } 4276 }
4314 4277
4315 @Override 4278 @Override
4316 public Type visitCapturedType(CapturedType t, Void s) { 4279 public Type visitCapturedType(CapturedType t, Void s) {
4317 Type w_bound = t.wildcard.type; 4280 Type w_bound = t.wildcard.type;

mercurial