/*******************************************************************\

Module: Set Claims

Author: Daniel Kroening, kroening@cs.cmu.edu

\*******************************************************************/

#include <i2string.h>

#include "set_claims.h"

/*******************************************************************\

Function: set_claims

  Inputs:

 Outputs:

 Purpose:

\*******************************************************************/

void convert_claims(
  const std::list<std::string> &claims,
  std::set<unsigned> &unsigned_claims)
{
  for(std::list<std::string>::const_iterator
      it=claims.begin();
      it!=claims.end();
      it++)
    unsigned_claims.insert(atoi(it->c_str()));
}

/*******************************************************************\

Function: set_claims

  Inputs:

 Outputs:

 Purpose:

\*******************************************************************/

void set_claims(
  goto_programt &goto_program,
  const std::set<unsigned> &claims,
  unsigned &count)
{
  for(goto_programt::instructionst::iterator
      it=goto_program.instructions.begin();
      it!=goto_program.instructions.end();
      it++)
  {
    if(it->is_assert())
    {
      count++;
      
      // assume-guarantee
      if(claims.find(count)==claims.end())
        it->type=ASSUME;
    }
  }
}

/*******************************************************************\

Function: set_claims

  Inputs:

 Outputs:

 Purpose:

\*******************************************************************/

void set_claims(
  goto_programt &goto_program,
  const std::list<std::string> &claims)
{
  std::set<unsigned> unsigned_claims;
  
  convert_claims(claims, unsigned_claims);

  if(unsigned_claims.empty()) return;

  unsigned count=0;

  set_claims(goto_program, unsigned_claims, count);
  
  unsigned largest=*(--unsigned_claims.end());
  
  if(count<largest)
    throw "claim "+i2string(largest)+" not found";
}

/*******************************************************************\

Function: set_claims

  Inputs:

 Outputs:

 Purpose:

\*******************************************************************/

void set_claims(
  goto_functionst &goto_functions,
  const std::list<std::string> &claims)
{
  std::set<unsigned> unsigned_claims;
  
  convert_claims(claims, unsigned_claims);

  if(unsigned_claims.empty()) return;

  unsigned count=0;

  for(goto_functionst::function_mapt::iterator
      it=goto_functions.function_map.begin();
      it!=goto_functions.function_map.end();
      it++)
    if(!it->second.is_inlined())
      set_claims(it->second.body, unsigned_claims, count);

  unsigned largest=*(--unsigned_claims.end());
  
  if(count<largest)
    throw "claim "+i2string(largest)+" not found";
}
