]> git.saurik.com Git - bison.git/blob - src/symtab.c
allow modification on retrieved code_props.
[bison.git] / src / symtab.c
1 /* Symbol table manager for Bison.
2
3 Copyright (C) 1984, 1989, 2000-2002, 2004-2012 Free Software
4 Foundation, Inc.
5
6 This file is part of Bison, the GNU Compiler Compiler.
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include <config.h>
22 #include "system.h"
23
24 #include <hash.h>
25
26 #include "complain.h"
27 #include "gram.h"
28 #include "symtab.h"
29
30 /*-------------------------------------------------------------------.
31 | Symbols sorted by tag. Allocated by the first invocation of |
32 | symbols_do, after which no more symbols should be created. |
33 `-------------------------------------------------------------------*/
34
35 static symbol **symbols_sorted = NULL;
36 static symbol **semantic_types_sorted = NULL;
37
38 /*------------------------.
39 | Distinguished symbols. |
40 `------------------------*/
41
42 symbol *errtoken = NULL;
43 symbol *undeftoken = NULL;
44 symbol *endtoken = NULL;
45 symbol *accept = NULL;
46 symbol *startsymbol = NULL;
47 location startsymbol_location;
48
49 /*---------------------------------------.
50 | Default %destructor's and %printer's. |
51 `---------------------------------------*/
52
53 static code_props default_tagged_code_props[CODE_PROPS_SIZE] =
54 {
55 CODE_PROPS_NONE_INIT,
56 CODE_PROPS_NONE_INIT,
57 };
58 static code_props default_tagless_code_props[CODE_PROPS_SIZE] =
59 {
60 CODE_PROPS_NONE_INIT,
61 CODE_PROPS_NONE_INIT,
62 };
63
64 /*---------------------------------.
65 | Create a new symbol, named TAG. |
66 `---------------------------------*/
67
68 static symbol *
69 symbol_new (uniqstr tag, location loc)
70 {
71 symbol *res = xmalloc (sizeof *res);
72
73 uniqstr_assert (tag);
74
75 /* If the tag is not a string (starts with a double quote), check
76 that it is valid for Yacc. */
77 if (tag[0] != '\"' && tag[0] != '\'' && strchr (tag, '-'))
78 complain_at (loc, Wyacc,
79 _("POSIX Yacc forbids dashes in symbol names: %s"), tag);
80
81 res->tag = tag;
82 res->location = loc;
83
84 res->type_name = NULL;
85 for (int i = 0; i < CODE_PROPS_SIZE; ++i)
86 code_props_none_init (&res->props[i]);
87
88 res->number = NUMBER_UNDEFINED;
89 res->prec = 0;
90 res->assoc = undef_assoc;
91 res->user_token_number = USER_NUMBER_UNDEFINED;
92
93 res->alias = NULL;
94 res->class = unknown_sym;
95 res->status = undeclared;
96
97 if (nsyms == SYMBOL_NUMBER_MAXIMUM)
98 complain (fatal, _("too many symbols in input grammar (limit is %d)"),
99 SYMBOL_NUMBER_MAXIMUM);
100 nsyms++;
101 return res;
102 }
103
104 char const *
105 code_props_type_string (code_props_type kind)
106 {
107 switch (kind)
108 {
109 case destructor:
110 return "%destructor";
111 case printer:
112 return "%printer";
113 }
114 assert (0);
115 }
116
117 /*----------------------------------------.
118 | Create a new semantic type, named TAG. |
119 `----------------------------------------*/
120
121 static semantic_type *
122 semantic_type_new (uniqstr tag, const location *loc)
123 {
124 semantic_type *res = xmalloc (sizeof *res);
125
126 uniqstr_assert (tag);
127 res->tag = tag;
128 if (loc)
129 res->location = *loc;
130 for (int i = 0; i < CODE_PROPS_SIZE; ++i)
131 code_props_none_init (&res->props[i]);
132
133 return res;
134 }
135
136
137 /*-----------------.
138 | Print a symbol. |
139 `-----------------*/
140
141 #define SYMBOL_ATTR_PRINT(Attr) \
142 if (s->Attr) \
143 fprintf (f, " %s { %s }", #Attr, s->Attr)
144
145 #define SYMBOL_CODE_PRINT(Attr) \
146 if (s->props[Attr].code) \
147 fprintf (f, " %s { %s }", #Attr, s->props[Attr].code)
148
149 void
150 symbol_print (symbol const *s, FILE *f)
151 {
152 if (s)
153 {
154 fprintf (f, "\"%s\"", s->tag);
155 SYMBOL_ATTR_PRINT (type_name);
156 SYMBOL_CODE_PRINT (destructor);
157 SYMBOL_CODE_PRINT (printer);
158 }
159 else
160 fprintf (f, "<NULL>");
161 }
162
163 #undef SYMBOL_ATTR_PRINT
164 #undef SYMBOL_CODE_PRINT
165
166
167 /*----------------------------------.
168 | Whether S is a valid identifier. |
169 `----------------------------------*/
170
171 static bool
172 is_identifier (uniqstr s)
173 {
174 static char const alphanum[26 + 26 + 1 + 10] =
175 "abcdefghijklmnopqrstuvwxyz"
176 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
177 "_"
178 "0123456789";
179 if (!s || ! memchr (alphanum, *s, sizeof alphanum - 10))
180 return false;
181 for (++s; *s; ++s)
182 if (! memchr (alphanum, *s, sizeof alphanum))
183 return false;
184 return true;
185 }
186
187
188 /*-----------------------------------------------.
189 | Get the identifier associated to this symbol. |
190 `-----------------------------------------------*/
191 uniqstr
192 symbol_id_get (symbol const *sym)
193 {
194 aver (sym->user_token_number != USER_NUMBER_HAS_STRING_ALIAS);
195 if (sym->alias)
196 sym = sym->alias;
197 return is_identifier (sym->tag) ? sym->tag : 0;
198 }
199
200
201 /*------------------------------------------------------------------.
202 | Complain that S's WHAT is redeclared at SECOND, and was first set |
203 | at FIRST. |
204 `------------------------------------------------------------------*/
205
206 static void
207 symbol_redeclaration (symbol *s, const char *what, location first,
208 location second)
209 {
210 complain_at (second, complaint, _("%s redeclaration for %s"), what, s->tag);
211 complain_at (first, complaint, _("previous declaration"));
212 }
213
214 static void
215 semantic_type_redeclaration (semantic_type *s, const char *what, location first,
216 location second)
217 {
218 complain_at (second, complaint, _("%s redeclaration for <%s>"), what, s->tag);
219 complain_at (first, complaint, _("previous declaration"));
220 }
221
222
223
224 /*-----------------------------------------------------------------.
225 | Set the TYPE_NAME associated with SYM. Does nothing if passed 0 |
226 | as TYPE_NAME. |
227 `-----------------------------------------------------------------*/
228
229 void
230 symbol_type_set (symbol *sym, uniqstr type_name, location loc)
231 {
232 if (type_name)
233 {
234 if (sym->type_name)
235 symbol_redeclaration (sym, "%type", sym->type_location, loc);
236 uniqstr_assert (type_name);
237 sym->type_name = type_name;
238 sym->type_location = loc;
239 }
240 }
241
242 /*--------------------------------------------------------.
243 | Set the DESTRUCTOR or PRINTER associated with the SYM. |
244 `--------------------------------------------------------*/
245
246 void
247 symbol_code_props_set (symbol *sym, code_props_type kind,
248 code_props const *code)
249 {
250 if (sym->props[kind].code)
251 symbol_redeclaration (sym, code_props_type_string (kind),
252 sym->props[kind].location,
253 code->location);
254 sym->props[kind] = *code;
255 }
256
257 /*-----------------------------------------------------.
258 | Set the DESTRUCTOR or PRINTER associated with TYPE. |
259 `-----------------------------------------------------*/
260
261 void
262 semantic_type_code_props_set (semantic_type *type,
263 code_props_type kind,
264 code_props const *code)
265 {
266 if (type->props[kind].code)
267 semantic_type_redeclaration (type, code_props_type_string (kind),
268 type->props[kind].location,
269 code->location);
270 type->props[kind] = *code;
271 }
272
273 /*---------------------------------------------------.
274 | Get the computed %destructor or %printer for SYM. |
275 `---------------------------------------------------*/
276
277 code_props *
278 symbol_code_props_get (symbol *sym, code_props_type kind)
279 {
280 /* Per-symbol code props. */
281 if (sym->props[kind].code)
282 return &sym->props[kind];
283
284 /* Per-type code props. */
285 if (sym->type_name)
286 {
287 code_props *code =
288 &semantic_type_get (sym->type_name, NULL)->props[kind];
289 if (code->code)
290 return code;
291 }
292
293 /* Apply default code props's only to user-defined symbols. */
294 if (sym->tag[0] == '$' || sym == errtoken)
295 return &code_props_none;
296
297 if (sym->type_name)
298 return &default_tagged_code_props[kind];
299 return &default_tagless_code_props[kind];
300 }
301
302 /*-----------------------------------------------------------------.
303 | Set the PRECEDENCE associated with SYM. Does nothing if invoked |
304 | with UNDEF_ASSOC as ASSOC. |
305 `-----------------------------------------------------------------*/
306
307 void
308 symbol_precedence_set (symbol *sym, int prec, assoc a, location loc)
309 {
310 if (a != undef_assoc)
311 {
312 if (sym->prec != 0)
313 symbol_redeclaration (sym, assoc_to_string (a), sym->prec_location,
314 loc);
315 sym->prec = prec;
316 sym->assoc = a;
317 sym->prec_location = loc;
318 }
319
320 /* Only terminals have a precedence. */
321 symbol_class_set (sym, token_sym, loc, false);
322 }
323
324
325 /*------------------------------------.
326 | Set the CLASS associated with SYM. |
327 `------------------------------------*/
328
329 void
330 symbol_class_set (symbol *sym, symbol_class class, location loc, bool declaring)
331 {
332 bool warned = false;
333 if (sym->class != unknown_sym && sym->class != class)
334 {
335 complain_at (loc, complaint, _("symbol %s redefined"), sym->tag);
336 // Don't report both "redefined" and "redeclared".
337 warned = true;
338 }
339
340 if (class == nterm_sym && sym->class != nterm_sym)
341 sym->number = nvars++;
342 else if (class == token_sym && sym->number == NUMBER_UNDEFINED)
343 sym->number = ntokens++;
344
345 sym->class = class;
346
347 if (declaring)
348 {
349 if (sym->status == declared && !warned)
350 complain_at (loc, Wother, _("symbol %s redeclared"), sym->tag);
351 sym->status = declared;
352 }
353 }
354
355
356 /*------------------------------------------------.
357 | Set the USER_TOKEN_NUMBER associated with SYM. |
358 `------------------------------------------------*/
359
360 void
361 symbol_user_token_number_set (symbol *sym, int user_token_number, location loc)
362 {
363 int *user_token_numberp;
364
365 if (sym->user_token_number != USER_NUMBER_HAS_STRING_ALIAS)
366 user_token_numberp = &sym->user_token_number;
367 else
368 user_token_numberp = &sym->alias->user_token_number;
369 if (*user_token_numberp != USER_NUMBER_UNDEFINED
370 && *user_token_numberp != user_token_number)
371 complain_at (loc, complaint, _("redefining user token number of %s"),
372 sym->tag);
373
374 *user_token_numberp = user_token_number;
375 /* User defined $end token? */
376 if (user_token_number == 0)
377 {
378 endtoken = sym;
379 /* It is always mapped to 0, so it was already counted in
380 NTOKENS. */
381 if (endtoken->number != NUMBER_UNDEFINED)
382 --ntokens;
383 endtoken->number = 0;
384 }
385 }
386
387
388 /*----------------------------------------------------------.
389 | If SYM is not defined, report an error, and consider it a |
390 | nonterminal. |
391 `----------------------------------------------------------*/
392
393 static inline bool
394 symbol_check_defined (symbol *sym)
395 {
396 if (sym->class == unknown_sym)
397 {
398 switch (sym->status)
399 {
400 case used:
401 complain_at (sym->location, Wother,
402 _("symbol %s is used, but is not defined as a token"
403 " and has no rules"),
404 sym->tag);
405 break;
406 case undeclared:
407 case needed:
408 complain_at (sym->location, complaint,
409 _("symbol %s is used, but is not defined as a token"
410 " and has no rules"),
411 sym->tag);
412 break;
413 case declared:
414 /* If declared, then sym->class != unknown_sym. */
415 assert (0);
416 }
417
418 sym->class = nterm_sym;
419 sym->number = nvars++;
420 }
421
422 for (int i = 0; i < 2; ++i)
423 symbol_code_props_get (sym, i)->is_used = true;
424
425 /* Set the semantic type status associated to the current symbol to
426 'declared' so that we could check semantic types unnecessary uses. */
427 if (sym->type_name)
428 {
429 semantic_type *sem_type = semantic_type_get (sym->type_name, NULL);
430 if (sem_type)
431 sem_type->status = declared;
432 }
433
434 return true;
435 }
436
437 static inline bool
438 semantic_type_check_defined (semantic_type *sem_type)
439 {
440 if (sem_type->status == declared)
441 {
442 for (int i = 0; i < 2; ++i)
443 if (sem_type->props[i].kind != CODE_PROPS_NONE
444 && ! sem_type->props[i].is_used)
445 complain_at (sem_type->location, Wother,
446 _("useless %s for type <%s>"),
447 code_props_type_string (i), sem_type->tag);
448 }
449 else
450 complain_at (sem_type->location, Wother,
451 _("type <%s> is used, but is not associated to any symbol"),
452 sem_type->tag);
453
454 return true;
455 }
456
457 static bool
458 symbol_check_defined_processor (void *sym, void *null ATTRIBUTE_UNUSED)
459 {
460 return symbol_check_defined (sym);
461 }
462
463 static bool
464 semantic_type_check_defined_processor (void *sem_type,
465 void *null ATTRIBUTE_UNUSED)
466 {
467 return semantic_type_check_defined (sem_type);
468 }
469
470
471 void
472 symbol_make_alias (symbol *sym, symbol *str, location loc)
473 {
474 if (str->alias)
475 complain_at (loc, Wother,
476 _("symbol %s used more than once as a literal string"), str->tag);
477 else if (sym->alias)
478 complain_at (loc, Wother,
479 _("symbol %s given more than one literal string"), sym->tag);
480 else
481 {
482 str->class = token_sym;
483 str->user_token_number = sym->user_token_number;
484 sym->user_token_number = USER_NUMBER_HAS_STRING_ALIAS;
485 str->alias = sym;
486 sym->alias = str;
487 str->number = sym->number;
488 symbol_type_set (str, sym->type_name, loc);
489 }
490 }
491
492
493 /*---------------------------------------------------------.
494 | Check that THIS, and its alias, have same precedence and |
495 | associativity. |
496 `---------------------------------------------------------*/
497
498 static inline void
499 symbol_check_alias_consistency (symbol *this)
500 {
501 symbol *sym = this;
502 symbol *str = this->alias;
503
504 /* Check only the symbol in the symbol-string pair. */
505 if (!(this->alias
506 && this->user_token_number == USER_NUMBER_HAS_STRING_ALIAS))
507 return;
508
509 if (str->type_name != sym->type_name)
510 {
511 if (str->type_name)
512 symbol_type_set (sym, str->type_name, str->type_location);
513 else
514 symbol_type_set (str, sym->type_name, sym->type_location);
515 }
516
517
518 for (int i = 0; i < CODE_PROPS_SIZE; ++i)
519 if (str->props[i].code)
520 symbol_code_props_set (sym, i, &str->props[i]);
521 else if (sym->props[i].code)
522 symbol_code_props_set (str, i, &sym->props[i]);
523
524 if (sym->prec || str->prec)
525 {
526 if (str->prec)
527 symbol_precedence_set (sym, str->prec, str->assoc,
528 str->prec_location);
529 else
530 symbol_precedence_set (str, sym->prec, sym->assoc,
531 sym->prec_location);
532 }
533 }
534
535 static bool
536 symbol_check_alias_consistency_processor (void *this,
537 void *null ATTRIBUTE_UNUSED)
538 {
539 symbol_check_alias_consistency (this);
540 return true;
541 }
542
543
544 /*-------------------------------------------------------------------.
545 | Assign a symbol number, and write the definition of the token name |
546 | into FDEFINES. Put in SYMBOLS. |
547 `-------------------------------------------------------------------*/
548
549 static inline bool
550 symbol_pack (symbol *this)
551 {
552 aver (this->number != NUMBER_UNDEFINED);
553 if (this->class == nterm_sym)
554 this->number += ntokens;
555 else if (this->user_token_number == USER_NUMBER_HAS_STRING_ALIAS)
556 return true;
557
558 symbols[this->number] = this;
559 return true;
560 }
561
562 static bool
563 symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED)
564 {
565 return symbol_pack (this);
566 }
567
568
569 static void
570 user_token_number_redeclaration (int num, symbol *first, symbol *second)
571 {
572 /* User token numbers are not assigned during the parsing, but in a
573 second step, via a traversal of the symbol table sorted on tag.
574
575 However, error messages make more sense if we keep the first
576 declaration first. */
577 if (location_cmp (first->location, second->location) > 0)
578 {
579 symbol* tmp = first;
580 first = second;
581 second = tmp;
582 }
583 complain_at (second->location, complaint,
584 _("user token number %d redeclaration for %s"),
585 num, second->tag);
586 complain_at (first->location, complaint, _("previous declaration for %s"),
587 first->tag);
588 }
589
590 /*--------------------------------------------------.
591 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
592 `--------------------------------------------------*/
593
594 static inline bool
595 symbol_translation (symbol *this)
596 {
597 /* Non-terminal? */
598 if (this->class == token_sym
599 && this->user_token_number != USER_NUMBER_HAS_STRING_ALIAS)
600 {
601 /* A token which translation has already been set? */
602 if (token_translations[this->user_token_number] != undeftoken->number)
603 user_token_number_redeclaration
604 (this->user_token_number,
605 symbols[token_translations[this->user_token_number]],
606 this);
607
608 token_translations[this->user_token_number] = this->number;
609 }
610
611 return true;
612 }
613
614 static bool
615 symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED)
616 {
617 return symbol_translation (this);
618 }
619
620
621 /*---------------------------------------.
622 | Symbol and semantic type hash tables. |
623 `---------------------------------------*/
624
625 /* Initial capacity of symbol and semantic type hash table. */
626 #define HT_INITIAL_CAPACITY 257
627
628 static struct hash_table *symbol_table = NULL;
629 static struct hash_table *semantic_type_table = NULL;
630
631 static inline bool
632 hash_compare_symbol (const symbol *m1, const symbol *m2)
633 {
634 /* Since tags are unique, we can compare the pointers themselves. */
635 return UNIQSTR_EQ (m1->tag, m2->tag);
636 }
637
638 static inline bool
639 hash_compare_semantic_type (const semantic_type *m1, const semantic_type *m2)
640 {
641 /* Since names are unique, we can compare the pointers themselves. */
642 return UNIQSTR_EQ (m1->tag, m2->tag);
643 }
644
645 static bool
646 hash_symbol_comparator (void const *m1, void const *m2)
647 {
648 return hash_compare_symbol (m1, m2);
649 }
650
651 static bool
652 hash_semantic_type_comparator (void const *m1, void const *m2)
653 {
654 return hash_compare_semantic_type (m1, m2);
655 }
656
657 static inline size_t
658 hash_symbol (const symbol *m, size_t tablesize)
659 {
660 /* Since tags are unique, we can hash the pointer itself. */
661 return ((uintptr_t) m->tag) % tablesize;
662 }
663
664 static inline size_t
665 hash_semantic_type (const semantic_type *m, size_t tablesize)
666 {
667 /* Since names are unique, we can hash the pointer itself. */
668 return ((uintptr_t) m->tag) % tablesize;
669 }
670
671 static size_t
672 hash_symbol_hasher (void const *m, size_t tablesize)
673 {
674 return hash_symbol (m, tablesize);
675 }
676
677 static size_t
678 hash_semantic_type_hasher (void const *m, size_t tablesize)
679 {
680 return hash_semantic_type (m, tablesize);
681 }
682
683 /*-------------------------------.
684 | Create the symbol hash table. |
685 `-------------------------------*/
686
687 void
688 symbols_new (void)
689 {
690 symbol_table = hash_initialize (HT_INITIAL_CAPACITY,
691 NULL,
692 hash_symbol_hasher,
693 hash_symbol_comparator,
694 free);
695 semantic_type_table = hash_initialize (HT_INITIAL_CAPACITY,
696 NULL,
697 hash_semantic_type_hasher,
698 hash_semantic_type_comparator,
699 free);
700 }
701
702
703 /*----------------------------------------------------------------.
704 | Find the symbol named KEY, and return it. If it does not exist |
705 | yet, create it. |
706 `----------------------------------------------------------------*/
707
708 symbol *
709 symbol_from_uniqstr (const uniqstr key, location loc)
710 {
711 symbol probe;
712 symbol *entry;
713
714 probe.tag = key;
715 entry = hash_lookup (symbol_table, &probe);
716
717 if (!entry)
718 {
719 /* First insertion in the hash. */
720 aver (!symbols_sorted);
721 entry = symbol_new (key, loc);
722 if (!hash_insert (symbol_table, entry))
723 xalloc_die ();
724 }
725 return entry;
726 }
727
728
729 /*-----------------------------------------------------------------------.
730 | Find the semantic type named KEY, and return it. If it does not exist |
731 | yet, create it. |
732 `-----------------------------------------------------------------------*/
733
734 semantic_type *
735 semantic_type_from_uniqstr (const uniqstr key, const location *loc)
736 {
737 semantic_type probe;
738 semantic_type *entry;
739
740 probe.tag = key;
741 entry = hash_lookup (semantic_type_table, &probe);
742
743 if (!entry)
744 {
745 /* First insertion in the hash. */
746 entry = semantic_type_new (key, loc);
747 if (!hash_insert (semantic_type_table, entry))
748 xalloc_die ();
749 }
750 return entry;
751 }
752
753
754 /*----------------------------------------------------------------.
755 | Find the symbol named KEY, and return it. If it does not exist |
756 | yet, create it. |
757 `----------------------------------------------------------------*/
758
759 symbol *
760 symbol_get (const char *key, location loc)
761 {
762 return symbol_from_uniqstr (uniqstr_new (key), loc);
763 }
764
765
766 /*-----------------------------------------------------------------------.
767 | Find the semantic type named KEY, and return it. If it does not exist |
768 | yet, create it. |
769 `-----------------------------------------------------------------------*/
770
771 semantic_type *
772 semantic_type_get (const char *key, const location *loc)
773 {
774 return semantic_type_from_uniqstr (uniqstr_new (key), loc);
775 }
776
777
778 /*------------------------------------------------------------------.
779 | Generate a dummy nonterminal, whose name cannot conflict with the |
780 | user's names. |
781 `------------------------------------------------------------------*/
782
783 symbol *
784 dummy_symbol_get (location loc)
785 {
786 /* Incremented for each generated symbol. */
787 static int dummy_count = 0;
788 static char buf[256];
789
790 symbol *sym;
791
792 sprintf (buf, "$@%d", ++dummy_count);
793 sym = symbol_get (buf, loc);
794 sym->class = nterm_sym;
795 sym->number = nvars++;
796 return sym;
797 }
798
799 bool
800 symbol_is_dummy (const symbol *sym)
801 {
802 return sym->tag[0] == '@' || (sym->tag[0] == '$' && sym->tag[1] == '@');
803 }
804
805 /*-------------------.
806 | Free the symbols. |
807 `-------------------*/
808
809 void
810 symbols_free (void)
811 {
812 hash_free (symbol_table);
813 hash_free (semantic_type_table);
814 free (symbols);
815 free (symbols_sorted);
816 }
817
818
819 /*---------------------------------------------------------------.
820 | Look for undefined symbols, report an error, and consider them |
821 | terminals. |
822 `---------------------------------------------------------------*/
823
824 static int
825 symbols_cmp (symbol const *a, symbol const *b)
826 {
827 return strcmp (a->tag, b->tag);
828 }
829
830 static int
831 symbols_cmp_qsort (void const *a, void const *b)
832 {
833 return symbols_cmp (*(symbol * const *)a, *(symbol * const *)b);
834 }
835
836 static void
837 symbols_do (Hash_processor processor, void *processor_data,
838 struct hash_table *table, symbol **sorted)
839 {
840 size_t count = hash_get_n_entries (table);
841 if (!sorted)
842 {
843 sorted = xnmalloc (count, sizeof *sorted);
844 hash_get_entries (table, (void**)sorted, count);
845 qsort (sorted, count, sizeof *sorted, symbols_cmp_qsort);
846 }
847 {
848 size_t i;
849 for (i = 0; i < count; ++i)
850 processor (sorted[i], processor_data);
851 }
852 }
853
854 /*--------------------------------------------------------------.
855 | Check that all the symbols are defined. Report any undefined |
856 | symbols and consider them nonterminals. |
857 `--------------------------------------------------------------*/
858
859 void
860 symbols_check_defined (void)
861 {
862 symbols_do (symbol_check_defined_processor, NULL,
863 symbol_table, symbols_sorted);
864 symbols_do (semantic_type_check_defined_processor, NULL,
865 semantic_type_table, semantic_types_sorted);
866 }
867
868 /*------------------------------------------------------------------.
869 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
870 | number. |
871 `------------------------------------------------------------------*/
872
873 static void
874 symbols_token_translations_init (void)
875 {
876 bool num_256_available_p = true;
877 int i;
878
879 /* Find the highest user token number, and whether 256, the POSIX
880 preferred user token number for the error token, is used. */
881 max_user_token_number = 0;
882 for (i = 0; i < ntokens; ++i)
883 {
884 symbol *this = symbols[i];
885 if (this->user_token_number != USER_NUMBER_UNDEFINED)
886 {
887 if (this->user_token_number > max_user_token_number)
888 max_user_token_number = this->user_token_number;
889 if (this->user_token_number == 256)
890 num_256_available_p = false;
891 }
892 }
893
894 /* If 256 is not used, assign it to error, to follow POSIX. */
895 if (num_256_available_p
896 && errtoken->user_token_number == USER_NUMBER_UNDEFINED)
897 errtoken->user_token_number = 256;
898
899 /* Set the missing user numbers. */
900 if (max_user_token_number < 256)
901 max_user_token_number = 256;
902
903 for (i = 0; i < ntokens; ++i)
904 {
905 symbol *this = symbols[i];
906 if (this->user_token_number == USER_NUMBER_UNDEFINED)
907 this->user_token_number = ++max_user_token_number;
908 if (this->user_token_number > max_user_token_number)
909 max_user_token_number = this->user_token_number;
910 }
911
912 token_translations = xnmalloc (max_user_token_number + 1,
913 sizeof *token_translations);
914
915 /* Initialize all entries for literal tokens to the internal token
916 number for $undefined, which represents all invalid inputs. */
917 for (i = 0; i < max_user_token_number + 1; i++)
918 token_translations[i] = undeftoken->number;
919 symbols_do (symbol_translation_processor, NULL,
920 symbol_table, symbols_sorted);
921 }
922
923
924 /*----------------------------------------------------------------.
925 | Assign symbol numbers, and write definition of token names into |
926 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
927 `----------------------------------------------------------------*/
928
929 void
930 symbols_pack (void)
931 {
932 symbols_do (symbol_check_alias_consistency_processor, NULL,
933 symbol_table, symbols_sorted);
934
935 symbols = xcalloc (nsyms, sizeof *symbols);
936 symbols_do (symbol_pack_processor, NULL, symbol_table, symbols_sorted);
937
938 /* Aliases leave empty slots in symbols, so remove them. */
939 {
940 int writei;
941 int readi;
942 int nsyms_old = nsyms;
943 for (writei = 0, readi = 0; readi < nsyms_old; readi += 1)
944 {
945 if (symbols[readi] == NULL)
946 {
947 nsyms -= 1;
948 ntokens -= 1;
949 }
950 else
951 {
952 symbols[writei] = symbols[readi];
953 symbols[writei]->number = writei;
954 if (symbols[writei]->alias)
955 symbols[writei]->alias->number = writei;
956 writei += 1;
957 }
958 }
959 }
960 symbols = xnrealloc (symbols, nsyms, sizeof *symbols);
961
962 symbols_token_translations_init ();
963
964 if (startsymbol->class == unknown_sym)
965 complain_at (startsymbol_location, fatal,
966 _("the start symbol %s is undefined"),
967 startsymbol->tag);
968 else if (startsymbol->class == token_sym)
969 complain_at (startsymbol_location, fatal,
970 _("the start symbol %s is a token"),
971 startsymbol->tag);
972 }
973
974
975 /*--------------------------------------------------.
976 | Set default tagged/tagless %destructor/%printer. |
977 `--------------------------------------------------*/
978
979 void
980 default_tagged_code_props_set (code_props_type kind, code_props const *code)
981 {
982 if (default_tagged_code_props[kind].code)
983 {
984 complain_at (code->location, complaint,
985 _("redeclaration for default tagged %s"),
986 code_props_type_string (kind));
987 complain_at (default_tagged_code_props[kind].location, complaint,
988 _("previous declaration"));
989 }
990 default_tagged_code_props[kind] = *code;
991 }
992
993 void
994 default_tagless_code_props_set (code_props_type kind, code_props const *code)
995 {
996 if (default_tagless_code_props[kind].code)
997 {
998 complain_at (code->location, complaint,
999 _("redeclaration for default tagless %s"),
1000 code_props_type_string (kind));
1001 complain_at (default_tagless_code_props[kind].location, complaint,
1002 _("previous declaration"));
1003 }
1004 default_tagless_code_props[kind] = *code;
1005 }