CBMC
cpp_destructor.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: C++ Language Type Checking
4 
5 Author: 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 
27  assert(!is_reference(tmp_type));
28 
29  // PODs don't need a destructor
30  if(cpp_is_pod(tmp_type))
31  return {};
32 
33  if(tmp_type.id()==ID_array)
34  {
35  const exprt &size_expr=
36  to_array_type(tmp_type).size();
37 
38  if(size_expr.id() == ID_infinity)
39  return {}; // don't initialize
40 
41  exprt tmp_size=size_expr;
42  make_constant_index(tmp_size);
43 
44  mp_integer s;
45  if(to_integer(to_constant_expr(tmp_size), 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  {
74  const struct_typet &struct_type=
75  to_struct_type(tmp_type);
76 
77  // enter struct scope
78  cpp_save_scopet save_scope(cpp_scopes);
79  cpp_scopes.set_scope(struct_type.get(ID_name));
80 
81  // find name of destructor
82  const struct_typet::componentst &components=
83  struct_type.components();
84 
85  irep_idt dtor_name;
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 &&
93  to_code_type(type).return_type().id() == ID_destructor)
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);
105  member.add(ID_component_cpp_name) = cpp_name;
106  member.copy_to_operands(object);
107 
108  side_effect_expr_function_callt function_call(
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 }
exprt::copy_to_operands
void copy_to_operands(const exprt &expr)
Copy the given argument to the end of exprt's operands.
Definition: expr.h:155
struct_union_typet::components
const componentst & components() const
Definition: std_types.h:147
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
code_blockt
A codet representing sequential composition of program statements.
Definition: std_code.h:129
mp_integer
BigInt mp_integer
Definition: smt_terms.h:17
cpp_typecheckt::elaborate_class_template
void elaborate_class_template(const typet &type)
elaborate class template instances
Definition: cpp_instantiate_template.cpp:221
arith_tools.h
to_struct_type
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
Definition: std_types.h:308
cpp_save_scopet
Definition: cpp_scopes.h:127
cpp_typecheckt::cpp_scopes
cpp_scopest cpp_scopes
Definition: cpp_typecheck.h:104
typet
The type of an expression, extends irept.
Definition: type.h:28
side_effect_expr_function_callt
A side_effect_exprt representation of a function call side effect.
Definition: std_code.h:1691
exprt
Base class for all expressions.
Definition: expr.h:55
struct_union_typet::componentst
std::vector< componentt > componentst
Definition: std_types.h:140
to_integer
bool to_integer(const constant_exprt &expr, mp_integer &int_value)
Convert a constant expression expr to an arbitrary-precision integer.
Definition: arith_tools.cpp:20
messaget::eom
static eomt eom
Definition: message.h:297
irept::get
const irep_idt & get(const irep_idt &name) const
Definition: irep.cpp:45
array_typet::size
const exprt & size() const
Definition: std_types.h:800
cpp_typecheckt::cpp_is_pod
bool cpp_is_pod(const typet &type) const
Definition: cpp_is_pod.cpp:16
to_code_type
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition: std_types.h:744
messaget::error
mstreamt & error() const
Definition: message.h:399
cpp_typecheckt::typecheck_side_effect_function_call
void typecheck_side_effect_function_call(side_effect_expr_function_callt &) override
Definition: cpp_typecheck_expr.cpp:1504
already_typechecked_exprt::make_already_typechecked
static void make_already_typechecked(exprt &expr)
Definition: c_typecheck_base.h:316
messaget::mstreamt::source_location
source_locationt source_location
Definition: message.h:247
c_typecheck_baset::make_constant_index
virtual void make_constant_index(exprt &expr)
Definition: c_typecheck_expr.cpp:4414
irept::id
const irep_idt & id() const
Definition: irep.h:396
dstringt::empty
bool empty() const
Definition: dstring.h:88
optionalt
nonstd::optional< T > optionalt
Definition: optional.h:35
cpp_typecheck.h
source_locationt
Definition: source_location.h:18
is_reference
bool is_reference(const typet &type)
Returns true if the type is a reference.
Definition: std_types.cpp:143
struct_typet
Structure type, corresponds to C style structs.
Definition: std_types.h:230
irept::add
irept & add(const irep_idt &name)
Definition: irep.cpp:116
namespace_baset::follow
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
Definition: namespace.cpp:49
from_integer
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
Definition: arith_tools.cpp:100
c_typecheck_baset::return_type
typet return_type
Definition: c_typecheck_base.h:170
to_array_type
const array_typet & to_array_type(const typet &type)
Cast a typet to an array_typet.
Definition: std_types.h:844
exprt::add_to_operands
void add_to_operands(const exprt &expr)
Add the given argument to the end of exprt's operands.
Definition: expr.h:162
cpp_typecheckt::to_string
std::string to_string(const typet &) override
Definition: cpp_typecheck.cpp:82
index_exprt
Array index operator.
Definition: std_expr.h:1409
exprt::add_source_location
source_locationt & add_source_location()
Definition: expr.h:216
cpp_typecheckt::cpp_destructor
optionalt< codet > cpp_destructor(const source_locationt &source_location, const exprt &object)
Definition: cpp_destructor.cpp:19
uninitialized_typet
Definition: cpp_parse_tree.h:31
c_index_type
bitvector_typet c_index_type()
Definition: c_types.cpp:16
cpp_namet
Definition: cpp_name.h:16
c_types.h
cpp_scopest::set_scope
cpp_scopet & set_scope(const irep_idt &identifier)
Definition: cpp_scopes.h:87
validation_modet::INVARIANT
@ INVARIANT
code_expressiont
codet representation of an expression statement.
Definition: std_code.h:1393
to_constant_expr
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
Definition: std_expr.h:2992