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