CBMC
format_type.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 #include "format_type.h"
10 #include "c_types.h"
11 #include "format_expr.h"
12 #include "mathematical_types.h"
13 #include "pointer_expr.h"
14 #include "std_types.h"
15 
16 #include <ostream>
17 
19 static std::ostream &format_rec(std::ostream &os, const struct_typet &src)
20 {
21  os << "struct"
22  << " {";
23  bool first = true;
24 
25  for(const auto &c : src.components())
26  {
27  if(first)
28  first = false;
29  else
30  os << ',';
31 
32  os << ' ' << format(c.type()) << ' ' << c.get_name();
33  }
34 
35  return os << " }";
36 }
37 
39 static std::ostream &format_rec(std::ostream &os, const union_typet &src)
40 {
41  os << "union"
42  << " {";
43  bool first = true;
44 
45  for(const auto &c : src.components())
46  {
47  if(first)
48  first = false;
49  else
50  os << ',';
51 
52  os << ' ' << format(c.type()) << ' ' << c.get_name();
53  }
54 
55  return os << " }";
56 }
57 
58 // The below generates a string in a generic syntax
59 // that is inspired by C/C++/Java, and is meant for debugging
60 // purposes.
61 std::ostream &format_rec(std::ostream &os, const typet &type)
62 {
63  const auto &id = type.id();
64 
65  if(id == ID_pointer)
66  return os << format(to_pointer_type(type).base_type()) << '*';
67  else if(id == ID_array)
68  {
69  const auto &t = to_array_type(type);
70  if(t.is_complete())
71  return os << format(t.element_type()) << '[' << format(t.size()) << ']';
72  else
73  return os << format(t.element_type()) << "[]";
74  }
75  else if(id == ID_struct)
76  return format_rec(os, to_struct_type(type));
77  else if(id == ID_union)
78  return format_rec(os, to_union_type(type));
79  else if(id == ID_union_tag)
80  return os << "union " << to_union_tag_type(type).get_identifier();
81  else if(id == ID_struct_tag)
82  return os << "struct " << to_struct_tag_type(type).get_identifier();
83  else if(id == ID_c_enum_tag)
84  return os << "c_enum " << to_c_enum_tag_type(type).get_identifier();
85  else if(id == ID_signedbv)
86  return os << "signedbv[" << to_signedbv_type(type).get_width() << ']';
87  else if(id == ID_unsignedbv)
88  return os << "unsignedbv[" << to_unsignedbv_type(type).get_width() << ']';
89  else if(id == ID_bv)
90  return os << "bv[" << to_bitvector_type(type).get_width() << ']';
91  else if(id == ID_floatbv)
92  return os << "floatbv[" << to_floatbv_type(type).get_width() << ']';
93  else if(id == ID_c_bool)
94  return os << "c_bool[" << to_c_bool_type(type).get_width() << ']';
95  else if(id == ID_bool)
96  return os << "\xf0\x9d\x94\xb9"; // u+1D539, 'B'
97  else if(id == ID_integer)
98  return os << "\xe2\x84\xa4"; // u+2124, 'Z'
99  else if(id == ID_natural)
100  return os << "\xe2\x84\x95"; // u+2115, 'N'
101  else if(id == ID_range)
102  {
103  auto &range_type = to_range_type(type);
104  return os << "{ " << range_type.get_from() << ", ..., "
105  << range_type.get_to() << " }";
106  }
107  else if(id == ID_rational)
108  return os << "\xe2\x84\x9a"; // u+211A, 'Q'
109  else if(id == ID_mathematical_function)
110  {
111  const auto &mathematical_function = to_mathematical_function_type(type);
112  bool first = true;
113  for(const auto &domain : mathematical_function.domain())
114  {
115  if(first)
116  first = false;
117  else
118  os << u8" \u00d7 "; // ×
119  os << format(domain);
120  }
121  os << u8" \u2192 "; // → -- we don't use ⟶ since that doesn't render well
122  return os << format(mathematical_function.codomain());
123  }
124  else
125  return os << id;
126 }
const floatbv_typet & to_floatbv_type(const typet &type)
Cast a typet to a floatbv_typet.
const signedbv_typet & to_signedbv_type(const typet &type)
Cast a typet to a signedbv_typet.
const bitvector_typet & to_bitvector_type(const typet &type)
Cast a typet to a bitvector_typet.
const unsignedbv_typet & to_unsignedbv_type(const typet &type)
Cast a typet to an unsignedbv_typet.
uint64_t u8
Definition: bytecode_info.h:58
const union_tag_typet & to_union_tag_type(const typet &type)
Cast a typet to a union_tag_typet.
Definition: c_types.h:224
const union_typet & to_union_type(const typet &type)
Cast a typet to a union_typet.
Definition: c_types.h:184
const c_enum_tag_typet & to_c_enum_tag_type(const typet &type)
Cast a typet to a c_enum_tag_typet.
Definition: c_types.h:377
const c_bool_typet & to_c_bool_type(const typet &type)
Cast a typet to a c_bool_typet.
Definition: c_types.h:128
std::size_t get_width() const
Definition: std_types.h:925
const irep_idt & id() const
Definition: irep.h:388
Structure type, corresponds to C style structs.
Definition: std_types.h:231
const componentst & components() const
Definition: std_types.h:147
const irep_idt & get_identifier() const
Definition: std_types.h:410
The type of an expression, extends irept.
Definition: type.h:29
The union type.
Definition: c_types.h:147
static format_containert< T > format(const T &o)
Definition: format.h:37
static std::ostream & format_rec(std::ostream &os, const struct_typet &src)
format a struct_typet
Definition: format_type.cpp:19
Mathematical types.
const mathematical_function_typet & to_mathematical_function_type(const typet &type)
Cast a typet to a mathematical_function_typet.
API to expression classes for Pointers.
const pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
Definition: pointer_expr.h:93
Pre-defined types.
const range_typet & to_range_type(const typet &type)
Cast a typet to a range_typet.
Definition: std_types.h:1037
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
Definition: std_types.h:308
const array_typet & to_array_type(const typet &type)
Cast a typet to an array_typet.
Definition: std_types.h:888
const struct_tag_typet & to_struct_tag_type(const typet &type)
Cast a typet to a struct_tag_typet.
Definition: std_types.h:518