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

Wed, 17 Jul 2013 14:14:49 +0100

author
mcimadamore
date
Wed, 17 Jul 2013 14:14:49 +0100
changeset 1901
db2c539819dd
parent 1869
5c548a8542b8
child 1902
fae8f309ff80
permissions
-rw-r--r--

7041019: Bogus type-variable substitution with array types with dependencies on accessibility check
Summary: call to upperBound() when performing type-variable substitution on element type leads to unsoundness
Reviewed-by: jjg

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

mercurial