langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Infer.java
author jjg
Tue, 23 Feb 2016 16:38:04 -0800
changeset 36157 fdbf6c9be2ab
parent 33917 45d04023e689
child 36995 e19153419efd
permissions -rw-r--r--
8145472: replace remaining java.io.File with java.nio.file.Path Reviewed-by: vromero
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
36157
fdbf6c9be2ab 8145472: replace remaining java.io.File with java.nio.file.Path
jjg
parents: 33917
diff changeset
     2
 * Copyright (c) 1999, 2016, 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
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
    28
import com.sun.tools.javac.code.Type.UndetVar.UndetVarListener;
6592
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;
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
    30
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
    31
import com.sun.tools.javac.tree.TreeInfo;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import com.sun.tools.javac.util.*;
25844
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
    33
import com.sun.tools.javac.util.GraphUtils.DottableNode;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    34
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import com.sun.tools.javac.util.List;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    36
import com.sun.tools.javac.code.*;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    37
import com.sun.tools.javac.code.Type.*;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    38
import com.sun.tools.javac.code.Type.UndetVar.InferenceBound;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    39
import com.sun.tools.javac.code.Symbol.*;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    40
import com.sun.tools.javac.comp.DeferredAttr.AttrMode;
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
    41
import com.sun.tools.javac.comp.DeferredAttr.DeferredAttrContext;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    42
import com.sun.tools.javac.comp.Infer.GraphSolver.InferenceGraph;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    43
import com.sun.tools.javac.comp.Infer.GraphSolver.InferenceGraph.Node;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    44
import com.sun.tools.javac.comp.Resolve.InapplicableMethodException;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    45
import com.sun.tools.javac.comp.Resolve.VerboseResolutionMode;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    46
26267
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
    47
import java.io.IOException;
36157
fdbf6c9be2ab 8145472: replace remaining java.io.File with java.nio.file.Path
jjg
parents: 33917
diff changeset
    48
import java.io.Writer;
fdbf6c9be2ab 8145472: replace remaining java.io.File with java.nio.file.Path
jjg
parents: 33917
diff changeset
    49
import java.nio.file.Files;
fdbf6c9be2ab 8145472: replace remaining java.io.File with java.nio.file.Path
jjg
parents: 33917
diff changeset
    50
import java.nio.file.Path;
fdbf6c9be2ab 8145472: replace remaining java.io.File with java.nio.file.Path
jjg
parents: 33917
diff changeset
    51
import java.nio.file.Paths;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    52
import java.util.ArrayList;
25844
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
    53
import java.util.Collection;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    54
import java.util.Collections;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
    55
import java.util.EnumMap;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    56
import java.util.EnumSet;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
    57
import java.util.HashMap;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    58
import java.util.HashSet;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
    59
import java.util.LinkedHashSet;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
    60
import java.util.Map;
25844
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
    61
import java.util.Properties;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
    62
import java.util.Set;
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
    63
import java.util.function.BiFunction;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
    64
import java.util.function.BiPredicate;
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
    65
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14258
diff changeset
    66
import static com.sun.tools.javac.code.TypeTag.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
/** Helper class for type parameter inference, used by the attribution phase.
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5846
diff changeset
    70
 *  <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
    71
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
public class Infer {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
    76
    protected static final Context.Key<Infer> inferKey = new Context.Key<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    78
    Resolve rs;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    79
    Check chk;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    Symtab syms;
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    Types types;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    82
    JCDiagnostic.Factory diags;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10628
diff changeset
    83
    Log log;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    85
    /** should the graph solver be used? */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    86
    boolean allowGraphInference;
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15037
diff changeset
    87
26267
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
    88
    /**
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
    89
     * folder in which the inference dependency graphs should be written.
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
    90
     */
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
    91
    final private String dependenciesFolder;
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
    92
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
    93
    /**
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
    94
     * List of graphs awaiting to be dumped to a file.
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
    95
     */
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
    96
    private List<String> pendingGraphs;
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
    97
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    public static Infer instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        Infer instance = context.get(inferKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
            instance = new Infer(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    protected Infer(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        context.put(inferKey, this);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   107
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   108
        rs = Resolve.instance(context);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   109
        chk = Check.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
        syms = Symtab.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        types = Types.instance(context);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   112
        diags = JCDiagnostic.Factory.instance(context);
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10628
diff changeset
   113
        log = Log.instance(context);
13440
1ea85052f98d 7187104: Inference cleanup: remove redundant exception classes in Infer.java
mcimadamore
parents: 13438
diff changeset
   114
        inferenceException = new InferenceException(diags);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   115
        Options options = Options.instance(context);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   116
        allowGraphInference = Source.instance(context).allowGraphInference()
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   117
                && options.isUnset("useLegacyInference");
26267
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
   118
        dependenciesFolder = options.get("dumpInferenceGraphsTo");
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
   119
        pendingGraphs = List.nil();
31937
ad43a6639c4a 8132215: class InferenceContext should live in a separate file
vromero
parents: 30014
diff changeset
   120
ad43a6639c4a 8132215: class InferenceContext should live in a separate file
vromero
parents: 30014
diff changeset
   121
        emptyContext = new InferenceContext(this, List.<Type>nil());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   124
    /** 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
   125
    public static final Type anyPoly = new JCNoType();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   126
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   127
   /**
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   128
    * 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
   129
    * 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
   130
    */
11707
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 11550
diff changeset
   131
    public static class InferenceException extends InapplicableMethodException {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        private static final long serialVersionUID = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   134
        List<JCDiagnostic> messages = List.nil();
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   135
3140
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 3139
diff changeset
   136
        InferenceException(JCDiagnostic.Factory diags) {
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   137
            super(diags);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
        }
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   139
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   140
        @Override
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
   141
        InapplicableMethodException setMessage() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
   142
            //no message to set
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
   143
            return this;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
   144
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
   145
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
   146
        @Override
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   147
        InapplicableMethodException setMessage(JCDiagnostic diag) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   148
            messages = messages.append(diag);
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   149
            return this;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   150
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   151
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   152
        @Override
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   153
        public JCDiagnostic getDiagnostic() {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   154
            return messages.head;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   155
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   156
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   157
        void clear() {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   158
            messages = List.nil();
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   159
        }
3140
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 3139
diff changeset
   160
    }
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 3139
diff changeset
   161
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   162
    protected final InferenceException inferenceException;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   164
    // <editor-fold defaultstate="collapsed" desc="Inference routines">
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   165
    /**
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   166
     * Main inference entry point - instantiate a generic method type
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   167
     * using given argument types and (possibly) an expected target-type.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
     */
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   169
    Type instantiateMethod( Env<AttrContext> env,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   170
                            List<Type> tvars,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   171
                            MethodType mt,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   172
                            Attr.ResultInfo resultInfo,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   173
                            MethodSymbol msym,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   174
                            List<Type> argtypes,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   175
                            boolean allowBoxing,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   176
                            boolean useVarargs,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   177
                            Resolve.MethodResolutionContext resolveContext,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   178
                            Warner warn) throws InferenceException {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
        //-System.err.println("instantiateMethod(" + tvars + ", " + mt + ", " + argtypes + ")"); //DEBUG
31937
ad43a6639c4a 8132215: class InferenceContext should live in a separate file
vromero
parents: 30014
diff changeset
   180
        final InferenceContext inferenceContext = new InferenceContext(this, tvars);  //B0
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   181
        inferenceException.clear();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   182
        try {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   183
            DeferredAttr.DeferredAttrContext deferredAttrContext =
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   184
                        resolveContext.deferredAttrContext(msym, inferenceContext, resultInfo, warn);
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   185
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   186
            resolveContext.methodCheck.argumentsAcceptable(env, deferredAttrContext,   //B2
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   187
                    argtypes, mt.getParameterTypes(), warn);
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   188
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   189
            if (allowGraphInference && resultInfo != null && resultInfo.pt == anyPoly) {
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   190
                doIncorporation(inferenceContext, warn);
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   191
                //we are inside method attribution - just return a partially inferred type
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   192
                return new PartiallyInferredMethodType(mt, inferenceContext, env, warn);
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   193
            } else if (allowGraphInference &&
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   194
                    resultInfo != null &&
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15037
diff changeset
   195
                    !warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   196
                //inject return constraints earlier
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   197
                doIncorporation(inferenceContext, warn); //propagation
33906
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   198
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   199
                boolean shouldPropagate = resultInfo.checkContext.inferenceContext().free(resultInfo.pt);
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   200
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   201
                InferenceContext minContext = shouldPropagate ?
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   202
                        inferenceContext.min(roots(mt, deferredAttrContext), true, warn) :
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   203
                        inferenceContext;
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   204
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   205
                Type newRestype = generateReturnConstraints(env.tree, resultInfo,  //B3
33906
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   206
                        mt, minContext);
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   207
                mt = (MethodType)types.createMethodTypeWithReturn(mt, newRestype);
33906
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   208
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   209
                //propagate outwards if needed
33906
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   210
                if (shouldPropagate) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   211
                    //propagate inference context outwards and exit
33906
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   212
                    minContext.dupTo(resultInfo.checkContext.inferenceContext());
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   213
                    deferredAttrContext.complete();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   214
                    return mt;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   215
                }
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15037
diff changeset
   216
            }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15037
diff changeset
   217
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14547
diff changeset
   218
            deferredAttrContext.complete();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   220
            // minimize as yet undetermined type variables
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   221
            if (allowGraphInference) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   222
                inferenceContext.solve(warn);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   223
            } else {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   224
                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
   225
            }
3778
38a70273507b 6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents: 3140
diff changeset
   226
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
   227
            mt = (MethodType)inferenceContext.asInstType(mt);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   229
            if (!allowGraphInference &&
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   230
                    inferenceContext.restvars().nonEmpty() &&
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   231
                    resultInfo != null &&
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   232
                    !warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED)) {
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   233
                generateReturnConstraints(env.tree, resultInfo, mt, inferenceContext);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   234
                inferenceContext.solveLegacy(false, warn, LegacyInferenceSteps.EQ_UPPER.steps); //maximizeInst
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   235
                mt = (MethodType)inferenceContext.asInstType(mt);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   236
            }
3140
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 3139
diff changeset
   237
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   238
            if (resultInfo != null && rs.verboseResolutionMode.contains(VerboseResolutionMode.DEFERRED_INST)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   239
                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
   240
            }
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   241
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   242
            // return instantiated version of method type
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   243
            return mt;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   244
        } finally {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   245
            if (resultInfo != null || !allowGraphInference) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   246
                inferenceContext.notifyChange();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   247
            } else {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   248
                inferenceContext.notifyChange(inferenceContext.boundedVars());
14048
308d1cf8fe46 7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents: 14047
diff changeset
   249
            }
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   250
            if (resultInfo == null) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   251
                /* 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
   252
                 * 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
   253
                 */
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   254
                inferenceContext.captureTypeCache.clear();
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   255
            }
26267
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
   256
            dumpGraphsIfNeeded(env.tree, msym, resolveContext);
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
   257
        }
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
   258
    }
