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