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

Tue, 28 May 2013 12:46:10 +0100

author
vromero
date
Tue, 28 May 2013 12:46:10 +0100
changeset 1783
c6df5b20f9eb
parent 1697
950e8ac120f0
child 1850
6debfa63a4a1
permissions
-rw-r--r--

6970173: Debug pointer at bad position
Reviewed-by: mcimadamore

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

mercurial