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

Tue, 26 Feb 2013 09:04:19 +0000

author
vromero
date
Tue, 26 Feb 2013 09:04:19 +0000
changeset 1607
bd49e0304281
parent 1596
3a39d123d33a
child 1613
d2a98dde7ecc
permissions
-rw-r--r--

8008436: javac should not issue a warning for overriding equals without hasCode if hashCode has been overriden by a superclass
Reviewed-by: jjg, mcimadamore

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

mercurial