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

Wed, 17 Jul 2013 14:09:46 +0100

author
mcimadamore
date
Wed, 17 Jul 2013 14:09:46 +0100
changeset 1897
866c87c01285
parent 1875
f559ef7568ce
child 1898
a204cf7aab7e
permissions
-rw-r--r--

8016175: Add bottom-up type-checking support for unambiguous method references
Summary: Type-checking of non-overloaded method references should be independent from target-type
Reviewed-by: jjg, vromero

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

mercurial