]>
Commit | Line | Data |
---|---|---|
616915dd | 1 | /* |
11db3170 | 2 | ** Splint - annotation-assisted static program checker |
77d37419 | 3 | ** Copyright (C) 1994-2002 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 | ** idDecl.c | |
26 | */ | |
27 | ||
1b8ae690 | 28 | # include "splintMacros.nf" |
616915dd | 29 | # include "basic.h" |
30 | ||
31 | /*@only@*/ idDecl | |
28bf4b0b | 32 | idDecl_createClauses (/*@only@*/ cstring s, /*@only@*/ qtype t, /*@only@*/ functionClauseList clauses) |
616915dd | 33 | { |
34 | idDecl d = (idDecl) dmalloc (sizeof (*d)); | |
35 | ||
36 | d->id = s; | |
37 | d->typ = t; | |
28bf4b0b | 38 | d->clauses = clauses; |
616915dd | 39 | |
40 | return (d); | |
41 | } | |
42 | ||
28bf4b0b | 43 | /*@only@*/ idDecl |
44 | idDecl_create (/*@only@*/ cstring s, /*@only@*/ qtype t) | |
45 | { | |
46 | return idDecl_createClauses (s, t, functionClauseList_undefined); | |
47 | } | |
48 | ||
616915dd | 49 | void |
50 | idDecl_free (idDecl t) | |
51 | { | |
52 | if (idDecl_isDefined (t)) | |
53 | { | |
bb7c2085 | 54 | /*@i523 functionClauseList_free (t->clauses); */ /* evans 2002-01-03: splint catches this now! */ |
616915dd | 55 | qtype_free (t->typ); |
bb7c2085 | 56 | cstring_free (t->id); |
57 | ||
58 | /*@-compdestroy@*/ sfree (t); /*@=compdestroy@*/ | |
616915dd | 59 | } |
60 | } | |
61 | ||
62 | cstring | |
63 | idDecl_unparse (idDecl d) | |
64 | { | |
65 | if (idDecl_isDefined (d)) | |
66 | { | |
28bf4b0b | 67 | if (functionClauseList_isDefined (d->clauses)) |
68 | { | |
69 | return (message ("%s : %q / %q", d->id, qtype_unparse (d->typ), | |
70 | functionClauseList_unparse (d->clauses))); | |
71 | } | |
72 | else | |
73 | { | |
74 | return (message ("%s : %q", d->id, qtype_unparse (d->typ))); | |
75 | } | |
76 | } | |
77 | else | |
78 | { | |
79 | return (cstring_makeLiteral ("<undefined id>")); | |
80 | } | |
81 | } | |
82 | ||
495af944 | 83 | /*drl added 04-22-2002 |
84 | for code generate code | |
85 | */ | |
86 | ||
87 | cstring | |
88 | idDecl_unparseCNoType (idDecl d) | |
89 | { | |
90 | ||
91 | if (ctype_isFunction ( qtype_getType (d->typ ) ) ) | |
92 | { | |
93 | ||
94 | return idDecl_unparseC(d); | |
95 | } | |
96 | if (idDecl_isDefined (d)) | |
97 | { | |
98 | return (message ("%s", d->id)); | |
99 | } | |
100 | else | |
101 | { | |
102 | llassert(FALSE); | |
103 | return (cstring_makeLiteral ("<undefined id>")); | |
104 | } | |
105 | } | |
106 | ||
28bf4b0b | 107 | cstring |
108 | idDecl_unparseC (idDecl d) | |
109 | { | |
110 | if (idDecl_isDefined (d)) | |
111 | { | |
495af944 | 112 | if (qtype_isFixedArray(d->typ) ) |
113 | { | |
114 | cstring ret, arType, size; | |
115 | ||
116 | arType = qtype_unparseArrayType(d->typ); | |
117 | size = qtype_unparseArraySize(d->typ); | |
118 | ret = message ("%q %s %q", arType, d->id, size); | |
119 | return ret; | |
120 | } | |
121 | else if (qtype_isArray(d->typ) ) | |
122 | { | |
123 | cstring ret, arType; | |
124 | ||
125 | arType = qtype_unparseArrayType(d->typ); | |
126 | ret = message ("%q %s[]", arType, d->id); | |
127 | return ret; | |
128 | } | |
129 | ||
130 | else if (ctype_isFunction ( qtype_getType (d->typ ) ) ) | |
131 | { | |
132 | cstring st, tmp, cQuals, ret; | |
133 | ||
134 | st = cstring_copy (d->id); | |
135 | tmp = ctype_unparseFunction ( qtype_getType (d->typ ), st); | |
136 | if ( qualList_isDefined(idDecl_getQuals (d) ) ) | |
137 | { | |
138 | cQuals = qualList_unparse(idDecl_getQuals(d) ); | |
139 | } | |
140 | else | |
141 | { | |
142 | cQuals = cstring_undefined; | |
143 | } | |
144 | ||
145 | ret = message("%q %q", cQuals, tmp); | |
146 | return ret; | |
147 | } | |
148 | else | |
149 | { | |
150 | uentry ue; | |
151 | fileloc loc; | |
152 | typeId tId; | |
153 | ||
154 | tId = ctype_typeId(qtype_getType(d->typ) ); | |
155 | ||
156 | ue = usymtab_getTypeEntry (tId); | |
157 | ||
158 | loc = uentry_whereDeclared(ue) ; | |
159 | ||
160 | DPRINTF(( message("id = %s ue: %s at %s", d->id, uentry_unparse(ue), fileloc_unparse(loc) ) )); | |
161 | return (message ("%q %s", qtypetryToPrintStruct (d->typ), d->id)); | |
162 | } | |
163 | } | |
164 | else | |
165 | { | |
166 | return (cstring_makeLiteral ("<undefined id>")); | |
167 | } | |
168 | } | |
169 | ||
170 | ||
171 | /*drl added 04-24-2002*/ | |
172 | cstring | |
173 | idDecl_unparseCLoc (idDecl d, fileloc dLoc) | |
174 | { | |
175 | if (idDecl_isDefined (d)) | |
176 | { | |
177 | if (qtype_isFixedArray(d->typ) ) | |
178 | { | |
179 | cstring ret, arType, size; | |
180 | ||
181 | arType = qtype_unparseArrayType(d->typ); | |
182 | size = qtype_unparseArraySize(d->typ); | |
183 | ret = message ("%q %s %q", arType, d->id, size); | |
184 | return ret; | |
185 | } | |
186 | else if (qtype_isArray(d->typ) ) | |
187 | { | |
188 | cstring ret, arType; | |
189 | ||
190 | arType = qtype_unparseArrayType(d->typ); | |
191 | ret = message ("%q %s[]", arType, d->id); | |
192 | return ret; | |
193 | } | |
194 | else if (ctype_isFunctionPointer ( qtype_getType (d->typ ) ) ) | |
195 | { | |
196 | cstring st, tmp, cQuals, ret; | |
197 | ||
198 | st = cstring_copy (d->id); | |
199 | ||
200 | if ( qualList_isDefined(idDecl_getQuals (d) ) ) | |
201 | { | |
202 | cQuals = qualList_unparse(idDecl_getQuals(d) ); | |
203 | } | |
204 | else | |
205 | { | |
206 | cQuals = cstring_undefined; | |
207 | } | |
208 | ||
209 | tmp = ctype_unparseFunctionPointer ( qtype_getType (d->typ ), cQuals, st); | |
210 | ||
211 | ret = message("%q", tmp); | |
212 | cstring_free(cQuals); | |
213 | return ret; | |
214 | } | |
215 | else if (ctype_isFunction ( qtype_getType (d->typ ) ) ) | |
216 | { | |
217 | cstring st, tmp, cQuals, ret; | |
218 | ||
219 | st = cstring_copy (d->id); | |
220 | tmp = ctype_unparseFunction ( qtype_getType (d->typ ), st); | |
221 | if ( qualList_isDefined(idDecl_getQuals (d) ) ) | |
222 | { | |
223 | cQuals = qualList_unparse(idDecl_getQuals(d) ); | |
224 | } | |
225 | else | |
226 | { | |
227 | cQuals = cstring_undefined; | |
228 | } | |
229 | ||
230 | ret = message("%q %q", cQuals, tmp); | |
231 | return ret; | |
232 | } | |
233 | else | |
234 | { | |
235 | uentry ue; | |
236 | fileloc loc; | |
237 | typeId tId; | |
238 | ||
239 | if (ctype_isUser(qtype_getType(d->typ) ) ) | |
240 | { | |
241 | tId = ctype_typeId(qtype_getType(d->typ) ); | |
242 | ||
243 | ue = usymtab_getTypeEntry (tId); | |
244 | ||
245 | loc = uentry_whereDeclared(ue) ; | |
246 | ||
247 | DPRINTF(( message("id = %s ue: %s at %s", d->id, uentry_unparse(ue), fileloc_unparse(loc) ) )); | |
248 | ||
249 | if (fileloc_sameFileAndLine (loc, dLoc) ) | |
250 | return (message ("%q %s", qtypetryToPrintStruct (d->typ), d->id)); | |
251 | } | |
252 | ||
253 | return (message ("%s %s", qtype_unparse (d->typ), d->id)); | |
254 | ||
255 | } /*end if (ctype_isFunction ...*/ | |
616915dd | 256 | } |
257 | else | |
258 | { | |
259 | return (cstring_makeLiteral ("<undefined id>")); | |
260 | } | |
261 | } | |
262 | ||
495af944 | 263 | |
616915dd | 264 | /*@observer@*/ cstring |
265 | idDecl_observeId (idDecl d) | |
266 | { | |
267 | if (idDecl_isDefined (d)) | |
268 | { | |
269 | return (d->id); | |
270 | } | |
271 | else | |
272 | { | |
273 | return cstring_undefined; | |
274 | } | |
275 | } | |
276 | ||
277 | qtype | |
278 | idDecl_getTyp (idDecl d) | |
279 | { | |
280 | llassert (idDecl_isDefined (d)); | |
281 | ||
282 | return d->typ; | |
283 | } | |
284 | ||
285 | ctype | |
286 | idDecl_getCtype (idDecl d) | |
287 | { | |
288 | if (idDecl_isDefined (d)) | |
289 | { | |
290 | return (qtype_getType (d->typ)); | |
291 | } | |
292 | else | |
293 | { | |
294 | return ctype_unknown; | |
295 | } | |
296 | } | |
297 | ||
298 | qualList | |
299 | idDecl_getQuals (idDecl d) | |
300 | { | |
301 | if (idDecl_isDefined (d)) | |
302 | { | |
303 | return (qtype_getQuals (d->typ)); | |
304 | } | |
305 | else | |
306 | { | |
307 | return qualList_undefined; | |
308 | } | |
309 | } | |
310 | ||
28bf4b0b | 311 | functionClauseList |
312 | idDecl_getClauses (idDecl d) | |
313 | { | |
314 | if (idDecl_isDefined (d)) | |
315 | { | |
316 | return (d->clauses); | |
317 | } | |
318 | else | |
319 | { | |
320 | return functionClauseList_undefined; | |
321 | } | |
322 | } | |
323 | ||
616915dd | 324 | void |
325 | idDecl_addQual (idDecl d, qual q) | |
326 | { | |
327 | llassert (idDecl_isDefined (d)); | |
328 | ||
329 | (void) qtype_addQual (d->typ, q); | |
330 | } | |
331 | ||
332 | void | |
333 | idDecl_setTyp (idDecl d, qtype c) | |
334 | { | |
335 | llassert (idDecl_isDefined (d)); | |
336 | ||
337 | qtype_free (d->typ); | |
338 | d->typ = c; | |
339 | } | |
340 | ||
341 | idDecl | |
342 | idDecl_replaceCtype (/*@returned@*/ idDecl d, ctype c) | |
343 | { | |
344 | llassert (idDecl_isDefined (d)); | |
345 | ||
077d4458 | 346 | DPRINTF (("Replace type: %s / %s", idDecl_unparse (d), ctype_unparse (c))); |
616915dd | 347 | qtype_setType (d->typ, c); |
348 | return d; | |
349 | } | |
350 | ||
351 | idDecl | |
352 | idDecl_fixBase (/*@returned@*/ idDecl t, qtype b) | |
353 | { | |
354 | llassert (idDecl_isDefined (t)); | |
355 | ||
356 | t->typ = qtype_newQbase (t->typ, b); | |
357 | return t; | |
358 | } | |
359 | ||
360 | idDecl | |
361 | idDecl_fixParamBase (/*@returned@*/ idDecl t, qtype b) | |
362 | { | |
363 | qtype q; | |
364 | ctype c; | |
365 | ||
366 | llassert (idDecl_isDefined (t)); | |
367 | ||
368 | q = qtype_newQbase (t->typ, b); | |
369 | c = qtype_getType (q); | |
370 | ||
371 | /* | |
372 | ** For some reason, C adds an implicit pointer to function | |
373 | ** parameters. It is "optional" syntax. | |
374 | */ | |
375 | ||
376 | if (ctype_isFunction (c) && !ctype_isPointer (c)) | |
377 | { | |
378 | qtype_setType (q, ctype_makePointer (c)); | |
379 | } | |
380 | ||
381 | t->typ = q; | |
11db3170 | 382 | /* Splint thinks t->typ is kept. */ |
616915dd | 383 | /*@-compmempass@*/ return t; /*@=compmempass@*/ |
384 | } | |
385 | ||
386 | idDecl | |
387 | idDecl_expectFunction (/*@returned@*/ idDecl d) | |
388 | { | |
389 | llassert (idDecl_isDefined (d)); | |
390 | ||
391 | qtype_setType (d->typ, ctype_expectFunction (qtype_getType (d->typ))); | |
392 | return d; | |
393 | } | |
28bf4b0b | 394 | |
e8b84478 | 395 | /* |
396 | ** evans 2002-02-09: This is a bit of a kludge, but we | |
397 | ** need it to fix declarations like int (*p)[]; | |
398 | */ | |
399 | ||
400 | void | |
401 | idDecl_notExpectingFunction (/*@returned@*/ idDecl d) | |
402 | { | |
403 | if (idDecl_isDefined (d)) | |
404 | { | |
405 | ctype ct = qtype_getType (d->typ); | |
406 | ||
407 | if (ctype_isExpFcn (ct)) | |
408 | { | |
409 | qtype_setType (d->typ, ctype_dontExpectFunction (ct)); | |
410 | } | |
411 | } | |
412 | } | |
413 | ||
28bf4b0b | 414 | void |
415 | idDecl_addClauses (idDecl d, functionClauseList clauses) | |
416 | { | |
417 | llassert (idDecl_isDefined (d)); | |
418 | llassert (functionClauseList_isUndefined (d->clauses)); | |
419 | d->clauses = clauses; | |
420 | } |