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