]>
Commit | Line | Data |
---|---|---|
1 | /* Muscle table manager for Bison. | |
2 | ||
3 | Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Free Software | |
4 | Foundation, Inc. | |
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 | |
20 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
21 | Boston, MA 02110-1301, USA. */ | |
22 | ||
23 | #include <config.h> | |
24 | #include "system.h" | |
25 | ||
26 | #include <hash.h> | |
27 | #include <quotearg.h> | |
28 | ||
29 | #include "files.h" | |
30 | #include "muscle_tab.h" | |
31 | #include "getargs.h" | |
32 | ||
33 | /* A key-value pair, along with storage that can be reclaimed when | |
34 | this pair is no longer needed. */ | |
35 | typedef struct | |
36 | { | |
37 | char const *key; | |
38 | char const *value; | |
39 | char *storage; | |
40 | } muscle_entry; | |
41 | ||
42 | /* An obstack used to create some entries. */ | |
43 | struct obstack muscle_obstack; | |
44 | ||
45 | /* Initial capacity of muscles hash table. */ | |
46 | #define HT_INITIAL_CAPACITY 257 | |
47 | ||
48 | static struct hash_table *muscle_table = NULL; | |
49 | ||
50 | static bool | |
51 | hash_compare_muscles (void const *x, void const *y) | |
52 | { | |
53 | muscle_entry const *m1 = x; | |
54 | muscle_entry const *m2 = y; | |
55 | return strcmp (m1->key, m2->key) == 0; | |
56 | } | |
57 | ||
58 | static size_t | |
59 | hash_muscle (const void *x, size_t tablesize) | |
60 | { | |
61 | muscle_entry const *m = x; | |
62 | return hash_string (m->key, tablesize); | |
63 | } | |
64 | ||
65 | /*-----------------------------------------------------------------. | |
66 | | Create the MUSCLE_TABLE, and initialize it with default values. | | |
67 | | Also set up the MUSCLE_OBSTACK. | | |
68 | `-----------------------------------------------------------------*/ | |
69 | ||
70 | static void | |
71 | muscle_entry_free (void *entry) | |
72 | { | |
73 | muscle_entry *mentry = entry; | |
74 | free (mentry->storage); | |
75 | free (mentry); | |
76 | } | |
77 | ||
78 | void | |
79 | muscle_init (void) | |
80 | { | |
81 | /* Initialize the muscle obstack. */ | |
82 | obstack_init (&muscle_obstack); | |
83 | ||
84 | muscle_table = hash_initialize (HT_INITIAL_CAPACITY, NULL, hash_muscle, | |
85 | hash_compare_muscles, muscle_entry_free); | |
86 | ||
87 | /* Version and input file. */ | |
88 | MUSCLE_INSERT_STRING ("version", VERSION); | |
89 | MUSCLE_INSERT_C_STRING ("file_name", grammar_file); | |
90 | } | |
91 | ||
92 | ||
93 | /*------------------------------------------------------------. | |
94 | | Free all the memory consumed by the muscle machinery only. | | |
95 | `------------------------------------------------------------*/ | |
96 | ||
97 | void | |
98 | muscle_free (void) | |
99 | { | |
100 | hash_free (muscle_table); | |
101 | obstack_free (&muscle_obstack, NULL); | |
102 | } | |
103 | ||
104 | ||
105 | ||
106 | /*------------------------------------------------------------. | |
107 | | Insert (KEY, VALUE). If KEY already existed, overwrite the | | |
108 | | previous value. | | |
109 | `------------------------------------------------------------*/ | |
110 | ||
111 | void | |
112 | muscle_insert (char const *key, char const *value) | |
113 | { | |
114 | muscle_entry probe; | |
115 | muscle_entry *entry; | |
116 | ||
117 | probe.key = key; | |
118 | entry = hash_lookup (muscle_table, &probe); | |
119 | ||
120 | if (!entry) | |
121 | { | |
122 | /* First insertion in the hash. */ | |
123 | entry = xmalloc (sizeof *entry); | |
124 | entry->key = key; | |
125 | hash_insert (muscle_table, entry); | |
126 | } | |
127 | else | |
128 | free (entry->storage); | |
129 | entry->value = value; | |
130 | entry->storage = NULL; | |
131 | } | |
132 | ||
133 | ||
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 | `-------------------------------------------------------------------*/ | |
139 | ||
140 | void | |
141 | muscle_grow (const char *key, const char *val, const char *separator) | |
142 | { | |
143 | muscle_entry probe; | |
144 | muscle_entry *entry = NULL; | |
145 | ||
146 | probe.key = key; | |
147 | entry = hash_lookup (muscle_table, &probe); | |
148 | ||
149 | if (!entry) | |
150 | { | |
151 | /* First insertion in the hash. */ | |
152 | entry = xmalloc (sizeof *entry); | |
153 | entry->key = key; | |
154 | hash_insert (muscle_table, entry); | |
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 | /*------------------------------------------------------------------. | |
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 | ||
178 | void | |
179 | muscle_code_grow (const char *key, const char *val, location loc) | |
180 | { | |
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); | |
191 | } | |
192 | ||
193 | ||
194 | /*-------------------------------------------------------------------. | |
195 | | MUSCLE is an M4 list of pairs. Create or extend it with the pair | | |
196 | | (A1, A2). Note that because the muscle values are output *double* | | |
197 | | quoted, one needs to strip the first level of quotes to reach the | | |
198 | | list itself. | | |
199 | `-------------------------------------------------------------------*/ | |
200 | ||
201 | void muscle_pair_list_grow (const char *muscle, | |
202 | const char *a1, const char *a2) | |
203 | { | |
204 | char *pair; | |
205 | obstack_fgrow2 (&muscle_obstack, "[[[%s]], [[%s]]]", a1, a2); | |
206 | obstack_1grow (&muscle_obstack, 0); | |
207 | pair = obstack_finish (&muscle_obstack); | |
208 | muscle_grow (muscle, pair, ",\n"); | |
209 | obstack_free (&muscle_obstack, pair); | |
210 | } | |
211 | ||
212 | /*----------------------------------------------------------------------------. | |
213 | | Find the value of muscle KEY. Abort if muscle_insert was invoked more | | |
214 | | recently than muscle_grow for KEY since muscle_find can't return a | | |
215 | | char const *. | | |
216 | `----------------------------------------------------------------------------*/ | |
217 | ||
218 | char * | |
219 | muscle_find (const char *key) | |
220 | { | |
221 | muscle_entry probe; | |
222 | muscle_entry *result = NULL; | |
223 | ||
224 | probe.key = key; | |
225 | result = hash_lookup (muscle_table, &probe); | |
226 | if (result) | |
227 | { | |
228 | aver (result->value == result->storage); | |
229 | return result->storage; | |
230 | } | |
231 | return NULL; | |
232 | } | |
233 | ||
234 | ||
235 | /*------------------------------------------------. | |
236 | | Output the definition of ENTRY as a m4_define. | | |
237 | `------------------------------------------------*/ | |
238 | ||
239 | static inline bool | |
240 | muscle_m4_output (muscle_entry *entry, FILE *out) | |
241 | { | |
242 | fprintf (out, "m4_define([b4_%s],\n", entry->key); | |
243 | fprintf (out, "[[%s]])\n\n\n", entry->value); | |
244 | return true; | |
245 | } | |
246 | ||
247 | static bool | |
248 | muscle_m4_output_processor (void *entry, void *out) | |
249 | { | |
250 | return muscle_m4_output (entry, out); | |
251 | } | |
252 | ||
253 | ||
254 | /*----------------------------------------------------------------. | |
255 | | Output the definition of all the current muscles into a list of | | |
256 | | m4_defines. | | |
257 | `----------------------------------------------------------------*/ | |
258 | ||
259 | void | |
260 | muscles_m4_output (FILE *out) | |
261 | { | |
262 | hash_do_for_each (muscle_table, muscle_m4_output_processor, out); | |
263 | } |