]> git.saurik.com Git - bison.git/blame - src/symlist.c
gnulib: update
[bison.git] / src / symlist.c
CommitLineData
56c47203 1/* Lists of symbols for Bison
17ee7397 2
3209eb1c 3 Copyright (C) 2002, 2005-2007, 2009-2015 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
56c47203
AD
24#include "symlist.h"
25
17ee7397
PE
26/*--------------------------------------.
27| Create a list containing SYM at LOC. |
28`--------------------------------------*/
56c47203 29
17ee7397 30symbol_list *
3be03b13 31symbol_list_sym_new (symbol *sym, location loc)
56c47203 32{
da2a7671 33 symbol_list *res = xmalloc (sizeof *res);
affac613 34
3be03b13
JD
35 res->content_type = SYMLIST_SYMBOL;
36 res->content.sym = sym;
b9f1d9a4 37 res->location = res->sym_loc = loc;
ec6dc437 38 res->named_ref = NULL;
affac613 39
6ec2c0f2 40 res->midrule = NULL;
ffa4ba3a
JD
41 res->midrule_parent_rule = NULL;
42 res->midrule_parent_rhs_index = 0;
84866159 43
ec6dc437 44 /* Members used for LHS only. */
56c47203 45 res->ruleprec = NULL;
ae2b48f5 46 res->percent_empty_loc = empty_location;
ec6dc437 47 code_props_none_init (&res->action_props);
676385e2 48 res->dprec = 0;
0fe5a72a 49 res->dprec_location = empty_location;
676385e2 50 res->merger = 0;
0fe5a72a 51 res->merger_declaration_location = empty_location;
affac613
AD
52
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
63symbol_list *
64symbol_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
3be03b13
JD
82/*-----------------------------------------------------------------------.
83| Print this list, for which every content_type must be SYMLIST_SYMBOL. |
84`-----------------------------------------------------------------------*/
867a3e00
AD
85
86void
3be03b13 87symbol_list_syms_print (const symbol_list *l, FILE *f)
867a3e00 88{
848bcc28 89 char const *sep = "";
3be03b13 90 for (/* Nothing. */; l && l->content.sym; l = l->next)
22dda0f0 91 {
848bcc28
AD
92 fputs (sep, f);
93 fputs (l->content_type == SYMLIST_SYMBOL ? "symbol: "
94 : l->content_type == SYMLIST_TYPE ? "type: "
95 : "invalid content_type: ",
96 f);
3be03b13 97 symbol_print (l->content.sym, f);
848bcc28
AD
98 fputs (l->action_props.is_value_used ? " used" : " unused", f);
99 sep = ", ";
22dda0f0 100 }
867a3e00
AD
101}
102
103
3be03b13
JD
104/*---------------------------.
105| Prepend NODE to the LIST. |
106`---------------------------*/
56c47203 107
17ee7397 108symbol_list *
3be03b13 109symbol_list_prepend (symbol_list *list, symbol_list *node)
56c47203 110{
3be03b13
JD
111 node->next = list;
112 return node;
56c47203
AD
113}
114
115
93561c21
VT
116/*-------------------------.
117| Append NODE to the LIST. |
118`-------------------------*/
119
120symbol_list *
121symbol_list_append (symbol_list *list, symbol_list *node)
122{
123 if (!list)
124 return node;
125 symbol_list *next = list;
126 while (next->next)
127 next = next->next;
128 next->next = node;
129 return list;
130}
131
132
3be03b13
JD
133/*-----------------------------------------------.
134| Free the LIST, but not the items it contains. |
135`-----------------------------------------------*/
56c47203
AD
136
137void
17ee7397 138symbol_list_free (symbol_list *list)
56c47203 139{
13cdf208
AR
140 symbol_list *node, *next;
141 for (node = list; node; node = next)
142 {
143 next = node->next;
a4d1bf6a 144 named_ref_free (node->named_ref);
ae9c90ba
TR
145 if (node->content_type == SYMLIST_TYPE)
146 free (node->content.sem_type);
13cdf208
AR
147 free (node);
148 }
56c47203
AD
149}
150
151
dafdc66f
AD
152/*--------------------.
153| Return its length. |
154`--------------------*/
155
b37acfe1
PE
156int
157symbol_list_length (symbol_list const *l)
dafdc66f
AD
158{
159 int res = 0;
3be03b13
JD
160 for (/* Nothing. */;
161 l && !(l->content_type == SYMLIST_SYMBOL && l->content.sym == NULL);
162 l = l->next)
dafdc66f
AD
163 ++res;
164 return res;
165}
166
167
3be03b13
JD
168/*------------------------------.
169| Get item N in symbol list L. |
170`------------------------------*/
affac613
AD
171
172symbol_list *
173symbol_list_n_get (symbol_list *l, int n)
174{
175 int i;
671850a1 176 aver (0 <= n);
affac613
AD
177 for (i = 0; i < n; ++i)
178 {
179 l = l->next;
671850a1 180 aver (l);
affac613 181 }
671850a1
AD
182 aver (l->content_type == SYMLIST_SYMBOL);
183 aver (l->content.sym);
affac613
AD
184 return l;
185}
186
56c47203
AD
187/*--------------------------------------------------------------.
188| Get the data type (alternative in the union) of the value for |
affac613 189| symbol N in symbol list L. |
56c47203
AD
190`--------------------------------------------------------------*/
191
17ee7397 192uniqstr
a2c525b3 193symbol_list_n_type_name_get (symbol_list *l, int n)
56c47203 194{
671850a1 195 return symbol_list_n_get (l, n)->content.sym->type_name;
affac613 196}
56c47203 197
872b52bc 198bool
676997e5 199symbol_list_null (symbol_list *node)
872b52bc 200{
671850a1
AD
201 return (!node
202 || (node->content_type == SYMLIST_SYMBOL && !node->content.sym));
872b52bc 203}
56c47203 204
3be03b13 205void
71da68b3 206symbol_list_code_props_set (symbol_list *node, code_props_type kind,
4323e0da 207 code_props const *cprops)
3be03b13
JD
208{
209 switch (node->content_type)
210 {
3edfae04
AD
211 case SYMLIST_SYMBOL:
212 symbol_code_props_set (node->content.sym, kind, cprops);
213 if (node->content.sym->status == undeclared)
214 node->content.sym->status = used;
215 break;
216 case SYMLIST_TYPE:
217 semantic_type_code_props_set
218 (semantic_type_get (node->content.sem_type->tag,
219 &node->content.sem_type->location),
220 kind, cprops);
221 if (node->content.sem_type->status == undeclared)
222 node->content.sem_type->status = used;
223 break;
3be03b13
JD
224 }
225}