langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Enter.java
author emc
Tue, 21 Oct 2014 09:01:51 -0400
changeset 27224 228abfa87080
parent 25874 83c19f00452c
child 27857 7e913a535736
permissions -rw-r--r--
8054457: Refactor Symbol kinds from small ints to an enum Summary: Replace bitmap logic in symbol.kind and pkind with an enum-based API Reviewed-by: mcimadamore, jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
22442
8fd30fc4e3a3 8029690: Move symbol creation from ClassReader to Symtab
jfranck
parents: 22163
diff changeset
     2
 * Copyright (c) 1999, 2014, 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: 5008
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: 5008
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: 5008
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5008
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5008
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
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import javax.tools.JavaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import javax.tools.JavaFileManager;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import com.sun.tools.javac.code.*;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 25874
diff changeset
    33
import com.sun.tools.javac.code.Kinds.KindSelector;
7615
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents: 6709
diff changeset
    34
import com.sun.tools.javac.code.Scope.*;
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents: 6709
diff changeset
    35
import com.sun.tools.javac.code.Symbol.*;
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents: 6709
diff changeset
    36
import com.sun.tools.javac.code.Type.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import com.sun.tools.javac.jvm.*;
11314
b612aaca08d0 7120736: refactor javac option handling
jjg
parents: 10950
diff changeset
    38
import com.sun.tools.javac.main.Option.PkgInfo;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import com.sun.tools.javac.tree.*;
7615
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents: 6709
diff changeset
    40
import com.sun.tools.javac.tree.JCTree.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import com.sun.tools.javac.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import com.sun.tools.javac.util.List;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
import static com.sun.tools.javac.code.Flags.*;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 25874
diff changeset
    47
