langtools/src/share/classes/com/sun/tools/javac/comp/Infer.java
author vromero
Fri, 20 Jun 2014 20:36:54 +0100
changeset 25279 a665d5cbb8c3
parent 24795 ff348aab3b54
child 25844 48eab270456c
permissions -rw-r--r--
8044546: Crash on faulty reduce/lambda Reviewed-by: mcimadamore, dlsmith Contributed-by: maurizio.cimadamore@oracle.com, vicente.romero@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
23124
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
     2
 * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5489
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5489
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5489
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5489
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5489
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.comp;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
    28
import com.sun.tools.javac.tree.JCTree;
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
    29
import com.sun.tools.javac.tree.JCTree.JCTypeCast;
8036
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
    30
import com.sun.tools.javac.tree.TreeInfo;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import com.sun.tools.javac.util.*;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    32
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.tools.javac.util.List;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    34
import com.sun.tools.javac.code.*;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    35
import com.sun.tools.javac.code.Type.*;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    36
import com.sun.tools.javac.code.Type.UndetVar.InferenceBound;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    37
import com.sun.tools.javac.code.Symbol.*;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    38
import com.sun.tools.javac.comp.DeferredAttr.AttrMode;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    39
import com.sun.tools.javac.comp.Infer.GraphSolver.InferenceGraph;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    40
import com.sun.tools.javac.comp.Infer.GraphSolver.InferenceGraph.Node;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    41
import com.sun.tools.javac.comp.Resolve.InapplicableMethodException;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    42
import com.sun.tools.javac.comp.Resolve.VerboseResolutionMode;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
    43
import com.sun.tools.javac.util.GraphUtils.TarjanNode;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    44
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    45
import java.util.ArrayList;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    46
import java.util.Collections;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
    47
import java.util.EnumMap;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    48
import java.util.EnumSet;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
    49
import java.util.HashMap;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    50
import java.util.HashSet;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
    51
import java.util.LinkedHashSet;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
    52
import java.util.Map;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
    53
import java.util.Set;
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
    54
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14258
diff changeset
    55
import static com.sun.tools.javac.code.TypeTag.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
/** Helper class for type parameter inference, used by the attribution phase.
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5846
diff changeset
    59
 *  <p><b>This is NOT part of any supported API.
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5846
diff changeset
    60
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
public class Infer {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
    65
    protected static final Context.Key<Infer> inferKey = new Context.Key<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    67
    Resolve rs;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    68
    Check chk;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    Symtab syms;
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    Types types;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    71
    JCDiagnostic.Factory diags;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10628
diff changeset
    72
    Log log;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    74
    /** should the graph solver be used? */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    75
    boolean allowGraphInference;
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15037
diff changeset
    76
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    public static Infer instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        Infer instance = context.get(inferKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
            instance = new Infer(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
    protected Infer(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        context.put(inferKey, this);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    86
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    87
        rs = Resolve.instance(context);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    88
        chk = Check.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        syms = Symtab.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        types = Types.instance(context);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    91
        diags = JCDiagnostic.Factory.instance(context);
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10628
diff changeset
    92
        log = Log.instance(context);
13440
1ea85052f98d 7187104: Inference cleanup: remove redundant exception classes in Infer.java
mcimadamore
parents: 13438
diff changeset
    93
        inferenceException = new InferenceException(diags);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    94
        Options options = Options.instance(context);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    95
        allowGraphInference = Source.instance(context).allowGraphInference()
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    96
                && options.isUnset("useLegacyInference");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    99
    /** A value for prototypes that admit any type, including polymorphic ones. */
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18395
diff changeset
   100
    public static final Type anyPoly = new JCNoType();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   101
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   102
   /**
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   103
    * This exception class is design to store a list of diagnostics corresponding
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   104
    * to inference errors that can arise during a method applicability check.
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   105
    */
11707
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 11550
diff changeset
   106
    public static class InferenceException extends InapplicableMethodException {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        private static final long serialVersionUID = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   109
        List<JCDiagnostic> messages = List.nil();
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   110
3140
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 3139
diff changeset
   111
        InferenceException(JCDiagnostic.Factory diags) {
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   112
            super(diags);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        }
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   114
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   115
        @Override
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
   116
        InapplicableMethodException setMessage() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
   117
            //no message to set
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
   118
            return this;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
   119
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
   120
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
   121
        @Override
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   122
        InapplicableMethodException setMessage(JCDiagnostic diag) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   123
            messages = messages.append(diag);
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   124
            return this;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   125
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   126
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   127
        @Override
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   128
        public JCDiagnostic getDiagnostic() {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   129
            return messages.head;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   130
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   131
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   132
        void clear() {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   133
            messages = List.nil();
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   134
        }
3140
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 3139
diff changeset
   135
    }
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 3139
diff changeset
   136
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   137
    protected final InferenceException inferenceException;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   139
    // <editor-fold defaultstate="collapsed" desc="Inference routines">
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   140
    /**
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   141
     * Main inference entry point - instantiate a generic method type
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   142
     * using given argument types and (possibly) an expected target-type.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
     */
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   144
    Type instantiateMethod( Env<AttrContext> env,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   145
                            List<Type> tvars,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   146
                            MethodType mt,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   147
                            Attr.ResultInfo resultInfo,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   148
                            MethodSymbol msym,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   149
                            List<Type> argtypes,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   150
                            boolean allowBoxing,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   151
                            boolean useVarargs,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   152
                            Resolve.MethodResolutionContext resolveContext,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   153
                            Warner warn) throws InferenceException {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
        //-System.err.println("instantiateMethod(" + tvars + ", " + mt + ", " + argtypes + ")"); //DEBUG
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   155
        final InferenceContext inferenceContext = new InferenceContext(tvars);  //B0
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   156
        inferenceException.clear();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   157
        try {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   158
            DeferredAttr.DeferredAttrContext deferredAttrContext =
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   159
                        resolveContext.deferredAttrContext(msym, inferenceContext, resultInfo, warn);
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   160
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   161
            resolveContext.methodCheck.argumentsAcceptable(env, deferredAttrContext,   //B2
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   162
                    argtypes, mt.getParameterTypes(), warn);
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14547
diff changeset
   163
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   164
            if (allowGraphInference &&
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   165
                    resultInfo != null &&
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15037
diff changeset
   166
                    !warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   167
                //inject return constraints earlier
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   168
                checkWithinBounds(inferenceContext, warn); //propagation
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   169
                Type newRestype = generateReturnConstraints(env.tree, resultInfo,  //B3
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   170
                        mt, inferenceContext);
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   171
                mt = (MethodType)types.createMethodTypeWithReturn(mt, newRestype);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   172
                //propagate outwards if needed
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   173
                if (resultInfo.checkContext.inferenceContext().free(resultInfo.pt)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   174
                    //propagate inference context outwards and exit
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   175
                    inferenceContext.dupTo(resultInfo.checkContext.inferenceContext());
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   176
                    deferredAttrContext.complete();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   177
                    return mt;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   178
                }
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15037
diff changeset
   179
            }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15037
diff changeset
   180
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14547
diff changeset
   181
            deferredAttrContext.complete();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   183
            // minimize as yet undetermined type variables
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   184
            if (allowGraphInference) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   185
                inferenceContext.solve(warn);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   186
            } else {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   187
                inferenceContext.solveLegacy(true, warn, LegacyInferenceSteps.EQ_LOWER.steps); //minimizeInst
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   188
            }
3778
38a70273507b 6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents: 3140
diff changeset
   189
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
   190
            mt = (MethodType)inferenceContext.asInstType(mt);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   192
            if (!allowGraphInference &&
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   193
                    inferenceContext.restvars().nonEmpty() &&
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   194
                    resultInfo != null &&
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   195
                    !warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED)) {
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   196
                generateReturnConstraints(env.tree, resultInfo, mt, inferenceContext);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   197
                inferenceContext.solveLegacy(false, warn, LegacyInferenceSteps.EQ_UPPER.steps); //maximizeInst
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   198
                mt = (MethodType)inferenceContext.asInstType(mt);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   199
            }
3140
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 3139
diff changeset
   200
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   201
            if (resultInfo != null && rs.verboseResolutionMode.contains(VerboseResolutionMode.DEFERRED_INST)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   202
                log.note(env.tree.pos, "deferred.method.inst", msym, mt, resultInfo.pt);
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12468
diff changeset
   203
            }
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   204
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   205
            // return instantiated version of method type
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   206
            return mt;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   207
        } finally {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   208
            if (resultInfo != null || !allowGraphInference) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   209
                inferenceContext.notifyChange();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   210
            } else {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   211
                inferenceContext.notifyChange(inferenceContext.boundedVars());
14048
308d1cf8fe46 7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents: 14047
diff changeset
   212
            }
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   213
            if (resultInfo == null) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   214
                /* if the is no result info then we can clear the capture types
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   215
                 * cache without affecting any result info check
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   216
                 */
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   217
                inferenceContext.captureTypeCache.clear();
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   218
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
        }
8616
5a47f5535883 7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents: 8229
diff changeset
   220
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   222
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   223
     * Generate constraints from the generic method's return type. If the method
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   224
     * call occurs in a context where a type T is expected, use the expected
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   225
     * type to derive more constraints on the generic method inference variables.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   226
     */
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   227
    Type generateReturnConstraints(JCTree tree, Attr.ResultInfo resultInfo,
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   228
            MethodType mt, InferenceContext inferenceContext) {
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23124
diff changeset
   229
        InferenceContext rsInfoInfContext = resultInfo.checkContext.inferenceContext();
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   230
        Type from = mt.getReturnType();
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   231
        if (mt.getReturnType().containsAny(inferenceContext.inferencevars) &&
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23124
diff changeset
   232
                rsInfoInfContext != emptyContext) {
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   233
            from = types.capture(from);
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   234
            //add synthetic captured ivars
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   235
            for (Type t : from.getTypeArguments()) {
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   236
                if (t.hasTag(TYPEVAR) && ((TypeVar)t).isCaptured()) {
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   237
                    inferenceContext.addVar((TypeVar)t);
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   238
                }
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   239
            }
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   240
        }
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   241
        Type qtype = inferenceContext.asUndetVar(from);
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   242
        Type to = resultInfo.pt;
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   243
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   244
        if (qtype.hasTag(VOID)) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   245
            to = syms.voidType;
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   246
        } else if (to.hasTag(NONE)) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   247
            to = from.isPrimitive() ? from : syms.objectType;
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   248
        } else if (qtype.hasTag(UNDETVAR)) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   249
            if (resultInfo.pt.isReference()) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   250
                to = generateReturnConstraintsUndetVarToReference(
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   251
                        tree, (UndetVar)qtype, to, resultInfo, inferenceContext);
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   252
            } else {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   253
                if (to.isPrimitive()) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   254
                    to = generateReturnConstraintsPrimitive(tree, (UndetVar)qtype, to,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   255
                        resultInfo, inferenceContext);
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   256
                }
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   257
            }
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   258
        }
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23124
diff changeset
   259
        Assert.check(allowGraphInference || !rsInfoInfContext.free(to),
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   260
                "legacy inference engine cannot handle constraints on both sides of a subtyping assertion");
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   261
        //we need to skip capture?
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   262
        Warner retWarn = new Warner();
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   263
        if (!resultInfo.checkContext.compatible(qtype, rsInfoInfContext.asUndetVar(to), retWarn) ||
18008
6d75e3886bac 8015505: Spurious inference error when return type of generic method requires unchecked conversion to target
mcimadamore
parents: 16967
diff changeset
   264
                //unchecked conversion is not allowed in source 7 mode
6d75e3886bac 8015505: Spurious inference error when return type of generic method requires unchecked conversion to target
mcimadamore
parents: 16967
diff changeset
   265
                (!allowGraphInference && retWarn.hasLint(Lint.LintCategory.UNCHECKED))) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   266
            throw inferenceException
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   267
                    .setMessage("infer.no.conforming.instance.exists",
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   268
                    inferenceContext.restvars(), mt.getReturnType(), to);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   269
        }
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   270
        return from;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   271
    }
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   272
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   273
    private Type generateReturnConstraintsPrimitive(JCTree tree, UndetVar from,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   274
            Type to, Attr.ResultInfo resultInfo, InferenceContext inferenceContext) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   275
        if (!allowGraphInference) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   276
            //if legacy, just return boxed type
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   277
            return types.boxedClass(to).type;
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   278
        }
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   279
        //if graph inference we need to skip conflicting boxed bounds...
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   280
        for (Type t : from.getBounds(InferenceBound.EQ, InferenceBound.UPPER,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   281
                InferenceBound.LOWER)) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   282
            Type boundAsPrimitive = types.unboxedType(t);
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   283
            if (boundAsPrimitive == null || boundAsPrimitive.hasTag(NONE)) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   284
                continue;
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   285
            }
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   286
            return generateReferenceToTargetConstraint(tree, from, to,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   287
                    resultInfo, inferenceContext);
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   288
        }
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   289
        return types.boxedClass(to).type;
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   290
    }
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   291
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   292
    private Type generateReturnConstraintsUndetVarToReference(JCTree tree,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   293
            UndetVar from, Type to, Attr.ResultInfo resultInfo,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   294
            InferenceContext inferenceContext) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   295
        Type captureOfTo = types.capture(to);
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   296
        /* T is a reference type, but is not a wildcard-parameterized type, and either
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   297
         */
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   298
        if (captureOfTo == to) { //not a wildcard parameterized type
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   299
            /* i) B2 contains a bound of one of the forms alpha = S or S <: alpha,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   300
             *      where S is a wildcard-parameterized type, or
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   301
             */
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   302
            for (Type t : from.getBounds(InferenceBound.EQ, InferenceBound.LOWER)) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   303
                Type captureOfBound = types.capture(t);
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   304
                if (captureOfBound != t) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   305
                    return generateReferenceToTargetConstraint(tree, from, to,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   306
                            resultInfo, inferenceContext);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   307
                }
