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

Fri, 30 Nov 2012 15:14:36 +0000

author
mcimadamore
date
Fri, 30 Nov 2012 15:14:36 +0000
changeset 1435
9b26c96f5138
parent 1434
34d1ebaf4645
child 1436
f6f1fd261f57
permissions
-rw-r--r--

8004101: Add checks for method reference well-formedness
Summary: Bring method reference type-checking in sync with latest EDR
Reviewed-by: jjg

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

mercurial