001: /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
002: *
003: * ***** BEGIN LICENSE BLOCK *****
004: * Version: MPL 1.1/GPL 2.0
005: *
006: * The contents of this file are subject to the Mozilla Public License Version
007: * 1.1 (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: * http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the
014: * License.
015: *
016: * The Original Code is Rhino code, released
017: * May 6, 1999.
018: *
019: * The Initial Developer of the Original Code is
020: * Netscape Communications Corporation.
021: * Portions created by the Initial Developer are Copyright (C) 1997-1999
022: * the Initial Developer. All Rights Reserved.
023: *
024: * Contributor(s):
025: * Igor Bukanov
026: * Bob Jervis
027: * Norris Boyd
028: *
029: * Alternatively, the contents of this file may be used under the terms of
030: * the GNU General Public License Version 2 or later (the "GPL"), in which
031: * case the provisions of the GPL are applicable instead of those above. If
032: * you wish to allow use of your version of this file only under the terms of
033: * the GPL and not to allow others to use your version of this file under the
034: * MPL, indicate your decision by deleting the provisions above and replacing
035: * them with the notice and other provisions required by the GPL. If you do
036: * not delete the provisions above, a recipient may use your version of this
037: * file under either the MPL or the GPL.
038: *
039: * ***** END LICENSE BLOCK ***** */
040:
041: package org.mozilla.javascript;
042:
043: import java.util.ArrayList;
044:
045: public class ScriptOrFnNode extends Node.Scope {
046:
047: public ScriptOrFnNode(int nodeType) {
048: super (nodeType);
049: symbols = new ArrayList(4);
050: setParent(null);
051: }
052:
053: public final String getSourceName() {
054: return sourceName;
055: }
056:
057: public final void setSourceName(String sourceName) {
058: this .sourceName = sourceName;
059: }
060:
061: public final int getEncodedSourceStart() {
062: return encodedSourceStart;
063: }
064:
065: public final int getEncodedSourceEnd() {
066: return encodedSourceEnd;
067: }
068:
069: public final void setEncodedSourceBounds(int start, int end) {
070: this .encodedSourceStart = start;
071: this .encodedSourceEnd = end;
072: }
073:
074: public final int getBaseLineno() {
075: return this .lineno;
076: }
077:
078: public final void setBaseLineno(int lineno) {
079: // One time action
080: if (lineno < 0 || this .lineno >= 0)
081: Kit.codeBug();
082: this .lineno = lineno;
083: }
084:
085: public final int getEndLineno() {
086: return endLineno;
087: }
088:
089: public final void setEndLineno(int lineno) {
090: // One time action
091: if (lineno < 0 || endLineno >= 0)
092: Kit.codeBug();
093: endLineno = lineno;
094: }
095:
096: public final int getFunctionCount() {
097: if (functions == null) {
098: return 0;
099: }
100: return functions.size();
101: }
102:
103: public final FunctionNode getFunctionNode(int i) {
104: return (FunctionNode) functions.get(i);
105: }
106:
107: public final int addFunction(FunctionNode fnNode) {
108: if (fnNode == null)
109: Kit.codeBug();
110: if (functions == null) {
111: functions = new ObjArray();
112: }
113: functions.add(fnNode);
114: return functions.size() - 1;
115: }
116:
117: public final int getRegexpCount() {
118: if (regexps == null) {
119: return 0;
120: }
121: return regexps.size() / 2;
122: }
123:
124: public final String getRegexpString(int index) {
125: return (String) regexps.get(index * 2);
126: }
127:
128: public final String getRegexpFlags(int index) {
129: return (String) regexps.get(index * 2 + 1);
130: }
131:
132: public final int addRegexp(String string, String flags) {
133: if (string == null)
134: Kit.codeBug();
135: if (regexps == null) {
136: regexps = new ObjArray();
137: }
138: regexps.add(string);
139: regexps.add(flags);
140: return regexps.size() / 2 - 1;
141: }
142:
143: public int getIndexForNameNode(Node nameNode) {
144: if (variableNames == null)
145: throw Kit.codeBug();
146: Node.Scope node = nameNode.getScope();
147: Symbol symbol = node == null ? null : node.getSymbol(nameNode
148: .getString());
149: if (symbol == null)
150: return -1;
151: return symbol.index;
152: }
153:
154: public final String getParamOrVarName(int index) {
155: if (variableNames == null)
156: throw Kit.codeBug();
157: return variableNames[index];
158: }
159:
160: public final int getParamCount() {
161: return paramCount;
162: }
163:
164: public final int getParamAndVarCount() {
165: if (variableNames == null)
166: throw Kit.codeBug();
167: return symbols.size();
168: }
169:
170: public final String[] getParamAndVarNames() {
171: if (variableNames == null)
172: throw Kit.codeBug();
173: return variableNames;
174: }
175:
176: public final boolean[] getParamAndVarConst() {
177: if (variableNames == null)
178: throw Kit.codeBug();
179: return isConsts;
180: }
181:
182: void addSymbol(Symbol symbol) {
183: if (variableNames != null)
184: throw Kit.codeBug();
185: if (symbol.declType == Token.LP) {
186: paramCount++;
187: }
188: symbols.add(symbol);
189: }
190:
191: /**
192: * Assign every symbol a unique integer index. Generate arrays of variable
193: * names and constness that can be indexed by those indices.
194: *
195: * @param flattenAllTables if true, flatten all symbol tables, included
196: * nested block scope symbol tables. If false, just flatten the script's
197: * or function's symbol table.
198: */
199: void flattenSymbolTable(boolean flattenAllTables) {
200: if (!flattenAllTables) {
201: ArrayList newSymbols = new ArrayList();
202: if (this .symbolTable != null) {
203: // Just replace "symbols" with the symbols in this object's
204: // symbol table. Can't just work from symbolTable map since
205: // we need to retain duplicate parameters.
206: for (int i = 0; i < symbols.size(); i++) {
207: Symbol symbol = (Symbol) symbols.get(i);
208: if (symbol.containingTable == this ) {
209: newSymbols.add(symbol);
210: }
211: }
212: }
213: symbols = newSymbols;
214: }
215: variableNames = new String[symbols.size()];
216: isConsts = new boolean[symbols.size()];
217: for (int i = 0; i < symbols.size(); i++) {
218: Symbol symbol = (Symbol) symbols.get(i);
219: variableNames[i] = symbol.name;
220: isConsts[i] = symbol.declType == Token.CONST;
221: symbol.index = i;
222: }
223: }
224:
225: public final Object getCompilerData() {
226: return compilerData;
227: }
228:
229: public final void setCompilerData(Object data) {
230: if (data == null)
231: throw new IllegalArgumentException();
232: // Can only call once
233: if (compilerData != null)
234: throw new IllegalStateException();
235: compilerData = data;
236: }
237:
238: public String getNextTempName() {
239: return "$" + tempNumber++;
240: }
241:
242: private int encodedSourceStart;
243: private int encodedSourceEnd;
244: private String sourceName;
245: private int endLineno = -1;
246:
247: private ObjArray functions;
248: private ObjArray regexps;
249:
250: private ArrayList symbols;
251: private int paramCount = 0;
252: private String[] variableNames;
253: private boolean[] isConsts;
254:
255: private Object compilerData;
256: private int tempNumber = 0;
257: }
|