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.
67 /** Scope depth where this symbol was defined. */
74 struct symbol_header
{
75 /** Linkage in list of all headers in a given symbol table. */
76 struct symbol_header
*next
;
81 /** Linked list of symbols with the same name. */
82 struct symbol
*symbols
;
87 * Element of the scope stack.
90 /** Link to next (inner) scope level. */
91 struct scope_level
*next
;
93 /** Linked list of symbols with the same scope. */
94 struct symbol
*symbols
;
101 struct _mesa_symbol_table
{
102 /** Hash table containing all symbols in the symbol table. */
103 struct hash_table
*ht
;
105 /** Top of scope stack. */
106 struct scope_level
*current_scope
;
108 /** List of all symbol headers in the table. */
109 struct symbol_header
*hdr
;
111 /** Current scope depth. */
116 struct _mesa_symbol_table_iterator
{
118 * Name space of symbols returned by this iterator.
124 * Currently iterated symbol
126 * The next call to \c _mesa_symbol_table_iterator_get will return this
127 * value. It will also update this value to the value that should be
128 * returned by the next call.
135 check_symbol_table(struct _mesa_symbol_table
*table
)
138 struct scope_level
*scope
;
140 for (scope
= table
->current_scope
; scope
!= NULL
; scope
= scope
->next
) {
143 for (sym
= scope
->symbols
145 ; sym
= sym
->next_with_same_name
) {
146 const struct symbol_header
*const hdr
= sym
->hdr
;
149 for (sym2
= hdr
->symbols
151 ; sym2
= sym2
->next_with_same_name
) {
152 assert(sym2
->hdr
== hdr
);
160 _mesa_symbol_table_pop_scope(struct _mesa_symbol_table
*table
)
162 struct scope_level
*const scope
= table
->current_scope
;
163 struct symbol
*sym
= scope
->symbols
;
165 table
->current_scope
= scope
->next
;
170 while (sym
!= NULL
) {
171 struct symbol
*const next
= sym
->next_with_same_scope
;
172 struct symbol_header
*const hdr
= sym
->hdr
;
174 assert(hdr
->symbols
== sym
);
176 hdr
->symbols
= sym
->next_with_same_name
;
183 check_symbol_table(table
);
188 _mesa_symbol_table_push_scope(struct _mesa_symbol_table
*table
)
190 struct scope_level
*const scope
= calloc(1, sizeof(*scope
));
192 scope
->next
= table
->current_scope
;
193 table
->current_scope
= scope
;
198 static struct symbol_header
*
199 find_symbol(struct _mesa_symbol_table
*table
, const char *name
)
201 return (struct symbol_header
*) hash_table_find(table
->ht
, name
);
205 struct _mesa_symbol_table_iterator
*
206 _mesa_symbol_table_iterator_ctor(struct _mesa_symbol_table
*table
,
207 int name_space
, const char *name
)
209 struct _mesa_symbol_table_iterator
*iter
= calloc(1, sizeof(*iter
));
210 struct symbol_header
*const hdr
= find_symbol(table
, name
);
212 iter
->name_space
= name_space
;
217 for (sym
= hdr
->symbols
; sym
!= NULL
; sym
= sym
->next_with_same_name
) {
218 assert(sym
->hdr
== hdr
);
220 if ((name_space
== -1) || (sym
->name_space
== name_space
)) {
232 _mesa_symbol_table_iterator_dtor(struct _mesa_symbol_table_iterator
*iter
)
239 _mesa_symbol_table_iterator_get(struct _mesa_symbol_table_iterator
*iter
)
241 return (iter
->curr
== NULL
) ? NULL
: iter
->curr
->data
;
246 _mesa_symbol_table_iterator_next(struct _mesa_symbol_table_iterator
*iter
)
248 struct symbol_header
*hdr
;
250 if (iter
->curr
== NULL
) {
254 hdr
= iter
->curr
->hdr
;
255 iter
->curr
= iter
->curr
->next_with_same_name
;
257 while (iter
->curr
!= NULL
) {
258 assert(iter
->curr
->hdr
== hdr
);
260 if ((iter
->name_space
== -1)
261 || (iter
->curr
->name_space
== iter
->name_space
)) {
265 iter
->curr
= iter
->curr
->next_with_same_name
;
273 _mesa_symbol_table_find_symbol(struct _mesa_symbol_table
*table
,
274 int name_space
, const char *name
)
276 struct symbol_header
*const hdr
= find_symbol(table
, name
);
282 for (sym
= hdr
->symbols
; sym
!= NULL
; sym
= sym
->next_with_same_name
) {
283 assert(sym
->hdr
== hdr
);
285 if ((name_space
== -1) || (sym
->name_space
== name_space
)) {
296 _mesa_symbol_table_add_symbol(struct _mesa_symbol_table
*table
,
297 int name_space
, const char *name
,
300 struct symbol_header
*hdr
;
303 check_symbol_table(table
);
305 hdr
= find_symbol(table
, name
);
307 check_symbol_table(table
);
310 hdr
= calloc(1, sizeof(*hdr
));
313 hash_table_insert(table
->ht
, hdr
, name
);
314 hdr
->next
= table
->hdr
;
318 check_symbol_table(table
);
320 /* If the symbol already exists at this scope, it cannot be added to the
323 if (hdr
->symbols
&& (hdr
->symbols
->depth
== table
->depth
))
326 sym
= calloc(1, sizeof(*sym
));
327 sym
->next_with_same_name
= hdr
->symbols
;
328 sym
->next_with_same_scope
= table
->current_scope
->symbols
;
330 sym
->name_space
= name_space
;
331 sym
->data
= declaration
;
332 sym
->depth
= table
->depth
;
334 assert(sym
->hdr
== hdr
);
337 table
->current_scope
->symbols
= sym
;
339 check_symbol_table(table
);
344 struct _mesa_symbol_table
*
345 _mesa_symbol_table_ctor(void)
347 struct _mesa_symbol_table
*table
= calloc(1, sizeof(*table
));
350 table
->ht
= hash_table_ctor(32, hash_table_string_hash
,
351 hash_table_string_compare
);
353 _mesa_symbol_table_push_scope(table
);
361 _mesa_symbol_table_dtor(struct _mesa_symbol_table
*table
)
363 struct symbol_header
*hdr
;
364 struct symbol_header
*next
;
366 while (table
->current_scope
!= NULL
) {
367 _mesa_symbol_table_pop_scope(table
);
370 for (hdr
= table
->hdr
; hdr
!= NULL
; hdr
= next
) {
375 hash_table_dtor(table
->ht
);