src/jdk.compiler/share/classes/com/sun/tools/javac/comp/MemberEnter.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 55589 852dcb52bace
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
53275
f5e601ad26a8 8215482: check for cycles in type variables can provoke NPE
vromero
parents: 52796
diff changeset
     2
 * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4877
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4877
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4877
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4877
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4877
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.comp;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
30064
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
    28
import java.util.EnumSet;
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
    29
import java.util.Set;
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
    30
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import com.sun.tools.javac.code.*;
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25007
diff changeset
    32
import com.sun.tools.javac.code.Scope.WriteableScope;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.tools.javac.tree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import com.sun.tools.javac.util.*;
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
    35
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 47268
diff changeset
    36
import com.sun.tools.javac.util.JCDiagnostic.Error;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
24895
dd091d389fbf 8027262: Determine location for type annotations earlier in compiler pipeline
emc
parents: 24604
diff changeset
    38
import com.sun.tools.javac.code.Symbol.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import com.sun.tools.javac.code.Type.*;
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 45500
diff changeset
    40
import com.sun.tools.javac.resources.CompilerProperties.Errors;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import com.sun.tools.javac.tree.JCTree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import static com.sun.tools.javac.code.Flags.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
import static com.sun.tools.javac.code.Kinds.*;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26781
diff changeset
    45
import static com.sun.tools.javac.code.Kinds.Kind.*;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14259
diff changeset
    46
import static com.sun.tools.javac.code.TypeTag.TYPEVAR;
24604
7f68545b5128 8041422: Split javac ClassReader into ClassReader+ClassFinder
jjg
parents: 24396
diff changeset
    47
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
    48
/** Resolves field, method and constructor header, and constructs corresponding Symbols.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    50
 *  <p><b>This is NOT part of any supported API.
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    51
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
 */
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27854
diff changeset
    55
