KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > groovy > ast > VariableScope


1 /*
2  $Id: VariableScope.java,v 1.3 2004/07/10 03:31:38 bran Exp $
3
4  Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
5
6  Redistribution and use of this software and associated documentation
7  ("Software"), with or without modification, are permitted provided
8  that the following conditions are met:
9
10  1. Redistributions of source code must retain copyright
11     statements and notices. Redistributions must also contain a
12     copy of this document.
13
14  2. Redistributions in binary form must reproduce the
15     above copyright notice, this list of conditions and the
16     following disclaimer in the documentation and/or other
17     materials provided with the distribution.
18
19  3. The name "groovy" must not be used to endorse or promote
20     products derived from this Software without prior written
21     permission of The Codehaus. For written permission,
22     please contact info@codehaus.org.
23
24  4. Products derived from this Software may not be called "groovy"
25     nor may "groovy" appear in their names without prior written
26     permission of The Codehaus. "groovy" is a registered
27     trademark of The Codehaus.
28
29  5. Due credit should be given to The Codehaus -
30     http://groovy.codehaus.org/
31
32  THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
33  ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
34  NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
35  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
36  THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
37  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
43  OF THE POSSIBILITY OF SUCH DAMAGE.
44
45  */

46 package org.codehaus.groovy.ast;
47
48 import java.util.ArrayList JavaDoc;
49 import java.util.HashSet JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.List JavaDoc;
52 import java.util.Set JavaDoc;
53
54 /**
55  * Represents a variable scope. This is primarily used to determine variable sharing
56  * across method and closure boundaries.
57  *
58  * @author <a HREF="mailto:james@coredevelopers.net">James Strachan</a>
59  * @version $Revision: 1.3 $
60  */

61 public class VariableScope {
62     static int i = 0;
63     private Set JavaDoc declaredVariables = new HashSet JavaDoc(); // contain var names
64
private Set JavaDoc referencedVariables = new HashSet JavaDoc(); // contain var names
65

66     /**
67      * br contain vars really declared and defined in the current scope.
68      * to be filler later by comparing with parent scope all the ancestors.
69      */

70     private Set JavaDoc varsDeclaredHere = null;
71     // each var should really have a scope decarator
72
// for now it's a naked name, which has the problem of name clashing with parameters
73
// and ancesters' vars with the same name. Effectively this means we cannot have a
74
// local var defined with the same name with any parameters or vars in any of the
75
// ancestors.
76
// we need to reengineer the scope and vars framework.
77

78     private VariableScope parent;
79     private List JavaDoc children = new ArrayList JavaDoc();
80     String JavaDoc name = null;
81
82     public VariableScope() {
83         name = String.valueOf(i++); // should give it a more meaningful name, w/ class, methed, blockstatement etc.
84
}
85
86     public VariableScope(VariableScope parent) {
87         this.parent = parent;
88         parent.children.add(this);
89         name = String.valueOf(i);
90     }
91
92     public Set JavaDoc getDeclaredVariables() {
93         return declaredVariables;
94     }
95
96     public Set JavaDoc getReferencedVariables() {
97         return referencedVariables;
98     }
99
100     /**
101      * @return all the child scopes
102      */

103     public List JavaDoc getChildren() {
104         return children;
105     }
106
107     /**
108      * Creates a composite variable scope combining all the variable references
109      * and declarations from all the child scopes not including this scope
110      *
111      * @return
112      */

113     public VariableScope createCompositeChildScope() {
114         VariableScope answer = new VariableScope();
115         for (Iterator JavaDoc iter = children.iterator(); iter.hasNext(); ) {
116             answer.appendRecursive((VariableScope) iter.next());
117         }
118         answer.parent = this; // shall we cache this for better performance?
119
return answer;
120     }
121
122     /**
123      * Creates a scope including this scope and all nested scopes combined together
124      *
125      * @return
126      */

127     public VariableScope createRecursiveChildScope() {
128         VariableScope answer = createCompositeChildScope();
129         answer.referencedVariables.addAll(referencedVariables);
130         answer.declaredVariables.addAll(declaredVariables);
131         return answer;
132     }
133
134     /**
135      * Creates a scope including this scope and all parent scopes combined together
136      *
137      * @return
138      */

139     public VariableScope createRecursiveParentScope() {
140         VariableScope answer = new VariableScope();
141         VariableScope node = this;
142         do {
143             answer.append(node);
144             node = node.parent;
145         }
146         while (node != null);
147         return answer;
148     }
149
150     /**
151      * Appends all of the references and declarations from the given scope
152      * to this one
153      *
154      * @param scope
155      */

156     protected void append(VariableScope scope) {
157         referencedVariables.addAll(scope.referencedVariables);
158         declaredVariables.addAll(scope.declaredVariables);
159     }
160
161     /**
162      * Appends all of the references and declarations from the given scope
163      * and all its children to this one
164      *
165      * @param scope
166      */

167     protected void appendRecursive(VariableScope scope) {
168         append(scope);
169
170         // now lets traverse the children
171
for (Iterator JavaDoc iter = scope.children.iterator(); iter.hasNext(); ) {
172             appendRecursive((VariableScope) iter.next());
173         }
174      }
175
176     /*
177      * compute the real declares in the current scope
178      * @author bran
179      *
180      * To change the template for this generated type comment go to
181      * Window - Preferences - Java - Code Generation - Code and Comments
182      */

183     public void computeRealDeclares() {
184         if (this.varsDeclaredHere == null) this.varsDeclaredHere = new HashSet JavaDoc();
185         Set JavaDoc decls = this.declaredVariables;
186         for (Iterator JavaDoc iter = decls.iterator(); iter.hasNext();) {
187             String JavaDoc var = (String JavaDoc) iter.next();
188             // simplistic way of determing declares by simple name matching
189
if (!createRecursiveParentScope().getDeclaredVariables().contains(var)) {
190                 this.varsDeclaredHere.add(var);
191             }
192         }
193     }
194     /**
195      * @return Returns the varsDeclaredHere.
196      */

197     public Set JavaDoc getVarsDeclaredHere() {
198         if (this.varsDeclaredHere == null) computeRealDeclares();
199         return varsDeclaredHere;
200     }
201     /**
202      * @param varsDeclaredHere The varsDeclaredHere to set.
203      */

204     public void setVarsDeclaredHere(Set JavaDoc varsDeclaredHere) {
205         this.varsDeclaredHere = varsDeclaredHere;
206     }
207 }
208
Popular Tags