33906
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   259
    //where
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   260
        private List<Type> roots(MethodType mt, DeferredAttrContext deferredAttrContext) {
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   261
            ListBuffer<Type> roots = new ListBuffer<>();
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   262
            roots.add(mt.getReturnType());
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   263
            if (deferredAttrContext != null && deferredAttrContext.mode == AttrMode.CHECK) {
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   264
                roots.addAll(mt.getThrownTypes());
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   265
                for (DeferredAttr.DeferredAttrNode n : deferredAttrContext.deferredAttrNodes) {
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   266
                    roots.addAll(n.deferredStuckPolicy.stuckVars());
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   267
                    roots.addAll(n.deferredStuckPolicy.depVars());
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   268
                }
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   269
            }
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   270
            return roots.toList();
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   271
        }
26267
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
   272
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   273
    /**
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   274
     * A partially infered method/constructor type; such a type can be checked multiple times
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   275
     * against different targets.
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   276
     */
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   277
    public class PartiallyInferredMethodType extends MethodType {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   278
        public PartiallyInferredMethodType(MethodType mtype, InferenceContext inferenceContext, Env<AttrContext> env, Warner warn) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   279
            super(mtype.getParameterTypes(), mtype.getReturnType(), mtype.getThrownTypes(), mtype.tsym);
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   280
            this.inferenceContext = inferenceContext;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   281
            this.env = env;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   282
            this.warn = warn;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   283
        }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   284
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   285
        /** The inference context. */
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   286
        final InferenceContext inferenceContext;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   287
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   288
        /** The attribution environment. */
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   289
        Env<AttrContext> env;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   290
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   291
        /** The warner. */
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   292
        final Warner warn;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   293
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   294
        @Override
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   295
        public boolean isPartial() {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   296
            return true;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   297
        }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   298
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   299
        /**
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   300
         * Checks this type against a target; this means generating return type constraints, solve
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   301
         * and then roll back the results (to avoid poolluting the context).
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   302
         */
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   303
        Type check(Attr.ResultInfo resultInfo) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   304
            Warner noWarnings = new Warner(null);
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   305
            inferenceException.clear();
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   306
            List<Type> saved_undet = null;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   307
            try {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   308
                /** we need to save the inference context before generating target type constraints.
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   309
                 *  This constraints may pollute the inference context and make it useless in case we
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   310
                 *  need to use it several times: with several targets.
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   311
                 */
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   312
                saved_undet = inferenceContext.save();
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   313
                if (allowGraphInference && !warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED)) {
33906
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   314
                    boolean shouldPropagate = resultInfo.checkContext.inferenceContext().free(resultInfo.pt);
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   315
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   316
                    InferenceContext minContext = shouldPropagate ?
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   317
                            inferenceContext.min(roots(asMethodType(), null), false, warn) :
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   318
                            inferenceContext;
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   319
33906
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   320
                    MethodType other = (MethodType)minContext.update(asMethodType());
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   321
                    Type newRestype = generateReturnConstraints(env.tree, resultInfo,  //B3
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   322
                            other, minContext);
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   323
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   324
                    if (shouldPropagate) {
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   325
                        //propagate inference context outwards and exit
33906
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   326
                        minContext.dupTo(resultInfo.checkContext.inferenceContext(),
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   327
                                resultInfo.checkContext.deferredAttrContext().insideOverloadPhase());
33906
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   328
                        return newRestype;
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   329
                    }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   330
                }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   331
                inferenceContext.solve(noWarnings);
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   332
                return inferenceContext.asInstType(this).getReturnType();
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   333
            } catch (InferenceException ex) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   334
                resultInfo.checkContext.report(null, ex.getDiagnostic());
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   335
                Assert.error(); //cannot get here (the above should throw)
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   336
                return null;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   337
            } finally {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   338
                if (saved_undet != null) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   339
                    inferenceContext.rollback(saved_undet);
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   340
                }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   341
            }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   342
        }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   343
    }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   344
26267
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
   345
    private void dumpGraphsIfNeeded(DiagnosticPosition pos, Symbol msym, Resolve.MethodResolutionContext rsContext) {
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
   346
        int round = 0;
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
   347
        try {
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
   348
            for (String graph : pendingGraphs.reverse()) {
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
   349
                Assert.checkNonNull(dependenciesFolder);
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
   350
                Name name = msym.name == msym.name.table.names.init ?
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
   351
                        msym.owner.name : msym.name;
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
   352
                String filename = String.format("%s@%s[mode=%s,step=%s]_%d.dot",
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
   353
                        name,
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
   354
                        pos.getStartPosition(),
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
   355
                        rsContext.attrMode(),
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
   356
                        rsContext.step,
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
   357
                        round);
36157
fdbf6c9be2ab 8145472: replace remaining java.io.File with java.nio.file.Path
jjg
parents: 33917
diff changeset
   358
                Path dotFile = Paths.get(dependenciesFolder, filename);
fdbf6c9be2ab 8145472: replace remaining java.io.File with java.nio.file.Path
jjg
parents: 33917
diff changeset
   359
                try (Writer w = Files.newBufferedWriter(dotFile)) {
fdbf6c9be2ab 8145472: replace remaining java.io.File with java.nio.file.Path
jjg
parents: 33917
diff changeset
   360
                    w.append(graph);
26267
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
   361
                }
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
   362
                round++;
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
   363
            }
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
   364
        } catch (IOException ex) {
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
   365
            Assert.error("Error occurred when dumping inference graph: " + ex.getMessage());
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
   366
        } finally {
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
   367
            pendingGraphs = List.nil();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
        }
8616
5a47f5535883 7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents: 8229
diff changeset
   369
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   371
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   372
     * Generate constraints from the generic method's return type. If the method
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   373
     * 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
   374
     * type to derive more constraints on the generic method inference variables.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   375
     */
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   376
    Type generateReturnConstraints(JCTree tree, Attr.ResultInfo resultInfo,
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   377
            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
   378
        InferenceContext rsInfoInfContext = resultInfo.checkContext.inferenceContext();
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   379
        Type from = mt.getReturnType();
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   380
        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
   381
                rsInfoInfContext != emptyContext) {
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   382
            from = types.capture(from);
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   383
            //add synthetic captured ivars
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   384
            for (Type t : from.getTypeArguments()) {
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   385
                if (t.hasTag(TYPEVAR) && ((TypeVar)t).isCaptured()) {
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   386
                    inferenceContext.addVar((TypeVar)t);
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   387
                }
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   388
            }
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   389
        }
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   390
        Type qtype = inferenceContext.asUndetVar(from);
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   391
        Type to = resultInfo.pt;
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   392
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   393
        if (qtype.hasTag(VOID)) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   394
            to = syms.voidType;
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   395
        } else if (to.hasTag(NONE)) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   396
            to = from.isPrimitive() ? from : syms.objectType;
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   397
        } else if (qtype.hasTag(UNDETVAR)) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   398
            if (resultInfo.pt.isReference()) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   399
                to = generateReturnConstraintsUndetVarToReference(
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   400
                        tree, (UndetVar)qtype, to, resultInfo, inferenceContext);
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   401
            } else {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   402
                if (to.isPrimitive()) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   403
                    to = generateReturnConstraintsPrimitive(tree, (UndetVar)qtype, to,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   404
                        resultInfo, inferenceContext);
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   405
                }
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   406
            }
33708
99085a01b536 8141613: Compiler fails to infer generic type
mcimadamore
parents: 32915
diff changeset
   407
        } else if (rsInfoInfContext.free(resultInfo.pt)) {
99085a01b536 8141613: Compiler fails to infer generic type
mcimadamore
parents: 32915
diff changeset
   408
            //propagation - cache captured vars
99085a01b536 8141613: Compiler fails to infer generic type
mcimadamore
parents: 32915
diff changeset
   409
            qtype = inferenceContext.asUndetVar(rsInfoInfContext.cachedCapture(tree, from, false));
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   410
        }
23132
00a059740d87 8035891: javac, rename method asFree() in InferenceContext to asUndetVar() which reflects better it's purpose
vromero
parents: 23124
diff changeset
   411
        Assert.check(allowGraphInference || !rsInfoInfContext.free(to),
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   412
                "legacy inference engine cannot handle constraints on both sides of a subtyping assertion");
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   413
        //we need to skip capture?
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   414
        Warner retWarn = new Warner();
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   415
        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
   416
                //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
   417
                (!allowGraphInference && retWarn.hasLint(Lint.LintCategory.UNCHECKED))) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   418
            throw inferenceException
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   419
                    .setMessage("infer.no.conforming.instance.exists",
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   420
                    inferenceContext.restvars(), mt.getReturnType(), to);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   421
        }
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   422
        return from;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   423
    }
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   424
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   425
    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
   426
            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
   427
        if (!allowGraphInference) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   428
            //if legacy, just return boxed type
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   429
            return types.boxedClass(to).type;
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   430
        }
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   431
        //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
   432
        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
   433
                InferenceBound.LOWER)) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   434
            Type boundAsPrimitive = types.unboxedType(t);
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   435
            if (boundAsPrimitive == null || boundAsPrimitive.hasTag(NONE)) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   436
                continue;
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   437
            }
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   438
            return generateReferenceToTargetConstraint(tree, from, to,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   439
                    resultInfo, inferenceContext);
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   440
        }
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   441
        return types.boxedClass(to).type;
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   442
    }
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   443
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   444
    private Type generateReturnConstraintsUndetVarToReference(JCTree tree,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   445
            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
   446
            InferenceContext inferenceContext) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   447
        Type captureOfTo = types.capture(to);
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   448
        /* 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
   449
         */
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   450
        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
   451
            /* 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
   452
             *      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
   453
             */
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   454
            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
   455
                Type captureOfBound = types.capture(t);
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   456
                if (captureOfBound != t) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   457
                    return generateReferenceToTargetConstraint(tree, from, to,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   458
                            resultInfo, inferenceContext);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   459
                }
