]> git.saurik.com Git - bison.git/blob - src/symlist.c
72ca11096ddc06f2a6e13171ec2582e37979f517
[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 code_props_none_init (&res->action_props);
48 res->dprec = 0;
49 res->merger = 0;
50
51 res->next = NULL;
52
53 return res;
54 }
55
56
57 /*--------------------------------------------.
58 | Create a list containing TYPE_NAME at LOC. |
59 `--------------------------------------------*/
60
61 symbol_list *
62 symbol_list_type_new (uniqstr type_name, location loc)
63 {
64 symbol_list *res = xmalloc (sizeof *res);
65
66 res->content_type = SYMLIST_TYPE;
67 res->content.sem_type = xmalloc (sizeof (semantic_type));
68 res->content.sem_type->tag = type_name;
69 res->content.sem_type->location = loc;
70 res->content.sem_type->status = undeclared;
71
72 res->location = res->sym_loc = loc;
73 res->named_ref = NULL;
74 res->next = NULL;
75
76 return res;
77 }
78
79
80 /*-----------------------------------------------------------------------.
81 | Print this list, for which every content_type must be SYMLIST_SYMBOL. |
82 `-----------------------------------------------------------------------*/
83
84 void
85 symbol_list_syms_print (const symbol_list *l, FILE *f)
86 {
87 for (/* Nothing. */; l && l->content.sym; l = l->next)
88 {
89 symbol_print (l->content.sym, f);
90 fprintf (f, l->action_props.is_value_used ? " used" : " unused");
91 if (l && l->content.sym)
92 fprintf (f, ", ");
93 }
94 }
95
96
97 /*---------------------------.
98 | Prepend NODE to the LIST. |
99 `---------------------------*/
100
101 symbol_list *
102 symbol_list_prepend (symbol_list *list, symbol_list *node)
103 {
104 node->next = list;
105 return node;
106 }
107
108
109 /*-------------------------.
110 | Append NODE to the LIST. |
111 `-------------------------*/
112
113 symbol_list *
114 symbol_list_append (symbol_list *list, symbol_list *node)
115 {
116 if (!list)
117 return node;
118 symbol_list *next = list;
119 while (next->next)
120 next = next->next;
121 next->next = node;
122 return list;
123 }
124
125
126 /*-----------------------------------------------.
127 | Free the LIST, but not the items it contains. |
128 `-----------------------------------------------*/
129
130 void
131 symbol_list_free (symbol_list *list)
132 {
133 symbol_list *node, *next;
134 for (node = list; node; node = next)
135 {
136 next = node->next;
137 named_ref_free (node->named_ref);
138 if (node->content_type == SYMLIST_TYPE)
139 free (node->content.sem_type);
140 free (node);
141 }
142 }
143
144
145 /*--------------------.
146 | Return its length. |
147 `--------------------*/
148
149 int
150 symbol_list_length (symbol_list const *l)
151 {
152 int res = 0;
153 for (/* Nothing. */;
154 l && !(l->content_type == SYMLIST_SYMBOL && l->content.sym == NULL);
155 l = l->next)
156 ++res;
157 return res;
158 }
159
160
161 /*------------------------------.
162 | Get item N in symbol list L. |
163 `------------------------------*/
164
165 symbol_list *
166 symbol_list_n_get (symbol_list *l, int n)
167 {
168 int i;
169
170 if (n < 0)
171 return NULL;
172
173 for (i = 0; i < n; ++i)
174 {
175 l = l->next;
176 if (l == NULL
177 || (l->content_type == SYMLIST_SYMBOL && l->content.sym == NULL))
178 return NULL;
179 }
180
181 return l;
182 }
183
184
185 /*--------------------------------------------------------------.
186 | Get the data type (alternative in the union) of the value for |
187 | symbol N in symbol list L. |
188 `--------------------------------------------------------------*/
189
190 uniqstr
191 symbol_list_n_type_name_get (symbol_list *l, location loc, int n)
192 {
193 l = symbol_list_n_get (l, n);
194 if (!l)
195 {
196 complain (&loc, complaint, _("invalid $ value: $%d"), n);
197 return NULL;
198 }
199 aver (l->content_type == SYMLIST_SYMBOL);
200 return l->content.sym->type_name;
201 }
202
203 bool
204 symbol_list_null (symbol_list *node)
205 {
206 return !node ||
207 (node->content_type == SYMLIST_SYMBOL && !(node->content.sym));
208 }
209
210 void
211 symbol_list_code_props_set (symbol_list *node, code_props_type kind,
212 code_props const *cprops)
213 {
214 switch (node->content_type)
215 {
216 case SYMLIST_SYMBOL:
217 symbol_code_props_set (node->content.sym, kind, cprops);
218 if (node->content.sym->status == undeclared)
219 node->content.sym->status = used;
220 break;
221 case SYMLIST_TYPE:
222 semantic_type_code_props_set
223 (semantic_type_get (node->content.sem_type->tag,
224 &node->content.sem_type->location),
225 kind, cprops);
226 if (node->content.sem_type->status == undeclared)
227 node->content.sem_type->status = used;
228 break;
229 }
230 }