Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill a parameter that is always true
[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::mc {
23
24 /** A member of a structure, union
25  *
26  *  Inheritance is seen as a special member as well.
27  */
28 class Member {
29 public:
30   using flags_type                                 = int;
31   static constexpr flags_type INHERITANCE_FLAG     = 1;
32   static constexpr flags_type VIRTUAL_POINTER_FLAG = 2;
33
34   Member() = default;
35
36   /** Whether this member represent some inherited part of the object */
37   flags_type flags = 0;
38
39   /** Name of the member (if any) */
40   std::string name;
41
42   /** DWARF location expression for locating the location of the member */
43   simgrid::dwarf::DwarfExpression location_expression;
44
45   std::size_t byte_size = 0; // Do we really need this?
46
47   unsigned type_id        = 0;
48   simgrid::mc::Type* type = nullptr;
49
50   bool isInheritance() const { return this->flags & INHERITANCE_FLAG; }
51   bool isVirtualPointer() const { return this->flags & VIRTUAL_POINTER_FLAG; }
52
53   /** Whether the member is at a fixed offset from the base address */
54   bool has_offset_location() const
55   {
56     // Recognize the expression `DW_OP_plus_uconst(offset)`:
57     return location_expression.size() == 1 && location_expression[0].atom == DW_OP_plus_uconst;
58   }
59
60   /** Get the offset of the member
61    *
62    *  This is only valid is the member is at a fixed offset from the base.
63    *  This is often the case (for C types, C++ type without virtual
64    *  inheritance).
65    *
66    *  If the location is more complex, the location expression has
67    *  to be evaluated (which might need accessing the memory).
68    */
69   int offset() const
70   {
71     xbt_assert(this->has_offset_location());
72     return this->location_expression[0].number;
73   }
74
75   /** Set the location of the member as a fixed offset */
76   void offset(int new_offset)
77   {
78     // Set the expression to be `DW_OP_plus_uconst(offset)`:
79     Dwarf_Op op;
80     op.atom                   = DW_OP_plus_uconst;
81     op.number                 = new_offset;
82     this->location_expression = {op};
83   }
84 };
85
86 /** A type in the model-checked program */
87 class Type {
88 public:
89   Type() = default;
90
91   /** The DWARF TAG of the type (e.g. DW_TAG_array_type) */
92   int type    = 0;
93   unsigned id = 0;             /* Offset in the section (in hexadecimal form) */
94   std::string name;            /* Name of the type */
95   int byte_size     = 0;       /* Size in bytes */
96   int element_count = 0;       /* Number of elements for array type */
97   unsigned type_id  = 0;       /* DW_AT_type id */
98   std::vector<Member> members; /* if DW_TAG_structure_type, DW_TAG_class_type, DW_TAG_union_type*/
99
100   simgrid::mc::Type* subtype   = nullptr; // DW_AT_type
101   simgrid::mc::Type* full_type = nullptr; // The same (but more complete) type
102 };
103
104 } // namespace simgrid::mc
105
106 #endif