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

Mon, 16 Oct 2017 16:07:48 +0800

author
aoqi
date
Mon, 16 Oct 2017 16:07:48 +0800
changeset 2893
ca5783d9a597
parent 2814
380f6c17ea01
parent 2702
9ca8d8713094
child 3295
859dc787b52b
permissions
-rw-r--r--

merge

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

mercurial