cprover
cpp_typecheck_virtual_table.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/std_expr.h>
16 
18 {
19  assert(symbol.type.id()==ID_struct);
20 
21  // builds virtual-table value maps: (class x virtual_name x value)
22  std::map<irep_idt, std::map<irep_idt, exprt> > vt_value_maps;
23 
24  const struct_typet &struct_type=to_struct_type(symbol.type);
25 
26  for(std::size_t i=0; i < struct_type.components().size(); i++)
27  {
28  const struct_typet::componentt &compo=struct_type.components()[i];
29  if(!compo.get_bool(ID_is_virtual))
30  continue;
31 
32  const code_typet &code_type=to_code_type(compo.type());
33  assert(code_type.parameters().size() > 0);
34 
35  const pointer_typet &parameter_pointer_type=
36  to_pointer_type(code_type.parameters()[0].type());
37 
38  const irep_idt &class_id =
39  parameter_pointer_type.subtype().get(ID_identifier);
40 
41  std::map<irep_idt, exprt> &value_map =
42  vt_value_maps[class_id];
43 
44  exprt e=symbol_exprt(compo.get_name(), code_type);
45 
46  if(compo.get_bool(ID_is_pure_virtual))
47  {
48  pointer_typet code_pointer_type=pointer_type(code_type);
49  e=null_pointer_exprt(code_pointer_type);
50  value_map[compo.get(ID_virtual_name)] = e;
51  }
52  else
53  {
54  address_of_exprt address(e);
55  value_map[compo.get(ID_virtual_name)] = address;
56  }
57  }
58 
59  // create virtual-table symbol variables
60  for(std::map<irep_idt, std::map<irep_idt, exprt> >::const_iterator cit =
61  vt_value_maps.begin(); cit!=vt_value_maps.end(); cit++)
62  {
63  const std::map<irep_idt, exprt> &value_map=cit->second;
64 
65  const symbolt &late_cast_symb = lookup(cit->first);
66  const symbolt &vt_symb_type =
67  lookup("virtual_table::" + id2string(late_cast_symb.name));
68 
69  symbolt vt_symb_var;
70  vt_symb_var.name=
71  id2string(vt_symb_type.name) + "@"+ id2string(symbol.name);
72  vt_symb_var.base_name=
73  id2string(vt_symb_type.base_name) + "@" + id2string(symbol.base_name);
74  vt_symb_var.mode = symbol.mode;
75  vt_symb_var.module=module;
76  vt_symb_var.location=vt_symb_type.location;
77  vt_symb_var.type = struct_tag_typet(vt_symb_type.name);
78  vt_symb_var.is_lvalue=true;
79  vt_symb_var.is_static_lifetime=true;
80 
81  // do the values
82  const struct_typet &vt_type=to_struct_type(vt_symb_type.type);
83 
84  struct_exprt values({}, struct_tag_typet(vt_symb_type.name));
85 
86  for(const auto &compo : vt_type.components())
87  {
88  std::map<irep_idt, exprt>::const_iterator cit2 =
89  value_map.find(compo.get_base_name());
90  assert(cit2!=value_map.end());
91  const exprt &value=cit2->second;
92  assert(value.type()==compo.type());
93  values.operands().push_back(value);
94  }
95  vt_symb_var.value=values;
96 
97  bool failed=!symbol_table.insert(std::move(vt_symb_var)).second;
99  }
100 }
cpp_typecheckt::do_virtual_table
void do_virtual_table(const symbolt &symbol)
Definition: cpp_typecheck_virtual_table.cpp:17
struct_union_typet::components
const componentst & components() const
Definition: std_types.h:142
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
typet::subtype
const typet & subtype() const
Definition: type.h:47
to_struct_type
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
Definition: std_types.h:303
CHECK_RETURN
#define CHECK_RETURN(CONDITION)
Definition: invariant.h:496
symbolt::type
typet type
Type of symbol.
Definition: symbol.h:31
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
symbolt::base_name
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
struct_tag_typet
A struct tag type, i.e., struct_typet with an identifier.
Definition: std_types.h:490
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:82
struct_exprt
Struct constructor from list of elements.
Definition: std_expr.h:1633
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:81
irept::get_bool
bool get_bool(const irep_namet &name) const
Definition: irep.cpp:64
c_typecheck_baset::module
const irep_idt module
Definition: c_typecheck_base.h:68
to_code_type
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition: std_types.h:946
symbolt::mode
irep_idt mode
Language mode.
Definition: symbol.h:49
null_pointer_exprt
The null pointer constant.
Definition: std_expr.h:3989
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
struct_union_typet::componentt::get_name
const irep_idt & get_name() const
Definition: std_types.h:74
failed
static bool failed(bool error_indicator)
Definition: symtab2gb_parse_options.cpp:34
symbol_tablet::insert
virtual std::pair< symbolt &, bool > insert(symbolt symbol) override
Author: Diffblue Ltd.
Definition: symbol_table.cpp:19
c_typecheck_baset::symbol_table
symbol_tablet & symbol_table
Definition: c_typecheck_base.h:67
pointer_type
pointer_typet pointer_type(const typet &subtype)
Definition: c_types.cpp:243
code_typet
Base type of functions.
Definition: std_types.h:736
irept::id
const irep_idt & id() const
Definition: irep.h:418
code_typet::parameters
const parameterst & parameters() const
Definition: std_types.h:857
to_pointer_type
const pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
Definition: std_types.h:1526
cpp_typecheck.h
C++ Language Type Checking.
struct_union_typet::componentt
Definition: std_types.h:64
symbolt::value
exprt value
Initial value of symbol.
Definition: symbol.h:34
struct_typet
Structure type, corresponds to C style structs.
Definition: std_types.h:226
irept::get
const irep_idt & get(const irep_namet &name) const
Definition: irep.cpp:51
symbolt::location
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
symbolt
Symbol table entry.
Definition: symbol.h:28
symbolt::is_static_lifetime
bool is_static_lifetime
Definition: symbol.h:65
symbolt::is_lvalue
bool is_lvalue
Definition: symbol.h:66
address_of_exprt
Operator to return the address of an object.
Definition: std_expr.h:2786
pointer_typet
The pointer type These are both 'bitvector_typet' (they have a width) and 'type_with_subtypet' (they ...
Definition: std_types.h:1488
symbolt::module
irep_idt module
Name of module the symbol belongs to.
Definition: symbol.h:43
std_expr.h
API to expression classes.
c_types.h
symbolt::name
irep_idt name
The unique identifier.
Definition: symbol.h:40