Thu, 10 Jun 2010 16:08:01 -0700
6944312: Potential rebranding issues in openjdk/langtools repository sources
Reviewed-by: darcy
1 /*
2 * Copyright (c) 1999, 2008, 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.tools.javac.util.*;
29 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
30 import com.sun.tools.javac.code.*;
31 import com.sun.tools.javac.jvm.*;
32 import com.sun.tools.javac.tree.*;
33 import com.sun.tools.javac.api.Formattable.LocalizedString;
34 import static com.sun.tools.javac.comp.Resolve.MethodResolutionPhase.*;
36 import com.sun.tools.javac.code.Type.*;
37 import com.sun.tools.javac.code.Symbol.*;
38 import com.sun.tools.javac.tree.JCTree.*;
40 import static com.sun.tools.javac.code.Flags.*;
41 import static com.sun.tools.javac.code.Kinds.*;
42 import static com.sun.tools.javac.code.TypeTags.*;
43 import javax.lang.model.element.ElementVisitor;
45 import java.util.Map;
46 import java.util.HashMap;
48 /** Helper class for name resolution, used mostly by the attribution phase.
49 *
50 * <p><b>This is NOT part of any supported API.
51 * If you write code that depends on this, you do so at your own risk.
52 * This code and its internal interfaces are subject to change or
53 * deletion without notice.</b>
54 */
55 public class Resolve {
56 protected static final Context.Key<Resolve> resolveKey =
57 new Context.Key<Resolve>();
59 Names names;
60 Log log;
61 Symtab syms;
62 Check chk;
63 Infer infer;
64 ClassReader reader;
65 TreeInfo treeinfo;
66 Types types;
67 JCDiagnostic.Factory diags;
68 public final boolean boxingEnabled; // = source.allowBoxing();
69 public final boolean varargsEnabled; // = source.allowVarargs();
70 public final boolean allowInvokedynamic; // = options.get("invokedynamic");
71 private final boolean debugResolve;
73 public static Resolve instance(Context context) {
74 Resolve instance = context.get(resolveKey);
75 if (instance == null)
76 instance = new Resolve(context);
77 return instance;
78 }
80 protected Resolve(Context context) {
81 context.put(resolveKey, this);
82 syms = Symtab.instance(context);
84 varNotFound = new
85 SymbolNotFoundError(ABSENT_VAR);
86 wrongMethod = new
87 InapplicableSymbolError(syms.errSymbol);
88 wrongMethods = new
89 InapplicableSymbolsError(syms.errSymbol);
90 methodNotFound = new
91 SymbolNotFoundError(ABSENT_MTH);
92 typeNotFound = new
93 SymbolNotFoundError(ABSENT_TYP);
95 names = Names.instance(context);
96 log = Log.instance(context);
97 chk = Check.instance(context);
98 infer = Infer.instance(context);
99 reader = ClassReader.instance(context);
100 treeinfo = TreeInfo.instance(context);
101 types = Types.instance(context);
102 diags = JCDiagnostic.Factory.instance(context);
103 Source source = Source.instance(context);
104 boxingEnabled = source.allowBoxing();
105 varargsEnabled = source.allowVarargs();
106 Options options = Options.instance(context);
107 debugResolve = options.get("debugresolve") != null;
108 allowInvokedynamic = options.get("invokedynamic") != null;
109 }
111 /** error symbols, which are returned when resolution fails
112 */
113 final SymbolNotFoundError varNotFound;
114 final InapplicableSymbolError wrongMethod;
115 final InapplicableSymbolsError wrongMethods;
116 final SymbolNotFoundError methodNotFound;
117 final SymbolNotFoundError typeNotFound;
119 /* ************************************************************************
120 * Identifier resolution
121 *************************************************************************/
123 /** An environment is "static" if its static level is greater than
124 * the one of its outer environment
125 */
126 static boolean isStatic(Env<AttrContext> env) {
127 return env.info.staticLevel > env.outer.info.staticLevel;
128 }
130 /** An environment is an "initializer" if it is a constructor or
131 * an instance initializer.
132 */
133 static boolean isInitializer(Env<AttrContext> env) {
134 Symbol owner = env.info.scope.owner;
135 return owner.isConstructor() ||
136 owner.owner.kind == TYP &&
137 (owner.kind == VAR ||
138 owner.kind == MTH && (owner.flags() & BLOCK) != 0) &&
139 (owner.flags() & STATIC) == 0;
140 }
142 /** Is class accessible in given evironment?
143 * @param env The current environment.
144 * @param c The class whose accessibility is checked.
145 */
146 public boolean isAccessible(Env<AttrContext> env, TypeSymbol c) {
147 switch ((short)(c.flags() & AccessFlags)) {
148 case PRIVATE:
149 return
150 env.enclClass.sym.outermostClass() ==
151 c.owner.outermostClass();
152 case 0:
153 return
154 env.toplevel.packge == c.owner // fast special case
155 ||
156 env.toplevel.packge == c.packge()
157 ||
158 // Hack: this case is added since synthesized default constructors
159 // of anonymous classes should be allowed to access
160 // classes which would be inaccessible otherwise.
161 env.enclMethod != null &&
162 (env.enclMethod.mods.flags & ANONCONSTR) != 0;
163 default: // error recovery
164 case PUBLIC:
165 return true;
166 case PROTECTED:
167 return
168 env.toplevel.packge == c.owner // fast special case
169 ||
170 env.toplevel.packge == c.packge()
171 ||
172 isInnerSubClass(env.enclClass.sym, c.owner);
173 }
174 }
175 //where
176 /** Is given class a subclass of given base class, or an inner class
177 * of a subclass?
178 * Return null if no such class exists.
179 * @param c The class which is the subclass or is contained in it.
180 * @param base The base class
181 */
182 private boolean isInnerSubClass(ClassSymbol c, Symbol base) {
183 while (c != null && !c.isSubClass(base, types)) {
184 c = c.owner.enclClass();
185 }
186 return c != null;
187 }
189 boolean isAccessible(Env<AttrContext> env, Type t) {
190 return (t.tag == ARRAY)
191 ? isAccessible(env, types.elemtype(t))
192 : isAccessible(env, t.tsym);
193 }
195 /** Is symbol accessible as a member of given type in given evironment?
196 * @param env The current environment.
197 * @param site The type of which the tested symbol is regarded
198 * as a member.
199 * @param sym The symbol.
200 */
201 public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym) {
202 if (sym.name == names.init && sym.owner != site.tsym) return false;
203 ClassSymbol sub;
204 switch ((short)(sym.flags() & AccessFlags)) {
205 case PRIVATE:
206 return
207 (env.enclClass.sym == sym.owner // fast special case
208 ||
209 env.enclClass.sym.outermostClass() ==
210 sym.owner.outermostClass())
211 &&
212 sym.isInheritedIn(site.tsym, types);
213 case 0:
214 return
215 (env.toplevel.packge == sym.owner.owner // fast special case
216 ||
217 env.toplevel.packge == sym.packge())
218 &&
219 isAccessible(env, site)
220 &&
221 sym.isInheritedIn(site.tsym, types)
222 &&
223 notOverriddenIn(site, sym);
224 case PROTECTED:
225 return
226 (env.toplevel.packge == sym.owner.owner // fast special case
227 ||
228 env.toplevel.packge == sym.packge()
229 ||
230 isProtectedAccessible(sym, env.enclClass.sym, site)
231 ||
232 // OK to select instance method or field from 'super' or type name
233 // (but type names should be disallowed elsewhere!)
234 env.info.selectSuper && (sym.flags() & STATIC) == 0 && sym.kind != TYP)
235 &&
236 isAccessible(env, site)
237 &&
238 notOverriddenIn(site, sym);
239 default: // this case includes erroneous combinations as well
240 return isAccessible(env, site) && notOverriddenIn(site, sym);
241 }
242 }
243 //where
244 /* `sym' is accessible only if not overridden by
245 * another symbol which is a member of `site'
246 * (because, if it is overridden, `sym' is not strictly
247 * speaking a member of `site'.)
248 */
249 private boolean notOverriddenIn(Type site, Symbol sym) {
250 if (sym.kind != MTH || sym.isConstructor() || sym.isStatic())
251 return true;
252 else {
253 Symbol s2 = ((MethodSymbol)sym).implementation(site.tsym, types, true);
254 return (s2 == null || s2 == sym ||
255 !types.isSubSignature(types.memberType(site, s2), types.memberType(site, sym)));
256 }
257 }
258 //where
259 /** Is given protected symbol accessible if it is selected from given site
260 * and the selection takes place in given class?
261 * @param sym The symbol with protected access
262 * @param c The class where the access takes place
263 * @site The type of the qualifier
264 */
265 private
266 boolean isProtectedAccessible(Symbol sym, ClassSymbol c, Type site) {
267 while (c != null &&
268 !(c.isSubClass(sym.owner, types) &&
269 (c.flags() & INTERFACE) == 0 &&
270 // In JLS 2e 6.6.2.1, the subclass restriction applies
271 // only to instance fields and methods -- types are excluded
272 // regardless of whether they are declared 'static' or not.
273 ((sym.flags() & STATIC) != 0 || sym.kind == TYP || site.tsym.isSubClass(c, types))))
274 c = c.owner.enclClass();
275 return c != null;
276 }
278 /** Try to instantiate the type of a method so that it fits
279 * given type arguments and argument types. If succesful, return
280 * the method's instantiated type, else return null.
281 * The instantiation will take into account an additional leading
282 * formal parameter if the method is an instance method seen as a member
283 * of un underdetermined site In this case, we treat site as an additional
284 * parameter and the parameters of the class containing the method as
285 * additional type variables that get instantiated.
286 *
287 * @param env The current environment
288 * @param site The type of which the method is a member.
289 * @param m The method symbol.
290 * @param argtypes The invocation's given value arguments.
291 * @param typeargtypes The invocation's given type arguments.
292 * @param allowBoxing Allow boxing conversions of arguments.
293 * @param useVarargs Box trailing arguments into an array for varargs.
294 */
295 Type rawInstantiate(Env<AttrContext> env,
296 Type site,
297 Symbol m,
298 List<Type> argtypes,
299 List<Type> typeargtypes,
300 boolean allowBoxing,
301 boolean useVarargs,
302 Warner warn)
303 throws Infer.InferenceException {
304 if (useVarargs && (m.flags() & VARARGS) == 0) return null;
305 Type mt = types.memberType(site, m);
307 // tvars is the list of formal type variables for which type arguments
308 // need to inferred.
309 List<Type> tvars = env.info.tvars;
310 if (typeargtypes == null) typeargtypes = List.nil();
311 if (mt.tag != FORALL && typeargtypes.nonEmpty()) {
312 // This is not a polymorphic method, but typeargs are supplied
313 // which is fine, see JLS3 15.12.2.1
314 } else if (mt.tag == FORALL && typeargtypes.nonEmpty()) {
315 ForAll pmt = (ForAll) mt;
316 if (typeargtypes.length() != pmt.tvars.length())
317 return null;
318 // Check type arguments are within bounds
319 List<Type> formals = pmt.tvars;
320 List<Type> actuals = typeargtypes;
321 while (formals.nonEmpty() && actuals.nonEmpty()) {
322 List<Type> bounds = types.subst(types.getBounds((TypeVar)formals.head),
323 pmt.tvars, typeargtypes);
324 for (; bounds.nonEmpty(); bounds = bounds.tail)
325 if (!types.isSubtypeUnchecked(actuals.head, bounds.head, warn))
326 return null;
327 formals = formals.tail;
328 actuals = actuals.tail;
329 }
330 mt = types.subst(pmt.qtype, pmt.tvars, typeargtypes);
331 } else if (mt.tag == FORALL) {
332 ForAll pmt = (ForAll) mt;
333 List<Type> tvars1 = types.newInstances(pmt.tvars);
334 tvars = tvars.appendList(tvars1);
335 mt = types.subst(pmt.qtype, pmt.tvars, tvars1);
336 }
338 // find out whether we need to go the slow route via infer
339 boolean instNeeded = tvars.tail != null/*inlined: tvars.nonEmpty()*/;
340 for (List<Type> l = argtypes;
341 l.tail != null/*inlined: l.nonEmpty()*/ && !instNeeded;
342 l = l.tail) {
343 if (l.head.tag == FORALL) instNeeded = true;
344 }
346 if (instNeeded)
347 return
348 infer.instantiateMethod(env,
349 tvars,
350 (MethodType)mt,
351 m,
352 argtypes,
353 allowBoxing,
354 useVarargs,
355 warn);
356 return
357 argumentsAcceptable(argtypes, mt.getParameterTypes(),
358 allowBoxing, useVarargs, warn)
359 ? mt
360 : null;
361 }
363 /** Same but returns null instead throwing a NoInstanceException
364 */
365 Type instantiate(Env<AttrContext> env,
366 Type site,
367 Symbol m,
368 List<Type> argtypes,
369 List<Type> typeargtypes,
370 boolean allowBoxing,
371 boolean useVarargs,
372 Warner warn) {
373 try {
374 return rawInstantiate(env, site, m, argtypes, typeargtypes,
375 allowBoxing, useVarargs, warn);
376 } catch (Infer.InferenceException ex) {
377 return null;
378 }
379 }
381 /** Check if a parameter list accepts a list of args.
382 */
383 boolean argumentsAcceptable(List<Type> argtypes,
384 List<Type> formals,
385 boolean allowBoxing,
386 boolean useVarargs,
387 Warner warn) {
388 Type varargsFormal = useVarargs ? formals.last() : null;
389 while (argtypes.nonEmpty() && formals.head != varargsFormal) {
390 boolean works = allowBoxing
391 ? types.isConvertible(argtypes.head, formals.head, warn)
392 : types.isSubtypeUnchecked(argtypes.head, formals.head, warn);
393 if (!works) return false;
394 argtypes = argtypes.tail;
395 formals = formals.tail;
396 }
397 if (formals.head != varargsFormal) return false; // not enough args
398 if (!useVarargs)
399 return argtypes.isEmpty();
400 Type elt = types.elemtype(varargsFormal);
401 while (argtypes.nonEmpty()) {
402 if (!types.isConvertible(argtypes.head, elt, warn))
403 return false;
404 argtypes = argtypes.tail;
405 }
406 return true;
407 }
409 /* ***************************************************************************
410 * Symbol lookup
411 * the following naming conventions for arguments are used
412 *
413 * env is the environment where the symbol was mentioned
414 * site is the type of which the symbol is a member
415 * name is the symbol's name
416 * if no arguments are given
417 * argtypes are the value arguments, if we search for a method
418 *
419 * If no symbol was found, a ResolveError detailing the problem is returned.
420 ****************************************************************************/
422 /** Find field. Synthetic fields are always skipped.
423 * @param env The current environment.
424 * @param site The original type from where the selection takes place.
425 * @param name The name of the field.
426 * @param c The class to search for the field. This is always
427 * a superclass or implemented interface of site's class.
428 */
429 Symbol findField(Env<AttrContext> env,
430 Type site,
431 Name name,
432 TypeSymbol c) {
433 while (c.type.tag == TYPEVAR)
434 c = c.type.getUpperBound().tsym;
435 Symbol bestSoFar = varNotFound;
436 Symbol sym;
437 Scope.Entry e = c.members().lookup(name);
438 while (e.scope != null) {
439 if (e.sym.kind == VAR && (e.sym.flags_field & SYNTHETIC) == 0) {
440 return isAccessible(env, site, e.sym)
441 ? e.sym : new AccessError(env, site, e.sym);
442 }
443 e = e.next();
444 }
445 Type st = types.supertype(c.type);
446 if (st != null && (st.tag == CLASS || st.tag == TYPEVAR)) {
447 sym = findField(env, site, name, st.tsym);
448 if (sym.kind < bestSoFar.kind) bestSoFar = sym;
449 }
450 for (List<Type> l = types.interfaces(c.type);
451 bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
452 l = l.tail) {
453 sym = findField(env, site, name, l.head.tsym);
454 if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS &&
455 sym.owner != bestSoFar.owner)
456 bestSoFar = new AmbiguityError(bestSoFar, sym);
457 else if (sym.kind < bestSoFar.kind)
458 bestSoFar = sym;
459 }
460 return bestSoFar;
461 }
463 /** Resolve a field identifier, throw a fatal error if not found.
464 * @param pos The position to use for error reporting.
465 * @param env The environment current at the method invocation.
466 * @param site The type of the qualifying expression, in which
467 * identifier is searched.
468 * @param name The identifier's name.
469 */
470 public VarSymbol resolveInternalField(DiagnosticPosition pos, Env<AttrContext> env,
471 Type site, Name name) {
472 Symbol sym = findField(env, site, name, site.tsym);
473 if (sym.kind == VAR) return (VarSymbol)sym;
474 else throw new FatalError(
475 diags.fragment("fatal.err.cant.locate.field",
476 name));
477 }
479 /** Find unqualified variable or field with given name.
480 * Synthetic fields always skipped.
481 * @param env The current environment.
482 * @param name The name of the variable or field.
483 */
484 Symbol findVar(Env<AttrContext> env, Name name) {
485 Symbol bestSoFar = varNotFound;
486 Symbol sym;
487 Env<AttrContext> env1 = env;
488 boolean staticOnly = false;
489 while (env1.outer != null) {
490 if (isStatic(env1)) staticOnly = true;
491 Scope.Entry e = env1.info.scope.lookup(name);
492 while (e.scope != null &&
493 (e.sym.kind != VAR ||
494 (e.sym.flags_field & SYNTHETIC) != 0))
495 e = e.next();
496 sym = (e.scope != null)
497 ? e.sym
498 : findField(
499 env1, env1.enclClass.sym.type, name, env1.enclClass.sym);
500 if (sym.exists()) {
501 if (staticOnly &&
502 sym.kind == VAR &&
503 sym.owner.kind == TYP &&
504 (sym.flags() & STATIC) == 0)
505 return new StaticError(sym);
506 else
507 return sym;
508 } else if (sym.kind < bestSoFar.kind) {
509 bestSoFar = sym;
510 }
512 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
513 env1 = env1.outer;
514 }
516 sym = findField(env, syms.predefClass.type, name, syms.predefClass);
517 if (sym.exists())
518 return sym;
519 if (bestSoFar.exists())
520 return bestSoFar;
522 Scope.Entry e = env.toplevel.namedImportScope.lookup(name);
523 for (; e.scope != null; e = e.next()) {
524 sym = e.sym;
525 Type origin = e.getOrigin().owner.type;
526 if (sym.kind == VAR) {
527 if (e.sym.owner.type != origin)
528 sym = sym.clone(e.getOrigin().owner);
529 return isAccessible(env, origin, sym)
530 ? sym : new AccessError(env, origin, sym);
531 }
532 }
534 Symbol origin = null;
535 e = env.toplevel.starImportScope.lookup(name);
536 for (; e.scope != null; e = e.next()) {
537 sym = e.sym;
538 if (sym.kind != VAR)
539 continue;
540 // invariant: sym.kind == VAR
541 if (bestSoFar.kind < AMBIGUOUS && sym.owner != bestSoFar.owner)
542 return new AmbiguityError(bestSoFar, sym);
543 else if (bestSoFar.kind >= VAR) {
544 origin = e.getOrigin().owner;
545 bestSoFar = isAccessible(env, origin.type, sym)
546 ? sym : new AccessError(env, origin.type, sym);
547 }
548 }
549 if (bestSoFar.kind == VAR && bestSoFar.owner.type != origin.type)
550 return bestSoFar.clone(origin);
551 else
552 return bestSoFar;
553 }
555 Warner noteWarner = new Warner();
557 /** Select the best method for a call site among two choices.
558 * @param env The current environment.
559 * @param site The original type from where the
560 * selection takes place.
561 * @param argtypes The invocation's value arguments,
562 * @param typeargtypes The invocation's type arguments,
563 * @param sym Proposed new best match.
564 * @param bestSoFar Previously found best match.
565 * @param allowBoxing Allow boxing conversions of arguments.
566 * @param useVarargs Box trailing arguments into an array for varargs.
567 */
568 Symbol selectBest(Env<AttrContext> env,
569 Type site,
570 List<Type> argtypes,
571 List<Type> typeargtypes,
572 Symbol sym,
573 Symbol bestSoFar,
574 boolean allowBoxing,
575 boolean useVarargs,
576 boolean operator) {
577 if (sym.kind == ERR) return bestSoFar;
578 if (!sym.isInheritedIn(site.tsym, types)) return bestSoFar;
579 assert sym.kind < AMBIGUOUS;
580 try {
581 if (rawInstantiate(env, site, sym, argtypes, typeargtypes,
582 allowBoxing, useVarargs, Warner.noWarnings) == null) {
583 // inapplicable
584 switch (bestSoFar.kind) {
585 case ABSENT_MTH: return wrongMethod.setWrongSym(sym);
586 case WRONG_MTH: return wrongMethods;
587 default: return bestSoFar;
588 }
589 }
590 } catch (Infer.InferenceException ex) {
591 switch (bestSoFar.kind) {
592 case ABSENT_MTH:
593 return wrongMethod.setWrongSym(sym, ex.getDiagnostic());
594 case WRONG_MTH:
595 return wrongMethods;
596 default:
597 return bestSoFar;
598 }
599 }
600 if (!isAccessible(env, site, sym)) {
601 return (bestSoFar.kind == ABSENT_MTH)
602 ? new AccessError(env, site, sym)
603 : bestSoFar;
604 }
605 return (bestSoFar.kind > AMBIGUOUS)
606 ? sym
607 : mostSpecific(sym, bestSoFar, env, site,
608 allowBoxing && operator, useVarargs);
609 }
611 /* Return the most specific of the two methods for a call,
612 * given that both are accessible and applicable.
613 * @param m1 A new candidate for most specific.
614 * @param m2 The previous most specific candidate.
615 * @param env The current environment.
616 * @param site The original type from where the selection
617 * takes place.
618 * @param allowBoxing Allow boxing conversions of arguments.
619 * @param useVarargs Box trailing arguments into an array for varargs.
620 */
621 Symbol mostSpecific(Symbol m1,
622 Symbol m2,
623 Env<AttrContext> env,
624 final Type site,
625 boolean allowBoxing,
626 boolean useVarargs) {
627 switch (m2.kind) {
628 case MTH:
629 if (m1 == m2) return m1;
630 Type mt1 = types.memberType(site, m1);
631 noteWarner.unchecked = false;
632 boolean m1SignatureMoreSpecific =
633 (instantiate(env, site, m2, types.lowerBoundArgtypes(mt1), null,
634 allowBoxing, false, noteWarner) != null ||
635 useVarargs && instantiate(env, site, m2, types.lowerBoundArgtypes(mt1), null,
636 allowBoxing, true, noteWarner) != null) &&
637 !noteWarner.unchecked;
638 Type mt2 = types.memberType(site, m2);
639 noteWarner.unchecked = false;
640 boolean m2SignatureMoreSpecific =
641 (instantiate(env, site, m1, types.lowerBoundArgtypes(mt2), null,
642 allowBoxing, false, noteWarner) != null ||
643 useVarargs && instantiate(env, site, m1, types.lowerBoundArgtypes(mt2), null,
644 allowBoxing, true, noteWarner) != null) &&
645 !noteWarner.unchecked;
646 if (m1SignatureMoreSpecific && m2SignatureMoreSpecific) {
647 if (!types.overrideEquivalent(mt1, mt2))
648 return new AmbiguityError(m1, m2);
649 // same signature; select (a) the non-bridge method, or
650 // (b) the one that overrides the other, or (c) the concrete
651 // one, or (d) merge both abstract signatures
652 if ((m1.flags() & BRIDGE) != (m2.flags() & BRIDGE)) {
653 return ((m1.flags() & BRIDGE) != 0) ? m2 : m1;
654 }
655 // if one overrides or hides the other, use it
656 TypeSymbol m1Owner = (TypeSymbol)m1.owner;
657 TypeSymbol m2Owner = (TypeSymbol)m2.owner;
658 if (types.asSuper(m1Owner.type, m2Owner) != null &&
659 ((m1.owner.flags_field & INTERFACE) == 0 ||
660 (m2.owner.flags_field & INTERFACE) != 0) &&
661 m1.overrides(m2, m1Owner, types, false))
662 return m1;
663 if (types.asSuper(m2Owner.type, m1Owner) != null &&
664 ((m2.owner.flags_field & INTERFACE) == 0 ||
665 (m1.owner.flags_field & INTERFACE) != 0) &&
666 m2.overrides(m1, m2Owner, types, false))
667 return m2;
668 boolean m1Abstract = (m1.flags() & ABSTRACT) != 0;
669 boolean m2Abstract = (m2.flags() & ABSTRACT) != 0;
670 if (m1Abstract && !m2Abstract) return m2;
671 if (m2Abstract && !m1Abstract) return m1;
672 // both abstract or both concrete
673 if (!m1Abstract && !m2Abstract)
674 return new AmbiguityError(m1, m2);
675 // check that both signatures have the same erasure
676 if (!types.isSameTypes(m1.erasure(types).getParameterTypes(),
677 m2.erasure(types).getParameterTypes()))
678 return new AmbiguityError(m1, m2);
679 // both abstract, neither overridden; merge throws clause and result type
680 Symbol mostSpecific;
681 Type result2 = mt2.getReturnType();
682 if (mt2.tag == FORALL)
683 result2 = types.subst(result2, ((ForAll)mt2).tvars, ((ForAll)mt1).tvars);
684 if (types.isSubtype(mt1.getReturnType(), result2)) {
685 mostSpecific = m1;
686 } else if (types.isSubtype(result2, mt1.getReturnType())) {
687 mostSpecific = m2;
688 } else {
689 // Theoretically, this can't happen, but it is possible
690 // due to error recovery or mixing incompatible class files
691 return new AmbiguityError(m1, m2);
692 }
693 MethodSymbol result = new MethodSymbol(
694 mostSpecific.flags(),
695 mostSpecific.name,
696 null,
697 mostSpecific.owner) {
698 @Override
699 public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult) {
700 if (origin == site.tsym)
701 return this;
702 else
703 return super.implementation(origin, types, checkResult);
704 }
705 };
706 result.type = (Type)mostSpecific.type.clone();
707 result.type.setThrown(chk.intersect(mt1.getThrownTypes(),
708 mt2.getThrownTypes()));
709 return result;
710 }
711 if (m1SignatureMoreSpecific) return m1;
712 if (m2SignatureMoreSpecific) return m2;
713 return new AmbiguityError(m1, m2);
714 case AMBIGUOUS:
715 AmbiguityError e = (AmbiguityError)m2;
716 Symbol err1 = mostSpecific(m1, e.sym, env, site, allowBoxing, useVarargs);
717 Symbol err2 = mostSpecific(m1, e.sym2, env, site, allowBoxing, useVarargs);
718 if (err1 == err2) return err1;
719 if (err1 == e.sym && err2 == e.sym2) return m2;
720 if (err1 instanceof AmbiguityError &&
721 err2 instanceof AmbiguityError &&
722 ((AmbiguityError)err1).sym == ((AmbiguityError)err2).sym)
723 return new AmbiguityError(m1, m2);
724 else
725 return new AmbiguityError(err1, err2);
726 default:
727 throw new AssertionError();
728 }
729 }
731 /** Find best qualified method matching given name, type and value
732 * arguments.
733 * @param env The current environment.
734 * @param site The original type from where the selection
735 * takes place.
736 * @param name The method's name.
737 * @param argtypes The method's value arguments.
738 * @param typeargtypes The method's type arguments
739 * @param allowBoxing Allow boxing conversions of arguments.
740 * @param useVarargs Box trailing arguments into an array for varargs.
741 */
742 Symbol findMethod(Env<AttrContext> env,
743 Type site,
744 Name name,
745 List<Type> argtypes,
746 List<Type> typeargtypes,
747 boolean allowBoxing,
748 boolean useVarargs,
749 boolean operator) {
750 return findMethod(env,
751 site,
752 name,
753 argtypes,
754 typeargtypes,
755 site.tsym.type,
756 true,
757 methodNotFound,
758 allowBoxing,
759 useVarargs,
760 operator);
761 }
762 // where
763 private Symbol findMethod(Env<AttrContext> env,
764 Type site,
765 Name name,
766 List<Type> argtypes,
767 List<Type> typeargtypes,
768 Type intype,
769 boolean abstractok,
770 Symbol bestSoFar,
771 boolean allowBoxing,
772 boolean useVarargs,
773 boolean operator) {
774 for (Type ct = intype; ct.tag == CLASS || ct.tag == TYPEVAR; ct = types.supertype(ct)) {
775 while (ct.tag == TYPEVAR)
776 ct = ct.getUpperBound();
777 ClassSymbol c = (ClassSymbol)ct.tsym;
778 if ((c.flags() & (ABSTRACT | INTERFACE | ENUM)) == 0)
779 abstractok = false;
780 for (Scope.Entry e = c.members().lookup(name);
781 e.scope != null;
782 e = e.next()) {
783 //- System.out.println(" e " + e.sym);
784 if (e.sym.kind == MTH &&
785 (e.sym.flags_field & SYNTHETIC) == 0) {
786 bestSoFar = selectBest(env, site, argtypes, typeargtypes,
787 e.sym, bestSoFar,
788 allowBoxing,
789 useVarargs,
790 operator);
791 }
792 }
793 if (name == names.init)
794 break;
795 //- System.out.println(" - " + bestSoFar);
796 if (abstractok) {
797 Symbol concrete = methodNotFound;
798 if ((bestSoFar.flags() & ABSTRACT) == 0)
799 concrete = bestSoFar;
800 for (List<Type> l = types.interfaces(c.type);
801 l.nonEmpty();
802 l = l.tail) {
803 bestSoFar = findMethod(env, site, name, argtypes,
804 typeargtypes,
805 l.head, abstractok, bestSoFar,
806 allowBoxing, useVarargs, operator);
807 }
808 if (concrete != bestSoFar &&
809 concrete.kind < ERR && bestSoFar.kind < ERR &&
810 types.isSubSignature(concrete.type, bestSoFar.type))
811 bestSoFar = concrete;
812 }
813 }
814 return bestSoFar;
815 }
817 /** Find unqualified method matching given name, type and value arguments.
818 * @param env The current environment.
819 * @param name The method's name.
820 * @param argtypes The method's value arguments.
821 * @param typeargtypes The method's type arguments.
822 * @param allowBoxing Allow boxing conversions of arguments.
823 * @param useVarargs Box trailing arguments into an array for varargs.
824 */
825 Symbol findFun(Env<AttrContext> env, Name name,
826 List<Type> argtypes, List<Type> typeargtypes,
827 boolean allowBoxing, boolean useVarargs) {
828 Symbol bestSoFar = methodNotFound;
829 Symbol sym;
830 Env<AttrContext> env1 = env;
831 boolean staticOnly = false;
832 while (env1.outer != null) {
833 if (isStatic(env1)) staticOnly = true;
834 sym = findMethod(
835 env1, env1.enclClass.sym.type, name, argtypes, typeargtypes,
836 allowBoxing, useVarargs, false);
837 if (sym.exists()) {
838 if (staticOnly &&
839 sym.kind == MTH &&
840 sym.owner.kind == TYP &&
841 (sym.flags() & STATIC) == 0) return new StaticError(sym);
842 else return sym;
843 } else if (sym.kind < bestSoFar.kind) {
844 bestSoFar = sym;
845 }
846 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
847 env1 = env1.outer;
848 }
850 sym = findMethod(env, syms.predefClass.type, name, argtypes,
851 typeargtypes, allowBoxing, useVarargs, false);
852 if (sym.exists())
853 return sym;
855 Scope.Entry e = env.toplevel.namedImportScope.lookup(name);
856 for (; e.scope != null; e = e.next()) {
857 sym = e.sym;
858 Type origin = e.getOrigin().owner.type;
859 if (sym.kind == MTH) {
860 if (e.sym.owner.type != origin)
861 sym = sym.clone(e.getOrigin().owner);
862 if (!isAccessible(env, origin, sym))
863 sym = new AccessError(env, origin, sym);
864 bestSoFar = selectBest(env, origin,
865 argtypes, typeargtypes,
866 sym, bestSoFar,
867 allowBoxing, useVarargs, false);
868 }
869 }
870 if (bestSoFar.exists())
871 return bestSoFar;
873 e = env.toplevel.starImportScope.lookup(name);
874 for (; e.scope != null; e = e.next()) {
875 sym = e.sym;
876 Type origin = e.getOrigin().owner.type;
877 if (sym.kind == MTH) {
878 if (e.sym.owner.type != origin)
879 sym = sym.clone(e.getOrigin().owner);
880 if (!isAccessible(env, origin, sym))
881 sym = new AccessError(env, origin, sym);
882 bestSoFar = selectBest(env, origin,
883 argtypes, typeargtypes,
884 sym, bestSoFar,
885 allowBoxing, useVarargs, false);
886 }
887 }
888 return bestSoFar;
889 }
891 /** Find or create an implicit method of exactly the given type (after erasure).
892 * Searches in a side table, not the main scope of the site.
893 * This emulates the lookup process required by JSR 292 in JVM.
894 * @param env The current environment.
895 * @param site The original type from where the selection
896 * takes place.
897 * @param name The method's name.
898 * @param argtypes The method's value arguments.
899 * @param typeargtypes The method's type arguments
900 */
901 Symbol findImplicitMethod(Env<AttrContext> env,
902 Type site,
903 Name name,
904 List<Type> argtypes,
905 List<Type> typeargtypes) {
906 assert allowInvokedynamic;
907 assert site == syms.invokeDynamicType || (site == syms.methodHandleType && name == names.invoke);
908 ClassSymbol c = (ClassSymbol) site.tsym;
909 Scope implicit = c.members().next;
910 if (implicit == null) {
911 c.members().next = implicit = new Scope(c);
912 }
913 Type restype;
914 if (typeargtypes.isEmpty()) {
915 restype = syms.objectType;
916 } else {
917 restype = typeargtypes.head;
918 if (!typeargtypes.tail.isEmpty())
919 return methodNotFound;
920 }
921 List<Type> paramtypes = Type.map(argtypes, implicitArgType);
922 MethodType mtype = new MethodType(paramtypes,
923 restype,
924 List.<Type>nil(),
925 syms.methodClass);
926 int flags = PUBLIC | ABSTRACT;
927 if (site == syms.invokeDynamicType) flags |= STATIC;
928 Symbol m = null;
929 for (Scope.Entry e = implicit.lookup(name);
930 e.scope != null;
931 e = e.next()) {
932 Symbol sym = e.sym;
933 assert sym.kind == MTH;
934 if (types.isSameType(mtype, sym.type)
935 && (sym.flags() & STATIC) == (flags & STATIC)) {
936 m = sym;
937 break;
938 }
939 }
940 if (m == null) {
941 // create the desired method
942 m = new MethodSymbol(flags, name, mtype, c);
943 implicit.enter(m);
944 }
945 assert argumentsAcceptable(argtypes, types.memberType(site, m).getParameterTypes(),
946 false, false, Warner.noWarnings);
947 assert null != instantiate(env, site, m, argtypes, typeargtypes, false, false, Warner.noWarnings);
948 return m;
949 }
950 //where
951 Mapping implicitArgType = new Mapping ("implicitArgType") {
952 public Type apply(Type t) { return implicitArgType(t); }
953 };
954 Type implicitArgType(Type argType) {
955 argType = types.erasure(argType);
956 if (argType.tag == BOT)
957 // nulls type as the marker type Null (which has no instances)
958 // TO DO: figure out how to access java.lang.Null safely, else throw nice error
959 //argType = types.boxedClass(syms.botType).type;
960 argType = types.boxedClass(syms.voidType).type; // REMOVE
961 return argType;
962 }
964 /** Load toplevel or member class with given fully qualified name and
965 * verify that it is accessible.
966 * @param env The current environment.
967 * @param name The fully qualified name of the class to be loaded.
968 */
969 Symbol loadClass(Env<AttrContext> env, Name name) {
970 try {
971 ClassSymbol c = reader.loadClass(name);
972 return isAccessible(env, c) ? c : new AccessError(c);
973 } catch (ClassReader.BadClassFile err) {
974 throw err;
975 } catch (CompletionFailure ex) {
976 return typeNotFound;
977 }
978 }
980 /** Find qualified member type.
981 * @param env The current environment.
982 * @param site The original type from where the selection takes
983 * place.
984 * @param name The type's name.
985 * @param c The class to search for the member type. This is
986 * always a superclass or implemented interface of
987 * site's class.
988 */
989 Symbol findMemberType(Env<AttrContext> env,
990 Type site,
991 Name name,
992 TypeSymbol c) {
993 Symbol bestSoFar = typeNotFound;
994 Symbol sym;
995 Scope.Entry e = c.members().lookup(name);
996 while (e.scope != null) {
997 if (e.sym.kind == TYP) {
998 return isAccessible(env, site, e.sym)
999 ? e.sym
1000 : new AccessError(env, site, e.sym);
1001 }
1002 e = e.next();
1003 }
1004 Type st = types.supertype(c.type);
1005 if (st != null && st.tag == CLASS) {
1006 sym = findMemberType(env, site, name, st.tsym);
1007 if (sym.kind < bestSoFar.kind) bestSoFar = sym;
1008 }
1009 for (List<Type> l = types.interfaces(c.type);
1010 bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
1011 l = l.tail) {
1012 sym = findMemberType(env, site, name, l.head.tsym);
1013 if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS &&
1014 sym.owner != bestSoFar.owner)
1015 bestSoFar = new AmbiguityError(bestSoFar, sym);
1016 else if (sym.kind < bestSoFar.kind)
1017 bestSoFar = sym;
1018 }
1019 return bestSoFar;
1020 }
1022 /** Find a global type in given scope and load corresponding class.
1023 * @param env The current environment.
1024 * @param scope The scope in which to look for the type.
1025 * @param name The type's name.
1026 */
1027 Symbol findGlobalType(Env<AttrContext> env, Scope scope, Name name) {
1028 Symbol bestSoFar = typeNotFound;
1029 for (Scope.Entry e = scope.lookup(name); e.scope != null; e = e.next()) {
1030 Symbol sym = loadClass(env, e.sym.flatName());
1031 if (bestSoFar.kind == TYP && sym.kind == TYP &&
1032 bestSoFar != sym)
1033 return new AmbiguityError(bestSoFar, sym);
1034 else if (sym.kind < bestSoFar.kind)
1035 bestSoFar = sym;
1036 }
1037 return bestSoFar;
1038 }
1040 /** Find an unqualified type symbol.
1041 * @param env The current environment.
1042 * @param name The type's name.
1043 */
1044 Symbol findType(Env<AttrContext> env, Name name) {
1045 Symbol bestSoFar = typeNotFound;
1046 Symbol sym;
1047 boolean staticOnly = false;
1048 for (Env<AttrContext> env1 = env; env1.outer != null; env1 = env1.outer) {
1049 if (isStatic(env1)) staticOnly = true;
1050 for (Scope.Entry e = env1.info.scope.lookup(name);
1051 e.scope != null;
1052 e = e.next()) {
1053 if (e.sym.kind == TYP) {
1054 if (staticOnly &&
1055 e.sym.type.tag == TYPEVAR &&
1056 e.sym.owner.kind == TYP) return new StaticError(e.sym);
1057 return e.sym;
1058 }
1059 }
1061 sym = findMemberType(env1, env1.enclClass.sym.type, name,
1062 env1.enclClass.sym);
1063 if (staticOnly && sym.kind == TYP &&
1064 sym.type.tag == CLASS &&
1065 sym.type.getEnclosingType().tag == CLASS &&
1066 env1.enclClass.sym.type.isParameterized() &&
1067 sym.type.getEnclosingType().isParameterized())
1068 return new StaticError(sym);
1069 else if (sym.exists()) return sym;
1070 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
1072 JCClassDecl encl = env1.baseClause ? (JCClassDecl)env1.tree : env1.enclClass;
1073 if ((encl.sym.flags() & STATIC) != 0)
1074 staticOnly = true;
1075 }
1077 if (env.tree.getTag() != JCTree.IMPORT) {
1078 sym = findGlobalType(env, env.toplevel.namedImportScope, name);
1079 if (sym.exists()) return sym;
1080 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
1082 sym = findGlobalType(env, env.toplevel.packge.members(), name);
1083 if (sym.exists()) return sym;
1084 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
1086 sym = findGlobalType(env, env.toplevel.starImportScope, name);
1087 if (sym.exists()) return sym;
1088 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
1089 }
1091 return bestSoFar;
1092 }
1094 /** Find an unqualified identifier which matches a specified kind set.
1095 * @param env The current environment.
1096 * @param name The indentifier's name.
1097 * @param kind Indicates the possible symbol kinds
1098 * (a subset of VAL, TYP, PCK).
1099 */
1100 Symbol findIdent(Env<AttrContext> env, Name name, int kind) {
1101 Symbol bestSoFar = typeNotFound;
1102 Symbol sym;
1104 if ((kind & VAR) != 0) {
1105 sym = findVar(env, name);
1106 if (sym.exists()) return sym;
1107 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
1108 }
1110 if ((kind & TYP) != 0) {
1111 sym = findType(env, name);
1112 if (sym.exists()) return sym;
1113 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
1114 }
1116 if ((kind & PCK) != 0) return reader.enterPackage(name);
1117 else return bestSoFar;
1118 }
1120 /** Find an identifier in a package which matches a specified kind set.
1121 * @param env The current environment.
1122 * @param name The identifier's name.
1123 * @param kind Indicates the possible symbol kinds
1124 * (a nonempty subset of TYP, PCK).
1125 */
1126 Symbol findIdentInPackage(Env<AttrContext> env, TypeSymbol pck,
1127 Name name, int kind) {
1128 Name fullname = TypeSymbol.formFullName(name, pck);
1129 Symbol bestSoFar = typeNotFound;
1130 PackageSymbol pack = null;
1131 if ((kind & PCK) != 0) {
1132 pack = reader.enterPackage(fullname);
1133 if (pack.exists()) return pack;
1134 }
1135 if ((kind & TYP) != 0) {
1136 Symbol sym = loadClass(env, fullname);
1137 if (sym.exists()) {
1138 // don't allow programs to use flatnames
1139 if (name == sym.name) return sym;
1140 }
1141 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
1142 }
1143 return (pack != null) ? pack : bestSoFar;
1144 }
1146 /** Find an identifier among the members of a given type `site'.
1147 * @param env The current environment.
1148 * @param site The type containing the symbol to be found.
1149 * @param name The identifier's name.
1150 * @param kind Indicates the possible symbol kinds
1151 * (a subset of VAL, TYP).
1152 */
1153 Symbol findIdentInType(Env<AttrContext> env, Type site,
1154 Name name, int kind) {
1155 Symbol bestSoFar = typeNotFound;
1156 Symbol sym;
1157 if ((kind & VAR) != 0) {
1158 sym = findField(env, site, name, site.tsym);
1159 if (sym.exists()) return sym;
1160 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
1161 }
1163 if ((kind & TYP) != 0) {
1164 sym = findMemberType(env, site, name, site.tsym);
1165 if (sym.exists()) return sym;
1166 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
1167 }
1168 return bestSoFar;
1169 }
1171 /* ***************************************************************************
1172 * Access checking
1173 * The following methods convert ResolveErrors to ErrorSymbols, issuing
1174 * an error message in the process
1175 ****************************************************************************/
1177 /** If `sym' is a bad symbol: report error and return errSymbol
1178 * else pass through unchanged,
1179 * additional arguments duplicate what has been used in trying to find the
1180 * symbol (--> flyweight pattern). This improves performance since we
1181 * expect misses to happen frequently.
1182 *
1183 * @param sym The symbol that was found, or a ResolveError.
1184 * @param pos The position to use for error reporting.
1185 * @param site The original type from where the selection took place.
1186 * @param name The symbol's name.
1187 * @param argtypes The invocation's value arguments,
1188 * if we looked for a method.
1189 * @param typeargtypes The invocation's type arguments,
1190 * if we looked for a method.
1191 */
1192 Symbol access(Symbol sym,
1193 DiagnosticPosition pos,
1194 Type site,
1195 Name name,
1196 boolean qualified,
1197 List<Type> argtypes,
1198 List<Type> typeargtypes) {
1199 if (sym.kind >= AMBIGUOUS) {
1200 ResolveError errSym = (ResolveError)sym;
1201 if (!site.isErroneous() &&
1202 !Type.isErroneous(argtypes) &&
1203 (typeargtypes==null || !Type.isErroneous(typeargtypes)))
1204 logResolveError(errSym, pos, site, name, argtypes, typeargtypes);
1205 sym = errSym.access(name, qualified ? site.tsym : syms.noSymbol);
1206 }
1207 return sym;
1208 }
1210 /** Same as above, but without type arguments and arguments.
1211 */
1212 Symbol access(Symbol sym,
1213 DiagnosticPosition pos,
1214 Type site,
1215 Name name,
1216 boolean qualified) {
1217 if (sym.kind >= AMBIGUOUS)
1218 return access(sym, pos, site, name, qualified, List.<Type>nil(), null);
1219 else
1220 return sym;
1221 }
1223 /** Check that sym is not an abstract method.
1224 */
1225 void checkNonAbstract(DiagnosticPosition pos, Symbol sym) {
1226 if ((sym.flags() & ABSTRACT) != 0)
1227 log.error(pos, "abstract.cant.be.accessed.directly",
1228 kindName(sym), sym, sym.location());
1229 }
1231 /* ***************************************************************************
1232 * Debugging
1233 ****************************************************************************/
1235 /** print all scopes starting with scope s and proceeding outwards.
1236 * used for debugging.
1237 */
1238 public void printscopes(Scope s) {
1239 while (s != null) {
1240 if (s.owner != null)
1241 System.err.print(s.owner + ": ");
1242 for (Scope.Entry e = s.elems; e != null; e = e.sibling) {
1243 if ((e.sym.flags() & ABSTRACT) != 0)
1244 System.err.print("abstract ");
1245 System.err.print(e.sym + " ");
1246 }
1247 System.err.println();
1248 s = s.next;
1249 }
1250 }
1252 void printscopes(Env<AttrContext> env) {
1253 while (env.outer != null) {
1254 System.err.println("------------------------------");
1255 printscopes(env.info.scope);
1256 env = env.outer;
1257 }
1258 }
1260 public void printscopes(Type t) {
1261 while (t.tag == CLASS) {
1262 printscopes(t.tsym.members());
1263 t = types.supertype(t);
1264 }
1265 }
1267 /* ***************************************************************************
1268 * Name resolution
1269 * Naming conventions are as for symbol lookup
1270 * Unlike the find... methods these methods will report access errors
1271 ****************************************************************************/
1273 /** Resolve an unqualified (non-method) identifier.
1274 * @param pos The position to use for error reporting.
1275 * @param env The environment current at the identifier use.
1276 * @param name The identifier's name.
1277 * @param kind The set of admissible symbol kinds for the identifier.
1278 */
1279 Symbol resolveIdent(DiagnosticPosition pos, Env<AttrContext> env,
1280 Name name, int kind) {
1281 return access(
1282 findIdent(env, name, kind),
1283 pos, env.enclClass.sym.type, name, false);
1284 }
1286 /** Resolve an unqualified method identifier.
1287 * @param pos The position to use for error reporting.
1288 * @param env The environment current at the method invocation.
1289 * @param name The identifier's name.
1290 * @param argtypes The types of the invocation's value arguments.
1291 * @param typeargtypes The types of the invocation's type arguments.
1292 */
1293 Symbol resolveMethod(DiagnosticPosition pos,
1294 Env<AttrContext> env,
1295 Name name,
1296 List<Type> argtypes,
1297 List<Type> typeargtypes) {
1298 Symbol sym = methodNotFound;
1299 List<MethodResolutionPhase> steps = methodResolutionSteps;
1300 while (steps.nonEmpty() &&
1301 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
1302 sym.kind >= ERRONEOUS) {
1303 sym = findFun(env, name, argtypes, typeargtypes,
1304 steps.head.isBoxingRequired,
1305 env.info.varArgs = steps.head.isVarargsRequired);
1306 methodResolutionCache.put(steps.head, sym);
1307 steps = steps.tail;
1308 }
1309 if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error
1310 MethodResolutionPhase errPhase =
1311 firstErroneousResolutionPhase();
1312 sym = access(methodResolutionCache.get(errPhase),
1313 pos, env.enclClass.sym.type, name, false, argtypes, typeargtypes);
1314 env.info.varArgs = errPhase.isVarargsRequired;
1315 }
1316 return sym;
1317 }
1319 /** Resolve a qualified method identifier
1320 * @param pos The position to use for error reporting.
1321 * @param env The environment current at the method invocation.
1322 * @param site The type of the qualifying expression, in which
1323 * identifier is searched.
1324 * @param name The identifier's name.
1325 * @param argtypes The types of the invocation's value arguments.
1326 * @param typeargtypes The types of the invocation's type arguments.
1327 */
1328 Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env,
1329 Type site, Name name, List<Type> argtypes,
1330 List<Type> typeargtypes) {
1331 Symbol sym = methodNotFound;
1332 List<MethodResolutionPhase> steps = methodResolutionSteps;
1333 while (steps.nonEmpty() &&
1334 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
1335 sym.kind >= ERRONEOUS) {
1336 sym = findMethod(env, site, name, argtypes, typeargtypes,
1337 steps.head.isBoxingRequired(),
1338 env.info.varArgs = steps.head.isVarargsRequired(), false);
1339 methodResolutionCache.put(steps.head, sym);
1340 steps = steps.tail;
1341 }
1342 if (sym.kind >= AMBIGUOUS &&
1343 allowInvokedynamic &&
1344 (site == syms.invokeDynamicType ||
1345 site == syms.methodHandleType && name == names.invoke)) {
1346 // lookup failed; supply an exactly-typed implicit method
1347 sym = findImplicitMethod(env, site, name, argtypes, typeargtypes);
1348 env.info.varArgs = false;
1349 }
1350 if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error
1351 MethodResolutionPhase errPhase =
1352 firstErroneousResolutionPhase();
1353 sym = access(methodResolutionCache.get(errPhase),
1354 pos, site, name, true, argtypes, typeargtypes);
1355 env.info.varArgs = errPhase.isVarargsRequired;
1356 }
1357 return sym;
1358 }
1360 /** Resolve a qualified method identifier, throw a fatal error if not
1361 * found.
1362 * @param pos The position to use for error reporting.
1363 * @param env The environment current at the method invocation.
1364 * @param site The type of the qualifying expression, in which
1365 * identifier is searched.
1366 * @param name The identifier's name.
1367 * @param argtypes The types of the invocation's value arguments.
1368 * @param typeargtypes The types of the invocation's type arguments.
1369 */
1370 public MethodSymbol resolveInternalMethod(DiagnosticPosition pos, Env<AttrContext> env,
1371 Type site, Name name,
1372 List<Type> argtypes,
1373 List<Type> typeargtypes) {
1374 Symbol sym = resolveQualifiedMethod(
1375 pos, env, site, name, argtypes, typeargtypes);
1376 if (sym.kind == MTH) return (MethodSymbol)sym;
1377 else throw new FatalError(
1378 diags.fragment("fatal.err.cant.locate.meth",
1379 name));
1380 }
1382 /** Resolve constructor.
1383 * @param pos The position to use for error reporting.
1384 * @param env The environment current at the constructor invocation.
1385 * @param site The type of class for which a constructor is searched.
1386 * @param argtypes The types of the constructor invocation's value
1387 * arguments.
1388 * @param typeargtypes The types of the constructor invocation's type
1389 * arguments.
1390 */
1391 Symbol resolveConstructor(DiagnosticPosition pos,
1392 Env<AttrContext> env,
1393 Type site,
1394 List<Type> argtypes,
1395 List<Type> typeargtypes) {
1396 Symbol sym = methodNotFound;
1397 List<MethodResolutionPhase> steps = methodResolutionSteps;
1398 while (steps.nonEmpty() &&
1399 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
1400 sym.kind >= ERRONEOUS) {
1401 sym = resolveConstructor(pos, env, site, argtypes, typeargtypes,
1402 steps.head.isBoxingRequired(),
1403 env.info.varArgs = steps.head.isVarargsRequired());
1404 methodResolutionCache.put(steps.head, sym);
1405 steps = steps.tail;
1406 }
1407 if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error
1408 MethodResolutionPhase errPhase = firstErroneousResolutionPhase();
1409 sym = access(methodResolutionCache.get(errPhase),
1410 pos, site, names.init, true, argtypes, typeargtypes);
1411 env.info.varArgs = errPhase.isVarargsRequired();
1412 }
1413 return sym;
1414 }
1416 /** Resolve constructor using diamond inference.
1417 * @param pos The position to use for error reporting.
1418 * @param env The environment current at the constructor invocation.
1419 * @param site The type of class for which a constructor is searched.
1420 * The scope of this class has been touched in attribution.
1421 * @param argtypes The types of the constructor invocation's value
1422 * arguments.
1423 * @param typeargtypes The types of the constructor invocation's type
1424 * arguments.
1425 */
1426 Symbol resolveDiamond(DiagnosticPosition pos,
1427 Env<AttrContext> env,
1428 Type site,
1429 List<Type> argtypes,
1430 List<Type> typeargtypes, boolean reportErrors) {
1431 Symbol sym = methodNotFound;
1432 JCDiagnostic explanation = null;
1433 List<MethodResolutionPhase> steps = methodResolutionSteps;
1434 while (steps.nonEmpty() &&
1435 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
1436 sym.kind >= ERRONEOUS) {
1437 sym = resolveConstructor(pos, env, site, argtypes, typeargtypes,
1438 steps.head.isBoxingRequired(),
1439 env.info.varArgs = steps.head.isVarargsRequired());
1440 methodResolutionCache.put(steps.head, sym);
1441 if (sym.kind == WRONG_MTH &&
1442 ((InapplicableSymbolError)sym).explanation != null) {
1443 //if the symbol is an inapplicable method symbol, then the
1444 //explanation contains the reason for which inference failed
1445 explanation = ((InapplicableSymbolError)sym).explanation;
1446 }
1447 steps = steps.tail;
1448 }
1449 if (sym.kind >= AMBIGUOUS && reportErrors) {
1450 String key = explanation == null ?
1451 "cant.apply.diamond" :
1452 "cant.apply.diamond.1";
1453 log.error(pos, key, diags.fragment("diamond", site.tsym), explanation);
1454 }
1455 return sym;
1456 }
1458 /** Resolve constructor.
1459 * @param pos The position to use for error reporting.
1460 * @param env The environment current at the constructor invocation.
1461 * @param site The type of class for which a constructor is searched.
1462 * @param argtypes The types of the constructor invocation's value
1463 * arguments.
1464 * @param typeargtypes The types of the constructor invocation's type
1465 * arguments.
1466 * @param allowBoxing Allow boxing and varargs conversions.
1467 * @param useVarargs Box trailing arguments into an array for varargs.
1468 */
1469 Symbol resolveConstructor(DiagnosticPosition pos, Env<AttrContext> env,
1470 Type site, List<Type> argtypes,
1471 List<Type> typeargtypes,
1472 boolean allowBoxing,
1473 boolean useVarargs) {
1474 Symbol sym = findMethod(env, site,
1475 names.init, argtypes,
1476 typeargtypes, allowBoxing,
1477 useVarargs, false);
1478 if ((sym.flags() & DEPRECATED) != 0 &&
1479 (env.info.scope.owner.flags() & DEPRECATED) == 0 &&
1480 env.info.scope.owner.outermostClass() != sym.outermostClass())
1481 chk.warnDeprecated(pos, sym);
1482 return sym;
1483 }
1485 /** Resolve a constructor, throw a fatal error if not found.
1486 * @param pos The position to use for error reporting.
1487 * @param env The environment current at the method invocation.
1488 * @param site The type to be constructed.
1489 * @param argtypes The types of the invocation's value arguments.
1490 * @param typeargtypes The types of the invocation's type arguments.
1491 */
1492 public MethodSymbol resolveInternalConstructor(DiagnosticPosition pos, Env<AttrContext> env,
1493 Type site,
1494 List<Type> argtypes,
1495 List<Type> typeargtypes) {
1496 Symbol sym = resolveConstructor(
1497 pos, env, site, argtypes, typeargtypes);
1498 if (sym.kind == MTH) return (MethodSymbol)sym;
1499 else throw new FatalError(
1500 diags.fragment("fatal.err.cant.locate.ctor", site));
1501 }
1503 /** Resolve operator.
1504 * @param pos The position to use for error reporting.
1505 * @param optag The tag of the operation tree.
1506 * @param env The environment current at the operation.
1507 * @param argtypes The types of the operands.
1508 */
1509 Symbol resolveOperator(DiagnosticPosition pos, int optag,
1510 Env<AttrContext> env, List<Type> argtypes) {
1511 Name name = treeinfo.operatorName(optag);
1512 Symbol sym = findMethod(env, syms.predefClass.type, name, argtypes,
1513 null, false, false, true);
1514 if (boxingEnabled && sym.kind >= WRONG_MTHS)
1515 sym = findMethod(env, syms.predefClass.type, name, argtypes,
1516 null, true, false, true);
1517 return access(sym, pos, env.enclClass.sym.type, name,
1518 false, argtypes, null);
1519 }
1521 /** Resolve operator.
1522 * @param pos The position to use for error reporting.
1523 * @param optag The tag of the operation tree.
1524 * @param env The environment current at the operation.
1525 * @param arg The type of the operand.
1526 */
1527 Symbol resolveUnaryOperator(DiagnosticPosition pos, int optag, Env<AttrContext> env, Type arg) {
1528 return resolveOperator(pos, optag, env, List.of(arg));
1529 }
1531 /** Resolve binary operator.
1532 * @param pos The position to use for error reporting.
1533 * @param optag The tag of the operation tree.
1534 * @param env The environment current at the operation.
1535 * @param left The types of the left operand.
1536 * @param right The types of the right operand.
1537 */
1538 Symbol resolveBinaryOperator(DiagnosticPosition pos,
1539 int optag,
1540 Env<AttrContext> env,
1541 Type left,
1542 Type right) {
1543 return resolveOperator(pos, optag, env, List.of(left, right));
1544 }
1546 /**
1547 * Resolve `c.name' where name == this or name == super.
1548 * @param pos The position to use for error reporting.
1549 * @param env The environment current at the expression.
1550 * @param c The qualifier.
1551 * @param name The identifier's name.
1552 */
1553 Symbol resolveSelf(DiagnosticPosition pos,
1554 Env<AttrContext> env,
1555 TypeSymbol c,
1556 Name name) {
1557 Env<AttrContext> env1 = env;
1558 boolean staticOnly = false;
1559 while (env1.outer != null) {
1560 if (isStatic(env1)) staticOnly = true;
1561 if (env1.enclClass.sym == c) {
1562 Symbol sym = env1.info.scope.lookup(name).sym;
1563 if (sym != null) {
1564 if (staticOnly) sym = new StaticError(sym);
1565 return access(sym, pos, env.enclClass.sym.type,
1566 name, true);
1567 }
1568 }
1569 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
1570 env1 = env1.outer;
1571 }
1572 log.error(pos, "not.encl.class", c);
1573 return syms.errSymbol;
1574 }
1576 /**
1577 * Resolve `c.this' for an enclosing class c that contains the
1578 * named member.
1579 * @param pos The position to use for error reporting.
1580 * @param env The environment current at the expression.
1581 * @param member The member that must be contained in the result.
1582 */
1583 Symbol resolveSelfContaining(DiagnosticPosition pos,
1584 Env<AttrContext> env,
1585 Symbol member) {
1586 Name name = names._this;
1587 Env<AttrContext> env1 = env;
1588 boolean staticOnly = false;
1589 while (env1.outer != null) {
1590 if (isStatic(env1)) staticOnly = true;
1591 if (env1.enclClass.sym.isSubClass(member.owner, types) &&
1592 isAccessible(env, env1.enclClass.sym.type, member)) {
1593 Symbol sym = env1.info.scope.lookup(name).sym;
1594 if (sym != null) {
1595 if (staticOnly) sym = new StaticError(sym);
1596 return access(sym, pos, env.enclClass.sym.type,
1597 name, true);
1598 }
1599 }
1600 if ((env1.enclClass.sym.flags() & STATIC) != 0)
1601 staticOnly = true;
1602 env1 = env1.outer;
1603 }
1604 log.error(pos, "encl.class.required", member);
1605 return syms.errSymbol;
1606 }
1608 /**
1609 * Resolve an appropriate implicit this instance for t's container.
1610 * JLS2 8.8.5.1 and 15.9.2
1611 */
1612 Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t) {
1613 Type thisType = (((t.tsym.owner.kind & (MTH|VAR)) != 0)
1614 ? resolveSelf(pos, env, t.getEnclosingType().tsym, names._this)
1615 : resolveSelfContaining(pos, env, t.tsym)).type;
1616 if (env.info.isSelfCall && thisType.tsym == env.enclClass.sym)
1617 log.error(pos, "cant.ref.before.ctor.called", "this");
1618 return thisType;
1619 }
1621 /* ***************************************************************************
1622 * ResolveError classes, indicating error situations when accessing symbols
1623 ****************************************************************************/
1625 public void logAccessError(Env<AttrContext> env, JCTree tree, Type type) {
1626 AccessError error = new AccessError(env, type.getEnclosingType(), type.tsym);
1627 logResolveError(error, tree.pos(), type.getEnclosingType(), null, null, null);
1628 }
1629 //where
1630 private void logResolveError(ResolveError error,
1631 DiagnosticPosition pos,
1632 Type site,
1633 Name name,
1634 List<Type> argtypes,
1635 List<Type> typeargtypes) {
1636 JCDiagnostic d = error.getDiagnostic(JCDiagnostic.DiagnosticType.ERROR,
1637 pos, site, name, argtypes, typeargtypes);
1638 if (d != null)
1639 log.report(d);
1640 }
1642 private final LocalizedString noArgs = new LocalizedString("compiler.misc.no.args");
1644 public Object methodArguments(List<Type> argtypes) {
1645 return argtypes.isEmpty() ? noArgs : argtypes;
1646 }
1648 /**
1649 * Root class for resolution errors. Subclass of ResolveError
1650 * represent a different kinds of resolution error - as such they must
1651 * specify how they map into concrete compiler diagnostics.
1652 */
1653 private abstract class ResolveError extends Symbol {
1655 /** The name of the kind of error, for debugging only. */
1656 final String debugName;
1658 ResolveError(int kind, String debugName) {
1659 super(kind, 0, null, null, null);
1660 this.debugName = debugName;
1661 }
1663 @Override
1664 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
1665 throw new AssertionError();
1666 }
1668 @Override
1669 public String toString() {
1670 return debugName;
1671 }
1673 @Override
1674 public boolean exists() {
1675 return false;
1676 }
1678 /**
1679 * Create an external representation for this erroneous symbol to be
1680 * used during attribution - by default this returns the symbol of a
1681 * brand new error type which stores the original type found
1682 * during resolution.
1683 *
1684 * @param name the name used during resolution
1685 * @param location the location from which the symbol is accessed
1686 */
1687 protected Symbol access(Name name, TypeSymbol location) {
1688 return types.createErrorType(name, location, syms.errSymbol.type).tsym;
1689 }
1691 /**
1692 * Create a diagnostic representing this resolution error.
1693 *
1694 * @param dkind The kind of the diagnostic to be created (e.g error).
1695 * @param pos The position to be used for error reporting.
1696 * @param site The original type from where the selection took place.
1697 * @param name The name of the symbol to be resolved.
1698 * @param argtypes The invocation's value arguments,
1699 * if we looked for a method.
1700 * @param typeargtypes The invocation's type arguments,
1701 * if we looked for a method.
1702 */
1703 abstract JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
1704 DiagnosticPosition pos,
1705 Type site,
1706 Name name,
1707 List<Type> argtypes,
1708 List<Type> typeargtypes);
1710 /**
1711 * A name designates an operator if it consists
1712 * of a non-empty sequence of operator symbols +-~!/*%&|^<>=
1713 */
1714 boolean isOperator(Name name) {
1715 int i = 0;
1716 while (i < name.getByteLength() &&
1717 "+-~!*/%&|^<>=".indexOf(name.getByteAt(i)) >= 0) i++;
1718 return i > 0 && i == name.getByteLength();
1719 }
1720 }
1722 /**
1723 * This class is the root class of all resolution errors caused by
1724 * an invalid symbol being found during resolution.
1725 */
1726 abstract class InvalidSymbolError extends ResolveError {
1728 /** The invalid symbol found during resolution */
1729 Symbol sym;
1731 InvalidSymbolError(int kind, Symbol sym, String debugName) {
1732 super(kind, debugName);
1733 this.sym = sym;
1734 }
1736 @Override
1737 public boolean exists() {
1738 return true;
1739 }
1741 @Override
1742 public String toString() {
1743 return super.toString() + " wrongSym=" + sym;
1744 }
1746 @Override
1747 public Symbol access(Name name, TypeSymbol location) {
1748 if (sym.kind >= AMBIGUOUS)
1749 return ((ResolveError)sym).access(name, location);
1750 else if ((sym.kind & ERRONEOUS) == 0 && (sym.kind & TYP) != 0)
1751 return types.createErrorType(name, location, sym.type).tsym;
1752 else
1753 return sym;
1754 }
1755 }
1757 /**
1758 * InvalidSymbolError error class indicating that a symbol matching a
1759 * given name does not exists in a given site.
1760 */
1761 class SymbolNotFoundError extends ResolveError {
1763 SymbolNotFoundError(int kind) {
1764 super(kind, "symbol not found error");
1765 }
1767 @Override
1768 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
1769 DiagnosticPosition pos,
1770 Type site,
1771 Name name,
1772 List<Type> argtypes,
1773 List<Type> typeargtypes) {
1774 argtypes = argtypes == null ? List.<Type>nil() : argtypes;
1775 typeargtypes = typeargtypes == null ? List.<Type>nil() : typeargtypes;
1776 if (name == names.error)
1777 return null;
1779 if (isOperator(name)) {
1780 return diags.create(dkind, false, log.currentSource(), pos,
1781 "operator.cant.be.applied", name, argtypes);
1782 }
1783 boolean hasLocation = false;
1784 if (!site.tsym.name.isEmpty()) {
1785 if (site.tsym.kind == PCK && !site.tsym.exists()) {
1786 return diags.create(dkind, false, log.currentSource(), pos,
1787 "doesnt.exist", site.tsym);
1788 }
1789 hasLocation = true;
1790 }
1791 boolean isConstructor = kind == ABSENT_MTH &&
1792 name == names.table.names.init;
1793 KindName kindname = isConstructor ? KindName.CONSTRUCTOR : absentKind(kind);
1794 Name idname = isConstructor ? site.tsym.name : name;
1795 String errKey = getErrorKey(kindname, typeargtypes.nonEmpty(), hasLocation);
1796 if (hasLocation) {
1797 return diags.create(dkind, false, log.currentSource(), pos,
1798 errKey, kindname, idname, //symbol kindname, name
1799 typeargtypes, argtypes, //type parameters and arguments (if any)
1800 typeKindName(site), site); //location kindname, type
1801 }
1802 else {
1803 return diags.create(dkind, false, log.currentSource(), pos,
1804 errKey, kindname, idname, //symbol kindname, name
1805 typeargtypes, argtypes); //type parameters and arguments (if any)
1806 }
1807 }
1808 //where
1809 private String getErrorKey(KindName kindname, boolean hasTypeArgs, boolean hasLocation) {
1810 String key = "cant.resolve";
1811 String suffix = hasLocation ? ".location" : "";
1812 switch (kindname) {
1813 case METHOD:
1814 case CONSTRUCTOR: {
1815 suffix += ".args";
1816 suffix += hasTypeArgs ? ".params" : "";
1817 }
1818 }
1819 return key + suffix;
1820 }
1821 }
1823 /**
1824 * InvalidSymbolError error class indicating that a given symbol
1825 * (either a method, a constructor or an operand) is not applicable
1826 * given an actual arguments/type argument list.
1827 */
1828 class InapplicableSymbolError extends InvalidSymbolError {
1830 /** An auxiliary explanation set in case of instantiation errors. */
1831 JCDiagnostic explanation;
1833 InapplicableSymbolError(Symbol sym) {
1834 super(WRONG_MTH, sym, "inapplicable symbol error");
1835 }
1837 /** Update sym and explanation and return this.
1838 */
1839 InapplicableSymbolError setWrongSym(Symbol sym, JCDiagnostic explanation) {
1840 this.sym = sym;
1841 this.explanation = explanation;
1842 return this;
1843 }
1845 /** Update sym and return this.
1846 */
1847 InapplicableSymbolError setWrongSym(Symbol sym) {
1848 this.sym = sym;
1849 this.explanation = null;
1850 return this;
1851 }
1853 @Override
1854 public String toString() {
1855 return super.toString() + " explanation=" + explanation;
1856 }
1858 @Override
1859 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
1860 DiagnosticPosition pos,
1861 Type site,
1862 Name name,
1863 List<Type> argtypes,
1864 List<Type> typeargtypes) {
1865 if (name == names.error)
1866 return null;
1868 if (isOperator(name)) {
1869 return diags.create(dkind, false, log.currentSource(),
1870 pos, "operator.cant.be.applied", name, argtypes);
1871 }
1872 else {
1873 Symbol ws = sym.asMemberOf(site, types);
1874 return diags.create(dkind, false, log.currentSource(), pos,
1875 "cant.apply.symbol" + (explanation != null ? ".1" : ""),
1876 kindName(ws),
1877 ws.name == names.init ? ws.owner.name : ws.name,
1878 methodArguments(ws.type.getParameterTypes()),
1879 methodArguments(argtypes),
1880 kindName(ws.owner),
1881 ws.owner.type,
1882 explanation);
1883 }
1884 }
1886 @Override
1887 public Symbol access(Name name, TypeSymbol location) {
1888 return types.createErrorType(name, location, syms.errSymbol.type).tsym;
1889 }
1890 }
1892 /**
1893 * ResolveError error class indicating that a set of symbols
1894 * (either methods, constructors or operands) is not applicable
1895 * given an actual arguments/type argument list.
1896 */
1897 class InapplicableSymbolsError extends ResolveError {
1898 InapplicableSymbolsError(Symbol sym) {
1899 super(WRONG_MTHS, "inapplicable symbols");
1900 }
1902 @Override
1903 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
1904 DiagnosticPosition pos,
1905 Type site,
1906 Name name,
1907 List<Type> argtypes,
1908 List<Type> typeargtypes) {
1909 return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind, pos,
1910 site, name, argtypes, typeargtypes);
1911 }
1912 }
1914 /**
1915 * An InvalidSymbolError error class indicating that a symbol is not
1916 * accessible from a given site
1917 */
1918 class AccessError extends InvalidSymbolError {
1920 private Env<AttrContext> env;
1921 private Type site;
1923 AccessError(Symbol sym) {
1924 this(null, null, sym);
1925 }
1927 AccessError(Env<AttrContext> env, Type site, Symbol sym) {
1928 super(HIDDEN, sym, "access error");
1929 this.env = env;
1930 this.site = site;
1931 if (debugResolve)
1932 log.error("proc.messager", sym + " @ " + site + " is inaccessible.");
1933 }
1935 @Override
1936 public boolean exists() {
1937 return false;
1938 }
1940 @Override
1941 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
1942 DiagnosticPosition pos,
1943 Type site,
1944 Name name,
1945 List<Type> argtypes,
1946 List<Type> typeargtypes) {
1947 if (sym.owner.type.tag == ERROR)
1948 return null;
1950 if (sym.name == names.init && sym.owner != site.tsym) {
1951 return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind,
1952 pos, site, name, argtypes, typeargtypes);
1953 }
1954 else if ((sym.flags() & PUBLIC) != 0
1955 || (env != null && this.site != null
1956 && !isAccessible(env, this.site))) {
1957 return diags.create(dkind, false, log.currentSource(),
1958 pos, "not.def.access.class.intf.cant.access",
1959 sym, sym.location());
1960 }
1961 else if ((sym.flags() & (PRIVATE | PROTECTED)) != 0) {
1962 return diags.create(dkind, false, log.currentSource(),
1963 pos, "report.access", sym,
1964 asFlagSet(sym.flags() & (PRIVATE | PROTECTED)),
1965 sym.location());
1966 }
1967 else {
1968 return diags.create(dkind, false, log.currentSource(),
1969 pos, "not.def.public.cant.access", sym, sym.location());
1970 }
1971 }
1972 }
1974 /**
1975 * InvalidSymbolError error class indicating that an instance member
1976 * has erroneously been accessed from a static context.
1977 */
1978 class StaticError extends InvalidSymbolError {
1980 StaticError(Symbol sym) {
1981 super(STATICERR, sym, "static error");
1982 }
1984 @Override
1985 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
1986 DiagnosticPosition pos,
1987 Type site,
1988 Name name,
1989 List<Type> argtypes,
1990 List<Type> typeargtypes) {
1991 Symbol errSym = ((sym.kind == TYP && sym.type.tag == CLASS)
1992 ? types.erasure(sym.type).tsym
1993 : sym);
1994 return diags.create(dkind, false, log.currentSource(), pos,
1995 "non-static.cant.be.ref", kindName(sym), errSym);
1996 }
1997 }
1999 /**
2000 * InvalidSymbolError error class indicating that a pair of symbols
2001 * (either methods, constructors or operands) are ambiguous
2002 * given an actual arguments/type argument list.
2003 */
2004 class AmbiguityError extends InvalidSymbolError {
2006 /** The other maximally specific symbol */
2007 Symbol sym2;
2009 AmbiguityError(Symbol sym1, Symbol sym2) {
2010 super(AMBIGUOUS, sym1, "ambiguity error");
2011 this.sym2 = sym2;
2012 }
2014 @Override
2015 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
2016 DiagnosticPosition pos,
2017 Type site,
2018 Name name,
2019 List<Type> argtypes,
2020 List<Type> typeargtypes) {
2021 AmbiguityError pair = this;
2022 while (true) {
2023 if (pair.sym.kind == AMBIGUOUS)
2024 pair = (AmbiguityError)pair.sym;
2025 else if (pair.sym2.kind == AMBIGUOUS)
2026 pair = (AmbiguityError)pair.sym2;
2027 else break;
2028 }
2029 Name sname = pair.sym.name;
2030 if (sname == names.init) sname = pair.sym.owner.name;
2031 return diags.create(dkind, false, log.currentSource(),
2032 pos, "ref.ambiguous", sname,
2033 kindName(pair.sym),
2034 pair.sym,
2035 pair.sym.location(site, types),
2036 kindName(pair.sym2),
2037 pair.sym2,
2038 pair.sym2.location(site, types));
2039 }
2040 }
2042 enum MethodResolutionPhase {
2043 BASIC(false, false),
2044 BOX(true, false),
2045 VARARITY(true, true);
2047 boolean isBoxingRequired;
2048 boolean isVarargsRequired;
2050 MethodResolutionPhase(boolean isBoxingRequired, boolean isVarargsRequired) {
2051 this.isBoxingRequired = isBoxingRequired;
2052 this.isVarargsRequired = isVarargsRequired;
2053 }
2055 public boolean isBoxingRequired() {
2056 return isBoxingRequired;
2057 }
2059 public boolean isVarargsRequired() {
2060 return isVarargsRequired;
2061 }
2063 public boolean isApplicable(boolean boxingEnabled, boolean varargsEnabled) {
2064 return (varargsEnabled || !isVarargsRequired) &&
2065 (boxingEnabled || !isBoxingRequired);
2066 }
2067 }
2069 private Map<MethodResolutionPhase, Symbol> methodResolutionCache =
2070 new HashMap<MethodResolutionPhase, Symbol>(MethodResolutionPhase.values().length);
2072 final List<MethodResolutionPhase> methodResolutionSteps = List.of(BASIC, BOX, VARARITY);
2074 private MethodResolutionPhase firstErroneousResolutionPhase() {
2075 MethodResolutionPhase bestSoFar = BASIC;
2076 Symbol sym = methodNotFound;
2077 List<MethodResolutionPhase> steps = methodResolutionSteps;
2078 while (steps.nonEmpty() &&
2079 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
2080 sym.kind >= WRONG_MTHS) {
2081 sym = methodResolutionCache.get(steps.head);
2082 bestSoFar = steps.head;
2083 steps = steps.tail;
2084 }
2085 return bestSoFar;
2086 }
2087 }