langtools/src/share/classes/com/sun/tools/javac/comp/Infer.java
author mcimadamore
Wed, 17 Jul 2013 14:21:12 +0100
changeset 18918 4e8769f15a95
parent 18916 d93bea397df9
child 19127 8a0cbd5cb055
permissions -rw-r--r--
8019942: Graph inference: avoid redundant computation during bound incorporation Summary: Bound incorporation should not perform same operation multiple times Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
     2
 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5489
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5489
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5489
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5489
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5489
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.comp;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
    28
import com.sun.tools.javac.tree.JCTree;
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
    29
import com.sun.tools.javac.tree.JCTree.JCTypeCast;
8036
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
    30
import com.sun.tools.javac.tree.TreeInfo;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import com.sun.tools.javac.util.*;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    32
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.tools.javac.util.List;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    34
import com.sun.tools.javac.code.*;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    35
import com.sun.tools.javac.code.Type.*;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    36
import com.sun.tools.javac.code.Type.UndetVar.InferenceBound;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    37
import com.sun.tools.javac.code.Symbol.*;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    38
import com.sun.tools.javac.comp.DeferredAttr.AttrMode;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    39
import com.sun.tools.javac.comp.Infer.GraphSolver.InferenceGraph;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    40
import com.sun.tools.javac.comp.Infer.GraphSolver.InferenceGraph.Node;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    41
import com.sun.tools.javac.comp.Resolve.InapplicableMethodException;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    42
import com.sun.tools.javac.comp.Resolve.VerboseResolutionMode;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
18916
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
    44
import java.util.Comparator;
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
    45
import java.util.HashMap;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
    46
import java.util.Map;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    47
import java.util.Set;
18916
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
    48
import java.util.TreeSet;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    49
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    50
import java.util.ArrayList;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    51
import java.util.Collections;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    52
import java.util.EnumSet;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    53
import java.util.HashSet;
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
    54
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14258
diff changeset
    55
import static com.sun.tools.javac.code.TypeTag.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
/** Helper class for type parameter inference, used by the attribution phase.
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5846
diff changeset
    59
 *  <p><b>This is NOT part of any supported API.
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5846
diff changeset
    60
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
public class Infer {
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    protected static final Context.Key<Infer> inferKey =
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        new Context.Key<Infer>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    68
    Resolve rs;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    69
    Check chk;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    Symtab syms;
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    Types types;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    72
    JCDiagnostic.Factory diags;
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10628
diff changeset
    73
    Log log;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    75
    /** should the graph solver be used? */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    76
    boolean allowGraphInference;
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15037
diff changeset
    77
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
    public static Infer instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        Infer instance = context.get(inferKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
            instance = new Infer(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    protected Infer(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        context.put(inferKey, this);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    87
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    88
        rs = Resolve.instance(context);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    89
        chk = Check.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        syms = Symtab.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        types = Types.instance(context);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    92
        diags = JCDiagnostic.Factory.instance(context);
10816
ce8a7e9d8882 7098660: Write better overload resolution/inference tests
mcimadamore
parents: 10628
diff changeset
    93
        log = Log.instance(context);
13440
1ea85052f98d 7187104: Inference cleanup: remove redundant exception classes in Infer.java
mcimadamore
parents: 13438
diff changeset
    94
        inferenceException = new InferenceException(diags);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    95
        Options options = Options.instance(context);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    96
        allowGraphInference = Source.instance(context).allowGraphInference()
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
    97
                && options.isUnset("useLegacyInference");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   100
    /** 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
   101
    public static final Type anyPoly = new JCNoType();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   102
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   103
   /**
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   104
    * 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
   105
    * 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
   106
    */
11707
532f41763bc9 7129801: Merge the two method applicability routines
mcimadamore
parents: 11550
diff changeset
   107
    public static class InferenceException extends InapplicableMethodException {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
        private static final long serialVersionUID = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   110
        List<JCDiagnostic> messages = List.nil();
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   111
3140
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 3139
diff changeset
   112
        InferenceException(JCDiagnostic.Factory diags) {
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   113
            super(diags);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        }
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   115
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   116
        @Override
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   117
        InapplicableMethodException setMessage(JCDiagnostic diag) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   118
            messages = messages.append(diag);
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   119
            return this;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   120
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   121
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   122
        @Override
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   123
        public JCDiagnostic getDiagnostic() {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   124
            return messages.head;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   125
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   126
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   127
        void clear() {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   128
            messages = List.nil();
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   129
        }
3140
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 3139
diff changeset
   130
    }
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 3139
diff changeset
   131
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   132
    protected final InferenceException inferenceException;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   134
    // <editor-fold defaultstate="collapsed" desc="Inference routines">
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   135
    /**
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   136
     * Main inference entry point - instantiate a generic method type
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   137
     * using given argument types and (possibly) an expected target-type.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
     */
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12468
diff changeset
   139
    public Type instantiateMethod(Env<AttrContext> env,
5489
e7af65bf7577 6730476: invalid "unchecked generic array" warning
mcimadamore
parents: 3778
diff changeset
   140
                                  List<Type> tvars,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
                                  MethodType mt,
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12468
diff changeset
   142
                                  Attr.ResultInfo resultInfo,
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12468
diff changeset
   143
                                  Symbol msym,
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12468
diff changeset
   144
                                  List<Type> argtypes,
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12468
diff changeset
   145
                                  boolean allowBoxing,
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12468
diff changeset
   146
                                  boolean useVarargs,
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   147
                                  Resolve.MethodResolutionContext resolveContext,
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12468
diff changeset
   148
                                  Warner warn) throws InferenceException {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
        //-System.err.println("instantiateMethod(" + tvars + ", " + mt + ", " + argtypes + ")"); //DEBUG
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
   150
        final InferenceContext inferenceContext = new InferenceContext(tvars);
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   151
        inferenceException.clear();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   152
        try {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   153
            DeferredAttr.DeferredAttrContext deferredAttrContext =
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   154
                        resolveContext.deferredAttrContext(msym, inferenceContext, resultInfo, warn);
6710
b14e6fe7b290 5088624: cannot find symbol message should be more intelligent
mcimadamore
parents: 6592
diff changeset
   155
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16567
diff changeset
   156
            resolveContext.methodCheck.argumentsAcceptable(env, deferredAttrContext,
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   157
                    argtypes, mt.getParameterTypes(), warn);
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14547
diff changeset
   158
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   159
            if (allowGraphInference &&
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   160
                    resultInfo != null &&
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15037
diff changeset
   161
                    !warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   162
                //inject return constraints earlier
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   163
                checkWithinBounds(inferenceContext, warn); //propagation
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   164
                Type newRestype = generateReturnConstraints(resultInfo, mt, inferenceContext);
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   165
                mt = (MethodType)types.createMethodTypeWithReturn(mt, newRestype);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   166
                //propagate outwards if needed
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   167
                if (resultInfo.checkContext.inferenceContext().free(resultInfo.pt)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   168
                    //propagate inference context outwards and exit
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   169
                    inferenceContext.dupTo(resultInfo.checkContext.inferenceContext());
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   170
                    deferredAttrContext.complete();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   171
                    return mt;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   172
                }
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15037
diff changeset
   173
            }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15037
diff changeset
   174
15037
8ba16ac61b1a 8005243: Restructure method check code to allow pluggable checkers
mcimadamore
parents: 14547
diff changeset
   175
            deferredAttrContext.complete();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   177
            // minimize as yet undetermined type variables
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   178
            if (allowGraphInference) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   179
                inferenceContext.solve(warn);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   180
            } else {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   181
                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
   182
            }
3778
38a70273507b 6650759: Inference of formal type parameter (unused in formal parameters) is not performed
mcimadamore
parents: 3140
diff changeset
   183
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
   184
            mt = (MethodType)inferenceContext.asInstType(mt);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   186
            if (!allowGraphInference &&
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   187
                    inferenceContext.restvars().nonEmpty() &&
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   188
                    resultInfo != null &&
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   189
                    !warn.hasNonSilentLint(Lint.LintCategory.UNCHECKED)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   190
                generateReturnConstraints(resultInfo, mt, inferenceContext);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   191
                inferenceContext.solveLegacy(false, warn, LegacyInferenceSteps.EQ_UPPER.steps); //maximizeInst
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   192
                mt = (MethodType)inferenceContext.asInstType(mt);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   193
            }
3140
15a274b13051 6638712: Inference with wildcard types causes selection of inapplicable method
mcimadamore
parents: 3139
diff changeset
   194
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   195
            if (resultInfo != null && rs.verboseResolutionMode.contains(VerboseResolutionMode.DEFERRED_INST)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   196
                log.note(env.tree.pos, "deferred.method.inst", msym, mt, resultInfo.pt);
12915
28cf1e0dafdc 7166552: Inference: cleanup usage of Type.ForAll
mcimadamore
parents: 12468
diff changeset
   197
            }
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   198
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   199
            // return instantiated version of method type
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   200
            return mt;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   201
        } finally {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   202
            if (resultInfo != null || !allowGraphInference) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   203
                inferenceContext.notifyChange();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   204
            } else {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   205
                inferenceContext.notifyChange(inferenceContext.boundedVars());
14048
308d1cf8fe46 7177306: Regression: unchecked method call does not erase return type
mcimadamore
parents: 14047
diff changeset
   206
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
        }
8616
5a47f5535883 7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents: 8229
diff changeset
   208
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   210
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   211
     * Generate constraints from the generic method's return type. If the method
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   212
     * 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
   213
     * type to derive more constraints on the generic method inference variables.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   214
     */
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   215
    Type generateReturnConstraints(Attr.ResultInfo resultInfo,
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   216
            MethodType mt, InferenceContext inferenceContext) {
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   217
        Type from = mt.getReturnType();
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   218
        if (mt.getReturnType().containsAny(inferenceContext.inferencevars) &&
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   219
                resultInfo.checkContext.inferenceContext() != emptyContext) {
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   220
            from = types.capture(from);
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   221
            //add synthetic captured ivars
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   222
            for (Type t : from.getTypeArguments()) {
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   223
                if (t.hasTag(TYPEVAR) && ((TypeVar)t).isCaptured()) {
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   224
                    inferenceContext.addVar((TypeVar)t);
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   225
                }
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   226
            }
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   227
        }
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   228
        Type qtype1 = inferenceContext.asFree(from);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   229
        Type to = returnConstraintTarget(qtype1, resultInfo.pt);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   230
        Assert.check(allowGraphInference || !resultInfo.checkContext.inferenceContext().free(to),
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   231
                "legacy inference engine cannot handle constraints on both sides of a subtyping assertion");
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   232
        //we need to skip capture?
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   233
        Warner retWarn = new Warner();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   234
        if (!resultInfo.checkContext.compatible(qtype1, resultInfo.checkContext.inferenceContext().asFree(to), retWarn) ||
18008
6d75e3886bac 8015505: Spurious inference error when return type of generic method requires unchecked conversion to target
mcimadamore
parents: 16967
diff changeset
   235
                //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
   236
                (!allowGraphInference && retWarn.hasLint(Lint.LintCategory.UNCHECKED))) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   237
            throw inferenceException
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   238
                    .setMessage("infer.no.conforming.instance.exists",
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   239
                    inferenceContext.restvars(), mt.getReturnType(), to);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   240
        }
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   241
        return from;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   242
    }
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   243
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   244
    Type returnConstraintTarget(Type from, Type to) {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   245
        if (from.hasTag(VOID)) {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   246
            return syms.voidType;
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   247
        } else if (to.hasTag(NONE)) {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   248
            return from.isPrimitive() ? from : syms.objectType;
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   249
        } else if (from.hasTag(UNDETVAR) && to.isPrimitive()) {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   250
            if (!allowGraphInference) {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   251
                //if legacy, just return boxed type
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   252
                return types.boxedClass(to).type;
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   253
            }
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   254
            //if graph inference we need to skip conflicting boxed bounds...
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   255
            UndetVar uv = (UndetVar)from;
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   256
            for (Type t : uv.getBounds(InferenceBound.EQ, InferenceBound.LOWER)) {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   257
                Type boundAsPrimitive = types.unboxedType(t);
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   258
                if (boundAsPrimitive == null) continue;
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   259
                if (types.isConvertible(boundAsPrimitive, to)) {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   260
                    //effectively skip return-type constraint generation (compatibility)
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   261
                    return syms.objectType;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   262
                }
12468
1100643c0209 7154127: Inference cleanup: remove bound check analysis from visitors in Types.java
mcimadamore
parents: 12335
diff changeset
   263
            }
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   264
            return types.boxedClass(to).type;
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   265
        } else {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   266
            return to;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
        }
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18909
diff changeset
   268
    }
12468
1100643c0209 7154127: Inference cleanup: remove bound check analysis from visitors in Types.java
mcimadamore
parents: 12335
diff changeset
   269
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   270
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   271
      * Infer cyclic inference variables as described in 15.12.2.8.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   272
      */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   273
    private void instantiateAsUninferredVars(List<Type> vars, InferenceContext inferenceContext) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   274
        ListBuffer<Type> todo = ListBuffer.lb();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   275
        //step 1 - create fresh tvars
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   276
        for (Type t : vars) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   277
            UndetVar uv = (UndetVar)inferenceContext.asFree(t);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   278
            List<Type> upperBounds = uv.getBounds(InferenceBound.UPPER);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   279
            if (Type.containsAny(upperBounds, vars)) {
16967
79d444669f3f 7015104: use new subtype of TypeSymbol for type parameters
jfranck
parents: 16805
diff changeset
   280
                TypeSymbol fresh_tvar = new TypeVariableSymbol(Flags.SYNTHETIC, uv.qtype.tsym.name, null, uv.qtype.tsym.owner);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   281
                fresh_tvar.type = new TypeVar(fresh_tvar, types.makeCompoundType(uv.getBounds(InferenceBound.UPPER)), null);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   282
                todo.append(uv);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   283
                uv.inst = fresh_tvar.type;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   284
            } else if (upperBounds.nonEmpty()) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   285
                uv.inst = types.glb(upperBounds);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   286
            } else {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   287
                uv.inst = syms.objectType;
12468
1100643c0209 7154127: Inference cleanup: remove bound check analysis from visitors in Types.java
mcimadamore
parents: 12335
diff changeset
   288
            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   289
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   290
        //step 2 - replace fresh tvars in their bounds
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   291
        List<Type> formals = vars;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   292
        for (Type t : todo) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   293
            UndetVar uv = (UndetVar)t;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   294
            TypeVar ct = (TypeVar)uv.inst;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   295
            ct.bound = types.glb(inferenceContext.asInstTypes(types.getBounds(ct)));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   296
            if (ct.bound.isErroneous()) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   297
                //report inference error if glb fails
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   298
                reportBoundError(uv, BoundErrorKind.BAD_UPPER);
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15037
diff changeset
   299
            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   300
            formals = formals.tail;
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   301
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   302
    }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   303
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   304
    /**
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   305
     * 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
   306
     * 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
   307
     * 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
   308
     */
12335
4725d88691dd 7133185: Update 292 overload resolution logic to match JLS
mcimadamore
parents: 12334
diff changeset
   309
    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
   310
                                            MethodSymbol spMethod,  // sig. poly. method or null if none
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   311
                                            Resolve.MethodResolutionContext resolveContext,
8036
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   312
                                            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
   313
        final Type restype;
8036
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   314
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   315
        //The return type for a polymorphic signature call is computed from
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   316
        //the enclosing tree E, as follows: if E is a cast, then use the
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   317
        //target type of the cast expression as a return type; if E is an
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   318
        //expression statement, the return type is 'void' - otherwise the
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   319
        //return type is simply 'Object'. A correctness check ensures that
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   320
        //env.next refers to the lexically enclosing environment in which
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   321
        //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
   322
8036
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   323
        switch (env.next.tree.getTag()) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10816
diff changeset
   324
            case TYPECAST:
8036
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   325
                JCTypeCast castTree = (JCTypeCast)env.next.tree;
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   326
                restype = (TreeInfo.skipParens(castTree.expr) == env.tree) ?
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   327
                    castTree.clazz.type :
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   328
                    syms.objectType;
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   329
                break;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10816
diff changeset
   330
            case EXEC:
8036
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   331
                JCTree.JCExpressionStatement execTree =
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   332
                        (JCTree.JCExpressionStatement)env.next.tree;
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   333
                restype = (TreeInfo.skipParens(execTree.expr) == env.tree) ?
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   334
                    syms.voidType :
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   335
                    syms.objectType;
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   336
                break;
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   337
            default:
17b976649c61 6992698: JSR 292: remove support for transient syntax in polymorphic signature calls
mcimadamore
parents: 7681
diff changeset
   338
                restype = syms.objectType;
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   339
        }
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   340
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   341
        List<Type> paramtypes = Type.map(argtypes, new ImplicitArgType(spMethod, resolveContext.step));
6592
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   342
        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
   343
            spMethod.getThrownTypes() :
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   344
            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
   345
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   346
        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
   347
                                          restype,
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   348
                                          exType,
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   349
                                          syms.methodClass);
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   350
        return mtype;
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   351
    }
