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