]>
Commit | Line | Data |
---|---|---|
56c47203 | 1 | /* Lists of symbols for Bison |
17ee7397 | 2 | |
7d6bad19 | 3 | Copyright (C) 2002, 2005-2007, 2009-2013 Free Software Foundation, |
575619af | 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 | ||
17ee7397 PE |
27 | /*--------------------------------------. |
28 | | Create a list containing SYM at LOC. | | |
29 | `--------------------------------------*/ | |
56c47203 | 30 | |
17ee7397 | 31 | symbol_list * |
3be03b13 | 32 | symbol_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; | |
b9f1d9a4 | 38 | res->location = res->sym_loc = loc; |
ec6dc437 | 39 | res->named_ref = NULL; |
affac613 | 40 | |
6ec2c0f2 | 41 | res->midrule = NULL; |
ffa4ba3a JD |
42 | res->midrule_parent_rule = NULL; |
43 | res->midrule_parent_rhs_index = 0; | |
84866159 | 44 | |
ec6dc437 | 45 | /* Members used for LHS only. */ |
56c47203 | 46 | res->ruleprec = NULL; |
ae2b48f5 | 47 | res->percent_empty_loc = empty_location; |
ec6dc437 | 48 | code_props_none_init (&res->action_props); |
676385e2 PH |
49 | res->dprec = 0; |
50 | res->merger = 0; | |
affac613 AD |
51 | |
52 | res->next = NULL; | |
53 | ||
56c47203 AD |
54 | return res; |
55 | } | |
56 | ||
57 | ||
3be03b13 JD |
58 | /*--------------------------------------------. |
59 | | Create a list containing TYPE_NAME at LOC. | | |
60 | `--------------------------------------------*/ | |
61 | ||
62 | symbol_list * | |
63 | symbol_list_type_new (uniqstr type_name, location loc) | |
64 | { | |
65 | symbol_list *res = xmalloc (sizeof *res); | |
66 | ||
67 | res->content_type = SYMLIST_TYPE; | |
9641b918 VS |
68 | res->content.sem_type = xmalloc (sizeof (semantic_type)); |
69 | res->content.sem_type->tag = type_name; | |
70 | res->content.sem_type->location = loc; | |
71 | res->content.sem_type->status = undeclared; | |
72 | ||
b9f1d9a4 AR |
73 | res->location = res->sym_loc = loc; |
74 | res->named_ref = NULL; | |
3be03b13 JD |
75 | res->next = NULL; |
76 | ||
77 | return res; | |
78 | } | |
79 | ||
80 | ||
3be03b13 JD |
81 | /*-----------------------------------------------------------------------. |
82 | | Print this list, for which every content_type must be SYMLIST_SYMBOL. | | |
83 | `-----------------------------------------------------------------------*/ | |
867a3e00 AD |
84 | |
85 | void | |
3be03b13 | 86 | symbol_list_syms_print (const symbol_list *l, FILE *f) |
867a3e00 | 87 | { |
848bcc28 | 88 | char const *sep = ""; |
3be03b13 | 89 | for (/* Nothing. */; l && l->content.sym; l = l->next) |
22dda0f0 | 90 | { |
848bcc28 AD |
91 | fputs (sep, f); |
92 | fputs (l->content_type == SYMLIST_SYMBOL ? "symbol: " | |
93 | : l->content_type == SYMLIST_TYPE ? "type: " | |
94 | : "invalid content_type: ", | |
95 | f); | |
3be03b13 | 96 | symbol_print (l->content.sym, f); |
848bcc28 AD |
97 | fputs (l->action_props.is_value_used ? " used" : " unused", f); |
98 | sep = ", "; | |
22dda0f0 | 99 | } |
867a3e00 AD |
100 | } |
101 | ||
102 | ||
3be03b13 JD |
103 | /*---------------------------. |
104 | | Prepend NODE to the LIST. | | |
105 | `---------------------------*/ | |
56c47203 | 106 | |
17ee7397 | 107 | symbol_list * |
3be03b13 | 108 | symbol_list_prepend (symbol_list *list, symbol_list *node) |
56c47203 | 109 | { |
3be03b13 JD |
110 | node->next = list; |
111 | return node; | |
56c47203 AD |
112 | } |
113 | ||
114 | ||
93561c21 VT |
115 | /*-------------------------. |
116 | | Append NODE to the LIST. | | |
117 | `-------------------------*/ | |
118 | ||
119 | symbol_list * | |
120 | symbol_list_append (symbol_list *list, symbol_list *node) | |
121 | { | |
122 | if (!list) | |
123 | return node; | |
124 | symbol_list *next = list; | |
125 | while (next->next) | |
126 | next = next->next; | |
127 | next->next = node; | |
128 | return list; | |
129 | } | |
130 | ||
131 | ||
3be03b13 JD |
132 | /*-----------------------------------------------. |
133 | | Free the LIST, but not the items it contains. | | |
134 | `-----------------------------------------------*/ | |
56c47203 AD |
135 | |
136 | void | |
17ee7397 | 137 | symbol_list_free (symbol_list *list) |
56c47203 | 138 | { |
13cdf208 AR |
139 | symbol_list *node, *next; |
140 | for (node = list; node; node = next) | |
141 | { | |
142 | next = node->next; | |
a4d1bf6a | 143 | named_ref_free (node->named_ref); |
ae9c90ba TR |
144 | if (node->content_type == SYMLIST_TYPE) |
145 | free (node->content.sem_type); | |
13cdf208 AR |
146 | free (node); |
147 | } | |
56c47203 AD |
148 | } |
149 | ||
150 | ||
dafdc66f AD |
151 | /*--------------------. |
152 | | Return its length. | | |
153 | `--------------------*/ | |
154 | ||
b37acfe1 PE |
155 | int |
156 | symbol_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 | |
171 | symbol_list * | |
172 | symbol_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)) | |
e9690142 | 184 | return NULL; |
affac613 AD |
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 | 196 | uniqstr |
affac613 | 197 | symbol_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 | { |
bb8e56ff | 202 | complain (&loc, complaint, _("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 | |
872b52bc | 209 | bool |
676997e5 | 210 | symbol_list_null (symbol_list *node) |
872b52bc AR |
211 | { |
212 | return !node || | |
213 | (node->content_type == SYMLIST_SYMBOL && !(node->content.sym)); | |
214 | } | |
56c47203 | 215 | |
3be03b13 | 216 | void |
71da68b3 | 217 | symbol_list_code_props_set (symbol_list *node, code_props_type kind, |
4323e0da | 218 | code_props const *cprops) |
3be03b13 JD |
219 | { |
220 | switch (node->content_type) | |
221 | { | |
3edfae04 AD |
222 | case SYMLIST_SYMBOL: |
223 | symbol_code_props_set (node->content.sym, kind, cprops); | |
224 | if (node->content.sym->status == undeclared) | |
225 | node->content.sym->status = used; | |
226 | break; | |
227 | case SYMLIST_TYPE: | |
228 | semantic_type_code_props_set | |
229 | (semantic_type_get (node->content.sem_type->tag, | |
230 | &node->content.sem_type->location), | |
231 | kind, cprops); | |
232 | if (node->content.sem_type->status == undeclared) | |
233 | node->content.sem_type->status = used; | |
234 | break; | |
3be03b13 JD |
235 | } |
236 | } |