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

Fri, 15 Feb 2013 18:40:38 -0800

author
rfield
date
Fri, 15 Feb 2013 18:40:38 -0800
changeset 1587
f1f605f85850
parent 1582
3cd997b9fd84
child 1598
7ac9242d2ca6
permissions
-rw-r--r--

8004969: Generate $deserializeLambda$ method
8006763: super in method reference used in anonymous class - ClassFormatError is produced
8005632: Inner classes within lambdas cause build failures
8005653: Lambdas containing inner classes referencing external type variables do not correctly parameterize the inner classes
Reviewed-by: mcimadamore

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

mercurial