langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Infer.java
author mcimadamore
Fri, 08 Apr 2016 10:52:26 +0100
changeset 37003 12ece14d32e0
parent 36995 e19153419efd
child 38516 b643c42e9d25
permissions -rw-r--r--
8152832: Type inference regression in javac Summary: Stale incoropration actions caused missing inference bounds 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
36995
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   563
        if (spMethod == null || types.isSameType(spMethod.getReturnType(), syms.objectType, true)) {
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   564
            // The return type of the polymorphic signature is polymorphic,
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   565
            // and is computed from the enclosing tree E, as follows:
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   566
            // if E is a cast, then use the target type of the cast expression
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   567
            // as a return type; if E is an expression statement, the return
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   568
            // type is 'void'; otherwise
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   569
            // the return type is simply 'Object'. A correctness check ensures
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   570
            // that env.next refers to the lexically enclosing environment in
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   571
            // which 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
   572
36995
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   573
            switch (env.next.tree.getTag()) {
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   574
                case TYPECAST:
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   575
                    JCTypeCast castTree = (JCTypeCast)env.next.tree;
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   576
                    restype = (TreeInfo.skipParens(castTree.expr) == env.tree) ?
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   577
                              castTree.clazz.type :
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   578
                              syms.objectType;
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   579
                    break;
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   580
                case EXEC:
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   581
                    JCTree.JCExpressionStatement execTree =
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   582
                            (JCTree.JCExpressionStatement)env.next.tree;
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   583
                    restype = (TreeInfo.skipParens(execTree.expr) == env.tree) ?
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   584
                              syms.voidType :
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   585
                              syms.objectType;
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   586
                    break;
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   587
                default:
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   588
                    restype = syms.objectType;
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   589
            }
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   590
        } else {
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   591
            // The return type of the polymorphic signature is fixed
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   592
            // (not polymorphic)
e19153419efd 8149644: Integrate VarHandles
psandoz
parents: 36157
diff changeset
   593
            restype = spMethod.getReturnType();
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   594
        }
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   595
29554
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29147
diff changeset
   596
        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
   597
        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
   598
            spMethod.getThrownTypes() :
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   599
            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
   600
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   601
        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
   602
                                          restype,
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   603
                                          exType,
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   604
                                          syms.methodClass);
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   605
        return mtype;
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   606
    }
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   607
    //where
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   608
        class ImplicitArgType extends DeferredAttr.DeferredTypeMap {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   609
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   610
            public ImplicitArgType(Symbol msym, Resolve.MethodResolutionPhase phase) {
25279
a665d5cbb8c3 8044546: Crash on faulty reduce/lambda
vromero
parents: 24795
diff changeset
   611
                (rs.deferredAttr).super(AttrMode.SPECULATIVE, msym, phase);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   612
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   613
29554
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29147
diff changeset
   614
            @Override
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29147
diff changeset
   615
            public Type visitClassType(ClassType t, Void aVoid) {
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29147
diff changeset
   616
                return types.erasure(t);
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29147
diff changeset
   617
            }
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29147
diff changeset
   618
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29147
diff changeset
   619
            @Override
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29147
diff changeset
   620
            public Type visitType(Type t, Void _unused) {
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29147
diff changeset
   621
                if (t.hasTag(DEFERRED)) {
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29147
diff changeset
   622
                    return visit(super.visitType(t, null));
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29147
diff changeset
   623
                } else if (t.hasTag(BOT))
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   624
                    // 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
   625
                    // infer as java.lang.Void for now
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   626
                    t = types.boxedClass(syms.voidType).type;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   627
                return t;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   628
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   629
        }
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   630
33906
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   631
    TypeMapping<Void> fromTypeVarFun = new TypeMapping<Void>() {
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   632
        @Override
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   633
        public Type visitTypeVar(TypeVar tv, Void aVoid) {
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   634
            return new UndetVar(tv, incorporationEngine(), types);
33906
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   635
        }
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   636
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   637
        @Override
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   638
        public Type visitCapturedType(CapturedType t, Void aVoid) {
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   639
            return new CapturedUndetVar(t, incorporationEngine(), types);
33906
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   640
        }
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   641
    };
91b6bee06dc1 8046685: Uncompilable large expressions involving generics.
mcimadamore
parents: 33708
diff changeset
   642
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   643
    /**
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   644
      * 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
   645
      * SAM type contains one or more wildcards. An inference process is applied
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   646
      * so that wildcard bounds, as well as explicit lambda/method ref parameters
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   647
      * (where applicable) are used to constraint the solution.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   648
      */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   649
    public Type instantiateFunctionalInterface(DiagnosticPosition pos, Type funcInterface,
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   650
            List<Type> paramTypes, Check.CheckContext checkContext) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   651
        if (types.capture(funcInterface) == funcInterface) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   652
            //if capture doesn't change the type then return the target unchanged
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   653
            //(this means the target contains no wildcards!)
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   654
            return funcInterface;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   655
        } else {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   656
            Type formalInterface = funcInterface.tsym.type;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   657
            InferenceContext funcInterfaceContext =
31937
ad43a6639c4a 8132215: class InferenceContext should live in a separate file
vromero
parents: 30014
diff changeset
   658
                    new InferenceContext(this, funcInterface.tsym.type.getTypeArguments());
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   659
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   660
            Assert.check(paramTypes != null);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   661
            //get constraints from explicit params (this is done by
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   662
            //checking that explicit param types are equal to the ones
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   663
            //in the functional interface descriptors)
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   664
            List<Type> descParameterTypes = types.findDescriptorType(formalInterface).getParameterTypes();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   665
            if (descParameterTypes.size() != paramTypes.size()) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   666
                checkContext.report(pos, diags.fragment("incompatible.arg.types.in.lambda"));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   667
                return types.createErrorType(funcInterface);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   668
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   669
            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
   670
                if (!types.isSameType(funcInterfaceContext.asUndetVar(p), paramTypes.head)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   671
                    checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   672
                    return types.createErrorType(funcInterface);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   673
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   674
                paramTypes = paramTypes.tail;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   675
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   676
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   677
            try {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   678
                funcInterfaceContext.solve(funcInterfaceContext.boundedVars(), types.noWarnings);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   679
            } catch (InferenceException ex) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   680
                checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   681
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   682
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   683
            List<Type> actualTypeargs = funcInterface.getTypeArguments();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   684
            for (Type t : funcInterfaceContext.undetvars) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   685
                UndetVar uv = (UndetVar)t;
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   686
                if (uv.getInst() == null) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   687
                    uv.setInst(actualTypeargs.head);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   688
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   689
                actualTypeargs = actualTypeargs.tail;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   690
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   691
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   692
            Type owntype = funcInterfaceContext.asInstType(formalInterface);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   693
            if (!chk.checkValidGenericType(owntype)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   694
                //if the inferred functional interface type is not well-formed,
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   695
                //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
   696
                checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   697
            }
25279
a665d5cbb8c3 8044546: Crash on faulty reduce/lambda
vromero
parents: 24795
diff changeset
   698
            //propagate constraints as per JLS 18.2.1
a665d5cbb8c3 8044546: Crash on faulty reduce/lambda
vromero
parents: 24795
diff changeset
   699
            checkContext.compatible(owntype, funcInterface, types.noWarnings);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   700
            return owntype;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   701
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   702
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   703
    // </editor-fold>
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   704
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   705
    // <editor-fold defaultstate="collapsed" desc="Incorporation">
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   706
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   707
    /**
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   708
     * This class is the root of all incorporation actions.
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   709
     */
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   710
    public abstract class IncorporationAction {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   711
        UndetVar uv;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   712
        Type t;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   713
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   714
        IncorporationAction(UndetVar uv, Type t) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   715
            this.uv = uv;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   716
            this.t = t;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   717
        }
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   718
37003
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   719
        public abstract IncorporationAction dup(UndetVar that);
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   720
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   721
        /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   722
         * Incorporation action entry-point. Subclasses should define the logic associated with
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   723
         * this incorporation action.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   724
         */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   725
        abstract void apply(InferenceContext ic, Warner warn);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   726
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   727
        /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   728
         * Helper function: perform subtyping through incorporation cache.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   729
         */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   730
        boolean isSubtype(Type s, Type t, Warner warn) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   731
            return doIncorporationOp(IncorporationBinaryOpKind.IS_SUBTYPE, s, t, warn);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   732
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   733
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   734
        /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   735
         * Helper function: perform type-equivalence through incorporation cache.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   736
         */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   737
        boolean isSameType(Type s, Type t) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   738
            return doIncorporationOp(IncorporationBinaryOpKind.IS_SAME_TYPE, s, t, null);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   739
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   740
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   741
        @Override
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   742
        public String toString() {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   743
            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
   744
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   745
    }
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   746
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   747
    /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   748
     * Bound-check incorporation action. A newly added bound is checked against existing bounds,
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   749
     * 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
   750
     */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   751
    class CheckBounds extends IncorporationAction {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   752
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   753
        InferenceBound from;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   754
        BiFunction<InferenceContext, Type, Type> typeFunc;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   755
        BiPredicate<InferenceContext, Type> optFilter;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   756
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   757
        CheckBounds(UndetVar uv, Type t, InferenceBound from) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   758
            this(uv, t, InferenceContext::asUndetVar, null, from);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   759
        }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   760
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   761
        CheckBounds(UndetVar uv, Type t, BiFunction<InferenceContext, Type, Type> typeFunc,
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   762
                    BiPredicate<InferenceContext, Type> typeFilter, InferenceBound from) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   763
            super(uv, t);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   764
            this.from = from;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   765
            this.typeFunc = typeFunc;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   766
            this.optFilter = typeFilter;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   767
        }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   768
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   769
        @Override
37003
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   770
        public IncorporationAction dup(UndetVar that) {
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   771
            return new CheckBounds(that, t, typeFunc, optFilter, from);
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   772
        }
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   773
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   774
        @Override
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   775
        void apply(InferenceContext inferenceContext, Warner warn) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   776
            t = typeFunc.apply(inferenceContext, t);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   777
            if (optFilter != null && optFilter.test(inferenceContext, t)) return;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   778
            for (InferenceBound to : boundsToCheck()) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   779
                for (Type b : uv.getBounds(to)) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   780
                    b = typeFunc.apply(inferenceContext, b);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   781
                    if (optFilter != null && optFilter.test(inferenceContext, b)) continue;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   782
                    boolean success = checkBound(t, b, from, to, warn);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   783
                    if (!success) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   784
                        report(from, to);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   785
                    }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   786
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   787
            }
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
   788
        }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   789
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   790
        /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   791
         * The list of bound kinds to be checked.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   792
         */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   793
        EnumSet<InferenceBound> boundsToCheck() {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   794
            return (from == InferenceBound.EQ) ?
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   795
                            EnumSet.allOf(InferenceBound.class) :
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   796
                            EnumSet.complementOf(EnumSet.of(from));
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
        /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   800
         * 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
   801
         */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   802
        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
   803
            if (ib_s.lessThan(ib_t)) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   804
                return isSubtype(s, t, warn);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   805
            } else if (ib_t.lessThan(ib_s)) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   806
                return isSubtype(t, s, warn);
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
                return isSameType(s, t);
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
        /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   813
         * Report a bound check error.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   814
         */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   815
        void report(InferenceBound from, InferenceBound to) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   816
            //this is a workaround to preserve compatibility with existing messages
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   817
            if (from == to) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   818
                reportBoundError(uv, from);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   819
            } else if (from == InferenceBound.LOWER || to == InferenceBound.EQ) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   820
                reportBoundError(uv, to, from);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   821
            } else {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   822
                reportBoundError(uv, from, to);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   823
            }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   824
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   825
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   826
        @Override
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   827
        public String toString() {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   828
            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
   829
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   830
    }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   831
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   832
    /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   833
     * Custom check executed by the legacy incorporation engine. Newly added bounds are checked
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   834
     * against existing eq bounds.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   835
     */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   836
    class EqCheckLegacy extends CheckBounds {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   837
        EqCheckLegacy(UndetVar uv, Type t, InferenceBound from) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   838
            super(uv, t, InferenceContext::asInstType, InferenceContext::free, from);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   839
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   840
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   841
        @Override
37003
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   842
        public IncorporationAction dup(UndetVar that) {
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   843
            return new EqCheckLegacy(that, t, from);
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   844
        }
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   845
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   846
        @Override
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   847
        EnumSet<InferenceBound> boundsToCheck() {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   848
            return (from == InferenceBound.EQ) ?
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   849
                            EnumSet.allOf(InferenceBound.class) :
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   850
                            EnumSet.of(InferenceBound.EQ);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   851
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   852
    }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   853
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   854
    /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   855
     * Check that the inferred type conforms to all bounds.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   856
     */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   857
    class CheckInst extends CheckBounds {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   858
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   859
        EnumSet<InferenceBound> to;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   860
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   861
        CheckInst(UndetVar uv, InferenceBound ib, InferenceBound... rest) {
37003
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   862
            this(uv, EnumSet.of(ib, rest));
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   863
        }
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   864
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   865
        CheckInst(UndetVar uv, EnumSet<InferenceBound> to) {
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   866
            super(uv, uv.getInst(), InferenceBound.EQ);
37003
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   867
            this.to = to;
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   868
        }
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   869
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   870
        @Override
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   871
        public IncorporationAction dup(UndetVar that) {
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   872
            return new CheckInst(that, to);
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   873
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   874
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   875
        @Override
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   876
        EnumSet<InferenceBound> boundsToCheck() {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   877
            return to;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   878
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   879
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   880
        @Override
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   881
        void report(InferenceBound from, InferenceBound to) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   882
            reportInstError(uv, to);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   883
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   884
    }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   885
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   886
    /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   887
     * 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
   888
     */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   889
    class SubstBounds extends CheckInst {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   890
        SubstBounds(UndetVar uv) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   891
            super(uv, InferenceBound.LOWER, InferenceBound.EQ, InferenceBound.UPPER);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   892
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   893
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   894
        @Override
37003
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   895
        public IncorporationAction dup(UndetVar that) {
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   896
            return new SubstBounds(that);
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   897
        }
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   898
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   899
        @Override
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   900
        void apply(InferenceContext inferenceContext, Warner warn) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   901
            for (Type undet : inferenceContext.undetvars) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   902
                //we could filter out variables not mentioning uv2...
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   903
                UndetVar uv2 = (UndetVar)undet;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   904
                uv2.substBounds(List.of(uv.qtype), List.of(uv.getInst()), types);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   905
                checkCompatibleUpperBounds(uv2, inferenceContext);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   906
            }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   907
            super.apply(inferenceContext, warn);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   908
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   909
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   910
        /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   911
         * 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
   912
         * variable by making sure that a glb exists.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   913
         */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   914
        void checkCompatibleUpperBounds(UndetVar uv, InferenceContext inferenceContext) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   915
            List<Type> hibounds =
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   916
                    Type.filter(uv.getBounds(InferenceBound.UPPER), new BoundFilter(inferenceContext));
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   917
            final Type hb;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   918
            if (hibounds.isEmpty())
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   919
                hb = syms.objectType;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   920
            else if (hibounds.tail.isEmpty())
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   921
                hb = hibounds.head;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   922
            else
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   923
                hb = types.glb(hibounds);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   924
            if (hb == null || hb.isErroneous())
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   925
                reportBoundError(uv, InferenceBound.UPPER);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   926
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   927
    }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   928
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   929
    /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   930
     * Perform pairwise comparison between common generic supertypes of two upper bounds.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   931
     */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   932
    class CheckUpperBounds extends IncorporationAction {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   933
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   934
        public CheckUpperBounds(UndetVar uv, Type t) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   935
            super(uv, t);
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
        @Override
37003
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   939
        public IncorporationAction dup(UndetVar that) {
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   940
            return new CheckUpperBounds(that, t);
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   941
        }
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   942
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   943
        @Override
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   944
        void apply(InferenceContext inferenceContext, Warner warn) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   945
            List<Type> boundList = uv.getBounds(InferenceBound.UPPER).stream()
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   946
                    .collect(types.closureCollector(true, types::isSameType));
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   947
            for (Type b2 : boundList) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   948
                if (t == b2) continue;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   949
                    /* This wildcard check is temporary workaround. This code may need to be
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   950
                     * revisited once spec bug JDK-7034922 is fixed.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   951
                     */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   952
                if (t != b2 && !t.hasTag(WILDCARD) && !b2.hasTag(WILDCARD)) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   953
                    for (Pair<Type, Type> commonSupers : getParameterizedSupers(t, b2)) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   954
                        List<Type> allParamsSuperBound1 = commonSupers.fst.allparams();
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   955
                        List<Type> allParamsSuperBound2 = commonSupers.snd.allparams();
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   956
                        while (allParamsSuperBound1.nonEmpty() && allParamsSuperBound2.nonEmpty()) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   957
                            //traverse the list of all params comparing them
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   958
                            if (!allParamsSuperBound1.head.hasTag(WILDCARD) &&
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   959
                                    !allParamsSuperBound2.head.hasTag(WILDCARD)) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   960
                                if (!isSameType(inferenceContext.asUndetVar(allParamsSuperBound1.head),
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   961
                                        inferenceContext.asUndetVar(allParamsSuperBound2.head))) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   962
                                    reportBoundError(uv, InferenceBound.UPPER);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   963
                                }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   964
                            }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   965
                            allParamsSuperBound1 = allParamsSuperBound1.tail;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   966
                            allParamsSuperBound2 = allParamsSuperBound2.tail;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   967
                        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   968
                        Assert.check(allParamsSuperBound1.isEmpty() && allParamsSuperBound2.isEmpty());
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   969
                    }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   970
                }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   971
            }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   972
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   973
    }
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
     * 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
   977
     * kind of propagation occur:
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   978
     *
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   979
     * <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
   980
     * <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
   981
     * <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
   982
     */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   983
    class PropagateBounds extends IncorporationAction {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   984
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   985
        InferenceBound ib;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   986
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   987
        public PropagateBounds(UndetVar uv, Type t, InferenceBound ib) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   988
            super(uv, t);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   989
            this.ib = ib;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   990
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   991
37003
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   992
        @Override
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   993
        public IncorporationAction dup(UndetVar that) {
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   994
            return new PropagateBounds(that, t, ib);
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   995
        }
12ece14d32e0 8152832: Type inference regression in javac
mcimadamore
parents: 36995
diff changeset
   996
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   997
        void apply(InferenceContext inferenceContext, Warner warner) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   998
            Type undetT = inferenceContext.asUndetVar(t);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
   999
            if (undetT.hasTag(UNDETVAR) && !((UndetVar)undetT).isCaptured()) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1000
                UndetVar uv2 = (UndetVar)undetT;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1001
                //symmetric propagation
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1002
                uv2.addBound(ib.complement(), uv, types);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1003
                //backwards propagation
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1004
                for (InferenceBound ib2 : backwards()) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1005
                    for (Type b : uv2.getBounds(ib2)) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1006
                        uv.addBound(ib2, b, types);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1007
                    }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1008
                }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1009
            }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1010
            //forward propagation
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1011
            for (InferenceBound ib2 : forward()) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1012
                for (Type l : uv.getBounds(ib2)) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1013
                    Type undet = inferenceContext.asUndetVar(l);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1014
                    if (undet.hasTag(TypeTag.UNDETVAR) && !((UndetVar)undet).isCaptured()) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1015
                        UndetVar uv2 = (UndetVar)undet;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1016
                        uv2.addBound(ib, inferenceContext.asInstType(t), types);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1017
                    }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1018
                }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1019
            }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1020
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1021
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1022
        EnumSet<InferenceBound> forward() {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1023
            return (ib == InferenceBound.EQ) ?
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1024
                    EnumSet.of(InferenceBound.EQ) : EnumSet.complementOf(EnumSet.of(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
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1027
        EnumSet<InferenceBound> backwards() {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1028
            return (ib == InferenceBound.EQ) ?
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1029
                    EnumSet.allOf(InferenceBound.class) : EnumSet.of(ib);
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
        @Override
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1033
        public String toString() {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1034
            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
  1035
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1036
    }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1037
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1038
    /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1039
     * This class models an incorporation engine. The engine is responsible for listening to
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1040
     * changes in inference variables and register incorporation actions accordingly.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1041
     */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1042
    abstract class AbstractIncorporationEngine implements UndetVarListener {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1043
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1044
        @Override
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1045
        public void varInstantiated(UndetVar uv) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1046
            uv.incorporationActions.addFirst(new SubstBounds(uv));
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
        @Override
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1050
        public void varBoundChanged(UndetVar uv, InferenceBound ib, Type bound, boolean update) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1051
            if (uv.isCaptured()) return;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1052
            uv.incorporationActions.addAll(getIncorporationActions(uv, ib, bound, update));
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1053
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1054
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1055
        abstract List<IncorporationAction> getIncorporationActions(UndetVar uv, InferenceBound ib, Type t, boolean update);
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
     * A legacy incorporation engine. Used for source <= 7.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1060
     */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1061
    AbstractIncorporationEngine legacyEngine = new AbstractIncorporationEngine() {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1062
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1063
        List<IncorporationAction> getIncorporationActions(UndetVar uv, InferenceBound ib, Type t, boolean update) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1064
            ListBuffer<IncorporationAction> actions = new ListBuffer<>();
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1065
            Type inst = uv.getInst();
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1066
            if (inst != null) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1067
                actions.add(new CheckInst(uv, ib));
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1068
            }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1069
            actions.add(new EqCheckLegacy(uv, t, ib));
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1070
            return actions.toList();
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1071
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1072
    };
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1073
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1074
    /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1075
     * The standard incorporation engine. Used for source >= 8.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1076
     */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1077
    AbstractIncorporationEngine graphEngine = new AbstractIncorporationEngine() {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1078
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1079
        @Override
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1080
        List<IncorporationAction> getIncorporationActions(UndetVar uv, InferenceBound ib, Type t, boolean update) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1081
            ListBuffer<IncorporationAction> actions = new ListBuffer<>();
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1082
            Type inst = uv.getInst();
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1083
            if (inst != null) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1084
                actions.add(new CheckInst(uv, ib));
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1085
            }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1086
            actions.add(new CheckBounds(uv, t, ib));
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1087
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1088
            if (update) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1089
                return actions.toList();
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1090
            }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1091
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1092
            if (ib == InferenceBound.UPPER) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1093
                actions.add(new CheckUpperBounds(uv, t));
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1094
            }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1095
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1096
            actions.add(new PropagateBounds(uv, t, ib));
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1097
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1098
            return actions.toList();
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1099
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1100
    };
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1101
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1102
    /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1103
     * Get the incorporation engine to be used in this compilation.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1104
     */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1105
    AbstractIncorporationEngine incorporationEngine() {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1106
        return allowGraphInference ? graphEngine : legacyEngine;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1107
    }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1108
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1109
    /** max number of incorporation rounds. */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1110
    static final int MAX_INCORPORATION_STEPS = 10000;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1111
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1112
    /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1113
     * Check bounds and perform incorporation.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1114
     */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1115
    void doIncorporation(InferenceContext inferenceContext, Warner warn) throws InferenceException {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1116
        try {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1117
            boolean progress = true;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1118
            int round = 0;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1119
            while (progress && round < MAX_INCORPORATION_STEPS) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1120
                progress = false;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1121
                for (Type t : inferenceContext.undetvars) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1122
                    UndetVar uv = (UndetVar)t;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1123
                    if (!uv.incorporationActions.isEmpty()) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1124
                        progress = true;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1125
                        uv.incorporationActions.removeFirst().apply(inferenceContext, warn);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1126
                    }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1127
                }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1128
                round++;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1129
            }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1130
        } finally {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1131
            incorporationCache.clear();
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1132
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1133
    }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1134
