cprover
parameter_assignments.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Add parameter assignments
4 
5 Author: Daniel Kroening
6 
7 Date: September 2015
8 
9 \*******************************************************************/
10 
13 
14 #include "parameter_assignments.h"
15 
16 #include <util/std_expr.h>
17 #include <util/symbol_table.h>
18 
20 {
21 public:
22  explicit parameter_assignmentst(symbol_tablet &_symbol_table):
23  symbol_table(_symbol_table)
24  {
25  }
26 
27  void operator()(
28  goto_functionst &goto_functions);
29 
30 protected:
32 
33  void do_function_calls(
34  goto_programt &goto_program);
35 };
36 
39  goto_programt &goto_program)
40 {
41  Forall_goto_program_instructions(i_it, goto_program)
42  {
43  if(i_it->is_function_call())
44  {
45  code_function_callt &function_call=to_code_function_call(i_it->code);
46 
47  // add x=y for f(y) where x is the parameter
48 
49  PRECONDITION(function_call.function().id() == ID_symbol);
50 
51  const irep_idt &identifier=
52  to_symbol_expr(function_call.function()).get_identifier();
53 
54  // see if we have it
55  const namespacet ns(symbol_table);
56  const symbolt &function_symbol=ns.lookup(identifier);
57  const code_typet &code_type=to_code_type(function_symbol.type);
58 
59  goto_programt tmp;
60 
61  for(std::size_t nr=0; nr<code_type.parameters().size(); nr++)
62  {
63  irep_idt p_identifier=code_type.parameters()[nr].get_identifier();
64 
65  if(p_identifier.empty())
66  continue;
67 
68  if(nr<function_call.arguments().size())
69  {
71  t->make_assignment();
72  t->source_location=i_it->source_location;
73  const symbolt &lhs_symbol=ns.lookup(p_identifier);
74  symbol_exprt lhs=lhs_symbol.symbol_expr();
75  exprt rhs=function_call.arguments()[nr];
76  if(rhs.type()!=lhs.type())
77  rhs.make_typecast(lhs.type());
78  t->code=code_assignt(lhs, rhs);
79  t->function=i_it->function;
80  }
81  }
82 
83  std::size_t count=tmp.instructions.size();
84  goto_program.insert_before_swap(i_it, tmp);
85 
86  for(; count!=0; count--) i_it++;
87  }
88  }
89 }
90 
92 {
93  Forall_goto_functions(it, goto_functions)
94  do_function_calls(it->second.body);
95 }
96 
99  symbol_tablet &symbol_table,
100  goto_functionst &goto_functions)
101 {
102  parameter_assignmentst rr(symbol_table);
103  rr(goto_functions);
104 }
105 
108 {
109  parameter_assignmentst rr(goto_model.symbol_table);
110  rr(goto_model.goto_functions);
111 }
Base type of functions.
Definition: std_types.h:751
void insert_before_swap(targett target)
Insertion that preserves jumps to "target".
Definition: goto_program.h:477
void parameter_assignments(symbol_tablet &symbol_table, goto_functionst &goto_functions)
removes returns
void operator()(goto_functionst &goto_functions)
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition: std_types.h:982
typet & type()
Return the type of the expression.
Definition: expr.h:68
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:29
Symbol table entry.
Definition: symbol.h:27
const irep_idt & id() const
Definition: irep.h:259
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
Definition: symbol.cpp:121
argumentst & arguments()
Definition: std_code.h:1109
instructionst::iterator targett
Definition: goto_program.h:414
void do_function_calls(goto_programt &goto_program)
turns x=f(...) into f(...); lhs=f::return_value;
instructionst instructions
The list of instructions in the goto program.
Definition: goto_program.h:420
API to expression classes.
The symbol table.
Definition: symbol_table.h:19
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:93
#define PRECONDITION(CONDITION)
Definition: invariant.h:438
codet representation of a function call statement.
Definition: std_code.h:1036
A collection of goto functions.
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition: std_expr.h:251
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:35
Author: Diffblue Ltd.
A generic container class for the GOTO intermediate representation of one function.
Definition: goto_program.h:72
typet type
Type of symbol.
Definition: symbol.h:31
exprt & function()
Definition: std_code.h:1099
targett add_instruction()
Adds an instruction at the end.
Definition: goto_program.h:541
Base class for all expressions.
Definition: expr.h:54
const parameterst & parameters() const
Definition: std_types.h:893
#define Forall_goto_functions(it, functions)
#define Forall_goto_program_instructions(it, program)
Definition: goto_program.h:809
Expression to hold a symbol (variable)
Definition: std_expr.h:143
parameter_assignmentst(symbol_tablet &_symbol_table)
Add parameter assignments.
bool empty() const
Definition: dstring.h:75
void make_typecast(const typet &_type)
Create a typecast_exprt to the given type.
Definition: expr.cpp:74
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:32
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:166
A codet representing an assignment in the program.
Definition: std_code.h:256
const code_function_callt & to_code_function_call(const codet &code)
Definition: std_code.h:1173