Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / src / mc / inspect / Type.hpp
1 /* Copyright (c) 2007-2022. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_MC_TYPE_HPP
7 #define SIMGRID_MC_TYPE_HPP
8
9 #include <cstddef>
10
11 #include <string>
12 #include <vector>
13
14 #include <dwarf.h>
15
16 #include "xbt/asserts.h"
17 #include "xbt/base.h"
18
19 #include "src/mc/inspect/LocationList.hpp"
20 #include "src/mc/mc_forward.hpp"
21
22 namespace simgrid {
23 namespace mc {
24
25 /** A member of a structure, union
26  *
27  *  Inheritance is seen as a special member as well.
28  */
29 class Member {
30 public:
31   using flags_type                                 = int;
32   static constexpr flags_type INHERITANCE_FLAG     = 1;
33   static constexpr flags_type VIRTUAL_POINTER_FLAG = 2;
34
35   Member() = default;
36
37   /** Whether this member represent some inherited part of the object */
38   flags_type flags = 0;
39
40   /** Name of the member (if any) */
41   std::string name;
42
43   /** DWARF location expression for locating the location of the member */
44   simgrid::dwarf::DwarfExpression location_expression;
45
46   std::size_t byte_size = 0; // Do we really need this?
47
48   unsigned type_id        = 0;
49   simgrid::mc::Type* type = nullptr;
50
51   bool isInheritance() const { return this->flags & INHERITANCE_FLAG; }
52   bool isVirtualPointer() const { return this->flags & VIRTUAL_POINTER_FLAG; }
53
54   /** Whether the member is at a fixed offset from the base address */
55   bool has_offset_location() const
56   {
57     // Recognize the expression `DW_OP_plus_uconst(offset)`:
58     return location_expression.size() == 1 && location_expression[0].atom == DW_OP_plus_uconst;
59   }
60
61   /** Get the offset of the member
62    *
63    *  This is only valid is the member is at a fixed offset from the base.
64    *  This is often the case (for C types, C++ type without virtual
65    *  inheritance).
66    *
67    *  If the location is more complex, the location expression has
68    *  to be evaluated (which might need accessing the memory).
69    */
70   int offset() const
71   {
72     xbt_assert(this->has_offset_location());
73     return this->location_expression[0].number;
74   }
75
76   /** Set the location of the member as a fixed offset */
77   void offset(int new_offset)
78   {
79     // Set the expression to be `DW_OP_plus_uconst(offset)`:
80     Dwarf_Op op;
81     op.atom                   = DW_OP_plus_uconst;
82     op.number                 = new_offset;
83     this->location_expression = {op};
84   }
85 };
86
87 /** A type in the model-checked program */
88 class Type {
89 public:
90   Type() = default;
91
92   /** The DWARF TAG of the type (e.g. DW_TAG_array_type) */
93   int type    = 0;
94   unsigned id = 0;             /* Offset in the section (in hexadecimal form) */
95   std::string name;            /* Name of the type */
96   int byte_size     = 0;       /* Size in bytes */
97   int element_count = 0;       /* Number of elements for array type */
98   unsigned type_id  = 0;       /* DW_AT_type id */
99   std::vector<Member> members; /* if DW_TAG_structure_type, DW_TAG_class_type, DW_TAG_union_type*/
100
101   simgrid::mc::Type* subtype   = nullptr; // DW_AT_type
102   simgrid::mc::Type* full_type = nullptr; // The same (but more complete) type
103 };
104
105 } // namespace mc
106 } // namespace simgrid
107
108 #endif