src/share/classes/com/sun/tools/javac/comp/DeferredAttr.java

Sun, 17 Feb 2013 16:44:55 -0500

author
dholmes
date
Sun, 17 Feb 2013 16:44:55 -0500
changeset 1571
af8417e590f4
parent 1562
2154ed9ff6c8
child 1581
4ff468de829d
permissions
-rw-r--r--

Merge

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@1481 41 import java.util.EnumSet;
mcimadamore@1415 42 import java.util.LinkedHashSet;
mcimadamore@1347 43 import java.util.Map;
mcimadamore@1347 44 import java.util.Queue;
mcimadamore@1347 45 import java.util.Set;
mcimadamore@1347 46 import java.util.WeakHashMap;
mcimadamore@1347 47
mcimadamore@1415 48 import static com.sun.tools.javac.code.TypeTag.*;
mcimadamore@1347 49 import static com.sun.tools.javac.tree.JCTree.Tag.*;
mcimadamore@1347 50
mcimadamore@1347 51 /**
mcimadamore@1347 52 * This is an helper class that is used to perform deferred type-analysis.
mcimadamore@1347 53 * Each time a poly expression occurs in argument position, javac attributes it
mcimadamore@1347 54 * with a temporary 'deferred type' that is checked (possibly multiple times)
mcimadamore@1347 55 * against an expected formal type.
mcimadamore@1347 56 *
mcimadamore@1347 57 * <p><b>This is NOT part of any supported API.
mcimadamore@1347 58 * If you write code that depends on this, you do so at your own risk.
mcimadamore@1347 59 * This code and its internal interfaces are subject to change or
mcimadamore@1347 60 * deletion without notice.</b>
mcimadamore@1347 61 */
mcimadamore@1347 62 public class DeferredAttr extends JCTree.Visitor {
mcimadamore@1347 63 protected static final Context.Key<DeferredAttr> deferredAttrKey =
mcimadamore@1347 64 new Context.Key<DeferredAttr>();
mcimadamore@1347 65
mcimadamore@1347 66 final Attr attr;
mcimadamore@1347 67 final Check chk;
mcimadamore@1510 68 final JCDiagnostic.Factory diags;
mcimadamore@1347 69 final Enter enter;
mcimadamore@1347 70 final Infer infer;
mcimadamore@1347 71 final Log log;
mcimadamore@1347 72 final Symtab syms;
mcimadamore@1347 73 final TreeMaker make;
mcimadamore@1347 74 final Types types;
mcimadamore@1347 75
mcimadamore@1347 76 public static DeferredAttr instance(Context context) {
mcimadamore@1347 77 DeferredAttr instance = context.get(deferredAttrKey);
mcimadamore@1347 78 if (instance == null)
mcimadamore@1347 79 instance = new DeferredAttr(context);
mcimadamore@1347 80 return instance;
mcimadamore@1347 81 }
mcimadamore@1347 82
mcimadamore@1347 83 protected DeferredAttr(Context context) {
mcimadamore@1347 84 context.put(deferredAttrKey, this);
mcimadamore@1347 85 attr = Attr.instance(context);
mcimadamore@1347 86 chk = Check.instance(context);
mcimadamore@1510 87 diags = JCDiagnostic.Factory.instance(context);
mcimadamore@1347 88 enter = Enter.instance(context);
mcimadamore@1347 89 infer = Infer.instance(context);
mcimadamore@1347 90 log = Log.instance(context);
mcimadamore@1347 91 syms = Symtab.instance(context);
mcimadamore@1347 92 make = TreeMaker.instance(context);
mcimadamore@1347 93 types = Types.instance(context);
mcimadamore@1510 94 Names names = Names.instance(context);
mcimadamore@1510 95 stuckTree = make.Ident(names.empty).setType(Type.noType);
mcimadamore@1347 96 }
mcimadamore@1347 97
mcimadamore@1510 98 /** shared tree for stuck expressions */
mcimadamore@1510 99 final JCTree stuckTree;
mcimadamore@1510 100
mcimadamore@1347 101 /**
mcimadamore@1347 102 * This type represents a deferred type. A deferred type starts off with
mcimadamore@1347 103 * no information on the underlying expression type. Such info needs to be
mcimadamore@1347 104 * discovered through type-checking the deferred type against a target-type.
mcimadamore@1347 105 * Every deferred type keeps a pointer to the AST node from which it originated.
mcimadamore@1347 106 */
mcimadamore@1347 107 public class DeferredType extends Type {
mcimadamore@1347 108
mcimadamore@1347 109 public JCExpression tree;
mcimadamore@1347 110 Env<AttrContext> env;
mcimadamore@1347 111 AttrMode mode;
mcimadamore@1347 112 SpeculativeCache speculativeCache;
mcimadamore@1347 113
mcimadamore@1347 114 DeferredType(JCExpression tree, Env<AttrContext> env) {
mcimadamore@1347 115 super(DEFERRED, null);
mcimadamore@1347 116 this.tree = tree;
mcimadamore@1347 117 this.env = env.dup(tree, env.info.dup());
mcimadamore@1347 118 this.speculativeCache = new SpeculativeCache();
mcimadamore@1347 119 }
mcimadamore@1347 120
mcimadamore@1347 121 /**
mcimadamore@1347 122 * A speculative cache is used to keep track of all overload resolution rounds
mcimadamore@1347 123 * that triggered speculative attribution on a given deferred type. Each entry
mcimadamore@1347 124 * stores a pointer to the speculative tree and the resolution phase in which the entry
mcimadamore@1347 125 * has been added.
mcimadamore@1347 126 */
mcimadamore@1347 127 class SpeculativeCache {
mcimadamore@1347 128
mcimadamore@1347 129 private Map<Symbol, List<Entry>> cache =
mcimadamore@1347 130 new WeakHashMap<Symbol, List<Entry>>();
mcimadamore@1347 131
mcimadamore@1347 132 class Entry {
mcimadamore@1347 133 JCTree speculativeTree;
mcimadamore@1347 134 Resolve.MethodResolutionPhase phase;
mcimadamore@1347 135
mcimadamore@1347 136 public Entry(JCTree speculativeTree, MethodResolutionPhase phase) {
mcimadamore@1347 137 this.speculativeTree = speculativeTree;
mcimadamore@1347 138 this.phase = phase;
mcimadamore@1347 139 }
mcimadamore@1347 140
mcimadamore@1347 141 boolean matches(Resolve.MethodResolutionPhase phase) {
mcimadamore@1347 142 return this.phase == phase;
mcimadamore@1347 143 }
mcimadamore@1347 144 }
mcimadamore@1347 145
mcimadamore@1347 146 /**
mcimadamore@1347 147 * Retrieve a speculative cache entry corresponding to given symbol
mcimadamore@1347 148 * and resolution phase
mcimadamore@1347 149 */
mcimadamore@1347 150 Entry get(Symbol msym, MethodResolutionPhase phase) {
mcimadamore@1347 151 List<Entry> entries = cache.get(msym);
mcimadamore@1347 152 if (entries == null) return null;
mcimadamore@1347 153 for (Entry e : entries) {
mcimadamore@1347 154 if (e.matches(phase)) return e;
mcimadamore@1347 155 }
mcimadamore@1347 156 return null;
mcimadamore@1347 157 }
mcimadamore@1347 158
mcimadamore@1347 159 /**
mcimadamore@1347 160 * Stores a speculative cache entry corresponding to given symbol
mcimadamore@1347 161 * and resolution phase
mcimadamore@1347 162 */
mcimadamore@1347 163 void put(Symbol msym, JCTree speculativeTree, MethodResolutionPhase phase) {
mcimadamore@1347 164 List<Entry> entries = cache.get(msym);
mcimadamore@1347 165 if (entries == null) {
mcimadamore@1347 166 entries = List.nil();
mcimadamore@1347 167 }
mcimadamore@1347 168 cache.put(msym, entries.prepend(new Entry(speculativeTree, phase)));
mcimadamore@1347 169 }
mcimadamore@1347 170 }
mcimadamore@1347 171
mcimadamore@1347 172 /**
mcimadamore@1347 173 * Get the type that has been computed during a speculative attribution round
mcimadamore@1347 174 */
mcimadamore@1347 175 Type speculativeType(Symbol msym, MethodResolutionPhase phase) {
mcimadamore@1347 176 SpeculativeCache.Entry e = speculativeCache.get(msym, phase);
mcimadamore@1347 177 return e != null ? e.speculativeTree.type : Type.noType;
mcimadamore@1347 178 }
mcimadamore@1347 179
mcimadamore@1347 180 /**
mcimadamore@1347 181 * Check a deferred type against a potential target-type. Depending on
mcimadamore@1347 182 * the current attribution mode, a normal vs. speculative attribution
mcimadamore@1347 183 * round is performed on the underlying AST node. There can be only one
mcimadamore@1347 184 * speculative round for a given target method symbol; moreover, a normal
mcimadamore@1347 185 * attribution round must follow one or more speculative rounds.
mcimadamore@1347 186 */
mcimadamore@1347 187 Type check(ResultInfo resultInfo) {
mcimadamore@1481 188 return check(resultInfo, stuckVars(tree, env, resultInfo), basicCompleter);
mcimadamore@1481 189 }
mcimadamore@1481 190
mcimadamore@1481 191 Type check(ResultInfo resultInfo, List<Type> stuckVars, DeferredTypeCompleter deferredTypeCompleter) {
mcimadamore@1347 192 DeferredAttrContext deferredAttrContext =
mcimadamore@1347 193 resultInfo.checkContext.deferredAttrContext();
mcimadamore@1347 194 Assert.check(deferredAttrContext != emptyDeferredAttrContext);
mcimadamore@1347 195 if (stuckVars.nonEmpty()) {
mcimadamore@1347 196 deferredAttrContext.addDeferredAttrNode(this, resultInfo, stuckVars);
mcimadamore@1347 197 return Type.noType;
mcimadamore@1347 198 } else {
mcimadamore@1347 199 try {
mcimadamore@1481 200 return deferredTypeCompleter.complete(this, resultInfo, deferredAttrContext);
mcimadamore@1347 201 } finally {
mcimadamore@1347 202 mode = deferredAttrContext.mode;
mcimadamore@1347 203 }
mcimadamore@1347 204 }
mcimadamore@1347 205 }
mcimadamore@1347 206 }
mcimadamore@1347 207
mcimadamore@1347 208 /**
mcimadamore@1481 209 * A completer for deferred types. Defines an entry point for type-checking
mcimadamore@1481 210 * a deferred type.
mcimadamore@1481 211 */
mcimadamore@1481 212 interface DeferredTypeCompleter {
mcimadamore@1481 213 /**
mcimadamore@1481 214 * Entry point for type-checking a deferred type. Depending on the
mcimadamore@1481 215 * circumstances, type-checking could amount to full attribution
mcimadamore@1481 216 * or partial structural check (aka potential applicability).
mcimadamore@1481 217 */
mcimadamore@1481 218 Type complete(DeferredType dt, ResultInfo resultInfo, DeferredAttrContext deferredAttrContext);
mcimadamore@1481 219 }
mcimadamore@1481 220
mcimadamore@1481 221 /**
mcimadamore@1481 222 * A basic completer for deferred types. This completer type-checks a deferred type
mcimadamore@1481 223 * using attribution; depending on the attribution mode, this could be either standard
mcimadamore@1481 224 * or speculative attribution.
mcimadamore@1481 225 */
mcimadamore@1481 226 DeferredTypeCompleter basicCompleter = new DeferredTypeCompleter() {
mcimadamore@1481 227 public Type complete(DeferredType dt, ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) {
mcimadamore@1481 228 switch (deferredAttrContext.mode) {
mcimadamore@1481 229 case SPECULATIVE:
mcimadamore@1481 230 Assert.check(dt.mode == null ||
mcimadamore@1481 231 (dt.mode == AttrMode.SPECULATIVE &&
mcimadamore@1481 232 dt.speculativeType(deferredAttrContext.msym, deferredAttrContext.phase).hasTag(NONE)));
mcimadamore@1481 233 JCTree speculativeTree = attribSpeculative(dt.tree, dt.env, resultInfo);
mcimadamore@1481 234 dt.speculativeCache.put(deferredAttrContext.msym, speculativeTree, deferredAttrContext.phase);
mcimadamore@1481 235 return speculativeTree.type;
mcimadamore@1481 236 case CHECK:
mcimadamore@1562 237 Assert.check(dt.mode != null);
mcimadamore@1481 238 return attr.attribTree(dt.tree, dt.env, resultInfo);
mcimadamore@1481 239 }
mcimadamore@1481 240 Assert.error();
mcimadamore@1481 241 return null;
mcimadamore@1481 242 }
mcimadamore@1481 243 };
mcimadamore@1481 244
mcimadamore@1562 245 DeferredTypeCompleter dummyCompleter = new DeferredTypeCompleter() {
mcimadamore@1562 246 public Type complete(DeferredType dt, ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) {
mcimadamore@1562 247 Assert.check(deferredAttrContext.mode == AttrMode.CHECK);
mcimadamore@1562 248 return dt.tree.type = Type.noType;
mcimadamore@1562 249 }
mcimadamore@1562 250 };
mcimadamore@1562 251
mcimadamore@1481 252 /**
mcimadamore@1347 253 * The 'mode' in which the deferred type is to be type-checked
mcimadamore@1347 254 */
mcimadamore@1347 255 public enum AttrMode {
mcimadamore@1347 256 /**
mcimadamore@1347 257 * A speculative type-checking round is used during overload resolution
mcimadamore@1347 258 * mainly to generate constraints on inference variables. Side-effects
mcimadamore@1347 259 * arising from type-checking the expression associated with the deferred
mcimadamore@1347 260 * type are reversed after the speculative round finishes. This means the
mcimadamore@1347 261 * expression tree will be left in a blank state.
mcimadamore@1347 262 */
mcimadamore@1347 263 SPECULATIVE,
mcimadamore@1347 264 /**
mcimadamore@1347 265 * This is the plain type-checking mode. Produces side-effects on the underlying AST node
mcimadamore@1347 266 */
mcimadamore@1347 267 CHECK;
mcimadamore@1347 268 }
mcimadamore@1347 269
mcimadamore@1347 270 /**
mcimadamore@1347 271 * Routine that performs speculative type-checking; the input AST node is
mcimadamore@1347 272 * cloned (to avoid side-effects cause by Attr) and compiler state is
mcimadamore@1347 273 * restored after type-checking. All diagnostics (but critical ones) are
mcimadamore@1347 274 * disabled during speculative type-checking.
mcimadamore@1347 275 */
mcimadamore@1347 276 JCTree attribSpeculative(JCTree tree, Env<AttrContext> env, ResultInfo resultInfo) {
mcimadamore@1347 277 JCTree newTree = new TreeCopier<Object>(make).copy(tree);
mcimadamore@1347 278 Env<AttrContext> speculativeEnv = env.dup(newTree, env.info.dup(env.info.scope.dupUnshared()));
mcimadamore@1347 279 speculativeEnv.info.scope.owner = env.info.scope.owner;
mcimadamore@1347 280 final JavaFileObject currentSource = log.currentSourceFile();
jjg@1406 281 Log.DeferredDiagnosticHandler deferredDiagnosticHandler =
jjg@1406 282 new Log.DeferredDiagnosticHandler(log, new Filter<JCDiagnostic>() {
jjg@1406 283 public boolean accepts(JCDiagnostic t) {
jjg@1406 284 return t.getDiagnosticSource().getFile().equals(currentSource);
jjg@1406 285 }
jjg@1406 286 });
mcimadamore@1347 287 try {
mcimadamore@1347 288 attr.attribTree(newTree, speculativeEnv, resultInfo);
mcimadamore@1347 289 unenterScanner.scan(newTree);
mcimadamore@1347 290 return newTree;
mcimadamore@1347 291 } catch (Abort ex) {
mcimadamore@1347 292 //if some very bad condition occurred during deferred attribution
mcimadamore@1347 293 //we should dump all errors before killing javac
jjg@1406 294 deferredDiagnosticHandler.reportDeferredDiagnostics();
mcimadamore@1347 295 throw ex;
mcimadamore@1347 296 } finally {
mcimadamore@1347 297 unenterScanner.scan(newTree);
jjg@1406 298 log.popDiagnosticHandler(deferredDiagnosticHandler);
mcimadamore@1347 299 }
mcimadamore@1347 300 }
mcimadamore@1347 301 //where
mcimadamore@1347 302 protected TreeScanner unenterScanner = new TreeScanner() {
mcimadamore@1347 303 @Override
mcimadamore@1347 304 public void visitClassDef(JCClassDecl tree) {
mcimadamore@1347 305 ClassSymbol csym = tree.sym;
mcimadamore@1415 306 //if something went wrong during method applicability check
mcimadamore@1415 307 //it is possible that nested expressions inside argument expression
mcimadamore@1415 308 //are left unchecked - in such cases there's nothing to clean up.
mcimadamore@1415 309 if (csym == null) return;
mcimadamore@1347 310 enter.typeEnvs.remove(csym);
mcimadamore@1347 311 chk.compiled.remove(csym.flatname);
mcimadamore@1347 312 syms.classes.remove(csym.flatname);
mcimadamore@1347 313 super.visitClassDef(tree);
mcimadamore@1347 314 }
mcimadamore@1347 315 };
mcimadamore@1347 316
mcimadamore@1347 317 /**
mcimadamore@1347 318 * A deferred context is created on each method check. A deferred context is
mcimadamore@1347 319 * used to keep track of information associated with the method check, such as
mcimadamore@1347 320 * the symbol of the method being checked, the overload resolution phase,
mcimadamore@1347 321 * the kind of attribution mode to be applied to deferred types and so forth.
mcimadamore@1347 322 * As deferred types are processed (by the method check routine) stuck AST nodes
mcimadamore@1347 323 * are added (as new deferred attribution nodes) to this context. The complete()
mcimadamore@1347 324 * routine makes sure that all pending nodes are properly processed, by
mcimadamore@1347 325 * progressively instantiating all inference variables on which one or more
mcimadamore@1347 326 * deferred attribution node is stuck.
mcimadamore@1347 327 */
mcimadamore@1347 328 class DeferredAttrContext {
mcimadamore@1347 329
mcimadamore@1347 330 /** attribution mode */
mcimadamore@1347 331 final AttrMode mode;
mcimadamore@1347 332
mcimadamore@1347 333 /** symbol of the method being checked */
mcimadamore@1347 334 final Symbol msym;
mcimadamore@1347 335
mcimadamore@1347 336 /** method resolution step */
mcimadamore@1347 337 final Resolve.MethodResolutionPhase phase;
mcimadamore@1347 338
mcimadamore@1347 339 /** inference context */
mcimadamore@1347 340 final InferenceContext inferenceContext;
mcimadamore@1347 341
mcimadamore@1551 342 /** parent deferred context */
mcimadamore@1551 343 final DeferredAttrContext parent;
mcimadamore@1551 344
mcimadamore@1551 345 /** Warner object to report warnings */
mcimadamore@1551 346 final Warner warn;
mcimadamore@1551 347
mcimadamore@1347 348 /** list of deferred attribution nodes to be processed */
mcimadamore@1347 349 ArrayList<DeferredAttrNode> deferredAttrNodes = new ArrayList<DeferredAttrNode>();
mcimadamore@1347 350
mcimadamore@1551 351 DeferredAttrContext(AttrMode mode, Symbol msym, MethodResolutionPhase phase,
mcimadamore@1551 352 InferenceContext inferenceContext, DeferredAttrContext parent, Warner warn) {
mcimadamore@1347 353 this.mode = mode;
mcimadamore@1347 354 this.msym = msym;
mcimadamore@1347 355 this.phase = phase;
mcimadamore@1551 356 this.parent = parent;
mcimadamore@1551 357 this.warn = warn;
mcimadamore@1347 358 this.inferenceContext = inferenceContext;
mcimadamore@1347 359 }
mcimadamore@1347 360
mcimadamore@1347 361 /**
mcimadamore@1347 362 * Adds a node to the list of deferred attribution nodes - used by Resolve.rawCheckArgumentsApplicable
mcimadamore@1347 363 * Nodes added this way act as 'roots' for the out-of-order method checking process.
mcimadamore@1347 364 */
mcimadamore@1347 365 void addDeferredAttrNode(final DeferredType dt, ResultInfo resultInfo, List<Type> stuckVars) {
mcimadamore@1347 366 deferredAttrNodes.add(new DeferredAttrNode(dt, resultInfo, stuckVars));
mcimadamore@1347 367 }
mcimadamore@1347 368
mcimadamore@1347 369 /**
mcimadamore@1347 370 * Incrementally process all nodes, by skipping 'stuck' nodes and attributing
mcimadamore@1347 371 * 'unstuck' ones. If at any point no progress can be made (no 'unstuck' nodes)
mcimadamore@1347 372 * some inference variable might get eagerly instantiated so that all nodes
mcimadamore@1347 373 * can be type-checked.
mcimadamore@1347 374 */
mcimadamore@1347 375 void complete() {
mcimadamore@1347 376 while (!deferredAttrNodes.isEmpty()) {
mcimadamore@1415 377 Set<Type> stuckVars = new LinkedHashSet<Type>();
mcimadamore@1347 378 boolean progress = false;
mcimadamore@1347 379 //scan a defensive copy of the node list - this is because a deferred
mcimadamore@1347 380 //attribution round can add new nodes to the list
mcimadamore@1347 381 for (DeferredAttrNode deferredAttrNode : List.from(deferredAttrNodes)) {
mcimadamore@1551 382 if (!deferredAttrNode.process(this)) {
mcimadamore@1510 383 stuckVars.addAll(deferredAttrNode.stuckVars);
mcimadamore@1510 384 } else {
mcimadamore@1347 385 deferredAttrNodes.remove(deferredAttrNode);
mcimadamore@1347 386 progress = true;
mcimadamore@1347 387 }
mcimadamore@1347 388 }
mcimadamore@1347 389 if (!progress) {
mcimadamore@1347 390 //remove all variables that have already been instantiated
mcimadamore@1347 391 //from the list of stuck variables
mcimadamore@1562 392 inferenceContext.solveAny(List.from(stuckVars), warn);
mcimadamore@1550 393 inferenceContext.notifyChange();
mcimadamore@1347 394 }
mcimadamore@1347 395 }
mcimadamore@1347 396 }
mcimadamore@1551 397 }
mcimadamore@1551 398
mcimadamore@1551 399 /**
mcimadamore@1551 400 * Class representing a deferred attribution node. It keeps track of
mcimadamore@1551 401 * a deferred type, along with the expected target type information.
mcimadamore@1551 402 */
mcimadamore@1551 403 class DeferredAttrNode implements Infer.FreeTypeListener {
mcimadamore@1551 404
mcimadamore@1551 405 /** underlying deferred type */
mcimadamore@1551 406 DeferredType dt;
mcimadamore@1551 407
mcimadamore@1551 408 /** underlying target type information */
mcimadamore@1551 409 ResultInfo resultInfo;
mcimadamore@1551 410
mcimadamore@1551 411 /** list of uninferred inference variables causing this node to be stuck */
mcimadamore@1551 412 List<Type> stuckVars;
mcimadamore@1551 413
mcimadamore@1551 414 DeferredAttrNode(DeferredType dt, ResultInfo resultInfo, List<Type> stuckVars) {
mcimadamore@1551 415 this.dt = dt;
mcimadamore@1551 416 this.resultInfo = resultInfo;
mcimadamore@1551 417 this.stuckVars = stuckVars;
mcimadamore@1551 418 if (!stuckVars.isEmpty()) {
mcimadamore@1551 419 resultInfo.checkContext.inferenceContext().addFreeTypeListener(stuckVars, this);
mcimadamore@1551 420 }
mcimadamore@1551 421 }
mcimadamore@1551 422
mcimadamore@1551 423 @Override
mcimadamore@1551 424 public void typesInferred(InferenceContext inferenceContext) {
mcimadamore@1551 425 stuckVars = List.nil();
mcimadamore@1551 426 resultInfo = resultInfo.dup(inferenceContext.asInstType(resultInfo.pt));
mcimadamore@1551 427 }
mcimadamore@1347 428
mcimadamore@1347 429 /**
mcimadamore@1551 430 * Process a deferred attribution node.
mcimadamore@1551 431 * Invariant: a stuck node cannot be processed.
mcimadamore@1347 432 */
mcimadamore@1551 433 @SuppressWarnings("fallthrough")
mcimadamore@1551 434 boolean process(DeferredAttrContext deferredAttrContext) {
mcimadamore@1551 435 switch (deferredAttrContext.mode) {
mcimadamore@1551 436 case SPECULATIVE:
mcimadamore@1551 437 dt.check(resultInfo, List.<Type>nil(), new StructuralStuckChecker());
mcimadamore@1551 438 return true;
mcimadamore@1551 439 case CHECK:
mcimadamore@1551 440 if (stuckVars.nonEmpty()) {
mcimadamore@1562 441 //stuck expression - see if we can propagate
mcimadamore@1562 442 if (deferredAttrContext.parent != emptyDeferredAttrContext &&
mcimadamore@1562 443 Type.containsAny(deferredAttrContext.parent.inferenceContext.inferencevars, List.from(stuckVars))) {
mcimadamore@1562 444 deferredAttrContext.parent.deferredAttrNodes.add(this);
mcimadamore@1562 445 dt.check(resultInfo, List.<Type>nil(), dummyCompleter);
mcimadamore@1562 446 return true;
mcimadamore@1562 447 } else {
mcimadamore@1562 448 return false;
mcimadamore@1562 449 }
mcimadamore@1551 450 } else {
mcimadamore@1551 451 dt.check(resultInfo, stuckVars, basicCompleter);
mcimadamore@1551 452 return true;
mcimadamore@1551 453 }
mcimadamore@1551 454 default:
mcimadamore@1551 455 throw new AssertionError("Bad mode");
mcimadamore@1551 456 }
mcimadamore@1551 457 }
mcimadamore@1347 458
mcimadamore@1551 459 /**
mcimadamore@1551 460 * Structural checker for stuck expressions
mcimadamore@1551 461 */
mcimadamore@1551 462 class StructuralStuckChecker extends TreeScanner implements DeferredTypeCompleter {
mcimadamore@1347 463
mcimadamore@1347 464 ResultInfo resultInfo;
mcimadamore@1551 465 InferenceContext inferenceContext;
mcimadamore@1347 466
mcimadamore@1551 467 public Type complete(DeferredType dt, ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) {
mcimadamore@1551 468 this.resultInfo = resultInfo;
mcimadamore@1551 469 this.inferenceContext = deferredAttrContext.inferenceContext;
mcimadamore@1551 470 dt.tree.accept(this);
mcimadamore@1551 471 dt.speculativeCache.put(deferredAttrContext.msym, stuckTree, deferredAttrContext.phase);
mcimadamore@1551 472 return Type.noType;
mcimadamore@1551 473 }
mcimadamore@1347 474
mcimadamore@1551 475 @Override
mcimadamore@1551 476 public void visitLambda(JCLambda tree) {
mcimadamore@1551 477 Check.CheckContext checkContext = resultInfo.checkContext;
mcimadamore@1551 478 Type pt = resultInfo.pt;
mcimadamore@1551 479 if (inferenceContext.inferencevars.contains(pt)) {
mcimadamore@1551 480 //ok
mcimadamore@1551 481 return;
mcimadamore@1551 482 } else {
mcimadamore@1551 483 //must be a functional descriptor
mcimadamore@1551 484 try {
mcimadamore@1551 485 Type desc = types.findDescriptorType(pt);
mcimadamore@1551 486 if (desc.getParameterTypes().length() != tree.params.length()) {
mcimadamore@1551 487 checkContext.report(tree, diags.fragment("incompatible.arg.types.in.lambda"));
mcimadamore@1551 488 }
mcimadamore@1551 489 } catch (Types.FunctionDescriptorLookupError ex) {
mcimadamore@1551 490 checkContext.report(null, ex.getDiagnostic());
mcimadamore@1551 491 }
mcimadamore@1347 492 }
mcimadamore@1347 493 }
mcimadamore@1347 494
mcimadamore@1347 495 @Override
mcimadamore@1551 496 public void visitNewClass(JCNewClass tree) {
mcimadamore@1551 497 //do nothing
mcimadamore@1347 498 }
mcimadamore@1347 499
mcimadamore@1551 500 @Override
mcimadamore@1551 501 public void visitApply(JCMethodInvocation tree) {
mcimadamore@1551 502 //do nothing
mcimadamore@1347 503 }
mcimadamore@1347 504
mcimadamore@1551 505 @Override
mcimadamore@1551 506 public void visitReference(JCMemberReference tree) {
mcimadamore@1551 507 Check.CheckContext checkContext = resultInfo.checkContext;
mcimadamore@1551 508 Type pt = resultInfo.pt;
mcimadamore@1551 509 if (inferenceContext.inferencevars.contains(pt)) {
mcimadamore@1551 510 //ok
mcimadamore@1551 511 return;
mcimadamore@1551 512 } else {
mcimadamore@1551 513 try {
mcimadamore@1551 514 //TODO: we should speculative determine if there's a match
mcimadamore@1551 515 //based on arity - if yes, method is applicable.
mcimadamore@1551 516 types.findDescriptorType(pt);
mcimadamore@1551 517 } catch (Types.FunctionDescriptorLookupError ex) {
mcimadamore@1551 518 checkContext.report(null, ex.getDiagnostic());
mcimadamore@1510 519 }
mcimadamore@1510 520 }
mcimadamore@1347 521 }
mcimadamore@1347 522 }
mcimadamore@1347 523 }
mcimadamore@1347 524
mcimadamore@1347 525 /** an empty deferred attribution context - all methods throw exceptions */
mcimadamore@1347 526 final DeferredAttrContext emptyDeferredAttrContext =
mcimadamore@1551 527 new DeferredAttrContext(AttrMode.CHECK, null, MethodResolutionPhase.BOX, null, null, null) {
mcimadamore@1347 528 @Override
mcimadamore@1347 529 void addDeferredAttrNode(DeferredType dt, ResultInfo ri, List<Type> stuckVars) {
mcimadamore@1347 530 Assert.error("Empty deferred context!");
mcimadamore@1347 531 }
mcimadamore@1347 532 @Override
mcimadamore@1347 533 void complete() {
mcimadamore@1347 534 Assert.error("Empty deferred context!");
mcimadamore@1347 535 }
mcimadamore@1347 536 };
mcimadamore@1347 537
mcimadamore@1347 538 /**
mcimadamore@1347 539 * Map a list of types possibly containing one or more deferred types
mcimadamore@1347 540 * into a list of ordinary types. Each deferred type D is mapped into a type T,
mcimadamore@1347 541 * where T is computed by retrieving the type that has already been
mcimadamore@1347 542 * computed for D during a previous deferred attribution round of the given kind.
mcimadamore@1347 543 */
mcimadamore@1347 544 class DeferredTypeMap extends Type.Mapping {
mcimadamore@1347 545
mcimadamore@1347 546 DeferredAttrContext deferredAttrContext;
mcimadamore@1347 547
mcimadamore@1347 548 protected DeferredTypeMap(AttrMode mode, Symbol msym, MethodResolutionPhase phase) {
mcimadamore@1347 549 super(String.format("deferredTypeMap[%s]", mode));
mcimadamore@1551 550 this.deferredAttrContext = new DeferredAttrContext(mode, msym, phase,
mcimadamore@1551 551 infer.emptyContext, emptyDeferredAttrContext, types.noWarnings);
mcimadamore@1347 552 }
mcimadamore@1347 553
mcimadamore@1347 554 protected boolean validState(DeferredType dt) {
mcimadamore@1347 555 return dt.mode != null &&
mcimadamore@1347 556 deferredAttrContext.mode.ordinal() <= dt.mode.ordinal();
mcimadamore@1347 557 }
mcimadamore@1347 558
mcimadamore@1347 559 @Override
mcimadamore@1347 560 public Type apply(Type t) {
jjg@1374 561 if (!t.hasTag(DEFERRED)) {
mcimadamore@1347 562 return t.map(this);
mcimadamore@1347 563 } else {
mcimadamore@1347 564 DeferredType dt = (DeferredType)t;
mcimadamore@1347 565 Assert.check(validState(dt));
mcimadamore@1347 566 return typeOf(dt);
mcimadamore@1347 567 }
mcimadamore@1347 568 }
mcimadamore@1347 569
mcimadamore@1347 570 protected Type typeOf(DeferredType dt) {
mcimadamore@1347 571 switch (deferredAttrContext.mode) {
mcimadamore@1347 572 case CHECK:
mcimadamore@1347 573 return dt.tree.type == null ? Type.noType : dt.tree.type;
mcimadamore@1347 574 case SPECULATIVE:
mcimadamore@1347 575 return dt.speculativeType(deferredAttrContext.msym, deferredAttrContext.phase);
mcimadamore@1347 576 }
mcimadamore@1347 577 Assert.error();
mcimadamore@1347 578 return null;
mcimadamore@1347 579 }
mcimadamore@1347 580 }
mcimadamore@1347 581
mcimadamore@1347 582 /**
mcimadamore@1347 583 * Specialized recovery deferred mapping.
mcimadamore@1347 584 * Each deferred type D is mapped into a type T, where T is computed either by
mcimadamore@1347 585 * (i) retrieving the type that has already been computed for D during a previous
mcimadamore@1347 586 * attribution round (as before), or (ii) by synthesizing a new type R for D
mcimadamore@1347 587 * (the latter step is useful in a recovery scenario).
mcimadamore@1347 588 */
mcimadamore@1347 589 public class RecoveryDeferredTypeMap extends DeferredTypeMap {
mcimadamore@1347 590
mcimadamore@1347 591 public RecoveryDeferredTypeMap(AttrMode mode, Symbol msym, MethodResolutionPhase phase) {
mcimadamore@1415 592 super(mode, msym, phase != null ? phase : MethodResolutionPhase.BOX);
mcimadamore@1347 593 }
mcimadamore@1347 594
mcimadamore@1347 595 @Override
mcimadamore@1347 596 protected Type typeOf(DeferredType dt) {
mcimadamore@1347 597 Type owntype = super.typeOf(dt);
mcimadamore@1415 598 return owntype == Type.noType ?
mcimadamore@1347 599 recover(dt) : owntype;
mcimadamore@1347 600 }
mcimadamore@1347 601
mcimadamore@1347 602 @Override
mcimadamore@1347 603 protected boolean validState(DeferredType dt) {
mcimadamore@1347 604 return true;
mcimadamore@1347 605 }
mcimadamore@1347 606
mcimadamore@1347 607 /**
mcimadamore@1347 608 * Synthesize a type for a deferred type that hasn't been previously
mcimadamore@1347 609 * reduced to an ordinary type. Functional deferred types and conditionals
mcimadamore@1347 610 * are mapped to themselves, in order to have a richer diagnostic
mcimadamore@1347 611 * representation. Remaining deferred types are attributed using
mcimadamore@1347 612 * a default expected type (j.l.Object).
mcimadamore@1347 613 */
mcimadamore@1347 614 private Type recover(DeferredType dt) {
mcimadamore@1348 615 dt.check(attr.new RecoveryInfo(deferredAttrContext));
mcimadamore@1415 616 return super.apply(dt);
mcimadamore@1347 617 }
mcimadamore@1347 618 }
mcimadamore@1347 619
mcimadamore@1347 620 /**
mcimadamore@1347 621 * Retrieves the list of inference variables that need to be inferred before
mcimadamore@1347 622 * an AST node can be type-checked
mcimadamore@1347 623 */
mcimadamore@1347 624 @SuppressWarnings("fallthrough")
mcimadamore@1415 625 List<Type> stuckVars(JCTree tree, Env<AttrContext> env, ResultInfo resultInfo) {
mcimadamore@1415 626 if (resultInfo.pt.hasTag(NONE) || resultInfo.pt.isErroneous()) {
mcimadamore@1348 627 return List.nil();
mcimadamore@1348 628 } else {
mcimadamore@1481 629 return stuckVarsInternal(tree, resultInfo.pt, resultInfo.checkContext.inferenceContext());
mcimadamore@1481 630 }
mcimadamore@1481 631 }
mcimadamore@1481 632 //where
mcimadamore@1481 633 private List<Type> stuckVarsInternal(JCTree tree, Type pt, Infer.InferenceContext inferenceContext) {
mcimadamore@1481 634 StuckChecker sc = new StuckChecker(pt, inferenceContext);
mcimadamore@1348 635 sc.scan(tree);
mcimadamore@1348 636 return List.from(sc.stuckVars);
mcimadamore@1348 637 }
mcimadamore@1481 638
mcimadamore@1481 639 /**
mcimadamore@1481 640 * A special tree scanner that would only visit portions of a given tree.
mcimadamore@1481 641 * The set of nodes visited by the scanner can be customized at construction-time.
mcimadamore@1481 642 */
mcimadamore@1481 643 abstract static class FilterScanner extends TreeScanner {
mcimadamore@1481 644
mcimadamore@1481 645 final Filter<JCTree> treeFilter;
mcimadamore@1481 646
mcimadamore@1481 647 FilterScanner(final Set<JCTree.Tag> validTags) {
mcimadamore@1481 648 this.treeFilter = new Filter<JCTree>() {
mcimadamore@1481 649 public boolean accepts(JCTree t) {
mcimadamore@1481 650 return validTags.contains(t.getTag());
mcimadamore@1481 651 }
mcimadamore@1481 652 };
mcimadamore@1481 653 }
mcimadamore@1481 654
mcimadamore@1481 655 @Override
mcimadamore@1481 656 public void scan(JCTree tree) {
mcimadamore@1481 657 if (tree != null) {
mcimadamore@1481 658 if (treeFilter.accepts(tree)) {
mcimadamore@1481 659 super.scan(tree);
mcimadamore@1481 660 } else {
mcimadamore@1481 661 skip(tree);
mcimadamore@1481 662 }
mcimadamore@1481 663 }
mcimadamore@1481 664 }
mcimadamore@1481 665
mcimadamore@1481 666 /**
mcimadamore@1481 667 * handler that is executed when a node has been discarded
mcimadamore@1481 668 */
mcimadamore@1481 669 abstract void skip(JCTree tree);
mcimadamore@1481 670 }
mcimadamore@1481 671
mcimadamore@1481 672 /**
mcimadamore@1481 673 * A tree scanner suitable for visiting the target-type dependent nodes of
mcimadamore@1481 674 * a given argument expression.
mcimadamore@1481 675 */
mcimadamore@1481 676 static class PolyScanner extends FilterScanner {
mcimadamore@1481 677
mcimadamore@1481 678 PolyScanner() {
mcimadamore@1481 679 super(EnumSet.of(CONDEXPR, PARENS, LAMBDA, REFERENCE));
mcimadamore@1481 680 }
mcimadamore@1481 681
mcimadamore@1481 682 @Override
mcimadamore@1481 683 void skip(JCTree tree) {
mcimadamore@1481 684 //do nothing
mcimadamore@1481 685 }
mcimadamore@1481 686 }
mcimadamore@1481 687
mcimadamore@1481 688 /**
mcimadamore@1481 689 * A tree scanner suitable for visiting the target-type dependent nodes nested
mcimadamore@1481 690 * within a lambda expression body.
mcimadamore@1481 691 */
mcimadamore@1481 692 static class LambdaReturnScanner extends FilterScanner {
mcimadamore@1481 693
mcimadamore@1481 694 LambdaReturnScanner() {
mcimadamore@1481 695 super(EnumSet.of(BLOCK, CASE, CATCH, DOLOOP, FOREACHLOOP,
mcimadamore@1481 696 FORLOOP, RETURN, SYNCHRONIZED, SWITCH, TRY, WHILELOOP));
mcimadamore@1481 697 }
mcimadamore@1481 698
mcimadamore@1481 699 @Override
mcimadamore@1481 700 void skip(JCTree tree) {
mcimadamore@1481 701 //do nothing
mcimadamore@1481 702 }
mcimadamore@1348 703 }
mcimadamore@1348 704
mcimadamore@1348 705 /**
mcimadamore@1348 706 * This visitor is used to check that structural expressions conform
mcimadamore@1348 707 * to their target - this step is required as inference could end up
mcimadamore@1348 708 * inferring types that make some of the nested expressions incompatible
mcimadamore@1348 709 * with their corresponding instantiated target
mcimadamore@1348 710 */
mcimadamore@1481 711 class StuckChecker extends PolyScanner {
mcimadamore@1348 712
mcimadamore@1348 713 Type pt;
mcimadamore@1348 714 Infer.InferenceContext inferenceContext;
mcimadamore@1415 715 Set<Type> stuckVars = new LinkedHashSet<Type>();
mcimadamore@1348 716
mcimadamore@1481 717 StuckChecker(Type pt, Infer.InferenceContext inferenceContext) {
mcimadamore@1481 718 this.pt = pt;
mcimadamore@1481 719 this.inferenceContext = inferenceContext;
mcimadamore@1348 720 }
mcimadamore@1348 721
mcimadamore@1348 722 @Override
mcimadamore@1348 723 public void visitLambda(JCLambda tree) {
mcimadamore@1481 724 if (inferenceContext.inferenceVars().contains(pt)) {
mcimadamore@1481 725 stuckVars.add(pt);
mcimadamore@1348 726 }
mcimadamore@1510 727 if (!types.isFunctionalInterface(pt)) {
mcimadamore@1481 728 return;
mcimadamore@1481 729 }
mcimadamore@1481 730 Type descType = types.findDescriptorType(pt);
mcimadamore@1481 731 List<Type> freeArgVars = inferenceContext.freeVarsIn(descType.getParameterTypes());
mcimadamore@1510 732 if (tree.paramKind == JCLambda.ParameterKind.IMPLICIT &&
mcimadamore@1481 733 freeArgVars.nonEmpty()) {
mcimadamore@1481 734 stuckVars.addAll(freeArgVars);
mcimadamore@1481 735 }
mcimadamore@1481 736 scanLambdaBody(tree, descType.getReturnType());
mcimadamore@1348 737 }
mcimadamore@1348 738
mcimadamore@1348 739 @Override
mcimadamore@1348 740 public void visitReference(JCMemberReference tree) {
mcimadamore@1348 741 scan(tree.expr);
mcimadamore@1348 742 if (inferenceContext.inferenceVars().contains(pt)) {
mcimadamore@1348 743 stuckVars.add(pt);
mcimadamore@1348 744 return;
mcimadamore@1348 745 }
mcimadamore@1510 746 if (!types.isFunctionalInterface(pt)) {
mcimadamore@1348 747 return;
mcimadamore@1348 748 }
mcimadamore@1415 749
mcimadamore@1348 750 Type descType = types.findDescriptorType(pt);
mcimadamore@1348 751 List<Type> freeArgVars = inferenceContext.freeVarsIn(descType.getParameterTypes());
mcimadamore@1348 752 stuckVars.addAll(freeArgVars);
mcimadamore@1348 753 }
mcimadamore@1348 754
mcimadamore@1481 755 void scanLambdaBody(JCLambda lambda, final Type pt) {
mcimadamore@1481 756 if (lambda.getBodyKind() == JCTree.JCLambda.BodyKind.EXPRESSION) {
mcimadamore@1481 757 stuckVars.addAll(stuckVarsInternal(lambda.body, pt, inferenceContext));
mcimadamore@1481 758 } else {
mcimadamore@1481 759 LambdaReturnScanner lambdaScanner = new LambdaReturnScanner() {
mcimadamore@1481 760 @Override
mcimadamore@1481 761 public void visitReturn(JCReturn tree) {
mcimadamore@1481 762 if (tree.expr != null) {
mcimadamore@1481 763 stuckVars.addAll(stuckVarsInternal(tree.expr, pt, inferenceContext));
mcimadamore@1481 764 }
mcimadamore@1481 765 }
mcimadamore@1481 766 };
mcimadamore@1481 767 lambdaScanner.scan(lambda.body);
mcimadamore@1348 768 }
mcimadamore@1347 769 }
mcimadamore@1347 770 }
mcimadamore@1347 771 }

mercurial