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

Mon, 26 Mar 2012 15:28:22 +0100

author
mcimadamore
date
Mon, 26 Mar 2012 15:28:22 +0100
changeset 1238
e28a06a3c5d9
parent 1221
c2234816495f
child 1239
2827076dbf64
permissions
-rw-r--r--

7151492: Encapsulate check logic into Attr.ResultInfo
Summary: ResultInfo class should be used to make attribution code transparent w.r.t. check logic being used
Reviewed-by: jjg, dlsmith

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@1114 30 import com.sun.tools.javac.code.Type.*;
mcimadamore@1114 31 import com.sun.tools.javac.code.Symbol.*;
mcimadamore@1238 32 import com.sun.tools.javac.comp.Attr.ResultInfo;
mcimadamore@1238 33 import com.sun.tools.javac.comp.Check.CheckContext;
mcimadamore@1215 34 import com.sun.tools.javac.comp.Resolve.MethodResolutionContext.Candidate;
duke@1 35 import com.sun.tools.javac.jvm.*;
duke@1 36 import com.sun.tools.javac.tree.*;
mcimadamore@1114 37 import com.sun.tools.javac.tree.JCTree.*;
mcimadamore@1114 38 import com.sun.tools.javac.util.*;
mcimadamore@1114 39 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
mcimadamore@1114 40 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
mcimadamore@1114 41 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
duke@1 42
mcimadamore@1114 43 import java.util.Arrays;
mcimadamore@1114 44 import java.util.Collection;
mcimadamore@1215 45 import java.util.EnumMap;
mcimadamore@1114 46 import java.util.EnumSet;
mcimadamore@1114 47 import java.util.HashSet;
mcimadamore@1114 48 import java.util.Map;
mcimadamore@1114 49 import java.util.Set;
mcimadamore@1114 50
mcimadamore@1114 51 import javax.lang.model.element.ElementVisitor;
duke@1 52
duke@1 53 import static com.sun.tools.javac.code.Flags.*;
jjg@1127 54 import static com.sun.tools.javac.code.Flags.BLOCK;
duke@1 55 import static com.sun.tools.javac.code.Kinds.*;
jjg@1127 56 import static com.sun.tools.javac.code.Kinds.ERRONEOUS;
duke@1 57 import static com.sun.tools.javac.code.TypeTags.*;
mcimadamore@1114 58 import static com.sun.tools.javac.comp.Resolve.MethodResolutionPhase.*;
jjg@1127 59 import static com.sun.tools.javac.tree.JCTree.Tag.*;
mcimadamore@160 60
duke@1 61 /** Helper class for name resolution, used mostly by the attribution phase.
duke@1 62 *
jjg@581 63 * <p><b>This is NOT part of any supported API.
jjg@581 64 * If you write code that depends on this, you do so at your own risk.
duke@1 65 * This code and its internal interfaces are subject to change or
duke@1 66 * deletion without notice.</b>
duke@1 67 */
duke@1 68 public class Resolve {
duke@1 69 protected static final Context.Key<Resolve> resolveKey =
duke@1 70 new Context.Key<Resolve>();
duke@1 71
jjg@113 72 Names names;
duke@1 73 Log log;
duke@1 74 Symtab syms;
mcimadamore@1238 75 Attr attr;
duke@1 76 Check chk;
duke@1 77 Infer infer;
duke@1 78 ClassReader reader;
duke@1 79 TreeInfo treeinfo;
duke@1 80 Types types;
mcimadamore@89 81 JCDiagnostic.Factory diags;
duke@1 82 public final boolean boxingEnabled; // = source.allowBoxing();
duke@1 83 public final boolean varargsEnabled; // = source.allowVarargs();
mcimadamore@674 84 public final boolean allowMethodHandles;
duke@1 85 private final boolean debugResolve;
mcimadamore@1114 86 final EnumSet<VerboseResolutionMode> verboseResolutionMode;
duke@1 87
mcimadamore@674 88 Scope polymorphicSignatureScope;
mcimadamore@674 89
mcimadamore@1215 90 protected Resolve(Context context) {
mcimadamore@1215 91 context.put(resolveKey, this);
mcimadamore@1215 92 syms = Symtab.instance(context);
mcimadamore@1215 93
mcimadamore@1215 94 varNotFound = new
mcimadamore@1215 95 SymbolNotFoundError(ABSENT_VAR);
mcimadamore@1215 96 wrongMethod = new
mcimadamore@1215 97 InapplicableSymbolError();
mcimadamore@1215 98 wrongMethods = new
mcimadamore@1215 99 InapplicableSymbolsError();
mcimadamore@1215 100 methodNotFound = new
mcimadamore@1215 101 SymbolNotFoundError(ABSENT_MTH);
mcimadamore@1215 102 typeNotFound = new
mcimadamore@1215 103 SymbolNotFoundError(ABSENT_TYP);
mcimadamore@1215 104
mcimadamore@1215 105 names = Names.instance(context);
mcimadamore@1215 106 log = Log.instance(context);
mcimadamore@1238 107 attr = Attr.instance(context);
mcimadamore@1215 108 chk = Check.instance(context);
mcimadamore@1215 109 infer = Infer.instance(context);
mcimadamore@1215 110 reader = ClassReader.instance(context);
mcimadamore@1215 111 treeinfo = TreeInfo.instance(context);
mcimadamore@1215 112 types = Types.instance(context);
mcimadamore@1215 113 diags = JCDiagnostic.Factory.instance(context);
mcimadamore@1215 114 Source source = Source.instance(context);
mcimadamore@1215 115 boxingEnabled = source.allowBoxing();
mcimadamore@1215 116 varargsEnabled = source.allowVarargs();
mcimadamore@1215 117 Options options = Options.instance(context);
mcimadamore@1215 118 debugResolve = options.isSet("debugresolve");
mcimadamore@1215 119 verboseResolutionMode = VerboseResolutionMode.getVerboseResolutionMode(options);
mcimadamore@1215 120 Target target = Target.instance(context);
mcimadamore@1215 121 allowMethodHandles = target.hasMethodHandles();
mcimadamore@1215 122 polymorphicSignatureScope = new Scope(syms.noSymbol);
mcimadamore@1215 123
mcimadamore@1215 124 inapplicableMethodException = new InapplicableMethodException(diags);
mcimadamore@1215 125 }
mcimadamore@1215 126
mcimadamore@1215 127 /** error symbols, which are returned when resolution fails
mcimadamore@1215 128 */
mcimadamore@1215 129 private final SymbolNotFoundError varNotFound;
mcimadamore@1215 130 private final InapplicableSymbolError wrongMethod;
mcimadamore@1215 131 private final InapplicableSymbolsError wrongMethods;
mcimadamore@1215 132 private final SymbolNotFoundError methodNotFound;
mcimadamore@1215 133 private final SymbolNotFoundError typeNotFound;
mcimadamore@1215 134
mcimadamore@1215 135 public static Resolve instance(Context context) {
mcimadamore@1215 136 Resolve instance = context.get(resolveKey);
mcimadamore@1215 137 if (instance == null)
mcimadamore@1215 138 instance = new Resolve(context);
mcimadamore@1215 139 return instance;
mcimadamore@1215 140 }
mcimadamore@1215 141
mcimadamore@1215 142 // <editor-fold defaultstate="collapsed" desc="Verbose resolution diagnostics support">
mcimadamore@1114 143 enum VerboseResolutionMode {
mcimadamore@1114 144 SUCCESS("success"),
mcimadamore@1114 145 FAILURE("failure"),
mcimadamore@1114 146 APPLICABLE("applicable"),
mcimadamore@1114 147 INAPPLICABLE("inapplicable"),
mcimadamore@1114 148 DEFERRED_INST("deferred-inference"),
mcimadamore@1114 149 PREDEF("predef"),
mcimadamore@1114 150 OBJECT_INIT("object-init"),
mcimadamore@1114 151 INTERNAL("internal");
mcimadamore@1114 152
mcimadamore@1114 153 String opt;
mcimadamore@1114 154
mcimadamore@1114 155 private VerboseResolutionMode(String opt) {
mcimadamore@1114 156 this.opt = opt;
mcimadamore@1114 157 }
mcimadamore@1114 158
mcimadamore@1114 159 static EnumSet<VerboseResolutionMode> getVerboseResolutionMode(Options opts) {
mcimadamore@1114 160 String s = opts.get("verboseResolution");
mcimadamore@1114 161 EnumSet<VerboseResolutionMode> res = EnumSet.noneOf(VerboseResolutionMode.class);
mcimadamore@1114 162 if (s == null) return res;
mcimadamore@1114 163 if (s.contains("all")) {
mcimadamore@1114 164 res = EnumSet.allOf(VerboseResolutionMode.class);
mcimadamore@1114 165 }
mcimadamore@1114 166 Collection<String> args = Arrays.asList(s.split(","));
mcimadamore@1114 167 for (VerboseResolutionMode mode : values()) {
mcimadamore@1114 168 if (args.contains(mode.opt)) {
mcimadamore@1114 169 res.add(mode);
mcimadamore@1114 170 } else if (args.contains("-" + mode.opt)) {
mcimadamore@1114 171 res.remove(mode);
mcimadamore@1114 172 }
mcimadamore@1114 173 }
mcimadamore@1114 174 return res;
mcimadamore@1114 175 }
mcimadamore@1114 176 }
mcimadamore@1114 177
mcimadamore@1215 178 void reportVerboseResolutionDiagnostic(DiagnosticPosition dpos, Name name, Type site,
mcimadamore@1215 179 List<Type> argtypes, List<Type> typeargtypes, Symbol bestSoFar) {
mcimadamore@1215 180 boolean success = bestSoFar.kind < ERRONEOUS;
mcimadamore@1215 181
mcimadamore@1215 182 if (success && !verboseResolutionMode.contains(VerboseResolutionMode.SUCCESS)) {
mcimadamore@1215 183 return;
mcimadamore@1215 184 } else if (!success && !verboseResolutionMode.contains(VerboseResolutionMode.FAILURE)) {
mcimadamore@1215 185 return;
mcimadamore@1215 186 }
mcimadamore@1215 187
mcimadamore@1215 188 if (bestSoFar.name == names.init &&
mcimadamore@1215 189 bestSoFar.owner == syms.objectType.tsym &&
mcimadamore@1215 190 !verboseResolutionMode.contains(VerboseResolutionMode.OBJECT_INIT)) {
mcimadamore@1215 191 return; //skip diags for Object constructor resolution
mcimadamore@1215 192 } else if (site == syms.predefClass.type &&
mcimadamore@1215 193 !verboseResolutionMode.contains(VerboseResolutionMode.PREDEF)) {
mcimadamore@1215 194 return; //skip spurious diags for predef symbols (i.e. operators)
mcimadamore@1215 195 } else if (currentResolutionContext.internalResolution &&
mcimadamore@1215 196 !verboseResolutionMode.contains(VerboseResolutionMode.INTERNAL)) {
mcimadamore@1215 197 return;
mcimadamore@1215 198 }
mcimadamore@1215 199
mcimadamore@1215 200 int pos = 0;
mcimadamore@1215 201 int mostSpecificPos = -1;
mcimadamore@1215 202 ListBuffer<JCDiagnostic> subDiags = ListBuffer.lb();
mcimadamore@1215 203 for (Candidate c : currentResolutionContext.candidates) {
mcimadamore@1215 204 if (currentResolutionContext.step != c.step ||
mcimadamore@1215 205 (c.isApplicable() && !verboseResolutionMode.contains(VerboseResolutionMode.APPLICABLE)) ||
mcimadamore@1215 206 (!c.isApplicable() && !verboseResolutionMode.contains(VerboseResolutionMode.INAPPLICABLE))) {
mcimadamore@1215 207 continue;
mcimadamore@1215 208 } else {
mcimadamore@1215 209 subDiags.append(c.isApplicable() ?
mcimadamore@1215 210 getVerboseApplicableCandidateDiag(pos, c.sym, c.mtype) :
mcimadamore@1215 211 getVerboseInapplicableCandidateDiag(pos, c.sym, c.details));
mcimadamore@1215 212 if (c.sym == bestSoFar)
mcimadamore@1215 213 mostSpecificPos = pos;
mcimadamore@1215 214 pos++;
mcimadamore@1215 215 }
mcimadamore@1215 216 }
mcimadamore@1215 217 String key = success ? "verbose.resolve.multi" : "verbose.resolve.multi.1";
mcimadamore@1215 218 JCDiagnostic main = diags.note(log.currentSource(), dpos, key, name,
mcimadamore@1215 219 site.tsym, mostSpecificPos, currentResolutionContext.step,
mcimadamore@1215 220 methodArguments(argtypes), methodArguments(typeargtypes));
mcimadamore@1215 221 JCDiagnostic d = new JCDiagnostic.MultilineDiagnostic(main, subDiags.toList());
mcimadamore@1215 222 log.report(d);
duke@1 223 }
duke@1 224
mcimadamore@1215 225 JCDiagnostic getVerboseApplicableCandidateDiag(int pos, Symbol sym, Type inst) {
mcimadamore@1215 226 JCDiagnostic subDiag = null;
mcimadamore@1215 227 if (inst.getReturnType().tag == FORALL) {
mcimadamore@1215 228 Type diagType = types.createMethodTypeWithReturn(inst.asMethodType(),
mcimadamore@1215 229 ((ForAll)inst.getReturnType()).qtype);
mcimadamore@1215 230 subDiag = diags.fragment("partial.inst.sig", diagType);
mcimadamore@1215 231 } else if (sym.type.tag == FORALL) {
mcimadamore@1215 232 subDiag = diags.fragment("full.inst.sig", inst.asMethodType());
mcimadamore@1215 233 }
duke@1 234
mcimadamore@1215 235 String key = subDiag == null ?
mcimadamore@1215 236 "applicable.method.found" :
mcimadamore@1215 237 "applicable.method.found.1";
duke@1 238
mcimadamore@1215 239 return diags.fragment(key, pos, sym, subDiag);
duke@1 240 }
duke@1 241
mcimadamore@1215 242 JCDiagnostic getVerboseInapplicableCandidateDiag(int pos, Symbol sym, JCDiagnostic subDiag) {
mcimadamore@1215 243 return diags.fragment("not.applicable.method.found", pos, sym, subDiag);
mcimadamore@1215 244 }
mcimadamore@1215 245 // </editor-fold>
duke@1 246
duke@1 247 /* ************************************************************************
duke@1 248 * Identifier resolution
duke@1 249 *************************************************************************/
duke@1 250
duke@1 251 /** An environment is "static" if its static level is greater than
duke@1 252 * the one of its outer environment
duke@1 253 */
duke@1 254 static boolean isStatic(Env<AttrContext> env) {
duke@1 255 return env.info.staticLevel > env.outer.info.staticLevel;
duke@1 256 }
duke@1 257
duke@1 258 /** An environment is an "initializer" if it is a constructor or
duke@1 259 * an instance initializer.
duke@1 260 */
duke@1 261 static boolean isInitializer(Env<AttrContext> env) {
duke@1 262 Symbol owner = env.info.scope.owner;
duke@1 263 return owner.isConstructor() ||
duke@1 264 owner.owner.kind == TYP &&
duke@1 265 (owner.kind == VAR ||
duke@1 266 owner.kind == MTH && (owner.flags() & BLOCK) != 0) &&
duke@1 267 (owner.flags() & STATIC) == 0;
duke@1 268 }
duke@1 269
duke@1 270 /** Is class accessible in given evironment?
duke@1 271 * @param env The current environment.
duke@1 272 * @param c The class whose accessibility is checked.
duke@1 273 */
duke@1 274 public boolean isAccessible(Env<AttrContext> env, TypeSymbol c) {
mcimadamore@741 275 return isAccessible(env, c, false);
mcimadamore@741 276 }
mcimadamore@741 277
mcimadamore@741 278 public boolean isAccessible(Env<AttrContext> env, TypeSymbol c, boolean checkInner) {
mcimadamore@741 279 boolean isAccessible = false;
duke@1 280 switch ((short)(c.flags() & AccessFlags)) {
mcimadamore@741 281 case PRIVATE:
mcimadamore@741 282 isAccessible =
mcimadamore@741 283 env.enclClass.sym.outermostClass() ==
mcimadamore@741 284 c.owner.outermostClass();
mcimadamore@741 285 break;
mcimadamore@741 286 case 0:
mcimadamore@741 287 isAccessible =
mcimadamore@741 288 env.toplevel.packge == c.owner // fast special case
mcimadamore@741 289 ||
mcimadamore@741 290 env.toplevel.packge == c.packge()
mcimadamore@741 291 ||
mcimadamore@741 292 // Hack: this case is added since synthesized default constructors
mcimadamore@741 293 // of anonymous classes should be allowed to access
mcimadamore@741 294 // classes which would be inaccessible otherwise.
mcimadamore@741 295 env.enclMethod != null &&
mcimadamore@741 296 (env.enclMethod.mods.flags & ANONCONSTR) != 0;
mcimadamore@741 297 break;
mcimadamore@741 298 default: // error recovery
mcimadamore@741 299 case PUBLIC:
mcimadamore@741 300 isAccessible = true;
mcimadamore@741 301 break;
mcimadamore@741 302 case PROTECTED:
mcimadamore@741 303 isAccessible =
mcimadamore@741 304 env.toplevel.packge == c.owner // fast special case
mcimadamore@741 305 ||
mcimadamore@741 306 env.toplevel.packge == c.packge()
mcimadamore@741 307 ||
mcimadamore@741 308 isInnerSubClass(env.enclClass.sym, c.owner);
mcimadamore@741 309 break;
duke@1 310 }
mcimadamore@741 311 return (checkInner == false || c.type.getEnclosingType() == Type.noType) ?
mcimadamore@741 312 isAccessible :
jjg@789 313 isAccessible && isAccessible(env, c.type.getEnclosingType(), checkInner);
duke@1 314 }
duke@1 315 //where
duke@1 316 /** Is given class a subclass of given base class, or an inner class
duke@1 317 * of a subclass?
duke@1 318 * Return null if no such class exists.
duke@1 319 * @param c The class which is the subclass or is contained in it.
duke@1 320 * @param base The base class
duke@1 321 */
duke@1 322 private boolean isInnerSubClass(ClassSymbol c, Symbol base) {
duke@1 323 while (c != null && !c.isSubClass(base, types)) {
duke@1 324 c = c.owner.enclClass();
duke@1 325 }
duke@1 326 return c != null;
duke@1 327 }
duke@1 328
duke@1 329 boolean isAccessible(Env<AttrContext> env, Type t) {
mcimadamore@741 330 return isAccessible(env, t, false);
mcimadamore@741 331 }
mcimadamore@741 332
mcimadamore@741 333 boolean isAccessible(Env<AttrContext> env, Type t, boolean checkInner) {
duke@1 334 return (t.tag == ARRAY)
duke@1 335 ? isAccessible(env, types.elemtype(t))
mcimadamore@741 336 : isAccessible(env, t.tsym, checkInner);
duke@1 337 }
duke@1 338
duke@1 339 /** Is symbol accessible as a member of given type in given evironment?
duke@1 340 * @param env The current environment.
duke@1 341 * @param site The type of which the tested symbol is regarded
duke@1 342 * as a member.
duke@1 343 * @param sym The symbol.
duke@1 344 */
duke@1 345 public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym) {
mcimadamore@741 346 return isAccessible(env, site, sym, false);
mcimadamore@741 347 }
mcimadamore@741 348 public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym, boolean checkInner) {
duke@1 349 if (sym.name == names.init && sym.owner != site.tsym) return false;
duke@1 350 switch ((short)(sym.flags() & AccessFlags)) {
duke@1 351 case PRIVATE:
duke@1 352 return
duke@1 353 (env.enclClass.sym == sym.owner // fast special case
duke@1 354 ||
duke@1 355 env.enclClass.sym.outermostClass() ==
duke@1 356 sym.owner.outermostClass())
duke@1 357 &&
duke@1 358 sym.isInheritedIn(site.tsym, types);
duke@1 359 case 0:
duke@1 360 return
duke@1 361 (env.toplevel.packge == sym.owner.owner // fast special case
duke@1 362 ||
duke@1 363 env.toplevel.packge == sym.packge())
duke@1 364 &&
mcimadamore@741 365 isAccessible(env, site, checkInner)
duke@1 366 &&
mcimadamore@254 367 sym.isInheritedIn(site.tsym, types)
mcimadamore@254 368 &&
mcimadamore@254 369 notOverriddenIn(site, sym);
duke@1 370 case PROTECTED:
duke@1 371 return
duke@1 372 (env.toplevel.packge == sym.owner.owner // fast special case
duke@1 373 ||
duke@1 374 env.toplevel.packge == sym.packge()
duke@1 375 ||
duke@1 376 isProtectedAccessible(sym, env.enclClass.sym, site)
duke@1 377 ||
duke@1 378 // OK to select instance method or field from 'super' or type name
duke@1 379 // (but type names should be disallowed elsewhere!)
duke@1 380 env.info.selectSuper && (sym.flags() & STATIC) == 0 && sym.kind != TYP)
duke@1 381 &&
mcimadamore@741 382 isAccessible(env, site, checkInner)
duke@1 383 &&
mcimadamore@254 384 notOverriddenIn(site, sym);
duke@1 385 default: // this case includes erroneous combinations as well
mcimadamore@741 386 return isAccessible(env, site, checkInner) && notOverriddenIn(site, sym);
mcimadamore@254 387 }
mcimadamore@254 388 }
mcimadamore@254 389 //where
mcimadamore@254 390 /* `sym' is accessible only if not overridden by
mcimadamore@254 391 * another symbol which is a member of `site'
mcimadamore@254 392 * (because, if it is overridden, `sym' is not strictly
mcimadamore@674 393 * speaking a member of `site'). A polymorphic signature method
mcimadamore@674 394 * cannot be overridden (e.g. MH.invokeExact(Object[])).
mcimadamore@254 395 */
mcimadamore@254 396 private boolean notOverriddenIn(Type site, Symbol sym) {
mcimadamore@254 397 if (sym.kind != MTH || sym.isConstructor() || sym.isStatic())
mcimadamore@254 398 return true;
mcimadamore@254 399 else {
mcimadamore@254 400 Symbol s2 = ((MethodSymbol)sym).implementation(site.tsym, types, true);
mcimadamore@844 401 return (s2 == null || s2 == sym || sym.owner == s2.owner ||
mcimadamore@674 402 s2.isPolymorphicSignatureGeneric() ||
mcimadamore@325 403 !types.isSubSignature(types.memberType(site, s2), types.memberType(site, sym)));
duke@1 404 }
duke@1 405 }
duke@1 406 //where
duke@1 407 /** Is given protected symbol accessible if it is selected from given site
duke@1 408 * and the selection takes place in given class?
duke@1 409 * @param sym The symbol with protected access
duke@1 410 * @param c The class where the access takes place
duke@1 411 * @site The type of the qualifier
duke@1 412 */
duke@1 413 private
duke@1 414 boolean isProtectedAccessible(Symbol sym, ClassSymbol c, Type site) {
duke@1 415 while (c != null &&
duke@1 416 !(c.isSubClass(sym.owner, types) &&
duke@1 417 (c.flags() & INTERFACE) == 0 &&
duke@1 418 // In JLS 2e 6.6.2.1, the subclass restriction applies
duke@1 419 // only to instance fields and methods -- types are excluded
duke@1 420 // regardless of whether they are declared 'static' or not.
duke@1 421 ((sym.flags() & STATIC) != 0 || sym.kind == TYP || site.tsym.isSubClass(c, types))))
duke@1 422 c = c.owner.enclClass();
duke@1 423 return c != null;
duke@1 424 }
duke@1 425
duke@1 426 /** Try to instantiate the type of a method so that it fits
duke@1 427 * given type arguments and argument types. If succesful, return
duke@1 428 * the method's instantiated type, else return null.
duke@1 429 * The instantiation will take into account an additional leading
duke@1 430 * formal parameter if the method is an instance method seen as a member
duke@1 431 * of un underdetermined site In this case, we treat site as an additional
duke@1 432 * parameter and the parameters of the class containing the method as
duke@1 433 * additional type variables that get instantiated.
duke@1 434 *
duke@1 435 * @param env The current environment
duke@1 436 * @param site The type of which the method is a member.
duke@1 437 * @param m The method symbol.
duke@1 438 * @param argtypes The invocation's given value arguments.
duke@1 439 * @param typeargtypes The invocation's given type arguments.
duke@1 440 * @param allowBoxing Allow boxing conversions of arguments.
duke@1 441 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 442 */
duke@1 443 Type rawInstantiate(Env<AttrContext> env,
duke@1 444 Type site,
duke@1 445 Symbol m,
duke@1 446 List<Type> argtypes,
duke@1 447 List<Type> typeargtypes,
duke@1 448 boolean allowBoxing,
duke@1 449 boolean useVarargs,
duke@1 450 Warner warn)
mcimadamore@299 451 throws Infer.InferenceException {
mcimadamore@820 452 boolean polymorphicSignature = m.isPolymorphicSignatureGeneric() && allowMethodHandles;
mcimadamore@689 453 if (useVarargs && (m.flags() & VARARGS) == 0)
mcimadamore@845 454 throw inapplicableMethodException.setMessage();
duke@1 455 Type mt = types.memberType(site, m);
duke@1 456
duke@1 457 // tvars is the list of formal type variables for which type arguments
duke@1 458 // need to inferred.
mcimadamore@950 459 List<Type> tvars = null;
mcimadamore@950 460 if (env.info.tvars != null) {
mcimadamore@950 461 tvars = types.newInstances(env.info.tvars);
mcimadamore@950 462 mt = types.subst(mt, env.info.tvars, tvars);
mcimadamore@950 463 }
duke@1 464 if (typeargtypes == null) typeargtypes = List.nil();
mcimadamore@820 465 if (mt.tag != FORALL && typeargtypes.nonEmpty()) {
duke@1 466 // This is not a polymorphic method, but typeargs are supplied
jjh@972 467 // which is fine, see JLS 15.12.2.1
duke@1 468 } else if (mt.tag == FORALL && typeargtypes.nonEmpty()) {
duke@1 469 ForAll pmt = (ForAll) mt;
duke@1 470 if (typeargtypes.length() != pmt.tvars.length())
mcimadamore@689 471 throw inapplicableMethodException.setMessage("arg.length.mismatch"); // not enough args
duke@1 472 // Check type arguments are within bounds
duke@1 473 List<Type> formals = pmt.tvars;
duke@1 474 List<Type> actuals = typeargtypes;
duke@1 475 while (formals.nonEmpty() && actuals.nonEmpty()) {
duke@1 476 List<Type> bounds = types.subst(types.getBounds((TypeVar)formals.head),
duke@1 477 pmt.tvars, typeargtypes);
duke@1 478 for (; bounds.nonEmpty(); bounds = bounds.tail)
duke@1 479 if (!types.isSubtypeUnchecked(actuals.head, bounds.head, warn))
mcimadamore@689 480 throw inapplicableMethodException.setMessage("explicit.param.do.not.conform.to.bounds",actuals.head, bounds);
duke@1 481 formals = formals.tail;
duke@1 482 actuals = actuals.tail;
duke@1 483 }
duke@1 484 mt = types.subst(pmt.qtype, pmt.tvars, typeargtypes);
duke@1 485 } else if (mt.tag == FORALL) {
duke@1 486 ForAll pmt = (ForAll) mt;
duke@1 487 List<Type> tvars1 = types.newInstances(pmt.tvars);
duke@1 488 tvars = tvars.appendList(tvars1);
duke@1 489 mt = types.subst(pmt.qtype, pmt.tvars, tvars1);
duke@1 490 }
duke@1 491
duke@1 492 // find out whether we need to go the slow route via infer
mcimadamore@674 493 boolean instNeeded = tvars.tail != null || /*inlined: tvars.nonEmpty()*/
mcimadamore@674 494 polymorphicSignature;
duke@1 495 for (List<Type> l = argtypes;
duke@1 496 l.tail != null/*inlined: l.nonEmpty()*/ && !instNeeded;
duke@1 497 l = l.tail) {
duke@1 498 if (l.head.tag == FORALL) instNeeded = true;
duke@1 499 }
duke@1 500
duke@1 501 if (instNeeded)
mcimadamore@674 502 return polymorphicSignature ?
mcimadamore@820 503 infer.instantiatePolymorphicSignatureInstance(env, site, m.name, (MethodSymbol)m, argtypes) :
mcimadamore@674 504 infer.instantiateMethod(env,
mcimadamore@547 505 tvars,
duke@1 506 (MethodType)mt,
mcimadamore@580 507 m,
duke@1 508 argtypes,
duke@1 509 allowBoxing,
duke@1 510 useVarargs,
duke@1 511 warn);
mcimadamore@689 512
mcimadamore@845 513 checkRawArgumentsAcceptable(env, argtypes, mt.getParameterTypes(),
mcimadamore@689 514 allowBoxing, useVarargs, warn);
mcimadamore@689 515 return mt;
duke@1 516 }
duke@1 517
duke@1 518 /** Same but returns null instead throwing a NoInstanceException
duke@1 519 */
duke@1 520 Type instantiate(Env<AttrContext> env,
duke@1 521 Type site,
duke@1 522 Symbol m,
duke@1 523 List<Type> argtypes,
duke@1 524 List<Type> typeargtypes,
duke@1 525 boolean allowBoxing,
duke@1 526 boolean useVarargs,
duke@1 527 Warner warn) {
duke@1 528 try {
duke@1 529 return rawInstantiate(env, site, m, argtypes, typeargtypes,
duke@1 530 allowBoxing, useVarargs, warn);
mcimadamore@689 531 } catch (InapplicableMethodException ex) {
duke@1 532 return null;
duke@1 533 }
duke@1 534 }
duke@1 535
duke@1 536 /** Check if a parameter list accepts a list of args.
duke@1 537 */
mcimadamore@845 538 boolean argumentsAcceptable(Env<AttrContext> env,
mcimadamore@845 539 List<Type> argtypes,
duke@1 540 List<Type> formals,
duke@1 541 boolean allowBoxing,
duke@1 542 boolean useVarargs,
duke@1 543 Warner warn) {
mcimadamore@689 544 try {
mcimadamore@845 545 checkRawArgumentsAcceptable(env, argtypes, formals, allowBoxing, useVarargs, warn);
mcimadamore@689 546 return true;
mcimadamore@689 547 } catch (InapplicableMethodException ex) {
mcimadamore@689 548 return false;
mcimadamore@689 549 }
mcimadamore@689 550 }
mcimadamore@1186 551 /**
mcimadamore@1186 552 * A check handler is used by the main method applicability routine in order
mcimadamore@1186 553 * to handle specific method applicability failures. It is assumed that a class
mcimadamore@1186 554 * implementing this interface should throw exceptions that are a subtype of
mcimadamore@1186 555 * InapplicableMethodException (see below). Such exception will terminate the
mcimadamore@1186 556 * method applicability check and propagate important info outwards (for the
mcimadamore@1186 557 * purpose of generating better diagnostics).
mcimadamore@1186 558 */
mcimadamore@1186 559 interface MethodCheckHandler {
mcimadamore@1186 560 /* The number of actuals and formals differ */
mcimadamore@1186 561 InapplicableMethodException arityMismatch();
mcimadamore@1186 562 /* An actual argument type does not conform to the corresponding formal type */
mcimadamore@1186 563 InapplicableMethodException argumentMismatch(boolean varargs, Type found, Type expected);
mcimadamore@1186 564 /* The element type of a varargs is not accessible in the current context */
mcimadamore@1186 565 InapplicableMethodException inaccessibleVarargs(Symbol location, Type expected);
mcimadamore@1186 566 }
mcimadamore@1186 567
mcimadamore@1186 568 /**
mcimadamore@1186 569 * Basic method check handler used within Resolve - all methods end up
mcimadamore@1186 570 * throwing InapplicableMethodException; a diagnostic fragment that describes
mcimadamore@1186 571 * the cause as to why the method is not applicable is set on the exception
mcimadamore@1186 572 * before it is thrown.
mcimadamore@1186 573 */
mcimadamore@1186 574 MethodCheckHandler resolveHandler = new MethodCheckHandler() {
mcimadamore@1186 575 public InapplicableMethodException arityMismatch() {
mcimadamore@1186 576 return inapplicableMethodException.setMessage("arg.length.mismatch");
mcimadamore@1186 577 }
mcimadamore@1186 578 public InapplicableMethodException argumentMismatch(boolean varargs, Type found, Type expected) {
mcimadamore@1186 579 String key = varargs ?
mcimadamore@1186 580 "varargs.argument.mismatch" :
mcimadamore@1186 581 "no.conforming.assignment.exists";
mcimadamore@1186 582 return inapplicableMethodException.setMessage(key,
mcimadamore@1186 583 found, expected);
mcimadamore@1186 584 }
mcimadamore@1186 585 public InapplicableMethodException inaccessibleVarargs(Symbol location, Type expected) {
mcimadamore@1186 586 return inapplicableMethodException.setMessage("inaccessible.varargs.type",
mcimadamore@1186 587 expected, Kinds.kindName(location), location);
mcimadamore@1186 588 }
mcimadamore@1186 589 };
mcimadamore@1186 590
mcimadamore@845 591 void checkRawArgumentsAcceptable(Env<AttrContext> env,
mcimadamore@845 592 List<Type> argtypes,
mcimadamore@689 593 List<Type> formals,
mcimadamore@689 594 boolean allowBoxing,
mcimadamore@689 595 boolean useVarargs,
mcimadamore@689 596 Warner warn) {
mcimadamore@1186 597 checkRawArgumentsAcceptable(env, List.<Type>nil(), argtypes, formals,
mcimadamore@1186 598 allowBoxing, useVarargs, warn, resolveHandler);
mcimadamore@1186 599 }
mcimadamore@1186 600
mcimadamore@1186 601 /**
mcimadamore@1186 602 * Main method applicability routine. Given a list of actual types A,
mcimadamore@1186 603 * a list of formal types F, determines whether the types in A are
mcimadamore@1186 604 * compatible (by method invocation conversion) with the types in F.
mcimadamore@1186 605 *
mcimadamore@1186 606 * Since this routine is shared between overload resolution and method
mcimadamore@1186 607 * type-inference, it is crucial that actual types are converted to the
mcimadamore@1186 608 * corresponding 'undet' form (i.e. where inference variables are replaced
mcimadamore@1186 609 * with undetvars) so that constraints can be propagated and collected.
mcimadamore@1186 610 *
mcimadamore@1186 611 * Moreover, if one or more types in A is a poly type, this routine calls
mcimadamore@1186 612 * Infer.instantiateArg in order to complete the poly type (this might involve
mcimadamore@1186 613 * deferred attribution).
mcimadamore@1186 614 *
mcimadamore@1186 615 * A method check handler (see above) is used in order to report errors.
mcimadamore@1186 616 */
mcimadamore@1186 617 List<Type> checkRawArgumentsAcceptable(Env<AttrContext> env,
mcimadamore@1186 618 List<Type> undetvars,
mcimadamore@1186 619 List<Type> argtypes,
mcimadamore@1186 620 List<Type> formals,
mcimadamore@1186 621 boolean allowBoxing,
mcimadamore@1186 622 boolean useVarargs,
mcimadamore@1186 623 Warner warn,
mcimadamore@1186 624 MethodCheckHandler handler) {
duke@1 625 Type varargsFormal = useVarargs ? formals.last() : null;
mcimadamore@1186 626 ListBuffer<Type> checkedArgs = ListBuffer.lb();
mcimadamore@1186 627
mcimadamore@689 628 if (varargsFormal == null &&
mcimadamore@689 629 argtypes.size() != formals.size()) {
mcimadamore@1186 630 throw handler.arityMismatch(); // not enough args
mcimadamore@689 631 }
mcimadamore@689 632
duke@1 633 while (argtypes.nonEmpty() && formals.head != varargsFormal) {
mcimadamore@1238 634 ResultInfo resultInfo = methodCheckResult(formals.head, allowBoxing, false, undetvars, handler, warn);
mcimadamore@1238 635 checkedArgs.append(resultInfo.check(env.tree.pos(), argtypes.head));
duke@1 636 argtypes = argtypes.tail;
duke@1 637 formals = formals.tail;
duke@1 638 }
mcimadamore@689 639
mcimadamore@1186 640 if (formals.head != varargsFormal) {
mcimadamore@1186 641 throw handler.arityMismatch(); // not enough args
mcimadamore@1186 642 }
mcimadamore@689 643
mcimadamore@689 644 if (useVarargs) {
mcimadamore@1186 645 //note: if applicability check is triggered by most specific test,
mcimadamore@1186 646 //the last argument of a varargs is _not_ an array type (see JLS 15.12.2.5)
mcimadamore@1006 647 Type elt = types.elemtype(varargsFormal);
mcimadamore@689 648 while (argtypes.nonEmpty()) {
mcimadamore@1238 649 ResultInfo resultInfo = methodCheckResult(elt, allowBoxing, true, undetvars, handler, warn);
mcimadamore@1238 650 checkedArgs.append(resultInfo.check(env.tree.pos(), argtypes.head));
mcimadamore@689 651 argtypes = argtypes.tail;
mcimadamore@689 652 }
mcimadamore@845 653 //check varargs element type accessibility
mcimadamore@1186 654 if (undetvars.isEmpty() && !isAccessible(env, elt)) {
mcimadamore@845 655 Symbol location = env.enclClass.sym;
mcimadamore@1186 656 throw handler.inaccessibleVarargs(location, elt);
mcimadamore@845 657 }
duke@1 658 }
mcimadamore@1186 659 return checkedArgs.toList();
duke@1 660 }
mcimadamore@689 661
mcimadamore@1238 662 /**
mcimadamore@1238 663 * Check context to be used during method applicability checks. A method check
mcimadamore@1238 664 * context might contain inference variables.
mcimadamore@1238 665 */
mcimadamore@1238 666 abstract class MethodCheckContext implements CheckContext {
mcimadamore@689 667
mcimadamore@1238 668 MethodCheckHandler handler;
mcimadamore@1238 669 boolean useVarargs;
mcimadamore@1238 670 List<Type> undetvars;
mcimadamore@1238 671 Warner rsWarner;
mcimadamore@1238 672
mcimadamore@1238 673 public MethodCheckContext(MethodCheckHandler handler, boolean useVarargs, List<Type> undetvars, Warner rsWarner) {
mcimadamore@1238 674 this.handler = handler;
mcimadamore@1238 675 this.useVarargs = useVarargs;
mcimadamore@1238 676 this.undetvars = undetvars;
mcimadamore@1238 677 this.rsWarner = rsWarner;
mcimadamore@1238 678 }
mcimadamore@1238 679
mcimadamore@1238 680 public void report(DiagnosticPosition pos, Type found, Type req, JCDiagnostic details) {
mcimadamore@1238 681 throw handler.argumentMismatch(useVarargs, found, req);
mcimadamore@1238 682 }
mcimadamore@1238 683
mcimadamore@1238 684 public Type rawInstantiatePoly(ForAll found, Type req, Warner warn) {
mcimadamore@1238 685 throw new AssertionError("ForAll in argument position");
mcimadamore@1238 686 }
mcimadamore@1238 687
mcimadamore@1238 688 public Warner checkWarner(DiagnosticPosition pos, Type found, Type req) {
mcimadamore@1238 689 return rsWarner;
mcimadamore@1238 690 }
mcimadamore@1238 691 }
mcimadamore@1238 692
mcimadamore@1238 693 /**
mcimadamore@1238 694 * Subclass of method check context class that implements strict method conversion.
mcimadamore@1238 695 * Strict method conversion checks compatibility between types using subtyping tests.
mcimadamore@1238 696 */
mcimadamore@1238 697 class StrictMethodContext extends MethodCheckContext {
mcimadamore@1238 698
mcimadamore@1238 699 public StrictMethodContext(MethodCheckHandler handler, boolean useVarargs, List<Type> undetvars, Warner rsWarner) {
mcimadamore@1238 700 super(handler, useVarargs, undetvars, rsWarner);
mcimadamore@1238 701 }
mcimadamore@1238 702
mcimadamore@1238 703 public boolean compatible(Type found, Type req, Warner warn) {
mcimadamore@1238 704 return types.isSubtypeUnchecked(found, infer.asUndetType(req, undetvars), warn);
mcimadamore@1238 705 }
mcimadamore@1238 706 }
mcimadamore@1238 707
mcimadamore@1238 708 /**
mcimadamore@1238 709 * Subclass of method check context class that implements loose method conversion.
mcimadamore@1238 710 * Loose method conversion checks compatibility between types using method conversion tests.
mcimadamore@1238 711 */
mcimadamore@1238 712 class LooseMethodContext extends MethodCheckContext {
mcimadamore@1238 713
mcimadamore@1238 714 public LooseMethodContext(MethodCheckHandler handler, boolean useVarargs, List<Type> undetvars, Warner rsWarner) {
mcimadamore@1238 715 super(handler, useVarargs, undetvars, rsWarner);
mcimadamore@1238 716 }
mcimadamore@1238 717
mcimadamore@1238 718 public boolean compatible(Type found, Type req, Warner warn) {
mcimadamore@1238 719 return types.isConvertible(found, infer.asUndetType(req, undetvars), warn);
mcimadamore@1238 720 }
mcimadamore@1238 721 }
mcimadamore@1238 722
mcimadamore@1238 723 /**
mcimadamore@1238 724 * Create a method check context to be used during method applicability check
mcimadamore@1238 725 */
mcimadamore@1238 726 ResultInfo methodCheckResult(Type to, boolean allowBoxing, boolean useVarargs,
mcimadamore@1238 727 List<Type> undetvars, MethodCheckHandler methodHandler, Warner rsWarner) {
mcimadamore@1238 728 MethodCheckContext checkContext = allowBoxing ?
mcimadamore@1238 729 new LooseMethodContext(methodHandler, useVarargs, undetvars, rsWarner) :
mcimadamore@1238 730 new StrictMethodContext(methodHandler, useVarargs, undetvars, rsWarner);
mcimadamore@1238 731 return attr.new ResultInfo(VAL, to, checkContext) {
mcimadamore@1238 732 @Override
mcimadamore@1238 733 protected Type check(DiagnosticPosition pos, Type found) {
mcimadamore@1238 734 return super.check(pos, chk.checkNonVoid(pos, types.capture(types.upperBound(found))));
mcimadamore@689 735 }
mcimadamore@1238 736 };
mcimadamore@1238 737 }
mcimadamore@689 738
mcimadamore@1238 739 public static class InapplicableMethodException extends RuntimeException {
mcimadamore@1238 740 private static final long serialVersionUID = 0;
mcimadamore@1238 741
mcimadamore@1238 742 JCDiagnostic diagnostic;
mcimadamore@1238 743 JCDiagnostic.Factory diags;
mcimadamore@1238 744
mcimadamore@1238 745 InapplicableMethodException(JCDiagnostic.Factory diags) {
mcimadamore@1238 746 this.diagnostic = null;
mcimadamore@1238 747 this.diags = diags;
mcimadamore@689 748 }
mcimadamore@1238 749 InapplicableMethodException setMessage() {
mcimadamore@1238 750 this.diagnostic = null;
mcimadamore@1238 751 return this;
mcimadamore@1238 752 }
mcimadamore@1238 753 InapplicableMethodException setMessage(String key) {
mcimadamore@1238 754 this.diagnostic = key != null ? diags.fragment(key) : null;
mcimadamore@1238 755 return this;
mcimadamore@1238 756 }
mcimadamore@1238 757 InapplicableMethodException setMessage(String key, Object... args) {
mcimadamore@1238 758 this.diagnostic = key != null ? diags.fragment(key, args) : null;
mcimadamore@1238 759 return this;
mcimadamore@1238 760 }
mcimadamore@1238 761 InapplicableMethodException setMessage(JCDiagnostic diag) {
mcimadamore@1238 762 this.diagnostic = diag;
mcimadamore@1238 763 return this;
mcimadamore@1238 764 }
mcimadamore@1238 765
mcimadamore@1238 766 public JCDiagnostic getDiagnostic() {
mcimadamore@1238 767 return diagnostic;
mcimadamore@1238 768 }
mcimadamore@1238 769 }
mcimadamore@1238 770 private final InapplicableMethodException inapplicableMethodException;
duke@1 771
duke@1 772 /* ***************************************************************************
duke@1 773 * Symbol lookup
duke@1 774 * the following naming conventions for arguments are used
duke@1 775 *
duke@1 776 * env is the environment where the symbol was mentioned
duke@1 777 * site is the type of which the symbol is a member
duke@1 778 * name is the symbol's name
duke@1 779 * if no arguments are given
duke@1 780 * argtypes are the value arguments, if we search for a method
duke@1 781 *
duke@1 782 * If no symbol was found, a ResolveError detailing the problem is returned.
duke@1 783 ****************************************************************************/
duke@1 784
duke@1 785 /** Find field. Synthetic fields are always skipped.
duke@1 786 * @param env The current environment.
duke@1 787 * @param site The original type from where the selection takes place.
duke@1 788 * @param name The name of the field.
duke@1 789 * @param c The class to search for the field. This is always
duke@1 790 * a superclass or implemented interface of site's class.
duke@1 791 */
duke@1 792 Symbol findField(Env<AttrContext> env,
duke@1 793 Type site,
duke@1 794 Name name,
duke@1 795 TypeSymbol c) {
mcimadamore@19 796 while (c.type.tag == TYPEVAR)
mcimadamore@19 797 c = c.type.getUpperBound().tsym;
duke@1 798 Symbol bestSoFar = varNotFound;
duke@1 799 Symbol sym;
duke@1 800 Scope.Entry e = c.members().lookup(name);
duke@1 801 while (e.scope != null) {
duke@1 802 if (e.sym.kind == VAR && (e.sym.flags_field & SYNTHETIC) == 0) {
duke@1 803 return isAccessible(env, site, e.sym)
duke@1 804 ? e.sym : new AccessError(env, site, e.sym);
duke@1 805 }
duke@1 806 e = e.next();
duke@1 807 }
duke@1 808 Type st = types.supertype(c.type);
mcimadamore@19 809 if (st != null && (st.tag == CLASS || st.tag == TYPEVAR)) {
duke@1 810 sym = findField(env, site, name, st.tsym);
duke@1 811 if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 812 }
duke@1 813 for (List<Type> l = types.interfaces(c.type);
duke@1 814 bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
duke@1 815 l = l.tail) {
duke@1 816 sym = findField(env, site, name, l.head.tsym);
duke@1 817 if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS &&
duke@1 818 sym.owner != bestSoFar.owner)
duke@1 819 bestSoFar = new AmbiguityError(bestSoFar, sym);
duke@1 820 else if (sym.kind < bestSoFar.kind)
duke@1 821 bestSoFar = sym;
duke@1 822 }
duke@1 823 return bestSoFar;
duke@1 824 }
duke@1 825
duke@1 826 /** Resolve a field identifier, throw a fatal error if not found.
duke@1 827 * @param pos The position to use for error reporting.
duke@1 828 * @param env The environment current at the method invocation.
duke@1 829 * @param site The type of the qualifying expression, in which
duke@1 830 * identifier is searched.
duke@1 831 * @param name The identifier's name.
duke@1 832 */
duke@1 833 public VarSymbol resolveInternalField(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 834 Type site, Name name) {
duke@1 835 Symbol sym = findField(env, site, name, site.tsym);
duke@1 836 if (sym.kind == VAR) return (VarSymbol)sym;
duke@1 837 else throw new FatalError(
mcimadamore@89 838 diags.fragment("fatal.err.cant.locate.field",
duke@1 839 name));
duke@1 840 }
duke@1 841
duke@1 842 /** Find unqualified variable or field with given name.
duke@1 843 * Synthetic fields always skipped.
duke@1 844 * @param env The current environment.
duke@1 845 * @param name The name of the variable or field.
duke@1 846 */
duke@1 847 Symbol findVar(Env<AttrContext> env, Name name) {
duke@1 848 Symbol bestSoFar = varNotFound;
duke@1 849 Symbol sym;
duke@1 850 Env<AttrContext> env1 = env;
duke@1 851 boolean staticOnly = false;
duke@1 852 while (env1.outer != null) {
duke@1 853 if (isStatic(env1)) staticOnly = true;
duke@1 854 Scope.Entry e = env1.info.scope.lookup(name);
duke@1 855 while (e.scope != null &&
duke@1 856 (e.sym.kind != VAR ||
duke@1 857 (e.sym.flags_field & SYNTHETIC) != 0))
duke@1 858 e = e.next();
duke@1 859 sym = (e.scope != null)
duke@1 860 ? e.sym
duke@1 861 : findField(
duke@1 862 env1, env1.enclClass.sym.type, name, env1.enclClass.sym);
duke@1 863 if (sym.exists()) {
duke@1 864 if (staticOnly &&
duke@1 865 sym.kind == VAR &&
duke@1 866 sym.owner.kind == TYP &&
duke@1 867 (sym.flags() & STATIC) == 0)
duke@1 868 return new StaticError(sym);
duke@1 869 else
duke@1 870 return sym;
duke@1 871 } else if (sym.kind < bestSoFar.kind) {
duke@1 872 bestSoFar = sym;
duke@1 873 }
duke@1 874
duke@1 875 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
duke@1 876 env1 = env1.outer;
duke@1 877 }
duke@1 878
duke@1 879 sym = findField(env, syms.predefClass.type, name, syms.predefClass);
duke@1 880 if (sym.exists())
duke@1 881 return sym;
duke@1 882 if (bestSoFar.exists())
duke@1 883 return bestSoFar;
duke@1 884
duke@1 885 Scope.Entry e = env.toplevel.namedImportScope.lookup(name);
duke@1 886 for (; e.scope != null; e = e.next()) {
duke@1 887 sym = e.sym;
duke@1 888 Type origin = e.getOrigin().owner.type;
duke@1 889 if (sym.kind == VAR) {
duke@1 890 if (e.sym.owner.type != origin)
duke@1 891 sym = sym.clone(e.getOrigin().owner);
duke@1 892 return isAccessible(env, origin, sym)
duke@1 893 ? sym : new AccessError(env, origin, sym);
duke@1 894 }
duke@1 895 }
duke@1 896
duke@1 897 Symbol origin = null;
duke@1 898 e = env.toplevel.starImportScope.lookup(name);
duke@1 899 for (; e.scope != null; e = e.next()) {
duke@1 900 sym = e.sym;
duke@1 901 if (sym.kind != VAR)
duke@1 902 continue;
duke@1 903 // invariant: sym.kind == VAR
duke@1 904 if (bestSoFar.kind < AMBIGUOUS && sym.owner != bestSoFar.owner)
duke@1 905 return new AmbiguityError(bestSoFar, sym);
duke@1 906 else if (bestSoFar.kind >= VAR) {
duke@1 907 origin = e.getOrigin().owner;
duke@1 908 bestSoFar = isAccessible(env, origin.type, sym)
duke@1 909 ? sym : new AccessError(env, origin.type, sym);
duke@1 910 }
duke@1 911 }
duke@1 912 if (bestSoFar.kind == VAR && bestSoFar.owner.type != origin.type)
duke@1 913 return bestSoFar.clone(origin);
duke@1 914 else
duke@1 915 return bestSoFar;
duke@1 916 }
duke@1 917
duke@1 918 Warner noteWarner = new Warner();
duke@1 919
duke@1 920 /** Select the best method for a call site among two choices.
duke@1 921 * @param env The current environment.
duke@1 922 * @param site The original type from where the
duke@1 923 * selection takes place.
duke@1 924 * @param argtypes The invocation's value arguments,
duke@1 925 * @param typeargtypes The invocation's type arguments,
duke@1 926 * @param sym Proposed new best match.
duke@1 927 * @param bestSoFar Previously found best match.
duke@1 928 * @param allowBoxing Allow boxing conversions of arguments.
duke@1 929 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 930 */
mcimadamore@689 931 @SuppressWarnings("fallthrough")
duke@1 932 Symbol selectBest(Env<AttrContext> env,
duke@1 933 Type site,
duke@1 934 List<Type> argtypes,
duke@1 935 List<Type> typeargtypes,
duke@1 936 Symbol sym,
duke@1 937 Symbol bestSoFar,
duke@1 938 boolean allowBoxing,
duke@1 939 boolean useVarargs,
duke@1 940 boolean operator) {
duke@1 941 if (sym.kind == ERR) return bestSoFar;
mcimadamore@171 942 if (!sym.isInheritedIn(site.tsym, types)) return bestSoFar;
jjg@816 943 Assert.check(sym.kind < AMBIGUOUS);
duke@1 944 try {
mcimadamore@1114 945 Type mt = rawInstantiate(env, site, sym, argtypes, typeargtypes,
mcimadamore@689 946 allowBoxing, useVarargs, Warner.noWarnings);
mcimadamore@1215 947 if (!operator)
mcimadamore@1215 948 currentResolutionContext.addApplicableCandidate(sym, mt);
mcimadamore@689 949 } catch (InapplicableMethodException ex) {
mcimadamore@1215 950 if (!operator)
mcimadamore@1215 951 currentResolutionContext.addInapplicableCandidate(sym, ex.getDiagnostic());
duke@1 952 switch (bestSoFar.kind) {
duke@1 953 case ABSENT_MTH:
mcimadamore@1215 954 return wrongMethod;
duke@1 955 case WRONG_MTH:
mcimadamore@1110 956 if (operator) return bestSoFar;
mcimadamore@689 957 case WRONG_MTHS:
mcimadamore@1215 958 return wrongMethods;
duke@1 959 default:
duke@1 960 return bestSoFar;
duke@1 961 }
duke@1 962 }
duke@1 963 if (!isAccessible(env, site, sym)) {
duke@1 964 return (bestSoFar.kind == ABSENT_MTH)
duke@1 965 ? new AccessError(env, site, sym)
duke@1 966 : bestSoFar;
mcimadamore@1215 967 }
duke@1 968 return (bestSoFar.kind > AMBIGUOUS)
duke@1 969 ? sym
duke@1 970 : mostSpecific(sym, bestSoFar, env, site,
duke@1 971 allowBoxing && operator, useVarargs);
duke@1 972 }
duke@1 973
duke@1 974 /* Return the most specific of the two methods for a call,
duke@1 975 * given that both are accessible and applicable.
duke@1 976 * @param m1 A new candidate for most specific.
duke@1 977 * @param m2 The previous most specific candidate.
duke@1 978 * @param env The current environment.
duke@1 979 * @param site The original type from where the selection
duke@1 980 * takes place.
duke@1 981 * @param allowBoxing Allow boxing conversions of arguments.
duke@1 982 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 983 */
duke@1 984 Symbol mostSpecific(Symbol m1,
duke@1 985 Symbol m2,
duke@1 986 Env<AttrContext> env,
mcimadamore@254 987 final Type site,
duke@1 988 boolean allowBoxing,
duke@1 989 boolean useVarargs) {
duke@1 990 switch (m2.kind) {
duke@1 991 case MTH:
duke@1 992 if (m1 == m2) return m1;
mcimadamore@775 993 boolean m1SignatureMoreSpecific = signatureMoreSpecific(env, site, m1, m2, allowBoxing, useVarargs);
mcimadamore@775 994 boolean m2SignatureMoreSpecific = signatureMoreSpecific(env, site, m2, m1, allowBoxing, useVarargs);
duke@1 995 if (m1SignatureMoreSpecific && m2SignatureMoreSpecific) {
mcimadamore@775 996 Type mt1 = types.memberType(site, m1);
mcimadamore@775 997 Type mt2 = types.memberType(site, m2);
duke@1 998 if (!types.overrideEquivalent(mt1, mt2))
mcimadamore@844 999 return ambiguityError(m1, m2);
mcimadamore@844 1000
duke@1 1001 // same signature; select (a) the non-bridge method, or
duke@1 1002 // (b) the one that overrides the other, or (c) the concrete
duke@1 1003 // one, or (d) merge both abstract signatures
mcimadamore@844 1004 if ((m1.flags() & BRIDGE) != (m2.flags() & BRIDGE))
duke@1 1005 return ((m1.flags() & BRIDGE) != 0) ? m2 : m1;
mcimadamore@844 1006
duke@1 1007 // if one overrides or hides the other, use it
duke@1 1008 TypeSymbol m1Owner = (TypeSymbol)m1.owner;
duke@1 1009 TypeSymbol m2Owner = (TypeSymbol)m2.owner;
duke@1 1010 if (types.asSuper(m1Owner.type, m2Owner) != null &&
duke@1 1011 ((m1.owner.flags_field & INTERFACE) == 0 ||
duke@1 1012 (m2.owner.flags_field & INTERFACE) != 0) &&
duke@1 1013 m1.overrides(m2, m1Owner, types, false))
duke@1 1014 return m1;
duke@1 1015 if (types.asSuper(m2Owner.type, m1Owner) != null &&
duke@1 1016 ((m2.owner.flags_field & INTERFACE) == 0 ||
duke@1 1017 (m1.owner.flags_field & INTERFACE) != 0) &&
duke@1 1018 m2.overrides(m1, m2Owner, types, false))
duke@1 1019 return m2;
duke@1 1020 boolean m1Abstract = (m1.flags() & ABSTRACT) != 0;
duke@1 1021 boolean m2Abstract = (m2.flags() & ABSTRACT) != 0;
duke@1 1022 if (m1Abstract && !m2Abstract) return m2;
duke@1 1023 if (m2Abstract && !m1Abstract) return m1;
duke@1 1024 // both abstract or both concrete
duke@1 1025 if (!m1Abstract && !m2Abstract)
mcimadamore@844 1026 return ambiguityError(m1, m2);
mcimadamore@156 1027 // check that both signatures have the same erasure
mcimadamore@156 1028 if (!types.isSameTypes(m1.erasure(types).getParameterTypes(),
mcimadamore@156 1029 m2.erasure(types).getParameterTypes()))
mcimadamore@844 1030 return ambiguityError(m1, m2);
duke@1 1031 // both abstract, neither overridden; merge throws clause and result type
mcimadamore@1059 1032 Type mst = mostSpecificReturnType(mt1, mt2);
mcimadamore@1059 1033 if (mst == null) {
duke@1 1034 // Theoretically, this can't happen, but it is possible
duke@1 1035 // due to error recovery or mixing incompatible class files
mcimadamore@844 1036 return ambiguityError(m1, m2);
duke@1 1037 }
mcimadamore@1059 1038 Symbol mostSpecific = mst == mt1 ? m1 : m2;
dlsmith@880 1039 List<Type> allThrown = chk.intersect(mt1.getThrownTypes(), mt2.getThrownTypes());
dlsmith@880 1040 Type newSig = types.createMethodTypeWithThrown(mostSpecific.type, allThrown);
mcimadamore@254 1041 MethodSymbol result = new MethodSymbol(
mcimadamore@254 1042 mostSpecific.flags(),
mcimadamore@254 1043 mostSpecific.name,
dlsmith@880 1044 newSig,
mcimadamore@254 1045 mostSpecific.owner) {
mcimadamore@254 1046 @Override
mcimadamore@254 1047 public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult) {
mcimadamore@254 1048 if (origin == site.tsym)
mcimadamore@254 1049 return this;
mcimadamore@254 1050 else
mcimadamore@254 1051 return super.implementation(origin, types, checkResult);
mcimadamore@254 1052 }
mcimadamore@254 1053 };
duke@1 1054 return result;
duke@1 1055 }
duke@1 1056 if (m1SignatureMoreSpecific) return m1;
duke@1 1057 if (m2SignatureMoreSpecific) return m2;
mcimadamore@844 1058 return ambiguityError(m1, m2);
duke@1 1059 case AMBIGUOUS:
duke@1 1060 AmbiguityError e = (AmbiguityError)m2;
mcimadamore@302 1061 Symbol err1 = mostSpecific(m1, e.sym, env, site, allowBoxing, useVarargs);
duke@1 1062 Symbol err2 = mostSpecific(m1, e.sym2, env, site, allowBoxing, useVarargs);
duke@1 1063 if (err1 == err2) return err1;
mcimadamore@302 1064 if (err1 == e.sym && err2 == e.sym2) return m2;
duke@1 1065 if (err1 instanceof AmbiguityError &&
duke@1 1066 err2 instanceof AmbiguityError &&
mcimadamore@302 1067 ((AmbiguityError)err1).sym == ((AmbiguityError)err2).sym)
mcimadamore@844 1068 return ambiguityError(m1, m2);
duke@1 1069 else
mcimadamore@844 1070 return ambiguityError(err1, err2);
duke@1 1071 default:
duke@1 1072 throw new AssertionError();
duke@1 1073 }
duke@1 1074 }
mcimadamore@775 1075 //where
mcimadamore@775 1076 private boolean signatureMoreSpecific(Env<AttrContext> env, Type site, Symbol m1, Symbol m2, boolean allowBoxing, boolean useVarargs) {
mcimadamore@795 1077 noteWarner.clear();
mcimadamore@775 1078 Type mtype1 = types.memberType(site, adjustVarargs(m1, m2, useVarargs));
mcimadamore@1006 1079 Type mtype2 = instantiate(env, site, adjustVarargs(m2, m1, useVarargs),
mcimadamore@1006 1080 types.lowerBoundArgtypes(mtype1), null,
mcimadamore@1006 1081 allowBoxing, false, noteWarner);
mcimadamore@1006 1082 return mtype2 != null &&
mcimadamore@795 1083 !noteWarner.hasLint(Lint.LintCategory.UNCHECKED);
mcimadamore@775 1084 }
mcimadamore@775 1085 //where
mcimadamore@775 1086 private Symbol adjustVarargs(Symbol to, Symbol from, boolean useVarargs) {
mcimadamore@775 1087 List<Type> fromArgs = from.type.getParameterTypes();
mcimadamore@775 1088 List<Type> toArgs = to.type.getParameterTypes();
mcimadamore@775 1089 if (useVarargs &&
mcimadamore@775 1090 (from.flags() & VARARGS) != 0 &&
mcimadamore@775 1091 (to.flags() & VARARGS) != 0) {
mcimadamore@775 1092 Type varargsTypeFrom = fromArgs.last();
mcimadamore@775 1093 Type varargsTypeTo = toArgs.last();
mcimadamore@787 1094 ListBuffer<Type> args = ListBuffer.lb();
mcimadamore@787 1095 if (toArgs.length() < fromArgs.length()) {
mcimadamore@787 1096 //if we are checking a varargs method 'from' against another varargs
mcimadamore@787 1097 //method 'to' (where arity of 'to' < arity of 'from') then expand signature
mcimadamore@787 1098 //of 'to' to 'fit' arity of 'from' (this means adding fake formals to 'to'
mcimadamore@787 1099 //until 'to' signature has the same arity as 'from')
mcimadamore@787 1100 while (fromArgs.head != varargsTypeFrom) {
mcimadamore@787 1101 args.append(toArgs.head == varargsTypeTo ? types.elemtype(varargsTypeTo) : toArgs.head);
mcimadamore@787 1102 fromArgs = fromArgs.tail;
mcimadamore@787 1103 toArgs = toArgs.head == varargsTypeTo ?
mcimadamore@787 1104 toArgs :
mcimadamore@787 1105 toArgs.tail;
mcimadamore@787 1106 }
mcimadamore@787 1107 } else {
mcimadamore@787 1108 //formal argument list is same as original list where last
mcimadamore@787 1109 //argument (array type) is removed
mcimadamore@787 1110 args.appendList(toArgs.reverse().tail.reverse());
mcimadamore@775 1111 }
mcimadamore@787 1112 //append varargs element type as last synthetic formal
mcimadamore@787 1113 args.append(types.elemtype(varargsTypeTo));
dlsmith@880 1114 Type mtype = types.createMethodTypeWithParameters(to.type, args.toList());
mcimadamore@1006 1115 return new MethodSymbol(to.flags_field & ~VARARGS, to.name, mtype, to.owner);
mcimadamore@775 1116 } else {
mcimadamore@775 1117 return to;
mcimadamore@775 1118 }
mcimadamore@775 1119 }
mcimadamore@844 1120 //where
mcimadamore@1059 1121 Type mostSpecificReturnType(Type mt1, Type mt2) {
mcimadamore@1059 1122 Type rt1 = mt1.getReturnType();
mcimadamore@1059 1123 Type rt2 = mt2.getReturnType();
mcimadamore@1059 1124
mcimadamore@1059 1125 if (mt1.tag == FORALL && mt2.tag == FORALL) {
mcimadamore@1059 1126 //if both are generic methods, adjust return type ahead of subtyping check
mcimadamore@1059 1127 rt1 = types.subst(rt1, mt1.getTypeArguments(), mt2.getTypeArguments());
mcimadamore@1059 1128 }
mcimadamore@1059 1129 //first use subtyping, then return type substitutability
mcimadamore@1059 1130 if (types.isSubtype(rt1, rt2)) {
mcimadamore@1059 1131 return mt1;
mcimadamore@1059 1132 } else if (types.isSubtype(rt2, rt1)) {
mcimadamore@1059 1133 return mt2;
mcimadamore@1059 1134 } else if (types.returnTypeSubstitutable(mt1, mt2)) {
mcimadamore@1059 1135 return mt1;
mcimadamore@1059 1136 } else if (types.returnTypeSubstitutable(mt2, mt1)) {
mcimadamore@1059 1137 return mt2;
mcimadamore@1059 1138 } else {
mcimadamore@1059 1139 return null;
mcimadamore@1059 1140 }
mcimadamore@1059 1141 }
mcimadamore@1059 1142 //where
mcimadamore@844 1143 Symbol ambiguityError(Symbol m1, Symbol m2) {
mcimadamore@844 1144 if (((m1.flags() | m2.flags()) & CLASH) != 0) {
mcimadamore@844 1145 return (m1.flags() & CLASH) == 0 ? m1 : m2;
mcimadamore@844 1146 } else {
mcimadamore@844 1147 return new AmbiguityError(m1, m2);
mcimadamore@844 1148 }
mcimadamore@844 1149 }
duke@1 1150
duke@1 1151 /** Find best qualified method matching given name, type and value
duke@1 1152 * arguments.
duke@1 1153 * @param env The current environment.
duke@1 1154 * @param site The original type from where the selection
duke@1 1155 * takes place.
duke@1 1156 * @param name The method's name.
duke@1 1157 * @param argtypes The method's value arguments.
duke@1 1158 * @param typeargtypes The method's type arguments
duke@1 1159 * @param allowBoxing Allow boxing conversions of arguments.
duke@1 1160 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 1161 */
duke@1 1162 Symbol findMethod(Env<AttrContext> env,
duke@1 1163 Type site,
duke@1 1164 Name name,
duke@1 1165 List<Type> argtypes,
duke@1 1166 List<Type> typeargtypes,
duke@1 1167 boolean allowBoxing,
duke@1 1168 boolean useVarargs,
duke@1 1169 boolean operator) {
jrose@571 1170 Symbol bestSoFar = methodNotFound;
mcimadamore@1114 1171 bestSoFar = findMethod(env,
duke@1 1172 site,
duke@1 1173 name,
duke@1 1174 argtypes,
duke@1 1175 typeargtypes,
duke@1 1176 site.tsym.type,
duke@1 1177 true,
jrose@571 1178 bestSoFar,
duke@1 1179 allowBoxing,
duke@1 1180 useVarargs,
mcimadamore@913 1181 operator,
mcimadamore@913 1182 new HashSet<TypeSymbol>());
mcimadamore@1114 1183 reportVerboseResolutionDiagnostic(env.tree.pos(), name, site, argtypes, typeargtypes, bestSoFar);
mcimadamore@1114 1184 return bestSoFar;
duke@1 1185 }
duke@1 1186 // where
duke@1 1187 private Symbol findMethod(Env<AttrContext> env,
duke@1 1188 Type site,
duke@1 1189 Name name,
duke@1 1190 List<Type> argtypes,
duke@1 1191 List<Type> typeargtypes,
duke@1 1192 Type intype,
duke@1 1193 boolean abstractok,
duke@1 1194 Symbol bestSoFar,
duke@1 1195 boolean allowBoxing,
duke@1 1196 boolean useVarargs,
mcimadamore@913 1197 boolean operator,
mcimadamore@913 1198 Set<TypeSymbol> seen) {
mcimadamore@19 1199 for (Type ct = intype; ct.tag == CLASS || ct.tag == TYPEVAR; ct = types.supertype(ct)) {
mcimadamore@19 1200 while (ct.tag == TYPEVAR)
mcimadamore@19 1201 ct = ct.getUpperBound();
duke@1 1202 ClassSymbol c = (ClassSymbol)ct.tsym;
mcimadamore@913 1203 if (!seen.add(c)) return bestSoFar;
mcimadamore@135 1204 if ((c.flags() & (ABSTRACT | INTERFACE | ENUM)) == 0)
duke@1 1205 abstractok = false;
duke@1 1206 for (Scope.Entry e = c.members().lookup(name);
duke@1 1207 e.scope != null;
duke@1 1208 e = e.next()) {
duke@1 1209 //- System.out.println(" e " + e.sym);
duke@1 1210 if (e.sym.kind == MTH &&
duke@1 1211 (e.sym.flags_field & SYNTHETIC) == 0) {
duke@1 1212 bestSoFar = selectBest(env, site, argtypes, typeargtypes,
duke@1 1213 e.sym, bestSoFar,
duke@1 1214 allowBoxing,
duke@1 1215 useVarargs,
duke@1 1216 operator);
duke@1 1217 }
duke@1 1218 }
mcimadamore@537 1219 if (name == names.init)
mcimadamore@537 1220 break;
duke@1 1221 //- System.out.println(" - " + bestSoFar);
duke@1 1222 if (abstractok) {
duke@1 1223 Symbol concrete = methodNotFound;
duke@1 1224 if ((bestSoFar.flags() & ABSTRACT) == 0)
duke@1 1225 concrete = bestSoFar;
duke@1 1226 for (List<Type> l = types.interfaces(c.type);
duke@1 1227 l.nonEmpty();
duke@1 1228 l = l.tail) {
duke@1 1229 bestSoFar = findMethod(env, site, name, argtypes,
duke@1 1230 typeargtypes,
duke@1 1231 l.head, abstractok, bestSoFar,
mcimadamore@913 1232 allowBoxing, useVarargs, operator, seen);
duke@1 1233 }
duke@1 1234 if (concrete != bestSoFar &&
duke@1 1235 concrete.kind < ERR && bestSoFar.kind < ERR &&
duke@1 1236 types.isSubSignature(concrete.type, bestSoFar.type))
duke@1 1237 bestSoFar = concrete;
duke@1 1238 }
duke@1 1239 }
duke@1 1240 return bestSoFar;
duke@1 1241 }
duke@1 1242
duke@1 1243 /** Find unqualified method matching given name, type and value arguments.
duke@1 1244 * @param env The current environment.
duke@1 1245 * @param name The method's name.
duke@1 1246 * @param argtypes The method's value arguments.
duke@1 1247 * @param typeargtypes The method's type arguments.
duke@1 1248 * @param allowBoxing Allow boxing conversions of arguments.
duke@1 1249 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 1250 */
duke@1 1251 Symbol findFun(Env<AttrContext> env, Name name,
duke@1 1252 List<Type> argtypes, List<Type> typeargtypes,
duke@1 1253 boolean allowBoxing, boolean useVarargs) {
duke@1 1254 Symbol bestSoFar = methodNotFound;
duke@1 1255 Symbol sym;
duke@1 1256 Env<AttrContext> env1 = env;
duke@1 1257 boolean staticOnly = false;
duke@1 1258 while (env1.outer != null) {
duke@1 1259 if (isStatic(env1)) staticOnly = true;
duke@1 1260 sym = findMethod(
duke@1 1261 env1, env1.enclClass.sym.type, name, argtypes, typeargtypes,
duke@1 1262 allowBoxing, useVarargs, false);
duke@1 1263 if (sym.exists()) {
duke@1 1264 if (staticOnly &&
duke@1 1265 sym.kind == MTH &&
duke@1 1266 sym.owner.kind == TYP &&
duke@1 1267 (sym.flags() & STATIC) == 0) return new StaticError(sym);
duke@1 1268 else return sym;
duke@1 1269 } else if (sym.kind < bestSoFar.kind) {
duke@1 1270 bestSoFar = sym;
duke@1 1271 }
duke@1 1272 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
duke@1 1273 env1 = env1.outer;
duke@1 1274 }
duke@1 1275
duke@1 1276 sym = findMethod(env, syms.predefClass.type, name, argtypes,
duke@1 1277 typeargtypes, allowBoxing, useVarargs, false);
duke@1 1278 if (sym.exists())
duke@1 1279 return sym;
duke@1 1280
duke@1 1281 Scope.Entry e = env.toplevel.namedImportScope.lookup(name);
duke@1 1282 for (; e.scope != null; e = e.next()) {
duke@1 1283 sym = e.sym;
duke@1 1284 Type origin = e.getOrigin().owner.type;
duke@1 1285 if (sym.kind == MTH) {
duke@1 1286 if (e.sym.owner.type != origin)
duke@1 1287 sym = sym.clone(e.getOrigin().owner);
duke@1 1288 if (!isAccessible(env, origin, sym))
duke@1 1289 sym = new AccessError(env, origin, sym);
duke@1 1290 bestSoFar = selectBest(env, origin,
duke@1 1291 argtypes, typeargtypes,
duke@1 1292 sym, bestSoFar,
duke@1 1293 allowBoxing, useVarargs, false);
duke@1 1294 }
duke@1 1295 }
duke@1 1296 if (bestSoFar.exists())
duke@1 1297 return bestSoFar;
duke@1 1298
duke@1 1299 e = env.toplevel.starImportScope.lookup(name);
duke@1 1300 for (; e.scope != null; e = e.next()) {
duke@1 1301 sym = e.sym;
duke@1 1302 Type origin = e.getOrigin().owner.type;
duke@1 1303 if (sym.kind == MTH) {
duke@1 1304 if (e.sym.owner.type != origin)
duke@1 1305 sym = sym.clone(e.getOrigin().owner);
duke@1 1306 if (!isAccessible(env, origin, sym))
duke@1 1307 sym = new AccessError(env, origin, sym);
duke@1 1308 bestSoFar = selectBest(env, origin,
duke@1 1309 argtypes, typeargtypes,
duke@1 1310 sym, bestSoFar,
duke@1 1311 allowBoxing, useVarargs, false);
duke@1 1312 }
duke@1 1313 }
duke@1 1314 return bestSoFar;
duke@1 1315 }
duke@1 1316
duke@1 1317 /** Load toplevel or member class with given fully qualified name and
duke@1 1318 * verify that it is accessible.
duke@1 1319 * @param env The current environment.
duke@1 1320 * @param name The fully qualified name of the class to be loaded.
duke@1 1321 */
duke@1 1322 Symbol loadClass(Env<AttrContext> env, Name name) {
duke@1 1323 try {
duke@1 1324 ClassSymbol c = reader.loadClass(name);
duke@1 1325 return isAccessible(env, c) ? c : new AccessError(c);
duke@1 1326 } catch (ClassReader.BadClassFile err) {
duke@1 1327 throw err;
duke@1 1328 } catch (CompletionFailure ex) {
duke@1 1329 return typeNotFound;
duke@1 1330 }
duke@1 1331 }
duke@1 1332
duke@1 1333 /** Find qualified member type.
duke@1 1334 * @param env The current environment.
duke@1 1335 * @param site The original type from where the selection takes
duke@1 1336 * place.
duke@1 1337 * @param name The type's name.
duke@1 1338 * @param c The class to search for the member type. This is
duke@1 1339 * always a superclass or implemented interface of
duke@1 1340 * site's class.
duke@1 1341 */
duke@1 1342 Symbol findMemberType(Env<AttrContext> env,
duke@1 1343 Type site,
duke@1 1344 Name name,
duke@1 1345 TypeSymbol c) {
duke@1 1346 Symbol bestSoFar = typeNotFound;
duke@1 1347 Symbol sym;
duke@1 1348 Scope.Entry e = c.members().lookup(name);
duke@1 1349 while (e.scope != null) {
duke@1 1350 if (e.sym.kind == TYP) {
duke@1 1351 return isAccessible(env, site, e.sym)
duke@1 1352 ? e.sym
duke@1 1353 : new AccessError(env, site, e.sym);
duke@1 1354 }
duke@1 1355 e = e.next();
duke@1 1356 }
duke@1 1357 Type st = types.supertype(c.type);
duke@1 1358 if (st != null && st.tag == CLASS) {
duke@1 1359 sym = findMemberType(env, site, name, st.tsym);
duke@1 1360 if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1361 }
duke@1 1362 for (List<Type> l = types.interfaces(c.type);
duke@1 1363 bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
duke@1 1364 l = l.tail) {
duke@1 1365 sym = findMemberType(env, site, name, l.head.tsym);
duke@1 1366 if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS &&
duke@1 1367 sym.owner != bestSoFar.owner)
duke@1 1368 bestSoFar = new AmbiguityError(bestSoFar, sym);
duke@1 1369 else if (sym.kind < bestSoFar.kind)
duke@1 1370 bestSoFar = sym;
duke@1 1371 }
duke@1 1372 return bestSoFar;
duke@1 1373 }
duke@1 1374
duke@1 1375 /** Find a global type in given scope and load corresponding class.
duke@1 1376 * @param env The current environment.
duke@1 1377 * @param scope The scope in which to look for the type.
duke@1 1378 * @param name The type's name.
duke@1 1379 */
duke@1 1380 Symbol findGlobalType(Env<AttrContext> env, Scope scope, Name name) {
duke@1 1381 Symbol bestSoFar = typeNotFound;
duke@1 1382 for (Scope.Entry e = scope.lookup(name); e.scope != null; e = e.next()) {
duke@1 1383 Symbol sym = loadClass(env, e.sym.flatName());
duke@1 1384 if (bestSoFar.kind == TYP && sym.kind == TYP &&
duke@1 1385 bestSoFar != sym)
duke@1 1386 return new AmbiguityError(bestSoFar, sym);
duke@1 1387 else if (sym.kind < bestSoFar.kind)
duke@1 1388 bestSoFar = sym;
duke@1 1389 }
duke@1 1390 return bestSoFar;
duke@1 1391 }
duke@1 1392
duke@1 1393 /** Find an unqualified type symbol.
duke@1 1394 * @param env The current environment.
duke@1 1395 * @param name The type's name.
duke@1 1396 */
duke@1 1397 Symbol findType(Env<AttrContext> env, Name name) {
duke@1 1398 Symbol bestSoFar = typeNotFound;
duke@1 1399 Symbol sym;
duke@1 1400 boolean staticOnly = false;
duke@1 1401 for (Env<AttrContext> env1 = env; env1.outer != null; env1 = env1.outer) {
duke@1 1402 if (isStatic(env1)) staticOnly = true;
duke@1 1403 for (Scope.Entry e = env1.info.scope.lookup(name);
duke@1 1404 e.scope != null;
duke@1 1405 e = e.next()) {
duke@1 1406 if (e.sym.kind == TYP) {
duke@1 1407 if (staticOnly &&
duke@1 1408 e.sym.type.tag == TYPEVAR &&
duke@1 1409 e.sym.owner.kind == TYP) return new StaticError(e.sym);
duke@1 1410 return e.sym;
duke@1 1411 }
duke@1 1412 }
duke@1 1413
duke@1 1414 sym = findMemberType(env1, env1.enclClass.sym.type, name,
duke@1 1415 env1.enclClass.sym);
duke@1 1416 if (staticOnly && sym.kind == TYP &&
duke@1 1417 sym.type.tag == CLASS &&
duke@1 1418 sym.type.getEnclosingType().tag == CLASS &&
duke@1 1419 env1.enclClass.sym.type.isParameterized() &&
duke@1 1420 sym.type.getEnclosingType().isParameterized())
duke@1 1421 return new StaticError(sym);
duke@1 1422 else if (sym.exists()) return sym;
duke@1 1423 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1424
duke@1 1425 JCClassDecl encl = env1.baseClause ? (JCClassDecl)env1.tree : env1.enclClass;
duke@1 1426 if ((encl.sym.flags() & STATIC) != 0)
duke@1 1427 staticOnly = true;
duke@1 1428 }
duke@1 1429
jjg@1127 1430 if (!env.tree.hasTag(IMPORT)) {
duke@1 1431 sym = findGlobalType(env, env.toplevel.namedImportScope, name);
duke@1 1432 if (sym.exists()) return sym;
duke@1 1433 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1434
duke@1 1435 sym = findGlobalType(env, env.toplevel.packge.members(), name);
duke@1 1436 if (sym.exists()) return sym;
duke@1 1437 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1438
duke@1 1439 sym = findGlobalType(env, env.toplevel.starImportScope, name);
duke@1 1440 if (sym.exists()) return sym;
duke@1 1441 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1442 }
duke@1 1443
duke@1 1444 return bestSoFar;
duke@1 1445 }
duke@1 1446
duke@1 1447 /** Find an unqualified identifier which matches a specified kind set.
duke@1 1448 * @param env The current environment.
duke@1 1449 * @param name The indentifier's name.
duke@1 1450 * @param kind Indicates the possible symbol kinds
duke@1 1451 * (a subset of VAL, TYP, PCK).
duke@1 1452 */
duke@1 1453 Symbol findIdent(Env<AttrContext> env, Name name, int kind) {
duke@1 1454 Symbol bestSoFar = typeNotFound;
duke@1 1455 Symbol sym;
duke@1 1456
duke@1 1457 if ((kind & VAR) != 0) {
duke@1 1458 sym = findVar(env, name);
duke@1 1459 if (sym.exists()) return sym;
duke@1 1460 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1461 }
duke@1 1462
duke@1 1463 if ((kind & TYP) != 0) {
duke@1 1464 sym = findType(env, name);
duke@1 1465 if (sym.exists()) return sym;
duke@1 1466 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1467 }
duke@1 1468
duke@1 1469 if ((kind & PCK) != 0) return reader.enterPackage(name);
duke@1 1470 else return bestSoFar;
duke@1 1471 }
duke@1 1472
duke@1 1473 /** Find an identifier in a package which matches a specified kind set.
duke@1 1474 * @param env The current environment.
duke@1 1475 * @param name The identifier's name.
duke@1 1476 * @param kind Indicates the possible symbol kinds
duke@1 1477 * (a nonempty subset of TYP, PCK).
duke@1 1478 */
duke@1 1479 Symbol findIdentInPackage(Env<AttrContext> env, TypeSymbol pck,
duke@1 1480 Name name, int kind) {
duke@1 1481 Name fullname = TypeSymbol.formFullName(name, pck);
duke@1 1482 Symbol bestSoFar = typeNotFound;
duke@1 1483 PackageSymbol pack = null;
duke@1 1484 if ((kind & PCK) != 0) {
duke@1 1485 pack = reader.enterPackage(fullname);
duke@1 1486 if (pack.exists()) return pack;
duke@1 1487 }
duke@1 1488 if ((kind & TYP) != 0) {
duke@1 1489 Symbol sym = loadClass(env, fullname);
duke@1 1490 if (sym.exists()) {
duke@1 1491 // don't allow programs to use flatnames
duke@1 1492 if (name == sym.name) return sym;
duke@1 1493 }
duke@1 1494 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1495 }
duke@1 1496 return (pack != null) ? pack : bestSoFar;
duke@1 1497 }
duke@1 1498
duke@1 1499 /** Find an identifier among the members of a given type `site'.
duke@1 1500 * @param env The current environment.
duke@1 1501 * @param site The type containing the symbol to be found.
duke@1 1502 * @param name The identifier's name.
duke@1 1503 * @param kind Indicates the possible symbol kinds
duke@1 1504 * (a subset of VAL, TYP).
duke@1 1505 */
duke@1 1506 Symbol findIdentInType(Env<AttrContext> env, Type site,
duke@1 1507 Name name, int kind) {
duke@1 1508 Symbol bestSoFar = typeNotFound;
duke@1 1509 Symbol sym;
duke@1 1510 if ((kind & VAR) != 0) {
duke@1 1511 sym = findField(env, site, name, site.tsym);
duke@1 1512 if (sym.exists()) return sym;
duke@1 1513 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1514 }
duke@1 1515
duke@1 1516 if ((kind & TYP) != 0) {
duke@1 1517 sym = findMemberType(env, site, name, site.tsym);
duke@1 1518 if (sym.exists()) return sym;
duke@1 1519 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1520 }
duke@1 1521 return bestSoFar;
duke@1 1522 }
duke@1 1523
duke@1 1524 /* ***************************************************************************
duke@1 1525 * Access checking
duke@1 1526 * The following methods convert ResolveErrors to ErrorSymbols, issuing
duke@1 1527 * an error message in the process
duke@1 1528 ****************************************************************************/
duke@1 1529
duke@1 1530 /** If `sym' is a bad symbol: report error and return errSymbol
duke@1 1531 * else pass through unchanged,
duke@1 1532 * additional arguments duplicate what has been used in trying to find the
duke@1 1533 * symbol (--> flyweight pattern). This improves performance since we
duke@1 1534 * expect misses to happen frequently.
duke@1 1535 *
duke@1 1536 * @param sym The symbol that was found, or a ResolveError.
duke@1 1537 * @param pos The position to use for error reporting.
duke@1 1538 * @param site The original type from where the selection took place.
duke@1 1539 * @param name The symbol's name.
duke@1 1540 * @param argtypes The invocation's value arguments,
duke@1 1541 * if we looked for a method.
duke@1 1542 * @param typeargtypes The invocation's type arguments,
duke@1 1543 * if we looked for a method.
duke@1 1544 */
duke@1 1545 Symbol access(Symbol sym,
duke@1 1546 DiagnosticPosition pos,
mcimadamore@829 1547 Symbol location,
duke@1 1548 Type site,
duke@1 1549 Name name,
duke@1 1550 boolean qualified,
duke@1 1551 List<Type> argtypes,
duke@1 1552 List<Type> typeargtypes) {
duke@1 1553 if (sym.kind >= AMBIGUOUS) {
mcimadamore@302 1554 ResolveError errSym = (ResolveError)sym;
duke@1 1555 if (!site.isErroneous() &&
duke@1 1556 !Type.isErroneous(argtypes) &&
duke@1 1557 (typeargtypes==null || !Type.isErroneous(typeargtypes)))
mcimadamore@829 1558 logResolveError(errSym, pos, location, site, name, argtypes, typeargtypes);
mcimadamore@302 1559 sym = errSym.access(name, qualified ? site.tsym : syms.noSymbol);
duke@1 1560 }
duke@1 1561 return sym;
duke@1 1562 }
duke@1 1563
mcimadamore@829 1564 /** Same as original access(), but without location.
mcimadamore@829 1565 */
mcimadamore@829 1566 Symbol access(Symbol sym,
mcimadamore@829 1567 DiagnosticPosition pos,
mcimadamore@829 1568 Type site,
mcimadamore@829 1569 Name name,
mcimadamore@829 1570 boolean qualified,
mcimadamore@829 1571 List<Type> argtypes,
mcimadamore@829 1572 List<Type> typeargtypes) {
mcimadamore@829 1573 return access(sym, pos, site.tsym, site, name, qualified, argtypes, typeargtypes);
mcimadamore@829 1574 }
mcimadamore@829 1575
mcimadamore@829 1576 /** Same as original access(), but without type arguments and arguments.
mcimadamore@829 1577 */
mcimadamore@829 1578 Symbol access(Symbol sym,
mcimadamore@829 1579 DiagnosticPosition pos,
mcimadamore@829 1580 Symbol location,
mcimadamore@829 1581 Type site,
mcimadamore@829 1582 Name name,
mcimadamore@829 1583 boolean qualified) {
mcimadamore@829 1584 if (sym.kind >= AMBIGUOUS)
mcimadamore@829 1585 return access(sym, pos, location, site, name, qualified, List.<Type>nil(), null);
mcimadamore@829 1586 else
mcimadamore@829 1587 return sym;
mcimadamore@829 1588 }
mcimadamore@829 1589
mcimadamore@829 1590 /** Same as original access(), but without location, type arguments and arguments.
duke@1 1591 */
duke@1 1592 Symbol access(Symbol sym,
duke@1 1593 DiagnosticPosition pos,
duke@1 1594 Type site,
duke@1 1595 Name name,
duke@1 1596 boolean qualified) {
mcimadamore@829 1597 return access(sym, pos, site.tsym, site, name, qualified);
duke@1 1598 }
duke@1 1599
duke@1 1600 /** Check that sym is not an abstract method.
duke@1 1601 */
duke@1 1602 void checkNonAbstract(DiagnosticPosition pos, Symbol sym) {
duke@1 1603 if ((sym.flags() & ABSTRACT) != 0)
duke@1 1604 log.error(pos, "abstract.cant.be.accessed.directly",
duke@1 1605 kindName(sym), sym, sym.location());
duke@1 1606 }
duke@1 1607
duke@1 1608 /* ***************************************************************************
duke@1 1609 * Debugging
duke@1 1610 ****************************************************************************/
duke@1 1611
duke@1 1612 /** print all scopes starting with scope s and proceeding outwards.
duke@1 1613 * used for debugging.
duke@1 1614 */
duke@1 1615 public void printscopes(Scope s) {
duke@1 1616 while (s != null) {
duke@1 1617 if (s.owner != null)
duke@1 1618 System.err.print(s.owner + ": ");
duke@1 1619 for (Scope.Entry e = s.elems; e != null; e = e.sibling) {
duke@1 1620 if ((e.sym.flags() & ABSTRACT) != 0)
duke@1 1621 System.err.print("abstract ");
duke@1 1622 System.err.print(e.sym + " ");
duke@1 1623 }
duke@1 1624 System.err.println();
duke@1 1625 s = s.next;
duke@1 1626 }
duke@1 1627 }
duke@1 1628
duke@1 1629 void printscopes(Env<AttrContext> env) {
duke@1 1630 while (env.outer != null) {
duke@1 1631 System.err.println("------------------------------");
duke@1 1632 printscopes(env.info.scope);
duke@1 1633 env = env.outer;
duke@1 1634 }
duke@1 1635 }
duke@1 1636
duke@1 1637 public void printscopes(Type t) {
duke@1 1638 while (t.tag == CLASS) {
duke@1 1639 printscopes(t.tsym.members());
duke@1 1640 t = types.supertype(t);
duke@1 1641 }
duke@1 1642 }
duke@1 1643
duke@1 1644 /* ***************************************************************************
duke@1 1645 * Name resolution
duke@1 1646 * Naming conventions are as for symbol lookup
duke@1 1647 * Unlike the find... methods these methods will report access errors
duke@1 1648 ****************************************************************************/
duke@1 1649
duke@1 1650 /** Resolve an unqualified (non-method) identifier.
duke@1 1651 * @param pos The position to use for error reporting.
duke@1 1652 * @param env The environment current at the identifier use.
duke@1 1653 * @param name The identifier's name.
duke@1 1654 * @param kind The set of admissible symbol kinds for the identifier.
duke@1 1655 */
duke@1 1656 Symbol resolveIdent(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 1657 Name name, int kind) {
duke@1 1658 return access(
duke@1 1659 findIdent(env, name, kind),
duke@1 1660 pos, env.enclClass.sym.type, name, false);
duke@1 1661 }
duke@1 1662
duke@1 1663 /** Resolve an unqualified method identifier.
duke@1 1664 * @param pos The position to use for error reporting.
duke@1 1665 * @param env The environment current at the method invocation.
duke@1 1666 * @param name The identifier's name.
duke@1 1667 * @param argtypes The types of the invocation's value arguments.
duke@1 1668 * @param typeargtypes The types of the invocation's type arguments.
duke@1 1669 */
duke@1 1670 Symbol resolveMethod(DiagnosticPosition pos,
duke@1 1671 Env<AttrContext> env,
duke@1 1672 Name name,
duke@1 1673 List<Type> argtypes,
duke@1 1674 List<Type> typeargtypes) {
mcimadamore@1215 1675 MethodResolutionContext prevResolutionContext = currentResolutionContext;
mcimadamore@1215 1676 try {
mcimadamore@1215 1677 currentResolutionContext = new MethodResolutionContext();
mcimadamore@1215 1678 Symbol sym = methodNotFound;
mcimadamore@1215 1679 List<MethodResolutionPhase> steps = methodResolutionSteps;
mcimadamore@1215 1680 while (steps.nonEmpty() &&
mcimadamore@1215 1681 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@1215 1682 sym.kind >= ERRONEOUS) {
mcimadamore@1215 1683 currentResolutionContext.step = steps.head;
mcimadamore@1215 1684 sym = findFun(env, name, argtypes, typeargtypes,
mcimadamore@1215 1685 steps.head.isBoxingRequired,
mcimadamore@1215 1686 env.info.varArgs = steps.head.isVarargsRequired);
mcimadamore@1215 1687 currentResolutionContext.resolutionCache.put(steps.head, sym);
mcimadamore@1215 1688 steps = steps.tail;
mcimadamore@1215 1689 }
mcimadamore@1215 1690 if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error
mcimadamore@1215 1691 MethodResolutionPhase errPhase =
mcimadamore@1215 1692 currentResolutionContext.firstErroneousResolutionPhase();
mcimadamore@1215 1693 sym = access(currentResolutionContext.resolutionCache.get(errPhase),
mcimadamore@1215 1694 pos, env.enclClass.sym.type, name, false, argtypes, typeargtypes);
mcimadamore@1215 1695 env.info.varArgs = errPhase.isVarargsRequired;
mcimadamore@1215 1696 }
mcimadamore@1215 1697 return sym;
duke@1 1698 }
mcimadamore@1215 1699 finally {
mcimadamore@1215 1700 currentResolutionContext = prevResolutionContext;
duke@1 1701 }
mcimadamore@689 1702 }
mcimadamore@689 1703
duke@1 1704 /** Resolve a qualified method identifier
duke@1 1705 * @param pos The position to use for error reporting.
duke@1 1706 * @param env The environment current at the method invocation.
duke@1 1707 * @param site The type of the qualifying expression, in which
duke@1 1708 * identifier is searched.
duke@1 1709 * @param name The identifier's name.
duke@1 1710 * @param argtypes The types of the invocation's value arguments.
duke@1 1711 * @param typeargtypes The types of the invocation's type arguments.
duke@1 1712 */
duke@1 1713 Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 1714 Type site, Name name, List<Type> argtypes,
duke@1 1715 List<Type> typeargtypes) {
mcimadamore@829 1716 return resolveQualifiedMethod(pos, env, site.tsym, site, name, argtypes, typeargtypes);
mcimadamore@829 1717 }
mcimadamore@829 1718 Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env,
mcimadamore@829 1719 Symbol location, Type site, Name name, List<Type> argtypes,
mcimadamore@829 1720 List<Type> typeargtypes) {
mcimadamore@1215 1721 return resolveQualifiedMethod(new MethodResolutionContext(), pos, env, location, site, name, argtypes, typeargtypes);
mcimadamore@1215 1722 }
mcimadamore@1215 1723 private Symbol resolveQualifiedMethod(MethodResolutionContext resolveContext,
mcimadamore@1215 1724 DiagnosticPosition pos, Env<AttrContext> env,
mcimadamore@1215 1725 Symbol location, Type site, Name name, List<Type> argtypes,
mcimadamore@1215 1726 List<Type> typeargtypes) {
mcimadamore@1215 1727 MethodResolutionContext prevResolutionContext = currentResolutionContext;
mcimadamore@1215 1728 try {
mcimadamore@1215 1729 currentResolutionContext = resolveContext;
mcimadamore@1215 1730 Symbol sym = methodNotFound;
mcimadamore@1215 1731 List<MethodResolutionPhase> steps = methodResolutionSteps;
mcimadamore@1215 1732 while (steps.nonEmpty() &&
mcimadamore@1215 1733 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@1215 1734 sym.kind >= ERRONEOUS) {
mcimadamore@1215 1735 currentResolutionContext.step = steps.head;
mcimadamore@1215 1736 sym = findMethod(env, site, name, argtypes, typeargtypes,
mcimadamore@1215 1737 steps.head.isBoxingRequired(),
mcimadamore@1215 1738 env.info.varArgs = steps.head.isVarargsRequired(), false);
mcimadamore@1215 1739 currentResolutionContext.resolutionCache.put(steps.head, sym);
mcimadamore@1215 1740 steps = steps.tail;
mcimadamore@1215 1741 }
mcimadamore@1215 1742 if (sym.kind >= AMBIGUOUS) {
mcimadamore@1215 1743 if (site.tsym.isPolymorphicSignatureGeneric()) {
mcimadamore@1215 1744 //polymorphic receiver - synthesize new method symbol
mcimadamore@1215 1745 env.info.varArgs = false;
mcimadamore@1215 1746 sym = findPolymorphicSignatureInstance(env,
mcimadamore@1215 1747 site, name, null, argtypes);
mcimadamore@1215 1748 }
mcimadamore@1215 1749 else {
mcimadamore@1215 1750 //if nothing is found return the 'first' error
mcimadamore@1215 1751 MethodResolutionPhase errPhase =
mcimadamore@1215 1752 currentResolutionContext.firstErroneousResolutionPhase();
mcimadamore@1215 1753 sym = access(currentResolutionContext.resolutionCache.get(errPhase),
mcimadamore@1215 1754 pos, location, site, name, true, argtypes, typeargtypes);
mcimadamore@1215 1755 env.info.varArgs = errPhase.isVarargsRequired;
mcimadamore@1215 1756 }
mcimadamore@1215 1757 } else if (allowMethodHandles && sym.isPolymorphicSignatureGeneric()) {
mcimadamore@1215 1758 //non-instantiated polymorphic signature - synthesize new method symbol
mcimadamore@674 1759 env.info.varArgs = false;
mcimadamore@674 1760 sym = findPolymorphicSignatureInstance(env,
mcimadamore@1215 1761 site, name, (MethodSymbol)sym, argtypes);
mcimadamore@674 1762 }
mcimadamore@1215 1763 return sym;
duke@1 1764 }
mcimadamore@1215 1765 finally {
mcimadamore@1215 1766 currentResolutionContext = prevResolutionContext;
mcimadamore@1215 1767 }
duke@1 1768 }
duke@1 1769
mcimadamore@674 1770 /** Find or create an implicit method of exactly the given type (after erasure).
mcimadamore@674 1771 * Searches in a side table, not the main scope of the site.
mcimadamore@674 1772 * This emulates the lookup process required by JSR 292 in JVM.
mcimadamore@674 1773 * @param env Attribution environment
mcimadamore@674 1774 * @param site The original type from where the selection takes place.
mcimadamore@674 1775 * @param name The method's name.
mcimadamore@674 1776 * @param spMethod A template for the implicit method, or null.
mcimadamore@674 1777 * @param argtypes The required argument types.
mcimadamore@674 1778 * @param typeargtypes The required type arguments.
mcimadamore@674 1779 */
mcimadamore@674 1780 Symbol findPolymorphicSignatureInstance(Env<AttrContext> env, Type site,
mcimadamore@674 1781 Name name,
mcimadamore@674 1782 MethodSymbol spMethod, // sig. poly. method or null if none
mcimadamore@820 1783 List<Type> argtypes) {
mcimadamore@674 1784 Type mtype = infer.instantiatePolymorphicSignatureInstance(env,
mcimadamore@820 1785 site, name, spMethod, argtypes);
mcimadamore@674 1786 long flags = ABSTRACT | HYPOTHETICAL | POLYMORPHIC_SIGNATURE |
mcimadamore@674 1787 (spMethod != null ?
mcimadamore@674 1788 spMethod.flags() & Flags.AccessFlags :
mcimadamore@674 1789 Flags.PUBLIC | Flags.STATIC);
mcimadamore@674 1790 Symbol m = null;
mcimadamore@674 1791 for (Scope.Entry e = polymorphicSignatureScope.lookup(name);
mcimadamore@674 1792 e.scope != null;
mcimadamore@674 1793 e = e.next()) {
mcimadamore@674 1794 Symbol sym = e.sym;
mcimadamore@674 1795 if (types.isSameType(mtype, sym.type) &&
mcimadamore@674 1796 (sym.flags() & Flags.STATIC) == (flags & Flags.STATIC) &&
mcimadamore@674 1797 types.isSameType(sym.owner.type, site)) {
mcimadamore@674 1798 m = sym;
mcimadamore@674 1799 break;
mcimadamore@674 1800 }
mcimadamore@674 1801 }
mcimadamore@674 1802 if (m == null) {
mcimadamore@674 1803 // create the desired method
mcimadamore@674 1804 m = new MethodSymbol(flags, name, mtype, site.tsym);
mcimadamore@674 1805 polymorphicSignatureScope.enter(m);
mcimadamore@674 1806 }
mcimadamore@674 1807 return m;
mcimadamore@674 1808 }
mcimadamore@674 1809
duke@1 1810 /** Resolve a qualified method identifier, throw a fatal error if not
duke@1 1811 * found.
duke@1 1812 * @param pos The position to use for error reporting.
duke@1 1813 * @param env The environment current at the method invocation.
duke@1 1814 * @param site The type of the qualifying expression, in which
duke@1 1815 * identifier is searched.
duke@1 1816 * @param name The identifier's name.
duke@1 1817 * @param argtypes The types of the invocation's value arguments.
duke@1 1818 * @param typeargtypes The types of the invocation's type arguments.
duke@1 1819 */
duke@1 1820 public MethodSymbol resolveInternalMethod(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 1821 Type site, Name name,
duke@1 1822 List<Type> argtypes,
duke@1 1823 List<Type> typeargtypes) {
mcimadamore@1215 1824 MethodResolutionContext resolveContext = new MethodResolutionContext();
mcimadamore@1215 1825 resolveContext.internalResolution = true;
mcimadamore@1215 1826 Symbol sym = resolveQualifiedMethod(resolveContext, pos, env, site.tsym,
mcimadamore@1215 1827 site, name, argtypes, typeargtypes);
mcimadamore@1215 1828 if (sym.kind == MTH) return (MethodSymbol)sym;
mcimadamore@1215 1829 else throw new FatalError(
mcimadamore@1215 1830 diags.fragment("fatal.err.cant.locate.meth",
mcimadamore@1215 1831 name));
duke@1 1832 }
duke@1 1833
duke@1 1834 /** Resolve constructor.
duke@1 1835 * @param pos The position to use for error reporting.
duke@1 1836 * @param env The environment current at the constructor invocation.
duke@1 1837 * @param site The type of class for which a constructor is searched.
duke@1 1838 * @param argtypes The types of the constructor invocation's value
duke@1 1839 * arguments.
duke@1 1840 * @param typeargtypes The types of the constructor invocation's type
duke@1 1841 * arguments.
duke@1 1842 */
duke@1 1843 Symbol resolveConstructor(DiagnosticPosition pos,
duke@1 1844 Env<AttrContext> env,
duke@1 1845 Type site,
duke@1 1846 List<Type> argtypes,
duke@1 1847 List<Type> typeargtypes) {
mcimadamore@1215 1848 return resolveConstructor(new MethodResolutionContext(), pos, env, site, argtypes, typeargtypes);
mcimadamore@1215 1849 }
mcimadamore@1215 1850 private Symbol resolveConstructor(MethodResolutionContext resolveContext,
mcimadamore@1215 1851 DiagnosticPosition pos,
mcimadamore@1215 1852 Env<AttrContext> env,
mcimadamore@1215 1853 Type site,
mcimadamore@1215 1854 List<Type> argtypes,
mcimadamore@1215 1855 List<Type> typeargtypes) {
mcimadamore@1215 1856 MethodResolutionContext prevResolutionContext = currentResolutionContext;
mcimadamore@1215 1857 try {
mcimadamore@1215 1858 currentResolutionContext = resolveContext;
mcimadamore@1215 1859 Symbol sym = methodNotFound;
mcimadamore@1215 1860 List<MethodResolutionPhase> steps = methodResolutionSteps;
mcimadamore@1215 1861 while (steps.nonEmpty() &&
mcimadamore@1215 1862 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@1215 1863 sym.kind >= ERRONEOUS) {
mcimadamore@1215 1864 currentResolutionContext.step = steps.head;
mcimadamore@1215 1865 sym = findConstructor(pos, env, site, argtypes, typeargtypes,
mcimadamore@1215 1866 steps.head.isBoxingRequired(),
mcimadamore@1215 1867 env.info.varArgs = steps.head.isVarargsRequired());
mcimadamore@1215 1868 currentResolutionContext.resolutionCache.put(steps.head, sym);
mcimadamore@1215 1869 steps = steps.tail;
mcimadamore@1215 1870 }
mcimadamore@1215 1871 if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error
mcimadamore@1215 1872 MethodResolutionPhase errPhase = currentResolutionContext.firstErroneousResolutionPhase();
mcimadamore@1215 1873 sym = access(currentResolutionContext.resolutionCache.get(errPhase),
mcimadamore@1215 1874 pos, site, names.init, true, argtypes, typeargtypes);
mcimadamore@1215 1875 env.info.varArgs = errPhase.isVarargsRequired();
mcimadamore@1215 1876 }
mcimadamore@1215 1877 return sym;
duke@1 1878 }
mcimadamore@1215 1879 finally {
mcimadamore@1215 1880 currentResolutionContext = prevResolutionContext;
duke@1 1881 }
duke@1 1882 }
duke@1 1883
mcimadamore@537 1884 /** Resolve constructor using diamond inference.
mcimadamore@537 1885 * @param pos The position to use for error reporting.
mcimadamore@537 1886 * @param env The environment current at the constructor invocation.
mcimadamore@537 1887 * @param site The type of class for which a constructor is searched.
mcimadamore@537 1888 * The scope of this class has been touched in attribution.
mcimadamore@537 1889 * @param argtypes The types of the constructor invocation's value
mcimadamore@537 1890 * arguments.
mcimadamore@537 1891 * @param typeargtypes The types of the constructor invocation's type
mcimadamore@537 1892 * arguments.
mcimadamore@537 1893 */
mcimadamore@537 1894 Symbol resolveDiamond(DiagnosticPosition pos,
mcimadamore@537 1895 Env<AttrContext> env,
mcimadamore@537 1896 Type site,
mcimadamore@537 1897 List<Type> argtypes,
mcimadamore@631 1898 List<Type> typeargtypes) {
mcimadamore@1215 1899 MethodResolutionContext prevResolutionContext = currentResolutionContext;
mcimadamore@1215 1900 try {
mcimadamore@1215 1901 currentResolutionContext = new MethodResolutionContext();
mcimadamore@1215 1902 Symbol sym = methodNotFound;
mcimadamore@1215 1903 List<MethodResolutionPhase> steps = methodResolutionSteps;
mcimadamore@1215 1904 while (steps.nonEmpty() &&
mcimadamore@1215 1905 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@1215 1906 sym.kind >= ERRONEOUS) {
mcimadamore@1215 1907 currentResolutionContext.step = steps.head;
mcimadamore@1217 1908 sym = findDiamond(env, site, argtypes, typeargtypes,
mcimadamore@1215 1909 steps.head.isBoxingRequired(),
mcimadamore@1215 1910 env.info.varArgs = steps.head.isVarargsRequired());
mcimadamore@1215 1911 currentResolutionContext.resolutionCache.put(steps.head, sym);
mcimadamore@1215 1912 steps = steps.tail;
mcimadamore@1215 1913 }
mcimadamore@1215 1914 if (sym.kind >= AMBIGUOUS) {
mcimadamore@1215 1915 final JCDiagnostic details = sym.kind == WRONG_MTH ?
mcimadamore@1215 1916 currentResolutionContext.candidates.head.details :
mcimadamore@1215 1917 null;
mcimadamore@1215 1918 Symbol errSym = new ResolveError(WRONG_MTH, "diamond error") {
mcimadamore@1215 1919 @Override
mcimadamore@1215 1920 JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos,
mcimadamore@1215 1921 Symbol location, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) {
mcimadamore@1215 1922 String key = details == null ?
mcimadamore@1215 1923 "cant.apply.diamond" :
mcimadamore@1215 1924 "cant.apply.diamond.1";
mcimadamore@1215 1925 return diags.create(dkind, log.currentSource(), pos, key,
mcimadamore@1215 1926 diags.fragment("diamond", site.tsym), details);
mcimadamore@1215 1927 }
mcimadamore@1215 1928 };
mcimadamore@1215 1929 MethodResolutionPhase errPhase = currentResolutionContext.firstErroneousResolutionPhase();
mcimadamore@1215 1930 sym = access(errSym, pos, site, names.init, true, argtypes, typeargtypes);
mcimadamore@1215 1931 env.info.varArgs = errPhase.isVarargsRequired();
mcimadamore@1215 1932 }
mcimadamore@1215 1933 return sym;
mcimadamore@537 1934 }
mcimadamore@1215 1935 finally {
mcimadamore@1215 1936 currentResolutionContext = prevResolutionContext;
mcimadamore@537 1937 }
mcimadamore@537 1938 }
mcimadamore@537 1939
mcimadamore@1217 1940 /** This method scans all the constructor symbol in a given class scope -
mcimadamore@1217 1941 * assuming that the original scope contains a constructor of the kind:
mcimadamore@1217 1942 * Foo(X x, Y y), where X,Y are class type-variables declared in Foo,
mcimadamore@1217 1943 * a method check is executed against the modified constructor type:
mcimadamore@1217 1944 * <X,Y>Foo<X,Y>(X x, Y y). This is crucial in order to enable diamond
mcimadamore@1217 1945 * inference. The inferred return type of the synthetic constructor IS
mcimadamore@1217 1946 * the inferred type for the diamond operator.
mcimadamore@1217 1947 */
mcimadamore@1217 1948 private Symbol findDiamond(Env<AttrContext> env,
mcimadamore@1217 1949 Type site,
mcimadamore@1217 1950 List<Type> argtypes,
mcimadamore@1217 1951 List<Type> typeargtypes,
mcimadamore@1217 1952 boolean allowBoxing,
mcimadamore@1217 1953 boolean useVarargs) {
mcimadamore@1217 1954 Symbol bestSoFar = methodNotFound;
mcimadamore@1217 1955 for (Scope.Entry e = site.tsym.members().lookup(names.init);
mcimadamore@1217 1956 e.scope != null;
mcimadamore@1217 1957 e = e.next()) {
mcimadamore@1217 1958 //- System.out.println(" e " + e.sym);
mcimadamore@1217 1959 if (e.sym.kind == MTH &&
mcimadamore@1217 1960 (e.sym.flags_field & SYNTHETIC) == 0) {
mcimadamore@1217 1961 List<Type> oldParams = e.sym.type.tag == FORALL ?
mcimadamore@1217 1962 ((ForAll)e.sym.type).tvars :
mcimadamore@1217 1963 List.<Type>nil();
mcimadamore@1217 1964 Type constrType = new ForAll(site.tsym.type.getTypeArguments().appendList(oldParams),
mcimadamore@1217 1965 types.createMethodTypeWithReturn(e.sym.type.asMethodType(), site));
mcimadamore@1217 1966 bestSoFar = selectBest(env, site, argtypes, typeargtypes,
mcimadamore@1217 1967 new MethodSymbol(e.sym.flags(), names.init, constrType, site.tsym),
mcimadamore@1217 1968 bestSoFar,
mcimadamore@1217 1969 allowBoxing,
mcimadamore@1217 1970 useVarargs,
mcimadamore@1217 1971 false);
mcimadamore@1217 1972 }
mcimadamore@1217 1973 }
mcimadamore@1217 1974 return bestSoFar;
mcimadamore@1217 1975 }
mcimadamore@1217 1976
duke@1 1977 /** Resolve constructor.
duke@1 1978 * @param pos The position to use for error reporting.
duke@1 1979 * @param env The environment current at the constructor invocation.
duke@1 1980 * @param site The type of class for which a constructor is searched.
duke@1 1981 * @param argtypes The types of the constructor invocation's value
duke@1 1982 * arguments.
duke@1 1983 * @param typeargtypes The types of the constructor invocation's type
duke@1 1984 * arguments.
duke@1 1985 * @param allowBoxing Allow boxing and varargs conversions.
duke@1 1986 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 1987 */
duke@1 1988 Symbol resolveConstructor(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 1989 Type site, List<Type> argtypes,
duke@1 1990 List<Type> typeargtypes,
duke@1 1991 boolean allowBoxing,
duke@1 1992 boolean useVarargs) {
mcimadamore@1215 1993 MethodResolutionContext prevResolutionContext = currentResolutionContext;
mcimadamore@1215 1994 try {
mcimadamore@1215 1995 currentResolutionContext = new MethodResolutionContext();
mcimadamore@1215 1996 return findConstructor(pos, env, site, argtypes, typeargtypes, allowBoxing, useVarargs);
mcimadamore@1215 1997 }
mcimadamore@1215 1998 finally {
mcimadamore@1215 1999 currentResolutionContext = prevResolutionContext;
mcimadamore@1215 2000 }
mcimadamore@1215 2001 }
mcimadamore@1215 2002
mcimadamore@1215 2003 Symbol findConstructor(DiagnosticPosition pos, Env<AttrContext> env,
mcimadamore@1215 2004 Type site, List<Type> argtypes,
mcimadamore@1215 2005 List<Type> typeargtypes,
mcimadamore@1215 2006 boolean allowBoxing,
mcimadamore@1215 2007 boolean useVarargs) {
duke@1 2008 Symbol sym = findMethod(env, site,
mcimadamore@1215 2009 names.init, argtypes,
mcimadamore@1215 2010 typeargtypes, allowBoxing,
mcimadamore@1215 2011 useVarargs, false);
mcimadamore@852 2012 chk.checkDeprecated(pos, env.info.scope.owner, sym);
duke@1 2013 return sym;
duke@1 2014 }
duke@1 2015
duke@1 2016 /** Resolve a constructor, throw a fatal error if not found.
duke@1 2017 * @param pos The position to use for error reporting.
duke@1 2018 * @param env The environment current at the method invocation.
duke@1 2019 * @param site The type to be constructed.
duke@1 2020 * @param argtypes The types of the invocation's value arguments.
duke@1 2021 * @param typeargtypes The types of the invocation's type arguments.
duke@1 2022 */
duke@1 2023 public MethodSymbol resolveInternalConstructor(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 2024 Type site,
duke@1 2025 List<Type> argtypes,
duke@1 2026 List<Type> typeargtypes) {
mcimadamore@1215 2027 MethodResolutionContext resolveContext = new MethodResolutionContext();
mcimadamore@1215 2028 resolveContext.internalResolution = true;
mcimadamore@1215 2029 Symbol sym = resolveConstructor(resolveContext, pos, env, site, argtypes, typeargtypes);
duke@1 2030 if (sym.kind == MTH) return (MethodSymbol)sym;
duke@1 2031 else throw new FatalError(
mcimadamore@89 2032 diags.fragment("fatal.err.cant.locate.ctor", site));
duke@1 2033 }
duke@1 2034
duke@1 2035 /** Resolve operator.
duke@1 2036 * @param pos The position to use for error reporting.
duke@1 2037 * @param optag The tag of the operation tree.
duke@1 2038 * @param env The environment current at the operation.
duke@1 2039 * @param argtypes The types of the operands.
duke@1 2040 */
jjg@1127 2041 Symbol resolveOperator(DiagnosticPosition pos, JCTree.Tag optag,
duke@1 2042 Env<AttrContext> env, List<Type> argtypes) {
mcimadamore@1215 2043 MethodResolutionContext prevResolutionContext = currentResolutionContext;
mcimadamore@1215 2044 try {
mcimadamore@1215 2045 currentResolutionContext = new MethodResolutionContext();
mcimadamore@1215 2046 Name name = treeinfo.operatorName(optag);
mcimadamore@1215 2047 Symbol sym = findMethod(env, syms.predefClass.type, name, argtypes,
mcimadamore@1215 2048 null, false, false, true);
mcimadamore@1215 2049 if (boxingEnabled && sym.kind >= WRONG_MTHS)
mcimadamore@1215 2050 sym = findMethod(env, syms.predefClass.type, name, argtypes,
mcimadamore@1215 2051 null, true, false, true);
mcimadamore@1215 2052 return access(sym, pos, env.enclClass.sym.type, name,
mcimadamore@1215 2053 false, argtypes, null);
mcimadamore@1215 2054 }
mcimadamore@1215 2055 finally {
mcimadamore@1215 2056 currentResolutionContext = prevResolutionContext;
mcimadamore@1215 2057 }
duke@1 2058 }
duke@1 2059
duke@1 2060 /** Resolve operator.
duke@1 2061 * @param pos The position to use for error reporting.
duke@1 2062 * @param optag The tag of the operation tree.
duke@1 2063 * @param env The environment current at the operation.
duke@1 2064 * @param arg The type of the operand.
duke@1 2065 */
jjg@1127 2066 Symbol resolveUnaryOperator(DiagnosticPosition pos, JCTree.Tag optag, Env<AttrContext> env, Type arg) {
duke@1 2067 return resolveOperator(pos, optag, env, List.of(arg));
duke@1 2068 }
duke@1 2069
duke@1 2070 /** Resolve binary operator.
duke@1 2071 * @param pos The position to use for error reporting.
duke@1 2072 * @param optag The tag of the operation tree.
duke@1 2073 * @param env The environment current at the operation.
duke@1 2074 * @param left The types of the left operand.
duke@1 2075 * @param right The types of the right operand.
duke@1 2076 */
duke@1 2077 Symbol resolveBinaryOperator(DiagnosticPosition pos,
jjg@1127 2078 JCTree.Tag optag,
duke@1 2079 Env<AttrContext> env,
duke@1 2080 Type left,
duke@1 2081 Type right) {
duke@1 2082 return resolveOperator(pos, optag, env, List.of(left, right));
duke@1 2083 }
duke@1 2084
duke@1 2085 /**
duke@1 2086 * Resolve `c.name' where name == this or name == super.
duke@1 2087 * @param pos The position to use for error reporting.
duke@1 2088 * @param env The environment current at the expression.
duke@1 2089 * @param c The qualifier.
duke@1 2090 * @param name The identifier's name.
duke@1 2091 */
duke@1 2092 Symbol resolveSelf(DiagnosticPosition pos,
duke@1 2093 Env<AttrContext> env,
duke@1 2094 TypeSymbol c,
duke@1 2095 Name name) {
duke@1 2096 Env<AttrContext> env1 = env;
duke@1 2097 boolean staticOnly = false;
duke@1 2098 while (env1.outer != null) {
duke@1 2099 if (isStatic(env1)) staticOnly = true;
duke@1 2100 if (env1.enclClass.sym == c) {
duke@1 2101 Symbol sym = env1.info.scope.lookup(name).sym;
duke@1 2102 if (sym != null) {
duke@1 2103 if (staticOnly) sym = new StaticError(sym);
duke@1 2104 return access(sym, pos, env.enclClass.sym.type,
duke@1 2105 name, true);
duke@1 2106 }
duke@1 2107 }
duke@1 2108 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
duke@1 2109 env1 = env1.outer;
duke@1 2110 }
duke@1 2111 log.error(pos, "not.encl.class", c);
duke@1 2112 return syms.errSymbol;
duke@1 2113 }
duke@1 2114
duke@1 2115 /**
duke@1 2116 * Resolve `c.this' for an enclosing class c that contains the
duke@1 2117 * named member.
duke@1 2118 * @param pos The position to use for error reporting.
duke@1 2119 * @param env The environment current at the expression.
duke@1 2120 * @param member The member that must be contained in the result.
duke@1 2121 */
duke@1 2122 Symbol resolveSelfContaining(DiagnosticPosition pos,
duke@1 2123 Env<AttrContext> env,
mcimadamore@901 2124 Symbol member,
mcimadamore@901 2125 boolean isSuperCall) {
duke@1 2126 Name name = names._this;
mcimadamore@901 2127 Env<AttrContext> env1 = isSuperCall ? env.outer : env;
duke@1 2128 boolean staticOnly = false;
mcimadamore@901 2129 if (env1 != null) {
mcimadamore@901 2130 while (env1 != null && env1.outer != null) {
mcimadamore@901 2131 if (isStatic(env1)) staticOnly = true;
mcimadamore@901 2132 if (env1.enclClass.sym.isSubClass(member.owner, types)) {
mcimadamore@901 2133 Symbol sym = env1.info.scope.lookup(name).sym;
mcimadamore@901 2134 if (sym != null) {
mcimadamore@901 2135 if (staticOnly) sym = new StaticError(sym);
mcimadamore@901 2136 return access(sym, pos, env.enclClass.sym.type,
mcimadamore@901 2137 name, true);
mcimadamore@901 2138 }
duke@1 2139 }
mcimadamore@901 2140 if ((env1.enclClass.sym.flags() & STATIC) != 0)
mcimadamore@901 2141 staticOnly = true;
mcimadamore@901 2142 env1 = env1.outer;
duke@1 2143 }
duke@1 2144 }
duke@1 2145 log.error(pos, "encl.class.required", member);
duke@1 2146 return syms.errSymbol;
duke@1 2147 }
duke@1 2148
duke@1 2149 /**
duke@1 2150 * Resolve an appropriate implicit this instance for t's container.
jjh@972 2151 * JLS 8.8.5.1 and 15.9.2
duke@1 2152 */
duke@1 2153 Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t) {
mcimadamore@901 2154 return resolveImplicitThis(pos, env, t, false);
mcimadamore@901 2155 }
mcimadamore@901 2156
mcimadamore@901 2157 Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t, boolean isSuperCall) {
duke@1 2158 Type thisType = (((t.tsym.owner.kind & (MTH|VAR)) != 0)
duke@1 2159 ? resolveSelf(pos, env, t.getEnclosingType().tsym, names._this)
mcimadamore@901 2160 : resolveSelfContaining(pos, env, t.tsym, isSuperCall)).type;
duke@1 2161 if (env.info.isSelfCall && thisType.tsym == env.enclClass.sym)
duke@1 2162 log.error(pos, "cant.ref.before.ctor.called", "this");
duke@1 2163 return thisType;
duke@1 2164 }
duke@1 2165
duke@1 2166 /* ***************************************************************************
duke@1 2167 * ResolveError classes, indicating error situations when accessing symbols
duke@1 2168 ****************************************************************************/
duke@1 2169
mcimadamore@1221 2170 //used by TransTypes when checking target type of synthetic cast
mcimadamore@1221 2171 public void logAccessErrorInternal(Env<AttrContext> env, JCTree tree, Type type) {
mcimadamore@1221 2172 AccessError error = new AccessError(env, env.enclClass.type, type.tsym);
mcimadamore@1221 2173 logResolveError(error, tree.pos(), env.enclClass.sym, env.enclClass.type, null, null, null);
mcimadamore@302 2174 }
mcimadamore@302 2175 //where
mcimadamore@302 2176 private void logResolveError(ResolveError error,
mcimadamore@302 2177 DiagnosticPosition pos,
mcimadamore@829 2178 Symbol location,
mcimadamore@302 2179 Type site,
mcimadamore@302 2180 Name name,
mcimadamore@302 2181 List<Type> argtypes,
mcimadamore@302 2182 List<Type> typeargtypes) {
mcimadamore@302 2183 JCDiagnostic d = error.getDiagnostic(JCDiagnostic.DiagnosticType.ERROR,
mcimadamore@829 2184 pos, location, site, name, argtypes, typeargtypes);
jjg@643 2185 if (d != null) {
jjg@643 2186 d.setFlag(DiagnosticFlag.RESOLVE_ERROR);
mcimadamore@302 2187 log.report(d);
jjg@643 2188 }
duke@1 2189 }
duke@1 2190
mcimadamore@161 2191 private final LocalizedString noArgs = new LocalizedString("compiler.misc.no.args");
mcimadamore@161 2192
mcimadamore@161 2193 public Object methodArguments(List<Type> argtypes) {
mcimadamore@1114 2194 return argtypes == null || argtypes.isEmpty() ? noArgs : argtypes;
mcimadamore@161 2195 }
mcimadamore@161 2196
mcimadamore@302 2197 /**
mcimadamore@302 2198 * Root class for resolution errors. Subclass of ResolveError
mcimadamore@302 2199 * represent a different kinds of resolution error - as such they must
mcimadamore@302 2200 * specify how they map into concrete compiler diagnostics.
duke@1 2201 */
mcimadamore@302 2202 private abstract class ResolveError extends Symbol {
duke@1 2203
mcimadamore@302 2204 /** The name of the kind of error, for debugging only. */
mcimadamore@302 2205 final String debugName;
mcimadamore@302 2206
mcimadamore@302 2207 ResolveError(int kind, String debugName) {
duke@1 2208 super(kind, 0, null, null, null);
duke@1 2209 this.debugName = debugName;
duke@1 2210 }
duke@1 2211
mcimadamore@302 2212 @Override
duke@1 2213 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
duke@1 2214 throw new AssertionError();
duke@1 2215 }
duke@1 2216
mcimadamore@302 2217 @Override
duke@1 2218 public String toString() {
mcimadamore@302 2219 return debugName;
duke@1 2220 }
duke@1 2221
mcimadamore@302 2222 @Override
mcimadamore@302 2223 public boolean exists() {
mcimadamore@302 2224 return false;
duke@1 2225 }
duke@1 2226
mcimadamore@302 2227 /**
mcimadamore@302 2228 * Create an external representation for this erroneous symbol to be
mcimadamore@302 2229 * used during attribution - by default this returns the symbol of a
mcimadamore@302 2230 * brand new error type which stores the original type found
mcimadamore@302 2231 * during resolution.
mcimadamore@302 2232 *
mcimadamore@302 2233 * @param name the name used during resolution
mcimadamore@302 2234 * @param location the location from which the symbol is accessed
duke@1 2235 */
mcimadamore@302 2236 protected Symbol access(Name name, TypeSymbol location) {
mcimadamore@302 2237 return types.createErrorType(name, location, syms.errSymbol.type).tsym;
duke@1 2238 }
duke@1 2239
mcimadamore@302 2240 /**
mcimadamore@302 2241 * Create a diagnostic representing this resolution error.
mcimadamore@302 2242 *
mcimadamore@302 2243 * @param dkind The kind of the diagnostic to be created (e.g error).
mcimadamore@302 2244 * @param pos The position to be used for error reporting.
mcimadamore@302 2245 * @param site The original type from where the selection took place.
mcimadamore@302 2246 * @param name The name of the symbol to be resolved.
mcimadamore@302 2247 * @param argtypes The invocation's value arguments,
mcimadamore@302 2248 * if we looked for a method.
mcimadamore@302 2249 * @param typeargtypes The invocation's type arguments,
mcimadamore@302 2250 * if we looked for a method.
mcimadamore@302 2251 */
mcimadamore@302 2252 abstract JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 2253 DiagnosticPosition pos,
mcimadamore@829 2254 Symbol location,
mcimadamore@302 2255 Type site,
mcimadamore@302 2256 Name name,
mcimadamore@302 2257 List<Type> argtypes,
mcimadamore@302 2258 List<Type> typeargtypes);
duke@1 2259
mcimadamore@302 2260 /**
mcimadamore@302 2261 * A name designates an operator if it consists
mcimadamore@302 2262 * of a non-empty sequence of operator symbols +-~!/*%&|^<>=
mcimadamore@80 2263 */
mcimadamore@80 2264 boolean isOperator(Name name) {
mcimadamore@80 2265 int i = 0;
jjg@113 2266 while (i < name.getByteLength() &&
jjg@113 2267 "+-~!*/%&|^<>=".indexOf(name.getByteAt(i)) >= 0) i++;
jjg@113 2268 return i > 0 && i == name.getByteLength();
mcimadamore@80 2269 }
duke@1 2270 }
duke@1 2271
mcimadamore@302 2272 /**
mcimadamore@302 2273 * This class is the root class of all resolution errors caused by
mcimadamore@302 2274 * an invalid symbol being found during resolution.
duke@1 2275 */
mcimadamore@302 2276 abstract class InvalidSymbolError extends ResolveError {
mcimadamore@302 2277
mcimadamore@302 2278 /** The invalid symbol found during resolution */
mcimadamore@302 2279 Symbol sym;
mcimadamore@302 2280
mcimadamore@302 2281 InvalidSymbolError(int kind, Symbol sym, String debugName) {
mcimadamore@302 2282 super(kind, debugName);
mcimadamore@302 2283 this.sym = sym;
mcimadamore@302 2284 }
mcimadamore@302 2285
mcimadamore@302 2286 @Override
mcimadamore@302 2287 public boolean exists() {
mcimadamore@302 2288 return true;
mcimadamore@302 2289 }
mcimadamore@302 2290
mcimadamore@302 2291 @Override
mcimadamore@302 2292 public String toString() {
mcimadamore@302 2293 return super.toString() + " wrongSym=" + sym;
mcimadamore@302 2294 }
mcimadamore@302 2295
mcimadamore@302 2296 @Override
mcimadamore@302 2297 public Symbol access(Name name, TypeSymbol location) {
mcimadamore@302 2298 if (sym.kind >= AMBIGUOUS)
mcimadamore@302 2299 return ((ResolveError)sym).access(name, location);
mcimadamore@302 2300 else if ((sym.kind & ERRONEOUS) == 0 && (sym.kind & TYP) != 0)
mcimadamore@302 2301 return types.createErrorType(name, location, sym.type).tsym;
mcimadamore@302 2302 else
mcimadamore@302 2303 return sym;
mcimadamore@302 2304 }
mcimadamore@302 2305 }
mcimadamore@302 2306
mcimadamore@302 2307 /**
mcimadamore@302 2308 * InvalidSymbolError error class indicating that a symbol matching a
mcimadamore@302 2309 * given name does not exists in a given site.
mcimadamore@302 2310 */
mcimadamore@302 2311 class SymbolNotFoundError extends ResolveError {
mcimadamore@302 2312
mcimadamore@302 2313 SymbolNotFoundError(int kind) {
mcimadamore@302 2314 super(kind, "symbol not found error");
mcimadamore@302 2315 }
mcimadamore@302 2316
mcimadamore@302 2317 @Override
mcimadamore@302 2318 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 2319 DiagnosticPosition pos,
mcimadamore@829 2320 Symbol location,
mcimadamore@302 2321 Type site,
mcimadamore@302 2322 Name name,
mcimadamore@302 2323 List<Type> argtypes,
mcimadamore@302 2324 List<Type> typeargtypes) {
mcimadamore@302 2325 argtypes = argtypes == null ? List.<Type>nil() : argtypes;
mcimadamore@302 2326 typeargtypes = typeargtypes == null ? List.<Type>nil() : typeargtypes;
mcimadamore@302 2327 if (name == names.error)
mcimadamore@302 2328 return null;
mcimadamore@302 2329
mcimadamore@302 2330 if (isOperator(name)) {
mcimadamore@829 2331 boolean isUnaryOp = argtypes.size() == 1;
mcimadamore@829 2332 String key = argtypes.size() == 1 ?
mcimadamore@829 2333 "operator.cant.be.applied" :
mcimadamore@829 2334 "operator.cant.be.applied.1";
mcimadamore@829 2335 Type first = argtypes.head;
mcimadamore@829 2336 Type second = !isUnaryOp ? argtypes.tail.head : null;
jjg@612 2337 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@829 2338 key, name, first, second);
mcimadamore@302 2339 }
mcimadamore@302 2340 boolean hasLocation = false;
mcimadamore@855 2341 if (location == null) {
mcimadamore@855 2342 location = site.tsym;
mcimadamore@855 2343 }
mcimadamore@829 2344 if (!location.name.isEmpty()) {
mcimadamore@829 2345 if (location.kind == PCK && !site.tsym.exists()) {
jjg@612 2346 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@829 2347 "doesnt.exist", location);
mcimadamore@302 2348 }
mcimadamore@829 2349 hasLocation = !location.name.equals(names._this) &&
mcimadamore@829 2350 !location.name.equals(names._super);
mcimadamore@302 2351 }
mcimadamore@302 2352 boolean isConstructor = kind == ABSENT_MTH &&
mcimadamore@302 2353 name == names.table.names.init;
mcimadamore@302 2354 KindName kindname = isConstructor ? KindName.CONSTRUCTOR : absentKind(kind);
mcimadamore@302 2355 Name idname = isConstructor ? site.tsym.name : name;
mcimadamore@302 2356 String errKey = getErrorKey(kindname, typeargtypes.nonEmpty(), hasLocation);
mcimadamore@302 2357 if (hasLocation) {
jjg@612 2358 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@302 2359 errKey, kindname, idname, //symbol kindname, name
mcimadamore@302 2360 typeargtypes, argtypes, //type parameters and arguments (if any)
mcimadamore@855 2361 getLocationDiag(location, site)); //location kindname, type
mcimadamore@302 2362 }
mcimadamore@302 2363 else {
jjg@612 2364 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@302 2365 errKey, kindname, idname, //symbol kindname, name
mcimadamore@302 2366 typeargtypes, argtypes); //type parameters and arguments (if any)
mcimadamore@302 2367 }
mcimadamore@302 2368 }
mcimadamore@302 2369 //where
mcimadamore@302 2370 private String getErrorKey(KindName kindname, boolean hasTypeArgs, boolean hasLocation) {
mcimadamore@302 2371 String key = "cant.resolve";
mcimadamore@302 2372 String suffix = hasLocation ? ".location" : "";
mcimadamore@302 2373 switch (kindname) {
mcimadamore@302 2374 case METHOD:
mcimadamore@302 2375 case CONSTRUCTOR: {
mcimadamore@302 2376 suffix += ".args";
mcimadamore@302 2377 suffix += hasTypeArgs ? ".params" : "";
mcimadamore@302 2378 }
mcimadamore@302 2379 }
mcimadamore@302 2380 return key + suffix;
mcimadamore@302 2381 }
mcimadamore@855 2382 private JCDiagnostic getLocationDiag(Symbol location, Type site) {
mcimadamore@855 2383 if (location.kind == VAR) {
mcimadamore@855 2384 return diags.fragment("location.1",
mcimadamore@829 2385 kindName(location),
mcimadamore@829 2386 location,
mcimadamore@855 2387 location.type);
mcimadamore@855 2388 } else {
mcimadamore@855 2389 return diags.fragment("location",
mcimadamore@855 2390 typeKindName(site),
mcimadamore@855 2391 site,
mcimadamore@855 2392 null);
mcimadamore@855 2393 }
mcimadamore@829 2394 }
mcimadamore@302 2395 }
mcimadamore@302 2396
mcimadamore@302 2397 /**
mcimadamore@302 2398 * InvalidSymbolError error class indicating that a given symbol
mcimadamore@302 2399 * (either a method, a constructor or an operand) is not applicable
mcimadamore@302 2400 * given an actual arguments/type argument list.
mcimadamore@302 2401 */
mcimadamore@1215 2402 class InapplicableSymbolError extends ResolveError {
mcimadamore@302 2403
mcimadamore@1215 2404 InapplicableSymbolError() {
mcimadamore@1215 2405 super(WRONG_MTH, "inapplicable symbol error");
mcimadamore@302 2406 }
mcimadamore@302 2407
mcimadamore@1215 2408 protected InapplicableSymbolError(int kind, String debugName) {
mcimadamore@1215 2409 super(kind, debugName);
mcimadamore@302 2410 }
mcimadamore@302 2411
mcimadamore@302 2412 @Override
mcimadamore@302 2413 public String toString() {
mcimadamore@1215 2414 return super.toString();
mcimadamore@1215 2415 }
mcimadamore@1215 2416
mcimadamore@1215 2417 @Override
mcimadamore@1215 2418 public boolean exists() {
mcimadamore@1215 2419 return true;
mcimadamore@302 2420 }
mcimadamore@302 2421
mcimadamore@302 2422 @Override
mcimadamore@302 2423 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 2424 DiagnosticPosition pos,
mcimadamore@829 2425 Symbol location,
mcimadamore@302 2426 Type site,
mcimadamore@302 2427 Name name,
mcimadamore@302 2428 List<Type> argtypes,
mcimadamore@302 2429 List<Type> typeargtypes) {
mcimadamore@302 2430 if (name == names.error)
mcimadamore@302 2431 return null;
mcimadamore@302 2432
mcimadamore@302 2433 if (isOperator(name)) {
mcimadamore@853 2434 boolean isUnaryOp = argtypes.size() == 1;
mcimadamore@853 2435 String key = argtypes.size() == 1 ?
mcimadamore@853 2436 "operator.cant.be.applied" :
mcimadamore@853 2437 "operator.cant.be.applied.1";
mcimadamore@853 2438 Type first = argtypes.head;
mcimadamore@853 2439 Type second = !isUnaryOp ? argtypes.tail.head : null;
mcimadamore@853 2440 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@853 2441 key, name, first, second);
mcimadamore@302 2442 }
mcimadamore@302 2443 else {
mcimadamore@1215 2444 Candidate c = errCandidate();
mcimadamore@1215 2445 Symbol ws = c.sym.asMemberOf(site, types);
jjg@612 2446 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@1215 2447 "cant.apply.symbol" + (c.details != null ? ".1" : ""),
mcimadamore@302 2448 kindName(ws),
mcimadamore@302 2449 ws.name == names.init ? ws.owner.name : ws.name,
mcimadamore@302 2450 methodArguments(ws.type.getParameterTypes()),
mcimadamore@302 2451 methodArguments(argtypes),
mcimadamore@302 2452 kindName(ws.owner),
mcimadamore@302 2453 ws.owner.type,
mcimadamore@1215 2454 c.details);
mcimadamore@302 2455 }
mcimadamore@302 2456 }
mcimadamore@302 2457
mcimadamore@302 2458 @Override
mcimadamore@302 2459 public Symbol access(Name name, TypeSymbol location) {
mcimadamore@302 2460 return types.createErrorType(name, location, syms.errSymbol.type).tsym;
mcimadamore@302 2461 }
mcimadamore@1215 2462
mcimadamore@1215 2463 protected boolean shouldReport(Candidate c) {
mcimadamore@1215 2464 return !c.isApplicable() &&
mcimadamore@1215 2465 (((c.sym.flags() & VARARGS) != 0 && c.step == VARARITY) ||
mcimadamore@1215 2466 (c.sym.flags() & VARARGS) == 0 && c.step == (boxingEnabled ? BOX : BASIC));
mcimadamore@1215 2467 }
mcimadamore@1215 2468
mcimadamore@1215 2469 private Candidate errCandidate() {
mcimadamore@1215 2470 for (Candidate c : currentResolutionContext.candidates) {
mcimadamore@1215 2471 if (shouldReport(c)) {
mcimadamore@1215 2472 return c;
mcimadamore@1215 2473 }
mcimadamore@1215 2474 }
mcimadamore@1215 2475 Assert.error();
mcimadamore@1215 2476 return null;
mcimadamore@1215 2477 }
mcimadamore@302 2478 }
mcimadamore@302 2479
mcimadamore@302 2480 /**
mcimadamore@302 2481 * ResolveError error class indicating that a set of symbols
mcimadamore@302 2482 * (either methods, constructors or operands) is not applicable
mcimadamore@302 2483 * given an actual arguments/type argument list.
mcimadamore@302 2484 */
mcimadamore@1215 2485 class InapplicableSymbolsError extends InapplicableSymbolError {
mcimadamore@689 2486
mcimadamore@1215 2487 InapplicableSymbolsError() {
mcimadamore@302 2488 super(WRONG_MTHS, "inapplicable symbols");
mcimadamore@302 2489 }
mcimadamore@302 2490
mcimadamore@302 2491 @Override
mcimadamore@302 2492 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 2493 DiagnosticPosition pos,
mcimadamore@829 2494 Symbol location,
mcimadamore@302 2495 Type site,
mcimadamore@302 2496 Name name,
mcimadamore@302 2497 List<Type> argtypes,
mcimadamore@302 2498 List<Type> typeargtypes) {
mcimadamore@1215 2499 if (currentResolutionContext.candidates.nonEmpty()) {
mcimadamore@689 2500 JCDiagnostic err = diags.create(dkind,
mcimadamore@689 2501 log.currentSource(),
mcimadamore@689 2502 pos,
mcimadamore@689 2503 "cant.apply.symbols",
mcimadamore@689 2504 name == names.init ? KindName.CONSTRUCTOR : absentKind(kind),
mcimadamore@689 2505 getName(),
mcimadamore@689 2506 argtypes);
mcimadamore@689 2507 return new JCDiagnostic.MultilineDiagnostic(err, candidateDetails(site));
mcimadamore@689 2508 } else {
mcimadamore@689 2509 return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind, pos,
mcimadamore@829 2510 location, site, name, argtypes, typeargtypes);
mcimadamore@689 2511 }
mcimadamore@689 2512 }
mcimadamore@689 2513
mcimadamore@689 2514 //where
mcimadamore@689 2515 List<JCDiagnostic> candidateDetails(Type site) {
mcimadamore@689 2516 List<JCDiagnostic> details = List.nil();
mcimadamore@1215 2517 for (Candidate c : currentResolutionContext.candidates) {
mcimadamore@1215 2518 if (!shouldReport(c)) continue;
mcimadamore@1215 2519 JCDiagnostic detailDiag = diags.fragment("inapplicable.method",
mcimadamore@1215 2520 Kinds.kindName(c.sym),
mcimadamore@1215 2521 c.sym.location(site, types),
mcimadamore@1215 2522 c.sym.asMemberOf(site, types),
mcimadamore@1215 2523 c.details);
mcimadamore@1215 2524 details = details.prepend(detailDiag);
mcimadamore@1215 2525 }
mcimadamore@689 2526 return details.reverse();
mcimadamore@689 2527 }
mcimadamore@689 2528
mcimadamore@689 2529 private Name getName() {
mcimadamore@1215 2530 Symbol sym = currentResolutionContext.candidates.head.sym;
mcimadamore@689 2531 return sym.name == names.init ?
mcimadamore@689 2532 sym.owner.name :
mcimadamore@689 2533 sym.name;
mcimadamore@689 2534 }
mcimadamore@302 2535 }
mcimadamore@302 2536
mcimadamore@302 2537 /**
mcimadamore@302 2538 * An InvalidSymbolError error class indicating that a symbol is not
mcimadamore@302 2539 * accessible from a given site
mcimadamore@302 2540 */
mcimadamore@302 2541 class AccessError extends InvalidSymbolError {
mcimadamore@302 2542
mcimadamore@302 2543 private Env<AttrContext> env;
mcimadamore@302 2544 private Type site;
duke@1 2545
duke@1 2546 AccessError(Symbol sym) {
duke@1 2547 this(null, null, sym);
duke@1 2548 }
duke@1 2549
duke@1 2550 AccessError(Env<AttrContext> env, Type site, Symbol sym) {
duke@1 2551 super(HIDDEN, sym, "access error");
duke@1 2552 this.env = env;
duke@1 2553 this.site = site;
duke@1 2554 if (debugResolve)
duke@1 2555 log.error("proc.messager", sym + " @ " + site + " is inaccessible.");
duke@1 2556 }
duke@1 2557
mcimadamore@302 2558 @Override
mcimadamore@302 2559 public boolean exists() {
mcimadamore@302 2560 return false;
mcimadamore@302 2561 }
duke@1 2562
mcimadamore@302 2563 @Override
mcimadamore@302 2564 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 2565 DiagnosticPosition pos,
mcimadamore@829 2566 Symbol location,
mcimadamore@302 2567 Type site,
mcimadamore@302 2568 Name name,
mcimadamore@302 2569 List<Type> argtypes,
mcimadamore@302 2570 List<Type> typeargtypes) {
mcimadamore@302 2571 if (sym.owner.type.tag == ERROR)
mcimadamore@302 2572 return null;
mcimadamore@302 2573
mcimadamore@302 2574 if (sym.name == names.init && sym.owner != site.tsym) {
mcimadamore@302 2575 return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind,
mcimadamore@829 2576 pos, location, site, name, argtypes, typeargtypes);
mcimadamore@302 2577 }
mcimadamore@302 2578 else if ((sym.flags() & PUBLIC) != 0
mcimadamore@302 2579 || (env != null && this.site != null
mcimadamore@302 2580 && !isAccessible(env, this.site))) {
jjg@612 2581 return diags.create(dkind, log.currentSource(),
mcimadamore@302 2582 pos, "not.def.access.class.intf.cant.access",
mcimadamore@302 2583 sym, sym.location());
mcimadamore@302 2584 }
mcimadamore@302 2585 else if ((sym.flags() & (PRIVATE | PROTECTED)) != 0) {
jjg@612 2586 return diags.create(dkind, log.currentSource(),
mcimadamore@302 2587 pos, "report.access", sym,
mcimadamore@302 2588 asFlagSet(sym.flags() & (PRIVATE | PROTECTED)),
mcimadamore@302 2589 sym.location());
mcimadamore@302 2590 }
mcimadamore@302 2591 else {
jjg@612 2592 return diags.create(dkind, log.currentSource(),
mcimadamore@302 2593 pos, "not.def.public.cant.access", sym, sym.location());
duke@1 2594 }
duke@1 2595 }
duke@1 2596 }
duke@1 2597
mcimadamore@302 2598 /**
mcimadamore@302 2599 * InvalidSymbolError error class indicating that an instance member
mcimadamore@302 2600 * has erroneously been accessed from a static context.
duke@1 2601 */
mcimadamore@302 2602 class StaticError extends InvalidSymbolError {
mcimadamore@302 2603
duke@1 2604 StaticError(Symbol sym) {
duke@1 2605 super(STATICERR, sym, "static error");
duke@1 2606 }
duke@1 2607
mcimadamore@302 2608 @Override
mcimadamore@302 2609 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 2610 DiagnosticPosition pos,
mcimadamore@829 2611 Symbol location,
mcimadamore@302 2612 Type site,
mcimadamore@302 2613 Name name,
mcimadamore@302 2614 List<Type> argtypes,
mcimadamore@302 2615 List<Type> typeargtypes) {
mcimadamore@80 2616 Symbol errSym = ((sym.kind == TYP && sym.type.tag == CLASS)
mcimadamore@80 2617 ? types.erasure(sym.type).tsym
mcimadamore@80 2618 : sym);
jjg@612 2619 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@302 2620 "non-static.cant.be.ref", kindName(sym), errSym);
duke@1 2621 }
duke@1 2622 }
duke@1 2623
mcimadamore@302 2624 /**
mcimadamore@302 2625 * InvalidSymbolError error class indicating that a pair of symbols
mcimadamore@302 2626 * (either methods, constructors or operands) are ambiguous
mcimadamore@302 2627 * given an actual arguments/type argument list.
duke@1 2628 */
mcimadamore@302 2629 class AmbiguityError extends InvalidSymbolError {
mcimadamore@302 2630
mcimadamore@302 2631 /** The other maximally specific symbol */
duke@1 2632 Symbol sym2;
duke@1 2633
duke@1 2634 AmbiguityError(Symbol sym1, Symbol sym2) {
duke@1 2635 super(AMBIGUOUS, sym1, "ambiguity error");
duke@1 2636 this.sym2 = sym2;
duke@1 2637 }
duke@1 2638
mcimadamore@302 2639 @Override
mcimadamore@302 2640 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 2641 DiagnosticPosition pos,
mcimadamore@829 2642 Symbol location,
mcimadamore@302 2643 Type site,
mcimadamore@302 2644 Name name,
mcimadamore@302 2645 List<Type> argtypes,
mcimadamore@302 2646 List<Type> typeargtypes) {
duke@1 2647 AmbiguityError pair = this;
duke@1 2648 while (true) {
mcimadamore@302 2649 if (pair.sym.kind == AMBIGUOUS)
mcimadamore@302 2650 pair = (AmbiguityError)pair.sym;
duke@1 2651 else if (pair.sym2.kind == AMBIGUOUS)
duke@1 2652 pair = (AmbiguityError)pair.sym2;
duke@1 2653 else break;
duke@1 2654 }
mcimadamore@302 2655 Name sname = pair.sym.name;
mcimadamore@302 2656 if (sname == names.init) sname = pair.sym.owner.name;
jjg@612 2657 return diags.create(dkind, log.currentSource(),
mcimadamore@302 2658 pos, "ref.ambiguous", sname,
mcimadamore@302 2659 kindName(pair.sym),
mcimadamore@302 2660 pair.sym,
mcimadamore@302 2661 pair.sym.location(site, types),
duke@1 2662 kindName(pair.sym2),
duke@1 2663 pair.sym2,
duke@1 2664 pair.sym2.location(site, types));
duke@1 2665 }
duke@1 2666 }
mcimadamore@160 2667
mcimadamore@160 2668 enum MethodResolutionPhase {
mcimadamore@160 2669 BASIC(false, false),
mcimadamore@160 2670 BOX(true, false),
mcimadamore@160 2671 VARARITY(true, true);
mcimadamore@160 2672
mcimadamore@160 2673 boolean isBoxingRequired;
mcimadamore@160 2674 boolean isVarargsRequired;
mcimadamore@160 2675
mcimadamore@160 2676 MethodResolutionPhase(boolean isBoxingRequired, boolean isVarargsRequired) {
mcimadamore@160 2677 this.isBoxingRequired = isBoxingRequired;
mcimadamore@160 2678 this.isVarargsRequired = isVarargsRequired;
mcimadamore@160 2679 }
mcimadamore@160 2680
mcimadamore@160 2681 public boolean isBoxingRequired() {
mcimadamore@160 2682 return isBoxingRequired;
mcimadamore@160 2683 }
mcimadamore@160 2684
mcimadamore@160 2685 public boolean isVarargsRequired() {
mcimadamore@160 2686 return isVarargsRequired;
mcimadamore@160 2687 }
mcimadamore@160 2688
mcimadamore@160 2689 public boolean isApplicable(boolean boxingEnabled, boolean varargsEnabled) {
mcimadamore@160 2690 return (varargsEnabled || !isVarargsRequired) &&
mcimadamore@160 2691 (boxingEnabled || !isBoxingRequired);
mcimadamore@160 2692 }
mcimadamore@160 2693 }
mcimadamore@160 2694
mcimadamore@160 2695 final List<MethodResolutionPhase> methodResolutionSteps = List.of(BASIC, BOX, VARARITY);
mcimadamore@160 2696
mcimadamore@1215 2697 /**
mcimadamore@1215 2698 * A resolution context is used to keep track of intermediate results of
mcimadamore@1215 2699 * overload resolution, such as list of method that are not applicable
mcimadamore@1215 2700 * (used to generate more precise diagnostics) and so on. Resolution contexts
mcimadamore@1215 2701 * can be nested - this means that when each overload resolution routine should
mcimadamore@1215 2702 * work within the resolution context it created.
mcimadamore@1215 2703 */
mcimadamore@1215 2704 class MethodResolutionContext {
mcimadamore@689 2705
mcimadamore@1215 2706 private List<Candidate> candidates = List.nil();
mcimadamore@1114 2707
mcimadamore@1215 2708 private Map<MethodResolutionPhase, Symbol> resolutionCache =
mcimadamore@1215 2709 new EnumMap<MethodResolutionPhase, Symbol>(MethodResolutionPhase.class);
mcimadamore@1215 2710
mcimadamore@1215 2711 private MethodResolutionPhase step = null;
mcimadamore@1215 2712
mcimadamore@1215 2713 private boolean internalResolution = false;
mcimadamore@1215 2714
mcimadamore@1215 2715 private MethodResolutionPhase firstErroneousResolutionPhase() {
mcimadamore@1215 2716 MethodResolutionPhase bestSoFar = BASIC;
mcimadamore@1215 2717 Symbol sym = methodNotFound;
mcimadamore@1215 2718 List<MethodResolutionPhase> steps = methodResolutionSteps;
mcimadamore@1215 2719 while (steps.nonEmpty() &&
mcimadamore@1215 2720 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@1215 2721 sym.kind >= WRONG_MTHS) {
mcimadamore@1215 2722 sym = resolutionCache.get(steps.head);
mcimadamore@1215 2723 bestSoFar = steps.head;
mcimadamore@1215 2724 steps = steps.tail;
mcimadamore@1215 2725 }
mcimadamore@1215 2726 return bestSoFar;
mcimadamore@160 2727 }
mcimadamore@1215 2728
mcimadamore@1215 2729 void addInapplicableCandidate(Symbol sym, JCDiagnostic details) {
mcimadamore@1215 2730 Candidate c = new Candidate(currentResolutionContext.step, sym, details, null);
mcimadamore@1215 2731 if (!candidates.contains(c))
mcimadamore@1215 2732 candidates = candidates.append(c);
mcimadamore@1215 2733 }
mcimadamore@1215 2734
mcimadamore@1215 2735 void addApplicableCandidate(Symbol sym, Type mtype) {
mcimadamore@1215 2736 Candidate c = new Candidate(currentResolutionContext.step, sym, null, mtype);
mcimadamore@1215 2737 candidates = candidates.append(c);
mcimadamore@1215 2738 }
mcimadamore@1215 2739
mcimadamore@1215 2740 /**
mcimadamore@1215 2741 * This class represents an overload resolution candidate. There are two
mcimadamore@1215 2742 * kinds of candidates: applicable methods and inapplicable methods;
mcimadamore@1215 2743 * applicable methods have a pointer to the instantiated method type,
mcimadamore@1215 2744 * while inapplicable candidates contain further details about the
mcimadamore@1215 2745 * reason why the method has been considered inapplicable.
mcimadamore@1215 2746 */
mcimadamore@1215 2747 class Candidate {
mcimadamore@1215 2748
mcimadamore@1215 2749 final MethodResolutionPhase step;
mcimadamore@1215 2750 final Symbol sym;
mcimadamore@1215 2751 final JCDiagnostic details;
mcimadamore@1215 2752 final Type mtype;
mcimadamore@1215 2753
mcimadamore@1215 2754 private Candidate(MethodResolutionPhase step, Symbol sym, JCDiagnostic details, Type mtype) {
mcimadamore@1215 2755 this.step = step;
mcimadamore@1215 2756 this.sym = sym;
mcimadamore@1215 2757 this.details = details;
mcimadamore@1215 2758 this.mtype = mtype;
mcimadamore@1215 2759 }
mcimadamore@1215 2760
mcimadamore@1215 2761 @Override
mcimadamore@1215 2762 public boolean equals(Object o) {
mcimadamore@1215 2763 if (o instanceof Candidate) {
mcimadamore@1215 2764 Symbol s1 = this.sym;
mcimadamore@1215 2765 Symbol s2 = ((Candidate)o).sym;
mcimadamore@1215 2766 if ((s1 != s2 &&
mcimadamore@1215 2767 (s1.overrides(s2, s1.owner.type.tsym, types, false) ||
mcimadamore@1215 2768 (s2.overrides(s1, s2.owner.type.tsym, types, false)))) ||
mcimadamore@1215 2769 ((s1.isConstructor() || s2.isConstructor()) && s1.owner != s2.owner))
mcimadamore@1215 2770 return true;
mcimadamore@1215 2771 }
mcimadamore@1215 2772 return false;
mcimadamore@1215 2773 }
mcimadamore@1215 2774
mcimadamore@1215 2775 boolean isApplicable() {
mcimadamore@1215 2776 return mtype != null;
mcimadamore@1215 2777 }
mcimadamore@1215 2778 }
mcimadamore@160 2779 }
mcimadamore@1215 2780
mcimadamore@1215 2781 MethodResolutionContext currentResolutionContext = null;
duke@1 2782 }

mercurial