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