-
Notifications
You must be signed in to change notification settings - Fork 300
Expand file tree
/
Copy pathcasting_replace_symbol.cpp
More file actions
107 lines (81 loc) · 2.7 KB
/
Copy pathcasting_replace_symbol.cpp
File metadata and controls
107 lines (81 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*******************************************************************\
Module: ANSI-C Linking
Author: Michael Tautschnig
\*******************************************************************/
/// \file
/// ANSI-C Linking
#include "casting_replace_symbol.h"
#include <util/pointer_expr.h>
#include <util/std_code.h>
#include <util/std_expr.h>
bool casting_replace_symbolt::replace(exprt &dest) const
{
bool result = true; // unchanged
// first look at type
const exprt &const_dest(dest);
if(have_to_replace(const_dest.type()))
if(!replace_symbolt::replace(dest.type()))
result = false;
// now do expression itself
if(!have_to_replace(dest))
return result;
if(dest.id() == ID_side_effect)
{
if(auto call = expr_try_dynamic_cast<side_effect_expr_function_callt>(dest))
{
if(!have_to_replace(call->function()))
return replace_symbolt::replace(dest);
exprt before = dest;
code_typet type = to_code_type(call->function().type());
result &= replace_symbolt::replace(call->function());
// maybe add type casts here?
for(auto &arg : call->arguments())
result &= replace_symbolt::replace(arg);
if(
type.return_type() !=
to_code_type(call->function().type()).return_type())
{
call->type() = to_code_type(call->function().type()).return_type();
dest = typecast_exprt(*call, type.return_type());
result = true;
}
return result;
}
}
else if(dest.id() == ID_address_of)
{
pointer_typet ptr_type = to_pointer_type(dest.type());
result &= replace_symbolt::replace(dest);
address_of_exprt address_of = to_address_of_expr(dest);
if(address_of.object().type() != ptr_type.base_type())
{
to_pointer_type(address_of.type()).base_type() =
address_of.object().type();
dest = typecast_exprt{address_of, std::move(ptr_type)};
result = true;
}
return result;
}
return replace_symbolt::replace(dest);
}
bool casting_replace_symbolt::replace_symbol_expr(symbol_exprt &s) const
{
expr_mapt::const_iterator it = expr_map.find(s.identifier());
if(it == expr_map.end())
return true;
const exprt &e = it->second;
source_locationt previous_source_location{s.source_location()};
DATA_INVARIANT_WITH_DIAGNOSTICS(
previous_source_location.is_not_nil(),
"front-ends should construct symbol expressions with source locations",
s.pretty());
if(e.type().id() != ID_array && e.type().id() != ID_code)
{
typet type = s.type();
static_cast<exprt &>(s) = typecast_exprt::conditional_cast(e, type);
}
else
static_cast<exprt &>(s) = e;
s.add_source_location() = std::move(previous_source_location);
return false;
}