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

Tue, 13 Jan 2009 13:31:35 +0000

author
mcimadamore
date
Tue, 13 Jan 2009 13:31:35 +0000
changeset 187
e157bd68dfc5
parent 185
d57378c34fdb
child 196
1ca2dc8584e1
permissions
-rw-r--r--

6558559: Extra "unchecked" diagnostic
Summary: Fixed Types.sideCast in order to suppress redundant unchecked warnings
Reviewed-by: jjg

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

mercurial