]>
Commit | Line | Data |
---|---|---|
56c47203 | 1 | /* Lists of symbols for Bison |
17ee7397 | 2 | |
28e52c0d | 3 | Copyright (C) 2002, 2005, 2006, 2007 Free Software Foundation, Inc. |
56c47203 AD |
4 | |
5 | This file is part of Bison, the GNU Compiler Compiler. | |
6 | ||
f16b0819 | 7 | This program is free software: you can redistribute it and/or modify |
56c47203 | 8 | it under the terms of the GNU General Public License as published by |
f16b0819 PE |
9 | the Free Software Foundation, either version 3 of the License, or |
10 | (at your option) any later version. | |
56c47203 | 11 | |
f16b0819 | 12 | This program is distributed in the hope that it will be useful, |
56c47203 AD |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
f16b0819 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
56c47203 | 19 | |
2cec9080 | 20 | #include <config.h> |
56c47203 | 21 | #include "system.h" |
17ee7397 | 22 | |
9280d3ef | 23 | #include "complain.h" |
56c47203 AD |
24 | #include "symlist.h" |
25 | ||
26 | ||
17ee7397 PE |
27 | /*--------------------------------------. |
28 | | Create a list containing SYM at LOC. | | |
29 | `--------------------------------------*/ | |
56c47203 | 30 | |
17ee7397 | 31 | symbol_list * |
3be03b13 | 32 | symbol_list_sym_new (symbol *sym, location loc) |
56c47203 | 33 | { |
da2a7671 | 34 | symbol_list *res = xmalloc (sizeof *res); |
affac613 | 35 | |
3be03b13 JD |
36 | res->content_type = SYMLIST_SYMBOL; |
37 | res->content.sym = sym; | |
b9f1d9a4 | 38 | res->location = res->sym_loc = loc; |
affac613 | 39 | |
6ec2c0f2 | 40 | res->midrule = NULL; |
ffa4ba3a JD |
41 | res->midrule_parent_rule = NULL; |
42 | res->midrule_parent_rhs_index = 0; | |
84866159 | 43 | |
f6857bbf | 44 | code_props_none_init (&res->action_props); |
affac613 | 45 | |
56c47203 | 46 | res->ruleprec = NULL; |
676385e2 PH |
47 | res->dprec = 0; |
48 | res->merger = 0; | |
affac613 | 49 | |
b9f1d9a4 AR |
50 | res->named_ref = NULL; |
51 | ||
affac613 AD |
52 | res->next = NULL; |
53 | ||
56c47203 AD |
54 | return res; |
55 | } | |
56 | ||
57 | ||
3be03b13 JD |
58 | /*--------------------------------------------. |
59 | | Create a list containing TYPE_NAME at LOC. | | |
60 | `--------------------------------------------*/ | |
61 | ||
62 | symbol_list * | |
63 | symbol_list_type_new (uniqstr type_name, location loc) | |
64 | { | |
65 | symbol_list *res = xmalloc (sizeof *res); | |
66 | ||
67 | res->content_type = SYMLIST_TYPE; | |
68 | res->content.type_name = type_name; | |
b9f1d9a4 AR |
69 | res->location = res->sym_loc = loc; |
70 | res->named_ref = NULL; | |
3be03b13 JD |
71 | res->next = NULL; |
72 | ||
73 | return res; | |
74 | } | |
75 | ||
76 | ||
12e35840 JD |
77 | /*----------------------------------------. |
78 | | Create a list containing a <*> at LOC. | | |
79 | `----------------------------------------*/ | |
3be03b13 JD |
80 | |
81 | symbol_list * | |
12e35840 | 82 | symbol_list_default_tagged_new (location loc) |
3be03b13 JD |
83 | { |
84 | symbol_list *res = xmalloc (sizeof *res); | |
85 | ||
12e35840 | 86 | res->content_type = SYMLIST_DEFAULT_TAGGED; |
b9f1d9a4 AR |
87 | res->location = res->sym_loc = loc; |
88 | res->named_ref = NULL; | |
12e35840 JD |
89 | res->next = NULL; |
90 | ||
91 | return res; | |
92 | } | |
93 | ||
94 | ||
3ebecc24 JD |
95 | /*---------------------------------------. |
96 | | Create a list containing a <> at LOC. | | |
97 | `---------------------------------------*/ | |
12e35840 JD |
98 | |
99 | symbol_list * | |
100 | symbol_list_default_tagless_new (location loc) | |
101 | { | |
102 | symbol_list *res = xmalloc (sizeof *res); | |
103 | ||
104 | res->content_type = SYMLIST_DEFAULT_TAGLESS; | |
b9f1d9a4 AR |
105 | res->location = res->sym_loc = loc; |
106 | res->named_ref = NULL; | |
3be03b13 JD |
107 | res->next = NULL; |
108 | ||
109 | return res; | |
110 | } | |
111 | ||
112 | ||
113 | /*-----------------------------------------------------------------------. | |
114 | | Print this list, for which every content_type must be SYMLIST_SYMBOL. | | |
115 | `-----------------------------------------------------------------------*/ | |
867a3e00 AD |
116 | |
117 | void | |
3be03b13 | 118 | symbol_list_syms_print (const symbol_list *l, FILE *f) |
867a3e00 | 119 | { |
3be03b13 | 120 | for (/* Nothing. */; l && l->content.sym; l = l->next) |
22dda0f0 | 121 | { |
3be03b13 | 122 | symbol_print (l->content.sym, f); |
f6857bbf | 123 | fprintf (stderr, l->action_props.is_value_used ? " used" : " unused"); |
3be03b13 | 124 | if (l && l->content.sym) |
affac613 | 125 | fprintf (f, ", "); |
22dda0f0 | 126 | } |
867a3e00 AD |
127 | } |
128 | ||
129 | ||
3be03b13 JD |
130 | /*---------------------------. |
131 | | Prepend NODE to the LIST. | | |
132 | `---------------------------*/ | |
56c47203 | 133 | |
17ee7397 | 134 | symbol_list * |
3be03b13 | 135 | symbol_list_prepend (symbol_list *list, symbol_list *node) |
56c47203 | 136 | { |
3be03b13 JD |
137 | node->next = list; |
138 | return node; | |
56c47203 AD |
139 | } |
140 | ||
141 | ||
3be03b13 JD |
142 | /*-----------------------------------------------. |
143 | | Free the LIST, but not the items it contains. | | |
144 | `-----------------------------------------------*/ | |
56c47203 AD |
145 | |
146 | void | |
17ee7397 | 147 | symbol_list_free (symbol_list *list) |
56c47203 | 148 | { |
13cdf208 AR |
149 | symbol_list *node, *next; |
150 | for (node = list; node; node = next) | |
151 | { | |
152 | next = node->next; | |
153 | if (node->named_ref) | |
154 | named_ref_free (node->named_ref); | |
155 | free (node); | |
156 | } | |
56c47203 AD |
157 | } |
158 | ||
159 | ||
dafdc66f AD |
160 | /*--------------------. |
161 | | Return its length. | | |
162 | `--------------------*/ | |
163 | ||
b37acfe1 PE |
164 | int |
165 | symbol_list_length (symbol_list const *l) | |
dafdc66f AD |
166 | { |
167 | int res = 0; | |
3be03b13 JD |
168 | for (/* Nothing. */; |
169 | l && !(l->content_type == SYMLIST_SYMBOL && l->content.sym == NULL); | |
170 | l = l->next) | |
dafdc66f AD |
171 | ++res; |
172 | return res; | |
173 | } | |
174 | ||
175 | ||
3be03b13 JD |
176 | /*------------------------------. |
177 | | Get item N in symbol list L. | | |
178 | `------------------------------*/ | |
affac613 AD |
179 | |
180 | symbol_list * | |
181 | symbol_list_n_get (symbol_list *l, int n) | |
182 | { | |
183 | int i; | |
184 | ||
185 | if (n < 0) | |
186 | return NULL; | |
187 | ||
188 | for (i = 0; i < n; ++i) | |
189 | { | |
190 | l = l->next; | |
3be03b13 JD |
191 | if (l == NULL |
192 | || (l->content_type == SYMLIST_SYMBOL && l->content.sym == NULL)) | |
affac613 AD |
193 | return NULL; |
194 | } | |
195 | ||
196 | return l; | |
197 | } | |
198 | ||
199 | ||
56c47203 AD |
200 | /*--------------------------------------------------------------. |
201 | | Get the data type (alternative in the union) of the value for | | |
affac613 | 202 | | symbol N in symbol list L. | |
56c47203 AD |
203 | `--------------------------------------------------------------*/ |
204 | ||
17ee7397 | 205 | uniqstr |
affac613 | 206 | symbol_list_n_type_name_get (symbol_list *l, location loc, int n) |
56c47203 | 207 | { |
affac613 AD |
208 | l = symbol_list_n_get (l, n); |
209 | if (!l) | |
56c47203 | 210 | { |
867a3e00 | 211 | complain_at (loc, _("invalid $ value: $%d"), n); |
56c47203 AD |
212 | return NULL; |
213 | } | |
4f82b42a | 214 | aver (l->content_type == SYMLIST_SYMBOL); |
3be03b13 | 215 | return l->content.sym->type_name; |
affac613 | 216 | } |
56c47203 | 217 | |
872b52bc AR |
218 | bool |
219 | symbol_list_null (symbol_list *node) | |
220 | { | |
221 | return !node || | |
222 | (node->content_type == SYMLIST_SYMBOL && !(node->content.sym)); | |
223 | } | |
56c47203 | 224 | |
3be03b13 | 225 | void |
95021767 | 226 | symbol_list_destructor_set (symbol_list *node, char const *code, location loc) |
3be03b13 | 227 | { |
7c0c6181 JD |
228 | code_props destructor; |
229 | code_props_symbol_action_init (&destructor, code, loc); | |
230 | code_props_translate_code (&destructor); | |
3be03b13 JD |
231 | switch (node->content_type) |
232 | { | |
233 | case SYMLIST_SYMBOL: | |
95021767 | 234 | symbol_destructor_set (node->content.sym, &destructor); |
3be03b13 JD |
235 | break; |
236 | case SYMLIST_TYPE: | |
b2a0b7ca | 237 | semantic_type_destructor_set ( |
95021767 | 238 | semantic_type_get (node->content.type_name), &destructor); |
3be03b13 | 239 | break; |
12e35840 | 240 | case SYMLIST_DEFAULT_TAGGED: |
95021767 | 241 | default_tagged_destructor_set (&destructor); |
12e35840 JD |
242 | break; |
243 | case SYMLIST_DEFAULT_TAGLESS: | |
95021767 | 244 | default_tagless_destructor_set (&destructor); |
3be03b13 JD |
245 | break; |
246 | } | |
247 | } | |
248 | ||
249 | void | |
7c0c6181 | 250 | symbol_list_printer_set (symbol_list *node, char const *code, location loc) |
3be03b13 | 251 | { |
7c0c6181 JD |
252 | code_props printer; |
253 | code_props_symbol_action_init (&printer, code, loc); | |
254 | code_props_translate_code (&printer); | |
3be03b13 JD |
255 | switch (node->content_type) |
256 | { | |
257 | case SYMLIST_SYMBOL: | |
95021767 | 258 | symbol_printer_set (node->content.sym, &printer); |
3be03b13 JD |
259 | break; |
260 | case SYMLIST_TYPE: | |
b2a0b7ca | 261 | semantic_type_printer_set ( |
95021767 | 262 | semantic_type_get (node->content.type_name), &printer); |
3be03b13 | 263 | break; |
12e35840 | 264 | case SYMLIST_DEFAULT_TAGGED: |
95021767 | 265 | default_tagged_printer_set (&printer); |
12e35840 JD |
266 | break; |
267 | case SYMLIST_DEFAULT_TAGLESS: | |
95021767 | 268 | default_tagless_printer_set (&printer); |
3be03b13 JD |
269 | break; |
270 | } | |
271 | } |