12468
1100643c0209 7154127: Inference cleanup: remove bound check analysis from visitors in Types.java
mcimadamore
parents: 12335
diff changeset
   460
            }
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   461
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   462
            /* 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
   463
             * 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
   464
             * 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
   465
             */
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   466
            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
   467
                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
   468
                    if (aLowerBound != anotherLowerBound &&
26663
f2d8845a9bb2 8055963: Inference failure with nested invocation
mcimadamore
parents: 26267
diff changeset
   469
                            !inferenceContext.free(aLowerBound) &&
f2d8845a9bb2 8055963: Inference failure with nested invocation
mcimadamore
parents: 26267
diff changeset
   470
                            !inferenceContext.free(anotherLowerBound) &&
f2d8845a9bb2 8055963: Inference failure with nested invocation
mcimadamore
parents: 26267
diff changeset
   471
                            commonSuperWithDiffParameterization(aLowerBound, anotherLowerBound)) {
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   472
                        return generateReferenceToTargetConstraint(tree, from, to,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   473
                            resultInfo, inferenceContext);
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   474
                    }
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   475
                }
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   476
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
        }
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   478
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   479
        /* 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
   480
         * 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
   481
         * 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
   482
         * 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
   483
         */
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   484
        if (to.isParameterized()) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   485
            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
   486
                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
   487
                if (sup != null && sup.isRaw()) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   488
                    return generateReferenceToTargetConstraint(tree, from, to,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   489
                            resultInfo, inferenceContext);
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   490
                }
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   491
            }
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   492
        }
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   493
        return to;
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   494
    }
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   495
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   496
    private boolean commonSuperWithDiffParameterization(Type t, Type s) {
24795
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   497
        for (Pair<Type, Type> supers : getParameterizedSupers(t, s)) {
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   498
            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
   499
        }
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
   500
        return false;
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   501
    }
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   502
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   503
    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
   504
            Type to, Attr.ResultInfo resultInfo,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   505
            InferenceContext inferenceContext) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   506
        inferenceContext.solve(List.of(from.qtype), new Warner());
25279
a665d5cbb8c3 8044546: Crash on faulty reduce/lambda
vromero
parents: 24795
diff changeset
   507
        inferenceContext.notifyChange();
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   508
        Type capturedType = resultInfo.checkContext.inferenceContext()
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   509
                .cachedCapture(tree, from.getInst(), false);
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   510
        if (types.isConvertible(capturedType,
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   511
                resultInfo.checkContext.inferenceContext().asUndetVar(to))) {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   512
            //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
   513
            return syms.objectType;
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   514
        }
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24063
diff changeset
   515
        return to;
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   516
    }
12468
1100643c0209 7154127: Inference cleanup: remove bound check analysis from visitors in Types.java
mcimadamore
parents: 12335
diff changeset
   517
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   518
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   519
      * Infer cyclic inference variables as described in 15.12.2.8.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   520
      */
