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