cprover
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/type.h>
17 #include <util/std_types.h>
18 
19 static std::string do_prefix(const std::string &s)
20 {
21  if(s.find(',')!=std::string::npos ||
22  (s!="" && isdigit(s[0])))
23  return std::to_string(s.size())+"_"+s;
24 
25  return s;
26 }
27 
28 static void irep2name(const irept &irep, std::string &result)
29 {
30  result="";
31 
32  if(is_reference(static_cast<const typet&>(irep)))
33  result+="reference";
34 
35  if(irep.id()!="")
36  result+=do_prefix(irep.id_string());
37 
38  if(irep.get_named_sub().empty() &&
39  irep.get_sub().empty() &&
40  irep.get_comments().empty())
41  return;
42 
43  result+='(';
44  bool first=true;
45 
47  {
48  if(first)
49  first=false;
50  else
51  result+=',';
52 
53  result+=do_prefix(name2string(it->first));
54 
55  result+='=';
56  std::string tmp;
57  irep2name(it->second, tmp);
58  result+=tmp;
59  }
60 
62  if(it->first==ID_C_constant ||
63  it->first==ID_C_volatile ||
64  it->first==ID_C_restricted)
65  {
66  if(first)
67  first=false;
68  else
69  result+=',';
70  result+=do_prefix(name2string(it->first));
71  result+='=';
72  std::string tmp;
73  irep2name(it->second, tmp);
74  result+=tmp;
75  }
76 
77  forall_irep(it, irep.get_sub())
78  {
79  if(first)
80  first=false;
81  else
82  result+=',';
83  std::string tmp;
84  irep2name(*it, tmp);
85  result+=tmp;
86  }
87 
88  result+=')';
89 }
90 
91 std::string cpp_type2name(const typet &type)
92 {
93  std::string result;
94 
95  if(type.get_bool(ID_C_constant))
96  result+="const_";
97 
98  if(type.get_bool(ID_C_restricted))
99  result+="restricted_";
100 
101  if(type.get_bool(ID_C_volatile))
102  result+="volatile_";
103 
104  if(type.id()==ID_empty || type.id()==ID_void)
105  result+="void";
106  else if(type.id()==ID_bool)
107  result+="bool";
108  else if(type.id()==ID_pointer)
109  {
110  if(is_reference(type))
111  result+="ref_"+cpp_type2name(type.subtype());
112  else if(is_rvalue_reference(type))
113  result+="rref_"+cpp_type2name(type.subtype());
114  else
115  result+="ptr_"+cpp_type2name(type.subtype());
116  }
117  else if(type.id()==ID_signedbv || type.id()==ID_unsignedbv)
118  {
119  // we try to use #c_type
120  const irep_idt c_type=type.get(ID_C_c_type);
121 
122  if(!c_type.empty())
123  result+=id2string(c_type);
124  else if(type.id()==ID_unsignedbv)
125  result+="unsigned_int";
126  else
127  result+="signed_int";
128  }
129  else if(type.id()==ID_fixedbv || type.id()==ID_floatbv)
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
137  result+="double";
138  }
139  else if(type.id()==ID_code)
140  {
141  // we do (args)->(return_type)
142  const code_typet::parameterst &parameters=to_code_type(type).parameters();
143  const typet &return_type=to_code_type(type).return_type();
144 
145  result+='(';
146 
147  for(code_typet::parameterst::const_iterator
148  arg_it=parameters.begin();
149  arg_it!=parameters.end();
150  arg_it++)
151  {
152  if(arg_it!=parameters.begin())
153  result+=',';
154  result+=cpp_type2name(arg_it->type());
155  }
156 
157  result+=')';
158  result+="->(";
159  result+=cpp_type2name(return_type);
160  result+=')';
161  }
162  else
163  {
164  // give up
165  std::string tmp;
166  irep2name(type, tmp);
167  return tmp;
168  }
169 
170  return result;
171 }
172 
173 std::string cpp_expr2name(const exprt &expr)
174 {
175  std::string tmp;
176  irep2name(expr, tmp);
177  return tmp;
178 }
The type of an expression.
Definition: type.h:22
const std::string & id2string(const irep_idt &d)
Definition: irep.h:43
const code_typet & to_code_type(const typet &type)
Cast a generic typet to a code_typet.
Definition: std_types.h:987
std::vector< parametert > parameterst
Definition: std_types.h:767
bool get_bool(const irep_namet &name) const
Definition: irep.cpp:240
#define forall_named_irep(it, irep)
Definition: irep.h:69
subt & get_sub()
Definition: irep.h:245
const irep_idt & id() const
Definition: irep.h:189
const std::string & name2string(const irep_namet &n)
Definition: irep.h:52
C++ Language Module.
bool is_reference(const typet &type)
TO_BE_DOCUMENTED.
Definition: std_types.cpp:105
const irep_idt & get(const irep_namet &name) const
Definition: irep.cpp:213
named_subt & get_comments()
Definition: irep.h:249
std::string cpp_type2name(const typet &type)
Base class for tree-like data structures with sharing.
Definition: irep.h:86
named_subt & get_named_sub()
Definition: irep.h:247
API to type classes.
Base class for all expressions.
Definition: expr.h:42
bool is_rvalue_reference(const typet &type)
TO_BE_DOCUMENTED.
Definition: std_types.cpp:111
const parameterst & parameters() const
Definition: std_types.h:905
std::string to_string(const string_constraintt &expr)
Used for debug printing.
static void irep2name(const irept &irep, std::string &result)
const std::string & id_string() const
Definition: irep.h:192
static std::string do_prefix(const std::string &s)
std::string cpp_expr2name(const exprt &expr)
const typet & subtype() const
Definition: type.h:33
bool empty() const
Definition: dstring.h:61
const typet & return_type() const
Definition: std_types.h:895
#define forall_irep(it, irep)
Definition: irep.h:61