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

Mon, 18 Jan 2016 10:58:12 +0000

author
mcimadamore
date
Mon, 18 Jan 2016 10:58:12 +0000
changeset 3075
745c9feb99f2
parent 3005
0353cf89ea96
child 3092
8c3890c90147
permissions
-rw-r--r--

8130506: javac AssertionError when invoking MethodHandle.invoke with lambda parameter
Reviewed-by: mcimadamore
Contributed-by: srikanth.adayapalam@oracle.com

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

mercurial