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

Tue, 25 Sep 2012 11:52:37 +0100

author
mcimadamore
date
Tue, 25 Sep 2012 11:52:37 +0100
changeset 1335
99983a4a593b
parent 1326
30c36e23f154
child 1337
2eca84194807
permissions
-rw-r--r--

7193913: Cleanup Resolve.findMethod
Summary: Refactor method lookup logic in Resolve.findMethod
Reviewed-by: jjg

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

mercurial