cprover
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 "format_expr.h"
11 #include "std_types.h"
12 
13 #include <ostream>
14 
16 static std::ostream &format_rec(std::ostream &os, const struct_typet &src)
17 {
18  os << "struct"
19  << " {";
20  bool first = true;
21 
22  for(const auto &c : src.components())
23  {
24  if(first)
25  first = false;
26  else
27  os << ',';
28 
29  os << ' ' << format(c.type()) << ' ' << c.get_name();
30  }
31 
32  return os << " }";
33 }
34 
35 // The below generates a string in a generic syntax
36 // that is inspired by C/C++/Java, and is meant for debugging
37 // purposes.
38 std::ostream &format_rec(std::ostream &os, const typet &type)
39 {
40  const auto &id = type.id();
41 
42  if(id == ID_pointer)
43  return os << '*' << format(type.subtype());
44  else if(id == ID_array)
45  {
46  const auto &t = to_array_type(type);
47  if(t.is_complete())
48  return os << format(type.subtype()) << '[' << format(t.size()) << ']';
49  else
50  return os << format(type.subtype()) << "[]";
51  }
52  else if(id == ID_struct)
53  return format_rec(os, to_struct_type(type));
54  else if(id == ID_symbol)
55  return os << "symbol_type " << to_symbol_type(type).get_identifier();
56  else
57  return os << id;
58 }
The type of an expression.
Definition: type.h:22
static std::ostream & format_rec(std::ostream &os, const struct_typet &src)
format a struct_typet
Definition: format_type.cpp:16
const symbol_typet & to_symbol_type(const typet &type)
Cast a generic typet to a symbol_typet.
Definition: std_types.h:139
const componentst & components() const
Definition: std_types.h:245
Structure type.
Definition: std_types.h:297
const irep_idt & id() const
Definition: irep.h:259
const struct_typet & to_struct_type(const typet &type)
Cast a generic typet to a struct_typet.
Definition: std_types.h:318
API to type classes.
const array_typet & to_array_type(const typet &type)
Cast a generic typet to an array_typet.
Definition: std_types.h:1054
const typet & subtype() const
Definition: type.h:33
const irep_idt & get_identifier() const
Definition: std_types.h:123
static format_containert< T > format(const T &o)
Definition: format.h:35