]> andersk Git - splint.git/blob - src/constraintTerm.c
Dave's Updates
[splint.git] / src / constraintTerm.c
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 "exprNodeSList.h"
15
16 /*@-czechfcns@*/
17
18 //#include "constraintExpr.h"
19
20 /*@access exprNode @*/
21
22 /*@unused@*/ static bool constraintTerm_same (constraintTerm p_term1, constraintTerm p_term2) ;
23
24 void constraintTerm_free (/*@only@*/ constraintTerm term)
25 {
26   llassert(constraintTerm_isDefined(term) );
27   fileloc_free (term->loc);
28   
29   switch (term->kind) 
30     {
31     case EXPRNODE:
32       /* we don't free an exprNode*/
33       break;
34     case SREF:
35       /* sref */
36       sRef_free (term->value.sref);
37       break;
38     case INTLITERAL:
39       /* don't free an int */
40       break;
41     case  ERRORBADCONSTRAINTTERMTYPE:
42     default:
43       /* type was set incorrectly */
44       llcontbug (message("constraintTerm_free type was set incorrectly"));
45     }
46   //  term->value.intlit = 0;
47   term->kind =  ERRORBADCONSTRAINTTERMTYPE;
48   free (term);
49 }
50
51 /*@only@*/ static/*@out@*/ constraintTerm new_constraintTermExpr (void)
52 {
53   constraintTerm ret;
54   ret = dmalloc (sizeof (* ret ) );
55   ret->value.intlit = 0;
56   return ret;
57 }
58
59
60 bool constraintTerm_isIntLiteral (constraintTerm term)
61 {
62   llassert(term != NULL);
63   
64   if (term->kind == INTLITERAL)
65     return TRUE;
66
67   return FALSE;
68 }
69
70 bool constraintTerm_isStringLiteral (constraintTerm c) /*@*/
71 {
72   llassert (c != NULL);
73   if (c->kind == EXPRNODE)
74     {
75       if (exprNode_knownStringValue(c->value.expr) )
76         {
77           return TRUE;
78         }
79     }
80   return FALSE;
81 }
82
83 cstring constraintTerm_getStringLiteral (constraintTerm c)
84 {
85   llassert (c != NULL);
86   llassert (constraintTerm_isStringLiteral (c) );
87   llassert (c->kind == EXPRNODE);
88   
89   return (cstring_copy ( multiVal_forceString (exprNode_getValue (c->value.expr) ) ) );
90 }
91
92 constraintTerm constraintTerm_simplify (/*@returned@*/ constraintTerm term) /*@modifies term@*/
93 {
94   if (term->kind == EXPRNODE)
95     {
96       if ( exprNode_knownIntValue (term->value.expr ) )
97         {
98           long int temp;
99
100           temp  = exprNode_getLongValue (term->value.expr);
101           term->value.intlit = (int)temp;
102           term->kind = INTLITERAL;
103         }
104     }
105   return term;
106 }
107
108 fileloc constraintTerm_getFileloc (constraintTerm t)
109 {
110   return (fileloc_copy (t->loc) );
111 }
112
113 constraintTermType constraintTerm_getKind (constraintTerm t)
114 {
115   llassert (constraintTerm_isDefined(t) );
116   
117   return (t->kind);
118 }
119
120 /*@exposed@*/ sRef constraintTerm_getSRef (constraintTerm t)
121 {
122   llassert (constraintTerm_isDefined(t) );
123   llassert (t->kind == SREF);
124
125   return (t->value.sref);
126 }
127
128 /*@only@*/ constraintTerm constraintTerm_makeExprNode (/*@depenedent@*/  exprNode e)
129 {
130   constraintTerm ret = new_constraintTermExpr();
131   ret->loc =  fileloc_copy(exprNode_getfileloc(e));
132   ret->value.expr = e;
133   ret->kind = EXPRNODE;
134   ret = constraintTerm_simplify(ret);
135   return ret;
136 }
137
138 /*@only@*/ constraintTerm constraintTerm_makesRef  (/*@temp@*/ /*@observer@*/ sRef s)
139 {
140   constraintTerm ret = new_constraintTermExpr();
141   ret->loc =  fileloc_undefined;
142   ret->value.sref = sRef_saveCopy(s);
143   ret->kind = SREF;
144   ret = constraintTerm_simplify(ret);
145   return ret;
146 }
147
148
149
150 constraintTerm constraintTerm_copy (constraintTerm term)
151 {
152   constraintTerm ret;
153   ret = new_constraintTermExpr();
154   ret->loc = fileloc_copy (term->loc);
155   
156   switch (term->kind)
157     {
158     case EXPRNODE:
159       ret->value.expr = term->value.expr;
160       break;
161     case INTLITERAL:
162       ret->value.intlit = term->value.intlit;
163       break;
164       
165     case SREF:
166       ret->value.sref = sRef_saveCopy(term->value.sref);
167       break;
168     default:
169       BADEXIT;
170     }
171   ret->kind = term->kind;
172   return ret;
173 }
174
175 constraintTerm constraintTerm_setFileloc (/*@returned@*/ constraintTerm term, fileloc loc) 
176 {
177   llassert(term != NULL);
178
179   if ( fileloc_isDefined(  term->loc ) )
180     fileloc_free(term->loc);
181
182   term->loc = fileloc_copy(loc);
183   return term;
184 }
185
186
187 static cstring constraintTerm_getName (constraintTerm term)
188 {
189   cstring s;
190   s = cstring_undefined;
191   
192   llassert (term != NULL);
193
194   switch (term->kind)
195     {
196     case EXPRNODE:
197       /*@i334*/  //wtf
198       s = message ("%s", exprNode_unparse (term->value.expr) );
199       break;
200     case INTLITERAL:
201       s = message (" %d ", term->value.intlit);
202       break;
203       
204     case SREF:
205       s = message ("%q", sRef_unparse (term->value.sref) );
206
207       break;
208     default:
209       BADEXIT;
210       /*@notreached@*/
211       break;
212     }
213   
214   return s;
215 }
216
217 constraintTerm 
218 constraintTerm_doSRefFixBaseParam (/*@returned@*/constraintTerm term, exprNodeList arglist) /*@modifies term@*/
219 {
220   llassert (term != NULL);
221   
222   switch (term->kind)
223     {
224     case EXPRNODE:
225       /*@i334*/  //wtf
226       //   s = message ("%s @ %s ", exprNode_unparse (term->value.expr),
227       //           fileloc_unparse (term->loc) );
228       break;
229     case INTLITERAL:
230       //  s = message (" %d ", term->value.intlit);
231        break;
232       
233     case SREF:
234       term->value.sref = sRef_fixBaseParam (term->value.sref, arglist);
235       //      s = message ("%s ", sRef_unparse (term->value.sref) );
236
237       break;
238     default:
239       BADEXIT;
240     }
241   return term;
242   
243 }
244
245 cstring constraintTerm_print (constraintTerm term)  /*@*/
246 {
247   cstring s;
248   s = cstring_undefined;
249   
250   llassert (term != NULL);
251
252   switch (term->kind)
253     {
254     case EXPRNODE:
255       /*@i334*/  //wtf
256       s = message ("%s @ %q ", exprNode_unparse (term->value.expr),
257                    fileloc_unparse (term->loc) );
258       break;
259     case INTLITERAL:
260       s = message (" %d ", term->value.intlit);
261       break;
262       
263     case SREF:
264       s = message ("%q ", sRef_unparseDebug (term->value.sref) );
265
266       break;
267     default:
268       BADEXIT;
269     }
270   
271   return s;
272 }
273
274
275 constraintTerm constraintTerm_makeIntLiteral (int i)
276 {
277   constraintTerm ret = new_constraintTermExpr();
278   ret->value.intlit = i;
279   ret->kind = INTLITERAL;
280   ret->loc =  fileloc_undefined;
281   return ret;
282 }
283
284 bool constraintTerm_canGetValue (constraintTerm term)
285 {
286   if (term->kind == INTLITERAL)
287     return TRUE;
288   else
289     return FALSE;
290 }
291
292 int constraintTerm_getValue (constraintTerm term) 
293 {
294   llassert (term->kind == INTLITERAL);
295   return term->value.intlit;
296 }
297
298 /* same and similar are similar but not the same*/
299 static bool constraintTerm_same (constraintTerm term1, constraintTerm term2)
300 {
301   llassert (term1 !=NULL && term2 !=NULL);
302
303   if ( (term1->kind != term2->kind) || (term1->kind != EXPRNODE) )
304     {
305       return FALSE;
306     }
307       
308  DPRINTF ( (message
309             ("Comparing srefs for %s and  %s ", constraintTerm_print(term1), constraintTerm_print(term2)
310              )
311             )
312            );
313  
314  if (sRef_same (term1->value.expr->sref, term2->value.expr->sref) )
315    {
316      DPRINTF ((message (" %s and %s are same", constraintTerm_print(term1), constraintTerm_print(term2)  )  ));
317      return TRUE;
318    }
319  else
320    {
321      DPRINTF ((message (" %s and %s are not same", constraintTerm_print(term1), constraintTerm_print(term2)  )  ));
322      return FALSE;
323    }     
324     
325 }
326
327 static /*@exposed@*/ sRef constraintTerm_getsRef (constraintTerm t)
328 {
329   llassert (t != NULL);
330   if (t->kind == EXPRNODE)
331     {
332       return exprNode_getSref(t->value.expr);
333     }
334
335   if (t->kind == SREF)
336     {
337       return t->value.sref;
338     }
339
340   return sRef_undefined;
341 }
342
343 bool constraintTerm_probSame (constraintTerm term1, constraintTerm term2)
344 {
345   cstring s1, s2;
346
347   llassert (term1 !=NULL && term2 !=NULL);
348      
349  DPRINTF ( (message
350             ("Comparing srefs for %s and  %s ", constraintTerm_print(term1), constraintTerm_print(term2)
351              )
352             )
353            );
354   
355   s1 = constraintTerm_getName (term1);
356   s2 = constraintTerm_getName (term2);
357
358   if (cstring_equal (s1, s2) )
359     {
360       DPRINTF ((message (" %q and %q are same", s1, s2 ) ) );
361      return TRUE;
362    }
363   else
364      {
365      DPRINTF ((message (" %q and %q are not same", s1, s2 ) ) );
366      return FALSE;
367    }   
368 }
369
370 bool constraintTerm_similar (constraintTerm term1, constraintTerm term2)
371 {
372   sRef s1, s2;
373   
374   llassert (term1 !=NULL && term2 !=NULL);
375   
376   if ( (term1->kind == INTLITERAL) && (term2->kind == INTLITERAL) )
377     {
378       int t1, t2;
379       llassert (constraintTerm_canGetValue(term1) );
380       t1 = constraintTerm_getValue (term1);
381
382       llassert (constraintTerm_canGetValue(term2) );
383       t2 = constraintTerm_getValue (term2);
384       if (t1 == t2)
385         return TRUE;
386       
387        return FALSE;
388     }
389     
390   s1 = constraintTerm_getsRef (term1);
391   s2 = constraintTerm_getsRef (term2);
392
393   if ( ! (sRef_isValid(s1) && sRef_isValid(s2) ) )
394     {
395       return FALSE;
396     }
397   
398  DPRINTF( (message
399             ("Comparing srefs for %s and  %s ", constraintTerm_print(term1), constraintTerm_print(term2)
400              )
401             )
402            );
403  
404  if (sRef_similarRelaxed(s1, s2)   || sRef_sameName (s1, s2) )
405    {
406      DPRINTF ((message (" %s and %s are same", constraintTerm_print(term1), constraintTerm_print(term2)  )  ));
407      return TRUE;
408    }
409  else
410    {
411      DPRINTF ((message (" %s and %s are not same", constraintTerm_print(term1), constraintTerm_print(term2)  )  ));
412      return FALSE;
413    }     
414     
415 }
416
417 void constraintTerm_dump ( /*@observer@*/ constraintTerm t,  FILE *f)
418 {
419   fileloc loc;
420   constraintTermValue value;
421   constraintTermType kind;
422   uentry u;
423   
424   loc = t->loc;
425
426   value = t->value;
427
428   kind  = t->kind;
429
430   fprintf(f, "%d\n", (int) kind);
431   
432   switch (kind)
433     {
434       
435     case EXPRNODE:
436       u = exprNode_getUentry(t->value.expr);
437       fprintf(f, "%s\n", cstring_toCharsSafe( uentry_rawName (u) )
438               );
439       break;
440       
441     case SREF:
442       {
443         sRef s;
444
445         s =  t->value.sref;
446         
447         if (sRef_isResult (s ) )
448           {
449             fprintf(f, "Result\n");
450           }
451         else if (sRef_isParam (s ) )
452           {
453             int param;
454             ctype ct;
455             cstring ctString;
456
457             
458             ct =  sRef_getType (s); 
459             param = sRef_getParam(s);
460
461             ctString =  ctype_dump(ct);
462             
463             fprintf(f, "Param %s %d\n", cstring_toCharsSafe(ctString), (int) param );
464             cstring_free(ctString);
465           }
466         else
467           {
468             u = sRef_getUentry(s);
469             fprintf(f, "%s\n", cstring_toCharsSafe(uentry_rawName (u) ) );
470           }
471         
472       }
473       break;
474       
475     case INTLITERAL:
476       fprintf (f, "%d\n", t->value.intlit);
477       break;
478       
479     default:
480       BADEXIT;
481     }
482   
483 }
484
485
486 /*@only@*/ constraintTerm constraintTerm_undump ( FILE *f)
487 {
488   constraintTermType kind;
489   constraintTerm ret;
490   
491   uentry ue;
492   
493   char * str;
494   char * os;
495
496   str = mstring_create (MAX_DUMP_LINE_LENGTH);
497   os = str;
498   str = fgets(os, MAX_DUMP_LINE_LENGTH, f);
499
500   kind = (constraintTermType) reader_getInt(&str);
501   str = fgets(os, MAX_DUMP_LINE_LENGTH, f);
502
503   switch (kind)
504     {
505       
506     case SREF:
507       {
508         sRef s;
509         char * term;
510         term = reader_getWord(&str);
511         
512         if (strcmp (term, "Result") == 0 )
513           {
514             s = sRef_makeResult();
515           }
516         else if (strcmp (term, "Param" ) == 0 )
517           {
518             int param;
519             char *str2, *ostr2;
520             
521             ctype t;
522
523             reader_checkChar(&str, ' ');
524             str2  = reader_getWord(&str);
525             param = reader_getInt(&str);
526
527             ostr2 = str2;
528             t = ctype_undump(&str2) ;
529             s = sRef_makeParam (param, t );
530             free (ostr2);
531           }
532         else  //This must be an identified that we can search for
533           // in usymTab
534           {
535             cstring termStr = cstring_makeLiteralTemp(term);
536
537             ue = usymtab_lookup (termStr);
538             s = uentry_getSref(ue);
539           }
540         
541         ret = constraintTerm_makesRef(s);
542
543         free(term);
544       }
545       break;
546
547     case EXPRNODE:
548       {
549         sRef s;
550         char * term;
551         cstring termStr;
552                 
553         term = reader_getWord(&str);
554         //This must be an identifier that we can search for
555           // in usymTab
556         termStr = cstring_makeLiteralTemp(term);
557         
558         ue = usymtab_lookup (termStr);
559         s = uentry_getSref(ue);
560         ret = constraintTerm_makesRef(s);
561
562         free (term);
563       }
564       break;
565       
566       
567     case INTLITERAL:
568       {
569         int i;
570
571         i = reader_getInt(&str);
572         ret = constraintTerm_makeIntLiteral (i);
573       }
574       break;
575       
576     default:
577       BADEXIT;
578     }
579   free (os);
580
581   return ret;
582 }
583
584
585
586
This page took 0.084557 seconds and 5 git commands to generate.