Skip to content

Commit 698be3d

Browse files
committed
move codet into separate header file
The codet class is currently shared between the front-end code classes and the classes that represent goto-instructions. This commit moves this class into a separate header file so it can be consumed by both without including everything else.
1 parent 268a0d0 commit 698be3d

File tree

2 files changed

+163
-148
lines changed

2 files changed

+163
-148
lines changed

src/util/std_code.h

Lines changed: 1 addition & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -12,155 +12,8 @@ Author: Daniel Kroening, kroening@kroening.com
1212

1313
#include <list>
1414

15-
#include "expr_cast.h"
16-
#include "invariant.h"
15+
#include "std_code_base.h"
1716
#include "std_expr.h"
18-
#include "validate.h"
19-
#include "validate_code.h"
20-
21-
/// Data structure for representing an arbitrary statement in a program. Every
22-
/// specific type of statement (e.g. block of statements, assignment,
23-
/// if-then-else statement...) is represented by a subtype of `codet`.
24-
/// `codet`s are represented to be subtypes of \ref exprt since statements can
25-
/// occur in an expression context in C: for example, the assignment `x = y;`
26-
/// is an expression with return value `y`. For other types of statements in an
27-
/// expression context, see e.g.
28-
/// https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html.
29-
/// To distinguish a `codet` from other [exprts](\ref exprt), we set its
30-
/// [id()](\ref irept::id) to `ID_code`. To distinguish different types of
31-
/// `codet`, we use a named sub `ID_statement`.
32-
class codet:public exprt
33-
{
34-
public:
35-
/// \param statement: Specifies the type of the `codet` to be constructed,
36-
/// e.g. `ID_block` for a \ref code_blockt or `ID_assign` for a
37-
/// \ref code_frontend_assignt.
38-
explicit codet(const irep_idt &statement) : exprt(ID_code, empty_typet())
39-
{
40-
set_statement(statement);
41-
}
42-
43-
codet(const irep_idt &statement, source_locationt loc)
44-
: exprt(ID_code, empty_typet(), std::move(loc))
45-
{
46-
set_statement(statement);
47-
}
48-
49-
/// \param statement: Specifies the type of the `codet` to be constructed,
50-
/// e.g. `ID_block` for a \ref code_blockt or `ID_assign` for a
51-
/// \ref code_frontend_assignt.
52-
/// \param _op: any operands to be added
53-
explicit codet(const irep_idt &statement, operandst _op) : codet(statement)
54-
{
55-
operands() = std::move(_op);
56-
}
57-
58-
codet(const irep_idt &statement, operandst op, source_locationt loc)
59-
: codet(statement, std::move(loc))
60-
{
61-
operands() = std::move(op);
62-
}
63-
64-
void set_statement(const irep_idt &statement)
65-
{
66-
set(ID_statement, statement);
67-
}
68-
69-
const irep_idt &get_statement() const
70-
{
71-
return get(ID_statement);
72-
}
73-
74-
codet &first_statement();
75-
const codet &first_statement() const;
76-
codet &last_statement();
77-
const codet &last_statement() const;
78-
79-
/// Check that the code statement is well-formed (shallow checks only, i.e.,
80-
/// enclosed statements, subexpressions, etc. are not checked)
81-
///
82-
/// Subclasses may override this function to provide specific well-formedness
83-
/// checks for the corresponding types.
84-
///
85-
/// The validation mode indicates whether well-formedness check failures are
86-
/// reported via DATA_INVARIANT violations or exceptions.
87-
static void check(const codet &, const validation_modet)
88-
{
89-
}
90-
91-
/// Check that the code statement is well-formed, assuming that all its
92-
/// enclosed statements, subexpressions, etc. have all ready been checked for
93-
/// well-formedness.
94-
///
95-
/// Subclasses may override this function to provide specific well-formedness
96-
/// checks for the corresponding types.
97-
///
98-
/// The validation mode indicates whether well-formedness check failures are
99-
/// reported via DATA_INVARIANT violations or exceptions.
100-
static void validate(
101-
const codet &code,
102-
const namespacet &,
103-
const validation_modet vm = validation_modet::INVARIANT)
104-
{
105-
check_code(code, vm);
106-
}
107-
108-
/// Check that the code statement is well-formed (full check, including checks
109-
/// of all subexpressions)
110-
///
111-
/// Subclasses may override this function to provide specific well-formedness
112-
/// checks for the corresponding types.
113-
///
114-
/// The validation mode indicates whether well-formedness check failures are
115-
/// reported via DATA_INVARIANT violations or exceptions.
116-
static void validate_full(
117-
const codet &code,
118-
const namespacet &,
119-
const validation_modet vm = validation_modet::INVARIANT)
120-
{
121-
check_code(code, vm);
122-
}
123-
124-
using exprt::op0;
125-
using exprt::op1;
126-
using exprt::op2;
127-
using exprt::op3;
128-
};
129-
130-
namespace detail // NOLINT
131-
{
132-
133-
template<typename Tag>
134-
inline bool can_cast_code_impl(const exprt &expr, const Tag &tag)
135-
{
136-
if(const auto ptr = expr_try_dynamic_cast<codet>(expr))
137-
{
138-
return ptr->get_statement() == tag;
139-
}
140-
return false;
141-
}
142-
143-
} // namespace detail
144-
145-
template<> inline bool can_cast_expr<codet>(const exprt &base)
146-
{
147-
return base.id()==ID_code;
148-
}
149-
150-
// to_code has no validation other than checking the id(), so no validate_expr
151-
// is provided for codet
152-
153-
inline const codet &to_code(const exprt &expr)
154-
{
155-
PRECONDITION(expr.id() == ID_code);
156-
return static_cast<const codet &>(expr);
157-
}
158-
159-
inline codet &to_code(exprt &expr)
160-
{
161-
PRECONDITION(expr.id() == ID_code);
162-
return static_cast<codet &>(expr);
163-
}
16417

