src/jdk.compiler/share/classes/com/sun/tools/javac/comp/DeferredAttr.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58018 a3c63a9dfb2c
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
     1
/*
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 52625
diff changeset
     2
 * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
     4
 *
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    10
 *
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    15
 * accompanied this code).
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    16
 *
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    20
 *
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    23
 * questions.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    24
 */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    25
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    26
package com.sun.tools.javac.comp;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    27
24066
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
    28
import com.sun.source.tree.LambdaExpressionTree.BodyKind;
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
    29
import com.sun.source.tree.NewClassTree;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    30
import com.sun.tools.javac.code.*;
50898
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
    31
import com.sun.tools.javac.code.Type.ErrorType;
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
    32
import com.sun.tools.javac.code.Type.MethodType;
44186
fe848a208b20 8176265: Method overload resolution on a covariant base type doesn't work in 9
mcimadamore
parents: 42827
diff changeset
    33
import com.sun.tools.javac.code.Type.StructuralTypeMapping;
fe848a208b20 8176265: Method overload resolution on a covariant base type doesn't work in 9
mcimadamore
parents: 42827
diff changeset
    34
import com.sun.tools.javac.code.Types.TypeMapping;
32911
6ee3c12d2d18 8137269: Add better support for local caching in ArgumentAttr
mcimadamore
parents: 32709
diff changeset
    35
import com.sun.tools.javac.comp.ArgumentAttr.LocalCacheContext;
50564
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
    36
import com.sun.tools.javac.comp.Infer.GraphSolver.InferenceGraph;
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28275
diff changeset
    37
import com.sun.tools.javac.comp.Resolve.ResolveError;
28458
f20c5dd05e34 8069164: Fix langtools make build so that diagnostic framework can be used
mcimadamore
parents: 28457
diff changeset
    38
import com.sun.tools.javac.resources.CompilerProperties.Fragments;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    39
import com.sun.tools.javac.tree.*;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    40
import com.sun.tools.javac.util.*;
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    41
import com.sun.tools.javac.util.DefinedBy.Api;
38516
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
    42
import com.sun.tools.javac.util.GraphUtils.DependencyKind;
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16566
diff changeset
    43
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    44
import com.sun.tools.javac.comp.Attr.ResultInfo;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    45
import com.sun.tools.javac.comp.Resolve.MethodResolutionPhase;
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44393
diff changeset
    46
import com.sun.tools.javac.resources.CompilerProperties.Errors;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    47
import com.sun.tools.javac.tree.JCTree.*;
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28275
diff changeset
    48
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents: 27551
diff changeset
    49
import com.sun.tools.javac.util.Log.DeferredDiagnosticHandler;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    50
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    51
import java.util.ArrayList;
38516
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
    52
import java.util.Collection;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
    53
import java.util.Collections;
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
    54
import java.util.EnumSet;
38516
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
    55
import java.util.HashSet;
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14538
diff changeset
    56
import java.util.LinkedHashSet;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    57
import java.util.Map;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    58
import java.util.Set;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    59
import java.util.WeakHashMap;
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents: 27551
diff changeset
    60
import java.util.function.Function;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    61
44393
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
    62
import com.sun.source.tree.MemberReferenceTree;
50898
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
    63
import com.sun.tools.javac.code.Type;
44393
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
    64
import com.sun.tools.javac.tree.JCTree.JCMemberReference.OverloadKind;
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
    65
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14538
diff changeset
    66
import static com.sun.tools.javac.code.TypeTag.*;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    67
import static com.sun.tools.javac.tree.JCTree.Tag.*;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    68
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    69
/**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    70
 * This is an helper class that is used to perform deferred type-analysis.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    71
 * Each time a poly expression occurs in argument position, javac attributes it
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    72
 * with a temporary 'deferred type' that is checked (possibly multiple times)
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    73
 * against an expected formal type.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    74
 *
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    75
 *  <p><b>This is NOT part of any supported API.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    76
 *  If you write code that depends on this, you do so at your own risk.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    77
 *  This code and its internal interfaces are subject to change or
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    78
 *  deletion without notice.</b>
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    79
 */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    80
