< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java

Print this page

  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  */
  25 
  26 package com.sun.tools.javac.code;
  27 
  28 import java.lang.ref.SoftReference;
  29 import java.lang.runtime.ExactConversionsSupport;
  30 import java.util.HashSet;
  31 import java.util.HashMap;
  32 import java.util.Locale;
  33 import java.util.Map;
  34 import java.util.Optional;
  35 import java.util.Set;
  36 import java.util.WeakHashMap;

  37 import java.util.function.BiPredicate;
  38 import java.util.function.Function;
  39 import java.util.function.Predicate;
  40 import java.util.stream.Collector;
  41 
  42 import javax.tools.JavaFileObject;
  43 
  44 import com.sun.tools.javac.code.Attribute.RetentionPolicy;
  45 import com.sun.tools.javac.code.Lint.LintCategory;
  46 import com.sun.tools.javac.code.Type.UndetVar.InferenceBound;
  47 import com.sun.tools.javac.code.TypeMetadata.Annotations;
  48 import com.sun.tools.javac.comp.AttrContext;
  49 import com.sun.tools.javac.comp.Check;
  50 import com.sun.tools.javac.comp.Enter;
  51 import com.sun.tools.javac.comp.Env;
  52 import com.sun.tools.javac.jvm.ClassFile;
  53 import com.sun.tools.javac.util.*;
  54 
  55 import static com.sun.tools.javac.code.BoundKind.*;
  56 import static com.sun.tools.javac.code.Flags.*;

 104 
 105     // <editor-fold defaultstate="collapsed" desc="Instantiating">
 106     public static Types instance(Context context) {
 107         Types instance = context.get(typesKey);
 108         if (instance == null)
 109             instance = new Types(context);
 110         return instance;
 111     }
 112 
 113     @SuppressWarnings("this-escape")
 114     protected Types(Context context) {
 115         context.put(typesKey, this);
 116         syms = Symtab.instance(context);
 117         names = Names.instance(context);
 118         Source source = Source.instance(context);
 119         chk = Check.instance(context);
 120         enter = Enter.instance(context);
 121         capturedName = names.fromString("<captured wildcard>");
 122         messages = JavacMessages.instance(context);
 123         diags = JCDiagnostic.Factory.instance(context);
 124         noWarnings = new Warner(null);





 125         Options options = Options.instance(context);
 126         dumpStacktraceOnError = options.isSet("dev") || options.isSet(DOE);
 127     }
 128     // </editor-fold>
 129 
 130     // <editor-fold defaultstate="collapsed" desc="bounds">
 131     /**
 132      * Get a wildcard's upper bound, returning non-wildcards unchanged.
 133      * @param t a type argument, either a wildcard or a type
 134      */
 135     public Type wildUpperBound(Type t) {
 136         if (t.hasTag(WILDCARD)) {
 137             WildcardType w = (WildcardType) t;
 138             if (w.isSuperBound())
 139                 return w.bound == null ? syms.objectType : w.bound.getUpperBound();
 140             else
 141                 return wildUpperBound(w.type);
 142         }
 143         else return t;
 144     }

