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