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

Fri, 29 Aug 2008 11:10:12 -0700

author
jjg
date
Fri, 29 Aug 2008 11:10:12 -0700
changeset 104
5e89c4ca637c
parent 79
36df13bde238
child 110
91eea580fbe9
permissions
-rw-r--r--

6597471: unused imports in javax.tools.JavaCompiler
6597531: unused imports and unused private const. in com.sun.tools.javac.Server.java
Reviewed-by: mcimadamore
Contributed-by: davide.angelocola@gmail.com

     1 /*
     2  * Copyright 1999-2008 Sun Microsystems, Inc.  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.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any 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 API supported by Sun Microsystems.  If
    60  *  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 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         assert constValue() != null;
   206         if (tag == BOOLEAN)
   207             return ((Integer) constValue()).intValue() == 0 ? "false" : "true";
   208         else if (tag == CHAR)
   209             return String.valueOf((char) ((Integer) constValue()).intValue());
   210         else
   211             return constValue().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         StringBuffer buf = new StringBuffer();
   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 isPrimitive() {
   332         return tag < VOID;
   333     }
   335     /**
   336      * Does this type contain occurrences of type t?
   337      */
   338     public boolean contains(Type t) {
   339         return t == this;
   340     }
   342     public static boolean contains(List<Type> ts, Type t) {
   343         for (List<Type> l = ts;
   344              l.tail != null /*inlined: l.nonEmpty()*/;
   345              l = l.tail)
   346             if (l.head.contains(t)) return true;
   347         return false;
   348     }
   350     /** Does this type contain an occurrence of some type in `elems'?
   351      */
   352     public boolean containsSome(List<Type> ts) {
   353         for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
   354             if (this.contains(ts.head)) return true;
   355         return false;
   356     }
   358     public boolean isSuperBound() { return false; }
   359     public boolean isExtendsBound() { return false; }
   360     public boolean isUnbound() { return false; }
   361     public Type withTypeVar(Type t) { return this; }
   363     public static List<Type> removeBounds(List<Type> ts) {
   364         ListBuffer<Type> result = new ListBuffer<Type>();
   365         for(;ts.nonEmpty(); ts = ts.tail) {
   366             result.append(ts.head.removeBounds());
   367         }
   368         return result.toList();
   369     }
   370     public Type removeBounds() {
   371         return this;
   372     }
   374     /** The underlying method type of this type.
   375      */
   376     public MethodType asMethodType() { throw new AssertionError(); }
   378     /** Complete loading all classes in this type.
   379      */
   380     public void complete() {}
   382     public Object clone() {
   383         try {
   384             return super.clone();
   385         } catch (CloneNotSupportedException e) {
   386             throw new AssertionError(e);
   387         }
   388     }
   390     public TypeSymbol asElement() {
   391         return tsym;
   392     }
   394     public TypeKind getKind() {
   395         switch (tag) {
   396         case BYTE:      return TypeKind.BYTE;
   397         case CHAR:      return TypeKind.CHAR;
   398         case SHORT:     return TypeKind.SHORT;
   399         case INT:       return TypeKind.INT;
   400         case LONG:      return TypeKind.LONG;
   401         case FLOAT:     return TypeKind.FLOAT;
   402         case DOUBLE:    return TypeKind.DOUBLE;
   403         case BOOLEAN:   return TypeKind.BOOLEAN;
   404         case VOID:      return TypeKind.VOID;
   405         case BOT:       return TypeKind.NULL;
   406         case NONE:      return TypeKind.NONE;
   407         default:        return TypeKind.OTHER;
   408         }
   409     }
   411     public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   412         if (isPrimitive())
   413             return v.visitPrimitive(this, p);
   414         else
   415             throw new AssertionError();
   416     }
   418     public static class WildcardType extends Type
   419             implements javax.lang.model.type.WildcardType {
   421         public Type type;
   422         public BoundKind kind;
   423         public TypeVar bound;
   425         @Override
   426         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   427             return v.visitWildcardType(this, s);
   428         }
   430         public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) {
   431             super(WILDCARD, tsym);
   432             assert(type != null);
   433             this.kind = kind;
   434             this.type = type;
   435         }
   436         public WildcardType(WildcardType t, TypeVar bound) {
   437             this(t.type, t.kind, t.tsym, bound);
   438         }
   440         public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, TypeVar bound) {
   441             this(type, kind, tsym);
   442             this.bound = bound;
   443         }
   445         public boolean isSuperBound() {
   446             return kind == SUPER ||
   447                 kind == UNBOUND;
   448         }
   449         public boolean isExtendsBound() {
   450             return kind == EXTENDS ||
   451                 kind == UNBOUND;
   452         }
   453         public boolean isUnbound() {
   454             return kind == UNBOUND;
   455         }
   457         public Type withTypeVar(Type t) {
   458             //-System.err.println(this+".withTypeVar("+t+");");//DEBUG
   459             if (bound == t)
   460                 return this;
   461             bound = (TypeVar)t;
   462             return this;
   463         }
   465         boolean isPrintingBound = false;
   466         public String toString() {
   467             StringBuffer s = new StringBuffer();
   468             s.append(kind.toString());
   469             if (kind != UNBOUND)
   470                 s.append(type);
   471             if (moreInfo && bound != null && !isPrintingBound)
   472                 try {
   473                     isPrintingBound = true;
   474                     s.append("{:").append(bound.bound).append(":}");
   475                 } finally {
   476                     isPrintingBound = false;
   477                 }
   478             return s.toString();
   479         }
   481         public Type map(Mapping f) {
   482             //- System.err.println("   (" + this + ").map(" + f + ")");//DEBUG
   483             Type t = type;
   484             if (t != null)
   485                 t = f.apply(t);
   486             if (t == type)
   487                 return this;
   488             else
   489                 return new WildcardType(t, kind, tsym, bound);
   490         }
   492         public Type removeBounds() {
   493             return isUnbound() ? this : type;
   494         }
   496         public Type getExtendsBound() {
   497             if (kind == EXTENDS)
   498                 return type;
   499             else
   500                 return null;
   501         }
   503         public Type getSuperBound() {
   504             if (kind == SUPER)
   505                 return type;
   506             else
   507                 return null;
   508         }
   510         public TypeKind getKind() {
   511             return TypeKind.WILDCARD;
   512         }
   514         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   515             return v.visitWildcard(this, p);
   516         }
   517     }
   519     public static class ClassType extends Type implements DeclaredType {
   521         /** The enclosing type of this type. If this is the type of an inner
   522          *  class, outer_field refers to the type of its enclosing
   523          *  instance class, in all other cases it referes to noType.
   524          */
   525         private Type outer_field;
   527         /** The type parameters of this type (to be set once class is loaded).
   528          */
   529         public List<Type> typarams_field;
   531         /** A cache variable for the type parameters of this type,
   532          *  appended to all parameters of its enclosing class.
   533          *  @see #allparams
   534          */
   535         public List<Type> allparams_field;
   537         /** The supertype of this class (to be set once class is loaded).
   538          */
   539         public Type supertype_field;
   541         /** The interfaces of this class (to be set once class is loaded).
   542          */
   543         public List<Type> interfaces_field;
   545         public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) {
   546             super(CLASS, tsym);
   547             this.outer_field = outer;
   548             this.typarams_field = typarams;
   549             this.allparams_field = null;
   550             this.supertype_field = null;
   551             this.interfaces_field = null;
   552             /*
   553             // this can happen during error recovery
   554             assert
   555                 outer.isParameterized() ?
   556                 typarams.length() == tsym.type.typarams().length() :
   557                 outer.isRaw() ?
   558                 typarams.length() == 0 :
   559                 true;
   560             */
   561         }
   563         @Override
   564         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   565             return v.visitClassType(this, s);
   566         }
   568         public Type constType(Object constValue) {
   569             final Object value = constValue;
   570             return new ClassType(getEnclosingType(), typarams_field, tsym) {
   571                     @Override
   572                     public Object constValue() {
   573                         return value;
   574                     }
   575                     @Override
   576                     public Type baseType() {
   577                         return tsym.type;
   578                     }
   579                 };
   580         }
   582         /** The Java source which this type represents.
   583          */
   584         public String toString() {
   585             StringBuffer buf = new StringBuffer();
   586             if (getEnclosingType().tag == CLASS && tsym.owner.kind == TYP) {
   587                 buf.append(getEnclosingType().toString());
   588                 buf.append(".");
   589                 buf.append(className(tsym, false));
   590             } else {
   591                 buf.append(className(tsym, true));
   592             }
   593             if (getTypeArguments().nonEmpty()) {
   594                 buf.append('<');
   595                 buf.append(getTypeArguments().toString());
   596                 buf.append(">");
   597             }
   598             return buf.toString();
   599         }
   600 //where
   601             private String className(Symbol sym, boolean longform) {
   602                 if (sym.name.len == 0 && (sym.flags() & COMPOUND) != 0) {
   603                     StringBuffer s = new StringBuffer(supertype_field.toString());
   604                     for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) {
   605                         s.append("&");
   606                         s.append(is.head.toString());
   607                     }
   608                     return s.toString();
   609                 } else if (sym.name.len == 0) {
   610                     String s;
   611                     ClassType norm = (ClassType) tsym.type;
   612                     if (norm == null) {
   613                         s = Log.getLocalizedString("anonymous.class", (Object)null);
   614                     } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
   615                         s = Log.getLocalizedString("anonymous.class",
   616                                                    norm.interfaces_field.head);
   617                     } else {
   618                         s = Log.getLocalizedString("anonymous.class",
   619                                                    norm.supertype_field);
   620                     }
   621                     if (moreInfo)
   622                         s += String.valueOf(sym.hashCode());
   623                     return s;
   624                 } else if (longform) {
   625                     return sym.getQualifiedName().toString();
   626                 } else {
   627                     return sym.name.toString();
   628                 }
   629             }
   631         public List<Type> getTypeArguments() {
   632             if (typarams_field == null) {
   633                 complete();
   634                 if (typarams_field == null)
   635                     typarams_field = List.nil();
   636             }
   637             return typarams_field;
   638         }
   640         public boolean hasErasedSupertypes() {
   641             return isRaw();
   642         }
   644         public Type getEnclosingType() {
   645             return outer_field;
   646         }
   648         public void setEnclosingType(Type outer) {
   649             outer_field = outer;
   650         }
   652         public List<Type> allparams() {
   653             if (allparams_field == null) {
   654                 allparams_field = getTypeArguments().prependList(getEnclosingType().allparams());
   655             }
   656             return allparams_field;
   657         }
   659         public boolean isErroneous() {
   660             return
   661                 getEnclosingType().isErroneous() ||
   662                 isErroneous(getTypeArguments()) ||
   663                 this != tsym.type && tsym.type.isErroneous();
   664         }
   666         public boolean isParameterized() {
   667             return allparams().tail != null;
   668             // optimization, was: allparams().nonEmpty();
   669         }
   671         /** A cache for the rank. */
   672         int rank_field = -1;
   674         /** A class type is raw if it misses some
   675          *  of its type parameter sections.
   676          *  After validation, this is equivalent to:
   677          *  allparams.isEmpty() && tsym.type.allparams.nonEmpty();
   678          */
   679         public boolean isRaw() {
   680             return
   681                 this != tsym.type && // necessary, but not sufficient condition
   682                 tsym.type.allparams().nonEmpty() &&
   683                 allparams().isEmpty();
   684         }
   686         public Type map(Mapping f) {
   687             Type outer = getEnclosingType();
   688             Type outer1 = f.apply(outer);
   689             List<Type> typarams = getTypeArguments();
   690             List<Type> typarams1 = map(typarams, f);
   691             if (outer1 == outer && typarams1 == typarams) return this;
   692             else return new ClassType(outer1, typarams1, tsym);
   693         }
   695         public boolean contains(Type elem) {
   696             return
   697                 elem == this
   698                 || (isParameterized()
   699                     && (getEnclosingType().contains(elem) || contains(getTypeArguments(), 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 List<Type> allparams() { return elemtype.allparams(); }
   758         public boolean isErroneous() {
   759             return elemtype.isErroneous();
   760         }
   762         public boolean isParameterized() {
   763             return elemtype.isParameterized();
   764         }
   766         public boolean isRaw() {
   767             return elemtype.isRaw();
   768         }
   770         public Type map(Mapping f) {
   771             Type elemtype1 = f.apply(elemtype);
   772             if (elemtype1 == elemtype) return this;
   773             else return new ArrayType(elemtype1, tsym);
   774         }
   776         public boolean contains(Type elem) {
   777             return elem == this || elemtype.contains(elem);
   778         }
   780         public void complete() {
   781             elemtype.complete();
   782         }
   784         public Type getComponentType() {
   785             return elemtype;
   786         }
   788         public TypeKind getKind() {
   789             return TypeKind.ARRAY;
   790         }
   792         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   793             return v.visitArray(this, p);
   794         }
   795     }
   797     public static class MethodType extends Type
   798                     implements Cloneable, ExecutableType {
   800         public List<Type> argtypes;
   801         public Type restype;
   802         public List<Type> thrown;
   804         public MethodType(List<Type> argtypes,
   805                           Type restype,
   806                           List<Type> thrown,
   807                           TypeSymbol methodClass) {
   808             super(METHOD, methodClass);
   809             this.argtypes = argtypes;
   810             this.restype = restype;
   811             this.thrown = thrown;
   812         }
   814         @Override
   815         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   816             return v.visitMethodType(this, s);
   817         }
   819         /** The Java source which this type represents.
   820          *
   821          *  XXX 06/09/99 iris This isn't correct Java syntax, but it probably
   822          *  should be.
   823          */
   824         public String toString() {
   825             return "(" + argtypes + ")" + restype;
   826         }
   828         public boolean equals(Object obj) {
   829             if (this == obj)
   830                 return true;
   831             if (!(obj instanceof MethodType))
   832                 return false;
   833             MethodType m = (MethodType)obj;
   834             List<Type> args1 = argtypes;
   835             List<Type> args2 = m.argtypes;
   836             while (!args1.isEmpty() && !args2.isEmpty()) {
   837                 if (!args1.head.equals(args2.head))
   838                     return false;
   839                 args1 = args1.tail;
   840                 args2 = args2.tail;
   841             }
   842             if (!args1.isEmpty() || !args2.isEmpty())
   843                 return false;
   844             return restype.equals(m.restype);
   845         }
   847         public int hashCode() {
   848             int h = METHOD;
   849             for (List<Type> thisargs = this.argtypes;
   850                  thisargs.tail != null; /*inlined: thisargs.nonEmpty()*/
   851                  thisargs = thisargs.tail)
   852                 h = (h << 5) + thisargs.head.hashCode();
   853             return (h << 5) + this.restype.hashCode();
   854         }
   856         public List<Type>        getParameterTypes() { return argtypes; }
   857         public Type              getReturnType()     { return restype; }
   858         public List<Type>        getThrownTypes()    { return thrown; }
   860         public void setThrown(List<Type> t) {
   861             thrown = t;
   862         }
   864         public boolean isErroneous() {
   865             return
   866                 isErroneous(argtypes) ||
   867                 restype != null && restype.isErroneous();
   868         }
   870         public Type map(Mapping f) {
   871             List<Type> argtypes1 = map(argtypes, f);
   872             Type restype1 = f.apply(restype);
   873             List<Type> thrown1 = map(thrown, f);
   874             if (argtypes1 == argtypes &&
   875                 restype1 == restype &&
   876                 thrown1 == thrown) return this;
   877             else return new MethodType(argtypes1, restype1, thrown1, tsym);
   878         }
   880         public boolean contains(Type elem) {
   881             return elem == this || contains(argtypes, elem) || restype.contains(elem);
   882         }
   884         public MethodType asMethodType() { return this; }
   886         public void complete() {
   887             for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail)
   888                 l.head.complete();
   889             restype.complete();
   890             for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
   891                 l.head.complete();
   892         }
   894         public List<TypeVar> getTypeVariables() {
   895             return List.nil();
   896         }
   898         public TypeSymbol asElement() {
   899             return null;
   900         }
   902         public TypeKind getKind() {
   903             return TypeKind.EXECUTABLE;
   904         }
   906         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   907             return v.visitExecutable(this, p);
   908         }
   909     }
   911     public static class PackageType extends Type implements NoType {
   913         PackageType(TypeSymbol tsym) {
   914             super(PACKAGE, tsym);
   915         }
   917         @Override
   918         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   919             return v.visitPackageType(this, s);
   920         }
   922         public String toString() {
   923             return tsym.getQualifiedName().toString();
   924         }
   926         public TypeKind getKind() {
   927             return TypeKind.PACKAGE;
   928         }
   930         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   931             return v.visitNoType(this, p);
   932         }
   933     }
   935     public static class TypeVar extends Type implements TypeVariable {
   937         /** The bound of this type variable; set from outside.
   938          *  Must be nonempty once it is set.
   939          *  For a bound, `bound' is the bound type itself.
   940          *  Multiple bounds are expressed as a single class type which has the
   941          *  individual bounds as superclass, respectively interfaces.
   942          *  The class type then has as `tsym' a compiler generated class `c',
   943          *  which has a flag COMPOUND and whose owner is the type variable
   944          *  itself. Furthermore, the erasure_field of the class
   945          *  points to the first class or interface bound.
   946          */
   947         public Type bound = null;
   948         public Type lower;
   950         public TypeVar(Name name, Symbol owner, Type lower) {
   951             super(TYPEVAR, null);
   952             tsym = new TypeSymbol(0, name, this, owner);
   953             this.lower = lower;
   954         }
   956         public TypeVar(TypeSymbol tsym, Type bound, Type lower) {
   957             super(TYPEVAR, tsym);
   958             this.bound = bound;
   959             this.lower = lower;
   960         }
   962         @Override
   963         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   964             return v.visitTypeVar(this, s);
   965         }
   967         public Type getUpperBound() { return bound; }
   969         int rank_field = -1;
   971         public Type getLowerBound() {
   972             return lower;
   973         }
   975         public TypeKind getKind() {
   976             return TypeKind.TYPEVAR;
   977         }
   979         public boolean isCaptured() {
   980             return false;
   981         }
   983         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   984             return v.visitTypeVariable(this, p);
   985         }
   986     }
   988     /** A captured type variable comes from wildcards which can have
   989      *  both upper and lower bound.  CapturedType extends TypeVar with
   990      *  a lower bound.
   991      */
   992     public static class CapturedType extends TypeVar {
   994         public Type lower;
   995         public WildcardType wildcard;
   997         public CapturedType(Name name,
   998                             Symbol owner,
   999                             Type upper,
  1000                             Type lower,
  1001                             WildcardType wildcard) {
  1002             super(name, owner, lower);
  1003             assert lower != null;
  1004             this.bound = upper;
  1005             this.lower = lower;
  1006             this.wildcard = wildcard;
  1009         @Override
  1010         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1011             return v.visitCapturedType(this, s);
  1014         public Type getLowerBound() {
  1015             return lower;
  1018         @Override
  1019         public boolean isCaptured() {
  1020             return true;
  1023         @Override
  1024         public String toString() {
  1025             return "capture#"
  1026                 + (hashCode() & 0xFFFFFFFFL) % PRIME
  1027                 + " of "
  1028                 + wildcard;
  1030         static final int PRIME = 997;  // largest prime less than 1000
  1033     public static abstract class DelegatedType extends Type {
  1034         public Type qtype;
  1035         public DelegatedType(int tag, Type qtype) {
  1036             super(tag, qtype.tsym);
  1037             this.qtype = qtype;
  1039         public String toString() { return qtype.toString(); }
  1040         public List<Type> getTypeArguments() { return qtype.getTypeArguments(); }
  1041         public Type getEnclosingType() { return qtype.getEnclosingType(); }
  1042         public List<Type> getParameterTypes() { return qtype.getParameterTypes(); }
  1043         public Type getReturnType() { return qtype.getReturnType(); }
  1044         public List<Type> getThrownTypes() { return qtype.getThrownTypes(); }
  1045         public List<Type> allparams() { return qtype.allparams(); }
  1046         public Type getUpperBound() { return qtype.getUpperBound(); }
  1047         public Object clone() { DelegatedType t = (DelegatedType)super.clone(); t.qtype = (Type)qtype.clone(); return t; }
  1048         public boolean isErroneous() { return qtype.isErroneous(); }
  1051     public static class ForAll extends DelegatedType
  1052             implements Cloneable, ExecutableType {
  1053         public List<Type> tvars;
  1055         public ForAll(List<Type> tvars, Type qtype) {
  1056             super(FORALL, qtype);
  1057             this.tvars = tvars;
  1060         @Override
  1061         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1062             return v.visitForAll(this, s);
  1065         public String toString() {
  1066             return "<" + tvars + ">" + qtype;
  1069         public List<Type> getTypeArguments()   { return tvars; }
  1071         public void setThrown(List<Type> t) {
  1072             qtype.setThrown(t);
  1075         public Object clone() {
  1076             ForAll result = (ForAll)super.clone();
  1077             result.qtype = (Type)result.qtype.clone();
  1078             return result;
  1081         public boolean isErroneous()  {
  1082             return qtype.isErroneous();
  1085         public Type map(Mapping f) {
  1086             return f.apply(qtype);
  1089         public boolean contains(Type elem) {
  1090             return qtype.contains(elem);
  1093         public MethodType asMethodType() {
  1094             return qtype.asMethodType();
  1097         public void complete() {
  1098             for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) {
  1099                 ((TypeVar)l.head).bound.complete();
  1101             qtype.complete();
  1104         public List<TypeVar> getTypeVariables() {
  1105             return List.convert(TypeVar.class, getTypeArguments());
  1108         public TypeKind getKind() {
  1109             return TypeKind.EXECUTABLE;
  1112         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1113             return v.visitExecutable(this, p);
  1117     /** A class for instantiatable variables, for use during type
  1118      *  inference.
  1119      */
  1120     public static class UndetVar extends DelegatedType {
  1121         public List<Type> lobounds = List.nil();
  1122         public List<Type> hibounds = List.nil();
  1123         public Type inst = null;
  1125         @Override
  1126         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1127             return v.visitUndetVar(this, s);
  1130         public UndetVar(Type origin) {
  1131             super(UNDETVAR, origin);
  1134         public String toString() {
  1135             if (inst != null) return inst.toString();
  1136             else return qtype + "?";
  1139         public Type baseType() {
  1140             if (inst != null) return inst.baseType();
  1141             else return this;
  1145     /** Represents VOID or NONE.
  1146      */
  1147     static class JCNoType extends Type implements NoType {
  1148         public JCNoType(int tag) {
  1149             super(tag, null);
  1152         @Override
  1153         public TypeKind getKind() {
  1154             switch (tag) {
  1155             case VOID:  return TypeKind.VOID;
  1156             case NONE:  return TypeKind.NONE;
  1157             default:
  1158                 throw new AssertionError("Unexpected tag: " + tag);
  1162         @Override
  1163         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1164             return v.visitNoType(this, p);
  1168     static class BottomType extends Type implements NullType {
  1169         public BottomType() {
  1170             super(TypeTags.BOT, null);
  1173         @Override
  1174         public TypeKind getKind() {
  1175             return TypeKind.NULL;
  1178         @Override
  1179         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1180             return v.visitNull(this, p);
  1183         @Override
  1184         public Type constType(Object value) {
  1185             return this;
  1188         @Override
  1189         public String stringValue() {
  1190             return "null";
  1194     public static class ErrorType extends ClassType
  1195             implements javax.lang.model.type.ErrorType {
  1197         public ErrorType() {
  1198             super(noType, List.<Type>nil(), null);
  1199             tag = ERROR;
  1202         public ErrorType(ClassSymbol c) {
  1203             this();
  1204             tsym = c;
  1205             c.type = this;
  1206             c.kind = ERR;
  1207             c.members_field = new Scope.ErrorScope(c);
  1210         public ErrorType(Name name, TypeSymbol container) {
  1211             this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container));
  1214         @Override
  1215         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1216             return v.visitErrorType(this, s);
  1219         public Type constType(Object constValue) { return this; }
  1220         public Type getEnclosingType()          { return this; }
  1221         public Type getReturnType()              { return this; }
  1222         public Type asSub(Symbol sym)            { return this; }
  1223         public Type map(Mapping f)               { return this; }
  1225         public boolean isGenType(Type t)         { return true; }
  1226         public boolean isErroneous()             { return true; }
  1227         public boolean isCompound()              { return false; }
  1228         public boolean isInterface()             { return false; }
  1230         public List<Type> allparams()            { return List.nil(); }
  1231         public List<Type> getTypeArguments()     { return List.nil(); }
  1233         public TypeKind getKind() {
  1234             return TypeKind.ERROR;
  1237         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1238             return v.visitError(this, p);
  1242     /**
  1243      * A visitor for types.  A visitor is used to implement operations
  1244      * (or relations) on types.  Most common operations on types are
  1245      * binary relations and this interface is designed for binary
  1246      * relations, that is, operations on the form
  1247      * Type&nbsp;&times;&nbsp;S&nbsp;&rarr;&nbsp;R.
  1248      * <!-- In plain text: Type x S -> R -->
  1250      * @param <R> the return type of the operation implemented by this
  1251      * visitor; use Void if no return type is needed.
  1252      * @param <S> the type of the second argument (the first being the
  1253      * type itself) of the operation implemented by this visitor; use
  1254      * Void if a second argument is not needed.
  1255      */
  1256     public interface Visitor<R,S> {
  1257         R visitClassType(ClassType t, S s);
  1258         R visitWildcardType(WildcardType t, S s);
  1259         R visitArrayType(ArrayType t, S s);
  1260         R visitMethodType(MethodType t, S s);
  1261         R visitPackageType(PackageType t, S s);
  1262         R visitTypeVar(TypeVar t, S s);
  1263         R visitCapturedType(CapturedType t, S s);
  1264         R visitForAll(ForAll t, S s);
  1265         R visitUndetVar(UndetVar t, S s);
  1266         R visitErrorType(ErrorType t, S s);
  1267         R visitType(Type t, S s);

mercurial