]> andersk Git - splint.git/blobdiff - src/constraintTerm.c
*** empty log message ***
[splint.git] / src / constraintTerm.c
index eded0598b7afd20d246f5679770e33aff7e7b334..42f607ca7335b3577b524517dd2de3544a0fe69a 100644 (file)
 # include "cgrammar_tokens.h"
 
 # include "exprChecks.h"
-# include "aliasChecks.h"
 # include "exprNodeSList.h"
 
 /*@-czechfcns@*/
 
 //#include "constraintExpr.h"
 
-/*@access exprNode, constraintTermValue @*/
+/*@access exprNode @*/
 
-/*@unused@*/ static bool constraintTerm_same (constraintTerm term1, constraintTerm term2) ;
+bool constraintTerm_isDefined (constraintTerm t)
+{
+  return t != NULL;
+}
+
+/*@unused@*/ static bool constraintTerm_same (constraintTerm p_term1, constraintTerm p_term2) ;
 
 void constraintTerm_free (/*@only@*/ constraintTerm term)
 {
-  llassert(constraintTerm_isDefined(term) );
+  llassert (constraintTerm_isDefined (term));
+
   fileloc_free (term->loc);
   
   switch (term->kind) 
@@ -34,7 +39,7 @@ void constraintTerm_free (/*@only@*/ constraintTerm term)
       break;
     case SREF:
       /* sref */
-      //sRef_free (term->value.sref);
+      sRef_free (term->value.sref);
       break;
     case INTLITERAL:
       /* don't free an int */
@@ -44,6 +49,8 @@ void constraintTerm_free (/*@only@*/ constraintTerm term)
       /* type was set incorrectly */
       llcontbug (message("constraintTerm_free type was set incorrectly"));
     }
+  //  term->value.intlit = 0;
+  term->kind =  ERRORBADCONSTRAINTTERMTYPE;
   free (term);
 }
 
@@ -95,7 +102,7 @@ constraintTerm constraintTerm_simplify (/*@returned@*/ constraintTerm term) /*@m
       if ( exprNode_knownIntValue (term->value.expr ) )
        {
          long int temp;
-         #warning is this a leak?
+
          temp  = exprNode_getLongValue (term->value.expr);
          term->value.intlit = (int)temp;
          term->kind = INTLITERAL;
@@ -106,6 +113,7 @@ constraintTerm constraintTerm_simplify (/*@returned@*/ constraintTerm term) /*@m
 
 fileloc constraintTerm_getFileloc (constraintTerm t)
 {
+  llassert (constraintTerm_isDefined (t));
   return (fileloc_copy (t->loc) );
 }
 
@@ -124,7 +132,7 @@ constraintTermType constraintTerm_getKind (constraintTerm t)
   return (t->value.sref);
 }
 
-/*@only@*/ constraintTerm constraintTerm_makeExprNode (/*@dependent@*/ exprNode e)
+/*@only@*/ constraintTerm constraintTerm_makeExprNode (/*@dependent@*/  exprNode e)
 {
   constraintTerm ret = new_constraintTermExpr();
   ret->loc =  fileloc_copy(exprNode_getfileloc(e));
@@ -134,7 +142,7 @@ constraintTermType constraintTerm_getKind (constraintTerm t)
   return ret;
 }
 
-/*@only@*/ constraintTerm constraintTerm_makesRef  (/*@exposed@*/ sRef s)
+/*@only@*/ constraintTerm constraintTerm_makesRef  (/*@temp@*/ /*@observer@*/ sRef s)
 {
   constraintTerm ret = new_constraintTermExpr();
   ret->loc =  fileloc_undefined;
@@ -144,12 +152,29 @@ constraintTermType constraintTerm_getKind (constraintTerm t)
   return ret;
 }
 
+
+
 constraintTerm constraintTerm_copy (constraintTerm term)
 {
   constraintTerm ret;
   ret = new_constraintTermExpr();
   ret->loc = fileloc_copy (term->loc);
-  constraintTermValue_copy (ret->value, term->value);
+  
+  switch (term->kind)
+    {
+    case EXPRNODE:
+      ret->value.expr = term->value.expr;
+      break;
+    case INTLITERAL:
+      ret->value.intlit = term->value.intlit;
+      break;
+      
+    case SREF:
+      ret->value.sref = sRef_saveCopy(term->value.sref);
+      break;
+    default:
+      BADEXIT;
+    }
   ret->kind = term->kind;
   return ret;
 }