12468
1100643c0209 7154127: Inference cleanup: remove bound check analysis from visitors in Types.java
mcimadamore
parents: 12335
diff changeset
   308
            }
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   309
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   310
            /* ii) B2 contains two bounds of the forms S1 <: alpha and S2 <: alpha,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   311
             * where S1 and S2 have supertypes that are two different
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   312
             * parameterizations of the same generic class or interface.
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   313
             */
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   314
            for (Type aLowerBound : from.getBounds(InferenceBound.LOWER)) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   315
                for (Type anotherLowerBound : from.getBounds(InferenceBound.LOWER)) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   316
                    if (aLowerBound != anotherLowerBound &&
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   317
                        commonSuperWithDiffParameterization(aLowerBound, anotherLowerBound)) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   318
                        /* self comment check if any lower bound may be and undetVar,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   319
                         * in that case the result of this call may be a false positive.
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   320
                         * Should this be restricted to non free types?
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   321
                         */
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   322
                        return generateReferenceToTargetConstraint(tree, from, to,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   323
                            resultInfo, inferenceContext);
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   324
                    }
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   325
                }
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   326
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
        }
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   328
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   329
        /* T is a parameterization of a generic class or interface, G,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   330
         * and B2 contains a bound of one of the forms alpha = S or S <: alpha,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   331
         * where there exists no type of the form G<...> that is a
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   332
         * supertype of S, but the raw type G is a supertype of S
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   333
         */
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   334
        if (to.isParameterized()) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   335
            for (Type t : from.getBounds(InferenceBound.EQ, InferenceBound.LOWER)) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   336
                Type sup = types.asSuper(t, to.tsym);
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   337
                if (sup != null && sup.isRaw()) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   338
                    return generateReferenceToTargetConstraint(tree, from, to,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   339
                            resultInfo, inferenceContext);
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   340
                }
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   341
            }
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   342
        }
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   343
        return to;
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   344
    }
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   345
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   346
    private boolean commonSuperWithDiffParameterization(Type t, Type s) {
24795
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   347
        for (Pair<Type, Type> supers : getParameterizedSupers(t, s)) {
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   348
            if (!types.isSameType(supers.fst, supers.snd)) return true;
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   349
        }
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   350
        return false;
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   351
    }
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   352
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   353
    private Type generateReferenceToTargetConstraint(JCTree tree, UndetVar from,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   354
            Type to, Attr.ResultInfo resultInfo,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   355
            InferenceContext inferenceContext) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   356
        inferenceContext.solve(List.of(from.qtype), new Warner());
25279
a665d5cbb8c3 8044546: Crash on faulty reduce/lambda
vromero
parents: 24795
diff changeset
   357
        inferenceContext.notifyChange();
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   358
        Type capturedType = resultInfo.checkContext.inferenceContext()
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   359
                .cachedCapture(tree, from.inst, false);
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   360
        if (types.isConvertible(capturedType,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   361
                resultInfo.checkContext.inferenceContext().asUndetVar(to))) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   362
            //effectively skip additional return-type constraint generation (compatibility)
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   363
            return syms.objectType;
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   364
        }
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   365
        return to;
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   366
    }
