langtools/src/share/classes/com/sun/tools/javac/comp/DeferredAttr.java
author mcimadamore
Fri, 05 Oct 2012 14:35:24 +0100
changeset 14058 c7ec7facdd20
parent 14057 b4b0377b8dba
child 14258 8d2148961366
permissions -rw-r--r--
7177385: Add attribution support for lambda expressions Summary: Add support for function descriptor lookup, functional interface inference and lambda expression type-checking Reviewed-by: jjg, dlsmith
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
     1
/*
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
     4
 *
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    10
 *
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    15
 * accompanied this code).
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    16
 *
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    20
 *
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    23
 * questions.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    24
 */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    25
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    26
package com.sun.tools.javac.comp;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    27
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    28
import com.sun.tools.javac.code.*;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    29
import com.sun.tools.javac.tree.*;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    30
import com.sun.tools.javac.util.*;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    31
import com.sun.tools.javac.code.Symbol.*;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    32
import com.sun.tools.javac.code.Type.*;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    33
import com.sun.tools.javac.comp.Attr.ResultInfo;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    34
import com.sun.tools.javac.comp.Infer.InferenceContext;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    35
import com.sun.tools.javac.comp.Resolve.MethodResolutionPhase;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    36
import com.sun.tools.javac.tree.JCTree.*;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    37
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    38
import javax.tools.JavaFileObject;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    39
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    40
import java.util.ArrayList;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    41
import java.util.HashSet;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    42
import java.util.Map;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    43
import java.util.Queue;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    44
import java.util.Set;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    45
import java.util.WeakHashMap;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    46
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    47
import static com.sun.tools.javac.code.TypeTags.*;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    48
import static com.sun.tools.javac.tree.JCTree.Tag.*;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    49
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    50
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    51
/**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    52
 * This is an helper class that is used to perform deferred type-analysis.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    53
 * Each time a poly expression occurs in argument position, javac attributes it
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    54
 * with a temporary 'deferred type' that is checked (possibly multiple times)
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    55
 * against an expected formal type.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    56
 *
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    57
 *  <p><b>This is NOT part of any supported API.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    58
 *  If you write code that depends on this, you do so at your own risk.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    59
 *  This code and its internal interfaces are subject to change or
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    60
 *  deletion without notice.</b>
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    61
 */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    62
