Fri, 12 Jul 2013 13:11:12 -0700
8020278: NPE in javadoc
Reviewed-by: mcimadamore, vromero
mcimadamore@1347 | 1 | /* |
mcimadamore@1674 | 2 | * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
mcimadamore@1347 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
mcimadamore@1347 | 4 | * |
mcimadamore@1347 | 5 | * This code is free software; you can redistribute it and/or modify it |
mcimadamore@1347 | 6 | * under the terms of the GNU General Public License version 2 only, as |
mcimadamore@1347 | 7 | * published by the Free Software Foundation. Oracle designates this |
mcimadamore@1347 | 8 | * particular file as subject to the "Classpath" exception as provided |
mcimadamore@1347 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
mcimadamore@1347 | 10 | * |
mcimadamore@1347 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
mcimadamore@1347 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
mcimadamore@1347 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
mcimadamore@1347 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
mcimadamore@1347 | 15 | * accompanied this code). |
mcimadamore@1347 | 16 | * |
mcimadamore@1347 | 17 | * You should have received a copy of the GNU General Public License version |
mcimadamore@1347 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
mcimadamore@1347 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
mcimadamore@1347 | 20 | * |
mcimadamore@1347 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
mcimadamore@1347 | 22 | * or visit www.oracle.com if you need additional information or have any |
mcimadamore@1347 | 23 | * questions. |
mcimadamore@1347 | 24 | */ |
mcimadamore@1347 | 25 | |
mcimadamore@1347 | 26 | package com.sun.tools.javac.comp; |
mcimadamore@1347 | 27 | |
mcimadamore@1347 | 28 | import com.sun.tools.javac.code.*; |
mcimadamore@1347 | 29 | import com.sun.tools.javac.tree.*; |
mcimadamore@1347 | 30 | import com.sun.tools.javac.util.*; |
mcimadamore@1674 | 31 | import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
mcimadamore@1347 | 32 | import com.sun.tools.javac.code.Symbol.*; |
mcimadamore@1347 | 33 | import com.sun.tools.javac.code.Type.*; |
mcimadamore@1347 | 34 | import com.sun.tools.javac.comp.Attr.ResultInfo; |
mcimadamore@1347 | 35 | import com.sun.tools.javac.comp.Infer.InferenceContext; |
mcimadamore@1347 | 36 | import com.sun.tools.javac.comp.Resolve.MethodResolutionPhase; |
mcimadamore@1347 | 37 | import com.sun.tools.javac.tree.JCTree.*; |
mcimadamore@1347 | 38 | |
mcimadamore@1347 | 39 | import javax.tools.JavaFileObject; |
mcimadamore@1347 | 40 | |
mcimadamore@1347 | 41 | import java.util.ArrayList; |
mcimadamore@1481 | 42 | import java.util.EnumSet; |
mcimadamore@1415 | 43 | import java.util.LinkedHashSet; |
mcimadamore@1347 | 44 | import java.util.Map; |
mcimadamore@1347 | 45 | import java.util.Queue; |
mcimadamore@1347 | 46 | import java.util.Set; |
mcimadamore@1347 | 47 | import java.util.WeakHashMap; |
mcimadamore@1347 | 48 | |
mcimadamore@1415 | 49 | import static com.sun.tools.javac.code.TypeTag.*; |
mcimadamore@1347 | 50 | import static com.sun.tools.javac.tree.JCTree.Tag.*; |
mcimadamore@1347 | 51 | |
mcimadamore@1347 | 52 | /** |
mcimadamore@1347 | 53 | * This is an helper class that is used to perform deferred type-analysis. |
mcimadamore@1347 | 54 | * Each time a poly expression occurs in argument position, javac attributes it |
mcimadamore@1347 | 55 | * with a temporary 'deferred type' that is checked (possibly multiple times) |
mcimadamore@1347 | 56 | * against an expected formal type. |
mcimadamore@1347 | 57 | * |
mcimadamore@1347 | 58 | * <p><b>This is NOT part of any supported API. |
mcimadamore@1347 | 59 | * If you write code that depends on this, you do so at your own risk. |
mcimadamore@1347 | 60 | * This code and its internal interfaces are subject to change or |
mcimadamore@1347 | 61 | * deletion without notice.</b> |
mcimadamore@1347 | 62 | */ |
mcimadamore@1347 | 63 | public class DeferredAttr extends JCTree.Visitor { |
mcimadamore@1347 | 64 | protected static final Context.Key<DeferredAttr> deferredAttrKey = |
mcimadamore@1347 | 65 | new Context.Key<DeferredAttr>(); |
mcimadamore@1347 | 66 | |
mcimadamore@1347 | 67 | final Attr attr; |
mcimadamore@1347 | 68 | final Check chk; |
mcimadamore@1510 | 69 | final JCDiagnostic.Factory diags; |
mcimadamore@1347 | 70 | final Enter enter; |
mcimadamore@1347 | 71 | final Infer infer; |
mcimadamore@1581 | 72 | final Resolve rs; |
mcimadamore@1347 | 73 | final Log log; |
mcimadamore@1347 | 74 | final Symtab syms; |
mcimadamore@1347 | 75 | final TreeMaker make; |
mcimadamore@1347 | 76 | final Types types; |
mcimadamore@1347 | 77 | |
mcimadamore@1347 | 78 | public static DeferredAttr instance(Context context) { |
mcimadamore@1347 | 79 | DeferredAttr instance = context.get(deferredAttrKey); |
mcimadamore@1347 | 80 | if (instance == null) |
mcimadamore@1347 | 81 | instance = new DeferredAttr(context); |
mcimadamore@1347 | 82 | return instance; |
mcimadamore@1347 | 83 | } |
mcimadamore@1347 | 84 | |
mcimadamore@1347 | 85 | protected DeferredAttr(Context context) { |
mcimadamore@1347 | 86 | context.put(deferredAttrKey, this); |
mcimadamore@1347 | 87 | attr = Attr.instance(context); |
mcimadamore@1347 | 88 | chk = Check.instance(context); |
mcimadamore@1510 | 89 | diags = JCDiagnostic.Factory.instance(context); |
mcimadamore@1347 | 90 | enter = Enter.instance(context); |
mcimadamore@1347 | 91 | infer = Infer.instance(context); |
mcimadamore@1581 | 92 | rs = Resolve.instance(context); |
mcimadamore@1347 | 93 | log = Log.instance(context); |
mcimadamore@1347 | 94 | syms = Symtab.instance(context); |
mcimadamore@1347 | 95 | make = TreeMaker.instance(context); |
mcimadamore@1347 | 96 | types = Types.instance(context); |
mcimadamore@1510 | 97 | Names names = Names.instance(context); |
mcimadamore@1889 | 98 | stuckTree = make.Ident(names.empty).setType(Type.stuckType); |
mcimadamore@1347 | 99 | } |
mcimadamore@1347 | 100 | |
mcimadamore@1510 | 101 | /** shared tree for stuck expressions */ |
mcimadamore@1510 | 102 | final JCTree stuckTree; |
mcimadamore@1510 | 103 | |
mcimadamore@1347 | 104 | /** |
mcimadamore@1347 | 105 | * This type represents a deferred type. A deferred type starts off with |
mcimadamore@1347 | 106 | * no information on the underlying expression type. Such info needs to be |
mcimadamore@1347 | 107 | * discovered through type-checking the deferred type against a target-type. |
mcimadamore@1347 | 108 | * Every deferred type keeps a pointer to the AST node from which it originated. |
mcimadamore@1347 | 109 | */ |
mcimadamore@1347 | 110 | public class DeferredType extends Type { |
mcimadamore@1347 | 111 | |
mcimadamore@1347 | 112 | public JCExpression tree; |
mcimadamore@1347 | 113 | Env<AttrContext> env; |
mcimadamore@1347 | 114 | AttrMode mode; |
mcimadamore@1347 | 115 | SpeculativeCache speculativeCache; |
mcimadamore@1347 | 116 | |
mcimadamore@1347 | 117 | DeferredType(JCExpression tree, Env<AttrContext> env) { |
vromero@1853 | 118 | super(null); |
mcimadamore@1347 | 119 | this.tree = tree; |
mcimadamore@1347 | 120 | this.env = env.dup(tree, env.info.dup()); |
mcimadamore@1347 | 121 | this.speculativeCache = new SpeculativeCache(); |
mcimadamore@1347 | 122 | } |
mcimadamore@1347 | 123 | |
vromero@1853 | 124 | @Override |
vromero@1853 | 125 | public TypeTag getTag() { |
vromero@1853 | 126 | return DEFERRED; |
vromero@1853 | 127 | } |
vromero@1853 | 128 | |
mcimadamore@1347 | 129 | /** |
mcimadamore@1347 | 130 | * A speculative cache is used to keep track of all overload resolution rounds |
mcimadamore@1347 | 131 | * that triggered speculative attribution on a given deferred type. Each entry |
mcimadamore@1347 | 132 | * stores a pointer to the speculative tree and the resolution phase in which the entry |
mcimadamore@1347 | 133 | * has been added. |
mcimadamore@1347 | 134 | */ |
mcimadamore@1347 | 135 | class SpeculativeCache { |
mcimadamore@1347 | 136 | |
mcimadamore@1347 | 137 | private Map<Symbol, List<Entry>> cache = |
mcimadamore@1347 | 138 | new WeakHashMap<Symbol, List<Entry>>(); |
mcimadamore@1347 | 139 | |
mcimadamore@1347 | 140 | class Entry { |
mcimadamore@1347 | 141 | JCTree speculativeTree; |
mcimadamore@1347 | 142 | Resolve.MethodResolutionPhase phase; |
mcimadamore@1347 | 143 | |
mcimadamore@1347 | 144 | public Entry(JCTree speculativeTree, MethodResolutionPhase phase) { |
mcimadamore@1347 | 145 | this.speculativeTree = speculativeTree; |
mcimadamore@1347 | 146 | this.phase = phase; |
mcimadamore@1347 | 147 | } |
mcimadamore@1347 | 148 | |
mcimadamore@1347 | 149 | boolean matches(Resolve.MethodResolutionPhase phase) { |
mcimadamore@1347 | 150 | return this.phase == phase; |
mcimadamore@1347 | 151 | } |
mcimadamore@1347 | 152 | } |
mcimadamore@1347 | 153 | |
mcimadamore@1347 | 154 | /** |
mcimadamore@1347 | 155 | * Retrieve a speculative cache entry corresponding to given symbol |
mcimadamore@1347 | 156 | * and resolution phase |
mcimadamore@1347 | 157 | */ |
mcimadamore@1347 | 158 | Entry get(Symbol msym, MethodResolutionPhase phase) { |
mcimadamore@1347 | 159 | List<Entry> entries = cache.get(msym); |
mcimadamore@1347 | 160 | if (entries == null) return null; |
mcimadamore@1347 | 161 | for (Entry e : entries) { |
mcimadamore@1347 | 162 | if (e.matches(phase)) return e; |
mcimadamore@1347 | 163 | } |
mcimadamore@1347 | 164 | return null; |
mcimadamore@1347 | 165 | } |
mcimadamore@1347 | 166 | |
mcimadamore@1347 | 167 | /** |
mcimadamore@1347 | 168 | * Stores a speculative cache entry corresponding to given symbol |
mcimadamore@1347 | 169 | * and resolution phase |
mcimadamore@1347 | 170 | */ |
mcimadamore@1347 | 171 | void put(Symbol msym, JCTree speculativeTree, MethodResolutionPhase phase) { |
mcimadamore@1347 | 172 | List<Entry> entries = cache.get(msym); |
mcimadamore@1347 | 173 | if (entries == null) { |
mcimadamore@1347 | 174 | entries = List.nil(); |
mcimadamore@1347 | 175 | } |
mcimadamore@1347 | 176 | cache.put(msym, entries.prepend(new Entry(speculativeTree, phase))); |
mcimadamore@1347 | 177 | } |
mcimadamore@1347 | 178 | } |
mcimadamore@1347 | 179 | |
mcimadamore@1347 | 180 | /** |
mcimadamore@1347 | 181 | * Get the type that has been computed during a speculative attribution round |
mcimadamore@1347 | 182 | */ |
mcimadamore@1347 | 183 | Type speculativeType(Symbol msym, MethodResolutionPhase phase) { |
mcimadamore@1347 | 184 | SpeculativeCache.Entry e = speculativeCache.get(msym, phase); |
mcimadamore@1347 | 185 | return e != null ? e.speculativeTree.type : Type.noType; |
mcimadamore@1347 | 186 | } |
mcimadamore@1347 | 187 | |
mcimadamore@1347 | 188 | /** |
mcimadamore@1347 | 189 | * Check a deferred type against a potential target-type. Depending on |
mcimadamore@1347 | 190 | * the current attribution mode, a normal vs. speculative attribution |
mcimadamore@1347 | 191 | * round is performed on the underlying AST node. There can be only one |
mcimadamore@1347 | 192 | * speculative round for a given target method symbol; moreover, a normal |
mcimadamore@1347 | 193 | * attribution round must follow one or more speculative rounds. |
mcimadamore@1347 | 194 | */ |
mcimadamore@1347 | 195 | Type check(ResultInfo resultInfo) { |
mcimadamore@1481 | 196 | return check(resultInfo, stuckVars(tree, env, resultInfo), basicCompleter); |
mcimadamore@1481 | 197 | } |
mcimadamore@1481 | 198 | |
mcimadamore@1481 | 199 | Type check(ResultInfo resultInfo, List<Type> stuckVars, DeferredTypeCompleter deferredTypeCompleter) { |
mcimadamore@1347 | 200 | DeferredAttrContext deferredAttrContext = |
mcimadamore@1347 | 201 | resultInfo.checkContext.deferredAttrContext(); |
mcimadamore@1347 | 202 | Assert.check(deferredAttrContext != emptyDeferredAttrContext); |
mcimadamore@1347 | 203 | if (stuckVars.nonEmpty()) { |
mcimadamore@1347 | 204 | deferredAttrContext.addDeferredAttrNode(this, resultInfo, stuckVars); |
mcimadamore@1347 | 205 | return Type.noType; |
mcimadamore@1347 | 206 | } else { |
mcimadamore@1347 | 207 | try { |
mcimadamore@1481 | 208 | return deferredTypeCompleter.complete(this, resultInfo, deferredAttrContext); |
mcimadamore@1347 | 209 | } finally { |
mcimadamore@1347 | 210 | mode = deferredAttrContext.mode; |
mcimadamore@1347 | 211 | } |
mcimadamore@1347 | 212 | } |
mcimadamore@1347 | 213 | } |
mcimadamore@1347 | 214 | } |
mcimadamore@1347 | 215 | |
mcimadamore@1347 | 216 | /** |
mcimadamore@1481 | 217 | * A completer for deferred types. Defines an entry point for type-checking |
mcimadamore@1481 | 218 | * a deferred type. |
mcimadamore@1481 | 219 | */ |
mcimadamore@1481 | 220 | interface DeferredTypeCompleter { |
mcimadamore@1481 | 221 | /** |
mcimadamore@1481 | 222 | * Entry point for type-checking a deferred type. Depending on the |
mcimadamore@1481 | 223 | * circumstances, type-checking could amount to full attribution |
mcimadamore@1481 | 224 | * or partial structural check (aka potential applicability). |
mcimadamore@1481 | 225 | */ |
mcimadamore@1481 | 226 | Type complete(DeferredType dt, ResultInfo resultInfo, DeferredAttrContext deferredAttrContext); |
mcimadamore@1481 | 227 | } |
mcimadamore@1481 | 228 | |
mcimadamore@1481 | 229 | /** |
mcimadamore@1481 | 230 | * A basic completer for deferred types. This completer type-checks a deferred type |
mcimadamore@1481 | 231 | * using attribution; depending on the attribution mode, this could be either standard |
mcimadamore@1481 | 232 | * or speculative attribution. |
mcimadamore@1481 | 233 | */ |
mcimadamore@1481 | 234 | DeferredTypeCompleter basicCompleter = new DeferredTypeCompleter() { |
mcimadamore@1481 | 235 | public Type complete(DeferredType dt, ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) { |
mcimadamore@1481 | 236 | switch (deferredAttrContext.mode) { |
mcimadamore@1481 | 237 | case SPECULATIVE: |
mcimadamore@1654 | 238 | //Note: if a symbol is imported twice we might do two identical |
mcimadamore@1654 | 239 | //speculative rounds... |
mcimadamore@1654 | 240 | Assert.check(dt.mode == null || dt.mode == AttrMode.SPECULATIVE); |
mcimadamore@1481 | 241 | JCTree speculativeTree = attribSpeculative(dt.tree, dt.env, resultInfo); |
mcimadamore@1481 | 242 | dt.speculativeCache.put(deferredAttrContext.msym, speculativeTree, deferredAttrContext.phase); |
mcimadamore@1481 | 243 | return speculativeTree.type; |
mcimadamore@1481 | 244 | case CHECK: |
mcimadamore@1562 | 245 | Assert.check(dt.mode != null); |
mcimadamore@1481 | 246 | return attr.attribTree(dt.tree, dt.env, resultInfo); |
mcimadamore@1481 | 247 | } |
mcimadamore@1481 | 248 | Assert.error(); |
mcimadamore@1481 | 249 | return null; |
mcimadamore@1481 | 250 | } |
mcimadamore@1481 | 251 | }; |
mcimadamore@1481 | 252 | |
mcimadamore@1562 | 253 | DeferredTypeCompleter dummyCompleter = new DeferredTypeCompleter() { |
mcimadamore@1562 | 254 | public Type complete(DeferredType dt, ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) { |
mcimadamore@1562 | 255 | Assert.check(deferredAttrContext.mode == AttrMode.CHECK); |
mcimadamore@1562 | 256 | return dt.tree.type = Type.noType; |
mcimadamore@1562 | 257 | } |
mcimadamore@1562 | 258 | }; |
mcimadamore@1562 | 259 | |
mcimadamore@1481 | 260 | /** |
mcimadamore@1347 | 261 | * The 'mode' in which the deferred type is to be type-checked |
mcimadamore@1347 | 262 | */ |
mcimadamore@1347 | 263 | public enum AttrMode { |
mcimadamore@1347 | 264 | /** |
mcimadamore@1347 | 265 | * A speculative type-checking round is used during overload resolution |
mcimadamore@1347 | 266 | * mainly to generate constraints on inference variables. Side-effects |
mcimadamore@1347 | 267 | * arising from type-checking the expression associated with the deferred |
mcimadamore@1347 | 268 | * type are reversed after the speculative round finishes. This means the |
mcimadamore@1347 | 269 | * expression tree will be left in a blank state. |
mcimadamore@1347 | 270 | */ |
mcimadamore@1347 | 271 | SPECULATIVE, |
mcimadamore@1347 | 272 | /** |
mcimadamore@1347 | 273 | * This is the plain type-checking mode. Produces side-effects on the underlying AST node |
mcimadamore@1347 | 274 | */ |
mcimadamore@1347 | 275 | CHECK; |
mcimadamore@1347 | 276 | } |
mcimadamore@1347 | 277 | |
mcimadamore@1347 | 278 | /** |
mcimadamore@1347 | 279 | * Routine that performs speculative type-checking; the input AST node is |
mcimadamore@1347 | 280 | * cloned (to avoid side-effects cause by Attr) and compiler state is |
mcimadamore@1347 | 281 | * restored after type-checking. All diagnostics (but critical ones) are |
mcimadamore@1347 | 282 | * disabled during speculative type-checking. |
mcimadamore@1347 | 283 | */ |
mcimadamore@1347 | 284 | JCTree attribSpeculative(JCTree tree, Env<AttrContext> env, ResultInfo resultInfo) { |
mcimadamore@1596 | 285 | final JCTree newTree = new TreeCopier<Object>(make).copy(tree); |
mcimadamore@1347 | 286 | Env<AttrContext> speculativeEnv = env.dup(newTree, env.info.dup(env.info.scope.dupUnshared())); |
mcimadamore@1347 | 287 | speculativeEnv.info.scope.owner = env.info.scope.owner; |
jjg@1406 | 288 | Log.DeferredDiagnosticHandler deferredDiagnosticHandler = |
jjg@1406 | 289 | new Log.DeferredDiagnosticHandler(log, new Filter<JCDiagnostic>() { |
mcimadamore@1596 | 290 | public boolean accepts(final JCDiagnostic d) { |
mcimadamore@1596 | 291 | class PosScanner extends TreeScanner { |
mcimadamore@1596 | 292 | boolean found = false; |
mcimadamore@1596 | 293 | |
mcimadamore@1596 | 294 | @Override |
mcimadamore@1596 | 295 | public void scan(JCTree tree) { |
mcimadamore@1596 | 296 | if (tree != null && |
mcimadamore@1596 | 297 | tree.pos() == d.getDiagnosticPosition()) { |
mcimadamore@1596 | 298 | found = true; |
mcimadamore@1596 | 299 | } |
mcimadamore@1596 | 300 | super.scan(tree); |
mcimadamore@1596 | 301 | } |
mcimadamore@1596 | 302 | }; |
mcimadamore@1596 | 303 | PosScanner posScanner = new PosScanner(); |
mcimadamore@1596 | 304 | posScanner.scan(newTree); |
mcimadamore@1596 | 305 | return posScanner.found; |
jjg@1406 | 306 | } |
jjg@1406 | 307 | }); |
mcimadamore@1347 | 308 | try { |
mcimadamore@1347 | 309 | attr.attribTree(newTree, speculativeEnv, resultInfo); |
mcimadamore@1347 | 310 | unenterScanner.scan(newTree); |
mcimadamore@1347 | 311 | return newTree; |
mcimadamore@1347 | 312 | } finally { |
mcimadamore@1347 | 313 | unenterScanner.scan(newTree); |
jjg@1406 | 314 | log.popDiagnosticHandler(deferredDiagnosticHandler); |
mcimadamore@1347 | 315 | } |
mcimadamore@1347 | 316 | } |
mcimadamore@1347 | 317 | //where |
mcimadamore@1347 | 318 | protected TreeScanner unenterScanner = new TreeScanner() { |
mcimadamore@1347 | 319 | @Override |
mcimadamore@1347 | 320 | public void visitClassDef(JCClassDecl tree) { |
mcimadamore@1347 | 321 | ClassSymbol csym = tree.sym; |
mcimadamore@1415 | 322 | //if something went wrong during method applicability check |
mcimadamore@1415 | 323 | //it is possible that nested expressions inside argument expression |
mcimadamore@1415 | 324 | //are left unchecked - in such cases there's nothing to clean up. |
mcimadamore@1415 | 325 | if (csym == null) return; |
mcimadamore@1347 | 326 | enter.typeEnvs.remove(csym); |
mcimadamore@1347 | 327 | chk.compiled.remove(csym.flatname); |
mcimadamore@1347 | 328 | syms.classes.remove(csym.flatname); |
mcimadamore@1347 | 329 | super.visitClassDef(tree); |
mcimadamore@1347 | 330 | } |
mcimadamore@1347 | 331 | }; |
mcimadamore@1347 | 332 | |
mcimadamore@1347 | 333 | /** |
mcimadamore@1347 | 334 | * A deferred context is created on each method check. A deferred context is |
mcimadamore@1347 | 335 | * used to keep track of information associated with the method check, such as |
mcimadamore@1347 | 336 | * the symbol of the method being checked, the overload resolution phase, |
mcimadamore@1347 | 337 | * the kind of attribution mode to be applied to deferred types and so forth. |
mcimadamore@1347 | 338 | * As deferred types are processed (by the method check routine) stuck AST nodes |
mcimadamore@1347 | 339 | * are added (as new deferred attribution nodes) to this context. The complete() |
mcimadamore@1347 | 340 | * routine makes sure that all pending nodes are properly processed, by |
mcimadamore@1347 | 341 | * progressively instantiating all inference variables on which one or more |
mcimadamore@1347 | 342 | * deferred attribution node is stuck. |
mcimadamore@1347 | 343 | */ |
mcimadamore@1347 | 344 | class DeferredAttrContext { |
mcimadamore@1347 | 345 | |
mcimadamore@1347 | 346 | /** attribution mode */ |
mcimadamore@1347 | 347 | final AttrMode mode; |
mcimadamore@1347 | 348 | |
mcimadamore@1347 | 349 | /** symbol of the method being checked */ |
mcimadamore@1347 | 350 | final Symbol msym; |
mcimadamore@1347 | 351 | |
mcimadamore@1347 | 352 | /** method resolution step */ |
mcimadamore@1347 | 353 | final Resolve.MethodResolutionPhase phase; |
mcimadamore@1347 | 354 | |
mcimadamore@1347 | 355 | /** inference context */ |
mcimadamore@1347 | 356 | final InferenceContext inferenceContext; |
mcimadamore@1347 | 357 | |
mcimadamore@1551 | 358 | /** parent deferred context */ |
mcimadamore@1551 | 359 | final DeferredAttrContext parent; |
mcimadamore@1551 | 360 | |
mcimadamore@1551 | 361 | /** Warner object to report warnings */ |
mcimadamore@1551 | 362 | final Warner warn; |
mcimadamore@1551 | 363 | |
mcimadamore@1347 | 364 | /** list of deferred attribution nodes to be processed */ |
mcimadamore@1347 | 365 | ArrayList<DeferredAttrNode> deferredAttrNodes = new ArrayList<DeferredAttrNode>(); |
mcimadamore@1347 | 366 | |
mcimadamore@1551 | 367 | DeferredAttrContext(AttrMode mode, Symbol msym, MethodResolutionPhase phase, |
mcimadamore@1551 | 368 | InferenceContext inferenceContext, DeferredAttrContext parent, Warner warn) { |
mcimadamore@1347 | 369 | this.mode = mode; |
mcimadamore@1347 | 370 | this.msym = msym; |
mcimadamore@1347 | 371 | this.phase = phase; |
mcimadamore@1551 | 372 | this.parent = parent; |
mcimadamore@1551 | 373 | this.warn = warn; |
mcimadamore@1347 | 374 | this.inferenceContext = inferenceContext; |
mcimadamore@1347 | 375 | } |
mcimadamore@1347 | 376 | |
mcimadamore@1347 | 377 | /** |
mcimadamore@1347 | 378 | * Adds a node to the list of deferred attribution nodes - used by Resolve.rawCheckArgumentsApplicable |
mcimadamore@1347 | 379 | * Nodes added this way act as 'roots' for the out-of-order method checking process. |
mcimadamore@1347 | 380 | */ |
mcimadamore@1347 | 381 | void addDeferredAttrNode(final DeferredType dt, ResultInfo resultInfo, List<Type> stuckVars) { |
mcimadamore@1347 | 382 | deferredAttrNodes.add(new DeferredAttrNode(dt, resultInfo, stuckVars)); |
mcimadamore@1347 | 383 | } |
mcimadamore@1347 | 384 | |
mcimadamore@1347 | 385 | /** |
mcimadamore@1347 | 386 | * Incrementally process all nodes, by skipping 'stuck' nodes and attributing |
mcimadamore@1347 | 387 | * 'unstuck' ones. If at any point no progress can be made (no 'unstuck' nodes) |
mcimadamore@1347 | 388 | * some inference variable might get eagerly instantiated so that all nodes |
mcimadamore@1347 | 389 | * can be type-checked. |
mcimadamore@1347 | 390 | */ |
mcimadamore@1347 | 391 | void complete() { |
mcimadamore@1347 | 392 | while (!deferredAttrNodes.isEmpty()) { |
mcimadamore@1415 | 393 | Set<Type> stuckVars = new LinkedHashSet<Type>(); |
mcimadamore@1347 | 394 | boolean progress = false; |
mcimadamore@1347 | 395 | //scan a defensive copy of the node list - this is because a deferred |
mcimadamore@1347 | 396 | //attribution round can add new nodes to the list |
mcimadamore@1347 | 397 | for (DeferredAttrNode deferredAttrNode : List.from(deferredAttrNodes)) { |
mcimadamore@1551 | 398 | if (!deferredAttrNode.process(this)) { |
mcimadamore@1510 | 399 | stuckVars.addAll(deferredAttrNode.stuckVars); |
mcimadamore@1510 | 400 | } else { |
mcimadamore@1347 | 401 | deferredAttrNodes.remove(deferredAttrNode); |
mcimadamore@1347 | 402 | progress = true; |
mcimadamore@1347 | 403 | } |
mcimadamore@1347 | 404 | } |
mcimadamore@1347 | 405 | if (!progress) { |
mcimadamore@1347 | 406 | //remove all variables that have already been instantiated |
mcimadamore@1347 | 407 | //from the list of stuck variables |
mcimadamore@1562 | 408 | inferenceContext.solveAny(List.from(stuckVars), warn); |
mcimadamore@1550 | 409 | inferenceContext.notifyChange(); |
mcimadamore@1347 | 410 | } |
mcimadamore@1347 | 411 | } |
mcimadamore@1347 | 412 | } |
mcimadamore@1551 | 413 | } |
mcimadamore@1551 | 414 | |
mcimadamore@1551 | 415 | /** |
mcimadamore@1551 | 416 | * Class representing a deferred attribution node. It keeps track of |
mcimadamore@1551 | 417 | * a deferred type, along with the expected target type information. |
mcimadamore@1551 | 418 | */ |
mcimadamore@1551 | 419 | class DeferredAttrNode implements Infer.FreeTypeListener { |
mcimadamore@1551 | 420 | |
mcimadamore@1551 | 421 | /** underlying deferred type */ |
mcimadamore@1551 | 422 | DeferredType dt; |
mcimadamore@1551 | 423 | |
mcimadamore@1551 | 424 | /** underlying target type information */ |
mcimadamore@1551 | 425 | ResultInfo resultInfo; |
mcimadamore@1551 | 426 | |
mcimadamore@1551 | 427 | /** list of uninferred inference variables causing this node to be stuck */ |
mcimadamore@1551 | 428 | List<Type> stuckVars; |
mcimadamore@1551 | 429 | |
mcimadamore@1551 | 430 | DeferredAttrNode(DeferredType dt, ResultInfo resultInfo, List<Type> stuckVars) { |
mcimadamore@1551 | 431 | this.dt = dt; |
mcimadamore@1551 | 432 | this.resultInfo = resultInfo; |
mcimadamore@1551 | 433 | this.stuckVars = stuckVars; |
mcimadamore@1551 | 434 | if (!stuckVars.isEmpty()) { |
mcimadamore@1551 | 435 | resultInfo.checkContext.inferenceContext().addFreeTypeListener(stuckVars, this); |
mcimadamore@1551 | 436 | } |
mcimadamore@1551 | 437 | } |
mcimadamore@1551 | 438 | |
mcimadamore@1551 | 439 | @Override |
mcimadamore@1551 | 440 | public void typesInferred(InferenceContext inferenceContext) { |
mcimadamore@1551 | 441 | stuckVars = List.nil(); |
mcimadamore@1551 | 442 | resultInfo = resultInfo.dup(inferenceContext.asInstType(resultInfo.pt)); |
mcimadamore@1551 | 443 | } |
mcimadamore@1347 | 444 | |
mcimadamore@1347 | 445 | /** |
mcimadamore@1551 | 446 | * Process a deferred attribution node. |
mcimadamore@1551 | 447 | * Invariant: a stuck node cannot be processed. |
mcimadamore@1347 | 448 | */ |
mcimadamore@1551 | 449 | @SuppressWarnings("fallthrough") |
mcimadamore@1551 | 450 | boolean process(DeferredAttrContext deferredAttrContext) { |
mcimadamore@1551 | 451 | switch (deferredAttrContext.mode) { |
mcimadamore@1551 | 452 | case SPECULATIVE: |
mcimadamore@1551 | 453 | dt.check(resultInfo, List.<Type>nil(), new StructuralStuckChecker()); |
mcimadamore@1551 | 454 | return true; |
mcimadamore@1551 | 455 | case CHECK: |
mcimadamore@1551 | 456 | if (stuckVars.nonEmpty()) { |
mcimadamore@1562 | 457 | //stuck expression - see if we can propagate |
mcimadamore@1562 | 458 | if (deferredAttrContext.parent != emptyDeferredAttrContext && |
mcimadamore@1562 | 459 | Type.containsAny(deferredAttrContext.parent.inferenceContext.inferencevars, List.from(stuckVars))) { |
mcimadamore@1562 | 460 | deferredAttrContext.parent.deferredAttrNodes.add(this); |
mcimadamore@1562 | 461 | dt.check(resultInfo, List.<Type>nil(), dummyCompleter); |
mcimadamore@1562 | 462 | return true; |
mcimadamore@1562 | 463 | } else { |
mcimadamore@1562 | 464 | return false; |
mcimadamore@1562 | 465 | } |
mcimadamore@1551 | 466 | } else { |
mcimadamore@1551 | 467 | dt.check(resultInfo, stuckVars, basicCompleter); |
mcimadamore@1551 | 468 | return true; |
mcimadamore@1551 | 469 | } |
mcimadamore@1551 | 470 | default: |
mcimadamore@1551 | 471 | throw new AssertionError("Bad mode"); |
mcimadamore@1551 | 472 | } |
mcimadamore@1551 | 473 | } |
mcimadamore@1347 | 474 | |
mcimadamore@1551 | 475 | /** |
mcimadamore@1551 | 476 | * Structural checker for stuck expressions |
mcimadamore@1551 | 477 | */ |
mcimadamore@1551 | 478 | class StructuralStuckChecker extends TreeScanner implements DeferredTypeCompleter { |
mcimadamore@1347 | 479 | |
mcimadamore@1347 | 480 | ResultInfo resultInfo; |
mcimadamore@1551 | 481 | InferenceContext inferenceContext; |
mcimadamore@1581 | 482 | Env<AttrContext> env; |
mcimadamore@1347 | 483 | |
mcimadamore@1551 | 484 | public Type complete(DeferredType dt, ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) { |
mcimadamore@1551 | 485 | this.resultInfo = resultInfo; |
mcimadamore@1551 | 486 | this.inferenceContext = deferredAttrContext.inferenceContext; |
mcimadamore@1581 | 487 | this.env = dt.env.dup(dt.tree, dt.env.info.dup()); |
mcimadamore@1551 | 488 | dt.tree.accept(this); |
mcimadamore@1551 | 489 | dt.speculativeCache.put(deferredAttrContext.msym, stuckTree, deferredAttrContext.phase); |
mcimadamore@1551 | 490 | return Type.noType; |
mcimadamore@1551 | 491 | } |
mcimadamore@1347 | 492 | |
mcimadamore@1551 | 493 | @Override |
mcimadamore@1551 | 494 | public void visitLambda(JCLambda tree) { |
mcimadamore@1551 | 495 | Check.CheckContext checkContext = resultInfo.checkContext; |
mcimadamore@1551 | 496 | Type pt = resultInfo.pt; |
mcimadamore@1551 | 497 | if (inferenceContext.inferencevars.contains(pt)) { |
mcimadamore@1551 | 498 | //ok |
mcimadamore@1551 | 499 | return; |
mcimadamore@1551 | 500 | } else { |
mcimadamore@1551 | 501 | //must be a functional descriptor |
mcimadamore@1551 | 502 | try { |
mcimadamore@1551 | 503 | Type desc = types.findDescriptorType(pt); |
mcimadamore@1551 | 504 | if (desc.getParameterTypes().length() != tree.params.length()) { |
mcimadamore@1551 | 505 | checkContext.report(tree, diags.fragment("incompatible.arg.types.in.lambda")); |
mcimadamore@1551 | 506 | } |
mcimadamore@1551 | 507 | } catch (Types.FunctionDescriptorLookupError ex) { |
mcimadamore@1551 | 508 | checkContext.report(null, ex.getDiagnostic()); |
mcimadamore@1551 | 509 | } |
mcimadamore@1347 | 510 | } |
mcimadamore@1347 | 511 | } |
mcimadamore@1347 | 512 | |
mcimadamore@1347 | 513 | @Override |
mcimadamore@1551 | 514 | public void visitNewClass(JCNewClass tree) { |
mcimadamore@1551 | 515 | //do nothing |
mcimadamore@1347 | 516 | } |
mcimadamore@1347 | 517 | |
mcimadamore@1551 | 518 | @Override |
mcimadamore@1551 | 519 | public void visitApply(JCMethodInvocation tree) { |
mcimadamore@1551 | 520 | //do nothing |
mcimadamore@1347 | 521 | } |
mcimadamore@1347 | 522 | |
mcimadamore@1551 | 523 | @Override |
mcimadamore@1551 | 524 | public void visitReference(JCMemberReference tree) { |
mcimadamore@1551 | 525 | Check.CheckContext checkContext = resultInfo.checkContext; |
mcimadamore@1551 | 526 | Type pt = resultInfo.pt; |
mcimadamore@1551 | 527 | if (inferenceContext.inferencevars.contains(pt)) { |
mcimadamore@1551 | 528 | //ok |
mcimadamore@1551 | 529 | return; |
mcimadamore@1551 | 530 | } else { |
mcimadamore@1551 | 531 | try { |
mcimadamore@1551 | 532 | types.findDescriptorType(pt); |
mcimadamore@1551 | 533 | } catch (Types.FunctionDescriptorLookupError ex) { |
mcimadamore@1551 | 534 | checkContext.report(null, ex.getDiagnostic()); |
mcimadamore@1510 | 535 | } |
mcimadamore@1581 | 536 | JCExpression exprTree = (JCExpression)attribSpeculative(tree.getQualifierExpression(), env, |
mcimadamore@1581 | 537 | attr.memberReferenceQualifierResult(tree)); |
mcimadamore@1581 | 538 | ListBuffer<Type> argtypes = ListBuffer.lb(); |
mcimadamore@1581 | 539 | for (Type t : types.findDescriptorType(pt).getParameterTypes()) { |
mcimadamore@1674 | 540 | argtypes.append(Type.noType); |
mcimadamore@1581 | 541 | } |
mcimadamore@1581 | 542 | JCMemberReference mref2 = new TreeCopier<Void>(make).copy(tree); |
mcimadamore@1581 | 543 | mref2.expr = exprTree; |
mcimadamore@1581 | 544 | Pair<Symbol, ?> lookupRes = |
mcimadamore@1674 | 545 | rs.resolveMemberReference(tree, env, mref2, exprTree.type, |
mcimadamore@1674 | 546 | tree.name, argtypes.toList(), null, true, rs.arityMethodCheck); |
mcimadamore@1581 | 547 | switch (lookupRes.fst.kind) { |
mcimadamore@1581 | 548 | //note: as argtypes are erroneous types, type-errors must |
mcimadamore@1581 | 549 | //have been caused by arity mismatch |
mcimadamore@1581 | 550 | case Kinds.ABSENT_MTH: |
mcimadamore@1581 | 551 | case Kinds.WRONG_MTH: |
mcimadamore@1581 | 552 | case Kinds.WRONG_MTHS: |
mcimadamore@1581 | 553 | case Kinds.STATICERR: |
mcimadamore@1581 | 554 | case Kinds.MISSING_ENCL: |
mcimadamore@1581 | 555 | checkContext.report(null, diags.fragment("incompatible.arg.types.in.mref")); |
mcimadamore@1581 | 556 | } |
mcimadamore@1510 | 557 | } |
mcimadamore@1347 | 558 | } |
mcimadamore@1347 | 559 | } |
mcimadamore@1347 | 560 | } |
mcimadamore@1347 | 561 | |
mcimadamore@1347 | 562 | /** an empty deferred attribution context - all methods throw exceptions */ |
mcimadamore@1347 | 563 | final DeferredAttrContext emptyDeferredAttrContext = |
mcimadamore@1551 | 564 | new DeferredAttrContext(AttrMode.CHECK, null, MethodResolutionPhase.BOX, null, null, null) { |
mcimadamore@1347 | 565 | @Override |
mcimadamore@1347 | 566 | void addDeferredAttrNode(DeferredType dt, ResultInfo ri, List<Type> stuckVars) { |
mcimadamore@1347 | 567 | Assert.error("Empty deferred context!"); |
mcimadamore@1347 | 568 | } |
mcimadamore@1347 | 569 | @Override |
mcimadamore@1347 | 570 | void complete() { |
mcimadamore@1347 | 571 | Assert.error("Empty deferred context!"); |
mcimadamore@1347 | 572 | } |
mcimadamore@1347 | 573 | }; |
mcimadamore@1347 | 574 | |
mcimadamore@1347 | 575 | /** |
mcimadamore@1347 | 576 | * Map a list of types possibly containing one or more deferred types |
mcimadamore@1347 | 577 | * into a list of ordinary types. Each deferred type D is mapped into a type T, |
mcimadamore@1347 | 578 | * where T is computed by retrieving the type that has already been |
mcimadamore@1347 | 579 | * computed for D during a previous deferred attribution round of the given kind. |
mcimadamore@1347 | 580 | */ |
mcimadamore@1347 | 581 | class DeferredTypeMap extends Type.Mapping { |
mcimadamore@1347 | 582 | |
mcimadamore@1347 | 583 | DeferredAttrContext deferredAttrContext; |
mcimadamore@1347 | 584 | |
mcimadamore@1347 | 585 | protected DeferredTypeMap(AttrMode mode, Symbol msym, MethodResolutionPhase phase) { |
mcimadamore@1347 | 586 | super(String.format("deferredTypeMap[%s]", mode)); |
mcimadamore@1551 | 587 | this.deferredAttrContext = new DeferredAttrContext(mode, msym, phase, |
mcimadamore@1551 | 588 | infer.emptyContext, emptyDeferredAttrContext, types.noWarnings); |
mcimadamore@1347 | 589 | } |
mcimadamore@1347 | 590 | |
mcimadamore@1347 | 591 | protected boolean validState(DeferredType dt) { |
mcimadamore@1347 | 592 | return dt.mode != null && |
mcimadamore@1347 | 593 | deferredAttrContext.mode.ordinal() <= dt.mode.ordinal(); |
mcimadamore@1347 | 594 | } |
mcimadamore@1347 | 595 | |
mcimadamore@1347 | 596 | @Override |
mcimadamore@1347 | 597 | public Type apply(Type t) { |
jjg@1374 | 598 | if (!t.hasTag(DEFERRED)) { |
mcimadamore@1347 | 599 | return t.map(this); |
mcimadamore@1347 | 600 | } else { |
mcimadamore@1347 | 601 | DeferredType dt = (DeferredType)t; |
mcimadamore@1347 | 602 | Assert.check(validState(dt)); |
mcimadamore@1347 | 603 | return typeOf(dt); |
mcimadamore@1347 | 604 | } |
mcimadamore@1347 | 605 | } |
mcimadamore@1347 | 606 | |
mcimadamore@1347 | 607 | protected Type typeOf(DeferredType dt) { |
mcimadamore@1347 | 608 | switch (deferredAttrContext.mode) { |
mcimadamore@1347 | 609 | case CHECK: |
mcimadamore@1347 | 610 | return dt.tree.type == null ? Type.noType : dt.tree.type; |
mcimadamore@1347 | 611 | case SPECULATIVE: |
mcimadamore@1347 | 612 | return dt.speculativeType(deferredAttrContext.msym, deferredAttrContext.phase); |
mcimadamore@1347 | 613 | } |
mcimadamore@1347 | 614 | Assert.error(); |
mcimadamore@1347 | 615 | return null; |
mcimadamore@1347 | 616 | } |
mcimadamore@1347 | 617 | } |
mcimadamore@1347 | 618 | |
mcimadamore@1347 | 619 | /** |
mcimadamore@1347 | 620 | * Specialized recovery deferred mapping. |
mcimadamore@1347 | 621 | * Each deferred type D is mapped into a type T, where T is computed either by |
mcimadamore@1347 | 622 | * (i) retrieving the type that has already been computed for D during a previous |
mcimadamore@1347 | 623 | * attribution round (as before), or (ii) by synthesizing a new type R for D |
mcimadamore@1347 | 624 | * (the latter step is useful in a recovery scenario). |
mcimadamore@1347 | 625 | */ |
mcimadamore@1347 | 626 | public class RecoveryDeferredTypeMap extends DeferredTypeMap { |
mcimadamore@1347 | 627 | |
mcimadamore@1347 | 628 | public RecoveryDeferredTypeMap(AttrMode mode, Symbol msym, MethodResolutionPhase phase) { |
mcimadamore@1415 | 629 | super(mode, msym, phase != null ? phase : MethodResolutionPhase.BOX); |
mcimadamore@1347 | 630 | } |
mcimadamore@1347 | 631 | |
mcimadamore@1347 | 632 | @Override |
mcimadamore@1347 | 633 | protected Type typeOf(DeferredType dt) { |
mcimadamore@1347 | 634 | Type owntype = super.typeOf(dt); |
mcimadamore@1415 | 635 | return owntype == Type.noType ? |
mcimadamore@1347 | 636 | recover(dt) : owntype; |
mcimadamore@1347 | 637 | } |
mcimadamore@1347 | 638 | |
mcimadamore@1347 | 639 | @Override |
mcimadamore@1347 | 640 | protected boolean validState(DeferredType dt) { |
mcimadamore@1347 | 641 | return true; |
mcimadamore@1347 | 642 | } |
mcimadamore@1347 | 643 | |
mcimadamore@1347 | 644 | /** |
mcimadamore@1347 | 645 | * Synthesize a type for a deferred type that hasn't been previously |
mcimadamore@1347 | 646 | * reduced to an ordinary type. Functional deferred types and conditionals |
mcimadamore@1347 | 647 | * are mapped to themselves, in order to have a richer diagnostic |
mcimadamore@1347 | 648 | * representation. Remaining deferred types are attributed using |
mcimadamore@1347 | 649 | * a default expected type (j.l.Object). |
mcimadamore@1347 | 650 | */ |
mcimadamore@1347 | 651 | private Type recover(DeferredType dt) { |
mcimadamore@1889 | 652 | dt.check(attr.new RecoveryInfo(deferredAttrContext) { |
mcimadamore@1889 | 653 | @Override |
mcimadamore@1889 | 654 | protected Type check(DiagnosticPosition pos, Type found) { |
mcimadamore@1889 | 655 | return chk.checkNonVoid(pos, super.check(pos, found)); |
mcimadamore@1889 | 656 | } |
mcimadamore@1889 | 657 | }); |
mcimadamore@1415 | 658 | return super.apply(dt); |
mcimadamore@1347 | 659 | } |
mcimadamore@1347 | 660 | } |
mcimadamore@1347 | 661 | |
mcimadamore@1347 | 662 | /** |
mcimadamore@1347 | 663 | * Retrieves the list of inference variables that need to be inferred before |
mcimadamore@1347 | 664 | * an AST node can be type-checked |
mcimadamore@1347 | 665 | */ |
mcimadamore@1347 | 666 | @SuppressWarnings("fallthrough") |
mcimadamore@1415 | 667 | List<Type> stuckVars(JCTree tree, Env<AttrContext> env, ResultInfo resultInfo) { |
mcimadamore@1415 | 668 | if (resultInfo.pt.hasTag(NONE) || resultInfo.pt.isErroneous()) { |
mcimadamore@1348 | 669 | return List.nil(); |
mcimadamore@1348 | 670 | } else { |
mcimadamore@1481 | 671 | return stuckVarsInternal(tree, resultInfo.pt, resultInfo.checkContext.inferenceContext()); |
mcimadamore@1481 | 672 | } |
mcimadamore@1481 | 673 | } |
mcimadamore@1481 | 674 | //where |
mcimadamore@1481 | 675 | private List<Type> stuckVarsInternal(JCTree tree, Type pt, Infer.InferenceContext inferenceContext) { |
mcimadamore@1481 | 676 | StuckChecker sc = new StuckChecker(pt, inferenceContext); |
mcimadamore@1348 | 677 | sc.scan(tree); |
mcimadamore@1348 | 678 | return List.from(sc.stuckVars); |
mcimadamore@1348 | 679 | } |
mcimadamore@1481 | 680 | |
mcimadamore@1481 | 681 | /** |
mcimadamore@1481 | 682 | * A special tree scanner that would only visit portions of a given tree. |
mcimadamore@1481 | 683 | * The set of nodes visited by the scanner can be customized at construction-time. |
mcimadamore@1481 | 684 | */ |
mcimadamore@1481 | 685 | abstract static class FilterScanner extends TreeScanner { |
mcimadamore@1481 | 686 | |
mcimadamore@1481 | 687 | final Filter<JCTree> treeFilter; |
mcimadamore@1481 | 688 | |
mcimadamore@1481 | 689 | FilterScanner(final Set<JCTree.Tag> validTags) { |
mcimadamore@1481 | 690 | this.treeFilter = new Filter<JCTree>() { |
mcimadamore@1481 | 691 | public boolean accepts(JCTree t) { |
mcimadamore@1481 | 692 | return validTags.contains(t.getTag()); |
mcimadamore@1481 | 693 | } |
mcimadamore@1481 | 694 | }; |
mcimadamore@1481 | 695 | } |
mcimadamore@1481 | 696 | |
mcimadamore@1481 | 697 | @Override |
mcimadamore@1481 | 698 | public void scan(JCTree tree) { |
mcimadamore@1481 | 699 | if (tree != null) { |
mcimadamore@1481 | 700 | if (treeFilter.accepts(tree)) { |
mcimadamore@1481 | 701 | super.scan(tree); |
mcimadamore@1481 | 702 | } else { |
mcimadamore@1481 | 703 | skip(tree); |
mcimadamore@1481 | 704 | } |
mcimadamore@1481 | 705 | } |
mcimadamore@1481 | 706 | } |
mcimadamore@1481 | 707 | |
mcimadamore@1481 | 708 | /** |
mcimadamore@1481 | 709 | * handler that is executed when a node has been discarded |
mcimadamore@1481 | 710 | */ |
mcimadamore@1481 | 711 | abstract void skip(JCTree tree); |
mcimadamore@1481 | 712 | } |
mcimadamore@1481 | 713 | |
mcimadamore@1481 | 714 | /** |
mcimadamore@1481 | 715 | * A tree scanner suitable for visiting the target-type dependent nodes of |
mcimadamore@1481 | 716 | * a given argument expression. |
mcimadamore@1481 | 717 | */ |
mcimadamore@1481 | 718 | static class PolyScanner extends FilterScanner { |
mcimadamore@1481 | 719 | |
mcimadamore@1481 | 720 | PolyScanner() { |
mcimadamore@1481 | 721 | super(EnumSet.of(CONDEXPR, PARENS, LAMBDA, REFERENCE)); |
mcimadamore@1481 | 722 | } |
mcimadamore@1481 | 723 | |
mcimadamore@1481 | 724 | @Override |
mcimadamore@1481 | 725 | void skip(JCTree tree) { |
mcimadamore@1481 | 726 | //do nothing |
mcimadamore@1481 | 727 | } |
mcimadamore@1481 | 728 | } |
mcimadamore@1481 | 729 | |
mcimadamore@1481 | 730 | /** |
mcimadamore@1481 | 731 | * A tree scanner suitable for visiting the target-type dependent nodes nested |
mcimadamore@1481 | 732 | * within a lambda expression body. |
mcimadamore@1481 | 733 | */ |
mcimadamore@1481 | 734 | static class LambdaReturnScanner extends FilterScanner { |
mcimadamore@1481 | 735 | |
mcimadamore@1481 | 736 | LambdaReturnScanner() { |
mcimadamore@1481 | 737 | super(EnumSet.of(BLOCK, CASE, CATCH, DOLOOP, FOREACHLOOP, |
mcimadamore@1481 | 738 | FORLOOP, RETURN, SYNCHRONIZED, SWITCH, TRY, WHILELOOP)); |
mcimadamore@1481 | 739 | } |
mcimadamore@1481 | 740 | |
mcimadamore@1481 | 741 | @Override |
mcimadamore@1481 | 742 | void skip(JCTree tree) { |
mcimadamore@1481 | 743 | //do nothing |
mcimadamore@1481 | 744 | } |
mcimadamore@1348 | 745 | } |
mcimadamore@1348 | 746 | |
mcimadamore@1348 | 747 | /** |
mcimadamore@1348 | 748 | * This visitor is used to check that structural expressions conform |
mcimadamore@1348 | 749 | * to their target - this step is required as inference could end up |
mcimadamore@1348 | 750 | * inferring types that make some of the nested expressions incompatible |
mcimadamore@1348 | 751 | * with their corresponding instantiated target |
mcimadamore@1348 | 752 | */ |
mcimadamore@1481 | 753 | class StuckChecker extends PolyScanner { |
mcimadamore@1348 | 754 | |
mcimadamore@1348 | 755 | Type pt; |
mcimadamore@1348 | 756 | Infer.InferenceContext inferenceContext; |
mcimadamore@1415 | 757 | Set<Type> stuckVars = new LinkedHashSet<Type>(); |
mcimadamore@1348 | 758 | |
mcimadamore@1481 | 759 | StuckChecker(Type pt, Infer.InferenceContext inferenceContext) { |
mcimadamore@1481 | 760 | this.pt = pt; |
mcimadamore@1481 | 761 | this.inferenceContext = inferenceContext; |
mcimadamore@1348 | 762 | } |
mcimadamore@1348 | 763 | |
mcimadamore@1348 | 764 | @Override |
mcimadamore@1348 | 765 | public void visitLambda(JCLambda tree) { |
mcimadamore@1481 | 766 | if (inferenceContext.inferenceVars().contains(pt)) { |
mcimadamore@1481 | 767 | stuckVars.add(pt); |
mcimadamore@1348 | 768 | } |
mcimadamore@1510 | 769 | if (!types.isFunctionalInterface(pt)) { |
mcimadamore@1481 | 770 | return; |
mcimadamore@1481 | 771 | } |
mcimadamore@1481 | 772 | Type descType = types.findDescriptorType(pt); |
mcimadamore@1481 | 773 | List<Type> freeArgVars = inferenceContext.freeVarsIn(descType.getParameterTypes()); |
mcimadamore@1510 | 774 | if (tree.paramKind == JCLambda.ParameterKind.IMPLICIT && |
mcimadamore@1481 | 775 | freeArgVars.nonEmpty()) { |
mcimadamore@1481 | 776 | stuckVars.addAll(freeArgVars); |
mcimadamore@1481 | 777 | } |
mcimadamore@1481 | 778 | scanLambdaBody(tree, descType.getReturnType()); |
mcimadamore@1348 | 779 | } |
mcimadamore@1348 | 780 | |
mcimadamore@1348 | 781 | @Override |
mcimadamore@1348 | 782 | public void visitReference(JCMemberReference tree) { |
mcimadamore@1348 | 783 | scan(tree.expr); |
mcimadamore@1348 | 784 | if (inferenceContext.inferenceVars().contains(pt)) { |
mcimadamore@1348 | 785 | stuckVars.add(pt); |
mcimadamore@1348 | 786 | return; |
mcimadamore@1348 | 787 | } |
mcimadamore@1510 | 788 | if (!types.isFunctionalInterface(pt)) { |
mcimadamore@1348 | 789 | return; |
mcimadamore@1348 | 790 | } |
mcimadamore@1415 | 791 | |
mcimadamore@1348 | 792 | Type descType = types.findDescriptorType(pt); |
mcimadamore@1348 | 793 | List<Type> freeArgVars = inferenceContext.freeVarsIn(descType.getParameterTypes()); |
mcimadamore@1348 | 794 | stuckVars.addAll(freeArgVars); |
mcimadamore@1348 | 795 | } |
mcimadamore@1348 | 796 | |
mcimadamore@1481 | 797 | void scanLambdaBody(JCLambda lambda, final Type pt) { |
mcimadamore@1481 | 798 | if (lambda.getBodyKind() == JCTree.JCLambda.BodyKind.EXPRESSION) { |
mcimadamore@1481 | 799 | stuckVars.addAll(stuckVarsInternal(lambda.body, pt, inferenceContext)); |
mcimadamore@1481 | 800 | } else { |
mcimadamore@1481 | 801 | LambdaReturnScanner lambdaScanner = new LambdaReturnScanner() { |
mcimadamore@1481 | 802 | @Override |
mcimadamore@1481 | 803 | public void visitReturn(JCReturn tree) { |
mcimadamore@1481 | 804 | if (tree.expr != null) { |
mcimadamore@1481 | 805 | stuckVars.addAll(stuckVarsInternal(tree.expr, pt, inferenceContext)); |
mcimadamore@1481 | 806 | } |
mcimadamore@1481 | 807 | } |
mcimadamore@1481 | 808 | }; |
mcimadamore@1481 | 809 | lambdaScanner.scan(lambda.body); |
mcimadamore@1348 | 810 | } |
mcimadamore@1347 | 811 | } |
mcimadamore@1347 | 812 | } |
mcimadamore@1697 | 813 | |
mcimadamore@1697 | 814 | /** |
mcimadamore@1697 | 815 | * Does the argument expression {@code expr} need speculative type-checking? |
mcimadamore@1697 | 816 | */ |
mcimadamore@1697 | 817 | boolean isDeferred(Env<AttrContext> env, JCExpression expr) { |
mcimadamore@1697 | 818 | DeferredChecker dc = new DeferredChecker(env); |
mcimadamore@1697 | 819 | dc.scan(expr); |
mcimadamore@1697 | 820 | return dc.result.isPoly(); |
mcimadamore@1697 | 821 | } |
mcimadamore@1697 | 822 | |
mcimadamore@1697 | 823 | /** |
mcimadamore@1697 | 824 | * The kind of an argument expression. This is used by the analysis that |
mcimadamore@1697 | 825 | * determines as to whether speculative attribution is necessary. |
mcimadamore@1697 | 826 | */ |
mcimadamore@1697 | 827 | enum ArgumentExpressionKind { |
mcimadamore@1697 | 828 | |
mcimadamore@1697 | 829 | /** kind that denotes poly argument expression */ |
mcimadamore@1697 | 830 | POLY, |
mcimadamore@1697 | 831 | /** kind that denotes a standalone expression */ |
mcimadamore@1697 | 832 | NO_POLY, |
mcimadamore@1697 | 833 | /** kind that denotes a primitive/boxed standalone expression */ |
mcimadamore@1697 | 834 | PRIMITIVE; |
mcimadamore@1697 | 835 | |
mcimadamore@1697 | 836 | /** |
mcimadamore@1697 | 837 | * Does this kind denote a poly argument expression |
mcimadamore@1697 | 838 | */ |
mcimadamore@1697 | 839 | public final boolean isPoly() { |
mcimadamore@1697 | 840 | return this == POLY; |
mcimadamore@1697 | 841 | } |
mcimadamore@1697 | 842 | |
mcimadamore@1697 | 843 | /** |
mcimadamore@1697 | 844 | * Does this kind denote a primitive standalone expression |
mcimadamore@1697 | 845 | */ |
mcimadamore@1697 | 846 | public final boolean isPrimitive() { |
mcimadamore@1697 | 847 | return this == PRIMITIVE; |
mcimadamore@1697 | 848 | } |
mcimadamore@1697 | 849 | |
mcimadamore@1697 | 850 | /** |
mcimadamore@1697 | 851 | * Compute the kind of a standalone expression of a given type |
mcimadamore@1697 | 852 | */ |
mcimadamore@1697 | 853 | static ArgumentExpressionKind standaloneKind(Type type, Types types) { |
mcimadamore@1697 | 854 | return types.unboxedTypeOrType(type).isPrimitive() ? |
mcimadamore@1697 | 855 | ArgumentExpressionKind.PRIMITIVE : |
mcimadamore@1697 | 856 | ArgumentExpressionKind.NO_POLY; |
mcimadamore@1697 | 857 | } |
mcimadamore@1697 | 858 | |
mcimadamore@1697 | 859 | /** |
mcimadamore@1697 | 860 | * Compute the kind of a method argument expression given its symbol |
mcimadamore@1697 | 861 | */ |
mcimadamore@1697 | 862 | static ArgumentExpressionKind methodKind(Symbol sym, Types types) { |
mcimadamore@1697 | 863 | Type restype = sym.type.getReturnType(); |
mcimadamore@1697 | 864 | if (sym.type.hasTag(FORALL) && |
mcimadamore@1697 | 865 | restype.containsAny(((ForAll)sym.type).tvars)) { |
mcimadamore@1697 | 866 | return ArgumentExpressionKind.POLY; |
mcimadamore@1697 | 867 | } else { |
mcimadamore@1697 | 868 | return ArgumentExpressionKind.standaloneKind(restype, types); |
mcimadamore@1697 | 869 | } |
mcimadamore@1697 | 870 | } |
mcimadamore@1697 | 871 | } |
mcimadamore@1697 | 872 | |
mcimadamore@1697 | 873 | /** |
mcimadamore@1697 | 874 | * Tree scanner used for checking as to whether an argument expression |
mcimadamore@1697 | 875 | * requires speculative attribution |
mcimadamore@1697 | 876 | */ |
mcimadamore@1697 | 877 | final class DeferredChecker extends FilterScanner { |
mcimadamore@1697 | 878 | |
mcimadamore@1697 | 879 | Env<AttrContext> env; |
mcimadamore@1697 | 880 | ArgumentExpressionKind result; |
mcimadamore@1697 | 881 | |
mcimadamore@1697 | 882 | public DeferredChecker(Env<AttrContext> env) { |
mcimadamore@1697 | 883 | super(deferredCheckerTags); |
mcimadamore@1697 | 884 | this.env = env; |
mcimadamore@1697 | 885 | } |
mcimadamore@1697 | 886 | |
mcimadamore@1697 | 887 | @Override |
mcimadamore@1697 | 888 | public void visitLambda(JCLambda tree) { |
mcimadamore@1697 | 889 | //a lambda is always a poly expression |
mcimadamore@1697 | 890 | result = ArgumentExpressionKind.POLY; |
mcimadamore@1697 | 891 | } |
mcimadamore@1697 | 892 | |
mcimadamore@1697 | 893 | @Override |
mcimadamore@1697 | 894 | public void visitReference(JCMemberReference tree) { |
mcimadamore@1697 | 895 | //a method reference is always a poly expression |
mcimadamore@1697 | 896 | result = ArgumentExpressionKind.POLY; |
mcimadamore@1697 | 897 | } |
mcimadamore@1697 | 898 | |
mcimadamore@1697 | 899 | @Override |
mcimadamore@1697 | 900 | public void visitTypeCast(JCTypeCast tree) { |
mcimadamore@1697 | 901 | //a cast is always a standalone expression |
mcimadamore@1697 | 902 | result = ArgumentExpressionKind.NO_POLY; |
mcimadamore@1697 | 903 | } |
mcimadamore@1697 | 904 | |
mcimadamore@1697 | 905 | @Override |
mcimadamore@1697 | 906 | public void visitConditional(JCConditional tree) { |
mcimadamore@1697 | 907 | scan(tree.truepart); |
mcimadamore@1697 | 908 | if (!result.isPrimitive()) { |
mcimadamore@1697 | 909 | result = ArgumentExpressionKind.POLY; |
mcimadamore@1697 | 910 | return; |
mcimadamore@1697 | 911 | } |
mcimadamore@1697 | 912 | scan(tree.falsepart); |
mcimadamore@1697 | 913 | result = reduce(ArgumentExpressionKind.PRIMITIVE); |
mcimadamore@1697 | 914 | } |
mcimadamore@1697 | 915 | |
mcimadamore@1697 | 916 | @Override |
mcimadamore@1697 | 917 | public void visitNewClass(JCNewClass tree) { |
mcimadamore@1697 | 918 | result = (TreeInfo.isDiamond(tree) || attr.findDiamonds) ? |
mcimadamore@1697 | 919 | ArgumentExpressionKind.POLY : ArgumentExpressionKind.NO_POLY; |
mcimadamore@1697 | 920 | } |
mcimadamore@1697 | 921 | |
mcimadamore@1697 | 922 | @Override |
mcimadamore@1697 | 923 | public void visitApply(JCMethodInvocation tree) { |
mcimadamore@1697 | 924 | Name name = TreeInfo.name(tree.meth); |
mcimadamore@1697 | 925 | |
mcimadamore@1697 | 926 | //fast path |
mcimadamore@1697 | 927 | if (tree.typeargs.nonEmpty() || |
mcimadamore@1697 | 928 | name == name.table.names._this || |
mcimadamore@1697 | 929 | name == name.table.names._super) { |
mcimadamore@1697 | 930 | result = ArgumentExpressionKind.NO_POLY; |
mcimadamore@1697 | 931 | return; |
mcimadamore@1697 | 932 | } |
mcimadamore@1697 | 933 | |
mcimadamore@1697 | 934 | //slow path |
mcimadamore@1697 | 935 | final JCExpression rec = tree.meth.hasTag(SELECT) ? |
mcimadamore@1697 | 936 | ((JCFieldAccess)tree.meth).selected : |
mcimadamore@1697 | 937 | null; |
mcimadamore@1697 | 938 | |
mcimadamore@1697 | 939 | if (rec != null && !isSimpleReceiver(rec)) { |
mcimadamore@1697 | 940 | //give up if receiver is too complex (to cut down analysis time) |
mcimadamore@1697 | 941 | result = ArgumentExpressionKind.POLY; |
mcimadamore@1697 | 942 | return; |
mcimadamore@1697 | 943 | } |
mcimadamore@1697 | 944 | |
mcimadamore@1697 | 945 | Type site = rec != null ? |
mcimadamore@1697 | 946 | attribSpeculative(rec, env, attr.unknownTypeExprInfo).type : |
mcimadamore@1697 | 947 | env.enclClass.sym.type; |
mcimadamore@1697 | 948 | |
mcimadamore@1888 | 949 | while (site.hasTag(TYPEVAR)) { |
mcimadamore@1888 | 950 | site = site.getUpperBound(); |
mcimadamore@1888 | 951 | } |
mcimadamore@1888 | 952 | |
mcimadamore@1697 | 953 | ListBuffer<Type> args = ListBuffer.lb(); |
mcimadamore@1697 | 954 | for (int i = 0; i < tree.args.length(); i ++) { |
mcimadamore@1697 | 955 | args.append(Type.noType); |
mcimadamore@1697 | 956 | } |
mcimadamore@1697 | 957 | |
mcimadamore@1697 | 958 | Resolve.LookupHelper lh = rs.new LookupHelper(name, site, args.toList(), List.<Type>nil(), MethodResolutionPhase.VARARITY) { |
mcimadamore@1697 | 959 | @Override |
mcimadamore@1697 | 960 | Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) { |
mcimadamore@1697 | 961 | return rec == null ? |
mcimadamore@1697 | 962 | rs.findFun(env, name, argtypes, typeargtypes, phase.isBoxingRequired(), phase.isVarargsRequired()) : |
mcimadamore@1697 | 963 | rs.findMethod(env, site, name, argtypes, typeargtypes, phase.isBoxingRequired(), phase.isVarargsRequired(), false); |
mcimadamore@1697 | 964 | } |
mcimadamore@1697 | 965 | @Override |
mcimadamore@1697 | 966 | Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym) { |
mcimadamore@1697 | 967 | return sym; |
mcimadamore@1697 | 968 | } |
mcimadamore@1697 | 969 | }; |
mcimadamore@1697 | 970 | |
mcimadamore@1697 | 971 | Symbol sym = rs.lookupMethod(env, tree, site.tsym, rs.arityMethodCheck, lh); |
mcimadamore@1697 | 972 | |
mcimadamore@1697 | 973 | if (sym.kind == Kinds.AMBIGUOUS) { |
mcimadamore@1697 | 974 | Resolve.AmbiguityError err = (Resolve.AmbiguityError)sym.baseSymbol(); |
mcimadamore@1697 | 975 | result = ArgumentExpressionKind.PRIMITIVE; |
vromero@1850 | 976 | for (Symbol s : err.ambiguousSyms) { |
vromero@1850 | 977 | if (result.isPoly()) break; |
mcimadamore@1697 | 978 | if (s.kind == Kinds.MTH) { |
mcimadamore@1697 | 979 | result = reduce(ArgumentExpressionKind.methodKind(s, types)); |
mcimadamore@1697 | 980 | } |
mcimadamore@1697 | 981 | } |
mcimadamore@1697 | 982 | } else { |
mcimadamore@1697 | 983 | result = (sym.kind == Kinds.MTH) ? |
mcimadamore@1697 | 984 | ArgumentExpressionKind.methodKind(sym, types) : |
mcimadamore@1697 | 985 | ArgumentExpressionKind.NO_POLY; |
mcimadamore@1697 | 986 | } |
mcimadamore@1697 | 987 | } |
mcimadamore@1697 | 988 | //where |
mcimadamore@1697 | 989 | private boolean isSimpleReceiver(JCTree rec) { |
mcimadamore@1697 | 990 | switch (rec.getTag()) { |
mcimadamore@1697 | 991 | case IDENT: |
mcimadamore@1697 | 992 | return true; |
mcimadamore@1697 | 993 | case SELECT: |
mcimadamore@1697 | 994 | return isSimpleReceiver(((JCFieldAccess)rec).selected); |
mcimadamore@1697 | 995 | case TYPEAPPLY: |
mcimadamore@1697 | 996 | case TYPEARRAY: |
mcimadamore@1697 | 997 | return true; |
mcimadamore@1697 | 998 | case ANNOTATED_TYPE: |
mcimadamore@1697 | 999 | return isSimpleReceiver(((JCAnnotatedType)rec).underlyingType); |
mcimadamore@1697 | 1000 | default: |
mcimadamore@1697 | 1001 | return false; |
mcimadamore@1697 | 1002 | } |
mcimadamore@1697 | 1003 | } |
mcimadamore@1697 | 1004 | private ArgumentExpressionKind reduce(ArgumentExpressionKind kind) { |
mcimadamore@1697 | 1005 | switch (result) { |
mcimadamore@1697 | 1006 | case PRIMITIVE: return kind; |
mcimadamore@1697 | 1007 | case NO_POLY: return kind.isPoly() ? kind : result; |
mcimadamore@1697 | 1008 | case POLY: return result; |
mcimadamore@1697 | 1009 | default: |
mcimadamore@1697 | 1010 | Assert.error(); |
mcimadamore@1697 | 1011 | return null; |
mcimadamore@1697 | 1012 | } |
mcimadamore@1697 | 1013 | } |
mcimadamore@1697 | 1014 | |
mcimadamore@1697 | 1015 | @Override |
mcimadamore@1697 | 1016 | public void visitLiteral(JCLiteral tree) { |
mcimadamore@1697 | 1017 | Type litType = attr.litType(tree.typetag); |
mcimadamore@1697 | 1018 | result = ArgumentExpressionKind.standaloneKind(litType, types); |
mcimadamore@1697 | 1019 | } |
mcimadamore@1697 | 1020 | |
mcimadamore@1697 | 1021 | @Override |
mcimadamore@1697 | 1022 | void skip(JCTree tree) { |
mcimadamore@1697 | 1023 | result = ArgumentExpressionKind.NO_POLY; |
mcimadamore@1697 | 1024 | } |
mcimadamore@1697 | 1025 | } |
mcimadamore@1697 | 1026 | //where |
mcimadamore@1697 | 1027 | private EnumSet<JCTree.Tag> deferredCheckerTags = |
mcimadamore@1697 | 1028 | EnumSet.of(LAMBDA, REFERENCE, PARENS, TYPECAST, |
mcimadamore@1697 | 1029 | CONDEXPR, NEWCLASS, APPLY, LITERAL); |
mcimadamore@1347 | 1030 | } |