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

Wed, 17 Jul 2013 14:19:25 +0100

author
mcimadamore
date
Wed, 17 Jul 2013 14:19:25 +0100
changeset 1904
b577222ef7b3
parent 1901
db2c539819dd
child 1921
dae52d74c1fc
permissions
-rw-r--r--

8019340: varargs-related warnings are meaningless on signature-polymorphic methods such as MethodHandle.invokeExact
Summary: Disable certain varargs warnings when compiling polymorphic signature calls
Reviewed-by: jjg

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

mercurial