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