dc56420a69bc 6979327: method handle invocation should use casts instead of type parameters to specify return type
mcimadamore
parents: 6348
diff changeset
   352
    //where
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   353
        class ImplicitArgType extends DeferredAttr.DeferredTypeMap {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   354
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   355
            public ImplicitArgType(Symbol msym, Resolve.MethodResolutionPhase phase) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   356
                rs.deferredAttr.super(AttrMode.SPECULATIVE, msym, phase);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   357
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   358
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   359
            public Type apply(Type t) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   360
                t = types.erasure(super.apply(t));
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14258
diff changeset
   361
                if (t.hasTag(BOT))
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   362
                    // 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
   363
                    // infer as java.lang.Void for now
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   364
                    t = types.boxedClass(syms.voidType).type;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   365
                return t;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   366
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
   367
        }
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   368
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
   369
    /**
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   370
      * 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
   371
      * SAM type contains one or more wildcards. An inference process is applied
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   372
      * so that wildcard bounds, as well as explicit lambda/method ref parameters
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   373
      * (where applicable) are used to constraint the solution.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   374
      */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   375
    public Type instantiateFunctionalInterface(DiagnosticPosition pos, Type funcInterface,
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   376
            List<Type> paramTypes, Check.CheckContext checkContext) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   377
        if (types.capture(funcInterface) == funcInterface) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   378
            //if capture doesn't change the type then return the target unchanged
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   379
            //(this means the target contains no wildcards!)
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   380
            return funcInterface;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   381
        } else {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   382
            Type formalInterface = funcInterface.tsym.type;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   383
            InferenceContext funcInterfaceContext =
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   384
                    new InferenceContext(funcInterface.tsym.type.getTypeArguments());
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   385
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   386
            Assert.check(paramTypes != null);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   387
            //get constraints from explicit params (this is done by
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   388
            //checking that explicit param types are equal to the ones
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   389
            //in the functional interface descriptors)
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   390
            List<Type> descParameterTypes = types.findDescriptorType(formalInterface).getParameterTypes();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   391
            if (descParameterTypes.size() != paramTypes.size()) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   392
                checkContext.report(pos, diags.fragment("incompatible.arg.types.in.lambda"));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   393
                return types.createErrorType(funcInterface);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   394
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   395
            for (Type p : descParameterTypes) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   396
                if (!types.isSameType(funcInterfaceContext.asFree(p), paramTypes.head)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   397
                    checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   398
                    return types.createErrorType(funcInterface);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   399
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   400
                paramTypes = paramTypes.tail;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   401
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   402
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   403
            try {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   404
                funcInterfaceContext.solve(funcInterfaceContext.boundedVars(), types.noWarnings);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   405
            } catch (InferenceException ex) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   406
                checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   407
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   408
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   409
            List<Type> actualTypeargs = funcInterface.getTypeArguments();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   410
            for (Type t : funcInterfaceContext.undetvars) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   411
                UndetVar uv = (UndetVar)t;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   412
                if (uv.inst == null) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   413
                    uv.inst = actualTypeargs.head;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   414
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   415
                actualTypeargs = actualTypeargs.tail;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   416
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   417
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   418
            Type owntype = funcInterfaceContext.asInstType(formalInterface);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   419
            if (!chk.checkValidGenericType(owntype)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   420
                //if the inferred functional interface type is not well-formed,
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   421
                //or if it's not a subtype of the original target, issue an error
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   422
                checkContext.report(pos, diags.fragment("no.suitable.functional.intf.inst", funcInterface));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   423
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   424
            return owntype;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   425
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   426
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   427
    // </editor-fold>
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   428
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   429
    // <editor-fold defaultstate="collapsed" desc="Bound checking">
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   430
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   431
     * Check bounds and perform incorporation
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   432
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   433
    void checkWithinBounds(InferenceContext inferenceContext,
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   434
                             Warner warn) throws InferenceException {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   435
        MultiUndetVarListener mlistener = new MultiUndetVarListener(inferenceContext.undetvars);
18904
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
   436
        List<Type> saved_undet = inferenceContext.save();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   437
        try {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   438
            while (true) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   439
                mlistener.reset();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   440
                if (!allowGraphInference) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   441
                    //in legacy mode we lack of transitivity, so bound check
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   442
                    //cannot be run in parallel with other incoprporation rounds
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   443
                    for (Type t : inferenceContext.undetvars) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   444
                        UndetVar uv = (UndetVar)t;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   445
                        IncorporationStep.CHECK_BOUNDS.apply(uv, inferenceContext, warn);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   446
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   447
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   448
                for (Type t : inferenceContext.undetvars) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   449
                    UndetVar uv = (UndetVar)t;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   450
                    //bound incorporation
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   451
                    EnumSet<IncorporationStep> incorporationSteps = allowGraphInference ?
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   452
                            incorporationStepsGraph : incorporationStepsLegacy;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   453
                    for (IncorporationStep is : incorporationSteps) {
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   454
                        if (is.accepts(uv, inferenceContext)) {
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   455
                            is.apply(uv, inferenceContext, warn);
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   456
                        }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   457
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   458
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   459
                if (!mlistener.changed || !allowGraphInference) break;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   460
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   461
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   462
        finally {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   463
            mlistener.detach();
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   464
            if (incorporationCache.size() == MAX_INCORPORATION_STEPS) {
18904
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
   465
                inferenceContext.rollback(saved_undet);
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
   466
            }
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   467
            incorporationCache.clear();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   468
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   469
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   470
    //where
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   471
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   472
         * This listener keeps track of changes on a group of inference variable
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   473
         * bounds. Note: the listener must be detached (calling corresponding
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   474
         * method) to make sure that the underlying inference variable is
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   475
         * left in a clean state.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   476
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   477
        class MultiUndetVarListener implements UndetVar.UndetVarListener {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   478
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   479
            boolean changed;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   480
            List<Type> undetvars;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   481
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   482
            public MultiUndetVarListener(List<Type> undetvars) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   483
                this.undetvars = undetvars;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   484
                for (Type t : undetvars) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   485
                    UndetVar uv = (UndetVar)t;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   486
                    uv.listener = this;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   487
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   488
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   489
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   490
            public void varChanged(UndetVar uv, Set<InferenceBound> ibs) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   491
                //avoid non-termination
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   492
                if (incorporationCache.size() < MAX_INCORPORATION_STEPS) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   493
                    changed = true;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   494
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   495
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   496
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   497
            void reset() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   498
                changed = false;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   499
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   500
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   501
            void detach() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   502
                for (Type t : undetvars) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   503
                    UndetVar uv = (UndetVar)t;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   504
                    uv.listener = null;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   505
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   506
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   507
        };
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   508
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   509
        /** max number of incorporation rounds */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   510
        static final int MAX_INCORPORATION_STEPS = 100;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   511
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   512
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   513
     * This enumeration defines an entry point for doing inference variable
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   514
     * bound incorporation - it can be used to inject custom incorporation
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   515
     * logic into the basic bound checking routine
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   516
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   517
    enum IncorporationStep {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   518
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   519
         * Performs basic bound checking - i.e. is the instantiated type for a given
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   520
         * inference variable compatible with its bounds?
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   521
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   522
        CHECK_BOUNDS() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   523
            public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   524
                Infer infer = inferenceContext.infer();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   525
                uv.substBounds(inferenceContext.inferenceVars(), inferenceContext.instTypes(), infer.types);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   526
                infer.checkCompatibleUpperBounds(uv, inferenceContext);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   527
                if (uv.inst != null) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   528
                    Type inst = uv.inst;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   529
                    for (Type u : uv.getBounds(InferenceBound.UPPER)) {
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   530
                        if (!isSubtype(inst, inferenceContext.asFree(u), warn, infer)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   531
                            infer.reportBoundError(uv, BoundErrorKind.UPPER);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   532
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   533
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   534
                    for (Type l : uv.getBounds(InferenceBound.LOWER)) {
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   535
                        if (!isSubtype(inferenceContext.asFree(l), inst, warn, infer)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   536
                            infer.reportBoundError(uv, BoundErrorKind.LOWER);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   537
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   538
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   539
                    for (Type e : uv.getBounds(InferenceBound.EQ)) {
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   540
                        if (!isSameType(inst, inferenceContext.asFree(e), infer)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   541
                            infer.reportBoundError(uv, BoundErrorKind.EQ);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   542
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   543
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   544
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   545
            }
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   546
            @Override
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   547
            boolean accepts(UndetVar uv, InferenceContext inferenceContext) {
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   548
                //applies to all undetvars
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   549
                return true;
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   550
            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   551
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   552
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   553
         * Check consistency of equality constraints. This is a slightly more aggressive
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   554
         * inference routine that is designed as to maximize compatibility with JDK 7.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   555
         * Note: this is not used in graph mode.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   556
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   557
        EQ_CHECK_LEGACY() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   558
            public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   559
                Infer infer = inferenceContext.infer();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   560
                Type eq = null;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   561
                for (Type e : uv.getBounds(InferenceBound.EQ)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   562
                    Assert.check(!inferenceContext.free(e));
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   563
                    if (eq != null && !isSameType(e, eq, infer)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   564
                        infer.reportBoundError(uv, BoundErrorKind.EQ);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   565
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   566
                    eq = e;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   567
                    for (Type l : uv.getBounds(InferenceBound.LOWER)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   568
                        Assert.check(!inferenceContext.free(l));
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   569
                        if (!isSubtype(l, e, warn, infer)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   570
                            infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_LOWER);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   571
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   572
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   573
                    for (Type u : uv.getBounds(InferenceBound.UPPER)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   574
                        if (inferenceContext.free(u)) continue;
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   575
                        if (!isSubtype(e, u, warn, infer)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   576
                            infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_UPPER);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   577
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   578
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   579
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   580
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   581
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   582
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   583
         * Check consistency of equality constraints.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   584
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   585
        EQ_CHECK() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   586
            public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   587
                Infer infer = inferenceContext.infer();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   588
                for (Type e : uv.getBounds(InferenceBound.EQ)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   589
                    if (e.containsAny(inferenceContext.inferenceVars())) continue;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   590
                    for (Type u : uv.getBounds(InferenceBound.UPPER)) {
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   591
                        if (!isSubtype(e, inferenceContext.asFree(u), warn, infer)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   592
                            infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_UPPER);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   593
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   594
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   595
                    for (Type l : uv.getBounds(InferenceBound.LOWER)) {
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   596
                        if (!isSubtype(inferenceContext.asFree(l), e, warn, infer)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   597
                            infer.reportBoundError(uv, BoundErrorKind.BAD_EQ_LOWER);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   598
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   599
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   600
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   601
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   602
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   603
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   604
         * Given a bound set containing {@code alpha <: T} and {@code alpha :> S}
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   605
         * perform {@code S <: T} (which could lead to new bounds).
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   606
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   607
        CROSS_UPPER_LOWER() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   608
            public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   609
                Infer infer = inferenceContext.infer();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   610
                for (Type b1 : uv.getBounds(InferenceBound.UPPER)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   611
                    for (Type b2 : uv.getBounds(InferenceBound.LOWER)) {
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   612
                        isSubtype(inferenceContext.asFree(b2), inferenceContext.asFree(b1), warn , infer);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   613
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   614
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   615
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   616
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   617
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   618
         * Given a bound set containing {@code alpha <: T} and {@code alpha == S}
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   619
         * perform {@code S <: T} (which could lead to new bounds).
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   620
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   621
        CROSS_UPPER_EQ() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   622
            public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   623
                Infer infer = inferenceContext.infer();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   624
                for (Type b1 : uv.getBounds(InferenceBound.UPPER)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   625
                    for (Type b2 : uv.getBounds(InferenceBound.EQ)) {
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   626
                        isSubtype(inferenceContext.asFree(b2), inferenceContext.asFree(b1), warn, infer);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   627
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   628
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   629
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   630
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   631
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   632
         * Given a bound set containing {@code alpha :> S} and {@code alpha == T}
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   633
         * perform {@code S <: T} (which could lead to new bounds).
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   634
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   635
        CROSS_EQ_LOWER() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   636
            public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   637
                Infer infer = inferenceContext.infer();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   638
                for (Type b1 : uv.getBounds(InferenceBound.EQ)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   639
                    for (Type b2 : uv.getBounds(InferenceBound.LOWER)) {
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   640
                        isSubtype(inferenceContext.asFree(b2), inferenceContext.asFree(b1), warn, infer);
16567
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16341
diff changeset
   641
                    }
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16341
diff changeset
   642
                }
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16341
diff changeset
   643
            }
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16341
diff changeset
   644
        },
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16341
diff changeset
   645
        /**
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16341
diff changeset
   646
         * Given a bound set containing {@code alpha == S} and {@code alpha == T}
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16341
diff changeset
   647
         * perform {@code S == T} (which could lead to new bounds).
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16341
diff changeset
   648
         */
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16341
diff changeset
   649
        CROSS_EQ_EQ() {
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16341
diff changeset
   650
            public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16341
diff changeset
   651
                Infer infer = inferenceContext.infer();
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16341
diff changeset
   652
                for (Type b1 : uv.getBounds(InferenceBound.EQ)) {
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16341
diff changeset
   653
                    for (Type b2 : uv.getBounds(InferenceBound.EQ)) {
946441667c02 8010303: Graph inference: missing incorporation step causes spurious inference error
mcimadamore
parents: 16341
diff changeset
   654
                        if (b1 != b2) {
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   655
                            isSameType(inferenceContext.asFree(b2), inferenceContext.asFree(b1), infer);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   656
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   657
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   658
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   659
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   660
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   661
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   662
         * Given a bound set containing {@code alpha <: beta} propagate lower bounds
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   663
         * from alpha to beta; also propagate upper bounds from beta to alpha.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   664
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   665
        PROP_UPPER() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   666
            public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   667
                Infer infer = inferenceContext.infer();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   668
                for (Type b : uv.getBounds(InferenceBound.UPPER)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   669
                    if (inferenceContext.inferenceVars().contains(b)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   670
                        UndetVar uv2 = (UndetVar)inferenceContext.asFree(b);
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   671
                        if (uv2.isCaptured()) continue;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   672
                        //alpha <: beta
16341
cc4b4e3c0f12 8009545: Graph inference: dependencies between inference variables should be set during incorporation
mcimadamore
parents: 16321
diff changeset
   673
                        //0. set beta :> alpha
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   674
                        addBound(InferenceBound.LOWER, uv2, inferenceContext.asInstType(uv.qtype), infer);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   675
                        //1. copy alpha's lower to beta's
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   676
                        for (Type l : uv.getBounds(InferenceBound.LOWER)) {
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   677
                            addBound(InferenceBound.LOWER, uv2, inferenceContext.asInstType(l), infer);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   678
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   679
                        //2. copy beta's upper to alpha's
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   680
                        for (Type u : uv2.getBounds(InferenceBound.UPPER)) {
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   681
                            addBound(InferenceBound.UPPER, uv, inferenceContext.asInstType(u), infer);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   682
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   683
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   684
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   685
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   686
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   687
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   688
         * Given a bound set containing {@code alpha :> beta} propagate lower bounds
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   689
         * from beta to alpha; also propagate upper bounds from alpha to beta.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   690
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   691
        PROP_LOWER() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   692
            public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   693
                Infer infer = inferenceContext.infer();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   694
                for (Type b : uv.getBounds(InferenceBound.LOWER)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   695
                    if (inferenceContext.inferenceVars().contains(b)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   696
                        UndetVar uv2 = (UndetVar)inferenceContext.asFree(b);
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   697
                        if (uv2.isCaptured()) continue;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   698
                        //alpha :> beta
16341
cc4b4e3c0f12 8009545: Graph inference: dependencies between inference variables should be set during incorporation
mcimadamore
parents: 16321
diff changeset
   699
                        //0. set beta <: alpha
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   700
                        addBound(InferenceBound.UPPER, uv2, inferenceContext.asInstType(uv.qtype), infer);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   701
                        //1. copy alpha's upper to beta's
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   702
                        for (Type u : uv.getBounds(InferenceBound.UPPER)) {
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   703
                            addBound(InferenceBound.UPPER, uv2, inferenceContext.asInstType(u), infer);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   704
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   705
                        //2. copy beta's lower to alpha's
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   706
                        for (Type l : uv2.getBounds(InferenceBound.LOWER)) {
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   707
                            addBound(InferenceBound.LOWER, uv, inferenceContext.asInstType(l), infer);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   708
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   709
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   710
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   711
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   712
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   713
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   714
         * Given a bound set containing {@code alpha == beta} propagate lower/upper
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   715
         * bounds from alpha to beta and back.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   716
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   717
        PROP_EQ() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   718
            public void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   719
                Infer infer = inferenceContext.infer();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   720
                for (Type b : uv.getBounds(InferenceBound.EQ)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   721
                    if (inferenceContext.inferenceVars().contains(b)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   722
                        UndetVar uv2 = (UndetVar)inferenceContext.asFree(b);
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   723
                        if (uv2.isCaptured()) continue;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   724
                        //alpha == beta
16341
cc4b4e3c0f12 8009545: Graph inference: dependencies between inference variables should be set during incorporation
mcimadamore
parents: 16321
diff changeset
   725
                        //0. set beta == alpha
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   726
                        addBound(InferenceBound.EQ, uv2, inferenceContext.asInstType(uv.qtype), infer);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   727
                        //1. copy all alpha's bounds to beta's
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   728
                        for (InferenceBound ib : InferenceBound.values()) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   729
                            for (Type b2 : uv.getBounds(ib)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   730
                                if (b2 != uv2) {
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   731
                                    addBound(ib, uv2, inferenceContext.asInstType(b2), infer);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   732
                                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   733
                            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   734
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   735
                        //2. copy all beta's bounds to alpha's
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   736
                        for (InferenceBound ib : InferenceBound.values()) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   737
                            for (Type b2 : uv2.getBounds(ib)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   738
                                if (b2 != uv) {
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   739
                                    addBound(ib, uv, inferenceContext.asInstType(b2), infer);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   740
                                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   741
                            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   742
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   743
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   744
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   745
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   746
        };
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   747
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   748
        abstract void apply(UndetVar uv, InferenceContext inferenceContext, Warner warn);
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   749
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   750
        boolean accepts(UndetVar uv, InferenceContext inferenceContext) {
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   751
            return !uv.isCaptured();
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
   752
        }
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   753
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   754
        boolean isSubtype(Type s, Type t, Warner warn, Infer infer) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   755
            return doIncorporationOp(IncorporationBinaryOpKind.IS_SUBTYPE, s, t, warn, infer);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   756
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   757
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   758
        boolean isSameType(Type s, Type t, Infer infer) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   759
            return doIncorporationOp(IncorporationBinaryOpKind.IS_SAME_TYPE, s, t, null, infer);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   760
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   761
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   762
        void addBound(InferenceBound ib, UndetVar uv, Type b, Infer infer) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   763
            doIncorporationOp(opFor(ib), uv, b, null, infer);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   764
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   765
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   766
        IncorporationBinaryOpKind opFor(InferenceBound boundKind) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   767
            switch (boundKind) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   768
                case EQ:
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   769
                    return IncorporationBinaryOpKind.ADD_EQ_BOUND;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   770
                case LOWER:
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   771
                    return IncorporationBinaryOpKind.ADD_LOWER_BOUND;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   772
                case UPPER:
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   773
                    return IncorporationBinaryOpKind.ADD_UPPER_BOUND;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   774
                default:
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   775
                    Assert.error("Can't get here!");
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   776
                    return null;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   777
            }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   778
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   779
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   780
        boolean doIncorporationOp(IncorporationBinaryOpKind opKind, Type op1, Type op2, Warner warn, Infer infer) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   781
            IncorporationBinaryOp newOp = infer.new IncorporationBinaryOp(opKind, op1, op2);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   782
            Boolean res = infer.incorporationCache.get(newOp);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   783
            if (res == null) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   784
                infer.incorporationCache.put(newOp, res = newOp.apply(warn));
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   785
            }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   786
            return res;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   787
        }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   788
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   789
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   790
    /** incorporation steps to be executed when running in legacy mode */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   791
    EnumSet<IncorporationStep> incorporationStepsLegacy = EnumSet.of(IncorporationStep.EQ_CHECK_LEGACY);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   792
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   793
    /** incorporation steps to be executed when running in graph mode */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   794
    EnumSet<IncorporationStep> incorporationStepsGraph =
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   795
            EnumSet.complementOf(EnumSet.of(IncorporationStep.EQ_CHECK_LEGACY));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   796
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   797
    /**
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   798
     * 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
   799
     * (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
   800
     * upper/lower/eq bound).
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   801
     */
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   802
    enum IncorporationBinaryOpKind {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   803
        IS_SUBTYPE() {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   804
            @Override
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   805
            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
   806
                return types.isSubtypeUnchecked(op1, op2, warn);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   807
            }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   808
        },
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   809
        IS_SAME_TYPE() {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   810
            @Override
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   811
            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
   812
                return types.isSameType(op1, op2);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   813
            }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   814
        },
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   815
        ADD_UPPER_BOUND() {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   816
            @Override
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   817
            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
   818
                UndetVar uv = (UndetVar)op1;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   819
                uv.addBound(InferenceBound.UPPER, op2, types);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   820
                return true;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   821
            }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   822
        },
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   823
        ADD_LOWER_BOUND() {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   824
            @Override
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   825
            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
   826
                UndetVar uv = (UndetVar)op1;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   827
                uv.addBound(InferenceBound.LOWER, op2, types);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   828
                return true;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   829
            }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   830
        },
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   831
        ADD_EQ_BOUND() {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   832
            @Override
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   833
            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
   834
                UndetVar uv = (UndetVar)op1;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   835
                uv.addBound(InferenceBound.EQ, op2, types);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   836
                return true;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   837
            }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   838
        };
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   839
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   840
        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
   841
    }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   842
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   843
    /**
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   844
     * This class encapsulates a basic incorporation operation; incorporation
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   845
     * 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
   846
     * 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
   847
     * 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
   848
     * same bounds over and over).
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   849
     */
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   850
    class IncorporationBinaryOp {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   851
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   852
        IncorporationBinaryOpKind opKind;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   853
        Type op1;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   854
        Type op2;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   855
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   856
        IncorporationBinaryOp(IncorporationBinaryOpKind opKind, Type op1, Type op2) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   857
            this.opKind = opKind;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   858
            this.op1 = op1;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   859
            this.op2 = op2;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   860
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   861
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   862
        @Override
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   863
        public boolean equals(Object o) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   864
            if (!(o instanceof IncorporationBinaryOp)) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   865
                return false;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   866
            } else {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   867
                IncorporationBinaryOp that = (IncorporationBinaryOp)o;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   868
                return opKind == that.opKind &&
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   869
                        types.isSameType(op1, that.op1, true) &&
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   870
                        types.isSameType(op2, that.op2, true);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   871
            }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   872
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   873
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   874
        @Override
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   875
        public int hashCode() {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   876
            int result = opKind.hashCode();
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   877
            result *= 127;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   878
            result += types.hashCode(op1);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   879
            result *= 127;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   880
            result += types.hashCode(op2);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   881
            return result;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   882
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   883
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   884
        boolean apply(Warner warn) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   885
            return opKind.apply(op1, op2, warn, types);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   886
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   887
    }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   888
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   889
    /** an incorporation cache keeps track of all executed incorporation-related operations */
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   890
    Map<IncorporationBinaryOp, Boolean> incorporationCache =
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   891
            new HashMap<IncorporationBinaryOp, Boolean>();
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   892
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
   893
    /**
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   894
     * Make sure that the upper bounds we got so far lead to a solvable inference
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   895
     * variable by making sure that a glb exists.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   896
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   897
    void checkCompatibleUpperBounds(UndetVar uv, InferenceContext inferenceContext) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   898
        List<Type> hibounds =
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   899
                Type.filter(uv.getBounds(InferenceBound.UPPER), new BoundFilter(inferenceContext));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   900
        Type hb = null;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   901
        if (hibounds.isEmpty())
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   902
            hb = syms.objectType;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   903
        else if (hibounds.tail.isEmpty())
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   904
            hb = hibounds.head;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   905
        else
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   906
            hb = types.glb(hibounds);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   907
        if (hb == null || hb.isErroneous())
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   908
            reportBoundError(uv, BoundErrorKind.BAD_UPPER);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   909
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   910
    //where
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   911
        protected static class BoundFilter implements Filter<Type> {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   912
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   913
            InferenceContext inferenceContext;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   914
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   915
            public BoundFilter(InferenceContext inferenceContext) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   916
                this.inferenceContext = inferenceContext;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   917
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   918
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   919
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   920
            public boolean accepts(Type t) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   921
                return !t.isErroneous() && !inferenceContext.free(t) &&
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   922
                        !t.hasTag(BOT);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   923
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   924
        };
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   925
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   926
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   927
     * This enumeration defines all possible bound-checking related errors.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   928
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   929
    enum BoundErrorKind {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   930
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   931
         * The (uninstantiated) inference variable has incompatible upper bounds.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   932
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   933
        BAD_UPPER() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   934
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   935
            InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   936
                return ex.setMessage("incompatible.upper.bounds", uv.qtype,
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   937
                        uv.getBounds(InferenceBound.UPPER));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   938
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   939
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   940
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   941
         * An equality constraint is not compatible with an upper bound.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   942
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   943
        BAD_EQ_UPPER() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   944
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   945
            InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   946
                return ex.setMessage("incompatible.eq.upper.bounds", uv.qtype,
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   947
                        uv.getBounds(InferenceBound.EQ), uv.getBounds(InferenceBound.UPPER));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   948
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   949
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   950
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   951
         * An equality constraint is not compatible with a lower bound.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   952
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   953
        BAD_EQ_LOWER() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   954
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   955
            InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   956
                return ex.setMessage("incompatible.eq.lower.bounds", uv.qtype,
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   957
                        uv.getBounds(InferenceBound.EQ), uv.getBounds(InferenceBound.LOWER));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   958
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   959
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   960
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   961
         * Instantiated inference variable is not compatible with an upper bound.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   962
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   963
        UPPER() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   964
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   965
            InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   966
                return ex.setMessage("inferred.do.not.conform.to.upper.bounds", uv.inst,
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   967
                        uv.getBounds(InferenceBound.UPPER));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   968
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   969
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   970
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   971
         * Instantiated inference variable is not compatible with a lower bound.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   972
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   973
        LOWER() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   974
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   975
            InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   976
                return ex.setMessage("inferred.do.not.conform.to.lower.bounds", uv.inst,
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   977
                        uv.getBounds(InferenceBound.LOWER));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   978
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   979
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   980
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   981
         * Instantiated inference variable is not compatible with an equality constraint.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   982
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   983
        EQ() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   984
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   985
            InapplicableMethodException setMessage(InferenceException ex, UndetVar uv) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   986
                return ex.setMessage("inferred.do.not.conform.to.eq.bounds", uv.inst,
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   987
                        uv.getBounds(InferenceBound.EQ));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   988
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   989
        };
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   990
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   991
        abstract InapplicableMethodException setMessage(InferenceException ex, UndetVar uv);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   992
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   993
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   994
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   995
     * Report a bound-checking error of given kind
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   996
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   997
    void reportBoundError(UndetVar uv, BoundErrorKind bk) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   998
        throw bk.setMessage(inferenceException, uv);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   999
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1000
    // </editor-fold>
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1001
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1002
    // <editor-fold defaultstate="collapsed" desc="Inference engine">
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1003
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1004
     * 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
  1005
     * 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
  1006
     * and (ii) tell th engine when we are done fixing inference variables
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1007
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1008
    interface GraphStrategy {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1009
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1010
         * Pick the next node (leaf) to solve in the graph
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1011
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1012
        Node pickNode(InferenceGraph g);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1013
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1014
         * Is this the last step?
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1015
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1016
        boolean done();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1017
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1018
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1019
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1020
     * Simple solver strategy class that locates all leaves inside a graph
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1021
     * and picks the first leaf as the next node to solve
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1022
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1023
    abstract class LeafSolver implements GraphStrategy {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1024
        public Node pickNode(InferenceGraph g) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1025
                        Assert.check(!g.nodes.isEmpty(), "No nodes to solve!");
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1026
            return g.nodes.get(0);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1027
        }
18918
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1028
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1029
        boolean isSubtype(Type s, Type t, Warner warn, Infer infer) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1030
            return doIncorporationOp(IncorporationBinaryOpKind.IS_SUBTYPE, s, t, warn, infer);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1031
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1032
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1033
        boolean isSameType(Type s, Type t, Infer infer) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1034
            return doIncorporationOp(IncorporationBinaryOpKind.IS_SAME_TYPE, s, t, null, infer);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1035
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1036
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1037
        void addBound(InferenceBound ib, UndetVar uv, Type b, Infer infer) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1038
            doIncorporationOp(opFor(ib), uv, b, null, infer);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1039
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1040
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1041
        IncorporationBinaryOpKind opFor(InferenceBound boundKind) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1042
            switch (boundKind) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1043
                case EQ:
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1044
                    return IncorporationBinaryOpKind.ADD_EQ_BOUND;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1045
                case LOWER:
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1046
                    return IncorporationBinaryOpKind.ADD_LOWER_BOUND;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1047
                case UPPER:
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1048
                    return IncorporationBinaryOpKind.ADD_UPPER_BOUND;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1049
                default:
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1050
                    Assert.error("Can't get here!");
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1051
                    return null;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1052
            }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1053
        }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1054
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1055
        boolean doIncorporationOp(IncorporationBinaryOpKind opKind, Type op1, Type op2, Warner warn, Infer infer) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1056
            IncorporationBinaryOp newOp = infer.new IncorporationBinaryOp(opKind, op1, op2);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1057
            Boolean res = infer.incorporationCache.get(newOp);
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1058
            if (res == null) {
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1059
                infer.incorporationCache.put(newOp, res = newOp.apply(warn));
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1060
            }
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1061
            return res;
4e8769f15a95 8019942: Graph inference: avoid redundant computation during bound incorporation
mcimadamore
parents: 18916
diff changeset
  1062
        }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1063
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1064
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1065
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1066
     * This solver uses an heuristic to pick the best leaf - the heuristic
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1067
     * tries to select the node that has maximal probability to contain one
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1068
     * or more inference variables in a given list
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1069
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1070
    abstract class BestLeafSolver extends LeafSolver {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1071
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1072
        List<Type> varsToSolve;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1073
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1074
        BestLeafSolver(List<Type> varsToSolve) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1075
            this.varsToSolve = varsToSolve;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1076
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1077
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1078
        /**
18916
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1079
         * Computes the minimum path that goes from a given node to any of the nodes
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1080
         * containing a variable in {@code varsToSolve}. For any given path, the cost
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1081
         * is computed as the total number of type-variables that should be eagerly
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1082
         * instantiated across that path.
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1083
         */
18916
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1084
        int computeMinPath(InferenceGraph g, Node n) {
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1085
            return computeMinPath(g, n, List.<Node>nil(), 0);
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1086
        }
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1087
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1088
        int computeMinPath(InferenceGraph g, Node n, List<Node> path, int cost) {
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1089
            if (path.contains(n)) return Integer.MAX_VALUE;
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1090
            List<Node> path2 = path.prepend(n);
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1091
            int cost2 = cost + n.data.size();
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1092
            if (!Collections.disjoint(n.data, varsToSolve)) {
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1093
                return cost2;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1094
            } else {
18916
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1095
               int bestPath = Integer.MAX_VALUE;
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1096
               for (Node n2 : g.nodes) {
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1097
                   if (n2.deps.contains(n)) {
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1098
                       int res = computeMinPath(g, n2, path2, cost2);
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1099
                       if (res < bestPath) {
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1100
                           bestPath = res;
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1101
                       }
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1102
                   }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1103
                }
18916
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1104
               return bestPath;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1105
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1106
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1107
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1108
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1109
         * Pick the leaf that minimize cost
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1110
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1111
        @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1112
        public Node pickNode(final InferenceGraph g) {
18916
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1113
            final Map<Node, Integer> leavesMap = new HashMap<Node, Integer>();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1114
            for (Node n : g.nodes) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1115
                if (n.isLeaf(n)) {
18916
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1116
                    leavesMap.put(n, computeMinPath(g, n));
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1117
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1118
            }
18916
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1119
            Assert.check(!leavesMap.isEmpty(), "No nodes to solve!");
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1120
            TreeSet<Node> orderedLeaves = new TreeSet<Node>(new Comparator<Node>() {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1121
                public int compare(Node n1, Node n2) {
18916
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1122
                    return leavesMap.get(n1) - leavesMap.get(n2);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1123
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1124
            });
18916
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1125
            orderedLeaves.addAll(leavesMap.keySet());
d93bea397df9 8020149: Graph inference: wrong logic for picking best variable to solve
mcimadamore
parents: 18911
diff changeset
  1126
            return orderedLeaves.first();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1127
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1128
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1129
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1130
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1131
     * 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
  1132
     * instantiates an inference variable using a subset of the inference variable
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1133
     * bounds, if certain condition are met. Decisions such as the sequence in which
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1134
     * 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
  1135
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1136
    enum InferenceStep {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1137
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1138
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1139
         * Instantiate an inference variables using one of its (ground) equality
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1140
         * constraints
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1141
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1142
        EQ(InferenceBound.EQ) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1143
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1144
            Type solve(UndetVar uv, InferenceContext inferenceContext) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1145
                return filterBounds(uv, inferenceContext).head;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1146
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1147
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1148
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1149
         * Instantiate an inference variables using its (ground) lower bounds. Such
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1150
         * bounds are merged together using lub().
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1151
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1152
        LOWER(InferenceBound.LOWER) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1153
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1154
            Type solve(UndetVar uv, InferenceContext inferenceContext) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1155
                Infer infer = inferenceContext.infer();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1156
                List<Type> lobounds = filterBounds(uv, inferenceContext);
18395
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18008
diff changeset
  1157
                //note: lobounds should have at least one element
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18008
diff changeset
  1158
                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
  1159
                if (owntype.isPrimitive() || owntype.hasTag(ERROR)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1160
                    throw infer.inferenceException
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1161
                        .setMessage("no.unique.minimal.instance.exists",
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1162
                                    uv.qtype, lobounds);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1163
                } else {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1164
                    return owntype;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1165
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1166
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1167
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1168
        /**
18909
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1169
         * Infer uninstantiated/unbound inference variables occurring in 'throws'
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1170
         * clause as RuntimeException
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1171
         */
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1172
        THROWS(InferenceBound.UPPER) {
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1173
            @Override
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1174
            public boolean accepts(UndetVar t, InferenceContext inferenceContext) {
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1175
                if ((t.qtype.tsym.flags() & Flags.THROWS) == 0) {
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1176
                    //not a throws undet var
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1177
                    return false;
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1178
                }
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1179
                if (t.getBounds(InferenceBound.EQ, InferenceBound.LOWER, InferenceBound.UPPER)
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1180
                            .diff(t.getDeclaredBounds()).nonEmpty()) {
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1181
                    //not an unbounded undet var
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1182
                    return false;
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1183
                }
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1184
                Infer infer = inferenceContext.infer();
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1185
                for (Type db : t.getDeclaredBounds()) {
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1186
                    if (t.isInterface()) continue;
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1187
                    if (infer.types.asSuper(infer.syms.runtimeExceptionType, db.tsym) != null) {
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1188
                        //declared bound is a supertype of RuntimeException
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1189
                        return true;
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1190
                    }
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1191
                }
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1192
                //declared bound is more specific then RuntimeException - give up
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1193
                return false;
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1194
            }
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1195
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1196
            @Override
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1197
            Type solve(UndetVar uv, InferenceContext inferenceContext) {
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1198
                return inferenceContext.infer().syms.runtimeExceptionType;
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1199
            }
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1200
        },
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18904
diff changeset
  1201
        /**
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1202
         * Instantiate an inference variables using its (ground) upper bounds. Such
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1203
         * bounds are merged together using glb().
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1204
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1205
        UPPER(InferenceBound.UPPER) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1206
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1207
            Type solve(UndetVar uv, InferenceContext inferenceContext) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1208
                Infer infer = inferenceContext.infer();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1209
                List<Type> hibounds = filterBounds(uv, inferenceContext);
18395
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18008
diff changeset
  1210
                //note: lobounds should have at least one element
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18008
diff changeset
  1211
                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
  1212
                if (owntype.isPrimitive() || owntype.hasTag(ERROR)) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1213
                    throw infer.inferenceException
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1214
                        .setMessage("no.unique.maximal.instance.exists",
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1215
                                    uv.qtype, hibounds);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1216
                } else {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1217
                    return owntype;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1218
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1219
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1220
        },
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1221
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1222
         * 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
  1223
         * if all upper bounds are ground.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1224
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1225
        UPPER_LEGACY(InferenceBound.UPPER) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1226
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1227
            public boolean accepts(UndetVar t, InferenceContext inferenceContext) {
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1228
                return !inferenceContext.free(t.getBounds(ib)) && !t.isCaptured();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1229
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1230
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1231
            @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1232
            Type solve(UndetVar uv, InferenceContext inferenceContext) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1233
                return UPPER.solve(uv, inferenceContext);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1234
            }
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1235
        },
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1236
        /**
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1237
         * 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
  1238
         * if all upper/lower bounds are ground.
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1239
         */
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1240
        CAPTURED(InferenceBound.UPPER) {
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1241
            @Override
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1242
            public boolean accepts(UndetVar t, InferenceContext inferenceContext) {
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1243
                return !inferenceContext.free(t.getBounds(InferenceBound.UPPER, InferenceBound.LOWER));
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1244
            }
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1245
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1246
            @Override
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1247
            Type solve(UndetVar uv, InferenceContext inferenceContext) {
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1248
                Infer infer = inferenceContext.infer();
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1249
                Type upper = UPPER.filterBounds(uv, inferenceContext).nonEmpty() ?
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1250
                        UPPER.solve(uv, inferenceContext) :
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1251
                        infer.syms.objectType;
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1252
                Type lower = LOWER.filterBounds(uv, inferenceContext).nonEmpty() ?
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1253
                        LOWER.solve(uv, inferenceContext) :
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1254
                        infer.syms.botType;
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1255
                CapturedType prevCaptured = (CapturedType)uv.qtype;
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1256
                return new CapturedType(prevCaptured.tsym.name, prevCaptured.tsym.owner, upper, lower, prevCaptured.wildcard);
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1257
            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1258
        };
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1259
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1260
        final InferenceBound ib;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1261
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1262
        InferenceStep(InferenceBound ib) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1263
            this.ib = ib;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1264
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1265
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1266
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1267
         * Find an instantiated type for a given inference variable within
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1268
         * a given inference context
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1269
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1270
        abstract Type solve(UndetVar uv, InferenceContext inferenceContext);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1271
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1272
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1273
         * Can the inference variable be instantiated using this step?
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1274
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1275
        public boolean accepts(UndetVar t, InferenceContext inferenceContext) {
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1276
            return filterBounds(t, inferenceContext).nonEmpty() && !t.isCaptured();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1277
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1278
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1279
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1280
         * 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
  1281
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1282
        List<Type> filterBounds(UndetVar uv, InferenceContext inferenceContext) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1283
            return Type.filter(uv.getBounds(ib), new BoundFilter(inferenceContext));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1284
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1285
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1286
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1287
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1288
     * This enumeration defines the sequence of steps to be applied when the
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1289
     * solver works in legacy mode. The steps in this enumeration reflect
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1290
     * 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
  1291
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1292
    enum LegacyInferenceSteps {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1293
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1294
        EQ_LOWER(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER)),
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1295
        EQ_UPPER(EnumSet.of(InferenceStep.EQ, InferenceStep.UPPER_LEGACY));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1296
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1297
        final EnumSet<InferenceStep> steps;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1298
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1299
        LegacyInferenceSteps(EnumSet<InferenceStep> steps) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1300
            this.steps = steps;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1301
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1302
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1303
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1304
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1305
     * This enumeration defines the sequence of steps to be applied when the
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1306
     * graph solver is used. This order is defined so as to maximize compatibility
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1307
     * 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
  1308
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1309
    enum GraphInferenceSteps {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1310
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1311
        EQ(EnumSet.of(InferenceStep.EQ)),
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1312
        EQ_LOWER(EnumSet.of(InferenceStep.EQ, InferenceStep.LOWER)),
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1313
        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
  1314
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1315
        final EnumSet<InferenceStep> steps;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1316
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1317
        GraphInferenceSteps(EnumSet<InferenceStep> steps) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1318
            this.steps = steps;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1319
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1320
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1321
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1322
    /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1323
     * This is the graph inference solver - the solver organizes all inference variables in
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1324
     * a given inference context by bound dependencies - in the general case, such dependencies
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1325
     * 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
  1326
     * an acyclic graph, where all cyclic variables are bundled together. An inference
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1327
     * 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
  1328
     * relying on a given strategy (see GraphStrategy).
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1329
     */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1330
    class GraphSolver {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1331
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1332
        InferenceContext inferenceContext;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1333
        Warner warn;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1334
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1335
        GraphSolver(InferenceContext inferenceContext, Warner warn) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1336
            this.inferenceContext = inferenceContext;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1337
            this.warn = warn;
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
         * Solve variables in a given inference context. The amount of variables
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1342
         * 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
  1343
         * depends on the selected solver strategy.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1344
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1345
        void solve(GraphStrategy sstrategy) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1346
            checkWithinBounds(inferenceContext, warn); //initial propagation of bounds
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1347
            InferenceGraph inferenceGraph = new InferenceGraph();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1348
            while (!sstrategy.done()) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1349
                InferenceGraph.Node nodeToSolve = sstrategy.pickNode(inferenceGraph);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1350
                List<Type> varsToSolve = List.from(nodeToSolve.data);
18904
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  1351
                List<Type> saved_undet = inferenceContext.save();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1352
                try {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1353
                    //repeat until all variables are solved
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1354
                    outer: while (Type.containsAny(inferenceContext.restvars(), varsToSolve)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1355
                        //for each inference phase
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1356
                        for (GraphInferenceSteps step : GraphInferenceSteps.values()) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1357
                            if (inferenceContext.solveBasic(varsToSolve, step.steps)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1358
                                checkWithinBounds(inferenceContext, warn);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1359
                                continue outer;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1360
                            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1361
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1362
                        //no progress
18395
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18008
diff changeset
  1363
                        throw inferenceException.setMessage();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1364
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1365
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1366
                catch (InferenceException ex) {
18395
d56a5fbf0b32 8016267: javac, TypeTag refactoring has provoked performance issues
vromero
parents: 18008
diff changeset
  1367
                    //did we fail because of interdependent ivars?
18904
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  1368
                    inferenceContext.rollback(saved_undet);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1369
                    instantiateAsUninferredVars(varsToSolve, inferenceContext);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1370
                    checkWithinBounds(inferenceContext, warn);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1371
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1372
                inferenceGraph.deleteNode(nodeToSolve);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1373
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1374
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1375
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1376
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1377
         * The dependencies between the inference variables that need to be solved
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1378
         * form a (possibly cyclic) graph. This class reduces the original dependency graph
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1379
         * 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
  1380
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1381
        class InferenceGraph {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1382
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1383
            /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1384
             * This class represents a node in the graph. Each node corresponds
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1385
             * to an inference variable and has edges (dependencies) on other
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1386
             * nodes. The node defines an entry point that can be used to receive
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1387
             * updates on the structure of the graph this node belongs to (used to
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1388
             * keep dependencies in sync).
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1389
             */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1390
            class Node extends GraphUtils.TarjanNode<ListBuffer<Type>> {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1391
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1392
                Set<Node> deps;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1393
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1394
                Node(Type ivar) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1395
                    super(ListBuffer.of(ivar));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1396
                    this.deps = new HashSet<Node>();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1397
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1398
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1399
                @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1400
                public Iterable<? extends Node> getDependencies() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1401
                    return deps;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1402
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1403
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1404
                @Override
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1405
                public String printDependency(GraphUtils.Node<ListBuffer<Type>> to) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1406
                    StringBuilder buf = new StringBuilder();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1407
                    String sep = "";
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1408
                    for (Type from : data) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1409
                        UndetVar uv = (UndetVar)inferenceContext.asFree(from);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1410
                        for (Type bound : uv.getBounds(InferenceBound.values())) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1411
                            if (bound.containsAny(List.from(to.data))) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1412
                                buf.append(sep);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1413
                                buf.append(bound);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1414
                                sep = ",";
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1415
                            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1416
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1417
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1418
                    return buf.toString();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1419
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1420
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1421
                boolean isLeaf(Node n) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1422
                    //no deps, or only one self dep
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1423
                    return (n.deps.isEmpty() ||
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1424
                            n.deps.size() == 1 && n.deps.contains(n));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1425
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1426
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1427
                void mergeWith(List<? extends Node> nodes) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1428
                    for (Node n : nodes) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1429
                        Assert.check(n.data.length() == 1, "Attempt to merge a compound node!");
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1430
                        data.appendList(n.data);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1431
                        deps.addAll(n.deps);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1432
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1433
                    //update deps
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1434
                    Set<Node> deps2 = new HashSet<Node>();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1435
                    for (Node d : deps) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1436
                        if (data.contains(d.data.first())) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1437
                            deps2.add(this);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1438
                        } else {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1439
                            deps2.add(d);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1440
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1441
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1442
                    deps = deps2;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1443
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1444
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1445
                void graphChanged(Node from, Node to) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1446
                    if (deps.contains(from)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1447
                        deps.remove(from);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1448
                        if (to != null) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1449
                            deps.add(to);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1450
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1451
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1452
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1453
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1454
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1455
            /** the nodes in the inference graph */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1456
            ArrayList<Node> nodes;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1457
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1458
            InferenceGraph() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1459
                initNodes();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1460
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1461
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1462
            /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1463
             * Delete a node from the graph. This update the underlying structure
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1464
             * of the graph (including dependencies) via listeners updates.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1465
             */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1466
            public void deleteNode(Node n) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1467
                Assert.check(nodes.contains(n));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1468
                nodes.remove(n);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1469
                notifyUpdate(n, null);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1470
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1471
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1472
            /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1473
             * 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
  1474
             * {@code null} the source node is assumed to be removed.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1475
             */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1476
            void notifyUpdate(Node from, Node to) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1477
                for (Node n : nodes) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1478
                    n.graphChanged(from, to);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1479
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1480
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1481
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1482
            /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1483
             * Create the graph nodes. First a simple node is created for every inference
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1484
             * variables to be solved. Then Tarjan is used to found all connected components
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1485
             * in the graph. For each component containing more than one node, a super node is
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1486
                 * created, effectively replacing the original cyclic nodes.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1487
             */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1488
            void initNodes() {
16321
75e64bcd3ab0 8008723: Graph Inference: bad graph calculation leads to assertion error
mcimadamore
parents: 15717
diff changeset
  1489
                nodes = new ArrayList<Node>();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1490
                for (Type t : inferenceContext.restvars()) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1491
                    nodes.add(new Node(t));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1492
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1493
                for (Node n_i : nodes) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1494
                    Type i = n_i.data.first();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1495
                    for (Node n_j : nodes) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1496
                        Type j = n_j.data.first();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1497
                        UndetVar uv_i = (UndetVar)inferenceContext.asFree(i);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1498
                        if (Type.containsAny(uv_i.getBounds(InferenceBound.values()), List.of(j))) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1499
                            //update i's deps
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1500
                            n_i.deps.add(n_j);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1501
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1502
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1503
                }
16321
75e64bcd3ab0 8008723: Graph Inference: bad graph calculation leads to assertion error
mcimadamore
parents: 15717
diff changeset
  1504
                ArrayList<Node> acyclicNodes = new ArrayList<Node>();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1505
                for (List<? extends Node> conSubGraph : GraphUtils.tarjan(nodes)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1506
                    if (conSubGraph.length() > 1) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1507
                        Node root = conSubGraph.head;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1508
                        root.mergeWith(conSubGraph.tail);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1509
                        for (Node n : conSubGraph) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1510
                            notifyUpdate(n, root);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1511
                        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1512
                    }
16321
75e64bcd3ab0 8008723: Graph Inference: bad graph calculation leads to assertion error
mcimadamore
parents: 15717
diff changeset
  1513
                    acyclicNodes.add(conSubGraph.head);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1514
                }
16321
75e64bcd3ab0 8008723: Graph Inference: bad graph calculation leads to assertion error
mcimadamore
parents: 15717
diff changeset
  1515
                nodes = acyclicNodes;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1516
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1517
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1518
            /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1519
             * Debugging: dot representation of this graph
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1520
             */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1521
            String toDot() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1522
                StringBuilder buf = new StringBuilder();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1523
                for (Type t : inferenceContext.undetvars) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1524
                    UndetVar uv = (UndetVar)t;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1525
                    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
  1526
                            uv.qtype, uv.getBounds(InferenceBound.UPPER), uv.getBounds(InferenceBound.LOWER),
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1527
                            uv.getBounds(InferenceBound.EQ)));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1528
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1529
                return GraphUtils.toDot(nodes, "inferenceGraph" + hashCode(), buf.toString());
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1530
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1531
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1532
    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1533
    // </editor-fold>
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1534
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1535
    // <editor-fold defaultstate="collapsed" desc="Inference context">
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1536
    /**
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1537
     * Functional interface for defining inference callbacks. Certain actions
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1538
     * (i.e. subtyping checks) might need to be redone after all inference variables
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1539
     * have been fixed.
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1540
     */
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1541
    interface FreeTypeListener {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1542
        void typesInferred(InferenceContext inferenceContext);
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1543
    }
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1544
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1545
    /**
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1546
     * An inference context keeps track of the set of variables that are free
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1547
     * in the current context. It provides utility methods for opening/closing
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1548
     * types to their corresponding free/closed forms. It also provide hooks for
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1549
     * attaching deferred post-inference action (see PendingCheck). Finally,
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1550
     * it can be used as an entry point for performing upper/lower bound inference
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1551
     * (see InferenceKind).
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1552
     */
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1553
     class InferenceContext {
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1554
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1555
        /** list of inference vars as undet vars */
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1556
        List<Type> undetvars;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1557
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1558
        /** list of inference vars in this context */
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1559
        List<Type> inferencevars;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1560
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1561
        java.util.Map<FreeTypeListener, List<Type>> freeTypeListeners =
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1562
                new java.util.HashMap<FreeTypeListener, List<Type>>();
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1563
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1564
        List<FreeTypeListener> freetypeListeners = List.nil();
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1565
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1566
        public InferenceContext(List<Type> inferencevars) {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1567
            this.undetvars = Type.map(inferencevars, fromTypeVarFun);
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1568
            this.inferencevars = inferencevars;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1569
        }
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1570
        //where
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1571
            Mapping fromTypeVarFun = new Mapping("fromTypeVarFunWithBounds") {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1572
                // mapping that turns inference variables into undet vars
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1573
                public Type apply(Type t) {
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1574
                    if (t.hasTag(TYPEVAR)) {
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1575
                        TypeVar tv = (TypeVar)t;
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1576
                        return tv.isCaptured() ?
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1577
                                new CapturedUndetVar((CapturedType)tv, types) :
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1578
                                new UndetVar(tv, types);
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1579
                    } else {
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1580
                        return t.map(this);
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1581
                    }
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1582
                }
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1583
            };
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1584
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1585
        /**
18911
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1586
         * add a new inference var to this inference context
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1587
         */
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1588
        void addVar(TypeVar t) {
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1589
            this.undetvars = this.undetvars.prepend(fromTypeVarFun.apply(t));
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1590
            this.inferencevars = this.inferencevars.prepend(t);
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1591
        }
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1592
dcc1e26a8c9c 8012238: Nested method capture and inference
mcimadamore
parents: 18910
diff changeset
  1593
        /**
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1594
         * returns the list of free variables (as type-variables) in this
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1595
         * inference context
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1596
         */
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1597
        List<Type> inferenceVars() {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1598
            return inferencevars;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1599
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1600
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1601
        /**
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1602
         * returns the list of uninstantiated variables (as type-variables) in this
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1603
         * inference context
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1604
         */
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1605
        List<Type> restvars() {
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1606
            return filterVars(new Filter<UndetVar>() {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1607
                public boolean accepts(UndetVar uv) {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1608
                    return uv.inst == null;
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1609
                }
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1610
            });
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1611
        }
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1612
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1613
        /**
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1614
         * returns the list of instantiated variables (as type-variables) in this
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1615
         * inference context
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1616
         */
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1617
        List<Type> instvars() {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1618
            return filterVars(new Filter<UndetVar>() {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1619
                public boolean accepts(UndetVar uv) {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1620
                    return uv.inst != null;
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1621
                }
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1622
            });
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1623
        }
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1624
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1625
        /**
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1626
         * Get list of bounded inference variables (where bound is other than
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1627
         * declared bounds).
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1628
         */
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1629
        final List<Type> boundedVars() {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1630
            return filterVars(new Filter<UndetVar>() {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1631
                public boolean accepts(UndetVar uv) {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1632
                    return uv.getBounds(InferenceBound.UPPER)
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1633
                            .diff(uv.getDeclaredBounds())
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1634
                            .appendList(uv.getBounds(InferenceBound.EQ, InferenceBound.LOWER)).nonEmpty();
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1635
                }
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1636
            });
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1637
        }
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1638
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1639
        private List<Type> filterVars(Filter<UndetVar> fu) {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1640
            ListBuffer<Type> res = ListBuffer.lb();
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1641
            for (Type t : undetvars) {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1642
                UndetVar uv = (UndetVar)t;
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1643
                if (fu.accepts(uv)) {
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1644
                    res.append(uv.qtype);
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1645
                }
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1646
            }
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1647
            return res.toList();
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1648
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1649
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1650
        /**
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1651
         * is this type free?
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1652
         */
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1653
        final boolean free(Type t) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1654
            return t.containsAny(inferencevars);
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1655
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1656
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1657
        final boolean free(List<Type> ts) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1658
            for (Type t : ts) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1659
                if (free(t)) return true;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1660
            }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1661
            return false;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1662
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1663
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1664
        /**
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1665
         * Returns a list of free variables in a given type
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1666
         */
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1667
        final List<Type> freeVarsIn(Type t) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1668
            ListBuffer<Type> buf = ListBuffer.lb();
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1669
            for (Type iv : inferenceVars()) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1670
                if (t.contains(iv)) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1671
                    buf.add(iv);
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1672
                }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1673
            }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1674
            return buf.toList();
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1675
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1676
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1677
        final List<Type> freeVarsIn(List<Type> ts) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1678
            ListBuffer<Type> buf = ListBuffer.lb();
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1679
            for (Type t : ts) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1680
                buf.appendList(freeVarsIn(t));
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1681
            }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1682
            ListBuffer<Type> buf2 = ListBuffer.lb();
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1683
            for (Type t : buf) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1684
                if (!buf2.contains(t)) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1685
                    buf2.add(t);
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1686
                }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1687
            }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1688
            return buf2.toList();
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1689
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1690
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1691
        /**
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1692
         * Replace all free variables in a given type with corresponding
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1693
         * undet vars (used ahead of subtyping/compatibility checks to allow propagation
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1694
         * of inference constraints).
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1695
         */
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1696
        final Type asFree(Type t) {
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1697
            return types.subst(t, inferencevars, undetvars);
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1698
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1699
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1700
        final List<Type> asFree(List<Type> ts) {
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1701
            ListBuffer<Type> buf = ListBuffer.lb();
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1702
            for (Type t : ts) {
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1703
                buf.append(asFree(t));
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1704
            }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1705
            return buf.toList();
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1706
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1707
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1708
        List<Type> instTypes() {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1709
            ListBuffer<Type> buf = ListBuffer.lb();
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1710
            for (Type t : undetvars) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1711
                UndetVar uv = (UndetVar)t;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1712
                buf.append(uv.inst != null ? uv.inst : uv.qtype);
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1713
            }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1714
            return buf.toList();
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1715
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1716
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1717
        /**
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1718
         * Replace all free variables in a given type with corresponding
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1719
         * instantiated types - if one or more free variable has not been
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1720
         * fully instantiated, it will still be available in the resulting type.
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1721
         */
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1722
        Type asInstType(Type t) {
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1723
            return types.subst(t, inferencevars, instTypes());
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1724
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1725
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1726
        List<Type> asInstTypes(List<Type> ts) {
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1727
            ListBuffer<Type> buf = ListBuffer.lb();
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1728
            for (Type t : ts) {
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1729
                buf.append(asInstType(t));
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1730
            }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1731
            return buf.toList();
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1732
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1733
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1734
        /**
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1735
         * Add custom hook for performing post-inference action
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1736
         */
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1737
        void addFreeTypeListener(List<Type> types, FreeTypeListener ftl) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1738
            freeTypeListeners.put(ftl, freeVarsIn(types));
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1739
        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1740
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1741
        /**
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1742
         * Mark the inference context as complete and trigger evaluation
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1743
         * of all deferred checks.
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1744
         */
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1745
        void notifyChange() {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1746
            notifyChange(inferencevars.diff(restvars()));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1747
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1748
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1749
        void notifyChange(List<Type> inferredVars) {
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1750
            InferenceException thrownEx = null;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1751
            for (Map.Entry<FreeTypeListener, List<Type>> entry :
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1752
                    new HashMap<FreeTypeListener, List<Type>>(freeTypeListeners).entrySet()) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1753
                if (!Type.containsAny(entry.getValue(), inferencevars.diff(inferredVars))) {
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1754
                    try {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1755
                        entry.getKey().typesInferred(this);
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1756
                        freeTypeListeners.remove(entry.getKey());
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1757
                    } catch (InferenceException ex) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1758
                        if (thrownEx == null) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1759
                            thrownEx = ex;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1760
                        }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1761
                    }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1762
                }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1763
            }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1764
            //inference exception multiplexing - present any inference exception
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1765
            //thrown when processing listeners as a single one
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1766
            if (thrownEx != null) {
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1767
                throw thrownEx;
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1768
            }
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1769
        }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
  1770
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1771
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1772
         * Save the state of this inference context
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1773
         */
18904
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  1774
        List<Type> save() {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1775
            ListBuffer<Type> buf = ListBuffer.lb();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1776
            for (Type t : undetvars) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1777
                UndetVar uv = (UndetVar)t;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1778
                UndetVar uv2 = new UndetVar((TypeVar)uv.qtype, types);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1779
                for (InferenceBound ib : InferenceBound.values()) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1780
                    for (Type b : uv.getBounds(ib)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1781
                        uv2.addBound(ib, b, types);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1782
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1783
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1784
                uv2.inst = uv.inst;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1785
                buf.add(uv2);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1786
            }
18904
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  1787
            return buf.toList();
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1788
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1789
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1790
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1791
         * Restore the state of this inference context to the previous known checkpoint
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1792
         */
18904
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  1793
        void rollback(List<Type> saved_undet) {
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  1794
             Assert.check(saved_undet != null && saved_undet.length() == undetvars.length());
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  1795
            //restore bounds (note: we need to preserve the old instances)
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  1796
            for (Type t : undetvars) {
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  1797
                UndetVar uv = (UndetVar)t;
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  1798
                UndetVar uv_saved = (UndetVar)saved_undet.head;
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  1799
                for (InferenceBound ib : InferenceBound.values()) {
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  1800
                    uv.setBounds(ib, uv_saved.getBounds(ib));
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  1801
                }
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  1802
                uv.inst = uv_saved.inst;
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  1803
                saved_undet = saved_undet.tail;
3c4c6457ec5b 8019824: very long error messages on inference error
mcimadamore
parents: 18646
diff changeset
  1804
            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1805
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1806
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1807
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1808
         * Copy variable in this inference context to the given context
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1809
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1810
        void dupTo(final InferenceContext that) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1811
            that.inferencevars = that.inferencevars.appendList(inferencevars);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1812
            that.undetvars = that.undetvars.appendList(undetvars);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1813
            //set up listeners to notify original inference contexts as
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1814
            //propagated vars are inferred in new context
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1815
            for (Type t : inferencevars) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1816
                that.freeTypeListeners.put(new FreeTypeListener() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1817
                    public void typesInferred(InferenceContext inferenceContext) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1818
                        InferenceContext.this.notifyChange();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1819
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1820
                }, List.of(t));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1821
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1822
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1823
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1824
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1825
         * Solve with given graph strategy.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1826
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1827
        private void solve(GraphStrategy ss, Warner warn) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1828
            GraphSolver s = new GraphSolver(this, warn);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1829
            s.solve(ss);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1830
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1831
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1832
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1833
         * Solve all variables in this context.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1834
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1835
        public void solve(Warner warn) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1836
            solve(new LeafSolver() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1837
                public boolean done() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1838
                    return restvars().isEmpty();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1839
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1840
            }, warn);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1841
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1842
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1843
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1844
         * Solve all variables in the given list.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1845
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1846
        public void solve(final List<Type> vars, Warner warn) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1847
            solve(new BestLeafSolver(vars) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1848
                public boolean done() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1849
                    return !free(asInstTypes(vars));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1850
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1851
            }, warn);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1852
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1853
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1854
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1855
         * Solve at least one variable in given list.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1856
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1857
        public void solveAny(List<Type> varsToSolve, Warner warn) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1858
            checkWithinBounds(this, warn); //propagate bounds
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1859
            List<Type> boundedVars = boundedVars().intersect(restvars()).intersect(varsToSolve);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1860
            if (boundedVars.isEmpty()) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1861
                throw inferenceException.setMessage("cyclic.inference",
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1862
                                freeVarsIn(varsToSolve));
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1863
            }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1864
            solve(new BestLeafSolver(boundedVars) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1865
                public boolean done() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1866
                    return instvars().intersect(varsToSolve).nonEmpty();
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1867
                }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1868
            }, warn);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1869
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1870
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1871
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1872
         * Apply a set of inference steps
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1873
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1874
        private boolean solveBasic(EnumSet<InferenceStep> steps) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1875
            return solveBasic(inferencevars, steps);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1876
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1877
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1878
        private boolean solveBasic(List<Type> varsToSolve, EnumSet<InferenceStep> steps) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1879
            boolean changed = false;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1880
            for (Type t : varsToSolve.intersect(restvars())) {
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1881
                UndetVar uv = (UndetVar)asFree(t);
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1882
                for (InferenceStep step : steps) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1883
                    if (step.accepts(uv, this)) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1884
                        uv.inst = step.solve(uv, this);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1885
                        changed = true;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1886
                        break;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
  1887
                    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
  1888
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
  1889
            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1890
            return changed;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1891
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1892
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1893
        /**
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1894
         * Instantiate inference variables in legacy mode (JLS 15.12.2.7, 15.12.2.8).
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1895
         * During overload resolution, instantiation is done by doing a partial
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1896
         * inference process using eq/lower bound instantiation. During check,
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1897
         * we also instantiate any remaining vars by repeatedly using eq/upper
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1898
         * instantiation, until all variables are solved.
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1899
         */
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1900
        public void solveLegacy(boolean partial, Warner warn, EnumSet<InferenceStep> steps) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1901
            while (true) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1902
                boolean stuck = !solveBasic(steps);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1903
                if (restvars().isEmpty() || partial) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1904
                    //all variables have been instantiated - exit
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1905
                    break;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1906
                } else if (stuck) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1907
                    //some variables could not be instantiated because of cycles in
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1908
                    //upper bounds - provide a (possibly recursive) default instantiation
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1909
                    instantiateAsUninferredVars(restvars(), this);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1910
                    break;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1911
                } else {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1912
                    //some variables have been instantiated - replace newly instantiated
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1913
                    //variables in remaining upper bounds and continue
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1914
                    for (Type t : undetvars) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1915
                        UndetVar uv = (UndetVar)t;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1916
                        uv.substBounds(inferenceVars(), instTypes(), types);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1917
                    }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1918
                }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
  1919
            }
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1920
            checkWithinBounds(this, warn);
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1921
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1922
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1923
        private Infer infer() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1924
            //back-door to infer
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
  1925
            return Infer.this;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14048
diff changeset
  1926
        }
8616
5a47f5535883 7015430: Incorrect thrown type determined for unchecked invocations
mcimadamore
parents: 8229
diff changeset
  1927
    }
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1928
15705
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1929
    final InferenceContext emptyContext = new InferenceContext(List.<Type>nil());
c4124695db0c 8007463: Cleanup inference related classes
mcimadamore
parents: 15383
diff changeset
  1930
    // </editor-fold>
14047
7c7a5611cf76 7175433: Inference cleanup: add helper class to handle inference variables
mcimadamore
parents: 13440
diff changeset
  1931
}