1 /* Muscle table manager for Bison.
3 Copyright (C) 2001-2013 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 #include "muscle-tab.h"
31 /* A key-value pair, along with storage that can be reclaimed when
32 this pair is no longer needed. */
40 /* An obstack used to create some entries. */
41 struct obstack muscle_obstack
;
43 /* Initial capacity of muscles hash table. */
44 #define HT_INITIAL_CAPACITY 257
46 static struct hash_table
*muscle_table
= NULL
;
49 hash_compare_muscles (void const *x
, void const *y
)
51 muscle_entry
const *m1
= x
;
52 muscle_entry
const *m2
= y
;
53 return STREQ (m1
->key
, m2
->key
);
57 hash_muscle (const void *x
, size_t tablesize
)
59 muscle_entry
const *m
= x
;
60 return hash_string (m
->key
, tablesize
);
64 muscle_entry_free (void *entry
)
66 muscle_entry
*mentry
= entry
;
67 free (mentry
->storage
);
74 /* Initialize the muscle obstack. */
75 obstack_init (&muscle_obstack
);
77 muscle_table
= hash_initialize (HT_INITIAL_CAPACITY
, NULL
, hash_muscle
,
78 hash_compare_muscles
, muscle_entry_free
);
80 /* Version and input file. */
81 MUSCLE_INSERT_STRING ("version", VERSION
);
88 hash_free (muscle_table
);
89 obstack_free (&muscle_obstack
, NULL
);
94 muscle_insert (char const *key
, char const *value
)
100 entry
= hash_lookup (muscle_table
, &probe
);
104 /* First insertion in the hash. */
105 entry
= xmalloc (sizeof *entry
);
107 if (!hash_insert (muscle_table
, entry
))
111 free (entry
->storage
);
112 entry
->value
= value
;
113 entry
->storage
= NULL
;
117 /*-------------------------------------------------------------------.
118 | Append VALUE to the current value of KEY. If KEY did not already |
119 | exist, create it. Use MUSCLE_OBSTACK. De-allocate the previously |
120 | associated value. Copy VALUE and SEPARATOR. |
121 `-------------------------------------------------------------------*/
124 muscle_grow (const char *key
, const char *val
, const char *separator
)
127 muscle_entry
*entry
= NULL
;
130 entry
= hash_lookup (muscle_table
, &probe
);
134 /* First insertion in the hash. */
135 entry
= xmalloc (sizeof *entry
);
137 if (!hash_insert (muscle_table
, entry
))
139 entry
->value
= entry
->storage
= xstrdup (val
);
143 /* Grow the current value. */
145 obstack_printf (&muscle_obstack
, "%s%s%s", entry
->value
, separator
, val
);
146 free (entry
->storage
);
147 new_val
= obstack_finish0 (&muscle_obstack
);
148 entry
->value
= entry
->storage
= xstrdup (new_val
);
149 obstack_free (&muscle_obstack
, new_val
);
153 /*------------------------------------------------------------------.
154 | Using muscle_grow, append a synchronization line for the location |
155 | LOC to the current value of KEY. |
156 `------------------------------------------------------------------*/
159 muscle_syncline_grow (char const *key
, location loc
)
161 char *extension
= NULL
;
162 obstack_printf (&muscle_obstack
, "]b4_syncline(%d, ", loc
.start
.line
);
163 obstack_quote (&muscle_obstack
,
164 quotearg_style (c_quoting_style
, loc
.start
.file
));
165 obstack_sgrow (&muscle_obstack
, ")[");
166 extension
= obstack_finish0 (&muscle_obstack
);
167 muscle_grow (key
, extension
, "");
168 obstack_free (&muscle_obstack
, extension
);
171 /*------------------------------------------------------------------.
172 | Append VALUE to the current value of KEY, using muscle_grow. But |
173 | in addition, issue a synchronization line for the location LOC |
174 | using muscle_syncline_grow. |
175 `------------------------------------------------------------------*/
178 muscle_code_grow (const char *key
, const char *val
, location loc
)
180 muscle_syncline_grow (key
, loc
);
181 muscle_grow (key
, val
, "\n");
185 void muscle_pair_list_grow (const char *muscle
,
186 const char *a1
, const char *a2
)
189 obstack_sgrow (&muscle_obstack
, "[");
190 obstack_quote (&muscle_obstack
, a1
);
191 obstack_sgrow (&muscle_obstack
, ", ");
192 obstack_quote (&muscle_obstack
, a2
);
193 obstack_sgrow (&muscle_obstack
, "]");
194 pair
= obstack_finish0 (&muscle_obstack
);
195 muscle_grow (muscle
, pair
, ",\n");
196 obstack_free (&muscle_obstack
, pair
);
201 muscle_find_const (char const *key
)
204 muscle_entry
*result
= NULL
;
207 result
= hash_lookup (muscle_table
, &probe
);
209 return result
->value
;
215 muscle_find (char const *key
)
218 muscle_entry
*result
= NULL
;
221 result
= hash_lookup (muscle_table
, &probe
);
224 aver (result
->value
== result
->storage
);
225 return result
->storage
;
231 /* In the format 'file_name:line.column', append BOUND to MUSCLE. Use
232 digraphs for special characters in the file name. */
235 muscle_boundary_grow (char const *key
, boundary bound
)
238 obstack_sgrow (&muscle_obstack
, "[[");
239 obstack_escape (&muscle_obstack
, bound
.file
);
240 obstack_printf (&muscle_obstack
, ":%d.%d]]", bound
.line
, bound
.column
);
241 extension
= obstack_finish0 (&muscle_obstack
);
242 muscle_grow (key
, extension
, "");
243 obstack_free (&muscle_obstack
, extension
);
247 /* In the format '[[file_name:line.column]], [[file_name:line.column]]',
248 append LOC to MUSCLE. Use digraphs for special characters in each
252 muscle_location_grow (char const *key
, location loc
)
254 muscle_boundary_grow (key
, loc
.start
);
255 muscle_grow (key
, "", ", ");
256 muscle_boundary_grow (key
, loc
.end
);
259 #define COMMON_DECODE(Value) \
261 aver (*++(Value) == ']'); \
262 aver (*++(Value) == '['); \
263 obstack_sgrow (&muscle_obstack, "$"); \
266 switch (*++(Value)) \
268 case '@': obstack_sgrow (&muscle_obstack, "@" ); break; \
269 case '{': obstack_sgrow (&muscle_obstack, "[" ); break; \
270 case '}': obstack_sgrow (&muscle_obstack, "]" ); break; \
271 default: aver (false); break; \
275 obstack_1grow (&muscle_obstack, *(Value)); \
278 /* Reverse of obstack_escape. */
280 string_decode (char const *key
)
282 char const *value
= muscle_find_const (key
);
291 COMMON_DECODE (value
)
298 value_decoded
= obstack_finish (&muscle_obstack
);
299 result
= xstrdup (value_decoded
);
300 obstack_free (&muscle_obstack
, value_decoded
);
304 /* Reverse of muscle_location_grow. */
306 location_decode (char const *key
)
309 char const *value
= muscle_find_const (key
);
311 aver (*value
== '[');
312 aver (*++value
== '[');
316 COMMON_DECODE (value
)
323 aver (*++value
== ']');
324 boundary_str
= obstack_finish0 (&muscle_obstack
);
328 boundary_set_from_string (&loc
.start
, boundary_str
);
329 obstack_free (&muscle_obstack
, boundary_str
);
330 aver (*++value
== ' ');
331 aver (*++value
== '[');
332 aver (*++value
== '[');
335 boundary_set_from_string (&loc
.end
, boundary_str
);
336 obstack_free (&muscle_obstack
, boundary_str
);
351 muscle_user_name_list_grow (char const *key
, char const *user_name
,
354 muscle_grow (key
, "[[[[", ",");
355 muscle_grow (key
, user_name
, "");
356 muscle_grow (key
, "]], ", "");
357 muscle_location_grow (key
, loc
);
358 muscle_grow (key
, "]]", "");
362 /** Return an allocated string that represents the %define directive
363 that performs the assignment.
365 @param assignment "VAR", or "VAR=VAL".
366 @param value default value if VAL \a assignment has no '='.
369 "foo", NULL => "%define foo"
370 "foo", "baz" => "%define foo baz"
371 "foo=bar", NULL => "%define foo bar"
372 "foo=bar", "baz" => "%define foo bar"
373 "foo=", NULL => "%define foo"
374 "foo=", "baz" => "%define foo"
379 define_directive (char const *assignment
, char const *value
)
381 char *eq
= strchr (assignment
, '=');
382 char const *fmt
= !eq
&& value
&& *value
? "%%define %s %s" : "%%define %s";
383 char *res
= xmalloc (strlen (fmt
) + strlen (assignment
)
384 + (value
? strlen (value
) : 0));
385 sprintf (res
, fmt
, assignment
, value
);
386 eq
= strchr (res
, '=');
388 *eq
= eq
[1] ? ' ' : '\0';
392 /** If the \a variable name is obsolete, return the name to use,
393 * otherwise \a variable. If the \a value is obsolete, update it too.
395 * Allocates the returned value. */
398 muscle_percent_variable_update (char const *variable
, location variable_loc
,
403 const char *obsolete
;
406 const conversion_type conversion
[] =
408 { "api.push_pull", "api.push-pull", },
409 { "api.tokens.prefix", "api.token.prefix", },
410 { "lex_symbol", "api.token.constructor", },
411 { "location_type", "api.location.type", },
412 { "lr.default-reductions", "lr.default-reduction", },
413 { "lr.keep-unreachable-states", "lr.keep-unreachable-state", },
414 { "lr.keep_unreachable_states", "lr.keep-unreachable-state", },
415 { "namespace", "api.namespace", },
416 { "stype", "api.value.type", },
417 { "variant=", "api.value.type=variant", },
418 { "variant=true", "api.value.type=variant", },
421 conversion_type
const *c
;
422 for (c
= conversion
; c
->obsolete
; ++c
)
424 char const *eq
= strchr (c
->obsolete
, '=');
426 ? (!strncmp (c
->obsolete
, variable
, eq
- c
->obsolete
)
427 && STREQ (eq
+ 1, *value
))
428 : STREQ (c
->obsolete
, variable
))
430 char *old
= define_directive (c
->obsolete
, *value
);
431 char *upd
= define_directive (c
->updated
, *value
);
432 deprecated_directive (&variable_loc
, old
, upd
);
435 char *res
= xstrdup (c
->updated
);
437 char *eq2
= strchr (res
, '=');
447 return xstrdup (variable
);
451 muscle_percent_define_insert (char const *var
, location variable_loc
,
453 muscle_percent_define_how how
)
455 /* Backward compatibility. */
456 char *variable
= muscle_percent_variable_update (var
, variable_loc
, &value
);
457 char const *name
= UNIQSTR_CONCAT ("percent_define(", variable
, ")");
458 char const *loc_name
= UNIQSTR_CONCAT ("percent_define_loc(", variable
, ")");
459 char const *syncline_name
=
460 UNIQSTR_CONCAT ("percent_define_syncline(", variable
, ")");
461 char const *how_name
= UNIQSTR_CONCAT ("percent_define_how(", variable
, ")");
463 /* Command-line options are processed before the grammar file. */
464 if (how
== MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE
465 && muscle_find_const (name
))
467 muscle_percent_define_how how_old
= atoi (muscle_find_const (how_name
));
469 if (how_old
== MUSCLE_PERCENT_DEFINE_F
)
471 complain_indent (&variable_loc
, complaint
, &i
,
472 _("%%define variable %s redefined"),
475 location loc
= muscle_percent_define_get_loc (variable
);
476 complain_indent (&loc
, complaint
, &i
, _("previous definition"));
479 MUSCLE_INSERT_STRING (name
, value
);
480 muscle_insert (loc_name
, "");
481 muscle_location_grow (loc_name
, variable_loc
);
482 muscle_insert (syncline_name
, "");
483 muscle_syncline_grow (syncline_name
, variable_loc
);
484 muscle_user_name_list_grow ("percent_define_user_variables", variable
,
486 MUSCLE_INSERT_INT (how_name
, how
);
491 /* This is used for backward compatibility, e.g., "%define api.pure"
492 supersedes "%pure-parser". */
494 muscle_percent_define_ensure (char const *variable
, location loc
,
497 char const *val
= value
? "" : "false";
498 char const *name
= UNIQSTR_CONCAT ("percent_define(", variable
, ")");
500 /* %pure-parser is deprecated in favor of '%define api.pure', so use
501 '%define api.pure' in a backward-compatible manner here. First,
502 don't complain if %pure-parser is specified multiple times. */
503 if (!muscle_find_const (name
))
504 muscle_percent_define_insert (variable
, loc
, val
,
505 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE
);
506 /* In all cases, use api.pure now so that the backend doesn't complain if
507 the skeleton ignores api.pure, but do warn now if there's a previous
508 conflicting definition from an actual %define. */
509 if (muscle_percent_define_flag_if (variable
) != value
)
510 muscle_percent_define_insert (variable
, loc
, val
,
511 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE
);
515 muscle_percent_define_get (char const *variable
)
517 char const *name
= UNIQSTR_CONCAT ("percent_define(", variable
, ")");
518 char const *usage_name
=
519 UNIQSTR_CONCAT ("percent_define_bison_variables(", variable
, ")");
520 char *value
= string_decode (name
);
522 value
= xstrdup ("");
524 muscle_insert (usage_name
, "");
529 muscle_percent_define_get_loc (char const *variable
)
531 char const *loc_name
= UNIQSTR_CONCAT ("percent_define_loc(", variable
, ")");
532 if (!muscle_find_const (loc_name
))
533 complain (NULL
, fatal
, _("%s: undefined %%define variable %s"),
534 "muscle_percent_define_get_loc", quote (variable
));
535 return location_decode (loc_name
);
539 muscle_percent_define_get_syncline (char const *variable
)
541 char const *syncline_name
=
542 UNIQSTR_CONCAT ("percent_define_syncline(", variable
, ")");
543 char const *syncline
= muscle_find_const (syncline_name
);
545 complain (NULL
, fatal
, _("%s: undefined %%define variable %s"),
546 "muscle_percent_define_get_syncline", quote (variable
));
551 muscle_percent_define_ifdef (char const *variable
)
553 char const *name
= UNIQSTR_CONCAT ("percent_define(", variable
, ")");
554 char const *usage_name
=
555 UNIQSTR_CONCAT ("percent_define_bison_variables(", variable
, ")");
556 char const *value
= muscle_find_const (name
);
559 muscle_insert (usage_name
, "");
567 muscle_percent_define_flag_if (char const *variable
)
569 char const *invalid_boolean_name
=
570 UNIQSTR_CONCAT ("percent_define_invalid_boolean(", variable
, ")");
573 if (muscle_percent_define_ifdef (variable
))
575 char *value
= muscle_percent_define_get (variable
);
576 if (value
[0] == '\0' || STREQ (value
, "true"))
578 else if (STREQ (value
, "false"))
580 else if (!muscle_find_const (invalid_boolean_name
))
582 muscle_insert (invalid_boolean_name
, "");
583 location loc
= muscle_percent_define_get_loc (variable
);
584 complain (&loc
, complaint
,
585 _("invalid value for %%define Boolean variable %s"),
591 complain (NULL
, fatal
, _("%s: undefined %%define variable %s"),
592 "muscle_percent_define_flag", quote (variable
));
598 muscle_percent_define_default (char const *variable
, char const *value
)
600 char const *name
= UNIQSTR_CONCAT ("percent_define(", variable
, ")");
601 char const *loc_name
= UNIQSTR_CONCAT ("percent_define_loc(", variable
, ")");
602 char const *syncline_name
=
603 UNIQSTR_CONCAT ("percent_define_syncline(", variable
, ")");
604 if (!muscle_find_const (name
))
607 MUSCLE_INSERT_STRING (name
, value
);
608 loc
.start
.file
= loc
.end
.file
= "<default value>";
609 loc
.start
.line
= loc
.end
.line
= -1;
610 loc
.start
.column
= loc
.end
.column
= -1;
611 muscle_insert (loc_name
, "");
612 muscle_location_grow (loc_name
, loc
);
613 muscle_insert (syncline_name
, "");
618 muscle_percent_define_check_values (char const * const *values
)
620 for (; *values
; ++values
)
622 char const * const *variablep
= values
;
623 char const *name
= UNIQSTR_CONCAT ("percent_define(", *variablep
, ")");
624 char *value
= string_decode (name
);
627 for (++values
; *values
; ++values
)
629 if (STREQ (value
, *values
))
635 location loc
= muscle_percent_define_get_loc (*variablep
);
636 complain_indent (&loc
, complaint
, &i
,
637 _("invalid value for %%define variable %s: %s"),
638 quote (*variablep
), quote_n (1, value
));
640 for (values
= variablep
+ 1; *values
; ++values
)
641 complain_indent (&loc
, complaint
| no_caret
| silent
, &i
,
642 _("accepted value: %s"), quote (*values
));
652 complain (NULL
, fatal
, _("%s: undefined %%define variable %s"),
653 "muscle_percent_define_check_values", quote (*variablep
));
658 muscle_percent_code_grow (char const *qualifier
, location qualifier_loc
,
659 char const *code
, location code_loc
)
661 char const *name
= UNIQSTR_CONCAT ("percent_code(", qualifier
, ")");
662 muscle_code_grow (name
, code
, code_loc
);
663 muscle_user_name_list_grow ("percent_code_user_qualifiers", qualifier
,
668 /*------------------------------------------------.
669 | Output the definition of ENTRY as a m4_define. |
670 `------------------------------------------------*/
673 muscle_m4_output (muscle_entry
*entry
, FILE *out
)
676 "m4_define([b4_%s],\n"
677 "[[%s]])\n\n\n", entry
->key
, entry
->value
);
682 muscle_m4_output_processor (void *entry
, void *out
)
684 return muscle_m4_output (entry
, out
);
689 muscles_m4_output (FILE *out
)
691 hash_do_for_each (muscle_table
, muscle_m4_output_processor
, out
);