duke@1: /* xdono@117: * Copyright 1999-2008 Sun Microsystems, Inc. All Rights Reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as duke@1: * published by the Free Software Foundation. Sun designates this duke@1: * particular file as subject to the "Classpath" exception as provided duke@1: * by Sun in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * duke@1: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@1: * CA 95054 USA or visit www.sun.com if you need additional information or duke@1: * have any questions. duke@1: */ duke@1: duke@1: package com.sun.tools.javac.comp; duke@1: duke@1: import com.sun.tools.javac.tree.*; duke@1: import java.util.Iterator; duke@1: import java.util.NoSuchElementException; duke@1: duke@1: /** A class for environments, instances of which are passed as duke@1: * arguments to tree visitors. Environments refer to important ancestors duke@1: * of the subtree that's currently visited, such as the enclosing method, duke@1: * the enclosing class, or the enclosing toplevel node. They also contain duke@1: * a generic component, represented as a type parameter, to carry further duke@1: * information specific to individual passes. duke@1: * duke@1: *

This is NOT part of any API supported by Sun Microsystems. If duke@1: * you write code that depends on this, you do so at your own risk. duke@1: * This code and its internal interfaces are subject to change or duke@1: * deletion without notice. duke@1: */ duke@1: public class Env implements Iterable> { duke@1: duke@1: /** The next enclosing environment. duke@1: */ duke@1: public Env next; duke@1: duke@1: /** The environment enclosing the current class. duke@1: */ duke@1: public Env outer; duke@1: duke@1: /** The tree with which this environment is associated. duke@1: */ duke@1: public JCTree tree; duke@1: duke@1: /** The enclosing toplevel tree. duke@1: */ duke@1: public JCTree.JCCompilationUnit toplevel; duke@1: duke@1: /** The next enclosing class definition. duke@1: */ duke@1: public JCTree.JCClassDecl enclClass; duke@1: duke@1: /** The next enclosing method definition. duke@1: */ duke@1: public JCTree.JCMethodDecl enclMethod; duke@1: duke@1: /** A generic field for further information. duke@1: */ duke@1: public A info; duke@1: duke@1: /** Is this an environment for evaluating a base clause? duke@1: */ duke@1: public boolean baseClause = false; duke@1: duke@1: /** Create an outermost environment for a given (toplevel)tree, duke@1: * with a given info field. duke@1: */ duke@1: public Env(JCTree tree, A info) { duke@1: this.next = null; duke@1: this.outer = null; duke@1: this.tree = tree; duke@1: this.toplevel = null; duke@1: this.enclClass = null; duke@1: this.enclMethod = null; duke@1: this.info = info; duke@1: } duke@1: duke@1: /** Duplicate this environment, updating with given tree and info, duke@1: * and copying all other fields. duke@1: */ duke@1: public Env dup(JCTree tree, A info) { duke@1: return dupto(new Env(tree, info)); duke@1: } duke@1: duke@1: /** Duplicate this environment into a given Environment, duke@1: * using its tree and info, and copying all other fields. duke@1: */ duke@1: public Env dupto(Env that) { duke@1: that.next = this; duke@1: that.outer = this.outer; duke@1: that.toplevel = this.toplevel; duke@1: that.enclClass = this.enclClass; duke@1: that.enclMethod = this.enclMethod; duke@1: return that; duke@1: } duke@1: duke@1: /** Duplicate this environment, updating with given tree, duke@1: * and copying all other fields. duke@1: */ duke@1: public Env dup(JCTree tree) { duke@1: return dup(tree, this.info); duke@1: } duke@1: duke@1: /** Return closest enclosing environment which points to a tree with given tag. duke@1: */ duke@1: public Env enclosing(int tag) { duke@1: Env env1 = this; duke@1: while (env1 != null && env1.tree.getTag() != tag) env1 = env1.next; duke@1: return env1; duke@1: } duke@1: duke@1: public String toString() { duke@1: return "Env[" + info + (outer == null ? "" : ",outer=" + outer) + "]"; duke@1: } duke@1: duke@1: public Iterator> iterator() { duke@1: return new Iterator>() { duke@1: Env next = Env.this; duke@1: public boolean hasNext() { duke@1: return next.outer != null; duke@1: } duke@1: public Env next() { duke@1: if (hasNext()) { duke@1: Env current = next; duke@1: next = current.outer; duke@1: return current; duke@1: } duke@1: throw new NoSuchElementException(); duke@1: duke@1: } duke@1: public void remove() { duke@1: throw new UnsupportedOperationException(); duke@1: } duke@1: }; duke@1: } duke@1: }