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

Thu, 28 Feb 2013 14:05:44 +0000

author
mcimadamore
date
Thu, 28 Feb 2013 14:05:44 +0000
changeset 1609
332f23993353
parent 1599
9f0ec00514b6
child 1610
08782b8b03ce
permissions
-rw-r--r--

8008813: Structural most specific fails when method reference is passed to overloaded method
Summary: Bad logic for checking most specific method reference type
Reviewed-by: jjg

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

mercurial