]>
Commit | Line | Data |
---|---|---|
56c47203 | 1 | /* Lists of symbols for Bison |
17ee7397 | 2 | |
34136e65 | 3 | Copyright (C) 2002, 2005-2007, 2009-2012 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; |
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 | 49 | |
b9f1d9a4 AR |
50 | res->named_ref = NULL; |
51 | ||
affac613 AD |
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 | ||
12e35840 JD |
81 | /*----------------------------------------. |
82 | | Create a list containing a <*> at LOC. | | |
83 | `----------------------------------------*/ | |
3be03b13 JD |
84 | |
85 | symbol_list * | |
12e35840 | 86 | symbol_list_default_tagged_new (location loc) |
3be03b13 JD |
87 | { |
88 | symbol_list *res = xmalloc (sizeof *res); | |
89 | ||
12e35840 | 90 | res->content_type = SYMLIST_DEFAULT_TAGGED; |
b9f1d9a4 AR |
91 | res->location = res->sym_loc = loc; |
92 | res->named_ref = NULL; | |
12e35840 JD |
93 | res->next = NULL; |
94 | ||
95 | return res; | |
96 | } | |
97 | ||
98 | ||
3ebecc24 JD |
99 | /*---------------------------------------. |
100 | | Create a list containing a <> at LOC. | | |
101 | `---------------------------------------*/ | |
12e35840 JD |
102 | |
103 | symbol_list * | |
104 | symbol_list_default_tagless_new (location loc) | |
105 | { | |
106 | symbol_list *res = xmalloc (sizeof *res); | |
107 | ||
108 | res->content_type = SYMLIST_DEFAULT_TAGLESS; | |
b9f1d9a4 AR |
109 | res->location = res->sym_loc = loc; |
110 | res->named_ref = NULL; | |
3be03b13 JD |
111 | res->next = NULL; |
112 | ||
113 | return res; | |
114 | } | |
115 | ||
116 | ||
117 | /*-----------------------------------------------------------------------. | |
118 | | Print this list, for which every content_type must be SYMLIST_SYMBOL. | | |
119 | `-----------------------------------------------------------------------*/ | |
867a3e00 AD |
120 | |
121 | void | |
3be03b13 | 122 | symbol_list_syms_print (const symbol_list *l, FILE *f) |
867a3e00 | 123 | { |
3be03b13 | 124 | for (/* Nothing. */; l && l->content.sym; l = l->next) |
22dda0f0 | 125 | { |
3be03b13 | 126 | symbol_print (l->content.sym, f); |
f6857bbf | 127 | fprintf (stderr, l->action_props.is_value_used ? " used" : " unused"); |
3be03b13 | 128 | if (l && l->content.sym) |
e9690142 | 129 | fprintf (f, ", "); |
22dda0f0 | 130 | } |
867a3e00 AD |
131 | } |
132 | ||
133 | ||
3be03b13 JD |
134 | /*---------------------------. |
135 | | Prepend NODE to the LIST. | | |
136 | `---------------------------*/ | |
56c47203 | 137 | |
17ee7397 | 138 | symbol_list * |
3be03b13 | 139 | symbol_list_prepend (symbol_list *list, symbol_list *node) |
56c47203 | 140 | { |
3be03b13 JD |
141 | node->next = list; |
142 | return node; | |
56c47203 AD |
143 | } |
144 | ||
145 | ||
3be03b13 JD |
146 | /*-----------------------------------------------. |
147 | | Free the LIST, but not the items it contains. | | |
148 | `-----------------------------------------------*/ | |
56c47203 AD |
149 | |
150 | void | |
17ee7397 | 151 | symbol_list_free (symbol_list *list) |
56c47203 | 152 | { |
13cdf208 AR |
153 | symbol_list *node, *next; |
154 | for (node = list; node; node = next) | |
155 | { | |
156 | next = node->next; | |
a4d1bf6a | 157 | named_ref_free (node->named_ref); |
13cdf208 AR |
158 | free (node); |
159 | } | |
56c47203 AD |
160 | } |
161 | ||
162 | ||
dafdc66f AD |
163 | /*--------------------. |
164 | | Return its length. | | |
165 | `--------------------*/ | |
166 | ||
b37acfe1 PE |
167 | int |
168 | symbol_list_length (symbol_list const *l) | |
dafdc66f AD |
169 | { |
170 | int res = 0; | |
3be03b13 JD |
171 | for (/* Nothing. */; |
172 | l && !(l->content_type == SYMLIST_SYMBOL && l->content.sym == NULL); | |
173 | l = l->next) | |
dafdc66f AD |
174 | ++res; |
175 | return res; | |
176 | } | |
177 | ||
178 | ||
3be03b13 JD |
179 | /*------------------------------. |
180 | | Get item N in symbol list L. | | |
181 | `------------------------------*/ | |
affac613 AD |
182 | |
183 | symbol_list * | |
184 | symbol_list_n_get (symbol_list *l, int n) | |
185 | { | |
186 | int i; | |
187 | ||
188 | if (n < 0) | |
189 | return NULL; | |
190 | ||
191 | for (i = 0; i < n; ++i) | |
192 | { | |
193 | l = l->next; | |
3be03b13 JD |
194 | if (l == NULL |
195 | || (l->content_type == SYMLIST_SYMBOL && l->content.sym == NULL)) | |
e9690142 | 196 | return NULL; |
affac613 AD |
197 | } |
198 | ||
199 | return l; | |
200 | } | |
201 | ||
202 | ||
56c47203 AD |
203 | /*--------------------------------------------------------------. |
204 | | Get the data type (alternative in the union) of the value for | | |
affac613 | 205 | | symbol N in symbol list L. | |
56c47203 AD |
206 | `--------------------------------------------------------------*/ |
207 | ||
17ee7397 | 208 | uniqstr |
affac613 | 209 | symbol_list_n_type_name_get (symbol_list *l, location loc, int n) |
56c47203 | 210 | { |
affac613 AD |
211 | l = symbol_list_n_get (l, n); |
212 | if (!l) | |
56c47203 | 213 | { |
6fb8b256 | 214 | complain_at (loc, complaint, _("invalid $ value: $%d"), n); |
56c47203 AD |
215 | return NULL; |
216 | } | |
4f82b42a | 217 | aver (l->content_type == SYMLIST_SYMBOL); |
3be03b13 | 218 | return l->content.sym->type_name; |
affac613 | 219 | } |
56c47203 | 220 | |
872b52bc | 221 | bool |
676997e5 | 222 | symbol_list_null (symbol_list *node) |
872b52bc AR |
223 | { |
224 | return !node || | |
225 | (node->content_type == SYMLIST_SYMBOL && !(node->content.sym)); | |
226 | } | |
56c47203 | 227 | |
3be03b13 | 228 | void |
71da68b3 VS |
229 | symbol_list_code_props_set (symbol_list *node, code_props_type kind, |
230 | location loc, char const *code) | |
3be03b13 | 231 | { |
71da68b3 VS |
232 | code_props cprops; |
233 | code_props_symbol_action_init (&cprops, code, loc); | |
234 | code_props_translate_code (&cprops); | |
3be03b13 JD |
235 | switch (node->content_type) |
236 | { | |
237 | case SYMLIST_SYMBOL: | |
71da68b3 | 238 | symbol_code_props_set (node->content.sym, kind, &cprops); |
3b0b682f | 239 | if (node->content.sym->status == undeclared) |
b921d92f | 240 | node->content.sym->status = used; |
3be03b13 JD |
241 | break; |
242 | case SYMLIST_TYPE: | |
71da68b3 | 243 | semantic_type_code_props_set |
9641b918 VS |
244 | (semantic_type_get (node->content.sem_type->tag, |
245 | &node->content.sem_type->location), | |
71da68b3 | 246 | kind, &cprops); |
9641b918 VS |
247 | if (node->content.sem_type->status == undeclared) |
248 | node->content.sem_type->status = used; | |
3be03b13 | 249 | break; |
12e35840 | 250 | case SYMLIST_DEFAULT_TAGGED: |
71da68b3 | 251 | default_tagged_code_props_set (kind, &cprops); |
12e35840 JD |
252 | break; |
253 | case SYMLIST_DEFAULT_TAGLESS: | |
71da68b3 | 254 | default_tagless_code_props_set (kind, &cprops); |
3be03b13 JD |
255 | break; |
256 | } | |
257 | } |