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

Tue, 18 Mar 2014 22:12:46 +0000

author
vromero
date
Tue, 18 Mar 2014 22:12:46 +0000
changeset 2434
7de1481c6cd8
parent 2431
37c7dbe8efee
child 2525
2eb010b6cb22
child 2543
c6d5efccedc3
permissions
-rw-r--r--

8036007: javac crashes when encountering an unresolvable interface
Reviewed-by: vromero, jlahoda
Contributed-by: paul.govereau@oracle.com

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

mercurial