mcimadamore@1347: /*
vromero@2730: * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
mcimadamore@1347: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mcimadamore@1347: *
mcimadamore@1347: * This code is free software; you can redistribute it and/or modify it
mcimadamore@1347: * under the terms of the GNU General Public License version 2 only, as
mcimadamore@1347: * published by the Free Software Foundation. Oracle designates this
mcimadamore@1347: * particular file as subject to the "Classpath" exception as provided
mcimadamore@1347: * by Oracle in the LICENSE file that accompanied this code.
mcimadamore@1347: *
mcimadamore@1347: * This code is distributed in the hope that it will be useful, but WITHOUT
mcimadamore@1347: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mcimadamore@1347: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mcimadamore@1347: * version 2 for more details (a copy is included in the LICENSE file that
mcimadamore@1347: * accompanied this code).
mcimadamore@1347: *
mcimadamore@1347: * You should have received a copy of the GNU General Public License version
mcimadamore@1347: * 2 along with this work; if not, write to the Free Software Foundation,
mcimadamore@1347: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mcimadamore@1347: *
mcimadamore@1347: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
mcimadamore@1347: * or visit www.oracle.com if you need additional information or have any
mcimadamore@1347: * questions.
mcimadamore@1347: */
mcimadamore@1347:
mcimadamore@1347: package com.sun.tools.javac.comp;
mcimadamore@1347:
vromero@2370: import com.sun.source.tree.LambdaExpressionTree.BodyKind;
mcimadamore@1347: import com.sun.tools.javac.code.*;
mcimadamore@1347: import com.sun.tools.javac.tree.*;
mcimadamore@1347: import com.sun.tools.javac.util.*;
mcimadamore@1674: import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
mcimadamore@1347: import com.sun.tools.javac.code.Symbol.*;
mcimadamore@1347: import com.sun.tools.javac.code.Type.*;
mcimadamore@1347: import com.sun.tools.javac.comp.Attr.ResultInfo;
mcimadamore@1347: import com.sun.tools.javac.comp.Infer.InferenceContext;
mcimadamore@1347: import com.sun.tools.javac.comp.Resolve.MethodResolutionPhase;
mcimadamore@1347: import com.sun.tools.javac.tree.JCTree.*;
mcimadamore@1347:
mcimadamore@1347: import java.util.ArrayList;
vromero@2000: import java.util.Collections;
mcimadamore@1481: import java.util.EnumSet;
vromero@2000: import java.util.LinkedHashMap;
mcimadamore@1415: import java.util.LinkedHashSet;
mcimadamore@1347: import java.util.Map;
mcimadamore@1347: import java.util.Set;
mcimadamore@1347: import java.util.WeakHashMap;
mcimadamore@1347:
vromero@2370: import static com.sun.tools.javac.code.Kinds.VAL;
mcimadamore@1415: import static com.sun.tools.javac.code.TypeTag.*;
mcimadamore@1347: import static com.sun.tools.javac.tree.JCTree.Tag.*;
mcimadamore@1347:
mcimadamore@1347: /**
mcimadamore@1347: * This is an helper class that is used to perform deferred type-analysis.
mcimadamore@1347: * Each time a poly expression occurs in argument position, javac attributes it
mcimadamore@1347: * with a temporary 'deferred type' that is checked (possibly multiple times)
mcimadamore@1347: * against an expected formal type.
mcimadamore@1347: *
mcimadamore@1347: *
This is NOT part of any supported API.
mcimadamore@1347: * If you write code that depends on this, you do so at your own risk.
mcimadamore@1347: * This code and its internal interfaces are subject to change or
mcimadamore@1347: * deletion without notice.
mcimadamore@1347: */
mcimadamore@1347: public class DeferredAttr extends JCTree.Visitor {
mcimadamore@1347: protected static final Context.Key deferredAttrKey =
mcimadamore@1347: new Context.Key();
mcimadamore@1347:
mcimadamore@1347: final Attr attr;
mcimadamore@1347: final Check chk;
mcimadamore@1510: final JCDiagnostic.Factory diags;
mcimadamore@1347: final Enter enter;
mcimadamore@1347: final Infer infer;
mcimadamore@1581: final Resolve rs;
mcimadamore@1347: final Log log;
mcimadamore@1347: final Symtab syms;
mcimadamore@1347: final TreeMaker make;
mcimadamore@1347: final Types types;
vromero@2370: final Flow flow;
vromero@2370: final Names names;
pgovereau@2425: final TypeEnvs typeEnvs;
mcimadamore@1347:
mcimadamore@1347: public static DeferredAttr instance(Context context) {
mcimadamore@1347: DeferredAttr instance = context.get(deferredAttrKey);
mcimadamore@1347: if (instance == null)
mcimadamore@1347: instance = new DeferredAttr(context);
mcimadamore@1347: return instance;
mcimadamore@1347: }
mcimadamore@1347:
mcimadamore@1347: protected DeferredAttr(Context context) {
mcimadamore@1347: context.put(deferredAttrKey, this);
mcimadamore@1347: attr = Attr.instance(context);
mcimadamore@1347: chk = Check.instance(context);
mcimadamore@1510: diags = JCDiagnostic.Factory.instance(context);
mcimadamore@1347: enter = Enter.instance(context);
mcimadamore@1347: infer = Infer.instance(context);
mcimadamore@1581: rs = Resolve.instance(context);
mcimadamore@1347: log = Log.instance(context);
mcimadamore@1347: syms = Symtab.instance(context);
mcimadamore@1347: make = TreeMaker.instance(context);
mcimadamore@1347: types = Types.instance(context);
vromero@2370: flow = Flow.instance(context);
vromero@2370: names = Names.instance(context);
mcimadamore@1889: stuckTree = make.Ident(names.empty).setType(Type.stuckType);
pgovereau@2425: typeEnvs = TypeEnvs.instance(context);
mcimadamore@1897: emptyDeferredAttrContext =
mcimadamore@1897: new DeferredAttrContext(AttrMode.CHECK, null, MethodResolutionPhase.BOX, infer.emptyContext, null, null) {
mcimadamore@1897: @Override
vromero@2000: void addDeferredAttrNode(DeferredType dt, ResultInfo ri, DeferredStuckPolicy deferredStuckPolicy) {
mcimadamore@1897: Assert.error("Empty deferred context!");
mcimadamore@1897: }
mcimadamore@1897: @Override
mcimadamore@1897: void complete() {
mcimadamore@1897: Assert.error("Empty deferred context!");
mcimadamore@1897: }
vromero@2382:
vromero@2382: @Override
vromero@2382: public String toString() {
vromero@2382: return "Empty deferred context!";
vromero@2382: }
mcimadamore@1897: };
mcimadamore@1347: }
mcimadamore@1347:
mcimadamore@1510: /** shared tree for stuck expressions */
mcimadamore@1510: final JCTree stuckTree;
mcimadamore@1510:
mcimadamore@1347: /**
mcimadamore@1347: * This type represents a deferred type. A deferred type starts off with
mcimadamore@1347: * no information on the underlying expression type. Such info needs to be
mcimadamore@1347: * discovered through type-checking the deferred type against a target-type.
mcimadamore@1347: * Every deferred type keeps a pointer to the AST node from which it originated.
mcimadamore@1347: */
mcimadamore@1347: public class DeferredType extends Type {
mcimadamore@1347:
mcimadamore@1347: public JCExpression tree;
mcimadamore@1347: Env env;
mcimadamore@1347: AttrMode mode;
mcimadamore@1347: SpeculativeCache speculativeCache;
mcimadamore@1347:
mcimadamore@1347: DeferredType(JCExpression tree, Env env) {
vromero@1853: super(null);
mcimadamore@1347: this.tree = tree;
mcimadamore@1899: this.env = attr.copyEnv(env);
mcimadamore@1347: this.speculativeCache = new SpeculativeCache();
mcimadamore@1347: }
mcimadamore@1347:
vromero@1853: @Override
vromero@1853: public TypeTag getTag() {
vromero@1853: return DEFERRED;
vromero@1853: }
vromero@1853:
vromero@2370: @Override
vromero@2370: public String toString() {
vromero@2370: return "DeferredType";
vromero@2370: }
vromero@2370:
mcimadamore@1347: /**
mcimadamore@1347: * A speculative cache is used to keep track of all overload resolution rounds
mcimadamore@1347: * that triggered speculative attribution on a given deferred type. Each entry
mcimadamore@1347: * stores a pointer to the speculative tree and the resolution phase in which the entry
mcimadamore@1347: * has been added.
mcimadamore@1347: */
mcimadamore@1347: class SpeculativeCache {
mcimadamore@1347:
mcimadamore@1347: private Map> cache =
mcimadamore@1347: new WeakHashMap>();
mcimadamore@1347:
mcimadamore@1347: class Entry {
mcimadamore@1347: JCTree speculativeTree;
vromero@2000: ResultInfo resultInfo;
mcimadamore@1347:
vromero@2000: public Entry(JCTree speculativeTree, ResultInfo resultInfo) {
mcimadamore@1347: this.speculativeTree = speculativeTree;
vromero@2000: this.resultInfo = resultInfo;
mcimadamore@1347: }
mcimadamore@1347:
vromero@2000: boolean matches(MethodResolutionPhase phase) {
vromero@2000: return resultInfo.checkContext.deferredAttrContext().phase == phase;
mcimadamore@1347: }
mcimadamore@1347: }
mcimadamore@1347:
mcimadamore@1347: /**
mcimadamore@1347: * Retrieve a speculative cache entry corresponding to given symbol
mcimadamore@1347: * and resolution phase
mcimadamore@1347: */
mcimadamore@1347: Entry get(Symbol msym, MethodResolutionPhase phase) {
mcimadamore@1347: List entries = cache.get(msym);
mcimadamore@1347: if (entries == null) return null;
mcimadamore@1347: for (Entry e : entries) {
mcimadamore@1347: if (e.matches(phase)) return e;
mcimadamore@1347: }
mcimadamore@1347: return null;
mcimadamore@1347: }
mcimadamore@1347:
mcimadamore@1347: /**
mcimadamore@1347: * Stores a speculative cache entry corresponding to given symbol
mcimadamore@1347: * and resolution phase
mcimadamore@1347: */
vromero@2000: void put(JCTree speculativeTree, ResultInfo resultInfo) {
vromero@2000: Symbol msym = resultInfo.checkContext.deferredAttrContext().msym;
mcimadamore@1347: List entries = cache.get(msym);
mcimadamore@1347: if (entries == null) {
mcimadamore@1347: entries = List.nil();
mcimadamore@1347: }
vromero@2000: cache.put(msym, entries.prepend(new Entry(speculativeTree, resultInfo)));
mcimadamore@1347: }
mcimadamore@1347: }
mcimadamore@1347:
mcimadamore@1347: /**
mcimadamore@1347: * Get the type that has been computed during a speculative attribution round
mcimadamore@1347: */
mcimadamore@1347: Type speculativeType(Symbol msym, MethodResolutionPhase phase) {
mcimadamore@1347: SpeculativeCache.Entry e = speculativeCache.get(msym, phase);
mcimadamore@1347: return e != null ? e.speculativeTree.type : Type.noType;
mcimadamore@1347: }
mcimadamore@1347:
mcimadamore@1347: /**
mcimadamore@1347: * Check a deferred type against a potential target-type. Depending on
mcimadamore@1347: * the current attribution mode, a normal vs. speculative attribution
mcimadamore@1347: * round is performed on the underlying AST node. There can be only one
mcimadamore@1347: * speculative round for a given target method symbol; moreover, a normal
mcimadamore@1347: * attribution round must follow one or more speculative rounds.
mcimadamore@1347: */
mcimadamore@1347: Type check(ResultInfo resultInfo) {
vromero@2000: DeferredStuckPolicy deferredStuckPolicy;
vromero@2000: if (resultInfo.pt.hasTag(NONE) || resultInfo.pt.isErroneous()) {
vromero@2000: deferredStuckPolicy = dummyStuckPolicy;
vromero@2730: } else if (resultInfo.checkContext.deferredAttrContext().mode == AttrMode.SPECULATIVE ||
vromero@2730: resultInfo.checkContext.deferredAttrContext().insideOverloadPhase()) {
vromero@2000: deferredStuckPolicy = new OverloadStuckPolicy(resultInfo, this);
vromero@2000: } else {
vromero@2000: deferredStuckPolicy = new CheckStuckPolicy(resultInfo, this);
vromero@2000: }
vromero@2000: return check(resultInfo, deferredStuckPolicy, basicCompleter);
mcimadamore@1481: }
mcimadamore@1481:
vromero@2000: private Type check(ResultInfo resultInfo, DeferredStuckPolicy deferredStuckPolicy,
vromero@2000: DeferredTypeCompleter deferredTypeCompleter) {
mcimadamore@1347: DeferredAttrContext deferredAttrContext =
mcimadamore@1347: resultInfo.checkContext.deferredAttrContext();
mcimadamore@1347: Assert.check(deferredAttrContext != emptyDeferredAttrContext);
vromero@2000: if (deferredStuckPolicy.isStuck()) {
vromero@2000: deferredAttrContext.addDeferredAttrNode(this, resultInfo, deferredStuckPolicy);
mcimadamore@1347: return Type.noType;
mcimadamore@1347: } else {
mcimadamore@1347: try {
mcimadamore@1481: return deferredTypeCompleter.complete(this, resultInfo, deferredAttrContext);
mcimadamore@1347: } finally {
mcimadamore@1347: mode = deferredAttrContext.mode;
mcimadamore@1347: }
mcimadamore@1347: }
mcimadamore@1347: }
mcimadamore@1347: }
mcimadamore@1347:
mcimadamore@1347: /**
mcimadamore@1481: * A completer for deferred types. Defines an entry point for type-checking
mcimadamore@1481: * a deferred type.
mcimadamore@1481: */
mcimadamore@1481: interface DeferredTypeCompleter {
mcimadamore@1481: /**
mcimadamore@1481: * Entry point for type-checking a deferred type. Depending on the
mcimadamore@1481: * circumstances, type-checking could amount to full attribution
mcimadamore@1481: * or partial structural check (aka potential applicability).
mcimadamore@1481: */
mcimadamore@1481: Type complete(DeferredType dt, ResultInfo resultInfo, DeferredAttrContext deferredAttrContext);
mcimadamore@1481: }
mcimadamore@1481:
vromero@2000:
mcimadamore@1481: /**
mcimadamore@1481: * A basic completer for deferred types. This completer type-checks a deferred type
mcimadamore@1481: * using attribution; depending on the attribution mode, this could be either standard
mcimadamore@1481: * or speculative attribution.
mcimadamore@1481: */
mcimadamore@1481: DeferredTypeCompleter basicCompleter = new DeferredTypeCompleter() {
mcimadamore@1481: public Type complete(DeferredType dt, ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) {
mcimadamore@1481: switch (deferredAttrContext.mode) {
mcimadamore@1481: case SPECULATIVE:
mcimadamore@1654: //Note: if a symbol is imported twice we might do two identical
mcimadamore@1654: //speculative rounds...
mcimadamore@1654: Assert.check(dt.mode == null || dt.mode == AttrMode.SPECULATIVE);
mcimadamore@1481: JCTree speculativeTree = attribSpeculative(dt.tree, dt.env, resultInfo);
vromero@2000: dt.speculativeCache.put(speculativeTree, resultInfo);
mcimadamore@1481: return speculativeTree.type;
mcimadamore@1481: case CHECK:
mcimadamore@1562: Assert.check(dt.mode != null);
mcimadamore@1481: return attr.attribTree(dt.tree, dt.env, resultInfo);
mcimadamore@1481: }
mcimadamore@1481: Assert.error();
mcimadamore@1481: return null;
mcimadamore@1481: }
mcimadamore@1481: };
mcimadamore@1481:
mcimadamore@1562: DeferredTypeCompleter dummyCompleter = new DeferredTypeCompleter() {
mcimadamore@1562: public Type complete(DeferredType dt, ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) {
mcimadamore@1562: Assert.check(deferredAttrContext.mode == AttrMode.CHECK);
mcimadamore@1899: return dt.tree.type = Type.stuckType;
mcimadamore@1562: }
mcimadamore@1562: };
mcimadamore@1562:
mcimadamore@1481: /**
vromero@2000: * Policy for detecting stuck expressions. Different criteria might cause
vromero@2000: * an expression to be judged as stuck, depending on whether the check
vromero@2000: * is performed during overload resolution or after most specific.
vromero@2000: */
vromero@2000: interface DeferredStuckPolicy {
vromero@2000: /**
vromero@2000: * Has the policy detected that a given expression should be considered stuck?
vromero@2000: */
vromero@2000: boolean isStuck();
vromero@2000: /**
vromero@2000: * Get the set of inference variables a given expression depends upon.
vromero@2000: */
vromero@2000: Set stuckVars();
vromero@2000: /**
vromero@2000: * Get the set of inference variables which might get new constraints
vromero@2000: * if a given expression is being type-checked.
vromero@2000: */
vromero@2000: Set depVars();
vromero@2000: }
vromero@2000:
vromero@2000: /**
vromero@2000: * Basic stuck policy; an expression is never considered to be stuck.
vromero@2000: */
vromero@2000: DeferredStuckPolicy dummyStuckPolicy = new DeferredStuckPolicy() {
vromero@2000: @Override
vromero@2000: public boolean isStuck() {
vromero@2000: return false;
vromero@2000: }
vromero@2000: @Override
vromero@2000: public Set stuckVars() {
vromero@2000: return Collections.emptySet();
vromero@2000: }
vromero@2000: @Override
vromero@2000: public Set depVars() {
vromero@2000: return Collections.emptySet();
vromero@2000: }
vromero@2000: };
vromero@2000:
vromero@2000: /**
mcimadamore@1347: * The 'mode' in which the deferred type is to be type-checked
mcimadamore@1347: */
mcimadamore@1347: public enum AttrMode {
mcimadamore@1347: /**
mcimadamore@1347: * A speculative type-checking round is used during overload resolution
mcimadamore@1347: * mainly to generate constraints on inference variables. Side-effects
mcimadamore@1347: * arising from type-checking the expression associated with the deferred
mcimadamore@1347: * type are reversed after the speculative round finishes. This means the
mcimadamore@1347: * expression tree will be left in a blank state.
mcimadamore@1347: */
mcimadamore@1347: SPECULATIVE,
mcimadamore@1347: /**
mcimadamore@1347: * This is the plain type-checking mode. Produces side-effects on the underlying AST node
mcimadamore@1347: */
mcimadamore@1347: CHECK;
mcimadamore@1347: }
mcimadamore@1347:
mcimadamore@1347: /**
mcimadamore@1347: * Routine that performs speculative type-checking; the input AST node is
mcimadamore@1347: * cloned (to avoid side-effects cause by Attr) and compiler state is
mcimadamore@1347: * restored after type-checking. All diagnostics (but critical ones) are
mcimadamore@1347: * disabled during speculative type-checking.
mcimadamore@1347: */
mcimadamore@1347: JCTree attribSpeculative(JCTree tree, Env env, ResultInfo resultInfo) {
mcimadamore@1596: final JCTree newTree = new TreeCopier