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

Thu, 31 May 2012 17:42:14 +0100

author
mcimadamore
date
Thu, 31 May 2012 17:42:14 +0100
changeset 1268
af6a4c24f4e3
parent 1239
2827076dbf64
child 1296
cddc2c894cc6
permissions
-rw-r--r--

7166552: Inference: cleanup usage of Type.ForAll
Summary: Remove hack to callback into type-inference from assignment context
Reviewed-by: dlsmith, jjg

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

mercurial