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