42 |
`---------------------------------*/ |
`---------------------------------*/ |
43 |
|
|
44 |
static symbol_t * |
static symbol_t * |
45 |
symbol_new (const char *tag, location_t location) |
symbol_new (struniq_t tag, location_t location) |
46 |
{ |
{ |
47 |
symbol_t *res = XMALLOC (symbol_t, 1); |
symbol_t *res = XMALLOC (symbol_t, 1); |
48 |
|
|
49 |
res->tag = xstrdup (tag); |
struniq_assert (tag); |
50 |
|
res->tag = tag; |
51 |
res->location = location; |
res->location = location; |
52 |
|
|
53 |
res->type_name = NULL; |
res->type_name = NULL; |
73 |
`------------------------------------------------------------------*/ |
`------------------------------------------------------------------*/ |
74 |
|
|
75 |
void |
void |
76 |
symbol_type_set (symbol_t *symbol, char *type_name, location_t location) |
symbol_type_set (symbol_t *symbol, struniq_t type_name, location_t location) |
77 |
{ |
{ |
78 |
if (type_name) |
if (type_name) |
79 |
{ |
{ |
80 |
if (symbol->type_name) |
if (symbol->type_name) |
81 |
complain_at (location, |
complain_at (location, |
82 |
_("type redeclaration for %s"), symbol->tag); |
_("type redeclaration for %s"), symbol->tag); |
83 |
|
struniq_assert (type_name); |
84 |
symbol->type_name = type_name; |
symbol->type_name = type_name; |
85 |
} |
} |
86 |
} |
} |
202 |
static void |
static void |
203 |
symbol_free (symbol_t *this) |
symbol_free (symbol_t *this) |
204 |
{ |
{ |
|
free (this->tag); |
|
205 |
free (this); |
free (this); |
206 |
} |
} |
207 |
|
|
377 |
static struct hash_table *symbol_table = NULL; |
static struct hash_table *symbol_table = NULL; |
378 |
|
|
379 |
static bool |
static bool |
380 |
hash_compare_symbol_t (const symbol_t *m1, const symbol_t *m2) |
hash_compare_symbol (const symbol_t *m1, const symbol_t *m2) |
381 |
{ |
{ |
382 |
return strcmp (m1->tag, m2->tag) == 0; |
/* Since tags are unique, we can compare the pointers themselves. */ |
383 |
|
return STRUNIQ_EQ (m1->tag, m2->tag); |
384 |
} |
} |
385 |
|
|
386 |
static unsigned int |
static unsigned int |
387 |
hash_symbol_t (const symbol_t *m, unsigned int tablesize) |
hash_symbol (const symbol_t *m, unsigned int tablesize) |
388 |
{ |
{ |
389 |
return hash_string (m->tag, tablesize); |
/* Since tags are unique, we can hash the pointer itself. */ |
390 |
|
return ((size_t) m->tag) % tablesize; |
391 |
} |
} |
392 |
|
|
393 |
|
|
400 |
{ |
{ |
401 |
symbol_table = hash_initialize (HT_INITIAL_CAPACITY, |
symbol_table = hash_initialize (HT_INITIAL_CAPACITY, |
402 |
NULL, |
NULL, |
403 |
(Hash_hasher) hash_symbol_t, |
(Hash_hasher) hash_symbol, |
404 |
(Hash_comparator) hash_compare_symbol_t, |
(Hash_comparator) hash_compare_symbol, |
405 |
(Hash_data_freer) symbol_free); |
(Hash_data_freer) symbol_free); |
406 |
} |
} |
407 |
|
|
418 |
symbol_t *entry; |
symbol_t *entry; |
419 |
|
|
420 |
/* Keep the symbol in a printable form. */ |
/* Keep the symbol in a printable form. */ |
421 |
key = quotearg_style (escape_quoting_style, key); |
key = struniq_new (quotearg_style (escape_quoting_style, key)); |
422 |
*(char const **) &probe.tag = key; |
*(char const **) &probe.tag = key; |
423 |
entry = hash_lookup (symbol_table, &probe); |
entry = hash_lookup (symbol_table, &probe); |
424 |
|
|