]> git.saurik.com Git - bison.git/blame - src/muscle_tab.c
Sync.
[bison.git] / src / muscle_tab.c
CommitLineData
e00b6826
PE
1/* Muscle table manager for Bison.
2
05ac60f3
PE
3 Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software
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
f753cd62 23#include "system.h"
8322e8f5
PE
24
25#include <hash.h>
26#include <quotearg.h>
27
f753cd62
MA
28#include "files.h"
29#include "muscle_tab.h"
f508cb0a 30#include "getargs.h"
f753cd62 31
8322e8f5
PE
32typedef struct
33{
34 const char *key;
35 char *value;
36} muscle_entry;
592e8d4d
AD
37
38/* An obstack used to create some entries. */
39struct obstack muscle_obstack;
40
beda758b
AD
41/* Initial capacity of muscles hash table. */
42#define HT_INITIAL_CAPACITY 257
f753cd62 43
beda758b 44struct hash_table *muscle_table = NULL;
f753cd62 45
beda758b
AD
46static bool
47hash_compare_muscles (void const *x, void const *y)
f753cd62 48{
8322e8f5
PE
49 muscle_entry const *m1 = x;
50 muscle_entry const *m2 = y;
5dd5fd4a 51 return strcmp (m1->key, m2->key) == 0;
f753cd62
MA
52}
53
233a88ad
PE
54static size_t
55hash_muscle (const void *x, size_t tablesize)
f753cd62 56{
8322e8f5 57 muscle_entry const *m = x;
beda758b 58 return hash_string (m->key, tablesize);
f753cd62
MA
59}
60
592e8d4d
AD
61/*-----------------------------------------------------------------.
62| Create the MUSCLE_TABLE, and initialize it with default values. |
63| Also set up the MUSCLE_OBSTACK. |
64`-----------------------------------------------------------------*/
65
f753cd62
MA
66void
67muscle_init (void)
68{
ae7453f2
AD
69 /* Initialize the muscle obstack. */
70 obstack_init (&muscle_obstack);
71
beda758b 72 muscle_table = hash_initialize (HT_INITIAL_CAPACITY, NULL, hash_muscle,
592e8d4d 73 hash_compare_muscles, free);
f753cd62
MA
74
75 /* Version and input file. */
ae7453f2 76 MUSCLE_INSERT_STRING ("version", VERSION);
95612cfa 77 MUSCLE_INSERT_C_STRING ("filename", grammar_file);
f753cd62
MA
78}
79
592e8d4d
AD
80
81/*------------------------------------------------------------.
82| Free all the memory consumed by the muscle machinery only. |
83`------------------------------------------------------------*/
84
85void
86muscle_free (void)
87{
88 hash_free (muscle_table);
89 obstack_free (&muscle_obstack, NULL);
90}
91
92
93
ae7453f2
AD
94/*------------------------------------------------------------.
95| Insert (KEY, VALUE). If KEY already existed, overwrite the |
96| previous value. |
97`------------------------------------------------------------*/
98
a870c567 99void
ae7453f2 100muscle_insert (const char *key, char *value)
f753cd62 101{
8322e8f5 102 muscle_entry probe;
da2a7671 103 muscle_entry *entry;
e9bca3ad 104
ae7453f2
AD
105 probe.key = key;
106 entry = hash_lookup (muscle_table, &probe);
beda758b
AD
107
108 if (!entry)
109 {
110 /* First insertion in the hash. */
da2a7671 111 entry = xmalloc (sizeof *entry);
beda758b
AD
112 entry->key = key;
113 hash_insert (muscle_table, entry);
114 }
115 entry->value = value;
f753cd62
MA
116}
117
ae7453f2
AD
118
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`-------------------------------------------------------------------*/
124
125void
126muscle_grow (const char *key, const char *val, const char *separator)
127{
8322e8f5
PE
128 muscle_entry probe;
129 muscle_entry *entry = NULL;
ae7453f2
AD
130
131 probe.key = key;
132 entry = hash_lookup (muscle_table, &probe);
133
134 if (!entry)
135 {
136 /* First insertion in the hash. */
da2a7671 137 entry = xmalloc (sizeof *entry);
ae7453f2
AD
138 entry->key = key;
139 hash_insert (muscle_table, entry);
140 entry->value = xstrdup (val);
141 }
142 else
143 {
144 /* Grow the current value. */
145 char *new_val;
ae7453f2
AD
146 obstack_sgrow (&muscle_obstack, entry->value);
147 free (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);
ae7453f2
AD
153 obstack_free (&muscle_obstack, new_val);
154 }
155}
156
157
cd3684cf
AD
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`------------------------------------------------------------------*/
162
163void
164muscle_code_grow (const char *key, const char *val, location loc)
165{
166 char *extension = NULL;
05ac60f3 167 obstack_fgrow1 (&muscle_obstack, "]b4_syncline(%d, [[", loc.start.line);
cd3684cf
AD
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, "");
175}
176
177
ae7453f2
AD
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 |
182| list itself. |
183`-------------------------------------------------------------------*/
184
185void muscle_pair_list_grow (const char *muscle,
186 const char *a1, const char *a2)
187{
66d30cd4 188 char *pair;
ae7453f2
AD
189 obstack_fgrow2 (&muscle_obstack, "[[[%s]], [[%s]]]", a1, a2);
190 obstack_1grow (&muscle_obstack, 0);
66d30cd4
AD
191 pair = obstack_finish (&muscle_obstack);
192 muscle_grow (muscle, pair, ",\n");
193 obstack_free (&muscle_obstack, pair);
ae7453f2
AD
194}
195
196/*-------------------------------.
197| Find the value of muscle KEY. |
198`-------------------------------*/
199
200char*
f753cd62
MA
201muscle_find (const char *key)
202{
8322e8f5
PE
203 muscle_entry probe;
204 muscle_entry *result = NULL;
e9bca3ad 205
ae7453f2
AD
206 probe.key = key;
207 result = hash_lookup (muscle_table, &probe);
beda758b 208 return result ? result->value : NULL;
f753cd62 209}
be2a1a68
AD
210
211
ae7453f2
AD
212/*------------------------------------------------.
213| Output the definition of ENTRY as a m4_define. |
214`------------------------------------------------*/
be2a1a68 215
e00b6826 216static inline bool
8322e8f5 217muscle_m4_output (muscle_entry *entry, FILE *out)
be2a1a68
AD
218{
219 fprintf (out, "m4_define([b4_%s],\n", entry->key);
ae7453f2 220 fprintf (out, "[[%s]])\n\n\n", entry->value);
e00b6826
PE
221 return true;
222}
223
224static bool
225muscle_m4_output_processor (void *entry, void *out)
226{
227 return muscle_m4_output (entry, out);
be2a1a68
AD
228}
229
230
ae7453f2
AD
231/*----------------------------------------------------------------.
232| Output the definition of all the current muscles into a list of |
233| m4_defines. |
234`----------------------------------------------------------------*/
be2a1a68
AD
235
236void
237muscles_m4_output (FILE *out)
238{
e00b6826 239 hash_do_for_each (muscle_table, muscle_m4_output_processor, out);
be2a1a68 240}