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