add license comments
[cparser] / symbol_table.c
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
20 #include <config.h>
21
22 #include "symbol_table_t.h"
23 #include "token_t.h"
24 #include "adt/hash_string.h"
25 #include "adt/obst.h"
26
27 struct obstack symbol_obstack;
28
29 static inline
30 void init_symbol_table_entry(symbol_t *entry, const char *string)
31 {
32         entry->string      = string;
33         entry->ID          = T_IDENTIFIER;
34         entry->pp_ID       = 0;
35         entry->declaration = NULL;
36 }
37
38 #define HashSet                    symbol_table_t
39 #define HashSetIterator            symbol_table_iterator_t
40 #define HashSetEntry               symbol_table_hash_entry_t
41 #define ValueType                  symbol_t*
42 #define NullValue                  NULL
43 #define DeletedValue               ((symbol_t*)-1)
44 #define KeyType                    const char *
45 #define ConstKeyType               const char *
46 #define GetKey(value)              (value)->string
47 #define InitData(this,value,key)   { (value) = (ValueType) obstack_alloc(&symbol_obstack, sizeof(symbol_t)); init_symbol_table_entry((value), key); }
48 #define Hash(this, key)            hash_string(key)
49 #define KeysEqual(this,key1,key2)  (strcmp(key1, key2) == 0)
50 #define SetRangeEmpty(ptr,size)    memset(ptr, 0, (size) * sizeof(symbol_table_hash_entry_t))
51
52 #define hashset_init            _symbol_table_init
53 #define hashset_init_size       _symbol_table_init_size
54 #define hashset_destroy         _symbol_table_destroy
55 #define hashset_insert          _symbol_table_insert
56 #define hashset_remove          _symbol_table_remove
57 #define hashset_find            _symbol_table_find
58 #define hashset_size            _symbol_table_size
59 #define hashset_iterator_init   _symbol_table_iterator_init
60 #define hashset_iterator_next   _symbol_table_iterator_next
61 #define hashset_remove_iterator _symbol_table_remove_iterator
62
63 #include "adt/hashset.c"
64
65 static symbol_table_t  symbol_table;
66
67 symbol_t *symbol_table_insert(const char *symbol)
68 {
69         return _symbol_table_insert(&symbol_table, symbol);
70 }
71
72 void init_symbol_table(void)
73 {
74         obstack_init(&symbol_obstack);
75         _symbol_table_init(&symbol_table);
76 }
77
78 void exit_symbol_table(void)
79 {
80         _symbol_table_destroy(&symbol_table);
81         obstack_free(&symbol_obstack, NULL);
82 }