1 46 package org.codehaus.groovy.ast; 47 48 import java.util.ArrayList ; 49 import java.util.HashSet ; 50 import java.util.Iterator ; 51 import java.util.List ; 52 import java.util.Set ; 53 54 61 public class VariableScope { 62 static int i = 0; 63 private Set declaredVariables = new HashSet (); private Set referencedVariables = new HashSet (); 66 70 private Set varsDeclaredHere = null; 71 78 private VariableScope parent; 79 private List children = new ArrayList (); 80 String name = null; 81 82 public VariableScope() { 83 name = String.valueOf(i++); } 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 getDeclaredVariables() { 93 return declaredVariables; 94 } 95 96 public Set getReferencedVariables() { 97 return referencedVariables; 98 } 99 100 103 public List getChildren() { 104 return children; 105 } 106 107 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; return answer; 120 } 121 122 127 public VariableScope createRecursiveChildScope() { 128 VariableScope answer = createCompositeChildScope(); 129 answer.referencedVariables.addAll(referencedVariables); 130 answer.declaredVariables.addAll(declaredVariables); 131 return answer; 132 } 133 134 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 156 protected void append(VariableScope scope) { 157 referencedVariables.addAll(scope.referencedVariables); 158 declaredVariables.addAll(scope.declaredVariables); 159 } 160 161 167 protected void appendRecursive(VariableScope scope) { 168 append(scope); 169 170 for (Iterator iter = scope.children.iterator(); iter.hasNext(); ) { 172 appendRecursive((VariableScope) iter.next()); 173 } 174 } 175 176 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 if (!createRecursiveParentScope().getDeclaredVariables().contains(var)) { 190 this.varsDeclaredHere.add(var); 191 } 192 } 193 } 194 197 public Set getVarsDeclaredHere() { 198 if (this.varsDeclaredHere == null) computeRealDeclares(); 199 return varsDeclaredHere; 200 } 201 204 public void setVarsDeclaredHere(Set varsDeclaredHere) { 205 this.varsDeclaredHere = varsDeclaredHere; 206 } 207 } 208 | Popular Tags |