1 /* Muscle table manager for Bison.
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 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. */
30 #include "muscle_tab.h"
33 /* A key-value pair, along with storage that can be reclaimed when
34 this pair is no longer needed. */
42 /* An obstack used to create some entries. */
43 struct obstack muscle_obstack
;
45 /* Initial capacity of muscles hash table. */
46 #define HT_INITIAL_CAPACITY 257
48 static struct hash_table
*muscle_table
= NULL
;
51 hash_compare_muscles (void const *x
, void const *y
)
53 muscle_entry
const *m1
= x
;
54 muscle_entry
const *m2
= y
;
55 return strcmp (m1
->key
, m2
->key
) == 0;
59 hash_muscle (const void *x
, size_t tablesize
)
61 muscle_entry
const *m
= x
;
62 return hash_string (m
->key
, tablesize
);
65 /*-----------------------------------------------------------------.
66 | Create the MUSCLE_TABLE, and initialize it with default values. |
67 | Also set up the MUSCLE_OBSTACK. |
68 `-----------------------------------------------------------------*/
71 muscle_entry_free (void *entry
)
73 muscle_entry
*mentry
= entry
;
74 free (mentry
->storage
);
81 /* Initialize the muscle obstack. */
82 obstack_init (&muscle_obstack
);
84 muscle_table
= hash_initialize (HT_INITIAL_CAPACITY
, NULL
, hash_muscle
,
85 hash_compare_muscles
, muscle_entry_free
);
87 /* Version and input file. */
88 MUSCLE_INSERT_STRING ("version", VERSION
);
89 MUSCLE_INSERT_C_STRING ("file_name", grammar_file
);
93 /*------------------------------------------------------------.
94 | Free all the memory consumed by the muscle machinery only. |
95 `------------------------------------------------------------*/
100 hash_free (muscle_table
);
101 obstack_free (&muscle_obstack
, NULL
);
106 /*------------------------------------------------------------.
107 | Insert (KEY, VALUE). If KEY already existed, overwrite the |
109 `------------------------------------------------------------*/
112 muscle_insert (char const *key
, char const *value
)
118 entry
= hash_lookup (muscle_table
, &probe
);
122 /* First insertion in the hash. */
123 entry
= xmalloc (sizeof *entry
);
125 hash_insert (muscle_table
, entry
);
128 free (entry
->storage
);
129 entry
->value
= value
;
130 entry
->storage
= NULL
;
134 /*-------------------------------------------------------------------.
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. |
138 `-------------------------------------------------------------------*/
141 muscle_grow (const char *key
, const char *val
, const char *separator
)
144 muscle_entry
*entry
= NULL
;
147 entry
= hash_lookup (muscle_table
, &probe
);
151 /* First insertion in the hash. */
152 entry
= xmalloc (sizeof *entry
);
154 hash_insert (muscle_table
, entry
);
155 entry
->value
= entry
->storage
= xstrdup (val
);
159 /* Grow the current value. */
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
);
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 `------------------------------------------------------------------*/
179 muscle_code_grow (const char *key
, const char *val
, location loc
)
181 char *extension
= NULL
;
182 obstack_fgrow1 (&muscle_obstack
, "]b4_syncline(%d, [[", loc
.start
.line
);
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
, "");
190 obstack_free (&muscle_obstack
, extension
);
194 void muscle_pair_list_grow (const char *muscle
,
195 const char *a1
, const char *a2
)
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
, "]]]");
203 obstack_1grow (&muscle_obstack
, 0);
204 pair
= obstack_finish (&muscle_obstack
);
205 muscle_grow (muscle
, pair
, ",\n");
206 obstack_free (&muscle_obstack
, pair
);
210 /*----------------------------------------------------------------------------.
211 | Find the value of muscle KEY. Unlike MUSCLE_FIND, this is always reliable |
212 | to determine whether KEY has a value. |
213 `----------------------------------------------------------------------------*/
216 muscle_find_const (char const *key
)
219 muscle_entry
*result
= NULL
;
222 result
= hash_lookup (muscle_table
, &probe
);
224 return result
->value
;
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 |
233 `----------------------------------------------------------------------------*/
236 muscle_find (char const *key
)
239 muscle_entry
*result
= NULL
;
242 result
= hash_lookup (muscle_table
, &probe
);
245 aver (result
->value
== result
->storage
);
246 return result
->storage
;
252 /*------------------------------------------------.
253 | Output the definition of ENTRY as a m4_define. |
254 `------------------------------------------------*/
257 muscle_m4_output (muscle_entry
*entry
, FILE *out
)
259 fprintf (out
, "m4_define([b4_%s],\n", entry
->key
);
260 fprintf (out
, "[[%s]])\n\n\n", entry
->value
);
265 muscle_m4_output_processor (void *entry
, void *out
)
267 return muscle_m4_output (entry
, out
);
271 /*----------------------------------------------------------------.
272 | Output the definition of all the current muscles into a list of |
274 `----------------------------------------------------------------*/
277 muscles_m4_output (FILE *out
)
279 hash_do_for_each (muscle_table
, muscle_m4_output_processor
, out
);
283 muscle_boundary_grow (char const *key
, boundary bound
)
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
);
298 muscle_grow_user_name_list (char const *key
, char const *user_name
,
301 muscle_grow (key
, "[[[[", ",");
302 muscle_grow (key
, user_name
, "");
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
, "]]]]", "");