Tue, 04 Mar 2008 15:45:20 +0000
6663588: Compiler goes into infinite loop for Cyclic Inheritance test case
Summary: interplay between cyclic inheritance and tvar bounds hangs javac
Reviewed-by: jjg
1 /*
2 * Copyright 1999-2006 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any 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.*;
34 import com.sun.tools.javac.code.Type.*;
35 import com.sun.tools.javac.code.Symbol.*;
36 import com.sun.tools.javac.tree.JCTree.*;
38 import static com.sun.tools.javac.code.Flags.*;
39 import static com.sun.tools.javac.code.Kinds.*;
40 import static com.sun.tools.javac.code.TypeTags.*;
41 import javax.lang.model.element.ElementVisitor;
43 /** Helper class for name resolution, used mostly by the attribution phase.
44 *
45 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
46 * you write code that depends on this, you do so at your own risk.
47 * This code and its internal interfaces are subject to change or
48 * deletion without notice.</b>
49 */
50 public class Resolve {
51 protected static final Context.Key<Resolve> resolveKey =
52 new Context.Key<Resolve>();
54 Name.Table names;
55 Log log;
56 Symtab syms;
57 Check chk;
58 Infer infer;
59 ClassReader reader;
60 TreeInfo treeinfo;
61 Types types;
62 public final boolean boxingEnabled; // = source.allowBoxing();
63 public final boolean varargsEnabled; // = source.allowVarargs();
64 private final boolean debugResolve;
66 public static Resolve instance(Context context) {
67 Resolve instance = context.get(resolveKey);
68 if (instance == null)
69 instance = new Resolve(context);
70 return instance;
71 }
73 protected Resolve(Context context) {
74 context.put(resolveKey, this);
75 syms = Symtab.instance(context);
77 varNotFound = new
78 ResolveError(ABSENT_VAR, syms.errSymbol, "variable not found");
79 wrongMethod = new
80 ResolveError(WRONG_MTH, syms.errSymbol, "method not found");
81 wrongMethods = new
82 ResolveError(WRONG_MTHS, syms.errSymbol, "wrong methods");
83 methodNotFound = new
84 ResolveError(ABSENT_MTH, syms.errSymbol, "method not found");
85 typeNotFound = new
86 ResolveError(ABSENT_TYP, syms.errSymbol, "type not found");
88 names = Name.Table.instance(context);
89 log = Log.instance(context);
90 chk = Check.instance(context);
91 infer = Infer.instance(context);
92 reader = ClassReader.instance(context);
93 treeinfo = TreeInfo.instance(context);
94 types = Types.instance(context);
95 Source source = Source.instance(context);
96 boxingEnabled = source.allowBoxing();
97 varargsEnabled = source.allowVarargs();
98 Options options = Options.instance(context);
99 debugResolve = options.get("debugresolve") != null;
100 }
102 /** error symbols, which are returned when resolution fails
103 */
104 final ResolveError varNotFound;
105 final ResolveError wrongMethod;
106 final ResolveError wrongMethods;
107 final ResolveError methodNotFound;
108 final ResolveError typeNotFound;
110 /* ************************************************************************
111 * Identifier resolution
112 *************************************************************************/
114 /** An environment is "static" if its static level is greater than
115 * the one of its outer environment
116 */
117 static boolean isStatic(Env<AttrContext> env) {
118 return env.info.staticLevel > env.outer.info.staticLevel;
119 }
121 /** An environment is an "initializer" if it is a constructor or
122 * an instance initializer.
123 */
124 static boolean isInitializer(Env<AttrContext> env) {
125 Symbol owner = env.info.scope.owner;
126 return owner.isConstructor() ||
127 owner.owner.kind == TYP &&
128 (owner.kind == VAR ||
129 owner.kind == MTH && (owner.flags() & BLOCK) != 0) &&
130 (owner.flags() & STATIC) == 0;
131 }
133 /** Is class accessible in given evironment?
134 * @param env The current environment.
135 * @param c The class whose accessibility is checked.
136 */
137 public boolean isAccessible(Env<AttrContext> env, TypeSymbol c) {
138 switch ((short)(c.flags() & AccessFlags)) {
139 case PRIVATE:
140 return
141 env.enclClass.sym.outermostClass() ==
142 c.owner.outermostClass();
143 case 0:
144 return
145 env.toplevel.packge == c.owner // fast special case
146 ||
147 env.toplevel.packge == c.packge()
148 ||
149 // Hack: this case is added since synthesized default constructors
150 // of anonymous classes should be allowed to access
151 // classes which would be inaccessible otherwise.
152 env.enclMethod != null &&
153 (env.enclMethod.mods.flags & ANONCONSTR) != 0;
154 default: // error recovery
155 case PUBLIC:
156 return true;
157 case PROTECTED:
158 return
159 env.toplevel.packge == c.owner // fast special case
160 ||
161 env.toplevel.packge == c.packge()
162 ||
163 isInnerSubClass(env.enclClass.sym, c.owner);
164 }
165 }
166 //where
167 /** Is given class a subclass of given base class, or an inner class
168 * of a subclass?
169 * Return null if no such class exists.
170 * @param c The class which is the subclass or is contained in it.
171 * @param base The base class
172 */
173 private boolean isInnerSubClass(ClassSymbol c, Symbol base) {
174 while (c != null && !c.isSubClass(base, types)) {
175 c = c.owner.enclClass();
176 }
177 return c != null;
178 }
180 boolean isAccessible(Env<AttrContext> env, Type t) {
181 return (t.tag == ARRAY)
182 ? isAccessible(env, types.elemtype(t))
183 : isAccessible(env, t.tsym);
184 }
186 /** Is symbol accessible as a member of given type in given evironment?
187 * @param env The current environment.
188 * @param site The type of which the tested symbol is regarded
189 * as a member.
190 * @param sym The symbol.
191 */
192 public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym) {
193 if (sym.name == names.init && sym.owner != site.tsym) return false;
194 ClassSymbol sub;
195 switch ((short)(sym.flags() & AccessFlags)) {
196 case PRIVATE:
197 return
198 (env.enclClass.sym == sym.owner // fast special case
199 ||
200 env.enclClass.sym.outermostClass() ==
201 sym.owner.outermostClass())
202 &&
203 sym.isInheritedIn(site.tsym, types);
204 case 0:
205 return
206 (env.toplevel.packge == sym.owner.owner // fast special case
207 ||
208 env.toplevel.packge == sym.packge())
209 &&
210 isAccessible(env, site)
211 &&
212 sym.isInheritedIn(site.tsym, types);
213 case PROTECTED:
214 return
215 (env.toplevel.packge == sym.owner.owner // fast special case
216 ||
217 env.toplevel.packge == sym.packge()
218 ||
219 isProtectedAccessible(sym, env.enclClass.sym, site)
220 ||
221 // OK to select instance method or field from 'super' or type name
222 // (but type names should be disallowed elsewhere!)
223 env.info.selectSuper && (sym.flags() & STATIC) == 0 && sym.kind != TYP)
224 &&
225 isAccessible(env, site)
226 &&
227 // `sym' is accessible only if not overridden by
228 // another symbol which is a member of `site'
229 // (because, if it is overridden, `sym' is not strictly
230 // speaking a member of `site'.)
231 (sym.kind != MTH || sym.isConstructor() ||
232 ((MethodSymbol)sym).implementation(site.tsym, types, true) == sym);
233 default: // this case includes erroneous combinations as well
234 return isAccessible(env, site);
235 }
236 }
237 //where
238 /** Is given protected symbol accessible if it is selected from given site
239 * and the selection takes place in given class?
240 * @param sym The symbol with protected access
241 * @param c The class where the access takes place
242 * @site The type of the qualifier
243 */
244 private
245 boolean isProtectedAccessible(Symbol sym, ClassSymbol c, Type site) {
246 while (c != null &&
247 !(c.isSubClass(sym.owner, types) &&
248 (c.flags() & INTERFACE) == 0 &&
249 // In JLS 2e 6.6.2.1, the subclass restriction applies
250 // only to instance fields and methods -- types are excluded
251 // regardless of whether they are declared 'static' or not.
252 ((sym.flags() & STATIC) != 0 || sym.kind == TYP || site.tsym.isSubClass(c, types))))
253 c = c.owner.enclClass();
254 return c != null;
255 }
257 /** Try to instantiate the type of a method so that it fits
258 * given type arguments and argument types. If succesful, return
259 * the method's instantiated type, else return null.
260 * The instantiation will take into account an additional leading
261 * formal parameter if the method is an instance method seen as a member
262 * of un underdetermined site In this case, we treat site as an additional
263 * parameter and the parameters of the class containing the method as
264 * additional type variables that get instantiated.
265 *
266 * @param env The current environment
267 * @param site The type of which the method is a member.
268 * @param m The method symbol.
269 * @param argtypes The invocation's given value arguments.
270 * @param typeargtypes The invocation's given type arguments.
271 * @param allowBoxing Allow boxing conversions of arguments.
272 * @param useVarargs Box trailing arguments into an array for varargs.
273 */
274 Type rawInstantiate(Env<AttrContext> env,
275 Type site,
276 Symbol m,
277 List<Type> argtypes,
278 List<Type> typeargtypes,
279 boolean allowBoxing,
280 boolean useVarargs,
281 Warner warn)
282 throws Infer.NoInstanceException {
283 if (useVarargs && (m.flags() & VARARGS) == 0) return null;
284 Type mt = types.memberType(site, m);
286 // tvars is the list of formal type variables for which type arguments
287 // need to inferred.
288 List<Type> tvars = env.info.tvars;
289 if (typeargtypes == null) typeargtypes = List.nil();
290 if (mt.tag != FORALL && typeargtypes.nonEmpty()) {
291 // This is not a polymorphic method, but typeargs are supplied
292 // which is fine, see JLS3 15.12.2.1
293 } else if (mt.tag == FORALL && typeargtypes.nonEmpty()) {
294 ForAll pmt = (ForAll) mt;
295 if (typeargtypes.length() != pmt.tvars.length())
296 return null;
297 // Check type arguments are within bounds
298 List<Type> formals = pmt.tvars;
299 List<Type> actuals = typeargtypes;
300 while (formals.nonEmpty() && actuals.nonEmpty()) {
301 List<Type> bounds = types.subst(types.getBounds((TypeVar)formals.head),
302 pmt.tvars, typeargtypes);
303 for (; bounds.nonEmpty(); bounds = bounds.tail)
304 if (!types.isSubtypeUnchecked(actuals.head, bounds.head, warn))
305 return null;
306 formals = formals.tail;
307 actuals = actuals.tail;
308 }
309 mt = types.subst(pmt.qtype, pmt.tvars, typeargtypes);
310 } else if (mt.tag == FORALL) {
311 ForAll pmt = (ForAll) mt;
312 List<Type> tvars1 = types.newInstances(pmt.tvars);
313 tvars = tvars.appendList(tvars1);
314 mt = types.subst(pmt.qtype, pmt.tvars, tvars1);
315 }
317 // find out whether we need to go the slow route via infer
318 boolean instNeeded = tvars.tail != null/*inlined: tvars.nonEmpty()*/;
319 for (List<Type> l = argtypes;
320 l.tail != null/*inlined: l.nonEmpty()*/ && !instNeeded;
321 l = l.tail) {
322 if (l.head.tag == FORALL) instNeeded = true;
323 }
325 if (instNeeded)
326 return
327 infer.instantiateMethod(tvars,
328 (MethodType)mt,
329 argtypes,
330 allowBoxing,
331 useVarargs,
332 warn);
333 return
334 argumentsAcceptable(argtypes, mt.getParameterTypes(),
335 allowBoxing, useVarargs, warn)
336 ? mt
337 : null;
338 }
340 /** Same but returns null instead throwing a NoInstanceException
341 */
342 Type instantiate(Env<AttrContext> env,
343 Type site,
344 Symbol m,
345 List<Type> argtypes,
346 List<Type> typeargtypes,
347 boolean allowBoxing,
348 boolean useVarargs,
349 Warner warn) {
350 try {
351 return rawInstantiate(env, site, m, argtypes, typeargtypes,
352 allowBoxing, useVarargs, warn);
353 } catch (Infer.NoInstanceException ex) {
354 return null;
355 }
356 }
358 /** Check if a parameter list accepts a list of args.
359 */
360 boolean argumentsAcceptable(List<Type> argtypes,
361 List<Type> formals,
362 boolean allowBoxing,
363 boolean useVarargs,
364 Warner warn) {
365 Type varargsFormal = useVarargs ? formals.last() : null;
366 while (argtypes.nonEmpty() && formals.head != varargsFormal) {
367 boolean works = allowBoxing
368 ? types.isConvertible(argtypes.head, formals.head, warn)
369 : types.isSubtypeUnchecked(argtypes.head, formals.head, warn);
370 if (!works) return false;
371 argtypes = argtypes.tail;
372 formals = formals.tail;
373 }
374 if (formals.head != varargsFormal) return false; // not enough args
375 if (!useVarargs)
376 return argtypes.isEmpty();
377 Type elt = types.elemtype(varargsFormal);
378 while (argtypes.nonEmpty()) {
379 if (!types.isConvertible(argtypes.head, elt, warn))
380 return false;
381 argtypes = argtypes.tail;
382 }
383 return true;
384 }
386 /* ***************************************************************************
387 * Symbol lookup
388 * the following naming conventions for arguments are used
389 *
390 * env is the environment where the symbol was mentioned
391 * site is the type of which the symbol is a member
392 * name is the symbol's name
393 * if no arguments are given
394 * argtypes are the value arguments, if we search for a method
395 *
396 * If no symbol was found, a ResolveError detailing the problem is returned.
397 ****************************************************************************/
399 /** Find field. Synthetic fields are always skipped.
400 * @param env The current environment.
401 * @param site The original type from where the selection takes place.
402 * @param name The name of the field.
403 * @param c The class to search for the field. This is always
404 * a superclass or implemented interface of site's class.
405 */
406 Symbol findField(Env<AttrContext> env,
407 Type site,
408 Name name,
409 TypeSymbol c) {
410 Symbol bestSoFar = varNotFound;
411 Symbol sym;
412 Scope.Entry e = c.members().lookup(name);
413 while (e.scope != null) {
414 if (e.sym.kind == VAR && (e.sym.flags_field & SYNTHETIC) == 0) {
415 return isAccessible(env, site, e.sym)
416 ? e.sym : new AccessError(env, site, e.sym);
417 }
418 e = e.next();
419 }
420 Type st = types.supertype(c.type);
421 if (st != null && st.tag == CLASS) {
422 sym = findField(env, site, name, st.tsym);
423 if (sym.kind < bestSoFar.kind) bestSoFar = sym;
424 }
425 for (List<Type> l = types.interfaces(c.type);
426 bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
427 l = l.tail) {
428 sym = findField(env, site, name, l.head.tsym);
429 if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS &&
430 sym.owner != bestSoFar.owner)
431 bestSoFar = new AmbiguityError(bestSoFar, sym);
432 else if (sym.kind < bestSoFar.kind)
433 bestSoFar = sym;
434 }
435 return bestSoFar;
436 }
438 /** Resolve a field identifier, throw a fatal error if not found.
439 * @param pos The position to use for error reporting.
440 * @param env The environment current at the method invocation.
441 * @param site The type of the qualifying expression, in which
442 * identifier is searched.
443 * @param name The identifier's name.
444 */
445 public VarSymbol resolveInternalField(DiagnosticPosition pos, Env<AttrContext> env,
446 Type site, Name name) {
447 Symbol sym = findField(env, site, name, site.tsym);
448 if (sym.kind == VAR) return (VarSymbol)sym;
449 else throw new FatalError(
450 JCDiagnostic.fragment("fatal.err.cant.locate.field",
451 name));
452 }
454 /** Find unqualified variable or field with given name.
455 * Synthetic fields always skipped.
456 * @param env The current environment.
457 * @param name The name of the variable or field.
458 */
459 Symbol findVar(Env<AttrContext> env, Name name) {
460 Symbol bestSoFar = varNotFound;
461 Symbol sym;
462 Env<AttrContext> env1 = env;
463 boolean staticOnly = false;
464 while (env1.outer != null) {
465 if (isStatic(env1)) staticOnly = true;
466 Scope.Entry e = env1.info.scope.lookup(name);
467 while (e.scope != null &&
468 (e.sym.kind != VAR ||
469 (e.sym.flags_field & SYNTHETIC) != 0))
470 e = e.next();
471 sym = (e.scope != null)
472 ? e.sym
473 : findField(
474 env1, env1.enclClass.sym.type, name, env1.enclClass.sym);
475 if (sym.exists()) {
476 if (staticOnly &&
477 sym.kind == VAR &&
478 sym.owner.kind == TYP &&
479 (sym.flags() & STATIC) == 0)
480 return new StaticError(sym);
481 else
482 return sym;
483 } else if (sym.kind < bestSoFar.kind) {
484 bestSoFar = sym;
485 }
487 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
488 env1 = env1.outer;
489 }
491 sym = findField(env, syms.predefClass.type, name, syms.predefClass);
492 if (sym.exists())
493 return sym;
494 if (bestSoFar.exists())
495 return bestSoFar;
497 Scope.Entry e = env.toplevel.namedImportScope.lookup(name);
498 for (; e.scope != null; e = e.next()) {
499 sym = e.sym;
500 Type origin = e.getOrigin().owner.type;
501 if (sym.kind == VAR) {
502 if (e.sym.owner.type != origin)
503 sym = sym.clone(e.getOrigin().owner);
504 return isAccessible(env, origin, sym)
505 ? sym : new AccessError(env, origin, sym);
506 }
507 }
509 Symbol origin = null;
510 e = env.toplevel.starImportScope.lookup(name);
511 for (; e.scope != null; e = e.next()) {
512 sym = e.sym;
513 if (sym.kind != VAR)
514 continue;
515 // invariant: sym.kind == VAR
516 if (bestSoFar.kind < AMBIGUOUS && sym.owner != bestSoFar.owner)
517 return new AmbiguityError(bestSoFar, sym);
518 else if (bestSoFar.kind >= VAR) {
519 origin = e.getOrigin().owner;
520 bestSoFar = isAccessible(env, origin.type, sym)
521 ? sym : new AccessError(env, origin.type, sym);
522 }
523 }
524 if (bestSoFar.kind == VAR && bestSoFar.owner.type != origin.type)
525 return bestSoFar.clone(origin);
526 else
527 return bestSoFar;
528 }
530 Warner noteWarner = new Warner();
532 /** Select the best method for a call site among two choices.
533 * @param env The current environment.
534 * @param site The original type from where the
535 * selection takes place.
536 * @param argtypes The invocation's value arguments,
537 * @param typeargtypes The invocation's type arguments,
538 * @param sym Proposed new best match.
539 * @param bestSoFar Previously found best match.
540 * @param allowBoxing Allow boxing conversions of arguments.
541 * @param useVarargs Box trailing arguments into an array for varargs.
542 */
543 Symbol selectBest(Env<AttrContext> env,
544 Type site,
545 List<Type> argtypes,
546 List<Type> typeargtypes,
547 Symbol sym,
548 Symbol bestSoFar,
549 boolean allowBoxing,
550 boolean useVarargs,
551 boolean operator) {
552 if (sym.kind == ERR) return bestSoFar;
553 if (!sym.isInheritedIn(site.tsym, types)) return bestSoFar;
554 assert sym.kind < AMBIGUOUS;
555 try {
556 if (rawInstantiate(env, site, sym, argtypes, typeargtypes,
557 allowBoxing, useVarargs, Warner.noWarnings) == null) {
558 // inapplicable
559 switch (bestSoFar.kind) {
560 case ABSENT_MTH: return wrongMethod.setWrongSym(sym);
561 case WRONG_MTH: return wrongMethods;
562 default: return bestSoFar;
563 }
564 }
565 } catch (Infer.NoInstanceException ex) {
566 switch (bestSoFar.kind) {
567 case ABSENT_MTH:
568 return wrongMethod.setWrongSym(sym, ex.getDiagnostic());
569 case WRONG_MTH:
570 return wrongMethods;
571 default:
572 return bestSoFar;
573 }
574 }
575 if (!isAccessible(env, site, sym)) {
576 return (bestSoFar.kind == ABSENT_MTH)
577 ? new AccessError(env, site, sym)
578 : bestSoFar;
579 }
580 return (bestSoFar.kind > AMBIGUOUS)
581 ? sym
582 : mostSpecific(sym, bestSoFar, env, site,
583 allowBoxing && operator, useVarargs);
584 }
586 /* Return the most specific of the two methods for a call,
587 * given that both are accessible and applicable.
588 * @param m1 A new candidate for most specific.
589 * @param m2 The previous most specific candidate.
590 * @param env The current environment.
591 * @param site The original type from where the selection
592 * takes place.
593 * @param allowBoxing Allow boxing conversions of arguments.
594 * @param useVarargs Box trailing arguments into an array for varargs.
595 */
596 Symbol mostSpecific(Symbol m1,
597 Symbol m2,
598 Env<AttrContext> env,
599 Type site,
600 boolean allowBoxing,
601 boolean useVarargs) {
602 switch (m2.kind) {
603 case MTH:
604 if (m1 == m2) return m1;
605 Type mt1 = types.memberType(site, m1);
606 noteWarner.unchecked = false;
607 boolean m1SignatureMoreSpecific =
608 (instantiate(env, site, m2, types.lowerBoundArgtypes(mt1), null,
609 allowBoxing, false, noteWarner) != null ||
610 useVarargs && instantiate(env, site, m2, types.lowerBoundArgtypes(mt1), null,
611 allowBoxing, true, noteWarner) != null) &&
612 !noteWarner.unchecked;
613 Type mt2 = types.memberType(site, m2);
614 noteWarner.unchecked = false;
615 boolean m2SignatureMoreSpecific =
616 (instantiate(env, site, m1, types.lowerBoundArgtypes(mt2), null,
617 allowBoxing, false, noteWarner) != null ||
618 useVarargs && instantiate(env, site, m1, types.lowerBoundArgtypes(mt2), null,
619 allowBoxing, true, noteWarner) != null) &&
620 !noteWarner.unchecked;
621 if (m1SignatureMoreSpecific && m2SignatureMoreSpecific) {
622 if (!types.overrideEquivalent(mt1, mt2))
623 return new AmbiguityError(m1, m2);
624 // same signature; select (a) the non-bridge method, or
625 // (b) the one that overrides the other, or (c) the concrete
626 // one, or (d) merge both abstract signatures
627 if ((m1.flags() & BRIDGE) != (m2.flags() & BRIDGE)) {
628 return ((m1.flags() & BRIDGE) != 0) ? m2 : m1;
629 }
630 // if one overrides or hides the other, use it
631 TypeSymbol m1Owner = (TypeSymbol)m1.owner;
632 TypeSymbol m2Owner = (TypeSymbol)m2.owner;
633 if (types.asSuper(m1Owner.type, m2Owner) != null &&
634 ((m1.owner.flags_field & INTERFACE) == 0 ||
635 (m2.owner.flags_field & INTERFACE) != 0) &&
636 m1.overrides(m2, m1Owner, types, false))
637 return m1;
638 if (types.asSuper(m2Owner.type, m1Owner) != null &&
639 ((m2.owner.flags_field & INTERFACE) == 0 ||
640 (m1.owner.flags_field & INTERFACE) != 0) &&
641 m2.overrides(m1, m2Owner, types, false))
642 return m2;
643 boolean m1Abstract = (m1.flags() & ABSTRACT) != 0;
644 boolean m2Abstract = (m2.flags() & ABSTRACT) != 0;
645 if (m1Abstract && !m2Abstract) return m2;
646 if (m2Abstract && !m1Abstract) return m1;
647 // both abstract or both concrete
648 if (!m1Abstract && !m2Abstract)
649 return new AmbiguityError(m1, m2);
650 // check for same erasure
651 if (!types.isSameType(m1.erasure(types), m2.erasure(types)))
652 return new AmbiguityError(m1, m2);
653 // both abstract, neither overridden; merge throws clause and result type
654 Symbol result;
655 Type result2 = mt2.getReturnType();;
656 if (mt2.tag == FORALL)
657 result2 = types.subst(result2, ((ForAll)mt2).tvars, ((ForAll)mt1).tvars);
658 if (types.isSubtype(mt1.getReturnType(), result2)) {
659 result = m1;
660 } else if (types.isSubtype(result2, mt1.getReturnType())) {
661 result = m2;
662 } else {
663 // Theoretically, this can't happen, but it is possible
664 // due to error recovery or mixing incompatible class files
665 return new AmbiguityError(m1, m2);
666 }
667 result = result.clone(result.owner);
668 result.type = (Type)result.type.clone();
669 result.type.setThrown(chk.intersect(mt1.getThrownTypes(),
670 mt2.getThrownTypes()));
671 return result;
672 }
673 if (m1SignatureMoreSpecific) return m1;
674 if (m2SignatureMoreSpecific) return m2;
675 return new AmbiguityError(m1, m2);
676 case AMBIGUOUS:
677 AmbiguityError e = (AmbiguityError)m2;
678 Symbol err1 = mostSpecific(m1, e.sym1, env, site, allowBoxing, useVarargs);
679 Symbol err2 = mostSpecific(m1, e.sym2, env, site, allowBoxing, useVarargs);
680 if (err1 == err2) return err1;
681 if (err1 == e.sym1 && err2 == e.sym2) return m2;
682 if (err1 instanceof AmbiguityError &&
683 err2 instanceof AmbiguityError &&
684 ((AmbiguityError)err1).sym1 == ((AmbiguityError)err2).sym1)
685 return new AmbiguityError(m1, m2);
686 else
687 return new AmbiguityError(err1, err2);
688 default:
689 throw new AssertionError();
690 }
691 }
693 /** Find best qualified method matching given name, type and value
694 * arguments.
695 * @param env The current environment.
696 * @param site The original type from where the selection
697 * takes place.
698 * @param name The method's name.
699 * @param argtypes The method's value arguments.
700 * @param typeargtypes The method's type arguments
701 * @param allowBoxing Allow boxing conversions of arguments.
702 * @param useVarargs Box trailing arguments into an array for varargs.
703 */
704 Symbol findMethod(Env<AttrContext> env,
705 Type site,
706 Name name,
707 List<Type> argtypes,
708 List<Type> typeargtypes,
709 boolean allowBoxing,
710 boolean useVarargs,
711 boolean operator) {
712 return findMethod(env,
713 site,
714 name,
715 argtypes,
716 typeargtypes,
717 site.tsym.type,
718 true,
719 methodNotFound,
720 allowBoxing,
721 useVarargs,
722 operator);
723 }
724 // where
725 private Symbol findMethod(Env<AttrContext> env,
726 Type site,
727 Name name,
728 List<Type> argtypes,
729 List<Type> typeargtypes,
730 Type intype,
731 boolean abstractok,
732 Symbol bestSoFar,
733 boolean allowBoxing,
734 boolean useVarargs,
735 boolean operator) {
736 for (Type ct = intype; ct.tag == CLASS; ct = types.supertype(ct)) {
737 ClassSymbol c = (ClassSymbol)ct.tsym;
738 if ((c.flags() & (ABSTRACT | INTERFACE)) == 0)
739 abstractok = false;
740 for (Scope.Entry e = c.members().lookup(name);
741 e.scope != null;
742 e = e.next()) {
743 //- System.out.println(" e " + e.sym);
744 if (e.sym.kind == MTH &&
745 (e.sym.flags_field & SYNTHETIC) == 0) {
746 bestSoFar = selectBest(env, site, argtypes, typeargtypes,
747 e.sym, bestSoFar,
748 allowBoxing,
749 useVarargs,
750 operator);
751 }
752 }
753 //- System.out.println(" - " + bestSoFar);
754 if (abstractok) {
755 Symbol concrete = methodNotFound;
756 if ((bestSoFar.flags() & ABSTRACT) == 0)
757 concrete = bestSoFar;
758 for (List<Type> l = types.interfaces(c.type);
759 l.nonEmpty();
760 l = l.tail) {
761 bestSoFar = findMethod(env, site, name, argtypes,
762 typeargtypes,
763 l.head, abstractok, bestSoFar,
764 allowBoxing, useVarargs, operator);
765 }
766 if (concrete != bestSoFar &&
767 concrete.kind < ERR && bestSoFar.kind < ERR &&
768 types.isSubSignature(concrete.type, bestSoFar.type))
769 bestSoFar = concrete;
770 }
771 }
772 return bestSoFar;
773 }
775 /** Find unqualified method matching given name, type and value arguments.
776 * @param env The current environment.
777 * @param name The method's name.
778 * @param argtypes The method's value arguments.
779 * @param typeargtypes The method's type arguments.
780 * @param allowBoxing Allow boxing conversions of arguments.
781 * @param useVarargs Box trailing arguments into an array for varargs.
782 */
783 Symbol findFun(Env<AttrContext> env, Name name,
784 List<Type> argtypes, List<Type> typeargtypes,
785 boolean allowBoxing, boolean useVarargs) {
786 Symbol bestSoFar = methodNotFound;
787 Symbol sym;
788 Env<AttrContext> env1 = env;
789 boolean staticOnly = false;
790 while (env1.outer != null) {
791 if (isStatic(env1)) staticOnly = true;
792 sym = findMethod(
793 env1, env1.enclClass.sym.type, name, argtypes, typeargtypes,
794 allowBoxing, useVarargs, false);
795 if (sym.exists()) {
796 if (staticOnly &&
797 sym.kind == MTH &&
798 sym.owner.kind == TYP &&
799 (sym.flags() & STATIC) == 0) return new StaticError(sym);
800 else return sym;
801 } else if (sym.kind < bestSoFar.kind) {
802 bestSoFar = sym;
803 }
804 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
805 env1 = env1.outer;
806 }
808 sym = findMethod(env, syms.predefClass.type, name, argtypes,
809 typeargtypes, allowBoxing, useVarargs, false);
810 if (sym.exists())
811 return sym;
813 Scope.Entry e = env.toplevel.namedImportScope.lookup(name);
814 for (; e.scope != null; e = e.next()) {
815 sym = e.sym;
816 Type origin = e.getOrigin().owner.type;
817 if (sym.kind == MTH) {
818 if (e.sym.owner.type != origin)
819 sym = sym.clone(e.getOrigin().owner);
820 if (!isAccessible(env, origin, sym))
821 sym = new AccessError(env, origin, sym);
822 bestSoFar = selectBest(env, origin,
823 argtypes, typeargtypes,
824 sym, bestSoFar,
825 allowBoxing, useVarargs, false);
826 }
827 }
828 if (bestSoFar.exists())
829 return bestSoFar;
831 e = env.toplevel.starImportScope.lookup(name);
832 for (; e.scope != null; e = e.next()) {
833 sym = e.sym;
834 Type origin = e.getOrigin().owner.type;
835 if (sym.kind == MTH) {
836 if (e.sym.owner.type != origin)
837 sym = sym.clone(e.getOrigin().owner);
838 if (!isAccessible(env, origin, sym))
839 sym = new AccessError(env, origin, sym);
840 bestSoFar = selectBest(env, origin,
841 argtypes, typeargtypes,
842 sym, bestSoFar,
843 allowBoxing, useVarargs, false);
844 }
845 }
846 return bestSoFar;
847 }
849 /** Load toplevel or member class with given fully qualified name and
850 * verify that it is accessible.
851 * @param env The current environment.
852 * @param name The fully qualified name of the class to be loaded.
853 */
854 Symbol loadClass(Env<AttrContext> env, Name name) {
855 try {
856 ClassSymbol c = reader.loadClass(name);
857 return isAccessible(env, c) ? c : new AccessError(c);
858 } catch (ClassReader.BadClassFile err) {
859 throw err;
860 } catch (CompletionFailure ex) {
861 return typeNotFound;
862 }
863 }
865 /** Find qualified member type.
866 * @param env The current environment.
867 * @param site The original type from where the selection takes
868 * place.
869 * @param name The type's name.
870 * @param c The class to search for the member type. This is
871 * always a superclass or implemented interface of
872 * site's class.
873 */
874 Symbol findMemberType(Env<AttrContext> env,
875 Type site,
876 Name name,
877 TypeSymbol c) {
878 Symbol bestSoFar = typeNotFound;
879 Symbol sym;
880 Scope.Entry e = c.members().lookup(name);
881 while (e.scope != null) {
882 if (e.sym.kind == TYP) {
883 return isAccessible(env, site, e.sym)
884 ? e.sym
885 : new AccessError(env, site, e.sym);
886 }
887 e = e.next();
888 }
889 Type st = types.supertype(c.type);
890 if (st != null && st.tag == CLASS) {
891 sym = findMemberType(env, site, name, st.tsym);
892 if (sym.kind < bestSoFar.kind) bestSoFar = sym;
893 }
894 for (List<Type> l = types.interfaces(c.type);
895 bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
896 l = l.tail) {
897 sym = findMemberType(env, site, name, l.head.tsym);
898 if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS &&
899 sym.owner != bestSoFar.owner)
900 bestSoFar = new AmbiguityError(bestSoFar, sym);
901 else if (sym.kind < bestSoFar.kind)
902 bestSoFar = sym;
903 }
904 return bestSoFar;
905 }
907 /** Find a global type in given scope and load corresponding class.
908 * @param env The current environment.
909 * @param scope The scope in which to look for the type.
910 * @param name The type's name.
911 */
912 Symbol findGlobalType(Env<AttrContext> env, Scope scope, Name name) {
913 Symbol bestSoFar = typeNotFound;
914 for (Scope.Entry e = scope.lookup(name); e.scope != null; e = e.next()) {
915 Symbol sym = loadClass(env, e.sym.flatName());
916 if (bestSoFar.kind == TYP && sym.kind == TYP &&
917 bestSoFar != sym)
918 return new AmbiguityError(bestSoFar, sym);
919 else if (sym.kind < bestSoFar.kind)
920 bestSoFar = sym;
921 }
922 return bestSoFar;
923 }
925 /** Find an unqualified type symbol.
926 * @param env The current environment.
927 * @param name The type's name.
928 */
929 Symbol findType(Env<AttrContext> env, Name name) {
930 Symbol bestSoFar = typeNotFound;
931 Symbol sym;
932 boolean staticOnly = false;
933 for (Env<AttrContext> env1 = env; env1.outer != null; env1 = env1.outer) {
934 if (isStatic(env1)) staticOnly = true;
935 for (Scope.Entry e = env1.info.scope.lookup(name);
936 e.scope != null;
937 e = e.next()) {
938 if (e.sym.kind == TYP) {
939 if (staticOnly &&
940 e.sym.type.tag == TYPEVAR &&
941 e.sym.owner.kind == TYP) return new StaticError(e.sym);
942 return e.sym;
943 }
944 }
946 sym = findMemberType(env1, env1.enclClass.sym.type, name,
947 env1.enclClass.sym);
948 if (staticOnly && sym.kind == TYP &&
949 sym.type.tag == CLASS &&
950 sym.type.getEnclosingType().tag == CLASS &&
951 env1.enclClass.sym.type.isParameterized() &&
952 sym.type.getEnclosingType().isParameterized())
953 return new StaticError(sym);
954 else if (sym.exists()) return sym;
955 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
957 JCClassDecl encl = env1.baseClause ? (JCClassDecl)env1.tree : env1.enclClass;
958 if ((encl.sym.flags() & STATIC) != 0)
959 staticOnly = true;
960 }
962 if (env.tree.getTag() != JCTree.IMPORT) {
963 sym = findGlobalType(env, env.toplevel.namedImportScope, name);
964 if (sym.exists()) return sym;
965 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
967 sym = findGlobalType(env, env.toplevel.packge.members(), name);
968 if (sym.exists()) return sym;
969 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
971 sym = findGlobalType(env, env.toplevel.starImportScope, name);
972 if (sym.exists()) return sym;
973 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
974 }
976 return bestSoFar;
977 }
979 /** Find an unqualified identifier which matches a specified kind set.
980 * @param env The current environment.
981 * @param name The indentifier's name.
982 * @param kind Indicates the possible symbol kinds
983 * (a subset of VAL, TYP, PCK).
984 */
985 Symbol findIdent(Env<AttrContext> env, Name name, int kind) {
986 Symbol bestSoFar = typeNotFound;
987 Symbol sym;
989 if ((kind & VAR) != 0) {
990 sym = findVar(env, name);
991 if (sym.exists()) return sym;
992 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
993 }
995 if ((kind & TYP) != 0) {
996 sym = findType(env, name);
997 if (sym.exists()) return sym;
998 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
999 }
1001 if ((kind & PCK) != 0) return reader.enterPackage(name);
1002 else return bestSoFar;
1003 }
1005 /** Find an identifier in a package which matches a specified kind set.
1006 * @param env The current environment.
1007 * @param name The identifier's name.
1008 * @param kind Indicates the possible symbol kinds
1009 * (a nonempty subset of TYP, PCK).
1010 */
1011 Symbol findIdentInPackage(Env<AttrContext> env, TypeSymbol pck,
1012 Name name, int kind) {
1013 Name fullname = TypeSymbol.formFullName(name, pck);
1014 Symbol bestSoFar = typeNotFound;
1015 PackageSymbol pack = null;
1016 if ((kind & PCK) != 0) {
1017 pack = reader.enterPackage(fullname);
1018 if (pack.exists()) return pack;
1019 }
1020 if ((kind & TYP) != 0) {
1021 Symbol sym = loadClass(env, fullname);
1022 if (sym.exists()) {
1023 // don't allow programs to use flatnames
1024 if (name == sym.name) return sym;
1025 }
1026 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
1027 }
1028 return (pack != null) ? pack : bestSoFar;
1029 }
1031 /** Find an identifier among the members of a given type `site'.
1032 * @param env The current environment.
1033 * @param site The type containing the symbol to be found.
1034 * @param name The identifier's name.
1035 * @param kind Indicates the possible symbol kinds
1036 * (a subset of VAL, TYP).
1037 */
1038 Symbol findIdentInType(Env<AttrContext> env, Type site,
1039 Name name, int kind) {
1040 Symbol bestSoFar = typeNotFound;
1041 Symbol sym;
1042 if ((kind & VAR) != 0) {
1043 sym = findField(env, site, name, site.tsym);
1044 if (sym.exists()) return sym;
1045 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
1046 }
1048 if ((kind & TYP) != 0) {
1049 sym = findMemberType(env, site, name, site.tsym);
1050 if (sym.exists()) return sym;
1051 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
1052 }
1053 return bestSoFar;
1054 }
1056 /* ***************************************************************************
1057 * Access checking
1058 * The following methods convert ResolveErrors to ErrorSymbols, issuing
1059 * an error message in the process
1060 ****************************************************************************/
1062 /** If `sym' is a bad symbol: report error and return errSymbol
1063 * else pass through unchanged,
1064 * additional arguments duplicate what has been used in trying to find the
1065 * symbol (--> flyweight pattern). This improves performance since we
1066 * expect misses to happen frequently.
1067 *
1068 * @param sym The symbol that was found, or a ResolveError.
1069 * @param pos The position to use for error reporting.
1070 * @param site The original type from where the selection took place.
1071 * @param name The symbol's name.
1072 * @param argtypes The invocation's value arguments,
1073 * if we looked for a method.
1074 * @param typeargtypes The invocation's type arguments,
1075 * if we looked for a method.
1076 */
1077 Symbol access(Symbol sym,
1078 DiagnosticPosition pos,
1079 Type site,
1080 Name name,
1081 boolean qualified,
1082 List<Type> argtypes,
1083 List<Type> typeargtypes) {
1084 if (sym.kind >= AMBIGUOUS) {
1085 // printscopes(site.tsym.members());//DEBUG
1086 if (!site.isErroneous() &&
1087 !Type.isErroneous(argtypes) &&
1088 (typeargtypes==null || !Type.isErroneous(typeargtypes)))
1089 ((ResolveError)sym).report(log, pos, site, name, argtypes, typeargtypes);
1090 do {
1091 sym = ((ResolveError)sym).sym;
1092 } while (sym.kind >= AMBIGUOUS);
1093 if (sym == syms.errSymbol // preserve the symbol name through errors
1094 || ((sym.kind & ERRONEOUS) == 0 // make sure an error symbol is returned
1095 && (sym.kind & TYP) != 0))
1096 sym = new ErrorType(name, qualified?site.tsym:syms.noSymbol).tsym;
1097 }
1098 return sym;
1099 }
1101 /** Same as above, but without type arguments and arguments.
1102 */
1103 Symbol access(Symbol sym,
1104 DiagnosticPosition pos,
1105 Type site,
1106 Name name,
1107 boolean qualified) {
1108 if (sym.kind >= AMBIGUOUS)
1109 return access(sym, pos, site, name, qualified, List.<Type>nil(), null);
1110 else
1111 return sym;
1112 }
1114 /** Check that sym is not an abstract method.
1115 */
1116 void checkNonAbstract(DiagnosticPosition pos, Symbol sym) {
1117 if ((sym.flags() & ABSTRACT) != 0)
1118 log.error(pos, "abstract.cant.be.accessed.directly",
1119 kindName(sym), sym, sym.location());
1120 }
1122 /* ***************************************************************************
1123 * Debugging
1124 ****************************************************************************/
1126 /** print all scopes starting with scope s and proceeding outwards.
1127 * used for debugging.
1128 */
1129 public void printscopes(Scope s) {
1130 while (s != null) {
1131 if (s.owner != null)
1132 System.err.print(s.owner + ": ");
1133 for (Scope.Entry e = s.elems; e != null; e = e.sibling) {
1134 if ((e.sym.flags() & ABSTRACT) != 0)
1135 System.err.print("abstract ");
1136 System.err.print(e.sym + " ");
1137 }
1138 System.err.println();
1139 s = s.next;
1140 }
1141 }
1143 void printscopes(Env<AttrContext> env) {
1144 while (env.outer != null) {
1145 System.err.println("------------------------------");
1146 printscopes(env.info.scope);
1147 env = env.outer;
1148 }
1149 }
1151 public void printscopes(Type t) {
1152 while (t.tag == CLASS) {
1153 printscopes(t.tsym.members());
1154 t = types.supertype(t);
1155 }
1156 }
1158 /* ***************************************************************************
1159 * Name resolution
1160 * Naming conventions are as for symbol lookup
1161 * Unlike the find... methods these methods will report access errors
1162 ****************************************************************************/
1164 /** Resolve an unqualified (non-method) identifier.
1165 * @param pos The position to use for error reporting.
1166 * @param env The environment current at the identifier use.
1167 * @param name The identifier's name.
1168 * @param kind The set of admissible symbol kinds for the identifier.
1169 */
1170 Symbol resolveIdent(DiagnosticPosition pos, Env<AttrContext> env,
1171 Name name, int kind) {
1172 return access(
1173 findIdent(env, name, kind),
1174 pos, env.enclClass.sym.type, name, false);
1175 }
1177 /** Resolve an unqualified method identifier.
1178 * @param pos The position to use for error reporting.
1179 * @param env The environment current at the method invocation.
1180 * @param name The identifier's name.
1181 * @param argtypes The types of the invocation's value arguments.
1182 * @param typeargtypes The types of the invocation's type arguments.
1183 */
1184 Symbol resolveMethod(DiagnosticPosition pos,
1185 Env<AttrContext> env,
1186 Name name,
1187 List<Type> argtypes,
1188 List<Type> typeargtypes) {
1189 Symbol sym = findFun(env, name, argtypes, typeargtypes, false, env.info.varArgs=false);
1190 if (varargsEnabled && sym.kind >= WRONG_MTHS) {
1191 sym = findFun(env, name, argtypes, typeargtypes, true, false);
1192 if (sym.kind >= WRONG_MTHS)
1193 sym = findFun(env, name, argtypes, typeargtypes, true, env.info.varArgs=true);
1194 }
1195 if (sym.kind >= AMBIGUOUS) {
1196 sym = access(
1197 sym, pos, env.enclClass.sym.type, name, false, argtypes, typeargtypes);
1198 }
1199 return sym;
1200 }
1202 /** Resolve a qualified method identifier
1203 * @param pos The position to use for error reporting.
1204 * @param env The environment current at the method invocation.
1205 * @param site The type of the qualifying expression, in which
1206 * identifier is searched.
1207 * @param name The identifier's name.
1208 * @param argtypes The types of the invocation's value arguments.
1209 * @param typeargtypes The types of the invocation's type arguments.
1210 */
1211 Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env,
1212 Type site, Name name, List<Type> argtypes,
1213 List<Type> typeargtypes) {
1214 Symbol sym = findMethod(env, site, name, argtypes, typeargtypes, false,
1215 env.info.varArgs=false, false);
1216 if (varargsEnabled && sym.kind >= WRONG_MTHS) {
1217 sym = findMethod(env, site, name, argtypes, typeargtypes, true,
1218 false, false);
1219 if (sym.kind >= WRONG_MTHS)
1220 sym = findMethod(env, site, name, argtypes, typeargtypes, true,
1221 env.info.varArgs=true, false);
1222 }
1223 if (sym.kind >= AMBIGUOUS) {
1224 sym = access(sym, pos, site, name, true, argtypes, typeargtypes);
1225 }
1226 return sym;
1227 }
1229 /** Resolve a qualified method identifier, throw a fatal error if not
1230 * found.
1231 * @param pos The position to use for error reporting.
1232 * @param env The environment current at the method invocation.
1233 * @param site The type of the qualifying expression, in which
1234 * identifier is searched.
1235 * @param name The identifier's name.
1236 * @param argtypes The types of the invocation's value arguments.
1237 * @param typeargtypes The types of the invocation's type arguments.
1238 */
1239 public MethodSymbol resolveInternalMethod(DiagnosticPosition pos, Env<AttrContext> env,
1240 Type site, Name name,
1241 List<Type> argtypes,
1242 List<Type> typeargtypes) {
1243 Symbol sym = resolveQualifiedMethod(
1244 pos, env, site, name, argtypes, typeargtypes);
1245 if (sym.kind == MTH) return (MethodSymbol)sym;
1246 else throw new FatalError(
1247 JCDiagnostic.fragment("fatal.err.cant.locate.meth",
1248 name));
1249 }
1251 /** Resolve constructor.
1252 * @param pos The position to use for error reporting.
1253 * @param env The environment current at the constructor invocation.
1254 * @param site The type of class for which a constructor is searched.
1255 * @param argtypes The types of the constructor invocation's value
1256 * arguments.
1257 * @param typeargtypes The types of the constructor invocation's type
1258 * arguments.
1259 */
1260 Symbol resolveConstructor(DiagnosticPosition pos,
1261 Env<AttrContext> env,
1262 Type site,
1263 List<Type> argtypes,
1264 List<Type> typeargtypes) {
1265 Symbol sym = resolveConstructor(pos, env, site, argtypes, typeargtypes, false, env.info.varArgs=false);
1266 if (varargsEnabled && sym.kind >= WRONG_MTHS) {
1267 sym = resolveConstructor(pos, env, site, argtypes, typeargtypes, true, false);
1268 if (sym.kind >= WRONG_MTHS)
1269 sym = resolveConstructor(pos, env, site, argtypes, typeargtypes, true, env.info.varArgs=true);
1270 }
1271 if (sym.kind >= AMBIGUOUS) {
1272 sym = access(sym, pos, site, names.init, true, argtypes, typeargtypes);
1273 }
1274 return sym;
1275 }
1277 /** Resolve constructor.
1278 * @param pos The position to use for error reporting.
1279 * @param env The environment current at the constructor invocation.
1280 * @param site The type of class for which a constructor is searched.
1281 * @param argtypes The types of the constructor invocation's value
1282 * arguments.
1283 * @param typeargtypes The types of the constructor invocation's type
1284 * arguments.
1285 * @param allowBoxing Allow boxing and varargs conversions.
1286 * @param useVarargs Box trailing arguments into an array for varargs.
1287 */
1288 Symbol resolveConstructor(DiagnosticPosition pos, Env<AttrContext> env,
1289 Type site, List<Type> argtypes,
1290 List<Type> typeargtypes,
1291 boolean allowBoxing,
1292 boolean useVarargs) {
1293 Symbol sym = findMethod(env, site,
1294 names.init, argtypes,
1295 typeargtypes, allowBoxing,
1296 useVarargs, false);
1297 if ((sym.flags() & DEPRECATED) != 0 &&
1298 (env.info.scope.owner.flags() & DEPRECATED) == 0 &&
1299 env.info.scope.owner.outermostClass() != sym.outermostClass())
1300 chk.warnDeprecated(pos, sym);
1301 return sym;
1302 }
1304 /** Resolve a constructor, throw a fatal error if not found.
1305 * @param pos The position to use for error reporting.
1306 * @param env The environment current at the method invocation.
1307 * @param site The type to be constructed.
1308 * @param argtypes The types of the invocation's value arguments.
1309 * @param typeargtypes The types of the invocation's type arguments.
1310 */
1311 public MethodSymbol resolveInternalConstructor(DiagnosticPosition pos, Env<AttrContext> env,
1312 Type site,
1313 List<Type> argtypes,
1314 List<Type> typeargtypes) {
1315 Symbol sym = resolveConstructor(
1316 pos, env, site, argtypes, typeargtypes);
1317 if (sym.kind == MTH) return (MethodSymbol)sym;
1318 else throw new FatalError(
1319 JCDiagnostic.fragment("fatal.err.cant.locate.ctor", site));
1320 }
1322 /** Resolve operator.
1323 * @param pos The position to use for error reporting.
1324 * @param optag The tag of the operation tree.
1325 * @param env The environment current at the operation.
1326 * @param argtypes The types of the operands.
1327 */
1328 Symbol resolveOperator(DiagnosticPosition pos, int optag,
1329 Env<AttrContext> env, List<Type> argtypes) {
1330 Name name = treeinfo.operatorName(optag);
1331 Symbol sym = findMethod(env, syms.predefClass.type, name, argtypes,
1332 null, false, false, true);
1333 if (boxingEnabled && sym.kind >= WRONG_MTHS)
1334 sym = findMethod(env, syms.predefClass.type, name, argtypes,
1335 null, true, false, true);
1336 return access(sym, pos, env.enclClass.sym.type, name,
1337 false, argtypes, null);
1338 }
1340 /** Resolve operator.
1341 * @param pos The position to use for error reporting.
1342 * @param optag The tag of the operation tree.
1343 * @param env The environment current at the operation.
1344 * @param arg The type of the operand.
1345 */
1346 Symbol resolveUnaryOperator(DiagnosticPosition pos, int optag, Env<AttrContext> env, Type arg) {
1347 return resolveOperator(pos, optag, env, List.of(arg));
1348 }
1350 /** Resolve binary operator.
1351 * @param pos The position to use for error reporting.
1352 * @param optag The tag of the operation tree.
1353 * @param env The environment current at the operation.
1354 * @param left The types of the left operand.
1355 * @param right The types of the right operand.
1356 */
1357 Symbol resolveBinaryOperator(DiagnosticPosition pos,
1358 int optag,
1359 Env<AttrContext> env,
1360 Type left,
1361 Type right) {
1362 return resolveOperator(pos, optag, env, List.of(left, right));
1363 }
1365 /**
1366 * Resolve `c.name' where name == this or name == super.
1367 * @param pos The position to use for error reporting.
1368 * @param env The environment current at the expression.
1369 * @param c The qualifier.
1370 * @param name The identifier's name.
1371 */
1372 Symbol resolveSelf(DiagnosticPosition pos,
1373 Env<AttrContext> env,
1374 TypeSymbol c,
1375 Name name) {
1376 Env<AttrContext> env1 = env;
1377 boolean staticOnly = false;
1378 while (env1.outer != null) {
1379 if (isStatic(env1)) staticOnly = true;
1380 if (env1.enclClass.sym == c) {
1381 Symbol sym = env1.info.scope.lookup(name).sym;
1382 if (sym != null) {
1383 if (staticOnly) sym = new StaticError(sym);
1384 return access(sym, pos, env.enclClass.sym.type,
1385 name, true);
1386 }
1387 }
1388 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
1389 env1 = env1.outer;
1390 }
1391 log.error(pos, "not.encl.class", c);
1392 return syms.errSymbol;
1393 }
1395 /**
1396 * Resolve `c.this' for an enclosing class c that contains the
1397 * named member.
1398 * @param pos The position to use for error reporting.
1399 * @param env The environment current at the expression.
1400 * @param member The member that must be contained in the result.
1401 */
1402 Symbol resolveSelfContaining(DiagnosticPosition pos,
1403 Env<AttrContext> env,
1404 Symbol member) {
1405 Name name = names._this;
1406 Env<AttrContext> env1 = env;
1407 boolean staticOnly = false;
1408 while (env1.outer != null) {
1409 if (isStatic(env1)) staticOnly = true;
1410 if (env1.enclClass.sym.isSubClass(member.owner, types) &&
1411 isAccessible(env, env1.enclClass.sym.type, member)) {
1412 Symbol sym = env1.info.scope.lookup(name).sym;
1413 if (sym != null) {
1414 if (staticOnly) sym = new StaticError(sym);
1415 return access(sym, pos, env.enclClass.sym.type,
1416 name, true);
1417 }
1418 }
1419 if ((env1.enclClass.sym.flags() & STATIC) != 0)
1420 staticOnly = true;
1421 env1 = env1.outer;
1422 }
1423 log.error(pos, "encl.class.required", member);
1424 return syms.errSymbol;
1425 }
1427 /**
1428 * Resolve an appropriate implicit this instance for t's container.
1429 * JLS2 8.8.5.1 and 15.9.2
1430 */
1431 Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t) {
1432 Type thisType = (((t.tsym.owner.kind & (MTH|VAR)) != 0)
1433 ? resolveSelf(pos, env, t.getEnclosingType().tsym, names._this)
1434 : resolveSelfContaining(pos, env, t.tsym)).type;
1435 if (env.info.isSelfCall && thisType.tsym == env.enclClass.sym)
1436 log.error(pos, "cant.ref.before.ctor.called", "this");
1437 return thisType;
1438 }
1440 /* ***************************************************************************
1441 * Methods related to kinds
1442 ****************************************************************************/
1444 /** A localized string describing a given kind.
1445 */
1446 static JCDiagnostic kindName(int kind) {
1447 switch (kind) {
1448 case PCK: return JCDiagnostic.fragment("kindname.package");
1449 case TYP: return JCDiagnostic.fragment("kindname.class");
1450 case VAR: return JCDiagnostic.fragment("kindname.variable");
1451 case VAL: return JCDiagnostic.fragment("kindname.value");
1452 case MTH: return JCDiagnostic.fragment("kindname.method");
1453 default : return JCDiagnostic.fragment("kindname",
1454 Integer.toString(kind)); //debug
1455 }
1456 }
1458 static JCDiagnostic kindName(Symbol sym) {
1459 switch (sym.getKind()) {
1460 case PACKAGE:
1461 return JCDiagnostic.fragment("kindname.package");
1463 case ENUM:
1464 case ANNOTATION_TYPE:
1465 case INTERFACE:
1466 case CLASS:
1467 return JCDiagnostic.fragment("kindname.class");
1469 case TYPE_PARAMETER:
1470 return JCDiagnostic.fragment("kindname.type.variable");
1472 case ENUM_CONSTANT:
1473 case FIELD:
1474 case PARAMETER:
1475 case LOCAL_VARIABLE:
1476 case EXCEPTION_PARAMETER:
1477 return JCDiagnostic.fragment("kindname.variable");
1479 case METHOD:
1480 case CONSTRUCTOR:
1481 case STATIC_INIT:
1482 case INSTANCE_INIT:
1483 return JCDiagnostic.fragment("kindname.method");
1485 default:
1486 if (sym.kind == VAL)
1487 // I don't think this can happen but it can't harm
1488 // playing it safe --ahe
1489 return JCDiagnostic.fragment("kindname.value");
1490 else
1491 return JCDiagnostic.fragment("kindname", sym.getKind()); // debug
1492 }
1493 }
1495 /** A localized string describing a given set of kinds.
1496 */
1497 static JCDiagnostic kindNames(int kind) {
1498 StringBuffer key = new StringBuffer();
1499 key.append("kindname");
1500 if ((kind & VAL) != 0)
1501 key.append(((kind & VAL) == VAR) ? ".variable" : ".value");
1502 if ((kind & MTH) != 0) key.append(".method");
1503 if ((kind & TYP) != 0) key.append(".class");
1504 if ((kind & PCK) != 0) key.append(".package");
1505 return JCDiagnostic.fragment(key.toString(), kind);
1506 }
1508 /** A localized string describing the kind -- either class or interface --
1509 * of a given type.
1510 */
1511 static JCDiagnostic typeKindName(Type t) {
1512 if (t.tag == TYPEVAR ||
1513 t.tag == CLASS && (t.tsym.flags() & COMPOUND) != 0)
1514 return JCDiagnostic.fragment("kindname.type.variable.bound");
1515 else if (t.tag == PACKAGE)
1516 return JCDiagnostic.fragment("kindname.package");
1517 else if ((t.tsym.flags_field & ANNOTATION) != 0)
1518 return JCDiagnostic.fragment("kindname.annotation");
1519 else if ((t.tsym.flags_field & INTERFACE) != 0)
1520 return JCDiagnostic.fragment("kindname.interface");
1521 else
1522 return JCDiagnostic.fragment("kindname.class");
1523 }
1525 /** A localized string describing the kind of a missing symbol, given an
1526 * error kind.
1527 */
1528 static JCDiagnostic absentKindName(int kind) {
1529 switch (kind) {
1530 case ABSENT_VAR:
1531 return JCDiagnostic.fragment("kindname.variable");
1532 case WRONG_MTHS: case WRONG_MTH: case ABSENT_MTH:
1533 return JCDiagnostic.fragment("kindname.method");
1534 case ABSENT_TYP:
1535 return JCDiagnostic.fragment("kindname.class");
1536 default:
1537 return JCDiagnostic.fragment("kindname", kind);
1538 }
1539 }
1541 /* ***************************************************************************
1542 * ResolveError classes, indicating error situations when accessing symbols
1543 ****************************************************************************/
1545 public void logAccessError(Env<AttrContext> env, JCTree tree, Type type) {
1546 AccessError error = new AccessError(env, type.getEnclosingType(), type.tsym);
1547 error.report(log, tree.pos(), type.getEnclosingType(), null, null, null);
1548 }
1550 /** Root class for resolve errors.
1551 * Instances of this class indicate "Symbol not found".
1552 * Instances of subclass indicate other errors.
1553 */
1554 private class ResolveError extends Symbol {
1556 ResolveError(int kind, Symbol sym, String debugName) {
1557 super(kind, 0, null, null, null);
1558 this.debugName = debugName;
1559 this.sym = sym;
1560 }
1562 /** The name of the kind of error, for debugging only.
1563 */
1564 final String debugName;
1566 /** The symbol that was determined by resolution, or errSymbol if none
1567 * was found.
1568 */
1569 final Symbol sym;
1571 /** The symbol that was a close mismatch, or null if none was found.
1572 * wrongSym is currently set if a simgle method with the correct name, but
1573 * the wrong parameters was found.
1574 */
1575 Symbol wrongSym;
1577 /** An auxiliary explanation set in case of instantiation errors.
1578 */
1579 JCDiagnostic explanation;
1582 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
1583 throw new AssertionError();
1584 }
1586 /** Print the (debug only) name of the kind of error.
1587 */
1588 public String toString() {
1589 return debugName + " wrongSym=" + wrongSym + " explanation=" + explanation;
1590 }
1592 /** Update wrongSym and explanation and return this.
1593 */
1594 ResolveError setWrongSym(Symbol sym, JCDiagnostic explanation) {
1595 this.wrongSym = sym;
1596 this.explanation = explanation;
1597 return this;
1598 }
1600 /** Update wrongSym and return this.
1601 */
1602 ResolveError setWrongSym(Symbol sym) {
1603 this.wrongSym = sym;
1604 this.explanation = null;
1605 return this;
1606 }
1608 public boolean exists() {
1609 switch (kind) {
1610 case HIDDEN:
1611 case ABSENT_VAR:
1612 case ABSENT_MTH:
1613 case ABSENT_TYP:
1614 return false;
1615 default:
1616 return true;
1617 }
1618 }
1620 /** Report error.
1621 * @param log The error log to be used for error reporting.
1622 * @param pos The position to be used for error reporting.
1623 * @param site The original type from where the selection took place.
1624 * @param name The name of the symbol to be resolved.
1625 * @param argtypes The invocation's value arguments,
1626 * if we looked for a method.
1627 * @param typeargtypes The invocation's type arguments,
1628 * if we looked for a method.
1629 */
1630 void report(Log log, DiagnosticPosition pos, Type site, Name name,
1631 List<Type> argtypes, List<Type> typeargtypes) {
1632 if (name != name.table.error) {
1633 JCDiagnostic kindname = absentKindName(kind);
1634 String idname = name.toString();
1635 String args = "";
1636 String typeargs = "";
1637 if (kind >= WRONG_MTHS && kind <= ABSENT_MTH) {
1638 if (isOperator(name)) {
1639 log.error(pos, "operator.cant.be.applied",
1640 name, Type.toString(argtypes));
1641 return;
1642 }
1643 if (name == name.table.init) {
1644 kindname = JCDiagnostic.fragment("kindname.constructor");
1645 idname = site.tsym.name.toString();
1646 }
1647 args = "(" + Type.toString(argtypes) + ")";
1648 if (typeargtypes != null && typeargtypes.nonEmpty())
1649 typeargs = "<" + Type.toString(typeargtypes) + ">";
1650 }
1651 if (kind == WRONG_MTH) {
1652 log.error(pos,
1653 "cant.apply.symbol" + (explanation != null ? ".1" : ""),
1654 wrongSym.asMemberOf(site, types),
1655 wrongSym.location(site, types),
1656 typeargs,
1657 Type.toString(argtypes),
1658 explanation);
1659 } else if (site.tsym.name.len != 0) {
1660 if (site.tsym.kind == PCK && !site.tsym.exists())
1661 log.error(pos, "doesnt.exist", site.tsym);
1662 else
1663 log.error(pos, "cant.resolve.location",
1664 kindname, idname, args, typeargs,
1665 typeKindName(site), site);
1666 } else {
1667 log.error(pos, "cant.resolve", kindname, idname, args, typeargs);
1668 }
1669 }
1670 }
1671 //where
1672 /** A name designates an operator if it consists
1673 * of a non-empty sequence of operator symbols +-~!/*%&|^<>=
1674 */
1675 boolean isOperator(Name name) {
1676 int i = 0;
1677 while (i < name.len &&
1678 "+-~!*/%&|^<>=".indexOf(name.byteAt(i)) >= 0) i++;
1679 return i > 0 && i == name.len;
1680 }
1681 }
1683 /** Resolve error class indicating that a symbol is not accessible.
1684 */
1685 class AccessError extends ResolveError {
1687 AccessError(Symbol sym) {
1688 this(null, null, sym);
1689 }
1691 AccessError(Env<AttrContext> env, Type site, Symbol sym) {
1692 super(HIDDEN, sym, "access error");
1693 this.env = env;
1694 this.site = site;
1695 if (debugResolve)
1696 log.error("proc.messager", sym + " @ " + site + " is inaccessible.");
1697 }
1699 private Env<AttrContext> env;
1700 private Type site;
1702 /** Report error.
1703 * @param log The error log to be used for error reporting.
1704 * @param pos The position to be used for error reporting.
1705 * @param site The original type from where the selection took place.
1706 * @param name The name of the symbol to be resolved.
1707 * @param argtypes The invocation's value arguments,
1708 * if we looked for a method.
1709 * @param typeargtypes The invocation's type arguments,
1710 * if we looked for a method.
1711 */
1712 void report(Log log, DiagnosticPosition pos, Type site, Name name,
1713 List<Type> argtypes, List<Type> typeargtypes) {
1714 if (sym.owner.type.tag != ERROR) {
1715 if (sym.name == sym.name.table.init && sym.owner != site.tsym)
1716 new ResolveError(ABSENT_MTH, sym.owner, "absent method " + sym).report(
1717 log, pos, site, name, argtypes, typeargtypes);
1718 if ((sym.flags() & PUBLIC) != 0
1719 || (env != null && this.site != null
1720 && !isAccessible(env, this.site)))
1721 log.error(pos, "not.def.access.class.intf.cant.access",
1722 sym, sym.location());
1723 else if ((sym.flags() & (PRIVATE | PROTECTED)) != 0)
1724 log.error(pos, "report.access", sym,
1725 TreeInfo.flagNames(sym.flags() & (PRIVATE | PROTECTED)),
1726 sym.location());
1727 else
1728 log.error(pos, "not.def.public.cant.access",
1729 sym, sym.location());
1730 }
1731 }
1732 }
1734 /** Resolve error class indicating that an instance member was accessed
1735 * from a static context.
1736 */
1737 class StaticError extends ResolveError {
1738 StaticError(Symbol sym) {
1739 super(STATICERR, sym, "static error");
1740 }
1742 /** Report error.
1743 * @param log The error log to be used for error reporting.
1744 * @param pos The position to be used for error reporting.
1745 * @param site The original type from where the selection took place.
1746 * @param name The name of the symbol to be resolved.
1747 * @param argtypes The invocation's value arguments,
1748 * if we looked for a method.
1749 * @param typeargtypes The invocation's type arguments,
1750 * if we looked for a method.
1751 */
1752 void report(Log log,
1753 DiagnosticPosition pos,
1754 Type site,
1755 Name name,
1756 List<Type> argtypes,
1757 List<Type> typeargtypes) {
1758 String symstr = ((sym.kind == TYP && sym.type.tag == CLASS)
1759 ? types.erasure(sym.type)
1760 : sym).toString();
1761 log.error(pos, "non-static.cant.be.ref",
1762 kindName(sym), symstr);
1763 }
1764 }
1766 /** Resolve error class indicating an ambiguous reference.
1767 */
1768 class AmbiguityError extends ResolveError {
1769 Symbol sym1;
1770 Symbol sym2;
1772 AmbiguityError(Symbol sym1, Symbol sym2) {
1773 super(AMBIGUOUS, sym1, "ambiguity error");
1774 this.sym1 = sym1;
1775 this.sym2 = sym2;
1776 }
1778 /** Report error.
1779 * @param log The error log to be used for error reporting.
1780 * @param pos The position to be used for error reporting.
1781 * @param site The original type from where the selection took place.
1782 * @param name The name of the symbol to be resolved.
1783 * @param argtypes The invocation's value arguments,
1784 * if we looked for a method.
1785 * @param typeargtypes The invocation's type arguments,
1786 * if we looked for a method.
1787 */
1788 void report(Log log, DiagnosticPosition pos, Type site, Name name,
1789 List<Type> argtypes, List<Type> typeargtypes) {
1790 AmbiguityError pair = this;
1791 while (true) {
1792 if (pair.sym1.kind == AMBIGUOUS)
1793 pair = (AmbiguityError)pair.sym1;
1794 else if (pair.sym2.kind == AMBIGUOUS)
1795 pair = (AmbiguityError)pair.sym2;
1796 else break;
1797 }
1798 Name sname = pair.sym1.name;
1799 if (sname == sname.table.init) sname = pair.sym1.owner.name;
1800 log.error(pos, "ref.ambiguous", sname,
1801 kindName(pair.sym1),
1802 pair.sym1,
1803 pair.sym1.location(site, types),
1804 kindName(pair.sym2),
1805 pair.sym2,
1806 pair.sym2.location(site, types));
1807 }
1808 }
1809 }