CBMC
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 #include <util/expr_util.h>
15 #include <util/source_location.h>
16 #include <util/std_expr.h>
17 
19  const code_function_callt &function_call,
20  goto_programt &dest,
21  const irep_idt &mode)
22 {
24  function_call.lhs(),
25  function_call.function(),
26  function_call.arguments(),
27  dest,
28  mode);
29 }
30 
32  const exprt &lhs,
33  const exprt &function,
34  const exprt::operandst &arguments,
35  goto_programt &dest,
36  const irep_idt &mode)
37 {
38  // make it all side effect free
39 
40  exprt new_lhs=lhs,
41  new_function=function;
42 
43  exprt::operandst new_arguments=arguments;
44 
45  if(!new_lhs.is_nil())
46  clean_expr(new_lhs, dest, mode);
47 
48  clean_expr(new_function, dest, mode);
49 
50  for(auto &new_argument : new_arguments)
51  clean_expr(new_argument, dest, mode);
52 
53  // split on the function
54 
55  if(new_function.id()==ID_if)
56  {
58  new_lhs, to_if_expr(new_function), new_arguments, dest, mode);
59  }
60  else if(new_function.id()==ID_symbol)
61  {
63  new_lhs, to_symbol_expr(new_function), new_arguments, dest, mode);
64  }
65  else if(new_function.id() == ID_null_object)
66  {
67  }
68  else if(new_function.id()==ID_dereference ||
69  new_function.id()=="virtual_function")
70  {
71  do_function_call_other(new_lhs, new_function, new_arguments, dest);
72  }
73  else
74  {
76  false,
77  "unexpected function argument",
78  new_function.id(),
79  function.find_source_location());
80  }
81 }
82 
84  const exprt &lhs,
85  const if_exprt &function,
86  const exprt::operandst &arguments,
87  goto_programt &dest,
88  const irep_idt &mode)
89 {
90  // case split
91 
92  // c?f():g()
93  //--------------------
94  // v: if(!c) goto y;
95  // w: f();
96  // x: goto z;
97  // y: g();
98  // z: ;
99 
100  // do the v label
101  goto_programt tmp_v;
103  boolean_negate(function.cond()), function.cond().source_location()));
104 
105  // do the x label
106  goto_programt tmp_x;
109 
110  // do the z label
111  goto_programt tmp_z;
113 
114  // y: g();
115  goto_programt tmp_y;
117 
118  do_function_call(lhs, function.false_case(), arguments, tmp_y, mode);
119 
120  if(tmp_y.instructions.empty())
121  y = tmp_y.add(goto_programt::make_skip());
122  else
123  y=tmp_y.instructions.begin();
124 
125  // v: if(!c) goto y;
126  v->complete_goto(y);
127 
128  // w: f();
129  goto_programt tmp_w;
130 
131  do_function_call(lhs, function.true_case(), arguments, tmp_w, mode);
132 
133  if(tmp_w.instructions.empty())
134  tmp_w.add(goto_programt::make_skip());
135 
136  // x: goto z;
137  x->complete_goto(z);
138 
139  dest.destructive_append(tmp_v);
140  dest.destructive_append(tmp_w);
141  dest.destructive_append(tmp_x);
142  dest.destructive_append(tmp_y);
143  dest.destructive_append(tmp_z);
144 }
145 
147  const exprt &lhs,
148  const exprt &function,
149  const exprt::operandst &arguments,
150  goto_programt &dest)
151 {
152  // don't know what to do with it
153  code_function_callt function_call(lhs, function, arguments);
154  function_call.add_source_location()=function.source_location();
156  function_call, function.source_location()));
157 }
goto_instruction_codet representation of a function call statement.
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
Base class for all expressions.
Definition: expr.h:56
std::vector< exprt > operandst
Definition: expr.h:58
source_locationt & add_source_location()
Definition: expr.h:236
virtual void do_function_call_if(const exprt &lhs, const if_exprt &function, const exprt::operandst &arguments, goto_programt &dest, const irep_idt &mode)
virtual void do_function_call(const exprt &lhs, const exprt &function, const exprt::operandst &arguments, goto_programt &dest, const irep_idt &mode)
void convert_function_call(const code_function_callt &code, goto_programt &dest, const irep_idt &mode)
virtual void do_function_call_symbol(const exprt &lhs, const symbol_exprt &function, const exprt::operandst &arguments, goto_programt &dest, const irep_idt &mode)
add function calls to function queue for later processing
void clean_expr(exprt &expr, goto_programt &dest, const irep_idt &mode, bool result_is_used=true)
virtual void do_function_call_other(const exprt &lhs, const exprt &function, const exprt::operandst &arguments, goto_programt &dest)
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:73
instructionst instructions
The list of instructions in the goto program.
Definition: goto_program.h:622
instructionst::iterator targett
Definition: goto_program.h:614
void destructive_append(goto_programt &p)
Appends the given program p to *this. p is destroyed.
Definition: goto_program.h:722
static instructiont make_skip(const source_locationt &l=source_locationt::nil())
Definition: goto_program.h:891
static instructiont make_function_call(const code_function_callt &_code, const source_locationt &l=source_locationt::nil())
Create a function call instruction.
targett add(instructiont &&instruction)
Adds a given instruction at the end.
Definition: goto_program.h:739
static instructiont make_incomplete_goto(const exprt &_cond, const source_locationt &l=source_locationt::nil())
The trinary if-then-else operator.
Definition: std_expr.h:2370
bool is_nil() const
Definition: irep.h:364
The Boolean constant true.
Definition: std_expr.h:3055
exprt boolean_negate(const exprt &src)
negate a Boolean expression, possibly removing a not_exprt, and swapping false and true
Definition: expr_util.cpp:129
Deprecated expression utility functions.
Program Transformation.
#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:437
API to expression classes.
const if_exprt & to_if_expr(const exprt &expr)
Cast an exprt to an if_exprt.
Definition: std_expr.h:2450
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:272