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

Thu, 06 Feb 2014 21:11:27 +0000

author
vromero
date
Thu, 06 Feb 2014 21:11:27 +0000
changeset 2261
79dc4b992c0a
parent 2260
fb870c70e774
child 2368
0524f786d7e8
permissions
-rw-r--r--

8030855: Default methods should be visible under source previous to 8
Reviewed-by: jjg, dlsmith

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

mercurial