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. */
30 #include "muscle_tab.h"
39 /* An obstack used to create some entries. */
40 struct obstack muscle_obstack
;
42 /* Initial capacity of muscles hash table. */
43 #define HT_INITIAL_CAPACITY 257
45 static struct hash_table
*muscle_table
= NULL
;
48 hash_compare_muscles (void const *x
, void const *y
)
50 muscle_entry
const *m1
= x
;
51 muscle_entry
const *m2
= y
;
52 return strcmp (m1
->key
, m2
->key
) == 0;
56 hash_muscle (const void *x
, size_t tablesize
)
58 muscle_entry
const *m
= x
;
59 return hash_string (m
->key
, tablesize
);
62 /*-----------------------------------------------------------------.
63 | Create the MUSCLE_TABLE, and initialize it with default values. |
64 | Also set up the MUSCLE_OBSTACK. |
65 `-----------------------------------------------------------------*/
70 /* Initialize the muscle obstack. */
71 obstack_init (&muscle_obstack
);
73 muscle_table
= hash_initialize (HT_INITIAL_CAPACITY
, NULL
, hash_muscle
,
74 hash_compare_muscles
, free
);
76 /* Version and input file. */
77 MUSCLE_INSERT_STRING ("version", VERSION
);
78 MUSCLE_INSERT_C_STRING ("file_name", grammar_file
);
82 /*------------------------------------------------------------.
83 | Free all the memory consumed by the muscle machinery only. |
84 `------------------------------------------------------------*/
89 hash_free (muscle_table
);
90 obstack_free (&muscle_obstack
, NULL
);
95 /*------------------------------------------------------------.
96 | Insert (KEY, VALUE). If KEY already existed, overwrite the |
98 `------------------------------------------------------------*/
101 muscle_insert (const char *key
, char *value
)
107 entry
= hash_lookup (muscle_table
, &probe
);
111 /* First insertion in the hash. */
112 entry
= xmalloc (sizeof *entry
);
114 hash_insert (muscle_table
, entry
);
116 entry
->value
= value
;
120 /*-------------------------------------------------------------------.
121 | Append VALUE to the current value of KEY. If KEY did not already |
122 | exist, create it. Use MUSCLE_OBSTACK. De-allocate the previously |
123 | associated value. Copy VALUE and SEPARATOR. |
124 `-------------------------------------------------------------------*/
127 muscle_grow (const char *key
, const char *val
, const char *separator
)
130 muscle_entry
*entry
= NULL
;
133 entry
= hash_lookup (muscle_table
, &probe
);
137 /* First insertion in the hash. */
138 entry
= xmalloc (sizeof *entry
);
140 hash_insert (muscle_table
, entry
);
141 entry
->value
= xstrdup (val
);
145 /* Grow the current value. */
147 obstack_sgrow (&muscle_obstack
, entry
->value
);
149 obstack_sgrow (&muscle_obstack
, separator
);
150 obstack_sgrow (&muscle_obstack
, val
);
151 obstack_1grow (&muscle_obstack
, 0);
152 new_val
= obstack_finish (&muscle_obstack
);
153 entry
->value
= xstrdup (new_val
);
154 obstack_free (&muscle_obstack
, new_val
);
159 /*------------------------------------------------------------------.
160 | Append VALUE to the current value of KEY, using muscle_grow. But |
161 | in addition, issue a synchronization line for the location LOC. |
162 `------------------------------------------------------------------*/
165 muscle_code_grow (const char *key
, const char *val
, location loc
)
167 char *extension
= NULL
;
168 obstack_fgrow1 (&muscle_obstack
, "]b4_syncline(%d, [[", loc
.start
.line
);
169 MUSCLE_OBSTACK_SGROW (&muscle_obstack
,
170 quotearg_style (c_quoting_style
, loc
.start
.file
));
171 obstack_sgrow (&muscle_obstack
, "]])[\n");
172 obstack_sgrow (&muscle_obstack
, val
);
173 obstack_1grow (&muscle_obstack
, 0);
174 extension
= obstack_finish (&muscle_obstack
);
175 muscle_grow (key
, extension
, "");
179 /*-------------------------------------------------------------------.
180 | MUSCLE is an M4 list of pairs. Create or extend it with the pair |
181 | (A1, A2). Note that because the muscle values are output *double* |
182 | quoted, one needs to strip the first level of quotes to reach the |
184 `-------------------------------------------------------------------*/
186 void muscle_pair_list_grow (const char *muscle
,
187 const char *a1
, const char *a2
)
190 obstack_fgrow2 (&muscle_obstack
, "[[[%s]], [[%s]]]", a1
, a2
);
191 obstack_1grow (&muscle_obstack
, 0);
192 pair
= obstack_finish (&muscle_obstack
);
193 muscle_grow (muscle
, pair
, ",\n");
194 obstack_free (&muscle_obstack
, pair
);
197 /*-------------------------------.
198 | Find the value of muscle KEY. |
199 `-------------------------------*/
202 muscle_find (const char *key
)
205 muscle_entry
*result
= NULL
;
208 result
= hash_lookup (muscle_table
, &probe
);
209 return result
? result
->value
: NULL
;
213 /*------------------------------------------------.
214 | Output the definition of ENTRY as a m4_define. |
215 `------------------------------------------------*/
218 muscle_m4_output (muscle_entry
*entry
, FILE *out
)
220 fprintf (out
, "m4_define([b4_%s],\n", entry
->key
);
221 fprintf (out
, "[[%s]])\n\n\n", entry
->value
);
226 muscle_m4_output_processor (void *entry
, void *out
)
228 return muscle_m4_output (entry
, out
);
232 /*----------------------------------------------------------------.
233 | Output the definition of all the current muscles into a list of |
235 `----------------------------------------------------------------*/
238 muscles_m4_output (FILE *out
)
240 hash_do_for_each (muscle_table
, muscle_m4_output_processor
, out
);