1 /* Muscle table manager for Bison.
3 Copyright (C) 2001, 2002, 2003, 2004, 2005 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. */
29 #include "muscle_tab.h"
38 /* An obstack used to create some entries. */
39 struct obstack muscle_obstack
;
41 /* Initial capacity of muscles hash table. */
42 #define HT_INITIAL_CAPACITY 257
44 struct hash_table
*muscle_table
= NULL
;
47 hash_compare_muscles (void const *x
, void const *y
)
49 muscle_entry
const *m1
= x
;
50 muscle_entry
const *m2
= y
;
51 return strcmp (m1
->key
, m2
->key
) == 0;
55 hash_muscle (const void *x
, size_t tablesize
)
57 muscle_entry
const *m
= x
;
58 return hash_string (m
->key
, tablesize
);
61 /*-----------------------------------------------------------------.
62 | Create the MUSCLE_TABLE, and initialize it with default values. |
63 | Also set up the MUSCLE_OBSTACK. |
64 `-----------------------------------------------------------------*/
69 /* Initialize the muscle obstack. */
70 obstack_init (&muscle_obstack
);
72 muscle_table
= hash_initialize (HT_INITIAL_CAPACITY
, NULL
, hash_muscle
,
73 hash_compare_muscles
, free
);
75 /* Version and input file. */
76 MUSCLE_INSERT_STRING ("version", VERSION
);
77 MUSCLE_INSERT_C_STRING ("filename", grammar_file
);
81 /*------------------------------------------------------------.
82 | Free all the memory consumed by the muscle machinery only. |
83 `------------------------------------------------------------*/
88 hash_free (muscle_table
);
89 obstack_free (&muscle_obstack
, NULL
);
94 /*------------------------------------------------------------.
95 | Insert (KEY, VALUE). If KEY already existed, overwrite the |
97 `------------------------------------------------------------*/
100 muscle_insert (const char *key
, char *value
)
106 entry
= hash_lookup (muscle_table
, &probe
);
110 /* First insertion in the hash. */
111 entry
= xmalloc (sizeof *entry
);
113 hash_insert (muscle_table
, entry
);
115 entry
->value
= value
;
119 /*-------------------------------------------------------------------.
120 | Insert (KEY, VALUE). If KEY already existed, overwrite the |
121 | previous value. Uses MUSCLE_OBSTACK. De-allocates the previously |
122 | associated value. VALUE and SEPARATOR are copied. |
123 `-------------------------------------------------------------------*/
126 muscle_grow (const char *key
, const char *val
, const char *separator
)
129 muscle_entry
*entry
= NULL
;
132 entry
= hash_lookup (muscle_table
, &probe
);
136 /* First insertion in the hash. */
137 entry
= xmalloc (sizeof *entry
);
139 hash_insert (muscle_table
, entry
);
140 entry
->value
= xstrdup (val
);
144 /* Grow the current value. */
146 obstack_sgrow (&muscle_obstack
, entry
->value
);
148 obstack_sgrow (&muscle_obstack
, separator
);
149 obstack_sgrow (&muscle_obstack
, val
);
150 obstack_1grow (&muscle_obstack
, 0);
151 new_val
= obstack_finish (&muscle_obstack
);
152 entry
->value
= xstrdup (new_val
);
153 obstack_free (&muscle_obstack
, new_val
);
158 /*------------------------------------------------------------------.
159 | Append VALUE to the current value of KEY, using muscle_grow. But |
160 | in addition, issue a synchronization line for the location LOC. |
161 `------------------------------------------------------------------*/
164 muscle_code_grow (const char *key
, const char *val
, location loc
)
166 char *extension
= NULL
;
167 obstack_fgrow1 (&muscle_obstack
, "]b4_syncline(%d, [[", loc
.start
.line
);
168 MUSCLE_OBSTACK_SGROW (&muscle_obstack
,
169 quotearg_style (c_quoting_style
, loc
.start
.file
));
170 obstack_sgrow (&muscle_obstack
, "]])[\n");
171 obstack_sgrow (&muscle_obstack
, val
);
172 obstack_1grow (&muscle_obstack
, 0);
173 extension
= obstack_finish (&muscle_obstack
);
174 muscle_grow (key
, extension
, "");
178 /*-------------------------------------------------------------------.
179 | MUSCLE is an M4 list of pairs. Create or extend it with the pair |
180 | (A1, A2). Note that because the muscle values are output *double* |
181 | quoted, one needs to strip the first level of quotes to reach the |
183 `-------------------------------------------------------------------*/
185 void muscle_pair_list_grow (const char *muscle
,
186 const char *a1
, const char *a2
)
189 obstack_fgrow2 (&muscle_obstack
, "[[[%s]], [[%s]]]", a1
, a2
);
190 obstack_1grow (&muscle_obstack
, 0);
191 pair
= obstack_finish (&muscle_obstack
);
192 muscle_grow (muscle
, pair
, ",\n");
193 obstack_free (&muscle_obstack
, pair
);
196 /*-------------------------------.
197 | Find the value of muscle KEY. |
198 `-------------------------------*/
201 muscle_find (const char *key
)
204 muscle_entry
*result
= NULL
;
207 result
= hash_lookup (muscle_table
, &probe
);
208 return result
? result
->value
: NULL
;
212 /*------------------------------------------------.
213 | Output the definition of ENTRY as a m4_define. |
214 `------------------------------------------------*/
217 muscle_m4_output (muscle_entry
*entry
, FILE *out
)
219 fprintf (out
, "m4_define([b4_%s],\n", entry
->key
);
220 fprintf (out
, "[[%s]])\n\n\n", entry
->value
);
225 muscle_m4_output_processor (void *entry
, void *out
)
227 return muscle_m4_output (entry
, out
);
231 /*----------------------------------------------------------------.
232 | Output the definition of all the current muscles into a list of |
234 `----------------------------------------------------------------*/
237 muscles_m4_output (FILE *out
)
239 hash_do_for_each (muscle_table
, muscle_m4_output_processor
, out
);