1 | /* |
2 | * Copyright 1999-2006 Sun Microsystems, Inc. All Rights Reserved. |
3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | * |
5 | * This code is free software; you can redistribute it and/or modify it |
6 | * under the terms of the GNU General Public License version 2 only, as |
7 | * published by the Free Software Foundation. Sun designates this |
8 | * particular file as subject to the "Classpath" exception as provided |
9 | * by Sun in the LICENSE file that accompanied this code. |
10 | * |
11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 | * version 2 for more details (a copy is included in the LICENSE file that |
15 | * accompanied this code). |
16 | * |
17 | * You should have received a copy of the GNU General Public License version |
18 | * 2 along with this work; if not, write to the Free Software Foundation, |
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
20 | * |
21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
23 | * have any questions. |
24 | */ |
25 | |
26 | package com.sun.tools.javac.comp; |
27 | |
28 | import com.sun.tools.javac.util.*; |
29 | import com.sun.tools.javac.tree.*; |
30 | import java.util.Iterator; |
31 | import java.util.NoSuchElementException; |
32 | |
33 | /** A class for environments, instances of which are passed as |
34 | * arguments to tree visitors. Environments refer to important ancestors |
35 | * of the subtree that's currently visited, such as the enclosing method, |
36 | * the enclosing class, or the enclosing toplevel node. They also contain |
37 | * a generic component, represented as a type parameter, to carry further |
38 | * information specific to individual passes. |
39 | * |
40 | * <p><b>This is NOT part of any API supported by Sun Microsystems. If |
41 | * you write code that depends on this, you do so at your own risk. |
42 | * This code and its internal interfaces are subject to change or |
43 | * deletion without notice.</b> |
44 | */ |
45 | public class Env<A> implements Iterable<Env<A>> { |
46 | |
47 | /** The next enclosing environment. |
48 | */ |
49 | public Env<A> next; |
50 | |
51 | /** The environment enclosing the current class. |
52 | */ |
53 | public Env<A> outer; |
54 | |
55 | /** The tree with which this environment is associated. |
56 | */ |
57 | public JCTree tree; |
58 | |
59 | /** The enclosing toplevel tree. |
60 | */ |
61 | public JCTree.JCCompilationUnit toplevel; |
62 | |
63 | /** The next enclosing class definition. |
64 | */ |
65 | public JCTree.JCClassDecl enclClass; |
66 | |
67 | /** The next enclosing method definition. |
68 | */ |
69 | public JCTree.JCMethodDecl enclMethod; |
70 | |
71 | /** A generic field for further information. |
72 | */ |
73 | public A info; |
74 | |
75 | /** Is this an environment for evaluating a base clause? |
76 | */ |
77 | public boolean baseClause = false; |
78 | |
79 | /** Create an outermost environment for a given (toplevel)tree, |
80 | * with a given info field. |
81 | */ |
82 | public Env(JCTree tree, A info) { |
83 | this.next = null; |
84 | this.outer = null; |
85 | this.tree = tree; |
86 | this.toplevel = null; |
87 | this.enclClass = null; |
88 | this.enclMethod = null; |
89 | this.info = info; |
90 | } |
91 | |
92 | /** Duplicate this environment, updating with given tree and info, |
93 | * and copying all other fields. |
94 | */ |
95 | public Env<A> dup(JCTree tree, A info) { |
96 | return dupto(new Env<A>(tree, info)); |
97 | } |
98 | |
99 | /** Duplicate this environment into a given Environment, |
100 | * using its tree and info, and copying all other fields. |
101 | */ |
102 | public Env<A> dupto(Env<A> that) { |
103 | that.next = this; |
104 | that.outer = this.outer; |
105 | that.toplevel = this.toplevel; |
106 | that.enclClass = this.enclClass; |
107 | that.enclMethod = this.enclMethod; |
108 | return that; |
109 | } |
110 | |
111 | /** Duplicate this environment, updating with given tree, |
112 | * and copying all other fields. |
113 | */ |
114 | public Env<A> dup(JCTree tree) { |
115 | return dup(tree, this.info); |
116 | } |
117 | |
118 | /** Return closest enclosing environment which points to a tree with given tag. |
119 | */ |
120 | public Env<A> enclosing(int tag) { |
121 | Env<A> env1 = this; |
122 | while (env1 != null && env1.tree.getTag() != tag) env1 = env1.next; |
123 | return env1; |
124 | } |
125 | |
126 | public String toString() { |
127 | return "Env[" + info + (outer == null ? "" : ",outer=" + outer) + "]"; |
128 | } |
129 | |
130 | public Iterator<Env<A>> iterator() { |
131 | return new Iterator<Env<A>>() { |
132 | Env<A> next = Env.this; |
133 | public boolean hasNext() { |
134 | return next.outer != null; |
135 | } |
136 | public Env<A> next() { |
137 | if (hasNext()) { |
138 | Env<A> current = next; |
139 | next = current.outer; |
140 | return current; |
141 | } |
142 | throw new NoSuchElementException(); |
143 | |
144 | } |
145 | public void remove() { |
146 | throw new UnsupportedOperationException(); |
147 | } |
148 | }; |
149 | } |
150 | } |