cprover
cpp_typecheck_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/c_types.h>
15 
16 bool 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  assert(symbol.type.id()==ID_struct ||
33  symbol.type.id()==ID_union);
34 
35  cpp_declaratort decl;
36  decl.name() = cpp_namet("~" + id2string(symbol.base_name), symbol.location);
37  decl.type().id(ID_function_type);
38  decl.type().subtype().make_nil();
39 
40  decl.value() = code_blockt();
41  decl.add(ID_cv).make_nil();
42  decl.add(ID_throw_decl).make_nil();
43 
44  dtor.add(ID_type).id(ID_destructor);
45  dtor.add(ID_storage_spec).id(ID_cpp_storage_spec);
46  dtor.add_to_operands(std::move(decl));
47 }
48 
50 codet cpp_typecheckt::dtor(const symbolt &symbol, const symbol_exprt &this_expr)
51 {
52  assert(symbol.type.id()==ID_struct ||
53  symbol.type.id()==ID_union);
54 
55  source_locationt source_location=symbol.type.source_location();
56 
57  source_location.set_function(
58  id2string(symbol.base_name)+
59  "::~"+id2string(symbol.base_name)+"()");
60 
61  code_blockt block;
62 
63  const struct_union_typet::componentst &components =
65 
66  // take care of virtual methods
67  for(const auto &c : components)
68  {
69  if(c.get_bool(ID_is_vtptr))
70  {
71  const cpp_namet cppname(c.get_base_name());
72 
73  const symbolt &virtual_table_symbol_type =
74  lookup(c.type().subtype().get(ID_identifier));
75 
76  const symbolt &virtual_table_symbol_var = lookup(
77  id2string(virtual_table_symbol_type.name) + "@" +
78  id2string(symbol.name));
79 
80  exprt var=virtual_table_symbol_var.symbol_expr();
81  address_of_exprt address(var);
82  assert(address.type() == c.type());
83 
85 
86  exprt ptrmember(ID_ptrmember);
87  ptrmember.set(ID_component_name, c.get_name());
88  ptrmember.operands().push_back(this_expr);
89 
90  code_assignt assign(ptrmember, address);
91  block.add(assign);
92  continue;
93  }
94  }
95 
96  // call the data member destructors in the reverse order
97  for(struct_union_typet::componentst::const_reverse_iterator
98  cit=components.rbegin();
99  cit!=components.rend();
100  cit++)
101  {
102  const typet &type=cit->type();
103 
104  if(cit->get_bool(ID_from_base) ||
105  cit->get_bool(ID_is_type) ||
106  cit->get_bool(ID_is_static) ||
107  type.id()==ID_code ||
108  is_reference(type) ||
109  cpp_is_pod(type))
110  continue;
111 
112  const cpp_namet cppname(cit->get_base_name(), source_location);
113 
114  exprt member(ID_ptrmember, type);
115  member.set(ID_component_cpp_name, cppname);
116  member.operands().push_back(this_expr);
117  member.add_source_location() = source_location;
118 
119  const bool disabled_access_control = disable_access_control;
120  disable_access_control = true;
121  auto dtor_code = cpp_destructor(source_location, member);
122  disable_access_control = disabled_access_control;
123 
124  if(dtor_code.has_value())
125  block.add(dtor_code.value());
126  }
127 
128  if(symbol.type.id() == ID_union)
129  return std::move(block);
130 
131  const auto &bases = to_struct_type(symbol.type).bases();
132 
133  // call the base destructors in the reverse order
134  for(class_typet::basest::const_reverse_iterator bit = bases.rbegin();
135  bit != bases.rend();
136  bit++)
137  {
138  DATA_INVARIANT(bit->id() == ID_base, "base class expression expected");
139  const symbolt &psymb = lookup(bit->type());
140 
141  dereference_exprt object{this_expr, psymb.type};
142  object.add_source_location() = source_location;
143 
144  const bool disabled_access_control = disable_access_control;
145  disable_access_control = true;
146  auto dtor_code = cpp_destructor(source_location, object);
147  disable_access_control = disabled_access_control;
148 
149  if(dtor_code.has_value())
150  block.add(dtor_code.value());
151  }
152 
153  return std::move(block);
154 }
struct_union_typet::components
const componentst & components() const
Definition: std_types.h:142
code_blockt
A codet representing sequential composition of program statements.
Definition: std_code.h:170
typet::subtype
const typet & subtype() const
Definition: type.h:47
source_locationt::set_function
void set_function(const irep_idt &function)
Definition: source_location.h:126
to_struct_type
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
Definition: std_types.h:303
to_struct_union_type
const struct_union_typet & to_struct_union_type(const typet &type)
Cast a typet to a struct_union_typet.
Definition: std_types.h:209
irept::make_nil
void make_nil()
Definition: irep.h:475
typet
The type of an expression, extends irept.
Definition: type.h:29
symbolt::type
typet type
Type of symbol.
Definition: symbol.h:31
dereference_exprt
Operator to dereference a pointer.
Definition: std_expr.h:2888
irept::add
irept & add(const irep_namet &name)
Definition: irep.cpp:113
cpp_declaratort::name
cpp_namet & name()
Definition: cpp_declarator.h:36
namespacet::lookup
const symbolt & lookup(const irep_idt &name) const
Lookup a symbol in the namespace.
Definition: namespace.h:44
exprt
Base class for all expressions.
Definition: expr.h:53
struct_union_typet::componentst
std::vector< componentt > componentst
Definition: std_types.h:135
symbolt::base_name
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
cpp_typecheckt::dtor
codet dtor(const symbolt &symb, const symbol_exprt &this_expr)
produces destructor code for a class object
Definition: cpp_typecheck_destructor.cpp:50
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:82
cpp_typecheckt::default_dtor
void default_dtor(const symbolt &symb, cpp_declarationt &dtor)
Note:
Definition: cpp_typecheck_destructor.cpp:28
cpp_typecheckt::cpp_is_pod
bool cpp_is_pod(const typet &type) const
Definition: cpp_is_pod.cpp:14
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:81
already_typechecked_exprt::make_already_typechecked
static void make_already_typechecked(exprt &expr)
Definition: c_typecheck_base.h:289
DATA_INVARIANT
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition: invariant.h:511
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
typet::source_location
const source_locationt & source_location() const
Definition: type.h:71
symbolt::symbol_expr
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
Definition: symbol.cpp:122
struct_typet::bases
const basest & bases() const
Get the collection of base classes/structs.
Definition: std_types.h:257
cpp_declarationt
Definition: cpp_declaration.h:24
irept::id
const irep_idt & id() const
Definition: irep.h:418
code_blockt::add
void add(const codet &code)
Definition: std_code.h:208
typet::add_source_location
source_locationt & add_source_location()
Definition: type.h:76
cpp_typecheck.h
C++ Language Type Checking.
source_locationt
Definition: source_location.h:20
cpp_typecheckt::find_dtor
bool find_dtor(const symbolt &symbol) const
Definition: cpp_typecheck_destructor.cpp:16
is_reference
bool is_reference(const typet &type)
Returns true if the type is a reference.
Definition: std_types.cpp:133
symbolt::location
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
symbolt
Symbol table entry.
Definition: symbol.h:28
irept::set
void set(const irep_namet &name, const irep_idt &value)
Definition: irep.h:442
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:157
cpp_declaratort::value
exprt & value()
Definition: cpp_declarator.h:42
exprt::operands
operandst & operands()
Definition: expr.h:95
address_of_exprt
Operator to return the address of an object.
Definition: std_expr.h:2786
exprt::add_source_location
source_locationt & add_source_location()
Definition: expr.h:259
code_assignt
A codet representing an assignment in the program.
Definition: std_code.h:295
cpp_typecheckt::cpp_destructor
optionalt< codet > cpp_destructor(const source_locationt &source_location, const exprt &object)
Definition: cpp_destructor.cpp:19
cpp_namet
Definition: cpp_name.h:17
c_types.h
cpp_typecheckt::disable_access_control
bool disable_access_control
Definition: cpp_typecheck.h:594
symbolt::name
irep_idt name
The unique identifier.
Definition: symbol.h:40
cpp_declaratort
Definition: cpp_declarator.h:20
codet
Data structure for representing an arbitrary statement in a program.
Definition: std_code.h:35