src/share/classes/com/sun/tools/javac/comp/Resolve.java

Thu, 04 Oct 2012 13:04:53 +0100

author
mcimadamore
date
Thu, 04 Oct 2012 13:04:53 +0100
changeset 1347
1408af4cd8b0
parent 1346
20e4a54b1629
child 1348
573ceb23beeb
permissions
-rw-r--r--

7177387: Add target-typing support in method context
Summary: Add support for deferred types and speculative attribution
Reviewed-by: jjg, dlsmith

duke@1 1 /*
mcimadamore@1186 2 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.comp;
duke@1 27
mcimadamore@1114 28 import com.sun.tools.javac.api.Formattable.LocalizedString;
duke@1 29 import com.sun.tools.javac.code.*;
mcimadamore@1342 30 import com.sun.tools.javac.code.Symbol.*;
mcimadamore@1114 31 import com.sun.tools.javac.code.Type.*;
mcimadamore@1238 32 import com.sun.tools.javac.comp.Attr.ResultInfo;
mcimadamore@1238 33 import com.sun.tools.javac.comp.Check.CheckContext;
mcimadamore@1347 34 import com.sun.tools.javac.comp.DeferredAttr.AttrMode;
mcimadamore@1347 35 import com.sun.tools.javac.comp.DeferredAttr.DeferredAttrContext;
mcimadamore@1347 36 import com.sun.tools.javac.comp.DeferredAttr.DeferredType;
mcimadamore@1337 37 import com.sun.tools.javac.comp.Infer.InferenceContext;
mcimadamore@1337 38 import com.sun.tools.javac.comp.Infer.InferenceContext.FreeTypeListener;
mcimadamore@1215 39 import com.sun.tools.javac.comp.Resolve.MethodResolutionContext.Candidate;
duke@1 40 import com.sun.tools.javac.jvm.*;
duke@1 41 import com.sun.tools.javac.tree.*;
mcimadamore@1114 42 import com.sun.tools.javac.tree.JCTree.*;
mcimadamore@1114 43 import com.sun.tools.javac.util.*;
mcimadamore@1114 44 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
mcimadamore@1114 45 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
mcimadamore@1114 46 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
duke@1 47
mcimadamore@1114 48 import java.util.Arrays;
mcimadamore@1114 49 import java.util.Collection;
mcimadamore@1215 50 import java.util.EnumMap;
mcimadamore@1114 51 import java.util.EnumSet;
mcimadamore@1342 52 import java.util.Iterator;
mcimadamore@1114 53 import java.util.Map;
mcimadamore@1114 54
mcimadamore@1114 55 import javax.lang.model.element.ElementVisitor;
duke@1 56
duke@1 57 import static com.sun.tools.javac.code.Flags.*;
jjg@1127 58 import static com.sun.tools.javac.code.Flags.BLOCK;
duke@1 59 import static com.sun.tools.javac.code.Kinds.*;
jjg@1127 60 import static com.sun.tools.javac.code.Kinds.ERRONEOUS;
duke@1 61 import static com.sun.tools.javac.code.TypeTags.*;
mcimadamore@1114 62 import static com.sun.tools.javac.comp.Resolve.MethodResolutionPhase.*;
jjg@1127 63 import static com.sun.tools.javac.tree.JCTree.Tag.*;
mcimadamore@160 64
duke@1 65 /** Helper class for name resolution, used mostly by the attribution phase.
duke@1 66 *
jjg@581 67 * <p><b>This is NOT part of any supported API.
jjg@581 68 * If you write code that depends on this, you do so at your own risk.
duke@1 69 * This code and its internal interfaces are subject to change or
duke@1 70 * deletion without notice.</b>
duke@1 71 */
duke@1 72 public class Resolve {
duke@1 73 protected static final Context.Key<Resolve> resolveKey =
duke@1 74 new Context.Key<Resolve>();
duke@1 75
jjg@113 76 Names names;
duke@1 77 Log log;
duke@1 78 Symtab syms;
mcimadamore@1238 79 Attr attr;
mcimadamore@1347 80 DeferredAttr deferredAttr;
duke@1 81 Check chk;
duke@1 82 Infer infer;
duke@1 83 ClassReader reader;
duke@1 84 TreeInfo treeinfo;
duke@1 85 Types types;
mcimadamore@89 86 JCDiagnostic.Factory diags;
duke@1 87 public final boolean boxingEnabled; // = source.allowBoxing();
duke@1 88 public final boolean varargsEnabled; // = source.allowVarargs();
mcimadamore@674 89 public final boolean allowMethodHandles;
duke@1 90 private final boolean debugResolve;
mcimadamore@1114 91 final EnumSet<VerboseResolutionMode> verboseResolutionMode;
duke@1 92
mcimadamore@674 93 Scope polymorphicSignatureScope;
mcimadamore@674 94
mcimadamore@1215 95 protected Resolve(Context context) {
mcimadamore@1215 96 context.put(resolveKey, this);
mcimadamore@1215 97 syms = Symtab.instance(context);
mcimadamore@1215 98
mcimadamore@1215 99 varNotFound = new
mcimadamore@1215 100 SymbolNotFoundError(ABSENT_VAR);
mcimadamore@1215 101 wrongMethod = new
mcimadamore@1215 102 InapplicableSymbolError();
mcimadamore@1215 103 wrongMethods = new
mcimadamore@1215 104 InapplicableSymbolsError();
mcimadamore@1215 105 methodNotFound = new
mcimadamore@1215 106 SymbolNotFoundError(ABSENT_MTH);
mcimadamore@1215 107 typeNotFound = new
mcimadamore@1215 108 SymbolNotFoundError(ABSENT_TYP);
mcimadamore@1215 109
mcimadamore@1215 110 names = Names.instance(context);
mcimadamore@1215 111 log = Log.instance(context);
mcimadamore@1238 112 attr = Attr.instance(context);
mcimadamore@1347 113 deferredAttr = DeferredAttr.instance(context);
mcimadamore@1215 114 chk = Check.instance(context);
mcimadamore@1215 115 infer = Infer.instance(context);
mcimadamore@1215 116 reader = ClassReader.instance(context);
mcimadamore@1215 117 treeinfo = TreeInfo.instance(context);
mcimadamore@1215 118 types = Types.instance(context);
mcimadamore@1215 119 diags = JCDiagnostic.Factory.instance(context);
mcimadamore@1215 120 Source source = Source.instance(context);
mcimadamore@1215 121 boxingEnabled = source.allowBoxing();
mcimadamore@1215 122 varargsEnabled = source.allowVarargs();
mcimadamore@1215 123 Options options = Options.instance(context);
mcimadamore@1215 124 debugResolve = options.isSet("debugresolve");
mcimadamore@1215 125 verboseResolutionMode = VerboseResolutionMode.getVerboseResolutionMode(options);
mcimadamore@1215 126 Target target = Target.instance(context);
mcimadamore@1215 127 allowMethodHandles = target.hasMethodHandles();
mcimadamore@1215 128 polymorphicSignatureScope = new Scope(syms.noSymbol);
mcimadamore@1215 129
mcimadamore@1215 130 inapplicableMethodException = new InapplicableMethodException(diags);
mcimadamore@1215 131 }
mcimadamore@1215 132
mcimadamore@1215 133 /** error symbols, which are returned when resolution fails
mcimadamore@1215 134 */
mcimadamore@1215 135 private final SymbolNotFoundError varNotFound;
mcimadamore@1215 136 private final InapplicableSymbolError wrongMethod;
mcimadamore@1215 137 private final InapplicableSymbolsError wrongMethods;
mcimadamore@1215 138 private final SymbolNotFoundError methodNotFound;
mcimadamore@1215 139 private final SymbolNotFoundError typeNotFound;
mcimadamore@1215 140
mcimadamore@1215 141 public static Resolve instance(Context context) {
mcimadamore@1215 142 Resolve instance = context.get(resolveKey);
mcimadamore@1215 143 if (instance == null)
mcimadamore@1215 144 instance = new Resolve(context);
mcimadamore@1215 145 return instance;
mcimadamore@1215 146 }
mcimadamore@1215 147
mcimadamore@1215 148 // <editor-fold defaultstate="collapsed" desc="Verbose resolution diagnostics support">
mcimadamore@1114 149 enum VerboseResolutionMode {
mcimadamore@1114 150 SUCCESS("success"),
mcimadamore@1114 151 FAILURE("failure"),
mcimadamore@1114 152 APPLICABLE("applicable"),
mcimadamore@1114 153 INAPPLICABLE("inapplicable"),
mcimadamore@1114 154 DEFERRED_INST("deferred-inference"),
mcimadamore@1114 155 PREDEF("predef"),
mcimadamore@1114 156 OBJECT_INIT("object-init"),
mcimadamore@1114 157 INTERNAL("internal");
mcimadamore@1114 158
mcimadamore@1114 159 String opt;
mcimadamore@1114 160
mcimadamore@1114 161 private VerboseResolutionMode(String opt) {
mcimadamore@1114 162 this.opt = opt;
mcimadamore@1114 163 }
mcimadamore@1114 164
mcimadamore@1114 165 static EnumSet<VerboseResolutionMode> getVerboseResolutionMode(Options opts) {
mcimadamore@1114 166 String s = opts.get("verboseResolution");
mcimadamore@1114 167 EnumSet<VerboseResolutionMode> res = EnumSet.noneOf(VerboseResolutionMode.class);
mcimadamore@1114 168 if (s == null) return res;
mcimadamore@1114 169 if (s.contains("all")) {
mcimadamore@1114 170 res = EnumSet.allOf(VerboseResolutionMode.class);
mcimadamore@1114 171 }
mcimadamore@1114 172 Collection<String> args = Arrays.asList(s.split(","));
mcimadamore@1114 173 for (VerboseResolutionMode mode : values()) {
mcimadamore@1114 174 if (args.contains(mode.opt)) {
mcimadamore@1114 175 res.add(mode);
mcimadamore@1114 176 } else if (args.contains("-" + mode.opt)) {
mcimadamore@1114 177 res.remove(mode);
mcimadamore@1114 178 }
mcimadamore@1114 179 }
mcimadamore@1114 180 return res;
mcimadamore@1114 181 }
mcimadamore@1114 182 }
mcimadamore@1114 183
mcimadamore@1215 184 void reportVerboseResolutionDiagnostic(DiagnosticPosition dpos, Name name, Type site,
mcimadamore@1215 185 List<Type> argtypes, List<Type> typeargtypes, Symbol bestSoFar) {
mcimadamore@1215 186 boolean success = bestSoFar.kind < ERRONEOUS;
mcimadamore@1215 187
mcimadamore@1215 188 if (success && !verboseResolutionMode.contains(VerboseResolutionMode.SUCCESS)) {
mcimadamore@1215 189 return;
mcimadamore@1215 190 } else if (!success && !verboseResolutionMode.contains(VerboseResolutionMode.FAILURE)) {
mcimadamore@1215 191 return;
mcimadamore@1215 192 }
mcimadamore@1215 193
mcimadamore@1215 194 if (bestSoFar.name == names.init &&
mcimadamore@1215 195 bestSoFar.owner == syms.objectType.tsym &&
mcimadamore@1215 196 !verboseResolutionMode.contains(VerboseResolutionMode.OBJECT_INIT)) {
mcimadamore@1215 197 return; //skip diags for Object constructor resolution
mcimadamore@1215 198 } else if (site == syms.predefClass.type &&
mcimadamore@1215 199 !verboseResolutionMode.contains(VerboseResolutionMode.PREDEF)) {
mcimadamore@1215 200 return; //skip spurious diags for predef symbols (i.e. operators)
mcimadamore@1215 201 } else if (currentResolutionContext.internalResolution &&
mcimadamore@1215 202 !verboseResolutionMode.contains(VerboseResolutionMode.INTERNAL)) {
mcimadamore@1215 203 return;
mcimadamore@1215 204 }
mcimadamore@1215 205
mcimadamore@1215 206 int pos = 0;
mcimadamore@1215 207 int mostSpecificPos = -1;
mcimadamore@1215 208 ListBuffer<JCDiagnostic> subDiags = ListBuffer.lb();
mcimadamore@1215 209 for (Candidate c : currentResolutionContext.candidates) {
mcimadamore@1215 210 if (currentResolutionContext.step != c.step ||
mcimadamore@1215 211 (c.isApplicable() && !verboseResolutionMode.contains(VerboseResolutionMode.APPLICABLE)) ||
mcimadamore@1215 212 (!c.isApplicable() && !verboseResolutionMode.contains(VerboseResolutionMode.INAPPLICABLE))) {
mcimadamore@1215 213 continue;
mcimadamore@1215 214 } else {
mcimadamore@1215 215 subDiags.append(c.isApplicable() ?
mcimadamore@1215 216 getVerboseApplicableCandidateDiag(pos, c.sym, c.mtype) :
mcimadamore@1215 217 getVerboseInapplicableCandidateDiag(pos, c.sym, c.details));
mcimadamore@1215 218 if (c.sym == bestSoFar)
mcimadamore@1215 219 mostSpecificPos = pos;
mcimadamore@1215 220 pos++;
mcimadamore@1215 221 }
mcimadamore@1215 222 }
mcimadamore@1215 223 String key = success ? "verbose.resolve.multi" : "verbose.resolve.multi.1";
mcimadamore@1347 224 List<Type> argtypes2 = Type.map(argtypes,
mcimadamore@1347 225 deferredAttr.new RecoveryDeferredTypeMap(AttrMode.SPECULATIVE, bestSoFar, currentResolutionContext.step));
mcimadamore@1215 226 JCDiagnostic main = diags.note(log.currentSource(), dpos, key, name,
mcimadamore@1215 227 site.tsym, mostSpecificPos, currentResolutionContext.step,
mcimadamore@1347 228 methodArguments(argtypes2),
mcimadamore@1347 229 methodArguments(typeargtypes));
mcimadamore@1215 230 JCDiagnostic d = new JCDiagnostic.MultilineDiagnostic(main, subDiags.toList());
mcimadamore@1215 231 log.report(d);
duke@1 232 }
duke@1 233
mcimadamore@1215 234 JCDiagnostic getVerboseApplicableCandidateDiag(int pos, Symbol sym, Type inst) {
mcimadamore@1215 235 JCDiagnostic subDiag = null;
mcimadamore@1268 236 if (sym.type.tag == FORALL) {
mcimadamore@1268 237 subDiag = diags.fragment("partial.inst.sig", inst);
mcimadamore@1215 238 }
duke@1 239
mcimadamore@1215 240 String key = subDiag == null ?
mcimadamore@1215 241 "applicable.method.found" :
mcimadamore@1215 242 "applicable.method.found.1";
duke@1 243
mcimadamore@1215 244 return diags.fragment(key, pos, sym, subDiag);
duke@1 245 }
duke@1 246
mcimadamore@1215 247 JCDiagnostic getVerboseInapplicableCandidateDiag(int pos, Symbol sym, JCDiagnostic subDiag) {
mcimadamore@1215 248 return diags.fragment("not.applicable.method.found", pos, sym, subDiag);
mcimadamore@1215 249 }
mcimadamore@1215 250 // </editor-fold>
duke@1 251
duke@1 252 /* ************************************************************************
duke@1 253 * Identifier resolution
duke@1 254 *************************************************************************/
duke@1 255
duke@1 256 /** An environment is "static" if its static level is greater than
duke@1 257 * the one of its outer environment
duke@1 258 */
ksrini@1346 259 protected static boolean isStatic(Env<AttrContext> env) {
duke@1 260 return env.info.staticLevel > env.outer.info.staticLevel;
duke@1 261 }
duke@1 262
duke@1 263 /** An environment is an "initializer" if it is a constructor or
duke@1 264 * an instance initializer.
duke@1 265 */
duke@1 266 static boolean isInitializer(Env<AttrContext> env) {
duke@1 267 Symbol owner = env.info.scope.owner;
duke@1 268 return owner.isConstructor() ||
duke@1 269 owner.owner.kind == TYP &&
duke@1 270 (owner.kind == VAR ||
duke@1 271 owner.kind == MTH && (owner.flags() & BLOCK) != 0) &&
duke@1 272 (owner.flags() & STATIC) == 0;
duke@1 273 }
duke@1 274
duke@1 275 /** Is class accessible in given evironment?
duke@1 276 * @param env The current environment.
duke@1 277 * @param c The class whose accessibility is checked.
duke@1 278 */
duke@1 279 public boolean isAccessible(Env<AttrContext> env, TypeSymbol c) {
mcimadamore@741 280 return isAccessible(env, c, false);
mcimadamore@741 281 }
mcimadamore@741 282
mcimadamore@741 283 public boolean isAccessible(Env<AttrContext> env, TypeSymbol c, boolean checkInner) {
mcimadamore@741 284 boolean isAccessible = false;
duke@1 285 switch ((short)(c.flags() & AccessFlags)) {
mcimadamore@741 286 case PRIVATE:
mcimadamore@741 287 isAccessible =
mcimadamore@741 288 env.enclClass.sym.outermostClass() ==
mcimadamore@741 289 c.owner.outermostClass();
mcimadamore@741 290 break;
mcimadamore@741 291 case 0:
mcimadamore@741 292 isAccessible =
mcimadamore@741 293 env.toplevel.packge == c.owner // fast special case
mcimadamore@741 294 ||
mcimadamore@741 295 env.toplevel.packge == c.packge()
mcimadamore@741 296 ||
mcimadamore@741 297 // Hack: this case is added since synthesized default constructors
mcimadamore@741 298 // of anonymous classes should be allowed to access
mcimadamore@741 299 // classes which would be inaccessible otherwise.
mcimadamore@741 300 env.enclMethod != null &&
mcimadamore@741 301 (env.enclMethod.mods.flags & ANONCONSTR) != 0;
mcimadamore@741 302 break;
mcimadamore@741 303 default: // error recovery
mcimadamore@741 304 case PUBLIC:
mcimadamore@741 305 isAccessible = true;
mcimadamore@741 306 break;
mcimadamore@741 307 case PROTECTED:
mcimadamore@741 308 isAccessible =
mcimadamore@741 309 env.toplevel.packge == c.owner // fast special case
mcimadamore@741 310 ||
mcimadamore@741 311 env.toplevel.packge == c.packge()
mcimadamore@741 312 ||
mcimadamore@741 313 isInnerSubClass(env.enclClass.sym, c.owner);
mcimadamore@741 314 break;
duke@1 315 }
mcimadamore@741 316 return (checkInner == false || c.type.getEnclosingType() == Type.noType) ?
mcimadamore@741 317 isAccessible :
jjg@789 318 isAccessible && isAccessible(env, c.type.getEnclosingType(), checkInner);
duke@1 319 }
duke@1 320 //where
duke@1 321 /** Is given class a subclass of given base class, or an inner class
duke@1 322 * of a subclass?
duke@1 323 * Return null if no such class exists.
duke@1 324 * @param c The class which is the subclass or is contained in it.
duke@1 325 * @param base The base class
duke@1 326 */
duke@1 327 private boolean isInnerSubClass(ClassSymbol c, Symbol base) {
duke@1 328 while (c != null && !c.isSubClass(base, types)) {
duke@1 329 c = c.owner.enclClass();
duke@1 330 }
duke@1 331 return c != null;
duke@1 332 }
duke@1 333
duke@1 334 boolean isAccessible(Env<AttrContext> env, Type t) {
mcimadamore@741 335 return isAccessible(env, t, false);
mcimadamore@741 336 }
mcimadamore@741 337
mcimadamore@741 338 boolean isAccessible(Env<AttrContext> env, Type t, boolean checkInner) {
duke@1 339 return (t.tag == ARRAY)
duke@1 340 ? isAccessible(env, types.elemtype(t))
mcimadamore@741 341 : isAccessible(env, t.tsym, checkInner);
duke@1 342 }
duke@1 343
duke@1 344 /** Is symbol accessible as a member of given type in given evironment?
duke@1 345 * @param env The current environment.
duke@1 346 * @param site The type of which the tested symbol is regarded
duke@1 347 * as a member.
duke@1 348 * @param sym The symbol.
duke@1 349 */
duke@1 350 public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym) {
mcimadamore@741 351 return isAccessible(env, site, sym, false);
mcimadamore@741 352 }
mcimadamore@741 353 public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym, boolean checkInner) {
duke@1 354 if (sym.name == names.init && sym.owner != site.tsym) return false;
duke@1 355 switch ((short)(sym.flags() & AccessFlags)) {
duke@1 356 case PRIVATE:
duke@1 357 return
duke@1 358 (env.enclClass.sym == sym.owner // fast special case
duke@1 359 ||
duke@1 360 env.enclClass.sym.outermostClass() ==
duke@1 361 sym.owner.outermostClass())
duke@1 362 &&
duke@1 363 sym.isInheritedIn(site.tsym, types);
duke@1 364 case 0:
duke@1 365 return
duke@1 366 (env.toplevel.packge == sym.owner.owner // fast special case
duke@1 367 ||
duke@1 368 env.toplevel.packge == sym.packge())
duke@1 369 &&
mcimadamore@741 370 isAccessible(env, site, checkInner)
duke@1 371 &&
mcimadamore@254 372 sym.isInheritedIn(site.tsym, types)
mcimadamore@254 373 &&
mcimadamore@254 374 notOverriddenIn(site, sym);
duke@1 375 case PROTECTED:
duke@1 376 return
duke@1 377 (env.toplevel.packge == sym.owner.owner // fast special case
duke@1 378 ||
duke@1 379 env.toplevel.packge == sym.packge()
duke@1 380 ||
duke@1 381 isProtectedAccessible(sym, env.enclClass.sym, site)
duke@1 382 ||
duke@1 383 // OK to select instance method or field from 'super' or type name
duke@1 384 // (but type names should be disallowed elsewhere!)
duke@1 385 env.info.selectSuper && (sym.flags() & STATIC) == 0 && sym.kind != TYP)
duke@1 386 &&
mcimadamore@741 387 isAccessible(env, site, checkInner)
duke@1 388 &&
mcimadamore@254 389 notOverriddenIn(site, sym);
duke@1 390 default: // this case includes erroneous combinations as well
mcimadamore@741 391 return isAccessible(env, site, checkInner) && notOverriddenIn(site, sym);
mcimadamore@254 392 }
mcimadamore@254 393 }
mcimadamore@254 394 //where
mcimadamore@254 395 /* `sym' is accessible only if not overridden by
mcimadamore@254 396 * another symbol which is a member of `site'
mcimadamore@254 397 * (because, if it is overridden, `sym' is not strictly
mcimadamore@674 398 * speaking a member of `site'). A polymorphic signature method
mcimadamore@674 399 * cannot be overridden (e.g. MH.invokeExact(Object[])).
mcimadamore@254 400 */
mcimadamore@254 401 private boolean notOverriddenIn(Type site, Symbol sym) {
mcimadamore@254 402 if (sym.kind != MTH || sym.isConstructor() || sym.isStatic())
mcimadamore@254 403 return true;
mcimadamore@254 404 else {
mcimadamore@254 405 Symbol s2 = ((MethodSymbol)sym).implementation(site.tsym, types, true);
mcimadamore@844 406 return (s2 == null || s2 == sym || sym.owner == s2.owner ||
mcimadamore@325 407 !types.isSubSignature(types.memberType(site, s2), types.memberType(site, sym)));
duke@1 408 }
duke@1 409 }
duke@1 410 //where
duke@1 411 /** Is given protected symbol accessible if it is selected from given site
duke@1 412 * and the selection takes place in given class?
duke@1 413 * @param sym The symbol with protected access
duke@1 414 * @param c The class where the access takes place
duke@1 415 * @site The type of the qualifier
duke@1 416 */
duke@1 417 private
duke@1 418 boolean isProtectedAccessible(Symbol sym, ClassSymbol c, Type site) {
duke@1 419 while (c != null &&
duke@1 420 !(c.isSubClass(sym.owner, types) &&
duke@1 421 (c.flags() & INTERFACE) == 0 &&
duke@1 422 // In JLS 2e 6.6.2.1, the subclass restriction applies
duke@1 423 // only to instance fields and methods -- types are excluded
duke@1 424 // regardless of whether they are declared 'static' or not.
duke@1 425 ((sym.flags() & STATIC) != 0 || sym.kind == TYP || site.tsym.isSubClass(c, types))))
duke@1 426 c = c.owner.enclClass();
duke@1 427 return c != null;
duke@1 428 }
duke@1 429
duke@1 430 /** Try to instantiate the type of a method so that it fits
duke@1 431 * given type arguments and argument types. If succesful, return
duke@1 432 * the method's instantiated type, else return null.
duke@1 433 * The instantiation will take into account an additional leading
duke@1 434 * formal parameter if the method is an instance method seen as a member
duke@1 435 * of un underdetermined site In this case, we treat site as an additional
duke@1 436 * parameter and the parameters of the class containing the method as
duke@1 437 * additional type variables that get instantiated.
duke@1 438 *
duke@1 439 * @param env The current environment
duke@1 440 * @param site The type of which the method is a member.
duke@1 441 * @param m The method symbol.
duke@1 442 * @param argtypes The invocation's given value arguments.
duke@1 443 * @param typeargtypes The invocation's given type arguments.
duke@1 444 * @param allowBoxing Allow boxing conversions of arguments.
duke@1 445 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 446 */
duke@1 447 Type rawInstantiate(Env<AttrContext> env,
duke@1 448 Type site,
duke@1 449 Symbol m,
mcimadamore@1268 450 ResultInfo resultInfo,
duke@1 451 List<Type> argtypes,
duke@1 452 List<Type> typeargtypes,
duke@1 453 boolean allowBoxing,
duke@1 454 boolean useVarargs,
duke@1 455 Warner warn)
mcimadamore@299 456 throws Infer.InferenceException {
mcimadamore@689 457 if (useVarargs && (m.flags() & VARARGS) == 0)
mcimadamore@845 458 throw inapplicableMethodException.setMessage();
duke@1 459 Type mt = types.memberType(site, m);
duke@1 460
duke@1 461 // tvars is the list of formal type variables for which type arguments
duke@1 462 // need to inferred.
mcimadamore@1268 463 List<Type> tvars = List.nil();
duke@1 464 if (typeargtypes == null) typeargtypes = List.nil();
mcimadamore@820 465 if (mt.tag != FORALL && typeargtypes.nonEmpty()) {
duke@1 466 // This is not a polymorphic method, but typeargs are supplied
jjh@972 467 // which is fine, see JLS 15.12.2.1
duke@1 468 } else if (mt.tag == FORALL && typeargtypes.nonEmpty()) {
duke@1 469 ForAll pmt = (ForAll) mt;
duke@1 470 if (typeargtypes.length() != pmt.tvars.length())
mcimadamore@689 471 throw inapplicableMethodException.setMessage("arg.length.mismatch"); // not enough args
duke@1 472 // Check type arguments are within bounds
duke@1 473 List<Type> formals = pmt.tvars;
duke@1 474 List<Type> actuals = typeargtypes;
duke@1 475 while (formals.nonEmpty() && actuals.nonEmpty()) {
duke@1 476 List<Type> bounds = types.subst(types.getBounds((TypeVar)formals.head),
duke@1 477 pmt.tvars, typeargtypes);
duke@1 478 for (; bounds.nonEmpty(); bounds = bounds.tail)
duke@1 479 if (!types.isSubtypeUnchecked(actuals.head, bounds.head, warn))
mcimadamore@689 480 throw inapplicableMethodException.setMessage("explicit.param.do.not.conform.to.bounds",actuals.head, bounds);
duke@1 481 formals = formals.tail;
duke@1 482 actuals = actuals.tail;
duke@1 483 }
duke@1 484 mt = types.subst(pmt.qtype, pmt.tvars, typeargtypes);
duke@1 485 } else if (mt.tag == FORALL) {
duke@1 486 ForAll pmt = (ForAll) mt;
duke@1 487 List<Type> tvars1 = types.newInstances(pmt.tvars);
duke@1 488 tvars = tvars.appendList(tvars1);
duke@1 489 mt = types.subst(pmt.qtype, pmt.tvars, tvars1);
duke@1 490 }
duke@1 491
duke@1 492 // find out whether we need to go the slow route via infer
mcimadamore@1239 493 boolean instNeeded = tvars.tail != null; /*inlined: tvars.nonEmpty()*/
duke@1 494 for (List<Type> l = argtypes;
duke@1 495 l.tail != null/*inlined: l.nonEmpty()*/ && !instNeeded;
duke@1 496 l = l.tail) {
duke@1 497 if (l.head.tag == FORALL) instNeeded = true;
duke@1 498 }
duke@1 499
duke@1 500 if (instNeeded)
mcimadamore@1239 501 return infer.instantiateMethod(env,
mcimadamore@547 502 tvars,
duke@1 503 (MethodType)mt,
mcimadamore@1268 504 resultInfo,
mcimadamore@580 505 m,
duke@1 506 argtypes,
duke@1 507 allowBoxing,
duke@1 508 useVarargs,
mcimadamore@1347 509 currentResolutionContext,
duke@1 510 warn);
mcimadamore@689 511
mcimadamore@1347 512 checkRawArgumentsAcceptable(env, m, argtypes, mt.getParameterTypes(),
mcimadamore@689 513 allowBoxing, useVarargs, warn);
mcimadamore@689 514 return mt;
duke@1 515 }
duke@1 516
mcimadamore@1347 517 Type checkMethod(Env<AttrContext> env,
mcimadamore@1347 518 Type site,
mcimadamore@1347 519 Symbol m,
mcimadamore@1347 520 ResultInfo resultInfo,
mcimadamore@1347 521 List<Type> argtypes,
mcimadamore@1347 522 List<Type> typeargtypes,
mcimadamore@1347 523 Warner warn) {
mcimadamore@1347 524 MethodResolutionContext prevContext = currentResolutionContext;
mcimadamore@1347 525 try {
mcimadamore@1347 526 currentResolutionContext = new MethodResolutionContext();
mcimadamore@1347 527 currentResolutionContext.attrMode = DeferredAttr.AttrMode.CHECK;
mcimadamore@1347 528 MethodResolutionPhase step = currentResolutionContext.step = env.info.pendingResolutionPhase;
mcimadamore@1347 529 return rawInstantiate(env, site, m, resultInfo, argtypes, typeargtypes,
mcimadamore@1347 530 step.isBoxingRequired(), step.isVarargsRequired(), warn);
mcimadamore@1347 531 }
mcimadamore@1347 532 finally {
mcimadamore@1347 533 currentResolutionContext = prevContext;
mcimadamore@1347 534 }
mcimadamore@1347 535 }
mcimadamore@1347 536
duke@1 537 /** Same but returns null instead throwing a NoInstanceException
duke@1 538 */
duke@1 539 Type instantiate(Env<AttrContext> env,
duke@1 540 Type site,
duke@1 541 Symbol m,
mcimadamore@1268 542 ResultInfo resultInfo,
duke@1 543 List<Type> argtypes,
duke@1 544 List<Type> typeargtypes,
duke@1 545 boolean allowBoxing,
duke@1 546 boolean useVarargs,
duke@1 547 Warner warn) {
duke@1 548 try {
mcimadamore@1268 549 return rawInstantiate(env, site, m, resultInfo, argtypes, typeargtypes,
duke@1 550 allowBoxing, useVarargs, warn);
mcimadamore@689 551 } catch (InapplicableMethodException ex) {
duke@1 552 return null;
duke@1 553 }
duke@1 554 }
duke@1 555
duke@1 556 /** Check if a parameter list accepts a list of args.
duke@1 557 */
mcimadamore@845 558 boolean argumentsAcceptable(Env<AttrContext> env,
mcimadamore@1347 559 Symbol msym,
mcimadamore@845 560 List<Type> argtypes,
duke@1 561 List<Type> formals,
duke@1 562 boolean allowBoxing,
duke@1 563 boolean useVarargs,
duke@1 564 Warner warn) {
mcimadamore@689 565 try {
mcimadamore@1347 566 checkRawArgumentsAcceptable(env, msym, argtypes, formals, allowBoxing, useVarargs, warn);
mcimadamore@689 567 return true;
mcimadamore@689 568 } catch (InapplicableMethodException ex) {
mcimadamore@689 569 return false;
mcimadamore@689 570 }
mcimadamore@689 571 }
mcimadamore@1186 572 /**
mcimadamore@1186 573 * A check handler is used by the main method applicability routine in order
mcimadamore@1186 574 * to handle specific method applicability failures. It is assumed that a class
mcimadamore@1186 575 * implementing this interface should throw exceptions that are a subtype of
mcimadamore@1186 576 * InapplicableMethodException (see below). Such exception will terminate the
mcimadamore@1186 577 * method applicability check and propagate important info outwards (for the
mcimadamore@1186 578 * purpose of generating better diagnostics).
mcimadamore@1186 579 */
mcimadamore@1186 580 interface MethodCheckHandler {
mcimadamore@1186 581 /* The number of actuals and formals differ */
mcimadamore@1186 582 InapplicableMethodException arityMismatch();
mcimadamore@1186 583 /* An actual argument type does not conform to the corresponding formal type */
mcimadamore@1296 584 InapplicableMethodException argumentMismatch(boolean varargs, JCDiagnostic details);
mcimadamore@1186 585 /* The element type of a varargs is not accessible in the current context */
mcimadamore@1186 586 InapplicableMethodException inaccessibleVarargs(Symbol location, Type expected);
mcimadamore@1186 587 }
mcimadamore@1186 588
mcimadamore@1186 589 /**
mcimadamore@1186 590 * Basic method check handler used within Resolve - all methods end up
mcimadamore@1186 591 * throwing InapplicableMethodException; a diagnostic fragment that describes
mcimadamore@1186 592 * the cause as to why the method is not applicable is set on the exception
mcimadamore@1186 593 * before it is thrown.
mcimadamore@1186 594 */
mcimadamore@1186 595 MethodCheckHandler resolveHandler = new MethodCheckHandler() {
mcimadamore@1186 596 public InapplicableMethodException arityMismatch() {
mcimadamore@1186 597 return inapplicableMethodException.setMessage("arg.length.mismatch");
mcimadamore@1186 598 }
mcimadamore@1296 599 public InapplicableMethodException argumentMismatch(boolean varargs, JCDiagnostic details) {
mcimadamore@1186 600 String key = varargs ?
mcimadamore@1186 601 "varargs.argument.mismatch" :
mcimadamore@1186 602 "no.conforming.assignment.exists";
mcimadamore@1186 603 return inapplicableMethodException.setMessage(key,
mcimadamore@1296 604 details);
mcimadamore@1186 605 }
mcimadamore@1186 606 public InapplicableMethodException inaccessibleVarargs(Symbol location, Type expected) {
mcimadamore@1186 607 return inapplicableMethodException.setMessage("inaccessible.varargs.type",
mcimadamore@1186 608 expected, Kinds.kindName(location), location);
mcimadamore@1186 609 }
mcimadamore@1186 610 };
mcimadamore@1186 611
mcimadamore@845 612 void checkRawArgumentsAcceptable(Env<AttrContext> env,
mcimadamore@1347 613 Symbol msym,
mcimadamore@845 614 List<Type> argtypes,
mcimadamore@689 615 List<Type> formals,
mcimadamore@689 616 boolean allowBoxing,
mcimadamore@689 617 boolean useVarargs,
mcimadamore@689 618 Warner warn) {
mcimadamore@1347 619 checkRawArgumentsAcceptable(env, msym, currentResolutionContext.attrMode(), infer.emptyContext, argtypes, formals,
mcimadamore@1186 620 allowBoxing, useVarargs, warn, resolveHandler);
mcimadamore@1186 621 }
mcimadamore@1186 622
mcimadamore@1186 623 /**
mcimadamore@1186 624 * Main method applicability routine. Given a list of actual types A,
mcimadamore@1186 625 * a list of formal types F, determines whether the types in A are
mcimadamore@1186 626 * compatible (by method invocation conversion) with the types in F.
mcimadamore@1186 627 *
mcimadamore@1186 628 * Since this routine is shared between overload resolution and method
mcimadamore@1347 629 * type-inference, a (possibly empty) inference context is used to convert
mcimadamore@1347 630 * formal types to the corresponding 'undet' form ahead of a compatibility
mcimadamore@1347 631 * check so that constraints can be propagated and collected.
mcimadamore@1186 632 *
mcimadamore@1347 633 * Moreover, if one or more types in A is a deferred type, this routine uses
mcimadamore@1347 634 * DeferredAttr in order to perform deferred attribution. If one or more actual
mcimadamore@1347 635 * deferred types are stuck, they are placed in a queue and revisited later
mcimadamore@1347 636 * after the remainder of the arguments have been seen. If this is not sufficient
mcimadamore@1347 637 * to 'unstuck' the argument, a cyclic inference error is called out.
mcimadamore@1186 638 *
mcimadamore@1186 639 * A method check handler (see above) is used in order to report errors.
mcimadamore@1186 640 */
mcimadamore@1337 641 void checkRawArgumentsAcceptable(final Env<AttrContext> env,
mcimadamore@1347 642 Symbol msym,
mcimadamore@1347 643 DeferredAttr.AttrMode mode,
mcimadamore@1337 644 final Infer.InferenceContext inferenceContext,
mcimadamore@1186 645 List<Type> argtypes,
mcimadamore@1186 646 List<Type> formals,
mcimadamore@1186 647 boolean allowBoxing,
mcimadamore@1186 648 boolean useVarargs,
mcimadamore@1186 649 Warner warn,
mcimadamore@1347 650 final MethodCheckHandler handler) {
duke@1 651 Type varargsFormal = useVarargs ? formals.last() : null;
mcimadamore@1186 652
mcimadamore@689 653 if (varargsFormal == null &&
mcimadamore@689 654 argtypes.size() != formals.size()) {
mcimadamore@1186 655 throw handler.arityMismatch(); // not enough args
mcimadamore@689 656 }
mcimadamore@689 657
mcimadamore@1347 658 DeferredAttr.DeferredAttrContext deferredAttrContext =
mcimadamore@1347 659 deferredAttr.new DeferredAttrContext(mode, msym, currentResolutionContext.step, inferenceContext);
mcimadamore@1347 660
duke@1 661 while (argtypes.nonEmpty() && formals.head != varargsFormal) {
mcimadamore@1347 662 ResultInfo mresult = methodCheckResult(formals.head, allowBoxing, false, inferenceContext, deferredAttrContext, handler, warn);
mcimadamore@1347 663 mresult.check(null, argtypes.head);
duke@1 664 argtypes = argtypes.tail;
duke@1 665 formals = formals.tail;
duke@1 666 }
mcimadamore@689 667
mcimadamore@1186 668 if (formals.head != varargsFormal) {
mcimadamore@1186 669 throw handler.arityMismatch(); // not enough args
mcimadamore@1186 670 }
mcimadamore@689 671
mcimadamore@689 672 if (useVarargs) {
mcimadamore@1186 673 //note: if applicability check is triggered by most specific test,
mcimadamore@1186 674 //the last argument of a varargs is _not_ an array type (see JLS 15.12.2.5)
mcimadamore@1347 675 final Type elt = types.elemtype(varargsFormal);
mcimadamore@1347 676 ResultInfo mresult = methodCheckResult(elt, allowBoxing, true, inferenceContext, deferredAttrContext, handler, warn);
mcimadamore@689 677 while (argtypes.nonEmpty()) {
mcimadamore@1347 678 mresult.check(null, argtypes.head);
mcimadamore@689 679 argtypes = argtypes.tail;
mcimadamore@689 680 }
mcimadamore@845 681 //check varargs element type accessibility
mcimadamore@1337 682 varargsAccessible(env, elt, handler, inferenceContext);
mcimadamore@1337 683 }
mcimadamore@1347 684
mcimadamore@1347 685 deferredAttrContext.complete();
mcimadamore@1337 686 }
mcimadamore@1337 687
mcimadamore@1337 688 void varargsAccessible(final Env<AttrContext> env, final Type t, final Resolve.MethodCheckHandler handler, final InferenceContext inferenceContext) {
mcimadamore@1337 689 if (inferenceContext.free(t)) {
mcimadamore@1337 690 inferenceContext.addFreeTypeListener(List.of(t), new FreeTypeListener() {
mcimadamore@1337 691 @Override
mcimadamore@1337 692 public void typesInferred(InferenceContext inferenceContext) {
mcimadamore@1337 693 varargsAccessible(env, inferenceContext.asInstType(t, types), handler, inferenceContext);
mcimadamore@1337 694 }
mcimadamore@1337 695 });
mcimadamore@1337 696 } else {
mcimadamore@1337 697 if (!isAccessible(env, t)) {
mcimadamore@845 698 Symbol location = env.enclClass.sym;
mcimadamore@1337 699 throw handler.inaccessibleVarargs(location, t);
mcimadamore@845 700 }
duke@1 701 }
duke@1 702 }
mcimadamore@689 703
mcimadamore@1238 704 /**
mcimadamore@1238 705 * Check context to be used during method applicability checks. A method check
mcimadamore@1238 706 * context might contain inference variables.
mcimadamore@1238 707 */
mcimadamore@1238 708 abstract class MethodCheckContext implements CheckContext {
mcimadamore@689 709
mcimadamore@1238 710 MethodCheckHandler handler;
mcimadamore@1238 711 boolean useVarargs;
mcimadamore@1337 712 Infer.InferenceContext inferenceContext;
mcimadamore@1347 713 DeferredAttrContext deferredAttrContext;
mcimadamore@1238 714 Warner rsWarner;
mcimadamore@1238 715
mcimadamore@1347 716 public MethodCheckContext(MethodCheckHandler handler, boolean useVarargs,
mcimadamore@1347 717 Infer.InferenceContext inferenceContext, DeferredAttrContext deferredAttrContext, Warner rsWarner) {
mcimadamore@1238 718 this.handler = handler;
mcimadamore@1238 719 this.useVarargs = useVarargs;
mcimadamore@1337 720 this.inferenceContext = inferenceContext;
mcimadamore@1347 721 this.deferredAttrContext = deferredAttrContext;
mcimadamore@1238 722 this.rsWarner = rsWarner;
mcimadamore@1238 723 }
mcimadamore@1238 724
mcimadamore@1296 725 public void report(DiagnosticPosition pos, JCDiagnostic details) {
mcimadamore@1296 726 throw handler.argumentMismatch(useVarargs, details);
mcimadamore@1238 727 }
mcimadamore@1238 728
mcimadamore@1238 729 public Warner checkWarner(DiagnosticPosition pos, Type found, Type req) {
mcimadamore@1238 730 return rsWarner;
mcimadamore@1238 731 }
mcimadamore@1337 732
mcimadamore@1337 733 public InferenceContext inferenceContext() {
mcimadamore@1337 734 return inferenceContext;
mcimadamore@1337 735 }
mcimadamore@1347 736
mcimadamore@1347 737 public DeferredAttrContext deferredAttrContext() {
mcimadamore@1347 738 return deferredAttrContext;
mcimadamore@1347 739 }
mcimadamore@1238 740 }
mcimadamore@1238 741
mcimadamore@1238 742 /**
mcimadamore@1238 743 * Subclass of method check context class that implements strict method conversion.
mcimadamore@1238 744 * Strict method conversion checks compatibility between types using subtyping tests.
mcimadamore@1238 745 */
mcimadamore@1238 746 class StrictMethodContext extends MethodCheckContext {
mcimadamore@1238 747
mcimadamore@1347 748 public StrictMethodContext(MethodCheckHandler handler, boolean useVarargs,
mcimadamore@1347 749 Infer.InferenceContext inferenceContext, DeferredAttrContext deferredAttrContext, Warner rsWarner) {
mcimadamore@1347 750 super(handler, useVarargs, inferenceContext, deferredAttrContext, rsWarner);
mcimadamore@1238 751 }
mcimadamore@1238 752
mcimadamore@1238 753 public boolean compatible(Type found, Type req, Warner warn) {
mcimadamore@1337 754 return types.isSubtypeUnchecked(found, inferenceContext.asFree(req, types), warn);
mcimadamore@1238 755 }
mcimadamore@1238 756 }
mcimadamore@1238 757
mcimadamore@1238 758 /**
mcimadamore@1238 759 * Subclass of method check context class that implements loose method conversion.
mcimadamore@1238 760 * Loose method conversion checks compatibility between types using method conversion tests.
mcimadamore@1238 761 */
mcimadamore@1238 762 class LooseMethodContext extends MethodCheckContext {
mcimadamore@1238 763
mcimadamore@1347 764 public LooseMethodContext(MethodCheckHandler handler, boolean useVarargs,
mcimadamore@1347 765 Infer.InferenceContext inferenceContext, DeferredAttrContext deferredAttrContext, Warner rsWarner) {
mcimadamore@1347 766 super(handler, useVarargs, inferenceContext, deferredAttrContext, rsWarner);
mcimadamore@1238 767 }
mcimadamore@1238 768
mcimadamore@1238 769 public boolean compatible(Type found, Type req, Warner warn) {
mcimadamore@1337 770 return types.isConvertible(found, inferenceContext.asFree(req, types), warn);
mcimadamore@1238 771 }
mcimadamore@1238 772 }
mcimadamore@1238 773
mcimadamore@1238 774 /**
mcimadamore@1238 775 * Create a method check context to be used during method applicability check
mcimadamore@1238 776 */
mcimadamore@1238 777 ResultInfo methodCheckResult(Type to, boolean allowBoxing, boolean useVarargs,
mcimadamore@1347 778 Infer.InferenceContext inferenceContext, DeferredAttr.DeferredAttrContext deferredAttrContext,
mcimadamore@1347 779 MethodCheckHandler methodHandler, Warner rsWarner) {
mcimadamore@1238 780 MethodCheckContext checkContext = allowBoxing ?
mcimadamore@1347 781 new LooseMethodContext(methodHandler, useVarargs, inferenceContext, deferredAttrContext, rsWarner) :
mcimadamore@1347 782 new StrictMethodContext(methodHandler, useVarargs, inferenceContext, deferredAttrContext, rsWarner);
mcimadamore@1347 783 return new MethodResultInfo(to, checkContext, deferredAttrContext);
mcimadamore@1347 784 }
mcimadamore@1347 785
mcimadamore@1347 786 class MethodResultInfo extends ResultInfo {
mcimadamore@1347 787
mcimadamore@1347 788 DeferredAttr.DeferredAttrContext deferredAttrContext;
mcimadamore@1347 789
mcimadamore@1347 790 public MethodResultInfo(Type pt, MethodCheckContext checkContext, DeferredAttr.DeferredAttrContext deferredAttrContext) {
mcimadamore@1347 791 attr.super(VAL, pt, checkContext);
mcimadamore@1347 792 this.deferredAttrContext = deferredAttrContext;
mcimadamore@1347 793 }
mcimadamore@1347 794
mcimadamore@1347 795 @Override
mcimadamore@1347 796 protected Type check(DiagnosticPosition pos, Type found) {
mcimadamore@1347 797 if (found.tag == DEFERRED) {
mcimadamore@1347 798 DeferredType dt = (DeferredType)found;
mcimadamore@1347 799 return dt.check(this);
mcimadamore@1347 800 } else {
mcimadamore@1338 801 return super.check(pos, chk.checkNonVoid(pos, types.capture(types.upperBound(found.baseType()))));
mcimadamore@689 802 }
mcimadamore@1347 803 }
mcimadamore@1347 804
mcimadamore@1347 805 @Override
mcimadamore@1347 806 protected MethodResultInfo dup(Type newPt) {
mcimadamore@1347 807 return new MethodResultInfo(newPt, (MethodCheckContext)checkContext, deferredAttrContext);
mcimadamore@1347 808 }
mcimadamore@1238 809 }
mcimadamore@689 810
mcimadamore@1238 811 public static class InapplicableMethodException extends RuntimeException {
mcimadamore@1238 812 private static final long serialVersionUID = 0;
mcimadamore@1238 813
mcimadamore@1238 814 JCDiagnostic diagnostic;
mcimadamore@1238 815 JCDiagnostic.Factory diags;
mcimadamore@1238 816
mcimadamore@1238 817 InapplicableMethodException(JCDiagnostic.Factory diags) {
mcimadamore@1238 818 this.diagnostic = null;
mcimadamore@1238 819 this.diags = diags;
mcimadamore@689 820 }
mcimadamore@1238 821 InapplicableMethodException setMessage() {
mcimadamore@1337 822 return setMessage((JCDiagnostic)null);
mcimadamore@1238 823 }
mcimadamore@1238 824 InapplicableMethodException setMessage(String key) {
mcimadamore@1337 825 return setMessage(key != null ? diags.fragment(key) : null);
mcimadamore@1238 826 }
mcimadamore@1238 827 InapplicableMethodException setMessage(String key, Object... args) {
mcimadamore@1337 828 return setMessage(key != null ? diags.fragment(key, args) : null);
mcimadamore@1238 829 }
mcimadamore@1238 830 InapplicableMethodException setMessage(JCDiagnostic diag) {
mcimadamore@1238 831 this.diagnostic = diag;
mcimadamore@1238 832 return this;
mcimadamore@1238 833 }
mcimadamore@1238 834
mcimadamore@1238 835 public JCDiagnostic getDiagnostic() {
mcimadamore@1238 836 return diagnostic;
mcimadamore@1238 837 }
mcimadamore@1238 838 }
mcimadamore@1238 839 private final InapplicableMethodException inapplicableMethodException;
duke@1 840
duke@1 841 /* ***************************************************************************
duke@1 842 * Symbol lookup
duke@1 843 * the following naming conventions for arguments are used
duke@1 844 *
duke@1 845 * env is the environment where the symbol was mentioned
duke@1 846 * site is the type of which the symbol is a member
duke@1 847 * name is the symbol's name
duke@1 848 * if no arguments are given
duke@1 849 * argtypes are the value arguments, if we search for a method
duke@1 850 *
duke@1 851 * If no symbol was found, a ResolveError detailing the problem is returned.
duke@1 852 ****************************************************************************/
duke@1 853
duke@1 854 /** Find field. Synthetic fields are always skipped.
duke@1 855 * @param env The current environment.
duke@1 856 * @param site The original type from where the selection takes place.
duke@1 857 * @param name The name of the field.
duke@1 858 * @param c The class to search for the field. This is always
duke@1 859 * a superclass or implemented interface of site's class.
duke@1 860 */
duke@1 861 Symbol findField(Env<AttrContext> env,
duke@1 862 Type site,
duke@1 863 Name name,
duke@1 864 TypeSymbol c) {
mcimadamore@19 865 while (c.type.tag == TYPEVAR)
mcimadamore@19 866 c = c.type.getUpperBound().tsym;
duke@1 867 Symbol bestSoFar = varNotFound;
duke@1 868 Symbol sym;
duke@1 869 Scope.Entry e = c.members().lookup(name);
duke@1 870 while (e.scope != null) {
duke@1 871 if (e.sym.kind == VAR && (e.sym.flags_field & SYNTHETIC) == 0) {
duke@1 872 return isAccessible(env, site, e.sym)
duke@1 873 ? e.sym : new AccessError(env, site, e.sym);
duke@1 874 }
duke@1 875 e = e.next();
duke@1 876 }
duke@1 877 Type st = types.supertype(c.type);
mcimadamore@19 878 if (st != null && (st.tag == CLASS || st.tag == TYPEVAR)) {
duke@1 879 sym = findField(env, site, name, st.tsym);
duke@1 880 if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 881 }
duke@1 882 for (List<Type> l = types.interfaces(c.type);
duke@1 883 bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
duke@1 884 l = l.tail) {
duke@1 885 sym = findField(env, site, name, l.head.tsym);
duke@1 886 if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS &&
duke@1 887 sym.owner != bestSoFar.owner)
duke@1 888 bestSoFar = new AmbiguityError(bestSoFar, sym);
duke@1 889 else if (sym.kind < bestSoFar.kind)
duke@1 890 bestSoFar = sym;
duke@1 891 }
duke@1 892 return bestSoFar;
duke@1 893 }
duke@1 894
duke@1 895 /** Resolve a field identifier, throw a fatal error if not found.
duke@1 896 * @param pos The position to use for error reporting.
duke@1 897 * @param env The environment current at the method invocation.
duke@1 898 * @param site The type of the qualifying expression, in which
duke@1 899 * identifier is searched.
duke@1 900 * @param name The identifier's name.
duke@1 901 */
duke@1 902 public VarSymbol resolveInternalField(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 903 Type site, Name name) {
duke@1 904 Symbol sym = findField(env, site, name, site.tsym);
duke@1 905 if (sym.kind == VAR) return (VarSymbol)sym;
duke@1 906 else throw new FatalError(
mcimadamore@89 907 diags.fragment("fatal.err.cant.locate.field",
duke@1 908 name));
duke@1 909 }
duke@1 910
duke@1 911 /** Find unqualified variable or field with given name.
duke@1 912 * Synthetic fields always skipped.
duke@1 913 * @param env The current environment.
duke@1 914 * @param name The name of the variable or field.
duke@1 915 */
duke@1 916 Symbol findVar(Env<AttrContext> env, Name name) {
duke@1 917 Symbol bestSoFar = varNotFound;
duke@1 918 Symbol sym;
duke@1 919 Env<AttrContext> env1 = env;
duke@1 920 boolean staticOnly = false;
duke@1 921 while (env1.outer != null) {
duke@1 922 if (isStatic(env1)) staticOnly = true;
duke@1 923 Scope.Entry e = env1.info.scope.lookup(name);
duke@1 924 while (e.scope != null &&
duke@1 925 (e.sym.kind != VAR ||
duke@1 926 (e.sym.flags_field & SYNTHETIC) != 0))
duke@1 927 e = e.next();
duke@1 928 sym = (e.scope != null)
duke@1 929 ? e.sym
duke@1 930 : findField(
duke@1 931 env1, env1.enclClass.sym.type, name, env1.enclClass.sym);
duke@1 932 if (sym.exists()) {
duke@1 933 if (staticOnly &&
duke@1 934 sym.kind == VAR &&
duke@1 935 sym.owner.kind == TYP &&
duke@1 936 (sym.flags() & STATIC) == 0)
duke@1 937 return new StaticError(sym);
duke@1 938 else
duke@1 939 return sym;
duke@1 940 } else if (sym.kind < bestSoFar.kind) {
duke@1 941 bestSoFar = sym;
duke@1 942 }
duke@1 943
duke@1 944 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
duke@1 945 env1 = env1.outer;
duke@1 946 }
duke@1 947
duke@1 948 sym = findField(env, syms.predefClass.type, name, syms.predefClass);
duke@1 949 if (sym.exists())
duke@1 950 return sym;
duke@1 951 if (bestSoFar.exists())
duke@1 952 return bestSoFar;
duke@1 953
duke@1 954 Scope.Entry e = env.toplevel.namedImportScope.lookup(name);
duke@1 955 for (; e.scope != null; e = e.next()) {
duke@1 956 sym = e.sym;
duke@1 957 Type origin = e.getOrigin().owner.type;
duke@1 958 if (sym.kind == VAR) {
duke@1 959 if (e.sym.owner.type != origin)
duke@1 960 sym = sym.clone(e.getOrigin().owner);
duke@1 961 return isAccessible(env, origin, sym)
duke@1 962 ? sym : new AccessError(env, origin, sym);
duke@1 963 }
duke@1 964 }
duke@1 965
duke@1 966 Symbol origin = null;
duke@1 967 e = env.toplevel.starImportScope.lookup(name);
duke@1 968 for (; e.scope != null; e = e.next()) {
duke@1 969 sym = e.sym;
duke@1 970 if (sym.kind != VAR)
duke@1 971 continue;
duke@1 972 // invariant: sym.kind == VAR
duke@1 973 if (bestSoFar.kind < AMBIGUOUS && sym.owner != bestSoFar.owner)
duke@1 974 return new AmbiguityError(bestSoFar, sym);
duke@1 975 else if (bestSoFar.kind >= VAR) {
duke@1 976 origin = e.getOrigin().owner;
duke@1 977 bestSoFar = isAccessible(env, origin.type, sym)
duke@1 978 ? sym : new AccessError(env, origin.type, sym);
duke@1 979 }
duke@1 980 }
duke@1 981 if (bestSoFar.kind == VAR && bestSoFar.owner.type != origin.type)
duke@1 982 return bestSoFar.clone(origin);
duke@1 983 else
duke@1 984 return bestSoFar;
duke@1 985 }
duke@1 986
duke@1 987 Warner noteWarner = new Warner();
duke@1 988
duke@1 989 /** Select the best method for a call site among two choices.
duke@1 990 * @param env The current environment.
duke@1 991 * @param site The original type from where the
duke@1 992 * selection takes place.
duke@1 993 * @param argtypes The invocation's value arguments,
duke@1 994 * @param typeargtypes The invocation's type arguments,
duke@1 995 * @param sym Proposed new best match.
duke@1 996 * @param bestSoFar Previously found best match.
duke@1 997 * @param allowBoxing Allow boxing conversions of arguments.
duke@1 998 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 999 */
mcimadamore@689 1000 @SuppressWarnings("fallthrough")
duke@1 1001 Symbol selectBest(Env<AttrContext> env,
duke@1 1002 Type site,
duke@1 1003 List<Type> argtypes,
duke@1 1004 List<Type> typeargtypes,
duke@1 1005 Symbol sym,
duke@1 1006 Symbol bestSoFar,
duke@1 1007 boolean allowBoxing,
duke@1 1008 boolean useVarargs,
duke@1 1009 boolean operator) {
duke@1 1010 if (sym.kind == ERR) return bestSoFar;
mcimadamore@171 1011 if (!sym.isInheritedIn(site.tsym, types)) return bestSoFar;
jjg@816 1012 Assert.check(sym.kind < AMBIGUOUS);
duke@1 1013 try {
mcimadamore@1268 1014 Type mt = rawInstantiate(env, site, sym, null, argtypes, typeargtypes,
mcimadamore@689 1015 allowBoxing, useVarargs, Warner.noWarnings);
mcimadamore@1215 1016 if (!operator)
mcimadamore@1215 1017 currentResolutionContext.addApplicableCandidate(sym, mt);
mcimadamore@689 1018 } catch (InapplicableMethodException ex) {
mcimadamore@1215 1019 if (!operator)
mcimadamore@1215 1020 currentResolutionContext.addInapplicableCandidate(sym, ex.getDiagnostic());
duke@1 1021 switch (bestSoFar.kind) {
duke@1 1022 case ABSENT_MTH:
mcimadamore@1215 1023 return wrongMethod;
duke@1 1024 case WRONG_MTH:
mcimadamore@1110 1025 if (operator) return bestSoFar;
mcimadamore@689 1026 case WRONG_MTHS:
mcimadamore@1215 1027 return wrongMethods;
duke@1 1028 default:
duke@1 1029 return bestSoFar;
duke@1 1030 }
duke@1 1031 }
duke@1 1032 if (!isAccessible(env, site, sym)) {
duke@1 1033 return (bestSoFar.kind == ABSENT_MTH)
duke@1 1034 ? new AccessError(env, site, sym)
duke@1 1035 : bestSoFar;
mcimadamore@1215 1036 }
duke@1 1037 return (bestSoFar.kind > AMBIGUOUS)
duke@1 1038 ? sym
duke@1 1039 : mostSpecific(sym, bestSoFar, env, site,
duke@1 1040 allowBoxing && operator, useVarargs);
duke@1 1041 }
duke@1 1042
duke@1 1043 /* Return the most specific of the two methods for a call,
duke@1 1044 * given that both are accessible and applicable.
duke@1 1045 * @param m1 A new candidate for most specific.
duke@1 1046 * @param m2 The previous most specific candidate.
duke@1 1047 * @param env The current environment.
duke@1 1048 * @param site The original type from where the selection
duke@1 1049 * takes place.
duke@1 1050 * @param allowBoxing Allow boxing conversions of arguments.
duke@1 1051 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 1052 */
duke@1 1053 Symbol mostSpecific(Symbol m1,
duke@1 1054 Symbol m2,
duke@1 1055 Env<AttrContext> env,
mcimadamore@254 1056 final Type site,
duke@1 1057 boolean allowBoxing,
duke@1 1058 boolean useVarargs) {
duke@1 1059 switch (m2.kind) {
duke@1 1060 case MTH:
duke@1 1061 if (m1 == m2) return m1;
mcimadamore@775 1062 boolean m1SignatureMoreSpecific = signatureMoreSpecific(env, site, m1, m2, allowBoxing, useVarargs);
mcimadamore@775 1063 boolean m2SignatureMoreSpecific = signatureMoreSpecific(env, site, m2, m1, allowBoxing, useVarargs);
duke@1 1064 if (m1SignatureMoreSpecific && m2SignatureMoreSpecific) {
mcimadamore@775 1065 Type mt1 = types.memberType(site, m1);
mcimadamore@775 1066 Type mt2 = types.memberType(site, m2);
duke@1 1067 if (!types.overrideEquivalent(mt1, mt2))
mcimadamore@844 1068 return ambiguityError(m1, m2);
mcimadamore@844 1069
duke@1 1070 // same signature; select (a) the non-bridge method, or
duke@1 1071 // (b) the one that overrides the other, or (c) the concrete
duke@1 1072 // one, or (d) merge both abstract signatures
mcimadamore@844 1073 if ((m1.flags() & BRIDGE) != (m2.flags() & BRIDGE))
duke@1 1074 return ((m1.flags() & BRIDGE) != 0) ? m2 : m1;
mcimadamore@844 1075
duke@1 1076 // if one overrides or hides the other, use it
duke@1 1077 TypeSymbol m1Owner = (TypeSymbol)m1.owner;
duke@1 1078 TypeSymbol m2Owner = (TypeSymbol)m2.owner;
duke@1 1079 if (types.asSuper(m1Owner.type, m2Owner) != null &&
duke@1 1080 ((m1.owner.flags_field & INTERFACE) == 0 ||
duke@1 1081 (m2.owner.flags_field & INTERFACE) != 0) &&
duke@1 1082 m1.overrides(m2, m1Owner, types, false))
duke@1 1083 return m1;
duke@1 1084 if (types.asSuper(m2Owner.type, m1Owner) != null &&
duke@1 1085 ((m2.owner.flags_field & INTERFACE) == 0 ||
duke@1 1086 (m1.owner.flags_field & INTERFACE) != 0) &&
duke@1 1087 m2.overrides(m1, m2Owner, types, false))
duke@1 1088 return m2;
duke@1 1089 boolean m1Abstract = (m1.flags() & ABSTRACT) != 0;
duke@1 1090 boolean m2Abstract = (m2.flags() & ABSTRACT) != 0;
duke@1 1091 if (m1Abstract && !m2Abstract) return m2;
duke@1 1092 if (m2Abstract && !m1Abstract) return m1;
duke@1 1093 // both abstract or both concrete
duke@1 1094 if (!m1Abstract && !m2Abstract)
mcimadamore@844 1095 return ambiguityError(m1, m2);
mcimadamore@156 1096 // check that both signatures have the same erasure
mcimadamore@156 1097 if (!types.isSameTypes(m1.erasure(types).getParameterTypes(),
mcimadamore@156 1098 m2.erasure(types).getParameterTypes()))
mcimadamore@844 1099 return ambiguityError(m1, m2);
duke@1 1100 // both abstract, neither overridden; merge throws clause and result type
mcimadamore@1059 1101 Type mst = mostSpecificReturnType(mt1, mt2);
mcimadamore@1059 1102 if (mst == null) {
duke@1 1103 // Theoretically, this can't happen, but it is possible
duke@1 1104 // due to error recovery or mixing incompatible class files
mcimadamore@844 1105 return ambiguityError(m1, m2);
duke@1 1106 }
mcimadamore@1059 1107 Symbol mostSpecific = mst == mt1 ? m1 : m2;
dlsmith@880 1108 List<Type> allThrown = chk.intersect(mt1.getThrownTypes(), mt2.getThrownTypes());
dlsmith@880 1109 Type newSig = types.createMethodTypeWithThrown(mostSpecific.type, allThrown);
mcimadamore@254 1110 MethodSymbol result = new MethodSymbol(
mcimadamore@254 1111 mostSpecific.flags(),
mcimadamore@254 1112 mostSpecific.name,
dlsmith@880 1113 newSig,
mcimadamore@254 1114 mostSpecific.owner) {
mcimadamore@254 1115 @Override
mcimadamore@254 1116 public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult) {
mcimadamore@254 1117 if (origin == site.tsym)
mcimadamore@254 1118 return this;
mcimadamore@254 1119 else
mcimadamore@254 1120 return super.implementation(origin, types, checkResult);
mcimadamore@1335 1121 }
mcimadamore@1335 1122 };
duke@1 1123 return result;
duke@1 1124 }
duke@1 1125 if (m1SignatureMoreSpecific) return m1;
duke@1 1126 if (m2SignatureMoreSpecific) return m2;
mcimadamore@844 1127 return ambiguityError(m1, m2);
duke@1 1128 case AMBIGUOUS:
duke@1 1129 AmbiguityError e = (AmbiguityError)m2;
mcimadamore@302 1130 Symbol err1 = mostSpecific(m1, e.sym, env, site, allowBoxing, useVarargs);
duke@1 1131 Symbol err2 = mostSpecific(m1, e.sym2, env, site, allowBoxing, useVarargs);
duke@1 1132 if (err1 == err2) return err1;
mcimadamore@302 1133 if (err1 == e.sym && err2 == e.sym2) return m2;
duke@1 1134 if (err1 instanceof AmbiguityError &&
duke@1 1135 err2 instanceof AmbiguityError &&
mcimadamore@302 1136 ((AmbiguityError)err1).sym == ((AmbiguityError)err2).sym)
mcimadamore@844 1137 return ambiguityError(m1, m2);
duke@1 1138 else
mcimadamore@844 1139 return ambiguityError(err1, err2);
duke@1 1140 default:
duke@1 1141 throw new AssertionError();
duke@1 1142 }
duke@1 1143 }
mcimadamore@775 1144 //where
mcimadamore@775 1145 private boolean signatureMoreSpecific(Env<AttrContext> env, Type site, Symbol m1, Symbol m2, boolean allowBoxing, boolean useVarargs) {
mcimadamore@795 1146 noteWarner.clear();
mcimadamore@775 1147 Type mtype1 = types.memberType(site, adjustVarargs(m1, m2, useVarargs));
mcimadamore@1268 1148 Type mtype2 = instantiate(env, site, adjustVarargs(m2, m1, useVarargs), null,
mcimadamore@1006 1149 types.lowerBoundArgtypes(mtype1), null,
mcimadamore@1006 1150 allowBoxing, false, noteWarner);
mcimadamore@1006 1151 return mtype2 != null &&
mcimadamore@795 1152 !noteWarner.hasLint(Lint.LintCategory.UNCHECKED);
mcimadamore@775 1153 }
mcimadamore@775 1154 //where
mcimadamore@775 1155 private Symbol adjustVarargs(Symbol to, Symbol from, boolean useVarargs) {
mcimadamore@775 1156 List<Type> fromArgs = from.type.getParameterTypes();
mcimadamore@775 1157 List<Type> toArgs = to.type.getParameterTypes();
mcimadamore@775 1158 if (useVarargs &&
mcimadamore@775 1159 (from.flags() & VARARGS) != 0 &&
mcimadamore@775 1160 (to.flags() & VARARGS) != 0) {
mcimadamore@775 1161 Type varargsTypeFrom = fromArgs.last();
mcimadamore@775 1162 Type varargsTypeTo = toArgs.last();
mcimadamore@787 1163 ListBuffer<Type> args = ListBuffer.lb();
mcimadamore@787 1164 if (toArgs.length() < fromArgs.length()) {
mcimadamore@787 1165 //if we are checking a varargs method 'from' against another varargs
mcimadamore@787 1166 //method 'to' (where arity of 'to' < arity of 'from') then expand signature
mcimadamore@787 1167 //of 'to' to 'fit' arity of 'from' (this means adding fake formals to 'to'
mcimadamore@787 1168 //until 'to' signature has the same arity as 'from')
mcimadamore@787 1169 while (fromArgs.head != varargsTypeFrom) {
mcimadamore@787 1170 args.append(toArgs.head == varargsTypeTo ? types.elemtype(varargsTypeTo) : toArgs.head);
mcimadamore@787 1171 fromArgs = fromArgs.tail;
mcimadamore@787 1172 toArgs = toArgs.head == varargsTypeTo ?
mcimadamore@787 1173 toArgs :
mcimadamore@787 1174 toArgs.tail;
mcimadamore@787 1175 }
mcimadamore@787 1176 } else {
mcimadamore@787 1177 //formal argument list is same as original list where last
mcimadamore@787 1178 //argument (array type) is removed
mcimadamore@787 1179 args.appendList(toArgs.reverse().tail.reverse());
mcimadamore@775 1180 }
mcimadamore@787 1181 //append varargs element type as last synthetic formal
mcimadamore@787 1182 args.append(types.elemtype(varargsTypeTo));
dlsmith@880 1183 Type mtype = types.createMethodTypeWithParameters(to.type, args.toList());
mcimadamore@1006 1184 return new MethodSymbol(to.flags_field & ~VARARGS, to.name, mtype, to.owner);
mcimadamore@775 1185 } else {
mcimadamore@775 1186 return to;
mcimadamore@775 1187 }
mcimadamore@775 1188 }
mcimadamore@844 1189 //where
mcimadamore@1059 1190 Type mostSpecificReturnType(Type mt1, Type mt2) {
mcimadamore@1059 1191 Type rt1 = mt1.getReturnType();
mcimadamore@1059 1192 Type rt2 = mt2.getReturnType();
mcimadamore@1059 1193
mcimadamore@1059 1194 if (mt1.tag == FORALL && mt2.tag == FORALL) {
mcimadamore@1059 1195 //if both are generic methods, adjust return type ahead of subtyping check
mcimadamore@1059 1196 rt1 = types.subst(rt1, mt1.getTypeArguments(), mt2.getTypeArguments());
mcimadamore@1059 1197 }
mcimadamore@1059 1198 //first use subtyping, then return type substitutability
mcimadamore@1059 1199 if (types.isSubtype(rt1, rt2)) {
mcimadamore@1059 1200 return mt1;
mcimadamore@1059 1201 } else if (types.isSubtype(rt2, rt1)) {
mcimadamore@1059 1202 return mt2;
mcimadamore@1059 1203 } else if (types.returnTypeSubstitutable(mt1, mt2)) {
mcimadamore@1059 1204 return mt1;
mcimadamore@1059 1205 } else if (types.returnTypeSubstitutable(mt2, mt1)) {
mcimadamore@1059 1206 return mt2;
mcimadamore@1059 1207 } else {
mcimadamore@1059 1208 return null;
mcimadamore@1059 1209 }
mcimadamore@1059 1210 }
mcimadamore@1059 1211 //where
mcimadamore@844 1212 Symbol ambiguityError(Symbol m1, Symbol m2) {
mcimadamore@844 1213 if (((m1.flags() | m2.flags()) & CLASH) != 0) {
mcimadamore@844 1214 return (m1.flags() & CLASH) == 0 ? m1 : m2;
mcimadamore@844 1215 } else {
mcimadamore@844 1216 return new AmbiguityError(m1, m2);
mcimadamore@844 1217 }
mcimadamore@844 1218 }
duke@1 1219
duke@1 1220 /** Find best qualified method matching given name, type and value
duke@1 1221 * arguments.
duke@1 1222 * @param env The current environment.
duke@1 1223 * @param site The original type from where the selection
duke@1 1224 * takes place.
duke@1 1225 * @param name The method's name.
duke@1 1226 * @param argtypes The method's value arguments.
duke@1 1227 * @param typeargtypes The method's type arguments
duke@1 1228 * @param allowBoxing Allow boxing conversions of arguments.
duke@1 1229 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 1230 */
duke@1 1231 Symbol findMethod(Env<AttrContext> env,
duke@1 1232 Type site,
duke@1 1233 Name name,
duke@1 1234 List<Type> argtypes,
duke@1 1235 List<Type> typeargtypes,
duke@1 1236 boolean allowBoxing,
duke@1 1237 boolean useVarargs,
duke@1 1238 boolean operator) {
jrose@571 1239 Symbol bestSoFar = methodNotFound;
mcimadamore@1114 1240 bestSoFar = findMethod(env,
duke@1 1241 site,
duke@1 1242 name,
duke@1 1243 argtypes,
duke@1 1244 typeargtypes,
duke@1 1245 site.tsym.type,
jrose@571 1246 bestSoFar,
duke@1 1247 allowBoxing,
duke@1 1248 useVarargs,
mcimadamore@1335 1249 operator);
mcimadamore@1114 1250 reportVerboseResolutionDiagnostic(env.tree.pos(), name, site, argtypes, typeargtypes, bestSoFar);
mcimadamore@1114 1251 return bestSoFar;
duke@1 1252 }
duke@1 1253 // where
duke@1 1254 private Symbol findMethod(Env<AttrContext> env,
duke@1 1255 Type site,
duke@1 1256 Name name,
duke@1 1257 List<Type> argtypes,
duke@1 1258 List<Type> typeargtypes,
duke@1 1259 Type intype,
duke@1 1260 Symbol bestSoFar,
duke@1 1261 boolean allowBoxing,
duke@1 1262 boolean useVarargs,
mcimadamore@1335 1263 boolean operator) {
mcimadamore@1335 1264 boolean abstractOk = true;
mcimadamore@1335 1265 List<Type> itypes = List.nil();
mcimadamore@1335 1266 for (TypeSymbol s : superclasses(intype)) {
mcimadamore@1335 1267 bestSoFar = lookupMethod(env, site, name, argtypes, typeargtypes,
mcimadamore@1335 1268 s.members(), bestSoFar, allowBoxing, useVarargs, operator, true);
mcimadamore@1342 1269 //We should not look for abstract methods if receiver is a concrete class
mcimadamore@1342 1270 //(as concrete classes are expected to implement all abstracts coming
mcimadamore@1342 1271 //from superinterfaces)
mcimadamore@1342 1272 abstractOk &= (s.flags() & (ABSTRACT | INTERFACE | ENUM)) != 0;
mcimadamore@1335 1273 if (abstractOk) {
mcimadamore@1335 1274 for (Type itype : types.interfaces(s.type)) {
mcimadamore@1335 1275 itypes = types.union(types.closure(itype), itypes);
duke@1 1276 }
duke@1 1277 }
mcimadamore@1335 1278 if (name == names.init) break;
mcimadamore@1335 1279 }
mcimadamore@1335 1280
mcimadamore@1335 1281 Symbol concrete = bestSoFar.kind < ERR &&
mcimadamore@1335 1282 (bestSoFar.flags() & ABSTRACT) == 0 ?
mcimadamore@1335 1283 bestSoFar : methodNotFound;
mcimadamore@1335 1284
mcimadamore@1335 1285 if (name != names.init) {
mcimadamore@1335 1286 //keep searching for abstract methods
mcimadamore@1335 1287 for (Type itype : itypes) {
mcimadamore@1335 1288 if (!itype.isInterface()) continue; //skip j.l.Object (included by Types.closure())
mcimadamore@1335 1289 bestSoFar = lookupMethod(env, site, name, argtypes, typeargtypes,
mcimadamore@1335 1290 itype.tsym.members(), bestSoFar, allowBoxing, useVarargs, operator, true);
mcimadamore@1335 1291 if (concrete != bestSoFar &&
mcimadamore@1335 1292 concrete.kind < ERR && bestSoFar.kind < ERR &&
mcimadamore@1335 1293 types.isSubSignature(concrete.type, bestSoFar.type)) {
mcimadamore@1335 1294 //this is an hack - as javac does not do full membership checks
mcimadamore@1335 1295 //most specific ends up comparing abstract methods that might have
mcimadamore@1335 1296 //been implemented by some concrete method in a subclass and,
mcimadamore@1335 1297 //because of raw override, it is possible for an abstract method
mcimadamore@1335 1298 //to be more specific than the concrete method - so we need
mcimadamore@1335 1299 //to explicitly call that out (see CR 6178365)
mcimadamore@1335 1300 bestSoFar = concrete;
mcimadamore@1335 1301 }
duke@1 1302 }
duke@1 1303 }
duke@1 1304 return bestSoFar;
duke@1 1305 }
duke@1 1306
mcimadamore@1335 1307 /**
mcimadamore@1335 1308 * Return an Iterable object to scan the superclasses of a given type.
mcimadamore@1335 1309 * It's crucial that the scan is done lazily, as we don't want to accidentally
mcimadamore@1335 1310 * access more supertypes than strictly needed (as this could trigger completion
mcimadamore@1335 1311 * errors if some of the not-needed supertypes are missing/ill-formed).
mcimadamore@1335 1312 */
mcimadamore@1335 1313 Iterable<TypeSymbol> superclasses(final Type intype) {
mcimadamore@1335 1314 return new Iterable<TypeSymbol>() {
mcimadamore@1335 1315 public Iterator<TypeSymbol> iterator() {
mcimadamore@1335 1316 return new Iterator<TypeSymbol>() {
mcimadamore@1335 1317
mcimadamore@1335 1318 List<TypeSymbol> seen = List.nil();
mcimadamore@1342 1319 TypeSymbol currentSym = symbolFor(intype);
mcimadamore@1342 1320 TypeSymbol prevSym = null;
mcimadamore@1335 1321
mcimadamore@1335 1322 public boolean hasNext() {
mcimadamore@1342 1323 if (currentSym == syms.noSymbol) {
mcimadamore@1342 1324 currentSym = symbolFor(types.supertype(prevSym.type));
mcimadamore@1342 1325 }
mcimadamore@1335 1326 return currentSym != null;
mcimadamore@1335 1327 }
mcimadamore@1335 1328
mcimadamore@1335 1329 public TypeSymbol next() {
mcimadamore@1342 1330 prevSym = currentSym;
mcimadamore@1342 1331 currentSym = syms.noSymbol;
mcimadamore@1342 1332 Assert.check(prevSym != null || prevSym != syms.noSymbol);
mcimadamore@1335 1333 return prevSym;
mcimadamore@1335 1334 }
mcimadamore@1335 1335
mcimadamore@1335 1336 public void remove() {
mcimadamore@1342 1337 throw new UnsupportedOperationException();
mcimadamore@1335 1338 }
mcimadamore@1335 1339
mcimadamore@1342 1340 TypeSymbol symbolFor(Type t) {
mcimadamore@1342 1341 if (t.tag != CLASS &&
mcimadamore@1342 1342 t.tag != TYPEVAR) {
mcimadamore@1335 1343 return null;
mcimadamore@1335 1344 }
mcimadamore@1342 1345 while (t.tag == TYPEVAR)
mcimadamore@1342 1346 t = t.getUpperBound();
mcimadamore@1342 1347 if (seen.contains(t.tsym)) {
mcimadamore@1335 1348 //degenerate case in which we have a circular
mcimadamore@1335 1349 //class hierarchy - because of ill-formed classfiles
mcimadamore@1335 1350 return null;
mcimadamore@1335 1351 }
mcimadamore@1342 1352 seen = seen.prepend(t.tsym);
mcimadamore@1342 1353 return t.tsym;
mcimadamore@1335 1354 }
mcimadamore@1335 1355 };
mcimadamore@1335 1356 }
mcimadamore@1335 1357 };
mcimadamore@1335 1358 }
mcimadamore@1335 1359
mcimadamore@1335 1360 /**
mcimadamore@1335 1361 * Lookup a method with given name and argument types in a given scope
mcimadamore@1335 1362 */
mcimadamore@1335 1363 Symbol lookupMethod(Env<AttrContext> env,
mcimadamore@1335 1364 Type site,
mcimadamore@1335 1365 Name name,
mcimadamore@1335 1366 List<Type> argtypes,
mcimadamore@1335 1367 List<Type> typeargtypes,
mcimadamore@1335 1368 Scope sc,
mcimadamore@1335 1369 Symbol bestSoFar,
mcimadamore@1335 1370 boolean allowBoxing,
mcimadamore@1335 1371 boolean useVarargs,
mcimadamore@1335 1372 boolean operator,
mcimadamore@1335 1373 boolean abstractok) {
mcimadamore@1335 1374 for (Symbol s : sc.getElementsByName(name, lookupFilter)) {
mcimadamore@1335 1375 bestSoFar = selectBest(env, site, argtypes, typeargtypes, s,
mcimadamore@1335 1376 bestSoFar, allowBoxing, useVarargs, operator);
mcimadamore@1335 1377 }
mcimadamore@1335 1378 return bestSoFar;
mcimadamore@1335 1379 }
mcimadamore@1335 1380 //where
mcimadamore@1335 1381 Filter<Symbol> lookupFilter = new Filter<Symbol>() {
mcimadamore@1335 1382 public boolean accepts(Symbol s) {
mcimadamore@1335 1383 return s.kind == MTH &&
mcimadamore@1335 1384 (s.flags() & SYNTHETIC) == 0;
mcimadamore@1335 1385 }
mcimadamore@1335 1386 };
mcimadamore@1335 1387
duke@1 1388 /** Find unqualified method matching given name, type and value arguments.
duke@1 1389 * @param env The current environment.
duke@1 1390 * @param name The method's name.
duke@1 1391 * @param argtypes The method's value arguments.
duke@1 1392 * @param typeargtypes The method's type arguments.
duke@1 1393 * @param allowBoxing Allow boxing conversions of arguments.
duke@1 1394 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 1395 */
duke@1 1396 Symbol findFun(Env<AttrContext> env, Name name,
duke@1 1397 List<Type> argtypes, List<Type> typeargtypes,
duke@1 1398 boolean allowBoxing, boolean useVarargs) {
duke@1 1399 Symbol bestSoFar = methodNotFound;
duke@1 1400 Symbol sym;
duke@1 1401 Env<AttrContext> env1 = env;
duke@1 1402 boolean staticOnly = false;
duke@1 1403 while (env1.outer != null) {
duke@1 1404 if (isStatic(env1)) staticOnly = true;
duke@1 1405 sym = findMethod(
duke@1 1406 env1, env1.enclClass.sym.type, name, argtypes, typeargtypes,
duke@1 1407 allowBoxing, useVarargs, false);
duke@1 1408 if (sym.exists()) {
duke@1 1409 if (staticOnly &&
duke@1 1410 sym.kind == MTH &&
duke@1 1411 sym.owner.kind == TYP &&
duke@1 1412 (sym.flags() & STATIC) == 0) return new StaticError(sym);
duke@1 1413 else return sym;
duke@1 1414 } else if (sym.kind < bestSoFar.kind) {
duke@1 1415 bestSoFar = sym;
duke@1 1416 }
duke@1 1417 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
duke@1 1418 env1 = env1.outer;
duke@1 1419 }
duke@1 1420
duke@1 1421 sym = findMethod(env, syms.predefClass.type, name, argtypes,
duke@1 1422 typeargtypes, allowBoxing, useVarargs, false);
duke@1 1423 if (sym.exists())
duke@1 1424 return sym;
duke@1 1425
duke@1 1426 Scope.Entry e = env.toplevel.namedImportScope.lookup(name);
duke@1 1427 for (; e.scope != null; e = e.next()) {
duke@1 1428 sym = e.sym;
duke@1 1429 Type origin = e.getOrigin().owner.type;
duke@1 1430 if (sym.kind == MTH) {
duke@1 1431 if (e.sym.owner.type != origin)
duke@1 1432 sym = sym.clone(e.getOrigin().owner);
duke@1 1433 if (!isAccessible(env, origin, sym))
duke@1 1434 sym = new AccessError(env, origin, sym);
duke@1 1435 bestSoFar = selectBest(env, origin,
duke@1 1436 argtypes, typeargtypes,
duke@1 1437 sym, bestSoFar,
duke@1 1438 allowBoxing, useVarargs, false);
duke@1 1439 }
duke@1 1440 }
duke@1 1441 if (bestSoFar.exists())
duke@1 1442 return bestSoFar;
duke@1 1443
duke@1 1444 e = env.toplevel.starImportScope.lookup(name);
duke@1 1445 for (; e.scope != null; e = e.next()) {
duke@1 1446 sym = e.sym;
duke@1 1447 Type origin = e.getOrigin().owner.type;
duke@1 1448 if (sym.kind == MTH) {
duke@1 1449 if (e.sym.owner.type != origin)
duke@1 1450 sym = sym.clone(e.getOrigin().owner);
duke@1 1451 if (!isAccessible(env, origin, sym))
duke@1 1452 sym = new AccessError(env, origin, sym);
duke@1 1453 bestSoFar = selectBest(env, origin,
duke@1 1454 argtypes, typeargtypes,
duke@1 1455 sym, bestSoFar,
duke@1 1456 allowBoxing, useVarargs, false);
duke@1 1457 }
duke@1 1458 }
duke@1 1459 return bestSoFar;
duke@1 1460 }
duke@1 1461
duke@1 1462 /** Load toplevel or member class with given fully qualified name and
duke@1 1463 * verify that it is accessible.
duke@1 1464 * @param env The current environment.
duke@1 1465 * @param name The fully qualified name of the class to be loaded.
duke@1 1466 */
duke@1 1467 Symbol loadClass(Env<AttrContext> env, Name name) {
duke@1 1468 try {
duke@1 1469 ClassSymbol c = reader.loadClass(name);
duke@1 1470 return isAccessible(env, c) ? c : new AccessError(c);
duke@1 1471 } catch (ClassReader.BadClassFile err) {
duke@1 1472 throw err;
duke@1 1473 } catch (CompletionFailure ex) {
duke@1 1474 return typeNotFound;
duke@1 1475 }
duke@1 1476 }
duke@1 1477
duke@1 1478 /** Find qualified member type.
duke@1 1479 * @param env The current environment.
duke@1 1480 * @param site The original type from where the selection takes
duke@1 1481 * place.
duke@1 1482 * @param name The type's name.
duke@1 1483 * @param c The class to search for the member type. This is
duke@1 1484 * always a superclass or implemented interface of
duke@1 1485 * site's class.
duke@1 1486 */
duke@1 1487 Symbol findMemberType(Env<AttrContext> env,
duke@1 1488 Type site,
duke@1 1489 Name name,
duke@1 1490 TypeSymbol c) {
duke@1 1491 Symbol bestSoFar = typeNotFound;
duke@1 1492 Symbol sym;
duke@1 1493 Scope.Entry e = c.members().lookup(name);
duke@1 1494 while (e.scope != null) {
duke@1 1495 if (e.sym.kind == TYP) {
duke@1 1496 return isAccessible(env, site, e.sym)
duke@1 1497 ? e.sym
duke@1 1498 : new AccessError(env, site, e.sym);
duke@1 1499 }
duke@1 1500 e = e.next();
duke@1 1501 }
duke@1 1502 Type st = types.supertype(c.type);
duke@1 1503 if (st != null && st.tag == CLASS) {
duke@1 1504 sym = findMemberType(env, site, name, st.tsym);
duke@1 1505 if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1506 }
duke@1 1507 for (List<Type> l = types.interfaces(c.type);
duke@1 1508 bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
duke@1 1509 l = l.tail) {
duke@1 1510 sym = findMemberType(env, site, name, l.head.tsym);
duke@1 1511 if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS &&
duke@1 1512 sym.owner != bestSoFar.owner)
duke@1 1513 bestSoFar = new AmbiguityError(bestSoFar, sym);
duke@1 1514 else if (sym.kind < bestSoFar.kind)
duke@1 1515 bestSoFar = sym;
duke@1 1516 }
duke@1 1517 return bestSoFar;
duke@1 1518 }
duke@1 1519
duke@1 1520 /** Find a global type in given scope and load corresponding class.
duke@1 1521 * @param env The current environment.
duke@1 1522 * @param scope The scope in which to look for the type.
duke@1 1523 * @param name The type's name.
duke@1 1524 */
duke@1 1525 Symbol findGlobalType(Env<AttrContext> env, Scope scope, Name name) {
duke@1 1526 Symbol bestSoFar = typeNotFound;
duke@1 1527 for (Scope.Entry e = scope.lookup(name); e.scope != null; e = e.next()) {
duke@1 1528 Symbol sym = loadClass(env, e.sym.flatName());
duke@1 1529 if (bestSoFar.kind == TYP && sym.kind == TYP &&
duke@1 1530 bestSoFar != sym)
duke@1 1531 return new AmbiguityError(bestSoFar, sym);
duke@1 1532 else if (sym.kind < bestSoFar.kind)
duke@1 1533 bestSoFar = sym;
duke@1 1534 }
duke@1 1535 return bestSoFar;
duke@1 1536 }
duke@1 1537
duke@1 1538 /** Find an unqualified type symbol.
duke@1 1539 * @param env The current environment.
duke@1 1540 * @param name The type's name.
duke@1 1541 */
duke@1 1542 Symbol findType(Env<AttrContext> env, Name name) {
duke@1 1543 Symbol bestSoFar = typeNotFound;
duke@1 1544 Symbol sym;
duke@1 1545 boolean staticOnly = false;
duke@1 1546 for (Env<AttrContext> env1 = env; env1.outer != null; env1 = env1.outer) {
duke@1 1547 if (isStatic(env1)) staticOnly = true;
duke@1 1548 for (Scope.Entry e = env1.info.scope.lookup(name);
duke@1 1549 e.scope != null;
duke@1 1550 e = e.next()) {
duke@1 1551 if (e.sym.kind == TYP) {
duke@1 1552 if (staticOnly &&
duke@1 1553 e.sym.type.tag == TYPEVAR &&
duke@1 1554 e.sym.owner.kind == TYP) return new StaticError(e.sym);
duke@1 1555 return e.sym;
duke@1 1556 }
duke@1 1557 }
duke@1 1558
duke@1 1559 sym = findMemberType(env1, env1.enclClass.sym.type, name,
duke@1 1560 env1.enclClass.sym);
duke@1 1561 if (staticOnly && sym.kind == TYP &&
duke@1 1562 sym.type.tag == CLASS &&
duke@1 1563 sym.type.getEnclosingType().tag == CLASS &&
duke@1 1564 env1.enclClass.sym.type.isParameterized() &&
duke@1 1565 sym.type.getEnclosingType().isParameterized())
duke@1 1566 return new StaticError(sym);
duke@1 1567 else if (sym.exists()) return sym;
duke@1 1568 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1569
duke@1 1570 JCClassDecl encl = env1.baseClause ? (JCClassDecl)env1.tree : env1.enclClass;
duke@1 1571 if ((encl.sym.flags() & STATIC) != 0)
duke@1 1572 staticOnly = true;
duke@1 1573 }
duke@1 1574
jjg@1127 1575 if (!env.tree.hasTag(IMPORT)) {
duke@1 1576 sym = findGlobalType(env, env.toplevel.namedImportScope, name);
duke@1 1577 if (sym.exists()) return sym;
duke@1 1578 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1579
duke@1 1580 sym = findGlobalType(env, env.toplevel.packge.members(), name);
duke@1 1581 if (sym.exists()) return sym;
duke@1 1582 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1583
duke@1 1584 sym = findGlobalType(env, env.toplevel.starImportScope, name);
duke@1 1585 if (sym.exists()) return sym;
duke@1 1586 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1587 }
duke@1 1588
duke@1 1589 return bestSoFar;
duke@1 1590 }
duke@1 1591
duke@1 1592 /** Find an unqualified identifier which matches a specified kind set.
duke@1 1593 * @param env The current environment.
duke@1 1594 * @param name The indentifier's name.
duke@1 1595 * @param kind Indicates the possible symbol kinds
duke@1 1596 * (a subset of VAL, TYP, PCK).
duke@1 1597 */
duke@1 1598 Symbol findIdent(Env<AttrContext> env, Name name, int kind) {
duke@1 1599 Symbol bestSoFar = typeNotFound;
duke@1 1600 Symbol sym;
duke@1 1601
duke@1 1602 if ((kind & VAR) != 0) {
duke@1 1603 sym = findVar(env, name);
duke@1 1604 if (sym.exists()) return sym;
duke@1 1605 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1606 }
duke@1 1607
duke@1 1608 if ((kind & TYP) != 0) {
duke@1 1609 sym = findType(env, name);
duke@1 1610 if (sym.exists()) return sym;
duke@1 1611 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1612 }
duke@1 1613
duke@1 1614 if ((kind & PCK) != 0) return reader.enterPackage(name);
duke@1 1615 else return bestSoFar;
duke@1 1616 }
duke@1 1617
duke@1 1618 /** Find an identifier in a package which matches a specified kind set.
duke@1 1619 * @param env The current environment.
duke@1 1620 * @param name The identifier's name.
duke@1 1621 * @param kind Indicates the possible symbol kinds
duke@1 1622 * (a nonempty subset of TYP, PCK).
duke@1 1623 */
duke@1 1624 Symbol findIdentInPackage(Env<AttrContext> env, TypeSymbol pck,
duke@1 1625 Name name, int kind) {
duke@1 1626 Name fullname = TypeSymbol.formFullName(name, pck);
duke@1 1627 Symbol bestSoFar = typeNotFound;
duke@1 1628 PackageSymbol pack = null;
duke@1 1629 if ((kind & PCK) != 0) {
duke@1 1630 pack = reader.enterPackage(fullname);
duke@1 1631 if (pack.exists()) return pack;
duke@1 1632 }
duke@1 1633 if ((kind & TYP) != 0) {
duke@1 1634 Symbol sym = loadClass(env, fullname);
duke@1 1635 if (sym.exists()) {
duke@1 1636 // don't allow programs to use flatnames
duke@1 1637 if (name == sym.name) return sym;
duke@1 1638 }
duke@1 1639 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1640 }
duke@1 1641 return (pack != null) ? pack : bestSoFar;
duke@1 1642 }
duke@1 1643
duke@1 1644 /** Find an identifier among the members of a given type `site'.
duke@1 1645 * @param env The current environment.
duke@1 1646 * @param site The type containing the symbol to be found.
duke@1 1647 * @param name The identifier's name.
duke@1 1648 * @param kind Indicates the possible symbol kinds
duke@1 1649 * (a subset of VAL, TYP).
duke@1 1650 */
duke@1 1651 Symbol findIdentInType(Env<AttrContext> env, Type site,
duke@1 1652 Name name, int kind) {
duke@1 1653 Symbol bestSoFar = typeNotFound;
duke@1 1654 Symbol sym;
duke@1 1655 if ((kind & VAR) != 0) {
duke@1 1656 sym = findField(env, site, name, site.tsym);
duke@1 1657 if (sym.exists()) return sym;
duke@1 1658 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1659 }
duke@1 1660
duke@1 1661 if ((kind & TYP) != 0) {
duke@1 1662 sym = findMemberType(env, site, name, site.tsym);
duke@1 1663 if (sym.exists()) return sym;
duke@1 1664 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1665 }
duke@1 1666 return bestSoFar;
duke@1 1667 }
duke@1 1668
duke@1 1669 /* ***************************************************************************
duke@1 1670 * Access checking
duke@1 1671 * The following methods convert ResolveErrors to ErrorSymbols, issuing
duke@1 1672 * an error message in the process
duke@1 1673 ****************************************************************************/
duke@1 1674
duke@1 1675 /** If `sym' is a bad symbol: report error and return errSymbol
duke@1 1676 * else pass through unchanged,
duke@1 1677 * additional arguments duplicate what has been used in trying to find the
jjg@1326 1678 * symbol {@literal (--> flyweight pattern)}. This improves performance since we
duke@1 1679 * expect misses to happen frequently.
duke@1 1680 *
duke@1 1681 * @param sym The symbol that was found, or a ResolveError.
duke@1 1682 * @param pos The position to use for error reporting.
mcimadamore@1347 1683 * @param location The symbol the served as a context for this lookup
duke@1 1684 * @param site The original type from where the selection took place.
duke@1 1685 * @param name The symbol's name.
mcimadamore@1347 1686 * @param qualified Did we get here through a qualified expression resolution?
duke@1 1687 * @param argtypes The invocation's value arguments,
duke@1 1688 * if we looked for a method.
duke@1 1689 * @param typeargtypes The invocation's type arguments,
duke@1 1690 * if we looked for a method.
mcimadamore@1347 1691 * @param logResolveHelper helper class used to log resolve errors
duke@1 1692 */
mcimadamore@1347 1693 Symbol accessInternal(Symbol sym,
mcimadamore@1347 1694 DiagnosticPosition pos,
mcimadamore@1347 1695 Symbol location,
mcimadamore@1347 1696 Type site,
mcimadamore@1347 1697 Name name,
mcimadamore@1347 1698 boolean qualified,
mcimadamore@1347 1699 List<Type> argtypes,
mcimadamore@1347 1700 List<Type> typeargtypes,
mcimadamore@1347 1701 LogResolveHelper logResolveHelper) {
mcimadamore@1347 1702 if (sym.kind >= AMBIGUOUS) {
mcimadamore@1347 1703 ResolveError errSym = (ResolveError)sym;
mcimadamore@1347 1704 sym = errSym.access(name, qualified ? site.tsym : syms.noSymbol);
mcimadamore@1347 1705 argtypes = logResolveHelper.getArgumentTypes(errSym, sym, name, argtypes);
mcimadamore@1347 1706 if (logResolveHelper.resolveDiagnosticNeeded(site, argtypes, typeargtypes)) {
mcimadamore@1347 1707 logResolveError(errSym, pos, location, site, name, argtypes, typeargtypes);
mcimadamore@1347 1708 }
mcimadamore@1347 1709 }
mcimadamore@1347 1710 return sym;
mcimadamore@1347 1711 }
mcimadamore@1347 1712
mcimadamore@1347 1713 /**
mcimadamore@1347 1714 * Variant of the generalized access routine, to be used for generating method
mcimadamore@1347 1715 * resolution diagnostics
mcimadamore@1347 1716 */
mcimadamore@1347 1717 Symbol accessMethod(Symbol sym,
duke@1 1718 DiagnosticPosition pos,
mcimadamore@829 1719 Symbol location,
duke@1 1720 Type site,
duke@1 1721 Name name,
duke@1 1722 boolean qualified,
duke@1 1723 List<Type> argtypes,
duke@1 1724 List<Type> typeargtypes) {
mcimadamore@1347 1725 return accessInternal(sym, pos, location, site, name, qualified, argtypes, typeargtypes, methodLogResolveHelper);
duke@1 1726 }
duke@1 1727
mcimadamore@1347 1728 /** Same as original accessMethod(), but without location.
mcimadamore@829 1729 */
mcimadamore@1347 1730 Symbol accessMethod(Symbol sym,
mcimadamore@829 1731 DiagnosticPosition pos,
mcimadamore@829 1732 Type site,
mcimadamore@829 1733 Name name,
mcimadamore@829 1734 boolean qualified,
mcimadamore@829 1735 List<Type> argtypes,
mcimadamore@829 1736 List<Type> typeargtypes) {
mcimadamore@1347 1737 return accessMethod(sym, pos, site.tsym, site, name, qualified, argtypes, typeargtypes);
mcimadamore@829 1738 }
mcimadamore@829 1739
mcimadamore@1347 1740 /**
mcimadamore@1347 1741 * Variant of the generalized access routine, to be used for generating variable,
mcimadamore@1347 1742 * type resolution diagnostics
mcimadamore@829 1743 */
mcimadamore@1347 1744 Symbol accessBase(Symbol sym,
mcimadamore@829 1745 DiagnosticPosition pos,
mcimadamore@829 1746 Symbol location,
mcimadamore@829 1747 Type site,
mcimadamore@829 1748 Name name,
mcimadamore@829 1749 boolean qualified) {
mcimadamore@1347 1750 return accessInternal(sym, pos, location, site, name, qualified, List.<Type>nil(), null, basicLogResolveHelper);
mcimadamore@829 1751 }
mcimadamore@829 1752
mcimadamore@1347 1753 /** Same as original accessBase(), but without location.
duke@1 1754 */
mcimadamore@1347 1755 Symbol accessBase(Symbol sym,
duke@1 1756 DiagnosticPosition pos,
duke@1 1757 Type site,
duke@1 1758 Name name,
duke@1 1759 boolean qualified) {
mcimadamore@1347 1760 return accessBase(sym, pos, site.tsym, site, name, qualified);
duke@1 1761 }
duke@1 1762
mcimadamore@1347 1763 interface LogResolveHelper {
mcimadamore@1347 1764 boolean resolveDiagnosticNeeded(Type site, List<Type> argtypes, List<Type> typeargtypes);
mcimadamore@1347 1765 List<Type> getArgumentTypes(ResolveError errSym, Symbol accessedSym, Name name, List<Type> argtypes);
mcimadamore@1347 1766 }
mcimadamore@1347 1767
mcimadamore@1347 1768 LogResolveHelper basicLogResolveHelper = new LogResolveHelper() {
mcimadamore@1347 1769 public boolean resolveDiagnosticNeeded(Type site, List<Type> argtypes, List<Type> typeargtypes) {
mcimadamore@1347 1770 return !site.isErroneous();
mcimadamore@1347 1771 }
mcimadamore@1347 1772 public List<Type> getArgumentTypes(ResolveError errSym, Symbol accessedSym, Name name, List<Type> argtypes) {
mcimadamore@1347 1773 return argtypes;
mcimadamore@1347 1774 }
mcimadamore@1347 1775 };
mcimadamore@1347 1776
mcimadamore@1347 1777 LogResolveHelper methodLogResolveHelper = new LogResolveHelper() {
mcimadamore@1347 1778 public boolean resolveDiagnosticNeeded(Type site, List<Type> argtypes, List<Type> typeargtypes) {
mcimadamore@1347 1779 return !site.isErroneous() &&
mcimadamore@1347 1780 !Type.isErroneous(argtypes) &&
mcimadamore@1347 1781 (typeargtypes == null || !Type.isErroneous(typeargtypes));
mcimadamore@1347 1782 }
mcimadamore@1347 1783 public List<Type> getArgumentTypes(ResolveError errSym, Symbol accessedSym, Name name, List<Type> argtypes) {
mcimadamore@1347 1784 if (syms.operatorNames.contains(name)) {
mcimadamore@1347 1785 return argtypes;
mcimadamore@1347 1786 } else {
mcimadamore@1347 1787 Symbol msym = errSym.kind == WRONG_MTH ?
mcimadamore@1347 1788 ((InapplicableSymbolError)errSym).errCandidate().sym : accessedSym;
mcimadamore@1347 1789
mcimadamore@1347 1790 List<Type> argtypes2 = Type.map(argtypes,
mcimadamore@1347 1791 deferredAttr.new RecoveryDeferredTypeMap(AttrMode.SPECULATIVE, msym, currentResolutionContext.firstErroneousResolutionPhase()));
mcimadamore@1347 1792
mcimadamore@1347 1793 if (msym != accessedSym) {
mcimadamore@1347 1794 //fixup deferred type caches - this 'hack' is required because the symbol
mcimadamore@1347 1795 //returned by InapplicableSymbolError.access() will hide the candidate
mcimadamore@1347 1796 //method symbol that can be used for lookups in the speculative cache,
mcimadamore@1347 1797 //causing problems in Attr.checkId()
mcimadamore@1347 1798 for (Type t : argtypes) {
mcimadamore@1347 1799 if (t.tag == DEFERRED) {
mcimadamore@1347 1800 DeferredType dt = (DeferredType)t;
mcimadamore@1347 1801 dt.speculativeCache.dupAllTo(msym, accessedSym);
mcimadamore@1347 1802 }
mcimadamore@1347 1803 }
mcimadamore@1347 1804 }
mcimadamore@1347 1805 return argtypes2;
mcimadamore@1347 1806 }
mcimadamore@1347 1807 }
mcimadamore@1347 1808 };
mcimadamore@1347 1809
duke@1 1810 /** Check that sym is not an abstract method.
duke@1 1811 */
duke@1 1812 void checkNonAbstract(DiagnosticPosition pos, Symbol sym) {
duke@1 1813 if ((sym.flags() & ABSTRACT) != 0)
duke@1 1814 log.error(pos, "abstract.cant.be.accessed.directly",
duke@1 1815 kindName(sym), sym, sym.location());
duke@1 1816 }
duke@1 1817
duke@1 1818 /* ***************************************************************************
duke@1 1819 * Debugging
duke@1 1820 ****************************************************************************/
duke@1 1821
duke@1 1822 /** print all scopes starting with scope s and proceeding outwards.
duke@1 1823 * used for debugging.
duke@1 1824 */
duke@1 1825 public void printscopes(Scope s) {
duke@1 1826 while (s != null) {
duke@1 1827 if (s.owner != null)
duke@1 1828 System.err.print(s.owner + ": ");
duke@1 1829 for (Scope.Entry e = s.elems; e != null; e = e.sibling) {
duke@1 1830 if ((e.sym.flags() & ABSTRACT) != 0)
duke@1 1831 System.err.print("abstract ");
duke@1 1832 System.err.print(e.sym + " ");
duke@1 1833 }
duke@1 1834 System.err.println();
duke@1 1835 s = s.next;
duke@1 1836 }
duke@1 1837 }
duke@1 1838
duke@1 1839 void printscopes(Env<AttrContext> env) {
duke@1 1840 while (env.outer != null) {
duke@1 1841 System.err.println("------------------------------");
duke@1 1842 printscopes(env.info.scope);
duke@1 1843 env = env.outer;
duke@1 1844 }
duke@1 1845 }
duke@1 1846
duke@1 1847 public void printscopes(Type t) {
duke@1 1848 while (t.tag == CLASS) {
duke@1 1849 printscopes(t.tsym.members());
duke@1 1850 t = types.supertype(t);
duke@1 1851 }
duke@1 1852 }
duke@1 1853
duke@1 1854 /* ***************************************************************************
duke@1 1855 * Name resolution
duke@1 1856 * Naming conventions are as for symbol lookup
duke@1 1857 * Unlike the find... methods these methods will report access errors
duke@1 1858 ****************************************************************************/
duke@1 1859
duke@1 1860 /** Resolve an unqualified (non-method) identifier.
duke@1 1861 * @param pos The position to use for error reporting.
duke@1 1862 * @param env The environment current at the identifier use.
duke@1 1863 * @param name The identifier's name.
duke@1 1864 * @param kind The set of admissible symbol kinds for the identifier.
duke@1 1865 */
duke@1 1866 Symbol resolveIdent(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 1867 Name name, int kind) {
mcimadamore@1347 1868 return accessBase(
duke@1 1869 findIdent(env, name, kind),
duke@1 1870 pos, env.enclClass.sym.type, name, false);
duke@1 1871 }
duke@1 1872
duke@1 1873 /** Resolve an unqualified method identifier.
duke@1 1874 * @param pos The position to use for error reporting.
duke@1 1875 * @param env The environment current at the method invocation.
duke@1 1876 * @param name The identifier's name.
duke@1 1877 * @param argtypes The types of the invocation's value arguments.
duke@1 1878 * @param typeargtypes The types of the invocation's type arguments.
duke@1 1879 */
duke@1 1880 Symbol resolveMethod(DiagnosticPosition pos,
duke@1 1881 Env<AttrContext> env,
duke@1 1882 Name name,
duke@1 1883 List<Type> argtypes,
duke@1 1884 List<Type> typeargtypes) {
mcimadamore@1215 1885 MethodResolutionContext prevResolutionContext = currentResolutionContext;
mcimadamore@1215 1886 try {
mcimadamore@1215 1887 currentResolutionContext = new MethodResolutionContext();
mcimadamore@1215 1888 Symbol sym = methodNotFound;
mcimadamore@1215 1889 List<MethodResolutionPhase> steps = methodResolutionSteps;
mcimadamore@1215 1890 while (steps.nonEmpty() &&
mcimadamore@1215 1891 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@1215 1892 sym.kind >= ERRONEOUS) {
mcimadamore@1347 1893 currentResolutionContext.step = env.info.pendingResolutionPhase = steps.head;
mcimadamore@1215 1894 sym = findFun(env, name, argtypes, typeargtypes,
mcimadamore@1215 1895 steps.head.isBoxingRequired,
mcimadamore@1347 1896 steps.head.isVarargsRequired);
mcimadamore@1215 1897 currentResolutionContext.resolutionCache.put(steps.head, sym);
mcimadamore@1215 1898 steps = steps.tail;
mcimadamore@1215 1899 }
mcimadamore@1215 1900 if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error
mcimadamore@1215 1901 MethodResolutionPhase errPhase =
mcimadamore@1215 1902 currentResolutionContext.firstErroneousResolutionPhase();
mcimadamore@1347 1903 sym = accessMethod(currentResolutionContext.resolutionCache.get(errPhase),
mcimadamore@1215 1904 pos, env.enclClass.sym.type, name, false, argtypes, typeargtypes);
mcimadamore@1347 1905 env.info.pendingResolutionPhase = errPhase;
mcimadamore@1215 1906 }
mcimadamore@1215 1907 return sym;
duke@1 1908 }
mcimadamore@1215 1909 finally {
mcimadamore@1215 1910 currentResolutionContext = prevResolutionContext;
duke@1 1911 }
mcimadamore@689 1912 }
mcimadamore@689 1913
duke@1 1914 /** Resolve a qualified method identifier
duke@1 1915 * @param pos The position to use for error reporting.
duke@1 1916 * @param env The environment current at the method invocation.
duke@1 1917 * @param site The type of the qualifying expression, in which
duke@1 1918 * identifier is searched.
duke@1 1919 * @param name The identifier's name.
duke@1 1920 * @param argtypes The types of the invocation's value arguments.
duke@1 1921 * @param typeargtypes The types of the invocation's type arguments.
duke@1 1922 */
duke@1 1923 Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 1924 Type site, Name name, List<Type> argtypes,
duke@1 1925 List<Type> typeargtypes) {
mcimadamore@829 1926 return resolveQualifiedMethod(pos, env, site.tsym, site, name, argtypes, typeargtypes);
mcimadamore@829 1927 }
mcimadamore@829 1928 Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env,
mcimadamore@829 1929 Symbol location, Type site, Name name, List<Type> argtypes,
mcimadamore@829 1930 List<Type> typeargtypes) {
mcimadamore@1215 1931 return resolveQualifiedMethod(new MethodResolutionContext(), pos, env, location, site, name, argtypes, typeargtypes);
mcimadamore@1215 1932 }
mcimadamore@1215 1933 private Symbol resolveQualifiedMethod(MethodResolutionContext resolveContext,
mcimadamore@1215 1934 DiagnosticPosition pos, Env<AttrContext> env,
mcimadamore@1215 1935 Symbol location, Type site, Name name, List<Type> argtypes,
mcimadamore@1215 1936 List<Type> typeargtypes) {
mcimadamore@1215 1937 MethodResolutionContext prevResolutionContext = currentResolutionContext;
mcimadamore@1215 1938 try {
mcimadamore@1215 1939 currentResolutionContext = resolveContext;
mcimadamore@1215 1940 Symbol sym = methodNotFound;
mcimadamore@1215 1941 List<MethodResolutionPhase> steps = methodResolutionSteps;
mcimadamore@1215 1942 while (steps.nonEmpty() &&
mcimadamore@1215 1943 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@1215 1944 sym.kind >= ERRONEOUS) {
mcimadamore@1347 1945 currentResolutionContext.step = env.info.pendingResolutionPhase = steps.head;
mcimadamore@1215 1946 sym = findMethod(env, site, name, argtypes, typeargtypes,
mcimadamore@1215 1947 steps.head.isBoxingRequired(),
mcimadamore@1347 1948 steps.head.isVarargsRequired(), false);
mcimadamore@1215 1949 currentResolutionContext.resolutionCache.put(steps.head, sym);
mcimadamore@1215 1950 steps = steps.tail;
mcimadamore@1215 1951 }
mcimadamore@1215 1952 if (sym.kind >= AMBIGUOUS) {
mcimadamore@1239 1953 //if nothing is found return the 'first' error
mcimadamore@1239 1954 MethodResolutionPhase errPhase =
mcimadamore@1239 1955 currentResolutionContext.firstErroneousResolutionPhase();
mcimadamore@1347 1956 sym = accessMethod(currentResolutionContext.resolutionCache.get(errPhase),
mcimadamore@1239 1957 pos, location, site, name, true, argtypes, typeargtypes);
mcimadamore@1347 1958 env.info.pendingResolutionPhase = errPhase;
mcimadamore@1239 1959 } else if (allowMethodHandles) {
mcimadamore@1239 1960 MethodSymbol msym = (MethodSymbol)sym;
mcimadamore@1239 1961 if (msym.isSignaturePolymorphic(types)) {
mcimadamore@1347 1962 env.info.pendingResolutionPhase = BASIC;
mcimadamore@1239 1963 return findPolymorphicSignatureInstance(env, sym, argtypes);
mcimadamore@1215 1964 }
mcimadamore@674 1965 }
mcimadamore@1215 1966 return sym;
duke@1 1967 }
mcimadamore@1215 1968 finally {
mcimadamore@1215 1969 currentResolutionContext = prevResolutionContext;
mcimadamore@1215 1970 }
duke@1 1971 }
duke@1 1972
mcimadamore@674 1973 /** Find or create an implicit method of exactly the given type (after erasure).
mcimadamore@674 1974 * Searches in a side table, not the main scope of the site.
mcimadamore@674 1975 * This emulates the lookup process required by JSR 292 in JVM.
mcimadamore@674 1976 * @param env Attribution environment
mcimadamore@1239 1977 * @param spMethod signature polymorphic method - i.e. MH.invokeExact
mcimadamore@1239 1978 * @param argtypes The required argument types
mcimadamore@674 1979 */
mcimadamore@1239 1980 Symbol findPolymorphicSignatureInstance(Env<AttrContext> env,
mcimadamore@1239 1981 Symbol spMethod,
mcimadamore@820 1982 List<Type> argtypes) {
mcimadamore@674 1983 Type mtype = infer.instantiatePolymorphicSignatureInstance(env,
mcimadamore@1347 1984 (MethodSymbol)spMethod, currentResolutionContext, argtypes);
mcimadamore@1239 1985 for (Symbol sym : polymorphicSignatureScope.getElementsByName(spMethod.name)) {
mcimadamore@1239 1986 if (types.isSameType(mtype, sym.type)) {
mcimadamore@1239 1987 return sym;
mcimadamore@674 1988 }
mcimadamore@674 1989 }
mcimadamore@1239 1990
mcimadamore@1239 1991 // create the desired method
mcimadamore@1239 1992 long flags = ABSTRACT | HYPOTHETICAL | spMethod.flags() & Flags.AccessFlags;
mcimadamore@1239 1993 Symbol msym = new MethodSymbol(flags, spMethod.name, mtype, spMethod.owner);
mcimadamore@1239 1994 polymorphicSignatureScope.enter(msym);
mcimadamore@1239 1995 return msym;
mcimadamore@674 1996 }
mcimadamore@674 1997
duke@1 1998 /** Resolve a qualified method identifier, throw a fatal error if not
duke@1 1999 * found.
duke@1 2000 * @param pos The position to use for error reporting.
duke@1 2001 * @param env The environment current at the method invocation.
duke@1 2002 * @param site The type of the qualifying expression, in which
duke@1 2003 * identifier is searched.
duke@1 2004 * @param name The identifier's name.
duke@1 2005 * @param argtypes The types of the invocation's value arguments.
duke@1 2006 * @param typeargtypes The types of the invocation's type arguments.
duke@1 2007 */
duke@1 2008 public MethodSymbol resolveInternalMethod(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 2009 Type site, Name name,
duke@1 2010 List<Type> argtypes,
duke@1 2011 List<Type> typeargtypes) {
mcimadamore@1215 2012 MethodResolutionContext resolveContext = new MethodResolutionContext();
mcimadamore@1215 2013 resolveContext.internalResolution = true;
mcimadamore@1215 2014 Symbol sym = resolveQualifiedMethod(resolveContext, pos, env, site.tsym,
mcimadamore@1215 2015 site, name, argtypes, typeargtypes);
mcimadamore@1215 2016 if (sym.kind == MTH) return (MethodSymbol)sym;
mcimadamore@1215 2017 else throw new FatalError(
mcimadamore@1215 2018 diags.fragment("fatal.err.cant.locate.meth",
mcimadamore@1215 2019 name));
duke@1 2020 }
duke@1 2021
duke@1 2022 /** Resolve constructor.
duke@1 2023 * @param pos The position to use for error reporting.
duke@1 2024 * @param env The environment current at the constructor invocation.
duke@1 2025 * @param site The type of class for which a constructor is searched.
duke@1 2026 * @param argtypes The types of the constructor invocation's value
duke@1 2027 * arguments.
duke@1 2028 * @param typeargtypes The types of the constructor invocation's type
duke@1 2029 * arguments.
duke@1 2030 */
duke@1 2031 Symbol resolveConstructor(DiagnosticPosition pos,
duke@1 2032 Env<AttrContext> env,
duke@1 2033 Type site,
duke@1 2034 List<Type> argtypes,
duke@1 2035 List<Type> typeargtypes) {
mcimadamore@1215 2036 return resolveConstructor(new MethodResolutionContext(), pos, env, site, argtypes, typeargtypes);
mcimadamore@1215 2037 }
mcimadamore@1215 2038 private Symbol resolveConstructor(MethodResolutionContext resolveContext,
mcimadamore@1215 2039 DiagnosticPosition pos,
mcimadamore@1215 2040 Env<AttrContext> env,
mcimadamore@1215 2041 Type site,
mcimadamore@1215 2042 List<Type> argtypes,
mcimadamore@1215 2043 List<Type> typeargtypes) {
mcimadamore@1215 2044 MethodResolutionContext prevResolutionContext = currentResolutionContext;
mcimadamore@1215 2045 try {
mcimadamore@1215 2046 currentResolutionContext = resolveContext;
mcimadamore@1215 2047 Symbol sym = methodNotFound;
mcimadamore@1215 2048 List<MethodResolutionPhase> steps = methodResolutionSteps;
mcimadamore@1215 2049 while (steps.nonEmpty() &&
mcimadamore@1215 2050 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@1215 2051 sym.kind >= ERRONEOUS) {
mcimadamore@1347 2052 currentResolutionContext.step = env.info.pendingResolutionPhase = steps.head;
mcimadamore@1215 2053 sym = findConstructor(pos, env, site, argtypes, typeargtypes,
mcimadamore@1215 2054 steps.head.isBoxingRequired(),
mcimadamore@1347 2055 steps.head.isVarargsRequired());
mcimadamore@1215 2056 currentResolutionContext.resolutionCache.put(steps.head, sym);
mcimadamore@1215 2057 steps = steps.tail;
mcimadamore@1215 2058 }
mcimadamore@1215 2059 if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error
mcimadamore@1215 2060 MethodResolutionPhase errPhase = currentResolutionContext.firstErroneousResolutionPhase();
mcimadamore@1347 2061 sym = accessMethod(currentResolutionContext.resolutionCache.get(errPhase),
mcimadamore@1215 2062 pos, site, names.init, true, argtypes, typeargtypes);
mcimadamore@1347 2063 env.info.pendingResolutionPhase = errPhase;
mcimadamore@1215 2064 }
mcimadamore@1215 2065 return sym;
duke@1 2066 }
mcimadamore@1215 2067 finally {
mcimadamore@1215 2068 currentResolutionContext = prevResolutionContext;
duke@1 2069 }
duke@1 2070 }
duke@1 2071
mcimadamore@537 2072 /** Resolve constructor using diamond inference.
mcimadamore@537 2073 * @param pos The position to use for error reporting.
mcimadamore@537 2074 * @param env The environment current at the constructor invocation.
mcimadamore@537 2075 * @param site The type of class for which a constructor is searched.
mcimadamore@537 2076 * The scope of this class has been touched in attribution.
mcimadamore@537 2077 * @param argtypes The types of the constructor invocation's value
mcimadamore@537 2078 * arguments.
mcimadamore@537 2079 * @param typeargtypes The types of the constructor invocation's type
mcimadamore@537 2080 * arguments.
mcimadamore@537 2081 */
mcimadamore@537 2082 Symbol resolveDiamond(DiagnosticPosition pos,
mcimadamore@537 2083 Env<AttrContext> env,
mcimadamore@537 2084 Type site,
mcimadamore@537 2085 List<Type> argtypes,
mcimadamore@631 2086 List<Type> typeargtypes) {
mcimadamore@1215 2087 MethodResolutionContext prevResolutionContext = currentResolutionContext;
mcimadamore@1215 2088 try {
mcimadamore@1215 2089 currentResolutionContext = new MethodResolutionContext();
mcimadamore@1215 2090 Symbol sym = methodNotFound;
mcimadamore@1215 2091 List<MethodResolutionPhase> steps = methodResolutionSteps;
mcimadamore@1215 2092 while (steps.nonEmpty() &&
mcimadamore@1215 2093 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@1215 2094 sym.kind >= ERRONEOUS) {
mcimadamore@1347 2095 currentResolutionContext.step = env.info.pendingResolutionPhase = steps.head;
mcimadamore@1217 2096 sym = findDiamond(env, site, argtypes, typeargtypes,
mcimadamore@1215 2097 steps.head.isBoxingRequired(),
mcimadamore@1347 2098 steps.head.isVarargsRequired());
mcimadamore@1215 2099 currentResolutionContext.resolutionCache.put(steps.head, sym);
mcimadamore@1215 2100 steps = steps.tail;
mcimadamore@1215 2101 }
mcimadamore@1215 2102 if (sym.kind >= AMBIGUOUS) {
mcimadamore@1341 2103 Symbol errSym =
mcimadamore@1341 2104 currentResolutionContext.resolutionCache.get(currentResolutionContext.firstErroneousResolutionPhase());
mcimadamore@1341 2105 final JCDiagnostic details = errSym.kind == WRONG_MTH ?
mcimadamore@1341 2106 ((InapplicableSymbolError)errSym).errCandidate().details :
mcimadamore@1215 2107 null;
mcimadamore@1341 2108 errSym = new InapplicableSymbolError(errSym.kind, "diamondError") {
mcimadamore@1215 2109 @Override
mcimadamore@1215 2110 JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos,
mcimadamore@1215 2111 Symbol location, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) {
mcimadamore@1215 2112 String key = details == null ?
mcimadamore@1215 2113 "cant.apply.diamond" :
mcimadamore@1215 2114 "cant.apply.diamond.1";
mcimadamore@1215 2115 return diags.create(dkind, log.currentSource(), pos, key,
mcimadamore@1215 2116 diags.fragment("diamond", site.tsym), details);
mcimadamore@1215 2117 }
mcimadamore@1215 2118 };
mcimadamore@1215 2119 MethodResolutionPhase errPhase = currentResolutionContext.firstErroneousResolutionPhase();
mcimadamore@1347 2120 sym = accessMethod(errSym, pos, site, names.init, true, argtypes, typeargtypes);
mcimadamore@1347 2121 env.info.pendingResolutionPhase = errPhase;
mcimadamore@1215 2122 }
mcimadamore@1215 2123 return sym;
mcimadamore@537 2124 }
mcimadamore@1215 2125 finally {
mcimadamore@1215 2126 currentResolutionContext = prevResolutionContext;
mcimadamore@537 2127 }
mcimadamore@537 2128 }
mcimadamore@537 2129
mcimadamore@1217 2130 /** This method scans all the constructor symbol in a given class scope -
mcimadamore@1217 2131 * assuming that the original scope contains a constructor of the kind:
jjg@1326 2132 * {@code Foo(X x, Y y)}, where X,Y are class type-variables declared in Foo,
mcimadamore@1217 2133 * a method check is executed against the modified constructor type:
jjg@1326 2134 * {@code <X,Y>Foo<X,Y>(X x, Y y)}. This is crucial in order to enable diamond
mcimadamore@1217 2135 * inference. The inferred return type of the synthetic constructor IS
mcimadamore@1217 2136 * the inferred type for the diamond operator.
mcimadamore@1217 2137 */
mcimadamore@1217 2138 private Symbol findDiamond(Env<AttrContext> env,
mcimadamore@1217 2139 Type site,
mcimadamore@1217 2140 List<Type> argtypes,
mcimadamore@1217 2141 List<Type> typeargtypes,
mcimadamore@1217 2142 boolean allowBoxing,
mcimadamore@1217 2143 boolean useVarargs) {
mcimadamore@1217 2144 Symbol bestSoFar = methodNotFound;
mcimadamore@1217 2145 for (Scope.Entry e = site.tsym.members().lookup(names.init);
mcimadamore@1217 2146 e.scope != null;
mcimadamore@1217 2147 e = e.next()) {
mcimadamore@1341 2148 final Symbol sym = e.sym;
mcimadamore@1217 2149 //- System.out.println(" e " + e.sym);
mcimadamore@1341 2150 if (sym.kind == MTH &&
mcimadamore@1341 2151 (sym.flags_field & SYNTHETIC) == 0) {
mcimadamore@1217 2152 List<Type> oldParams = e.sym.type.tag == FORALL ?
mcimadamore@1341 2153 ((ForAll)sym.type).tvars :
mcimadamore@1217 2154 List.<Type>nil();
mcimadamore@1217 2155 Type constrType = new ForAll(site.tsym.type.getTypeArguments().appendList(oldParams),
mcimadamore@1341 2156 types.createMethodTypeWithReturn(sym.type.asMethodType(), site));
mcimadamore@1341 2157 MethodSymbol newConstr = new MethodSymbol(sym.flags(), names.init, constrType, site.tsym) {
mcimadamore@1341 2158 @Override
mcimadamore@1341 2159 public Symbol baseSymbol() {
mcimadamore@1341 2160 return sym;
mcimadamore@1341 2161 }
mcimadamore@1341 2162 };
mcimadamore@1217 2163 bestSoFar = selectBest(env, site, argtypes, typeargtypes,
mcimadamore@1341 2164 newConstr,
mcimadamore@1217 2165 bestSoFar,
mcimadamore@1217 2166 allowBoxing,
mcimadamore@1217 2167 useVarargs,
mcimadamore@1217 2168 false);
mcimadamore@1217 2169 }
mcimadamore@1217 2170 }
mcimadamore@1217 2171 return bestSoFar;
mcimadamore@1217 2172 }
mcimadamore@1217 2173
duke@1 2174 /** Resolve constructor.
duke@1 2175 * @param pos The position to use for error reporting.
duke@1 2176 * @param env The environment current at the constructor invocation.
duke@1 2177 * @param site The type of class for which a constructor is searched.
duke@1 2178 * @param argtypes The types of the constructor invocation's value
duke@1 2179 * arguments.
duke@1 2180 * @param typeargtypes The types of the constructor invocation's type
duke@1 2181 * arguments.
duke@1 2182 * @param allowBoxing Allow boxing and varargs conversions.
duke@1 2183 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 2184 */
duke@1 2185 Symbol resolveConstructor(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 2186 Type site, List<Type> argtypes,
duke@1 2187 List<Type> typeargtypes,
duke@1 2188 boolean allowBoxing,
duke@1 2189 boolean useVarargs) {
mcimadamore@1215 2190 MethodResolutionContext prevResolutionContext = currentResolutionContext;
mcimadamore@1215 2191 try {
mcimadamore@1215 2192 currentResolutionContext = new MethodResolutionContext();
mcimadamore@1215 2193 return findConstructor(pos, env, site, argtypes, typeargtypes, allowBoxing, useVarargs);
mcimadamore@1215 2194 }
mcimadamore@1215 2195 finally {
mcimadamore@1215 2196 currentResolutionContext = prevResolutionContext;
mcimadamore@1215 2197 }
mcimadamore@1215 2198 }
mcimadamore@1215 2199
mcimadamore@1215 2200 Symbol findConstructor(DiagnosticPosition pos, Env<AttrContext> env,
mcimadamore@1215 2201 Type site, List<Type> argtypes,
mcimadamore@1215 2202 List<Type> typeargtypes,
mcimadamore@1215 2203 boolean allowBoxing,
mcimadamore@1215 2204 boolean useVarargs) {
duke@1 2205 Symbol sym = findMethod(env, site,
mcimadamore@1215 2206 names.init, argtypes,
mcimadamore@1215 2207 typeargtypes, allowBoxing,
mcimadamore@1215 2208 useVarargs, false);
mcimadamore@852 2209 chk.checkDeprecated(pos, env.info.scope.owner, sym);
duke@1 2210 return sym;
duke@1 2211 }
duke@1 2212
duke@1 2213 /** Resolve a constructor, throw a fatal error if not found.
duke@1 2214 * @param pos The position to use for error reporting.
duke@1 2215 * @param env The environment current at the method invocation.
duke@1 2216 * @param site The type to be constructed.
duke@1 2217 * @param argtypes The types of the invocation's value arguments.
duke@1 2218 * @param typeargtypes The types of the invocation's type arguments.
duke@1 2219 */
duke@1 2220 public MethodSymbol resolveInternalConstructor(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 2221 Type site,
duke@1 2222 List<Type> argtypes,
duke@1 2223 List<Type> typeargtypes) {
mcimadamore@1215 2224 MethodResolutionContext resolveContext = new MethodResolutionContext();
mcimadamore@1215 2225 resolveContext.internalResolution = true;
mcimadamore@1215 2226 Symbol sym = resolveConstructor(resolveContext, pos, env, site, argtypes, typeargtypes);
duke@1 2227 if (sym.kind == MTH) return (MethodSymbol)sym;
duke@1 2228 else throw new FatalError(
mcimadamore@89 2229 diags.fragment("fatal.err.cant.locate.ctor", site));
duke@1 2230 }
duke@1 2231
duke@1 2232 /** Resolve operator.
duke@1 2233 * @param pos The position to use for error reporting.
duke@1 2234 * @param optag The tag of the operation tree.
duke@1 2235 * @param env The environment current at the operation.
duke@1 2236 * @param argtypes The types of the operands.
duke@1 2237 */
jjg@1127 2238 Symbol resolveOperator(DiagnosticPosition pos, JCTree.Tag optag,
duke@1 2239 Env<AttrContext> env, List<Type> argtypes) {
mcimadamore@1215 2240 MethodResolutionContext prevResolutionContext = currentResolutionContext;
mcimadamore@1215 2241 try {
mcimadamore@1215 2242 currentResolutionContext = new MethodResolutionContext();
mcimadamore@1215 2243 Name name = treeinfo.operatorName(optag);
mcimadamore@1215 2244 Symbol sym = findMethod(env, syms.predefClass.type, name, argtypes,
mcimadamore@1215 2245 null, false, false, true);
mcimadamore@1215 2246 if (boxingEnabled && sym.kind >= WRONG_MTHS)
mcimadamore@1215 2247 sym = findMethod(env, syms.predefClass.type, name, argtypes,
mcimadamore@1215 2248 null, true, false, true);
mcimadamore@1347 2249 return accessMethod(sym, pos, env.enclClass.sym.type, name,
mcimadamore@1215 2250 false, argtypes, null);
mcimadamore@1215 2251 }
mcimadamore@1215 2252 finally {
mcimadamore@1215 2253 currentResolutionContext = prevResolutionContext;
mcimadamore@1215 2254 }
duke@1 2255 }
duke@1 2256
duke@1 2257 /** Resolve operator.
duke@1 2258 * @param pos The position to use for error reporting.
duke@1 2259 * @param optag The tag of the operation tree.
duke@1 2260 * @param env The environment current at the operation.
duke@1 2261 * @param arg The type of the operand.
duke@1 2262 */
jjg@1127 2263 Symbol resolveUnaryOperator(DiagnosticPosition pos, JCTree.Tag optag, Env<AttrContext> env, Type arg) {
duke@1 2264 return resolveOperator(pos, optag, env, List.of(arg));
duke@1 2265 }
duke@1 2266
duke@1 2267 /** Resolve binary operator.
duke@1 2268 * @param pos The position to use for error reporting.
duke@1 2269 * @param optag The tag of the operation tree.
duke@1 2270 * @param env The environment current at the operation.
duke@1 2271 * @param left The types of the left operand.
duke@1 2272 * @param right The types of the right operand.
duke@1 2273 */
duke@1 2274 Symbol resolveBinaryOperator(DiagnosticPosition pos,
jjg@1127 2275 JCTree.Tag optag,
duke@1 2276 Env<AttrContext> env,
duke@1 2277 Type left,
duke@1 2278 Type right) {
duke@1 2279 return resolveOperator(pos, optag, env, List.of(left, right));
duke@1 2280 }
duke@1 2281
duke@1 2282 /**
duke@1 2283 * Resolve `c.name' where name == this or name == super.
duke@1 2284 * @param pos The position to use for error reporting.
duke@1 2285 * @param env The environment current at the expression.
duke@1 2286 * @param c The qualifier.
duke@1 2287 * @param name The identifier's name.
duke@1 2288 */
duke@1 2289 Symbol resolveSelf(DiagnosticPosition pos,
duke@1 2290 Env<AttrContext> env,
duke@1 2291 TypeSymbol c,
duke@1 2292 Name name) {
duke@1 2293 Env<AttrContext> env1 = env;
duke@1 2294 boolean staticOnly = false;
duke@1 2295 while (env1.outer != null) {
duke@1 2296 if (isStatic(env1)) staticOnly = true;
duke@1 2297 if (env1.enclClass.sym == c) {
duke@1 2298 Symbol sym = env1.info.scope.lookup(name).sym;
duke@1 2299 if (sym != null) {
duke@1 2300 if (staticOnly) sym = new StaticError(sym);
mcimadamore@1347 2301 return accessBase(sym, pos, env.enclClass.sym.type,
duke@1 2302 name, true);
duke@1 2303 }
duke@1 2304 }
duke@1 2305 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
duke@1 2306 env1 = env1.outer;
duke@1 2307 }
duke@1 2308 log.error(pos, "not.encl.class", c);
duke@1 2309 return syms.errSymbol;
duke@1 2310 }
duke@1 2311
duke@1 2312 /**
duke@1 2313 * Resolve `c.this' for an enclosing class c that contains the
duke@1 2314 * named member.
duke@1 2315 * @param pos The position to use for error reporting.
duke@1 2316 * @param env The environment current at the expression.
duke@1 2317 * @param member The member that must be contained in the result.
duke@1 2318 */
duke@1 2319 Symbol resolveSelfContaining(DiagnosticPosition pos,
duke@1 2320 Env<AttrContext> env,
mcimadamore@901 2321 Symbol member,
mcimadamore@901 2322 boolean isSuperCall) {
duke@1 2323 Name name = names._this;
mcimadamore@901 2324 Env<AttrContext> env1 = isSuperCall ? env.outer : env;
duke@1 2325 boolean staticOnly = false;
mcimadamore@901 2326 if (env1 != null) {
mcimadamore@901 2327 while (env1 != null && env1.outer != null) {
mcimadamore@901 2328 if (isStatic(env1)) staticOnly = true;
mcimadamore@901 2329 if (env1.enclClass.sym.isSubClass(member.owner, types)) {
mcimadamore@901 2330 Symbol sym = env1.info.scope.lookup(name).sym;
mcimadamore@901 2331 if (sym != null) {
mcimadamore@901 2332 if (staticOnly) sym = new StaticError(sym);
mcimadamore@1347 2333 return accessBase(sym, pos, env.enclClass.sym.type,
mcimadamore@901 2334 name, true);
mcimadamore@901 2335 }
duke@1 2336 }
mcimadamore@901 2337 if ((env1.enclClass.sym.flags() & STATIC) != 0)
mcimadamore@901 2338 staticOnly = true;
mcimadamore@901 2339 env1 = env1.outer;
duke@1 2340 }
duke@1 2341 }
duke@1 2342 log.error(pos, "encl.class.required", member);
duke@1 2343 return syms.errSymbol;
duke@1 2344 }
duke@1 2345
duke@1 2346 /**
duke@1 2347 * Resolve an appropriate implicit this instance for t's container.
jjh@972 2348 * JLS 8.8.5.1 and 15.9.2
duke@1 2349 */
duke@1 2350 Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t) {
mcimadamore@901 2351 return resolveImplicitThis(pos, env, t, false);
mcimadamore@901 2352 }
mcimadamore@901 2353
mcimadamore@901 2354 Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t, boolean isSuperCall) {
duke@1 2355 Type thisType = (((t.tsym.owner.kind & (MTH|VAR)) != 0)
duke@1 2356 ? resolveSelf(pos, env, t.getEnclosingType().tsym, names._this)
mcimadamore@901 2357 : resolveSelfContaining(pos, env, t.tsym, isSuperCall)).type;
duke@1 2358 if (env.info.isSelfCall && thisType.tsym == env.enclClass.sym)
duke@1 2359 log.error(pos, "cant.ref.before.ctor.called", "this");
duke@1 2360 return thisType;
duke@1 2361 }
duke@1 2362
duke@1 2363 /* ***************************************************************************
duke@1 2364 * ResolveError classes, indicating error situations when accessing symbols
duke@1 2365 ****************************************************************************/
duke@1 2366
mcimadamore@1221 2367 //used by TransTypes when checking target type of synthetic cast
mcimadamore@1221 2368 public void logAccessErrorInternal(Env<AttrContext> env, JCTree tree, Type type) {
mcimadamore@1221 2369 AccessError error = new AccessError(env, env.enclClass.type, type.tsym);
mcimadamore@1221 2370 logResolveError(error, tree.pos(), env.enclClass.sym, env.enclClass.type, null, null, null);
mcimadamore@302 2371 }
mcimadamore@302 2372 //where
mcimadamore@302 2373 private void logResolveError(ResolveError error,
mcimadamore@302 2374 DiagnosticPosition pos,
mcimadamore@829 2375 Symbol location,
mcimadamore@302 2376 Type site,
mcimadamore@302 2377 Name name,
mcimadamore@302 2378 List<Type> argtypes,
mcimadamore@302 2379 List<Type> typeargtypes) {
mcimadamore@302 2380 JCDiagnostic d = error.getDiagnostic(JCDiagnostic.DiagnosticType.ERROR,
mcimadamore@829 2381 pos, location, site, name, argtypes, typeargtypes);
jjg@643 2382 if (d != null) {
jjg@643 2383 d.setFlag(DiagnosticFlag.RESOLVE_ERROR);
mcimadamore@302 2384 log.report(d);
jjg@643 2385 }
duke@1 2386 }
duke@1 2387
mcimadamore@161 2388 private final LocalizedString noArgs = new LocalizedString("compiler.misc.no.args");
mcimadamore@161 2389
mcimadamore@161 2390 public Object methodArguments(List<Type> argtypes) {
mcimadamore@1114 2391 return argtypes == null || argtypes.isEmpty() ? noArgs : argtypes;
mcimadamore@161 2392 }
mcimadamore@161 2393
mcimadamore@302 2394 /**
mcimadamore@302 2395 * Root class for resolution errors. Subclass of ResolveError
mcimadamore@302 2396 * represent a different kinds of resolution error - as such they must
mcimadamore@302 2397 * specify how they map into concrete compiler diagnostics.
duke@1 2398 */
mcimadamore@302 2399 private abstract class ResolveError extends Symbol {
duke@1 2400
mcimadamore@302 2401 /** The name of the kind of error, for debugging only. */
mcimadamore@302 2402 final String debugName;
mcimadamore@302 2403
mcimadamore@302 2404 ResolveError(int kind, String debugName) {
duke@1 2405 super(kind, 0, null, null, null);
duke@1 2406 this.debugName = debugName;
duke@1 2407 }
duke@1 2408
mcimadamore@302 2409 @Override
duke@1 2410 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
duke@1 2411 throw new AssertionError();
duke@1 2412 }
duke@1 2413
mcimadamore@302 2414 @Override
duke@1 2415 public String toString() {
mcimadamore@302 2416 return debugName;
duke@1 2417 }
duke@1 2418
mcimadamore@302 2419 @Override
mcimadamore@302 2420 public boolean exists() {
mcimadamore@302 2421 return false;
duke@1 2422 }
duke@1 2423
mcimadamore@302 2424 /**
mcimadamore@302 2425 * Create an external representation for this erroneous symbol to be
mcimadamore@302 2426 * used during attribution - by default this returns the symbol of a
mcimadamore@302 2427 * brand new error type which stores the original type found
mcimadamore@302 2428 * during resolution.
mcimadamore@302 2429 *
mcimadamore@302 2430 * @param name the name used during resolution
mcimadamore@302 2431 * @param location the location from which the symbol is accessed
duke@1 2432 */
mcimadamore@302 2433 protected Symbol access(Name name, TypeSymbol location) {
mcimadamore@302 2434 return types.createErrorType(name, location, syms.errSymbol.type).tsym;
duke@1 2435 }
duke@1 2436
mcimadamore@302 2437 /**
mcimadamore@302 2438 * Create a diagnostic representing this resolution error.
mcimadamore@302 2439 *
mcimadamore@302 2440 * @param dkind The kind of the diagnostic to be created (e.g error).
mcimadamore@302 2441 * @param pos The position to be used for error reporting.
mcimadamore@302 2442 * @param site The original type from where the selection took place.
mcimadamore@302 2443 * @param name The name of the symbol to be resolved.
mcimadamore@302 2444 * @param argtypes The invocation's value arguments,
mcimadamore@302 2445 * if we looked for a method.
mcimadamore@302 2446 * @param typeargtypes The invocation's type arguments,
mcimadamore@302 2447 * if we looked for a method.
mcimadamore@302 2448 */
mcimadamore@302 2449 abstract JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 2450 DiagnosticPosition pos,
mcimadamore@829 2451 Symbol location,
mcimadamore@302 2452 Type site,
mcimadamore@302 2453 Name name,
mcimadamore@302 2454 List<Type> argtypes,
mcimadamore@302 2455 List<Type> typeargtypes);
duke@1 2456 }
duke@1 2457
mcimadamore@302 2458 /**
mcimadamore@302 2459 * This class is the root class of all resolution errors caused by
mcimadamore@302 2460 * an invalid symbol being found during resolution.
duke@1 2461 */
mcimadamore@302 2462 abstract class InvalidSymbolError extends ResolveError {
mcimadamore@302 2463
mcimadamore@302 2464 /** The invalid symbol found during resolution */
mcimadamore@302 2465 Symbol sym;
mcimadamore@302 2466
mcimadamore@302 2467 InvalidSymbolError(int kind, Symbol sym, String debugName) {
mcimadamore@302 2468 super(kind, debugName);
mcimadamore@302 2469 this.sym = sym;
mcimadamore@302 2470 }
mcimadamore@302 2471
mcimadamore@302 2472 @Override
mcimadamore@302 2473 public boolean exists() {
mcimadamore@302 2474 return true;
mcimadamore@302 2475 }
mcimadamore@302 2476
mcimadamore@302 2477 @Override
mcimadamore@302 2478 public String toString() {
mcimadamore@302 2479 return super.toString() + " wrongSym=" + sym;
mcimadamore@302 2480 }
mcimadamore@302 2481
mcimadamore@302 2482 @Override
mcimadamore@302 2483 public Symbol access(Name name, TypeSymbol location) {
mcimadamore@302 2484 if (sym.kind >= AMBIGUOUS)
mcimadamore@302 2485 return ((ResolveError)sym).access(name, location);
mcimadamore@302 2486 else if ((sym.kind & ERRONEOUS) == 0 && (sym.kind & TYP) != 0)
mcimadamore@302 2487 return types.createErrorType(name, location, sym.type).tsym;
mcimadamore@302 2488 else
mcimadamore@302 2489 return sym;
mcimadamore@302 2490 }
mcimadamore@302 2491 }
mcimadamore@302 2492
mcimadamore@302 2493 /**
mcimadamore@302 2494 * InvalidSymbolError error class indicating that a symbol matching a
mcimadamore@302 2495 * given name does not exists in a given site.
mcimadamore@302 2496 */
mcimadamore@302 2497 class SymbolNotFoundError extends ResolveError {
mcimadamore@302 2498
mcimadamore@302 2499 SymbolNotFoundError(int kind) {
mcimadamore@302 2500 super(kind, "symbol not found error");
mcimadamore@302 2501 }
mcimadamore@302 2502
mcimadamore@302 2503 @Override
mcimadamore@302 2504 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 2505 DiagnosticPosition pos,
mcimadamore@829 2506 Symbol location,
mcimadamore@302 2507 Type site,
mcimadamore@302 2508 Name name,
mcimadamore@302 2509 List<Type> argtypes,
mcimadamore@302 2510 List<Type> typeargtypes) {
mcimadamore@302 2511 argtypes = argtypes == null ? List.<Type>nil() : argtypes;
mcimadamore@302 2512 typeargtypes = typeargtypes == null ? List.<Type>nil() : typeargtypes;
mcimadamore@302 2513 if (name == names.error)
mcimadamore@302 2514 return null;
mcimadamore@302 2515
mcimadamore@1347 2516 if (syms.operatorNames.contains(name)) {
mcimadamore@829 2517 boolean isUnaryOp = argtypes.size() == 1;
mcimadamore@829 2518 String key = argtypes.size() == 1 ?
mcimadamore@829 2519 "operator.cant.be.applied" :
mcimadamore@829 2520 "operator.cant.be.applied.1";
mcimadamore@829 2521 Type first = argtypes.head;
mcimadamore@829 2522 Type second = !isUnaryOp ? argtypes.tail.head : null;
jjg@612 2523 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@829 2524 key, name, first, second);
mcimadamore@302 2525 }
mcimadamore@302 2526 boolean hasLocation = false;
mcimadamore@855 2527 if (location == null) {
mcimadamore@855 2528 location = site.tsym;
mcimadamore@855 2529 }
mcimadamore@829 2530 if (!location.name.isEmpty()) {
mcimadamore@829 2531 if (location.kind == PCK && !site.tsym.exists()) {
jjg@612 2532 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@829 2533 "doesnt.exist", location);
mcimadamore@302 2534 }
mcimadamore@829 2535 hasLocation = !location.name.equals(names._this) &&
mcimadamore@829 2536 !location.name.equals(names._super);
mcimadamore@302 2537 }
mcimadamore@1347 2538 boolean isConstructor = kind == ABSENT_MTH && name == names.init;
mcimadamore@302 2539 KindName kindname = isConstructor ? KindName.CONSTRUCTOR : absentKind(kind);
mcimadamore@302 2540 Name idname = isConstructor ? site.tsym.name : name;
mcimadamore@302 2541 String errKey = getErrorKey(kindname, typeargtypes.nonEmpty(), hasLocation);
mcimadamore@302 2542 if (hasLocation) {
jjg@612 2543 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@302 2544 errKey, kindname, idname, //symbol kindname, name
mcimadamore@302 2545 typeargtypes, argtypes, //type parameters and arguments (if any)
mcimadamore@855 2546 getLocationDiag(location, site)); //location kindname, type
mcimadamore@302 2547 }
mcimadamore@302 2548 else {
jjg@612 2549 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@302 2550 errKey, kindname, idname, //symbol kindname, name
mcimadamore@302 2551 typeargtypes, argtypes); //type parameters and arguments (if any)
mcimadamore@302 2552 }
mcimadamore@302 2553 }
mcimadamore@302 2554 //where
mcimadamore@302 2555 private String getErrorKey(KindName kindname, boolean hasTypeArgs, boolean hasLocation) {
mcimadamore@302 2556 String key = "cant.resolve";
mcimadamore@302 2557 String suffix = hasLocation ? ".location" : "";
mcimadamore@302 2558 switch (kindname) {
mcimadamore@302 2559 case METHOD:
mcimadamore@302 2560 case CONSTRUCTOR: {
mcimadamore@302 2561 suffix += ".args";
mcimadamore@302 2562 suffix += hasTypeArgs ? ".params" : "";
mcimadamore@302 2563 }
mcimadamore@302 2564 }
mcimadamore@302 2565 return key + suffix;
mcimadamore@302 2566 }
mcimadamore@855 2567 private JCDiagnostic getLocationDiag(Symbol location, Type site) {
mcimadamore@855 2568 if (location.kind == VAR) {
mcimadamore@855 2569 return diags.fragment("location.1",
mcimadamore@829 2570 kindName(location),
mcimadamore@829 2571 location,
mcimadamore@855 2572 location.type);
mcimadamore@855 2573 } else {
mcimadamore@855 2574 return diags.fragment("location",
mcimadamore@855 2575 typeKindName(site),
mcimadamore@855 2576 site,
mcimadamore@855 2577 null);
mcimadamore@855 2578 }
mcimadamore@829 2579 }
mcimadamore@302 2580 }
mcimadamore@302 2581
mcimadamore@302 2582 /**
mcimadamore@302 2583 * InvalidSymbolError error class indicating that a given symbol
mcimadamore@302 2584 * (either a method, a constructor or an operand) is not applicable
mcimadamore@302 2585 * given an actual arguments/type argument list.
mcimadamore@302 2586 */
mcimadamore@1215 2587 class InapplicableSymbolError extends ResolveError {
mcimadamore@302 2588
mcimadamore@1215 2589 InapplicableSymbolError() {
mcimadamore@1215 2590 super(WRONG_MTH, "inapplicable symbol error");
mcimadamore@302 2591 }
mcimadamore@302 2592
mcimadamore@1215 2593 protected InapplicableSymbolError(int kind, String debugName) {
mcimadamore@1215 2594 super(kind, debugName);
mcimadamore@302 2595 }
mcimadamore@302 2596
mcimadamore@302 2597 @Override
mcimadamore@302 2598 public String toString() {
mcimadamore@1215 2599 return super.toString();
mcimadamore@1215 2600 }
mcimadamore@1215 2601
mcimadamore@1215 2602 @Override
mcimadamore@1215 2603 public boolean exists() {
mcimadamore@1215 2604 return true;
mcimadamore@302 2605 }
mcimadamore@302 2606
mcimadamore@302 2607 @Override
mcimadamore@302 2608 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 2609 DiagnosticPosition pos,
mcimadamore@829 2610 Symbol location,
mcimadamore@302 2611 Type site,
mcimadamore@302 2612 Name name,
mcimadamore@302 2613 List<Type> argtypes,
mcimadamore@302 2614 List<Type> typeargtypes) {
mcimadamore@302 2615 if (name == names.error)
mcimadamore@302 2616 return null;
mcimadamore@302 2617
mcimadamore@1347 2618 if (syms.operatorNames.contains(name)) {
mcimadamore@853 2619 boolean isUnaryOp = argtypes.size() == 1;
mcimadamore@853 2620 String key = argtypes.size() == 1 ?
mcimadamore@853 2621 "operator.cant.be.applied" :
mcimadamore@853 2622 "operator.cant.be.applied.1";
mcimadamore@853 2623 Type first = argtypes.head;
mcimadamore@853 2624 Type second = !isUnaryOp ? argtypes.tail.head : null;
mcimadamore@853 2625 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@853 2626 key, name, first, second);
mcimadamore@302 2627 }
mcimadamore@302 2628 else {
mcimadamore@1215 2629 Candidate c = errCandidate();
mcimadamore@1215 2630 Symbol ws = c.sym.asMemberOf(site, types);
jjg@612 2631 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@1215 2632 "cant.apply.symbol" + (c.details != null ? ".1" : ""),
mcimadamore@302 2633 kindName(ws),
mcimadamore@302 2634 ws.name == names.init ? ws.owner.name : ws.name,
mcimadamore@302 2635 methodArguments(ws.type.getParameterTypes()),
mcimadamore@302 2636 methodArguments(argtypes),
mcimadamore@302 2637 kindName(ws.owner),
mcimadamore@302 2638 ws.owner.type,
mcimadamore@1215 2639 c.details);
mcimadamore@302 2640 }
mcimadamore@302 2641 }
mcimadamore@302 2642
mcimadamore@302 2643 @Override
mcimadamore@302 2644 public Symbol access(Name name, TypeSymbol location) {
mcimadamore@302 2645 return types.createErrorType(name, location, syms.errSymbol.type).tsym;
mcimadamore@302 2646 }
mcimadamore@1215 2647
mcimadamore@1215 2648 protected boolean shouldReport(Candidate c) {
mcimadamore@1215 2649 return !c.isApplicable() &&
mcimadamore@1215 2650 (((c.sym.flags() & VARARGS) != 0 && c.step == VARARITY) ||
mcimadamore@1215 2651 (c.sym.flags() & VARARGS) == 0 && c.step == (boxingEnabled ? BOX : BASIC));
mcimadamore@1215 2652 }
mcimadamore@1215 2653
mcimadamore@1215 2654 private Candidate errCandidate() {
mcimadamore@1215 2655 for (Candidate c : currentResolutionContext.candidates) {
mcimadamore@1215 2656 if (shouldReport(c)) {
mcimadamore@1215 2657 return c;
mcimadamore@1215 2658 }
mcimadamore@1215 2659 }
mcimadamore@1215 2660 Assert.error();
mcimadamore@1215 2661 return null;
mcimadamore@1215 2662 }
mcimadamore@302 2663 }
mcimadamore@302 2664
mcimadamore@302 2665 /**
mcimadamore@302 2666 * ResolveError error class indicating that a set of symbols
mcimadamore@302 2667 * (either methods, constructors or operands) is not applicable
mcimadamore@302 2668 * given an actual arguments/type argument list.
mcimadamore@302 2669 */
mcimadamore@1215 2670 class InapplicableSymbolsError extends InapplicableSymbolError {
mcimadamore@689 2671
mcimadamore@1215 2672 InapplicableSymbolsError() {
mcimadamore@302 2673 super(WRONG_MTHS, "inapplicable symbols");
mcimadamore@302 2674 }
mcimadamore@302 2675
mcimadamore@302 2676 @Override
mcimadamore@302 2677 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 2678 DiagnosticPosition pos,
mcimadamore@829 2679 Symbol location,
mcimadamore@302 2680 Type site,
mcimadamore@302 2681 Name name,
mcimadamore@302 2682 List<Type> argtypes,
mcimadamore@302 2683 List<Type> typeargtypes) {
mcimadamore@1215 2684 if (currentResolutionContext.candidates.nonEmpty()) {
mcimadamore@689 2685 JCDiagnostic err = diags.create(dkind,
mcimadamore@689 2686 log.currentSource(),
mcimadamore@689 2687 pos,
mcimadamore@689 2688 "cant.apply.symbols",
mcimadamore@689 2689 name == names.init ? KindName.CONSTRUCTOR : absentKind(kind),
mcimadamore@689 2690 getName(),
mcimadamore@689 2691 argtypes);
mcimadamore@689 2692 return new JCDiagnostic.MultilineDiagnostic(err, candidateDetails(site));
mcimadamore@689 2693 } else {
mcimadamore@689 2694 return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind, pos,
mcimadamore@829 2695 location, site, name, argtypes, typeargtypes);
mcimadamore@689 2696 }
mcimadamore@689 2697 }
mcimadamore@689 2698
mcimadamore@689 2699 //where
mcimadamore@689 2700 List<JCDiagnostic> candidateDetails(Type site) {
mcimadamore@689 2701 List<JCDiagnostic> details = List.nil();
mcimadamore@1215 2702 for (Candidate c : currentResolutionContext.candidates) {
mcimadamore@1215 2703 if (!shouldReport(c)) continue;
mcimadamore@1215 2704 JCDiagnostic detailDiag = diags.fragment("inapplicable.method",
mcimadamore@1215 2705 Kinds.kindName(c.sym),
mcimadamore@1215 2706 c.sym.location(site, types),
mcimadamore@1215 2707 c.sym.asMemberOf(site, types),
mcimadamore@1215 2708 c.details);
mcimadamore@1215 2709 details = details.prepend(detailDiag);
mcimadamore@1215 2710 }
mcimadamore@689 2711 return details.reverse();
mcimadamore@689 2712 }
mcimadamore@689 2713
mcimadamore@689 2714 private Name getName() {
mcimadamore@1215 2715 Symbol sym = currentResolutionContext.candidates.head.sym;
mcimadamore@689 2716 return sym.name == names.init ?
mcimadamore@689 2717 sym.owner.name :
mcimadamore@689 2718 sym.name;
mcimadamore@689 2719 }
mcimadamore@302 2720 }
mcimadamore@302 2721
mcimadamore@302 2722 /**
mcimadamore@302 2723 * An InvalidSymbolError error class indicating that a symbol is not
mcimadamore@302 2724 * accessible from a given site
mcimadamore@302 2725 */
mcimadamore@302 2726 class AccessError extends InvalidSymbolError {
mcimadamore@302 2727
mcimadamore@302 2728 private Env<AttrContext> env;
mcimadamore@302 2729 private Type site;
duke@1 2730
duke@1 2731 AccessError(Symbol sym) {
duke@1 2732 this(null, null, sym);
duke@1 2733 }
duke@1 2734
duke@1 2735 AccessError(Env<AttrContext> env, Type site, Symbol sym) {
duke@1 2736 super(HIDDEN, sym, "access error");
duke@1 2737 this.env = env;
duke@1 2738 this.site = site;
duke@1 2739 if (debugResolve)
duke@1 2740 log.error("proc.messager", sym + " @ " + site + " is inaccessible.");
duke@1 2741 }
duke@1 2742
mcimadamore@302 2743 @Override
mcimadamore@302 2744 public boolean exists() {
mcimadamore@302 2745 return false;
mcimadamore@302 2746 }
duke@1 2747
mcimadamore@302 2748 @Override
mcimadamore@302 2749 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 2750 DiagnosticPosition pos,
mcimadamore@829 2751 Symbol location,
mcimadamore@302 2752 Type site,
mcimadamore@302 2753 Name name,
mcimadamore@302 2754 List<Type> argtypes,
mcimadamore@302 2755 List<Type> typeargtypes) {
mcimadamore@302 2756 if (sym.owner.type.tag == ERROR)
mcimadamore@302 2757 return null;
mcimadamore@302 2758
mcimadamore@302 2759 if (sym.name == names.init && sym.owner != site.tsym) {
mcimadamore@302 2760 return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind,
mcimadamore@829 2761 pos, location, site, name, argtypes, typeargtypes);
mcimadamore@302 2762 }
mcimadamore@302 2763 else if ((sym.flags() & PUBLIC) != 0
mcimadamore@302 2764 || (env != null && this.site != null
mcimadamore@302 2765 && !isAccessible(env, this.site))) {
jjg@612 2766 return diags.create(dkind, log.currentSource(),
mcimadamore@302 2767 pos, "not.def.access.class.intf.cant.access",
mcimadamore@302 2768 sym, sym.location());
mcimadamore@302 2769 }
mcimadamore@302 2770 else if ((sym.flags() & (PRIVATE | PROTECTED)) != 0) {
jjg@612 2771 return diags.create(dkind, log.currentSource(),
mcimadamore@302 2772 pos, "report.access", sym,
mcimadamore@302 2773 asFlagSet(sym.flags() & (PRIVATE | PROTECTED)),
mcimadamore@302 2774 sym.location());
mcimadamore@302 2775 }
mcimadamore@302 2776 else {
jjg@612 2777 return diags.create(dkind, log.currentSource(),
mcimadamore@302 2778 pos, "not.def.public.cant.access", sym, sym.location());
duke@1 2779 }
duke@1 2780 }
duke@1 2781 }
duke@1 2782
mcimadamore@302 2783 /**
mcimadamore@302 2784 * InvalidSymbolError error class indicating that an instance member
mcimadamore@302 2785 * has erroneously been accessed from a static context.
duke@1 2786 */
mcimadamore@302 2787 class StaticError extends InvalidSymbolError {
mcimadamore@302 2788
duke@1 2789 StaticError(Symbol sym) {
duke@1 2790 super(STATICERR, sym, "static error");
duke@1 2791 }
duke@1 2792
mcimadamore@302 2793 @Override
mcimadamore@302 2794 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 2795 DiagnosticPosition pos,
mcimadamore@829 2796 Symbol location,
mcimadamore@302 2797 Type site,
mcimadamore@302 2798 Name name,
mcimadamore@302 2799 List<Type> argtypes,
mcimadamore@302 2800 List<Type> typeargtypes) {
mcimadamore@80 2801 Symbol errSym = ((sym.kind == TYP && sym.type.tag == CLASS)
mcimadamore@80 2802 ? types.erasure(sym.type).tsym
mcimadamore@80 2803 : sym);
jjg@612 2804 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@302 2805 "non-static.cant.be.ref", kindName(sym), errSym);
duke@1 2806 }
duke@1 2807 }
duke@1 2808
mcimadamore@302 2809 /**
mcimadamore@302 2810 * InvalidSymbolError error class indicating that a pair of symbols
mcimadamore@302 2811 * (either methods, constructors or operands) are ambiguous
mcimadamore@302 2812 * given an actual arguments/type argument list.
duke@1 2813 */
mcimadamore@302 2814 class AmbiguityError extends InvalidSymbolError {
mcimadamore@302 2815
mcimadamore@302 2816 /** The other maximally specific symbol */
duke@1 2817 Symbol sym2;
duke@1 2818
duke@1 2819 AmbiguityError(Symbol sym1, Symbol sym2) {
duke@1 2820 super(AMBIGUOUS, sym1, "ambiguity error");
duke@1 2821 this.sym2 = sym2;
duke@1 2822 }
duke@1 2823
mcimadamore@302 2824 @Override
mcimadamore@302 2825 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 2826 DiagnosticPosition pos,
mcimadamore@829 2827 Symbol location,
mcimadamore@302 2828 Type site,
mcimadamore@302 2829 Name name,
mcimadamore@302 2830 List<Type> argtypes,
mcimadamore@302 2831 List<Type> typeargtypes) {
duke@1 2832 AmbiguityError pair = this;
duke@1 2833 while (true) {
mcimadamore@302 2834 if (pair.sym.kind == AMBIGUOUS)
mcimadamore@302 2835 pair = (AmbiguityError)pair.sym;
duke@1 2836 else if (pair.sym2.kind == AMBIGUOUS)
duke@1 2837 pair = (AmbiguityError)pair.sym2;
duke@1 2838 else break;
duke@1 2839 }
mcimadamore@302 2840 Name sname = pair.sym.name;
mcimadamore@302 2841 if (sname == names.init) sname = pair.sym.owner.name;
jjg@612 2842 return diags.create(dkind, log.currentSource(),
mcimadamore@302 2843 pos, "ref.ambiguous", sname,
mcimadamore@302 2844 kindName(pair.sym),
mcimadamore@302 2845 pair.sym,
mcimadamore@302 2846 pair.sym.location(site, types),
duke@1 2847 kindName(pair.sym2),
duke@1 2848 pair.sym2,
duke@1 2849 pair.sym2.location(site, types));
duke@1 2850 }
duke@1 2851 }
mcimadamore@160 2852
mcimadamore@160 2853 enum MethodResolutionPhase {
mcimadamore@160 2854 BASIC(false, false),
mcimadamore@160 2855 BOX(true, false),
mcimadamore@160 2856 VARARITY(true, true);
mcimadamore@160 2857
mcimadamore@160 2858 boolean isBoxingRequired;
mcimadamore@160 2859 boolean isVarargsRequired;
mcimadamore@160 2860
mcimadamore@160 2861 MethodResolutionPhase(boolean isBoxingRequired, boolean isVarargsRequired) {
mcimadamore@160 2862 this.isBoxingRequired = isBoxingRequired;
mcimadamore@160 2863 this.isVarargsRequired = isVarargsRequired;
mcimadamore@160 2864 }
mcimadamore@160 2865
mcimadamore@160 2866 public boolean isBoxingRequired() {
mcimadamore@160 2867 return isBoxingRequired;
mcimadamore@160 2868 }
mcimadamore@160 2869
mcimadamore@160 2870 public boolean isVarargsRequired() {
mcimadamore@160 2871 return isVarargsRequired;
mcimadamore@160 2872 }
mcimadamore@160 2873
mcimadamore@160 2874 public boolean isApplicable(boolean boxingEnabled, boolean varargsEnabled) {
mcimadamore@160 2875 return (varargsEnabled || !isVarargsRequired) &&
mcimadamore@160 2876 (boxingEnabled || !isBoxingRequired);
mcimadamore@160 2877 }
mcimadamore@160 2878 }
mcimadamore@160 2879
mcimadamore@160 2880 final List<MethodResolutionPhase> methodResolutionSteps = List.of(BASIC, BOX, VARARITY);
mcimadamore@160 2881
mcimadamore@1215 2882 /**
mcimadamore@1215 2883 * A resolution context is used to keep track of intermediate results of
mcimadamore@1215 2884 * overload resolution, such as list of method that are not applicable
mcimadamore@1215 2885 * (used to generate more precise diagnostics) and so on. Resolution contexts
mcimadamore@1215 2886 * can be nested - this means that when each overload resolution routine should
mcimadamore@1215 2887 * work within the resolution context it created.
mcimadamore@1215 2888 */
mcimadamore@1215 2889 class MethodResolutionContext {
mcimadamore@689 2890
mcimadamore@1215 2891 private List<Candidate> candidates = List.nil();
mcimadamore@1114 2892
mcimadamore@1215 2893 private Map<MethodResolutionPhase, Symbol> resolutionCache =
mcimadamore@1215 2894 new EnumMap<MethodResolutionPhase, Symbol>(MethodResolutionPhase.class);
mcimadamore@1215 2895
mcimadamore@1347 2896 MethodResolutionPhase step = null;
mcimadamore@1215 2897
mcimadamore@1215 2898 private boolean internalResolution = false;
mcimadamore@1347 2899 private DeferredAttr.AttrMode attrMode = DeferredAttr.AttrMode.SPECULATIVE;
mcimadamore@1215 2900
mcimadamore@1215 2901 private MethodResolutionPhase firstErroneousResolutionPhase() {
mcimadamore@1215 2902 MethodResolutionPhase bestSoFar = BASIC;
mcimadamore@1215 2903 Symbol sym = methodNotFound;
mcimadamore@1215 2904 List<MethodResolutionPhase> steps = methodResolutionSteps;
mcimadamore@1215 2905 while (steps.nonEmpty() &&
mcimadamore@1215 2906 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@1215 2907 sym.kind >= WRONG_MTHS) {
mcimadamore@1215 2908 sym = resolutionCache.get(steps.head);
mcimadamore@1215 2909 bestSoFar = steps.head;
mcimadamore@1215 2910 steps = steps.tail;
mcimadamore@1215 2911 }
mcimadamore@1215 2912 return bestSoFar;
mcimadamore@160 2913 }
mcimadamore@1215 2914
mcimadamore@1215 2915 void addInapplicableCandidate(Symbol sym, JCDiagnostic details) {
mcimadamore@1215 2916 Candidate c = new Candidate(currentResolutionContext.step, sym, details, null);
mcimadamore@1215 2917 if (!candidates.contains(c))
mcimadamore@1215 2918 candidates = candidates.append(c);
mcimadamore@1215 2919 }
mcimadamore@1215 2920
mcimadamore@1215 2921 void addApplicableCandidate(Symbol sym, Type mtype) {
mcimadamore@1215 2922 Candidate c = new Candidate(currentResolutionContext.step, sym, null, mtype);
mcimadamore@1215 2923 candidates = candidates.append(c);
mcimadamore@1215 2924 }
mcimadamore@1215 2925
mcimadamore@1215 2926 /**
mcimadamore@1215 2927 * This class represents an overload resolution candidate. There are two
mcimadamore@1215 2928 * kinds of candidates: applicable methods and inapplicable methods;
mcimadamore@1215 2929 * applicable methods have a pointer to the instantiated method type,
mcimadamore@1215 2930 * while inapplicable candidates contain further details about the
mcimadamore@1215 2931 * reason why the method has been considered inapplicable.
mcimadamore@1215 2932 */
mcimadamore@1215 2933 class Candidate {
mcimadamore@1215 2934
mcimadamore@1215 2935 final MethodResolutionPhase step;
mcimadamore@1215 2936 final Symbol sym;
mcimadamore@1215 2937 final JCDiagnostic details;
mcimadamore@1215 2938 final Type mtype;
mcimadamore@1215 2939
mcimadamore@1215 2940 private Candidate(MethodResolutionPhase step, Symbol sym, JCDiagnostic details, Type mtype) {
mcimadamore@1215 2941 this.step = step;
mcimadamore@1215 2942 this.sym = sym;
mcimadamore@1215 2943 this.details = details;
mcimadamore@1215 2944 this.mtype = mtype;
mcimadamore@1215 2945 }
mcimadamore@1215 2946
mcimadamore@1215 2947 @Override
mcimadamore@1215 2948 public boolean equals(Object o) {
mcimadamore@1215 2949 if (o instanceof Candidate) {
mcimadamore@1215 2950 Symbol s1 = this.sym;
mcimadamore@1215 2951 Symbol s2 = ((Candidate)o).sym;
mcimadamore@1215 2952 if ((s1 != s2 &&
mcimadamore@1215 2953 (s1.overrides(s2, s1.owner.type.tsym, types, false) ||
mcimadamore@1215 2954 (s2.overrides(s1, s2.owner.type.tsym, types, false)))) ||
mcimadamore@1215 2955 ((s1.isConstructor() || s2.isConstructor()) && s1.owner != s2.owner))
mcimadamore@1215 2956 return true;
mcimadamore@1215 2957 }
mcimadamore@1215 2958 return false;
mcimadamore@1215 2959 }
mcimadamore@1215 2960
mcimadamore@1215 2961 boolean isApplicable() {
mcimadamore@1215 2962 return mtype != null;
mcimadamore@1215 2963 }
mcimadamore@1215 2964 }
mcimadamore@1347 2965
mcimadamore@1347 2966 DeferredAttr.AttrMode attrMode() {
mcimadamore@1347 2967 return attrMode;
mcimadamore@1347 2968 }
mcimadamore@1347 2969
mcimadamore@1347 2970 boolean internal() {
mcimadamore@1347 2971 return internalResolution;
mcimadamore@1347 2972 }
mcimadamore@160 2973 }
mcimadamore@1215 2974
mcimadamore@1215 2975 MethodResolutionContext currentResolutionContext = null;
duke@1 2976 }

mercurial