cprover
format_constant.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 
10 #include "format_constant.h"
11 
12 #include "arith_tools.h"
13 #include "fixedbv.h"
14 #include "ieee_float.h"
15 #include "expr.h"
16 #include "std_expr.h"
17 
18 std::string format_constantt::operator()(const exprt &expr)
19 {
20  if(expr.is_constant())
21  {
22  if(expr.type().id()==ID_natural ||
23  expr.type().id()==ID_integer ||
24  expr.type().id()==ID_unsignedbv ||
25  expr.type().id()==ID_signedbv)
26  {
27  mp_integer i;
28  if(to_integer(to_constant_expr(expr), i))
29  return "(number conversion failed)";
30 
31  return integer2string(i);
32  }
33  else if(expr.type().id()==ID_fixedbv)
34  {
35  return fixedbvt(to_constant_expr(expr)).format(*this);
36  }
37  else if(expr.type().id()==ID_floatbv)
38  {
39  return ieee_floatt(to_constant_expr(expr)).format(*this);
40  }
41  }
42  else if(expr.id()==ID_string_constant)
43  return expr.get_string(ID_value);
44 
45  return "(format-constant failed: "+expr.id_string()+")";
46 }
BigInt mp_integer
Definition: mp_arith.h:22
const std::string integer2string(const mp_integer &n, unsigned base)
Definition: mp_arith.cpp:106
std::string operator()(const exprt &expr)
typet & type()
Definition: expr.h:56
const irep_idt & id() const
Definition: irep.h:259
API to expression classes.
std::string format(const format_spect &format_spec) const
Definition: fixedbv.cpp:124
const constant_exprt & to_constant_expr(const exprt &expr)
Cast a generic exprt to a constant_exprt.
Definition: std_expr.h:4463
bool is_constant() const
Definition: expr.cpp:119
std::string format(const format_spect &format_spec) const
Definition: ieee_float.cpp:63
Base class for all expressions.
Definition: expr.h:42
const std::string & get_string(const irep_namet &name) const
Definition: irep.h:272
const std::string & id_string() const
Definition: irep.h:262
bool to_integer(const exprt &expr, mp_integer &int_value)
Definition: arith_tools.cpp:17