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

Mon, 21 Jan 2013 20:13:56 +0000

author
mcimadamore
date
Mon, 21 Jan 2013 20:13:56 +0000
changeset 1510
7873d37f5b37
parent 1481
d07340b61e6a
child 1550
1df20330f6bd
permissions
-rw-r--r--

8005244: Implement overload resolution as per latest spec EDR
Summary: Add support for stuck expressions and provisional applicability
Reviewed-by: jjg

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@1481 237 Assert.check(dt.mode == AttrMode.SPECULATIVE);
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@1481 245 /**
mcimadamore@1347 246 * The 'mode' in which the deferred type is to be type-checked
mcimadamore@1347 247 */
mcimadamore@1347 248 public enum AttrMode {
mcimadamore@1347 249 /**
mcimadamore@1347 250 * A speculative type-checking round is used during overload resolution
mcimadamore@1347 251 * mainly to generate constraints on inference variables. Side-effects
mcimadamore@1347 252 * arising from type-checking the expression associated with the deferred
mcimadamore@1347 253 * type are reversed after the speculative round finishes. This means the
mcimadamore@1347 254 * expression tree will be left in a blank state.
mcimadamore@1347 255 */
mcimadamore@1347 256 SPECULATIVE,
mcimadamore@1347 257 /**
mcimadamore@1347 258 * This is the plain type-checking mode. Produces side-effects on the underlying AST node
mcimadamore@1347 259 */
mcimadamore@1347 260 CHECK;
mcimadamore@1347 261 }
mcimadamore@1347 262
mcimadamore@1347 263 /**
mcimadamore@1347 264 * Routine that performs speculative type-checking; the input AST node is
mcimadamore@1347 265 * cloned (to avoid side-effects cause by Attr) and compiler state is
mcimadamore@1347 266 * restored after type-checking. All diagnostics (but critical ones) are
mcimadamore@1347 267 * disabled during speculative type-checking.
mcimadamore@1347 268 */
mcimadamore@1347 269 JCTree attribSpeculative(JCTree tree, Env<AttrContext> env, ResultInfo resultInfo) {
mcimadamore@1347 270 JCTree newTree = new TreeCopier<Object>(make).copy(tree);
mcimadamore@1347 271 Env<AttrContext> speculativeEnv = env.dup(newTree, env.info.dup(env.info.scope.dupUnshared()));
mcimadamore@1347 272 speculativeEnv.info.scope.owner = env.info.scope.owner;
mcimadamore@1347 273 final JavaFileObject currentSource = log.currentSourceFile();
jjg@1406 274 Log.DeferredDiagnosticHandler deferredDiagnosticHandler =
jjg@1406 275 new Log.DeferredDiagnosticHandler(log, new Filter<JCDiagnostic>() {
jjg@1406 276 public boolean accepts(JCDiagnostic t) {
jjg@1406 277 return t.getDiagnosticSource().getFile().equals(currentSource);
jjg@1406 278 }
jjg@1406 279 });
mcimadamore@1347 280 try {
mcimadamore@1347 281 attr.attribTree(newTree, speculativeEnv, resultInfo);
mcimadamore@1347 282 unenterScanner.scan(newTree);
mcimadamore@1347 283 return newTree;
mcimadamore@1347 284 } catch (Abort ex) {
mcimadamore@1347 285 //if some very bad condition occurred during deferred attribution
mcimadamore@1347 286 //we should dump all errors before killing javac
jjg@1406 287 deferredDiagnosticHandler.reportDeferredDiagnostics();
mcimadamore@1347 288 throw ex;
mcimadamore@1347 289 } finally {
mcimadamore@1347 290 unenterScanner.scan(newTree);
jjg@1406 291 log.popDiagnosticHandler(deferredDiagnosticHandler);
mcimadamore@1347 292 }
mcimadamore@1347 293 }
mcimadamore@1347 294 //where
mcimadamore@1347 295 protected TreeScanner unenterScanner = new TreeScanner() {
mcimadamore@1347 296 @Override
mcimadamore@1347 297 public void visitClassDef(JCClassDecl tree) {
mcimadamore@1347 298 ClassSymbol csym = tree.sym;
mcimadamore@1415 299 //if something went wrong during method applicability check
mcimadamore@1415 300 //it is possible that nested expressions inside argument expression
mcimadamore@1415 301 //are left unchecked - in such cases there's nothing to clean up.
mcimadamore@1415 302 if (csym == null) return;
mcimadamore@1347 303 enter.typeEnvs.remove(csym);
mcimadamore@1347 304 chk.compiled.remove(csym.flatname);
mcimadamore@1347 305 syms.classes.remove(csym.flatname);
mcimadamore@1347 306 super.visitClassDef(tree);
mcimadamore@1347 307 }
mcimadamore@1347 308 };
mcimadamore@1347 309
mcimadamore@1347 310 /**
mcimadamore@1347 311 * A deferred context is created on each method check. A deferred context is
mcimadamore@1347 312 * used to keep track of information associated with the method check, such as
mcimadamore@1347 313 * the symbol of the method being checked, the overload resolution phase,
mcimadamore@1347 314 * the kind of attribution mode to be applied to deferred types and so forth.
mcimadamore@1347 315 * As deferred types are processed (by the method check routine) stuck AST nodes
mcimadamore@1347 316 * are added (as new deferred attribution nodes) to this context. The complete()
mcimadamore@1347 317 * routine makes sure that all pending nodes are properly processed, by
mcimadamore@1347 318 * progressively instantiating all inference variables on which one or more
mcimadamore@1347 319 * deferred attribution node is stuck.
mcimadamore@1347 320 */
mcimadamore@1347 321 class DeferredAttrContext {
mcimadamore@1347 322
mcimadamore@1347 323 /** attribution mode */
mcimadamore@1347 324 final AttrMode mode;
mcimadamore@1347 325
mcimadamore@1347 326 /** symbol of the method being checked */
mcimadamore@1347 327 final Symbol msym;
mcimadamore@1347 328
mcimadamore@1347 329 /** method resolution step */
mcimadamore@1347 330 final Resolve.MethodResolutionPhase phase;
mcimadamore@1347 331
mcimadamore@1347 332 /** inference context */
mcimadamore@1347 333 final InferenceContext inferenceContext;
mcimadamore@1347 334
mcimadamore@1347 335 /** list of deferred attribution nodes to be processed */
mcimadamore@1347 336 ArrayList<DeferredAttrNode> deferredAttrNodes = new ArrayList<DeferredAttrNode>();
mcimadamore@1347 337
mcimadamore@1347 338 DeferredAttrContext(AttrMode mode, Symbol msym, MethodResolutionPhase phase, InferenceContext inferenceContext) {
mcimadamore@1347 339 this.mode = mode;
mcimadamore@1347 340 this.msym = msym;
mcimadamore@1347 341 this.phase = phase;
mcimadamore@1347 342 this.inferenceContext = inferenceContext;
mcimadamore@1347 343 }
mcimadamore@1347 344
mcimadamore@1347 345 /**
mcimadamore@1347 346 * Adds a node to the list of deferred attribution nodes - used by Resolve.rawCheckArgumentsApplicable
mcimadamore@1347 347 * Nodes added this way act as 'roots' for the out-of-order method checking process.
mcimadamore@1347 348 */
mcimadamore@1347 349 void addDeferredAttrNode(final DeferredType dt, ResultInfo resultInfo, List<Type> stuckVars) {
mcimadamore@1347 350 deferredAttrNodes.add(new DeferredAttrNode(dt, resultInfo, stuckVars));
mcimadamore@1347 351 }
mcimadamore@1347 352
mcimadamore@1347 353 /**
mcimadamore@1347 354 * Incrementally process all nodes, by skipping 'stuck' nodes and attributing
mcimadamore@1347 355 * 'unstuck' ones. If at any point no progress can be made (no 'unstuck' nodes)
mcimadamore@1347 356 * some inference variable might get eagerly instantiated so that all nodes
mcimadamore@1347 357 * can be type-checked.
mcimadamore@1347 358 */
mcimadamore@1347 359 void complete() {
mcimadamore@1347 360 while (!deferredAttrNodes.isEmpty()) {
mcimadamore@1415 361 Set<Type> stuckVars = new LinkedHashSet<Type>();
mcimadamore@1347 362 boolean progress = false;
mcimadamore@1347 363 //scan a defensive copy of the node list - this is because a deferred
mcimadamore@1347 364 //attribution round can add new nodes to the list
mcimadamore@1347 365 for (DeferredAttrNode deferredAttrNode : List.from(deferredAttrNodes)) {
mcimadamore@1510 366 if (!deferredAttrNode.process()) {
mcimadamore@1510 367 stuckVars.addAll(deferredAttrNode.stuckVars);
mcimadamore@1510 368 } else {
mcimadamore@1347 369 deferredAttrNodes.remove(deferredAttrNode);
mcimadamore@1347 370 progress = true;
mcimadamore@1347 371 }
mcimadamore@1347 372 }
mcimadamore@1347 373 if (!progress) {
mcimadamore@1347 374 //remove all variables that have already been instantiated
mcimadamore@1347 375 //from the list of stuck variables
mcimadamore@1347 376 inferenceContext.solveAny(inferenceContext.freeVarsIn(List.from(stuckVars)), types, infer);
mcimadamore@1347 377 inferenceContext.notifyChange(types);
mcimadamore@1347 378 }
mcimadamore@1347 379 }
mcimadamore@1347 380 }
mcimadamore@1347 381
mcimadamore@1347 382 /**
mcimadamore@1347 383 * Class representing a deferred attribution node. It keeps track of
mcimadamore@1347 384 * a deferred type, along with the expected target type information.
mcimadamore@1347 385 */
mcimadamore@1347 386 class DeferredAttrNode implements Infer.InferenceContext.FreeTypeListener {
mcimadamore@1347 387
mcimadamore@1347 388 /** underlying deferred type */
mcimadamore@1347 389 DeferredType dt;
mcimadamore@1347 390
mcimadamore@1347 391 /** underlying target type information */
mcimadamore@1347 392 ResultInfo resultInfo;
mcimadamore@1347 393
mcimadamore@1347 394 /** list of uninferred inference variables causing this node to be stuck */
mcimadamore@1347 395 List<Type> stuckVars;
mcimadamore@1347 396
mcimadamore@1347 397 DeferredAttrNode(DeferredType dt, ResultInfo resultInfo, List<Type> stuckVars) {
mcimadamore@1347 398 this.dt = dt;
mcimadamore@1347 399 this.resultInfo = resultInfo;
mcimadamore@1347 400 this.stuckVars = stuckVars;
mcimadamore@1347 401 if (!stuckVars.isEmpty()) {
mcimadamore@1347 402 resultInfo.checkContext.inferenceContext().addFreeTypeListener(stuckVars, this);
mcimadamore@1347 403 }
mcimadamore@1347 404 }
mcimadamore@1347 405
mcimadamore@1347 406 @Override
mcimadamore@1347 407 public void typesInferred(InferenceContext inferenceContext) {
mcimadamore@1347 408 stuckVars = List.nil();
mcimadamore@1347 409 resultInfo = resultInfo.dup(inferenceContext.asInstType(resultInfo.pt, types));
mcimadamore@1347 410 }
mcimadamore@1347 411
mcimadamore@1347 412 /**
mcimadamore@1510 413 * Process a deferred attribution node.
mcimadamore@1510 414 * Invariant: a stuck node cannot be processed.
mcimadamore@1347 415 */
mcimadamore@1510 416 @SuppressWarnings("fallthrough")
mcimadamore@1510 417 boolean process() {
mcimadamore@1510 418 switch (mode) {
mcimadamore@1510 419 case SPECULATIVE:
mcimadamore@1510 420 dt.check(resultInfo, List.<Type>nil(), new StructuralStuckChecker());
mcimadamore@1510 421 return true;
mcimadamore@1510 422 case CHECK:
mcimadamore@1510 423 if (stuckVars.nonEmpty()) {
mcimadamore@1510 424 return false;
mcimadamore@1510 425 } else {
mcimadamore@1510 426 dt.check(resultInfo, stuckVars, basicCompleter);
mcimadamore@1510 427 return true;
mcimadamore@1510 428 }
mcimadamore@1510 429 default:
mcimadamore@1510 430 throw new AssertionError("Bad mode");
mcimadamore@1510 431 }
mcimadamore@1347 432 }
mcimadamore@1347 433
mcimadamore@1347 434 /**
mcimadamore@1510 435 * Structural checker for stuck expressions
mcimadamore@1347 436 */
mcimadamore@1510 437 class StructuralStuckChecker extends TreeScanner implements DeferredTypeCompleter {
mcimadamore@1510 438
mcimadamore@1510 439 ResultInfo resultInfo;
mcimadamore@1510 440
mcimadamore@1510 441 public Type complete(DeferredType dt, ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) {
mcimadamore@1510 442 this.resultInfo = resultInfo;
mcimadamore@1510 443 dt.tree.accept(this);
mcimadamore@1510 444 dt.speculativeCache.put(msym, stuckTree, phase);
mcimadamore@1510 445 return Type.noType;
mcimadamore@1347 446 }
mcimadamore@1510 447
mcimadamore@1510 448 @Override
mcimadamore@1510 449 public void visitLambda(JCLambda tree) {
mcimadamore@1510 450 Check.CheckContext checkContext = resultInfo.checkContext;
mcimadamore@1510 451 Type pt = resultInfo.pt;
mcimadamore@1510 452 if (inferenceContext.inferencevars.contains(pt)) {
mcimadamore@1510 453 //ok
mcimadamore@1510 454 return;
mcimadamore@1510 455 } else {
mcimadamore@1510 456 //must be a functional descriptor
mcimadamore@1510 457 try {
mcimadamore@1510 458 Type desc = types.findDescriptorType(pt);
mcimadamore@1510 459 if (desc.getParameterTypes().length() != tree.params.length()) {
mcimadamore@1510 460 checkContext.report(tree, diags.fragment("incompatible.arg.types.in.lambda"));
mcimadamore@1510 461 }
mcimadamore@1510 462 } catch (Types.FunctionDescriptorLookupError ex) {
mcimadamore@1510 463 checkContext.report(null, ex.getDiagnostic());
mcimadamore@1510 464 }
mcimadamore@1510 465 }
mcimadamore@1510 466 }
mcimadamore@1510 467
mcimadamore@1510 468 @Override
mcimadamore@1510 469 public void visitNewClass(JCNewClass tree) {
mcimadamore@1510 470 //do nothing
mcimadamore@1510 471 }
mcimadamore@1510 472
mcimadamore@1510 473 @Override
mcimadamore@1510 474 public void visitApply(JCMethodInvocation tree) {
mcimadamore@1510 475 //do nothing
mcimadamore@1510 476 }
mcimadamore@1510 477
mcimadamore@1510 478 @Override
mcimadamore@1510 479 public void visitReference(JCMemberReference tree) {
mcimadamore@1510 480 Check.CheckContext checkContext = resultInfo.checkContext;
mcimadamore@1510 481 Type pt = resultInfo.pt;
mcimadamore@1510 482 if (inferenceContext.inferencevars.contains(pt)) {
mcimadamore@1510 483 //ok
mcimadamore@1510 484 return;
mcimadamore@1510 485 } else {
mcimadamore@1510 486 try {
mcimadamore@1510 487 //TODO: we should speculative determine if there's a match
mcimadamore@1510 488 //based on arity - if yes, method is applicable.
mcimadamore@1510 489 types.findDescriptorType(pt);
mcimadamore@1510 490 } catch (Types.FunctionDescriptorLookupError ex) {
mcimadamore@1510 491 checkContext.report(null, ex.getDiagnostic());
mcimadamore@1510 492 }
mcimadamore@1510 493 }
mcimadamore@1510 494 }
mcimadamore@1347 495 }
mcimadamore@1347 496 }
mcimadamore@1347 497 }
mcimadamore@1347 498
mcimadamore@1347 499 /** an empty deferred attribution context - all methods throw exceptions */
mcimadamore@1347 500 final DeferredAttrContext emptyDeferredAttrContext =
mcimadamore@1415 501 new DeferredAttrContext(AttrMode.CHECK, null, MethodResolutionPhase.BOX, null) {
mcimadamore@1347 502 @Override
mcimadamore@1347 503 void addDeferredAttrNode(DeferredType dt, ResultInfo ri, List<Type> stuckVars) {
mcimadamore@1347 504 Assert.error("Empty deferred context!");
mcimadamore@1347 505 }
mcimadamore@1347 506 @Override
mcimadamore@1347 507 void complete() {
mcimadamore@1347 508 Assert.error("Empty deferred context!");
mcimadamore@1347 509 }
mcimadamore@1347 510 };
mcimadamore@1347 511
mcimadamore@1347 512 /**
mcimadamore@1347 513 * Map a list of types possibly containing one or more deferred types
mcimadamore@1347 514 * into a list of ordinary types. Each deferred type D is mapped into a type T,
mcimadamore@1347 515 * where T is computed by retrieving the type that has already been
mcimadamore@1347 516 * computed for D during a previous deferred attribution round of the given kind.
mcimadamore@1347 517 */
mcimadamore@1347 518 class DeferredTypeMap extends Type.Mapping {
mcimadamore@1347 519
mcimadamore@1347 520 DeferredAttrContext deferredAttrContext;
mcimadamore@1347 521
mcimadamore@1347 522 protected DeferredTypeMap(AttrMode mode, Symbol msym, MethodResolutionPhase phase) {
mcimadamore@1347 523 super(String.format("deferredTypeMap[%s]", mode));
mcimadamore@1347 524 this.deferredAttrContext = new DeferredAttrContext(mode, msym, phase, infer.emptyContext);
mcimadamore@1347 525 }
mcimadamore@1347 526
mcimadamore@1347 527 protected boolean validState(DeferredType dt) {
mcimadamore@1347 528 return dt.mode != null &&
mcimadamore@1347 529 deferredAttrContext.mode.ordinal() <= dt.mode.ordinal();
mcimadamore@1347 530 }
mcimadamore@1347 531
mcimadamore@1347 532 @Override
mcimadamore@1347 533 public Type apply(Type t) {
jjg@1374 534 if (!t.hasTag(DEFERRED)) {
mcimadamore@1347 535 return t.map(this);
mcimadamore@1347 536 } else {
mcimadamore@1347 537 DeferredType dt = (DeferredType)t;
mcimadamore@1347 538 Assert.check(validState(dt));
mcimadamore@1347 539 return typeOf(dt);
mcimadamore@1347 540 }
mcimadamore@1347 541 }
mcimadamore@1347 542
mcimadamore@1347 543 protected Type typeOf(DeferredType dt) {
mcimadamore@1347 544 switch (deferredAttrContext.mode) {
mcimadamore@1347 545 case CHECK:
mcimadamore@1347 546 return dt.tree.type == null ? Type.noType : dt.tree.type;
mcimadamore@1347 547 case SPECULATIVE:
mcimadamore@1347 548 return dt.speculativeType(deferredAttrContext.msym, deferredAttrContext.phase);
mcimadamore@1347 549 }
mcimadamore@1347 550 Assert.error();
mcimadamore@1347 551 return null;
mcimadamore@1347 552 }
mcimadamore@1347 553 }
mcimadamore@1347 554
mcimadamore@1347 555 /**
mcimadamore@1347 556 * Specialized recovery deferred mapping.
mcimadamore@1347 557 * Each deferred type D is mapped into a type T, where T is computed either by
mcimadamore@1347 558 * (i) retrieving the type that has already been computed for D during a previous
mcimadamore@1347 559 * attribution round (as before), or (ii) by synthesizing a new type R for D
mcimadamore@1347 560 * (the latter step is useful in a recovery scenario).
mcimadamore@1347 561 */
mcimadamore@1347 562 public class RecoveryDeferredTypeMap extends DeferredTypeMap {
mcimadamore@1347 563
mcimadamore@1347 564 public RecoveryDeferredTypeMap(AttrMode mode, Symbol msym, MethodResolutionPhase phase) {
mcimadamore@1415 565 super(mode, msym, phase != null ? phase : MethodResolutionPhase.BOX);
mcimadamore@1347 566 }
mcimadamore@1347 567
mcimadamore@1347 568 @Override
mcimadamore@1347 569 protected Type typeOf(DeferredType dt) {
mcimadamore@1347 570 Type owntype = super.typeOf(dt);
mcimadamore@1415 571 return owntype == Type.noType ?
mcimadamore@1347 572 recover(dt) : owntype;
mcimadamore@1347 573 }
mcimadamore@1347 574
mcimadamore@1347 575 @Override
mcimadamore@1347 576 protected boolean validState(DeferredType dt) {
mcimadamore@1347 577 return true;
mcimadamore@1347 578 }
mcimadamore@1347 579
mcimadamore@1347 580 /**
mcimadamore@1347 581 * Synthesize a type for a deferred type that hasn't been previously
mcimadamore@1347 582 * reduced to an ordinary type. Functional deferred types and conditionals
mcimadamore@1347 583 * are mapped to themselves, in order to have a richer diagnostic
mcimadamore@1347 584 * representation. Remaining deferred types are attributed using
mcimadamore@1347 585 * a default expected type (j.l.Object).
mcimadamore@1347 586 */
mcimadamore@1347 587 private Type recover(DeferredType dt) {
mcimadamore@1348 588 dt.check(attr.new RecoveryInfo(deferredAttrContext));
mcimadamore@1415 589 return super.apply(dt);
mcimadamore@1347 590 }
mcimadamore@1347 591 }
mcimadamore@1347 592
mcimadamore@1347 593 /**
mcimadamore@1347 594 * Retrieves the list of inference variables that need to be inferred before
mcimadamore@1347 595 * an AST node can be type-checked
mcimadamore@1347 596 */
mcimadamore@1347 597 @SuppressWarnings("fallthrough")
mcimadamore@1415 598 List<Type> stuckVars(JCTree tree, Env<AttrContext> env, ResultInfo resultInfo) {
mcimadamore@1415 599 if (resultInfo.pt.hasTag(NONE) || resultInfo.pt.isErroneous()) {
mcimadamore@1348 600 return List.nil();
mcimadamore@1348 601 } else {
mcimadamore@1481 602 return stuckVarsInternal(tree, resultInfo.pt, resultInfo.checkContext.inferenceContext());
mcimadamore@1481 603 }
mcimadamore@1481 604 }
mcimadamore@1481 605 //where
mcimadamore@1481 606 private List<Type> stuckVarsInternal(JCTree tree, Type pt, Infer.InferenceContext inferenceContext) {
mcimadamore@1481 607 StuckChecker sc = new StuckChecker(pt, inferenceContext);
mcimadamore@1348 608 sc.scan(tree);
mcimadamore@1348 609 return List.from(sc.stuckVars);
mcimadamore@1348 610 }
mcimadamore@1481 611
mcimadamore@1481 612 /**
mcimadamore@1481 613 * A special tree scanner that would only visit portions of a given tree.
mcimadamore@1481 614 * The set of nodes visited by the scanner can be customized at construction-time.
mcimadamore@1481 615 */
mcimadamore@1481 616 abstract static class FilterScanner extends TreeScanner {
mcimadamore@1481 617
mcimadamore@1481 618 final Filter<JCTree> treeFilter;
mcimadamore@1481 619
mcimadamore@1481 620 FilterScanner(final Set<JCTree.Tag> validTags) {
mcimadamore@1481 621 this.treeFilter = new Filter<JCTree>() {
mcimadamore@1481 622 public boolean accepts(JCTree t) {
mcimadamore@1481 623 return validTags.contains(t.getTag());
mcimadamore@1481 624 }
mcimadamore@1481 625 };
mcimadamore@1481 626 }
mcimadamore@1481 627
mcimadamore@1481 628 @Override
mcimadamore@1481 629 public void scan(JCTree tree) {
mcimadamore@1481 630 if (tree != null) {
mcimadamore@1481 631 if (treeFilter.accepts(tree)) {
mcimadamore@1481 632 super.scan(tree);
mcimadamore@1481 633 } else {
mcimadamore@1481 634 skip(tree);
mcimadamore@1481 635 }
mcimadamore@1481 636 }
mcimadamore@1481 637 }
mcimadamore@1481 638
mcimadamore@1481 639 /**
mcimadamore@1481 640 * handler that is executed when a node has been discarded
mcimadamore@1481 641 */
mcimadamore@1481 642 abstract void skip(JCTree tree);
mcimadamore@1481 643 }
mcimadamore@1481 644
mcimadamore@1481 645 /**
mcimadamore@1481 646 * A tree scanner suitable for visiting the target-type dependent nodes of
mcimadamore@1481 647 * a given argument expression.
mcimadamore@1481 648 */
mcimadamore@1481 649 static class PolyScanner extends FilterScanner {
mcimadamore@1481 650
mcimadamore@1481 651 PolyScanner() {
mcimadamore@1481 652 super(EnumSet.of(CONDEXPR, PARENS, LAMBDA, REFERENCE));
mcimadamore@1481 653 }
mcimadamore@1481 654
mcimadamore@1481 655 @Override
mcimadamore@1481 656 void skip(JCTree tree) {
mcimadamore@1481 657 //do nothing
mcimadamore@1481 658 }
mcimadamore@1481 659 }
mcimadamore@1481 660
mcimadamore@1481 661 /**
mcimadamore@1481 662 * A tree scanner suitable for visiting the target-type dependent nodes nested
mcimadamore@1481 663 * within a lambda expression body.
mcimadamore@1481 664 */
mcimadamore@1481 665 static class LambdaReturnScanner extends FilterScanner {
mcimadamore@1481 666
mcimadamore@1481 667 LambdaReturnScanner() {
mcimadamore@1481 668 super(EnumSet.of(BLOCK, CASE, CATCH, DOLOOP, FOREACHLOOP,
mcimadamore@1481 669 FORLOOP, RETURN, SYNCHRONIZED, SWITCH, TRY, WHILELOOP));
mcimadamore@1481 670 }
mcimadamore@1481 671
mcimadamore@1481 672 @Override
mcimadamore@1481 673 void skip(JCTree tree) {
mcimadamore@1481 674 //do nothing
mcimadamore@1481 675 }
mcimadamore@1348 676 }
mcimadamore@1348 677
mcimadamore@1348 678 /**
mcimadamore@1348 679 * This visitor is used to check that structural expressions conform
mcimadamore@1348 680 * to their target - this step is required as inference could end up
mcimadamore@1348 681 * inferring types that make some of the nested expressions incompatible
mcimadamore@1348 682 * with their corresponding instantiated target
mcimadamore@1348 683 */
mcimadamore@1481 684 class StuckChecker extends PolyScanner {
mcimadamore@1348 685
mcimadamore@1348 686 Type pt;
mcimadamore@1348 687 Infer.InferenceContext inferenceContext;
mcimadamore@1415 688 Set<Type> stuckVars = new LinkedHashSet<Type>();
mcimadamore@1348 689
mcimadamore@1481 690 StuckChecker(Type pt, Infer.InferenceContext inferenceContext) {
mcimadamore@1481 691 this.pt = pt;
mcimadamore@1481 692 this.inferenceContext = inferenceContext;
mcimadamore@1348 693 }
mcimadamore@1348 694
mcimadamore@1348 695 @Override
mcimadamore@1348 696 public void visitLambda(JCLambda tree) {
mcimadamore@1481 697 if (inferenceContext.inferenceVars().contains(pt)) {
mcimadamore@1481 698 stuckVars.add(pt);
mcimadamore@1348 699 }
mcimadamore@1510 700 if (!types.isFunctionalInterface(pt)) {
mcimadamore@1481 701 return;
mcimadamore@1481 702 }
mcimadamore@1481 703 Type descType = types.findDescriptorType(pt);
mcimadamore@1481 704 List<Type> freeArgVars = inferenceContext.freeVarsIn(descType.getParameterTypes());
mcimadamore@1510 705 if (tree.paramKind == JCLambda.ParameterKind.IMPLICIT &&
mcimadamore@1481 706 freeArgVars.nonEmpty()) {
mcimadamore@1481 707 stuckVars.addAll(freeArgVars);
mcimadamore@1481 708 }
mcimadamore@1481 709 scanLambdaBody(tree, descType.getReturnType());
mcimadamore@1348 710 }
mcimadamore@1348 711
mcimadamore@1348 712 @Override
mcimadamore@1348 713 public void visitReference(JCMemberReference tree) {
mcimadamore@1348 714 scan(tree.expr);
mcimadamore@1348 715 if (inferenceContext.inferenceVars().contains(pt)) {
mcimadamore@1348 716 stuckVars.add(pt);
mcimadamore@1348 717 return;
mcimadamore@1348 718 }
mcimadamore@1510 719 if (!types.isFunctionalInterface(pt)) {
mcimadamore@1348 720 return;
mcimadamore@1348 721 }
mcimadamore@1415 722
mcimadamore@1348 723 Type descType = types.findDescriptorType(pt);
mcimadamore@1348 724 List<Type> freeArgVars = inferenceContext.freeVarsIn(descType.getParameterTypes());
mcimadamore@1348 725 stuckVars.addAll(freeArgVars);
mcimadamore@1348 726 }
mcimadamore@1348 727
mcimadamore@1481 728 void scanLambdaBody(JCLambda lambda, final Type pt) {
mcimadamore@1481 729 if (lambda.getBodyKind() == JCTree.JCLambda.BodyKind.EXPRESSION) {
mcimadamore@1481 730 stuckVars.addAll(stuckVarsInternal(lambda.body, pt, inferenceContext));
mcimadamore@1481 731 } else {
mcimadamore@1481 732 LambdaReturnScanner lambdaScanner = new LambdaReturnScanner() {
mcimadamore@1481 733 @Override
mcimadamore@1481 734 public void visitReturn(JCReturn tree) {
mcimadamore@1481 735 if (tree.expr != null) {
mcimadamore@1481 736 stuckVars.addAll(stuckVarsInternal(tree.expr, pt, inferenceContext));
mcimadamore@1481 737 }
mcimadamore@1481 738 }
mcimadamore@1481 739 };
mcimadamore@1481 740 lambdaScanner.scan(lambda.body);
mcimadamore@1348 741 }
mcimadamore@1347 742 }
mcimadamore@1347 743 }
mcimadamore@1347 744 }

mercurial