1 |
/* Symbol table manager for Bison, |
/* Symbol table manager for Bison. |
2 |
|
|
3 |
Copyright (C) 1984, 1989, 2000, 2001, 2002 Free Software Foundation, Inc. |
Copyright (C) 1984, 1989, 2000, 2001, 2002 Free Software Foundation, Inc. |
4 |
|
|
5 |
This file is part of Bison, the GNU Compiler Compiler. |
This file is part of Bison, the GNU Compiler Compiler. |
47 |
static symbol * |
static symbol * |
48 |
symbol_new (uniqstr tag, location loc) |
symbol_new (uniqstr tag, location loc) |
49 |
{ |
{ |
50 |
symbol *res = XMALLOC (symbol, 1); |
symbol *res = MALLOC (res, 1); |
51 |
|
|
52 |
uniqstr_assert (tag); |
uniqstr_assert (tag); |
53 |
res->tag = tag; |
res->tag = tag; |
191 |
} |
} |
192 |
|
|
193 |
|
|
|
/*-----------. |
|
|
| Free SYM. | |
|
|
`-----------*/ |
|
|
|
|
|
static void |
|
|
symbol_free (symbol *sym) |
|
|
{ |
|
|
free (sym); |
|
|
} |
|
|
|
|
|
|
|
194 |
/*----------------------------------------------------------. |
/*----------------------------------------------------------. |
195 |
| If SYM is not defined, report an error, and consider it a | |
| If SYM is not defined, report an error, and consider it a | |
196 |
| nonterminal. | |
| nonterminal. | |
197 |
`----------------------------------------------------------*/ |
`----------------------------------------------------------*/ |
198 |
|
|
199 |
static bool |
static inline bool |
200 |
symbol_check_defined (symbol *sym) |
symbol_check_defined (symbol *sym) |
201 |
{ |
{ |
202 |
if (sym->class == unknown_sym) |
if (sym->class == unknown_sym) |
212 |
return true; |
return true; |
213 |
} |
} |
214 |
|
|
215 |
|
static bool |
216 |
|
symbol_check_defined_processor (void *sym, void *null ATTRIBUTE_UNUSED) |
217 |
|
{ |
218 |
|
return symbol_check_defined (sym); |
219 |
|
} |
220 |
|
|
221 |
|
|
222 |
/*------------------------------------------------------------------. |
/*------------------------------------------------------------------. |
223 |
| 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 | |
256 |
| associativity. | |
| associativity. | |
257 |
`---------------------------------------------------------*/ |
`---------------------------------------------------------*/ |
258 |
|
|
259 |
static bool |
static inline bool |
260 |
symbol_check_alias_consistence (symbol *this) |
symbol_check_alias_consistency (symbol *this) |
261 |
{ |
{ |
262 |
/* Check only those who _are_ the aliases. */ |
/* Check only those who _are_ the aliases. */ |
263 |
if (this->alias && this->user_token_number == USER_NUMBER_ALIAS) |
if (this->alias && this->user_token_number == USER_NUMBER_ALIAS) |
290 |
return true; |
return true; |
291 |
} |
} |
292 |
|
|
293 |
|
static bool |
294 |
|
symbol_check_alias_consistency_processor (void *this, |
295 |
|
void *null ATTRIBUTE_UNUSED) |
296 |
|
{ |
297 |
|
return symbol_check_alias_consistency (this); |
298 |
|
} |
299 |
|
|
300 |
|
|
301 |
/*-------------------------------------------------------------------. |
/*-------------------------------------------------------------------. |
302 |
| Assign a symbol number, and write the definition of the token name | |
| Assign a symbol number, and write the definition of the token name | |
303 |
| into FDEFINES. Put in SYMBOLS. | |
| into FDEFINES. Put in SYMBOLS. | |
304 |
`-------------------------------------------------------------------*/ |
`-------------------------------------------------------------------*/ |
305 |
|
|
306 |
static bool |
static inline bool |
307 |
symbol_pack (symbol *this) |
symbol_pack (symbol *this) |
308 |
{ |
{ |
309 |
if (this->class == nterm_sym) |
if (this->class == nterm_sym) |
340 |
return true; |
return true; |
341 |
} |
} |
342 |
|
|
343 |
|
static bool |
344 |
|
symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED) |
345 |
|
{ |
346 |
|
return symbol_pack (this); |
347 |
|
} |
348 |
|
|
349 |
|
|
350 |
|
|
351 |
|
|
353 |
| Put THIS in TOKEN_TRANSLATIONS if it is a token. | |
| Put THIS in TOKEN_TRANSLATIONS if it is a token. | |
354 |
`--------------------------------------------------*/ |
`--------------------------------------------------*/ |
355 |
|
|
356 |
static bool |
static inline bool |
357 |
symbol_translation (symbol *this) |
symbol_translation (symbol *this) |
358 |
{ |
{ |
359 |
/* Non-terminal? */ |
/* Non-terminal? */ |
373 |
return true; |
return true; |
374 |
} |
} |
375 |
|
|
376 |
|
static bool |
377 |
|
symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED) |
378 |
|
{ |
379 |
|
return symbol_translation (this); |
380 |
|
} |
381 |
|
|
382 |
|
|
383 |
/*----------------------. |
/*----------------------. |
384 |
| A symbol hash table. | |
| A symbol hash table. | |
389 |
|
|
390 |
static struct hash_table *symbol_table = NULL; |
static struct hash_table *symbol_table = NULL; |
391 |
|
|
392 |
static bool |
static inline bool |
393 |
hash_compare_symbol (const symbol *m1, const symbol *m2) |
hash_compare_symbol (const symbol *m1, const symbol *m2) |
394 |
{ |
{ |
395 |
/* Since tags are unique, we can compare the pointers themselves. */ |
/* Since tags are unique, we can compare the pointers themselves. */ |
396 |
return UNIQSTR_EQ (m1->tag, m2->tag); |
return UNIQSTR_EQ (m1->tag, m2->tag); |
397 |
} |
} |
398 |
|
|
399 |
static unsigned int |
static bool |
400 |
|
hash_symbol_comparator (void const *m1, void const *m2) |
401 |
|
{ |
402 |
|
return hash_compare_symbol (m1, m2); |
403 |
|
} |
404 |
|
|
405 |
|
static inline unsigned int |
406 |
hash_symbol (const symbol *m, unsigned int tablesize) |
hash_symbol (const symbol *m, unsigned int tablesize) |
407 |
{ |
{ |
408 |
/* Since tags are unique, we can hash the pointer itself. */ |
/* Since tags are unique, we can hash the pointer itself. */ |
409 |
return ((size_t) m->tag) % tablesize; |
return ((uintptr_t) m->tag) % tablesize; |
410 |
|
} |
411 |
|
|
412 |
|
static unsigned int |
413 |
|
hash_symbol_hasher (void const *m, unsigned int tablesize) |
414 |
|
{ |
415 |
|
return hash_symbol (m, tablesize); |
416 |
} |
} |
417 |
|
|
418 |
|
|
425 |
{ |
{ |
426 |
symbol_table = hash_initialize (HT_INITIAL_CAPACITY, |
symbol_table = hash_initialize (HT_INITIAL_CAPACITY, |
427 |
NULL, |
NULL, |
428 |
(Hash_hasher) hash_symbol, |
hash_symbol_hasher, |
429 |
(Hash_comparator) hash_compare_symbol, |
hash_symbol_comparator, |
430 |
(Hash_data_freer) symbol_free); |
free); |
431 |
} |
} |
432 |
|
|
433 |
|
|
444 |
|
|
445 |
/* Keep the symbol in a printable form. */ |
/* Keep the symbol in a printable form. */ |
446 |
key = uniqstr_new (quotearg_style (escape_quoting_style, key)); |
key = uniqstr_new (quotearg_style (escape_quoting_style, key)); |
447 |
*(char const **) &probe.tag = key; |
probe.tag = key; |
448 |
entry = hash_lookup (symbol_table, &probe); |
entry = hash_lookup (symbol_table, &probe); |
449 |
|
|
450 |
if (!entry) |
if (!entry) |
496 |
| terminals. | |
| terminals. | |
497 |
`---------------------------------------------------------------*/ |
`---------------------------------------------------------------*/ |
498 |
|
|
499 |
void |
static void |
500 |
symbols_do (symbol_processor processor, void *processor_data) |
symbols_do (Hash_processor processor, void *processor_data) |
501 |
{ |
{ |
502 |
hash_do_for_each (symbol_table, |
hash_do_for_each (symbol_table, processor, processor_data); |
|
(Hash_processor) processor, |
|
|
processor_data); |
|
503 |
} |
} |
504 |
|
|
505 |
|
|
511 |
void |
void |
512 |
symbols_check_defined (void) |
symbols_check_defined (void) |
513 |
{ |
{ |
514 |
symbols_do (symbol_check_defined, NULL); |
symbols_do (symbol_check_defined_processor, NULL); |
515 |
} |
} |
516 |
|
|
517 |
/*------------------------------------------------------------------. |
/*------------------------------------------------------------------. |
558 |
max_user_token_number = this->user_token_number; |
max_user_token_number = this->user_token_number; |
559 |
} |
} |
560 |
|
|
561 |
token_translations = XCALLOC (symbol_number, max_user_token_number + 1); |
CALLOC (token_translations, max_user_token_number + 1); |
562 |
|
|
563 |
/* Initialize all entries for literal tokens to 2, the internal |
/* Initialize all entries for literal tokens to 2, the internal |
564 |
token number for $undefined, which represents all invalid inputs. |
token number for $undefined, which represents all invalid inputs. |
565 |
*/ |
*/ |
566 |
for (i = 0; i < max_user_token_number + 1; i++) |
for (i = 0; i < max_user_token_number + 1; i++) |
567 |
token_translations[i] = undeftoken->number; |
token_translations[i] = undeftoken->number; |
568 |
symbols_do (symbol_translation, NULL); |
symbols_do (symbol_translation_processor, NULL); |
569 |
} |
} |
570 |
|
|
571 |
|
|
577 |
void |
void |
578 |
symbols_pack (void) |
symbols_pack (void) |
579 |
{ |
{ |
580 |
symbols = XCALLOC (symbol *, nsyms); |
CALLOC (symbols, nsyms); |
581 |
|
|
582 |
symbols_do (symbol_check_alias_consistence, NULL); |
symbols_do (symbol_check_alias_consistency_processor, NULL); |
583 |
symbols_do (symbol_pack, NULL); |
symbols_do (symbol_pack_processor, NULL); |
584 |
|
|
585 |
symbols_token_translations_init (); |
symbols_token_translations_init (); |
586 |
|
|