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

Fri, 05 Oct 2012 14:35:24 +0100

author
mcimadamore
date
Fri, 05 Oct 2012 14:35:24 +0100
changeset 1348
573ceb23beeb
parent 1347
1408af4cd8b0
child 1357
c75be5bc5283
permissions
-rw-r--r--

7177385: Add attribution support for lambda expressions
Summary: Add support for function descriptor lookup, functional interface inference and lambda expression type-checking
Reviewed-by: jjg, dlsmith

     1 /*
     2  * Copyright (c) 1999, 2012, 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.code.Symbol.*;
    31 import com.sun.tools.javac.util.*;
    33 import java.util.EnumMap;
    34 import java.util.EnumSet;
    35 import java.util.Map;
    36 import java.util.Set;
    38 import javax.lang.model.type.*;
    40 import static com.sun.tools.javac.code.BoundKind.*;
    41 import static com.sun.tools.javac.code.Flags.*;
    42 import static com.sun.tools.javac.code.Kinds.*;
    43 import static com.sun.tools.javac.code.TypeTags.*;
    45 /** This class represents Java types. The class itself defines the behavior of
    46  *  the following types:
    47  *  <pre>
    48  *  base types (tags: BYTE, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE, BOOLEAN),
    49  *  type `void' (tag: VOID),
    50  *  the bottom type (tag: BOT),
    51  *  the missing type (tag: NONE).
    52  *  </pre>
    53  *  <p>The behavior of the following types is defined in subclasses, which are
    54  *  all static inner classes of this class:
    55  *  <pre>
    56  *  class types (tag: CLASS, class: ClassType),
    57  *  array types (tag: ARRAY, class: ArrayType),
    58  *  method types (tag: METHOD, class: MethodType),
    59  *  package types (tag: PACKAGE, class: PackageType),
    60  *  type variables (tag: TYPEVAR, class: TypeVar),
    61  *  type arguments (tag: WILDCARD, class: WildcardType),
    62  *  generic method types (tag: FORALL, class: ForAll),
    63  *  the error type (tag: ERROR, class: ErrorType).
    64  *  </pre>
    65  *
    66  *  <p><b>This is NOT part of any supported API.
    67  *  If you write code that depends on this, you do so at your own risk.
    68  *  This code and its internal interfaces are subject to change or
    69  *  deletion without notice.</b>
    70  *
    71  *  @see TypeTags
    72  */
    73 public class Type implements PrimitiveType {
    75     /** Constant type: no type at all. */
    76     public static final JCNoType noType = new JCNoType(NONE);
    78     /** Constant type: special type to be used during recovery of deferred expressions. */
    79     public static final JCNoType recoveryType = new JCNoType(NONE);
    81     /** If this switch is turned on, the names of type variables
    82      *  and anonymous classes are printed with hashcodes appended.
    83      */
    84     public static boolean moreInfo = false;
    86     /** The tag of this type.
    87      *
    88      *  @see TypeTags
    89      */
    90     public int tag;
    92     /** The defining class / interface / package / type variable
    93      */
    94     public TypeSymbol tsym;
    96     /**
    97      * The constant value of this type, null if this type does not
    98      * have a constant value attribute. Only primitive types and
    99      * strings (ClassType) can have a constant value attribute.
   100      * @return the constant value attribute of this type
   101      */
   102     public Object constValue() {
   103         return null;
   104     }
   106     /**
   107      * Get the representation of this type used for modelling purposes.
   108      * By default, this is itself. For ErrorType, a different value
   109      * may be provided,
   110      */
   111     public Type getModelType() {
   112         return this;
   113     }
   115     public static List<Type> getModelTypes(List<Type> ts) {
   116         ListBuffer<Type> lb = new ListBuffer<Type>();
   117         for (Type t: ts)
   118             lb.append(t.getModelType());
   119         return lb.toList();
   120     }
   122     public <R,S> R accept(Type.Visitor<R,S> v, S s) { return v.visitType(this, s); }
   124     /** Define a type given its tag and type symbol
   125      */
   126     public Type(int tag, TypeSymbol tsym) {
   127         this.tag = tag;
   128         this.tsym = tsym;
   129     }
   131     /** An abstract class for mappings from types to types
   132      */
   133     public static abstract class Mapping {
   134         private String name;
   135         public Mapping(String name) {
   136             this.name = name;
   137         }
   138         public abstract Type apply(Type t);
   139         public String toString() {
   140             return name;
   141         }
   142     }
   144     /** map a type function over all immediate descendants of this type
   145      */
   146     public Type map(Mapping f) {
   147         return this;
   148     }
   150     /** map a type function over a list of types
   151      */
   152     public static List<Type> map(List<Type> ts, Mapping f) {
   153         if (ts.nonEmpty()) {
   154             List<Type> tail1 = map(ts.tail, f);
   155             Type t = f.apply(ts.head);
   156             if (tail1 != ts.tail || t != ts.head)
   157                 return tail1.prepend(t);
   158         }
   159         return ts;
   160     }
   162     /** Define a constant type, of the same kind as this type
   163      *  and with given constant value
   164      */
   165     public Type constType(Object constValue) {
   166         final Object value = constValue;
   167         Assert.check(tag <= BOOLEAN);
   168         return new Type(tag, tsym) {
   169                 @Override
   170                 public Object constValue() {
   171                     return value;
   172                 }
   173                 @Override
   174                 public Type baseType() {
   175                     return tsym.type;
   176                 }
   177             };
   178     }
   180     /**
   181      * If this is a constant type, return its underlying type.
   182      * Otherwise, return the type itself.
   183      */
   184     public Type baseType() {
   185         return this;
   186     }
   188     /** Return the base types of a list of types.
   189      */
   190     public static List<Type> baseTypes(List<Type> ts) {
   191         if (ts.nonEmpty()) {
   192             Type t = ts.head.baseType();
   193             List<Type> baseTypes = baseTypes(ts.tail);
   194             if (t != ts.head || baseTypes != ts.tail)
   195                 return baseTypes.prepend(t);
   196         }
   197         return ts;
   198     }
   200     /** The Java source which this type represents.
   201      */
   202     public String toString() {
   203         String s = (tsym == null || tsym.name == null)
   204             ? "<none>"
   205             : tsym.name.toString();
   206         if (moreInfo && tag == TYPEVAR) s = s + hashCode();
   207         return s;
   208     }
   210     /**
   211      * The Java source which this type list represents.  A List is
   212      * represented as a comma-spearated listing of the elements in
   213      * that list.
   214      */
   215     public static String toString(List<Type> ts) {
   216         if (ts.isEmpty()) {
   217             return "";
   218         } else {
   219             StringBuilder buf = new StringBuilder();
   220             buf.append(ts.head.toString());
   221             for (List<Type> l = ts.tail; l.nonEmpty(); l = l.tail)
   222                 buf.append(",").append(l.head.toString());
   223             return buf.toString();
   224         }
   225     }
   227     /**
   228      * The constant value of this type, converted to String
   229      */
   230     public String stringValue() {
   231         Object cv = Assert.checkNonNull(constValue());
   232         if (tag == BOOLEAN)
   233             return ((Integer) cv).intValue() == 0 ? "false" : "true";
   234         else if (tag == CHAR)
   235             return String.valueOf((char) ((Integer) cv).intValue());
   236         else
   237             return cv.toString();
   238     }
   240     /**
   241      * This method is analogous to isSameType, but weaker, since we
   242      * never complete classes. Where isSameType would complete a
   243      * class, equals assumes that the two types are different.
   244      */
   245     public boolean equals(Object t) {
   246         return super.equals(t);
   247     }
   249     public int hashCode() {
   250         return super.hashCode();
   251     }
   253     /** Is this a constant type whose value is false?
   254      */
   255     public boolean isFalse() {
   256         return
   257             tag == BOOLEAN &&
   258             constValue() != null &&
   259             ((Integer)constValue()).intValue() == 0;
   260     }
   262     /** Is this a constant type whose value is true?
   263      */
   264     public boolean isTrue() {
   265         return
   266             tag == BOOLEAN &&
   267             constValue() != null &&
   268             ((Integer)constValue()).intValue() != 0;
   269     }
   271     public String argtypes(boolean varargs) {
   272         List<Type> args = getParameterTypes();
   273         if (!varargs) return args.toString();
   274         StringBuilder buf = new StringBuilder();
   275         while (args.tail.nonEmpty()) {
   276             buf.append(args.head);
   277             args = args.tail;
   278             buf.append(',');
   279         }
   280         if (args.head.tag == ARRAY) {
   281             buf.append(((ArrayType)args.head).elemtype);
   282             buf.append("...");
   283         } else {
   284             buf.append(args.head);
   285         }
   286         return buf.toString();
   287     }
   289     /** Access methods.
   290      */
   291     public List<Type>        getTypeArguments()  { return List.nil(); }
   292     public Type              getEnclosingType() { return null; }
   293     public List<Type>        getParameterTypes() { return List.nil(); }
   294     public Type              getReturnType()     { return null; }
   295     public List<Type>        getThrownTypes()    { return List.nil(); }
   296     public Type              getUpperBound()     { return null; }
   297     public Type              getLowerBound()     { return null; }
   299     /** Navigation methods, these will work for classes, type variables,
   300      *  foralls, but will return null for arrays and methods.
   301      */
   303    /** Return all parameters of this type and all its outer types in order
   304     *  outer (first) to inner (last).
   305     */
   306     public List<Type> allparams() { return List.nil(); }
   308     /** Does this type contain "error" elements?
   309      */
   310     public boolean isErroneous() {
   311         return false;
   312     }
   314     public static boolean isErroneous(List<Type> ts) {
   315         for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
   316             if (l.head.isErroneous()) return true;
   317         return false;
   318     }
   320     /** Is this type parameterized?
   321      *  A class type is parameterized if it has some parameters.
   322      *  An array type is parameterized if its element type is parameterized.
   323      *  All other types are not parameterized.
   324      */
   325     public boolean isParameterized() {
   326         return false;
   327     }
   329     /** Is this type a raw type?
   330      *  A class type is a raw type if it misses some of its parameters.
   331      *  An array type is a raw type if its element type is raw.
   332      *  All other types are not raw.
   333      *  Type validation will ensure that the only raw types
   334      *  in a program are types that miss all their type variables.
   335      */
   336     public boolean isRaw() {
   337         return false;
   338     }
   340     public boolean isCompound() {
   341         return tsym.completer == null
   342             // Compound types can't have a completer.  Calling
   343             // flags() will complete the symbol causing the
   344             // compiler to load classes unnecessarily.  This led
   345             // to regression 6180021.
   346             && (tsym.flags() & COMPOUND) != 0;
   347     }
   349     public boolean isInterface() {
   350         return (tsym.flags() & INTERFACE) != 0;
   351     }
   353     public boolean isFinal() {
   354         return (tsym.flags() & FINAL) != 0;
   355     }
   357     public boolean isPrimitive() {
   358         return tag < VOID;
   359     }
   361     /**
   362      * Does this type contain occurrences of type t?
   363      */
   364     public boolean contains(Type t) {
   365         return t == this;
   366     }
   368     public static boolean contains(List<Type> ts, Type t) {
   369         for (List<Type> l = ts;
   370              l.tail != null /*inlined: l.nonEmpty()*/;
   371              l = l.tail)
   372             if (l.head.contains(t)) return true;
   373         return false;
   374     }
   376     /** Does this type contain an occurrence of some type in 'ts'?
   377      */
   378     public boolean containsAny(List<Type> ts) {
   379         for (Type t : ts)
   380             if (this.contains(t)) return true;
   381         return false;
   382     }
   384     public static boolean containsAny(List<Type> ts1, List<Type> ts2) {
   385         for (Type t : ts1)
   386             if (t.containsAny(ts2)) return true;
   387         return false;
   388     }
   390     public static List<Type> filter(List<Type> ts, Filter<Type> tf) {
   391         ListBuffer<Type> buf = ListBuffer.lb();
   392         for (Type t : ts) {
   393             if (tf.accepts(t)) {
   394                 buf.append(t);
   395             }
   396         }
   397         return buf.toList();
   398     }
   400     public boolean isSuperBound() { return false; }
   401     public boolean isExtendsBound() { return false; }
   402     public boolean isUnbound() { return false; }
   403     public Type withTypeVar(Type t) { return this; }
   405     /** The underlying method type of this type.
   406      */
   407     public MethodType asMethodType() { throw new AssertionError(); }
   409     /** Complete loading all classes in this type.
   410      */
   411     public void complete() {}
   413     public TypeSymbol asElement() {
   414         return tsym;
   415     }
   417     public TypeKind getKind() {
   418         switch (tag) {
   419         case BYTE:      return TypeKind.BYTE;
   420         case CHAR:      return TypeKind.CHAR;
   421         case SHORT:     return TypeKind.SHORT;
   422         case INT:       return TypeKind.INT;
   423         case LONG:      return TypeKind.LONG;
   424         case FLOAT:     return TypeKind.FLOAT;
   425         case DOUBLE:    return TypeKind.DOUBLE;
   426         case BOOLEAN:   return TypeKind.BOOLEAN;
   427         case VOID:      return TypeKind.VOID;
   428         case BOT:       return TypeKind.NULL;
   429         case NONE:      return TypeKind.NONE;
   430         default:        return TypeKind.OTHER;
   431         }
   432     }
   434     public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   435         if (isPrimitive())
   436             return v.visitPrimitive(this, p);
   437         else
   438             throw new AssertionError();
   439     }
   441     public static class WildcardType extends Type
   442             implements javax.lang.model.type.WildcardType {
   444         public Type type;
   445         public BoundKind kind;
   446         public TypeVar bound;
   448         @Override
   449         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   450             return v.visitWildcardType(this, s);
   451         }
   453         public WildcardType(Type type, BoundKind kind, TypeSymbol tsym) {
   454             super(WILDCARD, tsym);
   455             this.type = Assert.checkNonNull(type);
   456             this.kind = kind;
   457         }
   458         public WildcardType(WildcardType t, TypeVar bound) {
   459             this(t.type, t.kind, t.tsym, bound);
   460         }
   462         public WildcardType(Type type, BoundKind kind, TypeSymbol tsym, TypeVar bound) {
   463             this(type, kind, tsym);
   464             this.bound = bound;
   465         }
   467         public boolean contains(Type t) {
   468             return kind != UNBOUND && type.contains(t);
   469         }
   471         public boolean isSuperBound() {
   472             return kind == SUPER ||
   473                 kind == UNBOUND;
   474         }
   475         public boolean isExtendsBound() {
   476             return kind == EXTENDS ||
   477                 kind == UNBOUND;
   478         }
   479         public boolean isUnbound() {
   480             return kind == UNBOUND;
   481         }
   483         public Type withTypeVar(Type t) {
   484             //-System.err.println(this+".withTypeVar("+t+");");//DEBUG
   485             if (bound == t)
   486                 return this;
   487             bound = (TypeVar)t;
   488             return this;
   489         }
   491         boolean isPrintingBound = false;
   492         public String toString() {
   493             StringBuilder s = new StringBuilder();
   494             s.append(kind.toString());
   495             if (kind != UNBOUND)
   496                 s.append(type);
   497             if (moreInfo && bound != null && !isPrintingBound)
   498                 try {
   499                     isPrintingBound = true;
   500                     s.append("{:").append(bound.bound).append(":}");
   501                 } finally {
   502                     isPrintingBound = false;
   503                 }
   504             return s.toString();
   505         }
   507         public Type map(Mapping f) {
   508             //- System.err.println("   (" + this + ").map(" + f + ")");//DEBUG
   509             Type t = type;
   510             if (t != null)
   511                 t = f.apply(t);
   512             if (t == type)
   513                 return this;
   514             else
   515                 return new WildcardType(t, kind, tsym, bound);
   516         }
   518         public Type getExtendsBound() {
   519             if (kind == EXTENDS)
   520                 return type;
   521             else
   522                 return null;
   523         }
   525         public Type getSuperBound() {
   526             if (kind == SUPER)
   527                 return type;
   528             else
   529                 return null;
   530         }
   532         public TypeKind getKind() {
   533             return TypeKind.WILDCARD;
   534         }
   536         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   537             return v.visitWildcard(this, p);
   538         }
   539     }
   541     public static class ClassType extends Type implements DeclaredType {
   543         /** The enclosing type of this type. If this is the type of an inner
   544          *  class, outer_field refers to the type of its enclosing
   545          *  instance class, in all other cases it referes to noType.
   546          */
   547         private Type outer_field;
   549         /** The type parameters of this type (to be set once class is loaded).
   550          */
   551         public List<Type> typarams_field;
   553         /** A cache variable for the type parameters of this type,
   554          *  appended to all parameters of its enclosing class.
   555          *  @see #allparams
   556          */
   557         public List<Type> allparams_field;
   559         /** The supertype of this class (to be set once class is loaded).
   560          */
   561         public Type supertype_field;
   563         /** The interfaces of this class (to be set once class is loaded).
   564          */
   565         public List<Type> interfaces_field;
   567         /** All the interfaces of this class, including missing ones.
   568          */
   569         public List<Type> all_interfaces_field;
   571         public ClassType(Type outer, List<Type> typarams, TypeSymbol tsym) {
   572             super(CLASS, tsym);
   573             this.outer_field = outer;
   574             this.typarams_field = typarams;
   575             this.allparams_field = null;
   576             this.supertype_field = null;
   577             this.interfaces_field = null;
   578             /*
   579             // this can happen during error recovery
   580             assert
   581                 outer.isParameterized() ?
   582                 typarams.length() == tsym.type.typarams().length() :
   583                 outer.isRaw() ?
   584                 typarams.length() == 0 :
   585                 true;
   586             */
   587         }
   589         @Override
   590         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   591             return v.visitClassType(this, s);
   592         }
   594         public Type constType(Object constValue) {
   595             final Object value = constValue;
   596             return new ClassType(getEnclosingType(), typarams_field, tsym) {
   597                     @Override
   598                     public Object constValue() {
   599                         return value;
   600                     }
   601                     @Override
   602                     public Type baseType() {
   603                         return tsym.type;
   604                     }
   605                 };
   606         }
   608         /** The Java source which this type represents.
   609          */
   610         public String toString() {
   611             StringBuilder buf = new StringBuilder();
   612             if (getEnclosingType().tag == CLASS && tsym.owner.kind == TYP) {
   613                 buf.append(getEnclosingType().toString());
   614                 buf.append(".");
   615                 buf.append(className(tsym, false));
   616             } else {
   617                 buf.append(className(tsym, true));
   618             }
   619             if (getTypeArguments().nonEmpty()) {
   620                 buf.append('<');
   621                 buf.append(getTypeArguments().toString());
   622                 buf.append(">");
   623             }
   624             return buf.toString();
   625         }
   626 //where
   627             private String className(Symbol sym, boolean longform) {
   628                 if (sym.name.isEmpty() && (sym.flags() & COMPOUND) != 0) {
   629                     StringBuilder s = new StringBuilder(supertype_field.toString());
   630                     for (List<Type> is=interfaces_field; is.nonEmpty(); is = is.tail) {
   631                         s.append("&");
   632                         s.append(is.head.toString());
   633                     }
   634                     return s.toString();
   635                 } else if (sym.name.isEmpty()) {
   636                     String s;
   637                     ClassType norm = (ClassType) tsym.type;
   638                     if (norm == null) {
   639                         s = Log.getLocalizedString("anonymous.class", (Object)null);
   640                     } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
   641                         s = Log.getLocalizedString("anonymous.class",
   642                                                    norm.interfaces_field.head);
   643                     } else {
   644                         s = Log.getLocalizedString("anonymous.class",
   645                                                    norm.supertype_field);
   646                     }
   647                     if (moreInfo)
   648                         s += String.valueOf(sym.hashCode());
   649                     return s;
   650                 } else if (longform) {
   651                     return sym.getQualifiedName().toString();
   652                 } else {
   653                     return sym.name.toString();
   654                 }
   655             }
   657         public List<Type> getTypeArguments() {
   658             if (typarams_field == null) {
   659                 complete();
   660                 if (typarams_field == null)
   661                     typarams_field = List.nil();
   662             }
   663             return typarams_field;
   664         }
   666         public boolean hasErasedSupertypes() {
   667             return isRaw();
   668         }
   670         public Type getEnclosingType() {
   671             return outer_field;
   672         }
   674         public void setEnclosingType(Type outer) {
   675             outer_field = outer;
   676         }
   678         public List<Type> allparams() {
   679             if (allparams_field == null) {
   680                 allparams_field = getTypeArguments().prependList(getEnclosingType().allparams());
   681             }
   682             return allparams_field;
   683         }
   685         public boolean isErroneous() {
   686             return
   687                 getEnclosingType().isErroneous() ||
   688                 isErroneous(getTypeArguments()) ||
   689                 this != tsym.type && tsym.type.isErroneous();
   690         }
   692         public boolean isParameterized() {
   693             return allparams().tail != null;
   694             // optimization, was: allparams().nonEmpty();
   695         }
   697         /** A cache for the rank. */
   698         int rank_field = -1;
   700         /** A class type is raw if it misses some
   701          *  of its type parameter sections.
   702          *  After validation, this is equivalent to:
   703          *  {@code allparams.isEmpty() && tsym.type.allparams.nonEmpty(); }
   704          */
   705         public boolean isRaw() {
   706             return
   707                 this != tsym.type && // necessary, but not sufficient condition
   708                 tsym.type.allparams().nonEmpty() &&
   709                 allparams().isEmpty();
   710         }
   712         public Type map(Mapping f) {
   713             Type outer = getEnclosingType();
   714             Type outer1 = f.apply(outer);
   715             List<Type> typarams = getTypeArguments();
   716             List<Type> typarams1 = map(typarams, f);
   717             if (outer1 == outer && typarams1 == typarams) return this;
   718             else return new ClassType(outer1, typarams1, tsym);
   719         }
   721         public boolean contains(Type elem) {
   722             return
   723                 elem == this
   724                 || (isParameterized()
   725                     && (getEnclosingType().contains(elem) || contains(getTypeArguments(), elem)))
   726                 || (isCompound()
   727                     && (supertype_field.contains(elem) || contains(interfaces_field, elem)));
   728         }
   730         public void complete() {
   731             if (tsym.completer != null) tsym.complete();
   732         }
   734         public TypeKind getKind() {
   735             return TypeKind.DECLARED;
   736         }
   738         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   739             return v.visitDeclared(this, p);
   740         }
   741     }
   743     public static class ErasedClassType extends ClassType {
   744         public ErasedClassType(Type outer, TypeSymbol tsym) {
   745             super(outer, List.<Type>nil(), tsym);
   746         }
   748         @Override
   749         public boolean hasErasedSupertypes() {
   750             return true;
   751         }
   752     }
   754     // a clone of a ClassType that knows about the alternatives of a union type.
   755     public static class UnionClassType extends ClassType implements UnionType {
   756         final List<? extends Type> alternatives_field;
   758         public UnionClassType(ClassType ct, List<? extends Type> alternatives) {
   759             super(ct.outer_field, ct.typarams_field, ct.tsym);
   760             allparams_field = ct.allparams_field;
   761             supertype_field = ct.supertype_field;
   762             interfaces_field = ct.interfaces_field;
   763             all_interfaces_field = ct.interfaces_field;
   764             alternatives_field = alternatives;
   765         }
   767         public Type getLub() {
   768             return tsym.type;
   769         }
   771         public java.util.List<? extends TypeMirror> getAlternatives() {
   772             return Collections.unmodifiableList(alternatives_field);
   773         }
   775         @Override
   776         public TypeKind getKind() {
   777             return TypeKind.UNION;
   778         }
   780         @Override
   781         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   782             return v.visitUnion(this, p);
   783         }
   784     }
   786     public static class ArrayType extends Type
   787             implements javax.lang.model.type.ArrayType {
   789         public Type elemtype;
   791         public ArrayType(Type elemtype, TypeSymbol arrayClass) {
   792             super(ARRAY, arrayClass);
   793             this.elemtype = elemtype;
   794         }
   796         @Override
   797         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   798             return v.visitArrayType(this, s);
   799         }
   801         public String toString() {
   802             return elemtype + "[]";
   803         }
   805         public boolean equals(Object obj) {
   806             return
   807                 this == obj ||
   808                 (obj instanceof ArrayType &&
   809                  this.elemtype.equals(((ArrayType)obj).elemtype));
   810         }
   812         public int hashCode() {
   813             return (ARRAY << 5) + elemtype.hashCode();
   814         }
   816         public boolean isVarargs() {
   817             return false;
   818         }
   820         public List<Type> allparams() { return elemtype.allparams(); }
   822         public boolean isErroneous() {
   823             return elemtype.isErroneous();
   824         }
   826         public boolean isParameterized() {
   827             return elemtype.isParameterized();
   828         }
   830         public boolean isRaw() {
   831             return elemtype.isRaw();
   832         }
   834         public ArrayType makeVarargs() {
   835             return new ArrayType(elemtype, tsym) {
   836                 @Override
   837                 public boolean isVarargs() {
   838                     return true;
   839                 }
   840             };
   841         }
   843         public Type map(Mapping f) {
   844             Type elemtype1 = f.apply(elemtype);
   845             if (elemtype1 == elemtype) return this;
   846             else return new ArrayType(elemtype1, tsym);
   847         }
   849         public boolean contains(Type elem) {
   850             return elem == this || elemtype.contains(elem);
   851         }
   853         public void complete() {
   854             elemtype.complete();
   855         }
   857         public Type getComponentType() {
   858             return elemtype;
   859         }
   861         public TypeKind getKind() {
   862             return TypeKind.ARRAY;
   863         }
   865         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   866             return v.visitArray(this, p);
   867         }
   868     }
   870     public static class MethodType extends Type implements ExecutableType {
   872         public List<Type> argtypes;
   873         public Type restype;
   874         public List<Type> thrown;
   876         public MethodType(List<Type> argtypes,
   877                           Type restype,
   878                           List<Type> thrown,
   879                           TypeSymbol methodClass) {
   880             super(METHOD, methodClass);
   881             this.argtypes = argtypes;
   882             this.restype = restype;
   883             this.thrown = thrown;
   884         }
   886         @Override
   887         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   888             return v.visitMethodType(this, s);
   889         }
   891         /** The Java source which this type represents.
   892          *
   893          *  XXX 06/09/99 iris This isn't correct Java syntax, but it probably
   894          *  should be.
   895          */
   896         public String toString() {
   897             return "(" + argtypes + ")" + restype;
   898         }
   900         public boolean equals(Object obj) {
   901             if (this == obj)
   902                 return true;
   903             if (!(obj instanceof MethodType))
   904                 return false;
   905             MethodType m = (MethodType)obj;
   906             List<Type> args1 = argtypes;
   907             List<Type> args2 = m.argtypes;
   908             while (!args1.isEmpty() && !args2.isEmpty()) {
   909                 if (!args1.head.equals(args2.head))
   910                     return false;
   911                 args1 = args1.tail;
   912                 args2 = args2.tail;
   913             }
   914             if (!args1.isEmpty() || !args2.isEmpty())
   915                 return false;
   916             return restype.equals(m.restype);
   917         }
   919         public int hashCode() {
   920             int h = METHOD;
   921             for (List<Type> thisargs = this.argtypes;
   922                  thisargs.tail != null; /*inlined: thisargs.nonEmpty()*/
   923                  thisargs = thisargs.tail)
   924                 h = (h << 5) + thisargs.head.hashCode();
   925             return (h << 5) + this.restype.hashCode();
   926         }
   928         public List<Type>        getParameterTypes() { return argtypes; }
   929         public Type              getReturnType()     { return restype; }
   930         public List<Type>        getThrownTypes()    { return thrown; }
   932         public boolean isErroneous() {
   933             return
   934                 isErroneous(argtypes) ||
   935                 restype != null && restype.isErroneous();
   936         }
   938         public Type map(Mapping f) {
   939             List<Type> argtypes1 = map(argtypes, f);
   940             Type restype1 = f.apply(restype);
   941             List<Type> thrown1 = map(thrown, f);
   942             if (argtypes1 == argtypes &&
   943                 restype1 == restype &&
   944                 thrown1 == thrown) return this;
   945             else return new MethodType(argtypes1, restype1, thrown1, tsym);
   946         }
   948         public boolean contains(Type elem) {
   949             return elem == this || contains(argtypes, elem) || restype.contains(elem);
   950         }
   952         public MethodType asMethodType() { return this; }
   954         public void complete() {
   955             for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail)
   956                 l.head.complete();
   957             restype.complete();
   958             for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
   959                 l.head.complete();
   960         }
   962         public List<TypeVar> getTypeVariables() {
   963             return List.nil();
   964         }
   966         public TypeSymbol asElement() {
   967             return null;
   968         }
   970         public TypeKind getKind() {
   971             return TypeKind.EXECUTABLE;
   972         }
   974         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   975             return v.visitExecutable(this, p);
   976         }
   977     }
   979     public static class PackageType extends Type implements NoType {
   981         PackageType(TypeSymbol tsym) {
   982             super(PACKAGE, tsym);
   983         }
   985         @Override
   986         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
   987             return v.visitPackageType(this, s);
   988         }
   990         public String toString() {
   991             return tsym.getQualifiedName().toString();
   992         }
   994         public TypeKind getKind() {
   995             return TypeKind.PACKAGE;
   996         }
   998         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
   999             return v.visitNoType(this, p);
  1003     public static class TypeVar extends Type implements TypeVariable {
  1005         /** The upper bound of this type variable; set from outside.
  1006          *  Must be nonempty once it is set.
  1007          *  For a bound, `bound' is the bound type itself.
  1008          *  Multiple bounds are expressed as a single class type which has the
  1009          *  individual bounds as superclass, respectively interfaces.
  1010          *  The class type then has as `tsym' a compiler generated class `c',
  1011          *  which has a flag COMPOUND and whose owner is the type variable
  1012          *  itself. Furthermore, the erasure_field of the class
  1013          *  points to the first class or interface bound.
  1014          */
  1015         public Type bound = null;
  1017         /** The lower bound of this type variable.
  1018          *  TypeVars don't normally have a lower bound, so it is normally set
  1019          *  to syms.botType.
  1020          *  Subtypes, such as CapturedType, may provide a different value.
  1021          */
  1022         public Type lower;
  1024         public TypeVar(Name name, Symbol owner, Type lower) {
  1025             super(TYPEVAR, null);
  1026             tsym = new TypeSymbol(0, name, this, owner);
  1027             this.lower = lower;
  1030         public TypeVar(TypeSymbol tsym, Type bound, Type lower) {
  1031             super(TYPEVAR, tsym);
  1032             this.bound = bound;
  1033             this.lower = lower;
  1036         @Override
  1037         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1038             return v.visitTypeVar(this, s);
  1041         @Override
  1042         public Type getUpperBound() { return bound; }
  1044         int rank_field = -1;
  1046         @Override
  1047         public Type getLowerBound() {
  1048             return lower;
  1051         public TypeKind getKind() {
  1052             return TypeKind.TYPEVAR;
  1055         public boolean isCaptured() {
  1056             return false;
  1059         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1060             return v.visitTypeVariable(this, p);
  1064     /** A captured type variable comes from wildcards which can have
  1065      *  both upper and lower bound.  CapturedType extends TypeVar with
  1066      *  a lower bound.
  1067      */
  1068     public static class CapturedType extends TypeVar {
  1070         public WildcardType wildcard;
  1072         public CapturedType(Name name,
  1073                             Symbol owner,
  1074                             Type upper,
  1075                             Type lower,
  1076                             WildcardType wildcard) {
  1077             super(name, owner, lower);
  1078             this.lower = Assert.checkNonNull(lower);
  1079             this.bound = upper;
  1080             this.wildcard = wildcard;
  1083         @Override
  1084         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1085             return v.visitCapturedType(this, s);
  1088         @Override
  1089         public boolean isCaptured() {
  1090             return true;
  1093         @Override
  1094         public String toString() {
  1095             return "capture#"
  1096                 + (hashCode() & 0xFFFFFFFFL) % Printer.PRIME
  1097                 + " of "
  1098                 + wildcard;
  1102     public static abstract class DelegatedType extends Type {
  1103         public Type qtype;
  1104         public DelegatedType(int tag, Type qtype) {
  1105             super(tag, qtype.tsym);
  1106             this.qtype = qtype;
  1108         public String toString() { return qtype.toString(); }
  1109         public List<Type> getTypeArguments() { return qtype.getTypeArguments(); }
  1110         public Type getEnclosingType() { return qtype.getEnclosingType(); }
  1111         public List<Type> getParameterTypes() { return qtype.getParameterTypes(); }
  1112         public Type getReturnType() { return qtype.getReturnType(); }
  1113         public List<Type> getThrownTypes() { return qtype.getThrownTypes(); }
  1114         public List<Type> allparams() { return qtype.allparams(); }
  1115         public Type getUpperBound() { return qtype.getUpperBound(); }
  1116         public boolean isErroneous() { return qtype.isErroneous(); }
  1119     /**
  1120      * The type of a generic method type. It consists of a method type and
  1121      * a list of method type-parameters that are used within the method
  1122      * type.
  1123      */
  1124     public static class ForAll extends DelegatedType implements ExecutableType {
  1125         public List<Type> tvars;
  1127         public ForAll(List<Type> tvars, Type qtype) {
  1128             super(FORALL, (MethodType)qtype);
  1129             this.tvars = tvars;
  1132         @Override
  1133         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1134             return v.visitForAll(this, s);
  1137         public String toString() {
  1138             return "<" + tvars + ">" + qtype;
  1141         public List<Type> getTypeArguments()   { return tvars; }
  1143         public boolean isErroneous()  {
  1144             return qtype.isErroneous();
  1147         public Type map(Mapping f) {
  1148             return f.apply(qtype);
  1151         public boolean contains(Type elem) {
  1152             return qtype.contains(elem);
  1155         public MethodType asMethodType() {
  1156             return (MethodType)qtype;
  1159         public void complete() {
  1160             for (List<Type> l = tvars; l.nonEmpty(); l = l.tail) {
  1161                 ((TypeVar)l.head).bound.complete();
  1163             qtype.complete();
  1166         public List<TypeVar> getTypeVariables() {
  1167             return List.convert(TypeVar.class, getTypeArguments());
  1170         public TypeKind getKind() {
  1171             return TypeKind.EXECUTABLE;
  1174         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1175             return v.visitExecutable(this, p);
  1179     /** A class for inference variables, for use during method/diamond type
  1180      *  inference. An inference variable has upper/lower bounds and a set
  1181      *  of equality constraints. Such bounds are set during subtyping, type-containment,
  1182      *  type-equality checks, when the types being tested contain inference variables.
  1183      *  A change listener can be attached to an inference variable, to receive notifications
  1184      *  whenever the bounds of an inference variable change.
  1185      */
  1186     public static class UndetVar extends DelegatedType {
  1188         /** Inference variable change listener. The listener method is called
  1189          *  whenever a change to the inference variable's bounds occurs
  1190          */
  1191         public interface UndetVarListener {
  1192             /** called when some inference variable bounds (of given kinds ibs) change */
  1193             void varChanged(UndetVar uv, Set<InferenceBound> ibs);
  1196         /**
  1197          * Inference variable bound kinds
  1198          */
  1199         public enum InferenceBound {
  1200             /** upper bounds */
  1201             UPPER,
  1202             /** lower bounds */
  1203             LOWER,
  1204             /** equality constraints */
  1205             EQ;
  1208         /** inference variable bounds */
  1209         private Map<InferenceBound, List<Type>> bounds;
  1211         /** inference variable's inferred type (set from Infer.java) */
  1212         public Type inst = null;
  1214         /** inference variable's change listener */
  1215         public UndetVarListener listener = null;
  1217         @Override
  1218         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1219             return v.visitUndetVar(this, s);
  1222         public UndetVar(TypeVar origin, Types types) {
  1223             this(origin, types, true);
  1226         public UndetVar(TypeVar origin, Types types, boolean includeBounds) {
  1227             super(UNDETVAR, origin);
  1228             bounds = new EnumMap<InferenceBound, List<Type>>(InferenceBound.class);
  1229             bounds.put(InferenceBound.UPPER, includeBounds ? types.getBounds(origin) : List.<Type>nil());
  1230             bounds.put(InferenceBound.LOWER, List.<Type>nil());
  1231             bounds.put(InferenceBound.EQ, List.<Type>nil());
  1234         public String toString() {
  1235             if (inst != null) return inst.toString();
  1236             else return qtype + "?";
  1239         public Type baseType() {
  1240             if (inst != null) return inst.baseType();
  1241             else return this;
  1244         /** get all bounds of a given kind */
  1245         public List<Type> getBounds(InferenceBound ib) {
  1246             return bounds.get(ib);
  1249         /** add a bound of a given kind - this might trigger listener notification */
  1250         public void addBound(InferenceBound ib, Type bound, Types types) {
  1251             List<Type> prevBounds = bounds.get(ib);
  1252             for (Type b : prevBounds) {
  1253                 if (types.isSameType(b, bound)) {
  1254                     return;
  1257             bounds.put(ib, prevBounds.prepend(bound));
  1258             notifyChange(EnumSet.of(ib));
  1261         /** replace types in all bounds - this might trigger listener notification */
  1262         public void substBounds(List<Type> from, List<Type> to, Types types) {
  1263             EnumSet<InferenceBound> changed = EnumSet.noneOf(InferenceBound.class);
  1264             Map<InferenceBound, List<Type>> bounds2 = new EnumMap<InferenceBound, List<Type>>(InferenceBound.class);
  1265             for (Map.Entry<InferenceBound, List<Type>> _entry : bounds.entrySet()) {
  1266                 InferenceBound ib = _entry.getKey();
  1267                 List<Type> prevBounds = _entry.getValue();
  1268                 List<Type> newBounds = types.subst(prevBounds, from, to);
  1269                 bounds2.put(ib, newBounds);
  1270                 if (prevBounds != newBounds) {
  1271                     changed.add(ib);
  1274             if (!changed.isEmpty()) {
  1275                 bounds = bounds2;
  1276                 notifyChange(changed);
  1280         private void notifyChange(EnumSet<InferenceBound> ibs) {
  1281             if (listener != null) {
  1282                 listener.varChanged(this, ibs);
  1287     /** Represents VOID or NONE.
  1288      */
  1289     static class JCNoType extends Type implements NoType {
  1290         public JCNoType(int tag) {
  1291             super(tag, null);
  1294         @Override
  1295         public TypeKind getKind() {
  1296             switch (tag) {
  1297             case VOID:  return TypeKind.VOID;
  1298             case NONE:  return TypeKind.NONE;
  1299             default:
  1300                 throw new AssertionError("Unexpected tag: " + tag);
  1304         @Override
  1305         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1306             return v.visitNoType(this, p);
  1310     static class BottomType extends Type implements NullType {
  1311         public BottomType() {
  1312             super(TypeTags.BOT, null);
  1315         @Override
  1316         public TypeKind getKind() {
  1317             return TypeKind.NULL;
  1320         @Override
  1321         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1322             return v.visitNull(this, p);
  1325         @Override
  1326         public Type constType(Object value) {
  1327             return this;
  1330         @Override
  1331         public String stringValue() {
  1332             return "null";
  1336     public static class ErrorType extends ClassType
  1337             implements javax.lang.model.type.ErrorType {
  1339         private Type originalType = null;
  1341         public ErrorType(Type originalType, TypeSymbol tsym) {
  1342             super(noType, List.<Type>nil(), null);
  1343             tag = ERROR;
  1344             this.tsym = tsym;
  1345             this.originalType = (originalType == null ? noType : originalType);
  1348         public ErrorType(ClassSymbol c, Type originalType) {
  1349             this(originalType, c);
  1350             c.type = this;
  1351             c.kind = ERR;
  1352             c.members_field = new Scope.ErrorScope(c);
  1355         public ErrorType(Name name, TypeSymbol container, Type originalType) {
  1356             this(new ClassSymbol(PUBLIC|STATIC|ACYCLIC, name, null, container), originalType);
  1359         @Override
  1360         public <R,S> R accept(Type.Visitor<R,S> v, S s) {
  1361             return v.visitErrorType(this, s);
  1364         public Type constType(Object constValue) { return this; }
  1365         public Type getEnclosingType()          { return this; }
  1366         public Type getReturnType()              { return this; }
  1367         public Type asSub(Symbol sym)            { return this; }
  1368         public Type map(Mapping f)               { return this; }
  1370         public boolean isGenType(Type t)         { return true; }
  1371         public boolean isErroneous()             { return true; }
  1372         public boolean isCompound()              { return false; }
  1373         public boolean isInterface()             { return false; }
  1375         public List<Type> allparams()            { return List.nil(); }
  1376         public List<Type> getTypeArguments()     { return List.nil(); }
  1378         public TypeKind getKind() {
  1379             return TypeKind.ERROR;
  1382         public Type getOriginalType() {
  1383             return originalType;
  1386         public <R, P> R accept(TypeVisitor<R, P> v, P p) {
  1387             return v.visitError(this, p);
  1391     /**
  1392      * A visitor for types.  A visitor is used to implement operations
  1393      * (or relations) on types.  Most common operations on types are
  1394      * binary relations and this interface is designed for binary
  1395      * relations, that is, operations on the form
  1396      * Type&nbsp;&times;&nbsp;S&nbsp;&rarr;&nbsp;R.
  1397      * <!-- In plain text: Type x S -> R -->
  1399      * @param <R> the return type of the operation implemented by this
  1400      * visitor; use Void if no return type is needed.
  1401      * @param <S> the type of the second argument (the first being the
  1402      * type itself) of the operation implemented by this visitor; use
  1403      * Void if a second argument is not needed.
  1404      */
  1405     public interface Visitor<R,S> {
  1406         R visitClassType(ClassType t, S s);
  1407         R visitWildcardType(WildcardType t, S s);
  1408         R visitArrayType(ArrayType t, S s);
  1409         R visitMethodType(MethodType t, S s);
  1410         R visitPackageType(PackageType t, S s);
  1411         R visitTypeVar(TypeVar t, S s);
  1412         R visitCapturedType(CapturedType t, S s);
  1413         R visitForAll(ForAll t, S s);
  1414         R visitUndetVar(UndetVar t, S s);
  1415         R visitErrorType(ErrorType t, S s);
  1416         R visitType(Type t, S s);

mercurial