31937
ad43a6639c4a 8132215: class InferenceContext should live in a separate file
vromero
parents: 30014
diff changeset
   521
    void instantiateAsUninferredVars(List<Type> vars, InferenceContext inferenceContext) {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19914
diff changeset
   522
        ListBuffer<Type> todo = new ListBuffer<>();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   523
        //step 1 - create fresh tvars
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   524
        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
   525
            UndetVar uv = (UndetVar)inferenceContext.asUndetVar(t);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   526
            List<Type> upperBounds = uv.getBounds(InferenceBound.UPPER);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   527
            if (Type.containsAny(upperBounds, vars)) {
16967
79d444669f3f 7015104: use new subtype of TypeSymbol for type parameters
jfranck
parents: 16805
diff changeset
   528
                TypeSymbol fresh_tvar = new TypeVariableSymbol(Flags.SYNTHETIC, uv.qtype.tsym.name, null, uv.qtype.tsym.owner);
29147
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 27391
diff changeset
   529
                fresh_tvar.type = new TypeVar(fresh_tvar, types.makeIntersectionType(uv.getBounds(InferenceBound.UPPER)), null);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   530
                todo.append(uv);
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   531
                uv.setInst(fresh_tvar.type);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   532
            } else if (upperBounds.nonEmpty()) {
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   533
                uv.setInst(types.glb(upperBounds));
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   534
            } else {
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   535
                uv.setInst(syms.objectType);
12468
1100643c0209 7154127: Inference cleanup: remove bound check analysis from visitors in Types.java
mcimadamore
parents: 12335
diff changeset
   536
            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   537
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   538
        //step 2 - replace fresh tvars in their bounds
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   539
        List<Type> formals = vars;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   540
        for (Type t : todo) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   541
            UndetVar uv = (UndetVar)t;
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   542
            TypeVar ct = (TypeVar)uv.getInst();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   543
            ct.bound = types.glb(inferenceContext.asInstTypes(types.getBounds(ct)));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   544
            if (ct.bound.isErroneous()) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   545
                //report inference error if glb fails
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   546
                reportBoundError(uv, InferenceBound.UPPER);
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15037
diff changeset
   547
            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   548
            formals = formals.tail;
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   549
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   550
    }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   551
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   552
    /**
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   553
     * 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
   554
     * 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
   555
     * 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
   556
     */
12335
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
   557
    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
   558
                                            MethodSymbol spMethod,  // sig. poly. method or null if none
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   559
                                            Resolve.MethodResolutionContext resolveContext,
8036
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   560
                                            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
   561
        final Type restype;
8036
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   562
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   563
        //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
   564
        //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
   565
        //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
   566
        //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
   567
        //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
   568
        //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
   569
        //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
   570
8036
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   571
        switch (env.next.tree.getTag()) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10816
diff changeset
   572
            case TYPECAST:
8036
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   573
                JCTypeCast castTree = (JCTypeCast)env.next.tree;
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   574
                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
   575
                    castTree.clazz.type :
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   576
                    syms.objectType;
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   577
                break;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10816
diff changeset
   578
            case EXEC:
8036
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   579
                JCTree.JCExpressionStatement execTree =
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   580
                        (JCTree.JCExpressionStatement)env.next.tree;
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   581
                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
   582
                    syms.voidType :
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   583
                    syms.objectType;
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   584
                break;
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   585
            default:
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   586
                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
   587
        }
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   588
29554
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29147
diff changeset
   589
        List<Type> paramtypes = argtypes.map(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
   590
        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
   591
            spMethod.getThrownTypes() :
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   592
            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
   593
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   594
        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
   595
                                          restype,
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   596
                                          exType,
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   597
                                          syms.methodClass);
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   598
        return mtype;
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   599
    }
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   600
    //where
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   601
        class ImplicitArgType extends DeferredAttr.DeferredTypeMap {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   602
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   603
            public ImplicitArgType(Symbol msym, Resolve.MethodResolutionPhase phase) {
25279
a665d5cbb8c3 8044546: Crash on faulty reduce/lambda
vromero
parents: 24795
diff changeset
   604
                (rs.deferredAttr).super(AttrMode.SPECULATIVE, msym, phase);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   605
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   606
29554
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29147
diff changeset
   607
            @Override
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29147
diff changeset
   608
            public Type visitClassType(ClassType t, Void aVoid) {
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29147
diff changeset
   609
                return types.erasure(t);
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29147
diff changeset
   610
            }
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29147
diff changeset
   611
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29147
diff changeset
   612
            @Override
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29147
diff changeset
   613
            public Type visitType(Type t, Void _unused) {
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29147
diff changeset
   614
                if (t.hasTag(DEFERRED)) {
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29147
diff changeset
   615
                    return visit(super.visitType(t, null));
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29147
diff changeset
   616
                } else if (t.hasTag(BOT))
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   617
                    // 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
   618
                    // infer as java.lang.Void for now
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   619
                    t = types.boxedClass(syms.voidType).type;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   620
                return t;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   621
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   622
        }
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   623
33906
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   624
    TypeMapping<Void> fromTypeVarFun = new TypeMapping<Void>() {
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   625
        @Override
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   626
        public Type visitTypeVar(TypeVar tv, Void aVoid) {
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   627
            return new UndetVar(tv, incorporationEngine(), types);
33906
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   628
        }
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   629
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   630
        @Override
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   631
        public Type visitCapturedType(CapturedType t, Void aVoid) {
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   632
            return new CapturedUndetVar(t, incorporationEngine(), types);
33906
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   633
        }
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   634
    };
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   635
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   636
    /**
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   637
      * 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
   638
      * SAM type contains one or more wildcards. An inference process is applied
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   639
      * so that wildcard bounds, as well as explicit lambda/method ref parameters
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   640
      * (where applicable) are used to constraint the solution.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   641
      */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   642
    public Type instantiateFunctionalInterface(DiagnosticPosition pos, Type funcInterface,
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   643
            List<Type> paramTypes, Check.CheckContext checkContext) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   644
        if (types.capture(funcInterface) == funcInterface) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   645
            //if capture doesn't change the type then return the target unchanged
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   646
            //(this means the target contains no wildcards!)
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   647
            return funcInterface;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   648
        } else {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   649
            Type formalInterface = funcInterface.tsym.type;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   650
            InferenceContext funcInterfaceContext =
31937
ad43a6639c4a 8132215: class InferenceContext should live in a separate file
vromero
parents: 30014
diff changeset
   651
                    new InferenceContext(this, funcInterface.tsym.type.getTypeArguments());
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   652
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   653
            Assert.check(paramTypes != null);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   654
            //get constraints from explicit params (this is done by
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   655
            //checking that explicit param types are equal to the ones
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   656
            //in the functional interface descriptors)
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   657
            List<Type> descParameterTypes = types.findDescriptorType(formalInterface).getParameterTypes();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   658
            if (descParameterTypes.size() != paramTypes.size()) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   659
                checkContext.report(pos, diags.fragment("incompatible.arg.types.in.lambda"));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   660
                return types.createErrorType(funcInterface);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   661
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   662
            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
   663
                if (!types.isSameType(funcInterfaceContext.asUndetVar(p), paramTypes.head)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   664
                    checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   665
                    return types.createErrorType(funcInterface);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   666
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   667
                paramTypes = paramTypes.tail;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   668
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   669
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   670
            try {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   671
                funcInterfaceContext.solve(funcInterfaceContext.boundedVars(), types.noWarnings);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   672
            } catch (InferenceException ex) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   673
                checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   674
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   675
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   676
            List<Type> actualTypeargs = funcInterface.getTypeArguments();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   677
            for (Type t : funcInterfaceContext.undetvars) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   678
                UndetVar uv = (UndetVar)t;
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   679
                if (uv.getInst() == null) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   680
                    uv.setInst(actualTypeargs.head);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   681
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   682
                actualTypeargs = actualTypeargs.tail;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   683
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   684
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   685
            Type owntype = funcInterfaceContext.asInstType(formalInterface);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   686
            if (!chk.checkValidGenericType(owntype)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   687
                //if the inferred functional interface type is not well-formed,
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   688
                //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
   689
                checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   690
            }
25279
a665d5cbb8c3 8044546: Crash on faulty reduce/lambda
vromero
parents: 24795
diff changeset
   691
            //propagate constraints as per JLS 18.2.1
a665d5cbb8c3 8044546: Crash on faulty reduce/lambda
vromero
parents: 24795
diff changeset
   692
            checkContext.compatible(owntype, funcInterface, types.noWarnings);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   693
            return owntype;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   694
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   695
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   696
    // </editor-fold>
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   697
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   698
    // <editor-fold defaultstate="collapsed" desc="Incorporation">
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   699
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   700
    /**
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   701
     * This class is the root of all incorporation actions.
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   702
     */
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   703
    public abstract class IncorporationAction {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   704
        UndetVar uv;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   705
        Type t;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   706
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   707
        IncorporationAction(UndetVar uv, Type t) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   708
            this.uv = uv;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   709
            this.t = t;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   710
        }
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   711
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   712
        /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   713
         * Incorporation action entry-point. Subclasses should define the logic associated with
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   714
         * this incorporation action.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   715
         */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   716
        abstract void apply(InferenceContext ic, Warner warn);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   717
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   718
        /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   719
         * Helper function: perform subtyping through incorporation cache.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   720
         */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   721
        boolean isSubtype(Type s, Type t, Warner warn) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   722
            return doIncorporationOp(IncorporationBinaryOpKind.IS_SUBTYPE, s, t, warn);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   723
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   724
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   725
        /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   726
         * Helper function: perform type-equivalence through incorporation cache.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   727
         */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   728
        boolean isSameType(Type s, Type t) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   729
            return doIncorporationOp(IncorporationBinaryOpKind.IS_SAME_TYPE, s, t, null);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   730
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   731
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   732
        @Override
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   733
        public String toString() {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   734
            return String.format("%s[undet=%s,t=%s]", getClass().getSimpleName(), uv.qtype, t);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   735
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   736
    }
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   737
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   738
    /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   739
     * Bound-check incorporation action. A newly added bound is checked against existing bounds,
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   740
     * to verify its compatibility; each bound is checked using either subtyping or type equivalence.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   741
     */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   742
    class CheckBounds extends IncorporationAction {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   743
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   744
        InferenceBound from;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   745
        BiFunction<InferenceContext, Type, Type> typeFunc;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   746
        BiPredicate<InferenceContext, Type> optFilter;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   747
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   748
        CheckBounds(UndetVar uv, Type t, InferenceBound from) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   749
            this(uv, t, InferenceContext::asUndetVar, null, from);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   750
        }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   751
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   752
        CheckBounds(UndetVar uv, Type t, BiFunction<InferenceContext, Type, Type> typeFunc,
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   753
                    BiPredicate<InferenceContext, Type> typeFilter, InferenceBound from) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   754
            super(uv, t);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   755
            this.from = from;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   756
            this.typeFunc = typeFunc;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   757
            this.optFilter = typeFilter;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   758
        }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   759
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   760
        @Override
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   761
        void apply(InferenceContext inferenceContext, Warner warn) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   762
            t = typeFunc.apply(inferenceContext, t);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   763
            if (optFilter != null && optFilter.test(inferenceContext, t)) return;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   764
            for (InferenceBound to : boundsToCheck()) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   765
                for (Type b : uv.getBounds(to)) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   766
                    b = typeFunc.apply(inferenceContext, b);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   767
                    if (optFilter != null && optFilter.test(inferenceContext, b)) continue;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   768
                    boolean success = checkBound(t, b, from, to, warn);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   769
                    if (!success) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   770
                        report(from, to);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   771
                    }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   772
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   773
            }
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
   774
        }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   775
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   776
        /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   777
         * The list of bound kinds to be checked.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   778
         */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   779
        EnumSet<InferenceBound> boundsToCheck() {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   780
            return (from == InferenceBound.EQ) ?
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   781
                            EnumSet.allOf(InferenceBound.class) :
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   782
                            EnumSet.complementOf(EnumSet.of(from));
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   783
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   784
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   785
        /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   786
         * Is source type 's' compatible with target type 't' given source and target bound kinds?
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   787
         */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   788
        boolean checkBound(Type s, Type t, InferenceBound ib_s, InferenceBound ib_t, Warner warn) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   789
            if (ib_s.lessThan(ib_t)) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   790
                return isSubtype(s, t, warn);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   791
            } else if (ib_t.lessThan(ib_s)) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   792
                return isSubtype(t, s, warn);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   793
            } else {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   794
                return isSameType(s, t);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   795
            }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   796
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   797
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   798
        /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   799
         * Report a bound check error.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   800
         */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   801
        void report(InferenceBound from, InferenceBound to) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   802
            //this is a workaround to preserve compatibility with existing messages
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   803
            if (from == to) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   804
                reportBoundError(uv, from);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   805
            } else if (from == InferenceBound.LOWER || to == InferenceBound.EQ) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   806
                reportBoundError(uv, to, from);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   807
            } else {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   808
                reportBoundError(uv, from, to);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   809
            }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   810
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   811
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   812
        @Override
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   813
        public String toString() {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   814
            return String.format("%s[undet=%s,t=%s,bound=%s]", getClass().getSimpleName(), uv.qtype, t, from);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   815
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   816
    }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   817
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   818
    /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   819
     * Custom check executed by the legacy incorporation engine. Newly added bounds are checked
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   820
     * against existing eq bounds.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   821
     */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   822
    class EqCheckLegacy extends CheckBounds {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   823
        EqCheckLegacy(UndetVar uv, Type t, InferenceBound from) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   824
            super(uv, t, InferenceContext::asInstType, InferenceContext::free, from);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   825
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   826
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   827
        @Override
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   828
        EnumSet<InferenceBound> boundsToCheck() {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   829
            return (from == InferenceBound.EQ) ?
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   830
                            EnumSet.allOf(InferenceBound.class) :
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   831
                            EnumSet.of(InferenceBound.EQ);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   832
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   833
    }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   834
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   835
    /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   836
     * Check that the inferred type conforms to all bounds.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   837
     */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   838
    class CheckInst extends CheckBounds {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   839
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   840
        EnumSet<InferenceBound> to;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   841
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   842
        CheckInst(UndetVar uv, InferenceBound ib, InferenceBound... rest) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   843
            super(uv, uv.getInst(), InferenceBound.EQ);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   844
            this.to = EnumSet.of(ib, rest);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   845
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   846
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   847
        @Override
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   848
        EnumSet<InferenceBound> boundsToCheck() {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   849
            return to;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   850
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   851
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   852
        @Override
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   853
        void report(InferenceBound from, InferenceBound to) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   854
            reportInstError(uv, to);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   855
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   856
    }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   857
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   858
    /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   859
     * Replace undetvars in bounds and check that the inferred type conforms to all bounds.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   860
     */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   861
    class SubstBounds extends CheckInst {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   862
        SubstBounds(UndetVar uv) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   863
            super(uv, InferenceBound.LOWER, InferenceBound.EQ, InferenceBound.UPPER);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   864
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   865
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   866
        @Override
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   867
        void apply(InferenceContext inferenceContext, Warner warn) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   868
            for (Type undet : inferenceContext.undetvars) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   869
                //we could filter out variables not mentioning uv2...
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   870
                UndetVar uv2 = (UndetVar)undet;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   871
                uv2.substBounds(List.of(uv.qtype), List.of(uv.getInst()), types);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   872
                checkCompatibleUpperBounds(uv2, inferenceContext);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   873
            }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   874
            super.apply(inferenceContext, warn);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   875
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   876
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   877
        /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   878
         * Make sure that the upper bounds we got so far lead to a solvable inference
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   879
         * variable by making sure that a glb exists.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   880
         */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   881
        void checkCompatibleUpperBounds(UndetVar uv, InferenceContext inferenceContext) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   882
            List<Type> hibounds =
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   883
                    Type.filter(uv.getBounds(InferenceBound.UPPER), new BoundFilter(inferenceContext));
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   884
            final Type hb;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   885
            if (hibounds.isEmpty())
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   886
                hb = syms.objectType;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   887
            else if (hibounds.tail.isEmpty())
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   888
                hb = hibounds.head;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   889
            else
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   890
                hb = types.glb(hibounds);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   891
            if (hb == null || hb.isErroneous())
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   892
                reportBoundError(uv, InferenceBound.UPPER);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   893
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   894
    }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   895
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   896
    /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   897
     * Perform pairwise comparison between common generic supertypes of two upper bounds.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   898
     */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   899
    class CheckUpperBounds extends IncorporationAction {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   900
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   901
        public CheckUpperBounds(UndetVar uv, Type t) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   902
            super(uv, t);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   903
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   904
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   905
        @Override
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   906
        void apply(InferenceContext inferenceContext, Warner warn) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   907
            List<Type> boundList = uv.getBounds(InferenceBound.UPPER).stream()
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   908
                    .collect(types.closureCollector(true, types::isSameType));
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   909
            for (Type b2 : boundList) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   910
                if (t == b2) continue;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   911
                    /* This wildcard check is temporary workaround. This code may need to be
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   912
                     * revisited once spec bug JDK-7034922 is fixed.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   913
                     */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   914
                if (t != b2 && !t.hasTag(WILDCARD) && !b2.hasTag(WILDCARD)) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   915
                    for (Pair<Type, Type> commonSupers : getParameterizedSupers(t, b2)) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   916
                        List<Type> allParamsSuperBound1 = commonSupers.fst.allparams();
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   917
                        List<Type> allParamsSuperBound2 = commonSupers.snd.allparams();
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   918
                        while (allParamsSuperBound1.nonEmpty() && allParamsSuperBound2.nonEmpty()) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   919
                            //traverse the list of all params comparing them
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   920
                            if (!allParamsSuperBound1.head.hasTag(WILDCARD) &&
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   921
                                    !allParamsSuperBound2.head.hasTag(WILDCARD)) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   922
                                if (!isSameType(inferenceContext.asUndetVar(allParamsSuperBound1.head),
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   923
                                        inferenceContext.asUndetVar(allParamsSuperBound2.head))) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   924
                                    reportBoundError(uv, InferenceBound.UPPER);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   925
                                }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   926
                            }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   927
                            allParamsSuperBound1 = allParamsSuperBound1.tail;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   928
                            allParamsSuperBound2 = allParamsSuperBound2.tail;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   929
                        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   930
                        Assert.check(allParamsSuperBound1.isEmpty() && allParamsSuperBound2.isEmpty());
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   931
                    }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   932
                }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   933
            }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   934
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   935
    }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   936
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   937
    /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   938
     * Perform propagation of bounds. Given a constraint of the kind {@code alpha <: T}, three
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   939
     * kind of propagation occur:
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   940
     *
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   941
     * <li>T is copied into all matching bounds (i.e. lower/eq bounds) B of alpha such that B=beta (forward propagation)</li>
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   942
     * <li>if T=beta, matching bounds (i.e. upper bounds) of beta are copied into alpha (backwards propagation)</li>
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   943
     * <li>if T=beta, sets a symmetric bound on beta (i.e. beta :> alpha) (symmetric propagation) </li>
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   944
     */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   945
    class PropagateBounds extends IncorporationAction {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   946
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   947
        InferenceBound ib;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   948
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   949
        public PropagateBounds(UndetVar uv, Type t, InferenceBound ib) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   950
            super(uv, t);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   951
            this.ib = ib;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   952
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   953
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   954
        void apply(InferenceContext inferenceContext, Warner warner) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   955
            Type undetT = inferenceContext.asUndetVar(t);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   956
            if (undetT.hasTag(UNDETVAR) && !((UndetVar)undetT).isCaptured()) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   957
                UndetVar uv2 = (UndetVar)undetT;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   958
                //symmetric propagation
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   959
                uv2.addBound(ib.complement(), uv, types);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   960
                //backwards propagation
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   961
                for (InferenceBound ib2 : backwards()) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   962
                    for (Type b : uv2.getBounds(ib2)) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   963
                        uv.addBound(ib2, b, types);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   964
                    }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   965
                }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   966
            }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   967
            //forward propagation
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   968
            for (InferenceBound ib2 : forward()) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   969
                for (Type l : uv.getBounds(ib2)) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   970
                    Type undet = inferenceContext.asUndetVar(l);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   971
                    if (undet.hasTag(TypeTag.UNDETVAR) && !((UndetVar)undet).isCaptured()) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   972
                        UndetVar uv2 = (UndetVar)undet;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   973
                        uv2.addBound(ib, inferenceContext.asInstType(t), types);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   974
                    }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   975
                }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   976
            }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   977
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   978
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   979
        EnumSet<InferenceBound> forward() {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   980
            return (ib == InferenceBound.EQ) ?
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   981
                    EnumSet.of(InferenceBound.EQ) : EnumSet.complementOf(EnumSet.of(ib));
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   982
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   983
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   984
        EnumSet<InferenceBound> backwards() {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   985
            return (ib == InferenceBound.EQ) ?
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   986
                    EnumSet.allOf(InferenceBound.class) : EnumSet.of(ib);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   987
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   988
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   989
        @Override
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   990
        public String toString() {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   991
            return String.format("%s[undet=%s,t=%s,bound=%s]", getClass().getSimpleName(), uv.qtype, t, ib);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   992
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   993
    }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   994
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   995
    /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   996
     * This class models an incorporation engine. The engine is responsible for listening to
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   997
     * changes in inference variables and register incorporation actions accordingly.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   998
     */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   999
    abstract class AbstractIncorporationEngine implements UndetVarListener {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1000
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1001
        @Override
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1002
        public void varInstantiated(UndetVar uv) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1003
            uv.incorporationActions.addFirst(new SubstBounds(uv));
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1004
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1005
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1006
        @Override
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1007
        public void varBoundChanged(UndetVar uv, InferenceBound ib, Type bound, boolean update) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1008
            if (uv.isCaptured()) return;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1009
            uv.incorporationActions.addAll(getIncorporationActions(uv, ib, bound, update));
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1010
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1011
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1012
        abstract List<IncorporationAction> getIncorporationActions(UndetVar uv, InferenceBound ib, Type t, boolean update);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1013
    }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1014
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1015
    /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1016
     * A legacy incorporation engine. Used for source <= 7.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1017
     */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1018
    AbstractIncorporationEngine legacyEngine = new AbstractIncorporationEngine() {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1019
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1020
        List<IncorporationAction> getIncorporationActions(UndetVar uv, InferenceBound ib, Type t, boolean update) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1021
            ListBuffer<IncorporationAction> actions = new ListBuffer<>();
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1022
            Type inst = uv.getInst();
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1023
            if (inst != null) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1024
                actions.add(new CheckInst(uv, ib));
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1025
            }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1026
            actions.add(new EqCheckLegacy(uv, t, ib));
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1027
            return actions.toList();
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1028
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1029
    };
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1030
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1031
    /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1032
     * The standard incorporation engine. Used for source >= 8.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1033
     */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1034
    AbstractIncorporationEngine graphEngine = new AbstractIncorporationEngine() {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1035
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1036
        @Override
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1037
        List<IncorporationAction> getIncorporationActions(UndetVar uv, InferenceBound ib, Type t, boolean update) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1038
            ListBuffer<IncorporationAction> actions = new ListBuffer<>();
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1039
            Type inst = uv.getInst();
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1040
            if (inst != null) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1041
                actions.add(new CheckInst(uv, ib));
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1042
            }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1043
            actions.add(new CheckBounds(uv, t, ib));
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1044
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1045
            if (update) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1046
                return actions.toList();
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1047
            }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1048
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1049
            if (ib == InferenceBound.UPPER) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1050
                actions.add(new CheckUpperBounds(uv, t));
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1051
            }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1052
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1053
            actions.add(new PropagateBounds(uv, t, ib));
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1054
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1055
            return actions.toList();
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1056
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1057
    };
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1058
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1059
    /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1060
     * Get the incorporation engine to be used in this compilation.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1061
     */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1062
    AbstractIncorporationEngine incorporationEngine() {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1063
        return allowGraphInference ? graphEngine : legacyEngine;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1064
    }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1065
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1066
    /** max number of incorporation rounds. */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1067
    static final int MAX_INCORPORATION_STEPS = 10000;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1068
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1069
    /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1070
     * Check bounds and perform incorporation.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1071
     */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1072
    void doIncorporation(InferenceContext inferenceContext, Warner warn) throws InferenceException {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1073
        try {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1074
            boolean progress = true;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1075
            int round = 0;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1076
            while (progress && round < MAX_INCORPORATION_STEPS) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1077
                progress = false;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1078
                for (Type t : inferenceContext.undetvars) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1079
                    UndetVar uv = (UndetVar)t;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1080
                    if (!uv.incorporationActions.isEmpty()) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1081
                        progress = true;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1082
                        uv.incorporationActions.removeFirst().apply(inferenceContext, warn);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1083
                    }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1084
                }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1085
                round++;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1086
            }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1087
        } finally {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1088
            incorporationCache.clear();
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1089
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1090
    }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1091
