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

Wed, 11 Apr 2012 10:50:11 +0100

author
mcimadamore
date
Wed, 11 Apr 2012 10:50:11 +0100
changeset 1251
6f0ed5a89c25
parent 988
7ae6c0fd479b
child 1268
af6a4c24f4e3
permissions
-rw-r--r--

7154127: Inference cleanup: remove bound check analysis from visitors in Types.java
Summary: Remove bound checking rules from recursive subtype visitors in Types.java and replace with centralized bound-checking logic
Reviewed-by: jjg, dlsmith

     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 java.util.Collections;
    30 import com.sun.tools.javac.util.*;
    31 import com.sun.tools.javac.code.Symbol.*;
    33 import javax.lang.model.type.*;
    35 import static com.sun.tools.javac.code.Flags.*;
    36 import static com.sun.tools.javac.code.Kinds.*;
    37 import static com.sun.tools.javac.code.BoundKind.*;
    38 import static com.sun.tools.javac.code.TypeTags.*;
    40 /** This class represents Java types. The class itself defines the behavior of
    41  *  the following types:
    42  *  <pre>
    43  *  base types (tags: BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, BOOLEAN),
    44  *  type `void' (tag: VOID),
    45  *  the bottom type (tag: BOT),
    46  *  the missing type (tag: NONE).
    47  *  </pre>
    48  *  <p>The behavior of the following types is defined in subclasses, which are
    49  *  all static inner classes of this class:
    50  *  <pre>
    51  *  class types (tag: CLASS, class: ClassType),
    52  *  array types (tag: ARRAY, class: ArrayType),
    53  *  method types (tag: METHOD, class: MethodType),
    54  *  package types (tag: PACKAGE, class: PackageType),
    55  *  type variables (tag: TYPEVAR, class: TypeVar),
    56  *  type arguments (tag: WILDCARD, class: WildcardType),
    57  *  polymorphic types (tag: FORALL, class: ForAll),
    58  *  the error type (tag: ERROR, class: ErrorType).
    59  *  </pre>
    60  *
    61  *  <p><b>This is NOT part of any supported API.
    62  *  If you write code that depends on this, you do so at your own risk.
    63  *  This code and its internal interfaces are subject to change or
    64  *  deletion without notice.</b>
    65  *
    66  *  @see TypeTags
    67  */
    68 public class Type implements PrimitiveType {
    70     /** Constant type: no type at all. */
    71     public static final JCNoType noType = new JCNoType(NONE);
    73     /** If this switch is turned on, the names of type variables
    74      *  and anonymous classes are printed with hashcodes appended.
    75      */
    76     public static boolean moreInfo = false;
    78     /** The tag of this type.
    79      *
    80      *  @see TypeTags
    81      */
    82     public int tag;
    84     /** The defining class / interface / package / type variable
    85      */
    86     public TypeSymbol tsym;
    88     /**
    89      * The constant value of this type, null if this type does not
    90      * have a constant value attribute. Only primitive types and
    91      * strings (ClassType) can have a constant value attribute.
    92      * @return the constant value attribute of this type
    93      */
    94     public Object constValue() {
    95         return null;
    96     }
    98     /**
    99      * Get the representation of this type used for modelling purposes.
   100      * By default, this is itself. For ErrorType, a different value
   101      * may be provided,
   102      */
   103     public Type getModelType() {
   104         return this;
   105     }
   107     public static List<Type> getModelTypes(List<Type> ts) {
   108         ListBuffer<Type> lb = new ListBuffer<Type>();
   109         for (Type t: ts)
   110             lb.append(t.getModelType());
   111         return lb.toList();
   112     }
   114     public <R,S> R accept(Type.Visitor<R,S> v, S s) { return v.visitType(this, s); }
   116     /** Define a type given its tag and type symbol
   117      */
   118     public Type(int tag, TypeSymbol tsym) {
   119         this.tag = tag;
   120         this.tsym = tsym;
   121     }
   123     /** An abstract class for mappings from types to types
   124      */
   125     public static abstract class Mapping {
   126         private String name;
   127         public Mapping(String name) {
   128             this.name = name;
   129         }
   130         public abstract Type apply(Type t);
   131         public String toString() {
   132             return name;
   133         }
   134     }
   136     /** map a type function over all immediate descendants of this type
   137      */
   138     public Type map(Mapping f) {
   139         return this;
   140     }
   142     /** map a type function over a list of types
   143      */
   144     public static List<Type> map(List<Type> ts, Mapping f) {
   145         if (ts.nonEmpty()) {
   146             List<Type> tail1 = map(ts.tail, f);
   147             Type t = f.apply(ts.head);
   148             if (tail1 != ts.tail || t != ts.head)
   149                 return tail1.prepend(t);
   150         }
   151         return ts;
   152     }
   154     /** Define a constant type, of the same kind as this type
   155      *  and with given constant value
   156      */
   157     public Type constType(Object constValue) {
   158         final Object value = constValue;
   159         Assert.check(tag <= BOOLEAN);
   160         return new Type(tag, tsym) {
   161                 @Override
   162                 public Object constValue() {
   163                     return value;
   164                 }
   165                 @Override
   166                 public Type baseType() {
   167                     return tsym.type;
   168                 }
   169             };
   170     }
   172     /**
   173      * If this is a constant type, return its underlying type.
   174      * Otherwise, return the type itself.
   175      */
   176     public Type baseType() {
   177         return this;
   178     }
   180     /** Return the base types of a list of types.
   181      */
   182     public static List<Type> baseTypes(List<Type> ts) {
   183         if (ts.nonEmpty()) {
   184             Type t = ts.head.baseType();
   185             List<Type> baseTypes = baseTypes(ts.tail);
   186             if (t != ts.head || baseTypes != ts.tail)
   187                 return baseTypes.prepend(t);
   188         }
   189         return ts;
   190     }
   192     /** The Java source which this type represents.
   193      */
   194     public String toString() {
   195         String s = (tsym == null || tsym.name == null)
   196             ? "<none>"
   197             : tsym.name.toString();
   198         if (moreInfo && tag == TYPEVAR) s = s + hashCode();
   199         return s;
   200     }
   202     /**
   203      * The Java source which this type list represents.  A List is
   204      * represented as a comma-spearated listing of the elements in
   205      * that list.
   206      */
   207     public static String toString(List<Type> ts) {
   208         if (ts.isEmpty()) {
   209             return "";
   210         } else {
   211             StringBuilder buf = new StringBuilder();
   212             buf.append(ts.head.toString());
   213             for (List<Type> l = ts.tail; l.nonEmpty(); l = l.tail)
   214                 buf.append(",").append(l.head.toString());
   215             return buf.toString();
   216         }
   217     }
   219     /**
   220      * The constant value of this type, converted to String
   221      */
   222     public String stringValue() {
   223         Object cv = Assert.checkNonNull(constValue());
   224         if (tag == BOOLEAN)
   225             return ((Integer) cv).intValue() == 0 ? "false" : "true";
   226         else if (tag == CHAR)
   227             return String.valueOf((char) ((Integer) cv).intValue());
   228         else
   229             return cv.toString();
   230     }
   232     /**
   233      * This method is analogous to isSameType, but weaker, since we
   234      * never complete classes. Where isSameType would complete a
   235      * class, equals assumes that the two types are different.
   236      */
   237     public boolean equals(Object t) {
   238         return super.equals(t);
   239     }
   241     public int hashCode() {
   242         return super.hashCode();
   243     }
   245     /** Is this a constant type whose value is false?
   246      */
   247     public boolean isFalse() {
   248         return
   249             tag == BOOLEAN &&
   250             constValue() != null &&
   251             ((Integer)constValue()).intValue() == 0;
   252     }
   254     /** Is this a constant type whose value is true?
   255      */
   256     public boolean isTrue() {
   257         return
   258             tag == BOOLEAN &&
   259             constValue() != null &&
   260             ((Integer)constValue()).intValue() != 0;
   261     }
   263     public String argtypes(boolean varargs) {
   264         List<Type> args = getParameterTypes();
   265         if (!varargs) return args.toString();
   266         StringBuilder buf = new StringBuilder();
   267         while (args.tail.nonEmpty()) {
   268             buf.append(args.head);
   269             args = args.tail;
   270             buf.append(',');
   271         }
   272         if (args.head.tag == ARRAY) {
   273             buf.append(((ArrayType)args.head).elemtype);
   274             buf.append("...");
   275         } else {
   276             buf.append(args.head);
   277         }
   278         return buf.toString();
   279     }
   281     /** Access methods.
   282      */
   283     public List<Type>        getTypeArguments()  { return List.nil(); }
   284     public Type              getEnclosingType() { return null; }
   285     public List<Type>        getParameterTypes() { return List.nil(); }
   286     public Type              getReturnType()     { return null; }
   287     public List<Type>        getThrownTypes()    { return List.nil(); }
   288     public Type              getUpperBound()     { return null; }
   289     public Type              getLowerBound()     { return null; }
   291     /** Navigation methods, these will work for classes, type variables,
   292      *  foralls, but will return null for arrays and methods.
   293      */
   295    /** Return all parameters of this type and all its outer types in order
   296     *  outer (first) to inner (last).
   297     */
   298     public List<Type> allparams() { return List.nil(); }
   300     /** Does this type contain "error" elements?
   301      */
   302     public boolean isErroneous() {
   303         return false;
   304     }
   306     public static boolean isErroneous(List<Type> ts) {
   307         for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
   308             if (l.head.isErroneous()) return true;
   309         return false;
   310     }
   312     /** Is this type parameterized?
   313      *  A class type is parameterized if it has some parameters.
   314      *  An array type is parameterized if its element type is parameterized.
   315      *  All other types are not parameterized.
   316      */
   317     public boolean isParameterized() {
   318         return false;
   319     }
   321     /** Is this type a raw type?
   322      *  A class type is a raw type if it misses some of its parameters.
   323      *  An array type is a raw type if its element type is raw.
   324      *  All other types are not raw.
   325      *  Type validation will ensure that the only raw types
   326      *  in a program are types that miss all their type variables.
   327      */
   328     public boolean isRaw() {
   329         return false;
   330     }
   332     public boolean isCompound() {
   333         return tsym.completer == null
   334             // Compound types can't have a completer.  Calling
   335             // flags() will complete the symbol causing the
   336             // compiler to load classes unnecessarily.  This led
   337             // to regression 6180021.
   338             && (tsym.flags() & COMPOUND) != 0;
   339     }
   341     public boolean isInterface() {
   342         return (tsym.flags() & INTERFACE) != 0;
   343     }
   345     public boolean isFinal() {
   346         return (tsym.flags() & FINAL) != 0;
   347     }
   349     public boolean isPrimitive() {
   350         return tag < VOID;
   351     }
   353     /**
   354      * Does this type contain occurrences of type t?
   355      */
   356     public boolean contains(Type t) {
   357         return t == this;
   358     }
   360     public static boolean contains(List<Type> ts, Type t) {
   361         for (List<Type> l = ts;
   362              l.tail != null /*inlined: l.nonEmpty()*/;
   363              l = l.tail)
   364             if (l.head.contains(t)) return true;
   365         return false;
   366     }
   368     /** Does this type contain an occurrence of some type in 'ts'?
   369      */
   370     public boolean containsAny(List<Type> ts) {
   371         for (Type t : ts)
   372             if (this.contains(t)) return true;
   373         return false;
   374     }
   376     public static boolean containsAny(List<Type> ts1, List<Type> ts2) {
   377         for (Type t : ts1)
   378             if (t.containsAny(ts2)) return true;
   379         return false;
   380     }
   382     public static List<Type> filter(List<Type> ts, Filter<Type> tf) {
   383         ListBuffer<Type> buf = ListBuffer.lb();
   384         for (Type t : ts) {
   385             if (tf.accepts(t)) {
   386                 buf.append(t);
   387             }
   388         }
   389         return buf.toList();
   390     }
   392     public boolean isSuperBound() { return false; }
   393     public boolean isExtendsBound() { return false; }
   394     public boolean isUnbound() { return false; }
   395     public Type withTypeVar(Type t) { return this; }
   397     /** The underlying method type of this type.
   398      */
   399     public MethodType asMethodType() { throw new AssertionError(); }
   401     /** Complete loading all classes in this type.
   402      */
   403     public void complete() {}
   405     public TypeSymbol asElement() {
   406         return tsym;
   407     }
   409     public TypeKind getKind() {
   410         switch (tag) {
   411         case BYTE:      return TypeKind.BYTE;
   412         case CHAR:      return TypeKind.CHAR;
   413         case SHORT:     return TypeKind.SHORT;
   414         case INT:       return TypeKind.INT;
   415         case LONG:      return TypeKind.LONG;
   416         case FLOAT:     return TypeKind.FLOAT;
   417         case DOUBLE:    return TypeKind.DOUBLE;
   418         case BOOLEAN:   return TypeKind.BOOLEAN;
   419         case VOID:      return TypeKind.VOID;
   420         case BOT:       return TypeKind.NULL;
   421         case NONE:      return TypeKind.NONE;
   422         default:        return TypeKind.OTHER;
   423         }
   424     }
   426     public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   427         if (isPrimitive())
   428             return v.visitPrimitive(this, p);
   429         else
   430             throw new AssertionError();
   431     }
   433     public static class WildcardType extends Type
   434             implements javax.lang.model.type.WildcardType {
   436         public Type type;
   437         public BoundKind kind;
   438         public TypeVar bound;
   440         @Override
   441         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   442             return v.visitWildcardType(this, s);
   443         }
   445         public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) {
   446             super(WILDCARD, tsym);
   447             this.type = Assert.checkNonNull(type);
   448             this.kind = kind;
   449         }
   450         public WildcardType(WildcardType t, TypeVar bound) {
   451             this(t.type, t.kind, t.tsym, bound);
   452         }
   454         public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, TypeVar bound) {
   455             this(type, kind, tsym);
   456             this.bound = bound;
   457         }
   459         public boolean contains(Type t) {
   460             return kind != UNBOUND && type.contains(t);
   461         }
   463         public boolean isSuperBound() {
   464             return kind == SUPER ||
   465                 kind == UNBOUND;
   466         }
   467         public boolean isExtendsBound() {
   468             return kind == EXTENDS ||
   469                 kind == UNBOUND;
   470         }
   471         public boolean isUnbound() {
   472             return kind == UNBOUND;
   473         }
   475         public Type withTypeVar(Type t) {
   476             //-System.err.println(this+".withTypeVar("+t+");");//DEBUG
   477             if (bound == t)
   478                 return this;
   479             bound = (TypeVar)t;
   480             return this;
   481         }
   483         boolean isPrintingBound = false;
   484         public String toString() {
   485             StringBuilder s = new StringBuilder();
   486             s.append(kind.toString());
   487             if (kind != UNBOUND)
   488                 s.append(type);
   489             if (moreInfo && bound != null && !isPrintingBound)
   490                 try {
   491                     isPrintingBound = true;
   492                     s.append("{:").append(bound.bound).append(":}");
   493                 } finally {
   494                     isPrintingBound = false;
   495                 }
   496             return s.toString();
   497         }
   499         public Type map(Mapping f) {
   500             //- System.err.println("   (" + this + ").map(" + f + ")");//DEBUG
   501             Type t = type;
   502             if (t != null)
   503                 t = f.apply(t);
   504             if (t == type)
   505                 return this;
   506             else
   507                 return new WildcardType(t, kind, tsym, bound);
   508         }
   510         public Type getExtendsBound() {
   511             if (kind == EXTENDS)
   512                 return type;
   513             else
   514                 return null;
   515         }
   517         public Type getSuperBound() {
   518             if (kind == SUPER)
   519                 return type;
   520             else
   521                 return null;
   522         }
   524         public TypeKind getKind() {
   525             return TypeKind.WILDCARD;
   526         }
   528         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   529             return v.visitWildcard(this, p);
   530         }
   531     }
   533     public static class ClassType extends Type implements DeclaredType {
   535         /** The enclosing type of this type. If this is the type of an inner
   536          *  class, outer_field refers to the type of its enclosing
   537          *  instance class, in all other cases it referes to noType.
   538          */
   539         private Type outer_field;
   541         /** The type parameters of this type (to be set once class is loaded).
   542          */
   543         public List<Type> typarams_field;
   545         /** A cache variable for the type parameters of this type,
   546          *  appended to all parameters of its enclosing class.
   547          *  @see #allparams
   548          */
   549         public List<Type> allparams_field;
   551         /** The supertype of this class (to be set once class is loaded).
   552          */
   553         public Type supertype_field;
   555         /** The interfaces of this class (to be set once class is loaded).
   556          */
   557         public List<Type> interfaces_field;
   559         /** All the interfaces of this class, including missing ones.
   560          */
   561         public List<Type> all_interfaces_field;
   563         public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) {
   564             super(CLASS, tsym);
   565             this.outer_field = outer;
   566             this.typarams_field = typarams;
   567             this.allparams_field = null;
   568             this.supertype_field = null;
   569             this.interfaces_field = null;
   570             /*
   571             // this can happen during error recovery
   572             assert
   573                 outer.isParameterized() ?
   574                 typarams.length() == tsym.type.typarams().length() :
   575                 outer.isRaw() ?
   576                 typarams.length() == 0 :
   577                 true;
   578             */
   579         }
   581         @Override
   582         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   583             return v.visitClassType(this, s);
   584         }
   586         public Type constType(Object constValue) {
   587             final Object value = constValue;
   588             return new ClassType(getEnclosingType(), typarams_field, tsym) {
   589                     @Override
   590                     public Object constValue() {
   591                         return value;
   592                     }
   593                     @Override
   594                     public Type baseType() {
   595                         return tsym.type;
   596                     }
   597                 };
   598         }
   600         /** The Java source which this type represents.
   601          */
   602         public String toString() {
   603             StringBuilder buf = new StringBuilder();
   604             if (getEnclosingType().tag == CLASS && tsym.owner.kind == TYP) {
   605                 buf.append(getEnclosingType().toString());
   606                 buf.append(".");
   607                 buf.append(className(tsym, false));
   608             } else {
   609                 buf.append(className(tsym, true));
   610             }
   611             if (getTypeArguments().nonEmpty()) {
   612                 buf.append('<');
   613                 buf.append(getTypeArguments().toString());
   614                 buf.append(">");
   615             }
   616             return buf.toString();
   617         }
   618 //where
   619             private String className(Symbol sym, boolean longform) {
   620                 if (sym.name.isEmpty() && (sym.flags() & COMPOUND) != 0) {
   621                     StringBuilder s = new StringBuilder(supertype_field.toString());
   622                     for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) {
   623                         s.append("&");
   624                         s.append(is.head.toString());
   625                     }
   626                     return s.toString();
   627                 } else if (sym.name.isEmpty()) {
   628                     String s;
   629                     ClassType norm = (ClassType) tsym.type;
   630                     if (norm == null) {
   631                         s = Log.getLocalizedString("anonymous.class", (Object)null);
   632                     } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
   633                         s = Log.getLocalizedString("anonymous.class",
   634                                                    norm.interfaces_field.head);
   635                     } else {
   636                         s = Log.getLocalizedString("anonymous.class",
   637                                                    norm.supertype_field);
   638                     }
   639                     if (moreInfo)
   640                         s += String.valueOf(sym.hashCode());
   641                     return s;
   642                 } else if (longform) {
   643                     return sym.getQualifiedName().toString();
   644                 } else {
   645                     return sym.name.toString();
   646                 }
   647             }
   649         public List<Type> getTypeArguments() {
   650             if (typarams_field == null) {
   651                 complete();
   652                 if (typarams_field == null)
   653                     typarams_field = List.nil();
   654             }
   655             return typarams_field;
   656         }
   658         public boolean hasErasedSupertypes() {
   659             return isRaw();
   660         }
   662         public Type getEnclosingType() {
   663             return outer_field;
   664         }
   666         public void setEnclosingType(Type outer) {
   667             outer_field = outer;
   668         }
   670         public List<Type> allparams() {
   671             if (allparams_field == null) {
   672                 allparams_field = getTypeArguments().prependList(getEnclosingType().allparams());
   673             }
   674             return allparams_field;
   675         }
   677         public boolean isErroneous() {
   678             return
   679                 getEnclosingType().isErroneous() ||
   680                 isErroneous(getTypeArguments()) ||
   681                 this != tsym.type && tsym.type.isErroneous();
   682         }
   684         public boolean isParameterized() {
   685             return allparams().tail != null;
   686             // optimization, was: allparams().nonEmpty();
   687         }
   689         /** A cache for the rank. */
   690         int rank_field = -1;
   692         /** A class type is raw if it misses some
   693          *  of its type parameter sections.
   694          *  After validation, this is equivalent to:
   695          *  allparams.isEmpty() && tsym.type.allparams.nonEmpty();
   696          */
   697         public boolean isRaw() {
   698             return
   699                 this != tsym.type && // necessary, but not sufficient condition
   700                 tsym.type.allparams().nonEmpty() &&
   701                 allparams().isEmpty();
   702         }
   704         public Type map(Mapping f) {
   705             Type outer = getEnclosingType();
   706             Type outer1 = f.apply(outer);
   707             List<Type> typarams = getTypeArguments();
   708             List<Type> typarams1 = map(typarams, f);
   709             if (outer1 == outer && typarams1 == typarams) return this;
   710             else return new ClassType(outer1, typarams1, tsym);
   711         }
   713         public boolean contains(Type elem) {
   714             return
   715                 elem == this
   716                 || (isParameterized()
   717                     && (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem)))
   718                 || (isCompound()
   719                     && (supertype_field.contains(elem) || contains(interfaces_field, elem)));
   720         }
   722         public void complete() {
   723             if (tsym.completer != null) tsym.complete();
   724         }
   726         public TypeKind getKind() {
   727             return TypeKind.DECLARED;
   728         }
   730         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   731             return v.visitDeclared(this, p);
   732         }
   733     }
   735     public static class ErasedClassType extends ClassType {
   736         public ErasedClassType(Type outer, TypeSymbol tsym) {
   737             super(outer, List.<Type>nil(), tsym);
   738         }
   740         @Override
   741         public boolean hasErasedSupertypes() {
   742             return true;
   743         }
   744     }
   746     // a clone of a ClassType that knows about the alternatives of a union type.
   747     public static class UnionClassType extends ClassType implements UnionType {
   748         final List<? extends Type> alternatives_field;
   750         public UnionClassType(ClassType ct, List<? extends Type> alternatives) {
   751             super(ct.outer_field, ct.typarams_field, ct.tsym);
   752             allparams_field = ct.allparams_field;
   753             supertype_field = ct.supertype_field;
   754             interfaces_field = ct.interfaces_field;
   755             all_interfaces_field = ct.interfaces_field;
   756             alternatives_field = alternatives;
   757         }
   759         public Type getLub() {
   760             return tsym.type;
   761         }
   763         public java.util.List<? extends TypeMirror> getAlternatives() {
   764             return Collections.unmodifiableList(alternatives_field);
   765         }
   767         @Override
   768         public TypeKind getKind() {
   769             return TypeKind.UNION;
   770         }
   772         @Override
   773         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   774             return v.visitUnion(this, p);
   775         }
   776     }
   778     public static class ArrayType extends Type
   779             implements javax.lang.model.type.ArrayType {
   781         public Type elemtype;
   783         public ArrayType(Type elemtype, TypeSymbol arrayClass) {
   784             super(ARRAY, arrayClass);
   785             this.elemtype = elemtype;
   786         }
   788         @Override
   789         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   790             return v.visitArrayType(this, s);
   791         }
   793         public String toString() {
   794             return elemtype + "[]";
   795         }
   797         public boolean equals(Object obj) {
   798             return
   799                 this == obj ||
   800                 (obj instanceof ArrayType &&
   801                  this.elemtype.equals(((ArrayType)obj).elemtype));
   802         }
   804         public int hashCode() {
   805             return (ARRAY << 5) + elemtype.hashCode();
   806         }
   808         public boolean isVarargs() {
   809             return false;
   810         }
   812         public List<Type> allparams() { return elemtype.allparams(); }
   814         public boolean isErroneous() {
   815             return elemtype.isErroneous();
   816         }
   818         public boolean isParameterized() {
   819             return elemtype.isParameterized();
   820         }
   822         public boolean isRaw() {
   823             return elemtype.isRaw();
   824         }
   826         public ArrayType makeVarargs() {
   827             return new ArrayType(elemtype, tsym) {
   828                 @Override
   829                 public boolean isVarargs() {
   830                     return true;
   831                 }
   832             };
   833         }
   835         public Type map(Mapping f) {
   836             Type elemtype1 = f.apply(elemtype);
   837             if (elemtype1 == elemtype) return this;
   838             else return new ArrayType(elemtype1, tsym);
   839         }
   841         public boolean contains(Type elem) {
   842             return elem == this || elemtype.contains(elem);
   843         }
   845         public void complete() {
   846             elemtype.complete();
   847         }
   849         public Type getComponentType() {
   850             return elemtype;
   851         }
   853         public TypeKind getKind() {
   854             return TypeKind.ARRAY;
   855         }
   857         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   858             return v.visitArray(this, p);
   859         }
   860     }
   862     public static class MethodType extends Type implements ExecutableType {
   864         public List<Type> argtypes;
   865         public Type restype;
   866         public List<Type> thrown;
   868         public MethodType(List<Type> argtypes,
   869                           Type restype,
   870                           List<Type> thrown,
   871                           TypeSymbol methodClass) {
   872             super(METHOD, methodClass);
   873             this.argtypes = argtypes;
   874             this.restype = restype;
   875             this.thrown = thrown;
   876         }
   878         @Override
   879         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   880             return v.visitMethodType(this, s);
   881         }
   883         /** The Java source which this type represents.
   884          *
   885          *  XXX 06/09/99 iris This isn't correct Java syntax, but it probably
   886          *  should be.
   887          */
   888         public String toString() {
   889             return "(" + argtypes + ")" + restype;
   890         }
   892         public boolean equals(Object obj) {
   893             if (this == obj)
   894                 return true;
   895             if (!(obj instanceof MethodType))
   896                 return false;
   897             MethodType m = (MethodType)obj;
   898             List<Type> args1 = argtypes;
   899             List<Type> args2 = m.argtypes;
   900             while (!args1.isEmpty() && !args2.isEmpty()) {
   901                 if (!args1.head.equals(args2.head))
   902                     return false;
   903                 args1 = args1.tail;
   904                 args2 = args2.tail;
   905             }
   906             if (!args1.isEmpty() || !args2.isEmpty())
   907                 return false;
   908             return restype.equals(m.restype);
   909         }
   911         public int hashCode() {
   912             int h = METHOD;
   913             for (List<Type> thisargs = this.argtypes;
   914                  thisargs.tail != null; /*inlined: thisargs.nonEmpty()*/
   915                  thisargs = thisargs.tail)
   916                 h = (h << 5) + thisargs.head.hashCode();
   917             return (h << 5) + this.restype.hashCode();
   918         }
   920         public List<Type>        getParameterTypes() { return argtypes; }
   921         public Type              getReturnType()     { return restype; }
   922         public List<Type>        getThrownTypes()    { return thrown; }
   924         public boolean isErroneous() {
   925             return
   926                 isErroneous(argtypes) ||
   927                 restype != null && restype.isErroneous();
   928         }
   930         public Type map(Mapping f) {
   931             List<Type> argtypes1 = map(argtypes, f);
   932             Type restype1 = f.apply(restype);
   933             List<Type> thrown1 = map(thrown, f);
   934             if (argtypes1 == argtypes &&
   935                 restype1 == restype &&
   936                 thrown1 == thrown) return this;
   937             else return new MethodType(argtypes1, restype1, thrown1, tsym);
   938         }
   940         public boolean contains(Type elem) {
   941             return elem == this || contains(argtypes, elem) || restype.contains(elem);
   942         }
   944         public MethodType asMethodType() { return this; }
   946         public void complete() {
   947             for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail)
   948                 l.head.complete();
   949             restype.complete();
   950             for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
   951                 l.head.complete();
   952         }
   954         public List<TypeVar> getTypeVariables() {
   955             return List.nil();
   956         }
   958         public TypeSymbol asElement() {
   959             return null;
   960         }
   962         public TypeKind getKind() {
   963             return TypeKind.EXECUTABLE;
   964         }
   966         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   967             return v.visitExecutable(this, p);
   968         }
   969     }
   971     public static class PackageType extends Type implements NoType {
   973         PackageType(TypeSymbol tsym) {
   974             super(PACKAGE, tsym);
   975         }
   977         @Override
   978         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   979             return v.visitPackageType(this, s);
   980         }
   982         public String toString() {
   983             return tsym.getQualifiedName().toString();
   984         }
   986         public TypeKind getKind() {
   987             return TypeKind.PACKAGE;
   988         }
   990         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   991             return v.visitNoType(this, p);
   992         }
   993     }
   995     public static class TypeVar extends Type implements TypeVariable {
   997         /** The upper bound of this type variable; set from outside.
   998          *  Must be nonempty once it is set.
   999          *  For a bound, `bound' is the bound type itself.
  1000          *  Multiple bounds are expressed as a single class type which has the
  1001          *  individual bounds as superclass, respectively interfaces.
  1002          *  The class type then has as `tsym' a compiler generated class `c',
  1003          *  which has a flag COMPOUND and whose owner is the type variable
  1004          *  itself. Furthermore, the erasure_field of the class
  1005          *  points to the first class or interface bound.
  1006          */
  1007         public Type bound = null;
  1009         /** The lower bound of this type variable.
  1010          *  TypeVars don't normally have a lower bound, so it is normally set
  1011          *  to syms.botType.
  1012          *  Subtypes, such as CapturedType, may provide a different value.
  1013          */
  1014         public Type lower;
  1016         public TypeVar(Name name, Symbol owner, Type lower) {
  1017             super(TYPEVAR, null);
  1018             tsym = new TypeSymbol(0, name, this, owner);
  1019             this.lower = lower;
  1022         public TypeVar(TypeSymbol tsym, Type bound, Type lower) {
  1023             super(TYPEVAR, tsym);
  1024             this.bound = bound;
  1025             this.lower = lower;
  1028         @Override
  1029         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1030             return v.visitTypeVar(this, s);
  1033         @Override
  1034         public Type getUpperBound() { return bound; }
  1036         int rank_field = -1;
  1038         @Override
  1039         public Type getLowerBound() {
  1040             return lower;
  1043         public TypeKind getKind() {
  1044             return TypeKind.TYPEVAR;
  1047         public boolean isCaptured() {
  1048             return false;
  1051         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1052             return v.visitTypeVariable(this, p);
  1056     /** A captured type variable comes from wildcards which can have
  1057      *  both upper and lower bound.  CapturedType extends TypeVar with
  1058      *  a lower bound.
  1059      */
  1060     public static class CapturedType extends TypeVar {
  1062         public WildcardType wildcard;
  1064         public CapturedType(Name name,
  1065                             Symbol owner,
  1066                             Type upper,
  1067                             Type lower,
  1068                             WildcardType wildcard) {
  1069             super(name, owner, lower);
  1070             this.lower = Assert.checkNonNull(lower);
  1071             this.bound = upper;
  1072             this.wildcard = wildcard;
  1075         @Override
  1076         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1077             return v.visitCapturedType(this, s);
  1080         @Override
  1081         public boolean isCaptured() {
  1082             return true;
  1085         @Override
  1086         public String toString() {
  1087             return "capture#"
  1088                 + (hashCode() & 0xFFFFFFFFL) % Printer.PRIME
  1089                 + " of "
  1090                 + wildcard;
  1094     public static abstract class DelegatedType extends Type {
  1095         public Type qtype;
  1096         public DelegatedType(int tag, Type qtype) {
  1097             super(tag, qtype.tsym);
  1098             this.qtype = qtype;
  1100         public String toString() { return qtype.toString(); }
  1101         public List<Type> getTypeArguments() { return qtype.getTypeArguments(); }
  1102         public Type getEnclosingType() { return qtype.getEnclosingType(); }
  1103         public List<Type> getParameterTypes() { return qtype.getParameterTypes(); }
  1104         public Type getReturnType() { return qtype.getReturnType(); }
  1105         public List<Type> getThrownTypes() { return qtype.getThrownTypes(); }
  1106         public List<Type> allparams() { return qtype.allparams(); }
  1107         public Type getUpperBound() { return qtype.getUpperBound(); }
  1108         public boolean isErroneous() { return qtype.isErroneous(); }
  1111     public static class ForAll extends DelegatedType implements ExecutableType {
  1112         public List<Type> tvars;
  1114         public ForAll(List<Type> tvars, Type qtype) {
  1115             super(FORALL, qtype);
  1116             this.tvars = tvars;
  1119         @Override
  1120         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1121             return v.visitForAll(this, s);
  1124         public String toString() {
  1125             return "<" + tvars + ">" + qtype;
  1128         public List<Type> getTypeArguments()   { return tvars; }
  1130         public boolean isErroneous()  {
  1131             return qtype.isErroneous();
  1134         /**
  1135          * Replaces this ForAll's typevars with a set of concrete Java types
  1136          * and returns the instantiated generic type. Subclasses should override
  1137          * in order to check that the list of types is a valid instantiation
  1138          * of the ForAll's typevars.
  1140          * @param actuals list of actual types
  1141          * @param types types instance
  1142          * @return qtype where all occurrences of tvars are replaced
  1143          * by types in actuals
  1144          */
  1145         public Type inst(List<Type> actuals, Types types) {
  1146             return types.subst(qtype, tvars, actuals);
  1149         /**
  1150          * Get the type-constraints of a given kind for a given type-variable of
  1151          * this ForAll type. Subclasses should override in order to return more
  1152          * accurate sets of constraints.
  1154          * @param tv the type-variable for which the constraint is to be retrieved
  1155          * @param ck the constraint kind to be retrieved
  1156          * @return the list of types specified by the selected constraint
  1157          */
  1158         public List<Type> undetvars() {
  1159             return List.nil();
  1162         public Type map(Mapping f) {
  1163             return f.apply(qtype);
  1166         public boolean contains(Type elem) {
  1167             return qtype.contains(elem);
  1170         public MethodType asMethodType() {
  1171             return qtype.asMethodType();
  1174         public void complete() {
  1175             for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) {
  1176                 ((TypeVar)l.head).bound.complete();
  1178             qtype.complete();
  1181         public List<TypeVar> getTypeVariables() {
  1182             return List.convert(TypeVar.class, getTypeArguments());
  1185         public TypeKind getKind() {
  1186             return TypeKind.EXECUTABLE;
  1189         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1190             return v.visitExecutable(this, p);
  1194     /** A class for instantiatable variables, for use during type
  1195      *  inference.
  1196      */
  1197     public static class UndetVar extends DelegatedType {
  1198         public List<Type> lobounds = List.nil();
  1199         public List<Type> hibounds = List.nil();
  1200         public List<Type> eq = List.nil();
  1201         public Type inst = null;
  1203         @Override
  1204         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1205             return v.visitUndetVar(this, s);
  1208         public UndetVar(Type origin) {
  1209             super(UNDETVAR, origin);
  1212         public String toString() {
  1213             if (inst != null) return inst.toString();
  1214             else return qtype + "?";
  1217         public Type baseType() {
  1218             if (inst != null) return inst.baseType();
  1219             else return this;
  1223     /** Represents VOID or NONE.
  1224      */
  1225     static class JCNoType extends Type implements NoType {
  1226         public JCNoType(int tag) {
  1227             super(tag, null);
  1230         @Override
  1231         public TypeKind getKind() {
  1232             switch (tag) {
  1233             case VOID:  return TypeKind.VOID;
  1234             case NONE:  return TypeKind.NONE;
  1235             default:
  1236                 throw new AssertionError("Unexpected tag: " + tag);
  1240         @Override
  1241         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1242             return v.visitNoType(this, p);
  1246     static class BottomType extends Type implements NullType {
  1247         public BottomType() {
  1248             super(TypeTags.BOT, null);
  1251         @Override
  1252         public TypeKind getKind() {
  1253             return TypeKind.NULL;
  1256         @Override
  1257         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1258             return v.visitNull(this, p);
  1261         @Override
  1262         public Type constType(Object value) {
  1263             return this;
  1266         @Override
  1267         public String stringValue() {
  1268             return "null";
  1272     public static class ErrorType extends ClassType
  1273             implements javax.lang.model.type.ErrorType {
  1275         private Type originalType = null;
  1277         public ErrorType(Type originalType, TypeSymbol tsym) {
  1278             super(noType, List.<Type>nil(), null);
  1279             tag = ERROR;
  1280             this.tsym = tsym;
  1281             this.originalType = (originalType == null ? noType : originalType);
  1284         public ErrorType(ClassSymbol c, Type originalType) {
  1285             this(originalType, c);
  1286             c.type = this;
  1287             c.kind = ERR;
  1288             c.members_field = new Scope.ErrorScope(c);
  1291         public ErrorType(Name name, TypeSymbol container, Type originalType) {
  1292             this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container), originalType);
  1295         @Override
  1296         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1297             return v.visitErrorType(this, s);
  1300         public Type constType(Object constValue) { return this; }
  1301         public Type getEnclosingType()          { return this; }
  1302         public Type getReturnType()              { return this; }
  1303         public Type asSub(Symbol sym)            { return this; }
  1304         public Type map(Mapping f)               { return this; }
  1306         public boolean isGenType(Type t)         { return true; }
  1307         public boolean isErroneous()             { return true; }
  1308         public boolean isCompound()              { return false; }
  1309         public boolean isInterface()             { return false; }
  1311         public List<Type> allparams()            { return List.nil(); }
  1312         public List<Type> getTypeArguments()     { return List.nil(); }
  1314         public TypeKind getKind() {
  1315             return TypeKind.ERROR;
  1318         public Type getOriginalType() {
  1319             return originalType;
  1322         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1323             return v.visitError(this, p);
  1327     /**
  1328      * A visitor for types.  A visitor is used to implement operations
  1329      * (or relations) on types.  Most common operations on types are
  1330      * binary relations and this interface is designed for binary
  1331      * relations, that is, operations on the form
  1332      * Type&nbsp;&times;&nbsp;S&nbsp;&rarr;&nbsp;R.
  1333      * <!-- In plain text: Type x S -> R -->
  1335      * @param <R> the return type of the operation implemented by this
  1336      * visitor; use Void if no return type is needed.
  1337      * @param <S> the type of the second argument (the first being the
  1338      * type itself) of the operation implemented by this visitor; use
  1339      * Void if a second argument is not needed.
  1340      */
  1341     public interface Visitor<R,S> {
  1342         R visitClassType(ClassType t, S s);
  1343         R visitWildcardType(WildcardType t, S s);
  1344         R visitArrayType(ArrayType t, S s);
  1345         R visitMethodType(MethodType t, S s);
  1346         R visitPackageType(PackageType t, S s);
  1347         R visitTypeVar(TypeVar t, S s);
  1348         R visitCapturedType(CapturedType t, S s);
  1349         R visitForAll(ForAll t, S s);
  1350         R visitUndetVar(UndetVar t, S s);
  1351         R visitErrorType(ErrorType t, S s);
  1352         R visitType(Type t, S s);

mercurial