Wed, 16 Feb 2011 10:27:00 -0800
6990136: Cleanup use of Type.clone()
Summary: Introduced factory methods in class Types which can be used rather than clone().
Reviewed-by: jjg, mcimadamore
duke@1 | 1 | /* |
jjg@816 | 2 | * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.javac.code; |
duke@1 | 27 | |
mcimadamore@341 | 28 | import java.lang.ref.SoftReference; |
duke@1 | 29 | import java.util.*; |
duke@1 | 30 | |
duke@1 | 31 | import com.sun.tools.javac.util.*; |
duke@1 | 32 | import com.sun.tools.javac.util.List; |
duke@1 | 33 | |
duke@1 | 34 | import com.sun.tools.javac.jvm.ClassReader; |
jjg@657 | 35 | import com.sun.tools.javac.code.Attribute.RetentionPolicy; |
mcimadamore@795 | 36 | import com.sun.tools.javac.code.Lint.LintCategory; |
duke@1 | 37 | import com.sun.tools.javac.comp.Check; |
duke@1 | 38 | |
mcimadamore@858 | 39 | import static com.sun.tools.javac.code.Scope.*; |
duke@1 | 40 | import static com.sun.tools.javac.code.Type.*; |
duke@1 | 41 | import static com.sun.tools.javac.code.TypeTags.*; |
duke@1 | 42 | import static com.sun.tools.javac.code.Symbol.*; |
duke@1 | 43 | import static com.sun.tools.javac.code.Flags.*; |
duke@1 | 44 | import static com.sun.tools.javac.code.BoundKind.*; |
duke@1 | 45 | import static com.sun.tools.javac.util.ListBuffer.lb; |
duke@1 | 46 | |
duke@1 | 47 | /** |
duke@1 | 48 | * Utility class containing various operations on types. |
duke@1 | 49 | * |
duke@1 | 50 | * <p>Unless other names are more illustrative, the following naming |
duke@1 | 51 | * conventions should be observed in this file: |
duke@1 | 52 | * |
duke@1 | 53 | * <dl> |
duke@1 | 54 | * <dt>t</dt> |
duke@1 | 55 | * <dd>If the first argument to an operation is a type, it should be named t.</dd> |
duke@1 | 56 | * <dt>s</dt> |
duke@1 | 57 | * <dd>Similarly, if the second argument to an operation is a type, it should be named s.</dd> |
duke@1 | 58 | * <dt>ts</dt> |
duke@1 | 59 | * <dd>If an operations takes a list of types, the first should be named ts.</dd> |
duke@1 | 60 | * <dt>ss</dt> |
duke@1 | 61 | * <dd>A second list of types should be named ss.</dd> |
duke@1 | 62 | * </dl> |
duke@1 | 63 | * |
jjg@581 | 64 | * <p><b>This is NOT part of any supported API. |
duke@1 | 65 | * If you write code that depends on this, you do so at your own risk. |
duke@1 | 66 | * This code and its internal interfaces are subject to change or |
duke@1 | 67 | * deletion without notice.</b> |
duke@1 | 68 | */ |
duke@1 | 69 | public class Types { |
duke@1 | 70 | protected static final Context.Key<Types> typesKey = |
duke@1 | 71 | new Context.Key<Types>(); |
duke@1 | 72 | |
duke@1 | 73 | final Symtab syms; |
mcimadamore@136 | 74 | final JavacMessages messages; |
jjg@113 | 75 | final Names names; |
duke@1 | 76 | final boolean allowBoxing; |
duke@1 | 77 | final ClassReader reader; |
duke@1 | 78 | final Source source; |
duke@1 | 79 | final Check chk; |
duke@1 | 80 | List<Warner> warnStack = List.nil(); |
duke@1 | 81 | final Name capturedName; |
duke@1 | 82 | |
duke@1 | 83 | // <editor-fold defaultstate="collapsed" desc="Instantiating"> |
duke@1 | 84 | public static Types instance(Context context) { |
duke@1 | 85 | Types instance = context.get(typesKey); |
duke@1 | 86 | if (instance == null) |
duke@1 | 87 | instance = new Types(context); |
duke@1 | 88 | return instance; |
duke@1 | 89 | } |
duke@1 | 90 | |
duke@1 | 91 | protected Types(Context context) { |
duke@1 | 92 | context.put(typesKey, this); |
duke@1 | 93 | syms = Symtab.instance(context); |
jjg@113 | 94 | names = Names.instance(context); |
duke@1 | 95 | allowBoxing = Source.instance(context).allowBoxing(); |
duke@1 | 96 | reader = ClassReader.instance(context); |
duke@1 | 97 | source = Source.instance(context); |
duke@1 | 98 | chk = Check.instance(context); |
duke@1 | 99 | capturedName = names.fromString("<captured wildcard>"); |
mcimadamore@136 | 100 | messages = JavacMessages.instance(context); |
duke@1 | 101 | } |
duke@1 | 102 | // </editor-fold> |
duke@1 | 103 | |
duke@1 | 104 | // <editor-fold defaultstate="collapsed" desc="upperBound"> |
duke@1 | 105 | /** |
duke@1 | 106 | * The "rvalue conversion".<br> |
duke@1 | 107 | * The upper bound of most types is the type |
duke@1 | 108 | * itself. Wildcards, on the other hand have upper |
duke@1 | 109 | * and lower bounds. |
duke@1 | 110 | * @param t a type |
duke@1 | 111 | * @return the upper bound of the given type |
duke@1 | 112 | */ |
duke@1 | 113 | public Type upperBound(Type t) { |
duke@1 | 114 | return upperBound.visit(t); |
duke@1 | 115 | } |
duke@1 | 116 | // where |
duke@1 | 117 | private final MapVisitor<Void> upperBound = new MapVisitor<Void>() { |
duke@1 | 118 | |
duke@1 | 119 | @Override |
duke@1 | 120 | public Type visitWildcardType(WildcardType t, Void ignored) { |
duke@1 | 121 | if (t.isSuperBound()) |
duke@1 | 122 | return t.bound == null ? syms.objectType : t.bound.bound; |
duke@1 | 123 | else |
duke@1 | 124 | return visit(t.type); |
duke@1 | 125 | } |
duke@1 | 126 | |
duke@1 | 127 | @Override |
duke@1 | 128 | public Type visitCapturedType(CapturedType t, Void ignored) { |
duke@1 | 129 | return visit(t.bound); |
duke@1 | 130 | } |
duke@1 | 131 | }; |
duke@1 | 132 | // </editor-fold> |
duke@1 | 133 | |
duke@1 | 134 | // <editor-fold defaultstate="collapsed" desc="lowerBound"> |
duke@1 | 135 | /** |
duke@1 | 136 | * The "lvalue conversion".<br> |
duke@1 | 137 | * The lower bound of most types is the type |
duke@1 | 138 | * itself. Wildcards, on the other hand have upper |
duke@1 | 139 | * and lower bounds. |
duke@1 | 140 | * @param t a type |
duke@1 | 141 | * @return the lower bound of the given type |
duke@1 | 142 | */ |
duke@1 | 143 | public Type lowerBound(Type t) { |
duke@1 | 144 | return lowerBound.visit(t); |
duke@1 | 145 | } |
duke@1 | 146 | // where |
duke@1 | 147 | private final MapVisitor<Void> lowerBound = new MapVisitor<Void>() { |
duke@1 | 148 | |
duke@1 | 149 | @Override |
duke@1 | 150 | public Type visitWildcardType(WildcardType t, Void ignored) { |
duke@1 | 151 | return t.isExtendsBound() ? syms.botType : visit(t.type); |
duke@1 | 152 | } |
duke@1 | 153 | |
duke@1 | 154 | @Override |
duke@1 | 155 | public Type visitCapturedType(CapturedType t, Void ignored) { |
duke@1 | 156 | return visit(t.getLowerBound()); |
duke@1 | 157 | } |
duke@1 | 158 | }; |
duke@1 | 159 | // </editor-fold> |
duke@1 | 160 | |
duke@1 | 161 | // <editor-fold defaultstate="collapsed" desc="isUnbounded"> |
duke@1 | 162 | /** |
duke@1 | 163 | * Checks that all the arguments to a class are unbounded |
duke@1 | 164 | * wildcards or something else that doesn't make any restrictions |
duke@1 | 165 | * on the arguments. If a class isUnbounded, a raw super- or |
duke@1 | 166 | * subclass can be cast to it without a warning. |
duke@1 | 167 | * @param t a type |
duke@1 | 168 | * @return true iff the given type is unbounded or raw |
duke@1 | 169 | */ |
duke@1 | 170 | public boolean isUnbounded(Type t) { |
duke@1 | 171 | return isUnbounded.visit(t); |
duke@1 | 172 | } |
duke@1 | 173 | // where |
duke@1 | 174 | private final UnaryVisitor<Boolean> isUnbounded = new UnaryVisitor<Boolean>() { |
duke@1 | 175 | |
duke@1 | 176 | public Boolean visitType(Type t, Void ignored) { |
duke@1 | 177 | return true; |
duke@1 | 178 | } |
duke@1 | 179 | |
duke@1 | 180 | @Override |
duke@1 | 181 | public Boolean visitClassType(ClassType t, Void ignored) { |
duke@1 | 182 | List<Type> parms = t.tsym.type.allparams(); |
duke@1 | 183 | List<Type> args = t.allparams(); |
duke@1 | 184 | while (parms.nonEmpty()) { |
duke@1 | 185 | WildcardType unb = new WildcardType(syms.objectType, |
duke@1 | 186 | BoundKind.UNBOUND, |
duke@1 | 187 | syms.boundClass, |
duke@1 | 188 | (TypeVar)parms.head); |
duke@1 | 189 | if (!containsType(args.head, unb)) |
duke@1 | 190 | return false; |
duke@1 | 191 | parms = parms.tail; |
duke@1 | 192 | args = args.tail; |
duke@1 | 193 | } |
duke@1 | 194 | return true; |
duke@1 | 195 | } |
duke@1 | 196 | }; |
duke@1 | 197 | // </editor-fold> |
duke@1 | 198 | |
duke@1 | 199 | // <editor-fold defaultstate="collapsed" desc="asSub"> |
duke@1 | 200 | /** |
duke@1 | 201 | * Return the least specific subtype of t that starts with symbol |
duke@1 | 202 | * sym. If none exists, return null. The least specific subtype |
duke@1 | 203 | * is determined as follows: |
duke@1 | 204 | * |
duke@1 | 205 | * <p>If there is exactly one parameterized instance of sym that is a |
duke@1 | 206 | * subtype of t, that parameterized instance is returned.<br> |
duke@1 | 207 | * Otherwise, if the plain type or raw type `sym' is a subtype of |
duke@1 | 208 | * type t, the type `sym' itself is returned. Otherwise, null is |
duke@1 | 209 | * returned. |
duke@1 | 210 | */ |
duke@1 | 211 | public Type asSub(Type t, Symbol sym) { |
duke@1 | 212 | return asSub.visit(t, sym); |
duke@1 | 213 | } |
duke@1 | 214 | // where |
duke@1 | 215 | private final SimpleVisitor<Type,Symbol> asSub = new SimpleVisitor<Type,Symbol>() { |
duke@1 | 216 | |
duke@1 | 217 | public Type visitType(Type t, Symbol sym) { |
duke@1 | 218 | return null; |
duke@1 | 219 | } |
duke@1 | 220 | |
duke@1 | 221 | @Override |
duke@1 | 222 | public Type visitClassType(ClassType t, Symbol sym) { |
duke@1 | 223 | if (t.tsym == sym) |
duke@1 | 224 | return t; |
duke@1 | 225 | Type base = asSuper(sym.type, t.tsym); |
duke@1 | 226 | if (base == null) |
duke@1 | 227 | return null; |
duke@1 | 228 | ListBuffer<Type> from = new ListBuffer<Type>(); |
duke@1 | 229 | ListBuffer<Type> to = new ListBuffer<Type>(); |
duke@1 | 230 | try { |
duke@1 | 231 | adapt(base, t, from, to); |
duke@1 | 232 | } catch (AdaptFailure ex) { |
duke@1 | 233 | return null; |
duke@1 | 234 | } |
duke@1 | 235 | Type res = subst(sym.type, from.toList(), to.toList()); |
duke@1 | 236 | if (!isSubtype(res, t)) |
duke@1 | 237 | return null; |
duke@1 | 238 | ListBuffer<Type> openVars = new ListBuffer<Type>(); |
duke@1 | 239 | for (List<Type> l = sym.type.allparams(); |
duke@1 | 240 | l.nonEmpty(); l = l.tail) |
duke@1 | 241 | if (res.contains(l.head) && !t.contains(l.head)) |
duke@1 | 242 | openVars.append(l.head); |
duke@1 | 243 | if (openVars.nonEmpty()) { |
duke@1 | 244 | if (t.isRaw()) { |
duke@1 | 245 | // The subtype of a raw type is raw |
duke@1 | 246 | res = erasure(res); |
duke@1 | 247 | } else { |
duke@1 | 248 | // Unbound type arguments default to ? |
duke@1 | 249 | List<Type> opens = openVars.toList(); |
duke@1 | 250 | ListBuffer<Type> qs = new ListBuffer<Type>(); |
duke@1 | 251 | for (List<Type> iter = opens; iter.nonEmpty(); iter = iter.tail) { |
duke@1 | 252 | qs.append(new WildcardType(syms.objectType, BoundKind.UNBOUND, syms.boundClass, (TypeVar) iter.head)); |
duke@1 | 253 | } |
duke@1 | 254 | res = subst(res, opens, qs.toList()); |
duke@1 | 255 | } |
duke@1 | 256 | } |
duke@1 | 257 | return res; |
duke@1 | 258 | } |
duke@1 | 259 | |
duke@1 | 260 | @Override |
duke@1 | 261 | public Type visitErrorType(ErrorType t, Symbol sym) { |
duke@1 | 262 | return t; |
duke@1 | 263 | } |
duke@1 | 264 | }; |
duke@1 | 265 | // </editor-fold> |
duke@1 | 266 | |
duke@1 | 267 | // <editor-fold defaultstate="collapsed" desc="isConvertible"> |
duke@1 | 268 | /** |
duke@1 | 269 | * Is t a subtype of or convertiable via boxing/unboxing |
duke@1 | 270 | * convertions to s? |
duke@1 | 271 | */ |
duke@1 | 272 | public boolean isConvertible(Type t, Type s, Warner warn) { |
duke@1 | 273 | boolean tPrimitive = t.isPrimitive(); |
duke@1 | 274 | boolean sPrimitive = s.isPrimitive(); |
mcimadamore@795 | 275 | if (tPrimitive == sPrimitive) { |
mcimadamore@795 | 276 | checkUnsafeVarargsConversion(t, s, warn); |
duke@1 | 277 | return isSubtypeUnchecked(t, s, warn); |
mcimadamore@795 | 278 | } |
duke@1 | 279 | if (!allowBoxing) return false; |
duke@1 | 280 | return tPrimitive |
duke@1 | 281 | ? isSubtype(boxedClass(t).type, s) |
duke@1 | 282 | : isSubtype(unboxedType(t), s); |
duke@1 | 283 | } |
mcimadamore@795 | 284 | //where |
mcimadamore@795 | 285 | private void checkUnsafeVarargsConversion(Type t, Type s, Warner warn) { |
mcimadamore@795 | 286 | if (t.tag != ARRAY || isReifiable(t)) return; |
mcimadamore@795 | 287 | ArrayType from = (ArrayType)t; |
mcimadamore@795 | 288 | boolean shouldWarn = false; |
mcimadamore@795 | 289 | switch (s.tag) { |
mcimadamore@795 | 290 | case ARRAY: |
mcimadamore@795 | 291 | ArrayType to = (ArrayType)s; |
mcimadamore@795 | 292 | shouldWarn = from.isVarargs() && |
mcimadamore@795 | 293 | !to.isVarargs() && |
mcimadamore@795 | 294 | !isReifiable(from); |
mcimadamore@795 | 295 | break; |
mcimadamore@795 | 296 | case CLASS: |
mcimadamore@795 | 297 | shouldWarn = from.isVarargs() && |
mcimadamore@795 | 298 | isSubtype(from, s); |
mcimadamore@795 | 299 | break; |
mcimadamore@795 | 300 | } |
mcimadamore@795 | 301 | if (shouldWarn) { |
mcimadamore@795 | 302 | warn.warn(LintCategory.VARARGS); |
mcimadamore@795 | 303 | } |
mcimadamore@795 | 304 | } |
duke@1 | 305 | |
duke@1 | 306 | /** |
duke@1 | 307 | * Is t a subtype of or convertiable via boxing/unboxing |
duke@1 | 308 | * convertions to s? |
duke@1 | 309 | */ |
duke@1 | 310 | public boolean isConvertible(Type t, Type s) { |
duke@1 | 311 | return isConvertible(t, s, Warner.noWarnings); |
duke@1 | 312 | } |
duke@1 | 313 | // </editor-fold> |
duke@1 | 314 | |
duke@1 | 315 | // <editor-fold defaultstate="collapsed" desc="isSubtype"> |
duke@1 | 316 | /** |
duke@1 | 317 | * Is t an unchecked subtype of s? |
duke@1 | 318 | */ |
duke@1 | 319 | public boolean isSubtypeUnchecked(Type t, Type s) { |
duke@1 | 320 | return isSubtypeUnchecked(t, s, Warner.noWarnings); |
duke@1 | 321 | } |
duke@1 | 322 | /** |
duke@1 | 323 | * Is t an unchecked subtype of s? |
duke@1 | 324 | */ |
duke@1 | 325 | public boolean isSubtypeUnchecked(Type t, Type s, Warner warn) { |
duke@1 | 326 | if (t.tag == ARRAY && s.tag == ARRAY) { |
mcimadamore@795 | 327 | if (((ArrayType)t).elemtype.tag <= lastBaseTag) { |
mcimadamore@795 | 328 | return isSameType(elemtype(t), elemtype(s)); |
mcimadamore@795 | 329 | } else { |
mcimadamore@795 | 330 | ArrayType from = (ArrayType)t; |
mcimadamore@795 | 331 | ArrayType to = (ArrayType)s; |
mcimadamore@795 | 332 | if (from.isVarargs() && |
mcimadamore@795 | 333 | !to.isVarargs() && |
mcimadamore@795 | 334 | !isReifiable(from)) { |
mcimadamore@795 | 335 | warn.warn(LintCategory.VARARGS); |
mcimadamore@795 | 336 | } |
mcimadamore@795 | 337 | return isSubtypeUnchecked(elemtype(t), elemtype(s), warn); |
mcimadamore@795 | 338 | } |
duke@1 | 339 | } else if (isSubtype(t, s)) { |
duke@1 | 340 | return true; |
mcimadamore@41 | 341 | } |
mcimadamore@41 | 342 | else if (t.tag == TYPEVAR) { |
mcimadamore@41 | 343 | return isSubtypeUnchecked(t.getUpperBound(), s, warn); |
mcimadamore@41 | 344 | } |
mcimadamore@93 | 345 | else if (s.tag == UNDETVAR) { |
mcimadamore@93 | 346 | UndetVar uv = (UndetVar)s; |
mcimadamore@93 | 347 | if (uv.inst != null) |
mcimadamore@93 | 348 | return isSubtypeUnchecked(t, uv.inst, warn); |
mcimadamore@93 | 349 | } |
mcimadamore@41 | 350 | else if (!s.isRaw()) { |
duke@1 | 351 | Type t2 = asSuper(t, s.tsym); |
duke@1 | 352 | if (t2 != null && t2.isRaw()) { |
duke@1 | 353 | if (isReifiable(s)) |
mcimadamore@795 | 354 | warn.silentWarn(LintCategory.UNCHECKED); |
duke@1 | 355 | else |
mcimadamore@795 | 356 | warn.warn(LintCategory.UNCHECKED); |
duke@1 | 357 | return true; |
duke@1 | 358 | } |
duke@1 | 359 | } |
duke@1 | 360 | return false; |
duke@1 | 361 | } |
duke@1 | 362 | |
duke@1 | 363 | /** |
duke@1 | 364 | * Is t a subtype of s?<br> |
duke@1 | 365 | * (not defined for Method and ForAll types) |
duke@1 | 366 | */ |
duke@1 | 367 | final public boolean isSubtype(Type t, Type s) { |
duke@1 | 368 | return isSubtype(t, s, true); |
duke@1 | 369 | } |
duke@1 | 370 | final public boolean isSubtypeNoCapture(Type t, Type s) { |
duke@1 | 371 | return isSubtype(t, s, false); |
duke@1 | 372 | } |
duke@1 | 373 | public boolean isSubtype(Type t, Type s, boolean capture) { |
duke@1 | 374 | if (t == s) |
duke@1 | 375 | return true; |
duke@1 | 376 | |
duke@1 | 377 | if (s.tag >= firstPartialTag) |
duke@1 | 378 | return isSuperType(s, t); |
duke@1 | 379 | |
mcimadamore@299 | 380 | if (s.isCompound()) { |
mcimadamore@299 | 381 | for (Type s2 : interfaces(s).prepend(supertype(s))) { |
mcimadamore@299 | 382 | if (!isSubtype(t, s2, capture)) |
mcimadamore@299 | 383 | return false; |
mcimadamore@299 | 384 | } |
mcimadamore@299 | 385 | return true; |
mcimadamore@299 | 386 | } |
mcimadamore@299 | 387 | |
duke@1 | 388 | Type lower = lowerBound(s); |
duke@1 | 389 | if (s != lower) |
duke@1 | 390 | return isSubtype(capture ? capture(t) : t, lower, false); |
duke@1 | 391 | |
duke@1 | 392 | return isSubtype.visit(capture ? capture(t) : t, s); |
duke@1 | 393 | } |
duke@1 | 394 | // where |
duke@1 | 395 | private TypeRelation isSubtype = new TypeRelation() |
duke@1 | 396 | { |
duke@1 | 397 | public Boolean visitType(Type t, Type s) { |
duke@1 | 398 | switch (t.tag) { |
duke@1 | 399 | case BYTE: case CHAR: |
duke@1 | 400 | return (t.tag == s.tag || |
duke@1 | 401 | t.tag + 2 <= s.tag && s.tag <= DOUBLE); |
duke@1 | 402 | case SHORT: case INT: case LONG: case FLOAT: case DOUBLE: |
duke@1 | 403 | return t.tag <= s.tag && s.tag <= DOUBLE; |
duke@1 | 404 | case BOOLEAN: case VOID: |
duke@1 | 405 | return t.tag == s.tag; |
duke@1 | 406 | case TYPEVAR: |
duke@1 | 407 | return isSubtypeNoCapture(t.getUpperBound(), s); |
duke@1 | 408 | case BOT: |
duke@1 | 409 | return |
duke@1 | 410 | s.tag == BOT || s.tag == CLASS || |
duke@1 | 411 | s.tag == ARRAY || s.tag == TYPEVAR; |
duke@1 | 412 | case NONE: |
duke@1 | 413 | return false; |
duke@1 | 414 | default: |
duke@1 | 415 | throw new AssertionError("isSubtype " + t.tag); |
duke@1 | 416 | } |
duke@1 | 417 | } |
duke@1 | 418 | |
duke@1 | 419 | private Set<TypePair> cache = new HashSet<TypePair>(); |
duke@1 | 420 | |
duke@1 | 421 | private boolean containsTypeRecursive(Type t, Type s) { |
duke@1 | 422 | TypePair pair = new TypePair(t, s); |
duke@1 | 423 | if (cache.add(pair)) { |
duke@1 | 424 | try { |
duke@1 | 425 | return containsType(t.getTypeArguments(), |
duke@1 | 426 | s.getTypeArguments()); |
duke@1 | 427 | } finally { |
duke@1 | 428 | cache.remove(pair); |
duke@1 | 429 | } |
duke@1 | 430 | } else { |
duke@1 | 431 | return containsType(t.getTypeArguments(), |
duke@1 | 432 | rewriteSupers(s).getTypeArguments()); |
duke@1 | 433 | } |
duke@1 | 434 | } |
duke@1 | 435 | |
duke@1 | 436 | private Type rewriteSupers(Type t) { |
duke@1 | 437 | if (!t.isParameterized()) |
duke@1 | 438 | return t; |
duke@1 | 439 | ListBuffer<Type> from = lb(); |
duke@1 | 440 | ListBuffer<Type> to = lb(); |
duke@1 | 441 | adaptSelf(t, from, to); |
duke@1 | 442 | if (from.isEmpty()) |
duke@1 | 443 | return t; |
duke@1 | 444 | ListBuffer<Type> rewrite = lb(); |
duke@1 | 445 | boolean changed = false; |
duke@1 | 446 | for (Type orig : to.toList()) { |
duke@1 | 447 | Type s = rewriteSupers(orig); |
duke@1 | 448 | if (s.isSuperBound() && !s.isExtendsBound()) { |
duke@1 | 449 | s = new WildcardType(syms.objectType, |
duke@1 | 450 | BoundKind.UNBOUND, |
duke@1 | 451 | syms.boundClass); |
duke@1 | 452 | changed = true; |
duke@1 | 453 | } else if (s != orig) { |
duke@1 | 454 | s = new WildcardType(upperBound(s), |
duke@1 | 455 | BoundKind.EXTENDS, |
duke@1 | 456 | syms.boundClass); |
duke@1 | 457 | changed = true; |
duke@1 | 458 | } |
duke@1 | 459 | rewrite.append(s); |
duke@1 | 460 | } |
duke@1 | 461 | if (changed) |
duke@1 | 462 | return subst(t.tsym.type, from.toList(), rewrite.toList()); |
duke@1 | 463 | else |
duke@1 | 464 | return t; |
duke@1 | 465 | } |
duke@1 | 466 | |
duke@1 | 467 | @Override |
duke@1 | 468 | public Boolean visitClassType(ClassType t, Type s) { |
duke@1 | 469 | Type sup = asSuper(t, s.tsym); |
duke@1 | 470 | return sup != null |
duke@1 | 471 | && sup.tsym == s.tsym |
duke@1 | 472 | // You're not allowed to write |
duke@1 | 473 | // Vector<Object> vec = new Vector<String>(); |
duke@1 | 474 | // But with wildcards you can write |
duke@1 | 475 | // Vector<? extends Object> vec = new Vector<String>(); |
duke@1 | 476 | // which means that subtype checking must be done |
duke@1 | 477 | // here instead of same-type checking (via containsType). |
duke@1 | 478 | && (!s.isParameterized() || containsTypeRecursive(s, sup)) |
duke@1 | 479 | && isSubtypeNoCapture(sup.getEnclosingType(), |
duke@1 | 480 | s.getEnclosingType()); |
duke@1 | 481 | } |
duke@1 | 482 | |
duke@1 | 483 | @Override |
duke@1 | 484 | public Boolean visitArrayType(ArrayType t, Type s) { |
duke@1 | 485 | if (s.tag == ARRAY) { |
duke@1 | 486 | if (t.elemtype.tag <= lastBaseTag) |
duke@1 | 487 | return isSameType(t.elemtype, elemtype(s)); |
duke@1 | 488 | else |
duke@1 | 489 | return isSubtypeNoCapture(t.elemtype, elemtype(s)); |
duke@1 | 490 | } |
duke@1 | 491 | |
duke@1 | 492 | if (s.tag == CLASS) { |
duke@1 | 493 | Name sname = s.tsym.getQualifiedName(); |
duke@1 | 494 | return sname == names.java_lang_Object |
duke@1 | 495 | || sname == names.java_lang_Cloneable |
duke@1 | 496 | || sname == names.java_io_Serializable; |
duke@1 | 497 | } |
duke@1 | 498 | |
duke@1 | 499 | return false; |
duke@1 | 500 | } |
duke@1 | 501 | |
duke@1 | 502 | @Override |
duke@1 | 503 | public Boolean visitUndetVar(UndetVar t, Type s) { |
duke@1 | 504 | //todo: test against origin needed? or replace with substitution? |
duke@1 | 505 | if (t == s || t.qtype == s || s.tag == ERROR || s.tag == UNKNOWN) |
duke@1 | 506 | return true; |
duke@1 | 507 | |
duke@1 | 508 | if (t.inst != null) |
duke@1 | 509 | return isSubtypeNoCapture(t.inst, s); // TODO: ", warn"? |
duke@1 | 510 | |
duke@1 | 511 | t.hibounds = t.hibounds.prepend(s); |
duke@1 | 512 | return true; |
duke@1 | 513 | } |
duke@1 | 514 | |
duke@1 | 515 | @Override |
duke@1 | 516 | public Boolean visitErrorType(ErrorType t, Type s) { |
duke@1 | 517 | return true; |
duke@1 | 518 | } |
duke@1 | 519 | }; |
duke@1 | 520 | |
duke@1 | 521 | /** |
duke@1 | 522 | * Is t a subtype of every type in given list `ts'?<br> |
duke@1 | 523 | * (not defined for Method and ForAll types)<br> |
duke@1 | 524 | * Allows unchecked conversions. |
duke@1 | 525 | */ |
duke@1 | 526 | public boolean isSubtypeUnchecked(Type t, List<Type> ts, Warner warn) { |
duke@1 | 527 | for (List<Type> l = ts; l.nonEmpty(); l = l.tail) |
duke@1 | 528 | if (!isSubtypeUnchecked(t, l.head, warn)) |
duke@1 | 529 | return false; |
duke@1 | 530 | return true; |
duke@1 | 531 | } |
duke@1 | 532 | |
duke@1 | 533 | /** |
duke@1 | 534 | * Are corresponding elements of ts subtypes of ss? If lists are |
duke@1 | 535 | * of different length, return false. |
duke@1 | 536 | */ |
duke@1 | 537 | public boolean isSubtypes(List<Type> ts, List<Type> ss) { |
duke@1 | 538 | while (ts.tail != null && ss.tail != null |
duke@1 | 539 | /*inlined: ts.nonEmpty() && ss.nonEmpty()*/ && |
duke@1 | 540 | isSubtype(ts.head, ss.head)) { |
duke@1 | 541 | ts = ts.tail; |
duke@1 | 542 | ss = ss.tail; |
duke@1 | 543 | } |
duke@1 | 544 | return ts.tail == null && ss.tail == null; |
duke@1 | 545 | /*inlined: ts.isEmpty() && ss.isEmpty();*/ |
duke@1 | 546 | } |
duke@1 | 547 | |
duke@1 | 548 | /** |
duke@1 | 549 | * Are corresponding elements of ts subtypes of ss, allowing |
duke@1 | 550 | * unchecked conversions? If lists are of different length, |
duke@1 | 551 | * return false. |
duke@1 | 552 | **/ |
duke@1 | 553 | public boolean isSubtypesUnchecked(List<Type> ts, List<Type> ss, Warner warn) { |
duke@1 | 554 | while (ts.tail != null && ss.tail != null |
duke@1 | 555 | /*inlined: ts.nonEmpty() && ss.nonEmpty()*/ && |
duke@1 | 556 | isSubtypeUnchecked(ts.head, ss.head, warn)) { |
duke@1 | 557 | ts = ts.tail; |
duke@1 | 558 | ss = ss.tail; |
duke@1 | 559 | } |
duke@1 | 560 | return ts.tail == null && ss.tail == null; |
duke@1 | 561 | /*inlined: ts.isEmpty() && ss.isEmpty();*/ |
duke@1 | 562 | } |
duke@1 | 563 | // </editor-fold> |
duke@1 | 564 | |
duke@1 | 565 | // <editor-fold defaultstate="collapsed" desc="isSuperType"> |
duke@1 | 566 | /** |
duke@1 | 567 | * Is t a supertype of s? |
duke@1 | 568 | */ |
duke@1 | 569 | public boolean isSuperType(Type t, Type s) { |
duke@1 | 570 | switch (t.tag) { |
duke@1 | 571 | case ERROR: |
duke@1 | 572 | return true; |
duke@1 | 573 | case UNDETVAR: { |
duke@1 | 574 | UndetVar undet = (UndetVar)t; |
duke@1 | 575 | if (t == s || |
duke@1 | 576 | undet.qtype == s || |
duke@1 | 577 | s.tag == ERROR || |
duke@1 | 578 | s.tag == BOT) return true; |
duke@1 | 579 | if (undet.inst != null) |
duke@1 | 580 | return isSubtype(s, undet.inst); |
duke@1 | 581 | undet.lobounds = undet.lobounds.prepend(s); |
duke@1 | 582 | return true; |
duke@1 | 583 | } |
duke@1 | 584 | default: |
duke@1 | 585 | return isSubtype(s, t); |
duke@1 | 586 | } |
duke@1 | 587 | } |
duke@1 | 588 | // </editor-fold> |
duke@1 | 589 | |
duke@1 | 590 | // <editor-fold defaultstate="collapsed" desc="isSameType"> |
duke@1 | 591 | /** |
duke@1 | 592 | * Are corresponding elements of the lists the same type? If |
duke@1 | 593 | * lists are of different length, return false. |
duke@1 | 594 | */ |
duke@1 | 595 | public boolean isSameTypes(List<Type> ts, List<Type> ss) { |
duke@1 | 596 | while (ts.tail != null && ss.tail != null |
duke@1 | 597 | /*inlined: ts.nonEmpty() && ss.nonEmpty()*/ && |
duke@1 | 598 | isSameType(ts.head, ss.head)) { |
duke@1 | 599 | ts = ts.tail; |
duke@1 | 600 | ss = ss.tail; |
duke@1 | 601 | } |
duke@1 | 602 | return ts.tail == null && ss.tail == null; |
duke@1 | 603 | /*inlined: ts.isEmpty() && ss.isEmpty();*/ |
duke@1 | 604 | } |
duke@1 | 605 | |
duke@1 | 606 | /** |
duke@1 | 607 | * Is t the same type as s? |
duke@1 | 608 | */ |
duke@1 | 609 | public boolean isSameType(Type t, Type s) { |
duke@1 | 610 | return isSameType.visit(t, s); |
duke@1 | 611 | } |
duke@1 | 612 | // where |
duke@1 | 613 | private TypeRelation isSameType = new TypeRelation() { |
duke@1 | 614 | |
duke@1 | 615 | public Boolean visitType(Type t, Type s) { |
duke@1 | 616 | if (t == s) |
duke@1 | 617 | return true; |
duke@1 | 618 | |
duke@1 | 619 | if (s.tag >= firstPartialTag) |
duke@1 | 620 | return visit(s, t); |
duke@1 | 621 | |
duke@1 | 622 | switch (t.tag) { |
duke@1 | 623 | case BYTE: case CHAR: case SHORT: case INT: case LONG: case FLOAT: |
duke@1 | 624 | case DOUBLE: case BOOLEAN: case VOID: case BOT: case NONE: |
duke@1 | 625 | return t.tag == s.tag; |
mcimadamore@561 | 626 | case TYPEVAR: { |
mcimadamore@561 | 627 | if (s.tag == TYPEVAR) { |
mcimadamore@561 | 628 | //type-substitution does not preserve type-var types |
mcimadamore@561 | 629 | //check that type var symbols and bounds are indeed the same |
mcimadamore@561 | 630 | return t.tsym == s.tsym && |
mcimadamore@561 | 631 | visit(t.getUpperBound(), s.getUpperBound()); |
mcimadamore@561 | 632 | } |
mcimadamore@561 | 633 | else { |
mcimadamore@561 | 634 | //special case for s == ? super X, where upper(s) = u |
mcimadamore@561 | 635 | //check that u == t, where u has been set by Type.withTypeVar |
mcimadamore@561 | 636 | return s.isSuperBound() && |
mcimadamore@561 | 637 | !s.isExtendsBound() && |
mcimadamore@561 | 638 | visit(t, upperBound(s)); |
mcimadamore@561 | 639 | } |
mcimadamore@561 | 640 | } |
duke@1 | 641 | default: |
duke@1 | 642 | throw new AssertionError("isSameType " + t.tag); |
duke@1 | 643 | } |
duke@1 | 644 | } |
duke@1 | 645 | |
duke@1 | 646 | @Override |
duke@1 | 647 | public Boolean visitWildcardType(WildcardType t, Type s) { |
duke@1 | 648 | if (s.tag >= firstPartialTag) |
duke@1 | 649 | return visit(s, t); |
duke@1 | 650 | else |
duke@1 | 651 | return false; |
duke@1 | 652 | } |
duke@1 | 653 | |
duke@1 | 654 | @Override |
duke@1 | 655 | public Boolean visitClassType(ClassType t, Type s) { |
duke@1 | 656 | if (t == s) |
duke@1 | 657 | return true; |
duke@1 | 658 | |
duke@1 | 659 | if (s.tag >= firstPartialTag) |
duke@1 | 660 | return visit(s, t); |
duke@1 | 661 | |
duke@1 | 662 | if (s.isSuperBound() && !s.isExtendsBound()) |
duke@1 | 663 | return visit(t, upperBound(s)) && visit(t, lowerBound(s)); |
duke@1 | 664 | |
duke@1 | 665 | if (t.isCompound() && s.isCompound()) { |
duke@1 | 666 | if (!visit(supertype(t), supertype(s))) |
duke@1 | 667 | return false; |
duke@1 | 668 | |
duke@1 | 669 | HashSet<SingletonType> set = new HashSet<SingletonType>(); |
duke@1 | 670 | for (Type x : interfaces(t)) |
duke@1 | 671 | set.add(new SingletonType(x)); |
duke@1 | 672 | for (Type x : interfaces(s)) { |
duke@1 | 673 | if (!set.remove(new SingletonType(x))) |
duke@1 | 674 | return false; |
duke@1 | 675 | } |
jjg@789 | 676 | return (set.isEmpty()); |
duke@1 | 677 | } |
duke@1 | 678 | return t.tsym == s.tsym |
duke@1 | 679 | && visit(t.getEnclosingType(), s.getEnclosingType()) |
duke@1 | 680 | && containsTypeEquivalent(t.getTypeArguments(), s.getTypeArguments()); |
duke@1 | 681 | } |
duke@1 | 682 | |
duke@1 | 683 | @Override |
duke@1 | 684 | public Boolean visitArrayType(ArrayType t, Type s) { |
duke@1 | 685 | if (t == s) |
duke@1 | 686 | return true; |
duke@1 | 687 | |
duke@1 | 688 | if (s.tag >= firstPartialTag) |
duke@1 | 689 | return visit(s, t); |
duke@1 | 690 | |
duke@1 | 691 | return s.tag == ARRAY |
duke@1 | 692 | && containsTypeEquivalent(t.elemtype, elemtype(s)); |
duke@1 | 693 | } |
duke@1 | 694 | |
duke@1 | 695 | @Override |
duke@1 | 696 | public Boolean visitMethodType(MethodType t, Type s) { |
duke@1 | 697 | // isSameType for methods does not take thrown |
duke@1 | 698 | // exceptions into account! |
duke@1 | 699 | return hasSameArgs(t, s) && visit(t.getReturnType(), s.getReturnType()); |
duke@1 | 700 | } |
duke@1 | 701 | |
duke@1 | 702 | @Override |
duke@1 | 703 | public Boolean visitPackageType(PackageType t, Type s) { |
duke@1 | 704 | return t == s; |
duke@1 | 705 | } |
duke@1 | 706 | |
duke@1 | 707 | @Override |
duke@1 | 708 | public Boolean visitForAll(ForAll t, Type s) { |
duke@1 | 709 | if (s.tag != FORALL) |
duke@1 | 710 | return false; |
duke@1 | 711 | |
duke@1 | 712 | ForAll forAll = (ForAll)s; |
duke@1 | 713 | return hasSameBounds(t, forAll) |
duke@1 | 714 | && visit(t.qtype, subst(forAll.qtype, forAll.tvars, t.tvars)); |
duke@1 | 715 | } |
duke@1 | 716 | |
duke@1 | 717 | @Override |
duke@1 | 718 | public Boolean visitUndetVar(UndetVar t, Type s) { |
duke@1 | 719 | if (s.tag == WILDCARD) |
duke@1 | 720 | // FIXME, this might be leftovers from before capture conversion |
duke@1 | 721 | return false; |
duke@1 | 722 | |
duke@1 | 723 | if (t == s || t.qtype == s || s.tag == ERROR || s.tag == UNKNOWN) |
duke@1 | 724 | return true; |
duke@1 | 725 | |
duke@1 | 726 | if (t.inst != null) |
duke@1 | 727 | return visit(t.inst, s); |
duke@1 | 728 | |
duke@1 | 729 | t.inst = fromUnknownFun.apply(s); |
duke@1 | 730 | for (List<Type> l = t.lobounds; l.nonEmpty(); l = l.tail) { |
duke@1 | 731 | if (!isSubtype(l.head, t.inst)) |
duke@1 | 732 | return false; |
duke@1 | 733 | } |
duke@1 | 734 | for (List<Type> l = t.hibounds; l.nonEmpty(); l = l.tail) { |
duke@1 | 735 | if (!isSubtype(t.inst, l.head)) |
duke@1 | 736 | return false; |
duke@1 | 737 | } |
duke@1 | 738 | return true; |
duke@1 | 739 | } |
duke@1 | 740 | |
duke@1 | 741 | @Override |
duke@1 | 742 | public Boolean visitErrorType(ErrorType t, Type s) { |
duke@1 | 743 | return true; |
duke@1 | 744 | } |
duke@1 | 745 | }; |
duke@1 | 746 | // </editor-fold> |
duke@1 | 747 | |
duke@1 | 748 | // <editor-fold defaultstate="collapsed" desc="fromUnknownFun"> |
duke@1 | 749 | /** |
duke@1 | 750 | * A mapping that turns all unknown types in this type to fresh |
duke@1 | 751 | * unknown variables. |
duke@1 | 752 | */ |
duke@1 | 753 | public Mapping fromUnknownFun = new Mapping("fromUnknownFun") { |
duke@1 | 754 | public Type apply(Type t) { |
duke@1 | 755 | if (t.tag == UNKNOWN) return new UndetVar(t); |
duke@1 | 756 | else return t.map(this); |
duke@1 | 757 | } |
duke@1 | 758 | }; |
duke@1 | 759 | // </editor-fold> |
duke@1 | 760 | |
duke@1 | 761 | // <editor-fold defaultstate="collapsed" desc="Contains Type"> |
duke@1 | 762 | public boolean containedBy(Type t, Type s) { |
duke@1 | 763 | switch (t.tag) { |
duke@1 | 764 | case UNDETVAR: |
duke@1 | 765 | if (s.tag == WILDCARD) { |
duke@1 | 766 | UndetVar undetvar = (UndetVar)t; |
mcimadamore@210 | 767 | WildcardType wt = (WildcardType)s; |
mcimadamore@210 | 768 | switch(wt.kind) { |
mcimadamore@210 | 769 | case UNBOUND: //similar to ? extends Object |
mcimadamore@210 | 770 | case EXTENDS: { |
mcimadamore@210 | 771 | Type bound = upperBound(s); |
mcimadamore@210 | 772 | // We should check the new upper bound against any of the |
mcimadamore@210 | 773 | // undetvar's lower bounds. |
mcimadamore@210 | 774 | for (Type t2 : undetvar.lobounds) { |
mcimadamore@210 | 775 | if (!isSubtype(t2, bound)) |
mcimadamore@210 | 776 | return false; |
mcimadamore@210 | 777 | } |
mcimadamore@210 | 778 | undetvar.hibounds = undetvar.hibounds.prepend(bound); |
mcimadamore@210 | 779 | break; |
mcimadamore@210 | 780 | } |
mcimadamore@210 | 781 | case SUPER: { |
mcimadamore@210 | 782 | Type bound = lowerBound(s); |
mcimadamore@210 | 783 | // We should check the new lower bound against any of the |
mcimadamore@210 | 784 | // undetvar's lower bounds. |
mcimadamore@210 | 785 | for (Type t2 : undetvar.hibounds) { |
mcimadamore@210 | 786 | if (!isSubtype(bound, t2)) |
mcimadamore@210 | 787 | return false; |
mcimadamore@210 | 788 | } |
mcimadamore@210 | 789 | undetvar.lobounds = undetvar.lobounds.prepend(bound); |
mcimadamore@210 | 790 | break; |
mcimadamore@210 | 791 | } |
mcimadamore@162 | 792 | } |
duke@1 | 793 | return true; |
duke@1 | 794 | } else { |
duke@1 | 795 | return isSameType(t, s); |
duke@1 | 796 | } |
duke@1 | 797 | case ERROR: |
duke@1 | 798 | return true; |
duke@1 | 799 | default: |
duke@1 | 800 | return containsType(s, t); |
duke@1 | 801 | } |
duke@1 | 802 | } |
duke@1 | 803 | |
duke@1 | 804 | boolean containsType(List<Type> ts, List<Type> ss) { |
duke@1 | 805 | while (ts.nonEmpty() && ss.nonEmpty() |
duke@1 | 806 | && containsType(ts.head, ss.head)) { |
duke@1 | 807 | ts = ts.tail; |
duke@1 | 808 | ss = ss.tail; |
duke@1 | 809 | } |
duke@1 | 810 | return ts.isEmpty() && ss.isEmpty(); |
duke@1 | 811 | } |
duke@1 | 812 | |
duke@1 | 813 | /** |
duke@1 | 814 | * Check if t contains s. |
duke@1 | 815 | * |
duke@1 | 816 | * <p>T contains S if: |
duke@1 | 817 | * |
duke@1 | 818 | * <p>{@code L(T) <: L(S) && U(S) <: U(T)} |
duke@1 | 819 | * |
duke@1 | 820 | * <p>This relation is only used by ClassType.isSubtype(), that |
duke@1 | 821 | * is, |
duke@1 | 822 | * |
duke@1 | 823 | * <p>{@code C<S> <: C<T> if T contains S.} |
duke@1 | 824 | * |
duke@1 | 825 | * <p>Because of F-bounds, this relation can lead to infinite |
duke@1 | 826 | * recursion. Thus we must somehow break that recursion. Notice |
duke@1 | 827 | * that containsType() is only called from ClassType.isSubtype(). |
duke@1 | 828 | * Since the arguments have already been checked against their |
duke@1 | 829 | * bounds, we know: |
duke@1 | 830 | * |
duke@1 | 831 | * <p>{@code U(S) <: U(T) if T is "super" bound (U(T) *is* the bound)} |
duke@1 | 832 | * |
duke@1 | 833 | * <p>{@code L(T) <: L(S) if T is "extends" bound (L(T) is bottom)} |
duke@1 | 834 | * |
duke@1 | 835 | * @param t a type |
duke@1 | 836 | * @param s a type |
duke@1 | 837 | */ |
duke@1 | 838 | public boolean containsType(Type t, Type s) { |
duke@1 | 839 | return containsType.visit(t, s); |
duke@1 | 840 | } |
duke@1 | 841 | // where |
duke@1 | 842 | private TypeRelation containsType = new TypeRelation() { |
duke@1 | 843 | |
duke@1 | 844 | private Type U(Type t) { |
duke@1 | 845 | while (t.tag == WILDCARD) { |
duke@1 | 846 | WildcardType w = (WildcardType)t; |
duke@1 | 847 | if (w.isSuperBound()) |
duke@1 | 848 | return w.bound == null ? syms.objectType : w.bound.bound; |
duke@1 | 849 | else |
duke@1 | 850 | t = w.type; |
duke@1 | 851 | } |
duke@1 | 852 | return t; |
duke@1 | 853 | } |
duke@1 | 854 | |
duke@1 | 855 | private Type L(Type t) { |
duke@1 | 856 | while (t.tag == WILDCARD) { |
duke@1 | 857 | WildcardType w = (WildcardType)t; |
duke@1 | 858 | if (w.isExtendsBound()) |
duke@1 | 859 | return syms.botType; |
duke@1 | 860 | else |
duke@1 | 861 | t = w.type; |
duke@1 | 862 | } |
duke@1 | 863 | return t; |
duke@1 | 864 | } |
duke@1 | 865 | |
duke@1 | 866 | public Boolean visitType(Type t, Type s) { |
duke@1 | 867 | if (s.tag >= firstPartialTag) |
duke@1 | 868 | return containedBy(s, t); |
duke@1 | 869 | else |
duke@1 | 870 | return isSameType(t, s); |
duke@1 | 871 | } |
duke@1 | 872 | |
jjg@789 | 873 | // void debugContainsType(WildcardType t, Type s) { |
jjg@789 | 874 | // System.err.println(); |
jjg@789 | 875 | // System.err.format(" does %s contain %s?%n", t, s); |
jjg@789 | 876 | // System.err.format(" %s U(%s) <: U(%s) %s = %s%n", |
jjg@789 | 877 | // upperBound(s), s, t, U(t), |
jjg@789 | 878 | // t.isSuperBound() |
jjg@789 | 879 | // || isSubtypeNoCapture(upperBound(s), U(t))); |
jjg@789 | 880 | // System.err.format(" %s L(%s) <: L(%s) %s = %s%n", |
jjg@789 | 881 | // L(t), t, s, lowerBound(s), |
jjg@789 | 882 | // t.isExtendsBound() |
jjg@789 | 883 | // || isSubtypeNoCapture(L(t), lowerBound(s))); |
jjg@789 | 884 | // System.err.println(); |
jjg@789 | 885 | // } |
duke@1 | 886 | |
duke@1 | 887 | @Override |
duke@1 | 888 | public Boolean visitWildcardType(WildcardType t, Type s) { |
duke@1 | 889 | if (s.tag >= firstPartialTag) |
duke@1 | 890 | return containedBy(s, t); |
duke@1 | 891 | else { |
jjg@789 | 892 | // debugContainsType(t, s); |
duke@1 | 893 | return isSameWildcard(t, s) |
duke@1 | 894 | || isCaptureOf(s, t) |
duke@1 | 895 | || ((t.isExtendsBound() || isSubtypeNoCapture(L(t), lowerBound(s))) && |
duke@1 | 896 | (t.isSuperBound() || isSubtypeNoCapture(upperBound(s), U(t)))); |
duke@1 | 897 | } |
duke@1 | 898 | } |
duke@1 | 899 | |
duke@1 | 900 | @Override |
duke@1 | 901 | public Boolean visitUndetVar(UndetVar t, Type s) { |
duke@1 | 902 | if (s.tag != WILDCARD) |
duke@1 | 903 | return isSameType(t, s); |
duke@1 | 904 | else |
duke@1 | 905 | return false; |
duke@1 | 906 | } |
duke@1 | 907 | |
duke@1 | 908 | @Override |
duke@1 | 909 | public Boolean visitErrorType(ErrorType t, Type s) { |
duke@1 | 910 | return true; |
duke@1 | 911 | } |
duke@1 | 912 | }; |
duke@1 | 913 | |
duke@1 | 914 | public boolean isCaptureOf(Type s, WildcardType t) { |
mcimadamore@79 | 915 | if (s.tag != TYPEVAR || !((TypeVar)s).isCaptured()) |
duke@1 | 916 | return false; |
duke@1 | 917 | return isSameWildcard(t, ((CapturedType)s).wildcard); |
duke@1 | 918 | } |
duke@1 | 919 | |
duke@1 | 920 | public boolean isSameWildcard(WildcardType t, Type s) { |
duke@1 | 921 | if (s.tag != WILDCARD) |
duke@1 | 922 | return false; |
duke@1 | 923 | WildcardType w = (WildcardType)s; |
duke@1 | 924 | return w.kind == t.kind && w.type == t.type; |
duke@1 | 925 | } |
duke@1 | 926 | |
duke@1 | 927 | public boolean containsTypeEquivalent(List<Type> ts, List<Type> ss) { |
duke@1 | 928 | while (ts.nonEmpty() && ss.nonEmpty() |
duke@1 | 929 | && containsTypeEquivalent(ts.head, ss.head)) { |
duke@1 | 930 | ts = ts.tail; |
duke@1 | 931 | ss = ss.tail; |
duke@1 | 932 | } |
duke@1 | 933 | return ts.isEmpty() && ss.isEmpty(); |
duke@1 | 934 | } |
duke@1 | 935 | // </editor-fold> |
duke@1 | 936 | |
duke@1 | 937 | // <editor-fold defaultstate="collapsed" desc="isCastable"> |
duke@1 | 938 | public boolean isCastable(Type t, Type s) { |
duke@1 | 939 | return isCastable(t, s, Warner.noWarnings); |
duke@1 | 940 | } |
duke@1 | 941 | |
duke@1 | 942 | /** |
duke@1 | 943 | * Is t is castable to s?<br> |
duke@1 | 944 | * s is assumed to be an erased type.<br> |
duke@1 | 945 | * (not defined for Method and ForAll types). |
duke@1 | 946 | */ |
duke@1 | 947 | public boolean isCastable(Type t, Type s, Warner warn) { |
duke@1 | 948 | if (t == s) |
duke@1 | 949 | return true; |
duke@1 | 950 | |
duke@1 | 951 | if (t.isPrimitive() != s.isPrimitive()) |
jrose@665 | 952 | return allowBoxing && (isConvertible(t, s, warn) || isConvertible(s, t, warn)); |
duke@1 | 953 | |
duke@1 | 954 | if (warn != warnStack.head) { |
duke@1 | 955 | try { |
duke@1 | 956 | warnStack = warnStack.prepend(warn); |
mcimadamore@795 | 957 | checkUnsafeVarargsConversion(t, s, warn); |
mcimadamore@185 | 958 | return isCastable.visit(t,s); |
duke@1 | 959 | } finally { |
duke@1 | 960 | warnStack = warnStack.tail; |
duke@1 | 961 | } |
duke@1 | 962 | } else { |
mcimadamore@185 | 963 | return isCastable.visit(t,s); |
duke@1 | 964 | } |
duke@1 | 965 | } |
duke@1 | 966 | // where |
duke@1 | 967 | private TypeRelation isCastable = new TypeRelation() { |
duke@1 | 968 | |
duke@1 | 969 | public Boolean visitType(Type t, Type s) { |
duke@1 | 970 | if (s.tag == ERROR) |
duke@1 | 971 | return true; |
duke@1 | 972 | |
duke@1 | 973 | switch (t.tag) { |
duke@1 | 974 | case BYTE: case CHAR: case SHORT: case INT: case LONG: case FLOAT: |
duke@1 | 975 | case DOUBLE: |
duke@1 | 976 | return s.tag <= DOUBLE; |
duke@1 | 977 | case BOOLEAN: |
duke@1 | 978 | return s.tag == BOOLEAN; |
duke@1 | 979 | case VOID: |
duke@1 | 980 | return false; |
duke@1 | 981 | case BOT: |
duke@1 | 982 | return isSubtype(t, s); |
duke@1 | 983 | default: |
duke@1 | 984 | throw new AssertionError(); |
duke@1 | 985 | } |
duke@1 | 986 | } |
duke@1 | 987 | |
duke@1 | 988 | @Override |
duke@1 | 989 | public Boolean visitWildcardType(WildcardType t, Type s) { |
duke@1 | 990 | return isCastable(upperBound(t), s, warnStack.head); |
duke@1 | 991 | } |
duke@1 | 992 | |
duke@1 | 993 | @Override |
duke@1 | 994 | public Boolean visitClassType(ClassType t, Type s) { |
duke@1 | 995 | if (s.tag == ERROR || s.tag == BOT) |
duke@1 | 996 | return true; |
duke@1 | 997 | |
duke@1 | 998 | if (s.tag == TYPEVAR) { |
mcimadamore@640 | 999 | if (isCastable(t, s.getUpperBound(), Warner.noWarnings)) { |
mcimadamore@795 | 1000 | warnStack.head.warn(LintCategory.UNCHECKED); |
duke@1 | 1001 | return true; |
duke@1 | 1002 | } else { |
duke@1 | 1003 | return false; |
duke@1 | 1004 | } |
duke@1 | 1005 | } |
duke@1 | 1006 | |
duke@1 | 1007 | if (t.isCompound()) { |
mcimadamore@211 | 1008 | Warner oldWarner = warnStack.head; |
mcimadamore@211 | 1009 | warnStack.head = Warner.noWarnings; |
duke@1 | 1010 | if (!visit(supertype(t), s)) |
duke@1 | 1011 | return false; |
duke@1 | 1012 | for (Type intf : interfaces(t)) { |
duke@1 | 1013 | if (!visit(intf, s)) |
duke@1 | 1014 | return false; |
duke@1 | 1015 | } |
mcimadamore@795 | 1016 | if (warnStack.head.hasLint(LintCategory.UNCHECKED)) |
mcimadamore@795 | 1017 | oldWarner.warn(LintCategory.UNCHECKED); |
duke@1 | 1018 | return true; |
duke@1 | 1019 | } |
duke@1 | 1020 | |
duke@1 | 1021 | if (s.isCompound()) { |
duke@1 | 1022 | // call recursively to reuse the above code |
duke@1 | 1023 | return visitClassType((ClassType)s, t); |
duke@1 | 1024 | } |
duke@1 | 1025 | |
duke@1 | 1026 | if (s.tag == CLASS || s.tag == ARRAY) { |
duke@1 | 1027 | boolean upcast; |
duke@1 | 1028 | if ((upcast = isSubtype(erasure(t), erasure(s))) |
duke@1 | 1029 | || isSubtype(erasure(s), erasure(t))) { |
duke@1 | 1030 | if (!upcast && s.tag == ARRAY) { |
duke@1 | 1031 | if (!isReifiable(s)) |
mcimadamore@795 | 1032 | warnStack.head.warn(LintCategory.UNCHECKED); |
duke@1 | 1033 | return true; |
duke@1 | 1034 | } else if (s.isRaw()) { |
duke@1 | 1035 | return true; |
duke@1 | 1036 | } else if (t.isRaw()) { |
duke@1 | 1037 | if (!isUnbounded(s)) |
mcimadamore@795 | 1038 | warnStack.head.warn(LintCategory.UNCHECKED); |
duke@1 | 1039 | return true; |
duke@1 | 1040 | } |
duke@1 | 1041 | // Assume |a| <: |b| |
duke@1 | 1042 | final Type a = upcast ? t : s; |
duke@1 | 1043 | final Type b = upcast ? s : t; |
duke@1 | 1044 | final boolean HIGH = true; |
duke@1 | 1045 | final boolean LOW = false; |
duke@1 | 1046 | final boolean DONT_REWRITE_TYPEVARS = false; |
duke@1 | 1047 | Type aHigh = rewriteQuantifiers(a, HIGH, DONT_REWRITE_TYPEVARS); |
duke@1 | 1048 | Type aLow = rewriteQuantifiers(a, LOW, DONT_REWRITE_TYPEVARS); |
duke@1 | 1049 | Type bHigh = rewriteQuantifiers(b, HIGH, DONT_REWRITE_TYPEVARS); |
duke@1 | 1050 | Type bLow = rewriteQuantifiers(b, LOW, DONT_REWRITE_TYPEVARS); |
duke@1 | 1051 | Type lowSub = asSub(bLow, aLow.tsym); |
duke@1 | 1052 | Type highSub = (lowSub == null) ? null : asSub(bHigh, aHigh.tsym); |
duke@1 | 1053 | if (highSub == null) { |
duke@1 | 1054 | final boolean REWRITE_TYPEVARS = true; |
duke@1 | 1055 | aHigh = rewriteQuantifiers(a, HIGH, REWRITE_TYPEVARS); |
duke@1 | 1056 | aLow = rewriteQuantifiers(a, LOW, REWRITE_TYPEVARS); |
duke@1 | 1057 | bHigh = rewriteQuantifiers(b, HIGH, REWRITE_TYPEVARS); |
duke@1 | 1058 | bLow = rewriteQuantifiers(b, LOW, REWRITE_TYPEVARS); |
duke@1 | 1059 | lowSub = asSub(bLow, aLow.tsym); |
duke@1 | 1060 | highSub = (lowSub == null) ? null : asSub(bHigh, aHigh.tsym); |
duke@1 | 1061 | } |
duke@1 | 1062 | if (highSub != null) { |
jjg@816 | 1063 | if (!(a.tsym == highSub.tsym && a.tsym == lowSub.tsym)) { |
jjg@816 | 1064 | Assert.error(a.tsym + " != " + highSub.tsym + " != " + lowSub.tsym); |
jjg@816 | 1065 | } |
mcimadamore@185 | 1066 | if (!disjointTypes(aHigh.allparams(), highSub.allparams()) |
mcimadamore@185 | 1067 | && !disjointTypes(aHigh.allparams(), lowSub.allparams()) |
mcimadamore@185 | 1068 | && !disjointTypes(aLow.allparams(), highSub.allparams()) |
mcimadamore@185 | 1069 | && !disjointTypes(aLow.allparams(), lowSub.allparams())) { |
mcimadamore@779 | 1070 | if (upcast ? giveWarning(a, b) : |
mcimadamore@235 | 1071 | giveWarning(b, a)) |
mcimadamore@795 | 1072 | warnStack.head.warn(LintCategory.UNCHECKED); |
duke@1 | 1073 | return true; |
duke@1 | 1074 | } |
duke@1 | 1075 | } |
duke@1 | 1076 | if (isReifiable(s)) |
duke@1 | 1077 | return isSubtypeUnchecked(a, b); |
duke@1 | 1078 | else |
duke@1 | 1079 | return isSubtypeUnchecked(a, b, warnStack.head); |
duke@1 | 1080 | } |
duke@1 | 1081 | |
duke@1 | 1082 | // Sidecast |
duke@1 | 1083 | if (s.tag == CLASS) { |
duke@1 | 1084 | if ((s.tsym.flags() & INTERFACE) != 0) { |
duke@1 | 1085 | return ((t.tsym.flags() & FINAL) == 0) |
duke@1 | 1086 | ? sideCast(t, s, warnStack.head) |
duke@1 | 1087 | : sideCastFinal(t, s, warnStack.head); |
duke@1 | 1088 | } else if ((t.tsym.flags() & INTERFACE) != 0) { |
duke@1 | 1089 | return ((s.tsym.flags() & FINAL) == 0) |
duke@1 | 1090 | ? sideCast(t, s, warnStack.head) |
duke@1 | 1091 | : sideCastFinal(t, s, warnStack.head); |
duke@1 | 1092 | } else { |
duke@1 | 1093 | // unrelated class types |
duke@1 | 1094 | return false; |
duke@1 | 1095 | } |
duke@1 | 1096 | } |
duke@1 | 1097 | } |
duke@1 | 1098 | return false; |
duke@1 | 1099 | } |
duke@1 | 1100 | |
duke@1 | 1101 | @Override |
duke@1 | 1102 | public Boolean visitArrayType(ArrayType t, Type s) { |
duke@1 | 1103 | switch (s.tag) { |
duke@1 | 1104 | case ERROR: |
duke@1 | 1105 | case BOT: |
duke@1 | 1106 | return true; |
duke@1 | 1107 | case TYPEVAR: |
duke@1 | 1108 | if (isCastable(s, t, Warner.noWarnings)) { |
mcimadamore@795 | 1109 | warnStack.head.warn(LintCategory.UNCHECKED); |
duke@1 | 1110 | return true; |
duke@1 | 1111 | } else { |
duke@1 | 1112 | return false; |
duke@1 | 1113 | } |
duke@1 | 1114 | case CLASS: |
duke@1 | 1115 | return isSubtype(t, s); |
duke@1 | 1116 | case ARRAY: |
mcimadamore@786 | 1117 | if (elemtype(t).tag <= lastBaseTag || |
mcimadamore@786 | 1118 | elemtype(s).tag <= lastBaseTag) { |
duke@1 | 1119 | return elemtype(t).tag == elemtype(s).tag; |
duke@1 | 1120 | } else { |
duke@1 | 1121 | return visit(elemtype(t), elemtype(s)); |
duke@1 | 1122 | } |
duke@1 | 1123 | default: |
duke@1 | 1124 | return false; |
duke@1 | 1125 | } |
duke@1 | 1126 | } |
duke@1 | 1127 | |
duke@1 | 1128 | @Override |
duke@1 | 1129 | public Boolean visitTypeVar(TypeVar t, Type s) { |
duke@1 | 1130 | switch (s.tag) { |
duke@1 | 1131 | case ERROR: |
duke@1 | 1132 | case BOT: |
duke@1 | 1133 | return true; |
duke@1 | 1134 | case TYPEVAR: |
duke@1 | 1135 | if (isSubtype(t, s)) { |
duke@1 | 1136 | return true; |
duke@1 | 1137 | } else if (isCastable(t.bound, s, Warner.noWarnings)) { |
mcimadamore@795 | 1138 | warnStack.head.warn(LintCategory.UNCHECKED); |
duke@1 | 1139 | return true; |
duke@1 | 1140 | } else { |
duke@1 | 1141 | return false; |
duke@1 | 1142 | } |
duke@1 | 1143 | default: |
duke@1 | 1144 | return isCastable(t.bound, s, warnStack.head); |
duke@1 | 1145 | } |
duke@1 | 1146 | } |
duke@1 | 1147 | |
duke@1 | 1148 | @Override |
duke@1 | 1149 | public Boolean visitErrorType(ErrorType t, Type s) { |
duke@1 | 1150 | return true; |
duke@1 | 1151 | } |
duke@1 | 1152 | }; |
duke@1 | 1153 | // </editor-fold> |
duke@1 | 1154 | |
duke@1 | 1155 | // <editor-fold defaultstate="collapsed" desc="disjointTypes"> |
duke@1 | 1156 | public boolean disjointTypes(List<Type> ts, List<Type> ss) { |
duke@1 | 1157 | while (ts.tail != null && ss.tail != null) { |
duke@1 | 1158 | if (disjointType(ts.head, ss.head)) return true; |
duke@1 | 1159 | ts = ts.tail; |
duke@1 | 1160 | ss = ss.tail; |
duke@1 | 1161 | } |
duke@1 | 1162 | return false; |
duke@1 | 1163 | } |
duke@1 | 1164 | |
duke@1 | 1165 | /** |
duke@1 | 1166 | * Two types or wildcards are considered disjoint if it can be |
duke@1 | 1167 | * proven that no type can be contained in both. It is |
duke@1 | 1168 | * conservative in that it is allowed to say that two types are |
duke@1 | 1169 | * not disjoint, even though they actually are. |
duke@1 | 1170 | * |
duke@1 | 1171 | * The type C<X> is castable to C<Y> exactly if X and Y are not |
duke@1 | 1172 | * disjoint. |
duke@1 | 1173 | */ |
duke@1 | 1174 | public boolean disjointType(Type t, Type s) { |
duke@1 | 1175 | return disjointType.visit(t, s); |
duke@1 | 1176 | } |
duke@1 | 1177 | // where |
duke@1 | 1178 | private TypeRelation disjointType = new TypeRelation() { |
duke@1 | 1179 | |
duke@1 | 1180 | private Set<TypePair> cache = new HashSet<TypePair>(); |
duke@1 | 1181 | |
duke@1 | 1182 | public Boolean visitType(Type t, Type s) { |
duke@1 | 1183 | if (s.tag == WILDCARD) |
duke@1 | 1184 | return visit(s, t); |
duke@1 | 1185 | else |
duke@1 | 1186 | return notSoftSubtypeRecursive(t, s) || notSoftSubtypeRecursive(s, t); |
duke@1 | 1187 | } |
duke@1 | 1188 | |
duke@1 | 1189 | private boolean isCastableRecursive(Type t, Type s) { |
duke@1 | 1190 | TypePair pair = new TypePair(t, s); |
duke@1 | 1191 | if (cache.add(pair)) { |
duke@1 | 1192 | try { |
duke@1 | 1193 | return Types.this.isCastable(t, s); |
duke@1 | 1194 | } finally { |
duke@1 | 1195 | cache.remove(pair); |
duke@1 | 1196 | } |
duke@1 | 1197 | } else { |
duke@1 | 1198 | return true; |
duke@1 | 1199 | } |
duke@1 | 1200 | } |
duke@1 | 1201 | |
duke@1 | 1202 | private boolean notSoftSubtypeRecursive(Type t, Type s) { |
duke@1 | 1203 | TypePair pair = new TypePair(t, s); |
duke@1 | 1204 | if (cache.add(pair)) { |
duke@1 | 1205 | try { |
duke@1 | 1206 | return Types.this.notSoftSubtype(t, s); |
duke@1 | 1207 | } finally { |
duke@1 | 1208 | cache.remove(pair); |
duke@1 | 1209 | } |
duke@1 | 1210 | } else { |
duke@1 | 1211 | return false; |
duke@1 | 1212 | } |
duke@1 | 1213 | } |
duke@1 | 1214 | |
duke@1 | 1215 | @Override |
duke@1 | 1216 | public Boolean visitWildcardType(WildcardType t, Type s) { |
duke@1 | 1217 | if (t.isUnbound()) |
duke@1 | 1218 | return false; |
duke@1 | 1219 | |
duke@1 | 1220 | if (s.tag != WILDCARD) { |
duke@1 | 1221 | if (t.isExtendsBound()) |
duke@1 | 1222 | return notSoftSubtypeRecursive(s, t.type); |
duke@1 | 1223 | else // isSuperBound() |
duke@1 | 1224 | return notSoftSubtypeRecursive(t.type, s); |
duke@1 | 1225 | } |
duke@1 | 1226 | |
duke@1 | 1227 | if (s.isUnbound()) |
duke@1 | 1228 | return false; |
duke@1 | 1229 | |
duke@1 | 1230 | if (t.isExtendsBound()) { |
duke@1 | 1231 | if (s.isExtendsBound()) |
duke@1 | 1232 | return !isCastableRecursive(t.type, upperBound(s)); |
duke@1 | 1233 | else if (s.isSuperBound()) |
duke@1 | 1234 | return notSoftSubtypeRecursive(lowerBound(s), t.type); |
duke@1 | 1235 | } else if (t.isSuperBound()) { |
duke@1 | 1236 | if (s.isExtendsBound()) |
duke@1 | 1237 | return notSoftSubtypeRecursive(t.type, upperBound(s)); |
duke@1 | 1238 | } |
duke@1 | 1239 | return false; |
duke@1 | 1240 | } |
duke@1 | 1241 | }; |
duke@1 | 1242 | // </editor-fold> |
duke@1 | 1243 | |
duke@1 | 1244 | // <editor-fold defaultstate="collapsed" desc="lowerBoundArgtypes"> |
duke@1 | 1245 | /** |
duke@1 | 1246 | * Returns the lower bounds of the formals of a method. |
duke@1 | 1247 | */ |
duke@1 | 1248 | public List<Type> lowerBoundArgtypes(Type t) { |
duke@1 | 1249 | return map(t.getParameterTypes(), lowerBoundMapping); |
duke@1 | 1250 | } |
duke@1 | 1251 | private final Mapping lowerBoundMapping = new Mapping("lowerBound") { |
duke@1 | 1252 | public Type apply(Type t) { |
duke@1 | 1253 | return lowerBound(t); |
duke@1 | 1254 | } |
duke@1 | 1255 | }; |
duke@1 | 1256 | // </editor-fold> |
duke@1 | 1257 | |
duke@1 | 1258 | // <editor-fold defaultstate="collapsed" desc="notSoftSubtype"> |
duke@1 | 1259 | /** |
duke@1 | 1260 | * This relation answers the question: is impossible that |
duke@1 | 1261 | * something of type `t' can be a subtype of `s'? This is |
duke@1 | 1262 | * different from the question "is `t' not a subtype of `s'?" |
duke@1 | 1263 | * when type variables are involved: Integer is not a subtype of T |
duke@1 | 1264 | * where <T extends Number> but it is not true that Integer cannot |
duke@1 | 1265 | * possibly be a subtype of T. |
duke@1 | 1266 | */ |
duke@1 | 1267 | public boolean notSoftSubtype(Type t, Type s) { |
duke@1 | 1268 | if (t == s) return false; |
duke@1 | 1269 | if (t.tag == TYPEVAR) { |
duke@1 | 1270 | TypeVar tv = (TypeVar) t; |
duke@1 | 1271 | return !isCastable(tv.bound, |
mcimadamore@640 | 1272 | relaxBound(s), |
duke@1 | 1273 | Warner.noWarnings); |
duke@1 | 1274 | } |
duke@1 | 1275 | if (s.tag != WILDCARD) |
duke@1 | 1276 | s = upperBound(s); |
mcimadamore@640 | 1277 | |
mcimadamore@640 | 1278 | return !isSubtype(t, relaxBound(s)); |
mcimadamore@640 | 1279 | } |
mcimadamore@640 | 1280 | |
mcimadamore@640 | 1281 | private Type relaxBound(Type t) { |
mcimadamore@640 | 1282 | if (t.tag == TYPEVAR) { |
mcimadamore@640 | 1283 | while (t.tag == TYPEVAR) |
mcimadamore@640 | 1284 | t = t.getUpperBound(); |
mcimadamore@640 | 1285 | t = rewriteQuantifiers(t, true, true); |
mcimadamore@640 | 1286 | } |
mcimadamore@640 | 1287 | return t; |
duke@1 | 1288 | } |
duke@1 | 1289 | // </editor-fold> |
duke@1 | 1290 | |
duke@1 | 1291 | // <editor-fold defaultstate="collapsed" desc="isReifiable"> |
duke@1 | 1292 | public boolean isReifiable(Type t) { |
duke@1 | 1293 | return isReifiable.visit(t); |
duke@1 | 1294 | } |
duke@1 | 1295 | // where |
duke@1 | 1296 | private UnaryVisitor<Boolean> isReifiable = new UnaryVisitor<Boolean>() { |
duke@1 | 1297 | |
duke@1 | 1298 | public Boolean visitType(Type t, Void ignored) { |
duke@1 | 1299 | return true; |
duke@1 | 1300 | } |
duke@1 | 1301 | |
duke@1 | 1302 | @Override |
duke@1 | 1303 | public Boolean visitClassType(ClassType t, Void ignored) { |
mcimadamore@356 | 1304 | if (t.isCompound()) |
mcimadamore@356 | 1305 | return false; |
mcimadamore@356 | 1306 | else { |
mcimadamore@356 | 1307 | if (!t.isParameterized()) |
mcimadamore@356 | 1308 | return true; |
mcimadamore@356 | 1309 | |
mcimadamore@356 | 1310 | for (Type param : t.allparams()) { |
mcimadamore@356 | 1311 | if (!param.isUnbound()) |
mcimadamore@356 | 1312 | return false; |
mcimadamore@356 | 1313 | } |
duke@1 | 1314 | return true; |
duke@1 | 1315 | } |
duke@1 | 1316 | } |
duke@1 | 1317 | |
duke@1 | 1318 | @Override |
duke@1 | 1319 | public Boolean visitArrayType(ArrayType t, Void ignored) { |
duke@1 | 1320 | return visit(t.elemtype); |
duke@1 | 1321 | } |
duke@1 | 1322 | |
duke@1 | 1323 | @Override |
duke@1 | 1324 | public Boolean visitTypeVar(TypeVar t, Void ignored) { |
duke@1 | 1325 | return false; |
duke@1 | 1326 | } |
duke@1 | 1327 | }; |
duke@1 | 1328 | // </editor-fold> |
duke@1 | 1329 | |
duke@1 | 1330 | // <editor-fold defaultstate="collapsed" desc="Array Utils"> |
duke@1 | 1331 | public boolean isArray(Type t) { |
duke@1 | 1332 | while (t.tag == WILDCARD) |
duke@1 | 1333 | t = upperBound(t); |
duke@1 | 1334 | return t.tag == ARRAY; |
duke@1 | 1335 | } |
duke@1 | 1336 | |
duke@1 | 1337 | /** |
duke@1 | 1338 | * The element type of an array. |
duke@1 | 1339 | */ |
duke@1 | 1340 | public Type elemtype(Type t) { |
duke@1 | 1341 | switch (t.tag) { |
duke@1 | 1342 | case WILDCARD: |
duke@1 | 1343 | return elemtype(upperBound(t)); |
duke@1 | 1344 | case ARRAY: |
duke@1 | 1345 | return ((ArrayType)t).elemtype; |
duke@1 | 1346 | case FORALL: |
duke@1 | 1347 | return elemtype(((ForAll)t).qtype); |
duke@1 | 1348 | case ERROR: |
duke@1 | 1349 | return t; |
duke@1 | 1350 | default: |
duke@1 | 1351 | return null; |
duke@1 | 1352 | } |
duke@1 | 1353 | } |
duke@1 | 1354 | |
mcimadamore@787 | 1355 | public Type elemtypeOrType(Type t) { |
mcimadamore@787 | 1356 | Type elemtype = elemtype(t); |
mcimadamore@787 | 1357 | return elemtype != null ? |
mcimadamore@787 | 1358 | elemtype : |
mcimadamore@787 | 1359 | t; |
mcimadamore@787 | 1360 | } |
mcimadamore@787 | 1361 | |
duke@1 | 1362 | /** |
duke@1 | 1363 | * Mapping to take element type of an arraytype |
duke@1 | 1364 | */ |
duke@1 | 1365 | private Mapping elemTypeFun = new Mapping ("elemTypeFun") { |
duke@1 | 1366 | public Type apply(Type t) { return elemtype(t); } |
duke@1 | 1367 | }; |
duke@1 | 1368 | |
duke@1 | 1369 | /** |
duke@1 | 1370 | * The number of dimensions of an array type. |
duke@1 | 1371 | */ |
duke@1 | 1372 | public int dimensions(Type t) { |
duke@1 | 1373 | int result = 0; |
duke@1 | 1374 | while (t.tag == ARRAY) { |
duke@1 | 1375 | result++; |
duke@1 | 1376 | t = elemtype(t); |
duke@1 | 1377 | } |
duke@1 | 1378 | return result; |
duke@1 | 1379 | } |
duke@1 | 1380 | // </editor-fold> |
duke@1 | 1381 | |
duke@1 | 1382 | // <editor-fold defaultstate="collapsed" desc="asSuper"> |
duke@1 | 1383 | /** |
duke@1 | 1384 | * Return the (most specific) base type of t that starts with the |
duke@1 | 1385 | * given symbol. If none exists, return null. |
duke@1 | 1386 | * |
duke@1 | 1387 | * @param t a type |
duke@1 | 1388 | * @param sym a symbol |
duke@1 | 1389 | */ |
duke@1 | 1390 | public Type asSuper(Type t, Symbol sym) { |
duke@1 | 1391 | return asSuper.visit(t, sym); |
duke@1 | 1392 | } |
duke@1 | 1393 | // where |
duke@1 | 1394 | private SimpleVisitor<Type,Symbol> asSuper = new SimpleVisitor<Type,Symbol>() { |
duke@1 | 1395 | |
duke@1 | 1396 | public Type visitType(Type t, Symbol sym) { |
duke@1 | 1397 | return null; |
duke@1 | 1398 | } |
duke@1 | 1399 | |
duke@1 | 1400 | @Override |
duke@1 | 1401 | public Type visitClassType(ClassType t, Symbol sym) { |
duke@1 | 1402 | if (t.tsym == sym) |
duke@1 | 1403 | return t; |
duke@1 | 1404 | |
duke@1 | 1405 | Type st = supertype(t); |
mcimadamore@19 | 1406 | if (st.tag == CLASS || st.tag == TYPEVAR || st.tag == ERROR) { |
duke@1 | 1407 | Type x = asSuper(st, sym); |
duke@1 | 1408 | if (x != null) |
duke@1 | 1409 | return x; |
duke@1 | 1410 | } |
duke@1 | 1411 | if ((sym.flags() & INTERFACE) != 0) { |
duke@1 | 1412 | for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail) { |
duke@1 | 1413 | Type x = asSuper(l.head, sym); |
duke@1 | 1414 | if (x != null) |
duke@1 | 1415 | return x; |
duke@1 | 1416 | } |
duke@1 | 1417 | } |
duke@1 | 1418 | return null; |
duke@1 | 1419 | } |
duke@1 | 1420 | |
duke@1 | 1421 | @Override |
duke@1 | 1422 | public Type visitArrayType(ArrayType t, Symbol sym) { |
duke@1 | 1423 | return isSubtype(t, sym.type) ? sym.type : null; |
duke@1 | 1424 | } |
duke@1 | 1425 | |
duke@1 | 1426 | @Override |
duke@1 | 1427 | public Type visitTypeVar(TypeVar t, Symbol sym) { |
mcimadamore@19 | 1428 | if (t.tsym == sym) |
mcimadamore@19 | 1429 | return t; |
mcimadamore@19 | 1430 | else |
mcimadamore@19 | 1431 | return asSuper(t.bound, sym); |
duke@1 | 1432 | } |
duke@1 | 1433 | |
duke@1 | 1434 | @Override |
duke@1 | 1435 | public Type visitErrorType(ErrorType t, Symbol sym) { |
duke@1 | 1436 | return t; |
duke@1 | 1437 | } |
duke@1 | 1438 | }; |
duke@1 | 1439 | |
duke@1 | 1440 | /** |
duke@1 | 1441 | * Return the base type of t or any of its outer types that starts |
duke@1 | 1442 | * with the given symbol. If none exists, return null. |
duke@1 | 1443 | * |
duke@1 | 1444 | * @param t a type |
duke@1 | 1445 | * @param sym a symbol |
duke@1 | 1446 | */ |
duke@1 | 1447 | public Type asOuterSuper(Type t, Symbol sym) { |
duke@1 | 1448 | switch (t.tag) { |
duke@1 | 1449 | case CLASS: |
duke@1 | 1450 | do { |
duke@1 | 1451 | Type s = asSuper(t, sym); |
duke@1 | 1452 | if (s != null) return s; |
duke@1 | 1453 | t = t.getEnclosingType(); |
duke@1 | 1454 | } while (t.tag == CLASS); |
duke@1 | 1455 | return null; |
duke@1 | 1456 | case ARRAY: |
duke@1 | 1457 | return isSubtype(t, sym.type) ? sym.type : null; |
duke@1 | 1458 | case TYPEVAR: |
duke@1 | 1459 | return asSuper(t, sym); |
duke@1 | 1460 | case ERROR: |
duke@1 | 1461 | return t; |
duke@1 | 1462 | default: |
duke@1 | 1463 | return null; |
duke@1 | 1464 | } |
duke@1 | 1465 | } |
duke@1 | 1466 | |
duke@1 | 1467 | /** |
duke@1 | 1468 | * Return the base type of t or any of its enclosing types that |
duke@1 | 1469 | * starts with the given symbol. If none exists, return null. |
duke@1 | 1470 | * |
duke@1 | 1471 | * @param t a type |
duke@1 | 1472 | * @param sym a symbol |
duke@1 | 1473 | */ |
duke@1 | 1474 | public Type asEnclosingSuper(Type t, Symbol sym) { |
duke@1 | 1475 | switch (t.tag) { |
duke@1 | 1476 | case CLASS: |
duke@1 | 1477 | do { |
duke@1 | 1478 | Type s = asSuper(t, sym); |
duke@1 | 1479 | if (s != null) return s; |
duke@1 | 1480 | Type outer = t.getEnclosingType(); |
duke@1 | 1481 | t = (outer.tag == CLASS) ? outer : |
duke@1 | 1482 | (t.tsym.owner.enclClass() != null) ? t.tsym.owner.enclClass().type : |
duke@1 | 1483 | Type.noType; |
duke@1 | 1484 | } while (t.tag == CLASS); |
duke@1 | 1485 | return null; |
duke@1 | 1486 | case ARRAY: |
duke@1 | 1487 | return isSubtype(t, sym.type) ? sym.type : null; |
duke@1 | 1488 | case TYPEVAR: |
duke@1 | 1489 | return asSuper(t, sym); |
duke@1 | 1490 | case ERROR: |
duke@1 | 1491 | return t; |
duke@1 | 1492 | default: |
duke@1 | 1493 | return null; |
duke@1 | 1494 | } |
duke@1 | 1495 | } |
duke@1 | 1496 | // </editor-fold> |
duke@1 | 1497 | |
duke@1 | 1498 | // <editor-fold defaultstate="collapsed" desc="memberType"> |
duke@1 | 1499 | /** |
duke@1 | 1500 | * The type of given symbol, seen as a member of t. |
duke@1 | 1501 | * |
duke@1 | 1502 | * @param t a type |
duke@1 | 1503 | * @param sym a symbol |
duke@1 | 1504 | */ |
duke@1 | 1505 | public Type memberType(Type t, Symbol sym) { |
duke@1 | 1506 | return (sym.flags() & STATIC) != 0 |
duke@1 | 1507 | ? sym.type |
duke@1 | 1508 | : memberType.visit(t, sym); |
mcimadamore@341 | 1509 | } |
duke@1 | 1510 | // where |
duke@1 | 1511 | private SimpleVisitor<Type,Symbol> memberType = new SimpleVisitor<Type,Symbol>() { |
duke@1 | 1512 | |
duke@1 | 1513 | public Type visitType(Type t, Symbol sym) { |
duke@1 | 1514 | return sym.type; |
duke@1 | 1515 | } |
duke@1 | 1516 | |
duke@1 | 1517 | @Override |
duke@1 | 1518 | public Type visitWildcardType(WildcardType t, Symbol sym) { |
duke@1 | 1519 | return memberType(upperBound(t), sym); |
duke@1 | 1520 | } |
duke@1 | 1521 | |
duke@1 | 1522 | @Override |
duke@1 | 1523 | public Type visitClassType(ClassType t, Symbol sym) { |
duke@1 | 1524 | Symbol owner = sym.owner; |
duke@1 | 1525 | long flags = sym.flags(); |
duke@1 | 1526 | if (((flags & STATIC) == 0) && owner.type.isParameterized()) { |
duke@1 | 1527 | Type base = asOuterSuper(t, owner); |
mcimadamore@134 | 1528 | //if t is an intersection type T = CT & I1 & I2 ... & In |
mcimadamore@134 | 1529 | //its supertypes CT, I1, ... In might contain wildcards |
mcimadamore@134 | 1530 | //so we need to go through capture conversion |
mcimadamore@134 | 1531 | base = t.isCompound() ? capture(base) : base; |
duke@1 | 1532 | if (base != null) { |
duke@1 | 1533 | List<Type> ownerParams = owner.type.allparams(); |
duke@1 | 1534 | List<Type> baseParams = base.allparams(); |
duke@1 | 1535 | if (ownerParams.nonEmpty()) { |
duke@1 | 1536 | if (baseParams.isEmpty()) { |
duke@1 | 1537 | // then base is a raw type |
duke@1 | 1538 | return erasure(sym.type); |
duke@1 | 1539 | } else { |
duke@1 | 1540 | return subst(sym.type, ownerParams, baseParams); |
duke@1 | 1541 | } |
duke@1 | 1542 | } |
duke@1 | 1543 | } |
duke@1 | 1544 | } |
duke@1 | 1545 | return sym.type; |
duke@1 | 1546 | } |
duke@1 | 1547 | |
duke@1 | 1548 | @Override |
duke@1 | 1549 | public Type visitTypeVar(TypeVar t, Symbol sym) { |
duke@1 | 1550 | return memberType(t.bound, sym); |
duke@1 | 1551 | } |
duke@1 | 1552 | |
duke@1 | 1553 | @Override |
duke@1 | 1554 | public Type visitErrorType(ErrorType t, Symbol sym) { |
duke@1 | 1555 | return t; |
duke@1 | 1556 | } |
duke@1 | 1557 | }; |
duke@1 | 1558 | // </editor-fold> |
duke@1 | 1559 | |
duke@1 | 1560 | // <editor-fold defaultstate="collapsed" desc="isAssignable"> |
duke@1 | 1561 | public boolean isAssignable(Type t, Type s) { |
duke@1 | 1562 | return isAssignable(t, s, Warner.noWarnings); |
duke@1 | 1563 | } |
duke@1 | 1564 | |
duke@1 | 1565 | /** |
duke@1 | 1566 | * Is t assignable to s?<br> |
duke@1 | 1567 | * Equivalent to subtype except for constant values and raw |
duke@1 | 1568 | * types.<br> |
duke@1 | 1569 | * (not defined for Method and ForAll types) |
duke@1 | 1570 | */ |
duke@1 | 1571 | public boolean isAssignable(Type t, Type s, Warner warn) { |
duke@1 | 1572 | if (t.tag == ERROR) |
duke@1 | 1573 | return true; |
duke@1 | 1574 | if (t.tag <= INT && t.constValue() != null) { |
duke@1 | 1575 | int value = ((Number)t.constValue()).intValue(); |
duke@1 | 1576 | switch (s.tag) { |
duke@1 | 1577 | case BYTE: |
duke@1 | 1578 | if (Byte.MIN_VALUE <= value && value <= Byte.MAX_VALUE) |
duke@1 | 1579 | return true; |
duke@1 | 1580 | break; |
duke@1 | 1581 | case CHAR: |
duke@1 | 1582 | if (Character.MIN_VALUE <= value && value <= Character.MAX_VALUE) |
duke@1 | 1583 | return true; |
duke@1 | 1584 | break; |
duke@1 | 1585 | case SHORT: |
duke@1 | 1586 | if (Short.MIN_VALUE <= value && value <= Short.MAX_VALUE) |
duke@1 | 1587 | return true; |
duke@1 | 1588 | break; |
duke@1 | 1589 | case INT: |
duke@1 | 1590 | return true; |
duke@1 | 1591 | case CLASS: |
duke@1 | 1592 | switch (unboxedType(s).tag) { |
duke@1 | 1593 | case BYTE: |
duke@1 | 1594 | case CHAR: |
duke@1 | 1595 | case SHORT: |
duke@1 | 1596 | return isAssignable(t, unboxedType(s), warn); |
duke@1 | 1597 | } |
duke@1 | 1598 | break; |
duke@1 | 1599 | } |
duke@1 | 1600 | } |
duke@1 | 1601 | return isConvertible(t, s, warn); |
duke@1 | 1602 | } |
duke@1 | 1603 | // </editor-fold> |
duke@1 | 1604 | |
duke@1 | 1605 | // <editor-fold defaultstate="collapsed" desc="erasure"> |
duke@1 | 1606 | /** |
duke@1 | 1607 | * The erasure of t {@code |t|} -- the type that results when all |
duke@1 | 1608 | * type parameters in t are deleted. |
duke@1 | 1609 | */ |
duke@1 | 1610 | public Type erasure(Type t) { |
mcimadamore@30 | 1611 | return erasure(t, false); |
mcimadamore@30 | 1612 | } |
mcimadamore@30 | 1613 | //where |
mcimadamore@30 | 1614 | private Type erasure(Type t, boolean recurse) { |
duke@1 | 1615 | if (t.tag <= lastBaseTag) |
duke@1 | 1616 | return t; /* fast special case */ |
duke@1 | 1617 | else |
mcimadamore@30 | 1618 | return erasure.visit(t, recurse); |
mcimadamore@341 | 1619 | } |
duke@1 | 1620 | // where |
mcimadamore@30 | 1621 | private SimpleVisitor<Type, Boolean> erasure = new SimpleVisitor<Type, Boolean>() { |
mcimadamore@30 | 1622 | public Type visitType(Type t, Boolean recurse) { |
duke@1 | 1623 | if (t.tag <= lastBaseTag) |
duke@1 | 1624 | return t; /*fast special case*/ |
duke@1 | 1625 | else |
mcimadamore@30 | 1626 | return t.map(recurse ? erasureRecFun : erasureFun); |
duke@1 | 1627 | } |
duke@1 | 1628 | |
duke@1 | 1629 | @Override |
mcimadamore@30 | 1630 | public Type visitWildcardType(WildcardType t, Boolean recurse) { |
mcimadamore@30 | 1631 | return erasure(upperBound(t), recurse); |
duke@1 | 1632 | } |
duke@1 | 1633 | |
duke@1 | 1634 | @Override |
mcimadamore@30 | 1635 | public Type visitClassType(ClassType t, Boolean recurse) { |
mcimadamore@30 | 1636 | Type erased = t.tsym.erasure(Types.this); |
mcimadamore@30 | 1637 | if (recurse) { |
mcimadamore@30 | 1638 | erased = new ErasedClassType(erased.getEnclosingType(),erased.tsym); |
mcimadamore@30 | 1639 | } |
mcimadamore@30 | 1640 | return erased; |
duke@1 | 1641 | } |
duke@1 | 1642 | |
duke@1 | 1643 | @Override |
mcimadamore@30 | 1644 | public Type visitTypeVar(TypeVar t, Boolean recurse) { |
mcimadamore@30 | 1645 | return erasure(t.bound, recurse); |
duke@1 | 1646 | } |
duke@1 | 1647 | |
duke@1 | 1648 | @Override |
mcimadamore@30 | 1649 | public Type visitErrorType(ErrorType t, Boolean recurse) { |
duke@1 | 1650 | return t; |
duke@1 | 1651 | } |
duke@1 | 1652 | }; |
mcimadamore@30 | 1653 | |
duke@1 | 1654 | private Mapping erasureFun = new Mapping ("erasure") { |
duke@1 | 1655 | public Type apply(Type t) { return erasure(t); } |
duke@1 | 1656 | }; |
duke@1 | 1657 | |
mcimadamore@30 | 1658 | private Mapping erasureRecFun = new Mapping ("erasureRecursive") { |
mcimadamore@30 | 1659 | public Type apply(Type t) { return erasureRecursive(t); } |
mcimadamore@30 | 1660 | }; |
mcimadamore@30 | 1661 | |
duke@1 | 1662 | public List<Type> erasure(List<Type> ts) { |
duke@1 | 1663 | return Type.map(ts, erasureFun); |
duke@1 | 1664 | } |
mcimadamore@30 | 1665 | |
mcimadamore@30 | 1666 | public Type erasureRecursive(Type t) { |
mcimadamore@30 | 1667 | return erasure(t, true); |
mcimadamore@30 | 1668 | } |
mcimadamore@30 | 1669 | |
mcimadamore@30 | 1670 | public List<Type> erasureRecursive(List<Type> ts) { |
mcimadamore@30 | 1671 | return Type.map(ts, erasureRecFun); |
mcimadamore@30 | 1672 | } |
duke@1 | 1673 | // </editor-fold> |
duke@1 | 1674 | |
duke@1 | 1675 | // <editor-fold defaultstate="collapsed" desc="makeCompoundType"> |
duke@1 | 1676 | /** |
duke@1 | 1677 | * Make a compound type from non-empty list of types |
duke@1 | 1678 | * |
duke@1 | 1679 | * @param bounds the types from which the compound type is formed |
duke@1 | 1680 | * @param supertype is objectType if all bounds are interfaces, |
duke@1 | 1681 | * null otherwise. |
duke@1 | 1682 | */ |
duke@1 | 1683 | public Type makeCompoundType(List<Type> bounds, |
duke@1 | 1684 | Type supertype) { |
duke@1 | 1685 | ClassSymbol bc = |
duke@1 | 1686 | new ClassSymbol(ABSTRACT|PUBLIC|SYNTHETIC|COMPOUND|ACYCLIC, |
duke@1 | 1687 | Type.moreInfo |
duke@1 | 1688 | ? names.fromString(bounds.toString()) |
duke@1 | 1689 | : names.empty, |
duke@1 | 1690 | syms.noSymbol); |
duke@1 | 1691 | if (bounds.head.tag == TYPEVAR) |
duke@1 | 1692 | // error condition, recover |
mcimadamore@121 | 1693 | bc.erasure_field = syms.objectType; |
mcimadamore@121 | 1694 | else |
mcimadamore@121 | 1695 | bc.erasure_field = erasure(bounds.head); |
mcimadamore@121 | 1696 | bc.members_field = new Scope(bc); |
duke@1 | 1697 | ClassType bt = (ClassType)bc.type; |
duke@1 | 1698 | bt.allparams_field = List.nil(); |
duke@1 | 1699 | if (supertype != null) { |
duke@1 | 1700 | bt.supertype_field = supertype; |
duke@1 | 1701 | bt.interfaces_field = bounds; |
duke@1 | 1702 | } else { |
duke@1 | 1703 | bt.supertype_field = bounds.head; |
duke@1 | 1704 | bt.interfaces_field = bounds.tail; |
duke@1 | 1705 | } |
jjg@816 | 1706 | Assert.check(bt.supertype_field.tsym.completer != null |
jjg@816 | 1707 | || !bt.supertype_field.isInterface(), |
jjg@816 | 1708 | bt.supertype_field); |
duke@1 | 1709 | return bt; |
duke@1 | 1710 | } |
duke@1 | 1711 | |
duke@1 | 1712 | /** |
duke@1 | 1713 | * Same as {@link #makeCompoundType(List,Type)}, except that the |
duke@1 | 1714 | * second parameter is computed directly. Note that this might |
duke@1 | 1715 | * cause a symbol completion. Hence, this version of |
duke@1 | 1716 | * makeCompoundType may not be called during a classfile read. |
duke@1 | 1717 | */ |
duke@1 | 1718 | public Type makeCompoundType(List<Type> bounds) { |
duke@1 | 1719 | Type supertype = (bounds.head.tsym.flags() & INTERFACE) != 0 ? |
duke@1 | 1720 | supertype(bounds.head) : null; |
duke@1 | 1721 | return makeCompoundType(bounds, supertype); |
duke@1 | 1722 | } |
duke@1 | 1723 | |
duke@1 | 1724 | /** |
duke@1 | 1725 | * A convenience wrapper for {@link #makeCompoundType(List)}; the |
duke@1 | 1726 | * arguments are converted to a list and passed to the other |
duke@1 | 1727 | * method. Note that this might cause a symbol completion. |
duke@1 | 1728 | * Hence, this version of makeCompoundType may not be called |
duke@1 | 1729 | * during a classfile read. |
duke@1 | 1730 | */ |
duke@1 | 1731 | public Type makeCompoundType(Type bound1, Type bound2) { |
duke@1 | 1732 | return makeCompoundType(List.of(bound1, bound2)); |
duke@1 | 1733 | } |
duke@1 | 1734 | // </editor-fold> |
duke@1 | 1735 | |
duke@1 | 1736 | // <editor-fold defaultstate="collapsed" desc="supertype"> |
duke@1 | 1737 | public Type supertype(Type t) { |
duke@1 | 1738 | return supertype.visit(t); |
duke@1 | 1739 | } |
duke@1 | 1740 | // where |
duke@1 | 1741 | private UnaryVisitor<Type> supertype = new UnaryVisitor<Type>() { |
duke@1 | 1742 | |
duke@1 | 1743 | public Type visitType(Type t, Void ignored) { |
duke@1 | 1744 | // A note on wildcards: there is no good way to |
duke@1 | 1745 | // determine a supertype for a super bounded wildcard. |
duke@1 | 1746 | return null; |
duke@1 | 1747 | } |
duke@1 | 1748 | |
duke@1 | 1749 | @Override |
duke@1 | 1750 | public Type visitClassType(ClassType t, Void ignored) { |
duke@1 | 1751 | if (t.supertype_field == null) { |
duke@1 | 1752 | Type supertype = ((ClassSymbol)t.tsym).getSuperclass(); |
duke@1 | 1753 | // An interface has no superclass; its supertype is Object. |
duke@1 | 1754 | if (t.isInterface()) |
duke@1 | 1755 | supertype = ((ClassType)t.tsym.type).supertype_field; |
duke@1 | 1756 | if (t.supertype_field == null) { |
duke@1 | 1757 | List<Type> actuals = classBound(t).allparams(); |
duke@1 | 1758 | List<Type> formals = t.tsym.type.allparams(); |
mcimadamore@30 | 1759 | if (t.hasErasedSupertypes()) { |
mcimadamore@30 | 1760 | t.supertype_field = erasureRecursive(supertype); |
mcimadamore@30 | 1761 | } else if (formals.nonEmpty()) { |
duke@1 | 1762 | t.supertype_field = subst(supertype, formals, actuals); |
duke@1 | 1763 | } |
mcimadamore@30 | 1764 | else { |
mcimadamore@30 | 1765 | t.supertype_field = supertype; |
mcimadamore@30 | 1766 | } |
duke@1 | 1767 | } |
duke@1 | 1768 | } |
duke@1 | 1769 | return t.supertype_field; |
duke@1 | 1770 | } |
duke@1 | 1771 | |
duke@1 | 1772 | /** |
duke@1 | 1773 | * The supertype is always a class type. If the type |
duke@1 | 1774 | * variable's bounds start with a class type, this is also |
duke@1 | 1775 | * the supertype. Otherwise, the supertype is |
duke@1 | 1776 | * java.lang.Object. |
duke@1 | 1777 | */ |
duke@1 | 1778 | @Override |
duke@1 | 1779 | public Type visitTypeVar(TypeVar t, Void ignored) { |
duke@1 | 1780 | if (t.bound.tag == TYPEVAR || |
duke@1 | 1781 | (!t.bound.isCompound() && !t.bound.isInterface())) { |
duke@1 | 1782 | return t.bound; |
duke@1 | 1783 | } else { |
duke@1 | 1784 | return supertype(t.bound); |
duke@1 | 1785 | } |
duke@1 | 1786 | } |
duke@1 | 1787 | |
duke@1 | 1788 | @Override |
duke@1 | 1789 | public Type visitArrayType(ArrayType t, Void ignored) { |
duke@1 | 1790 | if (t.elemtype.isPrimitive() || isSameType(t.elemtype, syms.objectType)) |
duke@1 | 1791 | return arraySuperType(); |
duke@1 | 1792 | else |
duke@1 | 1793 | return new ArrayType(supertype(t.elemtype), t.tsym); |
duke@1 | 1794 | } |
duke@1 | 1795 | |
duke@1 | 1796 | @Override |
duke@1 | 1797 | public Type visitErrorType(ErrorType t, Void ignored) { |
duke@1 | 1798 | return t; |
duke@1 | 1799 | } |
duke@1 | 1800 | }; |
duke@1 | 1801 | // </editor-fold> |
duke@1 | 1802 | |
duke@1 | 1803 | // <editor-fold defaultstate="collapsed" desc="interfaces"> |
duke@1 | 1804 | /** |
duke@1 | 1805 | * Return the interfaces implemented by this class. |
duke@1 | 1806 | */ |
duke@1 | 1807 | public List<Type> interfaces(Type t) { |
duke@1 | 1808 | return interfaces.visit(t); |
duke@1 | 1809 | } |
duke@1 | 1810 | // where |
duke@1 | 1811 | private UnaryVisitor<List<Type>> interfaces = new UnaryVisitor<List<Type>>() { |
duke@1 | 1812 | |
duke@1 | 1813 | public List<Type> visitType(Type t, Void ignored) { |
duke@1 | 1814 | return List.nil(); |
duke@1 | 1815 | } |
duke@1 | 1816 | |
duke@1 | 1817 | @Override |
duke@1 | 1818 | public List<Type> visitClassType(ClassType t, Void ignored) { |
duke@1 | 1819 | if (t.interfaces_field == null) { |
duke@1 | 1820 | List<Type> interfaces = ((ClassSymbol)t.tsym).getInterfaces(); |
duke@1 | 1821 | if (t.interfaces_field == null) { |
duke@1 | 1822 | // If t.interfaces_field is null, then t must |
duke@1 | 1823 | // be a parameterized type (not to be confused |
duke@1 | 1824 | // with a generic type declaration). |
duke@1 | 1825 | // Terminology: |
duke@1 | 1826 | // Parameterized type: List<String> |
duke@1 | 1827 | // Generic type declaration: class List<E> { ... } |
duke@1 | 1828 | // So t corresponds to List<String> and |
duke@1 | 1829 | // t.tsym.type corresponds to List<E>. |
duke@1 | 1830 | // The reason t must be parameterized type is |
duke@1 | 1831 | // that completion will happen as a side |
duke@1 | 1832 | // effect of calling |
duke@1 | 1833 | // ClassSymbol.getInterfaces. Since |
duke@1 | 1834 | // t.interfaces_field is null after |
duke@1 | 1835 | // completion, we can assume that t is not the |
duke@1 | 1836 | // type of a class/interface declaration. |
jjg@816 | 1837 | Assert.check(t != t.tsym.type, t); |
duke@1 | 1838 | List<Type> actuals = t.allparams(); |
duke@1 | 1839 | List<Type> formals = t.tsym.type.allparams(); |
mcimadamore@30 | 1840 | if (t.hasErasedSupertypes()) { |
mcimadamore@30 | 1841 | t.interfaces_field = erasureRecursive(interfaces); |
mcimadamore@30 | 1842 | } else if (formals.nonEmpty()) { |
duke@1 | 1843 | t.interfaces_field = |
duke@1 | 1844 | upperBounds(subst(interfaces, formals, actuals)); |
duke@1 | 1845 | } |
mcimadamore@30 | 1846 | else { |
mcimadamore@30 | 1847 | t.interfaces_field = interfaces; |
mcimadamore@30 | 1848 | } |
duke@1 | 1849 | } |
duke@1 | 1850 | } |
duke@1 | 1851 | return t.interfaces_field; |
duke@1 | 1852 | } |
duke@1 | 1853 | |
duke@1 | 1854 | @Override |
duke@1 | 1855 | public List<Type> visitTypeVar(TypeVar t, Void ignored) { |
duke@1 | 1856 | if (t.bound.isCompound()) |
duke@1 | 1857 | return interfaces(t.bound); |
duke@1 | 1858 | |
duke@1 | 1859 | if (t.bound.isInterface()) |
duke@1 | 1860 | return List.of(t.bound); |
duke@1 | 1861 | |
duke@1 | 1862 | return List.nil(); |
duke@1 | 1863 | } |
duke@1 | 1864 | }; |
duke@1 | 1865 | // </editor-fold> |
duke@1 | 1866 | |
duke@1 | 1867 | // <editor-fold defaultstate="collapsed" desc="isDerivedRaw"> |
duke@1 | 1868 | Map<Type,Boolean> isDerivedRawCache = new HashMap<Type,Boolean>(); |
duke@1 | 1869 | |
duke@1 | 1870 | public boolean isDerivedRaw(Type t) { |
duke@1 | 1871 | Boolean result = isDerivedRawCache.get(t); |
duke@1 | 1872 | if (result == null) { |
duke@1 | 1873 | result = isDerivedRawInternal(t); |
duke@1 | 1874 | isDerivedRawCache.put(t, result); |
duke@1 | 1875 | } |
duke@1 | 1876 | return result; |
duke@1 | 1877 | } |
duke@1 | 1878 | |
duke@1 | 1879 | public boolean isDerivedRawInternal(Type t) { |
duke@1 | 1880 | if (t.isErroneous()) |
duke@1 | 1881 | return false; |
duke@1 | 1882 | return |
duke@1 | 1883 | t.isRaw() || |
duke@1 | 1884 | supertype(t) != null && isDerivedRaw(supertype(t)) || |
duke@1 | 1885 | isDerivedRaw(interfaces(t)); |
duke@1 | 1886 | } |
duke@1 | 1887 | |
duke@1 | 1888 | public boolean isDerivedRaw(List<Type> ts) { |
duke@1 | 1889 | List<Type> l = ts; |
duke@1 | 1890 | while (l.nonEmpty() && !isDerivedRaw(l.head)) l = l.tail; |
duke@1 | 1891 | return l.nonEmpty(); |
duke@1 | 1892 | } |
duke@1 | 1893 | // </editor-fold> |
duke@1 | 1894 | |
duke@1 | 1895 | // <editor-fold defaultstate="collapsed" desc="setBounds"> |
duke@1 | 1896 | /** |
duke@1 | 1897 | * Set the bounds field of the given type variable to reflect a |
duke@1 | 1898 | * (possibly multiple) list of bounds. |
duke@1 | 1899 | * @param t a type variable |
duke@1 | 1900 | * @param bounds the bounds, must be nonempty |
duke@1 | 1901 | * @param supertype is objectType if all bounds are interfaces, |
duke@1 | 1902 | * null otherwise. |
duke@1 | 1903 | */ |
duke@1 | 1904 | public void setBounds(TypeVar t, List<Type> bounds, Type supertype) { |
duke@1 | 1905 | if (bounds.tail.isEmpty()) |
duke@1 | 1906 | t.bound = bounds.head; |
duke@1 | 1907 | else |
duke@1 | 1908 | t.bound = makeCompoundType(bounds, supertype); |
duke@1 | 1909 | t.rank_field = -1; |
duke@1 | 1910 | } |
duke@1 | 1911 | |
duke@1 | 1912 | /** |
duke@1 | 1913 | * Same as {@link #setBounds(Type.TypeVar,List,Type)}, except that |
mcimadamore@563 | 1914 | * third parameter is computed directly, as follows: if all |
mcimadamore@563 | 1915 | * all bounds are interface types, the computed supertype is Object, |
mcimadamore@563 | 1916 | * otherwise the supertype is simply left null (in this case, the supertype |
mcimadamore@563 | 1917 | * is assumed to be the head of the bound list passed as second argument). |
mcimadamore@563 | 1918 | * Note that this check might cause a symbol completion. Hence, this version of |
duke@1 | 1919 | * setBounds may not be called during a classfile read. |
duke@1 | 1920 | */ |
duke@1 | 1921 | public void setBounds(TypeVar t, List<Type> bounds) { |
duke@1 | 1922 | Type supertype = (bounds.head.tsym.flags() & INTERFACE) != 0 ? |
mcimadamore@563 | 1923 | syms.objectType : null; |
duke@1 | 1924 | setBounds(t, bounds, supertype); |
duke@1 | 1925 | t.rank_field = -1; |
duke@1 | 1926 | } |
duke@1 | 1927 | // </editor-fold> |
duke@1 | 1928 | |
duke@1 | 1929 | // <editor-fold defaultstate="collapsed" desc="getBounds"> |
duke@1 | 1930 | /** |
duke@1 | 1931 | * Return list of bounds of the given type variable. |
duke@1 | 1932 | */ |
duke@1 | 1933 | public List<Type> getBounds(TypeVar t) { |
duke@1 | 1934 | if (t.bound.isErroneous() || !t.bound.isCompound()) |
duke@1 | 1935 | return List.of(t.bound); |
duke@1 | 1936 | else if ((erasure(t).tsym.flags() & INTERFACE) == 0) |
duke@1 | 1937 | return interfaces(t).prepend(supertype(t)); |
duke@1 | 1938 | else |
duke@1 | 1939 | // No superclass was given in bounds. |
duke@1 | 1940 | // In this case, supertype is Object, erasure is first interface. |
duke@1 | 1941 | return interfaces(t); |
duke@1 | 1942 | } |
duke@1 | 1943 | // </editor-fold> |
duke@1 | 1944 | |
duke@1 | 1945 | // <editor-fold defaultstate="collapsed" desc="classBound"> |
duke@1 | 1946 | /** |
duke@1 | 1947 | * If the given type is a (possibly selected) type variable, |
duke@1 | 1948 | * return the bounding class of this type, otherwise return the |
duke@1 | 1949 | * type itself. |
duke@1 | 1950 | */ |
duke@1 | 1951 | public Type classBound(Type t) { |
duke@1 | 1952 | return classBound.visit(t); |
duke@1 | 1953 | } |
duke@1 | 1954 | // where |
duke@1 | 1955 | private UnaryVisitor<Type> classBound = new UnaryVisitor<Type>() { |
duke@1 | 1956 | |
duke@1 | 1957 | public Type visitType(Type t, Void ignored) { |
duke@1 | 1958 | return t; |
duke@1 | 1959 | } |
duke@1 | 1960 | |
duke@1 | 1961 | @Override |
duke@1 | 1962 | public Type visitClassType(ClassType t, Void ignored) { |
duke@1 | 1963 | Type outer1 = classBound(t.getEnclosingType()); |
duke@1 | 1964 | if (outer1 != t.getEnclosingType()) |
duke@1 | 1965 | return new ClassType(outer1, t.getTypeArguments(), t.tsym); |
duke@1 | 1966 | else |
duke@1 | 1967 | return t; |
duke@1 | 1968 | } |
duke@1 | 1969 | |
duke@1 | 1970 | @Override |
duke@1 | 1971 | public Type visitTypeVar(TypeVar t, Void ignored) { |
duke@1 | 1972 | return classBound(supertype(t)); |
duke@1 | 1973 | } |
duke@1 | 1974 | |
duke@1 | 1975 | @Override |
duke@1 | 1976 | public Type visitErrorType(ErrorType t, Void ignored) { |
duke@1 | 1977 | return t; |
duke@1 | 1978 | } |
duke@1 | 1979 | }; |
duke@1 | 1980 | // </editor-fold> |
duke@1 | 1981 | |
duke@1 | 1982 | // <editor-fold defaultstate="collapsed" desc="sub signature / override equivalence"> |
duke@1 | 1983 | /** |
duke@1 | 1984 | * Returns true iff the first signature is a <em>sub |
duke@1 | 1985 | * signature</em> of the other. This is <b>not</b> an equivalence |
duke@1 | 1986 | * relation. |
duke@1 | 1987 | * |
duke@1 | 1988 | * @see "The Java Language Specification, Third Ed. (8.4.2)." |
duke@1 | 1989 | * @see #overrideEquivalent(Type t, Type s) |
duke@1 | 1990 | * @param t first signature (possibly raw). |
duke@1 | 1991 | * @param s second signature (could be subjected to erasure). |
duke@1 | 1992 | * @return true if t is a sub signature of s. |
duke@1 | 1993 | */ |
duke@1 | 1994 | public boolean isSubSignature(Type t, Type s) { |
duke@1 | 1995 | return hasSameArgs(t, s) || hasSameArgs(t, erasure(s)); |
duke@1 | 1996 | } |
duke@1 | 1997 | |
duke@1 | 1998 | /** |
duke@1 | 1999 | * Returns true iff these signatures are related by <em>override |
duke@1 | 2000 | * equivalence</em>. This is the natural extension of |
duke@1 | 2001 | * isSubSignature to an equivalence relation. |
duke@1 | 2002 | * |
duke@1 | 2003 | * @see "The Java Language Specification, Third Ed. (8.4.2)." |
duke@1 | 2004 | * @see #isSubSignature(Type t, Type s) |
duke@1 | 2005 | * @param t a signature (possible raw, could be subjected to |
duke@1 | 2006 | * erasure). |
duke@1 | 2007 | * @param s a signature (possible raw, could be subjected to |
duke@1 | 2008 | * erasure). |
duke@1 | 2009 | * @return true if either argument is a sub signature of the other. |
duke@1 | 2010 | */ |
duke@1 | 2011 | public boolean overrideEquivalent(Type t, Type s) { |
duke@1 | 2012 | return hasSameArgs(t, s) || |
duke@1 | 2013 | hasSameArgs(t, erasure(s)) || hasSameArgs(erasure(t), s); |
duke@1 | 2014 | } |
duke@1 | 2015 | |
mcimadamore@673 | 2016 | // <editor-fold defaultstate="collapsed" desc="Determining method implementation in given site"> |
mcimadamore@673 | 2017 | class ImplementationCache { |
mcimadamore@673 | 2018 | |
mcimadamore@673 | 2019 | private WeakHashMap<MethodSymbol, SoftReference<Map<TypeSymbol, Entry>>> _map = |
mcimadamore@673 | 2020 | new WeakHashMap<MethodSymbol, SoftReference<Map<TypeSymbol, Entry>>>(); |
mcimadamore@673 | 2021 | |
mcimadamore@673 | 2022 | class Entry { |
mcimadamore@673 | 2023 | final MethodSymbol cachedImpl; |
mcimadamore@673 | 2024 | final Filter<Symbol> implFilter; |
mcimadamore@673 | 2025 | final boolean checkResult; |
mcimadamore@877 | 2026 | final int prevMark; |
mcimadamore@673 | 2027 | |
mcimadamore@673 | 2028 | public Entry(MethodSymbol cachedImpl, |
mcimadamore@673 | 2029 | Filter<Symbol> scopeFilter, |
mcimadamore@877 | 2030 | boolean checkResult, |
mcimadamore@877 | 2031 | int prevMark) { |
mcimadamore@673 | 2032 | this.cachedImpl = cachedImpl; |
mcimadamore@673 | 2033 | this.implFilter = scopeFilter; |
mcimadamore@673 | 2034 | this.checkResult = checkResult; |
mcimadamore@877 | 2035 | this.prevMark = prevMark; |
mcimadamore@673 | 2036 | } |
mcimadamore@673 | 2037 | |
mcimadamore@877 | 2038 | boolean matches(Filter<Symbol> scopeFilter, boolean checkResult, int mark) { |
mcimadamore@673 | 2039 | return this.implFilter == scopeFilter && |
mcimadamore@877 | 2040 | this.checkResult == checkResult && |
mcimadamore@877 | 2041 | this.prevMark == mark; |
mcimadamore@673 | 2042 | } |
mcimadamore@341 | 2043 | } |
mcimadamore@673 | 2044 | |
mcimadamore@858 | 2045 | MethodSymbol get(MethodSymbol ms, TypeSymbol origin, boolean checkResult, Filter<Symbol> implFilter) { |
mcimadamore@673 | 2046 | SoftReference<Map<TypeSymbol, Entry>> ref_cache = _map.get(ms); |
mcimadamore@673 | 2047 | Map<TypeSymbol, Entry> cache = ref_cache != null ? ref_cache.get() : null; |
mcimadamore@673 | 2048 | if (cache == null) { |
mcimadamore@673 | 2049 | cache = new HashMap<TypeSymbol, Entry>(); |
mcimadamore@673 | 2050 | _map.put(ms, new SoftReference<Map<TypeSymbol, Entry>>(cache)); |
mcimadamore@673 | 2051 | } |
mcimadamore@673 | 2052 | Entry e = cache.get(origin); |
mcimadamore@877 | 2053 | CompoundScope members = membersClosure(origin.type); |
mcimadamore@673 | 2054 | if (e == null || |
mcimadamore@877 | 2055 | !e.matches(implFilter, checkResult, members.getMark())) { |
mcimadamore@877 | 2056 | MethodSymbol impl = implementationInternal(ms, origin, checkResult, implFilter); |
mcimadamore@877 | 2057 | cache.put(origin, new Entry(impl, implFilter, checkResult, members.getMark())); |
mcimadamore@673 | 2058 | return impl; |
mcimadamore@673 | 2059 | } |
mcimadamore@673 | 2060 | else { |
mcimadamore@673 | 2061 | return e.cachedImpl; |
mcimadamore@673 | 2062 | } |
mcimadamore@673 | 2063 | } |
mcimadamore@673 | 2064 | |
mcimadamore@877 | 2065 | private MethodSymbol implementationInternal(MethodSymbol ms, TypeSymbol origin, boolean checkResult, Filter<Symbol> implFilter) { |
mcimadamore@877 | 2066 | for (Type t = origin.type; t.tag == CLASS || t.tag == TYPEVAR; t = supertype(t)) { |
mcimadamore@341 | 2067 | while (t.tag == TYPEVAR) |
mcimadamore@341 | 2068 | t = t.getUpperBound(); |
mcimadamore@341 | 2069 | TypeSymbol c = t.tsym; |
mcimadamore@673 | 2070 | for (Scope.Entry e = c.members().lookup(ms.name, implFilter); |
mcimadamore@341 | 2071 | e.scope != null; |
mcimadamore@780 | 2072 | e = e.next(implFilter)) { |
mcimadamore@673 | 2073 | if (e.sym != null && |
mcimadamore@877 | 2074 | e.sym.overrides(ms, origin, Types.this, checkResult)) |
mcimadamore@673 | 2075 | return (MethodSymbol)e.sym; |
mcimadamore@341 | 2076 | } |
mcimadamore@341 | 2077 | } |
mcimadamore@673 | 2078 | return null; |
mcimadamore@341 | 2079 | } |
mcimadamore@341 | 2080 | } |
mcimadamore@341 | 2081 | |
mcimadamore@673 | 2082 | private ImplementationCache implCache = new ImplementationCache(); |
mcimadamore@673 | 2083 | |
mcimadamore@858 | 2084 | public MethodSymbol implementation(MethodSymbol ms, TypeSymbol origin, boolean checkResult, Filter<Symbol> implFilter) { |
mcimadamore@858 | 2085 | return implCache.get(ms, origin, checkResult, implFilter); |
mcimadamore@673 | 2086 | } |
mcimadamore@673 | 2087 | // </editor-fold> |
mcimadamore@673 | 2088 | |
mcimadamore@858 | 2089 | // <editor-fold defaultstate="collapsed" desc="compute transitive closure of all members in given site"> |
mcimadamore@877 | 2090 | public CompoundScope membersClosure(Type site) { |
mcimadamore@858 | 2091 | return membersClosure.visit(site); |
mcimadamore@858 | 2092 | } |
mcimadamore@858 | 2093 | |
mcimadamore@877 | 2094 | UnaryVisitor<CompoundScope> membersClosure = new UnaryVisitor<CompoundScope>() { |
mcimadamore@877 | 2095 | |
mcimadamore@877 | 2096 | public CompoundScope visitType(Type t, Void s) { |
mcimadamore@858 | 2097 | return null; |
mcimadamore@858 | 2098 | } |
mcimadamore@858 | 2099 | |
mcimadamore@858 | 2100 | @Override |
mcimadamore@877 | 2101 | public CompoundScope visitClassType(ClassType t, Void s) { |
mcimadamore@858 | 2102 | ClassSymbol csym = (ClassSymbol)t.tsym; |
mcimadamore@858 | 2103 | if (csym.membersClosure == null) { |
mcimadamore@877 | 2104 | CompoundScope membersClosure = new CompoundScope(csym); |
mcimadamore@858 | 2105 | for (Type i : interfaces(t)) { |
mcimadamore@877 | 2106 | membersClosure.addSubScope(visit(i)); |
mcimadamore@858 | 2107 | } |
mcimadamore@877 | 2108 | membersClosure.addSubScope(visit(supertype(t))); |
mcimadamore@877 | 2109 | membersClosure.addSubScope(csym.members()); |
mcimadamore@858 | 2110 | csym.membersClosure = membersClosure; |
mcimadamore@858 | 2111 | } |
mcimadamore@858 | 2112 | return csym.membersClosure; |
mcimadamore@858 | 2113 | } |
mcimadamore@858 | 2114 | |
mcimadamore@858 | 2115 | @Override |
mcimadamore@877 | 2116 | public CompoundScope visitTypeVar(TypeVar t, Void s) { |
mcimadamore@858 | 2117 | return visit(t.getUpperBound()); |
mcimadamore@858 | 2118 | } |
mcimadamore@858 | 2119 | }; |
mcimadamore@858 | 2120 | // </editor-fold> |
mcimadamore@858 | 2121 | |
duke@1 | 2122 | /** |
duke@1 | 2123 | * Does t have the same arguments as s? It is assumed that both |
duke@1 | 2124 | * types are (possibly polymorphic) method types. Monomorphic |
duke@1 | 2125 | * method types "have the same arguments", if their argument lists |
duke@1 | 2126 | * are equal. Polymorphic method types "have the same arguments", |
duke@1 | 2127 | * if they have the same arguments after renaming all type |
duke@1 | 2128 | * variables of one to corresponding type variables in the other, |
duke@1 | 2129 | * where correspondence is by position in the type parameter list. |
duke@1 | 2130 | */ |
duke@1 | 2131 | public boolean hasSameArgs(Type t, Type s) { |
duke@1 | 2132 | return hasSameArgs.visit(t, s); |
duke@1 | 2133 | } |
duke@1 | 2134 | // where |
duke@1 | 2135 | private TypeRelation hasSameArgs = new TypeRelation() { |
duke@1 | 2136 | |
duke@1 | 2137 | public Boolean visitType(Type t, Type s) { |
duke@1 | 2138 | throw new AssertionError(); |
duke@1 | 2139 | } |
duke@1 | 2140 | |
duke@1 | 2141 | @Override |
duke@1 | 2142 | public Boolean visitMethodType(MethodType t, Type s) { |
duke@1 | 2143 | return s.tag == METHOD |
duke@1 | 2144 | && containsTypeEquivalent(t.argtypes, s.getParameterTypes()); |
duke@1 | 2145 | } |
duke@1 | 2146 | |
duke@1 | 2147 | @Override |
duke@1 | 2148 | public Boolean visitForAll(ForAll t, Type s) { |
duke@1 | 2149 | if (s.tag != FORALL) |
duke@1 | 2150 | return false; |
duke@1 | 2151 | |
duke@1 | 2152 | ForAll forAll = (ForAll)s; |
duke@1 | 2153 | return hasSameBounds(t, forAll) |
duke@1 | 2154 | && visit(t.qtype, subst(forAll.qtype, forAll.tvars, t.tvars)); |
duke@1 | 2155 | } |
duke@1 | 2156 | |
duke@1 | 2157 | @Override |
duke@1 | 2158 | public Boolean visitErrorType(ErrorType t, Type s) { |
duke@1 | 2159 | return false; |
duke@1 | 2160 | } |
duke@1 | 2161 | }; |
duke@1 | 2162 | // </editor-fold> |
duke@1 | 2163 | |
duke@1 | 2164 | // <editor-fold defaultstate="collapsed" desc="subst"> |
duke@1 | 2165 | public List<Type> subst(List<Type> ts, |
duke@1 | 2166 | List<Type> from, |
duke@1 | 2167 | List<Type> to) { |
duke@1 | 2168 | return new Subst(from, to).subst(ts); |
duke@1 | 2169 | } |
duke@1 | 2170 | |
duke@1 | 2171 | /** |
duke@1 | 2172 | * Substitute all occurrences of a type in `from' with the |
duke@1 | 2173 | * corresponding type in `to' in 't'. Match lists `from' and `to' |
duke@1 | 2174 | * from the right: If lists have different length, discard leading |
duke@1 | 2175 | * elements of the longer list. |
duke@1 | 2176 | */ |
duke@1 | 2177 | public Type subst(Type t, List<Type> from, List<Type> to) { |
duke@1 | 2178 | return new Subst(from, to).subst(t); |
duke@1 | 2179 | } |
duke@1 | 2180 | |
duke@1 | 2181 | private class Subst extends UnaryVisitor<Type> { |
duke@1 | 2182 | List<Type> from; |
duke@1 | 2183 | List<Type> to; |
duke@1 | 2184 | |
duke@1 | 2185 | public Subst(List<Type> from, List<Type> to) { |
duke@1 | 2186 | int fromLength = from.length(); |
duke@1 | 2187 | int toLength = to.length(); |
duke@1 | 2188 | while (fromLength > toLength) { |
duke@1 | 2189 | fromLength--; |
duke@1 | 2190 | from = from.tail; |
duke@1 | 2191 | } |
duke@1 | 2192 | while (fromLength < toLength) { |
duke@1 | 2193 | toLength--; |
duke@1 | 2194 | to = to.tail; |
duke@1 | 2195 | } |
duke@1 | 2196 | this.from = from; |
duke@1 | 2197 | this.to = to; |
duke@1 | 2198 | } |
duke@1 | 2199 | |
duke@1 | 2200 | Type subst(Type t) { |
duke@1 | 2201 | if (from.tail == null) |
duke@1 | 2202 | return t; |
duke@1 | 2203 | else |
duke@1 | 2204 | return visit(t); |
mcimadamore@238 | 2205 | } |
duke@1 | 2206 | |
duke@1 | 2207 | List<Type> subst(List<Type> ts) { |
duke@1 | 2208 | if (from.tail == null) |
duke@1 | 2209 | return ts; |
duke@1 | 2210 | boolean wild = false; |
duke@1 | 2211 | if (ts.nonEmpty() && from.nonEmpty()) { |
duke@1 | 2212 | Type head1 = subst(ts.head); |
duke@1 | 2213 | List<Type> tail1 = subst(ts.tail); |
duke@1 | 2214 | if (head1 != ts.head || tail1 != ts.tail) |
duke@1 | 2215 | return tail1.prepend(head1); |
duke@1 | 2216 | } |
duke@1 | 2217 | return ts; |
duke@1 | 2218 | } |
duke@1 | 2219 | |
duke@1 | 2220 | public Type visitType(Type t, Void ignored) { |
duke@1 | 2221 | return t; |
duke@1 | 2222 | } |
duke@1 | 2223 | |
duke@1 | 2224 | @Override |
duke@1 | 2225 | public Type visitMethodType(MethodType t, Void ignored) { |
duke@1 | 2226 | List<Type> argtypes = subst(t.argtypes); |
duke@1 | 2227 | Type restype = subst(t.restype); |
duke@1 | 2228 | List<Type> thrown = subst(t.thrown); |
duke@1 | 2229 | if (argtypes == t.argtypes && |
duke@1 | 2230 | restype == t.restype && |
duke@1 | 2231 | thrown == t.thrown) |
duke@1 | 2232 | return t; |
duke@1 | 2233 | else |
duke@1 | 2234 | return new MethodType(argtypes, restype, thrown, t.tsym); |
duke@1 | 2235 | } |
duke@1 | 2236 | |
duke@1 | 2237 | @Override |
duke@1 | 2238 | public Type visitTypeVar(TypeVar t, Void ignored) { |
duke@1 | 2239 | for (List<Type> from = this.from, to = this.to; |
duke@1 | 2240 | from.nonEmpty(); |
duke@1 | 2241 | from = from.tail, to = to.tail) { |
duke@1 | 2242 | if (t == from.head) { |
duke@1 | 2243 | return to.head.withTypeVar(t); |
duke@1 | 2244 | } |
duke@1 | 2245 | } |
duke@1 | 2246 | return t; |
duke@1 | 2247 | } |
duke@1 | 2248 | |
duke@1 | 2249 | @Override |
duke@1 | 2250 | public Type visitClassType(ClassType t, Void ignored) { |
duke@1 | 2251 | if (!t.isCompound()) { |
duke@1 | 2252 | List<Type> typarams = t.getTypeArguments(); |
duke@1 | 2253 | List<Type> typarams1 = subst(typarams); |
duke@1 | 2254 | Type outer = t.getEnclosingType(); |
duke@1 | 2255 | Type outer1 = subst(outer); |
duke@1 | 2256 | if (typarams1 == typarams && outer1 == outer) |
duke@1 | 2257 | return t; |
duke@1 | 2258 | else |
duke@1 | 2259 | return new ClassType(outer1, typarams1, t.tsym); |
duke@1 | 2260 | } else { |
duke@1 | 2261 | Type st = subst(supertype(t)); |
duke@1 | 2262 | List<Type> is = upperBounds(subst(interfaces(t))); |
duke@1 | 2263 | if (st == supertype(t) && is == interfaces(t)) |
duke@1 | 2264 | return t; |
duke@1 | 2265 | else |
duke@1 | 2266 | return makeCompoundType(is.prepend(st)); |
duke@1 | 2267 | } |
duke@1 | 2268 | } |
duke@1 | 2269 | |
duke@1 | 2270 | @Override |
duke@1 | 2271 | public Type visitWildcardType(WildcardType t, Void ignored) { |
duke@1 | 2272 | Type bound = t.type; |
duke@1 | 2273 | if (t.kind != BoundKind.UNBOUND) |
duke@1 | 2274 | bound = subst(bound); |
duke@1 | 2275 | if (bound == t.type) { |
duke@1 | 2276 | return t; |
duke@1 | 2277 | } else { |
duke@1 | 2278 | if (t.isExtendsBound() && bound.isExtendsBound()) |
duke@1 | 2279 | bound = upperBound(bound); |
duke@1 | 2280 | return new WildcardType(bound, t.kind, syms.boundClass, t.bound); |
duke@1 | 2281 | } |
duke@1 | 2282 | } |
duke@1 | 2283 | |
duke@1 | 2284 | @Override |
duke@1 | 2285 | public Type visitArrayType(ArrayType t, Void ignored) { |
duke@1 | 2286 | Type elemtype = subst(t.elemtype); |
duke@1 | 2287 | if (elemtype == t.elemtype) |
duke@1 | 2288 | return t; |
duke@1 | 2289 | else |
duke@1 | 2290 | return new ArrayType(upperBound(elemtype), t.tsym); |
duke@1 | 2291 | } |
duke@1 | 2292 | |
duke@1 | 2293 | @Override |
duke@1 | 2294 | public Type visitForAll(ForAll t, Void ignored) { |
mcimadamore@846 | 2295 | if (Type.containsAny(to, t.tvars)) { |
mcimadamore@846 | 2296 | //perform alpha-renaming of free-variables in 't' |
mcimadamore@846 | 2297 | //if 'to' types contain variables that are free in 't' |
mcimadamore@846 | 2298 | List<Type> freevars = newInstances(t.tvars); |
mcimadamore@846 | 2299 | t = new ForAll(freevars, |
mcimadamore@846 | 2300 | Types.this.subst(t.qtype, t.tvars, freevars)); |
mcimadamore@846 | 2301 | } |
duke@1 | 2302 | List<Type> tvars1 = substBounds(t.tvars, from, to); |
duke@1 | 2303 | Type qtype1 = subst(t.qtype); |
duke@1 | 2304 | if (tvars1 == t.tvars && qtype1 == t.qtype) { |
duke@1 | 2305 | return t; |
duke@1 | 2306 | } else if (tvars1 == t.tvars) { |
duke@1 | 2307 | return new ForAll(tvars1, qtype1); |
duke@1 | 2308 | } else { |
duke@1 | 2309 | return new ForAll(tvars1, Types.this.subst(qtype1, t.tvars, tvars1)); |
duke@1 | 2310 | } |
duke@1 | 2311 | } |
duke@1 | 2312 | |
duke@1 | 2313 | @Override |
duke@1 | 2314 | public Type visitErrorType(ErrorType t, Void ignored) { |
duke@1 | 2315 | return t; |
duke@1 | 2316 | } |
duke@1 | 2317 | } |
duke@1 | 2318 | |
duke@1 | 2319 | public List<Type> substBounds(List<Type> tvars, |
duke@1 | 2320 | List<Type> from, |
duke@1 | 2321 | List<Type> to) { |
duke@1 | 2322 | if (tvars.isEmpty()) |
duke@1 | 2323 | return tvars; |
duke@1 | 2324 | ListBuffer<Type> newBoundsBuf = lb(); |
duke@1 | 2325 | boolean changed = false; |
duke@1 | 2326 | // calculate new bounds |
duke@1 | 2327 | for (Type t : tvars) { |
duke@1 | 2328 | TypeVar tv = (TypeVar) t; |
duke@1 | 2329 | Type bound = subst(tv.bound, from, to); |
duke@1 | 2330 | if (bound != tv.bound) |
duke@1 | 2331 | changed = true; |
duke@1 | 2332 | newBoundsBuf.append(bound); |
duke@1 | 2333 | } |
duke@1 | 2334 | if (!changed) |
duke@1 | 2335 | return tvars; |
duke@1 | 2336 | ListBuffer<Type> newTvars = lb(); |
duke@1 | 2337 | // create new type variables without bounds |
duke@1 | 2338 | for (Type t : tvars) { |
duke@1 | 2339 | newTvars.append(new TypeVar(t.tsym, null, syms.botType)); |
duke@1 | 2340 | } |
duke@1 | 2341 | // the new bounds should use the new type variables in place |
duke@1 | 2342 | // of the old |
duke@1 | 2343 | List<Type> newBounds = newBoundsBuf.toList(); |
duke@1 | 2344 | from = tvars; |
duke@1 | 2345 | to = newTvars.toList(); |
duke@1 | 2346 | for (; !newBounds.isEmpty(); newBounds = newBounds.tail) { |
duke@1 | 2347 | newBounds.head = subst(newBounds.head, from, to); |
duke@1 | 2348 | } |
duke@1 | 2349 | newBounds = newBoundsBuf.toList(); |
duke@1 | 2350 | // set the bounds of new type variables to the new bounds |
duke@1 | 2351 | for (Type t : newTvars.toList()) { |
duke@1 | 2352 | TypeVar tv = (TypeVar) t; |
duke@1 | 2353 | tv.bound = newBounds.head; |
duke@1 | 2354 | newBounds = newBounds.tail; |
duke@1 | 2355 | } |
duke@1 | 2356 | return newTvars.toList(); |
duke@1 | 2357 | } |
duke@1 | 2358 | |
duke@1 | 2359 | public TypeVar substBound(TypeVar t, List<Type> from, List<Type> to) { |
duke@1 | 2360 | Type bound1 = subst(t.bound, from, to); |
duke@1 | 2361 | if (bound1 == t.bound) |
duke@1 | 2362 | return t; |
mcimadamore@212 | 2363 | else { |
mcimadamore@212 | 2364 | // create new type variable without bounds |
mcimadamore@212 | 2365 | TypeVar tv = new TypeVar(t.tsym, null, syms.botType); |
mcimadamore@212 | 2366 | // the new bound should use the new type variable in place |
mcimadamore@212 | 2367 | // of the old |
mcimadamore@212 | 2368 | tv.bound = subst(bound1, List.<Type>of(t), List.<Type>of(tv)); |
mcimadamore@212 | 2369 | return tv; |
mcimadamore@212 | 2370 | } |
duke@1 | 2371 | } |
duke@1 | 2372 | // </editor-fold> |
duke@1 | 2373 | |
duke@1 | 2374 | // <editor-fold defaultstate="collapsed" desc="hasSameBounds"> |
duke@1 | 2375 | /** |
duke@1 | 2376 | * Does t have the same bounds for quantified variables as s? |
duke@1 | 2377 | */ |
duke@1 | 2378 | boolean hasSameBounds(ForAll t, ForAll s) { |
duke@1 | 2379 | List<Type> l1 = t.tvars; |
duke@1 | 2380 | List<Type> l2 = s.tvars; |
duke@1 | 2381 | while (l1.nonEmpty() && l2.nonEmpty() && |
duke@1 | 2382 | isSameType(l1.head.getUpperBound(), |
duke@1 | 2383 | subst(l2.head.getUpperBound(), |
duke@1 | 2384 | s.tvars, |
duke@1 | 2385 | t.tvars))) { |
duke@1 | 2386 | l1 = l1.tail; |
duke@1 | 2387 | l2 = l2.tail; |
duke@1 | 2388 | } |
duke@1 | 2389 | return l1.isEmpty() && l2.isEmpty(); |
duke@1 | 2390 | } |
duke@1 | 2391 | // </editor-fold> |
duke@1 | 2392 | |
duke@1 | 2393 | // <editor-fold defaultstate="collapsed" desc="newInstances"> |
duke@1 | 2394 | /** Create new vector of type variables from list of variables |
duke@1 | 2395 | * changing all recursive bounds from old to new list. |
duke@1 | 2396 | */ |
duke@1 | 2397 | public List<Type> newInstances(List<Type> tvars) { |
duke@1 | 2398 | List<Type> tvars1 = Type.map(tvars, newInstanceFun); |
duke@1 | 2399 | for (List<Type> l = tvars1; l.nonEmpty(); l = l.tail) { |
duke@1 | 2400 | TypeVar tv = (TypeVar) l.head; |
duke@1 | 2401 | tv.bound = subst(tv.bound, tvars, tvars1); |
duke@1 | 2402 | } |
duke@1 | 2403 | return tvars1; |
duke@1 | 2404 | } |
duke@1 | 2405 | static private Mapping newInstanceFun = new Mapping("newInstanceFun") { |
duke@1 | 2406 | public Type apply(Type t) { return new TypeVar(t.tsym, t.getUpperBound(), t.getLowerBound()); } |
duke@1 | 2407 | }; |
duke@1 | 2408 | // </editor-fold> |
duke@1 | 2409 | |
dlsmith@880 | 2410 | public Type createMethodTypeWithParameters(Type original, List<Type> newParams) { |
dlsmith@880 | 2411 | return original.accept(methodWithParameters, newParams); |
dlsmith@880 | 2412 | } |
dlsmith@880 | 2413 | // where |
dlsmith@880 | 2414 | private final MapVisitor<List<Type>> methodWithParameters = new MapVisitor<List<Type>>() { |
dlsmith@880 | 2415 | public Type visitType(Type t, List<Type> newParams) { |
dlsmith@880 | 2416 | throw new IllegalArgumentException("Not a method type: " + t); |
dlsmith@880 | 2417 | } |
dlsmith@880 | 2418 | public Type visitMethodType(MethodType t, List<Type> newParams) { |
dlsmith@880 | 2419 | return new MethodType(newParams, t.restype, t.thrown, t.tsym); |
dlsmith@880 | 2420 | } |
dlsmith@880 | 2421 | public Type visitForAll(ForAll t, List<Type> newParams) { |
dlsmith@880 | 2422 | return new ForAll(t.tvars, t.qtype.accept(this, newParams)); |
dlsmith@880 | 2423 | } |
dlsmith@880 | 2424 | }; |
dlsmith@880 | 2425 | |
dlsmith@880 | 2426 | public Type createMethodTypeWithThrown(Type original, List<Type> newThrown) { |
dlsmith@880 | 2427 | return original.accept(methodWithThrown, newThrown); |
dlsmith@880 | 2428 | } |
dlsmith@880 | 2429 | // where |
dlsmith@880 | 2430 | private final MapVisitor<List<Type>> methodWithThrown = new MapVisitor<List<Type>>() { |
dlsmith@880 | 2431 | public Type visitType(Type t, List<Type> newThrown) { |
dlsmith@880 | 2432 | throw new IllegalArgumentException("Not a method type: " + t); |
dlsmith@880 | 2433 | } |
dlsmith@880 | 2434 | public Type visitMethodType(MethodType t, List<Type> newThrown) { |
dlsmith@880 | 2435 | return new MethodType(t.argtypes, t.restype, newThrown, t.tsym); |
dlsmith@880 | 2436 | } |
dlsmith@880 | 2437 | public Type visitForAll(ForAll t, List<Type> newThrown) { |
dlsmith@880 | 2438 | return new ForAll(t.tvars, t.qtype.accept(this, newThrown)); |
dlsmith@880 | 2439 | } |
dlsmith@880 | 2440 | }; |
dlsmith@880 | 2441 | |
jjg@110 | 2442 | // <editor-fold defaultstate="collapsed" desc="createErrorType"> |
jjg@110 | 2443 | public Type createErrorType(Type originalType) { |
jjg@110 | 2444 | return new ErrorType(originalType, syms.errSymbol); |
jjg@110 | 2445 | } |
jjg@110 | 2446 | |
jjg@110 | 2447 | public Type createErrorType(ClassSymbol c, Type originalType) { |
jjg@110 | 2448 | return new ErrorType(c, originalType); |
jjg@110 | 2449 | } |
jjg@110 | 2450 | |
jjg@110 | 2451 | public Type createErrorType(Name name, TypeSymbol container, Type originalType) { |
jjg@110 | 2452 | return new ErrorType(name, container, originalType); |
jjg@110 | 2453 | } |
jjg@110 | 2454 | // </editor-fold> |
jjg@110 | 2455 | |
duke@1 | 2456 | // <editor-fold defaultstate="collapsed" desc="rank"> |
duke@1 | 2457 | /** |
duke@1 | 2458 | * The rank of a class is the length of the longest path between |
duke@1 | 2459 | * the class and java.lang.Object in the class inheritance |
duke@1 | 2460 | * graph. Undefined for all but reference types. |
duke@1 | 2461 | */ |
duke@1 | 2462 | public int rank(Type t) { |
duke@1 | 2463 | switch(t.tag) { |
duke@1 | 2464 | case CLASS: { |
duke@1 | 2465 | ClassType cls = (ClassType)t; |
duke@1 | 2466 | if (cls.rank_field < 0) { |
duke@1 | 2467 | Name fullname = cls.tsym.getQualifiedName(); |
jjg@113 | 2468 | if (fullname == names.java_lang_Object) |
duke@1 | 2469 | cls.rank_field = 0; |
duke@1 | 2470 | else { |
duke@1 | 2471 | int r = rank(supertype(cls)); |
duke@1 | 2472 | for (List<Type> l = interfaces(cls); |
duke@1 | 2473 | l.nonEmpty(); |
duke@1 | 2474 | l = l.tail) { |
duke@1 | 2475 | if (rank(l.head) > r) |
duke@1 | 2476 | r = rank(l.head); |
duke@1 | 2477 | } |
duke@1 | 2478 | cls.rank_field = r + 1; |
duke@1 | 2479 | } |
duke@1 | 2480 | } |
duke@1 | 2481 | return cls.rank_field; |
duke@1 | 2482 | } |
duke@1 | 2483 | case TYPEVAR: { |
duke@1 | 2484 | TypeVar tvar = (TypeVar)t; |
duke@1 | 2485 | if (tvar.rank_field < 0) { |
duke@1 | 2486 | int r = rank(supertype(tvar)); |
duke@1 | 2487 | for (List<Type> l = interfaces(tvar); |
duke@1 | 2488 | l.nonEmpty(); |
duke@1 | 2489 | l = l.tail) { |
duke@1 | 2490 | if (rank(l.head) > r) r = rank(l.head); |
duke@1 | 2491 | } |
duke@1 | 2492 | tvar.rank_field = r + 1; |
duke@1 | 2493 | } |
duke@1 | 2494 | return tvar.rank_field; |
duke@1 | 2495 | } |
duke@1 | 2496 | case ERROR: |
duke@1 | 2497 | return 0; |
duke@1 | 2498 | default: |
duke@1 | 2499 | throw new AssertionError(); |
duke@1 | 2500 | } |
duke@1 | 2501 | } |
duke@1 | 2502 | // </editor-fold> |
duke@1 | 2503 | |
mcimadamore@121 | 2504 | /** |
mcimadamore@238 | 2505 | * Helper method for generating a string representation of a given type |
mcimadamore@121 | 2506 | * accordingly to a given locale |
mcimadamore@121 | 2507 | */ |
mcimadamore@121 | 2508 | public String toString(Type t, Locale locale) { |
mcimadamore@238 | 2509 | return Printer.createStandardPrinter(messages).visit(t, locale); |
mcimadamore@121 | 2510 | } |
mcimadamore@121 | 2511 | |
mcimadamore@121 | 2512 | /** |
mcimadamore@238 | 2513 | * Helper method for generating a string representation of a given type |
mcimadamore@121 | 2514 | * accordingly to a given locale |
mcimadamore@121 | 2515 | */ |
mcimadamore@121 | 2516 | public String toString(Symbol t, Locale locale) { |
mcimadamore@238 | 2517 | return Printer.createStandardPrinter(messages).visit(t, locale); |
mcimadamore@121 | 2518 | } |
mcimadamore@121 | 2519 | |
duke@1 | 2520 | // <editor-fold defaultstate="collapsed" desc="toString"> |
duke@1 | 2521 | /** |
duke@1 | 2522 | * This toString is slightly more descriptive than the one on Type. |
mcimadamore@121 | 2523 | * |
mcimadamore@121 | 2524 | * @deprecated Types.toString(Type t, Locale l) provides better support |
mcimadamore@121 | 2525 | * for localization |
duke@1 | 2526 | */ |
mcimadamore@121 | 2527 | @Deprecated |
duke@1 | 2528 | public String toString(Type t) { |
duke@1 | 2529 | if (t.tag == FORALL) { |
duke@1 | 2530 | ForAll forAll = (ForAll)t; |
duke@1 | 2531 | return typaramsString(forAll.tvars) + forAll.qtype; |
duke@1 | 2532 | } |
duke@1 | 2533 | return "" + t; |
duke@1 | 2534 | } |
duke@1 | 2535 | // where |
duke@1 | 2536 | private String typaramsString(List<Type> tvars) { |
duke@1 | 2537 | StringBuffer s = new StringBuffer(); |
duke@1 | 2538 | s.append('<'); |
duke@1 | 2539 | boolean first = true; |
duke@1 | 2540 | for (Type t : tvars) { |
duke@1 | 2541 | if (!first) s.append(", "); |
duke@1 | 2542 | first = false; |
duke@1 | 2543 | appendTyparamString(((TypeVar)t), s); |
duke@1 | 2544 | } |
duke@1 | 2545 | s.append('>'); |
duke@1 | 2546 | return s.toString(); |
duke@1 | 2547 | } |
duke@1 | 2548 | private void appendTyparamString(TypeVar t, StringBuffer buf) { |
duke@1 | 2549 | buf.append(t); |
duke@1 | 2550 | if (t.bound == null || |
duke@1 | 2551 | t.bound.tsym.getQualifiedName() == names.java_lang_Object) |
duke@1 | 2552 | return; |
duke@1 | 2553 | buf.append(" extends "); // Java syntax; no need for i18n |
duke@1 | 2554 | Type bound = t.bound; |
duke@1 | 2555 | if (!bound.isCompound()) { |
duke@1 | 2556 | buf.append(bound); |
duke@1 | 2557 | } else if ((erasure(t).tsym.flags() & INTERFACE) == 0) { |
duke@1 | 2558 | buf.append(supertype(t)); |
duke@1 | 2559 | for (Type intf : interfaces(t)) { |
duke@1 | 2560 | buf.append('&'); |
duke@1 | 2561 | buf.append(intf); |
duke@1 | 2562 | } |
duke@1 | 2563 | } else { |
duke@1 | 2564 | // No superclass was given in bounds. |
duke@1 | 2565 | // In this case, supertype is Object, erasure is first interface. |
duke@1 | 2566 | boolean first = true; |
duke@1 | 2567 | for (Type intf : interfaces(t)) { |
duke@1 | 2568 | if (!first) buf.append('&'); |
duke@1 | 2569 | first = false; |
duke@1 | 2570 | buf.append(intf); |
duke@1 | 2571 | } |
duke@1 | 2572 | } |
duke@1 | 2573 | } |
duke@1 | 2574 | // </editor-fold> |
duke@1 | 2575 | |
duke@1 | 2576 | // <editor-fold defaultstate="collapsed" desc="Determining least upper bounds of types"> |
duke@1 | 2577 | /** |
duke@1 | 2578 | * A cache for closures. |
duke@1 | 2579 | * |
duke@1 | 2580 | * <p>A closure is a list of all the supertypes and interfaces of |
duke@1 | 2581 | * a class or interface type, ordered by ClassSymbol.precedes |
duke@1 | 2582 | * (that is, subclasses come first, arbitrary but fixed |
duke@1 | 2583 | * otherwise). |
duke@1 | 2584 | */ |
duke@1 | 2585 | private Map<Type,List<Type>> closureCache = new HashMap<Type,List<Type>>(); |
duke@1 | 2586 | |
duke@1 | 2587 | /** |
duke@1 | 2588 | * Returns the closure of a class or interface type. |
duke@1 | 2589 | */ |
duke@1 | 2590 | public List<Type> closure(Type t) { |
duke@1 | 2591 | List<Type> cl = closureCache.get(t); |
duke@1 | 2592 | if (cl == null) { |
duke@1 | 2593 | Type st = supertype(t); |
duke@1 | 2594 | if (!t.isCompound()) { |
duke@1 | 2595 | if (st.tag == CLASS) { |
duke@1 | 2596 | cl = insert(closure(st), t); |
duke@1 | 2597 | } else if (st.tag == TYPEVAR) { |
duke@1 | 2598 | cl = closure(st).prepend(t); |
duke@1 | 2599 | } else { |
duke@1 | 2600 | cl = List.of(t); |
duke@1 | 2601 | } |
duke@1 | 2602 | } else { |
duke@1 | 2603 | cl = closure(supertype(t)); |
duke@1 | 2604 | } |
duke@1 | 2605 | for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail) |
duke@1 | 2606 | cl = union(cl, closure(l.head)); |
duke@1 | 2607 | closureCache.put(t, cl); |
duke@1 | 2608 | } |
duke@1 | 2609 | return cl; |
duke@1 | 2610 | } |
duke@1 | 2611 | |
duke@1 | 2612 | /** |
duke@1 | 2613 | * Insert a type in a closure |
duke@1 | 2614 | */ |
duke@1 | 2615 | public List<Type> insert(List<Type> cl, Type t) { |
duke@1 | 2616 | if (cl.isEmpty() || t.tsym.precedes(cl.head.tsym, this)) { |
duke@1 | 2617 | return cl.prepend(t); |
duke@1 | 2618 | } else if (cl.head.tsym.precedes(t.tsym, this)) { |
duke@1 | 2619 | return insert(cl.tail, t).prepend(cl.head); |
duke@1 | 2620 | } else { |
duke@1 | 2621 | return cl; |
duke@1 | 2622 | } |
duke@1 | 2623 | } |
duke@1 | 2624 | |
duke@1 | 2625 | /** |
duke@1 | 2626 | * Form the union of two closures |
duke@1 | 2627 | */ |
duke@1 | 2628 | public List<Type> union(List<Type> cl1, List<Type> cl2) { |
duke@1 | 2629 | if (cl1.isEmpty()) { |
duke@1 | 2630 | return cl2; |
duke@1 | 2631 | } else if (cl2.isEmpty()) { |
duke@1 | 2632 | return cl1; |
duke@1 | 2633 | } else if (cl1.head.tsym.precedes(cl2.head.tsym, this)) { |
duke@1 | 2634 | return union(cl1.tail, cl2).prepend(cl1.head); |
duke@1 | 2635 | } else if (cl2.head.tsym.precedes(cl1.head.tsym, this)) { |
duke@1 | 2636 | return union(cl1, cl2.tail).prepend(cl2.head); |
duke@1 | 2637 | } else { |
duke@1 | 2638 | return union(cl1.tail, cl2.tail).prepend(cl1.head); |
duke@1 | 2639 | } |
duke@1 | 2640 | } |
duke@1 | 2641 | |
duke@1 | 2642 | /** |
duke@1 | 2643 | * Intersect two closures |
duke@1 | 2644 | */ |
duke@1 | 2645 | public List<Type> intersect(List<Type> cl1, List<Type> cl2) { |
duke@1 | 2646 | if (cl1 == cl2) |
duke@1 | 2647 | return cl1; |
duke@1 | 2648 | if (cl1.isEmpty() || cl2.isEmpty()) |
duke@1 | 2649 | return List.nil(); |
duke@1 | 2650 | if (cl1.head.tsym.precedes(cl2.head.tsym, this)) |
duke@1 | 2651 | return intersect(cl1.tail, cl2); |
duke@1 | 2652 | if (cl2.head.tsym.precedes(cl1.head.tsym, this)) |
duke@1 | 2653 | return intersect(cl1, cl2.tail); |
duke@1 | 2654 | if (isSameType(cl1.head, cl2.head)) |
duke@1 | 2655 | return intersect(cl1.tail, cl2.tail).prepend(cl1.head); |
duke@1 | 2656 | if (cl1.head.tsym == cl2.head.tsym && |
duke@1 | 2657 | cl1.head.tag == CLASS && cl2.head.tag == CLASS) { |
duke@1 | 2658 | if (cl1.head.isParameterized() && cl2.head.isParameterized()) { |
duke@1 | 2659 | Type merge = merge(cl1.head,cl2.head); |
duke@1 | 2660 | return intersect(cl1.tail, cl2.tail).prepend(merge); |
duke@1 | 2661 | } |
duke@1 | 2662 | if (cl1.head.isRaw() || cl2.head.isRaw()) |
duke@1 | 2663 | return intersect(cl1.tail, cl2.tail).prepend(erasure(cl1.head)); |
duke@1 | 2664 | } |
duke@1 | 2665 | return intersect(cl1.tail, cl2.tail); |
duke@1 | 2666 | } |
duke@1 | 2667 | // where |
duke@1 | 2668 | class TypePair { |
duke@1 | 2669 | final Type t1; |
duke@1 | 2670 | final Type t2; |
duke@1 | 2671 | TypePair(Type t1, Type t2) { |
duke@1 | 2672 | this.t1 = t1; |
duke@1 | 2673 | this.t2 = t2; |
duke@1 | 2674 | } |
duke@1 | 2675 | @Override |
duke@1 | 2676 | public int hashCode() { |
jjg@507 | 2677 | return 127 * Types.hashCode(t1) + Types.hashCode(t2); |
duke@1 | 2678 | } |
duke@1 | 2679 | @Override |
duke@1 | 2680 | public boolean equals(Object obj) { |
duke@1 | 2681 | if (!(obj instanceof TypePair)) |
duke@1 | 2682 | return false; |
duke@1 | 2683 | TypePair typePair = (TypePair)obj; |
duke@1 | 2684 | return isSameType(t1, typePair.t1) |
duke@1 | 2685 | && isSameType(t2, typePair.t2); |
duke@1 | 2686 | } |
duke@1 | 2687 | } |
duke@1 | 2688 | Set<TypePair> mergeCache = new HashSet<TypePair>(); |
duke@1 | 2689 | private Type merge(Type c1, Type c2) { |
duke@1 | 2690 | ClassType class1 = (ClassType) c1; |
duke@1 | 2691 | List<Type> act1 = class1.getTypeArguments(); |
duke@1 | 2692 | ClassType class2 = (ClassType) c2; |
duke@1 | 2693 | List<Type> act2 = class2.getTypeArguments(); |
duke@1 | 2694 | ListBuffer<Type> merged = new ListBuffer<Type>(); |
duke@1 | 2695 | List<Type> typarams = class1.tsym.type.getTypeArguments(); |
duke@1 | 2696 | |
duke@1 | 2697 | while (act1.nonEmpty() && act2.nonEmpty() && typarams.nonEmpty()) { |
duke@1 | 2698 | if (containsType(act1.head, act2.head)) { |
duke@1 | 2699 | merged.append(act1.head); |
duke@1 | 2700 | } else if (containsType(act2.head, act1.head)) { |
duke@1 | 2701 | merged.append(act2.head); |
duke@1 | 2702 | } else { |
duke@1 | 2703 | TypePair pair = new TypePair(c1, c2); |
duke@1 | 2704 | Type m; |
duke@1 | 2705 | if (mergeCache.add(pair)) { |
duke@1 | 2706 | m = new WildcardType(lub(upperBound(act1.head), |
duke@1 | 2707 | upperBound(act2.head)), |
duke@1 | 2708 | BoundKind.EXTENDS, |
duke@1 | 2709 | syms.boundClass); |
duke@1 | 2710 | mergeCache.remove(pair); |
duke@1 | 2711 | } else { |
duke@1 | 2712 | m = new WildcardType(syms.objectType, |
duke@1 | 2713 | BoundKind.UNBOUND, |
duke@1 | 2714 | syms.boundClass); |
duke@1 | 2715 | } |
duke@1 | 2716 | merged.append(m.withTypeVar(typarams.head)); |
duke@1 | 2717 | } |
duke@1 | 2718 | act1 = act1.tail; |
duke@1 | 2719 | act2 = act2.tail; |
duke@1 | 2720 | typarams = typarams.tail; |
duke@1 | 2721 | } |
jjg@816 | 2722 | Assert.check(act1.isEmpty() && act2.isEmpty() && typarams.isEmpty()); |
duke@1 | 2723 | return new ClassType(class1.getEnclosingType(), merged.toList(), class1.tsym); |
duke@1 | 2724 | } |
duke@1 | 2725 | |
duke@1 | 2726 | /** |
duke@1 | 2727 | * Return the minimum type of a closure, a compound type if no |
duke@1 | 2728 | * unique minimum exists. |
duke@1 | 2729 | */ |
duke@1 | 2730 | private Type compoundMin(List<Type> cl) { |
duke@1 | 2731 | if (cl.isEmpty()) return syms.objectType; |
duke@1 | 2732 | List<Type> compound = closureMin(cl); |
duke@1 | 2733 | if (compound.isEmpty()) |
duke@1 | 2734 | return null; |
duke@1 | 2735 | else if (compound.tail.isEmpty()) |
duke@1 | 2736 | return compound.head; |
duke@1 | 2737 | else |
duke@1 | 2738 | return makeCompoundType(compound); |
duke@1 | 2739 | } |
duke@1 | 2740 | |
duke@1 | 2741 | /** |
duke@1 | 2742 | * Return the minimum types of a closure, suitable for computing |
duke@1 | 2743 | * compoundMin or glb. |
duke@1 | 2744 | */ |
duke@1 | 2745 | private List<Type> closureMin(List<Type> cl) { |
duke@1 | 2746 | ListBuffer<Type> classes = lb(); |
duke@1 | 2747 | ListBuffer<Type> interfaces = lb(); |
duke@1 | 2748 | while (!cl.isEmpty()) { |
duke@1 | 2749 | Type current = cl.head; |
duke@1 | 2750 | if (current.isInterface()) |
duke@1 | 2751 | interfaces.append(current); |
duke@1 | 2752 | else |
duke@1 | 2753 | classes.append(current); |
duke@1 | 2754 | ListBuffer<Type> candidates = lb(); |
duke@1 | 2755 | for (Type t : cl.tail) { |
duke@1 | 2756 | if (!isSubtypeNoCapture(current, t)) |
duke@1 | 2757 | candidates.append(t); |
duke@1 | 2758 | } |
duke@1 | 2759 | cl = candidates.toList(); |
duke@1 | 2760 | } |
duke@1 | 2761 | return classes.appendList(interfaces).toList(); |
duke@1 | 2762 | } |
duke@1 | 2763 | |
duke@1 | 2764 | /** |
duke@1 | 2765 | * Return the least upper bound of pair of types. if the lub does |
duke@1 | 2766 | * not exist return null. |
duke@1 | 2767 | */ |
duke@1 | 2768 | public Type lub(Type t1, Type t2) { |
duke@1 | 2769 | return lub(List.of(t1, t2)); |
duke@1 | 2770 | } |
duke@1 | 2771 | |
duke@1 | 2772 | /** |
duke@1 | 2773 | * Return the least upper bound (lub) of set of types. If the lub |
duke@1 | 2774 | * does not exist return the type of null (bottom). |
duke@1 | 2775 | */ |
duke@1 | 2776 | public Type lub(List<Type> ts) { |
duke@1 | 2777 | final int ARRAY_BOUND = 1; |
duke@1 | 2778 | final int CLASS_BOUND = 2; |
duke@1 | 2779 | int boundkind = 0; |
duke@1 | 2780 | for (Type t : ts) { |
duke@1 | 2781 | switch (t.tag) { |
duke@1 | 2782 | case CLASS: |
duke@1 | 2783 | boundkind |= CLASS_BOUND; |
duke@1 | 2784 | break; |
duke@1 | 2785 | case ARRAY: |
duke@1 | 2786 | boundkind |= ARRAY_BOUND; |
duke@1 | 2787 | break; |
duke@1 | 2788 | case TYPEVAR: |
duke@1 | 2789 | do { |
duke@1 | 2790 | t = t.getUpperBound(); |
duke@1 | 2791 | } while (t.tag == TYPEVAR); |
duke@1 | 2792 | if (t.tag == ARRAY) { |
duke@1 | 2793 | boundkind |= ARRAY_BOUND; |
duke@1 | 2794 | } else { |
duke@1 | 2795 | boundkind |= CLASS_BOUND; |
duke@1 | 2796 | } |
duke@1 | 2797 | break; |
duke@1 | 2798 | default: |
duke@1 | 2799 | if (t.isPrimitive()) |
mcimadamore@5 | 2800 | return syms.errType; |
duke@1 | 2801 | } |
duke@1 | 2802 | } |
duke@1 | 2803 | switch (boundkind) { |
duke@1 | 2804 | case 0: |
duke@1 | 2805 | return syms.botType; |
duke@1 | 2806 | |
duke@1 | 2807 | case ARRAY_BOUND: |
duke@1 | 2808 | // calculate lub(A[], B[]) |
duke@1 | 2809 | List<Type> elements = Type.map(ts, elemTypeFun); |
duke@1 | 2810 | for (Type t : elements) { |
duke@1 | 2811 | if (t.isPrimitive()) { |
duke@1 | 2812 | // if a primitive type is found, then return |
duke@1 | 2813 | // arraySuperType unless all the types are the |
duke@1 | 2814 | // same |
duke@1 | 2815 | Type first = ts.head; |
duke@1 | 2816 | for (Type s : ts.tail) { |
duke@1 | 2817 | if (!isSameType(first, s)) { |
duke@1 | 2818 | // lub(int[], B[]) is Cloneable & Serializable |
duke@1 | 2819 | return arraySuperType(); |
duke@1 | 2820 | } |
duke@1 | 2821 | } |
duke@1 | 2822 | // all the array types are the same, return one |
duke@1 | 2823 | // lub(int[], int[]) is int[] |
duke@1 | 2824 | return first; |
duke@1 | 2825 | } |
duke@1 | 2826 | } |
duke@1 | 2827 | // lub(A[], B[]) is lub(A, B)[] |
duke@1 | 2828 | return new ArrayType(lub(elements), syms.arrayClass); |
duke@1 | 2829 | |
duke@1 | 2830 | case CLASS_BOUND: |
duke@1 | 2831 | // calculate lub(A, B) |
duke@1 | 2832 | while (ts.head.tag != CLASS && ts.head.tag != TYPEVAR) |
duke@1 | 2833 | ts = ts.tail; |
jjg@816 | 2834 | Assert.check(!ts.isEmpty()); |
duke@1 | 2835 | List<Type> cl = closure(ts.head); |
duke@1 | 2836 | for (Type t : ts.tail) { |
duke@1 | 2837 | if (t.tag == CLASS || t.tag == TYPEVAR) |
duke@1 | 2838 | cl = intersect(cl, closure(t)); |
duke@1 | 2839 | } |
duke@1 | 2840 | return compoundMin(cl); |
duke@1 | 2841 | |
duke@1 | 2842 | default: |
duke@1 | 2843 | // calculate lub(A, B[]) |
duke@1 | 2844 | List<Type> classes = List.of(arraySuperType()); |
duke@1 | 2845 | for (Type t : ts) { |
duke@1 | 2846 | if (t.tag != ARRAY) // Filter out any arrays |
duke@1 | 2847 | classes = classes.prepend(t); |
duke@1 | 2848 | } |
duke@1 | 2849 | // lub(A, B[]) is lub(A, arraySuperType) |
duke@1 | 2850 | return lub(classes); |
duke@1 | 2851 | } |
duke@1 | 2852 | } |
duke@1 | 2853 | // where |
duke@1 | 2854 | private Type arraySuperType = null; |
duke@1 | 2855 | private Type arraySuperType() { |
duke@1 | 2856 | // initialized lazily to avoid problems during compiler startup |
duke@1 | 2857 | if (arraySuperType == null) { |
duke@1 | 2858 | synchronized (this) { |
duke@1 | 2859 | if (arraySuperType == null) { |
duke@1 | 2860 | // JLS 10.8: all arrays implement Cloneable and Serializable. |
duke@1 | 2861 | arraySuperType = makeCompoundType(List.of(syms.serializableType, |
duke@1 | 2862 | syms.cloneableType), |
duke@1 | 2863 | syms.objectType); |
duke@1 | 2864 | } |
duke@1 | 2865 | } |
duke@1 | 2866 | } |
duke@1 | 2867 | return arraySuperType; |
duke@1 | 2868 | } |
duke@1 | 2869 | // </editor-fold> |
duke@1 | 2870 | |
duke@1 | 2871 | // <editor-fold defaultstate="collapsed" desc="Greatest lower bound"> |
mcimadamore@210 | 2872 | public Type glb(List<Type> ts) { |
mcimadamore@210 | 2873 | Type t1 = ts.head; |
mcimadamore@210 | 2874 | for (Type t2 : ts.tail) { |
mcimadamore@210 | 2875 | if (t1.isErroneous()) |
mcimadamore@210 | 2876 | return t1; |
mcimadamore@210 | 2877 | t1 = glb(t1, t2); |
mcimadamore@210 | 2878 | } |
mcimadamore@210 | 2879 | return t1; |
mcimadamore@210 | 2880 | } |
mcimadamore@210 | 2881 | //where |
duke@1 | 2882 | public Type glb(Type t, Type s) { |
duke@1 | 2883 | if (s == null) |
duke@1 | 2884 | return t; |
mcimadamore@753 | 2885 | else if (t.isPrimitive() || s.isPrimitive()) |
mcimadamore@753 | 2886 | return syms.errType; |
duke@1 | 2887 | else if (isSubtypeNoCapture(t, s)) |
duke@1 | 2888 | return t; |
duke@1 | 2889 | else if (isSubtypeNoCapture(s, t)) |
duke@1 | 2890 | return s; |
duke@1 | 2891 | |
duke@1 | 2892 | List<Type> closure = union(closure(t), closure(s)); |
duke@1 | 2893 | List<Type> bounds = closureMin(closure); |
duke@1 | 2894 | |
duke@1 | 2895 | if (bounds.isEmpty()) { // length == 0 |
duke@1 | 2896 | return syms.objectType; |
duke@1 | 2897 | } else if (bounds.tail.isEmpty()) { // length == 1 |
duke@1 | 2898 | return bounds.head; |
duke@1 | 2899 | } else { // length > 1 |
duke@1 | 2900 | int classCount = 0; |
duke@1 | 2901 | for (Type bound : bounds) |
duke@1 | 2902 | if (!bound.isInterface()) |
duke@1 | 2903 | classCount++; |
duke@1 | 2904 | if (classCount > 1) |
jjg@110 | 2905 | return createErrorType(t); |
duke@1 | 2906 | } |
duke@1 | 2907 | return makeCompoundType(bounds); |
duke@1 | 2908 | } |
duke@1 | 2909 | // </editor-fold> |
duke@1 | 2910 | |
duke@1 | 2911 | // <editor-fold defaultstate="collapsed" desc="hashCode"> |
duke@1 | 2912 | /** |
duke@1 | 2913 | * Compute a hash code on a type. |
duke@1 | 2914 | */ |
duke@1 | 2915 | public static int hashCode(Type t) { |
duke@1 | 2916 | return hashCode.visit(t); |
duke@1 | 2917 | } |
duke@1 | 2918 | // where |
duke@1 | 2919 | private static final UnaryVisitor<Integer> hashCode = new UnaryVisitor<Integer>() { |
duke@1 | 2920 | |
duke@1 | 2921 | public Integer visitType(Type t, Void ignored) { |
duke@1 | 2922 | return t.tag; |
duke@1 | 2923 | } |
duke@1 | 2924 | |
duke@1 | 2925 | @Override |
duke@1 | 2926 | public Integer visitClassType(ClassType t, Void ignored) { |
duke@1 | 2927 | int result = visit(t.getEnclosingType()); |
duke@1 | 2928 | result *= 127; |
duke@1 | 2929 | result += t.tsym.flatName().hashCode(); |
duke@1 | 2930 | for (Type s : t.getTypeArguments()) { |
duke@1 | 2931 | result *= 127; |
duke@1 | 2932 | result += visit(s); |
duke@1 | 2933 | } |
duke@1 | 2934 | return result; |
duke@1 | 2935 | } |
duke@1 | 2936 | |
duke@1 | 2937 | @Override |
duke@1 | 2938 | public Integer visitWildcardType(WildcardType t, Void ignored) { |
duke@1 | 2939 | int result = t.kind.hashCode(); |
duke@1 | 2940 | if (t.type != null) { |
duke@1 | 2941 | result *= 127; |
duke@1 | 2942 | result += visit(t.type); |
duke@1 | 2943 | } |
duke@1 | 2944 | return result; |
duke@1 | 2945 | } |
duke@1 | 2946 | |
duke@1 | 2947 | @Override |
duke@1 | 2948 | public Integer visitArrayType(ArrayType t, Void ignored) { |
duke@1 | 2949 | return visit(t.elemtype) + 12; |
duke@1 | 2950 | } |
duke@1 | 2951 | |
duke@1 | 2952 | @Override |
duke@1 | 2953 | public Integer visitTypeVar(TypeVar t, Void ignored) { |
duke@1 | 2954 | return System.identityHashCode(t.tsym); |
duke@1 | 2955 | } |
duke@1 | 2956 | |
duke@1 | 2957 | @Override |
duke@1 | 2958 | public Integer visitUndetVar(UndetVar t, Void ignored) { |
duke@1 | 2959 | return System.identityHashCode(t); |
duke@1 | 2960 | } |
duke@1 | 2961 | |
duke@1 | 2962 | @Override |
duke@1 | 2963 | public Integer visitErrorType(ErrorType t, Void ignored) { |
duke@1 | 2964 | return 0; |
duke@1 | 2965 | } |
duke@1 | 2966 | }; |
duke@1 | 2967 | // </editor-fold> |
duke@1 | 2968 | |
duke@1 | 2969 | // <editor-fold defaultstate="collapsed" desc="Return-Type-Substitutable"> |
duke@1 | 2970 | /** |
duke@1 | 2971 | * Does t have a result that is a subtype of the result type of s, |
duke@1 | 2972 | * suitable for covariant returns? It is assumed that both types |
duke@1 | 2973 | * are (possibly polymorphic) method types. Monomorphic method |
duke@1 | 2974 | * types are handled in the obvious way. Polymorphic method types |
duke@1 | 2975 | * require renaming all type variables of one to corresponding |
duke@1 | 2976 | * type variables in the other, where correspondence is by |
duke@1 | 2977 | * position in the type parameter list. */ |
duke@1 | 2978 | public boolean resultSubtype(Type t, Type s, Warner warner) { |
duke@1 | 2979 | List<Type> tvars = t.getTypeArguments(); |
duke@1 | 2980 | List<Type> svars = s.getTypeArguments(); |
duke@1 | 2981 | Type tres = t.getReturnType(); |
duke@1 | 2982 | Type sres = subst(s.getReturnType(), svars, tvars); |
duke@1 | 2983 | return covariantReturnType(tres, sres, warner); |
duke@1 | 2984 | } |
duke@1 | 2985 | |
duke@1 | 2986 | /** |
duke@1 | 2987 | * Return-Type-Substitutable. |
duke@1 | 2988 | * @see <a href="http://java.sun.com/docs/books/jls/">The Java |
duke@1 | 2989 | * Language Specification, Third Ed. (8.4.5)</a> |
duke@1 | 2990 | */ |
duke@1 | 2991 | public boolean returnTypeSubstitutable(Type r1, Type r2) { |
duke@1 | 2992 | if (hasSameArgs(r1, r2)) |
tbell@202 | 2993 | return resultSubtype(r1, r2, Warner.noWarnings); |
duke@1 | 2994 | else |
duke@1 | 2995 | return covariantReturnType(r1.getReturnType(), |
tbell@202 | 2996 | erasure(r2.getReturnType()), |
tbell@202 | 2997 | Warner.noWarnings); |
tbell@202 | 2998 | } |
tbell@202 | 2999 | |
tbell@202 | 3000 | public boolean returnTypeSubstitutable(Type r1, |
tbell@202 | 3001 | Type r2, Type r2res, |
tbell@202 | 3002 | Warner warner) { |
tbell@202 | 3003 | if (isSameType(r1.getReturnType(), r2res)) |
tbell@202 | 3004 | return true; |
tbell@202 | 3005 | if (r1.getReturnType().isPrimitive() || r2res.isPrimitive()) |
tbell@202 | 3006 | return false; |
tbell@202 | 3007 | |
tbell@202 | 3008 | if (hasSameArgs(r1, r2)) |
tbell@202 | 3009 | return covariantReturnType(r1.getReturnType(), r2res, warner); |
tbell@202 | 3010 | if (!source.allowCovariantReturns()) |
tbell@202 | 3011 | return false; |
tbell@202 | 3012 | if (isSubtypeUnchecked(r1.getReturnType(), r2res, warner)) |
tbell@202 | 3013 | return true; |
tbell@202 | 3014 | if (!isSubtype(r1.getReturnType(), erasure(r2res))) |
tbell@202 | 3015 | return false; |
mcimadamore@795 | 3016 | warner.warn(LintCategory.UNCHECKED); |
tbell@202 | 3017 | return true; |
duke@1 | 3018 | } |
duke@1 | 3019 | |
duke@1 | 3020 | /** |
duke@1 | 3021 | * Is t an appropriate return type in an overrider for a |
duke@1 | 3022 | * method that returns s? |
duke@1 | 3023 | */ |
duke@1 | 3024 | public boolean covariantReturnType(Type t, Type s, Warner warner) { |
tbell@202 | 3025 | return |
tbell@202 | 3026 | isSameType(t, s) || |
tbell@202 | 3027 | source.allowCovariantReturns() && |
duke@1 | 3028 | !t.isPrimitive() && |
tbell@202 | 3029 | !s.isPrimitive() && |
tbell@202 | 3030 | isAssignable(t, s, warner); |
duke@1 | 3031 | } |
duke@1 | 3032 | // </editor-fold> |
duke@1 | 3033 | |
duke@1 | 3034 | // <editor-fold defaultstate="collapsed" desc="Box/unbox support"> |
duke@1 | 3035 | /** |
duke@1 | 3036 | * Return the class that boxes the given primitive. |
duke@1 | 3037 | */ |
duke@1 | 3038 | public ClassSymbol boxedClass(Type t) { |
duke@1 | 3039 | return reader.enterClass(syms.boxedName[t.tag]); |
duke@1 | 3040 | } |
duke@1 | 3041 | |
duke@1 | 3042 | /** |
mcimadamore@753 | 3043 | * Return the boxed type if 't' is primitive, otherwise return 't' itself. |
mcimadamore@753 | 3044 | */ |
mcimadamore@753 | 3045 | public Type boxedTypeOrType(Type t) { |
mcimadamore@753 | 3046 | return t.isPrimitive() ? |
mcimadamore@753 | 3047 | boxedClass(t).type : |
mcimadamore@753 | 3048 | t; |
mcimadamore@753 | 3049 | } |
mcimadamore@753 | 3050 | |
mcimadamore@753 | 3051 | /** |
duke@1 | 3052 | * Return the primitive type corresponding to a boxed type. |
duke@1 | 3053 | */ |
duke@1 | 3054 | public Type unboxedType(Type t) { |
duke@1 | 3055 | if (allowBoxing) { |
duke@1 | 3056 | for (int i=0; i<syms.boxedName.length; i++) { |
duke@1 | 3057 | Name box = syms.boxedName[i]; |
duke@1 | 3058 | if (box != null && |
duke@1 | 3059 | asSuper(t, reader.enterClass(box)) != null) |
duke@1 | 3060 | return syms.typeOfTag[i]; |
duke@1 | 3061 | } |
duke@1 | 3062 | } |
duke@1 | 3063 | return Type.noType; |
duke@1 | 3064 | } |
duke@1 | 3065 | // </editor-fold> |
duke@1 | 3066 | |
duke@1 | 3067 | // <editor-fold defaultstate="collapsed" desc="Capture conversion"> |
duke@1 | 3068 | /* |
duke@1 | 3069 | * JLS 3rd Ed. 5.1.10 Capture Conversion: |
duke@1 | 3070 | * |
duke@1 | 3071 | * Let G name a generic type declaration with n formal type |
duke@1 | 3072 | * parameters A1 ... An with corresponding bounds U1 ... Un. There |
duke@1 | 3073 | * exists a capture conversion from G<T1 ... Tn> to G<S1 ... Sn>, |
duke@1 | 3074 | * where, for 1 <= i <= n: |
duke@1 | 3075 | * |
duke@1 | 3076 | * + If Ti is a wildcard type argument (4.5.1) of the form ? then |
duke@1 | 3077 | * Si is a fresh type variable whose upper bound is |
duke@1 | 3078 | * Ui[A1 := S1, ..., An := Sn] and whose lower bound is the null |
duke@1 | 3079 | * type. |
duke@1 | 3080 | * |
duke@1 | 3081 | * + If Ti is a wildcard type argument of the form ? extends Bi, |
duke@1 | 3082 | * then Si is a fresh type variable whose upper bound is |
duke@1 | 3083 | * glb(Bi, Ui[A1 := S1, ..., An := Sn]) and whose lower bound is |
duke@1 | 3084 | * the null type, where glb(V1,... ,Vm) is V1 & ... & Vm. It is |
duke@1 | 3085 | * a compile-time error if for any two classes (not interfaces) |
duke@1 | 3086 | * Vi and Vj,Vi is not a subclass of Vj or vice versa. |
duke@1 | 3087 | * |
duke@1 | 3088 | * + If Ti is a wildcard type argument of the form ? super Bi, |
duke@1 | 3089 | * then Si is a fresh type variable whose upper bound is |
duke@1 | 3090 | * Ui[A1 := S1, ..., An := Sn] and whose lower bound is Bi. |
duke@1 | 3091 | * |
duke@1 | 3092 | * + Otherwise, Si = Ti. |
duke@1 | 3093 | * |
duke@1 | 3094 | * Capture conversion on any type other than a parameterized type |
duke@1 | 3095 | * (4.5) acts as an identity conversion (5.1.1). Capture |
duke@1 | 3096 | * conversions never require a special action at run time and |
duke@1 | 3097 | * therefore never throw an exception at run time. |
duke@1 | 3098 | * |
duke@1 | 3099 | * Capture conversion is not applied recursively. |
duke@1 | 3100 | */ |
duke@1 | 3101 | /** |
duke@1 | 3102 | * Capture conversion as specified by JLS 3rd Ed. |
duke@1 | 3103 | */ |
mcimadamore@299 | 3104 | |
mcimadamore@299 | 3105 | public List<Type> capture(List<Type> ts) { |
mcimadamore@299 | 3106 | List<Type> buf = List.nil(); |
mcimadamore@299 | 3107 | for (Type t : ts) { |
mcimadamore@299 | 3108 | buf = buf.prepend(capture(t)); |
mcimadamore@299 | 3109 | } |
mcimadamore@299 | 3110 | return buf.reverse(); |
mcimadamore@299 | 3111 | } |
duke@1 | 3112 | public Type capture(Type t) { |
duke@1 | 3113 | if (t.tag != CLASS) |
duke@1 | 3114 | return t; |
mcimadamore@637 | 3115 | if (t.getEnclosingType() != Type.noType) { |
mcimadamore@637 | 3116 | Type capturedEncl = capture(t.getEnclosingType()); |
mcimadamore@637 | 3117 | if (capturedEncl != t.getEnclosingType()) { |
mcimadamore@637 | 3118 | Type type1 = memberType(capturedEncl, t.tsym); |
mcimadamore@637 | 3119 | t = subst(type1, t.tsym.type.getTypeArguments(), t.getTypeArguments()); |
mcimadamore@637 | 3120 | } |
mcimadamore@637 | 3121 | } |
duke@1 | 3122 | ClassType cls = (ClassType)t; |
duke@1 | 3123 | if (cls.isRaw() || !cls.isParameterized()) |
duke@1 | 3124 | return cls; |
duke@1 | 3125 | |
duke@1 | 3126 | ClassType G = (ClassType)cls.asElement().asType(); |
duke@1 | 3127 | List<Type> A = G.getTypeArguments(); |
duke@1 | 3128 | List<Type> T = cls.getTypeArguments(); |
duke@1 | 3129 | List<Type> S = freshTypeVariables(T); |
duke@1 | 3130 | |
duke@1 | 3131 | List<Type> currentA = A; |
duke@1 | 3132 | List<Type> currentT = T; |
duke@1 | 3133 | List<Type> currentS = S; |
duke@1 | 3134 | boolean captured = false; |
duke@1 | 3135 | while (!currentA.isEmpty() && |
duke@1 | 3136 | !currentT.isEmpty() && |
duke@1 | 3137 | !currentS.isEmpty()) { |
duke@1 | 3138 | if (currentS.head != currentT.head) { |
duke@1 | 3139 | captured = true; |
duke@1 | 3140 | WildcardType Ti = (WildcardType)currentT.head; |
duke@1 | 3141 | Type Ui = currentA.head.getUpperBound(); |
duke@1 | 3142 | CapturedType Si = (CapturedType)currentS.head; |
duke@1 | 3143 | if (Ui == null) |
duke@1 | 3144 | Ui = syms.objectType; |
duke@1 | 3145 | switch (Ti.kind) { |
duke@1 | 3146 | case UNBOUND: |
duke@1 | 3147 | Si.bound = subst(Ui, A, S); |
duke@1 | 3148 | Si.lower = syms.botType; |
duke@1 | 3149 | break; |
duke@1 | 3150 | case EXTENDS: |
duke@1 | 3151 | Si.bound = glb(Ti.getExtendsBound(), subst(Ui, A, S)); |
duke@1 | 3152 | Si.lower = syms.botType; |
duke@1 | 3153 | break; |
duke@1 | 3154 | case SUPER: |
duke@1 | 3155 | Si.bound = subst(Ui, A, S); |
duke@1 | 3156 | Si.lower = Ti.getSuperBound(); |
duke@1 | 3157 | break; |
duke@1 | 3158 | } |
duke@1 | 3159 | if (Si.bound == Si.lower) |
duke@1 | 3160 | currentS.head = Si.bound; |
duke@1 | 3161 | } |
duke@1 | 3162 | currentA = currentA.tail; |
duke@1 | 3163 | currentT = currentT.tail; |
duke@1 | 3164 | currentS = currentS.tail; |
duke@1 | 3165 | } |
duke@1 | 3166 | if (!currentA.isEmpty() || !currentT.isEmpty() || !currentS.isEmpty()) |
duke@1 | 3167 | return erasure(t); // some "rare" type involved |
duke@1 | 3168 | |
duke@1 | 3169 | if (captured) |
duke@1 | 3170 | return new ClassType(cls.getEnclosingType(), S, cls.tsym); |
duke@1 | 3171 | else |
duke@1 | 3172 | return t; |
duke@1 | 3173 | } |
duke@1 | 3174 | // where |
mcimadamore@238 | 3175 | public List<Type> freshTypeVariables(List<Type> types) { |
duke@1 | 3176 | ListBuffer<Type> result = lb(); |
duke@1 | 3177 | for (Type t : types) { |
duke@1 | 3178 | if (t.tag == WILDCARD) { |
duke@1 | 3179 | Type bound = ((WildcardType)t).getExtendsBound(); |
duke@1 | 3180 | if (bound == null) |
duke@1 | 3181 | bound = syms.objectType; |
duke@1 | 3182 | result.append(new CapturedType(capturedName, |
duke@1 | 3183 | syms.noSymbol, |
duke@1 | 3184 | bound, |
duke@1 | 3185 | syms.botType, |
duke@1 | 3186 | (WildcardType)t)); |
duke@1 | 3187 | } else { |
duke@1 | 3188 | result.append(t); |
duke@1 | 3189 | } |
duke@1 | 3190 | } |
duke@1 | 3191 | return result.toList(); |
duke@1 | 3192 | } |
duke@1 | 3193 | // </editor-fold> |
duke@1 | 3194 | |
duke@1 | 3195 | // <editor-fold defaultstate="collapsed" desc="Internal utility methods"> |
duke@1 | 3196 | private List<Type> upperBounds(List<Type> ss) { |
duke@1 | 3197 | if (ss.isEmpty()) return ss; |
duke@1 | 3198 | Type head = upperBound(ss.head); |
duke@1 | 3199 | List<Type> tail = upperBounds(ss.tail); |
duke@1 | 3200 | if (head != ss.head || tail != ss.tail) |
duke@1 | 3201 | return tail.prepend(head); |
duke@1 | 3202 | else |
duke@1 | 3203 | return ss; |
duke@1 | 3204 | } |
duke@1 | 3205 | |
duke@1 | 3206 | private boolean sideCast(Type from, Type to, Warner warn) { |
duke@1 | 3207 | // We are casting from type $from$ to type $to$, which are |
duke@1 | 3208 | // non-final unrelated types. This method |
duke@1 | 3209 | // tries to reject a cast by transferring type parameters |
duke@1 | 3210 | // from $to$ to $from$ by common superinterfaces. |
duke@1 | 3211 | boolean reverse = false; |
duke@1 | 3212 | Type target = to; |
duke@1 | 3213 | if ((to.tsym.flags() & INTERFACE) == 0) { |
jjg@816 | 3214 | Assert.check((from.tsym.flags() & INTERFACE) != 0); |
duke@1 | 3215 | reverse = true; |
duke@1 | 3216 | to = from; |
duke@1 | 3217 | from = target; |
duke@1 | 3218 | } |
duke@1 | 3219 | List<Type> commonSupers = superClosure(to, erasure(from)); |
duke@1 | 3220 | boolean giveWarning = commonSupers.isEmpty(); |
duke@1 | 3221 | // The arguments to the supers could be unified here to |
duke@1 | 3222 | // get a more accurate analysis |
duke@1 | 3223 | while (commonSupers.nonEmpty()) { |
duke@1 | 3224 | Type t1 = asSuper(from, commonSupers.head.tsym); |
duke@1 | 3225 | Type t2 = commonSupers.head; // same as asSuper(to, commonSupers.head.tsym); |
duke@1 | 3226 | if (disjointTypes(t1.getTypeArguments(), t2.getTypeArguments())) |
duke@1 | 3227 | return false; |
duke@1 | 3228 | giveWarning = giveWarning || (reverse ? giveWarning(t2, t1) : giveWarning(t1, t2)); |
duke@1 | 3229 | commonSupers = commonSupers.tail; |
duke@1 | 3230 | } |
mcimadamore@187 | 3231 | if (giveWarning && !isReifiable(reverse ? from : to)) |
mcimadamore@795 | 3232 | warn.warn(LintCategory.UNCHECKED); |
duke@1 | 3233 | if (!source.allowCovariantReturns()) |
duke@1 | 3234 | // reject if there is a common method signature with |
duke@1 | 3235 | // incompatible return types. |
duke@1 | 3236 | chk.checkCompatibleAbstracts(warn.pos(), from, to); |
duke@1 | 3237 | return true; |
duke@1 | 3238 | } |
duke@1 | 3239 | |
duke@1 | 3240 | private boolean sideCastFinal(Type from, Type to, Warner warn) { |
duke@1 | 3241 | // We are casting from type $from$ to type $to$, which are |
duke@1 | 3242 | // unrelated types one of which is final and the other of |
duke@1 | 3243 | // which is an interface. This method |
duke@1 | 3244 | // tries to reject a cast by transferring type parameters |
duke@1 | 3245 | // from the final class to the interface. |
duke@1 | 3246 | boolean reverse = false; |
duke@1 | 3247 | Type target = to; |
duke@1 | 3248 | if ((to.tsym.flags() & INTERFACE) == 0) { |
jjg@816 | 3249 | Assert.check((from.tsym.flags() & INTERFACE) != 0); |
duke@1 | 3250 | reverse = true; |
duke@1 | 3251 | to = from; |
duke@1 | 3252 | from = target; |
duke@1 | 3253 | } |
jjg@816 | 3254 | Assert.check((from.tsym.flags() & FINAL) != 0); |
duke@1 | 3255 | Type t1 = asSuper(from, to.tsym); |
duke@1 | 3256 | if (t1 == null) return false; |
duke@1 | 3257 | Type t2 = to; |
duke@1 | 3258 | if (disjointTypes(t1.getTypeArguments(), t2.getTypeArguments())) |
duke@1 | 3259 | return false; |
duke@1 | 3260 | if (!source.allowCovariantReturns()) |
duke@1 | 3261 | // reject if there is a common method signature with |
duke@1 | 3262 | // incompatible return types. |
duke@1 | 3263 | chk.checkCompatibleAbstracts(warn.pos(), from, to); |
duke@1 | 3264 | if (!isReifiable(target) && |
duke@1 | 3265 | (reverse ? giveWarning(t2, t1) : giveWarning(t1, t2))) |
mcimadamore@795 | 3266 | warn.warn(LintCategory.UNCHECKED); |
duke@1 | 3267 | return true; |
duke@1 | 3268 | } |
duke@1 | 3269 | |
duke@1 | 3270 | private boolean giveWarning(Type from, Type to) { |
mcimadamore@235 | 3271 | Type subFrom = asSub(from, to.tsym); |
mcimadamore@235 | 3272 | return to.isParameterized() && |
mcimadamore@235 | 3273 | (!(isUnbounded(to) || |
mcimadamore@235 | 3274 | isSubtype(from, to) || |
mcimadamore@736 | 3275 | ((subFrom != null) && containsType(to.allparams(), subFrom.allparams())))); |
duke@1 | 3276 | } |
duke@1 | 3277 | |
duke@1 | 3278 | private List<Type> superClosure(Type t, Type s) { |
duke@1 | 3279 | List<Type> cl = List.nil(); |
duke@1 | 3280 | for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail) { |
duke@1 | 3281 | if (isSubtype(s, erasure(l.head))) { |
duke@1 | 3282 | cl = insert(cl, l.head); |
duke@1 | 3283 | } else { |
duke@1 | 3284 | cl = union(cl, superClosure(l.head, s)); |
duke@1 | 3285 | } |
duke@1 | 3286 | } |
duke@1 | 3287 | return cl; |
duke@1 | 3288 | } |
duke@1 | 3289 | |
duke@1 | 3290 | private boolean containsTypeEquivalent(Type t, Type s) { |
duke@1 | 3291 | return |
duke@1 | 3292 | isSameType(t, s) || // shortcut |
duke@1 | 3293 | containsType(t, s) && containsType(s, t); |
duke@1 | 3294 | } |
duke@1 | 3295 | |
mcimadamore@138 | 3296 | // <editor-fold defaultstate="collapsed" desc="adapt"> |
duke@1 | 3297 | /** |
duke@1 | 3298 | * Adapt a type by computing a substitution which maps a source |
duke@1 | 3299 | * type to a target type. |
duke@1 | 3300 | * |
duke@1 | 3301 | * @param source the source type |
duke@1 | 3302 | * @param target the target type |
duke@1 | 3303 | * @param from the type variables of the computed substitution |
duke@1 | 3304 | * @param to the types of the computed substitution. |
duke@1 | 3305 | */ |
duke@1 | 3306 | public void adapt(Type source, |
duke@1 | 3307 | Type target, |
duke@1 | 3308 | ListBuffer<Type> from, |
duke@1 | 3309 | ListBuffer<Type> to) throws AdaptFailure { |
mcimadamore@138 | 3310 | new Adapter(from, to).adapt(source, target); |
mcimadamore@138 | 3311 | } |
mcimadamore@138 | 3312 | |
mcimadamore@138 | 3313 | class Adapter extends SimpleVisitor<Void, Type> { |
mcimadamore@138 | 3314 | |
mcimadamore@138 | 3315 | ListBuffer<Type> from; |
mcimadamore@138 | 3316 | ListBuffer<Type> to; |
mcimadamore@138 | 3317 | Map<Symbol,Type> mapping; |
mcimadamore@138 | 3318 | |
mcimadamore@138 | 3319 | Adapter(ListBuffer<Type> from, ListBuffer<Type> to) { |
mcimadamore@138 | 3320 | this.from = from; |
mcimadamore@138 | 3321 | this.to = to; |
mcimadamore@138 | 3322 | mapping = new HashMap<Symbol,Type>(); |
duke@1 | 3323 | } |
mcimadamore@138 | 3324 | |
mcimadamore@138 | 3325 | public void adapt(Type source, Type target) throws AdaptFailure { |
mcimadamore@138 | 3326 | visit(source, target); |
mcimadamore@138 | 3327 | List<Type> fromList = from.toList(); |
mcimadamore@138 | 3328 | List<Type> toList = to.toList(); |
mcimadamore@138 | 3329 | while (!fromList.isEmpty()) { |
mcimadamore@138 | 3330 | Type val = mapping.get(fromList.head.tsym); |
mcimadamore@138 | 3331 | if (toList.head != val) |
mcimadamore@138 | 3332 | toList.head = val; |
mcimadamore@138 | 3333 | fromList = fromList.tail; |
mcimadamore@138 | 3334 | toList = toList.tail; |
mcimadamore@138 | 3335 | } |
mcimadamore@138 | 3336 | } |
mcimadamore@138 | 3337 | |
mcimadamore@138 | 3338 | @Override |
mcimadamore@138 | 3339 | public Void visitClassType(ClassType source, Type target) throws AdaptFailure { |
mcimadamore@138 | 3340 | if (target.tag == CLASS) |
mcimadamore@138 | 3341 | adaptRecursive(source.allparams(), target.allparams()); |
mcimadamore@138 | 3342 | return null; |
mcimadamore@138 | 3343 | } |
mcimadamore@138 | 3344 | |
mcimadamore@138 | 3345 | @Override |
mcimadamore@138 | 3346 | public Void visitArrayType(ArrayType source, Type target) throws AdaptFailure { |
mcimadamore@138 | 3347 | if (target.tag == ARRAY) |
mcimadamore@138 | 3348 | adaptRecursive(elemtype(source), elemtype(target)); |
mcimadamore@138 | 3349 | return null; |
mcimadamore@138 | 3350 | } |
mcimadamore@138 | 3351 | |
mcimadamore@138 | 3352 | @Override |
mcimadamore@138 | 3353 | public Void visitWildcardType(WildcardType source, Type target) throws AdaptFailure { |
mcimadamore@138 | 3354 | if (source.isExtendsBound()) |
mcimadamore@138 | 3355 | adaptRecursive(upperBound(source), upperBound(target)); |
mcimadamore@138 | 3356 | else if (source.isSuperBound()) |
mcimadamore@138 | 3357 | adaptRecursive(lowerBound(source), lowerBound(target)); |
mcimadamore@138 | 3358 | return null; |
mcimadamore@138 | 3359 | } |
mcimadamore@138 | 3360 | |
mcimadamore@138 | 3361 | @Override |
mcimadamore@138 | 3362 | public Void visitTypeVar(TypeVar source, Type target) throws AdaptFailure { |
mcimadamore@138 | 3363 | // Check to see if there is |
mcimadamore@138 | 3364 | // already a mapping for $source$, in which case |
mcimadamore@138 | 3365 | // the old mapping will be merged with the new |
mcimadamore@138 | 3366 | Type val = mapping.get(source.tsym); |
mcimadamore@138 | 3367 | if (val != null) { |
mcimadamore@138 | 3368 | if (val.isSuperBound() && target.isSuperBound()) { |
mcimadamore@138 | 3369 | val = isSubtype(lowerBound(val), lowerBound(target)) |
mcimadamore@138 | 3370 | ? target : val; |
mcimadamore@138 | 3371 | } else if (val.isExtendsBound() && target.isExtendsBound()) { |
mcimadamore@138 | 3372 | val = isSubtype(upperBound(val), upperBound(target)) |
mcimadamore@138 | 3373 | ? val : target; |
mcimadamore@138 | 3374 | } else if (!isSameType(val, target)) { |
mcimadamore@138 | 3375 | throw new AdaptFailure(); |
duke@1 | 3376 | } |
mcimadamore@138 | 3377 | } else { |
mcimadamore@138 | 3378 | val = target; |
mcimadamore@138 | 3379 | from.append(source); |
mcimadamore@138 | 3380 | to.append(target); |
mcimadamore@138 | 3381 | } |
mcimadamore@138 | 3382 | mapping.put(source.tsym, val); |
mcimadamore@138 | 3383 | return null; |
mcimadamore@138 | 3384 | } |
mcimadamore@138 | 3385 | |
mcimadamore@138 | 3386 | @Override |
mcimadamore@138 | 3387 | public Void visitType(Type source, Type target) { |
mcimadamore@138 | 3388 | return null; |
mcimadamore@138 | 3389 | } |
mcimadamore@138 | 3390 | |
mcimadamore@138 | 3391 | private Set<TypePair> cache = new HashSet<TypePair>(); |
mcimadamore@138 | 3392 | |
mcimadamore@138 | 3393 | private void adaptRecursive(Type source, Type target) { |
mcimadamore@138 | 3394 | TypePair pair = new TypePair(source, target); |
mcimadamore@138 | 3395 | if (cache.add(pair)) { |
mcimadamore@138 | 3396 | try { |
mcimadamore@138 | 3397 | visit(source, target); |
mcimadamore@138 | 3398 | } finally { |
mcimadamore@138 | 3399 | cache.remove(pair); |
duke@1 | 3400 | } |
duke@1 | 3401 | } |
duke@1 | 3402 | } |
mcimadamore@138 | 3403 | |
mcimadamore@138 | 3404 | private void adaptRecursive(List<Type> source, List<Type> target) { |
mcimadamore@138 | 3405 | if (source.length() == target.length()) { |
mcimadamore@138 | 3406 | while (source.nonEmpty()) { |
mcimadamore@138 | 3407 | adaptRecursive(source.head, target.head); |
mcimadamore@138 | 3408 | source = source.tail; |
mcimadamore@138 | 3409 | target = target.tail; |
mcimadamore@138 | 3410 | } |
duke@1 | 3411 | } |
duke@1 | 3412 | } |
duke@1 | 3413 | } |
duke@1 | 3414 | |
mcimadamore@138 | 3415 | public static class AdaptFailure extends RuntimeException { |
mcimadamore@138 | 3416 | static final long serialVersionUID = -7490231548272701566L; |
mcimadamore@138 | 3417 | } |
mcimadamore@138 | 3418 | |
duke@1 | 3419 | private void adaptSelf(Type t, |
duke@1 | 3420 | ListBuffer<Type> from, |
duke@1 | 3421 | ListBuffer<Type> to) { |
duke@1 | 3422 | try { |
duke@1 | 3423 | //if (t.tsym.type != t) |
duke@1 | 3424 | adapt(t.tsym.type, t, from, to); |
duke@1 | 3425 | } catch (AdaptFailure ex) { |
duke@1 | 3426 | // Adapt should never fail calculating a mapping from |
duke@1 | 3427 | // t.tsym.type to t as there can be no merge problem. |
duke@1 | 3428 | throw new AssertionError(ex); |
duke@1 | 3429 | } |
duke@1 | 3430 | } |
mcimadamore@138 | 3431 | // </editor-fold> |
duke@1 | 3432 | |
duke@1 | 3433 | /** |
duke@1 | 3434 | * Rewrite all type variables (universal quantifiers) in the given |
duke@1 | 3435 | * type to wildcards (existential quantifiers). This is used to |
duke@1 | 3436 | * determine if a cast is allowed. For example, if high is true |
duke@1 | 3437 | * and {@code T <: Number}, then {@code List<T>} is rewritten to |
duke@1 | 3438 | * {@code List<? extends Number>}. Since {@code List<Integer> <: |
duke@1 | 3439 | * List<? extends Number>} a {@code List<T>} can be cast to {@code |
duke@1 | 3440 | * List<Integer>} with a warning. |
duke@1 | 3441 | * @param t a type |
duke@1 | 3442 | * @param high if true return an upper bound; otherwise a lower |
duke@1 | 3443 | * bound |
duke@1 | 3444 | * @param rewriteTypeVars only rewrite captured wildcards if false; |
duke@1 | 3445 | * otherwise rewrite all type variables |
duke@1 | 3446 | * @return the type rewritten with wildcards (existential |
duke@1 | 3447 | * quantifiers) only |
duke@1 | 3448 | */ |
duke@1 | 3449 | private Type rewriteQuantifiers(Type t, boolean high, boolean rewriteTypeVars) { |
mcimadamore@640 | 3450 | return new Rewriter(high, rewriteTypeVars).visit(t); |
mcimadamore@157 | 3451 | } |
mcimadamore@157 | 3452 | |
mcimadamore@157 | 3453 | class Rewriter extends UnaryVisitor<Type> { |
mcimadamore@157 | 3454 | |
mcimadamore@157 | 3455 | boolean high; |
mcimadamore@157 | 3456 | boolean rewriteTypeVars; |
mcimadamore@157 | 3457 | |
mcimadamore@157 | 3458 | Rewriter(boolean high, boolean rewriteTypeVars) { |
mcimadamore@157 | 3459 | this.high = high; |
mcimadamore@157 | 3460 | this.rewriteTypeVars = rewriteTypeVars; |
mcimadamore@157 | 3461 | } |
mcimadamore@157 | 3462 | |
mcimadamore@640 | 3463 | @Override |
mcimadamore@640 | 3464 | public Type visitClassType(ClassType t, Void s) { |
mcimadamore@157 | 3465 | ListBuffer<Type> rewritten = new ListBuffer<Type>(); |
mcimadamore@157 | 3466 | boolean changed = false; |
mcimadamore@640 | 3467 | for (Type arg : t.allparams()) { |
mcimadamore@157 | 3468 | Type bound = visit(arg); |
mcimadamore@157 | 3469 | if (arg != bound) { |
mcimadamore@157 | 3470 | changed = true; |
mcimadamore@157 | 3471 | } |
mcimadamore@157 | 3472 | rewritten.append(bound); |
duke@1 | 3473 | } |
mcimadamore@157 | 3474 | if (changed) |
mcimadamore@640 | 3475 | return subst(t.tsym.type, |
mcimadamore@640 | 3476 | t.tsym.type.allparams(), |
mcimadamore@640 | 3477 | rewritten.toList()); |
mcimadamore@157 | 3478 | else |
mcimadamore@157 | 3479 | return t; |
duke@1 | 3480 | } |
mcimadamore@157 | 3481 | |
mcimadamore@157 | 3482 | public Type visitType(Type t, Void s) { |
mcimadamore@157 | 3483 | return high ? upperBound(t) : lowerBound(t); |
mcimadamore@157 | 3484 | } |
mcimadamore@157 | 3485 | |
mcimadamore@157 | 3486 | @Override |
mcimadamore@157 | 3487 | public Type visitCapturedType(CapturedType t, Void s) { |
mcimadamore@640 | 3488 | Type bound = visitWildcardType(t.wildcard, null); |
mcimadamore@640 | 3489 | return (bound.contains(t)) ? |
mcimadamore@779 | 3490 | erasure(bound) : |
mcimadamore@779 | 3491 | bound; |
mcimadamore@157 | 3492 | } |
mcimadamore@157 | 3493 | |
mcimadamore@157 | 3494 | @Override |
mcimadamore@157 | 3495 | public Type visitTypeVar(TypeVar t, Void s) { |
mcimadamore@640 | 3496 | if (rewriteTypeVars) { |
mcimadamore@640 | 3497 | Type bound = high ? |
mcimadamore@640 | 3498 | (t.bound.contains(t) ? |
mcimadamore@779 | 3499 | erasure(t.bound) : |
mcimadamore@640 | 3500 | visit(t.bound)) : |
mcimadamore@640 | 3501 | syms.botType; |
mcimadamore@640 | 3502 | return rewriteAsWildcardType(bound, t); |
mcimadamore@640 | 3503 | } |
mcimadamore@157 | 3504 | else |
mcimadamore@157 | 3505 | return t; |
mcimadamore@157 | 3506 | } |
mcimadamore@157 | 3507 | |
mcimadamore@157 | 3508 | @Override |
mcimadamore@157 | 3509 | public Type visitWildcardType(WildcardType t, Void s) { |
mcimadamore@157 | 3510 | Type bound = high ? t.getExtendsBound() : |
mcimadamore@157 | 3511 | t.getSuperBound(); |
mcimadamore@157 | 3512 | if (bound == null) |
mcimadamore@640 | 3513 | bound = high ? syms.objectType : syms.botType; |
mcimadamore@640 | 3514 | return rewriteAsWildcardType(visit(bound), t.bound); |
mcimadamore@640 | 3515 | } |
mcimadamore@640 | 3516 | |
mcimadamore@640 | 3517 | private Type rewriteAsWildcardType(Type bound, TypeVar formal) { |
mcimadamore@640 | 3518 | return high ? |
mcimadamore@640 | 3519 | makeExtendsWildcard(B(bound), formal) : |
mcimadamore@640 | 3520 | makeSuperWildcard(B(bound), formal); |
mcimadamore@640 | 3521 | } |
mcimadamore@640 | 3522 | |
mcimadamore@640 | 3523 | Type B(Type t) { |
mcimadamore@640 | 3524 | while (t.tag == WILDCARD) { |
mcimadamore@640 | 3525 | WildcardType w = (WildcardType)t; |
mcimadamore@640 | 3526 | t = high ? |
mcimadamore@640 | 3527 | w.getExtendsBound() : |
mcimadamore@640 | 3528 | w.getSuperBound(); |
mcimadamore@640 | 3529 | if (t == null) { |
mcimadamore@640 | 3530 | t = high ? syms.objectType : syms.botType; |
mcimadamore@640 | 3531 | } |
mcimadamore@640 | 3532 | } |
mcimadamore@640 | 3533 | return t; |
mcimadamore@157 | 3534 | } |
duke@1 | 3535 | } |
duke@1 | 3536 | |
mcimadamore@640 | 3537 | |
duke@1 | 3538 | /** |
duke@1 | 3539 | * Create a wildcard with the given upper (extends) bound; create |
duke@1 | 3540 | * an unbounded wildcard if bound is Object. |
duke@1 | 3541 | * |
duke@1 | 3542 | * @param bound the upper bound |
duke@1 | 3543 | * @param formal the formal type parameter that will be |
duke@1 | 3544 | * substituted by the wildcard |
duke@1 | 3545 | */ |
duke@1 | 3546 | private WildcardType makeExtendsWildcard(Type bound, TypeVar formal) { |
duke@1 | 3547 | if (bound == syms.objectType) { |
duke@1 | 3548 | return new WildcardType(syms.objectType, |
duke@1 | 3549 | BoundKind.UNBOUND, |
duke@1 | 3550 | syms.boundClass, |
duke@1 | 3551 | formal); |
duke@1 | 3552 | } else { |
duke@1 | 3553 | return new WildcardType(bound, |
duke@1 | 3554 | BoundKind.EXTENDS, |
duke@1 | 3555 | syms.boundClass, |
duke@1 | 3556 | formal); |
duke@1 | 3557 | } |
duke@1 | 3558 | } |
duke@1 | 3559 | |
duke@1 | 3560 | /** |
duke@1 | 3561 | * Create a wildcard with the given lower (super) bound; create an |
duke@1 | 3562 | * unbounded wildcard if bound is bottom (type of {@code null}). |
duke@1 | 3563 | * |
duke@1 | 3564 | * @param bound the lower bound |
duke@1 | 3565 | * @param formal the formal type parameter that will be |
duke@1 | 3566 | * substituted by the wildcard |
duke@1 | 3567 | */ |
duke@1 | 3568 | private WildcardType makeSuperWildcard(Type bound, TypeVar formal) { |
duke@1 | 3569 | if (bound.tag == BOT) { |
duke@1 | 3570 | return new WildcardType(syms.objectType, |
duke@1 | 3571 | BoundKind.UNBOUND, |
duke@1 | 3572 | syms.boundClass, |
duke@1 | 3573 | formal); |
duke@1 | 3574 | } else { |
duke@1 | 3575 | return new WildcardType(bound, |
duke@1 | 3576 | BoundKind.SUPER, |
duke@1 | 3577 | syms.boundClass, |
duke@1 | 3578 | formal); |
duke@1 | 3579 | } |
duke@1 | 3580 | } |
duke@1 | 3581 | |
duke@1 | 3582 | /** |
duke@1 | 3583 | * A wrapper for a type that allows use in sets. |
duke@1 | 3584 | */ |
duke@1 | 3585 | class SingletonType { |
duke@1 | 3586 | final Type t; |
duke@1 | 3587 | SingletonType(Type t) { |
duke@1 | 3588 | this.t = t; |
duke@1 | 3589 | } |
duke@1 | 3590 | public int hashCode() { |
jjg@507 | 3591 | return Types.hashCode(t); |
duke@1 | 3592 | } |
duke@1 | 3593 | public boolean equals(Object obj) { |
duke@1 | 3594 | return (obj instanceof SingletonType) && |
duke@1 | 3595 | isSameType(t, ((SingletonType)obj).t); |
duke@1 | 3596 | } |
duke@1 | 3597 | public String toString() { |
duke@1 | 3598 | return t.toString(); |
duke@1 | 3599 | } |
duke@1 | 3600 | } |
duke@1 | 3601 | // </editor-fold> |
duke@1 | 3602 | |
duke@1 | 3603 | // <editor-fold defaultstate="collapsed" desc="Visitors"> |
duke@1 | 3604 | /** |
duke@1 | 3605 | * A default visitor for types. All visitor methods except |
duke@1 | 3606 | * visitType are implemented by delegating to visitType. Concrete |
duke@1 | 3607 | * subclasses must provide an implementation of visitType and can |
duke@1 | 3608 | * override other methods as needed. |
duke@1 | 3609 | * |
duke@1 | 3610 | * @param <R> the return type of the operation implemented by this |
duke@1 | 3611 | * visitor; use Void if no return type is needed. |
duke@1 | 3612 | * @param <S> the type of the second argument (the first being the |
duke@1 | 3613 | * type itself) of the operation implemented by this visitor; use |
duke@1 | 3614 | * Void if a second argument is not needed. |
duke@1 | 3615 | */ |
duke@1 | 3616 | public static abstract class DefaultTypeVisitor<R,S> implements Type.Visitor<R,S> { |
duke@1 | 3617 | final public R visit(Type t, S s) { return t.accept(this, s); } |
duke@1 | 3618 | public R visitClassType(ClassType t, S s) { return visitType(t, s); } |
duke@1 | 3619 | public R visitWildcardType(WildcardType t, S s) { return visitType(t, s); } |
duke@1 | 3620 | public R visitArrayType(ArrayType t, S s) { return visitType(t, s); } |
duke@1 | 3621 | public R visitMethodType(MethodType t, S s) { return visitType(t, s); } |
duke@1 | 3622 | public R visitPackageType(PackageType t, S s) { return visitType(t, s); } |
duke@1 | 3623 | public R visitTypeVar(TypeVar t, S s) { return visitType(t, s); } |
duke@1 | 3624 | public R visitCapturedType(CapturedType t, S s) { return visitType(t, s); } |
duke@1 | 3625 | public R visitForAll(ForAll t, S s) { return visitType(t, s); } |
duke@1 | 3626 | public R visitUndetVar(UndetVar t, S s) { return visitType(t, s); } |
duke@1 | 3627 | public R visitErrorType(ErrorType t, S s) { return visitType(t, s); } |
duke@1 | 3628 | } |
duke@1 | 3629 | |
duke@1 | 3630 | /** |
mcimadamore@121 | 3631 | * A default visitor for symbols. All visitor methods except |
mcimadamore@121 | 3632 | * visitSymbol are implemented by delegating to visitSymbol. Concrete |
mcimadamore@121 | 3633 | * subclasses must provide an implementation of visitSymbol and can |
mcimadamore@121 | 3634 | * override other methods as needed. |
mcimadamore@121 | 3635 | * |
mcimadamore@121 | 3636 | * @param <R> the return type of the operation implemented by this |
mcimadamore@121 | 3637 | * visitor; use Void if no return type is needed. |
mcimadamore@121 | 3638 | * @param <S> the type of the second argument (the first being the |
mcimadamore@121 | 3639 | * symbol itself) of the operation implemented by this visitor; use |
mcimadamore@121 | 3640 | * Void if a second argument is not needed. |
mcimadamore@121 | 3641 | */ |
mcimadamore@121 | 3642 | public static abstract class DefaultSymbolVisitor<R,S> implements Symbol.Visitor<R,S> { |
mcimadamore@121 | 3643 | final public R visit(Symbol s, S arg) { return s.accept(this, arg); } |
mcimadamore@121 | 3644 | public R visitClassSymbol(ClassSymbol s, S arg) { return visitSymbol(s, arg); } |
mcimadamore@121 | 3645 | public R visitMethodSymbol(MethodSymbol s, S arg) { return visitSymbol(s, arg); } |
mcimadamore@121 | 3646 | public R visitOperatorSymbol(OperatorSymbol s, S arg) { return visitSymbol(s, arg); } |
mcimadamore@121 | 3647 | public R visitPackageSymbol(PackageSymbol s, S arg) { return visitSymbol(s, arg); } |
mcimadamore@121 | 3648 | public R visitTypeSymbol(TypeSymbol s, S arg) { return visitSymbol(s, arg); } |
mcimadamore@121 | 3649 | public R visitVarSymbol(VarSymbol s, S arg) { return visitSymbol(s, arg); } |
mcimadamore@121 | 3650 | } |
mcimadamore@121 | 3651 | |
mcimadamore@121 | 3652 | /** |
duke@1 | 3653 | * A <em>simple</em> visitor for types. This visitor is simple as |
duke@1 | 3654 | * captured wildcards, for-all types (generic methods), and |
duke@1 | 3655 | * undetermined type variables (part of inference) are hidden. |
duke@1 | 3656 | * Captured wildcards are hidden by treating them as type |
duke@1 | 3657 | * variables and the rest are hidden by visiting their qtypes. |
duke@1 | 3658 | * |
duke@1 | 3659 | * @param <R> the return type of the operation implemented by this |
duke@1 | 3660 | * visitor; use Void if no return type is needed. |
duke@1 | 3661 | * @param <S> the type of the second argument (the first being the |
duke@1 | 3662 | * type itself) of the operation implemented by this visitor; use |
duke@1 | 3663 | * Void if a second argument is not needed. |
duke@1 | 3664 | */ |
duke@1 | 3665 | public static abstract class SimpleVisitor<R,S> extends DefaultTypeVisitor<R,S> { |
duke@1 | 3666 | @Override |
duke@1 | 3667 | public R visitCapturedType(CapturedType t, S s) { |
duke@1 | 3668 | return visitTypeVar(t, s); |
duke@1 | 3669 | } |
duke@1 | 3670 | @Override |
duke@1 | 3671 | public R visitForAll(ForAll t, S s) { |
duke@1 | 3672 | return visit(t.qtype, s); |
duke@1 | 3673 | } |
duke@1 | 3674 | @Override |
duke@1 | 3675 | public R visitUndetVar(UndetVar t, S s) { |
duke@1 | 3676 | return visit(t.qtype, s); |
duke@1 | 3677 | } |
duke@1 | 3678 | } |
duke@1 | 3679 | |
duke@1 | 3680 | /** |
duke@1 | 3681 | * A plain relation on types. That is a 2-ary function on the |
duke@1 | 3682 | * form Type × Type → Boolean. |
duke@1 | 3683 | * <!-- In plain text: Type x Type -> Boolean --> |
duke@1 | 3684 | */ |
duke@1 | 3685 | public static abstract class TypeRelation extends SimpleVisitor<Boolean,Type> {} |
duke@1 | 3686 | |
duke@1 | 3687 | /** |
duke@1 | 3688 | * A convenience visitor for implementing operations that only |
duke@1 | 3689 | * require one argument (the type itself), that is, unary |
duke@1 | 3690 | * operations. |
duke@1 | 3691 | * |
duke@1 | 3692 | * @param <R> the return type of the operation implemented by this |
duke@1 | 3693 | * visitor; use Void if no return type is needed. |
duke@1 | 3694 | */ |
duke@1 | 3695 | public static abstract class UnaryVisitor<R> extends SimpleVisitor<R,Void> { |
duke@1 | 3696 | final public R visit(Type t) { return t.accept(this, null); } |
duke@1 | 3697 | } |
duke@1 | 3698 | |
duke@1 | 3699 | /** |
duke@1 | 3700 | * A visitor for implementing a mapping from types to types. The |
duke@1 | 3701 | * default behavior of this class is to implement the identity |
duke@1 | 3702 | * mapping (mapping a type to itself). This can be overridden in |
duke@1 | 3703 | * subclasses. |
duke@1 | 3704 | * |
duke@1 | 3705 | * @param <S> the type of the second argument (the first being the |
duke@1 | 3706 | * type itself) of this mapping; use Void if a second argument is |
duke@1 | 3707 | * not needed. |
duke@1 | 3708 | */ |
duke@1 | 3709 | public static class MapVisitor<S> extends DefaultTypeVisitor<Type,S> { |
duke@1 | 3710 | final public Type visit(Type t) { return t.accept(this, null); } |
duke@1 | 3711 | public Type visitType(Type t, S s) { return t; } |
duke@1 | 3712 | } |
duke@1 | 3713 | // </editor-fold> |
jjg@657 | 3714 | |
jjg@657 | 3715 | |
jjg@657 | 3716 | // <editor-fold defaultstate="collapsed" desc="Annotation support"> |
jjg@657 | 3717 | |
jjg@657 | 3718 | public RetentionPolicy getRetention(Attribute.Compound a) { |
jjg@657 | 3719 | RetentionPolicy vis = RetentionPolicy.CLASS; // the default |
jjg@657 | 3720 | Attribute.Compound c = a.type.tsym.attribute(syms.retentionType.tsym); |
jjg@657 | 3721 | if (c != null) { |
jjg@657 | 3722 | Attribute value = c.member(names.value); |
jjg@657 | 3723 | if (value != null && value instanceof Attribute.Enum) { |
jjg@657 | 3724 | Name levelName = ((Attribute.Enum)value).value.name; |
jjg@657 | 3725 | if (levelName == names.SOURCE) vis = RetentionPolicy.SOURCE; |
jjg@657 | 3726 | else if (levelName == names.CLASS) vis = RetentionPolicy.CLASS; |
jjg@657 | 3727 | else if (levelName == names.RUNTIME) vis = RetentionPolicy.RUNTIME; |
jjg@657 | 3728 | else ;// /* fail soft */ throw new AssertionError(levelName); |
jjg@657 | 3729 | } |
jjg@657 | 3730 | } |
jjg@657 | 3731 | return vis; |
jjg@657 | 3732 | } |
jjg@657 | 3733 | // </editor-fold> |
duke@1 | 3734 | } |