cprover
Toggle main menu visibility
Loading...
Searching...
No Matches
pointer_arithmetic.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 "
pointer_arithmetic.h
"
10
11
#include <
util/arith_tools.h
>
12
#include <
util/pointer_expr.h
>
13
#include <
util/std_expr.h
>
14
15
pointer_arithmetict::pointer_arithmetict
(
const
exprt
&src)
16
{
17
pointer
.make_nil();
18
offset
.make_nil();
19
read
(src);
20
}
21
22
void
pointer_arithmetict::read
(
const
exprt
&src)
23
{
24
if
(src.
id
()==ID_plus)
25
{
26
for
(
const
auto
&op : src.
operands
())
27
{
28
if
(op.type().id() == ID_pointer)
29
read
(op);
30
else
31
add_to_offset
(op);
32
}
33
}
34
else
if
(src.
id
()==ID_minus)
35
{
36
auto
const
&minus_src =
to_minus_expr
(src);
37
read
(minus_src.op0());
38
const
unary_minus_exprt
unary_minus(
39
minus_src.op1(), minus_src.op1().type());
40
add_to_offset
(unary_minus);
41
}
42
else
if
(src.
id
()==ID_address_of)
43
{
44
auto
const
&address_of_src =
to_address_of_expr
(src);
45
if
(address_of_src.op().id() == ID_index)
46
{
47
const
index_exprt
&index_expr =
to_index_expr
(address_of_src.op());
48
49
if
(index_expr.
index
() == 0)
50
make_pointer
(address_of_src);
51
else
52
{
53
add_to_offset
(index_expr.
index
());
54
// produce &x[0] + i instead of &x[i]
55
auto
new_address_of_src = address_of_src;
56
to_index_expr
(new_address_of_src.op()).
index
() =
57
from_integer
(0, index_expr.
index
().
type
());
58
make_pointer
(new_address_of_src);
59
}
60
}
61
else
62
make_pointer
(address_of_src);
63
}
64
else
65
make_pointer
(src);
66
}
67
68
void
pointer_arithmetict::add_to_offset
(
const
exprt
&src)
69
{
70
if
(
offset
.is_nil())
71
offset
=src;
72
else
if
(
offset
.id()==ID_plus)
73
offset
.copy_to_operands(src);
74
else
75
{
76
offset
=
77
plus_exprt
(
offset
,
typecast_exprt::conditional_cast
(src,
offset
.type()));
78
}
79
}
80
81
void
pointer_arithmetict::make_pointer
(
const
exprt
&src)
82
{
83
if
(
pointer
.is_nil())
84
pointer
=src;
85
else
86
add_to_offset
(src);
87
}
from_integer
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
Definition
arith_tools.cpp:99
arith_tools.h
exprt
Base class for all expressions.
Definition
expr.h:57
exprt::type
typet & type()
Return the type of the expression.
Definition
expr.h:85
exprt::operands
operandst & operands()
Definition
expr.h:95
index_exprt
Array index operator.
Definition
std_expr.h:1421
index_exprt::index
exprt & index()
Definition
std_expr.h:1461
irept::id
const irep_idt & id() const
Definition
irep.h:388
plus_exprt
The plus expression Associativity is not specified.
Definition
std_expr.h:996
typecast_exprt::conditional_cast
static exprt conditional_cast(const exprt &expr, const typet &type)
Definition
std_expr.h:1993
unary_minus_exprt
The unary minus expression.
Definition
std_expr.h:467
pointer_arithmetic.h
pointer_expr.h
API to expression classes for Pointers.
to_address_of_expr
const address_of_exprt & to_address_of_expr(const exprt &expr)
Cast an exprt to an address_of_exprt.
Definition
pointer_expr.h:577
std_expr.h
API to expression classes.
to_index_expr
const index_exprt & to_index_expr(const exprt &expr)
Cast an exprt to an index_exprt.
Definition
std_expr.h:1484
to_minus_expr
const minus_exprt & to_minus_expr(const exprt &expr)
Cast an exprt to a minus_exprt.
Definition
std_expr.h:1075
pointer_arithmetict::pointer
exprt pointer
Definition
pointer_arithmetic.h:17
pointer_arithmetict::make_pointer
void make_pointer(const exprt &src)
Definition
pointer_arithmetic.cpp:81
pointer_arithmetict::add_to_offset
void add_to_offset(const exprt &src)
Definition
pointer_arithmetic.cpp:68
pointer_arithmetict::read
void read(const exprt &src)
Definition
pointer_arithmetic.cpp:22
pointer_arithmetict::offset
exprt offset
Definition
pointer_arithmetic.h:17
pointer_arithmetict::pointer_arithmetict
pointer_arithmetict(const exprt &src)
Definition
pointer_arithmetic.cpp:15
goto-programs
pointer_arithmetic.cpp
Generated by
1.17.0