24795
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1092
    /* 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
  1093
     * 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
  1094
     * 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
  1095
     * 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
  1096
     * 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
  1097
     *
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
  1098
     * As an example for the following input:
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
  1099
     *
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
  1100
     * t = java.util.ArrayList<java.lang.String>
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
  1101
     * s = java.util.List<T>
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
  1102
     *
24795
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1103
     * we get this ouput (singleton list):
24063
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
  1104
     *
24795
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1105
     * [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
  1106
     */
24795
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1107
    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
  1108
        Type lubResult = types.lub(t, s);
24795
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1109
        if (lubResult == syms.errType || lubResult == syms.botType) {
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1110
            return List.nil();
24063
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
  1111
        }
29147
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 27391
diff changeset
  1112
        List<Type> supertypesToCheck = lubResult.isIntersection() ?
24795
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1113
                ((IntersectionClassType)lubResult).getComponents() :
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1114
                List.of(lubResult);
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1115
        ListBuffer<Pair<Type, Type>> commonSupertypes = new ListBuffer<>();
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1116
        for (Type sup : supertypesToCheck) {
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1117
            if (sup.isParameterized()) {
27391
17f7b6c40c6d 8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents: 27228
diff changeset
  1118
                Type asSuperOfT = asSuper(t, sup);
17f7b6c40c6d 8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents: 27228
diff changeset
  1119
                Type asSuperOfS = asSuper(s, sup);
24795
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1120
                commonSupertypes.add(new Pair<>(asSuperOfT, asSuperOfS));
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1121
            }
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1122
        }
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1123
        return commonSupertypes.toList();
