< prev index next >

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

Print this page




  76  * <dd>Similarly, if the second argument to an operation is a type, it should be named s.</dd>
  77  * <dt>ts</dt>
  78  * <dd>If an operations takes a list of types, the first should be named ts.</dd>
  79  * <dt>ss</dt>
  80  * <dd>A second list of types should be named ss.</dd>
  81  * </dl>
  82  *
  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     /**


 584                 }
 585                 return res;
 586             }
 587 
 588             @Override
 589             public Type visitErrorType(ErrorType t, Symbol sym) {
 590                 return t;
 591             }
 592         };
 593     // </editor-fold>
 594 
 595     // <editor-fold defaultstate="collapsed" desc="isConvertible">
 596     /**
 597      * Is t a subtype of or convertible via boxing/unboxing
 598      * conversion to s?
 599      */
 600     public boolean isConvertible(Type t, Type s, Warner warn) {
 601         if (t.hasTag(ERROR)) {
 602             return true;
 603         }

 604         boolean tPrimitive = t.isPrimitive();
 605         boolean sPrimitive = s.isPrimitive();
 606         if (tPrimitive == sPrimitive) {
 607             return isSubtypeUnchecked(t, s, warn);
 608         }
 609         boolean tUndet = t.hasTag(UNDETVAR);
 610         boolean sUndet = s.hasTag(UNDETVAR);
 611 
 612         if (tUndet || sUndet) {
 613             return tUndet ?
 614                     isSubtype(t, boxedTypeOrType(s)) :
 615                     isSubtype(boxedTypeOrType(t), s);
 616         }
 617 
 618         return tPrimitive
 619             ? isSubtype(boxedClass(t).type, s)
 620             : isSubtype(unboxedType(t), s);
 621     }
 622 
 623     /**


 977     * Scope filter used to skip methods that should be ignored (such as methods
 978     * overridden by j.l.Object) during function interface conversion interface check
 979     */
 980     class DescriptorFilter implements Filter<Symbol> {
 981 
 982        TypeSymbol origin;
 983 
 984        DescriptorFilter(TypeSymbol origin) {
 985            this.origin = origin;
 986        }
 987 
 988        @Override
 989        public boolean accepts(Symbol sym) {
 990            return sym.kind == MTH &&
 991                    (sym.flags() & (ABSTRACT | DEFAULT)) == ABSTRACT &&
 992                    !overridesObjectMethod(origin, sym) &&
 993                    (interfaceCandidates(origin.type, (MethodSymbol)sym).head.flags() & DEFAULT) == 0;
 994        }
 995     }
 996 









































 997     // <editor-fold defaultstate="collapsed" desc="isSubtype">
 998     /**
 999      * Is t an unchecked subtype of s?
1000      */
1001     public boolean isSubtypeUnchecked(Type t, Type s) {
1002         return isSubtypeUnchecked(t, s, noWarnings);
1003     }
1004     /**
1005      * Is t an unchecked subtype of s?
1006      */
1007     public boolean isSubtypeUnchecked(Type t, Type s, Warner warn) {
1008         boolean result = isSubtypeUncheckedInternal(t, s, true, warn);
1009         if (result) {
1010             checkUnsafeVarargsConversion(t, s, warn);
1011         }
1012         return result;
1013     }
1014     //where
1015         private boolean isSubtypeUncheckedInternal(Type t, Type s, boolean capture, Warner warn) {
1016             if (t.hasTag(ARRAY) && s.hasTag(ARRAY)) {
1017                 if (((ArrayType)t).elemtype.isPrimitive()) {
1018                     return isSameType(elemtype(t), elemtype(s));
1019                 } else {
1020                     return isSubtypeUncheckedInternal(elemtype(t), elemtype(s), false, warn);









1021                 }
1022             } else if (isSubtype(t, s, capture)) {
1023                 return true;
1024             } else if (t.hasTag(TYPEVAR)) {
1025                 return isSubtypeUncheckedInternal(t.getUpperBound(), s, false, warn);
1026             } else if (!s.isRaw()) {
1027                 Type t2 = asSuper(t, s.tsym);
1028                 if (t2 != null && t2.isRaw()) {
1029                     if (isReifiable(s)) {
1030                         warn.silentWarn(LintCategory.UNCHECKED);
1031                     } else {
1032                         warn.warn(LintCategory.UNCHECKED);
1033                     }
1034                     return true;
1035                 }
1036             }
1037             return false;
1038         }
1039 
1040         private void checkUnsafeVarargsConversion(Type t, Type s, Warner warn) {


1097     }
1098     // where
1099         private TypeRelation isSubtype = new TypeRelation()
1100         {
1101             @Override
1102             public Boolean visitType(Type t, Type s) {
1103                 switch (t.getTag()) {
1104                  case BYTE:
1105                      return (!s.hasTag(CHAR) && t.getTag().isSubRangeOf(s.getTag()));
1106                  case CHAR:
1107                      return (!s.hasTag(SHORT) && t.getTag().isSubRangeOf(s.getTag()));
1108                  case SHORT: case INT: case LONG:
1109                  case FLOAT: case DOUBLE:
1110                      return t.getTag().isSubRangeOf(s.getTag());
1111                  case BOOLEAN: case VOID:
1112                      return t.hasTag(s.getTag());
1113                  case TYPEVAR:
1114                      return isSubtypeNoCapture(t.getUpperBound(), s);
1115                  case BOT:
1116                      return
1117                          s.hasTag(BOT) || s.hasTag(CLASS) ||
1118                          s.hasTag(ARRAY) || s.hasTag(TYPEVAR);
1119                  case WILDCARD: //we shouldn't be here - avoids crash (see 7034495)
1120                  case NONE:
1121                      return false;
1122                  default:
1123                      throw new AssertionError("isSubtype " + t.getTag());
1124                  }
1125             }
1126 
1127             private Set<TypePair> cache = new HashSet<>();
1128 
1129             private boolean containsTypeRecursive(Type t, Type s) {
1130                 TypePair pair = new TypePair(t, s);
1131                 if (cache.add(pair)) {
1132                     try {
1133                         return containsType(t.getTypeArguments(),
1134                                             s.getTypeArguments());
1135                     } finally {
1136                         cache.remove(pair);
1137                     }


1666                 case DOUBLE:
1667                     return s.isNumeric();
1668                 case BOOLEAN:
1669                     return s.hasTag(BOOLEAN);
1670                 case VOID:
1671                     return false;
1672                 case BOT:
1673                     return isSubtype(t, s);
1674                 default:
1675                     throw new AssertionError();
1676                 }
1677             }
1678 
1679             @Override
1680             public Boolean visitWildcardType(WildcardType t, Type s) {
1681                 return isCastable(wildUpperBound(t), s, warnStack.head);
1682             }
1683 
1684             @Override
1685             public Boolean visitClassType(ClassType t, Type s) {
1686                 if (s.hasTag(ERROR) || s.hasTag(BOT))
1687                     return true;
1688 
1689                 if (s.hasTag(TYPEVAR)) {
1690                     if (isCastable(t, s.getUpperBound(), noWarnings)) {
1691                         warnStack.head.warn(LintCategory.UNCHECKED);
1692                         return true;
1693                     } else {
1694                         return false;
1695                     }
1696                 }
1697 
1698                 if (t.isCompound() || s.isCompound()) {
1699                     return !t.isCompound() ?
1700                             visitCompoundType((ClassType)s, t, true) :
1701                             visitCompoundType(t, s, false);
1702                 }
1703 
1704                 if (s.hasTag(CLASS) || s.hasTag(ARRAY)) {
1705                     boolean upcast;
1706                     if ((upcast = isSubtype(erasure(t), erasure(s)))


1790 
1791             @Override
1792             public Boolean visitArrayType(ArrayType t, Type s) {
1793                 switch (s.getTag()) {
1794                 case ERROR:
1795                 case BOT:
1796                     return true;
1797                 case TYPEVAR:
1798                     if (isCastable(s, t, noWarnings)) {
1799                         warnStack.head.warn(LintCategory.UNCHECKED);
1800                         return true;
1801                     } else {
1802                         return false;
1803                     }
1804                 case CLASS:
1805                     return isSubtype(t, s);
1806                 case ARRAY:
1807                     if (elemtype(t).isPrimitive() || elemtype(s).isPrimitive()) {
1808                         return elemtype(t).hasTag(elemtype(s).getTag());
1809                     } else {
1810                         return visit(elemtype(t), elemtype(s));









1811                     }
1812                 default:
1813                     return false;
1814                 }
1815             }
1816 
1817             @Override
1818             public Boolean visitTypeVar(TypeVar t, Type s) {
1819                 switch (s.getTag()) {
1820                 case ERROR:
1821                 case BOT:
1822                     return true;
1823                 case TYPEVAR:
1824                     if (isSubtype(t, s)) {
1825                         return true;
1826                     } else if (isCastable(t.getUpperBound(), s, noWarnings)) {
1827                         warnStack.head.warn(LintCategory.UNCHECKED);
1828                         return true;
1829                     } else {
1830                         return false;


2107          * (j.u.List<capture#160 of ? extends c.s.s.d.DocTree>, Iterable) =>
2108          *     Iterable<capture#160 of ? extends c.s.s.d.DocTree>
2109          */
2110         if (sym.type == syms.objectType) { //optimization
2111             return syms.objectType;
2112         }
2113         return asSuper.visit(t, sym);
2114     }
2115     // where
2116         private SimpleVisitor<Type,Symbol> asSuper = new SimpleVisitor<Type,Symbol>() {
2117 
2118             public Type visitType(Type t, Symbol sym) {
2119                 return null;
2120             }
2121 
2122             @Override
2123             public Type visitClassType(ClassType t, Symbol sym) {
2124                 if (t.tsym == sym)
2125                     return t;
2126 







2127                 Type st = supertype(t);
2128                 if (st.hasTag(CLASS) || st.hasTag(TYPEVAR)) {
2129                     Type x = asSuper(st, sym);
2130                     if (x != null)
2131                         return x;
2132                 }
2133                 if ((sym.flags() & INTERFACE) != 0) {
2134                     for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail) {
2135                         if (!l.head.hasTag(ERROR)) {
2136                             Type x = asSuper(l.head, sym);
2137                             if (x != null)
2138                                 return x;
2139                         }
2140                     }
2141                 }
2142                 return null;
2143             }
2144 
2145             @Override
2146             public Type visitArrayType(ArrayType t, Symbol sym) {


4800     private WildcardType makeSuperWildcard(Type bound, TypeVar formal) {
4801         if (bound.hasTag(BOT)) {
4802             return new WildcardType(syms.objectType,
4803                                     BoundKind.UNBOUND,
4804                                     syms.boundClass,
4805                                     formal);
4806         } else {
4807             return new WildcardType(bound,
4808                                     BoundKind.SUPER,
4809                                     syms.boundClass,
4810                                     formal);
4811         }
4812     }
4813 
4814     /**
4815      * A wrapper for a type that allows use in sets.
4816      */
4817     public static class UniqueType {
4818         public final Type type;
4819         final Types types;

4820 
4821         public UniqueType(Type type, Types types) {
4822             this.type = type;
4823             this.types = types;





4824         }
4825 
4826         public int hashCode() {
4827             return types.hashCode(type);
4828         }
4829 
4830         public boolean equals(Object obj) {
4831             return (obj instanceof UniqueType) &&
4832                 types.isSameType(type, ((UniqueType)obj).type);
4833         }
4834 




4835         public String toString() {
4836             return type.toString();
4837         }
4838 
4839     }
4840     // </editor-fold>
4841 
4842     // <editor-fold defaultstate="collapsed" desc="Visitors">
4843     /**
4844      * A default visitor for types.  All visitor methods except
4845      * visitType are implemented by delegating to visitType.  Concrete
4846      * subclasses must provide an implementation of visitType and can
4847      * override other methods as needed.
4848      *
4849      * @param <R> the return type of the operation implemented by this
4850      * visitor; use Void if no return type is needed.
4851      * @param <S> the type of the second argument (the first being the
4852      * type itself) of the operation implemented by this visitor; use
4853      * Void if a second argument is not needed.
4854      */


5047                     break;
5048                 case LONG:
5049                     append('J');
5050                     break;
5051                 case FLOAT:
5052                     append('F');
5053                     break;
5054                 case DOUBLE:
5055                     append('D');
5056                     break;
5057                 case BOOLEAN:
5058                     append('Z');
5059                     break;
5060                 case VOID:
5061                     append('V');
5062                     break;
5063                 case CLASS:
5064                     if (type.isCompound()) {
5065                         reportIllegalSignature(type);
5066                     }
5067                     append('L');



5068                     assembleClassSig(type);
5069                     append(';');
5070                     break;
5071                 case ARRAY:
5072                     ArrayType at = (ArrayType) type;
5073                     append('[');
5074                     assembleSig(at.elemtype);
5075                     break;
5076                 case METHOD:
5077                     MethodType mt = (MethodType) type;
5078                     append('(');
5079                     assembleSig(mt.argtypes);
5080                     append(')');
5081                     assembleSig(mt.restype);
5082                     if (hasTypeVar(mt.thrown)) {
5083                         for (List<Type> l = mt.thrown; l.nonEmpty(); l = l.tail) {
5084                             append('^');
5085                             assembleSig(l.head);
5086                         }
5087                     }




  76  * <dd>Similarly, if the second argument to an operation is a type, it should be named s.</dd>
  77  * <dt>ts</dt>
  78  * <dd>If an operations takes a list of types, the first should be named ts.</dd>
  79  * <dt>ss</dt>
  80  * <dd>A second list of types should be named ss.</dd>
  81  * </dl>
  82  *
  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 boolean allowValueBasedClasses;
  97     final boolean nonCovariantValueArrays;
  98     final Check chk;
  99     final Enter enter;
 100     JCDiagnostic.Factory diags;
 101     List<Warner> warnStack = List.nil();
 102     final Name capturedName;
 103 
 104     public final Warner noWarnings;
 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     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         allowDefaultMethods = Feature.DEFAULT_METHODS.allowedInSource(source);
 120         mapCapturesToBounds = Feature.MAP_CAPTURES_TO_BOUNDS.allowedInSource(source);
 121         chk = Check.instance(context);
 122         enter = Enter.instance(context);
 123         capturedName = names.fromString("<captured wildcard>");
 124         messages = JavacMessages.instance(context);
 125         diags = JCDiagnostic.Factory.instance(context);
 126         noWarnings = new Warner(null);
 127         Options options = Options.instance(context);
 128         allowValueBasedClasses = options.isSet("allowValueBasedClasses");
 129         nonCovariantValueArrays = options.isSet("nonCovariantValueArrays");
 130     }
 131     // </editor-fold>
 132 
 133     // <editor-fold defaultstate="collapsed" desc="bounds">
 134     /**
 135      * Get a wildcard's upper bound, returning non-wildcards unchanged.
 136      * @param t a type argument, either a wildcard or a type
 137      */
 138     public Type wildUpperBound(Type t) {
 139         if (t.hasTag(WILDCARD)) {
 140             WildcardType w = (WildcardType) t;
 141             if (w.isSuperBound())
 142                 return w.bound == null ? syms.objectType : w.bound.getUpperBound();
 143             else
 144                 return wildUpperBound(w.type);
 145         }
 146         else return t;
 147     }
 148 
 149     /**


 589                 }
 590                 return res;
 591             }
 592 
 593             @Override
 594             public Type visitErrorType(ErrorType t, Symbol sym) {
 595                 return t;
 596             }
 597         };
 598     // </editor-fold>
 599 
 600     // <editor-fold defaultstate="collapsed" desc="isConvertible">
 601     /**
 602      * Is t a subtype of or convertible via boxing/unboxing
 603      * conversion to s?
 604      */
 605     public boolean isConvertible(Type t, Type s, Warner warn) {
 606         if (t.hasTag(ERROR)) {
 607             return true;
 608         }
 609 
 610         boolean tPrimitive = t.isPrimitive();
 611         boolean sPrimitive = s.isPrimitive();
 612         if (tPrimitive == sPrimitive) {
 613             return isSubtypeUnchecked(t, s, warn);
 614         }
 615         boolean tUndet = t.hasTag(UNDETVAR);
 616         boolean sUndet = s.hasTag(UNDETVAR);
 617 
 618         if (tUndet || sUndet) {
 619             return tUndet ?
 620                     isSubtype(t, boxedTypeOrType(s)) :
 621                     isSubtype(boxedTypeOrType(t), s);
 622         }
 623 
 624         return tPrimitive
 625             ? isSubtype(boxedClass(t).type, s)
 626             : isSubtype(unboxedType(t), s);
 627     }
 628 
 629     /**


 983     * Scope filter used to skip methods that should be ignored (such as methods
 984     * overridden by j.l.Object) during function interface conversion interface check
 985     */
 986     class DescriptorFilter implements Filter<Symbol> {
 987 
 988        TypeSymbol origin;
 989 
 990        DescriptorFilter(TypeSymbol origin) {
 991            this.origin = origin;
 992        }
 993 
 994        @Override
 995        public boolean accepts(Symbol sym) {
 996            return sym.kind == MTH &&
 997                    (sym.flags() & (ABSTRACT | DEFAULT)) == ABSTRACT &&
 998                    !overridesObjectMethod(origin, sym) &&
 999                    (interfaceCandidates(origin.type, (MethodSymbol)sym).head.flags() & DEFAULT) == 0;
1000        }
1001     }
1002 
1003     public boolean isValue(Type t) {
1004         return t != null && t.tsym != null && (t.tsym.flags() & Flags.VALUE) != 0;
1005     }
1006 
1007     public boolean isValueBased(Type t) {
1008         return allowValueBasedClasses && t != null && t.tsym != null && (t.tsym.flags() & Flags.VALUEBASED) != 0;
1009     }
1010 
1011     private final HashMap<ClassSymbol, ClassSymbol> nullableProjectionsMap = new HashMap<>();
1012 
1013     public ClassSymbol projectedNullableType(ClassSymbol c) {
1014         if (!c.isValue() || !c.type.hasTag(CLASS))
1015             return null;
1016         ClassSymbol lox = nullableProjectionsMap.get(c);
1017         if (lox != null)
1018             return lox;
1019 
1020         ClassType ct = (ClassType) c.type;
1021         ClassType loxType = new ClassType(ct.getEnclosingType(), ct.typarams_field, null);
1022         loxType.allparams_field = ct.allparams_field;
1023         loxType.supertype_field = ct.supertype_field;
1024         loxType.interfaces_field = ct.interfaces_field;
1025         loxType.all_interfaces_field = ct.all_interfaces_field;
1026         lox = new ClassSymbol((c.flags() & ~VALUE), c.name, loxType, c.owner) {
1027             @Override
1028             public boolean isProjectedNullable() {
1029                 return true;
1030             }
1031 
1032             @Override
1033             public ClassSymbol nullFreeTypeSymbol() {
1034                 return c;
1035             }
1036         };
1037         lox.members_field = c.members();
1038         loxType.tsym = lox;
1039 
1040         nullableProjectionsMap.put(c, lox);
1041         return lox;
1042     }
1043 
1044     // <editor-fold defaultstate="collapsed" desc="isSubtype">
1045     /**
1046      * Is t an unchecked subtype of s?
1047      */
1048     public boolean isSubtypeUnchecked(Type t, Type s) {
1049         return isSubtypeUnchecked(t, s, noWarnings);
1050     }
1051     /**
1052      * Is t an unchecked subtype of s?
1053      */
1054     public boolean isSubtypeUnchecked(Type t, Type s, Warner warn) {
1055         boolean result = isSubtypeUncheckedInternal(t, s, true, warn);
1056         if (result) {
1057             checkUnsafeVarargsConversion(t, s, warn);
1058         }
1059         return result;
1060     }
1061     //where
1062         private boolean isSubtypeUncheckedInternal(Type t, Type s, boolean capture, Warner warn) {
1063             if (t.hasTag(ARRAY) && s.hasTag(ARRAY)) {
1064                 if (((ArrayType)t).elemtype.isPrimitive()) {
1065                     return isSameType(elemtype(t), elemtype(s));
1066                 } else {
1067                     Type et = elemtype(t);
1068                     Type es = elemtype(s);
1069                     if (!isSubtypeUncheckedInternal(et, es, false, warn))
1070                         return false;
1071                     if (nonCovariantValueArrays) {
1072                         if (isValue(et) || isValue(es)) {
1073                             return isSameType(erasure(et), erasure(es));
1074                         }
1075                     }
1076                     return true;
1077                 }
1078             } else if (isSubtype(t, s, capture)) {
1079                 return true;
1080             } else if (t.hasTag(TYPEVAR)) {
1081                 return isSubtypeUncheckedInternal(t.getUpperBound(), s, false, warn);
1082             } else if (!s.isRaw()) {
1083                 Type t2 = asSuper(t, s.tsym);
1084                 if (t2 != null && t2.isRaw()) {
1085                     if (isReifiable(s)) {
1086                         warn.silentWarn(LintCategory.UNCHECKED);
1087                     } else {
1088                         warn.warn(LintCategory.UNCHECKED);
1089                     }
1090                     return true;
1091                 }
1092             }
1093             return false;
1094         }
1095 
1096         private void checkUnsafeVarargsConversion(Type t, Type s, Warner warn) {


1153     }
1154     // where
1155         private TypeRelation isSubtype = new TypeRelation()
1156         {
1157             @Override
1158             public Boolean visitType(Type t, Type s) {
1159                 switch (t.getTag()) {
1160                  case BYTE:
1161                      return (!s.hasTag(CHAR) && t.getTag().isSubRangeOf(s.getTag()));
1162                  case CHAR:
1163                      return (!s.hasTag(SHORT) && t.getTag().isSubRangeOf(s.getTag()));
1164                  case SHORT: case INT: case LONG:
1165                  case FLOAT: case DOUBLE:
1166                      return t.getTag().isSubRangeOf(s.getTag());
1167                  case BOOLEAN: case VOID:
1168                      return t.hasTag(s.getTag());
1169                  case TYPEVAR:
1170                      return isSubtypeNoCapture(t.getUpperBound(), s);
1171                  case BOT:
1172                      return
1173                          s.hasTag(BOT) || (s.hasTag(CLASS) && (!isValue(s) || isValueBased(s))) ||
1174                          s.hasTag(ARRAY) || s.hasTag(TYPEVAR);
1175                  case WILDCARD: //we shouldn't be here - avoids crash (see 7034495)
1176                  case NONE:
1177                      return false;
1178                  default:
1179                      throw new AssertionError("isSubtype " + t.getTag());
1180                  }
1181             }
1182 
1183             private Set<TypePair> cache = new HashSet<>();
1184 
1185             private boolean containsTypeRecursive(Type t, Type s) {
1186                 TypePair pair = new TypePair(t, s);
1187                 if (cache.add(pair)) {
1188                     try {
1189                         return containsType(t.getTypeArguments(),
1190                                             s.getTypeArguments());
1191                     } finally {
1192                         cache.remove(pair);
1193                     }


1722                 case DOUBLE:
1723                     return s.isNumeric();
1724                 case BOOLEAN:
1725                     return s.hasTag(BOOLEAN);
1726                 case VOID:
1727                     return false;
1728                 case BOT:
1729                     return isSubtype(t, s);
1730                 default:
1731                     throw new AssertionError();
1732                 }
1733             }
1734 
1735             @Override
1736             public Boolean visitWildcardType(WildcardType t, Type s) {
1737                 return isCastable(wildUpperBound(t), s, warnStack.head);
1738             }
1739 
1740             @Override
1741             public Boolean visitClassType(ClassType t, Type s) {
1742                 if (s.hasTag(ERROR) || (s.hasTag(BOT) && !isValue(t)))
1743                     return true;
1744 
1745                 if (s.hasTag(TYPEVAR)) {
1746                     if (isCastable(t, s.getUpperBound(), noWarnings)) {
1747                         warnStack.head.warn(LintCategory.UNCHECKED);
1748                         return true;
1749                     } else {
1750                         return false;
1751                     }
1752                 }
1753 
1754                 if (t.isCompound() || s.isCompound()) {
1755                     return !t.isCompound() ?
1756                             visitCompoundType((ClassType)s, t, true) :
1757                             visitCompoundType(t, s, false);
1758                 }
1759 
1760                 if (s.hasTag(CLASS) || s.hasTag(ARRAY)) {
1761                     boolean upcast;
1762                     if ((upcast = isSubtype(erasure(t), erasure(s)))


1846 
1847             @Override
1848             public Boolean visitArrayType(ArrayType t, Type s) {
1849                 switch (s.getTag()) {
1850                 case ERROR:
1851                 case BOT:
1852                     return true;
1853                 case TYPEVAR:
1854                     if (isCastable(s, t, noWarnings)) {
1855                         warnStack.head.warn(LintCategory.UNCHECKED);
1856                         return true;
1857                     } else {
1858                         return false;
1859                     }
1860                 case CLASS:
1861                     return isSubtype(t, s);
1862                 case ARRAY:
1863                     if (elemtype(t).isPrimitive() || elemtype(s).isPrimitive()) {
1864                         return elemtype(t).hasTag(elemtype(s).getTag());
1865                     } else {
1866                         Type et = elemtype(t);
1867                         Type es = elemtype(s);
1868                         if (!visit(et, es))
1869                             return false;
1870                         if (nonCovariantValueArrays) {
1871                             if (isValue(et) || isValue(es)) {
1872                                 return isSameType(erasure(et), erasure(es));
1873                             }
1874                         }
1875                         return true;
1876                     }
1877                 default:
1878                     return false;
1879                 }
1880             }
1881 
1882             @Override
1883             public Boolean visitTypeVar(TypeVar t, Type s) {
1884                 switch (s.getTag()) {
1885                 case ERROR:
1886                 case BOT:
1887                     return true;
1888                 case TYPEVAR:
1889                     if (isSubtype(t, s)) {
1890                         return true;
1891                     } else if (isCastable(t.getUpperBound(), s, noWarnings)) {
1892                         warnStack.head.warn(LintCategory.UNCHECKED);
1893                         return true;
1894                     } else {
1895                         return false;


2172          * (j.u.List<capture#160 of ? extends c.s.s.d.DocTree>, Iterable) =>
2173          *     Iterable<capture#160 of ? extends c.s.s.d.DocTree>
2174          */
2175         if (sym.type == syms.objectType) { //optimization
2176             return syms.objectType;
2177         }
2178         return asSuper.visit(t, sym);
2179     }
2180     // where
2181         private SimpleVisitor<Type,Symbol> asSuper = new SimpleVisitor<Type,Symbol>() {
2182 
2183             public Type visitType(Type t, Symbol sym) {
2184                 return null;
2185             }
2186 
2187             @Override
2188             public Type visitClassType(ClassType t, Symbol sym) {
2189                 if (t.tsym == sym)
2190                     return t;
2191 
2192                 /* For inline types, the wired in super type is j.l.O.
2193                    So we need a special check for V <: V?
2194                 */
2195                 if (t.tsym == sym.nullFreeTypeSymbol()) {
2196                     return new ClassType(t.getEnclosingType(), t.getTypeArguments(), (TypeSymbol)sym, t.getMetadata());
2197                 }
2198 
2199                 Type st = supertype(t);
2200                 if (st.hasTag(CLASS) || st.hasTag(TYPEVAR)) {
2201                     Type x = asSuper(st, sym);
2202                     if (x != null)
2203                         return x;
2204                 }
2205                 if ((sym.flags() & INTERFACE) != 0) {
2206                     for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail) {
2207                         if (!l.head.hasTag(ERROR)) {
2208                             Type x = asSuper(l.head, sym);
2209                             if (x != null)
2210                                 return x;
2211                         }
2212                     }
2213                 }
2214                 return null;
2215             }
2216 
2217             @Override
2218             public Type visitArrayType(ArrayType t, Symbol sym) {


4872     private WildcardType makeSuperWildcard(Type bound, TypeVar formal) {
4873         if (bound.hasTag(BOT)) {
4874             return new WildcardType(syms.objectType,
4875                                     BoundKind.UNBOUND,
4876                                     syms.boundClass,
4877                                     formal);
4878         } else {
4879             return new WildcardType(bound,
4880                                     BoundKind.SUPER,
4881                                     syms.boundClass,
4882                                     formal);
4883         }
4884     }
4885 
4886     /**
4887      * A wrapper for a type that allows use in sets.
4888      */
4889     public static class UniqueType {
4890         public final Type type;
4891         final Types types;
4892         private boolean encodeTypeSig;
4893 
4894         public UniqueType(Type type, Types types, boolean encodeTypeSig) {
4895             this.type = type;
4896             this.types = types;
4897             this.encodeTypeSig = encodeTypeSig;
4898         }
4899 
4900         public UniqueType(Type type, Types types) {
4901             this(type, types, true);
4902         }
4903 
4904         public int hashCode() {
4905             return types.hashCode(type);
4906         }
4907 
4908         public boolean equals(Object obj) {
4909             return (obj instanceof UniqueType) &&
4910                 types.isSameType(type, ((UniqueType)obj).type);
4911         }
4912 
4913         public boolean encodeTypeSig() {
4914             return encodeTypeSig;
4915         }
4916 
4917         public String toString() {
4918             return type.toString();
4919         }
4920 
4921     }
4922     // </editor-fold>
4923 
4924     // <editor-fold defaultstate="collapsed" desc="Visitors">
4925     /**
4926      * A default visitor for types.  All visitor methods except
4927      * visitType are implemented by delegating to visitType.  Concrete
4928      * subclasses must provide an implementation of visitType and can
4929      * override other methods as needed.
4930      *
4931      * @param <R> the return type of the operation implemented by this
4932      * visitor; use Void if no return type is needed.
4933      * @param <S> the type of the second argument (the first being the
4934      * type itself) of the operation implemented by this visitor; use
4935      * Void if a second argument is not needed.
4936      */


5129                     break;
5130                 case LONG:
5131                     append('J');
5132                     break;
5133                 case FLOAT:
5134                     append('F');
5135                     break;
5136                 case DOUBLE:
5137                     append('D');
5138                     break;
5139                 case BOOLEAN:
5140                     append('Z');
5141                     break;
5142                 case VOID:
5143                     append('V');
5144                     break;
5145                 case CLASS:
5146                     if (type.isCompound()) {
5147                         reportIllegalSignature(type);
5148                     }
5149                     if (types.isValue(type))
5150                         append('Q');
5151                     else
5152                         append('L');
5153                     assembleClassSig(type);
5154                     append(';');
5155                     break;
5156                 case ARRAY:
5157                     ArrayType at = (ArrayType) type;
5158                     append('[');
5159                     assembleSig(at.elemtype);
5160                     break;
5161                 case METHOD:
5162                     MethodType mt = (MethodType) type;
5163                     append('(');
5164                     assembleSig(mt.argtypes);
5165                     append(')');
5166                     assembleSig(mt.restype);
5167                     if (hasTypeVar(mt.thrown)) {
5168                         for (List<Type> l = mt.thrown; l.nonEmpty(); l = l.tail) {
5169                             append('^');
5170                             assembleSig(l.head);
5171                         }
5172                     }


< prev index next >