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

Tue, 25 Sep 2012 11:56:46 +0100

author
mcimadamore
date
Tue, 25 Sep 2012 11:56:46 +0100
changeset 1338
ad2ca2a4ab5e
parent 1313
873ddd9f4900
child 1347
1408af4cd8b0
permissions
-rw-r--r--

7177306: Regression: unchecked method call does not erase return type
Summary: Spurious extra call to Attr.checkMethod when method call is unchecked
Reviewed-by: jjg, dlsmith

duke@1 1 /*
mcimadamore@1177 2 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.code;
duke@1 27
mcimadamore@341 28 import java.lang.ref.SoftReference;
duke@1 29 import java.util.*;
duke@1 30
duke@1 31 import com.sun.tools.javac.util.*;
duke@1 32 import com.sun.tools.javac.util.List;
duke@1 33
duke@1 34 import com.sun.tools.javac.jvm.ClassReader;
jjg@657 35 import com.sun.tools.javac.code.Attribute.RetentionPolicy;
mcimadamore@795 36 import com.sun.tools.javac.code.Lint.LintCategory;
mcimadamore@1338 37 import com.sun.tools.javac.code.Type.UndetVar.InferenceBound;
duke@1 38 import com.sun.tools.javac.comp.Check;
duke@1 39
mcimadamore@858 40 import static com.sun.tools.javac.code.Scope.*;
duke@1 41 import static com.sun.tools.javac.code.Type.*;
duke@1 42 import static com.sun.tools.javac.code.TypeTags.*;
duke@1 43 import static com.sun.tools.javac.code.Symbol.*;
duke@1 44 import static com.sun.tools.javac.code.Flags.*;
duke@1 45 import static com.sun.tools.javac.code.BoundKind.*;
duke@1 46 import static com.sun.tools.javac.util.ListBuffer.lb;
duke@1 47
duke@1 48 /**
duke@1 49 * Utility class containing various operations on types.
duke@1 50 *
duke@1 51 * <p>Unless other names are more illustrative, the following naming
duke@1 52 * conventions should be observed in this file:
duke@1 53 *
duke@1 54 * <dl>
duke@1 55 * <dt>t</dt>
duke@1 56 * <dd>If the first argument to an operation is a type, it should be named t.</dd>
duke@1 57 * <dt>s</dt>
duke@1 58 * <dd>Similarly, if the second argument to an operation is a type, it should be named s.</dd>
duke@1 59 * <dt>ts</dt>
duke@1 60 * <dd>If an operations takes a list of types, the first should be named ts.</dd>
duke@1 61 * <dt>ss</dt>
duke@1 62 * <dd>A second list of types should be named ss.</dd>
duke@1 63 * </dl>
duke@1 64 *
jjg@581 65 * <p><b>This is NOT part of any supported API.
duke@1 66 * If you write code that depends on this, you do so at your own risk.
duke@1 67 * This code and its internal interfaces are subject to change or
duke@1 68 * deletion without notice.</b>
duke@1 69 */
duke@1 70 public class Types {
duke@1 71 protected static final Context.Key<Types> typesKey =
duke@1 72 new Context.Key<Types>();
duke@1 73
duke@1 74 final Symtab syms;
mcimadamore@136 75 final JavacMessages messages;
jjg@113 76 final Names names;
duke@1 77 final boolean allowBoxing;
jjg@984 78 final boolean allowCovariantReturns;
jjg@984 79 final boolean allowObjectToPrimitiveCast;
duke@1 80 final ClassReader reader;
duke@1 81 final Check chk;
duke@1 82 List<Warner> warnStack = List.nil();
duke@1 83 final Name capturedName;
duke@1 84
duke@1 85 // <editor-fold defaultstate="collapsed" desc="Instantiating">
duke@1 86 public static Types instance(Context context) {
duke@1 87 Types instance = context.get(typesKey);
duke@1 88 if (instance == null)
duke@1 89 instance = new Types(context);
duke@1 90 return instance;
duke@1 91 }
duke@1 92
duke@1 93 protected Types(Context context) {
duke@1 94 context.put(typesKey, this);
duke@1 95 syms = Symtab.instance(context);
jjg@113 96 names = Names.instance(context);
jjg@984 97 Source source = Source.instance(context);
jjg@984 98 allowBoxing = source.allowBoxing();
jjg@984 99 allowCovariantReturns = source.allowCovariantReturns();
jjg@984 100 allowObjectToPrimitiveCast = source.allowObjectToPrimitiveCast();
duke@1 101 reader = ClassReader.instance(context);
duke@1 102 chk = Check.instance(context);
duke@1 103 capturedName = names.fromString("<captured wildcard>");
mcimadamore@136 104 messages = JavacMessages.instance(context);
duke@1 105 }
duke@1 106 // </editor-fold>
duke@1 107
duke@1 108 // <editor-fold defaultstate="collapsed" desc="upperBound">
duke@1 109 /**
duke@1 110 * The "rvalue conversion".<br>
duke@1 111 * The upper bound of most types is the type
duke@1 112 * itself. Wildcards, on the other hand have upper
duke@1 113 * and lower bounds.
duke@1 114 * @param t a type
duke@1 115 * @return the upper bound of the given type
duke@1 116 */
duke@1 117 public Type upperBound(Type t) {
duke@1 118 return upperBound.visit(t);
duke@1 119 }
duke@1 120 // where
duke@1 121 private final MapVisitor<Void> upperBound = new MapVisitor<Void>() {
duke@1 122
duke@1 123 @Override
duke@1 124 public Type visitWildcardType(WildcardType t, Void ignored) {
duke@1 125 if (t.isSuperBound())
duke@1 126 return t.bound == null ? syms.objectType : t.bound.bound;
duke@1 127 else
duke@1 128 return visit(t.type);
duke@1 129 }
duke@1 130
duke@1 131 @Override
duke@1 132 public Type visitCapturedType(CapturedType t, Void ignored) {
duke@1 133 return visit(t.bound);
duke@1 134 }
duke@1 135 };
duke@1 136 // </editor-fold>
duke@1 137
duke@1 138 // <editor-fold defaultstate="collapsed" desc="lowerBound">
duke@1 139 /**
duke@1 140 * The "lvalue conversion".<br>
duke@1 141 * The lower bound of most types is the type
duke@1 142 * itself. Wildcards, on the other hand have upper
duke@1 143 * and lower bounds.
duke@1 144 * @param t a type
duke@1 145 * @return the lower bound of the given type
duke@1 146 */
duke@1 147 public Type lowerBound(Type t) {
duke@1 148 return lowerBound.visit(t);
duke@1 149 }
duke@1 150 // where
duke@1 151 private final MapVisitor<Void> lowerBound = new MapVisitor<Void>() {
duke@1 152
duke@1 153 @Override
duke@1 154 public Type visitWildcardType(WildcardType t, Void ignored) {
duke@1 155 return t.isExtendsBound() ? syms.botType : visit(t.type);
duke@1 156 }
duke@1 157
duke@1 158 @Override
duke@1 159 public Type visitCapturedType(CapturedType t, Void ignored) {
duke@1 160 return visit(t.getLowerBound());
duke@1 161 }
duke@1 162 };
duke@1 163 // </editor-fold>
duke@1 164
duke@1 165 // <editor-fold defaultstate="collapsed" desc="isUnbounded">
duke@1 166 /**
duke@1 167 * Checks that all the arguments to a class are unbounded
duke@1 168 * wildcards or something else that doesn't make any restrictions
duke@1 169 * on the arguments. If a class isUnbounded, a raw super- or
duke@1 170 * subclass can be cast to it without a warning.
duke@1 171 * @param t a type
duke@1 172 * @return true iff the given type is unbounded or raw
duke@1 173 */
duke@1 174 public boolean isUnbounded(Type t) {
duke@1 175 return isUnbounded.visit(t);
duke@1 176 }
duke@1 177 // where
duke@1 178 private final UnaryVisitor<Boolean> isUnbounded = new UnaryVisitor<Boolean>() {
duke@1 179
duke@1 180 public Boolean visitType(Type t, Void ignored) {
duke@1 181 return true;
duke@1 182 }
duke@1 183
duke@1 184 @Override
duke@1 185 public Boolean visitClassType(ClassType t, Void ignored) {
duke@1 186 List<Type> parms = t.tsym.type.allparams();
duke@1 187 List<Type> args = t.allparams();
duke@1 188 while (parms.nonEmpty()) {
duke@1 189 WildcardType unb = new WildcardType(syms.objectType,
duke@1 190 BoundKind.UNBOUND,
duke@1 191 syms.boundClass,
duke@1 192 (TypeVar)parms.head);
duke@1 193 if (!containsType(args.head, unb))
duke@1 194 return false;
duke@1 195 parms = parms.tail;
duke@1 196 args = args.tail;
duke@1 197 }
duke@1 198 return true;
duke@1 199 }
duke@1 200 };
duke@1 201 // </editor-fold>
duke@1 202
duke@1 203 // <editor-fold defaultstate="collapsed" desc="asSub">
duke@1 204 /**
duke@1 205 * Return the least specific subtype of t that starts with symbol
duke@1 206 * sym. If none exists, return null. The least specific subtype
duke@1 207 * is determined as follows:
duke@1 208 *
duke@1 209 * <p>If there is exactly one parameterized instance of sym that is a
duke@1 210 * subtype of t, that parameterized instance is returned.<br>
duke@1 211 * Otherwise, if the plain type or raw type `sym' is a subtype of
duke@1 212 * type t, the type `sym' itself is returned. Otherwise, null is
duke@1 213 * returned.
duke@1 214 */
duke@1 215 public Type asSub(Type t, Symbol sym) {
duke@1 216 return asSub.visit(t, sym);
duke@1 217 }
duke@1 218 // where
duke@1 219 private final SimpleVisitor<Type,Symbol> asSub = new SimpleVisitor<Type,Symbol>() {
duke@1 220
duke@1 221 public Type visitType(Type t, Symbol sym) {
duke@1 222 return null;
duke@1 223 }
duke@1 224
duke@1 225 @Override
duke@1 226 public Type visitClassType(ClassType t, Symbol sym) {
duke@1 227 if (t.tsym == sym)
duke@1 228 return t;
duke@1 229 Type base = asSuper(sym.type, t.tsym);
duke@1 230 if (base == null)
duke@1 231 return null;
duke@1 232 ListBuffer<Type> from = new ListBuffer<Type>();
duke@1 233 ListBuffer<Type> to = new ListBuffer<Type>();
duke@1 234 try {
duke@1 235 adapt(base, t, from, to);
duke@1 236 } catch (AdaptFailure ex) {
duke@1 237 return null;
duke@1 238 }
duke@1 239 Type res = subst(sym.type, from.toList(), to.toList());
duke@1 240 if (!isSubtype(res, t))
duke@1 241 return null;
duke@1 242 ListBuffer<Type> openVars = new ListBuffer<Type>();
duke@1 243 for (List<Type> l = sym.type.allparams();
duke@1 244 l.nonEmpty(); l = l.tail)
duke@1 245 if (res.contains(l.head) && !t.contains(l.head))
duke@1 246 openVars.append(l.head);
duke@1 247 if (openVars.nonEmpty()) {
duke@1 248 if (t.isRaw()) {
duke@1 249 // The subtype of a raw type is raw
duke@1 250 res = erasure(res);
duke@1 251 } else {
duke@1 252 // Unbound type arguments default to ?
duke@1 253 List<Type> opens = openVars.toList();
duke@1 254 ListBuffer<Type> qs = new ListBuffer<Type>();
duke@1 255 for (List<Type> iter = opens; iter.nonEmpty(); iter = iter.tail) {
duke@1 256 qs.append(new WildcardType(syms.objectType, BoundKind.UNBOUND, syms.boundClass, (TypeVar) iter.head));
duke@1 257 }
duke@1 258 res = subst(res, opens, qs.toList());
duke@1 259 }
duke@1 260 }
duke@1 261 return res;
duke@1 262 }
duke@1 263
duke@1 264 @Override
duke@1 265 public Type visitErrorType(ErrorType t, Symbol sym) {
duke@1 266 return t;
duke@1 267 }
duke@1 268 };
duke@1 269 // </editor-fold>
duke@1 270
duke@1 271 // <editor-fold defaultstate="collapsed" desc="isConvertible">
duke@1 272 /**
mcimadamore@1071 273 * Is t a subtype of or convertible via boxing/unboxing
mcimadamore@1071 274 * conversion to s?
duke@1 275 */
duke@1 276 public boolean isConvertible(Type t, Type s, Warner warn) {
mcimadamore@1071 277 if (t.tag == ERROR)
mcimadamore@1071 278 return true;
duke@1 279 boolean tPrimitive = t.isPrimitive();
duke@1 280 boolean sPrimitive = s.isPrimitive();
mcimadamore@795 281 if (tPrimitive == sPrimitive) {
duke@1 282 return isSubtypeUnchecked(t, s, warn);
mcimadamore@795 283 }
duke@1 284 if (!allowBoxing) return false;
duke@1 285 return tPrimitive
duke@1 286 ? isSubtype(boxedClass(t).type, s)
duke@1 287 : isSubtype(unboxedType(t), s);
duke@1 288 }
duke@1 289
duke@1 290 /**
duke@1 291 * Is t a subtype of or convertiable via boxing/unboxing
duke@1 292 * convertions to s?
duke@1 293 */
duke@1 294 public boolean isConvertible(Type t, Type s) {
duke@1 295 return isConvertible(t, s, Warner.noWarnings);
duke@1 296 }
duke@1 297 // </editor-fold>
duke@1 298
duke@1 299 // <editor-fold defaultstate="collapsed" desc="isSubtype">
duke@1 300 /**
duke@1 301 * Is t an unchecked subtype of s?
duke@1 302 */
duke@1 303 public boolean isSubtypeUnchecked(Type t, Type s) {
duke@1 304 return isSubtypeUnchecked(t, s, Warner.noWarnings);
duke@1 305 }
duke@1 306 /**
duke@1 307 * Is t an unchecked subtype of s?
duke@1 308 */
duke@1 309 public boolean isSubtypeUnchecked(Type t, Type s, Warner warn) {
mcimadamore@1108 310 boolean result = isSubtypeUncheckedInternal(t, s, warn);
mcimadamore@1108 311 if (result) {
mcimadamore@1108 312 checkUnsafeVarargsConversion(t, s, warn);
mcimadamore@1108 313 }
mcimadamore@1108 314 return result;
mcimadamore@1108 315 }
mcimadamore@1108 316 //where
mcimadamore@1108 317 private boolean isSubtypeUncheckedInternal(Type t, Type s, Warner warn) {
mcimadamore@1108 318 if (t.tag == ARRAY && s.tag == ARRAY) {
mcimadamore@1108 319 if (((ArrayType)t).elemtype.tag <= lastBaseTag) {
mcimadamore@1108 320 return isSameType(elemtype(t), elemtype(s));
mcimadamore@1108 321 } else {
mcimadamore@1108 322 return isSubtypeUnchecked(elemtype(t), elemtype(s), warn);
mcimadamore@795 323 }
mcimadamore@1108 324 } else if (isSubtype(t, s)) {
duke@1 325 return true;
duke@1 326 }
mcimadamore@1108 327 else if (t.tag == TYPEVAR) {
mcimadamore@1108 328 return isSubtypeUnchecked(t.getUpperBound(), s, warn);
mcimadamore@1108 329 }
mcimadamore@1108 330 else if (!s.isRaw()) {
mcimadamore@1108 331 Type t2 = asSuper(t, s.tsym);
mcimadamore@1108 332 if (t2 != null && t2.isRaw()) {
mcimadamore@1108 333 if (isReifiable(s))
mcimadamore@1108 334 warn.silentWarn(LintCategory.UNCHECKED);
mcimadamore@1108 335 else
mcimadamore@1108 336 warn.warn(LintCategory.UNCHECKED);
mcimadamore@1108 337 return true;
mcimadamore@1108 338 }
mcimadamore@1108 339 }
mcimadamore@1108 340 return false;
duke@1 341 }
mcimadamore@1108 342
mcimadamore@1108 343 private void checkUnsafeVarargsConversion(Type t, Type s, Warner warn) {
mcimadamore@1108 344 if (t.tag != ARRAY || isReifiable(t)) return;
mcimadamore@1108 345 ArrayType from = (ArrayType)t;
mcimadamore@1108 346 boolean shouldWarn = false;
mcimadamore@1108 347 switch (s.tag) {
mcimadamore@1108 348 case ARRAY:
mcimadamore@1108 349 ArrayType to = (ArrayType)s;
mcimadamore@1108 350 shouldWarn = from.isVarargs() &&
mcimadamore@1108 351 !to.isVarargs() &&
mcimadamore@1108 352 !isReifiable(from);
mcimadamore@1108 353 break;
mcimadamore@1108 354 case CLASS:
mcimadamore@1108 355 shouldWarn = from.isVarargs();
mcimadamore@1108 356 break;
mcimadamore@1108 357 }
mcimadamore@1108 358 if (shouldWarn) {
mcimadamore@1108 359 warn.warn(LintCategory.VARARGS);
mcimadamore@1108 360 }
mcimadamore@1108 361 }
duke@1 362
duke@1 363 /**
duke@1 364 * Is t a subtype of s?<br>
duke@1 365 * (not defined for Method and ForAll types)
duke@1 366 */
duke@1 367 final public boolean isSubtype(Type t, Type s) {
duke@1 368 return isSubtype(t, s, true);
duke@1 369 }
duke@1 370 final public boolean isSubtypeNoCapture(Type t, Type s) {
duke@1 371 return isSubtype(t, s, false);
duke@1 372 }
duke@1 373 public boolean isSubtype(Type t, Type s, boolean capture) {
duke@1 374 if (t == s)
duke@1 375 return true;
duke@1 376
duke@1 377 if (s.tag >= firstPartialTag)
duke@1 378 return isSuperType(s, t);
duke@1 379
mcimadamore@299 380 if (s.isCompound()) {
mcimadamore@299 381 for (Type s2 : interfaces(s).prepend(supertype(s))) {
mcimadamore@299 382 if (!isSubtype(t, s2, capture))
mcimadamore@299 383 return false;
mcimadamore@299 384 }
mcimadamore@299 385 return true;
mcimadamore@299 386 }
mcimadamore@299 387
duke@1 388 Type lower = lowerBound(s);
duke@1 389 if (s != lower)
duke@1 390 return isSubtype(capture ? capture(t) : t, lower, false);
duke@1 391
duke@1 392 return isSubtype.visit(capture ? capture(t) : t, s);
duke@1 393 }
duke@1 394 // where
duke@1 395 private TypeRelation isSubtype = new TypeRelation()
duke@1 396 {
duke@1 397 public Boolean visitType(Type t, Type s) {
duke@1 398 switch (t.tag) {
duke@1 399 case BYTE: case CHAR:
duke@1 400 return (t.tag == s.tag ||
duke@1 401 t.tag + 2 <= s.tag && s.tag <= DOUBLE);
duke@1 402 case SHORT: case INT: case LONG: case FLOAT: case DOUBLE:
duke@1 403 return t.tag <= s.tag && s.tag <= DOUBLE;
duke@1 404 case BOOLEAN: case VOID:
duke@1 405 return t.tag == s.tag;
duke@1 406 case TYPEVAR:
duke@1 407 return isSubtypeNoCapture(t.getUpperBound(), s);
duke@1 408 case BOT:
duke@1 409 return
duke@1 410 s.tag == BOT || s.tag == CLASS ||
duke@1 411 s.tag == ARRAY || s.tag == TYPEVAR;
mcimadamore@991 412 case WILDCARD: //we shouldn't be here - avoids crash (see 7034495)
duke@1 413 case NONE:
duke@1 414 return false;
duke@1 415 default:
duke@1 416 throw new AssertionError("isSubtype " + t.tag);
duke@1 417 }
duke@1 418 }
duke@1 419
duke@1 420 private Set<TypePair> cache = new HashSet<TypePair>();
duke@1 421
duke@1 422 private boolean containsTypeRecursive(Type t, Type s) {
duke@1 423 TypePair pair = new TypePair(t, s);
duke@1 424 if (cache.add(pair)) {
duke@1 425 try {
duke@1 426 return containsType(t.getTypeArguments(),
duke@1 427 s.getTypeArguments());
duke@1 428 } finally {
duke@1 429 cache.remove(pair);
duke@1 430 }
duke@1 431 } else {
duke@1 432 return containsType(t.getTypeArguments(),
duke@1 433 rewriteSupers(s).getTypeArguments());
duke@1 434 }
duke@1 435 }
duke@1 436
duke@1 437 private Type rewriteSupers(Type t) {
duke@1 438 if (!t.isParameterized())
duke@1 439 return t;
duke@1 440 ListBuffer<Type> from = lb();
duke@1 441 ListBuffer<Type> to = lb();
duke@1 442 adaptSelf(t, from, to);
duke@1 443 if (from.isEmpty())
duke@1 444 return t;
duke@1 445 ListBuffer<Type> rewrite = lb();
duke@1 446 boolean changed = false;
duke@1 447 for (Type orig : to.toList()) {
duke@1 448 Type s = rewriteSupers(orig);
duke@1 449 if (s.isSuperBound() && !s.isExtendsBound()) {
duke@1 450 s = new WildcardType(syms.objectType,
duke@1 451 BoundKind.UNBOUND,
duke@1 452 syms.boundClass);
duke@1 453 changed = true;
duke@1 454 } else if (s != orig) {
duke@1 455 s = new WildcardType(upperBound(s),
duke@1 456 BoundKind.EXTENDS,
duke@1 457 syms.boundClass);
duke@1 458 changed = true;
duke@1 459 }
duke@1 460 rewrite.append(s);
duke@1 461 }
duke@1 462 if (changed)
duke@1 463 return subst(t.tsym.type, from.toList(), rewrite.toList());
duke@1 464 else
duke@1 465 return t;
duke@1 466 }
duke@1 467
duke@1 468 @Override
duke@1 469 public Boolean visitClassType(ClassType t, Type s) {
duke@1 470 Type sup = asSuper(t, s.tsym);
duke@1 471 return sup != null
duke@1 472 && sup.tsym == s.tsym
duke@1 473 // You're not allowed to write
duke@1 474 // Vector<Object> vec = new Vector<String>();
duke@1 475 // But with wildcards you can write
duke@1 476 // Vector<? extends Object> vec = new Vector<String>();
duke@1 477 // which means that subtype checking must be done
duke@1 478 // here instead of same-type checking (via containsType).
duke@1 479 && (!s.isParameterized() || containsTypeRecursive(s, sup))
duke@1 480 && isSubtypeNoCapture(sup.getEnclosingType(),
duke@1 481 s.getEnclosingType());
duke@1 482 }
duke@1 483
duke@1 484 @Override
duke@1 485 public Boolean visitArrayType(ArrayType t, Type s) {
duke@1 486 if (s.tag == ARRAY) {
duke@1 487 if (t.elemtype.tag <= lastBaseTag)
duke@1 488 return isSameType(t.elemtype, elemtype(s));
duke@1 489 else
duke@1 490 return isSubtypeNoCapture(t.elemtype, elemtype(s));
duke@1 491 }
duke@1 492
duke@1 493 if (s.tag == CLASS) {
duke@1 494 Name sname = s.tsym.getQualifiedName();
duke@1 495 return sname == names.java_lang_Object
duke@1 496 || sname == names.java_lang_Cloneable
duke@1 497 || sname == names.java_io_Serializable;
duke@1 498 }
duke@1 499
duke@1 500 return false;
duke@1 501 }
duke@1 502
duke@1 503 @Override
duke@1 504 public Boolean visitUndetVar(UndetVar t, Type s) {
duke@1 505 //todo: test against origin needed? or replace with substitution?
mcimadamore@1093 506 if (t == s || t.qtype == s || s.tag == ERROR || s.tag == UNKNOWN) {
duke@1 507 return true;
mcimadamore@1093 508 } else if (s.tag == BOT) {
mcimadamore@1093 509 //if 's' is 'null' there's no instantiated type U for which
mcimadamore@1093 510 //U <: s (but 'null' itself, which is not a valid type)
mcimadamore@1093 511 return false;
mcimadamore@1093 512 }
duke@1 513
mcimadamore@1338 514 t.addBound(InferenceBound.UPPER, s, Types.this);
duke@1 515 return true;
duke@1 516 }
duke@1 517
duke@1 518 @Override
duke@1 519 public Boolean visitErrorType(ErrorType t, Type s) {
duke@1 520 return true;
duke@1 521 }
duke@1 522 };
duke@1 523
duke@1 524 /**
duke@1 525 * Is t a subtype of every type in given list `ts'?<br>
duke@1 526 * (not defined for Method and ForAll types)<br>
duke@1 527 * Allows unchecked conversions.
duke@1 528 */
duke@1 529 public boolean isSubtypeUnchecked(Type t, List<Type> ts, Warner warn) {
duke@1 530 for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
duke@1 531 if (!isSubtypeUnchecked(t, l.head, warn))
duke@1 532 return false;
duke@1 533 return true;
duke@1 534 }
duke@1 535
duke@1 536 /**
duke@1 537 * Are corresponding elements of ts subtypes of ss? If lists are
duke@1 538 * of different length, return false.
duke@1 539 */
duke@1 540 public boolean isSubtypes(List<Type> ts, List<Type> ss) {
duke@1 541 while (ts.tail != null && ss.tail != null
duke@1 542 /*inlined: ts.nonEmpty() && ss.nonEmpty()*/ &&
duke@1 543 isSubtype(ts.head, ss.head)) {
duke@1 544 ts = ts.tail;
duke@1 545 ss = ss.tail;
duke@1 546 }
duke@1 547 return ts.tail == null && ss.tail == null;
duke@1 548 /*inlined: ts.isEmpty() && ss.isEmpty();*/
duke@1 549 }
duke@1 550
duke@1 551 /**
duke@1 552 * Are corresponding elements of ts subtypes of ss, allowing
duke@1 553 * unchecked conversions? If lists are of different length,
duke@1 554 * return false.
duke@1 555 **/
duke@1 556 public boolean isSubtypesUnchecked(List<Type> ts, List<Type> ss, Warner warn) {
duke@1 557 while (ts.tail != null && ss.tail != null
duke@1 558 /*inlined: ts.nonEmpty() && ss.nonEmpty()*/ &&
duke@1 559 isSubtypeUnchecked(ts.head, ss.head, warn)) {
duke@1 560 ts = ts.tail;
duke@1 561 ss = ss.tail;
duke@1 562 }
duke@1 563 return ts.tail == null && ss.tail == null;
duke@1 564 /*inlined: ts.isEmpty() && ss.isEmpty();*/
duke@1 565 }
duke@1 566 // </editor-fold>
duke@1 567
duke@1 568 // <editor-fold defaultstate="collapsed" desc="isSuperType">
duke@1 569 /**
duke@1 570 * Is t a supertype of s?
duke@1 571 */
duke@1 572 public boolean isSuperType(Type t, Type s) {
duke@1 573 switch (t.tag) {
duke@1 574 case ERROR:
duke@1 575 return true;
duke@1 576 case UNDETVAR: {
duke@1 577 UndetVar undet = (UndetVar)t;
duke@1 578 if (t == s ||
duke@1 579 undet.qtype == s ||
duke@1 580 s.tag == ERROR ||
duke@1 581 s.tag == BOT) return true;
mcimadamore@1338 582 undet.addBound(InferenceBound.LOWER, s, this);
duke@1 583 return true;
duke@1 584 }
duke@1 585 default:
duke@1 586 return isSubtype(s, t);
duke@1 587 }
duke@1 588 }
duke@1 589 // </editor-fold>
duke@1 590
duke@1 591 // <editor-fold defaultstate="collapsed" desc="isSameType">
duke@1 592 /**
duke@1 593 * Are corresponding elements of the lists the same type? If
duke@1 594 * lists are of different length, return false.
duke@1 595 */
duke@1 596 public boolean isSameTypes(List<Type> ts, List<Type> ss) {
duke@1 597 while (ts.tail != null && ss.tail != null
duke@1 598 /*inlined: ts.nonEmpty() && ss.nonEmpty()*/ &&
duke@1 599 isSameType(ts.head, ss.head)) {
duke@1 600 ts = ts.tail;
duke@1 601 ss = ss.tail;
duke@1 602 }
duke@1 603 return ts.tail == null && ss.tail == null;
duke@1 604 /*inlined: ts.isEmpty() && ss.isEmpty();*/
duke@1 605 }
duke@1 606
duke@1 607 /**
duke@1 608 * Is t the same type as s?
duke@1 609 */
duke@1 610 public boolean isSameType(Type t, Type s) {
duke@1 611 return isSameType.visit(t, s);
duke@1 612 }
duke@1 613 // where
duke@1 614 private TypeRelation isSameType = new TypeRelation() {
duke@1 615
duke@1 616 public Boolean visitType(Type t, Type s) {
duke@1 617 if (t == s)
duke@1 618 return true;
duke@1 619
duke@1 620 if (s.tag >= firstPartialTag)
duke@1 621 return visit(s, t);
duke@1 622
duke@1 623 switch (t.tag) {
duke@1 624 case BYTE: case CHAR: case SHORT: case INT: case LONG: case FLOAT:
duke@1 625 case DOUBLE: case BOOLEAN: case VOID: case BOT: case NONE:
duke@1 626 return t.tag == s.tag;
mcimadamore@561 627 case TYPEVAR: {
mcimadamore@561 628 if (s.tag == TYPEVAR) {
mcimadamore@561 629 //type-substitution does not preserve type-var types
mcimadamore@561 630 //check that type var symbols and bounds are indeed the same
mcimadamore@561 631 return t.tsym == s.tsym &&
mcimadamore@561 632 visit(t.getUpperBound(), s.getUpperBound());
mcimadamore@561 633 }
mcimadamore@561 634 else {
mcimadamore@561 635 //special case for s == ? super X, where upper(s) = u
mcimadamore@561 636 //check that u == t, where u has been set by Type.withTypeVar
mcimadamore@561 637 return s.isSuperBound() &&
mcimadamore@561 638 !s.isExtendsBound() &&
mcimadamore@561 639 visit(t, upperBound(s));
mcimadamore@561 640 }
mcimadamore@561 641 }
duke@1 642 default:
duke@1 643 throw new AssertionError("isSameType " + t.tag);
duke@1 644 }
duke@1 645 }
duke@1 646
duke@1 647 @Override
duke@1 648 public Boolean visitWildcardType(WildcardType t, Type s) {
duke@1 649 if (s.tag >= firstPartialTag)
duke@1 650 return visit(s, t);
duke@1 651 else
duke@1 652 return false;
duke@1 653 }
duke@1 654
duke@1 655 @Override
duke@1 656 public Boolean visitClassType(ClassType t, Type s) {
duke@1 657 if (t == s)
duke@1 658 return true;
duke@1 659
duke@1 660 if (s.tag >= firstPartialTag)
duke@1 661 return visit(s, t);
duke@1 662
duke@1 663 if (s.isSuperBound() && !s.isExtendsBound())
duke@1 664 return visit(t, upperBound(s)) && visit(t, lowerBound(s));
duke@1 665
duke@1 666 if (t.isCompound() && s.isCompound()) {
duke@1 667 if (!visit(supertype(t), supertype(s)))
duke@1 668 return false;
duke@1 669
duke@1 670 HashSet<SingletonType> set = new HashSet<SingletonType>();
duke@1 671 for (Type x : interfaces(t))
duke@1 672 set.add(new SingletonType(x));
duke@1 673 for (Type x : interfaces(s)) {
duke@1 674 if (!set.remove(new SingletonType(x)))
duke@1 675 return false;
duke@1 676 }
jjg@789 677 return (set.isEmpty());
duke@1 678 }
duke@1 679 return t.tsym == s.tsym
duke@1 680 && visit(t.getEnclosingType(), s.getEnclosingType())
duke@1 681 && containsTypeEquivalent(t.getTypeArguments(), s.getTypeArguments());
duke@1 682 }
duke@1 683
duke@1 684 @Override
duke@1 685 public Boolean visitArrayType(ArrayType t, Type s) {
duke@1 686 if (t == s)
duke@1 687 return true;
duke@1 688
duke@1 689 if (s.tag >= firstPartialTag)
duke@1 690 return visit(s, t);
duke@1 691
duke@1 692 return s.tag == ARRAY
duke@1 693 && containsTypeEquivalent(t.elemtype, elemtype(s));
duke@1 694 }
duke@1 695
duke@1 696 @Override
duke@1 697 public Boolean visitMethodType(MethodType t, Type s) {
duke@1 698 // isSameType for methods does not take thrown
duke@1 699 // exceptions into account!
duke@1 700 return hasSameArgs(t, s) && visit(t.getReturnType(), s.getReturnType());
duke@1 701 }
duke@1 702
duke@1 703 @Override
duke@1 704 public Boolean visitPackageType(PackageType t, Type s) {
duke@1 705 return t == s;
duke@1 706 }
duke@1 707
duke@1 708 @Override
duke@1 709 public Boolean visitForAll(ForAll t, Type s) {
duke@1 710 if (s.tag != FORALL)
duke@1 711 return false;
duke@1 712
duke@1 713 ForAll forAll = (ForAll)s;
duke@1 714 return hasSameBounds(t, forAll)
duke@1 715 && visit(t.qtype, subst(forAll.qtype, forAll.tvars, t.tvars));
duke@1 716 }
duke@1 717
duke@1 718 @Override
duke@1 719 public Boolean visitUndetVar(UndetVar t, Type s) {
duke@1 720 if (s.tag == WILDCARD)
duke@1 721 // FIXME, this might be leftovers from before capture conversion
duke@1 722 return false;
duke@1 723
duke@1 724 if (t == s || t.qtype == s || s.tag == ERROR || s.tag == UNKNOWN)
duke@1 725 return true;
duke@1 726
mcimadamore@1338 727 t.addBound(InferenceBound.EQ, s, Types.this);
mcimadamore@1251 728
duke@1 729 return true;
duke@1 730 }
duke@1 731
duke@1 732 @Override
duke@1 733 public Boolean visitErrorType(ErrorType t, Type s) {
duke@1 734 return true;
duke@1 735 }
duke@1 736 };
duke@1 737 // </editor-fold>
duke@1 738
duke@1 739 // <editor-fold defaultstate="collapsed" desc="Contains Type">
duke@1 740 public boolean containedBy(Type t, Type s) {
duke@1 741 switch (t.tag) {
duke@1 742 case UNDETVAR:
duke@1 743 if (s.tag == WILDCARD) {
duke@1 744 UndetVar undetvar = (UndetVar)t;
mcimadamore@210 745 WildcardType wt = (WildcardType)s;
mcimadamore@210 746 switch(wt.kind) {
mcimadamore@210 747 case UNBOUND: //similar to ? extends Object
mcimadamore@210 748 case EXTENDS: {
mcimadamore@210 749 Type bound = upperBound(s);
mcimadamore@1338 750 undetvar.addBound(InferenceBound.UPPER, bound, this);
mcimadamore@210 751 break;
mcimadamore@210 752 }
mcimadamore@210 753 case SUPER: {
mcimadamore@210 754 Type bound = lowerBound(s);
mcimadamore@1338 755 undetvar.addBound(InferenceBound.LOWER, bound, this);
mcimadamore@210 756 break;
mcimadamore@210 757 }
mcimadamore@162 758 }
duke@1 759 return true;
duke@1 760 } else {
duke@1 761 return isSameType(t, s);
duke@1 762 }
duke@1 763 case ERROR:
duke@1 764 return true;
duke@1 765 default:
duke@1 766 return containsType(s, t);
duke@1 767 }
duke@1 768 }
duke@1 769
duke@1 770 boolean containsType(List<Type> ts, List<Type> ss) {
duke@1 771 while (ts.nonEmpty() && ss.nonEmpty()
duke@1 772 && containsType(ts.head, ss.head)) {
duke@1 773 ts = ts.tail;
duke@1 774 ss = ss.tail;
duke@1 775 }
duke@1 776 return ts.isEmpty() && ss.isEmpty();
duke@1 777 }
duke@1 778
duke@1 779 /**
duke@1 780 * Check if t contains s.
duke@1 781 *
duke@1 782 * <p>T contains S if:
duke@1 783 *
duke@1 784 * <p>{@code L(T) <: L(S) && U(S) <: U(T)}
duke@1 785 *
duke@1 786 * <p>This relation is only used by ClassType.isSubtype(), that
duke@1 787 * is,
duke@1 788 *
duke@1 789 * <p>{@code C<S> <: C<T> if T contains S.}
duke@1 790 *
duke@1 791 * <p>Because of F-bounds, this relation can lead to infinite
duke@1 792 * recursion. Thus we must somehow break that recursion. Notice
duke@1 793 * that containsType() is only called from ClassType.isSubtype().
duke@1 794 * Since the arguments have already been checked against their
duke@1 795 * bounds, we know:
duke@1 796 *
duke@1 797 * <p>{@code U(S) <: U(T) if T is "super" bound (U(T) *is* the bound)}
duke@1 798 *
duke@1 799 * <p>{@code L(T) <: L(S) if T is "extends" bound (L(T) is bottom)}
duke@1 800 *
duke@1 801 * @param t a type
duke@1 802 * @param s a type
duke@1 803 */
duke@1 804 public boolean containsType(Type t, Type s) {
duke@1 805 return containsType.visit(t, s);
duke@1 806 }
duke@1 807 // where
duke@1 808 private TypeRelation containsType = new TypeRelation() {
duke@1 809
duke@1 810 private Type U(Type t) {
duke@1 811 while (t.tag == WILDCARD) {
duke@1 812 WildcardType w = (WildcardType)t;
duke@1 813 if (w.isSuperBound())
duke@1 814 return w.bound == null ? syms.objectType : w.bound.bound;
duke@1 815 else
duke@1 816 t = w.type;
duke@1 817 }
duke@1 818 return t;
duke@1 819 }
duke@1 820
duke@1 821 private Type L(Type t) {
duke@1 822 while (t.tag == WILDCARD) {
duke@1 823 WildcardType w = (WildcardType)t;
duke@1 824 if (w.isExtendsBound())
duke@1 825 return syms.botType;
duke@1 826 else
duke@1 827 t = w.type;
duke@1 828 }
duke@1 829 return t;
duke@1 830 }
duke@1 831
duke@1 832 public Boolean visitType(Type t, Type s) {
duke@1 833 if (s.tag >= firstPartialTag)
duke@1 834 return containedBy(s, t);
duke@1 835 else
duke@1 836 return isSameType(t, s);
duke@1 837 }
duke@1 838
jjg@789 839 // void debugContainsType(WildcardType t, Type s) {
jjg@789 840 // System.err.println();
jjg@789 841 // System.err.format(" does %s contain %s?%n", t, s);
jjg@789 842 // System.err.format(" %s U(%s) <: U(%s) %s = %s%n",
jjg@789 843 // upperBound(s), s, t, U(t),
jjg@789 844 // t.isSuperBound()
jjg@789 845 // || isSubtypeNoCapture(upperBound(s), U(t)));
jjg@789 846 // System.err.format(" %s L(%s) <: L(%s) %s = %s%n",
jjg@789 847 // L(t), t, s, lowerBound(s),
jjg@789 848 // t.isExtendsBound()
jjg@789 849 // || isSubtypeNoCapture(L(t), lowerBound(s)));
jjg@789 850 // System.err.println();
jjg@789 851 // }
duke@1 852
duke@1 853 @Override
duke@1 854 public Boolean visitWildcardType(WildcardType t, Type s) {
duke@1 855 if (s.tag >= firstPartialTag)
duke@1 856 return containedBy(s, t);
duke@1 857 else {
jjg@789 858 // debugContainsType(t, s);
duke@1 859 return isSameWildcard(t, s)
duke@1 860 || isCaptureOf(s, t)
duke@1 861 || ((t.isExtendsBound() || isSubtypeNoCapture(L(t), lowerBound(s))) &&
duke@1 862 (t.isSuperBound() || isSubtypeNoCapture(upperBound(s), U(t))));
duke@1 863 }
duke@1 864 }
duke@1 865
duke@1 866 @Override
duke@1 867 public Boolean visitUndetVar(UndetVar t, Type s) {
duke@1 868 if (s.tag != WILDCARD)
duke@1 869 return isSameType(t, s);
duke@1 870 else
duke@1 871 return false;
duke@1 872 }
duke@1 873
duke@1 874 @Override
duke@1 875 public Boolean visitErrorType(ErrorType t, Type s) {
duke@1 876 return true;
duke@1 877 }
duke@1 878 };
duke@1 879
duke@1 880 public boolean isCaptureOf(Type s, WildcardType t) {
mcimadamore@79 881 if (s.tag != TYPEVAR || !((TypeVar)s).isCaptured())
duke@1 882 return false;
duke@1 883 return isSameWildcard(t, ((CapturedType)s).wildcard);
duke@1 884 }
duke@1 885
duke@1 886 public boolean isSameWildcard(WildcardType t, Type s) {
duke@1 887 if (s.tag != WILDCARD)
duke@1 888 return false;
duke@1 889 WildcardType w = (WildcardType)s;
duke@1 890 return w.kind == t.kind && w.type == t.type;
duke@1 891 }
duke@1 892
duke@1 893 public boolean containsTypeEquivalent(List<Type> ts, List<Type> ss) {
duke@1 894 while (ts.nonEmpty() && ss.nonEmpty()
duke@1 895 && containsTypeEquivalent(ts.head, ss.head)) {
duke@1 896 ts = ts.tail;
duke@1 897 ss = ss.tail;
duke@1 898 }
duke@1 899 return ts.isEmpty() && ss.isEmpty();
duke@1 900 }
duke@1 901 // </editor-fold>
duke@1 902
duke@1 903 // <editor-fold defaultstate="collapsed" desc="isCastable">
duke@1 904 public boolean isCastable(Type t, Type s) {
duke@1 905 return isCastable(t, s, Warner.noWarnings);
duke@1 906 }
duke@1 907
duke@1 908 /**
duke@1 909 * Is t is castable to s?<br>
duke@1 910 * s is assumed to be an erased type.<br>
duke@1 911 * (not defined for Method and ForAll types).
duke@1 912 */
duke@1 913 public boolean isCastable(Type t, Type s, Warner warn) {
duke@1 914 if (t == s)
duke@1 915 return true;
duke@1 916
duke@1 917 if (t.isPrimitive() != s.isPrimitive())
jjg@984 918 return allowBoxing && (
jjg@984 919 isConvertible(t, s, warn)
mcimadamore@1007 920 || (allowObjectToPrimitiveCast &&
mcimadamore@1007 921 s.isPrimitive() &&
mcimadamore@1007 922 isSubtype(boxedClass(s).type, t)));
duke@1 923 if (warn != warnStack.head) {
duke@1 924 try {
duke@1 925 warnStack = warnStack.prepend(warn);
mcimadamore@795 926 checkUnsafeVarargsConversion(t, s, warn);
mcimadamore@185 927 return isCastable.visit(t,s);
duke@1 928 } finally {
duke@1 929 warnStack = warnStack.tail;
duke@1 930 }
duke@1 931 } else {
mcimadamore@185 932 return isCastable.visit(t,s);
duke@1 933 }
duke@1 934 }
duke@1 935 // where
duke@1 936 private TypeRelation isCastable = new TypeRelation() {
duke@1 937
duke@1 938 public Boolean visitType(Type t, Type s) {
duke@1 939 if (s.tag == ERROR)
duke@1 940 return true;
duke@1 941
duke@1 942 switch (t.tag) {
duke@1 943 case BYTE: case CHAR: case SHORT: case INT: case LONG: case FLOAT:
duke@1 944 case DOUBLE:
duke@1 945 return s.tag <= DOUBLE;
duke@1 946 case BOOLEAN:
duke@1 947 return s.tag == BOOLEAN;
duke@1 948 case VOID:
duke@1 949 return false;
duke@1 950 case BOT:
duke@1 951 return isSubtype(t, s);
duke@1 952 default:
duke@1 953 throw new AssertionError();
duke@1 954 }
duke@1 955 }
duke@1 956
duke@1 957 @Override
duke@1 958 public Boolean visitWildcardType(WildcardType t, Type s) {
duke@1 959 return isCastable(upperBound(t), s, warnStack.head);
duke@1 960 }
duke@1 961
duke@1 962 @Override
duke@1 963 public Boolean visitClassType(ClassType t, Type s) {
duke@1 964 if (s.tag == ERROR || s.tag == BOT)
duke@1 965 return true;
duke@1 966
duke@1 967 if (s.tag == TYPEVAR) {
mcimadamore@640 968 if (isCastable(t, s.getUpperBound(), Warner.noWarnings)) {
mcimadamore@795 969 warnStack.head.warn(LintCategory.UNCHECKED);
duke@1 970 return true;
duke@1 971 } else {
duke@1 972 return false;
duke@1 973 }
duke@1 974 }
duke@1 975
duke@1 976 if (t.isCompound()) {
mcimadamore@211 977 Warner oldWarner = warnStack.head;
mcimadamore@211 978 warnStack.head = Warner.noWarnings;
duke@1 979 if (!visit(supertype(t), s))
duke@1 980 return false;
duke@1 981 for (Type intf : interfaces(t)) {
duke@1 982 if (!visit(intf, s))
duke@1 983 return false;
duke@1 984 }
mcimadamore@795 985 if (warnStack.head.hasLint(LintCategory.UNCHECKED))
mcimadamore@795 986 oldWarner.warn(LintCategory.UNCHECKED);
duke@1 987 return true;
duke@1 988 }
duke@1 989
duke@1 990 if (s.isCompound()) {
duke@1 991 // call recursively to reuse the above code
duke@1 992 return visitClassType((ClassType)s, t);
duke@1 993 }
duke@1 994
duke@1 995 if (s.tag == CLASS || s.tag == ARRAY) {
duke@1 996 boolean upcast;
duke@1 997 if ((upcast = isSubtype(erasure(t), erasure(s)))
duke@1 998 || isSubtype(erasure(s), erasure(t))) {
duke@1 999 if (!upcast && s.tag == ARRAY) {
duke@1 1000 if (!isReifiable(s))
mcimadamore@795 1001 warnStack.head.warn(LintCategory.UNCHECKED);
duke@1 1002 return true;
duke@1 1003 } else if (s.isRaw()) {
duke@1 1004 return true;
duke@1 1005 } else if (t.isRaw()) {
duke@1 1006 if (!isUnbounded(s))
mcimadamore@795 1007 warnStack.head.warn(LintCategory.UNCHECKED);
duke@1 1008 return true;
duke@1 1009 }
duke@1 1010 // Assume |a| <: |b|
duke@1 1011 final Type a = upcast ? t : s;
duke@1 1012 final Type b = upcast ? s : t;
duke@1 1013 final boolean HIGH = true;
duke@1 1014 final boolean LOW = false;
duke@1 1015 final boolean DONT_REWRITE_TYPEVARS = false;
duke@1 1016 Type aHigh = rewriteQuantifiers(a, HIGH, DONT_REWRITE_TYPEVARS);
duke@1 1017 Type aLow = rewriteQuantifiers(a, LOW, DONT_REWRITE_TYPEVARS);
duke@1 1018 Type bHigh = rewriteQuantifiers(b, HIGH, DONT_REWRITE_TYPEVARS);
duke@1 1019 Type bLow = rewriteQuantifiers(b, LOW, DONT_REWRITE_TYPEVARS);
duke@1 1020 Type lowSub = asSub(bLow, aLow.tsym);
duke@1 1021 Type highSub = (lowSub == null) ? null : asSub(bHigh, aHigh.tsym);
duke@1 1022 if (highSub == null) {
duke@1 1023 final boolean REWRITE_TYPEVARS = true;
duke@1 1024 aHigh = rewriteQuantifiers(a, HIGH, REWRITE_TYPEVARS);
duke@1 1025 aLow = rewriteQuantifiers(a, LOW, REWRITE_TYPEVARS);
duke@1 1026 bHigh = rewriteQuantifiers(b, HIGH, REWRITE_TYPEVARS);
duke@1 1027 bLow = rewriteQuantifiers(b, LOW, REWRITE_TYPEVARS);
duke@1 1028 lowSub = asSub(bLow, aLow.tsym);
duke@1 1029 highSub = (lowSub == null) ? null : asSub(bHigh, aHigh.tsym);
duke@1 1030 }
duke@1 1031 if (highSub != null) {
jjg@816 1032 if (!(a.tsym == highSub.tsym && a.tsym == lowSub.tsym)) {
jjg@816 1033 Assert.error(a.tsym + " != " + highSub.tsym + " != " + lowSub.tsym);
jjg@816 1034 }
mcimadamore@185 1035 if (!disjointTypes(aHigh.allparams(), highSub.allparams())
mcimadamore@185 1036 && !disjointTypes(aHigh.allparams(), lowSub.allparams())
mcimadamore@185 1037 && !disjointTypes(aLow.allparams(), highSub.allparams())
mcimadamore@185 1038 && !disjointTypes(aLow.allparams(), lowSub.allparams())) {
mcimadamore@779 1039 if (upcast ? giveWarning(a, b) :
mcimadamore@235 1040 giveWarning(b, a))
mcimadamore@795 1041 warnStack.head.warn(LintCategory.UNCHECKED);
duke@1 1042 return true;
duke@1 1043 }
duke@1 1044 }
duke@1 1045 if (isReifiable(s))
duke@1 1046 return isSubtypeUnchecked(a, b);
duke@1 1047 else
duke@1 1048 return isSubtypeUnchecked(a, b, warnStack.head);
duke@1 1049 }
duke@1 1050
duke@1 1051 // Sidecast
duke@1 1052 if (s.tag == CLASS) {
duke@1 1053 if ((s.tsym.flags() & INTERFACE) != 0) {
duke@1 1054 return ((t.tsym.flags() & FINAL) == 0)
duke@1 1055 ? sideCast(t, s, warnStack.head)
duke@1 1056 : sideCastFinal(t, s, warnStack.head);
duke@1 1057 } else if ((t.tsym.flags() & INTERFACE) != 0) {
duke@1 1058 return ((s.tsym.flags() & FINAL) == 0)
duke@1 1059 ? sideCast(t, s, warnStack.head)
duke@1 1060 : sideCastFinal(t, s, warnStack.head);
duke@1 1061 } else {
duke@1 1062 // unrelated class types
duke@1 1063 return false;
duke@1 1064 }
duke@1 1065 }
duke@1 1066 }
duke@1 1067 return false;
duke@1 1068 }
duke@1 1069
duke@1 1070 @Override
duke@1 1071 public Boolean visitArrayType(ArrayType t, Type s) {
duke@1 1072 switch (s.tag) {
duke@1 1073 case ERROR:
duke@1 1074 case BOT:
duke@1 1075 return true;
duke@1 1076 case TYPEVAR:
duke@1 1077 if (isCastable(s, t, Warner.noWarnings)) {
mcimadamore@795 1078 warnStack.head.warn(LintCategory.UNCHECKED);
duke@1 1079 return true;
duke@1 1080 } else {
duke@1 1081 return false;
duke@1 1082 }
duke@1 1083 case CLASS:
duke@1 1084 return isSubtype(t, s);
duke@1 1085 case ARRAY:
mcimadamore@786 1086 if (elemtype(t).tag <= lastBaseTag ||
mcimadamore@786 1087 elemtype(s).tag <= lastBaseTag) {
duke@1 1088 return elemtype(t).tag == elemtype(s).tag;
duke@1 1089 } else {
duke@1 1090 return visit(elemtype(t), elemtype(s));
duke@1 1091 }
duke@1 1092 default:
duke@1 1093 return false;
duke@1 1094 }
duke@1 1095 }
duke@1 1096
duke@1 1097 @Override
duke@1 1098 public Boolean visitTypeVar(TypeVar t, Type s) {
duke@1 1099 switch (s.tag) {
duke@1 1100 case ERROR:
duke@1 1101 case BOT:
duke@1 1102 return true;
duke@1 1103 case TYPEVAR:
duke@1 1104 if (isSubtype(t, s)) {
duke@1 1105 return true;
duke@1 1106 } else if (isCastable(t.bound, s, Warner.noWarnings)) {
mcimadamore@795 1107 warnStack.head.warn(LintCategory.UNCHECKED);
duke@1 1108 return true;
duke@1 1109 } else {
duke@1 1110 return false;
duke@1 1111 }
duke@1 1112 default:
duke@1 1113 return isCastable(t.bound, s, warnStack.head);
duke@1 1114 }
duke@1 1115 }
duke@1 1116
duke@1 1117 @Override
duke@1 1118 public Boolean visitErrorType(ErrorType t, Type s) {
duke@1 1119 return true;
duke@1 1120 }
duke@1 1121 };
duke@1 1122 // </editor-fold>
duke@1 1123
duke@1 1124 // <editor-fold defaultstate="collapsed" desc="disjointTypes">
duke@1 1125 public boolean disjointTypes(List<Type> ts, List<Type> ss) {
duke@1 1126 while (ts.tail != null && ss.tail != null) {
duke@1 1127 if (disjointType(ts.head, ss.head)) return true;
duke@1 1128 ts = ts.tail;
duke@1 1129 ss = ss.tail;
duke@1 1130 }
duke@1 1131 return false;
duke@1 1132 }
duke@1 1133
duke@1 1134 /**
duke@1 1135 * Two types or wildcards are considered disjoint if it can be
duke@1 1136 * proven that no type can be contained in both. It is
duke@1 1137 * conservative in that it is allowed to say that two types are
duke@1 1138 * not disjoint, even though they actually are.
duke@1 1139 *
duke@1 1140 * The type C<X> is castable to C<Y> exactly if X and Y are not
duke@1 1141 * disjoint.
duke@1 1142 */
duke@1 1143 public boolean disjointType(Type t, Type s) {
duke@1 1144 return disjointType.visit(t, s);
duke@1 1145 }
duke@1 1146 // where
duke@1 1147 private TypeRelation disjointType = new TypeRelation() {
duke@1 1148
duke@1 1149 private Set<TypePair> cache = new HashSet<TypePair>();
duke@1 1150
duke@1 1151 public Boolean visitType(Type t, Type s) {
duke@1 1152 if (s.tag == WILDCARD)
duke@1 1153 return visit(s, t);
duke@1 1154 else
duke@1 1155 return notSoftSubtypeRecursive(t, s) || notSoftSubtypeRecursive(s, t);
duke@1 1156 }
duke@1 1157
duke@1 1158 private boolean isCastableRecursive(Type t, Type s) {
duke@1 1159 TypePair pair = new TypePair(t, s);
duke@1 1160 if (cache.add(pair)) {
duke@1 1161 try {
duke@1 1162 return Types.this.isCastable(t, s);
duke@1 1163 } finally {
duke@1 1164 cache.remove(pair);
duke@1 1165 }
duke@1 1166 } else {
duke@1 1167 return true;
duke@1 1168 }
duke@1 1169 }
duke@1 1170
duke@1 1171 private boolean notSoftSubtypeRecursive(Type t, Type s) {
duke@1 1172 TypePair pair = new TypePair(t, s);
duke@1 1173 if (cache.add(pair)) {
duke@1 1174 try {
duke@1 1175 return Types.this.notSoftSubtype(t, s);
duke@1 1176 } finally {
duke@1 1177 cache.remove(pair);
duke@1 1178 }
duke@1 1179 } else {
duke@1 1180 return false;
duke@1 1181 }
duke@1 1182 }
duke@1 1183
duke@1 1184 @Override
duke@1 1185 public Boolean visitWildcardType(WildcardType t, Type s) {
duke@1 1186 if (t.isUnbound())
duke@1 1187 return false;
duke@1 1188
duke@1 1189 if (s.tag != WILDCARD) {
duke@1 1190 if (t.isExtendsBound())
duke@1 1191 return notSoftSubtypeRecursive(s, t.type);
duke@1 1192 else // isSuperBound()
duke@1 1193 return notSoftSubtypeRecursive(t.type, s);
duke@1 1194 }
duke@1 1195
duke@1 1196 if (s.isUnbound())
duke@1 1197 return false;
duke@1 1198
duke@1 1199 if (t.isExtendsBound()) {
duke@1 1200 if (s.isExtendsBound())
duke@1 1201 return !isCastableRecursive(t.type, upperBound(s));
duke@1 1202 else if (s.isSuperBound())
duke@1 1203 return notSoftSubtypeRecursive(lowerBound(s), t.type);
duke@1 1204 } else if (t.isSuperBound()) {
duke@1 1205 if (s.isExtendsBound())
duke@1 1206 return notSoftSubtypeRecursive(t.type, upperBound(s));
duke@1 1207 }
duke@1 1208 return false;
duke@1 1209 }
duke@1 1210 };
duke@1 1211 // </editor-fold>
duke@1 1212
duke@1 1213 // <editor-fold defaultstate="collapsed" desc="lowerBoundArgtypes">
duke@1 1214 /**
duke@1 1215 * Returns the lower bounds of the formals of a method.
duke@1 1216 */
duke@1 1217 public List<Type> lowerBoundArgtypes(Type t) {
duke@1 1218 return map(t.getParameterTypes(), lowerBoundMapping);
duke@1 1219 }
duke@1 1220 private final Mapping lowerBoundMapping = new Mapping("lowerBound") {
duke@1 1221 public Type apply(Type t) {
duke@1 1222 return lowerBound(t);
duke@1 1223 }
duke@1 1224 };
duke@1 1225 // </editor-fold>
duke@1 1226
duke@1 1227 // <editor-fold defaultstate="collapsed" desc="notSoftSubtype">
duke@1 1228 /**
duke@1 1229 * This relation answers the question: is impossible that
duke@1 1230 * something of type `t' can be a subtype of `s'? This is
duke@1 1231 * different from the question "is `t' not a subtype of `s'?"
duke@1 1232 * when type variables are involved: Integer is not a subtype of T
duke@1 1233 * where <T extends Number> but it is not true that Integer cannot
duke@1 1234 * possibly be a subtype of T.
duke@1 1235 */
duke@1 1236 public boolean notSoftSubtype(Type t, Type s) {
duke@1 1237 if (t == s) return false;
duke@1 1238 if (t.tag == TYPEVAR) {
duke@1 1239 TypeVar tv = (TypeVar) t;
duke@1 1240 return !isCastable(tv.bound,
mcimadamore@640 1241 relaxBound(s),
duke@1 1242 Warner.noWarnings);
duke@1 1243 }
duke@1 1244 if (s.tag != WILDCARD)
duke@1 1245 s = upperBound(s);
mcimadamore@640 1246
mcimadamore@640 1247 return !isSubtype(t, relaxBound(s));
mcimadamore@640 1248 }
mcimadamore@640 1249
mcimadamore@640 1250 private Type relaxBound(Type t) {
mcimadamore@640 1251 if (t.tag == TYPEVAR) {
mcimadamore@640 1252 while (t.tag == TYPEVAR)
mcimadamore@640 1253 t = t.getUpperBound();
mcimadamore@640 1254 t = rewriteQuantifiers(t, true, true);
mcimadamore@640 1255 }
mcimadamore@640 1256 return t;
duke@1 1257 }
duke@1 1258 // </editor-fold>
duke@1 1259
duke@1 1260 // <editor-fold defaultstate="collapsed" desc="isReifiable">
duke@1 1261 public boolean isReifiable(Type t) {
duke@1 1262 return isReifiable.visit(t);
duke@1 1263 }
duke@1 1264 // where
duke@1 1265 private UnaryVisitor<Boolean> isReifiable = new UnaryVisitor<Boolean>() {
duke@1 1266
duke@1 1267 public Boolean visitType(Type t, Void ignored) {
duke@1 1268 return true;
duke@1 1269 }
duke@1 1270
duke@1 1271 @Override
duke@1 1272 public Boolean visitClassType(ClassType t, Void ignored) {
mcimadamore@356 1273 if (t.isCompound())
mcimadamore@356 1274 return false;
mcimadamore@356 1275 else {
mcimadamore@356 1276 if (!t.isParameterized())
mcimadamore@356 1277 return true;
mcimadamore@356 1278
mcimadamore@356 1279 for (Type param : t.allparams()) {
mcimadamore@356 1280 if (!param.isUnbound())
mcimadamore@356 1281 return false;
mcimadamore@356 1282 }
duke@1 1283 return true;
duke@1 1284 }
duke@1 1285 }
duke@1 1286
duke@1 1287 @Override
duke@1 1288 public Boolean visitArrayType(ArrayType t, Void ignored) {
duke@1 1289 return visit(t.elemtype);
duke@1 1290 }
duke@1 1291
duke@1 1292 @Override
duke@1 1293 public Boolean visitTypeVar(TypeVar t, Void ignored) {
duke@1 1294 return false;
duke@1 1295 }
duke@1 1296 };
duke@1 1297 // </editor-fold>
duke@1 1298
duke@1 1299 // <editor-fold defaultstate="collapsed" desc="Array Utils">
duke@1 1300 public boolean isArray(Type t) {
duke@1 1301 while (t.tag == WILDCARD)
duke@1 1302 t = upperBound(t);
duke@1 1303 return t.tag == ARRAY;
duke@1 1304 }
duke@1 1305
duke@1 1306 /**
duke@1 1307 * The element type of an array.
duke@1 1308 */
duke@1 1309 public Type elemtype(Type t) {
duke@1 1310 switch (t.tag) {
duke@1 1311 case WILDCARD:
duke@1 1312 return elemtype(upperBound(t));
duke@1 1313 case ARRAY:
duke@1 1314 return ((ArrayType)t).elemtype;
duke@1 1315 case FORALL:
duke@1 1316 return elemtype(((ForAll)t).qtype);
duke@1 1317 case ERROR:
duke@1 1318 return t;
duke@1 1319 default:
duke@1 1320 return null;
duke@1 1321 }
duke@1 1322 }
duke@1 1323
mcimadamore@787 1324 public Type elemtypeOrType(Type t) {
mcimadamore@787 1325 Type elemtype = elemtype(t);
mcimadamore@787 1326 return elemtype != null ?
mcimadamore@787 1327 elemtype :
mcimadamore@787 1328 t;
mcimadamore@787 1329 }
mcimadamore@787 1330
duke@1 1331 /**
duke@1 1332 * Mapping to take element type of an arraytype
duke@1 1333 */
duke@1 1334 private Mapping elemTypeFun = new Mapping ("elemTypeFun") {
duke@1 1335 public Type apply(Type t) { return elemtype(t); }
duke@1 1336 };
duke@1 1337
duke@1 1338 /**
duke@1 1339 * The number of dimensions of an array type.
duke@1 1340 */
duke@1 1341 public int dimensions(Type t) {
duke@1 1342 int result = 0;
duke@1 1343 while (t.tag == ARRAY) {
duke@1 1344 result++;
duke@1 1345 t = elemtype(t);
duke@1 1346 }
duke@1 1347 return result;
duke@1 1348 }
jfranck@1313 1349
jfranck@1313 1350 /**
jfranck@1313 1351 * Returns an ArrayType with the component type t
jfranck@1313 1352 *
jfranck@1313 1353 * @param t The component type of the ArrayType
jfranck@1313 1354 * @return the ArrayType for the given component
jfranck@1313 1355 */
jfranck@1313 1356 public ArrayType makeArrayType(Type t) {
jfranck@1313 1357 if (t.tag == VOID ||
jfranck@1313 1358 t.tag >= PACKAGE) {
jfranck@1313 1359 Assert.error("Type t must not be a a VOID or PACKAGE type, " + t.toString());
jfranck@1313 1360 }
jfranck@1313 1361 return new ArrayType(t, syms.arrayClass);
jfranck@1313 1362 }
duke@1 1363 // </editor-fold>
duke@1 1364
duke@1 1365 // <editor-fold defaultstate="collapsed" desc="asSuper">
duke@1 1366 /**
duke@1 1367 * Return the (most specific) base type of t that starts with the
duke@1 1368 * given symbol. If none exists, return null.
duke@1 1369 *
duke@1 1370 * @param t a type
duke@1 1371 * @param sym a symbol
duke@1 1372 */
duke@1 1373 public Type asSuper(Type t, Symbol sym) {
duke@1 1374 return asSuper.visit(t, sym);
duke@1 1375 }
duke@1 1376 // where
duke@1 1377 private SimpleVisitor<Type,Symbol> asSuper = new SimpleVisitor<Type,Symbol>() {
duke@1 1378
duke@1 1379 public Type visitType(Type t, Symbol sym) {
duke@1 1380 return null;
duke@1 1381 }
duke@1 1382
duke@1 1383 @Override
duke@1 1384 public Type visitClassType(ClassType t, Symbol sym) {
duke@1 1385 if (t.tsym == sym)
duke@1 1386 return t;
duke@1 1387
duke@1 1388 Type st = supertype(t);
mcimadamore@19 1389 if (st.tag == CLASS || st.tag == TYPEVAR || st.tag == ERROR) {
duke@1 1390 Type x = asSuper(st, sym);
duke@1 1391 if (x != null)
duke@1 1392 return x;
duke@1 1393 }
duke@1 1394 if ((sym.flags() & INTERFACE) != 0) {
duke@1 1395 for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail) {
duke@1 1396 Type x = asSuper(l.head, sym);
duke@1 1397 if (x != null)
duke@1 1398 return x;
duke@1 1399 }
duke@1 1400 }
duke@1 1401 return null;
duke@1 1402 }
duke@1 1403
duke@1 1404 @Override
duke@1 1405 public Type visitArrayType(ArrayType t, Symbol sym) {
duke@1 1406 return isSubtype(t, sym.type) ? sym.type : null;
duke@1 1407 }
duke@1 1408
duke@1 1409 @Override
duke@1 1410 public Type visitTypeVar(TypeVar t, Symbol sym) {
mcimadamore@19 1411 if (t.tsym == sym)
mcimadamore@19 1412 return t;
mcimadamore@19 1413 else
mcimadamore@19 1414 return asSuper(t.bound, sym);
duke@1 1415 }
duke@1 1416
duke@1 1417 @Override
duke@1 1418 public Type visitErrorType(ErrorType t, Symbol sym) {
duke@1 1419 return t;
duke@1 1420 }
duke@1 1421 };
duke@1 1422
duke@1 1423 /**
duke@1 1424 * Return the base type of t or any of its outer types that starts
duke@1 1425 * with the given symbol. If none exists, return null.
duke@1 1426 *
duke@1 1427 * @param t a type
duke@1 1428 * @param sym a symbol
duke@1 1429 */
duke@1 1430 public Type asOuterSuper(Type t, Symbol sym) {
duke@1 1431 switch (t.tag) {
duke@1 1432 case CLASS:
duke@1 1433 do {
duke@1 1434 Type s = asSuper(t, sym);
duke@1 1435 if (s != null) return s;
duke@1 1436 t = t.getEnclosingType();
duke@1 1437 } while (t.tag == CLASS);
duke@1 1438 return null;
duke@1 1439 case ARRAY:
duke@1 1440 return isSubtype(t, sym.type) ? sym.type : null;
duke@1 1441 case TYPEVAR:
duke@1 1442 return asSuper(t, sym);
duke@1 1443 case ERROR:
duke@1 1444 return t;
duke@1 1445 default:
duke@1 1446 return null;
duke@1 1447 }
duke@1 1448 }
duke@1 1449
duke@1 1450 /**
duke@1 1451 * Return the base type of t or any of its enclosing types that
duke@1 1452 * starts with the given symbol. If none exists, return null.
duke@1 1453 *
duke@1 1454 * @param t a type
duke@1 1455 * @param sym a symbol
duke@1 1456 */
duke@1 1457 public Type asEnclosingSuper(Type t, Symbol sym) {
duke@1 1458 switch (t.tag) {
duke@1 1459 case CLASS:
duke@1 1460 do {
duke@1 1461 Type s = asSuper(t, sym);
duke@1 1462 if (s != null) return s;
duke@1 1463 Type outer = t.getEnclosingType();
duke@1 1464 t = (outer.tag == CLASS) ? outer :
duke@1 1465 (t.tsym.owner.enclClass() != null) ? t.tsym.owner.enclClass().type :
duke@1 1466 Type.noType;
duke@1 1467 } while (t.tag == CLASS);
duke@1 1468 return null;
duke@1 1469 case ARRAY:
duke@1 1470 return isSubtype(t, sym.type) ? sym.type : null;
duke@1 1471 case TYPEVAR:
duke@1 1472 return asSuper(t, sym);
duke@1 1473 case ERROR:
duke@1 1474 return t;
duke@1 1475 default:
duke@1 1476 return null;
duke@1 1477 }
duke@1 1478 }
duke@1 1479 // </editor-fold>
duke@1 1480
duke@1 1481 // <editor-fold defaultstate="collapsed" desc="memberType">
duke@1 1482 /**
duke@1 1483 * The type of given symbol, seen as a member of t.
duke@1 1484 *
duke@1 1485 * @param t a type
duke@1 1486 * @param sym a symbol
duke@1 1487 */
duke@1 1488 public Type memberType(Type t, Symbol sym) {
duke@1 1489 return (sym.flags() & STATIC) != 0
duke@1 1490 ? sym.type
duke@1 1491 : memberType.visit(t, sym);
mcimadamore@341 1492 }
duke@1 1493 // where
duke@1 1494 private SimpleVisitor<Type,Symbol> memberType = new SimpleVisitor<Type,Symbol>() {
duke@1 1495
duke@1 1496 public Type visitType(Type t, Symbol sym) {
duke@1 1497 return sym.type;
duke@1 1498 }
duke@1 1499
duke@1 1500 @Override
duke@1 1501 public Type visitWildcardType(WildcardType t, Symbol sym) {
duke@1 1502 return memberType(upperBound(t), sym);
duke@1 1503 }
duke@1 1504
duke@1 1505 @Override
duke@1 1506 public Type visitClassType(ClassType t, Symbol sym) {
duke@1 1507 Symbol owner = sym.owner;
duke@1 1508 long flags = sym.flags();
duke@1 1509 if (((flags & STATIC) == 0) && owner.type.isParameterized()) {
duke@1 1510 Type base = asOuterSuper(t, owner);
mcimadamore@134 1511 //if t is an intersection type T = CT & I1 & I2 ... & In
mcimadamore@134 1512 //its supertypes CT, I1, ... In might contain wildcards
mcimadamore@134 1513 //so we need to go through capture conversion
mcimadamore@134 1514 base = t.isCompound() ? capture(base) : base;
duke@1 1515 if (base != null) {
duke@1 1516 List<Type> ownerParams = owner.type.allparams();
duke@1 1517 List<Type> baseParams = base.allparams();
duke@1 1518 if (ownerParams.nonEmpty()) {
duke@1 1519 if (baseParams.isEmpty()) {
duke@1 1520 // then base is a raw type
duke@1 1521 return erasure(sym.type);
duke@1 1522 } else {
duke@1 1523 return subst(sym.type, ownerParams, baseParams);
duke@1 1524 }
duke@1 1525 }
duke@1 1526 }
duke@1 1527 }
duke@1 1528 return sym.type;
duke@1 1529 }
duke@1 1530
duke@1 1531 @Override
duke@1 1532 public Type visitTypeVar(TypeVar t, Symbol sym) {
duke@1 1533 return memberType(t.bound, sym);
duke@1 1534 }
duke@1 1535
duke@1 1536 @Override
duke@1 1537 public Type visitErrorType(ErrorType t, Symbol sym) {
duke@1 1538 return t;
duke@1 1539 }
duke@1 1540 };
duke@1 1541 // </editor-fold>
duke@1 1542
duke@1 1543 // <editor-fold defaultstate="collapsed" desc="isAssignable">
duke@1 1544 public boolean isAssignable(Type t, Type s) {
duke@1 1545 return isAssignable(t, s, Warner.noWarnings);
duke@1 1546 }
duke@1 1547
duke@1 1548 /**
duke@1 1549 * Is t assignable to s?<br>
duke@1 1550 * Equivalent to subtype except for constant values and raw
duke@1 1551 * types.<br>
duke@1 1552 * (not defined for Method and ForAll types)
duke@1 1553 */
duke@1 1554 public boolean isAssignable(Type t, Type s, Warner warn) {
duke@1 1555 if (t.tag == ERROR)
duke@1 1556 return true;
duke@1 1557 if (t.tag <= INT && t.constValue() != null) {
duke@1 1558 int value = ((Number)t.constValue()).intValue();
duke@1 1559 switch (s.tag) {
duke@1 1560 case BYTE:
duke@1 1561 if (Byte.MIN_VALUE <= value && value <= Byte.MAX_VALUE)
duke@1 1562 return true;
duke@1 1563 break;
duke@1 1564 case CHAR:
duke@1 1565 if (Character.MIN_VALUE <= value && value <= Character.MAX_VALUE)
duke@1 1566 return true;
duke@1 1567 break;
duke@1 1568 case SHORT:
duke@1 1569 if (Short.MIN_VALUE <= value && value <= Short.MAX_VALUE)
duke@1 1570 return true;
duke@1 1571 break;
duke@1 1572 case INT:
duke@1 1573 return true;
duke@1 1574 case CLASS:
duke@1 1575 switch (unboxedType(s).tag) {
duke@1 1576 case BYTE:
duke@1 1577 case CHAR:
duke@1 1578 case SHORT:
duke@1 1579 return isAssignable(t, unboxedType(s), warn);
duke@1 1580 }
duke@1 1581 break;
duke@1 1582 }
duke@1 1583 }
duke@1 1584 return isConvertible(t, s, warn);
duke@1 1585 }
duke@1 1586 // </editor-fold>
duke@1 1587
duke@1 1588 // <editor-fold defaultstate="collapsed" desc="erasure">
duke@1 1589 /**
duke@1 1590 * The erasure of t {@code |t|} -- the type that results when all
duke@1 1591 * type parameters in t are deleted.
duke@1 1592 */
duke@1 1593 public Type erasure(Type t) {
sundar@1307 1594 return eraseNotNeeded(t)? t : erasure(t, false);
mcimadamore@30 1595 }
mcimadamore@30 1596 //where
sundar@1307 1597 private boolean eraseNotNeeded(Type t) {
sundar@1307 1598 // We don't want to erase primitive types and String type as that
sundar@1307 1599 // operation is idempotent. Also, erasing these could result in loss
sundar@1307 1600 // of information such as constant values attached to such types.
sundar@1307 1601 return (t.tag <= lastBaseTag) || (syms.stringType.tsym == t.tsym);
sundar@1307 1602 }
sundar@1307 1603
mcimadamore@30 1604 private Type erasure(Type t, boolean recurse) {
duke@1 1605 if (t.tag <= lastBaseTag)
duke@1 1606 return t; /* fast special case */
duke@1 1607 else
mcimadamore@30 1608 return erasure.visit(t, recurse);
mcimadamore@341 1609 }
duke@1 1610 // where
mcimadamore@30 1611 private SimpleVisitor<Type, Boolean> erasure = new SimpleVisitor<Type, Boolean>() {
mcimadamore@30 1612 public Type visitType(Type t, Boolean recurse) {
duke@1 1613 if (t.tag <= lastBaseTag)
duke@1 1614 return t; /*fast special case*/
duke@1 1615 else
mcimadamore@30 1616 return t.map(recurse ? erasureRecFun : erasureFun);
duke@1 1617 }
duke@1 1618
duke@1 1619 @Override
mcimadamore@30 1620 public Type visitWildcardType(WildcardType t, Boolean recurse) {
mcimadamore@30 1621 return erasure(upperBound(t), recurse);
duke@1 1622 }
duke@1 1623
duke@1 1624 @Override
mcimadamore@30 1625 public Type visitClassType(ClassType t, Boolean recurse) {
mcimadamore@30 1626 Type erased = t.tsym.erasure(Types.this);
mcimadamore@30 1627 if (recurse) {
mcimadamore@30 1628 erased = new ErasedClassType(erased.getEnclosingType(),erased.tsym);
mcimadamore@30 1629 }
mcimadamore@30 1630 return erased;
duke@1 1631 }
duke@1 1632
duke@1 1633 @Override
mcimadamore@30 1634 public Type visitTypeVar(TypeVar t, Boolean recurse) {
mcimadamore@30 1635 return erasure(t.bound, recurse);
duke@1 1636 }
duke@1 1637
duke@1 1638 @Override
mcimadamore@30 1639 public Type visitErrorType(ErrorType t, Boolean recurse) {
duke@1 1640 return t;
duke@1 1641 }
duke@1 1642 };
mcimadamore@30 1643
duke@1 1644 private Mapping erasureFun = new Mapping ("erasure") {
duke@1 1645 public Type apply(Type t) { return erasure(t); }
duke@1 1646 };
duke@1 1647
mcimadamore@30 1648 private Mapping erasureRecFun = new Mapping ("erasureRecursive") {
mcimadamore@30 1649 public Type apply(Type t) { return erasureRecursive(t); }
mcimadamore@30 1650 };
mcimadamore@30 1651
duke@1 1652 public List<Type> erasure(List<Type> ts) {
duke@1 1653 return Type.map(ts, erasureFun);
duke@1 1654 }
mcimadamore@30 1655
mcimadamore@30 1656 public Type erasureRecursive(Type t) {
mcimadamore@30 1657 return erasure(t, true);
mcimadamore@30 1658 }
mcimadamore@30 1659
mcimadamore@30 1660 public List<Type> erasureRecursive(List<Type> ts) {
mcimadamore@30 1661 return Type.map(ts, erasureRecFun);
mcimadamore@30 1662 }
duke@1 1663 // </editor-fold>
duke@1 1664
duke@1 1665 // <editor-fold defaultstate="collapsed" desc="makeCompoundType">
duke@1 1666 /**
duke@1 1667 * Make a compound type from non-empty list of types
duke@1 1668 *
duke@1 1669 * @param bounds the types from which the compound type is formed
duke@1 1670 * @param supertype is objectType if all bounds are interfaces,
duke@1 1671 * null otherwise.
duke@1 1672 */
duke@1 1673 public Type makeCompoundType(List<Type> bounds,
duke@1 1674 Type supertype) {
duke@1 1675 ClassSymbol bc =
duke@1 1676 new ClassSymbol(ABSTRACT|PUBLIC|SYNTHETIC|COMPOUND|ACYCLIC,
duke@1 1677 Type.moreInfo
duke@1 1678 ? names.fromString(bounds.toString())
duke@1 1679 : names.empty,
duke@1 1680 syms.noSymbol);
duke@1 1681 if (bounds.head.tag == TYPEVAR)
duke@1 1682 // error condition, recover
mcimadamore@121 1683 bc.erasure_field = syms.objectType;
mcimadamore@121 1684 else
mcimadamore@121 1685 bc.erasure_field = erasure(bounds.head);
mcimadamore@121 1686 bc.members_field = new Scope(bc);
duke@1 1687 ClassType bt = (ClassType)bc.type;
duke@1 1688 bt.allparams_field = List.nil();
duke@1 1689 if (supertype != null) {
duke@1 1690 bt.supertype_field = supertype;
duke@1 1691 bt.interfaces_field = bounds;
duke@1 1692 } else {
duke@1 1693 bt.supertype_field = bounds.head;
duke@1 1694 bt.interfaces_field = bounds.tail;
duke@1 1695 }
jjg@816 1696 Assert.check(bt.supertype_field.tsym.completer != null
jjg@816 1697 || !bt.supertype_field.isInterface(),
jjg@816 1698 bt.supertype_field);
duke@1 1699 return bt;
duke@1 1700 }
duke@1 1701
duke@1 1702 /**
duke@1 1703 * Same as {@link #makeCompoundType(List,Type)}, except that the
duke@1 1704 * second parameter is computed directly. Note that this might
duke@1 1705 * cause a symbol completion. Hence, this version of
duke@1 1706 * makeCompoundType may not be called during a classfile read.
duke@1 1707 */
duke@1 1708 public Type makeCompoundType(List<Type> bounds) {
duke@1 1709 Type supertype = (bounds.head.tsym.flags() & INTERFACE) != 0 ?
duke@1 1710 supertype(bounds.head) : null;
duke@1 1711 return makeCompoundType(bounds, supertype);
duke@1 1712 }
duke@1 1713
duke@1 1714 /**
duke@1 1715 * A convenience wrapper for {@link #makeCompoundType(List)}; the
duke@1 1716 * arguments are converted to a list and passed to the other
duke@1 1717 * method. Note that this might cause a symbol completion.
duke@1 1718 * Hence, this version of makeCompoundType may not be called
duke@1 1719 * during a classfile read.
duke@1 1720 */
duke@1 1721 public Type makeCompoundType(Type bound1, Type bound2) {
duke@1 1722 return makeCompoundType(List.of(bound1, bound2));
duke@1 1723 }
duke@1 1724 // </editor-fold>
duke@1 1725
duke@1 1726 // <editor-fold defaultstate="collapsed" desc="supertype">
duke@1 1727 public Type supertype(Type t) {
duke@1 1728 return supertype.visit(t);
duke@1 1729 }
duke@1 1730 // where
duke@1 1731 private UnaryVisitor<Type> supertype = new UnaryVisitor<Type>() {
duke@1 1732
duke@1 1733 public Type visitType(Type t, Void ignored) {
duke@1 1734 // A note on wildcards: there is no good way to
duke@1 1735 // determine a supertype for a super bounded wildcard.
duke@1 1736 return null;
duke@1 1737 }
duke@1 1738
duke@1 1739 @Override
duke@1 1740 public Type visitClassType(ClassType t, Void ignored) {
duke@1 1741 if (t.supertype_field == null) {
duke@1 1742 Type supertype = ((ClassSymbol)t.tsym).getSuperclass();
duke@1 1743 // An interface has no superclass; its supertype is Object.
duke@1 1744 if (t.isInterface())
duke@1 1745 supertype = ((ClassType)t.tsym.type).supertype_field;
duke@1 1746 if (t.supertype_field == null) {
duke@1 1747 List<Type> actuals = classBound(t).allparams();
duke@1 1748 List<Type> formals = t.tsym.type.allparams();
mcimadamore@30 1749 if (t.hasErasedSupertypes()) {
mcimadamore@30 1750 t.supertype_field = erasureRecursive(supertype);
mcimadamore@30 1751 } else if (formals.nonEmpty()) {
duke@1 1752 t.supertype_field = subst(supertype, formals, actuals);
duke@1 1753 }
mcimadamore@30 1754 else {
mcimadamore@30 1755 t.supertype_field = supertype;
mcimadamore@30 1756 }
duke@1 1757 }
duke@1 1758 }
duke@1 1759 return t.supertype_field;
duke@1 1760 }
duke@1 1761
duke@1 1762 /**
duke@1 1763 * The supertype is always a class type. If the type
duke@1 1764 * variable's bounds start with a class type, this is also
duke@1 1765 * the supertype. Otherwise, the supertype is
duke@1 1766 * java.lang.Object.
duke@1 1767 */
duke@1 1768 @Override
duke@1 1769 public Type visitTypeVar(TypeVar t, Void ignored) {
duke@1 1770 if (t.bound.tag == TYPEVAR ||
duke@1 1771 (!t.bound.isCompound() && !t.bound.isInterface())) {
duke@1 1772 return t.bound;
duke@1 1773 } else {
duke@1 1774 return supertype(t.bound);
duke@1 1775 }
duke@1 1776 }
duke@1 1777
duke@1 1778 @Override
duke@1 1779 public Type visitArrayType(ArrayType t, Void ignored) {
duke@1 1780 if (t.elemtype.isPrimitive() || isSameType(t.elemtype, syms.objectType))
duke@1 1781 return arraySuperType();
duke@1 1782 else
duke@1 1783 return new ArrayType(supertype(t.elemtype), t.tsym);
duke@1 1784 }
duke@1 1785
duke@1 1786 @Override
duke@1 1787 public Type visitErrorType(ErrorType t, Void ignored) {
duke@1 1788 return t;
duke@1 1789 }
duke@1 1790 };
duke@1 1791 // </editor-fold>
duke@1 1792
duke@1 1793 // <editor-fold defaultstate="collapsed" desc="interfaces">
duke@1 1794 /**
duke@1 1795 * Return the interfaces implemented by this class.
duke@1 1796 */
duke@1 1797 public List<Type> interfaces(Type t) {
duke@1 1798 return interfaces.visit(t);
duke@1 1799 }
duke@1 1800 // where
duke@1 1801 private UnaryVisitor<List<Type>> interfaces = new UnaryVisitor<List<Type>>() {
duke@1 1802
duke@1 1803 public List<Type> visitType(Type t, Void ignored) {
duke@1 1804 return List.nil();
duke@1 1805 }
duke@1 1806
duke@1 1807 @Override
duke@1 1808 public List<Type> visitClassType(ClassType t, Void ignored) {
duke@1 1809 if (t.interfaces_field == null) {
duke@1 1810 List<Type> interfaces = ((ClassSymbol)t.tsym).getInterfaces();
duke@1 1811 if (t.interfaces_field == null) {
duke@1 1812 // If t.interfaces_field is null, then t must
duke@1 1813 // be a parameterized type (not to be confused
duke@1 1814 // with a generic type declaration).
duke@1 1815 // Terminology:
duke@1 1816 // Parameterized type: List<String>
duke@1 1817 // Generic type declaration: class List<E> { ... }
duke@1 1818 // So t corresponds to List<String> and
duke@1 1819 // t.tsym.type corresponds to List<E>.
duke@1 1820 // The reason t must be parameterized type is
duke@1 1821 // that completion will happen as a side
duke@1 1822 // effect of calling
duke@1 1823 // ClassSymbol.getInterfaces. Since
duke@1 1824 // t.interfaces_field is null after
duke@1 1825 // completion, we can assume that t is not the
duke@1 1826 // type of a class/interface declaration.
jjg@816 1827 Assert.check(t != t.tsym.type, t);
duke@1 1828 List<Type> actuals = t.allparams();
duke@1 1829 List<Type> formals = t.tsym.type.allparams();
mcimadamore@30 1830 if (t.hasErasedSupertypes()) {
mcimadamore@30 1831 t.interfaces_field = erasureRecursive(interfaces);
mcimadamore@30 1832 } else if (formals.nonEmpty()) {
duke@1 1833 t.interfaces_field =
duke@1 1834 upperBounds(subst(interfaces, formals, actuals));
duke@1 1835 }
mcimadamore@30 1836 else {
mcimadamore@30 1837 t.interfaces_field = interfaces;
mcimadamore@30 1838 }
duke@1 1839 }
duke@1 1840 }
duke@1 1841 return t.interfaces_field;
duke@1 1842 }
duke@1 1843
duke@1 1844 @Override
duke@1 1845 public List<Type> visitTypeVar(TypeVar t, Void ignored) {
duke@1 1846 if (t.bound.isCompound())
duke@1 1847 return interfaces(t.bound);
duke@1 1848
duke@1 1849 if (t.bound.isInterface())
duke@1 1850 return List.of(t.bound);
duke@1 1851
duke@1 1852 return List.nil();
duke@1 1853 }
duke@1 1854 };
duke@1 1855 // </editor-fold>
duke@1 1856
duke@1 1857 // <editor-fold defaultstate="collapsed" desc="isDerivedRaw">
duke@1 1858 Map<Type,Boolean> isDerivedRawCache = new HashMap<Type,Boolean>();
duke@1 1859
duke@1 1860 public boolean isDerivedRaw(Type t) {
duke@1 1861 Boolean result = isDerivedRawCache.get(t);
duke@1 1862 if (result == null) {
duke@1 1863 result = isDerivedRawInternal(t);
duke@1 1864 isDerivedRawCache.put(t, result);
duke@1 1865 }
duke@1 1866 return result;
duke@1 1867 }
duke@1 1868
duke@1 1869 public boolean isDerivedRawInternal(Type t) {
duke@1 1870 if (t.isErroneous())
duke@1 1871 return false;
duke@1 1872 return
duke@1 1873 t.isRaw() ||
duke@1 1874 supertype(t) != null && isDerivedRaw(supertype(t)) ||
duke@1 1875 isDerivedRaw(interfaces(t));
duke@1 1876 }
duke@1 1877
duke@1 1878 public boolean isDerivedRaw(List<Type> ts) {
duke@1 1879 List<Type> l = ts;
duke@1 1880 while (l.nonEmpty() && !isDerivedRaw(l.head)) l = l.tail;
duke@1 1881 return l.nonEmpty();
duke@1 1882 }
duke@1 1883 // </editor-fold>
duke@1 1884
duke@1 1885 // <editor-fold defaultstate="collapsed" desc="setBounds">
duke@1 1886 /**
duke@1 1887 * Set the bounds field of the given type variable to reflect a
duke@1 1888 * (possibly multiple) list of bounds.
duke@1 1889 * @param t a type variable
duke@1 1890 * @param bounds the bounds, must be nonempty
duke@1 1891 * @param supertype is objectType if all bounds are interfaces,
duke@1 1892 * null otherwise.
duke@1 1893 */
duke@1 1894 public void setBounds(TypeVar t, List<Type> bounds, Type supertype) {
duke@1 1895 if (bounds.tail.isEmpty())
duke@1 1896 t.bound = bounds.head;
duke@1 1897 else
duke@1 1898 t.bound = makeCompoundType(bounds, supertype);
duke@1 1899 t.rank_field = -1;
duke@1 1900 }
duke@1 1901
duke@1 1902 /**
duke@1 1903 * Same as {@link #setBounds(Type.TypeVar,List,Type)}, except that
mcimadamore@563 1904 * third parameter is computed directly, as follows: if all
mcimadamore@563 1905 * all bounds are interface types, the computed supertype is Object,
mcimadamore@563 1906 * otherwise the supertype is simply left null (in this case, the supertype
mcimadamore@563 1907 * is assumed to be the head of the bound list passed as second argument).
mcimadamore@563 1908 * Note that this check might cause a symbol completion. Hence, this version of
duke@1 1909 * setBounds may not be called during a classfile read.
duke@1 1910 */
duke@1 1911 public void setBounds(TypeVar t, List<Type> bounds) {
duke@1 1912 Type supertype = (bounds.head.tsym.flags() & INTERFACE) != 0 ?
mcimadamore@563 1913 syms.objectType : null;
duke@1 1914 setBounds(t, bounds, supertype);
duke@1 1915 t.rank_field = -1;
duke@1 1916 }
duke@1 1917 // </editor-fold>
duke@1 1918
duke@1 1919 // <editor-fold defaultstate="collapsed" desc="getBounds">
duke@1 1920 /**
duke@1 1921 * Return list of bounds of the given type variable.
duke@1 1922 */
duke@1 1923 public List<Type> getBounds(TypeVar t) {
duke@1 1924 if (t.bound.isErroneous() || !t.bound.isCompound())
duke@1 1925 return List.of(t.bound);
duke@1 1926 else if ((erasure(t).tsym.flags() & INTERFACE) == 0)
duke@1 1927 return interfaces(t).prepend(supertype(t));
duke@1 1928 else
duke@1 1929 // No superclass was given in bounds.
duke@1 1930 // In this case, supertype is Object, erasure is first interface.
duke@1 1931 return interfaces(t);
duke@1 1932 }
duke@1 1933 // </editor-fold>
duke@1 1934
duke@1 1935 // <editor-fold defaultstate="collapsed" desc="classBound">
duke@1 1936 /**
duke@1 1937 * If the given type is a (possibly selected) type variable,
duke@1 1938 * return the bounding class of this type, otherwise return the
duke@1 1939 * type itself.
duke@1 1940 */
duke@1 1941 public Type classBound(Type t) {
duke@1 1942 return classBound.visit(t);
duke@1 1943 }
duke@1 1944 // where
duke@1 1945 private UnaryVisitor<Type> classBound = new UnaryVisitor<Type>() {
duke@1 1946
duke@1 1947 public Type visitType(Type t, Void ignored) {
duke@1 1948 return t;
duke@1 1949 }
duke@1 1950
duke@1 1951 @Override
duke@1 1952 public Type visitClassType(ClassType t, Void ignored) {
duke@1 1953 Type outer1 = classBound(t.getEnclosingType());
duke@1 1954 if (outer1 != t.getEnclosingType())
duke@1 1955 return new ClassType(outer1, t.getTypeArguments(), t.tsym);
duke@1 1956 else
duke@1 1957 return t;
duke@1 1958 }
duke@1 1959
duke@1 1960 @Override
duke@1 1961 public Type visitTypeVar(TypeVar t, Void ignored) {
duke@1 1962 return classBound(supertype(t));
duke@1 1963 }
duke@1 1964
duke@1 1965 @Override
duke@1 1966 public Type visitErrorType(ErrorType t, Void ignored) {
duke@1 1967 return t;
duke@1 1968 }
duke@1 1969 };
duke@1 1970 // </editor-fold>
duke@1 1971
duke@1 1972 // <editor-fold defaultstate="collapsed" desc="sub signature / override equivalence">
duke@1 1973 /**
duke@1 1974 * Returns true iff the first signature is a <em>sub
duke@1 1975 * signature</em> of the other. This is <b>not</b> an equivalence
duke@1 1976 * relation.
duke@1 1977 *
jjh@972 1978 * @jls section 8.4.2.
duke@1 1979 * @see #overrideEquivalent(Type t, Type s)
duke@1 1980 * @param t first signature (possibly raw).
duke@1 1981 * @param s second signature (could be subjected to erasure).
duke@1 1982 * @return true if t is a sub signature of s.
duke@1 1983 */
duke@1 1984 public boolean isSubSignature(Type t, Type s) {
mcimadamore@907 1985 return isSubSignature(t, s, true);
mcimadamore@907 1986 }
mcimadamore@907 1987
mcimadamore@907 1988 public boolean isSubSignature(Type t, Type s, boolean strict) {
mcimadamore@907 1989 return hasSameArgs(t, s, strict) || hasSameArgs(t, erasure(s), strict);
duke@1 1990 }
duke@1 1991
duke@1 1992 /**
duke@1 1993 * Returns true iff these signatures are related by <em>override
duke@1 1994 * equivalence</em>. This is the natural extension of
duke@1 1995 * isSubSignature to an equivalence relation.
duke@1 1996 *
jjh@972 1997 * @jls section 8.4.2.
duke@1 1998 * @see #isSubSignature(Type t, Type s)
duke@1 1999 * @param t a signature (possible raw, could be subjected to
duke@1 2000 * erasure).
duke@1 2001 * @param s a signature (possible raw, could be subjected to
duke@1 2002 * erasure).
duke@1 2003 * @return true if either argument is a sub signature of the other.
duke@1 2004 */
duke@1 2005 public boolean overrideEquivalent(Type t, Type s) {
duke@1 2006 return hasSameArgs(t, s) ||
duke@1 2007 hasSameArgs(t, erasure(s)) || hasSameArgs(erasure(t), s);
duke@1 2008 }
duke@1 2009
mcimadamore@673 2010 // <editor-fold defaultstate="collapsed" desc="Determining method implementation in given site">
mcimadamore@673 2011 class ImplementationCache {
mcimadamore@673 2012
mcimadamore@673 2013 private WeakHashMap<MethodSymbol, SoftReference<Map<TypeSymbol, Entry>>> _map =
mcimadamore@673 2014 new WeakHashMap<MethodSymbol, SoftReference<Map<TypeSymbol, Entry>>>();
mcimadamore@673 2015
mcimadamore@673 2016 class Entry {
mcimadamore@673 2017 final MethodSymbol cachedImpl;
mcimadamore@673 2018 final Filter<Symbol> implFilter;
mcimadamore@673 2019 final boolean checkResult;
mcimadamore@877 2020 final int prevMark;
mcimadamore@673 2021
mcimadamore@673 2022 public Entry(MethodSymbol cachedImpl,
mcimadamore@673 2023 Filter<Symbol> scopeFilter,
mcimadamore@877 2024 boolean checkResult,
mcimadamore@877 2025 int prevMark) {
mcimadamore@673 2026 this.cachedImpl = cachedImpl;
mcimadamore@673 2027 this.implFilter = scopeFilter;
mcimadamore@673 2028 this.checkResult = checkResult;
mcimadamore@877 2029 this.prevMark = prevMark;
mcimadamore@673 2030 }
mcimadamore@673 2031
mcimadamore@877 2032 boolean matches(Filter<Symbol> scopeFilter, boolean checkResult, int mark) {
mcimadamore@673 2033 return this.implFilter == scopeFilter &&
mcimadamore@877 2034 this.checkResult == checkResult &&
mcimadamore@877 2035 this.prevMark == mark;
mcimadamore@673 2036 }
mcimadamore@341 2037 }
mcimadamore@673 2038
mcimadamore@858 2039 MethodSymbol get(MethodSymbol ms, TypeSymbol origin, boolean checkResult, Filter<Symbol> implFilter) {
mcimadamore@673 2040 SoftReference<Map<TypeSymbol, Entry>> ref_cache = _map.get(ms);
mcimadamore@673 2041 Map<TypeSymbol, Entry> cache = ref_cache != null ? ref_cache.get() : null;
mcimadamore@673 2042 if (cache == null) {
mcimadamore@673 2043 cache = new HashMap<TypeSymbol, Entry>();
mcimadamore@673 2044 _map.put(ms, new SoftReference<Map<TypeSymbol, Entry>>(cache));
mcimadamore@673 2045 }
mcimadamore@673 2046 Entry e = cache.get(origin);
mcimadamore@1015 2047 CompoundScope members = membersClosure(origin.type, true);
mcimadamore@673 2048 if (e == null ||
mcimadamore@877 2049 !e.matches(implFilter, checkResult, members.getMark())) {
mcimadamore@877 2050 MethodSymbol impl = implementationInternal(ms, origin, checkResult, implFilter);
mcimadamore@877 2051 cache.put(origin, new Entry(impl, implFilter, checkResult, members.getMark()));
mcimadamore@673 2052 return impl;
mcimadamore@673 2053 }
mcimadamore@673 2054 else {
mcimadamore@673 2055 return e.cachedImpl;
mcimadamore@673 2056 }
mcimadamore@673 2057 }
mcimadamore@673 2058
mcimadamore@877 2059 private MethodSymbol implementationInternal(MethodSymbol ms, TypeSymbol origin, boolean checkResult, Filter<Symbol> implFilter) {
mcimadamore@877 2060 for (Type t = origin.type; t.tag == CLASS || t.tag == TYPEVAR; t = supertype(t)) {
mcimadamore@341 2061 while (t.tag == TYPEVAR)
mcimadamore@341 2062 t = t.getUpperBound();
mcimadamore@341 2063 TypeSymbol c = t.tsym;
mcimadamore@673 2064 for (Scope.Entry e = c.members().lookup(ms.name, implFilter);
mcimadamore@341 2065 e.scope != null;
mcimadamore@780 2066 e = e.next(implFilter)) {
mcimadamore@673 2067 if (e.sym != null &&
mcimadamore@877 2068 e.sym.overrides(ms, origin, Types.this, checkResult))
mcimadamore@673 2069 return (MethodSymbol)e.sym;
mcimadamore@341 2070 }
mcimadamore@341 2071 }
mcimadamore@673 2072 return null;
mcimadamore@341 2073 }
mcimadamore@341 2074 }
mcimadamore@341 2075
mcimadamore@673 2076 private ImplementationCache implCache = new ImplementationCache();
mcimadamore@673 2077
mcimadamore@858 2078 public MethodSymbol implementation(MethodSymbol ms, TypeSymbol origin, boolean checkResult, Filter<Symbol> implFilter) {
mcimadamore@858 2079 return implCache.get(ms, origin, checkResult, implFilter);
mcimadamore@673 2080 }
mcimadamore@673 2081 // </editor-fold>
mcimadamore@673 2082
mcimadamore@858 2083 // <editor-fold defaultstate="collapsed" desc="compute transitive closure of all members in given site">
mcimadamore@1015 2084 class MembersClosureCache extends SimpleVisitor<CompoundScope, Boolean> {
mcimadamore@1015 2085
mcimadamore@1015 2086 private WeakHashMap<TypeSymbol, Entry> _map =
mcimadamore@1015 2087 new WeakHashMap<TypeSymbol, Entry>();
mcimadamore@1015 2088
mcimadamore@1015 2089 class Entry {
mcimadamore@1015 2090 final boolean skipInterfaces;
mcimadamore@1015 2091 final CompoundScope compoundScope;
mcimadamore@1015 2092
mcimadamore@1015 2093 public Entry(boolean skipInterfaces, CompoundScope compoundScope) {
mcimadamore@1015 2094 this.skipInterfaces = skipInterfaces;
mcimadamore@1015 2095 this.compoundScope = compoundScope;
mcimadamore@1015 2096 }
mcimadamore@1015 2097
mcimadamore@1015 2098 boolean matches(boolean skipInterfaces) {
mcimadamore@1015 2099 return this.skipInterfaces == skipInterfaces;
mcimadamore@1015 2100 }
mcimadamore@1015 2101 }
mcimadamore@1015 2102
mcimadamore@1072 2103 List<TypeSymbol> seenTypes = List.nil();
mcimadamore@1072 2104
mcimadamore@1015 2105 /** members closure visitor methods **/
mcimadamore@1015 2106
mcimadamore@1015 2107 public CompoundScope visitType(Type t, Boolean skipInterface) {
mcimadamore@858 2108 return null;
mcimadamore@858 2109 }
mcimadamore@858 2110
mcimadamore@858 2111 @Override
mcimadamore@1015 2112 public CompoundScope visitClassType(ClassType t, Boolean skipInterface) {
mcimadamore@1072 2113 if (seenTypes.contains(t.tsym)) {
mcimadamore@1072 2114 //this is possible when an interface is implemented in multiple
mcimadamore@1072 2115 //superclasses, or when a classs hierarchy is circular - in such
mcimadamore@1072 2116 //cases we don't need to recurse (empty scope is returned)
mcimadamore@1072 2117 return new CompoundScope(t.tsym);
mcimadamore@1072 2118 }
mcimadamore@1072 2119 try {
mcimadamore@1072 2120 seenTypes = seenTypes.prepend(t.tsym);
mcimadamore@1072 2121 ClassSymbol csym = (ClassSymbol)t.tsym;
mcimadamore@1072 2122 Entry e = _map.get(csym);
mcimadamore@1072 2123 if (e == null || !e.matches(skipInterface)) {
mcimadamore@1072 2124 CompoundScope membersClosure = new CompoundScope(csym);
mcimadamore@1072 2125 if (!skipInterface) {
mcimadamore@1072 2126 for (Type i : interfaces(t)) {
mcimadamore@1072 2127 membersClosure.addSubScope(visit(i, skipInterface));
mcimadamore@1072 2128 }
mcimadamore@1015 2129 }
mcimadamore@1072 2130 membersClosure.addSubScope(visit(supertype(t), skipInterface));
mcimadamore@1072 2131 membersClosure.addSubScope(csym.members());
mcimadamore@1072 2132 e = new Entry(skipInterface, membersClosure);
mcimadamore@1072 2133 _map.put(csym, e);
mcimadamore@858 2134 }
mcimadamore@1072 2135 return e.compoundScope;
mcimadamore@858 2136 }
mcimadamore@1072 2137 finally {
mcimadamore@1072 2138 seenTypes = seenTypes.tail;
mcimadamore@1072 2139 }
mcimadamore@858 2140 }
mcimadamore@858 2141
mcimadamore@858 2142 @Override
mcimadamore@1015 2143 public CompoundScope visitTypeVar(TypeVar t, Boolean skipInterface) {
mcimadamore@1015 2144 return visit(t.getUpperBound(), skipInterface);
mcimadamore@858 2145 }
mcimadamore@1015 2146 }
mcimadamore@1015 2147
mcimadamore@1015 2148 private MembersClosureCache membersCache = new MembersClosureCache();
mcimadamore@1015 2149
mcimadamore@1015 2150 public CompoundScope membersClosure(Type site, boolean skipInterface) {
mcimadamore@1015 2151 return membersCache.visit(site, skipInterface);
mcimadamore@1015 2152 }
mcimadamore@858 2153 // </editor-fold>
mcimadamore@858 2154
duke@1 2155 /**
duke@1 2156 * Does t have the same arguments as s? It is assumed that both
duke@1 2157 * types are (possibly polymorphic) method types. Monomorphic
duke@1 2158 * method types "have the same arguments", if their argument lists
duke@1 2159 * are equal. Polymorphic method types "have the same arguments",
duke@1 2160 * if they have the same arguments after renaming all type
duke@1 2161 * variables of one to corresponding type variables in the other,
duke@1 2162 * where correspondence is by position in the type parameter list.
duke@1 2163 */
duke@1 2164 public boolean hasSameArgs(Type t, Type s) {
mcimadamore@907 2165 return hasSameArgs(t, s, true);
mcimadamore@907 2166 }
mcimadamore@907 2167
mcimadamore@907 2168 public boolean hasSameArgs(Type t, Type s, boolean strict) {
mcimadamore@907 2169 return hasSameArgs(t, s, strict ? hasSameArgs_strict : hasSameArgs_nonstrict);
mcimadamore@907 2170 }
mcimadamore@907 2171
mcimadamore@907 2172 private boolean hasSameArgs(Type t, Type s, TypeRelation hasSameArgs) {
duke@1 2173 return hasSameArgs.visit(t, s);
duke@1 2174 }
duke@1 2175 // where
mcimadamore@907 2176 private class HasSameArgs extends TypeRelation {
mcimadamore@907 2177
mcimadamore@907 2178 boolean strict;
mcimadamore@907 2179
mcimadamore@907 2180 public HasSameArgs(boolean strict) {
mcimadamore@907 2181 this.strict = strict;
mcimadamore@907 2182 }
duke@1 2183
duke@1 2184 public Boolean visitType(Type t, Type s) {
duke@1 2185 throw new AssertionError();
duke@1 2186 }
duke@1 2187
duke@1 2188 @Override
duke@1 2189 public Boolean visitMethodType(MethodType t, Type s) {
duke@1 2190 return s.tag == METHOD
duke@1 2191 && containsTypeEquivalent(t.argtypes, s.getParameterTypes());
duke@1 2192 }
duke@1 2193
duke@1 2194 @Override
duke@1 2195 public Boolean visitForAll(ForAll t, Type s) {
duke@1 2196 if (s.tag != FORALL)
mcimadamore@907 2197 return strict ? false : visitMethodType(t.asMethodType(), s);
duke@1 2198
duke@1 2199 ForAll forAll = (ForAll)s;
duke@1 2200 return hasSameBounds(t, forAll)
duke@1 2201 && visit(t.qtype, subst(forAll.qtype, forAll.tvars, t.tvars));
duke@1 2202 }
duke@1 2203
duke@1 2204 @Override
duke@1 2205 public Boolean visitErrorType(ErrorType t, Type s) {
duke@1 2206 return false;
duke@1 2207 }
duke@1 2208 };
mcimadamore@907 2209
mcimadamore@907 2210 TypeRelation hasSameArgs_strict = new HasSameArgs(true);
mcimadamore@907 2211 TypeRelation hasSameArgs_nonstrict = new HasSameArgs(false);
mcimadamore@907 2212
duke@1 2213 // </editor-fold>
duke@1 2214
duke@1 2215 // <editor-fold defaultstate="collapsed" desc="subst">
duke@1 2216 public List<Type> subst(List<Type> ts,
duke@1 2217 List<Type> from,
duke@1 2218 List<Type> to) {
duke@1 2219 return new Subst(from, to).subst(ts);
duke@1 2220 }
duke@1 2221
duke@1 2222 /**
duke@1 2223 * Substitute all occurrences of a type in `from' with the
duke@1 2224 * corresponding type in `to' in 't'. Match lists `from' and `to'
duke@1 2225 * from the right: If lists have different length, discard leading
duke@1 2226 * elements of the longer list.
duke@1 2227 */
duke@1 2228 public Type subst(Type t, List<Type> from, List<Type> to) {
duke@1 2229 return new Subst(from, to).subst(t);
duke@1 2230 }
duke@1 2231
duke@1 2232 private class Subst extends UnaryVisitor<Type> {
duke@1 2233 List<Type> from;
duke@1 2234 List<Type> to;
duke@1 2235
duke@1 2236 public Subst(List<Type> from, List<Type> to) {
duke@1 2237 int fromLength = from.length();
duke@1 2238 int toLength = to.length();
duke@1 2239 while (fromLength > toLength) {
duke@1 2240 fromLength--;
duke@1 2241 from = from.tail;
duke@1 2242 }
duke@1 2243 while (fromLength < toLength) {
duke@1 2244 toLength--;
duke@1 2245 to = to.tail;
duke@1 2246 }
duke@1 2247 this.from = from;
duke@1 2248 this.to = to;
duke@1 2249 }
duke@1 2250
duke@1 2251 Type subst(Type t) {
duke@1 2252 if (from.tail == null)
duke@1 2253 return t;
duke@1 2254 else
duke@1 2255 return visit(t);
mcimadamore@238 2256 }
duke@1 2257
duke@1 2258 List<Type> subst(List<Type> ts) {
duke@1 2259 if (from.tail == null)
duke@1 2260 return ts;
duke@1 2261 boolean wild = false;
duke@1 2262 if (ts.nonEmpty() && from.nonEmpty()) {
duke@1 2263 Type head1 = subst(ts.head);
duke@1 2264 List<Type> tail1 = subst(ts.tail);
duke@1 2265 if (head1 != ts.head || tail1 != ts.tail)
duke@1 2266 return tail1.prepend(head1);
duke@1 2267 }
duke@1 2268 return ts;
duke@1 2269 }
duke@1 2270
duke@1 2271 public Type visitType(Type t, Void ignored) {
duke@1 2272 return t;
duke@1 2273 }
duke@1 2274
duke@1 2275 @Override
duke@1 2276 public Type visitMethodType(MethodType t, Void ignored) {
duke@1 2277 List<Type> argtypes = subst(t.argtypes);
duke@1 2278 Type restype = subst(t.restype);
duke@1 2279 List<Type> thrown = subst(t.thrown);
duke@1 2280 if (argtypes == t.argtypes &&
duke@1 2281 restype == t.restype &&
duke@1 2282 thrown == t.thrown)
duke@1 2283 return t;
duke@1 2284 else
duke@1 2285 return new MethodType(argtypes, restype, thrown, t.tsym);
duke@1 2286 }
duke@1 2287
duke@1 2288 @Override
duke@1 2289 public Type visitTypeVar(TypeVar t, Void ignored) {
duke@1 2290 for (List<Type> from = this.from, to = this.to;
duke@1 2291 from.nonEmpty();
duke@1 2292 from = from.tail, to = to.tail) {
duke@1 2293 if (t == from.head) {
duke@1 2294 return to.head.withTypeVar(t);
duke@1 2295 }
duke@1 2296 }
duke@1 2297 return t;
duke@1 2298 }
duke@1 2299
duke@1 2300 @Override
duke@1 2301 public Type visitClassType(ClassType t, Void ignored) {
duke@1 2302 if (!t.isCompound()) {
duke@1 2303 List<Type> typarams = t.getTypeArguments();
duke@1 2304 List<Type> typarams1 = subst(typarams);
duke@1 2305 Type outer = t.getEnclosingType();
duke@1 2306 Type outer1 = subst(outer);
duke@1 2307 if (typarams1 == typarams && outer1 == outer)
duke@1 2308 return t;
duke@1 2309 else
duke@1 2310 return new ClassType(outer1, typarams1, t.tsym);
duke@1 2311 } else {
duke@1 2312 Type st = subst(supertype(t));
duke@1 2313 List<Type> is = upperBounds(subst(interfaces(t)));
duke@1 2314 if (st == supertype(t) && is == interfaces(t))
duke@1 2315 return t;
duke@1 2316 else
duke@1 2317 return makeCompoundType(is.prepend(st));
duke@1 2318 }
duke@1 2319 }
duke@1 2320
duke@1 2321 @Override
duke@1 2322 public Type visitWildcardType(WildcardType t, Void ignored) {
duke@1 2323 Type bound = t.type;
duke@1 2324 if (t.kind != BoundKind.UNBOUND)
duke@1 2325 bound = subst(bound);
duke@1 2326 if (bound == t.type) {
duke@1 2327 return t;
duke@1 2328 } else {
duke@1 2329 if (t.isExtendsBound() && bound.isExtendsBound())
duke@1 2330 bound = upperBound(bound);
duke@1 2331 return new WildcardType(bound, t.kind, syms.boundClass, t.bound);
duke@1 2332 }
duke@1 2333 }
duke@1 2334
duke@1 2335 @Override
duke@1 2336 public Type visitArrayType(ArrayType t, Void ignored) {
duke@1 2337 Type elemtype = subst(t.elemtype);
duke@1 2338 if (elemtype == t.elemtype)
duke@1 2339 return t;
duke@1 2340 else
mcimadamore@996 2341 return new ArrayType(upperBound(elemtype), t.tsym);
duke@1 2342 }
duke@1 2343
duke@1 2344 @Override
duke@1 2345 public Type visitForAll(ForAll t, Void ignored) {
mcimadamore@846 2346 if (Type.containsAny(to, t.tvars)) {
mcimadamore@846 2347 //perform alpha-renaming of free-variables in 't'
mcimadamore@846 2348 //if 'to' types contain variables that are free in 't'
mcimadamore@846 2349 List<Type> freevars = newInstances(t.tvars);
mcimadamore@846 2350 t = new ForAll(freevars,
mcimadamore@846 2351 Types.this.subst(t.qtype, t.tvars, freevars));
mcimadamore@846 2352 }
duke@1 2353 List<Type> tvars1 = substBounds(t.tvars, from, to);
duke@1 2354 Type qtype1 = subst(t.qtype);
duke@1 2355 if (tvars1 == t.tvars && qtype1 == t.qtype) {
duke@1 2356 return t;
duke@1 2357 } else if (tvars1 == t.tvars) {
duke@1 2358 return new ForAll(tvars1, qtype1);
duke@1 2359 } else {
duke@1 2360 return new ForAll(tvars1, Types.this.subst(qtype1, t.tvars, tvars1));
duke@1 2361 }
duke@1 2362 }
duke@1 2363
duke@1 2364 @Override
duke@1 2365 public Type visitErrorType(ErrorType t, Void ignored) {
duke@1 2366 return t;
duke@1 2367 }
duke@1 2368 }
duke@1 2369
duke@1 2370 public List<Type> substBounds(List<Type> tvars,
duke@1 2371 List<Type> from,
duke@1 2372 List<Type> to) {
duke@1 2373 if (tvars.isEmpty())
duke@1 2374 return tvars;
duke@1 2375 ListBuffer<Type> newBoundsBuf = lb();
duke@1 2376 boolean changed = false;
duke@1 2377 // calculate new bounds
duke@1 2378 for (Type t : tvars) {
duke@1 2379 TypeVar tv = (TypeVar) t;
duke@1 2380 Type bound = subst(tv.bound, from, to);
duke@1 2381 if (bound != tv.bound)
duke@1 2382 changed = true;
duke@1 2383 newBoundsBuf.append(bound);
duke@1 2384 }
duke@1 2385 if (!changed)
duke@1 2386 return tvars;
duke@1 2387 ListBuffer<Type> newTvars = lb();
duke@1 2388 // create new type variables without bounds
duke@1 2389 for (Type t : tvars) {
duke@1 2390 newTvars.append(new TypeVar(t.tsym, null, syms.botType));
duke@1 2391 }
duke@1 2392 // the new bounds should use the new type variables in place
duke@1 2393 // of the old
duke@1 2394 List<Type> newBounds = newBoundsBuf.toList();
duke@1 2395 from = tvars;
duke@1 2396 to = newTvars.toList();
duke@1 2397 for (; !newBounds.isEmpty(); newBounds = newBounds.tail) {
duke@1 2398 newBounds.head = subst(newBounds.head, from, to);
duke@1 2399 }
duke@1 2400 newBounds = newBoundsBuf.toList();
duke@1 2401 // set the bounds of new type variables to the new bounds
duke@1 2402 for (Type t : newTvars.toList()) {
duke@1 2403 TypeVar tv = (TypeVar) t;
duke@1 2404 tv.bound = newBounds.head;
duke@1 2405 newBounds = newBounds.tail;
duke@1 2406 }
duke@1 2407 return newTvars.toList();
duke@1 2408 }
duke@1 2409
duke@1 2410 public TypeVar substBound(TypeVar t, List<Type> from, List<Type> to) {
duke@1 2411 Type bound1 = subst(t.bound, from, to);
duke@1 2412 if (bound1 == t.bound)
duke@1 2413 return t;
mcimadamore@212 2414 else {
mcimadamore@212 2415 // create new type variable without bounds
mcimadamore@212 2416 TypeVar tv = new TypeVar(t.tsym, null, syms.botType);
mcimadamore@212 2417 // the new bound should use the new type variable in place
mcimadamore@212 2418 // of the old
mcimadamore@212 2419 tv.bound = subst(bound1, List.<Type>of(t), List.<Type>of(tv));
mcimadamore@212 2420 return tv;
mcimadamore@212 2421 }
duke@1 2422 }
duke@1 2423 // </editor-fold>
duke@1 2424
duke@1 2425 // <editor-fold defaultstate="collapsed" desc="hasSameBounds">
duke@1 2426 /**
duke@1 2427 * Does t have the same bounds for quantified variables as s?
duke@1 2428 */
duke@1 2429 boolean hasSameBounds(ForAll t, ForAll s) {
duke@1 2430 List<Type> l1 = t.tvars;
duke@1 2431 List<Type> l2 = s.tvars;
duke@1 2432 while (l1.nonEmpty() && l2.nonEmpty() &&
duke@1 2433 isSameType(l1.head.getUpperBound(),
duke@1 2434 subst(l2.head.getUpperBound(),
duke@1 2435 s.tvars,
duke@1 2436 t.tvars))) {
duke@1 2437 l1 = l1.tail;
duke@1 2438 l2 = l2.tail;
duke@1 2439 }
duke@1 2440 return l1.isEmpty() && l2.isEmpty();
duke@1 2441 }
duke@1 2442 // </editor-fold>
duke@1 2443
duke@1 2444 // <editor-fold defaultstate="collapsed" desc="newInstances">
duke@1 2445 /** Create new vector of type variables from list of variables
duke@1 2446 * changing all recursive bounds from old to new list.
duke@1 2447 */
duke@1 2448 public List<Type> newInstances(List<Type> tvars) {
duke@1 2449 List<Type> tvars1 = Type.map(tvars, newInstanceFun);
duke@1 2450 for (List<Type> l = tvars1; l.nonEmpty(); l = l.tail) {
duke@1 2451 TypeVar tv = (TypeVar) l.head;
duke@1 2452 tv.bound = subst(tv.bound, tvars, tvars1);
duke@1 2453 }
duke@1 2454 return tvars1;
duke@1 2455 }
duke@1 2456 static private Mapping newInstanceFun = new Mapping("newInstanceFun") {
duke@1 2457 public Type apply(Type t) { return new TypeVar(t.tsym, t.getUpperBound(), t.getLowerBound()); }
duke@1 2458 };
duke@1 2459 // </editor-fold>
duke@1 2460
dlsmith@880 2461 public Type createMethodTypeWithParameters(Type original, List<Type> newParams) {
dlsmith@880 2462 return original.accept(methodWithParameters, newParams);
dlsmith@880 2463 }
dlsmith@880 2464 // where
dlsmith@880 2465 private final MapVisitor<List<Type>> methodWithParameters = new MapVisitor<List<Type>>() {
dlsmith@880 2466 public Type visitType(Type t, List<Type> newParams) {
dlsmith@880 2467 throw new IllegalArgumentException("Not a method type: " + t);
dlsmith@880 2468 }
dlsmith@880 2469 public Type visitMethodType(MethodType t, List<Type> newParams) {
dlsmith@880 2470 return new MethodType(newParams, t.restype, t.thrown, t.tsym);
dlsmith@880 2471 }
dlsmith@880 2472 public Type visitForAll(ForAll t, List<Type> newParams) {
dlsmith@880 2473 return new ForAll(t.tvars, t.qtype.accept(this, newParams));
dlsmith@880 2474 }
dlsmith@880 2475 };
dlsmith@880 2476
dlsmith@880 2477 public Type createMethodTypeWithThrown(Type original, List<Type> newThrown) {
dlsmith@880 2478 return original.accept(methodWithThrown, newThrown);
dlsmith@880 2479 }
dlsmith@880 2480 // where
dlsmith@880 2481 private final MapVisitor<List<Type>> methodWithThrown = new MapVisitor<List<Type>>() {
dlsmith@880 2482 public Type visitType(Type t, List<Type> newThrown) {
dlsmith@880 2483 throw new IllegalArgumentException("Not a method type: " + t);
dlsmith@880 2484 }
dlsmith@880 2485 public Type visitMethodType(MethodType t, List<Type> newThrown) {
dlsmith@880 2486 return new MethodType(t.argtypes, t.restype, newThrown, t.tsym);
dlsmith@880 2487 }
dlsmith@880 2488 public Type visitForAll(ForAll t, List<Type> newThrown) {
dlsmith@880 2489 return new ForAll(t.tvars, t.qtype.accept(this, newThrown));
dlsmith@880 2490 }
dlsmith@880 2491 };
dlsmith@880 2492
mcimadamore@950 2493 public Type createMethodTypeWithReturn(Type original, Type newReturn) {
mcimadamore@950 2494 return original.accept(methodWithReturn, newReturn);
mcimadamore@950 2495 }
mcimadamore@950 2496 // where
mcimadamore@950 2497 private final MapVisitor<Type> methodWithReturn = new MapVisitor<Type>() {
mcimadamore@950 2498 public Type visitType(Type t, Type newReturn) {
mcimadamore@950 2499 throw new IllegalArgumentException("Not a method type: " + t);
mcimadamore@950 2500 }
mcimadamore@950 2501 public Type visitMethodType(MethodType t, Type newReturn) {
mcimadamore@950 2502 return new MethodType(t.argtypes, newReturn, t.thrown, t.tsym);
mcimadamore@950 2503 }
mcimadamore@950 2504 public Type visitForAll(ForAll t, Type newReturn) {
mcimadamore@950 2505 return new ForAll(t.tvars, t.qtype.accept(this, newReturn));
mcimadamore@950 2506 }
mcimadamore@950 2507 };
mcimadamore@950 2508
jjg@110 2509 // <editor-fold defaultstate="collapsed" desc="createErrorType">
jjg@110 2510 public Type createErrorType(Type originalType) {
jjg@110 2511 return new ErrorType(originalType, syms.errSymbol);
jjg@110 2512 }
jjg@110 2513
jjg@110 2514 public Type createErrorType(ClassSymbol c, Type originalType) {
jjg@110 2515 return new ErrorType(c, originalType);
jjg@110 2516 }
jjg@110 2517
jjg@110 2518 public Type createErrorType(Name name, TypeSymbol container, Type originalType) {
jjg@110 2519 return new ErrorType(name, container, originalType);
jjg@110 2520 }
jjg@110 2521 // </editor-fold>
jjg@110 2522
duke@1 2523 // <editor-fold defaultstate="collapsed" desc="rank">
duke@1 2524 /**
duke@1 2525 * The rank of a class is the length of the longest path between
duke@1 2526 * the class and java.lang.Object in the class inheritance
duke@1 2527 * graph. Undefined for all but reference types.
duke@1 2528 */
duke@1 2529 public int rank(Type t) {
duke@1 2530 switch(t.tag) {
duke@1 2531 case CLASS: {
duke@1 2532 ClassType cls = (ClassType)t;
duke@1 2533 if (cls.rank_field < 0) {
duke@1 2534 Name fullname = cls.tsym.getQualifiedName();
jjg@113 2535 if (fullname == names.java_lang_Object)
duke@1 2536 cls.rank_field = 0;
duke@1 2537 else {
duke@1 2538 int r = rank(supertype(cls));
duke@1 2539 for (List<Type> l = interfaces(cls);
duke@1 2540 l.nonEmpty();
duke@1 2541 l = l.tail) {
duke@1 2542 if (rank(l.head) > r)
duke@1 2543 r = rank(l.head);
duke@1 2544 }
duke@1 2545 cls.rank_field = r + 1;
duke@1 2546 }
duke@1 2547 }
duke@1 2548 return cls.rank_field;
duke@1 2549 }
duke@1 2550 case TYPEVAR: {
duke@1 2551 TypeVar tvar = (TypeVar)t;
duke@1 2552 if (tvar.rank_field < 0) {
duke@1 2553 int r = rank(supertype(tvar));
duke@1 2554 for (List<Type> l = interfaces(tvar);
duke@1 2555 l.nonEmpty();
duke@1 2556 l = l.tail) {
duke@1 2557 if (rank(l.head) > r) r = rank(l.head);
duke@1 2558 }
duke@1 2559 tvar.rank_field = r + 1;
duke@1 2560 }
duke@1 2561 return tvar.rank_field;
duke@1 2562 }
duke@1 2563 case ERROR:
duke@1 2564 return 0;
duke@1 2565 default:
duke@1 2566 throw new AssertionError();
duke@1 2567 }
duke@1 2568 }
duke@1 2569 // </editor-fold>
duke@1 2570
mcimadamore@121 2571 /**
mcimadamore@238 2572 * Helper method for generating a string representation of a given type
mcimadamore@121 2573 * accordingly to a given locale
mcimadamore@121 2574 */
mcimadamore@121 2575 public String toString(Type t, Locale locale) {
mcimadamore@238 2576 return Printer.createStandardPrinter(messages).visit(t, locale);
mcimadamore@121 2577 }
mcimadamore@121 2578
mcimadamore@121 2579 /**
mcimadamore@238 2580 * Helper method for generating a string representation of a given type
mcimadamore@121 2581 * accordingly to a given locale
mcimadamore@121 2582 */
mcimadamore@121 2583 public String toString(Symbol t, Locale locale) {
mcimadamore@238 2584 return Printer.createStandardPrinter(messages).visit(t, locale);
mcimadamore@121 2585 }
mcimadamore@121 2586
duke@1 2587 // <editor-fold defaultstate="collapsed" desc="toString">
duke@1 2588 /**
duke@1 2589 * This toString is slightly more descriptive than the one on Type.
mcimadamore@121 2590 *
mcimadamore@121 2591 * @deprecated Types.toString(Type t, Locale l) provides better support
mcimadamore@121 2592 * for localization
duke@1 2593 */
mcimadamore@121 2594 @Deprecated
duke@1 2595 public String toString(Type t) {
duke@1 2596 if (t.tag == FORALL) {
duke@1 2597 ForAll forAll = (ForAll)t;
duke@1 2598 return typaramsString(forAll.tvars) + forAll.qtype;
duke@1 2599 }
duke@1 2600 return "" + t;
duke@1 2601 }
duke@1 2602 // where
duke@1 2603 private String typaramsString(List<Type> tvars) {
jjg@904 2604 StringBuilder s = new StringBuilder();
duke@1 2605 s.append('<');
duke@1 2606 boolean first = true;
duke@1 2607 for (Type t : tvars) {
duke@1 2608 if (!first) s.append(", ");
duke@1 2609 first = false;
duke@1 2610 appendTyparamString(((TypeVar)t), s);
duke@1 2611 }
duke@1 2612 s.append('>');
duke@1 2613 return s.toString();
duke@1 2614 }
jjg@904 2615 private void appendTyparamString(TypeVar t, StringBuilder buf) {
duke@1 2616 buf.append(t);
duke@1 2617 if (t.bound == null ||
duke@1 2618 t.bound.tsym.getQualifiedName() == names.java_lang_Object)
duke@1 2619 return;
duke@1 2620 buf.append(" extends "); // Java syntax; no need for i18n
duke@1 2621 Type bound = t.bound;
duke@1 2622 if (!bound.isCompound()) {
duke@1 2623 buf.append(bound);
duke@1 2624 } else if ((erasure(t).tsym.flags() & INTERFACE) == 0) {
duke@1 2625 buf.append(supertype(t));
duke@1 2626 for (Type intf : interfaces(t)) {
duke@1 2627 buf.append('&');
duke@1 2628 buf.append(intf);
duke@1 2629 }
duke@1 2630 } else {
duke@1 2631 // No superclass was given in bounds.
duke@1 2632 // In this case, supertype is Object, erasure is first interface.
duke@1 2633 boolean first = true;
duke@1 2634 for (Type intf : interfaces(t)) {
duke@1 2635 if (!first) buf.append('&');
duke@1 2636 first = false;
duke@1 2637 buf.append(intf);
duke@1 2638 }
duke@1 2639 }
duke@1 2640 }
duke@1 2641 // </editor-fold>
duke@1 2642
duke@1 2643 // <editor-fold defaultstate="collapsed" desc="Determining least upper bounds of types">
duke@1 2644 /**
duke@1 2645 * A cache for closures.
duke@1 2646 *
duke@1 2647 * <p>A closure is a list of all the supertypes and interfaces of
duke@1 2648 * a class or interface type, ordered by ClassSymbol.precedes
duke@1 2649 * (that is, subclasses come first, arbitrary but fixed
duke@1 2650 * otherwise).
duke@1 2651 */
duke@1 2652 private Map<Type,List<Type>> closureCache = new HashMap<Type,List<Type>>();
duke@1 2653
duke@1 2654 /**
duke@1 2655 * Returns the closure of a class or interface type.
duke@1 2656 */
duke@1 2657 public List<Type> closure(Type t) {
duke@1 2658 List<Type> cl = closureCache.get(t);
duke@1 2659 if (cl == null) {
duke@1 2660 Type st = supertype(t);
duke@1 2661 if (!t.isCompound()) {
duke@1 2662 if (st.tag == CLASS) {
duke@1 2663 cl = insert(closure(st), t);
duke@1 2664 } else if (st.tag == TYPEVAR) {
duke@1 2665 cl = closure(st).prepend(t);
duke@1 2666 } else {
duke@1 2667 cl = List.of(t);
duke@1 2668 }
duke@1 2669 } else {
duke@1 2670 cl = closure(supertype(t));
duke@1 2671 }
duke@1 2672 for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail)
duke@1 2673 cl = union(cl, closure(l.head));
duke@1 2674 closureCache.put(t, cl);
duke@1 2675 }
duke@1 2676 return cl;
duke@1 2677 }
duke@1 2678
duke@1 2679 /**
duke@1 2680 * Insert a type in a closure
duke@1 2681 */
duke@1 2682 public List<Type> insert(List<Type> cl, Type t) {
duke@1 2683 if (cl.isEmpty() || t.tsym.precedes(cl.head.tsym, this)) {
duke@1 2684 return cl.prepend(t);
duke@1 2685 } else if (cl.head.tsym.precedes(t.tsym, this)) {
duke@1 2686 return insert(cl.tail, t).prepend(cl.head);
duke@1 2687 } else {
duke@1 2688 return cl;
duke@1 2689 }
duke@1 2690 }
duke@1 2691
duke@1 2692 /**
duke@1 2693 * Form the union of two closures
duke@1 2694 */
duke@1 2695 public List<Type> union(List<Type> cl1, List<Type> cl2) {
duke@1 2696 if (cl1.isEmpty()) {
duke@1 2697 return cl2;
duke@1 2698 } else if (cl2.isEmpty()) {
duke@1 2699 return cl1;
duke@1 2700 } else if (cl1.head.tsym.precedes(cl2.head.tsym, this)) {
duke@1 2701 return union(cl1.tail, cl2).prepend(cl1.head);
duke@1 2702 } else if (cl2.head.tsym.precedes(cl1.head.tsym, this)) {
duke@1 2703 return union(cl1, cl2.tail).prepend(cl2.head);
duke@1 2704 } else {
duke@1 2705 return union(cl1.tail, cl2.tail).prepend(cl1.head);
duke@1 2706 }
duke@1 2707 }
duke@1 2708
duke@1 2709 /**
duke@1 2710 * Intersect two closures
duke@1 2711 */
duke@1 2712 public List<Type> intersect(List<Type> cl1, List<Type> cl2) {
duke@1 2713 if (cl1 == cl2)
duke@1 2714 return cl1;
duke@1 2715 if (cl1.isEmpty() || cl2.isEmpty())
duke@1 2716 return List.nil();
duke@1 2717 if (cl1.head.tsym.precedes(cl2.head.tsym, this))
duke@1 2718 return intersect(cl1.tail, cl2);
duke@1 2719 if (cl2.head.tsym.precedes(cl1.head.tsym, this))
duke@1 2720 return intersect(cl1, cl2.tail);
duke@1 2721 if (isSameType(cl1.head, cl2.head))
duke@1 2722 return intersect(cl1.tail, cl2.tail).prepend(cl1.head);
duke@1 2723 if (cl1.head.tsym == cl2.head.tsym &&
duke@1 2724 cl1.head.tag == CLASS && cl2.head.tag == CLASS) {
duke@1 2725 if (cl1.head.isParameterized() && cl2.head.isParameterized()) {
duke@1 2726 Type merge = merge(cl1.head,cl2.head);
duke@1 2727 return intersect(cl1.tail, cl2.tail).prepend(merge);
duke@1 2728 }
duke@1 2729 if (cl1.head.isRaw() || cl2.head.isRaw())
duke@1 2730 return intersect(cl1.tail, cl2.tail).prepend(erasure(cl1.head));
duke@1 2731 }
duke@1 2732 return intersect(cl1.tail, cl2.tail);
duke@1 2733 }
duke@1 2734 // where
duke@1 2735 class TypePair {
duke@1 2736 final Type t1;
duke@1 2737 final Type t2;
duke@1 2738 TypePair(Type t1, Type t2) {
duke@1 2739 this.t1 = t1;
duke@1 2740 this.t2 = t2;
duke@1 2741 }
duke@1 2742 @Override
duke@1 2743 public int hashCode() {
jjg@507 2744 return 127 * Types.hashCode(t1) + Types.hashCode(t2);
duke@1 2745 }
duke@1 2746 @Override
duke@1 2747 public boolean equals(Object obj) {
duke@1 2748 if (!(obj instanceof TypePair))
duke@1 2749 return false;
duke@1 2750 TypePair typePair = (TypePair)obj;
duke@1 2751 return isSameType(t1, typePair.t1)
duke@1 2752 && isSameType(t2, typePair.t2);
duke@1 2753 }
duke@1 2754 }
duke@1 2755 Set<TypePair> mergeCache = new HashSet<TypePair>();
duke@1 2756 private Type merge(Type c1, Type c2) {
duke@1 2757 ClassType class1 = (ClassType) c1;
duke@1 2758 List<Type> act1 = class1.getTypeArguments();
duke@1 2759 ClassType class2 = (ClassType) c2;
duke@1 2760 List<Type> act2 = class2.getTypeArguments();
duke@1 2761 ListBuffer<Type> merged = new ListBuffer<Type>();
duke@1 2762 List<Type> typarams = class1.tsym.type.getTypeArguments();
duke@1 2763
duke@1 2764 while (act1.nonEmpty() && act2.nonEmpty() && typarams.nonEmpty()) {
duke@1 2765 if (containsType(act1.head, act2.head)) {
duke@1 2766 merged.append(act1.head);
duke@1 2767 } else if (containsType(act2.head, act1.head)) {
duke@1 2768 merged.append(act2.head);
duke@1 2769 } else {
duke@1 2770 TypePair pair = new TypePair(c1, c2);
duke@1 2771 Type m;
duke@1 2772 if (mergeCache.add(pair)) {
duke@1 2773 m = new WildcardType(lub(upperBound(act1.head),
duke@1 2774 upperBound(act2.head)),
duke@1 2775 BoundKind.EXTENDS,
duke@1 2776 syms.boundClass);
duke@1 2777 mergeCache.remove(pair);
duke@1 2778 } else {
duke@1 2779 m = new WildcardType(syms.objectType,
duke@1 2780 BoundKind.UNBOUND,
duke@1 2781 syms.boundClass);
duke@1 2782 }
duke@1 2783 merged.append(m.withTypeVar(typarams.head));
duke@1 2784 }
duke@1 2785 act1 = act1.tail;
duke@1 2786 act2 = act2.tail;
duke@1 2787 typarams = typarams.tail;
duke@1 2788 }
jjg@816 2789 Assert.check(act1.isEmpty() && act2.isEmpty() && typarams.isEmpty());
duke@1 2790 return new ClassType(class1.getEnclosingType(), merged.toList(), class1.tsym);
duke@1 2791 }
duke@1 2792
duke@1 2793 /**
duke@1 2794 * Return the minimum type of a closure, a compound type if no
duke@1 2795 * unique minimum exists.
duke@1 2796 */
duke@1 2797 private Type compoundMin(List<Type> cl) {
duke@1 2798 if (cl.isEmpty()) return syms.objectType;
duke@1 2799 List<Type> compound = closureMin(cl);
duke@1 2800 if (compound.isEmpty())
duke@1 2801 return null;
duke@1 2802 else if (compound.tail.isEmpty())
duke@1 2803 return compound.head;
duke@1 2804 else
duke@1 2805 return makeCompoundType(compound);
duke@1 2806 }
duke@1 2807
duke@1 2808 /**
duke@1 2809 * Return the minimum types of a closure, suitable for computing
duke@1 2810 * compoundMin or glb.
duke@1 2811 */
duke@1 2812 private List<Type> closureMin(List<Type> cl) {
duke@1 2813 ListBuffer<Type> classes = lb();
duke@1 2814 ListBuffer<Type> interfaces = lb();
duke@1 2815 while (!cl.isEmpty()) {
duke@1 2816 Type current = cl.head;
duke@1 2817 if (current.isInterface())
duke@1 2818 interfaces.append(current);
duke@1 2819 else
duke@1 2820 classes.append(current);
duke@1 2821 ListBuffer<Type> candidates = lb();
duke@1 2822 for (Type t : cl.tail) {
duke@1 2823 if (!isSubtypeNoCapture(current, t))
duke@1 2824 candidates.append(t);
duke@1 2825 }
duke@1 2826 cl = candidates.toList();
duke@1 2827 }
duke@1 2828 return classes.appendList(interfaces).toList();
duke@1 2829 }
duke@1 2830
duke@1 2831 /**
duke@1 2832 * Return the least upper bound of pair of types. if the lub does
duke@1 2833 * not exist return null.
duke@1 2834 */
duke@1 2835 public Type lub(Type t1, Type t2) {
duke@1 2836 return lub(List.of(t1, t2));
duke@1 2837 }
duke@1 2838
duke@1 2839 /**
duke@1 2840 * Return the least upper bound (lub) of set of types. If the lub
duke@1 2841 * does not exist return the type of null (bottom).
duke@1 2842 */
duke@1 2843 public Type lub(List<Type> ts) {
duke@1 2844 final int ARRAY_BOUND = 1;
duke@1 2845 final int CLASS_BOUND = 2;
duke@1 2846 int boundkind = 0;
duke@1 2847 for (Type t : ts) {
duke@1 2848 switch (t.tag) {
duke@1 2849 case CLASS:
duke@1 2850 boundkind |= CLASS_BOUND;
duke@1 2851 break;
duke@1 2852 case ARRAY:
duke@1 2853 boundkind |= ARRAY_BOUND;
duke@1 2854 break;
duke@1 2855 case TYPEVAR:
duke@1 2856 do {
duke@1 2857 t = t.getUpperBound();
duke@1 2858 } while (t.tag == TYPEVAR);
duke@1 2859 if (t.tag == ARRAY) {
duke@1 2860 boundkind |= ARRAY_BOUND;
duke@1 2861 } else {
duke@1 2862 boundkind |= CLASS_BOUND;
duke@1 2863 }
duke@1 2864 break;
duke@1 2865 default:
duke@1 2866 if (t.isPrimitive())
mcimadamore@5 2867 return syms.errType;
duke@1 2868 }
duke@1 2869 }
duke@1 2870 switch (boundkind) {
duke@1 2871 case 0:
duke@1 2872 return syms.botType;
duke@1 2873
duke@1 2874 case ARRAY_BOUND:
duke@1 2875 // calculate lub(A[], B[])
duke@1 2876 List<Type> elements = Type.map(ts, elemTypeFun);
duke@1 2877 for (Type t : elements) {
duke@1 2878 if (t.isPrimitive()) {
duke@1 2879 // if a primitive type is found, then return
duke@1 2880 // arraySuperType unless all the types are the
duke@1 2881 // same
duke@1 2882 Type first = ts.head;
duke@1 2883 for (Type s : ts.tail) {
duke@1 2884 if (!isSameType(first, s)) {
duke@1 2885 // lub(int[], B[]) is Cloneable & Serializable
duke@1 2886 return arraySuperType();
duke@1 2887 }
duke@1 2888 }
duke@1 2889 // all the array types are the same, return one
duke@1 2890 // lub(int[], int[]) is int[]
duke@1 2891 return first;
duke@1 2892 }
duke@1 2893 }
duke@1 2894 // lub(A[], B[]) is lub(A, B)[]
duke@1 2895 return new ArrayType(lub(elements), syms.arrayClass);
duke@1 2896
duke@1 2897 case CLASS_BOUND:
duke@1 2898 // calculate lub(A, B)
duke@1 2899 while (ts.head.tag != CLASS && ts.head.tag != TYPEVAR)
duke@1 2900 ts = ts.tail;
jjg@816 2901 Assert.check(!ts.isEmpty());
mcimadamore@896 2902 //step 1 - compute erased candidate set (EC)
mcimadamore@896 2903 List<Type> cl = erasedSupertypes(ts.head);
duke@1 2904 for (Type t : ts.tail) {
duke@1 2905 if (t.tag == CLASS || t.tag == TYPEVAR)
mcimadamore@896 2906 cl = intersect(cl, erasedSupertypes(t));
duke@1 2907 }
mcimadamore@896 2908 //step 2 - compute minimal erased candidate set (MEC)
mcimadamore@896 2909 List<Type> mec = closureMin(cl);
mcimadamore@896 2910 //step 3 - for each element G in MEC, compute lci(Inv(G))
mcimadamore@896 2911 List<Type> candidates = List.nil();
mcimadamore@896 2912 for (Type erasedSupertype : mec) {
mcimadamore@896 2913 List<Type> lci = List.of(asSuper(ts.head, erasedSupertype.tsym));
mcimadamore@896 2914 for (Type t : ts) {
mcimadamore@896 2915 lci = intersect(lci, List.of(asSuper(t, erasedSupertype.tsym)));
mcimadamore@896 2916 }
mcimadamore@896 2917 candidates = candidates.appendList(lci);
mcimadamore@896 2918 }
mcimadamore@896 2919 //step 4 - let MEC be { G1, G2 ... Gn }, then we have that
mcimadamore@896 2920 //lub = lci(Inv(G1)) & lci(Inv(G2)) & ... & lci(Inv(Gn))
mcimadamore@896 2921 return compoundMin(candidates);
duke@1 2922
duke@1 2923 default:
duke@1 2924 // calculate lub(A, B[])
duke@1 2925 List<Type> classes = List.of(arraySuperType());
duke@1 2926 for (Type t : ts) {
duke@1 2927 if (t.tag != ARRAY) // Filter out any arrays
duke@1 2928 classes = classes.prepend(t);
duke@1 2929 }
duke@1 2930 // lub(A, B[]) is lub(A, arraySuperType)
duke@1 2931 return lub(classes);
duke@1 2932 }
duke@1 2933 }
duke@1 2934 // where
mcimadamore@896 2935 List<Type> erasedSupertypes(Type t) {
mcimadamore@896 2936 ListBuffer<Type> buf = lb();
mcimadamore@896 2937 for (Type sup : closure(t)) {
mcimadamore@896 2938 if (sup.tag == TYPEVAR) {
mcimadamore@896 2939 buf.append(sup);
mcimadamore@896 2940 } else {
mcimadamore@896 2941 buf.append(erasure(sup));
mcimadamore@896 2942 }
mcimadamore@896 2943 }
mcimadamore@896 2944 return buf.toList();
mcimadamore@896 2945 }
mcimadamore@896 2946
duke@1 2947 private Type arraySuperType = null;
duke@1 2948 private Type arraySuperType() {
duke@1 2949 // initialized lazily to avoid problems during compiler startup
duke@1 2950 if (arraySuperType == null) {
duke@1 2951 synchronized (this) {
duke@1 2952 if (arraySuperType == null) {
duke@1 2953 // JLS 10.8: all arrays implement Cloneable and Serializable.
duke@1 2954 arraySuperType = makeCompoundType(List.of(syms.serializableType,
duke@1 2955 syms.cloneableType),
duke@1 2956 syms.objectType);
duke@1 2957 }
duke@1 2958 }
duke@1 2959 }
duke@1 2960 return arraySuperType;
duke@1 2961 }
duke@1 2962 // </editor-fold>
duke@1 2963
duke@1 2964 // <editor-fold defaultstate="collapsed" desc="Greatest lower bound">
mcimadamore@210 2965 public Type glb(List<Type> ts) {
mcimadamore@210 2966 Type t1 = ts.head;
mcimadamore@210 2967 for (Type t2 : ts.tail) {
mcimadamore@210 2968 if (t1.isErroneous())
mcimadamore@210 2969 return t1;
mcimadamore@210 2970 t1 = glb(t1, t2);
mcimadamore@210 2971 }
mcimadamore@210 2972 return t1;
mcimadamore@210 2973 }
mcimadamore@210 2974 //where
duke@1 2975 public Type glb(Type t, Type s) {
duke@1 2976 if (s == null)
duke@1 2977 return t;
mcimadamore@753 2978 else if (t.isPrimitive() || s.isPrimitive())
mcimadamore@753 2979 return syms.errType;
duke@1 2980 else if (isSubtypeNoCapture(t, s))
duke@1 2981 return t;
duke@1 2982 else if (isSubtypeNoCapture(s, t))
duke@1 2983 return s;
duke@1 2984
duke@1 2985 List<Type> closure = union(closure(t), closure(s));
duke@1 2986 List<Type> bounds = closureMin(closure);
duke@1 2987
duke@1 2988 if (bounds.isEmpty()) { // length == 0
duke@1 2989 return syms.objectType;
duke@1 2990 } else if (bounds.tail.isEmpty()) { // length == 1
duke@1 2991 return bounds.head;
duke@1 2992 } else { // length > 1
duke@1 2993 int classCount = 0;
duke@1 2994 for (Type bound : bounds)
duke@1 2995 if (!bound.isInterface())
duke@1 2996 classCount++;
duke@1 2997 if (classCount > 1)
jjg@110 2998 return createErrorType(t);
duke@1 2999 }
duke@1 3000 return makeCompoundType(bounds);
duke@1 3001 }
duke@1 3002 // </editor-fold>
duke@1 3003
duke@1 3004 // <editor-fold defaultstate="collapsed" desc="hashCode">
duke@1 3005 /**
duke@1 3006 * Compute a hash code on a type.
duke@1 3007 */
duke@1 3008 public static int hashCode(Type t) {
duke@1 3009 return hashCode.visit(t);
duke@1 3010 }
duke@1 3011 // where
duke@1 3012 private static final UnaryVisitor<Integer> hashCode = new UnaryVisitor<Integer>() {
duke@1 3013
duke@1 3014 public Integer visitType(Type t, Void ignored) {
duke@1 3015 return t.tag;
duke@1 3016 }
duke@1 3017
duke@1 3018 @Override
duke@1 3019 public Integer visitClassType(ClassType t, Void ignored) {
duke@1 3020 int result = visit(t.getEnclosingType());
duke@1 3021 result *= 127;
duke@1 3022 result += t.tsym.flatName().hashCode();
duke@1 3023 for (Type s : t.getTypeArguments()) {
duke@1 3024 result *= 127;
duke@1 3025 result += visit(s);
duke@1 3026 }
duke@1 3027 return result;
duke@1 3028 }
duke@1 3029
duke@1 3030 @Override
duke@1 3031 public Integer visitWildcardType(WildcardType t, Void ignored) {
duke@1 3032 int result = t.kind.hashCode();
duke@1 3033 if (t.type != null) {
duke@1 3034 result *= 127;
duke@1 3035 result += visit(t.type);
duke@1 3036 }
duke@1 3037 return result;
duke@1 3038 }
duke@1 3039
duke@1 3040 @Override
duke@1 3041 public Integer visitArrayType(ArrayType t, Void ignored) {
duke@1 3042 return visit(t.elemtype) + 12;
duke@1 3043 }
duke@1 3044
duke@1 3045 @Override
duke@1 3046 public Integer visitTypeVar(TypeVar t, Void ignored) {
duke@1 3047 return System.identityHashCode(t.tsym);
duke@1 3048 }
duke@1 3049
duke@1 3050 @Override
duke@1 3051 public Integer visitUndetVar(UndetVar t, Void ignored) {
duke@1 3052 return System.identityHashCode(t);
duke@1 3053 }
duke@1 3054
duke@1 3055 @Override
duke@1 3056 public Integer visitErrorType(ErrorType t, Void ignored) {
duke@1 3057 return 0;
duke@1 3058 }
duke@1 3059 };
duke@1 3060 // </editor-fold>
duke@1 3061
duke@1 3062 // <editor-fold defaultstate="collapsed" desc="Return-Type-Substitutable">
duke@1 3063 /**
duke@1 3064 * Does t have a result that is a subtype of the result type of s,
duke@1 3065 * suitable for covariant returns? It is assumed that both types
duke@1 3066 * are (possibly polymorphic) method types. Monomorphic method
duke@1 3067 * types are handled in the obvious way. Polymorphic method types
duke@1 3068 * require renaming all type variables of one to corresponding
duke@1 3069 * type variables in the other, where correspondence is by
duke@1 3070 * position in the type parameter list. */
duke@1 3071 public boolean resultSubtype(Type t, Type s, Warner warner) {
duke@1 3072 List<Type> tvars = t.getTypeArguments();
duke@1 3073 List<Type> svars = s.getTypeArguments();
duke@1 3074 Type tres = t.getReturnType();
duke@1 3075 Type sres = subst(s.getReturnType(), svars, tvars);
duke@1 3076 return covariantReturnType(tres, sres, warner);
duke@1 3077 }
duke@1 3078
duke@1 3079 /**
duke@1 3080 * Return-Type-Substitutable.
jjh@972 3081 * @jls section 8.4.5
duke@1 3082 */
duke@1 3083 public boolean returnTypeSubstitutable(Type r1, Type r2) {
duke@1 3084 if (hasSameArgs(r1, r2))
tbell@202 3085 return resultSubtype(r1, r2, Warner.noWarnings);
duke@1 3086 else
duke@1 3087 return covariantReturnType(r1.getReturnType(),
tbell@202 3088 erasure(r2.getReturnType()),
tbell@202 3089 Warner.noWarnings);
tbell@202 3090 }
tbell@202 3091
tbell@202 3092 public boolean returnTypeSubstitutable(Type r1,
tbell@202 3093 Type r2, Type r2res,
tbell@202 3094 Warner warner) {
tbell@202 3095 if (isSameType(r1.getReturnType(), r2res))
tbell@202 3096 return true;
tbell@202 3097 if (r1.getReturnType().isPrimitive() || r2res.isPrimitive())
tbell@202 3098 return false;
tbell@202 3099
tbell@202 3100 if (hasSameArgs(r1, r2))
tbell@202 3101 return covariantReturnType(r1.getReturnType(), r2res, warner);
jjg@984 3102 if (!allowCovariantReturns)
tbell@202 3103 return false;
tbell@202 3104 if (isSubtypeUnchecked(r1.getReturnType(), r2res, warner))
tbell@202 3105 return true;
tbell@202 3106 if (!isSubtype(r1.getReturnType(), erasure(r2res)))
tbell@202 3107 return false;
mcimadamore@795 3108 warner.warn(LintCategory.UNCHECKED);
tbell@202 3109 return true;
duke@1 3110 }
duke@1 3111
duke@1 3112 /**
duke@1 3113 * Is t an appropriate return type in an overrider for a
duke@1 3114 * method that returns s?
duke@1 3115 */
duke@1 3116 public boolean covariantReturnType(Type t, Type s, Warner warner) {
tbell@202 3117 return
tbell@202 3118 isSameType(t, s) ||
jjg@984 3119 allowCovariantReturns &&
duke@1 3120 !t.isPrimitive() &&
tbell@202 3121 !s.isPrimitive() &&
tbell@202 3122 isAssignable(t, s, warner);
duke@1 3123 }
duke@1 3124 // </editor-fold>
duke@1 3125
duke@1 3126 // <editor-fold defaultstate="collapsed" desc="Box/unbox support">
duke@1 3127 /**
duke@1 3128 * Return the class that boxes the given primitive.
duke@1 3129 */
duke@1 3130 public ClassSymbol boxedClass(Type t) {
duke@1 3131 return reader.enterClass(syms.boxedName[t.tag]);
duke@1 3132 }
duke@1 3133
duke@1 3134 /**
mcimadamore@753 3135 * Return the boxed type if 't' is primitive, otherwise return 't' itself.
mcimadamore@753 3136 */
mcimadamore@753 3137 public Type boxedTypeOrType(Type t) {
mcimadamore@753 3138 return t.isPrimitive() ?
mcimadamore@753 3139 boxedClass(t).type :
mcimadamore@753 3140 t;
mcimadamore@753 3141 }
mcimadamore@753 3142
mcimadamore@753 3143 /**
duke@1 3144 * Return the primitive type corresponding to a boxed type.
duke@1 3145 */
duke@1 3146 public Type unboxedType(Type t) {
duke@1 3147 if (allowBoxing) {
duke@1 3148 for (int i=0; i<syms.boxedName.length; i++) {
duke@1 3149 Name box = syms.boxedName[i];
duke@1 3150 if (box != null &&
duke@1 3151 asSuper(t, reader.enterClass(box)) != null)
duke@1 3152 return syms.typeOfTag[i];
duke@1 3153 }
duke@1 3154 }
duke@1 3155 return Type.noType;
duke@1 3156 }
duke@1 3157 // </editor-fold>
duke@1 3158
duke@1 3159 // <editor-fold defaultstate="collapsed" desc="Capture conversion">
duke@1 3160 /*
jjh@972 3161 * JLS 5.1.10 Capture Conversion:
duke@1 3162 *
duke@1 3163 * Let G name a generic type declaration with n formal type
duke@1 3164 * parameters A1 ... An with corresponding bounds U1 ... Un. There
duke@1 3165 * exists a capture conversion from G<T1 ... Tn> to G<S1 ... Sn>,
duke@1 3166 * where, for 1 <= i <= n:
duke@1 3167 *
duke@1 3168 * + If Ti is a wildcard type argument (4.5.1) of the form ? then
duke@1 3169 * Si is a fresh type variable whose upper bound is
duke@1 3170 * Ui[A1 := S1, ..., An := Sn] and whose lower bound is the null
duke@1 3171 * type.
duke@1 3172 *
duke@1 3173 * + If Ti is a wildcard type argument of the form ? extends Bi,
duke@1 3174 * then Si is a fresh type variable whose upper bound is
duke@1 3175 * glb(Bi, Ui[A1 := S1, ..., An := Sn]) and whose lower bound is
duke@1 3176 * the null type, where glb(V1,... ,Vm) is V1 & ... & Vm. It is
duke@1 3177 * a compile-time error if for any two classes (not interfaces)
duke@1 3178 * Vi and Vj,Vi is not a subclass of Vj or vice versa.
duke@1 3179 *
duke@1 3180 * + If Ti is a wildcard type argument of the form ? super Bi,
duke@1 3181 * then Si is a fresh type variable whose upper bound is
duke@1 3182 * Ui[A1 := S1, ..., An := Sn] and whose lower bound is Bi.
duke@1 3183 *
duke@1 3184 * + Otherwise, Si = Ti.
duke@1 3185 *
duke@1 3186 * Capture conversion on any type other than a parameterized type
duke@1 3187 * (4.5) acts as an identity conversion (5.1.1). Capture
duke@1 3188 * conversions never require a special action at run time and
duke@1 3189 * therefore never throw an exception at run time.
duke@1 3190 *
duke@1 3191 * Capture conversion is not applied recursively.
duke@1 3192 */
duke@1 3193 /**
jjh@972 3194 * Capture conversion as specified by the JLS.
duke@1 3195 */
mcimadamore@299 3196
mcimadamore@299 3197 public List<Type> capture(List<Type> ts) {
mcimadamore@299 3198 List<Type> buf = List.nil();
mcimadamore@299 3199 for (Type t : ts) {
mcimadamore@299 3200 buf = buf.prepend(capture(t));
mcimadamore@299 3201 }
mcimadamore@299 3202 return buf.reverse();
mcimadamore@299 3203 }
duke@1 3204 public Type capture(Type t) {
duke@1 3205 if (t.tag != CLASS)
duke@1 3206 return t;
mcimadamore@637 3207 if (t.getEnclosingType() != Type.noType) {
mcimadamore@637 3208 Type capturedEncl = capture(t.getEnclosingType());
mcimadamore@637 3209 if (capturedEncl != t.getEnclosingType()) {
mcimadamore@637 3210 Type type1 = memberType(capturedEncl, t.tsym);
mcimadamore@637 3211 t = subst(type1, t.tsym.type.getTypeArguments(), t.getTypeArguments());
mcimadamore@637 3212 }
mcimadamore@637 3213 }
duke@1 3214 ClassType cls = (ClassType)t;
duke@1 3215 if (cls.isRaw() || !cls.isParameterized())
duke@1 3216 return cls;
duke@1 3217
duke@1 3218 ClassType G = (ClassType)cls.asElement().asType();
duke@1 3219 List<Type> A = G.getTypeArguments();
duke@1 3220 List<Type> T = cls.getTypeArguments();
duke@1 3221 List<Type> S = freshTypeVariables(T);
duke@1 3222
duke@1 3223 List<Type> currentA = A;
duke@1 3224 List<Type> currentT = T;
duke@1 3225 List<Type> currentS = S;
duke@1 3226 boolean captured = false;
duke@1 3227 while (!currentA.isEmpty() &&
duke@1 3228 !currentT.isEmpty() &&
duke@1 3229 !currentS.isEmpty()) {
duke@1 3230 if (currentS.head != currentT.head) {
duke@1 3231 captured = true;
duke@1 3232 WildcardType Ti = (WildcardType)currentT.head;
duke@1 3233 Type Ui = currentA.head.getUpperBound();
duke@1 3234 CapturedType Si = (CapturedType)currentS.head;
duke@1 3235 if (Ui == null)
duke@1 3236 Ui = syms.objectType;
duke@1 3237 switch (Ti.kind) {
duke@1 3238 case UNBOUND:
duke@1 3239 Si.bound = subst(Ui, A, S);
duke@1 3240 Si.lower = syms.botType;
duke@1 3241 break;
duke@1 3242 case EXTENDS:
duke@1 3243 Si.bound = glb(Ti.getExtendsBound(), subst(Ui, A, S));
duke@1 3244 Si.lower = syms.botType;
duke@1 3245 break;
duke@1 3246 case SUPER:
duke@1 3247 Si.bound = subst(Ui, A, S);
duke@1 3248 Si.lower = Ti.getSuperBound();
duke@1 3249 break;
duke@1 3250 }
duke@1 3251 if (Si.bound == Si.lower)
duke@1 3252 currentS.head = Si.bound;
duke@1 3253 }
duke@1 3254 currentA = currentA.tail;
duke@1 3255 currentT = currentT.tail;
duke@1 3256 currentS = currentS.tail;
duke@1 3257 }
duke@1 3258 if (!currentA.isEmpty() || !currentT.isEmpty() || !currentS.isEmpty())
duke@1 3259 return erasure(t); // some "rare" type involved
duke@1 3260
duke@1 3261 if (captured)
duke@1 3262 return new ClassType(cls.getEnclosingType(), S, cls.tsym);
duke@1 3263 else
duke@1 3264 return t;
duke@1 3265 }
duke@1 3266 // where
mcimadamore@238 3267 public List<Type> freshTypeVariables(List<Type> types) {
duke@1 3268 ListBuffer<Type> result = lb();
duke@1 3269 for (Type t : types) {
duke@1 3270 if (t.tag == WILDCARD) {
duke@1 3271 Type bound = ((WildcardType)t).getExtendsBound();
duke@1 3272 if (bound == null)
duke@1 3273 bound = syms.objectType;
duke@1 3274 result.append(new CapturedType(capturedName,
duke@1 3275 syms.noSymbol,
duke@1 3276 bound,
duke@1 3277 syms.botType,
duke@1 3278 (WildcardType)t));
duke@1 3279 } else {
duke@1 3280 result.append(t);
duke@1 3281 }
duke@1 3282 }
duke@1 3283 return result.toList();
duke@1 3284 }
duke@1 3285 // </editor-fold>
duke@1 3286
duke@1 3287 // <editor-fold defaultstate="collapsed" desc="Internal utility methods">
duke@1 3288 private List<Type> upperBounds(List<Type> ss) {
duke@1 3289 if (ss.isEmpty()) return ss;
duke@1 3290 Type head = upperBound(ss.head);
duke@1 3291 List<Type> tail = upperBounds(ss.tail);
duke@1 3292 if (head != ss.head || tail != ss.tail)
duke@1 3293 return tail.prepend(head);
duke@1 3294 else
duke@1 3295 return ss;
duke@1 3296 }
duke@1 3297
duke@1 3298 private boolean sideCast(Type from, Type to, Warner warn) {
duke@1 3299 // We are casting from type $from$ to type $to$, which are
duke@1 3300 // non-final unrelated types. This method
duke@1 3301 // tries to reject a cast by transferring type parameters
duke@1 3302 // from $to$ to $from$ by common superinterfaces.
duke@1 3303 boolean reverse = false;
duke@1 3304 Type target = to;
duke@1 3305 if ((to.tsym.flags() & INTERFACE) == 0) {
jjg@816 3306 Assert.check((from.tsym.flags() & INTERFACE) != 0);
duke@1 3307 reverse = true;
duke@1 3308 to = from;
duke@1 3309 from = target;
duke@1 3310 }
duke@1 3311 List<Type> commonSupers = superClosure(to, erasure(from));
duke@1 3312 boolean giveWarning = commonSupers.isEmpty();
duke@1 3313 // The arguments to the supers could be unified here to
duke@1 3314 // get a more accurate analysis
duke@1 3315 while (commonSupers.nonEmpty()) {
duke@1 3316 Type t1 = asSuper(from, commonSupers.head.tsym);
duke@1 3317 Type t2 = commonSupers.head; // same as asSuper(to, commonSupers.head.tsym);
duke@1 3318 if (disjointTypes(t1.getTypeArguments(), t2.getTypeArguments()))
duke@1 3319 return false;
duke@1 3320 giveWarning = giveWarning || (reverse ? giveWarning(t2, t1) : giveWarning(t1, t2));
duke@1 3321 commonSupers = commonSupers.tail;
duke@1 3322 }
mcimadamore@187 3323 if (giveWarning && !isReifiable(reverse ? from : to))
mcimadamore@795 3324 warn.warn(LintCategory.UNCHECKED);
jjg@984 3325 if (!allowCovariantReturns)
duke@1 3326 // reject if there is a common method signature with
duke@1 3327 // incompatible return types.
duke@1 3328 chk.checkCompatibleAbstracts(warn.pos(), from, to);
duke@1 3329 return true;
duke@1 3330 }
duke@1 3331
duke@1 3332 private boolean sideCastFinal(Type from, Type to, Warner warn) {
duke@1 3333 // We are casting from type $from$ to type $to$, which are
duke@1 3334 // unrelated types one of which is final and the other of
duke@1 3335 // which is an interface. This method
duke@1 3336 // tries to reject a cast by transferring type parameters
duke@1 3337 // from the final class to the interface.
duke@1 3338 boolean reverse = false;
duke@1 3339 Type target = to;
duke@1 3340 if ((to.tsym.flags() & INTERFACE) == 0) {
jjg@816 3341 Assert.check((from.tsym.flags() & INTERFACE) != 0);
duke@1 3342 reverse = true;
duke@1 3343 to = from;
duke@1 3344 from = target;
duke@1 3345 }
jjg@816 3346 Assert.check((from.tsym.flags() & FINAL) != 0);
duke@1 3347 Type t1 = asSuper(from, to.tsym);
duke@1 3348 if (t1 == null) return false;
duke@1 3349 Type t2 = to;
duke@1 3350 if (disjointTypes(t1.getTypeArguments(), t2.getTypeArguments()))
duke@1 3351 return false;
jjg@984 3352 if (!allowCovariantReturns)
duke@1 3353 // reject if there is a common method signature with
duke@1 3354 // incompatible return types.
duke@1 3355 chk.checkCompatibleAbstracts(warn.pos(), from, to);
duke@1 3356 if (!isReifiable(target) &&
duke@1 3357 (reverse ? giveWarning(t2, t1) : giveWarning(t1, t2)))
mcimadamore@795 3358 warn.warn(LintCategory.UNCHECKED);
duke@1 3359 return true;
duke@1 3360 }
duke@1 3361
duke@1 3362 private boolean giveWarning(Type from, Type to) {
mcimadamore@235 3363 Type subFrom = asSub(from, to.tsym);
mcimadamore@235 3364 return to.isParameterized() &&
mcimadamore@235 3365 (!(isUnbounded(to) ||
mcimadamore@235 3366 isSubtype(from, to) ||
mcimadamore@736 3367 ((subFrom != null) && containsType(to.allparams(), subFrom.allparams()))));
duke@1 3368 }
duke@1 3369
duke@1 3370 private List<Type> superClosure(Type t, Type s) {
duke@1 3371 List<Type> cl = List.nil();
duke@1 3372 for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail) {
duke@1 3373 if (isSubtype(s, erasure(l.head))) {
duke@1 3374 cl = insert(cl, l.head);
duke@1 3375 } else {
duke@1 3376 cl = union(cl, superClosure(l.head, s));
duke@1 3377 }
duke@1 3378 }
duke@1 3379 return cl;
duke@1 3380 }
duke@1 3381
duke@1 3382 private boolean containsTypeEquivalent(Type t, Type s) {
duke@1 3383 return
duke@1 3384 isSameType(t, s) || // shortcut
duke@1 3385 containsType(t, s) && containsType(s, t);
duke@1 3386 }
duke@1 3387
mcimadamore@138 3388 // <editor-fold defaultstate="collapsed" desc="adapt">
duke@1 3389 /**
duke@1 3390 * Adapt a type by computing a substitution which maps a source
duke@1 3391 * type to a target type.
duke@1 3392 *
duke@1 3393 * @param source the source type
duke@1 3394 * @param target the target type
duke@1 3395 * @param from the type variables of the computed substitution
duke@1 3396 * @param to the types of the computed substitution.
duke@1 3397 */
duke@1 3398 public void adapt(Type source,
duke@1 3399 Type target,
duke@1 3400 ListBuffer<Type> from,
duke@1 3401 ListBuffer<Type> to) throws AdaptFailure {
mcimadamore@138 3402 new Adapter(from, to).adapt(source, target);
mcimadamore@138 3403 }
mcimadamore@138 3404
mcimadamore@138 3405 class Adapter extends SimpleVisitor<Void, Type> {
mcimadamore@138 3406
mcimadamore@138 3407 ListBuffer<Type> from;
mcimadamore@138 3408 ListBuffer<Type> to;
mcimadamore@138 3409 Map<Symbol,Type> mapping;
mcimadamore@138 3410
mcimadamore@138 3411 Adapter(ListBuffer<Type> from, ListBuffer<Type> to) {
mcimadamore@138 3412 this.from = from;
mcimadamore@138 3413 this.to = to;
mcimadamore@138 3414 mapping = new HashMap<Symbol,Type>();
duke@1 3415 }
mcimadamore@138 3416
mcimadamore@138 3417 public void adapt(Type source, Type target) throws AdaptFailure {
mcimadamore@138 3418 visit(source, target);
mcimadamore@138 3419 List<Type> fromList = from.toList();
mcimadamore@138 3420 List<Type> toList = to.toList();
mcimadamore@138 3421 while (!fromList.isEmpty()) {
mcimadamore@138 3422 Type val = mapping.get(fromList.head.tsym);
mcimadamore@138 3423 if (toList.head != val)
mcimadamore@138 3424 toList.head = val;
mcimadamore@138 3425 fromList = fromList.tail;
mcimadamore@138 3426 toList = toList.tail;
mcimadamore@138 3427 }
mcimadamore@138 3428 }
mcimadamore@138 3429
mcimadamore@138 3430 @Override
mcimadamore@138 3431 public Void visitClassType(ClassType source, Type target) throws AdaptFailure {
mcimadamore@138 3432 if (target.tag == CLASS)
mcimadamore@138 3433 adaptRecursive(source.allparams(), target.allparams());
mcimadamore@138 3434 return null;
mcimadamore@138 3435 }
mcimadamore@138 3436
mcimadamore@138 3437 @Override
mcimadamore@138 3438 public Void visitArrayType(ArrayType source, Type target) throws AdaptFailure {
mcimadamore@138 3439 if (target.tag == ARRAY)
mcimadamore@138 3440 adaptRecursive(elemtype(source), elemtype(target));
mcimadamore@138 3441 return null;
mcimadamore@138 3442 }
mcimadamore@138 3443
mcimadamore@138 3444 @Override
mcimadamore@138 3445 public Void visitWildcardType(WildcardType source, Type target) throws AdaptFailure {
mcimadamore@138 3446 if (source.isExtendsBound())
mcimadamore@138 3447 adaptRecursive(upperBound(source), upperBound(target));
mcimadamore@138 3448 else if (source.isSuperBound())
mcimadamore@138 3449 adaptRecursive(lowerBound(source), lowerBound(target));
mcimadamore@138 3450 return null;
mcimadamore@138 3451 }
mcimadamore@138 3452
mcimadamore@138 3453 @Override
mcimadamore@138 3454 public Void visitTypeVar(TypeVar source, Type target) throws AdaptFailure {
mcimadamore@138 3455 // Check to see if there is
mcimadamore@138 3456 // already a mapping for $source$, in which case
mcimadamore@138 3457 // the old mapping will be merged with the new
mcimadamore@138 3458 Type val = mapping.get(source.tsym);
mcimadamore@138 3459 if (val != null) {
mcimadamore@138 3460 if (val.isSuperBound() && target.isSuperBound()) {
mcimadamore@138 3461 val = isSubtype(lowerBound(val), lowerBound(target))
mcimadamore@138 3462 ? target : val;
mcimadamore@138 3463 } else if (val.isExtendsBound() && target.isExtendsBound()) {
mcimadamore@138 3464 val = isSubtype(upperBound(val), upperBound(target))
mcimadamore@138 3465 ? val : target;
mcimadamore@138 3466 } else if (!isSameType(val, target)) {
mcimadamore@138 3467 throw new AdaptFailure();
duke@1 3468 }
mcimadamore@138 3469 } else {
mcimadamore@138 3470 val = target;
mcimadamore@138 3471 from.append(source);
mcimadamore@138 3472 to.append(target);
mcimadamore@138 3473 }
mcimadamore@138 3474 mapping.put(source.tsym, val);
mcimadamore@138 3475 return null;
mcimadamore@138 3476 }
mcimadamore@138 3477
mcimadamore@138 3478 @Override
mcimadamore@138 3479 public Void visitType(Type source, Type target) {
mcimadamore@138 3480 return null;
mcimadamore@138 3481 }
mcimadamore@138 3482
mcimadamore@138 3483 private Set<TypePair> cache = new HashSet<TypePair>();
mcimadamore@138 3484
mcimadamore@138 3485 private void adaptRecursive(Type source, Type target) {
mcimadamore@138 3486 TypePair pair = new TypePair(source, target);
mcimadamore@138 3487 if (cache.add(pair)) {
mcimadamore@138 3488 try {
mcimadamore@138 3489 visit(source, target);
mcimadamore@138 3490 } finally {
mcimadamore@138 3491 cache.remove(pair);
duke@1 3492 }
duke@1 3493 }
duke@1 3494 }
mcimadamore@138 3495
mcimadamore@138 3496 private void adaptRecursive(List<Type> source, List<Type> target) {
mcimadamore@138 3497 if (source.length() == target.length()) {
mcimadamore@138 3498 while (source.nonEmpty()) {
mcimadamore@138 3499 adaptRecursive(source.head, target.head);
mcimadamore@138 3500 source = source.tail;
mcimadamore@138 3501 target = target.tail;
mcimadamore@138 3502 }
duke@1 3503 }
duke@1 3504 }
duke@1 3505 }
duke@1 3506
mcimadamore@138 3507 public static class AdaptFailure extends RuntimeException {
mcimadamore@138 3508 static final long serialVersionUID = -7490231548272701566L;
mcimadamore@138 3509 }
mcimadamore@138 3510
duke@1 3511 private void adaptSelf(Type t,
duke@1 3512 ListBuffer<Type> from,
duke@1 3513 ListBuffer<Type> to) {
duke@1 3514 try {
duke@1 3515 //if (t.tsym.type != t)
duke@1 3516 adapt(t.tsym.type, t, from, to);
duke@1 3517 } catch (AdaptFailure ex) {
duke@1 3518 // Adapt should never fail calculating a mapping from
duke@1 3519 // t.tsym.type to t as there can be no merge problem.
duke@1 3520 throw new AssertionError(ex);
duke@1 3521 }
duke@1 3522 }
mcimadamore@138 3523 // </editor-fold>
duke@1 3524
duke@1 3525 /**
duke@1 3526 * Rewrite all type variables (universal quantifiers) in the given
duke@1 3527 * type to wildcards (existential quantifiers). This is used to
duke@1 3528 * determine if a cast is allowed. For example, if high is true
duke@1 3529 * and {@code T <: Number}, then {@code List<T>} is rewritten to
duke@1 3530 * {@code List<? extends Number>}. Since {@code List<Integer> <:
duke@1 3531 * List<? extends Number>} a {@code List<T>} can be cast to {@code
duke@1 3532 * List<Integer>} with a warning.
duke@1 3533 * @param t a type
duke@1 3534 * @param high if true return an upper bound; otherwise a lower
duke@1 3535 * bound
duke@1 3536 * @param rewriteTypeVars only rewrite captured wildcards if false;
duke@1 3537 * otherwise rewrite all type variables
duke@1 3538 * @return the type rewritten with wildcards (existential
duke@1 3539 * quantifiers) only
duke@1 3540 */
duke@1 3541 private Type rewriteQuantifiers(Type t, boolean high, boolean rewriteTypeVars) {
mcimadamore@640 3542 return new Rewriter(high, rewriteTypeVars).visit(t);
mcimadamore@157 3543 }
mcimadamore@157 3544
mcimadamore@157 3545 class Rewriter extends UnaryVisitor<Type> {
mcimadamore@157 3546
mcimadamore@157 3547 boolean high;
mcimadamore@157 3548 boolean rewriteTypeVars;
mcimadamore@157 3549
mcimadamore@157 3550 Rewriter(boolean high, boolean rewriteTypeVars) {
mcimadamore@157 3551 this.high = high;
mcimadamore@157 3552 this.rewriteTypeVars = rewriteTypeVars;
mcimadamore@157 3553 }
mcimadamore@157 3554
mcimadamore@640 3555 @Override
mcimadamore@640 3556 public Type visitClassType(ClassType t, Void s) {
mcimadamore@157 3557 ListBuffer<Type> rewritten = new ListBuffer<Type>();
mcimadamore@157 3558 boolean changed = false;
mcimadamore@640 3559 for (Type arg : t.allparams()) {
mcimadamore@157 3560 Type bound = visit(arg);
mcimadamore@157 3561 if (arg != bound) {
mcimadamore@157 3562 changed = true;
mcimadamore@157 3563 }
mcimadamore@157 3564 rewritten.append(bound);
duke@1 3565 }
mcimadamore@157 3566 if (changed)
mcimadamore@640 3567 return subst(t.tsym.type,
mcimadamore@640 3568 t.tsym.type.allparams(),
mcimadamore@640 3569 rewritten.toList());
mcimadamore@157 3570 else
mcimadamore@157 3571 return t;
duke@1 3572 }
mcimadamore@157 3573
mcimadamore@157 3574 public Type visitType(Type t, Void s) {
mcimadamore@157 3575 return high ? upperBound(t) : lowerBound(t);
mcimadamore@157 3576 }
mcimadamore@157 3577
mcimadamore@157 3578 @Override
mcimadamore@157 3579 public Type visitCapturedType(CapturedType t, Void s) {
mcimadamore@1177 3580 Type w_bound = t.wildcard.type;
mcimadamore@1177 3581 Type bound = w_bound.contains(t) ?
mcimadamore@1177 3582 erasure(w_bound) :
mcimadamore@1177 3583 visit(w_bound);
mcimadamore@1177 3584 return rewriteAsWildcardType(visit(bound), t.wildcard.bound, t.wildcard.kind);
mcimadamore@157 3585 }
mcimadamore@157 3586
mcimadamore@157 3587 @Override
mcimadamore@157 3588 public Type visitTypeVar(TypeVar t, Void s) {
mcimadamore@640 3589 if (rewriteTypeVars) {
mcimadamore@1177 3590 Type bound = t.bound.contains(t) ?
mcimadamore@779 3591 erasure(t.bound) :
mcimadamore@1177 3592 visit(t.bound);
mcimadamore@1177 3593 return rewriteAsWildcardType(bound, t, EXTENDS);
mcimadamore@1177 3594 } else {
mcimadamore@1177 3595 return t;
mcimadamore@640 3596 }
mcimadamore@157 3597 }
mcimadamore@157 3598
mcimadamore@157 3599 @Override
mcimadamore@157 3600 public Type visitWildcardType(WildcardType t, Void s) {
mcimadamore@1177 3601 Type bound2 = visit(t.type);
mcimadamore@1177 3602 return t.type == bound2 ? t : rewriteAsWildcardType(bound2, t.bound, t.kind);
mcimadamore@640 3603 }
mcimadamore@640 3604
mcimadamore@1177 3605 private Type rewriteAsWildcardType(Type bound, TypeVar formal, BoundKind bk) {
mcimadamore@1177 3606 switch (bk) {
mcimadamore@1177 3607 case EXTENDS: return high ?
mcimadamore@1177 3608 makeExtendsWildcard(B(bound), formal) :
mcimadamore@1177 3609 makeExtendsWildcard(syms.objectType, formal);
mcimadamore@1177 3610 case SUPER: return high ?
mcimadamore@1177 3611 makeSuperWildcard(syms.botType, formal) :
mcimadamore@1177 3612 makeSuperWildcard(B(bound), formal);
mcimadamore@1177 3613 case UNBOUND: return makeExtendsWildcard(syms.objectType, formal);
mcimadamore@1177 3614 default:
mcimadamore@1177 3615 Assert.error("Invalid bound kind " + bk);
mcimadamore@1177 3616 return null;
mcimadamore@1177 3617 }
mcimadamore@640 3618 }
mcimadamore@640 3619
mcimadamore@640 3620 Type B(Type t) {
mcimadamore@640 3621 while (t.tag == WILDCARD) {
mcimadamore@640 3622 WildcardType w = (WildcardType)t;
mcimadamore@640 3623 t = high ?
mcimadamore@640 3624 w.getExtendsBound() :
mcimadamore@640 3625 w.getSuperBound();
mcimadamore@640 3626 if (t == null) {
mcimadamore@640 3627 t = high ? syms.objectType : syms.botType;
mcimadamore@640 3628 }
mcimadamore@640 3629 }
mcimadamore@640 3630 return t;
mcimadamore@157 3631 }
duke@1 3632 }
duke@1 3633
mcimadamore@640 3634
duke@1 3635 /**
duke@1 3636 * Create a wildcard with the given upper (extends) bound; create
duke@1 3637 * an unbounded wildcard if bound is Object.
duke@1 3638 *
duke@1 3639 * @param bound the upper bound
duke@1 3640 * @param formal the formal type parameter that will be
duke@1 3641 * substituted by the wildcard
duke@1 3642 */
duke@1 3643 private WildcardType makeExtendsWildcard(Type bound, TypeVar formal) {
duke@1 3644 if (bound == syms.objectType) {
duke@1 3645 return new WildcardType(syms.objectType,
duke@1 3646 BoundKind.UNBOUND,
duke@1 3647 syms.boundClass,
duke@1 3648 formal);
duke@1 3649 } else {
duke@1 3650 return new WildcardType(bound,
duke@1 3651 BoundKind.EXTENDS,
duke@1 3652 syms.boundClass,
duke@1 3653 formal);
duke@1 3654 }
duke@1 3655 }
duke@1 3656
duke@1 3657 /**
duke@1 3658 * Create a wildcard with the given lower (super) bound; create an
duke@1 3659 * unbounded wildcard if bound is bottom (type of {@code null}).
duke@1 3660 *
duke@1 3661 * @param bound the lower bound
duke@1 3662 * @param formal the formal type parameter that will be
duke@1 3663 * substituted by the wildcard
duke@1 3664 */
duke@1 3665 private WildcardType makeSuperWildcard(Type bound, TypeVar formal) {
duke@1 3666 if (bound.tag == BOT) {
duke@1 3667 return new WildcardType(syms.objectType,
duke@1 3668 BoundKind.UNBOUND,
duke@1 3669 syms.boundClass,
duke@1 3670 formal);
duke@1 3671 } else {
duke@1 3672 return new WildcardType(bound,
duke@1 3673 BoundKind.SUPER,
duke@1 3674 syms.boundClass,
duke@1 3675 formal);
duke@1 3676 }
duke@1 3677 }
duke@1 3678
duke@1 3679 /**
duke@1 3680 * A wrapper for a type that allows use in sets.
duke@1 3681 */
duke@1 3682 class SingletonType {
duke@1 3683 final Type t;
duke@1 3684 SingletonType(Type t) {
duke@1 3685 this.t = t;
duke@1 3686 }
duke@1 3687 public int hashCode() {
jjg@507 3688 return Types.hashCode(t);
duke@1 3689 }
duke@1 3690 public boolean equals(Object obj) {
duke@1 3691 return (obj instanceof SingletonType) &&
duke@1 3692 isSameType(t, ((SingletonType)obj).t);
duke@1 3693 }
duke@1 3694 public String toString() {
duke@1 3695 return t.toString();
duke@1 3696 }
duke@1 3697 }
duke@1 3698 // </editor-fold>
duke@1 3699
duke@1 3700 // <editor-fold defaultstate="collapsed" desc="Visitors">
duke@1 3701 /**
duke@1 3702 * A default visitor for types. All visitor methods except
duke@1 3703 * visitType are implemented by delegating to visitType. Concrete
duke@1 3704 * subclasses must provide an implementation of visitType and can
duke@1 3705 * override other methods as needed.
duke@1 3706 *
duke@1 3707 * @param <R> the return type of the operation implemented by this
duke@1 3708 * visitor; use Void if no return type is needed.
duke@1 3709 * @param <S> the type of the second argument (the first being the
duke@1 3710 * type itself) of the operation implemented by this visitor; use
duke@1 3711 * Void if a second argument is not needed.
duke@1 3712 */
duke@1 3713 public static abstract class DefaultTypeVisitor<R,S> implements Type.Visitor<R,S> {
duke@1 3714 final public R visit(Type t, S s) { return t.accept(this, s); }
duke@1 3715 public R visitClassType(ClassType t, S s) { return visitType(t, s); }
duke@1 3716 public R visitWildcardType(WildcardType t, S s) { return visitType(t, s); }
duke@1 3717 public R visitArrayType(ArrayType t, S s) { return visitType(t, s); }
duke@1 3718 public R visitMethodType(MethodType t, S s) { return visitType(t, s); }
duke@1 3719 public R visitPackageType(PackageType t, S s) { return visitType(t, s); }
duke@1 3720 public R visitTypeVar(TypeVar t, S s) { return visitType(t, s); }
duke@1 3721 public R visitCapturedType(CapturedType t, S s) { return visitType(t, s); }
duke@1 3722 public R visitForAll(ForAll t, S s) { return visitType(t, s); }
duke@1 3723 public R visitUndetVar(UndetVar t, S s) { return visitType(t, s); }
duke@1 3724 public R visitErrorType(ErrorType t, S s) { return visitType(t, s); }
duke@1 3725 }
duke@1 3726
duke@1 3727 /**
mcimadamore@121 3728 * A default visitor for symbols. All visitor methods except
mcimadamore@121 3729 * visitSymbol are implemented by delegating to visitSymbol. Concrete
mcimadamore@121 3730 * subclasses must provide an implementation of visitSymbol and can
mcimadamore@121 3731 * override other methods as needed.
mcimadamore@121 3732 *
mcimadamore@121 3733 * @param <R> the return type of the operation implemented by this
mcimadamore@121 3734 * visitor; use Void if no return type is needed.
mcimadamore@121 3735 * @param <S> the type of the second argument (the first being the
mcimadamore@121 3736 * symbol itself) of the operation implemented by this visitor; use
mcimadamore@121 3737 * Void if a second argument is not needed.
mcimadamore@121 3738 */
mcimadamore@121 3739 public static abstract class DefaultSymbolVisitor<R,S> implements Symbol.Visitor<R,S> {
mcimadamore@121 3740 final public R visit(Symbol s, S arg) { return s.accept(this, arg); }
mcimadamore@121 3741 public R visitClassSymbol(ClassSymbol s, S arg) { return visitSymbol(s, arg); }
mcimadamore@121 3742 public R visitMethodSymbol(MethodSymbol s, S arg) { return visitSymbol(s, arg); }
mcimadamore@121 3743 public R visitOperatorSymbol(OperatorSymbol s, S arg) { return visitSymbol(s, arg); }
mcimadamore@121 3744 public R visitPackageSymbol(PackageSymbol s, S arg) { return visitSymbol(s, arg); }
mcimadamore@121 3745 public R visitTypeSymbol(TypeSymbol s, S arg) { return visitSymbol(s, arg); }
mcimadamore@121 3746 public R visitVarSymbol(VarSymbol s, S arg) { return visitSymbol(s, arg); }
mcimadamore@121 3747 }
mcimadamore@121 3748
mcimadamore@121 3749 /**
duke@1 3750 * A <em>simple</em> visitor for types. This visitor is simple as
duke@1 3751 * captured wildcards, for-all types (generic methods), and
duke@1 3752 * undetermined type variables (part of inference) are hidden.
duke@1 3753 * Captured wildcards are hidden by treating them as type
duke@1 3754 * variables and the rest are hidden by visiting their qtypes.
duke@1 3755 *
duke@1 3756 * @param <R> the return type of the operation implemented by this
duke@1 3757 * visitor; use Void if no return type is needed.
duke@1 3758 * @param <S> the type of the second argument (the first being the
duke@1 3759 * type itself) of the operation implemented by this visitor; use
duke@1 3760 * Void if a second argument is not needed.
duke@1 3761 */
duke@1 3762 public static abstract class SimpleVisitor<R,S> extends DefaultTypeVisitor<R,S> {
duke@1 3763 @Override
duke@1 3764 public R visitCapturedType(CapturedType t, S s) {
duke@1 3765 return visitTypeVar(t, s);
duke@1 3766 }
duke@1 3767 @Override
duke@1 3768 public R visitForAll(ForAll t, S s) {
duke@1 3769 return visit(t.qtype, s);
duke@1 3770 }
duke@1 3771 @Override
duke@1 3772 public R visitUndetVar(UndetVar t, S s) {
duke@1 3773 return visit(t.qtype, s);
duke@1 3774 }
duke@1 3775 }
duke@1 3776
duke@1 3777 /**
duke@1 3778 * A plain relation on types. That is a 2-ary function on the
duke@1 3779 * form Type&nbsp;&times;&nbsp;Type&nbsp;&rarr;&nbsp;Boolean.
duke@1 3780 * <!-- In plain text: Type x Type -> Boolean -->
duke@1 3781 */
duke@1 3782 public static abstract class TypeRelation extends SimpleVisitor<Boolean,Type> {}
duke@1 3783
duke@1 3784 /**
duke@1 3785 * A convenience visitor for implementing operations that only
duke@1 3786 * require one argument (the type itself), that is, unary
duke@1 3787 * operations.
duke@1 3788 *
duke@1 3789 * @param <R> the return type of the operation implemented by this
duke@1 3790 * visitor; use Void if no return type is needed.
duke@1 3791 */
duke@1 3792 public static abstract class UnaryVisitor<R> extends SimpleVisitor<R,Void> {
duke@1 3793 final public R visit(Type t) { return t.accept(this, null); }
duke@1 3794 }
duke@1 3795
duke@1 3796 /**
duke@1 3797 * A visitor for implementing a mapping from types to types. The
duke@1 3798 * default behavior of this class is to implement the identity
duke@1 3799 * mapping (mapping a type to itself). This can be overridden in
duke@1 3800 * subclasses.
duke@1 3801 *
duke@1 3802 * @param <S> the type of the second argument (the first being the
duke@1 3803 * type itself) of this mapping; use Void if a second argument is
duke@1 3804 * not needed.
duke@1 3805 */
duke@1 3806 public static class MapVisitor<S> extends DefaultTypeVisitor<Type,S> {
duke@1 3807 final public Type visit(Type t) { return t.accept(this, null); }
duke@1 3808 public Type visitType(Type t, S s) { return t; }
duke@1 3809 }
duke@1 3810 // </editor-fold>
jjg@657 3811
jjg@657 3812
jjg@657 3813 // <editor-fold defaultstate="collapsed" desc="Annotation support">
jjg@657 3814
jjg@657 3815 public RetentionPolicy getRetention(Attribute.Compound a) {
jfranck@1313 3816 return getRetention(a.type.tsym);
jfranck@1313 3817 }
jfranck@1313 3818
jfranck@1313 3819 public RetentionPolicy getRetention(Symbol sym) {
jjg@657 3820 RetentionPolicy vis = RetentionPolicy.CLASS; // the default
jfranck@1313 3821 Attribute.Compound c = sym.attribute(syms.retentionType.tsym);
jjg@657 3822 if (c != null) {
jjg@657 3823 Attribute value = c.member(names.value);
jjg@657 3824 if (value != null && value instanceof Attribute.Enum) {
jjg@657 3825 Name levelName = ((Attribute.Enum)value).value.name;
jjg@657 3826 if (levelName == names.SOURCE) vis = RetentionPolicy.SOURCE;
jjg@657 3827 else if (levelName == names.CLASS) vis = RetentionPolicy.CLASS;
jjg@657 3828 else if (levelName == names.RUNTIME) vis = RetentionPolicy.RUNTIME;
jjg@657 3829 else ;// /* fail soft */ throw new AssertionError(levelName);
jjg@657 3830 }
jjg@657 3831 }
jjg@657 3832 return vis;
jjg@657 3833 }
jjg@657 3834 // </editor-fold>
duke@1 3835 }

mercurial