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

Wed, 16 Jan 2013 17:40:28 +0000

author
mcimadamore
date
Wed, 16 Jan 2013 17:40:28 +0000
changeset 1498
1afdf1f1472b
parent 1496
f785dcac17b7
child 1510
7873d37f5b37
permissions
-rw-r--r--

8005964: Regression: difference in error recovery after ambiguity causes JCK test failure
Summary: Wrong implementation of ResolveError.access in AmbiguityError
Reviewed-by: jjh

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

mercurial