]>
Commit | Line | Data |
---|---|---|
616915dd | 1 | /* |
11db3170 | 2 | ** Splint - annotation-assisted static program checker |
c59f5181 | 3 | ** Copyright (C) 1994-2003 University of Virginia, |
616915dd | 4 | ** Massachusetts Institute of Technology |
5 | ** | |
6 | ** This program is free software; you can redistribute it and/or modify it | |
7 | ** under the terms of the GNU General Public License as published by the | |
8 | ** Free Software Foundation; either version 2 of the License, or (at your | |
9 | ** option) any later version. | |
10 | ** | |
11 | ** This program is distributed in the hope that it will be useful, but | |
12 | ** WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | ** General Public License for more details. | |
15 | ** | |
16 | ** The GNU General Public License is available from http://www.gnu.org/ or | |
17 | ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, | |
18 | ** MA 02111-1307, USA. | |
19 | ** | |
155af98d | 20 | ** For information on splint: info@splint.org |
21 | ** To report a bug: splint-bug@splint.org | |
11db3170 | 22 | ** For more information: http://www.splint.org |
616915dd | 23 | */ |
24 | /* | |
25 | ** termNodeList.c | |
26 | ** | |
27 | ** based on list_template.c | |
28 | ** | |
29 | ** where T has T_equal (or change this) and T_unparse | |
30 | */ | |
31 | ||
1b8ae690 | 32 | # include "splintMacros.nf" |
b73d1009 | 33 | # include "basic.h" |
616915dd | 34 | |
35 | termNodeList termNodeList_new () | |
36 | { | |
37 | termNodeList s = (termNodeList) dmalloc (sizeof (*s)); | |
38 | ||
39 | s->nelements = 0; | |
40 | s->nspacelow = termNodeListGROWLOW; | |
41 | s->nspacehigh = termNodeListGROWHI; | |
42 | s->elementsroot = (termNode *) dmalloc (sizeof (*s->elements) * (s->nspacelow + s->nspacehigh)); | |
43 | s->elements = s->elementsroot + termNodeListGROWLOW; | |
44 | s->current = 0; | |
45 | ||
46 | return (s); | |
47 | } | |
48 | ||
49 | static void | |
50 | termNodeList_grow (termNodeList s) | |
51 | { | |
52 | int i; | |
53 | termNode *newelements = (termNode *) dmalloc (sizeof (*newelements) | |
54 | * (s->nelements + termNodeListBASESIZE)); | |
55 | ||
56 | for (i = 0; i < s->nelements; i++) | |
57 | { | |
58 | newelements[i + termNodeListGROWLOW] = s->elements[i]; | |
59 | } | |
60 | ||
61 | sfree (s->elementsroot); | |
62 | ||
63 | s->nspacelow = termNodeListGROWLOW; | |
64 | s->nspacehigh = termNodeListGROWHI; | |
65 | ||
66 | s->elementsroot = newelements; | |
67 | s->elements = s->elementsroot + s->nspacelow; | |
68 | } | |
69 | ||
70 | void | |
71 | termNodeList_addh (termNodeList s, termNode el) | |
72 | { | |
73 | llassert (termNodeListGROWHI > 0); | |
74 | ||
75 | if (s->nspacehigh <= 0) | |
76 | termNodeList_grow (s); | |
77 | ||
78 | s->nspacehigh--; | |
79 | s->elements[s->nelements] = el; | |
80 | s->nelements++; | |
81 | } | |
82 | ||
83 | termNodeList | |
84 | termNodeList_push (termNodeList s, termNode el) | |
85 | { | |
86 | termNodeList_addh (s, el); | |
87 | return s; | |
88 | } | |
89 | ||
90 | void | |
91 | termNodeList_addl (termNodeList s, termNode el) | |
92 | { | |
93 | llassert (termNodeListGROWLOW > 0); | |
94 | ||
95 | if (s->nspacelow <= 0) | |
96 | termNodeList_grow (s); | |
97 | ||
98 | s->nspacelow--; | |
99 | s->elements--; | |
100 | s->elements[0] = el; | |
101 | s->current++; | |
102 | s->nelements++; | |
103 | } | |
104 | ||
105 | void | |
106 | termNodeList_reset (termNodeList s) | |
107 | { | |
108 | s->current = 0; | |
109 | } | |
110 | ||
111 | void | |
112 | termNodeList_finish (termNodeList s) | |
113 | { | |
114 | s->current = s->nelements - 1; | |
115 | } | |
116 | ||
117 | void | |
118 | termNodeList_advance (termNodeList s) | |
119 | { | |
120 | s->current++; | |
121 | llassert (s->current < s->nelements); | |
122 | } | |
123 | ||
124 | /*@exposed@*/ termNode | |
125 | termNodeList_head (termNodeList s) | |
126 | { | |
127 | llassert (s->nelements > 0); | |
128 | return (s->elements[0]); | |
129 | } | |
130 | ||
131 | /*@only@*/ termNodeList | |
132 | termNodeList_copy (termNodeList s) | |
133 | { | |
134 | termNodeList r = termNodeList_new (); | |
135 | ||
136 | termNodeList_elements (s, x) | |
137 | { | |
138 | termNodeList_addh (r, termNode_copySafe (x)); | |
139 | } end_termNodeList_elements; | |
140 | ||
141 | return r; | |
142 | } | |
143 | ||
144 | /*@exposed@*/ termNode | |
145 | termNodeList_current (termNodeList s) | |
146 | { | |
147 | llassert (!(s->current >= s->nelements)); | |
148 | return (s->elements[s->current]); | |
149 | } | |
150 | ||
151 | termNode | |
152 | termNodeList_getN (termNodeList s, int n) | |
153 | { | |
154 | llassert (n >= 0 && n < s->nelements); | |
155 | ||
156 | return (s->elements[n]); | |
157 | } | |
158 | ||
159 | /*@only@*/ cstring | |
160 | termNodeList_unparse (termNodeList s) | |
161 | { | |
162 | bool first = TRUE; | |
163 | cstring st = cstring_undefined; | |
164 | ||
165 | termNodeList_elements (s, current) | |
166 | { | |
167 | if (first) | |
168 | { | |
169 | st = termNode_unparse (current); | |
170 | first = FALSE; | |
171 | } | |
172 | else | |
173 | st = message ("%q, %q", st, termNode_unparse (current)); | |
174 | } end_termNodeList_elements; | |
175 | ||
176 | return st; | |
177 | } | |
178 | ||
179 | /*@only@*/ cstring | |
180 | termNodeList_unparseTail (termNodeList s) | |
181 | { | |
182 | bool head = TRUE; | |
183 | bool first = TRUE; | |
184 | cstring st = cstring_undefined; | |
185 | ||
186 | termNodeList_elements (s, current) | |
187 | { | |
188 | if (head) | |
189 | { | |
190 | head = FALSE; | |
191 | } | |
192 | else | |
193 | { | |
194 | if (first) | |
195 | { | |
196 | st = termNode_unparse (current); | |
197 | first = FALSE; | |
198 | } | |
199 | else | |
200 | st = message ("%q, %q", st, termNode_unparse (current)); | |
201 | } | |
202 | } end_termNodeList_elements; | |
203 | ||
204 | return st; | |
205 | } | |
206 | ||
207 | /*@only@*/ cstring | |
208 | termNodeList_unparseToCurrent (termNodeList s) | |
209 | { | |
210 | int i; | |
211 | cstring st = cstring_undefined; | |
212 | ||
213 | for (i = 0; i < s->current; i++) | |
214 | { | |
215 | termNode current = s->elements[i]; | |
216 | ||
217 | if (i == 0) | |
218 | st = termNode_unparse (current); | |
219 | else | |
220 | st = message ("%q, %q", st, termNode_unparse (current)); | |
221 | } | |
222 | ||
223 | return st; | |
224 | } | |
225 | ||
226 | /*@only@*/ cstring | |
227 | termNodeList_unparseSecondToCurrent (termNodeList s) | |
228 | { | |
229 | int i; | |
230 | cstring st = cstring_undefined; | |
231 | ||
232 | for (i = 1; i < s->current; i++) | |
233 | { | |
234 | termNode current = s->elements[i]; | |
235 | ||
236 | if (i == 1) | |
237 | { | |
238 | st = termNode_unparse (current); | |
239 | } | |
240 | else | |
241 | { | |
242 | st = message ("%q, %q", st, termNode_unparse (current)); | |
243 | } | |
244 | } | |
245 | ||
246 | return st; | |
247 | } | |
248 | ||
249 | void | |
250 | termNodeList_free (termNodeList s) | |
251 | { | |
252 | int i; | |
253 | for (i = 0; i < s->nelements; i++) | |
254 | { | |
255 | termNode_free (s->elements[i]); | |
256 | } | |
257 | ||
258 | sfree (s->elementsroot); | |
259 | sfree (s); | |
260 | } |