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