]> git.saurik.com Git - bison.git/blame - src/symlist.c
Remove last commit at the request of Paul Eggert.
[bison.git] / src / symlist.c
CommitLineData
56c47203 1/* Lists of symbols for Bison
17ee7397 2
84866159 3 Copyright (C) 2002, 2005, 2006 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
b0f4c4ea
JD
46 res->action = NULL;
47 res->used = false;
affac613 48
56c47203 49 res->ruleprec = NULL;
676385e2
PH
50 res->dprec = 0;
51 res->merger = 0;
affac613
AD
52
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;
70 res->location = loc;
71 res->next = NULL;
72
73 return res;
74}
75
76
12e35840
JD
77/*----------------------------------------.
78| Create a list containing a <*> at LOC. |
79`----------------------------------------*/
3be03b13
JD
80
81symbol_list *
12e35840 82symbol_list_default_tagged_new (location loc)
3be03b13
JD
83{
84 symbol_list *res = xmalloc (sizeof *res);
85
12e35840
JD
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
98symbol_list *
99symbol_list_default_tagless_new (location loc)
100{
101 symbol_list *res = xmalloc (sizeof *res);
102
103 res->content_type = SYMLIST_DEFAULT_TAGLESS;
3be03b13
JD
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`-----------------------------------------------------------------------*/
867a3e00
AD
114
115void
3be03b13 116symbol_list_syms_print (const symbol_list *l, FILE *f)
867a3e00 117{
3be03b13 118 for (/* Nothing. */; l && l->content.sym; l = l->next)
22dda0f0 119 {
3be03b13 120 symbol_print (l->content.sym, f);
b0f4c4ea 121 fprintf (stderr, l->used ? " used" : " unused");
3be03b13 122 if (l && l->content.sym)
affac613 123 fprintf (f, ", ");
22dda0f0 124 }
867a3e00
AD
125}
126
127
3be03b13
JD
128/*---------------------------.
129| Prepend NODE to the LIST. |
130`---------------------------*/
56c47203 131
17ee7397 132symbol_list *
3be03b13 133symbol_list_prepend (symbol_list *list, symbol_list *node)
56c47203 134{
3be03b13
JD
135 node->next = list;
136 return node;
56c47203
AD
137}
138
139
3be03b13
JD
140/*-----------------------------------------------.
141| Free the LIST, but not the items it contains. |
142`-----------------------------------------------*/
56c47203
AD
143
144void
17ee7397 145symbol_list_free (symbol_list *list)
56c47203 146{
17ee7397 147 LIST_FREE (symbol_list, list);
56c47203
AD
148}
149
150
dafdc66f
AD
151/*--------------------.
152| Return its length. |
153`--------------------*/
154
b37acfe1
PE
155int
156symbol_list_length (symbol_list const *l)
dafdc66f
AD
157{
158 int res = 0;
3be03b13
JD
159 for (/* Nothing. */;
160 l && !(l->content_type == SYMLIST_SYMBOL && l->content.sym == NULL);
161 l = l->next)
dafdc66f
AD
162 ++res;
163 return res;
164}
165
166
3be03b13
JD
167/*------------------------------.
168| Get item N in symbol list L. |
169`------------------------------*/
affac613
AD
170
171symbol_list *
172symbol_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;
3be03b13
JD
182 if (l == NULL
183 || (l->content_type == SYMLIST_SYMBOL && l->content.sym == NULL))
affac613
AD
184 return NULL;
185 }
186
187 return l;
188}
189
190
56c47203
AD
191/*--------------------------------------------------------------.
192| Get the data type (alternative in the union) of the value for |
affac613 193| symbol N in symbol list L. |
56c47203
AD
194`--------------------------------------------------------------*/
195
17ee7397 196uniqstr
affac613 197symbol_list_n_type_name_get (symbol_list *l, location loc, int n)
56c47203 198{
affac613
AD
199 l = symbol_list_n_get (l, n);
200 if (!l)
56c47203 201 {
867a3e00 202 complain_at (loc, _("invalid $ value: $%d"), n);
56c47203
AD
203 return NULL;
204 }
4f82b42a 205 aver (l->content_type == SYMLIST_SYMBOL);
3be03b13 206 return l->content.sym->type_name;
affac613 207}
56c47203 208
56c47203 209
b0f4c4ea
JD
210/*--------------------------------------.
211| The item N in symbol list L is USED. |
212`--------------------------------------*/
213
214void
215symbol_list_n_used_set (symbol_list *l, int n, bool used)
216{
217 l = symbol_list_n_get (l, n);
218 if (l)
219 l->used = used;
220}
221
3be03b13 222void
b0f4c4ea
JD
223symbol_list_destructor_set (symbol_list *node, const char *destructor,
224 location loc)
3be03b13
JD
225{
226 switch (node->content_type)
227 {
228 case SYMLIST_SYMBOL:
b0f4c4ea 229 symbol_destructor_set (node->content.sym, destructor, loc);
3be03b13
JD
230 break;
231 case SYMLIST_TYPE:
b2a0b7ca 232 semantic_type_destructor_set (
b0f4c4ea 233 semantic_type_get (node->content.type_name), destructor, loc);
3be03b13 234 break;
12e35840 235 case SYMLIST_DEFAULT_TAGGED:
b0f4c4ea 236 default_tagged_destructor_set (destructor, loc);
12e35840
JD
237 break;
238 case SYMLIST_DEFAULT_TAGLESS:
b0f4c4ea 239 default_tagless_destructor_set (destructor, loc);
3be03b13
JD
240 break;
241 }
242}
243
244void
b0f4c4ea 245symbol_list_printer_set (symbol_list *node, const char *printer, location loc)
3be03b13
JD
246{
247 switch (node->content_type)
248 {
249 case SYMLIST_SYMBOL:
b0f4c4ea 250 symbol_printer_set (node->content.sym, printer, loc);
3be03b13
JD
251 break;
252 case SYMLIST_TYPE:
b2a0b7ca 253 semantic_type_printer_set (
b0f4c4ea 254 semantic_type_get (node->content.type_name), printer, loc);
3be03b13 255 break;
12e35840 256 case SYMLIST_DEFAULT_TAGGED:
b0f4c4ea 257 default_tagged_printer_set (printer, loc);
12e35840
JD
258 break;
259 case SYMLIST_DEFAULT_TAGLESS:
b0f4c4ea 260 default_tagless_printer_set (printer, loc);
3be03b13
JD
261 break;
262 }
263}