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

Sun, 04 Nov 2012 10:59:42 +0000

author
mcimadamore
date
Sun, 04 Nov 2012 10:59:42 +0000
changeset 1393
d7d932236fee
parent 1374
c002fdee76fd
child 1394
dbc94b8363dd
permissions
-rw-r--r--

7192246: Add type-checking support for default methods
Summary: Add type-checking support for default methods as per Featherweight-Defender document
Reviewed-by: jjg, dlsmith

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

mercurial