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

Mon, 10 Jan 2011 15:08:31 -0800

author
jjg
date
Mon, 10 Jan 2011 15:08:31 -0800
changeset 816
7c537f4298fb
parent 798
4868a36f6fd8
child 828
19c900c703c6
permissions
-rw-r--r--

6396503: javac should not require assertions enabled
Reviewed-by: mcimadamore

     1 /*
     2  * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package com.sun.tools.javac.code;
    28 import com.sun.tools.javac.util.*;
    29 import com.sun.tools.javac.code.Symbol.*;
    31 import javax.lang.model.type.*;
    33 import static com.sun.tools.javac.code.Flags.*;
    34 import static com.sun.tools.javac.code.Kinds.*;
    35 import static com.sun.tools.javac.code.BoundKind.*;
    36 import static com.sun.tools.javac.code.TypeTags.*;
    38 /** This class represents Java types. The class itself defines the behavior of
    39  *  the following types:
    40  *  <pre>
    41  *  base types (tags: BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, BOOLEAN),
    42  *  type `void' (tag: VOID),
    43  *  the bottom type (tag: BOT),
    44  *  the missing type (tag: NONE).
    45  *  </pre>
    46  *  <p>The behavior of the following types is defined in subclasses, which are
    47  *  all static inner classes of this class:
    48  *  <pre>
    49  *  class types (tag: CLASS, class: ClassType),
    50  *  array types (tag: ARRAY, class: ArrayType),
    51  *  method types (tag: METHOD, class: MethodType),
    52  *  package types (tag: PACKAGE, class: PackageType),
    53  *  type variables (tag: TYPEVAR, class: TypeVar),
    54  *  type arguments (tag: WILDCARD, class: WildcardType),
    55  *  polymorphic types (tag: FORALL, class: ForAll),
    56  *  the error type (tag: ERROR, class: ErrorType).
    57  *  </pre>
    58  *
    59  *  <p><b>This is NOT part of any supported API.
    60  *  If you write code that depends on this, you do so at your own risk.
    61  *  This code and its internal interfaces are subject to change or
    62  *  deletion without notice.</b>
    63  *
    64  *  @see TypeTags
    65  */
    66 public class Type implements PrimitiveType {
    68     /** Constant type: no type at all. */
    69     public static final JCNoType noType = new JCNoType(NONE);
    71     /** If this switch is turned on, the names of type variables
    72      *  and anonymous classes are printed with hashcodes appended.
    73      */
    74     public static boolean moreInfo = false;
    76     /** The tag of this type.
    77      *
    78      *  @see TypeTags
    79      */
    80     public int tag;
    82     /** The defining class / interface / package / type variable
    83      */
    84     public TypeSymbol tsym;
    86     /**
    87      * The constant value of this type, null if this type does not
    88      * have a constant value attribute. Only primitive types and
    89      * strings (ClassType) can have a constant value attribute.
    90      * @return the constant value attribute of this type
    91      */
    92     public Object constValue() {
    93         return null;
    94     }
    96     public <R,S> R accept(Type.Visitor<R,S> v, S s) { return v.visitType(this, s); }
    98     /** Define a type given its tag and type symbol
    99      */
   100     public Type(int tag, TypeSymbol tsym) {
   101         this.tag = tag;
   102         this.tsym = tsym;
   103     }
   105     /** An abstract class for mappings from types to types
   106      */
   107     public static abstract class Mapping {
   108         private String name;
   109         public Mapping(String name) {
   110             this.name = name;
   111         }
   112         public abstract Type apply(Type t);
   113         public String toString() {
   114             return name;
   115         }
   116     }
   118     /** map a type function over all immediate descendants of this type
   119      */
   120     public Type map(Mapping f) {
   121         return this;
   122     }
   124     /** map a type function over a list of types
   125      */
   126     public static List<Type> map(List<Type> ts, Mapping f) {
   127         if (ts.nonEmpty()) {
   128             List<Type> tail1 = map(ts.tail, f);
   129             Type t = f.apply(ts.head);
   130             if (tail1 != ts.tail || t != ts.head)
   131                 return tail1.prepend(t);
   132         }
   133         return ts;
   134     }
   136     /** Define a constant type, of the same kind as this type
   137      *  and with given constant value
   138      */
   139     public Type constType(Object constValue) {
   140         final Object value = constValue;
   141         Assert.check(tag <= BOOLEAN);
   142         return new Type(tag, tsym) {
   143                 @Override
   144                 public Object constValue() {
   145                     return value;
   146                 }
   147                 @Override
   148                 public Type baseType() {
   149                     return tsym.type;
   150                 }
   151             };
   152     }
   154     /**
   155      * If this is a constant type, return its underlying type.
   156      * Otherwise, return the type itself.
   157      */
   158     public Type baseType() {
   159         return this;
   160     }
   162     /** Return the base types of a list of types.
   163      */
   164     public static List<Type> baseTypes(List<Type> ts) {
   165         if (ts.nonEmpty()) {
   166             Type t = ts.head.baseType();
   167             List<Type> baseTypes = baseTypes(ts.tail);
   168             if (t != ts.head || baseTypes != ts.tail)
   169                 return baseTypes.prepend(t);
   170         }
   171         return ts;
   172     }
   174     /** The Java source which this type represents.
   175      */
   176     public String toString() {
   177         String s = (tsym == null || tsym.name == null)
   178             ? "<none>"
   179             : tsym.name.toString();
   180         if (moreInfo && tag == TYPEVAR) s = s + hashCode();
   181         return s;
   182     }
   184     /**
   185      * The Java source which this type list represents.  A List is
   186      * represented as a comma-spearated listing of the elements in
   187      * that list.
   188      */
   189     public static String toString(List<Type> ts) {
   190         if (ts.isEmpty()) {
   191             return "";
   192         } else {
   193             StringBuffer buf = new StringBuffer();
   194             buf.append(ts.head.toString());
   195             for (List<Type> l = ts.tail; l.nonEmpty(); l = l.tail)
   196                 buf.append(",").append(l.head.toString());
   197             return buf.toString();
   198         }
   199     }
   201     /**
   202      * The constant value of this type, converted to String
   203      */
   204     public String stringValue() {
   205         Object cv = Assert.checkNonNull(constValue());
   206         if (tag == BOOLEAN)
   207             return ((Integer) cv).intValue() == 0 ? "false" : "true";
   208         else if (tag == CHAR)
   209             return String.valueOf((char) ((Integer) cv).intValue());
   210         else
   211             return cv.toString();
   212     }
   214     /**
   215      * This method is analogous to isSameType, but weaker, since we
   216      * never complete classes. Where isSameType would complete a
   217      * class, equals assumes that the two types are different.
   218      */
   219     public boolean equals(Object t) {
   220         return super.equals(t);
   221     }
   223     public int hashCode() {
   224         return super.hashCode();
   225     }
   227     /** Is this a constant type whose value is false?
   228      */
   229     public boolean isFalse() {
   230         return
   231             tag == BOOLEAN &&
   232             constValue() != null &&
   233             ((Integer)constValue()).intValue() == 0;
   234     }
   236     /** Is this a constant type whose value is true?
   237      */
   238     public boolean isTrue() {
   239         return
   240             tag == BOOLEAN &&
   241             constValue() != null &&
   242             ((Integer)constValue()).intValue() != 0;
   243     }
   245     public String argtypes(boolean varargs) {
   246         List<Type> args = getParameterTypes();
   247         if (!varargs) return args.toString();
   248         StringBuilder buf = new StringBuilder();
   249         while (args.tail.nonEmpty()) {
   250             buf.append(args.head);
   251             args = args.tail;
   252             buf.append(',');
   253         }
   254         if (args.head.tag == ARRAY) {
   255             buf.append(((ArrayType)args.head).elemtype);
   256             buf.append("...");
   257         } else {
   258             buf.append(args.head);
   259         }
   260         return buf.toString();
   261     }
   263     /** Access methods.
   264      */
   265     public List<Type>        getTypeArguments()  { return List.nil(); }
   266     public Type              getEnclosingType() { return null; }
   267     public List<Type>        getParameterTypes() { return List.nil(); }
   268     public Type              getReturnType()     { return null; }
   269     public List<Type>        getThrownTypes()    { return List.nil(); }
   270     public Type              getUpperBound()     { return null; }
   271     public Type              getLowerBound()     { return null; }
   273     public void setThrown(List<Type> ts) {
   274         throw new AssertionError();
   275     }
   277     /** Navigation methods, these will work for classes, type variables,
   278      *  foralls, but will return null for arrays and methods.
   279      */
   281    /** Return all parameters of this type and all its outer types in order
   282     *  outer (first) to inner (last).
   283     */
   284     public List<Type> allparams() { return List.nil(); }
   286     /** Does this type contain "error" elements?
   287      */
   288     public boolean isErroneous() {
   289         return false;
   290     }
   292     public static boolean isErroneous(List<Type> ts) {
   293         for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
   294             if (l.head.isErroneous()) return true;
   295         return false;
   296     }
   298     /** Is this type parameterized?
   299      *  A class type is parameterized if it has some parameters.
   300      *  An array type is parameterized if its element type is parameterized.
   301      *  All other types are not parameterized.
   302      */
   303     public boolean isParameterized() {
   304         return false;
   305     }
   307     /** Is this type a raw type?
   308      *  A class type is a raw type if it misses some of its parameters.
   309      *  An array type is a raw type if its element type is raw.
   310      *  All other types are not raw.
   311      *  Type validation will ensure that the only raw types
   312      *  in a program are types that miss all their type variables.
   313      */
   314     public boolean isRaw() {
   315         return false;
   316     }
   318     public boolean isCompound() {
   319         return tsym.completer == null
   320             // Compound types can't have a completer.  Calling
   321             // flags() will complete the symbol causing the
   322             // compiler to load classes unnecessarily.  This led
   323             // to regression 6180021.
   324             && (tsym.flags() & COMPOUND) != 0;
   325     }
   327     public boolean isInterface() {
   328         return (tsym.flags() & INTERFACE) != 0;
   329     }
   331     public boolean isFinal() {
   332         return (tsym.flags() & FINAL) != 0;
   333     }
   335     public boolean isPrimitive() {
   336         return tag < VOID;
   337     }
   339     /**
   340      * Does this type contain occurrences of type t?
   341      */
   342     public boolean contains(Type t) {
   343         return t == this;
   344     }
   346     public static boolean contains(List<Type> ts, Type t) {
   347         for (List<Type> l = ts;
   348              l.tail != null /*inlined: l.nonEmpty()*/;
   349              l = l.tail)
   350             if (l.head.contains(t)) return true;
   351         return false;
   352     }
   354     /** Does this type contain an occurrence of some type in 'ts'?
   355      */
   356     public boolean containsAny(List<Type> ts) {
   357         for (Type t : ts)
   358             if (this.contains(t)) return true;
   359         return false;
   360     }
   362     public static boolean containsAny(List<Type> ts1, List<Type> ts2) {
   363         for (Type t : ts1)
   364             if (t.containsAny(ts2)) return true;
   365         return false;
   366     }
   368     public boolean isSuperBound() { return false; }
   369     public boolean isExtendsBound() { return false; }
   370     public boolean isUnbound() { return false; }
   371     public Type withTypeVar(Type t) { return this; }
   373     /** The underlying method type of this type.
   374      */
   375     public MethodType asMethodType() { throw new AssertionError(); }
   377     /** Complete loading all classes in this type.
   378      */
   379     public void complete() {}
   381     public Object clone() {
   382         try {
   383             return super.clone();
   384         } catch (CloneNotSupportedException e) {
   385             throw new AssertionError(e);
   386         }
   387     }
   389     public TypeSymbol asElement() {
   390         return tsym;
   391     }
   393     public TypeKind getKind() {
   394         switch (tag) {
   395         case BYTE:      return TypeKind.BYTE;
   396         case CHAR:      return TypeKind.CHAR;
   397         case SHORT:     return TypeKind.SHORT;
   398         case INT:       return TypeKind.INT;
   399         case LONG:      return TypeKind.LONG;
   400         case FLOAT:     return TypeKind.FLOAT;
   401         case DOUBLE:    return TypeKind.DOUBLE;
   402         case BOOLEAN:   return TypeKind.BOOLEAN;
   403         case VOID:      return TypeKind.VOID;
   404         case BOT:       return TypeKind.NULL;
   405         case NONE:      return TypeKind.NONE;
   406         default:        return TypeKind.OTHER;
   407         }
   408     }
   410     public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   411         if (isPrimitive())
   412             return v.visitPrimitive(this, p);
   413         else
   414             throw new AssertionError();
   415     }
   417     public static class WildcardType extends Type
   418             implements javax.lang.model.type.WildcardType {
   420         public Type type;
   421         public BoundKind kind;
   422         public TypeVar bound;
   424         @Override
   425         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   426             return v.visitWildcardType(this, s);
   427         }
   429         public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) {
   430             super(WILDCARD, tsym);
   431             this.type = Assert.checkNonNull(type);
   432             this.kind = kind;
   433         }
   434         public WildcardType(WildcardType t, TypeVar bound) {
   435             this(t.type, t.kind, t.tsym, bound);
   436         }
   438         public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, TypeVar bound) {
   439             this(type, kind, tsym);
   440             this.bound = bound;
   441         }
   443         public boolean contains(Type t) {
   444             return kind != UNBOUND && type.contains(t);
   445         }
   447         public boolean isSuperBound() {
   448             return kind == SUPER ||
   449                 kind == UNBOUND;
   450         }
   451         public boolean isExtendsBound() {
   452             return kind == EXTENDS ||
   453                 kind == UNBOUND;
   454         }
   455         public boolean isUnbound() {
   456             return kind == UNBOUND;
   457         }
   459         public Type withTypeVar(Type t) {
   460             //-System.err.println(this+".withTypeVar("+t+");");//DEBUG
   461             if (bound == t)
   462                 return this;
   463             bound = (TypeVar)t;
   464             return this;
   465         }
   467         boolean isPrintingBound = false;
   468         public String toString() {
   469             StringBuffer s = new StringBuffer();
   470             s.append(kind.toString());
   471             if (kind != UNBOUND)
   472                 s.append(type);
   473             if (moreInfo && bound != null && !isPrintingBound)
   474                 try {
   475                     isPrintingBound = true;
   476                     s.append("{:").append(bound.bound).append(":}");
   477                 } finally {
   478                     isPrintingBound = false;
   479                 }
   480             return s.toString();
   481         }
   483         public Type map(Mapping f) {
   484             //- System.err.println("   (" + this + ").map(" + f + ")");//DEBUG
   485             Type t = type;
   486             if (t != null)
   487                 t = f.apply(t);
   488             if (t == type)
   489                 return this;
   490             else
   491                 return new WildcardType(t, kind, tsym, bound);
   492         }
   494         public Type getExtendsBound() {
   495             if (kind == EXTENDS)
   496                 return type;
   497             else
   498                 return null;
   499         }
   501         public Type getSuperBound() {
   502             if (kind == SUPER)
   503                 return type;
   504             else
   505                 return null;
   506         }
   508         public TypeKind getKind() {
   509             return TypeKind.WILDCARD;
   510         }
   512         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   513             return v.visitWildcard(this, p);
   514         }
   515     }
   517     public static class ClassType extends Type implements DeclaredType {
   519         /** The enclosing type of this type. If this is the type of an inner
   520          *  class, outer_field refers to the type of its enclosing
   521          *  instance class, in all other cases it referes to noType.
   522          */
   523         private Type outer_field;
   525         /** The type parameters of this type (to be set once class is loaded).
   526          */
   527         public List<Type> typarams_field;
   529         /** A cache variable for the type parameters of this type,
   530          *  appended to all parameters of its enclosing class.
   531          *  @see #allparams
   532          */
   533         public List<Type> allparams_field;
   535         /** The supertype of this class (to be set once class is loaded).
   536          */
   537         public Type supertype_field;
   539         /** The interfaces of this class (to be set once class is loaded).
   540          */
   541         public List<Type> interfaces_field;
   543         public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) {
   544             super(CLASS, tsym);
   545             this.outer_field = outer;
   546             this.typarams_field = typarams;
   547             this.allparams_field = null;
   548             this.supertype_field = null;
   549             this.interfaces_field = null;
   550             /*
   551             // this can happen during error recovery
   552             assert
   553                 outer.isParameterized() ?
   554                 typarams.length() == tsym.type.typarams().length() :
   555                 outer.isRaw() ?
   556                 typarams.length() == 0 :
   557                 true;
   558             */
   559         }
   561         @Override
   562         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   563             return v.visitClassType(this, s);
   564         }
   566         public Type constType(Object constValue) {
   567             final Object value = constValue;
   568             return new ClassType(getEnclosingType(), typarams_field, tsym) {
   569                     @Override
   570                     public Object constValue() {
   571                         return value;
   572                     }
   573                     @Override
   574                     public Type baseType() {
   575                         return tsym.type;
   576                     }
   577                 };
   578         }
   580         /** The Java source which this type represents.
   581          */
   582         public String toString() {
   583             StringBuffer buf = new StringBuffer();
   584             if (getEnclosingType().tag == CLASS && tsym.owner.kind == TYP) {
   585                 buf.append(getEnclosingType().toString());
   586                 buf.append(".");
   587                 buf.append(className(tsym, false));
   588             } else {
   589                 buf.append(className(tsym, true));
   590             }
   591             if (getTypeArguments().nonEmpty()) {
   592                 buf.append('<');
   593                 buf.append(getTypeArguments().toString());
   594                 buf.append(">");
   595             }
   596             return buf.toString();
   597         }
   598 //where
   599             private String className(Symbol sym, boolean longform) {
   600                 if (sym.name.isEmpty() && (sym.flags() & COMPOUND) != 0) {
   601                     StringBuffer s = new StringBuffer(supertype_field.toString());
   602                     for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) {
   603                         s.append("&");
   604                         s.append(is.head.toString());
   605                     }
   606                     return s.toString();
   607                 } else if (sym.name.isEmpty()) {
   608                     String s;
   609                     ClassType norm = (ClassType) tsym.type;
   610                     if (norm == null) {
   611                         s = Log.getLocalizedString("anonymous.class", (Object)null);
   612                     } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
   613                         s = Log.getLocalizedString("anonymous.class",
   614                                                    norm.interfaces_field.head);
   615                     } else {
   616                         s = Log.getLocalizedString("anonymous.class",
   617                                                    norm.supertype_field);
   618                     }
   619                     if (moreInfo)
   620                         s += String.valueOf(sym.hashCode());
   621                     return s;
   622                 } else if (longform) {
   623                     return sym.getQualifiedName().toString();
   624                 } else {
   625                     return sym.name.toString();
   626                 }
   627             }
   629         public List<Type> getTypeArguments() {
   630             if (typarams_field == null) {
   631                 complete();
   632                 if (typarams_field == null)
   633                     typarams_field = List.nil();
   634             }
   635             return typarams_field;
   636         }
   638         public boolean hasErasedSupertypes() {
   639             return isRaw();
   640         }
   642         public Type getEnclosingType() {
   643             return outer_field;
   644         }
   646         public void setEnclosingType(Type outer) {
   647             outer_field = outer;
   648         }
   650         public List<Type> allparams() {
   651             if (allparams_field == null) {
   652                 allparams_field = getTypeArguments().prependList(getEnclosingType().allparams());
   653             }
   654             return allparams_field;
   655         }
   657         public boolean isErroneous() {
   658             return
   659                 getEnclosingType().isErroneous() ||
   660                 isErroneous(getTypeArguments()) ||
   661                 this != tsym.type && tsym.type.isErroneous();
   662         }
   664         public boolean isParameterized() {
   665             return allparams().tail != null;
   666             // optimization, was: allparams().nonEmpty();
   667         }
   669         /** A cache for the rank. */
   670         int rank_field = -1;
   672         /** A class type is raw if it misses some
   673          *  of its type parameter sections.
   674          *  After validation, this is equivalent to:
   675          *  allparams.isEmpty() && tsym.type.allparams.nonEmpty();
   676          */
   677         public boolean isRaw() {
   678             return
   679                 this != tsym.type && // necessary, but not sufficient condition
   680                 tsym.type.allparams().nonEmpty() &&
   681                 allparams().isEmpty();
   682         }
   684         public Type map(Mapping f) {
   685             Type outer = getEnclosingType();
   686             Type outer1 = f.apply(outer);
   687             List<Type> typarams = getTypeArguments();
   688             List<Type> typarams1 = map(typarams, f);
   689             if (outer1 == outer && typarams1 == typarams) return this;
   690             else return new ClassType(outer1, typarams1, tsym);
   691         }
   693         public boolean contains(Type elem) {
   694             return
   695                 elem == this
   696                 || (isParameterized()
   697                     && (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem)))
   698                 || (isCompound()
   699                     && (supertype_field.contains(elem) || contains(interfaces_field, elem)));
   700         }
   702         public void complete() {
   703             if (tsym.completer != null) tsym.complete();
   704         }
   706         public TypeKind getKind() {
   707             return TypeKind.DECLARED;
   708         }
   710         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   711             return v.visitDeclared(this, p);
   712         }
   713     }
   715     public static class ErasedClassType extends ClassType {
   716         public ErasedClassType(Type outer, TypeSymbol tsym) {
   717             super(outer, List.<Type>nil(), tsym);
   718         }
   720         @Override
   721         public boolean hasErasedSupertypes() {
   722             return true;
   723         }
   724     }
   726     public static class ArrayType extends Type
   727             implements javax.lang.model.type.ArrayType {
   729         public Type elemtype;
   731         public ArrayType(Type elemtype, TypeSymbol arrayClass) {
   732             super(ARRAY, arrayClass);
   733             this.elemtype = elemtype;
   734         }
   736         @Override
   737         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   738             return v.visitArrayType(this, s);
   739         }
   741         public String toString() {
   742             return elemtype + "[]";
   743         }
   745         public boolean equals(Object obj) {
   746             return
   747                 this == obj ||
   748                 (obj instanceof ArrayType &&
   749                  this.elemtype.equals(((ArrayType)obj).elemtype));
   750         }
   752         public int hashCode() {
   753             return (ARRAY << 5) + elemtype.hashCode();
   754         }
   756         public boolean isVarargs() {
   757             return false;
   758         }
   760         public List<Type> allparams() { return elemtype.allparams(); }
   762         public boolean isErroneous() {
   763             return elemtype.isErroneous();
   764         }
   766         public boolean isParameterized() {
   767             return elemtype.isParameterized();
   768         }
   770         public boolean isRaw() {
   771             return elemtype.isRaw();
   772         }
   774         public ArrayType makeVarargs() {
   775             return new ArrayType(elemtype, tsym) {
   776                 @Override
   777                 public boolean isVarargs() {
   778                     return true;
   779                 }
   780             };
   781         }
   783         public Type map(Mapping f) {
   784             Type elemtype1 = f.apply(elemtype);
   785             if (elemtype1 == elemtype) return this;
   786             else return new ArrayType(elemtype1, tsym);
   787         }
   789         public boolean contains(Type elem) {
   790             return elem == this || elemtype.contains(elem);
   791         }
   793         public void complete() {
   794             elemtype.complete();
   795         }
   797         public Type getComponentType() {
   798             return elemtype;
   799         }
   801         public TypeKind getKind() {
   802             return TypeKind.ARRAY;
   803         }
   805         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   806             return v.visitArray(this, p);
   807         }
   808     }
   810     public static class MethodType extends Type
   811                     implements Cloneable, ExecutableType {
   813         public List<Type> argtypes;
   814         public Type restype;
   815         public List<Type> thrown;
   817         public MethodType(List<Type> argtypes,
   818                           Type restype,
   819                           List<Type> thrown,
   820                           TypeSymbol methodClass) {
   821             super(METHOD, methodClass);
   822             this.argtypes = argtypes;
   823             this.restype = restype;
   824             this.thrown = thrown;
   825         }
   827         @Override
   828         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   829             return v.visitMethodType(this, s);
   830         }
   832         /** The Java source which this type represents.
   833          *
   834          *  XXX 06/09/99 iris This isn't correct Java syntax, but it probably
   835          *  should be.
   836          */
   837         public String toString() {
   838             return "(" + argtypes + ")" + restype;
   839         }
   841         public boolean equals(Object obj) {
   842             if (this == obj)
   843                 return true;
   844             if (!(obj instanceof MethodType))
   845                 return false;
   846             MethodType m = (MethodType)obj;
   847             List<Type> args1 = argtypes;
   848             List<Type> args2 = m.argtypes;
   849             while (!args1.isEmpty() && !args2.isEmpty()) {
   850                 if (!args1.head.equals(args2.head))
   851                     return false;
   852                 args1 = args1.tail;
   853                 args2 = args2.tail;
   854             }
   855             if (!args1.isEmpty() || !args2.isEmpty())
   856                 return false;
   857             return restype.equals(m.restype);
   858         }
   860         public int hashCode() {
   861             int h = METHOD;
   862             for (List<Type> thisargs = this.argtypes;
   863                  thisargs.tail != null; /*inlined: thisargs.nonEmpty()*/
   864                  thisargs = thisargs.tail)
   865                 h = (h << 5) + thisargs.head.hashCode();
   866             return (h << 5) + this.restype.hashCode();
   867         }
   869         public List<Type>        getParameterTypes() { return argtypes; }
   870         public Type              getReturnType()     { return restype; }
   871         public List<Type>        getThrownTypes()    { return thrown; }
   873         public void setThrown(List<Type> t) {
   874             thrown = t;
   875         }
   877         public boolean isErroneous() {
   878             return
   879                 isErroneous(argtypes) ||
   880                 restype != null && restype.isErroneous();
   881         }
   883         public Type map(Mapping f) {
   884             List<Type> argtypes1 = map(argtypes, f);
   885             Type restype1 = f.apply(restype);
   886             List<Type> thrown1 = map(thrown, f);
   887             if (argtypes1 == argtypes &&
   888                 restype1 == restype &&
   889                 thrown1 == thrown) return this;
   890             else return new MethodType(argtypes1, restype1, thrown1, tsym);
   891         }
   893         public boolean contains(Type elem) {
   894             return elem == this || contains(argtypes, elem) || restype.contains(elem);
   895         }
   897         public MethodType asMethodType() { return this; }
   899         public void complete() {
   900             for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail)
   901                 l.head.complete();
   902             restype.complete();
   903             for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
   904                 l.head.complete();
   905         }
   907         public List<TypeVar> getTypeVariables() {
   908             return List.nil();
   909         }
   911         public TypeSymbol asElement() {
   912             return null;
   913         }
   915         public TypeKind getKind() {
   916             return TypeKind.EXECUTABLE;
   917         }
   919         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   920             return v.visitExecutable(this, p);
   921         }
   922     }
   924     public static class PackageType extends Type implements NoType {
   926         PackageType(TypeSymbol tsym) {
   927             super(PACKAGE, tsym);
   928         }
   930         @Override
   931         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   932             return v.visitPackageType(this, s);
   933         }
   935         public String toString() {
   936             return tsym.getQualifiedName().toString();
   937         }
   939         public TypeKind getKind() {
   940             return TypeKind.PACKAGE;
   941         }
   943         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   944             return v.visitNoType(this, p);
   945         }
   946     }
   948     public static class TypeVar extends Type implements TypeVariable {
   950         /** The upper bound of this type variable; set from outside.
   951          *  Must be nonempty once it is set.
   952          *  For a bound, `bound' is the bound type itself.
   953          *  Multiple bounds are expressed as a single class type which has the
   954          *  individual bounds as superclass, respectively interfaces.
   955          *  The class type then has as `tsym' a compiler generated class `c',
   956          *  which has a flag COMPOUND and whose owner is the type variable
   957          *  itself. Furthermore, the erasure_field of the class
   958          *  points to the first class or interface bound.
   959          */
   960         public Type bound = null;
   962         /** The lower bound of this type variable.
   963          *  TypeVars don't normally have a lower bound, so it is normally set
   964          *  to syms.botType.
   965          *  Subtypes, such as CapturedType, may provide a different value.
   966          */
   967         public Type lower;
   969         public TypeVar(Name name, Symbol owner, Type lower) {
   970             super(TYPEVAR, null);
   971             tsym = new TypeSymbol(0, name, this, owner);
   972             this.lower = lower;
   973         }
   975         public TypeVar(TypeSymbol tsym, Type bound, Type lower) {
   976             super(TYPEVAR, tsym);
   977             this.bound = bound;
   978             this.lower = lower;
   979         }
   981         @Override
   982         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   983             return v.visitTypeVar(this, s);
   984         }
   986         @Override
   987         public Type getUpperBound() { return bound; }
   989         int rank_field = -1;
   991         @Override
   992         public Type getLowerBound() {
   993             return lower;
   994         }
   996         public TypeKind getKind() {
   997             return TypeKind.TYPEVAR;
   998         }
  1000         public boolean isCaptured() {
  1001             return false;
  1004         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1005             return v.visitTypeVariable(this, p);
  1009     /** A captured type variable comes from wildcards which can have
  1010      *  both upper and lower bound.  CapturedType extends TypeVar with
  1011      *  a lower bound.
  1012      */
  1013     public static class CapturedType extends TypeVar {
  1015         public WildcardType wildcard;
  1017         public CapturedType(Name name,
  1018                             Symbol owner,
  1019                             Type upper,
  1020                             Type lower,
  1021                             WildcardType wildcard) {
  1022             super(name, owner, lower);
  1023             this.lower = Assert.checkNonNull(lower);
  1024             this.bound = upper;
  1025             this.wildcard = wildcard;
  1028         @Override
  1029         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1030             return v.visitCapturedType(this, s);
  1033         @Override
  1034         public boolean isCaptured() {
  1035             return true;
  1038         @Override
  1039         public String toString() {
  1040             return "capture#"
  1041                 + (hashCode() & 0xFFFFFFFFL) % Printer.PRIME
  1042                 + " of "
  1043                 + wildcard;
  1047     public static abstract class DelegatedType extends Type {
  1048         public Type qtype;
  1049         public DelegatedType(int tag, Type qtype) {
  1050             super(tag, qtype.tsym);
  1051             this.qtype = qtype;
  1053         public String toString() { return qtype.toString(); }
  1054         public List<Type> getTypeArguments() { return qtype.getTypeArguments(); }
  1055         public Type getEnclosingType() { return qtype.getEnclosingType(); }
  1056         public List<Type> getParameterTypes() { return qtype.getParameterTypes(); }
  1057         public Type getReturnType() { return qtype.getReturnType(); }
  1058         public List<Type> getThrownTypes() { return qtype.getThrownTypes(); }
  1059         public List<Type> allparams() { return qtype.allparams(); }
  1060         public Type getUpperBound() { return qtype.getUpperBound(); }
  1061         public Object clone() { DelegatedType t = (DelegatedType)super.clone(); t.qtype = (Type)qtype.clone(); return t; }
  1062         public boolean isErroneous() { return qtype.isErroneous(); }
  1065     public static class ForAll extends DelegatedType
  1066             implements Cloneable, ExecutableType {
  1067         public List<Type> tvars;
  1069         public ForAll(List<Type> tvars, Type qtype) {
  1070             super(FORALL, qtype);
  1071             this.tvars = tvars;
  1074         @Override
  1075         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1076             return v.visitForAll(this, s);
  1079         public String toString() {
  1080             return "<" + tvars + ">" + qtype;
  1083         public List<Type> getTypeArguments()   { return tvars; }
  1085         public void setThrown(List<Type> t) {
  1086             qtype.setThrown(t);
  1089         public Object clone() {
  1090             ForAll result = (ForAll)super.clone();
  1091             result.qtype = (Type)result.qtype.clone();
  1092             return result;
  1095         public boolean isErroneous()  {
  1096             return qtype.isErroneous();
  1099         /**
  1100          * Replaces this ForAll's typevars with a set of concrete Java types
  1101          * and returns the instantiated generic type. Subclasses should override
  1102          * in order to check that the list of types is a valid instantiation
  1103          * of the ForAll's typevars.
  1105          * @param actuals list of actual types
  1106          * @param types types instance
  1107          * @return qtype where all occurrences of tvars are replaced
  1108          * by types in actuals
  1109          */
  1110         public Type inst(List<Type> actuals, Types types) {
  1111             return types.subst(qtype, tvars, actuals);
  1114         /**
  1115          * Kind of type-constraint derived during type inference
  1116          */
  1117         public enum ConstraintKind {
  1118             /**
  1119              * upper bound constraint (a type variable must be instantiated
  1120              * with a type T, where T is a subtype of all the types specified by
  1121              * its EXTENDS constraints).
  1122              */
  1123             EXTENDS,
  1124             /**
  1125              * lower bound constraint (a type variable must be instantiated
  1126              * with a type T, where T is a supertype of all the types specified by
  1127              * its SUPER constraints).
  1128              */
  1129             SUPER,
  1130             /**
  1131              * equality constraint (a type variable must be instantiated to the type
  1132              * specified by its EQUAL constraint.
  1133              */
  1134             EQUAL;
  1137         /**
  1138          * Get the type-constraints of a given kind for a given type-variable of
  1139          * this ForAll type. Subclasses should override in order to return more
  1140          * accurate sets of constraints.
  1142          * @param tv the type-variable for which the constraint is to be retrieved
  1143          * @param ck the constraint kind to be retrieved
  1144          * @return the list of types specified by the selected constraint
  1145          */
  1146         public List<Type> getConstraints(TypeVar tv, ConstraintKind ck) {
  1147             return List.nil();
  1150         public Type map(Mapping f) {
  1151             return f.apply(qtype);
  1154         public boolean contains(Type elem) {
  1155             return qtype.contains(elem);
  1158         public MethodType asMethodType() {
  1159             return qtype.asMethodType();
  1162         public void complete() {
  1163             for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) {
  1164                 ((TypeVar)l.head).bound.complete();
  1166             qtype.complete();
  1169         public List<TypeVar> getTypeVariables() {
  1170             return List.convert(TypeVar.class, getTypeArguments());
  1173         public TypeKind getKind() {
  1174             return TypeKind.EXECUTABLE;
  1177         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1178             return v.visitExecutable(this, p);
  1182     /** A class for instantiatable variables, for use during type
  1183      *  inference.
  1184      */
  1185     public static class UndetVar extends DelegatedType {
  1186         public List<Type> lobounds = List.nil();
  1187         public List<Type> hibounds = List.nil();
  1188         public Type inst = null;
  1190         @Override
  1191         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1192             return v.visitUndetVar(this, s);
  1195         public UndetVar(Type origin) {
  1196             super(UNDETVAR, origin);
  1199         public String toString() {
  1200             if (inst != null) return inst.toString();
  1201             else return qtype + "?";
  1204         public Type baseType() {
  1205             if (inst != null) return inst.baseType();
  1206             else return this;
  1210     /** Represents VOID or NONE.
  1211      */
  1212     static class JCNoType extends Type implements NoType {
  1213         public JCNoType(int tag) {
  1214             super(tag, null);
  1217         @Override
  1218         public TypeKind getKind() {
  1219             switch (tag) {
  1220             case VOID:  return TypeKind.VOID;
  1221             case NONE:  return TypeKind.NONE;
  1222             default:
  1223                 throw new AssertionError("Unexpected tag: " + tag);
  1227         @Override
  1228         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1229             return v.visitNoType(this, p);
  1233     static class BottomType extends Type implements NullType {
  1234         public BottomType() {
  1235             super(TypeTags.BOT, null);
  1238         @Override
  1239         public TypeKind getKind() {
  1240             return TypeKind.NULL;
  1243         @Override
  1244         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1245             return v.visitNull(this, p);
  1248         @Override
  1249         public Type constType(Object value) {
  1250             return this;
  1253         @Override
  1254         public String stringValue() {
  1255             return "null";
  1259     public static class ErrorType extends ClassType
  1260             implements javax.lang.model.type.ErrorType {
  1262         private Type originalType = null;
  1264         public ErrorType(Type originalType, TypeSymbol tsym) {
  1265             super(noType, List.<Type>nil(), null);
  1266             tag = ERROR;
  1267             this.tsym = tsym;
  1268             this.originalType = (originalType == null ? noType : originalType);
  1271         public ErrorType(ClassSymbol c, Type originalType) {
  1272             this(originalType, c);
  1273             c.type = this;
  1274             c.kind = ERR;
  1275             c.members_field = new Scope.ErrorScope(c);
  1278         public ErrorType(Name name, TypeSymbol container, Type originalType) {
  1279             this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container), originalType);
  1282         @Override
  1283         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1284             return v.visitErrorType(this, s);
  1287         public Type constType(Object constValue) { return this; }
  1288         public Type getEnclosingType()          { return this; }
  1289         public Type getReturnType()              { return this; }
  1290         public Type asSub(Symbol sym)            { return this; }
  1291         public Type map(Mapping f)               { return this; }
  1293         public boolean isGenType(Type t)         { return true; }
  1294         public boolean isErroneous()             { return true; }
  1295         public boolean isCompound()              { return false; }
  1296         public boolean isInterface()             { return false; }
  1298         public List<Type> allparams()            { return List.nil(); }
  1299         public List<Type> getTypeArguments()     { return List.nil(); }
  1301         public TypeKind getKind() {
  1302             return TypeKind.ERROR;
  1305         public Type getOriginalType() {
  1306             return originalType;
  1309         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1310             return v.visitError(this, p);
  1314     /**
  1315      * A visitor for types.  A visitor is used to implement operations
  1316      * (or relations) on types.  Most common operations on types are
  1317      * binary relations and this interface is designed for binary
  1318      * relations, that is, operations on the form
  1319      * Type&nbsp;&times;&nbsp;S&nbsp;&rarr;&nbsp;R.
  1320      * <!-- In plain text: Type x S -> R -->
  1322      * @param <R> the return type of the operation implemented by this
  1323      * visitor; use Void if no return type is needed.
  1324      * @param <S> the type of the second argument (the first being the
  1325      * type itself) of the operation implemented by this visitor; use
  1326      * Void if a second argument is not needed.
  1327      */
  1328     public interface Visitor<R,S> {
  1329         R visitClassType(ClassType t, S s);
  1330         R visitWildcardType(WildcardType t, S s);
  1331         R visitArrayType(ArrayType t, S s);
  1332         R visitMethodType(MethodType t, S s);
  1333         R visitPackageType(PackageType t, S s);
  1334         R visitTypeVar(TypeVar t, S s);
  1335         R visitCapturedType(CapturedType t, S s);
  1336         R visitForAll(ForAll t, S s);
  1337         R visitUndetVar(UndetVar t, S s);
  1338         R visitErrorType(ErrorType t, S s);
  1339         R visitType(Type t, S s);

mercurial