Do not use date ranges in copyright notices.
[bison.git] / src / muscle-tab.c
blobbcd96dfdbac13500ee3bffcef9d7b6a0b5943638
1 /* Muscle table manager for Bison.
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
4 2010 Free Software Foundation, Inc.
6 This file is part of Bison, the GNU Compiler Compiler.
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.
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.
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/>. */
21 #include <config.h>
22 #include "system.h"
24 #include <hash.h>
25 #include <quotearg.h>
27 #include "complain.h"
28 #include "files.h"
29 #include "muscle-tab.h"
30 #include "getargs.h"
32 /* A key-value pair, along with storage that can be reclaimed when
33 this pair is no longer needed. */
34 typedef struct
36 char const *key;
37 char const *value;
38 char *storage;
39 } muscle_entry;
41 /* An obstack used to create some entries. */
42 struct obstack muscle_obstack;
44 /* Initial capacity of muscles hash table. */
45 #define HT_INITIAL_CAPACITY 257
47 static struct hash_table *muscle_table = NULL;
49 static bool
50 hash_compare_muscles (void const *x, void const *y)
52 muscle_entry const *m1 = x;
53 muscle_entry const *m2 = y;
54 return strcmp (m1->key, m2->key) == 0;
57 static size_t
58 hash_muscle (const void *x, size_t tablesize)
60 muscle_entry const *m = x;
61 return hash_string (m->key, tablesize);
64 /*-----------------------------------------------------------------.
65 | Create the MUSCLE_TABLE, and initialize it with default values. |
66 | Also set up the MUSCLE_OBSTACK. |
67 `-----------------------------------------------------------------*/
69 static void
70 muscle_entry_free (void *entry)
72 muscle_entry *mentry = entry;
73 free (mentry->storage);
74 free (mentry);
77 void
78 muscle_init (void)
80 /* Initialize the muscle obstack. */
81 obstack_init (&muscle_obstack);
83 muscle_table = hash_initialize (HT_INITIAL_CAPACITY, NULL, hash_muscle,
84 hash_compare_muscles, muscle_entry_free);
86 /* Version and input file. */
87 MUSCLE_INSERT_STRING ("version", VERSION);
91 /*------------------------------------------------------------.
92 | Free all the memory consumed by the muscle machinery only. |
93 `------------------------------------------------------------*/
95 void
96 muscle_free (void)
98 hash_free (muscle_table);
99 obstack_free (&muscle_obstack, NULL);
104 /*------------------------------------------------------------.
105 | Insert (KEY, VALUE). If KEY already existed, overwrite the |
106 | previous value. |
107 `------------------------------------------------------------*/
109 void
110 muscle_insert (char const *key, char const *value)
112 muscle_entry probe;
113 muscle_entry *entry;
115 probe.key = key;
116 entry = hash_lookup (muscle_table, &probe);
118 if (!entry)
120 /* First insertion in the hash. */
121 entry = xmalloc (sizeof *entry);
122 entry->key = key;
123 if (!hash_insert (muscle_table, entry))
124 xalloc_die ();
126 else
127 free (entry->storage);
128 entry->value = value;
129 entry->storage = NULL;
133 /*-------------------------------------------------------------------.
134 | Append VALUE to the current value of KEY. If KEY did not already |
135 | exist, create it. Use MUSCLE_OBSTACK. De-allocate the previously |
136 | associated value. Copy VALUE and SEPARATOR. |
137 `-------------------------------------------------------------------*/
139 void
140 muscle_grow (const char *key, const char *val, const char *separator)
142 muscle_entry probe;
143 muscle_entry *entry = NULL;
145 probe.key = key;
146 entry = hash_lookup (muscle_table, &probe);
148 if (!entry)
150 /* First insertion in the hash. */
151 entry = xmalloc (sizeof *entry);
152 entry->key = key;
153 if (!hash_insert (muscle_table, entry))
154 xalloc_die ();
155 entry->value = entry->storage = xstrdup (val);
157 else
159 /* Grow the current value. */
160 char *new_val;
161 obstack_sgrow (&muscle_obstack, entry->value);
162 free (entry->storage);
163 obstack_sgrow (&muscle_obstack, separator);
164 obstack_sgrow (&muscle_obstack, val);
165 obstack_1grow (&muscle_obstack, 0);
166 new_val = obstack_finish (&muscle_obstack);
167 entry->value = entry->storage = xstrdup (new_val);
168 obstack_free (&muscle_obstack, new_val);
172 /*------------------------------------------------------------------.
173 | Using muscle_grow, append a synchronization line for the location |
174 | LOC to the current value of KEY. |
175 `------------------------------------------------------------------*/
177 static void
178 muscle_syncline_grow (char const *key, location loc)
180 char *extension = NULL;
181 obstack_fgrow1 (&muscle_obstack, "]b4_syncline(%d, [[", loc.start.line);
182 MUSCLE_OBSTACK_SGROW (&muscle_obstack,
183 quotearg_style (c_quoting_style, loc.start.file));
184 obstack_sgrow (&muscle_obstack, "]])[");
185 obstack_1grow (&muscle_obstack, 0);
186 extension = obstack_finish (&muscle_obstack);
187 muscle_grow (key, extension, "");
188 obstack_free (&muscle_obstack, extension);
191 /*------------------------------------------------------------------.
192 | Append VALUE to the current value of KEY, using muscle_grow. But |
193 | in addition, issue a synchronization line for the location LOC |
194 | using muscle_syncline_grow. |
195 `------------------------------------------------------------------*/
197 void
198 muscle_code_grow (const char *key, const char *val, location loc)
200 muscle_syncline_grow (key, loc);
201 muscle_grow (key, val, "\n");
205 void muscle_pair_list_grow (const char *muscle,
206 const char *a1, const char *a2)
208 char *pair;
209 obstack_sgrow (&muscle_obstack, "[[[");
210 MUSCLE_OBSTACK_SGROW (&muscle_obstack, a1);
211 obstack_sgrow (&muscle_obstack, "]], [[");
212 MUSCLE_OBSTACK_SGROW (&muscle_obstack, a2);
213 obstack_sgrow (&muscle_obstack, "]]]");
214 obstack_1grow (&muscle_obstack, 0);
215 pair = obstack_finish (&muscle_obstack);
216 muscle_grow (muscle, pair, ",\n");
217 obstack_free (&muscle_obstack, pair);
221 /*----------------------------------------------------------------------------.
222 | Find the value of muscle KEY. Unlike MUSCLE_FIND, this is always reliable |
223 | to determine whether KEY has a value. |
224 `----------------------------------------------------------------------------*/
226 char const *
227 muscle_find_const (char const *key)
229 muscle_entry probe;
230 muscle_entry *result = NULL;
232 probe.key = key;
233 result = hash_lookup (muscle_table, &probe);
234 if (result)
235 return result->value;
236 return NULL;
240 /*----------------------------------------------------------------------------.
241 | Find the value of muscle KEY. Abort if muscle_insert was invoked more |
242 | recently than muscle_grow for KEY since muscle_find can't return a |
243 | char const *. |
244 `----------------------------------------------------------------------------*/
246 char *
247 muscle_find (char const *key)
249 muscle_entry probe;
250 muscle_entry *result = NULL;
252 probe.key = key;
253 result = hash_lookup (muscle_table, &probe);
254 if (result)
256 aver (result->value == result->storage);
257 return result->storage;
259 return NULL;
263 void
264 muscle_boundary_grow (char const *key, boundary bound)
266 char *extension;
267 MUSCLE_OBSTACK_SGROW (&muscle_obstack, bound.file);
268 obstack_1grow (&muscle_obstack, ':');
269 obstack_fgrow1 (&muscle_obstack, "%d", bound.line);
270 obstack_1grow (&muscle_obstack, '.');
271 obstack_fgrow1 (&muscle_obstack, "%d", bound.column);
272 obstack_1grow (&muscle_obstack, '\0');
273 extension = obstack_finish (&muscle_obstack);
274 muscle_grow (key, extension, "");
275 obstack_free (&muscle_obstack, extension);
278 void
279 muscle_location_grow (char const *key, location loc)
281 muscle_grow (key, "[[", "");
282 muscle_boundary_grow (key, loc.start);
283 muscle_grow (key, "]], [[", "");
284 muscle_boundary_grow (key, loc.end);
285 muscle_grow (key, "]]", "");
288 #define MUSCLE_COMMON_DECODE(Value) \
289 case '$': \
290 aver (*++(Value) == ']'); \
291 aver (*++(Value) == '['); \
292 obstack_sgrow (&muscle_obstack, "$"); \
293 break; \
294 case '@': \
295 switch (*++(Value)) \
297 case '@': obstack_sgrow (&muscle_obstack, "@" ); break; \
298 case '{': obstack_sgrow (&muscle_obstack, "[" ); break; \
299 case '}': obstack_sgrow (&muscle_obstack, "]" ); break; \
300 default: aver (false); break; \
302 break; \
303 default: \
304 obstack_1grow (&muscle_obstack, *(Value)); \
305 break;
307 /* Reverse of MUSCLE_OBSTACK_SGROW. */
308 static char *
309 muscle_string_decode (char const *key)
311 char const *value;
312 char *value_decoded;
313 char *result;
315 value = muscle_find_const (key);
316 if (!value)
317 return NULL;
318 do {
319 switch (*value)
321 MUSCLE_COMMON_DECODE (value)
322 case '[':
323 case ']':
324 aver (false);
325 break;
327 } while (*value++);
328 value_decoded = obstack_finish (&muscle_obstack);
329 result = xstrdup (value_decoded);
330 obstack_free (&muscle_obstack, value_decoded);
331 return result;
334 /* Reverse of muscle_location_grow. */
335 static location
336 muscle_location_decode (char const *key)
338 location loc;
339 char const *value = muscle_find_const (key);
340 aver (value);
341 aver (*value == '[');
342 aver (*++value == '[');
343 while (*++value)
344 switch (*value)
346 MUSCLE_COMMON_DECODE (value)
347 case '[':
348 aver (false);
349 break;
350 case ']':
352 char *boundary_str;
353 aver (*++value == ']');
354 obstack_1grow (&muscle_obstack, '\0');
355 boundary_str = obstack_finish (&muscle_obstack);
356 switch (*++value)
358 case ',':
359 boundary_set_from_string (&loc.start, boundary_str);
360 obstack_free (&muscle_obstack, boundary_str);
361 aver (*++value == ' ');
362 aver (*++value == '[');
363 aver (*++value == '[');
364 break;
365 case '\0':
366 boundary_set_from_string (&loc.end, boundary_str);
367 obstack_free (&muscle_obstack, boundary_str);
368 return loc;
369 break;
370 default:
371 aver (false);
372 break;
375 break;
377 aver (false);
378 return loc;
381 void
382 muscle_user_name_list_grow (char const *key, char const *user_name,
383 location loc)
385 muscle_grow (key, "[[[[", ",");
386 muscle_grow (key, user_name, "");
387 muscle_grow (key, "]], ", "");
388 muscle_location_grow (key, loc);
389 muscle_grow (key, "]]", "");
392 void
393 muscle_percent_define_insert (char const *variable, location variable_loc,
394 char const *value,
395 muscle_percent_define_how how)
397 char *variable_tr = NULL;
398 char const *name;
399 char const *loc_name;
400 char const *syncline_name;
401 char const *how_name;
403 /* Permit certain names with underscores for backward compatibility. */
404 if (0 == strcmp (variable, "api.push_pull")
405 || 0 == strcmp (variable, "lr.keep_unreachable_states"))
407 variable_tr = strdup (variable);
408 tr (variable_tr, '_', '-');
409 variable = variable_tr;
412 name = UNIQSTR_CONCAT ("percent_define(", variable, ")");
413 loc_name = UNIQSTR_CONCAT ("percent_define_loc(", variable, ")");
414 syncline_name =
415 UNIQSTR_CONCAT ("percent_define_syncline(", variable, ")");
416 how_name = UNIQSTR_CONCAT ("percent_define_how(", variable, ")");
418 /* Command-line options are processed before the grammar file. */
419 if (how == MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE
420 && muscle_find_const (name))
422 muscle_percent_define_how how_old =
423 atoi (muscle_find_const (how_name));
424 if (how_old == MUSCLE_PERCENT_DEFINE_F)
426 free (variable_tr);
427 return;
429 complain_at (variable_loc, _("%%define variable `%s' redefined"),
430 variable);
431 complain_at (muscle_percent_define_get_loc (variable),
432 _("previous definition"));
435 MUSCLE_INSERT_STRING (name, value);
436 muscle_insert (loc_name, "");
437 muscle_location_grow (loc_name, variable_loc);
438 muscle_insert (syncline_name, "");
439 muscle_syncline_grow (syncline_name, variable_loc);
440 muscle_user_name_list_grow ("percent_define_user_variables", variable,
441 variable_loc);
442 MUSCLE_INSERT_INT (how_name, how);
444 free (variable_tr);
447 char *
448 muscle_percent_define_get (char const *variable)
450 char const *name;
451 char const *usage_name;
452 char *value;
454 name = UNIQSTR_CONCAT ("percent_define(", variable, ")");
455 usage_name = UNIQSTR_CONCAT ("percent_define_bison_variables(",
456 variable, ")");
458 muscle_insert (usage_name, "");
459 value = muscle_string_decode (name);
460 if (!value)
461 value = xstrdup ("");
462 return value;
465 location
466 muscle_percent_define_get_loc (char const *variable)
468 char const *loc_name;
469 loc_name = UNIQSTR_CONCAT ("percent_define_loc(", variable, ")");
470 if (!muscle_find_const (loc_name))
471 fatal(_("undefined %%define variable `%s' passed to"
472 " muscle_percent_define_get_loc"), variable);
473 return muscle_location_decode (loc_name);
476 char const *
477 muscle_percent_define_get_syncline (char const *variable)
479 char const *syncline_name;
480 char const *syncline;
481 syncline_name =
482 UNIQSTR_CONCAT ("percent_define_syncline(", variable, ")");
483 syncline = muscle_find_const (syncline_name);
484 if (!syncline)
485 fatal(_("undefined %%define variable `%s' passed to"
486 " muscle_percent_define_get_syncline"), variable);
487 return syncline;
490 bool
491 muscle_percent_define_ifdef (char const *variable)
493 char const *name;
494 char const *usage_name;
495 char const *value;
497 name = UNIQSTR_CONCAT ("percent_define(", variable, ")");
498 usage_name =
499 UNIQSTR_CONCAT ("percent_define_bison_variables(", variable, ")");
501 value = muscle_find_const (name);
502 if (value)
504 muscle_insert (usage_name, "");
505 return true;
508 return false;
511 bool
512 muscle_percent_define_flag_if (char const *variable)
514 char const *invalid_boolean_name;
515 bool result = false;
517 invalid_boolean_name =
518 UNIQSTR_CONCAT ("percent_define_invalid_boolean(", variable, ")");
520 if (muscle_percent_define_ifdef (variable))
522 char *value = muscle_percent_define_get (variable);
523 if (value[0] == '\0' || 0 == strcmp (value, "true"))
524 result = true;
525 else if (0 == strcmp (value, "false"))
526 result = false;
527 else if (!muscle_find_const (invalid_boolean_name))
529 muscle_insert (invalid_boolean_name, "");
530 complain_at(muscle_percent_define_get_loc (variable),
531 _("invalid value for %%define Boolean variable `%s'"),
532 variable);
534 free (value);
536 else
537 fatal(_("undefined %%define variable `%s' passed to muscle_percent_define_flag_if"),
538 variable);
540 return result;
543 void
544 muscle_percent_define_default (char const *variable, char const *value)
546 char const *name;
547 char const *loc_name;
548 char const *syncline_name;
549 name = UNIQSTR_CONCAT ("percent_define(", variable, ")");
550 loc_name = UNIQSTR_CONCAT ("percent_define_loc(", variable, ")");
551 syncline_name =
552 UNIQSTR_CONCAT ("percent_define_syncline(", variable, ")");
553 if (!muscle_find_const (name))
555 location loc;
556 MUSCLE_INSERT_STRING (name, value);
557 loc.start.file = loc.end.file = "<default value>";
558 loc.start.line = loc.end.line = -1;
559 loc.start.column = loc.end.column = -1;
560 muscle_insert (loc_name, "");
561 muscle_location_grow (loc_name, loc);
562 muscle_insert (syncline_name, "");
566 void
567 muscle_percent_define_check_values (char const * const *values)
569 for (; *values; ++values)
571 char const * const *variablep = values;
572 char const *name;
573 char *value;
575 name = UNIQSTR_CONCAT ("percent_define(", *variablep, ")");
577 value = muscle_string_decode (name);
578 if (value)
580 for (++values; *values; ++values)
582 if (0 == strcmp (value, *values))
583 break;
585 if (!*values)
587 location loc = muscle_percent_define_get_loc (*variablep);
588 complain_at(loc,
589 _("invalid value for %%define variable `%s': `%s'"),
590 *variablep, value);
591 for (values = variablep + 1; *values; ++values)
592 complain_at (loc, _("accepted value: `%s'"), *values);
594 else
596 while (*values)
597 ++values;
599 free (value);
601 else
602 fatal(_("undefined %%define variable `%s' passed to"
603 " muscle_percent_define_check_values"),
604 *variablep);
608 void
609 muscle_percent_code_grow (char const *qualifier, location qualifier_loc,
610 char const *code, location code_loc)
612 char const *name;
613 name = UNIQSTR_CONCAT ("percent_code(", qualifier, ")");
614 muscle_code_grow (name, code, code_loc);
615 muscle_user_name_list_grow ("percent_code_user_qualifiers", qualifier,
616 qualifier_loc);
620 /*------------------------------------------------.
621 | Output the definition of ENTRY as a m4_define. |
622 `------------------------------------------------*/
624 static inline bool
625 muscle_m4_output (muscle_entry *entry, FILE *out)
627 fprintf (out, "m4_define([b4_%s],\n", entry->key);
628 fprintf (out, "[[%s]])\n\n\n", entry->value);
629 return true;
632 static bool
633 muscle_m4_output_processor (void *entry, void *out)
635 return muscle_m4_output (entry, out);
639 /*----------------------------------------------------------------.
640 | Output the definition of all the current muscles into a list of |
641 | m4_defines. |
642 `----------------------------------------------------------------*/
644 void
645 muscles_m4_output (FILE *out)
647 hash_do_for_each (muscle_table, muscle_m4_output_processor, out);