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