cprover
goto_convert_function_call.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Program Transformation
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
11 
12 #include "goto_convert_class.h"
13 
14 
15 #include <util/cprover_prefix.h>
16 #include <util/expr_util.h>
17 #include <util/prefix.h>
18 #include <util/replace_expr.h>
19 #include <util/source_location.h>
20 #include <util/std_expr.h>
21 
22 #include <util/c_types.h>
23 
25  const code_function_callt &function_call,
26  goto_programt &dest,
27  const irep_idt &mode)
28 {
30  function_call.lhs(),
31  function_call.function(),
32  function_call.arguments(),
33  dest,
34  mode);
35 }
36 
38  const exprt &lhs,
39  const exprt &function,
40  const exprt::operandst &arguments,
41  goto_programt &dest,
42  const irep_idt &mode)
43 {
44  // make it all side effect free
45 
46  exprt new_lhs=lhs,
47  new_function=function;
48 
49  exprt::operandst new_arguments=arguments;
50 
51  if(!new_lhs.is_nil())
52  clean_expr(new_lhs, dest, mode);
53 
54  clean_expr(new_function, dest, mode);
55 
56  // the arguments of __noop do not get evaluated
57  if(new_function.id()==ID_symbol &&
58  to_symbol_expr(new_function).get_identifier()=="__noop")
59  {
60  new_arguments.clear();
61  }
62 
63  Forall_expr(it, new_arguments)
64  clean_expr(*it, dest, mode);
65 
66  // split on the function
67 
68  if(new_function.id()==ID_if)
69  {
71  new_lhs, to_if_expr(new_function), new_arguments, dest, mode);
72  }
73  else if(new_function.id()==ID_symbol)
74  {
76  new_lhs, to_symbol_expr(new_function), new_arguments, dest);
77  }
78  else if(new_function.id() == ID_null_object)
79  {
80  }
81  else if(new_function.id()==ID_dereference ||
82  new_function.id()=="virtual_function")
83  {
84  do_function_call_other(new_lhs, new_function, new_arguments, dest);
85  }
86  else
87  {
89  false,
90  "unexpected function argument",
91  new_function.id(),
92  function.find_source_location());
93  }
94 }
95 
97  const exprt &lhs,
98  const if_exprt &function,
99  const exprt::operandst &arguments,
100  goto_programt &dest,
101  const irep_idt &mode)
102 {
103  // case split
104 
105  // c?f():g()
106  //--------------------
107  // v: if(!c) goto y;
108  // w: f();
109  // x: goto z;
110  // y: g();
111  // z: ;
112 
113  // do the v label
114  goto_programt tmp_v;
116  boolean_negate(function.cond()), function.cond().source_location()));
117 
118  // do the x label
119  goto_programt tmp_x;
122 
123  // do the z label
124  goto_programt tmp_z;
126 
127  // y: g();
128  goto_programt tmp_y;
130 
131  do_function_call(lhs, function.false_case(), arguments, tmp_y, mode);
132 
133  if(tmp_y.instructions.empty())
134  y = tmp_y.add(goto_programt::make_skip());
135  else
136  y=tmp_y.instructions.begin();
137 
138  // v: if(!c) goto y;
139  v->complete_goto(y);
140 
141  // w: f();
142  goto_programt tmp_w;
143 
144  do_function_call(lhs, function.true_case(), arguments, tmp_w, mode);
145 
146  if(tmp_w.instructions.empty())
147  tmp_w.add(goto_programt::make_skip());
148 
149  // x: goto z;
150  x->complete_goto(z);
151 
152  dest.destructive_append(tmp_v);
153  dest.destructive_append(tmp_w);
154  dest.destructive_append(tmp_x);
155  dest.destructive_append(tmp_y);
156  dest.destructive_append(tmp_z);
157 }
158 
160  const exprt &lhs,
161  const exprt &function,
162  const exprt::operandst &arguments,
163  goto_programt &dest)
164 {
165  // don't know what to do with it
166  code_function_callt function_call(lhs, function, arguments);
167  function_call.add_source_location()=function.source_location();
169  function_call, function.source_location()));
170 }
goto_convertt::convert_function_call
void convert_function_call(const code_function_callt &code, goto_programt &dest, const irep_idt &mode)
Definition: goto_convert_function_call.cpp:24
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
Forall_expr
#define Forall_expr(it, expr)
Definition: expr.h:33
goto_convertt::clean_expr
void clean_expr(exprt &expr, goto_programt &dest, const irep_idt &mode, bool result_is_used=true)
Definition: goto_clean_expr.cpp:161
to_if_expr
const if_exprt & to_if_expr(const exprt &expr)
Cast an exprt to an if_exprt.
Definition: std_expr.h:3029
if_exprt
The trinary if-then-else operator.
Definition: std_expr.h:2964
prefix.h
goto_convert_class.h
Program Transformation.
goto_programt::add
targett add(instructiont &&instruction)
Adds a given instruction at the end.
Definition: goto_program.h:686
goto_convertt::do_function_call
virtual void do_function_call(const exprt &lhs, const exprt &function, const exprt::operandst &arguments, goto_programt &dest, const irep_idt &mode)
Definition: goto_convert_function_call.cpp:37
exprt
Base class for all expressions.
Definition: expr.h:53
code_function_callt::lhs
exprt & lhs()
Definition: std_code.h:1208
INVARIANT_WITH_DIAGNOSTICS
#define INVARIANT_WITH_DIAGNOSTICS(CONDITION, REASON,...)
Same as invariant, with one or more diagnostics attached Diagnostics can be of any type that has a sp...
Definition: invariant.h:438
goto_programt::make_function_call
static instructiont make_function_call(const code_function_callt &_code, const source_locationt &l=source_locationt::nil())
Create a function call instruction.
Definition: goto_program.h:1049
code_function_callt
codet representation of a function call statement.
Definition: std_code.h:1183
goto_programt::make_skip
static instructiont make_skip(const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:851
goto_convertt::do_function_call_other
virtual void do_function_call_other(const exprt &lhs, const exprt &function, const exprt::operandst &arguments, goto_programt &dest)
Definition: goto_convert_function_call.cpp:159
boolean_negate
exprt boolean_negate(const exprt &src)
negate a Boolean expression, possibly removing a not_exprt, and swapping false and true
Definition: expr_util.cpp:127
goto_convertt::do_function_call_symbol
virtual void do_function_call_symbol(const exprt &lhs, const symbol_exprt &function, const exprt::operandst &arguments, goto_programt &dest)
add function calls to function queue for later processing
Definition: builtin_functions.cpp:615
source_location.h
to_symbol_expr
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:177
irept::is_nil
bool is_nil() const
Definition: irep.h:398
exprt::operandst
std::vector< exprt > operandst
Definition: expr.h:55
cprover_prefix.h
code_function_callt::arguments
argumentst & arguments()
Definition: std_code.h:1228
goto_programt::destructive_append
void destructive_append(goto_programt &p)
Appends the given program p to *this. p is destroyed.
Definition: goto_program.h:669
goto_convertt::do_function_call_if
virtual void do_function_call_if(const exprt &lhs, const if_exprt &function, const exprt::operandst &arguments, goto_programt &dest, const irep_idt &mode)
Definition: goto_convert_function_call.cpp:96
goto_programt::instructions
instructionst instructions
The list of instructions in the goto program.
Definition: goto_program.h:585
expr_util.h
Deprecated expression utility functions.
goto_programt
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:73
replace_expr.h
exprt::add_source_location
source_locationt & add_source_location()
Definition: expr.h:259
true_exprt
The Boolean constant true.
Definition: std_expr.h:3955
std_expr.h
API to expression classes.
c_types.h
goto_programt::targett
instructionst::iterator targett
Definition: goto_program.h:579
code_function_callt::function
exprt & function()
Definition: std_code.h:1218
goto_programt::make_incomplete_goto
static instructiont make_incomplete_goto(const exprt &_cond, const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:967