duke@1: /*
mcimadamore@238: * Copyright 2003-2009 Sun Microsystems, Inc. All Rights Reserved.
duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1: *
duke@1: * This code is free software; you can redistribute it and/or modify it
duke@1: * under the terms of the GNU General Public License version 2 only, as
duke@1: * published by the Free Software Foundation. Sun designates this
duke@1: * particular file as subject to the "Classpath" exception as provided
duke@1: * by Sun in the LICENSE file that accompanied this code.
duke@1: *
duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1: * version 2 for more details (a copy is included in the LICENSE file that
duke@1: * accompanied this code).
duke@1: *
duke@1: * You should have received a copy of the GNU General Public License version
duke@1: * 2 along with this work; if not, write to the Free Software Foundation,
duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1: *
duke@1: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1: * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1: * have any questions.
duke@1: */
duke@1:
duke@1: package com.sun.tools.javac.code;
duke@1:
mcimadamore@341: import java.lang.ref.SoftReference;
duke@1: import java.util.*;
duke@1:
duke@1: import com.sun.tools.javac.util.*;
duke@1: import com.sun.tools.javac.util.List;
duke@1:
duke@1: import com.sun.tools.javac.jvm.ClassReader;
duke@1: import com.sun.tools.javac.comp.Check;
duke@1:
duke@1: import static com.sun.tools.javac.code.Type.*;
duke@1: import static com.sun.tools.javac.code.TypeTags.*;
duke@1: import static com.sun.tools.javac.code.Symbol.*;
duke@1: import static com.sun.tools.javac.code.Flags.*;
duke@1: import static com.sun.tools.javac.code.BoundKind.*;
duke@1: import static com.sun.tools.javac.util.ListBuffer.lb;
duke@1:
duke@1: /**
duke@1: * Utility class containing various operations on types.
duke@1: *
duke@1: *
Unless other names are more illustrative, the following naming
duke@1: * conventions should be observed in this file:
duke@1: *
duke@1: *
duke@1: *
t
duke@1: *
If the first argument to an operation is a type, it should be named t.
duke@1: *
s
duke@1: *
Similarly, if the second argument to an operation is a type, it should be named s.
duke@1: *
ts
duke@1: *
If an operations takes a list of types, the first should be named ts.
duke@1: *
ss
duke@1: *
A second list of types should be named ss.
duke@1: *
duke@1: *
duke@1: *
This is NOT part of any API supported by Sun Microsystems.
duke@1: * If you write code that depends on this, you do so at your own risk.
duke@1: * This code and its internal interfaces are subject to change or
duke@1: * deletion without notice.
duke@1: */
duke@1: public class Types {
duke@1: protected static final Context.Key typesKey =
duke@1: new Context.Key();
duke@1:
duke@1: final Symtab syms;
mcimadamore@136: final JavacMessages messages;
jjg@113: final Names names;
duke@1: final boolean allowBoxing;
duke@1: final ClassReader reader;
duke@1: final Source source;
duke@1: final Check chk;
duke@1: List warnStack = List.nil();
duke@1: final Name capturedName;
duke@1:
duke@1: //
duke@1: public static Types instance(Context context) {
duke@1: Types instance = context.get(typesKey);
duke@1: if (instance == null)
duke@1: instance = new Types(context);
duke@1: return instance;
duke@1: }
duke@1:
duke@1: protected Types(Context context) {
duke@1: context.put(typesKey, this);
duke@1: syms = Symtab.instance(context);
jjg@113: names = Names.instance(context);
duke@1: allowBoxing = Source.instance(context).allowBoxing();
duke@1: reader = ClassReader.instance(context);
duke@1: source = Source.instance(context);
duke@1: chk = Check.instance(context);
duke@1: capturedName = names.fromString("");
mcimadamore@136: messages = JavacMessages.instance(context);
duke@1: }
duke@1: //
duke@1:
duke@1: //
duke@1: /**
duke@1: * The "rvalue conversion".
duke@1: * The upper bound of most types is the type
duke@1: * itself. Wildcards, on the other hand have upper
duke@1: * and lower bounds.
duke@1: * @param t a type
duke@1: * @return the upper bound of the given type
duke@1: */
duke@1: public Type upperBound(Type t) {
duke@1: return upperBound.visit(t);
duke@1: }
duke@1: // where
duke@1: private final MapVisitor upperBound = new MapVisitor() {
duke@1:
duke@1: @Override
duke@1: public Type visitWildcardType(WildcardType t, Void ignored) {
duke@1: if (t.isSuperBound())
duke@1: return t.bound == null ? syms.objectType : t.bound.bound;
duke@1: else
duke@1: return visit(t.type);
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Type visitCapturedType(CapturedType t, Void ignored) {
duke@1: return visit(t.bound);
duke@1: }
duke@1: };
duke@1: //
duke@1:
duke@1: //
duke@1: /**
duke@1: * The "lvalue conversion".
duke@1: * The lower bound of most types is the type
duke@1: * itself. Wildcards, on the other hand have upper
duke@1: * and lower bounds.
duke@1: * @param t a type
duke@1: * @return the lower bound of the given type
duke@1: */
duke@1: public Type lowerBound(Type t) {
duke@1: return lowerBound.visit(t);
duke@1: }
duke@1: // where
duke@1: private final MapVisitor lowerBound = new MapVisitor() {
duke@1:
duke@1: @Override
duke@1: public Type visitWildcardType(WildcardType t, Void ignored) {
duke@1: return t.isExtendsBound() ? syms.botType : visit(t.type);
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Type visitCapturedType(CapturedType t, Void ignored) {
duke@1: return visit(t.getLowerBound());
duke@1: }
duke@1: };
duke@1: //
duke@1:
duke@1: //
duke@1: /**
duke@1: * Checks that all the arguments to a class are unbounded
duke@1: * wildcards or something else that doesn't make any restrictions
duke@1: * on the arguments. If a class isUnbounded, a raw super- or
duke@1: * subclass can be cast to it without a warning.
duke@1: * @param t a type
duke@1: * @return true iff the given type is unbounded or raw
duke@1: */
duke@1: public boolean isUnbounded(Type t) {
duke@1: return isUnbounded.visit(t);
duke@1: }
duke@1: // where
duke@1: private final UnaryVisitor isUnbounded = new UnaryVisitor() {
duke@1:
duke@1: public Boolean visitType(Type t, Void ignored) {
duke@1: return true;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Boolean visitClassType(ClassType t, Void ignored) {
duke@1: List parms = t.tsym.type.allparams();
duke@1: List args = t.allparams();
duke@1: while (parms.nonEmpty()) {
duke@1: WildcardType unb = new WildcardType(syms.objectType,
duke@1: BoundKind.UNBOUND,
duke@1: syms.boundClass,
duke@1: (TypeVar)parms.head);
duke@1: if (!containsType(args.head, unb))
duke@1: return false;
duke@1: parms = parms.tail;
duke@1: args = args.tail;
duke@1: }
duke@1: return true;
duke@1: }
duke@1: };
duke@1: //
duke@1:
duke@1: //
duke@1: /**
duke@1: * Return the least specific subtype of t that starts with symbol
duke@1: * sym. If none exists, return null. The least specific subtype
duke@1: * is determined as follows:
duke@1: *
duke@1: *
If there is exactly one parameterized instance of sym that is a
duke@1: * subtype of t, that parameterized instance is returned.
duke@1: * Otherwise, if the plain type or raw type `sym' is a subtype of
duke@1: * type t, the type `sym' itself is returned. Otherwise, null is
duke@1: * returned.
duke@1: */
duke@1: public Type asSub(Type t, Symbol sym) {
duke@1: return asSub.visit(t, sym);
duke@1: }
duke@1: // where
duke@1: private final SimpleVisitor asSub = new SimpleVisitor() {
duke@1:
duke@1: public Type visitType(Type t, Symbol sym) {
duke@1: return null;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Type visitClassType(ClassType t, Symbol sym) {
duke@1: if (t.tsym == sym)
duke@1: return t;
duke@1: Type base = asSuper(sym.type, t.tsym);
duke@1: if (base == null)
duke@1: return null;
duke@1: ListBuffer from = new ListBuffer();
duke@1: ListBuffer to = new ListBuffer();
duke@1: try {
duke@1: adapt(base, t, from, to);
duke@1: } catch (AdaptFailure ex) {
duke@1: return null;
duke@1: }
duke@1: Type res = subst(sym.type, from.toList(), to.toList());
duke@1: if (!isSubtype(res, t))
duke@1: return null;
duke@1: ListBuffer openVars = new ListBuffer();
duke@1: for (List l = sym.type.allparams();
duke@1: l.nonEmpty(); l = l.tail)
duke@1: if (res.contains(l.head) && !t.contains(l.head))
duke@1: openVars.append(l.head);
duke@1: if (openVars.nonEmpty()) {
duke@1: if (t.isRaw()) {
duke@1: // The subtype of a raw type is raw
duke@1: res = erasure(res);
duke@1: } else {
duke@1: // Unbound type arguments default to ?
duke@1: List opens = openVars.toList();
duke@1: ListBuffer qs = new ListBuffer();
duke@1: for (List iter = opens; iter.nonEmpty(); iter = iter.tail) {
duke@1: qs.append(new WildcardType(syms.objectType, BoundKind.UNBOUND, syms.boundClass, (TypeVar) iter.head));
duke@1: }
duke@1: res = subst(res, opens, qs.toList());
duke@1: }
duke@1: }
duke@1: return res;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Type visitErrorType(ErrorType t, Symbol sym) {
duke@1: return t;
duke@1: }
duke@1: };
duke@1: //
duke@1:
duke@1: //
duke@1: /**
duke@1: * Is t a subtype of or convertiable via boxing/unboxing
duke@1: * convertions to s?
duke@1: */
duke@1: public boolean isConvertible(Type t, Type s, Warner warn) {
duke@1: boolean tPrimitive = t.isPrimitive();
duke@1: boolean sPrimitive = s.isPrimitive();
duke@1: if (tPrimitive == sPrimitive)
duke@1: return isSubtypeUnchecked(t, s, warn);
duke@1: if (!allowBoxing) return false;
duke@1: return tPrimitive
duke@1: ? isSubtype(boxedClass(t).type, s)
duke@1: : isSubtype(unboxedType(t), s);
duke@1: }
duke@1:
duke@1: /**
duke@1: * Is t a subtype of or convertiable via boxing/unboxing
duke@1: * convertions to s?
duke@1: */
duke@1: public boolean isConvertible(Type t, Type s) {
duke@1: return isConvertible(t, s, Warner.noWarnings);
duke@1: }
duke@1: //
duke@1:
duke@1: //
duke@1: /**
duke@1: * Is t an unchecked subtype of s?
duke@1: */
duke@1: public boolean isSubtypeUnchecked(Type t, Type s) {
duke@1: return isSubtypeUnchecked(t, s, Warner.noWarnings);
duke@1: }
duke@1: /**
duke@1: * Is t an unchecked subtype of s?
duke@1: */
duke@1: public boolean isSubtypeUnchecked(Type t, Type s, Warner warn) {
duke@1: if (t.tag == ARRAY && s.tag == ARRAY) {
duke@1: return (((ArrayType)t).elemtype.tag <= lastBaseTag)
duke@1: ? isSameType(elemtype(t), elemtype(s))
duke@1: : isSubtypeUnchecked(elemtype(t), elemtype(s), warn);
duke@1: } else if (isSubtype(t, s)) {
duke@1: return true;
mcimadamore@41: }
mcimadamore@41: else if (t.tag == TYPEVAR) {
mcimadamore@41: return isSubtypeUnchecked(t.getUpperBound(), s, warn);
mcimadamore@41: }
mcimadamore@93: else if (s.tag == UNDETVAR) {
mcimadamore@93: UndetVar uv = (UndetVar)s;
mcimadamore@93: if (uv.inst != null)
mcimadamore@93: return isSubtypeUnchecked(t, uv.inst, warn);
mcimadamore@93: }
mcimadamore@41: else if (!s.isRaw()) {
duke@1: Type t2 = asSuper(t, s.tsym);
duke@1: if (t2 != null && t2.isRaw()) {
duke@1: if (isReifiable(s))
duke@1: warn.silentUnchecked();
duke@1: else
duke@1: warn.warnUnchecked();
duke@1: return true;
duke@1: }
duke@1: }
duke@1: return false;
duke@1: }
duke@1:
duke@1: /**
duke@1: * Is t a subtype of s?
duke@1: * (not defined for Method and ForAll types)
duke@1: */
duke@1: final public boolean isSubtype(Type t, Type s) {
duke@1: return isSubtype(t, s, true);
duke@1: }
duke@1: final public boolean isSubtypeNoCapture(Type t, Type s) {
duke@1: return isSubtype(t, s, false);
duke@1: }
duke@1: public boolean isSubtype(Type t, Type s, boolean capture) {
duke@1: if (t == s)
duke@1: return true;
duke@1:
duke@1: if (s.tag >= firstPartialTag)
duke@1: return isSuperType(s, t);
duke@1:
mcimadamore@299: if (s.isCompound()) {
mcimadamore@299: for (Type s2 : interfaces(s).prepend(supertype(s))) {
mcimadamore@299: if (!isSubtype(t, s2, capture))
mcimadamore@299: return false;
mcimadamore@299: }
mcimadamore@299: return true;
mcimadamore@299: }
mcimadamore@299:
duke@1: Type lower = lowerBound(s);
duke@1: if (s != lower)
duke@1: return isSubtype(capture ? capture(t) : t, lower, false);
duke@1:
duke@1: return isSubtype.visit(capture ? capture(t) : t, s);
duke@1: }
duke@1: // where
duke@1: private TypeRelation isSubtype = new TypeRelation()
duke@1: {
duke@1: public Boolean visitType(Type t, Type s) {
duke@1: switch (t.tag) {
duke@1: case BYTE: case CHAR:
duke@1: return (t.tag == s.tag ||
duke@1: t.tag + 2 <= s.tag && s.tag <= DOUBLE);
duke@1: case SHORT: case INT: case LONG: case FLOAT: case DOUBLE:
duke@1: return t.tag <= s.tag && s.tag <= DOUBLE;
duke@1: case BOOLEAN: case VOID:
duke@1: return t.tag == s.tag;
duke@1: case TYPEVAR:
duke@1: return isSubtypeNoCapture(t.getUpperBound(), s);
duke@1: case BOT:
duke@1: return
duke@1: s.tag == BOT || s.tag == CLASS ||
duke@1: s.tag == ARRAY || s.tag == TYPEVAR;
duke@1: case NONE:
duke@1: return false;
duke@1: default:
duke@1: throw new AssertionError("isSubtype " + t.tag);
duke@1: }
duke@1: }
duke@1:
duke@1: private Set cache = new HashSet();
duke@1:
duke@1: private boolean containsTypeRecursive(Type t, Type s) {
duke@1: TypePair pair = new TypePair(t, s);
duke@1: if (cache.add(pair)) {
duke@1: try {
duke@1: return containsType(t.getTypeArguments(),
duke@1: s.getTypeArguments());
duke@1: } finally {
duke@1: cache.remove(pair);
duke@1: }
duke@1: } else {
duke@1: return containsType(t.getTypeArguments(),
duke@1: rewriteSupers(s).getTypeArguments());
duke@1: }
duke@1: }
duke@1:
duke@1: private Type rewriteSupers(Type t) {
duke@1: if (!t.isParameterized())
duke@1: return t;
duke@1: ListBuffer from = lb();
duke@1: ListBuffer to = lb();
duke@1: adaptSelf(t, from, to);
duke@1: if (from.isEmpty())
duke@1: return t;
duke@1: ListBuffer rewrite = lb();
duke@1: boolean changed = false;
duke@1: for (Type orig : to.toList()) {
duke@1: Type s = rewriteSupers(orig);
duke@1: if (s.isSuperBound() && !s.isExtendsBound()) {
duke@1: s = new WildcardType(syms.objectType,
duke@1: BoundKind.UNBOUND,
duke@1: syms.boundClass);
duke@1: changed = true;
duke@1: } else if (s != orig) {
duke@1: s = new WildcardType(upperBound(s),
duke@1: BoundKind.EXTENDS,
duke@1: syms.boundClass);
duke@1: changed = true;
duke@1: }
duke@1: rewrite.append(s);
duke@1: }
duke@1: if (changed)
duke@1: return subst(t.tsym.type, from.toList(), rewrite.toList());
duke@1: else
duke@1: return t;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Boolean visitClassType(ClassType t, Type s) {
duke@1: Type sup = asSuper(t, s.tsym);
duke@1: return sup != null
duke@1: && sup.tsym == s.tsym
duke@1: // You're not allowed to write
duke@1: // Vector
duke@1:
duke@1: //
duke@1: /**
duke@1: * Is t a supertype of s?
duke@1: */
duke@1: public boolean isSuperType(Type t, Type s) {
duke@1: switch (t.tag) {
duke@1: case ERROR:
duke@1: return true;
duke@1: case UNDETVAR: {
duke@1: UndetVar undet = (UndetVar)t;
duke@1: if (t == s ||
duke@1: undet.qtype == s ||
duke@1: s.tag == ERROR ||
duke@1: s.tag == BOT) return true;
duke@1: if (undet.inst != null)
duke@1: return isSubtype(s, undet.inst);
duke@1: undet.lobounds = undet.lobounds.prepend(s);
duke@1: return true;
duke@1: }
duke@1: default:
duke@1: return isSubtype(s, t);
duke@1: }
duke@1: }
duke@1: //
duke@1:
duke@1: //
duke@1: /**
duke@1: * Are corresponding elements of the lists the same type? If
duke@1: * lists are of different length, return false.
duke@1: */
duke@1: public boolean isSameTypes(List ts, List ss) {
duke@1: while (ts.tail != null && ss.tail != null
duke@1: /*inlined: ts.nonEmpty() && ss.nonEmpty()*/ &&
duke@1: isSameType(ts.head, ss.head)) {
duke@1: ts = ts.tail;
duke@1: ss = ss.tail;
duke@1: }
duke@1: return ts.tail == null && ss.tail == null;
duke@1: /*inlined: ts.isEmpty() && ss.isEmpty();*/
duke@1: }
duke@1:
duke@1: /**
duke@1: * Is t the same type as s?
duke@1: */
duke@1: public boolean isSameType(Type t, Type s) {
duke@1: return isSameType.visit(t, s);
duke@1: }
duke@1: // where
duke@1: private TypeRelation isSameType = new TypeRelation() {
duke@1:
duke@1: public Boolean visitType(Type t, Type s) {
duke@1: if (t == s)
duke@1: return true;
duke@1:
duke@1: if (s.tag >= firstPartialTag)
duke@1: return visit(s, t);
duke@1:
duke@1: switch (t.tag) {
duke@1: case BYTE: case CHAR: case SHORT: case INT: case LONG: case FLOAT:
duke@1: case DOUBLE: case BOOLEAN: case VOID: case BOT: case NONE:
duke@1: return t.tag == s.tag;
duke@1: case TYPEVAR:
duke@1: return s.isSuperBound()
duke@1: && !s.isExtendsBound()
duke@1: && visit(t, upperBound(s));
duke@1: default:
duke@1: throw new AssertionError("isSameType " + t.tag);
duke@1: }
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Boolean visitWildcardType(WildcardType t, Type s) {
duke@1: if (s.tag >= firstPartialTag)
duke@1: return visit(s, t);
duke@1: else
duke@1: return false;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Boolean visitClassType(ClassType t, Type s) {
duke@1: if (t == s)
duke@1: return true;
duke@1:
duke@1: if (s.tag >= firstPartialTag)
duke@1: return visit(s, t);
duke@1:
duke@1: if (s.isSuperBound() && !s.isExtendsBound())
duke@1: return visit(t, upperBound(s)) && visit(t, lowerBound(s));
duke@1:
duke@1: if (t.isCompound() && s.isCompound()) {
duke@1: if (!visit(supertype(t), supertype(s)))
duke@1: return false;
duke@1:
duke@1: HashSet set = new HashSet();
duke@1: for (Type x : interfaces(t))
duke@1: set.add(new SingletonType(x));
duke@1: for (Type x : interfaces(s)) {
duke@1: if (!set.remove(new SingletonType(x)))
duke@1: return false;
duke@1: }
duke@1: return (set.size() == 0);
duke@1: }
duke@1: return t.tsym == s.tsym
duke@1: && visit(t.getEnclosingType(), s.getEnclosingType())
duke@1: && containsTypeEquivalent(t.getTypeArguments(), s.getTypeArguments());
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Boolean visitArrayType(ArrayType t, Type s) {
duke@1: if (t == s)
duke@1: return true;
duke@1:
duke@1: if (s.tag >= firstPartialTag)
duke@1: return visit(s, t);
duke@1:
duke@1: return s.tag == ARRAY
duke@1: && containsTypeEquivalent(t.elemtype, elemtype(s));
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Boolean visitMethodType(MethodType t, Type s) {
duke@1: // isSameType for methods does not take thrown
duke@1: // exceptions into account!
duke@1: return hasSameArgs(t, s) && visit(t.getReturnType(), s.getReturnType());
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Boolean visitPackageType(PackageType t, Type s) {
duke@1: return t == s;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Boolean visitForAll(ForAll t, Type s) {
duke@1: if (s.tag != FORALL)
duke@1: return false;
duke@1:
duke@1: ForAll forAll = (ForAll)s;
duke@1: return hasSameBounds(t, forAll)
duke@1: && visit(t.qtype, subst(forAll.qtype, forAll.tvars, t.tvars));
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Boolean visitUndetVar(UndetVar t, Type s) {
duke@1: if (s.tag == WILDCARD)
duke@1: // FIXME, this might be leftovers from before capture conversion
duke@1: return false;
duke@1:
duke@1: if (t == s || t.qtype == s || s.tag == ERROR || s.tag == UNKNOWN)
duke@1: return true;
duke@1:
duke@1: if (t.inst != null)
duke@1: return visit(t.inst, s);
duke@1:
duke@1: t.inst = fromUnknownFun.apply(s);
duke@1: for (List l = t.lobounds; l.nonEmpty(); l = l.tail) {
duke@1: if (!isSubtype(l.head, t.inst))
duke@1: return false;
duke@1: }
duke@1: for (List l = t.hibounds; l.nonEmpty(); l = l.tail) {
duke@1: if (!isSubtype(t.inst, l.head))
duke@1: return false;
duke@1: }
duke@1: return true;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Boolean visitErrorType(ErrorType t, Type s) {
duke@1: return true;
duke@1: }
duke@1: };
duke@1: //
duke@1:
duke@1: //
duke@1: /**
duke@1: * A mapping that turns all unknown types in this type to fresh
duke@1: * unknown variables.
duke@1: */
duke@1: public Mapping fromUnknownFun = new Mapping("fromUnknownFun") {
duke@1: public Type apply(Type t) {
duke@1: if (t.tag == UNKNOWN) return new UndetVar(t);
duke@1: else return t.map(this);
duke@1: }
duke@1: };
duke@1: //
duke@1:
duke@1: //
duke@1: public boolean containedBy(Type t, Type s) {
duke@1: switch (t.tag) {
duke@1: case UNDETVAR:
duke@1: if (s.tag == WILDCARD) {
duke@1: UndetVar undetvar = (UndetVar)t;
mcimadamore@210: WildcardType wt = (WildcardType)s;
mcimadamore@210: switch(wt.kind) {
mcimadamore@210: case UNBOUND: //similar to ? extends Object
mcimadamore@210: case EXTENDS: {
mcimadamore@210: Type bound = upperBound(s);
mcimadamore@210: // We should check the new upper bound against any of the
mcimadamore@210: // undetvar's lower bounds.
mcimadamore@210: for (Type t2 : undetvar.lobounds) {
mcimadamore@210: if (!isSubtype(t2, bound))
mcimadamore@210: return false;
mcimadamore@210: }
mcimadamore@210: undetvar.hibounds = undetvar.hibounds.prepend(bound);
mcimadamore@210: break;
mcimadamore@210: }
mcimadamore@210: case SUPER: {
mcimadamore@210: Type bound = lowerBound(s);
mcimadamore@210: // We should check the new lower bound against any of the
mcimadamore@210: // undetvar's lower bounds.
mcimadamore@210: for (Type t2 : undetvar.hibounds) {
mcimadamore@210: if (!isSubtype(bound, t2))
mcimadamore@210: return false;
mcimadamore@210: }
mcimadamore@210: undetvar.lobounds = undetvar.lobounds.prepend(bound);
mcimadamore@210: break;
mcimadamore@210: }
mcimadamore@162: }
duke@1: return true;
duke@1: } else {
duke@1: return isSameType(t, s);
duke@1: }
duke@1: case ERROR:
duke@1: return true;
duke@1: default:
duke@1: return containsType(s, t);
duke@1: }
duke@1: }
duke@1:
duke@1: boolean containsType(List ts, List ss) {
duke@1: while (ts.nonEmpty() && ss.nonEmpty()
duke@1: && containsType(ts.head, ss.head)) {
duke@1: ts = ts.tail;
duke@1: ss = ss.tail;
duke@1: }
duke@1: return ts.isEmpty() && ss.isEmpty();
duke@1: }
duke@1:
duke@1: /**
duke@1: * Check if t contains s.
duke@1: *
duke@1: *
This relation is only used by ClassType.isSubtype(), that
duke@1: * is,
duke@1: *
duke@1: *
{@code C <: C if T contains S.}
duke@1: *
duke@1: *
Because of F-bounds, this relation can lead to infinite
duke@1: * recursion. Thus we must somehow break that recursion. Notice
duke@1: * that containsType() is only called from ClassType.isSubtype().
duke@1: * Since the arguments have already been checked against their
duke@1: * bounds, we know:
duke@1: *
duke@1: *
{@code U(S) <: U(T) if T is "super" bound (U(T) *is* the bound)}
duke@1: *
duke@1: *
{@code L(T) <: L(S) if T is "extends" bound (L(T) is bottom)}
duke@1: *
duke@1: * @param t a type
duke@1: * @param s a type
duke@1: */
duke@1: public boolean containsType(Type t, Type s) {
duke@1: return containsType.visit(t, s);
duke@1: }
duke@1: // where
duke@1: private TypeRelation containsType = new TypeRelation() {
duke@1:
duke@1: private Type U(Type t) {
duke@1: while (t.tag == WILDCARD) {
duke@1: WildcardType w = (WildcardType)t;
duke@1: if (w.isSuperBound())
duke@1: return w.bound == null ? syms.objectType : w.bound.bound;
duke@1: else
duke@1: t = w.type;
duke@1: }
duke@1: return t;
duke@1: }
duke@1:
duke@1: private Type L(Type t) {
duke@1: while (t.tag == WILDCARD) {
duke@1: WildcardType w = (WildcardType)t;
duke@1: if (w.isExtendsBound())
duke@1: return syms.botType;
duke@1: else
duke@1: t = w.type;
duke@1: }
duke@1: return t;
duke@1: }
duke@1:
duke@1: public Boolean visitType(Type t, Type s) {
duke@1: if (s.tag >= firstPartialTag)
duke@1: return containedBy(s, t);
duke@1: else
duke@1: return isSameType(t, s);
duke@1: }
duke@1:
duke@1: void debugContainsType(WildcardType t, Type s) {
duke@1: System.err.println();
duke@1: System.err.format(" does %s contain %s?%n", t, s);
duke@1: System.err.format(" %s U(%s) <: U(%s) %s = %s%n",
duke@1: upperBound(s), s, t, U(t),
duke@1: t.isSuperBound()
duke@1: || isSubtypeNoCapture(upperBound(s), U(t)));
duke@1: System.err.format(" %s L(%s) <: L(%s) %s = %s%n",
duke@1: L(t), t, s, lowerBound(s),
duke@1: t.isExtendsBound()
duke@1: || isSubtypeNoCapture(L(t), lowerBound(s)));
duke@1: System.err.println();
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Boolean visitWildcardType(WildcardType t, Type s) {
duke@1: if (s.tag >= firstPartialTag)
duke@1: return containedBy(s, t);
duke@1: else {
duke@1: // debugContainsType(t, s);
duke@1: return isSameWildcard(t, s)
duke@1: || isCaptureOf(s, t)
duke@1: || ((t.isExtendsBound() || isSubtypeNoCapture(L(t), lowerBound(s))) &&
duke@1: (t.isSuperBound() || isSubtypeNoCapture(upperBound(s), U(t))));
duke@1: }
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Boolean visitUndetVar(UndetVar t, Type s) {
duke@1: if (s.tag != WILDCARD)
duke@1: return isSameType(t, s);
duke@1: else
duke@1: return false;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Boolean visitErrorType(ErrorType t, Type s) {
duke@1: return true;
duke@1: }
duke@1: };
duke@1:
duke@1: public boolean isCaptureOf(Type s, WildcardType t) {
mcimadamore@79: if (s.tag != TYPEVAR || !((TypeVar)s).isCaptured())
duke@1: return false;
duke@1: return isSameWildcard(t, ((CapturedType)s).wildcard);
duke@1: }
duke@1:
duke@1: public boolean isSameWildcard(WildcardType t, Type s) {
duke@1: if (s.tag != WILDCARD)
duke@1: return false;
duke@1: WildcardType w = (WildcardType)s;
duke@1: return w.kind == t.kind && w.type == t.type;
duke@1: }
duke@1:
duke@1: public boolean containsTypeEquivalent(List ts, List ss) {
duke@1: while (ts.nonEmpty() && ss.nonEmpty()
duke@1: && containsTypeEquivalent(ts.head, ss.head)) {
duke@1: ts = ts.tail;
duke@1: ss = ss.tail;
duke@1: }
duke@1: return ts.isEmpty() && ss.isEmpty();
duke@1: }
duke@1: //
duke@1:
duke@1: //
duke@1: public boolean isCastable(Type t, Type s) {
duke@1: return isCastable(t, s, Warner.noWarnings);
duke@1: }
duke@1:
duke@1: /**
duke@1: * Is t is castable to s?
duke@1: * s is assumed to be an erased type.
duke@1: * (not defined for Method and ForAll types).
duke@1: */
duke@1: public boolean isCastable(Type t, Type s, Warner warn) {
duke@1: if (t == s)
duke@1: return true;
duke@1:
duke@1: if (t.isPrimitive() != s.isPrimitive())
duke@1: return allowBoxing && isConvertible(t, s, warn);
duke@1:
duke@1: if (warn != warnStack.head) {
duke@1: try {
duke@1: warnStack = warnStack.prepend(warn);
mcimadamore@185: return isCastable.visit(t,s);
duke@1: } finally {
duke@1: warnStack = warnStack.tail;
duke@1: }
duke@1: } else {
mcimadamore@185: return isCastable.visit(t,s);
duke@1: }
duke@1: }
duke@1: // where
duke@1: private TypeRelation isCastable = new TypeRelation() {
duke@1:
duke@1: public Boolean visitType(Type t, Type s) {
duke@1: if (s.tag == ERROR)
duke@1: return true;
duke@1:
duke@1: switch (t.tag) {
duke@1: case BYTE: case CHAR: case SHORT: case INT: case LONG: case FLOAT:
duke@1: case DOUBLE:
duke@1: return s.tag <= DOUBLE;
duke@1: case BOOLEAN:
duke@1: return s.tag == BOOLEAN;
duke@1: case VOID:
duke@1: return false;
duke@1: case BOT:
duke@1: return isSubtype(t, s);
duke@1: default:
duke@1: throw new AssertionError();
duke@1: }
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Boolean visitWildcardType(WildcardType t, Type s) {
duke@1: return isCastable(upperBound(t), s, warnStack.head);
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Boolean visitClassType(ClassType t, Type s) {
duke@1: if (s.tag == ERROR || s.tag == BOT)
duke@1: return true;
duke@1:
duke@1: if (s.tag == TYPEVAR) {
duke@1: if (isCastable(s.getUpperBound(), t, Warner.noWarnings)) {
duke@1: warnStack.head.warnUnchecked();
duke@1: return true;
duke@1: } else {
duke@1: return false;
duke@1: }
duke@1: }
duke@1:
duke@1: if (t.isCompound()) {
mcimadamore@211: Warner oldWarner = warnStack.head;
mcimadamore@211: warnStack.head = Warner.noWarnings;
duke@1: if (!visit(supertype(t), s))
duke@1: return false;
duke@1: for (Type intf : interfaces(t)) {
duke@1: if (!visit(intf, s))
duke@1: return false;
duke@1: }
mcimadamore@211: if (warnStack.head.unchecked == true)
mcimadamore@211: oldWarner.warnUnchecked();
duke@1: return true;
duke@1: }
duke@1:
duke@1: if (s.isCompound()) {
duke@1: // call recursively to reuse the above code
duke@1: return visitClassType((ClassType)s, t);
duke@1: }
duke@1:
duke@1: if (s.tag == CLASS || s.tag == ARRAY) {
duke@1: boolean upcast;
duke@1: if ((upcast = isSubtype(erasure(t), erasure(s)))
duke@1: || isSubtype(erasure(s), erasure(t))) {
duke@1: if (!upcast && s.tag == ARRAY) {
duke@1: if (!isReifiable(s))
duke@1: warnStack.head.warnUnchecked();
duke@1: return true;
duke@1: } else if (s.isRaw()) {
duke@1: return true;
duke@1: } else if (t.isRaw()) {
duke@1: if (!isUnbounded(s))
duke@1: warnStack.head.warnUnchecked();
duke@1: return true;
duke@1: }
duke@1: // Assume |a| <: |b|
duke@1: final Type a = upcast ? t : s;
duke@1: final Type b = upcast ? s : t;
duke@1: final boolean HIGH = true;
duke@1: final boolean LOW = false;
duke@1: final boolean DONT_REWRITE_TYPEVARS = false;
duke@1: Type aHigh = rewriteQuantifiers(a, HIGH, DONT_REWRITE_TYPEVARS);
duke@1: Type aLow = rewriteQuantifiers(a, LOW, DONT_REWRITE_TYPEVARS);
duke@1: Type bHigh = rewriteQuantifiers(b, HIGH, DONT_REWRITE_TYPEVARS);
duke@1: Type bLow = rewriteQuantifiers(b, LOW, DONT_REWRITE_TYPEVARS);
duke@1: Type lowSub = asSub(bLow, aLow.tsym);
duke@1: Type highSub = (lowSub == null) ? null : asSub(bHigh, aHigh.tsym);
duke@1: if (highSub == null) {
duke@1: final boolean REWRITE_TYPEVARS = true;
duke@1: aHigh = rewriteQuantifiers(a, HIGH, REWRITE_TYPEVARS);
duke@1: aLow = rewriteQuantifiers(a, LOW, REWRITE_TYPEVARS);
duke@1: bHigh = rewriteQuantifiers(b, HIGH, REWRITE_TYPEVARS);
duke@1: bLow = rewriteQuantifiers(b, LOW, REWRITE_TYPEVARS);
duke@1: lowSub = asSub(bLow, aLow.tsym);
duke@1: highSub = (lowSub == null) ? null : asSub(bHigh, aHigh.tsym);
duke@1: }
duke@1: if (highSub != null) {
duke@1: assert a.tsym == highSub.tsym && a.tsym == lowSub.tsym
duke@1: : a.tsym + " != " + highSub.tsym + " != " + lowSub.tsym;
mcimadamore@185: if (!disjointTypes(aHigh.allparams(), highSub.allparams())
mcimadamore@185: && !disjointTypes(aHigh.allparams(), lowSub.allparams())
mcimadamore@185: && !disjointTypes(aLow.allparams(), highSub.allparams())
mcimadamore@185: && !disjointTypes(aLow.allparams(), lowSub.allparams())) {
mcimadamore@235: if (upcast ? giveWarning(a, b) :
mcimadamore@235: giveWarning(b, a))
duke@1: warnStack.head.warnUnchecked();
duke@1: return true;
duke@1: }
duke@1: }
duke@1: if (isReifiable(s))
duke@1: return isSubtypeUnchecked(a, b);
duke@1: else
duke@1: return isSubtypeUnchecked(a, b, warnStack.head);
duke@1: }
duke@1:
duke@1: // Sidecast
duke@1: if (s.tag == CLASS) {
duke@1: if ((s.tsym.flags() & INTERFACE) != 0) {
duke@1: return ((t.tsym.flags() & FINAL) == 0)
duke@1: ? sideCast(t, s, warnStack.head)
duke@1: : sideCastFinal(t, s, warnStack.head);
duke@1: } else if ((t.tsym.flags() & INTERFACE) != 0) {
duke@1: return ((s.tsym.flags() & FINAL) == 0)
duke@1: ? sideCast(t, s, warnStack.head)
duke@1: : sideCastFinal(t, s, warnStack.head);
duke@1: } else {
duke@1: // unrelated class types
duke@1: return false;
duke@1: }
duke@1: }
duke@1: }
duke@1: return false;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Boolean visitArrayType(ArrayType t, Type s) {
duke@1: switch (s.tag) {
duke@1: case ERROR:
duke@1: case BOT:
duke@1: return true;
duke@1: case TYPEVAR:
duke@1: if (isCastable(s, t, Warner.noWarnings)) {
duke@1: warnStack.head.warnUnchecked();
duke@1: return true;
duke@1: } else {
duke@1: return false;
duke@1: }
duke@1: case CLASS:
duke@1: return isSubtype(t, s);
duke@1: case ARRAY:
duke@1: if (elemtype(t).tag <= lastBaseTag) {
duke@1: return elemtype(t).tag == elemtype(s).tag;
duke@1: } else {
duke@1: return visit(elemtype(t), elemtype(s));
duke@1: }
duke@1: default:
duke@1: return false;
duke@1: }
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Boolean visitTypeVar(TypeVar t, Type s) {
duke@1: switch (s.tag) {
duke@1: case ERROR:
duke@1: case BOT:
duke@1: return true;
duke@1: case TYPEVAR:
duke@1: if (isSubtype(t, s)) {
duke@1: return true;
duke@1: } else if (isCastable(t.bound, s, Warner.noWarnings)) {
duke@1: warnStack.head.warnUnchecked();
duke@1: return true;
duke@1: } else {
duke@1: return false;
duke@1: }
duke@1: default:
duke@1: return isCastable(t.bound, s, warnStack.head);
duke@1: }
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Boolean visitErrorType(ErrorType t, Type s) {
duke@1: return true;
duke@1: }
duke@1: };
duke@1: //
duke@1:
duke@1: //
duke@1: public boolean disjointTypes(List ts, List ss) {
duke@1: while (ts.tail != null && ss.tail != null) {
duke@1: if (disjointType(ts.head, ss.head)) return true;
duke@1: ts = ts.tail;
duke@1: ss = ss.tail;
duke@1: }
duke@1: return false;
duke@1: }
duke@1:
duke@1: /**
duke@1: * Two types or wildcards are considered disjoint if it can be
duke@1: * proven that no type can be contained in both. It is
duke@1: * conservative in that it is allowed to say that two types are
duke@1: * not disjoint, even though they actually are.
duke@1: *
duke@1: * The type C is castable to C exactly if X and Y are not
duke@1: * disjoint.
duke@1: */
duke@1: public boolean disjointType(Type t, Type s) {
duke@1: return disjointType.visit(t, s);
duke@1: }
duke@1: // where
duke@1: private TypeRelation disjointType = new TypeRelation() {
duke@1:
duke@1: private Set cache = new HashSet();
duke@1:
duke@1: public Boolean visitType(Type t, Type s) {
duke@1: if (s.tag == WILDCARD)
duke@1: return visit(s, t);
duke@1: else
duke@1: return notSoftSubtypeRecursive(t, s) || notSoftSubtypeRecursive(s, t);
duke@1: }
duke@1:
duke@1: private boolean isCastableRecursive(Type t, Type s) {
duke@1: TypePair pair = new TypePair(t, s);
duke@1: if (cache.add(pair)) {
duke@1: try {
duke@1: return Types.this.isCastable(t, s);
duke@1: } finally {
duke@1: cache.remove(pair);
duke@1: }
duke@1: } else {
duke@1: return true;
duke@1: }
duke@1: }
duke@1:
duke@1: private boolean notSoftSubtypeRecursive(Type t, Type s) {
duke@1: TypePair pair = new TypePair(t, s);
duke@1: if (cache.add(pair)) {
duke@1: try {
duke@1: return Types.this.notSoftSubtype(t, s);
duke@1: } finally {
duke@1: cache.remove(pair);
duke@1: }
duke@1: } else {
duke@1: return false;
duke@1: }
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Boolean visitWildcardType(WildcardType t, Type s) {
duke@1: if (t.isUnbound())
duke@1: return false;
duke@1:
duke@1: if (s.tag != WILDCARD) {
duke@1: if (t.isExtendsBound())
duke@1: return notSoftSubtypeRecursive(s, t.type);
duke@1: else // isSuperBound()
duke@1: return notSoftSubtypeRecursive(t.type, s);
duke@1: }
duke@1:
duke@1: if (s.isUnbound())
duke@1: return false;
duke@1:
duke@1: if (t.isExtendsBound()) {
duke@1: if (s.isExtendsBound())
duke@1: return !isCastableRecursive(t.type, upperBound(s));
duke@1: else if (s.isSuperBound())
duke@1: return notSoftSubtypeRecursive(lowerBound(s), t.type);
duke@1: } else if (t.isSuperBound()) {
duke@1: if (s.isExtendsBound())
duke@1: return notSoftSubtypeRecursive(t.type, upperBound(s));
duke@1: }
duke@1: return false;
duke@1: }
duke@1: };
duke@1: //
duke@1:
duke@1: //
duke@1: /**
duke@1: * Returns the lower bounds of the formals of a method.
duke@1: */
duke@1: public List lowerBoundArgtypes(Type t) {
duke@1: return map(t.getParameterTypes(), lowerBoundMapping);
duke@1: }
duke@1: private final Mapping lowerBoundMapping = new Mapping("lowerBound") {
duke@1: public Type apply(Type t) {
duke@1: return lowerBound(t);
duke@1: }
duke@1: };
duke@1: //
duke@1:
duke@1: //
duke@1: /**
duke@1: * This relation answers the question: is impossible that
duke@1: * something of type `t' can be a subtype of `s'? This is
duke@1: * different from the question "is `t' not a subtype of `s'?"
duke@1: * when type variables are involved: Integer is not a subtype of T
duke@1: * where but it is not true that Integer cannot
duke@1: * possibly be a subtype of T.
duke@1: */
duke@1: public boolean notSoftSubtype(Type t, Type s) {
duke@1: if (t == s) return false;
duke@1: if (t.tag == TYPEVAR) {
duke@1: TypeVar tv = (TypeVar) t;
duke@1: if (s.tag == TYPEVAR)
duke@1: s = s.getUpperBound();
duke@1: return !isCastable(tv.bound,
duke@1: s,
duke@1: Warner.noWarnings);
duke@1: }
duke@1: if (s.tag != WILDCARD)
duke@1: s = upperBound(s);
duke@1: if (s.tag == TYPEVAR)
duke@1: s = s.getUpperBound();
mcimadamore@185:
duke@1: return !isSubtype(t, s);
duke@1: }
duke@1: //
duke@1:
duke@1: //
duke@1: public boolean isReifiable(Type t) {
duke@1: return isReifiable.visit(t);
duke@1: }
duke@1: // where
duke@1: private UnaryVisitor isReifiable = new UnaryVisitor() {
duke@1:
duke@1: public Boolean visitType(Type t, Void ignored) {
duke@1: return true;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Boolean visitClassType(ClassType t, Void ignored) {
mcimadamore@356: if (t.isCompound())
mcimadamore@356: return false;
mcimadamore@356: else {
mcimadamore@356: if (!t.isParameterized())
mcimadamore@356: return true;
mcimadamore@356:
mcimadamore@356: for (Type param : t.allparams()) {
mcimadamore@356: if (!param.isUnbound())
mcimadamore@356: return false;
mcimadamore@356: }
duke@1: return true;
duke@1: }
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Boolean visitArrayType(ArrayType t, Void ignored) {
duke@1: return visit(t.elemtype);
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Boolean visitTypeVar(TypeVar t, Void ignored) {
duke@1: return false;
duke@1: }
duke@1: };
duke@1: //
duke@1:
duke@1: //
duke@1: public boolean isArray(Type t) {
duke@1: while (t.tag == WILDCARD)
duke@1: t = upperBound(t);
duke@1: return t.tag == ARRAY;
duke@1: }
duke@1:
duke@1: /**
duke@1: * The element type of an array.
duke@1: */
duke@1: public Type elemtype(Type t) {
duke@1: switch (t.tag) {
duke@1: case WILDCARD:
duke@1: return elemtype(upperBound(t));
duke@1: case ARRAY:
duke@1: return ((ArrayType)t).elemtype;
duke@1: case FORALL:
duke@1: return elemtype(((ForAll)t).qtype);
duke@1: case ERROR:
duke@1: return t;
duke@1: default:
duke@1: return null;
duke@1: }
duke@1: }
duke@1:
duke@1: /**
duke@1: * Mapping to take element type of an arraytype
duke@1: */
duke@1: private Mapping elemTypeFun = new Mapping ("elemTypeFun") {
duke@1: public Type apply(Type t) { return elemtype(t); }
duke@1: };
duke@1:
duke@1: /**
duke@1: * The number of dimensions of an array type.
duke@1: */
duke@1: public int dimensions(Type t) {
duke@1: int result = 0;
duke@1: while (t.tag == ARRAY) {
duke@1: result++;
duke@1: t = elemtype(t);
duke@1: }
duke@1: return result;
duke@1: }
duke@1: //
duke@1:
duke@1: //
duke@1: /**
duke@1: * Return the (most specific) base type of t that starts with the
duke@1: * given symbol. If none exists, return null.
duke@1: *
duke@1: * @param t a type
duke@1: * @param sym a symbol
duke@1: */
duke@1: public Type asSuper(Type t, Symbol sym) {
duke@1: return asSuper.visit(t, sym);
duke@1: }
duke@1: // where
duke@1: private SimpleVisitor asSuper = new SimpleVisitor() {
duke@1:
duke@1: public Type visitType(Type t, Symbol sym) {
duke@1: return null;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Type visitClassType(ClassType t, Symbol sym) {
duke@1: if (t.tsym == sym)
duke@1: return t;
duke@1:
duke@1: Type st = supertype(t);
mcimadamore@19: if (st.tag == CLASS || st.tag == TYPEVAR || st.tag == ERROR) {
duke@1: Type x = asSuper(st, sym);
duke@1: if (x != null)
duke@1: return x;
duke@1: }
duke@1: if ((sym.flags() & INTERFACE) != 0) {
duke@1: for (List l = interfaces(t); l.nonEmpty(); l = l.tail) {
duke@1: Type x = asSuper(l.head, sym);
duke@1: if (x != null)
duke@1: return x;
duke@1: }
duke@1: }
duke@1: return null;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Type visitArrayType(ArrayType t, Symbol sym) {
duke@1: return isSubtype(t, sym.type) ? sym.type : null;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Type visitTypeVar(TypeVar t, Symbol sym) {
mcimadamore@19: if (t.tsym == sym)
mcimadamore@19: return t;
mcimadamore@19: else
mcimadamore@19: return asSuper(t.bound, sym);
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Type visitErrorType(ErrorType t, Symbol sym) {
duke@1: return t;
duke@1: }
duke@1: };
duke@1:
duke@1: /**
duke@1: * Return the base type of t or any of its outer types that starts
duke@1: * with the given symbol. If none exists, return null.
duke@1: *
duke@1: * @param t a type
duke@1: * @param sym a symbol
duke@1: */
duke@1: public Type asOuterSuper(Type t, Symbol sym) {
duke@1: switch (t.tag) {
duke@1: case CLASS:
duke@1: do {
duke@1: Type s = asSuper(t, sym);
duke@1: if (s != null) return s;
duke@1: t = t.getEnclosingType();
duke@1: } while (t.tag == CLASS);
duke@1: return null;
duke@1: case ARRAY:
duke@1: return isSubtype(t, sym.type) ? sym.type : null;
duke@1: case TYPEVAR:
duke@1: return asSuper(t, sym);
duke@1: case ERROR:
duke@1: return t;
duke@1: default:
duke@1: return null;
duke@1: }
duke@1: }
duke@1:
duke@1: /**
duke@1: * Return the base type of t or any of its enclosing types that
duke@1: * starts with the given symbol. If none exists, return null.
duke@1: *
duke@1: * @param t a type
duke@1: * @param sym a symbol
duke@1: */
duke@1: public Type asEnclosingSuper(Type t, Symbol sym) {
duke@1: switch (t.tag) {
duke@1: case CLASS:
duke@1: do {
duke@1: Type s = asSuper(t, sym);
duke@1: if (s != null) return s;
duke@1: Type outer = t.getEnclosingType();
duke@1: t = (outer.tag == CLASS) ? outer :
duke@1: (t.tsym.owner.enclClass() != null) ? t.tsym.owner.enclClass().type :
duke@1: Type.noType;
duke@1: } while (t.tag == CLASS);
duke@1: return null;
duke@1: case ARRAY:
duke@1: return isSubtype(t, sym.type) ? sym.type : null;
duke@1: case TYPEVAR:
duke@1: return asSuper(t, sym);
duke@1: case ERROR:
duke@1: return t;
duke@1: default:
duke@1: return null;
duke@1: }
duke@1: }
duke@1: //
duke@1:
duke@1: //
duke@1: /**
duke@1: * The type of given symbol, seen as a member of t.
duke@1: *
duke@1: * @param t a type
duke@1: * @param sym a symbol
duke@1: */
duke@1: public Type memberType(Type t, Symbol sym) {
duke@1: return (sym.flags() & STATIC) != 0
duke@1: ? sym.type
duke@1: : memberType.visit(t, sym);
mcimadamore@341: }
duke@1: // where
duke@1: private SimpleVisitor memberType = new SimpleVisitor() {
duke@1:
duke@1: public Type visitType(Type t, Symbol sym) {
duke@1: return sym.type;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Type visitWildcardType(WildcardType t, Symbol sym) {
duke@1: return memberType(upperBound(t), sym);
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Type visitClassType(ClassType t, Symbol sym) {
duke@1: Symbol owner = sym.owner;
duke@1: long flags = sym.flags();
duke@1: if (((flags & STATIC) == 0) && owner.type.isParameterized()) {
duke@1: Type base = asOuterSuper(t, owner);
mcimadamore@134: //if t is an intersection type T = CT & I1 & I2 ... & In
mcimadamore@134: //its supertypes CT, I1, ... In might contain wildcards
mcimadamore@134: //so we need to go through capture conversion
mcimadamore@134: base = t.isCompound() ? capture(base) : base;
duke@1: if (base != null) {
duke@1: List ownerParams = owner.type.allparams();
duke@1: List baseParams = base.allparams();
duke@1: if (ownerParams.nonEmpty()) {
duke@1: if (baseParams.isEmpty()) {
duke@1: // then base is a raw type
duke@1: return erasure(sym.type);
duke@1: } else {
duke@1: return subst(sym.type, ownerParams, baseParams);
duke@1: }
duke@1: }
duke@1: }
duke@1: }
duke@1: return sym.type;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Type visitTypeVar(TypeVar t, Symbol sym) {
duke@1: return memberType(t.bound, sym);
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Type visitErrorType(ErrorType t, Symbol sym) {
duke@1: return t;
duke@1: }
duke@1: };
duke@1: //
duke@1:
duke@1: //
duke@1: public boolean isAssignable(Type t, Type s) {
duke@1: return isAssignable(t, s, Warner.noWarnings);
duke@1: }
duke@1:
duke@1: /**
duke@1: * Is t assignable to s?
duke@1: * Equivalent to subtype except for constant values and raw
duke@1: * types.
duke@1: * (not defined for Method and ForAll types)
duke@1: */
duke@1: public boolean isAssignable(Type t, Type s, Warner warn) {
duke@1: if (t.tag == ERROR)
duke@1: return true;
duke@1: if (t.tag <= INT && t.constValue() != null) {
duke@1: int value = ((Number)t.constValue()).intValue();
duke@1: switch (s.tag) {
duke@1: case BYTE:
duke@1: if (Byte.MIN_VALUE <= value && value <= Byte.MAX_VALUE)
duke@1: return true;
duke@1: break;
duke@1: case CHAR:
duke@1: if (Character.MIN_VALUE <= value && value <= Character.MAX_VALUE)
duke@1: return true;
duke@1: break;
duke@1: case SHORT:
duke@1: if (Short.MIN_VALUE <= value && value <= Short.MAX_VALUE)
duke@1: return true;
duke@1: break;
duke@1: case INT:
duke@1: return true;
duke@1: case CLASS:
duke@1: switch (unboxedType(s).tag) {
duke@1: case BYTE:
duke@1: case CHAR:
duke@1: case SHORT:
duke@1: return isAssignable(t, unboxedType(s), warn);
duke@1: }
duke@1: break;
duke@1: }
duke@1: }
duke@1: return isConvertible(t, s, warn);
duke@1: }
duke@1: //
duke@1:
duke@1: //
duke@1: /**
duke@1: * The erasure of t {@code |t|} -- the type that results when all
duke@1: * type parameters in t are deleted.
duke@1: */
duke@1: public Type erasure(Type t) {
mcimadamore@30: return erasure(t, false);
mcimadamore@30: }
mcimadamore@30: //where
mcimadamore@30: private Type erasure(Type t, boolean recurse) {
duke@1: if (t.tag <= lastBaseTag)
duke@1: return t; /* fast special case */
duke@1: else
mcimadamore@30: return erasure.visit(t, recurse);
mcimadamore@341: }
duke@1: // where
mcimadamore@30: private SimpleVisitor erasure = new SimpleVisitor() {
mcimadamore@30: public Type visitType(Type t, Boolean recurse) {
duke@1: if (t.tag <= lastBaseTag)
duke@1: return t; /*fast special case*/
duke@1: else
mcimadamore@30: return t.map(recurse ? erasureRecFun : erasureFun);
duke@1: }
duke@1:
duke@1: @Override
mcimadamore@30: public Type visitWildcardType(WildcardType t, Boolean recurse) {
mcimadamore@30: return erasure(upperBound(t), recurse);
duke@1: }
duke@1:
duke@1: @Override
mcimadamore@30: public Type visitClassType(ClassType t, Boolean recurse) {
mcimadamore@30: Type erased = t.tsym.erasure(Types.this);
mcimadamore@30: if (recurse) {
mcimadamore@30: erased = new ErasedClassType(erased.getEnclosingType(),erased.tsym);
mcimadamore@30: }
mcimadamore@30: return erased;
duke@1: }
duke@1:
duke@1: @Override
mcimadamore@30: public Type visitTypeVar(TypeVar t, Boolean recurse) {
mcimadamore@30: return erasure(t.bound, recurse);
duke@1: }
duke@1:
duke@1: @Override
mcimadamore@30: public Type visitErrorType(ErrorType t, Boolean recurse) {
duke@1: return t;
duke@1: }
duke@1: };
mcimadamore@30:
duke@1: private Mapping erasureFun = new Mapping ("erasure") {
duke@1: public Type apply(Type t) { return erasure(t); }
duke@1: };
duke@1:
mcimadamore@30: private Mapping erasureRecFun = new Mapping ("erasureRecursive") {
mcimadamore@30: public Type apply(Type t) { return erasureRecursive(t); }
mcimadamore@30: };
mcimadamore@30:
duke@1: public List erasure(List ts) {
duke@1: return Type.map(ts, erasureFun);
duke@1: }
mcimadamore@30:
mcimadamore@30: public Type erasureRecursive(Type t) {
mcimadamore@30: return erasure(t, true);
mcimadamore@30: }
mcimadamore@30:
mcimadamore@30: public List erasureRecursive(List ts) {
mcimadamore@30: return Type.map(ts, erasureRecFun);
mcimadamore@30: }
duke@1: //
duke@1:
duke@1: //
duke@1: /**
duke@1: * Make a compound type from non-empty list of types
duke@1: *
duke@1: * @param bounds the types from which the compound type is formed
duke@1: * @param supertype is objectType if all bounds are interfaces,
duke@1: * null otherwise.
duke@1: */
duke@1: public Type makeCompoundType(List bounds,
duke@1: Type supertype) {
duke@1: ClassSymbol bc =
duke@1: new ClassSymbol(ABSTRACT|PUBLIC|SYNTHETIC|COMPOUND|ACYCLIC,
duke@1: Type.moreInfo
duke@1: ? names.fromString(bounds.toString())
duke@1: : names.empty,
duke@1: syms.noSymbol);
duke@1: if (bounds.head.tag == TYPEVAR)
duke@1: // error condition, recover
mcimadamore@121: bc.erasure_field = syms.objectType;
mcimadamore@121: else
mcimadamore@121: bc.erasure_field = erasure(bounds.head);
mcimadamore@121: bc.members_field = new Scope(bc);
duke@1: ClassType bt = (ClassType)bc.type;
duke@1: bt.allparams_field = List.nil();
duke@1: if (supertype != null) {
duke@1: bt.supertype_field = supertype;
duke@1: bt.interfaces_field = bounds;
duke@1: } else {
duke@1: bt.supertype_field = bounds.head;
duke@1: bt.interfaces_field = bounds.tail;
duke@1: }
duke@1: assert bt.supertype_field.tsym.completer != null
duke@1: || !bt.supertype_field.isInterface()
duke@1: : bt.supertype_field;
duke@1: return bt;
duke@1: }
duke@1:
duke@1: /**
duke@1: * Same as {@link #makeCompoundType(List,Type)}, except that the
duke@1: * second parameter is computed directly. Note that this might
duke@1: * cause a symbol completion. Hence, this version of
duke@1: * makeCompoundType may not be called during a classfile read.
duke@1: */
duke@1: public Type makeCompoundType(List bounds) {
duke@1: Type supertype = (bounds.head.tsym.flags() & INTERFACE) != 0 ?
duke@1: supertype(bounds.head) : null;
duke@1: return makeCompoundType(bounds, supertype);
duke@1: }
duke@1:
duke@1: /**
duke@1: * A convenience wrapper for {@link #makeCompoundType(List)}; the
duke@1: * arguments are converted to a list and passed to the other
duke@1: * method. Note that this might cause a symbol completion.
duke@1: * Hence, this version of makeCompoundType may not be called
duke@1: * during a classfile read.
duke@1: */
duke@1: public Type makeCompoundType(Type bound1, Type bound2) {
duke@1: return makeCompoundType(List.of(bound1, bound2));
duke@1: }
duke@1: //
duke@1:
duke@1: //
duke@1: public Type supertype(Type t) {
duke@1: return supertype.visit(t);
duke@1: }
duke@1: // where
duke@1: private UnaryVisitor supertype = new UnaryVisitor() {
duke@1:
duke@1: public Type visitType(Type t, Void ignored) {
duke@1: // A note on wildcards: there is no good way to
duke@1: // determine a supertype for a super bounded wildcard.
duke@1: return null;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Type visitClassType(ClassType t, Void ignored) {
duke@1: if (t.supertype_field == null) {
duke@1: Type supertype = ((ClassSymbol)t.tsym).getSuperclass();
duke@1: // An interface has no superclass; its supertype is Object.
duke@1: if (t.isInterface())
duke@1: supertype = ((ClassType)t.tsym.type).supertype_field;
duke@1: if (t.supertype_field == null) {
duke@1: List actuals = classBound(t).allparams();
duke@1: List formals = t.tsym.type.allparams();
mcimadamore@30: if (t.hasErasedSupertypes()) {
mcimadamore@30: t.supertype_field = erasureRecursive(supertype);
mcimadamore@30: } else if (formals.nonEmpty()) {
duke@1: t.supertype_field = subst(supertype, formals, actuals);
duke@1: }
mcimadamore@30: else {
mcimadamore@30: t.supertype_field = supertype;
mcimadamore@30: }
duke@1: }
duke@1: }
duke@1: return t.supertype_field;
duke@1: }
duke@1:
duke@1: /**
duke@1: * The supertype is always a class type. If the type
duke@1: * variable's bounds start with a class type, this is also
duke@1: * the supertype. Otherwise, the supertype is
duke@1: * java.lang.Object.
duke@1: */
duke@1: @Override
duke@1: public Type visitTypeVar(TypeVar t, Void ignored) {
duke@1: if (t.bound.tag == TYPEVAR ||
duke@1: (!t.bound.isCompound() && !t.bound.isInterface())) {
duke@1: return t.bound;
duke@1: } else {
duke@1: return supertype(t.bound);
duke@1: }
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Type visitArrayType(ArrayType t, Void ignored) {
duke@1: if (t.elemtype.isPrimitive() || isSameType(t.elemtype, syms.objectType))
duke@1: return arraySuperType();
duke@1: else
duke@1: return new ArrayType(supertype(t.elemtype), t.tsym);
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Type visitErrorType(ErrorType t, Void ignored) {
duke@1: return t;
duke@1: }
duke@1: };
duke@1: //
duke@1:
duke@1: //
duke@1: /**
duke@1: * Return the interfaces implemented by this class.
duke@1: */
duke@1: public List interfaces(Type t) {
duke@1: return interfaces.visit(t);
duke@1: }
duke@1: // where
duke@1: private UnaryVisitor> interfaces = new UnaryVisitor>() {
duke@1:
duke@1: public List visitType(Type t, Void ignored) {
duke@1: return List.nil();
duke@1: }
duke@1:
duke@1: @Override
duke@1: public List visitClassType(ClassType t, Void ignored) {
duke@1: if (t.interfaces_field == null) {
duke@1: List interfaces = ((ClassSymbol)t.tsym).getInterfaces();
duke@1: if (t.interfaces_field == null) {
duke@1: // If t.interfaces_field is null, then t must
duke@1: // be a parameterized type (not to be confused
duke@1: // with a generic type declaration).
duke@1: // Terminology:
duke@1: // Parameterized type: List
duke@1: // Generic type declaration: class List { ... }
duke@1: // So t corresponds to List and
duke@1: // t.tsym.type corresponds to List.
duke@1: // The reason t must be parameterized type is
duke@1: // that completion will happen as a side
duke@1: // effect of calling
duke@1: // ClassSymbol.getInterfaces. Since
duke@1: // t.interfaces_field is null after
duke@1: // completion, we can assume that t is not the
duke@1: // type of a class/interface declaration.
duke@1: assert t != t.tsym.type : t.toString();
duke@1: List actuals = t.allparams();
duke@1: List formals = t.tsym.type.allparams();
mcimadamore@30: if (t.hasErasedSupertypes()) {
mcimadamore@30: t.interfaces_field = erasureRecursive(interfaces);
mcimadamore@30: } else if (formals.nonEmpty()) {
duke@1: t.interfaces_field =
duke@1: upperBounds(subst(interfaces, formals, actuals));
duke@1: }
mcimadamore@30: else {
mcimadamore@30: t.interfaces_field = interfaces;
mcimadamore@30: }
duke@1: }
duke@1: }
duke@1: return t.interfaces_field;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public List visitTypeVar(TypeVar t, Void ignored) {
duke@1: if (t.bound.isCompound())
duke@1: return interfaces(t.bound);
duke@1:
duke@1: if (t.bound.isInterface())
duke@1: return List.of(t.bound);
duke@1:
duke@1: return List.nil();
duke@1: }
duke@1: };
duke@1: //
duke@1:
duke@1: //
duke@1: Map isDerivedRawCache = new HashMap();
duke@1:
duke@1: public boolean isDerivedRaw(Type t) {
duke@1: Boolean result = isDerivedRawCache.get(t);
duke@1: if (result == null) {
duke@1: result = isDerivedRawInternal(t);
duke@1: isDerivedRawCache.put(t, result);
duke@1: }
duke@1: return result;
duke@1: }
duke@1:
duke@1: public boolean isDerivedRawInternal(Type t) {
duke@1: if (t.isErroneous())
duke@1: return false;
duke@1: return
duke@1: t.isRaw() ||
duke@1: supertype(t) != null && isDerivedRaw(supertype(t)) ||
duke@1: isDerivedRaw(interfaces(t));
duke@1: }
duke@1:
duke@1: public boolean isDerivedRaw(List ts) {
duke@1: List l = ts;
duke@1: while (l.nonEmpty() && !isDerivedRaw(l.head)) l = l.tail;
duke@1: return l.nonEmpty();
duke@1: }
duke@1: //
duke@1:
duke@1: //
duke@1: /**
duke@1: * Set the bounds field of the given type variable to reflect a
duke@1: * (possibly multiple) list of bounds.
duke@1: * @param t a type variable
duke@1: * @param bounds the bounds, must be nonempty
duke@1: * @param supertype is objectType if all bounds are interfaces,
duke@1: * null otherwise.
duke@1: */
duke@1: public void setBounds(TypeVar t, List bounds, Type supertype) {
duke@1: if (bounds.tail.isEmpty())
duke@1: t.bound = bounds.head;
duke@1: else
duke@1: t.bound = makeCompoundType(bounds, supertype);
duke@1: t.rank_field = -1;
duke@1: }
duke@1:
duke@1: /**
duke@1: * Same as {@link #setBounds(Type.TypeVar,List,Type)}, except that
duke@1: * third parameter is computed directly. Note that this test
duke@1: * might cause a symbol completion. Hence, this version of
duke@1: * setBounds may not be called during a classfile read.
duke@1: */
duke@1: public void setBounds(TypeVar t, List bounds) {
duke@1: Type supertype = (bounds.head.tsym.flags() & INTERFACE) != 0 ?
duke@1: supertype(bounds.head) : null;
duke@1: setBounds(t, bounds, supertype);
duke@1: t.rank_field = -1;
duke@1: }
duke@1: //
duke@1:
duke@1: //
duke@1: /**
duke@1: * Return list of bounds of the given type variable.
duke@1: */
duke@1: public List getBounds(TypeVar t) {
duke@1: if (t.bound.isErroneous() || !t.bound.isCompound())
duke@1: return List.of(t.bound);
duke@1: else if ((erasure(t).tsym.flags() & INTERFACE) == 0)
duke@1: return interfaces(t).prepend(supertype(t));
duke@1: else
duke@1: // No superclass was given in bounds.
duke@1: // In this case, supertype is Object, erasure is first interface.
duke@1: return interfaces(t);
duke@1: }
duke@1: //
duke@1:
duke@1: //
duke@1: /**
duke@1: * If the given type is a (possibly selected) type variable,
duke@1: * return the bounding class of this type, otherwise return the
duke@1: * type itself.
duke@1: */
duke@1: public Type classBound(Type t) {
duke@1: return classBound.visit(t);
duke@1: }
duke@1: // where
duke@1: private UnaryVisitor classBound = new UnaryVisitor() {
duke@1:
duke@1: public Type visitType(Type t, Void ignored) {
duke@1: return t;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Type visitClassType(ClassType t, Void ignored) {
duke@1: Type outer1 = classBound(t.getEnclosingType());
duke@1: if (outer1 != t.getEnclosingType())
duke@1: return new ClassType(outer1, t.getTypeArguments(), t.tsym);
duke@1: else
duke@1: return t;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Type visitTypeVar(TypeVar t, Void ignored) {
duke@1: return classBound(supertype(t));
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Type visitErrorType(ErrorType t, Void ignored) {
duke@1: return t;
duke@1: }
duke@1: };
duke@1: //
duke@1:
duke@1: //
duke@1: /**
duke@1: * Returns true iff the first signature is a sub
duke@1: * signature of the other. This is not an equivalence
duke@1: * relation.
duke@1: *
duke@1: * @see "The Java Language Specification, Third Ed. (8.4.2)."
duke@1: * @see #overrideEquivalent(Type t, Type s)
duke@1: * @param t first signature (possibly raw).
duke@1: * @param s second signature (could be subjected to erasure).
duke@1: * @return true if t is a sub signature of s.
duke@1: */
duke@1: public boolean isSubSignature(Type t, Type s) {
duke@1: return hasSameArgs(t, s) || hasSameArgs(t, erasure(s));
duke@1: }
duke@1:
duke@1: /**
duke@1: * Returns true iff these signatures are related by override
duke@1: * equivalence. This is the natural extension of
duke@1: * isSubSignature to an equivalence relation.
duke@1: *
duke@1: * @see "The Java Language Specification, Third Ed. (8.4.2)."
duke@1: * @see #isSubSignature(Type t, Type s)
duke@1: * @param t a signature (possible raw, could be subjected to
duke@1: * erasure).
duke@1: * @param s a signature (possible raw, could be subjected to
duke@1: * erasure).
duke@1: * @return true if either argument is a sub signature of the other.
duke@1: */
duke@1: public boolean overrideEquivalent(Type t, Type s) {
duke@1: return hasSameArgs(t, s) ||
duke@1: hasSameArgs(t, erasure(s)) || hasSameArgs(erasure(t), s);
duke@1: }
duke@1:
mcimadamore@341: private WeakHashMap>> implCache_check =
mcimadamore@341: new WeakHashMap>>();
mcimadamore@341:
mcimadamore@341: private WeakHashMap>> implCache_nocheck =
mcimadamore@341: new WeakHashMap>>();
mcimadamore@341:
mcimadamore@341: public MethodSymbol implementation(MethodSymbol ms, TypeSymbol origin, Types types, boolean checkResult) {
mcimadamore@341: Map>> implCache = checkResult ?
mcimadamore@341: implCache_check : implCache_nocheck;
mcimadamore@341: SoftReference
duke@1:
duke@1: //
duke@1: public List subst(List ts,
duke@1: List from,
duke@1: List to) {
duke@1: return new Subst(from, to).subst(ts);
duke@1: }
duke@1:
duke@1: /**
duke@1: * Substitute all occurrences of a type in `from' with the
duke@1: * corresponding type in `to' in 't'. Match lists `from' and `to'
duke@1: * from the right: If lists have different length, discard leading
duke@1: * elements of the longer list.
duke@1: */
duke@1: public Type subst(Type t, List from, List to) {
duke@1: return new Subst(from, to).subst(t);
duke@1: }
duke@1:
duke@1: private class Subst extends UnaryVisitor {
duke@1: List from;
duke@1: List to;
duke@1:
duke@1: public Subst(List from, List to) {
duke@1: int fromLength = from.length();
duke@1: int toLength = to.length();
duke@1: while (fromLength > toLength) {
duke@1: fromLength--;
duke@1: from = from.tail;
duke@1: }
duke@1: while (fromLength < toLength) {
duke@1: toLength--;
duke@1: to = to.tail;
duke@1: }
duke@1: this.from = from;
duke@1: this.to = to;
duke@1: }
duke@1:
duke@1: Type subst(Type t) {
duke@1: if (from.tail == null)
duke@1: return t;
duke@1: else
duke@1: return visit(t);
mcimadamore@238: }
duke@1:
duke@1: List subst(List ts) {
duke@1: if (from.tail == null)
duke@1: return ts;
duke@1: boolean wild = false;
duke@1: if (ts.nonEmpty() && from.nonEmpty()) {
duke@1: Type head1 = subst(ts.head);
duke@1: List tail1 = subst(ts.tail);
duke@1: if (head1 != ts.head || tail1 != ts.tail)
duke@1: return tail1.prepend(head1);
duke@1: }
duke@1: return ts;
duke@1: }
duke@1:
duke@1: public Type visitType(Type t, Void ignored) {
duke@1: return t;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Type visitMethodType(MethodType t, Void ignored) {
duke@1: List argtypes = subst(t.argtypes);
duke@1: Type restype = subst(t.restype);
duke@1: List thrown = subst(t.thrown);
duke@1: if (argtypes == t.argtypes &&
duke@1: restype == t.restype &&
duke@1: thrown == t.thrown)
duke@1: return t;
duke@1: else
duke@1: return new MethodType(argtypes, restype, thrown, t.tsym);
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Type visitTypeVar(TypeVar t, Void ignored) {
duke@1: for (List from = this.from, to = this.to;
duke@1: from.nonEmpty();
duke@1: from = from.tail, to = to.tail) {
duke@1: if (t == from.head) {
duke@1: return to.head.withTypeVar(t);
duke@1: }
duke@1: }
duke@1: return t;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Type visitClassType(ClassType t, Void ignored) {
duke@1: if (!t.isCompound()) {
duke@1: List typarams = t.getTypeArguments();
duke@1: List typarams1 = subst(typarams);
duke@1: Type outer = t.getEnclosingType();
duke@1: Type outer1 = subst(outer);
duke@1: if (typarams1 == typarams && outer1 == outer)
duke@1: return t;
duke@1: else
duke@1: return new ClassType(outer1, typarams1, t.tsym);
duke@1: } else {
duke@1: Type st = subst(supertype(t));
duke@1: List is = upperBounds(subst(interfaces(t)));
duke@1: if (st == supertype(t) && is == interfaces(t))
duke@1: return t;
duke@1: else
duke@1: return makeCompoundType(is.prepend(st));
duke@1: }
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Type visitWildcardType(WildcardType t, Void ignored) {
duke@1: Type bound = t.type;
duke@1: if (t.kind != BoundKind.UNBOUND)
duke@1: bound = subst(bound);
duke@1: if (bound == t.type) {
duke@1: return t;
duke@1: } else {
duke@1: if (t.isExtendsBound() && bound.isExtendsBound())
duke@1: bound = upperBound(bound);
duke@1: return new WildcardType(bound, t.kind, syms.boundClass, t.bound);
duke@1: }
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Type visitArrayType(ArrayType t, Void ignored) {
duke@1: Type elemtype = subst(t.elemtype);
duke@1: if (elemtype == t.elemtype)
duke@1: return t;
duke@1: else
duke@1: return new ArrayType(upperBound(elemtype), t.tsym);
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Type visitForAll(ForAll t, Void ignored) {
duke@1: List tvars1 = substBounds(t.tvars, from, to);
duke@1: Type qtype1 = subst(t.qtype);
duke@1: if (tvars1 == t.tvars && qtype1 == t.qtype) {
duke@1: return t;
duke@1: } else if (tvars1 == t.tvars) {
duke@1: return new ForAll(tvars1, qtype1);
duke@1: } else {
duke@1: return new ForAll(tvars1, Types.this.subst(qtype1, t.tvars, tvars1));
duke@1: }
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Type visitErrorType(ErrorType t, Void ignored) {
duke@1: return t;
duke@1: }
duke@1: }
duke@1:
duke@1: public List substBounds(List tvars,
duke@1: List from,
duke@1: List to) {
duke@1: if (tvars.isEmpty())
duke@1: return tvars;
duke@1: ListBuffer newBoundsBuf = lb();
duke@1: boolean changed = false;
duke@1: // calculate new bounds
duke@1: for (Type t : tvars) {
duke@1: TypeVar tv = (TypeVar) t;
duke@1: Type bound = subst(tv.bound, from, to);
duke@1: if (bound != tv.bound)
duke@1: changed = true;
duke@1: newBoundsBuf.append(bound);
duke@1: }
duke@1: if (!changed)
duke@1: return tvars;
duke@1: ListBuffer newTvars = lb();
duke@1: // create new type variables without bounds
duke@1: for (Type t : tvars) {
duke@1: newTvars.append(new TypeVar(t.tsym, null, syms.botType));
duke@1: }
duke@1: // the new bounds should use the new type variables in place
duke@1: // of the old
duke@1: List newBounds = newBoundsBuf.toList();
duke@1: from = tvars;
duke@1: to = newTvars.toList();
duke@1: for (; !newBounds.isEmpty(); newBounds = newBounds.tail) {
duke@1: newBounds.head = subst(newBounds.head, from, to);
duke@1: }
duke@1: newBounds = newBoundsBuf.toList();
duke@1: // set the bounds of new type variables to the new bounds
duke@1: for (Type t : newTvars.toList()) {
duke@1: TypeVar tv = (TypeVar) t;
duke@1: tv.bound = newBounds.head;
duke@1: newBounds = newBounds.tail;
duke@1: }
duke@1: return newTvars.toList();
duke@1: }
duke@1:
duke@1: public TypeVar substBound(TypeVar t, List from, List to) {
duke@1: Type bound1 = subst(t.bound, from, to);
duke@1: if (bound1 == t.bound)
duke@1: return t;
mcimadamore@212: else {
mcimadamore@212: // create new type variable without bounds
mcimadamore@212: TypeVar tv = new TypeVar(t.tsym, null, syms.botType);
mcimadamore@212: // the new bound should use the new type variable in place
mcimadamore@212: // of the old
mcimadamore@212: tv.bound = subst(bound1, List.of(t), List.of(tv));
mcimadamore@212: return tv;
mcimadamore@212: }
duke@1: }
duke@1: //
duke@1:
duke@1: //
duke@1: /**
duke@1: * Does t have the same bounds for quantified variables as s?
duke@1: */
duke@1: boolean hasSameBounds(ForAll t, ForAll s) {
duke@1: List l1 = t.tvars;
duke@1: List l2 = s.tvars;
duke@1: while (l1.nonEmpty() && l2.nonEmpty() &&
duke@1: isSameType(l1.head.getUpperBound(),
duke@1: subst(l2.head.getUpperBound(),
duke@1: s.tvars,
duke@1: t.tvars))) {
duke@1: l1 = l1.tail;
duke@1: l2 = l2.tail;
duke@1: }
duke@1: return l1.isEmpty() && l2.isEmpty();
duke@1: }
duke@1: //
duke@1:
duke@1: //
duke@1: /** Create new vector of type variables from list of variables
duke@1: * changing all recursive bounds from old to new list.
duke@1: */
duke@1: public List newInstances(List tvars) {
duke@1: List tvars1 = Type.map(tvars, newInstanceFun);
duke@1: for (List l = tvars1; l.nonEmpty(); l = l.tail) {
duke@1: TypeVar tv = (TypeVar) l.head;
duke@1: tv.bound = subst(tv.bound, tvars, tvars1);
duke@1: }
duke@1: return tvars1;
duke@1: }
duke@1: static private Mapping newInstanceFun = new Mapping("newInstanceFun") {
duke@1: public Type apply(Type t) { return new TypeVar(t.tsym, t.getUpperBound(), t.getLowerBound()); }
duke@1: };
duke@1: //
duke@1:
jjg@110: //
jjg@110: public Type createErrorType(Type originalType) {
jjg@110: return new ErrorType(originalType, syms.errSymbol);
jjg@110: }
jjg@110:
jjg@110: public Type createErrorType(ClassSymbol c, Type originalType) {
jjg@110: return new ErrorType(c, originalType);
jjg@110: }
jjg@110:
jjg@110: public Type createErrorType(Name name, TypeSymbol container, Type originalType) {
jjg@110: return new ErrorType(name, container, originalType);
jjg@110: }
jjg@110: //
jjg@110:
duke@1: //
duke@1: /**
duke@1: * The rank of a class is the length of the longest path between
duke@1: * the class and java.lang.Object in the class inheritance
duke@1: * graph. Undefined for all but reference types.
duke@1: */
duke@1: public int rank(Type t) {
duke@1: switch(t.tag) {
duke@1: case CLASS: {
duke@1: ClassType cls = (ClassType)t;
duke@1: if (cls.rank_field < 0) {
duke@1: Name fullname = cls.tsym.getQualifiedName();
jjg@113: if (fullname == names.java_lang_Object)
duke@1: cls.rank_field = 0;
duke@1: else {
duke@1: int r = rank(supertype(cls));
duke@1: for (List l = interfaces(cls);
duke@1: l.nonEmpty();
duke@1: l = l.tail) {
duke@1: if (rank(l.head) > r)
duke@1: r = rank(l.head);
duke@1: }
duke@1: cls.rank_field = r + 1;
duke@1: }
duke@1: }
duke@1: return cls.rank_field;
duke@1: }
duke@1: case TYPEVAR: {
duke@1: TypeVar tvar = (TypeVar)t;
duke@1: if (tvar.rank_field < 0) {
duke@1: int r = rank(supertype(tvar));
duke@1: for (List l = interfaces(tvar);
duke@1: l.nonEmpty();
duke@1: l = l.tail) {
duke@1: if (rank(l.head) > r) r = rank(l.head);
duke@1: }
duke@1: tvar.rank_field = r + 1;
duke@1: }
duke@1: return tvar.rank_field;
duke@1: }
duke@1: case ERROR:
duke@1: return 0;
duke@1: default:
duke@1: throw new AssertionError();
duke@1: }
duke@1: }
duke@1: //
duke@1:
mcimadamore@121: /**
mcimadamore@238: * Helper method for generating a string representation of a given type
mcimadamore@121: * accordingly to a given locale
mcimadamore@121: */
mcimadamore@121: public String toString(Type t, Locale locale) {
mcimadamore@238: return Printer.createStandardPrinter(messages).visit(t, locale);
mcimadamore@121: }
mcimadamore@121:
mcimadamore@121: /**
mcimadamore@238: * Helper method for generating a string representation of a given type
mcimadamore@121: * accordingly to a given locale
mcimadamore@121: */
mcimadamore@121: public String toString(Symbol t, Locale locale) {
mcimadamore@238: return Printer.createStandardPrinter(messages).visit(t, locale);
mcimadamore@121: }
mcimadamore@121:
duke@1: //
duke@1: /**
duke@1: * This toString is slightly more descriptive than the one on Type.
mcimadamore@121: *
mcimadamore@121: * @deprecated Types.toString(Type t, Locale l) provides better support
mcimadamore@121: * for localization
duke@1: */
mcimadamore@121: @Deprecated
duke@1: public String toString(Type t) {
duke@1: if (t.tag == FORALL) {
duke@1: ForAll forAll = (ForAll)t;
duke@1: return typaramsString(forAll.tvars) + forAll.qtype;
duke@1: }
duke@1: return "" + t;
duke@1: }
duke@1: // where
duke@1: private String typaramsString(List tvars) {
duke@1: StringBuffer s = new StringBuffer();
duke@1: s.append('<');
duke@1: boolean first = true;
duke@1: for (Type t : tvars) {
duke@1: if (!first) s.append(", ");
duke@1: first = false;
duke@1: appendTyparamString(((TypeVar)t), s);
duke@1: }
duke@1: s.append('>');
duke@1: return s.toString();
duke@1: }
duke@1: private void appendTyparamString(TypeVar t, StringBuffer buf) {
duke@1: buf.append(t);
duke@1: if (t.bound == null ||
duke@1: t.bound.tsym.getQualifiedName() == names.java_lang_Object)
duke@1: return;
duke@1: buf.append(" extends "); // Java syntax; no need for i18n
duke@1: Type bound = t.bound;
duke@1: if (!bound.isCompound()) {
duke@1: buf.append(bound);
duke@1: } else if ((erasure(t).tsym.flags() & INTERFACE) == 0) {
duke@1: buf.append(supertype(t));
duke@1: for (Type intf : interfaces(t)) {
duke@1: buf.append('&');
duke@1: buf.append(intf);
duke@1: }
duke@1: } else {
duke@1: // No superclass was given in bounds.
duke@1: // In this case, supertype is Object, erasure is first interface.
duke@1: boolean first = true;
duke@1: for (Type intf : interfaces(t)) {
duke@1: if (!first) buf.append('&');
duke@1: first = false;
duke@1: buf.append(intf);
duke@1: }
duke@1: }
duke@1: }
duke@1: //
duke@1:
duke@1: //
duke@1: /**
duke@1: * A cache for closures.
duke@1: *
duke@1: *
A closure is a list of all the supertypes and interfaces of
duke@1: * a class or interface type, ordered by ClassSymbol.precedes
duke@1: * (that is, subclasses come first, arbitrary but fixed
duke@1: * otherwise).
duke@1: */
duke@1: private Map> closureCache = new HashMap>();
duke@1:
duke@1: /**
duke@1: * Returns the closure of a class or interface type.
duke@1: */
duke@1: public List closure(Type t) {
duke@1: List cl = closureCache.get(t);
duke@1: if (cl == null) {
duke@1: Type st = supertype(t);
duke@1: if (!t.isCompound()) {
duke@1: if (st.tag == CLASS) {
duke@1: cl = insert(closure(st), t);
duke@1: } else if (st.tag == TYPEVAR) {
duke@1: cl = closure(st).prepend(t);
duke@1: } else {
duke@1: cl = List.of(t);
duke@1: }
duke@1: } else {
duke@1: cl = closure(supertype(t));
duke@1: }
duke@1: for (List l = interfaces(t); l.nonEmpty(); l = l.tail)
duke@1: cl = union(cl, closure(l.head));
duke@1: closureCache.put(t, cl);
duke@1: }
duke@1: return cl;
duke@1: }
duke@1:
duke@1: /**
duke@1: * Insert a type in a closure
duke@1: */
duke@1: public List insert(List cl, Type t) {
duke@1: if (cl.isEmpty() || t.tsym.precedes(cl.head.tsym, this)) {
duke@1: return cl.prepend(t);
duke@1: } else if (cl.head.tsym.precedes(t.tsym, this)) {
duke@1: return insert(cl.tail, t).prepend(cl.head);
duke@1: } else {
duke@1: return cl;
duke@1: }
duke@1: }
duke@1:
duke@1: /**
duke@1: * Form the union of two closures
duke@1: */
duke@1: public List union(List cl1, List cl2) {
duke@1: if (cl1.isEmpty()) {
duke@1: return cl2;
duke@1: } else if (cl2.isEmpty()) {
duke@1: return cl1;
duke@1: } else if (cl1.head.tsym.precedes(cl2.head.tsym, this)) {
duke@1: return union(cl1.tail, cl2).prepend(cl1.head);
duke@1: } else if (cl2.head.tsym.precedes(cl1.head.tsym, this)) {
duke@1: return union(cl1, cl2.tail).prepend(cl2.head);
duke@1: } else {
duke@1: return union(cl1.tail, cl2.tail).prepend(cl1.head);
duke@1: }
duke@1: }
duke@1:
duke@1: /**
duke@1: * Intersect two closures
duke@1: */
duke@1: public List intersect(List cl1, List cl2) {
duke@1: if (cl1 == cl2)
duke@1: return cl1;
duke@1: if (cl1.isEmpty() || cl2.isEmpty())
duke@1: return List.nil();
duke@1: if (cl1.head.tsym.precedes(cl2.head.tsym, this))
duke@1: return intersect(cl1.tail, cl2);
duke@1: if (cl2.head.tsym.precedes(cl1.head.tsym, this))
duke@1: return intersect(cl1, cl2.tail);
duke@1: if (isSameType(cl1.head, cl2.head))
duke@1: return intersect(cl1.tail, cl2.tail).prepend(cl1.head);
duke@1: if (cl1.head.tsym == cl2.head.tsym &&
duke@1: cl1.head.tag == CLASS && cl2.head.tag == CLASS) {
duke@1: if (cl1.head.isParameterized() && cl2.head.isParameterized()) {
duke@1: Type merge = merge(cl1.head,cl2.head);
duke@1: return intersect(cl1.tail, cl2.tail).prepend(merge);
duke@1: }
duke@1: if (cl1.head.isRaw() || cl2.head.isRaw())
duke@1: return intersect(cl1.tail, cl2.tail).prepend(erasure(cl1.head));
duke@1: }
duke@1: return intersect(cl1.tail, cl2.tail);
duke@1: }
duke@1: // where
duke@1: class TypePair {
duke@1: final Type t1;
duke@1: final Type t2;
duke@1: TypePair(Type t1, Type t2) {
duke@1: this.t1 = t1;
duke@1: this.t2 = t2;
duke@1: }
duke@1: @Override
duke@1: public int hashCode() {
duke@1: return 127 * Types.this.hashCode(t1) + Types.this.hashCode(t2);
duke@1: }
duke@1: @Override
duke@1: public boolean equals(Object obj) {
duke@1: if (!(obj instanceof TypePair))
duke@1: return false;
duke@1: TypePair typePair = (TypePair)obj;
duke@1: return isSameType(t1, typePair.t1)
duke@1: && isSameType(t2, typePair.t2);
duke@1: }
duke@1: }
duke@1: Set mergeCache = new HashSet();
duke@1: private Type merge(Type c1, Type c2) {
duke@1: ClassType class1 = (ClassType) c1;
duke@1: List act1 = class1.getTypeArguments();
duke@1: ClassType class2 = (ClassType) c2;
duke@1: List act2 = class2.getTypeArguments();
duke@1: ListBuffer merged = new ListBuffer();
duke@1: List typarams = class1.tsym.type.getTypeArguments();
duke@1:
duke@1: while (act1.nonEmpty() && act2.nonEmpty() && typarams.nonEmpty()) {
duke@1: if (containsType(act1.head, act2.head)) {
duke@1: merged.append(act1.head);
duke@1: } else if (containsType(act2.head, act1.head)) {
duke@1: merged.append(act2.head);
duke@1: } else {
duke@1: TypePair pair = new TypePair(c1, c2);
duke@1: Type m;
duke@1: if (mergeCache.add(pair)) {
duke@1: m = new WildcardType(lub(upperBound(act1.head),
duke@1: upperBound(act2.head)),
duke@1: BoundKind.EXTENDS,
duke@1: syms.boundClass);
duke@1: mergeCache.remove(pair);
duke@1: } else {
duke@1: m = new WildcardType(syms.objectType,
duke@1: BoundKind.UNBOUND,
duke@1: syms.boundClass);
duke@1: }
duke@1: merged.append(m.withTypeVar(typarams.head));
duke@1: }
duke@1: act1 = act1.tail;
duke@1: act2 = act2.tail;
duke@1: typarams = typarams.tail;
duke@1: }
duke@1: assert(act1.isEmpty() && act2.isEmpty() && typarams.isEmpty());
duke@1: return new ClassType(class1.getEnclosingType(), merged.toList(), class1.tsym);
duke@1: }
duke@1:
duke@1: /**
duke@1: * Return the minimum type of a closure, a compound type if no
duke@1: * unique minimum exists.
duke@1: */
duke@1: private Type compoundMin(List cl) {
duke@1: if (cl.isEmpty()) return syms.objectType;
duke@1: List compound = closureMin(cl);
duke@1: if (compound.isEmpty())
duke@1: return null;
duke@1: else if (compound.tail.isEmpty())
duke@1: return compound.head;
duke@1: else
duke@1: return makeCompoundType(compound);
duke@1: }
duke@1:
duke@1: /**
duke@1: * Return the minimum types of a closure, suitable for computing
duke@1: * compoundMin or glb.
duke@1: */
duke@1: private List closureMin(List cl) {
duke@1: ListBuffer classes = lb();
duke@1: ListBuffer interfaces = lb();
duke@1: while (!cl.isEmpty()) {
duke@1: Type current = cl.head;
duke@1: if (current.isInterface())
duke@1: interfaces.append(current);
duke@1: else
duke@1: classes.append(current);
duke@1: ListBuffer candidates = lb();
duke@1: for (Type t : cl.tail) {
duke@1: if (!isSubtypeNoCapture(current, t))
duke@1: candidates.append(t);
duke@1: }
duke@1: cl = candidates.toList();
duke@1: }
duke@1: return classes.appendList(interfaces).toList();
duke@1: }
duke@1:
duke@1: /**
duke@1: * Return the least upper bound of pair of types. if the lub does
duke@1: * not exist return null.
duke@1: */
duke@1: public Type lub(Type t1, Type t2) {
duke@1: return lub(List.of(t1, t2));
duke@1: }
duke@1:
duke@1: /**
duke@1: * Return the least upper bound (lub) of set of types. If the lub
duke@1: * does not exist return the type of null (bottom).
duke@1: */
duke@1: public Type lub(List ts) {
duke@1: final int ARRAY_BOUND = 1;
duke@1: final int CLASS_BOUND = 2;
duke@1: int boundkind = 0;
duke@1: for (Type t : ts) {
duke@1: switch (t.tag) {
duke@1: case CLASS:
duke@1: boundkind |= CLASS_BOUND;
duke@1: break;
duke@1: case ARRAY:
duke@1: boundkind |= ARRAY_BOUND;
duke@1: break;
duke@1: case TYPEVAR:
duke@1: do {
duke@1: t = t.getUpperBound();
duke@1: } while (t.tag == TYPEVAR);
duke@1: if (t.tag == ARRAY) {
duke@1: boundkind |= ARRAY_BOUND;
duke@1: } else {
duke@1: boundkind |= CLASS_BOUND;
duke@1: }
duke@1: break;
duke@1: default:
duke@1: if (t.isPrimitive())
mcimadamore@5: return syms.errType;
duke@1: }
duke@1: }
duke@1: switch (boundkind) {
duke@1: case 0:
duke@1: return syms.botType;
duke@1:
duke@1: case ARRAY_BOUND:
duke@1: // calculate lub(A[], B[])
duke@1: List elements = Type.map(ts, elemTypeFun);
duke@1: for (Type t : elements) {
duke@1: if (t.isPrimitive()) {
duke@1: // if a primitive type is found, then return
duke@1: // arraySuperType unless all the types are the
duke@1: // same
duke@1: Type first = ts.head;
duke@1: for (Type s : ts.tail) {
duke@1: if (!isSameType(first, s)) {
duke@1: // lub(int[], B[]) is Cloneable & Serializable
duke@1: return arraySuperType();
duke@1: }
duke@1: }
duke@1: // all the array types are the same, return one
duke@1: // lub(int[], int[]) is int[]
duke@1: return first;
duke@1: }
duke@1: }
duke@1: // lub(A[], B[]) is lub(A, B)[]
duke@1: return new ArrayType(lub(elements), syms.arrayClass);
duke@1:
duke@1: case CLASS_BOUND:
duke@1: // calculate lub(A, B)
duke@1: while (ts.head.tag != CLASS && ts.head.tag != TYPEVAR)
duke@1: ts = ts.tail;
duke@1: assert !ts.isEmpty();
duke@1: List cl = closure(ts.head);
duke@1: for (Type t : ts.tail) {
duke@1: if (t.tag == CLASS || t.tag == TYPEVAR)
duke@1: cl = intersect(cl, closure(t));
duke@1: }
duke@1: return compoundMin(cl);
duke@1:
duke@1: default:
duke@1: // calculate lub(A, B[])
duke@1: List classes = List.of(arraySuperType());
duke@1: for (Type t : ts) {
duke@1: if (t.tag != ARRAY) // Filter out any arrays
duke@1: classes = classes.prepend(t);
duke@1: }
duke@1: // lub(A, B[]) is lub(A, arraySuperType)
duke@1: return lub(classes);
duke@1: }
duke@1: }
duke@1: // where
duke@1: private Type arraySuperType = null;
duke@1: private Type arraySuperType() {
duke@1: // initialized lazily to avoid problems during compiler startup
duke@1: if (arraySuperType == null) {
duke@1: synchronized (this) {
duke@1: if (arraySuperType == null) {
duke@1: // JLS 10.8: all arrays implement Cloneable and Serializable.
duke@1: arraySuperType = makeCompoundType(List.of(syms.serializableType,
duke@1: syms.cloneableType),
duke@1: syms.objectType);
duke@1: }
duke@1: }
duke@1: }
duke@1: return arraySuperType;
duke@1: }
duke@1: //
duke@1:
duke@1: //
mcimadamore@210: public Type glb(List ts) {
mcimadamore@210: Type t1 = ts.head;
mcimadamore@210: for (Type t2 : ts.tail) {
mcimadamore@210: if (t1.isErroneous())
mcimadamore@210: return t1;
mcimadamore@210: t1 = glb(t1, t2);
mcimadamore@210: }
mcimadamore@210: return t1;
mcimadamore@210: }
mcimadamore@210: //where
duke@1: public Type glb(Type t, Type s) {
duke@1: if (s == null)
duke@1: return t;
duke@1: else if (isSubtypeNoCapture(t, s))
duke@1: return t;
duke@1: else if (isSubtypeNoCapture(s, t))
duke@1: return s;
duke@1:
duke@1: List closure = union(closure(t), closure(s));
duke@1: List bounds = closureMin(closure);
duke@1:
duke@1: if (bounds.isEmpty()) { // length == 0
duke@1: return syms.objectType;
duke@1: } else if (bounds.tail.isEmpty()) { // length == 1
duke@1: return bounds.head;
duke@1: } else { // length > 1
duke@1: int classCount = 0;
duke@1: for (Type bound : bounds)
duke@1: if (!bound.isInterface())
duke@1: classCount++;
duke@1: if (classCount > 1)
jjg@110: return createErrorType(t);
duke@1: }
duke@1: return makeCompoundType(bounds);
duke@1: }
duke@1: //
duke@1:
duke@1: //
duke@1: /**
duke@1: * Compute a hash code on a type.
duke@1: */
duke@1: public static int hashCode(Type t) {
duke@1: return hashCode.visit(t);
duke@1: }
duke@1: // where
duke@1: private static final UnaryVisitor hashCode = new UnaryVisitor() {
duke@1:
duke@1: public Integer visitType(Type t, Void ignored) {
duke@1: return t.tag;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Integer visitClassType(ClassType t, Void ignored) {
duke@1: int result = visit(t.getEnclosingType());
duke@1: result *= 127;
duke@1: result += t.tsym.flatName().hashCode();
duke@1: for (Type s : t.getTypeArguments()) {
duke@1: result *= 127;
duke@1: result += visit(s);
duke@1: }
duke@1: return result;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Integer visitWildcardType(WildcardType t, Void ignored) {
duke@1: int result = t.kind.hashCode();
duke@1: if (t.type != null) {
duke@1: result *= 127;
duke@1: result += visit(t.type);
duke@1: }
duke@1: return result;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Integer visitArrayType(ArrayType t, Void ignored) {
duke@1: return visit(t.elemtype) + 12;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Integer visitTypeVar(TypeVar t, Void ignored) {
duke@1: return System.identityHashCode(t.tsym);
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Integer visitUndetVar(UndetVar t, Void ignored) {
duke@1: return System.identityHashCode(t);
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Integer visitErrorType(ErrorType t, Void ignored) {
duke@1: return 0;
duke@1: }
duke@1: };
duke@1: //
duke@1:
duke@1: //
duke@1: /**
duke@1: * Does t have a result that is a subtype of the result type of s,
duke@1: * suitable for covariant returns? It is assumed that both types
duke@1: * are (possibly polymorphic) method types. Monomorphic method
duke@1: * types are handled in the obvious way. Polymorphic method types
duke@1: * require renaming all type variables of one to corresponding
duke@1: * type variables in the other, where correspondence is by
duke@1: * position in the type parameter list. */
duke@1: public boolean resultSubtype(Type t, Type s, Warner warner) {
duke@1: List tvars = t.getTypeArguments();
duke@1: List svars = s.getTypeArguments();
duke@1: Type tres = t.getReturnType();
duke@1: Type sres = subst(s.getReturnType(), svars, tvars);
duke@1: return covariantReturnType(tres, sres, warner);
duke@1: }
duke@1:
duke@1: /**
duke@1: * Return-Type-Substitutable.
duke@1: * @see The Java
duke@1: * Language Specification, Third Ed. (8.4.5)
duke@1: */
duke@1: public boolean returnTypeSubstitutable(Type r1, Type r2) {
duke@1: if (hasSameArgs(r1, r2))
tbell@202: return resultSubtype(r1, r2, Warner.noWarnings);
duke@1: else
duke@1: return covariantReturnType(r1.getReturnType(),
tbell@202: erasure(r2.getReturnType()),
tbell@202: Warner.noWarnings);
tbell@202: }
tbell@202:
tbell@202: public boolean returnTypeSubstitutable(Type r1,
tbell@202: Type r2, Type r2res,
tbell@202: Warner warner) {
tbell@202: if (isSameType(r1.getReturnType(), r2res))
tbell@202: return true;
tbell@202: if (r1.getReturnType().isPrimitive() || r2res.isPrimitive())
tbell@202: return false;
tbell@202:
tbell@202: if (hasSameArgs(r1, r2))
tbell@202: return covariantReturnType(r1.getReturnType(), r2res, warner);
tbell@202: if (!source.allowCovariantReturns())
tbell@202: return false;
tbell@202: if (isSubtypeUnchecked(r1.getReturnType(), r2res, warner))
tbell@202: return true;
tbell@202: if (!isSubtype(r1.getReturnType(), erasure(r2res)))
tbell@202: return false;
tbell@202: warner.warnUnchecked();
tbell@202: return true;
duke@1: }
duke@1:
duke@1: /**
duke@1: * Is t an appropriate return type in an overrider for a
duke@1: * method that returns s?
duke@1: */
duke@1: public boolean covariantReturnType(Type t, Type s, Warner warner) {
tbell@202: return
tbell@202: isSameType(t, s) ||
tbell@202: source.allowCovariantReturns() &&
duke@1: !t.isPrimitive() &&
tbell@202: !s.isPrimitive() &&
tbell@202: isAssignable(t, s, warner);
duke@1: }
duke@1: //
duke@1:
duke@1: //
duke@1: /**
duke@1: * Return the class that boxes the given primitive.
duke@1: */
duke@1: public ClassSymbol boxedClass(Type t) {
duke@1: return reader.enterClass(syms.boxedName[t.tag]);
duke@1: }
duke@1:
duke@1: /**
duke@1: * Return the primitive type corresponding to a boxed type.
duke@1: */
duke@1: public Type unboxedType(Type t) {
duke@1: if (allowBoxing) {
duke@1: for (int i=0; i
duke@1:
duke@1: //
duke@1: /*
duke@1: * JLS 3rd Ed. 5.1.10 Capture Conversion:
duke@1: *
duke@1: * Let G name a generic type declaration with n formal type
duke@1: * parameters A1 ... An with corresponding bounds U1 ... Un. There
duke@1: * exists a capture conversion from G to G,
duke@1: * where, for 1 <= i <= n:
duke@1: *
duke@1: * + If Ti is a wildcard type argument (4.5.1) of the form ? then
duke@1: * Si is a fresh type variable whose upper bound is
duke@1: * Ui[A1 := S1, ..., An := Sn] and whose lower bound is the null
duke@1: * type.
duke@1: *
duke@1: * + If Ti is a wildcard type argument of the form ? extends Bi,
duke@1: * then Si is a fresh type variable whose upper bound is
duke@1: * glb(Bi, Ui[A1 := S1, ..., An := Sn]) and whose lower bound is
duke@1: * the null type, where glb(V1,... ,Vm) is V1 & ... & Vm. It is
duke@1: * a compile-time error if for any two classes (not interfaces)
duke@1: * Vi and Vj,Vi is not a subclass of Vj or vice versa.
duke@1: *
duke@1: * + If Ti is a wildcard type argument of the form ? super Bi,
duke@1: * then Si is a fresh type variable whose upper bound is
duke@1: * Ui[A1 := S1, ..., An := Sn] and whose lower bound is Bi.
duke@1: *
duke@1: * + Otherwise, Si = Ti.
duke@1: *
duke@1: * Capture conversion on any type other than a parameterized type
duke@1: * (4.5) acts as an identity conversion (5.1.1). Capture
duke@1: * conversions never require a special action at run time and
duke@1: * therefore never throw an exception at run time.
duke@1: *
duke@1: * Capture conversion is not applied recursively.
duke@1: */
duke@1: /**
duke@1: * Capture conversion as specified by JLS 3rd Ed.
duke@1: */
mcimadamore@299:
mcimadamore@299: public List capture(List ts) {
mcimadamore@299: List buf = List.nil();
mcimadamore@299: for (Type t : ts) {
mcimadamore@299: buf = buf.prepend(capture(t));
mcimadamore@299: }
mcimadamore@299: return buf.reverse();
mcimadamore@299: }
duke@1: public Type capture(Type t) {
duke@1: if (t.tag != CLASS)
duke@1: return t;
duke@1: ClassType cls = (ClassType)t;
duke@1: if (cls.isRaw() || !cls.isParameterized())
duke@1: return cls;
duke@1:
duke@1: ClassType G = (ClassType)cls.asElement().asType();
duke@1: List A = G.getTypeArguments();
duke@1: List T = cls.getTypeArguments();
duke@1: List S = freshTypeVariables(T);
duke@1:
duke@1: List currentA = A;
duke@1: List currentT = T;
duke@1: List currentS = S;
duke@1: boolean captured = false;
duke@1: while (!currentA.isEmpty() &&
duke@1: !currentT.isEmpty() &&
duke@1: !currentS.isEmpty()) {
duke@1: if (currentS.head != currentT.head) {
duke@1: captured = true;
duke@1: WildcardType Ti = (WildcardType)currentT.head;
duke@1: Type Ui = currentA.head.getUpperBound();
duke@1: CapturedType Si = (CapturedType)currentS.head;
duke@1: if (Ui == null)
duke@1: Ui = syms.objectType;
duke@1: switch (Ti.kind) {
duke@1: case UNBOUND:
duke@1: Si.bound = subst(Ui, A, S);
duke@1: Si.lower = syms.botType;
duke@1: break;
duke@1: case EXTENDS:
duke@1: Si.bound = glb(Ti.getExtendsBound(), subst(Ui, A, S));
duke@1: Si.lower = syms.botType;
duke@1: break;
duke@1: case SUPER:
duke@1: Si.bound = subst(Ui, A, S);
duke@1: Si.lower = Ti.getSuperBound();
duke@1: break;
duke@1: }
duke@1: if (Si.bound == Si.lower)
duke@1: currentS.head = Si.bound;
duke@1: }
duke@1: currentA = currentA.tail;
duke@1: currentT = currentT.tail;
duke@1: currentS = currentS.tail;
duke@1: }
duke@1: if (!currentA.isEmpty() || !currentT.isEmpty() || !currentS.isEmpty())
duke@1: return erasure(t); // some "rare" type involved
duke@1:
duke@1: if (captured)
duke@1: return new ClassType(cls.getEnclosingType(), S, cls.tsym);
duke@1: else
duke@1: return t;
duke@1: }
duke@1: // where
mcimadamore@238: public List freshTypeVariables(List types) {
duke@1: ListBuffer result = lb();
duke@1: for (Type t : types) {
duke@1: if (t.tag == WILDCARD) {
duke@1: Type bound = ((WildcardType)t).getExtendsBound();
duke@1: if (bound == null)
duke@1: bound = syms.objectType;
duke@1: result.append(new CapturedType(capturedName,
duke@1: syms.noSymbol,
duke@1: bound,
duke@1: syms.botType,
duke@1: (WildcardType)t));
duke@1: } else {
duke@1: result.append(t);
duke@1: }
duke@1: }
duke@1: return result.toList();
duke@1: }
duke@1: //
duke@1:
duke@1: //
duke@1: private List upperBounds(List ss) {
duke@1: if (ss.isEmpty()) return ss;
duke@1: Type head = upperBound(ss.head);
duke@1: List tail = upperBounds(ss.tail);
duke@1: if (head != ss.head || tail != ss.tail)
duke@1: return tail.prepend(head);
duke@1: else
duke@1: return ss;
duke@1: }
duke@1:
duke@1: private boolean sideCast(Type from, Type to, Warner warn) {
duke@1: // We are casting from type $from$ to type $to$, which are
duke@1: // non-final unrelated types. This method
duke@1: // tries to reject a cast by transferring type parameters
duke@1: // from $to$ to $from$ by common superinterfaces.
duke@1: boolean reverse = false;
duke@1: Type target = to;
duke@1: if ((to.tsym.flags() & INTERFACE) == 0) {
duke@1: assert (from.tsym.flags() & INTERFACE) != 0;
duke@1: reverse = true;
duke@1: to = from;
duke@1: from = target;
duke@1: }
duke@1: List commonSupers = superClosure(to, erasure(from));
duke@1: boolean giveWarning = commonSupers.isEmpty();
duke@1: // The arguments to the supers could be unified here to
duke@1: // get a more accurate analysis
duke@1: while (commonSupers.nonEmpty()) {
duke@1: Type t1 = asSuper(from, commonSupers.head.tsym);
duke@1: Type t2 = commonSupers.head; // same as asSuper(to, commonSupers.head.tsym);
duke@1: if (disjointTypes(t1.getTypeArguments(), t2.getTypeArguments()))
duke@1: return false;
duke@1: giveWarning = giveWarning || (reverse ? giveWarning(t2, t1) : giveWarning(t1, t2));
duke@1: commonSupers = commonSupers.tail;
duke@1: }
mcimadamore@187: if (giveWarning && !isReifiable(reverse ? from : to))
duke@1: warn.warnUnchecked();
duke@1: if (!source.allowCovariantReturns())
duke@1: // reject if there is a common method signature with
duke@1: // incompatible return types.
duke@1: chk.checkCompatibleAbstracts(warn.pos(), from, to);
duke@1: return true;
duke@1: }
duke@1:
duke@1: private boolean sideCastFinal(Type from, Type to, Warner warn) {
duke@1: // We are casting from type $from$ to type $to$, which are
duke@1: // unrelated types one of which is final and the other of
duke@1: // which is an interface. This method
duke@1: // tries to reject a cast by transferring type parameters
duke@1: // from the final class to the interface.
duke@1: boolean reverse = false;
duke@1: Type target = to;
duke@1: if ((to.tsym.flags() & INTERFACE) == 0) {
duke@1: assert (from.tsym.flags() & INTERFACE) != 0;
duke@1: reverse = true;
duke@1: to = from;
duke@1: from = target;
duke@1: }
duke@1: assert (from.tsym.flags() & FINAL) != 0;
duke@1: Type t1 = asSuper(from, to.tsym);
duke@1: if (t1 == null) return false;
duke@1: Type t2 = to;
duke@1: if (disjointTypes(t1.getTypeArguments(), t2.getTypeArguments()))
duke@1: return false;
duke@1: if (!source.allowCovariantReturns())
duke@1: // reject if there is a common method signature with
duke@1: // incompatible return types.
duke@1: chk.checkCompatibleAbstracts(warn.pos(), from, to);
duke@1: if (!isReifiable(target) &&
duke@1: (reverse ? giveWarning(t2, t1) : giveWarning(t1, t2)))
duke@1: warn.warnUnchecked();
duke@1: return true;
duke@1: }
duke@1:
duke@1: private boolean giveWarning(Type from, Type to) {
mcimadamore@235: Type subFrom = asSub(from, to.tsym);
mcimadamore@235: return to.isParameterized() &&
mcimadamore@235: (!(isUnbounded(to) ||
mcimadamore@235: isSubtype(from, to) ||
mcimadamore@235: ((subFrom != null) && isSameType(subFrom, to))));
duke@1: }
duke@1:
duke@1: private List superClosure(Type t, Type s) {
duke@1: List cl = List.nil();
duke@1: for (List l = interfaces(t); l.nonEmpty(); l = l.tail) {
duke@1: if (isSubtype(s, erasure(l.head))) {
duke@1: cl = insert(cl, l.head);
duke@1: } else {
duke@1: cl = union(cl, superClosure(l.head, s));
duke@1: }
duke@1: }
duke@1: return cl;
duke@1: }
duke@1:
duke@1: private boolean containsTypeEquivalent(Type t, Type s) {
duke@1: return
duke@1: isSameType(t, s) || // shortcut
duke@1: containsType(t, s) && containsType(s, t);
duke@1: }
duke@1:
mcimadamore@138: //
duke@1: /**
duke@1: * Adapt a type by computing a substitution which maps a source
duke@1: * type to a target type.
duke@1: *
duke@1: * @param source the source type
duke@1: * @param target the target type
duke@1: * @param from the type variables of the computed substitution
duke@1: * @param to the types of the computed substitution.
duke@1: */
duke@1: public void adapt(Type source,
duke@1: Type target,
duke@1: ListBuffer from,
duke@1: ListBuffer to) throws AdaptFailure {
mcimadamore@138: new Adapter(from, to).adapt(source, target);
mcimadamore@138: }
mcimadamore@138:
mcimadamore@138: class Adapter extends SimpleVisitor {
mcimadamore@138:
mcimadamore@138: ListBuffer from;
mcimadamore@138: ListBuffer to;
mcimadamore@138: Map mapping;
mcimadamore@138:
mcimadamore@138: Adapter(ListBuffer from, ListBuffer to) {
mcimadamore@138: this.from = from;
mcimadamore@138: this.to = to;
mcimadamore@138: mapping = new HashMap();
duke@1: }
mcimadamore@138:
mcimadamore@138: public void adapt(Type source, Type target) throws AdaptFailure {
mcimadamore@138: visit(source, target);
mcimadamore@138: List fromList = from.toList();
mcimadamore@138: List toList = to.toList();
mcimadamore@138: while (!fromList.isEmpty()) {
mcimadamore@138: Type val = mapping.get(fromList.head.tsym);
mcimadamore@138: if (toList.head != val)
mcimadamore@138: toList.head = val;
mcimadamore@138: fromList = fromList.tail;
mcimadamore@138: toList = toList.tail;
mcimadamore@138: }
mcimadamore@138: }
mcimadamore@138:
mcimadamore@138: @Override
mcimadamore@138: public Void visitClassType(ClassType source, Type target) throws AdaptFailure {
mcimadamore@138: if (target.tag == CLASS)
mcimadamore@138: adaptRecursive(source.allparams(), target.allparams());
mcimadamore@138: return null;
mcimadamore@138: }
mcimadamore@138:
mcimadamore@138: @Override
mcimadamore@138: public Void visitArrayType(ArrayType source, Type target) throws AdaptFailure {
mcimadamore@138: if (target.tag == ARRAY)
mcimadamore@138: adaptRecursive(elemtype(source), elemtype(target));
mcimadamore@138: return null;
mcimadamore@138: }
mcimadamore@138:
mcimadamore@138: @Override
mcimadamore@138: public Void visitWildcardType(WildcardType source, Type target) throws AdaptFailure {
mcimadamore@138: if (source.isExtendsBound())
mcimadamore@138: adaptRecursive(upperBound(source), upperBound(target));
mcimadamore@138: else if (source.isSuperBound())
mcimadamore@138: adaptRecursive(lowerBound(source), lowerBound(target));
mcimadamore@138: return null;
mcimadamore@138: }
mcimadamore@138:
mcimadamore@138: @Override
mcimadamore@138: public Void visitTypeVar(TypeVar source, Type target) throws AdaptFailure {
mcimadamore@138: // Check to see if there is
mcimadamore@138: // already a mapping for $source$, in which case
mcimadamore@138: // the old mapping will be merged with the new
mcimadamore@138: Type val = mapping.get(source.tsym);
mcimadamore@138: if (val != null) {
mcimadamore@138: if (val.isSuperBound() && target.isSuperBound()) {
mcimadamore@138: val = isSubtype(lowerBound(val), lowerBound(target))
mcimadamore@138: ? target : val;
mcimadamore@138: } else if (val.isExtendsBound() && target.isExtendsBound()) {
mcimadamore@138: val = isSubtype(upperBound(val), upperBound(target))
mcimadamore@138: ? val : target;
mcimadamore@138: } else if (!isSameType(val, target)) {
mcimadamore@138: throw new AdaptFailure();
duke@1: }
mcimadamore@138: } else {
mcimadamore@138: val = target;
mcimadamore@138: from.append(source);
mcimadamore@138: to.append(target);
mcimadamore@138: }
mcimadamore@138: mapping.put(source.tsym, val);
mcimadamore@138: return null;
mcimadamore@138: }
mcimadamore@138:
mcimadamore@138: @Override
mcimadamore@138: public Void visitType(Type source, Type target) {
mcimadamore@138: return null;
mcimadamore@138: }
mcimadamore@138:
mcimadamore@138: private Set cache = new HashSet();
mcimadamore@138:
mcimadamore@138: private void adaptRecursive(Type source, Type target) {
mcimadamore@138: TypePair pair = new TypePair(source, target);
mcimadamore@138: if (cache.add(pair)) {
mcimadamore@138: try {
mcimadamore@138: visit(source, target);
mcimadamore@138: } finally {
mcimadamore@138: cache.remove(pair);
duke@1: }
duke@1: }
duke@1: }
mcimadamore@138:
mcimadamore@138: private void adaptRecursive(List source, List target) {
mcimadamore@138: if (source.length() == target.length()) {
mcimadamore@138: while (source.nonEmpty()) {
mcimadamore@138: adaptRecursive(source.head, target.head);
mcimadamore@138: source = source.tail;
mcimadamore@138: target = target.tail;
mcimadamore@138: }
duke@1: }
duke@1: }
duke@1: }
duke@1:
mcimadamore@138: public static class AdaptFailure extends RuntimeException {
mcimadamore@138: static final long serialVersionUID = -7490231548272701566L;
mcimadamore@138: }
mcimadamore@138:
duke@1: private void adaptSelf(Type t,
duke@1: ListBuffer from,
duke@1: ListBuffer to) {
duke@1: try {
duke@1: //if (t.tsym.type != t)
duke@1: adapt(t.tsym.type, t, from, to);
duke@1: } catch (AdaptFailure ex) {
duke@1: // Adapt should never fail calculating a mapping from
duke@1: // t.tsym.type to t as there can be no merge problem.
duke@1: throw new AssertionError(ex);
duke@1: }
duke@1: }
mcimadamore@138: //
duke@1:
duke@1: /**
duke@1: * Rewrite all type variables (universal quantifiers) in the given
duke@1: * type to wildcards (existential quantifiers). This is used to
duke@1: * determine if a cast is allowed. For example, if high is true
duke@1: * and {@code T <: Number}, then {@code List} is rewritten to
duke@1: * {@code List extends Number>}. Since {@code List <:
duke@1: * List extends Number>} a {@code List} can be cast to {@code
duke@1: * List} with a warning.
duke@1: * @param t a type
duke@1: * @param high if true return an upper bound; otherwise a lower
duke@1: * bound
duke@1: * @param rewriteTypeVars only rewrite captured wildcards if false;
duke@1: * otherwise rewrite all type variables
duke@1: * @return the type rewritten with wildcards (existential
duke@1: * quantifiers) only
duke@1: */
duke@1: private Type rewriteQuantifiers(Type t, boolean high, boolean rewriteTypeVars) {
mcimadamore@157: return new Rewriter(high, rewriteTypeVars).rewrite(t);
mcimadamore@157: }
mcimadamore@157:
mcimadamore@157: class Rewriter extends UnaryVisitor {
mcimadamore@157:
mcimadamore@157: boolean high;
mcimadamore@157: boolean rewriteTypeVars;
mcimadamore@157:
mcimadamore@157: Rewriter(boolean high, boolean rewriteTypeVars) {
mcimadamore@157: this.high = high;
mcimadamore@157: this.rewriteTypeVars = rewriteTypeVars;
mcimadamore@157: }
mcimadamore@157:
mcimadamore@157: Type rewrite(Type t) {
mcimadamore@157: ListBuffer from = new ListBuffer();
mcimadamore@157: ListBuffer to = new ListBuffer();
mcimadamore@157: adaptSelf(t, from, to);
mcimadamore@157: ListBuffer rewritten = new ListBuffer();
mcimadamore@157: List formals = from.toList();
mcimadamore@157: boolean changed = false;
mcimadamore@157: for (Type arg : to.toList()) {
mcimadamore@157: Type bound = visit(arg);
mcimadamore@157: if (arg != bound) {
mcimadamore@157: changed = true;
mcimadamore@157: bound = high ? makeExtendsWildcard(bound, (TypeVar)formals.head)
mcimadamore@157: : makeSuperWildcard(bound, (TypeVar)formals.head);
mcimadamore@157: }
mcimadamore@157: rewritten.append(bound);
mcimadamore@157: formals = formals.tail;
duke@1: }
mcimadamore@157: if (changed)
mcimadamore@157: return subst(t.tsym.type, from.toList(), rewritten.toList());
mcimadamore@157: else
mcimadamore@157: return t;
duke@1: }
mcimadamore@157:
mcimadamore@157: public Type visitType(Type t, Void s) {
mcimadamore@157: return high ? upperBound(t) : lowerBound(t);
mcimadamore@157: }
mcimadamore@157:
mcimadamore@157: @Override
mcimadamore@157: public Type visitCapturedType(CapturedType t, Void s) {
mcimadamore@157: return visitWildcardType(t.wildcard, null);
mcimadamore@157: }
mcimadamore@157:
mcimadamore@157: @Override
mcimadamore@157: public Type visitTypeVar(TypeVar t, Void s) {
mcimadamore@157: if (rewriteTypeVars)
mcimadamore@157: return high ? t.bound : syms.botType;
mcimadamore@157: else
mcimadamore@157: return t;
mcimadamore@157: }
mcimadamore@157:
mcimadamore@157: @Override
mcimadamore@157: public Type visitWildcardType(WildcardType t, Void s) {
mcimadamore@157: Type bound = high ? t.getExtendsBound() :
mcimadamore@157: t.getSuperBound();
mcimadamore@157: if (bound == null)
mcimadamore@157: bound = high ? syms.objectType : syms.botType;
mcimadamore@157: return bound;
mcimadamore@157: }
duke@1: }
duke@1:
duke@1: /**
duke@1: * Create a wildcard with the given upper (extends) bound; create
duke@1: * an unbounded wildcard if bound is Object.
duke@1: *
duke@1: * @param bound the upper bound
duke@1: * @param formal the formal type parameter that will be
duke@1: * substituted by the wildcard
duke@1: */
duke@1: private WildcardType makeExtendsWildcard(Type bound, TypeVar formal) {
duke@1: if (bound == syms.objectType) {
duke@1: return new WildcardType(syms.objectType,
duke@1: BoundKind.UNBOUND,
duke@1: syms.boundClass,
duke@1: formal);
duke@1: } else {
duke@1: return new WildcardType(bound,
duke@1: BoundKind.EXTENDS,
duke@1: syms.boundClass,
duke@1: formal);
duke@1: }
duke@1: }
duke@1:
duke@1: /**
duke@1: * Create a wildcard with the given lower (super) bound; create an
duke@1: * unbounded wildcard if bound is bottom (type of {@code null}).
duke@1: *
duke@1: * @param bound the lower bound
duke@1: * @param formal the formal type parameter that will be
duke@1: * substituted by the wildcard
duke@1: */
duke@1: private WildcardType makeSuperWildcard(Type bound, TypeVar formal) {
duke@1: if (bound.tag == BOT) {
duke@1: return new WildcardType(syms.objectType,
duke@1: BoundKind.UNBOUND,
duke@1: syms.boundClass,
duke@1: formal);
duke@1: } else {
duke@1: return new WildcardType(bound,
duke@1: BoundKind.SUPER,
duke@1: syms.boundClass,
duke@1: formal);
duke@1: }
duke@1: }
duke@1:
duke@1: /**
duke@1: * A wrapper for a type that allows use in sets.
duke@1: */
duke@1: class SingletonType {
duke@1: final Type t;
duke@1: SingletonType(Type t) {
duke@1: this.t = t;
duke@1: }
duke@1: public int hashCode() {
duke@1: return Types.this.hashCode(t);
duke@1: }
duke@1: public boolean equals(Object obj) {
duke@1: return (obj instanceof SingletonType) &&
duke@1: isSameType(t, ((SingletonType)obj).t);
duke@1: }
duke@1: public String toString() {
duke@1: return t.toString();
duke@1: }
duke@1: }
duke@1: //
duke@1:
duke@1: //
duke@1: /**
duke@1: * A default visitor for types. All visitor methods except
duke@1: * visitType are implemented by delegating to visitType. Concrete
duke@1: * subclasses must provide an implementation of visitType and can
duke@1: * override other methods as needed.
duke@1: *
duke@1: * @param the return type of the operation implemented by this
duke@1: * visitor; use Void if no return type is needed.
duke@1: * @param the type of the second argument (the first being the
duke@1: * type itself) of the operation implemented by this visitor; use
duke@1: * Void if a second argument is not needed.
duke@1: */
duke@1: public static abstract class DefaultTypeVisitor implements Type.Visitor {
duke@1: final public R visit(Type t, S s) { return t.accept(this, s); }
duke@1: public R visitClassType(ClassType t, S s) { return visitType(t, s); }
duke@1: public R visitWildcardType(WildcardType t, S s) { return visitType(t, s); }
duke@1: public R visitArrayType(ArrayType t, S s) { return visitType(t, s); }
duke@1: public R visitMethodType(MethodType t, S s) { return visitType(t, s); }
duke@1: public R visitPackageType(PackageType t, S s) { return visitType(t, s); }
duke@1: public R visitTypeVar(TypeVar t, S s) { return visitType(t, s); }
duke@1: public R visitCapturedType(CapturedType t, S s) { return visitType(t, s); }
duke@1: public R visitForAll(ForAll t, S s) { return visitType(t, s); }
duke@1: public R visitUndetVar(UndetVar t, S s) { return visitType(t, s); }
duke@1: public R visitErrorType(ErrorType t, S s) { return visitType(t, s); }
duke@1: }
duke@1:
duke@1: /**
mcimadamore@121: * A default visitor for symbols. All visitor methods except
mcimadamore@121: * visitSymbol are implemented by delegating to visitSymbol. Concrete
mcimadamore@121: * subclasses must provide an implementation of visitSymbol and can
mcimadamore@121: * override other methods as needed.
mcimadamore@121: *
mcimadamore@121: * @param the return type of the operation implemented by this
mcimadamore@121: * visitor; use Void if no return type is needed.
mcimadamore@121: * @param the type of the second argument (the first being the
mcimadamore@121: * symbol itself) of the operation implemented by this visitor; use
mcimadamore@121: * Void if a second argument is not needed.
mcimadamore@121: */
mcimadamore@121: public static abstract class DefaultSymbolVisitor implements Symbol.Visitor {
mcimadamore@121: final public R visit(Symbol s, S arg) { return s.accept(this, arg); }
mcimadamore@121: public R visitClassSymbol(ClassSymbol s, S arg) { return visitSymbol(s, arg); }
mcimadamore@121: public R visitMethodSymbol(MethodSymbol s, S arg) { return visitSymbol(s, arg); }
mcimadamore@121: public R visitOperatorSymbol(OperatorSymbol s, S arg) { return visitSymbol(s, arg); }
mcimadamore@121: public R visitPackageSymbol(PackageSymbol s, S arg) { return visitSymbol(s, arg); }
mcimadamore@121: public R visitTypeSymbol(TypeSymbol s, S arg) { return visitSymbol(s, arg); }
mcimadamore@121: public R visitVarSymbol(VarSymbol s, S arg) { return visitSymbol(s, arg); }
mcimadamore@121: }
mcimadamore@121:
mcimadamore@121: /**
duke@1: * A simple visitor for types. This visitor is simple as
duke@1: * captured wildcards, for-all types (generic methods), and
duke@1: * undetermined type variables (part of inference) are hidden.
duke@1: * Captured wildcards are hidden by treating them as type
duke@1: * variables and the rest are hidden by visiting their qtypes.
duke@1: *
duke@1: * @param the return type of the operation implemented by this
duke@1: * visitor; use Void if no return type is needed.
duke@1: * @param the type of the second argument (the first being the
duke@1: * type itself) of the operation implemented by this visitor; use
duke@1: * Void if a second argument is not needed.
duke@1: */
duke@1: public static abstract class SimpleVisitor extends DefaultTypeVisitor {
duke@1: @Override
duke@1: public R visitCapturedType(CapturedType t, S s) {
duke@1: return visitTypeVar(t, s);
duke@1: }
duke@1: @Override
duke@1: public R visitForAll(ForAll t, S s) {
duke@1: return visit(t.qtype, s);
duke@1: }
duke@1: @Override
duke@1: public R visitUndetVar(UndetVar t, S s) {
duke@1: return visit(t.qtype, s);
duke@1: }
duke@1: }
duke@1:
duke@1: /**
duke@1: * A plain relation on types. That is a 2-ary function on the
duke@1: * form Type × Type → Boolean.
duke@1: *
duke@1: */
duke@1: public static abstract class TypeRelation extends SimpleVisitor {}
duke@1:
duke@1: /**
duke@1: * A convenience visitor for implementing operations that only
duke@1: * require one argument (the type itself), that is, unary
duke@1: * operations.
duke@1: *
duke@1: * @param the return type of the operation implemented by this
duke@1: * visitor; use Void if no return type is needed.
duke@1: */
duke@1: public static abstract class UnaryVisitor extends SimpleVisitor {
duke@1: final public R visit(Type t) { return t.accept(this, null); }
duke@1: }
duke@1:
duke@1: /**
duke@1: * A visitor for implementing a mapping from types to types. The
duke@1: * default behavior of this class is to implement the identity
duke@1: * mapping (mapping a type to itself). This can be overridden in
duke@1: * subclasses.
duke@1: *
duke@1: * @param the type of the second argument (the first being the
duke@1: * type itself) of this mapping; use Void if a second argument is
duke@1: * not needed.
duke@1: */
duke@1: public static class MapVisitor extends DefaultTypeVisitor {
duke@1: final public Type visit(Type t) { return t.accept(this, null); }
duke@1: public Type visitType(Type t, S s) { return t; }
duke@1: }
duke@1: //
duke@1: }