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

Sun, 04 Nov 2012 11:01:49 +0000

author
mcimadamore
date
Sun, 04 Nov 2012 11:01:49 +0000
changeset 1394
dbc94b8363dd
parent 1393
d7d932236fee
child 1396
9b85813d2262
permissions
-rw-r--r--

8000931: Cleanup Resolve.java
Summary: Unify all method resolution routines
Reviewed-by: jjg

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

mercurial