symtab.java

来自「是一款用JAVA 编写的编译器 具有很强的编译功能」· Java 代码 · 共 578 行 · 第 1/2 页

JAVA
578
字号
/* * 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.code;import java.util.*;import com.sun.tools.javac.util.*;import com.sun.tools.javac.util.List;import com.sun.tools.javac.code.Symbol.*;import com.sun.tools.javac.code.Type.*;import com.sun.tools.javac.jvm.*;import static com.sun.tools.javac.jvm.ByteCodes.*;import static com.sun.tools.javac.code.Flags.*;/** A class that defines all predefined constants and operators *  as well as special classes such as java.lang.Object, which need *  to be known to the compiler. All symbols are held in instance *  fields. This makes it possible to work in multiple concurrent *  projects, which might use different class files for library classes. * *  <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 Symtab {    /** The context key for the symbol table. */    protected static final Context.Key<Symtab> symtabKey =	new Context.Key<Symtab>();    /** Get the symbol table instance. */    public static Symtab instance(Context context) {	Symtab instance = context.get(symtabKey);	if (instance == null)	    instance = new Symtab(context);	return instance;    }    /** Builtin types.     */    public final Type byteType = new Type(TypeTags.BYTE, null);           public final Type charType = new Type(TypeTags.CHAR, null);           public final Type shortType = new Type(TypeTags.SHORT, null);         public final Type intType = new Type(TypeTags.INT, null);             public final Type longType = new Type(TypeTags.LONG, null);           public final Type floatType = new Type(TypeTags.FLOAT, null);         public final Type doubleType = new Type(TypeTags.DOUBLE, null);       public final Type booleanType = new Type(TypeTags.BOOLEAN, null);     public final Type botType = new BottomType();    public final JCNoType voidType = new JCNoType(TypeTags.VOID);    private final Name.Table names;    private final ClassReader reader;    /** A symbol for the root package.     */    public final PackageSymbol rootPackage;    /** A symbol for the unnamed package.     */    public final PackageSymbol unnamedPackage;    /** A symbol that stands for a missing symbol.     */    public final TypeSymbol noSymbol;    /** The error symbol.     */    public final ClassSymbol errSymbol;    /** An instance of the error type.     */    public final Type errType;    /** A value for the unknown type. */    public final Type unknownType;    /** The builtin type of all arrays. */    public final ClassSymbol arrayClass;    public final MethodSymbol arrayCloneMethod;    /** VGJ: The (singleton) type of all bound types. */    public final ClassSymbol boundClass;    /** The builtin type of all methods. */    public final ClassSymbol methodClass;    /** Predefined types.     */    public final Type objectType;    public final Type classType;    public final Type classLoaderType;    public final Type stringType;    public final Type stringBufferType;    public final Type stringBuilderType;    public final Type cloneableType;    public final Type serializableType;    public final Type throwableType;    public final Type errorType;    public final Type illegalArgumentExceptionType;    public final Type exceptionType;    public final Type runtimeExceptionType;    public final Type classNotFoundExceptionType;    public final Type noClassDefFoundErrorType;    public final Type noSuchFieldErrorType;    public final Type assertionErrorType;    public final Type cloneNotSupportedExceptionType;    public final Type annotationType;    public final TypeSymbol enumSym;    public final Type listType;    public final Type collectionsType;    public final Type comparableType;    public final Type arraysType;    public final Type iterableType;    public final Type iteratorType;    public final Type annotationTargetType;    public final Type overrideType;    public final Type retentionType;    public final Type deprecatedType;    public final Type suppressWarningsType;    public final Type inheritedType;    public final Type proprietaryType;    /** The symbol representing the length field of an array.     */    public final VarSymbol lengthVar;    /** The null check operator. */    public final OperatorSymbol nullcheck;    /** The symbol representing the final finalize method on enums */    public final MethodSymbol enumFinalFinalize;    /** The predefined type that belongs to a tag.     */    public final Type[] typeOfTag = new Type[TypeTags.TypeTagCount];    /** The name of the class that belongs to a basix type tag.     */    public final Name[] boxedName = new Name[TypeTags.TypeTagCount];    /** A hashtable containing the encountered top-level and member classes,     *  indexed by flat names. The table does not contain local classes.     *  It should be updated from the outside to reflect classes defined     *  by compiled source files.     */    public final Map<Name, ClassSymbol> classes = new HashMap<Name, ClassSymbol>();    /** A hashtable containing the encountered packages.     *  the table should be updated from outside to reflect packages defined     *  by compiled source files.     */    public final Map<Name, PackageSymbol> packages = new HashMap<Name, PackageSymbol>();    public void initType(Type type, ClassSymbol c) {	type.tsym = c;	typeOfTag[type.tag] = type;    }    public void initType(Type type, String name) {	initType(	    type,	    new ClassSymbol(		PUBLIC, names.fromString(name), type, rootPackage));    }    public void initType(Type type, String name, String bname) {	initType(type, name);	boxedName[type.tag] = names.fromString("java.lang." + bname);    }    /** The class symbol that owns all predefined symbols.     */    public final ClassSymbol predefClass;    /** Enter a constant into symbol table.     *  @param name   The constant's name.     *  @param type   The constant's type.     */    private VarSymbol enterConstant(String name, Type type) {        VarSymbol c = new VarSymbol(	    PUBLIC | STATIC | FINAL,	    names.fromString(name),	    type,	    predefClass);	c.setData(type.constValue());        predefClass.members().enter(c);	return c;    }    /** Enter a binary operation into symbol table.     *  @param name     The name of the operator.     *  @param left     The type of the left operand.     *  @param right    The type of the left operand.     *  @param res      The operation's result type.     *  @param opcode   The operation's bytecode instruction.     */    private void enterBinop(String name,			    Type left, Type right, Type res,			    int opcode) {        predefClass.members().enter(            new OperatorSymbol(		names.fromString(name),		new MethodType(List.of(left, right), res,			       List.<Type>nil(), methodClass),		opcode,		predefClass));    }    /** Enter a binary operation, as above but with two opcodes,     *  which get encoded as (opcode1 << ByteCodeTags.preShift) + opcode2.     *  @param opcode1     First opcode.     *  @param opcode2     Second opcode.     */    private void enterBinop(String name,			    Type left, Type right, Type res,			    int opcode1, int opcode2) {	enterBinop(	    name, left, right, res, (opcode1 << ByteCodes.preShift) | opcode2);    }    /** Enter a unary operation into symbol table.     *  @param name     The name of the operator.     *  @param arg      The type of the operand.     *  @param res      The operation's result type.     *  @param opcode   The operation's bytecode instruction.     */    private OperatorSymbol enterUnop(String name,				     Type arg,				     Type res,				     int opcode) {	OperatorSymbol sym =	    new OperatorSymbol(names.fromString(name),			       new MethodType(List.of(arg),					      res,					      List.<Type>nil(),					      methodClass),			       opcode,			       predefClass);        predefClass.members().enter(sym);	return sym;    }    /** Enter a class into symbol table.     *  @param    The name of the class.     */    private Type enterClass(String s) {	return reader.enterClass(names.fromString(s)).type;    }    /** Constructor; enters all predefined identifiers and operators     *  into symbol table.     */    protected Symtab(Context context) throws CompletionFailure {	context.put(symtabKey, this);	names = Name.Table.instance(context);	// Create the unknown type	unknownType = new Type(TypeTags.UNKNOWN, null);	// create the basic builtin symbols	rootPackage = new PackageSymbol(names.empty, null);        final Messages messages = Messages.instance(context);        unnamedPackage = new PackageSymbol(names.empty, rootPackage) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?