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

Thu, 12 Oct 2017 19:50:01 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:50:01 +0800
changeset 2702
9ca8d8713094
parent 2601
8dcde670aed3
parent 2525
2eb010b6cb22
child 2893
ca5783d9a597
permissions
-rw-r--r--

merge

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

mercurial