import static com.sun.tools.javac.code.Kinds.Kind.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
/** This class enters symbols for all encountered definitions into
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 *  the symbol table. The pass consists of two phases, organized as
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
 *  follows:
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
 *
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 13844
diff changeset
    53
 *  <p>In the first phase, all class symbols are entered into their
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
 *  enclosing scope, descending recursively down the tree for classes
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
 *  which are members of other classes. The class symbols are given a
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
 *  MemberEnter object as completer.
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
 *  <p>In the second phase classes are completed using
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
 *  MemberEnter.complete().  Completion might occur on demand, but
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
 *  any classes that are not completed that way will be eventually
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
 *  completed by processing the `uncompleted' queue.  Completion
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
 *  entails (1) determination of a class's parameters, supertype and
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
 *  interfaces, as well as (2) entering all symbols defined in the
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
 *  class into its scope, with the exception of class symbols which
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
 *  have been entered in phase 1.  (2) depends on (1) having been
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
 *  completed for a class and all its superclasses and enclosing
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
 *  classes. That's why, after doing (1), we put classes in a
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
 *  `halfcompleted' queue. Only when we have performed (1) for a class
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
 *  and all it's superclasses and enclosing classes, we proceed to
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
 *  (2).
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
 *  <p>Whereas the first phase is organized as a sweep through all
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
 *  compiled syntax trees, the second phase is demand. Members of a
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
 *  class are entered when the contents of a class are first
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
 *  accessed. This is accomplished by installing completer objects in
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
 *  class symbols for compiled classes which invoke the member-enter
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
 *  phase for the corresponding class tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
 *  <p>Classes migrate from one phase to the next via queues:
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
 *
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13689
diff changeset
    81
 *  <pre>{@literal
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
 *  class enter -> (Enter.uncompleted)         --> member enter (1)
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
 *              -> (MemberEnter.halfcompleted) --> member enter (2)
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
 *              -> (Todo)                      --> attribute
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
 *                                              (only for toplevel classes)
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13689
diff changeset
    86
 *  }</pre>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    88
 *  <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
    89
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
public class Enter extends JCTree.Visitor {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20235
diff changeset
    94
    protected static final Context.Key<Enter> enterKey = new Context.Key<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
    Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    Symtab syms;
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    Check chk;
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
    TreeMaker make;
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    Annotate annotate;
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    MemberEnter memberEnter;
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1206
diff changeset
   102
    Types types;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    Lint lint;
4871
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 1264
diff changeset
   104
    Names names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    JavaFileManager fileManager;
6575
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 5847
diff changeset
   106
    PkgInfo pkginfoOpt;
25007
eb097d3a68f5 8038975: Access control in enhanced for
pgovereau
parents: 24604
diff changeset
   107
    TypeEnvs typeEnvs;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
    private final Todo todo;
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
    public static Enter instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
        Enter instance = context.get(enterKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
            instance = new Enter(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
    protected Enter(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        context.put(enterKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        log = Log.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        make = TreeMaker.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        syms = Symtab.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
        chk = Check.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        memberEnter = MemberEnter.instance(context);
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1206
diff changeset
   126
        types = Types.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        annotate = Annotate.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        lint = Lint.instance(context);
4871
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 1264
diff changeset
   129
        names = Names.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        predefClassDef = make.ClassDef(
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
            make.Modifiers(PUBLIC),
15354
52a04c670c05 8004834: Add doclint support into javadoc
jjg
parents: 14359
diff changeset
   133
            syms.predefClass.name,
52a04c670c05 8004834: Add doclint support into javadoc
jjg
parents: 14359
diff changeset
   134
            List.<JCTypeParameter>nil(),
52a04c670c05 8004834: Add doclint support into javadoc
jjg
parents: 14359
diff changeset
   135
            null,
52a04c670c05 8004834: Add doclint support into javadoc
jjg
parents: 14359
diff changeset
   136
            List.<JCExpression>nil(),
52a04c670c05 8004834: Add doclint support into javadoc
jjg
parents: 14359
diff changeset
   137
            List.<JCTree>nil());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
        predefClassDef.sym = syms.predefClass;
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
        todo = Todo.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
        fileManager = context.get(JavaFileManager.class);
6575
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 5847
diff changeset
   141
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 5847
diff changeset
   142
        Options options = Options.instance(context);
ae1798028008 6960424: new option -Xpkginfo for better control of when package-info.class is generated
jjg
parents: 5847
diff changeset
   143
        pkginfoOpt = PkgInfo.get(options);
25007
eb097d3a68f5 8038975: Access control in enhanced for
pgovereau
parents: 24604
diff changeset
   144
        typeEnvs = TypeEnvs.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
    /** Accessor for typeEnvs
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
    public Env<AttrContext> getEnv(TypeSymbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
        return typeEnvs.get(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 22442
diff changeset
   153
    public Iterable<Env<AttrContext>> getEnvs() {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 22442
diff changeset
   154
        return typeEnvs.values();
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 22442
diff changeset
   155
    }
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 22442
diff changeset
   156
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
    public Env<AttrContext> getClassEnv(TypeSymbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
        Env<AttrContext> localEnv = getEnv(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
        Env<AttrContext> lintEnv = localEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
        while (lintEnv.info.lint == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
            lintEnv = lintEnv.next;
18010
604faee85350 8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents: 15354
diff changeset
   162
        localEnv.info.lint = lintEnv.info.lint.augment(sym);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        return localEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
    /** The queue of all classes that might still need to be completed;
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
     *  saved and initialized by main().
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
    ListBuffer<ClassSymbol> uncompleted;
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
    /** A dummy class to serve as enclClass for toplevel environments.
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
    private JCClassDecl predefClassDef;
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
/* ************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
 * environment construction
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
 *************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
    /** Create a fresh environment for class bodies.
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
     *  This will create a fresh scope for local symbols of a class, referred
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
     *  to by the environments info.scope field.
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
     *  This scope will contain
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
     *    - symbols for this and super
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
     *    - symbols for any type parameters
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
     *  In addition, it serves as an anchor for scopes of methods and initializers
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
     *  which are nested in this scope via Scope.dup().
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
     *  This scope should not be confused with the members scope of a class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
     *  @param tree     The class definition.
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
     *  @param env      The environment current outside of the class definition.
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
    public Env<AttrContext> classEnv(JCClassDecl tree, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
        Env<AttrContext> localEnv =
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25007
diff changeset
   195
            env.dup(tree, env.info.dup(WriteableScope.create(tree.sym)));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
        localEnv.enclClass = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
        localEnv.outer = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
        localEnv.info.isSelfCall = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
        localEnv.info.lint = null; // leave this to be filled in by Attr,
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
                                   // when annotations have been processed
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
        return localEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
    /** Create a fresh environment for toplevels.
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
     *  @param tree     The toplevel tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
    Env<AttrContext> topLevelEnv(JCCompilationUnit tree) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20235
diff changeset
   208
        Env<AttrContext> localEnv = new Env<>(tree, new AttrContext());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
        localEnv.toplevel = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
        localEnv.enclClass = predefClassDef;
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25007
diff changeset
   211
        tree.toplevelScope = WriteableScope.create(tree.packge);
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25007
diff changeset
   212
        tree.namedImportScope = new NamedImportScope(tree.packge, tree.toplevelScope);
7615
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents: 6709
diff changeset
   213
        tree.starImportScope = new StarImportScope(tree.packge);
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25007
diff changeset
   214
        localEnv.info.scope = tree.toplevelScope;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
        localEnv.info.lint = lint;
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
        return localEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
    public Env<AttrContext> getTopLevelEnv(JCCompilationUnit tree) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20235
diff changeset
   220
        Env<AttrContext> localEnv = new Env<>(tree, new AttrContext());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
        localEnv.toplevel = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
        localEnv.enclClass = predefClassDef;
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25007
diff changeset
   223
        localEnv.info.scope = tree.toplevelScope;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
        localEnv.info.lint = lint;
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
        return localEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
    /** The scope in which a member definition in environment env is to be entered
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
     *  This is usually the environment's scope, except for class environments,
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
     *  where the local scope is for type variables, and the this and super symbol
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
     *  only, and members go into the class member scope.
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
     */
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25007
diff changeset
   233
    WriteableScope enterScope(Env<AttrContext> env) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9087
