Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/Scope.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript.ast;
8
9 import org.mozilla.javascript.Node;
10 import org.mozilla.javascript.Token;
11
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.LinkedHashMap;
15 import java.util.List;
16 import java.util.Map;
17
18 /**
19 * Represents a scope in the lexical scope chain. Base type for
20 * all {@link AstNode} implementations that can introduce a new scope.
21 */
22 public class Scope extends Jump {
23
24 // Use LinkedHashMap so that the iteration order is the insertion order
25 protected Map<String,Symbol> symbolTable;
26 protected Scope parentScope;
27 protected ScriptNode top; // current script or function scope
28
29 private List<Scope> childScopes;
30
31 {
32 this.type = Token.BLOCK;
33 }
34
35 public Scope() {
36 }
37
38 public Scope(int pos) {
39 this.position = pos;
40 }
41
42 public Scope(int pos, int len) {
43 this(pos);
44 this.length = len;
45 }
46
47 public Scope getParentScope() {
48 return parentScope;
49 }
50
51 /**
52 * Sets parent scope
53 */
54 public void setParentScope(Scope parentScope) {
55 this.parentScope = parentScope;
56 this.top = parentScope == null ? (ScriptNode)this : parentScope.top;
57 }
58
59 /**
60 * Used only for code generation.
61 */
62 public void clearParentScope() {
63 this.parentScope = null;
64 }
65
66 /**
67 * Return a list of the scopes whose parent is this scope.
68 * @return the list of scopes we enclose, or {@code null} if none
69 */
70 public List<Scope> getChildScopes() {
71 return childScopes;
72 }
73
74 /**
75 * Add a scope to our list of child scopes.
76 * Sets the child's parent scope to this scope.
77 * @throws IllegalStateException if the child's parent scope is
78 * non-{@code null}
79 */
80 public void addChildScope(Scope child) {
81 if (childScopes == null) {
82 childScopes = new ArrayList<Scope>();
83 }
84 childScopes.add(child);
85 child.setParentScope(this);
86 }
87
88 /**
89 * Used by the parser; not intended for typical use.
90 * Changes the parent-scope links for this scope's child scopes
91 * to the specified new scope. Copies symbols from this scope
92 * into new scope.
93 *
94 * @param newScope the scope that will replace this one on the
95 * scope stack.
96 */
97 public void replaceWith(Scope newScope) {
98 if (childScopes != null) {
99 for (Scope kid : childScopes) {
100 newScope.addChildScope(kid); // sets kid's parent
101 }
102 childScopes.clear();
103 childScopes = null;
104 }
105 if (symbolTable != null && !symbolTable.isEmpty()) {
106 joinScopes(this, newScope);
107 }
108 }
109
110 /**
111 * Returns current script or function scope
112 */
113 public ScriptNode getTop() {
114 return top;
115 }
116
117 /**
118 * Sets top current script or function scope
119 */
120 public void setTop(ScriptNode top) {
121 this.top = top;
122 }
123
124 /**
125 * Creates a new scope node, moving symbol table information
126 * from "scope" to the new node, and making "scope" a nested
127 * scope contained by the new node.
128 * Useful for injecting a new scope in a scope chain.
129 */
130 public static Scope splitScope(Scope scope) {
131 Scope result = new Scope(scope.getType());
132 result.symbolTable = scope.symbolTable;
133 scope.symbolTable = null;
134 result.parent = scope.parent;
135 result.setParentScope(scope.getParentScope());
136 result.setParentScope(result);
137 scope.parent = result;
138 result.top = scope.top;
139 return result;
140 }
141
142 /**
143 * Copies all symbols from source scope to dest scope.
144 */
145 public static void joinScopes(Scope source, Scope dest) {
146 Map<String,Symbol> src = source.ensureSymbolTable();
147 Map<String,Symbol> dst = dest.ensureSymbolTable();
148 if (!Collections.disjoint(src.keySet(), dst.keySet())) {
149 codeBug();
150 }
151 for (Map.Entry<String, Symbol> entry: src.entrySet()) {
152 Symbol sym = entry.getValue();
153 sym.setContainingTable(dest);
154 dst.put(entry.getKey(), sym);
155 }
156 }
157
158 /**
159 * Returns the scope in which this name is defined
160 * @param name the symbol to look up
161 * @return this {@link Scope}, one of its parent scopes, or {@code null} if
162 * the name is not defined any this scope chain
163 */
164 public Scope getDefiningScope(String name) {
165 for (Scope s = this; s != null; s = s.parentScope) {
166 Map<String,Symbol> symbolTable = s.getSymbolTable();
167 if (symbolTable != null && symbolTable.containsKey(name)) {
168 return s;
169 }
170 }
171 return null;
172 }
173
174 /**
175 * Looks up a symbol in this scope.
176 * @param name the symbol name
177 * @return the Symbol, or {@code null} if not found
178 */
179 public Symbol getSymbol(String name) {
180 return symbolTable == null ? null : symbolTable.get(name);
181 }
182
183 /**
184 * Enters a symbol into this scope.
185 */
186 public void putSymbol(Symbol symbol) {
187 if (symbol.getName() == null)
188 throw new IllegalArgumentException("null symbol name");
189 ensureSymbolTable();
190 symbolTable.put(symbol.getName(), symbol);
191 symbol.setContainingTable(this);
192 top.addSymbol(symbol);
193 }
194
195 /**
196 * Returns the symbol table for this scope.
197 * @return the symbol table. May be {@code null}.
198 */
199 public Map<String,Symbol> getSymbolTable() {
200 return symbolTable;
201 }
202
203 /**
204 * Sets the symbol table for this scope. May be {@code null}.
205 */
206 public void setSymbolTable(Map<String, Symbol> table) {
207 symbolTable = table;
208 }
209
210 private Map<String,Symbol> ensureSymbolTable() {
211 if (symbolTable == null) {
212 symbolTable = new LinkedHashMap<String,Symbol>(5);
213 }
214 return symbolTable;
215 }
216
217 /**
218 * Returns a copy of the child list, with each child cast to an
219 * {@link AstNode}.
220 * @throws ClassCastException if any non-{@code AstNode} objects are
221 * in the child list, e.g. if this method is called after the code
222 * generator begins the tree transformation.
223 */
224 public List<AstNode> getStatements() {
225 List<AstNode> stmts = new ArrayList<AstNode>();
226 Node n = getFirstChild();
227 while (n != null) {
228 stmts.add((AstNode)n);
229 n = n.getNext();
230 }
231 return stmts;
232 }
233
234 @Override
235 public String toSource(int depth) {
236 StringBuilder sb = new StringBuilder();
237 sb.append(makeIndent(depth));
238 sb.append("{\n");
239 for (Node kid : this) {
240 sb.append(((AstNode)kid).toSource(depth+1));
241 }
242 sb.append(makeIndent(depth));
243 sb.append("}\n");
244 return sb.toString();
245 }
246
247 @Override
248 public void visit(NodeVisitor v) {
249 if (v.visit(this)) {
250 for (Node kid : this) {
251 ((AstNode)kid).visit(v);
252 }
253 }
254 }
255 }