24795
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1135
    /* 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
  1136
     * 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
  1137
     * 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
  1138
     * 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
  1139
     * 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
  1140
     *
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
  1141
     * As an example for the following input:
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
  1142
     *
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
  1143
     * t = java.util.ArrayList<java.lang.String>
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
  1144
     * s = java.util.List<T>
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
  1145
     *
24795
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1146
     * we get this ouput (singleton list):
24063
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
  1147
     *
24795
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1148
     * [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
  1149
     */
24795
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1150
    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
  1151
        Type lubResult = types.lub(t, s);
24795
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1152
        if (lubResult == syms.errType || lubResult == syms.botType) {
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1153
            return List.nil();
24063
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
  1154
        }
29147
4cba0458106b 8071291: Compiler crashes trying to cast UnionType to IntersectionClassType
mcimadamore
parents: 27391
diff changeset
  1155
        List<Type> supertypesToCheck = lubResult.isIntersection() ?
24795
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1156
                ((IntersectionClassType)lubResult).getComponents() :
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1157
                List.of(lubResult);
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1158
        ListBuffer<Pair<Type, Type>> commonSupertypes = new ListBuffer<>();
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1159
        for (Type sup : supertypesToCheck) {
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1160
            if (sup.isParameterized()) {
27391
17f7b6c40c6d 8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents: 27228
diff changeset
  1161
                Type asSuperOfT = asSuper(t, sup);
17f7b6c40c6d 8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents: 27228
diff changeset
  1162
                Type asSuperOfS = asSuper(s, sup);
24795
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1163
                commonSupertypes.add(new Pair<>(asSuperOfT, asSuperOfS));
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1164
            }
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1165
        }
