duke@1: /*
ohair@554: * Copyright (c) 1999, 2009, Oracle and/or its affiliates. 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
ohair@554: * published by the Free Software Foundation. Oracle designates this
duke@1: * particular file as subject to the "Classpath" exception as provided
ohair@554: * by Oracle 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: *
ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554: * or visit www.oracle.com if you need additional information or have any
ohair@554: * questions.
duke@1: */
duke@1:
duke@1: package com.sun.tools.javac.code;
duke@1:
duke@1: import com.sun.tools.javac.util.*;
duke@1: import com.sun.tools.javac.code.Symbol.*;
duke@1:
duke@1: import javax.lang.model.type.*;
duke@1:
duke@1: import static com.sun.tools.javac.code.Flags.*;
duke@1: import static com.sun.tools.javac.code.Kinds.*;
duke@1: import static com.sun.tools.javac.code.BoundKind.*;
duke@1: import static com.sun.tools.javac.code.TypeTags.*;
duke@1:
duke@1: /** This class represents Java types. The class itself defines the behavior of
duke@1: * the following types:
duke@1: *
duke@1: * base types (tags: BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, BOOLEAN),
duke@1: * type `void' (tag: VOID),
duke@1: * the bottom type (tag: BOT),
duke@1: * the missing type (tag: NONE).
duke@1: *
duke@1: * The behavior of the following types is defined in subclasses, which are
duke@1: * all static inner classes of this class:
duke@1: *
duke@1: * class types (tag: CLASS, class: ClassType),
duke@1: * array types (tag: ARRAY, class: ArrayType),
duke@1: * method types (tag: METHOD, class: MethodType),
duke@1: * package types (tag: PACKAGE, class: PackageType),
duke@1: * type variables (tag: TYPEVAR, class: TypeVar),
duke@1: * type arguments (tag: WILDCARD, class: WildcardType),
duke@1: * polymorphic types (tag: FORALL, class: ForAll),
duke@1: * the error type (tag: ERROR, class: ErrorType).
duke@1: *
duke@1: *
jjg@581: * This is NOT part of any supported API.
jjg@581: * 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: * @see TypeTags
duke@1: */
duke@1: public class Type implements PrimitiveType {
duke@1:
duke@1: /** Constant type: no type at all. */
duke@1: public static final JCNoType noType = new JCNoType(NONE);
duke@1:
duke@1: /** If this switch is turned on, the names of type variables
duke@1: * and anonymous classes are printed with hashcodes appended.
duke@1: */
duke@1: public static boolean moreInfo = false;
duke@1:
duke@1: /** The tag of this type.
duke@1: *
duke@1: * @see TypeTags
duke@1: */
duke@1: public int tag;
duke@1:
duke@1: /** The defining class / interface / package / type variable
duke@1: */
duke@1: public TypeSymbol tsym;
duke@1:
duke@1: /**
duke@1: * The constant value of this type, null if this type does not
duke@1: * have a constant value attribute. Only primitive types and
duke@1: * strings (ClassType) can have a constant value attribute.
duke@1: * @return the constant value attribute of this type
duke@1: */
duke@1: public Object constValue() {
duke@1: return null;
duke@1: }
duke@1:
duke@1: public R accept(Type.Visitor v, S s) { return v.visitType(this, s); }
duke@1:
duke@1: /** Define a type given its tag and type symbol
duke@1: */
duke@1: public Type(int tag, TypeSymbol tsym) {
duke@1: this.tag = tag;
duke@1: this.tsym = tsym;
duke@1: }
duke@1:
duke@1: /** An abstract class for mappings from types to types
duke@1: */
duke@1: public static abstract class Mapping {
duke@1: private String name;
duke@1: public Mapping(String name) {
duke@1: this.name = name;
duke@1: }
duke@1: public abstract Type apply(Type t);
duke@1: public String toString() {
duke@1: return name;
duke@1: }
duke@1: }
duke@1:
duke@1: /** map a type function over all immediate descendants of this type
duke@1: */
duke@1: public Type map(Mapping f) {
duke@1: return this;
duke@1: }
duke@1:
duke@1: /** map a type function over a list of types
duke@1: */
duke@1: public static List map(List ts, Mapping f) {
duke@1: if (ts.nonEmpty()) {
duke@1: List tail1 = map(ts.tail, f);
duke@1: Type t = f.apply(ts.head);
duke@1: if (tail1 != ts.tail || t != ts.head)
duke@1: return tail1.prepend(t);
duke@1: }
duke@1: return ts;
duke@1: }
duke@1:
duke@1: /** Define a constant type, of the same kind as this type
duke@1: * and with given constant value
duke@1: */
duke@1: public Type constType(Object constValue) {
duke@1: final Object value = constValue;
duke@1: assert tag <= BOOLEAN;
duke@1: return new Type(tag, tsym) {
duke@1: @Override
duke@1: public Object constValue() {
duke@1: return value;
duke@1: }
duke@1: @Override
duke@1: public Type baseType() {
duke@1: return tsym.type;
duke@1: }
duke@1: };
duke@1: }
duke@1:
duke@1: /**
duke@1: * If this is a constant type, return its underlying type.
duke@1: * Otherwise, return the type itself.
duke@1: */
duke@1: public Type baseType() {
duke@1: return this;
duke@1: }
duke@1:
duke@1: /** Return the base types of a list of types.
duke@1: */
duke@1: public static List baseTypes(List ts) {
duke@1: if (ts.nonEmpty()) {
duke@1: Type t = ts.head.baseType();
duke@1: List baseTypes = baseTypes(ts.tail);
duke@1: if (t != ts.head || baseTypes != ts.tail)
duke@1: return baseTypes.prepend(t);
duke@1: }
duke@1: return ts;
duke@1: }
duke@1:
duke@1: /** The Java source which this type represents.
duke@1: */
duke@1: public String toString() {
duke@1: String s = (tsym == null || tsym.name == null)
duke@1: ? ""
duke@1: : tsym.name.toString();
duke@1: if (moreInfo && tag == TYPEVAR) s = s + hashCode();
duke@1: return s;
duke@1: }
duke@1:
duke@1: /**
duke@1: * The Java source which this type list represents. A List is
duke@1: * represented as a comma-spearated listing of the elements in
duke@1: * that list.
duke@1: */
duke@1: public static String toString(List ts) {
duke@1: if (ts.isEmpty()) {
duke@1: return "";
duke@1: } else {
duke@1: StringBuffer buf = new StringBuffer();
duke@1: buf.append(ts.head.toString());
duke@1: for (List l = ts.tail; l.nonEmpty(); l = l.tail)
duke@1: buf.append(",").append(l.head.toString());
duke@1: return buf.toString();
duke@1: }
duke@1: }
duke@1:
duke@1: /**
duke@1: * The constant value of this type, converted to String
duke@1: */
duke@1: public String stringValue() {
duke@1: assert constValue() != null;
duke@1: if (tag == BOOLEAN)
duke@1: return ((Integer) constValue()).intValue() == 0 ? "false" : "true";
duke@1: else if (tag == CHAR)
duke@1: return String.valueOf((char) ((Integer) constValue()).intValue());
duke@1: else
duke@1: return constValue().toString();
duke@1: }
duke@1:
duke@1: /**
duke@1: * This method is analogous to isSameType, but weaker, since we
duke@1: * never complete classes. Where isSameType would complete a
duke@1: * class, equals assumes that the two types are different.
duke@1: */
duke@1: public boolean equals(Object t) {
duke@1: return super.equals(t);
duke@1: }
duke@1:
duke@1: public int hashCode() {
duke@1: return super.hashCode();
duke@1: }
duke@1:
duke@1: /** Is this a constant type whose value is false?
duke@1: */
duke@1: public boolean isFalse() {
duke@1: return
duke@1: tag == BOOLEAN &&
duke@1: constValue() != null &&
duke@1: ((Integer)constValue()).intValue() == 0;
duke@1: }
duke@1:
duke@1: /** Is this a constant type whose value is true?
duke@1: */
duke@1: public boolean isTrue() {
duke@1: return
duke@1: tag == BOOLEAN &&
duke@1: constValue() != null &&
duke@1: ((Integer)constValue()).intValue() != 0;
duke@1: }
duke@1:
duke@1: public String argtypes(boolean varargs) {
duke@1: List args = getParameterTypes();
duke@1: if (!varargs) return args.toString();
duke@1: StringBuffer buf = new StringBuffer();
duke@1: while (args.tail.nonEmpty()) {
duke@1: buf.append(args.head);
duke@1: args = args.tail;
duke@1: buf.append(',');
duke@1: }
duke@1: if (args.head.tag == ARRAY) {
duke@1: buf.append(((ArrayType)args.head).elemtype);
duke@1: buf.append("...");
duke@1: } else {
duke@1: buf.append(args.head);
duke@1: }
duke@1: return buf.toString();
duke@1: }
duke@1:
duke@1: /** Access methods.
duke@1: */
duke@1: public List getTypeArguments() { return List.nil(); }
duke@1: public Type getEnclosingType() { return null; }
duke@1: public List getParameterTypes() { return List.nil(); }
duke@1: public Type getReturnType() { return null; }
duke@1: public List getThrownTypes() { return List.nil(); }
duke@1: public Type getUpperBound() { return null; }
duke@1: public Type getLowerBound() { return null; }
duke@1:
duke@1: public void setThrown(List ts) {
duke@1: throw new AssertionError();
duke@1: }
duke@1:
duke@1: /** Navigation methods, these will work for classes, type variables,
duke@1: * foralls, but will return null for arrays and methods.
duke@1: */
duke@1:
duke@1: /** Return all parameters of this type and all its outer types in order
duke@1: * outer (first) to inner (last).
duke@1: */
duke@1: public List allparams() { return List.nil(); }
duke@1:
duke@1: /** Does this type contain "error" elements?
duke@1: */
duke@1: public boolean isErroneous() {
duke@1: return false;
duke@1: }
duke@1:
duke@1: public static boolean isErroneous(List ts) {
duke@1: for (List l = ts; l.nonEmpty(); l = l.tail)
duke@1: if (l.head.isErroneous()) return true;
duke@1: return false;
duke@1: }
duke@1:
duke@1: /** Is this type parameterized?
duke@1: * A class type is parameterized if it has some parameters.
duke@1: * An array type is parameterized if its element type is parameterized.
duke@1: * All other types are not parameterized.
duke@1: */
duke@1: public boolean isParameterized() {
duke@1: return false;
duke@1: }
duke@1:
duke@1: /** Is this type a raw type?
duke@1: * A class type is a raw type if it misses some of its parameters.
duke@1: * An array type is a raw type if its element type is raw.
duke@1: * All other types are not raw.
duke@1: * Type validation will ensure that the only raw types
duke@1: * in a program are types that miss all their type variables.
duke@1: */
duke@1: public boolean isRaw() {
duke@1: return false;
duke@1: }
duke@1:
duke@1: public boolean isCompound() {
duke@1: return tsym.completer == null
duke@1: // Compound types can't have a completer. Calling
duke@1: // flags() will complete the symbol causing the
duke@1: // compiler to load classes unnecessarily. This led
duke@1: // to regression 6180021.
duke@1: && (tsym.flags() & COMPOUND) != 0;
duke@1: }
duke@1:
duke@1: public boolean isInterface() {
duke@1: return (tsym.flags() & INTERFACE) != 0;
duke@1: }
duke@1:
mcimadamore@640: public boolean isFinal() {
mcimadamore@640: return (tsym.flags() & FINAL) != 0;
mcimadamore@640: }
mcimadamore@640:
duke@1: public boolean isPrimitive() {
duke@1: return tag < VOID;
duke@1: }
duke@1:
duke@1: /**
duke@1: * Does this type contain occurrences of type t?
duke@1: */
duke@1: public boolean contains(Type t) {
duke@1: return t == this;
duke@1: }
duke@1:
duke@1: public static boolean contains(List ts, Type t) {
duke@1: for (List l = ts;
duke@1: l.tail != null /*inlined: l.nonEmpty()*/;
duke@1: l = l.tail)
duke@1: if (l.head.contains(t)) return true;
duke@1: return false;
duke@1: }
duke@1:
mcimadamore@635: /** Does this type contain an occurrence of some type in 'ts'?
duke@1: */
mcimadamore@635: public boolean containsAny(List ts) {
mcimadamore@635: for (Type t : ts)
mcimadamore@635: if (this.contains(t)) return true;
mcimadamore@635: return false;
mcimadamore@635: }
mcimadamore@635:
mcimadamore@635: public static boolean containsAny(List ts1, List ts2) {
mcimadamore@635: for (Type t : ts1)
mcimadamore@635: if (t.containsAny(ts2)) return true;
duke@1: return false;
duke@1: }
duke@1:
duke@1: public boolean isSuperBound() { return false; }
duke@1: public boolean isExtendsBound() { return false; }
duke@1: public boolean isUnbound() { return false; }
duke@1: public Type withTypeVar(Type t) { return this; }
duke@1:
duke@1: /** The underlying method type of this type.
duke@1: */
duke@1: public MethodType asMethodType() { throw new AssertionError(); }
duke@1:
duke@1: /** Complete loading all classes in this type.
duke@1: */
duke@1: public void complete() {}
duke@1:
duke@1: public Object clone() {
duke@1: try {
duke@1: return super.clone();
duke@1: } catch (CloneNotSupportedException e) {
duke@1: throw new AssertionError(e);
duke@1: }
duke@1: }
duke@1:
duke@1: public TypeSymbol asElement() {
duke@1: return tsym;
duke@1: }
duke@1:
duke@1: public TypeKind getKind() {
duke@1: switch (tag) {
duke@1: case BYTE: return TypeKind.BYTE;
duke@1: case CHAR: return TypeKind.CHAR;
duke@1: case SHORT: return TypeKind.SHORT;
duke@1: case INT: return TypeKind.INT;
duke@1: case LONG: return TypeKind.LONG;
duke@1: case FLOAT: return TypeKind.FLOAT;
duke@1: case DOUBLE: return TypeKind.DOUBLE;
duke@1: case BOOLEAN: return TypeKind.BOOLEAN;
duke@1: case VOID: return TypeKind.VOID;
duke@1: case BOT: return TypeKind.NULL;
duke@1: case NONE: return TypeKind.NONE;
duke@1: default: return TypeKind.OTHER;
duke@1: }
duke@1: }
duke@1:
duke@1: public R accept(TypeVisitor v, P p) {
duke@1: if (isPrimitive())
duke@1: return v.visitPrimitive(this, p);
duke@1: else
duke@1: throw new AssertionError();
duke@1: }
duke@1:
duke@1: public static class WildcardType extends Type
duke@1: implements javax.lang.model.type.WildcardType {
duke@1:
duke@1: public Type type;
duke@1: public BoundKind kind;
duke@1: public TypeVar bound;
duke@1:
duke@1: @Override
duke@1: public R accept(Type.Visitor v, S s) {
duke@1: return v.visitWildcardType(this, s);
duke@1: }
duke@1:
duke@1: public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) {
duke@1: super(WILDCARD, tsym);
duke@1: assert(type != null);
duke@1: this.kind = kind;
duke@1: this.type = type;
duke@1: }
duke@1: public WildcardType(WildcardType t, TypeVar bound) {
duke@1: this(t.type, t.kind, t.tsym, bound);
duke@1: }
duke@1:
duke@1: public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, TypeVar bound) {
duke@1: this(type, kind, tsym);
duke@1: this.bound = bound;
duke@1: }
duke@1:
mcimadamore@635: public boolean contains(Type t) {
mcimadamore@635: return kind != UNBOUND && type.contains(t);
mcimadamore@635: }
mcimadamore@635:
duke@1: public boolean isSuperBound() {
duke@1: return kind == SUPER ||
duke@1: kind == UNBOUND;
duke@1: }
duke@1: public boolean isExtendsBound() {
duke@1: return kind == EXTENDS ||
duke@1: kind == UNBOUND;
duke@1: }
duke@1: public boolean isUnbound() {
duke@1: return kind == UNBOUND;
duke@1: }
duke@1:
duke@1: public Type withTypeVar(Type t) {
duke@1: //-System.err.println(this+".withTypeVar("+t+");");//DEBUG
duke@1: if (bound == t)
duke@1: return this;
duke@1: bound = (TypeVar)t;
duke@1: return this;
duke@1: }
duke@1:
duke@1: boolean isPrintingBound = false;
duke@1: public String toString() {
duke@1: StringBuffer s = new StringBuffer();
duke@1: s.append(kind.toString());
duke@1: if (kind != UNBOUND)
duke@1: s.append(type);
duke@1: if (moreInfo && bound != null && !isPrintingBound)
duke@1: try {
duke@1: isPrintingBound = true;
duke@1: s.append("{:").append(bound.bound).append(":}");
duke@1: } finally {
duke@1: isPrintingBound = false;
duke@1: }
duke@1: return s.toString();
duke@1: }
duke@1:
duke@1: public Type map(Mapping f) {
duke@1: //- System.err.println(" (" + this + ").map(" + f + ")");//DEBUG
duke@1: Type t = type;
duke@1: if (t != null)
duke@1: t = f.apply(t);
duke@1: if (t == type)
duke@1: return this;
duke@1: else
duke@1: return new WildcardType(t, kind, tsym, bound);
duke@1: }
duke@1:
duke@1: public Type getExtendsBound() {
duke@1: if (kind == EXTENDS)
duke@1: return type;
duke@1: else
duke@1: return null;
duke@1: }
duke@1:
duke@1: public Type getSuperBound() {
duke@1: if (kind == SUPER)
duke@1: return type;
duke@1: else
duke@1: return null;
duke@1: }
duke@1:
duke@1: public TypeKind getKind() {
duke@1: return TypeKind.WILDCARD;
duke@1: }
duke@1:
duke@1: public R accept(TypeVisitor v, P p) {
duke@1: return v.visitWildcard(this, p);
duke@1: }
duke@1: }
duke@1:
duke@1: public static class ClassType extends Type implements DeclaredType {
duke@1:
duke@1: /** The enclosing type of this type. If this is the type of an inner
duke@1: * class, outer_field refers to the type of its enclosing
duke@1: * instance class, in all other cases it referes to noType.
duke@1: */
duke@1: private Type outer_field;
duke@1:
duke@1: /** The type parameters of this type (to be set once class is loaded).
duke@1: */
duke@1: public List typarams_field;
duke@1:
duke@1: /** A cache variable for the type parameters of this type,
duke@1: * appended to all parameters of its enclosing class.
duke@1: * @see #allparams
duke@1: */
duke@1: public List allparams_field;
duke@1:
duke@1: /** The supertype of this class (to be set once class is loaded).
duke@1: */
duke@1: public Type supertype_field;
duke@1:
duke@1: /** The interfaces of this class (to be set once class is loaded).
duke@1: */
duke@1: public List interfaces_field;
duke@1:
duke@1: public ClassType(Type outer, List typarams, TypeSymbol tsym) {
duke@1: super(CLASS, tsym);
duke@1: this.outer_field = outer;
duke@1: this.typarams_field = typarams;
duke@1: this.allparams_field = null;
duke@1: this.supertype_field = null;
duke@1: this.interfaces_field = null;
duke@1: /*
duke@1: // this can happen during error recovery
duke@1: assert
duke@1: outer.isParameterized() ?
duke@1: typarams.length() == tsym.type.typarams().length() :
duke@1: outer.isRaw() ?
duke@1: typarams.length() == 0 :
duke@1: true;
duke@1: */
duke@1: }
duke@1:
duke@1: @Override
duke@1: public R accept(Type.Visitor v, S s) {
duke@1: return v.visitClassType(this, s);
duke@1: }
duke@1:
duke@1: public Type constType(Object constValue) {
duke@1: final Object value = constValue;
duke@1: return new ClassType(getEnclosingType(), typarams_field, tsym) {
duke@1: @Override
duke@1: public Object constValue() {
duke@1: return value;
duke@1: }
duke@1: @Override
duke@1: public Type baseType() {
duke@1: return tsym.type;
duke@1: }
duke@1: };
duke@1: }
duke@1:
duke@1: /** The Java source which this type represents.
duke@1: */
duke@1: public String toString() {
duke@1: StringBuffer buf = new StringBuffer();
duke@1: if (getEnclosingType().tag == CLASS && tsym.owner.kind == TYP) {
duke@1: buf.append(getEnclosingType().toString());
duke@1: buf.append(".");
duke@1: buf.append(className(tsym, false));
duke@1: } else {
duke@1: buf.append(className(tsym, true));
duke@1: }
duke@1: if (getTypeArguments().nonEmpty()) {
duke@1: buf.append('<');
duke@1: buf.append(getTypeArguments().toString());
duke@1: buf.append(">");
duke@1: }
duke@1: return buf.toString();
duke@1: }
duke@1: //where
duke@1: private String className(Symbol sym, boolean longform) {
jjg@113: if (sym.name.isEmpty() && (sym.flags() & COMPOUND) != 0) {
duke@1: StringBuffer s = new StringBuffer(supertype_field.toString());
duke@1: for (List is=interfaces_field; is.nonEmpty(); is = is.tail) {
duke@1: s.append("&");
duke@1: s.append(is.head.toString());
duke@1: }
duke@1: return s.toString();
jjg@113: } else if (sym.name.isEmpty()) {
duke@1: String s;
duke@1: ClassType norm = (ClassType) tsym.type;
duke@1: if (norm == null) {
duke@1: s = Log.getLocalizedString("anonymous.class", (Object)null);
duke@1: } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
duke@1: s = Log.getLocalizedString("anonymous.class",
duke@1: norm.interfaces_field.head);
duke@1: } else {
duke@1: s = Log.getLocalizedString("anonymous.class",
duke@1: norm.supertype_field);
duke@1: }
duke@1: if (moreInfo)
duke@1: s += String.valueOf(sym.hashCode());
duke@1: return s;
duke@1: } else if (longform) {
duke@1: return sym.getQualifiedName().toString();
duke@1: } else {
duke@1: return sym.name.toString();
duke@1: }
duke@1: }
duke@1:
duke@1: public List getTypeArguments() {
duke@1: if (typarams_field == null) {
duke@1: complete();
duke@1: if (typarams_field == null)
duke@1: typarams_field = List.nil();
duke@1: }
duke@1: return typarams_field;
duke@1: }
duke@1:
mcimadamore@30: public boolean hasErasedSupertypes() {
mcimadamore@30: return isRaw();
mcimadamore@30: }
mcimadamore@30:
duke@1: public Type getEnclosingType() {
duke@1: return outer_field;
duke@1: }
duke@1:
duke@1: public void setEnclosingType(Type outer) {
duke@1: outer_field = outer;
duke@1: }
duke@1:
duke@1: public List allparams() {
duke@1: if (allparams_field == null) {
duke@1: allparams_field = getTypeArguments().prependList(getEnclosingType().allparams());
duke@1: }
duke@1: return allparams_field;
duke@1: }
duke@1:
duke@1: public boolean isErroneous() {
duke@1: return
duke@1: getEnclosingType().isErroneous() ||
duke@1: isErroneous(getTypeArguments()) ||
duke@1: this != tsym.type && tsym.type.isErroneous();
duke@1: }
duke@1:
duke@1: public boolean isParameterized() {
duke@1: return allparams().tail != null;
duke@1: // optimization, was: allparams().nonEmpty();
duke@1: }
duke@1:
duke@1: /** A cache for the rank. */
duke@1: int rank_field = -1;
duke@1:
duke@1: /** A class type is raw if it misses some
duke@1: * of its type parameter sections.
duke@1: * After validation, this is equivalent to:
duke@1: * allparams.isEmpty() && tsym.type.allparams.nonEmpty();
duke@1: */
duke@1: public boolean isRaw() {
duke@1: return
duke@1: this != tsym.type && // necessary, but not sufficient condition
duke@1: tsym.type.allparams().nonEmpty() &&
duke@1: allparams().isEmpty();
duke@1: }
duke@1:
duke@1: public Type map(Mapping f) {
duke@1: Type outer = getEnclosingType();
duke@1: Type outer1 = f.apply(outer);
duke@1: List typarams = getTypeArguments();
duke@1: List typarams1 = map(typarams, f);
duke@1: if (outer1 == outer && typarams1 == typarams) return this;
duke@1: else return new ClassType(outer1, typarams1, tsym);
duke@1: }
duke@1:
duke@1: public boolean contains(Type elem) {
duke@1: return
duke@1: elem == this
duke@1: || (isParameterized()
mcimadamore@635: && (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem)))
mcimadamore@635: || (isCompound()
mcimadamore@635: && (supertype_field.contains(elem) || contains(interfaces_field, elem)));
duke@1: }
duke@1:
duke@1: public void complete() {
duke@1: if (tsym.completer != null) tsym.complete();
duke@1: }
duke@1:
duke@1: public TypeKind getKind() {
duke@1: return TypeKind.DECLARED;
duke@1: }
duke@1:
duke@1: public R accept(TypeVisitor v, P p) {
duke@1: return v.visitDeclared(this, p);
duke@1: }
duke@1: }
duke@1:
mcimadamore@30: public static class ErasedClassType extends ClassType {
mcimadamore@30: public ErasedClassType(Type outer, TypeSymbol tsym) {
mcimadamore@30: super(outer, List.nil(), tsym);
mcimadamore@30: }
mcimadamore@30:
mcimadamore@30: @Override
mcimadamore@30: public boolean hasErasedSupertypes() {
mcimadamore@30: return true;
mcimadamore@30: }
mcimadamore@30: }
mcimadamore@30:
duke@1: public static class ArrayType extends Type
duke@1: implements javax.lang.model.type.ArrayType {
duke@1:
duke@1: public Type elemtype;
duke@1:
duke@1: public ArrayType(Type elemtype, TypeSymbol arrayClass) {
duke@1: super(ARRAY, arrayClass);
duke@1: this.elemtype = elemtype;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public R accept(Type.Visitor v, S s) {
duke@1: return v.visitArrayType(this, s);
duke@1: }
duke@1:
duke@1: public String toString() {
duke@1: return elemtype + "[]";
duke@1: }
duke@1:
duke@1: public boolean equals(Object obj) {
duke@1: return
duke@1: this == obj ||
duke@1: (obj instanceof ArrayType &&
duke@1: this.elemtype.equals(((ArrayType)obj).elemtype));
duke@1: }
duke@1:
duke@1: public int hashCode() {
duke@1: return (ARRAY << 5) + elemtype.hashCode();
duke@1: }
duke@1:
duke@1: public List allparams() { return elemtype.allparams(); }
duke@1:
duke@1: public boolean isErroneous() {
duke@1: return elemtype.isErroneous();
duke@1: }
duke@1:
duke@1: public boolean isParameterized() {
duke@1: return elemtype.isParameterized();
duke@1: }
duke@1:
duke@1: public boolean isRaw() {
duke@1: return elemtype.isRaw();
duke@1: }
duke@1:
duke@1: public Type map(Mapping f) {
duke@1: Type elemtype1 = f.apply(elemtype);
duke@1: if (elemtype1 == elemtype) return this;
duke@1: else return new ArrayType(elemtype1, tsym);
duke@1: }
duke@1:
duke@1: public boolean contains(Type elem) {
duke@1: return elem == this || elemtype.contains(elem);
duke@1: }
duke@1:
duke@1: public void complete() {
duke@1: elemtype.complete();
duke@1: }
duke@1:
duke@1: public Type getComponentType() {
duke@1: return elemtype;
duke@1: }
duke@1:
duke@1: public TypeKind getKind() {
duke@1: return TypeKind.ARRAY;
duke@1: }
duke@1:
duke@1: public R accept(TypeVisitor v, P p) {
duke@1: return v.visitArray(this, p);
duke@1: }
duke@1: }
duke@1:
duke@1: public static class MethodType extends Type
duke@1: implements Cloneable, ExecutableType {
duke@1:
duke@1: public List argtypes;
duke@1: public Type restype;
duke@1: public List thrown;
duke@1:
duke@1: public MethodType(List argtypes,
duke@1: Type restype,
duke@1: List thrown,
duke@1: TypeSymbol methodClass) {
duke@1: super(METHOD, methodClass);
duke@1: this.argtypes = argtypes;
duke@1: this.restype = restype;
duke@1: this.thrown = thrown;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public R accept(Type.Visitor v, S s) {
duke@1: return v.visitMethodType(this, s);
duke@1: }
duke@1:
duke@1: /** The Java source which this type represents.
duke@1: *
duke@1: * XXX 06/09/99 iris This isn't correct Java syntax, but it probably
duke@1: * should be.
duke@1: */
duke@1: public String toString() {
duke@1: return "(" + argtypes + ")" + restype;
duke@1: }
duke@1:
duke@1: public boolean equals(Object obj) {
duke@1: if (this == obj)
duke@1: return true;
duke@1: if (!(obj instanceof MethodType))
duke@1: return false;
duke@1: MethodType m = (MethodType)obj;
duke@1: List args1 = argtypes;
duke@1: List args2 = m.argtypes;
duke@1: while (!args1.isEmpty() && !args2.isEmpty()) {
duke@1: if (!args1.head.equals(args2.head))
duke@1: return false;
duke@1: args1 = args1.tail;
duke@1: args2 = args2.tail;
duke@1: }
duke@1: if (!args1.isEmpty() || !args2.isEmpty())
duke@1: return false;
duke@1: return restype.equals(m.restype);
duke@1: }
duke@1:
duke@1: public int hashCode() {
duke@1: int h = METHOD;
duke@1: for (List thisargs = this.argtypes;
duke@1: thisargs.tail != null; /*inlined: thisargs.nonEmpty()*/
duke@1: thisargs = thisargs.tail)
duke@1: h = (h << 5) + thisargs.head.hashCode();
duke@1: return (h << 5) + this.restype.hashCode();
duke@1: }
duke@1:
duke@1: public List getParameterTypes() { return argtypes; }
duke@1: public Type getReturnType() { return restype; }
duke@1: public List getThrownTypes() { return thrown; }
duke@1:
duke@1: public void setThrown(List t) {
duke@1: thrown = t;
duke@1: }
duke@1:
duke@1: public boolean isErroneous() {
duke@1: return
duke@1: isErroneous(argtypes) ||
duke@1: restype != null && restype.isErroneous();
duke@1: }
duke@1:
duke@1: public Type map(Mapping f) {
duke@1: List argtypes1 = map(argtypes, f);
duke@1: Type restype1 = f.apply(restype);
duke@1: List thrown1 = map(thrown, f);
duke@1: if (argtypes1 == argtypes &&
duke@1: restype1 == restype &&
duke@1: thrown1 == thrown) return this;
duke@1: else return new MethodType(argtypes1, restype1, thrown1, tsym);
duke@1: }
duke@1:
duke@1: public boolean contains(Type elem) {
duke@1: return elem == this || contains(argtypes, elem) || restype.contains(elem);
duke@1: }
duke@1:
duke@1: public MethodType asMethodType() { return this; }
duke@1:
duke@1: public void complete() {
duke@1: for (List l = argtypes; l.nonEmpty(); l = l.tail)
duke@1: l.head.complete();
duke@1: restype.complete();
duke@1: for (List l = thrown; l.nonEmpty(); l = l.tail)
duke@1: l.head.complete();
duke@1: }
duke@1:
duke@1: public List getTypeVariables() {
duke@1: return List.nil();
duke@1: }
duke@1:
duke@1: public TypeSymbol asElement() {
duke@1: return null;
duke@1: }
duke@1:
duke@1: public TypeKind getKind() {
duke@1: return TypeKind.EXECUTABLE;
duke@1: }
duke@1:
duke@1: public R accept(TypeVisitor v, P p) {
duke@1: return v.visitExecutable(this, p);
duke@1: }
duke@1: }
duke@1:
duke@1: public static class PackageType extends Type implements NoType {
duke@1:
duke@1: PackageType(TypeSymbol tsym) {
duke@1: super(PACKAGE, tsym);
duke@1: }
duke@1:
duke@1: @Override
duke@1: public R accept(Type.Visitor v, S s) {
duke@1: return v.visitPackageType(this, s);
duke@1: }
duke@1:
duke@1: public String toString() {
duke@1: return tsym.getQualifiedName().toString();
duke@1: }
duke@1:
duke@1: public TypeKind getKind() {
duke@1: return TypeKind.PACKAGE;
duke@1: }
duke@1:
duke@1: public R accept(TypeVisitor v, P p) {
duke@1: return v.visitNoType(this, p);
duke@1: }
duke@1: }
duke@1:
duke@1: public static class TypeVar extends Type implements TypeVariable {
duke@1:
duke@1: /** The bound of this type variable; set from outside.
duke@1: * Must be nonempty once it is set.
duke@1: * For a bound, `bound' is the bound type itself.
duke@1: * Multiple bounds are expressed as a single class type which has the
duke@1: * individual bounds as superclass, respectively interfaces.
duke@1: * The class type then has as `tsym' a compiler generated class `c',
duke@1: * which has a flag COMPOUND and whose owner is the type variable
duke@1: * itself. Furthermore, the erasure_field of the class
duke@1: * points to the first class or interface bound.
duke@1: */
duke@1: public Type bound = null;
duke@1: public Type lower;
duke@1:
duke@1: public TypeVar(Name name, Symbol owner, Type lower) {
duke@1: super(TYPEVAR, null);
duke@1: tsym = new TypeSymbol(0, name, this, owner);
duke@1: this.lower = lower;
duke@1: }
duke@1:
duke@1: public TypeVar(TypeSymbol tsym, Type bound, Type lower) {
duke@1: super(TYPEVAR, tsym);
duke@1: this.bound = bound;
duke@1: this.lower = lower;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public R accept(Type.Visitor v, S s) {
duke@1: return v.visitTypeVar(this, s);
duke@1: }
duke@1:
duke@1: public Type getUpperBound() { return bound; }
duke@1:
duke@1: int rank_field = -1;
duke@1:
duke@1: public Type getLowerBound() {
duke@1: return lower;
duke@1: }
duke@1:
duke@1: public TypeKind getKind() {
duke@1: return TypeKind.TYPEVAR;
duke@1: }
duke@1:
mcimadamore@79: public boolean isCaptured() {
mcimadamore@79: return false;
mcimadamore@79: }
mcimadamore@79:
duke@1: public R accept(TypeVisitor v, P p) {
duke@1: return v.visitTypeVariable(this, p);
duke@1: }
duke@1: }
duke@1:
duke@1: /** A captured type variable comes from wildcards which can have
duke@1: * both upper and lower bound. CapturedType extends TypeVar with
duke@1: * a lower bound.
duke@1: */
duke@1: public static class CapturedType extends TypeVar {
duke@1:
duke@1: public Type lower;
duke@1: public WildcardType wildcard;
duke@1:
duke@1: public CapturedType(Name name,
duke@1: Symbol owner,
duke@1: Type upper,
duke@1: Type lower,
duke@1: WildcardType wildcard) {
duke@1: super(name, owner, lower);
duke@1: assert lower != null;
duke@1: this.bound = upper;
duke@1: this.lower = lower;
duke@1: this.wildcard = wildcard;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public R accept(Type.Visitor v, S s) {
duke@1: return v.visitCapturedType(this, s);
duke@1: }
duke@1:
duke@1: public Type getLowerBound() {
duke@1: return lower;
duke@1: }
duke@1:
duke@1: @Override
mcimadamore@79: public boolean isCaptured() {
mcimadamore@79: return true;
mcimadamore@79: }
mcimadamore@79:
mcimadamore@79: @Override
duke@1: public String toString() {
duke@1: return "capture#"
mcimadamore@288: + (hashCode() & 0xFFFFFFFFL) % Printer.PRIME
duke@1: + " of "
duke@1: + wildcard;
duke@1: }
duke@1: }
duke@1:
duke@1: public static abstract class DelegatedType extends Type {
duke@1: public Type qtype;
duke@1: public DelegatedType(int tag, Type qtype) {
duke@1: super(tag, qtype.tsym);
duke@1: this.qtype = qtype;
duke@1: }
duke@1: public String toString() { return qtype.toString(); }
duke@1: public List getTypeArguments() { return qtype.getTypeArguments(); }
duke@1: public Type getEnclosingType() { return qtype.getEnclosingType(); }
duke@1: public List getParameterTypes() { return qtype.getParameterTypes(); }
duke@1: public Type getReturnType() { return qtype.getReturnType(); }
duke@1: public List getThrownTypes() { return qtype.getThrownTypes(); }
duke@1: public List allparams() { return qtype.allparams(); }
duke@1: public Type getUpperBound() { return qtype.getUpperBound(); }
duke@1: public Object clone() { DelegatedType t = (DelegatedType)super.clone(); t.qtype = (Type)qtype.clone(); return t; }
duke@1: public boolean isErroneous() { return qtype.isErroneous(); }
duke@1: }
duke@1:
duke@1: public static class ForAll extends DelegatedType
duke@1: implements Cloneable, ExecutableType {
duke@1: public List tvars;
duke@1:
duke@1: public ForAll(List tvars, Type qtype) {
duke@1: super(FORALL, qtype);
duke@1: this.tvars = tvars;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public R accept(Type.Visitor v, S s) {
duke@1: return v.visitForAll(this, s);
duke@1: }
duke@1:
duke@1: public String toString() {
duke@1: return "<" + tvars + ">" + qtype;
duke@1: }
duke@1:
duke@1: public List getTypeArguments() { return tvars; }
duke@1:
duke@1: public void setThrown(List t) {
duke@1: qtype.setThrown(t);
duke@1: }
duke@1:
duke@1: public Object clone() {
duke@1: ForAll result = (ForAll)super.clone();
duke@1: result.qtype = (Type)result.qtype.clone();
duke@1: return result;
duke@1: }
duke@1:
duke@1: public boolean isErroneous() {
duke@1: return qtype.isErroneous();
duke@1: }
duke@1:
mcimadamore@299: /**
mcimadamore@299: * Replaces this ForAll's typevars with a set of concrete Java types
mcimadamore@396: * and returns the instantiated generic type. Subclasses should override
mcimadamore@299: * in order to check that the list of types is a valid instantiation
mcimadamore@299: * of the ForAll's typevars.
mcimadamore@299: *
mcimadamore@299: * @param actuals list of actual types
mcimadamore@299: * @param types types instance
mcimadamore@299: * @return qtype where all occurrences of tvars are replaced
mcimadamore@299: * by types in actuals
mcimadamore@299: */
mcimadamore@299: public Type inst(List actuals, Types types) {
mcimadamore@299: return types.subst(qtype, tvars, actuals);
mcimadamore@299: }
mcimadamore@299:
mcimadamore@396: /**
mcimadamore@396: * Kind of type-constraint derived during type inference
mcimadamore@396: */
mcimadamore@396: public enum ConstraintKind {
mcimadamore@396: /**
mcimadamore@396: * upper bound constraint (a type variable must be instantiated
mcimadamore@396: * with a type T, where T is a subtype of all the types specified by
mcimadamore@396: * its EXTENDS constraints).
mcimadamore@396: */
mcimadamore@396: EXTENDS,
mcimadamore@396: /**
mcimadamore@396: * lower bound constraint (a type variable must be instantiated
mcimadamore@396: * with a type T, where T is a supertype of all the types specified by
mcimadamore@396: * its SUPER constraints).
mcimadamore@396: */
mcimadamore@396: SUPER,
mcimadamore@396: /**
mcimadamore@396: * equality constraint (a type variable must be instantiated to the type
mcimadamore@396: * specified by its EQUAL constraint.
mcimadamore@396: */
mcimadamore@396: EQUAL;
mcimadamore@396: }
mcimadamore@396:
mcimadamore@396: /**
mcimadamore@396: * Get the type-constraints of a given kind for a given type-variable of
mcimadamore@396: * this ForAll type. Subclasses should override in order to return more
mcimadamore@396: * accurate sets of constraints.
mcimadamore@396: *
mcimadamore@396: * @param tv the type-variable for which the constraint is to be retrieved
mcimadamore@396: * @param ck the constraint kind to be retrieved
mcimadamore@396: * @return the list of types specified by the selected constraint
mcimadamore@396: */
mcimadamore@396: public List getConstraints(TypeVar tv, ConstraintKind ck) {
mcimadamore@396: return List.nil();
mcimadamore@396: }
mcimadamore@396:
duke@1: public Type map(Mapping f) {
duke@1: return f.apply(qtype);
duke@1: }
duke@1:
duke@1: public boolean contains(Type elem) {
duke@1: return qtype.contains(elem);
duke@1: }
duke@1:
duke@1: public MethodType asMethodType() {
duke@1: return qtype.asMethodType();
duke@1: }
duke@1:
duke@1: public void complete() {
duke@1: for (List l = tvars; l.nonEmpty(); l = l.tail) {
duke@1: ((TypeVar)l.head).bound.complete();
duke@1: }
duke@1: qtype.complete();
duke@1: }
duke@1:
duke@1: public List getTypeVariables() {
duke@1: return List.convert(TypeVar.class, getTypeArguments());
duke@1: }
duke@1:
duke@1: public TypeKind getKind() {
duke@1: return TypeKind.EXECUTABLE;
duke@1: }
duke@1:
duke@1: public R accept(TypeVisitor v, P p) {
duke@1: return v.visitExecutable(this, p);
duke@1: }
duke@1: }
duke@1:
duke@1: /** A class for instantiatable variables, for use during type
duke@1: * inference.
duke@1: */
duke@1: public static class UndetVar extends DelegatedType {
duke@1: public List lobounds = List.nil();
duke@1: public List hibounds = List.nil();
duke@1: public Type inst = null;
duke@1:
duke@1: @Override
duke@1: public R accept(Type.Visitor v, S s) {
duke@1: return v.visitUndetVar(this, s);
duke@1: }
duke@1:
duke@1: public UndetVar(Type origin) {
duke@1: super(UNDETVAR, origin);
duke@1: }
duke@1:
duke@1: public String toString() {
duke@1: if (inst != null) return inst.toString();
duke@1: else return qtype + "?";
duke@1: }
duke@1:
duke@1: public Type baseType() {
duke@1: if (inst != null) return inst.baseType();
duke@1: else return this;
duke@1: }
duke@1: }
duke@1:
duke@1: /** Represents VOID or NONE.
duke@1: */
duke@1: static class JCNoType extends Type implements NoType {
duke@1: public JCNoType(int tag) {
duke@1: super(tag, null);
duke@1: }
duke@1:
duke@1: @Override
duke@1: public TypeKind getKind() {
duke@1: switch (tag) {
duke@1: case VOID: return TypeKind.VOID;
duke@1: case NONE: return TypeKind.NONE;
duke@1: default:
duke@1: throw new AssertionError("Unexpected tag: " + tag);
duke@1: }
duke@1: }
duke@1:
duke@1: @Override
duke@1: public R accept(TypeVisitor v, P p) {
duke@1: return v.visitNoType(this, p);
duke@1: }
duke@1: }
duke@1:
duke@1: static class BottomType extends Type implements NullType {
duke@1: public BottomType() {
duke@1: super(TypeTags.BOT, null);
duke@1: }
duke@1:
duke@1: @Override
duke@1: public TypeKind getKind() {
duke@1: return TypeKind.NULL;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public R accept(TypeVisitor v, P p) {
duke@1: return v.visitNull(this, p);
duke@1: }
duke@1:
duke@1: @Override
duke@1: public Type constType(Object value) {
duke@1: return this;
duke@1: }
duke@1:
duke@1: @Override
duke@1: public String stringValue() {
duke@1: return "null";
duke@1: }
duke@1: }
duke@1:
duke@1: public static class ErrorType extends ClassType
duke@1: implements javax.lang.model.type.ErrorType {
duke@1:
jjg@110: private Type originalType = null;
jjg@110:
jjg@110: public ErrorType(Type originalType, TypeSymbol tsym) {
duke@1: super(noType, List.nil(), null);
duke@1: tag = ERROR;
jjg@110: this.tsym = tsym;
jjg@110: this.originalType = (originalType == null ? noType : originalType);
duke@1: }
duke@1:
jjg@110: public ErrorType(ClassSymbol c, Type originalType) {
jjg@110: this(originalType, c);
duke@1: c.type = this;
duke@1: c.kind = ERR;
duke@1: c.members_field = new Scope.ErrorScope(c);
duke@1: }
duke@1:
jjg@110: public ErrorType(Name name, TypeSymbol container, Type originalType) {
jjg@110: this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container), originalType);
duke@1: }
duke@1:
duke@1: @Override
duke@1: public R accept(Type.Visitor v, S s) {
duke@1: return v.visitErrorType(this, s);
duke@1: }
duke@1:
duke@1: public Type constType(Object constValue) { return this; }
duke@1: public Type getEnclosingType() { return this; }
duke@1: public Type getReturnType() { return this; }
duke@1: public Type asSub(Symbol sym) { return this; }
duke@1: public Type map(Mapping f) { return this; }
duke@1:
duke@1: public boolean isGenType(Type t) { return true; }
duke@1: public boolean isErroneous() { return true; }
duke@1: public boolean isCompound() { return false; }
duke@1: public boolean isInterface() { return false; }
duke@1:
duke@1: public List allparams() { return List.nil(); }
duke@1: public List getTypeArguments() { return List.nil(); }
duke@1:
duke@1: public TypeKind getKind() {
duke@1: return TypeKind.ERROR;
duke@1: }
duke@1:
jjg@110: public Type getOriginalType() {
jjg@110: return originalType;
jjg@110: }
jjg@110:
duke@1: public R accept(TypeVisitor v, P p) {
duke@1: return v.visitError(this, p);
duke@1: }
duke@1: }
duke@1:
duke@1: /**
duke@1: * A visitor for types. A visitor is used to implement operations
duke@1: * (or relations) on types. Most common operations on types are
duke@1: * binary relations and this interface is designed for binary
duke@1: * relations, that is, operations on the form
duke@1: * Type × S → R.
duke@1: *
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 interface Visitor {
duke@1: R visitClassType(ClassType t, S s);
duke@1: R visitWildcardType(WildcardType t, S s);
duke@1: R visitArrayType(ArrayType t, S s);
duke@1: R visitMethodType(MethodType t, S s);
duke@1: R visitPackageType(PackageType t, S s);
duke@1: R visitTypeVar(TypeVar t, S s);
duke@1: R visitCapturedType(CapturedType t, S s);
duke@1: R visitForAll(ForAll t, S s);
duke@1: R visitUndetVar(UndetVar t, S s);
duke@1: R visitErrorType(ErrorType t, S s);
duke@1: R visitType(Type t, S s);
duke@1: }
duke@1: }