]>
Commit | Line | Data |
---|---|---|
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" | |
4e956a7d | 23 | #include "quotearg.h" |
f753cd62 MA |
24 | #include "files.h" |
25 | #include "muscle_tab.h" | |
f508cb0a | 26 | #include "getargs.h" |
f753cd62 | 27 | |
592e8d4d AD |
28 | |
29 | /* An obstack used to create some entries. */ | |
30 | struct obstack muscle_obstack; | |
31 | ||
beda758b AD |
32 | /* Initial capacity of muscles hash table. */ |
33 | #define HT_INITIAL_CAPACITY 257 | |
f753cd62 | 34 | |
beda758b | 35 | struct hash_table *muscle_table = NULL; |
f753cd62 | 36 | |
beda758b AD |
37 | static bool |
38 | hash_compare_muscles (void const *x, void const *y) | |
f753cd62 | 39 | { |
beda758b AD |
40 | const muscle_entry_t *m1 = x; |
41 | const muscle_entry_t *m2 = y; | |
5dd5fd4a | 42 | return strcmp (m1->key, m2->key) == 0; |
f753cd62 MA |
43 | } |
44 | ||
beda758b AD |
45 | static unsigned int |
46 | hash_muscle (const void *x, unsigned int tablesize) | |
f753cd62 | 47 | { |
beda758b AD |
48 | const muscle_entry_t *m = x; |
49 | return hash_string (m->key, tablesize); | |
f753cd62 MA |
50 | } |
51 | ||
592e8d4d AD |
52 | /*-----------------------------------------------------------------. |
53 | | Create the MUSCLE_TABLE, and initialize it with default values. | | |
54 | | Also set up the MUSCLE_OBSTACK. | | |
55 | `-----------------------------------------------------------------*/ | |
56 | ||
f753cd62 MA |
57 | void |
58 | muscle_init (void) | |
59 | { | |
ae7453f2 AD |
60 | /* Initialize the muscle obstack. */ |
61 | obstack_init (&muscle_obstack); | |
62 | ||
beda758b | 63 | muscle_table = hash_initialize (HT_INITIAL_CAPACITY, NULL, hash_muscle, |
592e8d4d | 64 | hash_compare_muscles, free); |
f753cd62 MA |
65 | |
66 | /* Version and input file. */ | |
ae7453f2 | 67 | MUSCLE_INSERT_STRING ("version", VERSION); |
95612cfa | 68 | MUSCLE_INSERT_C_STRING ("filename", grammar_file); |
f753cd62 MA |
69 | } |
70 | ||
592e8d4d AD |
71 | |
72 | /*------------------------------------------------------------. | |
73 | | Free all the memory consumed by the muscle machinery only. | | |
74 | `------------------------------------------------------------*/ | |
75 | ||
76 | void | |
77 | muscle_free (void) | |
78 | { | |
79 | hash_free (muscle_table); | |
80 | obstack_free (&muscle_obstack, NULL); | |
81 | } | |
82 | ||
83 | ||
84 | ||
ae7453f2 AD |
85 | /*------------------------------------------------------------. |
86 | | Insert (KEY, VALUE). If KEY already existed, overwrite the | | |
87 | | previous value. | | |
88 | `------------------------------------------------------------*/ | |
89 | ||
a870c567 | 90 | void |
ae7453f2 | 91 | muscle_insert (const char *key, char *value) |
f753cd62 | 92 | { |
ae7453f2 | 93 | muscle_entry_t probe; |
e9bca3ad AD |
94 | muscle_entry_t *entry = NULL; |
95 | ||
ae7453f2 AD |
96 | probe.key = key; |
97 | entry = hash_lookup (muscle_table, &probe); | |
beda758b AD |
98 | |
99 | if (!entry) | |
100 | { | |
101 | /* First insertion in the hash. */ | |
102 | entry = XMALLOC (muscle_entry_t, 1); | |
103 | entry->key = key; | |
104 | hash_insert (muscle_table, entry); | |
105 | } | |
106 | entry->value = value; | |
f753cd62 MA |
107 | } |
108 | ||
ae7453f2 AD |
109 | |
110 | /*-------------------------------------------------------------------. | |
111 | | Insert (KEY, VALUE). If KEY already existed, overwrite the | | |
112 | | previous value. Uses MUSCLE_OBSTACK. De-allocates the previously | | |
113 | | associated value. VALUE and SEPARATOR are copied. | | |
114 | `-------------------------------------------------------------------*/ | |
115 | ||
116 | void | |
117 | muscle_grow (const char *key, const char *val, const char *separator) | |
118 | { | |
119 | muscle_entry_t probe; | |
120 | muscle_entry_t *entry = NULL; | |
121 | ||
122 | probe.key = key; | |
123 | entry = hash_lookup (muscle_table, &probe); | |
124 | ||
125 | if (!entry) | |
126 | { | |
127 | /* First insertion in the hash. */ | |
128 | entry = XMALLOC (muscle_entry_t, 1); | |
129 | entry->key = key; | |
130 | hash_insert (muscle_table, entry); | |
131 | entry->value = xstrdup (val); | |
132 | } | |
133 | else | |
134 | { | |
135 | /* Grow the current value. */ | |
136 | char *new_val; | |
ae7453f2 AD |
137 | obstack_sgrow (&muscle_obstack, entry->value); |
138 | free (entry->value); | |
139 | obstack_sgrow (&muscle_obstack, separator); | |
140 | obstack_sgrow (&muscle_obstack, val); | |
141 | obstack_1grow (&muscle_obstack, 0); | |
142 | new_val = obstack_finish (&muscle_obstack); | |
143 | entry->value = xstrdup (new_val); | |
ae7453f2 AD |
144 | obstack_free (&muscle_obstack, new_val); |
145 | } | |
146 | } | |
147 | ||
148 | ||
149 | /*-------------------------------------------------------------------. | |
150 | | MUSCLE is an M4 list of pairs. Create or extend it with the pair | | |
151 | | (A1, A2). Note that because the muscle values are output *double* | | |
152 | | quoted, one needs to strip the first level of quotes to reach the | | |
153 | | list itself. | | |
154 | `-------------------------------------------------------------------*/ | |
155 | ||
156 | void muscle_pair_list_grow (const char *muscle, | |
157 | const char *a1, const char *a2) | |
158 | { | |
66d30cd4 | 159 | char *pair; |
ae7453f2 AD |
160 | obstack_fgrow2 (&muscle_obstack, "[[[%s]], [[%s]]]", a1, a2); |
161 | obstack_1grow (&muscle_obstack, 0); | |
66d30cd4 AD |
162 | pair = obstack_finish (&muscle_obstack); |
163 | muscle_grow (muscle, pair, ",\n"); | |
164 | obstack_free (&muscle_obstack, pair); | |
ae7453f2 AD |
165 | } |
166 | ||
167 | /*-------------------------------. | |
168 | | Find the value of muscle KEY. | | |
169 | `-------------------------------*/ | |
170 | ||
171 | char* | |
f753cd62 MA |
172 | muscle_find (const char *key) |
173 | { | |
ae7453f2 | 174 | muscle_entry_t probe; |
e9bca3ad AD |
175 | muscle_entry_t *result = NULL; |
176 | ||
ae7453f2 AD |
177 | probe.key = key; |
178 | result = hash_lookup (muscle_table, &probe); | |
beda758b | 179 | return result ? result->value : NULL; |
f753cd62 | 180 | } |
be2a1a68 AD |
181 | |
182 | ||
ae7453f2 AD |
183 | /*------------------------------------------------. |
184 | | Output the definition of ENTRY as a m4_define. | | |
185 | `------------------------------------------------*/ | |
be2a1a68 | 186 | |
cfaee611 | 187 | static int |
be2a1a68 AD |
188 | muscle_m4_output (muscle_entry_t *entry, FILE *out) |
189 | { | |
190 | fprintf (out, "m4_define([b4_%s],\n", entry->key); | |
ae7453f2 | 191 | fprintf (out, "[[%s]])\n\n\n", entry->value); |
cfaee611 | 192 | return 1; |
be2a1a68 AD |
193 | } |
194 | ||
195 | ||
ae7453f2 AD |
196 | /*----------------------------------------------------------------. |
197 | | Output the definition of all the current muscles into a list of | | |
198 | | m4_defines. | | |
199 | `----------------------------------------------------------------*/ | |
be2a1a68 AD |
200 | |
201 | void | |
202 | muscles_m4_output (FILE *out) | |
203 | { | |
204 | hash_do_for_each (muscle_table, | |
205 | (Hash_processor) muscle_m4_output, | |
206 | out); | |
207 | } |