mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-16 07:43:03 +00:00
(symbol_check_defined,
symbol_check_alias_consistency, symbol_pack, symbol_translation, hash_compare_symbol, hash_symbol): Now inline. Return bool, not int. (symbol_check_defined_processor, symbol_check_alias_consistency_processor, symbol_pack_processor, symbol_translation_processor, hash_symbol_comparator, hash_symbol_hasher): New functions, to avoid casts. (symbols_new, symbols_do, symbols_check_defined, symbols_token_translations_init): Use new functions instead of casting old functions unportably. (symbol_free): Remove; unused. (symbol_get): Remove cast in lhs of assignment. (symbols_do): Now static. Accept generic arguments, not hashing-related ones.
This commit is contained in:
97
src/symtab.c
97
src/symtab.c
@@ -1,4 +1,5 @@
|
|||||||
/* Symbol table manager for Bison,
|
/* Symbol table manager for Bison.
|
||||||
|
|
||||||
Copyright (C) 1984, 1989, 2000, 2001, 2002 Free Software Foundation, Inc.
|
Copyright (C) 1984, 1989, 2000, 2001, 2002 Free Software Foundation, Inc.
|
||||||
|
|
||||||
This file is part of Bison, the GNU Compiler Compiler.
|
This file is part of Bison, the GNU Compiler Compiler.
|
||||||
@@ -46,7 +47,7 @@ location startsymbol_location;
|
|||||||
static symbol *
|
static symbol *
|
||||||
symbol_new (uniqstr tag, location loc)
|
symbol_new (uniqstr tag, location loc)
|
||||||
{
|
{
|
||||||
symbol *res = XMALLOC (symbol, 1);
|
symbol *res = MALLOC (res, 1);
|
||||||
|
|
||||||
uniqstr_assert (tag);
|
uniqstr_assert (tag);
|
||||||
res->tag = tag;
|
res->tag = tag;
|
||||||
@@ -190,23 +191,12 @@ symbol_user_token_number_set (symbol *sym, int user_token_number, location loc)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*-----------.
|
|
||||||
| Free SYM. |
|
|
||||||
`-----------*/
|
|
||||||
|
|
||||||
static void
|
|
||||||
symbol_free (symbol *sym)
|
|
||||||
{
|
|
||||||
free (sym);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*----------------------------------------------------------.
|
/*----------------------------------------------------------.
|
||||||
| If SYM is not defined, report an error, and consider it a |
|
| If SYM is not defined, report an error, and consider it a |
|
||||||
| nonterminal. |
|
| nonterminal. |
|
||||||
`----------------------------------------------------------*/
|
`----------------------------------------------------------*/
|
||||||
|
|
||||||
static bool
|
static inline bool
|
||||||
symbol_check_defined (symbol *sym)
|
symbol_check_defined (symbol *sym)
|
||||||
{
|
{
|
||||||
if (sym->class == unknown_sym)
|
if (sym->class == unknown_sym)
|
||||||
@@ -222,6 +212,12 @@ symbol_check_defined (symbol *sym)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
symbol_check_defined_processor (void *sym, void *null ATTRIBUTE_UNUSED)
|
||||||
|
{
|
||||||
|
return symbol_check_defined (sym);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*------------------------------------------------------------------.
|
/*------------------------------------------------------------------.
|
||||||
| Declare the new symbol SYM. Make it an alias of SYMVAL, and type |
|
| Declare the new symbol SYM. Make it an alias of SYMVAL, and type |
|
||||||
@@ -260,8 +256,8 @@ symbol_make_alias (symbol *sym, symbol *symval, location loc)
|
|||||||
| associativity. |
|
| associativity. |
|
||||||
`---------------------------------------------------------*/
|
`---------------------------------------------------------*/
|
||||||
|
|
||||||
static bool
|
static inline bool
|
||||||
symbol_check_alias_consistence (symbol *this)
|
symbol_check_alias_consistency (symbol *this)
|
||||||
{
|
{
|
||||||
/* Check only those who _are_ the aliases. */
|
/* Check only those who _are_ the aliases. */
|
||||||
if (this->alias && this->user_token_number == USER_NUMBER_ALIAS)
|
if (this->alias && this->user_token_number == USER_NUMBER_ALIAS)
|
||||||
@@ -294,13 +290,20 @@ symbol_check_alias_consistence (symbol *this)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
symbol_check_alias_consistency_processor (void *this,
|
||||||
|
void *null ATTRIBUTE_UNUSED)
|
||||||
|
{
|
||||||
|
return symbol_check_alias_consistency (this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*-------------------------------------------------------------------.
|
/*-------------------------------------------------------------------.
|
||||||
| Assign a symbol number, and write the definition of the token name |
|
| Assign a symbol number, and write the definition of the token name |
|
||||||
| into FDEFINES. Put in SYMBOLS. |
|
| into FDEFINES. Put in SYMBOLS. |
|
||||||
`-------------------------------------------------------------------*/
|
`-------------------------------------------------------------------*/
|
||||||
|
|
||||||
static bool
|
static inline bool
|
||||||
symbol_pack (symbol *this)
|
symbol_pack (symbol *this)
|
||||||
{
|
{
|
||||||
if (this->class == nterm_sym)
|
if (this->class == nterm_sym)
|
||||||
@@ -337,6 +340,12 @@ symbol_pack (symbol *this)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED)
|
||||||
|
{
|
||||||
|
return symbol_pack (this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -344,7 +353,7 @@ symbol_pack (symbol *this)
|
|||||||
| Put THIS in TOKEN_TRANSLATIONS if it is a token. |
|
| Put THIS in TOKEN_TRANSLATIONS if it is a token. |
|
||||||
`--------------------------------------------------*/
|
`--------------------------------------------------*/
|
||||||
|
|
||||||
static bool
|
static inline bool
|
||||||
symbol_translation (symbol *this)
|
symbol_translation (symbol *this)
|
||||||
{
|
{
|
||||||
/* Non-terminal? */
|
/* Non-terminal? */
|
||||||
@@ -364,6 +373,12 @@ symbol_translation (symbol *this)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED)
|
||||||
|
{
|
||||||
|
return symbol_translation (this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*----------------------.
|
/*----------------------.
|
||||||
| A symbol hash table. |
|
| A symbol hash table. |
|
||||||
@@ -374,18 +389,30 @@ symbol_translation (symbol *this)
|
|||||||
|
|
||||||
static struct hash_table *symbol_table = NULL;
|
static struct hash_table *symbol_table = NULL;
|
||||||
|
|
||||||
static bool
|
static inline bool
|
||||||
hash_compare_symbol (const symbol *m1, const symbol *m2)
|
hash_compare_symbol (const symbol *m1, const symbol *m2)
|
||||||
{
|
{
|
||||||
/* Since tags are unique, we can compare the pointers themselves. */
|
/* Since tags are unique, we can compare the pointers themselves. */
|
||||||
return UNIQSTR_EQ (m1->tag, m2->tag);
|
return UNIQSTR_EQ (m1->tag, m2->tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned int
|
static bool
|
||||||
|
hash_symbol_comparator (void const *m1, void const *m2)
|
||||||
|
{
|
||||||
|
return hash_compare_symbol (m1, m2);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline unsigned int
|
||||||
hash_symbol (const symbol *m, unsigned int tablesize)
|
hash_symbol (const symbol *m, unsigned int tablesize)
|
||||||
{
|
{
|
||||||
/* Since tags are unique, we can hash the pointer itself. */
|
/* Since tags are unique, we can hash the pointer itself. */
|
||||||
return ((size_t) m->tag) % tablesize;
|
return ((uintptr_t) m->tag) % tablesize;
|
||||||
|
}
|
||||||
|
|
||||||
|
static unsigned int
|
||||||
|
hash_symbol_hasher (void const *m, unsigned int tablesize)
|
||||||
|
{
|
||||||
|
return hash_symbol (m, tablesize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -398,9 +425,9 @@ symbols_new (void)
|
|||||||
{
|
{
|
||||||
symbol_table = hash_initialize (HT_INITIAL_CAPACITY,
|
symbol_table = hash_initialize (HT_INITIAL_CAPACITY,
|
||||||
NULL,
|
NULL,
|
||||||
(Hash_hasher) hash_symbol,
|
hash_symbol_hasher,
|
||||||
(Hash_comparator) hash_compare_symbol,
|
hash_symbol_comparator,
|
||||||
(Hash_data_freer) symbol_free);
|
free);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -417,7 +444,7 @@ symbol_get (const char *key, location loc)
|
|||||||
|
|
||||||
/* Keep the symbol in a printable form. */
|
/* Keep the symbol in a printable form. */
|
||||||
key = uniqstr_new (quotearg_style (escape_quoting_style, key));
|
key = uniqstr_new (quotearg_style (escape_quoting_style, key));
|
||||||
*(char const **) &probe.tag = key;
|
probe.tag = key;
|
||||||
entry = hash_lookup (symbol_table, &probe);
|
entry = hash_lookup (symbol_table, &probe);
|
||||||
|
|
||||||
if (!entry)
|
if (!entry)
|
||||||
@@ -469,12 +496,10 @@ symbols_free (void)
|
|||||||
| terminals. |
|
| terminals. |
|
||||||
`---------------------------------------------------------------*/
|
`---------------------------------------------------------------*/
|
||||||
|
|
||||||
void
|
static void
|
||||||
symbols_do (symbol_processor processor, void *processor_data)
|
symbols_do (Hash_processor processor, void *processor_data)
|
||||||
{
|
{
|
||||||
hash_do_for_each (symbol_table,
|
hash_do_for_each (symbol_table, processor, processor_data);
|
||||||
(Hash_processor) processor,
|
|
||||||
processor_data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -486,7 +511,7 @@ symbols_do (symbol_processor processor, void *processor_data)
|
|||||||
void
|
void
|
||||||
symbols_check_defined (void)
|
symbols_check_defined (void)
|
||||||
{
|
{
|
||||||
symbols_do (symbol_check_defined, NULL);
|
symbols_do (symbol_check_defined_processor, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*------------------------------------------------------------------.
|
/*------------------------------------------------------------------.
|
||||||
@@ -533,14 +558,14 @@ symbols_token_translations_init (void)
|
|||||||
max_user_token_number = this->user_token_number;
|
max_user_token_number = this->user_token_number;
|
||||||
}
|
}
|
||||||
|
|
||||||
token_translations = XCALLOC (symbol_number, max_user_token_number + 1);
|
CALLOC (token_translations, max_user_token_number + 1);
|
||||||
|
|
||||||
/* Initialize all entries for literal tokens to 2, the internal
|
/* Initialize all entries for literal tokens to 2, the internal
|
||||||
token number for $undefined, which represents all invalid inputs.
|
token number for $undefined, which represents all invalid inputs.
|
||||||
*/
|
*/
|
||||||
for (i = 0; i < max_user_token_number + 1; i++)
|
for (i = 0; i < max_user_token_number + 1; i++)
|
||||||
token_translations[i] = undeftoken->number;
|
token_translations[i] = undeftoken->number;
|
||||||
symbols_do (symbol_translation, NULL);
|
symbols_do (symbol_translation_processor, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -552,10 +577,10 @@ symbols_token_translations_init (void)
|
|||||||
void
|
void
|
||||||
symbols_pack (void)
|
symbols_pack (void)
|
||||||
{
|
{
|
||||||
symbols = XCALLOC (symbol *, nsyms);
|
CALLOC (symbols, nsyms);
|
||||||
|
|
||||||
symbols_do (symbol_check_alias_consistence, NULL);
|
symbols_do (symbol_check_alias_consistency_processor, NULL);
|
||||||
symbols_do (symbol_pack, NULL);
|
symbols_do (symbol_pack_processor, NULL);
|
||||||
|
|
||||||
symbols_token_translations_init ();
|
symbols_token_translations_init ();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user