ff348aab3b54 8043893: Inference doesn't report error on incompatible upper bounds
mcimadamore
parents: 24396
diff changeset
  1166
        return commonSupertypes.toList();
24063
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
  1167
    }
27391
17f7b6c40c6d 8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents: 27228
diff changeset
  1168
    //where
17f7b6c40c6d 8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents: 27228
diff changeset
  1169
        private Type asSuper(Type t, Type sup) {
17f7b6c40c6d 8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents: 27228
diff changeset
  1170
            return (sup.hasTag(ARRAY)) ?
17f7b6c40c6d 8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents: 27228
diff changeset
  1171
                    new ArrayType(asSuper(types.elemtype(t), types.elemtype(sup)), syms.arrayClass) :
17f7b6c40c6d 8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents: 27228
diff changeset
  1172
                    types.asSuper(t, sup.tsym);
17f7b6c40c6d 8062977: Inference: NullPointerException during bound incorporation
mcimadamore
parents: 27228
diff changeset
  1173
        }
24063
3e3c18700277 8029002: javac should take multiple upper bounds into account in incorporation
vromero
parents: 23395
diff changeset
  1174
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1175
    boolean doIncorporationOp(IncorporationBinaryOpKind opKind, Type op1, Type op2, Warner warn) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1176
            IncorporationBinaryOp newOp = new IncorporationBinaryOp(opKind, op1, op2);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1177
            Boolean res = incorporationCache.get(newOp);
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1178
            if (res == null) {
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1179
                incorporationCache.put(newOp, res = newOp.apply(warn));
18918
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
            return res;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1182
        }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1183
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1184
    /**
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1185
     * 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
  1186
     * (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
  1187
     * upper/lower/eq bound).
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1188
     */
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1189
    enum IncorporationBinaryOpKind {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1190
        IS_SUBTYPE() {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1191
            @Override
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1192
            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
  1193
                return types.isSubtypeUnchecked(op1, op2, warn);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1194
            }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1195
        },
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1196
        IS_SAME_TYPE() {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1197
            @Override
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1198
            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
  1199
                return types.isSameType(op1, op2);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1200
            }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1201
        };
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
        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
  1204
    }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1205
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
     * This class encapsulates a basic incorporation operation; incorporation
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1208
     * 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
  1209
     * 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
  1210
     * 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
  1211
     * same bounds over and over).
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1212
     */
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1213
    class IncorporationBinaryOp {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1214
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1215
        IncorporationBinaryOpKind opKind;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1216
        Type op1;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1217
        Type op2;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1218
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1219
        IncorporationBinaryOp(IncorporationBinaryOpKind opKind, Type op1, Type op2) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1220
            this.opKind = opKind;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1221
            this.op1 = op1;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1222
            this.op2 = op2;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1223
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1224
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1225
        @Override
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1226
        public boolean equals(Object o) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1227
            if (!(o instanceof IncorporationBinaryOp)) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1228
                return false;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1229
            } else {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1230
                IncorporationBinaryOp that = (IncorporationBinaryOp)o;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1231
                return opKind == that.opKind &&
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1232
                        types.isSameType(op1, that.op1, true) &&
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1233
                        types.isSameType(op2, that.op2, true);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1234
            }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1235
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1236
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1237
        @Override
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1238
        public int hashCode() {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1239
            int result = opKind.hashCode();
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1240
            result *= 127;
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1241
            result += types.hashCode(op1);
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1242
            result *= 127;
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1243
            result += types.hashCode(op2);
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1244
            return result;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1245
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1246
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1247
        boolean apply(Warner warn) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1248
            return opKind.apply(op1, op2, warn, types);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1249
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1250
    }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1251
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1252
    /** an incorporation cache keeps track of all executed incorporation-related operations */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1253
    Map<IncorporationBinaryOp, Boolean> incorporationCache = new HashMap<>();
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1254
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1255
    protected static class BoundFilter implements Filter<Type> {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1256
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1257
        InferenceContext inferenceContext;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1258
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1259
        public BoundFilter(InferenceContext inferenceContext) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1260
            this.inferenceContext = inferenceContext;
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1261
        }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1262
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1263
        @Override
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1264
        public boolean accepts(Type t) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1265
            return !t.isErroneous() && !inferenceContext.free(t) &&
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1266
                    !t.hasTag(BOT);
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1267
        }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1268
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1269
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1270
    /**
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1271
     * Incorporation error: mismatch between inferred type and given bound.
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1272
     */
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1273
    void reportInstError(UndetVar uv, InferenceBound ib) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1274
        reportInferenceError(
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1275
                String.format("inferred.do.not.conform.to.%s.bounds", StringUtils.toLowerCase(ib.name())),
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1276
                uv.getInst(),
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1277
                uv.getBounds(ib));
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1278
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1279
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1280
    /**
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1281
     * Incorporation error: mismatch between two (or more) bounds of same kind.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1282
     */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1283
    void reportBoundError(UndetVar uv, InferenceBound ib) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1284
        reportInferenceError(
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1285
                String.format("incompatible.%s.bounds", StringUtils.toLowerCase(ib.name())),
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1286
                uv.qtype,
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1287
                uv.getBounds(ib));
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1288
    }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1289
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1290
    /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1291
     * Incorporation error: mismatch between two (or more) bounds of different kinds.
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1292
     */
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1293
    void reportBoundError(UndetVar uv, InferenceBound ib1, InferenceBound ib2) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1294
        reportInferenceError(
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1295
                String.format("incompatible.%s.%s.bounds",
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1296
                        StringUtils.toLowerCase(ib1.name()),
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1297
                        StringUtils.toLowerCase(ib2.name())),
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1298
                uv.qtype,
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1299
                uv.getBounds(ib1),
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1300
                uv.getBounds(ib2));
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1301
    }
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1302
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1303
    /**
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1304
     * Helper method: reports an inference error.
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1305
     */
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1306
    void reportInferenceError(String key, Object... args) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1307
        throw inferenceException.setMessage(key, args);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1308
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1309
    // </editor-fold>
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1310
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1311
    // <editor-fold defaultstate="collapsed" desc="Inference engine">
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1312
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1313
     * 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
  1314
     * 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
  1315
     * and (ii) tell th engine when we are done fixing inference variables
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1316
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1317
    interface GraphStrategy {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1318
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1319
        /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1320
         * A NodeNotFoundException is thrown whenever an inference strategy fails
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1321
         * to pick the next node to solve in the inference graph.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1322
         */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1323
        public static class NodeNotFoundException extends RuntimeException {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1324
            private static final long serialVersionUID = 0;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1325
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1326
            InferenceGraph graph;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1327
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1328
            public NodeNotFoundException(InferenceGraph graph) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1329
                this.graph = graph;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1330
            }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1331
        }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1332
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1333
         * Pick the next node (leaf) to solve in the graph
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1334
         */
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1335
        Node pickNode(InferenceGraph g) throws NodeNotFoundException;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1336
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1337
         * Is this the last step?
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1338
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1339
        boolean done();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1340
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1341
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1342
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1343
     * Simple solver strategy class that locates all leaves inside a graph
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1344
     * and picks the first leaf as the next node to solve
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1345
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1346
    abstract class LeafSolver implements GraphStrategy {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1347
        public Node pickNode(InferenceGraph g) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1348
            if (g.nodes.isEmpty()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1349
                //should not happen
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1350
                throw new NodeNotFoundException(g);
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1351
            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1352
            return g.nodes.get(0);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1353
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1354
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1355
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1356
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1357
     * This solver uses an heuristic to pick the best leaf - the heuristic
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1358
     * tries to select the node that has maximal probability to contain one
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1359
     * or more inference variables in a given list
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1360
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1361
    abstract class BestLeafSolver extends LeafSolver {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1362
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1363
        /** list of ivars of which at least one must be solved */
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1364
        List<Type> varsToSolve;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1365
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1366
        BestLeafSolver(List<Type> varsToSolve) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1367
            this.varsToSolve = varsToSolve;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1368
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1369
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1370
        /**
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1371
         * 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
  1372
         * Typically this will start from a node containing a variable in
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1373
         * {@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
  1374
         * number of type-variables that should be eagerly instantiated across that path.
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1375
         */
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1376
        Pair<List<Node>, Integer> computeTreeToLeafs(Node n) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1377
            Pair<List<Node>, Integer> cachedPath = treeCache.get(n);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1378
            if (cachedPath == null) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1379
                //cache miss
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1380
                if (n.isLeaf()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1381
                    //if leaf, stop
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1382
                    cachedPath = new Pair<>(List.of(n), n.data.length());
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1383
                } else {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1384
                    //if non-leaf, proceed recursively
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1385
                    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
  1386
                    for (Node n2 : n.getAllDependencies()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1387
                        if (n2 == n) continue;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1388
                        Pair<List<Node>, Integer> subpath = computeTreeToLeafs(n2);
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1389
                        path = new Pair<>(path.fst.prependList(subpath.fst),
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1390
                                          path.snd + subpath.snd);
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1391
                    }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1392
                    cachedPath = path;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1393
                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1394
                //save results in cache
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1395
                treeCache.put(n, cachedPath);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1396
            }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1397
            return cachedPath;
18916
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1398
        }
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1399
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1400
        /** cache used to avoid redundant computation of tree costs */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1401
        final Map<Node, Pair<List<Node>, Integer>> treeCache = new HashMap<>();
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1402
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1403
        /** constant value used to mark non-existent paths */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1404
        final Pair<List<Node>, Integer> noPath = new Pair<>(null, Integer.MAX_VALUE);
15717
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
         * Pick the leaf that minimize cost
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1408
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1409
        @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1410
        public Node pickNode(final InferenceGraph g) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1411
            treeCache.clear(); //graph changes at every step - cache must be cleared
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1412
            Pair<List<Node>, Integer> bestPath = noPath;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1413
            for (Node n : g.nodes) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1414
                if (!Collections.disjoint(n.data, varsToSolve)) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1415
                    Pair<List<Node>, Integer> path = computeTreeToLeafs(n);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1416
                    //discard all paths containing at least a node in the
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1417
                    //closure computed above
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1418
                    if (path.snd < bestPath.snd) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1419
                        bestPath = path;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1420
                    }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1421
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1422
            }
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1423
            if (bestPath == noPath) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1424
                //no path leads there
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1425
                throw new NodeNotFoundException(g);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1426
            }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1427
            return bestPath.fst.head;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1428
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1429
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1430
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1431
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1432
     * 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
  1433
     * instantiates an inference variable using a subset of the inference variable
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1434
     * bounds, if certain condition are met. Decisions such as the sequence in which
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1435
     * 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
  1436
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1437
    enum InferenceStep {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1438
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1439
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1440
         * Instantiate an inference variables using one of its (ground) equality
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1441
         * constraints
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1442
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1443
        EQ(InferenceBound.EQ) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1444
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1445
            Type solve(UndetVar uv, InferenceContext inferenceContext) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1446
                return filterBounds(uv, inferenceContext).head;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1447
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1448
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1449
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1450
         * Instantiate an inference variables using its (ground) lower bounds. Such
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1451
         * bounds are merged together using lub().
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1452
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1453
        LOWER(InferenceBound.LOWER) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1454
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
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
                Infer infer = inferenceContext.infer;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1457
                List<Type> lobounds = filterBounds(uv, inferenceContext);
18395
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18008
diff changeset
  1458
                //note: lobounds should have at least one element
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18008
diff changeset
  1459
                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
  1460
                if (owntype.isPrimitive() || owntype.hasTag(ERROR)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1461
                    throw infer.inferenceException
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1462
                        .setMessage("no.unique.minimal.instance.exists",
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1463
                                    uv.qtype, lobounds);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1464
                } else {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1465
                    return owntype;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1466
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1467
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1468
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1469
        /**
18909
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1470
         * Infer uninstantiated/unbound inference variables occurring in 'throws'
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1471
         * clause as RuntimeException
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1472
         */
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1473
        THROWS(InferenceBound.UPPER) {
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1474
            @Override
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1475
            public boolean accepts(UndetVar t, InferenceContext inferenceContext) {
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1476
                if ((t.qtype.tsym.flags() & Flags.THROWS) == 0) {
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1477
                    //not a throws undet var
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1478
                    return false;
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1479
                }
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1480
                if (t.getBounds(InferenceBound.EQ, InferenceBound.LOWER, InferenceBound.UPPER)
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1481
                            .diff(t.getDeclaredBounds()).nonEmpty()) {
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1482
                    //not an unbounded undet var
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1483
                    return false;
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1484
                }
31937
ad43a6639c4a 8132215: class InferenceContext should live in a separate file
vromero
parents: 30014
diff changeset
  1485
                Infer infer = inferenceContext.infer;
18909
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1486
                for (Type db : t.getDeclaredBounds()) {
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1487
                    if (t.isInterface()) continue;
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1488
                    if (infer.types.asSuper(infer.syms.runtimeExceptionType, db.tsym) != null) {
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1489
                        //declared bound is a supertype of RuntimeException
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1490
                        return true;
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1491
                    }
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1492
                }
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1493
                //declared bound is more specific then RuntimeException - give up
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1494
                return false;
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1495
            }
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1496
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1497
            @Override
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1498
            Type solve(UndetVar uv, InferenceContext inferenceContext) {
31937
ad43a6639c4a 8132215: class InferenceContext should live in a separate file
vromero
parents: 30014
diff changeset
  1499
                return inferenceContext.infer.syms.runtimeExceptionType;
18909
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1500
            }
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1501
        },
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1502
        /**
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1503
         * Instantiate an inference variables using its (ground) upper bounds. Such
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1504
         * bounds are merged together using glb().
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1505
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1506
        UPPER(InferenceBound.UPPER) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1507
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1508
            Type solve(UndetVar uv, InferenceContext inferenceContext) {
31937
ad43a6639c4a 8132215: class InferenceContext should live in a separate file
vromero
parents: 30014
diff changeset
  1509
                Infer infer = inferenceContext.infer;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1510
                List<Type> hibounds = filterBounds(uv, inferenceContext);
24293
0d889f759fac 8033718: Inference ignores capture variable as upper bound
dlsmith
parents: 24226
diff changeset
  1511
                //note: hibounds should have at least one element
18395
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18008
diff changeset
  1512
                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
  1513
                if (owntype.isPrimitive() || owntype.hasTag(ERROR)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1514
                    throw infer.inferenceException
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1515
                        .setMessage("no.unique.maximal.instance.exists",
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1516
                                    uv.qtype, hibounds);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1517
                } else {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1518
                    return owntype;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1519
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1520
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1521
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1522
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1523
         * 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
  1524
         * if all upper bounds are ground.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1525
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1526
        UPPER_LEGACY(InferenceBound.UPPER) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1527
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1528
            public boolean accepts(UndetVar t, InferenceContext inferenceContext) {
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1529
                return !inferenceContext.free(t.getBounds(ib)) && !t.isCaptured();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1530
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1531
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1532
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1533
            Type solve(UndetVar uv, InferenceContext inferenceContext) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1534
                return UPPER.solve(uv, inferenceContext);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1535
            }
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1536
        },
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1537
        /**
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1538
         * 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
  1539
         * if all upper/lower bounds are ground.
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1540
         */
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1541
        CAPTURED(InferenceBound.UPPER) {
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1542
            @Override
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1543
            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
  1544
                return t.isCaptured() &&
8a0cbd5cb055 8020804: javac crashes when speculative attribution infers intersection type with array component
mcimadamore
parents: 18918
diff changeset
  1545
                        !inferenceContext.free(t.getBounds(InferenceBound.UPPER, InferenceBound.LOWER));
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1546
            }
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1547
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1548
            @Override
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1549
            Type solve(UndetVar uv, InferenceContext inferenceContext) {
31937
ad43a6639c4a 8132215: class InferenceContext should live in a separate file
vromero
parents: 30014
diff changeset
  1550
                Infer infer = inferenceContext.infer;
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1551
                Type upper = UPPER.filterBounds(uv, inferenceContext).nonEmpty() ?
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1552
                        UPPER.solve(uv, inferenceContext) :
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1553
                        infer.syms.objectType;
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1554
                Type lower = LOWER.filterBounds(uv, inferenceContext).nonEmpty() ?
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1555
                        LOWER.solve(uv, inferenceContext) :
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1556
                        infer.syms.botType;
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1557
                CapturedType prevCaptured = (CapturedType)uv.qtype;
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24293
diff changeset
  1558
                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
  1559
                                        upper, lower, prevCaptured.wildcard);
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1560
            }
15717
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
        final InferenceBound ib;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1564
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1565
        InferenceStep(InferenceBound ib) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1566
            this.ib = ib;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1567
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1568
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1569
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1570
         * Find an instantiated type for a given inference variable within
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1571
         * a given inference context
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1572
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1573
        abstract Type solve(UndetVar uv, InferenceContext inferenceContext);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1574
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1575
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1576
         * Can the inference variable be instantiated using this step?
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1577
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1578
        public boolean accepts(UndetVar t, InferenceContext inferenceContext) {
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1579
            return filterBounds(t, inferenceContext).nonEmpty() && !t.isCaptured();
15717
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
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1583
         * 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
  1584
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1585
        List<Type> filterBounds(UndetVar uv, InferenceContext inferenceContext) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1586
            return Type.filter(uv.getBounds(ib), new BoundFilter(inferenceContext));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1587
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1588
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1589
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1590
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1591
     * This enumeration defines the sequence of steps to be applied when the
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1592
     * solver works in legacy mode. The steps in this enumeration reflect
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1593
     * 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
  1594
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1595
    enum LegacyInferenceSteps {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1596
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1597
        EQ_LOWER(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER)),
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1598
        EQ_UPPER(EnumSet.of(InferenceStep.EQ, InferenceStep.UPPER_LEGACY));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1599
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1600
        final EnumSet<InferenceStep> steps;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1601
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1602
        LegacyInferenceSteps(EnumSet<InferenceStep> steps) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1603
            this.steps = steps;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1604
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1605
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1606
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1607
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1608
     * This enumeration defines the sequence of steps to be applied when the
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1609
     * graph solver is used. This order is defined so as to maximize compatibility
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1610
     * 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
  1611
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1612
    enum GraphInferenceSteps {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1613
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1614
        EQ(EnumSet.of(InferenceStep.EQ)),
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1615
        EQ_LOWER(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER)),
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1616
        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
  1617
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1618
        final EnumSet<InferenceStep> steps;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1619
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1620
        GraphInferenceSteps(EnumSet<InferenceStep> steps) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1621
            this.steps = steps;
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
    /**
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1626
     * There are two kinds of dependencies between inference variables. The basic
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1627
     * kind of dependency (or bound dependency) arises when a variable mention
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1628
     * 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
  1629
     * of dependency that arises when a variable 'might' lead to better constraints
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1630
     * on another variable (this is typically the case with variables holding up
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1631
     * stuck expressions).
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1632
     */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1633
    enum DependencyKind implements GraphUtils.DependencyKind {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1634
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1635
        /** bound dependency */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1636
        BOUND("dotted"),
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1637
        /** stuck dependency */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1638
        STUCK("dashed");
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1639
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1640
        final String dotSyle;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1641
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1642
        private DependencyKind(String dotSyle) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1643
            this.dotSyle = dotSyle;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1644
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1645
    }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1646
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1647
    /**
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1648
     * This is the graph inference solver - the solver organizes all inference variables in
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1649
     * a given inference context by bound dependencies - in the general case, such dependencies
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1650
     * 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
  1651
     * an acyclic graph, where all cyclic variables are bundled together. An inference
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1652
     * 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
  1653
     * relying on a given strategy (see GraphStrategy).
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1654
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1655
    class GraphSolver {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1656
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1657
        InferenceContext inferenceContext;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1658
        Map<Type, Set<Type>> stuckDeps;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1659
        Warner warn;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1660
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1661
        GraphSolver(InferenceContext inferenceContext, Map<Type, Set<Type>> stuckDeps, Warner warn) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1662
            this.inferenceContext = inferenceContext;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1663
            this.stuckDeps = stuckDeps;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1664
            this.warn = warn;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1665
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1666
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1667
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1668
         * Solve variables in a given inference context. The amount of variables
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1669
         * 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
  1670
         * depends on the selected solver strategy.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1671
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1672
        void solve(GraphStrategy sstrategy) {
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1673
            doIncorporation(inferenceContext, warn); //initial propagation of bounds
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1674
            InferenceGraph inferenceGraph = new InferenceGraph(stuckDeps);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1675
            while (!sstrategy.done()) {
26267
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
  1676
                if (dependenciesFolder != null) {
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
  1677
                    //add this graph to the pending queue
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
  1678
                    pendingGraphs = pendingGraphs.prepend(inferenceGraph.toDot());
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
  1679
                }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1680
                InferenceGraph.Node nodeToSolve = sstrategy.pickNode(inferenceGraph);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1681
                List<Type> varsToSolve = List.from(nodeToSolve.data);
18904
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  1682
                List<Type> saved_undet = inferenceContext.save();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1683
                try {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1684
                    //repeat until all variables are solved
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1685
                    outer: while (Type.containsAny(inferenceContext.restvars(), varsToSolve)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1686
                        //for each inference phase
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1687
                        for (GraphInferenceSteps step : GraphInferenceSteps.values()) {
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1688
                            if (inferenceContext.solveBasic(varsToSolve, step.steps).nonEmpty()) {
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1689
                                doIncorporation(inferenceContext, warn);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1690
                                continue outer;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1691
                            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1692
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1693
                        //no progress
18395
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18008
diff changeset
  1694
                        throw inferenceException.setMessage();
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
                catch (InferenceException ex) {
18395
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18008
diff changeset
  1698
                    //did we fail because of interdependent ivars?
18904
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  1699
                    inferenceContext.rollback(saved_undet);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1700
                    instantiateAsUninferredVars(varsToSolve, inferenceContext);
33917
45d04023e689 8067767: type inference performance regression
mcimadamore
parents: 33906
diff changeset
  1701
                    doIncorporation(inferenceContext, warn);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1702
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1703
                inferenceGraph.deleteNode(nodeToSolve);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1704
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1705
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1706
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1707
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1708
         * The dependencies between the inference variables that need to be solved
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1709
         * form a (possibly cyclic) graph. This class reduces the original dependency graph
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1710
         * 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
  1711
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1712
        class InferenceGraph {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1713
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1714
            /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1715
             * This class represents a node in the graph. Each node corresponds
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1716
             * to an inference variable and has edges (dependencies) on other
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1717
             * nodes. The node defines an entry point that can be used to receive
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1718
             * updates on the structure of the graph this node belongs to (used to
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1719
             * keep dependencies in sync).
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1720
             */
25844
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1721
            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
  1722
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1723
                /** map listing all dependencies (grouped by kind) */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1724
                EnumMap<DependencyKind, Set<Node>> deps;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1725
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1726
                Node(Type ivar) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1727
                    super(ListBuffer.of(ivar));
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1728
                    this.deps = new EnumMap<>(DependencyKind.class);
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1729
                }
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
                @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1732
                public GraphUtils.DependencyKind[] getSupportedDependencyKinds() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1733
                    return DependencyKind.values();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1734
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1735
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1736
                public Iterable<? extends Node> getAllDependencies() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1737
                    return getDependencies(DependencyKind.values());
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1738
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1739
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1740
                @Override
25844
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1741
                public Collection<? extends Node> getDependenciesByKind(GraphUtils.DependencyKind dk) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1742
                    return getDependencies((DependencyKind)dk);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1743
                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1744
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1745
                /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1746
                 * Retrieves all dependencies with given kind(s).
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
                protected Set<Node> getDependencies(DependencyKind... depKinds) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1749
                    Set<Node> buf = new LinkedHashSet<>();
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1750
                    for (DependencyKind dk : depKinds) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1751
                        Set<Node> depsByKind = deps.get(dk);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1752
                        if (depsByKind != null) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1753
                            buf.addAll(depsByKind);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1754
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1755
                    }
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1756
                    return buf;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1757
                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1758
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1759
                /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1760
                 * Adds dependency with given kind.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1761
                 */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1762
                protected void addDependency(DependencyKind dk, Node depToAdd) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1763
                    Set<Node> depsByKind = deps.get(dk);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1764
                    if (depsByKind == null) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1765
                        depsByKind = new LinkedHashSet<>();
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1766
                        deps.put(dk, depsByKind);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1767
                    }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1768
                    depsByKind.add(depToAdd);
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
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1771
                /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1772
                 * Add multiple dependencies of same given kind.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1773
                 */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1774
                protected void addDependencies(DependencyKind dk, Set<Node> depsToAdd) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1775
                    for (Node n : depsToAdd) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1776
                        addDependency(dk, n);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1777
                    }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1778
                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1779
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
                 * Remove a dependency, regardless of its kind.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1782
                 */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1783
                protected Set<DependencyKind> removeDependency(Node n) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1784
                    Set<DependencyKind> removedKinds = new HashSet<>();
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1785
                    for (DependencyKind dk : DependencyKind.values()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1786
                        Set<Node> depsByKind = deps.get(dk);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1787
                        if (depsByKind == null) continue;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1788
                        if (depsByKind.remove(n)) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1789
                            removedKinds.add(dk);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1790
                        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1791
                    }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1792
                    return removedKinds;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1793
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1794
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1795
                /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1796
                 * Compute closure of a give node, by recursively walking
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1797
                 * through all its dependencies (of given kinds)
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1798
                 */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1799
                protected Set<Node> closure(DependencyKind... depKinds) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1800
                    boolean progress = true;
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1801
                    Set<Node> closure = new HashSet<>();
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1802
                    closure.add(this);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1803
                    while (progress) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1804
                        progress = false;
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1805
                        for (Node n1 : new HashSet<>(closure)) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1806
                            progress = closure.addAll(n1.getDependencies(depKinds));
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1807
                        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1808
                    }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1809
                    return closure;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1810
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1811
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1812
                /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1813
                 * 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
  1814
                 * or it just has self-dependencies.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1815
                 */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1816
                protected boolean isLeaf() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1817
                    //no deps, or only one self dep
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1818
                    Set<Node> allDeps = getDependencies(DependencyKind.BOUND, DependencyKind.STUCK);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1819
                    if (allDeps.isEmpty()) return true;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1820
                    for (Node n : allDeps) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1821
                        if (n != this) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1822
                            return false;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1823
                        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1824
                    }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1825
                    return true;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1826
                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1827
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1828
                /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1829
                 * Merge this node with another node, acquiring its dependencies.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1830
                 * This routine is used to merge all cyclic node together and
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1831
                 * form an acyclic graph.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1832
                 */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1833
                protected void mergeWith(List<? extends Node> nodes) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1834
                    for (Node n : nodes) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1835
                        Assert.check(n.data.length() == 1, "Attempt to merge a compound node!");
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1836
                        data.appendList(n.data);
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1837
                        for (DependencyKind dk : DependencyKind.values()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1838
                            addDependencies(dk, n.getDependencies(dk));
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1839
                        }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1840
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1841
                    //update deps
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1842
                    EnumMap<DependencyKind, Set<Node>> deps2 = new EnumMap<>(DependencyKind.class);
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1843
                    for (DependencyKind dk : DependencyKind.values()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1844
                        for (Node d : getDependencies(dk)) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1845
                            Set<Node> depsByKind = deps2.get(dk);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1846
                            if (depsByKind == null) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1847
                                depsByKind = new LinkedHashSet<>();
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1848
                                deps2.put(dk, depsByKind);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1849
                            }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1850
                            if (data.contains(d.data.first())) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1851
                                depsByKind.add(this);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1852
                            } else {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1853
                                depsByKind.add(d);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1854
                            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1855
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1856
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1857
                    deps = deps2;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1858
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1859
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1860
                /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1861
                 * Notify all nodes that something has changed in the graph
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1862
                 * topology.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1863
                 */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1864
                private void graphChanged(Node from, Node to) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1865
                    for (DependencyKind dk : removeDependency(from)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1866
                        if (to != null) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1867
                            addDependency(dk, to);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1868
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1869
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1870
                }
25844
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1871
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1872
                @Override
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1873
                public Properties nodeAttributes() {
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1874
                    Properties p = new Properties();
26267
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
  1875
                    p.put("label", "\"" + toString() + "\"");
25844
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1876
                    return p;
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1877
                }
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1878
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1879
                @Override
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1880
                public Properties dependencyAttributes(Node sink, GraphUtils.DependencyKind dk) {
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1881
                    Properties p = new Properties();
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1882
                    p.put("style", ((DependencyKind)dk).dotSyle);
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1883
                    if (dk == DependencyKind.STUCK) return p;
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1884
                    else {
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1885
                        StringBuilder buf = new StringBuilder();
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1886
                        String sep = "";
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1887
                        for (Type from : data) {
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1888
                            UndetVar uv = (UndetVar)inferenceContext.asUndetVar(from);
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1889
                            for (Type bound : uv.getBounds(InferenceBound.values())) {
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1890
                                if (bound.containsAny(List.from(sink.data))) {
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1891
                                    buf.append(sep);
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1892
                                    buf.append(bound);
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1893
                                    sep = ",";
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1894
                                }
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1895
                            }
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1896
                        }
26267
4ebd9393b373 8056075: Add support for dumping inference dependency graphs
mcimadamore
parents: 25874
diff changeset
  1897
                        p.put("label", "\"" + buf.toString() + "\"");
25844
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1898
                    }
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1899
                    return p;
48eab270456c 8048890: Add option to keep track of symbol completion dependencies
mcimadamore
parents: 25279
diff changeset
  1900
                }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1901
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1902
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1903
            /** the nodes in the inference graph */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1904
            ArrayList<Node> nodes;
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
            InferenceGraph(Map<Type, Set<Type>> optDeps) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1907
                initNodes(optDeps);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1908
            }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1909
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1910
            /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1911
             * Basic lookup helper for retrieving a graph node given an inference
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1912
             * variable type.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1913
             */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1914
            public Node findNode(Type t) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1915
                for (Node n : nodes) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1916
                    if (n.data.contains(t)) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1917
                        return n;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1918
                    }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1919
                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1920
                return null;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1921
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1922
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1923
            /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1924
             * Delete a node from the graph. This update the underlying structure
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1925
             * of the graph (including dependencies) via listeners updates.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1926
             */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1927
            public void deleteNode(Node n) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1928
                Assert.check(nodes.contains(n));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1929
                nodes.remove(n);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1930
                notifyUpdate(n, null);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1931
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1932
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1933
            /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1934
             * 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
  1935
             * {@code null} the source node is assumed to be removed.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1936
             */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1937
            void notifyUpdate(Node from, Node to) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1938
                for (Node n : nodes) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1939
                    n.graphChanged(from, to);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1940
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1941
            }
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
             * Create the graph nodes. First a simple node is created for every inference
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1945
             * variables to be solved. Then Tarjan is used to found all connected components
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1946
             * 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
  1947
             * created, effectively replacing the original cyclic nodes.
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1948
             */
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1949
            void initNodes(Map<Type, Set<Type>> stuckDeps) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1950
                //add nodes
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1951
                nodes = new ArrayList<>();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1952
                for (Type t : inferenceContext.restvars()) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1953
                    nodes.add(new Node(t));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1954
                }
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1955
                //add dependencies
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1956
                for (Node n_i : nodes) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1957
                    Type i = n_i.data.first();
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1958
                    Set<Type> optDepsByNode = stuckDeps.get(i);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1959
                    for (Node n_j : nodes) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1960
                        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
  1961
                        UndetVar uv_i = (UndetVar)inferenceContext.asUndetVar(i);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1962
                        if (Type.containsAny(uv_i.getBounds(InferenceBound.values()), List.of(j))) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1963
                            //update i's bound dependencies
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1964
                            n_i.addDependency(DependencyKind.BOUND, n_j);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1965
                        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1966
                        if (optDepsByNode != null && optDepsByNode.contains(j)) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1967
                            //update i's stuck dependencies
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1968
                            n_i.addDependency(DependencyKind.STUCK, n_j);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1969
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1970
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1971
                }
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 19127
diff changeset
  1972
                //merge cyclic nodes
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21488
diff changeset
  1973
                ArrayList<Node> acyclicNodes = new ArrayList<>();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1974
                for (List<? extends Node> conSubGraph : GraphUtils.tarjan(nodes)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1975
                    if (conSubGraph.length() > 1) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1976
                        Node root = conSubGraph.head;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1977
                        root.mergeWith(conSubGraph.tail);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1978
                        for (Node n : conSubGraph) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1979
                            notifyUpdate(n, root);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1980
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1981
                    }
