LIEF: Library to Instrument Executable Formats Version 0.16.0
Loading...
Searching...
No Matches
DWARF/Type.hpp
Go to the documentation of this file.
1/* Copyright 2022 - 2024 R. Thomas
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#ifndef LIEF_DWARF_TYPE_H
16#define LIEF_DWARF_TYPE_H
17
18#include <memory>
19
20#include "LIEF/visibility.h"
21#include "LIEF/errors.hpp"
22#include "LIEF/debug_loc.hpp"
23#include "LIEF/canbe_unique.hpp"
24
25namespace LIEF {
26namespace dwarf {
27class Scope;
28
29namespace details {
30class Type;
31class TypeIt;
32}
33class LIEF_API Type {
65 public:
66 class LIEF_API Iterator {
67 public:
68 using iterator_category = std::bidirectional_iterator_tag;
69 using value_type = std::unique_ptr<Type>;
70 using difference_type = std::ptrdiff_t;
71 using pointer = Type*;
72 using reference = std::unique_ptr<Type>&;
73 using implementation = details::TypeIt;
74
75 class LIEF_API PointerProxy {
76 // Inspired from LLVM's iterator_facade_base
77 friend class Iterator;
78 public:
79 pointer operator->() const { return R.get(); }
80
81 private:
82 value_type R;
83
84 template <typename RefT>
85 PointerProxy(RefT &&R) : R(std::forward<RefT>(R)) {} // NOLINT(bugprone-forwarding-reference-overload)
86 };
87
88 Iterator(const Iterator&);
89 Iterator(Iterator&&) noexcept;
90 Iterator(std::unique_ptr<details::TypeIt> impl);
91 ~Iterator();
92
93 friend LIEF_API bool operator==(const Iterator& LHS, const Iterator& RHS);
94 friend LIEF_API bool operator!=(const Iterator& LHS, const Iterator& RHS) {
95 return !(LHS == RHS);
96 }
97
98 Iterator& operator++();
99 Iterator& operator--();
100
101 Iterator operator--(int) {
102 Iterator tmp = *static_cast<Iterator*>(this);
103 --*static_cast<Iterator *>(this);
104 return tmp;
105 }
106
107 Iterator operator++(int) {
108 Iterator tmp = *static_cast<Iterator*>(this);
109 ++*static_cast<Iterator *>(this);
110 return tmp;
111 }
112
113 std::unique_ptr<Type> operator*() const;
114
115 PointerProxy operator->() const {
116 return static_cast<const Iterator*>(this)->operator*();
117 }
118
119 private:
120 std::unique_ptr<details::TypeIt> impl_;
121 };
122
123 virtual ~Type();
124
125 enum class KIND {
126 UNKNOWN = 0,
127 UNSPECIFIED,
128 BASE,
129 CONST_KIND,
130 CLASS,
131 ARRAY,
132 POINTER,
133 STRUCT,
134 UNION,
135 TYPEDEF,
136 REF,
137 SET_TYPE,
138 STRING,
139 SUBROUTINE,
140 POINTER_MEMBER,
141 PACKED,
142 FILE,
143 THROWN,
144 VOLATILE,
145 RESTRICT,
146 INTERFACE,
147 SHARED,
148 RVALREF,
149 TEMPLATE_ALIAS,
150 COARRAY,
151 DYNAMIC,
152 ATOMIC,
153 IMMUTABLE,
154 ENUM,
155 };
156
157 KIND kind() const;
158 bool is_unspecified() const {
161 return kind() == KIND::UNSPECIFIED;
162 }
163 result<std::string> name() const;
167 result<uint64_t> size() const;
172 debug_location_t location() const;
175 std::unique_ptr<Scope> scope() const;
178
179 template<class T>
180 const T* as() const {
181 if (T::classof(this)) {
182 return static_cast<const T*>(this);
183 }
184 return nullptr;
185 }
186
187 static std::unique_ptr<Type> create(std::unique_ptr<details::Type> impl);
188
189 protected:
190 Type(std::unique_ptr<details::Type> impl);
191 Type(details::Type& impl);
192
193 LIEF::details::canbe_unique<details::Type> impl_;
194};
195
196}
197}
198#endif
This class materializes a scope in which Function, Variable, Type, ... can be defined.
Definition Scope.hpp:32
pointer operator->() const
Definition DWARF/Type.hpp:79
PointerProxy operator->() const
Definition DWARF/Type.hpp:115
Iterator operator--(int)
Definition DWARF/Type.hpp:101
Iterator operator++(int)
Definition DWARF/Type.hpp:107
Iterator & operator++()
Iterator(const Iterator &)
Iterator(Iterator &&) noexcept
std::unique_ptr< Type > operator*() const
Iterator & operator--()
This class represents a DWARF Type which includes:
Definition DWARF/Type.hpp:64
const T * as() const
Definition DWARF/Type.hpp:180
bool is_unspecified() const
Whether this type is a DW_TAG_unspecified_type
Definition DWARF/Type.hpp:160
KIND kind() const
result< std::string > name() const
Return the type's name using either DW_AT_name or DW_AT_picture_string (if any).
result< uint64_t > size() const
Return the size of the type or an error if it can't be computed.
virtual ~Type()
debug_location_t location() const
Return the debug location where this type is defined.
static std::unique_ptr< Type > create(std::unique_ptr< details::Type > impl)
std::unique_ptr< Scope > scope() const
Return the scope in which this type is defined.
Definition DWARF/CompilationUnit.hpp:30
Namespace for the DWARF debug format.
Definition DWARF/CompilationUnit.hpp:28
LIEF namespace.
Definition Abstract/Binary.hpp:36
#define LIEF_API
Definition visibility.h:41