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

Mon, 01 Jul 2013 14:57:03 +0100

author
mcimadamore
date
Mon, 01 Jul 2013 14:57:03 +0100
changeset 1875
f559ef7568ce
parent 1853
831467c4c6a7
child 1897
866c87c01285
permissions
-rw-r--r--

7034798: Ambiguity error for abstract method call is too eager
Summary: Javac should wait and see if ambiguous methods can be reconciled at the end of an overload resolution round
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
vromero@1790 352 /** Is symbol accessible as a member of given type in given environment?
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
vromero@1790 493 * given type arguments and argument types. If successful, 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
vromero@1790 497 * of an under determined 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);
mcimadamore@1809 1210 if (bestSoFar.exists() && sym.exists() &&
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@1779 1346 if (!operator || verboseResolutionMode.contains(VerboseResolutionMode.PREDEF))
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 return bestSoFar;
duke@1 1577 }
duke@1 1578 // where
duke@1 1579 private Symbol findMethod(Env<AttrContext> env,
duke@1 1580 Type site,
duke@1 1581 Name name,
duke@1 1582 List<Type> argtypes,
duke@1 1583 List<Type> typeargtypes,
duke@1 1584 Type intype,
duke@1 1585 Symbol bestSoFar,
duke@1 1586 boolean allowBoxing,
duke@1 1587 boolean useVarargs,
mcimadamore@1335 1588 boolean operator) {
mcimadamore@1393 1589 @SuppressWarnings({"unchecked","rawtypes"})
mcimadamore@1393 1590 List<Type>[] itypes = (List<Type>[])new List[] { List.<Type>nil(), List.<Type>nil() };
mcimadamore@1393 1591 InterfaceLookupPhase iphase = InterfaceLookupPhase.ABSTRACT_OK;
mcimadamore@1335 1592 for (TypeSymbol s : superclasses(intype)) {
mcimadamore@1394 1593 bestSoFar = findMethodInScope(env, site, name, argtypes, typeargtypes,
mcimadamore@1335 1594 s.members(), bestSoFar, allowBoxing, useVarargs, operator, true);
mcimadamore@1393 1595 if (name == names.init) return bestSoFar;
mcimadamore@1393 1596 iphase = (iphase == null) ? null : iphase.update(s, this);
mcimadamore@1393 1597 if (iphase != null) {
mcimadamore@1335 1598 for (Type itype : types.interfaces(s.type)) {
mcimadamore@1393 1599 itypes[iphase.ordinal()] = types.union(types.closure(itype), itypes[iphase.ordinal()]);
duke@1 1600 }
duke@1 1601 }
mcimadamore@1335 1602 }
mcimadamore@1335 1603
mcimadamore@1335 1604 Symbol concrete = bestSoFar.kind < ERR &&
mcimadamore@1335 1605 (bestSoFar.flags() & ABSTRACT) == 0 ?
mcimadamore@1335 1606 bestSoFar : methodNotFound;
mcimadamore@1335 1607
mcimadamore@1393 1608 for (InterfaceLookupPhase iphase2 : InterfaceLookupPhase.values()) {
mcimadamore@1393 1609 if (iphase2 == InterfaceLookupPhase.DEFAULT_OK && !allowDefaultMethods) break;
mcimadamore@1335 1610 //keep searching for abstract methods
mcimadamore@1393 1611 for (Type itype : itypes[iphase2.ordinal()]) {
mcimadamore@1335 1612 if (!itype.isInterface()) continue; //skip j.l.Object (included by Types.closure())
mcimadamore@1393 1613 if (iphase2 == InterfaceLookupPhase.DEFAULT_OK &&
mcimadamore@1393 1614 (itype.tsym.flags() & DEFAULT) == 0) continue;
mcimadamore@1394 1615 bestSoFar = findMethodInScope(env, site, name, argtypes, typeargtypes,
mcimadamore@1393 1616 itype.tsym.members(), bestSoFar, allowBoxing, useVarargs, operator, true);
mcimadamore@1393 1617 if (concrete != bestSoFar &&
mcimadamore@1393 1618 concrete.kind < ERR && bestSoFar.kind < ERR &&
mcimadamore@1393 1619 types.isSubSignature(concrete.type, bestSoFar.type)) {
mcimadamore@1393 1620 //this is an hack - as javac does not do full membership checks
mcimadamore@1393 1621 //most specific ends up comparing abstract methods that might have
mcimadamore@1393 1622 //been implemented by some concrete method in a subclass and,
mcimadamore@1393 1623 //because of raw override, it is possible for an abstract method
mcimadamore@1393 1624 //to be more specific than the concrete method - so we need
mcimadamore@1393 1625 //to explicitly call that out (see CR 6178365)
mcimadamore@1393 1626 bestSoFar = concrete;
mcimadamore@1393 1627 }
duke@1 1628 }
duke@1 1629 }
duke@1 1630 return bestSoFar;
duke@1 1631 }
duke@1 1632
mcimadamore@1393 1633 enum InterfaceLookupPhase {
mcimadamore@1393 1634 ABSTRACT_OK() {
mcimadamore@1393 1635 @Override
mcimadamore@1393 1636 InterfaceLookupPhase update(Symbol s, Resolve rs) {
mcimadamore@1393 1637 //We should not look for abstract methods if receiver is a concrete class
mcimadamore@1393 1638 //(as concrete classes are expected to implement all abstracts coming
mcimadamore@1393 1639 //from superinterfaces)
mcimadamore@1393 1640 if ((s.flags() & (ABSTRACT | INTERFACE | ENUM)) != 0) {
mcimadamore@1393 1641 return this;
mcimadamore@1393 1642 } else if (rs.allowDefaultMethods) {
mcimadamore@1393 1643 return DEFAULT_OK;
mcimadamore@1393 1644 } else {
mcimadamore@1393 1645 return null;
mcimadamore@1393 1646 }
mcimadamore@1393 1647 }
mcimadamore@1393 1648 },
mcimadamore@1393 1649 DEFAULT_OK() {
mcimadamore@1393 1650 @Override
mcimadamore@1393 1651 InterfaceLookupPhase update(Symbol s, Resolve rs) {
mcimadamore@1393 1652 return this;
mcimadamore@1393 1653 }
mcimadamore@1393 1654 };
mcimadamore@1393 1655
mcimadamore@1393 1656 abstract InterfaceLookupPhase update(Symbol s, Resolve rs);
mcimadamore@1393 1657 }
mcimadamore@1393 1658
mcimadamore@1335 1659 /**
mcimadamore@1335 1660 * Return an Iterable object to scan the superclasses of a given type.
mcimadamore@1335 1661 * It's crucial that the scan is done lazily, as we don't want to accidentally
mcimadamore@1335 1662 * access more supertypes than strictly needed (as this could trigger completion
mcimadamore@1335 1663 * errors if some of the not-needed supertypes are missing/ill-formed).
mcimadamore@1335 1664 */
mcimadamore@1335 1665 Iterable<TypeSymbol> superclasses(final Type intype) {
mcimadamore@1335 1666 return new Iterable<TypeSymbol>() {
mcimadamore@1335 1667 public Iterator<TypeSymbol> iterator() {
mcimadamore@1335 1668 return new Iterator<TypeSymbol>() {
mcimadamore@1335 1669
mcimadamore@1335 1670 List<TypeSymbol> seen = List.nil();
mcimadamore@1342 1671 TypeSymbol currentSym = symbolFor(intype);
mcimadamore@1342 1672 TypeSymbol prevSym = null;
mcimadamore@1335 1673
mcimadamore@1335 1674 public boolean hasNext() {
mcimadamore@1342 1675 if (currentSym == syms.noSymbol) {
mcimadamore@1342 1676 currentSym = symbolFor(types.supertype(prevSym.type));
mcimadamore@1342 1677 }
mcimadamore@1335 1678 return currentSym != null;
mcimadamore@1335 1679 }
mcimadamore@1335 1680
mcimadamore@1335 1681 public TypeSymbol next() {
mcimadamore@1342 1682 prevSym = currentSym;
mcimadamore@1342 1683 currentSym = syms.noSymbol;
mcimadamore@1342 1684 Assert.check(prevSym != null || prevSym != syms.noSymbol);
mcimadamore@1335 1685 return prevSym;
mcimadamore@1335 1686 }
mcimadamore@1335 1687
mcimadamore@1335 1688 public void remove() {
mcimadamore@1342 1689 throw new UnsupportedOperationException();
mcimadamore@1335 1690 }
mcimadamore@1335 1691
mcimadamore@1342 1692 TypeSymbol symbolFor(Type t) {
jjg@1374 1693 if (!t.hasTag(CLASS) &&
jjg@1374 1694 !t.hasTag(TYPEVAR)) {
mcimadamore@1335 1695 return null;
mcimadamore@1335 1696 }
jjg@1374 1697 while (t.hasTag(TYPEVAR))
mcimadamore@1342 1698 t = t.getUpperBound();
mcimadamore@1342 1699 if (seen.contains(t.tsym)) {
mcimadamore@1335 1700 //degenerate case in which we have a circular
mcimadamore@1335 1701 //class hierarchy - because of ill-formed classfiles
mcimadamore@1335 1702 return null;
mcimadamore@1335 1703 }
mcimadamore@1342 1704 seen = seen.prepend(t.tsym);
mcimadamore@1342 1705 return t.tsym;
mcimadamore@1335 1706 }
mcimadamore@1335 1707 };
mcimadamore@1335 1708 }
mcimadamore@1335 1709 };
mcimadamore@1335 1710 }
mcimadamore@1335 1711
duke@1 1712 /** Find unqualified method matching given name, type and value arguments.
duke@1 1713 * @param env The current environment.
duke@1 1714 * @param name The method's name.
duke@1 1715 * @param argtypes The method's value arguments.
duke@1 1716 * @param typeargtypes The method's type arguments.
duke@1 1717 * @param allowBoxing Allow boxing conversions of arguments.
duke@1 1718 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 1719 */
duke@1 1720 Symbol findFun(Env<AttrContext> env, Name name,
duke@1 1721 List<Type> argtypes, List<Type> typeargtypes,
duke@1 1722 boolean allowBoxing, boolean useVarargs) {
duke@1 1723 Symbol bestSoFar = methodNotFound;
duke@1 1724 Symbol sym;
duke@1 1725 Env<AttrContext> env1 = env;
duke@1 1726 boolean staticOnly = false;
duke@1 1727 while (env1.outer != null) {
duke@1 1728 if (isStatic(env1)) staticOnly = true;
duke@1 1729 sym = findMethod(
duke@1 1730 env1, env1.enclClass.sym.type, name, argtypes, typeargtypes,
duke@1 1731 allowBoxing, useVarargs, false);
duke@1 1732 if (sym.exists()) {
duke@1 1733 if (staticOnly &&
duke@1 1734 sym.kind == MTH &&
duke@1 1735 sym.owner.kind == TYP &&
duke@1 1736 (sym.flags() & STATIC) == 0) return new StaticError(sym);
duke@1 1737 else return sym;
duke@1 1738 } else if (sym.kind < bestSoFar.kind) {
duke@1 1739 bestSoFar = sym;
duke@1 1740 }
duke@1 1741 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
duke@1 1742 env1 = env1.outer;
duke@1 1743 }
duke@1 1744
duke@1 1745 sym = findMethod(env, syms.predefClass.type, name, argtypes,
duke@1 1746 typeargtypes, allowBoxing, useVarargs, false);
duke@1 1747 if (sym.exists())
duke@1 1748 return sym;
duke@1 1749
duke@1 1750 Scope.Entry e = env.toplevel.namedImportScope.lookup(name);
duke@1 1751 for (; e.scope != null; e = e.next()) {
duke@1 1752 sym = e.sym;
duke@1 1753 Type origin = e.getOrigin().owner.type;
duke@1 1754 if (sym.kind == MTH) {
duke@1 1755 if (e.sym.owner.type != origin)
duke@1 1756 sym = sym.clone(e.getOrigin().owner);
duke@1 1757 if (!isAccessible(env, origin, sym))
duke@1 1758 sym = new AccessError(env, origin, sym);
duke@1 1759 bestSoFar = selectBest(env, origin,
duke@1 1760 argtypes, typeargtypes,
duke@1 1761 sym, bestSoFar,
duke@1 1762 allowBoxing, useVarargs, false);
duke@1 1763 }
duke@1 1764 }
duke@1 1765 if (bestSoFar.exists())
duke@1 1766 return bestSoFar;
duke@1 1767
duke@1 1768 e = env.toplevel.starImportScope.lookup(name);
duke@1 1769 for (; e.scope != null; e = e.next()) {
duke@1 1770 sym = e.sym;
duke@1 1771 Type origin = e.getOrigin().owner.type;
duke@1 1772 if (sym.kind == MTH) {
duke@1 1773 if (e.sym.owner.type != origin)
duke@1 1774 sym = sym.clone(e.getOrigin().owner);
duke@1 1775 if (!isAccessible(env, origin, sym))
duke@1 1776 sym = new AccessError(env, origin, sym);
duke@1 1777 bestSoFar = selectBest(env, origin,
duke@1 1778 argtypes, typeargtypes,
duke@1 1779 sym, bestSoFar,
duke@1 1780 allowBoxing, useVarargs, false);
duke@1 1781 }
duke@1 1782 }
duke@1 1783 return bestSoFar;
duke@1 1784 }
duke@1 1785
duke@1 1786 /** Load toplevel or member class with given fully qualified name and
duke@1 1787 * verify that it is accessible.
duke@1 1788 * @param env The current environment.
duke@1 1789 * @param name The fully qualified name of the class to be loaded.
duke@1 1790 */
duke@1 1791 Symbol loadClass(Env<AttrContext> env, Name name) {
duke@1 1792 try {
duke@1 1793 ClassSymbol c = reader.loadClass(name);
duke@1 1794 return isAccessible(env, c) ? c : new AccessError(c);
duke@1 1795 } catch (ClassReader.BadClassFile err) {
duke@1 1796 throw err;
duke@1 1797 } catch (CompletionFailure ex) {
duke@1 1798 return typeNotFound;
duke@1 1799 }
duke@1 1800 }
duke@1 1801
duke@1 1802 /** Find qualified member type.
duke@1 1803 * @param env The current environment.
duke@1 1804 * @param site The original type from where the selection takes
duke@1 1805 * place.
duke@1 1806 * @param name The type's name.
duke@1 1807 * @param c The class to search for the member type. This is
duke@1 1808 * always a superclass or implemented interface of
duke@1 1809 * site's class.
duke@1 1810 */
duke@1 1811 Symbol findMemberType(Env<AttrContext> env,
duke@1 1812 Type site,
duke@1 1813 Name name,
duke@1 1814 TypeSymbol c) {
duke@1 1815 Symbol bestSoFar = typeNotFound;
duke@1 1816 Symbol sym;
duke@1 1817 Scope.Entry e = c.members().lookup(name);
duke@1 1818 while (e.scope != null) {
duke@1 1819 if (e.sym.kind == TYP) {
duke@1 1820 return isAccessible(env, site, e.sym)
duke@1 1821 ? e.sym
duke@1 1822 : new AccessError(env, site, e.sym);
duke@1 1823 }
duke@1 1824 e = e.next();
duke@1 1825 }
duke@1 1826 Type st = types.supertype(c.type);
jjg@1374 1827 if (st != null && st.hasTag(CLASS)) {
duke@1 1828 sym = findMemberType(env, site, name, st.tsym);
duke@1 1829 if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1830 }
duke@1 1831 for (List<Type> l = types.interfaces(c.type);
duke@1 1832 bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
duke@1 1833 l = l.tail) {
duke@1 1834 sym = findMemberType(env, site, name, l.head.tsym);
duke@1 1835 if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS &&
duke@1 1836 sym.owner != bestSoFar.owner)
duke@1 1837 bestSoFar = new AmbiguityError(bestSoFar, sym);
duke@1 1838 else if (sym.kind < bestSoFar.kind)
duke@1 1839 bestSoFar = sym;
duke@1 1840 }
duke@1 1841 return bestSoFar;
duke@1 1842 }
duke@1 1843
duke@1 1844 /** Find a global type in given scope and load corresponding class.
duke@1 1845 * @param env The current environment.
duke@1 1846 * @param scope The scope in which to look for the type.
duke@1 1847 * @param name The type's name.
duke@1 1848 */
duke@1 1849 Symbol findGlobalType(Env<AttrContext> env, Scope scope, Name name) {
duke@1 1850 Symbol bestSoFar = typeNotFound;
duke@1 1851 for (Scope.Entry e = scope.lookup(name); e.scope != null; e = e.next()) {
duke@1 1852 Symbol sym = loadClass(env, e.sym.flatName());
duke@1 1853 if (bestSoFar.kind == TYP && sym.kind == TYP &&
duke@1 1854 bestSoFar != sym)
duke@1 1855 return new AmbiguityError(bestSoFar, sym);
duke@1 1856 else if (sym.kind < bestSoFar.kind)
duke@1 1857 bestSoFar = sym;
duke@1 1858 }
duke@1 1859 return bestSoFar;
duke@1 1860 }
duke@1 1861
duke@1 1862 /** Find an unqualified type symbol.
duke@1 1863 * @param env The current environment.
duke@1 1864 * @param name The type's name.
duke@1 1865 */
duke@1 1866 Symbol findType(Env<AttrContext> env, Name name) {
duke@1 1867 Symbol bestSoFar = typeNotFound;
duke@1 1868 Symbol sym;
duke@1 1869 boolean staticOnly = false;
duke@1 1870 for (Env<AttrContext> env1 = env; env1.outer != null; env1 = env1.outer) {
duke@1 1871 if (isStatic(env1)) staticOnly = true;
duke@1 1872 for (Scope.Entry e = env1.info.scope.lookup(name);
duke@1 1873 e.scope != null;
duke@1 1874 e = e.next()) {
duke@1 1875 if (e.sym.kind == TYP) {
duke@1 1876 if (staticOnly &&
jjg@1374 1877 e.sym.type.hasTag(TYPEVAR) &&
duke@1 1878 e.sym.owner.kind == TYP) return new StaticError(e.sym);
duke@1 1879 return e.sym;
duke@1 1880 }
duke@1 1881 }
duke@1 1882
duke@1 1883 sym = findMemberType(env1, env1.enclClass.sym.type, name,
duke@1 1884 env1.enclClass.sym);
duke@1 1885 if (staticOnly && sym.kind == TYP &&
jjg@1374 1886 sym.type.hasTag(CLASS) &&
jjg@1374 1887 sym.type.getEnclosingType().hasTag(CLASS) &&
duke@1 1888 env1.enclClass.sym.type.isParameterized() &&
duke@1 1889 sym.type.getEnclosingType().isParameterized())
duke@1 1890 return new StaticError(sym);
duke@1 1891 else if (sym.exists()) return sym;
duke@1 1892 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1893
duke@1 1894 JCClassDecl encl = env1.baseClause ? (JCClassDecl)env1.tree : env1.enclClass;
duke@1 1895 if ((encl.sym.flags() & STATIC) != 0)
duke@1 1896 staticOnly = true;
duke@1 1897 }
duke@1 1898
jjg@1127 1899 if (!env.tree.hasTag(IMPORT)) {
duke@1 1900 sym = findGlobalType(env, env.toplevel.namedImportScope, name);
duke@1 1901 if (sym.exists()) return sym;
duke@1 1902 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1903
duke@1 1904 sym = findGlobalType(env, env.toplevel.packge.members(), name);
duke@1 1905 if (sym.exists()) return sym;
duke@1 1906 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1907
duke@1 1908 sym = findGlobalType(env, env.toplevel.starImportScope, name);
duke@1 1909 if (sym.exists()) return sym;
duke@1 1910 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1911 }
duke@1 1912
duke@1 1913 return bestSoFar;
duke@1 1914 }
duke@1 1915
duke@1 1916 /** Find an unqualified identifier which matches a specified kind set.
duke@1 1917 * @param env The current environment.
jjg@1409 1918 * @param name The identifier's name.
duke@1 1919 * @param kind Indicates the possible symbol kinds
duke@1 1920 * (a subset of VAL, TYP, PCK).
duke@1 1921 */
duke@1 1922 Symbol findIdent(Env<AttrContext> env, Name name, int kind) {
duke@1 1923 Symbol bestSoFar = typeNotFound;
duke@1 1924 Symbol sym;
duke@1 1925
duke@1 1926 if ((kind & VAR) != 0) {
duke@1 1927 sym = findVar(env, name);
duke@1 1928 if (sym.exists()) return sym;
duke@1 1929 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1930 }
duke@1 1931
duke@1 1932 if ((kind & TYP) != 0) {
duke@1 1933 sym = findType(env, name);
ohrstrom@1460 1934 if (sym.kind==TYP) {
ohrstrom@1460 1935 reportDependence(env.enclClass.sym, sym);
ohrstrom@1460 1936 }
duke@1 1937 if (sym.exists()) return sym;
duke@1 1938 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1939 }
duke@1 1940
duke@1 1941 if ((kind & PCK) != 0) return reader.enterPackage(name);
duke@1 1942 else return bestSoFar;
duke@1 1943 }
duke@1 1944
ohrstrom@1460 1945 /** Report dependencies.
ohrstrom@1460 1946 * @param from The enclosing class sym
ohrstrom@1460 1947 * @param to The found identifier that the class depends on.
ohrstrom@1460 1948 */
ohrstrom@1460 1949 public void reportDependence(Symbol from, Symbol to) {
ohrstrom@1460 1950 // Override if you want to collect the reported dependencies.
ohrstrom@1460 1951 }
ohrstrom@1460 1952
duke@1 1953 /** Find an identifier in a package which matches a specified kind set.
duke@1 1954 * @param env The current environment.
duke@1 1955 * @param name The identifier's name.
duke@1 1956 * @param kind Indicates the possible symbol kinds
duke@1 1957 * (a nonempty subset of TYP, PCK).
duke@1 1958 */
duke@1 1959 Symbol findIdentInPackage(Env<AttrContext> env, TypeSymbol pck,
duke@1 1960 Name name, int kind) {
duke@1 1961 Name fullname = TypeSymbol.formFullName(name, pck);
duke@1 1962 Symbol bestSoFar = typeNotFound;
duke@1 1963 PackageSymbol pack = null;
duke@1 1964 if ((kind & PCK) != 0) {
duke@1 1965 pack = reader.enterPackage(fullname);
duke@1 1966 if (pack.exists()) return pack;
duke@1 1967 }
duke@1 1968 if ((kind & TYP) != 0) {
duke@1 1969 Symbol sym = loadClass(env, fullname);
duke@1 1970 if (sym.exists()) {
duke@1 1971 // don't allow programs to use flatnames
duke@1 1972 if (name == sym.name) return sym;
duke@1 1973 }
duke@1 1974 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1975 }
duke@1 1976 return (pack != null) ? pack : bestSoFar;
duke@1 1977 }
duke@1 1978
duke@1 1979 /** Find an identifier among the members of a given type `site'.
duke@1 1980 * @param env The current environment.
duke@1 1981 * @param site The type containing the symbol to be found.
duke@1 1982 * @param name The identifier's name.
duke@1 1983 * @param kind Indicates the possible symbol kinds
duke@1 1984 * (a subset of VAL, TYP).
duke@1 1985 */
duke@1 1986 Symbol findIdentInType(Env<AttrContext> env, Type site,
duke@1 1987 Name name, int kind) {
duke@1 1988 Symbol bestSoFar = typeNotFound;
duke@1 1989 Symbol sym;
duke@1 1990 if ((kind & VAR) != 0) {
duke@1 1991 sym = findField(env, site, name, site.tsym);
duke@1 1992 if (sym.exists()) return sym;
duke@1 1993 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1994 }
duke@1 1995
duke@1 1996 if ((kind & TYP) != 0) {
duke@1 1997 sym = findMemberType(env, site, name, site.tsym);
duke@1 1998 if (sym.exists()) return sym;
duke@1 1999 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 2000 }
duke@1 2001 return bestSoFar;
duke@1 2002 }
duke@1 2003
duke@1 2004 /* ***************************************************************************
duke@1 2005 * Access checking
duke@1 2006 * The following methods convert ResolveErrors to ErrorSymbols, issuing
duke@1 2007 * an error message in the process
duke@1 2008 ****************************************************************************/
duke@1 2009
duke@1 2010 /** If `sym' is a bad symbol: report error and return errSymbol
duke@1 2011 * else pass through unchanged,
duke@1 2012 * additional arguments duplicate what has been used in trying to find the
jjg@1326 2013 * symbol {@literal (--> flyweight pattern)}. This improves performance since we
duke@1 2014 * expect misses to happen frequently.
duke@1 2015 *
duke@1 2016 * @param sym The symbol that was found, or a ResolveError.
duke@1 2017 * @param pos The position to use for error reporting.
mcimadamore@1347 2018 * @param location The symbol the served as a context for this lookup
duke@1 2019 * @param site The original type from where the selection took place.
duke@1 2020 * @param name The symbol's name.
mcimadamore@1347 2021 * @param qualified Did we get here through a qualified expression resolution?
duke@1 2022 * @param argtypes The invocation's value arguments,
duke@1 2023 * if we looked for a method.
duke@1 2024 * @param typeargtypes The invocation's type arguments,
duke@1 2025 * if we looked for a method.
mcimadamore@1347 2026 * @param logResolveHelper helper class used to log resolve errors
duke@1 2027 */
mcimadamore@1347 2028 Symbol accessInternal(Symbol sym,
mcimadamore@1347 2029 DiagnosticPosition pos,
mcimadamore@1347 2030 Symbol location,
mcimadamore@1347 2031 Type site,
mcimadamore@1347 2032 Name name,
mcimadamore@1347 2033 boolean qualified,
mcimadamore@1347 2034 List<Type> argtypes,
mcimadamore@1347 2035 List<Type> typeargtypes,
mcimadamore@1347 2036 LogResolveHelper logResolveHelper) {
mcimadamore@1347 2037 if (sym.kind >= AMBIGUOUS) {
mcimadamore@1347 2038 ResolveError errSym = (ResolveError)sym;
mcimadamore@1347 2039 sym = errSym.access(name, qualified ? site.tsym : syms.noSymbol);
mcimadamore@1347 2040 argtypes = logResolveHelper.getArgumentTypes(errSym, sym, name, argtypes);
mcimadamore@1347 2041 if (logResolveHelper.resolveDiagnosticNeeded(site, argtypes, typeargtypes)) {
mcimadamore@1347 2042 logResolveError(errSym, pos, location, site, name, argtypes, typeargtypes);
mcimadamore@1347 2043 }
mcimadamore@1347 2044 }
mcimadamore@1347 2045 return sym;
mcimadamore@1347 2046 }
mcimadamore@1347 2047
mcimadamore@1347 2048 /**
mcimadamore@1347 2049 * Variant of the generalized access routine, to be used for generating method
mcimadamore@1347 2050 * resolution diagnostics
mcimadamore@1347 2051 */
mcimadamore@1347 2052 Symbol accessMethod(Symbol sym,
duke@1 2053 DiagnosticPosition pos,
mcimadamore@829 2054 Symbol location,
duke@1 2055 Type site,
duke@1 2056 Name name,
duke@1 2057 boolean qualified,
duke@1 2058 List<Type> argtypes,
duke@1 2059 List<Type> typeargtypes) {
mcimadamore@1347 2060 return accessInternal(sym, pos, location, site, name, qualified, argtypes, typeargtypes, methodLogResolveHelper);
duke@1 2061 }
duke@1 2062
mcimadamore@1347 2063 /** Same as original accessMethod(), but without location.
mcimadamore@829 2064 */
mcimadamore@1347 2065 Symbol accessMethod(Symbol sym,
mcimadamore@829 2066 DiagnosticPosition pos,
mcimadamore@829 2067 Type site,
mcimadamore@829 2068 Name name,
mcimadamore@829 2069 boolean qualified,
mcimadamore@829 2070 List<Type> argtypes,
mcimadamore@829 2071 List<Type> typeargtypes) {
mcimadamore@1347 2072 return accessMethod(sym, pos, site.tsym, site, name, qualified, argtypes, typeargtypes);
mcimadamore@829 2073 }
mcimadamore@829 2074
mcimadamore@1347 2075 /**
mcimadamore@1347 2076 * Variant of the generalized access routine, to be used for generating variable,
mcimadamore@1347 2077 * type resolution diagnostics
mcimadamore@829 2078 */
mcimadamore@1347 2079 Symbol accessBase(Symbol sym,
mcimadamore@829 2080 DiagnosticPosition pos,
mcimadamore@829 2081 Symbol location,
mcimadamore@829 2082 Type site,
mcimadamore@829 2083 Name name,
mcimadamore@829 2084 boolean qualified) {
mcimadamore@1347 2085 return accessInternal(sym, pos, location, site, name, qualified, List.<Type>nil(), null, basicLogResolveHelper);
mcimadamore@829 2086 }
mcimadamore@829 2087
mcimadamore@1347 2088 /** Same as original accessBase(), but without location.
duke@1 2089 */
mcimadamore@1347 2090 Symbol accessBase(Symbol sym,
duke@1 2091 DiagnosticPosition pos,
duke@1 2092 Type site,
duke@1 2093 Name name,
duke@1 2094 boolean qualified) {
mcimadamore@1347 2095 return accessBase(sym, pos, site.tsym, site, name, qualified);
duke@1 2096 }
duke@1 2097
mcimadamore@1347 2098 interface LogResolveHelper {
mcimadamore@1347 2099 boolean resolveDiagnosticNeeded(Type site, List<Type> argtypes, List<Type> typeargtypes);
mcimadamore@1347 2100 List<Type> getArgumentTypes(ResolveError errSym, Symbol accessedSym, Name name, List<Type> argtypes);
mcimadamore@1347 2101 }
mcimadamore@1347 2102
mcimadamore@1347 2103 LogResolveHelper basicLogResolveHelper = new LogResolveHelper() {
mcimadamore@1347 2104 public boolean resolveDiagnosticNeeded(Type site, List<Type> argtypes, List<Type> typeargtypes) {
mcimadamore@1347 2105 return !site.isErroneous();
mcimadamore@1347 2106 }
mcimadamore@1347 2107 public List<Type> getArgumentTypes(ResolveError errSym, Symbol accessedSym, Name name, List<Type> argtypes) {
mcimadamore@1347 2108 return argtypes;
mcimadamore@1347 2109 }
mcimadamore@1347 2110 };
mcimadamore@1347 2111
mcimadamore@1347 2112 LogResolveHelper methodLogResolveHelper = new LogResolveHelper() {
mcimadamore@1347 2113 public boolean resolveDiagnosticNeeded(Type site, List<Type> argtypes, List<Type> typeargtypes) {
mcimadamore@1347 2114 return !site.isErroneous() &&
mcimadamore@1347 2115 !Type.isErroneous(argtypes) &&
mcimadamore@1347 2116 (typeargtypes == null || !Type.isErroneous(typeargtypes));
mcimadamore@1347 2117 }
mcimadamore@1347 2118 public List<Type> getArgumentTypes(ResolveError errSym, Symbol accessedSym, Name name, List<Type> argtypes) {
mcimadamore@1415 2119 return (syms.operatorNames.contains(name)) ?
mcimadamore@1415 2120 argtypes :
mcimadamore@1415 2121 Type.map(argtypes, new ResolveDeferredRecoveryMap(accessedSym));
mcimadamore@1415 2122 }
mcimadamore@1415 2123
mcimadamore@1415 2124 class ResolveDeferredRecoveryMap extends DeferredAttr.RecoveryDeferredTypeMap {
mcimadamore@1415 2125
mcimadamore@1415 2126 public ResolveDeferredRecoveryMap(Symbol msym) {
mcimadamore@1415 2127 deferredAttr.super(AttrMode.SPECULATIVE, msym, currentResolutionContext.step);
mcimadamore@1415 2128 }
mcimadamore@1415 2129
mcimadamore@1415 2130 @Override
mcimadamore@1415 2131 protected Type typeOf(DeferredType dt) {
mcimadamore@1415 2132 Type res = super.typeOf(dt);
mcimadamore@1415 2133 if (!res.isErroneous()) {
mcimadamore@1415 2134 switch (TreeInfo.skipParens(dt.tree).getTag()) {
mcimadamore@1415 2135 case LAMBDA:
mcimadamore@1415 2136 case REFERENCE:
mcimadamore@1415 2137 return dt;
mcimadamore@1415 2138 case CONDEXPR:
mcimadamore@1415 2139 return res == Type.recoveryType ?
mcimadamore@1415 2140 dt : res;
mcimadamore@1347 2141 }
mcimadamore@1347 2142 }
mcimadamore@1415 2143 return res;
mcimadamore@1347 2144 }
mcimadamore@1347 2145 }
mcimadamore@1347 2146 };
mcimadamore@1347 2147
duke@1 2148 /** Check that sym is not an abstract method.
duke@1 2149 */
duke@1 2150 void checkNonAbstract(DiagnosticPosition pos, Symbol sym) {
mcimadamore@1393 2151 if ((sym.flags() & ABSTRACT) != 0 && (sym.flags() & DEFAULT) == 0)
duke@1 2152 log.error(pos, "abstract.cant.be.accessed.directly",
duke@1 2153 kindName(sym), sym, sym.location());
duke@1 2154 }
duke@1 2155
duke@1 2156 /* ***************************************************************************
duke@1 2157 * Debugging
duke@1 2158 ****************************************************************************/
duke@1 2159
duke@1 2160 /** print all scopes starting with scope s and proceeding outwards.
duke@1 2161 * used for debugging.
duke@1 2162 */
duke@1 2163 public void printscopes(Scope s) {
duke@1 2164 while (s != null) {
duke@1 2165 if (s.owner != null)
duke@1 2166 System.err.print(s.owner + ": ");
duke@1 2167 for (Scope.Entry e = s.elems; e != null; e = e.sibling) {
duke@1 2168 if ((e.sym.flags() & ABSTRACT) != 0)
duke@1 2169 System.err.print("abstract ");
duke@1 2170 System.err.print(e.sym + " ");
duke@1 2171 }
duke@1 2172 System.err.println();
duke@1 2173 s = s.next;
duke@1 2174 }
duke@1 2175 }
duke@1 2176
duke@1 2177 void printscopes(Env<AttrContext> env) {
duke@1 2178 while (env.outer != null) {
duke@1 2179 System.err.println("------------------------------");
duke@1 2180 printscopes(env.info.scope);
duke@1 2181 env = env.outer;
duke@1 2182 }
duke@1 2183 }
duke@1 2184
duke@1 2185 public void printscopes(Type t) {
jjg@1374 2186 while (t.hasTag(CLASS)) {
duke@1 2187 printscopes(t.tsym.members());
duke@1 2188 t = types.supertype(t);
duke@1 2189 }
duke@1 2190 }
duke@1 2191
duke@1 2192 /* ***************************************************************************
duke@1 2193 * Name resolution
duke@1 2194 * Naming conventions are as for symbol lookup
duke@1 2195 * Unlike the find... methods these methods will report access errors
duke@1 2196 ****************************************************************************/
duke@1 2197
duke@1 2198 /** Resolve an unqualified (non-method) identifier.
duke@1 2199 * @param pos The position to use for error reporting.
duke@1 2200 * @param env The environment current at the identifier use.
duke@1 2201 * @param name The identifier's name.
duke@1 2202 * @param kind The set of admissible symbol kinds for the identifier.
duke@1 2203 */
duke@1 2204 Symbol resolveIdent(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 2205 Name name, int kind) {
mcimadamore@1347 2206 return accessBase(
duke@1 2207 findIdent(env, name, kind),
duke@1 2208 pos, env.enclClass.sym.type, name, false);
duke@1 2209 }
duke@1 2210
duke@1 2211 /** Resolve an unqualified method identifier.
duke@1 2212 * @param pos The position to use for error reporting.
duke@1 2213 * @param env The environment current at the method invocation.
duke@1 2214 * @param name The identifier's name.
duke@1 2215 * @param argtypes The types of the invocation's value arguments.
duke@1 2216 * @param typeargtypes The types of the invocation's type arguments.
duke@1 2217 */
duke@1 2218 Symbol resolveMethod(DiagnosticPosition pos,
duke@1 2219 Env<AttrContext> env,
duke@1 2220 Name name,
duke@1 2221 List<Type> argtypes,
duke@1 2222 List<Type> typeargtypes) {
mcimadamore@1674 2223 return lookupMethod(env, pos, env.enclClass.sym, resolveMethodCheck,
mcimadamore@1674 2224 new BasicLookupHelper(name, env.enclClass.sym.type, argtypes, typeargtypes) {
mcimadamore@1674 2225 @Override
mcimadamore@1875 2226 Symbol doLookup(Env<AttrContext> env, MethodResolutionPhase phase) {
mcimadamore@1674 2227 return findFun(env, name, argtypes, typeargtypes,
mcimadamore@1674 2228 phase.isBoxingRequired(),
mcimadamore@1674 2229 phase.isVarargsRequired());
mcimadamore@1674 2230 }});
mcimadamore@689 2231 }
mcimadamore@689 2232
duke@1 2233 /** Resolve a qualified method identifier
duke@1 2234 * @param pos The position to use for error reporting.
duke@1 2235 * @param env The environment current at the method invocation.
duke@1 2236 * @param site The type of the qualifying expression, in which
duke@1 2237 * identifier is searched.
duke@1 2238 * @param name The identifier's name.
duke@1 2239 * @param argtypes The types of the invocation's value arguments.
duke@1 2240 * @param typeargtypes The types of the invocation's type arguments.
duke@1 2241 */
duke@1 2242 Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 2243 Type site, Name name, List<Type> argtypes,
duke@1 2244 List<Type> typeargtypes) {
mcimadamore@829 2245 return resolveQualifiedMethod(pos, env, site.tsym, site, name, argtypes, typeargtypes);
mcimadamore@829 2246 }
mcimadamore@829 2247 Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env,
mcimadamore@829 2248 Symbol location, Type site, Name name, List<Type> argtypes,
mcimadamore@829 2249 List<Type> typeargtypes) {
mcimadamore@1215 2250 return resolveQualifiedMethod(new MethodResolutionContext(), pos, env, location, site, name, argtypes, typeargtypes);
mcimadamore@1215 2251 }
mcimadamore@1215 2252 private Symbol resolveQualifiedMethod(MethodResolutionContext resolveContext,
mcimadamore@1215 2253 DiagnosticPosition pos, Env<AttrContext> env,
mcimadamore@1215 2254 Symbol location, Type site, Name name, List<Type> argtypes,
mcimadamore@1215 2255 List<Type> typeargtypes) {
mcimadamore@1394 2256 return lookupMethod(env, pos, location, resolveContext, new BasicLookupHelper(name, site, argtypes, typeargtypes) {
mcimadamore@1394 2257 @Override
mcimadamore@1875 2258 Symbol doLookup(Env<AttrContext> env, MethodResolutionPhase phase) {
mcimadamore@1394 2259 return findMethod(env, site, name, argtypes, typeargtypes,
mcimadamore@1394 2260 phase.isBoxingRequired(),
mcimadamore@1394 2261 phase.isVarargsRequired(), false);
mcimadamore@1215 2262 }
mcimadamore@1394 2263 @Override
mcimadamore@1394 2264 Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym) {
mcimadamore@1394 2265 if (sym.kind >= AMBIGUOUS) {
mcimadamore@1394 2266 sym = super.access(env, pos, location, sym);
mcimadamore@1394 2267 } else if (allowMethodHandles) {
mcimadamore@1394 2268 MethodSymbol msym = (MethodSymbol)sym;
vromero@1820 2269 if ((msym.flags() & SIGNATURE_POLYMORPHIC) != 0) {
mcimadamore@1394 2270 return findPolymorphicSignatureInstance(env, sym, argtypes);
mcimadamore@1394 2271 }
mcimadamore@1215 2272 }
mcimadamore@1394 2273 return sym;
mcimadamore@674 2274 }
mcimadamore@1394 2275 });
duke@1 2276 }
duke@1 2277
mcimadamore@674 2278 /** Find or create an implicit method of exactly the given type (after erasure).
mcimadamore@674 2279 * Searches in a side table, not the main scope of the site.
mcimadamore@674 2280 * This emulates the lookup process required by JSR 292 in JVM.
mcimadamore@674 2281 * @param env Attribution environment
mcimadamore@1239 2282 * @param spMethod signature polymorphic method - i.e. MH.invokeExact
mcimadamore@1239 2283 * @param argtypes The required argument types
mcimadamore@674 2284 */
mcimadamore@1239 2285 Symbol findPolymorphicSignatureInstance(Env<AttrContext> env,
mcimadamore@1415 2286 final Symbol spMethod,
mcimadamore@820 2287 List<Type> argtypes) {
mcimadamore@674 2288 Type mtype = infer.instantiatePolymorphicSignatureInstance(env,
mcimadamore@1347 2289 (MethodSymbol)spMethod, currentResolutionContext, argtypes);
mcimadamore@1239 2290 for (Symbol sym : polymorphicSignatureScope.getElementsByName(spMethod.name)) {
mcimadamore@1239 2291 if (types.isSameType(mtype, sym.type)) {
mcimadamore@1239 2292 return sym;
mcimadamore@674 2293 }
mcimadamore@674 2294 }
mcimadamore@1239 2295
mcimadamore@1239 2296 // create the desired method
mcimadamore@1239 2297 long flags = ABSTRACT | HYPOTHETICAL | spMethod.flags() & Flags.AccessFlags;
mcimadamore@1415 2298 Symbol msym = new MethodSymbol(flags, spMethod.name, mtype, spMethod.owner) {
mcimadamore@1415 2299 @Override
mcimadamore@1415 2300 public Symbol baseSymbol() {
mcimadamore@1415 2301 return spMethod;
mcimadamore@1415 2302 }
mcimadamore@1415 2303 };
mcimadamore@1239 2304 polymorphicSignatureScope.enter(msym);
mcimadamore@1239 2305 return msym;
mcimadamore@674 2306 }
mcimadamore@674 2307
duke@1 2308 /** Resolve a qualified method identifier, throw a fatal error if not
duke@1 2309 * found.
duke@1 2310 * @param pos The position to use for error reporting.
duke@1 2311 * @param env The environment current at the method invocation.
duke@1 2312 * @param site The type of the qualifying expression, in which
duke@1 2313 * identifier is searched.
duke@1 2314 * @param name The identifier's name.
duke@1 2315 * @param argtypes The types of the invocation's value arguments.
duke@1 2316 * @param typeargtypes The types of the invocation's type arguments.
duke@1 2317 */
duke@1 2318 public MethodSymbol resolveInternalMethod(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 2319 Type site, Name name,
duke@1 2320 List<Type> argtypes,
duke@1 2321 List<Type> typeargtypes) {
mcimadamore@1215 2322 MethodResolutionContext resolveContext = new MethodResolutionContext();
mcimadamore@1215 2323 resolveContext.internalResolution = true;
mcimadamore@1215 2324 Symbol sym = resolveQualifiedMethod(resolveContext, pos, env, site.tsym,
mcimadamore@1215 2325 site, name, argtypes, typeargtypes);
mcimadamore@1215 2326 if (sym.kind == MTH) return (MethodSymbol)sym;
mcimadamore@1215 2327 else throw new FatalError(
mcimadamore@1215 2328 diags.fragment("fatal.err.cant.locate.meth",
mcimadamore@1215 2329 name));
duke@1 2330 }
duke@1 2331
duke@1 2332 /** Resolve constructor.
duke@1 2333 * @param pos The position to use for error reporting.
duke@1 2334 * @param env The environment current at the constructor invocation.
duke@1 2335 * @param site The type of class for which a constructor is searched.
duke@1 2336 * @param argtypes The types of the constructor invocation's value
duke@1 2337 * arguments.
duke@1 2338 * @param typeargtypes The types of the constructor invocation's type
duke@1 2339 * arguments.
duke@1 2340 */
duke@1 2341 Symbol resolveConstructor(DiagnosticPosition pos,
duke@1 2342 Env<AttrContext> env,
duke@1 2343 Type site,
duke@1 2344 List<Type> argtypes,
duke@1 2345 List<Type> typeargtypes) {
mcimadamore@1215 2346 return resolveConstructor(new MethodResolutionContext(), pos, env, site, argtypes, typeargtypes);
mcimadamore@1215 2347 }
mcimadamore@1394 2348
mcimadamore@1215 2349 private Symbol resolveConstructor(MethodResolutionContext resolveContext,
mcimadamore@1394 2350 final DiagnosticPosition pos,
mcimadamore@1215 2351 Env<AttrContext> env,
mcimadamore@1215 2352 Type site,
mcimadamore@1215 2353 List<Type> argtypes,
mcimadamore@1215 2354 List<Type> typeargtypes) {
mcimadamore@1394 2355 return lookupMethod(env, pos, site.tsym, resolveContext, new BasicLookupHelper(names.init, site, argtypes, typeargtypes) {
mcimadamore@1394 2356 @Override
mcimadamore@1875 2357 Symbol doLookup(Env<AttrContext> env, MethodResolutionPhase phase) {
mcimadamore@1394 2358 return findConstructor(pos, env, site, argtypes, typeargtypes,
mcimadamore@1394 2359 phase.isBoxingRequired(),
mcimadamore@1394 2360 phase.isVarargsRequired());
mcimadamore@1215 2361 }
mcimadamore@1394 2362 });
mcimadamore@1394 2363 }
mcimadamore@1394 2364
mcimadamore@1394 2365 /** Resolve a constructor, throw a fatal error if not found.
mcimadamore@1394 2366 * @param pos The position to use for error reporting.
mcimadamore@1394 2367 * @param env The environment current at the method invocation.
mcimadamore@1394 2368 * @param site The type to be constructed.
mcimadamore@1394 2369 * @param argtypes The types of the invocation's value arguments.
mcimadamore@1394 2370 * @param typeargtypes The types of the invocation's type arguments.
mcimadamore@1394 2371 */
mcimadamore@1394 2372 public MethodSymbol resolveInternalConstructor(DiagnosticPosition pos, Env<AttrContext> env,
mcimadamore@1394 2373 Type site,
mcimadamore@1394 2374 List<Type> argtypes,
mcimadamore@1394 2375 List<Type> typeargtypes) {
mcimadamore@1394 2376 MethodResolutionContext resolveContext = new MethodResolutionContext();
mcimadamore@1394 2377 resolveContext.internalResolution = true;
mcimadamore@1394 2378 Symbol sym = resolveConstructor(resolveContext, pos, env, site, argtypes, typeargtypes);
mcimadamore@1394 2379 if (sym.kind == MTH) return (MethodSymbol)sym;
mcimadamore@1394 2380 else throw new FatalError(
mcimadamore@1394 2381 diags.fragment("fatal.err.cant.locate.ctor", site));
mcimadamore@1394 2382 }
mcimadamore@1394 2383
mcimadamore@1394 2384 Symbol findConstructor(DiagnosticPosition pos, Env<AttrContext> env,
mcimadamore@1394 2385 Type site, List<Type> argtypes,
mcimadamore@1394 2386 List<Type> typeargtypes,
mcimadamore@1394 2387 boolean allowBoxing,
mcimadamore@1394 2388 boolean useVarargs) {
mcimadamore@1394 2389 Symbol sym = findMethod(env, site,
mcimadamore@1394 2390 names.init, argtypes,
mcimadamore@1394 2391 typeargtypes, allowBoxing,
mcimadamore@1394 2392 useVarargs, false);
mcimadamore@1394 2393 chk.checkDeprecated(pos, env.info.scope.owner, sym);
mcimadamore@1394 2394 return sym;
duke@1 2395 }
duke@1 2396
mcimadamore@537 2397 /** Resolve constructor using diamond inference.
mcimadamore@537 2398 * @param pos The position to use for error reporting.
mcimadamore@537 2399 * @param env The environment current at the constructor invocation.
mcimadamore@537 2400 * @param site The type of class for which a constructor is searched.
mcimadamore@537 2401 * The scope of this class has been touched in attribution.
mcimadamore@537 2402 * @param argtypes The types of the constructor invocation's value
mcimadamore@537 2403 * arguments.
mcimadamore@537 2404 * @param typeargtypes The types of the constructor invocation's type
mcimadamore@537 2405 * arguments.
mcimadamore@537 2406 */
mcimadamore@537 2407 Symbol resolveDiamond(DiagnosticPosition pos,
mcimadamore@537 2408 Env<AttrContext> env,
mcimadamore@537 2409 Type site,
mcimadamore@537 2410 List<Type> argtypes,
mcimadamore@631 2411 List<Type> typeargtypes) {
mcimadamore@1674 2412 return lookupMethod(env, pos, site.tsym, resolveMethodCheck,
mcimadamore@1674 2413 new BasicLookupHelper(names.init, site, argtypes, typeargtypes) {
mcimadamore@1674 2414 @Override
mcimadamore@1875 2415 Symbol doLookup(Env<AttrContext> env, MethodResolutionPhase phase) {
mcimadamore@1674 2416 return findDiamond(env, site, argtypes, typeargtypes,
mcimadamore@1674 2417 phase.isBoxingRequired(),
mcimadamore@1674 2418 phase.isVarargsRequired());
mcimadamore@1674 2419 }
mcimadamore@1674 2420 @Override
mcimadamore@1674 2421 Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym) {
mcimadamore@1674 2422 if (sym.kind >= AMBIGUOUS) {
mcimadamore@1674 2423 final JCDiagnostic details = sym.kind == WRONG_MTH ?
mcimadamore@1674 2424 ((InapplicableSymbolError)sym).errCandidate().details :
mcimadamore@1674 2425 null;
mcimadamore@1674 2426 sym = new InapplicableSymbolError(sym.kind, "diamondError", currentResolutionContext) {
mcimadamore@1674 2427 @Override
mcimadamore@1674 2428 JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos,
mcimadamore@1674 2429 Symbol location, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) {
mcimadamore@1674 2430 String key = details == null ?
mcimadamore@1674 2431 "cant.apply.diamond" :
mcimadamore@1674 2432 "cant.apply.diamond.1";
mcimadamore@1674 2433 return diags.create(dkind, log.currentSource(), pos, key,
mcimadamore@1674 2434 diags.fragment("diamond", site.tsym), details);
mcimadamore@1674 2435 }
mcimadamore@1674 2436 };
mcimadamore@1674 2437 sym = accessMethod(sym, pos, site, names.init, true, argtypes, typeargtypes);
mcimadamore@1674 2438 env.info.pendingResolutionPhase = currentResolutionContext.step;
mcimadamore@1394 2439 }
mcimadamore@1674 2440 return sym;
mcimadamore@1674 2441 }});
mcimadamore@537 2442 }
mcimadamore@537 2443
mcimadamore@1217 2444 /** This method scans all the constructor symbol in a given class scope -
mcimadamore@1217 2445 * assuming that the original scope contains a constructor of the kind:
jjg@1326 2446 * {@code Foo(X x, Y y)}, where X,Y are class type-variables declared in Foo,
mcimadamore@1217 2447 * a method check is executed against the modified constructor type:
jjg@1326 2448 * {@code <X,Y>Foo<X,Y>(X x, Y y)}. This is crucial in order to enable diamond
mcimadamore@1217 2449 * inference. The inferred return type of the synthetic constructor IS
mcimadamore@1217 2450 * the inferred type for the diamond operator.
mcimadamore@1217 2451 */
mcimadamore@1217 2452 private Symbol findDiamond(Env<AttrContext> env,
mcimadamore@1217 2453 Type site,
mcimadamore@1217 2454 List<Type> argtypes,
mcimadamore@1217 2455 List<Type> typeargtypes,
mcimadamore@1217 2456 boolean allowBoxing,
mcimadamore@1217 2457 boolean useVarargs) {
mcimadamore@1217 2458 Symbol bestSoFar = methodNotFound;
mcimadamore@1217 2459 for (Scope.Entry e = site.tsym.members().lookup(names.init);
mcimadamore@1217 2460 e.scope != null;
mcimadamore@1217 2461 e = e.next()) {
mcimadamore@1341 2462 final Symbol sym = e.sym;
mcimadamore@1217 2463 //- System.out.println(" e " + e.sym);
mcimadamore@1341 2464 if (sym.kind == MTH &&
mcimadamore@1341 2465 (sym.flags_field & SYNTHETIC) == 0) {
jjg@1374 2466 List<Type> oldParams = e.sym.type.hasTag(FORALL) ?
mcimadamore@1341 2467 ((ForAll)sym.type).tvars :
mcimadamore@1217 2468 List.<Type>nil();
mcimadamore@1217 2469 Type constrType = new ForAll(site.tsym.type.getTypeArguments().appendList(oldParams),
mcimadamore@1341 2470 types.createMethodTypeWithReturn(sym.type.asMethodType(), site));
mcimadamore@1341 2471 MethodSymbol newConstr = new MethodSymbol(sym.flags(), names.init, constrType, site.tsym) {
mcimadamore@1341 2472 @Override
mcimadamore@1341 2473 public Symbol baseSymbol() {
mcimadamore@1341 2474 return sym;
mcimadamore@1341 2475 }
mcimadamore@1341 2476 };
mcimadamore@1217 2477 bestSoFar = selectBest(env, site, argtypes, typeargtypes,
mcimadamore@1341 2478 newConstr,
mcimadamore@1217 2479 bestSoFar,
mcimadamore@1217 2480 allowBoxing,
mcimadamore@1217 2481 useVarargs,
mcimadamore@1217 2482 false);
mcimadamore@1217 2483 }
mcimadamore@1217 2484 }
mcimadamore@1217 2485 return bestSoFar;
mcimadamore@1217 2486 }
mcimadamore@1217 2487
mcimadamore@1394 2488
mcimadamore@1394 2489
mcimadamore@1394 2490 /** Resolve operator.
mcimadamore@1394 2491 * @param pos The position to use for error reporting.
mcimadamore@1394 2492 * @param optag The tag of the operation tree.
mcimadamore@1394 2493 * @param env The environment current at the operation.
mcimadamore@1394 2494 * @param argtypes The types of the operands.
mcimadamore@1394 2495 */
mcimadamore@1394 2496 Symbol resolveOperator(DiagnosticPosition pos, JCTree.Tag optag,
mcimadamore@1394 2497 Env<AttrContext> env, List<Type> argtypes) {
mcimadamore@1394 2498 MethodResolutionContext prevResolutionContext = currentResolutionContext;
mcimadamore@1394 2499 try {
mcimadamore@1394 2500 currentResolutionContext = new MethodResolutionContext();
mcimadamore@1394 2501 Name name = treeinfo.operatorName(optag);
mcimadamore@1779 2502 return lookupMethod(env, pos, syms.predefClass, currentResolutionContext,
mcimadamore@1779 2503 new BasicLookupHelper(name, syms.predefClass.type, argtypes, null, BOX) {
mcimadamore@1779 2504 @Override
mcimadamore@1875 2505 Symbol doLookup(Env<AttrContext> env, MethodResolutionPhase phase) {
mcimadamore@1779 2506 return findMethod(env, site, name, argtypes, typeargtypes,
mcimadamore@1779 2507 phase.isBoxingRequired(),
mcimadamore@1779 2508 phase.isVarargsRequired(), true);
mcimadamore@1779 2509 }
mcimadamore@1779 2510 @Override
mcimadamore@1779 2511 Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym) {
mcimadamore@1779 2512 return accessMethod(sym, pos, env.enclClass.sym.type, name,
mcimadamore@1394 2513 false, argtypes, null);
mcimadamore@1779 2514 }
mcimadamore@1779 2515 });
mcimadamore@1779 2516 } finally {
mcimadamore@1394 2517 currentResolutionContext = prevResolutionContext;
mcimadamore@1394 2518 }
mcimadamore@1394 2519 }
mcimadamore@1394 2520
mcimadamore@1394 2521 /** Resolve operator.
mcimadamore@1394 2522 * @param pos The position to use for error reporting.
mcimadamore@1394 2523 * @param optag The tag of the operation tree.
mcimadamore@1394 2524 * @param env The environment current at the operation.
mcimadamore@1394 2525 * @param arg The type of the operand.
mcimadamore@1394 2526 */
mcimadamore@1394 2527 Symbol resolveUnaryOperator(DiagnosticPosition pos, JCTree.Tag optag, Env<AttrContext> env, Type arg) {
mcimadamore@1394 2528 return resolveOperator(pos, optag, env, List.of(arg));
mcimadamore@1394 2529 }
mcimadamore@1394 2530
mcimadamore@1394 2531 /** Resolve binary operator.
mcimadamore@1394 2532 * @param pos The position to use for error reporting.
mcimadamore@1394 2533 * @param optag The tag of the operation tree.
mcimadamore@1394 2534 * @param env The environment current at the operation.
mcimadamore@1394 2535 * @param left The types of the left operand.
mcimadamore@1394 2536 * @param right The types of the right operand.
mcimadamore@1394 2537 */
mcimadamore@1394 2538 Symbol resolveBinaryOperator(DiagnosticPosition pos,
mcimadamore@1394 2539 JCTree.Tag optag,
mcimadamore@1394 2540 Env<AttrContext> env,
mcimadamore@1394 2541 Type left,
mcimadamore@1394 2542 Type right) {
mcimadamore@1394 2543 return resolveOperator(pos, optag, env, List.of(left, right));
mcimadamore@1394 2544 }
mcimadamore@1394 2545
mcimadamore@1352 2546 /**
mcimadamore@1352 2547 * Resolution of member references is typically done as a single
mcimadamore@1352 2548 * overload resolution step, where the argument types A are inferred from
mcimadamore@1352 2549 * the target functional descriptor.
mcimadamore@1352 2550 *
mcimadamore@1352 2551 * If the member reference is a method reference with a type qualifier,
mcimadamore@1352 2552 * a two-step lookup process is performed. The first step uses the
mcimadamore@1352 2553 * expected argument list A, while the second step discards the first
mcimadamore@1352 2554 * type from A (which is treated as a receiver type).
mcimadamore@1352 2555 *
mcimadamore@1352 2556 * There are two cases in which inference is performed: (i) if the member
mcimadamore@1352 2557 * reference is a constructor reference and the qualifier type is raw - in
mcimadamore@1352 2558 * which case diamond inference is used to infer a parameterization for the
mcimadamore@1352 2559 * type qualifier; (ii) if the member reference is an unbound reference
mcimadamore@1352 2560 * where the type qualifier is raw - in that case, during the unbound lookup
mcimadamore@1352 2561 * the receiver argument type is used to infer an instantiation for the raw
mcimadamore@1352 2562 * qualifier type.
mcimadamore@1352 2563 *
mcimadamore@1352 2564 * When a multi-step resolution process is exploited, it is an error
mcimadamore@1352 2565 * if two candidates are found (ambiguity).
mcimadamore@1352 2566 *
mcimadamore@1352 2567 * This routine returns a pair (T,S), where S is the member reference symbol,
mcimadamore@1352 2568 * and T is the type of the class in which S is defined. This is necessary as
mcimadamore@1352 2569 * the type T might be dynamically inferred (i.e. if constructor reference
mcimadamore@1352 2570 * has a raw qualifier).
mcimadamore@1352 2571 */
mcimadamore@1352 2572 Pair<Symbol, ReferenceLookupHelper> resolveMemberReference(DiagnosticPosition pos,
mcimadamore@1352 2573 Env<AttrContext> env,
mcimadamore@1352 2574 JCMemberReference referenceTree,
mcimadamore@1352 2575 Type site,
mcimadamore@1352 2576 Name name, List<Type> argtypes,
mcimadamore@1352 2577 List<Type> typeargtypes,
mcimadamore@1674 2578 boolean boxingAllowed,
mcimadamore@1674 2579 MethodCheck methodCheck) {
mcimadamore@1394 2580 MethodResolutionPhase maxPhase = boxingAllowed ? VARARITY : BASIC;
mcimadamore@1496 2581
mcimadamore@1496 2582 ReferenceLookupHelper boundLookupHelper;
mcimadamore@1496 2583 if (!name.equals(names.init)) {
mcimadamore@1496 2584 //method reference
mcimadamore@1496 2585 boundLookupHelper =
mcimadamore@1496 2586 new MethodReferenceLookupHelper(referenceTree, name, site, argtypes, typeargtypes, maxPhase);
mcimadamore@1496 2587 } else if (site.hasTag(ARRAY)) {
mcimadamore@1496 2588 //array constructor reference
mcimadamore@1496 2589 boundLookupHelper =
mcimadamore@1496 2590 new ArrayConstructorReferenceLookupHelper(referenceTree, site, argtypes, typeargtypes, maxPhase);
mcimadamore@1496 2591 } else {
mcimadamore@1496 2592 //class constructor reference
mcimadamore@1496 2593 boundLookupHelper =
mcimadamore@1496 2594 new ConstructorReferenceLookupHelper(referenceTree, site, argtypes, typeargtypes, maxPhase);
mcimadamore@1496 2595 }
mcimadamore@1496 2596
mcimadamore@1352 2597 //step 1 - bound lookup
mcimadamore@1352 2598 Env<AttrContext> boundEnv = env.dup(env.tree, env.info.dup());
mcimadamore@1674 2599 Symbol boundSym = lookupMethod(boundEnv, env.tree.pos(), site.tsym, methodCheck, boundLookupHelper);
mcimadamore@1352 2600
mcimadamore@1352 2601 //step 2 - unbound lookup
mcimadamore@1352 2602 ReferenceLookupHelper unboundLookupHelper = boundLookupHelper.unboundLookup();
mcimadamore@1352 2603 Env<AttrContext> unboundEnv = env.dup(env.tree, env.info.dup());
mcimadamore@1674 2604 Symbol unboundSym = lookupMethod(unboundEnv, env.tree.pos(), site.tsym, methodCheck, unboundLookupHelper);
mcimadamore@1352 2605
mcimadamore@1352 2606 //merge results
mcimadamore@1352 2607 Pair<Symbol, ReferenceLookupHelper> res;
mcimadamore@1581 2608 if (!lookupSuccess(unboundSym)) {
mcimadamore@1352 2609 res = new Pair<Symbol, ReferenceLookupHelper>(boundSym, boundLookupHelper);
mcimadamore@1352 2610 env.info.pendingResolutionPhase = boundEnv.info.pendingResolutionPhase;
mcimadamore@1581 2611 } else if (lookupSuccess(boundSym)) {
mcimadamore@1352 2612 res = new Pair<Symbol, ReferenceLookupHelper>(ambiguityError(boundSym, unboundSym), boundLookupHelper);
mcimadamore@1352 2613 env.info.pendingResolutionPhase = boundEnv.info.pendingResolutionPhase;
mcimadamore@1352 2614 } else {
mcimadamore@1352 2615 res = new Pair<Symbol, ReferenceLookupHelper>(unboundSym, unboundLookupHelper);
mcimadamore@1352 2616 env.info.pendingResolutionPhase = unboundEnv.info.pendingResolutionPhase;
mcimadamore@1352 2617 }
mcimadamore@1352 2618
mcimadamore@1352 2619 return res;
mcimadamore@1352 2620 }
mcimadamore@1581 2621 //private
mcimadamore@1581 2622 boolean lookupSuccess(Symbol s) {
mcimadamore@1581 2623 return s.kind == MTH || s.kind == AMBIGUOUS;
mcimadamore@1581 2624 }
mcimadamore@1352 2625
mcimadamore@1352 2626 /**
mcimadamore@1352 2627 * Helper for defining custom method-like lookup logic; a lookup helper
mcimadamore@1352 2628 * provides hooks for (i) the actual lookup logic and (ii) accessing the
mcimadamore@1352 2629 * lookup result (this step might result in compiler diagnostics to be generated)
mcimadamore@1352 2630 */
mcimadamore@1352 2631 abstract class LookupHelper {
mcimadamore@1352 2632
mcimadamore@1352 2633 /** name of the symbol to lookup */
mcimadamore@1352 2634 Name name;
mcimadamore@1352 2635
mcimadamore@1352 2636 /** location in which the lookup takes place */
mcimadamore@1352 2637 Type site;
mcimadamore@1352 2638
mcimadamore@1352 2639 /** actual types used during the lookup */
mcimadamore@1352 2640 List<Type> argtypes;
mcimadamore@1352 2641
mcimadamore@1352 2642 /** type arguments used during the lookup */
mcimadamore@1352 2643 List<Type> typeargtypes;
mcimadamore@1352 2644
mcimadamore@1394 2645 /** Max overload resolution phase handled by this helper */
mcimadamore@1394 2646 MethodResolutionPhase maxPhase;
mcimadamore@1394 2647
mcimadamore@1394 2648 LookupHelper(Name name, Type site, List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
mcimadamore@1352 2649 this.name = name;
mcimadamore@1352 2650 this.site = site;
mcimadamore@1352 2651 this.argtypes = argtypes;
mcimadamore@1352 2652 this.typeargtypes = typeargtypes;
mcimadamore@1394 2653 this.maxPhase = maxPhase;
mcimadamore@1394 2654 }
mcimadamore@1394 2655
mcimadamore@1394 2656 /**
mcimadamore@1394 2657 * Should lookup stop at given phase with given result
mcimadamore@1394 2658 */
mcimadamore@1394 2659 protected boolean shouldStop(Symbol sym, MethodResolutionPhase phase) {
mcimadamore@1394 2660 return phase.ordinal() > maxPhase.ordinal() ||
mcimadamore@1394 2661 sym.kind < ERRONEOUS || sym.kind == AMBIGUOUS;
mcimadamore@1352 2662 }
mcimadamore@1352 2663
mcimadamore@1352 2664 /**
mcimadamore@1352 2665 * Search for a symbol under a given overload resolution phase - this method
mcimadamore@1352 2666 * is usually called several times, once per each overload resolution phase
mcimadamore@1352 2667 */
mcimadamore@1352 2668 abstract Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase);
mcimadamore@1352 2669
mcimadamore@1352 2670 /**
mcimadamore@1875 2671 * Dump overload resolution info
mcimadamore@1875 2672 */
mcimadamore@1875 2673 void debug(DiagnosticPosition pos, Symbol sym) {
mcimadamore@1875 2674 //do nothing
mcimadamore@1875 2675 }
mcimadamore@1875 2676
mcimadamore@1875 2677 /**
mcimadamore@1352 2678 * Validate the result of the lookup
mcimadamore@1352 2679 */
mcimadamore@1394 2680 abstract Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym);
mcimadamore@1394 2681 }
mcimadamore@1394 2682
mcimadamore@1394 2683 abstract class BasicLookupHelper extends LookupHelper {
mcimadamore@1394 2684
mcimadamore@1394 2685 BasicLookupHelper(Name name, Type site, List<Type> argtypes, List<Type> typeargtypes) {
mcimadamore@1779 2686 this(name, site, argtypes, typeargtypes, MethodResolutionPhase.VARARITY);
mcimadamore@1779 2687 }
mcimadamore@1779 2688
mcimadamore@1779 2689 BasicLookupHelper(Name name, Type site, List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
mcimadamore@1779 2690 super(name, site, argtypes, typeargtypes, maxPhase);
mcimadamore@1394 2691 }
mcimadamore@1394 2692
mcimadamore@1394 2693 @Override
mcimadamore@1875 2694 final Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
mcimadamore@1875 2695 Symbol sym = doLookup(env, phase);
mcimadamore@1480 2696 if (sym.kind == AMBIGUOUS) {
mcimadamore@1480 2697 AmbiguityError a_err = (AmbiguityError)sym;
mcimadamore@1480 2698 sym = a_err.mergeAbstracts(site);
mcimadamore@1480 2699 }
mcimadamore@1875 2700 return sym;
mcimadamore@1875 2701 }
mcimadamore@1875 2702
mcimadamore@1875 2703 abstract Symbol doLookup(Env<AttrContext> env, MethodResolutionPhase phase);
mcimadamore@1875 2704
mcimadamore@1875 2705 @Override
mcimadamore@1875 2706 Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym) {
mcimadamore@1394 2707 if (sym.kind >= AMBIGUOUS) {
mcimadamore@1394 2708 //if nothing is found return the 'first' error
mcimadamore@1394 2709 sym = accessMethod(sym, pos, location, site, name, true, argtypes, typeargtypes);
mcimadamore@1394 2710 }
mcimadamore@1394 2711 return sym;
mcimadamore@1394 2712 }
mcimadamore@1875 2713
mcimadamore@1875 2714 @Override
mcimadamore@1875 2715 void debug(DiagnosticPosition pos, Symbol sym) {
mcimadamore@1875 2716 reportVerboseResolutionDiagnostic(pos, name, site, argtypes, typeargtypes, sym);
mcimadamore@1875 2717 }
mcimadamore@1352 2718 }
mcimadamore@1352 2719
mcimadamore@1352 2720 /**
mcimadamore@1352 2721 * Helper class for member reference lookup. A reference lookup helper
mcimadamore@1352 2722 * defines the basic logic for member reference lookup; a method gives
mcimadamore@1352 2723 * access to an 'unbound' helper used to perform an unbound member
mcimadamore@1352 2724 * reference lookup.
mcimadamore@1352 2725 */
mcimadamore@1352 2726 abstract class ReferenceLookupHelper extends LookupHelper {
mcimadamore@1352 2727
mcimadamore@1352 2728 /** The member reference tree */
mcimadamore@1352 2729 JCMemberReference referenceTree;
mcimadamore@1352 2730
mcimadamore@1352 2731 ReferenceLookupHelper(JCMemberReference referenceTree, Name name, Type site,
mcimadamore@1394 2732 List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
mcimadamore@1394 2733 super(name, site, argtypes, typeargtypes, maxPhase);
mcimadamore@1352 2734 this.referenceTree = referenceTree;
mcimadamore@1394 2735
mcimadamore@1352 2736 }
mcimadamore@1352 2737
mcimadamore@1352 2738 /**
mcimadamore@1352 2739 * Returns an unbound version of this lookup helper. By default, this
mcimadamore@1352 2740 * method returns an dummy lookup helper.
mcimadamore@1352 2741 */
mcimadamore@1352 2742 ReferenceLookupHelper unboundLookup() {
mcimadamore@1352 2743 //dummy loopkup helper that always return 'methodNotFound'
mcimadamore@1394 2744 return new ReferenceLookupHelper(referenceTree, name, site, argtypes, typeargtypes, maxPhase) {
mcimadamore@1352 2745 @Override
mcimadamore@1352 2746 ReferenceLookupHelper unboundLookup() {
mcimadamore@1352 2747 return this;
mcimadamore@1352 2748 }
mcimadamore@1352 2749 @Override
mcimadamore@1394 2750 Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
mcimadamore@1352 2751 return methodNotFound;
mcimadamore@1352 2752 }
mcimadamore@1352 2753 @Override
mcimadamore@1352 2754 ReferenceKind referenceKind(Symbol sym) {
mcimadamore@1352 2755 Assert.error();
mcimadamore@1352 2756 return null;
mcimadamore@1352 2757 }
mcimadamore@1352 2758 };
mcimadamore@1352 2759 }
mcimadamore@1352 2760
mcimadamore@1352 2761 /**
mcimadamore@1352 2762 * Get the kind of the member reference
mcimadamore@1352 2763 */
mcimadamore@1352 2764 abstract JCMemberReference.ReferenceKind referenceKind(Symbol sym);
mcimadamore@1352 2765
mcimadamore@1394 2766 Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym) {
mcimadamore@1480 2767 if (sym.kind == AMBIGUOUS) {
mcimadamore@1480 2768 AmbiguityError a_err = (AmbiguityError)sym;
mcimadamore@1480 2769 sym = a_err.mergeAbstracts(site);
mcimadamore@1480 2770 }
mcimadamore@1394 2771 //skip error reporting
mcimadamore@1352 2772 return sym;
mcimadamore@1352 2773 }
mcimadamore@1352 2774 }
mcimadamore@1352 2775
mcimadamore@1352 2776 /**
mcimadamore@1352 2777 * Helper class for method reference lookup. The lookup logic is based
mcimadamore@1352 2778 * upon Resolve.findMethod; in certain cases, this helper class has a
mcimadamore@1352 2779 * corresponding unbound helper class (see UnboundMethodReferenceLookupHelper).
mcimadamore@1352 2780 * In such cases, non-static lookup results are thrown away.
mcimadamore@1352 2781 */
mcimadamore@1352 2782 class MethodReferenceLookupHelper extends ReferenceLookupHelper {
mcimadamore@1352 2783
mcimadamore@1352 2784 MethodReferenceLookupHelper(JCMemberReference referenceTree, Name name, Type site,
mcimadamore@1394 2785 List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
mcimadamore@1394 2786 super(referenceTree, name, site, argtypes, typeargtypes, maxPhase);
mcimadamore@1352 2787 }
mcimadamore@1352 2788
mcimadamore@1610 2789 @Override
mcimadamore@1610 2790 final Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
mcimadamore@1352 2791 return findMethod(env, site, name, argtypes, typeargtypes,
mcimadamore@1352 2792 phase.isBoxingRequired(), phase.isVarargsRequired(), syms.operatorNames.contains(name));
mcimadamore@1352 2793 }
mcimadamore@1352 2794
mcimadamore@1352 2795 @Override
mcimadamore@1352 2796 ReferenceLookupHelper unboundLookup() {
mcimadamore@1352 2797 if (TreeInfo.isStaticSelector(referenceTree.expr, names) &&
mcimadamore@1352 2798 argtypes.nonEmpty() &&
mcimadamore@1674 2799 (argtypes.head.hasTag(NONE) || types.isSubtypeUnchecked(argtypes.head, site))) {
mcimadamore@1352 2800 return new UnboundMethodReferenceLookupHelper(referenceTree, name,
mcimadamore@1394 2801 site, argtypes, typeargtypes, maxPhase);
mcimadamore@1352 2802 } else {
mcimadamore@1352 2803 return super.unboundLookup();
mcimadamore@1352 2804 }
mcimadamore@1352 2805 }
mcimadamore@1352 2806
mcimadamore@1352 2807 @Override
mcimadamore@1352 2808 ReferenceKind referenceKind(Symbol sym) {
mcimadamore@1352 2809 if (sym.isStatic()) {
mcimadamore@1435 2810 return ReferenceKind.STATIC;
mcimadamore@1352 2811 } else {
mcimadamore@1352 2812 Name selName = TreeInfo.name(referenceTree.getQualifierExpression());
mcimadamore@1352 2813 return selName != null && selName == names._super ?
mcimadamore@1352 2814 ReferenceKind.SUPER :
mcimadamore@1352 2815 ReferenceKind.BOUND;
mcimadamore@1352 2816 }
mcimadamore@1352 2817 }
mcimadamore@1352 2818 }
mcimadamore@1352 2819
mcimadamore@1352 2820 /**
mcimadamore@1352 2821 * Helper class for unbound method reference lookup. Essentially the same
mcimadamore@1352 2822 * as the basic method reference lookup helper; main difference is that static
mcimadamore@1352 2823 * lookup results are thrown away. If qualifier type is raw, an attempt to
mcimadamore@1352 2824 * infer a parameterized type is made using the first actual argument (that
mcimadamore@1352 2825 * would otherwise be ignored during the lookup).
mcimadamore@1352 2826 */
mcimadamore@1352 2827 class UnboundMethodReferenceLookupHelper extends MethodReferenceLookupHelper {
mcimadamore@1352 2828
mcimadamore@1352 2829 UnboundMethodReferenceLookupHelper(JCMemberReference referenceTree, Name name, Type site,
mcimadamore@1394 2830 List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
mcimadamore@1581 2831 super(referenceTree, name, site, argtypes.tail, typeargtypes, maxPhase);
mcimadamore@1674 2832 if (site.isRaw() && !argtypes.head.hasTag(NONE)) {
mcimadamore@1674 2833 Type asSuperSite = types.asSuper(argtypes.head, site.tsym);
mcimadamore@1581 2834 this.site = asSuperSite;
mcimadamore@1581 2835 }
mcimadamore@1352 2836 }
mcimadamore@1352 2837
mcimadamore@1352 2838 @Override
mcimadamore@1352 2839 ReferenceLookupHelper unboundLookup() {
mcimadamore@1352 2840 return this;
mcimadamore@1352 2841 }
mcimadamore@1352 2842
mcimadamore@1352 2843 @Override
mcimadamore@1352 2844 ReferenceKind referenceKind(Symbol sym) {
mcimadamore@1352 2845 return ReferenceKind.UNBOUND;
mcimadamore@1352 2846 }
mcimadamore@1352 2847 }
mcimadamore@1352 2848
mcimadamore@1352 2849 /**
mcimadamore@1496 2850 * Helper class for array constructor lookup; an array constructor lookup
mcimadamore@1496 2851 * is simulated by looking up a method that returns the array type specified
mcimadamore@1496 2852 * as qualifier, and that accepts a single int parameter (size of the array).
mcimadamore@1496 2853 */
mcimadamore@1496 2854 class ArrayConstructorReferenceLookupHelper extends ReferenceLookupHelper {
mcimadamore@1496 2855
mcimadamore@1496 2856 ArrayConstructorReferenceLookupHelper(JCMemberReference referenceTree, Type site, List<Type> argtypes,
mcimadamore@1496 2857 List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
mcimadamore@1496 2858 super(referenceTree, names.init, site, argtypes, typeargtypes, maxPhase);
mcimadamore@1496 2859 }
mcimadamore@1496 2860
mcimadamore@1496 2861 @Override
mcimadamore@1496 2862 protected Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
mcimadamore@1496 2863 Scope sc = new Scope(syms.arrayClass);
mcimadamore@1496 2864 MethodSymbol arrayConstr = new MethodSymbol(PUBLIC, name, null, site.tsym);
vromero@1853 2865 arrayConstr.type = new MethodType(List.<Type>of(syms.intType), site, List.<Type>nil(), syms.methodClass);
mcimadamore@1496 2866 sc.enter(arrayConstr);
mcimadamore@1496 2867 return findMethodInScope(env, site, name, argtypes, typeargtypes, sc, methodNotFound, phase.isBoxingRequired(), phase.isVarargsRequired(), false, false);
mcimadamore@1496 2868 }
mcimadamore@1496 2869
mcimadamore@1496 2870 @Override
mcimadamore@1496 2871 ReferenceKind referenceKind(Symbol sym) {
mcimadamore@1496 2872 return ReferenceKind.ARRAY_CTOR;
mcimadamore@1496 2873 }
mcimadamore@1496 2874 }
mcimadamore@1496 2875
mcimadamore@1496 2876 /**
mcimadamore@1352 2877 * Helper class for constructor reference lookup. The lookup logic is based
mcimadamore@1352 2878 * upon either Resolve.findMethod or Resolve.findDiamond - depending on
mcimadamore@1352 2879 * whether the constructor reference needs diamond inference (this is the case
mcimadamore@1352 2880 * if the qualifier type is raw). A special erroneous symbol is returned
mcimadamore@1352 2881 * if the lookup returns the constructor of an inner class and there's no
mcimadamore@1352 2882 * enclosing instance in scope.
mcimadamore@1352 2883 */
mcimadamore@1352 2884 class ConstructorReferenceLookupHelper extends ReferenceLookupHelper {
mcimadamore@1352 2885
mcimadamore@1352 2886 boolean needsInference;
mcimadamore@1352 2887
mcimadamore@1352 2888 ConstructorReferenceLookupHelper(JCMemberReference referenceTree, Type site, List<Type> argtypes,
mcimadamore@1394 2889 List<Type> typeargtypes, MethodResolutionPhase maxPhase) {
mcimadamore@1394 2890 super(referenceTree, names.init, site, argtypes, typeargtypes, maxPhase);
mcimadamore@1352 2891 if (site.isRaw()) {
mcimadamore@1352 2892 this.site = new ClassType(site.getEnclosingType(), site.tsym.type.getTypeArguments(), site.tsym);
mcimadamore@1352 2893 needsInference = true;
mcimadamore@1352 2894 }
mcimadamore@1352 2895 }
mcimadamore@1352 2896
mcimadamore@1352 2897 @Override
mcimadamore@1394 2898 protected Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
mcimadamore@1352 2899 Symbol sym = needsInference ?
mcimadamore@1352 2900 findDiamond(env, site, argtypes, typeargtypes, phase.isBoxingRequired(), phase.isVarargsRequired()) :
mcimadamore@1352 2901 findMethod(env, site, name, argtypes, typeargtypes,
mcimadamore@1352 2902 phase.isBoxingRequired(), phase.isVarargsRequired(), syms.operatorNames.contains(name));
mcimadamore@1352 2903 return sym.kind != MTH ||
jjg@1374 2904 site.getEnclosingType().hasTag(NONE) ||
mcimadamore@1352 2905 hasEnclosingInstance(env, site) ?
mcimadamore@1352 2906 sym : new InvalidSymbolError(Kinds.MISSING_ENCL, sym, null) {
mcimadamore@1352 2907 @Override
mcimadamore@1352 2908 JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos, Symbol location, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) {
mcimadamore@1352 2909 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@1352 2910 "cant.access.inner.cls.constr", site.tsym.name, argtypes, site.getEnclosingType());
mcimadamore@1352 2911 }
mcimadamore@1352 2912 };
mcimadamore@1352 2913 }
mcimadamore@1352 2914
mcimadamore@1352 2915 @Override
mcimadamore@1352 2916 ReferenceKind referenceKind(Symbol sym) {
jjg@1374 2917 return site.getEnclosingType().hasTag(NONE) ?
mcimadamore@1352 2918 ReferenceKind.TOPLEVEL : ReferenceKind.IMPLICIT_INNER;
mcimadamore@1352 2919 }
mcimadamore@1352 2920 }
mcimadamore@1352 2921
mcimadamore@1352 2922 /**
mcimadamore@1394 2923 * Main overload resolution routine. On each overload resolution step, a
mcimadamore@1394 2924 * lookup helper class is used to perform the method/constructor lookup;
mcimadamore@1394 2925 * at the end of the lookup, the helper is used to validate the results
mcimadamore@1394 2926 * (this last step might trigger overload resolution diagnostics).
mcimadamore@1352 2927 */
mcimadamore@1674 2928 Symbol lookupMethod(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, MethodCheck methodCheck, LookupHelper lookupHelper) {
mcimadamore@1674 2929 MethodResolutionContext resolveContext = new MethodResolutionContext();
mcimadamore@1674 2930 resolveContext.methodCheck = methodCheck;
mcimadamore@1674 2931 return lookupMethod(env, pos, location, resolveContext, lookupHelper);
mcimadamore@1394 2932 }
mcimadamore@1394 2933
mcimadamore@1394 2934 Symbol lookupMethod(Env<AttrContext> env, DiagnosticPosition pos, Symbol location,
mcimadamore@1394 2935 MethodResolutionContext resolveContext, LookupHelper lookupHelper) {
mcimadamore@1352 2936 MethodResolutionContext prevResolutionContext = currentResolutionContext;
mcimadamore@1352 2937 try {
mcimadamore@1394 2938 Symbol bestSoFar = methodNotFound;
mcimadamore@1394 2939 currentResolutionContext = resolveContext;
mcimadamore@1394 2940 for (MethodResolutionPhase phase : methodResolutionSteps) {
mcimadamore@1394 2941 if (!phase.isApplicable(boxingEnabled, varargsEnabled) ||
mcimadamore@1394 2942 lookupHelper.shouldStop(bestSoFar, phase)) break;
mcimadamore@1394 2943 MethodResolutionPhase prevPhase = currentResolutionContext.step;
mcimadamore@1394 2944 Symbol prevBest = bestSoFar;
mcimadamore@1394 2945 currentResolutionContext.step = phase;
mcimadamore@1875 2946 Symbol sym = lookupHelper.lookup(env, phase);
mcimadamore@1875 2947 lookupHelper.debug(pos, sym);
mcimadamore@1875 2948 bestSoFar = phase.mergeResults(bestSoFar, sym);
mcimadamore@1394 2949 env.info.pendingResolutionPhase = (prevBest == bestSoFar) ? prevPhase : phase;
mcimadamore@1352 2950 }
mcimadamore@1394 2951 return lookupHelper.access(env, pos, location, bestSoFar);
mcimadamore@1394 2952 } finally {
mcimadamore@1352 2953 currentResolutionContext = prevResolutionContext;
mcimadamore@1352 2954 }
mcimadamore@1352 2955 }
mcimadamore@1352 2956
duke@1 2957 /**
duke@1 2958 * Resolve `c.name' where name == this or name == super.
duke@1 2959 * @param pos The position to use for error reporting.
duke@1 2960 * @param env The environment current at the expression.
duke@1 2961 * @param c The qualifier.
duke@1 2962 * @param name The identifier's name.
duke@1 2963 */
duke@1 2964 Symbol resolveSelf(DiagnosticPosition pos,
duke@1 2965 Env<AttrContext> env,
duke@1 2966 TypeSymbol c,
duke@1 2967 Name name) {
duke@1 2968 Env<AttrContext> env1 = env;
duke@1 2969 boolean staticOnly = false;
duke@1 2970 while (env1.outer != null) {
duke@1 2971 if (isStatic(env1)) staticOnly = true;
duke@1 2972 if (env1.enclClass.sym == c) {
duke@1 2973 Symbol sym = env1.info.scope.lookup(name).sym;
duke@1 2974 if (sym != null) {
duke@1 2975 if (staticOnly) sym = new StaticError(sym);
mcimadamore@1347 2976 return accessBase(sym, pos, env.enclClass.sym.type,
duke@1 2977 name, true);
duke@1 2978 }
duke@1 2979 }
duke@1 2980 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
duke@1 2981 env1 = env1.outer;
duke@1 2982 }
mcimadamore@1393 2983 if (allowDefaultMethods && c.isInterface() &&
mcimadamore@1393 2984 name == names._super && !isStatic(env) &&
mcimadamore@1415 2985 types.isDirectSuperInterface(c, env.enclClass.sym)) {
mcimadamore@1393 2986 //this might be a default super call if one of the superinterfaces is 'c'
mcimadamore@1393 2987 for (Type t : pruneInterfaces(env.enclClass.type)) {
mcimadamore@1393 2988 if (t.tsym == c) {
mcimadamore@1393 2989 env.info.defaultSuperCallSite = t;
mcimadamore@1393 2990 return new VarSymbol(0, names._super,
mcimadamore@1393 2991 types.asSuper(env.enclClass.type, c), env.enclClass.sym);
mcimadamore@1393 2992 }
mcimadamore@1393 2993 }
mcimadamore@1393 2994 //find a direct superinterface that is a subtype of 'c'
mcimadamore@1393 2995 for (Type i : types.interfaces(env.enclClass.type)) {
mcimadamore@1393 2996 if (i.tsym.isSubClass(c, types) && i.tsym != c) {
mcimadamore@1393 2997 log.error(pos, "illegal.default.super.call", c,
mcimadamore@1393 2998 diags.fragment("redundant.supertype", c, i));
mcimadamore@1393 2999 return syms.errSymbol;
mcimadamore@1393 3000 }
mcimadamore@1393 3001 }
mcimadamore@1393 3002 Assert.error();
mcimadamore@1393 3003 }
duke@1 3004 log.error(pos, "not.encl.class", c);
duke@1 3005 return syms.errSymbol;
duke@1 3006 }
mcimadamore@1393 3007 //where
mcimadamore@1393 3008 private List<Type> pruneInterfaces(Type t) {
mcimadamore@1393 3009 ListBuffer<Type> result = ListBuffer.lb();
mcimadamore@1393 3010 for (Type t1 : types.interfaces(t)) {
mcimadamore@1393 3011 boolean shouldAdd = true;
mcimadamore@1393 3012 for (Type t2 : types.interfaces(t)) {
mcimadamore@1393 3013 if (t1 != t2 && types.isSubtypeNoCapture(t2, t1)) {
mcimadamore@1393 3014 shouldAdd = false;
mcimadamore@1393 3015 }
mcimadamore@1393 3016 }
mcimadamore@1393 3017 if (shouldAdd) {
mcimadamore@1393 3018 result.append(t1);
mcimadamore@1393 3019 }
mcimadamore@1393 3020 }
mcimadamore@1393 3021 return result.toList();
mcimadamore@1393 3022 }
mcimadamore@1393 3023
duke@1 3024
duke@1 3025 /**
duke@1 3026 * Resolve `c.this' for an enclosing class c that contains the
duke@1 3027 * named member.
duke@1 3028 * @param pos The position to use for error reporting.
duke@1 3029 * @param env The environment current at the expression.
duke@1 3030 * @param member The member that must be contained in the result.
duke@1 3031 */
duke@1 3032 Symbol resolveSelfContaining(DiagnosticPosition pos,
duke@1 3033 Env<AttrContext> env,
mcimadamore@901 3034 Symbol member,
mcimadamore@901 3035 boolean isSuperCall) {
mcimadamore@1352 3036 Symbol sym = resolveSelfContainingInternal(env, member, isSuperCall);
mcimadamore@1352 3037 if (sym == null) {
mcimadamore@1352 3038 log.error(pos, "encl.class.required", member);
mcimadamore@1352 3039 return syms.errSymbol;
mcimadamore@1352 3040 } else {
mcimadamore@1352 3041 return accessBase(sym, pos, env.enclClass.sym.type, sym.name, true);
mcimadamore@1352 3042 }
mcimadamore@1352 3043 }
mcimadamore@1352 3044
mcimadamore@1352 3045 boolean hasEnclosingInstance(Env<AttrContext> env, Type type) {
mcimadamore@1352 3046 Symbol encl = resolveSelfContainingInternal(env, type.tsym, false);
mcimadamore@1352 3047 return encl != null && encl.kind < ERRONEOUS;
mcimadamore@1352 3048 }
mcimadamore@1352 3049
mcimadamore@1352 3050 private Symbol resolveSelfContainingInternal(Env<AttrContext> env,
mcimadamore@1352 3051 Symbol member,
mcimadamore@1352 3052 boolean isSuperCall) {
duke@1 3053 Name name = names._this;
mcimadamore@901 3054 Env<AttrContext> env1 = isSuperCall ? env.outer : env;
duke@1 3055 boolean staticOnly = false;
mcimadamore@901 3056 if (env1 != null) {
mcimadamore@901 3057 while (env1 != null && env1.outer != null) {
mcimadamore@901 3058 if (isStatic(env1)) staticOnly = true;
mcimadamore@901 3059 if (env1.enclClass.sym.isSubClass(member.owner, types)) {
mcimadamore@901 3060 Symbol sym = env1.info.scope.lookup(name).sym;
mcimadamore@901 3061 if (sym != null) {
mcimadamore@901 3062 if (staticOnly) sym = new StaticError(sym);
mcimadamore@1352 3063 return sym;
mcimadamore@901 3064 }
duke@1 3065 }
mcimadamore@901 3066 if ((env1.enclClass.sym.flags() & STATIC) != 0)
mcimadamore@901 3067 staticOnly = true;
mcimadamore@901 3068 env1 = env1.outer;
duke@1 3069 }
duke@1 3070 }
mcimadamore@1352 3071 return null;
duke@1 3072 }
duke@1 3073
duke@1 3074 /**
duke@1 3075 * Resolve an appropriate implicit this instance for t's container.
jjh@972 3076 * JLS 8.8.5.1 and 15.9.2
duke@1 3077 */
duke@1 3078 Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t) {
mcimadamore@901 3079 return resolveImplicitThis(pos, env, t, false);
mcimadamore@901 3080 }
mcimadamore@901 3081
mcimadamore@901 3082 Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t, boolean isSuperCall) {
duke@1 3083 Type thisType = (((t.tsym.owner.kind & (MTH|VAR)) != 0)
duke@1 3084 ? resolveSelf(pos, env, t.getEnclosingType().tsym, names._this)
mcimadamore@901 3085 : resolveSelfContaining(pos, env, t.tsym, isSuperCall)).type;
duke@1 3086 if (env.info.isSelfCall && thisType.tsym == env.enclClass.sym)
duke@1 3087 log.error(pos, "cant.ref.before.ctor.called", "this");
duke@1 3088 return thisType;
duke@1 3089 }
duke@1 3090
duke@1 3091 /* ***************************************************************************
duke@1 3092 * ResolveError classes, indicating error situations when accessing symbols
duke@1 3093 ****************************************************************************/
duke@1 3094
mcimadamore@1221 3095 //used by TransTypes when checking target type of synthetic cast
mcimadamore@1221 3096 public void logAccessErrorInternal(Env<AttrContext> env, JCTree tree, Type type) {
mcimadamore@1221 3097 AccessError error = new AccessError(env, env.enclClass.type, type.tsym);
mcimadamore@1221 3098 logResolveError(error, tree.pos(), env.enclClass.sym, env.enclClass.type, null, null, null);
mcimadamore@302 3099 }
mcimadamore@302 3100 //where
mcimadamore@302 3101 private void logResolveError(ResolveError error,
mcimadamore@302 3102 DiagnosticPosition pos,
mcimadamore@829 3103 Symbol location,
mcimadamore@302 3104 Type site,
mcimadamore@302 3105 Name name,
mcimadamore@302 3106 List<Type> argtypes,
mcimadamore@302 3107 List<Type> typeargtypes) {
mcimadamore@302 3108 JCDiagnostic d = error.getDiagnostic(JCDiagnostic.DiagnosticType.ERROR,
mcimadamore@829 3109 pos, location, site, name, argtypes, typeargtypes);
jjg@643 3110 if (d != null) {
jjg@643 3111 d.setFlag(DiagnosticFlag.RESOLVE_ERROR);
mcimadamore@302 3112 log.report(d);
jjg@643 3113 }
duke@1 3114 }
duke@1 3115
mcimadamore@161 3116 private final LocalizedString noArgs = new LocalizedString("compiler.misc.no.args");
mcimadamore@161 3117
mcimadamore@161 3118 public Object methodArguments(List<Type> argtypes) {
mcimadamore@1348 3119 if (argtypes == null || argtypes.isEmpty()) {
mcimadamore@1348 3120 return noArgs;
mcimadamore@1348 3121 } else {
mcimadamore@1348 3122 ListBuffer<Object> diagArgs = ListBuffer.lb();
mcimadamore@1348 3123 for (Type t : argtypes) {
jjg@1374 3124 if (t.hasTag(DEFERRED)) {
mcimadamore@1348 3125 diagArgs.append(((DeferredAttr.DeferredType)t).tree);
mcimadamore@1348 3126 } else {
mcimadamore@1348 3127 diagArgs.append(t);
mcimadamore@1348 3128 }
mcimadamore@1348 3129 }
mcimadamore@1348 3130 return diagArgs;
mcimadamore@1348 3131 }
mcimadamore@161 3132 }
mcimadamore@161 3133
mcimadamore@302 3134 /**
mcimadamore@302 3135 * Root class for resolution errors. Subclass of ResolveError
mcimadamore@302 3136 * represent a different kinds of resolution error - as such they must
mcimadamore@302 3137 * specify how they map into concrete compiler diagnostics.
duke@1 3138 */
mcimadamore@1352 3139 abstract class ResolveError extends Symbol {
duke@1 3140
mcimadamore@302 3141 /** The name of the kind of error, for debugging only. */
mcimadamore@302 3142 final String debugName;
mcimadamore@302 3143
mcimadamore@302 3144 ResolveError(int kind, String debugName) {
duke@1 3145 super(kind, 0, null, null, null);
duke@1 3146 this.debugName = debugName;
duke@1 3147 }
duke@1 3148
mcimadamore@302 3149 @Override
duke@1 3150 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
duke@1 3151 throw new AssertionError();
duke@1 3152 }
duke@1 3153
mcimadamore@302 3154 @Override
duke@1 3155 public String toString() {
mcimadamore@302 3156 return debugName;
duke@1 3157 }
duke@1 3158
mcimadamore@302 3159 @Override
mcimadamore@302 3160 public boolean exists() {
mcimadamore@302 3161 return false;
duke@1 3162 }
duke@1 3163
mcimadamore@302 3164 /**
mcimadamore@302 3165 * Create an external representation for this erroneous symbol to be
mcimadamore@302 3166 * used during attribution - by default this returns the symbol of a
mcimadamore@302 3167 * brand new error type which stores the original type found
mcimadamore@302 3168 * during resolution.
mcimadamore@302 3169 *
mcimadamore@302 3170 * @param name the name used during resolution
mcimadamore@302 3171 * @param location the location from which the symbol is accessed
duke@1 3172 */
mcimadamore@302 3173 protected Symbol access(Name name, TypeSymbol location) {
mcimadamore@302 3174 return types.createErrorType(name, location, syms.errSymbol.type).tsym;
duke@1 3175 }
duke@1 3176
mcimadamore@302 3177 /**
mcimadamore@302 3178 * Create a diagnostic representing this resolution error.
mcimadamore@302 3179 *
mcimadamore@302 3180 * @param dkind The kind of the diagnostic to be created (e.g error).
mcimadamore@302 3181 * @param pos The position to be used for error reporting.
mcimadamore@302 3182 * @param site The original type from where the selection took place.
mcimadamore@302 3183 * @param name The name of the symbol to be resolved.
mcimadamore@302 3184 * @param argtypes The invocation's value arguments,
mcimadamore@302 3185 * if we looked for a method.
mcimadamore@302 3186 * @param typeargtypes The invocation's type arguments,
mcimadamore@302 3187 * if we looked for a method.
mcimadamore@302 3188 */
mcimadamore@302 3189 abstract JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 3190 DiagnosticPosition pos,
mcimadamore@829 3191 Symbol location,
mcimadamore@302 3192 Type site,
mcimadamore@302 3193 Name name,
mcimadamore@302 3194 List<Type> argtypes,
mcimadamore@302 3195 List<Type> typeargtypes);
duke@1 3196 }
duke@1 3197
mcimadamore@302 3198 /**
mcimadamore@302 3199 * This class is the root class of all resolution errors caused by
mcimadamore@302 3200 * an invalid symbol being found during resolution.
duke@1 3201 */
mcimadamore@302 3202 abstract class InvalidSymbolError extends ResolveError {
mcimadamore@302 3203
mcimadamore@302 3204 /** The invalid symbol found during resolution */
mcimadamore@302 3205 Symbol sym;
mcimadamore@302 3206
mcimadamore@302 3207 InvalidSymbolError(int kind, Symbol sym, String debugName) {
mcimadamore@302 3208 super(kind, debugName);
mcimadamore@302 3209 this.sym = sym;
mcimadamore@302 3210 }
mcimadamore@302 3211
mcimadamore@302 3212 @Override
mcimadamore@302 3213 public boolean exists() {
mcimadamore@302 3214 return true;
mcimadamore@302 3215 }
mcimadamore@302 3216
mcimadamore@302 3217 @Override
mcimadamore@302 3218 public String toString() {
mcimadamore@302 3219 return super.toString() + " wrongSym=" + sym;
mcimadamore@302 3220 }
mcimadamore@302 3221
mcimadamore@302 3222 @Override
mcimadamore@302 3223 public Symbol access(Name name, TypeSymbol location) {
mcimadamore@1480 3224 if ((sym.kind & ERRONEOUS) == 0 && (sym.kind & TYP) != 0)
mcimadamore@302 3225 return types.createErrorType(name, location, sym.type).tsym;
mcimadamore@302 3226 else
mcimadamore@302 3227 return sym;
mcimadamore@302 3228 }
mcimadamore@302 3229 }
mcimadamore@302 3230
mcimadamore@302 3231 /**
mcimadamore@302 3232 * InvalidSymbolError error class indicating that a symbol matching a
mcimadamore@302 3233 * given name does not exists in a given site.
mcimadamore@302 3234 */
mcimadamore@302 3235 class SymbolNotFoundError extends ResolveError {
mcimadamore@302 3236
mcimadamore@302 3237 SymbolNotFoundError(int kind) {
mcimadamore@302 3238 super(kind, "symbol not found error");
mcimadamore@302 3239 }
mcimadamore@302 3240
mcimadamore@302 3241 @Override
mcimadamore@302 3242 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 3243 DiagnosticPosition pos,
mcimadamore@829 3244 Symbol location,
mcimadamore@302 3245 Type site,
mcimadamore@302 3246 Name name,
mcimadamore@302 3247 List<Type> argtypes,
mcimadamore@302 3248 List<Type> typeargtypes) {
mcimadamore@302 3249 argtypes = argtypes == null ? List.<Type>nil() : argtypes;
mcimadamore@302 3250 typeargtypes = typeargtypes == null ? List.<Type>nil() : typeargtypes;
mcimadamore@302 3251 if (name == names.error)
mcimadamore@302 3252 return null;
mcimadamore@302 3253
mcimadamore@1347 3254 if (syms.operatorNames.contains(name)) {
mcimadamore@829 3255 boolean isUnaryOp = argtypes.size() == 1;
mcimadamore@829 3256 String key = argtypes.size() == 1 ?
mcimadamore@829 3257 "operator.cant.be.applied" :
mcimadamore@829 3258 "operator.cant.be.applied.1";
mcimadamore@829 3259 Type first = argtypes.head;
mcimadamore@829 3260 Type second = !isUnaryOp ? argtypes.tail.head : null;
jjg@612 3261 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@829 3262 key, name, first, second);
mcimadamore@302 3263 }
mcimadamore@302 3264 boolean hasLocation = false;
mcimadamore@855 3265 if (location == null) {
mcimadamore@855 3266 location = site.tsym;
mcimadamore@855 3267 }
mcimadamore@829 3268 if (!location.name.isEmpty()) {
mcimadamore@829 3269 if (location.kind == PCK && !site.tsym.exists()) {
jjg@612 3270 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@829 3271 "doesnt.exist", location);
mcimadamore@302 3272 }
mcimadamore@829 3273 hasLocation = !location.name.equals(names._this) &&
mcimadamore@829 3274 !location.name.equals(names._super);
mcimadamore@302 3275 }
mcimadamore@1347 3276 boolean isConstructor = kind == ABSENT_MTH && name == names.init;
mcimadamore@302 3277 KindName kindname = isConstructor ? KindName.CONSTRUCTOR : absentKind(kind);
mcimadamore@302 3278 Name idname = isConstructor ? site.tsym.name : name;
mcimadamore@302 3279 String errKey = getErrorKey(kindname, typeargtypes.nonEmpty(), hasLocation);
mcimadamore@302 3280 if (hasLocation) {
jjg@612 3281 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@302 3282 errKey, kindname, idname, //symbol kindname, name
mcimadamore@1456 3283 typeargtypes, args(argtypes), //type parameters and arguments (if any)
mcimadamore@855 3284 getLocationDiag(location, site)); //location kindname, type
mcimadamore@302 3285 }
mcimadamore@302 3286 else {
jjg@612 3287 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@302 3288 errKey, kindname, idname, //symbol kindname, name
mcimadamore@1456 3289 typeargtypes, args(argtypes)); //type parameters and arguments (if any)
mcimadamore@302 3290 }
mcimadamore@302 3291 }
mcimadamore@302 3292 //where
mcimadamore@1456 3293 private Object args(List<Type> args) {
mcimadamore@1456 3294 return args.isEmpty() ? args : methodArguments(args);
mcimadamore@1456 3295 }
mcimadamore@1456 3296
mcimadamore@302 3297 private String getErrorKey(KindName kindname, boolean hasTypeArgs, boolean hasLocation) {
mcimadamore@302 3298 String key = "cant.resolve";
mcimadamore@302 3299 String suffix = hasLocation ? ".location" : "";
mcimadamore@302 3300 switch (kindname) {
mcimadamore@302 3301 case METHOD:
mcimadamore@302 3302 case CONSTRUCTOR: {
mcimadamore@302 3303 suffix += ".args";
mcimadamore@302 3304 suffix += hasTypeArgs ? ".params" : "";
mcimadamore@302 3305 }
mcimadamore@302 3306 }
mcimadamore@302 3307 return key + suffix;
mcimadamore@302 3308 }
mcimadamore@855 3309 private JCDiagnostic getLocationDiag(Symbol location, Type site) {
mcimadamore@855 3310 if (location.kind == VAR) {
mcimadamore@855 3311 return diags.fragment("location.1",
mcimadamore@829 3312 kindName(location),
mcimadamore@829 3313 location,
mcimadamore@855 3314 location.type);
mcimadamore@855 3315 } else {
mcimadamore@855 3316 return diags.fragment("location",
mcimadamore@855 3317 typeKindName(site),
mcimadamore@855 3318 site,
mcimadamore@855 3319 null);
mcimadamore@855 3320 }
mcimadamore@829 3321 }
mcimadamore@302 3322 }
mcimadamore@302 3323
mcimadamore@302 3324 /**
mcimadamore@302 3325 * InvalidSymbolError error class indicating that a given symbol
mcimadamore@302 3326 * (either a method, a constructor or an operand) is not applicable
mcimadamore@302 3327 * given an actual arguments/type argument list.
mcimadamore@302 3328 */
mcimadamore@1215 3329 class InapplicableSymbolError extends ResolveError {
mcimadamore@302 3330
mcimadamore@1352 3331 protected MethodResolutionContext resolveContext;
mcimadamore@1352 3332
mcimadamore@1352 3333 InapplicableSymbolError(MethodResolutionContext context) {
mcimadamore@1352 3334 this(WRONG_MTH, "inapplicable symbol error", context);
mcimadamore@302 3335 }
mcimadamore@302 3336
mcimadamore@1352 3337 protected InapplicableSymbolError(int kind, String debugName, MethodResolutionContext context) {
mcimadamore@1215 3338 super(kind, debugName);
mcimadamore@1352 3339 this.resolveContext = context;
mcimadamore@302 3340 }
mcimadamore@302 3341
mcimadamore@302 3342 @Override
mcimadamore@302 3343 public String toString() {
mcimadamore@1215 3344 return super.toString();
mcimadamore@1215 3345 }
mcimadamore@1215 3346
mcimadamore@1215 3347 @Override
mcimadamore@1215 3348 public boolean exists() {
mcimadamore@1215 3349 return true;
mcimadamore@302 3350 }
mcimadamore@302 3351
mcimadamore@302 3352 @Override
mcimadamore@302 3353 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 3354 DiagnosticPosition pos,
mcimadamore@829 3355 Symbol location,
mcimadamore@302 3356 Type site,
mcimadamore@302 3357 Name name,
mcimadamore@302 3358 List<Type> argtypes,
mcimadamore@302 3359 List<Type> typeargtypes) {
mcimadamore@302 3360 if (name == names.error)
mcimadamore@302 3361 return null;
mcimadamore@302 3362
mcimadamore@1347 3363 if (syms.operatorNames.contains(name)) {
mcimadamore@853 3364 boolean isUnaryOp = argtypes.size() == 1;
mcimadamore@853 3365 String key = argtypes.size() == 1 ?
mcimadamore@853 3366 "operator.cant.be.applied" :
mcimadamore@853 3367 "operator.cant.be.applied.1";
mcimadamore@853 3368 Type first = argtypes.head;
mcimadamore@853 3369 Type second = !isUnaryOp ? argtypes.tail.head : null;
mcimadamore@853 3370 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@853 3371 key, name, first, second);
mcimadamore@302 3372 }
mcimadamore@302 3373 else {
mcimadamore@1215 3374 Candidate c = errCandidate();
mcimadamore@1759 3375 if (compactMethodDiags) {
mcimadamore@1759 3376 for (Map.Entry<Template, DiagnosticRewriter> _entry :
mcimadamore@1759 3377 MethodResolutionDiagHelper.rewriters.entrySet()) {
mcimadamore@1759 3378 if (_entry.getKey().matches(c.details)) {
mcimadamore@1759 3379 JCDiagnostic simpleDiag =
mcimadamore@1759 3380 _entry.getValue().rewriteDiagnostic(diags, pos,
mcimadamore@1759 3381 log.currentSource(), dkind, c.details);
mcimadamore@1759 3382 simpleDiag.setFlag(DiagnosticFlag.COMPRESSED);
mcimadamore@1759 3383 return simpleDiag;
mcimadamore@1759 3384 }
mcimadamore@1759 3385 }
mcimadamore@1759 3386 }
mcimadamore@1215 3387 Symbol ws = c.sym.asMemberOf(site, types);
jjg@612 3388 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@1352 3389 "cant.apply.symbol",
mcimadamore@302 3390 kindName(ws),
mcimadamore@302 3391 ws.name == names.init ? ws.owner.name : ws.name,
mcimadamore@302 3392 methodArguments(ws.type.getParameterTypes()),
mcimadamore@302 3393 methodArguments(argtypes),
mcimadamore@302 3394 kindName(ws.owner),
mcimadamore@302 3395 ws.owner.type,
mcimadamore@1215 3396 c.details);
mcimadamore@302 3397 }
mcimadamore@302 3398 }
mcimadamore@302 3399
mcimadamore@302 3400 @Override
mcimadamore@302 3401 public Symbol access(Name name, TypeSymbol location) {
mcimadamore@302 3402 return types.createErrorType(name, location, syms.errSymbol.type).tsym;
mcimadamore@302 3403 }
mcimadamore@1215 3404
mcimadamore@1215 3405 private Candidate errCandidate() {
mcimadamore@1394 3406 Candidate bestSoFar = null;
mcimadamore@1352 3407 for (Candidate c : resolveContext.candidates) {
mcimadamore@1394 3408 if (c.isApplicable()) continue;
mcimadamore@1394 3409 bestSoFar = c;
mcimadamore@1215 3410 }
mcimadamore@1394 3411 Assert.checkNonNull(bestSoFar);
mcimadamore@1394 3412 return bestSoFar;
mcimadamore@1215 3413 }
mcimadamore@302 3414 }
mcimadamore@302 3415
mcimadamore@302 3416 /**
mcimadamore@302 3417 * ResolveError error class indicating that a set of symbols
mcimadamore@302 3418 * (either methods, constructors or operands) is not applicable
mcimadamore@302 3419 * given an actual arguments/type argument list.
mcimadamore@302 3420 */
mcimadamore@1215 3421 class InapplicableSymbolsError extends InapplicableSymbolError {
mcimadamore@689 3422
mcimadamore@1352 3423 InapplicableSymbolsError(MethodResolutionContext context) {
mcimadamore@1352 3424 super(WRONG_MTHS, "inapplicable symbols", context);
mcimadamore@302 3425 }
mcimadamore@302 3426
mcimadamore@302 3427 @Override
mcimadamore@302 3428 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 3429 DiagnosticPosition pos,
mcimadamore@829 3430 Symbol location,
mcimadamore@302 3431 Type site,
mcimadamore@302 3432 Name name,
mcimadamore@302 3433 List<Type> argtypes,
mcimadamore@302 3434 List<Type> typeargtypes) {
mcimadamore@1759 3435 Map<Symbol, JCDiagnostic> candidatesMap = mapCandidates();
mcimadamore@1759 3436 Map<Symbol, JCDiagnostic> filteredCandidates = filterCandidates(candidatesMap);
mcimadamore@1759 3437 if (filteredCandidates.isEmpty()) {
mcimadamore@1759 3438 filteredCandidates = candidatesMap;
mcimadamore@1759 3439 }
mcimadamore@1759 3440 boolean truncatedDiag = candidatesMap.size() != filteredCandidates.size();
mcimadamore@1759 3441 if (filteredCandidates.size() > 1) {
mcimadamore@689 3442 JCDiagnostic err = diags.create(dkind,
mcimadamore@1759 3443 null,
mcimadamore@1759 3444 truncatedDiag ?
mcimadamore@1759 3445 EnumSet.of(DiagnosticFlag.COMPRESSED) :
mcimadamore@1759 3446 EnumSet.noneOf(DiagnosticFlag.class),
mcimadamore@689 3447 log.currentSource(),
mcimadamore@689 3448 pos,
mcimadamore@689 3449 "cant.apply.symbols",
mcimadamore@689 3450 name == names.init ? KindName.CONSTRUCTOR : absentKind(kind),
mcimadamore@1394 3451 name == names.init ? site.tsym.name : name,
mcimadamore@1415 3452 methodArguments(argtypes));
mcimadamore@1759 3453 return new JCDiagnostic.MultilineDiagnostic(err, candidateDetails(filteredCandidates, site));
mcimadamore@1759 3454 } else if (filteredCandidates.size() == 1) {
mcimadamore@1759 3455 JCDiagnostic d = new InapplicableSymbolError(resolveContext).getDiagnostic(dkind, pos,
mcimadamore@1759 3456 location, site, name, argtypes, typeargtypes);
mcimadamore@1759 3457 if (truncatedDiag) {
mcimadamore@1759 3458 d.setFlag(DiagnosticFlag.COMPRESSED);
mcimadamore@1759 3459 }
mcimadamore@1759 3460 return d;
mcimadamore@689 3461 } else {
mcimadamore@689 3462 return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind, pos,
mcimadamore@829 3463 location, site, name, argtypes, typeargtypes);
mcimadamore@689 3464 }
mcimadamore@689 3465 }
mcimadamore@689 3466 //where
mcimadamore@1759 3467 private Map<Symbol, JCDiagnostic> mapCandidates() {
mcimadamore@1759 3468 Map<Symbol, JCDiagnostic> candidates = new LinkedHashMap<Symbol, JCDiagnostic>();
mcimadamore@1759 3469 for (Candidate c : resolveContext.candidates) {
mcimadamore@1759 3470 if (c.isApplicable()) continue;
mcimadamore@1759 3471 candidates.put(c.sym, c.details);
mcimadamore@1759 3472 }
mcimadamore@1759 3473 return candidates;
mcimadamore@1215 3474 }
mcimadamore@1759 3475
mcimadamore@1759 3476 Map<Symbol, JCDiagnostic> filterCandidates(Map<Symbol, JCDiagnostic> candidatesMap) {
mcimadamore@1759 3477 Map<Symbol, JCDiagnostic> candidates = new LinkedHashMap<Symbol, JCDiagnostic>();
mcimadamore@1759 3478 for (Map.Entry<Symbol, JCDiagnostic> _entry : candidatesMap.entrySet()) {
mcimadamore@1759 3479 JCDiagnostic d = _entry.getValue();
mcimadamore@1759 3480 if (!compactMethodDiags ||
mcimadamore@1759 3481 !new Template(MethodCheckDiag.ARITY_MISMATCH.regex()).matches(d)) {
mcimadamore@1759 3482 candidates.put(_entry.getKey(), d);
mcimadamore@1759 3483 }
mcimadamore@1759 3484 }
mcimadamore@1759 3485 return candidates;
mcimadamore@1759 3486 }
mcimadamore@1759 3487
mcimadamore@1759 3488 private List<JCDiagnostic> candidateDetails(Map<Symbol, JCDiagnostic> candidatesMap, Type site) {
mcimadamore@1759 3489 List<JCDiagnostic> details = List.nil();
mcimadamore@1759 3490 for (Map.Entry<Symbol, JCDiagnostic> _entry : candidatesMap.entrySet()) {
mcimadamore@1759 3491 Symbol sym = _entry.getKey();
mcimadamore@1759 3492 JCDiagnostic detailDiag = diags.fragment("inapplicable.method",
mcimadamore@1759 3493 Kinds.kindName(sym),
mcimadamore@1759 3494 sym.location(site, types),
mcimadamore@1759 3495 sym.asMemberOf(site, types),
mcimadamore@1759 3496 _entry.getValue());
mcimadamore@1759 3497 details = details.prepend(detailDiag);
mcimadamore@1759 3498 }
mcimadamore@1759 3499 //typically members are visited in reverse order (see Scope)
mcimadamore@1759 3500 //so we need to reverse the candidate list so that candidates
mcimadamore@1759 3501 //conform to source order
mcimadamore@1759 3502 return details;
mcimadamore@1759 3503 }
mcimadamore@302 3504 }
mcimadamore@302 3505
mcimadamore@302 3506 /**
mcimadamore@302 3507 * An InvalidSymbolError error class indicating that a symbol is not
mcimadamore@302 3508 * accessible from a given site
mcimadamore@302 3509 */
mcimadamore@302 3510 class AccessError extends InvalidSymbolError {
mcimadamore@302 3511
mcimadamore@302 3512 private Env<AttrContext> env;
mcimadamore@302 3513 private Type site;
duke@1 3514
duke@1 3515 AccessError(Symbol sym) {
duke@1 3516 this(null, null, sym);
duke@1 3517 }
duke@1 3518
duke@1 3519 AccessError(Env<AttrContext> env, Type site, Symbol sym) {
duke@1 3520 super(HIDDEN, sym, "access error");
duke@1 3521 this.env = env;
duke@1 3522 this.site = site;
duke@1 3523 if (debugResolve)
duke@1 3524 log.error("proc.messager", sym + " @ " + site + " is inaccessible.");
duke@1 3525 }
duke@1 3526
mcimadamore@302 3527 @Override
mcimadamore@302 3528 public boolean exists() {
mcimadamore@302 3529 return false;
mcimadamore@302 3530 }
duke@1 3531
mcimadamore@302 3532 @Override
mcimadamore@302 3533 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 3534 DiagnosticPosition pos,
mcimadamore@829 3535 Symbol location,
mcimadamore@302 3536 Type site,
mcimadamore@302 3537 Name name,
mcimadamore@302 3538 List<Type> argtypes,
mcimadamore@302 3539 List<Type> typeargtypes) {
jjg@1374 3540 if (sym.owner.type.hasTag(ERROR))
mcimadamore@302 3541 return null;
mcimadamore@302 3542
mcimadamore@302 3543 if (sym.name == names.init && sym.owner != site.tsym) {
mcimadamore@302 3544 return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind,
mcimadamore@829 3545 pos, location, site, name, argtypes, typeargtypes);
mcimadamore@302 3546 }
mcimadamore@302 3547 else if ((sym.flags() & PUBLIC) != 0
mcimadamore@302 3548 || (env != null && this.site != null
mcimadamore@302 3549 && !isAccessible(env, this.site))) {
jjg@612 3550 return diags.create(dkind, log.currentSource(),
mcimadamore@302 3551 pos, "not.def.access.class.intf.cant.access",
mcimadamore@302 3552 sym, sym.location());
mcimadamore@302 3553 }
mcimadamore@302 3554 else if ((sym.flags() & (PRIVATE | PROTECTED)) != 0) {
jjg@612 3555 return diags.create(dkind, log.currentSource(),
mcimadamore@302 3556 pos, "report.access", sym,
mcimadamore@302 3557 asFlagSet(sym.flags() & (PRIVATE | PROTECTED)),
mcimadamore@302 3558 sym.location());
mcimadamore@302 3559 }
mcimadamore@302 3560 else {
jjg@612 3561 return diags.create(dkind, log.currentSource(),
mcimadamore@302 3562 pos, "not.def.public.cant.access", sym, sym.location());
duke@1 3563 }
duke@1 3564 }
duke@1 3565 }
duke@1 3566
mcimadamore@302 3567 /**
mcimadamore@302 3568 * InvalidSymbolError error class indicating that an instance member
mcimadamore@302 3569 * has erroneously been accessed from a static context.
duke@1 3570 */
mcimadamore@302 3571 class StaticError extends InvalidSymbolError {
mcimadamore@302 3572
duke@1 3573 StaticError(Symbol sym) {
duke@1 3574 super(STATICERR, sym, "static error");
duke@1 3575 }
duke@1 3576
mcimadamore@302 3577 @Override
mcimadamore@302 3578 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 3579 DiagnosticPosition pos,
mcimadamore@829 3580 Symbol location,
mcimadamore@302 3581 Type site,
mcimadamore@302 3582 Name name,
mcimadamore@302 3583 List<Type> argtypes,
mcimadamore@302 3584 List<Type> typeargtypes) {
jjg@1374 3585 Symbol errSym = ((sym.kind == TYP && sym.type.hasTag(CLASS))
mcimadamore@80 3586 ? types.erasure(sym.type).tsym
mcimadamore@80 3587 : sym);
jjg@612 3588 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@302 3589 "non-static.cant.be.ref", kindName(sym), errSym);
duke@1 3590 }
duke@1 3591 }
duke@1 3592
mcimadamore@302 3593 /**
mcimadamore@302 3594 * InvalidSymbolError error class indicating that a pair of symbols
mcimadamore@302 3595 * (either methods, constructors or operands) are ambiguous
mcimadamore@302 3596 * given an actual arguments/type argument list.
duke@1 3597 */
mcimadamore@1480 3598 class AmbiguityError extends ResolveError {
mcimadamore@302 3599
mcimadamore@302 3600 /** The other maximally specific symbol */
mcimadamore@1480 3601 List<Symbol> ambiguousSyms = List.nil();
mcimadamore@1480 3602
mcimadamore@1480 3603 @Override
mcimadamore@1480 3604 public boolean exists() {
mcimadamore@1480 3605 return true;
mcimadamore@1480 3606 }
duke@1 3607
duke@1 3608 AmbiguityError(Symbol sym1, Symbol sym2) {
mcimadamore@1480 3609 super(AMBIGUOUS, "ambiguity error");
mcimadamore@1480 3610 ambiguousSyms = flatten(sym2).appendList(flatten(sym1));
mcimadamore@1480 3611 }
mcimadamore@1480 3612
mcimadamore@1480 3613 private List<Symbol> flatten(Symbol sym) {
mcimadamore@1480 3614 if (sym.kind == AMBIGUOUS) {
mcimadamore@1480 3615 return ((AmbiguityError)sym).ambiguousSyms;
mcimadamore@1480 3616 } else {
mcimadamore@1480 3617 return List.of(sym);
mcimadamore@1480 3618 }
mcimadamore@1480 3619 }
mcimadamore@1480 3620
mcimadamore@1480 3621 AmbiguityError addAmbiguousSymbol(Symbol s) {
mcimadamore@1480 3622 ambiguousSyms = ambiguousSyms.prepend(s);
mcimadamore@1480 3623 return this;
duke@1 3624 }
duke@1 3625
mcimadamore@302 3626 @Override
mcimadamore@302 3627 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 3628 DiagnosticPosition pos,
mcimadamore@829 3629 Symbol location,
mcimadamore@302 3630 Type site,
mcimadamore@302 3631 Name name,
mcimadamore@302 3632 List<Type> argtypes,
mcimadamore@302 3633 List<Type> typeargtypes) {
mcimadamore@1480 3634 List<Symbol> diagSyms = ambiguousSyms.reverse();
mcimadamore@1480 3635 Symbol s1 = diagSyms.head;
mcimadamore@1480 3636 Symbol s2 = diagSyms.tail.head;
mcimadamore@1480 3637 Name sname = s1.name;
mcimadamore@1480 3638 if (sname == names.init) sname = s1.owner.name;
jjg@612 3639 return diags.create(dkind, log.currentSource(),
mcimadamore@302 3640 pos, "ref.ambiguous", sname,
mcimadamore@1480 3641 kindName(s1),
mcimadamore@1480 3642 s1,
mcimadamore@1480 3643 s1.location(site, types),
mcimadamore@1480 3644 kindName(s2),
mcimadamore@1480 3645 s2,
mcimadamore@1480 3646 s2.location(site, types));
mcimadamore@1480 3647 }
mcimadamore@1480 3648
mcimadamore@1480 3649 /**
mcimadamore@1480 3650 * If multiple applicable methods are found during overload and none of them
mcimadamore@1480 3651 * is more specific than the others, attempt to merge their signatures.
mcimadamore@1480 3652 */
mcimadamore@1480 3653 Symbol mergeAbstracts(Type site) {
mcimadamore@1875 3654 List<Symbol> ambiguousInOrder = ambiguousSyms.reverse();
mcimadamore@1875 3655 for (Symbol s : ambiguousInOrder) {
mcimadamore@1875 3656 Type mt = types.memberType(site, s);
mcimadamore@1875 3657 boolean found = true;
mcimadamore@1875 3658 List<Type> allThrown = mt.getThrownTypes();
mcimadamore@1875 3659 for (Symbol s2 : ambiguousInOrder) {
mcimadamore@1875 3660 Type mt2 = types.memberType(site, s2);
mcimadamore@1875 3661 if ((s2.flags() & ABSTRACT) == 0 ||
mcimadamore@1875 3662 !types.overrideEquivalent(mt, mt2) ||
mcimadamore@1875 3663 !types.isSameTypes(s.erasure(types).getParameterTypes(),
mcimadamore@1875 3664 s2.erasure(types).getParameterTypes())) {
mcimadamore@1875 3665 //ambiguity cannot be resolved
mcimadamore@1480 3666 return this;
mcimadamore@1480 3667 }
mcimadamore@1875 3668 Type mst = mostSpecificReturnType(mt, mt2);
mcimadamore@1875 3669 if (mst == null || mst != mt) {
mcimadamore@1875 3670 found = false;
mcimadamore@1875 3671 break;
mcimadamore@1875 3672 }
mcimadamore@1875 3673 allThrown = chk.intersect(allThrown, mt2.getThrownTypes());
mcimadamore@1875 3674 }
mcimadamore@1875 3675 if (found) {
mcimadamore@1875 3676 //all ambiguous methods were abstract and one method had
mcimadamore@1875 3677 //most specific return type then others
mcimadamore@1875 3678 return (allThrown == mt.getThrownTypes()) ?
mcimadamore@1875 3679 s : new MethodSymbol(
mcimadamore@1875 3680 s.flags(),
mcimadamore@1875 3681 s.name,
mcimadamore@1875 3682 types.createMethodTypeWithThrown(mt, allThrown),
mcimadamore@1875 3683 s.owner);
mcimadamore@1480 3684 }
mcimadamore@1480 3685 }
mcimadamore@1875 3686 return this;
mcimadamore@1480 3687 }
mcimadamore@1480 3688
mcimadamore@1480 3689 @Override
mcimadamore@1480 3690 protected Symbol access(Name name, TypeSymbol location) {
mcimadamore@1498 3691 Symbol firstAmbiguity = ambiguousSyms.last();
mcimadamore@1498 3692 return firstAmbiguity.kind == TYP ?
mcimadamore@1498 3693 types.createErrorType(name, location, firstAmbiguity.type).tsym :
mcimadamore@1498 3694 firstAmbiguity;
duke@1 3695 }
duke@1 3696 }
mcimadamore@160 3697
mcimadamore@1599 3698 class BadVarargsMethod extends ResolveError {
mcimadamore@1599 3699
mcimadamore@1599 3700 ResolveError delegatedError;
mcimadamore@1599 3701
mcimadamore@1599 3702 BadVarargsMethod(ResolveError delegatedError) {
mcimadamore@1599 3703 super(delegatedError.kind, "badVarargs");
mcimadamore@1599 3704 this.delegatedError = delegatedError;
mcimadamore@1599 3705 }
mcimadamore@1599 3706
mcimadamore@1599 3707 @Override
mcimadamore@1697 3708 public Symbol baseSymbol() {
mcimadamore@1697 3709 return delegatedError.baseSymbol();
mcimadamore@1697 3710 }
mcimadamore@1697 3711
mcimadamore@1697 3712 @Override
mcimadamore@1599 3713 protected Symbol access(Name name, TypeSymbol location) {
mcimadamore@1599 3714 return delegatedError.access(name, location);
mcimadamore@1599 3715 }
mcimadamore@1599 3716
mcimadamore@1599 3717 @Override
mcimadamore@1599 3718 public boolean exists() {
mcimadamore@1599 3719 return true;
mcimadamore@1599 3720 }
mcimadamore@1599 3721
mcimadamore@1599 3722 @Override
mcimadamore@1599 3723 JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos, Symbol location, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) {
mcimadamore@1599 3724 return delegatedError.getDiagnostic(dkind, pos, location, site, name, argtypes, typeargtypes);
mcimadamore@1599 3725 }
mcimadamore@1599 3726 }
mcimadamore@1599 3727
mcimadamore@1759 3728 /**
mcimadamore@1759 3729 * Helper class for method resolution diagnostic simplification.
mcimadamore@1759 3730 * Certain resolution diagnostic are rewritten as simpler diagnostic
mcimadamore@1759 3731 * where the enclosing resolution diagnostic (i.e. 'inapplicable method')
mcimadamore@1759 3732 * is stripped away, as it doesn't carry additional info. The logic
mcimadamore@1759 3733 * for matching a given diagnostic is given in terms of a template
mcimadamore@1759 3734 * hierarchy: a diagnostic template can be specified programmatically,
mcimadamore@1759 3735 * so that only certain diagnostics are matched. Each templete is then
mcimadamore@1759 3736 * associated with a rewriter object that carries out the task of rewtiting
mcimadamore@1759 3737 * the diagnostic to a simpler one.
mcimadamore@1759 3738 */
mcimadamore@1759 3739 static class MethodResolutionDiagHelper {
mcimadamore@1759 3740
mcimadamore@1759 3741 /**
mcimadamore@1759 3742 * A diagnostic rewriter transforms a method resolution diagnostic
mcimadamore@1759 3743 * into a simpler one
mcimadamore@1759 3744 */
mcimadamore@1759 3745 interface DiagnosticRewriter {
mcimadamore@1759 3746 JCDiagnostic rewriteDiagnostic(JCDiagnostic.Factory diags,
mcimadamore@1759 3747 DiagnosticPosition preferedPos, DiagnosticSource preferredSource,
mcimadamore@1759 3748 DiagnosticType preferredKind, JCDiagnostic d);
mcimadamore@1759 3749 }
mcimadamore@1759 3750
mcimadamore@1759 3751 /**
mcimadamore@1759 3752 * A diagnostic template is made up of two ingredients: (i) a regular
mcimadamore@1759 3753 * expression for matching a diagnostic key and (ii) a list of sub-templates
mcimadamore@1759 3754 * for matching diagnostic arguments.
mcimadamore@1759 3755 */
mcimadamore@1759 3756 static class Template {
mcimadamore@1759 3757
mcimadamore@1759 3758 /** regex used to match diag key */
mcimadamore@1759 3759 String regex;
mcimadamore@1759 3760
mcimadamore@1759 3761 /** templates used to match diagnostic args */
mcimadamore@1759 3762 Template[] subTemplates;
mcimadamore@1759 3763
mcimadamore@1759 3764 Template(String key, Template... subTemplates) {
mcimadamore@1759 3765 this.regex = key;
mcimadamore@1759 3766 this.subTemplates = subTemplates;
mcimadamore@1759 3767 }
mcimadamore@1759 3768
mcimadamore@1759 3769 /**
mcimadamore@1759 3770 * Returns true if the regex matches the diagnostic key and if
mcimadamore@1759 3771 * all diagnostic arguments are matches by corresponding sub-templates.
mcimadamore@1759 3772 */
mcimadamore@1759 3773 boolean matches(Object o) {
mcimadamore@1759 3774 JCDiagnostic d = (JCDiagnostic)o;
mcimadamore@1759 3775 Object[] args = d.getArgs();
mcimadamore@1759 3776 if (!d.getCode().matches(regex) ||
mcimadamore@1759 3777 subTemplates.length != d.getArgs().length) {
mcimadamore@1759 3778 return false;
mcimadamore@1759 3779 }
mcimadamore@1759 3780 for (int i = 0; i < args.length ; i++) {
mcimadamore@1759 3781 if (!subTemplates[i].matches(args[i])) {
mcimadamore@1759 3782 return false;
mcimadamore@1759 3783 }
mcimadamore@1759 3784 }
mcimadamore@1759 3785 return true;
mcimadamore@1759 3786 }
mcimadamore@1759 3787 }
mcimadamore@1759 3788
mcimadamore@1759 3789 /** a dummy template that match any diagnostic argument */
mcimadamore@1759 3790 static final Template skip = new Template("") {
mcimadamore@1759 3791 @Override
mcimadamore@1759 3792 boolean matches(Object d) {
mcimadamore@1759 3793 return true;
mcimadamore@1759 3794 }
mcimadamore@1759 3795 };
mcimadamore@1759 3796
mcimadamore@1759 3797 /** rewriter map used for method resolution simplification */
mcimadamore@1759 3798 static final Map<Template, DiagnosticRewriter> rewriters =
mcimadamore@1759 3799 new LinkedHashMap<Template, DiagnosticRewriter>();
mcimadamore@1759 3800
mcimadamore@1759 3801 static {
mcimadamore@1759 3802 String argMismatchRegex = MethodCheckDiag.ARG_MISMATCH.regex();
mcimadamore@1759 3803 rewriters.put(new Template(argMismatchRegex, new Template("(.*)(bad.arg.types.in.lambda)", skip, skip)),
mcimadamore@1759 3804 new DiagnosticRewriter() {
mcimadamore@1759 3805 @Override
mcimadamore@1759 3806 public JCDiagnostic rewriteDiagnostic(JCDiagnostic.Factory diags,
mcimadamore@1759 3807 DiagnosticPosition preferedPos, DiagnosticSource preferredSource,
mcimadamore@1759 3808 DiagnosticType preferredKind, JCDiagnostic d) {
mcimadamore@1759 3809 return (JCDiagnostic)((JCDiagnostic)d.getArgs()[0]).getArgs()[1];
mcimadamore@1759 3810 }
mcimadamore@1759 3811 });
mcimadamore@1759 3812
mcimadamore@1759 3813 rewriters.put(new Template(argMismatchRegex, skip),
mcimadamore@1759 3814 new DiagnosticRewriter() {
mcimadamore@1759 3815 @Override
mcimadamore@1759 3816 public JCDiagnostic rewriteDiagnostic(JCDiagnostic.Factory diags,
mcimadamore@1759 3817 DiagnosticPosition preferedPos, DiagnosticSource preferredSource,
mcimadamore@1759 3818 DiagnosticType preferredKind, JCDiagnostic d) {
mcimadamore@1759 3819 JCDiagnostic cause = (JCDiagnostic)d.getArgs()[0];
mcimadamore@1759 3820 return diags.create(preferredKind, preferredSource, d.getDiagnosticPosition(),
mcimadamore@1759 3821 "prob.found.req", cause);
mcimadamore@1759 3822 }
mcimadamore@1759 3823 });
mcimadamore@1759 3824 }
mcimadamore@1759 3825 }
mcimadamore@1759 3826
mcimadamore@160 3827 enum MethodResolutionPhase {
mcimadamore@160 3828 BASIC(false, false),
mcimadamore@160 3829 BOX(true, false),
mcimadamore@1394 3830 VARARITY(true, true) {
mcimadamore@1394 3831 @Override
mcimadamore@1394 3832 public Symbol mergeResults(Symbol bestSoFar, Symbol sym) {
mcimadamore@1394 3833 switch (sym.kind) {
mcimadamore@1394 3834 case WRONG_MTH:
mcimadamore@1394 3835 return (bestSoFar.kind == WRONG_MTH || bestSoFar.kind == WRONG_MTHS) ?
mcimadamore@1394 3836 bestSoFar :
mcimadamore@1394 3837 sym;
mcimadamore@1394 3838 case ABSENT_MTH:
mcimadamore@1394 3839 return bestSoFar;
mcimadamore@1394 3840 default:
mcimadamore@1394 3841 return sym;
mcimadamore@1394 3842 }
mcimadamore@1394 3843 }
mcimadamore@1394 3844 };
mcimadamore@160 3845
vromero@1442 3846 final boolean isBoxingRequired;
vromero@1442 3847 final boolean isVarargsRequired;
mcimadamore@160 3848
mcimadamore@160 3849 MethodResolutionPhase(boolean isBoxingRequired, boolean isVarargsRequired) {
mcimadamore@160 3850 this.isBoxingRequired = isBoxingRequired;
mcimadamore@160 3851 this.isVarargsRequired = isVarargsRequired;
mcimadamore@160 3852 }
mcimadamore@160 3853
mcimadamore@160 3854 public boolean isBoxingRequired() {
mcimadamore@160 3855 return isBoxingRequired;
mcimadamore@160 3856 }
mcimadamore@160 3857
mcimadamore@160 3858 public boolean isVarargsRequired() {
mcimadamore@160 3859 return isVarargsRequired;
mcimadamore@160 3860 }
mcimadamore@160 3861
mcimadamore@160 3862 public boolean isApplicable(boolean boxingEnabled, boolean varargsEnabled) {
mcimadamore@160 3863 return (varargsEnabled || !isVarargsRequired) &&
mcimadamore@160 3864 (boxingEnabled || !isBoxingRequired);
mcimadamore@160 3865 }
mcimadamore@1394 3866
mcimadamore@1394 3867 public Symbol mergeResults(Symbol prev, Symbol sym) {
mcimadamore@1394 3868 return sym;
mcimadamore@1394 3869 }
mcimadamore@160 3870 }
mcimadamore@160 3871
mcimadamore@160 3872 final List<MethodResolutionPhase> methodResolutionSteps = List.of(BASIC, BOX, VARARITY);
mcimadamore@160 3873
mcimadamore@1215 3874 /**
mcimadamore@1215 3875 * A resolution context is used to keep track of intermediate results of
mcimadamore@1215 3876 * overload resolution, such as list of method that are not applicable
mcimadamore@1215 3877 * (used to generate more precise diagnostics) and so on. Resolution contexts
mcimadamore@1215 3878 * can be nested - this means that when each overload resolution routine should
mcimadamore@1215 3879 * work within the resolution context it created.
mcimadamore@1215 3880 */
mcimadamore@1215 3881 class MethodResolutionContext {
mcimadamore@689 3882
mcimadamore@1215 3883 private List<Candidate> candidates = List.nil();
mcimadamore@1114 3884
mcimadamore@1347 3885 MethodResolutionPhase step = null;
mcimadamore@1215 3886
mcimadamore@1674 3887 MethodCheck methodCheck = resolveMethodCheck;
mcimadamore@1674 3888
mcimadamore@1215 3889 private boolean internalResolution = false;
mcimadamore@1347 3890 private DeferredAttr.AttrMode attrMode = DeferredAttr.AttrMode.SPECULATIVE;
mcimadamore@1215 3891
mcimadamore@1215 3892 void addInapplicableCandidate(Symbol sym, JCDiagnostic details) {
mcimadamore@1215 3893 Candidate c = new Candidate(currentResolutionContext.step, sym, details, null);
mcimadamore@1352 3894 candidates = candidates.append(c);
mcimadamore@1215 3895 }
mcimadamore@1215 3896
mcimadamore@1215 3897 void addApplicableCandidate(Symbol sym, Type mtype) {
mcimadamore@1215 3898 Candidate c = new Candidate(currentResolutionContext.step, sym, null, mtype);
mcimadamore@1215 3899 candidates = candidates.append(c);
mcimadamore@1215 3900 }
mcimadamore@1215 3901
mcimadamore@1551 3902 DeferredAttrContext deferredAttrContext(Symbol sym, InferenceContext inferenceContext, ResultInfo pendingResult, Warner warn) {
mcimadamore@1551 3903 return deferredAttr.new DeferredAttrContext(attrMode, sym, step, inferenceContext, pendingResult != null ? pendingResult.checkContext.deferredAttrContext() : deferredAttr.emptyDeferredAttrContext, warn);
mcimadamore@1479 3904 }
mcimadamore@1479 3905
mcimadamore@1215 3906 /**
mcimadamore@1215 3907 * This class represents an overload resolution candidate. There are two
mcimadamore@1215 3908 * kinds of candidates: applicable methods and inapplicable methods;
mcimadamore@1215 3909 * applicable methods have a pointer to the instantiated method type,
mcimadamore@1215 3910 * while inapplicable candidates contain further details about the
mcimadamore@1215 3911 * reason why the method has been considered inapplicable.
mcimadamore@1215 3912 */
vromero@1588 3913 @SuppressWarnings("overrides")
mcimadamore@1215 3914 class Candidate {
mcimadamore@1215 3915
mcimadamore@1215 3916 final MethodResolutionPhase step;
mcimadamore@1215 3917 final Symbol sym;
mcimadamore@1215 3918 final JCDiagnostic details;
mcimadamore@1215 3919 final Type mtype;
mcimadamore@1215 3920
mcimadamore@1215 3921 private Candidate(MethodResolutionPhase step, Symbol sym, JCDiagnostic details, Type mtype) {
mcimadamore@1215 3922 this.step = step;
mcimadamore@1215 3923 this.sym = sym;
mcimadamore@1215 3924 this.details = details;
mcimadamore@1215 3925 this.mtype = mtype;
mcimadamore@1215 3926 }
mcimadamore@1215 3927
mcimadamore@1215 3928 @Override
mcimadamore@1215 3929 public boolean equals(Object o) {
mcimadamore@1215 3930 if (o instanceof Candidate) {
mcimadamore@1215 3931 Symbol s1 = this.sym;
mcimadamore@1215 3932 Symbol s2 = ((Candidate)o).sym;
mcimadamore@1215 3933 if ((s1 != s2 &&
mcimadamore@1352 3934 (s1.overrides(s2, s1.owner.type.tsym, types, false) ||
mcimadamore@1352 3935 (s2.overrides(s1, s2.owner.type.tsym, types, false)))) ||
mcimadamore@1352 3936 ((s1.isConstructor() || s2.isConstructor()) && s1.owner != s2.owner))
mcimadamore@1215 3937 return true;
mcimadamore@1215 3938 }
mcimadamore@1215 3939 return false;
mcimadamore@1215 3940 }
mcimadamore@1215 3941
mcimadamore@1215 3942 boolean isApplicable() {
mcimadamore@1215 3943 return mtype != null;
mcimadamore@1215 3944 }
mcimadamore@1215 3945 }
mcimadamore@1347 3946
mcimadamore@1347 3947 DeferredAttr.AttrMode attrMode() {
mcimadamore@1347 3948 return attrMode;
mcimadamore@1347 3949 }
mcimadamore@1347 3950
mcimadamore@1347 3951 boolean internal() {
mcimadamore@1347 3952 return internalResolution;
mcimadamore@1347 3953 }
mcimadamore@160 3954 }
mcimadamore@1215 3955
mcimadamore@1215 3956 MethodResolutionContext currentResolutionContext = null;
duke@1 3957 }

mercurial