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

Fri, 23 May 2014 16:27:22 +0100

author
pgovereau
date
Fri, 23 May 2014 16:27:22 +0100
changeset 2398
7c925f35f81c
parent 2385
856d94394294
child 2400
0e026d3f2786
permissions
-rw-r--r--

8033437: javac, inconsistent generic types behaviour when compiling together vs. separate
Reviewed-by: jjg
Contributed-by: vicente.romero@oracle.com, paul.govereau@oracle.com

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

mercurial