CBMC
Loading...
Searching...
No Matches
destructor.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Destructor Calls
4
5Author: Daniel Kroening, kroening@kroening.com
6
7\*******************************************************************/
8
11
12#include "destructor.h"
13
14#include <util/c_types.h>
15#include <util/namespace.h>
16#include <util/pointer_expr.h>
17#include <util/symbol.h>
18
20
22{
23 if(type.id() == ID_struct_tag)
24 {
25 return get_destructor(ns, ns.follow_tag(to_struct_tag_type(type)));
26 }
27 else if(type.id() == ID_struct)
28 {
29 const exprt &methods = static_cast<const exprt &>(type.find(ID_methods));
30
31 for(const auto &op : methods.operands())
32 {
33 if(op.type().id() == ID_code)
34 {
35 const code_typet &code_type = to_code_type(op.type());
36
37 if(
38 code_type.return_type().id() == ID_destructor &&
39 code_type.parameters().size() == 1)
40 {
41 const typet &arg_type = code_type.parameters().front().type();
42
43 if(arg_type.id() != ID_pointer)
44 continue;
45
46 const typet &base_type = to_pointer_type(arg_type).base_type();
47 if(
48 base_type.id() == ID_struct_tag &&
49 ns.follow_tag(to_struct_tag_type(base_type)) == type)
50 {
51 const symbol_exprt symbol_expr(op.get(ID_name), op.type());
52 return code_function_callt(symbol_expr);
53 }
54 }
55 }
56 }
57 }
58
59 return static_cast<const code_function_callt &>(get_nil_irep());
60}
61
63 const std::list<irep_idt> &vars,
64 goto_programt &dest,
65 const namespacet &ns)
66{
67 for(const auto &id : vars)
68 {
69 const symbolt &symbol = ns.lookup(id);
70
71 // do destructor
72 code_function_callt destructor = get_destructor(ns, symbol.type);
73
74 if(destructor.is_not_nil())
75 {
76 // add "this"
77 address_of_exprt this_expr(
78 symbol.symbol_expr(), pointer_type(symbol.type));
79 destructor.arguments().push_back(this_expr);
80
82 destructor, destructor.source_location()));
83 }
84
85 // now create a 'dead' instruction -- will be added after the
86 // destructor created below as unwind_destructor_stack pops off the
87 // top of the destructor stack
88 dest.add(goto_programt::make_dead(symbol.symbol_expr(), symbol.location));
89 }
90}
pointer_typet pointer_type(const typet &subtype)
Definition c_types.cpp:235
Operator to return the address of an object.
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
goto_instruction_codet representation of a function call statement.
Base type of functions.
Definition std_types.h:583
Base class for all expressions.
Definition expr.h:56
operandst & operands()
Definition expr.h:94
const source_locationt & source_location() const
Definition expr.h:231
A generic container class for the GOTO intermediate representation of one function.
static instructiont make_dead(const symbol_exprt &symbol, const source_locationt &l=source_locationt::nil())
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.
const irept & find(const irep_idt &name) const
Definition irep.cpp:93
bool is_not_nil() const
Definition irep.h:372
const irep_idt & id() const
Definition irep.h:388
const union_typet & follow_tag(const union_tag_typet &) const
Follow type tag of union type.
Definition namespace.cpp:63
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().
Expression to hold a symbol (variable)
Definition std_expr.h:131
Symbol table entry.
Definition symbol.h:28
source_locationt location
Source code location of definition of symbol.
Definition symbol.h:37
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
Definition symbol.cpp:121
typet type
Type of symbol.
Definition symbol.h:31
The type of an expression, extends irept.
Definition type.h:29
code_function_callt get_destructor(const namespacet &ns, const typet &type)
void destruct_locals(const std::list< irep_idt > &vars, goto_programt &dest, const namespacet &ns)
Destructor Calls.
Concrete Goto Program.
const irept & get_nil_irep()
Definition irep.cpp:19
API to expression classes for Pointers.
const pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition std_types.h:788
const struct_tag_typet & to_struct_tag_type(const typet &type)
Cast a typet to a struct_tag_typet.
Definition std_types.h:518
Symbol table entry.