1 /* Muscle table manager for Bison.
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
6 This file is part of Bison, the GNU Compiler Compiler.
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)
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.
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
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
31 #include "muscle_tab.h"
34 /* A key-value pair, along with storage that can be reclaimed when
35 this pair is no longer needed. */
43 /* An obstack used to create some entries. */
44 struct obstack muscle_obstack
;
46 /* Initial capacity of muscles hash table. */
47 #define HT_INITIAL_CAPACITY 257
49 static struct hash_table
*muscle_table
= NULL
;
52 hash_compare_muscles (void const *x
, void const *y
)
54 muscle_entry
const *m1
= x
;
55 muscle_entry
const *m2
= y
;
56 return strcmp (m1
->key
, m2
->key
) == 0;
60 hash_muscle (const void *x
, size_t tablesize
)
62 muscle_entry
const *m
= x
;
63 return hash_string (m
->key
, tablesize
);
66 /*-----------------------------------------------------------------.
67 | Create the MUSCLE_TABLE, and initialize it with default values. |
68 | Also set up the MUSCLE_OBSTACK. |
69 `-----------------------------------------------------------------*/
72 muscle_entry_free (void *entry
)
74 muscle_entry
*mentry
= entry
;
75 free (mentry
->storage
);
82 /* Initialize the muscle obstack. */
83 obstack_init (&muscle_obstack
);
85 muscle_table
= hash_initialize (HT_INITIAL_CAPACITY
, NULL
, hash_muscle
,
86 hash_compare_muscles
, muscle_entry_free
);
88 /* Version and input file. */
89 MUSCLE_INSERT_STRING ("version", VERSION
);
90 MUSCLE_INSERT_C_STRING ("file_name", grammar_file
);
94 /*------------------------------------------------------------.
95 | Free all the memory consumed by the muscle machinery only. |
96 `------------------------------------------------------------*/
101 hash_free (muscle_table
);
102 obstack_free (&muscle_obstack
, NULL
);
107 /*------------------------------------------------------------.
108 | Insert (KEY, VALUE). If KEY already existed, overwrite the |
110 `------------------------------------------------------------*/
113 muscle_insert (char const *key
, char const *value
)
119 entry
= hash_lookup (muscle_table
, &probe
);
123 /* First insertion in the hash. */
124 entry
= xmalloc (sizeof *entry
);
126 hash_insert (muscle_table
, entry
);
129 free (entry
->storage
);
130 entry
->value
= value
;
131 entry
->storage
= NULL
;
135 /*-------------------------------------------------------------------.
136 | Append VALUE to the current value of KEY. If KEY did not already |
137 | exist, create it. Use MUSCLE_OBSTACK. De-allocate the previously |
138 | associated value. Copy VALUE and SEPARATOR. |
139 `-------------------------------------------------------------------*/
142 muscle_grow (const char *key
, const char *val
, const char *separator
)
145 muscle_entry
*entry
= NULL
;
148 entry
= hash_lookup (muscle_table
, &probe
);
152 /* First insertion in the hash. */
153 entry
= xmalloc (sizeof *entry
);
155 hash_insert (muscle_table
, entry
);
156 entry
->value
= entry
->storage
= xstrdup (val
);
160 /* Grow the current value. */
162 obstack_sgrow (&muscle_obstack
, entry
->value
);
163 free (entry
->storage
);
164 obstack_sgrow (&muscle_obstack
, separator
);
165 obstack_sgrow (&muscle_obstack
, val
);
166 obstack_1grow (&muscle_obstack
, 0);
167 new_val
= obstack_finish (&muscle_obstack
);
168 entry
->value
= entry
->storage
= xstrdup (new_val
);
169 obstack_free (&muscle_obstack
, new_val
);
174 /*------------------------------------------------------------------.
175 | Append VALUE to the current value of KEY, using muscle_grow. But |
176 | in addition, issue a synchronization line for the location LOC. |
177 `------------------------------------------------------------------*/
180 muscle_code_grow (const char *key
, const char *val
, location loc
)
182 char *extension
= NULL
;
183 obstack_fgrow1 (&muscle_obstack
, "]b4_syncline(%d, [[", loc
.start
.line
);
184 MUSCLE_OBSTACK_SGROW (&muscle_obstack
,
185 quotearg_style (c_quoting_style
, loc
.start
.file
));
186 obstack_sgrow (&muscle_obstack
, "]])[\n");
187 obstack_sgrow (&muscle_obstack
, val
);
188 obstack_1grow (&muscle_obstack
, 0);
189 extension
= obstack_finish (&muscle_obstack
);
190 muscle_grow (key
, extension
, "");
191 obstack_free (&muscle_obstack
, extension
);
195 void muscle_pair_list_grow (const char *muscle
,
196 const char *a1
, const char *a2
)
199 obstack_sgrow (&muscle_obstack
, "[[[");
200 MUSCLE_OBSTACK_SGROW (&muscle_obstack
, a1
);
201 obstack_sgrow (&muscle_obstack
, "]], [[");
202 MUSCLE_OBSTACK_SGROW (&muscle_obstack
, a2
);
203 obstack_sgrow (&muscle_obstack
, "]]]");
204 obstack_1grow (&muscle_obstack
, 0);
205 pair
= obstack_finish (&muscle_obstack
);
206 muscle_grow (muscle
, pair
, ",\n");
207 obstack_free (&muscle_obstack
, pair
);
211 /*----------------------------------------------------------------------------.
212 | Find the value of muscle KEY. Unlike MUSCLE_FIND, this is always reliable |
213 | to determine whether KEY has a value. |
214 `----------------------------------------------------------------------------*/
217 muscle_find_const (char const *key
)
220 muscle_entry
*result
= NULL
;
223 result
= hash_lookup (muscle_table
, &probe
);
225 return result
->value
;
230 /*----------------------------------------------------------------------------.
231 | Find the value of muscle KEY. Abort if muscle_insert was invoked more |
232 | recently than muscle_grow for KEY since muscle_find can't return a |
234 `----------------------------------------------------------------------------*/
237 muscle_find (char const *key
)
240 muscle_entry
*result
= NULL
;
243 result
= hash_lookup (muscle_table
, &probe
);
246 aver (result
->value
== result
->storage
);
247 return result
->storage
;
254 muscle_boundary_grow (char const *key
, boundary bound
)
257 MUSCLE_OBSTACK_SGROW (&muscle_obstack
, bound
.file
);
258 obstack_1grow (&muscle_obstack
, ':');
259 obstack_fgrow1 (&muscle_obstack
, "%d", bound
.line
);
260 obstack_1grow (&muscle_obstack
, '.');
261 obstack_fgrow1 (&muscle_obstack
, "%d", bound
.column
);
262 obstack_1grow (&muscle_obstack
, '\0');
263 extension
= obstack_finish (&muscle_obstack
);
264 muscle_grow (key
, extension
, "");
265 obstack_free (&muscle_obstack
, extension
);
269 muscle_location_grow (char const *key
, location loc
)
271 muscle_grow (key
, "[[", "");
272 muscle_boundary_grow (key
, loc
.start
);
273 muscle_grow (key
, "]], [[", "");
274 muscle_boundary_grow (key
, loc
.end
);
275 muscle_grow (key
, "]]", "");
278 #define MUSCLE_COMMON_DECODE(Value) \
280 aver (*++(Value) == ']'); \
281 aver (*++(Value) == '['); \
282 obstack_sgrow (&muscle_obstack, "$"); \
285 switch (*++(Value)) \
287 case '@': obstack_sgrow (&muscle_obstack, "@" ); break; \
288 case '{': obstack_sgrow (&muscle_obstack, "[" ); break; \
289 case '}': obstack_sgrow (&muscle_obstack, "]" ); break; \
290 default: aver (false); break; \
294 obstack_1grow (&muscle_obstack, *(Value)); \
297 /* Reverse of MUSCLE_OBSTACK_SGROW. */
299 muscle_string_decode (char const *key
)
305 value
= muscle_find_const (key
);
311 MUSCLE_COMMON_DECODE (value
)
318 value_decoded
= obstack_finish (&muscle_obstack
);
319 result
= xstrdup (value_decoded
);
320 obstack_free (&muscle_obstack
, value_decoded
);
324 /* Reverse of muscle_location_grow. */
326 muscle_location_decode (char const *key
)
329 char const *value
= muscle_find_const (key
);
331 aver (*value
== '[');
332 aver (*++value
== '[');
336 MUSCLE_COMMON_DECODE (value
)
343 aver (*++value
== ']');
344 obstack_1grow (&muscle_obstack
, '\0');
345 boundary_str
= obstack_finish (&muscle_obstack
);
349 boundary_set_from_string (&loc
.start
, boundary_str
);
350 obstack_free (&muscle_obstack
, boundary_str
);
351 aver (*++value
== ' ');
352 aver (*++value
== '[');
353 aver (*++value
== '[');
356 boundary_set_from_string (&loc
.end
, boundary_str
);
357 obstack_free (&muscle_obstack
, boundary_str
);
372 muscle_user_name_list_grow (char const *key
, char const *user_name
,
375 muscle_grow (key
, "[[[[", ",");
376 muscle_grow (key
, user_name
, "");
377 muscle_grow (key
, "]], ", "");
378 muscle_location_grow (key
, loc
);
379 muscle_grow (key
, "]]", "");
382 #define MUSCLE_USER_NAME_CONVERT(NAME, PREFIX, USER_NAME, SUFFIX) \
385 size_t length = strlen ((USER_NAME)); \
386 tmp = xmalloc (sizeof (PREFIX) - 1 + length + sizeof (SUFFIX)); \
387 strcpy (tmp, (PREFIX)); \
388 strcpy (tmp + sizeof (PREFIX) - 1, (USER_NAME)); \
389 strcpy (tmp + sizeof (PREFIX) - 1 + length, (SUFFIX)); \
390 (NAME) = uniqstr_new (tmp); \
395 muscle_percent_define_insert (char const *variable
, location variable_loc
,
399 char const *loc_name
;
401 MUSCLE_USER_NAME_CONVERT (name
, "percent_define(", variable
, ")");
402 MUSCLE_USER_NAME_CONVERT (loc_name
, "percent_define_loc(", variable
, ")");
404 if (muscle_find_const (name
))
406 warn_at (variable_loc
, _("%s `%s' redefined"),
407 "%define variable", variable
);
408 warn_at (muscle_location_decode (loc_name
), _("previous definition"));
410 MUSCLE_INSERT_STRING (name
, value
);
412 muscle_insert (loc_name
, "");
413 muscle_location_grow (loc_name
, variable_loc
);
414 muscle_user_name_list_grow ("percent_define_user_variables", variable
,
419 muscle_percent_define_get (char const *variable
)
422 char const *usage_name
;
425 MUSCLE_USER_NAME_CONVERT (name
, "percent_define(", variable
, ")");
426 MUSCLE_USER_NAME_CONVERT (usage_name
, "percent_define_bison_variables(",
429 muscle_insert (usage_name
, "");
430 value
= muscle_string_decode (name
);
432 value
= xstrdup ("");
437 muscle_percent_define_ifdef (char const *variable
)
440 char const *usage_name
;
443 MUSCLE_USER_NAME_CONVERT (name
, "percent_define(", variable
, ")");
444 MUSCLE_USER_NAME_CONVERT (usage_name
, "percent_define_bison_variables(",
447 value
= muscle_find_const (name
);
450 muscle_insert (usage_name
, "");
458 muscle_percent_define_flag_if (char const *variable
)
461 char const *loc_name
;
462 char const *usage_name
;
465 MUSCLE_USER_NAME_CONVERT (name
, "percent_define(", variable
, ")");
466 MUSCLE_USER_NAME_CONVERT (loc_name
, "percent_define_loc(", variable
, ")");
467 MUSCLE_USER_NAME_CONVERT (usage_name
, "percent_define_bison_variables(",
470 if (muscle_percent_define_ifdef (variable
))
472 char *value
= muscle_percent_define_get (variable
);
473 if (value
[0] == '\0' || 0 == strcmp (value
, "true"))
475 else if (0 == strcmp (value
, "false"))
477 else if (!muscle_find_const (usage_name
))
478 complain_at(muscle_location_decode (loc_name
),
479 _("invalid value for %%define boolean variable `%s'"),
484 fatal(_("undefined %%define variable `%s' passed to muscle_percent_define_flag_if"),
491 muscle_percent_define_default (char const *variable
, char const *value
)
494 char const *loc_name
;
495 MUSCLE_USER_NAME_CONVERT (name
, "percent_define(", variable
, ")");
496 MUSCLE_USER_NAME_CONVERT (loc_name
, "percent_define_loc(", variable
, ")");
497 if (!muscle_find_const (name
))
500 MUSCLE_INSERT_STRING (name
, value
);
501 loc
.start
.file
= loc
.end
.file
= "[Bison:muscle_percent_define_default]";
502 loc
.start
.line
= loc
.start
.column
= 0;
503 loc
.end
.line
= loc
.end
.column
= 0;
504 muscle_insert (loc_name
, "");
505 muscle_location_grow (loc_name
, loc
);
510 muscle_percent_define_check_values (char const * const *values
)
512 for (; *values
; ++values
)
514 char const *variable
= *values
;
516 char const *loc_name
;
519 MUSCLE_USER_NAME_CONVERT (name
, "percent_define(", variable
, ")");
520 MUSCLE_USER_NAME_CONVERT (loc_name
, "percent_define_loc(", variable
, ")");
522 value
= muscle_string_decode (name
);
526 for (++values
; *values
; ++values
)
528 if (0 == strcmp (value
, *values
))
537 complain_at(muscle_location_decode (loc_name
),
538 _("invalid value for %%define variable `%s': `%s'"),
543 fatal(_("undefined %%define variable `%s' passed to muscle_percent_define_check_values"),
549 muscle_percent_code_grow (char const *qualifier
, location qualifier_loc
,
550 char const *code
, location code_loc
)
553 MUSCLE_USER_NAME_CONVERT (name
, "percent_code(", qualifier
, ")");
554 muscle_code_grow (name
, code
, code_loc
);
555 muscle_user_name_list_grow ("percent_code_user_qualifiers", qualifier
,
560 /*------------------------------------------------.
561 | Output the definition of ENTRY as a m4_define. |
562 `------------------------------------------------*/
565 muscle_m4_output (muscle_entry
*entry
, FILE *out
)
567 fprintf (out
, "m4_define([b4_%s],\n", entry
->key
);
568 fprintf (out
, "[[%s]])\n\n\n", entry
->value
);
573 muscle_m4_output_processor (void *entry
, void *out
)
575 return muscle_m4_output (entry
, out
);
579 /*----------------------------------------------------------------.
580 | Output the definition of all the current muscles into a list of |
582 `----------------------------------------------------------------*/
585 muscles_m4_output (FILE *out
)
587 hash_do_for_each (muscle_table
, muscle_m4_output_processor
, out
);