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