CBMC
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 "std_expr.h"
16 #include "string_constant.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 id2string(to_string_constant(expr).get_value());
44 
45  return "(format-constant failed: "+expr.id_string()+")";
46 }
format_constantt::operator()
std::string operator()(const exprt &expr)
Definition: format_constant.cpp:18
ieee_floatt
Definition: ieee_float.h:116
mp_integer
BigInt mp_integer
Definition: smt_terms.h:17
arith_tools.h
fixedbv.h
to_string_constant
const string_constantt & to_string_constant(const exprt &expr)
Definition: string_constant.h:40
string_constant.h
exprt
Base class for all expressions.
Definition: expr.h:55
to_integer
bool to_integer(const constant_exprt &expr, mp_integer &int_value)
Convert a constant expression expr to an arbitrary-precision integer.
Definition: arith_tools.cpp:20
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:84
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:47
irept::id_string
const std::string & id_string() const
Definition: irep.h:399
irept::id
const irep_idt & id() const
Definition: irep.h:396
format_constant.h
ieee_float.h
exprt::is_constant
bool is_constant() const
Return whether the expression is a constant.
Definition: expr.cpp:27
fixedbvt
Definition: fixedbv.h:41
ieee_floatt::format
std::string format(const format_spect &format_spec) const
Definition: ieee_float.cpp:69
std_expr.h
fixedbvt::format
std::string format(const format_spect &format_spec) const
Definition: fixedbv.cpp:134
to_constant_expr
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
Definition: std_expr.h:2992
integer2string
const std::string integer2string(const mp_integer &n, unsigned base)
Definition: mp_arith.cpp:103