]> git.saurik.com Git - bison.git/blob - src/symlist.c
muscle: factor the computation of variable names
[bison.git] / src / symlist.c
1 /* Lists of symbols for Bison
2
3 Copyright (C) 2002, 2005-2007, 2009-2013 Free Software Foundation,
4 Inc.
5
6 This file is part of Bison, the GNU Compiler Compiler.
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
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
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include <config.h>
22 #include "system.h"
23
24 #include "complain.h"
25 #include "symlist.h"
26
27 /*--------------------------------------.
28 | Create a list containing SYM at LOC. |
29 `--------------------------------------*/
30
31 symbol_list *
32 symbol_list_sym_new (symbol *sym, location loc)
33 {
34 symbol_list *res = xmalloc (sizeof *res);
35
36 res->content_type = SYMLIST_SYMBOL;
37 res->content.sym = sym;
38 res->location = res->sym_loc = loc;
39 res->named_ref = NULL;
40
41 res->midrule = NULL;
42 res->midrule_parent_rule = NULL;
43 res->midrule_parent_rhs_index = 0;
44
45 /* Members used for LHS only. */
46 res->ruleprec = NULL;
47 res->percent_empty_loc = empty_location;
48 code_props_none_init (&res->action_props);
49 res->dprec = 0;
50 res->dprec_location = empty_location;
51 res->merger = 0;
52 res->merger_declaration_location = empty_location;
53
54 res->next = NULL;
55
56 return res;
57 }
58
59
60 /*--------------------------------------------.
61 | Create a list containing TYPE_NAME at LOC. |
62 `--------------------------------------------*/
63
64 symbol_list *
65 symbol_list_type_new (uniqstr type_name, location loc)
66 {
67 symbol_list *res = xmalloc (sizeof *res);
68
69 res->content_type = SYMLIST_TYPE;
70 res->content.sem_type = xmalloc (sizeof (semantic_type));
71 res->content.sem_type->tag = type_name;
72 res->content.sem_type->location = loc;
73 res->content.sem_type->status = undeclared;
74
75 res->location = res->sym_loc = loc;
76 res->named_ref = NULL;
77 res->next = NULL;
78
79 return res;
80 }
81
82
83 /*-----------------------------------------------------------------------.
84 | Print this list, for which every content_type must be SYMLIST_SYMBOL. |
85 `-----------------------------------------------------------------------*/
86
87 void
88 symbol_list_syms_print (const symbol_list *l, FILE *f)
89 {
90 char const *sep = "";
91 for (/* Nothing. */; l && l->content.sym; l = l->next)
92 {
93 fputs (sep, f);
94 fputs (l->content_type == SYMLIST_SYMBOL ? "symbol: "
95 : l->content_type == SYMLIST_TYPE ? "type: "
96 : "invalid content_type: ",
97 f);
98 symbol_print (l->content.sym, f);
99 fputs (l->action_props.is_value_used ? " used" : " unused", f);
100 sep = ", ";
101 }
102 }
103
104
105 /*---------------------------.
106 | Prepend NODE to the LIST. |
107 `---------------------------*/
108
109 symbol_list *
110 symbol_list_prepend (symbol_list *list, symbol_list *node)
111 {
112 node->next = list;
113 return node;
114 }
115
116
117 /*-------------------------.
118 | Append NODE to the LIST. |
119 `-------------------------*/
120
121 symbol_list *
122 symbol_list_append (symbol_list *list, symbol_list *node)
123 {
124 if (!list)
125 return node;
126 symbol_list *next = list;
127 while (next->next)
128 next = next->next;
129 next->next = node;
130 return list;
131 }
132
133
134 /*-----------------------------------------------.
135 | Free the LIST, but not the items it contains. |
136 `-----------------------------------------------*/
137
138 void
139 symbol_list_free (symbol_list *list)
140 {
141 symbol_list *node, *next;
142 for (node = list; node; node = next)
143 {
144 next = node->next;
145 named_ref_free (node->named_ref);
146 if (node->content_type == SYMLIST_TYPE)
147 free (node->content.sem_type);
148 free (node);
149 }
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 (&loc, complaint, _("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 bool
212 symbol_list_null (symbol_list *node)
213 {
214 return !node ||
215 (node->content_type == SYMLIST_SYMBOL && !(node->content.sym));
216 }
217
218 void
219 symbol_list_code_props_set (symbol_list *node, code_props_type kind,
220 code_props const *cprops)
221 {
222 switch (node->content_type)
223 {
224 case SYMLIST_SYMBOL:
225 symbol_code_props_set (node->content.sym, kind, cprops);
226 if (node->content.sym->status == undeclared)
227 node->content.sym->status = used;
228 break;
229 case SYMLIST_TYPE:
230 semantic_type_code_props_set
231 (semantic_type_get (node->content.sem_type->tag,
232 &node->content.sem_type->location),
233 kind, cprops);
234 if (node->content.sem_type->status == undeclared)
235 node->content.sem_type->status = used;
236 break;
237 }
238 }