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

Thu, 24 May 2018 16:48:51 +0800

author
aoqi
date
Thu, 24 May 2018 16:48:51 +0800
changeset 3295
859dc787b52b
parent 3092
8c3890c90147
parent 2893
ca5783d9a597
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
rpatil@3092 2 * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.javac.comp;
aoqi@0 27
aoqi@0 28 import com.sun.source.tree.MemberReferenceTree.ReferenceMode;
aoqi@0 29 import com.sun.tools.javac.api.Formattable.LocalizedString;
aoqi@0 30 import com.sun.tools.javac.code.*;
aoqi@0 31 import com.sun.tools.javac.code.Symbol.*;
aoqi@0 32 import com.sun.tools.javac.code.Type.*;
aoqi@0 33 import com.sun.tools.javac.comp.Attr.ResultInfo;
aoqi@0 34 import com.sun.tools.javac.comp.Check.CheckContext;
aoqi@0 35 import com.sun.tools.javac.comp.DeferredAttr.AttrMode;
aoqi@0 36 import com.sun.tools.javac.comp.DeferredAttr.DeferredAttrContext;
aoqi@0 37 import com.sun.tools.javac.comp.DeferredAttr.DeferredType;
aoqi@0 38 import com.sun.tools.javac.comp.Infer.InferenceContext;
aoqi@0 39 import com.sun.tools.javac.comp.Infer.FreeTypeListener;
aoqi@0 40 import com.sun.tools.javac.comp.Resolve.MethodResolutionContext.Candidate;
aoqi@0 41 import com.sun.tools.javac.comp.Resolve.MethodResolutionDiagHelper.DiagnosticRewriter;
aoqi@0 42 import com.sun.tools.javac.comp.Resolve.MethodResolutionDiagHelper.Template;
aoqi@0 43 import com.sun.tools.javac.jvm.*;
aoqi@0 44 import com.sun.tools.javac.main.Option;
aoqi@0 45 import com.sun.tools.javac.tree.*;
aoqi@0 46 import com.sun.tools.javac.tree.JCTree.*;
aoqi@0 47 import com.sun.tools.javac.tree.JCTree.JCMemberReference.ReferenceKind;
aoqi@0 48 import com.sun.tools.javac.tree.JCTree.JCPolyExpression.*;
aoqi@0 49 import com.sun.tools.javac.util.*;
aoqi@0 50 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
aoqi@0 51 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
aoqi@0 52 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
aoqi@0 53
aoqi@0 54 import java.util.Arrays;
aoqi@0 55 import java.util.Collection;
aoqi@0 56 import java.util.EnumMap;
aoqi@0 57 import java.util.EnumSet;
aoqi@0 58 import java.util.Iterator;
aoqi@0 59 import java.util.LinkedHashMap;
aoqi@0 60 import java.util.LinkedHashSet;
aoqi@0 61 import java.util.Map;
aoqi@0 62
aoqi@0 63 import javax.lang.model.element.ElementVisitor;
aoqi@0 64
aoqi@0 65 import static com.sun.tools.javac.code.Flags.*;
aoqi@0 66 import static com.sun.tools.javac.code.Flags.BLOCK;
aoqi@0 67 import static com.sun.tools.javac.code.Kinds.*;
aoqi@0 68 import static com.sun.tools.javac.code.Kinds.ERRONEOUS;
aoqi@0 69 import static com.sun.tools.javac.code.TypeTag.*;
aoqi@0 70 import static com.sun.tools.javac.comp.Resolve.MethodResolutionPhase.*;
aoqi@0 71 import static com.sun.tools.javac.tree.JCTree.Tag.*;
aoqi@0 72
aoqi@0 73 /** Helper class for name resolution, used mostly by the attribution phase.
aoqi@0 74 *
aoqi@0 75 * <p><b>This is NOT part of any supported API.
aoqi@0 76 * If you write code that depends on this, you do so at your own risk.
aoqi@0 77 * This code and its internal interfaces are subject to change or
aoqi@0 78 * deletion without notice.</b>
aoqi@0 79 */
aoqi@0 80 public class Resolve {
aoqi@0 81 protected static final Context.Key<Resolve> resolveKey =
aoqi@0 82 new Context.Key<Resolve>();
aoqi@0 83
aoqi@0 84 Names names;
aoqi@0 85 Log log;
aoqi@0 86 Symtab syms;
aoqi@0 87 Attr attr;
aoqi@0 88 DeferredAttr deferredAttr;
aoqi@0 89 Check chk;
aoqi@0 90 Infer infer;
aoqi@0 91 ClassReader reader;
aoqi@0 92 TreeInfo treeinfo;
aoqi@0 93 Types types;
aoqi@0 94 JCDiagnostic.Factory diags;
aoqi@0 95 public final boolean boxingEnabled;
aoqi@0 96 public final boolean varargsEnabled;
aoqi@0 97 public final boolean allowMethodHandles;
aoqi@0 98 public final boolean allowFunctionalInterfaceMostSpecific;
vromero@2535 99 public final boolean checkVarargsAccessAfterResolution;
aoqi@0 100 private final boolean debugResolve;
aoqi@0 101 private final boolean compactMethodDiags;
aoqi@0 102 final EnumSet<VerboseResolutionMode> verboseResolutionMode;
aoqi@0 103
aoqi@0 104 Scope polymorphicSignatureScope;
aoqi@0 105
aoqi@0 106 protected Resolve(Context context) {
aoqi@0 107 context.put(resolveKey, this);
aoqi@0 108 syms = Symtab.instance(context);
aoqi@0 109
aoqi@0 110 varNotFound = new
aoqi@0 111 SymbolNotFoundError(ABSENT_VAR);
aoqi@0 112 methodNotFound = new
aoqi@0 113 SymbolNotFoundError(ABSENT_MTH);
aoqi@0 114 methodWithCorrectStaticnessNotFound = new
aoqi@0 115 SymbolNotFoundError(WRONG_STATICNESS,
aoqi@0 116 "method found has incorrect staticness");
aoqi@0 117 typeNotFound = new
aoqi@0 118 SymbolNotFoundError(ABSENT_TYP);
aoqi@0 119
aoqi@0 120 names = Names.instance(context);
aoqi@0 121 log = Log.instance(context);
aoqi@0 122 attr = Attr.instance(context);
aoqi@0 123 deferredAttr = DeferredAttr.instance(context);
aoqi@0 124 chk = Check.instance(context);
aoqi@0 125 infer = Infer.instance(context);
aoqi@0 126 reader = ClassReader.instance(context);
aoqi@0 127 treeinfo = TreeInfo.instance(context);
aoqi@0 128 types = Types.instance(context);
aoqi@0 129 diags = JCDiagnostic.Factory.instance(context);
aoqi@0 130 Source source = Source.instance(context);
aoqi@0 131 boxingEnabled = source.allowBoxing();
aoqi@0 132 varargsEnabled = source.allowVarargs();
aoqi@0 133 Options options = Options.instance(context);
aoqi@0 134 debugResolve = options.isSet("debugresolve");
aoqi@0 135 compactMethodDiags = options.isSet(Option.XDIAGS, "compact") ||
aoqi@0 136 options.isUnset(Option.XDIAGS) && options.isUnset("rawDiagnostics");
aoqi@0 137 verboseResolutionMode = VerboseResolutionMode.getVerboseResolutionMode(options);
aoqi@0 138 Target target = Target.instance(context);
aoqi@0 139 allowMethodHandles = target.hasMethodHandles();
aoqi@0 140 allowFunctionalInterfaceMostSpecific = source.allowFunctionalInterfaceMostSpecific();
vromero@2535 141 checkVarargsAccessAfterResolution =
vromero@2531 142 source.allowPostApplicabilityVarargsAccessCheck();
aoqi@0 143 polymorphicSignatureScope = new Scope(syms.noSymbol);
aoqi@0 144
aoqi@0 145 inapplicableMethodException = new InapplicableMethodException(diags);
aoqi@0 146 }
aoqi@0 147
aoqi@0 148 /** error symbols, which are returned when resolution fails
aoqi@0 149 */
aoqi@0 150 private final SymbolNotFoundError varNotFound;
aoqi@0 151 private final SymbolNotFoundError methodNotFound;
aoqi@0 152 private final SymbolNotFoundError methodWithCorrectStaticnessNotFound;
aoqi@0 153 private final SymbolNotFoundError typeNotFound;
aoqi@0 154
aoqi@0 155 public static Resolve instance(Context context) {
aoqi@0 156 Resolve instance = context.get(resolveKey);
aoqi@0 157 if (instance == null)
aoqi@0 158 instance = new Resolve(context);
aoqi@0 159 return instance;
aoqi@0 160 }
aoqi@0 161
aoqi@0 162 // <editor-fold defaultstate="collapsed" desc="Verbose resolution diagnostics support">
aoqi@0 163 enum VerboseResolutionMode {
aoqi@0 164 SUCCESS("success"),
aoqi@0 165 FAILURE("failure"),
aoqi@0 166 APPLICABLE("applicable"),
aoqi@0 167 INAPPLICABLE("inapplicable"),
aoqi@0 168 DEFERRED_INST("deferred-inference"),
aoqi@0 169 PREDEF("predef"),
aoqi@0 170 OBJECT_INIT("object-init"),
aoqi@0 171 INTERNAL("internal");
aoqi@0 172
aoqi@0 173 final String opt;
aoqi@0 174
aoqi@0 175 private VerboseResolutionMode(String opt) {
aoqi@0 176 this.opt = opt;
aoqi@0 177 }
aoqi@0 178
aoqi@0 179 static EnumSet<VerboseResolutionMode> getVerboseResolutionMode(Options opts) {
aoqi@0 180 String s = opts.get("verboseResolution");
aoqi@0 181 EnumSet<VerboseResolutionMode> res = EnumSet.noneOf(VerboseResolutionMode.class);
aoqi@0 182 if (s == null) return res;
aoqi@0 183 if (s.contains("all")) {
aoqi@0 184 res = EnumSet.allOf(VerboseResolutionMode.class);
aoqi@0 185 }
aoqi@0 186 Collection<String> args = Arrays.asList(s.split(","));
aoqi@0 187 for (VerboseResolutionMode mode : values()) {
aoqi@0 188 if (args.contains(mode.opt)) {
aoqi@0 189 res.add(mode);
aoqi@0 190 } else if (args.contains("-" + mode.opt)) {
aoqi@0 191 res.remove(mode);
aoqi@0 192 }
aoqi@0 193 }
aoqi@0 194 return res;
aoqi@0 195 }
aoqi@0 196 }
aoqi@0 197
aoqi@0 198 void reportVerboseResolutionDiagnostic(DiagnosticPosition dpos, Name name, Type site,
aoqi@0 199 List<Type> argtypes, List<Type> typeargtypes, Symbol bestSoFar) {
aoqi@0 200 boolean success = bestSoFar.kind < ERRONEOUS;
aoqi@0 201
aoqi@0 202 if (success && !verboseResolutionMode.contains(VerboseResolutionMode.SUCCESS)) {
aoqi@0 203 return;
aoqi@0 204 } else if (!success && !verboseResolutionMode.contains(VerboseResolutionMode.FAILURE)) {
aoqi@0 205 return;
aoqi@0 206 }
aoqi@0 207
aoqi@0 208 if (bestSoFar.name == names.init &&
aoqi@0 209 bestSoFar.owner == syms.objectType.tsym &&
aoqi@0 210 !verboseResolutionMode.contains(VerboseResolutionMode.OBJECT_INIT)) {
aoqi@0 211 return; //skip diags for Object constructor resolution
aoqi@0 212 } else if (site == syms.predefClass.type &&
aoqi@0 213 !verboseResolutionMode.contains(VerboseResolutionMode.PREDEF)) {
aoqi@0 214 return; //skip spurious diags for predef symbols (i.e. operators)
aoqi@0 215 } else if (currentResolutionContext.internalResolution &&
aoqi@0 216 !verboseResolutionMode.contains(VerboseResolutionMode.INTERNAL)) {
aoqi@0 217 return;
aoqi@0 218 }
aoqi@0 219
aoqi@0 220 int pos = 0;
aoqi@0 221 int mostSpecificPos = -1;
aoqi@0 222 ListBuffer<JCDiagnostic> subDiags = new ListBuffer<>();
aoqi@0 223 for (Candidate c : currentResolutionContext.candidates) {
aoqi@0 224 if (currentResolutionContext.step != c.step ||
aoqi@0 225 (c.isApplicable() && !verboseResolutionMode.contains(VerboseResolutionMode.APPLICABLE)) ||
aoqi@0 226 (!c.isApplicable() && !verboseResolutionMode.contains(VerboseResolutionMode.INAPPLICABLE))) {
aoqi@0 227 continue;
aoqi@0 228 } else {
aoqi@0 229 subDiags.append(c.isApplicable() ?
aoqi@0 230 getVerboseApplicableCandidateDiag(pos, c.sym, c.mtype) :
aoqi@0 231 getVerboseInapplicableCandidateDiag(pos, c.sym, c.details));
aoqi@0 232 if (c.sym == bestSoFar)
aoqi@0 233 mostSpecificPos = pos;
aoqi@0 234 pos++;
aoqi@0 235 }
aoqi@0 236 }
aoqi@0 237 String key = success ? "verbose.resolve.multi" : "verbose.resolve.multi.1";
aoqi@0 238 List<Type> argtypes2 = Type.map(argtypes,
aoqi@0 239 deferredAttr.new RecoveryDeferredTypeMap(AttrMode.SPECULATIVE, bestSoFar, currentResolutionContext.step));
aoqi@0 240 JCDiagnostic main = diags.note(log.currentSource(), dpos, key, name,
aoqi@0 241 site.tsym, mostSpecificPos, currentResolutionContext.step,
aoqi@0 242 methodArguments(argtypes2),
aoqi@0 243 methodArguments(typeargtypes));
aoqi@0 244 JCDiagnostic d = new JCDiagnostic.MultilineDiagnostic(main, subDiags.toList());
aoqi@0 245 log.report(d);
aoqi@0 246 }
aoqi@0 247
aoqi@0 248 JCDiagnostic getVerboseApplicableCandidateDiag(int pos, Symbol sym, Type inst) {
aoqi@0 249 JCDiagnostic subDiag = null;
aoqi@0 250 if (sym.type.hasTag(FORALL)) {
aoqi@0 251 subDiag = diags.fragment("partial.inst.sig", inst);
aoqi@0 252 }
aoqi@0 253
aoqi@0 254 String key = subDiag == null ?
aoqi@0 255 "applicable.method.found" :
aoqi@0 256 "applicable.method.found.1";
aoqi@0 257
aoqi@0 258 return diags.fragment(key, pos, sym, subDiag);
aoqi@0 259 }
aoqi@0 260
aoqi@0 261 JCDiagnostic getVerboseInapplicableCandidateDiag(int pos, Symbol sym, JCDiagnostic subDiag) {
aoqi@0 262 return diags.fragment("not.applicable.method.found", pos, sym, subDiag);
aoqi@0 263 }
aoqi@0 264 // </editor-fold>
aoqi@0 265
aoqi@0 266 /* ************************************************************************
aoqi@0 267 * Identifier resolution
aoqi@0 268 *************************************************************************/
aoqi@0 269
aoqi@0 270 /** An environment is "static" if its static level is greater than
aoqi@0 271 * the one of its outer environment
aoqi@0 272 */
aoqi@0 273 protected static boolean isStatic(Env<AttrContext> env) {
alundblad@2814 274 return env.outer != null && env.info.staticLevel > env.outer.info.staticLevel;
aoqi@0 275 }
aoqi@0 276
aoqi@0 277 /** An environment is an "initializer" if it is a constructor or
aoqi@0 278 * an instance initializer.
aoqi@0 279 */
aoqi@0 280 static boolean isInitializer(Env<AttrContext> env) {
aoqi@0 281 Symbol owner = env.info.scope.owner;
aoqi@0 282 return owner.isConstructor() ||
aoqi@0 283 owner.owner.kind == TYP &&
aoqi@0 284 (owner.kind == VAR ||
aoqi@0 285 owner.kind == MTH && (owner.flags() & BLOCK) != 0) &&
aoqi@0 286 (owner.flags() & STATIC) == 0;
aoqi@0 287 }
aoqi@0 288
aoqi@0 289 /** Is class accessible in given evironment?
aoqi@0 290 * @param env The current environment.
aoqi@0 291 * @param c The class whose accessibility is checked.
aoqi@0 292 */
aoqi@0 293 public boolean isAccessible(Env<AttrContext> env, TypeSymbol c) {
aoqi@0 294 return isAccessible(env, c, false);
aoqi@0 295 }
aoqi@0 296
aoqi@0 297 public boolean isAccessible(Env<AttrContext> env, TypeSymbol c, boolean checkInner) {
aoqi@0 298 boolean isAccessible = false;
aoqi@0 299 switch ((short)(c.flags() & AccessFlags)) {
aoqi@0 300 case PRIVATE:
aoqi@0 301 isAccessible =
aoqi@0 302 env.enclClass.sym.outermostClass() ==
aoqi@0 303 c.owner.outermostClass();
aoqi@0 304 break;
aoqi@0 305 case 0:
aoqi@0 306 isAccessible =
aoqi@0 307 env.toplevel.packge == c.owner // fast special case
aoqi@0 308 ||
aoqi@0 309 env.toplevel.packge == c.packge()
aoqi@0 310 ||
aoqi@0 311 // Hack: this case is added since synthesized default constructors
aoqi@0 312 // of anonymous classes should be allowed to access
aoqi@0 313 // classes which would be inaccessible otherwise.
aoqi@0 314 env.enclMethod != null &&
aoqi@0 315 (env.enclMethod.mods.flags & ANONCONSTR) != 0;
aoqi@0 316 break;
aoqi@0 317 default: // error recovery
aoqi@0 318 case PUBLIC:
aoqi@0 319 isAccessible = true;
aoqi@0 320 break;
aoqi@0 321 case PROTECTED:
aoqi@0 322 isAccessible =
aoqi@0 323 env.toplevel.packge == c.owner // fast special case
aoqi@0 324 ||
aoqi@0 325 env.toplevel.packge == c.packge()
aoqi@0 326 ||
aoqi@0 327 isInnerSubClass(env.enclClass.sym, c.owner);
aoqi@0 328 break;
aoqi@0 329 }
aoqi@0 330 return (checkInner == false || c.type.getEnclosingType() == Type.noType) ?
aoqi@0 331 isAccessible :
aoqi@0 332 isAccessible && isAccessible(env, c.type.getEnclosingType(), checkInner);
aoqi@0 333 }
aoqi@0 334 //where
aoqi@0 335 /** Is given class a subclass of given base class, or an inner class
aoqi@0 336 * of a subclass?
aoqi@0 337 * Return null if no such class exists.
aoqi@0 338 * @param c The class which is the subclass or is contained in it.
aoqi@0 339 * @param base The base class
aoqi@0 340 */
aoqi@0 341 private boolean isInnerSubClass(ClassSymbol c, Symbol base) {
aoqi@0 342 while (c != null && !c.isSubClass(base, types)) {
aoqi@0 343 c = c.owner.enclClass();
aoqi@0 344 }
aoqi@0 345 return c != null;
aoqi@0 346 }
aoqi@0 347
aoqi@0 348 boolean isAccessible(Env<AttrContext> env, Type t) {
aoqi@0 349 return isAccessible(env, t, false);
aoqi@0 350 }
aoqi@0 351
aoqi@0 352 boolean isAccessible(Env<AttrContext> env, Type t, boolean checkInner) {
aoqi@0 353 return (t.hasTag(ARRAY))
aoqi@0 354 ? isAccessible(env, types.cvarUpperBound(types.elemtype(t)))
aoqi@0 355 : isAccessible(env, t.tsym, checkInner);
aoqi@0 356 }
aoqi@0 357
aoqi@0 358 /** Is symbol accessible as a member of given type in given environment?
aoqi@0 359 * @param env The current environment.
aoqi@0 360 * @param site The type of which the tested symbol is regarded
aoqi@0 361 * as a member.
aoqi@0 362 * @param sym The symbol.
aoqi@0 363 */
aoqi@0 364 public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym) {
aoqi@0 365 return isAccessible(env, site, sym, false);
aoqi@0 366 }
aoqi@0 367 public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym, boolean checkInner) {
aoqi@0 368 if (sym.name == names.init && sym.owner != site.tsym) return false;
aoqi@0 369 switch ((short)(sym.flags() & AccessFlags)) {
aoqi@0 370 case PRIVATE:
aoqi@0 371 return
aoqi@0 372 (env.enclClass.sym == sym.owner // fast special case
aoqi@0 373 ||
aoqi@0 374 env.enclClass.sym.outermostClass() ==
aoqi@0 375 sym.owner.outermostClass())
aoqi@0 376 &&
aoqi@0 377 sym.isInheritedIn(site.tsym, types);
aoqi@0 378 case 0:
aoqi@0 379 return
aoqi@0 380 (env.toplevel.packge == sym.owner.owner // fast special case
aoqi@0 381 ||
aoqi@0 382 env.toplevel.packge == sym.packge())
aoqi@0 383 &&
aoqi@0 384 isAccessible(env, site, checkInner)
aoqi@0 385 &&
aoqi@0 386 sym.isInheritedIn(site.tsym, types)
aoqi@0 387 &&
aoqi@0 388 notOverriddenIn(site, sym);
aoqi@0 389 case PROTECTED:
aoqi@0 390 return
aoqi@0 391 (env.toplevel.packge == sym.owner.owner // fast special case
aoqi@0 392 ||
aoqi@0 393 env.toplevel.packge == sym.packge()
aoqi@0 394 ||
aoqi@0 395 isProtectedAccessible(sym, env.enclClass.sym, site)
aoqi@0 396 ||
aoqi@0 397 // OK to select instance method or field from 'super' or type name
aoqi@0 398 // (but type names should be disallowed elsewhere!)
aoqi@0 399 env.info.selectSuper && (sym.flags() & STATIC) == 0 && sym.kind != TYP)
aoqi@0 400 &&
aoqi@0 401 isAccessible(env, site, checkInner)
aoqi@0 402 &&
aoqi@0 403 notOverriddenIn(site, sym);
aoqi@0 404 default: // this case includes erroneous combinations as well
aoqi@0 405 return isAccessible(env, site, checkInner) && notOverriddenIn(site, sym);
aoqi@0 406 }
aoqi@0 407 }
aoqi@0 408 //where
aoqi@0 409 /* `sym' is accessible only if not overridden by
aoqi@0 410 * another symbol which is a member of `site'
aoqi@0 411 * (because, if it is overridden, `sym' is not strictly
aoqi@0 412 * speaking a member of `site'). A polymorphic signature method
aoqi@0 413 * cannot be overridden (e.g. MH.invokeExact(Object[])).
aoqi@0 414 */
aoqi@0 415 private boolean notOverriddenIn(Type site, Symbol sym) {
aoqi@0 416 if (sym.kind != MTH || sym.isConstructor() || sym.isStatic())
aoqi@0 417 return true;
aoqi@0 418 else {
aoqi@0 419 Symbol s2 = ((MethodSymbol)sym).implementation(site.tsym, types, true);
aoqi@0 420 return (s2 == null || s2 == sym || sym.owner == s2.owner ||
aoqi@0 421 !types.isSubSignature(types.memberType(site, s2), types.memberType(site, sym)));
aoqi@0 422 }
aoqi@0 423 }
aoqi@0 424 //where
aoqi@0 425 /** Is given protected symbol accessible if it is selected from given site
aoqi@0 426 * and the selection takes place in given class?
aoqi@0 427 * @param sym The symbol with protected access
aoqi@0 428 * @param c The class where the access takes place
aoqi@0 429 * @site The type of the qualifier
aoqi@0 430 */
aoqi@0 431 private
aoqi@0 432 boolean isProtectedAccessible(Symbol sym, ClassSymbol c, Type site) {
aoqi@0 433 Type newSite = site.hasTag(TYPEVAR) ? site.getUpperBound() : site;
aoqi@0 434 while (c != null &&
aoqi@0 435 !(c.isSubClass(sym.owner, types) &&
aoqi@0 436 (c.flags() & INTERFACE) == 0 &&
aoqi@0 437 // In JLS 2e 6.6.2.1, the subclass restriction applies
aoqi@0 438 // only to instance fields and methods -- types are excluded
aoqi@0 439 // regardless of whether they are declared 'static' or not.
aoqi@0 440 ((sym.flags() & STATIC) != 0 || sym.kind == TYP || newSite.tsym.isSubClass(c, types))))
aoqi@0 441 c = c.owner.enclClass();
aoqi@0 442 return c != null;
aoqi@0 443 }
aoqi@0 444
aoqi@0 445 /**
aoqi@0 446 * Performs a recursive scan of a type looking for accessibility problems
aoqi@0 447 * from current attribution environment
aoqi@0 448 */
aoqi@0 449 void checkAccessibleType(Env<AttrContext> env, Type t) {
aoqi@0 450 accessibilityChecker.visit(t, env);
aoqi@0 451 }
aoqi@0 452
aoqi@0 453 /**
aoqi@0 454 * Accessibility type-visitor
aoqi@0 455 */
aoqi@0 456 Types.SimpleVisitor<Void, Env<AttrContext>> accessibilityChecker =
aoqi@0 457 new Types.SimpleVisitor<Void, Env<AttrContext>>() {
aoqi@0 458
aoqi@0 459 void visit(List<Type> ts, Env<AttrContext> env) {
aoqi@0 460 for (Type t : ts) {
aoqi@0 461 visit(t, env);
aoqi@0 462 }
aoqi@0 463 }
aoqi@0 464
aoqi@0 465 public Void visitType(Type t, Env<AttrContext> env) {
aoqi@0 466 return null;
aoqi@0 467 }
aoqi@0 468
aoqi@0 469 @Override
aoqi@0 470 public Void visitArrayType(ArrayType t, Env<AttrContext> env) {
aoqi@0 471 visit(t.elemtype, env);
aoqi@0 472 return null;
aoqi@0 473 }
aoqi@0 474
aoqi@0 475 @Override
aoqi@0 476 public Void visitClassType(ClassType t, Env<AttrContext> env) {
aoqi@0 477 visit(t.getTypeArguments(), env);
aoqi@0 478 if (!isAccessible(env, t, true)) {
aoqi@0 479 accessBase(new AccessError(t.tsym), env.tree.pos(), env.enclClass.sym, t, t.tsym.name, true);
aoqi@0 480 }
aoqi@0 481 return null;
aoqi@0 482 }
aoqi@0 483
aoqi@0 484 @Override
aoqi@0 485 public Void visitWildcardType(WildcardType t, Env<AttrContext> env) {
aoqi@0 486 visit(t.type, env);
aoqi@0 487 return null;
aoqi@0 488 }
aoqi@0 489
aoqi@0 490 @Override
aoqi@0 491 public Void visitMethodType(MethodType t, Env<AttrContext> env) {
aoqi@0 492 visit(t.getParameterTypes(), env);
aoqi@0 493 visit(t.getReturnType(), env);
aoqi@0 494 visit(t.getThrownTypes(), env);
aoqi@0 495 return null;
aoqi@0 496 }
aoqi@0 497 };
aoqi@0 498
aoqi@0 499 /** Try to instantiate the type of a method so that it fits
aoqi@0 500 * given type arguments and argument types. If successful, return
aoqi@0 501 * the method's instantiated type, else return null.
aoqi@0 502 * The instantiation will take into account an additional leading
aoqi@0 503 * formal parameter if the method is an instance method seen as a member
aoqi@0 504 * of an under determined site. In this case, we treat site as an additional
aoqi@0 505 * parameter and the parameters of the class containing the method as
aoqi@0 506 * additional type variables that get instantiated.
aoqi@0 507 *
aoqi@0 508 * @param env The current environment
aoqi@0 509 * @param site The type of which the method is a member.
aoqi@0 510 * @param m The method symbol.
aoqi@0 511 * @param argtypes The invocation's given value arguments.
aoqi@0 512 * @param typeargtypes The invocation's given type arguments.
aoqi@0 513 * @param allowBoxing Allow boxing conversions of arguments.
aoqi@0 514 * @param useVarargs Box trailing arguments into an array for varargs.
aoqi@0 515 */
aoqi@0 516 Type rawInstantiate(Env<AttrContext> env,
aoqi@0 517 Type site,
aoqi@0 518 Symbol m,
aoqi@0 519 ResultInfo resultInfo,
aoqi@0 520 List<Type> argtypes,
aoqi@0 521 List<Type> typeargtypes,
aoqi@0 522 boolean allowBoxing,
aoqi@0 523 boolean useVarargs,
aoqi@0 524 Warner warn) throws Infer.InferenceException {
aoqi@0 525
aoqi@0 526 Type mt = types.memberType(site, m);
aoqi@0 527 // tvars is the list of formal type variables for which type arguments
aoqi@0 528 // need to inferred.
aoqi@0 529 List<Type> tvars = List.nil();
aoqi@0 530 if (typeargtypes == null) typeargtypes = List.nil();
aoqi@0 531 if (!mt.hasTag(FORALL) && typeargtypes.nonEmpty()) {
aoqi@0 532 // This is not a polymorphic method, but typeargs are supplied
aoqi@0 533 // which is fine, see JLS 15.12.2.1
aoqi@0 534 } else if (mt.hasTag(FORALL) && typeargtypes.nonEmpty()) {
aoqi@0 535 ForAll pmt = (ForAll) mt;
aoqi@0 536 if (typeargtypes.length() != pmt.tvars.length())
aoqi@0 537 throw inapplicableMethodException.setMessage("arg.length.mismatch"); // not enough args
aoqi@0 538 // Check type arguments are within bounds
aoqi@0 539 List<Type> formals = pmt.tvars;
aoqi@0 540 List<Type> actuals = typeargtypes;
aoqi@0 541 while (formals.nonEmpty() && actuals.nonEmpty()) {
aoqi@0 542 List<Type> bounds = types.subst(types.getBounds((TypeVar)formals.head),
aoqi@0 543 pmt.tvars, typeargtypes);
aoqi@0 544 for (; bounds.nonEmpty(); bounds = bounds.tail)
aoqi@0 545 if (!types.isSubtypeUnchecked(actuals.head, bounds.head, warn))
aoqi@0 546 throw inapplicableMethodException.setMessage("explicit.param.do.not.conform.to.bounds",actuals.head, bounds);
aoqi@0 547 formals = formals.tail;
aoqi@0 548 actuals = actuals.tail;
aoqi@0 549 }
aoqi@0 550 mt = types.subst(pmt.qtype, pmt.tvars, typeargtypes);
aoqi@0 551 } else if (mt.hasTag(FORALL)) {
aoqi@0 552 ForAll pmt = (ForAll) mt;
aoqi@0 553 List<Type> tvars1 = types.newInstances(pmt.tvars);
aoqi@0 554 tvars = tvars.appendList(tvars1);
aoqi@0 555 mt = types.subst(pmt.qtype, pmt.tvars, tvars1);
aoqi@0 556 }
aoqi@0 557
aoqi@0 558 // find out whether we need to go the slow route via infer
aoqi@0 559 boolean instNeeded = tvars.tail != null; /*inlined: tvars.nonEmpty()*/
aoqi@0 560 for (List<Type> l = argtypes;
aoqi@0 561 l.tail != null/*inlined: l.nonEmpty()*/ && !instNeeded;
aoqi@0 562 l = l.tail) {
aoqi@0 563 if (l.head.hasTag(FORALL)) instNeeded = true;
aoqi@0 564 }
aoqi@0 565
aoqi@0 566 if (instNeeded)
aoqi@0 567 return infer.instantiateMethod(env,
aoqi@0 568 tvars,
aoqi@0 569 (MethodType)mt,
aoqi@0 570 resultInfo,
aoqi@0 571 (MethodSymbol)m,
aoqi@0 572 argtypes,
aoqi@0 573 allowBoxing,
aoqi@0 574 useVarargs,
aoqi@0 575 currentResolutionContext,
aoqi@0 576 warn);
aoqi@0 577
aoqi@0 578 DeferredAttr.DeferredAttrContext dc = currentResolutionContext.deferredAttrContext(m, infer.emptyContext, resultInfo, warn);
aoqi@0 579 currentResolutionContext.methodCheck.argumentsAcceptable(env, dc,
aoqi@0 580 argtypes, mt.getParameterTypes(), warn);
aoqi@0 581 dc.complete();
aoqi@0 582 return mt;
aoqi@0 583 }
aoqi@0 584
aoqi@0 585 Type checkMethod(Env<AttrContext> env,
aoqi@0 586 Type site,
aoqi@0 587 Symbol m,
aoqi@0 588 ResultInfo resultInfo,
aoqi@0 589 List<Type> argtypes,
aoqi@0 590 List<Type> typeargtypes,
aoqi@0 591 Warner warn) {
aoqi@0 592 MethodResolutionContext prevContext = currentResolutionContext;
aoqi@0 593 try {
aoqi@0 594 currentResolutionContext = new MethodResolutionContext();
aoqi@0 595 currentResolutionContext.attrMode = DeferredAttr.AttrMode.CHECK;
aoqi@0 596 if (env.tree.hasTag(JCTree.Tag.REFERENCE)) {
aoqi@0 597 //method/constructor references need special check class
aoqi@0 598 //to handle inference variables in 'argtypes' (might happen
aoqi@0 599 //during an unsticking round)
aoqi@0 600 currentResolutionContext.methodCheck =
aoqi@0 601 new MethodReferenceCheck(resultInfo.checkContext.inferenceContext());
aoqi@0 602 }
aoqi@0 603 MethodResolutionPhase step = currentResolutionContext.step = env.info.pendingResolutionPhase;
aoqi@0 604 return rawInstantiate(env, site, m, resultInfo, argtypes, typeargtypes,
aoqi@0 605 step.isBoxingRequired(), step.isVarargsRequired(), warn);
aoqi@0 606 }
aoqi@0 607 finally {
aoqi@0 608 currentResolutionContext = prevContext;
aoqi@0 609 }
aoqi@0 610 }
aoqi@0 611
aoqi@0 612 /** Same but returns null instead throwing a NoInstanceException
aoqi@0 613 */
aoqi@0 614 Type instantiate(Env<AttrContext> env,
aoqi@0 615 Type site,
aoqi@0 616 Symbol m,
aoqi@0 617 ResultInfo resultInfo,
aoqi@0 618 List<Type> argtypes,
aoqi@0 619 List<Type> typeargtypes,
aoqi@0 620 boolean allowBoxing,
aoqi@0 621 boolean useVarargs,
aoqi@0 622 Warner warn) {
aoqi@0 623 try {
aoqi@0 624 return rawInstantiate(env, site, m, resultInfo, argtypes, typeargtypes,
aoqi@0 625 allowBoxing, useVarargs, warn);
aoqi@0 626 } catch (InapplicableMethodException ex) {
aoqi@0 627 return null;
aoqi@0 628 }
aoqi@0 629 }
aoqi@0 630
aoqi@0 631 /**
aoqi@0 632 * This interface defines an entry point that should be used to perform a
aoqi@0 633 * method check. A method check usually consist in determining as to whether
aoqi@0 634 * a set of types (actuals) is compatible with another set of types (formals).
aoqi@0 635 * Since the notion of compatibility can vary depending on the circumstances,
aoqi@0 636 * this interfaces allows to easily add new pluggable method check routines.
aoqi@0 637 */
aoqi@0 638 interface MethodCheck {
aoqi@0 639 /**
aoqi@0 640 * Main method check routine. A method check usually consist in determining
aoqi@0 641 * as to whether a set of types (actuals) is compatible with another set of
aoqi@0 642 * types (formals). If an incompatibility is found, an unchecked exception
aoqi@0 643 * is assumed to be thrown.
aoqi@0 644 */
aoqi@0 645 void argumentsAcceptable(Env<AttrContext> env,
aoqi@0 646 DeferredAttrContext deferredAttrContext,
aoqi@0 647 List<Type> argtypes,
aoqi@0 648 List<Type> formals,
aoqi@0 649 Warner warn);
aoqi@0 650
aoqi@0 651 /**
aoqi@0 652 * Retrieve the method check object that will be used during a
aoqi@0 653 * most specific check.
aoqi@0 654 */
aoqi@0 655 MethodCheck mostSpecificCheck(List<Type> actuals, boolean strict);
aoqi@0 656 }
aoqi@0 657
aoqi@0 658 /**
aoqi@0 659 * Helper enum defining all method check diagnostics (used by resolveMethodCheck).
aoqi@0 660 */
aoqi@0 661 enum MethodCheckDiag {
aoqi@0 662 /**
aoqi@0 663 * Actuals and formals differs in length.
aoqi@0 664 */
aoqi@0 665 ARITY_MISMATCH("arg.length.mismatch", "infer.arg.length.mismatch"),
aoqi@0 666 /**
aoqi@0 667 * An actual is incompatible with a formal.
aoqi@0 668 */
aoqi@0 669 ARG_MISMATCH("no.conforming.assignment.exists", "infer.no.conforming.assignment.exists"),
aoqi@0 670 /**
aoqi@0 671 * An actual is incompatible with the varargs element type.
aoqi@0 672 */
aoqi@0 673 VARARG_MISMATCH("varargs.argument.mismatch", "infer.varargs.argument.mismatch"),
aoqi@0 674 /**
aoqi@0 675 * The varargs element type is inaccessible.
aoqi@0 676 */
aoqi@0 677 INACCESSIBLE_VARARGS("inaccessible.varargs.type", "inaccessible.varargs.type");
aoqi@0 678
aoqi@0 679 final String basicKey;
aoqi@0 680 final String inferKey;
aoqi@0 681
aoqi@0 682 MethodCheckDiag(String basicKey, String inferKey) {
aoqi@0 683 this.basicKey = basicKey;
aoqi@0 684 this.inferKey = inferKey;
aoqi@0 685 }
aoqi@0 686
aoqi@0 687 String regex() {
aoqi@0 688 return String.format("([a-z]*\\.)*(%s|%s)", basicKey, inferKey);
aoqi@0 689 }
aoqi@0 690 }
aoqi@0 691
aoqi@0 692 /**
aoqi@0 693 * Dummy method check object. All methods are deemed applicable, regardless
aoqi@0 694 * of their formal parameter types.
aoqi@0 695 */
aoqi@0 696 MethodCheck nilMethodCheck = new MethodCheck() {
aoqi@0 697 public void argumentsAcceptable(Env<AttrContext> env, DeferredAttrContext deferredAttrContext, List<Type> argtypes, List<Type> formals, Warner warn) {
aoqi@0 698 //do nothing - method always applicable regardless of actuals
aoqi@0 699 }
aoqi@0 700
aoqi@0 701 public MethodCheck mostSpecificCheck(List<Type> actuals, boolean strict) {
aoqi@0 702 return this;
aoqi@0 703 }
aoqi@0 704 };
aoqi@0 705
aoqi@0 706 /**
aoqi@0 707 * Base class for 'real' method checks. The class defines the logic for
aoqi@0 708 * iterating through formals and actuals and provides and entry point
aoqi@0 709 * that can be used by subclasses in order to define the actual check logic.
aoqi@0 710 */
aoqi@0 711 abstract class AbstractMethodCheck implements MethodCheck {
aoqi@0 712 @Override
aoqi@0 713 public void argumentsAcceptable(final Env<AttrContext> env,
aoqi@0 714 DeferredAttrContext deferredAttrContext,
aoqi@0 715 List<Type> argtypes,
aoqi@0 716 List<Type> formals,
aoqi@0 717 Warner warn) {
aoqi@0 718 //should we expand formals?
aoqi@0 719 boolean useVarargs = deferredAttrContext.phase.isVarargsRequired();
sadayapalam@3005 720 JCTree callTree = treeForDiagnostics(env);
sadayapalam@3005 721 List<JCExpression> trees = TreeInfo.args(callTree);
aoqi@0 722
aoqi@0 723 //inference context used during this method check
aoqi@0 724 InferenceContext inferenceContext = deferredAttrContext.inferenceContext;
aoqi@0 725
aoqi@0 726 Type varargsFormal = useVarargs ? formals.last() : null;
aoqi@0 727
aoqi@0 728 if (varargsFormal == null &&
aoqi@0 729 argtypes.size() != formals.size()) {
sadayapalam@3005 730 reportMC(callTree, MethodCheckDiag.ARITY_MISMATCH, inferenceContext); // not enough args
aoqi@0 731 }
aoqi@0 732
aoqi@0 733 while (argtypes.nonEmpty() && formals.head != varargsFormal) {
aoqi@0 734 DiagnosticPosition pos = trees != null ? trees.head : null;
aoqi@0 735 checkArg(pos, false, argtypes.head, formals.head, deferredAttrContext, warn);
aoqi@0 736 argtypes = argtypes.tail;
aoqi@0 737 formals = formals.tail;
aoqi@0 738 trees = trees != null ? trees.tail : trees;
aoqi@0 739 }
aoqi@0 740
aoqi@0 741 if (formals.head != varargsFormal) {
sadayapalam@3005 742 reportMC(callTree, MethodCheckDiag.ARITY_MISMATCH, inferenceContext); // not enough args
aoqi@0 743 }
aoqi@0 744
aoqi@0 745 if (useVarargs) {
aoqi@0 746 //note: if applicability check is triggered by most specific test,
aoqi@0 747 //the last argument of a varargs is _not_ an array type (see JLS 15.12.2.5)
aoqi@0 748 final Type elt = types.elemtype(varargsFormal);
aoqi@0 749 while (argtypes.nonEmpty()) {
aoqi@0 750 DiagnosticPosition pos = trees != null ? trees.head : null;
aoqi@0 751 checkArg(pos, true, argtypes.head, elt, deferredAttrContext, warn);
aoqi@0 752 argtypes = argtypes.tail;
aoqi@0 753 trees = trees != null ? trees.tail : trees;
aoqi@0 754 }
aoqi@0 755 }
aoqi@0 756 }
aoqi@0 757
sadayapalam@3005 758 // where
sadayapalam@3005 759 private JCTree treeForDiagnostics(Env<AttrContext> env) {
sadayapalam@3005 760 return env.info.preferredTreeForDiagnostics != null ? env.info.preferredTreeForDiagnostics : env.tree;
sadayapalam@3005 761 }
sadayapalam@3005 762
aoqi@0 763 /**
aoqi@0 764 * Does the actual argument conforms to the corresponding formal?
aoqi@0 765 */
aoqi@0 766 abstract void checkArg(DiagnosticPosition pos, boolean varargs, Type actual, Type formal, DeferredAttrContext deferredAttrContext, Warner warn);
aoqi@0 767
aoqi@0 768 protected void reportMC(DiagnosticPosition pos, MethodCheckDiag diag, InferenceContext inferenceContext, Object... args) {
aoqi@0 769 boolean inferDiag = inferenceContext != infer.emptyContext;
aoqi@0 770 InapplicableMethodException ex = inferDiag ?
aoqi@0 771 infer.inferenceException : inapplicableMethodException;
aoqi@0 772 if (inferDiag && (!diag.inferKey.equals(diag.basicKey))) {
aoqi@0 773 Object[] args2 = new Object[args.length + 1];
aoqi@0 774 System.arraycopy(args, 0, args2, 1, args.length);
aoqi@0 775 args2[0] = inferenceContext.inferenceVars();
aoqi@0 776 args = args2;
aoqi@0 777 }
aoqi@0 778 String key = inferDiag ? diag.inferKey : diag.basicKey;
aoqi@0 779 throw ex.setMessage(diags.create(DiagnosticType.FRAGMENT, log.currentSource(), pos, key, args));
aoqi@0 780 }
aoqi@0 781
aoqi@0 782 public MethodCheck mostSpecificCheck(List<Type> actuals, boolean strict) {
aoqi@0 783 return nilMethodCheck;
aoqi@0 784 }
aoqi@0 785
aoqi@0 786 }
aoqi@0 787
aoqi@0 788 /**
aoqi@0 789 * Arity-based method check. A method is applicable if the number of actuals
aoqi@0 790 * supplied conforms to the method signature.
aoqi@0 791 */
aoqi@0 792 MethodCheck arityMethodCheck = new AbstractMethodCheck() {
aoqi@0 793 @Override
aoqi@0 794 void checkArg(DiagnosticPosition pos, boolean varargs, Type actual, Type formal, DeferredAttrContext deferredAttrContext, Warner warn) {
aoqi@0 795 //do nothing - actual always compatible to formals
aoqi@0 796 }
aoqi@0 797
aoqi@0 798 @Override
aoqi@0 799 public String toString() {
aoqi@0 800 return "arityMethodCheck";
aoqi@0 801 }
aoqi@0 802 };
aoqi@0 803
aoqi@0 804 List<Type> dummyArgs(int length) {
aoqi@0 805 ListBuffer<Type> buf = new ListBuffer<>();
aoqi@0 806 for (int i = 0 ; i < length ; i++) {
aoqi@0 807 buf.append(Type.noType);
aoqi@0 808 }
aoqi@0 809 return buf.toList();
aoqi@0 810 }
aoqi@0 811
aoqi@0 812 /**
aoqi@0 813 * Main method applicability routine. Given a list of actual types A,
aoqi@0 814 * a list of formal types F, determines whether the types in A are
aoqi@0 815 * compatible (by method invocation conversion) with the types in F.
aoqi@0 816 *
aoqi@0 817 * Since this routine is shared between overload resolution and method
aoqi@0 818 * type-inference, a (possibly empty) inference context is used to convert
aoqi@0 819 * formal types to the corresponding 'undet' form ahead of a compatibility
aoqi@0 820 * check so that constraints can be propagated and collected.
aoqi@0 821 *
aoqi@0 822 * Moreover, if one or more types in A is a deferred type, this routine uses
aoqi@0 823 * DeferredAttr in order to perform deferred attribution. If one or more actual
aoqi@0 824 * deferred types are stuck, they are placed in a queue and revisited later
aoqi@0 825 * after the remainder of the arguments have been seen. If this is not sufficient
aoqi@0 826 * to 'unstuck' the argument, a cyclic inference error is called out.
aoqi@0 827 *
aoqi@0 828 * A method check handler (see above) is used in order to report errors.
aoqi@0 829 */
aoqi@0 830 MethodCheck resolveMethodCheck = new AbstractMethodCheck() {
aoqi@0 831
aoqi@0 832 @Override
aoqi@0 833 void checkArg(DiagnosticPosition pos, boolean varargs, Type actual, Type formal, DeferredAttrContext deferredAttrContext, Warner warn) {
aoqi@0 834 ResultInfo mresult = methodCheckResult(varargs, formal, deferredAttrContext, warn);
aoqi@0 835 mresult.check(pos, actual);
aoqi@0 836 }
aoqi@0 837
aoqi@0 838 @Override
aoqi@0 839 public void argumentsAcceptable(final Env<AttrContext> env,
aoqi@0 840 DeferredAttrContext deferredAttrContext,
aoqi@0 841 List<Type> argtypes,
aoqi@0 842 List<Type> formals,
aoqi@0 843 Warner warn) {
aoqi@0 844 super.argumentsAcceptable(env, deferredAttrContext, argtypes, formals, warn);
dlsmith@2788 845 // should we check varargs element type accessibility?
aoqi@0 846 if (deferredAttrContext.phase.isVarargsRequired()) {
dlsmith@2788 847 if (deferredAttrContext.mode == AttrMode.CHECK || !checkVarargsAccessAfterResolution) {
dlsmith@2788 848 varargsAccessible(env, types.elemtype(formals.last()), deferredAttrContext.inferenceContext);
vromero@2535 849 }
aoqi@0 850 }
aoqi@0 851 }
aoqi@0 852
dlsmith@2788 853 /**
dlsmith@2788 854 * Test that the runtime array element type corresponding to 't' is accessible. 't' should be the
dlsmith@2788 855 * varargs element type of either the method invocation type signature (after inference completes)
dlsmith@2788 856 * or the method declaration signature (before inference completes).
dlsmith@2788 857 */
aoqi@0 858 private void varargsAccessible(final Env<AttrContext> env, final Type t, final InferenceContext inferenceContext) {
aoqi@0 859 if (inferenceContext.free(t)) {
aoqi@0 860 inferenceContext.addFreeTypeListener(List.of(t), new FreeTypeListener() {
aoqi@0 861 @Override
aoqi@0 862 public void typesInferred(InferenceContext inferenceContext) {
aoqi@0 863 varargsAccessible(env, inferenceContext.asInstType(t), inferenceContext);
aoqi@0 864 }
aoqi@0 865 });
aoqi@0 866 } else {
dlsmith@2788 867 if (!isAccessible(env, types.erasure(t))) {
aoqi@0 868 Symbol location = env.enclClass.sym;
aoqi@0 869 reportMC(env.tree, MethodCheckDiag.INACCESSIBLE_VARARGS, inferenceContext, t, Kinds.kindName(location), location);
aoqi@0 870 }
aoqi@0 871 }
aoqi@0 872 }
aoqi@0 873
aoqi@0 874 private ResultInfo methodCheckResult(final boolean varargsCheck, Type to,
aoqi@0 875 final DeferredAttr.DeferredAttrContext deferredAttrContext, Warner rsWarner) {
aoqi@0 876 CheckContext checkContext = new MethodCheckContext(!deferredAttrContext.phase.isBoxingRequired(), deferredAttrContext, rsWarner) {
aoqi@0 877 MethodCheckDiag methodDiag = varargsCheck ?
aoqi@0 878 MethodCheckDiag.VARARG_MISMATCH : MethodCheckDiag.ARG_MISMATCH;
aoqi@0 879
aoqi@0 880 @Override
aoqi@0 881 public void report(DiagnosticPosition pos, JCDiagnostic details) {
aoqi@0 882 reportMC(pos, methodDiag, deferredAttrContext.inferenceContext, details);
aoqi@0 883 }
aoqi@0 884 };
aoqi@0 885 return new MethodResultInfo(to, checkContext);
aoqi@0 886 }
aoqi@0 887
aoqi@0 888 @Override
aoqi@0 889 public MethodCheck mostSpecificCheck(List<Type> actuals, boolean strict) {
aoqi@0 890 return new MostSpecificCheck(strict, actuals);
aoqi@0 891 }
aoqi@0 892
aoqi@0 893 @Override
aoqi@0 894 public String toString() {
aoqi@0 895 return "resolveMethodCheck";
aoqi@0 896 }
aoqi@0 897 };
aoqi@0 898
aoqi@0 899 /**
aoqi@0 900 * This class handles method reference applicability checks; since during
aoqi@0 901 * these checks it's sometime possible to have inference variables on
aoqi@0 902 * the actual argument types list, the method applicability check must be
aoqi@0 903 * extended so that inference variables are 'opened' as needed.
aoqi@0 904 */
aoqi@0 905 class MethodReferenceCheck extends AbstractMethodCheck {
aoqi@0 906
aoqi@0 907 InferenceContext pendingInferenceContext;
aoqi@0 908
aoqi@0 909 MethodReferenceCheck(InferenceContext pendingInferenceContext) {
aoqi@0 910 this.pendingInferenceContext = pendingInferenceContext;
aoqi@0 911 }
aoqi@0 912
aoqi@0 913 @Override
aoqi@0 914 void checkArg(DiagnosticPosition pos, boolean varargs, Type actual, Type formal, DeferredAttrContext deferredAttrContext, Warner warn) {
aoqi@0 915 ResultInfo mresult = methodCheckResult(varargs, formal, deferredAttrContext, warn);
aoqi@0 916 mresult.check(pos, actual);
aoqi@0 917 }
aoqi@0 918
aoqi@0 919 private ResultInfo methodCheckResult(final boolean varargsCheck, Type to,
aoqi@0 920 final DeferredAttr.DeferredAttrContext deferredAttrContext, Warner rsWarner) {
aoqi@0 921 CheckContext checkContext = new MethodCheckContext(!deferredAttrContext.phase.isBoxingRequired(), deferredAttrContext, rsWarner) {
aoqi@0 922 MethodCheckDiag methodDiag = varargsCheck ?
aoqi@0 923 MethodCheckDiag.VARARG_MISMATCH : MethodCheckDiag.ARG_MISMATCH;
aoqi@0 924
aoqi@0 925 @Override
aoqi@0 926 public boolean compatible(Type found, Type req, Warner warn) {
aoqi@0 927 found = pendingInferenceContext.asUndetVar(found);
aoqi@0 928 if (found.hasTag(UNDETVAR) && req.isPrimitive()) {
aoqi@0 929 req = types.boxedClass(req).type;
aoqi@0 930 }
aoqi@0 931 return super.compatible(found, req, warn);
aoqi@0 932 }
aoqi@0 933
aoqi@0 934 @Override
aoqi@0 935 public void report(DiagnosticPosition pos, JCDiagnostic details) {
aoqi@0 936 reportMC(pos, methodDiag, deferredAttrContext.inferenceContext, details);
aoqi@0 937 }
aoqi@0 938 };
aoqi@0 939 return new MethodResultInfo(to, checkContext);
aoqi@0 940 }
aoqi@0 941
aoqi@0 942 @Override
aoqi@0 943 public MethodCheck mostSpecificCheck(List<Type> actuals, boolean strict) {
aoqi@0 944 return new MostSpecificCheck(strict, actuals);
aoqi@0 945 }
aoqi@0 946 };
aoqi@0 947
aoqi@0 948 /**
aoqi@0 949 * Check context to be used during method applicability checks. A method check
aoqi@0 950 * context might contain inference variables.
aoqi@0 951 */
aoqi@0 952 abstract class MethodCheckContext implements CheckContext {
aoqi@0 953
aoqi@0 954 boolean strict;
aoqi@0 955 DeferredAttrContext deferredAttrContext;
aoqi@0 956 Warner rsWarner;
aoqi@0 957
aoqi@0 958 public MethodCheckContext(boolean strict, DeferredAttrContext deferredAttrContext, Warner rsWarner) {
aoqi@0 959 this.strict = strict;
aoqi@0 960 this.deferredAttrContext = deferredAttrContext;
aoqi@0 961 this.rsWarner = rsWarner;
aoqi@0 962 }
aoqi@0 963
aoqi@0 964 public boolean compatible(Type found, Type req, Warner warn) {
vromero@2543 965 InferenceContext inferenceContext = deferredAttrContext.inferenceContext;
aoqi@0 966 return strict ?
vromero@2543 967 types.isSubtypeUnchecked(inferenceContext.asUndetVar(found), inferenceContext.asUndetVar(req), warn) :
vromero@2543 968 types.isConvertible(inferenceContext.asUndetVar(found), inferenceContext.asUndetVar(req), warn);
aoqi@0 969 }
aoqi@0 970
aoqi@0 971 public void report(DiagnosticPosition pos, JCDiagnostic details) {
aoqi@0 972 throw inapplicableMethodException.setMessage(details);
aoqi@0 973 }
aoqi@0 974
aoqi@0 975 public Warner checkWarner(DiagnosticPosition pos, Type found, Type req) {
aoqi@0 976 return rsWarner;
aoqi@0 977 }
aoqi@0 978
aoqi@0 979 public InferenceContext inferenceContext() {
aoqi@0 980 return deferredAttrContext.inferenceContext;
aoqi@0 981 }
aoqi@0 982
aoqi@0 983 public DeferredAttrContext deferredAttrContext() {
aoqi@0 984 return deferredAttrContext;
aoqi@0 985 }
aoqi@0 986
aoqi@0 987 @Override
aoqi@0 988 public String toString() {
aoqi@0 989 return "MethodReferenceCheck";
aoqi@0 990 }
aoqi@0 991
aoqi@0 992 }
aoqi@0 993
aoqi@0 994 /**
aoqi@0 995 * ResultInfo class to be used during method applicability checks. Check
aoqi@0 996 * for deferred types goes through special path.
aoqi@0 997 */
aoqi@0 998 class MethodResultInfo extends ResultInfo {
aoqi@0 999
aoqi@0 1000 public MethodResultInfo(Type pt, CheckContext checkContext) {
aoqi@0 1001 attr.super(VAL, pt, checkContext);
aoqi@0 1002 }
aoqi@0 1003
aoqi@0 1004 @Override
aoqi@0 1005 protected Type check(DiagnosticPosition pos, Type found) {
aoqi@0 1006 if (found.hasTag(DEFERRED)) {
aoqi@0 1007 DeferredType dt = (DeferredType)found;
aoqi@0 1008 return dt.check(this);
aoqi@0 1009 } else {
rpatil@3092 1010 Type uResult = U(found);
aoqi@0 1011 Type capturedType = pos == null || pos.getTree() == null ?
aoqi@0 1012 types.capture(uResult) :
aoqi@0 1013 checkContext.inferenceContext()
aoqi@0 1014 .cachedCapture(pos.getTree(), uResult, true);
aoqi@0 1015 return super.check(pos, chk.checkNonVoid(pos, capturedType));
aoqi@0 1016 }
aoqi@0 1017 }
aoqi@0 1018
aoqi@0 1019 /**
aoqi@0 1020 * javac has a long-standing 'simplification' (see 6391995):
aoqi@0 1021 * given an actual argument type, the method check is performed
aoqi@0 1022 * on its upper bound. This leads to inconsistencies when an
aoqi@0 1023 * argument type is checked against itself. For example, given
aoqi@0 1024 * a type-variable T, it is not true that {@code U(T) <: T},
aoqi@0 1025 * so we need to guard against that.
aoqi@0 1026 */
aoqi@0 1027 private Type U(Type found) {
aoqi@0 1028 return found == pt ?
aoqi@0 1029 found : types.cvarUpperBound(found);
aoqi@0 1030 }
aoqi@0 1031
aoqi@0 1032 @Override
aoqi@0 1033 protected MethodResultInfo dup(Type newPt) {
aoqi@0 1034 return new MethodResultInfo(newPt, checkContext);
aoqi@0 1035 }
aoqi@0 1036
aoqi@0 1037 @Override
aoqi@0 1038 protected ResultInfo dup(CheckContext newContext) {
aoqi@0 1039 return new MethodResultInfo(pt, newContext);
aoqi@0 1040 }
aoqi@0 1041 }
aoqi@0 1042
aoqi@0 1043 /**
aoqi@0 1044 * Most specific method applicability routine. Given a list of actual types A,
aoqi@0 1045 * a list of formal types F1, and a list of formal types F2, the routine determines
aoqi@0 1046 * as to whether the types in F1 can be considered more specific than those in F2 w.r.t.
aoqi@0 1047 * argument types A.
aoqi@0 1048 */
aoqi@0 1049 class MostSpecificCheck implements MethodCheck {
aoqi@0 1050
aoqi@0 1051 boolean strict;
aoqi@0 1052 List<Type> actuals;
aoqi@0 1053
aoqi@0 1054 MostSpecificCheck(boolean strict, List<Type> actuals) {
aoqi@0 1055 this.strict = strict;
aoqi@0 1056 this.actuals = actuals;
aoqi@0 1057 }
aoqi@0 1058
aoqi@0 1059 @Override
aoqi@0 1060 public void argumentsAcceptable(final Env<AttrContext> env,
aoqi@0 1061 DeferredAttrContext deferredAttrContext,
aoqi@0 1062 List<Type> formals1,
aoqi@0 1063 List<Type> formals2,
aoqi@0 1064 Warner warn) {
aoqi@0 1065 formals2 = adjustArgs(formals2, deferredAttrContext.msym, formals1.length(), deferredAttrContext.phase.isVarargsRequired());
aoqi@0 1066 while (formals2.nonEmpty()) {
aoqi@0 1067 ResultInfo mresult = methodCheckResult(formals2.head, deferredAttrContext, warn, actuals.head);
aoqi@0 1068 mresult.check(null, formals1.head);
aoqi@0 1069 formals1 = formals1.tail;
aoqi@0 1070 formals2 = formals2.tail;
aoqi@0 1071 actuals = actuals.isEmpty() ? actuals : actuals.tail;
aoqi@0 1072 }
aoqi@0 1073 }
aoqi@0 1074
aoqi@0 1075 /**
aoqi@0 1076 * Create a method check context to be used during the most specific applicability check
aoqi@0 1077 */
aoqi@0 1078 ResultInfo methodCheckResult(Type to, DeferredAttr.DeferredAttrContext deferredAttrContext,
aoqi@0 1079 Warner rsWarner, Type actual) {
aoqi@0 1080 return attr.new ResultInfo(Kinds.VAL, to,
aoqi@0 1081 new MostSpecificCheckContext(strict, deferredAttrContext, rsWarner, actual));
aoqi@0 1082 }
aoqi@0 1083
aoqi@0 1084 /**
aoqi@0 1085 * Subclass of method check context class that implements most specific
aoqi@0 1086 * method conversion. If the actual type under analysis is a deferred type
aoqi@0 1087 * a full blown structural analysis is carried out.
aoqi@0 1088 */
aoqi@0 1089 class MostSpecificCheckContext extends MethodCheckContext {
aoqi@0 1090
aoqi@0 1091 Type actual;
aoqi@0 1092
aoqi@0 1093 public MostSpecificCheckContext(boolean strict, DeferredAttrContext deferredAttrContext, Warner rsWarner, Type actual) {
aoqi@0 1094 super(strict, deferredAttrContext, rsWarner);
aoqi@0 1095 this.actual = actual;
aoqi@0 1096 }
aoqi@0 1097
aoqi@0 1098 public boolean compatible(Type found, Type req, Warner warn) {
aoqi@0 1099 if (allowFunctionalInterfaceMostSpecific &&
aoqi@0 1100 unrelatedFunctionalInterfaces(found, req) &&
aoqi@0 1101 (actual != null && actual.getTag() == DEFERRED)) {
aoqi@0 1102 DeferredType dt = (DeferredType) actual;
aoqi@0 1103 DeferredType.SpeculativeCache.Entry e =
aoqi@0 1104 dt.speculativeCache.get(deferredAttrContext.msym, deferredAttrContext.phase);
aoqi@0 1105 if (e != null && e.speculativeTree != deferredAttr.stuckTree) {
aoqi@0 1106 return functionalInterfaceMostSpecific(found, req, e.speculativeTree, warn);
aoqi@0 1107 }
aoqi@0 1108 }
aoqi@0 1109 return super.compatible(found, req, warn);
aoqi@0 1110 }
aoqi@0 1111
aoqi@0 1112 /** Whether {@code t} and {@code s} are unrelated functional interface types. */
aoqi@0 1113 private boolean unrelatedFunctionalInterfaces(Type t, Type s) {
aoqi@0 1114 return types.isFunctionalInterface(t.tsym) &&
aoqi@0 1115 types.isFunctionalInterface(s.tsym) &&
aoqi@0 1116 types.asSuper(t, s.tsym) == null &&
aoqi@0 1117 types.asSuper(s, t.tsym) == null;
aoqi@0 1118 }
aoqi@0 1119
aoqi@0 1120 /** Parameters {@code t} and {@code s} are unrelated functional interface types. */
aoqi@0 1121 private boolean functionalInterfaceMostSpecific(Type t, Type s, JCTree tree, Warner warn) {
aoqi@0 1122 FunctionalInterfaceMostSpecificChecker msc = new FunctionalInterfaceMostSpecificChecker(t, s, warn);
aoqi@0 1123 msc.scan(tree);
aoqi@0 1124 return msc.result;
aoqi@0 1125 }
aoqi@0 1126
aoqi@0 1127 /**
aoqi@0 1128 * Tests whether one functional interface type can be considered more specific
aoqi@0 1129 * than another unrelated functional interface type for the scanned expression.
aoqi@0 1130 */
aoqi@0 1131 class FunctionalInterfaceMostSpecificChecker extends DeferredAttr.PolyScanner {
aoqi@0 1132
aoqi@0 1133 final Type t;
aoqi@0 1134 final Type s;
aoqi@0 1135 final Warner warn;
aoqi@0 1136 boolean result;
aoqi@0 1137
aoqi@0 1138 /** Parameters {@code t} and {@code s} are unrelated functional interface types. */
aoqi@0 1139 FunctionalInterfaceMostSpecificChecker(Type t, Type s, Warner warn) {
aoqi@0 1140 this.t = t;
aoqi@0 1141 this.s = s;
aoqi@0 1142 this.warn = warn;
aoqi@0 1143 result = true;
aoqi@0 1144 }
aoqi@0 1145
aoqi@0 1146 @Override
aoqi@0 1147 void skip(JCTree tree) {
aoqi@0 1148 result &= false;
aoqi@0 1149 }
aoqi@0 1150
aoqi@0 1151 @Override
aoqi@0 1152 public void visitConditional(JCConditional tree) {
aoqi@0 1153 scan(tree.truepart);
aoqi@0 1154 scan(tree.falsepart);
aoqi@0 1155 }
aoqi@0 1156
aoqi@0 1157 @Override
aoqi@0 1158 public void visitReference(JCMemberReference tree) {
aoqi@0 1159 Type desc_t = types.findDescriptorType(t);
aoqi@0 1160 Type desc_s = types.findDescriptorType(s);
aoqi@0 1161 // use inference variables here for more-specific inference (18.5.4)
aoqi@0 1162 if (!types.isSameTypes(desc_t.getParameterTypes(),
aoqi@0 1163 inferenceContext().asUndetVars(desc_s.getParameterTypes()))) {
aoqi@0 1164 result &= false;
aoqi@0 1165 } else {
aoqi@0 1166 // compare return types
aoqi@0 1167 Type ret_t = desc_t.getReturnType();
aoqi@0 1168 Type ret_s = desc_s.getReturnType();
aoqi@0 1169 if (ret_s.hasTag(VOID)) {
aoqi@0 1170 result &= true;
aoqi@0 1171 } else if (ret_t.hasTag(VOID)) {
aoqi@0 1172 result &= false;
aoqi@0 1173 } else if (ret_t.isPrimitive() != ret_s.isPrimitive()) {
aoqi@0 1174 boolean retValIsPrimitive =
aoqi@0 1175 tree.refPolyKind == PolyKind.STANDALONE &&
aoqi@0 1176 tree.sym.type.getReturnType().isPrimitive();
aoqi@0 1177 result &= (retValIsPrimitive == ret_t.isPrimitive()) &&
aoqi@0 1178 (retValIsPrimitive != ret_s.isPrimitive());
aoqi@0 1179 } else {
aoqi@0 1180 result &= MostSpecificCheckContext.super.compatible(ret_t, ret_s, warn);
aoqi@0 1181 }
aoqi@0 1182 }
aoqi@0 1183 }
aoqi@0 1184
aoqi@0 1185 @Override
aoqi@0 1186 public void visitLambda(JCLambda tree) {
aoqi@0 1187 Type desc_t = types.findDescriptorType(t);
aoqi@0 1188 Type desc_s = types.findDescriptorType(s);
aoqi@0 1189 // use inference variables here for more-specific inference (18.5.4)
aoqi@0 1190 if (!types.isSameTypes(desc_t.getParameterTypes(),
aoqi@0 1191 inferenceContext().asUndetVars(desc_s.getParameterTypes()))) {
aoqi@0 1192 result &= false;
aoqi@0 1193 } else {
aoqi@0 1194 // compare return types
aoqi@0 1195 Type ret_t = desc_t.getReturnType();
aoqi@0 1196 Type ret_s = desc_s.getReturnType();
aoqi@0 1197 if (ret_s.hasTag(VOID)) {
aoqi@0 1198 result &= true;
aoqi@0 1199 } else if (ret_t.hasTag(VOID)) {
aoqi@0 1200 result &= false;
aoqi@0 1201 } else if (unrelatedFunctionalInterfaces(ret_t, ret_s)) {
aoqi@0 1202 for (JCExpression expr : lambdaResults(tree)) {
aoqi@0 1203 result &= functionalInterfaceMostSpecific(ret_t, ret_s, expr, warn);
aoqi@0 1204 }
aoqi@0 1205 } else if (ret_t.isPrimitive() != ret_s.isPrimitive()) {
aoqi@0 1206 for (JCExpression expr : lambdaResults(tree)) {
aoqi@0 1207 boolean retValIsPrimitive = expr.isStandalone() && expr.type.isPrimitive();
aoqi@0 1208 result &= (retValIsPrimitive == ret_t.isPrimitive()) &&
aoqi@0 1209 (retValIsPrimitive != ret_s.isPrimitive());
aoqi@0 1210 }
aoqi@0 1211 } else {
aoqi@0 1212 result &= MostSpecificCheckContext.super.compatible(ret_t, ret_s, warn);
aoqi@0 1213 }
aoqi@0 1214 }
aoqi@0 1215 }
aoqi@0 1216 //where
aoqi@0 1217
aoqi@0 1218 private List<JCExpression> lambdaResults(JCLambda lambda) {
aoqi@0 1219 if (lambda.getBodyKind() == JCTree.JCLambda.BodyKind.EXPRESSION) {
aoqi@0 1220 return List.of((JCExpression) lambda.body);
aoqi@0 1221 } else {
aoqi@0 1222 final ListBuffer<JCExpression> buffer = new ListBuffer<>();
aoqi@0 1223 DeferredAttr.LambdaReturnScanner lambdaScanner =
aoqi@0 1224 new DeferredAttr.LambdaReturnScanner() {
aoqi@0 1225 @Override
aoqi@0 1226 public void visitReturn(JCReturn tree) {
aoqi@0 1227 if (tree.expr != null) {
aoqi@0 1228 buffer.append(tree.expr);
aoqi@0 1229 }
aoqi@0 1230 }
aoqi@0 1231 };
aoqi@0 1232 lambdaScanner.scan(lambda.body);
aoqi@0 1233 return buffer.toList();
aoqi@0 1234 }
aoqi@0 1235 }
aoqi@0 1236 }
aoqi@0 1237
aoqi@0 1238 }
aoqi@0 1239
aoqi@0 1240 public MethodCheck mostSpecificCheck(List<Type> actuals, boolean strict) {
aoqi@0 1241 Assert.error("Cannot get here!");
aoqi@0 1242 return null;
aoqi@0 1243 }
aoqi@0 1244 }
aoqi@0 1245
aoqi@0 1246 public static class InapplicableMethodException extends RuntimeException {
aoqi@0 1247 private static final long serialVersionUID = 0;
aoqi@0 1248
aoqi@0 1249 JCDiagnostic diagnostic;
aoqi@0 1250 JCDiagnostic.Factory diags;
aoqi@0 1251
aoqi@0 1252 InapplicableMethodException(JCDiagnostic.Factory diags) {
aoqi@0 1253 this.diagnostic = null;
aoqi@0 1254 this.diags = diags;
aoqi@0 1255 }
aoqi@0 1256 InapplicableMethodException setMessage() {
aoqi@0 1257 return setMessage((JCDiagnostic)null);
aoqi@0 1258 }
aoqi@0 1259 InapplicableMethodException setMessage(String key) {
aoqi@0 1260 return setMessage(key != null ? diags.fragment(key) : null);
aoqi@0 1261 }
aoqi@0 1262 InapplicableMethodException setMessage(String key, Object... args) {
aoqi@0 1263 return setMessage(key != null ? diags.fragment(key, args) : null);
aoqi@0 1264 }
aoqi@0 1265 InapplicableMethodException setMessage(JCDiagnostic diag) {
aoqi@0 1266 this.diagnostic = diag;
aoqi@0 1267 return this;
aoqi@0 1268 }
aoqi@0 1269
aoqi@0 1270 public JCDiagnostic getDiagnostic() {
aoqi@0 1271 return diagnostic;
aoqi@0 1272 }
aoqi@0 1273 }
aoqi@0 1274 private final InapplicableMethodException inapplicableMethodException;
aoqi@0 1275
aoqi@0 1276 /* ***************************************************************************
aoqi@0 1277 * Symbol lookup
aoqi@0 1278 * the following naming conventions for arguments are used
aoqi@0 1279 *
aoqi@0 1280 * env is the environment where the symbol was mentioned
aoqi@0 1281 * site is the type of which the symbol is a member
aoqi@0 1282 * name is the symbol's name
aoqi@0 1283 * if no arguments are given
aoqi@0 1284 * argtypes are the value arguments, if we search for a method
aoqi@0 1285 *
aoqi@0 1286 * If no symbol was found, a ResolveError detailing the problem is returned.
aoqi@0 1287 ****************************************************************************/
aoqi@0 1288
aoqi@0 1289 /** Find field. Synthetic fields are always skipped.
aoqi@0 1290 * @param env The current environment.
aoqi@0 1291 * @param site The original type from where the selection takes place.
aoqi@0 1292 * @param name The name of the field.
aoqi@0 1293 * @param c The class to search for the field. This is always
aoqi@0 1294 * a superclass or implemented interface of site's class.
aoqi@0 1295 */
aoqi@0 1296 Symbol findField(Env<AttrContext> env,
aoqi@0 1297 Type site,
aoqi@0 1298 Name name,
aoqi@0 1299 TypeSymbol c) {
aoqi@0 1300 while (c.type.hasTag(TYPEVAR))
aoqi@0 1301 c = c.type.getUpperBound().tsym;
aoqi@0 1302 Symbol bestSoFar = varNotFound;
aoqi@0 1303 Symbol sym;
aoqi@0 1304 Scope.Entry e = c.members().lookup(name);
aoqi@0 1305 while (e.scope != null) {
aoqi@0 1306 if (e.sym.kind == VAR && (e.sym.flags_field & SYNTHETIC) == 0) {
aoqi@0 1307 return isAccessible(env, site, e.sym)
aoqi@0 1308 ? e.sym : new AccessError(env, site, e.sym);
aoqi@0 1309 }
aoqi@0 1310 e = e.next();
aoqi@0 1311 }
aoqi@0 1312 Type st = types.supertype(c.type);
aoqi@0 1313 if (st != null && (st.hasTag(CLASS) || st.hasTag(TYPEVAR))) {
aoqi@0 1314 sym = findField(env, site, name, st.tsym);
aoqi@0 1315 if (sym.kind < bestSoFar.kind) bestSoFar = sym;
aoqi@0 1316 }
aoqi@0 1317 for (List<Type> l = types.interfaces(c.type);
aoqi@0 1318 bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
aoqi@0 1319 l = l.tail) {
aoqi@0 1320 sym = findField(env, site, name, l.head.tsym);
aoqi@0 1321 if (bestSoFar.exists() && sym.exists() &&
aoqi@0 1322 sym.owner != bestSoFar.owner)
aoqi@0 1323 bestSoFar = new AmbiguityError(bestSoFar, sym);
aoqi@0 1324 else if (sym.kind < bestSoFar.kind)
aoqi@0 1325 bestSoFar = sym;
aoqi@0 1326 }
aoqi@0 1327 return bestSoFar;
aoqi@0 1328 }
aoqi@0 1329
aoqi@0 1330 /** Resolve a field identifier, throw a fatal error if not found.
aoqi@0 1331 * @param pos The position to use for error reporting.
aoqi@0 1332 * @param env The environment current at the method invocation.
aoqi@0 1333 * @param site The type of the qualifying expression, in which
aoqi@0 1334 * identifier is searched.
aoqi@0 1335 * @param name The identifier's name.
aoqi@0 1336 */
aoqi@0 1337 public VarSymbol resolveInternalField(DiagnosticPosition pos, Env<AttrContext> env,
aoqi@0 1338 Type site, Name name) {
aoqi@0 1339 Symbol sym = findField(env, site, name, site.tsym);
aoqi@0 1340 if (sym.kind == VAR) return (VarSymbol)sym;
aoqi@0 1341 else throw new FatalError(
aoqi@0 1342 diags.fragment("fatal.err.cant.locate.field",
aoqi@0 1343 name));
aoqi@0 1344 }
aoqi@0 1345
aoqi@0 1346 /** Find unqualified variable or field with given name.
aoqi@0 1347 * Synthetic fields always skipped.
aoqi@0 1348 * @param env The current environment.
aoqi@0 1349 * @param name The name of the variable or field.
aoqi@0 1350 */
aoqi@0 1351 Symbol findVar(Env<AttrContext> env, Name name) {
aoqi@0 1352 Symbol bestSoFar = varNotFound;
aoqi@0 1353 Symbol sym;
aoqi@0 1354 Env<AttrContext> env1 = env;
aoqi@0 1355 boolean staticOnly = false;
aoqi@0 1356 while (env1.outer != null) {
aoqi@0 1357 if (isStatic(env1)) staticOnly = true;
aoqi@0 1358 Scope.Entry e = env1.info.scope.lookup(name);
aoqi@0 1359 while (e.scope != null &&
aoqi@0 1360 (e.sym.kind != VAR ||
aoqi@0 1361 (e.sym.flags_field & SYNTHETIC) != 0))
aoqi@0 1362 e = e.next();
aoqi@0 1363 sym = (e.scope != null)
aoqi@0 1364 ? e.sym
aoqi@0 1365 : findField(
aoqi@0 1366 env1, env1.enclClass.sym.type, name, env1.enclClass.sym);
aoqi@0 1367 if (sym.exists()) {
aoqi@0 1368 if (staticOnly &&
aoqi@0 1369 sym.kind == VAR &&
aoqi@0 1370 sym.owner.kind == TYP &&
aoqi@0 1371 (sym.flags() & STATIC) == 0)
aoqi@0 1372 return new StaticError(sym);
aoqi@0 1373 else
aoqi@0 1374 return sym;
aoqi@0 1375 } else if (sym.kind < bestSoFar.kind) {
aoqi@0 1376 bestSoFar = sym;
aoqi@0 1377 }
aoqi@0 1378
aoqi@0 1379 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
aoqi@0 1380 env1 = env1.outer;
aoqi@0 1381 }
aoqi@0 1382
aoqi@0 1383 sym = findField(env, syms.predefClass.type, name, syms.predefClass);
aoqi@0 1384 if (sym.exists())
aoqi@0 1385 return sym;
aoqi@0 1386 if (bestSoFar.exists())
aoqi@0 1387 return bestSoFar;
aoqi@0 1388
aoqi@0 1389 Symbol origin = null;
aoqi@0 1390 for (Scope sc : new Scope[] { env.toplevel.namedImportScope, env.toplevel.starImportScope }) {
aoqi@0 1391 Scope.Entry e = sc.lookup(name);
aoqi@0 1392 for (; e.scope != null; e = e.next()) {
aoqi@0 1393 sym = e.sym;
aoqi@0 1394 if (sym.kind != VAR)
aoqi@0 1395 continue;
aoqi@0 1396 // invariant: sym.kind == VAR
aoqi@0 1397 if (bestSoFar.kind < AMBIGUOUS && sym.owner != bestSoFar.owner)
aoqi@0 1398 return new AmbiguityError(bestSoFar, sym);
aoqi@0 1399 else if (bestSoFar.kind >= VAR) {
aoqi@0 1400 origin = e.getOrigin().owner;
aoqi@0 1401 bestSoFar = isAccessible(env, origin.type, sym)
aoqi@0 1402 ? sym : new AccessError(env, origin.type, sym);
aoqi@0 1403 }
aoqi@0 1404 }
aoqi@0 1405 if (bestSoFar.exists()) break;
aoqi@0 1406 }
aoqi@0 1407 if (bestSoFar.kind == VAR && bestSoFar.owner.type != origin.type)
aoqi@0 1408 return bestSoFar.clone(origin);
aoqi@0 1409 else
aoqi@0 1410 return bestSoFar;
aoqi@0 1411 }
aoqi@0 1412
aoqi@0 1413 Warner noteWarner = new Warner();
aoqi@0 1414
aoqi@0 1415 /** Select the best method for a call site among two choices.
aoqi@0 1416 * @param env The current environment.
aoqi@0 1417 * @param site The original type from where the
aoqi@0 1418 * selection takes place.
aoqi@0 1419 * @param argtypes The invocation's value arguments,
aoqi@0 1420 * @param typeargtypes The invocation's type arguments,
aoqi@0 1421 * @param sym Proposed new best match.
aoqi@0 1422 * @param bestSoFar Previously found best match.
aoqi@0 1423 * @param allowBoxing Allow boxing conversions of arguments.
aoqi@0 1424 * @param useVarargs Box trailing arguments into an array for varargs.
aoqi@0 1425 */
aoqi@0 1426 @SuppressWarnings("fallthrough")
aoqi@0 1427 Symbol selectBest(Env<AttrContext> env,
aoqi@0 1428 Type site,
aoqi@0 1429 List<Type> argtypes,
aoqi@0 1430 List<Type> typeargtypes,
aoqi@0 1431 Symbol sym,
aoqi@0 1432 Symbol bestSoFar,
aoqi@0 1433 boolean allowBoxing,
aoqi@0 1434 boolean useVarargs,
aoqi@0 1435 boolean operator) {
aoqi@0 1436 if (sym.kind == ERR ||
aoqi@0 1437 !sym.isInheritedIn(site.tsym, types)) {
aoqi@0 1438 return bestSoFar;
aoqi@0 1439 } else if (useVarargs && (sym.flags() & VARARGS) == 0) {
aoqi@0 1440 return bestSoFar.kind >= ERRONEOUS ?
aoqi@0 1441 new BadVarargsMethod((ResolveError)bestSoFar.baseSymbol()) :
aoqi@0 1442 bestSoFar;
aoqi@0 1443 }
aoqi@0 1444 Assert.check(sym.kind < AMBIGUOUS);
aoqi@0 1445 try {
aoqi@0 1446 Type mt = rawInstantiate(env, site, sym, null, argtypes, typeargtypes,
aoqi@0 1447 allowBoxing, useVarargs, types.noWarnings);
aoqi@0 1448 if (!operator || verboseResolutionMode.contains(VerboseResolutionMode.PREDEF))
aoqi@0 1449 currentResolutionContext.addApplicableCandidate(sym, mt);
aoqi@0 1450 } catch (InapplicableMethodException ex) {
aoqi@0 1451 if (!operator)
aoqi@0 1452 currentResolutionContext.addInapplicableCandidate(sym, ex.getDiagnostic());
aoqi@0 1453 switch (bestSoFar.kind) {
aoqi@0 1454 case ABSENT_MTH:
aoqi@0 1455 return new InapplicableSymbolError(currentResolutionContext);
aoqi@0 1456 case WRONG_MTH:
aoqi@0 1457 if (operator) return bestSoFar;
aoqi@0 1458 bestSoFar = new InapplicableSymbolsError(currentResolutionContext);
aoqi@0 1459 default:
aoqi@0 1460 return bestSoFar;
aoqi@0 1461 }
aoqi@0 1462 }
aoqi@0 1463 if (!isAccessible(env, site, sym)) {
aoqi@0 1464 return (bestSoFar.kind == ABSENT_MTH)
aoqi@0 1465 ? new AccessError(env, site, sym)
aoqi@0 1466 : bestSoFar;
aoqi@0 1467 }
aoqi@0 1468 return (bestSoFar.kind > AMBIGUOUS)
aoqi@0 1469 ? sym
aoqi@0 1470 : mostSpecific(argtypes, sym, bestSoFar, env, site,
aoqi@0 1471 allowBoxing && operator, useVarargs);
aoqi@0 1472 }
aoqi@0 1473
aoqi@0 1474 /* Return the most specific of the two methods for a call,
aoqi@0 1475 * given that both are accessible and applicable.
aoqi@0 1476 * @param m1 A new candidate for most specific.
aoqi@0 1477 * @param m2 The previous most specific candidate.
aoqi@0 1478 * @param env The current environment.
aoqi@0 1479 * @param site The original type from where the selection
aoqi@0 1480 * takes place.
aoqi@0 1481 * @param allowBoxing Allow boxing conversions of arguments.
aoqi@0 1482 * @param useVarargs Box trailing arguments into an array for varargs.
aoqi@0 1483 */
aoqi@0 1484 Symbol mostSpecific(List<Type> argtypes, Symbol m1,
aoqi@0 1485 Symbol m2,
aoqi@0 1486 Env<AttrContext> env,
aoqi@0 1487 final Type site,
aoqi@0 1488 boolean allowBoxing,
aoqi@0 1489 boolean useVarargs) {
aoqi@0 1490 switch (m2.kind) {
aoqi@0 1491 case MTH:
aoqi@0 1492 if (m1 == m2) return m1;
aoqi@0 1493 boolean m1SignatureMoreSpecific =
aoqi@0 1494 signatureMoreSpecific(argtypes, env, site, m1, m2, allowBoxing, useVarargs);
aoqi@0 1495 boolean m2SignatureMoreSpecific =
aoqi@0 1496 signatureMoreSpecific(argtypes, env, site, m2, m1, allowBoxing, useVarargs);
aoqi@0 1497 if (m1SignatureMoreSpecific && m2SignatureMoreSpecific) {
aoqi@0 1498 Type mt1 = types.memberType(site, m1);
aoqi@0 1499 Type mt2 = types.memberType(site, m2);
aoqi@0 1500 if (!types.overrideEquivalent(mt1, mt2))
aoqi@0 1501 return ambiguityError(m1, m2);
aoqi@0 1502
aoqi@0 1503 // same signature; select (a) the non-bridge method, or
aoqi@0 1504 // (b) the one that overrides the other, or (c) the concrete
aoqi@0 1505 // one, or (d) merge both abstract signatures
aoqi@0 1506 if ((m1.flags() & BRIDGE) != (m2.flags() & BRIDGE))
aoqi@0 1507 return ((m1.flags() & BRIDGE) != 0) ? m2 : m1;
aoqi@0 1508
aoqi@0 1509 // if one overrides or hides the other, use it
aoqi@0 1510 TypeSymbol m1Owner = (TypeSymbol)m1.owner;
aoqi@0 1511 TypeSymbol m2Owner = (TypeSymbol)m2.owner;
aoqi@0 1512 if (types.asSuper(m1Owner.type, m2Owner) != null &&
aoqi@0 1513 ((m1.owner.flags_field & INTERFACE) == 0 ||
aoqi@0 1514 (m2.owner.flags_field & INTERFACE) != 0) &&
aoqi@0 1515 m1.overrides(m2, m1Owner, types, false))
aoqi@0 1516 return m1;
aoqi@0 1517 if (types.asSuper(m2Owner.type, m1Owner) != null &&
aoqi@0 1518 ((m2.owner.flags_field & INTERFACE) == 0 ||
aoqi@0 1519 (m1.owner.flags_field & INTERFACE) != 0) &&
aoqi@0 1520 m2.overrides(m1, m2Owner, types, false))
aoqi@0 1521 return m2;
aoqi@0 1522 boolean m1Abstract = (m1.flags() & ABSTRACT) != 0;
aoqi@0 1523 boolean m2Abstract = (m2.flags() & ABSTRACT) != 0;
aoqi@0 1524 if (m1Abstract && !m2Abstract) return m2;
aoqi@0 1525 if (m2Abstract && !m1Abstract) return m1;
aoqi@0 1526 // both abstract or both concrete
aoqi@0 1527 return ambiguityError(m1, m2);
aoqi@0 1528 }
aoqi@0 1529 if (m1SignatureMoreSpecific) return m1;
aoqi@0 1530 if (m2SignatureMoreSpecific) return m2;
aoqi@0 1531 return ambiguityError(m1, m2);
aoqi@0 1532 case AMBIGUOUS:
aoqi@0 1533 //compare m1 to ambiguous methods in m2
aoqi@0 1534 AmbiguityError e = (AmbiguityError)m2.baseSymbol();
aoqi@0 1535 boolean m1MoreSpecificThanAnyAmbiguous = true;
aoqi@0 1536 boolean allAmbiguousMoreSpecificThanM1 = true;
aoqi@0 1537 for (Symbol s : e.ambiguousSyms) {
aoqi@0 1538 Symbol moreSpecific = mostSpecific(argtypes, m1, s, env, site, allowBoxing, useVarargs);
aoqi@0 1539 m1MoreSpecificThanAnyAmbiguous &= moreSpecific == m1;
aoqi@0 1540 allAmbiguousMoreSpecificThanM1 &= moreSpecific == s;
aoqi@0 1541 }
aoqi@0 1542 if (m1MoreSpecificThanAnyAmbiguous)
aoqi@0 1543 return m1;
aoqi@0 1544 //if m1 is more specific than some ambiguous methods, but other ambiguous methods are
aoqi@0 1545 //more specific than m1, add it as a new ambiguous method:
aoqi@0 1546 if (!allAmbiguousMoreSpecificThanM1)
aoqi@0 1547 e.addAmbiguousSymbol(m1);
aoqi@0 1548 return e;
aoqi@0 1549 default:
aoqi@0 1550 throw new AssertionError();
aoqi@0 1551 }
aoqi@0 1552 }
aoqi@0 1553 //where
aoqi@0 1554 private boolean signatureMoreSpecific(List<Type> actuals, Env<AttrContext> env, Type site, Symbol m1, Symbol m2, boolean allowBoxing, boolean useVarargs) {
aoqi@0 1555 noteWarner.clear();
aoqi@0 1556 int maxLength = Math.max(
aoqi@0 1557 Math.max(m1.type.getParameterTypes().length(), actuals.length()),
aoqi@0 1558 m2.type.getParameterTypes().length());
aoqi@0 1559 MethodResolutionContext prevResolutionContext = currentResolutionContext;
aoqi@0 1560 try {
aoqi@0 1561 currentResolutionContext = new MethodResolutionContext();
aoqi@0 1562 currentResolutionContext.step = prevResolutionContext.step;
aoqi@0 1563 currentResolutionContext.methodCheck =
aoqi@0 1564 prevResolutionContext.methodCheck.mostSpecificCheck(actuals, !allowBoxing);
aoqi@0 1565 Type mst = instantiate(env, site, m2, null,
aoqi@0 1566 adjustArgs(types.cvarLowerBounds(types.memberType(site, m1).getParameterTypes()), m1, maxLength, useVarargs), null,
aoqi@0 1567 allowBoxing, useVarargs, noteWarner);
aoqi@0 1568 return mst != null &&
aoqi@0 1569 !noteWarner.hasLint(Lint.LintCategory.UNCHECKED);
aoqi@0 1570 } finally {
aoqi@0 1571 currentResolutionContext = prevResolutionContext;
aoqi@0 1572 }
aoqi@0 1573 }
aoqi@0 1574
aoqi@0 1575 List<Type> adjustArgs(List<Type> args, Symbol msym, int length, boolean allowVarargs) {
aoqi@0 1576 if ((msym.flags() & VARARGS) != 0 && allowVarargs) {
aoqi@0 1577 Type varargsElem = types.elemtype(args.last());
aoqi@0 1578 if (varargsElem == null) {
aoqi@0 1579 Assert.error("Bad varargs = " + args.last() + " " + msym);
aoqi@0 1580 }
aoqi@0 1581 List<Type> newArgs = args.reverse().tail.prepend(varargsElem).reverse();
aoqi@0 1582 while (newArgs.length() < length) {
aoqi@0 1583 newArgs = newArgs.append(newArgs.last());
aoqi@0 1584 }
aoqi@0 1585 return newArgs;
aoqi@0 1586 } else {
aoqi@0 1587 return args;
aoqi@0 1588 }
aoqi@0 1589 }
aoqi@0 1590 //where
aoqi@0 1591 Type mostSpecificReturnType(Type mt1, Type mt2) {
aoqi@0 1592 Type rt1 = mt1.getReturnType();
aoqi@0 1593 Type rt2 = mt2.getReturnType();
aoqi@0 1594
aoqi@0 1595 if (mt1.hasTag(FORALL) && mt2.hasTag(FORALL)) {
aoqi@0 1596 //if both are generic methods, adjust return type ahead of subtyping check
aoqi@0 1597 rt1 = types.subst(rt1, mt1.getTypeArguments(), mt2.getTypeArguments());
aoqi@0 1598 }
aoqi@0 1599 //first use subtyping, then return type substitutability
aoqi@0 1600 if (types.isSubtype(rt1, rt2)) {
aoqi@0 1601 return mt1;
aoqi@0 1602 } else if (types.isSubtype(rt2, rt1)) {
aoqi@0 1603 return mt2;
aoqi@0 1604 } else if (types.returnTypeSubstitutable(mt1, mt2)) {
aoqi@0 1605 return mt1;
aoqi@0 1606 } else if (types.returnTypeSubstitutable(mt2, mt1)) {
aoqi@0 1607 return mt2;
aoqi@0 1608 } else {
aoqi@0 1609 return null;
aoqi@0 1610 }
aoqi@0 1611 }
aoqi@0 1612 //where
aoqi@0 1613 Symbol ambiguityError(Symbol m1, Symbol m2) {
aoqi@0 1614 if (((m1.flags() | m2.flags()) & CLASH) != 0) {
aoqi@0 1615 return (m1.flags() & CLASH) == 0 ? m1 : m2;
aoqi@0 1616 } else {
aoqi@0 1617 return new AmbiguityError(m1, m2);
aoqi@0 1618 }
aoqi@0 1619 }
aoqi@0 1620
aoqi@0 1621 Symbol findMethodInScope(Env<AttrContext> env,
aoqi@0 1622 Type site,
aoqi@0 1623 Name name,
aoqi@0 1624 List<Type> argtypes,
aoqi@0 1625 List<Type> typeargtypes,
aoqi@0 1626 Scope sc,
aoqi@0 1627 Symbol bestSoFar,
aoqi@0 1628 boolean allowBoxing,
aoqi@0 1629 boolean useVarargs,
aoqi@0 1630 boolean operator,
aoqi@0 1631 boolean abstractok) {
aoqi@0 1632 for (Symbol s : sc.getElementsByName(name, new LookupFilter(abstractok))) {
aoqi@0 1633 bestSoFar = selectBest(env, site, argtypes, typeargtypes, s,
aoqi@0 1634 bestSoFar, allowBoxing, useVarargs, operator);
aoqi@0 1635 }
aoqi@0 1636 return bestSoFar;
aoqi@0 1637 }
aoqi@0 1638 //where
aoqi@0 1639 class LookupFilter implements Filter<Symbol> {
aoqi@0 1640
aoqi@0 1641 boolean abstractOk;
aoqi@0 1642
aoqi@0 1643 LookupFilter(boolean abstractOk) {
aoqi@0 1644 this.abstractOk = abstractOk;
aoqi@0 1645 }
aoqi@0 1646
aoqi@0 1647 public boolean accepts(Symbol s) {
aoqi@0 1648 long flags = s.flags();
aoqi@0 1649 return s.kind == MTH &&
aoqi@0 1650 (flags & SYNTHETIC) == 0 &&
aoqi@0 1651 (abstractOk ||
aoqi@0 1652 (flags & DEFAULT) != 0 ||
aoqi@0 1653 (flags & ABSTRACT) == 0);
aoqi@0 1654 }
aoqi@0 1655 };
aoqi@0 1656
aoqi@0 1657 /** Find best qualified method matching given name, type and value
aoqi@0 1658 * arguments.
aoqi@0 1659 * @param env The current environment.
aoqi@0 1660 * @param site The original type from where the selection
aoqi@0 1661 * takes place.
aoqi@0 1662 * @param name The method's name.
aoqi@0 1663 * @param argtypes The method's value arguments.
aoqi@0 1664 * @param typeargtypes The method's type arguments
aoqi@0 1665 * @param allowBoxing Allow boxing conversions of arguments.
aoqi@0 1666 * @param useVarargs Box trailing arguments into an array for varargs.
aoqi@0 1667 */
aoqi@0 1668 Symbol findMethod(Env<AttrContext> env,
aoqi@0 1669 Type site,
aoqi@0 1670 Name name,
aoqi@0 1671 List<Type> argtypes,
aoqi@0 1672 List<Type> typeargtypes,
aoqi@0 1673 boolean allowBoxing,
aoqi@0 1674 boolean useVarargs,
aoqi@0 1675 boolean operator) {
aoqi@0 1676 Symbol bestSoFar = methodNotFound;
aoqi@0 1677 bestSoFar = findMethod(env,
aoqi@0 1678 site,
aoqi@0 1679 name,
aoqi@0 1680 argtypes,
aoqi@0 1681 typeargtypes,
aoqi@0 1682 site.tsym.type,
aoqi@0 1683 bestSoFar,
aoqi@0 1684 allowBoxing,
aoqi@0 1685 useVarargs,
aoqi@0 1686 operator);
aoqi@0 1687 return bestSoFar;
aoqi@0 1688 }
aoqi@0 1689 // where
aoqi@0 1690 private Symbol findMethod(Env<AttrContext> env,
aoqi@0 1691 Type site,
aoqi@0 1692 Name name,
aoqi@0 1693 List<Type> argtypes,
aoqi@0 1694 List<Type> typeargtypes,
aoqi@0 1695 Type intype,
aoqi@0 1696 Symbol bestSoFar,
aoqi@0 1697 boolean allowBoxing,
aoqi@0 1698 boolean useVarargs,
aoqi@0 1699 boolean operator) {
aoqi@0 1700 @SuppressWarnings({"unchecked","rawtypes"})
aoqi@0 1701 List<Type>[] itypes = (List<Type>[])new List[] { List.<Type>nil(), List.<Type>nil() };
aoqi@0 1702 InterfaceLookupPhase iphase = InterfaceLookupPhase.ABSTRACT_OK;
aoqi@0 1703 for (TypeSymbol s : superclasses(intype)) {
aoqi@0 1704 bestSoFar = findMethodInScope(env, site, name, argtypes, typeargtypes,
aoqi@0 1705 s.members(), bestSoFar, allowBoxing, useVarargs, operator, true);
aoqi@0 1706 if (name == names.init) return bestSoFar;
aoqi@0 1707 iphase = (iphase == null) ? null : iphase.update(s, this);
aoqi@0 1708 if (iphase != null) {
aoqi@0 1709 for (Type itype : types.interfaces(s.type)) {
aoqi@0 1710 itypes[iphase.ordinal()] = types.union(types.closure(itype), itypes[iphase.ordinal()]);
aoqi@0 1711 }
aoqi@0 1712 }
aoqi@0 1713 }
aoqi@0 1714
aoqi@0 1715 Symbol concrete = bestSoFar.kind < ERR &&
aoqi@0 1716 (bestSoFar.flags() & ABSTRACT) == 0 ?
aoqi@0 1717 bestSoFar : methodNotFound;
aoqi@0 1718
aoqi@0 1719 for (InterfaceLookupPhase iphase2 : InterfaceLookupPhase.values()) {
aoqi@0 1720 //keep searching for abstract methods
aoqi@0 1721 for (Type itype : itypes[iphase2.ordinal()]) {
aoqi@0 1722 if (!itype.isInterface()) continue; //skip j.l.Object (included by Types.closure())
aoqi@0 1723 if (iphase2 == InterfaceLookupPhase.DEFAULT_OK &&
aoqi@0 1724 (itype.tsym.flags() & DEFAULT) == 0) continue;
aoqi@0 1725 bestSoFar = findMethodInScope(env, site, name, argtypes, typeargtypes,
aoqi@0 1726 itype.tsym.members(), bestSoFar, allowBoxing, useVarargs, operator, true);
aoqi@0 1727 if (concrete != bestSoFar &&
aoqi@0 1728 concrete.kind < ERR && bestSoFar.kind < ERR &&
aoqi@0 1729 types.isSubSignature(concrete.type, bestSoFar.type)) {
aoqi@0 1730 //this is an hack - as javac does not do full membership checks
aoqi@0 1731 //most specific ends up comparing abstract methods that might have
aoqi@0 1732 //been implemented by some concrete method in a subclass and,
aoqi@0 1733 //because of raw override, it is possible for an abstract method
aoqi@0 1734 //to be more specific than the concrete method - so we need
aoqi@0 1735 //to explicitly call that out (see CR 6178365)
aoqi@0 1736 bestSoFar = concrete;
aoqi@0 1737 }
aoqi@0 1738 }
aoqi@0 1739 }
aoqi@0 1740 return bestSoFar;
aoqi@0 1741 }
aoqi@0 1742
aoqi@0 1743 enum InterfaceLookupPhase {
aoqi@0 1744 ABSTRACT_OK() {
aoqi@0 1745 @Override
aoqi@0 1746 InterfaceLookupPhase update(Symbol s, Resolve rs) {
aoqi@0 1747 //We should not look for abstract methods if receiver is a concrete class
aoqi@0 1748 //(as concrete classes are expected to implement all abstracts coming
aoqi@0 1749 //from superinterfaces)
aoqi@0 1750 if ((s.flags() & (ABSTRACT | INTERFACE | ENUM)) != 0) {
aoqi@0 1751 return this;
aoqi@0 1752 } else {
aoqi@0 1753 return DEFAULT_OK;
aoqi@0 1754 }
aoqi@0 1755 }
aoqi@0 1756 },
aoqi@0 1757 DEFAULT_OK() {
aoqi@0 1758 @Override
aoqi@0 1759 InterfaceLookupPhase update(Symbol s, Resolve rs) {
aoqi@0 1760 return this;
aoqi@0 1761 }
aoqi@0 1762 };
aoqi@0 1763
aoqi@0 1764 abstract InterfaceLookupPhase update(Symbol s, Resolve rs);
aoqi@0 1765 }
aoqi@0 1766
aoqi@0 1767 /**
aoqi@0 1768 * Return an Iterable object to scan the superclasses of a given type.
aoqi@0 1769 * It's crucial that the scan is done lazily, as we don't want to accidentally
aoqi@0 1770 * access more supertypes than strictly needed (as this could trigger completion
aoqi@0 1771 * errors if some of the not-needed supertypes are missing/ill-formed).
aoqi@0 1772 */
aoqi@0 1773 Iterable<TypeSymbol> superclasses(final Type intype) {
aoqi@0 1774 return new Iterable<TypeSymbol>() {
aoqi@0 1775 public Iterator<TypeSymbol> iterator() {
aoqi@0 1776 return new Iterator<TypeSymbol>() {
aoqi@0 1777
aoqi@0 1778 List<TypeSymbol> seen = List.nil();
aoqi@0 1779 TypeSymbol currentSym = symbolFor(intype);
aoqi@0 1780 TypeSymbol prevSym = null;
aoqi@0 1781
aoqi@0 1782 public boolean hasNext() {
aoqi@0 1783 if (currentSym == syms.noSymbol) {
aoqi@0 1784 currentSym = symbolFor(types.supertype(prevSym.type));
aoqi@0 1785 }
aoqi@0 1786 return currentSym != null;
aoqi@0 1787 }
aoqi@0 1788
aoqi@0 1789 public TypeSymbol next() {
aoqi@0 1790 prevSym = currentSym;
aoqi@0 1791 currentSym = syms.noSymbol;
aoqi@0 1792 Assert.check(prevSym != null || prevSym != syms.noSymbol);
aoqi@0 1793 return prevSym;
aoqi@0 1794 }
aoqi@0 1795
aoqi@0 1796 public void remove() {
aoqi@0 1797 throw new UnsupportedOperationException();
aoqi@0 1798 }
aoqi@0 1799
aoqi@0 1800 TypeSymbol symbolFor(Type t) {
aoqi@0 1801 if (!t.hasTag(CLASS) &&
aoqi@0 1802 !t.hasTag(TYPEVAR)) {
aoqi@0 1803 return null;
aoqi@0 1804 }
aoqi@0 1805 while (t.hasTag(TYPEVAR))
aoqi@0 1806 t = t.getUpperBound();
aoqi@0 1807 if (seen.contains(t.tsym)) {
aoqi@0 1808 //degenerate case in which we have a circular
aoqi@0 1809 //class hierarchy - because of ill-formed classfiles
aoqi@0 1810 return null;
aoqi@0 1811 }
aoqi@0 1812 seen = seen.prepend(t.tsym);
aoqi@0 1813 return t.tsym;
aoqi@0 1814 }
aoqi@0 1815 };
aoqi@0 1816 }
aoqi@0 1817 };
aoqi@0 1818 }
aoqi@0 1819
aoqi@0 1820 /** Find unqualified method matching given name, type and value arguments.
aoqi@0 1821 * @param env The current environment.
aoqi@0 1822 * @param name The method's name.
aoqi@0 1823 * @param argtypes The method's value arguments.
aoqi@0 1824 * @param typeargtypes The method's type arguments.
aoqi@0 1825 * @param allowBoxing Allow boxing conversions of arguments.
aoqi@0 1826 * @param useVarargs Box trailing arguments into an array for varargs.
aoqi@0 1827 */
aoqi@0 1828 Symbol findFun(Env<AttrContext> env, Name name,
aoqi@0 1829 List<Type> argtypes, List<Type> typeargtypes,
aoqi@0 1830 boolean allowBoxing, boolean useVarargs) {
aoqi@0 1831 Symbol bestSoFar = methodNotFound;
aoqi@0 1832 Symbol sym;
aoqi@0 1833 Env<AttrContext> env1 = env;
aoqi@0 1834 boolean staticOnly = false;
aoqi@0 1835 while (env1.outer != null) {
aoqi@0 1836 if (isStatic(env1)) staticOnly = true;
sadayapalam@3005 1837 Assert.check(env1.info.preferredTreeForDiagnostics == null);
sadayapalam@3005 1838 env1.info.preferredTreeForDiagnostics = env.tree;
sadayapalam@3005 1839 try {
sadayapalam@3005 1840 sym = findMethod(
sadayapalam@3005 1841 env1, env1.enclClass.sym.type, name, argtypes, typeargtypes,
sadayapalam@3005 1842 allowBoxing, useVarargs, false);
sadayapalam@3005 1843 if (sym.exists()) {
sadayapalam@3005 1844 if (staticOnly &&
sadayapalam@3005 1845 sym.kind == MTH &&
sadayapalam@3005 1846 sym.owner.kind == TYP &&
sadayapalam@3005 1847 (sym.flags() & STATIC) == 0) return new StaticError(sym);
sadayapalam@3005 1848 else return sym;
sadayapalam@3005 1849 } else if (sym.kind < bestSoFar.kind) {
sadayapalam@3005 1850 bestSoFar = sym;
sadayapalam@3005 1851 }
sadayapalam@3005 1852 } finally {
sadayapalam@3005 1853 env1.info.preferredTreeForDiagnostics = null;
aoqi@0 1854 }
aoqi@0 1855 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
aoqi@0 1856 env1 = env1.outer;
aoqi@0 1857 }
aoqi@0 1858
aoqi@0 1859 sym = findMethod(env, syms.predefClass.type, name, argtypes,
aoqi@0 1860 typeargtypes, allowBoxing, useVarargs, false);
aoqi@0 1861 if (sym.exists())
aoqi@0 1862 return sym;
aoqi@0 1863
aoqi@0 1864 Scope.Entry e = env.toplevel.namedImportScope.lookup(name);
aoqi@0 1865 for (; e.scope != null; e = e.next()) {
aoqi@0 1866 sym = e.sym;
aoqi@0 1867 Type origin = e.getOrigin().owner.type;
aoqi@0 1868 if (sym.kind == MTH) {
aoqi@0 1869 if (e.sym.owner.type != origin)
aoqi@0 1870 sym = sym.clone(e.getOrigin().owner);
aoqi@0 1871 if (!isAccessible(env, origin, sym))
aoqi@0 1872 sym = new AccessError(env, origin, sym);
aoqi@0 1873 bestSoFar = selectBest(env, origin,
aoqi@0 1874 argtypes, typeargtypes,
aoqi@0 1875 sym, bestSoFar,
aoqi@0 1876 allowBoxing, useVarargs, false);
aoqi@0 1877 }
aoqi@0 1878 }
aoqi@0 1879 if (bestSoFar.exists())
aoqi@0 1880 return bestSoFar;
aoqi@0 1881
aoqi@0 1882 e = env.toplevel.starImportScope.lookup(name);
aoqi@0 1883 for (; e.scope != null; e = e.next()) {
aoqi@0 1884 sym = e.sym;
aoqi@0 1885 Type origin = e.getOrigin().owner.type;
aoqi@0 1886 if (sym.kind == MTH) {
aoqi@0 1887 if (e.sym.owner.type != origin)
aoqi@0 1888 sym = sym.clone(e.getOrigin().owner);
aoqi@0 1889 if (!isAccessible(env, origin, sym))
aoqi@0 1890 sym = new AccessError(env, origin, sym);
aoqi@0 1891 bestSoFar = selectBest(env, origin,
aoqi@0 1892 argtypes, typeargtypes,
aoqi@0 1893 sym, bestSoFar,
aoqi@0 1894 allowBoxing, useVarargs, false);
aoqi@0 1895 }
aoqi@0 1896 }
aoqi@0 1897 return bestSoFar;
aoqi@0 1898 }
aoqi@0 1899
aoqi@0 1900 /** Load toplevel or member class with given fully qualified name and
aoqi@0 1901 * verify that it is accessible.
aoqi@0 1902 * @param env The current environment.
aoqi@0 1903 * @param name The fully qualified name of the class to be loaded.
aoqi@0 1904 */
aoqi@0 1905 Symbol loadClass(Env<AttrContext> env, Name name) {
aoqi@0 1906 try {
aoqi@0 1907 ClassSymbol c = reader.loadClass(name);
aoqi@0 1908 return isAccessible(env, c) ? c : new AccessError(c);
aoqi@0 1909 } catch (ClassReader.BadClassFile err) {
aoqi@0 1910 throw err;
aoqi@0 1911 } catch (CompletionFailure ex) {
aoqi@0 1912 return typeNotFound;
aoqi@0 1913 }
aoqi@0 1914 }
aoqi@0 1915
aoqi@0 1916
aoqi@0 1917 /**
aoqi@0 1918 * Find a type declared in a scope (not inherited). Return null
aoqi@0 1919 * if none is found.
aoqi@0 1920 * @param env The current environment.
aoqi@0 1921 * @param site The original type from where the selection takes
aoqi@0 1922 * place.
aoqi@0 1923 * @param name The type's name.
aoqi@0 1924 * @param c The class to search for the member type. This is
aoqi@0 1925 * always a superclass or implemented interface of
aoqi@0 1926 * site's class.
aoqi@0 1927 */
aoqi@0 1928 Symbol findImmediateMemberType(Env<AttrContext> env,
aoqi@0 1929 Type site,
aoqi@0 1930 Name name,
aoqi@0 1931 TypeSymbol c) {
aoqi@0 1932 Scope.Entry e = c.members().lookup(name);
aoqi@0 1933 while (e.scope != null) {
aoqi@0 1934 if (e.sym.kind == TYP) {
aoqi@0 1935 return isAccessible(env, site, e.sym)
aoqi@0 1936 ? e.sym
aoqi@0 1937 : new AccessError(env, site, e.sym);
aoqi@0 1938 }
aoqi@0 1939 e = e.next();
aoqi@0 1940 }
aoqi@0 1941 return typeNotFound;
aoqi@0 1942 }
aoqi@0 1943
aoqi@0 1944 /** Find a member type inherited from a superclass or interface.
aoqi@0 1945 * @param env The current environment.
aoqi@0 1946 * @param site The original type from where the selection takes
aoqi@0 1947 * place.
aoqi@0 1948 * @param name The type's name.
aoqi@0 1949 * @param c The class to search for the member type. This is
aoqi@0 1950 * always a superclass or implemented interface of
aoqi@0 1951 * site's class.
aoqi@0 1952 */
aoqi@0 1953 Symbol findInheritedMemberType(Env<AttrContext> env,
aoqi@0 1954 Type site,
aoqi@0 1955 Name name,
aoqi@0 1956 TypeSymbol c) {
aoqi@0 1957 Symbol bestSoFar = typeNotFound;
aoqi@0 1958 Symbol sym;
aoqi@0 1959 Type st = types.supertype(c.type);
aoqi@0 1960 if (st != null && st.hasTag(CLASS)) {
aoqi@0 1961 sym = findMemberType(env, site, name, st.tsym);
aoqi@0 1962 if (sym.kind < bestSoFar.kind) bestSoFar = sym;
aoqi@0 1963 }
aoqi@0 1964 for (List<Type> l = types.interfaces(c.type);
aoqi@0 1965 bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
aoqi@0 1966 l = l.tail) {
aoqi@0 1967 sym = findMemberType(env, site, name, l.head.tsym);
aoqi@0 1968 if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS &&
aoqi@0 1969 sym.owner != bestSoFar.owner)
aoqi@0 1970 bestSoFar = new AmbiguityError(bestSoFar, sym);
aoqi@0 1971 else if (sym.kind < bestSoFar.kind)
aoqi@0 1972 bestSoFar = sym;
aoqi@0 1973 }
aoqi@0 1974 return bestSoFar;
aoqi@0 1975 }
aoqi@0 1976
aoqi@0 1977 /** Find qualified member type.
aoqi@0 1978 * @param env The current environment.
aoqi@0 1979 * @param site The original type from where the selection takes
aoqi@0 1980 * place.
aoqi@0 1981 * @param name The type's name.
aoqi@0 1982 * @param c The class to search for the member type. This is
aoqi@0 1983 * always a superclass or implemented interface of
aoqi@0 1984 * site's class.
aoqi@0 1985 */
aoqi@0 1986 Symbol findMemberType(Env<AttrContext> env,
aoqi@0 1987 Type site,
aoqi@0 1988 Name name,
aoqi@0 1989 TypeSymbol c) {
aoqi@0 1990 Symbol sym = findImmediateMemberType(env, site, name, c);
aoqi@0 1991
aoqi@0 1992 if (sym != typeNotFound)
aoqi@0 1993 return sym;
aoqi@0 1994
aoqi@0 1995 return findInheritedMemberType(env, site, name, c);
aoqi@0 1996
aoqi@0 1997 }
aoqi@0 1998
aoqi@0 1999 /** Find a global type in given scope and load corresponding class.
aoqi@0 2000 * @param env The current environment.
aoqi@0 2001 * @param scope The scope in which to look for the type.
aoqi@0 2002 * @param name The type's name.
aoqi@0 2003 */
aoqi@0 2004 Symbol findGlobalType(Env<AttrContext> env, Scope scope, Name name) {
aoqi@0 2005 Symbol bestSoFar = typeNotFound;
aoqi@0 2006 for (Scope.Entry e = scope.lookup(name); e.scope != null; e = e.next()) {
aoqi@0 2007 Symbol sym = loadClass(env, e.sym.flatName());
aoqi@0 2008 if (bestSoFar.kind == TYP && sym.kind == TYP &&
aoqi@0 2009 bestSoFar != sym)
aoqi@0 2010 return new AmbiguityError(bestSoFar, sym);
aoqi@0 2011 else if (sym.kind < bestSoFar.kind)
aoqi@0 2012 bestSoFar = sym;
aoqi@0 2013 }
aoqi@0 2014 return bestSoFar;
aoqi@0 2015 }
aoqi@0 2016
aoqi@0 2017 Symbol findTypeVar(Env<AttrContext> env, Name name, boolean staticOnly) {
aoqi@0 2018 for (Scope.Entry e = env.info.scope.lookup(name);
aoqi@0 2019 e.scope != null;
aoqi@0 2020 e = e.next()) {
aoqi@0 2021 if (e.sym.kind == TYP) {
aoqi@0 2022 if (staticOnly &&
aoqi@0 2023 e.sym.type.hasTag(TYPEVAR) &&
aoqi@0 2024 e.sym.owner.kind == TYP)
aoqi@0 2025 return new StaticError(e.sym);
aoqi@0 2026 return e.sym;
aoqi@0 2027 }
aoqi@0 2028 }
aoqi@0 2029 return typeNotFound;
aoqi@0 2030 }
aoqi@0 2031
aoqi@0 2032 /** Find an unqualified type symbol.
aoqi@0 2033 * @param env The current environment.
aoqi@0 2034 * @param name The type's name.
aoqi@0 2035 */
aoqi@0 2036 Symbol findType(Env<AttrContext> env, Name name) {
aoqi@0 2037 Symbol bestSoFar = typeNotFound;
aoqi@0 2038 Symbol sym;
aoqi@0 2039 boolean staticOnly = false;
aoqi@0 2040 for (Env<AttrContext> env1 = env; env1.outer != null; env1 = env1.outer) {
aoqi@0 2041 if (isStatic(env1)) staticOnly = true;
aoqi@0 2042 // First, look for a type variable and the first member type
aoqi@0 2043 final Symbol tyvar = findTypeVar(env1, name, staticOnly);
aoqi@0 2044 sym = findImmediateMemberType(env1, env1.enclClass.sym.type,
aoqi@0 2045 name, env1.enclClass.sym);
aoqi@0 2046
aoqi@0 2047 // Return the type variable if we have it, and have no
aoqi@0 2048 // immediate member, OR the type variable is for a method.
aoqi@0 2049 if (tyvar != typeNotFound) {
aoqi@0 2050 if (sym == typeNotFound ||
aoqi@0 2051 (tyvar.kind == TYP && tyvar.exists() &&
aoqi@0 2052 tyvar.owner.kind == MTH))
aoqi@0 2053 return tyvar;
aoqi@0 2054 }
aoqi@0 2055
aoqi@0 2056 // If the environment is a class def, finish up,
aoqi@0 2057 // otherwise, do the entire findMemberType
aoqi@0 2058 if (sym == typeNotFound)
aoqi@0 2059 sym = findInheritedMemberType(env1, env1.enclClass.sym.type,
aoqi@0 2060 name, env1.enclClass.sym);
aoqi@0 2061
aoqi@0 2062 if (staticOnly && sym.kind == TYP &&
aoqi@0 2063 sym.type.hasTag(CLASS) &&
aoqi@0 2064 sym.type.getEnclosingType().hasTag(CLASS) &&
aoqi@0 2065 env1.enclClass.sym.type.isParameterized() &&
aoqi@0 2066 sym.type.getEnclosingType().isParameterized())
aoqi@0 2067 return new StaticError(sym);
aoqi@0 2068 else if (sym.exists()) return sym;
aoqi@0 2069 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
aoqi@0 2070
aoqi@0 2071 JCClassDecl encl = env1.baseClause ? (JCClassDecl)env1.tree : env1.enclClass;
aoqi@0 2072 if ((encl.sym.flags() & STATIC) != 0)
aoqi@0 2073 staticOnly = true;
aoqi@0 2074 }
aoqi@0 2075
aoqi@0 2076 if (!env.tree.hasTag(IMPORT)) {
aoqi@0 2077 sym = findGlobalType(env, env.toplevel.namedImportScope, name);
aoqi@0 2078 if (sym.exists()) return sym;
aoqi@0 2079 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
aoqi@0 2080
aoqi@0 2081 sym = findGlobalType(env, env.toplevel.packge.members(), name);
aoqi@0 2082 if (sym.exists()) return sym;
aoqi@0 2083 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
aoqi@0 2084
aoqi@0 2085 sym = findGlobalType(env, env.toplevel.starImportScope, name);
aoqi@0 2086 if (sym.exists()) return sym;
aoqi@0 2087 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
aoqi@0 2088 }
aoqi@0 2089
aoqi@0 2090 return bestSoFar;
aoqi@0 2091 }
aoqi@0 2092
aoqi@0 2093 /** Find an unqualified identifier which matches a specified kind set.
aoqi@0 2094 * @param env The current environment.
aoqi@0 2095 * @param name The identifier's name.
aoqi@0 2096 * @param kind Indicates the possible symbol kinds
aoqi@0 2097 * (a subset of VAL, TYP, PCK).
aoqi@0 2098 */
aoqi@0 2099 Symbol findIdent(Env<AttrContext> env, Name name, int kind) {
aoqi@0 2100 Symbol bestSoFar = typeNotFound;
aoqi@0 2101 Symbol sym;
aoqi@0 2102
aoqi@0 2103 if ((kind & VAR) != 0) {
aoqi@0 2104 sym = findVar(env, name);
aoqi@0 2105 if (sym.exists()) return sym;
aoqi@0 2106 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
aoqi@0 2107 }
aoqi@0 2108
aoqi@0 2109 if ((kind & TYP) != 0) {
aoqi@0 2110 sym = findType(env, name);
aoqi@0 2111 if (sym.kind==TYP) {
aoqi@0 2112 reportDependence(env.enclClass.sym, sym);
aoqi@0 2113 }
aoqi@0 2114 if (sym.exists()) return sym;
aoqi@0 2115 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
aoqi@0 2116 }
aoqi@0 2117
aoqi@0 2118 if ((kind & PCK) != 0) return reader.enterPackage(name);
aoqi@0 2119 else return bestSoFar;
aoqi@0 2120 }
aoqi@0 2121
aoqi@0 2122 /** Report dependencies.
aoqi@0 2123 * @param from The enclosing class sym
aoqi@0 2124 * @param to The found identifier that the class depends on.
aoqi@0 2125 */
aoqi@0 2126 public void reportDependence(Symbol from, Symbol to) {
aoqi@0 2127 // Override if you want to collect the reported dependencies.
aoqi@0 2128 }
aoqi@0 2129
aoqi@0 2130 /** Find an identifier in a package which matches a specified kind set.
aoqi@0 2131 * @param env The current environment.
aoqi@0 2132 * @param name The identifier's name.
aoqi@0 2133 * @param kind Indicates the possible symbol kinds
aoqi@0 2134 * (a nonempty subset of TYP, PCK).
aoqi@0 2135 */
aoqi@0 2136 Symbol findIdentInPackage(Env<AttrContext> env, TypeSymbol pck,
aoqi@0 2137 Name name, int kind) {
aoqi@0 2138 Name fullname = TypeSymbol.formFullName(name, pck);
aoqi@0 2139 Symbol bestSoFar = typeNotFound;
aoqi@0 2140 PackageSymbol pack = null;
aoqi@0 2141 if ((kind & PCK) != 0) {
aoqi@0 2142 pack = reader.enterPackage(fullname);
aoqi@0 2143 if (pack.exists()) return pack;
aoqi@0 2144 }
aoqi@0 2145 if ((kind & TYP) != 0) {
aoqi@0 2146 Symbol sym = loadClass(env, fullname);
aoqi@0 2147 if (sym.exists()) {
aoqi@0 2148 // don't allow programs to use flatnames
aoqi@0 2149 if (name == sym.name) return sym;
aoqi@0 2150 }
aoqi@0 2151 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
aoqi@0 2152 }
aoqi@0 2153 return (pack != null) ? pack : bestSoFar;
aoqi@0 2154 }
aoqi@0 2155
aoqi@0 2156 /** Find an identifier among the members of a given type `site'.
aoqi@0 2157 * @param env The current environment.
aoqi@0 2158 * @param site The type containing the symbol to be found.
aoqi@0 2159 * @param name The identifier's name.
aoqi@0 2160 * @param kind Indicates the possible symbol kinds
aoqi@0 2161 * (a subset of VAL, TYP).
aoqi@0 2162 */
aoqi@0 2163 Symbol findIdentInType(Env<AttrContext> env, Type site,
aoqi@0 2164 Name name, int kind) {
aoqi@0 2165 Symbol bestSoFar = typeNotFound;
aoqi@0 2166 Symbol sym;
aoqi@0 2167 if ((kind & VAR) != 0) {
aoqi@0 2168 sym = findField(env, site, name, site.tsym);
aoqi@0 2169 if (sym.exists()) return sym;
aoqi@0 2170 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
aoqi@0 2171 }
aoqi@0 2172
aoqi@0 2173 if ((kind & TYP) != 0) {
aoqi@0 2174 sym = findMemberType(env, site, name, site.tsym);
aoqi@0 2175 if (sym.exists()) return sym;
aoqi@0 2176 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
aoqi@0 2177 }
aoqi@0 2178 return bestSoFar;
aoqi@0 2179 }
aoqi@0 2180
aoqi@0 2181 /* ***************************************************************************
aoqi@0 2182 * Access checking
aoqi@0 2183 * The following methods convert ResolveErrors to ErrorSymbols, issuing
aoqi@0 2184 * an error message in the process
aoqi@0 2185 ****************************************************************************/
aoqi@0 2186
aoqi@0 2187 /** If `sym' is a bad symbol: report error and return errSymbol
aoqi@0 2188 * else pass through unchanged,
aoqi@0 2189 * additional arguments duplicate what has been used in trying to find the
aoqi@0 2190 * symbol {@literal (--> flyweight pattern)}. This improves performance since we
aoqi@0 2191 * expect misses to happen frequently.
aoqi@0 2192 *
aoqi@0 2193 * @param sym The symbol that was found, or a ResolveError.
aoqi@0 2194 * @param pos The position to use for error reporting.
aoqi@0 2195 * @param location The symbol the served as a context for this lookup
aoqi@0 2196 * @param site The original type from where the selection took place.
aoqi@0 2197 * @param name The symbol's name.
aoqi@0 2198 * @param qualified Did we get here through a qualified expression resolution?
aoqi@0 2199 * @param argtypes The invocation's value arguments,
aoqi@0 2200 * if we looked for a method.
aoqi@0 2201 * @param typeargtypes The invocation's type arguments,
aoqi@0 2202 * if we looked for a method.
aoqi@0 2203 * @param logResolveHelper helper class used to log resolve errors
aoqi@0 2204 */
aoqi@0 2205 Symbol accessInternal(Symbol sym,
aoqi@0 2206 DiagnosticPosition pos,
aoqi@0 2207 Symbol location,
aoqi@0 2208 Type site,
aoqi@0 2209 Name name,
aoqi@0 2210 boolean qualified,
aoqi@0 2211 List<Type> argtypes,
aoqi@0 2212 List<Type> typeargtypes,
aoqi@0 2213 LogResolveHelper logResolveHelper) {
aoqi@0 2214 if (sym.kind >= AMBIGUOUS) {
aoqi@0 2215 ResolveError errSym = (ResolveError)sym.baseSymbol();
aoqi@0 2216 sym = errSym.access(name, qualified ? site.tsym : syms.noSymbol);
aoqi@0 2217 argtypes = logResolveHelper.getArgumentTypes(errSym, sym, name, argtypes);
aoqi@0 2218 if (logResolveHelper.resolveDiagnosticNeeded(site, argtypes, typeargtypes)) {
aoqi@0 2219 logResolveError(errSym, pos, location, site, name, argtypes, typeargtypes);
aoqi@0 2220 }
aoqi@0 2221 }
aoqi@0 2222 return sym;
aoqi@0 2223 }
aoqi@0 2224
aoqi@0 2225 /**
aoqi@0 2226 * Variant of the generalized access routine, to be used for generating method
aoqi@0 2227 * resolution diagnostics
aoqi@0 2228 */
aoqi@0 2229 Symbol accessMethod(Symbol sym,
aoqi@0 2230 DiagnosticPosition pos,
aoqi@0 2231 Symbol location,
aoqi@0 2232 Type site,
aoqi@0 2233 Name name,
aoqi@0 2234 boolean qualified,
aoqi@0 2235 List<Type> argtypes,
aoqi@0 2236 List<Type> typeargtypes) {
aoqi@0 2237 return accessInternal(sym, pos, location, site, name, qualified, argtypes, typeargtypes, methodLogResolveHelper);
aoqi@0 2238 }
aoqi@0 2239
aoqi@0 2240 /** Same as original accessMethod(), but without location.
aoqi@0 2241 */
aoqi@0 2242 Symbol accessMethod(Symbol sym,
aoqi@0 2243 DiagnosticPosition pos,
aoqi@0 2244 Type site,
aoqi@0 2245 Name name,
aoqi@0 2246 boolean qualified,
aoqi@0 2247 List<Type> argtypes,
aoqi@0 2248 List<Type> typeargtypes) {
aoqi@0 2249 return accessMethod(sym, pos, site.tsym, site, name, qualified, argtypes, typeargtypes);
aoqi@0 2250 }
aoqi@0 2251
aoqi@0 2252 /**
aoqi@0 2253 * Variant of the generalized access routine, to be used for generating variable,
aoqi@0 2254 * type resolution diagnostics
aoqi@0 2255 */
aoqi@0 2256 Symbol accessBase(Symbol sym,
aoqi@0 2257 DiagnosticPosition pos,
aoqi@0 2258 Symbol location,
aoqi@0 2259 Type site,
aoqi@0 2260 Name name,
aoqi@0 2261 boolean qualified) {
aoqi@0 2262 return accessInternal(sym, pos, location, site, name, qualified, List.<Type>nil(), null, basicLogResolveHelper);
aoqi@0 2263 }
aoqi@0 2264
aoqi@0 2265 /** Same as original accessBase(), but without location.
aoqi@0 2266 */
aoqi@0 2267 Symbol accessBase(Symbol sym,
aoqi@0 2268 DiagnosticPosition pos,
aoqi@0 2269 Type site,
aoqi@0 2270 Name name,
aoqi@0 2271 boolean qualified) {
aoqi@0 2272 return accessBase(sym, pos, site.tsym, site, name, qualified);
aoqi@0 2273 }
aoqi@0 2274
aoqi@0 2275 interface LogResolveHelper {
aoqi@0 2276 boolean resolveDiagnosticNeeded(Type site, List<Type> argtypes, List<Type> typeargtypes);
aoqi@0 2277 List<Type> getArgumentTypes(ResolveError errSym, Symbol accessedSym, Name name, List<Type> argtypes);
aoqi@0 2278 }
aoqi@0 2279
aoqi@0 2280 LogResolveHelper basicLogResolveHelper = new LogResolveHelper() {
aoqi@0 2281 public boolean resolveDiagnosticNeeded(Type site, List<Type> argtypes, List<Type> typeargtypes) {
aoqi@0 2282 return !site.isErroneous();
aoqi@0 2283 }
aoqi@0 2284 public List<Type> getArgumentTypes(ResolveError errSym, Symbol accessedSym, Name name, List<Type> argtypes) {
aoqi@0 2285 return argtypes;
aoqi@0 2286 }
aoqi@0 2287 };
aoqi@0 2288
aoqi@0 2289 LogResolveHelper methodLogResolveHelper = new LogResolveHelper() {
aoqi@0 2290 public boolean resolveDiagnosticNeeded(Type site, List<Type> argtypes, List<Type> typeargtypes) {
aoqi@0 2291 return !site.isErroneous() &&
aoqi@0 2292 !Type.isErroneous(argtypes) &&
aoqi@0 2293 (typeargtypes == null || !Type.isErroneous(typeargtypes));
aoqi@0 2294 }
aoqi@0 2295 public List<Type> getArgumentTypes(ResolveError errSym, Symbol accessedSym, Name name, List<Type> argtypes) {
aoqi@0 2296 return (syms.operatorNames.contains(name)) ?
aoqi@0 2297 argtypes :
aoqi@0 2298 Type.map(argtypes, new ResolveDeferredRecoveryMap(AttrMode.SPECULATIVE, accessedSym, currentResolutionContext.step));
aoqi@0 2299 }
aoqi@0 2300 };
aoqi@0 2301
aoqi@0 2302 class ResolveDeferredRecoveryMap extends DeferredAttr.RecoveryDeferredTypeMap {
aoqi@0 2303
aoqi@0 2304 public ResolveDeferredRecoveryMap(AttrMode mode, Symbol msym, MethodResolutionPhase step) {
aoqi@0 2305 deferredAttr.super(mode, msym, step);
aoqi@0 2306 }
aoqi@0 2307
aoqi@0 2308 @Override
aoqi@0 2309 protected Type typeOf(DeferredType dt) {
aoqi@0 2310 Type res = super.typeOf(dt);
aoqi@0 2311 if (!res.isErroneous()) {
aoqi@0 2312 switch (TreeInfo.skipParens(dt.tree).getTag()) {
aoqi@0 2313 case LAMBDA:
aoqi@0 2314 case REFERENCE:
aoqi@0 2315 return dt;
aoqi@0 2316 case CONDEXPR:
aoqi@0 2317 return res == Type.recoveryType ?
aoqi@0 2318 dt : res;
aoqi@0 2319 }
aoqi@0 2320 }
aoqi@0 2321 return res;
aoqi@0 2322 }
aoqi@0 2323 }
aoqi@0 2324
aoqi@0 2325 /** Check that sym is not an abstract method.
aoqi@0 2326 */
aoqi@0 2327 void checkNonAbstract(DiagnosticPosition pos, Symbol sym) {
aoqi@0 2328 if ((sym.flags() & ABSTRACT) != 0 && (sym.flags() & DEFAULT) == 0)
aoqi@0 2329 log.error(pos, "abstract.cant.be.accessed.directly",
aoqi@0 2330 kindName(sym), sym, sym.location());
aoqi@0 2331 }
aoqi@0 2332
aoqi@0 2333 /* ***************************************************************************
aoqi@0 2334 * Debugging
aoqi@0 2335 ****************************************************************************/
aoqi@0 2336
aoqi@0 2337 /** print all scopes starting with scope s and proceeding outwards.
aoqi@0 2338 * used for debugging.
aoqi@0 2339 */
aoqi@0 2340 public void printscopes(Scope s) {
aoqi@0 2341 while (s != null) {
aoqi@0 2342 if (s.owner != null)
aoqi@0 2343 System.err.print(s.owner + ": ");
aoqi@0 2344 for (Scope.Entry e = s.elems; e != null; e = e.sibling) {
aoqi@0 2345 if ((e.sym.flags() & ABSTRACT) != 0)
aoqi@0 2346 System.err.print("abstract ");
aoqi@0 2347 System.err.print(e.sym + " ");
aoqi@0 2348 }
aoqi@0 2349 System.err.println();
aoqi@0 2350 s = s.next;
aoqi@0 2351 }
aoqi@0 2352 }
aoqi@0 2353
aoqi@0 2354 void printscopes(Env<AttrContext> env) {
aoqi@0 2355 while (env.outer != null) {
aoqi@0 2356 System.err.println("------------------------------");
aoqi@0 2357 printscopes(env.info.scope);
aoqi@0 2358 env = env.outer;
aoqi@0 2359 }
aoqi@0 2360 }
aoqi@0 2361
aoqi@0 2362 public void printscopes(Type t) {
aoqi@0 2363 while (t.hasTag(CLASS)) {
aoqi@0 2364 printscopes(t.tsym.members());
aoqi@0 2365 t = types.supertype(t);
aoqi@0 2366 }
aoqi@0 2367 }
aoqi@0 2368
aoqi@0 2369 /* ***************************************************************************
aoqi@0 2370 * Name resolution
aoqi@0 2371 * Naming conventions are as for symbol lookup
aoqi@0 2372 * Unlike the find... methods these methods will report access errors
aoqi@0 2373 ****************************************************************************/
aoqi@0 2374
aoqi@0 2375 /** Resolve an unqualified (non-method) identifier.
aoqi@0 2376 * @param pos The position to use for error reporting.
aoqi@0 2377 * @param env The environment current at the identifier use.
aoqi@0 2378 * @param name The identifier's name.
aoqi@0 2379 * @param kind The set of admissible symbol kinds for the identifier.
aoqi@0 2380 */
aoqi@0 2381 Symbol resolveIdent(DiagnosticPosition pos, Env<AttrContext> env,
aoqi@0 2382 Name name, int kind) {
aoqi@0 2383 return accessBase(
aoqi@0 2384 findIdent(env, name, kind),
aoqi@0 2385 pos, env.enclClass.sym.type, name, false);
aoqi@0 2386 }
aoqi@0 2387
aoqi@0 2388 /** Resolve an unqualified method identifier.
aoqi@0 2389 * @param pos The position to use for error reporting.
aoqi@0 2390 * @param env The environment current at the method invocation.
aoqi@0 2391 * @param name The identifier's name.
aoqi@0 2392 * @param argtypes The types of the invocation's value arguments.
aoqi@0 2393 * @param typeargtypes The types of the invocation's type arguments.
aoqi@0 2394 */
aoqi@0 2395 Symbol resolveMethod(DiagnosticPosition pos,
aoqi@0 2396 Env<AttrContext> env,
aoqi@0 2397 Name name,
aoqi@0 2398 List<Type> argtypes,
aoqi@0 2399 List<Type> typeargtypes) {
aoqi@0 2400 return lookupMethod(env, pos, env.enclClass.sym, resolveMethodCheck,
aoqi@0 2401 new BasicLookupHelper(name, env.enclClass.sym.type, argtypes, typeargtypes) {
aoqi@0 2402 @Override
aoqi@0 2403 Symbol doLookup(Env<AttrContext> env, MethodResolutionPhase phase) {
aoqi@0 2404 return findFun(env, name, argtypes, typeargtypes,
aoqi@0 2405 phase.isBoxingRequired(),
aoqi@0 2406 phase.isVarargsRequired());
aoqi@0 2407 }});
aoqi@0 2408 }
aoqi@0 2409
aoqi@0 2410 /** Resolve a qualified method identifier
aoqi@0 2411 * @param pos The position to use for error reporting.
aoqi@0 2412 * @param env The environment current at the method invocation.
aoqi@0 2413 * @param site The type of the qualifying expression, in which
aoqi@0 2414 * identifier is searched.
aoqi@0 2415 * @param name The identifier's name.
aoqi@0 2416 * @param argtypes The types of the invocation's value arguments.
aoqi@0 2417 * @param typeargtypes The types of the invocation's type arguments.
aoqi@0 2418 */
aoqi@0 2419 Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env,
aoqi@0 2420 Type site, Name name, List<Type> argtypes,
aoqi@0 2421 List<Type> typeargtypes) {
aoqi@0 2422 return resolveQualifiedMethod(pos, env, site.tsym, site, name, argtypes, typeargtypes);
aoqi@0 2423 }
aoqi@0 2424 Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env,
aoqi@0 2425 Symbol location, Type site, Name name, List<Type> argtypes,
aoqi@0 2426 List<Type> typeargtypes) {
aoqi@0 2427 return resolveQualifiedMethod(new MethodResolutionContext(), pos, env, location, site, name, argtypes, typeargtypes);
aoqi@0 2428 }
aoqi@0 2429 private Symbol resolveQualifiedMethod(MethodResolutionContext resolveContext,
aoqi@0 2430 DiagnosticPosition pos, Env<AttrContext> env,
aoqi@0 2431 Symbol location, Type site, Name name, List<Type> argtypes,
aoqi@0 2432 List<Type> typeargtypes) {
aoqi@0 2433 return lookupMethod(env, pos, location, resolveContext, new BasicLookupHelper(name, site, argtypes, typeargtypes) {
aoqi@0 2434 @Override
aoqi@0 2435 Symbol doLookup(Env<AttrContext> env, MethodResolutionPhase phase) {
aoqi@0 2436 return findMethod(env, site, name, argtypes, typeargtypes,
aoqi@0 2437 phase.isBoxingRequired(),
aoqi@0 2438 phase.isVarargsRequired(), false);
aoqi@0 2439 }
aoqi@0 2440 @Override
aoqi@0 2441 Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym) {
aoqi@0 2442 if (sym.kind >= AMBIGUOUS) {
aoqi@0 2443 sym = super.access(env, pos, location, sym);
aoqi@0 2444 } else if (allowMethodHandles) {
aoqi@0 2445 MethodSymbol msym = (MethodSymbol)sym;
aoqi@0 2446 if ((msym.flags() & SIGNATURE_POLYMORPHIC) != 0) {
aoqi@0 2447 return findPolymorphicSignatureInstance(env, sym, argtypes);
aoqi@0 2448 }
aoqi@0 2449 }
aoqi@0 2450 return sym;
aoqi@0 2451 }
aoqi@0 2452 });
aoqi@0 2453 }
aoqi@0 2454
aoqi@0 2455 /** Find or create an implicit method of exactly the given type (after erasure).
aoqi@0 2456 * Searches in a side table, not the main scope of the site.
aoqi@0 2457 * This emulates the lookup process required by JSR 292 in JVM.
aoqi@0 2458 * @param env Attribution environment
aoqi@0 2459 * @param spMethod signature polymorphic method - i.e. MH.invokeExact
aoqi@0 2460 * @param argtypes The required argument types
aoqi@0 2461 */
aoqi@0 2462 Symbol findPolymorphicSignatureInstance(Env<AttrContext> env,
aoqi@0 2463 final Symbol spMethod,
aoqi@0 2464 List<Type> argtypes) {
aoqi@0 2465 Type mtype = infer.instantiatePolymorphicSignatureInstance(env,
aoqi@0 2466 (MethodSymbol)spMethod, currentResolutionContext, argtypes);
aoqi@0 2467 for (Symbol sym : polymorphicSignatureScope.getElementsByName(spMethod.name)) {
aoqi@0 2468 if (types.isSameType(mtype, sym.type)) {
aoqi@0 2469 return sym;
aoqi@0 2470 }
aoqi@0 2471 }
aoqi@0 2472
aoqi@0 2473 // create the desired method
aoqi@0 2474 long flags = ABSTRACT | HYPOTHETICAL | spMethod.flags() & Flags.AccessFlags;
aoqi@0 2475 Symbol msym = new MethodSymbol(flags, spMethod.name, mtype, spMethod.owner) {
aoqi@0 2476 @Override
aoqi@0 2477 public Symbol baseSymbol() {
aoqi@0 2478 return spMethod;
aoqi@0 2479 }
aoqi@0 2480 };
mcimadamore@3075 2481 if (!mtype.isErroneous()) { // Cache only if kosher.
mcimadamore@3075 2482 polymorphicSignatureScope.enter(msym);
mcimadamore@3075 2483 }
aoqi@0 2484 return msym;
aoqi@0 2485 }
aoqi@0 2486
aoqi@0 2487 /** Resolve a qualified method identifier, throw a fatal error if not
aoqi@0 2488 * found.
aoqi@0 2489 * @param pos The position to use for error reporting.
aoqi@0 2490 * @param env The environment current at the method invocation.
aoqi@0 2491 * @param site The type of the qualifying expression, in which
aoqi@0 2492 * identifier is searched.
aoqi@0 2493 * @param name The identifier's name.
aoqi@0 2494 * @param argtypes The types of the invocation's value arguments.
aoqi@0 2495 * @param typeargtypes The types of the invocation's type arguments.
aoqi@0 2496 */
aoqi@0 2497 public MethodSymbol resolveInternalMethod(DiagnosticPosition pos, Env<AttrContext> env,
aoqi@0 2498 Type site, Name name,
aoqi@0 2499 List<Type> argtypes,
aoqi@0 2500 List<Type> typeargtypes) {
aoqi@0 2501 MethodResolutionContext resolveContext = new MethodResolutionContext();
aoqi@0 2502 resolveContext.internalResolution = true;
aoqi@0 2503 Symbol sym = resolveQualifiedMethod(resolveContext, pos, env, site.tsym,
aoqi@0 2504 site, name, argtypes, typeargtypes);
aoqi@0 2505 if (sym.kind == MTH) return (MethodSymbol)sym;
aoqi@0 2506 else throw new FatalError(
aoqi@0 2507 diags.fragment("fatal.err.cant.locate.meth",
aoqi@0 2508 name));
aoqi@0 2509 }
aoqi@0 2510
aoqi@0 2511 /** Resolve constructor.
aoqi@0 2512 * @param pos The position to use for error reporting.
aoqi@0 2513 * @param env The environment current at the constructor invocation.
aoqi@0 2514 * @param site The type of class for which a constructor is searched.
aoqi@0 2515 * @param argtypes The types of the constructor invocation's value
aoqi@0 2516 * arguments.
aoqi@0 2517 * @param typeargtypes The types of the constructor invocation's type
aoqi@0 2518 * arguments.
aoqi@0 2519 */
aoqi@0 2520 Symbol resolveConstructor(DiagnosticPosition pos,
aoqi@0 2521 Env<AttrContext> env,
aoqi@0 2522 Type site,
aoqi@0 2523 List<Type> argtypes,
aoqi@0 2524 List<Type> typeargtypes) {
aoqi@0 2525 return resolveConstructor(new MethodResolutionContext(), pos, env, site, argtypes, typeargtypes);
aoqi@0 2526 }
aoqi@0 2527
aoqi@0 2528 private Symbol resolveConstructor(MethodResolutionContext resolveContext,
aoqi@0 2529 final DiagnosticPosition pos,
aoqi@0 2530 Env<AttrContext> env,
aoqi@0 2531 Type site,
aoqi@0 2532 List<Type> argtypes,
aoqi@0 2533 List<Type> typeargtypes) {
aoqi@0 2534 return lookupMethod(env, pos, site.tsym, resolveContext, new BasicLookupHelper(names.init, site, argtypes, typeargtypes) {
aoqi@0 2535 @Override
aoqi@0 2536 Symbol doLookup(Env<AttrContext> env, MethodResolutionPhase phase) {
aoqi@0 2537 return findConstructor(pos, env, site, argtypes, typeargtypes,
aoqi@0 2538 phase.isBoxingRequired(),
aoqi@0 2539 phase.isVarargsRequired());
aoqi@0 2540 }
aoqi@0 2541 });
aoqi@0 2542 }
aoqi@0 2543
aoqi@0 2544 /** Resolve a constructor, throw a fatal error if not found.
aoqi@0 2545 * @param pos The position to use for error reporting.
aoqi@0 2546 * @param env The environment current at the method invocation.
aoqi@0 2547 * @param site The type to be constructed.
aoqi@0 2548 * @param argtypes The types of the invocation's value arguments.
aoqi@0 2549 * @param typeargtypes The types of the invocation's type arguments.
aoqi@0 2550 */
aoqi@0 2551 public MethodSymbol resolveInternalConstructor(DiagnosticPosition pos, Env<AttrContext> env,
aoqi@0 2552 Type site,
aoqi@0 2553 List<Type> argtypes,
aoqi@0 2554 List<Type> typeargtypes) {
aoqi@0 2555 MethodResolutionContext resolveContext = new MethodResolutionContext();
aoqi@0 2556 resolveContext.internalResolution = true;
aoqi@0 2557 Symbol sym = resolveConstructor(resolveContext, pos, env, site, argtypes, typeargtypes);
aoqi@0 2558 if (sym.kind == MTH) return (MethodSymbol)sym;
aoqi@0 2559 else throw new FatalError(
aoqi@0 2560 diags.fragment("fatal.err.cant.locate.ctor", site));
aoqi@0 2561 }
aoqi@0 2562
aoqi@0 2563 Symbol findConstructor(DiagnosticPosition pos, Env<AttrContext> env,
aoqi@0 2564 Type site, List<Type> argtypes,
aoqi@0 2565 List<Type> typeargtypes,
aoqi@0 2566 boolean allowBoxing,
aoqi@0 2567 boolean useVarargs) {
aoqi@0 2568 Symbol sym = findMethod(env, site,
aoqi@0 2569 names.init, argtypes,
aoqi@0 2570 typeargtypes, allowBoxing,
aoqi@0 2571 useVarargs, false);
aoqi@0 2572 chk.checkDeprecated(pos, env.info.scope.owner, sym);
aoqi@0 2573 return sym;
aoqi@0 2574 }
aoqi@0 2575
aoqi@0 2576 /** Resolve constructor using diamond inference.
aoqi@0 2577 * @param pos The position to use for error reporting.
aoqi@0 2578 * @param env The environment current at the constructor invocation.
aoqi@0 2579 * @param site The type of class for which a constructor is searched.
aoqi@0 2580 * The scope of this class has been touched in attribution.
aoqi@0 2581 * @param argtypes The types of the constructor invocation's value
aoqi@0 2582 * arguments.
aoqi@0 2583 * @param typeargtypes The types of the constructor invocation's type
aoqi@0 2584 * arguments.
aoqi@0 2585 */
aoqi@0 2586 Symbol resolveDiamond(DiagnosticPosition pos,
aoqi@0 2587 Env<AttrContext> env,
aoqi@0 2588 Type site,
aoqi@0 2589 List<Type> argtypes,
aoqi@0 2590 List<Type> typeargtypes) {
aoqi@0 2591 return lookupMethod(env, pos, site.tsym, resolveMethodCheck,
aoqi@0 2592 new BasicLookupHelper(names.init, site, argtypes, typeargtypes) {
aoqi@0 2593 @Override
aoqi@0 2594 Symbol doLookup(Env<AttrContext> env, MethodResolutionPhase phase) {
aoqi@0 2595 return findDiamond(env, site, argtypes, typeargtypes,
aoqi@0 2596 phase.isBoxingRequired(),
aoqi@0 2597 phase.isVarargsRequired());
aoqi@0 2598 }
aoqi@0 2599 @Override
aoqi@0 2600 Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym) {
aoqi@0 2601 if (sym.kind >= AMBIGUOUS) {
aoqi@0 2602 if (sym.kind != WRONG_MTH && sym.kind != WRONG_MTHS) {
aoqi@0 2603 sym = super.access(env, pos, location, sym);
aoqi@0 2604 } else {
aoqi@0 2605 final JCDiagnostic details = sym.kind == WRONG_MTH ?
aoqi@0 2606 ((InapplicableSymbolError)sym.baseSymbol()).errCandidate().snd :
aoqi@0 2607 null;
aoqi@0 2608 sym = new InapplicableSymbolError(sym.kind, "diamondError", currentResolutionContext) {
aoqi@0 2609 @Override
aoqi@0 2610 JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos,
aoqi@0 2611 Symbol location, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) {
aoqi@0 2612 String key = details == null ?
aoqi@0 2613 "cant.apply.diamond" :
aoqi@0 2614 "cant.apply.diamond.1";
aoqi@0 2615 return diags.create(dkind, log.currentSource(), pos, key,
aoqi@0 2616 diags.fragment("diamond", site.tsym), details);
aoqi@0 2617 }
aoqi@0 2618 };
aoqi@0 2619 sym = accessMethod(sym, pos, site, names.init, true, argtypes, typeargtypes);
aoqi@0 2620 env.info.pendingResolutionPhase = currentResolutionContext.step;
aoqi@0 2621 }
aoqi@0 2622 }
aoqi@0 2623 return sym;
aoqi@0 2624 }});
aoqi@0 2625 }
aoqi@0 2626
aoqi@0 2627 /** This method scans all the constructor symbol in a given class scope -
aoqi@0 2628 * assuming that the original scope contains a constructor of the kind:
aoqi@0 2629 * {@code Foo(X x, Y y)}, where X,Y are class type-variables declared in Foo,
aoqi@0 2630 * a method check is executed against the modified constructor type:
aoqi@0 2631 * {@code <X,Y>Foo<X,Y>(X x, Y y)}. This is crucial in order to enable diamond
aoqi@0 2632 * inference. The inferred return type of the synthetic constructor IS
aoqi@0 2633 * the inferred type for the diamond operator.
aoqi@0 2634 */
aoqi@0 2635 private Symbol findDiamond(Env<AttrContext> env,
aoqi@0 2636 Type site,
aoqi@0 2637 List<Type> argtypes,
aoqi@0 2638 List<Type> typeargtypes,
aoqi@0 2639 boolean allowBoxing,
aoqi@0 2640 boolean useVarargs) {
aoqi@0 2641 Symbol bestSoFar = methodNotFound;
aoqi@0 2642 for (Scope.Entry e = site.tsym.members().lookup(names.init);
aoqi@0 2643 e.scope != null;
aoqi@0 2644 e = e.next()) {
aoqi@0 2645 final Symbol sym = e.sym;
aoqi@0 2646 //- System.out.println(" e " + e.sym);
aoqi@0 2647 if (sym.kind == MTH &&
aoqi@0 2648 (sym.flags_field & SYNTHETIC) == 0) {
aoqi@0 2649 List<Type> oldParams = e.sym.type.hasTag(FORALL) ?
aoqi@0 2650 ((ForAll)sym.type).tvars :
aoqi@0 2651 List.<Type>nil();
aoqi@0 2652 Type constrType = new ForAll(site.tsym.type.getTypeArguments().appendList(oldParams),
aoqi@0 2653 types.createMethodTypeWithReturn(sym.type.asMethodType(), site));
aoqi@0 2654 MethodSymbol newConstr = new MethodSymbol(sym.flags(), names.init, constrType, site.tsym) {
aoqi@0 2655 @Override
aoqi@0 2656 public Symbol baseSymbol() {
aoqi@0 2657 return sym;
aoqi@0 2658 }
aoqi@0 2659 };
aoqi@0 2660 bestSoFar = selectBest(env, site, argtypes, typeargtypes,
aoqi@0 2661 newConstr,
aoqi@0 2662 bestSoFar,
aoqi@0 2663 allowBoxing,
aoqi@0 2664 useVarargs,
aoqi@0 2665 false);
aoqi@0 2666 }
aoqi@0 2667 }
aoqi@0 2668 return bestSoFar;
aoqi@0 2669 }
aoqi@0 2670
aoqi@0 2671
aoqi@0 2672
aoqi@0 2673 /** Resolve operator.
aoqi@0 2674 * @param pos The position to use for error reporting.
aoqi@0 2675 * @param optag The tag of the operation tree.
aoqi@0 2676 * @param env The environment current at the operation.
aoqi@0 2677 * @param argtypes The types of the operands.
aoqi@0 2678 */
aoqi@0 2679 Symbol resolveOperator(DiagnosticPosition pos, JCTree.Tag optag,
aoqi@0 2680 Env<AttrContext> env, List<Type> argtypes) {
aoqi@0 2681 MethodResolutionContext prevResolutionContext = currentResolutionContext;
aoqi@0 2682 try {
aoqi@0 2683 currentResolutionContext = new MethodResolutionContext();
aoqi@0 2684 Name name = treeinfo.operatorName(optag);
aoqi@0 2685 return lookupMethod(env, pos, syms.predefClass, currentResolutionContext,
aoqi@0 2686 new BasicLookupHelper(name, syms.predefClass.type, argtypes, null, BOX) {
aoqi@0 2687 @Override
aoqi@0 2688 Symbol doLookup(Env<AttrContext> env, MethodResolutionPhase phase) {
aoqi@0 2689 return findMethod(env, site, name, argtypes, typeargtypes,
aoqi@0 2690 phase.isBoxingRequired(),
aoqi@0 2691 phase.isVarargsRequired(), true);
aoqi@0 2692 }
aoqi@0 2693 @Override
aoqi@0 2694 Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym) {
aoqi@0 2695 return accessMethod(sym, pos, env.enclClass.sym.type, name,
aoqi@0 2696 false, argtypes, null);
aoqi@0 2697 }
aoqi@0 2698 });
aoqi@0 2699 } finally {
aoqi@0 2700 currentResolutionContext = prevResolutionContext;
aoqi@0 2701 }
aoqi@0 2702 }
aoqi@0 2703
aoqi@0 2704 /** Resolve operator.
aoqi@0 2705 * @param pos The position to use for error reporting.
aoqi@0 2706 * @param optag The tag of the operation tree.
aoqi@0 2707 * @param env The environment current at the operation.
aoqi@0 2708 * @param arg The type of the operand.
aoqi@0 2709 */
aoqi@0 2710 Symbol resolveUnaryOperator(DiagnosticPosition pos, JCTree.Tag optag, Env<AttrContext> env, Type arg) {
aoqi@0 2711 return resolveOperator(pos, optag, env, List.of(arg));
aoqi@0 2712 }
aoqi@0 2713
aoqi@0 2714 /** Resolve binary operator.
aoqi@0 2715 * @param pos The position to use for error reporting.
aoqi@0 2716 * @param optag The tag of the operation tree.
aoqi@0 2717 * @param env The environment current at the operation.
aoqi@0 2718 * @param left The types of the left operand.
aoqi@0 2719 * @param right The types of the right operand.
aoqi@0 2720 */
aoqi@0 2721 Symbol resolveBinaryOperator(DiagnosticPosition pos,
aoqi@0 2722 JCTree.Tag optag,
aoqi@0 2723 Env<AttrContext> env,
aoqi@0 2724 Type left,
aoqi@0 2725 Type right) {
aoqi@0 2726 return resolveOperator(pos, optag, env, List.of(left, right));
aoqi@0 2727 }
aoqi@0 2728
aoqi@0 2729 Symbol getMemberReference(DiagnosticPosition pos,
aoqi@0 2730 Env<AttrContext> env,
aoqi@0 2731 JCMemberReference referenceTree,
aoqi@0 2732 Type site,
aoqi@0 2733 Name name) {
aoqi@0 2734
aoqi@0 2735 site = types.capture(site);
aoqi@0 2736
aoqi@0 2737 ReferenceLookupHelper lookupHelper = makeReferenceLookupHelper(
aoqi@0 2738 referenceTree, site, name, List.<Type>nil(), null, VARARITY);
aoqi@0 2739
aoqi@0 2740 Env<AttrContext> newEnv = env.dup(env.tree, env.info.dup());
aoqi@0 2741 Symbol sym = lookupMethod(newEnv, env.tree.pos(), site.tsym,
aoqi@0 2742 nilMethodCheck, lookupHelper);
aoqi@0 2743
aoqi@0 2744 env.info.pendingResolutionPhase = newEnv.info.pendingResolutionPhase;
aoqi@0 2745
aoqi@0 2746 return sym;
aoqi@0 2747 }
aoqi@0 2748
aoqi@0 2749 ReferenceLookupHelper makeReferenceLookupHelper(JCMemberReference referenceTree,
aoqi@0 2750 Type site,
aoqi@0 2751 Name name,
aoqi@0 2752 List<Type> argtypes,
aoqi@0 2753 List<Type> typeargtypes,
aoqi@0 2754 MethodResolutionPhase maxPhase) {
aoqi@0 2755 ReferenceLookupHelper result;
aoqi@0 2756 if (!name.equals(names.init)) {
aoqi@0 2757 //method reference
aoqi@0 2758 result =
aoqi@0 2759 new MethodReferenceLookupHelper(referenceTree, name, site, argtypes, typeargtypes, maxPhase);
aoqi@0 2760 } else {
aoqi@0 2761 if (site.hasTag(ARRAY)) {
aoqi@0 2762 //array constructor reference
aoqi@0 2763 result =
aoqi@0 2764 new ArrayConstructorReferenceLookupHelper(referenceTree, site, argtypes, typeargtypes, maxPhase);
aoqi@0 2765 } else {
aoqi@0 2766 //class constructor reference
aoqi@0 2767 result =
aoqi@0 2768 new ConstructorReferenceLookupHelper(referenceTree, site, argtypes, typeargtypes, maxPhase);
aoqi@0 2769 }
aoqi@0 2770 }
aoqi@0 2771 return result;
aoqi@0 2772 }
aoqi@0 2773
aoqi@0 2774 Symbol resolveMemberReferenceByArity(Env<AttrContext> env,
aoqi@0 2775 JCMemberReference referenceTree,
aoqi@0 2776 Type site,
aoqi@0 2777 Name name,
aoqi@0 2778 List<Type> argtypes,
aoqi@0 2779 InferenceContext inferenceContext) {
aoqi@0 2780
aoqi@0 2781 boolean isStaticSelector = TreeInfo.isStaticSelector(referenceTree.expr, names);
aoqi@0 2782 site = types.capture(site);
aoqi@0 2783
aoqi@0 2784 ReferenceLookupHelper boundLookupHelper = makeReferenceLookupHelper(
aoqi@0 2785 referenceTree, site, name, argtypes, null, VARARITY);
aoqi@0 2786 //step 1 - bound lookup
aoqi@0 2787 Env<AttrContext> boundEnv = env.dup(env.tree, env.info.dup());
aoqi@0 2788 Symbol boundSym = lookupMethod(boundEnv, env.tree.pos(), site.tsym,
aoqi@0 2789 arityMethodCheck, boundLookupHelper);
aoqi@0 2790 if (isStaticSelector &&
aoqi@0 2791 !name.equals(names.init) &&
aoqi@0 2792 !boundSym.isStatic() &&
aoqi@0 2793 boundSym.kind < ERRONEOUS) {
aoqi@0 2794 boundSym = methodNotFound;
aoqi@0 2795 }
aoqi@0 2796
aoqi@0 2797 //step 2 - unbound lookup
aoqi@0 2798 Symbol unboundSym = methodNotFound;
aoqi@0 2799 ReferenceLookupHelper unboundLookupHelper = null;
aoqi@0 2800 Env<AttrContext> unboundEnv = env.dup(env.tree, env.info.dup());
aoqi@0 2801 if (isStaticSelector) {
aoqi@0 2802 unboundLookupHelper = boundLookupHelper.unboundLookup(inferenceContext);
aoqi@0 2803 unboundSym = lookupMethod(unboundEnv, env.tree.pos(), site.tsym,
aoqi@0 2804 arityMethodCheck, unboundLookupHelper);
aoqi@0 2805 if (unboundSym.isStatic() &&
aoqi@0 2806 unboundSym.kind < ERRONEOUS) {
aoqi@0 2807 unboundSym = methodNotFound;
aoqi@0 2808 }
aoqi@0 2809 }
aoqi@0 2810
aoqi@0 2811 //merge results
aoqi@0 2812 Symbol bestSym = choose(boundSym, unboundSym);
aoqi@0 2813 env.info.pendingResolutionPhase = bestSym == unboundSym ?
aoqi@0 2814 unboundEnv.info.pendingResolutionPhase :
aoqi@0 2815 boundEnv.info.pendingResolutionPhase;
aoqi@0 2816
aoqi@0 2817 return bestSym;
aoqi@0 2818 }
aoqi@0 2819
aoqi@0 2820 /**
aoqi@0 2821 * Resolution of member references is typically done as a single
aoqi@0 2822 * overload resolution step, where the argument types A are inferred from
aoqi@0 2823 * the target functional descriptor.
aoqi@0 2824 *
aoqi@0 2825 * If the member reference is a method reference with a type qualifier,
aoqi@0 2826 * a two-step lookup process is performed. The first step uses the
aoqi@0 2827 * expected argument list A, while the second step discards the first
aoqi@0 2828 * type from A (which is treated as a receiver type).
aoqi@0 2829 *
aoqi@0 2830 * There are two cases in which inference is performed: (i) if the member
aoqi@0 2831 * reference is a constructor reference and the qualifier type is raw - in
aoqi@0 2832 * which case diamond inference is used to infer a parameterization for the
aoqi@0 2833 * type qualifier; (ii) if the member reference is an unbound reference
aoqi@0 2834 * where the type qualifier is raw - in that case, during the unbound lookup
aoqi@0 2835 * the receiver argument type is used to infer an instantiation for the raw
aoqi@0 2836 * qualifier type.
aoqi@0 2837 *
aoqi@0 2838 * When a multi-step resolution process is exploited, it is an error
aoqi@0 2839 * if two candidates are found (ambiguity).
aoqi@0 2840 *
aoqi@0 2841 * This routine returns a pair (T,S), where S is the member reference symbol,
aoqi@0 2842 * and T is the type of the class in which S is defined. This is necessary as
aoqi@0 2843 * the type T might be dynamically inferred (i.e. if constructor reference
aoqi@0 2844 * has a raw qualifier).
aoqi@0 2845 */
aoqi@0 2846 Pair<Symbol, ReferenceLookupHelper> resolveMemberReference(Env<AttrContext> env,
aoqi@0 2847 JCMemberReference referenceTree,
aoqi@0 2848 Type site,
aoqi@0 2849 Name name,
aoqi@0 2850 List<Type> argtypes,
aoqi@0 2851 List<Type> typeargtypes,
aoqi@0 2852 MethodCheck methodCheck,
aoqi@0 2853 InferenceContext inferenceContext,
aoqi@0 2854 AttrMode mode) {
aoqi@0 2855
aoqi@0 2856 site = types.capture(site);
aoqi@0 2857 ReferenceLookupHelper boundLookupHelper = makeReferenceLookupHelper(
aoqi@0 2858 referenceTree, site, name, argtypes, typeargtypes, VARARITY);
aoqi@0 2859
aoqi@0 2860 //step 1 - bound lookup
aoqi@0 2861 Env<AttrContext> boundEnv = env.dup(env.tree, env.info.dup());
aoqi@0 2862 Symbol origBoundSym;
aoqi@0 2863 boolean staticErrorForBound = false;
aoqi@0 2864 MethodResolutionContext boundSearchResolveContext = new MethodResolutionContext();
aoqi@0 2865 boundSearchResolveContext.methodCheck = methodCheck;
aoqi@0 2866 Symbol boundSym = origBoundSym = lookupMethod(boundEnv, env.tree.pos(),
aoqi@0 2867 site.tsym, boundSearchResolveContext, boundLookupHelper);
aoqi@0 2868 SearchResultKind boundSearchResultKind = SearchResultKind.NOT_APPLICABLE_MATCH;
aoqi@0 2869 boolean isStaticSelector = TreeInfo.isStaticSelector(referenceTree.expr, names);
aoqi@0 2870 boolean shouldCheckForStaticness = isStaticSelector &&
aoqi@0 2871 referenceTree.getMode() == ReferenceMode.INVOKE;
aoqi@0 2872 if (boundSym.kind != WRONG_MTHS && boundSym.kind != WRONG_MTH) {
aoqi@0 2873 if (shouldCheckForStaticness) {
aoqi@0 2874 if (!boundSym.isStatic()) {
aoqi@0 2875 staticErrorForBound = true;
aoqi@0 2876 if (hasAnotherApplicableMethod(
aoqi@0 2877 boundSearchResolveContext, boundSym, true)) {
aoqi@0 2878 boundSearchResultKind = SearchResultKind.BAD_MATCH_MORE_SPECIFIC;
aoqi@0 2879 } else {
aoqi@0 2880 boundSearchResultKind = SearchResultKind.BAD_MATCH;
aoqi@0 2881 if (boundSym.kind < ERRONEOUS) {
aoqi@0 2882 boundSym = methodWithCorrectStaticnessNotFound;
aoqi@0 2883 }
aoqi@0 2884 }
aoqi@0 2885 } else if (boundSym.kind < ERRONEOUS) {
aoqi@0 2886 boundSearchResultKind = SearchResultKind.GOOD_MATCH;
aoqi@0 2887 }
aoqi@0 2888 }
aoqi@0 2889 }
aoqi@0 2890
aoqi@0 2891 //step 2 - unbound lookup
aoqi@0 2892 Symbol origUnboundSym = null;
aoqi@0 2893 Symbol unboundSym = methodNotFound;
aoqi@0 2894 ReferenceLookupHelper unboundLookupHelper = null;
aoqi@0 2895 Env<AttrContext> unboundEnv = env.dup(env.tree, env.info.dup());
aoqi@0 2896 SearchResultKind unboundSearchResultKind = SearchResultKind.NOT_APPLICABLE_MATCH;
aoqi@0 2897 boolean staticErrorForUnbound = false;
aoqi@0 2898 if (isStaticSelector) {
aoqi@0 2899 unboundLookupHelper = boundLookupHelper.unboundLookup(inferenceContext);
aoqi@0 2900 MethodResolutionContext unboundSearchResolveContext =
aoqi@0 2901 new MethodResolutionContext();
aoqi@0 2902 unboundSearchResolveContext.methodCheck = methodCheck;
aoqi@0 2903 unboundSym = origUnboundSym = lookupMethod(unboundEnv, env.tree.pos(),
aoqi@0 2904 site.tsym, unboundSearchResolveContext, unboundLookupHelper);
aoqi@0 2905
aoqi@0 2906 if (unboundSym.kind != WRONG_MTH && unboundSym.kind != WRONG_MTHS) {
aoqi@0 2907 if (shouldCheckForStaticness) {
aoqi@0 2908 if (unboundSym.isStatic()) {
aoqi@0 2909 staticErrorForUnbound = true;
aoqi@0 2910 if (hasAnotherApplicableMethod(
aoqi@0 2911 unboundSearchResolveContext, unboundSym, false)) {
aoqi@0 2912 unboundSearchResultKind = SearchResultKind.BAD_MATCH_MORE_SPECIFIC;
aoqi@0 2913 } else {
aoqi@0 2914 unboundSearchResultKind = SearchResultKind.BAD_MATCH;
aoqi@0 2915 if (unboundSym.kind < ERRONEOUS) {
aoqi@0 2916 unboundSym = methodWithCorrectStaticnessNotFound;
aoqi@0 2917 }
aoqi@0 2918 }
aoqi@0 2919 } else if (unboundSym.kind < ERRONEOUS) {
aoqi@0 2920 unboundSearchResultKind = SearchResultKind.GOOD_MATCH;
aoqi@0 2921 }
aoqi@0 2922 }
aoqi@0 2923 }
aoqi@0 2924 }
aoqi@0 2925
aoqi@0 2926 //merge results
aoqi@0 2927 Pair<Symbol, ReferenceLookupHelper> res;
aoqi@0 2928 Symbol bestSym = choose(boundSym, unboundSym);
aoqi@0 2929 if (bestSym.kind < ERRONEOUS && (staticErrorForBound || staticErrorForUnbound)) {
aoqi@0 2930 if (staticErrorForBound) {
aoqi@0 2931 boundSym = methodWithCorrectStaticnessNotFound;
aoqi@0 2932 }
aoqi@0 2933 if (staticErrorForUnbound) {
aoqi@0 2934 unboundSym = methodWithCorrectStaticnessNotFound;
aoqi@0 2935 }
aoqi@0 2936 bestSym = choose(boundSym, unboundSym);
aoqi@0 2937 }
aoqi@0 2938 if (bestSym == methodWithCorrectStaticnessNotFound && mode == AttrMode.CHECK) {
aoqi@0 2939 Symbol symToPrint = origBoundSym;
aoqi@0 2940 String errorFragmentToPrint = "non-static.cant.be.ref";
aoqi@0 2941 if (staticErrorForBound && staticErrorForUnbound) {
aoqi@0 2942 if (unboundSearchResultKind == SearchResultKind.BAD_MATCH_MORE_SPECIFIC) {
aoqi@0 2943 symToPrint = origUnboundSym;
aoqi@0 2944 errorFragmentToPrint = "static.method.in.unbound.lookup";
aoqi@0 2945 }
aoqi@0 2946 } else {
aoqi@0 2947 if (!staticErrorForBound) {
aoqi@0 2948 symToPrint = origUnboundSym;
aoqi@0 2949 errorFragmentToPrint = "static.method.in.unbound.lookup";
aoqi@0 2950 }
aoqi@0 2951 }
aoqi@0 2952 log.error(referenceTree.expr.pos(), "invalid.mref",
aoqi@0 2953 Kinds.kindName(referenceTree.getMode()),
aoqi@0 2954 diags.fragment(errorFragmentToPrint,
aoqi@0 2955 Kinds.kindName(symToPrint), symToPrint));
aoqi@0 2956 }
aoqi@0 2957 res = new Pair<>(bestSym,
aoqi@0 2958 bestSym == unboundSym ? unboundLookupHelper : boundLookupHelper);
aoqi@0 2959 env.info.pendingResolutionPhase = bestSym == unboundSym ?
aoqi@0 2960 unboundEnv.info.pendingResolutionPhase :
aoqi@0 2961 boundEnv.info.pendingResolutionPhase;
aoqi@0 2962
aoqi@0 2963 return res;
aoqi@0 2964 }
aoqi@0 2965
aoqi@0 2966 enum SearchResultKind {
aoqi@0 2967 GOOD_MATCH, //type I
aoqi@0 2968 BAD_MATCH_MORE_SPECIFIC, //type II
aoqi@0 2969 BAD_MATCH, //type III
aoqi@0 2970 NOT_APPLICABLE_MATCH //type IV
aoqi@0 2971 }
aoqi@0 2972
aoqi@0 2973 boolean hasAnotherApplicableMethod(MethodResolutionContext resolutionContext,
aoqi@0 2974 Symbol bestSoFar, boolean staticMth) {
aoqi@0 2975 for (Candidate c : resolutionContext.candidates) {
aoqi@0 2976 if (resolutionContext.step != c.step ||
aoqi@0 2977 !c.isApplicable() ||
aoqi@0 2978 c.sym == bestSoFar) {
aoqi@0 2979 continue;
aoqi@0 2980 } else {
aoqi@0 2981 if (c.sym.isStatic() == staticMth) {
aoqi@0 2982 return true;
aoqi@0 2983 }
aoqi@0 2984 }
aoqi@0 2985 }
aoqi@0 2986 return false;
aoqi@0 2987 }
aoqi@0 2988
aoqi@0 2989 //where
aoqi@0 2990 private Symbol choose(Symbol boundSym, Symbol unboundSym) {
aoqi@0 2991 if (lookupSuccess(boundSym) && lookupSuccess(unboundSym)) {
aoqi@0 2992 return ambiguityError(boundSym, unboundSym);
aoqi@0 2993 } else if (lookupSuccess(boundSym) ||
aoqi@0 2994 (canIgnore(unboundSym) && !canIgnore(boundSym))) {
aoqi@0 2995 return boundSym;
aoqi@0 2996 } else if (lookupSuccess(unboundSym) ||
aoqi@0 2997 (canIgnore(boundSym) && !canIgnore(unboundSym))) {
aoqi@0 2998 return unboundSym;
aoqi@0 2999 } else {
aoqi@0 3000 return boundSym;
aoqi@0 3001 }
aoqi@0 3002 }
aoqi@0 3003
aoqi@0 3004 private boolean lookupSuccess(Symbol s) {
aoqi@0 3005 return s.kind == MTH || s.kind == AMBIGUOUS;
aoqi@0 3006 }
aoqi@0 3007
aoqi@0 3008 private boolean canIgnore(Symbol s) {
aoqi@0 3009 switch (s.kind) {
aoqi@0 3010 case ABSENT_MTH:
aoqi@0 3011 return true;
aoqi@0 3012 case WRONG_MTH:
aoqi@0 3013 InapplicableSymbolError errSym =
aoqi@0 3014 (InapplicableSymbolError)s.baseSymbol();
aoqi@0 3015 return new Template(MethodCheckDiag.ARITY_MISMATCH.regex())
aoqi@0 3016 .matches(errSym.errCandidate().snd);
aoqi@0 3017 case WRONG_MTHS:
aoqi@0 3018 InapplicableSymbolsError errSyms =
aoqi@0 3019 (InapplicableSymbolsError)s.baseSymbol();
aoqi@0 3020 return errSyms.filterCandidates(errSyms.mapCandidates()).isEmpty();
aoqi@0 3021 case WRONG_STATICNESS:
aoqi@0 3022 return false;
aoqi@0 3023 default:
aoqi@0 3024 return false;
aoqi@0 3025 }
aoqi@0 3026 }
aoqi@0 3027
aoqi@0 3028 /**
aoqi@0 3029 * Helper for defining custom method-like lookup logic; a lookup helper
aoqi@0 3030 * provides hooks for (i) the actual lookup logic and (ii) accessing the
aoqi@0 3031 * lookup result (this step might result in compiler diagnostics to be generated)
aoqi@0 3032 */
aoqi@0 3033 abstract class LookupHelper {
aoqi@0 3034
aoqi@0 3035 /** name of the symbol to lookup */
aoqi@0 3036 Name name;
aoqi@0 3037
aoqi@0 3038 /** location in which the lookup takes place */
aoqi@0 3039 Type site;
aoqi@0 3040
aoqi@0 3041 /** actual types used during the lookup */
aoqi@0 3042 List<Type> argtypes;
aoqi@0 3043
aoqi@0 3044 /** type arguments used during the lookup */
aoqi@0 3045 List<Type> typeargtypes;
aoqi@0 3046
aoqi@0 3047 /** Max overload resolution phase handled by this helper */
aoqi@0 3048 MethodResolutionPhase maxPhase;
aoqi@0 3049
aoqi@0 3050 LookupHelper(Name name, Type site, List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
aoqi@0 3051 this.name = name;
aoqi@0 3052 this.site = site;
aoqi@0 3053 this.argtypes = argtypes;
aoqi@0 3054 this.typeargtypes = typeargtypes;
aoqi@0 3055 this.maxPhase = maxPhase;
aoqi@0 3056 }
aoqi@0 3057
aoqi@0 3058 /**
aoqi@0 3059 * Should lookup stop at given phase with given result
aoqi@0 3060 */
mcimadamore@2559 3061 final boolean shouldStop(Symbol sym, MethodResolutionPhase phase) {
aoqi@0 3062 return phase.ordinal() > maxPhase.ordinal() ||
aoqi@0 3063 sym.kind < ERRONEOUS || sym.kind == AMBIGUOUS;
aoqi@0 3064 }
aoqi@0 3065
aoqi@0 3066 /**
aoqi@0 3067 * Search for a symbol under a given overload resolution phase - this method
aoqi@0 3068 * is usually called several times, once per each overload resolution phase
aoqi@0 3069 */
aoqi@0 3070 abstract Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase);
aoqi@0 3071
aoqi@0 3072 /**
aoqi@0 3073 * Dump overload resolution info
aoqi@0 3074 */
aoqi@0 3075 void debug(DiagnosticPosition pos, Symbol sym) {
aoqi@0 3076 //do nothing
aoqi@0 3077 }
aoqi@0 3078
aoqi@0 3079 /**
aoqi@0 3080 * Validate the result of the lookup
aoqi@0 3081 */
aoqi@0 3082 abstract Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym);
aoqi@0 3083 }
aoqi@0 3084
aoqi@0 3085 abstract class BasicLookupHelper extends LookupHelper {
aoqi@0 3086
aoqi@0 3087 BasicLookupHelper(Name name, Type site, List<Type> argtypes, List<Type> typeargtypes) {
aoqi@0 3088 this(name, site, argtypes, typeargtypes, MethodResolutionPhase.VARARITY);
aoqi@0 3089 }
aoqi@0 3090
aoqi@0 3091 BasicLookupHelper(Name name, Type site, List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
aoqi@0 3092 super(name, site, argtypes, typeargtypes, maxPhase);
aoqi@0 3093 }
aoqi@0 3094
aoqi@0 3095 @Override
aoqi@0 3096 final Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
aoqi@0 3097 Symbol sym = doLookup(env, phase);
aoqi@0 3098 if (sym.kind == AMBIGUOUS) {
aoqi@0 3099 AmbiguityError a_err = (AmbiguityError)sym.baseSymbol();
aoqi@0 3100 sym = a_err.mergeAbstracts(site);
aoqi@0 3101 }
aoqi@0 3102 return sym;
aoqi@0 3103 }
aoqi@0 3104
aoqi@0 3105 abstract Symbol doLookup(Env<AttrContext> env, MethodResolutionPhase phase);
aoqi@0 3106
aoqi@0 3107 @Override
aoqi@0 3108 Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym) {
aoqi@0 3109 if (sym.kind >= AMBIGUOUS) {
aoqi@0 3110 //if nothing is found return the 'first' error
aoqi@0 3111 sym = accessMethod(sym, pos, location, site, name, true, argtypes, typeargtypes);
aoqi@0 3112 }
aoqi@0 3113 return sym;
aoqi@0 3114 }
aoqi@0 3115
aoqi@0 3116 @Override
aoqi@0 3117 void debug(DiagnosticPosition pos, Symbol sym) {
aoqi@0 3118 reportVerboseResolutionDiagnostic(pos, name, site, argtypes, typeargtypes, sym);
aoqi@0 3119 }
aoqi@0 3120 }
aoqi@0 3121
aoqi@0 3122 /**
aoqi@0 3123 * Helper class for member reference lookup. A reference lookup helper
aoqi@0 3124 * defines the basic logic for member reference lookup; a method gives
aoqi@0 3125 * access to an 'unbound' helper used to perform an unbound member
aoqi@0 3126 * reference lookup.
aoqi@0 3127 */
aoqi@0 3128 abstract class ReferenceLookupHelper extends LookupHelper {
aoqi@0 3129
aoqi@0 3130 /** The member reference tree */
aoqi@0 3131 JCMemberReference referenceTree;
aoqi@0 3132
aoqi@0 3133 ReferenceLookupHelper(JCMemberReference referenceTree, Name name, Type site,
aoqi@0 3134 List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
aoqi@0 3135 super(name, site, argtypes, typeargtypes, maxPhase);
aoqi@0 3136 this.referenceTree = referenceTree;
aoqi@0 3137 }
aoqi@0 3138
aoqi@0 3139 /**
aoqi@0 3140 * Returns an unbound version of this lookup helper. By default, this
aoqi@0 3141 * method returns an dummy lookup helper.
aoqi@0 3142 */
aoqi@0 3143 ReferenceLookupHelper unboundLookup(InferenceContext inferenceContext) {
aoqi@0 3144 //dummy loopkup helper that always return 'methodNotFound'
aoqi@0 3145 return new ReferenceLookupHelper(referenceTree, name, site, argtypes, typeargtypes, maxPhase) {
aoqi@0 3146 @Override
aoqi@0 3147 ReferenceLookupHelper unboundLookup(InferenceContext inferenceContext) {
aoqi@0 3148 return this;
aoqi@0 3149 }
aoqi@0 3150 @Override
aoqi@0 3151 Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
aoqi@0 3152 return methodNotFound;
aoqi@0 3153 }
aoqi@0 3154 @Override
aoqi@0 3155 ReferenceKind referenceKind(Symbol sym) {
aoqi@0 3156 Assert.error();
aoqi@0 3157 return null;
aoqi@0 3158 }
aoqi@0 3159 };
aoqi@0 3160 }
aoqi@0 3161
aoqi@0 3162 /**
aoqi@0 3163 * Get the kind of the member reference
aoqi@0 3164 */
aoqi@0 3165 abstract JCMemberReference.ReferenceKind referenceKind(Symbol sym);
aoqi@0 3166
aoqi@0 3167 Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym) {
aoqi@0 3168 if (sym.kind == AMBIGUOUS) {
aoqi@0 3169 AmbiguityError a_err = (AmbiguityError)sym.baseSymbol();
aoqi@0 3170 sym = a_err.mergeAbstracts(site);
aoqi@0 3171 }
aoqi@0 3172 //skip error reporting
aoqi@0 3173 return sym;
aoqi@0 3174 }
aoqi@0 3175 }
aoqi@0 3176
aoqi@0 3177 /**
aoqi@0 3178 * Helper class for method reference lookup. The lookup logic is based
aoqi@0 3179 * upon Resolve.findMethod; in certain cases, this helper class has a
aoqi@0 3180 * corresponding unbound helper class (see UnboundMethodReferenceLookupHelper).
aoqi@0 3181 * In such cases, non-static lookup results are thrown away.
aoqi@0 3182 */
aoqi@0 3183 class MethodReferenceLookupHelper extends ReferenceLookupHelper {
aoqi@0 3184
aoqi@0 3185 MethodReferenceLookupHelper(JCMemberReference referenceTree, Name name, Type site,
aoqi@0 3186 List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
aoqi@0 3187 super(referenceTree, name, site, argtypes, typeargtypes, maxPhase);
aoqi@0 3188 }
aoqi@0 3189
aoqi@0 3190 @Override
aoqi@0 3191 final Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
aoqi@0 3192 return findMethod(env, site, name, argtypes, typeargtypes,
aoqi@0 3193 phase.isBoxingRequired(), phase.isVarargsRequired(), syms.operatorNames.contains(name));
aoqi@0 3194 }
aoqi@0 3195
aoqi@0 3196 @Override
aoqi@0 3197 ReferenceLookupHelper unboundLookup(InferenceContext inferenceContext) {
aoqi@0 3198 if (TreeInfo.isStaticSelector(referenceTree.expr, names) &&
aoqi@0 3199 argtypes.nonEmpty() &&
aoqi@0 3200 (argtypes.head.hasTag(NONE) ||
aoqi@0 3201 types.isSubtypeUnchecked(inferenceContext.asUndetVar(argtypes.head), site))) {
aoqi@0 3202 return new UnboundMethodReferenceLookupHelper(referenceTree, name,
aoqi@0 3203 site, argtypes, typeargtypes, maxPhase);
aoqi@0 3204 } else {
aoqi@0 3205 return super.unboundLookup(inferenceContext);
aoqi@0 3206 }
aoqi@0 3207 }
aoqi@0 3208
aoqi@0 3209 @Override
aoqi@0 3210 ReferenceKind referenceKind(Symbol sym) {
aoqi@0 3211 if (sym.isStatic()) {
aoqi@0 3212 return ReferenceKind.STATIC;
aoqi@0 3213 } else {
aoqi@0 3214 Name selName = TreeInfo.name(referenceTree.getQualifierExpression());
aoqi@0 3215 return selName != null && selName == names._super ?
aoqi@0 3216 ReferenceKind.SUPER :
aoqi@0 3217 ReferenceKind.BOUND;
aoqi@0 3218 }
aoqi@0 3219 }
aoqi@0 3220 }
aoqi@0 3221
aoqi@0 3222 /**
aoqi@0 3223 * Helper class for unbound method reference lookup. Essentially the same
aoqi@0 3224 * as the basic method reference lookup helper; main difference is that static
aoqi@0 3225 * lookup results are thrown away. If qualifier type is raw, an attempt to
aoqi@0 3226 * infer a parameterized type is made using the first actual argument (that
aoqi@0 3227 * would otherwise be ignored during the lookup).
aoqi@0 3228 */
aoqi@0 3229 class UnboundMethodReferenceLookupHelper extends MethodReferenceLookupHelper {
aoqi@0 3230
aoqi@0 3231 UnboundMethodReferenceLookupHelper(JCMemberReference referenceTree, Name name, Type site,
aoqi@0 3232 List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
aoqi@0 3233 super(referenceTree, name, site, argtypes.tail, typeargtypes, maxPhase);
aoqi@0 3234 if (site.isRaw() && !argtypes.head.hasTag(NONE)) {
aoqi@0 3235 Type asSuperSite = types.asSuper(argtypes.head, site.tsym);
vromero@2611 3236 this.site = types.capture(asSuperSite);
aoqi@0 3237 }
aoqi@0 3238 }
aoqi@0 3239
aoqi@0 3240 @Override
aoqi@0 3241 ReferenceLookupHelper unboundLookup(InferenceContext inferenceContext) {
aoqi@0 3242 return this;
aoqi@0 3243 }
aoqi@0 3244
aoqi@0 3245 @Override
aoqi@0 3246 ReferenceKind referenceKind(Symbol sym) {
aoqi@0 3247 return ReferenceKind.UNBOUND;
aoqi@0 3248 }
aoqi@0 3249 }
aoqi@0 3250
aoqi@0 3251 /**
aoqi@0 3252 * Helper class for array constructor lookup; an array constructor lookup
aoqi@0 3253 * is simulated by looking up a method that returns the array type specified
aoqi@0 3254 * as qualifier, and that accepts a single int parameter (size of the array).
aoqi@0 3255 */
aoqi@0 3256 class ArrayConstructorReferenceLookupHelper extends ReferenceLookupHelper {
aoqi@0 3257
aoqi@0 3258 ArrayConstructorReferenceLookupHelper(JCMemberReference referenceTree, Type site, List<Type> argtypes,
aoqi@0 3259 List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
aoqi@0 3260 super(referenceTree, names.init, site, argtypes, typeargtypes, maxPhase);
aoqi@0 3261 }
aoqi@0 3262
aoqi@0 3263 @Override
aoqi@0 3264 protected Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
aoqi@0 3265 Scope sc = new Scope(syms.arrayClass);
aoqi@0 3266 MethodSymbol arrayConstr = new MethodSymbol(PUBLIC, name, null, site.tsym);
aoqi@0 3267 arrayConstr.type = new MethodType(List.<Type>of(syms.intType), site, List.<Type>nil(), syms.methodClass);
aoqi@0 3268 sc.enter(arrayConstr);
aoqi@0 3269 return findMethodInScope(env, site, name, argtypes, typeargtypes, sc, methodNotFound, phase.isBoxingRequired(), phase.isVarargsRequired(), false, false);
aoqi@0 3270 }
aoqi@0 3271
aoqi@0 3272 @Override
aoqi@0 3273 ReferenceKind referenceKind(Symbol sym) {
aoqi@0 3274 return ReferenceKind.ARRAY_CTOR;
aoqi@0 3275 }
aoqi@0 3276 }
aoqi@0 3277
aoqi@0 3278 /**
aoqi@0 3279 * Helper class for constructor reference lookup. The lookup logic is based
aoqi@0 3280 * upon either Resolve.findMethod or Resolve.findDiamond - depending on
aoqi@0 3281 * whether the constructor reference needs diamond inference (this is the case
aoqi@0 3282 * if the qualifier type is raw). A special erroneous symbol is returned
aoqi@0 3283 * if the lookup returns the constructor of an inner class and there's no
aoqi@0 3284 * enclosing instance in scope.
aoqi@0 3285 */
aoqi@0 3286 class ConstructorReferenceLookupHelper extends ReferenceLookupHelper {
aoqi@0 3287
aoqi@0 3288 boolean needsInference;
aoqi@0 3289
aoqi@0 3290 ConstructorReferenceLookupHelper(JCMemberReference referenceTree, Type site, List<Type> argtypes,
aoqi@0 3291 List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
aoqi@0 3292 super(referenceTree, names.init, site, argtypes, typeargtypes, maxPhase);
aoqi@0 3293 if (site.isRaw()) {
aoqi@0 3294 this.site = new ClassType(site.getEnclosingType(), site.tsym.type.getTypeArguments(), site.tsym);
aoqi@0 3295 needsInference = true;
aoqi@0 3296 }
aoqi@0 3297 }
aoqi@0 3298
aoqi@0 3299 @Override
aoqi@0 3300 protected Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
aoqi@0 3301 Symbol sym = needsInference ?
aoqi@0 3302 findDiamond(env, site, argtypes, typeargtypes, phase.isBoxingRequired(), phase.isVarargsRequired()) :
aoqi@0 3303 findMethod(env, site, name, argtypes, typeargtypes,
aoqi@0 3304 phase.isBoxingRequired(), phase.isVarargsRequired(), syms.operatorNames.contains(name));
aoqi@0 3305 return sym.kind != MTH ||
aoqi@0 3306 site.getEnclosingType().hasTag(NONE) ||
aoqi@0 3307 hasEnclosingInstance(env, site) ?
aoqi@0 3308 sym : new InvalidSymbolError(Kinds.MISSING_ENCL, sym, null) {
aoqi@0 3309 @Override
aoqi@0 3310 JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos, Symbol location, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) {
aoqi@0 3311 return diags.create(dkind, log.currentSource(), pos,
aoqi@0 3312 "cant.access.inner.cls.constr", site.tsym.name, argtypes, site.getEnclosingType());
aoqi@0 3313 }
aoqi@0 3314 };
aoqi@0 3315 }
aoqi@0 3316
aoqi@0 3317 @Override
aoqi@0 3318 ReferenceKind referenceKind(Symbol sym) {
aoqi@0 3319 return site.getEnclosingType().hasTag(NONE) ?
aoqi@0 3320 ReferenceKind.TOPLEVEL : ReferenceKind.IMPLICIT_INNER;
aoqi@0 3321 }
aoqi@0 3322 }
aoqi@0 3323
aoqi@0 3324 /**
aoqi@0 3325 * Main overload resolution routine. On each overload resolution step, a
aoqi@0 3326 * lookup helper class is used to perform the method/constructor lookup;
aoqi@0 3327 * at the end of the lookup, the helper is used to validate the results
aoqi@0 3328 * (this last step might trigger overload resolution diagnostics).
aoqi@0 3329 */
aoqi@0 3330 Symbol lookupMethod(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, MethodCheck methodCheck, LookupHelper lookupHelper) {
aoqi@0 3331 MethodResolutionContext resolveContext = new MethodResolutionContext();
aoqi@0 3332 resolveContext.methodCheck = methodCheck;
aoqi@0 3333 return lookupMethod(env, pos, location, resolveContext, lookupHelper);
aoqi@0 3334 }
aoqi@0 3335
aoqi@0 3336 Symbol lookupMethod(Env<AttrContext> env, DiagnosticPosition pos, Symbol location,
aoqi@0 3337 MethodResolutionContext resolveContext, LookupHelper lookupHelper) {
aoqi@0 3338 MethodResolutionContext prevResolutionContext = currentResolutionContext;
aoqi@0 3339 try {
aoqi@0 3340 Symbol bestSoFar = methodNotFound;
aoqi@0 3341 currentResolutionContext = resolveContext;
aoqi@0 3342 for (MethodResolutionPhase phase : methodResolutionSteps) {
aoqi@0 3343 if (!phase.isApplicable(boxingEnabled, varargsEnabled) ||
aoqi@0 3344 lookupHelper.shouldStop(bestSoFar, phase)) break;
aoqi@0 3345 MethodResolutionPhase prevPhase = currentResolutionContext.step;
aoqi@0 3346 Symbol prevBest = bestSoFar;
aoqi@0 3347 currentResolutionContext.step = phase;
aoqi@0 3348 Symbol sym = lookupHelper.lookup(env, phase);
aoqi@0 3349 lookupHelper.debug(pos, sym);
aoqi@0 3350 bestSoFar = phase.mergeResults(bestSoFar, sym);
aoqi@0 3351 env.info.pendingResolutionPhase = (prevBest == bestSoFar) ? prevPhase : phase;
aoqi@0 3352 }
aoqi@0 3353 return lookupHelper.access(env, pos, location, bestSoFar);
aoqi@0 3354 } finally {
aoqi@0 3355 currentResolutionContext = prevResolutionContext;
aoqi@0 3356 }
aoqi@0 3357 }
aoqi@0 3358
aoqi@0 3359 /**
aoqi@0 3360 * Resolve `c.name' where name == this or name == super.
aoqi@0 3361 * @param pos The position to use for error reporting.
aoqi@0 3362 * @param env The environment current at the expression.
aoqi@0 3363 * @param c The qualifier.
aoqi@0 3364 * @param name The identifier's name.
aoqi@0 3365 */
aoqi@0 3366 Symbol resolveSelf(DiagnosticPosition pos,
aoqi@0 3367 Env<AttrContext> env,
aoqi@0 3368 TypeSymbol c,
aoqi@0 3369 Name name) {
aoqi@0 3370 Env<AttrContext> env1 = env;
aoqi@0 3371 boolean staticOnly = false;
aoqi@0 3372 while (env1.outer != null) {
aoqi@0 3373 if (isStatic(env1)) staticOnly = true;
aoqi@0 3374 if (env1.enclClass.sym == c) {
aoqi@0 3375 Symbol sym = env1.info.scope.lookup(name).sym;
aoqi@0 3376 if (sym != null) {
aoqi@0 3377 if (staticOnly) sym = new StaticError(sym);
aoqi@0 3378 return accessBase(sym, pos, env.enclClass.sym.type,
aoqi@0 3379 name, true);
aoqi@0 3380 }
aoqi@0 3381 }
aoqi@0 3382 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
aoqi@0 3383 env1 = env1.outer;
aoqi@0 3384 }
aoqi@0 3385 if (c.isInterface() &&
aoqi@0 3386 name == names._super && !isStatic(env) &&
aoqi@0 3387 types.isDirectSuperInterface(c, env.enclClass.sym)) {
aoqi@0 3388 //this might be a default super call if one of the superinterfaces is 'c'
aoqi@0 3389 for (Type t : pruneInterfaces(env.enclClass.type)) {
aoqi@0 3390 if (t.tsym == c) {
aoqi@0 3391 env.info.defaultSuperCallSite = t;
aoqi@0 3392 return new VarSymbol(0, names._super,
aoqi@0 3393 types.asSuper(env.enclClass.type, c), env.enclClass.sym);
aoqi@0 3394 }
aoqi@0 3395 }
aoqi@0 3396 //find a direct superinterface that is a subtype of 'c'
aoqi@0 3397 for (Type i : types.interfaces(env.enclClass.type)) {
aoqi@0 3398 if (i.tsym.isSubClass(c, types) && i.tsym != c) {
aoqi@0 3399 log.error(pos, "illegal.default.super.call", c,
aoqi@0 3400 diags.fragment("redundant.supertype", c, i));
aoqi@0 3401 return syms.errSymbol;
aoqi@0 3402 }
aoqi@0 3403 }
aoqi@0 3404 Assert.error();
aoqi@0 3405 }
aoqi@0 3406 log.error(pos, "not.encl.class", c);
aoqi@0 3407 return syms.errSymbol;
aoqi@0 3408 }
aoqi@0 3409 //where
aoqi@0 3410 private List<Type> pruneInterfaces(Type t) {
aoqi@0 3411 ListBuffer<Type> result = new ListBuffer<>();
aoqi@0 3412 for (Type t1 : types.interfaces(t)) {
aoqi@0 3413 boolean shouldAdd = true;
aoqi@0 3414 for (Type t2 : types.interfaces(t)) {
aoqi@0 3415 if (t1 != t2 && types.isSubtypeNoCapture(t2, t1)) {
aoqi@0 3416 shouldAdd = false;
aoqi@0 3417 }
aoqi@0 3418 }
aoqi@0 3419 if (shouldAdd) {
aoqi@0 3420 result.append(t1);
aoqi@0 3421 }
aoqi@0 3422 }
aoqi@0 3423 return result.toList();
aoqi@0 3424 }
aoqi@0 3425
aoqi@0 3426
aoqi@0 3427 /**
aoqi@0 3428 * Resolve `c.this' for an enclosing class c that contains the
aoqi@0 3429 * named member.
aoqi@0 3430 * @param pos The position to use for error reporting.
aoqi@0 3431 * @param env The environment current at the expression.
aoqi@0 3432 * @param member The member that must be contained in the result.
aoqi@0 3433 */
aoqi@0 3434 Symbol resolveSelfContaining(DiagnosticPosition pos,
aoqi@0 3435 Env<AttrContext> env,
aoqi@0 3436 Symbol member,
aoqi@0 3437 boolean isSuperCall) {
aoqi@0 3438 Symbol sym = resolveSelfContainingInternal(env, member, isSuperCall);
aoqi@0 3439 if (sym == null) {
aoqi@0 3440 log.error(pos, "encl.class.required", member);
aoqi@0 3441 return syms.errSymbol;
aoqi@0 3442 } else {
aoqi@0 3443 return accessBase(sym, pos, env.enclClass.sym.type, sym.name, true);
aoqi@0 3444 }
aoqi@0 3445 }
aoqi@0 3446
aoqi@0 3447 boolean hasEnclosingInstance(Env<AttrContext> env, Type type) {
aoqi@0 3448 Symbol encl = resolveSelfContainingInternal(env, type.tsym, false);
aoqi@0 3449 return encl != null && encl.kind < ERRONEOUS;
aoqi@0 3450 }
aoqi@0 3451
aoqi@0 3452 private Symbol resolveSelfContainingInternal(Env<AttrContext> env,
aoqi@0 3453 Symbol member,
aoqi@0 3454 boolean isSuperCall) {
aoqi@0 3455 Name name = names._this;
aoqi@0 3456 Env<AttrContext> env1 = isSuperCall ? env.outer : env;
aoqi@0 3457 boolean staticOnly = false;
aoqi@0 3458 if (env1 != null) {
aoqi@0 3459 while (env1 != null && env1.outer != null) {
aoqi@0 3460 if (isStatic(env1)) staticOnly = true;
aoqi@0 3461 if (env1.enclClass.sym.isSubClass(member.owner, types)) {
aoqi@0 3462 Symbol sym = env1.info.scope.lookup(name).sym;
aoqi@0 3463 if (sym != null) {
aoqi@0 3464 if (staticOnly) sym = new StaticError(sym);
aoqi@0 3465 return sym;
aoqi@0 3466 }
aoqi@0 3467 }
aoqi@0 3468 if ((env1.enclClass.sym.flags() & STATIC) != 0)
aoqi@0 3469 staticOnly = true;
aoqi@0 3470 env1 = env1.outer;
aoqi@0 3471 }
aoqi@0 3472 }
aoqi@0 3473 return null;
aoqi@0 3474 }
aoqi@0 3475
aoqi@0 3476 /**
aoqi@0 3477 * Resolve an appropriate implicit this instance for t's container.
aoqi@0 3478 * JLS 8.8.5.1 and 15.9.2
aoqi@0 3479 */
aoqi@0 3480 Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t) {
aoqi@0 3481 return resolveImplicitThis(pos, env, t, false);
aoqi@0 3482 }
aoqi@0 3483
aoqi@0 3484 Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t, boolean isSuperCall) {
aoqi@0 3485 Type thisType = (((t.tsym.owner.kind & (MTH|VAR)) != 0)
aoqi@0 3486 ? resolveSelf(pos, env, t.getEnclosingType().tsym, names._this)
aoqi@0 3487 : resolveSelfContaining(pos, env, t.tsym, isSuperCall)).type;
aoqi@0 3488 if (env.info.isSelfCall && thisType.tsym == env.enclClass.sym)
aoqi@0 3489 log.error(pos, "cant.ref.before.ctor.called", "this");
aoqi@0 3490 return thisType;
aoqi@0 3491 }
aoqi@0 3492
aoqi@0 3493 /* ***************************************************************************
aoqi@0 3494 * ResolveError classes, indicating error situations when accessing symbols
aoqi@0 3495 ****************************************************************************/
aoqi@0 3496
aoqi@0 3497 //used by TransTypes when checking target type of synthetic cast
aoqi@0 3498 public void logAccessErrorInternal(Env<AttrContext> env, JCTree tree, Type type) {
aoqi@0 3499 AccessError error = new AccessError(env, env.enclClass.type, type.tsym);
aoqi@0 3500 logResolveError(error, tree.pos(), env.enclClass.sym, env.enclClass.type, null, null, null);
aoqi@0 3501 }
aoqi@0 3502 //where
aoqi@0 3503 private void logResolveError(ResolveError error,
aoqi@0 3504 DiagnosticPosition pos,
aoqi@0 3505 Symbol location,
aoqi@0 3506 Type site,
aoqi@0 3507 Name name,
aoqi@0 3508 List<Type> argtypes,
aoqi@0 3509 List<Type> typeargtypes) {
aoqi@0 3510 JCDiagnostic d = error.getDiagnostic(JCDiagnostic.DiagnosticType.ERROR,
aoqi@0 3511 pos, location, site, name, argtypes, typeargtypes);
aoqi@0 3512 if (d != null) {
aoqi@0 3513 d.setFlag(DiagnosticFlag.RESOLVE_ERROR);
aoqi@0 3514 log.report(d);
aoqi@0 3515 }
aoqi@0 3516 }
aoqi@0 3517
aoqi@0 3518 private final LocalizedString noArgs = new LocalizedString("compiler.misc.no.args");
aoqi@0 3519
aoqi@0 3520 public Object methodArguments(List<Type> argtypes) {
aoqi@0 3521 if (argtypes == null || argtypes.isEmpty()) {
aoqi@0 3522 return noArgs;
aoqi@0 3523 } else {
aoqi@0 3524 ListBuffer<Object> diagArgs = new ListBuffer<>();
aoqi@0 3525 for (Type t : argtypes) {
aoqi@0 3526 if (t.hasTag(DEFERRED)) {
aoqi@0 3527 diagArgs.append(((DeferredAttr.DeferredType)t).tree);
aoqi@0 3528 } else {
aoqi@0 3529 diagArgs.append(t);
aoqi@0 3530 }
aoqi@0 3531 }
aoqi@0 3532 return diagArgs;
aoqi@0 3533 }
aoqi@0 3534 }
aoqi@0 3535
aoqi@0 3536 /**
aoqi@0 3537 * Root class for resolution errors. Subclass of ResolveError
aoqi@0 3538 * represent a different kinds of resolution error - as such they must
aoqi@0 3539 * specify how they map into concrete compiler diagnostics.
aoqi@0 3540 */
aoqi@0 3541 abstract class ResolveError extends Symbol {
aoqi@0 3542
aoqi@0 3543 /** The name of the kind of error, for debugging only. */
aoqi@0 3544 final String debugName;
aoqi@0 3545
aoqi@0 3546 ResolveError(int kind, String debugName) {
aoqi@0 3547 super(kind, 0, null, null, null);
aoqi@0 3548 this.debugName = debugName;
aoqi@0 3549 }
aoqi@0 3550
aoqi@0 3551 @Override
aoqi@0 3552 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
aoqi@0 3553 throw new AssertionError();
aoqi@0 3554 }
aoqi@0 3555
aoqi@0 3556 @Override
aoqi@0 3557 public String toString() {
aoqi@0 3558 return debugName;
aoqi@0 3559 }
aoqi@0 3560
aoqi@0 3561 @Override
aoqi@0 3562 public boolean exists() {
aoqi@0 3563 return false;
aoqi@0 3564 }
aoqi@0 3565
aoqi@0 3566 @Override
aoqi@0 3567 public boolean isStatic() {
aoqi@0 3568 return false;
aoqi@0 3569 }
aoqi@0 3570
aoqi@0 3571 /**
aoqi@0 3572 * Create an external representation for this erroneous symbol to be
aoqi@0 3573 * used during attribution - by default this returns the symbol of a
aoqi@0 3574 * brand new error type which stores the original type found
aoqi@0 3575 * during resolution.
aoqi@0 3576 *
aoqi@0 3577 * @param name the name used during resolution
aoqi@0 3578 * @param location the location from which the symbol is accessed
aoqi@0 3579 */
aoqi@0 3580 protected Symbol access(Name name, TypeSymbol location) {
aoqi@0 3581 return types.createErrorType(name, location, syms.errSymbol.type).tsym;
aoqi@0 3582 }
aoqi@0 3583
aoqi@0 3584 /**
aoqi@0 3585 * Create a diagnostic representing this resolution error.
aoqi@0 3586 *
aoqi@0 3587 * @param dkind The kind of the diagnostic to be created (e.g error).
aoqi@0 3588 * @param pos The position to be used for error reporting.
aoqi@0 3589 * @param site The original type from where the selection took place.
aoqi@0 3590 * @param name The name of the symbol to be resolved.
aoqi@0 3591 * @param argtypes The invocation's value arguments,
aoqi@0 3592 * if we looked for a method.
aoqi@0 3593 * @param typeargtypes The invocation's type arguments,
aoqi@0 3594 * if we looked for a method.
aoqi@0 3595 */
aoqi@0 3596 abstract JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
aoqi@0 3597 DiagnosticPosition pos,
aoqi@0 3598 Symbol location,
aoqi@0 3599 Type site,
aoqi@0 3600 Name name,
aoqi@0 3601 List<Type> argtypes,
aoqi@0 3602 List<Type> typeargtypes);
aoqi@0 3603 }
aoqi@0 3604
aoqi@0 3605 /**
aoqi@0 3606 * This class is the root class of all resolution errors caused by
aoqi@0 3607 * an invalid symbol being found during resolution.
aoqi@0 3608 */
aoqi@0 3609 abstract class InvalidSymbolError extends ResolveError {
aoqi@0 3610
aoqi@0 3611 /** The invalid symbol found during resolution */
aoqi@0 3612 Symbol sym;
aoqi@0 3613
aoqi@0 3614 InvalidSymbolError(int kind, Symbol sym, String debugName) {
aoqi@0 3615 super(kind, debugName);
aoqi@0 3616 this.sym = sym;
aoqi@0 3617 }
aoqi@0 3618
aoqi@0 3619 @Override
aoqi@0 3620 public boolean exists() {
aoqi@0 3621 return true;
aoqi@0 3622 }
aoqi@0 3623
aoqi@0 3624 @Override
aoqi@0 3625 public String toString() {
aoqi@0 3626 return super.toString() + " wrongSym=" + sym;
aoqi@0 3627 }
aoqi@0 3628
aoqi@0 3629 @Override
aoqi@0 3630 public Symbol access(Name name, TypeSymbol location) {
aoqi@0 3631 if ((sym.kind & ERRONEOUS) == 0 && (sym.kind & TYP) != 0)
aoqi@0 3632 return types.createErrorType(name, location, sym.type).tsym;
aoqi@0 3633 else
aoqi@0 3634 return sym;
aoqi@0 3635 }
aoqi@0 3636 }
aoqi@0 3637
aoqi@0 3638 /**
aoqi@0 3639 * InvalidSymbolError error class indicating that a symbol matching a
aoqi@0 3640 * given name does not exists in a given site.
aoqi@0 3641 */
aoqi@0 3642 class SymbolNotFoundError extends ResolveError {
aoqi@0 3643
aoqi@0 3644 SymbolNotFoundError(int kind) {
aoqi@0 3645 this(kind, "symbol not found error");
aoqi@0 3646 }
aoqi@0 3647
aoqi@0 3648 SymbolNotFoundError(int kind, String debugName) {
aoqi@0 3649 super(kind, debugName);
aoqi@0 3650 }
aoqi@0 3651
aoqi@0 3652 @Override
aoqi@0 3653 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
aoqi@0 3654 DiagnosticPosition pos,
aoqi@0 3655 Symbol location,
aoqi@0 3656 Type site,
aoqi@0 3657 Name name,
aoqi@0 3658 List<Type> argtypes,
aoqi@0 3659 List<Type> typeargtypes) {
aoqi@0 3660 argtypes = argtypes == null ? List.<Type>nil() : argtypes;
aoqi@0 3661 typeargtypes = typeargtypes == null ? List.<Type>nil() : typeargtypes;
aoqi@0 3662 if (name == names.error)
aoqi@0 3663 return null;
aoqi@0 3664
aoqi@0 3665 if (syms.operatorNames.contains(name)) {
aoqi@0 3666 boolean isUnaryOp = argtypes.size() == 1;
aoqi@0 3667 String key = argtypes.size() == 1 ?
aoqi@0 3668 "operator.cant.be.applied" :
aoqi@0 3669 "operator.cant.be.applied.1";
aoqi@0 3670 Type first = argtypes.head;
aoqi@0 3671 Type second = !isUnaryOp ? argtypes.tail.head : null;
aoqi@0 3672 return diags.create(dkind, log.currentSource(), pos,
aoqi@0 3673 key, name, first, second);
aoqi@0 3674 }
aoqi@0 3675 boolean hasLocation = false;
aoqi@0 3676 if (location == null) {
aoqi@0 3677 location = site.tsym;
aoqi@0 3678 }
aoqi@0 3679 if (!location.name.isEmpty()) {
aoqi@0 3680 if (location.kind == PCK && !site.tsym.exists()) {
aoqi@0 3681 return diags.create(dkind, log.currentSource(), pos,
aoqi@0 3682 "doesnt.exist", location);
aoqi@0 3683 }
aoqi@0 3684 hasLocation = !location.name.equals(names._this) &&
aoqi@0 3685 !location.name.equals(names._super);
aoqi@0 3686 }
aoqi@0 3687 boolean isConstructor = (kind == ABSENT_MTH || kind == WRONG_STATICNESS) &&
aoqi@0 3688 name == names.init;
aoqi@0 3689 KindName kindname = isConstructor ? KindName.CONSTRUCTOR : absentKind(kind);
aoqi@0 3690 Name idname = isConstructor ? site.tsym.name : name;
aoqi@0 3691 String errKey = getErrorKey(kindname, typeargtypes.nonEmpty(), hasLocation);
aoqi@0 3692 if (hasLocation) {
aoqi@0 3693 return diags.create(dkind, log.currentSource(), pos,
aoqi@0 3694 errKey, kindname, idname, //symbol kindname, name
aoqi@0 3695 typeargtypes, args(argtypes), //type parameters and arguments (if any)
aoqi@0 3696 getLocationDiag(location, site)); //location kindname, type
aoqi@0 3697 }
aoqi@0 3698 else {
aoqi@0 3699 return diags.create(dkind, log.currentSource(), pos,
aoqi@0 3700 errKey, kindname, idname, //symbol kindname, name
aoqi@0 3701 typeargtypes, args(argtypes)); //type parameters and arguments (if any)
aoqi@0 3702 }
aoqi@0 3703 }
aoqi@0 3704 //where
aoqi@0 3705 private Object args(List<Type> args) {
aoqi@0 3706 return args.isEmpty() ? args : methodArguments(args);
aoqi@0 3707 }
aoqi@0 3708
aoqi@0 3709 private String getErrorKey(KindName kindname, boolean hasTypeArgs, boolean hasLocation) {
aoqi@0 3710 String key = "cant.resolve";
aoqi@0 3711 String suffix = hasLocation ? ".location" : "";
aoqi@0 3712 switch (kindname) {
aoqi@0 3713 case METHOD:
aoqi@0 3714 case CONSTRUCTOR: {
aoqi@0 3715 suffix += ".args";
aoqi@0 3716 suffix += hasTypeArgs ? ".params" : "";
aoqi@0 3717 }
aoqi@0 3718 }
aoqi@0 3719 return key + suffix;
aoqi@0 3720 }
aoqi@0 3721 private JCDiagnostic getLocationDiag(Symbol location, Type site) {
aoqi@0 3722 if (location.kind == VAR) {
aoqi@0 3723 return diags.fragment("location.1",
aoqi@0 3724 kindName(location),
aoqi@0 3725 location,
aoqi@0 3726 location.type);
aoqi@0 3727 } else {
aoqi@0 3728 return diags.fragment("location",
aoqi@0 3729 typeKindName(site),
aoqi@0 3730 site,
aoqi@0 3731 null);
aoqi@0 3732 }
aoqi@0 3733 }
aoqi@0 3734 }
aoqi@0 3735
aoqi@0 3736 /**
aoqi@0 3737 * InvalidSymbolError error class indicating that a given symbol
aoqi@0 3738 * (either a method, a constructor or an operand) is not applicable
aoqi@0 3739 * given an actual arguments/type argument list.
aoqi@0 3740 */
aoqi@0 3741 class InapplicableSymbolError extends ResolveError {
aoqi@0 3742
aoqi@0 3743 protected MethodResolutionContext resolveContext;
aoqi@0 3744
aoqi@0 3745 InapplicableSymbolError(MethodResolutionContext context) {
aoqi@0 3746 this(WRONG_MTH, "inapplicable symbol error", context);
aoqi@0 3747 }
aoqi@0 3748
aoqi@0 3749 protected InapplicableSymbolError(int kind, String debugName, MethodResolutionContext context) {
aoqi@0 3750 super(kind, debugName);
aoqi@0 3751 this.resolveContext = context;
aoqi@0 3752 }
aoqi@0 3753
aoqi@0 3754 @Override
aoqi@0 3755 public String toString() {
aoqi@0 3756 return super.toString();
aoqi@0 3757 }
aoqi@0 3758
aoqi@0 3759 @Override
aoqi@0 3760 public boolean exists() {
aoqi@0 3761 return true;
aoqi@0 3762 }
aoqi@0 3763
aoqi@0 3764 @Override
aoqi@0 3765 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
aoqi@0 3766 DiagnosticPosition pos,
aoqi@0 3767 Symbol location,
aoqi@0 3768 Type site,
aoqi@0 3769 Name name,
aoqi@0 3770 List<Type> argtypes,
aoqi@0 3771 List<Type> typeargtypes) {
aoqi@0 3772 if (name == names.error)
aoqi@0 3773 return null;
aoqi@0 3774
aoqi@0 3775 if (syms.operatorNames.contains(name)) {
aoqi@0 3776 boolean isUnaryOp = argtypes.size() == 1;
aoqi@0 3777 String key = argtypes.size() == 1 ?
aoqi@0 3778 "operator.cant.be.applied" :
aoqi@0 3779 "operator.cant.be.applied.1";
aoqi@0 3780 Type first = argtypes.head;
aoqi@0 3781 Type second = !isUnaryOp ? argtypes.tail.head : null;
aoqi@0 3782 return diags.create(dkind, log.currentSource(), pos,
aoqi@0 3783 key, name, first, second);
aoqi@0 3784 }
aoqi@0 3785 else {
aoqi@0 3786 Pair<Symbol, JCDiagnostic> c = errCandidate();
aoqi@0 3787 if (compactMethodDiags) {
aoqi@0 3788 for (Map.Entry<Template, DiagnosticRewriter> _entry :
aoqi@0 3789 MethodResolutionDiagHelper.rewriters.entrySet()) {
aoqi@0 3790 if (_entry.getKey().matches(c.snd)) {
aoqi@0 3791 JCDiagnostic simpleDiag =
aoqi@0 3792 _entry.getValue().rewriteDiagnostic(diags, pos,
aoqi@0 3793 log.currentSource(), dkind, c.snd);
aoqi@0 3794 simpleDiag.setFlag(DiagnosticFlag.COMPRESSED);
aoqi@0 3795 return simpleDiag;
aoqi@0 3796 }
aoqi@0 3797 }
aoqi@0 3798 }
aoqi@0 3799 Symbol ws = c.fst.asMemberOf(site, types);
aoqi@0 3800 return diags.create(dkind, log.currentSource(), pos,
aoqi@0 3801 "cant.apply.symbol",
aoqi@0 3802 kindName(ws),
aoqi@0 3803 ws.name == names.init ? ws.owner.name : ws.name,
aoqi@0 3804 methodArguments(ws.type.getParameterTypes()),
aoqi@0 3805 methodArguments(argtypes),
aoqi@0 3806 kindName(ws.owner),
aoqi@0 3807 ws.owner.type,
aoqi@0 3808 c.snd);
aoqi@0 3809 }
aoqi@0 3810 }
aoqi@0 3811
aoqi@0 3812 @Override
aoqi@0 3813 public Symbol access(Name name, TypeSymbol location) {
aoqi@0 3814 return types.createErrorType(name, location, syms.errSymbol.type).tsym;
aoqi@0 3815 }
aoqi@0 3816
aoqi@0 3817 protected Pair<Symbol, JCDiagnostic> errCandidate() {
aoqi@0 3818 Candidate bestSoFar = null;
aoqi@0 3819 for (Candidate c : resolveContext.candidates) {
aoqi@0 3820 if (c.isApplicable()) continue;
aoqi@0 3821 bestSoFar = c;
aoqi@0 3822 }
aoqi@0 3823 Assert.checkNonNull(bestSoFar);
aoqi@0 3824 return new Pair<Symbol, JCDiagnostic>(bestSoFar.sym, bestSoFar.details);
aoqi@0 3825 }
aoqi@0 3826 }
aoqi@0 3827
aoqi@0 3828 /**
aoqi@0 3829 * ResolveError error class indicating that a set of symbols
aoqi@0 3830 * (either methods, constructors or operands) is not applicable
aoqi@0 3831 * given an actual arguments/type argument list.
aoqi@0 3832 */
aoqi@0 3833 class InapplicableSymbolsError extends InapplicableSymbolError {
aoqi@0 3834
aoqi@0 3835 InapplicableSymbolsError(MethodResolutionContext context) {
aoqi@0 3836 super(WRONG_MTHS, "inapplicable symbols", context);
aoqi@0 3837 }
aoqi@0 3838
aoqi@0 3839 @Override
aoqi@0 3840 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
aoqi@0 3841 DiagnosticPosition pos,
aoqi@0 3842 Symbol location,
aoqi@0 3843 Type site,
aoqi@0 3844 Name name,
aoqi@0 3845 List<Type> argtypes,
aoqi@0 3846 List<Type> typeargtypes) {
aoqi@0 3847 Map<Symbol, JCDiagnostic> candidatesMap = mapCandidates();
aoqi@0 3848 Map<Symbol, JCDiagnostic> filteredCandidates = compactMethodDiags ?
aoqi@0 3849 filterCandidates(candidatesMap) :
aoqi@0 3850 mapCandidates();
aoqi@0 3851 if (filteredCandidates.isEmpty()) {
aoqi@0 3852 filteredCandidates = candidatesMap;
aoqi@0 3853 }
aoqi@0 3854 boolean truncatedDiag = candidatesMap.size() != filteredCandidates.size();
aoqi@0 3855 if (filteredCandidates.size() > 1) {
aoqi@0 3856 JCDiagnostic err = diags.create(dkind,
aoqi@0 3857 null,
aoqi@0 3858 truncatedDiag ?
aoqi@0 3859 EnumSet.of(DiagnosticFlag.COMPRESSED) :
aoqi@0 3860 EnumSet.noneOf(DiagnosticFlag.class),
aoqi@0 3861 log.currentSource(),
aoqi@0 3862 pos,
aoqi@0 3863 "cant.apply.symbols",
aoqi@0 3864 name == names.init ? KindName.CONSTRUCTOR : absentKind(kind),
aoqi@0 3865 name == names.init ? site.tsym.name : name,
aoqi@0 3866 methodArguments(argtypes));
aoqi@0 3867 return new JCDiagnostic.MultilineDiagnostic(err, candidateDetails(filteredCandidates, site));
aoqi@0 3868 } else if (filteredCandidates.size() == 1) {
aoqi@0 3869 Map.Entry<Symbol, JCDiagnostic> _e =
aoqi@0 3870 filteredCandidates.entrySet().iterator().next();
aoqi@0 3871 final Pair<Symbol, JCDiagnostic> p = new Pair<Symbol, JCDiagnostic>(_e.getKey(), _e.getValue());
aoqi@0 3872 JCDiagnostic d = new InapplicableSymbolError(resolveContext) {
aoqi@0 3873 @Override
aoqi@0 3874 protected Pair<Symbol, JCDiagnostic> errCandidate() {
aoqi@0 3875 return p;
aoqi@0 3876 }
aoqi@0 3877 }.getDiagnostic(dkind, pos,
aoqi@0 3878 location, site, name, argtypes, typeargtypes);
aoqi@0 3879 if (truncatedDiag) {
aoqi@0 3880 d.setFlag(DiagnosticFlag.COMPRESSED);
aoqi@0 3881 }
aoqi@0 3882 return d;
aoqi@0 3883 } else {
aoqi@0 3884 return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind, pos,
aoqi@0 3885 location, site, name, argtypes, typeargtypes);
aoqi@0 3886 }
aoqi@0 3887 }
aoqi@0 3888 //where
aoqi@0 3889 private Map<Symbol, JCDiagnostic> mapCandidates() {
aoqi@0 3890 Map<Symbol, JCDiagnostic> candidates = new LinkedHashMap<Symbol, JCDiagnostic>();
aoqi@0 3891 for (Candidate c : resolveContext.candidates) {
aoqi@0 3892 if (c.isApplicable()) continue;
aoqi@0 3893 candidates.put(c.sym, c.details);
aoqi@0 3894 }
aoqi@0 3895 return candidates;
aoqi@0 3896 }
aoqi@0 3897
aoqi@0 3898 Map<Symbol, JCDiagnostic> filterCandidates(Map<Symbol, JCDiagnostic> candidatesMap) {
aoqi@0 3899 Map<Symbol, JCDiagnostic> candidates = new LinkedHashMap<Symbol, JCDiagnostic>();
aoqi@0 3900 for (Map.Entry<Symbol, JCDiagnostic> _entry : candidatesMap.entrySet()) {
aoqi@0 3901 JCDiagnostic d = _entry.getValue();
aoqi@0 3902 if (!new Template(MethodCheckDiag.ARITY_MISMATCH.regex()).matches(d)) {
aoqi@0 3903 candidates.put(_entry.getKey(), d);
aoqi@0 3904 }
aoqi@0 3905 }
aoqi@0 3906 return candidates;
aoqi@0 3907 }
aoqi@0 3908
aoqi@0 3909 private List<JCDiagnostic> candidateDetails(Map<Symbol, JCDiagnostic> candidatesMap, Type site) {
aoqi@0 3910 List<JCDiagnostic> details = List.nil();
aoqi@0 3911 for (Map.Entry<Symbol, JCDiagnostic> _entry : candidatesMap.entrySet()) {
aoqi@0 3912 Symbol sym = _entry.getKey();
aoqi@0 3913 JCDiagnostic detailDiag = diags.fragment("inapplicable.method",
aoqi@0 3914 Kinds.kindName(sym),
aoqi@0 3915 sym.location(site, types),
aoqi@0 3916 sym.asMemberOf(site, types),
aoqi@0 3917 _entry.getValue());
aoqi@0 3918 details = details.prepend(detailDiag);
aoqi@0 3919 }
aoqi@0 3920 //typically members are visited in reverse order (see Scope)
aoqi@0 3921 //so we need to reverse the candidate list so that candidates
aoqi@0 3922 //conform to source order
aoqi@0 3923 return details;
aoqi@0 3924 }
aoqi@0 3925 }
aoqi@0 3926
aoqi@0 3927 /**
aoqi@0 3928 * An InvalidSymbolError error class indicating that a symbol is not
aoqi@0 3929 * accessible from a given site
aoqi@0 3930 */
aoqi@0 3931 class AccessError extends InvalidSymbolError {
aoqi@0 3932
aoqi@0 3933 private Env<AttrContext> env;
aoqi@0 3934 private Type site;
aoqi@0 3935
aoqi@0 3936 AccessError(Symbol sym) {
aoqi@0 3937 this(null, null, sym);
aoqi@0 3938 }
aoqi@0 3939
aoqi@0 3940 AccessError(Env<AttrContext> env, Type site, Symbol sym) {
aoqi@0 3941 super(HIDDEN, sym, "access error");
aoqi@0 3942 this.env = env;
aoqi@0 3943 this.site = site;
aoqi@0 3944 if (debugResolve)
aoqi@0 3945 log.error("proc.messager", sym + " @ " + site + " is inaccessible.");
aoqi@0 3946 }
aoqi@0 3947
aoqi@0 3948 @Override
aoqi@0 3949 public boolean exists() {
aoqi@0 3950 return false;
aoqi@0 3951 }
aoqi@0 3952
aoqi@0 3953 @Override
aoqi@0 3954 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
aoqi@0 3955 DiagnosticPosition pos,
aoqi@0 3956 Symbol location,
aoqi@0 3957 Type site,
aoqi@0 3958 Name name,
aoqi@0 3959 List<Type> argtypes,
aoqi@0 3960 List<Type> typeargtypes) {
aoqi@0 3961 if (sym.owner.type.hasTag(ERROR))
aoqi@0 3962 return null;
aoqi@0 3963
aoqi@0 3964 if (sym.name == names.init && sym.owner != site.tsym) {
aoqi@0 3965 return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind,
aoqi@0 3966 pos, location, site, name, argtypes, typeargtypes);
aoqi@0 3967 }
aoqi@0 3968 else if ((sym.flags() & PUBLIC) != 0
aoqi@0 3969 || (env != null && this.site != null
aoqi@0 3970 && !isAccessible(env, this.site))) {
aoqi@0 3971 return diags.create(dkind, log.currentSource(),
aoqi@0 3972 pos, "not.def.access.class.intf.cant.access",
aoqi@0 3973 sym, sym.location());
aoqi@0 3974 }
aoqi@0 3975 else if ((sym.flags() & (PRIVATE | PROTECTED)) != 0) {
aoqi@0 3976 return diags.create(dkind, log.currentSource(),
aoqi@0 3977 pos, "report.access", sym,
aoqi@0 3978 asFlagSet(sym.flags() & (PRIVATE | PROTECTED)),
aoqi@0 3979 sym.location());
aoqi@0 3980 }
aoqi@0 3981 else {
aoqi@0 3982 return diags.create(dkind, log.currentSource(),
aoqi@0 3983 pos, "not.def.public.cant.access", sym, sym.location());
aoqi@0 3984 }
aoqi@0 3985 }
aoqi@0 3986 }
aoqi@0 3987
aoqi@0 3988 /**
aoqi@0 3989 * InvalidSymbolError error class indicating that an instance member
aoqi@0 3990 * has erroneously been accessed from a static context.
aoqi@0 3991 */
aoqi@0 3992 class StaticError extends InvalidSymbolError {
aoqi@0 3993
aoqi@0 3994 StaticError(Symbol sym) {
aoqi@0 3995 super(STATICERR, sym, "static error");
aoqi@0 3996 }
aoqi@0 3997
aoqi@0 3998 @Override
aoqi@0 3999 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
aoqi@0 4000 DiagnosticPosition pos,
aoqi@0 4001 Symbol location,
aoqi@0 4002 Type site,
aoqi@0 4003 Name name,
aoqi@0 4004 List<Type> argtypes,
aoqi@0 4005 List<Type> typeargtypes) {
aoqi@0 4006 Symbol errSym = ((sym.kind == TYP && sym.type.hasTag(CLASS))
aoqi@0 4007 ? types.erasure(sym.type).tsym
aoqi@0 4008 : sym);
aoqi@0 4009 return diags.create(dkind, log.currentSource(), pos,
aoqi@0 4010 "non-static.cant.be.ref", kindName(sym), errSym);
aoqi@0 4011 }
aoqi@0 4012 }
aoqi@0 4013
aoqi@0 4014 /**
aoqi@0 4015 * InvalidSymbolError error class indicating that a pair of symbols
aoqi@0 4016 * (either methods, constructors or operands) are ambiguous
aoqi@0 4017 * given an actual arguments/type argument list.
aoqi@0 4018 */
aoqi@0 4019 class AmbiguityError extends ResolveError {
aoqi@0 4020
aoqi@0 4021 /** The other maximally specific symbol */
aoqi@0 4022 List<Symbol> ambiguousSyms = List.nil();
aoqi@0 4023
aoqi@0 4024 @Override
aoqi@0 4025 public boolean exists() {
aoqi@0 4026 return true;
aoqi@0 4027 }
aoqi@0 4028
aoqi@0 4029 AmbiguityError(Symbol sym1, Symbol sym2) {
aoqi@0 4030 super(AMBIGUOUS, "ambiguity error");
aoqi@0 4031 ambiguousSyms = flatten(sym2).appendList(flatten(sym1));
aoqi@0 4032 }
aoqi@0 4033
aoqi@0 4034 private List<Symbol> flatten(Symbol sym) {
aoqi@0 4035 if (sym.kind == AMBIGUOUS) {
aoqi@0 4036 return ((AmbiguityError)sym.baseSymbol()).ambiguousSyms;
aoqi@0 4037 } else {
aoqi@0 4038 return List.of(sym);
aoqi@0 4039 }
aoqi@0 4040 }
aoqi@0 4041
aoqi@0 4042 AmbiguityError addAmbiguousSymbol(Symbol s) {
aoqi@0 4043 ambiguousSyms = ambiguousSyms.prepend(s);
aoqi@0 4044 return this;
aoqi@0 4045 }
aoqi@0 4046
aoqi@0 4047 @Override
aoqi@0 4048 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
aoqi@0 4049 DiagnosticPosition pos,
aoqi@0 4050 Symbol location,
aoqi@0 4051 Type site,
aoqi@0 4052 Name name,
aoqi@0 4053 List<Type> argtypes,
aoqi@0 4054 List<Type> typeargtypes) {
aoqi@0 4055 List<Symbol> diagSyms = ambiguousSyms.reverse();
aoqi@0 4056 Symbol s1 = diagSyms.head;
aoqi@0 4057 Symbol s2 = diagSyms.tail.head;
aoqi@0 4058 Name sname = s1.name;
aoqi@0 4059 if (sname == names.init) sname = s1.owner.name;
aoqi@0 4060 return diags.create(dkind, log.currentSource(),
aoqi@0 4061 pos, "ref.ambiguous", sname,
aoqi@0 4062 kindName(s1),
aoqi@0 4063 s1,
aoqi@0 4064 s1.location(site, types),
aoqi@0 4065 kindName(s2),
aoqi@0 4066 s2,
aoqi@0 4067 s2.location(site, types));
aoqi@0 4068 }
aoqi@0 4069
aoqi@0 4070 /**
aoqi@0 4071 * If multiple applicable methods are found during overload and none of them
aoqi@0 4072 * is more specific than the others, attempt to merge their signatures.
aoqi@0 4073 */
aoqi@0 4074 Symbol mergeAbstracts(Type site) {
aoqi@0 4075 List<Symbol> ambiguousInOrder = ambiguousSyms.reverse();
aoqi@0 4076 for (Symbol s : ambiguousInOrder) {
aoqi@0 4077 Type mt = types.memberType(site, s);
aoqi@0 4078 boolean found = true;
aoqi@0 4079 List<Type> allThrown = mt.getThrownTypes();
aoqi@0 4080 for (Symbol s2 : ambiguousInOrder) {
aoqi@0 4081 Type mt2 = types.memberType(site, s2);
aoqi@0 4082 if ((s2.flags() & ABSTRACT) == 0 ||
aoqi@0 4083 !types.overrideEquivalent(mt, mt2) ||
aoqi@0 4084 !types.isSameTypes(s.erasure(types).getParameterTypes(),
aoqi@0 4085 s2.erasure(types).getParameterTypes())) {
aoqi@0 4086 //ambiguity cannot be resolved
aoqi@0 4087 return this;
aoqi@0 4088 }
aoqi@0 4089 Type mst = mostSpecificReturnType(mt, mt2);
aoqi@0 4090 if (mst == null || mst != mt) {
aoqi@0 4091 found = false;
aoqi@0 4092 break;
aoqi@0 4093 }
aoqi@0 4094 allThrown = chk.intersect(allThrown, mt2.getThrownTypes());
aoqi@0 4095 }
aoqi@0 4096 if (found) {
aoqi@0 4097 //all ambiguous methods were abstract and one method had
aoqi@0 4098 //most specific return type then others
aoqi@0 4099 return (allThrown == mt.getThrownTypes()) ?
aoqi@0 4100 s : new MethodSymbol(
aoqi@0 4101 s.flags(),
aoqi@0 4102 s.name,
mcimadamore@2794 4103 types.createMethodTypeWithThrown(s.type, allThrown),
aoqi@0 4104 s.owner);
aoqi@0 4105 }
aoqi@0 4106 }
aoqi@0 4107 return this;
aoqi@0 4108 }
aoqi@0 4109
aoqi@0 4110 @Override
aoqi@0 4111 protected Symbol access(Name name, TypeSymbol location) {
aoqi@0 4112 Symbol firstAmbiguity = ambiguousSyms.last();
aoqi@0 4113 return firstAmbiguity.kind == TYP ?
aoqi@0 4114 types.createErrorType(name, location, firstAmbiguity.type).tsym :
aoqi@0 4115 firstAmbiguity;
aoqi@0 4116 }
aoqi@0 4117 }
aoqi@0 4118
aoqi@0 4119 class BadVarargsMethod extends ResolveError {
aoqi@0 4120
aoqi@0 4121 ResolveError delegatedError;
aoqi@0 4122
aoqi@0 4123 BadVarargsMethod(ResolveError delegatedError) {
aoqi@0 4124 super(delegatedError.kind, "badVarargs");
aoqi@0 4125 this.delegatedError = delegatedError;
aoqi@0 4126 }
aoqi@0 4127
aoqi@0 4128 @Override
aoqi@0 4129 public Symbol baseSymbol() {
aoqi@0 4130 return delegatedError.baseSymbol();
aoqi@0 4131 }
aoqi@0 4132
aoqi@0 4133 @Override
aoqi@0 4134 protected Symbol access(Name name, TypeSymbol location) {
aoqi@0 4135 return delegatedError.access(name, location);
aoqi@0 4136 }
aoqi@0 4137
aoqi@0 4138 @Override
aoqi@0 4139 public boolean exists() {
aoqi@0 4140 return true;
aoqi@0 4141 }
aoqi@0 4142
aoqi@0 4143 @Override
aoqi@0 4144 JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos, Symbol location, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) {
aoqi@0 4145 return delegatedError.getDiagnostic(dkind, pos, location, site, name, argtypes, typeargtypes);
aoqi@0 4146 }
aoqi@0 4147 }
aoqi@0 4148
aoqi@0 4149 /**
aoqi@0 4150 * Helper class for method resolution diagnostic simplification.
aoqi@0 4151 * Certain resolution diagnostic are rewritten as simpler diagnostic
aoqi@0 4152 * where the enclosing resolution diagnostic (i.e. 'inapplicable method')
aoqi@0 4153 * is stripped away, as it doesn't carry additional info. The logic
aoqi@0 4154 * for matching a given diagnostic is given in terms of a template
aoqi@0 4155 * hierarchy: a diagnostic template can be specified programmatically,
aoqi@0 4156 * so that only certain diagnostics are matched. Each templete is then
aoqi@0 4157 * associated with a rewriter object that carries out the task of rewtiting
aoqi@0 4158 * the diagnostic to a simpler one.
aoqi@0 4159 */
aoqi@0 4160 static class MethodResolutionDiagHelper {
aoqi@0 4161
aoqi@0 4162 /**
aoqi@0 4163 * A diagnostic rewriter transforms a method resolution diagnostic
aoqi@0 4164 * into a simpler one
aoqi@0 4165 */
aoqi@0 4166 interface DiagnosticRewriter {
aoqi@0 4167 JCDiagnostic rewriteDiagnostic(JCDiagnostic.Factory diags,
aoqi@0 4168 DiagnosticPosition preferedPos, DiagnosticSource preferredSource,
aoqi@0 4169 DiagnosticType preferredKind, JCDiagnostic d);
aoqi@0 4170 }
aoqi@0 4171
aoqi@0 4172 /**
aoqi@0 4173 * A diagnostic template is made up of two ingredients: (i) a regular
aoqi@0 4174 * expression for matching a diagnostic key and (ii) a list of sub-templates
aoqi@0 4175 * for matching diagnostic arguments.
aoqi@0 4176 */
aoqi@0 4177 static class Template {
aoqi@0 4178
aoqi@0 4179 /** regex used to match diag key */
aoqi@0 4180 String regex;
aoqi@0 4181
aoqi@0 4182 /** templates used to match diagnostic args */
aoqi@0 4183 Template[] subTemplates;
aoqi@0 4184
aoqi@0 4185 Template(String key, Template... subTemplates) {
aoqi@0 4186 this.regex = key;
aoqi@0 4187 this.subTemplates = subTemplates;
aoqi@0 4188 }
aoqi@0 4189
aoqi@0 4190 /**
aoqi@0 4191 * Returns true if the regex matches the diagnostic key and if
aoqi@0 4192 * all diagnostic arguments are matches by corresponding sub-templates.
aoqi@0 4193 */
aoqi@0 4194 boolean matches(Object o) {
aoqi@0 4195 JCDiagnostic d = (JCDiagnostic)o;
aoqi@0 4196 Object[] args = d.getArgs();
aoqi@0 4197 if (!d.getCode().matches(regex) ||
aoqi@0 4198 subTemplates.length != d.getArgs().length) {
aoqi@0 4199 return false;
aoqi@0 4200 }
aoqi@0 4201 for (int i = 0; i < args.length ; i++) {
aoqi@0 4202 if (!subTemplates[i].matches(args[i])) {
aoqi@0 4203 return false;
aoqi@0 4204 }
aoqi@0 4205 }
aoqi@0 4206 return true;
aoqi@0 4207 }
aoqi@0 4208 }
aoqi@0 4209
aoqi@0 4210 /** a dummy template that match any diagnostic argument */
aoqi@0 4211 static final Template skip = new Template("") {
aoqi@0 4212 @Override
aoqi@0 4213 boolean matches(Object d) {
aoqi@0 4214 return true;
aoqi@0 4215 }
aoqi@0 4216 };
aoqi@0 4217
aoqi@0 4218 /** rewriter map used for method resolution simplification */
aoqi@0 4219 static final Map<Template, DiagnosticRewriter> rewriters =
aoqi@0 4220 new LinkedHashMap<Template, DiagnosticRewriter>();
aoqi@0 4221
aoqi@0 4222 static {
aoqi@0 4223 String argMismatchRegex = MethodCheckDiag.ARG_MISMATCH.regex();
aoqi@0 4224 rewriters.put(new Template(argMismatchRegex, skip),
aoqi@0 4225 new DiagnosticRewriter() {
aoqi@0 4226 @Override
aoqi@0 4227 public JCDiagnostic rewriteDiagnostic(JCDiagnostic.Factory diags,
aoqi@0 4228 DiagnosticPosition preferedPos, DiagnosticSource preferredSource,
aoqi@0 4229 DiagnosticType preferredKind, JCDiagnostic d) {
aoqi@0 4230 JCDiagnostic cause = (JCDiagnostic)d.getArgs()[0];
sadayapalam@3005 4231 DiagnosticPosition pos = d.getDiagnosticPosition();
sadayapalam@3005 4232 if (pos == null) {
sadayapalam@3005 4233 pos = preferedPos;
sadayapalam@3005 4234 }
sadayapalam@3005 4235 return diags.create(preferredKind, preferredSource, pos,
aoqi@0 4236 "prob.found.req", cause);
aoqi@0 4237 }
aoqi@0 4238 });
aoqi@0 4239 }
aoqi@0 4240 }
aoqi@0 4241
aoqi@0 4242 enum MethodResolutionPhase {
aoqi@0 4243 BASIC(false, false),
aoqi@0 4244 BOX(true, false),
aoqi@0 4245 VARARITY(true, true) {
aoqi@0 4246 @Override
aoqi@0 4247 public Symbol mergeResults(Symbol bestSoFar, Symbol sym) {
mcimadamore@2559 4248 //Check invariants (see {@code LookupHelper.shouldStop})
mcimadamore@2559 4249 Assert.check(bestSoFar.kind >= ERRONEOUS && bestSoFar.kind != AMBIGUOUS);
mcimadamore@2559 4250 if (sym.kind < ERRONEOUS) {
mcimadamore@2559 4251 //varargs resolution successful
mcimadamore@2559 4252 return sym;
mcimadamore@2559 4253 } else {
mcimadamore@2559 4254 //pick best error
mcimadamore@2559 4255 switch (bestSoFar.kind) {
mcimadamore@2559 4256 case WRONG_MTH:
mcimadamore@2559 4257 case WRONG_MTHS:
mcimadamore@2559 4258 //Override previous errors if they were caused by argument mismatch.
mcimadamore@2559 4259 //This generally means preferring current symbols - but we need to pay
mcimadamore@2559 4260 //attention to the fact that the varargs lookup returns 'less' candidates
mcimadamore@2559 4261 //than the previous rounds, and adjust that accordingly.
mcimadamore@2559 4262 switch (sym.kind) {
mcimadamore@2559 4263 case WRONG_MTH:
mcimadamore@2559 4264 //if the previous round matched more than one method, return that
mcimadamore@2559 4265 //result instead
mcimadamore@2559 4266 return bestSoFar.kind == WRONG_MTHS ?
mcimadamore@2559 4267 bestSoFar : sym;
mcimadamore@2559 4268 case ABSENT_MTH:
mcimadamore@2559 4269 //do not override erroneous symbol if the arity lookup did not
mcimadamore@2559 4270 //match any method
mcimadamore@2559 4271 return bestSoFar;
mcimadamore@2559 4272 case WRONG_MTHS:
mcimadamore@2559 4273 default:
mcimadamore@2559 4274 //safe to override
mcimadamore@2559 4275 return sym;
mcimadamore@2559 4276 }
mcimadamore@2559 4277 default:
mcimadamore@2559 4278 //otherwise, return first error
mcimadamore@2559 4279 return bestSoFar;
mcimadamore@2559 4280 }
aoqi@0 4281 }
aoqi@0 4282 }
aoqi@0 4283 };
aoqi@0 4284
aoqi@0 4285 final boolean isBoxingRequired;
aoqi@0 4286 final boolean isVarargsRequired;
aoqi@0 4287
aoqi@0 4288 MethodResolutionPhase(boolean isBoxingRequired, boolean isVarargsRequired) {
aoqi@0 4289 this.isBoxingRequired = isBoxingRequired;
aoqi@0 4290 this.isVarargsRequired = isVarargsRequired;
aoqi@0 4291 }
aoqi@0 4292
aoqi@0 4293 public boolean isBoxingRequired() {
aoqi@0 4294 return isBoxingRequired;
aoqi@0 4295 }
aoqi@0 4296
aoqi@0 4297 public boolean isVarargsRequired() {
aoqi@0 4298 return isVarargsRequired;
aoqi@0 4299 }
aoqi@0 4300
aoqi@0 4301 public boolean isApplicable(boolean boxingEnabled, boolean varargsEnabled) {
aoqi@0 4302 return (varargsEnabled || !isVarargsRequired) &&
aoqi@0 4303 (boxingEnabled || !isBoxingRequired);
aoqi@0 4304 }
aoqi@0 4305
aoqi@0 4306 public Symbol mergeResults(Symbol prev, Symbol sym) {
aoqi@0 4307 return sym;
aoqi@0 4308 }
aoqi@0 4309 }
aoqi@0 4310
aoqi@0 4311 final List<MethodResolutionPhase> methodResolutionSteps = List.of(BASIC, BOX, VARARITY);
aoqi@0 4312
aoqi@0 4313 /**
aoqi@0 4314 * A resolution context is used to keep track of intermediate results of
aoqi@0 4315 * overload resolution, such as list of method that are not applicable
aoqi@0 4316 * (used to generate more precise diagnostics) and so on. Resolution contexts
aoqi@0 4317 * can be nested - this means that when each overload resolution routine should
aoqi@0 4318 * work within the resolution context it created.
aoqi@0 4319 */
aoqi@0 4320 class MethodResolutionContext {
aoqi@0 4321
aoqi@0 4322 private List<Candidate> candidates = List.nil();
aoqi@0 4323
aoqi@0 4324 MethodResolutionPhase step = null;
aoqi@0 4325
aoqi@0 4326 MethodCheck methodCheck = resolveMethodCheck;
aoqi@0 4327
aoqi@0 4328 private boolean internalResolution = false;
aoqi@0 4329 private DeferredAttr.AttrMode attrMode = DeferredAttr.AttrMode.SPECULATIVE;
aoqi@0 4330
aoqi@0 4331 void addInapplicableCandidate(Symbol sym, JCDiagnostic details) {
aoqi@0 4332 Candidate c = new Candidate(currentResolutionContext.step, sym, details, null);
aoqi@0 4333 candidates = candidates.append(c);
aoqi@0 4334 }
aoqi@0 4335
aoqi@0 4336 void addApplicableCandidate(Symbol sym, Type mtype) {
aoqi@0 4337 Candidate c = new Candidate(currentResolutionContext.step, sym, null, mtype);
aoqi@0 4338 candidates = candidates.append(c);
aoqi@0 4339 }
aoqi@0 4340
aoqi@0 4341 DeferredAttrContext deferredAttrContext(Symbol sym, InferenceContext inferenceContext, ResultInfo pendingResult, Warner warn) {
aoqi@0 4342 DeferredAttrContext parent = (pendingResult == null)
aoqi@0 4343 ? deferredAttr.emptyDeferredAttrContext
aoqi@0 4344 : pendingResult.checkContext.deferredAttrContext();
aoqi@0 4345 return deferredAttr.new DeferredAttrContext(attrMode, sym, step,
aoqi@0 4346 inferenceContext, parent, warn);
aoqi@0 4347 }
aoqi@0 4348
aoqi@0 4349 /**
aoqi@0 4350 * This class represents an overload resolution candidate. There are two
aoqi@0 4351 * kinds of candidates: applicable methods and inapplicable methods;
aoqi@0 4352 * applicable methods have a pointer to the instantiated method type,
aoqi@0 4353 * while inapplicable candidates contain further details about the
aoqi@0 4354 * reason why the method has been considered inapplicable.
aoqi@0 4355 */
aoqi@0 4356 @SuppressWarnings("overrides")
aoqi@0 4357 class Candidate {
aoqi@0 4358
aoqi@0 4359 final MethodResolutionPhase step;
aoqi@0 4360 final Symbol sym;
aoqi@0 4361 final JCDiagnostic details;
aoqi@0 4362 final Type mtype;
aoqi@0 4363
aoqi@0 4364 private Candidate(MethodResolutionPhase step, Symbol sym, JCDiagnostic details, Type mtype) {
aoqi@0 4365 this.step = step;
aoqi@0 4366 this.sym = sym;
aoqi@0 4367 this.details = details;
aoqi@0 4368 this.mtype = mtype;
aoqi@0 4369 }
aoqi@0 4370
aoqi@0 4371 @Override
aoqi@0 4372 public boolean equals(Object o) {
aoqi@0 4373 if (o instanceof Candidate) {
aoqi@0 4374 Symbol s1 = this.sym;
aoqi@0 4375 Symbol s2 = ((Candidate)o).sym;
aoqi@0 4376 if ((s1 != s2 &&
aoqi@0 4377 (s1.overrides(s2, s1.owner.type.tsym, types, false) ||
aoqi@0 4378 (s2.overrides(s1, s2.owner.type.tsym, types, false)))) ||
aoqi@0 4379 ((s1.isConstructor() || s2.isConstructor()) && s1.owner != s2.owner))
aoqi@0 4380 return true;
aoqi@0 4381 }
aoqi@0 4382 return false;
aoqi@0 4383 }
aoqi@0 4384
aoqi@0 4385 boolean isApplicable() {
aoqi@0 4386 return mtype != null;
aoqi@0 4387 }
aoqi@0 4388 }
aoqi@0 4389
aoqi@0 4390 DeferredAttr.AttrMode attrMode() {
aoqi@0 4391 return attrMode;
aoqi@0 4392 }
aoqi@0 4393
aoqi@0 4394 boolean internal() {
aoqi@0 4395 return internalResolution;
aoqi@0 4396 }
aoqi@0 4397 }
aoqi@0 4398
aoqi@0 4399 MethodResolutionContext currentResolutionContext = null;
aoqi@0 4400 }

mercurial