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

Wed, 06 Feb 2013 14:03:39 +0000

author
mcimadamore
date
Wed, 06 Feb 2013 14:03:39 +0000
changeset 1550
1df20330f6bd
parent 1521
71f35e4b93a5
child 1563
bc456436c613
permissions
-rw-r--r--

8007463: Cleanup inference related classes
Summary: Make Infer.InferenceContext an inner class; adjust bound replacement logic in Type.UndetVar
Reviewed-by: jjg

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

mercurial