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

Thu, 20 Nov 2014 14:05:39 -0800

author
vromero
date
Thu, 20 Nov 2014 14:05:39 -0800
changeset 2611
9e80ab1dad9e
parent 2559
0253e7cc98a4
child 2702
9ca8d8713094
child 2788
f08330fad341
permissions
-rw-r--r--

8063052: Inference chokes on wildcard derived from method reference
Reviewed-by: mcimadamore

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

mercurial