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: *
028: * Alternatively, the contents of this file may be used under the terms of
029: * the GNU General Public License Version 2 or later (the "GPL"), in which
030: * case the provisions of the GPL are applicable instead of those above. If
031: * you wish to allow use of your version of this file only under the terms of
032: * the GPL and not to allow others to use your version of this file under the
033: * MPL, indicate your decision by deleting the provisions above and replacing
034: * them with the notice and other provisions required by the GPL. If you do
035: * not delete the provisions above, a recipient may use your version of this
036: * file under either the MPL or the GPL.
037: *
038: * ***** END LICENSE BLOCK ***** */
039:
040: package org.mozilla.javascript;
041:
042: public class ScriptOrFnNode extends Node {
043:
044: public ScriptOrFnNode(int nodeType) {
045: super (nodeType);
046: }
047:
048: public final String getSourceName() {
049: return sourceName;
050: }
051:
052: public final void setSourceName(String sourceName) {
053: this .sourceName = sourceName;
054: }
055:
056: public final int getEncodedSourceStart() {
057: return encodedSourceStart;
058: }
059:
060: public final int getEncodedSourceEnd() {
061: return encodedSourceEnd;
062: }
063:
064: public final void setEncodedSourceBounds(int start, int end) {
065: this .encodedSourceStart = start;
066: this .encodedSourceEnd = end;
067: }
068:
069: public final int getBaseLineno() {
070: return baseLineno;
071: }
072:
073: public final void setBaseLineno(int lineno) {
074: // One time action
075: if (lineno < 0 || baseLineno >= 0)
076: Kit.codeBug();
077: baseLineno = lineno;
078: }
079:
080: public final int getEndLineno() {
081: return endLineno;
082: }
083:
084: public final void setEndLineno(int lineno) {
085: // One time action
086: if (lineno < 0 || endLineno >= 0)
087: Kit.codeBug();
088: endLineno = lineno;
089: }
090:
091: public final int getFunctionCount() {
092: if (functions == null) {
093: return 0;
094: }
095: return functions.size();
096: }
097:
098: public final FunctionNode getFunctionNode(int i) {
099: return (FunctionNode) functions.get(i);
100: }
101:
102: public final int addFunction(FunctionNode fnNode) {
103: // <netbeans>
104: if (true) {
105: // I'm adding the function nodes directly
106: return -1;
107: }
108: // </netbeans>
109: if (fnNode == null)
110: Kit.codeBug();
111: if (functions == null) {
112: functions = new ObjArray();
113: }
114: functions.add(fnNode);
115: return functions.size() - 1;
116: }
117:
118: public final int getRegexpCount() {
119: if (regexps == null) {
120: return 0;
121: }
122: return regexps.size() / 2;
123: }
124:
125: public final String getRegexpString(int index) {
126: return (String) regexps.get(index * 2);
127: }
128:
129: public final String getRegexpFlags(int index) {
130: return (String) regexps.get(index * 2 + 1);
131: }
132:
133: public final int addRegexp(String string, String flags) {
134: if (string == null)
135: Kit.codeBug();
136: if (regexps == null) {
137: regexps = new ObjArray();
138: }
139: regexps.add(string);
140: regexps.add(flags);
141: return regexps.size() / 2 - 1;
142: }
143:
144: public final boolean hasParamOrVar(String name) {
145: return itsVariableNames.has(name);
146: }
147:
148: public final int getParamOrVarIndex(String name) {
149: return itsVariableNames.get(name, -1);
150: }
151:
152: public final String getParamOrVarName(int index) {
153: return (String) itsVariables.get(index);
154: }
155:
156: public final int getParamCount() {
157: return varStart;
158: }
159:
160: public final int getParamAndVarCount() {
161: return itsVariables.size();
162: }
163:
164: public final String[] getParamAndVarNames() {
165: int N = itsVariables.size();
166: if (N == 0) {
167: return ScriptRuntime.emptyStrings;
168: }
169: String[] array = new String[N];
170: itsVariables.toArray(array);
171: return array;
172: }
173:
174: public final boolean[] getParamAndVarConst() {
175: int N = itsVariables.size();
176: boolean[] array = new boolean[N];
177: for (int i = 0; i < N; i++)
178: if (itsConst.get(i) != null)
179: array[i] = true;
180: return array;
181: }
182:
183: public final void addParam(String name) {
184: // Check addparam is not called after addLocal
185: if (varStart != itsVariables.size())
186: Kit.codeBug();
187: // Allow non-unique parameter names: use the last occurrence (parser
188: // will warn about dups)
189: int index = varStart++;
190: itsVariables.add(name);
191: itsConst.add(null);
192: itsVariableNames.put(name, index);
193: }
194:
195: public static final int NO_DUPLICATE = 1;
196: public static final int DUPLICATE_VAR = 0;
197: public static final int DUPLICATE_PARAMETER = -1;
198: public static final int DUPLICATE_CONST = -2;
199:
200: /**
201: * This function adds a variable to the set of var declarations for a
202: * function (or script). This returns an indicator of a duplicate that
203: * overrides a formal parameter (false if this dups a parameter).
204: * @param name variable name
205: * @return 1 if the name is not any form of duplicate, 0 if it duplicates a
206: * non-parameter, -1 if it duplicates a parameter and -2 if it duplicates a
207: * const.
208: */
209: public final int addVar(String name) {
210: int vIndex = itsVariableNames.get(name, -1);
211: if (vIndex != -1) {
212: // There's already a variable or parameter with this name.
213: if (vIndex >= varStart) {
214: Object v = itsConst.get(vIndex);
215: if (v != null)
216: return DUPLICATE_CONST;
217: else
218: return DUPLICATE_VAR;
219: } else
220: return DUPLICATE_PARAMETER;
221: }
222: int index = itsVariables.size();
223: itsVariables.add(name);
224: itsConst.add(null);
225: itsVariableNames.put(name, index);
226: return NO_DUPLICATE;
227: }
228:
229: public final boolean addConst(String name) {
230: int vIndex = itsVariableNames.get(name, -1);
231: if (vIndex != -1) {
232: // There's already a variable or parameter with this name.
233: return false;
234: }
235: int index = itsVariables.size();
236: itsVariables.add(name);
237: itsConst.add(name);
238: itsVariableNames.put(name, index);
239: return true;
240: }
241:
242: public final void removeParamOrVar(String name) {
243: int i = itsVariableNames.get(name, -1);
244: if (i != -1) {
245: itsVariables.remove(i);
246: itsVariableNames.remove(name);
247: ObjToIntMap.Iterator iter = itsVariableNames.newIterator();
248: for (iter.start(); !iter.done(); iter.next()) {
249: int v = iter.getValue();
250: if (v > i) {
251: iter.setValue(v - 1);
252: }
253: }
254: }
255: }
256:
257: public final Object getCompilerData() {
258: return compilerData;
259: }
260:
261: public final void setCompilerData(Object data) {
262: if (data == null)
263: throw new IllegalArgumentException();
264: // Can only call once
265: if (compilerData != null)
266: throw new IllegalStateException();
267: compilerData = data;
268: }
269:
270: private int encodedSourceStart;
271: private int encodedSourceEnd;
272: private String sourceName;
273: private int baseLineno = -1;
274: private int endLineno = -1;
275:
276: private ObjArray functions;
277:
278: private ObjArray regexps;
279:
280: // a list of the formal parameters and local variables
281: private ObjArray itsVariables = new ObjArray();
282: private ObjArray itsConst = new ObjArray();
283:
284: // mapping from name to index in list
285: private ObjToIntMap itsVariableNames = new ObjToIntMap(11);
286:
287: private int varStart; // index in list of first variable
288:
289: private Object compilerData;
290: }
|