2003/05/12 05:10:30
[org.ibex.core.git] / src / org / mozilla / javascript / VariableTable.java
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-\r
2  *\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
7  *\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
12  *\r
13  * The Original Code is Rhino code, released\r
14  * May 6, 1999.\r
15  *\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
19  * Rights Reserved.\r
20  *\r
21  * Contributor(s): \r
22  * Roger Lawrence\r
23  *\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
34  */\r
35 \r
36 package org.mozilla.javascript;\r
37 \r
38 import java.io.*;\r
39 import java.util.*;\r
40 \r
41 public class VariableTable {\r
42 \r
43     public int size() {\r
44         return itsVariables.size();\r
45     }\r
46 \r
47     public int getParameterCount(){\r
48         return varStart;\r
49     }\r
50     \r
51     public LocalVariable createLocalVariable(String name, boolean isParameter) \r
52     {\r
53         return new LocalVariable(name, isParameter);\r
54     }\r
55 \r
56     public LocalVariable getVariable(int index) {\r
57         return (LocalVariable)(itsVariables.elementAt(index));\r
58     }\r
59 \r
60     public LocalVariable getVariable(String name) {\r
61         Integer vIndex = (Integer)(itsVariableNames.get(name));\r
62         if (vIndex != null)\r
63             return (LocalVariable)(itsVariables.elementAt(vIndex.intValue()));\r
64         else\r
65             return null;\r
66     }\r
67 \r
68     public int getOrdinal(String name) {\r
69         Integer vIndex = (Integer)(itsVariableNames.get(name));\r
70         if (vIndex != null)\r
71             return vIndex.intValue();\r
72         else\r
73             return -1;\r
74     }\r
75 \r
76     public String getName(int index) {\r
77         return ((LocalVariable)(itsVariables.elementAt(index))).getName();\r
78     }\r
79 \r
80     public String[] getAllNames() {    \r
81         int N = size();\r
82         String[] result = null;\r
83         if (N != 0) {\r
84             result = new String[N];\r
85             for (int i = 0; i != N; i++) {\r
86                 result[i] = getName(i);\r
87             }\r
88         }\r
89         return result;\r
90     }\r
91 \r
92     public void establishIndices() {\r
93         for (int i = 0; i < itsVariables.size(); i++) {\r
94             LocalVariable lVar = (LocalVariable)(itsVariables.elementAt(i));\r
95             lVar.setIndex(i);\r
96         }\r
97     }\r
98 \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
107             }\r
108             else {  // there's a local variable with this name, blow it off\r
109                 itsVariables.removeElementAt(pIndex.intValue());\r
110             }\r
111         }\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
116     }\r
117 \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
122             return;\r
123         }\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
128     }\r
129     \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
133         if (i != null) {\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
144                 ht.put(k, v);\r
145             }\r
146             itsVariableNames = ht;\r
147         }\r
148     }\r
149     \r
150     // a list of the formal parameters and local variables\r
151     protected Vector itsVariables = new Vector();    \r
152 \r
153     // mapping from name to index in list\r
154     protected Hashtable itsVariableNames = new Hashtable(11);   \r
155 \r
156     protected int varStart;               // index in list of first variable\r
157 \r
158 }\r