CBMC
std_code_base.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Data structures representing statements in a program
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 #ifndef CPROVER_UTIL_STD_CODE_BASE_H
10 #define CPROVER_UTIL_STD_CODE_BASE_H
11 
12 #include "expr_cast.h"
13 #include "invariant.h"
14 #include "std_types.h"
15 #include "validate.h"
16 
28 class codet : public exprt
29 {
30 public:
34  explicit codet(const irep_idt &statement) : exprt(ID_code, empty_typet())
35  {
36  set_statement(statement);
37  }
38 
39  codet(const irep_idt &statement, source_locationt loc)
40  : exprt(ID_code, empty_typet(), std::move(loc))
41  {
42  set_statement(statement);
43  }
44 
49  explicit codet(const irep_idt &statement, operandst _op) : codet(statement)
50  {
51  operands() = std::move(_op);
52  }
53 
54  codet(const irep_idt &statement, operandst op, source_locationt loc)
55  : codet(statement, std::move(loc))
56  {
57  operands() = std::move(op);
58  }
59 
60  void set_statement(const irep_idt &statement)
61  {
62  set(ID_statement, statement);
63  }
64 
65  const irep_idt &get_statement() const
66  {
67  return get(ID_statement);
68  }
69 
71  const codet &first_statement() const;
73  const codet &last_statement() const;
74 
75  using exprt::op0;
76  using exprt::op1;
77  using exprt::op2;
78  using exprt::op3;
79 };
80 
81 namespace detail // NOLINT
82 {
83 template <typename Tag>
84 inline bool can_cast_code_impl(const exprt &expr, const Tag &tag)
85 {
86  if(const auto ptr = expr_try_dynamic_cast<codet>(expr))
87  {
88  return ptr->get_statement() == tag;
89  }
90  return false;
91 }
92 
93 } // namespace detail
94 
95 template <>
96 inline bool can_cast_expr<codet>(const exprt &base)
97 {
98  return base.id() == ID_code;
99 }
100 
101 // to_code has no validation other than checking the id(), so no validate_expr
102 // is provided for codet
103 
104 inline const codet &to_code(const exprt &expr)
105 {
106  PRECONDITION(expr.id() == ID_code);
107  return static_cast<const codet &>(expr);
108 }
109 
110 inline codet &to_code(exprt &expr)
111 {
112  PRECONDITION(expr.id() == ID_code);
113  return static_cast<codet &>(expr);
114 }
115 
116 #endif // CPROVER_UTIL_STD_CODE_BASE_H
Data structure for representing an arbitrary statement in a program.
Definition: std_code_base.h:29
codet(const irep_idt &statement, source_locationt loc)
Definition: std_code_base.h:39
codet(const irep_idt &statement)
Definition: std_code_base.h:34
codet(const irep_idt &statement, operandst op, source_locationt loc)
Definition: std_code_base.h:54
codet & first_statement()
In the case of a codet type that represents multiple statements, return the first of them.
Definition: std_code.cpp:18
codet & last_statement()
In the case of a codet type that represents multiple statements, return the last of them.
Definition: std_code.cpp:52
void set_statement(const irep_idt &statement)
Definition: std_code_base.h:60
const irep_idt & get_statement() const
Definition: std_code_base.h:65
codet(const irep_idt &statement, operandst _op)
Definition: std_code_base.h:49
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
The empty type.
Definition: std_types.h:51
Base class for all expressions.
Definition: expr.h:56
std::vector< exprt > operandst
Definition: expr.h:58
exprt & op3()
Definition: expr.h:142
exprt & op1()
Definition: expr.h:136
exprt & op2()
Definition: expr.h:139
exprt & op0()
Definition: expr.h:133
operandst & operands()
Definition: expr.h:94
const irep_idt & get(const irep_idt &name) const
Definition: irep.cpp:44
void set(const irep_idt &name, const irep_idt &value)
Definition: irep.h:408
const irep_idt & id() const
Definition: irep.h:384
Templated functions to cast to specific exprt-derived classes.
bool can_cast_code_impl(const exprt &expr, const Tag &tag)
Definition: std_code_base.h:84
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
bool can_cast_expr< codet >(const exprt &base)
Definition: std_code_base.h:96
const codet & to_code(const exprt &expr)
Pre-defined types.