]> git.saurik.com Git - bison.git/blob - src/symlist.c
* src/symlist.c (symbol_list_length): Return int, not unsigned
[bison.git] / src / symlist.c
1 /* Lists of symbols for Bison
2
3 Copyright (C) 2002, 2005, 2006 Free Software Foundation, Inc.
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
7 Bison is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 Bison is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
21
22 #include <config.h>
23 #include "system.h"
24
25 #include "complain.h"
26 #include "symlist.h"
27
28
29 /*--------------------------------------.
30 | Create a list containing SYM at LOC. |
31 `--------------------------------------*/
32
33 symbol_list *
34 symbol_list_new (symbol *sym, location loc)
35 {
36 symbol_list *res = xmalloc (sizeof *res);
37
38 res->sym = sym;
39 res->location = loc;
40
41 res->midrule = NULL;
42 res->midrule_parent_rule = NULL;
43 res->midrule_parent_rhs_index = 0;
44
45 res->action = NULL;
46 res->used = false;
47
48 res->ruleprec = NULL;
49 res->dprec = 0;
50 res->merger = 0;
51
52 res->next = NULL;
53
54 return res;
55 }
56
57
58 /*------------------.
59 | Print this list. |
60 `------------------*/
61
62 void
63 symbol_list_print (const symbol_list *l, FILE *f)
64 {
65 for (/* Nothing. */; l && l->sym; l = l->next)
66 {
67 symbol_print (l->sym, f);
68 fprintf (stderr, l->used ? " used" : " unused");
69 if (l && l->sym)
70 fprintf (f, ", ");
71 }
72 }
73
74
75 /*---------------------------------.
76 | Prepend SYM at LOC to the LIST. |
77 `---------------------------------*/
78
79 symbol_list *
80 symbol_list_prepend (symbol_list *list, symbol *sym, location loc)
81 {
82 symbol_list *res = symbol_list_new (sym, loc);
83 res->next = list;
84 return res;
85 }
86
87
88 /*-------------------------------------------------.
89 | Free the LIST, but not the symbols it contains. |
90 `-------------------------------------------------*/
91
92 void
93 symbol_list_free (symbol_list *list)
94 {
95 LIST_FREE (symbol_list, list);
96 }
97
98
99 /*--------------------.
100 | Return its length. |
101 `--------------------*/
102
103 int
104 symbol_list_length (symbol_list const *l)
105 {
106 int res = 0;
107 for (/* Nothing. */; l && l->sym; l = l->next)
108 ++res;
109 return res;
110 }
111
112
113 /*--------------------------------.
114 | Get symbol N in symbol list L. |
115 `--------------------------------*/
116
117 symbol_list *
118 symbol_list_n_get (symbol_list *l, int n)
119 {
120 int i;
121
122 if (n < 0)
123 return NULL;
124
125 for (i = 0; i < n; ++i)
126 {
127 l = l->next;
128 if (l == NULL || l->sym == NULL)
129 return NULL;
130 }
131
132 return l;
133 }
134
135
136 /*--------------------------------------------------------------.
137 | Get the data type (alternative in the union) of the value for |
138 | symbol N in symbol list L. |
139 `--------------------------------------------------------------*/
140
141 uniqstr
142 symbol_list_n_type_name_get (symbol_list *l, location loc, int n)
143 {
144 l = symbol_list_n_get (l, n);
145 if (!l)
146 {
147 complain_at (loc, _("invalid $ value: $%d"), n);
148 return NULL;
149 }
150 return l->sym->type_name;
151 }
152
153
154 /*----------------------------------------.
155 | The symbol N in symbol list L is USED. |
156 `----------------------------------------*/
157
158 void
159 symbol_list_n_used_set (symbol_list *l, int n, bool used)
160 {
161 l = symbol_list_n_get (l, n);
162 if (l)
163 l->used = used;
164 }