CBMC
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
cpp_typecheck_destructor.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: C++ Language Type Checking
4
5Author: Daniel Kroening, kroening@cs.cmu.edu
6
7\*******************************************************************/
8
11
12#include "cpp_typecheck.h"
13
14#include <util/pointer_expr.h>
15
16bool cpp_typecheckt::find_dtor(const symbolt &symbol) const
17{
18 for(const auto &c : to_struct_type(symbol.type).components())
19 {
20 if(c.get_base_name() == "~" + id2string(symbol.base_name))
21 return true;
22 }
23
24 return false;
25}
26
29 const symbolt &symbol,
30 cpp_declarationt &dtor)
31{
32 PRECONDITION(symbol.type.id() == ID_struct || symbol.type.id() == ID_union);
33
34 cpp_declaratort decl;
35 decl.name() = cpp_namet("~" + id2string(symbol.base_name), symbol.location);
36 decl.type().id(ID_function_type);
37 decl.type().add_subtype().make_nil();
38
39 decl.value() = code_blockt();
40 decl.add(ID_cv).make_nil();
42
45 dtor.add_to_operands(std::move(decl));
46}
47
49codet cpp_typecheckt::dtor(const symbolt &symbol, const symbol_exprt &this_expr)
50{
51 PRECONDITION(symbol.type.id() == ID_struct || symbol.type.id() == ID_union);
52
53 source_locationt source_location=symbol.type.source_location();
54
55 source_location.set_function(
56 id2string(symbol.base_name)+
57 "::~"+id2string(symbol.base_name)+"()");
58
59 code_blockt block;
60
61 const struct_union_typet::componentst &components =
62 to_struct_union_type(symbol.type).components();
63
64 // take care of virtual methods
65 for(const auto &c : components)
66 {
67 if(c.get_bool(ID_is_vtptr))
68 {
69 const cpp_namet cppname(c.get_base_name());
70
72 lookup(to_pointer_type(c.type()).base_type().get(ID_identifier));
73
76 id2string(symbol.name));
77
78 exprt var=virtual_table_symbol_var.symbol_expr();
79 address_of_exprt address(var);
80 DATA_INVARIANT(address.type() == c.type(), "type mismatch");
81
83
85 ptrmember.set(ID_component_name, c.get_name());
86 ptrmember.operands().push_back(this_expr);
87
88 code_frontend_assignt assign(ptrmember, address);
89 block.add(assign);
90 continue;
91 }
92 }
93
94 // call the data member destructors in the reverse order
95 for(struct_union_typet::componentst::const_reverse_iterator
96 cit=components.rbegin();
97 cit!=components.rend();
98 cit++)
99 {
100 const typet &type=cit->type();
101
102 if(cit->get_bool(ID_from_base) ||
103 cit->get_bool(ID_is_type) ||
104 cit->get_bool(ID_is_static) ||
105 type.id()==ID_code ||
106 is_reference(type) ||
107 cpp_is_pod(type))
108 continue;
109
110 const cpp_namet cppname(cit->get_base_name(), source_location);
111
112 exprt member(ID_ptrmember, type);
114 member.operands().push_back(this_expr);
115 member.add_source_location() = source_location;
116
119 auto dtor_code = cpp_destructor(source_location, member);
121
122 if(dtor_code.has_value())
123 block.add(dtor_code.value());
124 }
125
126 if(symbol.type.id() == ID_union)
127 return std::move(block);
128
129 const auto &bases = to_struct_type(symbol.type).bases();
130
131 // call the base destructors in the reverse order
132 for(class_typet::basest::const_reverse_iterator bit = bases.rbegin();
133 bit != bases.rend();
134 bit++)
135 {
136 DATA_INVARIANT(bit->id() == ID_base, "base class expression expected");
137
138 dereference_exprt object{this_expr, bit->type()};
139 object.add_source_location() = source_location;
140
143 auto dtor_code = cpp_destructor(source_location, object);
145
146 if(dtor_code.has_value())
147 block.add(dtor_code.value());
148 }
149
150 return std::move(block);
151}
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
static void make_already_typechecked(exprt &expr)
A codet representing sequential composition of program statements.
Definition std_code.h:130
void add(const codet &code)
Definition std_code.h:168
A codet representing an assignment in the program.
Definition std_code.h:24
Data structure for representing an arbitrary statement in a program.
cpp_namet & name()
void default_dtor(const symbolt &symb, cpp_declarationt &dtor)
Note:
bool cpp_is_pod(const typet &type) const
codet dtor(const symbolt &symb, const symbol_exprt &this_expr)
produces destructor code for a class object
std::optional< codet > cpp_destructor(const source_locationt &source_location, const exprt &object)
bool disable_access_control
bool find_dtor(const symbolt &symbol) const
Operator to dereference a pointer.
Base class for all expressions.
Definition expr.h:56
typet & type()
Return the type of the expression.
Definition expr.h:84
operandst & operands()
Definition expr.h:94
source_locationt & add_source_location()
Definition expr.h:236
void add_to_operands(const exprt &expr)
Add the given argument to the end of exprt's operands.
Definition expr.h:170
void set(const irep_idt &name, const irep_idt &value)
Definition irep.h:412
void make_nil()
Definition irep.h:446
const irep_idt & id() const
Definition irep.h:388
irept & add(const irep_idt &name)
Definition irep.cpp:103
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
void set_function(const irep_idt &function)
std::vector< componentt > componentst
Definition std_types.h:140
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
typet type
Type of symbol.
Definition symbol.h:31
irep_idt name
The unique identifier.
Definition symbol.h:40
irep_idt irep_idt base_name
Name of module the symbol belongs to.
Definition symbol.h:46
The type of an expression, extends irept.
Definition type.h:29
const source_locationt & source_location() const
Definition type.h:72
typet & add_subtype()
Definition type.h:53
C++ Language Type Checking.
const std::string & id2string(const irep_idt &d)
Definition irep.h:44
API to expression classes for Pointers.
bool is_reference(const typet &type)
Returns true if the type is a reference.
const pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition invariant.h:534
#define PRECONDITION(CONDITION)
Definition invariant.h:463
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
Definition std_types.h:308
const struct_union_typet & to_struct_union_type(const typet &type)
Cast a typet to a struct_union_typet.
Definition std_types.h:214