12468
1100643c0209 7154127: Inference cleanup: remove bound check analysis from visitors in Types.java
mcimadamore
parents: 12335
diff changeset
   367
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   368
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   369
      * Infer cyclic inference variables as described in 15.12.2.8.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   370
      */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   371
    private void instantiateAsUninferredVars(List<Type> vars, InferenceContext inferenceContext) {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19914
diff changeset
   372
        ListBuffer<Type> todo = new ListBuffer<>();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   373
        //step 1 - create fresh tvars
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   374
        for (Type t : vars) {
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23124
diff changeset
   375
            UndetVar uv = (UndetVar)inferenceContext.asUndetVar(t);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   376
            List<Type> upperBounds = uv.getBounds(InferenceBound.UPPER);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   377
            if (Type.containsAny(upperBounds, vars)) {
16967
79d444669f3f 7015104: use new subtype of TypeSymbol for type parameters
jfranck
parents: 16805
diff changeset
   378
                TypeSymbol fresh_tvar = new TypeVariableSymbol(Flags.SYNTHETIC, uv.qtype.tsym.name, null, uv.qtype.tsym.owner);
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
   379
                fresh_tvar.type = new TypeVar(fresh_tvar, types.makeCompoundType(uv.getBounds(InferenceBound.UPPER)), null, Type.noAnnotations);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   380
                todo.append(uv);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   381
                uv.inst = fresh_tvar.type;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   382
            } else if (upperBounds.nonEmpty()) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   383
                uv.inst = types.glb(upperBounds);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   384
            } else {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   385
                uv.inst = syms.objectType;
12468
1100643c0209 7154127: Inference cleanup: remove bound check analysis from visitors in Types.java
mcimadamore
parents: 12335
diff changeset
   386
            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   387
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   388
        //step 2 - replace fresh tvars in their bounds
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   389
        List<Type> formals = vars;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   390
        for (Type t : todo) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   391
            UndetVar uv = (UndetVar)t;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   392
            TypeVar ct = (TypeVar)uv.inst;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   393
            ct.bound = types.glb(inferenceContext.asInstTypes(types.getBounds(ct)));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   394
            if (ct.bound.isErroneous()) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   395
                //report inference error if glb fails
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   396
                reportBoundError(uv, BoundErrorKind.BAD_UPPER);
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15037
diff changeset
   397
            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   398
            formals = formals.tail;
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   399
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   400
    }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   401
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   402
    /**
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   403
     * Compute a synthetic method type corresponding to the requested polymorphic
8036
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   404
     * method signature. The target return type is computed from the immediately
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   405
     * enclosing scope surrounding the polymorphic-signature call.
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   406
     */
12335
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
   407
    Type instantiatePolymorphicSignatureInstance(Env<AttrContext> env,
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   408
                                            MethodSymbol spMethod,  // sig. poly. method or null if none
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   409
                                            Resolve.MethodResolutionContext resolveContext,
8036
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   410
                                            List<Type> argtypes) {
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   411
        final Type restype;
8036
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   412
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   413
        //The return type for a polymorphic signature call is computed from
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   414
        //the enclosing tree E, as follows: if E is a cast, then use the
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   415
        //target type of the cast expression as a return type; if E is an
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   416
        //expression statement, the return type is 'void' - otherwise the
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   417
        //return type is simply 'Object'. A correctness check ensures that
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   418
        //env.next refers to the lexically enclosing environment in which
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   419
        //the polymorphic signature call environment is nested.
6934
258e5f06880f 6991980: polymorphic signature calls don't share the same CP entries
mcimadamore
parents: 6710
diff changeset
   420
8036
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   421
        switch (env.next.tree.getTag()) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10816
diff changeset
   422
            case TYPECAST:
8036
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   423
                JCTypeCast castTree = (JCTypeCast)env.next.tree;
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   424
                restype = (TreeInfo.skipParens(castTree.expr) == env.tree) ?
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   425
                    castTree.clazz.type :
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   426
                    syms.objectType;
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   427
                break;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10816
diff changeset
   428
            case EXEC:
8036
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   429
                JCTree.JCExpressionStatement execTree =
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   430
                        (JCTree.JCExpressionStatement)env.next.tree;
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   431
                restype = (TreeInfo.skipParens(execTree.expr) == env.tree) ?
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   432
                    syms.voidType :
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   433
                    syms.objectType;
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   434
                break;
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   435
            default:
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   436
                restype = syms.objectType;
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   437
        }
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   438
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   439
        List<Type> paramtypes = Type.map(argtypes, new ImplicitArgType(spMethod, resolveContext.step));
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   440
        List<Type> exType = spMethod != null ?
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   441
            spMethod.getThrownTypes() :
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   442
            List.of(syms.throwableType); // make it throw all exceptions
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   443
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   444
        MethodType mtype = new MethodType(paramtypes,
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   445
                                          restype,
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   446
                                          exType,
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   447
                                          syms.methodClass);
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   448
        return mtype;
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   449
    }
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   450
    //where
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   451
        class ImplicitArgType extends DeferredAttr.DeferredTypeMap {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   452
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   453
            public ImplicitArgType(Symbol msym, Resolve.MethodResolutionPhase phase) {
25279
a665d5cbb8c3 8044546: Crash on faulty reduce/lambda
vromero
parents: 24795
diff changeset
   454
                (rs.deferredAttr).super(AttrMode.SPECULATIVE, msym, phase);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   455
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   456
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   457
            public Type apply(Type t) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   458
                t = types.erasure(super.apply(t));
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14258
diff changeset
   459
                if (t.hasTag(BOT))
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   460
                    // nulls type as the marker type Null (which has no instances)
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   461
                    // infer as java.lang.Void for now
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   462
                    t = types.boxedClass(syms.voidType).type;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   463
                return t;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   464
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   465
        }
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   466
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   467
    /**
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   468
      * This method is used to infer a suitable target SAM in case the original
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   469
      * SAM type contains one or more wildcards. An inference process is applied
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   470
      * so that wildcard bounds, as well as explicit lambda/method ref parameters
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   471
      * (where applicable) are used to constraint the solution.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   472
      */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   473
    public Type instantiateFunctionalInterface(DiagnosticPosition pos, Type funcInterface,
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   474
            List<Type> paramTypes, Check.CheckContext checkContext) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   475
        if (types.capture(funcInterface) == funcInterface) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   476
            //if capture doesn't change the type then return the target unchanged
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   477
            //(this means the target contains no wildcards!)
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   478
            return funcInterface;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   479
        } else {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   480
            Type formalInterface = funcInterface.tsym.type;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   481
            InferenceContext funcInterfaceContext =
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   482
                    new InferenceContext(funcInterface.tsym.type.getTypeArguments());
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   483
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   484
            Assert.check(paramTypes != null);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   485
            //get constraints from explicit params (this is done by
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   486
            //checking that explicit param types are equal to the ones
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   487
            //in the functional interface descriptors)
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   488
            List<Type> descParameterTypes = types.findDescriptorType(formalInterface).getParameterTypes();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   489
            if (descParameterTypes.size() != paramTypes.size()) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   490
                checkContext.report(pos, diags.fragment("incompatible.arg.types.in.lambda"));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   491
                return types.createErrorType(funcInterface);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   492
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   493
            for (Type p : descParameterTypes) {
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23124
diff changeset
   494
                if (!types.isSameType(funcInterfaceContext.asUndetVar(p), paramTypes.head)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   495
                    checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   496
                    return types.createErrorType(funcInterface);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   497
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   498
                paramTypes = paramTypes.tail;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   499
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   500
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   501
            try {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   502
                funcInterfaceContext.solve(funcInterfaceContext.boundedVars(), types.noWarnings);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   503
            } catch (InferenceException ex) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   504
                checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   505
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   506
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   507
            List<Type> actualTypeargs = funcInterface.getTypeArguments();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   508
            for (Type t : funcInterfaceContext.undetvars) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   509
                UndetVar uv = (UndetVar)t;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   510
                if (uv.inst == null) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   511
                    uv.inst = actualTypeargs.head;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   512
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   513
                actualTypeargs = actualTypeargs.tail;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   514
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   515
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   516
            Type owntype = funcInterfaceContext.asInstType(formalInterface);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   517
            if (!chk.checkValidGenericType(owntype)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   518
                //if the inferred functional interface type is not well-formed,
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   519
                //or if it's not a subtype of the original target, issue an error
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   520
                checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   521
            }
25279
a665d5cbb8c3 8044546: Crash on faulty reduce/lambda
vromero
parents: 24795
diff changeset
   522
            //propagate constraints as per JLS 18.2.1
a665d5cbb8c3 8044546: Crash on faulty reduce/lambda
vromero
parents: 24795
diff changeset
   523
            checkContext.compatible(owntype, funcInterface, types.noWarnings);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   524
            return owntype;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   525
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   526
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   527
    // </editor-fold>
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   528
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   529
    // <editor-fold defaultstate="collapsed" desc="Bound checking">
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   530
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   531
     * Check bounds and perform incorporation
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   532
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   533
    void checkWithinBounds(InferenceContext inferenceContext,
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   534
                             Warner warn) throws InferenceException {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   535
        MultiUndetVarListener mlistener = new MultiUndetVarListener(inferenceContext.undetvars);
18904
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
   536
        List<Type> saved_undet = inferenceContext.save();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   537
        try {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   538
            while (true) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   539
                mlistener.reset();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   540
                if (!allowGraphInference) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   541
                    //in legacy mode we lack of transitivity, so bound check
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   542
                    //cannot be run in parallel with other incoprporation rounds
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   543
                    for (Type t : inferenceContext.undetvars) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   544
                        UndetVar uv = (UndetVar)t;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   545
                        IncorporationStep.CHECK_BOUNDS.apply(uv, inferenceContext, warn);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   546
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   547
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   548
                for (Type t : inferenceContext.undetvars) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   549
                    UndetVar uv = (UndetVar)t;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   550
                    //bound incorporation
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   551
                    EnumSet<IncorporationStep> incorporationSteps = allowGraphInference ?
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   552
                            incorporationStepsGraph : incorporationStepsLegacy;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   553
                    for (IncorporationStep is : incorporationSteps) {
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   554
                        if (is.accepts(uv, inferenceContext)) {
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   555
                            is.apply(uv, inferenceContext, warn);
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   556
                        }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   557
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   558
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   559
                if (!mlistener.changed || !allowGraphInference) break;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   560
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   561
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   562
        finally {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   563
            mlistener.detach();
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   564
            if (incorporationCache.size() == MAX_INCORPORATION_STEPS) {
18904
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
   565
                inferenceContext.rollback(saved_undet);
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
   566
            }
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   567
            incorporationCache.clear();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   568
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   569
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   570
    //where
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   571
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   572
         * This listener keeps track of changes on a group of inference variable
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   573
         * bounds. Note: the listener must be detached (calling corresponding
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   574
         * method) to make sure that the underlying inference variable is
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   575
         * left in a clean state.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   576
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   577
        class MultiUndetVarListener implements UndetVar.UndetVarListener {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   578
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   579
            boolean changed;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   580
            List<Type> undetvars;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   581
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   582
            public MultiUndetVarListener(List<Type> undetvars) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   583
                this.undetvars = undetvars;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   584
                for (Type t : undetvars) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   585
                    UndetVar uv = (UndetVar)t;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   586
                    uv.listener = this;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   587
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   588
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   589
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   590
            public void varChanged(UndetVar uv, Set<InferenceBound> ibs) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   591
                //avoid non-termination
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   592
                if (incorporationCache.size() < MAX_INCORPORATION_STEPS) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   593
                    changed = true;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   594
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   595
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   596
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   597
            void reset() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   598
                changed = false;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   599
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   600
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   601
            void detach() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   602
                for (Type t : undetvars) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   603
                    UndetVar uv = (UndetVar)t;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   604
                    uv.listener = null;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   605
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   606
            }
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
   607
        }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   608
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
   609
    /** max number of incorporation rounds */
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   610
        static final int MAX_INCORPORATION_STEPS = 100;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   611
24795
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   612
    /* If for two types t and s there is a least upper bound that contains
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   613
     * parameterized types G1, G2 ... Gn, then there exists supertypes of 't' of the form
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   614
     * G1<T1, ..., Tn>, G2<T1, ..., Tn>, ... Gn<T1, ..., Tn> and supertypes of 's' of the form
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   615
     * G1<S1, ..., Sn>, G2<S1, ..., Sn>, ... Gn<S1, ..., Sn> which will be returned by this method.
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   616
     * If no such common supertypes exists then an empty list is returned.
24063
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   617
     *
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   618
     * As an example for the following input:
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   619
     *
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   620
     * t = java.util.ArrayList<java.lang.String>
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   621
     * s = java.util.List<T>
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   622
     *
24795
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   623
     * we get this ouput (singleton list):
24063
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   624
     *
24795
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   625
     * [Pair[java.util.List<java.lang.String>,java.util.List<T>]]
24063
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   626
     */
24795
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   627
    private List<Pair<Type, Type>> getParameterizedSupers(Type t, Type s) {
24063
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   628
        Type lubResult = types.lub(t, s);
24795
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   629
        if (lubResult == syms.errType || lubResult == syms.botType) {
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   630
            return List.nil();
24063
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   631
        }
24795
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   632
        List<Type> supertypesToCheck = lubResult.isCompound() ?
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   633
                ((IntersectionClassType)lubResult).getComponents() :
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   634
                List.of(lubResult);
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   635
        ListBuffer<Pair<Type, Type>> commonSupertypes = new ListBuffer<>();
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   636
        for (Type sup : supertypesToCheck) {
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   637
            if (sup.isParameterized()) {
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   638
                Type asSuperOfT = types.asSuper(t, sup.tsym);
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   639
                Type asSuperOfS = types.asSuper(s, sup.tsym);
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   640
                commonSupertypes.add(new Pair<>(asSuperOfT, asSuperOfS));
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   641
            }
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   642
        }
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   643
        return commonSupertypes.toList();
24063
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   644
    }
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   645
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   646
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   647
     * This enumeration defines an entry point for doing inference variable
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   648
     * bound incorporation - it can be used to inject custom incorporation
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   649
     * logic into the basic bound checking routine
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   650
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   651
    enum IncorporationStep {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   652
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   653
         * Performs basic bound checking - i.e. is the instantiated type for a given
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   654
         * inference variable compatible with its bounds?
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   655
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   656
        CHECK_BOUNDS() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   657
            public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   658
                Infer infer = inferenceContext.infer();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   659
                uv.substBounds(inferenceContext.inferenceVars(), inferenceContext.instTypes(), infer.types);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   660
                infer.checkCompatibleUpperBounds(uv, inferenceContext);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   661
                if (uv.inst != null) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   662
                    Type inst = uv.inst;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   663
                    for (Type u : uv.getBounds(InferenceBound.UPPER)) {
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23124
diff changeset
   664
                        if (!isSubtype(inst, inferenceContext.asUndetVar(u), warn, infer)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   665
                            infer.reportBoundError(uv, BoundErrorKind.UPPER);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   666
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   667
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   668
                    for (Type l : uv.getBounds(InferenceBound.LOWER)) {
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23124
diff changeset
   669
                        if (!isSubtype(inferenceContext.asUndetVar(l), inst, warn, infer)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   670
                            infer.reportBoundError(uv, BoundErrorKind.LOWER);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   671
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   672
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   673
                    for (Type e : uv.getBounds(InferenceBound.EQ)) {
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23124
diff changeset
   674
                        if (!isSameType(inst, inferenceContext.asUndetVar(e), infer)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   675
                            infer.reportBoundError(uv, BoundErrorKind.EQ);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   676
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   677
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   678
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   679
            }
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23124
diff changeset
   680
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   681
            @Override
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   682
            boolean accepts(UndetVar uv, InferenceContext inferenceContext) {
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   683
                //applies to all undetvars
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   684
                return true;
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   685
            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   686
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   687
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   688
         * Check consistency of equality constraints. This is a slightly more aggressive
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   689
         * inference routine that is designed as to maximize compatibility with JDK 7.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   690
         * Note: this is not used in graph mode.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   691
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   692
        EQ_CHECK_LEGACY() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   693
            public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   694
                Infer infer = inferenceContext.infer();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   695
                Type eq = null;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   696
                for (Type e : uv.getBounds(InferenceBound.EQ)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   697
                    Assert.check(!inferenceContext.free(e));
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   698
                    if (eq != null && !isSameType(e, eq, infer)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   699
                        infer.reportBoundError(uv, BoundErrorKind.EQ);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   700
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   701
                    eq = e;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   702
                    for (Type l : uv.getBounds(InferenceBound.LOWER)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   703
                        Assert.check(!inferenceContext.free(l));
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   704
                        if (!isSubtype(l, e, warn, infer)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   705
                            infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_LOWER);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   706
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   707
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   708
                    for (Type u : uv.getBounds(InferenceBound.UPPER)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   709
                        if (inferenceContext.free(u)) continue;
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   710
                        if (!isSubtype(e, u, warn, infer)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   711
                            infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_UPPER);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   712
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   713
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   714
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   715
            }
23124
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   716
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   717
            @Override
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   718
            boolean accepts(UndetVar uv, InferenceContext inferenceContext) {
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   719
                return !uv.isCaptured() && uv.getBounds(InferenceBound.EQ).nonEmpty();
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   720
            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   721
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   722
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   723
         * Check consistency of equality constraints.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   724
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   725
        EQ_CHECK() {
23124
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   726
            @Override
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   727
            public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   728
                Infer infer = inferenceContext.infer();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   729
                for (Type e : uv.getBounds(InferenceBound.EQ)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   730
                    if (e.containsAny(inferenceContext.inferenceVars())) continue;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   731
                    for (Type u : uv.getBounds(InferenceBound.UPPER)) {
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23124
diff changeset
   732
                        if (!isSubtype(e, inferenceContext.asUndetVar(u), warn, infer)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   733
                            infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_UPPER);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   734
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   735
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   736
                    for (Type l : uv.getBounds(InferenceBound.LOWER)) {
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23124
diff changeset
   737
                        if (!isSubtype(inferenceContext.asUndetVar(l), e, warn, infer)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   738
                            infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_LOWER);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   739
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   740
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   741
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   742
            }
23124
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   743
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   744
            @Override
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   745
            boolean accepts(UndetVar uv, InferenceContext inferenceContext) {
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   746
                return !uv.isCaptured() && uv.getBounds(InferenceBound.EQ).nonEmpty();
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   747
            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   748
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   749
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   750
         * Given a bound set containing {@code alpha <: T} and {@code alpha :> S}
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   751
         * perform {@code S <: T} (which could lead to new bounds).
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   752
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   753
        CROSS_UPPER_LOWER() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   754
            public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   755
                Infer infer = inferenceContext.infer();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   756
                for (Type b1 : uv.getBounds(InferenceBound.UPPER)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   757
                    for (Type b2 : uv.getBounds(InferenceBound.LOWER)) {
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23124
diff changeset
   758
                        isSubtype(inferenceContext.asUndetVar(b2), inferenceContext.asUndetVar(b1), warn , infer);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   759
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   760
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   761
            }
23124
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   762
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   763
            @Override
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   764
            boolean accepts(UndetVar uv, InferenceContext inferenceContext) {
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   765
                return !uv.isCaptured() &&
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   766
                        uv.getBounds(InferenceBound.UPPER).nonEmpty() &&
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   767
                        uv.getBounds(InferenceBound.LOWER).nonEmpty();
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   768
            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   769
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   770
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   771
         * Given a bound set containing {@code alpha <: T} and {@code alpha == S}
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   772
         * perform {@code S <: T} (which could lead to new bounds).
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   773
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   774
        CROSS_UPPER_EQ() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   775
            public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   776
                Infer infer = inferenceContext.infer();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   777
                for (Type b1 : uv.getBounds(InferenceBound.UPPER)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   778
                    for (Type b2 : uv.getBounds(InferenceBound.EQ)) {
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23124
diff changeset
   779
                        isSubtype(inferenceContext.asUndetVar(b2), inferenceContext.asUndetVar(b1), warn, infer);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   780
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   781
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   782
            }
23124
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   783
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   784
            @Override
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   785
            boolean accepts(UndetVar uv, InferenceContext inferenceContext) {
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   786
                return !uv.isCaptured() &&
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   787
                        uv.getBounds(InferenceBound.EQ).nonEmpty() &&
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   788
                        uv.getBounds(InferenceBound.UPPER).nonEmpty();
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   789
            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   790
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   791
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   792
         * Given a bound set containing {@code alpha :> S} and {@code alpha == T}
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   793
         * perform {@code S <: T} (which could lead to new bounds).
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   794
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   795
        CROSS_EQ_LOWER() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   796
            public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   797
                Infer infer = inferenceContext.infer();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   798
                for (Type b1 : uv.getBounds(InferenceBound.EQ)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   799
                    for (Type b2 : uv.getBounds(InferenceBound.LOWER)) {
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23124
diff changeset
   800
                        isSubtype(inferenceContext.asUndetVar(b2), inferenceContext.asUndetVar(b1), warn, infer);
16567
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16341
diff changeset
   801
                    }
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16341
diff changeset
   802
                }
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16341
diff changeset
   803
            }
23124
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   804
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   805
            @Override
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   806
            boolean accepts(UndetVar uv, InferenceContext inferenceContext) {
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   807
                return !uv.isCaptured() &&
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   808
                        uv.getBounds(InferenceBound.EQ).nonEmpty() &&
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   809
                        uv.getBounds(InferenceBound.LOWER).nonEmpty();
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   810
            }
16567
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16341
diff changeset
   811
        },
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16341
diff changeset
   812
        /**
24063
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   813
         * Given a bound set containing {@code alpha <: P<T>} and
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   814
         * {@code alpha <: P<S>} where P is a parameterized type,
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   815
         * perform {@code T = S} (which could lead to new bounds).
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   816
         */
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   817
        CROSS_UPPER_UPPER() {
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   818
            @Override
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   819
            public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   820
                Infer infer = inferenceContext.infer();
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   821
                List<Type> boundList = uv.getBounds(InferenceBound.UPPER);
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   822
                List<Type> boundListTail = boundList.tail;
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   823
                while (boundList.nonEmpty()) {
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   824
                    List<Type> tmpTail = boundListTail;
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   825
                    while (tmpTail.nonEmpty()) {
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   826
                        Type b1 = boundList.head;
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   827
                        Type b2 = tmpTail.head;
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   828
                        if (b1 != b2) {
24795
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   829
                            for (Pair<Type, Type> commonSupers : infer.getParameterizedSupers(b1, b2)) {
24063
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   830
                                List<Type> allParamsSuperBound1 = commonSupers.fst.allparams();
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   831
                                List<Type> allParamsSuperBound2 = commonSupers.snd.allparams();
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   832
                                while (allParamsSuperBound1.nonEmpty() && allParamsSuperBound2.nonEmpty()) {
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   833
                                    //traverse the list of all params comparing them
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   834
                                    if (!allParamsSuperBound1.head.hasTag(WILDCARD) &&
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   835
                                        !allParamsSuperBound2.head.hasTag(WILDCARD)) {
24795
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   836
                                        if (!isSameType(inferenceContext.asUndetVar(allParamsSuperBound1.head),
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   837
                                            inferenceContext.asUndetVar(allParamsSuperBound2.head), infer)) {
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   838
                                            infer.reportBoundError(uv, BoundErrorKind.BAD_UPPER);
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   839
                                        }
24063
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   840
                                    }
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   841
                                    allParamsSuperBound1 = allParamsSuperBound1.tail;
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   842
                                    allParamsSuperBound2 = allParamsSuperBound2.tail;
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   843
                                }
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   844
                                Assert.check(allParamsSuperBound1.isEmpty() && allParamsSuperBound2.isEmpty());
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   845
                            }
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   846
                        }
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   847
                        tmpTail = tmpTail.tail;
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   848
                    }
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   849
                    boundList = boundList.tail;
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   850
                    boundListTail = boundList.tail;
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   851
                }
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   852
            }
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   853
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   854
            @Override
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   855
            boolean accepts(UndetVar uv, InferenceContext inferenceContext) {
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   856
                return !uv.isCaptured() &&
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   857
                        uv.getBounds(InferenceBound.UPPER).nonEmpty();
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   858
            }
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   859
        },
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
   860
        /**
16567
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16341
diff changeset
   861
         * Given a bound set containing {@code alpha == S} and {@code alpha == T}
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16341
diff changeset
   862
         * perform {@code S == T} (which could lead to new bounds).
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16341
diff changeset
   863
         */
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16341
diff changeset
   864
        CROSS_EQ_EQ() {
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16341
diff changeset
   865
            public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16341
diff changeset
   866
                Infer infer = inferenceContext.infer();
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16341
diff changeset
   867
                for (Type b1 : uv.getBounds(InferenceBound.EQ)) {
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16341
diff changeset
   868
                    for (Type b2 : uv.getBounds(InferenceBound.EQ)) {
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16341
diff changeset
   869
                        if (b1 != b2) {
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23124
diff changeset
   870
                            isSameType(inferenceContext.asUndetVar(b2), inferenceContext.asUndetVar(b1), infer);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   871
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   872
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   873
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   874
            }
23124
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   875
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   876
            @Override
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   877
            boolean accepts(UndetVar uv, InferenceContext inferenceContext) {
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   878
                return !uv.isCaptured() &&
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   879
                        uv.getBounds(InferenceBound.EQ).nonEmpty();
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   880
            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   881
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   882
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   883
         * Given a bound set containing {@code alpha <: beta} propagate lower bounds
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   884
         * from alpha to beta; also propagate upper bounds from beta to alpha.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   885
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   886
        PROP_UPPER() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   887
            public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   888
                Infer infer = inferenceContext.infer();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   889
                for (Type b : uv.getBounds(InferenceBound.UPPER)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   890
                    if (inferenceContext.inferenceVars().contains(b)) {
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23124
diff changeset
   891
                        UndetVar uv2 = (UndetVar)inferenceContext.asUndetVar(b);
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   892
                        if (uv2.isCaptured()) continue;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   893
                        //alpha <: beta
16341
cc4b4e3c0f12 8009545: Graph inference: dependencies between inference variables should be set during incorporation
mcimadamore
parents: 16321
diff changeset
   894
                        //0. set beta :> alpha
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   895
                        addBound(InferenceBound.LOWER, uv2, inferenceContext.asInstType(uv.qtype), infer);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   896
                        //1. copy alpha's lower to beta's
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   897
                        for (Type l : uv.getBounds(InferenceBound.LOWER)) {
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   898
                            addBound(InferenceBound.LOWER, uv2, inferenceContext.asInstType(l), infer);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   899
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   900
                        //2. copy beta's upper to alpha's
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   901
                        for (Type u : uv2.getBounds(InferenceBound.UPPER)) {
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   902
                            addBound(InferenceBound.UPPER, uv, inferenceContext.asInstType(u), infer);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   903
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   904
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   905
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   906
            }
23124
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   907
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   908
            @Override
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   909
            boolean accepts(UndetVar uv, InferenceContext inferenceContext) {
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   910
                return !uv.isCaptured() &&
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   911
                        uv.getBounds(InferenceBound.UPPER).nonEmpty();
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   912
            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   913
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   914
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   915
         * Given a bound set containing {@code alpha :> beta} propagate lower bounds
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   916
         * from beta to alpha; also propagate upper bounds from alpha to beta.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   917
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   918
        PROP_LOWER() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   919
            public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   920
                Infer infer = inferenceContext.infer();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   921
                for (Type b : uv.getBounds(InferenceBound.LOWER)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   922
                    if (inferenceContext.inferenceVars().contains(b)) {
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23124
diff changeset
   923
                        UndetVar uv2 = (UndetVar)inferenceContext.asUndetVar(b);
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   924
                        if (uv2.isCaptured()) continue;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   925
                        //alpha :> beta
16341
cc4b4e3c0f12 8009545: Graph inference: dependencies between inference variables should be set during incorporation
mcimadamore
parents: 16321
diff changeset
   926
                        //0. set beta <: alpha
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   927
                        addBound(InferenceBound.UPPER, uv2, inferenceContext.asInstType(uv.qtype), infer);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   928
                        //1. copy alpha's upper to beta's
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   929
                        for (Type u : uv.getBounds(InferenceBound.UPPER)) {
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   930
                            addBound(InferenceBound.UPPER, uv2, inferenceContext.asInstType(u), infer);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   931
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   932
                        //2. copy beta's lower to alpha's
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   933
                        for (Type l : uv2.getBounds(InferenceBound.LOWER)) {
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   934
                            addBound(InferenceBound.LOWER, uv, inferenceContext.asInstType(l), infer);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   935
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   936
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   937
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   938
            }
23124
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   939
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   940
            @Override
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   941
            boolean accepts(UndetVar uv, InferenceContext inferenceContext) {
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   942
                return !uv.isCaptured() &&
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   943
                        uv.getBounds(InferenceBound.LOWER).nonEmpty();
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   944
            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   945
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   946
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   947
         * Given a bound set containing {@code alpha == beta} propagate lower/upper
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   948
         * bounds from alpha to beta and back.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   949
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   950
        PROP_EQ() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   951
            public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   952
                Infer infer = inferenceContext.infer();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   953
                for (Type b : uv.getBounds(InferenceBound.EQ)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   954
                    if (inferenceContext.inferenceVars().contains(b)) {
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23124
diff changeset
   955
                        UndetVar uv2 = (UndetVar)inferenceContext.asUndetVar(b);
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   956
                        if (uv2.isCaptured()) continue;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   957
                        //alpha == beta
16341
cc4b4e3c0f12 8009545: Graph inference: dependencies between inference variables should be set during incorporation
mcimadamore
parents: 16321
diff changeset
   958
                        //0. set beta == alpha
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   959
                        addBound(InferenceBound.EQ, uv2, inferenceContext.asInstType(uv.qtype), infer);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   960
                        //1. copy all alpha's bounds to beta's
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   961
                        for (InferenceBound ib : InferenceBound.values()) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   962
                            for (Type b2 : uv.getBounds(ib)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   963
                                if (b2 != uv2) {
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   964
                                    addBound(ib, uv2, inferenceContext.asInstType(b2), infer);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   965
                                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   966
                            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   967
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   968
                        //2. copy all beta's bounds to alpha's
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   969
                        for (InferenceBound ib : InferenceBound.values()) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   970
                            for (Type b2 : uv2.getBounds(ib)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   971
                                if (b2 != uv) {
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   972
                                    addBound(ib, uv, inferenceContext.asInstType(b2), infer);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   973
                                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   974
                            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   975
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   976
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   977
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   978
            }
23124
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   979
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   980
            @Override
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   981
            boolean accepts(UndetVar uv, InferenceContext inferenceContext) {
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   982
                return !uv.isCaptured() &&
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   983
                        uv.getBounds(InferenceBound.EQ).nonEmpty();
63050cb08583 8034143: javac, subclasses of Infer.IncorporationStep should implement the accepts() method
vromero
parents: 22165
diff changeset
   984
            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   985
        };
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   986
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   987
        abstract void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn);
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   988
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   989
        boolean accepts(UndetVar uv, InferenceContext inferenceContext) {
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   990
            return !uv.isCaptured();
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   991
        }
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   992
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   993
        boolean isSubtype(Type s, Type t, Warner warn, Infer infer) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   994
            return doIncorporationOp(IncorporationBinaryOpKind.IS_SUBTYPE, s, t, warn, infer);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   995
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   996
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   997
        boolean isSameType(Type s, Type t, Infer infer) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   998
            return doIncorporationOp(IncorporationBinaryOpKind.IS_SAME_TYPE, s, t, null, infer);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   999
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1000
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1001
        void addBound(InferenceBound ib, UndetVar uv, Type b, Infer infer) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1002
            doIncorporationOp(opFor(ib), uv, b, null, infer);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1003
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1004
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1005
        IncorporationBinaryOpKind opFor(InferenceBound boundKind) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1006
            switch (boundKind) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1007
                case EQ:
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1008
                    return IncorporationBinaryOpKind.ADD_EQ_BOUND;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1009
                case LOWER:
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1010
                    return IncorporationBinaryOpKind.ADD_LOWER_BOUND;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1011
                case UPPER:
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1012
                    return IncorporationBinaryOpKind.ADD_UPPER_BOUND;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1013
                default:
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1014
                    Assert.error("Can't get here!");
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1015
                    return null;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1016
            }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1017
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1018
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1019
        boolean doIncorporationOp(IncorporationBinaryOpKind opKind, Type op1, Type op2, Warner warn, Infer infer) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1020
            IncorporationBinaryOp newOp = infer.new IncorporationBinaryOp(opKind, op1, op2);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1021
            Boolean res = infer.incorporationCache.get(newOp);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1022
            if (res == null) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1023
                infer.incorporationCache.put(newOp, res = newOp.apply(warn));
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1024
            }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1025
            return res;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1026
        }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1027
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1028
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1029
    /** incorporation steps to be executed when running in legacy mode */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1030
    EnumSet<IncorporationStep> incorporationStepsLegacy = EnumSet.of(IncorporationStep.EQ_CHECK_LEGACY);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1031
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1032
    /** incorporation steps to be executed when running in graph mode */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1033
    EnumSet<IncorporationStep> incorporationStepsGraph =
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1034
            EnumSet.complementOf(EnumSet.of(IncorporationStep.EQ_CHECK_LEGACY));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1035
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1036
    /**
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1037
     * Three kinds of basic operation are supported as part of an incorporation step:
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1038
     * (i) subtype check, (ii) same type check and (iii) bound addition (either
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1039
     * upper/lower/eq bound).
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1040
     */
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1041
    enum IncorporationBinaryOpKind {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1042
        IS_SUBTYPE() {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1043
            @Override
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1044
            boolean apply(Type op1, Type op2, Warner warn, Types types) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1045
                return types.isSubtypeUnchecked(op1, op2, warn);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1046
            }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1047
        },
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1048
        IS_SAME_TYPE() {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1049
            @Override
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1050
            boolean apply(Type op1, Type op2, Warner warn, Types types) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1051
                return types.isSameType(op1, op2);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1052
            }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1053
        },
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1054
        ADD_UPPER_BOUND() {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1055
            @Override
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1056
            boolean apply(Type op1, Type op2, Warner warn, Types types) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1057
                UndetVar uv = (UndetVar)op1;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1058
                uv.addBound(InferenceBound.UPPER, op2, types);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1059
                return true;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1060
            }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1061
        },
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1062
        ADD_LOWER_BOUND() {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1063
            @Override
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1064
            boolean apply(Type op1, Type op2, Warner warn, Types types) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1065
                UndetVar uv = (UndetVar)op1;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1066
                uv.addBound(InferenceBound.LOWER, op2, types);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1067
                return true;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1068
            }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1069
        },
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1070
        ADD_EQ_BOUND() {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1071
            @Override
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1072
            boolean apply(Type op1, Type op2, Warner warn, Types types) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1073
                UndetVar uv = (UndetVar)op1;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1074
                uv.addBound(InferenceBound.EQ, op2, types);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1075
                return true;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1076
            }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1077
        };
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1078
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1079
        abstract boolean apply(Type op1, Type op2, Warner warn, Types types);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1080
    }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1081
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1082
    /**
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1083
     * This class encapsulates a basic incorporation operation; incorporation
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1084
     * operations takes two type operands and a kind. Each operation performed
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1085
     * during an incorporation round is stored in a cache, so that operations
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1086
     * are not executed unnecessarily (which would potentially lead to adding
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1087
     * same bounds over and over).
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1088
     */
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1089
    class IncorporationBinaryOp {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1090
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1091
        IncorporationBinaryOpKind opKind;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1092
        Type op1;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1093
        Type op2;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1094
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1095
        IncorporationBinaryOp(IncorporationBinaryOpKind opKind, Type op1, Type op2) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1096
            this.opKind = opKind;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1097
            this.op1 = op1;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1098
            this.op2 = op2;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1099
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1100
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1101
        @Override
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1102
        public boolean equals(Object o) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1103
            if (!(o instanceof IncorporationBinaryOp)) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1104
                return false;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1105
            } else {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1106
                IncorporationBinaryOp that = (IncorporationBinaryOp)o;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1107
                return opKind == that.opKind &&
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1108
                        types.isSameType(op1, that.op1, true) &&
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1109
                        types.isSameType(op2, that.op2, true);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1110
            }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1111
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1112
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1113
        @Override
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1114
        public int hashCode() {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1115
            int result = opKind.hashCode();
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1116
            result *= 127;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1117
            result += types.hashCode(op1);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1118
            result *= 127;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1119
            result += types.hashCode(op2);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1120
            return result;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1121
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1122
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1123
        boolean apply(Warner warn) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1124
            return opKind.apply(op1, op2, warn, types);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1125
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1126
    }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1127
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1128
    /** an incorporation cache keeps track of all executed incorporation-related operations */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1129
    Map<IncorporationBinaryOp, Boolean> incorporationCache = new HashMap<>();
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1130
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1131
    /**
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1132
     * Make sure that the upper bounds we got so far lead to a solvable inference
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1133
     * variable by making sure that a glb exists.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1134
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1135
    void checkCompatibleUpperBounds(UndetVar uv, InferenceContext inferenceContext) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1136
        List<Type> hibounds =
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1137
                Type.filter(uv.getBounds(InferenceBound.UPPER), new BoundFilter(inferenceContext));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1138
        Type hb = null;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1139
        if (hibounds.isEmpty())
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1140
            hb = syms.objectType;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1141
        else if (hibounds.tail.isEmpty())
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1142
            hb = hibounds.head;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1143
        else
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1144
            hb = types.glb(hibounds);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1145
        if (hb == null || hb.isErroneous())
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1146
            reportBoundError(uv, BoundErrorKind.BAD_UPPER);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1147
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1148
    //where
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1149
        protected static class BoundFilter implements Filter<Type> {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1150
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1151
            InferenceContext inferenceContext;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1152
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1153
            public BoundFilter(InferenceContext inferenceContext) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1154
                this.inferenceContext = inferenceContext;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1155
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1156
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1157
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1158
            public boolean accepts(Type t) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1159
                return !t.isErroneous() && !inferenceContext.free(t) &&
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1160
                        !t.hasTag(BOT);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1161
            }
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1162
        }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1163
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1164
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1165
     * This enumeration defines all possible bound-checking related errors.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1166
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1167
    enum BoundErrorKind {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1168
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1169
         * The (uninstantiated) inference variable has incompatible upper bounds.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1170
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1171
        BAD_UPPER() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1172
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1173
            InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1174
                return ex.setMessage("incompatible.upper.bounds", uv.qtype,
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1175
                        uv.getBounds(InferenceBound.UPPER));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1176
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1177
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1178
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1179
         * An equality constraint is not compatible with an upper bound.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1180
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1181
        BAD_EQ_UPPER() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1182
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1183
            InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1184
                return ex.setMessage("incompatible.eq.upper.bounds", uv.qtype,
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1185
                        uv.getBounds(InferenceBound.EQ), uv.getBounds(InferenceBound.UPPER));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1186
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1187
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1188
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1189
         * An equality constraint is not compatible with a lower bound.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1190
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1191
        BAD_EQ_LOWER() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1192
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1193
            InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1194
                return ex.setMessage("incompatible.eq.lower.bounds", uv.qtype,
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1195
                        uv.getBounds(InferenceBound.EQ), uv.getBounds(InferenceBound.LOWER));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1196
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1197
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1198
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1199
         * Instantiated inference variable is not compatible with an upper bound.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1200
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1201
        UPPER() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1202
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1203
            InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1204
                return ex.setMessage("inferred.do.not.conform.to.upper.bounds", uv.inst,
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1205
                        uv.getBounds(InferenceBound.UPPER));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1206
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1207
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1208
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1209
         * Instantiated inference variable is not compatible with a lower bound.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1210
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1211
        LOWER() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1212
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1213
            InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1214
                return ex.setMessage("inferred.do.not.conform.to.lower.bounds", uv.inst,
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1215
                        uv.getBounds(InferenceBound.LOWER));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1216
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1217
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1218
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1219
         * Instantiated inference variable is not compatible with an equality constraint.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1220
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1221
        EQ() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1222
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1223
            InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1224
                return ex.setMessage("inferred.do.not.conform.to.eq.bounds", uv.inst,
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1225
                        uv.getBounds(InferenceBound.EQ));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1226
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1227
        };
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1228
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1229
        abstract InapplicableMethodException setMessage(InferenceException ex, UndetVar uv);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1230
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1231
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1232
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1233
     * Report a bound-checking error of given kind
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1234
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1235
    void reportBoundError(UndetVar uv, BoundErrorKind bk) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1236
        throw bk.setMessage(inferenceException, uv);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1237
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1238
    // </editor-fold>
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1239
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1240
    // <editor-fold defaultstate="collapsed" desc="Inference engine">
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1241
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1242
     * Graph inference strategy - act as an input to the inference solver; a strategy is
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1243
     * composed of two ingredients: (i) find a node to solve in the inference graph,
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1244
     * and (ii) tell th engine when we are done fixing inference variables
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1245
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1246
    interface GraphStrategy {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1247
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1248
        /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1249
         * A NodeNotFoundException is thrown whenever an inference strategy fails
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1250
         * to pick the next node to solve in the inference graph.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1251
         */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1252
        public static class NodeNotFoundException extends RuntimeException {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1253
            private static final long serialVersionUID = 0;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1254
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1255
            InferenceGraph graph;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1256
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1257
            public NodeNotFoundException(InferenceGraph graph) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1258
                this.graph = graph;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1259
            }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1260
        }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1261
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1262
         * Pick the next node (leaf) to solve in the graph
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1263
         */
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1264
        Node pickNode(InferenceGraph g) throws NodeNotFoundException;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1265
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1266
         * Is this the last step?
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1267
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1268
        boolean done();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1269
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1270
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1271
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1272
     * Simple solver strategy class that locates all leaves inside a graph
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1273
     * and picks the first leaf as the next node to solve
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1274
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1275
    abstract class LeafSolver implements GraphStrategy {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1276
        public Node pickNode(InferenceGraph g) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1277
            if (g.nodes.isEmpty()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1278
                //should not happen
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1279
                throw new NodeNotFoundException(g);
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1280
            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1281
            return g.nodes.get(0);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1282
        }
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1283
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1284
        boolean isSubtype(Type s, Type t, Warner warn, Infer infer) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1285
            return doIncorporationOp(IncorporationBinaryOpKind.IS_SUBTYPE, s, t, warn, infer);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1286
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1287
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1288
        boolean isSameType(Type s, Type t, Infer infer) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1289
            return doIncorporationOp(IncorporationBinaryOpKind.IS_SAME_TYPE, s, t, null, infer);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1290
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1291
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1292
        void addBound(InferenceBound ib, UndetVar uv, Type b, Infer infer) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1293
            doIncorporationOp(opFor(ib), uv, b, null, infer);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1294
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1295
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1296
        IncorporationBinaryOpKind opFor(InferenceBound boundKind) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1297
            switch (boundKind) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1298
                case EQ:
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1299
                    return IncorporationBinaryOpKind.ADD_EQ_BOUND;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1300
                case LOWER:
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1301
                    return IncorporationBinaryOpKind.ADD_LOWER_BOUND;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1302
                case UPPER:
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1303
                    return IncorporationBinaryOpKind.ADD_UPPER_BOUND;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1304
                default:
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1305
                    Assert.error("Can't get here!");
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1306
                    return null;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1307
            }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1308
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1309
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1310
        boolean doIncorporationOp(IncorporationBinaryOpKind opKind, Type op1, Type op2, Warner warn, Infer infer) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1311
            IncorporationBinaryOp newOp = infer.new IncorporationBinaryOp(opKind, op1, op2);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1312
            Boolean res = infer.incorporationCache.get(newOp);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1313
            if (res == null) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1314
                infer.incorporationCache.put(newOp, res = newOp.apply(warn));
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1315
            }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1316
            return res;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1317
        }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1318
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1319
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1320
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1321
     * This solver uses an heuristic to pick the best leaf - the heuristic
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1322
     * tries to select the node that has maximal probability to contain one
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1323
     * or more inference variables in a given list
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1324
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1325
    abstract class BestLeafSolver extends LeafSolver {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1326
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1327
        /** list of ivars of which at least one must be solved */
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1328
        List<Type> varsToSolve;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1329
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1330
        BestLeafSolver(List<Type> varsToSolve) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1331
            this.varsToSolve = varsToSolve;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1332
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1333
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1334
        /**
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1335
         * Computes a path that goes from a given node to the leafs in the graph.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1336
         * Typically this will start from a node containing a variable in
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1337
         * {@code varsToSolve}. For any given path, the cost is computed as the total
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1338
         * number of type-variables that should be eagerly instantiated across that path.
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1339
         */
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1340
        Pair<List<Node>, Integer> computeTreeToLeafs(Node n) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1341
            Pair<List<Node>, Integer> cachedPath = treeCache.get(n);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1342
            if (cachedPath == null) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1343
                //cache miss
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1344
                if (n.isLeaf()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1345
                    //if leaf, stop
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1346
                    cachedPath = new Pair<>(List.of(n), n.data.length());
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1347
                } else {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1348
                    //if non-leaf, proceed recursively
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1349
                    Pair<List<Node>, Integer> path = new Pair<>(List.of(n), n.data.length());
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1350
                    for (Node n2 : n.getAllDependencies()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1351
                        if (n2 == n) continue;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1352
                        Pair<List<Node>, Integer> subpath = computeTreeToLeafs(n2);
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1353
                        path = new Pair<>(path.fst.prependList(subpath.fst),
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1354
                                          path.snd + subpath.snd);
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1355
                    }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1356
                    cachedPath = path;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1357
                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1358
                //save results in cache
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1359
                treeCache.put(n, cachedPath);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1360
            }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1361
            return cachedPath;
18916
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1362
        }
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1363
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1364
        /** cache used to avoid redundant computation of tree costs */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1365
        final Map<Node, Pair<List<Node>, Integer>> treeCache = new HashMap<>();
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1366
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1367
        /** constant value used to mark non-existent paths */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1368
        final Pair<List<Node>, Integer> noPath = new Pair<>(null, Integer.MAX_VALUE);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1369
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1370
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1371
         * Pick the leaf that minimize cost
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1372
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1373
        @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1374
        public Node pickNode(final InferenceGraph g) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1375
            treeCache.clear(); //graph changes at every step - cache must be cleared
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1376
            Pair<List<Node>, Integer> bestPath = noPath;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1377
            for (Node n : g.nodes) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1378
                if (!Collections.disjoint(n.data, varsToSolve)) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1379
                    Pair<List<Node>, Integer> path = computeTreeToLeafs(n);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1380
                    //discard all paths containing at least a node in the
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1381
                    //closure computed above
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1382
                    if (path.snd < bestPath.snd) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1383
                        bestPath = path;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1384
                    }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1385
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1386
            }
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1387
            if (bestPath == noPath) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1388
                //no path leads there
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1389
                throw new NodeNotFoundException(g);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1390
            }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1391
            return bestPath.fst.head;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1392
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1393
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1394
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1395
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1396
     * The inference process can be thought of as a sequence of steps. Each step
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1397
     * instantiates an inference variable using a subset of the inference variable
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1398
     * bounds, if certain condition are met. Decisions such as the sequence in which
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1399
     * steps are applied, or which steps are to be applied are left to the inference engine.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1400
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1401
    enum InferenceStep {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1402
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1403
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1404
         * Instantiate an inference variables using one of its (ground) equality
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1405
         * constraints
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1406
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1407
        EQ(InferenceBound.EQ) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1408
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1409
            Type solve(UndetVar uv, InferenceContext inferenceContext) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1410
                return filterBounds(uv, inferenceContext).head;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1411
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1412
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1413
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1414
         * Instantiate an inference variables using its (ground) lower bounds. Such
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1415
         * bounds are merged together using lub().
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1416
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1417
        LOWER(InferenceBound.LOWER) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1418
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1419
            Type solve(UndetVar uv, InferenceContext inferenceContext) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1420
                Infer infer = inferenceContext.infer();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1421
                List<Type> lobounds = filterBounds(uv, inferenceContext);
18395
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18008
diff changeset
  1422
                //note: lobounds should have at least one element
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18008
diff changeset
  1423
                Type owntype = lobounds.tail.tail == null  ? lobounds.head : infer.types.lub(lobounds);
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18008
diff changeset
  1424
                if (owntype.isPrimitive() || owntype.hasTag(ERROR)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1425
                    throw infer.inferenceException
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1426
                        .setMessage("no.unique.minimal.instance.exists",
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1427
                                    uv.qtype, lobounds);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1428
                } else {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1429
                    return owntype;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1430
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1431
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1432
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1433
        /**
18909
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1434
         * Infer uninstantiated/unbound inference variables occurring in 'throws'
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1435
         * clause as RuntimeException
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1436
         */
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1437
        THROWS(InferenceBound.UPPER) {
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1438
            @Override
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1439
            public boolean accepts(UndetVar t, InferenceContext inferenceContext) {
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1440
                if ((t.qtype.tsym.flags() & Flags.THROWS) == 0) {
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1441
                    //not a throws undet var
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1442
                    return false;
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1443
                }
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1444
                if (t.getBounds(InferenceBound.EQ, InferenceBound.LOWER, InferenceBound.UPPER)
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1445
                            .diff(t.getDeclaredBounds()).nonEmpty()) {
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1446
                    //not an unbounded undet var
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1447
                    return false;
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1448
                }
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1449
                Infer infer = inferenceContext.infer();
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1450
                for (Type db : t.getDeclaredBounds()) {
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1451
                    if (t.isInterface()) continue;
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1452
                    if (infer.types.asSuper(infer.syms.runtimeExceptionType, db.tsym) != null) {
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1453
                        //declared bound is a supertype of RuntimeException
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1454
                        return true;
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1455
                    }
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1456
                }
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1457
                //declared bound is more specific then RuntimeException - give up
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1458
                return false;
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1459
            }
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1460
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1461
            @Override
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1462
            Type solve(UndetVar uv, InferenceContext inferenceContext) {
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1463
                return inferenceContext.infer().syms.runtimeExceptionType;
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1464
            }
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1465
        },
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1466
        /**
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1467
         * Instantiate an inference variables using its (ground) upper bounds. Such
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1468
         * bounds are merged together using glb().
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1469
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1470
        UPPER(InferenceBound.UPPER) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1471
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1472
            Type solve(UndetVar uv, InferenceContext inferenceContext) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1473
                Infer infer = inferenceContext.infer();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1474
                List<Type> hibounds = filterBounds(uv, inferenceContext);
24293
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24226
diff changeset
  1475
                //note: hibounds should have at least one element
18395
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18008
diff changeset
  1476
                Type owntype = hibounds.tail.tail == null  ? hibounds.head : infer.types.glb(hibounds);
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18008
diff changeset
  1477
                if (owntype.isPrimitive() || owntype.hasTag(ERROR)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1478
                    throw infer.inferenceException
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1479
                        .setMessage("no.unique.maximal.instance.exists",
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1480
                                    uv.qtype, hibounds);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1481
                } else {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1482
                    return owntype;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1483
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1484
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1485
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1486
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1487
         * Like the former; the only difference is that this step can only be applied
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1488
         * if all upper bounds are ground.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1489
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1490
        UPPER_LEGACY(InferenceBound.UPPER) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1491
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1492
            public boolean accepts(UndetVar t, InferenceContext inferenceContext) {
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1493
                return !inferenceContext.free(t.getBounds(ib)) && !t.isCaptured();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1494
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1495
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1496
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1497
            Type solve(UndetVar uv, InferenceContext inferenceContext) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1498
                return UPPER.solve(uv, inferenceContext);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1499
            }
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1500
        },
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1501
        /**
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1502
         * Like the former; the only difference is that this step can only be applied
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1503
         * if all upper/lower bounds are ground.
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1504
         */
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1505
        CAPTURED(InferenceBound.UPPER) {
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1506
            @Override
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1507
            public boolean accepts(UndetVar t, InferenceContext inferenceContext) {
19127
8a0cbd5cb055 8020804: javac crashes when speculative attribution infers intersection type with array component
mcimadamore
parents: 18918
diff changeset
  1508
                return t.isCaptured() &&
8a0cbd5cb055 8020804: javac crashes when speculative attribution infers intersection type with array component
mcimadamore
parents: 18918
diff changeset
  1509
                        !inferenceContext.free(t.getBounds(InferenceBound.UPPER, InferenceBound.LOWER));
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1510
            }
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1511
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1512
            @Override
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1513
            Type solve(UndetVar uv, InferenceContext inferenceContext) {
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1514
                Infer infer = inferenceContext.infer();
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1515
                Type upper = UPPER.filterBounds(uv, inferenceContext).nonEmpty() ?
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1516
                        UPPER.solve(uv, inferenceContext) :
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1517
                        infer.syms.objectType;
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1518
                Type lower = LOWER.filterBounds(uv, inferenceContext).nonEmpty() ?
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1519
                        LOWER.solve(uv, inferenceContext) :
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1520
                        infer.syms.botType;
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1521
                CapturedType prevCaptured = (CapturedType)uv.qtype;
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  1522
                return new CapturedType(prevCaptured.tsym.name, prevCaptured.tsym.owner,
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  1523
                                        upper, lower, prevCaptured.wildcard,
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  1524
                                        Type.noAnnotations);
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1525
            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1526
        };
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1527
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1528
        final InferenceBound ib;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1529
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1530
        InferenceStep(InferenceBound ib) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1531
            this.ib = ib;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1532
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1533
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1534
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1535
         * Find an instantiated type for a given inference variable within
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1536
         * a given inference context
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1537
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1538
        abstract Type solve(UndetVar uv, InferenceContext inferenceContext);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1539
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1540
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1541
         * Can the inference variable be instantiated using this step?
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1542
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1543
        public boolean accepts(UndetVar t, InferenceContext inferenceContext) {
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1544
            return filterBounds(t, inferenceContext).nonEmpty() && !t.isCaptured();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1545
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1546
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1547
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1548
         * Return the subset of ground bounds in a given bound set (i.e. eq/lower/upper)
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1549
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1550
        List<Type> filterBounds(UndetVar uv, InferenceContext inferenceContext) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1551
            return Type.filter(uv.getBounds(ib), new BoundFilter(inferenceContext));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1552
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1553
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1554
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1555
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1556
     * This enumeration defines the sequence of steps to be applied when the
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1557
     * solver works in legacy mode. The steps in this enumeration reflect
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1558
     * the behavior of old inference routine (see JLS SE 7 15.12.2.7/15.12.2.8).
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1559
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1560
    enum LegacyInferenceSteps {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1561
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1562
        EQ_LOWER(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER)),
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1563
        EQ_UPPER(EnumSet.of(InferenceStep.EQ, InferenceStep.UPPER_LEGACY));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1564
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1565
        final EnumSet<InferenceStep> steps;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1566
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1567
        LegacyInferenceSteps(EnumSet<InferenceStep> steps) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1568
            this.steps = steps;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1569
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1570
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1571
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1572
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1573
     * This enumeration defines the sequence of steps to be applied when the
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1574
     * graph solver is used. This order is defined so as to maximize compatibility
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1575
     * w.r.t. old inference routine (see JLS SE 7 15.12.2.7/15.12.2.8).
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1576
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1577
    enum GraphInferenceSteps {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1578
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1579
        EQ(EnumSet.of(InferenceStep.EQ)),
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1580
        EQ_LOWER(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER)),
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1581
        EQ_LOWER_THROWS_UPPER_CAPTURED(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER, InferenceStep.UPPER, InferenceStep.THROWS, InferenceStep.CAPTURED));
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1582
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1583
        final EnumSet<InferenceStep> steps;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1584
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1585
        GraphInferenceSteps(EnumSet<InferenceStep> steps) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1586
            this.steps = steps;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1587
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1588
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1589
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1590
    /**
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1591
     * There are two kinds of dependencies between inference variables. The basic
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1592
     * kind of dependency (or bound dependency) arises when a variable mention
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1593
     * another variable in one of its bounds. There's also a more subtle kind
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1594
     * of dependency that arises when a variable 'might' lead to better constraints
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1595
     * on another variable (this is typically the case with variables holding up
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1596
     * stuck expressions).
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1597
     */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1598
    enum DependencyKind implements GraphUtils.DependencyKind {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1599
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1600
        /** bound dependency */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1601
        BOUND("dotted"),
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1602
        /** stuck dependency */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1603
        STUCK("dashed");
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1604
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1605
        final String dotSyle;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1606
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1607
        private DependencyKind(String dotSyle) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1608
            this.dotSyle = dotSyle;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1609
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1610
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1611
        @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1612
        public String getDotStyle() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1613
            return dotSyle;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1614
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1615
    }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1616
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1617
    /**
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1618
     * This is the graph inference solver - the solver organizes all inference variables in
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1619
     * a given inference context by bound dependencies - in the general case, such dependencies
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1620
     * would lead to a cyclic directed graph (hence the name); the dependency info is used to build
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1621
     * an acyclic graph, where all cyclic variables are bundled together. An inference
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1622
     * step corresponds to solving a node in the acyclic graph - this is done by
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1623
     * relying on a given strategy (see GraphStrategy).
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1624
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1625
    class GraphSolver {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1626
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1627
        InferenceContext inferenceContext;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1628
        Map<Type, Set<Type>> stuckDeps;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1629
        Warner warn;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1630
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1631
        GraphSolver(InferenceContext inferenceContext, Map<Type, Set<Type>> stuckDeps, Warner warn) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1632
            this.inferenceContext = inferenceContext;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1633
            this.stuckDeps = stuckDeps;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1634
            this.warn = warn;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1635
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1636
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1637
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1638
         * Solve variables in a given inference context. The amount of variables
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1639
         * to be solved, and the way in which the underlying acyclic graph is explored
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1640
         * depends on the selected solver strategy.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1641
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1642
        void solve(GraphStrategy sstrategy) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1643
            checkWithinBounds(inferenceContext, warn); //initial propagation of bounds
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1644
            InferenceGraph inferenceGraph = new InferenceGraph(stuckDeps);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1645
            while (!sstrategy.done()) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1646
                InferenceGraph.Node nodeToSolve = sstrategy.pickNode(inferenceGraph);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1647
                List<Type> varsToSolve = List.from(nodeToSolve.data);
18904
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  1648
                List<Type> saved_undet = inferenceContext.save();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1649
                try {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1650
                    //repeat until all variables are solved
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1651
                    outer: while (Type.containsAny(inferenceContext.restvars(), varsToSolve)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1652
                        //for each inference phase
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1653
                        for (GraphInferenceSteps step : GraphInferenceSteps.values()) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1654
                            if (inferenceContext.solveBasic(varsToSolve, step.steps)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1655
                                checkWithinBounds(inferenceContext, warn);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1656
                                continue outer;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1657
                            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1658
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1659
                        //no progress
18395
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18008
diff changeset
  1660
                        throw inferenceException.setMessage();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1661
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1662
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1663
                catch (InferenceException ex) {
18395
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18008
diff changeset
  1664
                    //did we fail because of interdependent ivars?
18904
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  1665
                    inferenceContext.rollback(saved_undet);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1666
                    instantiateAsUninferredVars(varsToSolve, inferenceContext);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1667
                    checkWithinBounds(inferenceContext, warn);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1668
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1669
                inferenceGraph.deleteNode(nodeToSolve);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1670
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1671
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1672
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1673
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1674
         * The dependencies between the inference variables that need to be solved
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1675
         * form a (possibly cyclic) graph. This class reduces the original dependency graph
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1676
         * to an acyclic version, where cyclic nodes are folded into a single 'super node'.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1677
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1678
        class InferenceGraph {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1679
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1680
            /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1681
             * This class represents a node in the graph. Each node corresponds
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1682
             * to an inference variable and has edges (dependencies) on other
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1683
             * nodes. The node defines an entry point that can be used to receive
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1684
             * updates on the structure of the graph this node belongs to (used to
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1685
             * keep dependencies in sync).
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1686
             */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1687
            class Node extends GraphUtils.TarjanNode<ListBuffer<Type>> {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1688
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1689
                /** map listing all dependencies (grouped by kind) */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1690
                EnumMap<DependencyKind, Set<Node>> deps;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1691
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1692
                Node(Type ivar) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1693
                    super(ListBuffer.of(ivar));
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1694
                    this.deps = new EnumMap<>(DependencyKind.class);
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1695
                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1696
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1697
                @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1698
                public GraphUtils.DependencyKind[] getSupportedDependencyKinds() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1699
                    return DependencyKind.values();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1700
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1701
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1702
                @Override
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1703
                public String getDependencyName(GraphUtils.Node<ListBuffer<Type>> to, GraphUtils.DependencyKind dk) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1704
                    if (dk == DependencyKind.STUCK) return "";
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1705
                    else {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1706
                        StringBuilder buf = new StringBuilder();
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1707
                        String sep = "";
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1708
                        for (Type from : data) {
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23124
diff changeset
  1709
                            UndetVar uv = (UndetVar)inferenceContext.asUndetVar(from);
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1710
                            for (Type bound : uv.getBounds(InferenceBound.values())) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1711
                                if (bound.containsAny(List.from(to.data))) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1712
                                    buf.append(sep);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1713
                                    buf.append(bound);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1714
                                    sep = ",";
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1715
                                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1716
                            }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1717
                        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1718
                        return buf.toString();
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1719
                    }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1720
                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1721
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1722
                @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1723
                public Iterable<? extends Node> getAllDependencies() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1724
                    return getDependencies(DependencyKind.values());
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1725
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1726
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1727
                @Override
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1728
                public Iterable<? extends TarjanNode<ListBuffer<Type>>> getDependenciesByKind(GraphUtils.DependencyKind dk) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1729
                    return getDependencies((DependencyKind)dk);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1730
                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1731
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1732
                /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1733
                 * Retrieves all dependencies with given kind(s).
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1734
                 */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1735
                protected Set<Node> getDependencies(DependencyKind... depKinds) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1736
                    Set<Node> buf = new LinkedHashSet<>();
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1737
                    for (DependencyKind dk : depKinds) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1738
                        Set<Node> depsByKind = deps.get(dk);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1739
                        if (depsByKind != null) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1740
                            buf.addAll(depsByKind);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1741
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1742
                    }
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1743
                    return buf;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1744
                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1745
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1746
                /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1747
                 * Adds dependency with given kind.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1748
                 */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1749
                protected void addDependency(DependencyKind dk, Node depToAdd) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1750
                    Set<Node> depsByKind = deps.get(dk);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1751
                    if (depsByKind == null) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1752
                        depsByKind = new LinkedHashSet<>();
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1753
                        deps.put(dk, depsByKind);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1754
                    }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1755
                    depsByKind.add(depToAdd);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1756
                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1757
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1758
                /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1759
                 * Add multiple dependencies of same given kind.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1760
                 */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1761
                protected void addDependencies(DependencyKind dk, Set<Node> depsToAdd) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1762
                    for (Node n : depsToAdd) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1763
                        addDependency(dk, n);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1764
                    }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1765
                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1766
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1767
                /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1768
                 * Remove a dependency, regardless of its kind.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1769
                 */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1770
                protected Set<DependencyKind> removeDependency(Node n) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1771
                    Set<DependencyKind> removedKinds = new HashSet<>();
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1772
                    for (DependencyKind dk : DependencyKind.values()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1773
                        Set<Node> depsByKind = deps.get(dk);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1774
                        if (depsByKind == null) continue;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1775
                        if (depsByKind.remove(n)) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1776
                            removedKinds.add(dk);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1777
                        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1778
                    }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1779
                    return removedKinds;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1780
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1781
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1782
                /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1783
                 * Compute closure of a give node, by recursively walking
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1784
                 * through all its dependencies (of given kinds)
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1785
                 */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1786
                protected Set<Node> closure(DependencyKind... depKinds) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1787
                    boolean progress = true;
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1788
                    Set<Node> closure = new HashSet<>();
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1789
                    closure.add(this);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1790
                    while (progress) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1791
                        progress = false;
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1792
                        for (Node n1 : new HashSet<>(closure)) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1793
                            progress = closure.addAll(n1.getDependencies(depKinds));
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1794
                        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1795
                    }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1796
                    return closure;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1797
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1798
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1799
                /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1800
                 * Is this node a leaf? This means either the node has no dependencies,
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1801
                 * or it just has self-dependencies.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1802
                 */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1803
                protected boolean isLeaf() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1804
                    //no deps, or only one self dep
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1805
                    Set<Node> allDeps = getDependencies(DependencyKind.BOUND, DependencyKind.STUCK);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1806
                    if (allDeps.isEmpty()) return true;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1807
                    for (Node n : allDeps) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1808
                        if (n != this) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1809
                            return false;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1810
                        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1811
                    }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1812
                    return true;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1813
                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1814
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1815
                /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1816
                 * Merge this node with another node, acquiring its dependencies.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1817
                 * This routine is used to merge all cyclic node together and
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1818
                 * form an acyclic graph.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1819
                 */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1820
                protected void mergeWith(List<? extends Node> nodes) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1821
                    for (Node n : nodes) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1822
                        Assert.check(n.data.length() == 1, "Attempt to merge a compound node!");
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1823
                        data.appendList(n.data);
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1824
                        for (DependencyKind dk : DependencyKind.values()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1825
                            addDependencies(dk, n.getDependencies(dk));
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1826
                        }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1827
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1828
                    //update deps
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1829
                    EnumMap<DependencyKind, Set<Node>> deps2 = new EnumMap<>(DependencyKind.class);
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1830
                    for (DependencyKind dk : DependencyKind.values()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1831
                        for (Node d : getDependencies(dk)) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1832
                            Set<Node> depsByKind = deps2.get(dk);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1833
                            if (depsByKind == null) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1834
                                depsByKind = new LinkedHashSet<>();
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1835
                                deps2.put(dk, depsByKind);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1836
                            }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1837
                            if (data.contains(d.data.first())) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1838
                                depsByKind.add(this);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1839
                            } else {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1840
                                depsByKind.add(d);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1841
                            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1842
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1843
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1844
                    deps = deps2;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1845
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1846
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1847
                /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1848
                 * Notify all nodes that something has changed in the graph
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1849
                 * topology.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1850
                 */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1851
                private void graphChanged(Node from, Node to) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1852
                    for (DependencyKind dk : removeDependency(from)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1853
                        if (to != null) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1854
                            addDependency(dk, to);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1855
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1856
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1857
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1858
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1859
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1860
            /** the nodes in the inference graph */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1861
            ArrayList<Node> nodes;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1862
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1863
            InferenceGraph(Map<Type, Set<Type>> optDeps) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1864
                initNodes(optDeps);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1865
            }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1866
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1867
            /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1868
             * Basic lookup helper for retrieving a graph node given an inference
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1869
             * variable type.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1870
             */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1871
            public Node findNode(Type t) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1872
                for (Node n : nodes) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1873
                    if (n.data.contains(t)) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1874
                        return n;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1875
                    }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1876
                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1877
                return null;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1878
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1879
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1880
            /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1881
             * Delete a node from the graph. This update the underlying structure
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1882
             * of the graph (including dependencies) via listeners updates.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1883
             */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1884
            public void deleteNode(Node n) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1885
                Assert.check(nodes.contains(n));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1886
                nodes.remove(n);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1887
                notifyUpdate(n, null);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1888
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1889
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1890
            /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1891
             * Notify all nodes of a change in the graph. If the target node is
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1892
             * {@code null} the source node is assumed to be removed.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1893
             */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1894
            void notifyUpdate(Node from, Node to) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1895
                for (Node n : nodes) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1896
                    n.graphChanged(from, to);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1897
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1898
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1899
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1900
            /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1901
             * Create the graph nodes. First a simple node is created for every inference
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1902
             * variables to be solved. Then Tarjan is used to found all connected components
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1903
             * in the graph. For each component containing more than one node, a super node is
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1904
             * created, effectively replacing the original cyclic nodes.
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1905
             */
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1906
            void initNodes(Map<Type, Set<Type>> stuckDeps) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1907
                //add nodes
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1908
                nodes = new ArrayList<>();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1909
                for (Type t : inferenceContext.restvars()) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1910
                    nodes.add(new Node(t));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1911
                }
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1912
                //add dependencies
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1913
                for (Node n_i : nodes) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1914
                    Type i = n_i.data.first();
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1915
                    Set<Type> optDepsByNode = stuckDeps.get(i);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1916
                    for (Node n_j : nodes) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1917
                        Type j = n_j.data.first();
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23124
diff changeset
  1918
                        UndetVar uv_i = (UndetVar)inferenceContext.asUndetVar(i);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1919
                        if (Type.containsAny(uv_i.getBounds(InferenceBound.values()), List.of(j))) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1920
                            //update i's bound dependencies
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1921
                            n_i.addDependency(DependencyKind.BOUND, n_j);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1922
                        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1923
                        if (optDepsByNode != null && optDepsByNode.contains(j)) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1924
                            //update i's stuck dependencies
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1925
                            n_i.addDependency(DependencyKind.STUCK, n_j);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1926
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1927
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1928
                }
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1929
                //merge cyclic nodes
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1930
                ArrayList<Node> acyclicNodes = new ArrayList<>();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1931
                for (List<? extends Node> conSubGraph : GraphUtils.tarjan(nodes)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1932
                    if (conSubGraph.length() > 1) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1933
                        Node root = conSubGraph.head;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1934
                        root.mergeWith(conSubGraph.tail);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1935
                        for (Node n : conSubGraph) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1936
                            notifyUpdate(n, root);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1937
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1938
                    }
16321
75e64bcd3ab0 8008723: Graph Inference: bad graph calculation leads to assertion error
mcimadamore
parents: 15717
diff changeset
  1939
                    acyclicNodes.add(conSubGraph.head);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1940
                }
16321
75e64bcd3ab0 8008723: Graph Inference: bad graph calculation leads to assertion error
mcimadamore
parents: 15717
diff changeset
  1941
                nodes = acyclicNodes;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1942
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1943
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1944
            /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1945
             * Debugging: dot representation of this graph
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1946
             */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1947
            String toDot() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1948
                StringBuilder buf = new StringBuilder();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1949
                for (Type t : inferenceContext.undetvars) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1950
                    UndetVar uv = (UndetVar)t;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1951
                    buf.append(String.format("var %s - upper bounds = %s, lower bounds = %s, eq bounds = %s\\n",
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1952
                            uv.qtype, uv.getBounds(InferenceBound.UPPER), uv.getBounds(InferenceBound.LOWER),
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1953
                            uv.getBounds(InferenceBound.EQ)));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1954
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1955
                return GraphUtils.toDot(nodes, "inferenceGraph" + hashCode(), buf.toString());
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1956
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1957
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1958
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1959
    // </editor-fold>
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1960
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1961
    // <editor-fold defaultstate="collapsed" desc="Inference context">
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1962
    /**
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1963
     * Functional interface for defining inference callbacks. Certain actions
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1964
     * (i.e. subtyping checks) might need to be redone after all inference variables
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1965
     * have been fixed.
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1966
     */
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1967
    interface FreeTypeListener {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1968
        void typesInferred(InferenceContext inferenceContext);
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1969
    }
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1970
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1971
    /**
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1972
     * An inference context keeps track of the set of variables that are free
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1973
     * in the current context. It provides utility methods for opening/closing
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1974
     * types to their corresponding free/closed forms. It also provide hooks for
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1975
     * attaching deferred post-inference action (see PendingCheck). Finally,
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1976
     * it can be used as an entry point for performing upper/lower bound inference
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1977
     * (see InferenceKind).
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1978
     */
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1979
     class InferenceContext {
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1980
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1981
        /** list of inference vars as undet vars */
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1982
        List<Type> undetvars;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1983
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1984
        /** list of inference vars in this context */
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1985
        List<Type> inferencevars;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1986
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1987
        Map<FreeTypeListener, List<Type>> freeTypeListeners = new HashMap<>();
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1988
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1989
        List<FreeTypeListener> freetypeListeners = List.nil();
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1990
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1991
        public InferenceContext(List<Type> inferencevars) {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1992
            this.undetvars = Type.map(inferencevars, fromTypeVarFun);
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1993
            this.inferencevars = inferencevars;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1994
        }
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1995
        //where
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1996
            Mapping fromTypeVarFun = new Mapping("fromTypeVarFunWithBounds") {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1997
                // mapping that turns inference variables into undet vars
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1998
                public Type apply(Type t) {
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1999
                    if (t.hasTag(TYPEVAR)) {
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  2000
                        TypeVar tv = (TypeVar)t;
21488
4a69e26aa999 8025290: javac implicit versus explicit lambda compilation error
vromero
parents: 20249
diff changeset
  2001
                        if (tv.isCaptured()) {
4a69e26aa999 8025290: javac implicit versus explicit lambda compilation error
vromero
parents: 20249
diff changeset
  2002
                            return new CapturedUndetVar((CapturedType)tv, types);
4a69e26aa999 8025290: javac implicit versus explicit lambda compilation error
vromero
parents: 20249
diff changeset
  2003
                        } else {
4a69e26aa999 8025290: javac implicit versus explicit lambda compilation error
vromero
parents: 20249
diff changeset
  2004
                            return new UndetVar(tv, types);
4a69e26aa999 8025290: javac implicit versus explicit lambda compilation error
vromero
parents: 20249
diff changeset
  2005
                        }
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  2006
                    } else {
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  2007
                        return t.map(this);
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  2008
                    }
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2009
                }
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2010
            };
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2011
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2012
        /**
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  2013
         * add a new inference var to this inference context
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  2014
         */
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  2015
        void addVar(TypeVar t) {
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  2016
            this.undetvars = this.undetvars.prepend(fromTypeVarFun.apply(t));
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  2017
            this.inferencevars = this.inferencevars.prepend(t);
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  2018
        }
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  2019
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  2020
        /**
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2021
         * returns the list of free variables (as type-variables) in this
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2022
         * inference context
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2023
         */
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2024
        List<Type> inferenceVars() {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2025
            return inferencevars;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2026
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2027
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2028
        /**
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2029
         * returns the list of uninstantiated variables (as type-variables) in this
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2030
         * inference context
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2031
         */
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2032
        List<Type> restvars() {
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2033
            return filterVars(new Filter<UndetVar>() {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2034
                public boolean accepts(UndetVar uv) {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2035
                    return uv.inst == null;
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2036
                }
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2037
            });
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2038
        }
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2039
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2040
        /**
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2041
         * returns the list of instantiated variables (as type-variables) in this
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2042
         * inference context
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2043
         */
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2044
        List<Type> instvars() {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2045
            return filterVars(new Filter<UndetVar>() {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2046
                public boolean accepts(UndetVar uv) {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2047
                    return uv.inst != null;
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2048
                }
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2049
            });
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2050
        }
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2051
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2052
        /**
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2053
         * Get list of bounded inference variables (where bound is other than
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2054
         * declared bounds).
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2055
         */
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2056
        final List<Type> boundedVars() {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2057
            return filterVars(new Filter<UndetVar>() {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2058
                public boolean accepts(UndetVar uv) {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2059
                    return uv.getBounds(InferenceBound.UPPER)
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  2060
                             .diff(uv.getDeclaredBounds())
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  2061
                             .appendList(uv.getBounds(InferenceBound.EQ, InferenceBound.LOWER)).nonEmpty();
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2062
                }
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2063
            });
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2064
        }
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2065
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23124
diff changeset
  2066
        /* Returns the corresponding inference variables.
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23124
diff changeset
  2067
         */
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2068
        private List<Type> filterVars(Filter<UndetVar> fu) {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19914
diff changeset
  2069
            ListBuffer<Type> res = new ListBuffer<>();
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2070
            for (Type t : undetvars) {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2071
                UndetVar uv = (UndetVar)t;
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2072
                if (fu.accepts(uv)) {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2073
                    res.append(uv.qtype);
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2074
                }
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2075
            }
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2076
            return res.toList();
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2077
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2078
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2079
        /**
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2080
         * is this type free?
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2081
         */
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2082
        final boolean free(Type t) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2083
            return t.containsAny(inferencevars);
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2084
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2085
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2086
        final boolean free(List<Type> ts) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2087
            for (Type t : ts) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2088
                if (free(t)) return true;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2089
            }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2090
            return false;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2091
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2092
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2093
        /**
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2094
         * Returns a list of free variables in a given type
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2095
         */
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2096
        final List<Type> freeVarsIn(Type t) {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19914
diff changeset
  2097
            ListBuffer<Type> buf = new ListBuffer<>();
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2098
            for (Type iv : inferenceVars()) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2099
                if (t.contains(iv)) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2100
                    buf.add(iv);
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2101
                }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2102
            }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2103
            return buf.toList();
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2104
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2105
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2106
        final List<Type> freeVarsIn(List<Type> ts) {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19914
diff changeset
  2107
            ListBuffer<Type> buf = new ListBuffer<>();
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2108
            for (Type t : ts) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2109
                buf.appendList(freeVarsIn(t));
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2110
            }
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19914
diff changeset
  2111
            ListBuffer<Type> buf2 = new ListBuffer<>();
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2112
            for (Type t : buf) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2113
                if (!buf2.contains(t)) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2114
                    buf2.add(t);
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2115
                }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2116
            }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2117
            return buf2.toList();
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2118
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2119
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2120
        /**
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2121
         * Replace all free variables in a given type with corresponding
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2122
         * undet vars (used ahead of subtyping/compatibility checks to allow propagation
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2123
         * of inference constraints).
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2124
         */
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23124
diff changeset
  2125
        final Type asUndetVar(Type t) {
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2126
            return types.subst(t, inferencevars, undetvars);
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2127
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2128
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23124
diff changeset
  2129
        final List<Type> asUndetVars(List<Type> ts) {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19914
diff changeset
  2130
            ListBuffer<Type> buf = new ListBuffer<>();
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2131
            for (Type t : ts) {
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23124
diff changeset
  2132
                buf.append(asUndetVar(t));
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2133
            }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2134
            return buf.toList();
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2135
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2136
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2137
        List<Type> instTypes() {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19914
diff changeset
  2138
            ListBuffer<Type> buf = new ListBuffer<>();
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2139
            for (Type t : undetvars) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2140
                UndetVar uv = (UndetVar)t;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2141
                buf.append(uv.inst != null ? uv.inst : uv.qtype);
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2142
            }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2143
            return buf.toList();
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2144
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2145
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2146
        /**
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2147
         * Replace all free variables in a given type with corresponding
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2148
         * instantiated types - if one or more free variable has not been
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2149
         * fully instantiated, it will still be available in the resulting type.
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2150
         */
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2151
        Type asInstType(Type t) {
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2152
            return types.subst(t, inferencevars, instTypes());
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2153
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2154
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2155
        List<Type> asInstTypes(List<Type> ts) {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19914
diff changeset
  2156
            ListBuffer<Type> buf = new ListBuffer<>();
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2157
            for (Type t : ts) {
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2158
                buf.append(asInstType(t));
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2159
            }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2160
            return buf.toList();
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2161
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2162
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2163
        /**
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2164
         * Add custom hook for performing post-inference action
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2165
         */
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2166
        void addFreeTypeListener(List<Type> types, FreeTypeListener ftl) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2167
            freeTypeListeners.put(ftl, freeVarsIn(types));
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2168
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2169
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2170
        /**
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2171
         * Mark the inference context as complete and trigger evaluation
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2172
         * of all deferred checks.
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2173
         */
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2174
        void notifyChange() {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2175
            notifyChange(inferencevars.diff(restvars()));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2176
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2177
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2178
        void notifyChange(List<Type> inferredVars) {
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2179
            InferenceException thrownEx = null;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2180
            for (Map.Entry<FreeTypeListener, List<Type>> entry :
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  2181
                    new HashMap<>(freeTypeListeners).entrySet()) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2182
                if (!Type.containsAny(entry.getValue(), inferencevars.diff(inferredVars))) {
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2183
                    try {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2184
                        entry.getKey().typesInferred(this);
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2185
                        freeTypeListeners.remove(entry.getKey());
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2186
                    } catch (InferenceException ex) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2187
                        if (thrownEx == null) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2188
                            thrownEx = ex;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2189
                        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2190
                    }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2191
                }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2192
            }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2193
            //inference exception multiplexing - present any inference exception
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2194
            //thrown when processing listeners as a single one
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2195
            if (thrownEx != null) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2196
                throw thrownEx;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2197
            }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2198
        }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
  2199
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2200
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2201
         * Save the state of this inference context
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2202
         */
18904
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  2203
        List<Type> save() {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19914
diff changeset
  2204
            ListBuffer<Type> buf = new ListBuffer<>();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2205
            for (Type t : undetvars) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2206
                UndetVar uv = (UndetVar)t;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2207
                UndetVar uv2 = new UndetVar((TypeVar)uv.qtype, types);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2208
                for (InferenceBound ib : InferenceBound.values()) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2209
                    for (Type b : uv.getBounds(ib)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2210
                        uv2.addBound(ib, b, types);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2211
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2212
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2213
                uv2.inst = uv.inst;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2214
                buf.add(uv2);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2215
            }
18904
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  2216
            return buf.toList();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2217
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2218
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2219
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2220
         * Restore the state of this inference context to the previous known checkpoint
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2221
         */
18904
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  2222
        void rollback(List<Type> saved_undet) {
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  2223
             Assert.check(saved_undet != null && saved_undet.length() == undetvars.length());
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  2224
            //restore bounds (note: we need to preserve the old instances)
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  2225
            for (Type t : undetvars) {
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  2226
                UndetVar uv = (UndetVar)t;
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  2227
                UndetVar uv_saved = (UndetVar)saved_undet.head;
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  2228
                for (InferenceBound ib : InferenceBound.values()) {
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  2229
                    uv.setBounds(ib, uv_saved.getBounds(ib));
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  2230
                }
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  2231
                uv.inst = uv_saved.inst;
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  2232
                saved_undet = saved_undet.tail;
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  2233
            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2234
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2235
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2236
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2237
         * Copy variable in this inference context to the given context
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2238
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2239
        void dupTo(final InferenceContext that) {
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
  2240
            that.inferencevars = that.inferencevars.appendList(
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
  2241
                    inferencevars.diff(that.inferencevars));
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
  2242
            that.undetvars = that.undetvars.appendList(
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
  2243
                    undetvars.diff(that.undetvars));
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2244
            //set up listeners to notify original inference contexts as
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2245
            //propagated vars are inferred in new context
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2246
            for (Type t : inferencevars) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2247
                that.freeTypeListeners.put(new FreeTypeListener() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2248
                    public void typesInferred(InferenceContext inferenceContext) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2249
                        InferenceContext.this.notifyChange();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2250
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2251
                }, List.of(t));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2252
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2253
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2254
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  2255
        private void solve(GraphStrategy ss, Warner warn) {
22165
ec53c8946fc2 8030807: langtools should still build using jdk 7
vromero
parents: 22163
diff changeset
  2256
            solve(ss, new HashMap<Type, Set<Type>>(), warn);
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  2257
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  2258
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2259
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2260
         * Solve with given graph strategy.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2261
         */
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  2262
        private void solve(GraphStrategy ss, Map<Type, Set<Type>> stuckDeps, Warner warn) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  2263
            GraphSolver s = new GraphSolver(this, stuckDeps, warn);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2264
            s.solve(ss);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2265
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2266
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2267
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2268
         * Solve all variables in this context.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2269
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2270
        public void solve(Warner warn) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2271
            solve(new LeafSolver() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2272
                public boolean done() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2273
                    return restvars().isEmpty();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2274
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2275
            }, warn);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2276
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2277
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2278
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2279
         * Solve all variables in the given list.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2280
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2281
        public void solve(final List<Type> vars, Warner warn) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2282
            solve(new BestLeafSolver(vars) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2283
                public boolean done() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2284
                    return !free(asInstTypes(vars));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2285
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2286
            }, warn);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2287
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2288
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2289
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2290
         * Solve at least one variable in given list.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2291
         */
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  2292
        public void solveAny(List<Type> varsToSolve, Map<Type, Set<Type>> optDeps, Warner warn) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  2293
            solve(new BestLeafSolver(varsToSolve.intersect(restvars())) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2294
                public boolean done() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2295
                    return instvars().intersect(varsToSolve).nonEmpty();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2296
                }
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  2297
            }, optDeps, warn);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2298
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2299
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2300
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2301
         * Apply a set of inference steps
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2302
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2303
        private boolean solveBasic(EnumSet<InferenceStep> steps) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2304
            return solveBasic(inferencevars, steps);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2305
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2306
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2307
        private boolean solveBasic(List<Type> varsToSolve, EnumSet<InferenceStep> steps) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2308
            boolean changed = false;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2309
            for (Type t : varsToSolve.intersect(restvars())) {
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23124
diff changeset
  2310
                UndetVar uv = (UndetVar)asUndetVar(t);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2311
                for (InferenceStep step : steps) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2312
                    if (step.accepts(uv, this)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2313
                        uv.inst = step.solve(uv, this);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2314
                        changed = true;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2315
                        break;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
  2316
                    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
  2317
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
  2318
            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2319
            return changed;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2320
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2321
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2322
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2323
         * Instantiate inference variables in legacy mode (JLS 15.12.2.7, 15.12.2.8).
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2324
         * During overload resolution, instantiation is done by doing a partial
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2325
         * inference process using eq/lower bound instantiation. During check,
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2326
         * we also instantiate any remaining vars by repeatedly using eq/upper
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2327
         * instantiation, until all variables are solved.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2328
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2329
        public void solveLegacy(boolean partial, Warner warn, EnumSet<InferenceStep> steps) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2330
            while (true) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2331
                boolean stuck = !solveBasic(steps);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2332
                if (restvars().isEmpty() || partial) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2333
                    //all variables have been instantiated - exit
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2334
                    break;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2335
                } else if (stuck) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2336
                    //some variables could not be instantiated because of cycles in
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2337
                    //upper bounds - provide a (possibly recursive) default instantiation
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2338
                    instantiateAsUninferredVars(restvars(), this);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2339
                    break;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2340
                } else {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2341
                    //some variables have been instantiated - replace newly instantiated
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2342
                    //variables in remaining upper bounds and continue
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2343
                    for (Type t : undetvars) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2344
                        UndetVar uv = (UndetVar)t;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2345
                        uv.substBounds(inferenceVars(), instTypes(), types);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2346
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2347
                }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
  2348
            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2349
            checkWithinBounds(this, warn);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2350
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2351
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2352
        private Infer infer() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2353
            //back-door to infer
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2354
            return Infer.this;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
  2355
        }
23395
e7e0973e6d2e 8034127: javac provide debug oriented toString() methods to InferenceContext and UndetVar
vromero
parents: 23132
diff changeset
  2356
e7e0973e6d2e 8034127: javac provide debug oriented toString() methods to InferenceContext and UndetVar
vromero
parents: 23132
diff changeset
  2357
        @Override
e7e0973e6d2e 8034127: javac provide debug oriented toString() methods to InferenceContext and UndetVar
vromero
parents: 23132
diff changeset
  2358
        public String toString() {
e7e0973e6d2e 8034127: javac provide debug oriented toString() methods to InferenceContext and UndetVar
vromero
parents: 23132
diff changeset
  2359
            return "Inference vars: " + inferencevars + '\n' +
e7e0973e6d2e 8034127: javac provide debug oriented toString() methods to InferenceContext and UndetVar
vromero
parents: 23132
diff changeset
  2360
                   "Undet vars: " + undetvars;
e7e0973e6d2e 8034127: javac provide debug oriented toString() methods to InferenceContext and UndetVar
vromero
parents: 23132
diff changeset
  2361
        }
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
  2362
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
  2363
        /* Method Types.capture() generates a new type every time it's applied
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
  2364
         * to a wildcard parameterized type. This is intended functionality but
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
  2365
         * there are some cases when what you need is not to generate a new
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
  2366
         * captured type but to check that a previously generated captured type
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
  2367
         * is correct. There are cases when caching a captured type for later
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
  2368
         * reuse is sound. In general two captures from the same AST are equal.
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
  2369
         * This is why the tree is used as the key of the map below. This map
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
  2370
         * stores a Type per AST.
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
  2371
         */
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
  2372
        Map<JCTree, Type> captureTypeCache = new HashMap<>();
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
  2373
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
  2374
        Type cachedCapture(JCTree tree, Type t, boolean readOnly) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
  2375
            Type captured = captureTypeCache.get(tree);
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
  2376
            if (captured != null) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
  2377
                return captured;
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
  2378
            }
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
  2379
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
  2380
            Type result = types.capture(t);
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
  2381
            if (result != t && !readOnly) { // then t is a wildcard parameterized type
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
  2382
                captureTypeCache.put(tree, result);
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
  2383
            }
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
  2384
            return result;
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
  2385
        }
8616
5a47f5535883 7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents: 8229
diff changeset
  2386
    }
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2387
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2388
    final InferenceContext emptyContext = new InferenceContext(List.<Type>nil());
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2389
    // </editor-fold>
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2390
}