]> andersk Git - splint.git/blame - src/constraintTerm.c
Updating for cert move
[splint.git] / src / constraintTerm.c
CommitLineData
616915dd 1/*
2** constraintExpr.c
3*/
4
5//#define DEBUGPRINT 1
6
7# include <ctype.h> /* for isdigit */
8# include "lclintMacros.nf"
9# include "basic.h"
10# include "cgrammar.h"
11# include "cgrammar_tokens.h"
12
13# include "exprChecks.h"
14# include "aliasChecks.h"
15# include "exprNodeSList.h"
16
17//# include "exprData.i"
18
19/*@-czechfcns@*/
20
21//#include "constraintExpr.h"
22
c3e695ff 23/*@access exprNode @*/
616915dd 24
c3e695ff 25static/*@out@*/ constraintTerm new_constraintTermExpr (void)
616915dd 26{
27 constraintTerm ret;
28 ret = dmalloc (sizeof (* ret ) );
29 return ret;
30}
31
32
33bool constraintTerm_isIntLiteral (constraintTerm term)
34{
dc92450f 35 llassert(term != NULL);
616915dd 36
37 if (term->kind == INTLITERAL)
38 return TRUE;
39
40 return FALSE;
41}
42
dc92450f 43bool constraintTerm_isStringLiteral (constraintTerm c) /*@*/
616915dd 44{
dc92450f 45 llassert (c != NULL);
616915dd 46 if (c->kind == EXPRNODE)
47 {
48 if (exprNode_knownStringValue(c->value.expr) )
49 {
50 return TRUE;
51 }
52 }
53 return FALSE;
54}
55
56cstring constraintTerm_getStringLiteral (constraintTerm c)
57{
dc92450f 58 llassert (c != NULL);
616915dd 59 llassert (constraintTerm_isStringLiteral (c) );
60 llassert (c->kind == EXPRNODE);
61
dc92450f 62 return (cstring_copy ( multiVal_forceString (exprNode_getValue (c->value.expr) ) ) );
616915dd 63}
64
c3e695ff 65constraintTerm constraintTerm_simplify (/*@returned@*/ constraintTerm term) /*@modifies term@*/
616915dd 66{
67 if (term->kind == EXPRNODE)
68 {
69 if ( exprNode_knownIntValue (term->value.expr ) )
70 {
71 long int temp;
72 temp = exprNode_getLongValue (term->value.expr);
dc92450f 73 term->value.intlit = (int)temp;
616915dd 74 term->kind = INTLITERAL;
75 }
76 }
77 return term;
78}
79
80fileloc constraintTerm_getFileloc (constraintTerm t)
81{
82 return (fileloc_copy (t->loc) );
83}
84
85constraintTerm constraintTerm_makeExprNode (/*@only@*/ exprNode e)
86{
87 constraintTerm ret = new_constraintTermExpr();
88 ret->loc = exprNode_getfileloc(e);
89 ret->value.expr = e;
90 ret->kind = EXPRNODE;
91 ret = constraintTerm_simplify(ret);
92 return ret;
93}
94
dc92450f 95/*@only@*/ constraintTerm constraintTerm_makesRef (/*@only@*/ sRef s)
616915dd 96{
97 constraintTerm ret = new_constraintTermExpr();
98 ret->loc = fileloc_undefined;
99 ret->value.sref = s;
100 ret->kind = SREF;
101 ret = constraintTerm_simplify(ret);
102 return ret;
103}
104
105constraintTerm constraintTerm_copy (constraintTerm term)
106{
107 constraintTerm ret;
108 ret = new_constraintTermExpr();
109 ret->loc = fileloc_copy (term->loc);
c3e695ff 110 constraintTermValue_copy (ret->value, term->value);
616915dd 111 ret->kind = term->kind;
112 return ret;
113}
114
dc92450f 115constraintTerm constraintTerm_setFileloc (/*@returned@*/ constraintTerm term, fileloc loc)
616915dd 116{
dc92450f 117 llassert(term != NULL);
616915dd 118 term->loc = fileloc_copy(loc);
119 return term;
120}
121
122
123cstring constraintTerm_getName (constraintTerm term)
124{
125 cstring s;
126 s = cstring_undefined;
127
128 llassert (term != NULL);
129
130 switch (term->kind)
131 {
132 case EXPRNODE:
133 /*@i334*/ //wtf
134 s = message ("%s", exprNode_unparse (term->value.expr) );
135 break;
136 case INTLITERAL:
137 s = message (" %d ", term->value.intlit);
138 break;
139
140 case SREF:
141 s = message ("%s", sRef_unparse (term->value.sref) );
142
143 break;
c3e695ff 144 default:
145 BADEXIT;
146 /*@notreached@*/
616915dd 147 break;
148 }
616915dd 149
c3e695ff 150 return s;
616915dd 151}
152
153constraintTerm
c3e695ff 154constraintTerm_doSRefFixBaseParam (constraintTerm term, exprNodeList arglist) /*@modifies term->value@*/
616915dd 155{
156 llassert (term != NULL);
157
158 switch (term->kind)
159 {
160 case EXPRNODE:
161 /*@i334*/ //wtf
162 // s = message ("%s @ %s ", exprNode_unparse (term->value.expr),
163 // fileloc_unparse (term->loc) );
164 break;
165 case INTLITERAL:
166 // s = message (" %d ", term->value.intlit);
167 break;
168
169 case SREF:
170 term->value.sref = sRef_fixBaseParam (term->value.sref, arglist);
171 // s = message ("%s ", sRef_unparse (term->value.sref) );
172
173 break;
c3e695ff 174 default:
175 BADEXIT;
616915dd 176 }
177 return term;
178
179}
180
616915dd 181cstring constraintTerm_print (constraintTerm term) /*@*/
182{
183 cstring s;
184 s = cstring_undefined;
185
186 llassert (term != NULL);
187
188 switch (term->kind)
189 {
190 case EXPRNODE:
191 /*@i334*/ //wtf
192 s = message ("%s @ %s ", exprNode_unparse (term->value.expr),
193 fileloc_unparse (term->loc) );
194 break;
195 case INTLITERAL:
196 s = message (" %d ", term->value.intlit);
197 break;
198
199 case SREF:
200 s = message ("%s ", sRef_unparseDebug (term->value.sref) );
201
202 break;
c3e695ff 203 default:
204 BADEXIT;
616915dd 205 }
206
207 return s;
208}
209
210
211constraintTerm constraintTerm_makeIntLiteral (int i)
212{
213 constraintTerm ret = new_constraintTermExpr();
214 ret->value.intlit = i;
215 ret->kind = INTLITERAL;
216 ret->loc = fileloc_undefined;
217 return ret;
218}
219
220bool constraintTerm_canGetValue (constraintTerm term)
221{
222 if (term->kind == INTLITERAL)
223 return TRUE;
224 else
225 return FALSE;
226}
227
228int constraintTerm_getValue (constraintTerm term)
229{
230 llassert (term->kind == INTLITERAL);
231 return term->value.intlit;
232}
233
234
235
236/* same and similar are similar but not the same*/
237
238bool constraintTerm_same (constraintTerm term1, constraintTerm term2)
239{
240 llassert (term1 !=NULL && term2 !=NULL);
241
242 if ( (term1->kind != term2->kind) || (term1->kind != EXPRNODE) )
243 {
244 return FALSE;
245 }
246
247 DPRINTF ( (message
248 ("Comparing srefs for %s and %s ", constraintTerm_print(term1), constraintTerm_print(term2)
249 )
250 )
251 );
252
253 if (sRef_same (term1->value.expr->sref, term2->value.expr->sref) )
254 {
255 DPRINTF ((message (" %s and %s are same", constraintTerm_print(term1), constraintTerm_print(term2) ) ));
256 return TRUE;
257 }
258 else
259 {
260 DPRINTF ((message (" %s and %s are not same", constraintTerm_print(term1), constraintTerm_print(term2) ) ));
261 return FALSE;
262 }
263
264}
265
c3e695ff 266/*@exposed@*/ sRef constraintTerm_getsRef (constraintTerm t)
616915dd 267{
dc92450f 268 llassert (t != NULL);
616915dd 269 if (t->kind == EXPRNODE)
270 {
c3e695ff 271 return exprNode_getSref(t->value.expr);
616915dd 272 }
273
274 if (t->kind == SREF)
275 {
c3e695ff 276 return t->value.sref;
616915dd 277 }
278
279 return sRef_undefined;
280}
281
282bool constraintTerm_probSame (constraintTerm term1, constraintTerm term2)
283{
284 cstring s1, s2;
285
286 llassert (term1 !=NULL && term2 !=NULL);
287
288 DPRINTF ( (message
289 ("Comparing srefs for %s and %s ", constraintTerm_print(term1), constraintTerm_print(term2)
290 )
291 )
292 );
293
294 s1 = constraintTerm_getName (term1);
295 s2 = constraintTerm_getName (term2);
296
297 if (cstring_equal (s1, s2) )
298 {
299 DPRINTF ((message (" %s and %s are same", s1, s2 ) ) );
300 return TRUE;
301 }
302 else
303 {
304 DPRINTF ((message (" %s and %s are not same", s1, s2 ) ) );
305 return FALSE;
306 }
307}
308
309bool constraintTerm_similar (constraintTerm term1, constraintTerm term2)
310{
311 sRef s1, s2;
312
313 llassert (term1 !=NULL && term2 !=NULL);
90bc41f7 314
315 if ( (term1->kind == INTLITERAL) && (term2->kind == INTLITERAL) )
316 {
317 int t1, t2;
318 llassert (constraintTerm_canGetValue(term1) );
319 t1 = constraintTerm_getValue (term1);
320
321 llassert (constraintTerm_canGetValue(term2) );
322 t2 = constraintTerm_getValue (term2);
323 if (t1 == t2)
324 return TRUE;
325
326 return FALSE;
327 }
328
616915dd 329 s1 = constraintTerm_getsRef (term1);
330 s2 = constraintTerm_getsRef (term2);
331
332 if ( ! (s1 && s2) )
333 {
334 return FALSE;
335 }
336
337 DPRINTF ( (message
338 ("Comparing srefs for %s and %s ", constraintTerm_print(term1), constraintTerm_print(term2)
339 )
340 )
341 );
342
343 if (sRef_similarRelaxed(s1, s2) || sRef_sameName (s1, s2) )
344 {
345 DPRINTF ((message (" %s and %s are same", constraintTerm_print(term1), constraintTerm_print(term2) ) ));
346 return TRUE;
347 }
348 else
349 {
350 DPRINTF ((message (" %s and %s are not same", constraintTerm_print(term1), constraintTerm_print(term2) ) ));
351 return FALSE;
352 }
353
354}
This page took 0.78106 seconds and 5 git commands to generate.