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