Mon, 18 Feb 2013 14:33:25 +0000
6563143: javac should issue a warning for overriding equals without hashCode
Reviewed-by: jjg, mcimadamore
duke@1 | 1 | /* |
jjg@1521 | 2 | * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.javac.comp; |
duke@1 | 27 | |
mcimadamore@1114 | 28 | import com.sun.tools.javac.api.Formattable.LocalizedString; |
duke@1 | 29 | import com.sun.tools.javac.code.*; |
mcimadamore@1342 | 30 | import com.sun.tools.javac.code.Symbol.*; |
mcimadamore@1114 | 31 | import com.sun.tools.javac.code.Type.*; |
mcimadamore@1238 | 32 | import com.sun.tools.javac.comp.Attr.ResultInfo; |
mcimadamore@1238 | 33 | import com.sun.tools.javac.comp.Check.CheckContext; |
mcimadamore@1347 | 34 | import com.sun.tools.javac.comp.DeferredAttr.AttrMode; |
mcimadamore@1347 | 35 | import com.sun.tools.javac.comp.DeferredAttr.DeferredAttrContext; |
mcimadamore@1347 | 36 | import com.sun.tools.javac.comp.DeferredAttr.DeferredType; |
mcimadamore@1337 | 37 | import com.sun.tools.javac.comp.Infer.InferenceContext; |
mcimadamore@1550 | 38 | import com.sun.tools.javac.comp.Infer.FreeTypeListener; |
mcimadamore@1215 | 39 | import com.sun.tools.javac.comp.Resolve.MethodResolutionContext.Candidate; |
duke@1 | 40 | import com.sun.tools.javac.jvm.*; |
duke@1 | 41 | import com.sun.tools.javac.tree.*; |
mcimadamore@1114 | 42 | import com.sun.tools.javac.tree.JCTree.*; |
mcimadamore@1352 | 43 | import com.sun.tools.javac.tree.JCTree.JCMemberReference.ReferenceKind; |
mcimadamore@1510 | 44 | import com.sun.tools.javac.tree.JCTree.JCPolyExpression.*; |
mcimadamore@1114 | 45 | import com.sun.tools.javac.util.*; |
mcimadamore@1114 | 46 | import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag; |
mcimadamore@1114 | 47 | import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
mcimadamore@1114 | 48 | import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType; |
duke@1 | 49 | |
mcimadamore@1114 | 50 | import java.util.Arrays; |
mcimadamore@1114 | 51 | import java.util.Collection; |
mcimadamore@1215 | 52 | import java.util.EnumMap; |
mcimadamore@1114 | 53 | import java.util.EnumSet; |
mcimadamore@1342 | 54 | import java.util.Iterator; |
mcimadamore@1394 | 55 | import java.util.LinkedHashMap; |
mcimadamore@1394 | 56 | import java.util.LinkedHashSet; |
mcimadamore@1114 | 57 | import java.util.Map; |
mcimadamore@1114 | 58 | |
mcimadamore@1114 | 59 | import javax.lang.model.element.ElementVisitor; |
duke@1 | 60 | |
duke@1 | 61 | import static com.sun.tools.javac.code.Flags.*; |
jjg@1127 | 62 | import static com.sun.tools.javac.code.Flags.BLOCK; |
duke@1 | 63 | import static com.sun.tools.javac.code.Kinds.*; |
jjg@1127 | 64 | import static com.sun.tools.javac.code.Kinds.ERRONEOUS; |
jjg@1374 | 65 | import static com.sun.tools.javac.code.TypeTag.*; |
mcimadamore@1114 | 66 | import static com.sun.tools.javac.comp.Resolve.MethodResolutionPhase.*; |
jjg@1127 | 67 | import static com.sun.tools.javac.tree.JCTree.Tag.*; |
mcimadamore@160 | 68 | |
duke@1 | 69 | /** Helper class for name resolution, used mostly by the attribution phase. |
duke@1 | 70 | * |
jjg@581 | 71 | * <p><b>This is NOT part of any supported API. |
jjg@581 | 72 | * If you write code that depends on this, you do so at your own risk. |
duke@1 | 73 | * This code and its internal interfaces are subject to change or |
duke@1 | 74 | * deletion without notice.</b> |
duke@1 | 75 | */ |
duke@1 | 76 | public class Resolve { |
duke@1 | 77 | protected static final Context.Key<Resolve> resolveKey = |
duke@1 | 78 | new Context.Key<Resolve>(); |
duke@1 | 79 | |
jjg@113 | 80 | Names names; |
duke@1 | 81 | Log log; |
duke@1 | 82 | Symtab syms; |
mcimadamore@1238 | 83 | Attr attr; |
mcimadamore@1347 | 84 | DeferredAttr deferredAttr; |
duke@1 | 85 | Check chk; |
duke@1 | 86 | Infer infer; |
duke@1 | 87 | ClassReader reader; |
duke@1 | 88 | TreeInfo treeinfo; |
duke@1 | 89 | Types types; |
mcimadamore@89 | 90 | JCDiagnostic.Factory diags; |
duke@1 | 91 | public final boolean boxingEnabled; // = source.allowBoxing(); |
duke@1 | 92 | public final boolean varargsEnabled; // = source.allowVarargs(); |
mcimadamore@674 | 93 | public final boolean allowMethodHandles; |
mcimadamore@1393 | 94 | public final boolean allowDefaultMethods; |
mcimadamore@1510 | 95 | public final boolean allowStructuralMostSpecific; |
duke@1 | 96 | private final boolean debugResolve; |
mcimadamore@1114 | 97 | final EnumSet<VerboseResolutionMode> verboseResolutionMode; |
duke@1 | 98 | |
mcimadamore@674 | 99 | Scope polymorphicSignatureScope; |
mcimadamore@674 | 100 | |
mcimadamore@1215 | 101 | protected Resolve(Context context) { |
mcimadamore@1215 | 102 | context.put(resolveKey, this); |
mcimadamore@1215 | 103 | syms = Symtab.instance(context); |
mcimadamore@1215 | 104 | |
mcimadamore@1215 | 105 | varNotFound = new |
mcimadamore@1215 | 106 | SymbolNotFoundError(ABSENT_VAR); |
mcimadamore@1215 | 107 | methodNotFound = new |
mcimadamore@1215 | 108 | SymbolNotFoundError(ABSENT_MTH); |
mcimadamore@1215 | 109 | typeNotFound = new |
mcimadamore@1215 | 110 | SymbolNotFoundError(ABSENT_TYP); |
mcimadamore@1215 | 111 | |
mcimadamore@1215 | 112 | names = Names.instance(context); |
mcimadamore@1215 | 113 | log = Log.instance(context); |
mcimadamore@1238 | 114 | attr = Attr.instance(context); |
mcimadamore@1347 | 115 | deferredAttr = DeferredAttr.instance(context); |
mcimadamore@1215 | 116 | chk = Check.instance(context); |
mcimadamore@1215 | 117 | infer = Infer.instance(context); |
mcimadamore@1215 | 118 | reader = ClassReader.instance(context); |
mcimadamore@1215 | 119 | treeinfo = TreeInfo.instance(context); |
mcimadamore@1215 | 120 | types = Types.instance(context); |
mcimadamore@1215 | 121 | diags = JCDiagnostic.Factory.instance(context); |
mcimadamore@1215 | 122 | Source source = Source.instance(context); |
mcimadamore@1215 | 123 | boxingEnabled = source.allowBoxing(); |
mcimadamore@1215 | 124 | varargsEnabled = source.allowVarargs(); |
mcimadamore@1215 | 125 | Options options = Options.instance(context); |
mcimadamore@1215 | 126 | debugResolve = options.isSet("debugresolve"); |
mcimadamore@1215 | 127 | verboseResolutionMode = VerboseResolutionMode.getVerboseResolutionMode(options); |
mcimadamore@1215 | 128 | Target target = Target.instance(context); |
mcimadamore@1215 | 129 | allowMethodHandles = target.hasMethodHandles(); |
mcimadamore@1393 | 130 | allowDefaultMethods = source.allowDefaultMethods(); |
mcimadamore@1510 | 131 | allowStructuralMostSpecific = source.allowStructuralMostSpecific(); |
mcimadamore@1215 | 132 | polymorphicSignatureScope = new Scope(syms.noSymbol); |
mcimadamore@1215 | 133 | |
mcimadamore@1215 | 134 | inapplicableMethodException = new InapplicableMethodException(diags); |
mcimadamore@1215 | 135 | } |
mcimadamore@1215 | 136 | |
mcimadamore@1215 | 137 | /** error symbols, which are returned when resolution fails |
mcimadamore@1215 | 138 | */ |
mcimadamore@1215 | 139 | private final SymbolNotFoundError varNotFound; |
mcimadamore@1215 | 140 | private final SymbolNotFoundError methodNotFound; |
mcimadamore@1215 | 141 | private final SymbolNotFoundError typeNotFound; |
mcimadamore@1215 | 142 | |
mcimadamore@1215 | 143 | public static Resolve instance(Context context) { |
mcimadamore@1215 | 144 | Resolve instance = context.get(resolveKey); |
mcimadamore@1215 | 145 | if (instance == null) |
mcimadamore@1215 | 146 | instance = new Resolve(context); |
mcimadamore@1215 | 147 | return instance; |
mcimadamore@1215 | 148 | } |
mcimadamore@1215 | 149 | |
mcimadamore@1215 | 150 | // <editor-fold defaultstate="collapsed" desc="Verbose resolution diagnostics support"> |
mcimadamore@1114 | 151 | enum VerboseResolutionMode { |
mcimadamore@1114 | 152 | SUCCESS("success"), |
mcimadamore@1114 | 153 | FAILURE("failure"), |
mcimadamore@1114 | 154 | APPLICABLE("applicable"), |
mcimadamore@1114 | 155 | INAPPLICABLE("inapplicable"), |
mcimadamore@1114 | 156 | DEFERRED_INST("deferred-inference"), |
mcimadamore@1114 | 157 | PREDEF("predef"), |
mcimadamore@1114 | 158 | OBJECT_INIT("object-init"), |
mcimadamore@1114 | 159 | INTERNAL("internal"); |
mcimadamore@1114 | 160 | |
vromero@1442 | 161 | final String opt; |
mcimadamore@1114 | 162 | |
mcimadamore@1114 | 163 | private VerboseResolutionMode(String opt) { |
mcimadamore@1114 | 164 | this.opt = opt; |
mcimadamore@1114 | 165 | } |
mcimadamore@1114 | 166 | |
mcimadamore@1114 | 167 | static EnumSet<VerboseResolutionMode> getVerboseResolutionMode(Options opts) { |
mcimadamore@1114 | 168 | String s = opts.get("verboseResolution"); |
mcimadamore@1114 | 169 | EnumSet<VerboseResolutionMode> res = EnumSet.noneOf(VerboseResolutionMode.class); |
mcimadamore@1114 | 170 | if (s == null) return res; |
mcimadamore@1114 | 171 | if (s.contains("all")) { |
mcimadamore@1114 | 172 | res = EnumSet.allOf(VerboseResolutionMode.class); |
mcimadamore@1114 | 173 | } |
mcimadamore@1114 | 174 | Collection<String> args = Arrays.asList(s.split(",")); |
mcimadamore@1114 | 175 | for (VerboseResolutionMode mode : values()) { |
mcimadamore@1114 | 176 | if (args.contains(mode.opt)) { |
mcimadamore@1114 | 177 | res.add(mode); |
mcimadamore@1114 | 178 | } else if (args.contains("-" + mode.opt)) { |
mcimadamore@1114 | 179 | res.remove(mode); |
mcimadamore@1114 | 180 | } |
mcimadamore@1114 | 181 | } |
mcimadamore@1114 | 182 | return res; |
mcimadamore@1114 | 183 | } |
mcimadamore@1114 | 184 | } |
mcimadamore@1114 | 185 | |
mcimadamore@1215 | 186 | void reportVerboseResolutionDiagnostic(DiagnosticPosition dpos, Name name, Type site, |
mcimadamore@1215 | 187 | List<Type> argtypes, List<Type> typeargtypes, Symbol bestSoFar) { |
mcimadamore@1215 | 188 | boolean success = bestSoFar.kind < ERRONEOUS; |
mcimadamore@1215 | 189 | |
mcimadamore@1215 | 190 | if (success && !verboseResolutionMode.contains(VerboseResolutionMode.SUCCESS)) { |
mcimadamore@1215 | 191 | return; |
mcimadamore@1215 | 192 | } else if (!success && !verboseResolutionMode.contains(VerboseResolutionMode.FAILURE)) { |
mcimadamore@1215 | 193 | return; |
mcimadamore@1215 | 194 | } |
mcimadamore@1215 | 195 | |
mcimadamore@1215 | 196 | if (bestSoFar.name == names.init && |
mcimadamore@1215 | 197 | bestSoFar.owner == syms.objectType.tsym && |
mcimadamore@1215 | 198 | !verboseResolutionMode.contains(VerboseResolutionMode.OBJECT_INIT)) { |
mcimadamore@1215 | 199 | return; //skip diags for Object constructor resolution |
mcimadamore@1215 | 200 | } else if (site == syms.predefClass.type && |
mcimadamore@1215 | 201 | !verboseResolutionMode.contains(VerboseResolutionMode.PREDEF)) { |
mcimadamore@1215 | 202 | return; //skip spurious diags for predef symbols (i.e. operators) |
mcimadamore@1215 | 203 | } else if (currentResolutionContext.internalResolution && |
mcimadamore@1215 | 204 | !verboseResolutionMode.contains(VerboseResolutionMode.INTERNAL)) { |
mcimadamore@1215 | 205 | return; |
mcimadamore@1215 | 206 | } |
mcimadamore@1215 | 207 | |
mcimadamore@1215 | 208 | int pos = 0; |
mcimadamore@1215 | 209 | int mostSpecificPos = -1; |
mcimadamore@1215 | 210 | ListBuffer<JCDiagnostic> subDiags = ListBuffer.lb(); |
mcimadamore@1215 | 211 | for (Candidate c : currentResolutionContext.candidates) { |
mcimadamore@1215 | 212 | if (currentResolutionContext.step != c.step || |
mcimadamore@1215 | 213 | (c.isApplicable() && !verboseResolutionMode.contains(VerboseResolutionMode.APPLICABLE)) || |
mcimadamore@1215 | 214 | (!c.isApplicable() && !verboseResolutionMode.contains(VerboseResolutionMode.INAPPLICABLE))) { |
mcimadamore@1215 | 215 | continue; |
mcimadamore@1215 | 216 | } else { |
mcimadamore@1215 | 217 | subDiags.append(c.isApplicable() ? |
mcimadamore@1215 | 218 | getVerboseApplicableCandidateDiag(pos, c.sym, c.mtype) : |
mcimadamore@1215 | 219 | getVerboseInapplicableCandidateDiag(pos, c.sym, c.details)); |
mcimadamore@1215 | 220 | if (c.sym == bestSoFar) |
mcimadamore@1215 | 221 | mostSpecificPos = pos; |
mcimadamore@1215 | 222 | pos++; |
mcimadamore@1215 | 223 | } |
mcimadamore@1215 | 224 | } |
mcimadamore@1215 | 225 | String key = success ? "verbose.resolve.multi" : "verbose.resolve.multi.1"; |
mcimadamore@1347 | 226 | List<Type> argtypes2 = Type.map(argtypes, |
mcimadamore@1347 | 227 | deferredAttr.new RecoveryDeferredTypeMap(AttrMode.SPECULATIVE, bestSoFar, currentResolutionContext.step)); |
mcimadamore@1215 | 228 | JCDiagnostic main = diags.note(log.currentSource(), dpos, key, name, |
mcimadamore@1215 | 229 | site.tsym, mostSpecificPos, currentResolutionContext.step, |
mcimadamore@1347 | 230 | methodArguments(argtypes2), |
mcimadamore@1347 | 231 | methodArguments(typeargtypes)); |
mcimadamore@1215 | 232 | JCDiagnostic d = new JCDiagnostic.MultilineDiagnostic(main, subDiags.toList()); |
mcimadamore@1215 | 233 | log.report(d); |
duke@1 | 234 | } |
duke@1 | 235 | |
mcimadamore@1215 | 236 | JCDiagnostic getVerboseApplicableCandidateDiag(int pos, Symbol sym, Type inst) { |
mcimadamore@1215 | 237 | JCDiagnostic subDiag = null; |
jjg@1374 | 238 | if (sym.type.hasTag(FORALL)) { |
mcimadamore@1268 | 239 | subDiag = diags.fragment("partial.inst.sig", inst); |
mcimadamore@1215 | 240 | } |
duke@1 | 241 | |
mcimadamore@1215 | 242 | String key = subDiag == null ? |
mcimadamore@1215 | 243 | "applicable.method.found" : |
mcimadamore@1215 | 244 | "applicable.method.found.1"; |
duke@1 | 245 | |
mcimadamore@1215 | 246 | return diags.fragment(key, pos, sym, subDiag); |
duke@1 | 247 | } |
duke@1 | 248 | |
mcimadamore@1215 | 249 | JCDiagnostic getVerboseInapplicableCandidateDiag(int pos, Symbol sym, JCDiagnostic subDiag) { |
mcimadamore@1215 | 250 | return diags.fragment("not.applicable.method.found", pos, sym, subDiag); |
mcimadamore@1215 | 251 | } |
mcimadamore@1215 | 252 | // </editor-fold> |
duke@1 | 253 | |
duke@1 | 254 | /* ************************************************************************ |
duke@1 | 255 | * Identifier resolution |
duke@1 | 256 | *************************************************************************/ |
duke@1 | 257 | |
duke@1 | 258 | /** An environment is "static" if its static level is greater than |
duke@1 | 259 | * the one of its outer environment |
duke@1 | 260 | */ |
ksrini@1346 | 261 | protected static boolean isStatic(Env<AttrContext> env) { |
duke@1 | 262 | return env.info.staticLevel > env.outer.info.staticLevel; |
duke@1 | 263 | } |
duke@1 | 264 | |
duke@1 | 265 | /** An environment is an "initializer" if it is a constructor or |
duke@1 | 266 | * an instance initializer. |
duke@1 | 267 | */ |
duke@1 | 268 | static boolean isInitializer(Env<AttrContext> env) { |
duke@1 | 269 | Symbol owner = env.info.scope.owner; |
duke@1 | 270 | return owner.isConstructor() || |
duke@1 | 271 | owner.owner.kind == TYP && |
duke@1 | 272 | (owner.kind == VAR || |
duke@1 | 273 | owner.kind == MTH && (owner.flags() & BLOCK) != 0) && |
duke@1 | 274 | (owner.flags() & STATIC) == 0; |
duke@1 | 275 | } |
duke@1 | 276 | |
duke@1 | 277 | /** Is class accessible in given evironment? |
duke@1 | 278 | * @param env The current environment. |
duke@1 | 279 | * @param c The class whose accessibility is checked. |
duke@1 | 280 | */ |
duke@1 | 281 | public boolean isAccessible(Env<AttrContext> env, TypeSymbol c) { |
mcimadamore@741 | 282 | return isAccessible(env, c, false); |
mcimadamore@741 | 283 | } |
mcimadamore@741 | 284 | |
mcimadamore@741 | 285 | public boolean isAccessible(Env<AttrContext> env, TypeSymbol c, boolean checkInner) { |
mcimadamore@741 | 286 | boolean isAccessible = false; |
duke@1 | 287 | switch ((short)(c.flags() & AccessFlags)) { |
mcimadamore@741 | 288 | case PRIVATE: |
mcimadamore@741 | 289 | isAccessible = |
mcimadamore@741 | 290 | env.enclClass.sym.outermostClass() == |
mcimadamore@741 | 291 | c.owner.outermostClass(); |
mcimadamore@741 | 292 | break; |
mcimadamore@741 | 293 | case 0: |
mcimadamore@741 | 294 | isAccessible = |
mcimadamore@741 | 295 | env.toplevel.packge == c.owner // fast special case |
mcimadamore@741 | 296 | || |
mcimadamore@741 | 297 | env.toplevel.packge == c.packge() |
mcimadamore@741 | 298 | || |
mcimadamore@741 | 299 | // Hack: this case is added since synthesized default constructors |
mcimadamore@741 | 300 | // of anonymous classes should be allowed to access |
mcimadamore@741 | 301 | // classes which would be inaccessible otherwise. |
mcimadamore@741 | 302 | env.enclMethod != null && |
mcimadamore@741 | 303 | (env.enclMethod.mods.flags & ANONCONSTR) != 0; |
mcimadamore@741 | 304 | break; |
mcimadamore@741 | 305 | default: // error recovery |
mcimadamore@741 | 306 | case PUBLIC: |
mcimadamore@741 | 307 | isAccessible = true; |
mcimadamore@741 | 308 | break; |
mcimadamore@741 | 309 | case PROTECTED: |
mcimadamore@741 | 310 | isAccessible = |
mcimadamore@741 | 311 | env.toplevel.packge == c.owner // fast special case |
mcimadamore@741 | 312 | || |
mcimadamore@741 | 313 | env.toplevel.packge == c.packge() |
mcimadamore@741 | 314 | || |
mcimadamore@741 | 315 | isInnerSubClass(env.enclClass.sym, c.owner); |
mcimadamore@741 | 316 | break; |
duke@1 | 317 | } |
mcimadamore@741 | 318 | return (checkInner == false || c.type.getEnclosingType() == Type.noType) ? |
mcimadamore@741 | 319 | isAccessible : |
jjg@789 | 320 | isAccessible && isAccessible(env, c.type.getEnclosingType(), checkInner); |
duke@1 | 321 | } |
duke@1 | 322 | //where |
duke@1 | 323 | /** Is given class a subclass of given base class, or an inner class |
duke@1 | 324 | * of a subclass? |
duke@1 | 325 | * Return null if no such class exists. |
duke@1 | 326 | * @param c The class which is the subclass or is contained in it. |
duke@1 | 327 | * @param base The base class |
duke@1 | 328 | */ |
duke@1 | 329 | private boolean isInnerSubClass(ClassSymbol c, Symbol base) { |
duke@1 | 330 | while (c != null && !c.isSubClass(base, types)) { |
duke@1 | 331 | c = c.owner.enclClass(); |
duke@1 | 332 | } |
duke@1 | 333 | return c != null; |
duke@1 | 334 | } |
duke@1 | 335 | |
duke@1 | 336 | boolean isAccessible(Env<AttrContext> env, Type t) { |
mcimadamore@741 | 337 | return isAccessible(env, t, false); |
mcimadamore@741 | 338 | } |
mcimadamore@741 | 339 | |
mcimadamore@741 | 340 | boolean isAccessible(Env<AttrContext> env, Type t, boolean checkInner) { |
jjg@1374 | 341 | return (t.hasTag(ARRAY)) |
duke@1 | 342 | ? isAccessible(env, types.elemtype(t)) |
mcimadamore@741 | 343 | : isAccessible(env, t.tsym, checkInner); |
duke@1 | 344 | } |
duke@1 | 345 | |
duke@1 | 346 | /** Is symbol accessible as a member of given type in given evironment? |
duke@1 | 347 | * @param env The current environment. |
duke@1 | 348 | * @param site The type of which the tested symbol is regarded |
duke@1 | 349 | * as a member. |
duke@1 | 350 | * @param sym The symbol. |
duke@1 | 351 | */ |
duke@1 | 352 | public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym) { |
mcimadamore@741 | 353 | return isAccessible(env, site, sym, false); |
mcimadamore@741 | 354 | } |
mcimadamore@741 | 355 | public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym, boolean checkInner) { |
duke@1 | 356 | if (sym.name == names.init && sym.owner != site.tsym) return false; |
duke@1 | 357 | switch ((short)(sym.flags() & AccessFlags)) { |
duke@1 | 358 | case PRIVATE: |
duke@1 | 359 | return |
duke@1 | 360 | (env.enclClass.sym == sym.owner // fast special case |
duke@1 | 361 | || |
duke@1 | 362 | env.enclClass.sym.outermostClass() == |
duke@1 | 363 | sym.owner.outermostClass()) |
duke@1 | 364 | && |
duke@1 | 365 | sym.isInheritedIn(site.tsym, types); |
duke@1 | 366 | case 0: |
duke@1 | 367 | return |
duke@1 | 368 | (env.toplevel.packge == sym.owner.owner // fast special case |
duke@1 | 369 | || |
duke@1 | 370 | env.toplevel.packge == sym.packge()) |
duke@1 | 371 | && |
mcimadamore@741 | 372 | isAccessible(env, site, checkInner) |
duke@1 | 373 | && |
mcimadamore@254 | 374 | sym.isInheritedIn(site.tsym, types) |
mcimadamore@254 | 375 | && |
mcimadamore@254 | 376 | notOverriddenIn(site, sym); |
duke@1 | 377 | case PROTECTED: |
duke@1 | 378 | return |
duke@1 | 379 | (env.toplevel.packge == sym.owner.owner // fast special case |
duke@1 | 380 | || |
duke@1 | 381 | env.toplevel.packge == sym.packge() |
duke@1 | 382 | || |
duke@1 | 383 | isProtectedAccessible(sym, env.enclClass.sym, site) |
duke@1 | 384 | || |
duke@1 | 385 | // OK to select instance method or field from 'super' or type name |
duke@1 | 386 | // (but type names should be disallowed elsewhere!) |
duke@1 | 387 | env.info.selectSuper && (sym.flags() & STATIC) == 0 && sym.kind != TYP) |
duke@1 | 388 | && |
mcimadamore@741 | 389 | isAccessible(env, site, checkInner) |
duke@1 | 390 | && |
mcimadamore@254 | 391 | notOverriddenIn(site, sym); |
duke@1 | 392 | default: // this case includes erroneous combinations as well |
mcimadamore@741 | 393 | return isAccessible(env, site, checkInner) && notOverriddenIn(site, sym); |
mcimadamore@254 | 394 | } |
mcimadamore@254 | 395 | } |
mcimadamore@254 | 396 | //where |
mcimadamore@254 | 397 | /* `sym' is accessible only if not overridden by |
mcimadamore@254 | 398 | * another symbol which is a member of `site' |
mcimadamore@254 | 399 | * (because, if it is overridden, `sym' is not strictly |
mcimadamore@674 | 400 | * speaking a member of `site'). A polymorphic signature method |
mcimadamore@674 | 401 | * cannot be overridden (e.g. MH.invokeExact(Object[])). |
mcimadamore@254 | 402 | */ |
mcimadamore@254 | 403 | private boolean notOverriddenIn(Type site, Symbol sym) { |
mcimadamore@254 | 404 | if (sym.kind != MTH || sym.isConstructor() || sym.isStatic()) |
mcimadamore@254 | 405 | return true; |
mcimadamore@254 | 406 | else { |
mcimadamore@254 | 407 | Symbol s2 = ((MethodSymbol)sym).implementation(site.tsym, types, true); |
mcimadamore@844 | 408 | return (s2 == null || s2 == sym || sym.owner == s2.owner || |
mcimadamore@325 | 409 | !types.isSubSignature(types.memberType(site, s2), types.memberType(site, sym))); |
duke@1 | 410 | } |
duke@1 | 411 | } |
duke@1 | 412 | //where |
duke@1 | 413 | /** Is given protected symbol accessible if it is selected from given site |
duke@1 | 414 | * and the selection takes place in given class? |
duke@1 | 415 | * @param sym The symbol with protected access |
duke@1 | 416 | * @param c The class where the access takes place |
duke@1 | 417 | * @site The type of the qualifier |
duke@1 | 418 | */ |
duke@1 | 419 | private |
duke@1 | 420 | boolean isProtectedAccessible(Symbol sym, ClassSymbol c, Type site) { |
duke@1 | 421 | while (c != null && |
duke@1 | 422 | !(c.isSubClass(sym.owner, types) && |
duke@1 | 423 | (c.flags() & INTERFACE) == 0 && |
duke@1 | 424 | // In JLS 2e 6.6.2.1, the subclass restriction applies |
duke@1 | 425 | // only to instance fields and methods -- types are excluded |
duke@1 | 426 | // regardless of whether they are declared 'static' or not. |
duke@1 | 427 | ((sym.flags() & STATIC) != 0 || sym.kind == TYP || site.tsym.isSubClass(c, types)))) |
duke@1 | 428 | c = c.owner.enclClass(); |
duke@1 | 429 | return c != null; |
duke@1 | 430 | } |
duke@1 | 431 | |
mcimadamore@1415 | 432 | /** |
mcimadamore@1415 | 433 | * Performs a recursive scan of a type looking for accessibility problems |
mcimadamore@1415 | 434 | * from current attribution environment |
mcimadamore@1415 | 435 | */ |
mcimadamore@1415 | 436 | void checkAccessibleType(Env<AttrContext> env, Type t) { |
mcimadamore@1415 | 437 | accessibilityChecker.visit(t, env); |
mcimadamore@1415 | 438 | } |
mcimadamore@1415 | 439 | |
mcimadamore@1415 | 440 | /** |
mcimadamore@1415 | 441 | * Accessibility type-visitor |
mcimadamore@1415 | 442 | */ |
mcimadamore@1415 | 443 | Types.SimpleVisitor<Void, Env<AttrContext>> accessibilityChecker = |
mcimadamore@1415 | 444 | new Types.SimpleVisitor<Void, Env<AttrContext>>() { |
mcimadamore@1415 | 445 | |
mcimadamore@1415 | 446 | void visit(List<Type> ts, Env<AttrContext> env) { |
mcimadamore@1415 | 447 | for (Type t : ts) { |
mcimadamore@1415 | 448 | visit(t, env); |
mcimadamore@1415 | 449 | } |
mcimadamore@1415 | 450 | } |
mcimadamore@1415 | 451 | |
mcimadamore@1415 | 452 | public Void visitType(Type t, Env<AttrContext> env) { |
mcimadamore@1415 | 453 | return null; |
mcimadamore@1415 | 454 | } |
mcimadamore@1415 | 455 | |
mcimadamore@1415 | 456 | @Override |
mcimadamore@1415 | 457 | public Void visitArrayType(ArrayType t, Env<AttrContext> env) { |
mcimadamore@1415 | 458 | visit(t.elemtype, env); |
mcimadamore@1415 | 459 | return null; |
mcimadamore@1415 | 460 | } |
mcimadamore@1415 | 461 | |
mcimadamore@1415 | 462 | @Override |
mcimadamore@1415 | 463 | public Void visitClassType(ClassType t, Env<AttrContext> env) { |
mcimadamore@1415 | 464 | visit(t.getTypeArguments(), env); |
mcimadamore@1415 | 465 | if (!isAccessible(env, t, true)) { |
mcimadamore@1415 | 466 | accessBase(new AccessError(t.tsym), env.tree.pos(), env.enclClass.sym, t, t.tsym.name, true); |
mcimadamore@1415 | 467 | } |
mcimadamore@1415 | 468 | return null; |
mcimadamore@1415 | 469 | } |
mcimadamore@1415 | 470 | |
mcimadamore@1415 | 471 | @Override |
mcimadamore@1415 | 472 | public Void visitWildcardType(WildcardType t, Env<AttrContext> env) { |
mcimadamore@1415 | 473 | visit(t.type, env); |
mcimadamore@1415 | 474 | return null; |
mcimadamore@1415 | 475 | } |
mcimadamore@1415 | 476 | |
mcimadamore@1415 | 477 | @Override |
mcimadamore@1415 | 478 | public Void visitMethodType(MethodType t, Env<AttrContext> env) { |
mcimadamore@1415 | 479 | visit(t.getParameterTypes(), env); |
mcimadamore@1415 | 480 | visit(t.getReturnType(), env); |
mcimadamore@1415 | 481 | visit(t.getThrownTypes(), env); |
mcimadamore@1415 | 482 | return null; |
mcimadamore@1415 | 483 | } |
mcimadamore@1415 | 484 | }; |
mcimadamore@1415 | 485 | |
duke@1 | 486 | /** Try to instantiate the type of a method so that it fits |
duke@1 | 487 | * given type arguments and argument types. If succesful, return |
duke@1 | 488 | * the method's instantiated type, else return null. |
duke@1 | 489 | * The instantiation will take into account an additional leading |
duke@1 | 490 | * formal parameter if the method is an instance method seen as a member |
duke@1 | 491 | * of un underdetermined site In this case, we treat site as an additional |
duke@1 | 492 | * parameter and the parameters of the class containing the method as |
duke@1 | 493 | * additional type variables that get instantiated. |
duke@1 | 494 | * |
duke@1 | 495 | * @param env The current environment |
duke@1 | 496 | * @param site The type of which the method is a member. |
duke@1 | 497 | * @param m The method symbol. |
duke@1 | 498 | * @param argtypes The invocation's given value arguments. |
duke@1 | 499 | * @param typeargtypes The invocation's given type arguments. |
duke@1 | 500 | * @param allowBoxing Allow boxing conversions of arguments. |
duke@1 | 501 | * @param useVarargs Box trailing arguments into an array for varargs. |
duke@1 | 502 | */ |
duke@1 | 503 | Type rawInstantiate(Env<AttrContext> env, |
duke@1 | 504 | Type site, |
duke@1 | 505 | Symbol m, |
mcimadamore@1268 | 506 | ResultInfo resultInfo, |
duke@1 | 507 | List<Type> argtypes, |
duke@1 | 508 | List<Type> typeargtypes, |
duke@1 | 509 | boolean allowBoxing, |
duke@1 | 510 | boolean useVarargs, |
mcimadamore@1479 | 511 | MethodCheck methodCheck, |
mcimadamore@1394 | 512 | Warner warn) throws Infer.InferenceException { |
mcimadamore@1394 | 513 | |
duke@1 | 514 | Type mt = types.memberType(site, m); |
duke@1 | 515 | // tvars is the list of formal type variables for which type arguments |
duke@1 | 516 | // need to inferred. |
mcimadamore@1268 | 517 | List<Type> tvars = List.nil(); |
duke@1 | 518 | if (typeargtypes == null) typeargtypes = List.nil(); |
jjg@1374 | 519 | if (!mt.hasTag(FORALL) && typeargtypes.nonEmpty()) { |
duke@1 | 520 | // This is not a polymorphic method, but typeargs are supplied |
jjh@972 | 521 | // which is fine, see JLS 15.12.2.1 |
jjg@1374 | 522 | } else if (mt.hasTag(FORALL) && typeargtypes.nonEmpty()) { |
duke@1 | 523 | ForAll pmt = (ForAll) mt; |
duke@1 | 524 | if (typeargtypes.length() != pmt.tvars.length()) |
mcimadamore@689 | 525 | throw inapplicableMethodException.setMessage("arg.length.mismatch"); // not enough args |
duke@1 | 526 | // Check type arguments are within bounds |
duke@1 | 527 | List<Type> formals = pmt.tvars; |
duke@1 | 528 | List<Type> actuals = typeargtypes; |
duke@1 | 529 | while (formals.nonEmpty() && actuals.nonEmpty()) { |
duke@1 | 530 | List<Type> bounds = types.subst(types.getBounds((TypeVar)formals.head), |
duke@1 | 531 | pmt.tvars, typeargtypes); |
duke@1 | 532 | for (; bounds.nonEmpty(); bounds = bounds.tail) |
duke@1 | 533 | if (!types.isSubtypeUnchecked(actuals.head, bounds.head, warn)) |
mcimadamore@689 | 534 | throw inapplicableMethodException.setMessage("explicit.param.do.not.conform.to.bounds",actuals.head, bounds); |
duke@1 | 535 | formals = formals.tail; |
duke@1 | 536 | actuals = actuals.tail; |
duke@1 | 537 | } |
duke@1 | 538 | mt = types.subst(pmt.qtype, pmt.tvars, typeargtypes); |
jjg@1374 | 539 | } else if (mt.hasTag(FORALL)) { |
duke@1 | 540 | ForAll pmt = (ForAll) mt; |
duke@1 | 541 | List<Type> tvars1 = types.newInstances(pmt.tvars); |
duke@1 | 542 | tvars = tvars.appendList(tvars1); |
duke@1 | 543 | mt = types.subst(pmt.qtype, pmt.tvars, tvars1); |
duke@1 | 544 | } |
duke@1 | 545 | |
duke@1 | 546 | // find out whether we need to go the slow route via infer |
mcimadamore@1239 | 547 | boolean instNeeded = tvars.tail != null; /*inlined: tvars.nonEmpty()*/ |
duke@1 | 548 | for (List<Type> l = argtypes; |
duke@1 | 549 | l.tail != null/*inlined: l.nonEmpty()*/ && !instNeeded; |
duke@1 | 550 | l = l.tail) { |
jjg@1374 | 551 | if (l.head.hasTag(FORALL)) instNeeded = true; |
duke@1 | 552 | } |
duke@1 | 553 | |
duke@1 | 554 | if (instNeeded) |
mcimadamore@1239 | 555 | return infer.instantiateMethod(env, |
mcimadamore@547 | 556 | tvars, |
duke@1 | 557 | (MethodType)mt, |
mcimadamore@1268 | 558 | resultInfo, |
mcimadamore@580 | 559 | m, |
duke@1 | 560 | argtypes, |
duke@1 | 561 | allowBoxing, |
duke@1 | 562 | useVarargs, |
mcimadamore@1347 | 563 | currentResolutionContext, |
mcimadamore@1479 | 564 | methodCheck, |
duke@1 | 565 | warn); |
mcimadamore@689 | 566 | |
mcimadamore@1551 | 567 | methodCheck.argumentsAcceptable(env, currentResolutionContext.deferredAttrContext(m, infer.emptyContext, resultInfo, warn), |
mcimadamore@1479 | 568 | argtypes, mt.getParameterTypes(), warn); |
mcimadamore@689 | 569 | return mt; |
duke@1 | 570 | } |
duke@1 | 571 | |
mcimadamore@1347 | 572 | Type checkMethod(Env<AttrContext> env, |
mcimadamore@1347 | 573 | Type site, |
mcimadamore@1347 | 574 | Symbol m, |
mcimadamore@1347 | 575 | ResultInfo resultInfo, |
mcimadamore@1347 | 576 | List<Type> argtypes, |
mcimadamore@1347 | 577 | List<Type> typeargtypes, |
mcimadamore@1347 | 578 | Warner warn) { |
mcimadamore@1347 | 579 | MethodResolutionContext prevContext = currentResolutionContext; |
mcimadamore@1347 | 580 | try { |
mcimadamore@1347 | 581 | currentResolutionContext = new MethodResolutionContext(); |
mcimadamore@1347 | 582 | currentResolutionContext.attrMode = DeferredAttr.AttrMode.CHECK; |
mcimadamore@1347 | 583 | MethodResolutionPhase step = currentResolutionContext.step = env.info.pendingResolutionPhase; |
mcimadamore@1347 | 584 | return rawInstantiate(env, site, m, resultInfo, argtypes, typeargtypes, |
mcimadamore@1479 | 585 | step.isBoxingRequired(), step.isVarargsRequired(), resolveMethodCheck, warn); |
mcimadamore@1347 | 586 | } |
mcimadamore@1347 | 587 | finally { |
mcimadamore@1347 | 588 | currentResolutionContext = prevContext; |
mcimadamore@1347 | 589 | } |
mcimadamore@1347 | 590 | } |
mcimadamore@1347 | 591 | |
duke@1 | 592 | /** Same but returns null instead throwing a NoInstanceException |
duke@1 | 593 | */ |
duke@1 | 594 | Type instantiate(Env<AttrContext> env, |
duke@1 | 595 | Type site, |
duke@1 | 596 | Symbol m, |
mcimadamore@1268 | 597 | ResultInfo resultInfo, |
duke@1 | 598 | List<Type> argtypes, |
duke@1 | 599 | List<Type> typeargtypes, |
duke@1 | 600 | boolean allowBoxing, |
duke@1 | 601 | boolean useVarargs, |
mcimadamore@1479 | 602 | MethodCheck methodCheck, |
duke@1 | 603 | Warner warn) { |
duke@1 | 604 | try { |
mcimadamore@1268 | 605 | return rawInstantiate(env, site, m, resultInfo, argtypes, typeargtypes, |
mcimadamore@1479 | 606 | allowBoxing, useVarargs, methodCheck, warn); |
mcimadamore@689 | 607 | } catch (InapplicableMethodException ex) { |
duke@1 | 608 | return null; |
duke@1 | 609 | } |
duke@1 | 610 | } |
duke@1 | 611 | |
mcimadamore@1479 | 612 | /** |
mcimadamore@1479 | 613 | * This interface defines an entry point that should be used to perform a |
mcimadamore@1479 | 614 | * method check. A method check usually consist in determining as to whether |
mcimadamore@1479 | 615 | * a set of types (actuals) is compatible with another set of types (formals). |
mcimadamore@1479 | 616 | * Since the notion of compatibility can vary depending on the circumstances, |
mcimadamore@1479 | 617 | * this interfaces allows to easily add new pluggable method check routines. |
duke@1 | 618 | */ |
mcimadamore@1479 | 619 | interface MethodCheck { |
mcimadamore@1479 | 620 | /** |
mcimadamore@1479 | 621 | * Main method check routine. A method check usually consist in determining |
mcimadamore@1479 | 622 | * as to whether a set of types (actuals) is compatible with another set of |
mcimadamore@1479 | 623 | * types (formals). If an incompatibility is found, an unchecked exception |
mcimadamore@1479 | 624 | * is assumed to be thrown. |
mcimadamore@1479 | 625 | */ |
mcimadamore@1479 | 626 | void argumentsAcceptable(Env<AttrContext> env, |
mcimadamore@1479 | 627 | DeferredAttrContext deferredAttrContext, |
mcimadamore@845 | 628 | List<Type> argtypes, |
duke@1 | 629 | List<Type> formals, |
mcimadamore@1479 | 630 | Warner warn); |
mcimadamore@1479 | 631 | } |
mcimadamore@1479 | 632 | |
mcimadamore@1479 | 633 | /** |
mcimadamore@1479 | 634 | * Helper enum defining all method check diagnostics (used by resolveMethodCheck). |
mcimadamore@1479 | 635 | */ |
mcimadamore@1479 | 636 | enum MethodCheckDiag { |
mcimadamore@1479 | 637 | /** |
mcimadamore@1479 | 638 | * Actuals and formals differs in length. |
mcimadamore@1479 | 639 | */ |
mcimadamore@1479 | 640 | ARITY_MISMATCH("arg.length.mismatch", "infer.arg.length.mismatch"), |
mcimadamore@1479 | 641 | /** |
mcimadamore@1479 | 642 | * An actual is incompatible with a formal. |
mcimadamore@1479 | 643 | */ |
mcimadamore@1479 | 644 | ARG_MISMATCH("no.conforming.assignment.exists", "infer.no.conforming.assignment.exists"), |
mcimadamore@1479 | 645 | /** |
mcimadamore@1479 | 646 | * An actual is incompatible with the varargs element type. |
mcimadamore@1479 | 647 | */ |
mcimadamore@1479 | 648 | VARARG_MISMATCH("varargs.argument.mismatch", "infer.varargs.argument.mismatch"), |
mcimadamore@1479 | 649 | /** |
mcimadamore@1479 | 650 | * The varargs element type is inaccessible. |
mcimadamore@1479 | 651 | */ |
mcimadamore@1479 | 652 | INACCESSIBLE_VARARGS("inaccessible.varargs.type", "inaccessible.varargs.type"); |
mcimadamore@1479 | 653 | |
mcimadamore@1479 | 654 | final String basicKey; |
mcimadamore@1479 | 655 | final String inferKey; |
mcimadamore@1479 | 656 | |
mcimadamore@1479 | 657 | MethodCheckDiag(String basicKey, String inferKey) { |
mcimadamore@1479 | 658 | this.basicKey = basicKey; |
mcimadamore@1479 | 659 | this.inferKey = inferKey; |
mcimadamore@689 | 660 | } |
mcimadamore@689 | 661 | } |
mcimadamore@1186 | 662 | |
mcimadamore@1186 | 663 | /** |
mcimadamore@1186 | 664 | * Main method applicability routine. Given a list of actual types A, |
mcimadamore@1186 | 665 | * a list of formal types F, determines whether the types in A are |
mcimadamore@1186 | 666 | * compatible (by method invocation conversion) with the types in F. |
mcimadamore@1186 | 667 | * |
mcimadamore@1186 | 668 | * Since this routine is shared between overload resolution and method |
mcimadamore@1347 | 669 | * type-inference, a (possibly empty) inference context is used to convert |
mcimadamore@1347 | 670 | * formal types to the corresponding 'undet' form ahead of a compatibility |
mcimadamore@1347 | 671 | * check so that constraints can be propagated and collected. |
mcimadamore@1186 | 672 | * |
mcimadamore@1347 | 673 | * Moreover, if one or more types in A is a deferred type, this routine uses |
mcimadamore@1347 | 674 | * DeferredAttr in order to perform deferred attribution. If one or more actual |
mcimadamore@1347 | 675 | * deferred types are stuck, they are placed in a queue and revisited later |
mcimadamore@1347 | 676 | * after the remainder of the arguments have been seen. If this is not sufficient |
mcimadamore@1347 | 677 | * to 'unstuck' the argument, a cyclic inference error is called out. |
mcimadamore@1186 | 678 | * |
mcimadamore@1186 | 679 | * A method check handler (see above) is used in order to report errors. |
mcimadamore@1186 | 680 | */ |
mcimadamore@1479 | 681 | MethodCheck resolveMethodCheck = new MethodCheck() { |
mcimadamore@1479 | 682 | @Override |
mcimadamore@1479 | 683 | public void argumentsAcceptable(final Env<AttrContext> env, |
mcimadamore@1479 | 684 | DeferredAttrContext deferredAttrContext, |
mcimadamore@1479 | 685 | List<Type> argtypes, |
mcimadamore@1479 | 686 | List<Type> formals, |
mcimadamore@1479 | 687 | Warner warn) { |
mcimadamore@1479 | 688 | //should we expand formals? |
mcimadamore@1479 | 689 | boolean useVarargs = deferredAttrContext.phase.isVarargsRequired(); |
mcimadamore@1479 | 690 | |
mcimadamore@1479 | 691 | //inference context used during this method check |
mcimadamore@1479 | 692 | InferenceContext inferenceContext = deferredAttrContext.inferenceContext; |
mcimadamore@1479 | 693 | |
mcimadamore@1479 | 694 | Type varargsFormal = useVarargs ? formals.last() : null; |
mcimadamore@1479 | 695 | |
mcimadamore@1479 | 696 | if (varargsFormal == null && |
mcimadamore@1479 | 697 | argtypes.size() != formals.size()) { |
jjg@1521 | 698 | reportMC(MethodCheckDiag.ARITY_MISMATCH, inferenceContext); // not enough args |
mcimadamore@1479 | 699 | } |
mcimadamore@1479 | 700 | |
mcimadamore@1479 | 701 | while (argtypes.nonEmpty() && formals.head != varargsFormal) { |
mcimadamore@1479 | 702 | ResultInfo mresult = methodCheckResult(false, formals.head, deferredAttrContext, warn); |
mcimadamore@1347 | 703 | mresult.check(null, argtypes.head); |
mcimadamore@689 | 704 | argtypes = argtypes.tail; |
mcimadamore@1479 | 705 | formals = formals.tail; |
mcimadamore@689 | 706 | } |
mcimadamore@1479 | 707 | |
mcimadamore@1479 | 708 | if (formals.head != varargsFormal) { |
jjg@1521 | 709 | reportMC(MethodCheckDiag.ARITY_MISMATCH, inferenceContext); // not enough args |
mcimadamore@1479 | 710 | } |
mcimadamore@1479 | 711 | |
mcimadamore@1479 | 712 | if (useVarargs) { |
mcimadamore@1479 | 713 | //note: if applicability check is triggered by most specific test, |
mcimadamore@1479 | 714 | //the last argument of a varargs is _not_ an array type (see JLS 15.12.2.5) |
mcimadamore@1479 | 715 | final Type elt = types.elemtype(varargsFormal); |
mcimadamore@1479 | 716 | ResultInfo mresult = methodCheckResult(true, elt, deferredAttrContext, warn); |
mcimadamore@1479 | 717 | while (argtypes.nonEmpty()) { |
mcimadamore@1479 | 718 | mresult.check(null, argtypes.head); |
mcimadamore@1479 | 719 | argtypes = argtypes.tail; |
mcimadamore@1337 | 720 | } |
mcimadamore@1479 | 721 | //check varargs element type accessibility |
mcimadamore@1479 | 722 | varargsAccessible(env, elt, inferenceContext); |
mcimadamore@845 | 723 | } |
duke@1 | 724 | } |
mcimadamore@1479 | 725 | |
jjg@1521 | 726 | private void reportMC(MethodCheckDiag diag, InferenceContext inferenceContext, Object... args) { |
mcimadamore@1479 | 727 | boolean inferDiag = inferenceContext != infer.emptyContext; |
mcimadamore@1479 | 728 | InapplicableMethodException ex = inferDiag ? |
mcimadamore@1479 | 729 | infer.inferenceException : inapplicableMethodException; |
mcimadamore@1479 | 730 | if (inferDiag && (!diag.inferKey.equals(diag.basicKey))) { |
mcimadamore@1479 | 731 | Object[] args2 = new Object[args.length + 1]; |
mcimadamore@1479 | 732 | System.arraycopy(args, 0, args2, 1, args.length); |
mcimadamore@1479 | 733 | args2[0] = inferenceContext.inferenceVars(); |
mcimadamore@1479 | 734 | args = args2; |
mcimadamore@1479 | 735 | } |
mcimadamore@1479 | 736 | throw ex.setMessage(inferDiag ? diag.inferKey : diag.basicKey, args); |
mcimadamore@1479 | 737 | } |
mcimadamore@1479 | 738 | |
mcimadamore@1479 | 739 | private void varargsAccessible(final Env<AttrContext> env, final Type t, final InferenceContext inferenceContext) { |
mcimadamore@1479 | 740 | if (inferenceContext.free(t)) { |
mcimadamore@1479 | 741 | inferenceContext.addFreeTypeListener(List.of(t), new FreeTypeListener() { |
mcimadamore@1479 | 742 | @Override |
mcimadamore@1479 | 743 | public void typesInferred(InferenceContext inferenceContext) { |
mcimadamore@1550 | 744 | varargsAccessible(env, inferenceContext.asInstType(t), inferenceContext); |
mcimadamore@1479 | 745 | } |
mcimadamore@1479 | 746 | }); |
mcimadamore@1479 | 747 | } else { |
mcimadamore@1479 | 748 | if (!isAccessible(env, t)) { |
mcimadamore@1479 | 749 | Symbol location = env.enclClass.sym; |
jjg@1521 | 750 | reportMC(MethodCheckDiag.INACCESSIBLE_VARARGS, inferenceContext, t, Kinds.kindName(location), location); |
mcimadamore@1479 | 751 | } |
mcimadamore@1479 | 752 | } |
mcimadamore@1479 | 753 | } |
mcimadamore@1479 | 754 | |
mcimadamore@1479 | 755 | private ResultInfo methodCheckResult(final boolean varargsCheck, Type to, |
mcimadamore@1479 | 756 | final DeferredAttr.DeferredAttrContext deferredAttrContext, Warner rsWarner) { |
mcimadamore@1479 | 757 | CheckContext checkContext = new MethodCheckContext(!deferredAttrContext.phase.isBoxingRequired(), deferredAttrContext, rsWarner) { |
mcimadamore@1479 | 758 | MethodCheckDiag methodDiag = varargsCheck ? |
mcimadamore@1479 | 759 | MethodCheckDiag.VARARG_MISMATCH : MethodCheckDiag.ARG_MISMATCH; |
mcimadamore@1479 | 760 | |
mcimadamore@1479 | 761 | @Override |
mcimadamore@1479 | 762 | public void report(DiagnosticPosition pos, JCDiagnostic details) { |
jjg@1521 | 763 | reportMC(methodDiag, deferredAttrContext.inferenceContext, details); |
mcimadamore@1479 | 764 | } |
mcimadamore@1479 | 765 | }; |
mcimadamore@1479 | 766 | return new MethodResultInfo(to, checkContext); |
mcimadamore@1479 | 767 | } |
mcimadamore@1479 | 768 | }; |
mcimadamore@689 | 769 | |
mcimadamore@1238 | 770 | /** |
mcimadamore@1238 | 771 | * Check context to be used during method applicability checks. A method check |
mcimadamore@1238 | 772 | * context might contain inference variables. |
mcimadamore@1238 | 773 | */ |
mcimadamore@1238 | 774 | abstract class MethodCheckContext implements CheckContext { |
mcimadamore@689 | 775 | |
mcimadamore@1479 | 776 | boolean strict; |
mcimadamore@1347 | 777 | DeferredAttrContext deferredAttrContext; |
mcimadamore@1238 | 778 | Warner rsWarner; |
mcimadamore@1238 | 779 | |
mcimadamore@1479 | 780 | public MethodCheckContext(boolean strict, DeferredAttrContext deferredAttrContext, Warner rsWarner) { |
mcimadamore@1479 | 781 | this.strict = strict; |
mcimadamore@1479 | 782 | this.deferredAttrContext = deferredAttrContext; |
mcimadamore@1479 | 783 | this.rsWarner = rsWarner; |
mcimadamore@1238 | 784 | } |
mcimadamore@1238 | 785 | |
mcimadamore@1479 | 786 | public boolean compatible(Type found, Type req, Warner warn) { |
mcimadamore@1479 | 787 | return strict ? |
mcimadamore@1550 | 788 | types.isSubtypeUnchecked(found, deferredAttrContext.inferenceContext.asFree(req), warn) : |
mcimadamore@1550 | 789 | types.isConvertible(found, deferredAttrContext.inferenceContext.asFree(req), warn); |
mcimadamore@1479 | 790 | } |
mcimadamore@1479 | 791 | |
mcimadamore@1296 | 792 | public void report(DiagnosticPosition pos, JCDiagnostic details) { |
mcimadamore@1479 | 793 | throw inapplicableMethodException.setMessage(details); |
mcimadamore@1238 | 794 | } |
mcimadamore@1238 | 795 | |
mcimadamore@1238 | 796 | public Warner checkWarner(DiagnosticPosition pos, Type found, Type req) { |
mcimadamore@1238 | 797 | return rsWarner; |
mcimadamore@1238 | 798 | } |
mcimadamore@1337 | 799 | |
mcimadamore@1337 | 800 | public InferenceContext inferenceContext() { |
mcimadamore@1479 | 801 | return deferredAttrContext.inferenceContext; |
mcimadamore@1337 | 802 | } |
mcimadamore@1347 | 803 | |
mcimadamore@1347 | 804 | public DeferredAttrContext deferredAttrContext() { |
mcimadamore@1347 | 805 | return deferredAttrContext; |
mcimadamore@1347 | 806 | } |
mcimadamore@1238 | 807 | } |
mcimadamore@1238 | 808 | |
mcimadamore@1238 | 809 | /** |
mcimadamore@1479 | 810 | * ResultInfo class to be used during method applicability checks. Check |
mcimadamore@1479 | 811 | * for deferred types goes through special path. |
mcimadamore@1238 | 812 | */ |
mcimadamore@1347 | 813 | class MethodResultInfo extends ResultInfo { |
mcimadamore@1347 | 814 | |
mcimadamore@1479 | 815 | public MethodResultInfo(Type pt, CheckContext checkContext) { |
mcimadamore@1347 | 816 | attr.super(VAL, pt, checkContext); |
mcimadamore@1347 | 817 | } |
mcimadamore@1347 | 818 | |
mcimadamore@1347 | 819 | @Override |
mcimadamore@1347 | 820 | protected Type check(DiagnosticPosition pos, Type found) { |
jjg@1374 | 821 | if (found.hasTag(DEFERRED)) { |
mcimadamore@1347 | 822 | DeferredType dt = (DeferredType)found; |
mcimadamore@1347 | 823 | return dt.check(this); |
mcimadamore@1347 | 824 | } else { |
mcimadamore@1338 | 825 | return super.check(pos, chk.checkNonVoid(pos, types.capture(types.upperBound(found.baseType())))); |
mcimadamore@689 | 826 | } |
mcimadamore@1347 | 827 | } |
mcimadamore@1347 | 828 | |
mcimadamore@1347 | 829 | @Override |
mcimadamore@1347 | 830 | protected MethodResultInfo dup(Type newPt) { |
mcimadamore@1479 | 831 | return new MethodResultInfo(newPt, checkContext); |
mcimadamore@1415 | 832 | } |
mcimadamore@1415 | 833 | |
mcimadamore@1415 | 834 | @Override |
mcimadamore@1415 | 835 | protected ResultInfo dup(CheckContext newContext) { |
mcimadamore@1479 | 836 | return new MethodResultInfo(pt, newContext); |
mcimadamore@1347 | 837 | } |
mcimadamore@1238 | 838 | } |
mcimadamore@689 | 839 | |
mcimadamore@1510 | 840 | /** |
mcimadamore@1510 | 841 | * Most specific method applicability routine. Given a list of actual types A, |
mcimadamore@1510 | 842 | * a list of formal types F1, and a list of formal types F2, the routine determines |
mcimadamore@1510 | 843 | * as to whether the types in F1 can be considered more specific than those in F2 w.r.t. |
mcimadamore@1510 | 844 | * argument types A. |
mcimadamore@1510 | 845 | */ |
mcimadamore@1510 | 846 | class MostSpecificCheck implements MethodCheck { |
mcimadamore@1510 | 847 | |
mcimadamore@1510 | 848 | boolean strict; |
mcimadamore@1510 | 849 | List<Type> actuals; |
mcimadamore@1510 | 850 | |
mcimadamore@1510 | 851 | MostSpecificCheck(boolean strict, List<Type> actuals) { |
mcimadamore@1510 | 852 | this.strict = strict; |
mcimadamore@1510 | 853 | this.actuals = actuals; |
mcimadamore@1510 | 854 | } |
mcimadamore@1510 | 855 | |
mcimadamore@1510 | 856 | @Override |
mcimadamore@1510 | 857 | public void argumentsAcceptable(final Env<AttrContext> env, |
mcimadamore@1510 | 858 | DeferredAttrContext deferredAttrContext, |
mcimadamore@1510 | 859 | List<Type> formals1, |
mcimadamore@1510 | 860 | List<Type> formals2, |
mcimadamore@1510 | 861 | Warner warn) { |
mcimadamore@1510 | 862 | formals2 = adjustArgs(formals2, deferredAttrContext.msym, formals1.length(), deferredAttrContext.phase.isVarargsRequired()); |
mcimadamore@1510 | 863 | while (formals2.nonEmpty()) { |
mcimadamore@1510 | 864 | ResultInfo mresult = methodCheckResult(formals2.head, deferredAttrContext, warn, actuals.head); |
mcimadamore@1510 | 865 | mresult.check(null, formals1.head); |
mcimadamore@1510 | 866 | formals1 = formals1.tail; |
mcimadamore@1510 | 867 | formals2 = formals2.tail; |
mcimadamore@1510 | 868 | actuals = actuals.isEmpty() ? actuals : actuals.tail; |
mcimadamore@1510 | 869 | } |
mcimadamore@1510 | 870 | } |
mcimadamore@1510 | 871 | |
mcimadamore@1510 | 872 | /** |
mcimadamore@1510 | 873 | * Create a method check context to be used during the most specific applicability check |
mcimadamore@1510 | 874 | */ |
mcimadamore@1510 | 875 | ResultInfo methodCheckResult(Type to, DeferredAttr.DeferredAttrContext deferredAttrContext, |
mcimadamore@1510 | 876 | Warner rsWarner, Type actual) { |
mcimadamore@1510 | 877 | return attr.new ResultInfo(Kinds.VAL, to, |
mcimadamore@1510 | 878 | new MostSpecificCheckContext(strict, deferredAttrContext, rsWarner, actual)); |
mcimadamore@1510 | 879 | } |
mcimadamore@1510 | 880 | |
mcimadamore@1510 | 881 | /** |
mcimadamore@1510 | 882 | * Subclass of method check context class that implements most specific |
mcimadamore@1510 | 883 | * method conversion. If the actual type under analysis is a deferred type |
mcimadamore@1510 | 884 | * a full blown structural analysis is carried out. |
mcimadamore@1510 | 885 | */ |
mcimadamore@1510 | 886 | class MostSpecificCheckContext extends MethodCheckContext { |
mcimadamore@1510 | 887 | |
mcimadamore@1510 | 888 | Type actual; |
mcimadamore@1510 | 889 | |
mcimadamore@1510 | 890 | public MostSpecificCheckContext(boolean strict, DeferredAttrContext deferredAttrContext, Warner rsWarner, Type actual) { |
mcimadamore@1510 | 891 | super(strict, deferredAttrContext, rsWarner); |
mcimadamore@1510 | 892 | this.actual = actual; |
mcimadamore@1510 | 893 | } |
mcimadamore@1510 | 894 | |
mcimadamore@1510 | 895 | public boolean compatible(Type found, Type req, Warner warn) { |
mcimadamore@1510 | 896 | if (!allowStructuralMostSpecific || actual == null) { |
mcimadamore@1510 | 897 | return super.compatible(found, req, warn); |
mcimadamore@1510 | 898 | } else { |
mcimadamore@1510 | 899 | switch (actual.getTag()) { |
mcimadamore@1510 | 900 | case DEFERRED: |
mcimadamore@1510 | 901 | DeferredType dt = (DeferredType) actual; |
mcimadamore@1510 | 902 | DeferredType.SpeculativeCache.Entry e = dt.speculativeCache.get(deferredAttrContext.msym, deferredAttrContext.phase); |
mcimadamore@1510 | 903 | return (e == null || e.speculativeTree == deferredAttr.stuckTree) |
mcimadamore@1510 | 904 | ? false : mostSpecific(found, req, e.speculativeTree, warn); |
mcimadamore@1510 | 905 | default: |
mcimadamore@1510 | 906 | return standaloneMostSpecific(found, req, actual, warn); |
mcimadamore@1510 | 907 | } |
mcimadamore@1510 | 908 | } |
mcimadamore@1510 | 909 | } |
mcimadamore@1510 | 910 | |
mcimadamore@1510 | 911 | private boolean mostSpecific(Type t, Type s, JCTree tree, Warner warn) { |
mcimadamore@1510 | 912 | MostSpecificChecker msc = new MostSpecificChecker(t, s, warn); |
mcimadamore@1510 | 913 | msc.scan(tree); |
mcimadamore@1510 | 914 | return msc.result; |
mcimadamore@1510 | 915 | } |
mcimadamore@1510 | 916 | |
mcimadamore@1510 | 917 | boolean polyMostSpecific(Type t1, Type t2, Warner warn) { |
mcimadamore@1510 | 918 | return (!t1.isPrimitive() && t2.isPrimitive()) |
mcimadamore@1510 | 919 | ? true : super.compatible(t1, t2, warn); |
mcimadamore@1510 | 920 | } |
mcimadamore@1510 | 921 | |
mcimadamore@1510 | 922 | boolean standaloneMostSpecific(Type t1, Type t2, Type exprType, Warner warn) { |
mcimadamore@1510 | 923 | return (exprType.isPrimitive() == t1.isPrimitive() |
mcimadamore@1510 | 924 | && exprType.isPrimitive() != t2.isPrimitive()) |
mcimadamore@1510 | 925 | ? true : super.compatible(t1, t2, warn); |
mcimadamore@1510 | 926 | } |
mcimadamore@1510 | 927 | |
mcimadamore@1510 | 928 | /** |
mcimadamore@1510 | 929 | * Structural checker for most specific. |
mcimadamore@1510 | 930 | */ |
mcimadamore@1510 | 931 | class MostSpecificChecker extends DeferredAttr.PolyScanner { |
mcimadamore@1510 | 932 | |
mcimadamore@1510 | 933 | final Type t; |
mcimadamore@1510 | 934 | final Type s; |
mcimadamore@1510 | 935 | final Warner warn; |
mcimadamore@1510 | 936 | boolean result; |
mcimadamore@1510 | 937 | |
mcimadamore@1510 | 938 | MostSpecificChecker(Type t, Type s, Warner warn) { |
mcimadamore@1510 | 939 | this.t = t; |
mcimadamore@1510 | 940 | this.s = s; |
mcimadamore@1510 | 941 | this.warn = warn; |
mcimadamore@1510 | 942 | result = true; |
mcimadamore@1510 | 943 | } |
mcimadamore@1510 | 944 | |
mcimadamore@1510 | 945 | @Override |
mcimadamore@1510 | 946 | void skip(JCTree tree) { |
mcimadamore@1510 | 947 | result &= standaloneMostSpecific(t, s, tree.type, warn); |
mcimadamore@1510 | 948 | } |
mcimadamore@1510 | 949 | |
mcimadamore@1510 | 950 | @Override |
mcimadamore@1510 | 951 | public void visitConditional(JCConditional tree) { |
mcimadamore@1510 | 952 | if (tree.polyKind == PolyKind.STANDALONE) { |
mcimadamore@1510 | 953 | result &= standaloneMostSpecific(t, s, tree.type, warn); |
mcimadamore@1510 | 954 | } else { |
mcimadamore@1510 | 955 | super.visitConditional(tree); |
mcimadamore@1510 | 956 | } |
mcimadamore@1510 | 957 | } |
mcimadamore@1510 | 958 | |
mcimadamore@1510 | 959 | @Override |
mcimadamore@1510 | 960 | public void visitApply(JCMethodInvocation tree) { |
mcimadamore@1510 | 961 | result &= (tree.polyKind == PolyKind.STANDALONE) |
mcimadamore@1510 | 962 | ? standaloneMostSpecific(t, s, tree.type, warn) |
mcimadamore@1510 | 963 | : polyMostSpecific(t, s, warn); |
mcimadamore@1510 | 964 | } |
mcimadamore@1510 | 965 | |
mcimadamore@1510 | 966 | @Override |
mcimadamore@1510 | 967 | public void visitNewClass(JCNewClass tree) { |
mcimadamore@1510 | 968 | result &= (tree.polyKind == PolyKind.STANDALONE) |
mcimadamore@1510 | 969 | ? standaloneMostSpecific(t, s, tree.type, warn) |
mcimadamore@1510 | 970 | : polyMostSpecific(t, s, warn); |
mcimadamore@1510 | 971 | } |
mcimadamore@1510 | 972 | |
mcimadamore@1510 | 973 | @Override |
mcimadamore@1510 | 974 | public void visitReference(JCMemberReference tree) { |
mcimadamore@1510 | 975 | if (types.isFunctionalInterface(t.tsym) && |
mcimadamore@1510 | 976 | types.isFunctionalInterface(s.tsym) && |
mcimadamore@1510 | 977 | types.asSuper(t, s.tsym) == null && |
mcimadamore@1510 | 978 | types.asSuper(s, t.tsym) == null) { |
mcimadamore@1510 | 979 | Type desc_t = types.findDescriptorType(t); |
mcimadamore@1510 | 980 | Type desc_s = types.findDescriptorType(s); |
mcimadamore@1510 | 981 | if (types.isSameTypes(desc_t.getParameterTypes(), desc_s.getParameterTypes())) { |
mcimadamore@1510 | 982 | if (!desc_s.getReturnType().hasTag(VOID)) { |
mcimadamore@1510 | 983 | //perform structural comparison |
mcimadamore@1510 | 984 | Type ret_t = desc_t.getReturnType(); |
mcimadamore@1510 | 985 | Type ret_s = desc_s.getReturnType(); |
mcimadamore@1510 | 986 | result &= ((tree.refPolyKind == PolyKind.STANDALONE) |
mcimadamore@1510 | 987 | ? standaloneMostSpecific(ret_t, ret_s, tree.type, warn) |
mcimadamore@1510 | 988 | : polyMostSpecific(ret_t, ret_s, warn)); |
mcimadamore@1510 | 989 | } else { |
mcimadamore@1510 | 990 | return; |
mcimadamore@1510 | 991 | } |
mcimadamore@1510 | 992 | } else { |
mcimadamore@1510 | 993 | result &= false; |
mcimadamore@1510 | 994 | } |
mcimadamore@1510 | 995 | } else { |
mcimadamore@1510 | 996 | result &= MostSpecificCheckContext.super.compatible(t, s, warn); |
mcimadamore@1510 | 997 | } |
mcimadamore@1510 | 998 | } |
mcimadamore@1510 | 999 | |
mcimadamore@1510 | 1000 | @Override |
mcimadamore@1510 | 1001 | public void visitLambda(JCLambda tree) { |
mcimadamore@1510 | 1002 | if (types.isFunctionalInterface(t.tsym) && |
mcimadamore@1510 | 1003 | types.isFunctionalInterface(s.tsym) && |
mcimadamore@1510 | 1004 | types.asSuper(t, s.tsym) == null && |
mcimadamore@1510 | 1005 | types.asSuper(s, t.tsym) == null) { |
mcimadamore@1510 | 1006 | Type desc_t = types.findDescriptorType(t); |
mcimadamore@1510 | 1007 | Type desc_s = types.findDescriptorType(s); |
mcimadamore@1510 | 1008 | if (tree.paramKind == JCLambda.ParameterKind.EXPLICIT |
mcimadamore@1510 | 1009 | || types.isSameTypes(desc_t.getParameterTypes(), desc_s.getParameterTypes())) { |
mcimadamore@1510 | 1010 | if (!desc_s.getReturnType().hasTag(VOID)) { |
mcimadamore@1510 | 1011 | //perform structural comparison |
mcimadamore@1510 | 1012 | Type ret_t = desc_t.getReturnType(); |
mcimadamore@1510 | 1013 | Type ret_s = desc_s.getReturnType(); |
mcimadamore@1510 | 1014 | scanLambdaBody(tree, ret_t, ret_s); |
mcimadamore@1510 | 1015 | } else { |
mcimadamore@1510 | 1016 | return; |
mcimadamore@1510 | 1017 | } |
mcimadamore@1510 | 1018 | } else { |
mcimadamore@1510 | 1019 | result &= false; |
mcimadamore@1510 | 1020 | } |
mcimadamore@1510 | 1021 | } else { |
mcimadamore@1510 | 1022 | result &= MostSpecificCheckContext.super.compatible(t, s, warn); |
mcimadamore@1510 | 1023 | } |
mcimadamore@1510 | 1024 | } |
mcimadamore@1510 | 1025 | //where |
mcimadamore@1510 | 1026 | |
mcimadamore@1510 | 1027 | void scanLambdaBody(JCLambda lambda, final Type t, final Type s) { |
mcimadamore@1510 | 1028 | if (lambda.getBodyKind() == JCTree.JCLambda.BodyKind.EXPRESSION) { |
mcimadamore@1510 | 1029 | result &= MostSpecificCheckContext.this.mostSpecific(t, s, lambda.body, warn); |
mcimadamore@1510 | 1030 | } else { |
mcimadamore@1510 | 1031 | DeferredAttr.LambdaReturnScanner lambdaScanner = |
mcimadamore@1510 | 1032 | new DeferredAttr.LambdaReturnScanner() { |
mcimadamore@1510 | 1033 | @Override |
mcimadamore@1510 | 1034 | public void visitReturn(JCReturn tree) { |
mcimadamore@1510 | 1035 | if (tree.expr != null) { |
mcimadamore@1510 | 1036 | result &= MostSpecificCheckContext.this.mostSpecific(t, s, tree.expr, warn); |
mcimadamore@1510 | 1037 | } |
mcimadamore@1510 | 1038 | } |
mcimadamore@1510 | 1039 | }; |
mcimadamore@1510 | 1040 | lambdaScanner.scan(lambda.body); |
mcimadamore@1510 | 1041 | } |
mcimadamore@1510 | 1042 | } |
mcimadamore@1510 | 1043 | } |
mcimadamore@1510 | 1044 | } |
mcimadamore@1510 | 1045 | } |
mcimadamore@1510 | 1046 | |
mcimadamore@1238 | 1047 | public static class InapplicableMethodException extends RuntimeException { |
mcimadamore@1238 | 1048 | private static final long serialVersionUID = 0; |
mcimadamore@1238 | 1049 | |
mcimadamore@1238 | 1050 | JCDiagnostic diagnostic; |
mcimadamore@1238 | 1051 | JCDiagnostic.Factory diags; |
mcimadamore@1238 | 1052 | |
mcimadamore@1238 | 1053 | InapplicableMethodException(JCDiagnostic.Factory diags) { |
mcimadamore@1238 | 1054 | this.diagnostic = null; |
mcimadamore@1238 | 1055 | this.diags = diags; |
mcimadamore@689 | 1056 | } |
mcimadamore@1238 | 1057 | InapplicableMethodException setMessage() { |
mcimadamore@1337 | 1058 | return setMessage((JCDiagnostic)null); |
mcimadamore@1238 | 1059 | } |
mcimadamore@1238 | 1060 | InapplicableMethodException setMessage(String key) { |
mcimadamore@1337 | 1061 | return setMessage(key != null ? diags.fragment(key) : null); |
mcimadamore@1238 | 1062 | } |
mcimadamore@1238 | 1063 | InapplicableMethodException setMessage(String key, Object... args) { |
mcimadamore@1337 | 1064 | return setMessage(key != null ? diags.fragment(key, args) : null); |
mcimadamore@1238 | 1065 | } |
mcimadamore@1238 | 1066 | InapplicableMethodException setMessage(JCDiagnostic diag) { |
mcimadamore@1238 | 1067 | this.diagnostic = diag; |
mcimadamore@1238 | 1068 | return this; |
mcimadamore@1238 | 1069 | } |
mcimadamore@1238 | 1070 | |
mcimadamore@1238 | 1071 | public JCDiagnostic getDiagnostic() { |
mcimadamore@1238 | 1072 | return diagnostic; |
mcimadamore@1238 | 1073 | } |
mcimadamore@1238 | 1074 | } |
mcimadamore@1238 | 1075 | private final InapplicableMethodException inapplicableMethodException; |
duke@1 | 1076 | |
duke@1 | 1077 | /* *************************************************************************** |
duke@1 | 1078 | * Symbol lookup |
duke@1 | 1079 | * the following naming conventions for arguments are used |
duke@1 | 1080 | * |
duke@1 | 1081 | * env is the environment where the symbol was mentioned |
duke@1 | 1082 | * site is the type of which the symbol is a member |
duke@1 | 1083 | * name is the symbol's name |
duke@1 | 1084 | * if no arguments are given |
duke@1 | 1085 | * argtypes are the value arguments, if we search for a method |
duke@1 | 1086 | * |
duke@1 | 1087 | * If no symbol was found, a ResolveError detailing the problem is returned. |
duke@1 | 1088 | ****************************************************************************/ |
duke@1 | 1089 | |
duke@1 | 1090 | /** Find field. Synthetic fields are always skipped. |
duke@1 | 1091 | * @param env The current environment. |
duke@1 | 1092 | * @param site The original type from where the selection takes place. |
duke@1 | 1093 | * @param name The name of the field. |
duke@1 | 1094 | * @param c The class to search for the field. This is always |
duke@1 | 1095 | * a superclass or implemented interface of site's class. |
duke@1 | 1096 | */ |
duke@1 | 1097 | Symbol findField(Env<AttrContext> env, |
duke@1 | 1098 | Type site, |
duke@1 | 1099 | Name name, |
duke@1 | 1100 | TypeSymbol c) { |
jjg@1374 | 1101 | while (c.type.hasTag(TYPEVAR)) |
mcimadamore@19 | 1102 | c = c.type.getUpperBound().tsym; |
duke@1 | 1103 | Symbol bestSoFar = varNotFound; |
duke@1 | 1104 | Symbol sym; |
duke@1 | 1105 | Scope.Entry e = c.members().lookup(name); |
duke@1 | 1106 | while (e.scope != null) { |
duke@1 | 1107 | if (e.sym.kind == VAR && (e.sym.flags_field & SYNTHETIC) == 0) { |
duke@1 | 1108 | return isAccessible(env, site, e.sym) |
duke@1 | 1109 | ? e.sym : new AccessError(env, site, e.sym); |
duke@1 | 1110 | } |
duke@1 | 1111 | e = e.next(); |
duke@1 | 1112 | } |
duke@1 | 1113 | Type st = types.supertype(c.type); |
jjg@1374 | 1114 | if (st != null && (st.hasTag(CLASS) || st.hasTag(TYPEVAR))) { |
duke@1 | 1115 | sym = findField(env, site, name, st.tsym); |
duke@1 | 1116 | if (sym.kind < bestSoFar.kind) bestSoFar = sym; |
duke@1 | 1117 | } |
duke@1 | 1118 | for (List<Type> l = types.interfaces(c.type); |
duke@1 | 1119 | bestSoFar.kind != AMBIGUOUS && l.nonEmpty(); |
duke@1 | 1120 | l = l.tail) { |
duke@1 | 1121 | sym = findField(env, site, name, l.head.tsym); |
duke@1 | 1122 | if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS && |
duke@1 | 1123 | sym.owner != bestSoFar.owner) |
duke@1 | 1124 | bestSoFar = new AmbiguityError(bestSoFar, sym); |
duke@1 | 1125 | else if (sym.kind < bestSoFar.kind) |
duke@1 | 1126 | bestSoFar = sym; |
duke@1 | 1127 | } |
duke@1 | 1128 | return bestSoFar; |
duke@1 | 1129 | } |
duke@1 | 1130 | |
duke@1 | 1131 | /** Resolve a field identifier, throw a fatal error if not found. |
duke@1 | 1132 | * @param pos The position to use for error reporting. |
duke@1 | 1133 | * @param env The environment current at the method invocation. |
duke@1 | 1134 | * @param site The type of the qualifying expression, in which |
duke@1 | 1135 | * identifier is searched. |
duke@1 | 1136 | * @param name The identifier's name. |
duke@1 | 1137 | */ |
duke@1 | 1138 | public VarSymbol resolveInternalField(DiagnosticPosition pos, Env<AttrContext> env, |
duke@1 | 1139 | Type site, Name name) { |
duke@1 | 1140 | Symbol sym = findField(env, site, name, site.tsym); |
duke@1 | 1141 | if (sym.kind == VAR) return (VarSymbol)sym; |
duke@1 | 1142 | else throw new FatalError( |
mcimadamore@89 | 1143 | diags.fragment("fatal.err.cant.locate.field", |
duke@1 | 1144 | name)); |
duke@1 | 1145 | } |
duke@1 | 1146 | |
duke@1 | 1147 | /** Find unqualified variable or field with given name. |
duke@1 | 1148 | * Synthetic fields always skipped. |
duke@1 | 1149 | * @param env The current environment. |
duke@1 | 1150 | * @param name The name of the variable or field. |
duke@1 | 1151 | */ |
duke@1 | 1152 | Symbol findVar(Env<AttrContext> env, Name name) { |
duke@1 | 1153 | Symbol bestSoFar = varNotFound; |
duke@1 | 1154 | Symbol sym; |
duke@1 | 1155 | Env<AttrContext> env1 = env; |
duke@1 | 1156 | boolean staticOnly = false; |
duke@1 | 1157 | while (env1.outer != null) { |
duke@1 | 1158 | if (isStatic(env1)) staticOnly = true; |
duke@1 | 1159 | Scope.Entry e = env1.info.scope.lookup(name); |
duke@1 | 1160 | while (e.scope != null && |
duke@1 | 1161 | (e.sym.kind != VAR || |
duke@1 | 1162 | (e.sym.flags_field & SYNTHETIC) != 0)) |
duke@1 | 1163 | e = e.next(); |
duke@1 | 1164 | sym = (e.scope != null) |
duke@1 | 1165 | ? e.sym |
duke@1 | 1166 | : findField( |
duke@1 | 1167 | env1, env1.enclClass.sym.type, name, env1.enclClass.sym); |
duke@1 | 1168 | if (sym.exists()) { |
duke@1 | 1169 | if (staticOnly && |
duke@1 | 1170 | sym.kind == VAR && |
duke@1 | 1171 | sym.owner.kind == TYP && |
duke@1 | 1172 | (sym.flags() & STATIC) == 0) |
duke@1 | 1173 | return new StaticError(sym); |
duke@1 | 1174 | else |
duke@1 | 1175 | return sym; |
duke@1 | 1176 | } else if (sym.kind < bestSoFar.kind) { |
duke@1 | 1177 | bestSoFar = sym; |
duke@1 | 1178 | } |
duke@1 | 1179 | |
duke@1 | 1180 | if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true; |
duke@1 | 1181 | env1 = env1.outer; |
duke@1 | 1182 | } |
duke@1 | 1183 | |
duke@1 | 1184 | sym = findField(env, syms.predefClass.type, name, syms.predefClass); |
duke@1 | 1185 | if (sym.exists()) |
duke@1 | 1186 | return sym; |
duke@1 | 1187 | if (bestSoFar.exists()) |
duke@1 | 1188 | return bestSoFar; |
duke@1 | 1189 | |
duke@1 | 1190 | Scope.Entry e = env.toplevel.namedImportScope.lookup(name); |
duke@1 | 1191 | for (; e.scope != null; e = e.next()) { |
duke@1 | 1192 | sym = e.sym; |
duke@1 | 1193 | Type origin = e.getOrigin().owner.type; |
duke@1 | 1194 | if (sym.kind == VAR) { |
duke@1 | 1195 | if (e.sym.owner.type != origin) |
duke@1 | 1196 | sym = sym.clone(e.getOrigin().owner); |
duke@1 | 1197 | return isAccessible(env, origin, sym) |
duke@1 | 1198 | ? sym : new AccessError(env, origin, sym); |
duke@1 | 1199 | } |
duke@1 | 1200 | } |
duke@1 | 1201 | |
duke@1 | 1202 | Symbol origin = null; |
duke@1 | 1203 | e = env.toplevel.starImportScope.lookup(name); |
duke@1 | 1204 | for (; e.scope != null; e = e.next()) { |
duke@1 | 1205 | sym = e.sym; |
duke@1 | 1206 | if (sym.kind != VAR) |
duke@1 | 1207 | continue; |
duke@1 | 1208 | // invariant: sym.kind == VAR |
duke@1 | 1209 | if (bestSoFar.kind < AMBIGUOUS && sym.owner != bestSoFar.owner) |
duke@1 | 1210 | return new AmbiguityError(bestSoFar, sym); |
duke@1 | 1211 | else if (bestSoFar.kind >= VAR) { |
duke@1 | 1212 | origin = e.getOrigin().owner; |
duke@1 | 1213 | bestSoFar = isAccessible(env, origin.type, sym) |
duke@1 | 1214 | ? sym : new AccessError(env, origin.type, sym); |
duke@1 | 1215 | } |
duke@1 | 1216 | } |
duke@1 | 1217 | if (bestSoFar.kind == VAR && bestSoFar.owner.type != origin.type) |
duke@1 | 1218 | return bestSoFar.clone(origin); |
duke@1 | 1219 | else |
duke@1 | 1220 | return bestSoFar; |
duke@1 | 1221 | } |
duke@1 | 1222 | |
duke@1 | 1223 | Warner noteWarner = new Warner(); |
duke@1 | 1224 | |
duke@1 | 1225 | /** Select the best method for a call site among two choices. |
duke@1 | 1226 | * @param env The current environment. |
duke@1 | 1227 | * @param site The original type from where the |
duke@1 | 1228 | * selection takes place. |
duke@1 | 1229 | * @param argtypes The invocation's value arguments, |
duke@1 | 1230 | * @param typeargtypes The invocation's type arguments, |
duke@1 | 1231 | * @param sym Proposed new best match. |
duke@1 | 1232 | * @param bestSoFar Previously found best match. |
duke@1 | 1233 | * @param allowBoxing Allow boxing conversions of arguments. |
duke@1 | 1234 | * @param useVarargs Box trailing arguments into an array for varargs. |
duke@1 | 1235 | */ |
mcimadamore@689 | 1236 | @SuppressWarnings("fallthrough") |
duke@1 | 1237 | Symbol selectBest(Env<AttrContext> env, |
duke@1 | 1238 | Type site, |
duke@1 | 1239 | List<Type> argtypes, |
duke@1 | 1240 | List<Type> typeargtypes, |
duke@1 | 1241 | Symbol sym, |
duke@1 | 1242 | Symbol bestSoFar, |
duke@1 | 1243 | boolean allowBoxing, |
duke@1 | 1244 | boolean useVarargs, |
duke@1 | 1245 | boolean operator) { |
mcimadamore@1394 | 1246 | if (sym.kind == ERR || |
mcimadamore@1394 | 1247 | !sym.isInheritedIn(site.tsym, types) || |
mcimadamore@1394 | 1248 | (useVarargs && (sym.flags() & VARARGS) == 0)) { |
mcimadamore@1394 | 1249 | return bestSoFar; |
mcimadamore@1394 | 1250 | } |
jjg@816 | 1251 | Assert.check(sym.kind < AMBIGUOUS); |
duke@1 | 1252 | try { |
mcimadamore@1268 | 1253 | Type mt = rawInstantiate(env, site, sym, null, argtypes, typeargtypes, |
mcimadamore@1479 | 1254 | allowBoxing, useVarargs, resolveMethodCheck, types.noWarnings); |
mcimadamore@1215 | 1255 | if (!operator) |
mcimadamore@1215 | 1256 | currentResolutionContext.addApplicableCandidate(sym, mt); |
mcimadamore@689 | 1257 | } catch (InapplicableMethodException ex) { |
mcimadamore@1215 | 1258 | if (!operator) |
mcimadamore@1215 | 1259 | currentResolutionContext.addInapplicableCandidate(sym, ex.getDiagnostic()); |
duke@1 | 1260 | switch (bestSoFar.kind) { |
mcimadamore@1394 | 1261 | case ABSENT_MTH: |
mcimadamore@1394 | 1262 | return new InapplicableSymbolError(currentResolutionContext); |
mcimadamore@1394 | 1263 | case WRONG_MTH: |
mcimadamore@1394 | 1264 | if (operator) return bestSoFar; |
mcimadamore@1394 | 1265 | bestSoFar = new InapplicableSymbolsError(currentResolutionContext); |
mcimadamore@1394 | 1266 | default: |
mcimadamore@1394 | 1267 | return bestSoFar; |
duke@1 | 1268 | } |
duke@1 | 1269 | } |
duke@1 | 1270 | if (!isAccessible(env, site, sym)) { |
duke@1 | 1271 | return (bestSoFar.kind == ABSENT_MTH) |
duke@1 | 1272 | ? new AccessError(env, site, sym) |
duke@1 | 1273 | : bestSoFar; |
mcimadamore@1215 | 1274 | } |
duke@1 | 1275 | return (bestSoFar.kind > AMBIGUOUS) |
duke@1 | 1276 | ? sym |
mcimadamore@1348 | 1277 | : mostSpecific(argtypes, sym, bestSoFar, env, site, |
duke@1 | 1278 | allowBoxing && operator, useVarargs); |
duke@1 | 1279 | } |
duke@1 | 1280 | |
duke@1 | 1281 | /* Return the most specific of the two methods for a call, |
duke@1 | 1282 | * given that both are accessible and applicable. |
duke@1 | 1283 | * @param m1 A new candidate for most specific. |
duke@1 | 1284 | * @param m2 The previous most specific candidate. |
duke@1 | 1285 | * @param env The current environment. |
duke@1 | 1286 | * @param site The original type from where the selection |
duke@1 | 1287 | * takes place. |
duke@1 | 1288 | * @param allowBoxing Allow boxing conversions of arguments. |
duke@1 | 1289 | * @param useVarargs Box trailing arguments into an array for varargs. |
duke@1 | 1290 | */ |
mcimadamore@1348 | 1291 | Symbol mostSpecific(List<Type> argtypes, Symbol m1, |
duke@1 | 1292 | Symbol m2, |
duke@1 | 1293 | Env<AttrContext> env, |
mcimadamore@254 | 1294 | final Type site, |
duke@1 | 1295 | boolean allowBoxing, |
duke@1 | 1296 | boolean useVarargs) { |
duke@1 | 1297 | switch (m2.kind) { |
duke@1 | 1298 | case MTH: |
duke@1 | 1299 | if (m1 == m2) return m1; |
mcimadamore@1348 | 1300 | boolean m1SignatureMoreSpecific = |
mcimadamore@1348 | 1301 | signatureMoreSpecific(argtypes, env, site, m1, m2, allowBoxing, useVarargs); |
mcimadamore@1348 | 1302 | boolean m2SignatureMoreSpecific = |
mcimadamore@1348 | 1303 | signatureMoreSpecific(argtypes, env, site, m2, m1, allowBoxing, useVarargs); |
duke@1 | 1304 | if (m1SignatureMoreSpecific && m2SignatureMoreSpecific) { |
mcimadamore@775 | 1305 | Type mt1 = types.memberType(site, m1); |
mcimadamore@775 | 1306 | Type mt2 = types.memberType(site, m2); |
duke@1 | 1307 | if (!types.overrideEquivalent(mt1, mt2)) |
mcimadamore@844 | 1308 | return ambiguityError(m1, m2); |
mcimadamore@844 | 1309 | |
duke@1 | 1310 | // same signature; select (a) the non-bridge method, or |
duke@1 | 1311 | // (b) the one that overrides the other, or (c) the concrete |
duke@1 | 1312 | // one, or (d) merge both abstract signatures |
mcimadamore@844 | 1313 | if ((m1.flags() & BRIDGE) != (m2.flags() & BRIDGE)) |
duke@1 | 1314 | return ((m1.flags() & BRIDGE) != 0) ? m2 : m1; |
mcimadamore@844 | 1315 | |
duke@1 | 1316 | // if one overrides or hides the other, use it |
duke@1 | 1317 | TypeSymbol m1Owner = (TypeSymbol)m1.owner; |
duke@1 | 1318 | TypeSymbol m2Owner = (TypeSymbol)m2.owner; |
duke@1 | 1319 | if (types.asSuper(m1Owner.type, m2Owner) != null && |
duke@1 | 1320 | ((m1.owner.flags_field & INTERFACE) == 0 || |
duke@1 | 1321 | (m2.owner.flags_field & INTERFACE) != 0) && |
duke@1 | 1322 | m1.overrides(m2, m1Owner, types, false)) |
duke@1 | 1323 | return m1; |
duke@1 | 1324 | if (types.asSuper(m2Owner.type, m1Owner) != null && |
duke@1 | 1325 | ((m2.owner.flags_field & INTERFACE) == 0 || |
duke@1 | 1326 | (m1.owner.flags_field & INTERFACE) != 0) && |
duke@1 | 1327 | m2.overrides(m1, m2Owner, types, false)) |
duke@1 | 1328 | return m2; |
duke@1 | 1329 | boolean m1Abstract = (m1.flags() & ABSTRACT) != 0; |
duke@1 | 1330 | boolean m2Abstract = (m2.flags() & ABSTRACT) != 0; |
duke@1 | 1331 | if (m1Abstract && !m2Abstract) return m2; |
duke@1 | 1332 | if (m2Abstract && !m1Abstract) return m1; |
duke@1 | 1333 | // both abstract or both concrete |
mcimadamore@1480 | 1334 | return ambiguityError(m1, m2); |
duke@1 | 1335 | } |
duke@1 | 1336 | if (m1SignatureMoreSpecific) return m1; |
duke@1 | 1337 | if (m2SignatureMoreSpecific) return m2; |
mcimadamore@844 | 1338 | return ambiguityError(m1, m2); |
duke@1 | 1339 | case AMBIGUOUS: |
mcimadamore@1480 | 1340 | //check if m1 is more specific than all ambiguous methods in m2 |
duke@1 | 1341 | AmbiguityError e = (AmbiguityError)m2; |
mcimadamore@1480 | 1342 | for (Symbol s : e.ambiguousSyms) { |
mcimadamore@1480 | 1343 | if (mostSpecific(argtypes, m1, s, env, site, allowBoxing, useVarargs) != m1) { |
mcimadamore@1480 | 1344 | return e.addAmbiguousSymbol(m1); |
mcimadamore@1480 | 1345 | } |
mcimadamore@1480 | 1346 | } |
mcimadamore@1480 | 1347 | return m1; |
duke@1 | 1348 | default: |
duke@1 | 1349 | throw new AssertionError(); |
duke@1 | 1350 | } |
duke@1 | 1351 | } |
mcimadamore@775 | 1352 | //where |
mcimadamore@1348 | 1353 | private boolean signatureMoreSpecific(List<Type> actuals, Env<AttrContext> env, Type site, Symbol m1, Symbol m2, boolean allowBoxing, boolean useVarargs) { |
mcimadamore@1510 | 1354 | noteWarner.clear(); |
mcimadamore@1510 | 1355 | int maxLength = Math.max( |
mcimadamore@1510 | 1356 | Math.max(m1.type.getParameterTypes().length(), actuals.length()), |
mcimadamore@1510 | 1357 | m2.type.getParameterTypes().length()); |
mcimadamore@1510 | 1358 | Type mst = instantiate(env, site, m2, null, |
mcimadamore@1510 | 1359 | adjustArgs(types.lowerBounds(types.memberType(site, m1).getParameterTypes()), m1, maxLength, useVarargs), null, |
mcimadamore@1510 | 1360 | allowBoxing, useVarargs, new MostSpecificCheck(!allowBoxing, actuals), noteWarner); |
mcimadamore@1510 | 1361 | return mst != null && |
mcimadamore@1510 | 1362 | !noteWarner.hasLint(Lint.LintCategory.UNCHECKED); |
mcimadamore@1510 | 1363 | } |
mcimadamore@1510 | 1364 | private List<Type> adjustArgs(List<Type> args, Symbol msym, int length, boolean allowVarargs) { |
mcimadamore@1510 | 1365 | if ((msym.flags() & VARARGS) != 0 && allowVarargs) { |
mcimadamore@1510 | 1366 | Type varargsElem = types.elemtype(args.last()); |
mcimadamore@1510 | 1367 | if (varargsElem == null) { |
mcimadamore@1510 | 1368 | Assert.error("Bad varargs = " + args.last() + " " + msym); |
mcimadamore@1510 | 1369 | } |
mcimadamore@1510 | 1370 | List<Type> newArgs = args.reverse().tail.prepend(varargsElem).reverse(); |
mcimadamore@1510 | 1371 | while (newArgs.length() < length) { |
mcimadamore@1510 | 1372 | newArgs = newArgs.append(newArgs.last()); |
mcimadamore@1510 | 1373 | } |
mcimadamore@1510 | 1374 | return newArgs; |
mcimadamore@1510 | 1375 | } else { |
mcimadamore@1510 | 1376 | return args; |
mcimadamore@1348 | 1377 | } |
mcimadamore@1348 | 1378 | } |
mcimadamore@1348 | 1379 | //where |
mcimadamore@1059 | 1380 | Type mostSpecificReturnType(Type mt1, Type mt2) { |
mcimadamore@1059 | 1381 | Type rt1 = mt1.getReturnType(); |
mcimadamore@1059 | 1382 | Type rt2 = mt2.getReturnType(); |
mcimadamore@1059 | 1383 | |
jjg@1374 | 1384 | if (mt1.hasTag(FORALL) && mt2.hasTag(FORALL)) { |
mcimadamore@1059 | 1385 | //if both are generic methods, adjust return type ahead of subtyping check |
mcimadamore@1059 | 1386 | rt1 = types.subst(rt1, mt1.getTypeArguments(), mt2.getTypeArguments()); |
mcimadamore@1059 | 1387 | } |
mcimadamore@1059 | 1388 | //first use subtyping, then return type substitutability |
mcimadamore@1059 | 1389 | if (types.isSubtype(rt1, rt2)) { |
mcimadamore@1059 | 1390 | return mt1; |
mcimadamore@1059 | 1391 | } else if (types.isSubtype(rt2, rt1)) { |
mcimadamore@1059 | 1392 | return mt2; |
mcimadamore@1059 | 1393 | } else if (types.returnTypeSubstitutable(mt1, mt2)) { |
mcimadamore@1059 | 1394 | return mt1; |
mcimadamore@1059 | 1395 | } else if (types.returnTypeSubstitutable(mt2, mt1)) { |
mcimadamore@1059 | 1396 | return mt2; |
mcimadamore@1059 | 1397 | } else { |
mcimadamore@1059 | 1398 | return null; |
mcimadamore@1059 | 1399 | } |
mcimadamore@1059 | 1400 | } |
mcimadamore@1059 | 1401 | //where |
mcimadamore@844 | 1402 | Symbol ambiguityError(Symbol m1, Symbol m2) { |
mcimadamore@844 | 1403 | if (((m1.flags() | m2.flags()) & CLASH) != 0) { |
mcimadamore@844 | 1404 | return (m1.flags() & CLASH) == 0 ? m1 : m2; |
mcimadamore@844 | 1405 | } else { |
mcimadamore@844 | 1406 | return new AmbiguityError(m1, m2); |
mcimadamore@844 | 1407 | } |
mcimadamore@844 | 1408 | } |
duke@1 | 1409 | |
mcimadamore@1394 | 1410 | Symbol findMethodInScope(Env<AttrContext> env, |
mcimadamore@1393 | 1411 | Type site, |
mcimadamore@1393 | 1412 | Name name, |
mcimadamore@1393 | 1413 | List<Type> argtypes, |
mcimadamore@1393 | 1414 | List<Type> typeargtypes, |
mcimadamore@1393 | 1415 | Scope sc, |
mcimadamore@1393 | 1416 | Symbol bestSoFar, |
mcimadamore@1393 | 1417 | boolean allowBoxing, |
mcimadamore@1393 | 1418 | boolean useVarargs, |
mcimadamore@1393 | 1419 | boolean operator, |
mcimadamore@1393 | 1420 | boolean abstractok) { |
mcimadamore@1393 | 1421 | for (Symbol s : sc.getElementsByName(name, new LookupFilter(abstractok))) { |
mcimadamore@1393 | 1422 | bestSoFar = selectBest(env, site, argtypes, typeargtypes, s, |
mcimadamore@1393 | 1423 | bestSoFar, allowBoxing, useVarargs, operator); |
mcimadamore@1393 | 1424 | } |
mcimadamore@1393 | 1425 | return bestSoFar; |
mcimadamore@1393 | 1426 | } |
mcimadamore@1393 | 1427 | //where |
mcimadamore@1393 | 1428 | class LookupFilter implements Filter<Symbol> { |
mcimadamore@1393 | 1429 | |
mcimadamore@1393 | 1430 | boolean abstractOk; |
mcimadamore@1393 | 1431 | |
mcimadamore@1393 | 1432 | LookupFilter(boolean abstractOk) { |
mcimadamore@1393 | 1433 | this.abstractOk = abstractOk; |
mcimadamore@1393 | 1434 | } |
mcimadamore@1393 | 1435 | |
mcimadamore@1393 | 1436 | public boolean accepts(Symbol s) { |
mcimadamore@1393 | 1437 | long flags = s.flags(); |
mcimadamore@1393 | 1438 | return s.kind == MTH && |
mcimadamore@1393 | 1439 | (flags & SYNTHETIC) == 0 && |
mcimadamore@1393 | 1440 | (abstractOk || |
mcimadamore@1393 | 1441 | (flags & DEFAULT) != 0 || |
mcimadamore@1393 | 1442 | (flags & ABSTRACT) == 0); |
mcimadamore@1393 | 1443 | } |
mcimadamore@1393 | 1444 | }; |
mcimadamore@1393 | 1445 | |
duke@1 | 1446 | /** Find best qualified method matching given name, type and value |
duke@1 | 1447 | * arguments. |
duke@1 | 1448 | * @param env The current environment. |
duke@1 | 1449 | * @param site The original type from where the selection |
duke@1 | 1450 | * takes place. |
duke@1 | 1451 | * @param name The method's name. |
duke@1 | 1452 | * @param argtypes The method's value arguments. |
duke@1 | 1453 | * @param typeargtypes The method's type arguments |
duke@1 | 1454 | * @param allowBoxing Allow boxing conversions of arguments. |
duke@1 | 1455 | * @param useVarargs Box trailing arguments into an array for varargs. |
duke@1 | 1456 | */ |
duke@1 | 1457 | Symbol findMethod(Env<AttrContext> env, |
duke@1 | 1458 | Type site, |
duke@1 | 1459 | Name name, |
duke@1 | 1460 | List<Type> argtypes, |
duke@1 | 1461 | List<Type> typeargtypes, |
duke@1 | 1462 | boolean allowBoxing, |
duke@1 | 1463 | boolean useVarargs, |
duke@1 | 1464 | boolean operator) { |
jrose@571 | 1465 | Symbol bestSoFar = methodNotFound; |
mcimadamore@1114 | 1466 | bestSoFar = findMethod(env, |
duke@1 | 1467 | site, |
duke@1 | 1468 | name, |
duke@1 | 1469 | argtypes, |
duke@1 | 1470 | typeargtypes, |
duke@1 | 1471 | site.tsym.type, |
jrose@571 | 1472 | bestSoFar, |
duke@1 | 1473 | allowBoxing, |
duke@1 | 1474 | useVarargs, |
mcimadamore@1335 | 1475 | operator); |
mcimadamore@1114 | 1476 | reportVerboseResolutionDiagnostic(env.tree.pos(), name, site, argtypes, typeargtypes, bestSoFar); |
mcimadamore@1114 | 1477 | return bestSoFar; |
duke@1 | 1478 | } |
duke@1 | 1479 | // where |
duke@1 | 1480 | private Symbol findMethod(Env<AttrContext> env, |
duke@1 | 1481 | Type site, |
duke@1 | 1482 | Name name, |
duke@1 | 1483 | List<Type> argtypes, |
duke@1 | 1484 | List<Type> typeargtypes, |
duke@1 | 1485 | Type intype, |
duke@1 | 1486 | Symbol bestSoFar, |
duke@1 | 1487 | boolean allowBoxing, |
duke@1 | 1488 | boolean useVarargs, |
mcimadamore@1335 | 1489 | boolean operator) { |
mcimadamore@1393 | 1490 | @SuppressWarnings({"unchecked","rawtypes"}) |
mcimadamore@1393 | 1491 | List<Type>[] itypes = (List<Type>[])new List[] { List.<Type>nil(), List.<Type>nil() }; |
mcimadamore@1393 | 1492 | InterfaceLookupPhase iphase = InterfaceLookupPhase.ABSTRACT_OK; |
mcimadamore@1335 | 1493 | for (TypeSymbol s : superclasses(intype)) { |
mcimadamore@1394 | 1494 | bestSoFar = findMethodInScope(env, site, name, argtypes, typeargtypes, |
mcimadamore@1335 | 1495 | s.members(), bestSoFar, allowBoxing, useVarargs, operator, true); |
mcimadamore@1393 | 1496 | if (name == names.init) return bestSoFar; |
mcimadamore@1393 | 1497 | iphase = (iphase == null) ? null : iphase.update(s, this); |
mcimadamore@1393 | 1498 | if (iphase != null) { |
mcimadamore@1335 | 1499 | for (Type itype : types.interfaces(s.type)) { |
mcimadamore@1393 | 1500 | itypes[iphase.ordinal()] = types.union(types.closure(itype), itypes[iphase.ordinal()]); |
duke@1 | 1501 | } |
duke@1 | 1502 | } |
mcimadamore@1335 | 1503 | } |
mcimadamore@1335 | 1504 | |
mcimadamore@1335 | 1505 | Symbol concrete = bestSoFar.kind < ERR && |
mcimadamore@1335 | 1506 | (bestSoFar.flags() & ABSTRACT) == 0 ? |
mcimadamore@1335 | 1507 | bestSoFar : methodNotFound; |
mcimadamore@1335 | 1508 | |
mcimadamore@1393 | 1509 | for (InterfaceLookupPhase iphase2 : InterfaceLookupPhase.values()) { |
mcimadamore@1393 | 1510 | if (iphase2 == InterfaceLookupPhase.DEFAULT_OK && !allowDefaultMethods) break; |
mcimadamore@1335 | 1511 | //keep searching for abstract methods |
mcimadamore@1393 | 1512 | for (Type itype : itypes[iphase2.ordinal()]) { |
mcimadamore@1335 | 1513 | if (!itype.isInterface()) continue; //skip j.l.Object (included by Types.closure()) |
mcimadamore@1393 | 1514 | if (iphase2 == InterfaceLookupPhase.DEFAULT_OK && |
mcimadamore@1393 | 1515 | (itype.tsym.flags() & DEFAULT) == 0) continue; |
mcimadamore@1394 | 1516 | bestSoFar = findMethodInScope(env, site, name, argtypes, typeargtypes, |
mcimadamore@1393 | 1517 | itype.tsym.members(), bestSoFar, allowBoxing, useVarargs, operator, true); |
mcimadamore@1393 | 1518 | if (concrete != bestSoFar && |
mcimadamore@1393 | 1519 | concrete.kind < ERR && bestSoFar.kind < ERR && |
mcimadamore@1393 | 1520 | types.isSubSignature(concrete.type, bestSoFar.type)) { |
mcimadamore@1393 | 1521 | //this is an hack - as javac does not do full membership checks |
mcimadamore@1393 | 1522 | //most specific ends up comparing abstract methods that might have |
mcimadamore@1393 | 1523 | //been implemented by some concrete method in a subclass and, |
mcimadamore@1393 | 1524 | //because of raw override, it is possible for an abstract method |
mcimadamore@1393 | 1525 | //to be more specific than the concrete method - so we need |
mcimadamore@1393 | 1526 | //to explicitly call that out (see CR 6178365) |
mcimadamore@1393 | 1527 | bestSoFar = concrete; |
mcimadamore@1393 | 1528 | } |
duke@1 | 1529 | } |
duke@1 | 1530 | } |
duke@1 | 1531 | return bestSoFar; |
duke@1 | 1532 | } |
duke@1 | 1533 | |
mcimadamore@1393 | 1534 | enum InterfaceLookupPhase { |
mcimadamore@1393 | 1535 | ABSTRACT_OK() { |
mcimadamore@1393 | 1536 | @Override |
mcimadamore@1393 | 1537 | InterfaceLookupPhase update(Symbol s, Resolve rs) { |
mcimadamore@1393 | 1538 | //We should not look for abstract methods if receiver is a concrete class |
mcimadamore@1393 | 1539 | //(as concrete classes are expected to implement all abstracts coming |
mcimadamore@1393 | 1540 | //from superinterfaces) |
mcimadamore@1393 | 1541 | if ((s.flags() & (ABSTRACT | INTERFACE | ENUM)) != 0) { |
mcimadamore@1393 | 1542 | return this; |
mcimadamore@1393 | 1543 | } else if (rs.allowDefaultMethods) { |
mcimadamore@1393 | 1544 | return DEFAULT_OK; |
mcimadamore@1393 | 1545 | } else { |
mcimadamore@1393 | 1546 | return null; |
mcimadamore@1393 | 1547 | } |
mcimadamore@1393 | 1548 | } |
mcimadamore@1393 | 1549 | }, |
mcimadamore@1393 | 1550 | DEFAULT_OK() { |
mcimadamore@1393 | 1551 | @Override |
mcimadamore@1393 | 1552 | InterfaceLookupPhase update(Symbol s, Resolve rs) { |
mcimadamore@1393 | 1553 | return this; |
mcimadamore@1393 | 1554 | } |
mcimadamore@1393 | 1555 | }; |
mcimadamore@1393 | 1556 | |
mcimadamore@1393 | 1557 | abstract InterfaceLookupPhase update(Symbol s, Resolve rs); |
mcimadamore@1393 | 1558 | } |
mcimadamore@1393 | 1559 | |
mcimadamore@1335 | 1560 | /** |
mcimadamore@1335 | 1561 | * Return an Iterable object to scan the superclasses of a given type. |
mcimadamore@1335 | 1562 | * It's crucial that the scan is done lazily, as we don't want to accidentally |
mcimadamore@1335 | 1563 | * access more supertypes than strictly needed (as this could trigger completion |
mcimadamore@1335 | 1564 | * errors if some of the not-needed supertypes are missing/ill-formed). |
mcimadamore@1335 | 1565 | */ |
mcimadamore@1335 | 1566 | Iterable<TypeSymbol> superclasses(final Type intype) { |
mcimadamore@1335 | 1567 | return new Iterable<TypeSymbol>() { |
mcimadamore@1335 | 1568 | public Iterator<TypeSymbol> iterator() { |
mcimadamore@1335 | 1569 | return new Iterator<TypeSymbol>() { |
mcimadamore@1335 | 1570 | |
mcimadamore@1335 | 1571 | List<TypeSymbol> seen = List.nil(); |
mcimadamore@1342 | 1572 | TypeSymbol currentSym = symbolFor(intype); |
mcimadamore@1342 | 1573 | TypeSymbol prevSym = null; |
mcimadamore@1335 | 1574 | |
mcimadamore@1335 | 1575 | public boolean hasNext() { |
mcimadamore@1342 | 1576 | if (currentSym == syms.noSymbol) { |
mcimadamore@1342 | 1577 | currentSym = symbolFor(types.supertype(prevSym.type)); |
mcimadamore@1342 | 1578 | } |
mcimadamore@1335 | 1579 | return currentSym != null; |
mcimadamore@1335 | 1580 | } |
mcimadamore@1335 | 1581 | |
mcimadamore@1335 | 1582 | public TypeSymbol next() { |
mcimadamore@1342 | 1583 | prevSym = currentSym; |
mcimadamore@1342 | 1584 | currentSym = syms.noSymbol; |
mcimadamore@1342 | 1585 | Assert.check(prevSym != null || prevSym != syms.noSymbol); |
mcimadamore@1335 | 1586 | return prevSym; |
mcimadamore@1335 | 1587 | } |
mcimadamore@1335 | 1588 | |
mcimadamore@1335 | 1589 | public void remove() { |
mcimadamore@1342 | 1590 | throw new UnsupportedOperationException(); |
mcimadamore@1335 | 1591 | } |
mcimadamore@1335 | 1592 | |
mcimadamore@1342 | 1593 | TypeSymbol symbolFor(Type t) { |
jjg@1374 | 1594 | if (!t.hasTag(CLASS) && |
jjg@1374 | 1595 | !t.hasTag(TYPEVAR)) { |
mcimadamore@1335 | 1596 | return null; |
mcimadamore@1335 | 1597 | } |
jjg@1374 | 1598 | while (t.hasTag(TYPEVAR)) |
mcimadamore@1342 | 1599 | t = t.getUpperBound(); |
mcimadamore@1342 | 1600 | if (seen.contains(t.tsym)) { |
mcimadamore@1335 | 1601 | //degenerate case in which we have a circular |
mcimadamore@1335 | 1602 | //class hierarchy - because of ill-formed classfiles |
mcimadamore@1335 | 1603 | return null; |
mcimadamore@1335 | 1604 | } |
mcimadamore@1342 | 1605 | seen = seen.prepend(t.tsym); |
mcimadamore@1342 | 1606 | return t.tsym; |
mcimadamore@1335 | 1607 | } |
mcimadamore@1335 | 1608 | }; |
mcimadamore@1335 | 1609 | } |
mcimadamore@1335 | 1610 | }; |
mcimadamore@1335 | 1611 | } |
mcimadamore@1335 | 1612 | |
duke@1 | 1613 | /** Find unqualified method matching given name, type and value arguments. |
duke@1 | 1614 | * @param env The current environment. |
duke@1 | 1615 | * @param name The method's name. |
duke@1 | 1616 | * @param argtypes The method's value arguments. |
duke@1 | 1617 | * @param typeargtypes The method's type arguments. |
duke@1 | 1618 | * @param allowBoxing Allow boxing conversions of arguments. |
duke@1 | 1619 | * @param useVarargs Box trailing arguments into an array for varargs. |
duke@1 | 1620 | */ |
duke@1 | 1621 | Symbol findFun(Env<AttrContext> env, Name name, |
duke@1 | 1622 | List<Type> argtypes, List<Type> typeargtypes, |
duke@1 | 1623 | boolean allowBoxing, boolean useVarargs) { |
duke@1 | 1624 | Symbol bestSoFar = methodNotFound; |
duke@1 | 1625 | Symbol sym; |
duke@1 | 1626 | Env<AttrContext> env1 = env; |
duke@1 | 1627 | boolean staticOnly = false; |
duke@1 | 1628 | while (env1.outer != null) { |
duke@1 | 1629 | if (isStatic(env1)) staticOnly = true; |
duke@1 | 1630 | sym = findMethod( |
duke@1 | 1631 | env1, env1.enclClass.sym.type, name, argtypes, typeargtypes, |
duke@1 | 1632 | allowBoxing, useVarargs, false); |
duke@1 | 1633 | if (sym.exists()) { |
duke@1 | 1634 | if (staticOnly && |
duke@1 | 1635 | sym.kind == MTH && |
duke@1 | 1636 | sym.owner.kind == TYP && |
duke@1 | 1637 | (sym.flags() & STATIC) == 0) return new StaticError(sym); |
duke@1 | 1638 | else return sym; |
duke@1 | 1639 | } else if (sym.kind < bestSoFar.kind) { |
duke@1 | 1640 | bestSoFar = sym; |
duke@1 | 1641 | } |
duke@1 | 1642 | if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true; |
duke@1 | 1643 | env1 = env1.outer; |
duke@1 | 1644 | } |
duke@1 | 1645 | |
duke@1 | 1646 | sym = findMethod(env, syms.predefClass.type, name, argtypes, |
duke@1 | 1647 | typeargtypes, allowBoxing, useVarargs, false); |
duke@1 | 1648 | if (sym.exists()) |
duke@1 | 1649 | return sym; |
duke@1 | 1650 | |
duke@1 | 1651 | Scope.Entry e = env.toplevel.namedImportScope.lookup(name); |
duke@1 | 1652 | for (; e.scope != null; e = e.next()) { |
duke@1 | 1653 | sym = e.sym; |
duke@1 | 1654 | Type origin = e.getOrigin().owner.type; |
duke@1 | 1655 | if (sym.kind == MTH) { |
duke@1 | 1656 | if (e.sym.owner.type != origin) |
duke@1 | 1657 | sym = sym.clone(e.getOrigin().owner); |
duke@1 | 1658 | if (!isAccessible(env, origin, sym)) |
duke@1 | 1659 | sym = new AccessError(env, origin, sym); |
duke@1 | 1660 | bestSoFar = selectBest(env, origin, |
duke@1 | 1661 | argtypes, typeargtypes, |
duke@1 | 1662 | sym, bestSoFar, |
duke@1 | 1663 | allowBoxing, useVarargs, false); |
duke@1 | 1664 | } |
duke@1 | 1665 | } |
duke@1 | 1666 | if (bestSoFar.exists()) |
duke@1 | 1667 | return bestSoFar; |
duke@1 | 1668 | |
duke@1 | 1669 | e = env.toplevel.starImportScope.lookup(name); |
duke@1 | 1670 | for (; e.scope != null; e = e.next()) { |
duke@1 | 1671 | sym = e.sym; |
duke@1 | 1672 | Type origin = e.getOrigin().owner.type; |
duke@1 | 1673 | if (sym.kind == MTH) { |
duke@1 | 1674 | if (e.sym.owner.type != origin) |
duke@1 | 1675 | sym = sym.clone(e.getOrigin().owner); |
duke@1 | 1676 | if (!isAccessible(env, origin, sym)) |
duke@1 | 1677 | sym = new AccessError(env, origin, sym); |
duke@1 | 1678 | bestSoFar = selectBest(env, origin, |
duke@1 | 1679 | argtypes, typeargtypes, |
duke@1 | 1680 | sym, bestSoFar, |
duke@1 | 1681 | allowBoxing, useVarargs, false); |
duke@1 | 1682 | } |
duke@1 | 1683 | } |
duke@1 | 1684 | return bestSoFar; |
duke@1 | 1685 | } |
duke@1 | 1686 | |
duke@1 | 1687 | /** Load toplevel or member class with given fully qualified name and |
duke@1 | 1688 | * verify that it is accessible. |
duke@1 | 1689 | * @param env The current environment. |
duke@1 | 1690 | * @param name The fully qualified name of the class to be loaded. |
duke@1 | 1691 | */ |
duke@1 | 1692 | Symbol loadClass(Env<AttrContext> env, Name name) { |
duke@1 | 1693 | try { |
duke@1 | 1694 | ClassSymbol c = reader.loadClass(name); |
duke@1 | 1695 | return isAccessible(env, c) ? c : new AccessError(c); |
duke@1 | 1696 | } catch (ClassReader.BadClassFile err) { |
duke@1 | 1697 | throw err; |
duke@1 | 1698 | } catch (CompletionFailure ex) { |
duke@1 | 1699 | return typeNotFound; |
duke@1 | 1700 | } |
duke@1 | 1701 | } |
duke@1 | 1702 | |
duke@1 | 1703 | /** Find qualified member type. |
duke@1 | 1704 | * @param env The current environment. |
duke@1 | 1705 | * @param site The original type from where the selection takes |
duke@1 | 1706 | * place. |
duke@1 | 1707 | * @param name The type's name. |
duke@1 | 1708 | * @param c The class to search for the member type. This is |
duke@1 | 1709 | * always a superclass or implemented interface of |
duke@1 | 1710 | * site's class. |
duke@1 | 1711 | */ |
duke@1 | 1712 | Symbol findMemberType(Env<AttrContext> env, |
duke@1 | 1713 | Type site, |
duke@1 | 1714 | Name name, |
duke@1 | 1715 | TypeSymbol c) { |
duke@1 | 1716 | Symbol bestSoFar = typeNotFound; |
duke@1 | 1717 | Symbol sym; |
duke@1 | 1718 | Scope.Entry e = c.members().lookup(name); |
duke@1 | 1719 | while (e.scope != null) { |
duke@1 | 1720 | if (e.sym.kind == TYP) { |
duke@1 | 1721 | return isAccessible(env, site, e.sym) |
duke@1 | 1722 | ? e.sym |
duke@1 | 1723 | : new AccessError(env, site, e.sym); |
duke@1 | 1724 | } |
duke@1 | 1725 | e = e.next(); |
duke@1 | 1726 | } |
duke@1 | 1727 | Type st = types.supertype(c.type); |
jjg@1374 | 1728 | if (st != null && st.hasTag(CLASS)) { |
duke@1 | 1729 | sym = findMemberType(env, site, name, st.tsym); |
duke@1 | 1730 | if (sym.kind < bestSoFar.kind) bestSoFar = sym; |
duke@1 | 1731 | } |
duke@1 | 1732 | for (List<Type> l = types.interfaces(c.type); |
duke@1 | 1733 | bestSoFar.kind != AMBIGUOUS && l.nonEmpty(); |
duke@1 | 1734 | l = l.tail) { |
duke@1 | 1735 | sym = findMemberType(env, site, name, l.head.tsym); |
duke@1 | 1736 | if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS && |
duke@1 | 1737 | sym.owner != bestSoFar.owner) |
duke@1 | 1738 | bestSoFar = new AmbiguityError(bestSoFar, sym); |
duke@1 | 1739 | else if (sym.kind < bestSoFar.kind) |
duke@1 | 1740 | bestSoFar = sym; |
duke@1 | 1741 | } |
duke@1 | 1742 | return bestSoFar; |
duke@1 | 1743 | } |
duke@1 | 1744 | |
duke@1 | 1745 | /** Find a global type in given scope and load corresponding class. |
duke@1 | 1746 | * @param env The current environment. |
duke@1 | 1747 | * @param scope The scope in which to look for the type. |
duke@1 | 1748 | * @param name The type's name. |
duke@1 | 1749 | */ |
duke@1 | 1750 | Symbol findGlobalType(Env<AttrContext> env, Scope scope, Name name) { |
duke@1 | 1751 | Symbol bestSoFar = typeNotFound; |
duke@1 | 1752 | for (Scope.Entry e = scope.lookup(name); e.scope != null; e = e.next()) { |
duke@1 | 1753 | Symbol sym = loadClass(env, e.sym.flatName()); |
duke@1 | 1754 | if (bestSoFar.kind == TYP && sym.kind == TYP && |
duke@1 | 1755 | bestSoFar != sym) |
duke@1 | 1756 | return new AmbiguityError(bestSoFar, sym); |
duke@1 | 1757 | else if (sym.kind < bestSoFar.kind) |
duke@1 | 1758 | bestSoFar = sym; |
duke@1 | 1759 | } |
duke@1 | 1760 | return bestSoFar; |
duke@1 | 1761 | } |
duke@1 | 1762 | |
duke@1 | 1763 | /** Find an unqualified type symbol. |
duke@1 | 1764 | * @param env The current environment. |
duke@1 | 1765 | * @param name The type's name. |
duke@1 | 1766 | */ |
duke@1 | 1767 | Symbol findType(Env<AttrContext> env, Name name) { |
duke@1 | 1768 | Symbol bestSoFar = typeNotFound; |
duke@1 | 1769 | Symbol sym; |
duke@1 | 1770 | boolean staticOnly = false; |
duke@1 | 1771 | for (Env<AttrContext> env1 = env; env1.outer != null; env1 = env1.outer) { |
duke@1 | 1772 | if (isStatic(env1)) staticOnly = true; |
duke@1 | 1773 | for (Scope.Entry e = env1.info.scope.lookup(name); |
duke@1 | 1774 | e.scope != null; |
duke@1 | 1775 | e = e.next()) { |
duke@1 | 1776 | if (e.sym.kind == TYP) { |
duke@1 | 1777 | if (staticOnly && |
jjg@1374 | 1778 | e.sym.type.hasTag(TYPEVAR) && |
duke@1 | 1779 | e.sym.owner.kind == TYP) return new StaticError(e.sym); |
duke@1 | 1780 | return e.sym; |
duke@1 | 1781 | } |
duke@1 | 1782 | } |
duke@1 | 1783 | |
duke@1 | 1784 | sym = findMemberType(env1, env1.enclClass.sym.type, name, |
duke@1 | 1785 | env1.enclClass.sym); |
duke@1 | 1786 | if (staticOnly && sym.kind == TYP && |
jjg@1374 | 1787 | sym.type.hasTag(CLASS) && |
jjg@1374 | 1788 | sym.type.getEnclosingType().hasTag(CLASS) && |
duke@1 | 1789 | env1.enclClass.sym.type.isParameterized() && |
duke@1 | 1790 | sym.type.getEnclosingType().isParameterized()) |
duke@1 | 1791 | return new StaticError(sym); |
duke@1 | 1792 | else if (sym.exists()) return sym; |
duke@1 | 1793 | else if (sym.kind < bestSoFar.kind) bestSoFar = sym; |
duke@1 | 1794 | |
duke@1 | 1795 | JCClassDecl encl = env1.baseClause ? (JCClassDecl)env1.tree : env1.enclClass; |
duke@1 | 1796 | if ((encl.sym.flags() & STATIC) != 0) |
duke@1 | 1797 | staticOnly = true; |
duke@1 | 1798 | } |
duke@1 | 1799 | |
jjg@1127 | 1800 | if (!env.tree.hasTag(IMPORT)) { |
duke@1 | 1801 | sym = findGlobalType(env, env.toplevel.namedImportScope, name); |
duke@1 | 1802 | if (sym.exists()) return sym; |
duke@1 | 1803 | else if (sym.kind < bestSoFar.kind) bestSoFar = sym; |
duke@1 | 1804 | |
duke@1 | 1805 | sym = findGlobalType(env, env.toplevel.packge.members(), name); |
duke@1 | 1806 | if (sym.exists()) return sym; |
duke@1 | 1807 | else if (sym.kind < bestSoFar.kind) bestSoFar = sym; |
duke@1 | 1808 | |
duke@1 | 1809 | sym = findGlobalType(env, env.toplevel.starImportScope, name); |
duke@1 | 1810 | if (sym.exists()) return sym; |
duke@1 | 1811 | else if (sym.kind < bestSoFar.kind) bestSoFar = sym; |
duke@1 | 1812 | } |
duke@1 | 1813 | |
duke@1 | 1814 | return bestSoFar; |
duke@1 | 1815 | } |
duke@1 | 1816 | |
duke@1 | 1817 | /** Find an unqualified identifier which matches a specified kind set. |
duke@1 | 1818 | * @param env The current environment. |
jjg@1409 | 1819 | * @param name The identifier's name. |
duke@1 | 1820 | * @param kind Indicates the possible symbol kinds |
duke@1 | 1821 | * (a subset of VAL, TYP, PCK). |
duke@1 | 1822 | */ |
duke@1 | 1823 | Symbol findIdent(Env<AttrContext> env, Name name, int kind) { |
duke@1 | 1824 | Symbol bestSoFar = typeNotFound; |
duke@1 | 1825 | Symbol sym; |
duke@1 | 1826 | |
duke@1 | 1827 | if ((kind & VAR) != 0) { |
duke@1 | 1828 | sym = findVar(env, name); |
duke@1 | 1829 | if (sym.exists()) return sym; |
duke@1 | 1830 | else if (sym.kind < bestSoFar.kind) bestSoFar = sym; |
duke@1 | 1831 | } |
duke@1 | 1832 | |
duke@1 | 1833 | if ((kind & TYP) != 0) { |
duke@1 | 1834 | sym = findType(env, name); |
ohrstrom@1460 | 1835 | if (sym.kind==TYP) { |
ohrstrom@1460 | 1836 | reportDependence(env.enclClass.sym, sym); |
ohrstrom@1460 | 1837 | } |
duke@1 | 1838 | if (sym.exists()) return sym; |
duke@1 | 1839 | else if (sym.kind < bestSoFar.kind) bestSoFar = sym; |
duke@1 | 1840 | } |
duke@1 | 1841 | |
duke@1 | 1842 | if ((kind & PCK) != 0) return reader.enterPackage(name); |
duke@1 | 1843 | else return bestSoFar; |
duke@1 | 1844 | } |
duke@1 | 1845 | |
ohrstrom@1460 | 1846 | /** Report dependencies. |
ohrstrom@1460 | 1847 | * @param from The enclosing class sym |
ohrstrom@1460 | 1848 | * @param to The found identifier that the class depends on. |
ohrstrom@1460 | 1849 | */ |
ohrstrom@1460 | 1850 | public void reportDependence(Symbol from, Symbol to) { |
ohrstrom@1460 | 1851 | // Override if you want to collect the reported dependencies. |
ohrstrom@1460 | 1852 | } |
ohrstrom@1460 | 1853 | |
duke@1 | 1854 | /** Find an identifier in a package which matches a specified kind set. |
duke@1 | 1855 | * @param env The current environment. |
duke@1 | 1856 | * @param name The identifier's name. |
duke@1 | 1857 | * @param kind Indicates the possible symbol kinds |
duke@1 | 1858 | * (a nonempty subset of TYP, PCK). |
duke@1 | 1859 | */ |
duke@1 | 1860 | Symbol findIdentInPackage(Env<AttrContext> env, TypeSymbol pck, |
duke@1 | 1861 | Name name, int kind) { |
duke@1 | 1862 | Name fullname = TypeSymbol.formFullName(name, pck); |
duke@1 | 1863 | Symbol bestSoFar = typeNotFound; |
duke@1 | 1864 | PackageSymbol pack = null; |
duke@1 | 1865 | if ((kind & PCK) != 0) { |
duke@1 | 1866 | pack = reader.enterPackage(fullname); |
duke@1 | 1867 | if (pack.exists()) return pack; |
duke@1 | 1868 | } |
duke@1 | 1869 | if ((kind & TYP) != 0) { |
duke@1 | 1870 | Symbol sym = loadClass(env, fullname); |
duke@1 | 1871 | if (sym.exists()) { |
duke@1 | 1872 | // don't allow programs to use flatnames |
duke@1 | 1873 | if (name == sym.name) return sym; |
duke@1 | 1874 | } |
duke@1 | 1875 | else if (sym.kind < bestSoFar.kind) bestSoFar = sym; |
duke@1 | 1876 | } |
duke@1 | 1877 | return (pack != null) ? pack : bestSoFar; |
duke@1 | 1878 | } |
duke@1 | 1879 | |
duke@1 | 1880 | /** Find an identifier among the members of a given type `site'. |
duke@1 | 1881 | * @param env The current environment. |
duke@1 | 1882 | * @param site The type containing the symbol to be found. |
duke@1 | 1883 | * @param name The identifier's name. |
duke@1 | 1884 | * @param kind Indicates the possible symbol kinds |
duke@1 | 1885 | * (a subset of VAL, TYP). |
duke@1 | 1886 | */ |
duke@1 | 1887 | Symbol findIdentInType(Env<AttrContext> env, Type site, |
duke@1 | 1888 | Name name, int kind) { |
duke@1 | 1889 | Symbol bestSoFar = typeNotFound; |
duke@1 | 1890 | Symbol sym; |
duke@1 | 1891 | if ((kind & VAR) != 0) { |
duke@1 | 1892 | sym = findField(env, site, name, site.tsym); |
duke@1 | 1893 | if (sym.exists()) return sym; |
duke@1 | 1894 | else if (sym.kind < bestSoFar.kind) bestSoFar = sym; |
duke@1 | 1895 | } |
duke@1 | 1896 | |
duke@1 | 1897 | if ((kind & TYP) != 0) { |
duke@1 | 1898 | sym = findMemberType(env, site, name, site.tsym); |
duke@1 | 1899 | if (sym.exists()) return sym; |
duke@1 | 1900 | else if (sym.kind < bestSoFar.kind) bestSoFar = sym; |
duke@1 | 1901 | } |
duke@1 | 1902 | return bestSoFar; |
duke@1 | 1903 | } |
duke@1 | 1904 | |
duke@1 | 1905 | /* *************************************************************************** |
duke@1 | 1906 | * Access checking |
duke@1 | 1907 | * The following methods convert ResolveErrors to ErrorSymbols, issuing |
duke@1 | 1908 | * an error message in the process |
duke@1 | 1909 | ****************************************************************************/ |
duke@1 | 1910 | |
duke@1 | 1911 | /** If `sym' is a bad symbol: report error and return errSymbol |
duke@1 | 1912 | * else pass through unchanged, |
duke@1 | 1913 | * additional arguments duplicate what has been used in trying to find the |
jjg@1326 | 1914 | * symbol {@literal (--> flyweight pattern)}. This improves performance since we |
duke@1 | 1915 | * expect misses to happen frequently. |
duke@1 | 1916 | * |
duke@1 | 1917 | * @param sym The symbol that was found, or a ResolveError. |
duke@1 | 1918 | * @param pos The position to use for error reporting. |
mcimadamore@1347 | 1919 | * @param location The symbol the served as a context for this lookup |
duke@1 | 1920 | * @param site The original type from where the selection took place. |
duke@1 | 1921 | * @param name The symbol's name. |
mcimadamore@1347 | 1922 | * @param qualified Did we get here through a qualified expression resolution? |
duke@1 | 1923 | * @param argtypes The invocation's value arguments, |
duke@1 | 1924 | * if we looked for a method. |
duke@1 | 1925 | * @param typeargtypes The invocation's type arguments, |
duke@1 | 1926 | * if we looked for a method. |
mcimadamore@1347 | 1927 | * @param logResolveHelper helper class used to log resolve errors |
duke@1 | 1928 | */ |
mcimadamore@1347 | 1929 | Symbol accessInternal(Symbol sym, |
mcimadamore@1347 | 1930 | DiagnosticPosition pos, |
mcimadamore@1347 | 1931 | Symbol location, |
mcimadamore@1347 | 1932 | Type site, |
mcimadamore@1347 | 1933 | Name name, |
mcimadamore@1347 | 1934 | boolean qualified, |
mcimadamore@1347 | 1935 | List<Type> argtypes, |
mcimadamore@1347 | 1936 | List<Type> typeargtypes, |
mcimadamore@1347 | 1937 | LogResolveHelper logResolveHelper) { |
mcimadamore@1347 | 1938 | if (sym.kind >= AMBIGUOUS) { |
mcimadamore@1347 | 1939 | ResolveError errSym = (ResolveError)sym; |
mcimadamore@1347 | 1940 | sym = errSym.access(name, qualified ? site.tsym : syms.noSymbol); |
mcimadamore@1347 | 1941 | argtypes = logResolveHelper.getArgumentTypes(errSym, sym, name, argtypes); |
mcimadamore@1347 | 1942 | if (logResolveHelper.resolveDiagnosticNeeded(site, argtypes, typeargtypes)) { |
mcimadamore@1347 | 1943 | logResolveError(errSym, pos, location, site, name, argtypes, typeargtypes); |
mcimadamore@1347 | 1944 | } |
mcimadamore@1347 | 1945 | } |
mcimadamore@1347 | 1946 | return sym; |
mcimadamore@1347 | 1947 | } |
mcimadamore@1347 | 1948 | |
mcimadamore@1347 | 1949 | /** |
mcimadamore@1347 | 1950 | * Variant of the generalized access routine, to be used for generating method |
mcimadamore@1347 | 1951 | * resolution diagnostics |
mcimadamore@1347 | 1952 | */ |
mcimadamore@1347 | 1953 | Symbol accessMethod(Symbol sym, |
duke@1 | 1954 | DiagnosticPosition pos, |
mcimadamore@829 | 1955 | Symbol location, |
duke@1 | 1956 | Type site, |
duke@1 | 1957 | Name name, |
duke@1 | 1958 | boolean qualified, |
duke@1 | 1959 | List<Type> argtypes, |
duke@1 | 1960 | List<Type> typeargtypes) { |
mcimadamore@1347 | 1961 | return accessInternal(sym, pos, location, site, name, qualified, argtypes, typeargtypes, methodLogResolveHelper); |
duke@1 | 1962 | } |
duke@1 | 1963 | |
mcimadamore@1347 | 1964 | /** Same as original accessMethod(), but without location. |
mcimadamore@829 | 1965 | */ |
mcimadamore@1347 | 1966 | Symbol accessMethod(Symbol sym, |
mcimadamore@829 | 1967 | DiagnosticPosition pos, |
mcimadamore@829 | 1968 | Type site, |
mcimadamore@829 | 1969 | Name name, |
mcimadamore@829 | 1970 | boolean qualified, |
mcimadamore@829 | 1971 | List<Type> argtypes, |
mcimadamore@829 | 1972 | List<Type> typeargtypes) { |
mcimadamore@1347 | 1973 | return accessMethod(sym, pos, site.tsym, site, name, qualified, argtypes, typeargtypes); |
mcimadamore@829 | 1974 | } |
mcimadamore@829 | 1975 | |
mcimadamore@1347 | 1976 | /** |
mcimadamore@1347 | 1977 | * Variant of the generalized access routine, to be used for generating variable, |
mcimadamore@1347 | 1978 | * type resolution diagnostics |
mcimadamore@829 | 1979 | */ |
mcimadamore@1347 | 1980 | Symbol accessBase(Symbol sym, |
mcimadamore@829 | 1981 | DiagnosticPosition pos, |
mcimadamore@829 | 1982 | Symbol location, |
mcimadamore@829 | 1983 | Type site, |
mcimadamore@829 | 1984 | Name name, |
mcimadamore@829 | 1985 | boolean qualified) { |
mcimadamore@1347 | 1986 | return accessInternal(sym, pos, location, site, name, qualified, List.<Type>nil(), null, basicLogResolveHelper); |
mcimadamore@829 | 1987 | } |
mcimadamore@829 | 1988 | |
mcimadamore@1347 | 1989 | /** Same as original accessBase(), but without location. |
duke@1 | 1990 | */ |
mcimadamore@1347 | 1991 | Symbol accessBase(Symbol sym, |
duke@1 | 1992 | DiagnosticPosition pos, |
duke@1 | 1993 | Type site, |
duke@1 | 1994 | Name name, |
duke@1 | 1995 | boolean qualified) { |
mcimadamore@1347 | 1996 | return accessBase(sym, pos, site.tsym, site, name, qualified); |
duke@1 | 1997 | } |
duke@1 | 1998 | |
mcimadamore@1347 | 1999 | interface LogResolveHelper { |
mcimadamore@1347 | 2000 | boolean resolveDiagnosticNeeded(Type site, List<Type> argtypes, List<Type> typeargtypes); |
mcimadamore@1347 | 2001 | List<Type> getArgumentTypes(ResolveError errSym, Symbol accessedSym, Name name, List<Type> argtypes); |
mcimadamore@1347 | 2002 | } |
mcimadamore@1347 | 2003 | |
mcimadamore@1347 | 2004 | LogResolveHelper basicLogResolveHelper = new LogResolveHelper() { |
mcimadamore@1347 | 2005 | public boolean resolveDiagnosticNeeded(Type site, List<Type> argtypes, List<Type> typeargtypes) { |
mcimadamore@1347 | 2006 | return !site.isErroneous(); |
mcimadamore@1347 | 2007 | } |
mcimadamore@1347 | 2008 | public List<Type> getArgumentTypes(ResolveError errSym, Symbol accessedSym, Name name, List<Type> argtypes) { |
mcimadamore@1347 | 2009 | return argtypes; |
mcimadamore@1347 | 2010 | } |
mcimadamore@1347 | 2011 | }; |
mcimadamore@1347 | 2012 | |
mcimadamore@1347 | 2013 | LogResolveHelper methodLogResolveHelper = new LogResolveHelper() { |
mcimadamore@1347 | 2014 | public boolean resolveDiagnosticNeeded(Type site, List<Type> argtypes, List<Type> typeargtypes) { |
mcimadamore@1347 | 2015 | return !site.isErroneous() && |
mcimadamore@1347 | 2016 | !Type.isErroneous(argtypes) && |
mcimadamore@1347 | 2017 | (typeargtypes == null || !Type.isErroneous(typeargtypes)); |
mcimadamore@1347 | 2018 | } |
mcimadamore@1347 | 2019 | public List<Type> getArgumentTypes(ResolveError errSym, Symbol accessedSym, Name name, List<Type> argtypes) { |
mcimadamore@1415 | 2020 | return (syms.operatorNames.contains(name)) ? |
mcimadamore@1415 | 2021 | argtypes : |
mcimadamore@1415 | 2022 | Type.map(argtypes, new ResolveDeferredRecoveryMap(accessedSym)); |
mcimadamore@1415 | 2023 | } |
mcimadamore@1415 | 2024 | |
mcimadamore@1415 | 2025 | class ResolveDeferredRecoveryMap extends DeferredAttr.RecoveryDeferredTypeMap { |
mcimadamore@1415 | 2026 | |
mcimadamore@1415 | 2027 | public ResolveDeferredRecoveryMap(Symbol msym) { |
mcimadamore@1415 | 2028 | deferredAttr.super(AttrMode.SPECULATIVE, msym, currentResolutionContext.step); |
mcimadamore@1415 | 2029 | } |
mcimadamore@1415 | 2030 | |
mcimadamore@1415 | 2031 | @Override |
mcimadamore@1415 | 2032 | protected Type typeOf(DeferredType dt) { |
mcimadamore@1415 | 2033 | Type res = super.typeOf(dt); |
mcimadamore@1415 | 2034 | if (!res.isErroneous()) { |
mcimadamore@1415 | 2035 | switch (TreeInfo.skipParens(dt.tree).getTag()) { |
mcimadamore@1415 | 2036 | case LAMBDA: |
mcimadamore@1415 | 2037 | case REFERENCE: |
mcimadamore@1415 | 2038 | return dt; |
mcimadamore@1415 | 2039 | case CONDEXPR: |
mcimadamore@1415 | 2040 | return res == Type.recoveryType ? |
mcimadamore@1415 | 2041 | dt : res; |
mcimadamore@1347 | 2042 | } |
mcimadamore@1347 | 2043 | } |
mcimadamore@1415 | 2044 | return res; |
mcimadamore@1347 | 2045 | } |
mcimadamore@1347 | 2046 | } |
mcimadamore@1347 | 2047 | }; |
mcimadamore@1347 | 2048 | |
duke@1 | 2049 | /** Check that sym is not an abstract method. |
duke@1 | 2050 | */ |
duke@1 | 2051 | void checkNonAbstract(DiagnosticPosition pos, Symbol sym) { |
mcimadamore@1393 | 2052 | if ((sym.flags() & ABSTRACT) != 0 && (sym.flags() & DEFAULT) == 0) |
duke@1 | 2053 | log.error(pos, "abstract.cant.be.accessed.directly", |
duke@1 | 2054 | kindName(sym), sym, sym.location()); |
duke@1 | 2055 | } |
duke@1 | 2056 | |
duke@1 | 2057 | /* *************************************************************************** |
duke@1 | 2058 | * Debugging |
duke@1 | 2059 | ****************************************************************************/ |
duke@1 | 2060 | |
duke@1 | 2061 | /** print all scopes starting with scope s and proceeding outwards. |
duke@1 | 2062 | * used for debugging. |
duke@1 | 2063 | */ |
duke@1 | 2064 | public void printscopes(Scope s) { |
duke@1 | 2065 | while (s != null) { |
duke@1 | 2066 | if (s.owner != null) |
duke@1 | 2067 | System.err.print(s.owner + ": "); |
duke@1 | 2068 | for (Scope.Entry e = s.elems; e != null; e = e.sibling) { |
duke@1 | 2069 | if ((e.sym.flags() & ABSTRACT) != 0) |
duke@1 | 2070 | System.err.print("abstract "); |
duke@1 | 2071 | System.err.print(e.sym + " "); |
duke@1 | 2072 | } |
duke@1 | 2073 | System.err.println(); |
duke@1 | 2074 | s = s.next; |
duke@1 | 2075 | } |
duke@1 | 2076 | } |
duke@1 | 2077 | |
duke@1 | 2078 | void printscopes(Env<AttrContext> env) { |
duke@1 | 2079 | while (env.outer != null) { |
duke@1 | 2080 | System.err.println("------------------------------"); |
duke@1 | 2081 | printscopes(env.info.scope); |
duke@1 | 2082 | env = env.outer; |
duke@1 | 2083 | } |
duke@1 | 2084 | } |
duke@1 | 2085 | |
duke@1 | 2086 | public void printscopes(Type t) { |
jjg@1374 | 2087 | while (t.hasTag(CLASS)) { |
duke@1 | 2088 | printscopes(t.tsym.members()); |
duke@1 | 2089 | t = types.supertype(t); |
duke@1 | 2090 | } |
duke@1 | 2091 | } |
duke@1 | 2092 | |
duke@1 | 2093 | /* *************************************************************************** |
duke@1 | 2094 | * Name resolution |
duke@1 | 2095 | * Naming conventions are as for symbol lookup |
duke@1 | 2096 | * Unlike the find... methods these methods will report access errors |
duke@1 | 2097 | ****************************************************************************/ |
duke@1 | 2098 | |
duke@1 | 2099 | /** Resolve an unqualified (non-method) identifier. |
duke@1 | 2100 | * @param pos The position to use for error reporting. |
duke@1 | 2101 | * @param env The environment current at the identifier use. |
duke@1 | 2102 | * @param name The identifier's name. |
duke@1 | 2103 | * @param kind The set of admissible symbol kinds for the identifier. |
duke@1 | 2104 | */ |
duke@1 | 2105 | Symbol resolveIdent(DiagnosticPosition pos, Env<AttrContext> env, |
duke@1 | 2106 | Name name, int kind) { |
mcimadamore@1347 | 2107 | return accessBase( |
duke@1 | 2108 | findIdent(env, name, kind), |
duke@1 | 2109 | pos, env.enclClass.sym.type, name, false); |
duke@1 | 2110 | } |
duke@1 | 2111 | |
duke@1 | 2112 | /** Resolve an unqualified method identifier. |
duke@1 | 2113 | * @param pos The position to use for error reporting. |
duke@1 | 2114 | * @param env The environment current at the method invocation. |
duke@1 | 2115 | * @param name The identifier's name. |
duke@1 | 2116 | * @param argtypes The types of the invocation's value arguments. |
duke@1 | 2117 | * @param typeargtypes The types of the invocation's type arguments. |
duke@1 | 2118 | */ |
duke@1 | 2119 | Symbol resolveMethod(DiagnosticPosition pos, |
duke@1 | 2120 | Env<AttrContext> env, |
duke@1 | 2121 | Name name, |
duke@1 | 2122 | List<Type> argtypes, |
duke@1 | 2123 | List<Type> typeargtypes) { |
mcimadamore@1396 | 2124 | return lookupMethod(env, pos, env.enclClass.sym, new BasicLookupHelper(name, env.enclClass.sym.type, argtypes, typeargtypes) { |
mcimadamore@1394 | 2125 | @Override |
mcimadamore@1394 | 2126 | Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) { |
mcimadamore@1394 | 2127 | return findFun(env, name, argtypes, typeargtypes, |
mcimadamore@1394 | 2128 | phase.isBoxingRequired(), |
mcimadamore@1394 | 2129 | phase.isVarargsRequired()); |
mcimadamore@1215 | 2130 | } |
mcimadamore@1394 | 2131 | }); |
mcimadamore@689 | 2132 | } |
mcimadamore@689 | 2133 | |
duke@1 | 2134 | /** Resolve a qualified method identifier |
duke@1 | 2135 | * @param pos The position to use for error reporting. |
duke@1 | 2136 | * @param env The environment current at the method invocation. |
duke@1 | 2137 | * @param site The type of the qualifying expression, in which |
duke@1 | 2138 | * identifier is searched. |
duke@1 | 2139 | * @param name The identifier's name. |
duke@1 | 2140 | * @param argtypes The types of the invocation's value arguments. |
duke@1 | 2141 | * @param typeargtypes The types of the invocation's type arguments. |
duke@1 | 2142 | */ |
duke@1 | 2143 | Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env, |
duke@1 | 2144 | Type site, Name name, List<Type> argtypes, |
duke@1 | 2145 | List<Type> typeargtypes) { |
mcimadamore@829 | 2146 | return resolveQualifiedMethod(pos, env, site.tsym, site, name, argtypes, typeargtypes); |
mcimadamore@829 | 2147 | } |
mcimadamore@829 | 2148 | Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env, |
mcimadamore@829 | 2149 | Symbol location, Type site, Name name, List<Type> argtypes, |
mcimadamore@829 | 2150 | List<Type> typeargtypes) { |
mcimadamore@1215 | 2151 | return resolveQualifiedMethod(new MethodResolutionContext(), pos, env, location, site, name, argtypes, typeargtypes); |
mcimadamore@1215 | 2152 | } |
mcimadamore@1215 | 2153 | private Symbol resolveQualifiedMethod(MethodResolutionContext resolveContext, |
mcimadamore@1215 | 2154 | DiagnosticPosition pos, Env<AttrContext> env, |
mcimadamore@1215 | 2155 | Symbol location, Type site, Name name, List<Type> argtypes, |
mcimadamore@1215 | 2156 | List<Type> typeargtypes) { |
mcimadamore@1394 | 2157 | return lookupMethod(env, pos, location, resolveContext, new BasicLookupHelper(name, site, argtypes, typeargtypes) { |
mcimadamore@1394 | 2158 | @Override |
mcimadamore@1394 | 2159 | Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) { |
mcimadamore@1394 | 2160 | return findMethod(env, site, name, argtypes, typeargtypes, |
mcimadamore@1394 | 2161 | phase.isBoxingRequired(), |
mcimadamore@1394 | 2162 | phase.isVarargsRequired(), false); |
mcimadamore@1215 | 2163 | } |
mcimadamore@1394 | 2164 | @Override |
mcimadamore@1394 | 2165 | Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym) { |
mcimadamore@1394 | 2166 | if (sym.kind >= AMBIGUOUS) { |
mcimadamore@1394 | 2167 | sym = super.access(env, pos, location, sym); |
mcimadamore@1394 | 2168 | } else if (allowMethodHandles) { |
mcimadamore@1394 | 2169 | MethodSymbol msym = (MethodSymbol)sym; |
mcimadamore@1394 | 2170 | if (msym.isSignaturePolymorphic(types)) { |
mcimadamore@1394 | 2171 | return findPolymorphicSignatureInstance(env, sym, argtypes); |
mcimadamore@1394 | 2172 | } |
mcimadamore@1215 | 2173 | } |
mcimadamore@1394 | 2174 | return sym; |
mcimadamore@674 | 2175 | } |
mcimadamore@1394 | 2176 | }); |
duke@1 | 2177 | } |
duke@1 | 2178 | |
mcimadamore@674 | 2179 | /** Find or create an implicit method of exactly the given type (after erasure). |
mcimadamore@674 | 2180 | * Searches in a side table, not the main scope of the site. |
mcimadamore@674 | 2181 | * This emulates the lookup process required by JSR 292 in JVM. |
mcimadamore@674 | 2182 | * @param env Attribution environment |
mcimadamore@1239 | 2183 | * @param spMethod signature polymorphic method - i.e. MH.invokeExact |
mcimadamore@1239 | 2184 | * @param argtypes The required argument types |
mcimadamore@674 | 2185 | */ |
mcimadamore@1239 | 2186 | Symbol findPolymorphicSignatureInstance(Env<AttrContext> env, |
mcimadamore@1415 | 2187 | final Symbol spMethod, |
mcimadamore@820 | 2188 | List<Type> argtypes) { |
mcimadamore@674 | 2189 | Type mtype = infer.instantiatePolymorphicSignatureInstance(env, |
mcimadamore@1347 | 2190 | (MethodSymbol)spMethod, currentResolutionContext, argtypes); |
mcimadamore@1239 | 2191 | for (Symbol sym : polymorphicSignatureScope.getElementsByName(spMethod.name)) { |
mcimadamore@1239 | 2192 | if (types.isSameType(mtype, sym.type)) { |
mcimadamore@1239 | 2193 | return sym; |
mcimadamore@674 | 2194 | } |
mcimadamore@674 | 2195 | } |
mcimadamore@1239 | 2196 | |
mcimadamore@1239 | 2197 | // create the desired method |
mcimadamore@1239 | 2198 | long flags = ABSTRACT | HYPOTHETICAL | spMethod.flags() & Flags.AccessFlags; |
mcimadamore@1415 | 2199 | Symbol msym = new MethodSymbol(flags, spMethod.name, mtype, spMethod.owner) { |
mcimadamore@1415 | 2200 | @Override |
mcimadamore@1415 | 2201 | public Symbol baseSymbol() { |
mcimadamore@1415 | 2202 | return spMethod; |
mcimadamore@1415 | 2203 | } |
mcimadamore@1415 | 2204 | }; |
mcimadamore@1239 | 2205 | polymorphicSignatureScope.enter(msym); |
mcimadamore@1239 | 2206 | return msym; |
mcimadamore@674 | 2207 | } |
mcimadamore@674 | 2208 | |
duke@1 | 2209 | /** Resolve a qualified method identifier, throw a fatal error if not |
duke@1 | 2210 | * found. |
duke@1 | 2211 | * @param pos The position to use for error reporting. |
duke@1 | 2212 | * @param env The environment current at the method invocation. |
duke@1 | 2213 | * @param site The type of the qualifying expression, in which |
duke@1 | 2214 | * identifier is searched. |
duke@1 | 2215 | * @param name The identifier's name. |
duke@1 | 2216 | * @param argtypes The types of the invocation's value arguments. |
duke@1 | 2217 | * @param typeargtypes The types of the invocation's type arguments. |
duke@1 | 2218 | */ |
duke@1 | 2219 | public MethodSymbol resolveInternalMethod(DiagnosticPosition pos, Env<AttrContext> env, |
duke@1 | 2220 | Type site, Name name, |
duke@1 | 2221 | List<Type> argtypes, |
duke@1 | 2222 | List<Type> typeargtypes) { |
mcimadamore@1215 | 2223 | MethodResolutionContext resolveContext = new MethodResolutionContext(); |
mcimadamore@1215 | 2224 | resolveContext.internalResolution = true; |
mcimadamore@1215 | 2225 | Symbol sym = resolveQualifiedMethod(resolveContext, pos, env, site.tsym, |
mcimadamore@1215 | 2226 | site, name, argtypes, typeargtypes); |
mcimadamore@1215 | 2227 | if (sym.kind == MTH) return (MethodSymbol)sym; |
mcimadamore@1215 | 2228 | else throw new FatalError( |
mcimadamore@1215 | 2229 | diags.fragment("fatal.err.cant.locate.meth", |
mcimadamore@1215 | 2230 | name)); |
duke@1 | 2231 | } |
duke@1 | 2232 | |
duke@1 | 2233 | /** Resolve constructor. |
duke@1 | 2234 | * @param pos The position to use for error reporting. |
duke@1 | 2235 | * @param env The environment current at the constructor invocation. |
duke@1 | 2236 | * @param site The type of class for which a constructor is searched. |
duke@1 | 2237 | * @param argtypes The types of the constructor invocation's value |
duke@1 | 2238 | * arguments. |
duke@1 | 2239 | * @param typeargtypes The types of the constructor invocation's type |
duke@1 | 2240 | * arguments. |
duke@1 | 2241 | */ |
duke@1 | 2242 | Symbol resolveConstructor(DiagnosticPosition pos, |
duke@1 | 2243 | Env<AttrContext> env, |
duke@1 | 2244 | Type site, |
duke@1 | 2245 | List<Type> argtypes, |
duke@1 | 2246 | List<Type> typeargtypes) { |
mcimadamore@1215 | 2247 | return resolveConstructor(new MethodResolutionContext(), pos, env, site, argtypes, typeargtypes); |
mcimadamore@1215 | 2248 | } |
mcimadamore@1394 | 2249 | |
mcimadamore@1215 | 2250 | private Symbol resolveConstructor(MethodResolutionContext resolveContext, |
mcimadamore@1394 | 2251 | final DiagnosticPosition pos, |
mcimadamore@1215 | 2252 | Env<AttrContext> env, |
mcimadamore@1215 | 2253 | Type site, |
mcimadamore@1215 | 2254 | List<Type> argtypes, |
mcimadamore@1215 | 2255 | List<Type> typeargtypes) { |
mcimadamore@1394 | 2256 | return lookupMethod(env, pos, site.tsym, resolveContext, new BasicLookupHelper(names.init, site, argtypes, typeargtypes) { |
mcimadamore@1394 | 2257 | @Override |
mcimadamore@1394 | 2258 | Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) { |
mcimadamore@1394 | 2259 | return findConstructor(pos, env, site, argtypes, typeargtypes, |
mcimadamore@1394 | 2260 | phase.isBoxingRequired(), |
mcimadamore@1394 | 2261 | phase.isVarargsRequired()); |
mcimadamore@1215 | 2262 | } |
mcimadamore@1394 | 2263 | }); |
mcimadamore@1394 | 2264 | } |
mcimadamore@1394 | 2265 | |
mcimadamore@1394 | 2266 | /** Resolve a constructor, throw a fatal error if not found. |
mcimadamore@1394 | 2267 | * @param pos The position to use for error reporting. |
mcimadamore@1394 | 2268 | * @param env The environment current at the method invocation. |
mcimadamore@1394 | 2269 | * @param site The type to be constructed. |
mcimadamore@1394 | 2270 | * @param argtypes The types of the invocation's value arguments. |
mcimadamore@1394 | 2271 | * @param typeargtypes The types of the invocation's type arguments. |
mcimadamore@1394 | 2272 | */ |
mcimadamore@1394 | 2273 | public MethodSymbol resolveInternalConstructor(DiagnosticPosition pos, Env<AttrContext> env, |
mcimadamore@1394 | 2274 | Type site, |
mcimadamore@1394 | 2275 | List<Type> argtypes, |
mcimadamore@1394 | 2276 | List<Type> typeargtypes) { |
mcimadamore@1394 | 2277 | MethodResolutionContext resolveContext = new MethodResolutionContext(); |
mcimadamore@1394 | 2278 | resolveContext.internalResolution = true; |
mcimadamore@1394 | 2279 | Symbol sym = resolveConstructor(resolveContext, pos, env, site, argtypes, typeargtypes); |
mcimadamore@1394 | 2280 | if (sym.kind == MTH) return (MethodSymbol)sym; |
mcimadamore@1394 | 2281 | else throw new FatalError( |
mcimadamore@1394 | 2282 | diags.fragment("fatal.err.cant.locate.ctor", site)); |
mcimadamore@1394 | 2283 | } |
mcimadamore@1394 | 2284 | |
mcimadamore@1394 | 2285 | Symbol findConstructor(DiagnosticPosition pos, Env<AttrContext> env, |
mcimadamore@1394 | 2286 | Type site, List<Type> argtypes, |
mcimadamore@1394 | 2287 | List<Type> typeargtypes, |
mcimadamore@1394 | 2288 | boolean allowBoxing, |
mcimadamore@1394 | 2289 | boolean useVarargs) { |
mcimadamore@1394 | 2290 | Symbol sym = findMethod(env, site, |
mcimadamore@1394 | 2291 | names.init, argtypes, |
mcimadamore@1394 | 2292 | typeargtypes, allowBoxing, |
mcimadamore@1394 | 2293 | useVarargs, false); |
mcimadamore@1394 | 2294 | chk.checkDeprecated(pos, env.info.scope.owner, sym); |
mcimadamore@1394 | 2295 | return sym; |
duke@1 | 2296 | } |
duke@1 | 2297 | |
mcimadamore@537 | 2298 | /** Resolve constructor using diamond inference. |
mcimadamore@537 | 2299 | * @param pos The position to use for error reporting. |
mcimadamore@537 | 2300 | * @param env The environment current at the constructor invocation. |
mcimadamore@537 | 2301 | * @param site The type of class for which a constructor is searched. |
mcimadamore@537 | 2302 | * The scope of this class has been touched in attribution. |
mcimadamore@537 | 2303 | * @param argtypes The types of the constructor invocation's value |
mcimadamore@537 | 2304 | * arguments. |
mcimadamore@537 | 2305 | * @param typeargtypes The types of the constructor invocation's type |
mcimadamore@537 | 2306 | * arguments. |
mcimadamore@537 | 2307 | */ |
mcimadamore@537 | 2308 | Symbol resolveDiamond(DiagnosticPosition pos, |
mcimadamore@537 | 2309 | Env<AttrContext> env, |
mcimadamore@537 | 2310 | Type site, |
mcimadamore@537 | 2311 | List<Type> argtypes, |
mcimadamore@631 | 2312 | List<Type> typeargtypes) { |
mcimadamore@1394 | 2313 | return lookupMethod(env, pos, site.tsym, new BasicLookupHelper(names.init, site, argtypes, typeargtypes) { |
mcimadamore@1394 | 2314 | @Override |
mcimadamore@1394 | 2315 | Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) { |
mcimadamore@1394 | 2316 | return findDiamond(env, site, argtypes, typeargtypes, |
mcimadamore@1394 | 2317 | phase.isBoxingRequired(), |
mcimadamore@1394 | 2318 | phase.isVarargsRequired()); |
mcimadamore@1215 | 2319 | } |
mcimadamore@1394 | 2320 | @Override |
mcimadamore@1394 | 2321 | Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym) { |
mcimadamore@1394 | 2322 | if (sym.kind >= AMBIGUOUS) { |
mcimadamore@1394 | 2323 | final JCDiagnostic details = sym.kind == WRONG_MTH ? |
mcimadamore@1394 | 2324 | ((InapplicableSymbolError)sym).errCandidate().details : |
mcimadamore@1394 | 2325 | null; |
mcimadamore@1394 | 2326 | sym = new InapplicableSymbolError(sym.kind, "diamondError", currentResolutionContext) { |
mcimadamore@1394 | 2327 | @Override |
mcimadamore@1394 | 2328 | JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos, |
mcimadamore@1394 | 2329 | Symbol location, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) { |
mcimadamore@1394 | 2330 | String key = details == null ? |
mcimadamore@1394 | 2331 | "cant.apply.diamond" : |
mcimadamore@1394 | 2332 | "cant.apply.diamond.1"; |
mcimadamore@1394 | 2333 | return diags.create(dkind, log.currentSource(), pos, key, |
mcimadamore@1394 | 2334 | diags.fragment("diamond", site.tsym), details); |
mcimadamore@1394 | 2335 | } |
mcimadamore@1394 | 2336 | }; |
mcimadamore@1394 | 2337 | sym = accessMethod(sym, pos, site, names.init, true, argtypes, typeargtypes); |
mcimadamore@1394 | 2338 | env.info.pendingResolutionPhase = currentResolutionContext.step; |
mcimadamore@1394 | 2339 | } |
mcimadamore@1394 | 2340 | return sym; |
mcimadamore@1215 | 2341 | } |
mcimadamore@1394 | 2342 | }); |
mcimadamore@537 | 2343 | } |
mcimadamore@537 | 2344 | |
mcimadamore@1217 | 2345 | /** This method scans all the constructor symbol in a given class scope - |
mcimadamore@1217 | 2346 | * assuming that the original scope contains a constructor of the kind: |
jjg@1326 | 2347 | * {@code Foo(X x, Y y)}, where X,Y are class type-variables declared in Foo, |
mcimadamore@1217 | 2348 | * a method check is executed against the modified constructor type: |
jjg@1326 | 2349 | * {@code <X,Y>Foo<X,Y>(X x, Y y)}. This is crucial in order to enable diamond |
mcimadamore@1217 | 2350 | * inference. The inferred return type of the synthetic constructor IS |
mcimadamore@1217 | 2351 | * the inferred type for the diamond operator. |
mcimadamore@1217 | 2352 | */ |
mcimadamore@1217 | 2353 | private Symbol findDiamond(Env<AttrContext> env, |
mcimadamore@1217 | 2354 | Type site, |
mcimadamore@1217 | 2355 | List<Type> argtypes, |
mcimadamore@1217 | 2356 | List<Type> typeargtypes, |
mcimadamore@1217 | 2357 | boolean allowBoxing, |
mcimadamore@1217 | 2358 | boolean useVarargs) { |
mcimadamore@1217 | 2359 | Symbol bestSoFar = methodNotFound; |
mcimadamore@1217 | 2360 | for (Scope.Entry e = site.tsym.members().lookup(names.init); |
mcimadamore@1217 | 2361 | e.scope != null; |
mcimadamore@1217 | 2362 | e = e.next()) { |
mcimadamore@1341 | 2363 | final Symbol sym = e.sym; |
mcimadamore@1217 | 2364 | //- System.out.println(" e " + e.sym); |
mcimadamore@1341 | 2365 | if (sym.kind == MTH && |
mcimadamore@1341 | 2366 | (sym.flags_field & SYNTHETIC) == 0) { |
jjg@1374 | 2367 | List<Type> oldParams = e.sym.type.hasTag(FORALL) ? |
mcimadamore@1341 | 2368 | ((ForAll)sym.type).tvars : |
mcimadamore@1217 | 2369 | List.<Type>nil(); |
mcimadamore@1217 | 2370 | Type constrType = new ForAll(site.tsym.type.getTypeArguments().appendList(oldParams), |
mcimadamore@1341 | 2371 | types.createMethodTypeWithReturn(sym.type.asMethodType(), site)); |
mcimadamore@1341 | 2372 | MethodSymbol newConstr = new MethodSymbol(sym.flags(), names.init, constrType, site.tsym) { |
mcimadamore@1341 | 2373 | @Override |
mcimadamore@1341 | 2374 | public Symbol baseSymbol() { |
mcimadamore@1341 | 2375 | return sym; |
mcimadamore@1341 | 2376 | } |
mcimadamore@1341 | 2377 | }; |
mcimadamore@1217 | 2378 | bestSoFar = selectBest(env, site, argtypes, typeargtypes, |
mcimadamore@1341 | 2379 | newConstr, |
mcimadamore@1217 | 2380 | bestSoFar, |
mcimadamore@1217 | 2381 | allowBoxing, |
mcimadamore@1217 | 2382 | useVarargs, |
mcimadamore@1217 | 2383 | false); |
mcimadamore@1217 | 2384 | } |
mcimadamore@1217 | 2385 | } |
mcimadamore@1217 | 2386 | return bestSoFar; |
mcimadamore@1217 | 2387 | } |
mcimadamore@1217 | 2388 | |
mcimadamore@1394 | 2389 | |
mcimadamore@1394 | 2390 | |
mcimadamore@1394 | 2391 | /** Resolve operator. |
mcimadamore@1394 | 2392 | * @param pos The position to use for error reporting. |
mcimadamore@1394 | 2393 | * @param optag The tag of the operation tree. |
mcimadamore@1394 | 2394 | * @param env The environment current at the operation. |
mcimadamore@1394 | 2395 | * @param argtypes The types of the operands. |
mcimadamore@1394 | 2396 | */ |
mcimadamore@1394 | 2397 | Symbol resolveOperator(DiagnosticPosition pos, JCTree.Tag optag, |
mcimadamore@1394 | 2398 | Env<AttrContext> env, List<Type> argtypes) { |
mcimadamore@1394 | 2399 | MethodResolutionContext prevResolutionContext = currentResolutionContext; |
mcimadamore@1394 | 2400 | try { |
mcimadamore@1394 | 2401 | currentResolutionContext = new MethodResolutionContext(); |
mcimadamore@1394 | 2402 | Name name = treeinfo.operatorName(optag); |
mcimadamore@1479 | 2403 | env.info.pendingResolutionPhase = currentResolutionContext.step = BASIC; |
mcimadamore@1394 | 2404 | Symbol sym = findMethod(env, syms.predefClass.type, name, argtypes, |
mcimadamore@1394 | 2405 | null, false, false, true); |
mcimadamore@1394 | 2406 | if (boxingEnabled && sym.kind >= WRONG_MTHS) |
mcimadamore@1479 | 2407 | env.info.pendingResolutionPhase = currentResolutionContext.step = BOX; |
mcimadamore@1394 | 2408 | sym = findMethod(env, syms.predefClass.type, name, argtypes, |
mcimadamore@1394 | 2409 | null, true, false, true); |
mcimadamore@1394 | 2410 | return accessMethod(sym, pos, env.enclClass.sym.type, name, |
mcimadamore@1394 | 2411 | false, argtypes, null); |
mcimadamore@1394 | 2412 | } |
mcimadamore@1394 | 2413 | finally { |
mcimadamore@1394 | 2414 | currentResolutionContext = prevResolutionContext; |
mcimadamore@1394 | 2415 | } |
mcimadamore@1394 | 2416 | } |
mcimadamore@1394 | 2417 | |
mcimadamore@1394 | 2418 | /** Resolve operator. |
mcimadamore@1394 | 2419 | * @param pos The position to use for error reporting. |
mcimadamore@1394 | 2420 | * @param optag The tag of the operation tree. |
mcimadamore@1394 | 2421 | * @param env The environment current at the operation. |
mcimadamore@1394 | 2422 | * @param arg The type of the operand. |
mcimadamore@1394 | 2423 | */ |
mcimadamore@1394 | 2424 | Symbol resolveUnaryOperator(DiagnosticPosition pos, JCTree.Tag optag, Env<AttrContext> env, Type arg) { |
mcimadamore@1394 | 2425 | return resolveOperator(pos, optag, env, List.of(arg)); |
mcimadamore@1394 | 2426 | } |
mcimadamore@1394 | 2427 | |
mcimadamore@1394 | 2428 | /** Resolve binary operator. |
mcimadamore@1394 | 2429 | * @param pos The position to use for error reporting. |
mcimadamore@1394 | 2430 | * @param optag The tag of the operation tree. |
mcimadamore@1394 | 2431 | * @param env The environment current at the operation. |
mcimadamore@1394 | 2432 | * @param left The types of the left operand. |
mcimadamore@1394 | 2433 | * @param right The types of the right operand. |
mcimadamore@1394 | 2434 | */ |
mcimadamore@1394 | 2435 | Symbol resolveBinaryOperator(DiagnosticPosition pos, |
mcimadamore@1394 | 2436 | JCTree.Tag optag, |
mcimadamore@1394 | 2437 | Env<AttrContext> env, |
mcimadamore@1394 | 2438 | Type left, |
mcimadamore@1394 | 2439 | Type right) { |
mcimadamore@1394 | 2440 | return resolveOperator(pos, optag, env, List.of(left, right)); |
mcimadamore@1394 | 2441 | } |
mcimadamore@1394 | 2442 | |
mcimadamore@1352 | 2443 | /** |
mcimadamore@1352 | 2444 | * Resolution of member references is typically done as a single |
mcimadamore@1352 | 2445 | * overload resolution step, where the argument types A are inferred from |
mcimadamore@1352 | 2446 | * the target functional descriptor. |
mcimadamore@1352 | 2447 | * |
mcimadamore@1352 | 2448 | * If the member reference is a method reference with a type qualifier, |
mcimadamore@1352 | 2449 | * a two-step lookup process is performed. The first step uses the |
mcimadamore@1352 | 2450 | * expected argument list A, while the second step discards the first |
mcimadamore@1352 | 2451 | * type from A (which is treated as a receiver type). |
mcimadamore@1352 | 2452 | * |
mcimadamore@1352 | 2453 | * There are two cases in which inference is performed: (i) if the member |
mcimadamore@1352 | 2454 | * reference is a constructor reference and the qualifier type is raw - in |
mcimadamore@1352 | 2455 | * which case diamond inference is used to infer a parameterization for the |
mcimadamore@1352 | 2456 | * type qualifier; (ii) if the member reference is an unbound reference |
mcimadamore@1352 | 2457 | * where the type qualifier is raw - in that case, during the unbound lookup |
mcimadamore@1352 | 2458 | * the receiver argument type is used to infer an instantiation for the raw |
mcimadamore@1352 | 2459 | * qualifier type. |
mcimadamore@1352 | 2460 | * |
mcimadamore@1352 | 2461 | * When a multi-step resolution process is exploited, it is an error |
mcimadamore@1352 | 2462 | * if two candidates are found (ambiguity). |
mcimadamore@1352 | 2463 | * |
mcimadamore@1352 | 2464 | * This routine returns a pair (T,S), where S is the member reference symbol, |
mcimadamore@1352 | 2465 | * and T is the type of the class in which S is defined. This is necessary as |
mcimadamore@1352 | 2466 | * the type T might be dynamically inferred (i.e. if constructor reference |
mcimadamore@1352 | 2467 | * has a raw qualifier). |
mcimadamore@1352 | 2468 | */ |
mcimadamore@1352 | 2469 | Pair<Symbol, ReferenceLookupHelper> resolveMemberReference(DiagnosticPosition pos, |
mcimadamore@1352 | 2470 | Env<AttrContext> env, |
mcimadamore@1352 | 2471 | JCMemberReference referenceTree, |
mcimadamore@1352 | 2472 | Type site, |
mcimadamore@1352 | 2473 | Name name, List<Type> argtypes, |
mcimadamore@1352 | 2474 | List<Type> typeargtypes, |
mcimadamore@1352 | 2475 | boolean boxingAllowed) { |
mcimadamore@1394 | 2476 | MethodResolutionPhase maxPhase = boxingAllowed ? VARARITY : BASIC; |
mcimadamore@1496 | 2477 | |
mcimadamore@1496 | 2478 | ReferenceLookupHelper boundLookupHelper; |
mcimadamore@1496 | 2479 | if (!name.equals(names.init)) { |
mcimadamore@1496 | 2480 | //method reference |
mcimadamore@1496 | 2481 | boundLookupHelper = |
mcimadamore@1496 | 2482 | new MethodReferenceLookupHelper(referenceTree, name, site, argtypes, typeargtypes, maxPhase); |
mcimadamore@1496 | 2483 | } else if (site.hasTag(ARRAY)) { |
mcimadamore@1496 | 2484 | //array constructor reference |
mcimadamore@1496 | 2485 | boundLookupHelper = |
mcimadamore@1496 | 2486 | new ArrayConstructorReferenceLookupHelper(referenceTree, site, argtypes, typeargtypes, maxPhase); |
mcimadamore@1496 | 2487 | } else { |
mcimadamore@1496 | 2488 | //class constructor reference |
mcimadamore@1496 | 2489 | boundLookupHelper = |
mcimadamore@1496 | 2490 | new ConstructorReferenceLookupHelper(referenceTree, site, argtypes, typeargtypes, maxPhase); |
mcimadamore@1496 | 2491 | } |
mcimadamore@1496 | 2492 | |
mcimadamore@1352 | 2493 | //step 1 - bound lookup |
mcimadamore@1352 | 2494 | Env<AttrContext> boundEnv = env.dup(env.tree, env.info.dup()); |
mcimadamore@1394 | 2495 | Symbol boundSym = lookupMethod(boundEnv, env.tree.pos(), site.tsym, boundLookupHelper); |
mcimadamore@1352 | 2496 | |
mcimadamore@1352 | 2497 | //step 2 - unbound lookup |
mcimadamore@1352 | 2498 | ReferenceLookupHelper unboundLookupHelper = boundLookupHelper.unboundLookup(); |
mcimadamore@1352 | 2499 | Env<AttrContext> unboundEnv = env.dup(env.tree, env.info.dup()); |
mcimadamore@1394 | 2500 | Symbol unboundSym = lookupMethod(unboundEnv, env.tree.pos(), site.tsym, unboundLookupHelper); |
mcimadamore@1352 | 2501 | |
mcimadamore@1352 | 2502 | //merge results |
mcimadamore@1352 | 2503 | Pair<Symbol, ReferenceLookupHelper> res; |
mcimadamore@1581 | 2504 | if (!lookupSuccess(unboundSym)) { |
mcimadamore@1352 | 2505 | res = new Pair<Symbol, ReferenceLookupHelper>(boundSym, boundLookupHelper); |
mcimadamore@1352 | 2506 | env.info.pendingResolutionPhase = boundEnv.info.pendingResolutionPhase; |
mcimadamore@1581 | 2507 | } else if (lookupSuccess(boundSym)) { |
mcimadamore@1352 | 2508 | res = new Pair<Symbol, ReferenceLookupHelper>(ambiguityError(boundSym, unboundSym), boundLookupHelper); |
mcimadamore@1352 | 2509 | env.info.pendingResolutionPhase = boundEnv.info.pendingResolutionPhase; |
mcimadamore@1352 | 2510 | } else { |
mcimadamore@1352 | 2511 | res = new Pair<Symbol, ReferenceLookupHelper>(unboundSym, unboundLookupHelper); |
mcimadamore@1352 | 2512 | env.info.pendingResolutionPhase = unboundEnv.info.pendingResolutionPhase; |
mcimadamore@1352 | 2513 | } |
mcimadamore@1352 | 2514 | |
mcimadamore@1352 | 2515 | return res; |
mcimadamore@1352 | 2516 | } |
mcimadamore@1581 | 2517 | //private |
mcimadamore@1581 | 2518 | boolean lookupSuccess(Symbol s) { |
mcimadamore@1581 | 2519 | return s.kind == MTH || s.kind == AMBIGUOUS; |
mcimadamore@1581 | 2520 | } |
mcimadamore@1352 | 2521 | |
mcimadamore@1352 | 2522 | /** |
mcimadamore@1352 | 2523 | * Helper for defining custom method-like lookup logic; a lookup helper |
mcimadamore@1352 | 2524 | * provides hooks for (i) the actual lookup logic and (ii) accessing the |
mcimadamore@1352 | 2525 | * lookup result (this step might result in compiler diagnostics to be generated) |
mcimadamore@1352 | 2526 | */ |
mcimadamore@1352 | 2527 | abstract class LookupHelper { |
mcimadamore@1352 | 2528 | |
mcimadamore@1352 | 2529 | /** name of the symbol to lookup */ |
mcimadamore@1352 | 2530 | Name name; |
mcimadamore@1352 | 2531 | |
mcimadamore@1352 | 2532 | /** location in which the lookup takes place */ |
mcimadamore@1352 | 2533 | Type site; |
mcimadamore@1352 | 2534 | |
mcimadamore@1352 | 2535 | /** actual types used during the lookup */ |
mcimadamore@1352 | 2536 | List<Type> argtypes; |
mcimadamore@1352 | 2537 | |
mcimadamore@1352 | 2538 | /** type arguments used during the lookup */ |
mcimadamore@1352 | 2539 | List<Type> typeargtypes; |
mcimadamore@1352 | 2540 | |
mcimadamore@1394 | 2541 | /** Max overload resolution phase handled by this helper */ |
mcimadamore@1394 | 2542 | MethodResolutionPhase maxPhase; |
mcimadamore@1394 | 2543 | |
mcimadamore@1394 | 2544 | LookupHelper(Name name, Type site, List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) { |
mcimadamore@1352 | 2545 | this.name = name; |
mcimadamore@1352 | 2546 | this.site = site; |
mcimadamore@1352 | 2547 | this.argtypes = argtypes; |
mcimadamore@1352 | 2548 | this.typeargtypes = typeargtypes; |
mcimadamore@1394 | 2549 | this.maxPhase = maxPhase; |
mcimadamore@1394 | 2550 | } |
mcimadamore@1394 | 2551 | |
mcimadamore@1394 | 2552 | /** |
mcimadamore@1394 | 2553 | * Should lookup stop at given phase with given result |
mcimadamore@1394 | 2554 | */ |
mcimadamore@1394 | 2555 | protected boolean shouldStop(Symbol sym, MethodResolutionPhase phase) { |
mcimadamore@1394 | 2556 | return phase.ordinal() > maxPhase.ordinal() || |
mcimadamore@1394 | 2557 | sym.kind < ERRONEOUS || sym.kind == AMBIGUOUS; |
mcimadamore@1352 | 2558 | } |
mcimadamore@1352 | 2559 | |
mcimadamore@1352 | 2560 | /** |
mcimadamore@1352 | 2561 | * Search for a symbol under a given overload resolution phase - this method |
mcimadamore@1352 | 2562 | * is usually called several times, once per each overload resolution phase |
mcimadamore@1352 | 2563 | */ |
mcimadamore@1352 | 2564 | abstract Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase); |
mcimadamore@1352 | 2565 | |
mcimadamore@1352 | 2566 | /** |
mcimadamore@1352 | 2567 | * Validate the result of the lookup |
mcimadamore@1352 | 2568 | */ |
mcimadamore@1394 | 2569 | abstract Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym); |
mcimadamore@1394 | 2570 | } |
mcimadamore@1394 | 2571 | |
mcimadamore@1394 | 2572 | abstract class BasicLookupHelper extends LookupHelper { |
mcimadamore@1394 | 2573 | |
mcimadamore@1394 | 2574 | BasicLookupHelper(Name name, Type site, List<Type> argtypes, List<Type> typeargtypes) { |
mcimadamore@1394 | 2575 | super(name, site, argtypes, typeargtypes, MethodResolutionPhase.VARARITY); |
mcimadamore@1394 | 2576 | } |
mcimadamore@1394 | 2577 | |
mcimadamore@1394 | 2578 | @Override |
mcimadamore@1394 | 2579 | Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym) { |
mcimadamore@1480 | 2580 | if (sym.kind == AMBIGUOUS) { |
mcimadamore@1480 | 2581 | AmbiguityError a_err = (AmbiguityError)sym; |
mcimadamore@1480 | 2582 | sym = a_err.mergeAbstracts(site); |
mcimadamore@1480 | 2583 | } |
mcimadamore@1394 | 2584 | if (sym.kind >= AMBIGUOUS) { |
mcimadamore@1394 | 2585 | //if nothing is found return the 'first' error |
mcimadamore@1394 | 2586 | sym = accessMethod(sym, pos, location, site, name, true, argtypes, typeargtypes); |
mcimadamore@1394 | 2587 | } |
mcimadamore@1394 | 2588 | return sym; |
mcimadamore@1394 | 2589 | } |
mcimadamore@1352 | 2590 | } |
mcimadamore@1352 | 2591 | |
mcimadamore@1352 | 2592 | /** |
mcimadamore@1352 | 2593 | * Helper class for member reference lookup. A reference lookup helper |
mcimadamore@1352 | 2594 | * defines the basic logic for member reference lookup; a method gives |
mcimadamore@1352 | 2595 | * access to an 'unbound' helper used to perform an unbound member |
mcimadamore@1352 | 2596 | * reference lookup. |
mcimadamore@1352 | 2597 | */ |
mcimadamore@1352 | 2598 | abstract class ReferenceLookupHelper extends LookupHelper { |
mcimadamore@1352 | 2599 | |
mcimadamore@1352 | 2600 | /** The member reference tree */ |
mcimadamore@1352 | 2601 | JCMemberReference referenceTree; |
mcimadamore@1352 | 2602 | |
mcimadamore@1352 | 2603 | ReferenceLookupHelper(JCMemberReference referenceTree, Name name, Type site, |
mcimadamore@1394 | 2604 | List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) { |
mcimadamore@1394 | 2605 | super(name, site, argtypes, typeargtypes, maxPhase); |
mcimadamore@1352 | 2606 | this.referenceTree = referenceTree; |
mcimadamore@1394 | 2607 | |
mcimadamore@1352 | 2608 | } |
mcimadamore@1352 | 2609 | |
mcimadamore@1352 | 2610 | /** |
mcimadamore@1352 | 2611 | * Returns an unbound version of this lookup helper. By default, this |
mcimadamore@1352 | 2612 | * method returns an dummy lookup helper. |
mcimadamore@1352 | 2613 | */ |
mcimadamore@1352 | 2614 | ReferenceLookupHelper unboundLookup() { |
mcimadamore@1352 | 2615 | //dummy loopkup helper that always return 'methodNotFound' |
mcimadamore@1394 | 2616 | return new ReferenceLookupHelper(referenceTree, name, site, argtypes, typeargtypes, maxPhase) { |
mcimadamore@1352 | 2617 | @Override |
mcimadamore@1352 | 2618 | ReferenceLookupHelper unboundLookup() { |
mcimadamore@1352 | 2619 | return this; |
mcimadamore@1352 | 2620 | } |
mcimadamore@1352 | 2621 | @Override |
mcimadamore@1394 | 2622 | Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) { |
mcimadamore@1352 | 2623 | return methodNotFound; |
mcimadamore@1352 | 2624 | } |
mcimadamore@1352 | 2625 | @Override |
mcimadamore@1352 | 2626 | ReferenceKind referenceKind(Symbol sym) { |
mcimadamore@1352 | 2627 | Assert.error(); |
mcimadamore@1352 | 2628 | return null; |
mcimadamore@1352 | 2629 | } |
mcimadamore@1352 | 2630 | }; |
mcimadamore@1352 | 2631 | } |
mcimadamore@1352 | 2632 | |
mcimadamore@1352 | 2633 | /** |
mcimadamore@1352 | 2634 | * Get the kind of the member reference |
mcimadamore@1352 | 2635 | */ |
mcimadamore@1352 | 2636 | abstract JCMemberReference.ReferenceKind referenceKind(Symbol sym); |
mcimadamore@1352 | 2637 | |
mcimadamore@1394 | 2638 | Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym) { |
mcimadamore@1480 | 2639 | if (sym.kind == AMBIGUOUS) { |
mcimadamore@1480 | 2640 | AmbiguityError a_err = (AmbiguityError)sym; |
mcimadamore@1480 | 2641 | sym = a_err.mergeAbstracts(site); |
mcimadamore@1480 | 2642 | } |
mcimadamore@1394 | 2643 | //skip error reporting |
mcimadamore@1352 | 2644 | return sym; |
mcimadamore@1352 | 2645 | } |
mcimadamore@1352 | 2646 | } |
mcimadamore@1352 | 2647 | |
mcimadamore@1352 | 2648 | /** |
mcimadamore@1352 | 2649 | * Helper class for method reference lookup. The lookup logic is based |
mcimadamore@1352 | 2650 | * upon Resolve.findMethod; in certain cases, this helper class has a |
mcimadamore@1352 | 2651 | * corresponding unbound helper class (see UnboundMethodReferenceLookupHelper). |
mcimadamore@1352 | 2652 | * In such cases, non-static lookup results are thrown away. |
mcimadamore@1352 | 2653 | */ |
mcimadamore@1352 | 2654 | class MethodReferenceLookupHelper extends ReferenceLookupHelper { |
mcimadamore@1352 | 2655 | |
mcimadamore@1352 | 2656 | MethodReferenceLookupHelper(JCMemberReference referenceTree, Name name, Type site, |
mcimadamore@1394 | 2657 | List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) { |
mcimadamore@1394 | 2658 | super(referenceTree, name, site, argtypes, typeargtypes, maxPhase); |
mcimadamore@1352 | 2659 | } |
mcimadamore@1352 | 2660 | |
mcimadamore@1352 | 2661 | protected Symbol lookupReferenceInternal(Env<AttrContext> env, MethodResolutionPhase phase) { |
mcimadamore@1352 | 2662 | return findMethod(env, site, name, argtypes, typeargtypes, |
mcimadamore@1352 | 2663 | phase.isBoxingRequired(), phase.isVarargsRequired(), syms.operatorNames.contains(name)); |
mcimadamore@1352 | 2664 | } |
mcimadamore@1352 | 2665 | |
mcimadamore@1352 | 2666 | protected Symbol adjustLookupResult(Env<AttrContext> env, Symbol sym) { |
mcimadamore@1352 | 2667 | return !TreeInfo.isStaticSelector(referenceTree.expr, names) || |
mcimadamore@1352 | 2668 | sym.kind != MTH || |
mcimadamore@1352 | 2669 | sym.isStatic() ? sym : new StaticError(sym); |
mcimadamore@1352 | 2670 | } |
mcimadamore@1352 | 2671 | |
mcimadamore@1352 | 2672 | @Override |
mcimadamore@1394 | 2673 | final Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) { |
mcimadamore@1352 | 2674 | return adjustLookupResult(env, lookupReferenceInternal(env, phase)); |
mcimadamore@1352 | 2675 | } |
mcimadamore@1352 | 2676 | |
mcimadamore@1352 | 2677 | @Override |
mcimadamore@1352 | 2678 | ReferenceLookupHelper unboundLookup() { |
mcimadamore@1352 | 2679 | if (TreeInfo.isStaticSelector(referenceTree.expr, names) && |
mcimadamore@1352 | 2680 | argtypes.nonEmpty() && |
mcimadamore@1352 | 2681 | types.isSubtypeUnchecked(argtypes.head, site)) { |
mcimadamore@1352 | 2682 | return new UnboundMethodReferenceLookupHelper(referenceTree, name, |
mcimadamore@1394 | 2683 | site, argtypes, typeargtypes, maxPhase); |
mcimadamore@1352 | 2684 | } else { |
mcimadamore@1352 | 2685 | return super.unboundLookup(); |
mcimadamore@1352 | 2686 | } |
mcimadamore@1352 | 2687 | } |
mcimadamore@1352 | 2688 | |
mcimadamore@1352 | 2689 | @Override |
mcimadamore@1352 | 2690 | ReferenceKind referenceKind(Symbol sym) { |
mcimadamore@1352 | 2691 | if (sym.isStatic()) { |
mcimadamore@1435 | 2692 | return ReferenceKind.STATIC; |
mcimadamore@1352 | 2693 | } else { |
mcimadamore@1352 | 2694 | Name selName = TreeInfo.name(referenceTree.getQualifierExpression()); |
mcimadamore@1352 | 2695 | return selName != null && selName == names._super ? |
mcimadamore@1352 | 2696 | ReferenceKind.SUPER : |
mcimadamore@1352 | 2697 | ReferenceKind.BOUND; |
mcimadamore@1352 | 2698 | } |
mcimadamore@1352 | 2699 | } |
mcimadamore@1352 | 2700 | } |
mcimadamore@1352 | 2701 | |
mcimadamore@1352 | 2702 | /** |
mcimadamore@1352 | 2703 | * Helper class for unbound method reference lookup. Essentially the same |
mcimadamore@1352 | 2704 | * as the basic method reference lookup helper; main difference is that static |
mcimadamore@1352 | 2705 | * lookup results are thrown away. If qualifier type is raw, an attempt to |
mcimadamore@1352 | 2706 | * infer a parameterized type is made using the first actual argument (that |
mcimadamore@1352 | 2707 | * would otherwise be ignored during the lookup). |
mcimadamore@1352 | 2708 | */ |
mcimadamore@1352 | 2709 | class UnboundMethodReferenceLookupHelper extends MethodReferenceLookupHelper { |
mcimadamore@1352 | 2710 | |
mcimadamore@1352 | 2711 | UnboundMethodReferenceLookupHelper(JCMemberReference referenceTree, Name name, Type site, |
mcimadamore@1394 | 2712 | List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) { |
mcimadamore@1581 | 2713 | super(referenceTree, name, site, argtypes.tail, typeargtypes, maxPhase); |
mcimadamore@1581 | 2714 | Type asSuperSite = types.asSuper(argtypes.head, site.tsym); |
mcimadamore@1581 | 2715 | if (site.isRaw() && !asSuperSite.isErroneous()) { |
mcimadamore@1581 | 2716 | this.site = asSuperSite; |
mcimadamore@1581 | 2717 | } |
mcimadamore@1352 | 2718 | } |
mcimadamore@1352 | 2719 | |
mcimadamore@1352 | 2720 | @Override |
mcimadamore@1352 | 2721 | protected Symbol adjustLookupResult(Env<AttrContext> env, Symbol sym) { |
mcimadamore@1352 | 2722 | return sym.kind != MTH || !sym.isStatic() ? sym : new StaticError(sym); |
mcimadamore@1352 | 2723 | } |
mcimadamore@1352 | 2724 | |
mcimadamore@1352 | 2725 | @Override |
mcimadamore@1352 | 2726 | ReferenceLookupHelper unboundLookup() { |
mcimadamore@1352 | 2727 | return this; |
mcimadamore@1352 | 2728 | } |
mcimadamore@1352 | 2729 | |
mcimadamore@1352 | 2730 | @Override |
mcimadamore@1352 | 2731 | ReferenceKind referenceKind(Symbol sym) { |
mcimadamore@1352 | 2732 | return ReferenceKind.UNBOUND; |
mcimadamore@1352 | 2733 | } |
mcimadamore@1352 | 2734 | } |
mcimadamore@1352 | 2735 | |
mcimadamore@1352 | 2736 | /** |
mcimadamore@1496 | 2737 | * Helper class for array constructor lookup; an array constructor lookup |
mcimadamore@1496 | 2738 | * is simulated by looking up a method that returns the array type specified |
mcimadamore@1496 | 2739 | * as qualifier, and that accepts a single int parameter (size of the array). |
mcimadamore@1496 | 2740 | */ |
mcimadamore@1496 | 2741 | class ArrayConstructorReferenceLookupHelper extends ReferenceLookupHelper { |
mcimadamore@1496 | 2742 | |
mcimadamore@1496 | 2743 | ArrayConstructorReferenceLookupHelper(JCMemberReference referenceTree, Type site, List<Type> argtypes, |
mcimadamore@1496 | 2744 | List<Type> typeargtypes, MethodResolutionPhase maxPhase) { |
mcimadamore@1496 | 2745 | super(referenceTree, names.init, site, argtypes, typeargtypes, maxPhase); |
mcimadamore@1496 | 2746 | } |
mcimadamore@1496 | 2747 | |
mcimadamore@1496 | 2748 | @Override |
mcimadamore@1496 | 2749 | protected Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) { |
mcimadamore@1496 | 2750 | Scope sc = new Scope(syms.arrayClass); |
mcimadamore@1496 | 2751 | MethodSymbol arrayConstr = new MethodSymbol(PUBLIC, name, null, site.tsym); |
mcimadamore@1496 | 2752 | arrayConstr.type = new MethodType(List.of(syms.intType), site, List.<Type>nil(), syms.methodClass); |
mcimadamore@1496 | 2753 | sc.enter(arrayConstr); |
mcimadamore@1496 | 2754 | return findMethodInScope(env, site, name, argtypes, typeargtypes, sc, methodNotFound, phase.isBoxingRequired(), phase.isVarargsRequired(), false, false); |
mcimadamore@1496 | 2755 | } |
mcimadamore@1496 | 2756 | |
mcimadamore@1496 | 2757 | @Override |
mcimadamore@1496 | 2758 | ReferenceKind referenceKind(Symbol sym) { |
mcimadamore@1496 | 2759 | return ReferenceKind.ARRAY_CTOR; |
mcimadamore@1496 | 2760 | } |
mcimadamore@1496 | 2761 | } |
mcimadamore@1496 | 2762 | |
mcimadamore@1496 | 2763 | /** |
mcimadamore@1352 | 2764 | * Helper class for constructor reference lookup. The lookup logic is based |
mcimadamore@1352 | 2765 | * upon either Resolve.findMethod or Resolve.findDiamond - depending on |
mcimadamore@1352 | 2766 | * whether the constructor reference needs diamond inference (this is the case |
mcimadamore@1352 | 2767 | * if the qualifier type is raw). A special erroneous symbol is returned |
mcimadamore@1352 | 2768 | * if the lookup returns the constructor of an inner class and there's no |
mcimadamore@1352 | 2769 | * enclosing instance in scope. |
mcimadamore@1352 | 2770 | */ |
mcimadamore@1352 | 2771 | class ConstructorReferenceLookupHelper extends ReferenceLookupHelper { |
mcimadamore@1352 | 2772 | |
mcimadamore@1352 | 2773 | boolean needsInference; |
mcimadamore@1352 | 2774 | |
mcimadamore@1352 | 2775 | ConstructorReferenceLookupHelper(JCMemberReference referenceTree, Type site, List<Type> argtypes, |
mcimadamore@1394 | 2776 | List<Type> typeargtypes, MethodResolutionPhase maxPhase) { |
mcimadamore@1394 | 2777 | super(referenceTree, names.init, site, argtypes, typeargtypes, maxPhase); |
mcimadamore@1352 | 2778 | if (site.isRaw()) { |
mcimadamore@1352 | 2779 | this.site = new ClassType(site.getEnclosingType(), site.tsym.type.getTypeArguments(), site.tsym); |
mcimadamore@1352 | 2780 | needsInference = true; |
mcimadamore@1352 | 2781 | } |
mcimadamore@1352 | 2782 | } |
mcimadamore@1352 | 2783 | |
mcimadamore@1352 | 2784 | @Override |
mcimadamore@1394 | 2785 | protected Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) { |
mcimadamore@1352 | 2786 | Symbol sym = needsInference ? |
mcimadamore@1352 | 2787 | findDiamond(env, site, argtypes, typeargtypes, phase.isBoxingRequired(), phase.isVarargsRequired()) : |
mcimadamore@1352 | 2788 | findMethod(env, site, name, argtypes, typeargtypes, |
mcimadamore@1352 | 2789 | phase.isBoxingRequired(), phase.isVarargsRequired(), syms.operatorNames.contains(name)); |
mcimadamore@1352 | 2790 | return sym.kind != MTH || |
jjg@1374 | 2791 | site.getEnclosingType().hasTag(NONE) || |
mcimadamore@1352 | 2792 | hasEnclosingInstance(env, site) ? |
mcimadamore@1352 | 2793 | sym : new InvalidSymbolError(Kinds.MISSING_ENCL, sym, null) { |
mcimadamore@1352 | 2794 | @Override |
mcimadamore@1352 | 2795 | JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos, Symbol location, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) { |
mcimadamore@1352 | 2796 | return diags.create(dkind, log.currentSource(), pos, |
mcimadamore@1352 | 2797 | "cant.access.inner.cls.constr", site.tsym.name, argtypes, site.getEnclosingType()); |
mcimadamore@1352 | 2798 | } |
mcimadamore@1352 | 2799 | }; |
mcimadamore@1352 | 2800 | } |
mcimadamore@1352 | 2801 | |
mcimadamore@1352 | 2802 | @Override |
mcimadamore@1352 | 2803 | ReferenceKind referenceKind(Symbol sym) { |
jjg@1374 | 2804 | return site.getEnclosingType().hasTag(NONE) ? |
mcimadamore@1352 | 2805 | ReferenceKind.TOPLEVEL : ReferenceKind.IMPLICIT_INNER; |
mcimadamore@1352 | 2806 | } |
mcimadamore@1352 | 2807 | } |
mcimadamore@1352 | 2808 | |
mcimadamore@1352 | 2809 | /** |
mcimadamore@1394 | 2810 | * Main overload resolution routine. On each overload resolution step, a |
mcimadamore@1394 | 2811 | * lookup helper class is used to perform the method/constructor lookup; |
mcimadamore@1394 | 2812 | * at the end of the lookup, the helper is used to validate the results |
mcimadamore@1394 | 2813 | * (this last step might trigger overload resolution diagnostics). |
mcimadamore@1352 | 2814 | */ |
mcimadamore@1394 | 2815 | Symbol lookupMethod(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, LookupHelper lookupHelper) { |
mcimadamore@1394 | 2816 | return lookupMethod(env, pos, location, new MethodResolutionContext(), lookupHelper); |
mcimadamore@1394 | 2817 | } |
mcimadamore@1394 | 2818 | |
mcimadamore@1394 | 2819 | Symbol lookupMethod(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, |
mcimadamore@1394 | 2820 | MethodResolutionContext resolveContext, LookupHelper lookupHelper) { |
mcimadamore@1352 | 2821 | MethodResolutionContext prevResolutionContext = currentResolutionContext; |
mcimadamore@1352 | 2822 | try { |
mcimadamore@1394 | 2823 | Symbol bestSoFar = methodNotFound; |
mcimadamore@1394 | 2824 | currentResolutionContext = resolveContext; |
mcimadamore@1394 | 2825 | for (MethodResolutionPhase phase : methodResolutionSteps) { |
mcimadamore@1394 | 2826 | if (!phase.isApplicable(boxingEnabled, varargsEnabled) || |
mcimadamore@1394 | 2827 | lookupHelper.shouldStop(bestSoFar, phase)) break; |
mcimadamore@1394 | 2828 | MethodResolutionPhase prevPhase = currentResolutionContext.step; |
mcimadamore@1394 | 2829 | Symbol prevBest = bestSoFar; |
mcimadamore@1394 | 2830 | currentResolutionContext.step = phase; |
mcimadamore@1394 | 2831 | bestSoFar = phase.mergeResults(bestSoFar, lookupHelper.lookup(env, phase)); |
mcimadamore@1394 | 2832 | env.info.pendingResolutionPhase = (prevBest == bestSoFar) ? prevPhase : phase; |
mcimadamore@1352 | 2833 | } |
mcimadamore@1394 | 2834 | return lookupHelper.access(env, pos, location, bestSoFar); |
mcimadamore@1394 | 2835 | } finally { |
mcimadamore@1352 | 2836 | currentResolutionContext = prevResolutionContext; |
mcimadamore@1352 | 2837 | } |
mcimadamore@1352 | 2838 | } |
mcimadamore@1352 | 2839 | |
duke@1 | 2840 | /** |
duke@1 | 2841 | * Resolve `c.name' where name == this or name == super. |
duke@1 | 2842 | * @param pos The position to use for error reporting. |
duke@1 | 2843 | * @param env The environment current at the expression. |
duke@1 | 2844 | * @param c The qualifier. |
duke@1 | 2845 | * @param name The identifier's name. |
duke@1 | 2846 | */ |
duke@1 | 2847 | Symbol resolveSelf(DiagnosticPosition pos, |
duke@1 | 2848 | Env<AttrContext> env, |
duke@1 | 2849 | TypeSymbol c, |
duke@1 | 2850 | Name name) { |
duke@1 | 2851 | Env<AttrContext> env1 = env; |
duke@1 | 2852 | boolean staticOnly = false; |
duke@1 | 2853 | while (env1.outer != null) { |
duke@1 | 2854 | if (isStatic(env1)) staticOnly = true; |
duke@1 | 2855 | if (env1.enclClass.sym == c) { |
duke@1 | 2856 | Symbol sym = env1.info.scope.lookup(name).sym; |
duke@1 | 2857 | if (sym != null) { |
duke@1 | 2858 | if (staticOnly) sym = new StaticError(sym); |
mcimadamore@1347 | 2859 | return accessBase(sym, pos, env.enclClass.sym.type, |
duke@1 | 2860 | name, true); |
duke@1 | 2861 | } |
duke@1 | 2862 | } |
duke@1 | 2863 | if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true; |
duke@1 | 2864 | env1 = env1.outer; |
duke@1 | 2865 | } |
mcimadamore@1393 | 2866 | if (allowDefaultMethods && c.isInterface() && |
mcimadamore@1393 | 2867 | name == names._super && !isStatic(env) && |
mcimadamore@1415 | 2868 | types.isDirectSuperInterface(c, env.enclClass.sym)) { |
mcimadamore@1393 | 2869 | //this might be a default super call if one of the superinterfaces is 'c' |
mcimadamore@1393 | 2870 | for (Type t : pruneInterfaces(env.enclClass.type)) { |
mcimadamore@1393 | 2871 | if (t.tsym == c) { |
mcimadamore@1393 | 2872 | env.info.defaultSuperCallSite = t; |
mcimadamore@1393 | 2873 | return new VarSymbol(0, names._super, |
mcimadamore@1393 | 2874 | types.asSuper(env.enclClass.type, c), env.enclClass.sym); |
mcimadamore@1393 | 2875 | } |
mcimadamore@1393 | 2876 | } |
mcimadamore@1393 | 2877 | //find a direct superinterface that is a subtype of 'c' |
mcimadamore@1393 | 2878 | for (Type i : types.interfaces(env.enclClass.type)) { |
mcimadamore@1393 | 2879 | if (i.tsym.isSubClass(c, types) && i.tsym != c) { |
mcimadamore@1393 | 2880 | log.error(pos, "illegal.default.super.call", c, |
mcimadamore@1393 | 2881 | diags.fragment("redundant.supertype", c, i)); |
mcimadamore@1393 | 2882 | return syms.errSymbol; |
mcimadamore@1393 | 2883 | } |
mcimadamore@1393 | 2884 | } |
mcimadamore@1393 | 2885 | Assert.error(); |
mcimadamore@1393 | 2886 | } |
duke@1 | 2887 | log.error(pos, "not.encl.class", c); |
duke@1 | 2888 | return syms.errSymbol; |
duke@1 | 2889 | } |
mcimadamore@1393 | 2890 | //where |
mcimadamore@1393 | 2891 | private List<Type> pruneInterfaces(Type t) { |
mcimadamore@1393 | 2892 | ListBuffer<Type> result = ListBuffer.lb(); |
mcimadamore@1393 | 2893 | for (Type t1 : types.interfaces(t)) { |
mcimadamore@1393 | 2894 | boolean shouldAdd = true; |
mcimadamore@1393 | 2895 | for (Type t2 : types.interfaces(t)) { |
mcimadamore@1393 | 2896 | if (t1 != t2 && types.isSubtypeNoCapture(t2, t1)) { |
mcimadamore@1393 | 2897 | shouldAdd = false; |
mcimadamore@1393 | 2898 | } |
mcimadamore@1393 | 2899 | } |
mcimadamore@1393 | 2900 | if (shouldAdd) { |
mcimadamore@1393 | 2901 | result.append(t1); |
mcimadamore@1393 | 2902 | } |
mcimadamore@1393 | 2903 | } |
mcimadamore@1393 | 2904 | return result.toList(); |
mcimadamore@1393 | 2905 | } |
mcimadamore@1393 | 2906 | |
duke@1 | 2907 | |
duke@1 | 2908 | /** |
duke@1 | 2909 | * Resolve `c.this' for an enclosing class c that contains the |
duke@1 | 2910 | * named member. |
duke@1 | 2911 | * @param pos The position to use for error reporting. |
duke@1 | 2912 | * @param env The environment current at the expression. |
duke@1 | 2913 | * @param member The member that must be contained in the result. |
duke@1 | 2914 | */ |
duke@1 | 2915 | Symbol resolveSelfContaining(DiagnosticPosition pos, |
duke@1 | 2916 | Env<AttrContext> env, |
mcimadamore@901 | 2917 | Symbol member, |
mcimadamore@901 | 2918 | boolean isSuperCall) { |
mcimadamore@1352 | 2919 | Symbol sym = resolveSelfContainingInternal(env, member, isSuperCall); |
mcimadamore@1352 | 2920 | if (sym == null) { |
mcimadamore@1352 | 2921 | log.error(pos, "encl.class.required", member); |
mcimadamore@1352 | 2922 | return syms.errSymbol; |
mcimadamore@1352 | 2923 | } else { |
mcimadamore@1352 | 2924 | return accessBase(sym, pos, env.enclClass.sym.type, sym.name, true); |
mcimadamore@1352 | 2925 | } |
mcimadamore@1352 | 2926 | } |
mcimadamore@1352 | 2927 | |
mcimadamore@1352 | 2928 | boolean hasEnclosingInstance(Env<AttrContext> env, Type type) { |
mcimadamore@1352 | 2929 | Symbol encl = resolveSelfContainingInternal(env, type.tsym, false); |
mcimadamore@1352 | 2930 | return encl != null && encl.kind < ERRONEOUS; |
mcimadamore@1352 | 2931 | } |
mcimadamore@1352 | 2932 | |
mcimadamore@1352 | 2933 | private Symbol resolveSelfContainingInternal(Env<AttrContext> env, |
mcimadamore@1352 | 2934 | Symbol member, |
mcimadamore@1352 | 2935 | boolean isSuperCall) { |
duke@1 | 2936 | Name name = names._this; |
mcimadamore@901 | 2937 | Env<AttrContext> env1 = isSuperCall ? env.outer : env; |
duke@1 | 2938 | boolean staticOnly = false; |
mcimadamore@901 | 2939 | if (env1 != null) { |
mcimadamore@901 | 2940 | while (env1 != null && env1.outer != null) { |
mcimadamore@901 | 2941 | if (isStatic(env1)) staticOnly = true; |
mcimadamore@901 | 2942 | if (env1.enclClass.sym.isSubClass(member.owner, types)) { |
mcimadamore@901 | 2943 | Symbol sym = env1.info.scope.lookup(name).sym; |
mcimadamore@901 | 2944 | if (sym != null) { |
mcimadamore@901 | 2945 | if (staticOnly) sym = new StaticError(sym); |
mcimadamore@1352 | 2946 | return sym; |
mcimadamore@901 | 2947 | } |
duke@1 | 2948 | } |
mcimadamore@901 | 2949 | if ((env1.enclClass.sym.flags() & STATIC) != 0) |
mcimadamore@901 | 2950 | staticOnly = true; |
mcimadamore@901 | 2951 | env1 = env1.outer; |
duke@1 | 2952 | } |
duke@1 | 2953 | } |
mcimadamore@1352 | 2954 | return null; |
duke@1 | 2955 | } |
duke@1 | 2956 | |
duke@1 | 2957 | /** |
duke@1 | 2958 | * Resolve an appropriate implicit this instance for t's container. |
jjh@972 | 2959 | * JLS 8.8.5.1 and 15.9.2 |
duke@1 | 2960 | */ |
duke@1 | 2961 | Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t) { |
mcimadamore@901 | 2962 | return resolveImplicitThis(pos, env, t, false); |
mcimadamore@901 | 2963 | } |
mcimadamore@901 | 2964 | |
mcimadamore@901 | 2965 | Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t, boolean isSuperCall) { |
duke@1 | 2966 | Type thisType = (((t.tsym.owner.kind & (MTH|VAR)) != 0) |
duke@1 | 2967 | ? resolveSelf(pos, env, t.getEnclosingType().tsym, names._this) |
mcimadamore@901 | 2968 | : resolveSelfContaining(pos, env, t.tsym, isSuperCall)).type; |
duke@1 | 2969 | if (env.info.isSelfCall && thisType.tsym == env.enclClass.sym) |
duke@1 | 2970 | log.error(pos, "cant.ref.before.ctor.called", "this"); |
duke@1 | 2971 | return thisType; |
duke@1 | 2972 | } |
duke@1 | 2973 | |
duke@1 | 2974 | /* *************************************************************************** |
duke@1 | 2975 | * ResolveError classes, indicating error situations when accessing symbols |
duke@1 | 2976 | ****************************************************************************/ |
duke@1 | 2977 | |
mcimadamore@1221 | 2978 | //used by TransTypes when checking target type of synthetic cast |
mcimadamore@1221 | 2979 | public void logAccessErrorInternal(Env<AttrContext> env, JCTree tree, Type type) { |
mcimadamore@1221 | 2980 | AccessError error = new AccessError(env, env.enclClass.type, type.tsym); |
mcimadamore@1221 | 2981 | logResolveError(error, tree.pos(), env.enclClass.sym, env.enclClass.type, null, null, null); |
mcimadamore@302 | 2982 | } |
mcimadamore@302 | 2983 | //where |
mcimadamore@302 | 2984 | private void logResolveError(ResolveError error, |
mcimadamore@302 | 2985 | DiagnosticPosition pos, |
mcimadamore@829 | 2986 | Symbol location, |
mcimadamore@302 | 2987 | Type site, |
mcimadamore@302 | 2988 | Name name, |
mcimadamore@302 | 2989 | List<Type> argtypes, |
mcimadamore@302 | 2990 | List<Type> typeargtypes) { |
mcimadamore@302 | 2991 | JCDiagnostic d = error.getDiagnostic(JCDiagnostic.DiagnosticType.ERROR, |
mcimadamore@829 | 2992 | pos, location, site, name, argtypes, typeargtypes); |
jjg@643 | 2993 | if (d != null) { |
jjg@643 | 2994 | d.setFlag(DiagnosticFlag.RESOLVE_ERROR); |
mcimadamore@302 | 2995 | log.report(d); |
jjg@643 | 2996 | } |
duke@1 | 2997 | } |
duke@1 | 2998 | |
mcimadamore@161 | 2999 | private final LocalizedString noArgs = new LocalizedString("compiler.misc.no.args"); |
mcimadamore@161 | 3000 | |
mcimadamore@161 | 3001 | public Object methodArguments(List<Type> argtypes) { |
mcimadamore@1348 | 3002 | if (argtypes == null || argtypes.isEmpty()) { |
mcimadamore@1348 | 3003 | return noArgs; |
mcimadamore@1348 | 3004 | } else { |
mcimadamore@1348 | 3005 | ListBuffer<Object> diagArgs = ListBuffer.lb(); |
mcimadamore@1348 | 3006 | for (Type t : argtypes) { |
jjg@1374 | 3007 | if (t.hasTag(DEFERRED)) { |
mcimadamore@1348 | 3008 | diagArgs.append(((DeferredAttr.DeferredType)t).tree); |
mcimadamore@1348 | 3009 | } else { |
mcimadamore@1348 | 3010 | diagArgs.append(t); |
mcimadamore@1348 | 3011 | } |
mcimadamore@1348 | 3012 | } |
mcimadamore@1348 | 3013 | return diagArgs; |
mcimadamore@1348 | 3014 | } |
mcimadamore@161 | 3015 | } |
mcimadamore@161 | 3016 | |
mcimadamore@302 | 3017 | /** |
mcimadamore@302 | 3018 | * Root class for resolution errors. Subclass of ResolveError |
mcimadamore@302 | 3019 | * represent a different kinds of resolution error - as such they must |
mcimadamore@302 | 3020 | * specify how they map into concrete compiler diagnostics. |
duke@1 | 3021 | */ |
mcimadamore@1352 | 3022 | abstract class ResolveError extends Symbol { |
duke@1 | 3023 | |
mcimadamore@302 | 3024 | /** The name of the kind of error, for debugging only. */ |
mcimadamore@302 | 3025 | final String debugName; |
mcimadamore@302 | 3026 | |
mcimadamore@302 | 3027 | ResolveError(int kind, String debugName) { |
duke@1 | 3028 | super(kind, 0, null, null, null); |
duke@1 | 3029 | this.debugName = debugName; |
duke@1 | 3030 | } |
duke@1 | 3031 | |
mcimadamore@302 | 3032 | @Override |
duke@1 | 3033 | public <R, P> R accept(ElementVisitor<R, P> v, P p) { |
duke@1 | 3034 | throw new AssertionError(); |
duke@1 | 3035 | } |
duke@1 | 3036 | |
mcimadamore@302 | 3037 | @Override |
duke@1 | 3038 | public String toString() { |
mcimadamore@302 | 3039 | return debugName; |
duke@1 | 3040 | } |
duke@1 | 3041 | |
mcimadamore@302 | 3042 | @Override |
mcimadamore@302 | 3043 | public boolean exists() { |
mcimadamore@302 | 3044 | return false; |
duke@1 | 3045 | } |
duke@1 | 3046 | |
mcimadamore@302 | 3047 | /** |
mcimadamore@302 | 3048 | * Create an external representation for this erroneous symbol to be |
mcimadamore@302 | 3049 | * used during attribution - by default this returns the symbol of a |
mcimadamore@302 | 3050 | * brand new error type which stores the original type found |
mcimadamore@302 | 3051 | * during resolution. |
mcimadamore@302 | 3052 | * |
mcimadamore@302 | 3053 | * @param name the name used during resolution |
mcimadamore@302 | 3054 | * @param location the location from which the symbol is accessed |
duke@1 | 3055 | */ |
mcimadamore@302 | 3056 | protected Symbol access(Name name, TypeSymbol location) { |
mcimadamore@302 | 3057 | return types.createErrorType(name, location, syms.errSymbol.type).tsym; |
duke@1 | 3058 | } |
duke@1 | 3059 | |
mcimadamore@302 | 3060 | /** |
mcimadamore@302 | 3061 | * Create a diagnostic representing this resolution error. |
mcimadamore@302 | 3062 | * |
mcimadamore@302 | 3063 | * @param dkind The kind of the diagnostic to be created (e.g error). |
mcimadamore@302 | 3064 | * @param pos The position to be used for error reporting. |
mcimadamore@302 | 3065 | * @param site The original type from where the selection took place. |
mcimadamore@302 | 3066 | * @param name The name of the symbol to be resolved. |
mcimadamore@302 | 3067 | * @param argtypes The invocation's value arguments, |
mcimadamore@302 | 3068 | * if we looked for a method. |
mcimadamore@302 | 3069 | * @param typeargtypes The invocation's type arguments, |
mcimadamore@302 | 3070 | * if we looked for a method. |
mcimadamore@302 | 3071 | */ |
mcimadamore@302 | 3072 | abstract JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind, |
mcimadamore@302 | 3073 | DiagnosticPosition pos, |
mcimadamore@829 | 3074 | Symbol location, |
mcimadamore@302 | 3075 | Type site, |
mcimadamore@302 | 3076 | Name name, |
mcimadamore@302 | 3077 | List<Type> argtypes, |
mcimadamore@302 | 3078 | List<Type> typeargtypes); |
duke@1 | 3079 | } |
duke@1 | 3080 | |
mcimadamore@302 | 3081 | /** |
mcimadamore@302 | 3082 | * This class is the root class of all resolution errors caused by |
mcimadamore@302 | 3083 | * an invalid symbol being found during resolution. |
duke@1 | 3084 | */ |
mcimadamore@302 | 3085 | abstract class InvalidSymbolError extends ResolveError { |
mcimadamore@302 | 3086 | |
mcimadamore@302 | 3087 | /** The invalid symbol found during resolution */ |
mcimadamore@302 | 3088 | Symbol sym; |
mcimadamore@302 | 3089 | |
mcimadamore@302 | 3090 | InvalidSymbolError(int kind, Symbol sym, String debugName) { |
mcimadamore@302 | 3091 | super(kind, debugName); |
mcimadamore@302 | 3092 | this.sym = sym; |
mcimadamore@302 | 3093 | } |
mcimadamore@302 | 3094 | |
mcimadamore@302 | 3095 | @Override |
mcimadamore@302 | 3096 | public boolean exists() { |
mcimadamore@302 | 3097 | return true; |
mcimadamore@302 | 3098 | } |
mcimadamore@302 | 3099 | |
mcimadamore@302 | 3100 | @Override |
mcimadamore@302 | 3101 | public String toString() { |
mcimadamore@302 | 3102 | return super.toString() + " wrongSym=" + sym; |
mcimadamore@302 | 3103 | } |
mcimadamore@302 | 3104 | |
mcimadamore@302 | 3105 | @Override |
mcimadamore@302 | 3106 | public Symbol access(Name name, TypeSymbol location) { |
mcimadamore@1480 | 3107 | if ((sym.kind & ERRONEOUS) == 0 && (sym.kind & TYP) != 0) |
mcimadamore@302 | 3108 | return types.createErrorType(name, location, sym.type).tsym; |
mcimadamore@302 | 3109 | else |
mcimadamore@302 | 3110 | return sym; |
mcimadamore@302 | 3111 | } |
mcimadamore@302 | 3112 | } |
mcimadamore@302 | 3113 | |
mcimadamore@302 | 3114 | /** |
mcimadamore@302 | 3115 | * InvalidSymbolError error class indicating that a symbol matching a |
mcimadamore@302 | 3116 | * given name does not exists in a given site. |
mcimadamore@302 | 3117 | */ |
mcimadamore@302 | 3118 | class SymbolNotFoundError extends ResolveError { |
mcimadamore@302 | 3119 | |
mcimadamore@302 | 3120 | SymbolNotFoundError(int kind) { |
mcimadamore@302 | 3121 | super(kind, "symbol not found error"); |
mcimadamore@302 | 3122 | } |
mcimadamore@302 | 3123 | |
mcimadamore@302 | 3124 | @Override |
mcimadamore@302 | 3125 | JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind, |
mcimadamore@302 | 3126 | DiagnosticPosition pos, |
mcimadamore@829 | 3127 | Symbol location, |
mcimadamore@302 | 3128 | Type site, |
mcimadamore@302 | 3129 | Name name, |
mcimadamore@302 | 3130 | List<Type> argtypes, |
mcimadamore@302 | 3131 | List<Type> typeargtypes) { |
mcimadamore@302 | 3132 | argtypes = argtypes == null ? List.<Type>nil() : argtypes; |
mcimadamore@302 | 3133 | typeargtypes = typeargtypes == null ? List.<Type>nil() : typeargtypes; |
mcimadamore@302 | 3134 | if (name == names.error) |
mcimadamore@302 | 3135 | return null; |
mcimadamore@302 | 3136 | |
mcimadamore@1347 | 3137 | if (syms.operatorNames.contains(name)) { |
mcimadamore@829 | 3138 | boolean isUnaryOp = argtypes.size() == 1; |
mcimadamore@829 | 3139 | String key = argtypes.size() == 1 ? |
mcimadamore@829 | 3140 | "operator.cant.be.applied" : |
mcimadamore@829 | 3141 | "operator.cant.be.applied.1"; |
mcimadamore@829 | 3142 | Type first = argtypes.head; |
mcimadamore@829 | 3143 | Type second = !isUnaryOp ? argtypes.tail.head : null; |
jjg@612 | 3144 | return diags.create(dkind, log.currentSource(), pos, |
mcimadamore@829 | 3145 | key, name, first, second); |
mcimadamore@302 | 3146 | } |
mcimadamore@302 | 3147 | boolean hasLocation = false; |
mcimadamore@855 | 3148 | if (location == null) { |
mcimadamore@855 | 3149 | location = site.tsym; |
mcimadamore@855 | 3150 | } |
mcimadamore@829 | 3151 | if (!location.name.isEmpty()) { |
mcimadamore@829 | 3152 | if (location.kind == PCK && !site.tsym.exists()) { |
jjg@612 | 3153 | return diags.create(dkind, log.currentSource(), pos, |
mcimadamore@829 | 3154 | "doesnt.exist", location); |
mcimadamore@302 | 3155 | } |
mcimadamore@829 | 3156 | hasLocation = !location.name.equals(names._this) && |
mcimadamore@829 | 3157 | !location.name.equals(names._super); |
mcimadamore@302 | 3158 | } |
mcimadamore@1347 | 3159 | boolean isConstructor = kind == ABSENT_MTH && name == names.init; |
mcimadamore@302 | 3160 | KindName kindname = isConstructor ? KindName.CONSTRUCTOR : absentKind(kind); |
mcimadamore@302 | 3161 | Name idname = isConstructor ? site.tsym.name : name; |
mcimadamore@302 | 3162 | String errKey = getErrorKey(kindname, typeargtypes.nonEmpty(), hasLocation); |
mcimadamore@302 | 3163 | if (hasLocation) { |
jjg@612 | 3164 | return diags.create(dkind, log.currentSource(), pos, |
mcimadamore@302 | 3165 | errKey, kindname, idname, //symbol kindname, name |
mcimadamore@1456 | 3166 | typeargtypes, args(argtypes), //type parameters and arguments (if any) |
mcimadamore@855 | 3167 | getLocationDiag(location, site)); //location kindname, type |
mcimadamore@302 | 3168 | } |
mcimadamore@302 | 3169 | else { |
jjg@612 | 3170 | return diags.create(dkind, log.currentSource(), pos, |
mcimadamore@302 | 3171 | errKey, kindname, idname, //symbol kindname, name |
mcimadamore@1456 | 3172 | typeargtypes, args(argtypes)); //type parameters and arguments (if any) |
mcimadamore@302 | 3173 | } |
mcimadamore@302 | 3174 | } |
mcimadamore@302 | 3175 | //where |
mcimadamore@1456 | 3176 | private Object args(List<Type> args) { |
mcimadamore@1456 | 3177 | return args.isEmpty() ? args : methodArguments(args); |
mcimadamore@1456 | 3178 | } |
mcimadamore@1456 | 3179 | |
mcimadamore@302 | 3180 | private String getErrorKey(KindName kindname, boolean hasTypeArgs, boolean hasLocation) { |
mcimadamore@302 | 3181 | String key = "cant.resolve"; |
mcimadamore@302 | 3182 | String suffix = hasLocation ? ".location" : ""; |
mcimadamore@302 | 3183 | switch (kindname) { |
mcimadamore@302 | 3184 | case METHOD: |
mcimadamore@302 | 3185 | case CONSTRUCTOR: { |
mcimadamore@302 | 3186 | suffix += ".args"; |
mcimadamore@302 | 3187 | suffix += hasTypeArgs ? ".params" : ""; |
mcimadamore@302 | 3188 | } |
mcimadamore@302 | 3189 | } |
mcimadamore@302 | 3190 | return key + suffix; |
mcimadamore@302 | 3191 | } |
mcimadamore@855 | 3192 | private JCDiagnostic getLocationDiag(Symbol location, Type site) { |
mcimadamore@855 | 3193 | if (location.kind == VAR) { |
mcimadamore@855 | 3194 | return diags.fragment("location.1", |
mcimadamore@829 | 3195 | kindName(location), |
mcimadamore@829 | 3196 | location, |
mcimadamore@855 | 3197 | location.type); |
mcimadamore@855 | 3198 | } else { |
mcimadamore@855 | 3199 | return diags.fragment("location", |
mcimadamore@855 | 3200 | typeKindName(site), |
mcimadamore@855 | 3201 | site, |
mcimadamore@855 | 3202 | null); |
mcimadamore@855 | 3203 | } |
mcimadamore@829 | 3204 | } |
mcimadamore@302 | 3205 | } |
mcimadamore@302 | 3206 | |
mcimadamore@302 | 3207 | /** |
mcimadamore@302 | 3208 | * InvalidSymbolError error class indicating that a given symbol |
mcimadamore@302 | 3209 | * (either a method, a constructor or an operand) is not applicable |
mcimadamore@302 | 3210 | * given an actual arguments/type argument list. |
mcimadamore@302 | 3211 | */ |
mcimadamore@1215 | 3212 | class InapplicableSymbolError extends ResolveError { |
mcimadamore@302 | 3213 | |
mcimadamore@1352 | 3214 | protected MethodResolutionContext resolveContext; |
mcimadamore@1352 | 3215 | |
mcimadamore@1352 | 3216 | InapplicableSymbolError(MethodResolutionContext context) { |
mcimadamore@1352 | 3217 | this(WRONG_MTH, "inapplicable symbol error", context); |
mcimadamore@302 | 3218 | } |
mcimadamore@302 | 3219 | |
mcimadamore@1352 | 3220 | protected InapplicableSymbolError(int kind, String debugName, MethodResolutionContext context) { |
mcimadamore@1215 | 3221 | super(kind, debugName); |
mcimadamore@1352 | 3222 | this.resolveContext = context; |
mcimadamore@302 | 3223 | } |
mcimadamore@302 | 3224 | |
mcimadamore@302 | 3225 | @Override |
mcimadamore@302 | 3226 | public String toString() { |
mcimadamore@1215 | 3227 | return super.toString(); |
mcimadamore@1215 | 3228 | } |
mcimadamore@1215 | 3229 | |
mcimadamore@1215 | 3230 | @Override |
mcimadamore@1215 | 3231 | public boolean exists() { |
mcimadamore@1215 | 3232 | return true; |
mcimadamore@302 | 3233 | } |
mcimadamore@302 | 3234 | |
mcimadamore@302 | 3235 | @Override |
mcimadamore@302 | 3236 | JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind, |
mcimadamore@302 | 3237 | DiagnosticPosition pos, |
mcimadamore@829 | 3238 | Symbol location, |
mcimadamore@302 | 3239 | Type site, |
mcimadamore@302 | 3240 | Name name, |
mcimadamore@302 | 3241 | List<Type> argtypes, |
mcimadamore@302 | 3242 | List<Type> typeargtypes) { |
mcimadamore@302 | 3243 | if (name == names.error) |
mcimadamore@302 | 3244 | return null; |
mcimadamore@302 | 3245 | |
mcimadamore@1347 | 3246 | if (syms.operatorNames.contains(name)) { |
mcimadamore@853 | 3247 | boolean isUnaryOp = argtypes.size() == 1; |
mcimadamore@853 | 3248 | String key = argtypes.size() == 1 ? |
mcimadamore@853 | 3249 | "operator.cant.be.applied" : |
mcimadamore@853 | 3250 | "operator.cant.be.applied.1"; |
mcimadamore@853 | 3251 | Type first = argtypes.head; |
mcimadamore@853 | 3252 | Type second = !isUnaryOp ? argtypes.tail.head : null; |
mcimadamore@853 | 3253 | return diags.create(dkind, log.currentSource(), pos, |
mcimadamore@853 | 3254 | key, name, first, second); |
mcimadamore@302 | 3255 | } |
mcimadamore@302 | 3256 | else { |
mcimadamore@1215 | 3257 | Candidate c = errCandidate(); |
mcimadamore@1215 | 3258 | Symbol ws = c.sym.asMemberOf(site, types); |
jjg@612 | 3259 | return diags.create(dkind, log.currentSource(), pos, |
mcimadamore@1352 | 3260 | "cant.apply.symbol", |
mcimadamore@302 | 3261 | kindName(ws), |
mcimadamore@302 | 3262 | ws.name == names.init ? ws.owner.name : ws.name, |
mcimadamore@302 | 3263 | methodArguments(ws.type.getParameterTypes()), |
mcimadamore@302 | 3264 | methodArguments(argtypes), |
mcimadamore@302 | 3265 | kindName(ws.owner), |
mcimadamore@302 | 3266 | ws.owner.type, |
mcimadamore@1215 | 3267 | c.details); |
mcimadamore@302 | 3268 | } |
mcimadamore@302 | 3269 | } |
mcimadamore@302 | 3270 | |
mcimadamore@302 | 3271 | @Override |
mcimadamore@302 | 3272 | public Symbol access(Name name, TypeSymbol location) { |
mcimadamore@302 | 3273 | return types.createErrorType(name, location, syms.errSymbol.type).tsym; |
mcimadamore@302 | 3274 | } |
mcimadamore@1215 | 3275 | |
mcimadamore@1215 | 3276 | private Candidate errCandidate() { |
mcimadamore@1394 | 3277 | Candidate bestSoFar = null; |
mcimadamore@1352 | 3278 | for (Candidate c : resolveContext.candidates) { |
mcimadamore@1394 | 3279 | if (c.isApplicable()) continue; |
mcimadamore@1394 | 3280 | bestSoFar = c; |
mcimadamore@1215 | 3281 | } |
mcimadamore@1394 | 3282 | Assert.checkNonNull(bestSoFar); |
mcimadamore@1394 | 3283 | return bestSoFar; |
mcimadamore@1215 | 3284 | } |
mcimadamore@302 | 3285 | } |
mcimadamore@302 | 3286 | |
mcimadamore@302 | 3287 | /** |
mcimadamore@302 | 3288 | * ResolveError error class indicating that a set of symbols |
mcimadamore@302 | 3289 | * (either methods, constructors or operands) is not applicable |
mcimadamore@302 | 3290 | * given an actual arguments/type argument list. |
mcimadamore@302 | 3291 | */ |
mcimadamore@1215 | 3292 | class InapplicableSymbolsError extends InapplicableSymbolError { |
mcimadamore@689 | 3293 | |
mcimadamore@1352 | 3294 | InapplicableSymbolsError(MethodResolutionContext context) { |
mcimadamore@1352 | 3295 | super(WRONG_MTHS, "inapplicable symbols", context); |
mcimadamore@302 | 3296 | } |
mcimadamore@302 | 3297 | |
mcimadamore@302 | 3298 | @Override |
mcimadamore@302 | 3299 | JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind, |
mcimadamore@302 | 3300 | DiagnosticPosition pos, |
mcimadamore@829 | 3301 | Symbol location, |
mcimadamore@302 | 3302 | Type site, |
mcimadamore@302 | 3303 | Name name, |
mcimadamore@302 | 3304 | List<Type> argtypes, |
mcimadamore@302 | 3305 | List<Type> typeargtypes) { |
mcimadamore@1352 | 3306 | if (!resolveContext.candidates.isEmpty()) { |
mcimadamore@689 | 3307 | JCDiagnostic err = diags.create(dkind, |
mcimadamore@689 | 3308 | log.currentSource(), |
mcimadamore@689 | 3309 | pos, |
mcimadamore@689 | 3310 | "cant.apply.symbols", |
mcimadamore@689 | 3311 | name == names.init ? KindName.CONSTRUCTOR : absentKind(kind), |
mcimadamore@1394 | 3312 | name == names.init ? site.tsym.name : name, |
mcimadamore@1415 | 3313 | methodArguments(argtypes)); |
mcimadamore@689 | 3314 | return new JCDiagnostic.MultilineDiagnostic(err, candidateDetails(site)); |
mcimadamore@689 | 3315 | } else { |
mcimadamore@689 | 3316 | return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind, pos, |
mcimadamore@829 | 3317 | location, site, name, argtypes, typeargtypes); |
mcimadamore@689 | 3318 | } |
mcimadamore@689 | 3319 | } |
mcimadamore@689 | 3320 | |
mcimadamore@689 | 3321 | //where |
mcimadamore@689 | 3322 | List<JCDiagnostic> candidateDetails(Type site) { |
mcimadamore@1394 | 3323 | Map<Symbol, JCDiagnostic> details = new LinkedHashMap<Symbol, JCDiagnostic>(); |
mcimadamore@1352 | 3324 | for (Candidate c : resolveContext.candidates) { |
mcimadamore@1394 | 3325 | if (c.isApplicable()) continue; |
mcimadamore@1215 | 3326 | JCDiagnostic detailDiag = diags.fragment("inapplicable.method", |
mcimadamore@1215 | 3327 | Kinds.kindName(c.sym), |
mcimadamore@1215 | 3328 | c.sym.location(site, types), |
mcimadamore@1215 | 3329 | c.sym.asMemberOf(site, types), |
mcimadamore@1215 | 3330 | c.details); |
mcimadamore@1394 | 3331 | details.put(c.sym, detailDiag); |
mcimadamore@1215 | 3332 | } |
mcimadamore@1394 | 3333 | return List.from(details.values()); |
mcimadamore@689 | 3334 | } |
mcimadamore@302 | 3335 | } |
mcimadamore@302 | 3336 | |
mcimadamore@302 | 3337 | /** |
mcimadamore@302 | 3338 | * An InvalidSymbolError error class indicating that a symbol is not |
mcimadamore@302 | 3339 | * accessible from a given site |
mcimadamore@302 | 3340 | */ |
mcimadamore@302 | 3341 | class AccessError extends InvalidSymbolError { |
mcimadamore@302 | 3342 | |
mcimadamore@302 | 3343 | private Env<AttrContext> env; |
mcimadamore@302 | 3344 | private Type site; |
duke@1 | 3345 | |
duke@1 | 3346 | AccessError(Symbol sym) { |
duke@1 | 3347 | this(null, null, sym); |
duke@1 | 3348 | } |
duke@1 | 3349 | |
duke@1 | 3350 | AccessError(Env<AttrContext> env, Type site, Symbol sym) { |
duke@1 | 3351 | super(HIDDEN, sym, "access error"); |
duke@1 | 3352 | this.env = env; |
duke@1 | 3353 | this.site = site; |
duke@1 | 3354 | if (debugResolve) |
duke@1 | 3355 | log.error("proc.messager", sym + " @ " + site + " is inaccessible."); |
duke@1 | 3356 | } |
duke@1 | 3357 | |
mcimadamore@302 | 3358 | @Override |
mcimadamore@302 | 3359 | public boolean exists() { |
mcimadamore@302 | 3360 | return false; |
mcimadamore@302 | 3361 | } |
duke@1 | 3362 | |
mcimadamore@302 | 3363 | @Override |
mcimadamore@302 | 3364 | JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind, |
mcimadamore@302 | 3365 | DiagnosticPosition pos, |
mcimadamore@829 | 3366 | Symbol location, |
mcimadamore@302 | 3367 | Type site, |
mcimadamore@302 | 3368 | Name name, |
mcimadamore@302 | 3369 | List<Type> argtypes, |
mcimadamore@302 | 3370 | List<Type> typeargtypes) { |
jjg@1374 | 3371 | if (sym.owner.type.hasTag(ERROR)) |
mcimadamore@302 | 3372 | return null; |
mcimadamore@302 | 3373 | |
mcimadamore@302 | 3374 | if (sym.name == names.init && sym.owner != site.tsym) { |
mcimadamore@302 | 3375 | return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind, |
mcimadamore@829 | 3376 | pos, location, site, name, argtypes, typeargtypes); |
mcimadamore@302 | 3377 | } |
mcimadamore@302 | 3378 | else if ((sym.flags() & PUBLIC) != 0 |
mcimadamore@302 | 3379 | || (env != null && this.site != null |
mcimadamore@302 | 3380 | && !isAccessible(env, this.site))) { |
jjg@612 | 3381 | return diags.create(dkind, log.currentSource(), |
mcimadamore@302 | 3382 | pos, "not.def.access.class.intf.cant.access", |
mcimadamore@302 | 3383 | sym, sym.location()); |
mcimadamore@302 | 3384 | } |
mcimadamore@302 | 3385 | else if ((sym.flags() & (PRIVATE | PROTECTED)) != 0) { |
jjg@612 | 3386 | return diags.create(dkind, log.currentSource(), |
mcimadamore@302 | 3387 | pos, "report.access", sym, |
mcimadamore@302 | 3388 | asFlagSet(sym.flags() & (PRIVATE | PROTECTED)), |
mcimadamore@302 | 3389 | sym.location()); |
mcimadamore@302 | 3390 | } |
mcimadamore@302 | 3391 | else { |
jjg@612 | 3392 | return diags.create(dkind, log.currentSource(), |
mcimadamore@302 | 3393 | pos, "not.def.public.cant.access", sym, sym.location()); |
duke@1 | 3394 | } |
duke@1 | 3395 | } |
duke@1 | 3396 | } |
duke@1 | 3397 | |
mcimadamore@302 | 3398 | /** |
mcimadamore@302 | 3399 | * InvalidSymbolError error class indicating that an instance member |
mcimadamore@302 | 3400 | * has erroneously been accessed from a static context. |
duke@1 | 3401 | */ |
mcimadamore@302 | 3402 | class StaticError extends InvalidSymbolError { |
mcimadamore@302 | 3403 | |
duke@1 | 3404 | StaticError(Symbol sym) { |
duke@1 | 3405 | super(STATICERR, sym, "static error"); |
duke@1 | 3406 | } |
duke@1 | 3407 | |
mcimadamore@302 | 3408 | @Override |
mcimadamore@302 | 3409 | JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind, |
mcimadamore@302 | 3410 | DiagnosticPosition pos, |
mcimadamore@829 | 3411 | Symbol location, |
mcimadamore@302 | 3412 | Type site, |
mcimadamore@302 | 3413 | Name name, |
mcimadamore@302 | 3414 | List<Type> argtypes, |
mcimadamore@302 | 3415 | List<Type> typeargtypes) { |
jjg@1374 | 3416 | Symbol errSym = ((sym.kind == TYP && sym.type.hasTag(CLASS)) |
mcimadamore@80 | 3417 | ? types.erasure(sym.type).tsym |
mcimadamore@80 | 3418 | : sym); |
jjg@612 | 3419 | return diags.create(dkind, log.currentSource(), pos, |
mcimadamore@302 | 3420 | "non-static.cant.be.ref", kindName(sym), errSym); |
duke@1 | 3421 | } |
duke@1 | 3422 | } |
duke@1 | 3423 | |
mcimadamore@302 | 3424 | /** |
mcimadamore@302 | 3425 | * InvalidSymbolError error class indicating that a pair of symbols |
mcimadamore@302 | 3426 | * (either methods, constructors or operands) are ambiguous |
mcimadamore@302 | 3427 | * given an actual arguments/type argument list. |
duke@1 | 3428 | */ |
mcimadamore@1480 | 3429 | class AmbiguityError extends ResolveError { |
mcimadamore@302 | 3430 | |
mcimadamore@302 | 3431 | /** The other maximally specific symbol */ |
mcimadamore@1480 | 3432 | List<Symbol> ambiguousSyms = List.nil(); |
mcimadamore@1480 | 3433 | |
mcimadamore@1480 | 3434 | @Override |
mcimadamore@1480 | 3435 | public boolean exists() { |
mcimadamore@1480 | 3436 | return true; |
mcimadamore@1480 | 3437 | } |
duke@1 | 3438 | |
duke@1 | 3439 | AmbiguityError(Symbol sym1, Symbol sym2) { |
mcimadamore@1480 | 3440 | super(AMBIGUOUS, "ambiguity error"); |
mcimadamore@1480 | 3441 | ambiguousSyms = flatten(sym2).appendList(flatten(sym1)); |
mcimadamore@1480 | 3442 | } |
mcimadamore@1480 | 3443 | |
mcimadamore@1480 | 3444 | private List<Symbol> flatten(Symbol sym) { |
mcimadamore@1480 | 3445 | if (sym.kind == AMBIGUOUS) { |
mcimadamore@1480 | 3446 | return ((AmbiguityError)sym).ambiguousSyms; |
mcimadamore@1480 | 3447 | } else { |
mcimadamore@1480 | 3448 | return List.of(sym); |
mcimadamore@1480 | 3449 | } |
mcimadamore@1480 | 3450 | } |
mcimadamore@1480 | 3451 | |
mcimadamore@1480 | 3452 | AmbiguityError addAmbiguousSymbol(Symbol s) { |
mcimadamore@1480 | 3453 | ambiguousSyms = ambiguousSyms.prepend(s); |
mcimadamore@1480 | 3454 | return this; |
duke@1 | 3455 | } |
duke@1 | 3456 | |
mcimadamore@302 | 3457 | @Override |
mcimadamore@302 | 3458 | JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind, |
mcimadamore@302 | 3459 | DiagnosticPosition pos, |
mcimadamore@829 | 3460 | Symbol location, |
mcimadamore@302 | 3461 | Type site, |
mcimadamore@302 | 3462 | Name name, |
mcimadamore@302 | 3463 | List<Type> argtypes, |
mcimadamore@302 | 3464 | List<Type> typeargtypes) { |
mcimadamore@1480 | 3465 | List<Symbol> diagSyms = ambiguousSyms.reverse(); |
mcimadamore@1480 | 3466 | Symbol s1 = diagSyms.head; |
mcimadamore@1480 | 3467 | Symbol s2 = diagSyms.tail.head; |
mcimadamore@1480 | 3468 | Name sname = s1.name; |
mcimadamore@1480 | 3469 | if (sname == names.init) sname = s1.owner.name; |
jjg@612 | 3470 | return diags.create(dkind, log.currentSource(), |
mcimadamore@302 | 3471 | pos, "ref.ambiguous", sname, |
mcimadamore@1480 | 3472 | kindName(s1), |
mcimadamore@1480 | 3473 | s1, |
mcimadamore@1480 | 3474 | s1.location(site, types), |
mcimadamore@1480 | 3475 | kindName(s2), |
mcimadamore@1480 | 3476 | s2, |
mcimadamore@1480 | 3477 | s2.location(site, types)); |
mcimadamore@1480 | 3478 | } |
mcimadamore@1480 | 3479 | |
mcimadamore@1480 | 3480 | /** |
mcimadamore@1480 | 3481 | * If multiple applicable methods are found during overload and none of them |
mcimadamore@1480 | 3482 | * is more specific than the others, attempt to merge their signatures. |
mcimadamore@1480 | 3483 | */ |
mcimadamore@1480 | 3484 | Symbol mergeAbstracts(Type site) { |
mcimadamore@1480 | 3485 | Symbol fst = ambiguousSyms.last(); |
mcimadamore@1480 | 3486 | Symbol res = fst; |
mcimadamore@1480 | 3487 | for (Symbol s : ambiguousSyms.reverse()) { |
mcimadamore@1480 | 3488 | Type mt1 = types.memberType(site, res); |
mcimadamore@1480 | 3489 | Type mt2 = types.memberType(site, s); |
mcimadamore@1480 | 3490 | if ((s.flags() & ABSTRACT) == 0 || |
mcimadamore@1480 | 3491 | !types.overrideEquivalent(mt1, mt2) || |
mcimadamore@1480 | 3492 | !types.isSameTypes(fst.erasure(types).getParameterTypes(), |
mcimadamore@1480 | 3493 | s.erasure(types).getParameterTypes())) { |
mcimadamore@1480 | 3494 | //ambiguity cannot be resolved |
mcimadamore@1480 | 3495 | return this; |
mcimadamore@1480 | 3496 | } else { |
mcimadamore@1480 | 3497 | Type mst = mostSpecificReturnType(mt1, mt2); |
mcimadamore@1480 | 3498 | if (mst == null) { |
mcimadamore@1480 | 3499 | // Theoretically, this can't happen, but it is possible |
mcimadamore@1480 | 3500 | // due to error recovery or mixing incompatible class files |
mcimadamore@1480 | 3501 | return this; |
mcimadamore@1480 | 3502 | } |
mcimadamore@1480 | 3503 | Symbol mostSpecific = mst == mt1 ? res : s; |
mcimadamore@1480 | 3504 | List<Type> allThrown = chk.intersect(mt1.getThrownTypes(), mt2.getThrownTypes()); |
mcimadamore@1480 | 3505 | Type newSig = types.createMethodTypeWithThrown(mostSpecific.type, allThrown); |
mcimadamore@1480 | 3506 | res = new MethodSymbol( |
mcimadamore@1480 | 3507 | mostSpecific.flags(), |
mcimadamore@1480 | 3508 | mostSpecific.name, |
mcimadamore@1480 | 3509 | newSig, |
mcimadamore@1480 | 3510 | mostSpecific.owner); |
mcimadamore@1480 | 3511 | } |
mcimadamore@1480 | 3512 | } |
mcimadamore@1480 | 3513 | return res; |
mcimadamore@1480 | 3514 | } |
mcimadamore@1480 | 3515 | |
mcimadamore@1480 | 3516 | @Override |
mcimadamore@1480 | 3517 | protected Symbol access(Name name, TypeSymbol location) { |
mcimadamore@1498 | 3518 | Symbol firstAmbiguity = ambiguousSyms.last(); |
mcimadamore@1498 | 3519 | return firstAmbiguity.kind == TYP ? |
mcimadamore@1498 | 3520 | types.createErrorType(name, location, firstAmbiguity.type).tsym : |
mcimadamore@1498 | 3521 | firstAmbiguity; |
duke@1 | 3522 | } |
duke@1 | 3523 | } |
mcimadamore@160 | 3524 | |
mcimadamore@160 | 3525 | enum MethodResolutionPhase { |
mcimadamore@160 | 3526 | BASIC(false, false), |
mcimadamore@160 | 3527 | BOX(true, false), |
mcimadamore@1394 | 3528 | VARARITY(true, true) { |
mcimadamore@1394 | 3529 | @Override |
mcimadamore@1394 | 3530 | public Symbol mergeResults(Symbol bestSoFar, Symbol sym) { |
mcimadamore@1394 | 3531 | switch (sym.kind) { |
mcimadamore@1394 | 3532 | case WRONG_MTH: |
mcimadamore@1394 | 3533 | return (bestSoFar.kind == WRONG_MTH || bestSoFar.kind == WRONG_MTHS) ? |
mcimadamore@1394 | 3534 | bestSoFar : |
mcimadamore@1394 | 3535 | sym; |
mcimadamore@1394 | 3536 | case ABSENT_MTH: |
mcimadamore@1394 | 3537 | return bestSoFar; |
mcimadamore@1394 | 3538 | default: |
mcimadamore@1394 | 3539 | return sym; |
mcimadamore@1394 | 3540 | } |
mcimadamore@1394 | 3541 | } |
mcimadamore@1394 | 3542 | }; |
mcimadamore@160 | 3543 | |
vromero@1442 | 3544 | final boolean isBoxingRequired; |
vromero@1442 | 3545 | final boolean isVarargsRequired; |
mcimadamore@160 | 3546 | |
mcimadamore@160 | 3547 | MethodResolutionPhase(boolean isBoxingRequired, boolean isVarargsRequired) { |
mcimadamore@160 | 3548 | this.isBoxingRequired = isBoxingRequired; |
mcimadamore@160 | 3549 | this.isVarargsRequired = isVarargsRequired; |
mcimadamore@160 | 3550 | } |
mcimadamore@160 | 3551 | |
mcimadamore@160 | 3552 | public boolean isBoxingRequired() { |
mcimadamore@160 | 3553 | return isBoxingRequired; |
mcimadamore@160 | 3554 | } |
mcimadamore@160 | 3555 | |
mcimadamore@160 | 3556 | public boolean isVarargsRequired() { |
mcimadamore@160 | 3557 | return isVarargsRequired; |
mcimadamore@160 | 3558 | } |
mcimadamore@160 | 3559 | |
mcimadamore@160 | 3560 | public boolean isApplicable(boolean boxingEnabled, boolean varargsEnabled) { |
mcimadamore@160 | 3561 | return (varargsEnabled || !isVarargsRequired) && |
mcimadamore@160 | 3562 | (boxingEnabled || !isBoxingRequired); |
mcimadamore@160 | 3563 | } |
mcimadamore@1394 | 3564 | |
mcimadamore@1394 | 3565 | public Symbol mergeResults(Symbol prev, Symbol sym) { |
mcimadamore@1394 | 3566 | return sym; |
mcimadamore@1394 | 3567 | } |
mcimadamore@160 | 3568 | } |
mcimadamore@160 | 3569 | |
mcimadamore@160 | 3570 | final List<MethodResolutionPhase> methodResolutionSteps = List.of(BASIC, BOX, VARARITY); |
mcimadamore@160 | 3571 | |
mcimadamore@1215 | 3572 | /** |
mcimadamore@1215 | 3573 | * A resolution context is used to keep track of intermediate results of |
mcimadamore@1215 | 3574 | * overload resolution, such as list of method that are not applicable |
mcimadamore@1215 | 3575 | * (used to generate more precise diagnostics) and so on. Resolution contexts |
mcimadamore@1215 | 3576 | * can be nested - this means that when each overload resolution routine should |
mcimadamore@1215 | 3577 | * work within the resolution context it created. |
mcimadamore@1215 | 3578 | */ |
mcimadamore@1215 | 3579 | class MethodResolutionContext { |
mcimadamore@689 | 3580 | |
mcimadamore@1215 | 3581 | private List<Candidate> candidates = List.nil(); |
mcimadamore@1114 | 3582 | |
mcimadamore@1347 | 3583 | MethodResolutionPhase step = null; |
mcimadamore@1215 | 3584 | |
mcimadamore@1215 | 3585 | private boolean internalResolution = false; |
mcimadamore@1347 | 3586 | private DeferredAttr.AttrMode attrMode = DeferredAttr.AttrMode.SPECULATIVE; |
mcimadamore@1215 | 3587 | |
mcimadamore@1215 | 3588 | void addInapplicableCandidate(Symbol sym, JCDiagnostic details) { |
mcimadamore@1215 | 3589 | Candidate c = new Candidate(currentResolutionContext.step, sym, details, null); |
mcimadamore@1352 | 3590 | candidates = candidates.append(c); |
mcimadamore@1215 | 3591 | } |
mcimadamore@1215 | 3592 | |
mcimadamore@1215 | 3593 | void addApplicableCandidate(Symbol sym, Type mtype) { |
mcimadamore@1215 | 3594 | Candidate c = new Candidate(currentResolutionContext.step, sym, null, mtype); |
mcimadamore@1215 | 3595 | candidates = candidates.append(c); |
mcimadamore@1215 | 3596 | } |
mcimadamore@1215 | 3597 | |
mcimadamore@1551 | 3598 | DeferredAttrContext deferredAttrContext(Symbol sym, InferenceContext inferenceContext, ResultInfo pendingResult, Warner warn) { |
mcimadamore@1551 | 3599 | return deferredAttr.new DeferredAttrContext(attrMode, sym, step, inferenceContext, pendingResult != null ? pendingResult.checkContext.deferredAttrContext() : deferredAttr.emptyDeferredAttrContext, warn); |
mcimadamore@1479 | 3600 | } |
mcimadamore@1479 | 3601 | |
mcimadamore@1215 | 3602 | /** |
mcimadamore@1215 | 3603 | * This class represents an overload resolution candidate. There are two |
mcimadamore@1215 | 3604 | * kinds of candidates: applicable methods and inapplicable methods; |
mcimadamore@1215 | 3605 | * applicable methods have a pointer to the instantiated method type, |
mcimadamore@1215 | 3606 | * while inapplicable candidates contain further details about the |
mcimadamore@1215 | 3607 | * reason why the method has been considered inapplicable. |
mcimadamore@1215 | 3608 | */ |
vromero@1588 | 3609 | @SuppressWarnings("overrides") |
mcimadamore@1215 | 3610 | class Candidate { |
mcimadamore@1215 | 3611 | |
mcimadamore@1215 | 3612 | final MethodResolutionPhase step; |
mcimadamore@1215 | 3613 | final Symbol sym; |
mcimadamore@1215 | 3614 | final JCDiagnostic details; |
mcimadamore@1215 | 3615 | final Type mtype; |
mcimadamore@1215 | 3616 | |
mcimadamore@1215 | 3617 | private Candidate(MethodResolutionPhase step, Symbol sym, JCDiagnostic details, Type mtype) { |
mcimadamore@1215 | 3618 | this.step = step; |
mcimadamore@1215 | 3619 | this.sym = sym; |
mcimadamore@1215 | 3620 | this.details = details; |
mcimadamore@1215 | 3621 | this.mtype = mtype; |
mcimadamore@1215 | 3622 | } |
mcimadamore@1215 | 3623 | |
mcimadamore@1215 | 3624 | @Override |
mcimadamore@1215 | 3625 | public boolean equals(Object o) { |
mcimadamore@1215 | 3626 | if (o instanceof Candidate) { |
mcimadamore@1215 | 3627 | Symbol s1 = this.sym; |
mcimadamore@1215 | 3628 | Symbol s2 = ((Candidate)o).sym; |
mcimadamore@1215 | 3629 | if ((s1 != s2 && |
mcimadamore@1352 | 3630 | (s1.overrides(s2, s1.owner.type.tsym, types, false) || |
mcimadamore@1352 | 3631 | (s2.overrides(s1, s2.owner.type.tsym, types, false)))) || |
mcimadamore@1352 | 3632 | ((s1.isConstructor() || s2.isConstructor()) && s1.owner != s2.owner)) |
mcimadamore@1215 | 3633 | return true; |
mcimadamore@1215 | 3634 | } |
mcimadamore@1215 | 3635 | return false; |
mcimadamore@1215 | 3636 | } |
mcimadamore@1215 | 3637 | |
mcimadamore@1215 | 3638 | boolean isApplicable() { |
mcimadamore@1215 | 3639 | return mtype != null; |
mcimadamore@1215 | 3640 | } |
mcimadamore@1215 | 3641 | } |
mcimadamore@1347 | 3642 | |
mcimadamore@1347 | 3643 | DeferredAttr.AttrMode attrMode() { |
mcimadamore@1347 | 3644 | return attrMode; |
mcimadamore@1347 | 3645 | } |
mcimadamore@1347 | 3646 | |
mcimadamore@1347 | 3647 | boolean internal() { |
mcimadamore@1347 | 3648 | return internalResolution; |
mcimadamore@1347 | 3649 | } |
mcimadamore@160 | 3650 | } |
mcimadamore@1215 | 3651 | |
mcimadamore@1215 | 3652 | MethodResolutionContext currentResolutionContext = null; |
duke@1 | 3653 | } |