]> git.saurik.com Git - bison.git/blob - src/muscle-tab.c
Merge remote-tracking branch 'origin/maint'
[bison.git] / src / muscle-tab.c
1 /* Muscle table manager for Bison.
2
3 Copyright (C) 2001-2013 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
25 #include "complain.h"
26 #include "files.h"
27 #include "getargs.h"
28 #include "muscle-tab.h"
29 #include "quote.h"
30
31 muscle_kind
32 muscle_kind_new (char const *k)
33 {
34 if (STREQ (k, "code"))
35 return muscle_code;
36 else if (STREQ (k, "keyword"))
37 return muscle_keyword;
38 else if (STREQ (k, "string"))
39 return muscle_string;
40 aver (0);
41 }
42
43 char const *
44 muscle_kind_string (muscle_kind k)
45 {
46 switch (k)
47 {
48 case muscle_code: return "code";
49 case muscle_keyword: return "keyword";
50 case muscle_string: return "string";
51 }
52 aver (0);
53 }
54
55
56 /* A key-value pair, along with storage that can be reclaimed when
57 this pair is no longer needed. */
58 typedef struct
59 {
60 char const *key;
61 char const *value;
62 char *storage;
63 muscle_kind kind;
64 } muscle_entry;
65
66
67 /* The name of muscle for the %define variable VAR (corresponding to
68 FIELD, if defined). */
69 static uniqstr
70 muscle_name (char const *var, char const *field)
71 {
72 if (field)
73 return UNIQSTR_CONCAT ("percent_define_", field, "(", var, ")");
74 else
75 return UNIQSTR_CONCAT ("percent_define(", var, ")");
76 }
77
78 /* An obstack used to create some entries. */
79 struct obstack muscle_obstack;
80
81 /* Initial capacity of muscles hash table. */
82 #define HT_INITIAL_CAPACITY 257
83
84 static struct hash_table *muscle_table = NULL;
85
86 static bool
87 hash_compare_muscles (void const *x, void const *y)
88 {
89 muscle_entry const *m1 = x;
90 muscle_entry const *m2 = y;
91 return STREQ (m1->key, m2->key);
92 }
93
94 static size_t
95 hash_muscle (const void *x, size_t tablesize)
96 {
97 muscle_entry const *m = x;
98 return hash_string (m->key, tablesize);
99 }
100
101 /* Create a fresh muscle name KEY, and insert in the hash table. */
102 static void *
103 muscle_entry_new (char const *key)
104 {
105 muscle_entry *res = xmalloc (sizeof *res);
106 res->key = key;
107 res->value = NULL;
108 res->storage = NULL;
109 if (!hash_insert (muscle_table, res))
110 xalloc_die ();
111 return res;
112 }
113
114 static void
115 muscle_entry_free (void *entry)
116 {
117 muscle_entry *mentry = entry;
118 free (mentry->storage);
119 free (mentry);
120 }
121
122 void
123 muscle_init (void)
124 {
125 /* Initialize the muscle obstack. */
126 obstack_init (&muscle_obstack);
127
128 muscle_table = hash_initialize (HT_INITIAL_CAPACITY, NULL, hash_muscle,
129 hash_compare_muscles, muscle_entry_free);
130
131 /* Version and input file. */
132 MUSCLE_INSERT_STRING ("version", VERSION);
133 }
134
135
136 void
137 muscle_free (void)
138 {
139 hash_free (muscle_table);
140 obstack_free (&muscle_obstack, NULL);
141 }
142
143 /* Look for the muscle named KEY. Return NULL if does not exist. */
144 static
145 muscle_entry *
146 muscle_lookup (char const *key)
147 {
148 muscle_entry probe;
149 probe.key = key;
150 return hash_lookup (muscle_table, &probe);
151 }
152
153
154 void
155 muscle_insert (char const *key, char const *value)
156 {
157 muscle_entry *entry = muscle_lookup (key);
158 if (entry)
159 free (entry->storage);
160 else
161 /* First insertion in the hash. */
162 entry = muscle_entry_new (key);
163 entry->value = value;
164 entry->storage = NULL;
165 }
166
167
168 /* Append VALUE to the current value of KEY. If KEY did not already
169 exist, create it. Use MUSCLE_OBSTACK. De-allocate the previously
170 associated value. Copy VALUE and SEPARATOR. If VALUE does not end
171 with TERMINATOR, append one. */
172
173 static void
174 muscle_grow (const char *key, const char *val,
175 const char *separator, const char *terminator)
176 {
177 muscle_entry *entry = muscle_lookup (key);
178 size_t vals = strlen (val);
179 size_t terms = strlen (terminator);
180
181 if (entry)
182 {
183 obstack_sgrow (&muscle_obstack, entry->value);
184 obstack_sgrow (&muscle_obstack, separator);
185 free (entry->storage);
186 }
187 else
188 entry = muscle_entry_new (key);
189
190 obstack_sgrow (&muscle_obstack, val);
191
192 if (terms <= vals
193 && STRNEQ (val + vals - terms, terminator))
194 obstack_sgrow (&muscle_obstack, terminator);
195
196 {
197 char *new_val = obstack_finish0 (&muscle_obstack);
198 entry->value = entry->storage = xstrdup (new_val);
199 obstack_free (&muscle_obstack, new_val);
200 }
201 }
202
203 /*------------------------------------------------------------------.
204 | Using muscle_grow, append a synchronization line for the location |
205 | LOC to the current value of KEY. |
206 `------------------------------------------------------------------*/
207
208 static void
209 muscle_syncline_grow (char const *key, location loc)
210 {
211 char *extension = NULL;
212 obstack_printf (&muscle_obstack, "]b4_syncline(%d, ", loc.start.line);
213 obstack_quote (&muscle_obstack,
214 quotearg_style (c_quoting_style, loc.start.file));
215 obstack_sgrow (&muscle_obstack, ")[");
216 extension = obstack_finish0 (&muscle_obstack);
217 muscle_grow (key, extension, "", "");
218 obstack_free (&muscle_obstack, extension);
219 }
220
221 /*------------------------------------------------------------------.
222 | Append VALUE to the current value of KEY, using muscle_grow. But |
223 | in addition, issue a synchronization line for the location LOC |
224 | using muscle_syncline_grow. |
225 `------------------------------------------------------------------*/
226
227 void
228 muscle_code_grow (const char *key, const char *val, location loc)
229 {
230 muscle_syncline_grow (key, loc);
231 muscle_grow (key, val, "\n", "\n");
232 }
233
234
235 void
236 muscle_pair_list_grow (const char *muscle,
237 const char *a1, const char *a2)
238 {
239 char *pair;
240 obstack_sgrow (&muscle_obstack, "[");
241 obstack_quote (&muscle_obstack, a1);
242 obstack_sgrow (&muscle_obstack, ", ");
243 obstack_quote (&muscle_obstack, a2);
244 obstack_sgrow (&muscle_obstack, "]");
245 pair = obstack_finish0 (&muscle_obstack);
246 muscle_grow (muscle, pair, ",\n", "");
247 obstack_free (&muscle_obstack, pair);
248 }
249
250
251 char const *
252 muscle_find_const (char const *key)
253 {
254 muscle_entry *entry = muscle_lookup (key);
255 return entry ? entry->value : NULL;
256 }
257
258
259 char *
260 muscle_find (char const *key)
261 {
262 muscle_entry *entry = muscle_lookup (key);
263 if (entry)
264 {
265 aver (entry->value == entry->storage);
266 return entry->storage;
267 }
268 return NULL;
269 }
270
271
272 /* In the format 'file_name:line.column', append BOUND to MUSCLE. Use
273 digraphs for special characters in the file name. */
274
275 static void
276 muscle_boundary_grow (char const *key, boundary bound)
277 {
278 char *extension;
279 obstack_sgrow (&muscle_obstack, "[[");
280 obstack_escape (&muscle_obstack, bound.file);
281 obstack_printf (&muscle_obstack, ":%d.%d]]", bound.line, bound.column);
282 extension = obstack_finish0 (&muscle_obstack);
283 muscle_grow (key, extension, "", "");
284 obstack_free (&muscle_obstack, extension);
285 }
286
287
288 /* In the format '[[file_name:line.column]], [[file_name:line.column]]',
289 append LOC to MUSCLE. Use digraphs for special characters in each
290 file name. */
291
292 static void
293 muscle_location_grow (char const *key, location loc)
294 {
295 muscle_boundary_grow (key, loc.start);
296 muscle_grow (key, "", ", ", "");
297 muscle_boundary_grow (key, loc.end);
298 }
299
300 #define COMMON_DECODE(Value) \
301 case '$': \
302 aver (*++(Value) == ']'); \
303 aver (*++(Value) == '['); \
304 obstack_sgrow (&muscle_obstack, "$"); \
305 break; \
306 case '@': \
307 switch (*++(Value)) \
308 { \
309 case '@': obstack_sgrow (&muscle_obstack, "@" ); break; \
310 case '{': obstack_sgrow (&muscle_obstack, "[" ); break; \
311 case '}': obstack_sgrow (&muscle_obstack, "]" ); break; \
312 default: aver (false); break; \
313 } \
314 break; \
315 default: \
316 obstack_1grow (&muscle_obstack, *(Value)); \
317 break;
318
319 /* Reverse of obstack_escape. */
320 static char *
321 string_decode (char const *key)
322 {
323 char const *value = muscle_find_const (key);
324 char *value_decoded;
325 char *result;
326
327 if (!value)
328 return NULL;
329 do {
330 switch (*value)
331 {
332 COMMON_DECODE (value)
333 case '[':
334 case ']':
335 aver (false);
336 break;
337 }
338 } while (*value++);
339 value_decoded = obstack_finish (&muscle_obstack);
340 result = xstrdup (value_decoded);
341 obstack_free (&muscle_obstack, value_decoded);
342 return result;
343 }
344
345 /* Reverse of muscle_location_grow. */
346 static location
347 location_decode (char const *value)
348 {
349 location loc;
350 aver (value);
351 aver (*value == '[');
352 aver (*++value == '[');
353 while (*++value)
354 switch (*value)
355 {
356 COMMON_DECODE (value)
357 case '[':
358 aver (false);
359 break;
360 case ']':
361 {
362 char *boundary_str;
363 aver (*++value == ']');
364 boundary_str = obstack_finish0 (&muscle_obstack);
365 switch (*++value)
366 {
367 case ',':
368 boundary_set_from_string (&loc.start, boundary_str);
369 obstack_free (&muscle_obstack, boundary_str);
370 aver (*++value == ' ');
371 aver (*++value == '[');
372 aver (*++value == '[');
373 break;
374 case '\0':
375 boundary_set_from_string (&loc.end, boundary_str);
376 obstack_free (&muscle_obstack, boundary_str);
377 return loc;
378 break;
379 default:
380 aver (false);
381 break;
382 }
383 }
384 break;
385 }
386 aver (false);
387 return loc;
388 }
389
390 void
391 muscle_user_name_list_grow (char const *key, char const *user_name,
392 location loc)
393 {
394 muscle_grow (key, "[[[[", ",", "");
395 muscle_grow (key, user_name, "", "");
396 muscle_grow (key, "]], ", "", "");
397 muscle_location_grow (key, loc);
398 muscle_grow (key, "]]", "", "");
399 }
400
401
402 /** Return an allocated string that represents the %define directive
403 that performs the assignment.
404
405 @param assignment "VAR", or "VAR=VAL".
406 @param value default value if VAL \a assignment has no '='.
407
408 For instance:
409 "foo", NULL => "%define foo"
410 "foo", "baz" => "%define foo baz"
411 "foo=bar", NULL => "%define foo bar"
412 "foo=bar", "baz" => "%define foo bar"
413 "foo=", NULL => "%define foo"
414 "foo=", "baz" => "%define foo"
415 */
416
417 static
418 char *
419 define_directive (char const *assignment, char const *value)
420 {
421 char *eq = strchr (assignment, '=');
422 char const *fmt = !eq && value && *value ? "%%define %s %s" : "%%define %s";
423 char *res = xmalloc (strlen (fmt) + strlen (assignment)
424 + (value ? strlen (value) : 0));
425 sprintf (res, fmt, assignment, value);
426 eq = strchr (res, '=');
427 if (eq)
428 *eq = eq[1] ? ' ' : '\0';
429 return res;
430 }
431
432 /** If the \a variable name is obsolete, return the name to use,
433 * otherwise \a variable. If the \a value is obsolete, update it too.
434 *
435 * Allocates the returned value. */
436 static
437 char *
438 muscle_percent_variable_update (char const *variable, location variable_loc,
439 char const **value)
440 {
441 typedef struct
442 {
443 const char *obsolete;
444 const char *updated;
445 } conversion_type;
446 const conversion_type conversion[] =
447 {
448 { "api.push_pull", "api.push-pull", },
449 { "api.tokens.prefix", "api.token.prefix", },
450 { "lex_symbol", "api.token.constructor", },
451 { "location_type", "api.location.type", },
452 { "lr.default-reductions", "lr.default-reduction", },
453 { "lr.keep-unreachable-states", "lr.keep-unreachable-state", },
454 { "lr.keep_unreachable_states", "lr.keep-unreachable-state", },
455 { "namespace", "api.namespace", },
456 { "stype", "api.value.type", },
457 { "variant=", "api.value.type=variant", },
458 { "variant=true", "api.value.type=variant", },
459 { NULL, NULL, }
460 };
461 conversion_type const *c;
462 for (c = conversion; c->obsolete; ++c)
463 {
464 char const *eq = strchr (c->obsolete, '=');
465 if (eq
466 ? (!strncmp (c->obsolete, variable, eq - c->obsolete)
467 && STREQ (eq + 1, *value))
468 : STREQ (c->obsolete, variable))
469 {
470 char *old = define_directive (c->obsolete, *value);
471 char *upd = define_directive (c->updated, *value);
472 deprecated_directive (&variable_loc, old, upd);
473 free (old);
474 free (upd);
475 char *res = xstrdup (c->updated);
476 {
477 char *eq2 = strchr (res, '=');
478 if (eq2)
479 {
480 *eq2 = '\0';
481 *value = eq2 + 1;
482 }
483 }
484 return res;
485 }
486 }
487 return xstrdup (variable);
488 }
489
490 void
491 muscle_percent_define_insert (char const *var, location variable_loc,
492 muscle_kind kind,
493 char const *value,
494 muscle_percent_define_how how)
495 {
496 /* Backward compatibility. */
497 char *variable = muscle_percent_variable_update (var, variable_loc, &value);
498 uniqstr name = muscle_name (variable, NULL);
499 uniqstr loc_name = muscle_name (variable, "loc");
500 uniqstr syncline_name = muscle_name (variable, "syncline");
501 uniqstr how_name = muscle_name (variable, "how");
502 uniqstr kind_name = muscle_name (variable, "kind");
503
504 /* Command-line options are processed before the grammar file. */
505 if (how == MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE
506 && muscle_find_const (name))
507 {
508 muscle_percent_define_how how_old = atoi (muscle_find_const (how_name));
509 unsigned i = 0;
510 if (how_old == MUSCLE_PERCENT_DEFINE_F)
511 goto end;
512 complain_indent (&variable_loc, complaint, &i,
513 _("%%define variable %s redefined"),
514 quote (variable));
515 i += SUB_INDENT;
516 location loc = muscle_percent_define_get_loc (variable);
517 complain_indent (&loc, complaint, &i, _("previous definition"));
518 }
519
520 MUSCLE_INSERT_STRING (name, value);
521 muscle_insert (loc_name, "");
522 muscle_location_grow (loc_name, variable_loc);
523 muscle_insert (syncline_name, "");
524 muscle_syncline_grow (syncline_name, variable_loc);
525 muscle_user_name_list_grow ("percent_define_user_variables", variable,
526 variable_loc);
527 MUSCLE_INSERT_INT (how_name, how);
528 MUSCLE_INSERT_STRING (kind_name, muscle_kind_string (kind));
529 end:
530 free (variable);
531 }
532
533 /* This is used for backward compatibility, e.g., "%define api.pure"
534 supersedes "%pure-parser". */
535 void
536 muscle_percent_define_ensure (char const *variable, location loc,
537 bool value)
538 {
539 uniqstr name = muscle_name (variable, NULL);
540 char const *val = value ? "" : "false";
541
542 /* Don't complain is VARIABLE is already defined, but be sure to set
543 its value to VAL. */
544 if (!muscle_find_const (name)
545 || muscle_percent_define_flag_if (variable) != value)
546 muscle_percent_define_insert (variable, loc, muscle_keyword, val,
547 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
548 }
549
550 /* Mark %define VARIABLE as used. */
551 static void
552 muscle_percent_define_use (char const *variable)
553 {
554 muscle_insert (muscle_name (variable, "bison_variables"), "");
555 }
556
557 /* The value of %define variable VARIABLE (corresponding to FIELD, if
558 defined). Do not register as used, but diagnose unset variables. */
559
560 char const *
561 muscle_percent_define_get_raw (char const *variable, char const *field)
562 {
563 uniqstr name = muscle_name (variable, field);
564 char const *res = muscle_find_const (name);
565 if (!res)
566 complain (NULL, fatal, _("%s: undefined %%define variable %s"),
567 "muscle_percent_define_get_raw", quote (variable));
568 return res;
569 }
570
571 char *
572 muscle_percent_define_get (char const *variable)
573 {
574 uniqstr name = muscle_name (variable, NULL);
575 char *value = string_decode (name);
576 if (!value)
577 value = xstrdup ("");
578 muscle_percent_define_use (variable);
579 return value;
580 }
581
582 /* The kind of VARIABLE. An error if undefined. */
583 static muscle_kind
584 muscle_percent_define_get_kind (char const *variable)
585 {
586 return muscle_kind_new (muscle_percent_define_get_raw (variable, "kind"));
587 }
588
589 /* Check the kind of VARIABLE. An error if undefined. */
590 static void
591 muscle_percent_define_check_kind (char const *variable, muscle_kind kind)
592 {
593 if (muscle_percent_define_get_kind (variable) != kind)
594 {
595 location loc = muscle_percent_define_get_loc (variable);
596 switch (kind)
597 {
598 case muscle_code:
599 complain (&loc, Wdeprecated,
600 "%%define variable '%s' requires '{...}' values",
601 variable);
602 break;
603 case muscle_keyword:
604 complain (&loc, Wdeprecated,
605 "%%define variable '%s' requires keyword values",
606 variable);
607 break;
608 case muscle_string:
609 complain (&loc, Wdeprecated,
610 "%%define variable '%s' requires '\"...\"' values",
611 variable);
612 break;
613 }
614 }
615 }
616
617
618 location
619 muscle_percent_define_get_loc (char const *variable)
620 {
621 return location_decode (muscle_percent_define_get_raw (variable, "loc"));
622 }
623
624 char const *
625 muscle_percent_define_get_syncline (char const *variable)
626 {
627 return muscle_percent_define_get_raw (variable, "syncline");
628 }
629
630 bool
631 muscle_percent_define_ifdef (char const *variable)
632 {
633 if (muscle_find_const (muscle_name (variable, NULL)))
634 {
635 muscle_percent_define_use (variable);
636 return true;
637 }
638 else
639 return false;
640 }
641
642 bool
643 muscle_percent_define_flag_if (char const *variable)
644 {
645 uniqstr invalid_boolean_name = muscle_name (variable, "invalid_boolean");
646 bool result = false;
647
648 if (muscle_percent_define_ifdef (variable))
649 {
650 char *value = muscle_percent_define_get (variable);
651 muscle_percent_define_check_kind (variable, muscle_keyword);
652 if (value[0] == '\0' || STREQ (value, "true"))
653 result = true;
654 else if (STREQ (value, "false"))
655 result = false;
656 else if (!muscle_find_const (invalid_boolean_name))
657 {
658 muscle_insert (invalid_boolean_name, "");
659 location loc = muscle_percent_define_get_loc (variable);
660 complain (&loc, complaint,
661 _("invalid value for %%define Boolean variable %s"),
662 quote (variable));
663 }
664 free (value);
665 }
666 else
667 complain (NULL, fatal, _("%s: undefined %%define variable %s"),
668 "muscle_percent_define_flag", quote (variable));
669
670 return result;
671 }
672
673 void
674 muscle_percent_define_default (char const *variable, char const *value)
675 {
676 uniqstr name = muscle_name (variable, NULL);
677 if (!muscle_find_const (name))
678 {
679 MUSCLE_INSERT_STRING (name, value);
680 MUSCLE_INSERT_STRING (muscle_name (variable, "kind"), "keyword");
681 {
682 uniqstr loc_name = muscle_name (variable, "loc");
683 location loc;
684 loc.start.file = loc.end.file = "<default value>";
685 loc.start.line = loc.end.line = -1;
686 loc.start.column = loc.end.column = -1;
687 muscle_insert (loc_name, "");
688 muscle_location_grow (loc_name, loc);
689 }
690 muscle_insert (muscle_name (variable, "syncline"), "");
691 }
692 }
693
694 void
695 muscle_percent_define_check_values (char const * const *values)
696 {
697 for (; *values; ++values)
698 {
699 char const * const *variablep = values;
700 uniqstr name = muscle_name (*variablep, NULL);
701 char *value = string_decode (name);
702 muscle_percent_define_check_kind (*variablep, muscle_keyword);
703 if (value)
704 {
705 for (++values; *values; ++values)
706 {
707 if (STREQ (value, *values))
708 break;
709 }
710 if (!*values)
711 {
712 unsigned i = 0;
713 location loc = muscle_percent_define_get_loc (*variablep);
714 complain_indent (&loc, complaint, &i,
715 _("invalid value for %%define variable %s: %s"),
716 quote (*variablep), quote_n (1, value));
717 i += SUB_INDENT;
718 for (values = variablep + 1; *values; ++values)
719 complain_indent (&loc, complaint | no_caret | silent, &i,
720 _("accepted value: %s"), quote (*values));
721 }
722 else
723 {
724 while (*values)
725 ++values;
726 }
727 free (value);
728 }
729 else
730 complain (NULL, fatal, _("%s: undefined %%define variable %s"),
731 "muscle_percent_define_check_values", quote (*variablep));
732 }
733 }
734
735 void
736 muscle_percent_code_grow (char const *qualifier, location qualifier_loc,
737 char const *code, location code_loc)
738 {
739 char const *name = UNIQSTR_CONCAT ("percent_code(", qualifier, ")");
740 muscle_code_grow (name, code, code_loc);
741 muscle_user_name_list_grow ("percent_code_user_qualifiers", qualifier,
742 qualifier_loc);
743 }
744
745
746 /*------------------------------------------------.
747 | Output the definition of ENTRY as a m4_define. |
748 `------------------------------------------------*/
749
750 static inline bool
751 muscle_m4_output (muscle_entry *entry, FILE *out)
752 {
753 fprintf (out,
754 "m4_define([b4_%s],\n"
755 "[[%s]])\n\n\n", entry->key, entry->value);
756 return true;
757 }
758
759 static bool
760 muscle_m4_output_processor (void *entry, void *out)
761 {
762 return muscle_m4_output (entry, out);
763 }
764
765
766 void
767 muscles_m4_output (FILE *out)
768 {
769 hash_do_for_each (muscle_table, muscle_m4_output_processor, out);
770 }