]> git.saurik.com Git - bison.git/blame - src/muscle_tab.c
Regen.
[bison.git] / src / muscle_tab.c
CommitLineData
ae7453f2 1/* Muscle table manager for Bison,
beda758b 2 Copyright (C) 2001, 2002 Free Software Foundation, Inc.
f753cd62
MA
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
6 Bison is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 Bison is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
f753cd62
MA
21#include "system.h"
22#include "hash.h"
23#include "files.h"
24#include "muscle_tab.h"
f508cb0a 25#include "getargs.h"
f753cd62 26
592e8d4d
AD
27
28/* An obstack used to create some entries. */
29struct obstack muscle_obstack;
30
beda758b
AD
31/* Initial capacity of muscles hash table. */
32#define HT_INITIAL_CAPACITY 257
f753cd62 33
beda758b 34struct hash_table *muscle_table = NULL;
f753cd62 35
beda758b
AD
36static bool
37hash_compare_muscles (void const *x, void const *y)
f753cd62 38{
beda758b
AD
39 const muscle_entry_t *m1 = x;
40 const muscle_entry_t *m2 = y;
5dd5fd4a 41 return strcmp (m1->key, m2->key) == 0;
f753cd62
MA
42}
43
beda758b
AD
44static unsigned int
45hash_muscle (const void *x, unsigned int tablesize)
f753cd62 46{
beda758b
AD
47 const muscle_entry_t *m = x;
48 return hash_string (m->key, tablesize);
f753cd62
MA
49}
50
592e8d4d
AD
51/*-----------------------------------------------------------------.
52| Create the MUSCLE_TABLE, and initialize it with default values. |
53| Also set up the MUSCLE_OBSTACK. |
54`-----------------------------------------------------------------*/
55
f753cd62
MA
56void
57muscle_init (void)
58{
ae7453f2
AD
59 /* Initialize the muscle obstack. */
60 obstack_init (&muscle_obstack);
61
beda758b 62 muscle_table = hash_initialize (HT_INITIAL_CAPACITY, NULL, hash_muscle,
592e8d4d 63 hash_compare_muscles, free);
f753cd62
MA
64
65 /* Version and input file. */
ae7453f2
AD
66 MUSCLE_INSERT_STRING ("version", VERSION);
67 MUSCLE_INSERT_STRING ("filename", infile);
f753cd62
MA
68}
69
592e8d4d
AD
70
71/*------------------------------------------------------------.
72| Free all the memory consumed by the muscle machinery only. |
73`------------------------------------------------------------*/
74
75void
76muscle_free (void)
77{
78 hash_free (muscle_table);
79 obstack_free (&muscle_obstack, NULL);
80}
81
82
83
ae7453f2
AD
84/*------------------------------------------------------------.
85| Insert (KEY, VALUE). If KEY already existed, overwrite the |
86| previous value. |
87`------------------------------------------------------------*/
88
a870c567 89void
ae7453f2 90muscle_insert (const char *key, char *value)
f753cd62 91{
ae7453f2 92 muscle_entry_t probe;
e9bca3ad
AD
93 muscle_entry_t *entry = NULL;
94
ae7453f2
AD
95 probe.key = key;
96 entry = hash_lookup (muscle_table, &probe);
beda758b
AD
97
98 if (!entry)
99 {
100 /* First insertion in the hash. */
101 entry = XMALLOC (muscle_entry_t, 1);
102 entry->key = key;
103 hash_insert (muscle_table, entry);
104 }
105 entry->value = value;
f753cd62
MA
106}
107
ae7453f2
AD
108
109/*-------------------------------------------------------------------.
110| Insert (KEY, VALUE). If KEY already existed, overwrite the |
111| previous value. Uses MUSCLE_OBSTACK. De-allocates the previously |
112| associated value. VALUE and SEPARATOR are copied. |
113`-------------------------------------------------------------------*/
114
115void
116muscle_grow (const char *key, const char *val, const char *separator)
117{
118 muscle_entry_t probe;
119 muscle_entry_t *entry = NULL;
120
121 probe.key = key;
122 entry = hash_lookup (muscle_table, &probe);
123
124 if (!entry)
125 {
126 /* First insertion in the hash. */
127 entry = XMALLOC (muscle_entry_t, 1);
128 entry->key = key;
129 hash_insert (muscle_table, entry);
130 entry->value = xstrdup (val);
131 }
132 else
133 {
134 /* Grow the current value. */
135 char *new_val;
ae7453f2
AD
136 obstack_sgrow (&muscle_obstack, entry->value);
137 free (entry->value);
138 obstack_sgrow (&muscle_obstack, separator);
139 obstack_sgrow (&muscle_obstack, val);
140 obstack_1grow (&muscle_obstack, 0);
141 new_val = obstack_finish (&muscle_obstack);
142 entry->value = xstrdup (new_val);
ae7453f2
AD
143 obstack_free (&muscle_obstack, new_val);
144 }
145}
146
147
148/*-------------------------------------------------------------------.
149| MUSCLE is an M4 list of pairs. Create or extend it with the pair |
150| (A1, A2). Note that because the muscle values are output *double* |
151| quoted, one needs to strip the first level of quotes to reach the |
152| list itself. |
153`-------------------------------------------------------------------*/
154
155void muscle_pair_list_grow (const char *muscle,
156 const char *a1, const char *a2)
157{
66d30cd4 158 char *pair;
ae7453f2
AD
159 obstack_fgrow2 (&muscle_obstack, "[[[%s]], [[%s]]]", a1, a2);
160 obstack_1grow (&muscle_obstack, 0);
66d30cd4
AD
161 pair = obstack_finish (&muscle_obstack);
162 muscle_grow (muscle, pair, ",\n");
163 obstack_free (&muscle_obstack, pair);
ae7453f2
AD
164}
165
166/*-------------------------------.
167| Find the value of muscle KEY. |
168`-------------------------------*/
169
170char*
f753cd62
MA
171muscle_find (const char *key)
172{
ae7453f2 173 muscle_entry_t probe;
e9bca3ad
AD
174 muscle_entry_t *result = NULL;
175
ae7453f2
AD
176 probe.key = key;
177 result = hash_lookup (muscle_table, &probe);
beda758b 178 return result ? result->value : NULL;
f753cd62 179}
be2a1a68
AD
180
181
ae7453f2
AD
182/*------------------------------------------------.
183| Output the definition of ENTRY as a m4_define. |
184`------------------------------------------------*/
be2a1a68 185
cfaee611 186static int
be2a1a68
AD
187muscle_m4_output (muscle_entry_t *entry, FILE *out)
188{
189 fprintf (out, "m4_define([b4_%s],\n", entry->key);
ae7453f2 190 fprintf (out, "[[%s]])\n\n\n", entry->value);
cfaee611 191 return 1;
be2a1a68
AD
192}
193
194
ae7453f2
AD
195/*----------------------------------------------------------------.
196| Output the definition of all the current muscles into a list of |
197| m4_defines. |
198`----------------------------------------------------------------*/
be2a1a68
AD
199
200void
201muscles_m4_output (FILE *out)
202{
203 hash_do_for_each (muscle_table,
204 (Hash_processor) muscle_m4_output,
205 out);
206}