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

Tue, 25 May 2010 15:54:51 -0700

author
ohair
date
Tue, 25 May 2010 15:54:51 -0700
changeset 554
9d9f26857129
parent 507
dbcba45123cd
child 567
593a59e40bdb
permissions
-rw-r--r--

6943119: Rebrand source copyright notices
Reviewed-by: darcy

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

mercurial