2 * Copyright © 2008 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 #include "main/imports.h"
25 #include "symbol_table.h"
26 #include "hash_table.h"
30 * Link to the next symbol in the table with the same name
32 * The linked list of symbols with the same name is ordered by scope
33 * from inner-most to outer-most.
35 struct symbol
*next_with_same_name
;
39 * Link to the next symbol in the table with the same scope
41 * The linked list of symbols with the same scope is unordered. Symbols
42 * in this list my have unique names.
44 struct symbol
*next_with_same_scope
;
48 * Header information for the list of symbols with the same name.
50 struct symbol_header
*hdr
;
54 * Name space of the symbol
56 * Name space are arbitrary user assigned integers. No two symbols can
57 * exist in the same name space at the same scope level.
63 * Arbitrary user supplied data.
71 struct symbol_header
{
72 /** Linkage in list of all headers in a given symbol table. */
73 struct symbol_header
*next
;
78 /** Linked list of symbols with the same name. */
79 struct symbol
*symbols
;
84 * Element of the scope stack.
87 /** Link to next (inner) scope level. */
88 struct scope_level
*next
;
90 /** Linked list of symbols with the same scope. */
91 struct symbol
*symbols
;
98 struct _mesa_symbol_table
{
99 /** Hash table containing all symbols in the symbol table. */
100 struct hash_table
*ht
;
102 /** Top of scope stack. */
103 struct scope_level
*current_scope
;
105 /** List of all symbol headers in the table. */
106 struct symbol_header
*hdr
;
110 struct _mesa_symbol_table_iterator
{
112 * Name space of symbols returned by this iterator.
118 * Currently iterated symbol
120 * The next call to \c _mesa_symbol_table_iterator_get will return this
121 * value. It will also update this value to the value that should be
122 * returned by the next call.
129 check_symbol_table(struct _mesa_symbol_table
*table
)
132 struct scope_level
*scope
;
134 for (scope
= table
->current_scope
; scope
!= NULL
; scope
= scope
->next
) {
137 for (sym
= scope
->symbols
139 ; sym
= sym
->next_with_same_name
) {
140 const struct symbol_header
*const hdr
= sym
->hdr
;
143 for (sym2
= hdr
->symbols
145 ; sym2
= sym2
->next_with_same_name
) {
146 assert(sym2
->hdr
== hdr
);
154 _mesa_symbol_table_pop_scope(struct _mesa_symbol_table
*table
)
156 struct scope_level
*const scope
= table
->current_scope
;
157 struct symbol
*sym
= scope
->symbols
;
159 table
->current_scope
= scope
->next
;
163 while (sym
!= NULL
) {
164 struct symbol
*const next
= sym
->next_with_same_scope
;
165 struct symbol_header
*const hdr
= sym
->hdr
;
167 assert(hdr
->symbols
== sym
);
169 hdr
->symbols
= sym
->next_with_same_name
;
176 check_symbol_table(table
);
181 _mesa_symbol_table_push_scope(struct _mesa_symbol_table
*table
)
183 struct scope_level
*const scope
= calloc(1, sizeof(*scope
));
185 scope
->next
= table
->current_scope
;
186 table
->current_scope
= scope
;
190 static struct symbol_header
*
191 find_symbol(struct _mesa_symbol_table
*table
, const char *name
)
193 return (struct symbol_header
*) hash_table_find(table
->ht
, name
);
197 struct _mesa_symbol_table_iterator
*
198 _mesa_symbol_table_iterator_ctor(struct _mesa_symbol_table
*table
,
199 int name_space
, const char *name
)
201 struct _mesa_symbol_table_iterator
*iter
= calloc(1, sizeof(*iter
));
202 struct symbol_header
*const hdr
= find_symbol(table
, name
);
204 iter
->name_space
= name_space
;
209 for (sym
= hdr
->symbols
; sym
!= NULL
; sym
= sym
->next_with_same_name
) {
210 assert(sym
->hdr
== hdr
);
212 if ((name_space
== -1) || (sym
->name_space
== name_space
)) {
224 _mesa_symbol_table_iterator_dtor(struct _mesa_symbol_table_iterator
*iter
)
231 _mesa_symbol_table_iterator_get(struct _mesa_symbol_table_iterator
*iter
)
233 return (iter
->curr
== NULL
) ? NULL
: iter
->curr
->data
;
238 _mesa_symbol_table_iterator_next(struct _mesa_symbol_table_iterator
*iter
)
240 struct symbol_header
*hdr
;
242 if (iter
->curr
== NULL
) {
246 hdr
= iter
->curr
->hdr
;
247 iter
->curr
= iter
->curr
->next_with_same_name
;
249 while (iter
->curr
!= NULL
) {
250 assert(iter
->curr
->hdr
== hdr
);
252 if ((iter
->name_space
== -1)
253 || (iter
->curr
->name_space
== iter
->name_space
)) {
257 iter
->curr
= iter
->curr
->next_with_same_name
;
265 _mesa_symbol_table_find_symbol(struct _mesa_symbol_table
*table
,
266 int name_space
, const char *name
)
268 struct symbol_header
*const hdr
= find_symbol(table
, name
);
274 for (sym
= hdr
->symbols
; sym
!= NULL
; sym
= sym
->next_with_same_name
) {
275 assert(sym
->hdr
== hdr
);
277 if ((name_space
== -1) || (sym
->name_space
== name_space
)) {
288 _mesa_symbol_table_add_symbol(struct _mesa_symbol_table
*table
,
289 int name_space
, const char *name
,
292 struct symbol_header
*hdr
;
295 check_symbol_table(table
);
297 hdr
= find_symbol(table
, name
);
299 check_symbol_table(table
);
302 hdr
= calloc(1, sizeof(*hdr
));
305 hash_table_insert(table
->ht
, hdr
, name
);
306 hdr
->next
= table
->hdr
;
310 check_symbol_table(table
);
312 sym
= calloc(1, sizeof(*sym
));
313 sym
->next_with_same_name
= hdr
->symbols
;
314 sym
->next_with_same_scope
= table
->current_scope
->symbols
;
316 sym
->name_space
= name_space
;
317 sym
->data
= declaration
;
319 assert(sym
->hdr
== hdr
);
322 table
->current_scope
->symbols
= sym
;
324 check_symbol_table(table
);
329 struct _mesa_symbol_table
*
330 _mesa_symbol_table_ctor(void)
332 struct _mesa_symbol_table
*table
= calloc(1, sizeof(*table
));
335 table
->ht
= hash_table_ctor(32, hash_table_string_hash
,
336 hash_table_string_compare
);
338 _mesa_symbol_table_push_scope(table
);
346 _mesa_symbol_table_dtor(struct _mesa_symbol_table
*table
)
348 struct symbol_header
*hdr
;
349 struct symbol_header
*next
;
351 while (table
->current_scope
!= NULL
) {
352 _mesa_symbol_table_pop_scope(table
);
355 for (hdr
= table
->hdr
; hdr
!= NULL
; hdr
= next
) {
360 hash_table_dtor(table
->ht
);