]> git.saurik.com Git - bison.git/blob - src/muscle_tab.c
Prepare for 2.4.2 release.
[bison.git] / src / muscle_tab.c
1 /* Muscle table manager for Bison.
2
3 Copyright (C) 2001-2007, 2009-2010 Free Software Foundation, Inc.
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
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.
11
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.
16
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/>. */
19
20 #include <config.h>
21 #include "system.h"
22
23 #include <hash.h>
24 #include <quotearg.h>
25
26 #include "complain.h"
27 #include "files.h"
28 #include "muscle_tab.h"
29 #include "getargs.h"
30
31 /* A key-value pair, along with storage that can be reclaimed when
32 this pair is no longer needed. */
33 typedef struct
34 {
35 char const *key;
36 char const *value;
37 char *storage;
38 } muscle_entry;
39
40 /* An obstack used to create some entries. */
41 struct obstack muscle_obstack;
42
43 /* Initial capacity of muscles hash table. */
44 #define HT_INITIAL_CAPACITY 257
45
46 static struct hash_table *muscle_table = NULL;
47
48 static bool
49 hash_compare_muscles (void const *x, void const *y)
50 {
51 muscle_entry const *m1 = x;
52 muscle_entry const *m2 = y;
53 return strcmp (m1->key, m2->key) == 0;
54 }
55
56 static size_t
57 hash_muscle (const void *x, size_t tablesize)
58 {
59 muscle_entry const *m = x;
60 return hash_string (m->key, tablesize);
61 }
62
63 /*-----------------------------------------------------------------.
64 | Create the MUSCLE_TABLE, and initialize it with default values. |
65 | Also set up the MUSCLE_OBSTACK. |
66 `-----------------------------------------------------------------*/
67
68 static void
69 muscle_entry_free (void *entry)
70 {
71 muscle_entry *mentry = entry;
72 free (mentry->storage);
73 free (mentry);
74 }
75
76 void
77 muscle_init (void)
78 {
79 /* Initialize the muscle obstack. */
80 obstack_init (&muscle_obstack);
81
82 muscle_table = hash_initialize (HT_INITIAL_CAPACITY, NULL, hash_muscle,
83 hash_compare_muscles, muscle_entry_free);
84
85 /* Version and input file. */
86 MUSCLE_INSERT_STRING ("version", VERSION);
87 MUSCLE_INSERT_C_STRING ("file_name", grammar_file);
88 }
89
90
91 /*------------------------------------------------------------.
92 | Free all the memory consumed by the muscle machinery only. |
93 `------------------------------------------------------------*/
94
95 void
96 muscle_free (void)
97 {
98 hash_free (muscle_table);
99 obstack_free (&muscle_obstack, NULL);
100 }
101
102
103
104 /*------------------------------------------------------------.
105 | Insert (KEY, VALUE). If KEY already existed, overwrite the |
106 | previous value. |
107 `------------------------------------------------------------*/
108
109 void
110 muscle_insert (char const *key, char const *value)
111 {
112 muscle_entry probe;
113 muscle_entry *entry;
114
115 probe.key = key;
116 entry = hash_lookup (muscle_table, &probe);
117
118 if (!entry)
119 {
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 ();
125 }
126 else
127 free (entry->storage);
128 entry->value = value;
129 entry->storage = NULL;
130 }
131
132
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 `-------------------------------------------------------------------*/
138
139 void
140 muscle_grow (const char *key, const char *val, const char *separator)
141 {
142 muscle_entry probe;
143 muscle_entry *entry = NULL;
144
145 probe.key = key;
146 entry = hash_lookup (muscle_table, &probe);
147
148 if (!entry)
149 {
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);
156 }
157 else
158 {
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);
169 }
170 }
171
172 /*------------------------------------------------------------------.
173 | Using muscle_grow, append a synchronization line for the location |
174 | LOC to the current value of KEY. |
175 `------------------------------------------------------------------*/
176
177 static void
178 muscle_syncline_grow (char const *key, location loc)
179 {
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);
189 }
190
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 `------------------------------------------------------------------*/
196
197 void
198 muscle_code_grow (const char *key, const char *val, location loc)
199 {
200 muscle_syncline_grow (key, loc);
201 muscle_grow (key, val, "\n");
202 }
203
204
205 void muscle_pair_list_grow (const char *muscle,
206 const char *a1, const char *a2)
207 {
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);
218 }
219
220
221 /*----------------------------------------------------------------------------.
222 | Find the value of muscle KEY. Unlike MUSCLE_FIND, this is always reliable |
223 | to determine whether KEY has a value. |
224 `----------------------------------------------------------------------------*/
225
226 char const *
227 muscle_find_const (char const *key)
228 {
229 muscle_entry probe;
230 muscle_entry *result = NULL;
231
232 probe.key = key;
233 result = hash_lookup (muscle_table, &probe);
234 if (result)
235 return result->value;
236 return NULL;
237 }
238
239
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 `----------------------------------------------------------------------------*/
245
246 char *
247 muscle_find (char const *key)
248 {
249 muscle_entry probe;
250 muscle_entry *result = NULL;
251
252 probe.key = key;
253 result = hash_lookup (muscle_table, &probe);
254 if (result)
255 {
256 aver (result->value == result->storage);
257 return result->storage;
258 }
259 return NULL;
260 }
261
262
263 void
264 muscle_boundary_grow (char const *key, boundary bound)
265 {
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);
276 }
277
278 void
279 muscle_location_grow (char const *key, location loc)
280 {
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, "]]", "");
286 }
287
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)) \
296 { \
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; \
301 } \
302 break; \
303 default: \
304 obstack_1grow (&muscle_obstack, *(Value)); \
305 break;
306
307 /* Reverse of MUSCLE_OBSTACK_SGROW. */
308 static char *
309 muscle_string_decode (char const *key)
310 {
311 char const *value;
312 char *value_decoded;
313 char *result;
314
315 value = muscle_find_const (key);
316 if (!value)
317 return NULL;
318 do {
319 switch (*value)
320 {
321 MUSCLE_COMMON_DECODE (value)
322 case '[':
323 case ']':
324 aver (false);
325 break;
326 }
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;
332 }
333
334 /* Reverse of muscle_location_grow. */
335 static location
336 muscle_location_decode (char const *key)
337 {
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)
345 {
346 MUSCLE_COMMON_DECODE (value)
347 case '[':
348 aver (false);
349 break;
350 case ']':
351 {
352 char *boundary_str;
353 aver (*++value == ']');
354 obstack_1grow (&muscle_obstack, '\0');
355 boundary_str = obstack_finish (&muscle_obstack);
356 switch (*++value)
357 {
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;
373 }
374 }
375 break;
376 }
377 aver (false);
378 return loc;
379 }
380
381 void
382 muscle_user_name_list_grow (char const *key, char const *user_name,
383 location loc)
384 {
385 muscle_grow (key, "[[[[", ",");
386 muscle_grow (key, user_name, "");
387 muscle_grow (key, "]], ", "");
388 muscle_location_grow (key, loc);
389 muscle_grow (key, "]]", "");
390 }
391
392 #define MUSCLE_USER_NAME_CONVERT(NAME, PREFIX, USER_NAME, SUFFIX) \
393 do { \
394 char *tmp; \
395 size_t length = strlen ((USER_NAME)); \
396 tmp = xmalloc (sizeof (PREFIX) - 1 + length + sizeof (SUFFIX)); \
397 strcpy (tmp, (PREFIX)); \
398 strcpy (tmp + sizeof (PREFIX) - 1, (USER_NAME)); \
399 strcpy (tmp + sizeof (PREFIX) - 1 + length, (SUFFIX)); \
400 (NAME) = uniqstr_new (tmp); \
401 free (tmp); \
402 } while (0)
403
404 void
405 muscle_percent_define_insert (char const *variable, location variable_loc,
406 char const *value)
407 {
408 char const *name;
409 char const *loc_name;
410 char const *syncline_name;
411
412 MUSCLE_USER_NAME_CONVERT (name, "percent_define(", variable, ")");
413 MUSCLE_USER_NAME_CONVERT (loc_name, "percent_define_loc(", variable, ")");
414 MUSCLE_USER_NAME_CONVERT (syncline_name,
415 "percent_define_syncline(", variable, ")");
416
417 if (muscle_find_const (name))
418 {
419 warn_at (variable_loc, _("%s `%s' redefined"),
420 "%define variable", variable);
421 warn_at (muscle_percent_define_get_loc (variable),
422 _("previous definition"));
423 }
424 MUSCLE_INSERT_STRING (name, value);
425
426 muscle_insert (loc_name, "");
427 muscle_location_grow (loc_name, variable_loc);
428 muscle_insert (syncline_name, "");
429 muscle_syncline_grow (syncline_name, variable_loc);
430 muscle_user_name_list_grow ("percent_define_user_variables", variable,
431 variable_loc);
432 }
433
434 char *
435 muscle_percent_define_get (char const *variable)
436 {
437 char const *name;
438 char const *usage_name;
439 char *value;
440
441 MUSCLE_USER_NAME_CONVERT (name, "percent_define(", variable, ")");
442 MUSCLE_USER_NAME_CONVERT (usage_name, "percent_define_bison_variables(",
443 variable, ")");
444
445 muscle_insert (usage_name, "");
446 value = muscle_string_decode (name);
447 if (!value)
448 value = xstrdup ("");
449 return value;
450 }
451
452 location
453 muscle_percent_define_get_loc (char const *variable)
454 {
455 char const *loc_name;
456 MUSCLE_USER_NAME_CONVERT (loc_name, "percent_define_loc(", variable, ")");
457 if (!muscle_find_const (loc_name))
458 fatal(_("undefined %%define variable `%s' passed to muscle_percent_define_get_loc"),
459 variable);
460 return muscle_location_decode (loc_name);
461 }
462
463 char const *
464 muscle_percent_define_get_syncline (char const *variable)
465 {
466 char const *syncline_name;
467 char const *syncline;
468 MUSCLE_USER_NAME_CONVERT (syncline_name,
469 "percent_define_syncline(", variable, ")");
470 syncline = muscle_find_const (syncline_name);
471 if (!syncline)
472 fatal(_("undefined %%define variable `%s' passed to muscle_percent_define_get_syncline"),
473 variable);
474 return syncline;
475 }
476
477 bool
478 muscle_percent_define_ifdef (char const *variable)
479 {
480 char const *name;
481 char const *usage_name;
482 char const *value;
483
484 MUSCLE_USER_NAME_CONVERT (name, "percent_define(", variable, ")");
485 MUSCLE_USER_NAME_CONVERT (usage_name, "percent_define_bison_variables(",
486 variable, ")");
487
488 value = muscle_find_const (name);
489 if (value)
490 {
491 muscle_insert (usage_name, "");
492 return true;
493 }
494
495 return false;
496 }
497
498 bool
499 muscle_percent_define_flag_if (char const *variable)
500 {
501 char const *invalid_boolean_name;
502 bool result = false;
503
504 MUSCLE_USER_NAME_CONVERT (invalid_boolean_name,
505 "percent_define_invalid_boolean(", variable, ")");
506
507 if (muscle_percent_define_ifdef (variable))
508 {
509 char *value = muscle_percent_define_get (variable);
510 if (value[0] == '\0' || 0 == strcmp (value, "true"))
511 result = true;
512 else if (0 == strcmp (value, "false"))
513 result = false;
514 else if (!muscle_find_const (invalid_boolean_name))
515 {
516 muscle_insert (invalid_boolean_name, "");
517 complain_at(muscle_percent_define_get_loc (variable),
518 _("invalid value for %%define Boolean variable `%s'"),
519 variable);
520 }
521 free (value);
522 }
523 else
524 fatal(_("undefined %%define variable `%s' passed to muscle_percent_define_flag_if"),
525 variable);
526
527 return result;
528 }
529
530 void
531 muscle_percent_define_default (char const *variable, char const *value)
532 {
533 char const *name;
534 char const *loc_name;
535 char const *syncline_name;
536 MUSCLE_USER_NAME_CONVERT (name, "percent_define(", variable, ")");
537 MUSCLE_USER_NAME_CONVERT (loc_name, "percent_define_loc(", variable, ")");
538 MUSCLE_USER_NAME_CONVERT (syncline_name,
539 "percent_define_syncline(", variable, ")");
540 if (!muscle_find_const (name))
541 {
542 location loc;
543 MUSCLE_INSERT_STRING (name, value);
544 loc.start.file = loc.end.file = "<default value>";
545 loc.start.line = loc.end.line = -1;
546 loc.start.column = loc.end.column = -1;
547 muscle_insert (loc_name, "");
548 muscle_location_grow (loc_name, loc);
549 muscle_insert (syncline_name, "");
550 }
551 }
552
553 void
554 muscle_percent_define_check_values (char const * const *values)
555 {
556 for (; *values; ++values)
557 {
558 char const *variable = *values;
559 char const *name;
560 char *value;
561
562 MUSCLE_USER_NAME_CONVERT (name, "percent_define(", variable, ")");
563
564 value = muscle_string_decode (name);
565 if (value)
566 {
567 bool valid = false;
568 for (++values; *values; ++values)
569 {
570 if (0 == strcmp (value, *values))
571 {
572 valid = true;
573 while (*values)
574 ++values;
575 break;
576 }
577 }
578 if (!valid)
579 complain_at(muscle_percent_define_get_loc (variable),
580 _("invalid value for %%define variable `%s': `%s'"),
581 variable, value);
582 free (value);
583 }
584 else
585 fatal(_("undefined %%define variable `%s' passed to muscle_percent_define_check_values"),
586 variable);
587 }
588 }
589
590 void
591 muscle_percent_code_grow (char const *qualifier, location qualifier_loc,
592 char const *code, location code_loc)
593 {
594 char const *name;
595 MUSCLE_USER_NAME_CONVERT (name, "percent_code(", qualifier, ")");
596 muscle_code_grow (name, code, code_loc);
597 muscle_user_name_list_grow ("percent_code_user_qualifiers", qualifier,
598 qualifier_loc);
599 }
600
601
602 /*------------------------------------------------.
603 | Output the definition of ENTRY as a m4_define. |
604 `------------------------------------------------*/
605
606 static inline bool
607 muscle_m4_output (muscle_entry *entry, FILE *out)
608 {
609 fprintf (out, "m4_define([b4_%s],\n", entry->key);
610 fprintf (out, "[[%s]])\n\n\n", entry->value);
611 return true;
612 }
613
614 static bool
615 muscle_m4_output_processor (void *entry, void *out)
616 {
617 return muscle_m4_output (entry, out);
618 }
619
620
621 /*----------------------------------------------------------------.
622 | Output the definition of all the current muscles into a list of |
623 | m4_defines. |
624 `----------------------------------------------------------------*/
625
626 void
627 muscles_m4_output (FILE *out)
628 {
629 hash_do_for_each (muscle_table, muscle_m4_output_processor, out);
630 }