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