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

Fri, 22 Feb 2013 18:19:51 +0000

author
mcimadamore
date
Fri, 22 Feb 2013 18:19:51 +0000
changeset 1605
94e67bed460d
parent 1600
3fef0cae83b3
child 1644
40adaf938847
permissions
-rw-r--r--

8008708: Regression: separate compilation causes crash in wildcards inference logic
Summary: Invalid use of WildcardType.bound in Types.removeWildcards
Reviewed-by: jjg

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

mercurial