CBMC
goto_functions.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Goto Programs with Functions
4 
5 Author: Daniel Kroening
6 
7 Date: June 2003
8 
9 \*******************************************************************/
10 
13 
14 #include "goto_functions.h"
15 
16 #include <util/namespace.h>
17 #include <util/symbol.h>
18 
19 #include <algorithm>
20 
22 {
24  for(auto &func : function_map)
25  {
26  // Side-effect: bumps unused_location_number.
27  func.second.body.compute_location_numbers(unused_location_number);
28  }
29 }
30 
32  goto_programt &program)
33 {
34  // Renumber just this single function. Use fresh numbers in case it has
35  // grown since it was last numbered.
37 }
38 
40 {
41  for(auto &func : function_map)
42  {
43  func.second.body.compute_incoming_edges();
44  }
45 }
46 
48 {
49  for(auto &func : function_map)
50  {
51  func.second.body.compute_target_numbers();
52  }
53 }
54 
56 {
57  for(auto &func : function_map)
58  {
59  func.second.body.compute_loop_numbers();
60  }
61 }
62 
64 std::vector<goto_functionst::function_mapt::const_iterator>
66 {
67  std::vector<function_mapt::const_iterator> result;
68 
69  result.reserve(function_map.size());
70 
71  for(auto it = function_map.begin(); it != function_map.end(); it++)
72  result.push_back(it);
73 
74  std::sort(
75  result.begin(),
76  result.end(),
77  [](function_mapt::const_iterator a, function_mapt::const_iterator b) {
78  return id2string(a->first) < id2string(b->first);
79  });
80 
81  return result;
82 }
83 
85 std::vector<goto_functionst::function_mapt::iterator> goto_functionst::sorted()
86 {
87  std::vector<function_mapt::iterator> result;
88 
89  result.reserve(function_map.size());
90 
91  for(auto it = function_map.begin(); it != function_map.end(); it++)
92  result.push_back(it);
93 
94  std::sort(
95  result.begin(),
96  result.end(),
97  [](function_mapt::iterator a, function_mapt::iterator b) {
98  return id2string(a->first) < id2string(b->first);
99  });
100 
101  return result;
102 }
103 
105  const
106 {
107  for(const auto &entry : function_map)
108  {
109  const goto_functiont &goto_function = entry.second;
110  const auto &function_name = entry.first;
111  const symbolt &function_symbol = ns.lookup(function_name);
112  const code_typet::parameterst &parameters =
113  to_code_type(function_symbol.type).parameters();
114 
115  DATA_CHECK(
116  vm,
117  goto_function.parameter_identifiers.size() == parameters.size(),
118  id2string(function_name) + " parameter count inconsistency\n" +
119  "goto program: " +
120  std::to_string(goto_function.parameter_identifiers.size()) +
121  "\nsymbol table: " + std::to_string(parameters.size()));
122 
123  auto it = goto_function.parameter_identifiers.begin();
124  for(const auto &parameter : parameters)
125  {
126  DATA_CHECK(
127  vm,
128  it->empty() || ns.lookup(*it).type == parameter.type(),
129  id2string(function_name) + " parameter type inconsistency\n" +
130  "goto program: " + ns.lookup(*it).type.id_string() +
131  "\nsymbol table: " + parameter.type().id_string());
132  ++it;
133  }
134 
135  goto_function.validate(ns, vm);
136 
137  // Check that a void function does not contain any RETURN instructions
138  if(to_code_type(function_symbol.type).return_type().id() == ID_empty)
139  {
140  for(const auto &instruction : goto_function.body.instructions)
141  {
142  DATA_CHECK(
143  vm,
144  !instruction.is_set_return_value(),
145  "void function should not return a value");
146  }
147  }
148  }
149 }
std::vector< parametert > parameterst
Definition: std_types.h:585
const typet & return_type() const
Definition: std_types.h:689
const parameterst & parameters() const
Definition: std_types.h:699
void compute_incoming_edges()
void compute_loop_numbers()
unsigned unused_location_number
A location number such that numbers in the interval [unused_location_number, MAX_UINT] are all unused...
function_mapt function_map
void compute_location_numbers()
void validate(const namespacet &, validation_modet) const
Check that the goto functions are well-formed.
std::vector< function_mapt::const_iterator > sorted() const
returns a vector of the iterators in alphabetical order
void compute_target_numbers()
A goto function, consisting of function body (see body) and parameter identifiers (see parameter_iden...
Definition: goto_function.h:24
goto_programt body
Definition: goto_function.h:26
parameter_identifierst parameter_identifiers
The identifiers of the parameters of this function.
Definition: goto_function.h:33
void validate(const namespacet &ns, const validation_modet vm) const
Check that the goto function is well-formed.
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
void compute_location_numbers(unsigned &nr)
Compute location numbers.
Definition: goto_program.h:766
const irep_idt & id() const
Definition: irep.h:384
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:94
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:148
Symbol table entry.
Definition: symbol.h:28
typet type
Type of symbol.
Definition: symbol.h:31
Goto Programs with Functions.
const std::string & id2string(const irep_idt &d)
Definition: irep.h:40
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition: std_types.h:788
std::string to_string(const string_not_contains_constraintt &expr)
Used for debug printing.
Symbol table entry.
#define DATA_CHECK(vm, condition, message)
This macro takes a condition which denotes a well-formedness criterion on goto programs,...
Definition: validate.h:22
validation_modet