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

Wed, 19 Oct 2011 16:56:05 +0100

author
mcimadamore
date
Wed, 19 Oct 2011 16:56:05 +0100
changeset 1110
366c233eb838
parent 1059
0b5beb9562c6
child 1114
05814303a056
permissions
-rw-r--r--

7102515: javac running very very long and not returning
Summary: Verbose resolution diagnostics slow down with operator resolution
Reviewed-by: jjg

duke@1 1 /*
jjg@816 2 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.comp;
duke@1 27
duke@1 28 import com.sun.tools.javac.util.*;
duke@1 29 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
duke@1 30 import com.sun.tools.javac.code.*;
duke@1 31 import com.sun.tools.javac.jvm.*;
duke@1 32 import com.sun.tools.javac.tree.*;
mcimadamore@161 33 import com.sun.tools.javac.api.Formattable.LocalizedString;
mcimadamore@160 34 import static com.sun.tools.javac.comp.Resolve.MethodResolutionPhase.*;
duke@1 35
duke@1 36 import com.sun.tools.javac.code.Type.*;
duke@1 37 import com.sun.tools.javac.code.Symbol.*;
duke@1 38 import com.sun.tools.javac.tree.JCTree.*;
duke@1 39
duke@1 40 import static com.sun.tools.javac.code.Flags.*;
duke@1 41 import static com.sun.tools.javac.code.Kinds.*;
duke@1 42 import static com.sun.tools.javac.code.TypeTags.*;
jjg@643 43 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
mcimadamore@631 44 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
duke@1 45 import javax.lang.model.element.ElementVisitor;
duke@1 46
mcimadamore@160 47 import java.util.Map;
mcimadamore@913 48 import java.util.Set;
mcimadamore@160 49 import java.util.HashMap;
mcimadamore@913 50 import java.util.HashSet;
mcimadamore@160 51
duke@1 52 /** Helper class for name resolution, used mostly by the attribution phase.
duke@1 53 *
jjg@581 54 * <p><b>This is NOT part of any supported API.
jjg@581 55 * If you write code that depends on this, you do so at your own risk.
duke@1 56 * This code and its internal interfaces are subject to change or
duke@1 57 * deletion without notice.</b>
duke@1 58 */
duke@1 59 public class Resolve {
duke@1 60 protected static final Context.Key<Resolve> resolveKey =
duke@1 61 new Context.Key<Resolve>();
duke@1 62
jjg@113 63 Names names;
duke@1 64 Log log;
duke@1 65 Symtab syms;
duke@1 66 Check chk;
duke@1 67 Infer infer;
duke@1 68 ClassReader reader;
duke@1 69 TreeInfo treeinfo;
duke@1 70 Types types;
mcimadamore@89 71 JCDiagnostic.Factory diags;
duke@1 72 public final boolean boxingEnabled; // = source.allowBoxing();
duke@1 73 public final boolean varargsEnabled; // = source.allowVarargs();
mcimadamore@674 74 public final boolean allowMethodHandles;
duke@1 75 private final boolean debugResolve;
duke@1 76
mcimadamore@674 77 Scope polymorphicSignatureScope;
mcimadamore@674 78
duke@1 79 public static Resolve instance(Context context) {
duke@1 80 Resolve instance = context.get(resolveKey);
duke@1 81 if (instance == null)
duke@1 82 instance = new Resolve(context);
duke@1 83 return instance;
duke@1 84 }
duke@1 85
duke@1 86 protected Resolve(Context context) {
duke@1 87 context.put(resolveKey, this);
duke@1 88 syms = Symtab.instance(context);
duke@1 89
duke@1 90 varNotFound = new
mcimadamore@302 91 SymbolNotFoundError(ABSENT_VAR);
duke@1 92 wrongMethod = new
mcimadamore@302 93 InapplicableSymbolError(syms.errSymbol);
duke@1 94 wrongMethods = new
mcimadamore@302 95 InapplicableSymbolsError(syms.errSymbol);
duke@1 96 methodNotFound = new
mcimadamore@302 97 SymbolNotFoundError(ABSENT_MTH);
duke@1 98 typeNotFound = new
mcimadamore@302 99 SymbolNotFoundError(ABSENT_TYP);
duke@1 100
jjg@113 101 names = Names.instance(context);
duke@1 102 log = Log.instance(context);
duke@1 103 chk = Check.instance(context);
duke@1 104 infer = Infer.instance(context);
duke@1 105 reader = ClassReader.instance(context);
duke@1 106 treeinfo = TreeInfo.instance(context);
duke@1 107 types = Types.instance(context);
mcimadamore@89 108 diags = JCDiagnostic.Factory.instance(context);
duke@1 109 Source source = Source.instance(context);
duke@1 110 boxingEnabled = source.allowBoxing();
duke@1 111 varargsEnabled = source.allowVarargs();
duke@1 112 Options options = Options.instance(context);
jjg@700 113 debugResolve = options.isSet("debugresolve");
mcimadamore@674 114 Target target = Target.instance(context);
mcimadamore@820 115 allowMethodHandles = target.hasMethodHandles();
mcimadamore@674 116 polymorphicSignatureScope = new Scope(syms.noSymbol);
mcimadamore@689 117
mcimadamore@689 118 inapplicableMethodException = new InapplicableMethodException(diags);
duke@1 119 }
duke@1 120
duke@1 121 /** error symbols, which are returned when resolution fails
duke@1 122 */
mcimadamore@302 123 final SymbolNotFoundError varNotFound;
mcimadamore@302 124 final InapplicableSymbolError wrongMethod;
mcimadamore@302 125 final InapplicableSymbolsError wrongMethods;
mcimadamore@302 126 final SymbolNotFoundError methodNotFound;
mcimadamore@302 127 final SymbolNotFoundError typeNotFound;
duke@1 128
duke@1 129 /* ************************************************************************
duke@1 130 * Identifier resolution
duke@1 131 *************************************************************************/
duke@1 132
duke@1 133 /** An environment is "static" if its static level is greater than
duke@1 134 * the one of its outer environment
duke@1 135 */
duke@1 136 static boolean isStatic(Env<AttrContext> env) {
duke@1 137 return env.info.staticLevel > env.outer.info.staticLevel;
duke@1 138 }
duke@1 139
duke@1 140 /** An environment is an "initializer" if it is a constructor or
duke@1 141 * an instance initializer.
duke@1 142 */
duke@1 143 static boolean isInitializer(Env<AttrContext> env) {
duke@1 144 Symbol owner = env.info.scope.owner;
duke@1 145 return owner.isConstructor() ||
duke@1 146 owner.owner.kind == TYP &&
duke@1 147 (owner.kind == VAR ||
duke@1 148 owner.kind == MTH && (owner.flags() & BLOCK) != 0) &&
duke@1 149 (owner.flags() & STATIC) == 0;
duke@1 150 }
duke@1 151
duke@1 152 /** Is class accessible in given evironment?
duke@1 153 * @param env The current environment.
duke@1 154 * @param c The class whose accessibility is checked.
duke@1 155 */
duke@1 156 public boolean isAccessible(Env<AttrContext> env, TypeSymbol c) {
mcimadamore@741 157 return isAccessible(env, c, false);
mcimadamore@741 158 }
mcimadamore@741 159
mcimadamore@741 160 public boolean isAccessible(Env<AttrContext> env, TypeSymbol c, boolean checkInner) {
mcimadamore@741 161 boolean isAccessible = false;
duke@1 162 switch ((short)(c.flags() & AccessFlags)) {
mcimadamore@741 163 case PRIVATE:
mcimadamore@741 164 isAccessible =
mcimadamore@741 165 env.enclClass.sym.outermostClass() ==
mcimadamore@741 166 c.owner.outermostClass();
mcimadamore@741 167 break;
mcimadamore@741 168 case 0:
mcimadamore@741 169 isAccessible =
mcimadamore@741 170 env.toplevel.packge == c.owner // fast special case
mcimadamore@741 171 ||
mcimadamore@741 172 env.toplevel.packge == c.packge()
mcimadamore@741 173 ||
mcimadamore@741 174 // Hack: this case is added since synthesized default constructors
mcimadamore@741 175 // of anonymous classes should be allowed to access
mcimadamore@741 176 // classes which would be inaccessible otherwise.
mcimadamore@741 177 env.enclMethod != null &&
mcimadamore@741 178 (env.enclMethod.mods.flags & ANONCONSTR) != 0;
mcimadamore@741 179 break;
mcimadamore@741 180 default: // error recovery
mcimadamore@741 181 case PUBLIC:
mcimadamore@741 182 isAccessible = true;
mcimadamore@741 183 break;
mcimadamore@741 184 case PROTECTED:
mcimadamore@741 185 isAccessible =
mcimadamore@741 186 env.toplevel.packge == c.owner // fast special case
mcimadamore@741 187 ||
mcimadamore@741 188 env.toplevel.packge == c.packge()
mcimadamore@741 189 ||
mcimadamore@741 190 isInnerSubClass(env.enclClass.sym, c.owner);
mcimadamore@741 191 break;
duke@1 192 }
mcimadamore@741 193 return (checkInner == false || c.type.getEnclosingType() == Type.noType) ?
mcimadamore@741 194 isAccessible :
jjg@789 195 isAccessible && isAccessible(env, c.type.getEnclosingType(), checkInner);
duke@1 196 }
duke@1 197 //where
duke@1 198 /** Is given class a subclass of given base class, or an inner class
duke@1 199 * of a subclass?
duke@1 200 * Return null if no such class exists.
duke@1 201 * @param c The class which is the subclass or is contained in it.
duke@1 202 * @param base The base class
duke@1 203 */
duke@1 204 private boolean isInnerSubClass(ClassSymbol c, Symbol base) {
duke@1 205 while (c != null && !c.isSubClass(base, types)) {
duke@1 206 c = c.owner.enclClass();
duke@1 207 }
duke@1 208 return c != null;
duke@1 209 }
duke@1 210
duke@1 211 boolean isAccessible(Env<AttrContext> env, Type t) {
mcimadamore@741 212 return isAccessible(env, t, false);
mcimadamore@741 213 }
mcimadamore@741 214
mcimadamore@741 215 boolean isAccessible(Env<AttrContext> env, Type t, boolean checkInner) {
duke@1 216 return (t.tag == ARRAY)
duke@1 217 ? isAccessible(env, types.elemtype(t))
mcimadamore@741 218 : isAccessible(env, t.tsym, checkInner);
duke@1 219 }
duke@1 220
duke@1 221 /** Is symbol accessible as a member of given type in given evironment?
duke@1 222 * @param env The current environment.
duke@1 223 * @param site The type of which the tested symbol is regarded
duke@1 224 * as a member.
duke@1 225 * @param sym The symbol.
duke@1 226 */
duke@1 227 public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym) {
mcimadamore@741 228 return isAccessible(env, site, sym, false);
mcimadamore@741 229 }
mcimadamore@741 230 public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym, boolean checkInner) {
duke@1 231 if (sym.name == names.init && sym.owner != site.tsym) return false;
duke@1 232 switch ((short)(sym.flags() & AccessFlags)) {
duke@1 233 case PRIVATE:
duke@1 234 return
duke@1 235 (env.enclClass.sym == sym.owner // fast special case
duke@1 236 ||
duke@1 237 env.enclClass.sym.outermostClass() ==
duke@1 238 sym.owner.outermostClass())
duke@1 239 &&
duke@1 240 sym.isInheritedIn(site.tsym, types);
duke@1 241 case 0:
duke@1 242 return
duke@1 243 (env.toplevel.packge == sym.owner.owner // fast special case
duke@1 244 ||
duke@1 245 env.toplevel.packge == sym.packge())
duke@1 246 &&
mcimadamore@741 247 isAccessible(env, site, checkInner)
duke@1 248 &&
mcimadamore@254 249 sym.isInheritedIn(site.tsym, types)
mcimadamore@254 250 &&
mcimadamore@254 251 notOverriddenIn(site, sym);
duke@1 252 case PROTECTED:
duke@1 253 return
duke@1 254 (env.toplevel.packge == sym.owner.owner // fast special case
duke@1 255 ||
duke@1 256 env.toplevel.packge == sym.packge()
duke@1 257 ||
duke@1 258 isProtectedAccessible(sym, env.enclClass.sym, site)
duke@1 259 ||
duke@1 260 // OK to select instance method or field from 'super' or type name
duke@1 261 // (but type names should be disallowed elsewhere!)
duke@1 262 env.info.selectSuper && (sym.flags() & STATIC) == 0 && sym.kind != TYP)
duke@1 263 &&
mcimadamore@741 264 isAccessible(env, site, checkInner)
duke@1 265 &&
mcimadamore@254 266 notOverriddenIn(site, sym);
duke@1 267 default: // this case includes erroneous combinations as well
mcimadamore@741 268 return isAccessible(env, site, checkInner) && notOverriddenIn(site, sym);
mcimadamore@254 269 }
mcimadamore@254 270 }
mcimadamore@254 271 //where
mcimadamore@254 272 /* `sym' is accessible only if not overridden by
mcimadamore@254 273 * another symbol which is a member of `site'
mcimadamore@254 274 * (because, if it is overridden, `sym' is not strictly
mcimadamore@674 275 * speaking a member of `site'). A polymorphic signature method
mcimadamore@674 276 * cannot be overridden (e.g. MH.invokeExact(Object[])).
mcimadamore@254 277 */
mcimadamore@254 278 private boolean notOverriddenIn(Type site, Symbol sym) {
mcimadamore@254 279 if (sym.kind != MTH || sym.isConstructor() || sym.isStatic())
mcimadamore@254 280 return true;
mcimadamore@254 281 else {
mcimadamore@254 282 Symbol s2 = ((MethodSymbol)sym).implementation(site.tsym, types, true);
mcimadamore@844 283 return (s2 == null || s2 == sym || sym.owner == s2.owner ||
mcimadamore@674 284 s2.isPolymorphicSignatureGeneric() ||
mcimadamore@325 285 !types.isSubSignature(types.memberType(site, s2), types.memberType(site, sym)));
duke@1 286 }
duke@1 287 }
duke@1 288 //where
duke@1 289 /** Is given protected symbol accessible if it is selected from given site
duke@1 290 * and the selection takes place in given class?
duke@1 291 * @param sym The symbol with protected access
duke@1 292 * @param c The class where the access takes place
duke@1 293 * @site The type of the qualifier
duke@1 294 */
duke@1 295 private
duke@1 296 boolean isProtectedAccessible(Symbol sym, ClassSymbol c, Type site) {
duke@1 297 while (c != null &&
duke@1 298 !(c.isSubClass(sym.owner, types) &&
duke@1 299 (c.flags() & INTERFACE) == 0 &&
duke@1 300 // In JLS 2e 6.6.2.1, the subclass restriction applies
duke@1 301 // only to instance fields and methods -- types are excluded
duke@1 302 // regardless of whether they are declared 'static' or not.
duke@1 303 ((sym.flags() & STATIC) != 0 || sym.kind == TYP || site.tsym.isSubClass(c, types))))
duke@1 304 c = c.owner.enclClass();
duke@1 305 return c != null;
duke@1 306 }
duke@1 307
duke@1 308 /** Try to instantiate the type of a method so that it fits
duke@1 309 * given type arguments and argument types. If succesful, return
duke@1 310 * the method's instantiated type, else return null.
duke@1 311 * The instantiation will take into account an additional leading
duke@1 312 * formal parameter if the method is an instance method seen as a member
duke@1 313 * of un underdetermined site In this case, we treat site as an additional
duke@1 314 * parameter and the parameters of the class containing the method as
duke@1 315 * additional type variables that get instantiated.
duke@1 316 *
duke@1 317 * @param env The current environment
duke@1 318 * @param site The type of which the method is a member.
duke@1 319 * @param m The method symbol.
duke@1 320 * @param argtypes The invocation's given value arguments.
duke@1 321 * @param typeargtypes The invocation's given type arguments.
duke@1 322 * @param allowBoxing Allow boxing conversions of arguments.
duke@1 323 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 324 */
duke@1 325 Type rawInstantiate(Env<AttrContext> env,
duke@1 326 Type site,
duke@1 327 Symbol m,
duke@1 328 List<Type> argtypes,
duke@1 329 List<Type> typeargtypes,
duke@1 330 boolean allowBoxing,
duke@1 331 boolean useVarargs,
duke@1 332 Warner warn)
mcimadamore@299 333 throws Infer.InferenceException {
mcimadamore@820 334 boolean polymorphicSignature = m.isPolymorphicSignatureGeneric() && allowMethodHandles;
mcimadamore@689 335 if (useVarargs && (m.flags() & VARARGS) == 0)
mcimadamore@845 336 throw inapplicableMethodException.setMessage();
duke@1 337 Type mt = types.memberType(site, m);
duke@1 338
duke@1 339 // tvars is the list of formal type variables for which type arguments
duke@1 340 // need to inferred.
mcimadamore@950 341 List<Type> tvars = null;
mcimadamore@950 342 if (env.info.tvars != null) {
mcimadamore@950 343 tvars = types.newInstances(env.info.tvars);
mcimadamore@950 344 mt = types.subst(mt, env.info.tvars, tvars);
mcimadamore@950 345 }
duke@1 346 if (typeargtypes == null) typeargtypes = List.nil();
mcimadamore@820 347 if (mt.tag != FORALL && typeargtypes.nonEmpty()) {
duke@1 348 // This is not a polymorphic method, but typeargs are supplied
jjh@972 349 // which is fine, see JLS 15.12.2.1
duke@1 350 } else if (mt.tag == FORALL && typeargtypes.nonEmpty()) {
duke@1 351 ForAll pmt = (ForAll) mt;
duke@1 352 if (typeargtypes.length() != pmt.tvars.length())
mcimadamore@689 353 throw inapplicableMethodException.setMessage("arg.length.mismatch"); // not enough args
duke@1 354 // Check type arguments are within bounds
duke@1 355 List<Type> formals = pmt.tvars;
duke@1 356 List<Type> actuals = typeargtypes;
duke@1 357 while (formals.nonEmpty() && actuals.nonEmpty()) {
duke@1 358 List<Type> bounds = types.subst(types.getBounds((TypeVar)formals.head),
duke@1 359 pmt.tvars, typeargtypes);
duke@1 360 for (; bounds.nonEmpty(); bounds = bounds.tail)
duke@1 361 if (!types.isSubtypeUnchecked(actuals.head, bounds.head, warn))
mcimadamore@689 362 throw inapplicableMethodException.setMessage("explicit.param.do.not.conform.to.bounds",actuals.head, bounds);
duke@1 363 formals = formals.tail;
duke@1 364 actuals = actuals.tail;
duke@1 365 }
duke@1 366 mt = types.subst(pmt.qtype, pmt.tvars, typeargtypes);
duke@1 367 } else if (mt.tag == FORALL) {
duke@1 368 ForAll pmt = (ForAll) mt;
duke@1 369 List<Type> tvars1 = types.newInstances(pmt.tvars);
duke@1 370 tvars = tvars.appendList(tvars1);
duke@1 371 mt = types.subst(pmt.qtype, pmt.tvars, tvars1);
duke@1 372 }
duke@1 373
duke@1 374 // find out whether we need to go the slow route via infer
mcimadamore@674 375 boolean instNeeded = tvars.tail != null || /*inlined: tvars.nonEmpty()*/
mcimadamore@674 376 polymorphicSignature;
duke@1 377 for (List<Type> l = argtypes;
duke@1 378 l.tail != null/*inlined: l.nonEmpty()*/ && !instNeeded;
duke@1 379 l = l.tail) {
duke@1 380 if (l.head.tag == FORALL) instNeeded = true;
duke@1 381 }
duke@1 382
duke@1 383 if (instNeeded)
mcimadamore@674 384 return polymorphicSignature ?
mcimadamore@820 385 infer.instantiatePolymorphicSignatureInstance(env, site, m.name, (MethodSymbol)m, argtypes) :
mcimadamore@674 386 infer.instantiateMethod(env,
mcimadamore@547 387 tvars,
duke@1 388 (MethodType)mt,
mcimadamore@580 389 m,
duke@1 390 argtypes,
duke@1 391 allowBoxing,
duke@1 392 useVarargs,
duke@1 393 warn);
mcimadamore@689 394
mcimadamore@845 395 checkRawArgumentsAcceptable(env, argtypes, mt.getParameterTypes(),
mcimadamore@689 396 allowBoxing, useVarargs, warn);
mcimadamore@689 397 return mt;
duke@1 398 }
duke@1 399
duke@1 400 /** Same but returns null instead throwing a NoInstanceException
duke@1 401 */
duke@1 402 Type instantiate(Env<AttrContext> env,
duke@1 403 Type site,
duke@1 404 Symbol m,
duke@1 405 List<Type> argtypes,
duke@1 406 List<Type> typeargtypes,
duke@1 407 boolean allowBoxing,
duke@1 408 boolean useVarargs,
duke@1 409 Warner warn) {
duke@1 410 try {
duke@1 411 return rawInstantiate(env, site, m, argtypes, typeargtypes,
duke@1 412 allowBoxing, useVarargs, warn);
mcimadamore@689 413 } catch (InapplicableMethodException ex) {
duke@1 414 return null;
duke@1 415 }
duke@1 416 }
duke@1 417
duke@1 418 /** Check if a parameter list accepts a list of args.
duke@1 419 */
mcimadamore@845 420 boolean argumentsAcceptable(Env<AttrContext> env,
mcimadamore@845 421 List<Type> argtypes,
duke@1 422 List<Type> formals,
duke@1 423 boolean allowBoxing,
duke@1 424 boolean useVarargs,
duke@1 425 Warner warn) {
mcimadamore@689 426 try {
mcimadamore@845 427 checkRawArgumentsAcceptable(env, argtypes, formals, allowBoxing, useVarargs, warn);
mcimadamore@689 428 return true;
mcimadamore@689 429 } catch (InapplicableMethodException ex) {
mcimadamore@689 430 return false;
mcimadamore@689 431 }
mcimadamore@689 432 }
mcimadamore@845 433 void checkRawArgumentsAcceptable(Env<AttrContext> env,
mcimadamore@845 434 List<Type> argtypes,
mcimadamore@689 435 List<Type> formals,
mcimadamore@689 436 boolean allowBoxing,
mcimadamore@689 437 boolean useVarargs,
mcimadamore@689 438 Warner warn) {
duke@1 439 Type varargsFormal = useVarargs ? formals.last() : null;
mcimadamore@689 440 if (varargsFormal == null &&
mcimadamore@689 441 argtypes.size() != formals.size()) {
mcimadamore@689 442 throw inapplicableMethodException.setMessage("arg.length.mismatch"); // not enough args
mcimadamore@689 443 }
mcimadamore@689 444
duke@1 445 while (argtypes.nonEmpty() && formals.head != varargsFormal) {
duke@1 446 boolean works = allowBoxing
duke@1 447 ? types.isConvertible(argtypes.head, formals.head, warn)
duke@1 448 : types.isSubtypeUnchecked(argtypes.head, formals.head, warn);
mcimadamore@689 449 if (!works)
mcimadamore@689 450 throw inapplicableMethodException.setMessage("no.conforming.assignment.exists",
mcimadamore@689 451 argtypes.head,
mcimadamore@689 452 formals.head);
duke@1 453 argtypes = argtypes.tail;
duke@1 454 formals = formals.tail;
duke@1 455 }
mcimadamore@689 456
mcimadamore@689 457 if (formals.head != varargsFormal)
mcimadamore@689 458 throw inapplicableMethodException.setMessage("arg.length.mismatch"); // not enough args
mcimadamore@689 459
mcimadamore@689 460 if (useVarargs) {
mcimadamore@1006 461 Type elt = types.elemtype(varargsFormal);
mcimadamore@689 462 while (argtypes.nonEmpty()) {
mcimadamore@689 463 if (!types.isConvertible(argtypes.head, elt, warn))
mcimadamore@689 464 throw inapplicableMethodException.setMessage("varargs.argument.mismatch",
mcimadamore@689 465 argtypes.head,
mcimadamore@689 466 elt);
mcimadamore@689 467 argtypes = argtypes.tail;
mcimadamore@689 468 }
mcimadamore@845 469 //check varargs element type accessibility
mcimadamore@845 470 if (!isAccessible(env, elt)) {
mcimadamore@845 471 Symbol location = env.enclClass.sym;
mcimadamore@845 472 throw inapplicableMethodException.setMessage("inaccessible.varargs.type",
mcimadamore@845 473 elt,
mcimadamore@845 474 Kinds.kindName(location),
mcimadamore@845 475 location);
mcimadamore@845 476 }
duke@1 477 }
mcimadamore@689 478 return;
duke@1 479 }
mcimadamore@689 480 // where
mcimadamore@689 481 public static class InapplicableMethodException extends RuntimeException {
mcimadamore@689 482 private static final long serialVersionUID = 0;
mcimadamore@689 483
mcimadamore@689 484 JCDiagnostic diagnostic;
mcimadamore@689 485 JCDiagnostic.Factory diags;
mcimadamore@689 486
mcimadamore@689 487 InapplicableMethodException(JCDiagnostic.Factory diags) {
mcimadamore@689 488 this.diagnostic = null;
mcimadamore@689 489 this.diags = diags;
mcimadamore@689 490 }
mcimadamore@845 491 InapplicableMethodException setMessage() {
mcimadamore@845 492 this.diagnostic = null;
mcimadamore@845 493 return this;
mcimadamore@845 494 }
mcimadamore@689 495 InapplicableMethodException setMessage(String key) {
mcimadamore@689 496 this.diagnostic = key != null ? diags.fragment(key) : null;
mcimadamore@689 497 return this;
mcimadamore@689 498 }
mcimadamore@689 499 InapplicableMethodException setMessage(String key, Object... args) {
mcimadamore@689 500 this.diagnostic = key != null ? diags.fragment(key, args) : null;
mcimadamore@689 501 return this;
mcimadamore@689 502 }
mcimadamore@845 503 InapplicableMethodException setMessage(JCDiagnostic diag) {
mcimadamore@845 504 this.diagnostic = diag;
mcimadamore@845 505 return this;
mcimadamore@845 506 }
mcimadamore@689 507
mcimadamore@689 508 public JCDiagnostic getDiagnostic() {
mcimadamore@689 509 return diagnostic;
mcimadamore@689 510 }
mcimadamore@689 511 }
mcimadamore@689 512 private final InapplicableMethodException inapplicableMethodException;
duke@1 513
duke@1 514 /* ***************************************************************************
duke@1 515 * Symbol lookup
duke@1 516 * the following naming conventions for arguments are used
duke@1 517 *
duke@1 518 * env is the environment where the symbol was mentioned
duke@1 519 * site is the type of which the symbol is a member
duke@1 520 * name is the symbol's name
duke@1 521 * if no arguments are given
duke@1 522 * argtypes are the value arguments, if we search for a method
duke@1 523 *
duke@1 524 * If no symbol was found, a ResolveError detailing the problem is returned.
duke@1 525 ****************************************************************************/
duke@1 526
duke@1 527 /** Find field. Synthetic fields are always skipped.
duke@1 528 * @param env The current environment.
duke@1 529 * @param site The original type from where the selection takes place.
duke@1 530 * @param name The name of the field.
duke@1 531 * @param c The class to search for the field. This is always
duke@1 532 * a superclass or implemented interface of site's class.
duke@1 533 */
duke@1 534 Symbol findField(Env<AttrContext> env,
duke@1 535 Type site,
duke@1 536 Name name,
duke@1 537 TypeSymbol c) {
mcimadamore@19 538 while (c.type.tag == TYPEVAR)
mcimadamore@19 539 c = c.type.getUpperBound().tsym;
duke@1 540 Symbol bestSoFar = varNotFound;
duke@1 541 Symbol sym;
duke@1 542 Scope.Entry e = c.members().lookup(name);
duke@1 543 while (e.scope != null) {
duke@1 544 if (e.sym.kind == VAR && (e.sym.flags_field & SYNTHETIC) == 0) {
duke@1 545 return isAccessible(env, site, e.sym)
duke@1 546 ? e.sym : new AccessError(env, site, e.sym);
duke@1 547 }
duke@1 548 e = e.next();
duke@1 549 }
duke@1 550 Type st = types.supertype(c.type);
mcimadamore@19 551 if (st != null && (st.tag == CLASS || st.tag == TYPEVAR)) {
duke@1 552 sym = findField(env, site, name, st.tsym);
duke@1 553 if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 554 }
duke@1 555 for (List<Type> l = types.interfaces(c.type);
duke@1 556 bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
duke@1 557 l = l.tail) {
duke@1 558 sym = findField(env, site, name, l.head.tsym);
duke@1 559 if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS &&
duke@1 560 sym.owner != bestSoFar.owner)
duke@1 561 bestSoFar = new AmbiguityError(bestSoFar, sym);
duke@1 562 else if (sym.kind < bestSoFar.kind)
duke@1 563 bestSoFar = sym;
duke@1 564 }
duke@1 565 return bestSoFar;
duke@1 566 }
duke@1 567
duke@1 568 /** Resolve a field identifier, throw a fatal error if not found.
duke@1 569 * @param pos The position to use for error reporting.
duke@1 570 * @param env The environment current at the method invocation.
duke@1 571 * @param site The type of the qualifying expression, in which
duke@1 572 * identifier is searched.
duke@1 573 * @param name The identifier's name.
duke@1 574 */
duke@1 575 public VarSymbol resolveInternalField(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 576 Type site, Name name) {
duke@1 577 Symbol sym = findField(env, site, name, site.tsym);
duke@1 578 if (sym.kind == VAR) return (VarSymbol)sym;
duke@1 579 else throw new FatalError(
mcimadamore@89 580 diags.fragment("fatal.err.cant.locate.field",
duke@1 581 name));
duke@1 582 }
duke@1 583
duke@1 584 /** Find unqualified variable or field with given name.
duke@1 585 * Synthetic fields always skipped.
duke@1 586 * @param env The current environment.
duke@1 587 * @param name The name of the variable or field.
duke@1 588 */
duke@1 589 Symbol findVar(Env<AttrContext> env, Name name) {
duke@1 590 Symbol bestSoFar = varNotFound;
duke@1 591 Symbol sym;
duke@1 592 Env<AttrContext> env1 = env;
duke@1 593 boolean staticOnly = false;
duke@1 594 while (env1.outer != null) {
duke@1 595 if (isStatic(env1)) staticOnly = true;
duke@1 596 Scope.Entry e = env1.info.scope.lookup(name);
duke@1 597 while (e.scope != null &&
duke@1 598 (e.sym.kind != VAR ||
duke@1 599 (e.sym.flags_field & SYNTHETIC) != 0))
duke@1 600 e = e.next();
duke@1 601 sym = (e.scope != null)
duke@1 602 ? e.sym
duke@1 603 : findField(
duke@1 604 env1, env1.enclClass.sym.type, name, env1.enclClass.sym);
duke@1 605 if (sym.exists()) {
duke@1 606 if (staticOnly &&
duke@1 607 sym.kind == VAR &&
duke@1 608 sym.owner.kind == TYP &&
duke@1 609 (sym.flags() & STATIC) == 0)
duke@1 610 return new StaticError(sym);
duke@1 611 else
duke@1 612 return sym;
duke@1 613 } else if (sym.kind < bestSoFar.kind) {
duke@1 614 bestSoFar = sym;
duke@1 615 }
duke@1 616
duke@1 617 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
duke@1 618 env1 = env1.outer;
duke@1 619 }
duke@1 620
duke@1 621 sym = findField(env, syms.predefClass.type, name, syms.predefClass);
duke@1 622 if (sym.exists())
duke@1 623 return sym;
duke@1 624 if (bestSoFar.exists())
duke@1 625 return bestSoFar;
duke@1 626
duke@1 627 Scope.Entry e = env.toplevel.namedImportScope.lookup(name);
duke@1 628 for (; e.scope != null; e = e.next()) {
duke@1 629 sym = e.sym;
duke@1 630 Type origin = e.getOrigin().owner.type;
duke@1 631 if (sym.kind == VAR) {
duke@1 632 if (e.sym.owner.type != origin)
duke@1 633 sym = sym.clone(e.getOrigin().owner);
duke@1 634 return isAccessible(env, origin, sym)
duke@1 635 ? sym : new AccessError(env, origin, sym);
duke@1 636 }
duke@1 637 }
duke@1 638
duke@1 639 Symbol origin = null;
duke@1 640 e = env.toplevel.starImportScope.lookup(name);
duke@1 641 for (; e.scope != null; e = e.next()) {
duke@1 642 sym = e.sym;
duke@1 643 if (sym.kind != VAR)
duke@1 644 continue;
duke@1 645 // invariant: sym.kind == VAR
duke@1 646 if (bestSoFar.kind < AMBIGUOUS && sym.owner != bestSoFar.owner)
duke@1 647 return new AmbiguityError(bestSoFar, sym);
duke@1 648 else if (bestSoFar.kind >= VAR) {
duke@1 649 origin = e.getOrigin().owner;
duke@1 650 bestSoFar = isAccessible(env, origin.type, sym)
duke@1 651 ? sym : new AccessError(env, origin.type, sym);
duke@1 652 }
duke@1 653 }
duke@1 654 if (bestSoFar.kind == VAR && bestSoFar.owner.type != origin.type)
duke@1 655 return bestSoFar.clone(origin);
duke@1 656 else
duke@1 657 return bestSoFar;
duke@1 658 }
duke@1 659
duke@1 660 Warner noteWarner = new Warner();
duke@1 661
duke@1 662 /** Select the best method for a call site among two choices.
duke@1 663 * @param env The current environment.
duke@1 664 * @param site The original type from where the
duke@1 665 * selection takes place.
duke@1 666 * @param argtypes The invocation's value arguments,
duke@1 667 * @param typeargtypes The invocation's type arguments,
duke@1 668 * @param sym Proposed new best match.
duke@1 669 * @param bestSoFar Previously found best match.
duke@1 670 * @param allowBoxing Allow boxing conversions of arguments.
duke@1 671 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 672 */
mcimadamore@689 673 @SuppressWarnings("fallthrough")
duke@1 674 Symbol selectBest(Env<AttrContext> env,
duke@1 675 Type site,
duke@1 676 List<Type> argtypes,
duke@1 677 List<Type> typeargtypes,
duke@1 678 Symbol sym,
duke@1 679 Symbol bestSoFar,
duke@1 680 boolean allowBoxing,
duke@1 681 boolean useVarargs,
duke@1 682 boolean operator) {
duke@1 683 if (sym.kind == ERR) return bestSoFar;
mcimadamore@171 684 if (!sym.isInheritedIn(site.tsym, types)) return bestSoFar;
jjg@816 685 Assert.check(sym.kind < AMBIGUOUS);
duke@1 686 try {
mcimadamore@689 687 rawInstantiate(env, site, sym, argtypes, typeargtypes,
mcimadamore@689 688 allowBoxing, useVarargs, Warner.noWarnings);
mcimadamore@689 689 } catch (InapplicableMethodException ex) {
duke@1 690 switch (bestSoFar.kind) {
duke@1 691 case ABSENT_MTH:
duke@1 692 return wrongMethod.setWrongSym(sym, ex.getDiagnostic());
duke@1 693 case WRONG_MTH:
mcimadamore@1110 694 if (operator) return bestSoFar;
mcimadamore@689 695 wrongMethods.addCandidate(currentStep, wrongMethod.sym, wrongMethod.explanation);
mcimadamore@689 696 case WRONG_MTHS:
mcimadamore@689 697 return wrongMethods.addCandidate(currentStep, sym, ex.getDiagnostic());
duke@1 698 default:
duke@1 699 return bestSoFar;
duke@1 700 }
duke@1 701 }
duke@1 702 if (!isAccessible(env, site, sym)) {
duke@1 703 return (bestSoFar.kind == ABSENT_MTH)
duke@1 704 ? new AccessError(env, site, sym)
duke@1 705 : bestSoFar;
mcimadamore@689 706 }
duke@1 707 return (bestSoFar.kind > AMBIGUOUS)
duke@1 708 ? sym
duke@1 709 : mostSpecific(sym, bestSoFar, env, site,
duke@1 710 allowBoxing && operator, useVarargs);
duke@1 711 }
duke@1 712
duke@1 713 /* Return the most specific of the two methods for a call,
duke@1 714 * given that both are accessible and applicable.
duke@1 715 * @param m1 A new candidate for most specific.
duke@1 716 * @param m2 The previous most specific candidate.
duke@1 717 * @param env The current environment.
duke@1 718 * @param site The original type from where the selection
duke@1 719 * takes place.
duke@1 720 * @param allowBoxing Allow boxing conversions of arguments.
duke@1 721 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 722 */
duke@1 723 Symbol mostSpecific(Symbol m1,
duke@1 724 Symbol m2,
duke@1 725 Env<AttrContext> env,
mcimadamore@254 726 final Type site,
duke@1 727 boolean allowBoxing,
duke@1 728 boolean useVarargs) {
duke@1 729 switch (m2.kind) {
duke@1 730 case MTH:
duke@1 731 if (m1 == m2) return m1;
mcimadamore@775 732 boolean m1SignatureMoreSpecific = signatureMoreSpecific(env, site, m1, m2, allowBoxing, useVarargs);
mcimadamore@775 733 boolean m2SignatureMoreSpecific = signatureMoreSpecific(env, site, m2, m1, allowBoxing, useVarargs);
duke@1 734 if (m1SignatureMoreSpecific && m2SignatureMoreSpecific) {
mcimadamore@775 735 Type mt1 = types.memberType(site, m1);
mcimadamore@775 736 Type mt2 = types.memberType(site, m2);
duke@1 737 if (!types.overrideEquivalent(mt1, mt2))
mcimadamore@844 738 return ambiguityError(m1, m2);
mcimadamore@844 739
duke@1 740 // same signature; select (a) the non-bridge method, or
duke@1 741 // (b) the one that overrides the other, or (c) the concrete
duke@1 742 // one, or (d) merge both abstract signatures
mcimadamore@844 743 if ((m1.flags() & BRIDGE) != (m2.flags() & BRIDGE))
duke@1 744 return ((m1.flags() & BRIDGE) != 0) ? m2 : m1;
mcimadamore@844 745
duke@1 746 // if one overrides or hides the other, use it
duke@1 747 TypeSymbol m1Owner = (TypeSymbol)m1.owner;
duke@1 748 TypeSymbol m2Owner = (TypeSymbol)m2.owner;
duke@1 749 if (types.asSuper(m1Owner.type, m2Owner) != null &&
duke@1 750 ((m1.owner.flags_field & INTERFACE) == 0 ||
duke@1 751 (m2.owner.flags_field & INTERFACE) != 0) &&
duke@1 752 m1.overrides(m2, m1Owner, types, false))
duke@1 753 return m1;
duke@1 754 if (types.asSuper(m2Owner.type, m1Owner) != null &&
duke@1 755 ((m2.owner.flags_field & INTERFACE) == 0 ||
duke@1 756 (m1.owner.flags_field & INTERFACE) != 0) &&
duke@1 757 m2.overrides(m1, m2Owner, types, false))
duke@1 758 return m2;
duke@1 759 boolean m1Abstract = (m1.flags() & ABSTRACT) != 0;
duke@1 760 boolean m2Abstract = (m2.flags() & ABSTRACT) != 0;
duke@1 761 if (m1Abstract && !m2Abstract) return m2;
duke@1 762 if (m2Abstract && !m1Abstract) return m1;
duke@1 763 // both abstract or both concrete
duke@1 764 if (!m1Abstract && !m2Abstract)
mcimadamore@844 765 return ambiguityError(m1, m2);
mcimadamore@156 766 // check that both signatures have the same erasure
mcimadamore@156 767 if (!types.isSameTypes(m1.erasure(types).getParameterTypes(),
mcimadamore@156 768 m2.erasure(types).getParameterTypes()))
mcimadamore@844 769 return ambiguityError(m1, m2);
duke@1 770 // both abstract, neither overridden; merge throws clause and result type
mcimadamore@1059 771 Type mst = mostSpecificReturnType(mt1, mt2);
mcimadamore@1059 772 if (mst == null) {
duke@1 773 // Theoretically, this can't happen, but it is possible
duke@1 774 // due to error recovery or mixing incompatible class files
mcimadamore@844 775 return ambiguityError(m1, m2);
duke@1 776 }
mcimadamore@1059 777 Symbol mostSpecific = mst == mt1 ? m1 : m2;
dlsmith@880 778 List<Type> allThrown = chk.intersect(mt1.getThrownTypes(), mt2.getThrownTypes());
dlsmith@880 779 Type newSig = types.createMethodTypeWithThrown(mostSpecific.type, allThrown);
mcimadamore@254 780 MethodSymbol result = new MethodSymbol(
mcimadamore@254 781 mostSpecific.flags(),
mcimadamore@254 782 mostSpecific.name,
dlsmith@880 783 newSig,
mcimadamore@254 784 mostSpecific.owner) {
mcimadamore@254 785 @Override
mcimadamore@254 786 public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult) {
mcimadamore@254 787 if (origin == site.tsym)
mcimadamore@254 788 return this;
mcimadamore@254 789 else
mcimadamore@254 790 return super.implementation(origin, types, checkResult);
mcimadamore@254 791 }
mcimadamore@254 792 };
duke@1 793 return result;
duke@1 794 }
duke@1 795 if (m1SignatureMoreSpecific) return m1;
duke@1 796 if (m2SignatureMoreSpecific) return m2;
mcimadamore@844 797 return ambiguityError(m1, m2);
duke@1 798 case AMBIGUOUS:
duke@1 799 AmbiguityError e = (AmbiguityError)m2;
mcimadamore@302 800 Symbol err1 = mostSpecific(m1, e.sym, env, site, allowBoxing, useVarargs);
duke@1 801 Symbol err2 = mostSpecific(m1, e.sym2, env, site, allowBoxing, useVarargs);
duke@1 802 if (err1 == err2) return err1;
mcimadamore@302 803 if (err1 == e.sym && err2 == e.sym2) return m2;
duke@1 804 if (err1 instanceof AmbiguityError &&
duke@1 805 err2 instanceof AmbiguityError &&
mcimadamore@302 806 ((AmbiguityError)err1).sym == ((AmbiguityError)err2).sym)
mcimadamore@844 807 return ambiguityError(m1, m2);
duke@1 808 else
mcimadamore@844 809 return ambiguityError(err1, err2);
duke@1 810 default:
duke@1 811 throw new AssertionError();
duke@1 812 }
duke@1 813 }
mcimadamore@775 814 //where
mcimadamore@775 815 private boolean signatureMoreSpecific(Env<AttrContext> env, Type site, Symbol m1, Symbol m2, boolean allowBoxing, boolean useVarargs) {
mcimadamore@795 816 noteWarner.clear();
mcimadamore@775 817 Type mtype1 = types.memberType(site, adjustVarargs(m1, m2, useVarargs));
mcimadamore@1006 818 Type mtype2 = instantiate(env, site, adjustVarargs(m2, m1, useVarargs),
mcimadamore@1006 819 types.lowerBoundArgtypes(mtype1), null,
mcimadamore@1006 820 allowBoxing, false, noteWarner);
mcimadamore@1006 821 return mtype2 != null &&
mcimadamore@795 822 !noteWarner.hasLint(Lint.LintCategory.UNCHECKED);
mcimadamore@775 823 }
mcimadamore@775 824 //where
mcimadamore@775 825 private Symbol adjustVarargs(Symbol to, Symbol from, boolean useVarargs) {
mcimadamore@775 826 List<Type> fromArgs = from.type.getParameterTypes();
mcimadamore@775 827 List<Type> toArgs = to.type.getParameterTypes();
mcimadamore@775 828 if (useVarargs &&
mcimadamore@775 829 (from.flags() & VARARGS) != 0 &&
mcimadamore@775 830 (to.flags() & VARARGS) != 0) {
mcimadamore@775 831 Type varargsTypeFrom = fromArgs.last();
mcimadamore@775 832 Type varargsTypeTo = toArgs.last();
mcimadamore@787 833 ListBuffer<Type> args = ListBuffer.lb();
mcimadamore@787 834 if (toArgs.length() < fromArgs.length()) {
mcimadamore@787 835 //if we are checking a varargs method 'from' against another varargs
mcimadamore@787 836 //method 'to' (where arity of 'to' < arity of 'from') then expand signature
mcimadamore@787 837 //of 'to' to 'fit' arity of 'from' (this means adding fake formals to 'to'
mcimadamore@787 838 //until 'to' signature has the same arity as 'from')
mcimadamore@787 839 while (fromArgs.head != varargsTypeFrom) {
mcimadamore@787 840 args.append(toArgs.head == varargsTypeTo ? types.elemtype(varargsTypeTo) : toArgs.head);
mcimadamore@787 841 fromArgs = fromArgs.tail;
mcimadamore@787 842 toArgs = toArgs.head == varargsTypeTo ?
mcimadamore@787 843 toArgs :
mcimadamore@787 844 toArgs.tail;
mcimadamore@787 845 }
mcimadamore@787 846 } else {
mcimadamore@787 847 //formal argument list is same as original list where last
mcimadamore@787 848 //argument (array type) is removed
mcimadamore@787 849 args.appendList(toArgs.reverse().tail.reverse());
mcimadamore@775 850 }
mcimadamore@787 851 //append varargs element type as last synthetic formal
mcimadamore@787 852 args.append(types.elemtype(varargsTypeTo));
dlsmith@880 853 Type mtype = types.createMethodTypeWithParameters(to.type, args.toList());
mcimadamore@1006 854 return new MethodSymbol(to.flags_field & ~VARARGS, to.name, mtype, to.owner);
mcimadamore@775 855 } else {
mcimadamore@775 856 return to;
mcimadamore@775 857 }
mcimadamore@775 858 }
mcimadamore@844 859 //where
mcimadamore@1059 860 Type mostSpecificReturnType(Type mt1, Type mt2) {
mcimadamore@1059 861 Type rt1 = mt1.getReturnType();
mcimadamore@1059 862 Type rt2 = mt2.getReturnType();
mcimadamore@1059 863
mcimadamore@1059 864 if (mt1.tag == FORALL && mt2.tag == FORALL) {
mcimadamore@1059 865 //if both are generic methods, adjust return type ahead of subtyping check
mcimadamore@1059 866 rt1 = types.subst(rt1, mt1.getTypeArguments(), mt2.getTypeArguments());
mcimadamore@1059 867 }
mcimadamore@1059 868 //first use subtyping, then return type substitutability
mcimadamore@1059 869 if (types.isSubtype(rt1, rt2)) {
mcimadamore@1059 870 return mt1;
mcimadamore@1059 871 } else if (types.isSubtype(rt2, rt1)) {
mcimadamore@1059 872 return mt2;
mcimadamore@1059 873 } else if (types.returnTypeSubstitutable(mt1, mt2)) {
mcimadamore@1059 874 return mt1;
mcimadamore@1059 875 } else if (types.returnTypeSubstitutable(mt2, mt1)) {
mcimadamore@1059 876 return mt2;
mcimadamore@1059 877 } else {
mcimadamore@1059 878 return null;
mcimadamore@1059 879 }
mcimadamore@1059 880 }
mcimadamore@1059 881 //where
mcimadamore@844 882 Symbol ambiguityError(Symbol m1, Symbol m2) {
mcimadamore@844 883 if (((m1.flags() | m2.flags()) & CLASH) != 0) {
mcimadamore@844 884 return (m1.flags() & CLASH) == 0 ? m1 : m2;
mcimadamore@844 885 } else {
mcimadamore@844 886 return new AmbiguityError(m1, m2);
mcimadamore@844 887 }
mcimadamore@844 888 }
duke@1 889
duke@1 890 /** Find best qualified method matching given name, type and value
duke@1 891 * arguments.
duke@1 892 * @param env The current environment.
duke@1 893 * @param site The original type from where the selection
duke@1 894 * takes place.
duke@1 895 * @param name The method's name.
duke@1 896 * @param argtypes The method's value arguments.
duke@1 897 * @param typeargtypes The method's type arguments
duke@1 898 * @param allowBoxing Allow boxing conversions of arguments.
duke@1 899 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 900 */
duke@1 901 Symbol findMethod(Env<AttrContext> env,
duke@1 902 Type site,
duke@1 903 Name name,
duke@1 904 List<Type> argtypes,
duke@1 905 List<Type> typeargtypes,
duke@1 906 boolean allowBoxing,
duke@1 907 boolean useVarargs,
duke@1 908 boolean operator) {
jrose@571 909 Symbol bestSoFar = methodNotFound;
duke@1 910 return findMethod(env,
duke@1 911 site,
duke@1 912 name,
duke@1 913 argtypes,
duke@1 914 typeargtypes,
duke@1 915 site.tsym.type,
duke@1 916 true,
jrose@571 917 bestSoFar,
duke@1 918 allowBoxing,
duke@1 919 useVarargs,
mcimadamore@913 920 operator,
mcimadamore@913 921 new HashSet<TypeSymbol>());
duke@1 922 }
duke@1 923 // where
duke@1 924 private Symbol findMethod(Env<AttrContext> env,
duke@1 925 Type site,
duke@1 926 Name name,
duke@1 927 List<Type> argtypes,
duke@1 928 List<Type> typeargtypes,
duke@1 929 Type intype,
duke@1 930 boolean abstractok,
duke@1 931 Symbol bestSoFar,
duke@1 932 boolean allowBoxing,
duke@1 933 boolean useVarargs,
mcimadamore@913 934 boolean operator,
mcimadamore@913 935 Set<TypeSymbol> seen) {
mcimadamore@19 936 for (Type ct = intype; ct.tag == CLASS || ct.tag == TYPEVAR; ct = types.supertype(ct)) {
mcimadamore@19 937 while (ct.tag == TYPEVAR)
mcimadamore@19 938 ct = ct.getUpperBound();
duke@1 939 ClassSymbol c = (ClassSymbol)ct.tsym;
mcimadamore@913 940 if (!seen.add(c)) return bestSoFar;
mcimadamore@135 941 if ((c.flags() & (ABSTRACT | INTERFACE | ENUM)) == 0)
duke@1 942 abstractok = false;
duke@1 943 for (Scope.Entry e = c.members().lookup(name);
duke@1 944 e.scope != null;
duke@1 945 e = e.next()) {
duke@1 946 //- System.out.println(" e " + e.sym);
duke@1 947 if (e.sym.kind == MTH &&
duke@1 948 (e.sym.flags_field & SYNTHETIC) == 0) {
duke@1 949 bestSoFar = selectBest(env, site, argtypes, typeargtypes,
duke@1 950 e.sym, bestSoFar,
duke@1 951 allowBoxing,
duke@1 952 useVarargs,
duke@1 953 operator);
duke@1 954 }
duke@1 955 }
mcimadamore@537 956 if (name == names.init)
mcimadamore@537 957 break;
duke@1 958 //- System.out.println(" - " + bestSoFar);
duke@1 959 if (abstractok) {
duke@1 960 Symbol concrete = methodNotFound;
duke@1 961 if ((bestSoFar.flags() & ABSTRACT) == 0)
duke@1 962 concrete = bestSoFar;
duke@1 963 for (List<Type> l = types.interfaces(c.type);
duke@1 964 l.nonEmpty();
duke@1 965 l = l.tail) {
duke@1 966 bestSoFar = findMethod(env, site, name, argtypes,
duke@1 967 typeargtypes,
duke@1 968 l.head, abstractok, bestSoFar,
mcimadamore@913 969 allowBoxing, useVarargs, operator, seen);
duke@1 970 }
duke@1 971 if (concrete != bestSoFar &&
duke@1 972 concrete.kind < ERR && bestSoFar.kind < ERR &&
duke@1 973 types.isSubSignature(concrete.type, bestSoFar.type))
duke@1 974 bestSoFar = concrete;
duke@1 975 }
duke@1 976 }
duke@1 977 return bestSoFar;
duke@1 978 }
duke@1 979
duke@1 980 /** Find unqualified method matching given name, type and value arguments.
duke@1 981 * @param env The current environment.
duke@1 982 * @param name The method's name.
duke@1 983 * @param argtypes The method's value arguments.
duke@1 984 * @param typeargtypes The method's type arguments.
duke@1 985 * @param allowBoxing Allow boxing conversions of arguments.
duke@1 986 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 987 */
duke@1 988 Symbol findFun(Env<AttrContext> env, Name name,
duke@1 989 List<Type> argtypes, List<Type> typeargtypes,
duke@1 990 boolean allowBoxing, boolean useVarargs) {
duke@1 991 Symbol bestSoFar = methodNotFound;
duke@1 992 Symbol sym;
duke@1 993 Env<AttrContext> env1 = env;
duke@1 994 boolean staticOnly = false;
duke@1 995 while (env1.outer != null) {
duke@1 996 if (isStatic(env1)) staticOnly = true;
duke@1 997 sym = findMethod(
duke@1 998 env1, env1.enclClass.sym.type, name, argtypes, typeargtypes,
duke@1 999 allowBoxing, useVarargs, false);
duke@1 1000 if (sym.exists()) {
duke@1 1001 if (staticOnly &&
duke@1 1002 sym.kind == MTH &&
duke@1 1003 sym.owner.kind == TYP &&
duke@1 1004 (sym.flags() & STATIC) == 0) return new StaticError(sym);
duke@1 1005 else return sym;
duke@1 1006 } else if (sym.kind < bestSoFar.kind) {
duke@1 1007 bestSoFar = sym;
duke@1 1008 }
duke@1 1009 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
duke@1 1010 env1 = env1.outer;
duke@1 1011 }
duke@1 1012
duke@1 1013 sym = findMethod(env, syms.predefClass.type, name, argtypes,
duke@1 1014 typeargtypes, allowBoxing, useVarargs, false);
duke@1 1015 if (sym.exists())
duke@1 1016 return sym;
duke@1 1017
duke@1 1018 Scope.Entry e = env.toplevel.namedImportScope.lookup(name);
duke@1 1019 for (; e.scope != null; e = e.next()) {
duke@1 1020 sym = e.sym;
duke@1 1021 Type origin = e.getOrigin().owner.type;
duke@1 1022 if (sym.kind == MTH) {
duke@1 1023 if (e.sym.owner.type != origin)
duke@1 1024 sym = sym.clone(e.getOrigin().owner);
duke@1 1025 if (!isAccessible(env, origin, sym))
duke@1 1026 sym = new AccessError(env, origin, sym);
duke@1 1027 bestSoFar = selectBest(env, origin,
duke@1 1028 argtypes, typeargtypes,
duke@1 1029 sym, bestSoFar,
duke@1 1030 allowBoxing, useVarargs, false);
duke@1 1031 }
duke@1 1032 }
duke@1 1033 if (bestSoFar.exists())
duke@1 1034 return bestSoFar;
duke@1 1035
duke@1 1036 e = env.toplevel.starImportScope.lookup(name);
duke@1 1037 for (; e.scope != null; e = e.next()) {
duke@1 1038 sym = e.sym;
duke@1 1039 Type origin = e.getOrigin().owner.type;
duke@1 1040 if (sym.kind == MTH) {
duke@1 1041 if (e.sym.owner.type != origin)
duke@1 1042 sym = sym.clone(e.getOrigin().owner);
duke@1 1043 if (!isAccessible(env, origin, sym))
duke@1 1044 sym = new AccessError(env, origin, sym);
duke@1 1045 bestSoFar = selectBest(env, origin,
duke@1 1046 argtypes, typeargtypes,
duke@1 1047 sym, bestSoFar,
duke@1 1048 allowBoxing, useVarargs, false);
duke@1 1049 }
duke@1 1050 }
duke@1 1051 return bestSoFar;
duke@1 1052 }
duke@1 1053
duke@1 1054 /** Load toplevel or member class with given fully qualified name and
duke@1 1055 * verify that it is accessible.
duke@1 1056 * @param env The current environment.
duke@1 1057 * @param name The fully qualified name of the class to be loaded.
duke@1 1058 */
duke@1 1059 Symbol loadClass(Env<AttrContext> env, Name name) {
duke@1 1060 try {
duke@1 1061 ClassSymbol c = reader.loadClass(name);
duke@1 1062 return isAccessible(env, c) ? c : new AccessError(c);
duke@1 1063 } catch (ClassReader.BadClassFile err) {
duke@1 1064 throw err;
duke@1 1065 } catch (CompletionFailure ex) {
duke@1 1066 return typeNotFound;
duke@1 1067 }
duke@1 1068 }
duke@1 1069
duke@1 1070 /** Find qualified member type.
duke@1 1071 * @param env The current environment.
duke@1 1072 * @param site The original type from where the selection takes
duke@1 1073 * place.
duke@1 1074 * @param name The type's name.
duke@1 1075 * @param c The class to search for the member type. This is
duke@1 1076 * always a superclass or implemented interface of
duke@1 1077 * site's class.
duke@1 1078 */
duke@1 1079 Symbol findMemberType(Env<AttrContext> env,
duke@1 1080 Type site,
duke@1 1081 Name name,
duke@1 1082 TypeSymbol c) {
duke@1 1083 Symbol bestSoFar = typeNotFound;
duke@1 1084 Symbol sym;
duke@1 1085 Scope.Entry e = c.members().lookup(name);
duke@1 1086 while (e.scope != null) {
duke@1 1087 if (e.sym.kind == TYP) {
duke@1 1088 return isAccessible(env, site, e.sym)
duke@1 1089 ? e.sym
duke@1 1090 : new AccessError(env, site, e.sym);
duke@1 1091 }
duke@1 1092 e = e.next();
duke@1 1093 }
duke@1 1094 Type st = types.supertype(c.type);
duke@1 1095 if (st != null && st.tag == CLASS) {
duke@1 1096 sym = findMemberType(env, site, name, st.tsym);
duke@1 1097 if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1098 }
duke@1 1099 for (List<Type> l = types.interfaces(c.type);
duke@1 1100 bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
duke@1 1101 l = l.tail) {
duke@1 1102 sym = findMemberType(env, site, name, l.head.tsym);
duke@1 1103 if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS &&
duke@1 1104 sym.owner != bestSoFar.owner)
duke@1 1105 bestSoFar = new AmbiguityError(bestSoFar, sym);
duke@1 1106 else if (sym.kind < bestSoFar.kind)
duke@1 1107 bestSoFar = sym;
duke@1 1108 }
duke@1 1109 return bestSoFar;
duke@1 1110 }
duke@1 1111
duke@1 1112 /** Find a global type in given scope and load corresponding class.
duke@1 1113 * @param env The current environment.
duke@1 1114 * @param scope The scope in which to look for the type.
duke@1 1115 * @param name The type's name.
duke@1 1116 */
duke@1 1117 Symbol findGlobalType(Env<AttrContext> env, Scope scope, Name name) {
duke@1 1118 Symbol bestSoFar = typeNotFound;
duke@1 1119 for (Scope.Entry e = scope.lookup(name); e.scope != null; e = e.next()) {
duke@1 1120 Symbol sym = loadClass(env, e.sym.flatName());
duke@1 1121 if (bestSoFar.kind == TYP && sym.kind == TYP &&
duke@1 1122 bestSoFar != sym)
duke@1 1123 return new AmbiguityError(bestSoFar, sym);
duke@1 1124 else if (sym.kind < bestSoFar.kind)
duke@1 1125 bestSoFar = sym;
duke@1 1126 }
duke@1 1127 return bestSoFar;
duke@1 1128 }
duke@1 1129
duke@1 1130 /** Find an unqualified type symbol.
duke@1 1131 * @param env The current environment.
duke@1 1132 * @param name The type's name.
duke@1 1133 */
duke@1 1134 Symbol findType(Env<AttrContext> env, Name name) {
duke@1 1135 Symbol bestSoFar = typeNotFound;
duke@1 1136 Symbol sym;
duke@1 1137 boolean staticOnly = false;
duke@1 1138 for (Env<AttrContext> env1 = env; env1.outer != null; env1 = env1.outer) {
duke@1 1139 if (isStatic(env1)) staticOnly = true;
duke@1 1140 for (Scope.Entry e = env1.info.scope.lookup(name);
duke@1 1141 e.scope != null;
duke@1 1142 e = e.next()) {
duke@1 1143 if (e.sym.kind == TYP) {
duke@1 1144 if (staticOnly &&
duke@1 1145 e.sym.type.tag == TYPEVAR &&
duke@1 1146 e.sym.owner.kind == TYP) return new StaticError(e.sym);
duke@1 1147 return e.sym;
duke@1 1148 }
duke@1 1149 }
duke@1 1150
duke@1 1151 sym = findMemberType(env1, env1.enclClass.sym.type, name,
duke@1 1152 env1.enclClass.sym);
duke@1 1153 if (staticOnly && sym.kind == TYP &&
duke@1 1154 sym.type.tag == CLASS &&
duke@1 1155 sym.type.getEnclosingType().tag == CLASS &&
duke@1 1156 env1.enclClass.sym.type.isParameterized() &&
duke@1 1157 sym.type.getEnclosingType().isParameterized())
duke@1 1158 return new StaticError(sym);
duke@1 1159 else if (sym.exists()) return sym;
duke@1 1160 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1161
duke@1 1162 JCClassDecl encl = env1.baseClause ? (JCClassDecl)env1.tree : env1.enclClass;
duke@1 1163 if ((encl.sym.flags() & STATIC) != 0)
duke@1 1164 staticOnly = true;
duke@1 1165 }
duke@1 1166
duke@1 1167 if (env.tree.getTag() != JCTree.IMPORT) {
duke@1 1168 sym = findGlobalType(env, env.toplevel.namedImportScope, name);
duke@1 1169 if (sym.exists()) return sym;
duke@1 1170 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1171
duke@1 1172 sym = findGlobalType(env, env.toplevel.packge.members(), name);
duke@1 1173 if (sym.exists()) return sym;
duke@1 1174 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1175
duke@1 1176 sym = findGlobalType(env, env.toplevel.starImportScope, name);
duke@1 1177 if (sym.exists()) return sym;
duke@1 1178 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1179 }
duke@1 1180
duke@1 1181 return bestSoFar;
duke@1 1182 }
duke@1 1183
duke@1 1184 /** Find an unqualified identifier which matches a specified kind set.
duke@1 1185 * @param env The current environment.
duke@1 1186 * @param name The indentifier's name.
duke@1 1187 * @param kind Indicates the possible symbol kinds
duke@1 1188 * (a subset of VAL, TYP, PCK).
duke@1 1189 */
duke@1 1190 Symbol findIdent(Env<AttrContext> env, Name name, int kind) {
duke@1 1191 Symbol bestSoFar = typeNotFound;
duke@1 1192 Symbol sym;
duke@1 1193
duke@1 1194 if ((kind & VAR) != 0) {
duke@1 1195 sym = findVar(env, name);
duke@1 1196 if (sym.exists()) return sym;
duke@1 1197 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1198 }
duke@1 1199
duke@1 1200 if ((kind & TYP) != 0) {
duke@1 1201 sym = findType(env, name);
duke@1 1202 if (sym.exists()) return sym;
duke@1 1203 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1204 }
duke@1 1205
duke@1 1206 if ((kind & PCK) != 0) return reader.enterPackage(name);
duke@1 1207 else return bestSoFar;
duke@1 1208 }
duke@1 1209
duke@1 1210 /** Find an identifier in a package which matches a specified kind set.
duke@1 1211 * @param env The current environment.
duke@1 1212 * @param name The identifier's name.
duke@1 1213 * @param kind Indicates the possible symbol kinds
duke@1 1214 * (a nonempty subset of TYP, PCK).
duke@1 1215 */
duke@1 1216 Symbol findIdentInPackage(Env<AttrContext> env, TypeSymbol pck,
duke@1 1217 Name name, int kind) {
duke@1 1218 Name fullname = TypeSymbol.formFullName(name, pck);
duke@1 1219 Symbol bestSoFar = typeNotFound;
duke@1 1220 PackageSymbol pack = null;
duke@1 1221 if ((kind & PCK) != 0) {
duke@1 1222 pack = reader.enterPackage(fullname);
duke@1 1223 if (pack.exists()) return pack;
duke@1 1224 }
duke@1 1225 if ((kind & TYP) != 0) {
duke@1 1226 Symbol sym = loadClass(env, fullname);
duke@1 1227 if (sym.exists()) {
duke@1 1228 // don't allow programs to use flatnames
duke@1 1229 if (name == sym.name) return sym;
duke@1 1230 }
duke@1 1231 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1232 }
duke@1 1233 return (pack != null) ? pack : bestSoFar;
duke@1 1234 }
duke@1 1235
duke@1 1236 /** Find an identifier among the members of a given type `site'.
duke@1 1237 * @param env The current environment.
duke@1 1238 * @param site The type containing the symbol to be found.
duke@1 1239 * @param name The identifier's name.
duke@1 1240 * @param kind Indicates the possible symbol kinds
duke@1 1241 * (a subset of VAL, TYP).
duke@1 1242 */
duke@1 1243 Symbol findIdentInType(Env<AttrContext> env, Type site,
duke@1 1244 Name name, int kind) {
duke@1 1245 Symbol bestSoFar = typeNotFound;
duke@1 1246 Symbol sym;
duke@1 1247 if ((kind & VAR) != 0) {
duke@1 1248 sym = findField(env, site, name, site.tsym);
duke@1 1249 if (sym.exists()) return sym;
duke@1 1250 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1251 }
duke@1 1252
duke@1 1253 if ((kind & TYP) != 0) {
duke@1 1254 sym = findMemberType(env, site, name, site.tsym);
duke@1 1255 if (sym.exists()) return sym;
duke@1 1256 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1257 }
duke@1 1258 return bestSoFar;
duke@1 1259 }
duke@1 1260
duke@1 1261 /* ***************************************************************************
duke@1 1262 * Access checking
duke@1 1263 * The following methods convert ResolveErrors to ErrorSymbols, issuing
duke@1 1264 * an error message in the process
duke@1 1265 ****************************************************************************/
duke@1 1266
duke@1 1267 /** If `sym' is a bad symbol: report error and return errSymbol
duke@1 1268 * else pass through unchanged,
duke@1 1269 * additional arguments duplicate what has been used in trying to find the
duke@1 1270 * symbol (--> flyweight pattern). This improves performance since we
duke@1 1271 * expect misses to happen frequently.
duke@1 1272 *
duke@1 1273 * @param sym The symbol that was found, or a ResolveError.
duke@1 1274 * @param pos The position to use for error reporting.
duke@1 1275 * @param site The original type from where the selection took place.
duke@1 1276 * @param name The symbol's name.
duke@1 1277 * @param argtypes The invocation's value arguments,
duke@1 1278 * if we looked for a method.
duke@1 1279 * @param typeargtypes The invocation's type arguments,
duke@1 1280 * if we looked for a method.
duke@1 1281 */
duke@1 1282 Symbol access(Symbol sym,
duke@1 1283 DiagnosticPosition pos,
mcimadamore@829 1284 Symbol location,
duke@1 1285 Type site,
duke@1 1286 Name name,
duke@1 1287 boolean qualified,
duke@1 1288 List<Type> argtypes,
duke@1 1289 List<Type> typeargtypes) {
duke@1 1290 if (sym.kind >= AMBIGUOUS) {
mcimadamore@302 1291 ResolveError errSym = (ResolveError)sym;
duke@1 1292 if (!site.isErroneous() &&
duke@1 1293 !Type.isErroneous(argtypes) &&
duke@1 1294 (typeargtypes==null || !Type.isErroneous(typeargtypes)))
mcimadamore@829 1295 logResolveError(errSym, pos, location, site, name, argtypes, typeargtypes);
mcimadamore@302 1296 sym = errSym.access(name, qualified ? site.tsym : syms.noSymbol);
duke@1 1297 }
duke@1 1298 return sym;
duke@1 1299 }
duke@1 1300
mcimadamore@829 1301 /** Same as original access(), but without location.
mcimadamore@829 1302 */
mcimadamore@829 1303 Symbol access(Symbol sym,
mcimadamore@829 1304 DiagnosticPosition pos,
mcimadamore@829 1305 Type site,
mcimadamore@829 1306 Name name,
mcimadamore@829 1307 boolean qualified,
mcimadamore@829 1308 List<Type> argtypes,
mcimadamore@829 1309 List<Type> typeargtypes) {
mcimadamore@829 1310 return access(sym, pos, site.tsym, site, name, qualified, argtypes, typeargtypes);
mcimadamore@829 1311 }
mcimadamore@829 1312
mcimadamore@829 1313 /** Same as original access(), but without type arguments and arguments.
mcimadamore@829 1314 */
mcimadamore@829 1315 Symbol access(Symbol sym,
mcimadamore@829 1316 DiagnosticPosition pos,
mcimadamore@829 1317 Symbol location,
mcimadamore@829 1318 Type site,
mcimadamore@829 1319 Name name,
mcimadamore@829 1320 boolean qualified) {
mcimadamore@829 1321 if (sym.kind >= AMBIGUOUS)
mcimadamore@829 1322 return access(sym, pos, location, site, name, qualified, List.<Type>nil(), null);
mcimadamore@829 1323 else
mcimadamore@829 1324 return sym;
mcimadamore@829 1325 }
mcimadamore@829 1326
mcimadamore@829 1327 /** Same as original access(), but without location, type arguments and arguments.
duke@1 1328 */
duke@1 1329 Symbol access(Symbol sym,
duke@1 1330 DiagnosticPosition pos,
duke@1 1331 Type site,
duke@1 1332 Name name,
duke@1 1333 boolean qualified) {
mcimadamore@829 1334 return access(sym, pos, site.tsym, site, name, qualified);
duke@1 1335 }
duke@1 1336
duke@1 1337 /** Check that sym is not an abstract method.
duke@1 1338 */
duke@1 1339 void checkNonAbstract(DiagnosticPosition pos, Symbol sym) {
duke@1 1340 if ((sym.flags() & ABSTRACT) != 0)
duke@1 1341 log.error(pos, "abstract.cant.be.accessed.directly",
duke@1 1342 kindName(sym), sym, sym.location());
duke@1 1343 }
duke@1 1344
duke@1 1345 /* ***************************************************************************
duke@1 1346 * Debugging
duke@1 1347 ****************************************************************************/
duke@1 1348
duke@1 1349 /** print all scopes starting with scope s and proceeding outwards.
duke@1 1350 * used for debugging.
duke@1 1351 */
duke@1 1352 public void printscopes(Scope s) {
duke@1 1353 while (s != null) {
duke@1 1354 if (s.owner != null)
duke@1 1355 System.err.print(s.owner + ": ");
duke@1 1356 for (Scope.Entry e = s.elems; e != null; e = e.sibling) {
duke@1 1357 if ((e.sym.flags() & ABSTRACT) != 0)
duke@1 1358 System.err.print("abstract ");
duke@1 1359 System.err.print(e.sym + " ");
duke@1 1360 }
duke@1 1361 System.err.println();
duke@1 1362 s = s.next;
duke@1 1363 }
duke@1 1364 }
duke@1 1365
duke@1 1366 void printscopes(Env<AttrContext> env) {
duke@1 1367 while (env.outer != null) {
duke@1 1368 System.err.println("------------------------------");
duke@1 1369 printscopes(env.info.scope);
duke@1 1370 env = env.outer;
duke@1 1371 }
duke@1 1372 }
duke@1 1373
duke@1 1374 public void printscopes(Type t) {
duke@1 1375 while (t.tag == CLASS) {
duke@1 1376 printscopes(t.tsym.members());
duke@1 1377 t = types.supertype(t);
duke@1 1378 }
duke@1 1379 }
duke@1 1380
duke@1 1381 /* ***************************************************************************
duke@1 1382 * Name resolution
duke@1 1383 * Naming conventions are as for symbol lookup
duke@1 1384 * Unlike the find... methods these methods will report access errors
duke@1 1385 ****************************************************************************/
duke@1 1386
duke@1 1387 /** Resolve an unqualified (non-method) identifier.
duke@1 1388 * @param pos The position to use for error reporting.
duke@1 1389 * @param env The environment current at the identifier use.
duke@1 1390 * @param name The identifier's name.
duke@1 1391 * @param kind The set of admissible symbol kinds for the identifier.
duke@1 1392 */
duke@1 1393 Symbol resolveIdent(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 1394 Name name, int kind) {
duke@1 1395 return access(
duke@1 1396 findIdent(env, name, kind),
duke@1 1397 pos, env.enclClass.sym.type, name, false);
duke@1 1398 }
duke@1 1399
duke@1 1400 /** Resolve an unqualified method identifier.
duke@1 1401 * @param pos The position to use for error reporting.
duke@1 1402 * @param env The environment current at the method invocation.
duke@1 1403 * @param name The identifier's name.
duke@1 1404 * @param argtypes The types of the invocation's value arguments.
duke@1 1405 * @param typeargtypes The types of the invocation's type arguments.
duke@1 1406 */
duke@1 1407 Symbol resolveMethod(DiagnosticPosition pos,
duke@1 1408 Env<AttrContext> env,
duke@1 1409 Name name,
duke@1 1410 List<Type> argtypes,
duke@1 1411 List<Type> typeargtypes) {
mcimadamore@689 1412 Symbol sym = startResolution();
mcimadamore@160 1413 List<MethodResolutionPhase> steps = methodResolutionSteps;
mcimadamore@160 1414 while (steps.nonEmpty() &&
mcimadamore@160 1415 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@160 1416 sym.kind >= ERRONEOUS) {
mcimadamore@689 1417 currentStep = steps.head;
mcimadamore@160 1418 sym = findFun(env, name, argtypes, typeargtypes,
mcimadamore@160 1419 steps.head.isBoxingRequired,
mcimadamore@160 1420 env.info.varArgs = steps.head.isVarargsRequired);
mcimadamore@160 1421 methodResolutionCache.put(steps.head, sym);
mcimadamore@160 1422 steps = steps.tail;
duke@1 1423 }
mcimadamore@160 1424 if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error
mcimadamore@160 1425 MethodResolutionPhase errPhase =
mcimadamore@160 1426 firstErroneousResolutionPhase();
mcimadamore@160 1427 sym = access(methodResolutionCache.get(errPhase),
mcimadamore@160 1428 pos, env.enclClass.sym.type, name, false, argtypes, typeargtypes);
mcimadamore@160 1429 env.info.varArgs = errPhase.isVarargsRequired;
duke@1 1430 }
duke@1 1431 return sym;
duke@1 1432 }
duke@1 1433
mcimadamore@689 1434 private Symbol startResolution() {
mcimadamore@689 1435 wrongMethod.clear();
mcimadamore@689 1436 wrongMethods.clear();
mcimadamore@689 1437 return methodNotFound;
mcimadamore@689 1438 }
mcimadamore@689 1439
duke@1 1440 /** Resolve a qualified method identifier
duke@1 1441 * @param pos The position to use for error reporting.
duke@1 1442 * @param env The environment current at the method invocation.
duke@1 1443 * @param site The type of the qualifying expression, in which
duke@1 1444 * identifier is searched.
duke@1 1445 * @param name The identifier's name.
duke@1 1446 * @param argtypes The types of the invocation's value arguments.
duke@1 1447 * @param typeargtypes The types of the invocation's type arguments.
duke@1 1448 */
duke@1 1449 Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 1450 Type site, Name name, List<Type> argtypes,
duke@1 1451 List<Type> typeargtypes) {
mcimadamore@829 1452 return resolveQualifiedMethod(pos, env, site.tsym, site, name, argtypes, typeargtypes);
mcimadamore@829 1453 }
mcimadamore@829 1454 Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env,
mcimadamore@829 1455 Symbol location, Type site, Name name, List<Type> argtypes,
mcimadamore@829 1456 List<Type> typeargtypes) {
mcimadamore@689 1457 Symbol sym = startResolution();
mcimadamore@160 1458 List<MethodResolutionPhase> steps = methodResolutionSteps;
mcimadamore@160 1459 while (steps.nonEmpty() &&
mcimadamore@160 1460 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@160 1461 sym.kind >= ERRONEOUS) {
mcimadamore@689 1462 currentStep = steps.head;
mcimadamore@160 1463 sym = findMethod(env, site, name, argtypes, typeargtypes,
mcimadamore@160 1464 steps.head.isBoxingRequired(),
mcimadamore@160 1465 env.info.varArgs = steps.head.isVarargsRequired(), false);
mcimadamore@160 1466 methodResolutionCache.put(steps.head, sym);
mcimadamore@160 1467 steps = steps.tail;
duke@1 1468 }
mcimadamore@674 1469 if (sym.kind >= AMBIGUOUS) {
mcimadamore@820 1470 if (site.tsym.isPolymorphicSignatureGeneric()) {
mcimadamore@674 1471 //polymorphic receiver - synthesize new method symbol
mcimadamore@674 1472 env.info.varArgs = false;
mcimadamore@674 1473 sym = findPolymorphicSignatureInstance(env,
mcimadamore@820 1474 site, name, null, argtypes);
mcimadamore@674 1475 }
mcimadamore@674 1476 else {
mcimadamore@674 1477 //if nothing is found return the 'first' error
mcimadamore@674 1478 MethodResolutionPhase errPhase =
mcimadamore@674 1479 firstErroneousResolutionPhase();
mcimadamore@674 1480 sym = access(methodResolutionCache.get(errPhase),
mcimadamore@829 1481 pos, location, site, name, true, argtypes, typeargtypes);
mcimadamore@674 1482 env.info.varArgs = errPhase.isVarargsRequired;
mcimadamore@674 1483 }
mcimadamore@674 1484 } else if (allowMethodHandles && sym.isPolymorphicSignatureGeneric()) {
mcimadamore@674 1485 //non-instantiated polymorphic signature - synthesize new method symbol
mcimadamore@674 1486 env.info.varArgs = false;
mcimadamore@674 1487 sym = findPolymorphicSignatureInstance(env,
mcimadamore@820 1488 site, name, (MethodSymbol)sym, argtypes);
duke@1 1489 }
duke@1 1490 return sym;
duke@1 1491 }
duke@1 1492
mcimadamore@674 1493 /** Find or create an implicit method of exactly the given type (after erasure).
mcimadamore@674 1494 * Searches in a side table, not the main scope of the site.
mcimadamore@674 1495 * This emulates the lookup process required by JSR 292 in JVM.
mcimadamore@674 1496 * @param env Attribution environment
mcimadamore@674 1497 * @param site The original type from where the selection takes place.
mcimadamore@674 1498 * @param name The method's name.
mcimadamore@674 1499 * @param spMethod A template for the implicit method, or null.
mcimadamore@674 1500 * @param argtypes The required argument types.
mcimadamore@674 1501 * @param typeargtypes The required type arguments.
mcimadamore@674 1502 */
mcimadamore@674 1503 Symbol findPolymorphicSignatureInstance(Env<AttrContext> env, Type site,
mcimadamore@674 1504 Name name,
mcimadamore@674 1505 MethodSymbol spMethod, // sig. poly. method or null if none
mcimadamore@820 1506 List<Type> argtypes) {
mcimadamore@674 1507 Type mtype = infer.instantiatePolymorphicSignatureInstance(env,
mcimadamore@820 1508 site, name, spMethod, argtypes);
mcimadamore@674 1509 long flags = ABSTRACT | HYPOTHETICAL | POLYMORPHIC_SIGNATURE |
mcimadamore@674 1510 (spMethod != null ?
mcimadamore@674 1511 spMethod.flags() & Flags.AccessFlags :
mcimadamore@674 1512 Flags.PUBLIC | Flags.STATIC);
mcimadamore@674 1513 Symbol m = null;
mcimadamore@674 1514 for (Scope.Entry e = polymorphicSignatureScope.lookup(name);
mcimadamore@674 1515 e.scope != null;
mcimadamore@674 1516 e = e.next()) {
mcimadamore@674 1517 Symbol sym = e.sym;
mcimadamore@674 1518 if (types.isSameType(mtype, sym.type) &&
mcimadamore@674 1519 (sym.flags() & Flags.STATIC) == (flags & Flags.STATIC) &&
mcimadamore@674 1520 types.isSameType(sym.owner.type, site)) {
mcimadamore@674 1521 m = sym;
mcimadamore@674 1522 break;
mcimadamore@674 1523 }
mcimadamore@674 1524 }
mcimadamore@674 1525 if (m == null) {
mcimadamore@674 1526 // create the desired method
mcimadamore@674 1527 m = new MethodSymbol(flags, name, mtype, site.tsym);
mcimadamore@674 1528 polymorphicSignatureScope.enter(m);
mcimadamore@674 1529 }
mcimadamore@674 1530 return m;
mcimadamore@674 1531 }
mcimadamore@674 1532
duke@1 1533 /** Resolve a qualified method identifier, throw a fatal error if not
duke@1 1534 * found.
duke@1 1535 * @param pos The position to use for error reporting.
duke@1 1536 * @param env The environment current at the method invocation.
duke@1 1537 * @param site The type of the qualifying expression, in which
duke@1 1538 * identifier is searched.
duke@1 1539 * @param name The identifier's name.
duke@1 1540 * @param argtypes The types of the invocation's value arguments.
duke@1 1541 * @param typeargtypes The types of the invocation's type arguments.
duke@1 1542 */
duke@1 1543 public MethodSymbol resolveInternalMethod(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 1544 Type site, Name name,
duke@1 1545 List<Type> argtypes,
duke@1 1546 List<Type> typeargtypes) {
duke@1 1547 Symbol sym = resolveQualifiedMethod(
mcimadamore@829 1548 pos, env, site.tsym, site, name, argtypes, typeargtypes);
duke@1 1549 if (sym.kind == MTH) return (MethodSymbol)sym;
duke@1 1550 else throw new FatalError(
mcimadamore@89 1551 diags.fragment("fatal.err.cant.locate.meth",
duke@1 1552 name));
duke@1 1553 }
duke@1 1554
duke@1 1555 /** Resolve constructor.
duke@1 1556 * @param pos The position to use for error reporting.
duke@1 1557 * @param env The environment current at the constructor invocation.
duke@1 1558 * @param site The type of class for which a constructor is searched.
duke@1 1559 * @param argtypes The types of the constructor invocation's value
duke@1 1560 * arguments.
duke@1 1561 * @param typeargtypes The types of the constructor invocation's type
duke@1 1562 * arguments.
duke@1 1563 */
duke@1 1564 Symbol resolveConstructor(DiagnosticPosition pos,
duke@1 1565 Env<AttrContext> env,
duke@1 1566 Type site,
duke@1 1567 List<Type> argtypes,
duke@1 1568 List<Type> typeargtypes) {
mcimadamore@689 1569 Symbol sym = startResolution();
mcimadamore@160 1570 List<MethodResolutionPhase> steps = methodResolutionSteps;
mcimadamore@160 1571 while (steps.nonEmpty() &&
mcimadamore@160 1572 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@160 1573 sym.kind >= ERRONEOUS) {
mcimadamore@689 1574 currentStep = steps.head;
mcimadamore@160 1575 sym = resolveConstructor(pos, env, site, argtypes, typeargtypes,
mcimadamore@160 1576 steps.head.isBoxingRequired(),
mcimadamore@160 1577 env.info.varArgs = steps.head.isVarargsRequired());
mcimadamore@160 1578 methodResolutionCache.put(steps.head, sym);
mcimadamore@160 1579 steps = steps.tail;
duke@1 1580 }
mcimadamore@160 1581 if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error
mcimadamore@160 1582 MethodResolutionPhase errPhase = firstErroneousResolutionPhase();
mcimadamore@160 1583 sym = access(methodResolutionCache.get(errPhase),
mcimadamore@160 1584 pos, site, names.init, true, argtypes, typeargtypes);
mcimadamore@160 1585 env.info.varArgs = errPhase.isVarargsRequired();
duke@1 1586 }
duke@1 1587 return sym;
duke@1 1588 }
duke@1 1589
mcimadamore@537 1590 /** Resolve constructor using diamond inference.
mcimadamore@537 1591 * @param pos The position to use for error reporting.
mcimadamore@537 1592 * @param env The environment current at the constructor invocation.
mcimadamore@537 1593 * @param site The type of class for which a constructor is searched.
mcimadamore@537 1594 * The scope of this class has been touched in attribution.
mcimadamore@537 1595 * @param argtypes The types of the constructor invocation's value
mcimadamore@537 1596 * arguments.
mcimadamore@537 1597 * @param typeargtypes The types of the constructor invocation's type
mcimadamore@537 1598 * arguments.
mcimadamore@537 1599 */
mcimadamore@537 1600 Symbol resolveDiamond(DiagnosticPosition pos,
mcimadamore@537 1601 Env<AttrContext> env,
mcimadamore@537 1602 Type site,
mcimadamore@537 1603 List<Type> argtypes,
mcimadamore@631 1604 List<Type> typeargtypes) {
mcimadamore@689 1605 Symbol sym = startResolution();
mcimadamore@537 1606 List<MethodResolutionPhase> steps = methodResolutionSteps;
mcimadamore@537 1607 while (steps.nonEmpty() &&
mcimadamore@537 1608 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@537 1609 sym.kind >= ERRONEOUS) {
mcimadamore@689 1610 currentStep = steps.head;
mcimadamore@537 1611 sym = resolveConstructor(pos, env, site, argtypes, typeargtypes,
mcimadamore@537 1612 steps.head.isBoxingRequired(),
mcimadamore@537 1613 env.info.varArgs = steps.head.isVarargsRequired());
mcimadamore@537 1614 methodResolutionCache.put(steps.head, sym);
mcimadamore@537 1615 steps = steps.tail;
mcimadamore@537 1616 }
mcimadamore@631 1617 if (sym.kind >= AMBIGUOUS) {
mcimadamore@689 1618 final JCDiagnostic details = sym.kind == WRONG_MTH ?
mcimadamore@689 1619 ((InapplicableSymbolError)sym).explanation :
mcimadamore@689 1620 null;
mcimadamore@631 1621 Symbol errSym = new ResolveError(WRONG_MTH, "diamond error") {
mcimadamore@631 1622 @Override
mcimadamore@829 1623 JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos,
mcimadamore@829 1624 Symbol location, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) {
mcimadamore@631 1625 String key = details == null ?
mcimadamore@631 1626 "cant.apply.diamond" :
mcimadamore@631 1627 "cant.apply.diamond.1";
mcimadamore@829 1628 return diags.create(dkind, log.currentSource(), pos, key,
mcimadamore@829 1629 diags.fragment("diamond", site.tsym), details);
mcimadamore@631 1630 }
mcimadamore@631 1631 };
mcimadamore@631 1632 MethodResolutionPhase errPhase = firstErroneousResolutionPhase();
mcimadamore@631 1633 sym = access(errSym, pos, site, names.init, true, argtypes, typeargtypes);
mcimadamore@631 1634 env.info.varArgs = errPhase.isVarargsRequired();
mcimadamore@537 1635 }
mcimadamore@537 1636 return sym;
mcimadamore@537 1637 }
mcimadamore@537 1638
duke@1 1639 /** Resolve constructor.
duke@1 1640 * @param pos The position to use for error reporting.
duke@1 1641 * @param env The environment current at the constructor invocation.
duke@1 1642 * @param site The type of class for which a constructor is searched.
duke@1 1643 * @param argtypes The types of the constructor invocation's value
duke@1 1644 * arguments.
duke@1 1645 * @param typeargtypes The types of the constructor invocation's type
duke@1 1646 * arguments.
duke@1 1647 * @param allowBoxing Allow boxing and varargs conversions.
duke@1 1648 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 1649 */
duke@1 1650 Symbol resolveConstructor(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 1651 Type site, List<Type> argtypes,
duke@1 1652 List<Type> typeargtypes,
duke@1 1653 boolean allowBoxing,
duke@1 1654 boolean useVarargs) {
duke@1 1655 Symbol sym = findMethod(env, site,
duke@1 1656 names.init, argtypes,
duke@1 1657 typeargtypes, allowBoxing,
duke@1 1658 useVarargs, false);
mcimadamore@852 1659 chk.checkDeprecated(pos, env.info.scope.owner, sym);
duke@1 1660 return sym;
duke@1 1661 }
duke@1 1662
duke@1 1663 /** Resolve a constructor, throw a fatal error if not found.
duke@1 1664 * @param pos The position to use for error reporting.
duke@1 1665 * @param env The environment current at the method invocation.
duke@1 1666 * @param site The type to be constructed.
duke@1 1667 * @param argtypes The types of the invocation's value arguments.
duke@1 1668 * @param typeargtypes The types of the invocation's type arguments.
duke@1 1669 */
duke@1 1670 public MethodSymbol resolveInternalConstructor(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 1671 Type site,
duke@1 1672 List<Type> argtypes,
duke@1 1673 List<Type> typeargtypes) {
duke@1 1674 Symbol sym = resolveConstructor(
duke@1 1675 pos, env, site, argtypes, typeargtypes);
duke@1 1676 if (sym.kind == MTH) return (MethodSymbol)sym;
duke@1 1677 else throw new FatalError(
mcimadamore@89 1678 diags.fragment("fatal.err.cant.locate.ctor", site));
duke@1 1679 }
duke@1 1680
duke@1 1681 /** Resolve operator.
duke@1 1682 * @param pos The position to use for error reporting.
duke@1 1683 * @param optag The tag of the operation tree.
duke@1 1684 * @param env The environment current at the operation.
duke@1 1685 * @param argtypes The types of the operands.
duke@1 1686 */
duke@1 1687 Symbol resolveOperator(DiagnosticPosition pos, int optag,
duke@1 1688 Env<AttrContext> env, List<Type> argtypes) {
mcimadamore@1110 1689 startResolution();
duke@1 1690 Name name = treeinfo.operatorName(optag);
duke@1 1691 Symbol sym = findMethod(env, syms.predefClass.type, name, argtypes,
duke@1 1692 null, false, false, true);
duke@1 1693 if (boxingEnabled && sym.kind >= WRONG_MTHS)
duke@1 1694 sym = findMethod(env, syms.predefClass.type, name, argtypes,
duke@1 1695 null, true, false, true);
duke@1 1696 return access(sym, pos, env.enclClass.sym.type, name,
duke@1 1697 false, argtypes, null);
duke@1 1698 }
duke@1 1699
duke@1 1700 /** Resolve operator.
duke@1 1701 * @param pos The position to use for error reporting.
duke@1 1702 * @param optag The tag of the operation tree.
duke@1 1703 * @param env The environment current at the operation.
duke@1 1704 * @param arg The type of the operand.
duke@1 1705 */
duke@1 1706 Symbol resolveUnaryOperator(DiagnosticPosition pos, int optag, Env<AttrContext> env, Type arg) {
duke@1 1707 return resolveOperator(pos, optag, env, List.of(arg));
duke@1 1708 }
duke@1 1709
duke@1 1710 /** Resolve binary operator.
duke@1 1711 * @param pos The position to use for error reporting.
duke@1 1712 * @param optag The tag of the operation tree.
duke@1 1713 * @param env The environment current at the operation.
duke@1 1714 * @param left The types of the left operand.
duke@1 1715 * @param right The types of the right operand.
duke@1 1716 */
duke@1 1717 Symbol resolveBinaryOperator(DiagnosticPosition pos,
duke@1 1718 int optag,
duke@1 1719 Env<AttrContext> env,
duke@1 1720 Type left,
duke@1 1721 Type right) {
duke@1 1722 return resolveOperator(pos, optag, env, List.of(left, right));
duke@1 1723 }
duke@1 1724
duke@1 1725 /**
duke@1 1726 * Resolve `c.name' where name == this or name == super.
duke@1 1727 * @param pos The position to use for error reporting.
duke@1 1728 * @param env The environment current at the expression.
duke@1 1729 * @param c The qualifier.
duke@1 1730 * @param name The identifier's name.
duke@1 1731 */
duke@1 1732 Symbol resolveSelf(DiagnosticPosition pos,
duke@1 1733 Env<AttrContext> env,
duke@1 1734 TypeSymbol c,
duke@1 1735 Name name) {
duke@1 1736 Env<AttrContext> env1 = env;
duke@1 1737 boolean staticOnly = false;
duke@1 1738 while (env1.outer != null) {
duke@1 1739 if (isStatic(env1)) staticOnly = true;
duke@1 1740 if (env1.enclClass.sym == c) {
duke@1 1741 Symbol sym = env1.info.scope.lookup(name).sym;
duke@1 1742 if (sym != null) {
duke@1 1743 if (staticOnly) sym = new StaticError(sym);
duke@1 1744 return access(sym, pos, env.enclClass.sym.type,
duke@1 1745 name, true);
duke@1 1746 }
duke@1 1747 }
duke@1 1748 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
duke@1 1749 env1 = env1.outer;
duke@1 1750 }
duke@1 1751 log.error(pos, "not.encl.class", c);
duke@1 1752 return syms.errSymbol;
duke@1 1753 }
duke@1 1754
duke@1 1755 /**
duke@1 1756 * Resolve `c.this' for an enclosing class c that contains the
duke@1 1757 * named member.
duke@1 1758 * @param pos The position to use for error reporting.
duke@1 1759 * @param env The environment current at the expression.
duke@1 1760 * @param member The member that must be contained in the result.
duke@1 1761 */
duke@1 1762 Symbol resolveSelfContaining(DiagnosticPosition pos,
duke@1 1763 Env<AttrContext> env,
mcimadamore@901 1764 Symbol member,
mcimadamore@901 1765 boolean isSuperCall) {
duke@1 1766 Name name = names._this;
mcimadamore@901 1767 Env<AttrContext> env1 = isSuperCall ? env.outer : env;
duke@1 1768 boolean staticOnly = false;
mcimadamore@901 1769 if (env1 != null) {
mcimadamore@901 1770 while (env1 != null && env1.outer != null) {
mcimadamore@901 1771 if (isStatic(env1)) staticOnly = true;
mcimadamore@901 1772 if (env1.enclClass.sym.isSubClass(member.owner, types)) {
mcimadamore@901 1773 Symbol sym = env1.info.scope.lookup(name).sym;
mcimadamore@901 1774 if (sym != null) {
mcimadamore@901 1775 if (staticOnly) sym = new StaticError(sym);
mcimadamore@901 1776 return access(sym, pos, env.enclClass.sym.type,
mcimadamore@901 1777 name, true);
mcimadamore@901 1778 }
duke@1 1779 }
mcimadamore@901 1780 if ((env1.enclClass.sym.flags() & STATIC) != 0)
mcimadamore@901 1781 staticOnly = true;
mcimadamore@901 1782 env1 = env1.outer;
duke@1 1783 }
duke@1 1784 }
duke@1 1785 log.error(pos, "encl.class.required", member);
duke@1 1786 return syms.errSymbol;
duke@1 1787 }
duke@1 1788
duke@1 1789 /**
duke@1 1790 * Resolve an appropriate implicit this instance for t's container.
jjh@972 1791 * JLS 8.8.5.1 and 15.9.2
duke@1 1792 */
duke@1 1793 Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t) {
mcimadamore@901 1794 return resolveImplicitThis(pos, env, t, false);
mcimadamore@901 1795 }
mcimadamore@901 1796
mcimadamore@901 1797 Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t, boolean isSuperCall) {
duke@1 1798 Type thisType = (((t.tsym.owner.kind & (MTH|VAR)) != 0)
duke@1 1799 ? resolveSelf(pos, env, t.getEnclosingType().tsym, names._this)
mcimadamore@901 1800 : resolveSelfContaining(pos, env, t.tsym, isSuperCall)).type;
duke@1 1801 if (env.info.isSelfCall && thisType.tsym == env.enclClass.sym)
duke@1 1802 log.error(pos, "cant.ref.before.ctor.called", "this");
duke@1 1803 return thisType;
duke@1 1804 }
duke@1 1805
duke@1 1806 /* ***************************************************************************
duke@1 1807 * ResolveError classes, indicating error situations when accessing symbols
duke@1 1808 ****************************************************************************/
duke@1 1809
duke@1 1810 public void logAccessError(Env<AttrContext> env, JCTree tree, Type type) {
duke@1 1811 AccessError error = new AccessError(env, type.getEnclosingType(), type.tsym);
mcimadamore@829 1812 logResolveError(error, tree.pos(), type.getEnclosingType().tsym, type.getEnclosingType(), null, null, null);
mcimadamore@302 1813 }
mcimadamore@302 1814 //where
mcimadamore@302 1815 private void logResolveError(ResolveError error,
mcimadamore@302 1816 DiagnosticPosition pos,
mcimadamore@829 1817 Symbol location,
mcimadamore@302 1818 Type site,
mcimadamore@302 1819 Name name,
mcimadamore@302 1820 List<Type> argtypes,
mcimadamore@302 1821 List<Type> typeargtypes) {
mcimadamore@302 1822 JCDiagnostic d = error.getDiagnostic(JCDiagnostic.DiagnosticType.ERROR,
mcimadamore@829 1823 pos, location, site, name, argtypes, typeargtypes);
jjg@643 1824 if (d != null) {
jjg@643 1825 d.setFlag(DiagnosticFlag.RESOLVE_ERROR);
mcimadamore@302 1826 log.report(d);
jjg@643 1827 }
duke@1 1828 }
duke@1 1829
mcimadamore@161 1830 private final LocalizedString noArgs = new LocalizedString("compiler.misc.no.args");
mcimadamore@161 1831
mcimadamore@161 1832 public Object methodArguments(List<Type> argtypes) {
mcimadamore@161 1833 return argtypes.isEmpty() ? noArgs : argtypes;
mcimadamore@161 1834 }
mcimadamore@161 1835
mcimadamore@302 1836 /**
mcimadamore@302 1837 * Root class for resolution errors. Subclass of ResolveError
mcimadamore@302 1838 * represent a different kinds of resolution error - as such they must
mcimadamore@302 1839 * specify how they map into concrete compiler diagnostics.
duke@1 1840 */
mcimadamore@302 1841 private abstract class ResolveError extends Symbol {
duke@1 1842
mcimadamore@302 1843 /** The name of the kind of error, for debugging only. */
mcimadamore@302 1844 final String debugName;
mcimadamore@302 1845
mcimadamore@302 1846 ResolveError(int kind, String debugName) {
duke@1 1847 super(kind, 0, null, null, null);
duke@1 1848 this.debugName = debugName;
duke@1 1849 }
duke@1 1850
mcimadamore@302 1851 @Override
duke@1 1852 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
duke@1 1853 throw new AssertionError();
duke@1 1854 }
duke@1 1855
mcimadamore@302 1856 @Override
duke@1 1857 public String toString() {
mcimadamore@302 1858 return debugName;
duke@1 1859 }
duke@1 1860
mcimadamore@302 1861 @Override
mcimadamore@302 1862 public boolean exists() {
mcimadamore@302 1863 return false;
duke@1 1864 }
duke@1 1865
mcimadamore@302 1866 /**
mcimadamore@302 1867 * Create an external representation for this erroneous symbol to be
mcimadamore@302 1868 * used during attribution - by default this returns the symbol of a
mcimadamore@302 1869 * brand new error type which stores the original type found
mcimadamore@302 1870 * during resolution.
mcimadamore@302 1871 *
mcimadamore@302 1872 * @param name the name used during resolution
mcimadamore@302 1873 * @param location the location from which the symbol is accessed
duke@1 1874 */
mcimadamore@302 1875 protected Symbol access(Name name, TypeSymbol location) {
mcimadamore@302 1876 return types.createErrorType(name, location, syms.errSymbol.type).tsym;
duke@1 1877 }
duke@1 1878
mcimadamore@302 1879 /**
mcimadamore@302 1880 * Create a diagnostic representing this resolution error.
mcimadamore@302 1881 *
mcimadamore@302 1882 * @param dkind The kind of the diagnostic to be created (e.g error).
mcimadamore@302 1883 * @param pos The position to be used for error reporting.
mcimadamore@302 1884 * @param site The original type from where the selection took place.
mcimadamore@302 1885 * @param name The name of the symbol to be resolved.
mcimadamore@302 1886 * @param argtypes The invocation's value arguments,
mcimadamore@302 1887 * if we looked for a method.
mcimadamore@302 1888 * @param typeargtypes The invocation's type arguments,
mcimadamore@302 1889 * if we looked for a method.
mcimadamore@302 1890 */
mcimadamore@302 1891 abstract JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 1892 DiagnosticPosition pos,
mcimadamore@829 1893 Symbol location,
mcimadamore@302 1894 Type site,
mcimadamore@302 1895 Name name,
mcimadamore@302 1896 List<Type> argtypes,
mcimadamore@302 1897 List<Type> typeargtypes);
duke@1 1898
mcimadamore@302 1899 /**
mcimadamore@302 1900 * A name designates an operator if it consists
mcimadamore@302 1901 * of a non-empty sequence of operator symbols +-~!/*%&|^<>=
mcimadamore@80 1902 */
mcimadamore@80 1903 boolean isOperator(Name name) {
mcimadamore@80 1904 int i = 0;
jjg@113 1905 while (i < name.getByteLength() &&
jjg@113 1906 "+-~!*/%&|^<>=".indexOf(name.getByteAt(i)) >= 0) i++;
jjg@113 1907 return i > 0 && i == name.getByteLength();
mcimadamore@80 1908 }
duke@1 1909 }
duke@1 1910
mcimadamore@302 1911 /**
mcimadamore@302 1912 * This class is the root class of all resolution errors caused by
mcimadamore@302 1913 * an invalid symbol being found during resolution.
duke@1 1914 */
mcimadamore@302 1915 abstract class InvalidSymbolError extends ResolveError {
mcimadamore@302 1916
mcimadamore@302 1917 /** The invalid symbol found during resolution */
mcimadamore@302 1918 Symbol sym;
mcimadamore@302 1919
mcimadamore@302 1920 InvalidSymbolError(int kind, Symbol sym, String debugName) {
mcimadamore@302 1921 super(kind, debugName);
mcimadamore@302 1922 this.sym = sym;
mcimadamore@302 1923 }
mcimadamore@302 1924
mcimadamore@302 1925 @Override
mcimadamore@302 1926 public boolean exists() {
mcimadamore@302 1927 return true;
mcimadamore@302 1928 }
mcimadamore@302 1929
mcimadamore@302 1930 @Override
mcimadamore@302 1931 public String toString() {
mcimadamore@302 1932 return super.toString() + " wrongSym=" + sym;
mcimadamore@302 1933 }
mcimadamore@302 1934
mcimadamore@302 1935 @Override
mcimadamore@302 1936 public Symbol access(Name name, TypeSymbol location) {
mcimadamore@302 1937 if (sym.kind >= AMBIGUOUS)
mcimadamore@302 1938 return ((ResolveError)sym).access(name, location);
mcimadamore@302 1939 else if ((sym.kind & ERRONEOUS) == 0 && (sym.kind & TYP) != 0)
mcimadamore@302 1940 return types.createErrorType(name, location, sym.type).tsym;
mcimadamore@302 1941 else
mcimadamore@302 1942 return sym;
mcimadamore@302 1943 }
mcimadamore@302 1944 }
mcimadamore@302 1945
mcimadamore@302 1946 /**
mcimadamore@302 1947 * InvalidSymbolError error class indicating that a symbol matching a
mcimadamore@302 1948 * given name does not exists in a given site.
mcimadamore@302 1949 */
mcimadamore@302 1950 class SymbolNotFoundError extends ResolveError {
mcimadamore@302 1951
mcimadamore@302 1952 SymbolNotFoundError(int kind) {
mcimadamore@302 1953 super(kind, "symbol not found error");
mcimadamore@302 1954 }
mcimadamore@302 1955
mcimadamore@302 1956 @Override
mcimadamore@302 1957 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 1958 DiagnosticPosition pos,
mcimadamore@829 1959 Symbol location,
mcimadamore@302 1960 Type site,
mcimadamore@302 1961 Name name,
mcimadamore@302 1962 List<Type> argtypes,
mcimadamore@302 1963 List<Type> typeargtypes) {
mcimadamore@302 1964 argtypes = argtypes == null ? List.<Type>nil() : argtypes;
mcimadamore@302 1965 typeargtypes = typeargtypes == null ? List.<Type>nil() : typeargtypes;
mcimadamore@302 1966 if (name == names.error)
mcimadamore@302 1967 return null;
mcimadamore@302 1968
mcimadamore@302 1969 if (isOperator(name)) {
mcimadamore@829 1970 boolean isUnaryOp = argtypes.size() == 1;
mcimadamore@829 1971 String key = argtypes.size() == 1 ?
mcimadamore@829 1972 "operator.cant.be.applied" :
mcimadamore@829 1973 "operator.cant.be.applied.1";
mcimadamore@829 1974 Type first = argtypes.head;
mcimadamore@829 1975 Type second = !isUnaryOp ? argtypes.tail.head : null;
jjg@612 1976 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@829 1977 key, name, first, second);
mcimadamore@302 1978 }
mcimadamore@302 1979 boolean hasLocation = false;
mcimadamore@855 1980 if (location == null) {
mcimadamore@855 1981 location = site.tsym;
mcimadamore@855 1982 }
mcimadamore@829 1983 if (!location.name.isEmpty()) {
mcimadamore@829 1984 if (location.kind == PCK && !site.tsym.exists()) {
jjg@612 1985 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@829 1986 "doesnt.exist", location);
mcimadamore@302 1987 }
mcimadamore@829 1988 hasLocation = !location.name.equals(names._this) &&
mcimadamore@829 1989 !location.name.equals(names._super);
mcimadamore@302 1990 }
mcimadamore@302 1991 boolean isConstructor = kind == ABSENT_MTH &&
mcimadamore@302 1992 name == names.table.names.init;
mcimadamore@302 1993 KindName kindname = isConstructor ? KindName.CONSTRUCTOR : absentKind(kind);
mcimadamore@302 1994 Name idname = isConstructor ? site.tsym.name : name;
mcimadamore@302 1995 String errKey = getErrorKey(kindname, typeargtypes.nonEmpty(), hasLocation);
mcimadamore@302 1996 if (hasLocation) {
jjg@612 1997 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@302 1998 errKey, kindname, idname, //symbol kindname, name
mcimadamore@302 1999 typeargtypes, argtypes, //type parameters and arguments (if any)
mcimadamore@855 2000 getLocationDiag(location, site)); //location kindname, type
mcimadamore@302 2001 }
mcimadamore@302 2002 else {
jjg@612 2003 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@302 2004 errKey, kindname, idname, //symbol kindname, name
mcimadamore@302 2005 typeargtypes, argtypes); //type parameters and arguments (if any)
mcimadamore@302 2006 }
mcimadamore@302 2007 }
mcimadamore@302 2008 //where
mcimadamore@302 2009 private String getErrorKey(KindName kindname, boolean hasTypeArgs, boolean hasLocation) {
mcimadamore@302 2010 String key = "cant.resolve";
mcimadamore@302 2011 String suffix = hasLocation ? ".location" : "";
mcimadamore@302 2012 switch (kindname) {
mcimadamore@302 2013 case METHOD:
mcimadamore@302 2014 case CONSTRUCTOR: {
mcimadamore@302 2015 suffix += ".args";
mcimadamore@302 2016 suffix += hasTypeArgs ? ".params" : "";
mcimadamore@302 2017 }
mcimadamore@302 2018 }
mcimadamore@302 2019 return key + suffix;
mcimadamore@302 2020 }
mcimadamore@855 2021 private JCDiagnostic getLocationDiag(Symbol location, Type site) {
mcimadamore@855 2022 if (location.kind == VAR) {
mcimadamore@855 2023 return diags.fragment("location.1",
mcimadamore@829 2024 kindName(location),
mcimadamore@829 2025 location,
mcimadamore@855 2026 location.type);
mcimadamore@855 2027 } else {
mcimadamore@855 2028 return diags.fragment("location",
mcimadamore@855 2029 typeKindName(site),
mcimadamore@855 2030 site,
mcimadamore@855 2031 null);
mcimadamore@855 2032 }
mcimadamore@829 2033 }
mcimadamore@302 2034 }
mcimadamore@302 2035
mcimadamore@302 2036 /**
mcimadamore@302 2037 * InvalidSymbolError error class indicating that a given symbol
mcimadamore@302 2038 * (either a method, a constructor or an operand) is not applicable
mcimadamore@302 2039 * given an actual arguments/type argument list.
mcimadamore@302 2040 */
mcimadamore@302 2041 class InapplicableSymbolError extends InvalidSymbolError {
mcimadamore@302 2042
mcimadamore@302 2043 /** An auxiliary explanation set in case of instantiation errors. */
mcimadamore@302 2044 JCDiagnostic explanation;
mcimadamore@302 2045
mcimadamore@302 2046 InapplicableSymbolError(Symbol sym) {
mcimadamore@302 2047 super(WRONG_MTH, sym, "inapplicable symbol error");
mcimadamore@302 2048 }
mcimadamore@302 2049
mcimadamore@302 2050 /** Update sym and explanation and return this.
mcimadamore@302 2051 */
mcimadamore@302 2052 InapplicableSymbolError setWrongSym(Symbol sym, JCDiagnostic explanation) {
mcimadamore@302 2053 this.sym = sym;
mcimadamore@689 2054 if (this.sym == sym && explanation != null)
mcimadamore@689 2055 this.explanation = explanation; //update the details
mcimadamore@302 2056 return this;
mcimadamore@302 2057 }
mcimadamore@302 2058
mcimadamore@302 2059 /** Update sym and return this.
mcimadamore@302 2060 */
mcimadamore@302 2061 InapplicableSymbolError setWrongSym(Symbol sym) {
mcimadamore@302 2062 this.sym = sym;
mcimadamore@302 2063 return this;
mcimadamore@302 2064 }
mcimadamore@302 2065
mcimadamore@302 2066 @Override
mcimadamore@302 2067 public String toString() {
mcimadamore@302 2068 return super.toString() + " explanation=" + explanation;
mcimadamore@302 2069 }
mcimadamore@302 2070
mcimadamore@302 2071 @Override
mcimadamore@302 2072 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 2073 DiagnosticPosition pos,
mcimadamore@829 2074 Symbol location,
mcimadamore@302 2075 Type site,
mcimadamore@302 2076 Name name,
mcimadamore@302 2077 List<Type> argtypes,
mcimadamore@302 2078 List<Type> typeargtypes) {
mcimadamore@302 2079 if (name == names.error)
mcimadamore@302 2080 return null;
mcimadamore@302 2081
mcimadamore@302 2082 if (isOperator(name)) {
mcimadamore@853 2083 boolean isUnaryOp = argtypes.size() == 1;
mcimadamore@853 2084 String key = argtypes.size() == 1 ?
mcimadamore@853 2085 "operator.cant.be.applied" :
mcimadamore@853 2086 "operator.cant.be.applied.1";
mcimadamore@853 2087 Type first = argtypes.head;
mcimadamore@853 2088 Type second = !isUnaryOp ? argtypes.tail.head : null;
mcimadamore@853 2089 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@853 2090 key, name, first, second);
mcimadamore@302 2091 }
mcimadamore@302 2092 else {
mcimadamore@302 2093 Symbol ws = sym.asMemberOf(site, types);
jjg@612 2094 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@302 2095 "cant.apply.symbol" + (explanation != null ? ".1" : ""),
mcimadamore@302 2096 kindName(ws),
mcimadamore@302 2097 ws.name == names.init ? ws.owner.name : ws.name,
mcimadamore@302 2098 methodArguments(ws.type.getParameterTypes()),
mcimadamore@302 2099 methodArguments(argtypes),
mcimadamore@302 2100 kindName(ws.owner),
mcimadamore@302 2101 ws.owner.type,
mcimadamore@302 2102 explanation);
mcimadamore@302 2103 }
mcimadamore@302 2104 }
mcimadamore@302 2105
mcimadamore@689 2106 void clear() {
mcimadamore@689 2107 explanation = null;
mcimadamore@689 2108 }
mcimadamore@689 2109
mcimadamore@302 2110 @Override
mcimadamore@302 2111 public Symbol access(Name name, TypeSymbol location) {
mcimadamore@302 2112 return types.createErrorType(name, location, syms.errSymbol.type).tsym;
mcimadamore@302 2113 }
mcimadamore@302 2114 }
mcimadamore@302 2115
mcimadamore@302 2116 /**
mcimadamore@302 2117 * ResolveError error class indicating that a set of symbols
mcimadamore@302 2118 * (either methods, constructors or operands) is not applicable
mcimadamore@302 2119 * given an actual arguments/type argument list.
mcimadamore@302 2120 */
mcimadamore@302 2121 class InapplicableSymbolsError extends ResolveError {
mcimadamore@689 2122
mcimadamore@689 2123 private List<Candidate> candidates = List.nil();
mcimadamore@689 2124
mcimadamore@302 2125 InapplicableSymbolsError(Symbol sym) {
mcimadamore@302 2126 super(WRONG_MTHS, "inapplicable symbols");
mcimadamore@302 2127 }
mcimadamore@302 2128
mcimadamore@302 2129 @Override
mcimadamore@302 2130 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 2131 DiagnosticPosition pos,
mcimadamore@829 2132 Symbol location,
mcimadamore@302 2133 Type site,
mcimadamore@302 2134 Name name,
mcimadamore@302 2135 List<Type> argtypes,
mcimadamore@302 2136 List<Type> typeargtypes) {
mcimadamore@689 2137 if (candidates.nonEmpty()) {
mcimadamore@689 2138 JCDiagnostic err = diags.create(dkind,
mcimadamore@689 2139 log.currentSource(),
mcimadamore@689 2140 pos,
mcimadamore@689 2141 "cant.apply.symbols",
mcimadamore@689 2142 name == names.init ? KindName.CONSTRUCTOR : absentKind(kind),
mcimadamore@689 2143 getName(),
mcimadamore@689 2144 argtypes);
mcimadamore@689 2145 return new JCDiagnostic.MultilineDiagnostic(err, candidateDetails(site));
mcimadamore@689 2146 } else {
mcimadamore@689 2147 return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind, pos,
mcimadamore@829 2148 location, site, name, argtypes, typeargtypes);
mcimadamore@689 2149 }
mcimadamore@689 2150 }
mcimadamore@689 2151
mcimadamore@689 2152 //where
mcimadamore@689 2153 List<JCDiagnostic> candidateDetails(Type site) {
mcimadamore@689 2154 List<JCDiagnostic> details = List.nil();
mcimadamore@689 2155 for (Candidate c : candidates)
mcimadamore@689 2156 details = details.prepend(c.getDiagnostic(site));
mcimadamore@689 2157 return details.reverse();
mcimadamore@689 2158 }
mcimadamore@689 2159
mcimadamore@689 2160 Symbol addCandidate(MethodResolutionPhase currentStep, Symbol sym, JCDiagnostic details) {
mcimadamore@689 2161 Candidate c = new Candidate(currentStep, sym, details);
mcimadamore@689 2162 if (c.isValid() && !candidates.contains(c))
mcimadamore@689 2163 candidates = candidates.append(c);
mcimadamore@689 2164 return this;
mcimadamore@689 2165 }
mcimadamore@689 2166
mcimadamore@689 2167 void clear() {
mcimadamore@689 2168 candidates = List.nil();
mcimadamore@689 2169 }
mcimadamore@689 2170
mcimadamore@689 2171 private Name getName() {
mcimadamore@689 2172 Symbol sym = candidates.head.sym;
mcimadamore@689 2173 return sym.name == names.init ?
mcimadamore@689 2174 sym.owner.name :
mcimadamore@689 2175 sym.name;
mcimadamore@689 2176 }
mcimadamore@689 2177
mcimadamore@689 2178 private class Candidate {
mcimadamore@689 2179
mcimadamore@689 2180 final MethodResolutionPhase step;
mcimadamore@689 2181 final Symbol sym;
mcimadamore@689 2182 final JCDiagnostic details;
mcimadamore@689 2183
mcimadamore@689 2184 private Candidate(MethodResolutionPhase step, Symbol sym, JCDiagnostic details) {
mcimadamore@689 2185 this.step = step;
mcimadamore@689 2186 this.sym = sym;
mcimadamore@689 2187 this.details = details;
mcimadamore@689 2188 }
mcimadamore@689 2189
mcimadamore@689 2190 JCDiagnostic getDiagnostic(Type site) {
mcimadamore@689 2191 return diags.fragment("inapplicable.method",
mcimadamore@689 2192 Kinds.kindName(sym),
mcimadamore@689 2193 sym.location(site, types),
mcimadamore@689 2194 sym.asMemberOf(site, types),
mcimadamore@689 2195 details);
mcimadamore@689 2196 }
mcimadamore@689 2197
mcimadamore@689 2198 @Override
mcimadamore@689 2199 public boolean equals(Object o) {
mcimadamore@689 2200 if (o instanceof Candidate) {
mcimadamore@689 2201 Symbol s1 = this.sym;
mcimadamore@689 2202 Symbol s2 = ((Candidate)o).sym;
mcimadamore@689 2203 if ((s1 != s2 &&
mcimadamore@689 2204 (s1.overrides(s2, s1.owner.type.tsym, types, false) ||
mcimadamore@689 2205 (s2.overrides(s1, s2.owner.type.tsym, types, false)))) ||
mcimadamore@689 2206 ((s1.isConstructor() || s2.isConstructor()) && s1.owner != s2.owner))
mcimadamore@689 2207 return true;
mcimadamore@689 2208 }
mcimadamore@689 2209 return false;
mcimadamore@689 2210 }
mcimadamore@689 2211
mcimadamore@689 2212 boolean isValid() {
mcimadamore@689 2213 return (((sym.flags() & VARARGS) != 0 && step == VARARITY) ||
mcimadamore@689 2214 (sym.flags() & VARARGS) == 0 && step == (boxingEnabled ? BOX : BASIC));
mcimadamore@689 2215 }
mcimadamore@302 2216 }
mcimadamore@302 2217 }
mcimadamore@302 2218
mcimadamore@302 2219 /**
mcimadamore@302 2220 * An InvalidSymbolError error class indicating that a symbol is not
mcimadamore@302 2221 * accessible from a given site
mcimadamore@302 2222 */
mcimadamore@302 2223 class AccessError extends InvalidSymbolError {
mcimadamore@302 2224
mcimadamore@302 2225 private Env<AttrContext> env;
mcimadamore@302 2226 private Type site;
duke@1 2227
duke@1 2228 AccessError(Symbol sym) {
duke@1 2229 this(null, null, sym);
duke@1 2230 }
duke@1 2231
duke@1 2232 AccessError(Env<AttrContext> env, Type site, Symbol sym) {
duke@1 2233 super(HIDDEN, sym, "access error");
duke@1 2234 this.env = env;
duke@1 2235 this.site = site;
duke@1 2236 if (debugResolve)
duke@1 2237 log.error("proc.messager", sym + " @ " + site + " is inaccessible.");
duke@1 2238 }
duke@1 2239
mcimadamore@302 2240 @Override
mcimadamore@302 2241 public boolean exists() {
mcimadamore@302 2242 return false;
mcimadamore@302 2243 }
duke@1 2244
mcimadamore@302 2245 @Override
mcimadamore@302 2246 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 2247 DiagnosticPosition pos,
mcimadamore@829 2248 Symbol location,
mcimadamore@302 2249 Type site,
mcimadamore@302 2250 Name name,
mcimadamore@302 2251 List<Type> argtypes,
mcimadamore@302 2252 List<Type> typeargtypes) {
mcimadamore@302 2253 if (sym.owner.type.tag == ERROR)
mcimadamore@302 2254 return null;
mcimadamore@302 2255
mcimadamore@302 2256 if (sym.name == names.init && sym.owner != site.tsym) {
mcimadamore@302 2257 return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind,
mcimadamore@829 2258 pos, location, site, name, argtypes, typeargtypes);
mcimadamore@302 2259 }
mcimadamore@302 2260 else if ((sym.flags() & PUBLIC) != 0
mcimadamore@302 2261 || (env != null && this.site != null
mcimadamore@302 2262 && !isAccessible(env, this.site))) {
jjg@612 2263 return diags.create(dkind, log.currentSource(),
mcimadamore@302 2264 pos, "not.def.access.class.intf.cant.access",
mcimadamore@302 2265 sym, sym.location());
mcimadamore@302 2266 }
mcimadamore@302 2267 else if ((sym.flags() & (PRIVATE | PROTECTED)) != 0) {
jjg@612 2268 return diags.create(dkind, log.currentSource(),
mcimadamore@302 2269 pos, "report.access", sym,
mcimadamore@302 2270 asFlagSet(sym.flags() & (PRIVATE | PROTECTED)),
mcimadamore@302 2271 sym.location());
mcimadamore@302 2272 }
mcimadamore@302 2273 else {
jjg@612 2274 return diags.create(dkind, log.currentSource(),
mcimadamore@302 2275 pos, "not.def.public.cant.access", sym, sym.location());
duke@1 2276 }
duke@1 2277 }
duke@1 2278 }
duke@1 2279
mcimadamore@302 2280 /**
mcimadamore@302 2281 * InvalidSymbolError error class indicating that an instance member
mcimadamore@302 2282 * has erroneously been accessed from a static context.
duke@1 2283 */
mcimadamore@302 2284 class StaticError extends InvalidSymbolError {
mcimadamore@302 2285
duke@1 2286 StaticError(Symbol sym) {
duke@1 2287 super(STATICERR, sym, "static error");
duke@1 2288 }
duke@1 2289
mcimadamore@302 2290 @Override
mcimadamore@302 2291 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 2292 DiagnosticPosition pos,
mcimadamore@829 2293 Symbol location,
mcimadamore@302 2294 Type site,
mcimadamore@302 2295 Name name,
mcimadamore@302 2296 List<Type> argtypes,
mcimadamore@302 2297 List<Type> typeargtypes) {
mcimadamore@80 2298 Symbol errSym = ((sym.kind == TYP && sym.type.tag == CLASS)
mcimadamore@80 2299 ? types.erasure(sym.type).tsym
mcimadamore@80 2300 : sym);
jjg@612 2301 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@302 2302 "non-static.cant.be.ref", kindName(sym), errSym);
duke@1 2303 }
duke@1 2304 }
duke@1 2305
mcimadamore@302 2306 /**
mcimadamore@302 2307 * InvalidSymbolError error class indicating that a pair of symbols
mcimadamore@302 2308 * (either methods, constructors or operands) are ambiguous
mcimadamore@302 2309 * given an actual arguments/type argument list.
duke@1 2310 */
mcimadamore@302 2311 class AmbiguityError extends InvalidSymbolError {
mcimadamore@302 2312
mcimadamore@302 2313 /** The other maximally specific symbol */
duke@1 2314 Symbol sym2;
duke@1 2315
duke@1 2316 AmbiguityError(Symbol sym1, Symbol sym2) {
duke@1 2317 super(AMBIGUOUS, sym1, "ambiguity error");
duke@1 2318 this.sym2 = sym2;
duke@1 2319 }
duke@1 2320
mcimadamore@302 2321 @Override
mcimadamore@302 2322 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 2323 DiagnosticPosition pos,
mcimadamore@829 2324 Symbol location,
mcimadamore@302 2325 Type site,
mcimadamore@302 2326 Name name,
mcimadamore@302 2327 List<Type> argtypes,
mcimadamore@302 2328 List<Type> typeargtypes) {
duke@1 2329 AmbiguityError pair = this;
duke@1 2330 while (true) {
mcimadamore@302 2331 if (pair.sym.kind == AMBIGUOUS)
mcimadamore@302 2332 pair = (AmbiguityError)pair.sym;
duke@1 2333 else if (pair.sym2.kind == AMBIGUOUS)
duke@1 2334 pair = (AmbiguityError)pair.sym2;
duke@1 2335 else break;
duke@1 2336 }
mcimadamore@302 2337 Name sname = pair.sym.name;
mcimadamore@302 2338 if (sname == names.init) sname = pair.sym.owner.name;
jjg@612 2339 return diags.create(dkind, log.currentSource(),
mcimadamore@302 2340 pos, "ref.ambiguous", sname,
mcimadamore@302 2341 kindName(pair.sym),
mcimadamore@302 2342 pair.sym,
mcimadamore@302 2343 pair.sym.location(site, types),
duke@1 2344 kindName(pair.sym2),
duke@1 2345 pair.sym2,
duke@1 2346 pair.sym2.location(site, types));
duke@1 2347 }
duke@1 2348 }
mcimadamore@160 2349
mcimadamore@160 2350 enum MethodResolutionPhase {
mcimadamore@160 2351 BASIC(false, false),
mcimadamore@160 2352 BOX(true, false),
mcimadamore@160 2353 VARARITY(true, true);
mcimadamore@160 2354
mcimadamore@160 2355 boolean isBoxingRequired;
mcimadamore@160 2356 boolean isVarargsRequired;
mcimadamore@160 2357
mcimadamore@160 2358 MethodResolutionPhase(boolean isBoxingRequired, boolean isVarargsRequired) {
mcimadamore@160 2359 this.isBoxingRequired = isBoxingRequired;
mcimadamore@160 2360 this.isVarargsRequired = isVarargsRequired;
mcimadamore@160 2361 }
mcimadamore@160 2362
mcimadamore@160 2363 public boolean isBoxingRequired() {
mcimadamore@160 2364 return isBoxingRequired;
mcimadamore@160 2365 }
mcimadamore@160 2366
mcimadamore@160 2367 public boolean isVarargsRequired() {
mcimadamore@160 2368 return isVarargsRequired;
mcimadamore@160 2369 }
mcimadamore@160 2370
mcimadamore@160 2371 public boolean isApplicable(boolean boxingEnabled, boolean varargsEnabled) {
mcimadamore@160 2372 return (varargsEnabled || !isVarargsRequired) &&
mcimadamore@160 2373 (boxingEnabled || !isBoxingRequired);
mcimadamore@160 2374 }
mcimadamore@160 2375 }
mcimadamore@160 2376
mcimadamore@160 2377 private Map<MethodResolutionPhase, Symbol> methodResolutionCache =
mcimadamore@160 2378 new HashMap<MethodResolutionPhase, Symbol>(MethodResolutionPhase.values().length);
mcimadamore@160 2379
mcimadamore@160 2380 final List<MethodResolutionPhase> methodResolutionSteps = List.of(BASIC, BOX, VARARITY);
mcimadamore@160 2381
mcimadamore@689 2382 private MethodResolutionPhase currentStep = null;
mcimadamore@689 2383
mcimadamore@160 2384 private MethodResolutionPhase firstErroneousResolutionPhase() {
mcimadamore@160 2385 MethodResolutionPhase bestSoFar = BASIC;
mcimadamore@160 2386 Symbol sym = methodNotFound;
mcimadamore@160 2387 List<MethodResolutionPhase> steps = methodResolutionSteps;
mcimadamore@160 2388 while (steps.nonEmpty() &&
mcimadamore@160 2389 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@160 2390 sym.kind >= WRONG_MTHS) {
mcimadamore@160 2391 sym = methodResolutionCache.get(steps.head);
mcimadamore@160 2392 bestSoFar = steps.head;
mcimadamore@160 2393 steps = steps.tail;
mcimadamore@160 2394 }
mcimadamore@160 2395 return bestSoFar;
mcimadamore@160 2396 }
duke@1 2397 }

mercurial