< prev index next >

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

Print this page




  31 import java.util.Locale;
  32 import java.util.Map;
  33 import java.util.Optional;
  34 import java.util.Set;
  35 import java.util.WeakHashMap;
  36 import java.util.function.BiPredicate;
  37 import java.util.function.Function;
  38 import java.util.stream.Collector;
  39 
  40 import javax.tools.JavaFileObject;
  41 
  42 import com.sun.tools.javac.code.Attribute.RetentionPolicy;
  43 import com.sun.tools.javac.code.Lint.LintCategory;
  44 import com.sun.tools.javac.code.Source.Feature;
  45 import com.sun.tools.javac.code.Type.UndetVar.InferenceBound;
  46 import com.sun.tools.javac.code.TypeMetadata.Entry.Kind;
  47 import com.sun.tools.javac.comp.AttrContext;
  48 import com.sun.tools.javac.comp.Check;
  49 import com.sun.tools.javac.comp.Enter;
  50 import com.sun.tools.javac.comp.Env;
  51 import com.sun.tools.javac.comp.LambdaToMethod;
  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.*;
  57 import static com.sun.tools.javac.code.Kinds.Kind.*;
  58 import static com.sun.tools.javac.code.Scope.*;
  59 import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
  60 import static com.sun.tools.javac.code.Symbol.*;
  61 import static com.sun.tools.javac.code.Type.*;
  62 import static com.sun.tools.javac.code.TypeTag.*;
  63 import static com.sun.tools.javac.jvm.ClassFile.externalize;
  64 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
  65 
  66 /**
  67  * Utility class containing various operations on types.
  68  *
  69  * <p>Unless other names are more illustrative, the following naming
  70  * conventions should be observed in this file:
  71  *
  72  * <dl>


  83  * <p><b>This is NOT part of any supported API.
  84  * If you write code that depends on this, you do so at your own risk.
  85  * This code and its internal interfaces are subject to change or
  86  * deletion without notice.</b>
  87  */
  88 public class Types {
  89     protected static final Context.Key<Types> typesKey = new Context.Key<>();
  90 
  91     final Symtab syms;
  92     final JavacMessages messages;
  93     final Names names;
  94     final boolean allowDefaultMethods;
  95     final boolean mapCapturesToBounds;
  96     final Check chk;
  97     final Enter enter;
  98     JCDiagnostic.Factory diags;
  99     List<Warner> warnStack = List.nil();
 100     final Name capturedName;
 101 
 102     public final Warner noWarnings;


 103 
 104     // <editor-fold defaultstate="collapsed" desc="Instantiating">
 105     public static Types instance(Context context) {
 106         Types instance = context.get(typesKey);
 107         if (instance == null)
 108             instance = new Types(context);
 109         return instance;
 110     }
 111 
 112     protected Types(Context context) {
 113         context.put(typesKey, this);
 114         syms = Symtab.instance(context);
 115         names = Names.instance(context);
 116         Source source = Source.instance(context);
 117         allowDefaultMethods = Feature.DEFAULT_METHODS.allowedInSource(source);
 118         mapCapturesToBounds = Feature.MAP_CAPTURES_TO_BOUNDS.allowedInSource(source);
 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     }
 126     // </editor-fold>
 127 
 128     // <editor-fold defaultstate="collapsed" desc="bounds">
 129     /**
 130      * Get a wildcard's upper bound, returning non-wildcards unchanged.
 131      * @param t a type argument, either a wildcard or a type
 132      */
 133     public Type wildUpperBound(Type t) {
 134         if (t.hasTag(WILDCARD)) {
 135             WildcardType w = (WildcardType) t;
 136             if (w.isSuperBound())
 137                 return w.bound == null ? syms.objectType : w.bound.getUpperBound();
 138             else
 139                 return wildUpperBound(w.type);
 140         }
 141         else return t;
 142     }
 143 
 144     /**


1304     public boolean isSameTypes(List<Type> ts, List<Type> ss) {
1305         while (ts.tail != null && ss.tail != null
1306                /*inlined: ts.nonEmpty() && ss.nonEmpty()*/ &&
1307                isSameType(ts.head, ss.head)) {
1308             ts = ts.tail;
1309             ss = ss.tail;
1310         }
1311         return ts.tail == null && ss.tail == null;
1312         /*inlined: ts.isEmpty() && ss.isEmpty();*/
1313     }
1314 
1315     /**
1316      * A polymorphic signature method (JLS 15.12.3) is a method that
1317      *   (i) is declared in the java.lang.invoke.MethodHandle/VarHandle classes;
1318      *  (ii) takes a single variable arity parameter;
1319      * (iii) whose declared type is Object[];
1320      *  (iv) has any return type, Object signifying a polymorphic return type; and
1321      *   (v) is native.
1322     */
1323    public boolean isSignaturePolymorphic(MethodSymbol msym) {



1324        List<Type> argtypes = msym.type.getParameterTypes();
1325        return (msym.flags_field & NATIVE) != 0 &&
1326               (msym.owner == syms.methodHandleType.tsym || msym.owner == syms.varHandleType.tsym) &&
1327                argtypes.length() == 1 &&
1328                argtypes.head.hasTag(TypeTag.ARRAY) &&
1329                ((ArrayType)argtypes.head).elemtype.tsym == syms.objectType.tsym;
1330    }
1331 
1332     /**
1333      * Is t the same type as s?
1334      */
1335     public boolean isSameType(Type t, Type s) {
1336         return isSameTypeVisitor.visit(t, s);
1337     }
1338     // where
1339 
1340         /**
1341          * Type-equality relation - type variables are considered
1342          * equals if they share the same object identity.
1343          */


