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

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

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) {
aoqi@0 1308 case UNBOUND: //similar to ? extends Object
aoqi@0 1309 case EXTENDS: {
aoqi@0 1310 Type bound = wildUpperBound(s);
aoqi@0 1311 undetvar.addBound(InferenceBound.UPPER, bound, this);
aoqi@0 1312 break;
aoqi@0 1313 }
aoqi@0 1314 case SUPER: {
aoqi@0 1315 Type bound = wildLowerBound(s);
aoqi@0 1316 undetvar.addBound(InferenceBound.LOWER, bound, this);
aoqi@0 1317 break;
aoqi@0 1318 }
aoqi@0 1319 }
aoqi@0 1320 return true;
aoqi@0 1321 } else {
aoqi@0 1322 return isSameType(t, s);
aoqi@0 1323 }
aoqi@0 1324 case ERROR:
aoqi@0 1325 return true;
aoqi@0 1326 default:
aoqi@0 1327 return containsType(s, t);
aoqi@0 1328 }
aoqi@0 1329 }
aoqi@0 1330
aoqi@0 1331 boolean containsType(List<Type> ts, List<Type> ss) {
aoqi@0 1332 while (ts.nonEmpty() && ss.nonEmpty()
aoqi@0 1333 && containsType(ts.head, ss.head)) {
aoqi@0 1334 ts = ts.tail;
aoqi@0 1335 ss = ss.tail;
aoqi@0 1336 }
aoqi@0 1337 return ts.isEmpty() && ss.isEmpty();
aoqi@0 1338 }
aoqi@0 1339
aoqi@0 1340 /**
aoqi@0 1341 * Check if t contains s.
aoqi@0 1342 *
aoqi@0 1343 * <p>T contains S if:
aoqi@0 1344 *
aoqi@0 1345 * <p>{@code L(T) <: L(S) && U(S) <: U(T)}
aoqi@0 1346 *
aoqi@0 1347 * <p>This relation is only used by ClassType.isSubtype(), that
aoqi@0 1348 * is,
aoqi@0 1349 *
aoqi@0 1350 * <p>{@code C<S> <: C<T> if T contains S.}
aoqi@0 1351 *
aoqi@0 1352 * <p>Because of F-bounds, this relation can lead to infinite
aoqi@0 1353 * recursion. Thus we must somehow break that recursion. Notice
aoqi@0 1354 * that containsType() is only called from ClassType.isSubtype().
aoqi@0 1355 * Since the arguments have already been checked against their
aoqi@0 1356 * bounds, we know:
aoqi@0 1357 *
aoqi@0 1358 * <p>{@code U(S) <: U(T) if T is "super" bound (U(T) *is* the bound)}
aoqi@0 1359 *
aoqi@0 1360 * <p>{@code L(T) <: L(S) if T is "extends" bound (L(T) is bottom)}
aoqi@0 1361 *
aoqi@0 1362 * @param t a type
aoqi@0 1363 * @param s a type
aoqi@0 1364 */
aoqi@0 1365 public boolean containsType(Type t, Type s) {
aoqi@0 1366 return containsType.visit(t, s);
aoqi@0 1367 }
aoqi@0 1368 // where
aoqi@0 1369 private TypeRelation containsType = new TypeRelation() {
aoqi@0 1370
aoqi@0 1371 public Boolean visitType(Type t, Type s) {
aoqi@0 1372 if (s.isPartial())
aoqi@0 1373 return containedBy(s, t);
aoqi@0 1374 else
aoqi@0 1375 return isSameType(t, s);
aoqi@0 1376 }
aoqi@0 1377
aoqi@0 1378 // void debugContainsType(WildcardType t, Type s) {
aoqi@0 1379 // System.err.println();
aoqi@0 1380 // System.err.format(" does %s contain %s?%n", t, s);
aoqi@0 1381 // System.err.format(" %s U(%s) <: U(%s) %s = %s%n",
aoqi@0 1382 // wildUpperBound(s), s, t, wildUpperBound(t),
aoqi@0 1383 // t.isSuperBound()
aoqi@0 1384 // || isSubtypeNoCapture(wildUpperBound(s), wildUpperBound(t)));
aoqi@0 1385 // System.err.format(" %s L(%s) <: L(%s) %s = %s%n",
aoqi@0 1386 // wildLowerBound(t), t, s, wildLowerBound(s),
aoqi@0 1387 // t.isExtendsBound()
aoqi@0 1388 // || isSubtypeNoCapture(wildLowerBound(t), wildLowerBound(s)));
aoqi@0 1389 // System.err.println();
aoqi@0 1390 // }
aoqi@0 1391
aoqi@0 1392 @Override
aoqi@0 1393 public Boolean visitWildcardType(WildcardType t, Type s) {
aoqi@0 1394 if (s.isPartial())
aoqi@0 1395 return containedBy(s, t);
aoqi@0 1396 else {
aoqi@0 1397 // debugContainsType(t, s);
aoqi@0 1398 return isSameWildcard(t, s)
aoqi@0 1399 || isCaptureOf(s, t)
aoqi@0 1400 || ((t.isExtendsBound() || isSubtypeNoCapture(wildLowerBound(t), cvarLowerBound(wildLowerBound(s)))) &&
aoqi@0 1401 // TODO: JDK-8039214, cvarUpperBound call here is incorrect
aoqi@0 1402 (t.isSuperBound() || isSubtypeNoCapture(cvarUpperBound(wildUpperBound(s)), wildUpperBound(t))));
aoqi@0 1403 }
aoqi@0 1404 }
aoqi@0 1405
aoqi@0 1406 @Override
aoqi@0 1407 public Boolean visitUndetVar(UndetVar t, Type s) {
aoqi@0 1408 if (!s.hasTag(WILDCARD)) {
aoqi@0 1409 return isSameType(t, s);
aoqi@0 1410 } else {
aoqi@0 1411 return false;
aoqi@0 1412 }
aoqi@0 1413 }
aoqi@0 1414
aoqi@0 1415 @Override
aoqi@0 1416 public Boolean visitErrorType(ErrorType t, Type s) {
aoqi@0 1417 return true;
aoqi@0 1418 }
aoqi@0 1419 };
aoqi@0 1420
aoqi@0 1421 public boolean isCaptureOf(Type s, WildcardType t) {
aoqi@0 1422 if (!s.hasTag(TYPEVAR) || !((TypeVar)s.unannotatedType()).isCaptured())
aoqi@0 1423 return false;
aoqi@0 1424 return isSameWildcard(t, ((CapturedType)s.unannotatedType()).wildcard);
aoqi@0 1425 }
aoqi@0 1426
aoqi@0 1427 public boolean isSameWildcard(WildcardType t, Type s) {
aoqi@0 1428 if (!s.hasTag(WILDCARD))
aoqi@0 1429 return false;
aoqi@0 1430 WildcardType w = (WildcardType)s.unannotatedType();
aoqi@0 1431 return w.kind == t.kind && w.type == t.type;
aoqi@0 1432 }
aoqi@0 1433
aoqi@0 1434 public boolean containsTypeEquivalent(List<Type> ts, List<Type> ss) {
aoqi@0 1435 while (ts.nonEmpty() && ss.nonEmpty()
aoqi@0 1436 && containsTypeEquivalent(ts.head, ss.head)) {
aoqi@0 1437 ts = ts.tail;
aoqi@0 1438 ss = ss.tail;
aoqi@0 1439 }
aoqi@0 1440 return ts.isEmpty() && ss.isEmpty();
aoqi@0 1441 }
aoqi@0 1442 // </editor-fold>
aoqi@0 1443
aoqi@0 1444 /**
aoqi@0 1445 * Can t and s be compared for equality? Any primitive ==
aoqi@0 1446 * primitive or primitive == object comparisons here are an error.
aoqi@0 1447 * Unboxing and correct primitive == primitive comparisons are
aoqi@0 1448 * already dealt with in Attr.visitBinary.
aoqi@0 1449 *
aoqi@0 1450 */
aoqi@0 1451 public boolean isEqualityComparable(Type s, Type t, Warner warn) {
aoqi@0 1452 if (t.isNumeric() && s.isNumeric())
aoqi@0 1453 return true;
aoqi@0 1454
aoqi@0 1455 boolean tPrimitive = t.isPrimitive();
aoqi@0 1456 boolean sPrimitive = s.isPrimitive();
aoqi@0 1457 if (!tPrimitive && !sPrimitive) {
aoqi@0 1458 return isCastable(s, t, warn) || isCastable(t, s, warn);
aoqi@0 1459 } else {
aoqi@0 1460 return false;
aoqi@0 1461 }
aoqi@0 1462 }
aoqi@0 1463
aoqi@0 1464 // <editor-fold defaultstate="collapsed" desc="isCastable">
aoqi@0 1465 public boolean isCastable(Type t, Type s) {
aoqi@0 1466 return isCastable(t, s, noWarnings);
aoqi@0 1467 }
aoqi@0 1468
aoqi@0 1469 /**
aoqi@0 1470 * Is t is castable to s?<br>
aoqi@0 1471 * s is assumed to be an erased type.<br>
aoqi@0 1472 * (not defined for Method and ForAll types).
aoqi@0 1473 */
aoqi@0 1474 public boolean isCastable(Type t, Type s, Warner warn) {
aoqi@0 1475 if (t == s)
aoqi@0 1476 return true;
aoqi@0 1477
aoqi@0 1478 if (t.isPrimitive() != s.isPrimitive())
aoqi@0 1479 return allowBoxing && (
aoqi@0 1480 isConvertible(t, s, warn)
aoqi@0 1481 || (allowObjectToPrimitiveCast &&
aoqi@0 1482 s.isPrimitive() &&
aoqi@0 1483 isSubtype(boxedClass(s).type, t)));
aoqi@0 1484 if (warn != warnStack.head) {
aoqi@0 1485 try {
aoqi@0 1486 warnStack = warnStack.prepend(warn);
aoqi@0 1487 checkUnsafeVarargsConversion(t, s, warn);
aoqi@0 1488 return isCastable.visit(t,s);
aoqi@0 1489 } finally {
aoqi@0 1490 warnStack = warnStack.tail;
aoqi@0 1491 }
aoqi@0 1492 } else {
aoqi@0 1493 return isCastable.visit(t,s);
aoqi@0 1494 }
aoqi@0 1495 }
aoqi@0 1496 // where
aoqi@0 1497 private TypeRelation isCastable = new TypeRelation() {
aoqi@0 1498
aoqi@0 1499 public Boolean visitType(Type t, Type s) {
aoqi@0 1500 if (s.hasTag(ERROR))
aoqi@0 1501 return true;
aoqi@0 1502
aoqi@0 1503 switch (t.getTag()) {
aoqi@0 1504 case BYTE: case CHAR: case SHORT: case INT: case LONG: case FLOAT:
aoqi@0 1505 case DOUBLE:
aoqi@0 1506 return s.isNumeric();
aoqi@0 1507 case BOOLEAN:
aoqi@0 1508 return s.hasTag(BOOLEAN);
aoqi@0 1509 case VOID:
aoqi@0 1510 return false;
aoqi@0 1511 case BOT:
aoqi@0 1512 return isSubtype(t, s);
aoqi@0 1513 default:
aoqi@0 1514 throw new AssertionError();
aoqi@0 1515 }
aoqi@0 1516 }
aoqi@0 1517
aoqi@0 1518 @Override
aoqi@0 1519 public Boolean visitWildcardType(WildcardType t, Type s) {
aoqi@0 1520 return isCastable(wildUpperBound(t), s, warnStack.head);
aoqi@0 1521 }
aoqi@0 1522
aoqi@0 1523 @Override
aoqi@0 1524 public Boolean visitClassType(ClassType t, Type s) {
aoqi@0 1525 if (s.hasTag(ERROR) || s.hasTag(BOT))
aoqi@0 1526 return true;
aoqi@0 1527
aoqi@0 1528 if (s.hasTag(TYPEVAR)) {
aoqi@0 1529 if (isCastable(t, s.getUpperBound(), noWarnings)) {
aoqi@0 1530 warnStack.head.warn(LintCategory.UNCHECKED);
aoqi@0 1531 return true;
aoqi@0 1532 } else {
aoqi@0 1533 return false;
aoqi@0 1534 }
aoqi@0 1535 }
aoqi@0 1536
aoqi@0 1537 if (t.isCompound() || s.isCompound()) {
aoqi@0 1538 return !t.isCompound() ?
aoqi@0 1539 visitIntersectionType((IntersectionClassType)s.unannotatedType(), t, true) :
aoqi@0 1540 visitIntersectionType((IntersectionClassType)t.unannotatedType(), s, false);
aoqi@0 1541 }
aoqi@0 1542
aoqi@0 1543 if (s.hasTag(CLASS) || s.hasTag(ARRAY)) {
aoqi@0 1544 boolean upcast;
aoqi@0 1545 if ((upcast = isSubtype(erasure(t), erasure(s)))
aoqi@0 1546 || isSubtype(erasure(s), erasure(t))) {
aoqi@0 1547 if (!upcast && s.hasTag(ARRAY)) {
aoqi@0 1548 if (!isReifiable(s))
aoqi@0 1549 warnStack.head.warn(LintCategory.UNCHECKED);
aoqi@0 1550 return true;
aoqi@0 1551 } else if (s.isRaw()) {
aoqi@0 1552 return true;
aoqi@0 1553 } else if (t.isRaw()) {
aoqi@0 1554 if (!isUnbounded(s))
aoqi@0 1555 warnStack.head.warn(LintCategory.UNCHECKED);
aoqi@0 1556 return true;
aoqi@0 1557 }
aoqi@0 1558 // Assume |a| <: |b|
aoqi@0 1559 final Type a = upcast ? t : s;
aoqi@0 1560 final Type b = upcast ? s : t;
aoqi@0 1561 final boolean HIGH = true;
aoqi@0 1562 final boolean LOW = false;
aoqi@0 1563 final boolean DONT_REWRITE_TYPEVARS = false;
aoqi@0 1564 Type aHigh = rewriteQuantifiers(a, HIGH, DONT_REWRITE_TYPEVARS);
aoqi@0 1565 Type aLow = rewriteQuantifiers(a, LOW, DONT_REWRITE_TYPEVARS);
aoqi@0 1566 Type bHigh = rewriteQuantifiers(b, HIGH, DONT_REWRITE_TYPEVARS);
aoqi@0 1567 Type bLow = rewriteQuantifiers(b, LOW, DONT_REWRITE_TYPEVARS);
aoqi@0 1568 Type lowSub = asSub(bLow, aLow.tsym);
aoqi@0 1569 Type highSub = (lowSub == null) ? null : asSub(bHigh, aHigh.tsym);
aoqi@0 1570 if (highSub == null) {
aoqi@0 1571 final boolean REWRITE_TYPEVARS = true;
aoqi@0 1572 aHigh = rewriteQuantifiers(a, HIGH, REWRITE_TYPEVARS);
aoqi@0 1573 aLow = rewriteQuantifiers(a, LOW, REWRITE_TYPEVARS);
aoqi@0 1574 bHigh = rewriteQuantifiers(b, HIGH, REWRITE_TYPEVARS);
aoqi@0 1575 bLow = rewriteQuantifiers(b, LOW, REWRITE_TYPEVARS);
aoqi@0 1576 lowSub = asSub(bLow, aLow.tsym);
aoqi@0 1577 highSub = (lowSub == null) ? null : asSub(bHigh, aHigh.tsym);
aoqi@0 1578 }
aoqi@0 1579 if (highSub != null) {
aoqi@0 1580 if (!(a.tsym == highSub.tsym && a.tsym == lowSub.tsym)) {
aoqi@0 1581 Assert.error(a.tsym + " != " + highSub.tsym + " != " + lowSub.tsym);
aoqi@0 1582 }
aoqi@0 1583 if (!disjointTypes(aHigh.allparams(), highSub.allparams())
aoqi@0 1584 && !disjointTypes(aHigh.allparams(), lowSub.allparams())
aoqi@0 1585 && !disjointTypes(aLow.allparams(), highSub.allparams())
aoqi@0 1586 && !disjointTypes(aLow.allparams(), lowSub.allparams())) {
aoqi@0 1587 if (upcast ? giveWarning(a, b) :
aoqi@0 1588 giveWarning(b, a))
aoqi@0 1589 warnStack.head.warn(LintCategory.UNCHECKED);
aoqi@0 1590 return true;
aoqi@0 1591 }
aoqi@0 1592 }
aoqi@0 1593 if (isReifiable(s))
aoqi@0 1594 return isSubtypeUnchecked(a, b);
aoqi@0 1595 else
aoqi@0 1596 return isSubtypeUnchecked(a, b, warnStack.head);
aoqi@0 1597 }
aoqi@0 1598
aoqi@0 1599 // Sidecast
aoqi@0 1600 if (s.hasTag(CLASS)) {
aoqi@0 1601 if ((s.tsym.flags() & INTERFACE) != 0) {
aoqi@0 1602 return ((t.tsym.flags() & FINAL) == 0)
aoqi@0 1603 ? sideCast(t, s, warnStack.head)
aoqi@0 1604 : sideCastFinal(t, s, warnStack.head);
aoqi@0 1605 } else if ((t.tsym.flags() & INTERFACE) != 0) {
aoqi@0 1606 return ((s.tsym.flags() & FINAL) == 0)
aoqi@0 1607 ? sideCast(t, s, warnStack.head)
aoqi@0 1608 : sideCastFinal(t, s, warnStack.head);
aoqi@0 1609 } else {
aoqi@0 1610 // unrelated class types
aoqi@0 1611 return false;
aoqi@0 1612 }
aoqi@0 1613 }
aoqi@0 1614 }
aoqi@0 1615 return false;
aoqi@0 1616 }
aoqi@0 1617
aoqi@0 1618 boolean visitIntersectionType(IntersectionClassType ict, Type s, boolean reverse) {
aoqi@0 1619 Warner warn = noWarnings;
aoqi@0 1620 for (Type c : ict.getComponents()) {
aoqi@0 1621 warn.clear();
aoqi@0 1622 if (reverse ? !isCastable(s, c, warn) : !isCastable(c, s, warn))
aoqi@0 1623 return false;
aoqi@0 1624 }
aoqi@0 1625 if (warn.hasLint(LintCategory.UNCHECKED))
aoqi@0 1626 warnStack.head.warn(LintCategory.UNCHECKED);
aoqi@0 1627 return true;
aoqi@0 1628 }
aoqi@0 1629
aoqi@0 1630 @Override
aoqi@0 1631 public Boolean visitArrayType(ArrayType t, Type s) {
aoqi@0 1632 switch (s.getTag()) {
aoqi@0 1633 case ERROR:
aoqi@0 1634 case BOT:
aoqi@0 1635 return true;
aoqi@0 1636 case TYPEVAR:
aoqi@0 1637 if (isCastable(s, t, noWarnings)) {
aoqi@0 1638 warnStack.head.warn(LintCategory.UNCHECKED);
aoqi@0 1639 return true;
aoqi@0 1640 } else {
aoqi@0 1641 return false;
aoqi@0 1642 }
aoqi@0 1643 case CLASS:
aoqi@0 1644 return isSubtype(t, s);
aoqi@0 1645 case ARRAY:
aoqi@0 1646 if (elemtype(t).isPrimitive() || elemtype(s).isPrimitive()) {
aoqi@0 1647 return elemtype(t).hasTag(elemtype(s).getTag());
aoqi@0 1648 } else {
aoqi@0 1649 return visit(elemtype(t), elemtype(s));
aoqi@0 1650 }
aoqi@0 1651 default:
aoqi@0 1652 return false;
aoqi@0 1653 }
aoqi@0 1654 }
aoqi@0 1655
aoqi@0 1656 @Override
aoqi@0 1657 public Boolean visitTypeVar(TypeVar t, Type s) {
aoqi@0 1658 switch (s.getTag()) {
aoqi@0 1659 case ERROR:
aoqi@0 1660 case BOT:
aoqi@0 1661 return true;
aoqi@0 1662 case TYPEVAR:
aoqi@0 1663 if (isSubtype(t, s)) {
aoqi@0 1664 return true;
aoqi@0 1665 } else if (isCastable(t.bound, s, noWarnings)) {
aoqi@0 1666 warnStack.head.warn(LintCategory.UNCHECKED);
aoqi@0 1667 return true;
aoqi@0 1668 } else {
aoqi@0 1669 return false;
aoqi@0 1670 }
aoqi@0 1671 default:
aoqi@0 1672 return isCastable(t.bound, s, warnStack.head);
aoqi@0 1673 }
aoqi@0 1674 }
aoqi@0 1675
aoqi@0 1676 @Override
aoqi@0 1677 public Boolean visitErrorType(ErrorType t, Type s) {
aoqi@0 1678 return true;
aoqi@0 1679 }
aoqi@0 1680 };
aoqi@0 1681 // </editor-fold>
aoqi@0 1682
aoqi@0 1683 // <editor-fold defaultstate="collapsed" desc="disjointTypes">
aoqi@0 1684 public boolean disjointTypes(List<Type> ts, List<Type> ss) {
aoqi@0 1685 while (ts.tail != null && ss.tail != null) {
aoqi@0 1686 if (disjointType(ts.head, ss.head)) return true;
aoqi@0 1687 ts = ts.tail;
aoqi@0 1688 ss = ss.tail;
aoqi@0 1689 }
aoqi@0 1690 return false;
aoqi@0 1691 }
aoqi@0 1692
aoqi@0 1693 /**
aoqi@0 1694 * Two types or wildcards are considered disjoint if it can be
aoqi@0 1695 * proven that no type can be contained in both. It is
aoqi@0 1696 * conservative in that it is allowed to say that two types are
aoqi@0 1697 * not disjoint, even though they actually are.
aoqi@0 1698 *
aoqi@0 1699 * The type {@code C<X>} is castable to {@code C<Y>} exactly if
aoqi@0 1700 * {@code X} and {@code Y} are not disjoint.
aoqi@0 1701 */
aoqi@0 1702 public boolean disjointType(Type t, Type s) {
aoqi@0 1703 return disjointType.visit(t, s);
aoqi@0 1704 }
aoqi@0 1705 // where
aoqi@0 1706 private TypeRelation disjointType = new TypeRelation() {
aoqi@0 1707
aoqi@0 1708 private Set<TypePair> cache = new HashSet<TypePair>();
aoqi@0 1709
aoqi@0 1710 @Override
aoqi@0 1711 public Boolean visitType(Type t, Type s) {
aoqi@0 1712 if (s.hasTag(WILDCARD))
aoqi@0 1713 return visit(s, t);
aoqi@0 1714 else
aoqi@0 1715 return notSoftSubtypeRecursive(t, s) || notSoftSubtypeRecursive(s, t);
aoqi@0 1716 }
aoqi@0 1717
aoqi@0 1718 private boolean isCastableRecursive(Type t, Type s) {
aoqi@0 1719 TypePair pair = new TypePair(t, s);
aoqi@0 1720 if (cache.add(pair)) {
aoqi@0 1721 try {
aoqi@0 1722 return Types.this.isCastable(t, s);
aoqi@0 1723 } finally {
aoqi@0 1724 cache.remove(pair);
aoqi@0 1725 }
aoqi@0 1726 } else {
aoqi@0 1727 return true;
aoqi@0 1728 }
aoqi@0 1729 }
aoqi@0 1730
aoqi@0 1731 private boolean notSoftSubtypeRecursive(Type t, Type s) {
aoqi@0 1732 TypePair pair = new TypePair(t, s);
aoqi@0 1733 if (cache.add(pair)) {
aoqi@0 1734 try {
aoqi@0 1735 return Types.this.notSoftSubtype(t, s);
aoqi@0 1736 } finally {
aoqi@0 1737 cache.remove(pair);
aoqi@0 1738 }
aoqi@0 1739 } else {
aoqi@0 1740 return false;
aoqi@0 1741 }
aoqi@0 1742 }
aoqi@0 1743
aoqi@0 1744 @Override
aoqi@0 1745 public Boolean visitWildcardType(WildcardType t, Type s) {
aoqi@0 1746 if (t.isUnbound())
aoqi@0 1747 return false;
aoqi@0 1748
aoqi@0 1749 if (!s.hasTag(WILDCARD)) {
aoqi@0 1750 if (t.isExtendsBound())
aoqi@0 1751 return notSoftSubtypeRecursive(s, t.type);
aoqi@0 1752 else
aoqi@0 1753 return notSoftSubtypeRecursive(t.type, s);
aoqi@0 1754 }
aoqi@0 1755
aoqi@0 1756 if (s.isUnbound())
aoqi@0 1757 return false;
aoqi@0 1758
aoqi@0 1759 if (t.isExtendsBound()) {
aoqi@0 1760 if (s.isExtendsBound())
aoqi@0 1761 return !isCastableRecursive(t.type, wildUpperBound(s));
aoqi@0 1762 else if (s.isSuperBound())
aoqi@0 1763 return notSoftSubtypeRecursive(wildLowerBound(s), t.type);
aoqi@0 1764 } else if (t.isSuperBound()) {
aoqi@0 1765 if (s.isExtendsBound())
aoqi@0 1766 return notSoftSubtypeRecursive(t.type, wildUpperBound(s));
aoqi@0 1767 }
aoqi@0 1768 return false;
aoqi@0 1769 }
aoqi@0 1770 };
aoqi@0 1771 // </editor-fold>
aoqi@0 1772
aoqi@0 1773 // <editor-fold defaultstate="collapsed" desc="cvarLowerBounds">
aoqi@0 1774 public List<Type> cvarLowerBounds(List<Type> ts) {
aoqi@0 1775 return map(ts, cvarLowerBoundMapping);
aoqi@0 1776 }
aoqi@0 1777 private final Mapping cvarLowerBoundMapping = new Mapping("cvarLowerBound") {
aoqi@0 1778 public Type apply(Type t) {
aoqi@0 1779 return cvarLowerBound(t);
aoqi@0 1780 }
aoqi@0 1781 };
aoqi@0 1782 // </editor-fold>
aoqi@0 1783
aoqi@0 1784 // <editor-fold defaultstate="collapsed" desc="notSoftSubtype">
aoqi@0 1785 /**
aoqi@0 1786 * This relation answers the question: is impossible that
aoqi@0 1787 * something of type `t' can be a subtype of `s'? This is
aoqi@0 1788 * different from the question "is `t' not a subtype of `s'?"
aoqi@0 1789 * when type variables are involved: Integer is not a subtype of T
aoqi@0 1790 * where {@code <T extends Number>} but it is not true that Integer cannot
aoqi@0 1791 * possibly be a subtype of T.
aoqi@0 1792 */
aoqi@0 1793 public boolean notSoftSubtype(Type t, Type s) {
aoqi@0 1794 if (t == s) return false;
aoqi@0 1795 if (t.hasTag(TYPEVAR)) {
aoqi@0 1796 TypeVar tv = (TypeVar) t;
aoqi@0 1797 return !isCastable(tv.bound,
aoqi@0 1798 relaxBound(s),
aoqi@0 1799 noWarnings);
aoqi@0 1800 }
aoqi@0 1801 if (!s.hasTag(WILDCARD))
aoqi@0 1802 s = cvarUpperBound(s);
aoqi@0 1803
aoqi@0 1804 return !isSubtype(t, relaxBound(s));
aoqi@0 1805 }
aoqi@0 1806
aoqi@0 1807 private Type relaxBound(Type t) {
aoqi@0 1808 if (t.hasTag(TYPEVAR)) {
aoqi@0 1809 while (t.hasTag(TYPEVAR))
aoqi@0 1810 t = t.getUpperBound();
aoqi@0 1811 t = rewriteQuantifiers(t, true, true);
aoqi@0 1812 }
aoqi@0 1813 return t;
aoqi@0 1814 }
aoqi@0 1815 // </editor-fold>
aoqi@0 1816
aoqi@0 1817 // <editor-fold defaultstate="collapsed" desc="isReifiable">
aoqi@0 1818 public boolean isReifiable(Type t) {
aoqi@0 1819 return isReifiable.visit(t);
aoqi@0 1820 }
aoqi@0 1821 // where
aoqi@0 1822 private UnaryVisitor<Boolean> isReifiable = new UnaryVisitor<Boolean>() {
aoqi@0 1823
aoqi@0 1824 public Boolean visitType(Type t, Void ignored) {
aoqi@0 1825 return true;
aoqi@0 1826 }
aoqi@0 1827
aoqi@0 1828 @Override
aoqi@0 1829 public Boolean visitClassType(ClassType t, Void ignored) {
aoqi@0 1830 if (t.isCompound())
aoqi@0 1831 return false;
aoqi@0 1832 else {
aoqi@0 1833 if (!t.isParameterized())
aoqi@0 1834 return true;
aoqi@0 1835
aoqi@0 1836 for (Type param : t.allparams()) {
aoqi@0 1837 if (!param.isUnbound())
aoqi@0 1838 return false;
aoqi@0 1839 }
aoqi@0 1840 return true;
aoqi@0 1841 }
aoqi@0 1842 }
aoqi@0 1843
aoqi@0 1844 @Override
aoqi@0 1845 public Boolean visitArrayType(ArrayType t, Void ignored) {
aoqi@0 1846 return visit(t.elemtype);
aoqi@0 1847 }
aoqi@0 1848
aoqi@0 1849 @Override
aoqi@0 1850 public Boolean visitTypeVar(TypeVar t, Void ignored) {
aoqi@0 1851 return false;
aoqi@0 1852 }
aoqi@0 1853 };
aoqi@0 1854 // </editor-fold>
aoqi@0 1855
aoqi@0 1856 // <editor-fold defaultstate="collapsed" desc="Array Utils">
aoqi@0 1857 public boolean isArray(Type t) {
aoqi@0 1858 while (t.hasTag(WILDCARD))
aoqi@0 1859 t = wildUpperBound(t);
aoqi@0 1860 return t.hasTag(ARRAY);
aoqi@0 1861 }
aoqi@0 1862
aoqi@0 1863 /**
aoqi@0 1864 * The element type of an array.
aoqi@0 1865 */
aoqi@0 1866 public Type elemtype(Type t) {
aoqi@0 1867 switch (t.getTag()) {
aoqi@0 1868 case WILDCARD:
aoqi@0 1869 return elemtype(wildUpperBound(t));
aoqi@0 1870 case ARRAY:
aoqi@0 1871 t = t.unannotatedType();
aoqi@0 1872 return ((ArrayType)t).elemtype;
aoqi@0 1873 case FORALL:
aoqi@0 1874 return elemtype(((ForAll)t).qtype);
aoqi@0 1875 case ERROR:
aoqi@0 1876 return t;
aoqi@0 1877 default:
aoqi@0 1878 return null;
aoqi@0 1879 }
aoqi@0 1880 }
aoqi@0 1881
aoqi@0 1882 public Type elemtypeOrType(Type t) {
aoqi@0 1883 Type elemtype = elemtype(t);
aoqi@0 1884 return elemtype != null ?
aoqi@0 1885 elemtype :
aoqi@0 1886 t;
aoqi@0 1887 }
aoqi@0 1888
aoqi@0 1889 /**
aoqi@0 1890 * Mapping to take element type of an arraytype
aoqi@0 1891 */
aoqi@0 1892 private Mapping elemTypeFun = new Mapping ("elemTypeFun") {
aoqi@0 1893 public Type apply(Type t) { return elemtype(t); }
aoqi@0 1894 };
aoqi@0 1895
aoqi@0 1896 /**
aoqi@0 1897 * The number of dimensions of an array type.
aoqi@0 1898 */
aoqi@0 1899 public int dimensions(Type t) {
aoqi@0 1900 int result = 0;
aoqi@0 1901 while (t.hasTag(ARRAY)) {
aoqi@0 1902 result++;
aoqi@0 1903 t = elemtype(t);
aoqi@0 1904 }
aoqi@0 1905 return result;
aoqi@0 1906 }
aoqi@0 1907
aoqi@0 1908 /**
aoqi@0 1909 * Returns an ArrayType with the component type t
aoqi@0 1910 *
aoqi@0 1911 * @param t The component type of the ArrayType
aoqi@0 1912 * @return the ArrayType for the given component
aoqi@0 1913 */
aoqi@0 1914 public ArrayType makeArrayType(Type t) {
aoqi@0 1915 if (t.hasTag(VOID) || t.hasTag(PACKAGE)) {
aoqi@0 1916 Assert.error("Type t must not be a VOID or PACKAGE type, " + t.toString());
aoqi@0 1917 }
aoqi@0 1918 return new ArrayType(t, syms.arrayClass);
aoqi@0 1919 }
aoqi@0 1920 // </editor-fold>
aoqi@0 1921
aoqi@0 1922 // <editor-fold defaultstate="collapsed" desc="asSuper">
aoqi@0 1923 /**
aoqi@0 1924 * Return the (most specific) base type of t that starts with the
aoqi@0 1925 * given symbol. If none exists, return null.
aoqi@0 1926 *
aoqi@0 1927 * @param t a type
aoqi@0 1928 * @param sym a symbol
aoqi@0 1929 */
aoqi@0 1930 public Type asSuper(Type t, Symbol sym) {
aoqi@0 1931 /* Some examples:
aoqi@0 1932 *
aoqi@0 1933 * (Enum<E>, Comparable) => Comparable<E>
aoqi@0 1934 * (c.s.s.d.AttributeTree.ValueKind, Enum) => Enum<c.s.s.d.AttributeTree.ValueKind>
aoqi@0 1935 * (c.s.s.t.ExpressionTree, c.s.s.t.Tree) => c.s.s.t.Tree
aoqi@0 1936 * (j.u.List<capture#160 of ? extends c.s.s.d.DocTree>, Iterable) =>
aoqi@0 1937 * Iterable<capture#160 of ? extends c.s.s.d.DocTree>
aoqi@0 1938 */
aoqi@0 1939 if (sym.type == syms.objectType) { //optimization
aoqi@0 1940 return syms.objectType;
aoqi@0 1941 }
aoqi@0 1942 return asSuper.visit(t, sym);
aoqi@0 1943 }
aoqi@0 1944 // where
aoqi@0 1945 private SimpleVisitor<Type,Symbol> asSuper = new SimpleVisitor<Type,Symbol>() {
aoqi@0 1946
aoqi@0 1947 public Type visitType(Type t, Symbol sym) {
aoqi@0 1948 return null;
aoqi@0 1949 }
aoqi@0 1950
aoqi@0 1951 @Override
aoqi@0 1952 public Type visitClassType(ClassType t, Symbol sym) {
aoqi@0 1953 if (t.tsym == sym)
aoqi@0 1954 return t;
aoqi@0 1955
aoqi@0 1956 Type st = supertype(t);
aoqi@0 1957 if (st.hasTag(CLASS) || st.hasTag(TYPEVAR)) {
aoqi@0 1958 Type x = asSuper(st, sym);
aoqi@0 1959 if (x != null)
aoqi@0 1960 return x;
aoqi@0 1961 }
aoqi@0 1962 if ((sym.flags() & INTERFACE) != 0) {
aoqi@0 1963 for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail) {
aoqi@0 1964 if (!l.head.hasTag(ERROR)) {
aoqi@0 1965 Type x = asSuper(l.head, sym);
aoqi@0 1966 if (x != null)
aoqi@0 1967 return x;
aoqi@0 1968 }
aoqi@0 1969 }
aoqi@0 1970 }
aoqi@0 1971 return null;
aoqi@0 1972 }
aoqi@0 1973
aoqi@0 1974 @Override
aoqi@0 1975 public Type visitArrayType(ArrayType t, Symbol sym) {
aoqi@0 1976 return isSubtype(t, sym.type) ? sym.type : null;
aoqi@0 1977 }
aoqi@0 1978
aoqi@0 1979 @Override
aoqi@0 1980 public Type visitTypeVar(TypeVar t, Symbol sym) {
aoqi@0 1981 if (t.tsym == sym)
aoqi@0 1982 return t;
aoqi@0 1983 else
aoqi@0 1984 return asSuper(t.bound, sym);
aoqi@0 1985 }
aoqi@0 1986
aoqi@0 1987 @Override
aoqi@0 1988 public Type visitErrorType(ErrorType t, Symbol sym) {
aoqi@0 1989 return t;
aoqi@0 1990 }
aoqi@0 1991 };
aoqi@0 1992
aoqi@0 1993 /**
aoqi@0 1994 * Return the base type of t or any of its outer types that starts
aoqi@0 1995 * with the given symbol. If none exists, return null.
aoqi@0 1996 *
aoqi@0 1997 * @param t a type
aoqi@0 1998 * @param sym a symbol
aoqi@0 1999 */
aoqi@0 2000 public Type asOuterSuper(Type t, Symbol sym) {
aoqi@0 2001 switch (t.getTag()) {
aoqi@0 2002 case CLASS:
aoqi@0 2003 do {
aoqi@0 2004 Type s = asSuper(t, sym);
aoqi@0 2005 if (s != null) return s;
aoqi@0 2006 t = t.getEnclosingType();
aoqi@0 2007 } while (t.hasTag(CLASS));
aoqi@0 2008 return null;
aoqi@0 2009 case ARRAY:
aoqi@0 2010 return isSubtype(t, sym.type) ? sym.type : null;
aoqi@0 2011 case TYPEVAR:
aoqi@0 2012 return asSuper(t, sym);
aoqi@0 2013 case ERROR:
aoqi@0 2014 return t;
aoqi@0 2015 default:
aoqi@0 2016 return null;
aoqi@0 2017 }
aoqi@0 2018 }
aoqi@0 2019
aoqi@0 2020 /**
aoqi@0 2021 * Return the base type of t or any of its enclosing types that
aoqi@0 2022 * starts with the given symbol. If none exists, return null.
aoqi@0 2023 *
aoqi@0 2024 * @param t a type
aoqi@0 2025 * @param sym a symbol
aoqi@0 2026 */
aoqi@0 2027 public Type asEnclosingSuper(Type t, Symbol sym) {
aoqi@0 2028 switch (t.getTag()) {
aoqi@0 2029 case CLASS:
aoqi@0 2030 do {
aoqi@0 2031 Type s = asSuper(t, sym);
aoqi@0 2032 if (s != null) return s;
aoqi@0 2033 Type outer = t.getEnclosingType();
aoqi@0 2034 t = (outer.hasTag(CLASS)) ? outer :
aoqi@0 2035 (t.tsym.owner.enclClass() != null) ? t.tsym.owner.enclClass().type :
aoqi@0 2036 Type.noType;
aoqi@0 2037 } while (t.hasTag(CLASS));
aoqi@0 2038 return null;
aoqi@0 2039 case ARRAY:
aoqi@0 2040 return isSubtype(t, sym.type) ? sym.type : null;
aoqi@0 2041 case TYPEVAR:
aoqi@0 2042 return asSuper(t, sym);
aoqi@0 2043 case ERROR:
aoqi@0 2044 return t;
aoqi@0 2045 default:
aoqi@0 2046 return null;
aoqi@0 2047 }
aoqi@0 2048 }
aoqi@0 2049 // </editor-fold>
aoqi@0 2050
aoqi@0 2051 // <editor-fold defaultstate="collapsed" desc="memberType">
aoqi@0 2052 /**
aoqi@0 2053 * The type of given symbol, seen as a member of t.
aoqi@0 2054 *
aoqi@0 2055 * @param t a type
aoqi@0 2056 * @param sym a symbol
aoqi@0 2057 */
aoqi@0 2058 public Type memberType(Type t, Symbol sym) {
aoqi@0 2059 return (sym.flags() & STATIC) != 0
aoqi@0 2060 ? sym.type
aoqi@0 2061 : memberType.visit(t, sym);
aoqi@0 2062 }
aoqi@0 2063 // where
aoqi@0 2064 private SimpleVisitor<Type,Symbol> memberType = new SimpleVisitor<Type,Symbol>() {
aoqi@0 2065
aoqi@0 2066 public Type visitType(Type t, Symbol sym) {
aoqi@0 2067 return sym.type;
aoqi@0 2068 }
aoqi@0 2069
aoqi@0 2070 @Override
aoqi@0 2071 public Type visitWildcardType(WildcardType t, Symbol sym) {
aoqi@0 2072 return memberType(wildUpperBound(t), sym);
aoqi@0 2073 }
aoqi@0 2074
aoqi@0 2075 @Override
aoqi@0 2076 public Type visitClassType(ClassType t, Symbol sym) {
aoqi@0 2077 Symbol owner = sym.owner;
aoqi@0 2078 long flags = sym.flags();
aoqi@0 2079 if (((flags & STATIC) == 0) && owner.type.isParameterized()) {
aoqi@0 2080 Type base = asOuterSuper(t, owner);
aoqi@0 2081 //if t is an intersection type T = CT & I1 & I2 ... & In
aoqi@0 2082 //its supertypes CT, I1, ... In might contain wildcards
aoqi@0 2083 //so we need to go through capture conversion
aoqi@0 2084 base = t.isCompound() ? capture(base) : base;
aoqi@0 2085 if (base != null) {
aoqi@0 2086 List<Type> ownerParams = owner.type.allparams();
aoqi@0 2087 List<Type> baseParams = base.allparams();
aoqi@0 2088 if (ownerParams.nonEmpty()) {
aoqi@0 2089 if (baseParams.isEmpty()) {
aoqi@0 2090 // then base is a raw type
aoqi@0 2091 return erasure(sym.type);
aoqi@0 2092 } else {
aoqi@0 2093 return subst(sym.type, ownerParams, baseParams);
aoqi@0 2094 }
aoqi@0 2095 }
aoqi@0 2096 }
aoqi@0 2097 }
aoqi@0 2098 return sym.type;
aoqi@0 2099 }
aoqi@0 2100
aoqi@0 2101 @Override
aoqi@0 2102 public Type visitTypeVar(TypeVar t, Symbol sym) {
aoqi@0 2103 return memberType(t.bound, sym);
aoqi@0 2104 }
aoqi@0 2105
aoqi@0 2106 @Override
aoqi@0 2107 public Type visitErrorType(ErrorType t, Symbol sym) {
aoqi@0 2108 return t;
aoqi@0 2109 }
aoqi@0 2110 };
aoqi@0 2111 // </editor-fold>
aoqi@0 2112
aoqi@0 2113 // <editor-fold defaultstate="collapsed" desc="isAssignable">
aoqi@0 2114 public boolean isAssignable(Type t, Type s) {
aoqi@0 2115 return isAssignable(t, s, noWarnings);
aoqi@0 2116 }
aoqi@0 2117
aoqi@0 2118 /**
aoqi@0 2119 * Is t assignable to s?<br>
aoqi@0 2120 * Equivalent to subtype except for constant values and raw
aoqi@0 2121 * types.<br>
aoqi@0 2122 * (not defined for Method and ForAll types)
aoqi@0 2123 */
aoqi@0 2124 public boolean isAssignable(Type t, Type s, Warner warn) {
aoqi@0 2125 if (t.hasTag(ERROR))
aoqi@0 2126 return true;
aoqi@0 2127 if (t.getTag().isSubRangeOf(INT) && t.constValue() != null) {
aoqi@0 2128 int value = ((Number)t.constValue()).intValue();
aoqi@0 2129 switch (s.getTag()) {
aoqi@0 2130 case BYTE:
aoqi@0 2131 if (Byte.MIN_VALUE <= value && value <= Byte.MAX_VALUE)
aoqi@0 2132 return true;
aoqi@0 2133 break;
aoqi@0 2134 case CHAR:
aoqi@0 2135 if (Character.MIN_VALUE <= value && value <= Character.MAX_VALUE)
aoqi@0 2136 return true;
aoqi@0 2137 break;
aoqi@0 2138 case SHORT:
aoqi@0 2139 if (Short.MIN_VALUE <= value && value <= Short.MAX_VALUE)
aoqi@0 2140 return true;
aoqi@0 2141 break;
aoqi@0 2142 case INT:
aoqi@0 2143 return true;
aoqi@0 2144 case CLASS:
aoqi@0 2145 switch (unboxedType(s).getTag()) {
aoqi@0 2146 case BYTE:
aoqi@0 2147 case CHAR:
aoqi@0 2148 case SHORT:
aoqi@0 2149 return isAssignable(t, unboxedType(s), warn);
aoqi@0 2150 }
aoqi@0 2151 break;
aoqi@0 2152 }
aoqi@0 2153 }
aoqi@0 2154 return isConvertible(t, s, warn);
aoqi@0 2155 }
aoqi@0 2156 // </editor-fold>
aoqi@0 2157
aoqi@0 2158 // <editor-fold defaultstate="collapsed" desc="erasure">
aoqi@0 2159 /**
aoqi@0 2160 * The erasure of t {@code |t|} -- the type that results when all
aoqi@0 2161 * type parameters in t are deleted.
aoqi@0 2162 */
aoqi@0 2163 public Type erasure(Type t) {
aoqi@0 2164 return eraseNotNeeded(t)? t : erasure(t, false);
aoqi@0 2165 }
aoqi@0 2166 //where
aoqi@0 2167 private boolean eraseNotNeeded(Type t) {
aoqi@0 2168 // We don't want to erase primitive types and String type as that
aoqi@0 2169 // operation is idempotent. Also, erasing these could result in loss
aoqi@0 2170 // of information such as constant values attached to such types.
aoqi@0 2171 return (t.isPrimitive()) || (syms.stringType.tsym == t.tsym);
aoqi@0 2172 }
aoqi@0 2173
aoqi@0 2174 private Type erasure(Type t, boolean recurse) {
aoqi@0 2175 if (t.isPrimitive())
aoqi@0 2176 return t; /* fast special case */
aoqi@0 2177 else
aoqi@0 2178 return erasure.visit(t, recurse);
aoqi@0 2179 }
aoqi@0 2180 // where
aoqi@0 2181 private SimpleVisitor<Type, Boolean> erasure = new SimpleVisitor<Type, Boolean>() {
aoqi@0 2182 public Type visitType(Type t, Boolean recurse) {
aoqi@0 2183 if (t.isPrimitive())
aoqi@0 2184 return t; /*fast special case*/
aoqi@0 2185 else
aoqi@0 2186 return t.map(recurse ? erasureRecFun : erasureFun);
aoqi@0 2187 }
aoqi@0 2188
aoqi@0 2189 @Override
aoqi@0 2190 public Type visitWildcardType(WildcardType t, Boolean recurse) {
aoqi@0 2191 return erasure(wildUpperBound(t), recurse);
aoqi@0 2192 }
aoqi@0 2193
aoqi@0 2194 @Override
aoqi@0 2195 public Type visitClassType(ClassType t, Boolean recurse) {
aoqi@0 2196 Type erased = t.tsym.erasure(Types.this);
aoqi@0 2197 if (recurse) {
aoqi@0 2198 erased = new ErasedClassType(erased.getEnclosingType(),erased.tsym);
aoqi@0 2199 }
aoqi@0 2200 return erased;
aoqi@0 2201 }
aoqi@0 2202
aoqi@0 2203 @Override
aoqi@0 2204 public Type visitTypeVar(TypeVar t, Boolean recurse) {
aoqi@0 2205 return erasure(t.bound, recurse);
aoqi@0 2206 }
aoqi@0 2207
aoqi@0 2208 @Override
aoqi@0 2209 public Type visitErrorType(ErrorType t, Boolean recurse) {
aoqi@0 2210 return t;
aoqi@0 2211 }
aoqi@0 2212
aoqi@0 2213 @Override
aoqi@0 2214 public Type visitAnnotatedType(AnnotatedType t, Boolean recurse) {
aoqi@0 2215 Type erased = erasure(t.unannotatedType(), recurse);
aoqi@0 2216 if (erased.isAnnotated()) {
aoqi@0 2217 // This can only happen when the underlying type is a
aoqi@0 2218 // type variable and the upper bound of it is annotated.
aoqi@0 2219 // The annotation on the type variable overrides the one
aoqi@0 2220 // on the bound.
aoqi@0 2221 erased = ((AnnotatedType)erased).unannotatedType();
aoqi@0 2222 }
aoqi@0 2223 return erased.annotatedType(t.getAnnotationMirrors());
aoqi@0 2224 }
aoqi@0 2225 };
aoqi@0 2226
aoqi@0 2227 private Mapping erasureFun = new Mapping ("erasure") {
aoqi@0 2228 public Type apply(Type t) { return erasure(t); }
aoqi@0 2229 };
aoqi@0 2230
aoqi@0 2231 private Mapping erasureRecFun = new Mapping ("erasureRecursive") {
aoqi@0 2232 public Type apply(Type t) { return erasureRecursive(t); }
aoqi@0 2233 };
aoqi@0 2234
aoqi@0 2235 public List<Type> erasure(List<Type> ts) {
aoqi@0 2236 return Type.map(ts, erasureFun);
aoqi@0 2237 }
aoqi@0 2238
aoqi@0 2239 public Type erasureRecursive(Type t) {
aoqi@0 2240 return erasure(t, true);
aoqi@0 2241 }
aoqi@0 2242
aoqi@0 2243 public List<Type> erasureRecursive(List<Type> ts) {
aoqi@0 2244 return Type.map(ts, erasureRecFun);
aoqi@0 2245 }
aoqi@0 2246 // </editor-fold>
aoqi@0 2247
aoqi@0 2248 // <editor-fold defaultstate="collapsed" desc="makeCompoundType">
aoqi@0 2249 /**
aoqi@0 2250 * Make a compound type from non-empty list of types. The list should be
aoqi@0 2251 * ordered according to {@link Symbol#precedes(TypeSymbol,Types)}.
aoqi@0 2252 *
aoqi@0 2253 * @param bounds the types from which the compound type is formed
aoqi@0 2254 * @param supertype is objectType if all bounds are interfaces,
aoqi@0 2255 * null otherwise.
aoqi@0 2256 */
aoqi@0 2257 public Type makeCompoundType(List<Type> bounds) {
aoqi@0 2258 return makeCompoundType(bounds, bounds.head.tsym.isInterface());
aoqi@0 2259 }
aoqi@0 2260 public Type makeCompoundType(List<Type> bounds, boolean allInterfaces) {
aoqi@0 2261 Assert.check(bounds.nonEmpty());
aoqi@0 2262 Type firstExplicitBound = bounds.head;
aoqi@0 2263 if (allInterfaces) {
aoqi@0 2264 bounds = bounds.prepend(syms.objectType);
aoqi@0 2265 }
aoqi@0 2266 ClassSymbol bc =
aoqi@0 2267 new ClassSymbol(ABSTRACT|PUBLIC|SYNTHETIC|COMPOUND|ACYCLIC,
aoqi@0 2268 Type.moreInfo
aoqi@0 2269 ? names.fromString(bounds.toString())
aoqi@0 2270 : names.empty,
aoqi@0 2271 null,
aoqi@0 2272 syms.noSymbol);
aoqi@0 2273 bc.type = new IntersectionClassType(bounds, bc, allInterfaces);
aoqi@0 2274 bc.erasure_field = (bounds.head.hasTag(TYPEVAR)) ?
aoqi@0 2275 syms.objectType : // error condition, recover
aoqi@0 2276 erasure(firstExplicitBound);
aoqi@0 2277 bc.members_field = new Scope(bc);
aoqi@0 2278 return bc.type;
aoqi@0 2279 }
aoqi@0 2280
aoqi@0 2281 /**
aoqi@0 2282 * A convenience wrapper for {@link #makeCompoundType(List)}; the
aoqi@0 2283 * arguments are converted to a list and passed to the other
aoqi@0 2284 * method. Note that this might cause a symbol completion.
aoqi@0 2285 * Hence, this version of makeCompoundType may not be called
aoqi@0 2286 * during a classfile read.
aoqi@0 2287 */
aoqi@0 2288 public Type makeCompoundType(Type bound1, Type bound2) {
aoqi@0 2289 return makeCompoundType(List.of(bound1, bound2));
aoqi@0 2290 }
aoqi@0 2291 // </editor-fold>
aoqi@0 2292
aoqi@0 2293 // <editor-fold defaultstate="collapsed" desc="supertype">
aoqi@0 2294 public Type supertype(Type t) {
aoqi@0 2295 return supertype.visit(t);
aoqi@0 2296 }
aoqi@0 2297 // where
aoqi@0 2298 private UnaryVisitor<Type> supertype = new UnaryVisitor<Type>() {
aoqi@0 2299
aoqi@0 2300 public Type visitType(Type t, Void ignored) {
aoqi@0 2301 // A note on wildcards: there is no good way to
aoqi@0 2302 // determine a supertype for a super bounded wildcard.
aoqi@0 2303 return Type.noType;
aoqi@0 2304 }
aoqi@0 2305
aoqi@0 2306 @Override
aoqi@0 2307 public Type visitClassType(ClassType t, Void ignored) {
aoqi@0 2308 if (t.supertype_field == null) {
aoqi@0 2309 Type supertype = ((ClassSymbol)t.tsym).getSuperclass();
aoqi@0 2310 // An interface has no superclass; its supertype is Object.
aoqi@0 2311 if (t.isInterface())
aoqi@0 2312 supertype = ((ClassType)t.tsym.type).supertype_field;
aoqi@0 2313 if (t.supertype_field == null) {
aoqi@0 2314 List<Type> actuals = classBound(t).allparams();
aoqi@0 2315 List<Type> formals = t.tsym.type.allparams();
aoqi@0 2316 if (t.hasErasedSupertypes()) {
aoqi@0 2317 t.supertype_field = erasureRecursive(supertype);
aoqi@0 2318 } else if (formals.nonEmpty()) {
aoqi@0 2319 t.supertype_field = subst(supertype, formals, actuals);
aoqi@0 2320 }
aoqi@0 2321 else {
aoqi@0 2322 t.supertype_field = supertype;
aoqi@0 2323 }
aoqi@0 2324 }
aoqi@0 2325 }
aoqi@0 2326 return t.supertype_field;
aoqi@0 2327 }
aoqi@0 2328
aoqi@0 2329 /**
aoqi@0 2330 * The supertype is always a class type. If the type
aoqi@0 2331 * variable's bounds start with a class type, this is also
aoqi@0 2332 * the supertype. Otherwise, the supertype is
aoqi@0 2333 * java.lang.Object.
aoqi@0 2334 */
aoqi@0 2335 @Override
aoqi@0 2336 public Type visitTypeVar(TypeVar t, Void ignored) {
aoqi@0 2337 if (t.bound.hasTag(TYPEVAR) ||
aoqi@0 2338 (!t.bound.isCompound() && !t.bound.isInterface())) {
aoqi@0 2339 return t.bound;
aoqi@0 2340 } else {
aoqi@0 2341 return supertype(t.bound);
aoqi@0 2342 }
aoqi@0 2343 }
aoqi@0 2344
aoqi@0 2345 @Override
aoqi@0 2346 public Type visitArrayType(ArrayType t, Void ignored) {
aoqi@0 2347 if (t.elemtype.isPrimitive() || isSameType(t.elemtype, syms.objectType))
aoqi@0 2348 return arraySuperType();
aoqi@0 2349 else
aoqi@0 2350 return new ArrayType(supertype(t.elemtype), t.tsym);
aoqi@0 2351 }
aoqi@0 2352
aoqi@0 2353 @Override
aoqi@0 2354 public Type visitErrorType(ErrorType t, Void ignored) {
aoqi@0 2355 return Type.noType;
aoqi@0 2356 }
aoqi@0 2357 };
aoqi@0 2358 // </editor-fold>
aoqi@0 2359
aoqi@0 2360 // <editor-fold defaultstate="collapsed" desc="interfaces">
aoqi@0 2361 /**
aoqi@0 2362 * Return the interfaces implemented by this class.
aoqi@0 2363 */
aoqi@0 2364 public List<Type> interfaces(Type t) {
aoqi@0 2365 return interfaces.visit(t);
aoqi@0 2366 }
aoqi@0 2367 // where
aoqi@0 2368 private UnaryVisitor<List<Type>> interfaces = new UnaryVisitor<List<Type>>() {
aoqi@0 2369
aoqi@0 2370 public List<Type> visitType(Type t, Void ignored) {
aoqi@0 2371 return List.nil();
aoqi@0 2372 }
aoqi@0 2373
aoqi@0 2374 @Override
aoqi@0 2375 public List<Type> visitClassType(ClassType t, Void ignored) {
aoqi@0 2376 if (t.interfaces_field == null) {
aoqi@0 2377 List<Type> interfaces = ((ClassSymbol)t.tsym).getInterfaces();
aoqi@0 2378 if (t.interfaces_field == null) {
aoqi@0 2379 // If t.interfaces_field is null, then t must
aoqi@0 2380 // be a parameterized type (not to be confused
aoqi@0 2381 // with a generic type declaration).
aoqi@0 2382 // Terminology:
aoqi@0 2383 // Parameterized type: List<String>
aoqi@0 2384 // Generic type declaration: class List<E> { ... }
aoqi@0 2385 // So t corresponds to List<String> and
aoqi@0 2386 // t.tsym.type corresponds to List<E>.
aoqi@0 2387 // The reason t must be parameterized type is
aoqi@0 2388 // that completion will happen as a side
aoqi@0 2389 // effect of calling
aoqi@0 2390 // ClassSymbol.getInterfaces. Since
aoqi@0 2391 // t.interfaces_field is null after
aoqi@0 2392 // completion, we can assume that t is not the
aoqi@0 2393 // type of a class/interface declaration.
aoqi@0 2394 Assert.check(t != t.tsym.type, t);
aoqi@0 2395 List<Type> actuals = t.allparams();
aoqi@0 2396 List<Type> formals = t.tsym.type.allparams();
aoqi@0 2397 if (t.hasErasedSupertypes()) {
aoqi@0 2398 t.interfaces_field = erasureRecursive(interfaces);
aoqi@0 2399 } else if (formals.nonEmpty()) {
aoqi@0 2400 t.interfaces_field = subst(interfaces, formals, actuals);
aoqi@0 2401 }
aoqi@0 2402 else {
aoqi@0 2403 t.interfaces_field = interfaces;
aoqi@0 2404 }
aoqi@0 2405 }
aoqi@0 2406 }
aoqi@0 2407 return t.interfaces_field;
aoqi@0 2408 }
aoqi@0 2409
aoqi@0 2410 @Override
aoqi@0 2411 public List<Type> visitTypeVar(TypeVar t, Void ignored) {
aoqi@0 2412 if (t.bound.isCompound())
aoqi@0 2413 return interfaces(t.bound);
aoqi@0 2414
aoqi@0 2415 if (t.bound.isInterface())
aoqi@0 2416 return List.of(t.bound);
aoqi@0 2417
aoqi@0 2418 return List.nil();
aoqi@0 2419 }
aoqi@0 2420 };
aoqi@0 2421
aoqi@0 2422 public List<Type> directSupertypes(Type t) {
aoqi@0 2423 return directSupertypes.visit(t);
aoqi@0 2424 }
aoqi@0 2425 // where
aoqi@0 2426 private final UnaryVisitor<List<Type>> directSupertypes = new UnaryVisitor<List<Type>>() {
aoqi@0 2427
aoqi@0 2428 public List<Type> visitType(final Type type, final Void ignored) {
aoqi@0 2429 if (!type.isCompound()) {
aoqi@0 2430 final Type sup = supertype(type);
aoqi@0 2431 return (sup == Type.noType || sup == type || sup == null)
aoqi@0 2432 ? interfaces(type)
aoqi@0 2433 : interfaces(type).prepend(sup);
aoqi@0 2434 } else {
aoqi@0 2435 return visitIntersectionType((IntersectionClassType) type);
aoqi@0 2436 }
aoqi@0 2437 }
aoqi@0 2438
aoqi@0 2439 private List<Type> visitIntersectionType(final IntersectionClassType it) {
aoqi@0 2440 return it.getExplicitComponents();
aoqi@0 2441 }
aoqi@0 2442
aoqi@0 2443 };
aoqi@0 2444
aoqi@0 2445 public boolean isDirectSuperInterface(TypeSymbol isym, TypeSymbol origin) {
aoqi@0 2446 for (Type i2 : interfaces(origin.type)) {
aoqi@0 2447 if (isym == i2.tsym) return true;
aoqi@0 2448 }
aoqi@0 2449 return false;
aoqi@0 2450 }
aoqi@0 2451 // </editor-fold>
aoqi@0 2452
aoqi@0 2453 // <editor-fold defaultstate="collapsed" desc="isDerivedRaw">
aoqi@0 2454 Map<Type,Boolean> isDerivedRawCache = new HashMap<Type,Boolean>();
aoqi@0 2455
aoqi@0 2456 public boolean isDerivedRaw(Type t) {
aoqi@0 2457 Boolean result = isDerivedRawCache.get(t);
aoqi@0 2458 if (result == null) {
aoqi@0 2459 result = isDerivedRawInternal(t);
aoqi@0 2460 isDerivedRawCache.put(t, result);
aoqi@0 2461 }
aoqi@0 2462 return result;
aoqi@0 2463 }
aoqi@0 2464
aoqi@0 2465 public boolean isDerivedRawInternal(Type t) {
aoqi@0 2466 if (t.isErroneous())
aoqi@0 2467 return false;
aoqi@0 2468 return
aoqi@0 2469 t.isRaw() ||
aoqi@0 2470 supertype(t) != Type.noType && isDerivedRaw(supertype(t)) ||
aoqi@0 2471 isDerivedRaw(interfaces(t));
aoqi@0 2472 }
aoqi@0 2473
aoqi@0 2474 public boolean isDerivedRaw(List<Type> ts) {
aoqi@0 2475 List<Type> l = ts;
aoqi@0 2476 while (l.nonEmpty() && !isDerivedRaw(l.head)) l = l.tail;
aoqi@0 2477 return l.nonEmpty();
aoqi@0 2478 }
aoqi@0 2479 // </editor-fold>
aoqi@0 2480
aoqi@0 2481 // <editor-fold defaultstate="collapsed" desc="setBounds">
aoqi@0 2482 /**
aoqi@0 2483 * Set the bounds field of the given type variable to reflect a
aoqi@0 2484 * (possibly multiple) list of bounds.
aoqi@0 2485 * @param t a type variable
aoqi@0 2486 * @param bounds the bounds, must be nonempty
aoqi@0 2487 * @param supertype is objectType if all bounds are interfaces,
aoqi@0 2488 * null otherwise.
aoqi@0 2489 */
aoqi@0 2490 public void setBounds(TypeVar t, List<Type> bounds) {
aoqi@0 2491 setBounds(t, bounds, bounds.head.tsym.isInterface());
aoqi@0 2492 }
aoqi@0 2493
aoqi@0 2494 /**
aoqi@0 2495 * Same as {@link #setBounds(Type.TypeVar,List,Type)}, except that
aoqi@0 2496 * third parameter is computed directly, as follows: if all
aoqi@0 2497 * all bounds are interface types, the computed supertype is Object,
aoqi@0 2498 * otherwise the supertype is simply left null (in this case, the supertype
aoqi@0 2499 * is assumed to be the head of the bound list passed as second argument).
aoqi@0 2500 * Note that this check might cause a symbol completion. Hence, this version of
aoqi@0 2501 * setBounds may not be called during a classfile read.
aoqi@0 2502 */
aoqi@0 2503 public void setBounds(TypeVar t, List<Type> bounds, boolean allInterfaces) {
aoqi@0 2504 t.bound = bounds.tail.isEmpty() ?
aoqi@0 2505 bounds.head :
aoqi@0 2506 makeCompoundType(bounds, allInterfaces);
aoqi@0 2507 t.rank_field = -1;
aoqi@0 2508 }
aoqi@0 2509 // </editor-fold>
aoqi@0 2510
aoqi@0 2511 // <editor-fold defaultstate="collapsed" desc="getBounds">
aoqi@0 2512 /**
aoqi@0 2513 * Return list of bounds of the given type variable.
aoqi@0 2514 */
aoqi@0 2515 public List<Type> getBounds(TypeVar t) {
aoqi@0 2516 if (t.bound.hasTag(NONE))
aoqi@0 2517 return List.nil();
aoqi@0 2518 else if (t.bound.isErroneous() || !t.bound.isCompound())
aoqi@0 2519 return List.of(t.bound);
aoqi@0 2520 else if ((erasure(t).tsym.flags() & INTERFACE) == 0)
aoqi@0 2521 return interfaces(t).prepend(supertype(t));
aoqi@0 2522 else
aoqi@0 2523 // No superclass was given in bounds.
aoqi@0 2524 // In this case, supertype is Object, erasure is first interface.
aoqi@0 2525 return interfaces(t);
aoqi@0 2526 }
aoqi@0 2527 // </editor-fold>
aoqi@0 2528
aoqi@0 2529 // <editor-fold defaultstate="collapsed" desc="classBound">
aoqi@0 2530 /**
aoqi@0 2531 * If the given type is a (possibly selected) type variable,
aoqi@0 2532 * return the bounding class of this type, otherwise return the
aoqi@0 2533 * type itself.
aoqi@0 2534 */
aoqi@0 2535 public Type classBound(Type t) {
aoqi@0 2536 return classBound.visit(t);
aoqi@0 2537 }
aoqi@0 2538 // where
aoqi@0 2539 private UnaryVisitor<Type> classBound = new UnaryVisitor<Type>() {
aoqi@0 2540
aoqi@0 2541 public Type visitType(Type t, Void ignored) {
aoqi@0 2542 return t;
aoqi@0 2543 }
aoqi@0 2544
aoqi@0 2545 @Override
aoqi@0 2546 public Type visitClassType(ClassType t, Void ignored) {
aoqi@0 2547 Type outer1 = classBound(t.getEnclosingType());
aoqi@0 2548 if (outer1 != t.getEnclosingType())
aoqi@0 2549 return new ClassType(outer1, t.getTypeArguments(), t.tsym);
aoqi@0 2550 else
aoqi@0 2551 return t;
aoqi@0 2552 }
aoqi@0 2553
aoqi@0 2554 @Override
aoqi@0 2555 public Type visitTypeVar(TypeVar t, Void ignored) {
aoqi@0 2556 return classBound(supertype(t));
aoqi@0 2557 }
aoqi@0 2558
aoqi@0 2559 @Override
aoqi@0 2560 public Type visitErrorType(ErrorType t, Void ignored) {
aoqi@0 2561 return t;
aoqi@0 2562 }
aoqi@0 2563 };
aoqi@0 2564 // </editor-fold>
aoqi@0 2565
aoqi@0 2566 // <editor-fold defaultstate="collapsed" desc="sub signature / override equivalence">
aoqi@0 2567 /**
aoqi@0 2568 * Returns true iff the first signature is a <em>sub
aoqi@0 2569 * signature</em> of the other. This is <b>not</b> an equivalence
aoqi@0 2570 * relation.
aoqi@0 2571 *
aoqi@0 2572 * @jls section 8.4.2.
aoqi@0 2573 * @see #overrideEquivalent(Type t, Type s)
aoqi@0 2574 * @param t first signature (possibly raw).
aoqi@0 2575 * @param s second signature (could be subjected to erasure).
aoqi@0 2576 * @return true if t is a sub signature of s.
aoqi@0 2577 */
aoqi@0 2578 public boolean isSubSignature(Type t, Type s) {
aoqi@0 2579 return isSubSignature(t, s, true);
aoqi@0 2580 }
aoqi@0 2581
aoqi@0 2582 public boolean isSubSignature(Type t, Type s, boolean strict) {
aoqi@0 2583 return hasSameArgs(t, s, strict) || hasSameArgs(t, erasure(s), strict);
aoqi@0 2584 }
aoqi@0 2585
aoqi@0 2586 /**
aoqi@0 2587 * Returns true iff these signatures are related by <em>override
aoqi@0 2588 * equivalence</em>. This is the natural extension of
aoqi@0 2589 * isSubSignature to an equivalence relation.
aoqi@0 2590 *
aoqi@0 2591 * @jls section 8.4.2.
aoqi@0 2592 * @see #isSubSignature(Type t, Type s)
aoqi@0 2593 * @param t a signature (possible raw, could be subjected to
aoqi@0 2594 * erasure).
aoqi@0 2595 * @param s a signature (possible raw, could be subjected to
aoqi@0 2596 * erasure).
aoqi@0 2597 * @return true if either argument is a sub signature of the other.
aoqi@0 2598 */
aoqi@0 2599 public boolean overrideEquivalent(Type t, Type s) {
aoqi@0 2600 return hasSameArgs(t, s) ||
aoqi@0 2601 hasSameArgs(t, erasure(s)) || hasSameArgs(erasure(t), s);
aoqi@0 2602 }
aoqi@0 2603
aoqi@0 2604 public boolean overridesObjectMethod(TypeSymbol origin, Symbol msym) {
aoqi@0 2605 for (Scope.Entry e = syms.objectType.tsym.members().lookup(msym.name) ; e.scope != null ; e = e.next()) {
aoqi@0 2606 if (msym.overrides(e.sym, origin, Types.this, true)) {
aoqi@0 2607 return true;
aoqi@0 2608 }
aoqi@0 2609 }
aoqi@0 2610 return false;
aoqi@0 2611 }
aoqi@0 2612
aoqi@0 2613 // <editor-fold defaultstate="collapsed" desc="Determining method implementation in given site">
aoqi@0 2614 class ImplementationCache {
aoqi@0 2615
aoqi@0 2616 private WeakHashMap<MethodSymbol, SoftReference<Map<TypeSymbol, Entry>>> _map =
aoqi@0 2617 new WeakHashMap<MethodSymbol, SoftReference<Map<TypeSymbol, Entry>>>();
aoqi@0 2618
aoqi@0 2619 class Entry {
aoqi@0 2620 final MethodSymbol cachedImpl;
aoqi@0 2621 final Filter<Symbol> implFilter;
aoqi@0 2622 final boolean checkResult;
aoqi@0 2623 final int prevMark;
aoqi@0 2624
aoqi@0 2625 public Entry(MethodSymbol cachedImpl,
aoqi@0 2626 Filter<Symbol> scopeFilter,
aoqi@0 2627 boolean checkResult,
aoqi@0 2628 int prevMark) {
aoqi@0 2629 this.cachedImpl = cachedImpl;
aoqi@0 2630 this.implFilter = scopeFilter;
aoqi@0 2631 this.checkResult = checkResult;
aoqi@0 2632 this.prevMark = prevMark;
aoqi@0 2633 }
aoqi@0 2634
aoqi@0 2635 boolean matches(Filter<Symbol> scopeFilter, boolean checkResult, int mark) {
aoqi@0 2636 return this.implFilter == scopeFilter &&
aoqi@0 2637 this.checkResult == checkResult &&
aoqi@0 2638 this.prevMark == mark;
aoqi@0 2639 }
aoqi@0 2640 }
aoqi@0 2641
aoqi@0 2642 MethodSymbol get(MethodSymbol ms, TypeSymbol origin, boolean checkResult, Filter<Symbol> implFilter) {
aoqi@0 2643 SoftReference<Map<TypeSymbol, Entry>> ref_cache = _map.get(ms);
aoqi@0 2644 Map<TypeSymbol, Entry> cache = ref_cache != null ? ref_cache.get() : null;
aoqi@0 2645 if (cache == null) {
aoqi@0 2646 cache = new HashMap<TypeSymbol, Entry>();
aoqi@0 2647 _map.put(ms, new SoftReference<Map<TypeSymbol, Entry>>(cache));
aoqi@0 2648 }
aoqi@0 2649 Entry e = cache.get(origin);
aoqi@0 2650 CompoundScope members = membersClosure(origin.type, true);
aoqi@0 2651 if (e == null ||
aoqi@0 2652 !e.matches(implFilter, checkResult, members.getMark())) {
aoqi@0 2653 MethodSymbol impl = implementationInternal(ms, origin, checkResult, implFilter);
aoqi@0 2654 cache.put(origin, new Entry(impl, implFilter, checkResult, members.getMark()));
aoqi@0 2655 return impl;
aoqi@0 2656 }
aoqi@0 2657 else {
aoqi@0 2658 return e.cachedImpl;
aoqi@0 2659 }
aoqi@0 2660 }
aoqi@0 2661
aoqi@0 2662 private MethodSymbol implementationInternal(MethodSymbol ms, TypeSymbol origin, boolean checkResult, Filter<Symbol> implFilter) {
aoqi@0 2663 for (Type t = origin.type; t.hasTag(CLASS) || t.hasTag(TYPEVAR); t = supertype(t)) {
aoqi@0 2664 while (t.hasTag(TYPEVAR))
aoqi@0 2665 t = t.getUpperBound();
aoqi@0 2666 TypeSymbol c = t.tsym;
aoqi@0 2667 for (Scope.Entry e = c.members().lookup(ms.name, implFilter);
aoqi@0 2668 e.scope != null;
aoqi@0 2669 e = e.next(implFilter)) {
aoqi@0 2670 if (e.sym != null &&
aoqi@0 2671 e.sym.overrides(ms, origin, Types.this, checkResult))
aoqi@0 2672 return (MethodSymbol)e.sym;
aoqi@0 2673 }
aoqi@0 2674 }
aoqi@0 2675 return null;
aoqi@0 2676 }
aoqi@0 2677 }
aoqi@0 2678
aoqi@0 2679 private ImplementationCache implCache = new ImplementationCache();
aoqi@0 2680
aoqi@0 2681 public MethodSymbol implementation(MethodSymbol ms, TypeSymbol origin, boolean checkResult, Filter<Symbol> implFilter) {
aoqi@0 2682 return implCache.get(ms, origin, checkResult, implFilter);
aoqi@0 2683 }
aoqi@0 2684 // </editor-fold>
aoqi@0 2685
aoqi@0 2686 // <editor-fold defaultstate="collapsed" desc="compute transitive closure of all members in given site">
aoqi@0 2687 class MembersClosureCache extends SimpleVisitor<CompoundScope, Boolean> {
aoqi@0 2688
aoqi@0 2689 private WeakHashMap<TypeSymbol, Entry> _map =
aoqi@0 2690 new WeakHashMap<TypeSymbol, Entry>();
aoqi@0 2691
aoqi@0 2692 class Entry {
aoqi@0 2693 final boolean skipInterfaces;
aoqi@0 2694 final CompoundScope compoundScope;
aoqi@0 2695
aoqi@0 2696 public Entry(boolean skipInterfaces, CompoundScope compoundScope) {
aoqi@0 2697 this.skipInterfaces = skipInterfaces;
aoqi@0 2698 this.compoundScope = compoundScope;
aoqi@0 2699 }
aoqi@0 2700
aoqi@0 2701 boolean matches(boolean skipInterfaces) {
aoqi@0 2702 return this.skipInterfaces == skipInterfaces;
aoqi@0 2703 }
aoqi@0 2704 }
aoqi@0 2705
aoqi@0 2706 List<TypeSymbol> seenTypes = List.nil();
aoqi@0 2707
aoqi@0 2708 /** members closure visitor methods **/
aoqi@0 2709
aoqi@0 2710 public CompoundScope visitType(Type t, Boolean skipInterface) {
aoqi@0 2711 return null;
aoqi@0 2712 }
aoqi@0 2713
aoqi@0 2714 @Override
aoqi@0 2715 public CompoundScope visitClassType(ClassType t, Boolean skipInterface) {
aoqi@0 2716 if (seenTypes.contains(t.tsym)) {
aoqi@0 2717 //this is possible when an interface is implemented in multiple
aoqi@0 2718 //superclasses, or when a classs hierarchy is circular - in such
aoqi@0 2719 //cases we don't need to recurse (empty scope is returned)
aoqi@0 2720 return new CompoundScope(t.tsym);
aoqi@0 2721 }
aoqi@0 2722 try {
aoqi@0 2723 seenTypes = seenTypes.prepend(t.tsym);
aoqi@0 2724 ClassSymbol csym = (ClassSymbol)t.tsym;
aoqi@0 2725 Entry e = _map.get(csym);
aoqi@0 2726 if (e == null || !e.matches(skipInterface)) {
aoqi@0 2727 CompoundScope membersClosure = new CompoundScope(csym);
aoqi@0 2728 if (!skipInterface) {
aoqi@0 2729 for (Type i : interfaces(t)) {
aoqi@0 2730 membersClosure.addSubScope(visit(i, skipInterface));
aoqi@0 2731 }
aoqi@0 2732 }
aoqi@0 2733 membersClosure.addSubScope(visit(supertype(t), skipInterface));
aoqi@0 2734 membersClosure.addSubScope(csym.members());
aoqi@0 2735 e = new Entry(skipInterface, membersClosure);
aoqi@0 2736 _map.put(csym, e);
aoqi@0 2737 }
aoqi@0 2738 return e.compoundScope;
aoqi@0 2739 }
aoqi@0 2740 finally {
aoqi@0 2741 seenTypes = seenTypes.tail;
aoqi@0 2742 }
aoqi@0 2743 }
aoqi@0 2744
aoqi@0 2745 @Override
aoqi@0 2746 public CompoundScope visitTypeVar(TypeVar t, Boolean skipInterface) {
aoqi@0 2747 return visit(t.getUpperBound(), skipInterface);
aoqi@0 2748 }
aoqi@0 2749 }
aoqi@0 2750
aoqi@0 2751 private MembersClosureCache membersCache = new MembersClosureCache();
aoqi@0 2752
aoqi@0 2753 public CompoundScope membersClosure(Type site, boolean skipInterface) {
aoqi@0 2754 return membersCache.visit(site, skipInterface);
aoqi@0 2755 }
aoqi@0 2756 // </editor-fold>
aoqi@0 2757
aoqi@0 2758
aoqi@0 2759 //where
aoqi@0 2760 public List<MethodSymbol> interfaceCandidates(Type site, MethodSymbol ms) {
aoqi@0 2761 Filter<Symbol> filter = new MethodFilter(ms, site);
aoqi@0 2762 List<MethodSymbol> candidates = List.nil();
aoqi@0 2763 for (Symbol s : membersClosure(site, false).getElements(filter)) {
aoqi@0 2764 if (!site.tsym.isInterface() && !s.owner.isInterface()) {
aoqi@0 2765 return List.of((MethodSymbol)s);
aoqi@0 2766 } else if (!candidates.contains(s)) {
aoqi@0 2767 candidates = candidates.prepend((MethodSymbol)s);
aoqi@0 2768 }
aoqi@0 2769 }
aoqi@0 2770 return prune(candidates);
aoqi@0 2771 }
aoqi@0 2772
aoqi@0 2773 public List<MethodSymbol> prune(List<MethodSymbol> methods) {
aoqi@0 2774 ListBuffer<MethodSymbol> methodsMin = new ListBuffer<>();
aoqi@0 2775 for (MethodSymbol m1 : methods) {
aoqi@0 2776 boolean isMin_m1 = true;
aoqi@0 2777 for (MethodSymbol m2 : methods) {
aoqi@0 2778 if (m1 == m2) continue;
aoqi@0 2779 if (m2.owner != m1.owner &&
aoqi@0 2780 asSuper(m2.owner.type, m1.owner) != null) {
aoqi@0 2781 isMin_m1 = false;
aoqi@0 2782 break;
aoqi@0 2783 }
aoqi@0 2784 }
aoqi@0 2785 if (isMin_m1)
aoqi@0 2786 methodsMin.append(m1);
aoqi@0 2787 }
aoqi@0 2788 return methodsMin.toList();
aoqi@0 2789 }
aoqi@0 2790 // where
aoqi@0 2791 private class MethodFilter implements Filter<Symbol> {
aoqi@0 2792
aoqi@0 2793 Symbol msym;
aoqi@0 2794 Type site;
aoqi@0 2795
aoqi@0 2796 MethodFilter(Symbol msym, Type site) {
aoqi@0 2797 this.msym = msym;
aoqi@0 2798 this.site = site;
aoqi@0 2799 }
aoqi@0 2800
aoqi@0 2801 public boolean accepts(Symbol s) {
aoqi@0 2802 return s.kind == Kinds.MTH &&
aoqi@0 2803 s.name == msym.name &&
aoqi@0 2804 (s.flags() & SYNTHETIC) == 0 &&
aoqi@0 2805 s.isInheritedIn(site.tsym, Types.this) &&
aoqi@0 2806 overrideEquivalent(memberType(site, s), memberType(site, msym));
aoqi@0 2807 }
aoqi@0 2808 };
aoqi@0 2809 // </editor-fold>
aoqi@0 2810
aoqi@0 2811 /**
aoqi@0 2812 * Does t have the same arguments as s? It is assumed that both
aoqi@0 2813 * types are (possibly polymorphic) method types. Monomorphic
aoqi@0 2814 * method types "have the same arguments", if their argument lists
aoqi@0 2815 * are equal. Polymorphic method types "have the same arguments",
aoqi@0 2816 * if they have the same arguments after renaming all type
aoqi@0 2817 * variables of one to corresponding type variables in the other,
aoqi@0 2818 * where correspondence is by position in the type parameter list.
aoqi@0 2819 */
aoqi@0 2820 public boolean hasSameArgs(Type t, Type s) {
aoqi@0 2821 return hasSameArgs(t, s, true);
aoqi@0 2822 }
aoqi@0 2823
aoqi@0 2824 public boolean hasSameArgs(Type t, Type s, boolean strict) {
aoqi@0 2825 return hasSameArgs(t, s, strict ? hasSameArgs_strict : hasSameArgs_nonstrict);
aoqi@0 2826 }
aoqi@0 2827
aoqi@0 2828 private boolean hasSameArgs(Type t, Type s, TypeRelation hasSameArgs) {
aoqi@0 2829 return hasSameArgs.visit(t, s);
aoqi@0 2830 }
aoqi@0 2831 // where
aoqi@0 2832 private class HasSameArgs extends TypeRelation {
aoqi@0 2833
aoqi@0 2834 boolean strict;
aoqi@0 2835
aoqi@0 2836 public HasSameArgs(boolean strict) {
aoqi@0 2837 this.strict = strict;
aoqi@0 2838 }
aoqi@0 2839
aoqi@0 2840 public Boolean visitType(Type t, Type s) {
aoqi@0 2841 throw new AssertionError();
aoqi@0 2842 }
aoqi@0 2843
aoqi@0 2844 @Override
aoqi@0 2845 public Boolean visitMethodType(MethodType t, Type s) {
aoqi@0 2846 return s.hasTag(METHOD)
aoqi@0 2847 && containsTypeEquivalent(t.argtypes, s.getParameterTypes());
aoqi@0 2848 }
aoqi@0 2849
aoqi@0 2850 @Override
aoqi@0 2851 public Boolean visitForAll(ForAll t, Type s) {
aoqi@0 2852 if (!s.hasTag(FORALL))
aoqi@0 2853 return strict ? false : visitMethodType(t.asMethodType(), s);
aoqi@0 2854
aoqi@0 2855 ForAll forAll = (ForAll)s;
aoqi@0 2856 return hasSameBounds(t, forAll)
aoqi@0 2857 && visit(t.qtype, subst(forAll.qtype, forAll.tvars, t.tvars));
aoqi@0 2858 }
aoqi@0 2859
aoqi@0 2860 @Override
aoqi@0 2861 public Boolean visitErrorType(ErrorType t, Type s) {
aoqi@0 2862 return false;
aoqi@0 2863 }
aoqi@0 2864 };
aoqi@0 2865
aoqi@0 2866 TypeRelation hasSameArgs_strict = new HasSameArgs(true);
aoqi@0 2867 TypeRelation hasSameArgs_nonstrict = new HasSameArgs(false);
aoqi@0 2868
aoqi@0 2869 // </editor-fold>
aoqi@0 2870
aoqi@0 2871 // <editor-fold defaultstate="collapsed" desc="subst">
aoqi@0 2872 public List<Type> subst(List<Type> ts,
aoqi@0 2873 List<Type> from,
aoqi@0 2874 List<Type> to) {
aoqi@0 2875 return new Subst(from, to).subst(ts);
aoqi@0 2876 }
aoqi@0 2877
aoqi@0 2878 /**
aoqi@0 2879 * Substitute all occurrences of a type in `from' with the
aoqi@0 2880 * corresponding type in `to' in 't'. Match lists `from' and `to'
aoqi@0 2881 * from the right: If lists have different length, discard leading
aoqi@0 2882 * elements of the longer list.
aoqi@0 2883 */
aoqi@0 2884 public Type subst(Type t, List<Type> from, List<Type> to) {
aoqi@0 2885 return new Subst(from, to).subst(t);
aoqi@0 2886 }
aoqi@0 2887
aoqi@0 2888 private class Subst extends UnaryVisitor<Type> {
aoqi@0 2889 List<Type> from;
aoqi@0 2890 List<Type> to;
aoqi@0 2891
aoqi@0 2892 public Subst(List<Type> from, List<Type> to) {
aoqi@0 2893 int fromLength = from.length();
aoqi@0 2894 int toLength = to.length();
aoqi@0 2895 while (fromLength > toLength) {
aoqi@0 2896 fromLength--;
aoqi@0 2897 from = from.tail;
aoqi@0 2898 }
aoqi@0 2899 while (fromLength < toLength) {
aoqi@0 2900 toLength--;
aoqi@0 2901 to = to.tail;
aoqi@0 2902 }
aoqi@0 2903 this.from = from;
aoqi@0 2904 this.to = to;
aoqi@0 2905 }
aoqi@0 2906
aoqi@0 2907 Type subst(Type t) {
aoqi@0 2908 if (from.tail == null)
aoqi@0 2909 return t;
aoqi@0 2910 else
aoqi@0 2911 return visit(t);
aoqi@0 2912 }
aoqi@0 2913
aoqi@0 2914 List<Type> subst(List<Type> ts) {
aoqi@0 2915 if (from.tail == null)
aoqi@0 2916 return ts;
aoqi@0 2917 boolean wild = false;
aoqi@0 2918 if (ts.nonEmpty() && from.nonEmpty()) {
aoqi@0 2919 Type head1 = subst(ts.head);
aoqi@0 2920 List<Type> tail1 = subst(ts.tail);
aoqi@0 2921 if (head1 != ts.head || tail1 != ts.tail)
aoqi@0 2922 return tail1.prepend(head1);
aoqi@0 2923 }
aoqi@0 2924 return ts;
aoqi@0 2925 }
aoqi@0 2926
aoqi@0 2927 public Type visitType(Type t, Void ignored) {
aoqi@0 2928 return t;
aoqi@0 2929 }
aoqi@0 2930
aoqi@0 2931 @Override
aoqi@0 2932 public Type visitMethodType(MethodType t, Void ignored) {
aoqi@0 2933 List<Type> argtypes = subst(t.argtypes);
aoqi@0 2934 Type restype = subst(t.restype);
aoqi@0 2935 List<Type> thrown = subst(t.thrown);
aoqi@0 2936 if (argtypes == t.argtypes &&
aoqi@0 2937 restype == t.restype &&
aoqi@0 2938 thrown == t.thrown)
aoqi@0 2939 return t;
aoqi@0 2940 else
aoqi@0 2941 return new MethodType(argtypes, restype, thrown, t.tsym);
aoqi@0 2942 }
aoqi@0 2943
aoqi@0 2944 @Override
aoqi@0 2945 public Type visitTypeVar(TypeVar t, Void ignored) {
aoqi@0 2946 for (List<Type> from = this.from, to = this.to;
aoqi@0 2947 from.nonEmpty();
aoqi@0 2948 from = from.tail, to = to.tail) {
aoqi@0 2949 if (t == from.head) {
aoqi@0 2950 return to.head.withTypeVar(t);
aoqi@0 2951 }
aoqi@0 2952 }
aoqi@0 2953 return t;
aoqi@0 2954 }
aoqi@0 2955
aoqi@0 2956 @Override
aoqi@0 2957 public Type visitClassType(ClassType t, Void ignored) {
aoqi@0 2958 if (!t.isCompound()) {
aoqi@0 2959 List<Type> typarams = t.getTypeArguments();
aoqi@0 2960 List<Type> typarams1 = subst(typarams);
aoqi@0 2961 Type outer = t.getEnclosingType();
aoqi@0 2962 Type outer1 = subst(outer);
aoqi@0 2963 if (typarams1 == typarams && outer1 == outer)
aoqi@0 2964 return t;
aoqi@0 2965 else
aoqi@0 2966 return new ClassType(outer1, typarams1, t.tsym);
aoqi@0 2967 } else {
aoqi@0 2968 Type st = subst(supertype(t));
aoqi@0 2969 List<Type> is = subst(interfaces(t));
aoqi@0 2970 if (st == supertype(t) && is == interfaces(t))
aoqi@0 2971 return t;
aoqi@0 2972 else
aoqi@0 2973 return makeCompoundType(is.prepend(st));
aoqi@0 2974 }
aoqi@0 2975 }
aoqi@0 2976
aoqi@0 2977 @Override
aoqi@0 2978 public Type visitWildcardType(WildcardType t, Void ignored) {
aoqi@0 2979 Type bound = t.type;
aoqi@0 2980 if (t.kind != BoundKind.UNBOUND)
aoqi@0 2981 bound = subst(bound);
aoqi@0 2982 if (bound == t.type) {
aoqi@0 2983 return t;
aoqi@0 2984 } else {
aoqi@0 2985 if (t.isExtendsBound() && bound.isExtendsBound())
aoqi@0 2986 bound = wildUpperBound(bound);
aoqi@0 2987 return new WildcardType(bound, t.kind, syms.boundClass, t.bound);
aoqi@0 2988 }
aoqi@0 2989 }
aoqi@0 2990
aoqi@0 2991 @Override
aoqi@0 2992 public Type visitArrayType(ArrayType t, Void ignored) {
aoqi@0 2993 Type elemtype = subst(t.elemtype);
aoqi@0 2994 if (elemtype == t.elemtype)
aoqi@0 2995 return t;
aoqi@0 2996 else
aoqi@0 2997 return new ArrayType(elemtype, t.tsym);
aoqi@0 2998 }
aoqi@0 2999
aoqi@0 3000 @Override
aoqi@0 3001 public Type visitForAll(ForAll t, Void ignored) {
aoqi@0 3002 if (Type.containsAny(to, t.tvars)) {
aoqi@0 3003 //perform alpha-renaming of free-variables in 't'
aoqi@0 3004 //if 'to' types contain variables that are free in 't'
aoqi@0 3005 List<Type> freevars = newInstances(t.tvars);
aoqi@0 3006 t = new ForAll(freevars,
aoqi@0 3007 Types.this.subst(t.qtype, t.tvars, freevars));
aoqi@0 3008 }
aoqi@0 3009 List<Type> tvars1 = substBounds(t.tvars, from, to);
aoqi@0 3010 Type qtype1 = subst(t.qtype);
aoqi@0 3011 if (tvars1 == t.tvars && qtype1 == t.qtype) {
aoqi@0 3012 return t;
aoqi@0 3013 } else if (tvars1 == t.tvars) {
aoqi@0 3014 return new ForAll(tvars1, qtype1);
aoqi@0 3015 } else {
aoqi@0 3016 return new ForAll(tvars1, Types.this.subst(qtype1, t.tvars, tvars1));
aoqi@0 3017 }
aoqi@0 3018 }
aoqi@0 3019
aoqi@0 3020 @Override
aoqi@0 3021 public Type visitErrorType(ErrorType t, Void ignored) {
aoqi@0 3022 return t;
aoqi@0 3023 }
aoqi@0 3024 }
aoqi@0 3025
aoqi@0 3026 public List<Type> substBounds(List<Type> tvars,
aoqi@0 3027 List<Type> from,
aoqi@0 3028 List<Type> to) {
aoqi@0 3029 if (tvars.isEmpty())
aoqi@0 3030 return tvars;
aoqi@0 3031 ListBuffer<Type> newBoundsBuf = new ListBuffer<>();
aoqi@0 3032 boolean changed = false;
aoqi@0 3033 // calculate new bounds
aoqi@0 3034 for (Type t : tvars) {
aoqi@0 3035 TypeVar tv = (TypeVar) t;
aoqi@0 3036 Type bound = subst(tv.bound, from, to);
aoqi@0 3037 if (bound != tv.bound)
aoqi@0 3038 changed = true;
aoqi@0 3039 newBoundsBuf.append(bound);
aoqi@0 3040 }
aoqi@0 3041 if (!changed)
aoqi@0 3042 return tvars;
aoqi@0 3043 ListBuffer<Type> newTvars = new ListBuffer<>();
aoqi@0 3044 // create new type variables without bounds
aoqi@0 3045 for (Type t : tvars) {
aoqi@0 3046 newTvars.append(new TypeVar(t.tsym, null, syms.botType));
aoqi@0 3047 }
aoqi@0 3048 // the new bounds should use the new type variables in place
aoqi@0 3049 // of the old
aoqi@0 3050 List<Type> newBounds = newBoundsBuf.toList();
aoqi@0 3051 from = tvars;
aoqi@0 3052 to = newTvars.toList();
aoqi@0 3053 for (; !newBounds.isEmpty(); newBounds = newBounds.tail) {
aoqi@0 3054 newBounds.head = subst(newBounds.head, from, to);
aoqi@0 3055 }
aoqi@0 3056 newBounds = newBoundsBuf.toList();
aoqi@0 3057 // set the bounds of new type variables to the new bounds
aoqi@0 3058 for (Type t : newTvars.toList()) {
aoqi@0 3059 TypeVar tv = (TypeVar) t;
aoqi@0 3060 tv.bound = newBounds.head;
aoqi@0 3061 newBounds = newBounds.tail;
aoqi@0 3062 }
aoqi@0 3063 return newTvars.toList();
aoqi@0 3064 }
aoqi@0 3065
aoqi@0 3066 public TypeVar substBound(TypeVar t, List<Type> from, List<Type> to) {
aoqi@0 3067 Type bound1 = subst(t.bound, from, to);
aoqi@0 3068 if (bound1 == t.bound)
aoqi@0 3069 return t;
aoqi@0 3070 else {
aoqi@0 3071 // create new type variable without bounds
aoqi@0 3072 TypeVar tv = new TypeVar(t.tsym, null, syms.botType);
aoqi@0 3073 // the new bound should use the new type variable in place
aoqi@0 3074 // of the old
aoqi@0 3075 tv.bound = subst(bound1, List.<Type>of(t), List.<Type>of(tv));
aoqi@0 3076 return tv;
aoqi@0 3077 }
aoqi@0 3078 }
aoqi@0 3079 // </editor-fold>
aoqi@0 3080
aoqi@0 3081 // <editor-fold defaultstate="collapsed" desc="hasSameBounds">
aoqi@0 3082 /**
aoqi@0 3083 * Does t have the same bounds for quantified variables as s?
aoqi@0 3084 */
aoqi@0 3085 public boolean hasSameBounds(ForAll t, ForAll s) {
aoqi@0 3086 List<Type> l1 = t.tvars;
aoqi@0 3087 List<Type> l2 = s.tvars;
aoqi@0 3088 while (l1.nonEmpty() && l2.nonEmpty() &&
aoqi@0 3089 isSameType(l1.head.getUpperBound(),
aoqi@0 3090 subst(l2.head.getUpperBound(),
aoqi@0 3091 s.tvars,
aoqi@0 3092 t.tvars))) {
aoqi@0 3093 l1 = l1.tail;
aoqi@0 3094 l2 = l2.tail;
aoqi@0 3095 }
aoqi@0 3096 return l1.isEmpty() && l2.isEmpty();
aoqi@0 3097 }
aoqi@0 3098 // </editor-fold>
aoqi@0 3099
aoqi@0 3100 // <editor-fold defaultstate="collapsed" desc="newInstances">
aoqi@0 3101 /** Create new vector of type variables from list of variables
aoqi@0 3102 * changing all recursive bounds from old to new list.
aoqi@0 3103 */
aoqi@0 3104 public List<Type> newInstances(List<Type> tvars) {
aoqi@0 3105 List<Type> tvars1 = Type.map(tvars, newInstanceFun);
aoqi@0 3106 for (List<Type> l = tvars1; l.nonEmpty(); l = l.tail) {
aoqi@0 3107 TypeVar tv = (TypeVar) l.head;
aoqi@0 3108 tv.bound = subst(tv.bound, tvars, tvars1);
aoqi@0 3109 }
aoqi@0 3110 return tvars1;
aoqi@0 3111 }
aoqi@0 3112 private static final Mapping newInstanceFun = new Mapping("newInstanceFun") {
aoqi@0 3113 public Type apply(Type t) { return new TypeVar(t.tsym, t.getUpperBound(), t.getLowerBound()); }
aoqi@0 3114 };
aoqi@0 3115 // </editor-fold>
aoqi@0 3116
aoqi@0 3117 public Type createMethodTypeWithParameters(Type original, List<Type> newParams) {
aoqi@0 3118 return original.accept(methodWithParameters, newParams);
aoqi@0 3119 }
aoqi@0 3120 // where
aoqi@0 3121 private final MapVisitor<List<Type>> methodWithParameters = new MapVisitor<List<Type>>() {
aoqi@0 3122 public Type visitType(Type t, List<Type> newParams) {
aoqi@0 3123 throw new IllegalArgumentException("Not a method type: " + t);
aoqi@0 3124 }
aoqi@0 3125 public Type visitMethodType(MethodType t, List<Type> newParams) {
aoqi@0 3126 return new MethodType(newParams, t.restype, t.thrown, t.tsym);
aoqi@0 3127 }
aoqi@0 3128 public Type visitForAll(ForAll t, List<Type> newParams) {
aoqi@0 3129 return new ForAll(t.tvars, t.qtype.accept(this, newParams));
aoqi@0 3130 }
aoqi@0 3131 };
aoqi@0 3132
aoqi@0 3133 public Type createMethodTypeWithThrown(Type original, List<Type> newThrown) {
aoqi@0 3134 return original.accept(methodWithThrown, newThrown);
aoqi@0 3135 }
aoqi@0 3136 // where
aoqi@0 3137 private final MapVisitor<List<Type>> methodWithThrown = new MapVisitor<List<Type>>() {
aoqi@0 3138 public Type visitType(Type t, List<Type> newThrown) {
aoqi@0 3139 throw new IllegalArgumentException("Not a method type: " + t);
aoqi@0 3140 }
aoqi@0 3141 public Type visitMethodType(MethodType t, List<Type> newThrown) {
aoqi@0 3142 return new MethodType(t.argtypes, t.restype, newThrown, t.tsym);
aoqi@0 3143 }
aoqi@0 3144 public Type visitForAll(ForAll t, List<Type> newThrown) {
aoqi@0 3145 return new ForAll(t.tvars, t.qtype.accept(this, newThrown));
aoqi@0 3146 }
aoqi@0 3147 };
aoqi@0 3148
aoqi@0 3149 public Type createMethodTypeWithReturn(Type original, Type newReturn) {
aoqi@0 3150 return original.accept(methodWithReturn, newReturn);
aoqi@0 3151 }
aoqi@0 3152 // where
aoqi@0 3153 private final MapVisitor<Type> methodWithReturn = new MapVisitor<Type>() {
aoqi@0 3154 public Type visitType(Type t, Type newReturn) {
aoqi@0 3155 throw new IllegalArgumentException("Not a method type: " + t);
aoqi@0 3156 }
aoqi@0 3157 public Type visitMethodType(MethodType t, Type newReturn) {
aoqi@0 3158 return new MethodType(t.argtypes, newReturn, t.thrown, t.tsym);
aoqi@0 3159 }
aoqi@0 3160 public Type visitForAll(ForAll t, Type newReturn) {
aoqi@0 3161 return new ForAll(t.tvars, t.qtype.accept(this, newReturn));
aoqi@0 3162 }
aoqi@0 3163 };
aoqi@0 3164
aoqi@0 3165 // <editor-fold defaultstate="collapsed" desc="createErrorType">
aoqi@0 3166 public Type createErrorType(Type originalType) {
aoqi@0 3167 return new ErrorType(originalType, syms.errSymbol);
aoqi@0 3168 }
aoqi@0 3169
aoqi@0 3170 public Type createErrorType(ClassSymbol c, Type originalType) {
aoqi@0 3171 return new ErrorType(c, originalType);
aoqi@0 3172 }
aoqi@0 3173
aoqi@0 3174 public Type createErrorType(Name name, TypeSymbol container, Type originalType) {
aoqi@0 3175 return new ErrorType(name, container, originalType);
aoqi@0 3176 }
aoqi@0 3177 // </editor-fold>
aoqi@0 3178
aoqi@0 3179 // <editor-fold defaultstate="collapsed" desc="rank">
aoqi@0 3180 /**
aoqi@0 3181 * The rank of a class is the length of the longest path between
aoqi@0 3182 * the class and java.lang.Object in the class inheritance
aoqi@0 3183 * graph. Undefined for all but reference types.
aoqi@0 3184 */
aoqi@0 3185 public int rank(Type t) {
aoqi@0 3186 t = t.unannotatedType();
aoqi@0 3187 switch(t.getTag()) {
aoqi@0 3188 case CLASS: {
aoqi@0 3189 ClassType cls = (ClassType)t;
aoqi@0 3190 if (cls.rank_field < 0) {
aoqi@0 3191 Name fullname = cls.tsym.getQualifiedName();
aoqi@0 3192 if (fullname == names.java_lang_Object)
aoqi@0 3193 cls.rank_field = 0;
aoqi@0 3194 else {
aoqi@0 3195 int r = rank(supertype(cls));
aoqi@0 3196 for (List<Type> l = interfaces(cls);
aoqi@0 3197 l.nonEmpty();
aoqi@0 3198 l = l.tail) {
aoqi@0 3199 if (rank(l.head) > r)
aoqi@0 3200 r = rank(l.head);
aoqi@0 3201 }
aoqi@0 3202 cls.rank_field = r + 1;
aoqi@0 3203 }
aoqi@0 3204 }
aoqi@0 3205 return cls.rank_field;
aoqi@0 3206 }
aoqi@0 3207 case TYPEVAR: {
aoqi@0 3208 TypeVar tvar = (TypeVar)t;
aoqi@0 3209 if (tvar.rank_field < 0) {
aoqi@0 3210 int r = rank(supertype(tvar));
aoqi@0 3211 for (List<Type> l = interfaces(tvar);
aoqi@0 3212 l.nonEmpty();
aoqi@0 3213 l = l.tail) {
aoqi@0 3214 if (rank(l.head) > r) r = rank(l.head);
aoqi@0 3215 }
aoqi@0 3216 tvar.rank_field = r + 1;
aoqi@0 3217 }
aoqi@0 3218 return tvar.rank_field;
aoqi@0 3219 }
aoqi@0 3220 case ERROR:
aoqi@0 3221 case NONE:
aoqi@0 3222 return 0;
aoqi@0 3223 default:
aoqi@0 3224 throw new AssertionError();
aoqi@0 3225 }
aoqi@0 3226 }
aoqi@0 3227 // </editor-fold>
aoqi@0 3228
aoqi@0 3229 /**
aoqi@0 3230 * Helper method for generating a string representation of a given type
aoqi@0 3231 * accordingly to a given locale
aoqi@0 3232 */
aoqi@0 3233 public String toString(Type t, Locale locale) {
aoqi@0 3234 return Printer.createStandardPrinter(messages).visit(t, locale);
aoqi@0 3235 }
aoqi@0 3236
aoqi@0 3237 /**
aoqi@0 3238 * Helper method for generating a string representation of a given type
aoqi@0 3239 * accordingly to a given locale
aoqi@0 3240 */
aoqi@0 3241 public String toString(Symbol t, Locale locale) {
aoqi@0 3242 return Printer.createStandardPrinter(messages).visit(t, locale);
aoqi@0 3243 }
aoqi@0 3244
aoqi@0 3245 // <editor-fold defaultstate="collapsed" desc="toString">
aoqi@0 3246 /**
aoqi@0 3247 * This toString is slightly more descriptive than the one on Type.
aoqi@0 3248 *
aoqi@0 3249 * @deprecated Types.toString(Type t, Locale l) provides better support
aoqi@0 3250 * for localization
aoqi@0 3251 */
aoqi@0 3252 @Deprecated
aoqi@0 3253 public String toString(Type t) {
aoqi@0 3254 if (t.hasTag(FORALL)) {
aoqi@0 3255 ForAll forAll = (ForAll)t;
aoqi@0 3256 return typaramsString(forAll.tvars) + forAll.qtype;
aoqi@0 3257 }
aoqi@0 3258 return "" + t;
aoqi@0 3259 }
aoqi@0 3260 // where
aoqi@0 3261 private String typaramsString(List<Type> tvars) {
aoqi@0 3262 StringBuilder s = new StringBuilder();
aoqi@0 3263 s.append('<');
aoqi@0 3264 boolean first = true;
aoqi@0 3265 for (Type t : tvars) {
aoqi@0 3266 if (!first) s.append(", ");
aoqi@0 3267 first = false;
aoqi@0 3268 appendTyparamString(((TypeVar)t.unannotatedType()), s);
aoqi@0 3269 }
aoqi@0 3270 s.append('>');
aoqi@0 3271 return s.toString();
aoqi@0 3272 }
aoqi@0 3273 private void appendTyparamString(TypeVar t, StringBuilder buf) {
aoqi@0 3274 buf.append(t);
aoqi@0 3275 if (t.bound == null ||
aoqi@0 3276 t.bound.tsym.getQualifiedName() == names.java_lang_Object)
aoqi@0 3277 return;
aoqi@0 3278 buf.append(" extends "); // Java syntax; no need for i18n
aoqi@0 3279 Type bound = t.bound;
aoqi@0 3280 if (!bound.isCompound()) {
aoqi@0 3281 buf.append(bound);
aoqi@0 3282 } else if ((erasure(t).tsym.flags() & INTERFACE) == 0) {
aoqi@0 3283 buf.append(supertype(t));
aoqi@0 3284 for (Type intf : interfaces(t)) {
aoqi@0 3285 buf.append('&');
aoqi@0 3286 buf.append(intf);
aoqi@0 3287 }
aoqi@0 3288 } else {
aoqi@0 3289 // No superclass was given in bounds.
aoqi@0 3290 // In this case, supertype is Object, erasure is first interface.
aoqi@0 3291 boolean first = true;
aoqi@0 3292 for (Type intf : interfaces(t)) {
aoqi@0 3293 if (!first) buf.append('&');
aoqi@0 3294 first = false;
aoqi@0 3295 buf.append(intf);
aoqi@0 3296 }
aoqi@0 3297 }
aoqi@0 3298 }
aoqi@0 3299 // </editor-fold>
aoqi@0 3300
aoqi@0 3301 // <editor-fold defaultstate="collapsed" desc="Determining least upper bounds of types">
aoqi@0 3302 /**
aoqi@0 3303 * A cache for closures.
aoqi@0 3304 *
aoqi@0 3305 * <p>A closure is a list of all the supertypes and interfaces of
aoqi@0 3306 * a class or interface type, ordered by ClassSymbol.precedes
aoqi@0 3307 * (that is, subclasses come first, arbitrary but fixed
aoqi@0 3308 * otherwise).
aoqi@0 3309 */
aoqi@0 3310 private Map<Type,List<Type>> closureCache = new HashMap<Type,List<Type>>();
aoqi@0 3311
aoqi@0 3312 /**
aoqi@0 3313 * Returns the closure of a class or interface type.
aoqi@0 3314 */
aoqi@0 3315 public List<Type> closure(Type t) {
aoqi@0 3316 List<Type> cl = closureCache.get(t);
aoqi@0 3317 if (cl == null) {
aoqi@0 3318 Type st = supertype(t);
aoqi@0 3319 if (!t.isCompound()) {
aoqi@0 3320 if (st.hasTag(CLASS)) {
aoqi@0 3321 cl = insert(closure(st), t);
aoqi@0 3322 } else if (st.hasTag(TYPEVAR)) {
aoqi@0 3323 cl = closure(st).prepend(t);
aoqi@0 3324 } else {
aoqi@0 3325 cl = List.of(t);
aoqi@0 3326 }
aoqi@0 3327 } else {
aoqi@0 3328 cl = closure(supertype(t));
aoqi@0 3329 }
aoqi@0 3330 for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail)
aoqi@0 3331 cl = union(cl, closure(l.head));
aoqi@0 3332 closureCache.put(t, cl);
aoqi@0 3333 }
aoqi@0 3334 return cl;
aoqi@0 3335 }
aoqi@0 3336
aoqi@0 3337 /**
aoqi@0 3338 * Insert a type in a closure
aoqi@0 3339 */
aoqi@0 3340 public List<Type> insert(List<Type> cl, Type t) {
aoqi@0 3341 if (cl.isEmpty()) {
aoqi@0 3342 return cl.prepend(t);
aoqi@0 3343 } else if (t.tsym == cl.head.tsym) {
aoqi@0 3344 return cl;
aoqi@0 3345 } else if (t.tsym.precedes(cl.head.tsym, this)) {
aoqi@0 3346 return cl.prepend(t);
aoqi@0 3347 } else {
aoqi@0 3348 // t comes after head, or the two are unrelated
aoqi@0 3349 return insert(cl.tail, t).prepend(cl.head);
aoqi@0 3350 }
aoqi@0 3351 }
aoqi@0 3352
aoqi@0 3353 /**
aoqi@0 3354 * Form the union of two closures
aoqi@0 3355 */
aoqi@0 3356 public List<Type> union(List<Type> cl1, List<Type> cl2) {
aoqi@0 3357 if (cl1.isEmpty()) {
aoqi@0 3358 return cl2;
aoqi@0 3359 } else if (cl2.isEmpty()) {
aoqi@0 3360 return cl1;
aoqi@0 3361 } else if (cl1.head.tsym == cl2.head.tsym) {
aoqi@0 3362 return union(cl1.tail, cl2.tail).prepend(cl1.head);
aoqi@0 3363 } else if (cl1.head.tsym.precedes(cl2.head.tsym, this)) {
aoqi@0 3364 return union(cl1.tail, cl2).prepend(cl1.head);
aoqi@0 3365 } else if (cl2.head.tsym.precedes(cl1.head.tsym, this)) {
aoqi@0 3366 return union(cl1, cl2.tail).prepend(cl2.head);
aoqi@0 3367 } else {
aoqi@0 3368 // unrelated types
aoqi@0 3369 return union(cl1.tail, cl2).prepend(cl1.head);
aoqi@0 3370 }
aoqi@0 3371 }
aoqi@0 3372
aoqi@0 3373 /**
aoqi@0 3374 * Intersect two closures
aoqi@0 3375 */
aoqi@0 3376 public List<Type> intersect(List<Type> cl1, List<Type> cl2) {
aoqi@0 3377 if (cl1 == cl2)
aoqi@0 3378 return cl1;
aoqi@0 3379 if (cl1.isEmpty() || cl2.isEmpty())
aoqi@0 3380 return List.nil();
aoqi@0 3381 if (cl1.head.tsym.precedes(cl2.head.tsym, this))
aoqi@0 3382 return intersect(cl1.tail, cl2);
aoqi@0 3383 if (cl2.head.tsym.precedes(cl1.head.tsym, this))
aoqi@0 3384 return intersect(cl1, cl2.tail);
aoqi@0 3385 if (isSameType(cl1.head, cl2.head))
aoqi@0 3386 return intersect(cl1.tail, cl2.tail).prepend(cl1.head);
aoqi@0 3387 if (cl1.head.tsym == cl2.head.tsym &&
aoqi@0 3388 cl1.head.hasTag(CLASS) && cl2.head.hasTag(CLASS)) {
aoqi@0 3389 if (cl1.head.isParameterized() && cl2.head.isParameterized()) {
aoqi@0 3390 Type merge = merge(cl1.head,cl2.head);
aoqi@0 3391 return intersect(cl1.tail, cl2.tail).prepend(merge);
aoqi@0 3392 }
aoqi@0 3393 if (cl1.head.isRaw() || cl2.head.isRaw())
aoqi@0 3394 return intersect(cl1.tail, cl2.tail).prepend(erasure(cl1.head));
aoqi@0 3395 }
aoqi@0 3396 return intersect(cl1.tail, cl2.tail);
aoqi@0 3397 }
aoqi@0 3398 // where
aoqi@0 3399 class TypePair {
aoqi@0 3400 final Type t1;
aoqi@0 3401 final Type t2;
aoqi@0 3402 boolean strict;
aoqi@0 3403
aoqi@0 3404 TypePair(Type t1, Type t2) {
aoqi@0 3405 this(t1, t2, false);
aoqi@0 3406 }
aoqi@0 3407
aoqi@0 3408 TypePair(Type t1, Type t2, boolean strict) {
aoqi@0 3409 this.t1 = t1;
aoqi@0 3410 this.t2 = t2;
aoqi@0 3411 this.strict = strict;
aoqi@0 3412 }
aoqi@0 3413 @Override
aoqi@0 3414 public int hashCode() {
aoqi@0 3415 return 127 * Types.this.hashCode(t1) + Types.this.hashCode(t2);
aoqi@0 3416 }
aoqi@0 3417 @Override
aoqi@0 3418 public boolean equals(Object obj) {
aoqi@0 3419 if (!(obj instanceof TypePair))
aoqi@0 3420 return false;
aoqi@0 3421 TypePair typePair = (TypePair)obj;
aoqi@0 3422 return isSameType(t1, typePair.t1, strict)
aoqi@0 3423 && isSameType(t2, typePair.t2, strict);
aoqi@0 3424 }
aoqi@0 3425 }
aoqi@0 3426 Set<TypePair> mergeCache = new HashSet<TypePair>();
aoqi@0 3427 private Type merge(Type c1, Type c2) {
aoqi@0 3428 ClassType class1 = (ClassType) c1;
aoqi@0 3429 List<Type> act1 = class1.getTypeArguments();
aoqi@0 3430 ClassType class2 = (ClassType) c2;
aoqi@0 3431 List<Type> act2 = class2.getTypeArguments();
aoqi@0 3432 ListBuffer<Type> merged = new ListBuffer<Type>();
aoqi@0 3433 List<Type> typarams = class1.tsym.type.getTypeArguments();
aoqi@0 3434
aoqi@0 3435 while (act1.nonEmpty() && act2.nonEmpty() && typarams.nonEmpty()) {
aoqi@0 3436 if (containsType(act1.head, act2.head)) {
aoqi@0 3437 merged.append(act1.head);
aoqi@0 3438 } else if (containsType(act2.head, act1.head)) {
aoqi@0 3439 merged.append(act2.head);
aoqi@0 3440 } else {
aoqi@0 3441 TypePair pair = new TypePair(c1, c2);
aoqi@0 3442 Type m;
aoqi@0 3443 if (mergeCache.add(pair)) {
aoqi@0 3444 m = new WildcardType(lub(wildUpperBound(act1.head),
aoqi@0 3445 wildUpperBound(act2.head)),
aoqi@0 3446 BoundKind.EXTENDS,
aoqi@0 3447 syms.boundClass);
aoqi@0 3448 mergeCache.remove(pair);
aoqi@0 3449 } else {
aoqi@0 3450 m = new WildcardType(syms.objectType,
aoqi@0 3451 BoundKind.UNBOUND,
aoqi@0 3452 syms.boundClass);
aoqi@0 3453 }
aoqi@0 3454 merged.append(m.withTypeVar(typarams.head));
aoqi@0 3455 }
aoqi@0 3456 act1 = act1.tail;
aoqi@0 3457 act2 = act2.tail;
aoqi@0 3458 typarams = typarams.tail;
aoqi@0 3459 }
aoqi@0 3460 Assert.check(act1.isEmpty() && act2.isEmpty() && typarams.isEmpty());
aoqi@0 3461 return new ClassType(class1.getEnclosingType(), merged.toList(), class1.tsym);
aoqi@0 3462 }
aoqi@0 3463
aoqi@0 3464 /**
aoqi@0 3465 * Return the minimum type of a closure, a compound type if no
aoqi@0 3466 * unique minimum exists.
aoqi@0 3467 */
aoqi@0 3468 private Type compoundMin(List<Type> cl) {
aoqi@0 3469 if (cl.isEmpty()) return syms.objectType;
aoqi@0 3470 List<Type> compound = closureMin(cl);
aoqi@0 3471 if (compound.isEmpty())
aoqi@0 3472 return null;
aoqi@0 3473 else if (compound.tail.isEmpty())
aoqi@0 3474 return compound.head;
aoqi@0 3475 else
aoqi@0 3476 return makeCompoundType(compound);
aoqi@0 3477 }
aoqi@0 3478
aoqi@0 3479 /**
aoqi@0 3480 * Return the minimum types of a closure, suitable for computing
aoqi@0 3481 * compoundMin or glb.
aoqi@0 3482 */
aoqi@0 3483 private List<Type> closureMin(List<Type> cl) {
aoqi@0 3484 ListBuffer<Type> classes = new ListBuffer<>();
aoqi@0 3485 ListBuffer<Type> interfaces = new ListBuffer<>();
aoqi@0 3486 Set<Type> toSkip = new HashSet<>();
aoqi@0 3487 while (!cl.isEmpty()) {
aoqi@0 3488 Type current = cl.head;
aoqi@0 3489 boolean keep = !toSkip.contains(current);
aoqi@0 3490 if (keep && current.hasTag(TYPEVAR)) {
aoqi@0 3491 // skip lower-bounded variables with a subtype in cl.tail
aoqi@0 3492 for (Type t : cl.tail) {
aoqi@0 3493 if (isSubtypeNoCapture(t, current)) {
aoqi@0 3494 keep = false;
aoqi@0 3495 break;
aoqi@0 3496 }
aoqi@0 3497 }
aoqi@0 3498 }
aoqi@0 3499 if (keep) {
aoqi@0 3500 if (current.isInterface())
aoqi@0 3501 interfaces.append(current);
aoqi@0 3502 else
aoqi@0 3503 classes.append(current);
aoqi@0 3504 for (Type t : cl.tail) {
aoqi@0 3505 // skip supertypes of 'current' in cl.tail
aoqi@0 3506 if (isSubtypeNoCapture(current, t))
aoqi@0 3507 toSkip.add(t);
aoqi@0 3508 }
aoqi@0 3509 }
aoqi@0 3510 cl = cl.tail;
aoqi@0 3511 }
aoqi@0 3512 return classes.appendList(interfaces).toList();
aoqi@0 3513 }
aoqi@0 3514
aoqi@0 3515 /**
aoqi@0 3516 * Return the least upper bound of pair of types. if the lub does
aoqi@0 3517 * not exist return null.
aoqi@0 3518 */
aoqi@0 3519 public Type lub(Type t1, Type t2) {
aoqi@0 3520 return lub(List.of(t1, t2));
aoqi@0 3521 }
aoqi@0 3522
aoqi@0 3523 /**
aoqi@0 3524 * Return the least upper bound (lub) of set of types. If the lub
aoqi@0 3525 * does not exist return the type of null (bottom).
aoqi@0 3526 */
aoqi@0 3527 public Type lub(List<Type> ts) {
aoqi@0 3528 final int ARRAY_BOUND = 1;
aoqi@0 3529 final int CLASS_BOUND = 2;
aoqi@0 3530 int boundkind = 0;
aoqi@0 3531 for (Type t : ts) {
aoqi@0 3532 switch (t.getTag()) {
aoqi@0 3533 case CLASS:
aoqi@0 3534 boundkind |= CLASS_BOUND;
aoqi@0 3535 break;
aoqi@0 3536 case ARRAY:
aoqi@0 3537 boundkind |= ARRAY_BOUND;
aoqi@0 3538 break;
aoqi@0 3539 case TYPEVAR:
aoqi@0 3540 do {
aoqi@0 3541 t = t.getUpperBound();
aoqi@0 3542 } while (t.hasTag(TYPEVAR));
aoqi@0 3543 if (t.hasTag(ARRAY)) {
aoqi@0 3544 boundkind |= ARRAY_BOUND;
aoqi@0 3545 } else {
aoqi@0 3546 boundkind |= CLASS_BOUND;
aoqi@0 3547 }
aoqi@0 3548 break;
aoqi@0 3549 default:
aoqi@0 3550 if (t.isPrimitive())
aoqi@0 3551 return syms.errType;
aoqi@0 3552 }
aoqi@0 3553 }
aoqi@0 3554 switch (boundkind) {
aoqi@0 3555 case 0:
aoqi@0 3556 return syms.botType;
aoqi@0 3557
aoqi@0 3558 case ARRAY_BOUND:
aoqi@0 3559 // calculate lub(A[], B[])
aoqi@0 3560 List<Type> elements = Type.map(ts, elemTypeFun);
aoqi@0 3561 for (Type t : elements) {
aoqi@0 3562 if (t.isPrimitive()) {
aoqi@0 3563 // if a primitive type is found, then return
aoqi@0 3564 // arraySuperType unless all the types are the
aoqi@0 3565 // same
aoqi@0 3566 Type first = ts.head;
aoqi@0 3567 for (Type s : ts.tail) {
aoqi@0 3568 if (!isSameType(first, s)) {
aoqi@0 3569 // lub(int[], B[]) is Cloneable & Serializable
aoqi@0 3570 return arraySuperType();
aoqi@0 3571 }
aoqi@0 3572 }
aoqi@0 3573 // all the array types are the same, return one
aoqi@0 3574 // lub(int[], int[]) is int[]
aoqi@0 3575 return first;
aoqi@0 3576 }
aoqi@0 3577 }
aoqi@0 3578 // lub(A[], B[]) is lub(A, B)[]
aoqi@0 3579 return new ArrayType(lub(elements), syms.arrayClass);
aoqi@0 3580
aoqi@0 3581 case CLASS_BOUND:
aoqi@0 3582 // calculate lub(A, B)
aoqi@0 3583 while (!ts.head.hasTag(CLASS) && !ts.head.hasTag(TYPEVAR)) {
aoqi@0 3584 ts = ts.tail;
aoqi@0 3585 }
aoqi@0 3586 Assert.check(!ts.isEmpty());
aoqi@0 3587 //step 1 - compute erased candidate set (EC)
aoqi@0 3588 List<Type> cl = erasedSupertypes(ts.head);
aoqi@0 3589 for (Type t : ts.tail) {
aoqi@0 3590 if (t.hasTag(CLASS) || t.hasTag(TYPEVAR))
aoqi@0 3591 cl = intersect(cl, erasedSupertypes(t));
aoqi@0 3592 }
aoqi@0 3593 //step 2 - compute minimal erased candidate set (MEC)
aoqi@0 3594 List<Type> mec = closureMin(cl);
aoqi@0 3595 //step 3 - for each element G in MEC, compute lci(Inv(G))
aoqi@0 3596 List<Type> candidates = List.nil();
aoqi@0 3597 for (Type erasedSupertype : mec) {
aoqi@0 3598 List<Type> lci = List.of(asSuper(ts.head, erasedSupertype.tsym));
aoqi@0 3599 for (Type t : ts) {
aoqi@0 3600 lci = intersect(lci, List.of(asSuper(t, erasedSupertype.tsym)));
aoqi@0 3601 }
aoqi@0 3602 candidates = candidates.appendList(lci);
aoqi@0 3603 }
aoqi@0 3604 //step 4 - let MEC be { G1, G2 ... Gn }, then we have that
aoqi@0 3605 //lub = lci(Inv(G1)) & lci(Inv(G2)) & ... & lci(Inv(Gn))
aoqi@0 3606 return compoundMin(candidates);
aoqi@0 3607
aoqi@0 3608 default:
aoqi@0 3609 // calculate lub(A, B[])
aoqi@0 3610 List<Type> classes = List.of(arraySuperType());
aoqi@0 3611 for (Type t : ts) {
aoqi@0 3612 if (!t.hasTag(ARRAY)) // Filter out any arrays
aoqi@0 3613 classes = classes.prepend(t);
aoqi@0 3614 }
aoqi@0 3615 // lub(A, B[]) is lub(A, arraySuperType)
aoqi@0 3616 return lub(classes);
aoqi@0 3617 }
aoqi@0 3618 }
aoqi@0 3619 // where
aoqi@0 3620 List<Type> erasedSupertypes(Type t) {
aoqi@0 3621 ListBuffer<Type> buf = new ListBuffer<>();
aoqi@0 3622 for (Type sup : closure(t)) {
aoqi@0 3623 if (sup.hasTag(TYPEVAR)) {
aoqi@0 3624 buf.append(sup);
aoqi@0 3625 } else {
aoqi@0 3626 buf.append(erasure(sup));
aoqi@0 3627 }
aoqi@0 3628 }
aoqi@0 3629 return buf.toList();
aoqi@0 3630 }
aoqi@0 3631
aoqi@0 3632 private Type arraySuperType = null;
aoqi@0 3633 private Type arraySuperType() {
aoqi@0 3634 // initialized lazily to avoid problems during compiler startup
aoqi@0 3635 if (arraySuperType == null) {
aoqi@0 3636 synchronized (this) {
aoqi@0 3637 if (arraySuperType == null) {
aoqi@0 3638 // JLS 10.8: all arrays implement Cloneable and Serializable.
aoqi@0 3639 arraySuperType = makeCompoundType(List.of(syms.serializableType,
aoqi@0 3640 syms.cloneableType), true);
aoqi@0 3641 }
aoqi@0 3642 }
aoqi@0 3643 }
aoqi@0 3644 return arraySuperType;
aoqi@0 3645 }
aoqi@0 3646 // </editor-fold>
aoqi@0 3647
aoqi@0 3648 // <editor-fold defaultstate="collapsed" desc="Greatest lower bound">
aoqi@0 3649 public Type glb(List<Type> ts) {
aoqi@0 3650 Type t1 = ts.head;
aoqi@0 3651 for (Type t2 : ts.tail) {
aoqi@0 3652 if (t1.isErroneous())
aoqi@0 3653 return t1;
aoqi@0 3654 t1 = glb(t1, t2);
aoqi@0 3655 }
aoqi@0 3656 return t1;
aoqi@0 3657 }
aoqi@0 3658 //where
aoqi@0 3659 public Type glb(Type t, Type s) {
aoqi@0 3660 if (s == null)
aoqi@0 3661 return t;
aoqi@0 3662 else if (t.isPrimitive() || s.isPrimitive())
aoqi@0 3663 return syms.errType;
aoqi@0 3664 else if (isSubtypeNoCapture(t, s))
aoqi@0 3665 return t;
aoqi@0 3666 else if (isSubtypeNoCapture(s, t))
aoqi@0 3667 return s;
aoqi@0 3668
aoqi@0 3669 List<Type> closure = union(closure(t), closure(s));
aoqi@0 3670 return glbFlattened(closure, t);
aoqi@0 3671 }
aoqi@0 3672 //where
aoqi@0 3673 /**
aoqi@0 3674 * Perform glb for a list of non-primitive, non-error, non-compound types;
aoqi@0 3675 * redundant elements are removed. Bounds should be ordered according to
aoqi@0 3676 * {@link Symbol#precedes(TypeSymbol,Types)}.
aoqi@0 3677 *
aoqi@0 3678 * @param flatBounds List of type to glb
aoqi@0 3679 * @param errT Original type to use if the result is an error type
aoqi@0 3680 */
aoqi@0 3681 private Type glbFlattened(List<Type> flatBounds, Type errT) {
aoqi@0 3682 List<Type> bounds = closureMin(flatBounds);
aoqi@0 3683
aoqi@0 3684 if (bounds.isEmpty()) { // length == 0
aoqi@0 3685 return syms.objectType;
aoqi@0 3686 } else if (bounds.tail.isEmpty()) { // length == 1
aoqi@0 3687 return bounds.head;
aoqi@0 3688 } else { // length > 1
aoqi@0 3689 int classCount = 0;
aoqi@0 3690 List<Type> lowers = List.nil();
aoqi@0 3691 for (Type bound : bounds) {
aoqi@0 3692 if (!bound.isInterface()) {
aoqi@0 3693 classCount++;
aoqi@0 3694 Type lower = cvarLowerBound(bound);
aoqi@0 3695 if (bound != lower && !lower.hasTag(BOT))
aoqi@0 3696 lowers = insert(lowers, lower);
aoqi@0 3697 }
aoqi@0 3698 }
aoqi@0 3699 if (classCount > 1) {
aoqi@0 3700 if (lowers.isEmpty())
aoqi@0 3701 return createErrorType(errT);
aoqi@0 3702 else
aoqi@0 3703 return glbFlattened(union(bounds, lowers), errT);
aoqi@0 3704 }
aoqi@0 3705 }
aoqi@0 3706 return makeCompoundType(bounds);
aoqi@0 3707 }
aoqi@0 3708 // </editor-fold>
aoqi@0 3709
aoqi@0 3710 // <editor-fold defaultstate="collapsed" desc="hashCode">
aoqi@0 3711 /**
aoqi@0 3712 * Compute a hash code on a type.
aoqi@0 3713 */
aoqi@0 3714 public int hashCode(Type t) {
aoqi@0 3715 return hashCode.visit(t);
aoqi@0 3716 }
aoqi@0 3717 // where
aoqi@0 3718 private static final UnaryVisitor<Integer> hashCode = new UnaryVisitor<Integer>() {
aoqi@0 3719
aoqi@0 3720 public Integer visitType(Type t, Void ignored) {
aoqi@0 3721 return t.getTag().ordinal();
aoqi@0 3722 }
aoqi@0 3723
aoqi@0 3724 @Override
aoqi@0 3725 public Integer visitClassType(ClassType t, Void ignored) {
aoqi@0 3726 int result = visit(t.getEnclosingType());
aoqi@0 3727 result *= 127;
aoqi@0 3728 result += t.tsym.flatName().hashCode();
aoqi@0 3729 for (Type s : t.getTypeArguments()) {
aoqi@0 3730 result *= 127;
aoqi@0 3731 result += visit(s);
aoqi@0 3732 }
aoqi@0 3733 return result;
aoqi@0 3734 }
aoqi@0 3735
aoqi@0 3736 @Override
aoqi@0 3737 public Integer visitMethodType(MethodType t, Void ignored) {
aoqi@0 3738 int h = METHOD.ordinal();
aoqi@0 3739 for (List<Type> thisargs = t.argtypes;
aoqi@0 3740 thisargs.tail != null;
aoqi@0 3741 thisargs = thisargs.tail)
aoqi@0 3742 h = (h << 5) + visit(thisargs.head);
aoqi@0 3743 return (h << 5) + visit(t.restype);
aoqi@0 3744 }
aoqi@0 3745
aoqi@0 3746 @Override
aoqi@0 3747 public Integer visitWildcardType(WildcardType t, Void ignored) {
aoqi@0 3748 int result = t.kind.hashCode();
aoqi@0 3749 if (t.type != null) {
aoqi@0 3750 result *= 127;
aoqi@0 3751 result += visit(t.type);
aoqi@0 3752 }
aoqi@0 3753 return result;
aoqi@0 3754 }
aoqi@0 3755
aoqi@0 3756 @Override
aoqi@0 3757 public Integer visitArrayType(ArrayType t, Void ignored) {
aoqi@0 3758 return visit(t.elemtype) + 12;
aoqi@0 3759 }
aoqi@0 3760
aoqi@0 3761 @Override
aoqi@0 3762 public Integer visitTypeVar(TypeVar t, Void ignored) {
aoqi@0 3763 return System.identityHashCode(t.tsym);
aoqi@0 3764 }
aoqi@0 3765
aoqi@0 3766 @Override
aoqi@0 3767 public Integer visitUndetVar(UndetVar t, Void ignored) {
aoqi@0 3768 return System.identityHashCode(t);
aoqi@0 3769 }
aoqi@0 3770
aoqi@0 3771 @Override
aoqi@0 3772 public Integer visitErrorType(ErrorType t, Void ignored) {
aoqi@0 3773 return 0;
aoqi@0 3774 }
aoqi@0 3775 };
aoqi@0 3776 // </editor-fold>
aoqi@0 3777
aoqi@0 3778 // <editor-fold defaultstate="collapsed" desc="Return-Type-Substitutable">
aoqi@0 3779 /**
aoqi@0 3780 * Does t have a result that is a subtype of the result type of s,
aoqi@0 3781 * suitable for covariant returns? It is assumed that both types
aoqi@0 3782 * are (possibly polymorphic) method types. Monomorphic method
aoqi@0 3783 * types are handled in the obvious way. Polymorphic method types
aoqi@0 3784 * require renaming all type variables of one to corresponding
aoqi@0 3785 * type variables in the other, where correspondence is by
aoqi@0 3786 * position in the type parameter list. */
aoqi@0 3787 public boolean resultSubtype(Type t, Type s, Warner warner) {
aoqi@0 3788 List<Type> tvars = t.getTypeArguments();
aoqi@0 3789 List<Type> svars = s.getTypeArguments();
aoqi@0 3790 Type tres = t.getReturnType();
aoqi@0 3791 Type sres = subst(s.getReturnType(), svars, tvars);
aoqi@0 3792 return covariantReturnType(tres, sres, warner);
aoqi@0 3793 }
aoqi@0 3794
aoqi@0 3795 /**
aoqi@0 3796 * Return-Type-Substitutable.
aoqi@0 3797 * @jls section 8.4.5
aoqi@0 3798 */
aoqi@0 3799 public boolean returnTypeSubstitutable(Type r1, Type r2) {
aoqi@0 3800 if (hasSameArgs(r1, r2))
aoqi@0 3801 return resultSubtype(r1, r2, noWarnings);
aoqi@0 3802 else
aoqi@0 3803 return covariantReturnType(r1.getReturnType(),
aoqi@0 3804 erasure(r2.getReturnType()),
aoqi@0 3805 noWarnings);
aoqi@0 3806 }
aoqi@0 3807
aoqi@0 3808 public boolean returnTypeSubstitutable(Type r1,
aoqi@0 3809 Type r2, Type r2res,
aoqi@0 3810 Warner warner) {
aoqi@0 3811 if (isSameType(r1.getReturnType(), r2res))
aoqi@0 3812 return true;
aoqi@0 3813 if (r1.getReturnType().isPrimitive() || r2res.isPrimitive())
aoqi@0 3814 return false;
aoqi@0 3815
aoqi@0 3816 if (hasSameArgs(r1, r2))
aoqi@0 3817 return covariantReturnType(r1.getReturnType(), r2res, warner);
aoqi@0 3818 if (!allowCovariantReturns)
aoqi@0 3819 return false;
aoqi@0 3820 if (isSubtypeUnchecked(r1.getReturnType(), r2res, warner))
aoqi@0 3821 return true;
aoqi@0 3822 if (!isSubtype(r1.getReturnType(), erasure(r2res)))
aoqi@0 3823 return false;
aoqi@0 3824 warner.warn(LintCategory.UNCHECKED);
aoqi@0 3825 return true;
aoqi@0 3826 }
aoqi@0 3827
aoqi@0 3828 /**
aoqi@0 3829 * Is t an appropriate return type in an overrider for a
aoqi@0 3830 * method that returns s?
aoqi@0 3831 */
aoqi@0 3832 public boolean covariantReturnType(Type t, Type s, Warner warner) {
aoqi@0 3833 return
aoqi@0 3834 isSameType(t, s) ||
aoqi@0 3835 allowCovariantReturns &&
aoqi@0 3836 !t.isPrimitive() &&
aoqi@0 3837 !s.isPrimitive() &&
aoqi@0 3838 isAssignable(t, s, warner);
aoqi@0 3839 }
aoqi@0 3840 // </editor-fold>
aoqi@0 3841
aoqi@0 3842 // <editor-fold defaultstate="collapsed" desc="Box/unbox support">
aoqi@0 3843 /**
aoqi@0 3844 * Return the class that boxes the given primitive.
aoqi@0 3845 */
aoqi@0 3846 public ClassSymbol boxedClass(Type t) {
aoqi@0 3847 return reader.enterClass(syms.boxedName[t.getTag().ordinal()]);
aoqi@0 3848 }
aoqi@0 3849
aoqi@0 3850 /**
aoqi@0 3851 * Return the boxed type if 't' is primitive, otherwise return 't' itself.
aoqi@0 3852 */
aoqi@0 3853 public Type boxedTypeOrType(Type t) {
aoqi@0 3854 return t.isPrimitive() ?
aoqi@0 3855 boxedClass(t).type :
aoqi@0 3856 t;
aoqi@0 3857 }
aoqi@0 3858
aoqi@0 3859 /**
aoqi@0 3860 * Return the primitive type corresponding to a boxed type.
aoqi@0 3861 */
aoqi@0 3862 public Type unboxedType(Type t) {
aoqi@0 3863 if (allowBoxing) {
aoqi@0 3864 for (int i=0; i<syms.boxedName.length; i++) {
aoqi@0 3865 Name box = syms.boxedName[i];
aoqi@0 3866 if (box != null &&
aoqi@0 3867 asSuper(t, reader.enterClass(box)) != null)
aoqi@0 3868 return syms.typeOfTag[i];
aoqi@0 3869 }
aoqi@0 3870 }
aoqi@0 3871 return Type.noType;
aoqi@0 3872 }
aoqi@0 3873
aoqi@0 3874 /**
aoqi@0 3875 * Return the unboxed type if 't' is a boxed class, otherwise return 't' itself.
aoqi@0 3876 */
aoqi@0 3877 public Type unboxedTypeOrType(Type t) {
aoqi@0 3878 Type unboxedType = unboxedType(t);
aoqi@0 3879 return unboxedType.hasTag(NONE) ? t : unboxedType;
aoqi@0 3880 }
aoqi@0 3881 // </editor-fold>
aoqi@0 3882
aoqi@0 3883 // <editor-fold defaultstate="collapsed" desc="Capture conversion">
aoqi@0 3884 /*
aoqi@0 3885 * JLS 5.1.10 Capture Conversion:
aoqi@0 3886 *
aoqi@0 3887 * Let G name a generic type declaration with n formal type
aoqi@0 3888 * parameters A1 ... An with corresponding bounds U1 ... Un. There
aoqi@0 3889 * exists a capture conversion from G<T1 ... Tn> to G<S1 ... Sn>,
aoqi@0 3890 * where, for 1 <= i <= n:
aoqi@0 3891 *
aoqi@0 3892 * + If Ti is a wildcard type argument (4.5.1) of the form ? then
aoqi@0 3893 * Si is a fresh type variable whose upper bound is
aoqi@0 3894 * Ui[A1 := S1, ..., An := Sn] and whose lower bound is the null
aoqi@0 3895 * type.
aoqi@0 3896 *
aoqi@0 3897 * + If Ti is a wildcard type argument of the form ? extends Bi,
aoqi@0 3898 * then Si is a fresh type variable whose upper bound is
aoqi@0 3899 * glb(Bi, Ui[A1 := S1, ..., An := Sn]) and whose lower bound is
aoqi@0 3900 * the null type, where glb(V1,... ,Vm) is V1 & ... & Vm. It is
aoqi@0 3901 * a compile-time error if for any two classes (not interfaces)
aoqi@0 3902 * Vi and Vj,Vi is not a subclass of Vj or vice versa.
aoqi@0 3903 *
aoqi@0 3904 * + If Ti is a wildcard type argument of the form ? super Bi,
aoqi@0 3905 * then Si is a fresh type variable whose upper bound is
aoqi@0 3906 * Ui[A1 := S1, ..., An := Sn] and whose lower bound is Bi.
aoqi@0 3907 *
aoqi@0 3908 * + Otherwise, Si = Ti.
aoqi@0 3909 *
aoqi@0 3910 * Capture conversion on any type other than a parameterized type
aoqi@0 3911 * (4.5) acts as an identity conversion (5.1.1). Capture
aoqi@0 3912 * conversions never require a special action at run time and
aoqi@0 3913 * therefore never throw an exception at run time.
aoqi@0 3914 *
aoqi@0 3915 * Capture conversion is not applied recursively.
aoqi@0 3916 */
aoqi@0 3917 /**
aoqi@0 3918 * Capture conversion as specified by the JLS.
aoqi@0 3919 */
aoqi@0 3920
aoqi@0 3921 public List<Type> capture(List<Type> ts) {
aoqi@0 3922 List<Type> buf = List.nil();
aoqi@0 3923 for (Type t : ts) {
aoqi@0 3924 buf = buf.prepend(capture(t));
aoqi@0 3925 }
aoqi@0 3926 return buf.reverse();
aoqi@0 3927 }
aoqi@0 3928
aoqi@0 3929 public Type capture(Type t) {
aoqi@0 3930 if (!t.hasTag(CLASS)) {
aoqi@0 3931 return t;
aoqi@0 3932 }
aoqi@0 3933 if (t.getEnclosingType() != Type.noType) {
aoqi@0 3934 Type capturedEncl = capture(t.getEnclosingType());
aoqi@0 3935 if (capturedEncl != t.getEnclosingType()) {
aoqi@0 3936 Type type1 = memberType(capturedEncl, t.tsym);
aoqi@0 3937 t = subst(type1, t.tsym.type.getTypeArguments(), t.getTypeArguments());
aoqi@0 3938 }
aoqi@0 3939 }
aoqi@0 3940 t = t.unannotatedType();
aoqi@0 3941 ClassType cls = (ClassType)t;
aoqi@0 3942 if (cls.isRaw() || !cls.isParameterized())
aoqi@0 3943 return cls;
aoqi@0 3944
aoqi@0 3945 ClassType G = (ClassType)cls.asElement().asType();
aoqi@0 3946 List<Type> A = G.getTypeArguments();
aoqi@0 3947 List<Type> T = cls.getTypeArguments();
aoqi@0 3948 List<Type> S = freshTypeVariables(T);
aoqi@0 3949
aoqi@0 3950 List<Type> currentA = A;
aoqi@0 3951 List<Type> currentT = T;
aoqi@0 3952 List<Type> currentS = S;
aoqi@0 3953 boolean captured = false;
aoqi@0 3954 while (!currentA.isEmpty() &&
aoqi@0 3955 !currentT.isEmpty() &&
aoqi@0 3956 !currentS.isEmpty()) {
aoqi@0 3957 if (currentS.head != currentT.head) {
aoqi@0 3958 captured = true;
aoqi@0 3959 WildcardType Ti = (WildcardType)currentT.head.unannotatedType();
aoqi@0 3960 Type Ui = currentA.head.getUpperBound();
aoqi@0 3961 CapturedType Si = (CapturedType)currentS.head.unannotatedType();
aoqi@0 3962 if (Ui == null)
aoqi@0 3963 Ui = syms.objectType;
aoqi@0 3964 switch (Ti.kind) {
aoqi@0 3965 case UNBOUND:
aoqi@0 3966 Si.bound = subst(Ui, A, S);
aoqi@0 3967 Si.lower = syms.botType;
aoqi@0 3968 break;
aoqi@0 3969 case EXTENDS:
aoqi@0 3970 Si.bound = glb(Ti.getExtendsBound(), subst(Ui, A, S));
aoqi@0 3971 Si.lower = syms.botType;
aoqi@0 3972 break;
aoqi@0 3973 case SUPER:
aoqi@0 3974 Si.bound = subst(Ui, A, S);
aoqi@0 3975 Si.lower = Ti.getSuperBound();
aoqi@0 3976 break;
aoqi@0 3977 }
aoqi@0 3978 Type tmpBound = Si.bound.hasTag(UNDETVAR) ? ((UndetVar)Si.bound).qtype : Si.bound;
aoqi@0 3979 Type tmpLower = Si.lower.hasTag(UNDETVAR) ? ((UndetVar)Si.lower).qtype : Si.lower;
aoqi@0 3980 if (!Si.bound.hasTag(ERROR) &&
aoqi@0 3981 !Si.lower.hasTag(ERROR) &&
aoqi@0 3982 isSameType(tmpBound, tmpLower, false)) {
aoqi@0 3983 currentS.head = Si.bound;
aoqi@0 3984 }
aoqi@0 3985 }
aoqi@0 3986 currentA = currentA.tail;
aoqi@0 3987 currentT = currentT.tail;
aoqi@0 3988 currentS = currentS.tail;
aoqi@0 3989 }
aoqi@0 3990 if (!currentA.isEmpty() || !currentT.isEmpty() || !currentS.isEmpty())
aoqi@0 3991 return erasure(t); // some "rare" type involved
aoqi@0 3992
aoqi@0 3993 if (captured)
aoqi@0 3994 return new ClassType(cls.getEnclosingType(), S, cls.tsym);
aoqi@0 3995 else
aoqi@0 3996 return t;
aoqi@0 3997 }
aoqi@0 3998 // where
aoqi@0 3999 public List<Type> freshTypeVariables(List<Type> types) {
aoqi@0 4000 ListBuffer<Type> result = new ListBuffer<>();
aoqi@0 4001 for (Type t : types) {
aoqi@0 4002 if (t.hasTag(WILDCARD)) {
aoqi@0 4003 t = t.unannotatedType();
aoqi@0 4004 Type bound = ((WildcardType)t).getExtendsBound();
aoqi@0 4005 if (bound == null)
aoqi@0 4006 bound = syms.objectType;
aoqi@0 4007 result.append(new CapturedType(capturedName,
aoqi@0 4008 syms.noSymbol,
aoqi@0 4009 bound,
aoqi@0 4010 syms.botType,
aoqi@0 4011 (WildcardType)t));
aoqi@0 4012 } else {
aoqi@0 4013 result.append(t);
aoqi@0 4014 }
aoqi@0 4015 }
aoqi@0 4016 return result.toList();
aoqi@0 4017 }
aoqi@0 4018 // </editor-fold>
aoqi@0 4019
aoqi@0 4020 // <editor-fold defaultstate="collapsed" desc="Internal utility methods">
aoqi@0 4021 private boolean sideCast(Type from, Type to, Warner warn) {
aoqi@0 4022 // We are casting from type $from$ to type $to$, which are
aoqi@0 4023 // non-final unrelated types. This method
aoqi@0 4024 // tries to reject a cast by transferring type parameters
aoqi@0 4025 // from $to$ to $from$ by common superinterfaces.
aoqi@0 4026 boolean reverse = false;
aoqi@0 4027 Type target = to;
aoqi@0 4028 if ((to.tsym.flags() & INTERFACE) == 0) {
aoqi@0 4029 Assert.check((from.tsym.flags() & INTERFACE) != 0);
aoqi@0 4030 reverse = true;
aoqi@0 4031 to = from;
aoqi@0 4032 from = target;
aoqi@0 4033 }
aoqi@0 4034 List<Type> commonSupers = superClosure(to, erasure(from));
aoqi@0 4035 boolean giveWarning = commonSupers.isEmpty();
aoqi@0 4036 // The arguments to the supers could be unified here to
aoqi@0 4037 // get a more accurate analysis
aoqi@0 4038 while (commonSupers.nonEmpty()) {
aoqi@0 4039 Type t1 = asSuper(from, commonSupers.head.tsym);
aoqi@0 4040 Type t2 = commonSupers.head; // same as asSuper(to, commonSupers.head.tsym);
aoqi@0 4041 if (disjointTypes(t1.getTypeArguments(), t2.getTypeArguments()))
aoqi@0 4042 return false;
aoqi@0 4043 giveWarning = giveWarning || (reverse ? giveWarning(t2, t1) : giveWarning(t1, t2));
aoqi@0 4044 commonSupers = commonSupers.tail;
aoqi@0 4045 }
aoqi@0 4046 if (giveWarning && !isReifiable(reverse ? from : to))
aoqi@0 4047 warn.warn(LintCategory.UNCHECKED);
aoqi@0 4048 if (!allowCovariantReturns)
aoqi@0 4049 // reject if there is a common method signature with
aoqi@0 4050 // incompatible return types.
aoqi@0 4051 chk.checkCompatibleAbstracts(warn.pos(), from, to);
aoqi@0 4052 return true;
aoqi@0 4053 }
aoqi@0 4054
aoqi@0 4055 private boolean sideCastFinal(Type from, Type to, Warner warn) {
aoqi@0 4056 // We are casting from type $from$ to type $to$, which are
aoqi@0 4057 // unrelated types one of which is final and the other of
aoqi@0 4058 // which is an interface. This method
aoqi@0 4059 // tries to reject a cast by transferring type parameters
aoqi@0 4060 // from the final class to the interface.
aoqi@0 4061 boolean reverse = false;
aoqi@0 4062 Type target = to;
aoqi@0 4063 if ((to.tsym.flags() & INTERFACE) == 0) {
aoqi@0 4064 Assert.check((from.tsym.flags() & INTERFACE) != 0);
aoqi@0 4065 reverse = true;
aoqi@0 4066 to = from;
aoqi@0 4067 from = target;
aoqi@0 4068 }
aoqi@0 4069 Assert.check((from.tsym.flags() & FINAL) != 0);
aoqi@0 4070 Type t1 = asSuper(from, to.tsym);
aoqi@0 4071 if (t1 == null) return false;
aoqi@0 4072 Type t2 = to;
aoqi@0 4073 if (disjointTypes(t1.getTypeArguments(), t2.getTypeArguments()))
aoqi@0 4074 return false;
aoqi@0 4075 if (!allowCovariantReturns)
aoqi@0 4076 // reject if there is a common method signature with
aoqi@0 4077 // incompatible return types.
aoqi@0 4078 chk.checkCompatibleAbstracts(warn.pos(), from, to);
aoqi@0 4079 if (!isReifiable(target) &&
aoqi@0 4080 (reverse ? giveWarning(t2, t1) : giveWarning(t1, t2)))
aoqi@0 4081 warn.warn(LintCategory.UNCHECKED);
aoqi@0 4082 return true;
aoqi@0 4083 }
aoqi@0 4084
aoqi@0 4085 private boolean giveWarning(Type from, Type to) {
aoqi@0 4086 List<Type> bounds = to.isCompound() ?
aoqi@0 4087 ((IntersectionClassType)to.unannotatedType()).getComponents() : List.of(to);
aoqi@0 4088 for (Type b : bounds) {
aoqi@0 4089 Type subFrom = asSub(from, b.tsym);
aoqi@0 4090 if (b.isParameterized() &&
aoqi@0 4091 (!(isUnbounded(b) ||
aoqi@0 4092 isSubtype(from, b) ||
aoqi@0 4093 ((subFrom != null) && containsType(b.allparams(), subFrom.allparams()))))) {
aoqi@0 4094 return true;
aoqi@0 4095 }
aoqi@0 4096 }
aoqi@0 4097 return false;
aoqi@0 4098 }
aoqi@0 4099
aoqi@0 4100 private List<Type> superClosure(Type t, Type s) {
aoqi@0 4101 List<Type> cl = List.nil();
aoqi@0 4102 for (List<Type> l = interfaces(t); l.nonEmpty(); l = l.tail) {
aoqi@0 4103 if (isSubtype(s, erasure(l.head))) {
aoqi@0 4104 cl = insert(cl, l.head);
aoqi@0 4105 } else {
aoqi@0 4106 cl = union(cl, superClosure(l.head, s));
aoqi@0 4107 }
aoqi@0 4108 }
aoqi@0 4109 return cl;
aoqi@0 4110 }
aoqi@0 4111
aoqi@0 4112 private boolean containsTypeEquivalent(Type t, Type s) {
aoqi@0 4113 return
aoqi@0 4114 isSameType(t, s) || // shortcut
aoqi@0 4115 containsType(t, s) && containsType(s, t);
aoqi@0 4116 }
aoqi@0 4117
aoqi@0 4118 // <editor-fold defaultstate="collapsed" desc="adapt">
aoqi@0 4119 /**
aoqi@0 4120 * Adapt a type by computing a substitution which maps a source
aoqi@0 4121 * type to a target type.
aoqi@0 4122 *
aoqi@0 4123 * @param source the source type
aoqi@0 4124 * @param target the target type
aoqi@0 4125 * @param from the type variables of the computed substitution
aoqi@0 4126 * @param to the types of the computed substitution.
aoqi@0 4127 */
aoqi@0 4128 public void adapt(Type source,
aoqi@0 4129 Type target,
aoqi@0 4130 ListBuffer<Type> from,
aoqi@0 4131 ListBuffer<Type> to) throws AdaptFailure {
aoqi@0 4132 new Adapter(from, to).adapt(source, target);
aoqi@0 4133 }
aoqi@0 4134
aoqi@0 4135 class Adapter extends SimpleVisitor<Void, Type> {
aoqi@0 4136
aoqi@0 4137 ListBuffer<Type> from;
aoqi@0 4138 ListBuffer<Type> to;
aoqi@0 4139 Map<Symbol,Type> mapping;
aoqi@0 4140
aoqi@0 4141 Adapter(ListBuffer<Type> from, ListBuffer<Type> to) {
aoqi@0 4142 this.from = from;
aoqi@0 4143 this.to = to;
aoqi@0 4144 mapping = new HashMap<Symbol,Type>();
aoqi@0 4145 }
aoqi@0 4146
aoqi@0 4147 public void adapt(Type source, Type target) throws AdaptFailure {
aoqi@0 4148 visit(source, target);
aoqi@0 4149 List<Type> fromList = from.toList();
aoqi@0 4150 List<Type> toList = to.toList();
aoqi@0 4151 while (!fromList.isEmpty()) {
aoqi@0 4152 Type val = mapping.get(fromList.head.tsym);
aoqi@0 4153 if (toList.head != val)
aoqi@0 4154 toList.head = val;
aoqi@0 4155 fromList = fromList.tail;
aoqi@0 4156 toList = toList.tail;
aoqi@0 4157 }
aoqi@0 4158 }
aoqi@0 4159
aoqi@0 4160 @Override
aoqi@0 4161 public Void visitClassType(ClassType source, Type target) throws AdaptFailure {
aoqi@0 4162 if (target.hasTag(CLASS))
aoqi@0 4163 adaptRecursive(source.allparams(), target.allparams());
aoqi@0 4164 return null;
aoqi@0 4165 }
aoqi@0 4166
aoqi@0 4167 @Override
aoqi@0 4168 public Void visitArrayType(ArrayType source, Type target) throws AdaptFailure {
aoqi@0 4169 if (target.hasTag(ARRAY))
aoqi@0 4170 adaptRecursive(elemtype(source), elemtype(target));
aoqi@0 4171 return null;
aoqi@0 4172 }
aoqi@0 4173
aoqi@0 4174 @Override
aoqi@0 4175 public Void visitWildcardType(WildcardType source, Type target) throws AdaptFailure {
aoqi@0 4176 if (source.isExtendsBound())
aoqi@0 4177 adaptRecursive(wildUpperBound(source), wildUpperBound(target));
aoqi@0 4178 else if (source.isSuperBound())
aoqi@0 4179 adaptRecursive(wildLowerBound(source), wildLowerBound(target));
aoqi@0 4180 return null;
aoqi@0 4181 }
aoqi@0 4182
aoqi@0 4183 @Override
aoqi@0 4184 public Void visitTypeVar(TypeVar source, Type target) throws AdaptFailure {
aoqi@0 4185 // Check to see if there is
aoqi@0 4186 // already a mapping for $source$, in which case
aoqi@0 4187 // the old mapping will be merged with the new
aoqi@0 4188 Type val = mapping.get(source.tsym);
aoqi@0 4189 if (val != null) {
aoqi@0 4190 if (val.isSuperBound() && target.isSuperBound()) {
aoqi@0 4191 val = isSubtype(wildLowerBound(val), wildLowerBound(target))
aoqi@0 4192 ? target : val;
aoqi@0 4193 } else if (val.isExtendsBound() && target.isExtendsBound()) {
aoqi@0 4194 val = isSubtype(wildUpperBound(val), wildUpperBound(target))
aoqi@0 4195 ? val : target;
aoqi@0 4196 } else if (!isSameType(val, target)) {
aoqi@0 4197 throw new AdaptFailure();
aoqi@0 4198 }
aoqi@0 4199 } else {
aoqi@0 4200 val = target;
aoqi@0 4201 from.append(source);
aoqi@0 4202 to.append(target);
aoqi@0 4203 }
aoqi@0 4204 mapping.put(source.tsym, val);
aoqi@0 4205 return null;
aoqi@0 4206 }
aoqi@0 4207
aoqi@0 4208 @Override
aoqi@0 4209 public Void visitType(Type source, Type target) {
aoqi@0 4210 return null;
aoqi@0 4211 }
aoqi@0 4212
aoqi@0 4213 private Set<TypePair> cache = new HashSet<TypePair>();
aoqi@0 4214
aoqi@0 4215 private void adaptRecursive(Type source, Type target) {
aoqi@0 4216 TypePair pair = new TypePair(source, target);
aoqi@0 4217 if (cache.add(pair)) {
aoqi@0 4218 try {
aoqi@0 4219 visit(source, target);
aoqi@0 4220 } finally {
aoqi@0 4221 cache.remove(pair);
aoqi@0 4222 }
aoqi@0 4223 }
aoqi@0 4224 }
aoqi@0 4225
aoqi@0 4226 private void adaptRecursive(List<Type> source, List<Type> target) {
aoqi@0 4227 if (source.length() == target.length()) {
aoqi@0 4228 while (source.nonEmpty()) {
aoqi@0 4229 adaptRecursive(source.head, target.head);
aoqi@0 4230 source = source.tail;
aoqi@0 4231 target = target.tail;
aoqi@0 4232 }
aoqi@0 4233 }
aoqi@0 4234 }
aoqi@0 4235 }
aoqi@0 4236
aoqi@0 4237 public static class AdaptFailure extends RuntimeException {
aoqi@0 4238 static final long serialVersionUID = -7490231548272701566L;
aoqi@0 4239 }
aoqi@0 4240
aoqi@0 4241 private void adaptSelf(Type t,
aoqi@0 4242 ListBuffer<Type> from,
aoqi@0 4243 ListBuffer<Type> to) {
aoqi@0 4244 try {
aoqi@0 4245 //if (t.tsym.type != t)
aoqi@0 4246 adapt(t.tsym.type, t, from, to);
aoqi@0 4247 } catch (AdaptFailure ex) {
aoqi@0 4248 // Adapt should never fail calculating a mapping from
aoqi@0 4249 // t.tsym.type to t as there can be no merge problem.
aoqi@0 4250 throw new AssertionError(ex);
aoqi@0 4251 }
aoqi@0 4252 }
aoqi@0 4253 // </editor-fold>
aoqi@0 4254
aoqi@0 4255 /**
aoqi@0 4256 * Rewrite all type variables (universal quantifiers) in the given
aoqi@0 4257 * type to wildcards (existential quantifiers). This is used to
aoqi@0 4258 * determine if a cast is allowed. For example, if high is true
aoqi@0 4259 * and {@code T <: Number}, then {@code List<T>} is rewritten to
aoqi@0 4260 * {@code List<? extends Number>}. Since {@code List<Integer> <:
aoqi@0 4261 * List<? extends Number>} a {@code List<T>} can be cast to {@code
aoqi@0 4262 * List<Integer>} with a warning.
aoqi@0 4263 * @param t a type
aoqi@0 4264 * @param high if true return an upper bound; otherwise a lower
aoqi@0 4265 * bound
aoqi@0 4266 * @param rewriteTypeVars only rewrite captured wildcards if false;
aoqi@0 4267 * otherwise rewrite all type variables
aoqi@0 4268 * @return the type rewritten with wildcards (existential
aoqi@0 4269 * quantifiers) only
aoqi@0 4270 */
aoqi@0 4271 private Type rewriteQuantifiers(Type t, boolean high, boolean rewriteTypeVars) {
aoqi@0 4272 return new Rewriter(high, rewriteTypeVars).visit(t);
aoqi@0 4273 }
aoqi@0 4274
aoqi@0 4275 class Rewriter extends UnaryVisitor<Type> {
aoqi@0 4276
aoqi@0 4277 boolean high;
aoqi@0 4278 boolean rewriteTypeVars;
aoqi@0 4279
aoqi@0 4280 Rewriter(boolean high, boolean rewriteTypeVars) {
aoqi@0 4281 this.high = high;
aoqi@0 4282 this.rewriteTypeVars = rewriteTypeVars;
aoqi@0 4283 }
aoqi@0 4284
aoqi@0 4285 @Override
aoqi@0 4286 public Type visitClassType(ClassType t, Void s) {
aoqi@0 4287 ListBuffer<Type> rewritten = new ListBuffer<Type>();
aoqi@0 4288 boolean changed = false;
aoqi@0 4289 for (Type arg : t.allparams()) {
aoqi@0 4290 Type bound = visit(arg);
aoqi@0 4291 if (arg != bound) {
aoqi@0 4292 changed = true;
aoqi@0 4293 }
aoqi@0 4294 rewritten.append(bound);
aoqi@0 4295 }
aoqi@0 4296 if (changed)
aoqi@0 4297 return subst(t.tsym.type,
aoqi@0 4298 t.tsym.type.allparams(),
aoqi@0 4299 rewritten.toList());
aoqi@0 4300 else
aoqi@0 4301 return t;
aoqi@0 4302 }
aoqi@0 4303
aoqi@0 4304 public Type visitType(Type t, Void s) {
aoqi@0 4305 return t;
aoqi@0 4306 }
aoqi@0 4307
aoqi@0 4308 @Override
aoqi@0 4309 public Type visitCapturedType(CapturedType t, Void s) {
aoqi@0 4310 Type w_bound = t.wildcard.type;
aoqi@0 4311 Type bound = w_bound.contains(t) ?
aoqi@0 4312 erasure(w_bound) :
aoqi@0 4313 visit(w_bound);
aoqi@0 4314 return rewriteAsWildcardType(visit(bound), t.wildcard.bound, t.wildcard.kind);
aoqi@0 4315 }
aoqi@0 4316
aoqi@0 4317 @Override
aoqi@0 4318 public Type visitTypeVar(TypeVar t, Void s) {
aoqi@0 4319 if (rewriteTypeVars) {
aoqi@0 4320 Type bound = t.bound.contains(t) ?
aoqi@0 4321 erasure(t.bound) :
aoqi@0 4322 visit(t.bound);
aoqi@0 4323 return rewriteAsWildcardType(bound, t, EXTENDS);
aoqi@0 4324 } else {
aoqi@0 4325 return t;
aoqi@0 4326 }
aoqi@0 4327 }
aoqi@0 4328
aoqi@0 4329 @Override
aoqi@0 4330 public Type visitWildcardType(WildcardType t, Void s) {
aoqi@0 4331 Type bound2 = visit(t.type);
aoqi@0 4332 return t.type == bound2 ? t : rewriteAsWildcardType(bound2, t.bound, t.kind);
aoqi@0 4333 }
aoqi@0 4334
aoqi@0 4335 private Type rewriteAsWildcardType(Type bound, TypeVar formal, BoundKind bk) {
aoqi@0 4336 switch (bk) {
aoqi@0 4337 case EXTENDS: return high ?
aoqi@0 4338 makeExtendsWildcard(B(bound), formal) :
aoqi@0 4339 makeExtendsWildcard(syms.objectType, formal);
aoqi@0 4340 case SUPER: return high ?
aoqi@0 4341 makeSuperWildcard(syms.botType, formal) :
aoqi@0 4342 makeSuperWildcard(B(bound), formal);
aoqi@0 4343 case UNBOUND: return makeExtendsWildcard(syms.objectType, formal);
aoqi@0 4344 default:
aoqi@0 4345 Assert.error("Invalid bound kind " + bk);
aoqi@0 4346 return null;
aoqi@0 4347 }
aoqi@0 4348 }
aoqi@0 4349
aoqi@0 4350 Type B(Type t) {
aoqi@0 4351 while (t.hasTag(WILDCARD)) {
aoqi@0 4352 WildcardType w = (WildcardType)t.unannotatedType();
aoqi@0 4353 t = high ?
aoqi@0 4354 w.getExtendsBound() :
aoqi@0 4355 w.getSuperBound();
aoqi@0 4356 if (t == null) {
aoqi@0 4357 t = high ? syms.objectType : syms.botType;
aoqi@0 4358 }
aoqi@0 4359 }
aoqi@0 4360 return t;
aoqi@0 4361 }
aoqi@0 4362 }
aoqi@0 4363
aoqi@0 4364
aoqi@0 4365 /**
aoqi@0 4366 * Create a wildcard with the given upper (extends) bound; create
aoqi@0 4367 * an unbounded wildcard if bound is Object.
aoqi@0 4368 *
aoqi@0 4369 * @param bound the upper bound
aoqi@0 4370 * @param formal the formal type parameter that will be
aoqi@0 4371 * substituted by the wildcard
aoqi@0 4372 */
aoqi@0 4373 private WildcardType makeExtendsWildcard(Type bound, TypeVar formal) {
aoqi@0 4374 if (bound == syms.objectType) {
aoqi@0 4375 return new WildcardType(syms.objectType,
aoqi@0 4376 BoundKind.UNBOUND,
aoqi@0 4377 syms.boundClass,
aoqi@0 4378 formal);
aoqi@0 4379 } else {
aoqi@0 4380 return new WildcardType(bound,
aoqi@0 4381 BoundKind.EXTENDS,
aoqi@0 4382 syms.boundClass,
aoqi@0 4383 formal);
aoqi@0 4384 }
aoqi@0 4385 }
aoqi@0 4386
aoqi@0 4387 /**
aoqi@0 4388 * Create a wildcard with the given lower (super) bound; create an
aoqi@0 4389 * unbounded wildcard if bound is bottom (type of {@code null}).
aoqi@0 4390 *
aoqi@0 4391 * @param bound the lower bound
aoqi@0 4392 * @param formal the formal type parameter that will be
aoqi@0 4393 * substituted by the wildcard
aoqi@0 4394 */
aoqi@0 4395 private WildcardType makeSuperWildcard(Type bound, TypeVar formal) {
aoqi@0 4396 if (bound.hasTag(BOT)) {
aoqi@0 4397 return new WildcardType(syms.objectType,
aoqi@0 4398 BoundKind.UNBOUND,
aoqi@0 4399 syms.boundClass,
aoqi@0 4400 formal);
aoqi@0 4401 } else {
aoqi@0 4402 return new WildcardType(bound,
aoqi@0 4403 BoundKind.SUPER,
aoqi@0 4404 syms.boundClass,
aoqi@0 4405 formal);
aoqi@0 4406 }
aoqi@0 4407 }
aoqi@0 4408
aoqi@0 4409 /**
aoqi@0 4410 * A wrapper for a type that allows use in sets.
aoqi@0 4411 */
aoqi@0 4412 public static class UniqueType {
aoqi@0 4413 public final Type type;
aoqi@0 4414 final Types types;
aoqi@0 4415
aoqi@0 4416 public UniqueType(Type type, Types types) {
aoqi@0 4417 this.type = type;
aoqi@0 4418 this.types = types;
aoqi@0 4419 }
aoqi@0 4420
aoqi@0 4421 public int hashCode() {
aoqi@0 4422 return types.hashCode(type);
aoqi@0 4423 }
aoqi@0 4424
aoqi@0 4425 public boolean equals(Object obj) {
aoqi@0 4426 return (obj instanceof UniqueType) &&
aoqi@0 4427 types.isSameAnnotatedType(type, ((UniqueType)obj).type);
aoqi@0 4428 }
aoqi@0 4429
aoqi@0 4430 public String toString() {
aoqi@0 4431 return type.toString();
aoqi@0 4432 }
aoqi@0 4433
aoqi@0 4434 }
aoqi@0 4435 // </editor-fold>
aoqi@0 4436
aoqi@0 4437 // <editor-fold defaultstate="collapsed" desc="Visitors">
aoqi@0 4438 /**
aoqi@0 4439 * A default visitor for types. All visitor methods except
aoqi@0 4440 * visitType are implemented by delegating to visitType. Concrete
aoqi@0 4441 * subclasses must provide an implementation of visitType and can
aoqi@0 4442 * override other methods as needed.
aoqi@0 4443 *
aoqi@0 4444 * @param <R> the return type of the operation implemented by this
aoqi@0 4445 * visitor; use Void if no return type is needed.
aoqi@0 4446 * @param <S> the type of the second argument (the first being the
aoqi@0 4447 * type itself) of the operation implemented by this visitor; use
aoqi@0 4448 * Void if a second argument is not needed.
aoqi@0 4449 */
aoqi@0 4450 public static abstract class DefaultTypeVisitor<R,S> implements Type.Visitor<R,S> {
aoqi@0 4451 final public R visit(Type t, S s) { return t.accept(this, s); }
aoqi@0 4452 public R visitClassType(ClassType t, S s) { return visitType(t, s); }
aoqi@0 4453 public R visitWildcardType(WildcardType t, S s) { return visitType(t, s); }
aoqi@0 4454 public R visitArrayType(ArrayType t, S s) { return visitType(t, s); }
aoqi@0 4455 public R visitMethodType(MethodType t, S s) { return visitType(t, s); }
aoqi@0 4456 public R visitPackageType(PackageType t, S s) { return visitType(t, s); }
aoqi@0 4457 public R visitTypeVar(TypeVar t, S s) { return visitType(t, s); }
aoqi@0 4458 public R visitCapturedType(CapturedType t, S s) { return visitType(t, s); }
aoqi@0 4459 public R visitForAll(ForAll t, S s) { return visitType(t, s); }
aoqi@0 4460 public R visitUndetVar(UndetVar t, S s) { return visitType(t, s); }
aoqi@0 4461 public R visitErrorType(ErrorType t, S s) { return visitType(t, s); }
aoqi@0 4462 // Pretend annotations don't exist
aoqi@0 4463 public R visitAnnotatedType(AnnotatedType t, S s) { return visit(t.unannotatedType(), s); }
aoqi@0 4464 }
aoqi@0 4465
aoqi@0 4466 /**
aoqi@0 4467 * A default visitor for symbols. All visitor methods except
aoqi@0 4468 * visitSymbol are implemented by delegating to visitSymbol. Concrete
aoqi@0 4469 * subclasses must provide an implementation of visitSymbol 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 * symbol 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 DefaultSymbolVisitor<R,S> implements Symbol.Visitor<R,S> {
aoqi@0 4479 final public R visit(Symbol s, S arg) { return s.accept(this, arg); }
aoqi@0 4480 public R visitClassSymbol(ClassSymbol s, S arg) { return visitSymbol(s, arg); }
aoqi@0 4481 public R visitMethodSymbol(MethodSymbol s, S arg) { return visitSymbol(s, arg); }
aoqi@0 4482 public R visitOperatorSymbol(OperatorSymbol s, S arg) { return visitSymbol(s, arg); }
aoqi@0 4483 public R visitPackageSymbol(PackageSymbol s, S arg) { return visitSymbol(s, arg); }
aoqi@0 4484 public R visitTypeSymbol(TypeSymbol s, S arg) { return visitSymbol(s, arg); }
aoqi@0 4485 public R visitVarSymbol(VarSymbol s, S arg) { return visitSymbol(s, arg); }
aoqi@0 4486 }
aoqi@0 4487
aoqi@0 4488 /**
aoqi@0 4489 * A <em>simple</em> visitor for types. This visitor is simple as
aoqi@0 4490 * captured wildcards, for-all types (generic methods), and
aoqi@0 4491 * undetermined type variables (part of inference) are hidden.
aoqi@0 4492 * Captured wildcards are hidden by treating them as type
aoqi@0 4493 * variables and the rest are hidden by visiting their qtypes.
aoqi@0 4494 *
aoqi@0 4495 * @param <R> the return type of the operation implemented by this
aoqi@0 4496 * visitor; use Void if no return type is needed.
aoqi@0 4497 * @param <S> the type of the second argument (the first being the
aoqi@0 4498 * type itself) of the operation implemented by this visitor; use
aoqi@0 4499 * Void if a second argument is not needed.
aoqi@0 4500 */
aoqi@0 4501 public static abstract class SimpleVisitor<R,S> extends DefaultTypeVisitor<R,S> {
aoqi@0 4502 @Override
aoqi@0 4503 public R visitCapturedType(CapturedType t, S s) {
aoqi@0 4504 return visitTypeVar(t, s);
aoqi@0 4505 }
aoqi@0 4506 @Override
aoqi@0 4507 public R visitForAll(ForAll t, S s) {
aoqi@0 4508 return visit(t.qtype, s);
aoqi@0 4509 }
aoqi@0 4510 @Override
aoqi@0 4511 public R visitUndetVar(UndetVar t, S s) {
aoqi@0 4512 return visit(t.qtype, s);
aoqi@0 4513 }
aoqi@0 4514 }
aoqi@0 4515
aoqi@0 4516 /**
aoqi@0 4517 * A plain relation on types. That is a 2-ary function on the
aoqi@0 4518 * form Type&nbsp;&times;&nbsp;Type&nbsp;&rarr;&nbsp;Boolean.
aoqi@0 4519 * <!-- In plain text: Type x Type -> Boolean -->
aoqi@0 4520 */
aoqi@0 4521 public static abstract class TypeRelation extends SimpleVisitor<Boolean,Type> {}
aoqi@0 4522
aoqi@0 4523 /**
aoqi@0 4524 * A convenience visitor for implementing operations that only
aoqi@0 4525 * require one argument (the type itself), that is, unary
aoqi@0 4526 * operations.
aoqi@0 4527 *
aoqi@0 4528 * @param <R> the return type of the operation implemented by this
aoqi@0 4529 * visitor; use Void if no return type is needed.
aoqi@0 4530 */
aoqi@0 4531 public static abstract class UnaryVisitor<R> extends SimpleVisitor<R,Void> {
aoqi@0 4532 final public R visit(Type t) { return t.accept(this, null); }
aoqi@0 4533 }
aoqi@0 4534
aoqi@0 4535 /**
aoqi@0 4536 * A visitor for implementing a mapping from types to types. The
aoqi@0 4537 * default behavior of this class is to implement the identity
aoqi@0 4538 * mapping (mapping a type to itself). This can be overridden in
aoqi@0 4539 * subclasses.
aoqi@0 4540 *
aoqi@0 4541 * @param <S> the type of the second argument (the first being the
aoqi@0 4542 * type itself) of this mapping; use Void if a second argument is
aoqi@0 4543 * not needed.
aoqi@0 4544 */
aoqi@0 4545 public static class MapVisitor<S> extends DefaultTypeVisitor<Type,S> {
aoqi@0 4546 final public Type visit(Type t) { return t.accept(this, null); }
aoqi@0 4547 public Type visitType(Type t, S s) { return t; }
aoqi@0 4548 }
aoqi@0 4549 // </editor-fold>
aoqi@0 4550
aoqi@0 4551
aoqi@0 4552 // <editor-fold defaultstate="collapsed" desc="Annotation support">
aoqi@0 4553
aoqi@0 4554 public RetentionPolicy getRetention(Attribute.Compound a) {
aoqi@0 4555 return getRetention(a.type.tsym);
aoqi@0 4556 }
aoqi@0 4557
aoqi@0 4558 public RetentionPolicy getRetention(Symbol sym) {
aoqi@0 4559 RetentionPolicy vis = RetentionPolicy.CLASS; // the default
aoqi@0 4560 Attribute.Compound c = sym.attribute(syms.retentionType.tsym);
aoqi@0 4561 if (c != null) {
aoqi@0 4562 Attribute value = c.member(names.value);
aoqi@0 4563 if (value != null && value instanceof Attribute.Enum) {
aoqi@0 4564 Name levelName = ((Attribute.Enum)value).value.name;
aoqi@0 4565 if (levelName == names.SOURCE) vis = RetentionPolicy.SOURCE;
aoqi@0 4566 else if (levelName == names.CLASS) vis = RetentionPolicy.CLASS;
aoqi@0 4567 else if (levelName == names.RUNTIME) vis = RetentionPolicy.RUNTIME;
aoqi@0 4568 else ;// /* fail soft */ throw new AssertionError(levelName);
aoqi@0 4569 }
aoqi@0 4570 }
aoqi@0 4571 return vis;
aoqi@0 4572 }
aoqi@0 4573 // </editor-fold>
aoqi@0 4574
aoqi@0 4575 // <editor-fold defaultstate="collapsed" desc="Signature Generation">
aoqi@0 4576
aoqi@0 4577 public static abstract class SignatureGenerator {
aoqi@0 4578
aoqi@0 4579 private final Types types;
aoqi@0 4580
aoqi@0 4581 protected abstract void append(char ch);
aoqi@0 4582 protected abstract void append(byte[] ba);
aoqi@0 4583 protected abstract void append(Name name);
aoqi@0 4584 protected void classReference(ClassSymbol c) { /* by default: no-op */ }
aoqi@0 4585
aoqi@0 4586 protected SignatureGenerator(Types types) {
aoqi@0 4587 this.types = types;
aoqi@0 4588 }
aoqi@0 4589
aoqi@0 4590 /**
aoqi@0 4591 * Assemble signature of given type in string buffer.
aoqi@0 4592 */
aoqi@0 4593 public void assembleSig(Type type) {
aoqi@0 4594 type = type.unannotatedType();
aoqi@0 4595 switch (type.getTag()) {
aoqi@0 4596 case BYTE:
aoqi@0 4597 append('B');
aoqi@0 4598 break;
aoqi@0 4599 case SHORT:
aoqi@0 4600 append('S');
aoqi@0 4601 break;
aoqi@0 4602 case CHAR:
aoqi@0 4603 append('C');
aoqi@0 4604 break;
aoqi@0 4605 case INT:
aoqi@0 4606 append('I');
aoqi@0 4607 break;
aoqi@0 4608 case LONG:
aoqi@0 4609 append('J');
aoqi@0 4610 break;
aoqi@0 4611 case FLOAT:
aoqi@0 4612 append('F');
aoqi@0 4613 break;
aoqi@0 4614 case DOUBLE:
aoqi@0 4615 append('D');
aoqi@0 4616 break;
aoqi@0 4617 case BOOLEAN:
aoqi@0 4618 append('Z');
aoqi@0 4619 break;
aoqi@0 4620 case VOID:
aoqi@0 4621 append('V');
aoqi@0 4622 break;
aoqi@0 4623 case CLASS:
aoqi@0 4624 append('L');
aoqi@0 4625 assembleClassSig(type);
aoqi@0 4626 append(';');
aoqi@0 4627 break;
aoqi@0 4628 case ARRAY:
aoqi@0 4629 ArrayType at = (ArrayType) type;
aoqi@0 4630 append('[');
aoqi@0 4631 assembleSig(at.elemtype);
aoqi@0 4632 break;
aoqi@0 4633 case METHOD:
aoqi@0 4634 MethodType mt = (MethodType) type;
aoqi@0 4635 append('(');
aoqi@0 4636 assembleSig(mt.argtypes);
aoqi@0 4637 append(')');
aoqi@0 4638 assembleSig(mt.restype);
aoqi@0 4639 if (hasTypeVar(mt.thrown)) {
aoqi@0 4640 for (List<Type> l = mt.thrown; l.nonEmpty(); l = l.tail) {
aoqi@0 4641 append('^');
aoqi@0 4642 assembleSig(l.head);
aoqi@0 4643 }
aoqi@0 4644 }
aoqi@0 4645 break;
aoqi@0 4646 case WILDCARD: {
aoqi@0 4647 Type.WildcardType ta = (Type.WildcardType) type;
aoqi@0 4648 switch (ta.kind) {
aoqi@0 4649 case SUPER:
aoqi@0 4650 append('-');
aoqi@0 4651 assembleSig(ta.type);
aoqi@0 4652 break;
aoqi@0 4653 case EXTENDS:
aoqi@0 4654 append('+');
aoqi@0 4655 assembleSig(ta.type);
aoqi@0 4656 break;
aoqi@0 4657 case UNBOUND:
aoqi@0 4658 append('*');
aoqi@0 4659 break;
aoqi@0 4660 default:
aoqi@0 4661 throw new AssertionError(ta.kind);
aoqi@0 4662 }
aoqi@0 4663 break;
aoqi@0 4664 }
aoqi@0 4665 case TYPEVAR:
aoqi@0 4666 append('T');
aoqi@0 4667 append(type.tsym.name);
aoqi@0 4668 append(';');
aoqi@0 4669 break;
aoqi@0 4670 case FORALL:
aoqi@0 4671 Type.ForAll ft = (Type.ForAll) type;
aoqi@0 4672 assembleParamsSig(ft.tvars);
aoqi@0 4673 assembleSig(ft.qtype);
aoqi@0 4674 break;
aoqi@0 4675 default:
aoqi@0 4676 throw new AssertionError("typeSig " + type.getTag());
aoqi@0 4677 }
aoqi@0 4678 }
aoqi@0 4679
aoqi@0 4680 public boolean hasTypeVar(List<Type> l) {
aoqi@0 4681 while (l.nonEmpty()) {
aoqi@0 4682 if (l.head.hasTag(TypeTag.TYPEVAR)) {
aoqi@0 4683 return true;
aoqi@0 4684 }
aoqi@0 4685 l = l.tail;
aoqi@0 4686 }
aoqi@0 4687 return false;
aoqi@0 4688 }
aoqi@0 4689
aoqi@0 4690 public void assembleClassSig(Type type) {
aoqi@0 4691 type = type.unannotatedType();
aoqi@0 4692 ClassType ct = (ClassType) type;
aoqi@0 4693 ClassSymbol c = (ClassSymbol) ct.tsym;
aoqi@0 4694 classReference(c);
aoqi@0 4695 Type outer = ct.getEnclosingType();
aoqi@0 4696 if (outer.allparams().nonEmpty()) {
aoqi@0 4697 boolean rawOuter =
aoqi@0 4698 c.owner.kind == Kinds.MTH || // either a local class
aoqi@0 4699 c.name == types.names.empty; // or anonymous
aoqi@0 4700 assembleClassSig(rawOuter
aoqi@0 4701 ? types.erasure(outer)
aoqi@0 4702 : outer);
aoqi@0 4703 append(rawOuter ? '$' : '.');
aoqi@0 4704 Assert.check(c.flatname.startsWith(c.owner.enclClass().flatname));
aoqi@0 4705 append(rawOuter
aoqi@0 4706 ? c.flatname.subName(c.owner.enclClass().flatname.getByteLength() + 1, c.flatname.getByteLength())
aoqi@0 4707 : c.name);
aoqi@0 4708 } else {
aoqi@0 4709 append(externalize(c.flatname));
aoqi@0 4710 }
aoqi@0 4711 if (ct.getTypeArguments().nonEmpty()) {
aoqi@0 4712 append('<');
aoqi@0 4713 assembleSig(ct.getTypeArguments());
aoqi@0 4714 append('>');
aoqi@0 4715 }
aoqi@0 4716 }
aoqi@0 4717
aoqi@0 4718 public void assembleParamsSig(List<Type> typarams) {
aoqi@0 4719 append('<');
aoqi@0 4720 for (List<Type> ts = typarams; ts.nonEmpty(); ts = ts.tail) {
aoqi@0 4721 Type.TypeVar tvar = (Type.TypeVar) ts.head;
aoqi@0 4722 append(tvar.tsym.name);
aoqi@0 4723 List<Type> bounds = types.getBounds(tvar);
aoqi@0 4724 if ((bounds.head.tsym.flags() & INTERFACE) != 0) {
aoqi@0 4725 append(':');
aoqi@0 4726 }
aoqi@0 4727 for (List<Type> l = bounds; l.nonEmpty(); l = l.tail) {
aoqi@0 4728 append(':');
aoqi@0 4729 assembleSig(l.head);
aoqi@0 4730 }
aoqi@0 4731 }
aoqi@0 4732 append('>');
aoqi@0 4733 }
aoqi@0 4734
aoqi@0 4735 private void assembleSig(List<Type> types) {
aoqi@0 4736 for (List<Type> ts = types; ts.nonEmpty(); ts = ts.tail) {
aoqi@0 4737 assembleSig(ts.head);
aoqi@0 4738 }
aoqi@0 4739 }
aoqi@0 4740 }
aoqi@0 4741 // </editor-fold>
aoqi@0 4742 }

mercurial