diff changeset
   234
        return (env.tree.hasTag(JCTree.Tag.CLASSDEF))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
            ? ((JCClassDecl) env.tree).sym.members_field
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
            : env.info.scope;
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
/* ************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
 * Visitor methods for phase 1: class enter
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
 *************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
    /** Visitor argument: the current environment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
    protected Env<AttrContext> env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
    /** Visitor result: the computed type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
    Type result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
    /** Visitor method: enter all classes in given tree, catching any
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
     *  completion failure exceptions. Return the tree's type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
     *  @param tree    The tree to be visited.
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
     *  @param env     The environment visitor argument.
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
    Type classEnter(JCTree tree, Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
        Env<AttrContext> prevEnv = this.env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
            this.env = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
            tree.accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
            return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
        }  catch (CompletionFailure ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
            return chk.completionError(tree.pos(), ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
            this.env = prevEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
    /** Visitor method: enter classes of a list of trees, returning a list of types.
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
    <T extends JCTree> List<Type> classEnter(List<T> trees, Env<AttrContext> env) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20235
diff changeset
   273
        ListBuffer<Type> ts = new ListBuffer<>();
864
b1cf6afb8244 6724551: Use Queues instead of Lists to link compiler phases
jjg
parents: 10
diff changeset
   274
        for (List<T> l = trees; l.nonEmpty(); l = l.tail) {
b1cf6afb8244 6724551: Use Queues instead of Lists to link compiler phases
jjg
parents: 10
diff changeset
   275
            Type t = classEnter(l.head, env);
b1cf6afb8244 6724551: Use Queues instead of Lists to link compiler phases
jjg
parents: 10
diff changeset
   276
            if (t != null)
b1cf6afb8244 6724551: Use Queues instead of Lists to link compiler phases
jjg
parents: 10
diff changeset
   277
                ts.append(t);
b1cf6afb8244 6724551: Use Queues instead of Lists to link compiler phases
jjg
parents: 10
diff changeset
   278
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
        return ts.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
5008
3a90203fa91e 6930032: fix findbugs errors in com.sun.tools.javac.comp
jjg
parents: 4871
diff changeset
   282
    @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
    public void visitTopLevel(JCCompilationUnit tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
        JavaFileObject prev = log.useSource(tree.sourcefile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
        boolean addEnv = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
        boolean isPkgInfo = tree.sourcefile.isNameCompatible("package-info",
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
                                                             JavaFileObject.Kind.SOURCE);
24069
dfb8f11542fc 8034245: Refactor TopLevel tree node.
pgovereau
parents: 23810
diff changeset
   288
        JCPackageDecl pd = tree.getPackage();
dfb8f11542fc 8034245: Refactor TopLevel tree node.
pgovereau
parents: 23810
diff changeset
   289
        if (pd != null) {
dfb8f11542fc 8034245: Refactor TopLevel tree node.
pgovereau
parents: 23810
diff changeset
   290
            tree.packge = pd.packge = syms.enterPackage(TreeInfo.fullName(pd.pid));
dfb8f11542fc 8034245: Refactor TopLevel tree node.
pgovereau
parents: 23810
diff changeset
   291
            if (   pd.annotations.nonEmpty()
dfb8f11542fc 8034245: Refactor TopLevel tree node.
pgovereau
parents: 23810
diff changeset
   292
                || pkginfoOpt == PkgInfo.ALWAYS
dfb8f11542fc 8034245: Refactor TopLevel tree node.
pgovereau
parents: 23810
diff changeset
   293
                || tree.docComments != null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
                if (isPkgInfo) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
                    addEnv = true;
24069
dfb8f11542fc 8034245: Refactor TopLevel tree node.
pgovereau
parents: 23810
diff changeset
   296
                } else if (pd.annotations.nonEmpty()) {
dfb8f11542fc 8034245: Refactor TopLevel tree node.
pgovereau
parents: 23810
diff changeset
   297
                    log.error(pd.annotations.head.pos(),
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
                              "pkg.annotations.sb.in.package-info.java");
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
            tree.packge = syms.unnamedPackage;
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
        tree.packge.complete(); // Find all classes in package.
5008
3a90203fa91e 6930032: fix findbugs errors in com.sun.tools.javac.comp
jjg
parents: 4871
diff changeset
   305
        Env<AttrContext> topEnv = topLevelEnv(tree);
24069
dfb8f11542fc 8034245: Refactor TopLevel tree node.
pgovereau
parents: 23810
diff changeset
   306
        Env<AttrContext> packageEnv = isPkgInfo ? topEnv.dup(pd) : null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
        // Save environment of package-info.java file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
        if (isPkgInfo) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
            Env<AttrContext> env0 = typeEnvs.get(tree.packge);
24069
dfb8f11542fc 8034245: Refactor TopLevel tree node.
pgovereau
parents: 23810
diff changeset
   311
            if (env0 != null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
                JCCompilationUnit tree0 = env0.toplevel;
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
                if (!fileManager.isSameFile(tree.sourcefile, tree0.sourcefile)) {
24069
dfb8f11542fc 8034245: Refactor TopLevel tree node.
pgovereau
parents: 23810
diff changeset
   314
                    log.warning(pd != null ? pd.pid.pos() : null,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
                                "pkg-info.already.seen",
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
                                tree.packge);
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
            }
24069
dfb8f11542fc 8034245: Refactor TopLevel tree node.
pgovereau
parents: 23810
diff changeset
   319
            typeEnvs.put(tree.packge, packageEnv);
4871
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 1264
diff changeset
   320
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 1264
diff changeset
   321
            for (Symbol q = tree.packge; q != null && q.kind == PCK; q = q.owner)
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 1264
diff changeset
   322
                q.flags_field |= EXISTS;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 1264
diff changeset
   323
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 1264
diff changeset
   324
            Name name = names.package_info;
22442
8fd30fc4e3a3 8029690: Move symbol creation from ClassReader to Symtab
jfranck
parents: 22163
diff changeset
   325
            ClassSymbol c = syms.enterClass(name, tree.packge);
4871
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 1264
diff changeset
   326
            c.flatname = names.fromString(tree.packge + "." + name);
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 1264
diff changeset
   327
            c.sourcefile = tree.sourcefile;
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 1264
diff changeset
   328
            c.completer = null;
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25007
diff changeset
   329
            c.members_field = WriteableScope.create(c);
4871
655bba719625 6499119: Created package-info class file modeled improperly
jjg
parents: 1264
diff changeset
   330
            tree.packge.package_info = c;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
        }
5008
3a90203fa91e 6930032: fix findbugs errors in com.sun.tools.javac.comp
jjg
parents: 4871
diff changeset
   332
        classEnter(tree.defs, topEnv);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
        if (addEnv) {
24069
dfb8f11542fc 8034245: Refactor TopLevel tree node.
pgovereau
parents: 23810
diff changeset
   334
            todo.append(packageEnv);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
        log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
5008
3a90203fa91e 6930032: fix findbugs errors in com.sun.tools.javac.comp
jjg
parents: 4871
diff changeset
   340
    @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
    public void visitClassDef(JCClassDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
        Symbol owner = env.info.scope.owner;
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25007
diff changeset
   343
        WriteableScope enclScope = enterScope(env);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
        ClassSymbol c;
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
        if (owner.kind == PCK) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
            // We are seeing a toplevel class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
            PackageSymbol packge = (PackageSymbol)owner;
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
            for (Symbol q = packge; q != null && q.kind == PCK; q = q.owner)
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
                q.flags_field |= EXISTS;
22442
8fd30fc4e3a3 8029690: Move symbol creation from ClassReader to Symtab
jfranck
parents: 22163
diff changeset
   350
            c = syms.enterClass(tree.name, packge);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
            packge.members().enterIfAbsent(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
            if ((tree.mods.flags & PUBLIC) != 0 && !classNameMatchesFileName(c, env)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
                log.error(tree.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
                          "class.public.should.be.in.file", tree.name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
        } else {
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1257
diff changeset
   357
            if (!tree.name.isEmpty() &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
                !chk.checkUniqueClassName(tree.pos(), tree.name, enclScope)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
                result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
            if (owner.kind == TYP) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
                // We are seeing a member class.
22442
8fd30fc4e3a3 8029690: Move symbol creation from ClassReader to Symtab
jfranck
parents: 22163
diff changeset
   364
                c = syms.enterClass(tree.name, (TypeSymbol)owner);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
                if ((owner.flags_field & INTERFACE) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
                    tree.mods.flags |= PUBLIC | STATIC;
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
                // We are seeing a local class.
22442
8fd30fc4e3a3 8029690: Move symbol creation from ClassReader to Symtab
jfranck
parents: 22163
diff changeset
   370
                c = syms.defineClass(tree.name, owner);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
                c.flatname = chk.localClassName(c);
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1257
diff changeset
   372
                if (!c.name.isEmpty())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
                    chk.checkTransparentClass(tree.pos(), c, env.info.scope);
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
        tree.sym = c;
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
        // Enter class into `compiled' table and enclosing scope.
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
        if (chk.compiled.get(c.flatname) != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
            duplicateClass(tree.pos(), c);
1257
873b053bf757 6557752: Original type of an AST should be made available even if it is replaced with an ErrorType
jjg
parents: 1206
diff changeset
   381
            result = types.createErrorType(tree.name, (TypeSymbol)owner, Type.noType);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
            tree.sym = (ClassSymbol)result.tsym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
        chk.compiled.put(c.flatname, c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
        enclScope.enter(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
        // Set up an environment for class block and store in `typeEnvs'
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
        // table, to be retrieved later in memberEnter and attribution.
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
        Env<AttrContext> localEnv = classEnv(tree, env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
        typeEnvs.put(c, localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
        // Fill out class fields.
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
        c.completer = memberEnter;
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
        c.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, c, tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
        c.sourcefile = env.toplevel.sourcefile;
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25007
diff changeset
   397
        c.members_field = WriteableScope.create(c);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
        ClassType ct = (ClassType)c.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
        if (owner.kind != PCK && (c.flags_field & STATIC) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
            // We are seeing a local or inner class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
            // Set outer_field of this class to closest enclosing class
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
            // which contains this class in a non-static context
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
            // (its "enclosing instance class"), provided such a class exists.
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
            Symbol owner1 = owner;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 25874
diff changeset
   406
            while (owner1.kind.matches(KindSelector.VAL_MTH) &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
                   (owner1.flags_field & STATIC) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
                owner1 = owner1.owner;
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
            if (owner1.kind == TYP) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
                ct.setEnclosingType(owner1.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
        // Enter type parameters.
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
        ct.typarams_field = classEnter(tree.typarams, localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
        // Add non-local class to uncompleted, to make sure it will be
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
        // completed later.
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
        if (!c.isLocal() && uncompleted != null) uncompleted.append(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
//      System.err.println("entering " + c.fullname + " in " + c.owner);//DEBUG
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
        // Recursively enter all member classes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
        classEnter(tree.defs, localEnv);
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
        result = c.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
    //where
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
        /** Does class have the same name as the file it appears in?
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
        private static boolean classNameMatchesFileName(ClassSymbol c,
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
                                                        Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
            return env.toplevel.sourcefile.isNameCompatible(c.name.toString(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
                                                            JavaFileObject.Kind.SOURCE);
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
    /** Complain about a duplicate class. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
    protected void duplicateClass(DiagnosticPosition pos, ClassSymbol c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
        log.error(pos, "duplicate.class", c.fullname);
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
    /** Class enter visitor method for type parameters.
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
     *  Enter a symbol for type parameter in local scope, after checking that it
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
     *  is unique.
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
     */
