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.
29 #include "symbol_table.h"
30 #include "hash_table.h"
34 * Link to the next symbol in the table with the same name
36 * The linked list of symbols with the same name is ordered by scope
37 * from inner-most to outer-most.
39 struct symbol
*next_with_same_name
;
43 * Link to the next symbol in the table with the same scope
45 * The linked list of symbols with the same scope is unordered. Symbols
46 * in this list my have unique names.
48 struct symbol
*next_with_same_scope
;
52 * Header information for the list of symbols with the same name.
54 struct symbol_header
*hdr
;
58 * Name space of the symbol
60 * Name space are arbitrary user assigned integers. No two symbols can
61 * exist in the same name space at the same scope level.
67 * Arbitrary user supplied data.
75 struct symbol_header
{
76 /** Linkage in list of all headers in a given symbol table. */
77 struct symbol_header
*next
;
82 /** Linked list of symbols with the same name. */
83 struct symbol
*symbols
;
88 * Element of the scope stack.
91 /** Link to next (inner) scope level. */
92 struct scope_level
*next
;
94 /** Linked list of symbols with the same scope. */
95 struct symbol
*symbols
;
102 struct _mesa_symbol_table
{
103 /** Hash table containing all symbols in the symbol table. */
104 struct hash_table
*ht
;
106 /** Top of scope stack. */
107 struct scope_level
*current_scope
;
109 /** List of all symbol headers in the table. */
110 struct symbol_header
*hdr
;
114 struct _mesa_symbol_table_iterator
{
116 * Name space of symbols returned by this iterator.
122 * Currently iterated symbol
124 * The next call to \c _mesa_symbol_table_iterator_get will return this
125 * value. It will also update this value to the value that should be
126 * returned by the next call.
133 check_symbol_table(struct _mesa_symbol_table
*table
)
136 struct scope_level
*scope
;
138 for (scope
= table
->current_scope
; scope
!= NULL
; scope
= scope
->next
) {
141 for (sym
= scope
->symbols
143 ; sym
= sym
->next_with_same_name
) {
144 const struct symbol_header
*const hdr
= sym
->hdr
;
147 for (sym2
= hdr
->symbols
149 ; sym2
= sym2
->next_with_same_name
) {
150 assert(sym2
->hdr
== hdr
);
158 _mesa_symbol_table_pop_scope(struct _mesa_symbol_table
*table
)
160 struct scope_level
*const scope
= table
->current_scope
;
161 struct symbol
*sym
= scope
->symbols
;
163 table
->current_scope
= scope
->next
;
167 while (sym
!= NULL
) {
168 struct symbol
*const next
= sym
->next_with_same_scope
;
169 struct symbol_header
*const hdr
= sym
->hdr
;
171 assert(hdr
->symbols
== sym
);
173 hdr
->symbols
= sym
->next_with_same_name
;
180 check_symbol_table(table
);
185 _mesa_symbol_table_push_scope(struct _mesa_symbol_table
*table
)
187 struct scope_level
*const scope
= calloc(1, sizeof(*scope
));
189 scope
->next
= table
->current_scope
;
190 table
->current_scope
= scope
;
194 static struct symbol_header
*
195 find_symbol(struct _mesa_symbol_table
*table
, const char *name
)
197 return (struct symbol_header
*) hash_table_find(table
->ht
, name
);
201 struct _mesa_symbol_table_iterator
*
202 _mesa_symbol_table_iterator_ctor(struct _mesa_symbol_table
*table
,
203 int name_space
, const char *name
)
205 struct _mesa_symbol_table_iterator
*iter
= calloc(1, sizeof(*iter
));
206 struct symbol_header
*const hdr
= find_symbol(table
, name
);
208 iter
->name_space
= name_space
;
213 for (sym
= hdr
->symbols
; sym
!= NULL
; sym
= sym
->next_with_same_name
) {
214 assert(sym
->hdr
== hdr
);
216 if ((name_space
== -1) || (sym
->name_space
== name_space
)) {
228 _mesa_symbol_table_iterator_dtor(struct _mesa_symbol_table_iterator
*iter
)
235 _mesa_symbol_table_iterator_get(struct _mesa_symbol_table_iterator
*iter
)
237 return (iter
->curr
== NULL
) ? NULL
: iter
->curr
->data
;
242 _mesa_symbol_table_iterator_next(struct _mesa_symbol_table_iterator
*iter
)
244 struct symbol_header
*hdr
;
246 if (iter
->curr
== NULL
) {
250 hdr
= iter
->curr
->hdr
;
251 iter
->curr
= iter
->curr
->next_with_same_name
;
253 while (iter
->curr
!= NULL
) {
254 assert(iter
->curr
->hdr
== hdr
);
256 if ((iter
->name_space
== -1)
257 || (iter
->curr
->name_space
== iter
->name_space
)) {
261 iter
->curr
= iter
->curr
->next_with_same_name
;
269 _mesa_symbol_table_find_symbol(struct _mesa_symbol_table
*table
,
270 int name_space
, const char *name
)
272 struct symbol_header
*const hdr
= find_symbol(table
, name
);
278 for (sym
= hdr
->symbols
; sym
!= NULL
; sym
= sym
->next_with_same_name
) {
279 assert(sym
->hdr
== hdr
);
281 if ((name_space
== -1) || (sym
->name_space
== name_space
)) {
292 _mesa_symbol_table_add_symbol(struct _mesa_symbol_table
*table
,
293 int name_space
, const char *name
,
296 struct symbol_header
*hdr
;
299 check_symbol_table(table
);
301 hdr
= find_symbol(table
, name
);
303 check_symbol_table(table
);
306 hdr
= calloc(1, sizeof(*hdr
));
309 hash_table_insert(table
->ht
, hdr
, name
);
310 hdr
->next
= table
->hdr
;
314 check_symbol_table(table
);
316 sym
= calloc(1, sizeof(*sym
));
317 sym
->next_with_same_name
= hdr
->symbols
;
318 sym
->next_with_same_scope
= table
->current_scope
->symbols
;
320 sym
->name_space
= name_space
;
321 sym
->data
= declaration
;
323 assert(sym
->hdr
== hdr
);
326 table
->current_scope
->symbols
= sym
;
328 check_symbol_table(table
);
333 struct _mesa_symbol_table
*
334 _mesa_symbol_table_ctor(void)
336 struct _mesa_symbol_table
*table
= calloc(1, sizeof(*table
));
339 table
->ht
= hash_table_ctor(32, hash_table_string_hash
,
340 hash_table_string_compare
);
342 _mesa_symbol_table_push_scope(table
);
350 _mesa_symbol_table_dtor(struct _mesa_symbol_table
*table
)
352 struct symbol_header
*hdr
;
353 struct symbol_header
*next
;
355 while (table
->current_scope
!= NULL
) {
356 _mesa_symbol_table_pop_scope(table
);
359 for (hdr
= table
->hdr
; hdr
!= NULL
; hdr
= next
) {
364 hash_table_dtor(table
->ht
);