Mon, 17 Dec 2012 07:47:05 -0800
8004832: Add new doclint package
Reviewed-by: mcimadamore
mcimadamore@1347 | 1 | /* |
mcimadamore@1347 | 2 | * Copyright (c) 2012, 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@1347 | 31 | import com.sun.tools.javac.code.Symbol.*; |
mcimadamore@1347 | 32 | import com.sun.tools.javac.code.Type.*; |
mcimadamore@1347 | 33 | import com.sun.tools.javac.comp.Attr.ResultInfo; |
mcimadamore@1347 | 34 | import com.sun.tools.javac.comp.Infer.InferenceContext; |
mcimadamore@1347 | 35 | import com.sun.tools.javac.comp.Resolve.MethodResolutionPhase; |
mcimadamore@1347 | 36 | import com.sun.tools.javac.tree.JCTree.*; |
mcimadamore@1347 | 37 | |
mcimadamore@1347 | 38 | import javax.tools.JavaFileObject; |
mcimadamore@1347 | 39 | |
mcimadamore@1347 | 40 | import java.util.ArrayList; |
mcimadamore@1415 | 41 | import java.util.LinkedHashSet; |
mcimadamore@1347 | 42 | import java.util.Map; |
mcimadamore@1347 | 43 | import java.util.Queue; |
mcimadamore@1347 | 44 | import java.util.Set; |
mcimadamore@1347 | 45 | import java.util.WeakHashMap; |
mcimadamore@1347 | 46 | |
mcimadamore@1415 | 47 | import static com.sun.tools.javac.code.TypeTag.*; |
mcimadamore@1347 | 48 | import static com.sun.tools.javac.tree.JCTree.Tag.*; |
mcimadamore@1347 | 49 | |
mcimadamore@1347 | 50 | /** |
mcimadamore@1347 | 51 | * This is an helper class that is used to perform deferred type-analysis. |
mcimadamore@1347 | 52 | * Each time a poly expression occurs in argument position, javac attributes it |
mcimadamore@1347 | 53 | * with a temporary 'deferred type' that is checked (possibly multiple times) |
mcimadamore@1347 | 54 | * against an expected formal type. |
mcimadamore@1347 | 55 | * |
mcimadamore@1347 | 56 | * <p><b>This is NOT part of any supported API. |
mcimadamore@1347 | 57 | * If you write code that depends on this, you do so at your own risk. |
mcimadamore@1347 | 58 | * This code and its internal interfaces are subject to change or |
mcimadamore@1347 | 59 | * deletion without notice.</b> |
mcimadamore@1347 | 60 | */ |
mcimadamore@1347 | 61 | public class DeferredAttr extends JCTree.Visitor { |
mcimadamore@1347 | 62 | protected static final Context.Key<DeferredAttr> deferredAttrKey = |
mcimadamore@1347 | 63 | new Context.Key<DeferredAttr>(); |
mcimadamore@1347 | 64 | |
mcimadamore@1347 | 65 | final Attr attr; |
mcimadamore@1347 | 66 | final Check chk; |
mcimadamore@1347 | 67 | final Enter enter; |
mcimadamore@1347 | 68 | final Infer infer; |
mcimadamore@1347 | 69 | final Log log; |
mcimadamore@1347 | 70 | final Symtab syms; |
mcimadamore@1347 | 71 | final TreeMaker make; |
mcimadamore@1347 | 72 | final Types types; |
mcimadamore@1347 | 73 | |
mcimadamore@1347 | 74 | public static DeferredAttr instance(Context context) { |
mcimadamore@1347 | 75 | DeferredAttr instance = context.get(deferredAttrKey); |
mcimadamore@1347 | 76 | if (instance == null) |
mcimadamore@1347 | 77 | instance = new DeferredAttr(context); |
mcimadamore@1347 | 78 | return instance; |
mcimadamore@1347 | 79 | } |
mcimadamore@1347 | 80 | |
mcimadamore@1347 | 81 | protected DeferredAttr(Context context) { |
mcimadamore@1347 | 82 | context.put(deferredAttrKey, this); |
mcimadamore@1347 | 83 | attr = Attr.instance(context); |
mcimadamore@1347 | 84 | chk = Check.instance(context); |
mcimadamore@1347 | 85 | enter = Enter.instance(context); |
mcimadamore@1347 | 86 | infer = Infer.instance(context); |
mcimadamore@1347 | 87 | log = Log.instance(context); |
mcimadamore@1347 | 88 | syms = Symtab.instance(context); |
mcimadamore@1347 | 89 | make = TreeMaker.instance(context); |
mcimadamore@1347 | 90 | types = Types.instance(context); |
mcimadamore@1347 | 91 | } |
mcimadamore@1347 | 92 | |
mcimadamore@1347 | 93 | /** |
mcimadamore@1347 | 94 | * This type represents a deferred type. A deferred type starts off with |
mcimadamore@1347 | 95 | * no information on the underlying expression type. Such info needs to be |
mcimadamore@1347 | 96 | * discovered through type-checking the deferred type against a target-type. |
mcimadamore@1347 | 97 | * Every deferred type keeps a pointer to the AST node from which it originated. |
mcimadamore@1347 | 98 | */ |
mcimadamore@1347 | 99 | public class DeferredType extends Type { |
mcimadamore@1347 | 100 | |
mcimadamore@1347 | 101 | public JCExpression tree; |
mcimadamore@1347 | 102 | Env<AttrContext> env; |
mcimadamore@1347 | 103 | AttrMode mode; |
mcimadamore@1347 | 104 | SpeculativeCache speculativeCache; |
mcimadamore@1347 | 105 | |
mcimadamore@1347 | 106 | DeferredType(JCExpression tree, Env<AttrContext> env) { |
mcimadamore@1347 | 107 | super(DEFERRED, null); |
mcimadamore@1347 | 108 | this.tree = tree; |
mcimadamore@1347 | 109 | this.env = env.dup(tree, env.info.dup()); |
mcimadamore@1347 | 110 | this.speculativeCache = new SpeculativeCache(); |
mcimadamore@1347 | 111 | } |
mcimadamore@1347 | 112 | |
mcimadamore@1347 | 113 | /** |
mcimadamore@1347 | 114 | * A speculative cache is used to keep track of all overload resolution rounds |
mcimadamore@1347 | 115 | * that triggered speculative attribution on a given deferred type. Each entry |
mcimadamore@1347 | 116 | * stores a pointer to the speculative tree and the resolution phase in which the entry |
mcimadamore@1347 | 117 | * has been added. |
mcimadamore@1347 | 118 | */ |
mcimadamore@1347 | 119 | class SpeculativeCache { |
mcimadamore@1347 | 120 | |
mcimadamore@1347 | 121 | private Map<Symbol, List<Entry>> cache = |
mcimadamore@1347 | 122 | new WeakHashMap<Symbol, List<Entry>>(); |
mcimadamore@1347 | 123 | |
mcimadamore@1347 | 124 | class Entry { |
mcimadamore@1347 | 125 | JCTree speculativeTree; |
mcimadamore@1347 | 126 | Resolve.MethodResolutionPhase phase; |
mcimadamore@1347 | 127 | |
mcimadamore@1347 | 128 | public Entry(JCTree speculativeTree, MethodResolutionPhase phase) { |
mcimadamore@1347 | 129 | this.speculativeTree = speculativeTree; |
mcimadamore@1347 | 130 | this.phase = phase; |
mcimadamore@1347 | 131 | } |
mcimadamore@1347 | 132 | |
mcimadamore@1347 | 133 | boolean matches(Resolve.MethodResolutionPhase phase) { |
mcimadamore@1347 | 134 | return this.phase == phase; |
mcimadamore@1347 | 135 | } |
mcimadamore@1347 | 136 | } |
mcimadamore@1347 | 137 | |
mcimadamore@1347 | 138 | /** |
mcimadamore@1347 | 139 | * Retrieve a speculative cache entry corresponding to given symbol |
mcimadamore@1347 | 140 | * and resolution phase |
mcimadamore@1347 | 141 | */ |
mcimadamore@1347 | 142 | Entry get(Symbol msym, MethodResolutionPhase phase) { |
mcimadamore@1347 | 143 | List<Entry> entries = cache.get(msym); |
mcimadamore@1347 | 144 | if (entries == null) return null; |
mcimadamore@1347 | 145 | for (Entry e : entries) { |
mcimadamore@1347 | 146 | if (e.matches(phase)) return e; |
mcimadamore@1347 | 147 | } |
mcimadamore@1347 | 148 | return null; |
mcimadamore@1347 | 149 | } |
mcimadamore@1347 | 150 | |
mcimadamore@1347 | 151 | /** |
mcimadamore@1347 | 152 | * Stores a speculative cache entry corresponding to given symbol |
mcimadamore@1347 | 153 | * and resolution phase |
mcimadamore@1347 | 154 | */ |
mcimadamore@1347 | 155 | void put(Symbol msym, JCTree speculativeTree, MethodResolutionPhase phase) { |
mcimadamore@1347 | 156 | List<Entry> entries = cache.get(msym); |
mcimadamore@1347 | 157 | if (entries == null) { |
mcimadamore@1347 | 158 | entries = List.nil(); |
mcimadamore@1347 | 159 | } |
mcimadamore@1347 | 160 | cache.put(msym, entries.prepend(new Entry(speculativeTree, phase))); |
mcimadamore@1347 | 161 | } |
mcimadamore@1347 | 162 | } |
mcimadamore@1347 | 163 | |
mcimadamore@1347 | 164 | /** |
mcimadamore@1347 | 165 | * Get the type that has been computed during a speculative attribution round |
mcimadamore@1347 | 166 | */ |
mcimadamore@1347 | 167 | Type speculativeType(Symbol msym, MethodResolutionPhase phase) { |
mcimadamore@1347 | 168 | SpeculativeCache.Entry e = speculativeCache.get(msym, phase); |
mcimadamore@1347 | 169 | return e != null ? e.speculativeTree.type : Type.noType; |
mcimadamore@1347 | 170 | } |
mcimadamore@1347 | 171 | |
mcimadamore@1347 | 172 | /** |
mcimadamore@1347 | 173 | * Check a deferred type against a potential target-type. Depending on |
mcimadamore@1347 | 174 | * the current attribution mode, a normal vs. speculative attribution |
mcimadamore@1347 | 175 | * round is performed on the underlying AST node. There can be only one |
mcimadamore@1347 | 176 | * speculative round for a given target method symbol; moreover, a normal |
mcimadamore@1347 | 177 | * attribution round must follow one or more speculative rounds. |
mcimadamore@1347 | 178 | */ |
mcimadamore@1347 | 179 | Type check(ResultInfo resultInfo) { |
mcimadamore@1347 | 180 | DeferredAttrContext deferredAttrContext = |
mcimadamore@1347 | 181 | resultInfo.checkContext.deferredAttrContext(); |
mcimadamore@1347 | 182 | Assert.check(deferredAttrContext != emptyDeferredAttrContext); |
mcimadamore@1415 | 183 | List<Type> stuckVars = stuckVars(tree, env, resultInfo); |
mcimadamore@1347 | 184 | if (stuckVars.nonEmpty()) { |
mcimadamore@1347 | 185 | deferredAttrContext.addDeferredAttrNode(this, resultInfo, stuckVars); |
mcimadamore@1347 | 186 | return Type.noType; |
mcimadamore@1347 | 187 | } else { |
mcimadamore@1347 | 188 | try { |
mcimadamore@1347 | 189 | switch (deferredAttrContext.mode) { |
mcimadamore@1347 | 190 | case SPECULATIVE: |
mcimadamore@1347 | 191 | Assert.check(mode == null || |
mcimadamore@1347 | 192 | (mode == AttrMode.SPECULATIVE && |
jjg@1374 | 193 | speculativeType(deferredAttrContext.msym, deferredAttrContext.phase).hasTag(NONE))); |
mcimadamore@1347 | 194 | JCTree speculativeTree = attribSpeculative(tree, env, resultInfo); |
mcimadamore@1347 | 195 | speculativeCache.put(deferredAttrContext.msym, speculativeTree, deferredAttrContext.phase); |
mcimadamore@1347 | 196 | return speculativeTree.type; |
mcimadamore@1347 | 197 | case CHECK: |
mcimadamore@1347 | 198 | Assert.check(mode == AttrMode.SPECULATIVE); |
mcimadamore@1347 | 199 | return attr.attribTree(tree, env, resultInfo); |
mcimadamore@1347 | 200 | } |
mcimadamore@1347 | 201 | Assert.error(); |
mcimadamore@1347 | 202 | return null; |
mcimadamore@1347 | 203 | } finally { |
mcimadamore@1347 | 204 | mode = deferredAttrContext.mode; |
mcimadamore@1347 | 205 | } |
mcimadamore@1347 | 206 | } |
mcimadamore@1347 | 207 | } |
mcimadamore@1347 | 208 | } |
mcimadamore@1347 | 209 | |
mcimadamore@1347 | 210 | /** |
mcimadamore@1347 | 211 | * The 'mode' in which the deferred type is to be type-checked |
mcimadamore@1347 | 212 | */ |
mcimadamore@1347 | 213 | public enum AttrMode { |
mcimadamore@1347 | 214 | /** |
mcimadamore@1347 | 215 | * A speculative type-checking round is used during overload resolution |
mcimadamore@1347 | 216 | * mainly to generate constraints on inference variables. Side-effects |
mcimadamore@1347 | 217 | * arising from type-checking the expression associated with the deferred |
mcimadamore@1347 | 218 | * type are reversed after the speculative round finishes. This means the |
mcimadamore@1347 | 219 | * expression tree will be left in a blank state. |
mcimadamore@1347 | 220 | */ |
mcimadamore@1347 | 221 | SPECULATIVE, |
mcimadamore@1347 | 222 | /** |
mcimadamore@1347 | 223 | * This is the plain type-checking mode. Produces side-effects on the underlying AST node |
mcimadamore@1347 | 224 | */ |
mcimadamore@1347 | 225 | CHECK; |
mcimadamore@1347 | 226 | } |
mcimadamore@1347 | 227 | |
mcimadamore@1347 | 228 | /** |
mcimadamore@1347 | 229 | * Routine that performs speculative type-checking; the input AST node is |
mcimadamore@1347 | 230 | * cloned (to avoid side-effects cause by Attr) and compiler state is |
mcimadamore@1347 | 231 | * restored after type-checking. All diagnostics (but critical ones) are |
mcimadamore@1347 | 232 | * disabled during speculative type-checking. |
mcimadamore@1347 | 233 | */ |
mcimadamore@1347 | 234 | JCTree attribSpeculative(JCTree tree, Env<AttrContext> env, ResultInfo resultInfo) { |
mcimadamore@1347 | 235 | JCTree newTree = new TreeCopier<Object>(make).copy(tree); |
mcimadamore@1347 | 236 | Env<AttrContext> speculativeEnv = env.dup(newTree, env.info.dup(env.info.scope.dupUnshared())); |
mcimadamore@1347 | 237 | speculativeEnv.info.scope.owner = env.info.scope.owner; |
mcimadamore@1347 | 238 | final JavaFileObject currentSource = log.currentSourceFile(); |
jjg@1406 | 239 | Log.DeferredDiagnosticHandler deferredDiagnosticHandler = |
jjg@1406 | 240 | new Log.DeferredDiagnosticHandler(log, new Filter<JCDiagnostic>() { |
jjg@1406 | 241 | public boolean accepts(JCDiagnostic t) { |
jjg@1406 | 242 | return t.getDiagnosticSource().getFile().equals(currentSource); |
jjg@1406 | 243 | } |
jjg@1406 | 244 | }); |
mcimadamore@1347 | 245 | try { |
mcimadamore@1347 | 246 | attr.attribTree(newTree, speculativeEnv, resultInfo); |
mcimadamore@1347 | 247 | unenterScanner.scan(newTree); |
mcimadamore@1347 | 248 | return newTree; |
mcimadamore@1347 | 249 | } catch (Abort ex) { |
mcimadamore@1347 | 250 | //if some very bad condition occurred during deferred attribution |
mcimadamore@1347 | 251 | //we should dump all errors before killing javac |
jjg@1406 | 252 | deferredDiagnosticHandler.reportDeferredDiagnostics(); |
mcimadamore@1347 | 253 | throw ex; |
mcimadamore@1347 | 254 | } finally { |
mcimadamore@1347 | 255 | unenterScanner.scan(newTree); |
jjg@1406 | 256 | log.popDiagnosticHandler(deferredDiagnosticHandler); |
mcimadamore@1347 | 257 | } |
mcimadamore@1347 | 258 | } |
mcimadamore@1347 | 259 | //where |
mcimadamore@1347 | 260 | protected TreeScanner unenterScanner = new TreeScanner() { |
mcimadamore@1347 | 261 | @Override |
mcimadamore@1347 | 262 | public void visitClassDef(JCClassDecl tree) { |
mcimadamore@1347 | 263 | ClassSymbol csym = tree.sym; |
mcimadamore@1415 | 264 | //if something went wrong during method applicability check |
mcimadamore@1415 | 265 | //it is possible that nested expressions inside argument expression |
mcimadamore@1415 | 266 | //are left unchecked - in such cases there's nothing to clean up. |
mcimadamore@1415 | 267 | if (csym == null) return; |
mcimadamore@1347 | 268 | enter.typeEnvs.remove(csym); |
mcimadamore@1347 | 269 | chk.compiled.remove(csym.flatname); |
mcimadamore@1347 | 270 | syms.classes.remove(csym.flatname); |
mcimadamore@1347 | 271 | super.visitClassDef(tree); |
mcimadamore@1347 | 272 | } |
mcimadamore@1347 | 273 | }; |
mcimadamore@1347 | 274 | |
mcimadamore@1347 | 275 | /** |
mcimadamore@1347 | 276 | * A deferred context is created on each method check. A deferred context is |
mcimadamore@1347 | 277 | * used to keep track of information associated with the method check, such as |
mcimadamore@1347 | 278 | * the symbol of the method being checked, the overload resolution phase, |
mcimadamore@1347 | 279 | * the kind of attribution mode to be applied to deferred types and so forth. |
mcimadamore@1347 | 280 | * As deferred types are processed (by the method check routine) stuck AST nodes |
mcimadamore@1347 | 281 | * are added (as new deferred attribution nodes) to this context. The complete() |
mcimadamore@1347 | 282 | * routine makes sure that all pending nodes are properly processed, by |
mcimadamore@1347 | 283 | * progressively instantiating all inference variables on which one or more |
mcimadamore@1347 | 284 | * deferred attribution node is stuck. |
mcimadamore@1347 | 285 | */ |
mcimadamore@1347 | 286 | class DeferredAttrContext { |
mcimadamore@1347 | 287 | |
mcimadamore@1347 | 288 | /** attribution mode */ |
mcimadamore@1347 | 289 | final AttrMode mode; |
mcimadamore@1347 | 290 | |
mcimadamore@1347 | 291 | /** symbol of the method being checked */ |
mcimadamore@1347 | 292 | final Symbol msym; |
mcimadamore@1347 | 293 | |
mcimadamore@1347 | 294 | /** method resolution step */ |
mcimadamore@1347 | 295 | final Resolve.MethodResolutionPhase phase; |
mcimadamore@1347 | 296 | |
mcimadamore@1347 | 297 | /** inference context */ |
mcimadamore@1347 | 298 | final InferenceContext inferenceContext; |
mcimadamore@1347 | 299 | |
mcimadamore@1347 | 300 | /** list of deferred attribution nodes to be processed */ |
mcimadamore@1347 | 301 | ArrayList<DeferredAttrNode> deferredAttrNodes = new ArrayList<DeferredAttrNode>(); |
mcimadamore@1347 | 302 | |
mcimadamore@1347 | 303 | DeferredAttrContext(AttrMode mode, Symbol msym, MethodResolutionPhase phase, InferenceContext inferenceContext) { |
mcimadamore@1347 | 304 | this.mode = mode; |
mcimadamore@1347 | 305 | this.msym = msym; |
mcimadamore@1347 | 306 | this.phase = phase; |
mcimadamore@1347 | 307 | this.inferenceContext = inferenceContext; |
mcimadamore@1347 | 308 | } |
mcimadamore@1347 | 309 | |
mcimadamore@1347 | 310 | /** |
mcimadamore@1347 | 311 | * Adds a node to the list of deferred attribution nodes - used by Resolve.rawCheckArgumentsApplicable |
mcimadamore@1347 | 312 | * Nodes added this way act as 'roots' for the out-of-order method checking process. |
mcimadamore@1347 | 313 | */ |
mcimadamore@1347 | 314 | void addDeferredAttrNode(final DeferredType dt, ResultInfo resultInfo, List<Type> stuckVars) { |
mcimadamore@1347 | 315 | deferredAttrNodes.add(new DeferredAttrNode(dt, resultInfo, stuckVars)); |
mcimadamore@1347 | 316 | } |
mcimadamore@1347 | 317 | |
mcimadamore@1347 | 318 | /** |
mcimadamore@1347 | 319 | * Incrementally process all nodes, by skipping 'stuck' nodes and attributing |
mcimadamore@1347 | 320 | * 'unstuck' ones. If at any point no progress can be made (no 'unstuck' nodes) |
mcimadamore@1347 | 321 | * some inference variable might get eagerly instantiated so that all nodes |
mcimadamore@1347 | 322 | * can be type-checked. |
mcimadamore@1347 | 323 | */ |
mcimadamore@1347 | 324 | void complete() { |
mcimadamore@1347 | 325 | while (!deferredAttrNodes.isEmpty()) { |
mcimadamore@1415 | 326 | Set<Type> stuckVars = new LinkedHashSet<Type>(); |
mcimadamore@1347 | 327 | boolean progress = false; |
mcimadamore@1347 | 328 | //scan a defensive copy of the node list - this is because a deferred |
mcimadamore@1347 | 329 | //attribution round can add new nodes to the list |
mcimadamore@1347 | 330 | for (DeferredAttrNode deferredAttrNode : List.from(deferredAttrNodes)) { |
mcimadamore@1347 | 331 | if (!deferredAttrNode.isStuck()) { |
mcimadamore@1347 | 332 | deferredAttrNode.process(); |
mcimadamore@1347 | 333 | deferredAttrNodes.remove(deferredAttrNode); |
mcimadamore@1347 | 334 | progress = true; |
mcimadamore@1347 | 335 | } else { |
mcimadamore@1347 | 336 | stuckVars.addAll(deferredAttrNode.stuckVars); |
mcimadamore@1347 | 337 | } |
mcimadamore@1347 | 338 | } |
mcimadamore@1347 | 339 | if (!progress) { |
mcimadamore@1347 | 340 | //remove all variables that have already been instantiated |
mcimadamore@1347 | 341 | //from the list of stuck variables |
mcimadamore@1347 | 342 | inferenceContext.solveAny(inferenceContext.freeVarsIn(List.from(stuckVars)), types, infer); |
mcimadamore@1347 | 343 | inferenceContext.notifyChange(types); |
mcimadamore@1347 | 344 | } |
mcimadamore@1347 | 345 | } |
mcimadamore@1347 | 346 | } |
mcimadamore@1347 | 347 | |
mcimadamore@1347 | 348 | /** |
mcimadamore@1347 | 349 | * Class representing a deferred attribution node. It keeps track of |
mcimadamore@1347 | 350 | * a deferred type, along with the expected target type information. |
mcimadamore@1347 | 351 | */ |
mcimadamore@1347 | 352 | class DeferredAttrNode implements Infer.InferenceContext.FreeTypeListener { |
mcimadamore@1347 | 353 | |
mcimadamore@1347 | 354 | /** underlying deferred type */ |
mcimadamore@1347 | 355 | DeferredType dt; |
mcimadamore@1347 | 356 | |
mcimadamore@1347 | 357 | /** underlying target type information */ |
mcimadamore@1347 | 358 | ResultInfo resultInfo; |
mcimadamore@1347 | 359 | |
mcimadamore@1347 | 360 | /** list of uninferred inference variables causing this node to be stuck */ |
mcimadamore@1347 | 361 | List<Type> stuckVars; |
mcimadamore@1347 | 362 | |
mcimadamore@1347 | 363 | DeferredAttrNode(DeferredType dt, ResultInfo resultInfo, List<Type> stuckVars) { |
mcimadamore@1347 | 364 | this.dt = dt; |
mcimadamore@1347 | 365 | this.resultInfo = resultInfo; |
mcimadamore@1347 | 366 | this.stuckVars = stuckVars; |
mcimadamore@1347 | 367 | if (!stuckVars.isEmpty()) { |
mcimadamore@1347 | 368 | resultInfo.checkContext.inferenceContext().addFreeTypeListener(stuckVars, this); |
mcimadamore@1347 | 369 | } |
mcimadamore@1347 | 370 | } |
mcimadamore@1347 | 371 | |
mcimadamore@1347 | 372 | @Override |
mcimadamore@1347 | 373 | public void typesInferred(InferenceContext inferenceContext) { |
mcimadamore@1347 | 374 | stuckVars = List.nil(); |
mcimadamore@1347 | 375 | resultInfo = resultInfo.dup(inferenceContext.asInstType(resultInfo.pt, types)); |
mcimadamore@1347 | 376 | } |
mcimadamore@1347 | 377 | |
mcimadamore@1347 | 378 | /** |
mcimadamore@1347 | 379 | * is this node stuck? |
mcimadamore@1347 | 380 | */ |
mcimadamore@1347 | 381 | boolean isStuck() { |
mcimadamore@1347 | 382 | return stuckVars.nonEmpty(); |
mcimadamore@1347 | 383 | } |
mcimadamore@1347 | 384 | |
mcimadamore@1347 | 385 | /** |
mcimadamore@1347 | 386 | * Process a deferred attribution node. |
mcimadamore@1347 | 387 | * Invariant: a stuck node cannot be processed. |
mcimadamore@1347 | 388 | */ |
mcimadamore@1347 | 389 | void process() { |
mcimadamore@1347 | 390 | if (isStuck()) { |
mcimadamore@1347 | 391 | throw new IllegalStateException("Cannot process a stuck deferred node"); |
mcimadamore@1347 | 392 | } |
mcimadamore@1347 | 393 | dt.check(resultInfo); |
mcimadamore@1347 | 394 | } |
mcimadamore@1347 | 395 | } |
mcimadamore@1347 | 396 | } |
mcimadamore@1347 | 397 | |
mcimadamore@1347 | 398 | /** an empty deferred attribution context - all methods throw exceptions */ |
mcimadamore@1347 | 399 | final DeferredAttrContext emptyDeferredAttrContext = |
mcimadamore@1415 | 400 | new DeferredAttrContext(AttrMode.CHECK, null, MethodResolutionPhase.BOX, null) { |
mcimadamore@1347 | 401 | @Override |
mcimadamore@1347 | 402 | void addDeferredAttrNode(DeferredType dt, ResultInfo ri, List<Type> stuckVars) { |
mcimadamore@1347 | 403 | Assert.error("Empty deferred context!"); |
mcimadamore@1347 | 404 | } |
mcimadamore@1347 | 405 | @Override |
mcimadamore@1347 | 406 | void complete() { |
mcimadamore@1347 | 407 | Assert.error("Empty deferred context!"); |
mcimadamore@1347 | 408 | } |
mcimadamore@1347 | 409 | }; |
mcimadamore@1347 | 410 | |
mcimadamore@1347 | 411 | /** |
mcimadamore@1347 | 412 | * Map a list of types possibly containing one or more deferred types |
mcimadamore@1347 | 413 | * into a list of ordinary types. Each deferred type D is mapped into a type T, |
mcimadamore@1347 | 414 | * where T is computed by retrieving the type that has already been |
mcimadamore@1347 | 415 | * computed for D during a previous deferred attribution round of the given kind. |
mcimadamore@1347 | 416 | */ |
mcimadamore@1347 | 417 | class DeferredTypeMap extends Type.Mapping { |
mcimadamore@1347 | 418 | |
mcimadamore@1347 | 419 | DeferredAttrContext deferredAttrContext; |
mcimadamore@1347 | 420 | |
mcimadamore@1347 | 421 | protected DeferredTypeMap(AttrMode mode, Symbol msym, MethodResolutionPhase phase) { |
mcimadamore@1347 | 422 | super(String.format("deferredTypeMap[%s]", mode)); |
mcimadamore@1347 | 423 | this.deferredAttrContext = new DeferredAttrContext(mode, msym, phase, infer.emptyContext); |
mcimadamore@1347 | 424 | } |
mcimadamore@1347 | 425 | |
mcimadamore@1347 | 426 | protected boolean validState(DeferredType dt) { |
mcimadamore@1347 | 427 | return dt.mode != null && |
mcimadamore@1347 | 428 | deferredAttrContext.mode.ordinal() <= dt.mode.ordinal(); |
mcimadamore@1347 | 429 | } |
mcimadamore@1347 | 430 | |
mcimadamore@1347 | 431 | @Override |
mcimadamore@1347 | 432 | public Type apply(Type t) { |
jjg@1374 | 433 | if (!t.hasTag(DEFERRED)) { |
mcimadamore@1347 | 434 | return t.map(this); |
mcimadamore@1347 | 435 | } else { |
mcimadamore@1347 | 436 | DeferredType dt = (DeferredType)t; |
mcimadamore@1347 | 437 | Assert.check(validState(dt)); |
mcimadamore@1347 | 438 | return typeOf(dt); |
mcimadamore@1347 | 439 | } |
mcimadamore@1347 | 440 | } |
mcimadamore@1347 | 441 | |
mcimadamore@1347 | 442 | protected Type typeOf(DeferredType dt) { |
mcimadamore@1347 | 443 | switch (deferredAttrContext.mode) { |
mcimadamore@1347 | 444 | case CHECK: |
mcimadamore@1347 | 445 | return dt.tree.type == null ? Type.noType : dt.tree.type; |
mcimadamore@1347 | 446 | case SPECULATIVE: |
mcimadamore@1347 | 447 | return dt.speculativeType(deferredAttrContext.msym, deferredAttrContext.phase); |
mcimadamore@1347 | 448 | } |
mcimadamore@1347 | 449 | Assert.error(); |
mcimadamore@1347 | 450 | return null; |
mcimadamore@1347 | 451 | } |
mcimadamore@1347 | 452 | } |
mcimadamore@1347 | 453 | |
mcimadamore@1347 | 454 | /** |
mcimadamore@1347 | 455 | * Specialized recovery deferred mapping. |
mcimadamore@1347 | 456 | * Each deferred type D is mapped into a type T, where T is computed either by |
mcimadamore@1347 | 457 | * (i) retrieving the type that has already been computed for D during a previous |
mcimadamore@1347 | 458 | * attribution round (as before), or (ii) by synthesizing a new type R for D |
mcimadamore@1347 | 459 | * (the latter step is useful in a recovery scenario). |
mcimadamore@1347 | 460 | */ |
mcimadamore@1347 | 461 | public class RecoveryDeferredTypeMap extends DeferredTypeMap { |
mcimadamore@1347 | 462 | |
mcimadamore@1347 | 463 | public RecoveryDeferredTypeMap(AttrMode mode, Symbol msym, MethodResolutionPhase phase) { |
mcimadamore@1415 | 464 | super(mode, msym, phase != null ? phase : MethodResolutionPhase.BOX); |
mcimadamore@1347 | 465 | } |
mcimadamore@1347 | 466 | |
mcimadamore@1347 | 467 | @Override |
mcimadamore@1347 | 468 | protected Type typeOf(DeferredType dt) { |
mcimadamore@1347 | 469 | Type owntype = super.typeOf(dt); |
mcimadamore@1415 | 470 | return owntype == Type.noType ? |
mcimadamore@1347 | 471 | recover(dt) : owntype; |
mcimadamore@1347 | 472 | } |
mcimadamore@1347 | 473 | |
mcimadamore@1347 | 474 | @Override |
mcimadamore@1347 | 475 | protected boolean validState(DeferredType dt) { |
mcimadamore@1347 | 476 | return true; |
mcimadamore@1347 | 477 | } |
mcimadamore@1347 | 478 | |
mcimadamore@1347 | 479 | /** |
mcimadamore@1347 | 480 | * Synthesize a type for a deferred type that hasn't been previously |
mcimadamore@1347 | 481 | * reduced to an ordinary type. Functional deferred types and conditionals |
mcimadamore@1347 | 482 | * are mapped to themselves, in order to have a richer diagnostic |
mcimadamore@1347 | 483 | * representation. Remaining deferred types are attributed using |
mcimadamore@1347 | 484 | * a default expected type (j.l.Object). |
mcimadamore@1347 | 485 | */ |
mcimadamore@1347 | 486 | private Type recover(DeferredType dt) { |
mcimadamore@1348 | 487 | dt.check(attr.new RecoveryInfo(deferredAttrContext)); |
mcimadamore@1415 | 488 | return super.apply(dt); |
mcimadamore@1347 | 489 | } |
mcimadamore@1347 | 490 | } |
mcimadamore@1347 | 491 | |
mcimadamore@1347 | 492 | /** |
mcimadamore@1347 | 493 | * Retrieves the list of inference variables that need to be inferred before |
mcimadamore@1347 | 494 | * an AST node can be type-checked |
mcimadamore@1347 | 495 | */ |
mcimadamore@1347 | 496 | @SuppressWarnings("fallthrough") |
mcimadamore@1415 | 497 | List<Type> stuckVars(JCTree tree, Env<AttrContext> env, ResultInfo resultInfo) { |
mcimadamore@1415 | 498 | if (resultInfo.pt.hasTag(NONE) || resultInfo.pt.isErroneous()) { |
mcimadamore@1348 | 499 | return List.nil(); |
mcimadamore@1348 | 500 | } else { |
mcimadamore@1415 | 501 | StuckChecker sc = new StuckChecker(resultInfo, env); |
mcimadamore@1348 | 502 | sc.scan(tree); |
mcimadamore@1348 | 503 | return List.from(sc.stuckVars); |
mcimadamore@1348 | 504 | } |
mcimadamore@1348 | 505 | } |
mcimadamore@1348 | 506 | |
mcimadamore@1348 | 507 | /** |
mcimadamore@1348 | 508 | * This visitor is used to check that structural expressions conform |
mcimadamore@1348 | 509 | * to their target - this step is required as inference could end up |
mcimadamore@1348 | 510 | * inferring types that make some of the nested expressions incompatible |
mcimadamore@1348 | 511 | * with their corresponding instantiated target |
mcimadamore@1348 | 512 | */ |
mcimadamore@1348 | 513 | class StuckChecker extends TreeScanner { |
mcimadamore@1348 | 514 | |
mcimadamore@1348 | 515 | Type pt; |
mcimadamore@1348 | 516 | Filter<JCTree> treeFilter; |
mcimadamore@1348 | 517 | Infer.InferenceContext inferenceContext; |
mcimadamore@1415 | 518 | Set<Type> stuckVars = new LinkedHashSet<Type>(); |
mcimadamore@1415 | 519 | Env<AttrContext> env; |
mcimadamore@1348 | 520 | |
mcimadamore@1348 | 521 | final Filter<JCTree> argsFilter = new Filter<JCTree>() { |
mcimadamore@1348 | 522 | public boolean accepts(JCTree t) { |
mcimadamore@1348 | 523 | switch (t.getTag()) { |
mcimadamore@1348 | 524 | case CONDEXPR: |
mcimadamore@1348 | 525 | case LAMBDA: |
mcimadamore@1348 | 526 | case PARENS: |
mcimadamore@1348 | 527 | case REFERENCE: |
mcimadamore@1348 | 528 | return true; |
mcimadamore@1348 | 529 | default: |
mcimadamore@1348 | 530 | return false; |
mcimadamore@1348 | 531 | } |
mcimadamore@1348 | 532 | } |
mcimadamore@1348 | 533 | }; |
mcimadamore@1348 | 534 | |
mcimadamore@1348 | 535 | final Filter<JCTree> lambdaBodyFilter = new Filter<JCTree>() { |
mcimadamore@1348 | 536 | public boolean accepts(JCTree t) { |
mcimadamore@1348 | 537 | switch (t.getTag()) { |
mcimadamore@1348 | 538 | case BLOCK: case CASE: case CATCH: case DOLOOP: |
mcimadamore@1348 | 539 | case FOREACHLOOP: case FORLOOP: case RETURN: |
mcimadamore@1348 | 540 | case SYNCHRONIZED: case SWITCH: case TRY: case WHILELOOP: |
mcimadamore@1348 | 541 | return true; |
mcimadamore@1348 | 542 | default: |
mcimadamore@1348 | 543 | return false; |
mcimadamore@1348 | 544 | } |
mcimadamore@1348 | 545 | } |
mcimadamore@1348 | 546 | }; |
mcimadamore@1348 | 547 | |
mcimadamore@1415 | 548 | StuckChecker(ResultInfo resultInfo, Env<AttrContext> env) { |
mcimadamore@1348 | 549 | this.pt = resultInfo.pt; |
mcimadamore@1348 | 550 | this.inferenceContext = resultInfo.checkContext.inferenceContext(); |
mcimadamore@1348 | 551 | this.treeFilter = argsFilter; |
mcimadamore@1415 | 552 | this.env = env; |
mcimadamore@1348 | 553 | } |
mcimadamore@1348 | 554 | |
mcimadamore@1348 | 555 | @Override |
mcimadamore@1348 | 556 | public void scan(JCTree tree) { |
mcimadamore@1348 | 557 | if (tree != null && treeFilter.accepts(tree)) { |
mcimadamore@1348 | 558 | super.scan(tree); |
mcimadamore@1348 | 559 | } |
mcimadamore@1348 | 560 | } |
mcimadamore@1348 | 561 | |
mcimadamore@1348 | 562 | @Override |
mcimadamore@1348 | 563 | public void visitLambda(JCLambda tree) { |
mcimadamore@1348 | 564 | Type prevPt = pt; |
mcimadamore@1348 | 565 | Filter<JCTree> prevFilter = treeFilter; |
mcimadamore@1348 | 566 | try { |
mcimadamore@1348 | 567 | if (inferenceContext.inferenceVars().contains(pt)) { |
mcimadamore@1348 | 568 | stuckVars.add(pt); |
mcimadamore@1348 | 569 | } |
mcimadamore@1348 | 570 | if (!types.isFunctionalInterface(pt.tsym)) { |
mcimadamore@1348 | 571 | return; |
mcimadamore@1348 | 572 | } |
mcimadamore@1348 | 573 | Type descType = types.findDescriptorType(pt); |
mcimadamore@1348 | 574 | List<Type> freeArgVars = inferenceContext.freeVarsIn(descType.getParameterTypes()); |
mcimadamore@1348 | 575 | if (!TreeInfo.isExplicitLambda(tree) && |
mcimadamore@1348 | 576 | freeArgVars.nonEmpty()) { |
mcimadamore@1348 | 577 | stuckVars.addAll(freeArgVars); |
mcimadamore@1348 | 578 | } |
mcimadamore@1348 | 579 | pt = descType.getReturnType(); |
mcimadamore@1348 | 580 | if (tree.getBodyKind() == JCTree.JCLambda.BodyKind.EXPRESSION) { |
mcimadamore@1348 | 581 | scan(tree.getBody()); |
mcimadamore@1348 | 582 | } else { |
mcimadamore@1348 | 583 | treeFilter = lambdaBodyFilter; |
mcimadamore@1348 | 584 | super.visitLambda(tree); |
mcimadamore@1348 | 585 | } |
mcimadamore@1348 | 586 | } finally { |
mcimadamore@1348 | 587 | pt = prevPt; |
mcimadamore@1348 | 588 | treeFilter = prevFilter; |
mcimadamore@1348 | 589 | } |
mcimadamore@1348 | 590 | } |
mcimadamore@1348 | 591 | |
mcimadamore@1348 | 592 | @Override |
mcimadamore@1348 | 593 | public void visitReference(JCMemberReference tree) { |
mcimadamore@1348 | 594 | scan(tree.expr); |
mcimadamore@1348 | 595 | if (inferenceContext.inferenceVars().contains(pt)) { |
mcimadamore@1348 | 596 | stuckVars.add(pt); |
mcimadamore@1348 | 597 | return; |
mcimadamore@1348 | 598 | } |
mcimadamore@1348 | 599 | if (!types.isFunctionalInterface(pt.tsym)) { |
mcimadamore@1348 | 600 | return; |
mcimadamore@1348 | 601 | } |
mcimadamore@1415 | 602 | |
mcimadamore@1348 | 603 | Type descType = types.findDescriptorType(pt); |
mcimadamore@1348 | 604 | List<Type> freeArgVars = inferenceContext.freeVarsIn(descType.getParameterTypes()); |
mcimadamore@1348 | 605 | stuckVars.addAll(freeArgVars); |
mcimadamore@1348 | 606 | } |
mcimadamore@1348 | 607 | |
mcimadamore@1348 | 608 | @Override |
mcimadamore@1348 | 609 | public void visitReturn(JCReturn tree) { |
mcimadamore@1348 | 610 | Filter<JCTree> prevFilter = treeFilter; |
mcimadamore@1348 | 611 | try { |
mcimadamore@1348 | 612 | treeFilter = argsFilter; |
mcimadamore@1348 | 613 | if (tree.expr != null) { |
mcimadamore@1348 | 614 | scan(tree.expr); |
mcimadamore@1348 | 615 | } |
mcimadamore@1348 | 616 | } finally { |
mcimadamore@1348 | 617 | treeFilter = prevFilter; |
mcimadamore@1348 | 618 | } |
mcimadamore@1347 | 619 | } |
mcimadamore@1347 | 620 | } |
mcimadamore@1347 | 621 | } |