1 /* Muscle table manager for Bison.
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 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/>. */
29 #include "muscle-tab.h"
32 /* A key-value pair, along with storage that can be reclaimed when
33 this pair is no longer needed. */
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
;
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;
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 `-----------------------------------------------------------------*/
70 muscle_entry_free (void *entry
)
72 muscle_entry
*mentry
= entry
;
73 free (mentry
->storage
);
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 `------------------------------------------------------------*/
98 hash_free (muscle_table
);
99 obstack_free (&muscle_obstack
, NULL
);
104 /*------------------------------------------------------------.
105 | Insert (KEY, VALUE). If KEY already existed, overwrite the |
107 `------------------------------------------------------------*/
110 muscle_insert (char const *key
, char const *value
)
116 entry
= hash_lookup (muscle_table
, &probe
);
120 /* First insertion in the hash. */
121 entry
= xmalloc (sizeof *entry
);
123 if (!hash_insert (muscle_table
, entry
))
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 `-------------------------------------------------------------------*/
140 muscle_grow (const char *key
, const char *val
, const char *separator
)
143 muscle_entry
*entry
= NULL
;
146 entry
= hash_lookup (muscle_table
, &probe
);
150 /* First insertion in the hash. */
151 entry
= xmalloc (sizeof *entry
);
153 if (!hash_insert (muscle_table
, entry
))
155 entry
->value
= entry
->storage
= xstrdup (val
);
159 /* Grow the current value. */
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 `------------------------------------------------------------------*/
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 `------------------------------------------------------------------*/
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
)
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 `----------------------------------------------------------------------------*/
227 muscle_find_const (char const *key
)
230 muscle_entry
*result
= NULL
;
233 result
= hash_lookup (muscle_table
, &probe
);
235 return result
->value
;
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 |
244 `----------------------------------------------------------------------------*/
247 muscle_find (char const *key
)
250 muscle_entry
*result
= NULL
;
253 result
= hash_lookup (muscle_table
, &probe
);
256 aver (result
->value
== result
->storage
);
257 return result
->storage
;
264 muscle_boundary_grow (char const *key
, boundary bound
)
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
);
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) \
290 aver (*++(Value) == ']'); \
291 aver (*++(Value) == '['); \
292 obstack_sgrow (&muscle_obstack, "$"); \
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; \
304 obstack_1grow (&muscle_obstack, *(Value)); \
307 /* Reverse of MUSCLE_OBSTACK_SGROW. */
309 muscle_string_decode (char const *key
)
315 value
= muscle_find_const (key
);
321 MUSCLE_COMMON_DECODE (value
)
328 value_decoded
= obstack_finish (&muscle_obstack
);
329 result
= xstrdup (value_decoded
);
330 obstack_free (&muscle_obstack
, value_decoded
);
334 /* Reverse of muscle_location_grow. */
336 muscle_location_decode (char const *key
)
339 char const *value
= muscle_find_const (key
);
341 aver (*value
== '[');
342 aver (*++value
== '[');
346 MUSCLE_COMMON_DECODE (value
)
353 aver (*++value
== ']');
354 obstack_1grow (&muscle_obstack
, '\0');
355 boundary_str
= obstack_finish (&muscle_obstack
);
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
== '[');
366 boundary_set_from_string (&loc
.end
, boundary_str
);
367 obstack_free (&muscle_obstack
, boundary_str
);
382 muscle_user_name_list_grow (char const *key
, char const *user_name
,
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 /** If the \a variable name is obsolete, return the name to use,
393 * otherwise \a variable. */
396 muscle_percent_variable_update (char const *variable
)
400 const char *obsolete
;
403 const conversion_type conversion
[] =
405 { "api.push_pull", "api.push-pull", },
406 { "lr.keep_unreachable_states", "lr.keep-unreachable-states", },
407 { "namespace", "api.namespace", },
410 for (i
= 0; i
< sizeof conversion
/ sizeof *conversion
; ++i
)
411 if (!strcmp (conversion
[i
].obsolete
, variable
))
412 return conversion
[i
].updated
;
417 muscle_percent_define_insert (char const *variable
, location variable_loc
,
419 muscle_percent_define_how how
)
422 char const *loc_name
;
423 char const *syncline_name
;
424 char const *how_name
;
426 /* Permit certain names with underscores for backward compatibility. */
427 variable
= muscle_percent_variable_update (variable
);
429 name
= UNIQSTR_CONCAT ("percent_define(", variable
, ")");
430 loc_name
= UNIQSTR_CONCAT ("percent_define_loc(", variable
, ")");
432 UNIQSTR_CONCAT ("percent_define_syncline(", variable
, ")");
433 how_name
= UNIQSTR_CONCAT ("percent_define_how(", variable
, ")");
435 /* Command-line options are processed before the grammar file. */
436 if (how
== MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE
437 && muscle_find_const (name
))
439 muscle_percent_define_how how_old
=
440 atoi (muscle_find_const (how_name
));
441 if (how_old
== MUSCLE_PERCENT_DEFINE_F
)
443 complain_at (variable_loc
, _("%s `%s' redefined"),
444 "%define variable", variable
);
445 complain_at (muscle_percent_define_get_loc (variable
),
446 _("previous definition"));
449 MUSCLE_INSERT_STRING (name
, value
);
450 muscle_insert (loc_name
, "");
451 muscle_location_grow (loc_name
, variable_loc
);
452 muscle_insert (syncline_name
, "");
453 muscle_syncline_grow (syncline_name
, variable_loc
);
454 muscle_user_name_list_grow ("percent_define_user_variables", variable
,
456 MUSCLE_INSERT_INT (how_name
, how
);
459 /* This is used for backward compatibility, e.g., "%define api.pure"
460 supersedes "%pure-parser". */
462 muscle_percent_define_ensure (char const *variable
, location loc
,
465 char const *val
= value
? "" : "false";
467 name
= UNIQSTR_CONCAT ("percent_define(", variable
, ")");
469 /* %pure-parser is deprecated in favor of `%define api.pure', so use
470 `%define api.pure' in a backward-compatible manner here. First,
471 don't complain if %pure-parser is specified multiple times. */
472 if (!muscle_find_const (name
))
473 muscle_percent_define_insert (variable
, loc
, val
,
474 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE
);
475 /* In all cases, use api.pure now so that the backend doesn't complain if
476 the skeleton ignores api.pure, but do warn now if there's a previous
477 conflicting definition from an actual %define. */
478 if (muscle_percent_define_flag_if (variable
) != value
)
479 muscle_percent_define_insert (variable
, loc
, val
,
480 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE
);
484 muscle_percent_define_get (char const *variable
)
487 char const *usage_name
;
490 name
= UNIQSTR_CONCAT ("percent_define(", variable
, ")");
491 usage_name
= UNIQSTR_CONCAT ("percent_define_bison_variables(",
494 muscle_insert (usage_name
, "");
495 value
= muscle_string_decode (name
);
497 value
= xstrdup ("");
502 muscle_percent_define_get_loc (char const *variable
)
504 char const *loc_name
;
505 loc_name
= UNIQSTR_CONCAT ("percent_define_loc(", variable
, ")");
506 if (!muscle_find_const (loc_name
))
507 fatal(_("undefined %%define variable `%s' passed to"
508 " muscle_percent_define_get_loc"), variable
);
509 return muscle_location_decode (loc_name
);
513 muscle_percent_define_get_syncline (char const *variable
)
515 char const *syncline_name
;
516 char const *syncline
;
518 UNIQSTR_CONCAT ("percent_define_syncline(", variable
, ")");
519 syncline
= muscle_find_const (syncline_name
);
521 fatal(_("undefined %%define variable `%s' passed to"
522 " muscle_percent_define_get_syncline"), variable
);
527 muscle_percent_define_ifdef (char const *variable
)
530 char const *usage_name
;
533 name
= UNIQSTR_CONCAT ("percent_define(", variable
, ")");
535 UNIQSTR_CONCAT ("percent_define_bison_variables(", variable
, ")");
537 value
= muscle_find_const (name
);
540 muscle_insert (usage_name
, "");
548 muscle_percent_define_flag_if (char const *variable
)
550 char const *invalid_boolean_name
;
553 invalid_boolean_name
=
554 UNIQSTR_CONCAT ("percent_define_invalid_boolean(", variable
, ")");
556 if (muscle_percent_define_ifdef (variable
))
558 char *value
= muscle_percent_define_get (variable
);
559 if (value
[0] == '\0' || 0 == strcmp (value
, "true"))
561 else if (0 == strcmp (value
, "false"))
563 else if (!muscle_find_const (invalid_boolean_name
))
565 muscle_insert (invalid_boolean_name
, "");
566 complain_at(muscle_percent_define_get_loc (variable
),
567 _("invalid value for %%define Boolean variable `%s'"),
573 fatal(_("undefined %%define variable `%s' passed to muscle_percent_define_flag_if"),
580 muscle_percent_define_default (char const *variable
, char const *value
)
583 char const *loc_name
;
584 char const *syncline_name
;
585 name
= UNIQSTR_CONCAT ("percent_define(", variable
, ")");
586 loc_name
= UNIQSTR_CONCAT ("percent_define_loc(", variable
, ")");
588 UNIQSTR_CONCAT ("percent_define_syncline(", variable
, ")");
589 if (!muscle_find_const (name
))
592 MUSCLE_INSERT_STRING (name
, value
);
593 loc
.start
.file
= loc
.end
.file
= "<default value>";
594 loc
.start
.line
= loc
.end
.line
= -1;
595 loc
.start
.column
= loc
.end
.column
= -1;
596 muscle_insert (loc_name
, "");
597 muscle_location_grow (loc_name
, loc
);
598 muscle_insert (syncline_name
, "");
603 muscle_percent_define_check_values (char const * const *values
)
605 for (; *values
; ++values
)
607 char const * const *variablep
= values
;
611 name
= UNIQSTR_CONCAT ("percent_define(", *variablep
, ")");
613 value
= muscle_string_decode (name
);
616 for (++values
; *values
; ++values
)
618 if (0 == strcmp (value
, *values
))
623 location loc
= muscle_percent_define_get_loc (*variablep
);
625 _("invalid value for %%define variable `%s': `%s'"),
627 for (values
= variablep
+ 1; *values
; ++values
)
628 complain_at (loc
, _("accepted value: `%s'"), *values
);
638 fatal(_("undefined %%define variable `%s' passed to"
639 " muscle_percent_define_check_values"),
645 muscle_percent_code_grow (char const *qualifier
, location qualifier_loc
,
646 char const *code
, location code_loc
)
649 name
= UNIQSTR_CONCAT ("percent_code(", qualifier
, ")");
650 muscle_code_grow (name
, code
, code_loc
);
651 muscle_user_name_list_grow ("percent_code_user_qualifiers", qualifier
,
656 /*------------------------------------------------.
657 | Output the definition of ENTRY as a m4_define. |
658 `------------------------------------------------*/
661 muscle_m4_output (muscle_entry
*entry
, FILE *out
)
663 fprintf (out
, "m4_define([b4_%s],\n", entry
->key
);
664 fprintf (out
, "[[%s]])\n\n\n", entry
->value
);
669 muscle_m4_output_processor (void *entry
, void *out
)
671 return muscle_m4_output (entry
, out
);
675 /*----------------------------------------------------------------.
676 | Output the definition of all the current muscles into a list of |
678 `----------------------------------------------------------------*/
681 muscles_m4_output (FILE *out
)
683 hash_do_for_each (muscle_table
, muscle_m4_output_processor
, out
);