langtools/src/share/classes/com/sun/tools/javac/comp/DeferredAttr.java
author briangoetz
Wed, 18 Dec 2013 16:05:18 -0500
changeset 22163 3651128c74eb
parent 21718 74a5882faf79
child 24066 1dfb66929538
permissions -rw-r--r--
8030244: Update langtools to use Diamond Reviewed-by: darcy
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
/*
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16566
diff changeset
     2
 * Copyright (c) 2012, 2013, 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
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
    28
import com.sun.source.tree.MemberReferenceTree;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    29
import com.sun.tools.javac.code.*;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    30
import com.sun.tools.javac.tree.*;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    31
import com.sun.tools.javac.util.*;
16805
b419336382fd 8010922: Cleanup: add support for ad-hoc method check logic
mcimadamore
parents: 16566
diff changeset
    32
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    33
import com.sun.tools.javac.code.Symbol.*;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    34
import com.sun.tools.javac.code.Type.*;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    35
import com.sun.tools.javac.comp.Attr.ResultInfo;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    36
import com.sun.tools.javac.comp.Infer.InferenceContext;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    37
import com.sun.tools.javac.comp.Resolve.MethodResolutionPhase;
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18902
diff changeset
    38
import com.sun.tools.javac.comp.Resolve.ReferenceLookupHelper;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    39
import com.sun.tools.javac.tree.JCTree.*;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    40
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    41
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    42
import java.util.ArrayList;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
    43
import java.util.Collections;
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
    44
import java.util.EnumSet;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
    45
import java.util.LinkedHashMap;
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14538
diff changeset
    46
import java.util.LinkedHashSet;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    47
import java.util.Map;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    48
import java.util.Set;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    49
import java.util.WeakHashMap;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    50
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14538
diff changeset
    51
import static com.sun.tools.javac.code.TypeTag.*;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    52
import static com.sun.tools.javac.tree.JCTree.Tag.*;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    53
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    54
/**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    55
 * 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
    56
 * 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
    57
 * with a temporary 'deferred type' that is checked (possibly multiple times)
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    58
 * against an expected formal type.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    59
 *
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    60
 *  <p><b>This is NOT part of any supported API.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    61
 *  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
    62
 *  This code and its internal interfaces are subject to change or
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    63
 *  deletion without notice.</b>
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    64
 */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    65
public class DeferredAttr extends JCTree.Visitor {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21718
diff changeset
    66
    protected static final Context.Key<DeferredAttr> deferredAttrKey = new Context.Key<>();
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    67
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    68
    final Attr attr;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    69
    final Check chk;
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15039
diff changeset
    70
    final JCDiagnostic.Factory diags;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    71
    final Enter enter;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    72
    final Infer infer;
16294
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 15717
diff changeset
    73
    final Resolve rs;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    74
    final Log log;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    75
    final Symtab syms;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    76
    final TreeMaker make;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    77
    final Types types;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    78
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    79
    public static DeferredAttr instance(Context context) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    80
        DeferredAttr instance = context.get(deferredAttrKey);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    81
        if (instance == null)
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    82
            instance = new DeferredAttr(context);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    83
        return instance;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    84
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    85
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    86
    protected DeferredAttr(Context context) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    87
        context.put(deferredAttrKey, this);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    88
        attr = Attr.instance(context);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    89
        chk = Check.instance(context);
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15039
diff changeset
    90
        diags = JCDiagnostic.Factory.instance(context);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    91
        enter = Enter.instance(context);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    92
        infer = Infer.instance(context);
16294
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 15717
diff changeset
    93
        rs = Resolve.instance(context);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    94
        log = Log.instance(context);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    95
        syms = Symtab.instance(context);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    96
        make = TreeMaker.instance(context);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
    97
        types = Types.instance(context);
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15039
diff changeset
    98
        Names names = Names.instance(context);
18902
972298345a83 8016059: Cannot compile following lambda
mcimadamore
parents: 18901
diff changeset
    99
        stuckTree = make.Ident(names.empty).setType(Type.stuckType);
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18902
diff changeset
   100
        emptyDeferredAttrContext =
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18902
diff changeset
   101
            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
   102
                @Override
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   103
                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
   104
                    Assert.error("Empty deferred context!");
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18902
diff changeset
   105
                }
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18902
diff changeset
   106
                @Override
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18902
diff changeset
   107
                void complete() {
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18902
diff changeset
   108
                    Assert.error("Empty deferred context!");
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18902
diff changeset
   109
                }
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18902
diff changeset
   110
            };
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   111
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   112
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15039
diff changeset
   113
    /** shared tree for stuck expressions */
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15039
diff changeset
   114
    final JCTree stuckTree;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15039
