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

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

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

7133185: Update 292 overload resolution logic to match JLS
Summary: Re-implement special overload resolution support for method handles according to the JLS SE 7 definition
Reviewed-by: jjg, dlsmith, jrose

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

mercurial