CBMC
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/c_types.h>
15 #include <util/namespace.h>
16 #include <util/pointer_expr.h>
17 #include <util/std_expr.h>
18 
23  const exprt &src,
24  const namespacet &ns)
25 {
26  // the class identifier is in the root class
27  exprt e=src;
28 
29  while(1)
30  {
31  const struct_typet &struct_type =
33  const struct_typet::componentst &components=struct_type.components();
34  INVARIANT(!components.empty(), "class structs cannot be empty");
35 
36  const auto &first_member_name=components.front().get_name();
37  member_exprt member_expr(
38  e,
39  first_member_name,
40  components.front().type());
41 
42  if(first_member_name == JAVA_CLASS_IDENTIFIER_FIELD_NAME)
43  {
44  // found it
45  return std::move(member_expr);
46  }
47  else
48  {
49  e.swap(member_expr);
50  }
51  }
52 }
53 
58  const exprt &this_expr_in,
59  const struct_tag_typet &suggested_type,
60  const namespacet &ns)
61 {
62  // Get a pointer from which we can extract a clsid.
63  // If it's already a pointer to an object of some sort, just use it;
64  // if it's void* then use the suggested type.
65  PRECONDITION(this_expr_in.type().id() == ID_pointer);
66 
67  exprt this_expr=this_expr_in;
68  const auto &points_to = to_pointer_type(this_expr.type()).base_type();
69  if(points_to==empty_typet())
70  this_expr=typecast_exprt(this_expr, pointer_type(suggested_type));
71  const dereference_exprt deref{this_expr};
72  return build_class_identifier(deref, ns);
73 }
74 
84  struct_exprt &expr,
85  const namespacet &ns,
86  const struct_tag_typet &class_type)
87 {
88  const struct_typet &struct_type =
90  const struct_typet::componentst &components=struct_type.components();
91 
92  if(components.empty())
93  // Presumably this means the type has not yet been determined
94  return;
95  PRECONDITION(!expr.operands().empty());
96 
97  if(components.front().get_name() == JAVA_CLASS_IDENTIFIER_FIELD_NAME)
98  {
99  INVARIANT(expr.op0().is_constant(), "@class_identifier must be a constant");
100  expr.op0()=constant_exprt(class_type.get_identifier(), string_typet());
101  }
102  else
103  {
104  // The first member must be the base class
105  INVARIANT(
106  expr.op0().id()==ID_struct, "Non @class_identifier must be a structure");
107  set_class_identifier(to_struct_expr(expr.op0()), ns, class_type);
108  }
109 }
pointer_typet pointer_type(const typet &subtype)
Definition: c_types.cpp:235
static exprt build_class_identifier(const exprt &src, const namespacet &ns)
void set_class_identifier(struct_exprt &expr, const namespacet &ns, const struct_tag_typet &class_type)
If expr has its components filled in then sets the @class_identifier member of the struct.
exprt get_class_identifier_field(const exprt &this_expr_in, const struct_tag_typet &suggested_type, const namespacet &ns)
Extract class identifier.
#define JAVA_CLASS_IDENTIFIER_FIELD_NAME
A constant literal expression.
Definition: std_expr.h:2990
Operator to dereference a pointer.
Definition: pointer_expr.h:834
The empty type.
Definition: std_types.h:51
Base class for all expressions.
Definition: expr.h:56
typet & type()
Return the type of the expression.
Definition: expr.h:84
bool is_constant() const
Return whether the expression is a constant.
Definition: expr.h:212
operandst & operands()
Definition: expr.h:94
const irep_idt & id() const
Definition: irep.h:388
void swap(irept &irep)
Definition: irep.h:434
Extract member of struct or union.
Definition: std_expr.h:2844
exprt & op0()
Definition: std_expr.h:932
const union_typet & follow_tag(const union_tag_typet &) const
Follow type tag of union type.
Definition: namespace.cpp:63
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:94
const typet & base_type() const
The type of the data what we point to.
Definition: pointer_expr.h:35
String type.
Definition: std_types.h:966
Struct constructor from list of elements.
Definition: std_expr.h:1872
A struct tag type, i.e., struct_typet with an identifier.
Definition: std_types.h:493
Structure type, corresponds to C style structs.
Definition: std_types.h:231
const componentst & components() const
Definition: std_types.h:147
std::vector< componentt > componentst
Definition: std_types.h:140
const irep_idt & get_identifier() const
Definition: std_types.h:410
Semantic type conversion.
Definition: std_expr.h:2068
API to expression classes for Pointers.
const pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
Definition: pointer_expr.h:93
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition: invariant.h:423
API to expression classes.
const struct_exprt & to_struct_expr(const exprt &expr)
Cast an exprt to a struct_exprt.
Definition: std_expr.h:1895
const struct_tag_typet & to_struct_tag_type(const typet &type)
Cast a typet to a struct_tag_typet.
Definition: std_types.h:518