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

Mon, 24 Jan 2011 15:44:15 +0000

author
mcimadamore
date
Mon, 24 Jan 2011 15:44:15 +0000
changeset 828
19c900c703c6
parent 816
7c537f4298fb
child 880
0c24826853b2
permissions
-rw-r--r--

6943278: spurious error message for inference and type-variable with erroneous bound
Summary: type-inference should ignore erroneous bounds
Reviewed-by: jjg

     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 static List<Type> filter(List<Type> ts, Filter<Type> tf) {
   369         ListBuffer<Type> buf = ListBuffer.lb();
   370         for (Type t : ts) {
   371             if (tf.accepts(t)) {
   372                 buf.append(t);
   373             }
   374         }
   375         return buf.toList();
   376     }
   378     public boolean isSuperBound() { return false; }
   379     public boolean isExtendsBound() { return false; }
   380     public boolean isUnbound() { return false; }
   381     public Type withTypeVar(Type t) { return this; }
   383     /** The underlying method type of this type.
   384      */
   385     public MethodType asMethodType() { throw new AssertionError(); }
   387     /** Complete loading all classes in this type.
   388      */
   389     public void complete() {}
   391     public Object clone() {
   392         try {
   393             return super.clone();
   394         } catch (CloneNotSupportedException e) {
   395             throw new AssertionError(e);
   396         }
   397     }
   399     public TypeSymbol asElement() {
   400         return tsym;
   401     }
   403     public TypeKind getKind() {
   404         switch (tag) {
   405         case BYTE:      return TypeKind.BYTE;
   406         case CHAR:      return TypeKind.CHAR;
   407         case SHORT:     return TypeKind.SHORT;
   408         case INT:       return TypeKind.INT;
   409         case LONG:      return TypeKind.LONG;
   410         case FLOAT:     return TypeKind.FLOAT;
   411         case DOUBLE:    return TypeKind.DOUBLE;
   412         case BOOLEAN:   return TypeKind.BOOLEAN;
   413         case VOID:      return TypeKind.VOID;
   414         case BOT:       return TypeKind.NULL;
   415         case NONE:      return TypeKind.NONE;
   416         default:        return TypeKind.OTHER;
   417         }
   418     }
   420     public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   421         if (isPrimitive())
   422             return v.visitPrimitive(this, p);
   423         else
   424             throw new AssertionError();
   425     }
   427     public static class WildcardType extends Type
   428             implements javax.lang.model.type.WildcardType {
   430         public Type type;
   431         public BoundKind kind;
   432         public TypeVar bound;
   434         @Override
   435         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   436             return v.visitWildcardType(this, s);
   437         }
   439         public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) {
   440             super(WILDCARD, tsym);
   441             this.type = Assert.checkNonNull(type);
   442             this.kind = kind;
   443         }
   444         public WildcardType(WildcardType t, TypeVar bound) {
   445             this(t.type, t.kind, t.tsym, bound);
   446         }
   448         public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, TypeVar bound) {
   449             this(type, kind, tsym);
   450             this.bound = bound;
   451         }
   453         public boolean contains(Type t) {
   454             return kind != UNBOUND && type.contains(t);
   455         }
   457         public boolean isSuperBound() {
   458             return kind == SUPER ||
   459                 kind == UNBOUND;
   460         }
   461         public boolean isExtendsBound() {
   462             return kind == EXTENDS ||
   463                 kind == UNBOUND;
   464         }
   465         public boolean isUnbound() {
   466             return kind == UNBOUND;
   467         }
   469         public Type withTypeVar(Type t) {
   470             //-System.err.println(this+".withTypeVar("+t+");");//DEBUG
   471             if (bound == t)
   472                 return this;
   473             bound = (TypeVar)t;
   474             return this;
   475         }
   477         boolean isPrintingBound = false;
   478         public String toString() {
   479             StringBuffer s = new StringBuffer();
   480             s.append(kind.toString());
   481             if (kind != UNBOUND)
   482                 s.append(type);
   483             if (moreInfo && bound != null && !isPrintingBound)
   484                 try {
   485                     isPrintingBound = true;
   486                     s.append("{:").append(bound.bound).append(":}");
   487                 } finally {
   488                     isPrintingBound = false;
   489                 }
   490             return s.toString();
   491         }
   493         public Type map(Mapping f) {
   494             //- System.err.println("   (" + this + ").map(" + f + ")");//DEBUG
   495             Type t = type;
   496             if (t != null)
   497                 t = f.apply(t);
   498             if (t == type)
   499                 return this;
   500             else
   501                 return new WildcardType(t, kind, tsym, bound);
   502         }
   504         public Type getExtendsBound() {
   505             if (kind == EXTENDS)
   506                 return type;
   507             else
   508                 return null;
   509         }
   511         public Type getSuperBound() {
   512             if (kind == SUPER)
   513                 return type;
   514             else
   515                 return null;
   516         }
   518         public TypeKind getKind() {
   519             return TypeKind.WILDCARD;
   520         }
   522         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   523             return v.visitWildcard(this, p);
   524         }
   525     }
   527     public static class ClassType extends Type implements DeclaredType {
   529         /** The enclosing type of this type. If this is the type of an inner
   530          *  class, outer_field refers to the type of its enclosing
   531          *  instance class, in all other cases it referes to noType.
   532          */
   533         private Type outer_field;
   535         /** The type parameters of this type (to be set once class is loaded).
   536          */
   537         public List<Type> typarams_field;
   539         /** A cache variable for the type parameters of this type,
   540          *  appended to all parameters of its enclosing class.
   541          *  @see #allparams
   542          */
   543         public List<Type> allparams_field;
   545         /** The supertype of this class (to be set once class is loaded).
   546          */
   547         public Type supertype_field;
   549         /** The interfaces of this class (to be set once class is loaded).
   550          */
   551         public List<Type> interfaces_field;
   553         public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) {
   554             super(CLASS, tsym);
   555             this.outer_field = outer;
   556             this.typarams_field = typarams;
   557             this.allparams_field = null;
   558             this.supertype_field = null;
   559             this.interfaces_field = null;
   560             /*
   561             // this can happen during error recovery
   562             assert
   563                 outer.isParameterized() ?
   564                 typarams.length() == tsym.type.typarams().length() :
   565                 outer.isRaw() ?
   566                 typarams.length() == 0 :
   567                 true;
   568             */
   569         }
   571         @Override
   572         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   573             return v.visitClassType(this, s);
   574         }
   576         public Type constType(Object constValue) {
   577             final Object value = constValue;
   578             return new ClassType(getEnclosingType(), typarams_field, tsym) {
   579                     @Override
   580                     public Object constValue() {
   581                         return value;
   582                     }
   583                     @Override
   584                     public Type baseType() {
   585                         return tsym.type;
   586                     }
   587                 };
   588         }
   590         /** The Java source which this type represents.
   591          */
   592         public String toString() {
   593             StringBuffer buf = new StringBuffer();
   594             if (getEnclosingType().tag == CLASS && tsym.owner.kind == TYP) {
   595                 buf.append(getEnclosingType().toString());
   596                 buf.append(".");
   597                 buf.append(className(tsym, false));
   598             } else {
   599                 buf.append(className(tsym, true));
   600             }
   601             if (getTypeArguments().nonEmpty()) {
   602                 buf.append('<');
   603                 buf.append(getTypeArguments().toString());
   604                 buf.append(">");
   605             }
   606             return buf.toString();
   607         }
   608 //where
   609             private String className(Symbol sym, boolean longform) {
   610                 if (sym.name.isEmpty() && (sym.flags() & COMPOUND) != 0) {
   611                     StringBuffer s = new StringBuffer(supertype_field.toString());
   612                     for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) {
   613                         s.append("&");
   614                         s.append(is.head.toString());
   615                     }
   616                     return s.toString();
   617                 } else if (sym.name.isEmpty()) {
   618                     String s;
   619                     ClassType norm = (ClassType) tsym.type;
   620                     if (norm == null) {
   621                         s = Log.getLocalizedString("anonymous.class", (Object)null);
   622                     } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
   623                         s = Log.getLocalizedString("anonymous.class",
   624                                                    norm.interfaces_field.head);
   625                     } else {
   626                         s = Log.getLocalizedString("anonymous.class",
   627                                                    norm.supertype_field);
   628                     }
   629                     if (moreInfo)
   630                         s += String.valueOf(sym.hashCode());
   631                     return s;
   632                 } else if (longform) {
   633                     return sym.getQualifiedName().toString();
   634                 } else {
   635                     return sym.name.toString();
   636                 }
   637             }
   639         public List<Type> getTypeArguments() {
   640             if (typarams_field == null) {
   641                 complete();
   642                 if (typarams_field == null)
   643                     typarams_field = List.nil();
   644             }
   645             return typarams_field;
   646         }
   648         public boolean hasErasedSupertypes() {
   649             return isRaw();
   650         }
   652         public Type getEnclosingType() {
   653             return outer_field;
   654         }
   656         public void setEnclosingType(Type outer) {
   657             outer_field = outer;
   658         }
   660         public List<Type> allparams() {
   661             if (allparams_field == null) {
   662                 allparams_field = getTypeArguments().prependList(getEnclosingType().allparams());
   663             }
   664             return allparams_field;
   665         }
   667         public boolean isErroneous() {
   668             return
   669                 getEnclosingType().isErroneous() ||
   670                 isErroneous(getTypeArguments()) ||
   671                 this != tsym.type && tsym.type.isErroneous();
   672         }
   674         public boolean isParameterized() {
   675             return allparams().tail != null;
   676             // optimization, was: allparams().nonEmpty();
   677         }
   679         /** A cache for the rank. */
   680         int rank_field = -1;
   682         /** A class type is raw if it misses some
   683          *  of its type parameter sections.
   684          *  After validation, this is equivalent to:
   685          *  allparams.isEmpty() && tsym.type.allparams.nonEmpty();
   686          */
   687         public boolean isRaw() {
   688             return
   689                 this != tsym.type && // necessary, but not sufficient condition
   690                 tsym.type.allparams().nonEmpty() &&
   691                 allparams().isEmpty();
   692         }
   694         public Type map(Mapping f) {
   695             Type outer = getEnclosingType();
   696             Type outer1 = f.apply(outer);
   697             List<Type> typarams = getTypeArguments();
   698             List<Type> typarams1 = map(typarams, f);
   699             if (outer1 == outer && typarams1 == typarams) return this;
   700             else return new ClassType(outer1, typarams1, tsym);
   701         }
   703         public boolean contains(Type elem) {
   704             return
   705                 elem == this
   706                 || (isParameterized()
   707                     && (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem)))
   708                 || (isCompound()
   709                     && (supertype_field.contains(elem) || contains(interfaces_field, elem)));
   710         }
   712         public void complete() {
   713             if (tsym.completer != null) tsym.complete();
   714         }
   716         public TypeKind getKind() {
   717             return TypeKind.DECLARED;
   718         }
   720         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   721             return v.visitDeclared(this, p);
   722         }
   723     }
   725     public static class ErasedClassType extends ClassType {
   726         public ErasedClassType(Type outer, TypeSymbol tsym) {
   727             super(outer, List.<Type>nil(), tsym);
   728         }
   730         @Override
   731         public boolean hasErasedSupertypes() {
   732             return true;
   733         }
   734     }
   736     public static class ArrayType extends Type
   737             implements javax.lang.model.type.ArrayType {
   739         public Type elemtype;
   741         public ArrayType(Type elemtype, TypeSymbol arrayClass) {
   742             super(ARRAY, arrayClass);
   743             this.elemtype = elemtype;
   744         }
   746         @Override
   747         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   748             return v.visitArrayType(this, s);
   749         }
   751         public String toString() {
   752             return elemtype + "[]";
   753         }
   755         public boolean equals(Object obj) {
   756             return
   757                 this == obj ||
   758                 (obj instanceof ArrayType &&
   759                  this.elemtype.equals(((ArrayType)obj).elemtype));
   760         }
   762         public int hashCode() {
   763             return (ARRAY << 5) + elemtype.hashCode();
   764         }
   766         public boolean isVarargs() {
   767             return false;
   768         }
   770         public List<Type> allparams() { return elemtype.allparams(); }
   772         public boolean isErroneous() {
   773             return elemtype.isErroneous();
   774         }
   776         public boolean isParameterized() {
   777             return elemtype.isParameterized();
   778         }
   780         public boolean isRaw() {
   781             return elemtype.isRaw();
   782         }
   784         public ArrayType makeVarargs() {
   785             return new ArrayType(elemtype, tsym) {
   786                 @Override
   787                 public boolean isVarargs() {
   788                     return true;
   789                 }
   790             };
   791         }
   793         public Type map(Mapping f) {
   794             Type elemtype1 = f.apply(elemtype);
   795             if (elemtype1 == elemtype) return this;
   796             else return new ArrayType(elemtype1, tsym);
   797         }
   799         public boolean contains(Type elem) {
   800             return elem == this || elemtype.contains(elem);
   801         }
   803         public void complete() {
   804             elemtype.complete();
   805         }
   807         public Type getComponentType() {
   808             return elemtype;
   809         }
   811         public TypeKind getKind() {
   812             return TypeKind.ARRAY;
   813         }
   815         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   816             return v.visitArray(this, p);
   817         }
   818     }
   820     public static class MethodType extends Type
   821                     implements Cloneable, ExecutableType {
   823         public List<Type> argtypes;
   824         public Type restype;
   825         public List<Type> thrown;
   827         public MethodType(List<Type> argtypes,
   828                           Type restype,
   829                           List<Type> thrown,
   830                           TypeSymbol methodClass) {
   831             super(METHOD, methodClass);
   832             this.argtypes = argtypes;
   833             this.restype = restype;
   834             this.thrown = thrown;
   835         }
   837         @Override
   838         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   839             return v.visitMethodType(this, s);
   840         }
   842         /** The Java source which this type represents.
   843          *
   844          *  XXX 06/09/99 iris This isn't correct Java syntax, but it probably
   845          *  should be.
   846          */
   847         public String toString() {
   848             return "(" + argtypes + ")" + restype;
   849         }
   851         public boolean equals(Object obj) {
   852             if (this == obj)
   853                 return true;
   854             if (!(obj instanceof MethodType))
   855                 return false;
   856             MethodType m = (MethodType)obj;
   857             List<Type> args1 = argtypes;
   858             List<Type> args2 = m.argtypes;
   859             while (!args1.isEmpty() && !args2.isEmpty()) {
   860                 if (!args1.head.equals(args2.head))
   861                     return false;
   862                 args1 = args1.tail;
   863                 args2 = args2.tail;
   864             }
   865             if (!args1.isEmpty() || !args2.isEmpty())
   866                 return false;
   867             return restype.equals(m.restype);
   868         }
   870         public int hashCode() {
   871             int h = METHOD;
   872             for (List<Type> thisargs = this.argtypes;
   873                  thisargs.tail != null; /*inlined: thisargs.nonEmpty()*/
   874                  thisargs = thisargs.tail)
   875                 h = (h << 5) + thisargs.head.hashCode();
   876             return (h << 5) + this.restype.hashCode();
   877         }
   879         public List<Type>        getParameterTypes() { return argtypes; }
   880         public Type              getReturnType()     { return restype; }
   881         public List<Type>        getThrownTypes()    { return thrown; }
   883         public void setThrown(List<Type> t) {
   884             thrown = t;
   885         }
   887         public boolean isErroneous() {
   888             return
   889                 isErroneous(argtypes) ||
   890                 restype != null && restype.isErroneous();
   891         }
   893         public Type map(Mapping f) {
   894             List<Type> argtypes1 = map(argtypes, f);
   895             Type restype1 = f.apply(restype);
   896             List<Type> thrown1 = map(thrown, f);
   897             if (argtypes1 == argtypes &&
   898                 restype1 == restype &&
   899                 thrown1 == thrown) return this;
   900             else return new MethodType(argtypes1, restype1, thrown1, tsym);
   901         }
   903         public boolean contains(Type elem) {
   904             return elem == this || contains(argtypes, elem) || restype.contains(elem);
   905         }
   907         public MethodType asMethodType() { return this; }
   909         public void complete() {
   910             for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail)
   911                 l.head.complete();
   912             restype.complete();
   913             for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
   914                 l.head.complete();
   915         }
   917         public List<TypeVar> getTypeVariables() {
   918             return List.nil();
   919         }
   921         public TypeSymbol asElement() {
   922             return null;
   923         }
   925         public TypeKind getKind() {
   926             return TypeKind.EXECUTABLE;
   927         }
   929         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   930             return v.visitExecutable(this, p);
   931         }
   932     }
   934     public static class PackageType extends Type implements NoType {
   936         PackageType(TypeSymbol tsym) {
   937             super(PACKAGE, tsym);
   938         }
   940         @Override
   941         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   942             return v.visitPackageType(this, s);
   943         }
   945         public String toString() {
   946             return tsym.getQualifiedName().toString();
   947         }
   949         public TypeKind getKind() {
   950             return TypeKind.PACKAGE;
   951         }
   953         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   954             return v.visitNoType(this, p);
   955         }
   956     }
   958     public static class TypeVar extends Type implements TypeVariable {
   960         /** The upper bound of this type variable; set from outside.
   961          *  Must be nonempty once it is set.
   962          *  For a bound, `bound' is the bound type itself.
   963          *  Multiple bounds are expressed as a single class type which has the
   964          *  individual bounds as superclass, respectively interfaces.
   965          *  The class type then has as `tsym' a compiler generated class `c',
   966          *  which has a flag COMPOUND and whose owner is the type variable
   967          *  itself. Furthermore, the erasure_field of the class
   968          *  points to the first class or interface bound.
   969          */
   970         public Type bound = null;
   972         /** The lower bound of this type variable.
   973          *  TypeVars don't normally have a lower bound, so it is normally set
   974          *  to syms.botType.
   975          *  Subtypes, such as CapturedType, may provide a different value.
   976          */
   977         public Type lower;
   979         public TypeVar(Name name, Symbol owner, Type lower) {
   980             super(TYPEVAR, null);
   981             tsym = new TypeSymbol(0, name, this, owner);
   982             this.lower = lower;
   983         }
   985         public TypeVar(TypeSymbol tsym, Type bound, Type lower) {
   986             super(TYPEVAR, tsym);
   987             this.bound = bound;
   988             this.lower = lower;
   989         }
   991         @Override
   992         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   993             return v.visitTypeVar(this, s);
   994         }
   996         @Override
   997         public Type getUpperBound() { return bound; }
   999         int rank_field = -1;
  1001         @Override
  1002         public Type getLowerBound() {
  1003             return lower;
  1006         public TypeKind getKind() {
  1007             return TypeKind.TYPEVAR;
  1010         public boolean isCaptured() {
  1011             return false;
  1014         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1015             return v.visitTypeVariable(this, p);
  1019     /** A captured type variable comes from wildcards which can have
  1020      *  both upper and lower bound.  CapturedType extends TypeVar with
  1021      *  a lower bound.
  1022      */
  1023     public static class CapturedType extends TypeVar {
  1025         public WildcardType wildcard;
  1027         public CapturedType(Name name,
  1028                             Symbol owner,
  1029                             Type upper,
  1030                             Type lower,
  1031                             WildcardType wildcard) {
  1032             super(name, owner, lower);
  1033             this.lower = Assert.checkNonNull(lower);
  1034             this.bound = upper;
  1035             this.wildcard = wildcard;
  1038         @Override
  1039         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1040             return v.visitCapturedType(this, s);
  1043         @Override
  1044         public boolean isCaptured() {
  1045             return true;
  1048         @Override
  1049         public String toString() {
  1050             return "capture#"
  1051                 + (hashCode() & 0xFFFFFFFFL) % Printer.PRIME
  1052                 + " of "
  1053                 + wildcard;
  1057     public static abstract class DelegatedType extends Type {
  1058         public Type qtype;
  1059         public DelegatedType(int tag, Type qtype) {
  1060             super(tag, qtype.tsym);
  1061             this.qtype = qtype;
  1063         public String toString() { return qtype.toString(); }
  1064         public List<Type> getTypeArguments() { return qtype.getTypeArguments(); }
  1065         public Type getEnclosingType() { return qtype.getEnclosingType(); }
  1066         public List<Type> getParameterTypes() { return qtype.getParameterTypes(); }
  1067         public Type getReturnType() { return qtype.getReturnType(); }
  1068         public List<Type> getThrownTypes() { return qtype.getThrownTypes(); }
  1069         public List<Type> allparams() { return qtype.allparams(); }
  1070         public Type getUpperBound() { return qtype.getUpperBound(); }
  1071         public Object clone() { DelegatedType t = (DelegatedType)super.clone(); t.qtype = (Type)qtype.clone(); return t; }
  1072         public boolean isErroneous() { return qtype.isErroneous(); }
  1075     public static class ForAll extends DelegatedType
  1076             implements Cloneable, ExecutableType {
  1077         public List<Type> tvars;
  1079         public ForAll(List<Type> tvars, Type qtype) {
  1080             super(FORALL, qtype);
  1081             this.tvars = tvars;
  1084         @Override
  1085         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1086             return v.visitForAll(this, s);
  1089         public String toString() {
  1090             return "<" + tvars + ">" + qtype;
  1093         public List<Type> getTypeArguments()   { return tvars; }
  1095         public void setThrown(List<Type> t) {
  1096             qtype.setThrown(t);
  1099         public Object clone() {
  1100             ForAll result = (ForAll)super.clone();
  1101             result.qtype = (Type)result.qtype.clone();
  1102             return result;
  1105         public boolean isErroneous()  {
  1106             return qtype.isErroneous();
  1109         /**
  1110          * Replaces this ForAll's typevars with a set of concrete Java types
  1111          * and returns the instantiated generic type. Subclasses should override
  1112          * in order to check that the list of types is a valid instantiation
  1113          * of the ForAll's typevars.
  1115          * @param actuals list of actual types
  1116          * @param types types instance
  1117          * @return qtype where all occurrences of tvars are replaced
  1118          * by types in actuals
  1119          */
  1120         public Type inst(List<Type> actuals, Types types) {
  1121             return types.subst(qtype, tvars, actuals);
  1124         /**
  1125          * Kind of type-constraint derived during type inference
  1126          */
  1127         public enum ConstraintKind {
  1128             /**
  1129              * upper bound constraint (a type variable must be instantiated
  1130              * with a type T, where T is a subtype of all the types specified by
  1131              * its EXTENDS constraints).
  1132              */
  1133             EXTENDS,
  1134             /**
  1135              * lower bound constraint (a type variable must be instantiated
  1136              * with a type T, where T is a supertype of all the types specified by
  1137              * its SUPER constraints).
  1138              */
  1139             SUPER,
  1140             /**
  1141              * equality constraint (a type variable must be instantiated to the type
  1142              * specified by its EQUAL constraint.
  1143              */
  1144             EQUAL;
  1147         /**
  1148          * Get the type-constraints of a given kind for a given type-variable of
  1149          * this ForAll type. Subclasses should override in order to return more
  1150          * accurate sets of constraints.
  1152          * @param tv the type-variable for which the constraint is to be retrieved
  1153          * @param ck the constraint kind to be retrieved
  1154          * @return the list of types specified by the selected constraint
  1155          */
  1156         public List<Type> getConstraints(TypeVar tv, ConstraintKind ck) {
  1157             return List.nil();
  1160         public Type map(Mapping f) {
  1161             return f.apply(qtype);
  1164         public boolean contains(Type elem) {
  1165             return qtype.contains(elem);
  1168         public MethodType asMethodType() {
  1169             return qtype.asMethodType();
  1172         public void complete() {
  1173             for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) {
  1174                 ((TypeVar)l.head).bound.complete();
  1176             qtype.complete();
  1179         public List<TypeVar> getTypeVariables() {
  1180             return List.convert(TypeVar.class, getTypeArguments());
  1183         public TypeKind getKind() {
  1184             return TypeKind.EXECUTABLE;
  1187         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1188             return v.visitExecutable(this, p);
  1192     /** A class for instantiatable variables, for use during type
  1193      *  inference.
  1194      */
  1195     public static class UndetVar extends DelegatedType {
  1196         public List<Type> lobounds = List.nil();
  1197         public List<Type> hibounds = List.nil();
  1198         public Type inst = null;
  1200         @Override
  1201         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1202             return v.visitUndetVar(this, s);
  1205         public UndetVar(Type origin) {
  1206             super(UNDETVAR, origin);
  1209         public String toString() {
  1210             if (inst != null) return inst.toString();
  1211             else return qtype + "?";
  1214         public Type baseType() {
  1215             if (inst != null) return inst.baseType();
  1216             else return this;
  1220     /** Represents VOID or NONE.
  1221      */
  1222     static class JCNoType extends Type implements NoType {
  1223         public JCNoType(int tag) {
  1224             super(tag, null);
  1227         @Override
  1228         public TypeKind getKind() {
  1229             switch (tag) {
  1230             case VOID:  return TypeKind.VOID;
  1231             case NONE:  return TypeKind.NONE;
  1232             default:
  1233                 throw new AssertionError("Unexpected tag: " + tag);
  1237         @Override
  1238         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1239             return v.visitNoType(this, p);
  1243     static class BottomType extends Type implements NullType {
  1244         public BottomType() {
  1245             super(TypeTags.BOT, null);
  1248         @Override
  1249         public TypeKind getKind() {
  1250             return TypeKind.NULL;
  1253         @Override
  1254         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1255             return v.visitNull(this, p);
  1258         @Override
  1259         public Type constType(Object value) {
  1260             return this;
  1263         @Override
  1264         public String stringValue() {
  1265             return "null";
  1269     public static class ErrorType extends ClassType
  1270             implements javax.lang.model.type.ErrorType {
  1272         private Type originalType = null;
  1274         public ErrorType(Type originalType, TypeSymbol tsym) {
  1275             super(noType, List.<Type>nil(), null);
  1276             tag = ERROR;
  1277             this.tsym = tsym;
  1278             this.originalType = (originalType == null ? noType : originalType);
  1281         public ErrorType(ClassSymbol c, Type originalType) {
  1282             this(originalType, c);
  1283             c.type = this;
  1284             c.kind = ERR;
  1285             c.members_field = new Scope.ErrorScope(c);
  1288         public ErrorType(Name name, TypeSymbol container, Type originalType) {
  1289             this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container), originalType);
  1292         @Override
  1293         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1294             return v.visitErrorType(this, s);
  1297         public Type constType(Object constValue) { return this; }
  1298         public Type getEnclosingType()          { return this; }
  1299         public Type getReturnType()              { return this; }
  1300         public Type asSub(Symbol sym)            { return this; }
  1301         public Type map(Mapping f)               { return this; }
  1303         public boolean isGenType(Type t)         { return true; }
  1304         public boolean isErroneous()             { return true; }
  1305         public boolean isCompound()              { return false; }
  1306         public boolean isInterface()             { return false; }
  1308         public List<Type> allparams()            { return List.nil(); }
  1309         public List<Type> getTypeArguments()     { return List.nil(); }
  1311         public TypeKind getKind() {
  1312             return TypeKind.ERROR;
  1315         public Type getOriginalType() {
  1316             return originalType;
  1319         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1320             return v.visitError(this, p);
  1324     /**
  1325      * A visitor for types.  A visitor is used to implement operations
  1326      * (or relations) on types.  Most common operations on types are
  1327      * binary relations and this interface is designed for binary
  1328      * relations, that is, operations on the form
  1329      * Type&nbsp;&times;&nbsp;S&nbsp;&rarr;&nbsp;R.
  1330      * <!-- In plain text: Type x S -> R -->
  1332      * @param <R> the return type of the operation implemented by this
  1333      * visitor; use Void if no return type is needed.
  1334      * @param <S> the type of the second argument (the first being the
  1335      * type itself) of the operation implemented by this visitor; use
  1336      * Void if a second argument is not needed.
  1337      */
  1338     public interface Visitor<R,S> {
  1339         R visitClassType(ClassType t, S s);
  1340         R visitWildcardType(WildcardType t, S s);
  1341         R visitArrayType(ArrayType t, S s);
  1342         R visitMethodType(MethodType t, S s);
  1343         R visitPackageType(PackageType t, S s);
  1344         R visitTypeVar(TypeVar t, S s);
  1345         R visitCapturedType(CapturedType t, S s);
  1346         R visitForAll(ForAll t, S s);
  1347         R visitUndetVar(UndetVar t, S s);
  1348         R visitErrorType(ErrorType t, S s);
  1349         R visitType(Type t, S s);

mercurial