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