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

Fri, 13 Jun 2014 13:30:29 -0600

author
dlsmith
date
Fri, 13 Jun 2014 13:30:29 -0600
changeset 2420
1aeb322cf646
parent 2416
63ef1e0410d1
child 2422
4ee06c77b51b
permissions
-rw-r--r--

8046762: Revert some inference fixes in JDK-8033718
Reviewed-by: mcimadamore

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

mercurial