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

Tue, 27 May 2014 17:30:48 -0600

author
dlsmith
date
Tue, 27 May 2014 17:30:48 -0600
changeset 2400
0e026d3f2786
parent 2395
9c577131ffa6
child 2525
2eb010b6cb22
child 2531
fc2a01ba3d79
permissions
-rw-r--r--

8042338: Refactor Types.upperBound to treat wildcards and variables separately
Reviewed-by: vromero

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

mercurial