001 /*
002 $Id: VariableScope.java,v 1.5 2005/04/14 08:46:18 blackdrag Exp $
003
004 Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
005
006 Redistribution and use of this software and associated documentation
007 ("Software"), with or without modification, are permitted provided
008 that the following conditions are met:
009
010 1. Redistributions of source code must retain copyright
011 statements and notices. Redistributions must also contain a
012 copy of this document.
013
014 2. Redistributions in binary form must reproduce the
015 above copyright notice, this list of conditions and the
016 following disclaimer in the documentation and/or other
017 materials provided with the distribution.
018
019 3. The name "groovy" must not be used to endorse or promote
020 products derived from this Software without prior written
021 permission of The Codehaus. For written permission,
022 please contact info@codehaus.org.
023
024 4. Products derived from this Software may not be called "groovy"
025 nor may "groovy" appear in their names without prior written
026 permission of The Codehaus. "groovy" is a registered
027 trademark of The Codehaus.
028
029 5. Due credit should be given to The Codehaus -
030 http://groovy.codehaus.org/
031
032 THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
033 ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
034 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
035 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
036 THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
037 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
038 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
039 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
040 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
041 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
042 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
043 OF THE POSSIBILITY OF SUCH DAMAGE.
044
045 */
046 package org.codehaus.groovy.ast;
047
048 import java.util.ArrayList;
049 import java.util.HashSet;
050 import java.util.Iterator;
051 import java.util.List;
052 import java.util.Set;
053
054 /**
055 * Represents a variable scope. This is primarily used to determine variable sharing
056 * across method and closure boundaries.
057 *
058 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
059 * @version $Revision: 1.5 $
060 */
061 public class VariableScope {
062 static int i = 0;
063 private Set declaredVariables = new HashSet(); // contain var names
064 private Set referencedVariables = new HashSet(); // contain var names
065
066 /**
067 * br contain vars really declared and defined in the current scope.
068 * to be filler later by comparing with parent scope all the ancestors.
069 */
070 private Set varsDeclaredHere = null;
071 // each var should really have a scope decarator
072 // for now it's a naked name, which has the problem of name clashing with parameters
073 // and ancesters' vars with the same name. Effectively this means we cannot have a
074 // local var defined with the same name with any parameters or vars in any of the
075 // ancestors.
076 // we need to reengineer the scope and vars framework.
077
078 private VariableScope parent;
079 private List children = new ArrayList();
080 String name = null;
081
082 public VariableScope() {
083 name = String.valueOf(i++); // should give it a more meaningful name, w/ class, methed, blockstatement etc.
084 }
085
086 public VariableScope(VariableScope parent) {
087 this.parent = parent;
088 parent.children.add(this);
089 name = String.valueOf(i);
090 }
091
092 public Set getDeclaredVariables() {
093 return declaredVariables;
094 }
095
096 public Set getReferencedVariables() {
097 return referencedVariables;
098 }
099
100 /**
101 * @return all the child scopes
102 */
103 public List 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 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 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();
185 Set decls = this.declaredVariables;
186 for (Iterator iter = decls.iterator(); iter.hasNext();) {
187 String var = (String) 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 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 varsDeclaredHere) {
205 this.varsDeclaredHere = varsDeclaredHere;
206 }
207
208 public VariableScope getParent() {
209 return parent;
210 }
211 }