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