]> git.saurik.com Git - bison.git/blame - src/symlist.c
debug: improve the display of symbol lists
[bison.git] / src / symlist.c
CommitLineData
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 31symbol_list *
3be03b13 32symbol_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;
ec6dc437 47 code_props_none_init (&res->action_props);
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
61symbol_list *
62symbol_list_type_new (uniqstr type_name, location loc)
63{
64 symbol_list *res = xmalloc (sizeof *res);
65
66 res->content_type = SYMLIST_TYPE;
9641b918
VS
67 res->content.sem_type = xmalloc (sizeof (semantic_type));
68 res->content.sem_type->tag = type_name;
69 res->content.sem_type->location = loc;
70 res->content.sem_type->status = undeclared;
71
b9f1d9a4
AR
72 res->location = res->sym_loc = loc;
73 res->named_ref = NULL;
3be03b13
JD
74 res->next = NULL;
75
76 return res;
77}
78
79
3be03b13
JD
80/*-----------------------------------------------------------------------.
81| Print this list, for which every content_type must be SYMLIST_SYMBOL. |
82`-----------------------------------------------------------------------*/
867a3e00
AD
83
84void
3be03b13 85symbol_list_syms_print (const symbol_list *l, FILE *f)
867a3e00 86{
848bcc28 87 char const *sep = "";
3be03b13 88 for (/* Nothing. */; l && l->content.sym; l = l->next)
22dda0f0 89 {
848bcc28
AD
90 fputs (sep, f);
91 fputs (l->content_type == SYMLIST_SYMBOL ? "symbol: "
92 : l->content_type == SYMLIST_TYPE ? "type: "
93 : "invalid content_type: ",
94 f);
3be03b13 95 symbol_print (l->content.sym, f);
848bcc28
AD
96 fputs (l->action_props.is_value_used ? " used" : " unused", f);
97 sep = ", ";
22dda0f0 98 }
867a3e00
AD
99}
100
101
3be03b13
JD
102/*---------------------------.
103| Prepend NODE to the LIST. |
104`---------------------------*/
56c47203 105
17ee7397 106symbol_list *
3be03b13 107symbol_list_prepend (symbol_list *list, symbol_list *node)
56c47203 108{
3be03b13
JD
109 node->next = list;
110 return node;
56c47203
AD
111}
112
113
93561c21
VT
114/*-------------------------.
115| Append NODE to the LIST. |
116`-------------------------*/
117
118symbol_list *
119symbol_list_append (symbol_list *list, symbol_list *node)
120{
121 if (!list)
122 return node;
123 symbol_list *next = list;
124 while (next->next)
125 next = next->next;
126 next->next = node;
127 return list;
128}
129
130
3be03b13
JD
131/*-----------------------------------------------.
132| Free the LIST, but not the items it contains. |
133`-----------------------------------------------*/
56c47203
AD
134
135void
17ee7397 136symbol_list_free (symbol_list *list)
56c47203 137{
13cdf208
AR
138 symbol_list *node, *next;
139 for (node = list; node; node = next)
140 {
141 next = node->next;
a4d1bf6a 142 named_ref_free (node->named_ref);
ae9c90ba
TR
143 if (node->content_type == SYMLIST_TYPE)
144 free (node->content.sem_type);
13cdf208
AR
145 free (node);
146 }
56c47203
AD
147}
148
149
dafdc66f
AD
150/*--------------------.
151| Return its length. |
152`--------------------*/
153
b37acfe1
PE
154int
155symbol_list_length (symbol_list const *l)
dafdc66f
AD
156{
157 int res = 0;
3be03b13
JD
158 for (/* Nothing. */;
159 l && !(l->content_type == SYMLIST_SYMBOL && l->content.sym == NULL);
160 l = l->next)
dafdc66f
AD
161 ++res;
162 return res;
163}
164
165
3be03b13
JD
166/*------------------------------.
167| Get item N in symbol list L. |
168`------------------------------*/
affac613
AD
169
170symbol_list *
171symbol_list_n_get (symbol_list *l, int n)
172{
173 int i;
174
175 if (n < 0)
176 return NULL;
177
178 for (i = 0; i < n; ++i)
179 {
180 l = l->next;
3be03b13
JD
181 if (l == NULL
182 || (l->content_type == SYMLIST_SYMBOL && l->content.sym == NULL))
e9690142 183 return NULL;
affac613
AD
184 }
185
186 return l;
187}
188
189
56c47203
AD
190/*--------------------------------------------------------------.
191| Get the data type (alternative in the union) of the value for |
affac613 192| symbol N in symbol list L. |
56c47203
AD
193`--------------------------------------------------------------*/
194
17ee7397 195uniqstr
affac613 196symbol_list_n_type_name_get (symbol_list *l, location loc, int n)
56c47203 197{
affac613
AD
198 l = symbol_list_n_get (l, n);
199 if (!l)
56c47203 200 {
bb8e56ff 201 complain (&loc, complaint, _("invalid $ value: $%d"), n);
56c47203
AD
202 return NULL;
203 }
4f82b42a 204 aver (l->content_type == SYMLIST_SYMBOL);
3be03b13 205 return l->content.sym->type_name;
affac613 206}
56c47203 207
872b52bc 208bool
676997e5 209symbol_list_null (symbol_list *node)
872b52bc
AR
210{
211 return !node ||
212 (node->content_type == SYMLIST_SYMBOL && !(node->content.sym));
213}
56c47203 214
3be03b13 215void
71da68b3 216symbol_list_code_props_set (symbol_list *node, code_props_type kind,
4323e0da 217 code_props const *cprops)
3be03b13
JD
218{
219 switch (node->content_type)
220 {
3edfae04
AD
221 case SYMLIST_SYMBOL:
222 symbol_code_props_set (node->content.sym, kind, cprops);
223 if (node->content.sym->status == undeclared)
224 node->content.sym->status = used;
225 break;
226 case SYMLIST_TYPE:
227 semantic_type_code_props_set
228 (semantic_type_get (node->content.sem_type->tag,
229 &node->content.sem_type->location),
230 kind, cprops);
231 if (node->content.sem_type->status == undeclared)
232 node->content.sem_type->status = used;
233 break;
3be03b13
JD
234 }
235}