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().id(ID_code);
41  decl.value().add(ID_type).id(ID_code);
42  decl.value().set(ID_statement, ID_block);
43  decl.add(ID_cv).make_nil();
44  decl.add(ID_throw_decl).make_nil();
45 
46  dtor.add(ID_type).id(ID_destructor);
47  dtor.add(ID_storage_spec).id(ID_cpp_storage_spec);
48  dtor.move_to_operands(decl);
49 }
50 
55 {
56  assert(symbol.type.id()==ID_struct ||
57  symbol.type.id()==ID_union);
58 
59  source_locationt source_location=symbol.type.source_location();
60 
61  source_location.set_function(
62  id2string(symbol.base_name)+
63  "::~"+id2string(symbol.base_name)+"()");
64 
65  code_blockt block;
66 
67  const struct_union_typet::componentst &components =
69 
70  // take care of virtual methods
71  for(const auto &c : components)
72  {
73  if(c.get_bool(ID_is_vtptr))
74  {
75  const cpp_namet cppname(c.get_base_name());
76 
77  const symbolt &virtual_table_symbol_type =
78  lookup(c.type().subtype().get(ID_identifier));
79 
80  const symbolt &virtual_table_symbol_var = lookup(
81  id2string(virtual_table_symbol_type.name) + "@" +
82  id2string(symbol.name));
83 
84  exprt var=virtual_table_symbol_var.symbol_expr();
85  address_of_exprt address(var);
86  assert(address.type() == c.type());
87 
88  already_typechecked(address);
89 
90  exprt ptrmember(ID_ptrmember);
91  ptrmember.set(ID_component_name, c.get_name());
92  ptrmember.operands().push_back(exprt("cpp-this"));
93 
94  code_assignt assign(ptrmember, address);
95  block.add(assign);
96  continue;
97  }
98  }
99 
100  // call the data member destructors in the reverse order
101  for(struct_union_typet::componentst::const_reverse_iterator
102  cit=components.rbegin();
103  cit!=components.rend();
104  cit++)
105  {
106  const typet &type=cit->type();
107 
108  if(cit->get_bool(ID_from_base) ||
109  cit->get_bool(ID_is_type) ||
110  cit->get_bool(ID_is_static) ||
111  type.id()==ID_code ||
112  is_reference(type) ||
113  cpp_is_pod(type))
114  continue;
115 
116  const cpp_namet cppname(cit->get_base_name(), source_location);
117 
118  exprt member(ID_ptrmember, type);
119  member.set(ID_component_cpp_name, cppname);
120  member.operands().push_back(
121  symbol_exprt(ID_this, pointer_type(symbol.type)));
122  member.add_source_location() = source_location;
123 
124  const bool disabled_access_control = disable_access_control;
125  disable_access_control = true;
126  auto dtor_code = cpp_destructor(source_location, member);
127  disable_access_control = disabled_access_control;
128 
129  if(dtor_code.has_value())
130  block.add(dtor_code.value());
131  }
132 
133  if(symbol.type.id() == ID_union)
134  return std::move(block);
135 
136  const auto &bases = to_struct_type(symbol.type).bases();
137 
138  // call the base destructors in the reverse order
139  for(class_typet::basest::const_reverse_iterator bit = bases.rbegin();
140  bit != bases.rend();
141  bit++)
142  {
143  DATA_INVARIANT(bit->id() == ID_base, "base class expression expected");
144  const symbolt &psymb = lookup(bit->type());
145 
146  symbol_exprt this_ptr(ID_this, pointer_type(symbol.type));
147  dereference_exprt object(this_ptr, psymb.type);
148  object.add_source_location() = source_location;
149 
150  const bool disabled_access_control = disable_access_control;
151  disable_access_control = true;
152  auto dtor_code = cpp_destructor(source_location, object);
153  disable_access_control = disabled_access_control;
154 
155  if(dtor_code.has_value())
156  block.add(dtor_code.value());
157  }
158 
159  return std::move(block);
160 }
struct_union_typet::components
const componentst & components() const
Definition: std_types.h:205
code_blockt
A codet representing sequential composition of program statements.
Definition: std_code.h:150
exprt::move_to_operands
void move_to_operands(exprt &expr)
Move the given argument to the end of exprt's operands.
Definition: expr.cpp:29
source_locationt::set_function
void set_function(const irep_idt &function)
Definition: source_location.h:122
to_struct_type
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
Definition: std_types.h:349
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:485
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:260
already_typechecked
void already_typechecked(irept &irep)
Definition: cpp_util.h:18
typet
The type of an expression, extends irept.
Definition: type.h:27
symbolt::type
typet type
Type of symbol.
Definition: symbol.h:31
dereference_exprt
Operator to dereference a pointer.
Definition: std_expr.h:3355
irept::add
irept & add(const irep_namet &name)
Definition: irep.cpp:305
cpp_declaratort::name
cpp_namet & name()
Definition: cpp_declarator.h:36
exprt
Base class for all expressions.
Definition: expr.h:54
struct_union_typet::componentst
std::vector< componentt > componentst
Definition: std_types.h:203
symbolt::base_name
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:143
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:68
namespacet::lookup
bool lookup(const irep_idt &name, const symbolt *&symbol) const override
See documentation for namespace_baset::lookup().
Definition: namespace.cpp:166
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:62
cpp_typecheckt::dtor
codet dtor(const symbolt &symb)
produces destructor code for a class object
Definition: cpp_typecheck_destructor.cpp:54
struct_typet::bases
const basest & bases() const
Get the collection of base classes/structs.
Definition: std_types.h:303
pointer_type
pointer_typet pointer_type(const typet &subtype)
Definition: c_types.cpp:243
cpp_declarationt
Definition: cpp_declaration.h:23
irept::id
const irep_idt & id() const
Definition: irep.h:259
code_blockt::add
void add(const codet &code)
Definition: std_code.h:189
cpp_typecheck.h
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:132
symbolt::location
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
symbolt
Symbol table entry.
Definition: symbol.h:27
irept::set
void set(const irep_namet &name, const irep_idt &value)
Definition: irep.h:286
exprt::operands
operandst & operands()
Definition: expr.h:78
address_of_exprt
Operator to return the address of an object.
Definition: std_expr.h:3255
code_assignt
A codet representing an assignment in the program.
Definition: std_code.h:256
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:16
c_types.h
cpp_typecheckt::disable_access_control
bool disable_access_control
Definition: cpp_typecheck.h:593
symbolt::name
irep_idt name
The unique identifier.
Definition: symbol.h:40
cpp_declaratort
Definition: cpp_declarator.h:19
codet
Data structure for representing an arbitrary statement in a program.
Definition: std_code.h:34