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

Tue, 03 Sep 2013 23:41:37 +0100

author
vromero
date
Tue, 03 Sep 2013 23:41:37 +0100
changeset 2003
9be0afbdf244
parent 2000
4a6acc42c3a1
child 2026
03c26c60499c
permissions
-rw-r--r--

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

mercurial