CBMC
enum_encoding.cpp
Go to the documentation of this file.
1 // Author: Diffblue Ltd.
2 
3 #include "enum_encoding.h"
4 
5 #include <util/c_types.h>
6 #include <util/expr_cast.h>
7 #include <util/namespace.h>
8 
9 #include <queue>
10 
11 // Internal function to lower inner types of compound types (at the moment only
12 // arrays)
13 static typet encode(typet type, const namespacet &ns)
14 {
15  std::queue<typet *> work_queue;
16  work_queue.push(&type);
17  while(!work_queue.empty())
18  {
19  typet &current = *work_queue.front();
20  work_queue.pop();
21  if(const auto c_enum_tag = type_try_dynamic_cast<c_enum_tag_typet>(current))
22  {
23  // Replace the type of the c_enum_tag with the underlying type of the enum
24  // it points to
25  current = ns.follow_tag(*c_enum_tag).underlying_type();
26  }
27  if(const auto array = type_try_dynamic_cast<array_typet>(current))
28  {
29  // Add the type of the array's elements to the queue to be explored
30  work_queue.push(&array->element_type());
31  }
32  }
33  return type;
34 }
35 
36 // Worklist algorithm to lower enum types of expr and its sub-expressions.
37 exprt lower_enum(exprt expr, const namespacet &ns)
38 {
39  std::queue<exprt *> work_queue;
40  work_queue.push(&expr);
41  while(!work_queue.empty())
42  {
43  exprt &current = *work_queue.front();
44  work_queue.pop();
45 
46  // Replace the type of the expression node with the encoded one
47  current.type() = encode(current.type(), ns);
48 
49  for(auto &operand : current.operands())
50  work_queue.push(&operand);
51  }
52  return expr;
53 }
Base class for all expressions.
Definition: expr.h:56
typet & type()
Return the type of the expression.
Definition: expr.h:84
operandst & operands()
Definition: expr.h:94
const union_typet & follow_tag(const union_tag_typet &) const
Follow type tag of union type.
Definition: namespace.cpp:63
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:94
The type of an expression, extends irept.
Definition: type.h:29
static typet encode(typet type, const namespacet &ns)
exprt lower_enum(exprt expr, const namespacet &ns)
Function to lower expr and its sub-expressions containing enum types.
Templated functions to cast to specific exprt-derived classes.