Logo AND Algorithmique Numérique Distribuée

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