treemaker.java

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

JAVA
892
字号
/* * 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.tree;import com.sun.tools.javac.code.*;import com.sun.tools.javac.code.Symbol.*;import com.sun.tools.javac.code.Type.*;import com.sun.tools.javac.util.*;import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;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.*;/** Factory class for trees. * *  <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 TreeMaker implements JCTree.Factory {    /** The context key for the tree factory. */    protected static final Context.Key<TreeMaker> treeMakerKey =        new Context.Key<TreeMaker>();    /** Get the TreeMaker instance. */    public static TreeMaker instance(Context context) {        TreeMaker instance = context.get(treeMakerKey);        if (instance == null)            instance = new TreeMaker(context);        return instance;    }    /** The position at which subsequent trees will be created.     */    public int pos = Position.NOPOS;    /** The toplevel tree to which created trees belong.     */    public JCCompilationUnit toplevel;    /** The current name table. */    Name.Table names;    Types types;    /** The current symbol table. */    Symtab syms;    /** Create a tree maker with null toplevel and NOPOS as initial position.     */    protected TreeMaker(Context context) {        context.put(treeMakerKey, this);        this.pos = Position.NOPOS;        this.toplevel = null;        this.names = Name.Table.instance(context);        this.syms = Symtab.instance(context);        this.types = Types.instance(context);    }    /** Create a tree maker with a given toplevel and FIRSTPOS as initial position.     */    TreeMaker(JCCompilationUnit toplevel, Name.Table names, Types types, Symtab syms) {        this.pos = Position.FIRSTPOS;        this.toplevel = toplevel;        this.names = names;        this.types = types;        this.syms = syms;    }    /** Create a new tree maker for a given toplevel.     */    public TreeMaker forToplevel(JCCompilationUnit toplevel) {        return new TreeMaker(toplevel, names, types, syms);    }    /** Reassign current position.     */    public TreeMaker at(int pos) {        this.pos = pos;        return this;    }    /** Reassign current position.     */    public TreeMaker at(DiagnosticPosition pos) {        this.pos = (pos == null ? Position.NOPOS : pos.getStartPosition());        return this;    }    /**     * Create given tree node at current position.     * @param defs a list of ClassDef, Import, and Skip     */    public JCCompilationUnit TopLevel(List<JCAnnotation> packageAnnotations,				      JCExpression pid,				      List<JCTree> defs) {        assert packageAnnotations != null;        for (JCTree node : defs)            assert node instanceof JCClassDecl		|| node instanceof JCImport		|| node instanceof JCSkip                || node instanceof JCErroneous		|| (node instanceof JCExpressionStatement		    && ((JCExpressionStatement)node).expr instanceof JCErroneous)                 : node.getClass().getSimpleName();        JCCompilationUnit tree = new JCCompilationUnit(packageAnnotations, pid, defs,                                     null, null, null, null);        tree.pos = pos;        return tree;    }    public JCImport Import(JCTree qualid, boolean importStatic) {        JCImport tree = new JCImport(qualid, importStatic);        tree.pos = pos;        return tree;    }    public JCClassDecl ClassDef(JCModifiers mods,				Name name,				List<JCTypeParameter> typarams,				JCTree extending,				List<JCExpression> implementing,				List<JCTree> defs)    {        JCClassDecl tree = new JCClassDecl(mods,				     name,				     typarams,				     extending,				     implementing,				     defs,				     null);        tree.pos = pos;        return tree;    }    public JCMethodDecl MethodDef(JCModifiers mods,                               Name name,                               JCExpression restype,                               List<JCTypeParameter> typarams,                               List<JCVariableDecl> params,                               List<JCExpression> thrown,                               JCBlock body,                               JCExpression defaultValue)    {        JCMethodDecl tree = new JCMethodDecl(mods,                                       name,                                       restype,                                       typarams,                                       params,                                       thrown,                                       body,                                       defaultValue,                                       null);        tree.pos = pos;        return tree;    }    public JCVariableDecl VarDef(JCModifiers mods, Name name, JCExpression vartype, JCExpression init) {        JCVariableDecl tree = new JCVariableDecl(mods, name, vartype, init, null);        tree.pos = pos;        return tree;    }    public JCSkip Skip() {        JCSkip tree = new JCSkip();        tree.pos = pos;        return tree;    }    public JCBlock Block(long flags, List<JCStatement> stats) {        JCBlock tree = new JCBlock(flags, stats);        tree.pos = pos;        return tree;    }    public JCDoWhileLoop DoLoop(JCStatement body, JCExpression cond) {        JCDoWhileLoop tree = new JCDoWhileLoop(body, cond);        tree.pos = pos;        return tree;    }    public JCWhileLoop WhileLoop(JCExpression cond, JCStatement body) {        JCWhileLoop tree = new JCWhileLoop(cond, body);        tree.pos = pos;        return tree;    }    public JCForLoop ForLoop(List<JCStatement> init,                           JCExpression cond,                           List<JCExpressionStatement> step,                           JCStatement body)    {        JCForLoop tree = new JCForLoop(init, cond, step, body);        tree.pos = pos;        return tree;    }    public JCEnhancedForLoop ForeachLoop(JCVariableDecl var, JCExpression expr, JCStatement body) {        JCEnhancedForLoop tree = new JCEnhancedForLoop(var, expr, body);        tree.pos = pos;        return tree;    }    public JCLabeledStatement Labelled(Name label, JCStatement body) {        JCLabeledStatement tree = new JCLabeledStatement(label, body);        tree.pos = pos;        return tree;    }    public JCSwitch Switch(JCExpression selector, List<JCCase> cases) {        JCSwitch tree = new JCSwitch(selector, cases);        tree.pos = pos;        return tree;    }    public JCCase Case(JCExpression pat, List<JCStatement> stats) {        JCCase tree = new JCCase(pat, stats);        tree.pos = pos;        return tree;    }    public JCSynchronized Synchronized(JCExpression lock, JCBlock body) {        JCSynchronized tree = new JCSynchronized(lock, body);        tree.pos = pos;        return tree;    }    public JCTry Try(JCBlock body, List<JCCatch> catchers, JCBlock finalizer) {        JCTry tree = new JCTry(body, catchers, finalizer);        tree.pos = pos;        return tree;    }    public JCCatch Catch(JCVariableDecl param, JCBlock body) {        JCCatch tree = new JCCatch(param, body);        tree.pos = pos;        return tree;    }    public JCConditional Conditional(JCExpression cond,                                   JCExpression thenpart,                                   JCExpression elsepart)    {        JCConditional tree = new JCConditional(cond, thenpart, elsepart);        tree.pos = pos;        return tree;    }    public JCIf If(JCExpression cond, JCStatement thenpart, JCStatement elsepart) {        JCIf tree = new JCIf(cond, thenpart, elsepart);        tree.pos = pos;        return tree;    }    public JCExpressionStatement Exec(JCExpression expr) {        JCExpressionStatement tree = new JCExpressionStatement(expr);        tree.pos = pos;        return tree;    }    public JCBreak Break(Name label) {        JCBreak tree = new JCBreak(label, null);        tree.pos = pos;        return tree;    }    public JCContinue Continue(Name label) {        JCContinue tree = new JCContinue(label, null);        tree.pos = pos;        return tree;    }    public JCReturn Return(JCExpression expr) {        JCReturn tree = new JCReturn(expr);        tree.pos = pos;        return tree;    }    public JCThrow Throw(JCTree expr) {        JCThrow tree = new JCThrow(expr);        tree.pos = pos;        return tree;    }    public JCAssert Assert(JCExpression cond, JCExpression detail) {        JCAssert tree = new JCAssert(cond, detail);        tree.pos = pos;        return tree;    }    public JCMethodInvocation Apply(List<JCExpression> typeargs,		       JCExpression fn,		       List<JCExpression> args)    {        JCMethodInvocation tree = new JCMethodInvocation(typeargs, fn, args);        tree.pos = pos;        return tree;    }    public JCNewClass NewClass(JCExpression encl,                             List<JCExpression> typeargs,                             JCExpression clazz,                             List<JCExpression> args,                             JCClassDecl def)    {        JCNewClass tree = new JCNewClass(encl, typeargs, clazz, args, def);        tree.pos = pos;        return tree;    }    public JCNewArray NewArray(JCExpression elemtype,			     List<JCExpression> dims,			     List<JCExpression> elems)    {        JCNewArray tree = new JCNewArray(elemtype, dims, elems);        tree.pos = pos;        return tree;    }    public JCParens Parens(JCExpression expr) {        JCParens tree = new JCParens(expr);        tree.pos = pos;        return tree;    }    public JCAssign Assign(JCExpression lhs, JCExpression rhs) {        JCAssign tree = new JCAssign(lhs, rhs);        tree.pos = pos;        return tree;    }    public JCAssignOp Assignop(int opcode, JCTree lhs, JCTree rhs) {        JCAssignOp tree = new JCAssignOp(opcode, lhs, rhs, null);        tree.pos = pos;        return tree;    }    public JCUnary Unary(int opcode, JCExpression arg) {        JCUnary tree = new JCUnary(opcode, arg);        tree.pos = pos;        return tree;    }    public JCBinary Binary(int opcode, JCExpression lhs, JCExpression rhs) {        JCBinary tree = new JCBinary(opcode, lhs, rhs, null);        tree.pos = pos;        return tree;    }    public JCTypeCast TypeCast(JCTree clazz, JCExpression expr) {        JCTypeCast tree = new JCTypeCast(clazz, expr);        tree.pos = pos;        return tree;    }    public JCInstanceOf TypeTest(JCExpression expr, JCTree clazz) {        JCInstanceOf tree = new JCInstanceOf(expr, clazz);        tree.pos = pos;        return tree;    }    public JCArrayAccess Indexed(JCExpression indexed, JCExpression index) {        JCArrayAccess tree = new JCArrayAccess(indexed, index);        tree.pos = pos;        return tree;    }    public JCFieldAccess Select(JCExpression selected, Name selector) {        JCFieldAccess tree = new JCFieldAccess(selected, selector, null);        tree.pos = pos;        return tree;    }    public JCIdent Ident(Name name) {        JCIdent tree = new JCIdent(name, null);        tree.pos = pos;        return tree;    }    public JCLiteral Literal(int tag, Object value) {        JCLiteral tree = new JCLiteral(tag, value);        tree.pos = pos;        return tree;    }    public JCPrimitiveTypeTree TypeIdent(int typetag) {        JCPrimitiveTypeTree tree = new JCPrimitiveTypeTree(typetag);        tree.pos = pos;        return tree;    }    public JCArrayTypeTree TypeArray(JCExpression elemtype) {        JCArrayTypeTree tree = new JCArrayTypeTree(elemtype);        tree.pos = pos;        return tree;    }    public JCTypeApply TypeApply(JCExpression clazz, List<JCExpression> arguments) {        JCTypeApply tree = new JCTypeApply(clazz, arguments);        tree.pos = pos;        return tree;    }    public JCTypeParameter TypeParameter(Name name, List<JCExpression> bounds) {        JCTypeParameter tree = new JCTypeParameter(name, bounds);        tree.pos = pos;        return tree;    }    public JCWildcard Wildcard(TypeBoundKind kind, JCTree type) {        JCWildcard tree = new JCWildcard(kind, type);        tree.pos = pos;        return tree;    }    public TypeBoundKind TypeBoundKind(BoundKind kind) {        TypeBoundKind tree = new TypeBoundKind(kind);        tree.pos = pos;

⌨️ 快捷键说明

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