]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | ** Splint - annotation-assisted static program checker | |
3 | ** Copyright (C) 1994-2003 University of Virginia, | |
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 | ** | |
20 | ** For information on splint: info@splint.org | |
21 | ** To report a bug: splint-bug@splint.org | |
22 | ** For more information: http://www.splint.org | |
23 | */ | |
24 | /* | |
25 | ** exprNodeList.c | |
26 | ** | |
27 | ** based on list_template.c | |
28 | ** | |
29 | ** where T has T_equal (or change this) and T_unparse | |
30 | */ | |
31 | ||
32 | # include "splintMacros.nf" | |
33 | # include "basic.h" | |
34 | ||
35 | /*@only@*/ exprNodeList | |
36 | exprNodeList_new () | |
37 | { | |
38 | exprNodeList s = (exprNodeList) dmalloc (sizeof (*s)); | |
39 | ||
40 | s->nelements = 0; | |
41 | s->nspace = exprNodeListBASESIZE; | |
42 | s->elements = (exprNode *) | |
43 | dmalloc (sizeof (*s->elements) * exprNodeListBASESIZE); | |
44 | s->current = 0; | |
45 | ||
46 | return (s); | |
47 | } | |
48 | ||
49 | static void | |
50 | exprNodeList_grow (exprNodeList s) | |
51 | { | |
52 | int i; | |
53 | exprNode *newelements; | |
54 | int numnew; | |
55 | ||
56 | if (s->nelements < exprNodeListBASESIZE) | |
57 | { | |
58 | numnew = exprNodeListBASESIZE; | |
59 | } | |
60 | else | |
61 | { | |
62 | numnew = s->nelements; | |
63 | } | |
64 | ||
65 | s->nspace = numnew + s->nspace; | |
66 | ||
67 | newelements = (exprNode *) dmalloc (sizeof (*newelements) * (s->nelements + numnew)); | |
68 | ||
69 | if (newelements == (exprNode *) 0) | |
70 | { | |
71 | llfatalerror (cstring_makeLiteral ("exprNodeList_grow: out of memory!")); | |
72 | } | |
73 | ||
74 | for (i = 0; i < s->nelements; i++) | |
75 | { | |
76 | newelements[i] = s->elements[i]; | |
77 | } | |
78 | ||
79 | sfree (s->elements); | |
80 | s->elements = newelements; | |
81 | } | |
82 | ||
83 | void exprNodeList_addh (exprNodeList s, /*@only@*/ exprNode el) | |
84 | { | |
85 | llassert (exprNodeListBASESIZE > 0); | |
86 | ||
87 | if (s->nspace <= 0) | |
88 | exprNodeList_grow (s); | |
89 | ||
90 | s->nspace--; | |
91 | s->elements[s->nelements] = el; | |
92 | s->nelements++; | |
93 | } | |
94 | ||
95 | void exprNodeList_reset (exprNodeList s) | |
96 | { | |
97 | s->current = 0; | |
98 | } | |
99 | ||
100 | void exprNodeList_advance (exprNodeList s) | |
101 | { | |
102 | s->current++; | |
103 | llassert (s->current <= s->nelements); | |
104 | } | |
105 | ||
106 | /*@observer@*/ exprNode exprNodeList_head (exprNodeList s) | |
107 | { | |
108 | llassert (s->nelements > 0); | |
109 | return (s->elements[0]); | |
110 | } | |
111 | ||
112 | /*@observer@*/ exprNode exprNodeList_current (exprNodeList s) | |
113 | { | |
114 | llassert (s->current >= 0 && s->current < s->nelements); | |
115 | return (s->elements[s->current]); | |
116 | } | |
117 | ||
118 | exprNode exprNodeList_getN (exprNodeList s, int n) | |
119 | { | |
120 | llassert (n >= 0 && n < s->nelements); | |
121 | return (s->elements[n]); | |
122 | } | |
123 | ||
124 | /*@only@*/ exprNodeList exprNodeList_singleton (/*@only@*/ exprNode e) | |
125 | { | |
126 | exprNodeList s = (exprNodeList) dmalloc (sizeof (*s)); | |
127 | ||
128 | s->nelements = 1; | |
129 | s->nspace = exprNodeListBASESIZE - 1; | |
130 | s->elements = (exprNode *) dmalloc (sizeof (*s->elements) * exprNodeListBASESIZE); | |
131 | s->elements[0] = e; | |
132 | s->current = 0; | |
133 | ||
134 | DPRINTF (("List: %s", exprNode_unparse (e))); | |
135 | return (s); | |
136 | } | |
137 | ||
138 | exprNodeList exprNodeList_push (/*@returned@*/ exprNodeList args, /*@only@*/ exprNode e) | |
139 | { | |
140 | exprNodeList_addh (args, e); | |
141 | return (args); | |
142 | } | |
143 | ||
144 | /*@exposed@*/ exprNode | |
145 | exprNodeList_nth (exprNodeList args, int n) | |
146 | { | |
147 | if (n >= exprNodeList_size (args) || n < 0) | |
148 | { | |
149 | llcontbug (message ("exprNodeList_nth: out of range: %q arg %d\n", | |
150 | exprNodeList_unparse (args), n)); | |
151 | return exprNode_undefined; | |
152 | } | |
153 | ||
154 | return args->elements[n]; | |
155 | } | |
156 | ||
157 | /*@only@*/ cstring | |
158 | exprNodeList_unparse (exprNodeList s) | |
159 | { | |
160 | int i; | |
161 | cstring st = cstring_undefined; | |
162 | ||
163 | for (i = 0; i < s->nelements; i++) | |
164 | { | |
165 | if (i == 0) | |
166 | { | |
167 | st = cstring_copy (exprNode_unparse (s->elements[i])); | |
168 | } | |
169 | else | |
170 | st = message ("%q, %s", st, exprNode_unparse (s->elements[i])); | |
171 | } | |
172 | ||
173 | return st; | |
174 | } | |
175 | ||
176 | void | |
177 | exprNodeList_free (exprNodeList s) | |
178 | { | |
179 | int i; | |
180 | ||
181 | for (i = 0; i < s->nelements; i++) | |
182 | { | |
183 | exprNode_free (s->elements[i]); | |
184 | } | |
185 | ||
186 | sfree (s->elements); | |
187 | sfree (s); | |
188 | } | |
189 | ||
190 | void | |
191 | exprNodeList_freeShallow (/*@only@*/ exprNodeList s) | |
192 | { | |
193 | int i; | |
194 | ||
195 | for (i = 0; i < s->nelements; i++) | |
196 | { | |
197 | exprNode_freeShallow (s->elements[i]); | |
198 | } | |
199 | ||
200 | sfree (s->elements); | |
201 | sfree (s); | |
202 | } | |
203 |