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