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

Wed, 15 May 2013 14:00:31 +0100

author
mcimadamore
date
Wed, 15 May 2013 14:00:31 +0100
changeset 1759
05ec778794d0
parent 1697
950e8ac120f0
child 1779
97a9b4b3e63a
permissions
-rw-r--r--

8012003: Method diagnostics resolution need to be simplified in some cases
Summary: Unfold method resolution diagnostics when they mention errors in poly expressions
Reviewed-by: jjg, vromero

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

mercurial