1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
\r
3 * The contents of this file are subject to the Netscape Public
\r
4 * License Version 1.1 (the "License"); you may not use this file
\r
5 * except in compliance with the License. You may obtain a copy of
\r
6 * the License at http://www.mozilla.org/NPL/
\r
8 * Software distributed under the License is distributed on an "AS
\r
9 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr
\r
10 * implied. See the License for the specific language governing
\r
11 * rights and limitations under the License.
\r
13 * The Original Code is Rhino code, released
\r
16 * The Initial Developer of the Original Code is Netscape
\r
17 * Communications Corporation. Portions created by Netscape are
\r
18 * Copyright (C) 1997-1999 Netscape Communications Corporation. All
\r
24 * Alternatively, the contents of this file may be used under the
\r
25 * terms of the GNU Public License (the "GPL"), in which case the
\r
26 * provisions of the GPL are applicable instead of those above.
\r
27 * If you wish to allow use of your version of this file only
\r
28 * under the terms of the GPL and not to allow others to use your
\r
29 * version of this file under the NPL, indicate your decision by
\r
30 * deleting the provisions above and replace them with the notice
\r
31 * and other provisions required by the GPL. If you do not delete
\r
32 * the provisions above, a recipient may use your version of this
\r
33 * file under either the NPL or the GPL.
\r
36 package org.mozilla.javascript;
\r
41 public class VariableTable {
\r
44 return itsVariables.size();
\r
47 public int getParameterCount(){
\r
51 public LocalVariable createLocalVariable(String name, boolean isParameter)
\r
53 return new LocalVariable(name, isParameter);
\r
56 public LocalVariable getVariable(int index) {
\r
57 return (LocalVariable)(itsVariables.elementAt(index));
\r
60 public LocalVariable getVariable(String name) {
\r
61 Integer vIndex = (Integer)(itsVariableNames.get(name));
\r
63 return (LocalVariable)(itsVariables.elementAt(vIndex.intValue()));
\r
68 public int getOrdinal(String name) {
\r
69 Integer vIndex = (Integer)(itsVariableNames.get(name));
\r
71 return vIndex.intValue();
\r
76 public String getName(int index) {
\r
77 return ((LocalVariable)(itsVariables.elementAt(index))).getName();
\r
80 public String[] getAllNames() {
\r
82 String[] result = null;
\r
84 result = new String[N];
\r
85 for (int i = 0; i != N; i++) {
\r
86 result[i] = getName(i);
\r
92 public void establishIndices() {
\r
93 for (int i = 0; i < itsVariables.size(); i++) {
\r
94 LocalVariable lVar = (LocalVariable)(itsVariables.elementAt(i));
\r
99 public void addParameter(String pName) {
\r
100 Integer pIndex = (Integer)(itsVariableNames.get(pName));
\r
101 if (pIndex != null) {
\r
102 LocalVariable p = (LocalVariable)
\r
103 (itsVariables.elementAt(pIndex.intValue()));
\r
104 if (p.isParameter()) {
\r
105 String message = Context.getMessage1("msg.dup.parms", pName);
\r
106 Context.reportWarning(message, null, 0, null, 0);
\r
108 else { // there's a local variable with this name, blow it off
\r
109 itsVariables.removeElementAt(pIndex.intValue());
\r
112 int curIndex = varStart++;
\r
113 LocalVariable lVar = createLocalVariable(pName, true);
\r
114 itsVariables.insertElementAt(lVar, curIndex);
\r
115 itsVariableNames.put(pName, new Integer(curIndex));
\r
118 public void addLocal(String vName) {
\r
119 Integer vIndex = (Integer)(itsVariableNames.get(vName));
\r
120 if (vIndex != null) {
\r
121 // There's already a variable or parameter with this name.
\r
124 int index = itsVariables.size();
\r
125 LocalVariable lVar = createLocalVariable(vName, false);
\r
126 itsVariables.addElement(lVar);
\r
127 itsVariableNames.put(vName, new Integer(index));
\r
130 // This should only be called very early in compilation
\r
131 public void removeLocal(String name) {
\r
132 Integer i = (Integer) itsVariableNames.get(name);
\r
134 itsVariables.removeElementAt(i.intValue());
\r
135 itsVariableNames.remove(name);
\r
136 Hashtable ht = new Hashtable(11);
\r
137 Enumeration e = itsVariableNames.keys();
\r
138 while (e.hasMoreElements()) {
\r
139 Object k = e.nextElement();
\r
140 Integer v = (Integer) itsVariableNames.get(k);
\r
141 int v2 = v.intValue();
\r
142 if (v2 > i.intValue())
\r
143 v = new Integer(v2 - 1);
\r
146 itsVariableNames = ht;
\r
150 // a list of the formal parameters and local variables
\r
151 protected Vector itsVariables = new Vector();
\r
153 // mapping from name to index in list
\r
154 protected Hashtable itsVariableNames = new Hashtable(11);
\r
156 protected int varStart; // index in list of first variable
\r