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

Wed, 11 Jan 2012 18:23:24 +0000

author
mcimadamore
date
Wed, 11 Jan 2012 18:23:24 +0000
changeset 1177
70d92518063e
parent 1108
b5d0b8effc85
child 1251
6f0ed5a89c25
permissions
-rw-r--r--

7126754: Generics compilation failure casting List<? extends Set...> to List<Set...>
Summary: Problems with Types.rewriteQuantifiers not preserving variance
Reviewed-by: jjg

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

mercurial