public class MemberEnter extends JCTree.Visitor {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21498
diff changeset
    56
    protected static final Context.Key<MemberEnter> memberEnterKey = new Context.Key<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    private final Enter enter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    private final Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    private final Check chk;
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    private final Attr attr;
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    private final Symtab syms;
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    private final Annotate annotate;
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    private final Types types;
8236
0d8646b7c602 6594914: @SuppressWarnings("deprecation") does not not work for the type of a variable
mcimadamore
parents: 8225
diff changeset
    65
    private final DeferredLintHandler deferredLintHandler;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    public static MemberEnter instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        MemberEnter instance = context.get(memberEnterKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
            instance = new MemberEnter(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    protected MemberEnter(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
        context.put(memberEnterKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
        enter = Enter.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        log = Log.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        chk = Check.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        attr = Attr.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        syms = Symtab.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        annotate = Annotate.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        types = Types.instance(context);
8236
0d8646b7c602 6594914: @SuppressWarnings("deprecation") does not not work for the type of a variable
mcimadamore
parents: 8225
diff changeset
    83
        deferredLintHandler = DeferredLintHandler.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
    /** Construct method type from method signature.
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
     *  @param typarams    The method's type parameters.
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
     *  @param params      The method's value parameters.
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
     *  @param res             The method's result type,
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
     *                 null if it is a constructor.
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14443
diff changeset
    91
     *  @param recvparam       The method's receiver parameter,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14443
diff changeset
    92
     *                 null if none given; TODO: or already set here?
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
     *  @param thrown      The method's thrown exceptions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
     *  @param env             The method's (local) environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
     */
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
    96
    Type signature(MethodSymbol msym,
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
    97
                   List<JCTypeParameter> typarams,
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
    98
                   List<JCVariableDecl> params,
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
    99
                   JCTree res,
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
   100
                   JCVariableDecl recvparam,
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
   101
                   List<JCExpression> thrown,
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
   102
                   Env<AttrContext> env) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        // Enter and attribute type parameters.
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        List<Type> tvars = enter.classEnter(typarams, env);
53275
f5e601ad26a8 8215482: check for cycles in type variables can provoke NPE
vromero
parents: 52796
diff changeset
   106
        attr.attribTypeVariables(typarams, env, true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
   108
        // Enter and attribute value parameters.
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21498
diff changeset
   109
        ListBuffer<Type> argbuf = new ListBuffer<>();
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
   110
        for (List<JCVariableDecl> l = params; l.nonEmpty(); l = l.tail) {
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
   111
            memberEnter(l.head, env);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
            argbuf.append(l.head.vartype.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        // Attribute result type, if one is given.
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
   116
        Type restype = res == null ? syms.voidType : attr.attribType(res, env);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14443
diff changeset
   118
        // Attribute receiver type, if one is given.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14443
diff changeset
   119
        Type recvtype;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14443
diff changeset
   120
        if (recvparam!=null) {
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
   121
            memberEnter(recvparam, env);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14443
diff changeset
   122
            recvtype = recvparam.vartype.type;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14443
diff changeset
   123
        } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14443
diff changeset
   124
            recvtype = null;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14443
diff changeset
   125
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14443
diff changeset
   126
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        // Attribute thrown exceptions.
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 21498
diff changeset
   128
        ListBuffer<Type> thrownbuf = new ListBuffer<>();
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
   129
        for (List<JCExpression> l = thrown; l.nonEmpty(); l = l.tail) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
            Type exc = attr.attribType(l.head, env);
18909
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18643
diff changeset
   131
            if (!exc.hasTag(TYPEVAR)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
                exc = chk.checkClassType(l.head.pos(), exc);
18909
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18643
diff changeset
   133
            } else if (exc.tsym.owner == msym) {
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18643
diff changeset
   134
                //mark inference variables in 'throws' clause
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18643
diff changeset
   135
                exc.tsym.flags_field |= THROWS;
8f9fc5d876e4 8012242: Lambda compatibility and checked exceptions
mcimadamore
parents: 18643
diff changeset
   136
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
            thrownbuf.append(exc);
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
        }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14443
diff changeset
   139
        MethodType mtype = new MethodType(argbuf.toList(),
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
                                    restype,
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
                                    thrownbuf.toList(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
                                    syms.methodClass);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14443
diff changeset
   143
        mtype.recvtype = recvtype;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 14443
diff changeset
   144
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        return tvars.isEmpty() ? mtype : new ForAll(tvars, mtype);
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
/* ********************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
 * Visitor methods for member enter
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
 *********************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
    /** Visitor argument: the current environment
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
    protected Env<AttrContext> env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
    /** Enter field and method definitions and process import
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
     *  clauses, catching any completion failure exceptions.
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
     */
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
   159
    protected void memberEnter(JCTree tree, Env<AttrContext> env) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
        Env<AttrContext> prevEnv = this.env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
            this.env = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
            tree.accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
        }  catch (CompletionFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
            chk.completionError(tree.pos(), ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
            this.env = prevEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
    /** Enter members from a list of trees.
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
     */
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
   173
    void memberEnter(List<? extends JCTree> trees, Env<AttrContext> env) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail)
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
   175
            memberEnter(l.head, env);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
    public void visitMethodDef(JCMethodDecl tree) {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25007
diff changeset
   179
        WriteableScope enclScope = enter.enterScope(env);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
        MethodSymbol m = new MethodSymbol(0, tree.name, null, enclScope.owner);
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        m.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, m, tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
        tree.sym = m;
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
   183
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
   184
        //if this is a default method, add the DEFAULT flag to the enclosing interface
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
   185
        if ((tree.mods.flags & DEFAULT) != 0) {
51764
d883f528689d 8193561: Cyclic hierarchy causes a NullPointerException when setting DEFAULT flag
jlahoda
parents: 50278
diff changeset
   186
            m.owner.flags_field |= DEFAULT;
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
   187
        }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
   188
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
        Env<AttrContext> localEnv = methodEnv(tree, env);
27231
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   190
        DiagnosticPosition prevLintPos = deferredLintHandler.setPos(tree.pos());
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
   191
        try {
27231
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   192
            // Compute the method type
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   193
            m.type = signature(m, tree.typarams, tree.params,
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   194
                               tree.restype, tree.recvparam,
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   195
                               tree.thrown,
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   196
                               localEnv);
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   197
        } finally {
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   198
            deferredLintHandler.setPos(prevLintPos);
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   199
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
27231
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   201
        if (types.isSignaturePolymorphic(m)) {
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   202
            m.flags_field |= SIGNATURE_POLYMORPHIC;
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   203
        }
18389
a425d0819f36 8016569: javac, add new flag for polymorphic method signatures
vromero
parents: 18010
diff changeset
   204
27231
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   205
        // Set m.params
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   206
        ListBuffer<VarSymbol> params = new ListBuffer<>();
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   207
        JCVariableDecl lastParam = null;
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   208
        for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) {
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   209
            JCVariableDecl param = lastParam = l.head;
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   210
            params.append(Assert.checkNonNull(param.sym));
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   211
        }
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   212
        m.params = params.toList();
21498
58c2486db8d0 8006732: support correct bytecode storage of type annotations in multicatch
emc
parents: 21040
diff changeset
   213
27231
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   214
        // mark the method varargs, if necessary
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   215
        if (lastParam != null && (lastParam.mods.flags & Flags.VARARGS) != 0)
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   216
            m.flags_field |= Flags.VARARGS;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
27231
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   218
        localEnv.info.scope.leave();
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   219
        if (chk.checkUnique(tree.pos(), m, enclScope)) {
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   220
        enclScope.enter(m);
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   221
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
27231
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   223
        annotate.annotateLater(tree.mods.annotations, localEnv, m, tree.pos());
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   224
        // Visit the signature of the method. Note that
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   225
        // TypeAnnotate doesn't descend into the body.
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 28590
diff changeset
   226
        annotate.queueScanTreeAndTypeAnnotate(tree, localEnv, m, tree.pos());
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
   227
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 28590
diff changeset
   228
        if (tree.defaultValue != null) {
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 28590
diff changeset
   229
            m.defaultValue = annotate.unfinishedDefaultValue(); // set it to temporary sentinel for now
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 28590
diff changeset
   230
            annotate.annotateDefaultValueLater(tree.defaultValue, localEnv, m, tree.pos());
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 28590
diff changeset
   231
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
    /** Create a fresh environment for method bodies.
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
     *  @param tree     The method definition.
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
     *  @param env      The environment current outside of the method definition.
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
    Env<AttrContext> methodEnv(JCMethodDecl tree, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
        Env<AttrContext> localEnv =
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25007
diff changeset
   240
            env.dup(tree, env.info.dup(env.info.scope.dupUnshared(tree.sym)));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
        localEnv.enclMethod = tree;
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   242
        if (tree.sym.type != null) {
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   243
            //when this is called in the enter stage, there's no type to be set
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26781
diff changeset
   244
            localEnv.info.returnResult = attr.new ResultInfo(KindSelector.VAL,
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26781
diff changeset
   245
                                                             tree.sym.type.getReturnType());
14057
b4b0377b8dba 7177387: Add target-typing support in method context
mcimadamore
parents: 14051
diff changeset
   246
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
        if ((tree.mods.flags & STATIC) != 0) localEnv.info.staticLevel++;
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 53275
diff changeset
   248
        localEnv.info.yieldResult = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
        return localEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
    public void visitVarDef(JCVariableDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
        Env<AttrContext> localEnv = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
        if ((tree.mods.flags & STATIC) != 0 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
            (env.info.scope.owner.flags() & INTERFACE) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
            localEnv = env.dup(tree, env.info.dup());
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
            localEnv.info.staticLevel++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
        }
19942
e9dae0e41075 8021112: Spurious unchecked warning reported by javac
jlahoda
parents: 19941
diff changeset
   259
        DiagnosticPosition prevLintPos = deferredLintHandler.setPos(tree.pos());
29842
826ac2519523 8031744: Annotations on many Language Model elements are not returned
jjg
parents: 28590
diff changeset
   260
8236
0d8646b7c602 6594914: @SuppressWarnings("deprecation") does not not work for the type of a variable
mcimadamore
parents: 8225
diff changeset
   261
        try {
27231
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   262
            if (TreeInfo.isEnumInit(tree)) {
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   263
                attr.attribIdentAsEnumType(localEnv, (JCIdent)tree.vartype);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   264
            } else if (!tree.isImplicitlyTyped()) {
27231
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   265
                attr.attribType(tree.vartype, localEnv);
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   266
                if (TreeInfo.isReceiverParam(tree))
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   267
                    checkReceiver(tree, localEnv);
21498
58c2486db8d0 8006732: support correct bytecode storage of type annotations in multicatch
emc
parents: 21040
diff changeset
   268
            }
27231
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   269
        } finally {
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   270
            deferredLintHandler.setPos(prevLintPos);
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   271
        }
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   272
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   273
        if ((tree.mods.flags & VARARGS) != 0) {
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   274
            //if we are entering a varargs parameter, we need to
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   275
            //replace its type (a plain array type) with the more
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   276
            //precise VarargsType --- we need to do it this way
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   277
            //because varargs is represented in the tree as a
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   278
            //modifier on the parameter declaration, and not as a
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   279
            //distinct type of array node.
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   280
            ArrayType atype = (ArrayType)tree.vartype.type;
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   281
            tree.vartype.type = atype.makeVarargs();
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   282
        }
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   283
        WriteableScope enclScope = enter.enterScope(env);
50278
a8e77041c69f 8202178: type.getKind() for var is None instead of Error
sdama
parents: 48721
diff changeset
   284
        Type vartype = tree.isImplicitlyTyped()
a8e77041c69f 8202178: type.getKind() for var is None instead of Error
sdama
parents: 48721
diff changeset
   285
                ? env.info.scope.owner.kind == MTH ? Type.noType : syms.errType
a8e77041c69f 8202178: type.getKind() for var is None instead of Error
sdama
parents: 48721
diff changeset
   286
                : tree.vartype.type;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   287
        VarSymbol v = new VarSymbol(0, tree.name, vartype, enclScope.owner);
27231
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   288
        v.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, v, tree);
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   289
        tree.sym = v;
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   290
        if (tree.init != null) {
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   291
            v.flags_field |= HASINIT;
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   292
            if ((v.flags_field & FINAL) != 0 &&
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   293
                needsLazyConstValue(tree.init)) {
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   294
                Env<AttrContext> initEnv = getInitEnv(tree, env);
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   295
                initEnv.info.enclVar = v;
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   296
                v.setLazyConstValue(initEnv(tree, initEnv), attr, tree);
12916
021c069e8e27 7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents: 12335
diff changeset
   297
            }
27231
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   298
        }
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   299
        if (chk.checkUnique(tree.pos(), v, enclScope)) {
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   300
            chk.checkTransparentVar(tree.pos(), v, enclScope);
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   301
            enclScope.enter(v);
55589
852dcb52bace 8227010: Error recovery after local variable redeclaration can be improved
jlahoda
parents: 55306
diff changeset
   302
        } else if (v.owner.kind == MTH) {
852dcb52bace 8227010: Error recovery after local variable redeclaration can be improved
jlahoda
parents: 55306
diff changeset
   303
            enclScope.enter(v);
27231
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   304
        }
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
   305
27231
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   306
        annotate.annotateLater(tree.mods.annotations, localEnv, v, tree.pos());
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   307
        if (!tree.isImplicitlyTyped()) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   308
            annotate.queueScanTreeAndTypeAnnotate(tree.vartype, localEnv, v, tree.pos());
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47216
diff changeset
   309
        }
26532
aa84b6606229 8056021: checkin for JDK-8027262 breaks Checker Framework
jfranck
parents: 26266
diff changeset
   310
27231
c1ca668b421e 8054448: (ann) Cannot reference field of inner class in an anonymous class
jfranck
parents: 27224
diff changeset
   311
        v.pos = tree.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
    }
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16558
diff changeset
   313
    // where
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 47268
diff changeset
   314
    void checkType(JCTree tree, Type type, Error errorKey) {
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16558
diff changeset
   315
        if (!tree.type.isErroneous() && !types.isSameType(tree.type, type)) {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 47268
diff changeset
   316
            log.error(tree, errorKey);
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16558
diff changeset
   317
        }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16558
diff changeset
   318
    }
24903
41e1812d6b4c 8027886: javac allows illegal receiver parameters
pgovereau
parents: 24895
diff changeset
   319
    void checkReceiver(JCVariableDecl tree, Env<AttrContext> localEnv) {
41e1812d6b4c 8027886: javac allows illegal receiver parameters
pgovereau
parents: 24895
diff changeset
   320
        attr.attribExpr(tree.nameexpr, localEnv);
41e1812d6b4c 8027886: javac allows illegal receiver parameters
pgovereau
parents: 24895
diff changeset
   321
        MethodSymbol m = localEnv.enclMethod.sym;
41e1812d6b4c 8027886: javac allows illegal receiver parameters
pgovereau
parents: 24895
diff changeset
   322
        if (m.isConstructor()) {
41e1812d6b4c 8027886: javac allows illegal receiver parameters
pgovereau
parents: 24895
diff changeset
   323
            Type outertype = m.owner.owner.type;
41e1812d6b4c 8027886: javac allows illegal receiver parameters
pgovereau
parents: 24895
diff changeset
   324
            if (outertype.hasTag(TypeTag.METHOD)) {
41e1812d6b4c 8027886: javac allows illegal receiver parameters
pgovereau
parents: 24895
diff changeset
   325
                // we have a local inner class
41e1812d6b4c 8027886: javac allows illegal receiver parameters
pgovereau
parents: 24895
diff changeset
   326
                outertype = m.owner.owner.owner.type;
41e1812d6b4c 8027886: javac allows illegal receiver parameters
pgovereau
parents: 24895
diff changeset
   327
            }
41e1812d6b4c 8027886: javac allows illegal receiver parameters
pgovereau
parents: 24895
diff changeset
   328
            if (outertype.hasTag(TypeTag.CLASS)) {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 47268
diff changeset
   329
                checkType(tree.vartype, outertype, Errors.IncorrectConstructorReceiverType(outertype, tree.vartype.type));
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 47268
diff changeset
   330
                checkType(tree.nameexpr, outertype, Errors.IncorrectConstructorReceiverName(outertype, tree.nameexpr.type));
24903
41e1812d6b4c 8027886: javac allows illegal receiver parameters
pgovereau
parents: 24895
diff changeset
   331
            } else {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 45500
diff changeset
   332
                log.error(tree, Errors.ReceiverParameterNotApplicableConstructorToplevelClass);
24903
41e1812d6b4c 8027886: javac allows illegal receiver parameters
pgovereau
parents: 24895
diff changeset
   333
            }
41e1812d6b4c 8027886: javac allows illegal receiver parameters
pgovereau
parents: 24895
diff changeset
   334
        } else {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 47268
diff changeset
   335
            checkType(tree.vartype, m.owner.type, Errors.IncorrectReceiverType(m.owner.type, tree.vartype.type));
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 47268
diff changeset
   336
            checkType(tree.nameexpr, m.owner.type, Errors.IncorrectReceiverName(m.owner.type, tree.nameexpr.type));
24903
41e1812d6b4c 8027886: javac allows illegal receiver parameters
pgovereau
parents: 24895
diff changeset
   337
        }
41e1812d6b4c 8027886: javac allows illegal receiver parameters
pgovereau
parents: 24895
diff changeset
   338
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
19656
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   340
    public boolean needsLazyConstValue(JCTree tree) {
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   341
        InitTreeVisitor initTreeVisitor = new InitTreeVisitor();
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   342
        tree.accept(initTreeVisitor);
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   343
        return initTreeVisitor.result;
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   344
    }
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   345
30064
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   346
    /** Visitor class for expressions which might be constant expressions,
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   347
     *  as per JLS 15.28 (Constant Expressions).
19656
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   348
     */
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   349
    static class InitTreeVisitor extends JCTree.Visitor {
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   350
30064
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   351
        private static final Set<Tag> ALLOWED_OPERATORS =
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   352
                EnumSet.of(Tag.POS, Tag.NEG, Tag.NOT, Tag.COMPL, Tag.PLUS, Tag.MINUS,
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   353
                           Tag.MUL, Tag.DIV, Tag.MOD, Tag.SL, Tag.SR, Tag.USR,
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   354
                           Tag.LT, Tag.LE, Tag.GT, Tag.GE, Tag.EQ, Tag.NE,
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   355
                           Tag.BITAND, Tag.BITXOR, Tag.BITOR, Tag.AND, Tag.OR);
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   356
45500
365640343c55 8181464: Invalid lambda in annotation causes NPE in Lint.augment
vromero
parents: 37744
diff changeset
   357
        boolean result = true;
19656
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   358
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   359
        @Override
30064
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   360
        public void visitTree(JCTree tree) {
19656
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   361
            result = false;
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   362
        }
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   363
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   364
        @Override
30064
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   365
        public void visitLiteral(JCLiteral that) {}
19942
e9dae0e41075 8021112: Spurious unchecked warning reported by javac
jlahoda
parents: 19941
diff changeset
   366
e9dae0e41075 8021112: Spurious unchecked warning reported by javac
jlahoda
parents: 19941
diff changeset
   367
        @Override
30064
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   368
        public void visitTypeCast(JCTypeCast tree) {
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   369
            tree.expr.accept(this);
19656
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   370
        }
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   371
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   372
        @Override
30064
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   373
        public void visitUnary(JCUnary that) {
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   374
            if (!ALLOWED_OPERATORS.contains(that.getTag())) {
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   375
                result = false;
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   376
                return ;
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   377
            }
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   378
            that.arg.accept(this);
19656
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   379
        }
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   380
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   381
        @Override
30064
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   382
        public void visitBinary(JCBinary that) {
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   383
            if (!ALLOWED_OPERATORS.contains(that.getTag())) {
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   384
                result = false;
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   385
                return ;
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   386
            }
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   387
            that.lhs.accept(this);
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   388
            that.rhs.accept(this);
19656
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   389
        }
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   390
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   391
        @Override
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   392
        public void visitConditional(JCConditional tree) {
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   393
            tree.cond.accept(this);
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   394
            tree.truepart.accept(this);
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   395
            tree.falsepart.accept(this);
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   396
        }
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   397
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   398
        @Override
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   399
        public void visitParens(JCParens tree) {
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   400
            tree.expr.accept(this);
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   401
        }
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   402
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   403
        @Override
30064
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   404
        public void visitIdent(JCIdent that) {}
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   405
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   406
        @Override
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   407
        public void visitSelect(JCFieldAccess tree) {
39493809b601 8077605: Initializing static fields causes unbounded recursion in javac
jlahoda
parents: 29842
diff changeset
   408
            tree.selected.accept(this);
19656
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   409
        }
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   410
    }
7f0afbdbf142 8023112: javac should not use lazy constant evaluation approach for method references
vromero
parents: 19651
diff changeset
   411
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
    /** Create a fresh environment for a variable's initializer.
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
     *  If the variable is a field, the owner of the environment's scope
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
     *  is be the variable itself, otherwise the owner is the method
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
     *  enclosing the variable definition.
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
     *  @param tree     The variable definition.
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
     *  @param env      The environment current outside of the variable definition.
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
    Env<AttrContext> initEnv(JCVariableDecl tree, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
        Env<AttrContext> localEnv = env.dupto(new AttrContextEnv(tree, env.info.dup()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
        if (tree.sym.owner.kind == TYP) {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25007
diff changeset
   423
            localEnv.info.scope = env.info.scope.dupUnshared(tree.sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
        if ((tree.mods.flags & STATIC) != 0 ||
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents: 14369
diff changeset
   426
                ((env.enclClass.sym.flags() & INTERFACE) != 0 && env.enclMethod == null))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
            localEnv.info.staticLevel++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
        return localEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
    /** Default member enter visitor method: do nothing
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
    public void visitTree(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
    public void visitErroneous(JCErroneous tree) {
6929
81a6a7c1bef1 6988407: javac crashes running processor on errant code; it used to print error message
jjg
parents: 6721
diff changeset
   437
        if (tree.errs != null)
81a6a7c1bef1 6988407: javac crashes running processor on errant code; it used to print error message
jjg
parents: 6721
diff changeset
   438
            memberEnter(tree.errs, env);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
    public Env<AttrContext> getMethodEnv(JCMethodDecl tree, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
        Env<AttrContext> mEnv = methodEnv(tree, env);
18010
604faee85350 8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents: 17999
diff changeset
   443
        mEnv.info.lint = mEnv.info.lint.augment(tree.sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
        for (List<JCTypeParameter> l = tree.typarams; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
            mEnv.info.scope.enterIfAbsent(l.head.type.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
        for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
            mEnv.info.scope.enterIfAbsent(l.head.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
        return mEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
    public Env<AttrContext> getInitEnv(JCVariableDecl tree, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
        Env<AttrContext> iEnv = initEnv(tree, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
        return iEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
}