Line data Source code
1 : use lief_ffi as ffi;
2 :
3 : pub mod pointer;
4 : pub mod array;
5 : pub mod base;
6 : pub mod enum_ty;
7 : pub mod function;
8 : pub mod struct_ty;
9 : pub mod typedef;
10 :
11 : use crate::common::FromFFI;
12 :
13 : #[doc(inline)]
14 : pub use pointer::Pointer;
15 :
16 : #[doc(inline)]
17 : pub use array::Array;
18 :
19 : #[doc(inline)]
20 : pub use base::Base;
21 :
22 : #[doc(inline)]
23 : pub use enum_ty::Enum;
24 :
25 : #[doc(inline)]
26 : pub use function::Function;
27 :
28 : #[doc(inline)]
29 : pub use struct_ty::Struct;
30 :
31 : #[doc(inline)]
32 : pub use typedef::Typedef;
33 :
34 : /// The different types supported by the editor interface
35 : pub enum Type {
36 : /// Mirror `DW_TAG_pointer_type`
37 : Pointer(Pointer),
38 :
39 : /// Mirror `DW_TAG_array_type`
40 : Array(Array),
41 :
42 : /// Mirror `DW_TAG_base_type`
43 : Base(Base),
44 :
45 : /// Mirror `DW_TAG_enumeration_type`
46 : Enum(Enum),
47 :
48 : /// Mirror `DW_TAG_subroutine_type`
49 : Function(Function),
50 :
51 : /// Mirror `DW_TAG_class_type, DW_TAG_structure_type, DW_TAG_union_type`
52 : Struct(Struct),
53 :
54 : /// Mirror `DW_TAG_typedef`
55 : Typedef(Typedef),
56 :
57 : Generic(Generic),
58 : }
59 :
60 : impl FromFFI<ffi::DWARF_editor_Type> for Type {
61 0 : fn from_ffi(ffi_entry: cxx::UniquePtr<ffi::DWARF_editor_Type>) -> Self {
62 0 : unsafe {
63 0 : let type_ref = ffi_entry.as_ref().unwrap();
64 0 :
65 0 : if ffi::DWARF_editor_PointerType::classof(type_ref) {
66 0 : let raw = {
67 0 : type From = cxx::UniquePtr<ffi::DWARF_editor_Type>;
68 0 : type To = cxx::UniquePtr<ffi::DWARF_editor_PointerType>;
69 0 : std::mem::transmute::<From, To>(ffi_entry)
70 0 : };
71 0 : Type::Pointer(Pointer::from_ffi(raw))
72 0 : } else if ffi::DWARF_editor_ArrayType::classof(type_ref) {
73 0 : let raw = {
74 0 : type From = cxx::UniquePtr<ffi::DWARF_editor_Type>;
75 0 : type To = cxx::UniquePtr<ffi::DWARF_editor_ArrayType>;
76 0 : std::mem::transmute::<From, To>(ffi_entry)
77 0 : };
78 0 : Type::Array(Array::from_ffi(raw))
79 0 : } else if ffi::DWARF_editor_BaseType::classof(type_ref) {
80 0 : let raw = {
81 0 : type From = cxx::UniquePtr<ffi::DWARF_editor_Type>;
82 0 : type To = cxx::UniquePtr<ffi::DWARF_editor_BaseType>;
83 0 : std::mem::transmute::<From, To>(ffi_entry)
84 0 : };
85 0 : Type::Base(Base::from_ffi(raw))
86 0 : } else if ffi::DWARF_editor_EnumType::classof(type_ref) {
87 0 : let raw = {
88 0 : type From = cxx::UniquePtr<ffi::DWARF_editor_Type>;
89 0 : type To = cxx::UniquePtr<ffi::DWARF_editor_EnumType>;
90 0 : std::mem::transmute::<From, To>(ffi_entry)
91 0 : };
92 0 : Type::Enum(Enum::from_ffi(raw))
93 0 : } else if ffi::DWARF_editor_FunctionType::classof(type_ref) {
94 0 : let raw = {
95 0 : type From = cxx::UniquePtr<ffi::DWARF_editor_Type>;
96 0 : type To = cxx::UniquePtr<ffi::DWARF_editor_FunctionType>;
97 0 : std::mem::transmute::<From, To>(ffi_entry)
98 0 : };
99 0 : Type::Function(Function::from_ffi(raw))
100 0 : } else if ffi::DWARF_editor_StructType::classof(type_ref) {
101 0 : let raw = {
102 0 : type From = cxx::UniquePtr<ffi::DWARF_editor_Type>;
103 0 : type To = cxx::UniquePtr<ffi::DWARF_editor_StructType>;
104 0 : std::mem::transmute::<From, To>(ffi_entry)
105 0 : };
106 0 : Type::Struct(Struct::from_ffi(raw))
107 0 : } else if ffi::DWARF_editor_TypeDef::classof(type_ref) {
108 0 : let raw = {
109 0 : type From = cxx::UniquePtr<ffi::DWARF_editor_Type>;
110 0 : type To = cxx::UniquePtr<ffi::DWARF_editor_TypeDef>;
111 0 : std::mem::transmute::<From, To>(ffi_entry)
112 0 : };
113 0 : Type::Typedef(Typedef::from_ffi(raw))
114 : } else {
115 0 : Type::Generic(Generic::from_ffi(ffi_entry))
116 : }
117 : }
118 0 : }
119 : }
120 :
121 : /// Generic trait shared by all DWARF editor types
122 : pub trait EditorType {
123 : #[doc(hidden)]
124 : fn get_base(&self) -> &ffi::DWARF_editor_Type;
125 :
126 0 : fn pointer_to(&self) -> Pointer {
127 0 : Pointer::from_ffi(self.get_base().pointer_to())
128 0 : }
129 : }
130 :
131 : /// This structure represents a generic type (like `DW_TAG_unspecified_type`)
132 : pub struct Generic {
133 : ptr: cxx::UniquePtr<ffi::DWARF_editor_Type>,
134 : }
135 :
136 : impl FromFFI<ffi::DWARF_editor_Type> for Generic {
137 0 : fn from_ffi(cmd: cxx::UniquePtr<ffi::DWARF_editor_Type>) -> Self {
138 0 : Self {
139 0 : ptr: cmd,
140 0 : }
141 0 : }
142 : }
143 :
144 : impl EditorType for Generic {
145 0 : fn get_base(&self) -> &ffi::DWARF_editor_Type {
146 0 : self.ptr.as_ref().unwrap()
147 0 : }
148 : }
149 :
150 : impl EditorType for Type {
151 0 : fn get_base(&self) -> &ffi::DWARF_editor_Type {
152 0 : match &self {
153 0 : Type::Pointer(s) => {
154 0 : s.get_base()
155 : }
156 0 : Type::Array(s) => {
157 0 : s.get_base()
158 : }
159 0 : Type::Base(s) => {
160 0 : s.get_base()
161 : }
162 0 : Type::Enum(s) => {
163 0 : s.get_base()
164 : }
165 0 : Type::Function(s) => {
166 0 : s.get_base()
167 : }
168 0 : Type::Struct(s) => {
169 0 : s.get_base()
170 : }
171 0 : Type::Typedef(s) => {
172 0 : s.get_base()
173 : }
174 0 : Type::Generic(s) => {
175 0 : s.get_base()
176 : }
177 : }
178 0 : }
179 : }
180 :
|