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