]>
Commit | Line | Data |
---|---|---|
1 | /* Lists of symbols for Bison | |
2 | ||
3 | Copyright (C) 2002, 2005, 2006, 2007 Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of Bison, the GNU Compiler Compiler. | |
6 | ||
7 | Bison is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
11 | ||
12 | Bison is distributed in the hope that it will be useful, | |
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 | |
18 | along with Bison; see the file COPYING. If not, write to | |
19 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
20 | Boston, MA 02110-1301, USA. */ | |
21 | ||
22 | #include <config.h> | |
23 | #include "system.h" | |
24 | ||
25 | #include "complain.h" | |
26 | #include "symlist.h" | |
27 | ||
28 | ||
29 | /*--------------------------------------. | |
30 | | Create a list containing SYM at LOC. | | |
31 | `--------------------------------------*/ | |
32 | ||
33 | symbol_list * | |
34 | symbol_list_sym_new (symbol *sym, location loc) | |
35 | { | |
36 | symbol_list *res = xmalloc (sizeof *res); | |
37 | ||
38 | res->content_type = SYMLIST_SYMBOL; | |
39 | res->content.sym = sym; | |
40 | res->location = loc; | |
41 | ||
42 | res->midrule = NULL; | |
43 | res->midrule_parent_rule = NULL; | |
44 | res->midrule_parent_rhs_index = 0; | |
45 | ||
46 | res->action = NULL; | |
47 | res->used = false; | |
48 | ||
49 | res->ruleprec = NULL; | |
50 | res->dprec = 0; | |
51 | res->merger = 0; | |
52 | ||
53 | res->next = NULL; | |
54 | ||
55 | return res; | |
56 | } | |
57 | ||
58 | ||
59 | /*--------------------------------------------. | |
60 | | Create a list containing TYPE_NAME at LOC. | | |
61 | `--------------------------------------------*/ | |
62 | ||
63 | symbol_list * | |
64 | symbol_list_type_new (uniqstr type_name, location loc) | |
65 | { | |
66 | symbol_list *res = xmalloc (sizeof *res); | |
67 | ||
68 | res->content_type = SYMLIST_TYPE; | |
69 | res->content.type_name = type_name; | |
70 | res->location = loc; | |
71 | res->next = NULL; | |
72 | ||
73 | return res; | |
74 | } | |
75 | ||
76 | ||
77 | /*----------------------------------------. | |
78 | | Create a list containing a <*> at LOC. | | |
79 | `----------------------------------------*/ | |
80 | ||
81 | symbol_list * | |
82 | symbol_list_default_tagged_new (location loc) | |
83 | { | |
84 | symbol_list *res = xmalloc (sizeof *res); | |
85 | ||
86 | res->content_type = SYMLIST_DEFAULT_TAGGED; | |
87 | res->location = loc; | |
88 | res->next = NULL; | |
89 | ||
90 | return res; | |
91 | } | |
92 | ||
93 | ||
94 | /*---------------------------------------. | |
95 | | Create a list containing a <> at LOC. | | |
96 | `---------------------------------------*/ | |
97 | ||
98 | symbol_list * | |
99 | symbol_list_default_tagless_new (location loc) | |
100 | { | |
101 | symbol_list *res = xmalloc (sizeof *res); | |
102 | ||
103 | res->content_type = SYMLIST_DEFAULT_TAGLESS; | |
104 | res->location = loc; | |
105 | res->next = NULL; | |
106 | ||
107 | return res; | |
108 | } | |
109 | ||
110 | ||
111 | /*-----------------------------------------------------------------------. | |
112 | | Print this list, for which every content_type must be SYMLIST_SYMBOL. | | |
113 | `-----------------------------------------------------------------------*/ | |
114 | ||
115 | void | |
116 | symbol_list_syms_print (const symbol_list *l, FILE *f) | |
117 | { | |
118 | for (/* Nothing. */; l && l->content.sym; l = l->next) | |
119 | { | |
120 | symbol_print (l->content.sym, f); | |
121 | fprintf (stderr, l->used ? " used" : " unused"); | |
122 | if (l && l->content.sym) | |
123 | fprintf (f, ", "); | |
124 | } | |
125 | } | |
126 | ||
127 | ||
128 | /*---------------------------. | |
129 | | Prepend NODE to the LIST. | | |
130 | `---------------------------*/ | |
131 | ||
132 | symbol_list * | |
133 | symbol_list_prepend (symbol_list *list, symbol_list *node) | |
134 | { | |
135 | node->next = list; | |
136 | return node; | |
137 | } | |
138 | ||
139 | ||
140 | /*-----------------------------------------------. | |
141 | | Free the LIST, but not the items it contains. | | |
142 | `-----------------------------------------------*/ | |
143 | ||
144 | void | |
145 | symbol_list_free (symbol_list *list) | |
146 | { | |
147 | LIST_FREE (symbol_list, list); | |
148 | } | |
149 | ||
150 | ||
151 | /*--------------------. | |
152 | | Return its length. | | |
153 | `--------------------*/ | |
154 | ||
155 | int | |
156 | symbol_list_length (symbol_list const *l) | |
157 | { | |
158 | int res = 0; | |
159 | for (/* Nothing. */; | |
160 | l && !(l->content_type == SYMLIST_SYMBOL && l->content.sym == NULL); | |
161 | l = l->next) | |
162 | ++res; | |
163 | return res; | |
164 | } | |
165 | ||
166 | ||
167 | /*------------------------------. | |
168 | | Get item N in symbol list L. | | |
169 | `------------------------------*/ | |
170 | ||
171 | symbol_list * | |
172 | symbol_list_n_get (symbol_list *l, int n) | |
173 | { | |
174 | int i; | |
175 | ||
176 | if (n < 0) | |
177 | return NULL; | |
178 | ||
179 | for (i = 0; i < n; ++i) | |
180 | { | |
181 | l = l->next; | |
182 | if (l == NULL | |
183 | || (l->content_type == SYMLIST_SYMBOL && l->content.sym == NULL)) | |
184 | return NULL; | |
185 | } | |
186 | ||
187 | return l; | |
188 | } | |
189 | ||
190 | ||
191 | /*--------------------------------------------------------------. | |
192 | | Get the data type (alternative in the union) of the value for | | |
193 | | symbol N in symbol list L. | | |
194 | `--------------------------------------------------------------*/ | |
195 | ||
196 | uniqstr | |
197 | symbol_list_n_type_name_get (symbol_list *l, location loc, int n) | |
198 | { | |
199 | l = symbol_list_n_get (l, n); | |
200 | if (!l) | |
201 | { | |
202 | complain_at (loc, _("invalid $ value: $%d"), n); | |
203 | return NULL; | |
204 | } | |
205 | aver (l->content_type == SYMLIST_SYMBOL); | |
206 | return l->content.sym->type_name; | |
207 | } | |
208 | ||
209 | ||
210 | void | |
211 | symbol_list_destructor_set (symbol_list *node, const char *destructor, | |
212 | location loc) | |
213 | { | |
214 | switch (node->content_type) | |
215 | { | |
216 | case SYMLIST_SYMBOL: | |
217 | symbol_destructor_set (node->content.sym, destructor, loc); | |
218 | break; | |
219 | case SYMLIST_TYPE: | |
220 | semantic_type_destructor_set ( | |
221 | semantic_type_get (node->content.type_name), destructor, loc); | |
222 | break; | |
223 | case SYMLIST_DEFAULT_TAGGED: | |
224 | default_tagged_destructor_set (destructor, loc); | |
225 | break; | |
226 | case SYMLIST_DEFAULT_TAGLESS: | |
227 | default_tagless_destructor_set (destructor, loc); | |
228 | break; | |
229 | } | |
230 | } | |
231 | ||
232 | void | |
233 | symbol_list_printer_set (symbol_list *node, const char *printer, location loc) | |
234 | { | |
235 | switch (node->content_type) | |
236 | { | |
237 | case SYMLIST_SYMBOL: | |
238 | symbol_printer_set (node->content.sym, printer, loc); | |
239 | break; | |
240 | case SYMLIST_TYPE: | |
241 | semantic_type_printer_set ( | |
242 | semantic_type_get (node->content.type_name), printer, loc); | |
243 | break; | |
244 | case SYMLIST_DEFAULT_TAGGED: | |
245 | default_tagged_printer_set (printer, loc); | |
246 | break; | |
247 | case SYMLIST_DEFAULT_TAGLESS: | |
248 | default_tagless_printer_set (printer, loc); | |
249 | break; | |
250 | } | |
251 | } |