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

Wed, 19 Mar 2014 16:44:49 +0000

author
vromero
date
Wed, 19 Mar 2014 16:44:49 +0000
changeset 2301
27a3026256cd
parent 2260
fb870c70e774
child 2382
14979dd5e034
permissions
-rw-r--r--

8034924: Incorrect inheritance of inaccessible static method
Reviewed-by: jjg, jlahoda

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

mercurial