]> git.saurik.com Git - bison.git/blob - src/symlist.c
1fa981ec2c6c55db15a8f8ed0b79dbcd83e95d36
[bison.git] / src / symlist.c
1 /* Lists of symbols for Bison
2
3 Copyright (C) 2002, 2005, 2006 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 "scan-code.h"
27 #include "symlist.h"
28
29
30 /*--------------------------------------.
31 | Create a list containing SYM at LOC. |
32 `--------------------------------------*/
33
34 symbol_list *
35 symbol_list_sym_new (symbol *sym, location loc)
36 {
37 symbol_list *res = xmalloc (sizeof *res);
38
39 res->content_type = SYMLIST_SYMBOL;
40 res->content.sym = sym;
41 res->location = loc;
42
43 res->midrule = NULL;
44 res->midrule_parent_rule = NULL;
45 res->midrule_parent_rhs_index = 0;
46
47 code_props_none_init (&res->action_props);
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,
122 code_props_is_value_used (l->action_props)
123 ? " used" : " unused");
124 if (l && l->content.sym)
125 fprintf (f, ", ");
126 }
127 }
128
129
130 /*---------------------------.
131 | Prepend NODE to the LIST. |
132 `---------------------------*/
133
134 symbol_list *
135 symbol_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
146 void
147 symbol_list_free (symbol_list *list)
148 {
149 LIST_FREE (symbol_list, list);
150 }
151
152
153 /*--------------------.
154 | Return its length. |
155 `--------------------*/
156
157 int
158 symbol_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
173 symbol_list *
174 symbol_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
198 uniqstr
199 symbol_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
211
212 void
213 symbol_list_destructor_set (symbol_list *node, const char *code, location loc)
214 {
215 code_props destructor;
216 code_props_symbol_action_init (&destructor, code, loc);
217 code_props_translate_code (&destructor);
218 switch (node->content_type)
219 {
220 case SYMLIST_SYMBOL:
221 symbol_destructor_set (node->content.sym, destructor);
222 break;
223 case SYMLIST_TYPE:
224 semantic_type_destructor_set (
225 semantic_type_get (node->content.type_name), destructor);
226 break;
227 case SYMLIST_DEFAULT_TAGGED:
228 default_tagged_destructor_set (destructor);
229 break;
230 case SYMLIST_DEFAULT_TAGLESS:
231 default_tagless_destructor_set (destructor);
232 break;
233 }
234 }
235
236 void
237 symbol_list_printer_set (symbol_list *node, const char *code, location loc)
238 {
239 code_props printer;
240 code_props_symbol_action_init (&printer, code, loc);
241 code_props_translate_code (&printer);
242 switch (node->content_type)
243 {
244 case SYMLIST_SYMBOL:
245 symbol_printer_set (node->content.sym, printer);
246 break;
247 case SYMLIST_TYPE:
248 semantic_type_printer_set (
249 semantic_type_get (node->content.type_name), printer);
250 break;
251 case SYMLIST_DEFAULT_TAGGED:
252 default_tagged_printer_set (printer);
253 break;
254 case SYMLIST_DEFAULT_TAGLESS:
255 default_tagless_printer_set (printer);
256 break;
257 }
258 }