cprover
Loading...
Searching...
No Matches
cpp_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/arith_tools.h>
15
16#include <util/c_types.h>
17
20 const source_locationt &source_location,
21 const exprt &object)
22{
23 elaborate_class_template(object.type());
24
25 typet tmp_type(follow(object.type()));
26
28
29 // PODs don't need a destructor
31 return {};
32
33 if(tmp_type.id()==ID_array)
34 {
35 const exprt &size_expr=
37
38 if(size_expr.id() == ID_infinity)
39 return {}; // don't initialize
40
43
44 mp_integer s;
46 {
47 error().source_location=source_location;
48 error() << "array size '" << to_string(size_expr) << "' is not a constant"
49 << eom;
50 throw 0;
51 }
52
53 code_blockt new_code;
54 new_code.add_source_location()=source_location;
55
56 // for each element of the array, call the destructor
57 for(mp_integer i=0; i < s; ++i)
58 {
59 exprt constant = from_integer(i, c_index_type());
60 constant.add_source_location()=source_location;
61
62 index_exprt index(object, constant);
63 index.add_source_location()=source_location;
64
65 auto i_code = cpp_destructor(source_location, index);
66 if(i_code.has_value())
67 new_code.add_to_operands(std::move(i_code.value()));
68 }
69
70 return std::move(new_code);
71 }
72 else
73 {
76
77 // enter struct scope
80
81 // find name of destructor
82 const struct_typet::componentst &components=
83 struct_type.components();
84
86
87 for(const auto &c : components)
88 {
89 const typet &type = c.type();
90
91 if(
92 !c.get_bool(ID_from_base) && type.id() == ID_code &&
94 {
95 dtor_name = c.get_base_name();
96 break;
97 }
98 }
99
100 INVARIANT(!dtor_name.empty(), "non-PODs should have a destructor");
101
102 cpp_namet cpp_name(dtor_name, source_location);
103
104 exprt member(ID_member);
106 member.copy_to_operands(object);
107
109 std::move(member), {}, uninitialized_typet{}, source_location);
110
113
114 code_expressiont new_code(function_call);
115 new_code.add_source_location() = source_location;
116
117 return std::move(new_code);
118 }
119}
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
bool to_integer(const constant_exprt &expr, mp_integer &int_value)
Convert a constant expression expr to an arbitrary-precision integer.
bitvector_typet c_index_type()
Definition c_types.cpp:16
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:564
static void make_already_typechecked(exprt &expr)
const exprt & size() const
Definition std_types.h:790
virtual void make_constant_index(exprt &expr)
A codet representing sequential composition of program statements.
Definition std_code.h:130
codet representation of an expression statement.
Definition std_code.h:1394
const typet & return_type() const
Definition std_types.h:645
cpp_scopet & set_scope(const irep_idt &identifier)
Definition cpp_scopes.h:87
optionalt< codet > cpp_destructor(const source_locationt &source_location, const exprt &object)
void typecheck_side_effect_function_call(side_effect_expr_function_callt &) override
bool cpp_is_pod(const typet &type) const
void elaborate_class_template(const typet &type)
elaborate class template instances
std::string to_string(const typet &) override
cpp_scopest cpp_scopes
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition dstring.h:37
Base class for all expressions.
Definition expr.h:54
void copy_to_operands(const exprt &expr)
Copy the given argument to the end of exprt's operands.
Definition expr.h:129
source_locationt & add_source_location()
Definition expr.h:235
void add_to_operands(const exprt &expr)
Add the given argument to the end of exprt's operands.
Definition expr.h:136
Array index operator.
Definition std_expr.h:1328
const irep_idt & id() const
Definition irep.h:396
irept & add(const irep_idt &name)
Definition irep.cpp:116
source_locationt source_location
Definition message.h:247
mstreamt & error() const
Definition message.h:399
static eomt eom
Definition message.h:297
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
Definition namespace.cpp:49
A side_effect_exprt representation of a function call side effect.
Definition std_code.h:1692
Structure type, corresponds to C style structs.
Definition std_types.h:231
std::vector< componentt > componentst
Definition std_types.h:140
The type of an expression, extends irept.
Definition type.h:29
C++ Language Type Checking.
bool is_reference(const typet &type)
Returns true if the type is a reference.
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition invariant.h:423
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
Definition std_expr.h:2840
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition std_types.h:744
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
Definition std_types.h:308
const array_typet & to_array_type(const typet &type)
Cast a typet to an array_typet.
Definition std_types.h:832