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