16321
75e64bcd3ab0 8008723: Graph Inference: bad graph calculation leads to assertion error
mcimadamore
parents: 15717
diff changeset
  1982
                    acyclicNodes.add(conSubGraph.head);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1983
                }
16321
75e64bcd3ab0 8008723: Graph Inference: bad graph calculation leads to assertion error
mcimadamore
parents: 15717
diff changeset
  1984
                nodes = acyclicNodes;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1985
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1986
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1987
            /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1988
             * Debugging: dot representation of this graph
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1989
             */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1990
            String toDot() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1991
                StringBuilder buf = new StringBuilder();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1992
                for (Type t : inferenceContext.undetvars) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1993
                    UndetVar uv = (UndetVar)t;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1994
                    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
  1995
                            uv.qtype, uv.getBounds(InferenceBound.UPPER), uv.getBounds(InferenceBound.LOWER),
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1996
                            uv.getBounds(InferenceBound.EQ)));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1997
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1998
                return GraphUtils.toDot(nodes, "inferenceGraph" + hashCode(), buf.toString());
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1999
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2000
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2001
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2002
    // </editor-fold>
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2003
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2004
    // <editor-fold defaultstate="collapsed" desc="Inference context">
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  2005
    /**
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2006
     * Functional interface for defining inference callbacks. Certain actions
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2007
     * (i.e. subtyping checks) might need to be redone after all inference variables
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2008
     * have been fixed.
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2009
     */
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2010
    interface FreeTypeListener {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2011
        void typesInferred(InferenceContext inferenceContext);
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2012
    }
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2013
31937
ad43a6639c4a 8132215: class InferenceContext should live in a separate file
vromero
parents: 30014
diff changeset
  2014
    final InferenceContext emptyContext;
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  2015
    // </editor-fold>
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  2016
}