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 #include <util/pointer_expr.h>
16 
17 bool cpp_typecheckt::find_dtor(const symbolt &symbol) const
18 {
19  for(const auto &c : to_struct_type(symbol.type).components())
20  {
21  if(c.get_base_name() == "~" + id2string(symbol.base_name))
22  return true;
23  }
24 
25  return false;
26 }
27 
30  const symbolt &symbol,
31  cpp_declarationt &dtor)
32 {
33  assert(symbol.type.id()==ID_struct ||
34  symbol.type.id()==ID_union);
35 
36  cpp_declaratort decl;
37  decl.name() = cpp_namet("~" + id2string(symbol.base_name), symbol.location);
38  decl.type().id(ID_function_type);
39  decl.type().subtype().make_nil();
40 
41  decl.value() = code_blockt();
42  decl.add(ID_cv).make_nil();
43  decl.add(ID_throw_decl).make_nil();
44 
45  dtor.add(ID_type).id(ID_destructor);
46  dtor.add(ID_storage_spec).id(ID_cpp_storage_spec);
47  dtor.add_to_operands(std::move(decl));
48 }
49 
51 codet cpp_typecheckt::dtor(const symbolt &symbol, const symbol_exprt &this_expr)
52 {
53  assert(symbol.type.id()==ID_struct ||
54  symbol.type.id()==ID_union);
55 
56  source_locationt source_location=symbol.type.source_location();
57 
58  source_location.set_function(
59  id2string(symbol.base_name)+
60  "::~"+id2string(symbol.base_name)+"()");
61 
62  code_blockt block;
63 
64  const struct_union_typet::componentst &components =
66 
67  // take care of virtual methods
68  for(const auto &c : components)
69  {
70  if(c.get_bool(ID_is_vtptr))
71  {
72  const cpp_namet cppname(c.get_base_name());
73 
74  const symbolt &virtual_table_symbol_type =
75  lookup(c.type().subtype().get(ID_identifier));
76 
77  const symbolt &virtual_table_symbol_var = lookup(
78  id2string(virtual_table_symbol_type.name) + "@" +
79  id2string(symbol.name));
80 
81  exprt var=virtual_table_symbol_var.symbol_expr();
82  address_of_exprt address(var);
83  assert(address.type() == c.type());
84 
86 
87  exprt ptrmember(ID_ptrmember);
88  ptrmember.set(ID_component_name, c.get_name());
89  ptrmember.operands().push_back(this_expr);
90 
91  code_assignt assign(ptrmember, address);
92  block.add(assign);
93  continue;
94  }
95  }
96 
97  // call the data member destructors in the reverse order
98  for(struct_union_typet::componentst::const_reverse_iterator
99  cit=components.rbegin();
100  cit!=components.rend();
101  cit++)
102  {
103  const typet &type=cit->type();
104 
105  if(cit->get_bool(ID_from_base) ||
106  cit->get_bool(ID_is_type) ||
107  cit->get_bool(ID_is_static) ||
108  type.id()==ID_code ||
109  is_reference(type) ||
110  cpp_is_pod(type))
111  continue;
112 
113  const cpp_namet cppname(cit->get_base_name(), source_location);
114 
115  exprt member(ID_ptrmember, type);
116  member.set(ID_component_cpp_name, cppname);
117  member.operands().push_back(this_expr);
118  member.add_source_location() = source_location;
119 
120  const bool disabled_access_control = disable_access_control;
121  disable_access_control = true;
122  auto dtor_code = cpp_destructor(source_location, member);
123  disable_access_control = disabled_access_control;
124 
125  if(dtor_code.has_value())
126  block.add(dtor_code.value());
127  }
128 
129  if(symbol.type.id() == ID_union)
130  return std::move(block);
131 
132  const auto &bases = to_struct_type(symbol.type).bases();
133 
134  // call the base destructors in the reverse order
135  for(class_typet::basest::const_reverse_iterator bit = bases.rbegin();
136  bit != bases.rend();
137  bit++)
138  {
139  DATA_INVARIANT(bit->id() == ID_base, "base class expression expected");
140  const symbolt &psymb = lookup(bit->type());
141 
142  dereference_exprt object{this_expr, psymb.type};
143  object.add_source_location() = source_location;
144 
145  const bool disabled_access_control = disable_access_control;
146  disable_access_control = true;
147  auto dtor_code = cpp_destructor(source_location, object);
148  disable_access_control = disabled_access_control;
149 
150  if(dtor_code.has_value())
151  block.add(dtor_code.value());
152  }
153 
154  return std::move(block);
155 }
struct_union_typet::components
const componentst & components() const
Definition: std_types.h:141
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:302
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:208
irept::make_nil
void make_nil()
Definition: irep.h:465
typet
The type of an expression, extends irept.
Definition: type.h:28
symbolt::type
typet type
Type of symbol.
Definition: symbol.h:31
dereference_exprt
Operator to dereference a pointer.
Definition: pointer_expr.h:386
irept::add
irept & add(const irep_namet &name)
Definition: irep.cpp:122
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:54
struct_union_typet::componentst
std::vector< componentt > componentst
Definition: std_types.h:134
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:51
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:80
cpp_typecheckt::default_dtor
void default_dtor(const symbolt &symb, cpp_declarationt &dtor)
Note:
Definition: cpp_typecheck_destructor.cpp:29
cpp_typecheckt::cpp_is_pod
bool cpp_is_pod(const typet &type) const
Definition: cpp_is_pod.cpp:16
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:82
already_typechecked_exprt::make_already_typechecked
static void make_already_typechecked(exprt &expr)
Definition: c_typecheck_base.h:287
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:49
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
pointer_expr.h
API to expression classes for Pointers.
struct_typet::bases
const basest & bases() const
Get the collection of base classes/structs.
Definition: std_types.h:256
cpp_declarationt
Definition: cpp_declaration.h:22
irept::id
const irep_idt & id() const
Definition: irep.h:407
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:17
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:431
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:144
cpp_declaratort::value
exprt & value()
Definition: cpp_declarator.h:42
exprt::operands
operandst & operands()
Definition: expr.h:96
address_of_exprt
Operator to return the address of an object.
Definition: pointer_expr.h:330
exprt::add_source_location
source_locationt & add_source_location()
Definition: expr.h:243
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