]> git.saurik.com Git - bison.git/blame - src/muscle_tab.c
* src/scan-skel.l (at_directive_perform): Fix switch statements for last patch.
[bison.git] / src / muscle_tab.c
CommitLineData
e00b6826
PE
1/* Muscle table manager for Bison.
2
279cabb6 3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
05ac60f3 4 Foundation, Inc.
f753cd62
MA
5
6 This file is part of Bison, the GNU Compiler Compiler.
7
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)
11 any later version.
12
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.
17
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
0fb669f9
PE
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
f753cd62 22
2cec9080 23#include <config.h>
f753cd62 24#include "system.h"
8322e8f5
PE
25
26#include <hash.h>
27#include <quotearg.h>
28
f753cd62
MA
29#include "files.h"
30#include "muscle_tab.h"
f508cb0a 31#include "getargs.h"
f753cd62 32
eb095650
PE
33/* A key-value pair, along with storage that can be reclaimed when
34 this pair is no longer needed. */
8322e8f5
PE
35typedef struct
36{
eb095650
PE
37 char const *key;
38 char const *value;
39 char *storage;
8322e8f5 40} muscle_entry;
592e8d4d
AD
41
42/* An obstack used to create some entries. */
43struct obstack muscle_obstack;
44
beda758b
AD
45/* Initial capacity of muscles hash table. */
46#define HT_INITIAL_CAPACITY 257
f753cd62 47
04098407 48static struct hash_table *muscle_table = NULL;
f753cd62 49
beda758b
AD
50static bool
51hash_compare_muscles (void const *x, void const *y)
f753cd62 52{
8322e8f5
PE
53 muscle_entry const *m1 = x;
54 muscle_entry const *m2 = y;
5dd5fd4a 55 return strcmp (m1->key, m2->key) == 0;
f753cd62
MA
56}
57
233a88ad
PE
58static size_t
59hash_muscle (const void *x, size_t tablesize)
f753cd62 60{
8322e8f5 61 muscle_entry const *m = x;
beda758b 62 return hash_string (m->key, tablesize);
f753cd62
MA
63}
64
592e8d4d
AD
65/*-----------------------------------------------------------------.
66| Create the MUSCLE_TABLE, and initialize it with default values. |
67| Also set up the MUSCLE_OBSTACK. |
68`-----------------------------------------------------------------*/
69
eb095650
PE
70static void
71muscle_entry_free (void *entry)
72{
73 muscle_entry *mentry = entry;
74 free (mentry->storage);
75 free (mentry);
76}
77
f753cd62
MA
78void
79muscle_init (void)
80{
ae7453f2
AD
81 /* Initialize the muscle obstack. */
82 obstack_init (&muscle_obstack);
83
beda758b 84 muscle_table = hash_initialize (HT_INITIAL_CAPACITY, NULL, hash_muscle,
eb095650 85 hash_compare_muscles, muscle_entry_free);
f753cd62
MA
86
87 /* Version and input file. */
ae7453f2 88 MUSCLE_INSERT_STRING ("version", VERSION);
48b16bbc 89 MUSCLE_INSERT_C_STRING ("file_name", grammar_file);
f753cd62
MA
90}
91
592e8d4d
AD
92
93/*------------------------------------------------------------.
94| Free all the memory consumed by the muscle machinery only. |
95`------------------------------------------------------------*/
96
97void
98muscle_free (void)
99{
100 hash_free (muscle_table);
101 obstack_free (&muscle_obstack, NULL);
102}
103
104
105
ae7453f2
AD
106/*------------------------------------------------------------.
107| Insert (KEY, VALUE). If KEY already existed, overwrite the |
108| previous value. |
109`------------------------------------------------------------*/
110
a870c567 111void
eb095650 112muscle_insert (char const *key, char const *value)
f753cd62 113{
8322e8f5 114 muscle_entry probe;
da2a7671 115 muscle_entry *entry;
e9bca3ad 116
ae7453f2
AD
117 probe.key = key;
118 entry = hash_lookup (muscle_table, &probe);
beda758b
AD
119
120 if (!entry)
121 {
122 /* First insertion in the hash. */
da2a7671 123 entry = xmalloc (sizeof *entry);
beda758b
AD
124 entry->key = key;
125 hash_insert (muscle_table, entry);
126 }
4502eadc
JD
127 else
128 free (entry->storage);
beda758b 129 entry->value = value;
4502eadc 130 entry->storage = NULL;
f753cd62
MA
131}
132
ae7453f2
AD
133
134/*-------------------------------------------------------------------.
ff5150d9
PE
135| Append VALUE to the current value of KEY. If KEY did not already |
136| exist, create it. Use MUSCLE_OBSTACK. De-allocate the previously |
137| associated value. Copy VALUE and SEPARATOR. |
ae7453f2
AD
138`-------------------------------------------------------------------*/
139
140void
141muscle_grow (const char *key, const char *val, const char *separator)
142{
8322e8f5
PE
143 muscle_entry probe;
144 muscle_entry *entry = NULL;
ae7453f2
AD
145
146 probe.key = key;
147 entry = hash_lookup (muscle_table, &probe);
148
149 if (!entry)
150 {
151 /* First insertion in the hash. */
da2a7671 152 entry = xmalloc (sizeof *entry);
ae7453f2
AD
153 entry->key = key;
154 hash_insert (muscle_table, entry);
eb095650 155 entry->value = entry->storage = xstrdup (val);
ae7453f2
AD
156 }
157 else
158 {
159 /* Grow the current value. */
160 char *new_val;
ae7453f2 161 obstack_sgrow (&muscle_obstack, entry->value);
eb095650 162 free (entry->storage);
ae7453f2
AD
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);
eb095650 167 entry->value = entry->storage = xstrdup (new_val);
ae7453f2
AD
168 obstack_free (&muscle_obstack, new_val);
169 }
170}
171
172
cd3684cf
AD
173/*------------------------------------------------------------------.
174| Append VALUE to the current value of KEY, using muscle_grow. But |
175| in addition, issue a synchronization line for the location LOC. |
176`------------------------------------------------------------------*/
177
178void
179muscle_code_grow (const char *key, const char *val, location loc)
180{
181 char *extension = NULL;
05ac60f3 182 obstack_fgrow1 (&muscle_obstack, "]b4_syncline(%d, [[", loc.start.line);
cd3684cf
AD
183 MUSCLE_OBSTACK_SGROW (&muscle_obstack,
184 quotearg_style (c_quoting_style, loc.start.file));
185 obstack_sgrow (&muscle_obstack, "]])[\n");
186 obstack_sgrow (&muscle_obstack, val);
187 obstack_1grow (&muscle_obstack, 0);
188 extension = obstack_finish (&muscle_obstack);
189 muscle_grow (key, extension, "");
eb095650 190 obstack_free (&muscle_obstack, extension);
cd3684cf
AD
191}
192
193
ae7453f2
AD
194void muscle_pair_list_grow (const char *muscle,
195 const char *a1, const char *a2)
196{
66d30cd4 197 char *pair;
7ecec4dd
JD
198 obstack_sgrow (&muscle_obstack, "[[[");
199 MUSCLE_OBSTACK_SGROW (&muscle_obstack, a1);
200 obstack_sgrow (&muscle_obstack, "]], [[");
201 MUSCLE_OBSTACK_SGROW (&muscle_obstack, a2);
202 obstack_sgrow (&muscle_obstack, "]]]");
ae7453f2 203 obstack_1grow (&muscle_obstack, 0);
66d30cd4
AD
204 pair = obstack_finish (&muscle_obstack);
205 muscle_grow (muscle, pair, ",\n");
206 obstack_free (&muscle_obstack, pair);
ae7453f2
AD
207}
208
7eb8a0bc
JD
209
210/*----------------------------------------------------------------------------.
211| Find the value of muscle KEY. Unlike MUSCLE_FIND, this is always reliable |
212| to determine whether KEY has a value. |
213`----------------------------------------------------------------------------*/
214
215char const *
216muscle_find_const (char const *key)
217{
218 muscle_entry probe;
219 muscle_entry *result = NULL;
220
221 probe.key = key;
222 result = hash_lookup (muscle_table, &probe);
223 if (result)
224 return result->value;
225 return NULL;
226}
227
228
4502eadc
JD
229/*----------------------------------------------------------------------------.
230| Find the value of muscle KEY. Abort if muscle_insert was invoked more |
231| recently than muscle_grow for KEY since muscle_find can't return a |
232| char const *. |
233`----------------------------------------------------------------------------*/
ae7453f2 234
ff5150d9 235char *
7eb8a0bc 236muscle_find (char const *key)
f753cd62 237{
8322e8f5
PE
238 muscle_entry probe;
239 muscle_entry *result = NULL;
e9bca3ad 240
ae7453f2
AD
241 probe.key = key;
242 result = hash_lookup (muscle_table, &probe);
4502eadc
JD
243 if (result)
244 {
245 aver (result->value == result->storage);
246 return result->storage;
247 }
248 return NULL;
f753cd62 249}
be2a1a68
AD
250
251
ae7453f2
AD
252/*------------------------------------------------.
253| Output the definition of ENTRY as a m4_define. |
254`------------------------------------------------*/
be2a1a68 255
e00b6826 256static inline bool
8322e8f5 257muscle_m4_output (muscle_entry *entry, FILE *out)
be2a1a68
AD
258{
259 fprintf (out, "m4_define([b4_%s],\n", entry->key);
ae7453f2 260 fprintf (out, "[[%s]])\n\n\n", entry->value);
e00b6826
PE
261 return true;
262}
263
264static bool
265muscle_m4_output_processor (void *entry, void *out)
266{
267 return muscle_m4_output (entry, out);
be2a1a68
AD
268}
269
270
ae7453f2
AD
271/*----------------------------------------------------------------.
272| Output the definition of all the current muscles into a list of |
273| m4_defines. |
274`----------------------------------------------------------------*/
be2a1a68
AD
275
276void
277muscles_m4_output (FILE *out)
278{
e00b6826 279 hash_do_for_each (muscle_table, muscle_m4_output_processor, out);
be2a1a68 280}
3fc65ead
JD
281
282void
283muscle_boundary_grow (char const *key, boundary bound)
284{
285 char *extension;
286 MUSCLE_OBSTACK_SGROW (&muscle_obstack, bound.file);
287 obstack_1grow (&muscle_obstack, ':');
288 obstack_fgrow1 (&muscle_obstack, "%d", bound.line);
289 obstack_1grow (&muscle_obstack, '.');
290 obstack_fgrow1 (&muscle_obstack, "%d", bound.column);
291 obstack_1grow (&muscle_obstack, '\0');
292 extension = obstack_finish (&muscle_obstack);
293 muscle_grow (key, extension, "");
294 obstack_free (&muscle_obstack, extension);
295}
7eb8a0bc
JD
296
297void
6afc30cc 298muscle_grow_user_name_list (char const *key, char const *user_name,
7eb8a0bc
JD
299 location loc)
300{
301 muscle_grow (key, "[[[[", ",");
6afc30cc 302 muscle_grow (key, user_name, "");
7eb8a0bc
JD
303 muscle_grow (key, "]], [[", "");
304 muscle_boundary_grow (key, loc.start);
305 muscle_grow (key, "]], [[", "");
306 muscle_boundary_grow (key, loc.end);
307 muscle_grow (key, "]]]]", "");
308}