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