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

Tue, 18 Dec 2012 10:23:40 +0100

author
ohrstrom
date
Tue, 18 Dec 2012 10:23:40 +0100
changeset 1460
92fcf299cd09
parent 1456
f20568328a57
child 1479
38d3d1027f5a
permissions
-rw-r--r--

8004657: Add hooks to javac to enable reporting dependency information.
Reviewed-by: jjg, mcimadamore

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

mercurial