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