]>
Commit | Line | Data |
---|---|---|
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 | ||
23 | ||
24 | constraintTerm new_constraintTermExpr (void) | |
25 | { | |
26 | constraintTerm ret; | |
27 | ret = dmalloc (sizeof (* ret ) ); | |
28 | return ret; | |
29 | } | |
30 | ||
31 | ||
32 | bool constraintTerm_isIntLiteral (constraintTerm term) | |
33 | { | |
dc92450f | 34 | llassert(term != NULL); |
616915dd | 35 | |
36 | if (term->kind == INTLITERAL) | |
37 | return TRUE; | |
38 | ||
39 | return FALSE; | |
40 | } | |
41 | ||
dc92450f | 42 | bool constraintTerm_isStringLiteral (constraintTerm c) /*@*/ |
616915dd | 43 | { |
dc92450f | 44 | llassert (c != NULL); |
616915dd | 45 | if (c->kind == EXPRNODE) |
46 | { | |
47 | if (exprNode_knownStringValue(c->value.expr) ) | |
48 | { | |
49 | return TRUE; | |
50 | } | |
51 | } | |
52 | return FALSE; | |
53 | } | |
54 | ||
55 | cstring constraintTerm_getStringLiteral (constraintTerm c) | |
56 | { | |
dc92450f | 57 | llassert (c != NULL); |
616915dd | 58 | llassert (constraintTerm_isStringLiteral (c) ); |
59 | llassert (c->kind == EXPRNODE); | |
60 | ||
dc92450f | 61 | return (cstring_copy ( multiVal_forceString (exprNode_getValue (c->value.expr) ) ) ); |
616915dd | 62 | } |
63 | ||
dc92450f | 64 | constraintTerm constraintTerm_simplify (/*@returned@*/ constraintTerm term) |
616915dd | 65 | { |
66 | if (term->kind == EXPRNODE) | |
67 | { | |
68 | if ( exprNode_knownIntValue (term->value.expr ) ) | |
69 | { | |
70 | long int temp; | |
71 | temp = exprNode_getLongValue (term->value.expr); | |
dc92450f | 72 | term->value.intlit = (int)temp; |
616915dd | 73 | term->kind = INTLITERAL; |
74 | } | |
75 | } | |
76 | return term; | |
77 | } | |
78 | ||
79 | fileloc constraintTerm_getFileloc (constraintTerm t) | |
80 | { | |
81 | return (fileloc_copy (t->loc) ); | |
82 | } | |
83 | ||
84 | constraintTerm constraintTerm_makeExprNode (/*@only@*/ exprNode e) | |
85 | { | |
86 | constraintTerm ret = new_constraintTermExpr(); | |
87 | ret->loc = exprNode_getfileloc(e); | |
88 | ret->value.expr = e; | |
89 | ret->kind = EXPRNODE; | |
90 | ret = constraintTerm_simplify(ret); | |
91 | return ret; | |
92 | } | |
93 | ||
dc92450f | 94 | /*@only@*/ constraintTerm constraintTerm_makesRef (/*@only@*/ sRef s) |
616915dd | 95 | { |
96 | constraintTerm ret = new_constraintTermExpr(); | |
97 | ret->loc = fileloc_undefined; | |
98 | ret->value.sref = s; | |
99 | ret->kind = SREF; | |
100 | ret = constraintTerm_simplify(ret); | |
101 | return ret; | |
102 | } | |
103 | ||
104 | constraintTerm constraintTerm_copy (constraintTerm term) | |
105 | { | |
106 | constraintTerm ret; | |
107 | ret = new_constraintTermExpr(); | |
108 | ret->loc = fileloc_copy (term->loc); | |
109 | ret->value= term->value; | |
110 | ret->kind = term->kind; | |
111 | return ret; | |
112 | } | |
113 | ||
dc92450f | 114 | constraintTerm constraintTerm_setFileloc (/*@returned@*/ constraintTerm term, fileloc loc) |
616915dd | 115 | { |
dc92450f | 116 | llassert(term != NULL); |
616915dd | 117 | term->loc = fileloc_copy(loc); |
118 | return term; | |
119 | } | |
120 | ||
121 | ||
122 | cstring constraintTerm_getName (constraintTerm term) | |
123 | { | |
124 | cstring s; | |
125 | s = cstring_undefined; | |
126 | ||
127 | llassert (term != NULL); | |
128 | ||
129 | switch (term->kind) | |
130 | { | |
131 | case EXPRNODE: | |
132 | /*@i334*/ //wtf | |
133 | s = message ("%s", exprNode_unparse (term->value.expr) ); | |
134 | break; | |
135 | case INTLITERAL: | |
136 | s = message (" %d ", term->value.intlit); | |
137 | break; | |
138 | ||
139 | case SREF: | |
140 | s = message ("%s", sRef_unparse (term->value.sref) ); | |
141 | ||
142 | break; | |
143 | } | |
144 | ||
145 | return s; | |
146 | } | |
147 | ||
148 | constraintExpr | |
149 | constraintTerm_doFixResult (constraintExpr e, exprNode fcnCall) | |
150 | { | |
151 | constraintTerm t; | |
152 | sRef s; | |
153 | constraintExprData data = e->data; | |
154 | ||
155 | constraintExprKind kind = e->kind; | |
156 | ||
157 | constraintExpr ret; | |
158 | ||
159 | llassert(kind == term); | |
160 | ||
161 | t = constraintExprData_termGetTerm (data); | |
162 | llassert (t != NULL); | |
163 | ||
164 | ret = e; | |
165 | switch (t->kind) | |
166 | { | |
167 | case EXPRNODE: | |
168 | break; | |
169 | case INTLITERAL: | |
170 | break; | |
171 | ||
172 | case SREF: | |
173 | s = t->value.sref; | |
174 | if (s->kind == SK_RESULT) | |
175 | { | |
176 | ret = constraintExpr_makeExprNode(fcnCall); | |
177 | } | |
178 | break; | |
179 | } | |
180 | return ret; | |
181 | ||
182 | } | |
183 | ||
184 | constraintTerm | |
185 | constraintTerm_doSRefFixBaseParam (constraintTerm term, exprNodeList arglist) | |
186 | { | |
187 | llassert (term != NULL); | |
188 | ||
189 | switch (term->kind) | |
190 | { | |
191 | case EXPRNODE: | |
192 | /*@i334*/ //wtf | |
193 | // s = message ("%s @ %s ", exprNode_unparse (term->value.expr), | |
194 | // fileloc_unparse (term->loc) ); | |
195 | break; | |
196 | case INTLITERAL: | |
197 | // s = message (" %d ", term->value.intlit); | |
198 | break; | |
199 | ||
200 | case SREF: | |
201 | term->value.sref = sRef_fixBaseParam (term->value.sref, arglist); | |
202 | // s = message ("%s ", sRef_unparse (term->value.sref) ); | |
203 | ||
204 | break; | |
205 | } | |
206 | return term; | |
207 | ||
208 | } | |
209 | ||
210 | constraintExpr | |
211 | constraintTerm_doSRefFixConstraintParam (constraintExpr e, exprNodeList arglist) | |
212 | { | |
213 | constraintTerm t; | |
214 | ||
215 | constraintExprData data = e->data; | |
216 | ||
217 | constraintExprKind kind = e->kind; | |
218 | ||
219 | constraintExpr ret; | |
220 | ||
221 | llassert(kind == term); | |
222 | ||
223 | t = constraintExprData_termGetTerm (data); | |
224 | llassert (t != NULL); | |
225 | ||
226 | ret = e; | |
227 | switch (t->kind) | |
228 | { | |
229 | case EXPRNODE: | |
230 | /*@i334*/ //wtf | |
231 | // s = message ("%s @ %s ", exprNode_unparse (term->value.expr), | |
232 | // fileloc_unparse (term->loc) ); | |
233 | break; | |
234 | case INTLITERAL: | |
235 | // s = message (" %d ", term->value.intlit); | |
236 | break; | |
237 | ||
238 | case SREF: | |
239 | ret = sRef_fixConstraintParam (t->value.sref, arglist); | |
240 | ||
241 | // s = message ("%s ", sRef_unparse (term->value.sref) ); | |
242 | ||
243 | break; | |
244 | } | |
245 | return ret; | |
246 | ||
247 | } | |
248 | ||
249 | cstring constraintTerm_print (constraintTerm term) /*@*/ | |
250 | { | |
251 | cstring s; | |
252 | s = cstring_undefined; | |
253 | ||
254 | llassert (term != NULL); | |
255 | ||
256 | switch (term->kind) | |
257 | { | |
258 | case EXPRNODE: | |
259 | /*@i334*/ //wtf | |
260 | s = message ("%s @ %s ", exprNode_unparse (term->value.expr), | |
261 | fileloc_unparse (term->loc) ); | |
262 | break; | |
263 | case INTLITERAL: | |
264 | s = message (" %d ", term->value.intlit); | |
265 | break; | |
266 | ||
267 | case SREF: | |
268 | s = message ("%s ", sRef_unparseDebug (term->value.sref) ); | |
269 | ||
270 | break; | |
271 | } | |
272 | ||
273 | return s; | |
274 | } | |
275 | ||
276 | ||
277 | constraintTerm constraintTerm_makeIntLiteral (int i) | |
278 | { | |
279 | constraintTerm ret = new_constraintTermExpr(); | |
280 | ret->value.intlit = i; | |
281 | ret->kind = INTLITERAL; | |
282 | ret->loc = fileloc_undefined; | |
283 | return ret; | |
284 | } | |
285 | ||
286 | bool constraintTerm_canGetValue (constraintTerm term) | |
287 | { | |
288 | if (term->kind == INTLITERAL) | |
289 | return TRUE; | |
290 | else | |
291 | return FALSE; | |
292 | } | |
293 | ||
294 | int constraintTerm_getValue (constraintTerm term) | |
295 | { | |
296 | llassert (term->kind == INTLITERAL); | |
297 | return term->value.intlit; | |
298 | } | |
299 | ||
300 | ||
301 | ||
302 | /* same and similar are similar but not the same*/ | |
303 | ||
304 | bool constraintTerm_same (constraintTerm term1, constraintTerm term2) | |
305 | { | |
306 | llassert (term1 !=NULL && term2 !=NULL); | |
307 | ||
308 | if ( (term1->kind != term2->kind) || (term1->kind != EXPRNODE) ) | |
309 | { | |
310 | return FALSE; | |
311 | } | |
312 | ||
313 | DPRINTF ( (message | |
314 | ("Comparing srefs for %s and %s ", constraintTerm_print(term1), constraintTerm_print(term2) | |
315 | ) | |
316 | ) | |
317 | ); | |
318 | ||
319 | if (sRef_same (term1->value.expr->sref, term2->value.expr->sref) ) | |
320 | { | |
321 | DPRINTF ((message (" %s and %s are same", constraintTerm_print(term1), constraintTerm_print(term2) ) )); | |
322 | return TRUE; | |
323 | } | |
324 | else | |
325 | { | |
326 | DPRINTF ((message (" %s and %s are not same", constraintTerm_print(term1), constraintTerm_print(term2) ) )); | |
327 | return FALSE; | |
328 | } | |
329 | ||
330 | } | |
331 | ||
332 | sRef constraintTerm_getsRef (constraintTerm t) | |
333 | { | |
dc92450f | 334 | llassert (t != NULL); |
616915dd | 335 | if (t->kind == EXPRNODE) |
336 | { | |
337 | return t->value.expr->sref; | |
338 | } | |
339 | ||
340 | if (t->kind == SREF) | |
341 | { | |
dc92450f | 342 | /*@i34*/ return t->value.sref; |
616915dd | 343 | } |
344 | ||
345 | return sRef_undefined; | |
346 | } | |
347 | ||
348 | bool constraintTerm_probSame (constraintTerm term1, constraintTerm term2) | |
349 | { | |
350 | cstring s1, s2; | |
351 | ||
352 | llassert (term1 !=NULL && term2 !=NULL); | |
353 | ||
354 | DPRINTF ( (message | |
355 | ("Comparing srefs for %s and %s ", constraintTerm_print(term1), constraintTerm_print(term2) | |
356 | ) | |
357 | ) | |
358 | ); | |
359 | ||
360 | s1 = constraintTerm_getName (term1); | |
361 | s2 = constraintTerm_getName (term2); | |
362 | ||
363 | if (cstring_equal (s1, s2) ) | |
364 | { | |
365 | DPRINTF ((message (" %s and %s are same", s1, s2 ) ) ); | |
366 | return TRUE; | |
367 | } | |
368 | else | |
369 | { | |
370 | DPRINTF ((message (" %s and %s are not same", s1, s2 ) ) ); | |
371 | return FALSE; | |
372 | } | |
373 | } | |
374 | ||
375 | bool constraintTerm_similar (constraintTerm term1, constraintTerm term2) | |
376 | { | |
377 | sRef s1, s2; | |
378 | ||
379 | llassert (term1 !=NULL && term2 !=NULL); | |
380 | ||
381 | s1 = constraintTerm_getsRef (term1); | |
382 | s2 = constraintTerm_getsRef (term2); | |
383 | ||
384 | if ( ! (s1 && s2) ) | |
385 | { | |
386 | return FALSE; | |
387 | } | |
388 | ||
389 | DPRINTF ( (message | |
390 | ("Comparing srefs for %s and %s ", constraintTerm_print(term1), constraintTerm_print(term2) | |
391 | ) | |
392 | ) | |
393 | ); | |
394 | ||
395 | if (sRef_similarRelaxed(s1, s2) || sRef_sameName (s1, s2) ) | |
396 | { | |
397 | DPRINTF ((message (" %s and %s are same", constraintTerm_print(term1), constraintTerm_print(term2) ) )); | |
398 | return TRUE; | |
399 | } | |
400 | else | |
401 | { | |
402 | DPRINTF ((message (" %s and %s are not same", constraintTerm_print(term1), constraintTerm_print(term2) ) )); | |
403 | return FALSE; | |
404 | } | |
405 | ||
406 | } |