cprover
class_identifier.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Extract class identifier
4 
5 Author: Chris Smowton, chris.smowton@diffblue.com
6 
7 \*******************************************************************/
8 
11 
12 #include "class_identifier.h"
13 
14 #include <util/std_expr.h>
15 #include <util/c_types.h>
16 #include <util/namespace.h>
17 
22  const exprt &src,
23  const namespacet &ns)
24 {
25  // the class identifier is in the root class
26  exprt e=src;
27 
28  while(1)
29  {
30  const typet &type=ns.follow(e.type());
31  const struct_typet &struct_type=to_struct_type(type);
32  const struct_typet::componentst &components=struct_type.components();
33  assert(!components.empty());
34 
35  const auto &first_member_name=components.front().get_name();
36  member_exprt member_expr(
37  e,
38  first_member_name,
39  components.front().type());
40 
41  if(first_member_name=="@class_identifier")
42  {
43  // found it
44  return member_expr;
45  }
46  else
47  {
48  e.swap(member_expr);
49  }
50  }
51 }
52 
57  const exprt &this_expr_in,
58  const symbol_typet &suggested_type,
59  const namespacet &ns)
60 {
61  // Get a pointer from which we can extract a clsid.
62  // If it's already a pointer to an object of some sort, just use it;
63  // if it's void* then use the suggested type.
64 
65  exprt this_expr=this_expr_in;
66  assert(this_expr.type().id()==ID_pointer &&
67  "Non-pointer this-arg in remove-virtuals?");
68  const auto &points_to=this_expr.type().subtype();
69  if(points_to==empty_typet())
70  this_expr=typecast_exprt(this_expr, pointer_type(suggested_type));
71  exprt deref=dereference_exprt(this_expr, this_expr.type().subtype());
72  return build_class_identifier(deref, ns);
73 }
The type of an expression.
Definition: type.h:20
const typet & follow(const typet &src) const
Definition: namespace.cpp:66
semantic type conversion
Definition: std_expr.h:1725
pointer_typet pointer_type(const typet &subtype)
Definition: c_types.cpp:296
std::vector< componentt > componentst
Definition: std_types.h:240
const componentst & components() const
Definition: std_types.h:242
typet & type()
Definition: expr.h:60
Structure type.
Definition: std_types.h:296
Extract member of struct or union.
Definition: std_expr.h:3214
const irep_idt & id() const
Definition: irep.h:189
A reference into the symbol table.
Definition: std_types.h:109
The empty type.
Definition: std_types.h:53
Operator to dereference a pointer.
Definition: std_expr.h:2701
API to expression classes.
TO_BE_DOCUMENTED.
Definition: namespace.h:62
static exprt build_class_identifier(const exprt &src, const namespacet &ns)
exprt get_class_identifier_field(const exprt &this_expr_in, const symbol_typet &suggested_type, const namespacet &ns)
const struct_typet & to_struct_type(const typet &type)
Cast a generic typet to a struct_typet.
Definition: std_types.h:317
Base class for all expressions.
Definition: expr.h:46
void swap(irept &irep)
Definition: irep.h:231
Extract class identifier.
const typet & subtype() const
Definition: type.h:31