enter.java
来自「是一款用JAVA 编写的编译器 具有很强的编译功能」· Java 代码 · 共 490 行 · 第 1/2 页
JAVA
490 行
/* * Copyright 1999-2006 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Sun designates this * particular file as subject to the "Classpath" exception as provided * by Sun in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, * CA 95054 USA or visit www.sun.com if you need additional information or * have any questions. */package com.sun.tools.javac.comp;import java.util.*;import java.util.Set;import javax.tools.JavaFileObject;import javax.tools.JavaFileManager;import com.sun.tools.javac.code.*;import com.sun.tools.javac.jvm.*;import com.sun.tools.javac.tree.*;import com.sun.tools.javac.util.*;import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;import com.sun.tools.javac.util.List;import com.sun.tools.javac.code.Type.*;import com.sun.tools.javac.code.Symbol.*;import com.sun.tools.javac.tree.JCTree.*;import static com.sun.tools.javac.code.Flags.*;import static com.sun.tools.javac.code.Kinds.*;import static com.sun.tools.javac.code.TypeTags.*;/** This class enters symbols for all encountered definitions into * the symbol table. The pass consists of two phases, organized as * follows: * * <p>In the first phase, all class symbols are intered into their * enclosing scope, descending recursively down the tree for classes * which are members of other classes. The class symbols are given a * MemberEnter object as completer. * * <p>In the second phase classes are completed using * MemberEnter.complete(). Completion might occur on demand, but * any classes that are not completed that way will be eventually * completed by processing the `uncompleted' queue. Completion * entails (1) determination of a class's parameters, supertype and * interfaces, as well as (2) entering all symbols defined in the * class into its scope, with the exception of class symbols which * have been entered in phase 1. (2) depends on (1) having been * completed for a class and all its superclasses and enclosing * classes. That's why, after doing (1), we put classes in a * `halfcompleted' queue. Only when we have performed (1) for a class * and all it's superclasses and enclosing classes, we proceed to * (2). * * <p>Whereas the first phase is organized as a sweep through all * compiled syntax trees, the second phase is demand. Members of a * class are entered when the contents of a class are first * accessed. This is accomplished by installing completer objects in * class symbols for compiled classes which invoke the member-enter * phase for the corresponding class tree. * * <p>Classes migrate from one phase to the next via queues: * * <pre> * class enter -> (Enter.uncompleted) --> member enter (1) * -> (MemberEnter.halfcompleted) --> member enter (2) * -> (Todo) --> attribute * (only for toplevel classes) * </pre> * * <p><b>This is NOT part of any API supported by Sun Microsystems. If * you write code that depends on this, you do so at your own risk. * This code and its internal interfaces are subject to change or * deletion without notice.</b> */public class Enter extends JCTree.Visitor { protected static final Context.Key<Enter> enterKey = new Context.Key<Enter>(); Log log; Symtab syms; Check chk; TreeMaker make; ClassReader reader; Annotate annotate; MemberEnter memberEnter; Lint lint; JavaFileManager fileManager; private final Todo todo; public static Enter instance(Context context) { Enter instance = context.get(enterKey); if (instance == null) instance = new Enter(context); return instance; } protected Enter(Context context) { context.put(enterKey, this); log = Log.instance(context); reader = ClassReader.instance(context); make = TreeMaker.instance(context); syms = Symtab.instance(context); chk = Check.instance(context); memberEnter = MemberEnter.instance(context); annotate = Annotate.instance(context); lint = Lint.instance(context); predefClassDef = make.ClassDef( make.Modifiers(PUBLIC), syms.predefClass.name, null, null, null, null); predefClassDef.sym = syms.predefClass; todo = Todo.instance(context); fileManager = context.get(JavaFileManager.class); } /** A hashtable mapping classes and packages to the environments current * at the points of their definitions. */ Map<TypeSymbol,Env<AttrContext>> typeEnvs = new HashMap<TypeSymbol,Env<AttrContext>>(); /** Accessor for typeEnvs */ public Env<AttrContext> getEnv(TypeSymbol sym) { return typeEnvs.get(sym); } public Env<AttrContext> getClassEnv(TypeSymbol sym) { Env<AttrContext> localEnv = getEnv(sym); Env<AttrContext> lintEnv = localEnv; while (lintEnv.info.lint == null) lintEnv = lintEnv.next; localEnv.info.lint = lintEnv.info.lint.augment(sym.attributes_field, sym.flags()); return localEnv; } /** The queue of all classes that might still need to be completed; * saved and initialized by main(). */ ListBuffer<ClassSymbol> uncompleted; /** A dummy class to serve as enclClass for toplevel environments. */ private JCClassDecl predefClassDef;/* ************************************************************************ * environment construction *************************************************************************/ /** Create a fresh environment for class bodies. * This will create a fresh scope for local symbols of a class, referred * to by the environments info.scope field. * This scope will contain * - symbols for this and super * - symbols for any type parameters * In addition, it serves as an anchor for scopes of methods and initializers * which are nested in this scope via Scope.dup(). * This scope should not be confused with the members scope of a class. * * @param tree The class definition. * @param env The environment current outside of the class definition. */ public Env<AttrContext> classEnv(JCClassDecl tree, Env<AttrContext> env) { Env<AttrContext> localEnv = env.dup(tree, env.info.dup(new Scope(tree.sym))); localEnv.enclClass = tree; localEnv.outer = env; localEnv.info.isSelfCall = false; localEnv.info.lint = null; // leave this to be filled in by Attr, // when annotations have been processed return localEnv; } /** Create a fresh environment for toplevels. * @param tree The toplevel tree. */ Env<AttrContext> topLevelEnv(JCCompilationUnit tree) { Env<AttrContext> localEnv = new Env<AttrContext>(tree, new AttrContext()); localEnv.toplevel = tree; localEnv.enclClass = predefClassDef; tree.namedImportScope = new Scope.ImportScope(tree.packge); tree.starImportScope = new Scope.ImportScope(tree.packge); localEnv.info.scope = tree.namedImportScope; localEnv.info.lint = lint; return localEnv; } public Env<AttrContext> getTopLevelEnv(JCCompilationUnit tree) { Env<AttrContext> localEnv = new Env<AttrContext>(tree, new AttrContext()); localEnv.toplevel = tree; localEnv.enclClass = predefClassDef; localEnv.info.scope = tree.namedImportScope; localEnv.info.lint = lint; return localEnv; } /** The scope in which a member definition in environment env is to be entered * This is usually the environment's scope, except for class environments, * where the local scope is for type variables, and the this and super symbol * only, and members go into the class member scope. */ Scope enterScope(Env<AttrContext> env) { return (env.tree.getTag() == JCTree.CLASSDEF) ? ((JCClassDecl) env.tree).sym.members_field : env.info.scope; }/* ************************************************************************ * Visitor methods for phase 1: class enter *************************************************************************/ /** Visitor argument: the current environment. */ protected Env<AttrContext> env; /** Visitor result: the computed type. */ Type result; /** Visitor method: enter all classes in given tree, catching any * completion failure exceptions. Return the tree's type. * * @param tree The tree to be visited. * @param env The environment visitor argument. */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?