Tue, 25 Sep 2012 11:55:34 +0100
7175433: Inference cleanup: add helper class to handle inference variables
Summary: Add class to handle inference variables instantiation and associated info
Reviewed-by: jjg, dlsmith
duke@1 | 1 | /* |
mcimadamore@1186 | 2 | * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.javac.comp; |
duke@1 | 27 | |
mcimadamore@1114 | 28 | import com.sun.tools.javac.api.Formattable.LocalizedString; |
duke@1 | 29 | import com.sun.tools.javac.code.*; |
mcimadamore@1114 | 30 | import com.sun.tools.javac.code.Type.*; |
mcimadamore@1114 | 31 | import com.sun.tools.javac.code.Symbol.*; |
mcimadamore@1238 | 32 | import com.sun.tools.javac.comp.Attr.ResultInfo; |
mcimadamore@1238 | 33 | import com.sun.tools.javac.comp.Check.CheckContext; |
mcimadamore@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@1114 | 51 | import java.util.Map; |
mcimadamore@1114 | 52 | import java.util.Set; |
mcimadamore@1114 | 53 | |
mcimadamore@1114 | 54 | import javax.lang.model.element.ElementVisitor; |
duke@1 | 55 | |
duke@1 | 56 | import static com.sun.tools.javac.code.Flags.*; |
jjg@1127 | 57 | import static com.sun.tools.javac.code.Flags.BLOCK; |
duke@1 | 58 | import static com.sun.tools.javac.code.Kinds.*; |
jjg@1127 | 59 | import static com.sun.tools.javac.code.Kinds.ERRONEOUS; |
duke@1 | 60 | import static com.sun.tools.javac.code.TypeTags.*; |
mcimadamore@1114 | 61 | import static com.sun.tools.javac.comp.Resolve.MethodResolutionPhase.*; |
jjg@1127 | 62 | import static com.sun.tools.javac.tree.JCTree.Tag.*; |
mcimadamore@1335 | 63 | import java.util.Iterator; |
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@1238 | 740 | return super.check(pos, chk.checkNonVoid(pos, types.capture(types.upperBound(found)))); |
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@1335 | 1203 | abstractOk &= excludeAbstractsFilter.accepts(s); |
mcimadamore@1335 | 1204 | if (abstractOk) { |
mcimadamore@1335 | 1205 | for (Type itype : types.interfaces(s.type)) { |
mcimadamore@1335 | 1206 | itypes = types.union(types.closure(itype), itypes); |
duke@1 | 1207 | } |
duke@1 | 1208 | } |
mcimadamore@1335 | 1209 | if (name == names.init) break; |
mcimadamore@1335 | 1210 | } |
mcimadamore@1335 | 1211 | |
mcimadamore@1335 | 1212 | Symbol concrete = bestSoFar.kind < ERR && |
mcimadamore@1335 | 1213 | (bestSoFar.flags() & ABSTRACT) == 0 ? |
mcimadamore@1335 | 1214 | bestSoFar : methodNotFound; |
mcimadamore@1335 | 1215 | |
mcimadamore@1335 | 1216 | if (name != names.init) { |
mcimadamore@1335 | 1217 | //keep searching for abstract methods |
mcimadamore@1335 | 1218 | for (Type itype : itypes) { |
mcimadamore@1335 | 1219 | if (!itype.isInterface()) continue; //skip j.l.Object (included by Types.closure()) |
mcimadamore@1335 | 1220 | bestSoFar = lookupMethod(env, site, name, argtypes, typeargtypes, |
mcimadamore@1335 | 1221 | itype.tsym.members(), bestSoFar, allowBoxing, useVarargs, operator, true); |
mcimadamore@1335 | 1222 | if (concrete != bestSoFar && |
mcimadamore@1335 | 1223 | concrete.kind < ERR && bestSoFar.kind < ERR && |
mcimadamore@1335 | 1224 | types.isSubSignature(concrete.type, bestSoFar.type)) { |
mcimadamore@1335 | 1225 | //this is an hack - as javac does not do full membership checks |
mcimadamore@1335 | 1226 | //most specific ends up comparing abstract methods that might have |
mcimadamore@1335 | 1227 | //been implemented by some concrete method in a subclass and, |
mcimadamore@1335 | 1228 | //because of raw override, it is possible for an abstract method |
mcimadamore@1335 | 1229 | //to be more specific than the concrete method - so we need |
mcimadamore@1335 | 1230 | //to explicitly call that out (see CR 6178365) |
mcimadamore@1335 | 1231 | bestSoFar = concrete; |
mcimadamore@1335 | 1232 | } |
duke@1 | 1233 | } |
duke@1 | 1234 | } |
duke@1 | 1235 | return bestSoFar; |
duke@1 | 1236 | } |
duke@1 | 1237 | |
mcimadamore@1335 | 1238 | /** |
mcimadamore@1335 | 1239 | * Return an Iterable object to scan the superclasses of a given type. |
mcimadamore@1335 | 1240 | * It's crucial that the scan is done lazily, as we don't want to accidentally |
mcimadamore@1335 | 1241 | * access more supertypes than strictly needed (as this could trigger completion |
mcimadamore@1335 | 1242 | * errors if some of the not-needed supertypes are missing/ill-formed). |
mcimadamore@1335 | 1243 | */ |
mcimadamore@1335 | 1244 | Iterable<TypeSymbol> superclasses(final Type intype) { |
mcimadamore@1335 | 1245 | return new Iterable<TypeSymbol>() { |
mcimadamore@1335 | 1246 | public Iterator<TypeSymbol> iterator() { |
mcimadamore@1335 | 1247 | return new Iterator<TypeSymbol>() { |
mcimadamore@1335 | 1248 | |
mcimadamore@1335 | 1249 | List<TypeSymbol> seen = List.nil(); |
mcimadamore@1335 | 1250 | TypeSymbol currentSym = getSymbol(intype); |
mcimadamore@1335 | 1251 | |
mcimadamore@1335 | 1252 | public boolean hasNext() { |
mcimadamore@1335 | 1253 | return currentSym != null; |
mcimadamore@1335 | 1254 | } |
mcimadamore@1335 | 1255 | |
mcimadamore@1335 | 1256 | public TypeSymbol next() { |
mcimadamore@1335 | 1257 | TypeSymbol prevSym = currentSym; |
mcimadamore@1335 | 1258 | currentSym = getSymbol(types.supertype(currentSym.type)); |
mcimadamore@1335 | 1259 | return prevSym; |
mcimadamore@1335 | 1260 | } |
mcimadamore@1335 | 1261 | |
mcimadamore@1335 | 1262 | public void remove() { |
mcimadamore@1335 | 1263 | throw new UnsupportedOperationException("Not supported yet."); |
mcimadamore@1335 | 1264 | } |
mcimadamore@1335 | 1265 | |
mcimadamore@1335 | 1266 | TypeSymbol getSymbol(Type intype) { |
mcimadamore@1335 | 1267 | if (intype.tag != CLASS && |
mcimadamore@1335 | 1268 | intype.tag != TYPEVAR) { |
mcimadamore@1335 | 1269 | return null; |
mcimadamore@1335 | 1270 | } |
mcimadamore@1335 | 1271 | while (intype.tag == TYPEVAR) |
mcimadamore@1335 | 1272 | intype = intype.getUpperBound(); |
mcimadamore@1335 | 1273 | if (seen.contains(intype.tsym)) { |
mcimadamore@1335 | 1274 | //degenerate case in which we have a circular |
mcimadamore@1335 | 1275 | //class hierarchy - because of ill-formed classfiles |
mcimadamore@1335 | 1276 | return null; |
mcimadamore@1335 | 1277 | } |
mcimadamore@1335 | 1278 | seen = seen.prepend(intype.tsym); |
mcimadamore@1335 | 1279 | return intype.tsym; |
mcimadamore@1335 | 1280 | } |
mcimadamore@1335 | 1281 | }; |
mcimadamore@1335 | 1282 | } |
mcimadamore@1335 | 1283 | }; |
mcimadamore@1335 | 1284 | } |
mcimadamore@1335 | 1285 | |
mcimadamore@1335 | 1286 | /** |
mcimadamore@1335 | 1287 | * We should not look for abstract methods if receiver is a concrete class |
mcimadamore@1335 | 1288 | * (as concrete classes are expected to implement all abstracts coming |
mcimadamore@1335 | 1289 | * from superinterfaces) |
mcimadamore@1335 | 1290 | */ |
mcimadamore@1335 | 1291 | Filter<Symbol> excludeAbstractsFilter = new Filter<Symbol>() { |
mcimadamore@1335 | 1292 | public boolean accepts(Symbol s) { |
mcimadamore@1335 | 1293 | return (s.flags() & (ABSTRACT | INTERFACE | ENUM)) != 0; |
mcimadamore@1335 | 1294 | } |
mcimadamore@1335 | 1295 | }; |
mcimadamore@1335 | 1296 | |
mcimadamore@1335 | 1297 | /** |
mcimadamore@1335 | 1298 | * Lookup a method with given name and argument types in a given scope |
mcimadamore@1335 | 1299 | */ |
mcimadamore@1335 | 1300 | Symbol lookupMethod(Env<AttrContext> env, |
mcimadamore@1335 | 1301 | Type site, |
mcimadamore@1335 | 1302 | Name name, |
mcimadamore@1335 | 1303 | List<Type> argtypes, |
mcimadamore@1335 | 1304 | List<Type> typeargtypes, |
mcimadamore@1335 | 1305 | Scope sc, |
mcimadamore@1335 | 1306 | Symbol bestSoFar, |
mcimadamore@1335 | 1307 | boolean allowBoxing, |
mcimadamore@1335 | 1308 | boolean useVarargs, |
mcimadamore@1335 | 1309 | boolean operator, |
mcimadamore@1335 | 1310 | boolean abstractok) { |
mcimadamore@1335 | 1311 | for (Symbol s : sc.getElementsByName(name, lookupFilter)) { |
mcimadamore@1335 | 1312 | bestSoFar = selectBest(env, site, argtypes, typeargtypes, s, |
mcimadamore@1335 | 1313 | bestSoFar, allowBoxing, useVarargs, operator); |
mcimadamore@1335 | 1314 | } |
mcimadamore@1335 | 1315 | return bestSoFar; |
mcimadamore@1335 | 1316 | } |
mcimadamore@1335 | 1317 | //where |
mcimadamore@1335 | 1318 | Filter<Symbol> lookupFilter = new Filter<Symbol>() { |
mcimadamore@1335 | 1319 | public boolean accepts(Symbol s) { |
mcimadamore@1335 | 1320 | return s.kind == MTH && |
mcimadamore@1335 | 1321 | (s.flags() & SYNTHETIC) == 0; |
mcimadamore@1335 | 1322 | } |
mcimadamore@1335 | 1323 | }; |
mcimadamore@1335 | 1324 | |
duke@1 | 1325 | /** Find unqualified method matching given name, type and value arguments. |
duke@1 | 1326 | * @param env The current environment. |
duke@1 | 1327 | * @param name The method's name. |
duke@1 | 1328 | * @param argtypes The method's value arguments. |
duke@1 | 1329 | * @param typeargtypes The method's type arguments. |
duke@1 | 1330 | * @param allowBoxing Allow boxing conversions of arguments. |
duke@1 | 1331 | * @param useVarargs Box trailing arguments into an array for varargs. |
duke@1 | 1332 | */ |
duke@1 | 1333 | Symbol findFun(Env<AttrContext> env, Name name, |
duke@1 | 1334 | List<Type> argtypes, List<Type> typeargtypes, |
duke@1 | 1335 | boolean allowBoxing, boolean useVarargs) { |
duke@1 | 1336 | Symbol bestSoFar = methodNotFound; |
duke@1 | 1337 | Symbol sym; |
duke@1 | 1338 | Env<AttrContext> env1 = env; |
duke@1 | 1339 | boolean staticOnly = false; |
duke@1 | 1340 | while (env1.outer != null) { |
duke@1 | 1341 | if (isStatic(env1)) staticOnly = true; |
duke@1 | 1342 | sym = findMethod( |
duke@1 | 1343 | env1, env1.enclClass.sym.type, name, argtypes, typeargtypes, |
duke@1 | 1344 | allowBoxing, useVarargs, false); |
duke@1 | 1345 | if (sym.exists()) { |
duke@1 | 1346 | if (staticOnly && |
duke@1 | 1347 | sym.kind == MTH && |
duke@1 | 1348 | sym.owner.kind == TYP && |
duke@1 | 1349 | (sym.flags() & STATIC) == 0) return new StaticError(sym); |
duke@1 | 1350 | else return sym; |
duke@1 | 1351 | } else if (sym.kind < bestSoFar.kind) { |
duke@1 | 1352 | bestSoFar = sym; |
duke@1 | 1353 | } |
duke@1 | 1354 | if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true; |
duke@1 | 1355 | env1 = env1.outer; |
duke@1 | 1356 | } |
duke@1 | 1357 | |
duke@1 | 1358 | sym = findMethod(env, syms.predefClass.type, name, argtypes, |
duke@1 | 1359 | typeargtypes, allowBoxing, useVarargs, false); |
duke@1 | 1360 | if (sym.exists()) |
duke@1 | 1361 | return sym; |
duke@1 | 1362 | |
duke@1 | 1363 | Scope.Entry e = env.toplevel.namedImportScope.lookup(name); |
duke@1 | 1364 | for (; e.scope != null; e = e.next()) { |
duke@1 | 1365 | sym = e.sym; |
duke@1 | 1366 | Type origin = e.getOrigin().owner.type; |
duke@1 | 1367 | if (sym.kind == MTH) { |
duke@1 | 1368 | if (e.sym.owner.type != origin) |
duke@1 | 1369 | sym = sym.clone(e.getOrigin().owner); |
duke@1 | 1370 | if (!isAccessible(env, origin, sym)) |
duke@1 | 1371 | sym = new AccessError(env, origin, sym); |
duke@1 | 1372 | bestSoFar = selectBest(env, origin, |
duke@1 | 1373 | argtypes, typeargtypes, |
duke@1 | 1374 | sym, bestSoFar, |
duke@1 | 1375 | allowBoxing, useVarargs, false); |
duke@1 | 1376 | } |
duke@1 | 1377 | } |
duke@1 | 1378 | if (bestSoFar.exists()) |
duke@1 | 1379 | return bestSoFar; |
duke@1 | 1380 | |
duke@1 | 1381 | e = env.toplevel.starImportScope.lookup(name); |
duke@1 | 1382 | for (; e.scope != null; e = e.next()) { |
duke@1 | 1383 | sym = e.sym; |
duke@1 | 1384 | Type origin = e.getOrigin().owner.type; |
duke@1 | 1385 | if (sym.kind == MTH) { |
duke@1 | 1386 | if (e.sym.owner.type != origin) |
duke@1 | 1387 | sym = sym.clone(e.getOrigin().owner); |
duke@1 | 1388 | if (!isAccessible(env, origin, sym)) |
duke@1 | 1389 | sym = new AccessError(env, origin, sym); |
duke@1 | 1390 | bestSoFar = selectBest(env, origin, |
duke@1 | 1391 | argtypes, typeargtypes, |
duke@1 | 1392 | sym, bestSoFar, |
duke@1 | 1393 | allowBoxing, useVarargs, false); |
duke@1 | 1394 | } |
duke@1 | 1395 | } |
duke@1 | 1396 | return bestSoFar; |
duke@1 | 1397 | } |
duke@1 | 1398 | |
duke@1 | 1399 | /** Load toplevel or member class with given fully qualified name and |
duke@1 | 1400 | * verify that it is accessible. |
duke@1 | 1401 | * @param env The current environment. |
duke@1 | 1402 | * @param name The fully qualified name of the class to be loaded. |
duke@1 | 1403 | */ |
duke@1 | 1404 | Symbol loadClass(Env<AttrContext> env, Name name) { |
duke@1 | 1405 | try { |
duke@1 | 1406 | ClassSymbol c = reader.loadClass(name); |
duke@1 | 1407 | return isAccessible(env, c) ? c : new AccessError(c); |
duke@1 | 1408 | } catch (ClassReader.BadClassFile err) { |
duke@1 | 1409 | throw err; |
duke@1 | 1410 | } catch (CompletionFailure ex) { |
duke@1 | 1411 | return typeNotFound; |
duke@1 | 1412 | } |
duke@1 | 1413 | } |
duke@1 | 1414 | |
duke@1 | 1415 | /** Find qualified member type. |
duke@1 | 1416 | * @param env The current environment. |
duke@1 | 1417 | * @param site The original type from where the selection takes |
duke@1 | 1418 | * place. |
duke@1 | 1419 | * @param name The type's name. |
duke@1 | 1420 | * @param c The class to search for the member type. This is |
duke@1 | 1421 | * always a superclass or implemented interface of |
duke@1 | 1422 | * site's class. |
duke@1 | 1423 | */ |
duke@1 | 1424 | Symbol findMemberType(Env<AttrContext> env, |
duke@1 | 1425 | Type site, |
duke@1 | 1426 | Name name, |
duke@1 | 1427 | TypeSymbol c) { |
duke@1 | 1428 | Symbol bestSoFar = typeNotFound; |
duke@1 | 1429 | Symbol sym; |
duke@1 | 1430 | Scope.Entry e = c.members().lookup(name); |
duke@1 | 1431 | while (e.scope != null) { |
duke@1 | 1432 | if (e.sym.kind == TYP) { |
duke@1 | 1433 | return isAccessible(env, site, e.sym) |
duke@1 | 1434 | ? e.sym |
duke@1 | 1435 | : new AccessError(env, site, e.sym); |
duke@1 | 1436 | } |
duke@1 | 1437 | e = e.next(); |
duke@1 | 1438 | } |
duke@1 | 1439 | Type st = types.supertype(c.type); |
duke@1 | 1440 | if (st != null && st.tag == CLASS) { |
duke@1 | 1441 | sym = findMemberType(env, site, name, st.tsym); |
duke@1 | 1442 | if (sym.kind < bestSoFar.kind) bestSoFar = sym; |
duke@1 | 1443 | } |
duke@1 | 1444 | for (List<Type> l = types.interfaces(c.type); |
duke@1 | 1445 | bestSoFar.kind != AMBIGUOUS && l.nonEmpty(); |
duke@1 | 1446 | l = l.tail) { |
duke@1 | 1447 | sym = findMemberType(env, site, name, l.head.tsym); |
duke@1 | 1448 | if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS && |
duke@1 | 1449 | sym.owner != bestSoFar.owner) |
duke@1 | 1450 | bestSoFar = new AmbiguityError(bestSoFar, sym); |
duke@1 | 1451 | else if (sym.kind < bestSoFar.kind) |
duke@1 | 1452 | bestSoFar = sym; |
duke@1 | 1453 | } |
duke@1 | 1454 | return bestSoFar; |
duke@1 | 1455 | } |
duke@1 | 1456 | |
duke@1 | 1457 | /** Find a global type in given scope and load corresponding class. |
duke@1 | 1458 | * @param env The current environment. |
duke@1 | 1459 | * @param scope The scope in which to look for the type. |
duke@1 | 1460 | * @param name The type's name. |
duke@1 | 1461 | */ |
duke@1 | 1462 | Symbol findGlobalType(Env<AttrContext> env, Scope scope, Name name) { |
duke@1 | 1463 | Symbol bestSoFar = typeNotFound; |
duke@1 | 1464 | for (Scope.Entry e = scope.lookup(name); e.scope != null; e = e.next()) { |
duke@1 | 1465 | Symbol sym = loadClass(env, e.sym.flatName()); |
duke@1 | 1466 | if (bestSoFar.kind == TYP && sym.kind == TYP && |
duke@1 | 1467 | bestSoFar != sym) |
duke@1 | 1468 | return new AmbiguityError(bestSoFar, sym); |
duke@1 | 1469 | else if (sym.kind < bestSoFar.kind) |
duke@1 | 1470 | bestSoFar = sym; |
duke@1 | 1471 | } |
duke@1 | 1472 | return bestSoFar; |
duke@1 | 1473 | } |
duke@1 | 1474 | |
duke@1 | 1475 | /** Find an unqualified type symbol. |
duke@1 | 1476 | * @param env The current environment. |
duke@1 | 1477 | * @param name The type's name. |
duke@1 | 1478 | */ |
duke@1 | 1479 | Symbol findType(Env<AttrContext> env, Name name) { |
duke@1 | 1480 | Symbol bestSoFar = typeNotFound; |
duke@1 | 1481 | Symbol sym; |
duke@1 | 1482 | boolean staticOnly = false; |
duke@1 | 1483 | for (Env<AttrContext> env1 = env; env1.outer != null; env1 = env1.outer) { |
duke@1 | 1484 | if (isStatic(env1)) staticOnly = true; |
duke@1 | 1485 | for (Scope.Entry e = env1.info.scope.lookup(name); |
duke@1 | 1486 | e.scope != null; |
duke@1 | 1487 | e = e.next()) { |
duke@1 | 1488 | if (e.sym.kind == TYP) { |
duke@1 | 1489 | if (staticOnly && |
duke@1 | 1490 | e.sym.type.tag == TYPEVAR && |
duke@1 | 1491 | e.sym.owner.kind == TYP) return new StaticError(e.sym); |
duke@1 | 1492 | return e.sym; |
duke@1 | 1493 | } |
duke@1 | 1494 | } |
duke@1 | 1495 | |
duke@1 | 1496 | sym = findMemberType(env1, env1.enclClass.sym.type, name, |
duke@1 | 1497 | env1.enclClass.sym); |
duke@1 | 1498 | if (staticOnly && sym.kind == TYP && |
duke@1 | 1499 | sym.type.tag == CLASS && |
duke@1 | 1500 | sym.type.getEnclosingType().tag == CLASS && |
duke@1 | 1501 | env1.enclClass.sym.type.isParameterized() && |
duke@1 | 1502 | sym.type.getEnclosingType().isParameterized()) |
duke@1 | 1503 | return new StaticError(sym); |
duke@1 | 1504 | else if (sym.exists()) return sym; |
duke@1 | 1505 | else if (sym.kind < bestSoFar.kind) bestSoFar = sym; |
duke@1 | 1506 | |
duke@1 | 1507 | JCClassDecl encl = env1.baseClause ? (JCClassDecl)env1.tree : env1.enclClass; |
duke@1 | 1508 | if ((encl.sym.flags() & STATIC) != 0) |
duke@1 | 1509 | staticOnly = true; |
duke@1 | 1510 | } |
duke@1 | 1511 | |
jjg@1127 | 1512 | if (!env.tree.hasTag(IMPORT)) { |
duke@1 | 1513 | sym = findGlobalType(env, env.toplevel.namedImportScope, name); |
duke@1 | 1514 | if (sym.exists()) return sym; |
duke@1 | 1515 | else if (sym.kind < bestSoFar.kind) bestSoFar = sym; |
duke@1 | 1516 | |
duke@1 | 1517 | sym = findGlobalType(env, env.toplevel.packge.members(), name); |
duke@1 | 1518 | if (sym.exists()) return sym; |
duke@1 | 1519 | else if (sym.kind < bestSoFar.kind) bestSoFar = sym; |
duke@1 | 1520 | |
duke@1 | 1521 | sym = findGlobalType(env, env.toplevel.starImportScope, name); |
duke@1 | 1522 | if (sym.exists()) return sym; |
duke@1 | 1523 | else if (sym.kind < bestSoFar.kind) bestSoFar = sym; |
duke@1 | 1524 | } |
duke@1 | 1525 | |
duke@1 | 1526 | return bestSoFar; |
duke@1 | 1527 | } |
duke@1 | 1528 | |
duke@1 | 1529 | /** Find an unqualified identifier which matches a specified kind set. |
duke@1 | 1530 | * @param env The current environment. |
duke@1 | 1531 | * @param name The indentifier's name. |
duke@1 | 1532 | * @param kind Indicates the possible symbol kinds |
duke@1 | 1533 | * (a subset of VAL, TYP, PCK). |
duke@1 | 1534 | */ |
duke@1 | 1535 | Symbol findIdent(Env<AttrContext> env, Name name, int kind) { |
duke@1 | 1536 | Symbol bestSoFar = typeNotFound; |
duke@1 | 1537 | Symbol sym; |
duke@1 | 1538 | |
duke@1 | 1539 | if ((kind & VAR) != 0) { |
duke@1 | 1540 | sym = findVar(env, name); |
duke@1 | 1541 | if (sym.exists()) return sym; |
duke@1 | 1542 | else if (sym.kind < bestSoFar.kind) bestSoFar = sym; |
duke@1 | 1543 | } |
duke@1 | 1544 | |
duke@1 | 1545 | if ((kind & TYP) != 0) { |
duke@1 | 1546 | sym = findType(env, name); |
duke@1 | 1547 | if (sym.exists()) return sym; |
duke@1 | 1548 | else if (sym.kind < bestSoFar.kind) bestSoFar = sym; |
duke@1 | 1549 | } |
duke@1 | 1550 | |
duke@1 | 1551 | if ((kind & PCK) != 0) return reader.enterPackage(name); |
duke@1 | 1552 | else return bestSoFar; |
duke@1 | 1553 | } |
duke@1 | 1554 | |
duke@1 | 1555 | /** Find an identifier in a package which matches a specified kind set. |
duke@1 | 1556 | * @param env The current environment. |
duke@1 | 1557 | * @param name The identifier's name. |
duke@1 | 1558 | * @param kind Indicates the possible symbol kinds |
duke@1 | 1559 | * (a nonempty subset of TYP, PCK). |
duke@1 | 1560 | */ |
duke@1 | 1561 | Symbol findIdentInPackage(Env<AttrContext> env, TypeSymbol pck, |
duke@1 | 1562 | Name name, int kind) { |
duke@1 | 1563 | Name fullname = TypeSymbol.formFullName(name, pck); |
duke@1 | 1564 | Symbol bestSoFar = typeNotFound; |
duke@1 | 1565 | PackageSymbol pack = null; |
duke@1 | 1566 | if ((kind & PCK) != 0) { |
duke@1 | 1567 | pack = reader.enterPackage(fullname); |
duke@1 | 1568 | if (pack.exists()) return pack; |
duke@1 | 1569 | } |
duke@1 | 1570 | if ((kind & TYP) != 0) { |
duke@1 | 1571 | Symbol sym = loadClass(env, fullname); |
duke@1 | 1572 | if (sym.exists()) { |
duke@1 | 1573 | // don't allow programs to use flatnames |
duke@1 | 1574 | if (name == sym.name) return sym; |
duke@1 | 1575 | } |
duke@1 | 1576 | else if (sym.kind < bestSoFar.kind) bestSoFar = sym; |
duke@1 | 1577 | } |
duke@1 | 1578 | return (pack != null) ? pack : bestSoFar; |
duke@1 | 1579 | } |
duke@1 | 1580 | |
duke@1 | 1581 | /** Find an identifier among the members of a given type `site'. |
duke@1 | 1582 | * @param env The current environment. |
duke@1 | 1583 | * @param site The type containing the symbol to be found. |
duke@1 | 1584 | * @param name The identifier's name. |
duke@1 | 1585 | * @param kind Indicates the possible symbol kinds |
duke@1 | 1586 | * (a subset of VAL, TYP). |
duke@1 | 1587 | */ |
duke@1 | 1588 | Symbol findIdentInType(Env<AttrContext> env, Type site, |
duke@1 | 1589 | Name name, int kind) { |
duke@1 | 1590 | Symbol bestSoFar = typeNotFound; |
duke@1 | 1591 | Symbol sym; |
duke@1 | 1592 | if ((kind & VAR) != 0) { |
duke@1 | 1593 | sym = findField(env, site, name, site.tsym); |
duke@1 | 1594 | if (sym.exists()) return sym; |
duke@1 | 1595 | else if (sym.kind < bestSoFar.kind) bestSoFar = sym; |
duke@1 | 1596 | } |
duke@1 | 1597 | |
duke@1 | 1598 | if ((kind & TYP) != 0) { |
duke@1 | 1599 | sym = findMemberType(env, site, name, site.tsym); |
duke@1 | 1600 | if (sym.exists()) return sym; |
duke@1 | 1601 | else if (sym.kind < bestSoFar.kind) bestSoFar = sym; |
duke@1 | 1602 | } |
duke@1 | 1603 | return bestSoFar; |
duke@1 | 1604 | } |
duke@1 | 1605 | |
duke@1 | 1606 | /* *************************************************************************** |
duke@1 | 1607 | * Access checking |
duke@1 | 1608 | * The following methods convert ResolveErrors to ErrorSymbols, issuing |
duke@1 | 1609 | * an error message in the process |
duke@1 | 1610 | ****************************************************************************/ |
duke@1 | 1611 | |
duke@1 | 1612 | /** If `sym' is a bad symbol: report error and return errSymbol |
duke@1 | 1613 | * else pass through unchanged, |
duke@1 | 1614 | * additional arguments duplicate what has been used in trying to find the |
jjg@1326 | 1615 | * symbol {@literal (--> flyweight pattern)}. This improves performance since we |
duke@1 | 1616 | * expect misses to happen frequently. |
duke@1 | 1617 | * |
duke@1 | 1618 | * @param sym The symbol that was found, or a ResolveError. |
duke@1 | 1619 | * @param pos The position to use for error reporting. |
duke@1 | 1620 | * @param site The original type from where the selection took place. |
duke@1 | 1621 | * @param name The symbol's name. |
duke@1 | 1622 | * @param argtypes The invocation's value arguments, |
duke@1 | 1623 | * if we looked for a method. |
duke@1 | 1624 | * @param typeargtypes The invocation's type arguments, |
duke@1 | 1625 | * if we looked for a method. |
duke@1 | 1626 | */ |
duke@1 | 1627 | Symbol access(Symbol sym, |
duke@1 | 1628 | DiagnosticPosition pos, |
mcimadamore@829 | 1629 | Symbol location, |
duke@1 | 1630 | Type site, |
duke@1 | 1631 | Name name, |
duke@1 | 1632 | boolean qualified, |
duke@1 | 1633 | List<Type> argtypes, |
duke@1 | 1634 | List<Type> typeargtypes) { |
duke@1 | 1635 | if (sym.kind >= AMBIGUOUS) { |
mcimadamore@302 | 1636 | ResolveError errSym = (ResolveError)sym; |
duke@1 | 1637 | if (!site.isErroneous() && |
duke@1 | 1638 | !Type.isErroneous(argtypes) && |
duke@1 | 1639 | (typeargtypes==null || !Type.isErroneous(typeargtypes))) |
mcimadamore@829 | 1640 | logResolveError(errSym, pos, location, site, name, argtypes, typeargtypes); |
mcimadamore@302 | 1641 | sym = errSym.access(name, qualified ? site.tsym : syms.noSymbol); |
duke@1 | 1642 | } |
duke@1 | 1643 | return sym; |
duke@1 | 1644 | } |
duke@1 | 1645 | |
mcimadamore@829 | 1646 | /** Same as original access(), but without location. |
mcimadamore@829 | 1647 | */ |
mcimadamore@829 | 1648 | Symbol access(Symbol sym, |
mcimadamore@829 | 1649 | DiagnosticPosition pos, |
mcimadamore@829 | 1650 | Type site, |
mcimadamore@829 | 1651 | Name name, |
mcimadamore@829 | 1652 | boolean qualified, |
mcimadamore@829 | 1653 | List<Type> argtypes, |
mcimadamore@829 | 1654 | List<Type> typeargtypes) { |
mcimadamore@829 | 1655 | return access(sym, pos, site.tsym, site, name, qualified, argtypes, typeargtypes); |
mcimadamore@829 | 1656 | } |
mcimadamore@829 | 1657 | |
mcimadamore@829 | 1658 | /** Same as original access(), but without type arguments and arguments. |
mcimadamore@829 | 1659 | */ |
mcimadamore@829 | 1660 | Symbol access(Symbol sym, |
mcimadamore@829 | 1661 | DiagnosticPosition pos, |
mcimadamore@829 | 1662 | Symbol location, |
mcimadamore@829 | 1663 | Type site, |
mcimadamore@829 | 1664 | Name name, |
mcimadamore@829 | 1665 | boolean qualified) { |
mcimadamore@829 | 1666 | if (sym.kind >= AMBIGUOUS) |
mcimadamore@829 | 1667 | return access(sym, pos, location, site, name, qualified, List.<Type>nil(), null); |
mcimadamore@829 | 1668 | else |
mcimadamore@829 | 1669 | return sym; |
mcimadamore@829 | 1670 | } |
mcimadamore@829 | 1671 | |
mcimadamore@829 | 1672 | /** Same as original access(), but without location, type arguments and arguments. |
duke@1 | 1673 | */ |
duke@1 | 1674 | Symbol access(Symbol sym, |
duke@1 | 1675 | DiagnosticPosition pos, |
duke@1 | 1676 | Type site, |
duke@1 | 1677 | Name name, |
duke@1 | 1678 | boolean qualified) { |
mcimadamore@829 | 1679 | return access(sym, pos, site.tsym, site, name, qualified); |
duke@1 | 1680 | } |
duke@1 | 1681 | |
duke@1 | 1682 | /** Check that sym is not an abstract method. |
duke@1 | 1683 | */ |
duke@1 | 1684 | void checkNonAbstract(DiagnosticPosition pos, Symbol sym) { |
duke@1 | 1685 | if ((sym.flags() & ABSTRACT) != 0) |
duke@1 | 1686 | log.error(pos, "abstract.cant.be.accessed.directly", |
duke@1 | 1687 | kindName(sym), sym, sym.location()); |
duke@1 | 1688 | } |
duke@1 | 1689 | |
duke@1 | 1690 | /* *************************************************************************** |
duke@1 | 1691 | * Debugging |
duke@1 | 1692 | ****************************************************************************/ |
duke@1 | 1693 | |
duke@1 | 1694 | /** print all scopes starting with scope s and proceeding outwards. |
duke@1 | 1695 | * used for debugging. |
duke@1 | 1696 | */ |
duke@1 | 1697 | public void printscopes(Scope s) { |
duke@1 | 1698 | while (s != null) { |
duke@1 | 1699 | if (s.owner != null) |
duke@1 | 1700 | System.err.print(s.owner + ": "); |
duke@1 | 1701 | for (Scope.Entry e = s.elems; e != null; e = e.sibling) { |
duke@1 | 1702 | if ((e.sym.flags() & ABSTRACT) != 0) |
duke@1 | 1703 | System.err.print("abstract "); |
duke@1 | 1704 | System.err.print(e.sym + " "); |
duke@1 | 1705 | } |
duke@1 | 1706 | System.err.println(); |
duke@1 | 1707 | s = s.next; |
duke@1 | 1708 | } |
duke@1 | 1709 | } |
duke@1 | 1710 | |
duke@1 | 1711 | void printscopes(Env<AttrContext> env) { |
duke@1 | 1712 | while (env.outer != null) { |
duke@1 | 1713 | System.err.println("------------------------------"); |
duke@1 | 1714 | printscopes(env.info.scope); |
duke@1 | 1715 | env = env.outer; |
duke@1 | 1716 | } |
duke@1 | 1717 | } |
duke@1 | 1718 | |
duke@1 | 1719 | public void printscopes(Type t) { |
duke@1 | 1720 | while (t.tag == CLASS) { |
duke@1 | 1721 | printscopes(t.tsym.members()); |
duke@1 | 1722 | t = types.supertype(t); |
duke@1 | 1723 | } |
duke@1 | 1724 | } |
duke@1 | 1725 | |
duke@1 | 1726 | /* *************************************************************************** |
duke@1 | 1727 | * Name resolution |
duke@1 | 1728 | * Naming conventions are as for symbol lookup |
duke@1 | 1729 | * Unlike the find... methods these methods will report access errors |
duke@1 | 1730 | ****************************************************************************/ |
duke@1 | 1731 | |
duke@1 | 1732 | /** Resolve an unqualified (non-method) identifier. |
duke@1 | 1733 | * @param pos The position to use for error reporting. |
duke@1 | 1734 | * @param env The environment current at the identifier use. |
duke@1 | 1735 | * @param name The identifier's name. |
duke@1 | 1736 | * @param kind The set of admissible symbol kinds for the identifier. |
duke@1 | 1737 | */ |
duke@1 | 1738 | Symbol resolveIdent(DiagnosticPosition pos, Env<AttrContext> env, |
duke@1 | 1739 | Name name, int kind) { |
duke@1 | 1740 | return access( |
duke@1 | 1741 | findIdent(env, name, kind), |
duke@1 | 1742 | pos, env.enclClass.sym.type, name, false); |
duke@1 | 1743 | } |
duke@1 | 1744 | |
duke@1 | 1745 | /** Resolve an unqualified method identifier. |
duke@1 | 1746 | * @param pos The position to use for error reporting. |
duke@1 | 1747 | * @param env The environment current at the method invocation. |
duke@1 | 1748 | * @param name The identifier's name. |
duke@1 | 1749 | * @param argtypes The types of the invocation's value arguments. |
duke@1 | 1750 | * @param typeargtypes The types of the invocation's type arguments. |
duke@1 | 1751 | */ |
duke@1 | 1752 | Symbol resolveMethod(DiagnosticPosition pos, |
duke@1 | 1753 | Env<AttrContext> env, |
duke@1 | 1754 | Name name, |
duke@1 | 1755 | List<Type> argtypes, |
duke@1 | 1756 | List<Type> typeargtypes) { |
mcimadamore@1215 | 1757 | MethodResolutionContext prevResolutionContext = currentResolutionContext; |
mcimadamore@1215 | 1758 | try { |
mcimadamore@1215 | 1759 | currentResolutionContext = new MethodResolutionContext(); |
mcimadamore@1215 | 1760 | Symbol sym = methodNotFound; |
mcimadamore@1215 | 1761 | List<MethodResolutionPhase> steps = methodResolutionSteps; |
mcimadamore@1215 | 1762 | while (steps.nonEmpty() && |
mcimadamore@1215 | 1763 | steps.head.isApplicable(boxingEnabled, varargsEnabled) && |
mcimadamore@1215 | 1764 | sym.kind >= ERRONEOUS) { |
mcimadamore@1215 | 1765 | currentResolutionContext.step = steps.head; |
mcimadamore@1215 | 1766 | sym = findFun(env, name, argtypes, typeargtypes, |
mcimadamore@1215 | 1767 | steps.head.isBoxingRequired, |
mcimadamore@1215 | 1768 | env.info.varArgs = steps.head.isVarargsRequired); |
mcimadamore@1215 | 1769 | currentResolutionContext.resolutionCache.put(steps.head, sym); |
mcimadamore@1215 | 1770 | steps = steps.tail; |
mcimadamore@1215 | 1771 | } |
mcimadamore@1215 | 1772 | if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error |
mcimadamore@1215 | 1773 | MethodResolutionPhase errPhase = |
mcimadamore@1215 | 1774 | currentResolutionContext.firstErroneousResolutionPhase(); |
mcimadamore@1215 | 1775 | sym = access(currentResolutionContext.resolutionCache.get(errPhase), |
mcimadamore@1215 | 1776 | pos, env.enclClass.sym.type, name, false, argtypes, typeargtypes); |
mcimadamore@1215 | 1777 | env.info.varArgs = errPhase.isVarargsRequired; |
mcimadamore@1215 | 1778 | } |
mcimadamore@1215 | 1779 | return sym; |
duke@1 | 1780 | } |
mcimadamore@1215 | 1781 | finally { |
mcimadamore@1215 | 1782 | currentResolutionContext = prevResolutionContext; |
duke@1 | 1783 | } |
mcimadamore@689 | 1784 | } |
mcimadamore@689 | 1785 | |
duke@1 | 1786 | /** Resolve a qualified method identifier |
duke@1 | 1787 | * @param pos The position to use for error reporting. |
duke@1 | 1788 | * @param env The environment current at the method invocation. |
duke@1 | 1789 | * @param site The type of the qualifying expression, in which |
duke@1 | 1790 | * identifier is searched. |
duke@1 | 1791 | * @param name The identifier's name. |
duke@1 | 1792 | * @param argtypes The types of the invocation's value arguments. |
duke@1 | 1793 | * @param typeargtypes The types of the invocation's type arguments. |
duke@1 | 1794 | */ |
duke@1 | 1795 | Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env, |
duke@1 | 1796 | Type site, Name name, List<Type> argtypes, |
duke@1 | 1797 | List<Type> typeargtypes) { |
mcimadamore@829 | 1798 | return resolveQualifiedMethod(pos, env, site.tsym, site, name, argtypes, typeargtypes); |
mcimadamore@829 | 1799 | } |
mcimadamore@829 | 1800 | Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env, |
mcimadamore@829 | 1801 | Symbol location, Type site, Name name, List<Type> argtypes, |
mcimadamore@829 | 1802 | List<Type> typeargtypes) { |
mcimadamore@1215 | 1803 | return resolveQualifiedMethod(new MethodResolutionContext(), pos, env, location, site, name, argtypes, typeargtypes); |
mcimadamore@1215 | 1804 | } |
mcimadamore@1215 | 1805 | private Symbol resolveQualifiedMethod(MethodResolutionContext resolveContext, |
mcimadamore@1215 | 1806 | DiagnosticPosition pos, Env<AttrContext> env, |
mcimadamore@1215 | 1807 | Symbol location, Type site, Name name, List<Type> argtypes, |
mcimadamore@1215 | 1808 | List<Type> typeargtypes) { |
mcimadamore@1215 | 1809 | MethodResolutionContext prevResolutionContext = currentResolutionContext; |
mcimadamore@1215 | 1810 | try { |
mcimadamore@1215 | 1811 | currentResolutionContext = resolveContext; |
mcimadamore@1215 | 1812 | Symbol sym = methodNotFound; |
mcimadamore@1215 | 1813 | List<MethodResolutionPhase> steps = methodResolutionSteps; |
mcimadamore@1215 | 1814 | while (steps.nonEmpty() && |
mcimadamore@1215 | 1815 | steps.head.isApplicable(boxingEnabled, varargsEnabled) && |
mcimadamore@1215 | 1816 | sym.kind >= ERRONEOUS) { |
mcimadamore@1215 | 1817 | currentResolutionContext.step = steps.head; |
mcimadamore@1215 | 1818 | sym = findMethod(env, site, name, argtypes, typeargtypes, |
mcimadamore@1215 | 1819 | steps.head.isBoxingRequired(), |
mcimadamore@1215 | 1820 | env.info.varArgs = steps.head.isVarargsRequired(), false); |
mcimadamore@1215 | 1821 | currentResolutionContext.resolutionCache.put(steps.head, sym); |
mcimadamore@1215 | 1822 | steps = steps.tail; |
mcimadamore@1215 | 1823 | } |
mcimadamore@1215 | 1824 | if (sym.kind >= AMBIGUOUS) { |
mcimadamore@1239 | 1825 | //if nothing is found return the 'first' error |
mcimadamore@1239 | 1826 | MethodResolutionPhase errPhase = |
mcimadamore@1239 | 1827 | currentResolutionContext.firstErroneousResolutionPhase(); |
mcimadamore@1239 | 1828 | sym = access(currentResolutionContext.resolutionCache.get(errPhase), |
mcimadamore@1239 | 1829 | pos, location, site, name, true, argtypes, typeargtypes); |
mcimadamore@1239 | 1830 | env.info.varArgs = errPhase.isVarargsRequired; |
mcimadamore@1239 | 1831 | } else if (allowMethodHandles) { |
mcimadamore@1239 | 1832 | MethodSymbol msym = (MethodSymbol)sym; |
mcimadamore@1239 | 1833 | if (msym.isSignaturePolymorphic(types)) { |
mcimadamore@1215 | 1834 | env.info.varArgs = false; |
mcimadamore@1239 | 1835 | return findPolymorphicSignatureInstance(env, sym, argtypes); |
mcimadamore@1215 | 1836 | } |
mcimadamore@674 | 1837 | } |
mcimadamore@1215 | 1838 | return sym; |
duke@1 | 1839 | } |
mcimadamore@1215 | 1840 | finally { |
mcimadamore@1215 | 1841 | currentResolutionContext = prevResolutionContext; |
mcimadamore@1215 | 1842 | } |
duke@1 | 1843 | } |
duke@1 | 1844 | |
mcimadamore@674 | 1845 | /** Find or create an implicit method of exactly the given type (after erasure). |
mcimadamore@674 | 1846 | * Searches in a side table, not the main scope of the site. |
mcimadamore@674 | 1847 | * This emulates the lookup process required by JSR 292 in JVM. |
mcimadamore@674 | 1848 | * @param env Attribution environment |
mcimadamore@1239 | 1849 | * @param spMethod signature polymorphic method - i.e. MH.invokeExact |
mcimadamore@1239 | 1850 | * @param argtypes The required argument types |
mcimadamore@674 | 1851 | */ |
mcimadamore@1239 | 1852 | Symbol findPolymorphicSignatureInstance(Env<AttrContext> env, |
mcimadamore@1239 | 1853 | Symbol spMethod, |
mcimadamore@820 | 1854 | List<Type> argtypes) { |
mcimadamore@674 | 1855 | Type mtype = infer.instantiatePolymorphicSignatureInstance(env, |
mcimadamore@1239 | 1856 | (MethodSymbol)spMethod, argtypes); |
mcimadamore@1239 | 1857 | for (Symbol sym : polymorphicSignatureScope.getElementsByName(spMethod.name)) { |
mcimadamore@1239 | 1858 | if (types.isSameType(mtype, sym.type)) { |
mcimadamore@1239 | 1859 | return sym; |
mcimadamore@674 | 1860 | } |
mcimadamore@674 | 1861 | } |
mcimadamore@1239 | 1862 | |
mcimadamore@1239 | 1863 | // create the desired method |
mcimadamore@1239 | 1864 | long flags = ABSTRACT | HYPOTHETICAL | spMethod.flags() & Flags.AccessFlags; |
mcimadamore@1239 | 1865 | Symbol msym = new MethodSymbol(flags, spMethod.name, mtype, spMethod.owner); |
mcimadamore@1239 | 1866 | polymorphicSignatureScope.enter(msym); |
mcimadamore@1239 | 1867 | return msym; |
mcimadamore@674 | 1868 | } |
mcimadamore@674 | 1869 | |
duke@1 | 1870 | /** Resolve a qualified method identifier, throw a fatal error if not |
duke@1 | 1871 | * found. |
duke@1 | 1872 | * @param pos The position to use for error reporting. |
duke@1 | 1873 | * @param env The environment current at the method invocation. |
duke@1 | 1874 | * @param site The type of the qualifying expression, in which |
duke@1 | 1875 | * identifier is searched. |
duke@1 | 1876 | * @param name The identifier's name. |
duke@1 | 1877 | * @param argtypes The types of the invocation's value arguments. |
duke@1 | 1878 | * @param typeargtypes The types of the invocation's type arguments. |
duke@1 | 1879 | */ |
duke@1 | 1880 | public MethodSymbol resolveInternalMethod(DiagnosticPosition pos, Env<AttrContext> env, |
duke@1 | 1881 | Type site, Name name, |
duke@1 | 1882 | List<Type> argtypes, |
duke@1 | 1883 | List<Type> typeargtypes) { |
mcimadamore@1215 | 1884 | MethodResolutionContext resolveContext = new MethodResolutionContext(); |
mcimadamore@1215 | 1885 | resolveContext.internalResolution = true; |
mcimadamore@1215 | 1886 | Symbol sym = resolveQualifiedMethod(resolveContext, pos, env, site.tsym, |
mcimadamore@1215 | 1887 | site, name, argtypes, typeargtypes); |
mcimadamore@1215 | 1888 | if (sym.kind == MTH) return (MethodSymbol)sym; |
mcimadamore@1215 | 1889 | else throw new FatalError( |
mcimadamore@1215 | 1890 | diags.fragment("fatal.err.cant.locate.meth", |
mcimadamore@1215 | 1891 | name)); |
duke@1 | 1892 | } |
duke@1 | 1893 | |
duke@1 | 1894 | /** Resolve constructor. |
duke@1 | 1895 | * @param pos The position to use for error reporting. |
duke@1 | 1896 | * @param env The environment current at the constructor invocation. |
duke@1 | 1897 | * @param site The type of class for which a constructor is searched. |
duke@1 | 1898 | * @param argtypes The types of the constructor invocation's value |
duke@1 | 1899 | * arguments. |
duke@1 | 1900 | * @param typeargtypes The types of the constructor invocation's type |
duke@1 | 1901 | * arguments. |
duke@1 | 1902 | */ |
duke@1 | 1903 | Symbol resolveConstructor(DiagnosticPosition pos, |
duke@1 | 1904 | Env<AttrContext> env, |
duke@1 | 1905 | Type site, |
duke@1 | 1906 | List<Type> argtypes, |
duke@1 | 1907 | List<Type> typeargtypes) { |
mcimadamore@1215 | 1908 | return resolveConstructor(new MethodResolutionContext(), pos, env, site, argtypes, typeargtypes); |
mcimadamore@1215 | 1909 | } |
mcimadamore@1215 | 1910 | private Symbol resolveConstructor(MethodResolutionContext resolveContext, |
mcimadamore@1215 | 1911 | DiagnosticPosition pos, |
mcimadamore@1215 | 1912 | Env<AttrContext> env, |
mcimadamore@1215 | 1913 | Type site, |
mcimadamore@1215 | 1914 | List<Type> argtypes, |
mcimadamore@1215 | 1915 | List<Type> typeargtypes) { |
mcimadamore@1215 | 1916 | MethodResolutionContext prevResolutionContext = currentResolutionContext; |
mcimadamore@1215 | 1917 | try { |
mcimadamore@1215 | 1918 | currentResolutionContext = resolveContext; |
mcimadamore@1215 | 1919 | Symbol sym = methodNotFound; |
mcimadamore@1215 | 1920 | List<MethodResolutionPhase> steps = methodResolutionSteps; |
mcimadamore@1215 | 1921 | while (steps.nonEmpty() && |
mcimadamore@1215 | 1922 | steps.head.isApplicable(boxingEnabled, varargsEnabled) && |
mcimadamore@1215 | 1923 | sym.kind >= ERRONEOUS) { |
mcimadamore@1215 | 1924 | currentResolutionContext.step = steps.head; |
mcimadamore@1215 | 1925 | sym = findConstructor(pos, env, site, argtypes, typeargtypes, |
mcimadamore@1215 | 1926 | steps.head.isBoxingRequired(), |
mcimadamore@1215 | 1927 | env.info.varArgs = steps.head.isVarargsRequired()); |
mcimadamore@1215 | 1928 | currentResolutionContext.resolutionCache.put(steps.head, sym); |
mcimadamore@1215 | 1929 | steps = steps.tail; |
mcimadamore@1215 | 1930 | } |
mcimadamore@1215 | 1931 | if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error |
mcimadamore@1215 | 1932 | MethodResolutionPhase errPhase = currentResolutionContext.firstErroneousResolutionPhase(); |
mcimadamore@1215 | 1933 | sym = access(currentResolutionContext.resolutionCache.get(errPhase), |
mcimadamore@1215 | 1934 | pos, site, names.init, true, argtypes, typeargtypes); |
mcimadamore@1215 | 1935 | env.info.varArgs = errPhase.isVarargsRequired(); |
mcimadamore@1215 | 1936 | } |
mcimadamore@1215 | 1937 | return sym; |
duke@1 | 1938 | } |
mcimadamore@1215 | 1939 | finally { |
mcimadamore@1215 | 1940 | currentResolutionContext = prevResolutionContext; |
duke@1 | 1941 | } |
duke@1 | 1942 | } |
duke@1 | 1943 | |
mcimadamore@537 | 1944 | /** Resolve constructor using diamond inference. |
mcimadamore@537 | 1945 | * @param pos The position to use for error reporting. |
mcimadamore@537 | 1946 | * @param env The environment current at the constructor invocation. |
mcimadamore@537 | 1947 | * @param site The type of class for which a constructor is searched. |
mcimadamore@537 | 1948 | * The scope of this class has been touched in attribution. |
mcimadamore@537 | 1949 | * @param argtypes The types of the constructor invocation's value |
mcimadamore@537 | 1950 | * arguments. |
mcimadamore@537 | 1951 | * @param typeargtypes The types of the constructor invocation's type |
mcimadamore@537 | 1952 | * arguments. |
mcimadamore@537 | 1953 | */ |
mcimadamore@537 | 1954 | Symbol resolveDiamond(DiagnosticPosition pos, |
mcimadamore@537 | 1955 | Env<AttrContext> env, |
mcimadamore@537 | 1956 | Type site, |
mcimadamore@537 | 1957 | List<Type> argtypes, |
mcimadamore@631 | 1958 | List<Type> typeargtypes) { |
mcimadamore@1215 | 1959 | MethodResolutionContext prevResolutionContext = currentResolutionContext; |
mcimadamore@1215 | 1960 | try { |
mcimadamore@1215 | 1961 | currentResolutionContext = new MethodResolutionContext(); |
mcimadamore@1215 | 1962 | Symbol sym = methodNotFound; |
mcimadamore@1215 | 1963 | List<MethodResolutionPhase> steps = methodResolutionSteps; |
mcimadamore@1215 | 1964 | while (steps.nonEmpty() && |
mcimadamore@1215 | 1965 | steps.head.isApplicable(boxingEnabled, varargsEnabled) && |
mcimadamore@1215 | 1966 | sym.kind >= ERRONEOUS) { |
mcimadamore@1215 | 1967 | currentResolutionContext.step = steps.head; |
mcimadamore@1217 | 1968 | sym = findDiamond(env, site, argtypes, typeargtypes, |
mcimadamore@1215 | 1969 | steps.head.isBoxingRequired(), |
mcimadamore@1215 | 1970 | env.info.varArgs = steps.head.isVarargsRequired()); |
mcimadamore@1215 | 1971 | currentResolutionContext.resolutionCache.put(steps.head, sym); |
mcimadamore@1215 | 1972 | steps = steps.tail; |
mcimadamore@1215 | 1973 | } |
mcimadamore@1215 | 1974 | if (sym.kind >= AMBIGUOUS) { |
mcimadamore@1215 | 1975 | final JCDiagnostic details = sym.kind == WRONG_MTH ? |
mcimadamore@1215 | 1976 | currentResolutionContext.candidates.head.details : |
mcimadamore@1215 | 1977 | null; |
mcimadamore@1215 | 1978 | Symbol errSym = new ResolveError(WRONG_MTH, "diamond error") { |
mcimadamore@1215 | 1979 | @Override |
mcimadamore@1215 | 1980 | JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos, |
mcimadamore@1215 | 1981 | Symbol location, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) { |
mcimadamore@1215 | 1982 | String key = details == null ? |
mcimadamore@1215 | 1983 | "cant.apply.diamond" : |
mcimadamore@1215 | 1984 | "cant.apply.diamond.1"; |
mcimadamore@1215 | 1985 | return diags.create(dkind, log.currentSource(), pos, key, |
mcimadamore@1215 | 1986 | diags.fragment("diamond", site.tsym), details); |
mcimadamore@1215 | 1987 | } |
mcimadamore@1215 | 1988 | }; |
mcimadamore@1215 | 1989 | MethodResolutionPhase errPhase = currentResolutionContext.firstErroneousResolutionPhase(); |
mcimadamore@1215 | 1990 | sym = access(errSym, pos, site, names.init, true, argtypes, typeargtypes); |
mcimadamore@1215 | 1991 | env.info.varArgs = errPhase.isVarargsRequired(); |
mcimadamore@1215 | 1992 | } |
mcimadamore@1215 | 1993 | return sym; |
mcimadamore@537 | 1994 | } |
mcimadamore@1215 | 1995 | finally { |
mcimadamore@1215 | 1996 | currentResolutionContext = prevResolutionContext; |
mcimadamore@537 | 1997 | } |
mcimadamore@537 | 1998 | } |
mcimadamore@537 | 1999 | |
mcimadamore@1217 | 2000 | /** This method scans all the constructor symbol in a given class scope - |
mcimadamore@1217 | 2001 | * assuming that the original scope contains a constructor of the kind: |
jjg@1326 | 2002 | * {@code Foo(X x, Y y)}, where X,Y are class type-variables declared in Foo, |
mcimadamore@1217 | 2003 | * a method check is executed against the modified constructor type: |
jjg@1326 | 2004 | * {@code <X,Y>Foo<X,Y>(X x, Y y)}. This is crucial in order to enable diamond |
mcimadamore@1217 | 2005 | * inference. The inferred return type of the synthetic constructor IS |
mcimadamore@1217 | 2006 | * the inferred type for the diamond operator. |
mcimadamore@1217 | 2007 | */ |
mcimadamore@1217 | 2008 | private Symbol findDiamond(Env<AttrContext> env, |
mcimadamore@1217 | 2009 | Type site, |
mcimadamore@1217 | 2010 | List<Type> argtypes, |
mcimadamore@1217 | 2011 | List<Type> typeargtypes, |
mcimadamore@1217 | 2012 | boolean allowBoxing, |
mcimadamore@1217 | 2013 | boolean useVarargs) { |
mcimadamore@1217 | 2014 | Symbol bestSoFar = methodNotFound; |
mcimadamore@1217 | 2015 | for (Scope.Entry e = site.tsym.members().lookup(names.init); |
mcimadamore@1217 | 2016 | e.scope != null; |
mcimadamore@1217 | 2017 | e = e.next()) { |
mcimadamore@1217 | 2018 | //- System.out.println(" e " + e.sym); |
mcimadamore@1217 | 2019 | if (e.sym.kind == MTH && |
mcimadamore@1217 | 2020 | (e.sym.flags_field & SYNTHETIC) == 0) { |
mcimadamore@1217 | 2021 | List<Type> oldParams = e.sym.type.tag == FORALL ? |
mcimadamore@1217 | 2022 | ((ForAll)e.sym.type).tvars : |
mcimadamore@1217 | 2023 | List.<Type>nil(); |
mcimadamore@1217 | 2024 | Type constrType = new ForAll(site.tsym.type.getTypeArguments().appendList(oldParams), |
mcimadamore@1217 | 2025 | types.createMethodTypeWithReturn(e.sym.type.asMethodType(), site)); |
mcimadamore@1217 | 2026 | bestSoFar = selectBest(env, site, argtypes, typeargtypes, |
mcimadamore@1217 | 2027 | new MethodSymbol(e.sym.flags(), names.init, constrType, site.tsym), |
mcimadamore@1217 | 2028 | bestSoFar, |
mcimadamore@1217 | 2029 | allowBoxing, |
mcimadamore@1217 | 2030 | useVarargs, |
mcimadamore@1217 | 2031 | false); |
mcimadamore@1217 | 2032 | } |
mcimadamore@1217 | 2033 | } |
mcimadamore@1217 | 2034 | return bestSoFar; |
mcimadamore@1217 | 2035 | } |
mcimadamore@1217 | 2036 | |
duke@1 | 2037 | /** Resolve constructor. |
duke@1 | 2038 | * @param pos The position to use for error reporting. |
duke@1 | 2039 | * @param env The environment current at the constructor invocation. |
duke@1 | 2040 | * @param site The type of class for which a constructor is searched. |
duke@1 | 2041 | * @param argtypes The types of the constructor invocation's value |
duke@1 | 2042 | * arguments. |
duke@1 | 2043 | * @param typeargtypes The types of the constructor invocation's type |
duke@1 | 2044 | * arguments. |
duke@1 | 2045 | * @param allowBoxing Allow boxing and varargs conversions. |
duke@1 | 2046 | * @param useVarargs Box trailing arguments into an array for varargs. |
duke@1 | 2047 | */ |
duke@1 | 2048 | Symbol resolveConstructor(DiagnosticPosition pos, Env<AttrContext> env, |
duke@1 | 2049 | Type site, List<Type> argtypes, |
duke@1 | 2050 | List<Type> typeargtypes, |
duke@1 | 2051 | boolean allowBoxing, |
duke@1 | 2052 | boolean useVarargs) { |
mcimadamore@1215 | 2053 | MethodResolutionContext prevResolutionContext = currentResolutionContext; |
mcimadamore@1215 | 2054 | try { |
mcimadamore@1215 | 2055 | currentResolutionContext = new MethodResolutionContext(); |
mcimadamore@1215 | 2056 | return findConstructor(pos, env, site, argtypes, typeargtypes, allowBoxing, useVarargs); |
mcimadamore@1215 | 2057 | } |
mcimadamore@1215 | 2058 | finally { |
mcimadamore@1215 | 2059 | currentResolutionContext = prevResolutionContext; |
mcimadamore@1215 | 2060 | } |
mcimadamore@1215 | 2061 | } |
mcimadamore@1215 | 2062 | |
mcimadamore@1215 | 2063 | Symbol findConstructor(DiagnosticPosition pos, Env<AttrContext> env, |
mcimadamore@1215 | 2064 | Type site, List<Type> argtypes, |
mcimadamore@1215 | 2065 | List<Type> typeargtypes, |
mcimadamore@1215 | 2066 | boolean allowBoxing, |
mcimadamore@1215 | 2067 | boolean useVarargs) { |
duke@1 | 2068 | Symbol sym = findMethod(env, site, |
mcimadamore@1215 | 2069 | names.init, argtypes, |
mcimadamore@1215 | 2070 | typeargtypes, allowBoxing, |
mcimadamore@1215 | 2071 | useVarargs, false); |
mcimadamore@852 | 2072 | chk.checkDeprecated(pos, env.info.scope.owner, sym); |
duke@1 | 2073 | return sym; |
duke@1 | 2074 | } |
duke@1 | 2075 | |
duke@1 | 2076 | /** Resolve a constructor, throw a fatal error if not found. |
duke@1 | 2077 | * @param pos The position to use for error reporting. |
duke@1 | 2078 | * @param env The environment current at the method invocation. |
duke@1 | 2079 | * @param site The type to be constructed. |
duke@1 | 2080 | * @param argtypes The types of the invocation's value arguments. |
duke@1 | 2081 | * @param typeargtypes The types of the invocation's type arguments. |
duke@1 | 2082 | */ |
duke@1 | 2083 | public MethodSymbol resolveInternalConstructor(DiagnosticPosition pos, Env<AttrContext> env, |
duke@1 | 2084 | Type site, |
duke@1 | 2085 | List<Type> argtypes, |
duke@1 | 2086 | List<Type> typeargtypes) { |
mcimadamore@1215 | 2087 | MethodResolutionContext resolveContext = new MethodResolutionContext(); |
mcimadamore@1215 | 2088 | resolveContext.internalResolution = true; |
mcimadamore@1215 | 2089 | Symbol sym = resolveConstructor(resolveContext, pos, env, site, argtypes, typeargtypes); |
duke@1 | 2090 | if (sym.kind == MTH) return (MethodSymbol)sym; |
duke@1 | 2091 | else throw new FatalError( |
mcimadamore@89 | 2092 | diags.fragment("fatal.err.cant.locate.ctor", site)); |
duke@1 | 2093 | } |
duke@1 | 2094 | |
duke@1 | 2095 | /** Resolve operator. |
duke@1 | 2096 | * @param pos The position to use for error reporting. |
duke@1 | 2097 | * @param optag The tag of the operation tree. |
duke@1 | 2098 | * @param env The environment current at the operation. |
duke@1 | 2099 | * @param argtypes The types of the operands. |
duke@1 | 2100 | */ |
jjg@1127 | 2101 | Symbol resolveOperator(DiagnosticPosition pos, JCTree.Tag optag, |
duke@1 | 2102 | Env<AttrContext> env, List<Type> argtypes) { |
mcimadamore@1215 | 2103 | MethodResolutionContext prevResolutionContext = currentResolutionContext; |
mcimadamore@1215 | 2104 | try { |
mcimadamore@1215 | 2105 | currentResolutionContext = new MethodResolutionContext(); |
mcimadamore@1215 | 2106 | Name name = treeinfo.operatorName(optag); |
mcimadamore@1215 | 2107 | Symbol sym = findMethod(env, syms.predefClass.type, name, argtypes, |
mcimadamore@1215 | 2108 | null, false, false, true); |
mcimadamore@1215 | 2109 | if (boxingEnabled && sym.kind >= WRONG_MTHS) |
mcimadamore@1215 | 2110 | sym = findMethod(env, syms.predefClass.type, name, argtypes, |
mcimadamore@1215 | 2111 | null, true, false, true); |
mcimadamore@1215 | 2112 | return access(sym, pos, env.enclClass.sym.type, name, |
mcimadamore@1215 | 2113 | false, argtypes, null); |
mcimadamore@1215 | 2114 | } |
mcimadamore@1215 | 2115 | finally { |
mcimadamore@1215 | 2116 | currentResolutionContext = prevResolutionContext; |
mcimadamore@1215 | 2117 | } |
duke@1 | 2118 | } |
duke@1 | 2119 | |
duke@1 | 2120 | /** Resolve operator. |
duke@1 | 2121 | * @param pos The position to use for error reporting. |
duke@1 | 2122 | * @param optag The tag of the operation tree. |
duke@1 | 2123 | * @param env The environment current at the operation. |
duke@1 | 2124 | * @param arg The type of the operand. |
duke@1 | 2125 | */ |
jjg@1127 | 2126 | Symbol resolveUnaryOperator(DiagnosticPosition pos, JCTree.Tag optag, Env<AttrContext> env, Type arg) { |
duke@1 | 2127 | return resolveOperator(pos, optag, env, List.of(arg)); |
duke@1 | 2128 | } |
duke@1 | 2129 | |
duke@1 | 2130 | /** Resolve binary operator. |
duke@1 | 2131 | * @param pos The position to use for error reporting. |
duke@1 | 2132 | * @param optag The tag of the operation tree. |
duke@1 | 2133 | * @param env The environment current at the operation. |
duke@1 | 2134 | * @param left The types of the left operand. |
duke@1 | 2135 | * @param right The types of the right operand. |
duke@1 | 2136 | */ |
duke@1 | 2137 | Symbol resolveBinaryOperator(DiagnosticPosition pos, |
jjg@1127 | 2138 | JCTree.Tag optag, |
duke@1 | 2139 | Env<AttrContext> env, |
duke@1 | 2140 | Type left, |
duke@1 | 2141 | Type right) { |
duke@1 | 2142 | return resolveOperator(pos, optag, env, List.of(left, right)); |
duke@1 | 2143 | } |
duke@1 | 2144 | |
duke@1 | 2145 | /** |
duke@1 | 2146 | * Resolve `c.name' where name == this or name == super. |
duke@1 | 2147 | * @param pos The position to use for error reporting. |
duke@1 | 2148 | * @param env The environment current at the expression. |
duke@1 | 2149 | * @param c The qualifier. |
duke@1 | 2150 | * @param name The identifier's name. |
duke@1 | 2151 | */ |
duke@1 | 2152 | Symbol resolveSelf(DiagnosticPosition pos, |
duke@1 | 2153 | Env<AttrContext> env, |
duke@1 | 2154 | TypeSymbol c, |
duke@1 | 2155 | Name name) { |
duke@1 | 2156 | Env<AttrContext> env1 = env; |
duke@1 | 2157 | boolean staticOnly = false; |
duke@1 | 2158 | while (env1.outer != null) { |
duke@1 | 2159 | if (isStatic(env1)) staticOnly = true; |
duke@1 | 2160 | if (env1.enclClass.sym == c) { |
duke@1 | 2161 | Symbol sym = env1.info.scope.lookup(name).sym; |
duke@1 | 2162 | if (sym != null) { |
duke@1 | 2163 | if (staticOnly) sym = new StaticError(sym); |
duke@1 | 2164 | return access(sym, pos, env.enclClass.sym.type, |
duke@1 | 2165 | name, true); |
duke@1 | 2166 | } |
duke@1 | 2167 | } |
duke@1 | 2168 | if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true; |
duke@1 | 2169 | env1 = env1.outer; |
duke@1 | 2170 | } |
duke@1 | 2171 | log.error(pos, "not.encl.class", c); |
duke@1 | 2172 | return syms.errSymbol; |
duke@1 | 2173 | } |
duke@1 | 2174 | |
duke@1 | 2175 | /** |
duke@1 | 2176 | * Resolve `c.this' for an enclosing class c that contains the |
duke@1 | 2177 | * named member. |
duke@1 | 2178 | * @param pos The position to use for error reporting. |
duke@1 | 2179 | * @param env The environment current at the expression. |
duke@1 | 2180 | * @param member The member that must be contained in the result. |
duke@1 | 2181 | */ |
duke@1 | 2182 | Symbol resolveSelfContaining(DiagnosticPosition pos, |
duke@1 | 2183 | Env<AttrContext> env, |
mcimadamore@901 | 2184 | Symbol member, |
mcimadamore@901 | 2185 | boolean isSuperCall) { |
duke@1 | 2186 | Name name = names._this; |
mcimadamore@901 | 2187 | Env<AttrContext> env1 = isSuperCall ? env.outer : env; |
duke@1 | 2188 | boolean staticOnly = false; |
mcimadamore@901 | 2189 | if (env1 != null) { |
mcimadamore@901 | 2190 | while (env1 != null && env1.outer != null) { |
mcimadamore@901 | 2191 | if (isStatic(env1)) staticOnly = true; |
mcimadamore@901 | 2192 | if (env1.enclClass.sym.isSubClass(member.owner, types)) { |
mcimadamore@901 | 2193 | Symbol sym = env1.info.scope.lookup(name).sym; |
mcimadamore@901 | 2194 | if (sym != null) { |
mcimadamore@901 | 2195 | if (staticOnly) sym = new StaticError(sym); |
mcimadamore@901 | 2196 | return access(sym, pos, env.enclClass.sym.type, |
mcimadamore@901 | 2197 | name, true); |
mcimadamore@901 | 2198 | } |
duke@1 | 2199 | } |
mcimadamore@901 | 2200 | if ((env1.enclClass.sym.flags() & STATIC) != 0) |
mcimadamore@901 | 2201 | staticOnly = true; |
mcimadamore@901 | 2202 | env1 = env1.outer; |
duke@1 | 2203 | } |
duke@1 | 2204 | } |
duke@1 | 2205 | log.error(pos, "encl.class.required", member); |
duke@1 | 2206 | return syms.errSymbol; |
duke@1 | 2207 | } |
duke@1 | 2208 | |
duke@1 | 2209 | /** |
duke@1 | 2210 | * Resolve an appropriate implicit this instance for t's container. |
jjh@972 | 2211 | * JLS 8.8.5.1 and 15.9.2 |
duke@1 | 2212 | */ |
duke@1 | 2213 | Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t) { |
mcimadamore@901 | 2214 | return resolveImplicitThis(pos, env, t, false); |
mcimadamore@901 | 2215 | } |
mcimadamore@901 | 2216 | |
mcimadamore@901 | 2217 | Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t, boolean isSuperCall) { |
duke@1 | 2218 | Type thisType = (((t.tsym.owner.kind & (MTH|VAR)) != 0) |
duke@1 | 2219 | ? resolveSelf(pos, env, t.getEnclosingType().tsym, names._this) |
mcimadamore@901 | 2220 | : resolveSelfContaining(pos, env, t.tsym, isSuperCall)).type; |
duke@1 | 2221 | if (env.info.isSelfCall && thisType.tsym == env.enclClass.sym) |
duke@1 | 2222 | log.error(pos, "cant.ref.before.ctor.called", "this"); |
duke@1 | 2223 | return thisType; |
duke@1 | 2224 | } |
duke@1 | 2225 | |
duke@1 | 2226 | /* *************************************************************************** |
duke@1 | 2227 | * ResolveError classes, indicating error situations when accessing symbols |
duke@1 | 2228 | ****************************************************************************/ |
duke@1 | 2229 | |
mcimadamore@1221 | 2230 | //used by TransTypes when checking target type of synthetic cast |
mcimadamore@1221 | 2231 | public void logAccessErrorInternal(Env<AttrContext> env, JCTree tree, Type type) { |
mcimadamore@1221 | 2232 | AccessError error = new AccessError(env, env.enclClass.type, type.tsym); |
mcimadamore@1221 | 2233 | logResolveError(error, tree.pos(), env.enclClass.sym, env.enclClass.type, null, null, null); |
mcimadamore@302 | 2234 | } |
mcimadamore@302 | 2235 | //where |
mcimadamore@302 | 2236 | private void logResolveError(ResolveError error, |
mcimadamore@302 | 2237 | DiagnosticPosition pos, |
mcimadamore@829 | 2238 | Symbol location, |
mcimadamore@302 | 2239 | Type site, |
mcimadamore@302 | 2240 | Name name, |
mcimadamore@302 | 2241 | List<Type> argtypes, |
mcimadamore@302 | 2242 | List<Type> typeargtypes) { |
mcimadamore@302 | 2243 | JCDiagnostic d = error.getDiagnostic(JCDiagnostic.DiagnosticType.ERROR, |
mcimadamore@829 | 2244 | pos, location, site, name, argtypes, typeargtypes); |
jjg@643 | 2245 | if (d != null) { |
jjg@643 | 2246 | d.setFlag(DiagnosticFlag.RESOLVE_ERROR); |
mcimadamore@302 | 2247 | log.report(d); |
jjg@643 | 2248 | } |
duke@1 | 2249 | } |
duke@1 | 2250 | |
mcimadamore@161 | 2251 | private final LocalizedString noArgs = new LocalizedString("compiler.misc.no.args"); |
mcimadamore@161 | 2252 | |
mcimadamore@161 | 2253 | public Object methodArguments(List<Type> argtypes) { |
mcimadamore@1114 | 2254 | return argtypes == null || argtypes.isEmpty() ? noArgs : argtypes; |
mcimadamore@161 | 2255 | } |
mcimadamore@161 | 2256 | |
mcimadamore@302 | 2257 | /** |
mcimadamore@302 | 2258 | * Root class for resolution errors. Subclass of ResolveError |
mcimadamore@302 | 2259 | * represent a different kinds of resolution error - as such they must |
mcimadamore@302 | 2260 | * specify how they map into concrete compiler diagnostics. |
duke@1 | 2261 | */ |
mcimadamore@302 | 2262 | private abstract class ResolveError extends Symbol { |
duke@1 | 2263 | |
mcimadamore@302 | 2264 | /** The name of the kind of error, for debugging only. */ |
mcimadamore@302 | 2265 | final String debugName; |
mcimadamore@302 | 2266 | |
mcimadamore@302 | 2267 | ResolveError(int kind, String debugName) { |
duke@1 | 2268 | super(kind, 0, null, null, null); |
duke@1 | 2269 | this.debugName = debugName; |
duke@1 | 2270 | } |
duke@1 | 2271 | |
mcimadamore@302 | 2272 | @Override |
duke@1 | 2273 | public <R, P> R accept(ElementVisitor<R, P> v, P p) { |
duke@1 | 2274 | throw new AssertionError(); |
duke@1 | 2275 | } |
duke@1 | 2276 | |
mcimadamore@302 | 2277 | @Override |
duke@1 | 2278 | public String toString() { |
mcimadamore@302 | 2279 | return debugName; |
duke@1 | 2280 | } |
duke@1 | 2281 | |
mcimadamore@302 | 2282 | @Override |
mcimadamore@302 | 2283 | public boolean exists() { |
mcimadamore@302 | 2284 | return false; |
duke@1 | 2285 | } |
duke@1 | 2286 | |
mcimadamore@302 | 2287 | /** |
mcimadamore@302 | 2288 | * Create an external representation for this erroneous symbol to be |
mcimadamore@302 | 2289 | * used during attribution - by default this returns the symbol of a |
mcimadamore@302 | 2290 | * brand new error type which stores the original type found |
mcimadamore@302 | 2291 | * during resolution. |
mcimadamore@302 | 2292 | * |
mcimadamore@302 | 2293 | * @param name the name used during resolution |
mcimadamore@302 | 2294 | * @param location the location from which the symbol is accessed |
duke@1 | 2295 | */ |
mcimadamore@302 | 2296 | protected Symbol access(Name name, TypeSymbol location) { |
mcimadamore@302 | 2297 | return types.createErrorType(name, location, syms.errSymbol.type).tsym; |
duke@1 | 2298 | } |
duke@1 | 2299 | |
mcimadamore@302 | 2300 | /** |
mcimadamore@302 | 2301 | * Create a diagnostic representing this resolution error. |
mcimadamore@302 | 2302 | * |
mcimadamore@302 | 2303 | * @param dkind The kind of the diagnostic to be created (e.g error). |
mcimadamore@302 | 2304 | * @param pos The position to be used for error reporting. |
mcimadamore@302 | 2305 | * @param site The original type from where the selection took place. |
mcimadamore@302 | 2306 | * @param name The name of the symbol to be resolved. |
mcimadamore@302 | 2307 | * @param argtypes The invocation's value arguments, |
mcimadamore@302 | 2308 | * if we looked for a method. |
mcimadamore@302 | 2309 | * @param typeargtypes The invocation's type arguments, |
mcimadamore@302 | 2310 | * if we looked for a method. |
mcimadamore@302 | 2311 | */ |
mcimadamore@302 | 2312 | abstract JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind, |
mcimadamore@302 | 2313 | DiagnosticPosition pos, |
mcimadamore@829 | 2314 | Symbol location, |
mcimadamore@302 | 2315 | Type site, |
mcimadamore@302 | 2316 | Name name, |
mcimadamore@302 | 2317 | List<Type> argtypes, |
mcimadamore@302 | 2318 | List<Type> typeargtypes); |
duke@1 | 2319 | |
mcimadamore@302 | 2320 | /** |
mcimadamore@302 | 2321 | * A name designates an operator if it consists |
jjg@1326 | 2322 | * of a non-empty sequence of operator symbols {@literal +-~!/*%&|^<>= } |
mcimadamore@80 | 2323 | */ |
mcimadamore@80 | 2324 | boolean isOperator(Name name) { |
mcimadamore@80 | 2325 | int i = 0; |
jjg@113 | 2326 | while (i < name.getByteLength() && |
jjg@113 | 2327 | "+-~!*/%&|^<>=".indexOf(name.getByteAt(i)) >= 0) i++; |
jjg@113 | 2328 | return i > 0 && i == name.getByteLength(); |
mcimadamore@80 | 2329 | } |
duke@1 | 2330 | } |
duke@1 | 2331 | |
mcimadamore@302 | 2332 | /** |
mcimadamore@302 | 2333 | * This class is the root class of all resolution errors caused by |
mcimadamore@302 | 2334 | * an invalid symbol being found during resolution. |
duke@1 | 2335 | */ |
mcimadamore@302 | 2336 | abstract class InvalidSymbolError extends ResolveError { |
mcimadamore@302 | 2337 | |
mcimadamore@302 | 2338 | /** The invalid symbol found during resolution */ |
mcimadamore@302 | 2339 | Symbol sym; |
mcimadamore@302 | 2340 | |
mcimadamore@302 | 2341 | InvalidSymbolError(int kind, Symbol sym, String debugName) { |
mcimadamore@302 | 2342 | super(kind, debugName); |
mcimadamore@302 | 2343 | this.sym = sym; |
mcimadamore@302 | 2344 | } |
mcimadamore@302 | 2345 | |
mcimadamore@302 | 2346 | @Override |
mcimadamore@302 | 2347 | public boolean exists() { |
mcimadamore@302 | 2348 | return true; |
mcimadamore@302 | 2349 | } |
mcimadamore@302 | 2350 | |
mcimadamore@302 | 2351 | @Override |
mcimadamore@302 | 2352 | public String toString() { |
mcimadamore@302 | 2353 | return super.toString() + " wrongSym=" + sym; |
mcimadamore@302 | 2354 | } |
mcimadamore@302 | 2355 | |
mcimadamore@302 | 2356 | @Override |
mcimadamore@302 | 2357 | public Symbol access(Name name, TypeSymbol location) { |
mcimadamore@302 | 2358 | if (sym.kind >= AMBIGUOUS) |
mcimadamore@302 | 2359 | return ((ResolveError)sym).access(name, location); |
mcimadamore@302 | 2360 | else if ((sym.kind & ERRONEOUS) == 0 && (sym.kind & TYP) != 0) |
mcimadamore@302 | 2361 | return types.createErrorType(name, location, sym.type).tsym; |
mcimadamore@302 | 2362 | else |
mcimadamore@302 | 2363 | return sym; |
mcimadamore@302 | 2364 | } |
mcimadamore@302 | 2365 | } |
mcimadamore@302 | 2366 | |
mcimadamore@302 | 2367 | /** |
mcimadamore@302 | 2368 | * InvalidSymbolError error class indicating that a symbol matching a |
mcimadamore@302 | 2369 | * given name does not exists in a given site. |
mcimadamore@302 | 2370 | */ |
mcimadamore@302 | 2371 | class SymbolNotFoundError extends ResolveError { |
mcimadamore@302 | 2372 | |
mcimadamore@302 | 2373 | SymbolNotFoundError(int kind) { |
mcimadamore@302 | 2374 | super(kind, "symbol not found error"); |
mcimadamore@302 | 2375 | } |
mcimadamore@302 | 2376 | |
mcimadamore@302 | 2377 | @Override |
mcimadamore@302 | 2378 | JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind, |
mcimadamore@302 | 2379 | DiagnosticPosition pos, |
mcimadamore@829 | 2380 | Symbol location, |
mcimadamore@302 | 2381 | Type site, |
mcimadamore@302 | 2382 | Name name, |
mcimadamore@302 | 2383 | List<Type> argtypes, |
mcimadamore@302 | 2384 | List<Type> typeargtypes) { |
mcimadamore@302 | 2385 | argtypes = argtypes == null ? List.<Type>nil() : argtypes; |
mcimadamore@302 | 2386 | typeargtypes = typeargtypes == null ? List.<Type>nil() : typeargtypes; |
mcimadamore@302 | 2387 | if (name == names.error) |
mcimadamore@302 | 2388 | return null; |
mcimadamore@302 | 2389 | |
mcimadamore@302 | 2390 | if (isOperator(name)) { |
mcimadamore@829 | 2391 | boolean isUnaryOp = argtypes.size() == 1; |
mcimadamore@829 | 2392 | String key = argtypes.size() == 1 ? |
mcimadamore@829 | 2393 | "operator.cant.be.applied" : |
mcimadamore@829 | 2394 | "operator.cant.be.applied.1"; |
mcimadamore@829 | 2395 | Type first = argtypes.head; |
mcimadamore@829 | 2396 | Type second = !isUnaryOp ? argtypes.tail.head : null; |
jjg@612 | 2397 | return diags.create(dkind, log.currentSource(), pos, |
mcimadamore@829 | 2398 | key, name, first, second); |
mcimadamore@302 | 2399 | } |
mcimadamore@302 | 2400 | boolean hasLocation = false; |
mcimadamore@855 | 2401 | if (location == null) { |
mcimadamore@855 | 2402 | location = site.tsym; |
mcimadamore@855 | 2403 | } |
mcimadamore@829 | 2404 | if (!location.name.isEmpty()) { |
mcimadamore@829 | 2405 | if (location.kind == PCK && !site.tsym.exists()) { |
jjg@612 | 2406 | return diags.create(dkind, log.currentSource(), pos, |
mcimadamore@829 | 2407 | "doesnt.exist", location); |
mcimadamore@302 | 2408 | } |
mcimadamore@829 | 2409 | hasLocation = !location.name.equals(names._this) && |
mcimadamore@829 | 2410 | !location.name.equals(names._super); |
mcimadamore@302 | 2411 | } |
mcimadamore@302 | 2412 | boolean isConstructor = kind == ABSENT_MTH && |
mcimadamore@302 | 2413 | name == names.table.names.init; |
mcimadamore@302 | 2414 | KindName kindname = isConstructor ? KindName.CONSTRUCTOR : absentKind(kind); |
mcimadamore@302 | 2415 | Name idname = isConstructor ? site.tsym.name : name; |
mcimadamore@302 | 2416 | String errKey = getErrorKey(kindname, typeargtypes.nonEmpty(), hasLocation); |
mcimadamore@302 | 2417 | if (hasLocation) { |
jjg@612 | 2418 | return diags.create(dkind, log.currentSource(), pos, |
mcimadamore@302 | 2419 | errKey, kindname, idname, //symbol kindname, name |
mcimadamore@302 | 2420 | typeargtypes, argtypes, //type parameters and arguments (if any) |
mcimadamore@855 | 2421 | getLocationDiag(location, site)); //location kindname, type |
mcimadamore@302 | 2422 | } |
mcimadamore@302 | 2423 | else { |
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@302 | 2427 | } |
mcimadamore@302 | 2428 | } |
mcimadamore@302 | 2429 | //where |
mcimadamore@302 | 2430 | private String getErrorKey(KindName kindname, boolean hasTypeArgs, boolean hasLocation) { |
mcimadamore@302 | 2431 | String key = "cant.resolve"; |
mcimadamore@302 | 2432 | String suffix = hasLocation ? ".location" : ""; |
mcimadamore@302 | 2433 | switch (kindname) { |
mcimadamore@302 | 2434 | case METHOD: |
mcimadamore@302 | 2435 | case CONSTRUCTOR: { |
mcimadamore@302 | 2436 | suffix += ".args"; |
mcimadamore@302 | 2437 | suffix += hasTypeArgs ? ".params" : ""; |
mcimadamore@302 | 2438 | } |
mcimadamore@302 | 2439 | } |
mcimadamore@302 | 2440 | return key + suffix; |
mcimadamore@302 | 2441 | } |
mcimadamore@855 | 2442 | private JCDiagnostic getLocationDiag(Symbol location, Type site) { |
mcimadamore@855 | 2443 | if (location.kind == VAR) { |
mcimadamore@855 | 2444 | return diags.fragment("location.1", |
mcimadamore@829 | 2445 | kindName(location), |
mcimadamore@829 | 2446 | location, |
mcimadamore@855 | 2447 | location.type); |
mcimadamore@855 | 2448 | } else { |
mcimadamore@855 | 2449 | return diags.fragment("location", |
mcimadamore@855 | 2450 | typeKindName(site), |
mcimadamore@855 | 2451 | site, |
mcimadamore@855 | 2452 | null); |
mcimadamore@855 | 2453 | } |
mcimadamore@829 | 2454 | } |
mcimadamore@302 | 2455 | } |
mcimadamore@302 | 2456 | |
mcimadamore@302 | 2457 | /** |
mcimadamore@302 | 2458 | * InvalidSymbolError error class indicating that a given symbol |
mcimadamore@302 | 2459 | * (either a method, a constructor or an operand) is not applicable |
mcimadamore@302 | 2460 | * given an actual arguments/type argument list. |
mcimadamore@302 | 2461 | */ |
mcimadamore@1215 | 2462 | class InapplicableSymbolError extends ResolveError { |
mcimadamore@302 | 2463 | |
mcimadamore@1215 | 2464 | InapplicableSymbolError() { |
mcimadamore@1215 | 2465 | super(WRONG_MTH, "inapplicable symbol error"); |
mcimadamore@302 | 2466 | } |
mcimadamore@302 | 2467 | |
mcimadamore@1215 | 2468 | protected InapplicableSymbolError(int kind, String debugName) { |
mcimadamore@1215 | 2469 | super(kind, debugName); |
mcimadamore@302 | 2470 | } |
mcimadamore@302 | 2471 | |
mcimadamore@302 | 2472 | @Override |
mcimadamore@302 | 2473 | public String toString() { |
mcimadamore@1215 | 2474 | return super.toString(); |
mcimadamore@1215 | 2475 | } |
mcimadamore@1215 | 2476 | |
mcimadamore@1215 | 2477 | @Override |
mcimadamore@1215 | 2478 | public boolean exists() { |
mcimadamore@1215 | 2479 | return true; |
mcimadamore@302 | 2480 | } |
mcimadamore@302 | 2481 | |
mcimadamore@302 | 2482 | @Override |
mcimadamore@302 | 2483 | JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind, |
mcimadamore@302 | 2484 | DiagnosticPosition pos, |
mcimadamore@829 | 2485 | Symbol location, |
mcimadamore@302 | 2486 | Type site, |
mcimadamore@302 | 2487 | Name name, |
mcimadamore@302 | 2488 | List<Type> argtypes, |
mcimadamore@302 | 2489 | List<Type> typeargtypes) { |
mcimadamore@302 | 2490 | if (name == names.error) |
mcimadamore@302 | 2491 | return null; |
mcimadamore@302 | 2492 | |
mcimadamore@302 | 2493 | if (isOperator(name)) { |
mcimadamore@853 | 2494 | boolean isUnaryOp = argtypes.size() == 1; |
mcimadamore@853 | 2495 | String key = argtypes.size() == 1 ? |
mcimadamore@853 | 2496 | "operator.cant.be.applied" : |
mcimadamore@853 | 2497 | "operator.cant.be.applied.1"; |
mcimadamore@853 | 2498 | Type first = argtypes.head; |
mcimadamore@853 | 2499 | Type second = !isUnaryOp ? argtypes.tail.head : null; |
mcimadamore@853 | 2500 | return diags.create(dkind, log.currentSource(), pos, |
mcimadamore@853 | 2501 | key, name, first, second); |
mcimadamore@302 | 2502 | } |
mcimadamore@302 | 2503 | else { |
mcimadamore@1215 | 2504 | Candidate c = errCandidate(); |
mcimadamore@1215 | 2505 | Symbol ws = c.sym.asMemberOf(site, types); |
jjg@612 | 2506 | return diags.create(dkind, log.currentSource(), pos, |
mcimadamore@1215 | 2507 | "cant.apply.symbol" + (c.details != null ? ".1" : ""), |
mcimadamore@302 | 2508 | kindName(ws), |
mcimadamore@302 | 2509 | ws.name == names.init ? ws.owner.name : ws.name, |
mcimadamore@302 | 2510 | methodArguments(ws.type.getParameterTypes()), |
mcimadamore@302 | 2511 | methodArguments(argtypes), |
mcimadamore@302 | 2512 | kindName(ws.owner), |
mcimadamore@302 | 2513 | ws.owner.type, |
mcimadamore@1215 | 2514 | c.details); |
mcimadamore@302 | 2515 | } |
mcimadamore@302 | 2516 | } |
mcimadamore@302 | 2517 | |
mcimadamore@302 | 2518 | @Override |
mcimadamore@302 | 2519 | public Symbol access(Name name, TypeSymbol location) { |
mcimadamore@302 | 2520 | return types.createErrorType(name, location, syms.errSymbol.type).tsym; |
mcimadamore@302 | 2521 | } |
mcimadamore@1215 | 2522 | |
mcimadamore@1215 | 2523 | protected boolean shouldReport(Candidate c) { |
mcimadamore@1215 | 2524 | return !c.isApplicable() && |
mcimadamore@1215 | 2525 | (((c.sym.flags() & VARARGS) != 0 && c.step == VARARITY) || |
mcimadamore@1215 | 2526 | (c.sym.flags() & VARARGS) == 0 && c.step == (boxingEnabled ? BOX : BASIC)); |
mcimadamore@1215 | 2527 | } |
mcimadamore@1215 | 2528 | |
mcimadamore@1215 | 2529 | private Candidate errCandidate() { |
mcimadamore@1215 | 2530 | for (Candidate c : currentResolutionContext.candidates) { |
mcimadamore@1215 | 2531 | if (shouldReport(c)) { |
mcimadamore@1215 | 2532 | return c; |
mcimadamore@1215 | 2533 | } |
mcimadamore@1215 | 2534 | } |
mcimadamore@1215 | 2535 | Assert.error(); |
mcimadamore@1215 | 2536 | return null; |
mcimadamore@1215 | 2537 | } |
mcimadamore@302 | 2538 | } |
mcimadamore@302 | 2539 | |
mcimadamore@302 | 2540 | /** |
mcimadamore@302 | 2541 | * ResolveError error class indicating that a set of symbols |
mcimadamore@302 | 2542 | * (either methods, constructors or operands) is not applicable |
mcimadamore@302 | 2543 | * given an actual arguments/type argument list. |
mcimadamore@302 | 2544 | */ |
mcimadamore@1215 | 2545 | class InapplicableSymbolsError extends InapplicableSymbolError { |
mcimadamore@689 | 2546 | |
mcimadamore@1215 | 2547 | InapplicableSymbolsError() { |
mcimadamore@302 | 2548 | super(WRONG_MTHS, "inapplicable symbols"); |
mcimadamore@302 | 2549 | } |
mcimadamore@302 | 2550 | |
mcimadamore@302 | 2551 | @Override |
mcimadamore@302 | 2552 | JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind, |
mcimadamore@302 | 2553 | DiagnosticPosition pos, |
mcimadamore@829 | 2554 | Symbol location, |
mcimadamore@302 | 2555 | Type site, |
mcimadamore@302 | 2556 | Name name, |
mcimadamore@302 | 2557 | List<Type> argtypes, |
mcimadamore@302 | 2558 | List<Type> typeargtypes) { |
mcimadamore@1215 | 2559 | if (currentResolutionContext.candidates.nonEmpty()) { |
mcimadamore@689 | 2560 | JCDiagnostic err = diags.create(dkind, |
mcimadamore@689 | 2561 | log.currentSource(), |
mcimadamore@689 | 2562 | pos, |
mcimadamore@689 | 2563 | "cant.apply.symbols", |
mcimadamore@689 | 2564 | name == names.init ? KindName.CONSTRUCTOR : absentKind(kind), |
mcimadamore@689 | 2565 | getName(), |
mcimadamore@689 | 2566 | argtypes); |
mcimadamore@689 | 2567 | return new JCDiagnostic.MultilineDiagnostic(err, candidateDetails(site)); |
mcimadamore@689 | 2568 | } else { |
mcimadamore@689 | 2569 | return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind, pos, |
mcimadamore@829 | 2570 | location, site, name, argtypes, typeargtypes); |
mcimadamore@689 | 2571 | } |
mcimadamore@689 | 2572 | } |
mcimadamore@689 | 2573 | |
mcimadamore@689 | 2574 | //where |
mcimadamore@689 | 2575 | List<JCDiagnostic> candidateDetails(Type site) { |
mcimadamore@689 | 2576 | List<JCDiagnostic> details = List.nil(); |
mcimadamore@1215 | 2577 | for (Candidate c : currentResolutionContext.candidates) { |
mcimadamore@1215 | 2578 | if (!shouldReport(c)) continue; |
mcimadamore@1215 | 2579 | JCDiagnostic detailDiag = diags.fragment("inapplicable.method", |
mcimadamore@1215 | 2580 | Kinds.kindName(c.sym), |
mcimadamore@1215 | 2581 | c.sym.location(site, types), |
mcimadamore@1215 | 2582 | c.sym.asMemberOf(site, types), |
mcimadamore@1215 | 2583 | c.details); |
mcimadamore@1215 | 2584 | details = details.prepend(detailDiag); |
mcimadamore@1215 | 2585 | } |
mcimadamore@689 | 2586 | return details.reverse(); |
mcimadamore@689 | 2587 | } |
mcimadamore@689 | 2588 | |
mcimadamore@689 | 2589 | private Name getName() { |
mcimadamore@1215 | 2590 | Symbol sym = currentResolutionContext.candidates.head.sym; |
mcimadamore@689 | 2591 | return sym.name == names.init ? |
mcimadamore@689 | 2592 | sym.owner.name : |
mcimadamore@689 | 2593 | sym.name; |
mcimadamore@689 | 2594 | } |
mcimadamore@302 | 2595 | } |
mcimadamore@302 | 2596 | |
mcimadamore@302 | 2597 | /** |
mcimadamore@302 | 2598 | * An InvalidSymbolError error class indicating that a symbol is not |
mcimadamore@302 | 2599 | * accessible from a given site |
mcimadamore@302 | 2600 | */ |
mcimadamore@302 | 2601 | class AccessError extends InvalidSymbolError { |
mcimadamore@302 | 2602 | |
mcimadamore@302 | 2603 | private Env<AttrContext> env; |
mcimadamore@302 | 2604 | private Type site; |
duke@1 | 2605 | |
duke@1 | 2606 | AccessError(Symbol sym) { |
duke@1 | 2607 | this(null, null, sym); |
duke@1 | 2608 | } |
duke@1 | 2609 | |
duke@1 | 2610 | AccessError(Env<AttrContext> env, Type site, Symbol sym) { |
duke@1 | 2611 | super(HIDDEN, sym, "access error"); |
duke@1 | 2612 | this.env = env; |
duke@1 | 2613 | this.site = site; |
duke@1 | 2614 | if (debugResolve) |
duke@1 | 2615 | log.error("proc.messager", sym + " @ " + site + " is inaccessible."); |
duke@1 | 2616 | } |
duke@1 | 2617 | |
mcimadamore@302 | 2618 | @Override |
mcimadamore@302 | 2619 | public boolean exists() { |
mcimadamore@302 | 2620 | return false; |
mcimadamore@302 | 2621 | } |
duke@1 | 2622 | |
mcimadamore@302 | 2623 | @Override |
mcimadamore@302 | 2624 | JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind, |
mcimadamore@302 | 2625 | DiagnosticPosition pos, |
mcimadamore@829 | 2626 | Symbol location, |
mcimadamore@302 | 2627 | Type site, |
mcimadamore@302 | 2628 | Name name, |
mcimadamore@302 | 2629 | List<Type> argtypes, |
mcimadamore@302 | 2630 | List<Type> typeargtypes) { |
mcimadamore@302 | 2631 | if (sym.owner.type.tag == ERROR) |
mcimadamore@302 | 2632 | return null; |
mcimadamore@302 | 2633 | |
mcimadamore@302 | 2634 | if (sym.name == names.init && sym.owner != site.tsym) { |
mcimadamore@302 | 2635 | return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind, |
mcimadamore@829 | 2636 | pos, location, site, name, argtypes, typeargtypes); |
mcimadamore@302 | 2637 | } |
mcimadamore@302 | 2638 | else if ((sym.flags() & PUBLIC) != 0 |
mcimadamore@302 | 2639 | || (env != null && this.site != null |
mcimadamore@302 | 2640 | && !isAccessible(env, this.site))) { |
jjg@612 | 2641 | return diags.create(dkind, log.currentSource(), |
mcimadamore@302 | 2642 | pos, "not.def.access.class.intf.cant.access", |
mcimadamore@302 | 2643 | sym, sym.location()); |
mcimadamore@302 | 2644 | } |
mcimadamore@302 | 2645 | else if ((sym.flags() & (PRIVATE | PROTECTED)) != 0) { |
jjg@612 | 2646 | return diags.create(dkind, log.currentSource(), |
mcimadamore@302 | 2647 | pos, "report.access", sym, |
mcimadamore@302 | 2648 | asFlagSet(sym.flags() & (PRIVATE | PROTECTED)), |
mcimadamore@302 | 2649 | sym.location()); |
mcimadamore@302 | 2650 | } |
mcimadamore@302 | 2651 | else { |
jjg@612 | 2652 | return diags.create(dkind, log.currentSource(), |
mcimadamore@302 | 2653 | pos, "not.def.public.cant.access", sym, sym.location()); |
duke@1 | 2654 | } |
duke@1 | 2655 | } |
duke@1 | 2656 | } |
duke@1 | 2657 | |
mcimadamore@302 | 2658 | /** |
mcimadamore@302 | 2659 | * InvalidSymbolError error class indicating that an instance member |
mcimadamore@302 | 2660 | * has erroneously been accessed from a static context. |
duke@1 | 2661 | */ |
mcimadamore@302 | 2662 | class StaticError extends InvalidSymbolError { |
mcimadamore@302 | 2663 | |
duke@1 | 2664 | StaticError(Symbol sym) { |
duke@1 | 2665 | super(STATICERR, sym, "static error"); |
duke@1 | 2666 | } |
duke@1 | 2667 | |
mcimadamore@302 | 2668 | @Override |
mcimadamore@302 | 2669 | JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind, |
mcimadamore@302 | 2670 | DiagnosticPosition pos, |
mcimadamore@829 | 2671 | Symbol location, |
mcimadamore@302 | 2672 | Type site, |
mcimadamore@302 | 2673 | Name name, |
mcimadamore@302 | 2674 | List<Type> argtypes, |
mcimadamore@302 | 2675 | List<Type> typeargtypes) { |
mcimadamore@80 | 2676 | Symbol errSym = ((sym.kind == TYP && sym.type.tag == CLASS) |
mcimadamore@80 | 2677 | ? types.erasure(sym.type).tsym |
mcimadamore@80 | 2678 | : sym); |
jjg@612 | 2679 | return diags.create(dkind, log.currentSource(), pos, |
mcimadamore@302 | 2680 | "non-static.cant.be.ref", kindName(sym), errSym); |
duke@1 | 2681 | } |
duke@1 | 2682 | } |
duke@1 | 2683 | |
mcimadamore@302 | 2684 | /** |
mcimadamore@302 | 2685 | * InvalidSymbolError error class indicating that a pair of symbols |
mcimadamore@302 | 2686 | * (either methods, constructors or operands) are ambiguous |
mcimadamore@302 | 2687 | * given an actual arguments/type argument list. |
duke@1 | 2688 | */ |
mcimadamore@302 | 2689 | class AmbiguityError extends InvalidSymbolError { |
mcimadamore@302 | 2690 | |
mcimadamore@302 | 2691 | /** The other maximally specific symbol */ |
duke@1 | 2692 | Symbol sym2; |
duke@1 | 2693 | |
duke@1 | 2694 | AmbiguityError(Symbol sym1, Symbol sym2) { |
duke@1 | 2695 | super(AMBIGUOUS, sym1, "ambiguity error"); |
duke@1 | 2696 | this.sym2 = sym2; |
duke@1 | 2697 | } |
duke@1 | 2698 | |
mcimadamore@302 | 2699 | @Override |
mcimadamore@302 | 2700 | JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind, |
mcimadamore@302 | 2701 | DiagnosticPosition pos, |
mcimadamore@829 | 2702 | Symbol location, |
mcimadamore@302 | 2703 | Type site, |
mcimadamore@302 | 2704 | Name name, |
mcimadamore@302 | 2705 | List<Type> argtypes, |
mcimadamore@302 | 2706 | List<Type> typeargtypes) { |
duke@1 | 2707 | AmbiguityError pair = this; |
duke@1 | 2708 | while (true) { |
mcimadamore@302 | 2709 | if (pair.sym.kind == AMBIGUOUS) |
mcimadamore@302 | 2710 | pair = (AmbiguityError)pair.sym; |
duke@1 | 2711 | else if (pair.sym2.kind == AMBIGUOUS) |
duke@1 | 2712 | pair = (AmbiguityError)pair.sym2; |
duke@1 | 2713 | else break; |
duke@1 | 2714 | } |
mcimadamore@302 | 2715 | Name sname = pair.sym.name; |
mcimadamore@302 | 2716 | if (sname == names.init) sname = pair.sym.owner.name; |
jjg@612 | 2717 | return diags.create(dkind, log.currentSource(), |
mcimadamore@302 | 2718 | pos, "ref.ambiguous", sname, |
mcimadamore@302 | 2719 | kindName(pair.sym), |
mcimadamore@302 | 2720 | pair.sym, |
mcimadamore@302 | 2721 | pair.sym.location(site, types), |
duke@1 | 2722 | kindName(pair.sym2), |
duke@1 | 2723 | pair.sym2, |
duke@1 | 2724 | pair.sym2.location(site, types)); |
duke@1 | 2725 | } |
duke@1 | 2726 | } |
mcimadamore@160 | 2727 | |
mcimadamore@160 | 2728 | enum MethodResolutionPhase { |
mcimadamore@160 | 2729 | BASIC(false, false), |
mcimadamore@160 | 2730 | BOX(true, false), |
mcimadamore@160 | 2731 | VARARITY(true, true); |
mcimadamore@160 | 2732 | |
mcimadamore@160 | 2733 | boolean isBoxingRequired; |
mcimadamore@160 | 2734 | boolean isVarargsRequired; |
mcimadamore@160 | 2735 | |
mcimadamore@160 | 2736 | MethodResolutionPhase(boolean isBoxingRequired, boolean isVarargsRequired) { |
mcimadamore@160 | 2737 | this.isBoxingRequired = isBoxingRequired; |
mcimadamore@160 | 2738 | this.isVarargsRequired = isVarargsRequired; |
mcimadamore@160 | 2739 | } |
mcimadamore@160 | 2740 | |
mcimadamore@160 | 2741 | public boolean isBoxingRequired() { |
mcimadamore@160 | 2742 | return isBoxingRequired; |
mcimadamore@160 | 2743 | } |
mcimadamore@160 | 2744 | |
mcimadamore@160 | 2745 | public boolean isVarargsRequired() { |
mcimadamore@160 | 2746 | return isVarargsRequired; |
mcimadamore@160 | 2747 | } |
mcimadamore@160 | 2748 | |
mcimadamore@160 | 2749 | public boolean isApplicable(boolean boxingEnabled, boolean varargsEnabled) { |
mcimadamore@160 | 2750 | return (varargsEnabled || !isVarargsRequired) && |
mcimadamore@160 | 2751 | (boxingEnabled || !isBoxingRequired); |
mcimadamore@160 | 2752 | } |
mcimadamore@160 | 2753 | } |
mcimadamore@160 | 2754 | |
mcimadamore@160 | 2755 | final List<MethodResolutionPhase> methodResolutionSteps = List.of(BASIC, BOX, VARARITY); |
mcimadamore@160 | 2756 | |
mcimadamore@1215 | 2757 | /** |
mcimadamore@1215 | 2758 | * A resolution context is used to keep track of intermediate results of |
mcimadamore@1215 | 2759 | * overload resolution, such as list of method that are not applicable |
mcimadamore@1215 | 2760 | * (used to generate more precise diagnostics) and so on. Resolution contexts |
mcimadamore@1215 | 2761 | * can be nested - this means that when each overload resolution routine should |
mcimadamore@1215 | 2762 | * work within the resolution context it created. |
mcimadamore@1215 | 2763 | */ |
mcimadamore@1215 | 2764 | class MethodResolutionContext { |
mcimadamore@689 | 2765 | |
mcimadamore@1215 | 2766 | private List<Candidate> candidates = List.nil(); |
mcimadamore@1114 | 2767 | |
mcimadamore@1215 | 2768 | private Map<MethodResolutionPhase, Symbol> resolutionCache = |
mcimadamore@1215 | 2769 | new EnumMap<MethodResolutionPhase, Symbol>(MethodResolutionPhase.class); |
mcimadamore@1215 | 2770 | |
mcimadamore@1215 | 2771 | private MethodResolutionPhase step = null; |
mcimadamore@1215 | 2772 | |
mcimadamore@1215 | 2773 | private boolean internalResolution = false; |
mcimadamore@1215 | 2774 | |
mcimadamore@1215 | 2775 | private MethodResolutionPhase firstErroneousResolutionPhase() { |
mcimadamore@1215 | 2776 | MethodResolutionPhase bestSoFar = BASIC; |
mcimadamore@1215 | 2777 | Symbol sym = methodNotFound; |
mcimadamore@1215 | 2778 | List<MethodResolutionPhase> steps = methodResolutionSteps; |
mcimadamore@1215 | 2779 | while (steps.nonEmpty() && |
mcimadamore@1215 | 2780 | steps.head.isApplicable(boxingEnabled, varargsEnabled) && |
mcimadamore@1215 | 2781 | sym.kind >= WRONG_MTHS) { |
mcimadamore@1215 | 2782 | sym = resolutionCache.get(steps.head); |
mcimadamore@1215 | 2783 | bestSoFar = steps.head; |
mcimadamore@1215 | 2784 | steps = steps.tail; |
mcimadamore@1215 | 2785 | } |
mcimadamore@1215 | 2786 | return bestSoFar; |
mcimadamore@160 | 2787 | } |
mcimadamore@1215 | 2788 | |
mcimadamore@1215 | 2789 | void addInapplicableCandidate(Symbol sym, JCDiagnostic details) { |
mcimadamore@1215 | 2790 | Candidate c = new Candidate(currentResolutionContext.step, sym, details, null); |
mcimadamore@1215 | 2791 | if (!candidates.contains(c)) |
mcimadamore@1215 | 2792 | candidates = candidates.append(c); |
mcimadamore@1215 | 2793 | } |
mcimadamore@1215 | 2794 | |
mcimadamore@1215 | 2795 | void addApplicableCandidate(Symbol sym, Type mtype) { |
mcimadamore@1215 | 2796 | Candidate c = new Candidate(currentResolutionContext.step, sym, null, mtype); |
mcimadamore@1215 | 2797 | candidates = candidates.append(c); |
mcimadamore@1215 | 2798 | } |
mcimadamore@1215 | 2799 | |
mcimadamore@1215 | 2800 | /** |
mcimadamore@1215 | 2801 | * This class represents an overload resolution candidate. There are two |
mcimadamore@1215 | 2802 | * kinds of candidates: applicable methods and inapplicable methods; |
mcimadamore@1215 | 2803 | * applicable methods have a pointer to the instantiated method type, |
mcimadamore@1215 | 2804 | * while inapplicable candidates contain further details about the |
mcimadamore@1215 | 2805 | * reason why the method has been considered inapplicable. |
mcimadamore@1215 | 2806 | */ |
mcimadamore@1215 | 2807 | class Candidate { |
mcimadamore@1215 | 2808 | |
mcimadamore@1215 | 2809 | final MethodResolutionPhase step; |
mcimadamore@1215 | 2810 | final Symbol sym; |
mcimadamore@1215 | 2811 | final JCDiagnostic details; |
mcimadamore@1215 | 2812 | final Type mtype; |
mcimadamore@1215 | 2813 | |
mcimadamore@1215 | 2814 | private Candidate(MethodResolutionPhase step, Symbol sym, JCDiagnostic details, Type mtype) { |
mcimadamore@1215 | 2815 | this.step = step; |
mcimadamore@1215 | 2816 | this.sym = sym; |
mcimadamore@1215 | 2817 | this.details = details; |
mcimadamore@1215 | 2818 | this.mtype = mtype; |
mcimadamore@1215 | 2819 | } |
mcimadamore@1215 | 2820 | |
mcimadamore@1215 | 2821 | @Override |
mcimadamore@1215 | 2822 | public boolean equals(Object o) { |
mcimadamore@1215 | 2823 | if (o instanceof Candidate) { |
mcimadamore@1215 | 2824 | Symbol s1 = this.sym; |
mcimadamore@1215 | 2825 | Symbol s2 = ((Candidate)o).sym; |
mcimadamore@1215 | 2826 | if ((s1 != s2 && |
mcimadamore@1215 | 2827 | (s1.overrides(s2, s1.owner.type.tsym, types, false) || |
mcimadamore@1215 | 2828 | (s2.overrides(s1, s2.owner.type.tsym, types, false)))) || |
mcimadamore@1215 | 2829 | ((s1.isConstructor() || s2.isConstructor()) && s1.owner != s2.owner)) |
mcimadamore@1215 | 2830 | return true; |
mcimadamore@1215 | 2831 | } |
mcimadamore@1215 | 2832 | return false; |
mcimadamore@1215 | 2833 | } |
mcimadamore@1215 | 2834 | |
mcimadamore@1215 | 2835 | boolean isApplicable() { |
mcimadamore@1215 | 2836 | return mtype != null; |
mcimadamore@1215 | 2837 | } |
mcimadamore@1215 | 2838 | } |
mcimadamore@160 | 2839 | } |
mcimadamore@1215 | 2840 | |
mcimadamore@1215 | 2841 | MethodResolutionContext currentResolutionContext = null; |
duke@1 | 2842 | } |