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