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