24063
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
  1124
    }
27391
17f7b6c40c6d 8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents: 27228
diff changeset
  1125
    //where
17f7b6c40c6d 8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents: 27228
diff changeset
  1126
        private Type asSuper(Type t, Type sup) {
17f7b6c40c6d 8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents: 27228
diff changeset
  1127
            return (sup.hasTag(ARRAY)) ?
17f7b6c40c6d 8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents: 27228
diff changeset
  1128
                    new ArrayType(asSuper(types.elemtype(t), types.elemtype(sup)), syms.arrayClass) :
17f7b6c40c6d 8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents: 27228
diff changeset
  1129
                    types.asSuper(t, sup.tsym);
17f7b6c40c6d 8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents: 27228
diff changeset
  1130
        }
24063
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
  1131
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1132
    boolean doIncorporationOp(IncorporationBinaryOpKind opKind, Type op1, Type op2, Warner warn) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1133
            IncorporationBinaryOp newOp = new IncorporationBinaryOp(opKind, op1, op2);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1134
            Boolean res = incorporationCache.get(newOp);
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1135
            if (res == null) {
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1136
                incorporationCache.put(newOp, res = newOp.apply(warn));
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1137
            }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1138
            return res;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1139
        }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1140
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1141
    /**
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1142
     * 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
  1143
     * (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
  1144
     * upper/lower/eq bound).
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1145
     */
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1146
    enum IncorporationBinaryOpKind {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1147
        IS_SUBTYPE() {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1148
            @Override
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1149
            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
  1150
                return types.isSubtypeUnchecked(op1, op2, warn);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1151
            }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1152
        },
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1153
        IS_SAME_TYPE() {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1154
            @Override
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1155
            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
  1156
                return types.isSameType(op1, op2);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1157
            }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1158
        };
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1159
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1160
        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
  1161
    }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1162
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1163
    /**
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1164
     * This class encapsulates a basic incorporation operation; incorporation
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1165
     * 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
  1166
     * 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
  1167
     * 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
  1168
     * same bounds over and over).
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1169
     */
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1170
    class IncorporationBinaryOp {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1171
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1172
        IncorporationBinaryOpKind opKind;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1173
        Type op1;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1174
        Type op2;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1175
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1176
        IncorporationBinaryOp(IncorporationBinaryOpKind opKind, Type op1, Type op2) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1177
            this.opKind = opKind;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1178
            this.op1 = op1;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1179
            this.op2 = op2;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1180
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1181
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1182
        @Override
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1183
        public boolean equals(Object o) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1184
            if (!(o instanceof IncorporationBinaryOp)) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1185
                return false;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1186
            } else {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1187
                IncorporationBinaryOp that = (IncorporationBinaryOp)o;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1188
                return opKind == that.opKind &&
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1189
                        types.isSameType(op1, that.op1, true) &&
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1190
                        types.isSameType(op2, that.op2, true);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1191
            }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1192
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1193
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1194
        @Override
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1195
        public int hashCode() {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1196
            int result = opKind.hashCode();
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1197
            result *= 127;
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1198
            result += types.hashCode(op1);
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1199
            result *= 127;
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1200
            result += types.hashCode(op2);
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1201
            return result;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1202
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1203
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1204
        boolean apply(Warner warn) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1205
            return opKind.apply(op1, op2, warn, types);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1206
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1207
    }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1208
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1209
    /** an incorporation cache keeps track of all executed incorporation-related operations */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1210
    Map<IncorporationBinaryOp, Boolean> incorporationCache = new HashMap<>();
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1211
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1212
    protected static class BoundFilter implements Filter<Type> {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1213
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1214
        InferenceContext inferenceContext;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1215
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1216
        public BoundFilter(InferenceContext inferenceContext) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1217
            this.inferenceContext = inferenceContext;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1218
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1219
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1220
        @Override
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1221
        public boolean accepts(Type t) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1222
            return !t.isErroneous() && !inferenceContext.free(t) &&
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1223
                    !t.hasTag(BOT);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1224
        }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1225
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1226
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1227
    /**
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1228
     * Incorporation error: mismatch between inferred type and given bound.
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1229
     */
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1230
    void reportInstError(UndetVar uv, InferenceBound ib) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1231
        reportInferenceError(
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1232
                String.format("inferred.do.not.conform.to.%s.bounds", StringUtils.toLowerCase(ib.name())),
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1233
                uv.getInst(),
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1234
                uv.getBounds(ib));
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1235
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1236
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1237
    /**
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1238
     * Incorporation error: mismatch between two (or more) bounds of same kind.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1239
     */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1240
    void reportBoundError(UndetVar uv, InferenceBound ib) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1241
        reportInferenceError(
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1242
                String.format("incompatible.%s.bounds", StringUtils.toLowerCase(ib.name())),
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1243
                uv.qtype,
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1244
                uv.getBounds(ib));
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1245
    }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1246
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1247
    /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1248
     * Incorporation error: mismatch between two (or more) bounds of different kinds.
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1249
     */
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1250
    void reportBoundError(UndetVar uv, InferenceBound ib1, InferenceBound ib2) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1251
        reportInferenceError(
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1252
                String.format("incompatible.%s.%s.bounds",
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1253
                        StringUtils.toLowerCase(ib1.name()),
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1254
                        StringUtils.toLowerCase(ib2.name())),
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1255
                uv.qtype,
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1256
                uv.getBounds(ib1),
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1257
                uv.getBounds(ib2));
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1258
    }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1259
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1260
    /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1261
     * Helper method: reports an inference error.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1262
     */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1263
    void reportInferenceError(String key, Object... args) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1264
        throw inferenceException.setMessage(key, args);
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
    // </editor-fold>
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1267
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1268
    // <editor-fold defaultstate="collapsed" desc="Inference engine">
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1269
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1270
     * 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
  1271
     * 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
  1272
     * and (ii) tell th engine when we are done fixing inference variables
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1273
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1274
    interface GraphStrategy {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1275
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1276
        /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1277
         * A NodeNotFoundException is thrown whenever an inference strategy fails
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1278
         * to pick the next node to solve in the inference graph.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1279
         */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1280
        public static class NodeNotFoundException extends RuntimeException {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1281
            private static final long serialVersionUID = 0;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1282
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1283
            InferenceGraph graph;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1284
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1285
            public NodeNotFoundException(InferenceGraph graph) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1286
                this.graph = graph;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1287
            }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1288
        }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1289
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1290
         * Pick the next node (leaf) to solve in the graph
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1291
         */
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1292
        Node pickNode(InferenceGraph g) throws NodeNotFoundException;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1293
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1294
         * Is this the last step?
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1295
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1296
        boolean done();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1297
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1298
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1299
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1300
     * Simple solver strategy class that locates all leaves inside a graph
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1301
     * and picks the first leaf as the next node to solve
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1302
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1303
    abstract class LeafSolver implements GraphStrategy {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1304
        public Node pickNode(InferenceGraph g) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1305
            if (g.nodes.isEmpty()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1306
                //should not happen
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1307
                throw new NodeNotFoundException(g);
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1308
            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1309
            return g.nodes.get(0);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1310
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1311
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1312
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1313
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1314
     * This solver uses an heuristic to pick the best leaf - the heuristic
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1315
     * tries to select the node that has maximal probability to contain one
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1316
     * or more inference variables in a given list
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1317
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1318
    abstract class BestLeafSolver extends LeafSolver {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1319
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1320
        /** list of ivars of which at least one must be solved */
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1321
        List<Type> varsToSolve;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1322
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1323
        BestLeafSolver(List<Type> varsToSolve) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1324
            this.varsToSolve = varsToSolve;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1325
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1326
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1327
        /**
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1328
         * 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
  1329
         * Typically this will start from a node containing a variable in
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1330
         * {@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
  1331
         * number of type-variables that should be eagerly instantiated across that path.
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1332
         */
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1333
        Pair<List<Node>, Integer> computeTreeToLeafs(Node n) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1334
            Pair<List<Node>, Integer> cachedPath = treeCache.get(n);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1335
            if (cachedPath == null) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1336
                //cache miss
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1337
                if (n.isLeaf()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1338
                    //if leaf, stop
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1339
                    cachedPath = new Pair<>(List.of(n), n.data.length());
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1340
                } else {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1341
                    //if non-leaf, proceed recursively
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1342
                    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
  1343
                    for (Node n2 : n.getAllDependencies()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1344
                        if (n2 == n) continue;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1345
                        Pair<List<Node>, Integer> subpath = computeTreeToLeafs(n2);
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1346
                        path = new Pair<>(path.fst.prependList(subpath.fst),
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1347
                                          path.snd + subpath.snd);
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1348
                    }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1349
                    cachedPath = path;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1350
                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1351
                //save results in cache
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1352
                treeCache.put(n, cachedPath);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1353
            }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1354
            return cachedPath;
18916
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1355
        }
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1356
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1357
        /** cache used to avoid redundant computation of tree costs */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1358
        final Map<Node, Pair<List<Node>, Integer>> treeCache = new HashMap<>();
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1359
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1360
        /** constant value used to mark non-existent paths */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1361
        final Pair<List<Node>, Integer> noPath = new Pair<>(null, Integer.MAX_VALUE);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1362
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1363
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1364
         * Pick the leaf that minimize cost
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1365
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1366
        @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1367
        public Node pickNode(final InferenceGraph g) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1368
            treeCache.clear(); //graph changes at every step - cache must be cleared
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1369
            Pair<List<Node>, Integer> bestPath = noPath;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1370
            for (Node n : g.nodes) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1371
                if (!Collections.disjoint(n.data, varsToSolve)) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1372
                    Pair<List<Node>, Integer> path = computeTreeToLeafs(n);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1373
                    //discard all paths containing at least a node in the
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1374
                    //closure computed above
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1375
                    if (path.snd < bestPath.snd) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1376
                        bestPath = path;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1377
                    }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1378
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1379
            }
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1380
            if (bestPath == noPath) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1381
                //no path leads there
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1382
                throw new NodeNotFoundException(g);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1383
            }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1384
            return bestPath.fst.head;
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
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1387
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1388
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1389
     * 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
  1390
     * instantiates an inference variable using a subset of the inference variable
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1391
     * bounds, if certain condition are met. Decisions such as the sequence in which
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1392
     * 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
  1393
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1394
    enum InferenceStep {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1395
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1396
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1397
         * Instantiate an inference variables using one of its (ground) equality
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1398
         * constraints
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1399
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1400
        EQ(InferenceBound.EQ) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1401
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1402
            Type solve(UndetVar uv, InferenceContext inferenceContext) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1403
                return filterBounds(uv, inferenceContext).head;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1404
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1405
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1406
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1407
         * Instantiate an inference variables using its (ground) lower bounds. Such
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1408
         * bounds are merged together using lub().
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1409
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1410
        LOWER(InferenceBound.LOWER) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1411
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1412
            Type solve(UndetVar uv, InferenceContext inferenceContext) {
31937
ad43a6639c4a 8132215: class InferenceContext should live in a separate file
vromero
parents: 30014
diff changeset
  1413
                Infer infer = inferenceContext.infer;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1414
                List<Type> lobounds = filterBounds(uv, inferenceContext);
18395
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18008
diff changeset
  1415
                //note: lobounds should have at least one element
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18008
diff changeset
  1416
                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
  1417
                if (owntype.isPrimitive() || owntype.hasTag(ERROR)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1418
                    throw infer.inferenceException
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1419
                        .setMessage("no.unique.minimal.instance.exists",
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1420
                                    uv.qtype, lobounds);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1421
                } else {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1422
                    return owntype;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1423
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1424
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1425
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1426
        /**
18909
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1427
         * Infer uninstantiated/unbound inference variables occurring in 'throws'
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1428
         * clause as RuntimeException
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1429
         */
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1430
        THROWS(InferenceBound.UPPER) {
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1431
            @Override
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1432
            public boolean accepts(UndetVar t, InferenceContext inferenceContext) {
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1433
                if ((t.qtype.tsym.flags() & Flags.THROWS) == 0) {
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1434
                    //not a throws undet var
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1435
                    return false;
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
                if (t.getBounds(InferenceBound.EQ, InferenceBound.LOWER, InferenceBound.UPPER)
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1438
                            .diff(t.getDeclaredBounds()).nonEmpty()) {
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1439
                    //not an unbounded undet var
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1440
                    return false;
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1441
                }
31937
ad43a6639c4a 8132215: class InferenceContext should live in a separate file
vromero
parents: 30014
diff changeset
  1442
                Infer infer = inferenceContext.infer;
18909
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1443
                for (Type db : t.getDeclaredBounds()) {
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1444
                    if (t.isInterface()) continue;
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1445
                    if (infer.types.asSuper(infer.syms.runtimeExceptionType, db.tsym) != null) {
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1446
                        //declared bound is a supertype of RuntimeException
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1447
                        return true;
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
                }
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1450
                //declared bound is more specific then RuntimeException - give up
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1451
                return false;
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1452
            }
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1453
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1454
            @Override
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1455
            Type solve(UndetVar uv, InferenceContext inferenceContext) {
31937
ad43a6639c4a 8132215: class InferenceContext should live in a separate file
vromero
parents: 30014
diff changeset
  1456
                return inferenceContext.infer.syms.runtimeExceptionType;
18909
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1457
            }
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1458
        },
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1459
        /**
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1460
         * Instantiate an inference variables using its (ground) upper bounds. Such
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1461
         * bounds are merged together using glb().
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1462
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1463
        UPPER(InferenceBound.UPPER) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1464
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1465
            Type solve(UndetVar uv, InferenceContext inferenceContext) {
31937
ad43a6639c4a 8132215: class InferenceContext should live in a separate file
vromero
parents: 30014
diff changeset
  1466
                Infer infer = inferenceContext.infer;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1467
                List<Type> hibounds = filterBounds(uv, inferenceContext);
24293
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24226
diff changeset
  1468
                //note: hibounds should have at least one element
18395
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18008
diff changeset
  1469
                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
  1470
                if (owntype.isPrimitive() || owntype.hasTag(ERROR)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1471
                    throw infer.inferenceException
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1472
                        .setMessage("no.unique.maximal.instance.exists",
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1473
                                    uv.qtype, hibounds);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1474
                } else {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1475
                    return owntype;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1476
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1477
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1478
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1479
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1480
         * 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
  1481
         * if all upper bounds are ground.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1482
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1483
        UPPER_LEGACY(InferenceBound.UPPER) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1484
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1485
            public boolean accepts(UndetVar t, InferenceContext inferenceContext) {
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1486
                return !inferenceContext.free(t.getBounds(ib)) && !t.isCaptured();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1487
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1488
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1489
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1490
            Type solve(UndetVar uv, InferenceContext inferenceContext) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1491
                return UPPER.solve(uv, inferenceContext);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1492
            }
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1493
        },
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1494
        /**
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1495
         * 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
  1496
         * if all upper/lower bounds are ground.
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1497
         */
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1498
        CAPTURED(InferenceBound.UPPER) {
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1499
            @Override
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1500
            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
  1501
                return t.isCaptured() &&
8a0cbd5cb055 8020804: javac crashes when speculative attribution infers intersection type with array component
mcimadamore
parents: 18918
diff changeset
  1502
                        !inferenceContext.free(t.getBounds(InferenceBound.UPPER, InferenceBound.LOWER));
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1503
            }
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
            @Override
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1506
            Type solve(UndetVar uv, InferenceContext inferenceContext) {
31937
ad43a6639c4a 8132215: class InferenceContext should live in a separate file
vromero
parents: 30014
diff changeset
  1507
                Infer infer = inferenceContext.infer;
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1508
                Type upper = UPPER.filterBounds(uv, inferenceContext).nonEmpty() ?
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1509
                        UPPER.solve(uv, inferenceContext) :
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1510
                        infer.syms.objectType;
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1511
                Type lower = LOWER.filterBounds(uv, inferenceContext).nonEmpty() ?
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1512
                        LOWER.solve(uv, inferenceContext) :
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1513
                        infer.syms.botType;
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1514
                CapturedType prevCaptured = (CapturedType)uv.qtype;
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  1515
                return new CapturedType(prevCaptured.tsym.name, prevCaptured.tsym.owner,
26781
a786b07c7b91 8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents: 26663
diff changeset
  1516
                                        upper, lower, prevCaptured.wildcard);
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1517
            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1518
        };
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1519
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1520
        final InferenceBound ib;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1521
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1522
        InferenceStep(InferenceBound ib) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1523
            this.ib = ib;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1524
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1525
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1526
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1527
         * Find an instantiated type for a given inference variable within
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1528
         * a given inference context
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1529
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1530
        abstract Type solve(UndetVar uv, InferenceContext inferenceContext);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1531
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1532
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1533
         * Can the inference variable be instantiated using this step?
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1534
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1535
        public boolean accepts(UndetVar t, InferenceContext inferenceContext) {
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1536
            return filterBounds(t, inferenceContext).nonEmpty() && !t.isCaptured();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1537
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1538
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1539
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1540
         * 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
  1541
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1542
        List<Type> filterBounds(UndetVar uv, InferenceContext inferenceContext) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1543
            return Type.filter(uv.getBounds(ib), new BoundFilter(inferenceContext));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1544
        }
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
     * This enumeration defines the sequence of steps to be applied when the
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1549
     * solver works in legacy mode. The steps in this enumeration reflect
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1550
     * 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
  1551
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1552
    enum LegacyInferenceSteps {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1553
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1554
        EQ_LOWER(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER)),
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1555
        EQ_UPPER(EnumSet.of(InferenceStep.EQ, InferenceStep.UPPER_LEGACY));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1556
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1557
        final EnumSet<InferenceStep> steps;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1558
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1559
        LegacyInferenceSteps(EnumSet<InferenceStep> steps) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1560
            this.steps = steps;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1561
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1562
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1563
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1564
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1565
     * This enumeration defines the sequence of steps to be applied when the
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1566
     * graph solver is used. This order is defined so as to maximize compatibility
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1567
     * 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
  1568
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1569
    enum GraphInferenceSteps {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1570
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1571
        EQ(EnumSet.of(InferenceStep.EQ)),
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1572
        EQ_LOWER(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER)),
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1573
        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
  1574
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1575
        final EnumSet<InferenceStep> steps;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1576
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1577
        GraphInferenceSteps(EnumSet<InferenceStep> steps) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1578
            this.steps = steps;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1579
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1580
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1581
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1582
    /**
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1583
     * There are two kinds of dependencies between inference variables. The basic
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1584
     * kind of dependency (or bound dependency) arises when a variable mention
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1585
     * 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
  1586
     * of dependency that arises when a variable 'might' lead to better constraints
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1587
     * on another variable (this is typically the case with variables holding up
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1588
     * stuck expressions).
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1589
     */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1590
    enum DependencyKind implements GraphUtils.DependencyKind {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1591
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1592
        /** bound dependency */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1593
        BOUND("dotted"),
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1594
        /** stuck dependency */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1595
        STUCK("dashed");
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1596
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1597
        final String dotSyle;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1598
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1599
        private DependencyKind(String dotSyle) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1600
            this.dotSyle = dotSyle;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1601
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1602
    }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1603
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1604
    /**
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1605
     * This is the graph inference solver - the solver organizes all inference variables in
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1606
     * a given inference context by bound dependencies - in the general case, such dependencies
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1607
     * 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
  1608
     * an acyclic graph, where all cyclic variables are bundled together. An inference
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1609
     * 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
  1610
     * relying on a given strategy (see GraphStrategy).
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1611
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1612
    class GraphSolver {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1613
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1614
        InferenceContext inferenceContext;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1615
        Map<Type, Set<Type>> stuckDeps;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1616
        Warner warn;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1617
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1618
        GraphSolver(InferenceContext inferenceContext, Map<Type, Set<Type>> stuckDeps, Warner warn) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1619
            this.inferenceContext = inferenceContext;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1620
            this.stuckDeps = stuckDeps;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1621
            this.warn = warn;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1622
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1623
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1624
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1625
         * Solve variables in a given inference context. The amount of variables
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1626
         * 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
  1627
         * depends on the selected solver strategy.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1628
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1629
        void solve(GraphStrategy sstrategy) {
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1630
            doIncorporation(inferenceContext, warn); //initial propagation of bounds
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1631
            InferenceGraph inferenceGraph = new InferenceGraph(stuckDeps);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1632
            while (!sstrategy.done()) {
26267
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
  1633
                if (dependenciesFolder != null) {
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
  1634
                    //add this graph to the pending queue
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
  1635
                    pendingGraphs = pendingGraphs.prepend(inferenceGraph.toDot());
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
  1636
                }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1637
                InferenceGraph.Node nodeToSolve = sstrategy.pickNode(inferenceGraph);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1638
                List<Type> varsToSolve = List.from(nodeToSolve.data);
18904
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  1639
                List<Type> saved_undet = inferenceContext.save();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1640
                try {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1641
                    //repeat until all variables are solved
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1642
                    outer: while (Type.containsAny(inferenceContext.restvars(), varsToSolve)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1643
                        //for each inference phase
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1644
                        for (GraphInferenceSteps step : GraphInferenceSteps.values()) {
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1645
                            if (inferenceContext.solveBasic(varsToSolve, step.steps).nonEmpty()) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1646
                                doIncorporation(inferenceContext, warn);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1647
                                continue outer;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1648
                            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1649
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1650
                        //no progress
18395
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18008
diff changeset
  1651
                        throw inferenceException.setMessage();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1652
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1653
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1654
                catch (InferenceException ex) {
18395
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18008
diff changeset
  1655
                    //did we fail because of interdependent ivars?
18904
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  1656
                    inferenceContext.rollback(saved_undet);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1657
                    instantiateAsUninferredVars(varsToSolve, inferenceContext);
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1658
                    doIncorporation(inferenceContext, warn);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1659
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1660
                inferenceGraph.deleteNode(nodeToSolve);
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
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1664
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1665
         * The dependencies between the inference variables that need to be solved
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1666
         * form a (possibly cyclic) graph. This class reduces the original dependency graph
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1667
         * 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
  1668
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1669
        class InferenceGraph {
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
             * This class represents a node in the graph. Each node corresponds
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1673
             * to an inference variable and has edges (dependencies) on other
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1674
             * nodes. The node defines an entry point that can be used to receive
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1675
             * updates on the structure of the graph this node belongs to (used to
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1676
             * keep dependencies in sync).
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1677
             */
25844
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1678
            class Node extends GraphUtils.TarjanNode<ListBuffer<Type>, Node> implements DottableNode<ListBuffer<Type>, Node> {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1679
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1680
                /** map listing all dependencies (grouped by kind) */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1681
                EnumMap<DependencyKind, Set<Node>> deps;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1682
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1683
                Node(Type ivar) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1684
                    super(ListBuffer.of(ivar));
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1685
                    this.deps = new EnumMap<>(DependencyKind.class);
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1686
                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1687
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1688
                @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1689
                public GraphUtils.DependencyKind[] getSupportedDependencyKinds() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1690
                    return DependencyKind.values();
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
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1693
                public Iterable<? extends Node> getAllDependencies() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1694
                    return getDependencies(DependencyKind.values());
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1695
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1696
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1697
                @Override
25844
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1698
                public Collection<? extends Node> getDependenciesByKind(GraphUtils.DependencyKind dk) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1699
                    return getDependencies((DependencyKind)dk);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1700
                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1701
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1702
                /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1703
                 * Retrieves all dependencies with given kind(s).
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1704
                 */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1705
                protected Set<Node> getDependencies(DependencyKind... depKinds) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1706
                    Set<Node> buf = new LinkedHashSet<>();
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1707
                    for (DependencyKind dk : depKinds) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1708
                        Set<Node> depsByKind = deps.get(dk);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1709
                        if (depsByKind != null) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1710
                            buf.addAll(depsByKind);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1711
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1712
                    }
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1713
                    return buf;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1714
                }
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
                 * Adds dependency with given kind.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1718
                 */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1719
                protected void addDependency(DependencyKind dk, Node depToAdd) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1720
                    Set<Node> depsByKind = deps.get(dk);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1721
                    if (depsByKind == null) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1722
                        depsByKind = new LinkedHashSet<>();
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1723
                        deps.put(dk, depsByKind);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1724
                    }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1725
                    depsByKind.add(depToAdd);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1726
                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1727
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1728
                /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1729
                 * Add multiple dependencies of same given kind.
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
                protected void addDependencies(DependencyKind dk, Set<Node> depsToAdd) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1732
                    for (Node n : depsToAdd) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1733
                        addDependency(dk, n);
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
                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1736
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1737
                /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1738
                 * Remove a dependency, regardless of its kind.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1739
                 */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1740
                protected Set<DependencyKind> removeDependency(Node n) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1741
                    Set<DependencyKind> removedKinds = new HashSet<>();
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1742
                    for (DependencyKind dk : DependencyKind.values()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1743
                        Set<Node> depsByKind = deps.get(dk);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1744
                        if (depsByKind == null) continue;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1745
                        if (depsByKind.remove(n)) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1746
                            removedKinds.add(dk);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1747
                        }
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
                    return removedKinds;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1750
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1751
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1752
                /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1753
                 * Compute closure of a give node, by recursively walking
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1754
                 * through all its dependencies (of given kinds)
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1755
                 */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1756
                protected Set<Node> closure(DependencyKind... depKinds) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1757
                    boolean progress = true;
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1758
                    Set<Node> closure = new HashSet<>();
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1759
                    closure.add(this);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1760
                    while (progress) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1761
                        progress = false;
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1762
                        for (Node n1 : new HashSet<>(closure)) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1763
                            progress = closure.addAll(n1.getDependencies(depKinds));
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
                    return closure;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1767
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1768
19914
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
                 * 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
  1771
                 * or it just has self-dependencies.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1772
                 */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1773
                protected boolean isLeaf() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1774
                    //no deps, or only one self dep
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1775
                    Set<Node> allDeps = getDependencies(DependencyKind.BOUND, DependencyKind.STUCK);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1776
                    if (allDeps.isEmpty()) return true;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1777
                    for (Node n : allDeps) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1778
                        if (n != this) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1779
                            return false;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1780
                        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1781
                    }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1782
                    return true;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1783
                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1784
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
                 * Merge this node with another node, acquiring its dependencies.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1787
                 * This routine is used to merge all cyclic node together and
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1788
                 * form an acyclic graph.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1789
                 */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1790
                protected void mergeWith(List<? extends Node> nodes) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1791
                    for (Node n : nodes) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1792
                        Assert.check(n.data.length() == 1, "Attempt to merge a compound node!");
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1793
                        data.appendList(n.data);
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1794
                        for (DependencyKind dk : DependencyKind.values()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1795
                            addDependencies(dk, n.getDependencies(dk));
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1796
                        }
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
                    //update deps
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1799
                    EnumMap<DependencyKind, Set<Node>> deps2 = new EnumMap<>(DependencyKind.class);
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1800
                    for (DependencyKind dk : DependencyKind.values()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1801
                        for (Node d : getDependencies(dk)) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1802
                            Set<Node> depsByKind = deps2.get(dk);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1803
                            if (depsByKind == null) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1804
                                depsByKind = new LinkedHashSet<>();
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1805
                                deps2.put(dk, depsByKind);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1806
                            }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1807
                            if (data.contains(d.data.first())) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1808
                                depsByKind.add(this);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1809
                            } else {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1810
                                depsByKind.add(d);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1811
                            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1812
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1813
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1814
                    deps = deps2;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1815
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1816
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1817
                /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1818
                 * Notify all nodes that something has changed in the graph
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1819
                 * topology.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1820
                 */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1821
                private void graphChanged(Node from, Node to) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1822
                    for (DependencyKind dk : removeDependency(from)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1823
                        if (to != null) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1824
                            addDependency(dk, to);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1825
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1826
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1827
                }
25844
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1828
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1829
                @Override
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1830
                public Properties nodeAttributes() {
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1831
                    Properties p = new Properties();
26267
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
  1832
                    p.put("label", "\"" + toString() + "\"");
25844
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1833
                    return p;
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1834
                }
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1835
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1836
                @Override
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1837
                public Properties dependencyAttributes(Node sink, GraphUtils.DependencyKind dk) {
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1838
                    Properties p = new Properties();
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1839
                    p.put("style", ((DependencyKind)dk).dotSyle);
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1840
                    if (dk == DependencyKind.STUCK) return p;
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1841
                    else {
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1842
                        StringBuilder buf = new StringBuilder();
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1843
                        String sep = "";
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1844
                        for (Type from : data) {
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1845
                            UndetVar uv = (UndetVar)inferenceContext.asUndetVar(from);
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1846
                            for (Type bound : uv.getBounds(InferenceBound.values())) {
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1847
                                if (bound.containsAny(List.from(sink.data))) {
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1848
                                    buf.append(sep);
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1849
                                    buf.append(bound);
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1850
                                    sep = ",";
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1851
                                }
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1852
                            }
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1853
                        }
26267
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
  1854
                        p.put("label", "\"" + buf.toString() + "\"");
25844
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1855
                    }
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1856
                    return p;
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1857
                }
15717
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
31937
ad43a6639c4a 8132215: class InferenceContext should live in a separate file
vromero
parents: 30014
diff changeset
  1971
    final InferenceContext emptyContext;
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1972
    // </editor-fold>
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1973
}