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