]>
Commit | Line | Data |
---|---|---|
616915dd | 1 | /* |
11db3170 | 2 | ** Splint - annotation-assisted static program checker |
c59f5181 | 3 | ** Copyright (C) 1994-2003 University of Virginia, |
65f973be | 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 |
65f973be | 23 | */ |
24 | ||
25 | /* | |
26 | ** constraintTerm.c | |
616915dd | 27 | */ |
28 | ||
b7b694d6 | 29 | /* #define DEBUGPRINT 1 */ |
616915dd | 30 | |
31 | # include <ctype.h> /* for isdigit */ | |
1b8ae690 | 32 | # include "splintMacros.nf" |
616915dd | 33 | # include "basic.h" |
34 | # include "cgrammar.h" | |
35 | # include "cgrammar_tokens.h" | |
36 | ||
37 | # include "exprChecks.h" | |
616915dd | 38 | # include "exprNodeSList.h" |
39 | ||
f0171cff | 40 | bool constraintTerm_isDefined (constraintTerm t) |
990ec868 | 41 | { |
42 | return t != NULL; | |
43 | } | |
44 | ||
d46ce6a4 | 45 | void constraintTerm_free (/*@only@*/ constraintTerm term) |
46 | { | |
990ec868 | 47 | llassert (constraintTerm_isDefined (term)); |
48 | ||
d46ce6a4 | 49 | fileloc_free (term->loc); |
50 | ||
51 | switch (term->kind) | |
52 | { | |
2a6e9c30 | 53 | case CTT_EXPR: |
d46ce6a4 | 54 | /* we don't free an exprNode*/ |
55 | break; | |
2a6e9c30 | 56 | case CTT_SREF: |
d46ce6a4 | 57 | /* sref */ |
795e7f34 | 58 | sRef_free (term->value.sref); |
d46ce6a4 | 59 | break; |
2a6e9c30 | 60 | case CTT_INTLITERAL: |
d46ce6a4 | 61 | /* don't free an int */ |
62 | break; | |
2a6e9c30 | 63 | case CTT_ERRORBADCONSTRAINTTERMTYPE: |
d46ce6a4 | 64 | default: |
65 | /* type was set incorrectly */ | |
66 | llcontbug (message("constraintTerm_free type was set incorrectly")); | |
67 | } | |
b7b694d6 | 68 | |
2a6e9c30 | 69 | term->kind = CTT_ERRORBADCONSTRAINTTERMTYPE; |
d46ce6a4 | 70 | free (term); |
71 | } | |
616915dd | 72 | |
4ab867d6 | 73 | /*@only@*/ static/*@out@*/ constraintTerm new_constraintTermExpr (void) |
616915dd | 74 | { |
75 | constraintTerm ret; | |
76 | ret = dmalloc (sizeof (* ret ) ); | |
bb25bea6 | 77 | ret->value.intlit = 0; |
616915dd | 78 | return ret; |
79 | } | |
80 | ||
81 | ||
82 | bool constraintTerm_isIntLiteral (constraintTerm term) | |
83 | { | |
dc92450f | 84 | llassert(term != NULL); |
616915dd | 85 | |
2a6e9c30 | 86 | if (term->kind == CTT_INTLITERAL) |
616915dd | 87 | return TRUE; |
88 | ||
89 | return FALSE; | |
90 | } | |
91 | ||
d30bc0c7 | 92 | |
93 | bool constraintTerm_isInitBlock (/*@observer@*/ /*@temp@*/ constraintTerm c) /*@*/ | |
94 | { | |
95 | llassert (c != NULL); | |
2a6e9c30 | 96 | |
97 | if (c->kind == CTT_EXPR) | |
d30bc0c7 | 98 | { |
2a6e9c30 | 99 | if (exprNode_isInitBlock (c->value.expr)) |
d30bc0c7 | 100 | { |
101 | return TRUE; | |
102 | } | |
103 | } | |
104 | return FALSE; | |
105 | } | |
106 | ||
107 | ||
a779b61e | 108 | bool constraintTerm_isExprNode (/*@observer@*/ /*@temp@*/ constraintTerm c) /*@*/ |
109 | { | |
110 | llassert (c != NULL); | |
2a6e9c30 | 111 | |
112 | if (c->kind == CTT_EXPR) | |
a779b61e | 113 | { |
114 | return TRUE; | |
115 | } | |
116 | return FALSE; | |
117 | } | |
118 | ||
749f175a | 119 | /*@access exprNode@*/ |
d30bc0c7 | 120 | int constraintTerm_getInitBlockLength (/*@observer@*/ /*@temp@*/ constraintTerm c) /*@*/ |
121 | { | |
2a6e9c30 | 122 | exprNodeList list; |
d30bc0c7 | 123 | int ret; |
124 | llassert (c != NULL); | |
125 | llassert (constraintTerm_isInitBlock (c) ); | |
2a6e9c30 | 126 | llassert (c->kind == CTT_EXPR); |
d30bc0c7 | 127 | |
128 | llassert(exprNode_isDefined(c->value.expr) ); | |
129 | ||
130 | if (exprNode_isUndefined(c->value.expr) ) | |
131 | { | |
132 | return 1; | |
133 | } | |
134 | ||
135 | if (c->value.expr->edata == exprData_undefined) | |
136 | { | |
137 | return 1; | |
138 | } | |
139 | list = exprData_getArgs(c->value.expr->edata); | |
140 | ||
141 | ret = exprNodeList_size(list); | |
142 | ||
143 | return ret; | |
144 | } | |
749f175a | 145 | /*@noaccess exprNode@*/ |
d30bc0c7 | 146 | |
147 | ||
dc92450f | 148 | bool constraintTerm_isStringLiteral (constraintTerm c) /*@*/ |
616915dd | 149 | { |
dc92450f | 150 | llassert (c != NULL); |
2a6e9c30 | 151 | if (c->kind == CTT_EXPR) |
616915dd | 152 | { |
153 | if (exprNode_knownStringValue(c->value.expr) ) | |
154 | { | |
155 | return TRUE; | |
156 | } | |
157 | } | |
158 | return FALSE; | |
159 | } | |
160 | ||
d30bc0c7 | 161 | |
162 | ||
616915dd | 163 | cstring constraintTerm_getStringLiteral (constraintTerm c) |
164 | { | |
dc92450f | 165 | llassert (c != NULL); |
616915dd | 166 | llassert (constraintTerm_isStringLiteral (c) ); |
2a6e9c30 | 167 | llassert (c->kind == CTT_EXPR); |
616915dd | 168 | |
dc92450f | 169 | return (cstring_copy ( multiVal_forceString (exprNode_getValue (c->value.expr) ) ) ); |
616915dd | 170 | } |
171 | ||
4ab867d6 | 172 | constraintTerm constraintTerm_simplify (/*@returned@*/ constraintTerm term) /*@modifies term@*/ |
616915dd | 173 | { |
2a6e9c30 | 174 | if (term->kind == CTT_EXPR) |
616915dd | 175 | { |
176 | if ( exprNode_knownIntValue (term->value.expr ) ) | |
177 | { | |
178 | long int temp; | |
28bf4b0b | 179 | |
616915dd | 180 | temp = exprNode_getLongValue (term->value.expr); |
dc92450f | 181 | term->value.intlit = (int)temp; |
2a6e9c30 | 182 | term->kind = CTT_INTLITERAL; |
616915dd | 183 | } |
184 | } | |
185 | return term; | |
186 | } | |
187 | ||
188 | fileloc constraintTerm_getFileloc (constraintTerm t) | |
189 | { | |
990ec868 | 190 | llassert (constraintTerm_isDefined (t)); |
616915dd | 191 | return (fileloc_copy (t->loc) ); |
192 | } | |
193 | ||
a8e557d3 | 194 | constraintTermType constraintTerm_getKind (constraintTerm t) |
195 | { | |
196 | llassert (constraintTerm_isDefined(t) ); | |
197 | ||
198 | return (t->kind); | |
199 | } | |
200 | ||
201 | /*@exposed@*/ sRef constraintTerm_getSRef (constraintTerm t) | |
202 | { | |
203 | llassert (constraintTerm_isDefined(t) ); | |
2a6e9c30 | 204 | llassert (t->kind == CTT_SREF); |
a8e557d3 | 205 | |
206 | return (t->value.sref); | |
207 | } | |
208 | ||
517a2db3 | 209 | /*@only@*/ constraintTerm constraintTerm_makeExprNode (/*@dependent@*/ exprNode e) |
616915dd | 210 | { |
517a2db3 | 211 | constraintTerm ret = new_constraintTermExpr (); |
212 | ret->loc = fileloc_copy (exprNode_loc (e)); | |
616915dd | 213 | ret->value.expr = e; |
2a6e9c30 | 214 | ret->kind = CTT_EXPR; |
517a2db3 | 215 | ret = constraintTerm_simplify (ret); |
616915dd | 216 | return ret; |
217 | } | |
218 | ||
517a2db3 | 219 | /*@only@*/ constraintTerm constraintTerm_makesRef (/*@temp@*/ /*@observer@*/ sRef s) |
616915dd | 220 | { |
221 | constraintTerm ret = new_constraintTermExpr(); | |
222 | ret->loc = fileloc_undefined; | |
4ab867d6 | 223 | ret->value.sref = sRef_saveCopy(s); |
2a6e9c30 | 224 | ret->kind = CTT_SREF; |
616915dd | 225 | ret = constraintTerm_simplify(ret); |
226 | return ret; | |
227 | } | |
228 | ||
795e7f34 | 229 | |
230 | ||
616915dd | 231 | constraintTerm constraintTerm_copy (constraintTerm term) |
232 | { | |
233 | constraintTerm ret; | |
234 | ret = new_constraintTermExpr(); | |
235 | ret->loc = fileloc_copy (term->loc); | |
795e7f34 | 236 | |
237 | switch (term->kind) | |
238 | { | |
2a6e9c30 | 239 | case CTT_EXPR: |
795e7f34 | 240 | ret->value.expr = term->value.expr; |
241 | break; | |
2a6e9c30 | 242 | case CTT_INTLITERAL: |
795e7f34 | 243 | ret->value.intlit = term->value.intlit; |
244 | break; | |
245 | ||
2a6e9c30 | 246 | case CTT_SREF: |
795e7f34 | 247 | ret->value.sref = sRef_saveCopy(term->value.sref); |
248 | break; | |
249 | default: | |
250 | BADEXIT; | |
251 | } | |
616915dd | 252 | ret->kind = term->kind; |
253 | return ret; | |
254 | } | |
255 | ||
d46ce6a4 | 256 | constraintTerm constraintTerm_setFileloc (/*@returned@*/ constraintTerm term, fileloc loc) |
616915dd | 257 | { |
dc92450f | 258 | llassert(term != NULL); |
d46ce6a4 | 259 | |
28bf4b0b | 260 | if ( fileloc_isDefined( term->loc ) ) |
d46ce6a4 | 261 | fileloc_free(term->loc); |
262 | ||
616915dd | 263 | term->loc = fileloc_copy(loc); |
264 | return term; | |
265 | } | |
266 | ||
267 | ||
d46ce6a4 | 268 | static cstring constraintTerm_getName (constraintTerm term) |
616915dd | 269 | { |
270 | cstring s; | |
271 | s = cstring_undefined; | |
272 | ||
273 | llassert (term != NULL); | |
274 | ||
275 | switch (term->kind) | |
276 | { | |
2a6e9c30 | 277 | case CTT_EXPR: |
332e22fa | 278 | |
616915dd | 279 | s = message ("%s", exprNode_unparse (term->value.expr) ); |
280 | break; | |
2a6e9c30 | 281 | case CTT_INTLITERAL: |
a1fa5e0c | 282 | s = message (" %d ", (int) term->value.intlit); |
616915dd | 283 | break; |
284 | ||
2a6e9c30 | 285 | case CTT_SREF: |
d46ce6a4 | 286 | s = message ("%q", sRef_unparse (term->value.sref) ); |
616915dd | 287 | |
288 | break; | |
c3e695ff | 289 | default: |
290 | BADEXIT; | |
291 | /*@notreached@*/ | |
616915dd | 292 | break; |
293 | } | |
616915dd | 294 | |
c3e695ff | 295 | return s; |
616915dd | 296 | } |
297 | ||
298 | constraintTerm | |
d46ce6a4 | 299 | constraintTerm_doSRefFixBaseParam (/*@returned@*/constraintTerm term, exprNodeList arglist) /*@modifies term@*/ |
616915dd | 300 | { |
301 | llassert (term != NULL); | |
302 | ||
303 | switch (term->kind) | |
304 | { | |
2a6e9c30 | 305 | case CTT_EXPR: |
332e22fa | 306 | |
616915dd | 307 | break; |
2a6e9c30 | 308 | case CTT_INTLITERAL: |
b7b694d6 | 309 | break; |
616915dd | 310 | |
2a6e9c30 | 311 | case CTT_SREF: |
616915dd | 312 | term->value.sref = sRef_fixBaseParam (term->value.sref, arglist); |
616915dd | 313 | break; |
c3e695ff | 314 | default: |
315 | BADEXIT; | |
616915dd | 316 | } |
317 | return term; | |
318 | ||
319 | } | |
320 | ||
2a6e9c30 | 321 | cstring constraintTerm_unparse (constraintTerm term) /*@*/ |
616915dd | 322 | { |
323 | cstring s; | |
324 | s = cstring_undefined; | |
325 | ||
326 | llassert (term != NULL); | |
327 | ||
328 | switch (term->kind) | |
329 | { | |
2a6e9c30 | 330 | case CTT_EXPR: |
332e22fa | 331 | |
a779b61e | 332 | s = message ("%s @ %q", exprNode_unparse (term->value.expr), |
616915dd | 333 | fileloc_unparse (term->loc) ); |
334 | break; | |
2a6e9c30 | 335 | case CTT_INTLITERAL: |
a779b61e | 336 | s = message ("%d", (int)term->value.intlit); |
616915dd | 337 | break; |
338 | ||
2a6e9c30 | 339 | case CTT_SREF: |
a779b61e | 340 | s = message ("%q", sRef_unparseDebug (term->value.sref) ); |
616915dd | 341 | |
342 | break; | |
c3e695ff | 343 | default: |
344 | BADEXIT; | |
616915dd | 345 | } |
346 | ||
347 | return s; | |
348 | } | |
349 | ||
350 | ||
b9904f57 | 351 | constraintTerm constraintTerm_makeIntLiteral (long i) |
616915dd | 352 | { |
353 | constraintTerm ret = new_constraintTermExpr(); | |
354 | ret->value.intlit = i; | |
2a6e9c30 | 355 | ret->kind = CTT_INTLITERAL; |
616915dd | 356 | ret->loc = fileloc_undefined; |
357 | return ret; | |
358 | } | |
359 | ||
360 | bool constraintTerm_canGetValue (constraintTerm term) | |
361 | { | |
2a6e9c30 | 362 | if (term->kind == CTT_INTLITERAL) |
b9904f57 | 363 | { |
364 | return TRUE; | |
365 | } | |
2a6e9c30 | 366 | else if (term->kind == CTT_SREF) |
b9904f57 | 367 | { |
368 | if (sRef_hasValue (term->value.sref)) | |
369 | { | |
370 | multiVal mval = sRef_getValue (term->value.sref); | |
371 | ||
372 | return multiVal_isInt (mval); /* for now, only try to deal with int values */ | |
373 | } | |
374 | else | |
375 | { | |
376 | return FALSE; | |
377 | } | |
378 | } | |
2a6e9c30 | 379 | else if (term->kind == CTT_EXPR) |
b9904f57 | 380 | { |
381 | return FALSE; | |
382 | } | |
616915dd | 383 | else |
b9904f57 | 384 | { |
385 | return FALSE; | |
386 | } | |
616915dd | 387 | } |
388 | ||
ae133592 | 389 | void constraintTerm_setValue (constraintTerm term, long value) |
390 | { | |
391 | if (term->kind == CTT_INTLITERAL) | |
392 | { | |
393 | term->value.intlit = value; | |
394 | } | |
395 | else | |
396 | { | |
397 | BADBRANCH; | |
398 | } | |
399 | } | |
400 | ||
b9904f57 | 401 | long constraintTerm_getValue (constraintTerm term) |
616915dd | 402 | { |
b9904f57 | 403 | llassert (constraintTerm_canGetValue (term)); |
404 | ||
2a6e9c30 | 405 | if (term->kind == CTT_INTLITERAL) |
b9904f57 | 406 | { |
407 | return term->value.intlit; | |
408 | } | |
2a6e9c30 | 409 | else if (term->kind == CTT_SREF) |
b9904f57 | 410 | { |
411 | if (sRef_hasValue (term->value.sref)) | |
412 | { | |
413 | multiVal mval = sRef_getValue (term->value.sref); | |
414 | ||
415 | return multiVal_forceInt (mval); /* for now, only try to deal with int values */ | |
416 | } | |
417 | else | |
418 | { | |
419 | BADBRANCH; | |
420 | } | |
421 | } | |
2a6e9c30 | 422 | else if (term->kind == CTT_EXPR) |
b9904f57 | 423 | { |
424 | BADBRANCH; | |
425 | } | |
426 | else | |
427 | { | |
428 | BADBRANCH; | |
429 | } | |
430 | ||
d30bc0c7 | 431 | BADEXIT; |
616915dd | 432 | } |
433 | ||
e5f31c00 | 434 | /*drl added this 10.30.001 |
435 | */ | |
436 | ||
437 | /*@exposed@*/ exprNode constraintTerm_getExprNode (constraintTerm t) | |
438 | { | |
439 | llassert (t != NULL); | |
440 | ||
2a6e9c30 | 441 | llassert (t->kind == CTT_EXPR); |
e5f31c00 | 442 | |
443 | return t->value.expr; | |
444 | ||
445 | } | |
446 | ||
447 | /*@exposed@*/ sRef constraintTerm_getsRef (constraintTerm t) | |
616915dd | 448 | { |
dc92450f | 449 | llassert (t != NULL); |
2a6e9c30 | 450 | if (t->kind == CTT_EXPR) |
616915dd | 451 | { |
c3e695ff | 452 | return exprNode_getSref(t->value.expr); |
616915dd | 453 | } |
454 | ||
2a6e9c30 | 455 | if (t->kind == CTT_SREF) |
616915dd | 456 | { |
c3e695ff | 457 | return t->value.sref; |
616915dd | 458 | } |
459 | ||
460 | return sRef_undefined; | |
461 | } | |
462 | ||
463 | bool constraintTerm_probSame (constraintTerm term1, constraintTerm term2) | |
464 | { | |
465 | cstring s1, s2; | |
466 | ||
467 | llassert (term1 !=NULL && term2 !=NULL); | |
468 | ||
bb7c2085 | 469 | DPRINTF ((message |
566b8b82 | 470 | ("Comparing srefs for %s and %s ", constraintTerm_getName(term1), constraintTerm_getName(term2) |
616915dd | 471 | ) |
472 | ) | |
473 | ); | |
474 | ||
475 | s1 = constraintTerm_getName (term1); | |
476 | s2 = constraintTerm_getName (term2); | |
477 | ||
478 | if (cstring_equal (s1, s2) ) | |
479 | { | |
d46ce6a4 | 480 | DPRINTF ((message (" %q and %q are same", s1, s2 ) ) ); |
616915dd | 481 | return TRUE; |
482 | } | |
483 | else | |
484 | { | |
d46ce6a4 | 485 | DPRINTF ((message (" %q and %q are not same", s1, s2 ) ) ); |
616915dd | 486 | return FALSE; |
487 | } | |
488 | } | |
489 | ||
490 | bool constraintTerm_similar (constraintTerm term1, constraintTerm term2) | |
491 | { | |
492 | sRef s1, s2; | |
493 | ||
494 | llassert (term1 !=NULL && term2 !=NULL); | |
90bc41f7 | 495 | |
b9904f57 | 496 | if (constraintTerm_canGetValue (term1) && constraintTerm_canGetValue (term2)) |
2a6e9c30 | 497 | |
498 | /*3/30/2003 comment updated to reflect name change form INTLITERAL to CTT_INTLITERAL*/ | |
499 | /* evans 2001-07-24: was (term1->kind == CTT_INTLITERAL) && (term2->kind == CTT_INTLITERAL) ) */ | |
90bc41f7 | 500 | { |
b9904f57 | 501 | long t1, t2; |
90bc41f7 | 502 | |
b9904f57 | 503 | t1 = constraintTerm_getValue (term1); |
90bc41f7 | 504 | t2 = constraintTerm_getValue (term2); |
b9904f57 | 505 | |
506 | return (t1 == t2); | |
90bc41f7 | 507 | } |
7a8641bf | 508 | |
509 | /*drl this if statement handles the case where constraintTerm_canGetValue only returns | |
510 | true for term1 or term2 but no both | |
511 | if constraintTerm_canGetValue returned tru for both we would have returned in the previous if statement | |
512 | I suppose this could be done with xor but I've never used xor and don't feel like starting now | |
513 | besides this way is more effecient. | |
514 | */ | |
b9904f57 | 515 | if (constraintTerm_canGetValue (term1) || constraintTerm_canGetValue (term2)) |
516 | { | |
7a8641bf | 517 | |
b9904f57 | 518 | return FALSE; |
519 | } | |
520 | ||
616915dd | 521 | s1 = constraintTerm_getsRef (term1); |
522 | s2 = constraintTerm_getsRef (term2); | |
523 | ||
b9904f57 | 524 | if (!(sRef_isValid(s1) && sRef_isValid(s2))) |
616915dd | 525 | { |
526 | return FALSE; | |
527 | } | |
528 | ||
bb7c2085 | 529 | DPRINTF((message |
566b8b82 | 530 | ("Comparing srefs for %s and %s ", constraintTerm_getName(term1), constraintTerm_getName(term2) |
616915dd | 531 | ) |
532 | ) | |
533 | ); | |
b9904f57 | 534 | |
535 | if (sRef_similarRelaxed(s1, s2) || sRef_sameName (s1, s2) ) | |
536 | { | |
566b8b82 | 537 | DPRINTF ((message (" %s and %s are same", constraintTerm_getName(term1), constraintTerm_getName(term2) ) )); |
b9904f57 | 538 | return TRUE; |
539 | } | |
540 | else | |
541 | { | |
566b8b82 | 542 | DPRINTF ((message (" %s and %s are not same", constraintTerm_getName(term1), constraintTerm_getName(term2) ) )); |
b9904f57 | 543 | return FALSE; |
544 | } | |
616915dd | 545 | } |
920a3797 | 546 | |
6fcd0b1e | 547 | void constraintTerm_dump (/*@observer@*/ constraintTerm t, FILE *f) |
920a3797 | 548 | { |
549 | fileloc loc; | |
550 | constraintTermValue value; | |
551 | constraintTermType kind; | |
552 | uentry u; | |
553 | ||
554 | loc = t->loc; | |
555 | ||
556 | value = t->value; | |
557 | ||
558 | kind = t->kind; | |
559 | ||
560 | fprintf(f, "%d\n", (int) kind); | |
561 | ||
562 | switch (kind) | |
563 | { | |
564 | ||
2a6e9c30 | 565 | case CTT_EXPR: |
920a3797 | 566 | u = exprNode_getUentry(t->value.expr); |
6fcd0b1e | 567 | fprintf (f, "%s\n", cstring_toCharsSafe (uentry_rawName (u))); |
920a3797 | 568 | break; |
569 | ||
2a6e9c30 | 570 | case CTT_SREF: |
920a3797 | 571 | { |
572 | sRef s; | |
573 | ||
574 | s = t->value.sref; | |
575 | ||
576 | if (sRef_isResult (s ) ) | |
577 | { | |
578 | fprintf(f, "Result\n"); | |
579 | } | |
6fcd0b1e | 580 | else if (sRef_isParam (s)) |
920a3797 | 581 | { |
582 | int param; | |
583 | ctype ct; | |
584 | cstring ctString; | |
585 | ||
586 | ||
587 | ct = sRef_getType (s); | |
588 | param = sRef_getParam(s); | |
589 | ||
590 | ctString = ctype_dump(ct); | |
591 | ||
28bf4b0b | 592 | fprintf(f, "Param %s %d\n", cstring_toCharsSafe(ctString), (int) param ); |
920a3797 | 593 | cstring_free(ctString); |
594 | } | |
0f9b7373 | 595 | else if (sRef_isField (s) ) |
596 | { | |
597 | fprintf(f, "sRef_dump %s\n", cstring_toCharsSafe(sRef_dump(s)) ); | |
598 | } | |
920a3797 | 599 | else |
600 | { | |
601 | u = sRef_getUentry(s); | |
6fcd0b1e | 602 | fprintf (f, "%s\n", cstring_toCharsSafe (uentry_rawName (u))); |
920a3797 | 603 | } |
604 | ||
605 | } | |
606 | break; | |
607 | ||
2a6e9c30 | 608 | case CTT_INTLITERAL: |
b9904f57 | 609 | fprintf (f, "%ld\n", t->value.intlit); |
920a3797 | 610 | break; |
611 | ||
612 | default: | |
613 | BADEXIT; | |
614 | } | |
615 | ||
616 | } | |
617 | ||
618 | ||
abd7f895 | 619 | /*@only@*/ constraintTerm constraintTerm_undump (FILE *f) |
920a3797 | 620 | { |
920a3797 | 621 | constraintTermType kind; |
622 | constraintTerm ret; | |
623 | ||
624 | uentry ue; | |
625 | ||
6970c11b | 626 | char *str; |
627 | char *os; | |
920a3797 | 628 | |
3be9a165 | 629 | os = mstring_create (MAX_DUMP_LINE_LENGTH); |
630 | ||
6970c11b | 631 | str = fgets (os, MAX_DUMP_LINE_LENGTH, f); |
920a3797 | 632 | |
abd7f895 | 633 | llassert (str != NULL); |
634 | ||
28bf4b0b | 635 | kind = (constraintTermType) reader_getInt(&str); |
920a3797 | 636 | str = fgets(os, MAX_DUMP_LINE_LENGTH, f); |
637 | ||
abd7f895 | 638 | llassert (str != NULL); |
639 | ||
920a3797 | 640 | switch (kind) |
641 | { | |
642 | ||
2a6e9c30 | 643 | case CTT_SREF: |
920a3797 | 644 | { |
645 | sRef s; | |
646 | char * term; | |
28bf4b0b | 647 | term = reader_getWord(&str); |
749f175a | 648 | |
649 | if (term == NULL) | |
650 | { | |
651 | llfatalbug (message ("Library file appears to be corrupted.") ); | |
652 | } | |
920a3797 | 653 | if (strcmp (term, "Result") == 0 ) |
654 | { | |
b072092f | 655 | s = sRef_makeResult (ctype_unknown); |
920a3797 | 656 | } |
657 | else if (strcmp (term, "Param" ) == 0 ) | |
658 | { | |
659 | int param; | |
660 | char *str2, *ostr2; | |
661 | ||
662 | ctype t; | |
663 | ||
28bf4b0b | 664 | reader_checkChar(&str, ' '); |
665 | str2 = reader_getWord(&str); | |
666 | param = reader_getInt(&str); | |
920a3797 | 667 | |
749f175a | 668 | if (str2 == NULL) |
669 | { | |
670 | llfatalbug (message ("Library file appears to be corrupted.") ); | |
671 | } | |
672 | ||
920a3797 | 673 | ostr2 = str2; |
674 | t = ctype_undump(&str2) ; | |
16c024b5 | 675 | s = sRef_makeParam (param, t, stateInfo_makeLoc (g_currentloc, SA_CREATED)); |
920a3797 | 676 | free (ostr2); |
677 | } | |
0f9b7373 | 678 | else if (strcmp (term, "sRef_dump" ) == 0 ) |
679 | { | |
680 | reader_checkChar(&str, ' '); | |
681 | s = sRef_undump (&str); | |
682 | } | |
b7b694d6 | 683 | else /* This must be an identified that we can search for in usymTab */ |
920a3797 | 684 | { |
28bf4b0b | 685 | cstring termStr = cstring_makeLiteralTemp(term); |
686 | ||
687 | ue = usymtab_lookup (termStr); | |
920a3797 | 688 | s = uentry_getSref(ue); |
689 | } | |
690 | ||
691 | ret = constraintTerm_makesRef(s); | |
692 | ||
693 | free(term); | |
694 | } | |
695 | break; | |
696 | ||
2a6e9c30 | 697 | case CTT_EXPR: |
920a3797 | 698 | { |
699 | sRef s; | |
700 | char * term; | |
28bf4b0b | 701 | cstring termStr; |
920a3797 | 702 | |
28bf4b0b | 703 | term = reader_getWord(&str); |
749f175a | 704 | |
705 | if (term == NULL) | |
706 | { | |
707 | llfatalbug (message ("Library file appears to be corrupted.") ); | |
708 | } | |
709 | ||
b7b694d6 | 710 | /* This must be an identifier that we can search for in usymTab */ |
28bf4b0b | 711 | termStr = cstring_makeLiteralTemp(term); |
920a3797 | 712 | |
28bf4b0b | 713 | ue = usymtab_lookup (termStr); |
920a3797 | 714 | s = uentry_getSref(ue); |
715 | ret = constraintTerm_makesRef(s); | |
716 | ||
717 | free (term); | |
718 | } | |
719 | break; | |
720 | ||
721 | ||
2a6e9c30 | 722 | case CTT_INTLITERAL: |
920a3797 | 723 | { |
724 | int i; | |
725 | ||
28bf4b0b | 726 | i = reader_getInt(&str); |
920a3797 | 727 | ret = constraintTerm_makeIntLiteral (i); |
728 | } | |
729 | break; | |
730 | ||
731 | default: | |
732 | BADEXIT; | |
733 | } | |
734 | free (os); | |
735 | ||
736 | return ret; | |
737 | } | |
738 | ||
739 | ||
740 | ||
86d93ed3 | 741 | /* drl added sometime before 10/17/001*/ |
742 | ctype constraintTerm_getCType (constraintTerm term) | |
743 | { | |
744 | ctype ct; | |
745 | ||
746 | switch (term->kind) | |
747 | { | |
2a6e9c30 | 748 | case CTT_EXPR: |
86d93ed3 | 749 | ct = exprNode_getType (term->value.expr); |
750 | break; | |
751 | ||
2a6e9c30 | 752 | case CTT_INTLITERAL: |
86d93ed3 | 753 | ct = ctype_signedintegral; |
754 | break; | |
755 | ||
2a6e9c30 | 756 | case CTT_SREF: |
86d93ed3 | 757 | ct = sRef_getType (term->value.sref) ; |
758 | break; | |
759 | default: | |
760 | BADEXIT; | |
761 | } | |
762 | return ct; | |
763 | } | |
920a3797 | 764 | |
fba0ed37 | 765 | bool constraintTerm_isConstantOnly (constraintTerm term) |
766 | { | |
767 | switch (term->kind) | |
768 | { | |
2a6e9c30 | 769 | case CTT_EXPR: |
fba0ed37 | 770 | if (exprNode_isNumLiteral (term->value.expr) || |
771 | exprNode_isStringLiteral (term->value.expr) || | |
772 | exprNode_isCharLiteral (term->value.expr) ) | |
773 | { | |
774 | return TRUE; | |
775 | } | |
776 | else | |
777 | { | |
778 | return FALSE; | |
779 | } | |
780 | ||
2a6e9c30 | 781 | case CTT_INTLITERAL: |
fba0ed37 | 782 | return TRUE; |
783 | ||
2a6e9c30 | 784 | case CTT_SREF: |
fba0ed37 | 785 | if ( sRef_isConst (term->value.sref) ) |
786 | { | |
787 | return TRUE; | |
788 | } | |
789 | else | |
790 | { | |
791 | return FALSE; | |
792 | } | |
793 | default: | |
794 | BADEXIT; | |
795 | } | |
796 | ||
797 | BADEXIT; | |
798 | } |