@@ -158,7 +183,7 @@ constraintTerm constraintTerm_setFileloc (/*@returned@*/ constraintTerm term, fi
 {
   llassert(term != NULL);
 
-  if (term->loc != fileloc_undefined)
+  if ( fileloc_isDefined(  term->loc ) )
     fileloc_free(term->loc);
 
   term->loc = fileloc_copy(loc);
@@ -180,7 +205,7 @@ static cstring constraintTerm_getName (constraintTerm term)
       s = message ("%s", exprNode_unparse (term->value.expr) );
       break;
     case INTLITERAL:
-      s = message (" %d ", term->value.intlit);
+      s = message (" %d ", (int) term->value.intlit);
       break;
       
     case SREF:
@@ -239,7 +264,7 @@ cstring constraintTerm_print (constraintTerm term)  /*@*/
                   fileloc_unparse (term->loc) );
       break;
     case INTLITERAL:
-      s = message (" %d ", term->value.intlit);
+      s = message (" %d ", (int)term->value.intlit);
       break;
       
     case SREF:
@@ -254,7 +279,7 @@ cstring constraintTerm_print (constraintTerm term)  /*@*/
 }
 
 
-constraintTerm constraintTerm_makeIntLiteral (int i)
+constraintTerm constraintTerm_makeIntLiteral (long i)
 {
   constraintTerm ret = new_constraintTermExpr();
   ret->value.intlit = i;
@@ -266,15 +291,63 @@ constraintTerm constraintTerm_makeIntLiteral (int i)
 bool constraintTerm_canGetValue (constraintTerm term)
 {
   if (term->kind == INTLITERAL)
-    return TRUE;
+    {
+      return TRUE;
+    }
+  else if (term->kind == SREF)
+    {
+      if (sRef_hasValue (term->value.sref))
+       {
+         multiVal mval = sRef_getValue (term->value.sref);
+
+         return multiVal_isInt (mval); /* for now, only try to deal with int values */
+       }
+      else
+       {
+         return FALSE;
+       }
+    }
+  else if (term->kind == EXPRNODE)
+    {
+      return FALSE;
+    }
   else
-    return FALSE;
+    {
+      return FALSE;
+    }
 }
 
-int constraintTerm_getValue (constraintTerm term) 
+long constraintTerm_getValue (constraintTerm term) 
 {
-  llassert (term->kind == INTLITERAL);
-  return term->value.intlit;
+  llassert (constraintTerm_canGetValue (term));
+
+  if (term->kind == INTLITERAL)
+    {
+      return term->value.intlit; 
+    }
+  else if (term->kind == SREF)
+    {
+      if (sRef_hasValue (term->value.sref))
+       {
+         multiVal mval = sRef_getValue (term->value.sref);
+
+         return multiVal_forceInt (mval); /* for now, only try to deal with int values */
+       }
+      else
+       {
+         BADBRANCH;
+       }
+    }
+  else if (term->kind == EXPRNODE)
+    {
+      BADBRANCH;
+    }
+  else
+    {
+      BADBRANCH;
+    }
+
+  BADBRANCH;
 }
 
 /* same and similar are similar but not the same*/
@@ -355,45 +428,47 @@ bool constraintTerm_similar (constraintTerm term1, constraintTerm term2)
   
   llassert (term1 !=NULL && term2 !=NULL);
   
-  if ( (term1->kind == INTLITERAL) && (term2->kind == INTLITERAL) )
+  if (constraintTerm_canGetValue (term1) && constraintTerm_canGetValue (term2))
+    /* evans 2001-07-24: was (term1->kind == INTLITERAL) && (term2->kind == INTLITERAL) ) */
     {
-      int t1, t2;
-      llassert (constraintTerm_canGetValue(term1) );
-      t1 = constraintTerm_getValue (term1);
+      long t1, t2;
 
-      llassert (constraintTerm_canGetValue(term2) );
+      t1 = constraintTerm_getValue (term1);
       t2 = constraintTerm_getValue (term2);
-      if (t1 == t2)
-       return TRUE;
-      
-       return FALSE;
+
+      return (t1 == t2);
     }
-    
+  
+  if (constraintTerm_canGetValue (term1) || constraintTerm_canGetValue (term2))
+    {
+      /* evans 2001-07-24: is this right? */ /*@i534@*/
+      return FALSE;
+    }
+
   s1 = constraintTerm_getsRef (term1);
   s2 = constraintTerm_getsRef (term2);
 
-  if ( ! (s1 && s2) )
+  if (!(sRef_isValid(s1) && sRef_isValid(s2)))
     {
       return FALSE;
     }
   
DPRINTF ( (message
 DPRINTF( (message
            ("Comparing srefs for %s and  %s ", constraintTerm_print(term1), constraintTerm_print(term2)
             )
            )
           );
- if (sRef_similarRelaxed(s1, s2)   || sRef_sameName (s1, s2) )
-   {
-     DPRINTF ((message (" %s and %s are same", constraintTerm_print(term1), constraintTerm_print(term2)  )  ));
-     return TRUE;
-   }
- else
-   {
-     DPRINTF ((message (" %s and %s are not same", constraintTerm_print(term1), constraintTerm_print(term2)  )  ));
-     return FALSE;
-   }     
-    
+  
+  if (sRef_similarRelaxed(s1, s2)   || sRef_sameName (s1, s2) )
+    {
+      DPRINTF ((message (" %s and %s are same", constraintTerm_print(term1), constraintTerm_print(term2)  )  ));
+      return TRUE;
+    }
+  else
+    {
+      DPRINTF ((message (" %s and %s are not same", constraintTerm_print(term1), constraintTerm_print(term2)  )  ));
+      return FALSE;
+    }       
 }
 
 void constraintTerm_dump ( /*@observer@*/ constraintTerm t,  FILE *f)
@@ -416,7 +491,8 @@ void constraintTerm_dump ( /*@observer@*/ constraintTerm t,  FILE *f)
       
     case EXPRNODE:
       u = exprNode_getUentry(t->value.expr);
-      fprintf(f, "%s\n", uentry_rawName (u) );
+      fprintf(f, "%s\n", cstring_toCharsSafe( uentry_rawName (u) )
+             );
       break;
       
     case SREF:
@@ -441,20 +517,20 @@ void constraintTerm_dump ( /*@observer@*/ constraintTerm t,  FILE *f)
 
            ctString =  ctype_dump(ct);
            
-           fprintf(f, "Param %s %d\n", ctString, (int) param );
+           fprintf(f, "Param %s %d\n", cstring_toCharsSafe(ctString), (int) param );
            cstring_free(ctString);
          }
        else
          {
            u = sRef_getUentry(s);
-           fprintf(f, "%s\n", uentry_rawName (u) );
+           fprintf(f, "%s\n", cstring_toCharsSafe(uentry_rawName (u) ) );
          }
        
       }
       break;
       
     case INTLITERAL:
