]> git.saurik.com Git - bison.git/blame_incremental - src/symlist.c
Style changes and factoring.
[bison.git] / src / symlist.c
... / ...
CommitLineData
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 This program 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 3 of the License, or
10 (at your option) any later version.
11
12 This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#include <config.h>
21#include "system.h"
22
23#include "complain.h"
24#include "symlist.h"
25
26
27/*--------------------------------------.
28| Create a list containing SYM at LOC. |
29`--------------------------------------*/
30
31symbol_list *
32symbol_list_sym_new (symbol *sym, location loc)
33{
34 symbol_list *res = xmalloc (sizeof *res);
35
36 res->content_type = SYMLIST_SYMBOL;
37 res->content.sym = sym;
38 res->location = res->sym_loc = loc;
39
40 res->midrule = NULL;
41 res->midrule_parent_rule = NULL;
42 res->midrule_parent_rhs_index = 0;
43
44 code_props_none_init (&res->action_props);
45
46 res->ruleprec = NULL;
47 res->dprec = 0;
48 res->merger = 0;
49
50 res->named_ref = NULL;
51
52 res->next = NULL;
53
54 return res;
55}
56
57
58/*--------------------------------------------.
59| Create a list containing TYPE_NAME at LOC. |
60`--------------------------------------------*/
61
62symbol_list *
63symbol_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;
69 res->location = res->sym_loc = loc;
70 res->named_ref = NULL;
71 res->next = NULL;
72
73 return res;
74}
75
76
77/*----------------------------------------.
78| Create a list containing a <*> at LOC. |
79`----------------------------------------*/
80
81symbol_list *
82symbol_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 = res->sym_loc = loc;
88 res->named_ref = NULL;
89 res->next = NULL;
90
91 return res;
92}
93
94
95/*---------------------------------------.
96| Create a list containing a <> at LOC. |
97`---------------------------------------*/
98
99symbol_list *
100symbol_list_default_tagless_new (location loc)
101{
102 symbol_list *res = xmalloc (sizeof *res);
103
104 res->content_type = SYMLIST_DEFAULT_TAGLESS;
105 res->location = res->sym_loc = loc;
106 res->named_ref = NULL;
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`-----------------------------------------------------------------------*/
116
117void
118symbol_list_syms_print (const symbol_list *l, FILE *f)
119{
120 for (/* Nothing. */; l && l->content.sym; l = l->next)
121 {
122 symbol_print (l->content.sym, f);
123 fprintf (stderr, l->action_props.is_value_used ? " used" : " unused");
124 if (l && l->content.sym)
125 fprintf (f, ", ");
126 }
127}
128
129
130/*---------------------------.
131| Prepend NODE to the LIST. |
132`---------------------------*/
133
134symbol_list *
135symbol_list_prepend (symbol_list *list, symbol_list *node)
136{
137 node->next = list;
138 return node;
139}
140
141
142/*-----------------------------------------------.
143| Free the LIST, but not the items it contains. |
144`-----------------------------------------------*/
145
146void
147symbol_list_free (symbol_list *list)
148{
149 LIST_FREE (symbol_list, list);
150}
151
152
153/*--------------------.
154| Return its length. |
155`--------------------*/
156
157int
158symbol_list_length (symbol_list const *l)
159{
160 int res = 0;
161 for (/* Nothing. */;
162 l && !(l->content_type == SYMLIST_SYMBOL && l->content.sym == NULL);
163 l = l->next)
164 ++res;
165 return res;
166}
167
168
169/*------------------------------.
170| Get item N in symbol list L. |
171`------------------------------*/
172
173symbol_list *
174symbol_list_n_get (symbol_list *l, int n)
175{
176 int i;
177
178 if (n < 0)
179 return NULL;
180
181 for (i = 0; i < n; ++i)
182 {
183 l = l->next;
184 if (l == NULL
185 || (l->content_type == SYMLIST_SYMBOL && l->content.sym == NULL))
186 return NULL;
187 }
188
189 return l;
190}
191
192
193/*--------------------------------------------------------------.
194| Get the data type (alternative in the union) of the value for |
195| symbol N in symbol list L. |
196`--------------------------------------------------------------*/
197
198uniqstr
199symbol_list_n_type_name_get (symbol_list *l, location loc, int n)
200{
201 l = symbol_list_n_get (l, n);
202 if (!l)
203 {
204 complain_at (loc, _("invalid $ value: $%d"), n);
205 return NULL;
206 }
207 aver (l->content_type == SYMLIST_SYMBOL);
208 return l->content.sym->type_name;
209}
210
211bool
212symbol_list_null (symbol_list *node)
213{
214 return !node ||
215 (node->content_type == SYMLIST_SYMBOL && !(node->content.sym));
216}
217
218void
219symbol_list_destructor_set (symbol_list *node, char const *code, location loc)
220{
221 code_props destructor;
222 code_props_symbol_action_init (&destructor, code, loc);
223 code_props_translate_code (&destructor);
224 switch (node->content_type)
225 {
226 case SYMLIST_SYMBOL:
227 symbol_destructor_set (node->content.sym, &destructor);
228 break;
229 case SYMLIST_TYPE:
230 semantic_type_destructor_set (
231 semantic_type_get (node->content.type_name), &destructor);
232 break;
233 case SYMLIST_DEFAULT_TAGGED:
234 default_tagged_destructor_set (&destructor);
235 break;
236 case SYMLIST_DEFAULT_TAGLESS:
237 default_tagless_destructor_set (&destructor);
238 break;
239 }
240}
241
242void
243symbol_list_printer_set (symbol_list *node, char const *code, location loc)
244{
245 code_props printer;
246 code_props_symbol_action_init (&printer, code, loc);
247 code_props_translate_code (&printer);
248 switch (node->content_type)
249 {
250 case SYMLIST_SYMBOL:
251 symbol_printer_set (node->content.sym, &printer);
252 break;
253 case SYMLIST_TYPE:
254 semantic_type_printer_set (
255 semantic_type_get (node->content.type_name), &printer);
256 break;
257 case SYMLIST_DEFAULT_TAGGED:
258 default_tagged_printer_set (&printer);
259 break;
260 case SYMLIST_DEFAULT_TAGLESS:
261 default_tagless_printer_set (&printer);
262 break;
263 }
264}