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

Sat, 06 Oct 2012 10:35:38 +0100

author
mcimadamore
date
Sat, 06 Oct 2012 10:35:38 +0100
changeset 1352
d4b3cb1ece84
parent 1348
573ceb23beeb
child 1374
c002fdee76fd
permissions
-rw-r--r--

7177386: Add attribution support for method references
Summary: Add type-checking/lookup routines for method references
Reviewed-by: jjg, dlsmith

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

mercurial