5164             append('<');
5165             for (List<Type> ts = typarams; ts.nonEmpty(); ts = ts.tail) {
5166                 Type.TypeVar tvar = (Type.TypeVar) ts.head;
5167                 append(tvar.tsym.name);
5168                 List<Type> bounds = types.getBounds(tvar);
5169                 if ((bounds.head.tsym.flags() & INTERFACE) != 0) {
5170                     append(':');
5171                 }
5172                 for (List<Type> l = bounds; l.nonEmpty(); l = l.tail) {
5173                     append(':');
5174                     assembleSig(l.head);
5175                 }
5176             }
5177             append('>');
5178         }
5179 
5180         private void assembleSig(List<Type> types) {
5181             for (List<Type> ts = types; ts.nonEmpty(); ts = ts.tail) {
5182                 assembleSig(ts.head);
5183             }
5184         }
5185     }
5186 
5187     public Type constantType(LoadableConstant c) {
5188         switch (c.poolTag()) {
5189             case ClassFile.CONSTANT_Class:
5190                 return syms.classType;
5191             case ClassFile.CONSTANT_String:
5192                 return syms.stringType;
5193             case ClassFile.CONSTANT_Integer:
5194                 return syms.intType;
5195             case ClassFile.CONSTANT_Float:
5196                 return syms.floatType;
5197             case ClassFile.CONSTANT_Long:
5198                 return syms.longType;
5199             case ClassFile.CONSTANT_Double:
5200                 return syms.doubleType;
5201             case ClassFile.CONSTANT_MethodHandle:
5202                 return syms.methodHandleType;
5203             case ClassFile.CONSTANT_MethodType:
5204                 return syms.methodTypeType;
5205             default:
5206                 throw new AssertionError("Not a loadable constant: " + c.poolTag());
5207         }
5208     }
5209     // </editor-fold>
5210 
5211     public void newRound() {
5212         descCache._map.clear();
5213         isDerivedRawCache.clear();
5214         implCache._map.clear();
5215         membersCache._map.clear();
5216         closureCache.clear();
5217     }
5218 }


  31 import java.util.Locale;
  32 import java.util.Map;
  33 import java.util.Optional;
  34 import java.util.Set;
  35 import java.util.WeakHashMap;
  36 import java.util.function.BiPredicate;
  37 import java.util.function.Function;
  38 import java.util.stream.Collector;
  39 
  40 import javax.tools.JavaFileObject;
  41 
  42 import com.sun.tools.javac.code.Attribute.RetentionPolicy;
  43 import com.sun.tools.javac.code.Lint.LintCategory;
  44 import com.sun.tools.javac.code.Source.Feature;
  45 import com.sun.tools.javac.code.Type.UndetVar.InferenceBound;
  46 import com.sun.tools.javac.code.TypeMetadata.Entry.Kind;
  47 import com.sun.tools.javac.comp.AttrContext;
  48 import com.sun.tools.javac.comp.Check;
  49 import com.sun.tools.javac.comp.Enter;
  50 import com.sun.tools.javac.comp.Env;


  51 import com.sun.tools.javac.util.*;
  52 
  53 import static com.sun.tools.javac.code.BoundKind.*;
  54 import static com.sun.tools.javac.code.Flags.*;
  55 import static com.sun.tools.javac.code.Kinds.Kind.*;
  56 import static com.sun.tools.javac.code.Scope.*;
  57 import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
  58 import static com.sun.tools.javac.code.Symbol.*;
  59 import static com.sun.tools.javac.code.Type.*;
  60 import static com.sun.tools.javac.code.TypeTag.*;
  61 import static com.sun.tools.javac.jvm.ClassFile.externalize;
  62 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
  63 
  64 /**
  65  * Utility class containing various operations on types.
  66  *
  67  * <p>Unless other names are more illustrative, the following naming
  68  * conventions should be observed in this file:
  69  *
  70  * <dl>


  81  * <p><b>This is NOT part of any supported API.
  82  * If you write code that depends on this, you do so at your own risk.
  83  * This code and its internal interfaces are subject to change or
  84  * deletion without notice.</b>
  85  */
  86 public class Types {
  87     protected static final Context.Key<Types> typesKey = new Context.Key<>();
  88 
  89     final Symtab syms;
  90     final JavacMessages messages;
  91     final Names names;
  92     final boolean allowDefaultMethods;
  93     final boolean mapCapturesToBounds;
  94     final Check chk;
  95     final Enter enter;
  96     JCDiagnostic.Factory diags;
  97     List<Warner> warnStack = List.nil();
  98     final Name capturedName;
  99 
 100     public final Warner noWarnings;
 101     private final boolean doConstantFold;
 102     private final Constables constables;
 103 
 104     // <editor-fold defaultstate="collapsed" desc="Instantiating">
 105     public static Types instance(Context context) {
 106         Types instance = context.get(typesKey);
 107         if (instance == null)
 108             instance = new Types(context);
 109         return instance;
 110     }
 111 
 112     protected Types(Context context) {
 113         context.put(typesKey, this);
 114         syms = Symtab.instance(context);
 115         names = Names.instance(context);
 116         Source source = Source.instance(context);
 117         allowDefaultMethods = Feature.DEFAULT_METHODS.allowedInSource(source);
 118         mapCapturesToBounds = Feature.MAP_CAPTURES_TO_BOUNDS.allowedInSource(source);
 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         String foldingOp = options.get("folding");
 127         doConstantFold = foldingOp != null ?
 128                 foldingOp.equals("true") :
 129                 Feature.CONSTABLES.allowedInSource(source);
 130         constables = Constables.instance(context);
 131     }
 132     // </editor-fold>
 133 
 134     // <editor-fold defaultstate="collapsed" desc="bounds">
 135     /**
 136      * Get a wildcard's upper bound, returning non-wildcards unchanged.
 137      * @param t a type argument, either a wildcard or a type
 138      */
 139     public Type wildUpperBound(Type t) {
 140         if (t.hasTag(WILDCARD)) {
 141             WildcardType w = (WildcardType) t;
 142             if (w.isSuperBound())
 143                 return w.bound == null ? syms.objectType : w.bound.getUpperBound();
 144             else
 145                 return wildUpperBound(w.type);
 146         }
 147         else return t;
 148     }
 149 
 150     /**


1310     public boolean isSameTypes(List<Type> ts, List<Type> ss) {
1311         while (ts.tail != null && ss.tail != null
1312                /*inlined: ts.nonEmpty() && ss.nonEmpty()*/ &&
1313                isSameType(ts.head, ss.head)) {
1314             ts = ts.tail;
1315             ss = ss.tail;
1316         }
1317         return ts.tail == null && ss.tail == null;
1318         /*inlined: ts.isEmpty() && ss.isEmpty();*/
1319     }
1320 
1321     /**
1322      * A polymorphic signature method (JLS 15.12.3) is a method that
1323      *   (i) is declared in the java.lang.invoke.MethodHandle/VarHandle classes;
1324      *  (ii) takes a single variable arity parameter;
1325      * (iii) whose declared type is Object[];
1326      *  (iv) has any return type, Object signifying a polymorphic return type; and
1327      *   (v) is native.
1328     */
1329    public boolean isSignaturePolymorphic(MethodSymbol msym) {
1330        if (doConstantFold && constables.isIntrinsicsIndy(msym)) {
1331            return true;
1332        }
1333        List<Type> argtypes = msym.type.getParameterTypes();
1334        return (msym.flags_field & NATIVE) != 0 &&
1335               (msym.owner == syms.methodHandleType.tsym || msym.owner == syms.varHandleType.tsym) &&
1336                argtypes.length() == 1 &&
1337                argtypes.head.hasTag(TypeTag.ARRAY) &&
1338                ((ArrayType)argtypes.head).elemtype.tsym == syms.objectType.tsym;
1339    }
1340 
1341     /**
1342      * Is t the same type as s?
1343      */
1344     public boolean isSameType(Type t, Type s) {
1345         return isSameTypeVisitor.visit(t, s);
1346     }
1347     // where
1348 
1349         /**
1350          * Type-equality relation - type variables are considered
1351          * equals if they share the same object identity.
1352          */


5173             append('<');
5174             for (List<Type> ts = typarams; ts.nonEmpty(); ts = ts.tail) {
5175                 Type.TypeVar tvar = (Type.TypeVar) ts.head;
5176                 append(tvar.tsym.name);
5177                 List<Type> bounds = types.getBounds(tvar);
5178                 if ((bounds.head.tsym.flags() & INTERFACE) != 0) {
5179                     append(':');
5180                 }
5181                 for (List<Type> l = bounds; l.nonEmpty(); l = l.tail) {
5182                     append(':');
5183                     assembleSig(l.head);
5184                 }
5185             }
5186             append('>');
5187         }
5188 
5189         private void assembleSig(List<Type> types) {
5190             for (List<Type> ts = types; ts.nonEmpty(); ts = ts.tail) {
5191                 assembleSig(ts.head);
5192             }























5193         }
5194     }
5195     // </editor-fold>
5196 
5197     public void newRound() {
5198         descCache._map.clear();
5199         isDerivedRawCache.clear();
5200         implCache._map.clear();
5201         membersCache._map.clear();
5202         closureCache.clear();
5203     }
5204 }
< prev index next >