5008
3a90203fa91e 6930032: fix findbugs errors in com.sun.tools.javac.comp
jjg
parents: 4871
diff changeset
   446
    @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
    public void visitTypeParameter(JCTypeParameter tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
        TypeVar a = (tree.type != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
            ? (TypeVar)tree.type
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
            : new TypeVar(tree.name, env.info.scope.owner, syms.botType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
        tree.type = a;
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
        if (chk.checkUnique(tree.pos(), a.tsym, env.info.scope)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
            env.info.scope.enter(a.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
        result = a;
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
    /** Default class enter visitor method: do nothing.
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
     */
5008
3a90203fa91e 6930032: fix findbugs errors in com.sun.tools.javac.comp
jjg
parents: 4871
diff changeset
   460
    @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
    public void visitTree(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
        result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
    /** Main method: enter all classes in a list of toplevel trees.
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
     *  @param trees      The list of trees to be processed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
    public void main(List<JCCompilationUnit> trees) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
        complete(trees, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
    /** Main method: enter one class from a list of toplevel trees and
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
     *  place the rest on uncompleted for later processing.
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
     *  @param trees      The list of trees to be processed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
     *  @param c          The class symbol to be processed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
    public void complete(List<JCCompilationUnit> trees, ClassSymbol c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
        annotate.enterStart();
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
        ListBuffer<ClassSymbol> prevUncompleted = uncompleted;
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20235
diff changeset
   480
        if (memberEnter.completionEnabled) uncompleted = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
            // enter all classes, and construct uncompleted list
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
            classEnter(trees, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
            // complete all uncompleted classes in memberEnter
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
            if  (memberEnter.completionEnabled) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
                while (uncompleted.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
                    ClassSymbol clazz = uncompleted.next();
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
                    if (c == null || c == clazz || prevUncompleted == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
                        clazz.complete();
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
                    else
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
                        // defer
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
                        prevUncompleted.append(clazz);
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
                // if there remain any unimported toplevels (these must have
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
                // no classes at all), process their import statements as well.
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
                for (JCCompilationUnit tree : trees) {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 25007
diff changeset
   500
                    if (tree.starImportScope.isEmpty()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
                        JavaFileObject prev = log.useSource(tree.sourcefile);
5008
3a90203fa91e 6930032: fix findbugs errors in com.sun.tools.javac.comp
jjg
parents: 4871
diff changeset
   502
                        Env<AttrContext> topEnv = topLevelEnv(tree);
3a90203fa91e 6930032: fix findbugs errors in com.sun.tools.javac.comp
jjg
parents: 4871
diff changeset
   503
                        memberEnter.memberEnter(tree, topEnv);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
                        log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
            uncompleted = prevUncompleted;
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
            annotate.enterDone();
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
    }
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 22442
diff changeset
   513
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 22442
diff changeset
   514
    public void newRound() {
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 22442
diff changeset
   515
        typeEnvs.clear();
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 22442
diff changeset
   516
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
}