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 typet &type=ns.follow(e.type());
32  const struct_typet &struct_type=to_struct_type(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=to_struct_type(ns.follow(expr.type()));
89  const struct_typet::componentst &components=struct_type.components();
90 
91  if(components.empty())
92  // Presumably this means the type has not yet been determined
93  return;
94  PRECONDITION(!expr.operands().empty());
95 
96  if(components.front().get_name() == JAVA_CLASS_IDENTIFIER_FIELD_NAME)
97  {
98  INVARIANT(expr.op0().is_constant(), "@class_identifier must be a constant");
99  expr.op0()=constant_exprt(class_type.get_identifier(), string_typet());
100  }
101  else
102  {
103  // The first member must be the base class
104  INVARIANT(
105  expr.op0().id()==ID_struct, "Non @class_identifier must be a structure");
106  set_class_identifier(to_struct_expr(expr.op0()), ns, class_type);
107  }
108 }
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:2987
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:384
void swap(irept &irep)
Definition: irep.h:430
Extract member of struct or union.
Definition: std_expr.h:2841
exprt & op0()
Definition: std_expr.h:932
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
Definition: namespace.cpp:49
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:957
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
The type of an expression, extends irept.
Definition: type.h:29
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_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
Definition: std_types.h:308