diff changeset
   115
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   116
    /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   117
     * 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
   118
     * 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
   119
     * discovered through type-checking the deferred type against a target-type.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   120
     * 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
   121
     */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   122
    public class DeferredType extends Type {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   123
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   124
        public JCExpression tree;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   125
        Env<AttrContext> env;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   126
        AttrMode mode;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   127
        SpeculativeCache speculativeCache;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   128
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   129
        DeferredType(JCExpression tree, Env<AttrContext> env) {
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18643
diff changeset
   130
            super(null);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   131
            this.tree = tree;
18912
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18910
diff changeset
   132
            this.env = attr.copyEnv(env);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   133
            this.speculativeCache = new SpeculativeCache();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   134
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   135
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18643
diff changeset
   136
        @Override
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18643
diff changeset
   137
        public TypeTag getTag() {
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18643
diff changeset
   138
            return DEFERRED;
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18643
diff changeset
   139
        }
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 18643
diff changeset
   140
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   141
        /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   142
         * 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
   143
         * that triggered speculative attribution on a given deferred type. Each entry
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   144
         * 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
   145
         * has been added.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   146
         */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   147
        class SpeculativeCache {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   148
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21718
diff changeset
   149
            private Map<Symbol, List<Entry>> cache = new WeakHashMap<>();
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   150
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   151
            class Entry {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   152
                JCTree speculativeTree;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   153
                ResultInfo resultInfo;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   154
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   155
                public Entry(JCTree speculativeTree, ResultInfo resultInfo) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   156
                    this.speculativeTree = speculativeTree;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   157
                    this.resultInfo = resultInfo;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   158
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   159
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   160
                boolean matches(MethodResolutionPhase phase) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   161
                    return resultInfo.checkContext.deferredAttrContext().phase == phase;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   162
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   163
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   164
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   165
            /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   166
             * Retrieve a speculative cache entry corresponding to given symbol
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   167
             * and resolution phase
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   168
             */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   169
            Entry get(Symbol msym, MethodResolutionPhase phase) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   170
                List<Entry> entries = cache.get(msym);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   171
                if (entries == null) return null;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   172
                for (Entry e : entries) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   173
                    if (e.matches(phase)) return e;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   174
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   175
                return null;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   176
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   177
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   178
            /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   179
             * Stores a speculative cache entry corresponding to given symbol
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   180
             * and resolution phase
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   181
             */
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   182
            void put(JCTree speculativeTree, ResultInfo resultInfo) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   183
                Symbol msym = resultInfo.checkContext.deferredAttrContext().msym;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   184
                List<Entry> entries = cache.get(msym);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   185
                if (entries == null) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   186
                    entries = List.nil();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   187
                }
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   188
                cache.put(msym, entries.prepend(new Entry(speculativeTree, resultInfo)));
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   189
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   190
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   191
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   192
        /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   193
         * 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
   194
         */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   195
        Type speculativeType(Symbol msym, MethodResolutionPhase phase) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   196
            SpeculativeCache.Entry e = speculativeCache.get(msym, phase);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   197
            return e != null ? e.speculativeTree.type : Type.noType;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   198
        }
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
        /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   201
         * Check a deferred type against a potential target-type. Depending on
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   202
         * the current attribution mode, a normal vs. speculative attribution
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   203
         * 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
   204
         * speculative round for a given target method symbol; moreover, a normal
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   205
         * attribution round must follow one or more speculative rounds.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   206
         */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   207
        Type check(ResultInfo resultInfo) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   208
            DeferredStuckPolicy deferredStuckPolicy;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   209
            if (resultInfo.pt.hasTag(NONE) || resultInfo.pt.isErroneous()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   210
                deferredStuckPolicy = dummyStuckPolicy;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   211
            } else if (resultInfo.checkContext.deferredAttrContext().mode == AttrMode.SPECULATIVE) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   212
                deferredStuckPolicy = new OverloadStuckPolicy(resultInfo, this);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   213
            } else {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   214
                deferredStuckPolicy = new CheckStuckPolicy(resultInfo, this);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   215
            }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   216
            return check(resultInfo, deferredStuckPolicy, basicCompleter);
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   217
        }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   218
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   219
        private Type check(ResultInfo resultInfo, DeferredStuckPolicy deferredStuckPolicy,
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   220
                DeferredTypeCompleter deferredTypeCompleter) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   221
            DeferredAttrContext deferredAttrContext =
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   222
                    resultInfo.checkContext.deferredAttrContext();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   223
            Assert.check(deferredAttrContext != emptyDeferredAttrContext);
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   224
            if (deferredStuckPolicy.isStuck()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   225
                deferredAttrContext.addDeferredAttrNode(this, resultInfo, deferredStuckPolicy);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   226
                return Type.noType;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   227
            } else {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   228
                try {
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   229
                    return deferredTypeCompleter.complete(this, resultInfo, deferredAttrContext);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   230
                } finally {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   231
                    mode = deferredAttrContext.mode;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   232
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   233
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   234
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   235
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   236
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   237
    /**
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   238
     * 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
   239
     * a deferred type.
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   240
     */
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   241
    interface DeferredTypeCompleter {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   242
        /**
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   243
         * 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
   244
         * circumstances, type-checking could amount to full attribution
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   245
         * or partial structural check (aka potential applicability).
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   246
         */
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   247
        Type complete(DeferredType dt, ResultInfo resultInfo, DeferredAttrContext deferredAttrContext);
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   248
    }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   249
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   250
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   251
    /**
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   252
     * 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
   253
     * 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
   254
     * or speculative attribution.
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   255
     */
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   256
    DeferredTypeCompleter basicCompleter = new DeferredTypeCompleter() {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   257
        public Type complete(DeferredType dt, ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   258
            switch (deferredAttrContext.mode) {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   259
                case SPECULATIVE:
16566
604ce45d5f23 8009820: AssertionError when compiling java code with two identical static imports
mcimadamore
parents: 16326
diff changeset
   260
                    //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
   261
                    //speculative rounds...
604ce45d5f23 8009820: AssertionError when compiling java code with two identical static imports
mcimadamore
parents: 16326
diff changeset
   262
                    Assert.check(dt.mode == null || dt.mode == AttrMode.SPECULATIVE);
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   263
                    JCTree speculativeTree = attribSpeculative(dt.tree, dt.env, resultInfo);
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   264
                    dt.speculativeCache.put(speculativeTree, resultInfo);
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   265
                    return speculativeTree.type;
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   266
                case CHECK:
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   267
                    Assert.check(dt.mode != null);
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   268
                    return attr.attribTree(dt.tree, dt.env, resultInfo);
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   269
            }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   270
            Assert.error();
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   271
            return null;
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   272
        }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   273
    };
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   274
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   275
    DeferredTypeCompleter dummyCompleter = new DeferredTypeCompleter() {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   276
        public Type complete(DeferredType dt, ResultInfo resultInfo, DeferredAttrContext deferredAttrContext) {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   277
            Assert.check(deferredAttrContext.mode == AttrMode.CHECK);
18912
e25cd61d8e59 8020147: Spurious errors when compiling nested stuck lambdas
mcimadamore
parents: 18910
diff changeset
   278
            return dt.tree.type = Type.stuckType;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   279
        }
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   280
    };
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   281
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   282
    /**
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   283
     * Policy for detecting stuck expressions. Different criteria might cause
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   284
     * an expression to be judged as stuck, depending on whether the check
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   285
     * is performed during overload resolution or after most specific.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   286
     */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   287
    interface DeferredStuckPolicy {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   288
        /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   289
         * Has the policy detected that a given expression should be considered stuck?
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   290
         */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   291
        boolean isStuck();
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   292
        /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   293
         * Get the set of inference variables a given expression depends upon.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   294
         */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   295
        Set<Type> stuckVars();
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   296
        /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   297
         * Get the set of inference variables which might get new constraints
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   298
         * if a given expression is being type-checked.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   299
         */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   300
        Set<Type> depVars();
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   301
    }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   302
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   303
    /**
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   304
     * Basic stuck policy; an expression is never considered to be stuck.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   305
     */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   306
    DeferredStuckPolicy dummyStuckPolicy = new DeferredStuckPolicy() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   307
        @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   308
        public boolean isStuck() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   309
            return false;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   310
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   311
        @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   312
        public Set<Type> stuckVars() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   313
            return Collections.emptySet();
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   314
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   315
        @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   316
        public Set<Type> depVars() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   317
            return Collections.emptySet();
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   318
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   319
    };
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   320
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   321
    /**
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   322
     * 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
   323
     */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   324
    public enum AttrMode {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   325
        /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   326
         * A speculative type-checking round is used during overload resolution
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   327
         * mainly to generate constraints on inference variables. Side-effects
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   328
         * arising from type-checking the expression associated with the deferred
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   329
         * type are reversed after the speculative round finishes. This means the
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   330
         * expression tree will be left in a blank state.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   331
         */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   332
        SPECULATIVE,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   333
        /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   334
         * 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
   335
         */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21718
diff changeset
   336
        CHECK
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   337
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   338
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   339
    /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   340
     * Routine that performs speculative type-checking; the input AST node is
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   341
     * 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
   342
     * restored after type-checking. All diagnostics (but critical ones) are
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   343
     * disabled during speculative type-checking.
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
    JCTree attribSpeculative(JCTree tree, Env<AttrContext> env, ResultInfo resultInfo) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21718
diff changeset
   346
        final JCTree newTree = new TreeCopier<>(make).copy(tree);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   347
        Env<AttrContext> speculativeEnv = env.dup(newTree, env.info.dup(env.info.scope.dupUnshared()));
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   348
        speculativeEnv.info.scope.owner = env.info.scope.owner;
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14359
diff changeset
   349
        Log.DeferredDiagnosticHandler deferredDiagnosticHandler =
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14359
diff changeset
   350
                new Log.DeferredDiagnosticHandler(log, new Filter<JCDiagnostic>() {
16309
db71f8ecc136 8008276: assertion error in com.sun.tools.javac.comp.TransTypes.visitApply
mcimadamore
parents: 16294
diff changeset
   351
            public boolean accepts(final JCDiagnostic d) {
db71f8ecc136 8008276: assertion error in com.sun.tools.javac.comp.TransTypes.visitApply
mcimadamore
parents: 16294
diff changeset
   352
                class PosScanner extends TreeScanner {
db71f8ecc136 8008276: assertion error in com.sun.tools.javac.comp.TransTypes.visitApply
mcimadamore
parents: 16294
diff changeset
   353
                    boolean found = false;
db71f8ecc136 8008276: assertion error in com.sun.tools.javac.comp.TransTypes.visitApply
mcimadamore
parents: 16294
diff changeset
   354
db71f8ecc136 8008276: assertion error in com.sun.tools.javac.comp.TransTypes.visitApply
mcimadamore
parents: 16294
diff changeset
   355
                    @Override
db71f8ecc136 8008276: assertion error in com.sun.tools.javac.comp.TransTypes.visitApply
mcimadamore
parents: 16294
diff changeset
   356
                    public void scan(JCTree tree) {
db71f8ecc136 8008276: assertion error in com.sun.tools.javac.comp.TransTypes.visitApply
mcimadamore
parents: 16294
diff changeset
   357
                        if (tree != null &&
db71f8ecc136 8008276: assertion error in com.sun.tools.javac.comp.TransTypes.visitApply
mcimadamore
parents: 16294
diff changeset
   358
                                tree.pos() == d.getDiagnosticPosition()) {
db71f8ecc136 8008276: assertion error in com.sun.tools.javac.comp.TransTypes.visitApply
mcimadamore
parents: 16294
diff changeset
   359
                            found = true;
db71f8ecc136 8008276: assertion error in com.sun.tools.javac.comp.TransTypes.visitApply
mcimadamore
parents: 16294
diff changeset
   360
                        }
db71f8ecc136 8008276: assertion error in com.sun.tools.javac.comp.TransTypes.visitApply
mcimadamore
parents: 16294
diff changeset
   361
                        super.scan(tree);
db71f8ecc136 8008276: assertion error in com.sun.tools.javac.comp.TransTypes.visitApply
mcimadamore
parents: 16294
diff changeset
   362
                    }
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21718
diff changeset
   363
                }
16309
db71f8ecc136 8008276: assertion error in com.sun.tools.javac.comp.TransTypes.visitApply
mcimadamore
parents: 16294
diff changeset
   364
                PosScanner posScanner = new PosScanner();
db71f8ecc136 8008276: assertion error in com.sun.tools.javac.comp.TransTypes.visitApply
mcimadamore
parents: 16294
diff changeset
   365
                posScanner.scan(newTree);
db71f8ecc136 8008276: assertion error in com.sun.tools.javac.comp.TransTypes.visitApply
mcimadamore
parents: 16294
diff changeset
   366
                return posScanner.found;
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14359
diff changeset
   367
            }
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14359
diff changeset
   368
        });
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   369
        try {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   370
            attr.attribTree(newTree, speculativeEnv, resultInfo);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   371
            unenterScanner.scan(newTree);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   372
            return newTree;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   373
        } finally {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   374
            unenterScanner.scan(newTree);
14538
384681be798f 8003299: Cleanup javac Log support for deferred diagnostics
jjg
parents: 14359
diff changeset
   375
            log.popDiagnosticHandler(deferredDiagnosticHandler);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   376
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   377
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   378
    //where
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   379
        protected TreeScanner unenterScanner = new TreeScanner() {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   380
            @Override
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   381
            public void visitClassDef(JCClassDecl tree) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   382
                ClassSymbol csym = tree.sym;
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14538
diff changeset
   383
                //if something went wrong during method applicability check
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14538
diff changeset
   384
                //it is possible that nested expressions inside argument expression
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14538
diff changeset
   385
                //are left unchecked - in such cases there's nothing to clean up.
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14538
diff changeset
   386
                if (csym == null) return;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   387
                enter.typeEnvs.remove(csym);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   388
                chk.compiled.remove(csym.flatname);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   389
                syms.classes.remove(csym.flatname);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   390
                super.visitClassDef(tree);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   391
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   392
        };
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   393
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   394
    /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   395
     * 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
   396
     * 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
   397
     * the symbol of the method being checked, the overload resolution phase,
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   398
     * 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
   399
     * 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
   400
     * 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
   401
     * routine makes sure that all pending nodes are properly processed, by
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   402
     * progressively instantiating all inference variables on which one or more
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   403
     * deferred attribution node is stuck.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   404
     */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   405
    class DeferredAttrContext {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   406
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   407
        /** attribution mode */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   408
        final AttrMode mode;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   409
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   410
        /** symbol of the method being checked */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   411
        final Symbol msym;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   412
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   413
        /** method resolution step */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   414
        final Resolve.MethodResolutionPhase phase;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   415
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   416
        /** inference context */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   417
        final InferenceContext inferenceContext;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   418
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   419
        /** parent deferred context */
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   420
        final DeferredAttrContext parent;
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   421
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   422
        /** Warner object to report warnings */
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   423
        final Warner warn;
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   424
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   425
        /** list of deferred attribution nodes to be processed */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21718
diff changeset
   426
        ArrayList<DeferredAttrNode> deferredAttrNodes = new ArrayList<>();
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   427
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   428
        DeferredAttrContext(AttrMode mode, Symbol msym, MethodResolutionPhase phase,
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   429
                InferenceContext inferenceContext, DeferredAttrContext parent, Warner warn) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   430
            this.mode = mode;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   431
            this.msym = msym;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   432
            this.phase = phase;
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   433
            this.parent = parent;
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   434
            this.warn = warn;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   435
            this.inferenceContext = inferenceContext;
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
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   438
        /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   439
         * 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
   440
         * 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
   441
         */
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   442
        void addDeferredAttrNode(final DeferredType dt, ResultInfo resultInfo,
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   443
                DeferredStuckPolicy deferredStuckPolicy) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   444
            deferredAttrNodes.add(new DeferredAttrNode(dt, resultInfo, deferredStuckPolicy));
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   445
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   446
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   447
        /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   448
         * Incrementally process all nodes, by skipping 'stuck' nodes and attributing
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   449
         * '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
   450
         * some inference variable might get eagerly instantiated so that all nodes
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   451
         * can be type-checked.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   452
         */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   453
        void complete() {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   454
            while (!deferredAttrNodes.isEmpty()) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21718
diff changeset
   455
                Map<Type, Set<Type>> depVarsMap = new LinkedHashMap<>();
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   456
                List<Type> stuckVars = List.nil();
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   457
                boolean progress = false;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   458
                //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
   459
                //attribution round can add new nodes to the list
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   460
                for (DeferredAttrNode deferredAttrNode : List.from(deferredAttrNodes)) {
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   461
                    if (!deferredAttrNode.process(this)) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   462
                        List<Type> restStuckVars =
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   463
                                List.from(deferredAttrNode.deferredStuckPolicy.stuckVars())
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   464
                                .intersect(inferenceContext.restvars());
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   465
                        stuckVars = stuckVars.prependList(restStuckVars);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   466
                        //update dependency map
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   467
                        for (Type t : List.from(deferredAttrNode.deferredStuckPolicy.depVars())
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   468
                                .intersect(inferenceContext.restvars())) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   469
                            Set<Type> prevDeps = depVarsMap.get(t);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   470
                            if (prevDeps == null) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21718
diff changeset
   471
                                prevDeps = new LinkedHashSet<>();
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   472
                                depVarsMap.put(t, prevDeps);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   473
                            }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   474
                            prevDeps.addAll(restStuckVars);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   475
                        }
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15039
diff changeset
   476
                    } else {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   477
                        deferredAttrNodes.remove(deferredAttrNode);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   478
                        progress = true;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   479
                    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   480
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   481
                if (!progress) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   482
                    DeferredAttrContext dac = this;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   483
                    while (dac != emptyDeferredAttrContext) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   484
                        if (dac.mode == AttrMode.SPECULATIVE) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   485
                            //unsticking does not take place during overload
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   486
                            break;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   487
                        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   488
                        dac = dac.parent;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   489
                    }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   490
                    //remove all variables that have already been instantiated
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   491
                    //from the list of stuck variables
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   492
                    try {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   493
                        inferenceContext.solveAny(stuckVars, depVarsMap, warn);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   494
                        inferenceContext.notifyChange();
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   495
                    } catch (Infer.GraphStrategy.NodeNotFoundException ex) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   496
                        //this means that we are in speculative mode and the
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   497
                        //set of contraints are too tight for progess to be made.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   498
                        //Just leave the remaining expressions as stuck.
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   499
                        break;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   500
                    }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   501
                }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   502
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   503
        }
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   504
    }
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   505
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   506
    /**
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   507
     * 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
   508
     * 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
   509
     */
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   510
    class DeferredAttrNode {
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   511
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   512
        /** underlying deferred type */
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   513
        DeferredType dt;
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   514
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   515
        /** underlying target type information */
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   516
        ResultInfo resultInfo;
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   517
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   518
        /** stuck policy associated with this node */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   519
        DeferredStuckPolicy deferredStuckPolicy;
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   520
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   521
        DeferredAttrNode(DeferredType dt, ResultInfo resultInfo, DeferredStuckPolicy deferredStuckPolicy) {
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   522
            this.dt = dt;
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   523
            this.resultInfo = resultInfo;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   524
            this.deferredStuckPolicy = deferredStuckPolicy;
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   525
        }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   526
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   527
        /**
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   528
         * Process a deferred attribution node.
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   529
         * Invariant: a stuck node cannot be processed.
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   530
         */
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   531
        @SuppressWarnings("fallthrough")
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   532
        boolean process(final DeferredAttrContext deferredAttrContext) {
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   533
            switch (deferredAttrContext.mode) {
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   534
                case SPECULATIVE:
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   535
                    if (deferredStuckPolicy.isStuck()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   536
                        dt.check(resultInfo, dummyStuckPolicy, new StructuralStuckChecker());
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   537
                        return true;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   538
                    } else {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   539
                        Assert.error("Cannot get here");
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   540
                    }
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   541
                case CHECK:
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   542
                    if (deferredStuckPolicy.isStuck()) {
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   543
                        //stuck expression - see if we can propagate
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   544
                        if (deferredAttrContext.parent != emptyDeferredAttrContext &&
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   545
                                Type.containsAny(deferredAttrContext.parent.inferenceContext.inferencevars,
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   546
                                        List.from(deferredStuckPolicy.stuckVars()))) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   547
                            deferredAttrContext.parent.addDeferredAttrNode(dt,
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   548
                                    resultInfo.dup(new Check.NestedCheckContext(resultInfo.checkContext) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   549
                                @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   550
                                public InferenceContext inferenceContext() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   551
                                    return deferredAttrContext.parent.inferenceContext;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   552
                                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   553
                                @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   554
                                public DeferredAttrContext deferredAttrContext() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   555
                                    return deferredAttrContext.parent;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   556
                                }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   557
                            }), deferredStuckPolicy);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   558
                            dt.tree.type = Type.stuckType;
15717
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   559
                            return true;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   560
                        } else {
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   561
                            return false;
ab55670d2e62 8007464: Add graph inference support
mcimadamore
parents: 15706
diff changeset
   562
                        }
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   563
                    } else {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   564
                        ResultInfo instResultInfo =
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   565
                                resultInfo.dup(deferredAttrContext.inferenceContext.asInstType(resultInfo.pt));
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   566
                        dt.check(instResultInfo, dummyStuckPolicy, basicCompleter);
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   567
                        return true;
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   568
                    }
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   569
                default:
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   570
                    throw new AssertionError("Bad mode");
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   571
            }
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   572
        }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   573
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   574
        /**
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   575
         * Structural checker for stuck expressions
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   576
         */
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   577
        class StructuralStuckChecker extends TreeScanner implements DeferredTypeCompleter {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   578
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   579
            ResultInfo resultInfo;
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   580
            InferenceContext inferenceContext;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   581
            Env<AttrContext> env;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   582
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   583
            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
   584
                this.resultInfo = resultInfo;
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   585
                this.inferenceContext = deferredAttrContext.inferenceContext;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   586
                this.env = dt.env;
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   587
                dt.tree.accept(this);
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   588
                dt.speculativeCache.put(stuckTree, resultInfo);
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   589
                return Type.noType;
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   590
            }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   591
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   592
            @Override
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   593
            public void visitLambda(JCLambda tree) {
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   594
                Check.CheckContext checkContext = resultInfo.checkContext;
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   595
                Type pt = resultInfo.pt;
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   596
                if (inferenceContext.inferencevars.contains(pt)) {
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   597
                    //ok
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   598
                    return;
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   599
                } else {
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   600
                    //must be a functional descriptor
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   601
                    try {
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   602
                        Type desc = types.findDescriptorType(pt);
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   603
                        if (desc.getParameterTypes().length() != tree.params.length()) {
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   604
                            checkContext.report(tree, diags.fragment("incompatible.arg.types.in.lambda"));
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   605
                        }
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   606
                    } catch (Types.FunctionDescriptorLookupError ex) {
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   607
                        checkContext.report(null, ex.getDiagnostic());
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   608
                    }
14057
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
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   611
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   612
            @Override
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   613
            public void visitNewClass(JCNewClass tree) {
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   614
                //do nothing
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   615
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   616
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   617
            @Override
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   618
            public void visitApply(JCMethodInvocation tree) {
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   619
                //do nothing
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   620
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   621
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   622
            @Override
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   623
            public void visitReference(JCMemberReference tree) {
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   624
                Check.CheckContext checkContext = resultInfo.checkContext;
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   625
                Type pt = resultInfo.pt;
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   626
                if (inferenceContext.inferencevars.contains(pt)) {
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   627
                    //ok
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   628
                    return;
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   629
                } else {
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   630
                    try {
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   631
                        types.findDescriptorType(pt);
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   632
                    } catch (Types.FunctionDescriptorLookupError ex) {
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   633
                        checkContext.report(null, ex.getDiagnostic());
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15039
diff changeset
   634
                    }
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   635
                    Env<AttrContext> localEnv = env.dup(tree);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   636
                    JCExpression exprTree = (JCExpression)attribSpeculative(tree.getQualifierExpression(), localEnv,
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   637
                            attr.memberReferenceQualifierResult(tree));
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19914
diff changeset
   638
                    ListBuffer<Type> argtypes = new ListBuffer<>();
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   639
                    for (Type t : types.findDescriptorType(pt).getParameterTypes()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   640
                        argtypes.append(Type.noType);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   641
                    }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   642
                    JCMemberReference mref2 = new TreeCopier<Void>(make).copy(tree);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   643
                    mref2.expr = exprTree;
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 20249
diff changeset
   644
                    Symbol lookupSym =
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 20249
diff changeset
   645
                            rs.resolveMemberReferenceByArity(localEnv, mref2, exprTree.type,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 20249
diff changeset
   646
                                tree.name, argtypes.toList(), inferenceContext);
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 20249
diff changeset
   647
                    switch (lookupSym.kind) {
16294
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 15717
diff changeset
   648
                        //note: as argtypes are erroneous types, type-errors must
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 15717
diff changeset
   649
                        //have been caused by arity mismatch
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 15717
diff changeset
   650
                        case Kinds.ABSENT_MTH:
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 15717
diff changeset
   651
                        case Kinds.WRONG_MTH:
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 15717
diff changeset
   652
                        case Kinds.WRONG_MTHS:
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 20249
diff changeset
   653
                        case Kinds.WRONG_STATICNESS:
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   654
                           checkContext.report(tree, diags.fragment("incompatible.arg.types.in.mref"));
16294
0c291a3cd60d 8007462: Fix provisional applicability for method references
mcimadamore
parents: 15717
diff changeset
   655
                    }
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15039
diff changeset
   656
                }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   657
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   658
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   659
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   660
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   661
    /** 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
   662
    final DeferredAttrContext emptyDeferredAttrContext;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   663
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   664
    /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   665
     * 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
   666
     * 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
   667
     * 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
   668
     * 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
   669
     */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   670
    class DeferredTypeMap extends Type.Mapping {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   671
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   672
        DeferredAttrContext deferredAttrContext;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   673
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   674
        protected DeferredTypeMap(AttrMode mode, Symbol msym, MethodResolutionPhase phase) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   675
            super(String.format("deferredTypeMap[%s]", mode));
15706
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   676
            this.deferredAttrContext = new DeferredAttrContext(mode, msym, phase,
37a81a6987ac 8007479: Refactor DeferredAttrContext so that it points to parent context
mcimadamore
parents: 15705
diff changeset
   677
                    infer.emptyContext, emptyDeferredAttrContext, types.noWarnings);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   678
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   679
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   680
        @Override
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   681
        public Type apply(Type t) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14258
diff changeset
   682
            if (!t.hasTag(DEFERRED)) {
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   683
                return t.map(this);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   684
            } else {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   685
                DeferredType dt = (DeferredType)t;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   686
                return typeOf(dt);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   687
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   688
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   689
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   690
        protected Type typeOf(DeferredType dt) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   691
            switch (deferredAttrContext.mode) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   692
                case CHECK:
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   693
                    return dt.tree.type == null ? Type.noType : dt.tree.type;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   694
                case SPECULATIVE:
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   695
                    return dt.speculativeType(deferredAttrContext.msym, deferredAttrContext.phase);
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   696
            }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   697
            Assert.error();
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   698
            return null;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   699
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   700
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   701
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   702
    /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   703
     * Specialized recovery deferred mapping.
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   704
     * 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
   705
     * (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
   706
     * 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
   707
     * (the latter step is useful in a recovery scenario).
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   708
     */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   709
    public class RecoveryDeferredTypeMap extends DeferredTypeMap {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   710
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   711
        public RecoveryDeferredTypeMap(AttrMode mode, Symbol msym, MethodResolutionPhase phase) {
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14538
diff changeset
   712
            super(mode, msym, phase != null ? phase : MethodResolutionPhase.BOX);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   713
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   714
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   715
        @Override
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   716
        protected Type typeOf(DeferredType dt) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   717
            Type owntype = super.typeOf(dt);
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14538
diff changeset
   718
            return owntype == Type.noType ?
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   719
                        recover(dt) : owntype;
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   720
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   721
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   722
        /**
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   723
         * 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
   724
         * reduced to an ordinary type. Functional deferred types and conditionals
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   725
         * are mapped to themselves, in order to have a richer diagnostic
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   726
         * representation. Remaining deferred types are attributed using
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   727
         * a default expected type (j.l.Object).
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   728
         */
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   729
        private Type recover(DeferredType dt) {
18902
972298345a83 8016059: Cannot compile following lambda
mcimadamore
parents: 18901
diff changeset
   730
            dt.check(attr.new RecoveryInfo(deferredAttrContext) {
972298345a83 8016059: Cannot compile following lambda
mcimadamore
parents: 18901
diff changeset
   731
                @Override
972298345a83 8016059: Cannot compile following lambda
mcimadamore
parents: 18901
diff changeset
   732
                protected Type check(DiagnosticPosition pos, Type found) {
972298345a83 8016059: Cannot compile following lambda
mcimadamore
parents: 18901
diff changeset
   733
                    return chk.checkNonVoid(pos, super.check(pos, found));
972298345a83 8016059: Cannot compile following lambda
mcimadamore
parents: 18901
diff changeset
   734
                }
972298345a83 8016059: Cannot compile following lambda
mcimadamore
parents: 18901
diff changeset
   735
            });
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14538
diff changeset
   736
            return super.apply(dt);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   737
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   738
    }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   739
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   740
    /**
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   741
     * 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
   742
     * 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
   743
     */
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   744
    abstract static class FilterScanner extends TreeScanner {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   745
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   746
        final Filter<JCTree> treeFilter;
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   747
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   748
        FilterScanner(final Set<JCTree.Tag> validTags) {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   749
            this.treeFilter = new Filter<JCTree>() {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   750
                public boolean accepts(JCTree t) {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   751
                    return validTags.contains(t.getTag());
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   752
                }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   753
            };
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   754
        }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   755
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   756
        @Override
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   757
        public void scan(JCTree tree) {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   758
            if (tree != null) {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   759
                if (treeFilter.accepts(tree)) {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   760
                    super.scan(tree);
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   761
                } else {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   762
                    skip(tree);
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   763
                }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   764
            }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   765
        }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   766
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   767
        /**
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   768
         * 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
   769
         */
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   770
        abstract void skip(JCTree tree);
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   771
    }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   772
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   773
    /**
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   774
     * 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
   775
     * a given argument expression.
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   776
     */
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   777
    static class PolyScanner extends FilterScanner {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   778
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   779
        PolyScanner() {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   780
            super(EnumSet.of(CONDEXPR, PARENS, LAMBDA, REFERENCE));
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   781
        }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   782
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   783
        @Override
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   784
        void skip(JCTree tree) {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   785
            //do nothing
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   786
        }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   787
    }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   788
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   789
    /**
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   790
     * 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
   791
     * within a lambda expression body.
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   792
     */
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   793
    static class LambdaReturnScanner extends FilterScanner {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   794
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   795
        LambdaReturnScanner() {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   796
            super(EnumSet.of(BLOCK, CASE, CATCH, DOLOOP, FOREACHLOOP,
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   797
                    FORLOOP, RETURN, SYNCHRONIZED, SWITCH, TRY, WHILELOOP));
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   798
        }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   799
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   800
        @Override
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   801
        void skip(JCTree tree) {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   802
            //do nothing
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   803
        }
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   804
    }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   805
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   806
    /**
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   807
     * This visitor is used to check that structural expressions conform
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   808
     * 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
   809
     * inferring types that make some of the nested expressions incompatible
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   810
     * with their corresponding instantiated target
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   811
     */
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   812
    class CheckStuckPolicy extends PolyScanner implements DeferredStuckPolicy, Infer.FreeTypeListener {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   813
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   814
        Type pt;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   815
        Infer.InferenceContext inferenceContext;
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21718
diff changeset
   816
        Set<Type> stuckVars = new LinkedHashSet<>();
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21718
diff changeset
   817
        Set<Type> depVars = new LinkedHashSet<>();
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   818
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   819
        @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   820
        public boolean isStuck() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   821
            return !stuckVars.isEmpty();
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   822
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   823
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   824
        @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   825
        public Set<Type> stuckVars() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   826
            return stuckVars;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   827
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   828
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   829
        @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   830
        public Set<Type> depVars() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   831
            return depVars;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   832
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   833
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   834
        public CheckStuckPolicy(ResultInfo resultInfo, DeferredType dt) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   835
            this.pt = resultInfo.pt;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   836
            this.inferenceContext = resultInfo.checkContext.inferenceContext();
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   837
            scan(dt.tree);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   838
            if (!stuckVars.isEmpty()) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   839
                resultInfo.checkContext.inferenceContext()
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   840
                        .addFreeTypeListener(List.from(stuckVars), this);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   841
            }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   842
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   843
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   844
        @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   845
        public void typesInferred(InferenceContext inferenceContext) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   846
            stuckVars.clear();
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   847
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   848
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   849
        @Override
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   850
        public void visitLambda(JCLambda tree) {
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   851
            if (inferenceContext.inferenceVars().contains(pt)) {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   852
                stuckVars.add(pt);
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   853
            }
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15039
diff changeset
   854
            if (!types.isFunctionalInterface(pt)) {
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   855
                return;
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   856
            }
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   857
            Type descType = types.findDescriptorType(pt);
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   858
            List<Type> freeArgVars = inferenceContext.freeVarsIn(descType.getParameterTypes());
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15039
diff changeset
   859
            if (tree.paramKind == JCLambda.ParameterKind.IMPLICIT &&
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   860
                    freeArgVars.nonEmpty()) {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   861
                stuckVars.addAll(freeArgVars);
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   862
                depVars.addAll(inferenceContext.freeVarsIn(descType.getReturnType()));
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   863
            }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   864
            scanLambdaBody(tree, descType.getReturnType());
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   865
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   866
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   867
        @Override
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   868
        public void visitReference(JCMemberReference tree) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   869
            scan(tree.expr);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   870
            if (inferenceContext.inferenceVars().contains(pt)) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   871
                stuckVars.add(pt);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   872
                return;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   873
            }
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 15039
diff changeset
   874
            if (!types.isFunctionalInterface(pt)) {
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   875
                return;
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   876
            }
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14538
diff changeset
   877
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   878
            Type descType = types.findDescriptorType(pt);
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   879
            List<Type> freeArgVars = inferenceContext.freeVarsIn(descType.getParameterTypes());
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   880
            if (freeArgVars.nonEmpty() &&
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   881
                    tree.overloadKind == JCMemberReference.OverloadKind.OVERLOADED) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   882
                stuckVars.addAll(freeArgVars);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   883
                depVars.addAll(inferenceContext.freeVarsIn(descType.getReturnType()));
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18902
diff changeset
   884
            }
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   885
        }
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   886
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   887
        void scanLambdaBody(JCLambda lambda, final Type pt) {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   888
            if (lambda.getBodyKind() == JCTree.JCLambda.BodyKind.EXPRESSION) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   889
                Type prevPt = this.pt;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   890
                try {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   891
                    this.pt = pt;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   892
                    scan(lambda.body);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   893
                } finally {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   894
                    this.pt = prevPt;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   895
                }
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   896
            } else {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   897
                LambdaReturnScanner lambdaScanner = new LambdaReturnScanner() {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   898
                    @Override
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   899
                    public void visitReturn(JCReturn tree) {
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   900
                        if (tree.expr != null) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   901
                            Type prevPt = CheckStuckPolicy.this.pt;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   902
                            try {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   903
                                CheckStuckPolicy.this.pt = pt;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   904
                                CheckStuckPolicy.this.scan(tree.expr);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   905
                            } finally {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   906
                                CheckStuckPolicy.this.pt = prevPt;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   907
                            }
15039
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   908
                        }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   909
                    }
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   910
                };
80190ab051c0 8005184: Restructure DeferredAttr to allow pluggable deferred type completers
mcimadamore
parents: 14547
diff changeset
   911
                lambdaScanner.scan(lambda.body);
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   912
            }
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   913
        }
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
   914
    }
16975
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   915
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   916
    /**
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   917
     * This visitor is used to check that structural expressions conform
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   918
     * to their target - this step is required as inference could end up
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   919
     * inferring types that make some of the nested expressions incompatible
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   920
     * with their corresponding instantiated target
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   921
     */
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   922
    class OverloadStuckPolicy extends CheckStuckPolicy implements DeferredStuckPolicy {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   923
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   924
        boolean stuck;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   925
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   926
        @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   927
        public boolean isStuck() {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   928
            return super.isStuck() || stuck;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   929
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   930
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   931
        public OverloadStuckPolicy(ResultInfo resultInfo, DeferredType dt) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   932
            super(resultInfo, dt);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   933
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   934
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   935
        @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   936
        public void visitLambda(JCLambda tree) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   937
            super.visitLambda(tree);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   938
            if (tree.paramKind == JCLambda.ParameterKind.IMPLICIT) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   939
                stuck = true;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   940
            }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   941
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   942
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   943
        @Override
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   944
        public void visitReference(JCMemberReference tree) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   945
            super.visitReference(tree);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   946
            if (tree.overloadKind == JCMemberReference.OverloadKind.OVERLOADED) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   947
                stuck = true;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   948
            }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   949
        }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   950
    }
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   951
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
   952
    /**
16975
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   953
     * Does the argument expression {@code expr} need speculative type-checking?
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   954
     */
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   955
    boolean isDeferred(Env<AttrContext> env, JCExpression expr) {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   956
        DeferredChecker dc = new DeferredChecker(env);
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   957
        dc.scan(expr);
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   958
        return dc.result.isPoly();
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   959
    }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   960
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   961
    /**
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   962
     * The kind of an argument expression. This is used by the analysis that
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   963
     * determines as to whether speculative attribution is necessary.
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   964
     */
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   965
    enum ArgumentExpressionKind {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   966
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   967
        /** kind that denotes poly argument expression */
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   968
        POLY,
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   969
        /** kind that denotes a standalone expression */
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   970
        NO_POLY,
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   971
        /** kind that denotes a primitive/boxed standalone expression */
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   972
        PRIMITIVE;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   973
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   974
        /**
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   975
         * Does this kind denote a poly argument expression
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   976
         */
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   977
        public final boolean isPoly() {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   978
            return this == POLY;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   979
        }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   980
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   981
        /**
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   982
         * Does this kind denote a primitive standalone expression
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   983
         */
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   984
        public final boolean isPrimitive() {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   985
            return this == PRIMITIVE;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   986
        }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   987
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   988
        /**
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   989
         * Compute the kind of a standalone expression of a given type
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   990
         */
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   991
        static ArgumentExpressionKind standaloneKind(Type type, Types types) {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   992
            return types.unboxedTypeOrType(type).isPrimitive() ?
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   993
                    ArgumentExpressionKind.PRIMITIVE :
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   994
                    ArgumentExpressionKind.NO_POLY;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   995
        }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   996
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   997
        /**
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   998
         * Compute the kind of a method argument expression given its symbol
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
   999
         */
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1000
        static ArgumentExpressionKind methodKind(Symbol sym, Types types) {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1001
            Type restype = sym.type.getReturnType();
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1002
            if (sym.type.hasTag(FORALL) &&
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1003
                    restype.containsAny(((ForAll)sym.type).tvars)) {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1004
                return ArgumentExpressionKind.POLY;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1005
            } else {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1006
                return ArgumentExpressionKind.standaloneKind(restype, types);
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1007
            }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1008
        }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1009
    }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1010
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1011
    /**
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1012
     * Tree scanner used for checking as to whether an argument expression
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1013
     * requires speculative attribution
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1014
     */
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1015
    final class DeferredChecker extends FilterScanner {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1016
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1017
        Env<AttrContext> env;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1018
        ArgumentExpressionKind result;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1019
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1020
        public DeferredChecker(Env<AttrContext> env) {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1021
            super(deferredCheckerTags);
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1022
            this.env = env;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1023
        }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1024
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1025
        @Override
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1026
        public void visitLambda(JCLambda tree) {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1027
            //a lambda is always a poly expression
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1028
            result = ArgumentExpressionKind.POLY;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1029
        }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1030
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1031
        @Override
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1032
        public void visitReference(JCMemberReference tree) {
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1033
            //perform arity-based check
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1034
            Env<AttrContext> localEnv = env.dup(tree);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1035
            JCExpression exprTree = (JCExpression)attribSpeculative(tree.getQualifierExpression(), localEnv,
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1036
                    attr.memberReferenceQualifierResult(tree));
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1037
            JCMemberReference mref2 = new TreeCopier<Void>(make).copy(tree);
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1038
            mref2.expr = exprTree;
21718
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 20249
diff changeset
  1039
            Symbol res =
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 20249
diff changeset
  1040
                    rs.getMemberReference(tree, localEnv, mref2,
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 20249
diff changeset
  1041
                        exprTree.type, tree.name);
74a5882faf79 8026231: Look at 'static' flag when checking method references
vromero
parents: 20249
diff changeset
  1042
            tree.sym = res;
19914
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1043
            if (res.kind >= Kinds.ERRONEOUS ||
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1044
                    res.type.hasTag(FORALL) ||
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1045
                    (res.flags() & Flags.VARARGS) != 0 ||
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1046
                    (TreeInfo.isStaticSelector(exprTree, tree.name.table.names) &&
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1047
                    exprTree.type.isRaw())) {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1048
                tree.overloadKind = JCMemberReference.OverloadKind.OVERLOADED;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1049
            } else {
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1050
                tree.overloadKind = JCMemberReference.OverloadKind.UNOVERLOADED;
d86271bd430a 8016177: structural most specific and stuckness
vromero
parents: 18912
diff changeset
  1051
            }
16975
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1052
            //a method reference is always a poly expression
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1053
            result = ArgumentExpressionKind.POLY;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1054
        }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1055
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1056
        @Override
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1057
        public void visitTypeCast(JCTypeCast tree) {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1058
            //a cast is always a standalone expression
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1059
            result = ArgumentExpressionKind.NO_POLY;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1060
        }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1061
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1062
        @Override
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1063
        public void visitConditional(JCConditional tree) {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1064
            scan(tree.truepart);
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1065
            if (!result.isPrimitive()) {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1066
                result = ArgumentExpressionKind.POLY;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1067
                return;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1068
            }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1069
            scan(tree.falsepart);
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1070
            result = reduce(ArgumentExpressionKind.PRIMITIVE);
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1071
        }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1072
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1073
        @Override
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1074
        public void visitNewClass(JCNewClass tree) {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1075
            result = (TreeInfo.isDiamond(tree) || attr.findDiamonds) ?
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1076
                    ArgumentExpressionKind.POLY : ArgumentExpressionKind.NO_POLY;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1077
        }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1078
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1079
        @Override
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1080
        public void visitApply(JCMethodInvocation tree) {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1081
            Name name = TreeInfo.name(tree.meth);
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1082
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1083
            //fast path
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1084
            if (tree.typeargs.nonEmpty() ||
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1085
                    name == name.table.names._this ||
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1086
                    name == name.table.names._super) {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1087
                result = ArgumentExpressionKind.NO_POLY;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1088
                return;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1089
            }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1090
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1091
            //slow path
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1092
            final JCExpression rec = tree.meth.hasTag(SELECT) ?
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1093
                    ((JCFieldAccess)tree.meth).selected :
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1094
                    null;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1095
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1096
            if (rec != null && !isSimpleReceiver(rec)) {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1097
                //give up if receiver is too complex (to cut down analysis time)
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1098
                result = ArgumentExpressionKind.POLY;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1099
                return;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1100
            }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1101
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1102
            Type site = rec != null ?
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1103
                    attribSpeculative(rec, env, attr.unknownTypeExprInfo).type :
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1104
                    env.enclClass.sym.type;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1105
18901
8309dd8e14d9 8019480: Javac crashes when method is called on a type-variable receiver from lambda expression
mcimadamore
parents: 18646
diff changeset
  1106
            while (site.hasTag(TYPEVAR)) {
8309dd8e14d9 8019480: Javac crashes when method is called on a type-variable receiver from lambda expression
mcimadamore
parents: 18646
diff changeset
  1107
                site = site.getUpperBound();
8309dd8e14d9 8019480: Javac crashes when method is called on a type-variable receiver from lambda expression
mcimadamore
parents: 18646
diff changeset
  1108
            }
8309dd8e14d9 8019480: Javac crashes when method is called on a type-variable receiver from lambda expression
mcimadamore
parents: 18646
diff changeset
  1109
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18902
diff changeset
  1110
            List<Type> args = rs.dummyArgs(tree.args.length());
16975
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1111
18910
c967bfda9283 8016175: Add bottom-up type-checking support for unambiguous method references
mcimadamore
parents: 18902
diff changeset
  1112
            Resolve.LookupHelper lh = rs.new LookupHelper(name, site, args, List.<Type>nil(), MethodResolutionPhase.VARARITY) {
16975
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1113
                @Override
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1114
                Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1115
                    return rec == null ?
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1116
                        rs.findFun(env, name, argtypes, typeargtypes, phase.isBoxingRequired(), phase.isVarargsRequired()) :
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1117
                        rs.findMethod(env, site, name, argtypes, typeargtypes, phase.isBoxingRequired(), phase.isVarargsRequired(), false);
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1118
                }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1119
                @Override
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1120
                Symbol access(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, Symbol sym) {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1121
                    return sym;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1122
                }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1123
            };
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1124
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1125
            Symbol sym = rs.lookupMethod(env, tree, site.tsym, rs.arityMethodCheck, lh);
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1126
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1127
            if (sym.kind == Kinds.AMBIGUOUS) {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1128
                Resolve.AmbiguityError err = (Resolve.AmbiguityError)sym.baseSymbol();
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1129
                result = ArgumentExpressionKind.PRIMITIVE;
18643
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 16975
diff changeset
  1130
                for (Symbol s : err.ambiguousSyms) {
fdd7572e0c18 8016613: javac should avoid source 8 only analysis when compiling for source 7
vromero
parents: 16975
diff changeset
  1131
                    if (result.isPoly()) break;
16975
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1132
                    if (s.kind == Kinds.MTH) {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1133
                        result = reduce(ArgumentExpressionKind.methodKind(s, types));
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1134
                    }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1135
                }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1136
            } else {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1137
                result = (sym.kind == Kinds.MTH) ?
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1138
                    ArgumentExpressionKind.methodKind(sym, types) :
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1139
                    ArgumentExpressionKind.NO_POLY;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1140
            }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1141
        }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1142
        //where
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1143
            private boolean isSimpleReceiver(JCTree rec) {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1144
                switch (rec.getTag()) {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1145
                    case IDENT:
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1146
                        return true;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1147
                    case SELECT:
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1148
                        return isSimpleReceiver(((JCFieldAccess)rec).selected);
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1149
                    case TYPEAPPLY:
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1150
                    case TYPEARRAY:
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1151
                        return true;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1152
                    case ANNOTATED_TYPE:
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1153
                        return isSimpleReceiver(((JCAnnotatedType)rec).underlyingType);
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1154
                    default:
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1155
                        return false;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1156
                }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1157
            }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1158
            private ArgumentExpressionKind reduce(ArgumentExpressionKind kind) {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1159
                switch (result) {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1160
                    case PRIMITIVE: return kind;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1161
                    case NO_POLY: return kind.isPoly() ? kind : result;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1162
                    case POLY: return result;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1163
                    default:
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1164
                        Assert.error();
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1165
                        return null;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1166
                }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1167
            }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1168
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1169
        @Override
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1170
        public void visitLiteral(JCLiteral tree) {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1171
            Type litType = attr.litType(tree.typetag);
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1172
            result = ArgumentExpressionKind.standaloneKind(litType, types);
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1173
        }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1174
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1175
        @Override
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1176
        void skip(JCTree tree) {
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1177
            result = ArgumentExpressionKind.NO_POLY;
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1178
        }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1179
    }
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1180
    //where
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1181
    private EnumSet<JCTree.Tag> deferredCheckerTags =
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1182
            EnumSet.of(LAMBDA, REFERENCE, PARENS, TYPECAST,
124c8436d59c 8010923: Avoid redundant speculative attribution
mcimadamore
parents: 16805
diff changeset
  1183
                    CONDEXPR, NEWCLASS, APPLY, LITERAL);
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents:
diff changeset
  1184
}