CBMC
Loading...
Searching...
No Matches
class_identifier.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Extract class identifier
4
5Author: Chris Smowton, [email protected]
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 {
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();
38 e,
40 components.front().type());
41
43 {
44 // found it
45 return std::move(member_expr);
46 }
47 else
48 {
50 }
51 }
52}
53
58 const exprt &this_expr_in,
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();
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,
87{
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");
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
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
A constant literal expression.
Definition std_expr.h:3117
Operator to dereference a pointer.
The empty type.
Definition std_types.h:51
Base class for all expressions.
Definition expr.h:56
bool is_constant() const
Return whether the expression is a constant.
Definition expr.h:212
typet & type()
Return the type of the expression.
Definition expr.h:84
operandst & operands()
Definition expr.h:94
void swap(irept &irep)
Definition irep.h:434
const irep_idt & id() const
Definition irep.h:388
Extract member of struct or union.
Definition std_expr.h:2971
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:49
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition namespace.h:91
String type.
Definition std_types.h:966
Struct constructor from list of elements.
Definition std_expr.h:1877
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
std::vector< componentt > componentst
Definition std_types.h:140
Semantic type conversion.
Definition std_expr.h:2073
API to expression classes for Pointers.
const pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
#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:1900
const struct_tag_typet & to_struct_tag_type(const typet &type)
Cast a typet to a struct_tag_typet.
Definition std_types.h:518