16518
/// A \ref codet representing an assignment in the program.
16619
/// For example, if an expression `e1` is represented as an \ref exprt `expr1`

src/util/std_code_base.h

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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+
#include "validate_code.h"
17+
18+
/// Data structure for representing an arbitrary statement in a program. Every
19+
/// specific type of statement (e.g. block of statements, assignment,
20+
/// if-then-else statement...) is represented by a subtype of `codet`.
21+
/// `codet`s are represented to be subtypes of \ref exprt since statements can
22+
/// occur in an expression context in C: for example, the assignment `x = y;`
23+
/// is an expression with return value `y`. For other types of statements in an
24+
/// expression context, see e.g.
25+
/// https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html.
26+
/// To distinguish a `codet` from other [exprts](\ref exprt), we set its
27+
/// [id()](\ref irept::id) to `ID_code`. To distinguish different types of
28+
/// `codet`, we use a named sub `ID_statement`.
29+
class codet : public exprt
30+
{
31+
public:
32+
/// \param statement: Specifies the type of the `codet` to be constructed,
33+
/// e.g. `ID_block` for a \ref code_blockt or `ID_assign` for a
34+
/// \ref code_frontend_assignt.
35+
explicit codet(const irep_idt &statement) : exprt(ID_code, empty_typet())
36+
{
37+
set_statement(statement);
38+
}
39+
40+
codet(const irep_idt &statement, source_locationt loc)
41+
: exprt(ID_code, empty_typet(), std::move(loc))
42+
{
43+
set_statement(statement);
44+
}
45+
46+
/// \param statement: Specifies the type of the `codet` to be constructed,
47+
/// e.g. `ID_block` for a \ref code_blockt or `ID_assign` for a
48+
/// \ref code_frontend_assignt.
49+
/// \param _op: any operands to be added
50+
explicit codet(const irep_idt &statement, operandst _op) : codet(statement)
51+
{
52+
operands() = std::move(_op);
53+
}
54+
55+
codet(const irep_idt &statement, operandst op, source_locationt loc)
56+
: codet(statement, std::move(loc))
57+
{
58+
operands() = std::move(op);
59+
}
60+
61+
void set_statement(const irep_idt &statement)
62+
{
63+
set(ID_statement, statement);
64+
}
65+
66+
const irep_idt &get_statement() const
67+
{
68+
return get(ID_statement);
69+
}
70+
71+
codet &first_statement();
72+
const codet &first_statement() const;
73+
codet &last_statement();
74+
const codet &last_statement() const;
75+
76+
/// Check that the code statement is well-formed (shallow checks only, i.e.,
77+
/// enclosed statements, subexpressions, etc. are not checked)
78+
///
79+
/// Subclasses may override this function to provide specific well-formedness
80+
/// checks for the corresponding types.
81+
///
82+
/// The validation mode indicates whether well-formedness check failures are
83+
/// reported via DATA_INVARIANT violations or exceptions.
84+
static void check(const codet &, const validation_modet)
85+
{
86+
}
87+
88+
/// Check that the code statement is well-formed, assuming that all its
89+
/// enclosed statements, subexpressions, etc. have all ready been checked for
90+
/// well-formedness.
91+
///
92+
/// Subclasses may override this function to provide specific well-formedness
93+
/// checks for the corresponding types.
94+
///
95+
/// The validation mode indicates whether well-formedness check failures are
96+
/// reported via DATA_INVARIANT violations or exceptions.
97+
static void validate(
98+
const codet &code,
99+
const namespacet &,
100+
const validation_modet vm = validation_modet::INVARIANT)
101+
{
102+
check_code(code, vm);
103+
}
104+
105+
/// Check that the code statement is well-formed (full check, including checks
106+
/// of all subexpressions)
107+
///
108+
/// Subclasses may override this function to provide specific well-formedness
109+
/// checks for the corresponding types.
110+
///
111+
/// The validation mode indicates whether well-formedness check failures are
112+
/// reported via DATA_INVARIANT violations or exceptions.
113+
static void validate_full(
114+
const codet &code,
115+
const namespacet &,
116+
const validation_modet vm = validation_modet::INVARIANT)
117+
{
118+
check_code(code, vm);
119+
}
120+
121+
using exprt::op0;
122+
using exprt::op1;
123+
using exprt::op2;
124+
using exprt::op3;
125+
};
126+
127+
namespace detail // NOLINT
128+
{
129+
template <typename Tag>
130+
inline bool can_cast_code_impl(const exprt &expr, const Tag &tag)
131+
{
132+
if(const auto ptr = expr_try_dynamic_cast<codet>(expr))
133+
{
134+
return ptr->get_statement() == tag;
135+
}
136+
return false;
137+
}
138+
139+
} // namespace detail
140+
141+
template <>
142+
inline bool can_cast_expr<codet>(const exprt &base)
143+
{
144+
return base.id() == ID_code;
145+
}
146+
147+
// to_code has no validation other than checking the id(), so no validate_expr
148+
// is provided for codet
149+
150+
inline const codet &to_code(const exprt &expr)
151+
{
152+
PRECONDITION(expr.id() == ID_code);
153+
return static_cast<const codet &>(expr);
154+
}
155+
156+
inline codet &to_code(exprt &expr)
157+
{
158+
PRECONDITION(expr.id() == ID_code);
159+
return static_cast<codet &>(expr);
160+
}
161+
162+
#endif // CPROVER_UTIL_STD_CODE_BASE_H

0 commit comments

Comments
 (0)