public class DeferredAttr extends JCTree.Visitor {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    63
    protected static final Context.Key<DeferredAttr> deferredAttrKey =
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    64
        new Context.Key<DeferredAttr>();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    65
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    66
    final Attr attr;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    67
    final Check chk;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    68
    final Enter enter;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    69
    final Infer infer;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    70
    final Log log;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    71
    final Symtab syms;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    72
    final TreeMaker make;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    73
    final Types types;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    74
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    75
    public static DeferredAttr instance(Context context) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    76
        DeferredAttr instance = context.get(deferredAttrKey);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    77
        if (instance == null)
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    78
            instance = new DeferredAttr(context);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    79
        return instance;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    80
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    81
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    82
    protected DeferredAttr(Context context) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    83
        context.put(deferredAttrKey, this);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    84
        attr = Attr.instance(context);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    85
        chk = Check.instance(context);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    86
        enter = Enter.instance(context);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    87
        infer = Infer.instance(context);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    88
        log = Log.instance(context);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    89
        syms = Symtab.instance(context);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    90
        make = TreeMaker.instance(context);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    91
        types = Types.instance(context);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    92
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    93
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    94
    /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    95
     * This type represents a deferred type. A deferred type starts off with
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    96
     * no information on the underlying expression type. Such info needs to be
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    97
     * discovered through type-checking the deferred type against a target-type.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    98
     * Every deferred type keeps a pointer to the AST node from which it originated.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    99
     */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   100
    public class DeferredType extends Type {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   101
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   102
        public JCExpression tree;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   103
        Env<AttrContext> env;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   104
        AttrMode mode;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   105
        SpeculativeCache speculativeCache;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   106
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   107
        DeferredType(JCExpression tree, Env<AttrContext> env) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   108
            super(DEFERRED, null);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   109
            this.tree = tree;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   110
            this.env = env.dup(tree, env.info.dup());
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   111
            this.speculativeCache = new SpeculativeCache();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   112
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   113
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   114
        /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   115
         * A speculative cache is used to keep track of all overload resolution rounds
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   116
         * that triggered speculative attribution on a given deferred type. Each entry
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   117
         * stores a pointer to the speculative tree and the resolution phase in which the entry
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   118
         * has been added.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   119
         */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   120
        class SpeculativeCache {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   121
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   122
            private Map<Symbol, List<Entry>> cache =
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   123
                    new WeakHashMap<Symbol, List<Entry>>();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   124
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   125
            class Entry {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   126
                JCTree speculativeTree;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   127
                Resolve.MethodResolutionPhase phase;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   128
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   129
                public Entry(JCTree speculativeTree, MethodResolutionPhase phase) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   130
                    this.speculativeTree = speculativeTree;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   131
                    this.phase = phase;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   132
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   133
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   134
                boolean matches(Resolve.MethodResolutionPhase phase) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   135
                    return this.phase == phase;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   136
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   137
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   138
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   139
            /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   140
             * Clone a speculative cache entry as a fresh entry associated
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   141
             * with a new method (this maybe required to fixup speculative cache
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   142
             * misses after Resolve.access())
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   143
             */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   144
            void dupAllTo(Symbol from, Symbol to) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   145
                Assert.check(cache.get(to) == null);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   146
                List<Entry> entries = cache.get(from);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   147
                if (entries != null) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   148
                    cache.put(to, entries);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   149
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   150
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   151
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   152
            /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   153
             * Retrieve a speculative cache entry corresponding to given symbol
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   154
             * and resolution phase
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   155
             */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   156
            Entry get(Symbol msym, MethodResolutionPhase phase) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   157
                List<Entry> entries = cache.get(msym);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   158
                if (entries == null) return null;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   159
                for (Entry e : entries) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   160
                    if (e.matches(phase)) return e;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   161
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   162
                return null;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   163
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   164
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   165
            /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   166
             * Stores a speculative cache entry corresponding to given symbol
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   167
             * and resolution phase
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   168
             */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   169
            void put(Symbol msym, JCTree speculativeTree, MethodResolutionPhase phase) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   170
                List<Entry> entries = cache.get(msym);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   171
                if (entries == null) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   172
                    entries = List.nil();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   173
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   174
                cache.put(msym, entries.prepend(new Entry(speculativeTree, phase)));
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   175
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   176
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   177
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   178
        /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   179
         * Get the type that has been computed during a speculative attribution round
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   180
         */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   181
        Type speculativeType(Symbol msym, MethodResolutionPhase phase) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   182
            SpeculativeCache.Entry e = speculativeCache.get(msym, phase);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   183
            return e != null ? e.speculativeTree.type : Type.noType;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   184
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   185
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   186
        /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   187
         * Check a deferred type against a potential target-type. Depending on
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   188
         * the current attribution mode, a normal vs. speculative attribution
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   189
         * round is performed on the underlying AST node. There can be only one
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   190
         * speculative round for a given target method symbol; moreover, a normal
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   191
         * attribution round must follow one or more speculative rounds.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   192
         */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   193
        Type check(ResultInfo resultInfo) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   194
            DeferredAttrContext deferredAttrContext =
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   195
                    resultInfo.checkContext.deferredAttrContext();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   196
            Assert.check(deferredAttrContext != emptyDeferredAttrContext);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   197
            List<Type> stuckVars = stuckVars(tree, resultInfo);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   198
            if (stuckVars.nonEmpty()) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   199
                deferredAttrContext.addDeferredAttrNode(this, resultInfo, stuckVars);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   200
                return Type.noType;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   201
            } else {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   202
                try {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   203
                    switch (deferredAttrContext.mode) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   204
                        case SPECULATIVE:
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   205
                            Assert.check(mode == null ||
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   206
                                    (mode == AttrMode.SPECULATIVE &&
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   207
                                    speculativeType(deferredAttrContext.msym, deferredAttrContext.phase).tag == NONE));
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   208
                            JCTree speculativeTree = attribSpeculative(tree, env, resultInfo);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   209
                            speculativeCache.put(deferredAttrContext.msym, speculativeTree, deferredAttrContext.phase);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   210
                            return speculativeTree.type;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   211
                        case CHECK:
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   212
                            Assert.check(mode == AttrMode.SPECULATIVE);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   213
                            return attr.attribTree(tree, env, resultInfo);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   214
                    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   215
                    Assert.error();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   216
                    return null;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   217
                } finally {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   218
                    mode = deferredAttrContext.mode;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   219
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   220
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   221
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   222
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   223
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   224
    /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   225
     * The 'mode' in which the deferred type is to be type-checked
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   226
     */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   227
    public enum AttrMode {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   228
        /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   229
         * A speculative type-checking round is used during overload resolution
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   230
         * mainly to generate constraints on inference variables. Side-effects
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   231
         * arising from type-checking the expression associated with the deferred
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   232
         * type are reversed after the speculative round finishes. This means the
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   233
         * expression tree will be left in a blank state.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   234
         */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   235
        SPECULATIVE,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   236
        /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   237
         * This is the plain type-checking mode. Produces side-effects on the underlying AST node
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   238
         */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   239
        CHECK;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   240
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   241
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   242
    /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   243
     * Routine that performs speculative type-checking; the input AST node is
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   244
     * cloned (to avoid side-effects cause by Attr) and compiler state is
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   245
     * restored after type-checking. All diagnostics (but critical ones) are
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   246
     * disabled during speculative type-checking.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   247
     */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   248
    JCTree attribSpeculative(JCTree tree, Env<AttrContext> env, ResultInfo resultInfo) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   249
        JCTree newTree = new TreeCopier<Object>(make).copy(tree);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   250
        Env<AttrContext> speculativeEnv = env.dup(newTree, env.info.dup(env.info.scope.dupUnshared()));
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   251
        speculativeEnv.info.scope.owner = env.info.scope.owner;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   252
        Filter<JCDiagnostic> prevDeferDiagsFilter = log.deferredDiagFilter;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   253
        Queue<JCDiagnostic> prevDeferredDiags = log.deferredDiagnostics;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   254
        final JavaFileObject currentSource = log.currentSourceFile();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   255
        try {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   256
            log.deferredDiagnostics = new ListBuffer<JCDiagnostic>();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   257
            log.deferredDiagFilter = new Filter<JCDiagnostic>() {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   258
                public boolean accepts(JCDiagnostic t) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   259
                    return t.getDiagnosticSource().getFile().equals(currentSource);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   260
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   261
            };
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   262
            attr.attribTree(newTree, speculativeEnv, resultInfo);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   263
            unenterScanner.scan(newTree);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   264
            return newTree;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   265
        } catch (Abort ex) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   266
            //if some very bad condition occurred during deferred attribution
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   267
            //we should dump all errors before killing javac
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   268
            log.reportDeferredDiagnostics();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   269
            throw ex;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   270
        } finally {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   271
            unenterScanner.scan(newTree);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   272
            log.deferredDiagFilter = prevDeferDiagsFilter;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   273
            log.deferredDiagnostics = prevDeferredDiags;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   274
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   275
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   276
    //where
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   277
        protected TreeScanner unenterScanner = new TreeScanner() {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   278
            @Override
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   279
            public void visitClassDef(JCClassDecl tree) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   280
                ClassSymbol csym = tree.sym;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   281
                enter.typeEnvs.remove(csym);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   282
                chk.compiled.remove(csym.flatname);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   283
                syms.classes.remove(csym.flatname);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   284
                super.visitClassDef(tree);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   285
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   286
        };
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   287
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   288
    /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   289
     * A deferred context is created on each method check. A deferred context is
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   290
     * used to keep track of information associated with the method check, such as
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   291
     * the symbol of the method being checked, the overload resolution phase,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   292
     * the kind of attribution mode to be applied to deferred types and so forth.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   293
     * As deferred types are processed (by the method check routine) stuck AST nodes
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   294
     * are added (as new deferred attribution nodes) to this context. The complete()
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   295
     * routine makes sure that all pending nodes are properly processed, by
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   296
     * progressively instantiating all inference variables on which one or more
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   297
     * deferred attribution node is stuck.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   298
     */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   299
    class DeferredAttrContext {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   300
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   301
        /** attribution mode */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   302
        final AttrMode mode;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   303
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   304
        /** symbol of the method being checked */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   305
        final Symbol msym;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   306
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   307
        /** method resolution step */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   308
        final Resolve.MethodResolutionPhase phase;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   309
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   310
        /** inference context */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   311
        final InferenceContext inferenceContext;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   312
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   313
        /** list of deferred attribution nodes to be processed */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   314
        ArrayList<DeferredAttrNode> deferredAttrNodes = new ArrayList<DeferredAttrNode>();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   315
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   316
        DeferredAttrContext(AttrMode mode, Symbol msym, MethodResolutionPhase phase, InferenceContext inferenceContext) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   317
            this.mode = mode;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   318
            this.msym = msym;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   319
            this.phase = phase;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   320
            this.inferenceContext = inferenceContext;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   321
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   322
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   323
        /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   324
         * Adds a node to the list of deferred attribution nodes - used by Resolve.rawCheckArgumentsApplicable
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   325
         * Nodes added this way act as 'roots' for the out-of-order method checking process.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   326
         */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   327
        void addDeferredAttrNode(final DeferredType dt, ResultInfo resultInfo, List<Type> stuckVars) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   328
            deferredAttrNodes.add(new DeferredAttrNode(dt, resultInfo, stuckVars));
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   329
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   330
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   331
        /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   332
         * Incrementally process all nodes, by skipping 'stuck' nodes and attributing
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   333
         * 'unstuck' ones. If at any point no progress can be made (no 'unstuck' nodes)
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   334
         * some inference variable might get eagerly instantiated so that all nodes
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   335
         * can be type-checked.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   336
         */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   337
        void complete() {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   338
            while (!deferredAttrNodes.isEmpty()) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   339
                Set<Type> stuckVars = new HashSet<Type>();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   340
                boolean progress = false;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   341
                //scan a defensive copy of the node list - this is because a deferred
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   342
                //attribution round can add new nodes to the list
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   343
                for (DeferredAttrNode deferredAttrNode : List.from(deferredAttrNodes)) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   344
                    if (!deferredAttrNode.isStuck()) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   345
                        deferredAttrNode.process();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   346
                        deferredAttrNodes.remove(deferredAttrNode);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   347
                        progress = true;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   348
                    } else {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   349
                        stuckVars.addAll(deferredAttrNode.stuckVars);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   350
                    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   351
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   352
                if (!progress) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   353
                    //remove all variables that have already been instantiated
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   354
                    //from the list of stuck variables
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   355
                    inferenceContext.solveAny(inferenceContext.freeVarsIn(List.from(stuckVars)), types, infer);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   356
                    inferenceContext.notifyChange(types);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   357
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   358
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   359
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   360
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   361
        /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   362
         * Class representing a deferred attribution node. It keeps track of
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   363
         * a deferred type, along with the expected target type information.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   364
         */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   365
        class DeferredAttrNode implements Infer.InferenceContext.FreeTypeListener {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   366
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   367
            /** underlying deferred type */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   368
            DeferredType dt;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   369
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   370
            /** underlying target type information */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   371
            ResultInfo resultInfo;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   372
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   373
            /** list of uninferred inference variables causing this node to be stuck */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   374
            List<Type> stuckVars;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   375
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   376
            DeferredAttrNode(DeferredType dt, ResultInfo resultInfo, List<Type> stuckVars) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   377
                this.dt = dt;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   378
                this.resultInfo = resultInfo;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   379
                this.stuckVars = stuckVars;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   380
                if (!stuckVars.isEmpty()) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   381
                    resultInfo.checkContext.inferenceContext().addFreeTypeListener(stuckVars, this);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   382
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   383
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   384
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   385
            @Override
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   386
            public void typesInferred(InferenceContext inferenceContext) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   387
                stuckVars = List.nil();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   388
                resultInfo = resultInfo.dup(inferenceContext.asInstType(resultInfo.pt, types));
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   389
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   390
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   391
            /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   392
             * is this node stuck?
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   393
             */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   394
            boolean isStuck() {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   395
                return stuckVars.nonEmpty();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   396
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   397
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   398
            /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   399
             * Process a deferred attribution node.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   400
             * Invariant: a stuck node cannot be processed.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   401
             */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   402
            void process() {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   403
                if (isStuck()) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   404
                    throw new IllegalStateException("Cannot process a stuck deferred node");
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   405
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   406
                dt.check(resultInfo);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   407
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   408
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   409
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   410
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   411
    /** an empty deferred attribution context - all methods throw exceptions */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   412
    final DeferredAttrContext emptyDeferredAttrContext =
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   413
            new DeferredAttrContext(null, null, null, null) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   414
                @Override
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   415
                void addDeferredAttrNode(DeferredType dt, ResultInfo ri, List<Type> stuckVars) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   416
                    Assert.error("Empty deferred context!");
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   417
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   418
                @Override
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   419
                void complete() {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   420
                    Assert.error("Empty deferred context!");
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   421
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   422
            };
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   423
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   424
    /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   425
     * Map a list of types possibly containing one or more deferred types
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   426
     * into a list of ordinary types. Each deferred type D is mapped into a type T,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   427
     * where T is computed by retrieving the type that has already been
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   428
     * computed for D during a previous deferred attribution round of the given kind.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   429
     */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   430
    class DeferredTypeMap extends Type.Mapping {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   431
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   432
        DeferredAttrContext deferredAttrContext;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   433
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   434
        protected DeferredTypeMap(AttrMode mode, Symbol msym, MethodResolutionPhase phase) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   435
            super(String.format("deferredTypeMap[%s]", mode));
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   436
            this.deferredAttrContext = new DeferredAttrContext(mode, msym, phase, infer.emptyContext);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   437
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   438
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   439
        protected boolean validState(DeferredType dt) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   440
            return dt.mode != null &&
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   441
                    deferredAttrContext.mode.ordinal() <= dt.mode.ordinal();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   442
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   443
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   444
        @Override
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   445
        public Type apply(Type t) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   446
            if (t.tag != DEFERRED) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   447
                return t.map(this);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   448
            } else {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   449
                DeferredType dt = (DeferredType)t;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   450
                Assert.check(validState(dt));
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   451
                return typeOf(dt);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   452
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   453
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   454
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   455
        protected Type typeOf(DeferredType dt) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   456
            switch (deferredAttrContext.mode) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   457
                case CHECK:
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   458
                    return dt.tree.type == null ? Type.noType : dt.tree.type;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   459
                case SPECULATIVE:
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   460
                    return dt.speculativeType(deferredAttrContext.msym, deferredAttrContext.phase);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   461
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   462
            Assert.error();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   463
            return null;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   464
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   465
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   466
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   467
    /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   468
     * Specialized recovery deferred mapping.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   469
     * Each deferred type D is mapped into a type T, where T is computed either by
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   470
     * (i) retrieving the type that has already been computed for D during a previous
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   471
     * attribution round (as before), or (ii) by synthesizing a new type R for D
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   472
     * (the latter step is useful in a recovery scenario).
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   473
     */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   474
    public class RecoveryDeferredTypeMap extends DeferredTypeMap {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   475
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   476
        public RecoveryDeferredTypeMap(AttrMode mode, Symbol msym, MethodResolutionPhase phase) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   477
            super(mode, msym, phase);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   478
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   479
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   480
        @Override
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   481
        protected Type typeOf(DeferredType dt) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   482
            Type owntype = super.typeOf(dt);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   483
            return owntype.tag == NONE ?
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   484
                        recover(dt) : owntype;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   485
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   486
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   487
        @Override
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   488
        protected boolean validState(DeferredType dt) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   489
            return true;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   490
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   491
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   492
        /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   493
         * Synthesize a type for a deferred type that hasn't been previously
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   494
         * reduced to an ordinary type. Functional deferred types and conditionals
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   495
         * are mapped to themselves, in order to have a richer diagnostic
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   496
         * representation. Remaining deferred types are attributed using
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   497
         * a default expected type (j.l.Object).
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   498
         */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   499
        private Type recover(DeferredType dt) {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   500
            dt.check(attr.new RecoveryInfo(deferredAttrContext));
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   501
            switch (TreeInfo.skipParens(dt.tree).getTag()) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   502
                case LAMBDA:
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   503
                case REFERENCE:
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   504
                case CONDEXPR:
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   505
                    //propagate those deferred types to the
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   506
                    //diagnostic formatter
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   507
                    return dt;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   508
                default:
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   509
                    return super.apply(dt);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   510
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   511
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   512
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   513
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   514
    /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   515
     * Retrieves the list of inference variables that need to be inferred before
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   516
     * an AST node can be type-checked
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   517
     */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   518
    @SuppressWarnings("fallthrough")
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   519
    List<Type> stuckVars(JCTree tree, ResultInfo resultInfo) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   520
        if (resultInfo.pt.tag == NONE || resultInfo.pt.isErroneous()) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   521
            return List.nil();
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   522
        } else {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   523
            StuckChecker sc = new StuckChecker(resultInfo);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   524
            sc.scan(tree);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   525
            return List.from(sc.stuckVars);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   526
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   527
    }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   528
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   529
    /**
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   530
     * This visitor is used to check that structural expressions conform
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   531
     * to their target - this step is required as inference could end up
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   532
     * inferring types that make some of the nested expressions incompatible
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   533
     * with their corresponding instantiated target
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   534
     */
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   535
    class StuckChecker extends TreeScanner {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   536
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   537
        Type pt;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   538
        Filter<JCTree> treeFilter;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   539
        Infer.InferenceContext inferenceContext;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   540
        Set<Type> stuckVars = new HashSet<Type>();
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   541
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   542
        final Filter<JCTree> argsFilter = new Filter<JCTree>() {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   543
            public boolean accepts(JCTree t) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   544
                switch (t.getTag()) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   545
                    case CONDEXPR:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   546
                    case LAMBDA:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   547
                    case PARENS:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   548
                    case REFERENCE:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   549
                        return true;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   550
                    default:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   551
                        return false;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   552
                }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   553
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   554
        };
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   555
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   556
        final Filter<JCTree> lambdaBodyFilter = new Filter<JCTree>() {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   557
            public boolean accepts(JCTree t) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   558
                switch (t.getTag()) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   559
                    case BLOCK: case CASE: case CATCH: case DOLOOP:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   560
                    case FOREACHLOOP: case FORLOOP: case RETURN:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   561
                    case SYNCHRONIZED: case SWITCH: case TRY: case WHILELOOP:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   562
                        return true;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   563
                    default:
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   564
                        return false;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   565
                }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   566
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   567
        };
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   568
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   569
        StuckChecker(ResultInfo resultInfo) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   570
            this.pt = resultInfo.pt;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   571
            this.inferenceContext = resultInfo.checkContext.inferenceContext();
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   572
            this.treeFilter = argsFilter;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   573
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   574
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   575
        @Override
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   576
        public void scan(JCTree tree) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   577
            if (tree != null && treeFilter.accepts(tree)) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   578
                super.scan(tree);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   579
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   580
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   581
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   582
        @Override
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   583
        public void visitLambda(JCLambda tree) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   584
            Type prevPt = pt;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   585
            Filter<JCTree> prevFilter = treeFilter;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   586
            try {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   587
                if (inferenceContext.inferenceVars().contains(pt)) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   588
                    stuckVars.add(pt);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   589
                }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   590
                if (!types.isFunctionalInterface(pt.tsym)) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   591
                    return;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   592
                }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   593
                Type descType = types.findDescriptorType(pt);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   594
                List<Type> freeArgVars = inferenceContext.freeVarsIn(descType.getParameterTypes());
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   595
                if (!TreeInfo.isExplicitLambda(tree) &&
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   596
                        freeArgVars.nonEmpty()) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   597
                    stuckVars.addAll(freeArgVars);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   598
                }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   599
                pt = descType.getReturnType();
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   600
                if (tree.getBodyKind() == JCTree.JCLambda.BodyKind.EXPRESSION) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   601
                    scan(tree.getBody());
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   602
                } else {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   603
                    treeFilter = lambdaBodyFilter;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   604
                    super.visitLambda(tree);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   605
                }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   606
            } finally {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   607
                pt = prevPt;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   608
                treeFilter = prevFilter;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   609
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   610
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   611
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   612
        @Override
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   613
        public void visitReference(JCMemberReference tree) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   614
            scan(tree.expr);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   615
            if (inferenceContext.inferenceVars().contains(pt)) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   616
                stuckVars.add(pt);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   617
                return;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   618
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   619
            if (!types.isFunctionalInterface(pt.tsym)) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   620
                return;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   621
            }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   622
            Type descType = types.findDescriptorType(pt);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   623
            List<Type> freeArgVars = inferenceContext.freeVarsIn(descType.getParameterTypes());
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   624
            stuckVars.addAll(freeArgVars);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   625
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   626
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   627
        @Override
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   628
        public void visitReturn(JCReturn tree) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   629
            Filter<JCTree> prevFilter = treeFilter;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   630
            try {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   631
                treeFilter = argsFilter;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   632
                if (tree.expr != null) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   633
                    scan(tree.expr);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   634
                }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   635
            } finally {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   636
                treeFilter = prevFilter;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   637
            }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   638
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   639
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   640
}