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

Mon, 21 Jan 2013 20:13:56 +0000

author
mcimadamore
date
Mon, 21 Jan 2013 20:13:56 +0000
changeset 1510
7873d37f5b37
parent 1498
1afdf1f1472b
child 1521
71f35e4b93a5
permissions
-rw-r--r--

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

mercurial