CBMC
cpp_type2name.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: C++ Language Module
4 
5 Author: Daniel Kroening, kroening@cs.cmu.edu
6 
7 \*******************************************************************/
8 
11 
12 #include "cpp_type2name.h"
13 
14 #include <string>
15 
16 #include <util/cprover_prefix.h>
17 #include <util/pointer_expr.h>
18 #include <util/type.h>
19 
20 static std::string do_prefix(const std::string &s)
21 {
22  if(s.find(',') != std::string::npos || (!s.empty() && isdigit(s[0])))
23  return std::to_string(s.size())+"_"+s;
24 
25  return s;
26 }
27 
28 static std::string irep2name(const irept &irep)
29 {
30  std::string result;
31 
32  if(is_reference(static_cast<const typet&>(irep)))
33  result+="reference";
34 
35  if(is_rvalue_reference(static_cast<const typet &>(irep)))
36  result += "rvalue_reference";
37 
38  if(irep.id() == ID_frontend_pointer)
39  {
40  if(irep.get_bool(ID_C_reference))
41  result += "reference";
42 
43  if(irep.get_bool(ID_C_rvalue_reference))
44  result += "rvalue_reference";
45  }
46  else if(!irep.id().empty())
47  result+=do_prefix(irep.id_string());
48 
49  if(irep.get_named_sub().empty() && irep.get_sub().empty())
50  return result;
51 
52  result+='(';
53  bool first=true;
54 
55  for(const auto &named_sub : irep.get_named_sub())
56  {
57  if(!irept::is_comment(named_sub.first))
58  {
59  if(first)
60  first = false;
61  else
62  result += ',';
63 
64  result += do_prefix(id2string(named_sub.first));
65 
66  result += '=';
67  result += irep2name(named_sub.second);
68  }
69  }
70 
71  for(const auto &named_sub : irep.get_named_sub())
72  {
73  if(
74  named_sub.first == ID_C_constant || named_sub.first == ID_C_volatile ||
75  named_sub.first == ID_C_restricted)
76  {
77  if(first)
78  first=false;
79  else
80  result+=',';
81  result += do_prefix(id2string(named_sub.first));
82  result+='=';
83  result += irep2name(named_sub.second);
84  }
85  }
86 
87  for(const auto &sub : irep.get_sub())
88  {
89  if(first)
90  first=false;
91  else
92  result+=',';
93  result += irep2name(sub);
94  }
95 
96  result+=')';
97 
98  return result;
99 }
100 
101 std::string cpp_type2name(const typet &type)
102 {
103  std::string result;
104 
105  if(type.get_bool(ID_C_constant))
106  result+="const_";
107 
108  if(type.get_bool(ID_C_restricted))
109  result+="restricted_";
110 
111  if(type.get_bool(ID_C_volatile))
112  result+="volatile_";
113 
114  if(type.id()==ID_empty || type.id()==ID_void)
115  result+="void";
116  else if(type.id() == ID_c_bool)
117  result+="bool";
118  else if(type.id() == ID_bool)
119  result += CPROVER_PREFIX "bool";
120  else if(type.id()==ID_pointer)
121  {
122  if(is_reference(type))
123  result += "ref_" + cpp_type2name(to_reference_type(type).base_type());
124  else if(is_rvalue_reference(type))
125  result += "rref_" + cpp_type2name(to_pointer_type(type).base_type());
126  else
127  result += "ptr_" + cpp_type2name(to_pointer_type(type).base_type());
128  }
129  else if(type.id()==ID_signedbv || type.id()==ID_unsignedbv)
130  {
131  // we try to use #c_type
132  const irep_idt c_type=type.get(ID_C_c_type);
133 
134  if(!c_type.empty())
135  result+=id2string(c_type);
136  else if(type.id()==ID_unsignedbv)
137  result+="unsigned_int";
138  else
139  result+="signed_int";
140  }
141  else if(type.id()==ID_fixedbv || type.id()==ID_floatbv)
142  {
143  // we try to use #c_type
144  const irep_idt c_type=type.get(ID_C_c_type);
145 
146  if(!c_type.empty())
147  result+=id2string(c_type);
148  else
149  result+="double";
150  }
151  else if(type.id()==ID_code)
152  {
153  // we do (args)->(return_type)
154  const code_typet::parameterst &parameters=to_code_type(type).parameters();
155  const typet &return_type=to_code_type(type).return_type();
156 
157  result+='(';
158 
159  for(code_typet::parameterst::const_iterator
160  arg_it=parameters.begin();
161  arg_it!=parameters.end();
162  arg_it++)
163  {
164  if(arg_it!=parameters.begin())
165  result+=',';
166  result+=cpp_type2name(arg_it->type());
167  }
168 
169  result+=')';
170  result+="->(";
171  result+=cpp_type2name(return_type);
172  result+=')';
173  }
174  else
175  {
176  // give up
177  return irep2name(type);
178  }
179 
180  return result;
181 }
182 
183 std::string cpp_expr2name(const exprt &expr)
184 {
185  return irep2name(expr);
186 }
std::vector< parametert > parameterst
Definition: std_types.h:585
const typet & return_type() const
Definition: std_types.h:689
const parameterst & parameters() const
Definition: std_types.h:699
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
bool empty() const
Definition: dstring.h:89
Base class for all expressions.
Definition: expr.h:56
There are a large number of kinds of tree structured or tree-like data in CPROVER.
Definition: irep.h:364
bool get_bool(const irep_idt &name) const
Definition: irep.cpp:57
subt & get_sub()
Definition: irep.h:448
const irep_idt & get(const irep_idt &name) const
Definition: irep.cpp:44
named_subt & get_named_sub()
Definition: irep.h:450
const std::string & id_string() const
Definition: irep.h:391
const irep_idt & id() const
Definition: irep.h:388
static bool is_comment(const irep_idt &name)
Definition: irep.h:460
The type of an expression, extends irept.
Definition: type.h:29
std::string cpp_type2name(const typet &type)
std::string cpp_expr2name(const exprt &expr)
static std::string do_prefix(const std::string &s)
static std::string irep2name(const irept &irep)
C++ Language Module.
#define CPROVER_PREFIX
int isdigit(int c)
Definition: ctype.c:24
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
API to expression classes for Pointers.
bool is_reference(const typet &type)
Returns true if the type is a reference.
Definition: std_types.cpp:145
bool is_rvalue_reference(const typet &type)
Returns if the type is an R value reference.
Definition: std_types.cpp:152
const pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
Definition: pointer_expr.h:93
const reference_typet & to_reference_type(const typet &type)
Cast a typet to a reference_typet.
Definition: pointer_expr.h:162
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition: std_types.h:788
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
Defines typet, type_with_subtypet and type_with_subtypest.