blob: 5baedf52ff426f42dd52d404a771e0154412906f [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001/****************************************************************************\
2Copyright (c) 2002, NVIDIA Corporation.
3
4NVIDIA Corporation("NVIDIA") supplies this software to you in
5consideration of your agreement to the following terms, and your use,
6installation, modification or redistribution of this NVIDIA software
7constitutes acceptance of these terms. If you do not agree with these
8terms, please do not use, install, modify or redistribute this NVIDIA
9software.
10
11In consideration of your agreement to abide by the following terms, and
12subject to these terms, NVIDIA grants you a personal, non-exclusive
13license, under NVIDIA's copyrights in this original NVIDIA software (the
14"NVIDIA Software"), to use, reproduce, modify and redistribute the
15NVIDIA Software, with or without modifications, in source and/or binary
16forms; provided that if you redistribute the NVIDIA Software, you must
17retain the copyright notice of NVIDIA, this notice and the following
18text and disclaimers in all such redistributions of the NVIDIA Software.
19Neither the name, trademarks, service marks nor logos of NVIDIA
20Corporation may be used to endorse or promote products derived from the
21NVIDIA Software without specific prior written permission from NVIDIA.
22Except as expressly stated in this notice, no other rights or licenses
23express or implied, are granted by NVIDIA herein, including but not
24limited to any patent rights that may be infringed by your derivative
25works or by other works in which the NVIDIA Software may be
26incorporated. No hardware is licensed hereunder.
27
28THE NVIDIA SOFTWARE IS BEING PROVIDED ON AN "AS IS" BASIS, WITHOUT
29WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED,
30INCLUDING WITHOUT LIMITATION, WARRANTIES OR CONDITIONS OF TITLE,
31NON-INFRINGEMENT, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
32ITS USE AND OPERATION EITHER ALONE OR IN COMBINATION WITH OTHER
33PRODUCTS.
34
35IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT,
36INCIDENTAL, EXEMPLARY, CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
37TO, LOST PROFITS; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
38USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) OR ARISING IN ANY WAY
39OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE
40NVIDIA SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT,
41TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
42NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43\****************************************************************************/
44//
45// symbols.c
46//
47
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000048#include <stdlib.h>
49#include <stdio.h>
50#include <string.h>
51
daniel@transgaming.come6842292010-04-20 18:52:50 +000052#include "compiler/preprocessor/slglobals.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000053
54///////////////////////////////////////////////////////////////////////////////////////////////
55/////////////////////////////////// Symbol Table Variables: ///////////////////////////////////
56///////////////////////////////////////////////////////////////////////////////////////////////
57
58Scope *ScopeList = NULL;
59Scope *CurrentScope = NULL;
60Scope *GlobalScope = NULL;
61
62static void unlinkScope(void *_scope) {
63 Scope *scope = _scope;
64
65 if (scope->next)
66 scope->next->prev = scope->prev;
67 if (scope->prev)
68 scope->prev->next = scope->next;
69 else
70 ScopeList = scope->next;
71}
72
73/*
74 * NewScope()
75 *
76 */
77Scope *NewScopeInPool(MemoryPool *pool)
78{
79 Scope *lScope;
80
81 lScope = mem_Alloc(pool, sizeof(Scope));
82 lScope->pool = pool;
83 lScope->parent = NULL;
84 lScope->funScope = NULL;
85 lScope->symbols = NULL;
86
87 lScope->level = 0;
88
89 lScope->programs = NULL;
90 if ((lScope->next = ScopeList))
91 ScopeList->prev = lScope;
92 lScope->prev = 0;
93 ScopeList = lScope;
94 mem_AddCleanup(pool, unlinkScope, lScope);
95 return lScope;
96} // NewScope
97
98/*
99 * PushScope()
100 *
101 */
102
103void PushScope(Scope *fScope)
104{
105 Scope *lScope;
106
107 if (CurrentScope) {
108 fScope->level = CurrentScope->level + 1;
109 if (fScope->level == 1) {
110 if (!GlobalScope) {
111 /* HACK - CTD -- if GlobalScope==NULL and level==1, we're
112 * defining a function in the superglobal scope. Things
113 * will break if we leave the level as 1, so we arbitrarily
114 * set it to 2 */
115 fScope->level = 2;
116 }
117 }
118 if (fScope->level >= 2) {
119 lScope = fScope;
120 while (lScope->level > 2)
121 lScope = lScope->next;
122 fScope->funScope = lScope;
123 }
124 } else {
125 fScope->level = 0;
126 }
127 fScope->parent = CurrentScope;
128 CurrentScope = fScope;
129} // PushScope
130
131/*
132 * PopScope()
133 *
134 */
135
136Scope *PopScope(void)
137{
138 Scope *lScope;
139
140 lScope = CurrentScope;
141 if (CurrentScope)
142 CurrentScope = CurrentScope->parent;
143 return lScope;
144} // PopScope
145
146/*
147 * NewSymbol() - Allocate a new symbol node;
148 *
149 */
150
151Symbol *NewSymbol(SourceLoc *loc, Scope *fScope, int name, symbolkind kind)
152{
153 Symbol *lSymb;
154 char *pch;
alokp@chromium.orgbcfba4c2010-08-09 22:30:49 +0000155 unsigned int ii;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000156
157 lSymb = (Symbol *) mem_Alloc(fScope->pool, sizeof(Symbol));
158 lSymb->left = NULL;
159 lSymb->right = NULL;
160 lSymb->next = NULL;
161 lSymb->name = name;
162 lSymb->loc = *loc;
163 lSymb->kind = kind;
164
165 // Clear union area:
166
167 pch = (char *) &lSymb->details;
168 for (ii = 0; ii < sizeof(lSymb->details); ii++)
169 *pch++ = 0;
170 return lSymb;
171} // NewSymbol
172
173/*
174 * lAddToTree() - Using a binary tree is not a good idea for basic atom values because they
175 * are generated in order. We'll fix this later (by reversing the bit pattern).
176 */
177
178static void lAddToTree(Symbol **fSymbols, Symbol *fSymb)
179{
180 Symbol *lSymb;
181 int lrev, frev;
182
183 lSymb = *fSymbols;
184 if (lSymb) {
185 frev = GetReversedAtom(atable, fSymb->name);
186 while (lSymb) {
187 lrev = GetReversedAtom(atable, lSymb->name);
188 if (lrev == frev) {
189 CPPErrorToInfoLog("GetAtomString(atable, fSymb->name)");
190 break;
191 } else {
192 if (lrev > frev) {
193 if (lSymb->left) {
194 lSymb = lSymb->left;
195 } else {
196 lSymb->left = fSymb;
197 break;
198 }
199 } else {
200 if (lSymb->right) {
201 lSymb = lSymb->right;
202 } else {
203 lSymb->right = fSymb;
204 break;
205 }
206 }
207 }
208 }
209 } else {
210 *fSymbols = fSymb;
211 }
212} // lAddToTree
213
214
215/*
216 * AddSymbol() - Add a variable, type, or function name to a scope.
217 *
218 */
219
220Symbol *AddSymbol(SourceLoc *loc, Scope *fScope, int atom, symbolkind kind)
221{
222 Symbol *lSymb;
223
224 if (!fScope)
225 fScope = CurrentScope;
226 lSymb = NewSymbol(loc, fScope, atom, kind);
227 lAddToTree(&fScope->symbols, lSymb);
228 return lSymb;
229} // AddSymbol
230
231
232/*********************************************************************************************/
233/************************************ Symbol Semantic Functions ******************************/
234/*********************************************************************************************/
235
236/*
237 * LookUpLocalSymbol()
238 *
239 */
240
241Symbol *LookUpLocalSymbol(Scope *fScope, int atom)
242{
243 Symbol *lSymb;
244 int rname, ratom;
245
246 ratom = GetReversedAtom(atable, atom);
247 if (!fScope)
248 fScope = CurrentScope;
249 lSymb = fScope->symbols;
250 while (lSymb) {
251 rname = GetReversedAtom(atable, lSymb->name);
252 if (rname == ratom) {
253 return lSymb;
254 } else {
255 if (rname > ratom) {
256 lSymb = lSymb->left;
257 } else {
258 lSymb = lSymb->right;
259 }
260 }
261 }
262 return NULL;
263} // LookUpLocalSymbol
264
265/*
266 * LookUpSymbol()
267 *
268 */
269
270Symbol *LookUpSymbol(Scope *fScope, int atom)
271{
272 Symbol *lSymb;
273
274 if (!fScope)
275 fScope = CurrentScope;
276 while (fScope) {
277 lSymb = LookUpLocalSymbol(fScope, atom);
278 if (lSymb)
279 return lSymb;
280 fScope = fScope->parent;
281 }
282 return NULL;
283} // LookUpSymbol
284