diagnostics: better location for type redeclarations
[bison.git] / src / symlist.c
blob55c1fb6bc651aff7eec48408fc2dee3d6a9ed0aa
1 /* Lists of symbols for Bison
3 Copyright (C) 2002, 2005-2007, 2009-2015, 2018-2020 Free Software
4 Foundation, Inc.
6 This file is part of Bison, the GNU Compiler Compiler.
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.
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.
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/>. */
21 #include <config.h>
22 #include "system.h"
24 #include "symlist.h"
26 /*--------------------------------------.
27 | Create a list containing SYM at LOC. |
28 `--------------------------------------*/
30 symbol_list *
31 symbol_list_sym_new (symbol *sym, location loc)
33 symbol_list *res = xmalloc (sizeof *res);
35 res->content_type = SYMLIST_SYMBOL;
36 res->content.sym = sym;
37 res->sym_loc = loc;
38 res->named_ref = NULL;
40 res->midrule = NULL;
41 res->midrule_parent_rule = NULL;
42 res->midrule_parent_rhs_index = 0;
44 /* Members used for LHS only. */
45 res->rhs_loc = empty_loc;
46 res->ruleprec = NULL;
47 res->percent_empty_loc = empty_loc;
48 code_props_none_init (&res->action_props);
49 res->dprec = 0;
50 res->dprec_loc = empty_loc;
51 res->merger = 0;
52 res->merger_declaration_loc = empty_loc;
53 res->expected_sr_conflicts = -1;
54 res->expected_rr_conflicts = -1;
56 res->next = NULL;
58 return res;
62 /*--------------------------------------------.
63 | Create a list containing TYPE_NAME at LOC. |
64 `--------------------------------------------*/
66 symbol_list *
67 symbol_list_type_new (uniqstr type_name, location loc)
69 symbol_list *res = xmalloc (sizeof *res);
71 res->content_type = SYMLIST_TYPE;
72 res->content.sem_type = xmalloc (sizeof (semantic_type));
73 res->content.sem_type->tag = type_name;
74 res->content.sem_type->location = loc;
75 res->content.sem_type->status = undeclared;
77 res->sym_loc = loc;
78 res->named_ref = NULL;
79 res->next = NULL;
81 return res;
85 symbol_list *
86 symbol_list_type_set (symbol_list *syms, uniqstr type_name)
88 for (symbol_list *l = syms; l; l = l->next)
89 symbol_type_set (l->content.sym, type_name, l->sym_loc);
90 return syms;
94 /*-----------------------------------------------------------------------.
95 | Print this list, for which every content_type must be SYMLIST_SYMBOL. |
96 `-----------------------------------------------------------------------*/
98 void
99 symbol_list_syms_print (const symbol_list *l, FILE *f)
101 fputc ('[', f);
102 char const *sep = "";
103 for (/* Nothing. */; l && l->content.sym; l = l->next)
105 fputs (sep, f);
106 fputs (l->content_type == SYMLIST_SYMBOL ? "symbol: "
107 : l->content_type == SYMLIST_TYPE ? "type: "
108 : "invalid content_type: ",
110 if (l->content_type == SYMLIST_SYMBOL)
111 symbol_print (l->content.sym, f);
112 fputs (l->action_props.is_value_used ? " (used)" : " (unused)", f);
113 sep = ", ";
115 fputc (']', f);
119 /*---------------------------.
120 | Prepend NODE to the LIST. |
121 `---------------------------*/
123 symbol_list *
124 symbol_list_prepend (symbol_list *list, symbol_list *node)
126 node->next = list;
127 return node;
131 /*-------------------------.
132 | Append NODE to the LIST. |
133 `-------------------------*/
135 symbol_list *
136 symbol_list_append (symbol_list *list, symbol_list *node)
138 if (!list)
139 return node;
140 symbol_list *next = list;
141 while (next->next)
142 next = next->next;
143 next->next = node;
144 return list;
148 /*-----------------------------------------------.
149 | Free the LIST, but not the items it contains. |
150 `-----------------------------------------------*/
152 void
153 symbol_list_free (symbol_list *list)
155 for (symbol_list *next; list; list = next)
157 next = list->next;
158 named_ref_free (list->named_ref);
159 if (list->content_type == SYMLIST_TYPE)
160 free (list->content.sem_type);
161 free (list);
166 /*--------------------.
167 | Return its length. |
168 `--------------------*/
171 symbol_list_length (symbol_list const *l)
173 int res = 0;
174 for (/* Nothing. */;
175 l && !(l->content_type == SYMLIST_SYMBOL && l->content.sym == NULL);
176 l = l->next)
177 ++res;
178 return res;
182 /*------------------------------.
183 | Get item N in symbol list L. |
184 `------------------------------*/
186 symbol_list *
187 symbol_list_n_get (symbol_list *l, int n)
189 aver (0 <= n);
190 for (int i = 0; i < n; ++i)
192 l = l->next;
193 aver (l);
195 aver (l->content_type == SYMLIST_SYMBOL);
196 aver (l->content.sym);
197 return l;
200 /*--------------------------------------------------------------.
201 | Get the data type (alternative in the union) of the value for |
202 | symbol N in symbol list L. |
203 `--------------------------------------------------------------*/
205 uniqstr
206 symbol_list_n_type_name_get (symbol_list *l, int n)
208 return symbol_list_n_get (l, n)->content.sym->content->type_name;
211 bool
212 symbol_list_null (symbol_list *node)
214 return (!node
215 || (node->content_type == SYMLIST_SYMBOL && !node->content.sym));
218 void
219 symbol_list_code_props_set (symbol_list *node, code_props_type kind,
220 code_props const *cprops)
222 switch (node->content_type)
224 case SYMLIST_SYMBOL:
225 symbol_code_props_set (node->content.sym, kind, cprops);
226 if (node->content.sym->content->status == undeclared)
227 node->content.sym->content->status = used;
228 break;
229 case SYMLIST_TYPE:
230 semantic_type_code_props_set
231 (semantic_type_get (node->content.sem_type->tag,
232 &node->content.sem_type->location),
233 kind, cprops);
234 if (node->content.sem_type->status == undeclared)
235 node->content.sem_type->status = used;
236 break;