]> git.saurik.com Git - bison.git/blame - src/symlist.c
* NEWS (2.3a+): Mention yesterday's state-removal change.
[bison.git] / src / symlist.c
CommitLineData
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
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
0fb669f9
PE
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
56c47203 21
2cec9080 22#include <config.h>
56c47203 23#include "system.h"
17ee7397 24
9280d3ef 25#include "complain.h"
56c47203
AD
26#include "symlist.h"
27
28
17ee7397
PE
29/*--------------------------------------.
30| Create a list containing SYM at LOC. |
31`--------------------------------------*/
56c47203 32
17ee7397 33symbol_list *
3be03b13 34symbol_list_sym_new (symbol *sym, location loc)
56c47203 35{
da2a7671 36 symbol_list *res = xmalloc (sizeof *res);
affac613 37
3be03b13
JD
38 res->content_type = SYMLIST_SYMBOL;
39 res->content.sym = sym;
17ee7397 40 res->location = loc;
affac613 41
6ec2c0f2 42 res->midrule = NULL;
ffa4ba3a
JD
43 res->midrule_parent_rule = NULL;
44 res->midrule_parent_rhs_index = 0;
84866159 45
f6857bbf 46 code_props_none_init (&res->action_props);
affac613 47
56c47203 48 res->ruleprec = NULL;
676385e2
PH
49 res->dprec = 0;
50 res->merger = 0;
affac613
AD
51
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
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 = loc;
70 res->next = NULL;
71
72 return res;
73}
74
75
12e35840
JD
76/*----------------------------------------.
77| Create a list containing a <*> at LOC. |
78`----------------------------------------*/
3be03b13
JD
79
80symbol_list *
12e35840 81symbol_list_default_tagged_new (location loc)
3be03b13
JD
82{
83 symbol_list *res = xmalloc (sizeof *res);
84
12e35840
JD
85 res->content_type = SYMLIST_DEFAULT_TAGGED;
86 res->location = loc;
87 res->next = NULL;
88
89 return res;
90}
91
92
3ebecc24
JD
93/*---------------------------------------.
94| Create a list containing a <> at LOC. |
95`---------------------------------------*/
12e35840
JD
96
97symbol_list *
98symbol_list_default_tagless_new (location loc)
99{
100 symbol_list *res = xmalloc (sizeof *res);
101
102 res->content_type = SYMLIST_DEFAULT_TAGLESS;
3be03b13
JD
103 res->location = loc;
104 res->next = NULL;
105
106 return res;
107}
108
109
110/*-----------------------------------------------------------------------.
111| Print this list, for which every content_type must be SYMLIST_SYMBOL. |
112`-----------------------------------------------------------------------*/
867a3e00
AD
113
114void
3be03b13 115symbol_list_syms_print (const symbol_list *l, FILE *f)
867a3e00 116{
3be03b13 117 for (/* Nothing. */; l && l->content.sym; l = l->next)
22dda0f0 118 {
3be03b13 119 symbol_print (l->content.sym, f);
f6857bbf 120 fprintf (stderr, l->action_props.is_value_used ? " used" : " unused");
3be03b13 121 if (l && l->content.sym)
affac613 122 fprintf (f, ", ");
22dda0f0 123 }
867a3e00
AD
124}
125
126
3be03b13
JD
127/*---------------------------.
128| Prepend NODE to the LIST. |
129`---------------------------*/
56c47203 130
17ee7397 131symbol_list *
3be03b13 132symbol_list_prepend (symbol_list *list, symbol_list *node)
56c47203 133{
3be03b13
JD
134 node->next = list;
135 return node;
56c47203
AD
136}
137
138
3be03b13
JD
139/*-----------------------------------------------.
140| Free the LIST, but not the items it contains. |
141`-----------------------------------------------*/
56c47203
AD
142
143void
17ee7397 144symbol_list_free (symbol_list *list)
56c47203 145{
17ee7397 146 LIST_FREE (symbol_list, list);
56c47203
AD
147}
148
149
dafdc66f
AD
150/*--------------------.
151| Return its length. |
152`--------------------*/
153
b37acfe1
PE
154int
155symbol_list_length (symbol_list const *l)
dafdc66f
AD
156{
157 int res = 0;
3be03b13
JD
158 for (/* Nothing. */;
159 l && !(l->content_type == SYMLIST_SYMBOL && l->content.sym == NULL);
160 l = l->next)
dafdc66f
AD
161 ++res;
162 return res;
163}
164
165
3be03b13
JD
166/*------------------------------.
167| Get item N in symbol list L. |
168`------------------------------*/
affac613
AD
169
170symbol_list *
171symbol_list_n_get (symbol_list *l, int n)
172{
173 int i;
174
175 if (n < 0)
176 return NULL;
177
178 for (i = 0; i < n; ++i)
179 {
180 l = l->next;
3be03b13
JD
181 if (l == NULL
182 || (l->content_type == SYMLIST_SYMBOL && l->content.sym == NULL))
affac613
AD
183 return NULL;
184 }
185
186 return l;
187}
188
189
56c47203
AD
190/*--------------------------------------------------------------.
191| Get the data type (alternative in the union) of the value for |
affac613 192| symbol N in symbol list L. |
56c47203
AD
193`--------------------------------------------------------------*/
194
17ee7397 195uniqstr
affac613 196symbol_list_n_type_name_get (symbol_list *l, location loc, int n)
56c47203 197{
affac613
AD
198 l = symbol_list_n_get (l, n);
199 if (!l)
56c47203 200 {
867a3e00 201 complain_at (loc, _("invalid $ value: $%d"), n);
56c47203
AD
202 return NULL;
203 }
4f82b42a 204 aver (l->content_type == SYMLIST_SYMBOL);
3be03b13 205 return l->content.sym->type_name;
affac613 206}
56c47203 207
56c47203 208
3be03b13 209void
95021767 210symbol_list_destructor_set (symbol_list *node, char const *code, location loc)
3be03b13 211{
7c0c6181
JD
212 code_props destructor;
213 code_props_symbol_action_init (&destructor, code, loc);
214 code_props_translate_code (&destructor);
3be03b13
JD
215 switch (node->content_type)
216 {
217 case SYMLIST_SYMBOL:
95021767 218 symbol_destructor_set (node->content.sym, &destructor);
3be03b13
JD
219 break;
220 case SYMLIST_TYPE:
b2a0b7ca 221 semantic_type_destructor_set (
95021767 222 semantic_type_get (node->content.type_name), &destructor);
3be03b13 223 break;
12e35840 224 case SYMLIST_DEFAULT_TAGGED:
95021767 225 default_tagged_destructor_set (&destructor);
12e35840
JD
226 break;
227 case SYMLIST_DEFAULT_TAGLESS:
95021767 228 default_tagless_destructor_set (&destructor);
3be03b13
JD
229 break;
230 }
231}
232
233void
7c0c6181 234symbol_list_printer_set (symbol_list *node, char const *code, location loc)
3be03b13 235{
7c0c6181
JD
236 code_props printer;
237 code_props_symbol_action_init (&printer, code, loc);
238 code_props_translate_code (&printer);
3be03b13
JD
239 switch (node->content_type)
240 {
241 case SYMLIST_SYMBOL:
95021767 242 symbol_printer_set (node->content.sym, &printer);
3be03b13
JD
243 break;
244 case SYMLIST_TYPE:
b2a0b7ca 245 semantic_type_printer_set (
95021767 246 semantic_type_get (node->content.type_name), &printer);
3be03b13 247 break;
12e35840 248 case SYMLIST_DEFAULT_TAGGED:
95021767 249 default_tagged_printer_set (&printer);
12e35840
JD
250 break;
251 case SYMLIST_DEFAULT_TAGLESS:
95021767 252 default_tagless_printer_set (&printer);
3be03b13
JD
253 break;
254 }
255}