-      fprintf (f, "%d\n", t->value.intlit);
+      fprintf (f, "%ld\n", t->value.intlit);
       break;
       
     default:
@@ -466,7 +542,6 @@ void constraintTerm_dump ( /*@observer@*/ constraintTerm t,  FILE *f)
 
 /*@only@*/ constraintTerm constraintTerm_undump ( FILE *f)
 {
-  fileloc loc;
   constraintTermType kind;
   constraintTerm ret;
   
@@ -479,7 +554,7 @@ void constraintTerm_dump ( /*@observer@*/ constraintTerm t,  FILE *f)
   os = str;
   str = fgets(os, MAX_DUMP_LINE_LENGTH, f);
 
-  kind = (constraintTermType) getInt(&str);
+  kind = (constraintTermType) reader_getInt(&str);
   str = fgets(os, MAX_DUMP_LINE_LENGTH, f);
 
   switch (kind)
@@ -489,11 +564,11 @@ void constraintTerm_dump ( /*@observer@*/ constraintTerm t,  FILE *f)
       {
        sRef s;
        char * term;
-       term = getWord(&str);
+       term = reader_getWord(&str);
        
        if (strcmp (term, "Result") == 0 )
          {
-           s = sRef_makeResult();
+           s = sRef_makeResult (ctype_unknown);
          }
        else if (strcmp (term, "Param" ) == 0 )
          {
@@ -502,9 +577,9 @@ void constraintTerm_dump ( /*@observer@*/ constraintTerm t,  FILE *f)
            
            ctype t;
 
-           checkChar(&str, ' ');
-           str2  = getWord(&str);
-           param = getInt(&str);
+           reader_checkChar(&str, ' ');
+           str2  = reader_getWord(&str);
+           param = reader_getInt(&str);
 
            ostr2 = str2;
            t = ctype_undump(&str2) ;
@@ -514,8 +589,9 @@ void constraintTerm_dump ( /*@observer@*/ constraintTerm t,  FILE *f)
        else  //This must be an identified that we can search for
          // in usymTab
          {
-           
-           ue = usymtab_lookup (term);
+           cstring termStr = cstring_makeLiteralTemp(term);
+
+           ue = usymtab_lookup (termStr);
            s = uentry_getSref(ue);
          }
        
@@ -529,12 +605,14 @@ void constraintTerm_dump ( /*@observer@*/ constraintTerm t,  FILE *f)
       {
        sRef s;
        char * term;
+       cstring termStr;
                
-       term = getWord(&str);
+       term = reader_getWord(&str);
        //This must be an identifier that we can search for
          // in usymTab
+       termStr = cstring_makeLiteralTemp(term);
        
-       ue = usymtab_lookup (term);
+       ue = usymtab_lookup (termStr);
        s = uentry_getSref(ue);
        ret = constraintTerm_makesRef(s);
 
@@ -547,7 +625,7 @@ void constraintTerm_dump ( /*@observer@*/ constraintTerm t,  FILE *f)
       {
        int i;
 
-       i = getInt(&str);
+       i = reader_getInt(&str);
        ret = constraintTerm_makeIntLiteral (i);
       }
       break;
This page took 0.05909 seconds and 4 git commands to generate.