2140 
2141     /**
2142      * The number of dimensions of an array type.
2143      */
2144     public int dimensions(Type t) {
2145         int result = 0;
2146         while (t.hasTag(ARRAY)) {
2147             result++;
2148             t = elemtype(t);
2149         }
2150         return result;
2151     }
2152 
2153     /**
2154      * Returns an ArrayType with the component type t
2155      *
2156      * @param t The component type of the ArrayType
2157      * @return the ArrayType for the given component
2158      */
2159     public ArrayType makeArrayType(Type t) {




2160         if (t.hasTag(VOID) || t.hasTag(PACKAGE)) {
2161             Assert.error("Type t must not be a VOID or PACKAGE type, " + t.toString());
2162         }
2163         return new ArrayType(t, syms.arrayClass);




2164     }
2165     // </editor-fold>
2166 
2167     // <editor-fold defaultstate="collapsed" desc="asSuper">
2168     /**
2169      * Return the (most specific) base type of t that starts with the
2170      * given symbol.  If none exists, return null.
2171      *
2172      * Caveat Emptor: Since javac represents the class of all arrays with a singleton
2173      * symbol Symtab.arrayClass, which by being a singleton cannot hold any discriminant,
2174      * this method could yield surprising answers when invoked on arrays. For example when
2175      * invoked with t being byte [] and sym being t.sym itself, asSuper would answer null.
2176      *
2177      * @param t a type
2178      * @param sym a symbol
2179      */
2180     public Type asSuper(Type t, Symbol sym) {
2181         /* Some examples:
2182          *
2183          * (Enum<E>, Comparable) => Comparable<E>

3939                         m = new WildcardType(lub(wildUpperBound(act1.head),
3940                                                  wildUpperBound(act2.head)),
3941                                              BoundKind.EXTENDS,
3942                                              syms.boundClass);
3943                         mergeCache.remove(pair);
3944                     } else {
3945                         m = new WildcardType(syms.objectType,
3946                                              BoundKind.UNBOUND,
3947                                              syms.boundClass);
3948                     }
3949                     merged.append(m.withTypeVar(typarams.head));
3950                 }
3951                 act1 = act1.tail;
3952                 act2 = act2.tail;
3953                 typarams = typarams.tail;
3954             }
3955             Assert.check(act1.isEmpty() && act2.isEmpty() && typarams.isEmpty());
3956             // There is no spec detailing how type annotations are to
3957             // be inherited.  So set it to noAnnotations for now
3958             return new ClassType(class1.getEnclosingType(), merged.toList(),
3959                                  class1.tsym);
3960         }
3961 
3962     /**
3963      * Return the minimum type of a closure, a compound type if no
3964      * unique minimum exists.
3965      */
3966     private Type compoundMin(List<Type> cl) {
3967         if (cl.isEmpty()) return syms.objectType;
3968         List<Type> compound = closureMin(cl);
3969         if (compound.isEmpty())
3970             return null;
3971         else if (compound.tail.isEmpty())
3972             return compound.head;
3973         else
3974             return makeIntersectionType(compound);
3975     }
3976 
3977     /**
3978      * Return the minimum types of a closure, suitable for computing
3979      * compoundMin or glb.

4922     private WildcardType makeSuperWildcard(Type bound, TypeVar formal) {
4923         if (bound.hasTag(BOT)) {
4924             return new WildcardType(syms.objectType,
4925                                     BoundKind.UNBOUND,
4926                                     syms.boundClass,
4927                                     formal);
4928         } else {
4929             return new WildcardType(bound,
4930                                     BoundKind.SUPER,
4931                                     syms.boundClass,
4932                                     formal);
4933         }
4934     }
4935 
4936     /**
4937      * A wrapper for a type that allows use in sets.
4938      */
4939     public static class UniqueType {
4940         public final Type type;
4941         final Types types;

4942 
4943         public UniqueType(Type type, Types types) {
4944             this.type = type;
4945             this.types = types;





4946         }
4947 
4948         public int hashCode() {
4949             return types.hashCode(type);
4950         }
4951 
4952         public boolean equals(Object obj) {
4953             return (obj instanceof UniqueType uniqueType) &&
4954                     types.isSameType(type, uniqueType.type);
4955         }
4956 




4957         public String toString() {
4958             return type.toString();
4959         }
4960 
4961     }
4962     // </editor-fold>
4963 
4964     // <editor-fold defaultstate="collapsed" desc="Visitors">
4965     /**
4966      * A default visitor for types.  All visitor methods except
4967      * visitType are implemented by delegating to visitType.  Concrete
4968      * subclasses must provide an implementation of visitType and can
4969      * override other methods as needed.
4970      *
4971      * @param <R> the return type of the operation implemented by this
4972      * visitor; use Void if no return type is needed.
4973      * @param <S> the type of the second argument (the first being the
4974      * type itself) of the operation implemented by this visitor; use
4975      * Void if a second argument is not needed.
4976      */

  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  */
  25 
  26 package com.sun.tools.javac.code;
  27 
  28 import java.lang.ref.SoftReference;
  29 import java.lang.runtime.ExactConversionsSupport;
  30 import java.util.HashSet;
  31 import java.util.HashMap;
  32 import java.util.Locale;
  33 import java.util.Map;
  34 import java.util.Optional;
  35 import java.util.Set;
  36 import java.util.WeakHashMap;
  37 import java.util.function.BiFunction;
  38 import java.util.function.BiPredicate;
  39 import java.util.function.Function;
  40 import java.util.function.Predicate;
  41 import java.util.stream.Collector;
  42 
  43 import javax.tools.JavaFileObject;
  44 
  45 import com.sun.tools.javac.code.Attribute.RetentionPolicy;
  46 import com.sun.tools.javac.code.Lint.LintCategory;
  47 import com.sun.tools.javac.code.Type.UndetVar.InferenceBound;
  48 import com.sun.tools.javac.code.TypeMetadata.Annotations;
  49 import com.sun.tools.javac.comp.AttrContext;
  50 import com.sun.tools.javac.comp.Check;
  51 import com.sun.tools.javac.comp.Enter;
  52 import com.sun.tools.javac.comp.Env;
  53 import com.sun.tools.javac.jvm.ClassFile;
  54 import com.sun.tools.javac.util.*;
  55 
  56 import static com.sun.tools.javac.code.BoundKind.*;
  57 import static com.sun.tools.javac.code.Flags.*;

 105 
 106     // <editor-fold defaultstate="collapsed" desc="Instantiating">
 107     public static Types instance(Context context) {
 108         Types instance = context.get(typesKey);
 109         if (instance == null)
 110             instance = new Types(context);
 111         return instance;
 112     }
 113 
 114     @SuppressWarnings("this-escape")
 115     protected Types(Context context) {
 116         context.put(typesKey, this);
 117         syms = Symtab.instance(context);
 118         names = Names.instance(context);
 119         Source source = Source.instance(context);
 120         chk = Check.instance(context);
 121         enter = Enter.instance(context);
 122         capturedName = names.fromString("<captured wildcard>");
 123         messages = JavacMessages.instance(context);
 124         diags = JCDiagnostic.Factory.instance(context);
 125         noWarnings = new Warner(null) {
 126             @Override
 127             public String toString() {
 128                 return "NO_WARNINGS";
 129             }
 130         };
 131         Options options = Options.instance(context);
 132         dumpStacktraceOnError = options.isSet("dev") || options.isSet(DOE);
 133     }
 134     // </editor-fold>
 135 
 136     // <editor-fold defaultstate="collapsed" desc="bounds">
 137     /**
 138      * Get a wildcard's upper bound, returning non-wildcards unchanged.
 139      * @param t a type argument, either a wildcard or a type
 140      */
 141     public Type wildUpperBound(Type t) {
 142         if (t.hasTag(WILDCARD)) {
 143             WildcardType w = (WildcardType) t;
 144             if (w.isSuperBound())
 145                 return w.bound == null ? syms.objectType : w.bound.getUpperBound();
 146             else
 147                 return wildUpperBound(w.type);
 148         }
 149         else return t;
 150     }

2146 
2147     /**
2148      * The number of dimensions of an array type.
2149      */
2150     public int dimensions(Type t) {
2151         int result = 0;
2152         while (t.hasTag(ARRAY)) {
2153             result++;
2154             t = elemtype(t);
2155         }
2156         return result;
2157     }
2158 
2159     /**
2160      * Returns an ArrayType with the component type t
2161      *
2162      * @param t The component type of the ArrayType
2163      * @return the ArrayType for the given component
2164      */
2165     public ArrayType makeArrayType(Type t) {
2166         return makeArrayType(t, 1);
2167     }
2168 
2169     public ArrayType makeArrayType(Type t, int dimensions) {
2170         if (t.hasTag(VOID) || t.hasTag(PACKAGE)) {
2171             Assert.error("Type t must not be a VOID or PACKAGE type, " + t.toString());
2172         }
2173         ArrayType result = new ArrayType(t, syms.arrayClass);
2174         for (int i = 1; i < dimensions; i++) {
2175             result = new ArrayType(result, syms.arrayClass);
2176         }
2177         return result;
2178     }
2179     // </editor-fold>
2180 
2181     // <editor-fold defaultstate="collapsed" desc="asSuper">
2182     /**
2183      * Return the (most specific) base type of t that starts with the
2184      * given symbol.  If none exists, return null.
2185      *
2186      * Caveat Emptor: Since javac represents the class of all arrays with a singleton
2187      * symbol Symtab.arrayClass, which by being a singleton cannot hold any discriminant,
2188      * this method could yield surprising answers when invoked on arrays. For example when
2189      * invoked with t being byte [] and sym being t.sym itself, asSuper would answer null.
2190      *
2191      * @param t a type
2192      * @param sym a symbol
2193      */
2194     public Type asSuper(Type t, Symbol sym) {
2195         /* Some examples:
2196          *
2197          * (Enum<E>, Comparable) => Comparable<E>

3953                         m = new WildcardType(lub(wildUpperBound(act1.head),
3954                                                  wildUpperBound(act2.head)),
3955                                              BoundKind.EXTENDS,
3956                                              syms.boundClass);
3957                         mergeCache.remove(pair);
3958                     } else {
3959                         m = new WildcardType(syms.objectType,
3960                                              BoundKind.UNBOUND,
3961                                              syms.boundClass);
3962                     }
3963                     merged.append(m.withTypeVar(typarams.head));
3964                 }
3965                 act1 = act1.tail;
3966                 act2 = act2.tail;
3967                 typarams = typarams.tail;
3968             }
3969             Assert.check(act1.isEmpty() && act2.isEmpty() && typarams.isEmpty());
3970             // There is no spec detailing how type annotations are to
3971             // be inherited.  So set it to noAnnotations for now
3972             return new ClassType(class1.getEnclosingType(), merged.toList(),
3973                                  class1.tsym, List.nil());
3974         }
3975 
3976     /**
3977      * Return the minimum type of a closure, a compound type if no
3978      * unique minimum exists.
3979      */
3980     private Type compoundMin(List<Type> cl) {
3981         if (cl.isEmpty()) return syms.objectType;
3982         List<Type> compound = closureMin(cl);
3983         if (compound.isEmpty())
3984             return null;
3985         else if (compound.tail.isEmpty())
3986             return compound.head;
3987         else
3988             return makeIntersectionType(compound);
3989     }
3990 
3991     /**
3992      * Return the minimum types of a closure, suitable for computing
3993      * compoundMin or glb.

4936     private WildcardType makeSuperWildcard(Type bound, TypeVar formal) {
4937         if (bound.hasTag(BOT)) {
4938             return new WildcardType(syms.objectType,
4939                                     BoundKind.UNBOUND,
4940                                     syms.boundClass,
4941                                     formal);
4942         } else {
4943             return new WildcardType(bound,
4944                                     BoundKind.SUPER,
4945                                     syms.boundClass,
4946                                     formal);
4947         }
4948     }
4949 
4950     /**
4951      * A wrapper for a type that allows use in sets.
4952      */
4953     public static class UniqueType {
4954         public final Type type;
4955         final Types types;
4956         private boolean encodeTypeSig;
4957 
4958         public UniqueType(Type type, Types types, boolean encodeTypeSig) {
4959             this.type = type;
4960             this.types = types;
4961             this.encodeTypeSig = encodeTypeSig;
4962         }
4963 
4964         public UniqueType(Type type, Types types) {
4965             this(type, types, true);
4966         }
4967 
4968         public int hashCode() {
4969             return types.hashCode(type);
4970         }
4971 
4972         public boolean equals(Object obj) {
4973             return (obj instanceof UniqueType uniqueType) &&
4974                     types.isSameType(type, uniqueType.type);
4975         }
4976 
4977         public boolean encodeTypeSig() {
4978             return encodeTypeSig;
4979         }
4980 
4981         public String toString() {
4982             return type.toString();
4983         }
4984 
4985     }
4986     // </editor-fold>
4987 
4988     // <editor-fold defaultstate="collapsed" desc="Visitors">
4989     /**
4990      * A default visitor for types.  All visitor methods except
4991      * visitType are implemented by delegating to visitType.  Concrete
4992      * subclasses must provide an implementation of visitType and can
4993      * override other methods as needed.
4994      *
4995      * @param <R> the return type of the operation implemented by this
4996      * visitor; use Void if no return type is needed.
4997      * @param <S> the type of the second argument (the first being the
4998      * type itself) of the operation implemented by this visitor; use
4999      * Void if a second argument is not needed.
5000      */
< prev index next >