public class DeferredAttr extends JCTree.Visitor {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21718
diff changeset
    81
    protected static final Context.Key<DeferredAttr> deferredAttrKey = new Context.Key<>();
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    82
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    83
    final Attr attr;
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
    84
    final ArgumentAttr argumentAttr;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    85
    final Check chk;
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15039
diff changeset
    86
    final JCDiagnostic.Factory diags;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    87
    final Enter enter;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    88
    final Infer infer;
16294
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 15717
diff changeset
    89
    final Resolve rs;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    90
    final Log log;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    91
    final Symtab syms;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    92
    final TreeMaker make;
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
    93
    final TreeCopier<Void> treeCopier;
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
    94
    final TypeMapping<Void> deferredCopier;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    95
    final Types types;
24066
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
    96
    final Flow flow;
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
    97
    final Names names;
25007
eb097d3a68f5 8038975: Access control in enhanced for
pgovereau
parents: 24895
diff changeset
    98
    final TypeEnvs typeEnvs;
58018
a3c63a9dfb2c 8177068: incomplete classpath causes NPE in Flow
jlahoda
parents: 57963
diff changeset
    99
    final DeferredCompletionFailureHandler dcfh;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   100
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   101
    public static DeferredAttr instance(Context context) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   102
        DeferredAttr instance = context.get(deferredAttrKey);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   103
        if (instance == null)
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   104
            instance = new DeferredAttr(context);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   105
        return instance;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   106
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   107
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   108
    protected DeferredAttr(Context context) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   109
        context.put(deferredAttrKey, this);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   110
        attr = Attr.instance(context);
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   111
        argumentAttr = ArgumentAttr.instance(context);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   112
        chk = Check.instance(context);
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15039
diff changeset
   113
        diags = JCDiagnostic.Factory.instance(context);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   114
        enter = Enter.instance(context);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   115
        infer = Infer.instance(context);
16294
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 15717
diff changeset
   116
        rs = Resolve.instance(context);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   117
        log = Log.instance(context);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   118
        syms = Symtab.instance(context);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   119
        make = TreeMaker.instance(context);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   120
        types = Types.instance(context);
24066
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   121
        flow = Flow.instance(context);
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   122
        names = Names.instance(context);
18902
972298345a83 8016059: Cannot compile following lambda
mcimadamore
parents: 18901
diff changeset
   123
        stuckTree = make.Ident(names.empty).setType(Type.stuckType);
25007
eb097d3a68f5 8038975: Access control in enhanced for
pgovereau
parents: 24895
diff changeset
   124
        typeEnvs = TypeEnvs.instance(context);
58018
a3c63a9dfb2c 8177068: incomplete classpath causes NPE in Flow
jlahoda
parents: 57963
diff changeset
   125
        dcfh = DeferredCompletionFailureHandler.instance(context);
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18902
diff changeset
   126
        emptyDeferredAttrContext =
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18902
diff changeset
   127
            new DeferredAttrContext(AttrMode.CHECK, null, MethodResolutionPhase.BOX, infer.emptyContext, null, null) {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18902
diff changeset
   128
                @Override
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   129
                void addDeferredAttrNode(DeferredType dt, ResultInfo ri, DeferredStuckPolicy deferredStuckPolicy) {
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18902
diff changeset
   130
                    Assert.error("Empty deferred context!");
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18902
diff changeset
   131
                }
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18902
diff changeset
   132
                @Override
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18902
diff changeset
   133
                void complete() {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18902
diff changeset
   134
                    Assert.error("Empty deferred context!");
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18902
diff changeset
   135
                }
24226
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24066
diff changeset
   136
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24066
diff changeset
   137
                @Override
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24066
diff changeset
   138
                public String toString() {
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24066
diff changeset
   139
                    return "Empty deferred context!";
08b586e22328 8030741: Inference: implement eager resolution of return types, consistent with JDK-8028800
vromero
parents: 24066
diff changeset
   140
                }
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18902
diff changeset
   141
            };
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   142
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   143
        // For speculative attribution, skip the class definition in <>.
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   144
        treeCopier =
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   145
            new TreeCopier<Void>(make) {
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   146
                @Override @DefinedBy(Api.COMPILER_TREE)
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   147
                public JCTree visitNewClass(NewClassTree node, Void p) {
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   148
                    JCNewClass t = (JCNewClass) node;
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   149
                    if (TreeInfo.isDiamond(t)) {
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   150
                        JCExpression encl = copy(t.encl, p);
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   151
                        List<JCExpression> typeargs = copy(t.typeargs, p);
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   152
                        JCExpression clazz = copy(t.clazz, p);
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   153
                        List<JCExpression> args = copy(t.args, p);
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   154
                        JCClassDecl def = null;
52455
bbbc110cdffe 8210197: javac can't tell during speculative attribution if a diamond expression is creating an anonymous inner class or not
vromero
parents: 51721
diff changeset
   155
                        return make.at(t.pos).SpeculativeNewClass(encl, typeargs, clazz, args, def, t.def != null);
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   156
                    } else {
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   157
                        return super.visitNewClass(node, p);
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   158
                    }
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   159
                }
44393
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   160
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   161
                @Override @DefinedBy(Api.COMPILER_TREE)
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   162
                public JCTree visitMemberReference(MemberReferenceTree node, Void p) {
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   163
                    JCMemberReference t = (JCMemberReference) node;
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   164
                    JCExpression expr = copy(t.expr, p);
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   165
                    List<JCExpression> typeargs = copy(t.typeargs, p);
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   166
                    /** once the value for overloadKind is determined for a copy, it can be safely forwarded to
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   167
                     *  the copied tree, we want to profit from that
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   168
                     */
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   169
                    JCMemberReference result = new JCMemberReference(t.mode, t.name, expr, typeargs) {
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   170
                        @Override
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   171
                        public void setOverloadKind(OverloadKind overloadKind) {
50533
7c5fbc953121 8203679: AssertionError in DeferredAttr with parenthesized method reference
cushon
parents: 50250
diff changeset
   172
                            OverloadKind previous = t.getOverloadKind();
51721
96b76dca2be8 8210483: AssertionError in DeferredAttr at setOverloadKind caused by JDK-8203679
cushon
parents: 51563
diff changeset
   173
                            if (previous == null || previous == OverloadKind.ERROR) {
44393
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   174
                                t.setOverloadKind(overloadKind);
50533
7c5fbc953121 8203679: AssertionError in DeferredAttr with parenthesized method reference
cushon
parents: 50250
diff changeset
   175
                            } else {
52625
5e23b9a66fe6 8213908: AssertionError in DeferredAttr at setOverloadKind
cushon
parents: 52455
diff changeset
   176
                                Assert.check(previous == overloadKind || overloadKind == OverloadKind.ERROR);
44393
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   177
                            }
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   178
                        }
50533
7c5fbc953121 8203679: AssertionError in DeferredAttr with parenthesized method reference
cushon
parents: 50250
diff changeset
   179
7c5fbc953121 8203679: AssertionError in DeferredAttr with parenthesized method reference
cushon
parents: 50250
diff changeset
   180
                        @Override
7c5fbc953121 8203679: AssertionError in DeferredAttr with parenthesized method reference
cushon
parents: 50250
diff changeset
   181
                        public OverloadKind getOverloadKind() {
7c5fbc953121 8203679: AssertionError in DeferredAttr with parenthesized method reference
cushon
parents: 50250
diff changeset
   182
                            return t.getOverloadKind();
7c5fbc953121 8203679: AssertionError in DeferredAttr with parenthesized method reference
cushon
parents: 50250
diff changeset
   183
                        }
44393
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   184
                    };
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   185
                    result.pos = t.pos;
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   186
                    return result;
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   187
                }
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   188
            };
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   189
        deferredCopier = new TypeMapping<Void> () {
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   190
                @Override
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   191
                public Type visitType(Type t, Void v) {
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   192
                    if (t.hasTag(DEFERRED)) {
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   193
                        DeferredType dt = (DeferredType) t;
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   194
                        return new DeferredType(treeCopier.copy(dt.tree), dt.env);
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   195
                    }
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   196
                    return t;
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   197
                }
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   198
            };
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   199
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   200
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15039
diff changeset
   201
    /** shared tree for stuck expressions */
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15039
diff changeset
   202
    final JCTree stuckTree;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15039
diff changeset
   203
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   204
    /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   205
     * This type represents a deferred type. A deferred type starts off with
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   206
     * no information on the underlying expression type. Such info needs to be
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   207
     * discovered through type-checking the deferred type against a target-type.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   208
     * Every deferred type keeps a pointer to the AST node from which it originated.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   209
     */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   210
    public class DeferredType extends Type {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   211
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   212
        public JCExpression tree;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   213
        Env<AttrContext> env;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   214
        AttrMode mode;
48549
db044d7e9885 8195598: Reference to overloaded method is ambiguous with 3 methods but works with 2
mcimadamore
parents: 47216
diff changeset
   215
        Set<Symbol> notPertinentToApplicability = new HashSet<>();
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   216
        SpeculativeCache speculativeCache;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   217
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26531
diff changeset
   218
        DeferredType(JCExpression tree, Env<AttrContext> env) {
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
   219
            super(null, TypeMetadata.EMPTY);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   220
            this.tree = tree;
18912
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18910
diff changeset
   221
            this.env = attr.copyEnv(env);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   222
            this.speculativeCache = new SpeculativeCache();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   223
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   224
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18643
diff changeset
   225
        @Override
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 29776
diff changeset
   226
        public DeferredType cloneWithMetadata(TypeMetadata md) {
26781
a786b07c7b91 8048614: Add TypeMetadata to contain type annotations and other type information
emc
parents: 26532
diff changeset
   227
            throw new AssertionError("Cannot add metadata to a deferred type");
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24226
diff changeset
   228
        }
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24226
diff changeset
   229
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 24226
diff changeset
   230
        @Override
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18643
diff changeset
   231
        public TypeTag getTag() {
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18643
diff changeset
   232
            return DEFERRED;
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18643
diff changeset
   233
        }
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18643
diff changeset
   234
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   235
        @Override @DefinedBy(Api.LANGUAGE_MODEL)
24066
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   236
        public String toString() {
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   237
            return "DeferredType";
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   238
        }
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   239
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   240
        /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   241
         * A speculative cache is used to keep track of all overload resolution rounds
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   242
         * that triggered speculative attribution on a given deferred type. Each entry
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   243
         * stores a pointer to the speculative tree and the resolution phase in which the entry
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   244
         * has been added.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   245
         */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   246
        class SpeculativeCache {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   247
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21718
diff changeset
   248
            private Map<Symbol, List<Entry>> cache = new WeakHashMap<>();
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   249
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   250
            class Entry {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   251
                JCTree speculativeTree;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   252
                ResultInfo resultInfo;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   253
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   254
                public Entry(JCTree speculativeTree, ResultInfo resultInfo) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   255
                    this.speculativeTree = speculativeTree;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   256
                    this.resultInfo = resultInfo;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   257
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   258
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   259
                boolean matches(MethodResolutionPhase phase) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   260
                    return resultInfo.checkContext.deferredAttrContext().phase == phase;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   261
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   262
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   263
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   264
            /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   265
             * Retrieve a speculative cache entry corresponding to given symbol
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   266
             * and resolution phase
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   267
             */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   268
            Entry get(Symbol msym, MethodResolutionPhase phase) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   269
                List<Entry> entries = cache.get(msym);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   270
                if (entries == null) return null;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   271
                for (Entry e : entries) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   272
                    if (e.matches(phase)) return e;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   273
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   274
                return null;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   275
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   276
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   277
            /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   278
             * Stores a speculative cache entry corresponding to given symbol
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   279
             * and resolution phase
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   280
             */
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   281
            void put(JCTree speculativeTree, ResultInfo resultInfo) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   282
                Symbol msym = resultInfo.checkContext.deferredAttrContext().msym;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   283
                List<Entry> entries = cache.get(msym);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   284
                if (entries == null) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   285
                    entries = List.nil();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   286
                }
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   287
                cache.put(msym, entries.prepend(new Entry(speculativeTree, resultInfo)));
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   288
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   289
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   290
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   291
        /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   292
         * Get the type that has been computed during a speculative attribution round
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   293
         */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   294
        Type speculativeType(Symbol msym, MethodResolutionPhase phase) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   295
            SpeculativeCache.Entry e = speculativeCache.get(msym, phase);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   296
            return e != null ? e.speculativeTree.type : Type.noType;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   297
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   298
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   299
        JCTree speculativeTree(DeferredAttrContext deferredAttrContext) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   300
            DeferredType.SpeculativeCache.Entry e = speculativeCache.get(deferredAttrContext.msym, deferredAttrContext.phase);
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   301
            return e != null ? e.speculativeTree : stuckTree;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   302
        }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   303
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   304
        DeferredTypeCompleter completer() {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   305
            return basicCompleter;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   306
        }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   307
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   308
        /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   309
         * Check a deferred type against a potential target-type. Depending on
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   310
         * the current attribution mode, a normal vs. speculative attribution
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   311
         * round is performed on the underlying AST node. There can be only one
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   312
         * speculative round for a given target method symbol; moreover, a normal
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   313
         * attribution round must follow one or more speculative rounds.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   314
         */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   315
        Type check(ResultInfo resultInfo) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   316
            DeferredStuckPolicy deferredStuckPolicy;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   317
            if (resultInfo.pt.hasTag(NONE) || resultInfo.pt.isErroneous()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   318
                deferredStuckPolicy = dummyStuckPolicy;
28891
dc7d89f2e25c 8069545: javac shouldn't check nested stuck lambdas during overload resolution
sadayapalam
parents: 28458
diff changeset
   319
            } else if (resultInfo.checkContext.deferredAttrContext().mode == AttrMode.SPECULATIVE ||
dc7d89f2e25c 8069545: javac shouldn't check nested stuck lambdas during overload resolution
sadayapalam
parents: 28458
diff changeset
   320
                    resultInfo.checkContext.deferredAttrContext().insideOverloadPhase()) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   321
                deferredStuckPolicy = new OverloadStuckPolicy(resultInfo, this);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   322
            } else {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   323
                deferredStuckPolicy = new CheckStuckPolicy(resultInfo, this);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   324
            }
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   325
            return check(resultInfo, deferredStuckPolicy, completer());
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   326
        }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   327
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   328
        private Type check(ResultInfo resultInfo, DeferredStuckPolicy deferredStuckPolicy,
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   329
                DeferredTypeCompleter deferredTypeCompleter) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   330
            DeferredAttrContext deferredAttrContext =
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   331
                    resultInfo.checkContext.deferredAttrContext();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   332
            Assert.check(deferredAttrContext != emptyDeferredAttrContext);
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   333
            if (deferredStuckPolicy.isStuck()) {
48549
db044d7e9885 8195598: Reference to overloaded method is ambiguous with 3 methods but works with 2
mcimadamore
parents: 47216
diff changeset
   334
                notPertinentToApplicability.add(deferredAttrContext.msym);
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   335
                deferredAttrContext.addDeferredAttrNode(this, resultInfo, deferredStuckPolicy);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   336
                return Type.noType;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   337
            } else {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   338
                try {
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   339
                    return deferredTypeCompleter.complete(this, resultInfo, deferredAttrContext);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   340
                } finally {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   341
                    mode = deferredAttrContext.mode;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   342
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   343
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   344
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   345
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   346
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   347
    /**
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   348
     * A completer for deferred types. Defines an entry point for type-checking
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   349
     * a deferred type.
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   350
     */
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   351
    interface DeferredTypeCompleter {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   352
        /**
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   353
         * Entry point for type-checking a deferred type. Depending on the
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   354
         * circumstances, type-checking could amount to full attribution
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   355
         * or partial structural check (aka potential applicability).
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   356
         */
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   357
        Type complete(DeferredType dt, ResultInfo resultInfo, DeferredAttrContext deferredAttrContext);
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   358
    }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   359
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   360
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   361
    /**
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   362
     * A basic completer for deferred types. This completer type-checks a deferred type
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   363
     * using attribution; depending on the attribution mode, this could be either standard
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   364
     * or speculative attribution.
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   365
     */
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   366
    DeferredTypeCompleter basicCompleter = new DeferredTypeCompleter() {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   367
        public Type complete(DeferredType dt, ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   368
            switch (deferredAttrContext.mode) {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   369
                case SPECULATIVE:
16566
604ce45d5f23 8009820: AssertionError when compiling java code with two identical static imports
mcimadamore
parents: 16326
diff changeset
   370
                    //Note: if a symbol is imported twice we might do two identical
604ce45d5f23 8009820: AssertionError when compiling java code with two identical static imports
mcimadamore
parents: 16326
diff changeset
   371
                    //speculative rounds...
604ce45d5f23 8009820: AssertionError when compiling java code with two identical static imports
mcimadamore
parents: 16326
diff changeset
   372
                    Assert.check(dt.mode == null || dt.mode == AttrMode.SPECULATIVE);
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26531
diff changeset
   373
                    JCTree speculativeTree = attribSpeculative(dt.tree, dt.env, resultInfo);
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   374
                    dt.speculativeCache.put(speculativeTree, resultInfo);
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   375
                    return speculativeTree.type;
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   376
                case CHECK:
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   377
                    Assert.check(dt.mode != null);
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26531
diff changeset
   378
                    return attr.attribTree(dt.tree, dt.env, resultInfo);
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   379
            }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   380
            Assert.error();
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   381
            return null;
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   382
        }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   383
    };
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   384
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   385
    /**
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   386
     * Policy for detecting stuck expressions. Different criteria might cause
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   387
     * an expression to be judged as stuck, depending on whether the check
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   388
     * is performed during overload resolution or after most specific.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   389
     */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   390
    interface DeferredStuckPolicy {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   391
        /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   392
         * Has the policy detected that a given expression should be considered stuck?
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   393
         */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   394
        boolean isStuck();
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   395
        /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   396
         * Get the set of inference variables a given expression depends upon.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   397
         */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   398
        Set<Type> stuckVars();
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   399
        /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   400
         * Get the set of inference variables which might get new constraints
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   401
         * if a given expression is being type-checked.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   402
         */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   403
        Set<Type> depVars();
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   404
    }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   405
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   406
    /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   407
     * Basic stuck policy; an expression is never considered to be stuck.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   408
     */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   409
    DeferredStuckPolicy dummyStuckPolicy = new DeferredStuckPolicy() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   410
        @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   411
        public boolean isStuck() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   412
            return false;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   413
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   414
        @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   415
        public Set<Type> stuckVars() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   416
            return Collections.emptySet();
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   417
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   418
        @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   419
        public Set<Type> depVars() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   420
            return Collections.emptySet();
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   421
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   422
    };
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   423
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   424
    /**
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   425
     * The 'mode' in which the deferred type is to be type-checked
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   426
     */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   427
    public enum AttrMode {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   428
        /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   429
         * A speculative type-checking round is used during overload resolution
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   430
         * mainly to generate constraints on inference variables. Side-effects
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   431
         * arising from type-checking the expression associated with the deferred
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   432
         * type are reversed after the speculative round finishes. This means the
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   433
         * expression tree will be left in a blank state.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   434
         */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   435
        SPECULATIVE,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   436
        /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   437
         * This is the plain type-checking mode. Produces side-effects on the underlying AST node
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   438
         */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21718
diff changeset
   439
        CHECK
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   440
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   441
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   442
    /**
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   443
     * Performs speculative attribution of a lambda body and returns the speculative lambda tree,
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   444
     * in the absence of a target-type. Since {@link Attr#visitLambda(JCLambda)} cannot type-check
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   445
     * lambda bodies w/o a suitable target-type, this routine 'unrolls' the lambda by turning it
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   446
     * into a regular block, speculatively type-checks the block and then puts back the pieces.
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   447
     */
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   448
    JCLambda attribSpeculativeLambda(JCLambda that, Env<AttrContext> env, ResultInfo resultInfo) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   449
        ListBuffer<JCStatement> stats = new ListBuffer<>();
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   450
        stats.addAll(that.params);
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   451
        if (that.getBodyKind() == JCLambda.BodyKind.EXPRESSION) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   452
            stats.add(make.Return((JCExpression)that.body));
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   453
        } else {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   454
            stats.add((JCBlock)that.body);
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   455
        }
49947
cece972575ac 8202372: Diagnostic with incorrect line info generated when compiling lambda expression
mcimadamore
parents: 48549
diff changeset
   456
        JCBlock lambdaBlock = make.at(that.pos).Block(0, stats.toList());
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   457
        Env<AttrContext> localEnv = attr.lambdaEnv(that, env);
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   458
        try {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   459
            localEnv.info.returnResult = resultInfo;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   460
            JCBlock speculativeTree = (JCBlock)attribSpeculative(lambdaBlock, localEnv, resultInfo);
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   461
            List<JCVariableDecl> args = speculativeTree.getStatements().stream()
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   462
                    .filter(s -> s.hasTag(Tag.VARDEF))
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   463
                    .map(t -> (JCVariableDecl)t)
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   464
                    .collect(List.collector());
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   465
            JCTree lambdaBody = speculativeTree.getStatements().last();
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   466
            if (lambdaBody.hasTag(Tag.RETURN)) {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   467
                lambdaBody = ((JCReturn)lambdaBody).expr;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   468
            }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   469
            JCLambda speculativeLambda = make.Lambda(args, lambdaBody);
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   470
            attr.preFlow(speculativeLambda);
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   471
            flow.analyzeLambda(env, speculativeLambda, make, false);
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   472
            return speculativeLambda;
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   473
        } finally {
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   474
            localEnv.info.scope.leave();
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   475
        }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   476
    }
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   477
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   478
    /**
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   479
     * Routine that performs speculative type-checking; the input AST node is
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   480
     * cloned (to avoid side-effects cause by Attr) and compiler state is
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   481
     * restored after type-checking. All diagnostics (but critical ones) are
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   482
     * disabled during speculative type-checking.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   483
     */
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26531
diff changeset
   484
    JCTree attribSpeculative(JCTree tree, Env<AttrContext> env, ResultInfo resultInfo) {
29776
984a79b71cfe 8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents: 29774
diff changeset
   485
        return attribSpeculative(tree, env, resultInfo, treeCopier,
58018
a3c63a9dfb2c 8177068: incomplete classpath causes NPE in Flow
jlahoda
parents: 57963
diff changeset
   486
                newTree->new DeferredDiagnosticHandler(log), AttributionMode.SPECULATIVE, null);
44393
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   487
    }
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   488
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   489
    JCTree attribSpeculative(JCTree tree, Env<AttrContext> env, ResultInfo resultInfo, LocalCacheContext localCache) {
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   490
        return attribSpeculative(tree, env, resultInfo, treeCopier,
58018
a3c63a9dfb2c 8177068: incomplete classpath causes NPE in Flow
jlahoda
parents: 57963
diff changeset
   491
                newTree->new DeferredDiagnosticHandler(log), AttributionMode.SPECULATIVE, localCache);
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents: 27551
diff changeset
   492
    }
16309
db71f8ecc136 8008276: assertion error in com.sun.tools.javac.comp.TransTypes.visitApply
mcimadamore
parents: 16294
diff changeset
   493
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents: 27551
diff changeset
   494
    <Z> JCTree attribSpeculative(JCTree tree, Env<AttrContext> env, ResultInfo resultInfo, TreeCopier<Z> deferredCopier,
57963
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55622
diff changeset
   495
                                 Function<JCTree, DeferredDiagnosticHandler> diagHandlerCreator, AttributionMode attributionMode,
44393
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   496
                                 LocalCacheContext localCache) {
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents: 27551
diff changeset
   497
        final JCTree newTree = deferredCopier.copy(tree);
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents: 27551
diff changeset
   498
        Env<AttrContext> speculativeEnv = env.dup(newTree, env.info.dup(env.info.scope.dupUnshared(env.info.scope.owner)));
57963
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55622
diff changeset
   499
        speculativeEnv.info.attributionMode = attributionMode;
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents: 27551
diff changeset
   500
        Log.DeferredDiagnosticHandler deferredDiagnosticHandler = diagHandlerCreator.apply(newTree);
58018
a3c63a9dfb2c 8177068: incomplete classpath causes NPE in Flow
jlahoda
parents: 57963
diff changeset
   501
        DeferredCompletionFailureHandler.Handler prevCFHandler = dcfh.setHandler(dcfh.speculativeCodeHandler);
57963
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55622
diff changeset
   502
        int nwarnings = log.nwarnings;
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55622
diff changeset
   503
        log.nwarnings = 0;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   504
        try {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   505
            attr.attribTree(newTree, speculativeEnv, resultInfo);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   506
            return newTree;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   507
        } finally {
58018
a3c63a9dfb2c 8177068: incomplete classpath causes NPE in Flow
jlahoda
parents: 57963
diff changeset
   508
            dcfh.setHandler(prevCFHandler);
57963
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55622
diff changeset
   509
            log.nwarnings += nwarnings;
55622
0b470386f5f7 8223443: Calling Trees.getScope early changes names of local/anonymous classes
jlahoda
parents: 55306
diff changeset
   510
            enter.unenter(env.toplevel, newTree);
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14359
diff changeset
   511
            log.popDiagnosticHandler(deferredDiagnosticHandler);
44393
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   512
            if (localCache != null) {
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   513
                localCache.leave();
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   514
            }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   515
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   516
    }
28142
32a6b1af81b1 8064365: Better support for finder capabilities in target-typing context
mcimadamore
parents: 27551
diff changeset
   517
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   518
    /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   519
     * A deferred context is created on each method check. A deferred context is
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   520
     * used to keep track of information associated with the method check, such as
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   521
     * the symbol of the method being checked, the overload resolution phase,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   522
     * the kind of attribution mode to be applied to deferred types and so forth.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   523
     * As deferred types are processed (by the method check routine) stuck AST nodes
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   524
     * are added (as new deferred attribution nodes) to this context. The complete()
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   525
     * routine makes sure that all pending nodes are properly processed, by
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   526
     * progressively instantiating all inference variables on which one or more
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   527
     * deferred attribution node is stuck.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   528
     */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   529
    class DeferredAttrContext {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   530
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   531
        /** attribution mode */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   532
        final AttrMode mode;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   533
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   534
        /** symbol of the method being checked */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   535
        final Symbol msym;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   536
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   537
        /** method resolution step */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   538
        final Resolve.MethodResolutionPhase phase;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   539
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   540
        /** inference context */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   541
        final InferenceContext inferenceContext;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   542
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   543
        /** parent deferred context */
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   544
        final DeferredAttrContext parent;
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   545
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   546
        /** Warner object to report warnings */
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   547
        final Warner warn;
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   548
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   549
        /** list of deferred attribution nodes to be processed */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21718
diff changeset
   550
        ArrayList<DeferredAttrNode> deferredAttrNodes = new ArrayList<>();
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   551
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   552
        DeferredAttrContext(AttrMode mode, Symbol msym, MethodResolutionPhase phase,
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   553
                InferenceContext inferenceContext, DeferredAttrContext parent, Warner warn) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   554
            this.mode = mode;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   555
            this.msym = msym;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   556
            this.phase = phase;
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   557
            this.parent = parent;
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   558
            this.warn = warn;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   559
            this.inferenceContext = inferenceContext;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   560
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   561
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   562
        /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   563
         * Adds a node to the list of deferred attribution nodes - used by Resolve.rawCheckArgumentsApplicable
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   564
         * Nodes added this way act as 'roots' for the out-of-order method checking process.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   565
         */
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   566
        void addDeferredAttrNode(final DeferredType dt, ResultInfo resultInfo,
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   567
                DeferredStuckPolicy deferredStuckPolicy) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   568
            deferredAttrNodes.add(new DeferredAttrNode(dt, resultInfo, deferredStuckPolicy));
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   569
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   570
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   571
        /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   572
         * Incrementally process all nodes, by skipping 'stuck' nodes and attributing
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   573
         * 'unstuck' ones. If at any point no progress can be made (no 'unstuck' nodes)
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   574
         * some inference variable might get eagerly instantiated so that all nodes
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   575
         * can be type-checked.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   576
         */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   577
        void complete() {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   578
            while (!deferredAttrNodes.isEmpty()) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   579
                boolean progress = false;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   580
                //scan a defensive copy of the node list - this is because a deferred
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   581
                //attribution round can add new nodes to the list
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   582
                for (DeferredAttrNode deferredAttrNode : List.from(deferredAttrNodes)) {
38516
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   583
                    if (deferredAttrNode.process(this)) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   584
                        deferredAttrNodes.remove(deferredAttrNode);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   585
                        progress = true;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   586
                    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   587
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   588
                if (!progress) {
25444
27045478cf23 8033483: Should ignore nested lambda bodies during overload resolution
vromero
parents: 25443
diff changeset
   589
                    if (insideOverloadPhase()) {
27045478cf23 8033483: Should ignore nested lambda bodies during overload resolution
vromero
parents: 25443
diff changeset
   590
                        for (DeferredAttrNode deferredNode: deferredAttrNodes) {
27045478cf23 8033483: Should ignore nested lambda bodies during overload resolution
vromero
parents: 25443
diff changeset
   591
                            deferredNode.dt.tree.type = Type.noType;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   592
                        }
25444
27045478cf23 8033483: Should ignore nested lambda bodies during overload resolution
vromero
parents: 25443
diff changeset
   593
                        return;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   594
                    }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   595
                    //remove all variables that have already been instantiated
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   596
                    //from the list of stuck variables
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   597
                    try {
38516
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   598
                        //find stuck expression to unstuck
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   599
                        DeferredAttrNode toUnstuck = pickDeferredNode();
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   600
                        inferenceContext.solveAny(List.from(toUnstuck.deferredStuckPolicy.stuckVars()), warn);
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   601
                        inferenceContext.notifyChange();
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   602
                    } catch (Infer.GraphStrategy.NodeNotFoundException ex) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   603
                        //this means that we are in speculative mode and the
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   604
                        //set of contraints are too tight for progess to be made.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   605
                        //Just leave the remaining expressions as stuck.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   606
                        break;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   607
                    }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   608
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   609
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   610
        }
25444
27045478cf23 8033483: Should ignore nested lambda bodies during overload resolution
vromero
parents: 25443
diff changeset
   611
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   612
        public boolean insideOverloadPhase() {
25444
27045478cf23 8033483: Should ignore nested lambda bodies during overload resolution
vromero
parents: 25443
diff changeset
   613
            DeferredAttrContext dac = this;
27045478cf23 8033483: Should ignore nested lambda bodies during overload resolution
vromero
parents: 25443
diff changeset
   614
            if (dac == emptyDeferredAttrContext) {
27045478cf23 8033483: Should ignore nested lambda bodies during overload resolution
vromero
parents: 25443
diff changeset
   615
                return false;
27045478cf23 8033483: Should ignore nested lambda bodies during overload resolution
vromero
parents: 25443
diff changeset
   616
            }
27045478cf23 8033483: Should ignore nested lambda bodies during overload resolution
vromero
parents: 25443
diff changeset
   617
            if (dac.mode == AttrMode.SPECULATIVE) {
27045478cf23 8033483: Should ignore nested lambda bodies during overload resolution
vromero
parents: 25443
diff changeset
   618
                return true;
27045478cf23 8033483: Should ignore nested lambda bodies during overload resolution
vromero
parents: 25443
diff changeset
   619
            }
27045478cf23 8033483: Should ignore nested lambda bodies during overload resolution
vromero
parents: 25443
diff changeset
   620
            return dac.parent.insideOverloadPhase();
27045478cf23 8033483: Should ignore nested lambda bodies during overload resolution
vromero
parents: 25443
diff changeset
   621
        }
38516
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   622
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   623
        /**
50564
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   624
         * Pick the deferred node to be unstuck. First, deferred nodes are organized into a graph
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   625
         * (see {@code DeferredAttrContext.buildStuckGraph()}, where a node N1 depends on another node N2
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   626
         * if its input variable depends (as per the inference graph) on the output variables of N2
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   627
         * (see {@code DeferredAttrContext.canInfluence()}.
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   628
         *
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   629
         * Then, the chosen deferred node is the first strongly connected component containing exactly
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   630
         * one node found in such a graph. If no such component is found, the first deferred node is chosen.
38516
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   631
         */
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   632
        DeferredAttrNode pickDeferredNode() {
50564
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   633
            List<StuckNode> stuckGraph = buildStuckGraph();
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   634
            //compute tarjan on the stuck graph
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   635
            List<? extends StuckNode> csn = GraphUtils.tarjan(stuckGraph).get(0);
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   636
            return csn.length() == 1 ? csn.get(0).data : deferredAttrNodes.get(0);
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   637
        }
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   638
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   639
        List<StuckNode> buildStuckGraph() {
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   640
            //first, build inference graph
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   641
            infer.doIncorporation(inferenceContext, warn);
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   642
            InferenceGraph graph = infer.new GraphSolver(inferenceContext, types.noWarnings)
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   643
                    .new InferenceGraph();
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   644
            //then, build stuck graph
38516
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   645
            List<StuckNode> nodes = deferredAttrNodes.stream()
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   646
                    .map(StuckNode::new)
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   647
                    .collect(List.collector());
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   648
            //init stuck expression graph; a deferred node A depends on a deferred node B iff
50564
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   649
            //B's output variables can influence A's input variables.
38516
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   650
            for (StuckNode sn1 : nodes) {
50564
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   651
                for (StuckNode sn2 : nodes) {
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   652
                    if (sn1 != sn2 && canInfluence(graph, sn2, sn1)) {
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   653
                        sn1.deps.add(sn2);
38516
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   654
                    }
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   655
                }
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   656
            }
50564
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   657
            return nodes;
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   658
        }
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   659
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   660
        boolean canInfluence(InferenceGraph graph, StuckNode sn1, StuckNode sn2) {
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   661
            Set<Type> outputVars = sn1.data.deferredStuckPolicy.depVars();
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   662
            for (Type inputVar : sn2.data.deferredStuckPolicy.stuckVars()) {
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   663
                InferenceGraph.Node inputNode = graph.findNode(inputVar);
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   664
                //already solved stuck vars do not appear in the graph
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   665
                if (inputNode != null) {
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   666
                    Set<InferenceGraph.Node> inputClosure = inputNode.closure();
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   667
                    if (outputVars.stream()
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   668
                            .map(graph::findNode)
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   669
                            .anyMatch(inputClosure::contains)) {
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   670
                        return true;
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   671
                    }
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   672
                }
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   673
            }
ef7c4c77d9fa 8178150: Regression in logic for handling inference stuck constraints
mcimadamore
parents: 50533
diff changeset
   674
            return false;
38516
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   675
        }
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   676
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   677
        class StuckNode extends GraphUtils.TarjanNode<DeferredAttrNode, StuckNode> {
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   678
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   679
            Set<StuckNode> deps = new HashSet<>();
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   680
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   681
            StuckNode(DeferredAttrNode data) {
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   682
                super(data);
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   683
            }
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   684
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   685
            @Override
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   686
            public DependencyKind[] getSupportedDependencyKinds() {
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   687
                return new DependencyKind[] { Infer.DependencyKind.STUCK };
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   688
            }
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   689
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   690
            @Override
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   691
            public Collection<? extends StuckNode> getDependenciesByKind(DependencyKind dk) {
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   692
                if (dk == Infer.DependencyKind.STUCK) {
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   693
                    return deps;
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   694
                } else {
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   695
                    throw new IllegalStateException();
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   696
                }
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   697
            }
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   698
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   699
            @Override
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   700
            public Iterable<? extends StuckNode> getAllDependencies() {
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   701
                return deps;
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   702
            }
b643c42e9d25 8156954: javac incorrectly complains of incompatible types
mcimadamore
parents: 38512
diff changeset
   703
        }
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   704
    }
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   705
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   706
    /**
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   707
     * Class representing a deferred attribution node. It keeps track of
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   708
     * a deferred type, along with the expected target type information.
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   709
     */
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   710
    class DeferredAttrNode {
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   711
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   712
        /** underlying deferred type */
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   713
        DeferredType dt;
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   714
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   715
        /** underlying target type information */
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   716
        ResultInfo resultInfo;
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   717
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   718
        /** stuck policy associated with this node */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   719
        DeferredStuckPolicy deferredStuckPolicy;
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   720
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   721
        DeferredAttrNode(DeferredType dt, ResultInfo resultInfo, DeferredStuckPolicy deferredStuckPolicy) {
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   722
            this.dt = dt;
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   723
            this.resultInfo = resultInfo;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   724
            this.deferredStuckPolicy = deferredStuckPolicy;
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   725
        }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   726
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   727
        /**
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   728
         * Process a deferred attribution node.
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   729
         * Invariant: a stuck node cannot be processed.
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   730
         */
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   731
        @SuppressWarnings("fallthrough")
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   732
        boolean process(final DeferredAttrContext deferredAttrContext) {
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   733
            switch (deferredAttrContext.mode) {
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   734
                case SPECULATIVE:
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   735
                    if (deferredStuckPolicy.isStuck()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   736
                        dt.check(resultInfo, dummyStuckPolicy, new StructuralStuckChecker());
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   737
                        return true;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   738
                    } else {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   739
                        Assert.error("Cannot get here");
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   740
                    }
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   741
                case CHECK:
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   742
                    if (deferredStuckPolicy.isStuck()) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   743
                        //stuck expression - see if we can propagate
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   744
                        if (deferredAttrContext.parent != emptyDeferredAttrContext &&
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   745
                                Type.containsAny(deferredAttrContext.parent.inferenceContext.inferencevars,
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   746
                                        List.from(deferredStuckPolicy.stuckVars()))) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   747
                            deferredAttrContext.parent.addDeferredAttrNode(dt,
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   748
                                    resultInfo.dup(new Check.NestedCheckContext(resultInfo.checkContext) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   749
                                @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   750
                                public InferenceContext inferenceContext() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   751
                                    return deferredAttrContext.parent.inferenceContext;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   752
                                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   753
                                @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   754
                                public DeferredAttrContext deferredAttrContext() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   755
                                    return deferredAttrContext.parent;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   756
                                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   757
                            }), deferredStuckPolicy);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   758
                            dt.tree.type = Type.stuckType;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   759
                            return true;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   760
                        } else {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   761
                            return false;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   762
                        }
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   763
                    } else {
25444
27045478cf23 8033483: Should ignore nested lambda bodies during overload resolution
vromero
parents: 25443
diff changeset
   764
                        Assert.check(!deferredAttrContext.insideOverloadPhase(),
27045478cf23 8033483: Should ignore nested lambda bodies during overload resolution
vromero
parents: 25443
diff changeset
   765
                                "attribution shouldn't be happening here");
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   766
                        ResultInfo instResultInfo =
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   767
                                resultInfo.dup(deferredAttrContext.inferenceContext.asInstType(resultInfo.pt));
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   768
                        dt.check(instResultInfo, dummyStuckPolicy, basicCompleter);
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   769
                        return true;
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   770
                    }
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   771
                default:
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   772
                    throw new AssertionError("Bad mode");
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   773
            }
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   774
        }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   775
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   776
        /**
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   777
         * Structural checker for stuck expressions
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   778
         */
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   779
        class StructuralStuckChecker extends TreeScanner implements DeferredTypeCompleter {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   780
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   781
            ResultInfo resultInfo;
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   782
            InferenceContext inferenceContext;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   783
            Env<AttrContext> env;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   784
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   785
            public Type complete(DeferredType dt, ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) {
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   786
                this.resultInfo = resultInfo;
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   787
                this.inferenceContext = deferredAttrContext.inferenceContext;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   788
                this.env = dt.env;
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   789
                dt.tree.accept(this);
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   790
                dt.speculativeCache.put(stuckTree, resultInfo);
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   791
                return Type.noType;
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   792
            }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   793
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   794
            @Override
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   795
            public void visitLambda(JCLambda tree) {
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   796
                Check.CheckContext checkContext = resultInfo.checkContext;
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   797
                Type pt = resultInfo.pt;
24066
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   798
                if (!inferenceContext.inferencevars.contains(pt)) {
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   799
                    //must be a functional descriptor
24066
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   800
                    Type descriptorType = null;
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   801
                    try {
24066
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   802
                        descriptorType = types.findDescriptorType(pt);
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   803
                    } catch (Types.FunctionDescriptorLookupError ex) {
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   804
                        checkContext.report(null, ex.getDiagnostic());
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   805
                    }
24066
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   806
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   807
                    if (descriptorType.getParameterTypes().length() != tree.params.length()) {
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   808
                        checkContext.report(tree,
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44393
diff changeset
   809
                                diags.fragment(Fragments.IncompatibleArgTypesInLambda));
24066
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   810
                    }
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   811
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   812
                    Type currentReturnType = descriptorType.getReturnType();
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   813
                    boolean returnTypeIsVoid = currentReturnType.hasTag(VOID);
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   814
                    if (tree.getBodyKind() == BodyKind.EXPRESSION) {
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   815
                        boolean isExpressionCompatible = !returnTypeIsVoid ||
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   816
                            TreeInfo.isExpressionStatement((JCExpression)tree.getBody());
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   817
                        if (!isExpressionCompatible) {
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   818
                            resultInfo.checkContext.report(tree.pos(),
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44393
diff changeset
   819
                                diags.fragment(Fragments.IncompatibleRetTypeInLambda(Fragments.MissingRetVal(currentReturnType))));
24066
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   820
                        }
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   821
                    } else {
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   822
                        LambdaBodyStructChecker lambdaBodyChecker =
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   823
                                new LambdaBodyStructChecker();
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   824
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   825
                        tree.body.accept(lambdaBodyChecker);
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   826
                        boolean isVoidCompatible = lambdaBodyChecker.isVoidCompatible;
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   827
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   828
                        if (returnTypeIsVoid) {
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   829
                            if (!isVoidCompatible) {
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   830
                                resultInfo.checkContext.report(tree.pos(),
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44393
diff changeset
   831
                                    diags.fragment(Fragments.UnexpectedRetVal));
24066
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   832
                            }
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   833
                        } else {
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   834
                            boolean isValueCompatible = lambdaBodyChecker.isPotentiallyValueCompatible
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   835
                                && !canLambdaBodyCompleteNormally(tree);
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   836
                            if (!isValueCompatible && !isVoidCompatible) {
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   837
                                log.error(tree.body.pos(),
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44393
diff changeset
   838
                                          Errors.LambdaBodyNeitherValueNorVoidCompatible);
24066
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   839
                            }
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   840
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   841
                            if (!isValueCompatible) {
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   842
                                resultInfo.checkContext.report(tree.pos(),
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 44393
diff changeset
   843
                                    diags.fragment(Fragments.IncompatibleRetTypeInLambda(Fragments.MissingRetVal(currentReturnType))));
24066
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   844
                            }
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   845
                        }
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   846
                    }
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   847
                }
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   848
            }
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   849
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   850
            boolean canLambdaBodyCompleteNormally(JCLambda tree) {
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   851
                List<JCVariableDecl> oldParams = tree.params;
32911
6ee3c12d2d18 8137269: Add better support for local caching in ArgumentAttr
mcimadamore
parents: 32709
diff changeset
   852
                LocalCacheContext localCacheContext = argumentAttr.withLocalCacheContext();
24066
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   853
                try {
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   854
                    tree.params = tree.params.stream()
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   855
                            .map(vd -> make.VarDef(vd.mods, vd.name, make.Erroneous(), null))
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   856
                            .collect(List.collector());
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   857
                    return attribSpeculativeLambda(tree, env, attr.unknownExprInfo).canCompleteNormally;
24066
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   858
                } finally {
32911
6ee3c12d2d18 8137269: Add better support for local caching in ArgumentAttr
mcimadamore
parents: 32709
diff changeset
   859
                    localCacheContext.leave();
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
   860
                    tree.params = oldParams;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   861
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   862
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   863
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   864
            @Override
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   865
            public void visitNewClass(JCNewClass tree) {
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   866
                //do nothing
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   867
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   868
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   869
            @Override
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   870
            public void visitApply(JCMethodInvocation tree) {
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   871
                //do nothing
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   872
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   873
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   874
            @Override
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   875
            public void visitReference(JCMemberReference tree) {
44393
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   876
                Assert.checkNonNull(tree.getOverloadKind());
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   877
                Check.CheckContext checkContext = resultInfo.checkContext;
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   878
                Type pt = resultInfo.pt;
24066
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   879
                if (!inferenceContext.inferencevars.contains(pt)) {
50250
04f9bb270ab8 8203488: Remove error generation from TransTypes
mcimadamore
parents: 49947
diff changeset
   880
                    Type descriptor = null;
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   881
                    try {
50250
04f9bb270ab8 8203488: Remove error generation from TransTypes
mcimadamore
parents: 49947
diff changeset
   882
                        descriptor = types.findDescriptorType(pt);
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   883
                    } catch (Types.FunctionDescriptorLookupError ex) {
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   884
                        checkContext.report(null, ex.getDiagnostic());
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15039
diff changeset
   885
                    }
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   886
                    Env<AttrContext> localEnv = env.dup(tree);
44393
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   887
                    JCExpression exprTree;
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   888
                    exprTree = (JCExpression)attribSpeculative(tree.getQualifierExpression(), localEnv,
ce94820fa9d1 8176714: javac is wrongly assuming that field JCMemberReference.overloadKind has been assigned to
vromero
parents: 44186
diff changeset
   889
                            attr.memberReferenceQualifierResult(tree), argumentAttr.withLocalCacheContext());
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19914
diff changeset
   890
                    ListBuffer<Type> argtypes = new ListBuffer<>();
50250
04f9bb270ab8 8203488: Remove error generation from TransTypes
mcimadamore
parents: 49947
diff changeset
   891
                    for (Type t : descriptor.getParameterTypes()) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   892
                        argtypes.append(Type.noType);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   893
                    }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   894
                    JCMemberReference mref2 = new TreeCopier<Void>(make).copy(tree);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   895
                    mref2.expr = exprTree;
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 20249
diff changeset
   896
                    Symbol lookupSym =
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28275
diff changeset
   897
                            rs.resolveMemberReference(localEnv, mref2, exprTree.type,
50250
04f9bb270ab8 8203488: Remove error generation from TransTypes
mcimadamore
parents: 49947
diff changeset
   898
                                    tree.name, argtypes.toList(), List.nil(), descriptor, rs.arityMethodCheck,
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28275
diff changeset
   899
                                    inferenceContext, rs.structuralReferenceChooser).fst;
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 20249
diff changeset
   900
                    switch (lookupSym.kind) {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26781
diff changeset
   901
                        case WRONG_MTH:
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26781
diff changeset
   902
                        case WRONG_MTHS:
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28275
diff changeset
   903
                            //note: as argtypes are erroneous types, type-errors must
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28275
diff changeset
   904
                            //have been caused by arity mismatch
28458
f20c5dd05e34 8069164: Fix langtools make build so that diagnostic framework can be used
mcimadamore
parents: 28457
diff changeset
   905
                            checkContext.report(tree, diags.fragment(Fragments.IncompatibleArgTypesInMref));
28456
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28275
diff changeset
   906
                            break;
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28275
diff changeset
   907
                        case ABSENT_MTH:
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28275
diff changeset
   908
                        case STATICERR:
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28275
diff changeset
   909
                            //if no method found, or method found with wrong staticness, report better message
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28275
diff changeset
   910
                            checkContext.report(tree, ((ResolveError)lookupSym).getDiagnostic(DiagnosticType.FRAGMENT,
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28275
diff changeset
   911
                                    tree, exprTree.type.tsym, exprTree.type, tree.name, argtypes.toList(), List.nil()));
b87fdde1404d 8068995: Cleanup method reference lookup code
mcimadamore
parents: 28275
diff changeset
   912
                            break;
16294
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 15717
diff changeset
   913
                    }
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15039
diff changeset
   914
                }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   915
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   916
        }
24066
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   917
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   918
        /* This visitor looks for return statements, its analysis will determine if
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   919
         * a lambda body is void or value compatible. We must analyze return
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   920
         * statements contained in the lambda body only, thus any return statement
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   921
         * contained in an inner class or inner lambda body, should be ignored.
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   922
         */
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   923
        class LambdaBodyStructChecker extends TreeScanner {
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   924
            boolean isVoidCompatible = true;
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   925
            boolean isPotentiallyValueCompatible = true;
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   926
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   927
            @Override
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   928
            public void visitClassDef(JCClassDecl tree) {
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   929
                // do nothing
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   930
            }
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   931
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   932
            @Override
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   933
            public void visitLambda(JCLambda tree) {
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   934
                // do nothing
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   935
            }
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   936
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   937
            @Override
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   938
            public void visitNewClass(JCNewClass tree) {
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   939
                // do nothing
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   940
            }
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   941
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   942
            @Override
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   943
            public void visitReturn(JCReturn tree) {
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   944
                if (tree.expr != null) {
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   945
                    isVoidCompatible = false;
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   946
                } else {
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   947
                    isPotentiallyValueCompatible = false;
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   948
                }
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   949
            }
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
   950
        }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   951
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   952
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   953
    /** an empty deferred attribution context - all methods throw exceptions */
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18902
diff changeset
   954
    final DeferredAttrContext emptyDeferredAttrContext;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   955
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   956
    /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   957
     * Map a list of types possibly containing one or more deferred types
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   958
     * into a list of ordinary types. Each deferred type D is mapped into a type T,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   959
     * where T is computed by retrieving the type that has already been
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   960
     * computed for D during a previous deferred attribution round of the given kind.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   961
     */
50898
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
   962
    class DeferredTypeMap<T> extends StructuralTypeMapping<T> {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   963
        DeferredAttrContext deferredAttrContext;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   964
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   965
        protected DeferredTypeMap(AttrMode mode, Symbol msym, MethodResolutionPhase phase) {
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   966
            this.deferredAttrContext = new DeferredAttrContext(mode, msym, phase,
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   967
                    infer.emptyContext, emptyDeferredAttrContext, types.noWarnings);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   968
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   969
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   970
        @Override
50898
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
   971
        public Type visitType(Type t, T p) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14258
diff changeset
   972
            if (!t.hasTag(DEFERRED)) {
50898
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
   973
                return super.visitType(t, p);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   974
            } else {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   975
                DeferredType dt = (DeferredType)t;
50898
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
   976
                return typeOf(dt, p);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   977
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   978
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   979
50898
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
   980
        protected Type typeOf(DeferredType dt, T p) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   981
            switch (deferredAttrContext.mode) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   982
                case CHECK:
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   983
                    return dt.tree.type == null ? Type.noType : dt.tree.type;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   984
                case SPECULATIVE:
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   985
                    return dt.speculativeType(deferredAttrContext.msym, deferredAttrContext.phase);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   986
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   987
            Assert.error();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   988
            return null;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   989
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   990
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   991
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   992
    /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   993
     * Specialized recovery deferred mapping.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   994
     * Each deferred type D is mapped into a type T, where T is computed either by
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   995
     * (i) retrieving the type that has already been computed for D during a previous
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   996
     * attribution round (as before), or (ii) by synthesizing a new type R for D
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   997
     * (the latter step is useful in a recovery scenario).
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   998
     */
50898
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
   999
    public class RecoveryDeferredTypeMap extends DeferredTypeMap<Type> {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
  1000
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
  1001
        public RecoveryDeferredTypeMap(AttrMode mode, Symbol msym, MethodResolutionPhase phase) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14538
diff changeset
  1002
            super(mode, msym, phase != null ? phase : MethodResolutionPhase.BOX);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
  1003
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
  1004
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
  1005
        @Override
50898
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1006
        protected Type typeOf(DeferredType dt, Type pt) {
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1007
            Type owntype = super.typeOf(dt, pt);
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14538
diff changeset
  1008
            return owntype == Type.noType ?
50898
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1009
                        recover(dt, pt) : owntype;
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1010
        }
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1011
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1012
        @Override
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1013
        public Type visitMethodType(Type.MethodType t, Type pt) {
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1014
            if (t.hasTag(METHOD) && deferredAttrContext.mode == AttrMode.CHECK) {
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1015
                Type mtype = deferredAttrContext.msym.type;
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1016
                mtype = mtype.hasTag(ERROR) ? ((ErrorType)mtype).getOriginalType() : null;
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1017
                if (mtype != null && mtype.hasTag(METHOD)) {
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1018
                    List<Type> argtypes1 = map(t.getParameterTypes(), mtype.getParameterTypes());
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1019
                    Type restype1 = visit(t.getReturnType(), mtype.getReturnType());
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1020
                    List<Type> thrown1 = map(t.getThrownTypes(), mtype.getThrownTypes());
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1021
                    if (argtypes1 == t.getParameterTypes() &&
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1022
                        restype1 == t.getReturnType() &&
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1023
                        thrown1 == t.getThrownTypes()) return t;
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1024
                    else return new MethodType(argtypes1, restype1, thrown1, t.tsym);
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1025
                }
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1026
            }
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1027
            return super.visitMethodType(t, pt);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
  1028
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
  1029
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
  1030
        /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
  1031
         * Synthesize a type for a deferred type that hasn't been previously
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
  1032
         * reduced to an ordinary type. Functional deferred types and conditionals
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
  1033
         * are mapped to themselves, in order to have a richer diagnostic
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
  1034
         * representation. Remaining deferred types are attributed using
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
  1035
         * a default expected type (j.l.Object).
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
  1036
         */
50898
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1037
        private Type recover(DeferredType dt, Type pt) {
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1038
            dt.check(attr.new RecoveryInfo(deferredAttrContext, pt != null ? pt : Type.recoveryType) {
18902
972298345a83 8016059: Cannot compile following lambda
mcimadamore
parents: 18901
diff changeset
  1039
                @Override
972298345a83 8016059: Cannot compile following lambda
mcimadamore
parents: 18901
diff changeset
  1040
                protected Type check(DiagnosticPosition pos, Type found) {
972298345a83 8016059: Cannot compile following lambda
mcimadamore
parents: 18901
diff changeset
  1041
                    return chk.checkNonVoid(pos, super.check(pos, found));
972298345a83 8016059: Cannot compile following lambda
mcimadamore
parents: 18901
diff changeset
  1042
                }
972298345a83 8016059: Cannot compile following lambda
mcimadamore
parents: 18901
diff changeset
  1043
            });
29554
6d7957bd6866 8074100: Turn Type.Mapping into a true visitor
mcimadamore
parents: 29292
diff changeset
  1044
            return super.visit(dt);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
  1045
        }
50898
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1046
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1047
        private List<Type> map(List<Type> ts, List<Type> pts) {
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1048
            if (ts.nonEmpty()) {
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1049
                List<Type> tail1 = map(ts.tail, pts != null ? pts.tail : null);
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1050
                Type t = visit(ts.head, pts != null && pts.nonEmpty() ? pts.head : null);
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1051
                if (tail1 != ts.tail || t != ts.head)
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1052
                    return tail1.prepend(t);
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1053
            }
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1054
            return ts;
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50564
diff changeset
  1055
        }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
  1056
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
  1057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
  1058
    /**
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1059
     * A special tree scanner that would only visit portions of a given tree.
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1060
     * The set of nodes visited by the scanner can be customized at construction-time.
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1061
     */
32709
55d136799f79 8078093: Severe compiler performance regression Java 7 to 8 for nested method invocations
mcimadamore
parents: 31937
diff changeset
  1062
    abstract static class FilterScanner extends com.sun.tools.javac.tree.TreeScanner {
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1063
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1064
        final Filter<JCTree> treeFilter;
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1065
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1066
        FilterScanner(final Set<JCTree.Tag> validTags) {
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 38516
diff changeset
  1067
            this.treeFilter = t -> validTags.contains(t.getTag());
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1068
        }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1069
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1070
        @Override
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1071
        public void scan(JCTree tree) {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1072
            if (tree != null) {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1073
                if (treeFilter.accepts(tree)) {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1074
                    super.scan(tree);
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1075
                } else {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1076
                    skip(tree);
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1077
                }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1078
            }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1079
        }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1080
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1081
        /**
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1082
         * handler that is executed when a node has been discarded
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1083
         */
24066
1dfb66929538 8029718: Should always use lambda body structure to disambiguate overload resolution
vromero
parents: 22163
diff changeset
  1084
        void skip(JCTree tree) {}
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1085
    }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1086
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1087
    /**
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1088
     * A tree scanner suitable for visiting the target-type dependent nodes of
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1089
     * a given argument expression.
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1090
     */
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1091
    static class PolyScanner extends FilterScanner {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1092
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1093
        PolyScanner() {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1094
            super(EnumSet.of(CONDEXPR, PARENS, LAMBDA, REFERENCE));
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1095
        }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1096
    }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1097
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1098
    /**
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1099
     * A tree scanner suitable for visiting the target-type dependent nodes nested
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1100
     * within a lambda expression body.
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1101
     */
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1102
    static class LambdaReturnScanner extends FilterScanner {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1103
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1104
        LambdaReturnScanner() {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1105
            super(EnumSet.of(BLOCK, CASE, CATCH, DOLOOP, FOREACHLOOP,
25275
e5e9bf4b8c49 8042759: Lambda returning implicitly-typed lambdas considered pertinent to applicability
vromero
parents: 25007
diff changeset
  1106
                    FORLOOP, IF, RETURN, SYNCHRONIZED, SWITCH, TRY, WHILELOOP));
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1107
        }
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1108
    }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1109
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1110
    /**
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 50898
diff changeset
  1111
     * A tree scanner suitable for visiting the target-type dependent nodes nested
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 50898
diff changeset
  1112
     * within a switch expression body.
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 50898
diff changeset
  1113
     */
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 50898
diff changeset
  1114
    static class SwitchExpressionScanner extends FilterScanner {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 50898
diff changeset
  1115
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 50898
diff changeset
  1116
        SwitchExpressionScanner() {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 50898
diff changeset
  1117
            super(EnumSet.of(BLOCK, CASE, CATCH, DOLOOP, FOREACHLOOP,
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 52625
diff changeset
  1118
                    FORLOOP, IF, SYNCHRONIZED, SWITCH, TRY, WHILELOOP, YIELD));
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 50898
diff changeset
  1119
        }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 50898
diff changeset
  1120
    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 50898
diff changeset
  1121
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 50898
diff changeset
  1122
    /**
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1123
     * This visitor is used to check that structural expressions conform
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1124
     * to their target - this step is required as inference could end up
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1125
     * inferring types that make some of the nested expressions incompatible
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1126
     * with their corresponding instantiated target
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1127
     */
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1128
    class CheckStuckPolicy extends PolyScanner implements DeferredStuckPolicy, Infer.FreeTypeListener {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1129
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1130
        Type pt;
31937
ad43a6639c4a 8132215: class InferenceContext should live in a separate file
vromero
parents: 30728
diff changeset
  1131
        InferenceContext inferenceContext;
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21718
diff changeset
  1132
        Set<Type> stuckVars = new LinkedHashSet<>();
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21718
diff changeset
  1133
        Set<Type> depVars = new LinkedHashSet<>();
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1134
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1135
        @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1136
        public boolean isStuck() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1137
            return !stuckVars.isEmpty();
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1138
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1139
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1140
        @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1141
        public Set<Type> stuckVars() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1142
            return stuckVars;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1143
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1144
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1145
        @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1146
        public Set<Type> depVars() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1147
            return depVars;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1148
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1149
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1150
        public CheckStuckPolicy(ResultInfo resultInfo, DeferredType dt) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1151
            this.pt = resultInfo.pt;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1152
            this.inferenceContext = resultInfo.checkContext.inferenceContext();
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1153
            scan(dt.tree);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1154
            if (!stuckVars.isEmpty()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1155
                resultInfo.checkContext.inferenceContext()
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1156
                        .addFreeTypeListener(List.from(stuckVars), this);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1157
            }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1158
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1159
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1160
        @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1161
        public void typesInferred(InferenceContext inferenceContext) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1162
            stuckVars.clear();
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1163
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1164
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1165
        @Override
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1166
        public void visitLambda(JCLambda tree) {
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1167
            if (inferenceContext.inferenceVars().contains(pt)) {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1168
                stuckVars.add(pt);
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1169
            }
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15039
diff changeset
  1170
            if (!types.isFunctionalInterface(pt)) {
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1171
                return;
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1172
            }
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1173
            Type descType = types.findDescriptorType(pt);
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1174
            List<Type> freeArgVars = inferenceContext.freeVarsIn(descType.getParameterTypes());
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15039
diff changeset
  1175
            if (tree.paramKind == JCLambda.ParameterKind.IMPLICIT &&
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1176
                    freeArgVars.nonEmpty()) {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1177
                stuckVars.addAll(freeArgVars);
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1178
                depVars.addAll(inferenceContext.freeVarsIn(descType.getReturnType()));
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1179
            }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1180
            scanLambdaBody(tree, descType.getReturnType());
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1181
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1182
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1183
        @Override
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1184
        public void visitReference(JCMemberReference tree) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1185
            scan(tree.expr);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1186
            if (inferenceContext.inferenceVars().contains(pt)) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1187
                stuckVars.add(pt);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1188
                return;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1189
            }
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15039
diff changeset
  1190
            if (!types.isFunctionalInterface(pt)) {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1191
                return;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1192
            }
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14538
diff changeset
  1193
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1194
            Type descType = types.findDescriptorType(pt);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1195
            List<Type> freeArgVars = inferenceContext.freeVarsIn(descType.getParameterTypes());
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1196
            if (freeArgVars.nonEmpty() &&
51721
96b76dca2be8 8210483: AssertionError in DeferredAttr at setOverloadKind caused by JDK-8203679
cushon
parents: 51563
diff changeset
  1197
                    tree.getOverloadKind() != JCMemberReference.OverloadKind.UNOVERLOADED) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1198
                stuckVars.addAll(freeArgVars);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1199
                depVars.addAll(inferenceContext.freeVarsIn(descType.getReturnType()));
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18902
diff changeset
  1200
            }
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1201
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1202
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1203
        void scanLambdaBody(JCLambda lambda, final Type pt) {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1204
            if (lambda.getBodyKind() == JCTree.JCLambda.BodyKind.EXPRESSION) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1205
                Type prevPt = this.pt;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1206
                try {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1207
                    this.pt = pt;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1208
                    scan(lambda.body);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1209
                } finally {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1210
                    this.pt = prevPt;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1211
                }
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1212
            } else {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1213
                LambdaReturnScanner lambdaScanner = new LambdaReturnScanner() {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1214
                    @Override
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1215
                    public void visitReturn(JCReturn tree) {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1216
                        if (tree.expr != null) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1217
                            Type prevPt = CheckStuckPolicy.this.pt;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1218
                            try {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1219
                                CheckStuckPolicy.this.pt = pt;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1220
                                CheckStuckPolicy.this.scan(tree.expr);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1221
                            } finally {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1222
                                CheckStuckPolicy.this.pt = prevPt;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1223
                            }
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1224
                        }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1225
                    }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1226
                };
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
  1227
                lambdaScanner.scan(lambda.body);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
  1228
            }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
  1229
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
  1230
    }
16975
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1231
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1232
    /**
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1233
     * This visitor is used to check that structural expressions conform
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1234
     * to their target - this step is required as inference could end up
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1235
     * inferring types that make some of the nested expressions incompatible
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1236
     * with their corresponding instantiated target
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1237
     */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1238
    class OverloadStuckPolicy extends CheckStuckPolicy implements DeferredStuckPolicy {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1239
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1240
        boolean stuck;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1241
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1242
        @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1243
        public boolean isStuck() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1244
            return super.isStuck() || stuck;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1245
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1246
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1247
        public OverloadStuckPolicy(ResultInfo resultInfo, DeferredType dt) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1248
            super(resultInfo, dt);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1249
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1250
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1251
        @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1252
        public void visitLambda(JCLambda tree) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1253
            super.visitLambda(tree);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1254
            if (tree.paramKind == JCLambda.ParameterKind.IMPLICIT) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1255
                stuck = true;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1256
            }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1257
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1258
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1259
        @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1260
        public void visitReference(JCMemberReference tree) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1261
            super.visitReference(tree);
51721
96b76dca2be8 8210483: AssertionError in DeferredAttr at setOverloadKind caused by JDK-8203679
cushon
parents: 51563
diff changeset
  1262
            if (tree.getOverloadKind() != JCMemberReference.OverloadKind.UNOVERLOADED) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1263
                stuck = true;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1264
            }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1265
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1266
    }
57963
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55622
diff changeset
  1267
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55622
diff changeset
  1268
    /**
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55622
diff changeset
  1269
     * Mode of attribution (used in AttrContext).
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55622
diff changeset
  1270
     */
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55622
diff changeset
  1271
    enum AttributionMode {
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55622
diff changeset
  1272
        /**Normal, non-speculative, attribution.*/
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55622
diff changeset
  1273
        FULL(false),
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55622
diff changeset
  1274
        /**Speculative attribution on behalf of an Analyzer.*/
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55622
diff changeset
  1275
        ANALYZER(true),
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55622
diff changeset
  1276
        /**Speculative attribution.*/
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55622
diff changeset
  1277
        SPECULATIVE(true);
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55622
diff changeset
  1278
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55622
diff changeset
  1279
        AttributionMode(boolean isSpeculative) {
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55622
diff changeset
  1280
            this.isSpeculative = isSpeculative;
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55622
diff changeset
  1281
        }
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55622
diff changeset
  1282
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55622
diff changeset
  1283
        boolean isSpeculative() {
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55622
diff changeset
  1284
            return isSpeculative;
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55622
diff changeset
  1285
        }
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55622
diff changeset
  1286
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55622
diff changeset
  1287
        final boolean isSpeculative;
ed7eb20871c5 8230105: -XDfind=diamond crashes
jlahoda
parents: 55622
diff changeset
  1288
    }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
  1289
}