blob: f18b2569b378746a2466fb0ff9d5d275ed386541 [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
apatrick@chromium.orga1d80592012-01-25 21:52:10 +000054#if defined(_MSC_VER)
55#pragma warning(disable: 4706)
56#endif
57
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000058///////////////////////////////////////////////////////////////////////////////////////////////
59/////////////////////////////////// Symbol Table Variables: ///////////////////////////////////
60///////////////////////////////////////////////////////////////////////////////////////////////
61
62Scope *ScopeList = NULL;
63Scope *CurrentScope = NULL;
64Scope *GlobalScope = NULL;
65
66static void unlinkScope(void *_scope) {
67 Scope *scope = _scope;
68
69 if (scope->next)
70 scope->next->prev = scope->prev;
71 if (scope->prev)
72 scope->prev->next = scope->next;
73 else
74 ScopeList = scope->next;
75}
76
77/*
78 * NewScope()
79 *
80 */
81Scope *NewScopeInPool(MemoryPool *pool)
82{
83 Scope *lScope;
84
85 lScope = mem_Alloc(pool, sizeof(Scope));
86 lScope->pool = pool;
87 lScope->parent = NULL;
88 lScope->funScope = NULL;
89 lScope->symbols = NULL;
90
91 lScope->level = 0;
92
93 lScope->programs = NULL;
94 if ((lScope->next = ScopeList))
95 ScopeList->prev = lScope;
96 lScope->prev = 0;
97 ScopeList = lScope;
98 mem_AddCleanup(pool, unlinkScope, lScope);
99 return lScope;
100} // NewScope
101
102/*
103 * PushScope()
104 *
105 */
106
107void PushScope(Scope *fScope)
108{
109 Scope *lScope;
110
111 if (CurrentScope) {
112 fScope->level = CurrentScope->level + 1;
113 if (fScope->level == 1) {
114 if (!GlobalScope) {
115 /* HACK - CTD -- if GlobalScope==NULL and level==1, we're
116 * defining a function in the superglobal scope. Things
117 * will break if we leave the level as 1, so we arbitrarily
118 * set it to 2 */
119 fScope->level = 2;
120 }
121 }
122 if (fScope->level >= 2) {
123 lScope = fScope;
124 while (lScope->level > 2)
125 lScope = lScope->next;
126 fScope->funScope = lScope;
127 }
128 } else {
129 fScope->level = 0;
130 }
131 fScope->parent = CurrentScope;
132 CurrentScope = fScope;
133} // PushScope
134
135/*
136 * PopScope()
137 *
138 */
139
140Scope *PopScope(void)
141{
142 Scope *lScope;
143
144 lScope = CurrentScope;
145 if (CurrentScope)
146 CurrentScope = CurrentScope->parent;
147 return lScope;
148} // PopScope
149
150/*
151 * NewSymbol() - Allocate a new symbol node;
152 *
153 */
154
155Symbol *NewSymbol(SourceLoc *loc, Scope *fScope, int name, symbolkind kind)
156{
157 Symbol *lSymb;
158 char *pch;
alokp@chromium.orgbcfba4c2010-08-09 22:30:49 +0000159 unsigned int ii;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000160
161 lSymb = (Symbol *) mem_Alloc(fScope->pool, sizeof(Symbol));
162 lSymb->left = NULL;
163 lSymb->right = NULL;
164 lSymb->next = NULL;
165 lSymb->name = name;
166 lSymb->loc = *loc;
167 lSymb->kind = kind;
168
169 // Clear union area:
170
171 pch = (char *) &lSymb->details;
172 for (ii = 0; ii < sizeof(lSymb->details); ii++)
173 *pch++ = 0;
174 return lSymb;
175} // NewSymbol
176
177/*
178 * lAddToTree() - Using a binary tree is not a good idea for basic atom values because they
179 * are generated in order. We'll fix this later (by reversing the bit pattern).
180 */
181
182static void lAddToTree(Symbol **fSymbols, Symbol *fSymb)
183{
184 Symbol *lSymb;
185 int lrev, frev;
186
187 lSymb = *fSymbols;
188 if (lSymb) {
189 frev = GetReversedAtom(atable, fSymb->name);
190 while (lSymb) {
191 lrev = GetReversedAtom(atable, lSymb->name);
192 if (lrev == frev) {
193 CPPErrorToInfoLog("GetAtomString(atable, fSymb->name)");
194 break;
195 } else {
196 if (lrev > frev) {
197 if (lSymb->left) {
198 lSymb = lSymb->left;
199 } else {
200 lSymb->left = fSymb;
201 break;
202 }
203 } else {
204 if (lSymb->right) {
205 lSymb = lSymb->right;
206 } else {
207 lSymb->right = fSymb;
208 break;
209 }
210 }
211 }
212 }
213 } else {
214 *fSymbols = fSymb;
215 }
216} // lAddToTree
217
218
219/*
220 * AddSymbol() - Add a variable, type, or function name to a scope.
221 *
222 */
223
224Symbol *AddSymbol(SourceLoc *loc, Scope *fScope, int atom, symbolkind kind)
225{
226 Symbol *lSymb;
227
228 if (!fScope)
229 fScope = CurrentScope;
230 lSymb = NewSymbol(loc, fScope, atom, kind);
231 lAddToTree(&fScope->symbols, lSymb);
232 return lSymb;
233} // AddSymbol
234
235
236/*********************************************************************************************/
237/************************************ Symbol Semantic Functions ******************************/
238/*********************************************************************************************/
239
240/*
241 * LookUpLocalSymbol()
242 *
243 */
244
245Symbol *LookUpLocalSymbol(Scope *fScope, int atom)
246{
247 Symbol *lSymb;
248 int rname, ratom;
249
250 ratom = GetReversedAtom(atable, atom);
251 if (!fScope)
252 fScope = CurrentScope;
253 lSymb = fScope->symbols;
254 while (lSymb) {
255 rname = GetReversedAtom(atable, lSymb->name);
256 if (rname == ratom) {
257 return lSymb;
258 } else {
259 if (rname > ratom) {
260 lSymb = lSymb->left;
261 } else {
262 lSymb = lSymb->right;
263 }
264 }
265 }
266 return NULL;
267} // LookUpLocalSymbol
268
269/*
270 * LookUpSymbol()
271 *
272 */
273
274Symbol *LookUpSymbol(Scope *fScope, int atom)
275{
276 Symbol *lSymb;
277
278 if (!fScope)
279 fScope = CurrentScope;
280 while (fScope) {
281 lSymb = LookUpLocalSymbol(fScope, atom);
282 if (lSymb)
283 return lSymb;
284 fScope = fScope->parent;
285 }
286 return NULL;
287} // LookUpSymbol
288