Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/ScriptNode.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.List;
15
16 /**
17 * Base type for {@link AstRoot} and {@link FunctionNode} nodes, which need to
18 * collect much of the same information.
19 */
20 public class ScriptNode extends Scope {
21
22 private int encodedSourceStart = -1;
23 private int encodedSourceEnd = -1;
24 private String sourceName;
25 private String encodedSource;
26 private int endLineno = -1;
27
28 private List<FunctionNode> functions;
29 private List<RegExpLiteral> regexps;
30 private List<FunctionNode> EMPTY_LIST = Collections.emptyList();
31
32 private List<Symbol> symbols = new ArrayList<Symbol>(4);
33 private int paramCount = 0;
34 private String[] variableNames;
35 private boolean[] isConsts;
36
37 private Object compilerData;
38 private int tempNumber = 0;
39 private boolean inStrictMode;
40
41 {
42 // during parsing, a ScriptNode or FunctionNode's top scope is itself
43 this.top = this;
44 this.type = Token.SCRIPT;
45 }
46
47 public ScriptNode() {
48 }
49
50 public ScriptNode(int pos) {
51 super(pos);
52 }
53
54 /**
55 * Returns the URI, path or descriptive text indicating the origin
56 * of this script's source code.
57 */
58 public String getSourceName() {
59 return sourceName;
60 }
61
62 /**
63 * Sets the URI, path or descriptive text indicating the origin
64 * of this script's source code.
65 */
66 public void setSourceName(String sourceName) {
67 this.sourceName = sourceName;
68 }
69
70 /**
71 * Returns the start offset of the encoded source.
72 * Only valid if {@link #getEncodedSource} returns non-{@code null}.
73 */
74 public int getEncodedSourceStart() {
75 return encodedSourceStart;
76 }
77
78 /**
79 * Used by code generator.
80 * @see #getEncodedSource
81 */
82 public void setEncodedSourceStart(int start) {
83 this.encodedSourceStart = start;
84 }
85
86 /**
87 * Returns the end offset of the encoded source.
88 * Only valid if {@link #getEncodedSource} returns non-{@code null}.
89 */
90 public int getEncodedSourceEnd() {
91 return encodedSourceEnd;
92 }
93
94 /**
95 * Used by code generator.
96 * @see #getEncodedSource
97 */
98 public void setEncodedSourceEnd(int end) {
99 this.encodedSourceEnd = end;
100 }
101
102 /**
103 * Used by code generator.
104 * @see #getEncodedSource
105 */
106 public void setEncodedSourceBounds(int start, int end) {
107 this.encodedSourceStart = start;
108 this.encodedSourceEnd = end;
109 }
110
111 /**
112 * Used by the code generator.
113 * @see #getEncodedSource
114 */
115 public void setEncodedSource(String encodedSource) {
116 this.encodedSource = encodedSource;
117 }
118
119 /**
120 * Returns a canonical version of the source for this script or function,
121 * for use in implementing the {@code Object.toSource} method of
122 * JavaScript objects. This source encoding is only recorded during code
123 * generation. It must be passed back to
124 * {@link org.mozilla.javascript.Decompiler#decompile} to construct the
125 * human-readable source string.<p>
126 *
127 * Given a parsed AST, you can always convert it to source code using the
128 * {@link AstNode#toSource} method, although it's not guaranteed to produce
129 * exactly the same results as {@code Object.toSource} with respect to
130 * formatting, parenthesization and other details.
131 *
132 * @return the encoded source, or {@code null} if it was not recorded.
133 */
134 public String getEncodedSource() {
135 return encodedSource;
136 }
137
138 public int getBaseLineno() {
139 return lineno;
140 }
141
142 /**
143 * Sets base (starting) line number for this script or function.
144 * This is a one-time operation, and throws an exception if the
145 * line number has already been set.
146 */
147 public void setBaseLineno(int lineno) {
148 if (lineno < 0 || this.lineno >= 0) codeBug();
149 this.lineno = lineno;
150 }
151
152 public int getEndLineno() {
153 return endLineno;
154 }
155
156 public void setEndLineno(int lineno) {
157 // One time action
158 if (lineno < 0 || endLineno >= 0) codeBug();
159 endLineno = lineno;
160 }
161
162 public int getFunctionCount() {
163 return functions == null ? 0 : functions.size();
164 }
165
166 public FunctionNode getFunctionNode(int i) {
167 return functions.get(i);
168 }
169
170 public List<FunctionNode> getFunctions() {
171 return functions == null ? EMPTY_LIST : functions;
172 }
173
174 /**
175 * Adds a {@link FunctionNode} to the functions table for codegen.
176 * Does not set the parent of the node.
177 * @return the index of the function within its parent
178 */
179 public int addFunction(FunctionNode fnNode) {
180 if (fnNode == null) codeBug();
181 if (functions == null)
182 functions = new ArrayList<FunctionNode>();
183 functions.add(fnNode);
184 return functions.size() - 1;
185 }
186
187 public int getRegexpCount() {
188 return regexps == null ? 0 : regexps.size();
189 }
190
191 public String getRegexpString(int index) {
192 return regexps.get(index).getValue();
193 }
194
195 public String getRegexpFlags(int index) {
196 return regexps.get(index).getFlags();
197 }
198
199 /**
200 * Called by IRFactory to add a RegExp to the regexp table.
201 */
202 public void addRegExp(RegExpLiteral re) {
203 if (re == null) codeBug();
204 if (regexps == null)
205 regexps = new ArrayList<RegExpLiteral>();
206 regexps.add(re);
207 re.putIntProp(REGEXP_PROP, regexps.size() - 1);
208 }
209
210 public int getIndexForNameNode(Node nameNode) {
211 if (variableNames == null) codeBug();
212 Scope node = nameNode.getScope();
213 Symbol symbol = node == null
214 ? null
215 : node.getSymbol(((Name)nameNode).getIdentifier());
216 return (symbol == null) ? -1 : symbol.getIndex();
217 }
218
219 public String getParamOrVarName(int index) {
220 if (variableNames == null) codeBug();
221 return variableNames[index];
222 }
223
224 public int getParamCount() {
225 return paramCount;
226 }
227
228 public int getParamAndVarCount() {
229 if (variableNames == null) codeBug();
230 return symbols.size();
231 }
232
233 public String[] getParamAndVarNames() {
234 if (variableNames == null) codeBug();
235 return variableNames;
236 }
237
238 public boolean[] getParamAndVarConst() {
239 if (variableNames == null) codeBug();
240 return isConsts;
241 }
242
243 void addSymbol(Symbol symbol) {
244 if (variableNames != null) codeBug();
245 if (symbol.getDeclType() == Token.LP) {
246 paramCount++;
247 }
248 symbols.add(symbol);
249 }
250
251 public List<Symbol> getSymbols() {
252 return symbols;
253 }
254
255 public void setSymbols(List<Symbol> symbols) {
256 this.symbols = symbols;
257 }
258
259 /**
260 * Assign every symbol a unique integer index. Generate arrays of variable
261 * names and constness that can be indexed by those indices.
262 *
263 * @param flattenAllTables if true, flatten all symbol tables,
264 * included nested block scope symbol tables. If false, just flatten the
265 * script's or function's symbol table.
266 */
267 public void flattenSymbolTable(boolean flattenAllTables) {
268 if (!flattenAllTables) {
269 List<Symbol> newSymbols = new ArrayList<Symbol>();
270 if (this.symbolTable != null) {
271 // Just replace "symbols" with the symbols in this object's
272 // symbol table. Can't just work from symbolTable map since
273 // we need to retain duplicate parameters.
274 for (int i = 0; i < symbols.size(); i++) {
275 Symbol symbol = symbols.get(i);
276 if (symbol.getContainingTable() == this) {
277 newSymbols.add(symbol);
278 }
279 }
280 }
281 symbols = newSymbols;
282 }
283 variableNames = new String[symbols.size()];
284 isConsts = new boolean[symbols.size()];
285 for (int i = 0; i < symbols.size(); i++) {
286 Symbol symbol = symbols.get(i);
287 variableNames[i] = symbol.getName();
288 isConsts[i] = symbol.getDeclType() == Token.CONST;
289 symbol.setIndex(i);
290 }
291 }
292
293 public Object getCompilerData() {
294 return compilerData;
295 }
296
297 public void setCompilerData(Object data) {
298 assertNotNull(data);
299 // Can only call once
300 if (compilerData != null)
301 throw new IllegalStateException();
302 compilerData = data;
303 }
304
305 public String getNextTempName() {
306 return "$" + tempNumber++;
307 }
308
309 public void setInStrictMode(boolean inStrictMode) {
310 this.inStrictMode = inStrictMode;
311 }
312
313 public boolean isInStrictMode() {
314 return inStrictMode;
315 }
316
317 @Override
318 public void visit(NodeVisitor v) {
319 if (v.visit(this)) {
320 for (Node kid : this) {
321 ((AstNode)kid).visit(v);
322 }
323 }
324 }
325 }