001: /*
002: * Copyright 1999-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.javac.comp;
027:
028: import com.sun.tools.javac.util.*;
029: import com.sun.tools.javac.tree.*;
030: import java.util.Iterator;
031: import java.util.NoSuchElementException;
032:
033: /** A class for environments, instances of which are passed as
034: * arguments to tree visitors. Environments refer to important ancestors
035: * of the subtree that's currently visited, such as the enclosing method,
036: * the enclosing class, or the enclosing toplevel node. They also contain
037: * a generic component, represented as a type parameter, to carry further
038: * information specific to individual passes.
039: *
040: * <p><b>This is NOT part of any API supported by Sun Microsystems. If
041: * you write code that depends on this, you do so at your own risk.
042: * This code and its internal interfaces are subject to change or
043: * deletion without notice.</b>
044: */
045: @Version("@(#)Env.java 1.30 07/06/14")
046: public class Env<A> implements Iterable<Env<A>> {
047:
048: /** The next enclosing environment.
049: */
050: public Env<A> next;
051:
052: /** The environment enclosing the current class.
053: */
054: public Env<A> outer;
055:
056: /** The tree with which this environment is associated.
057: */
058: public JCTree tree;
059:
060: /** The enclosing toplevel tree.
061: */
062: public JCTree.JCCompilationUnit toplevel;
063:
064: /** The next enclosing class definition.
065: */
066: public JCTree.JCClassDecl enclClass;
067:
068: /** The next enclosing method definition.
069: */
070: public JCTree.JCMethodDecl enclMethod;
071:
072: /** A generic field for further information.
073: */
074: public A info;
075:
076: /** Is this an environment for evaluating a base clause?
077: */
078: public boolean baseClause = false;
079:
080: /** Create an outermost environment for a given (toplevel)tree,
081: * with a given info field.
082: */
083: public Env(JCTree tree, A info) {
084: this .next = null;
085: this .outer = null;
086: this .tree = tree;
087: this .toplevel = null;
088: this .enclClass = null;
089: this .enclMethod = null;
090: this .info = info;
091: }
092:
093: /** Duplicate this environment, updating with given tree and info,
094: * and copying all other fields.
095: */
096: public Env<A> dup(JCTree tree, A info) {
097: return dupto(new Env<A>(tree, info));
098: }
099:
100: /** Duplicate this environment into a given Environment,
101: * using its tree and info, and copying all other fields.
102: */
103: public Env<A> dupto(Env<A> that) {
104: that.next = this ;
105: that.outer = this .outer;
106: that.toplevel = this .toplevel;
107: that.enclClass = this .enclClass;
108: that.enclMethod = this .enclMethod;
109: return that;
110: }
111:
112: /** Duplicate this environment, updating with given tree,
113: * and copying all other fields.
114: */
115: public Env<A> dup(JCTree tree) {
116: return dup(tree, this .info);
117: }
118:
119: /** Return closest enclosing environment which points to a tree with given tag.
120: */
121: public Env<A> enclosing(int tag) {
122: Env<A> env1 = this ;
123: while (env1 != null && env1.tree.getTag() != tag)
124: env1 = env1.next;
125: return env1;
126: }
127:
128: public String toString() {
129: return "Env[" + info + (outer == null ? "" : ",outer=" + outer)
130: + "]";
131: }
132:
133: public Iterator<Env<A>> iterator() {
134: return new Iterator<Env<A>>() {
135: Env<A> next = Env.this ;
136:
137: public boolean hasNext() {
138: return next.outer != null;
139: }
140:
141: public Env<A> next() {
142: if (hasNext()) {
143: Env<A> current = next;
144: next = current.outer;
145: return current;
146: }
147: throw new NoSuchElementException();
148:
149: }
150:
151: public void remove() {
152: throw new UnsupportedOperationException();
153: }
154: };
155: }
156: }
|