Line data Source code
1 : use lief_ffi as ffi;
2 :
3 : use crate::common::FromFFI;
4 : use crate::dwarf::editor::types::EditorType;
5 :
6 : /// This structure represents a struct-like type which can be:
7 : ///
8 : /// - `DW_TAG_class_type`
9 : /// - `DW_TAG_structure_type`
10 : /// - `DW_TAG_union_type`
11 : pub struct Struct {
12 : ptr: cxx::UniquePtr<ffi::DWARF_editor_StructType>,
13 : }
14 :
15 : impl FromFFI<ffi::DWARF_editor_StructType> for Struct {
16 0 : fn from_ffi(cmd: cxx::UniquePtr<ffi::DWARF_editor_StructType>) -> Self {
17 0 : Self {
18 0 : ptr: cmd,
19 0 : }
20 0 : }
21 : }
22 :
23 : #[allow(non_camel_case_types)]
24 0 : #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
25 : pub enum Kind {
26 : CLASS,
27 : STRUCT,
28 : UNION,
29 : UNKNOWN(u32),
30 : }
31 :
32 : impl From<u32> for Kind {
33 0 : fn from(value: u32) -> Self {
34 0 : match value {
35 0 : 0x00000000 => Kind::CLASS,
36 0 : 0x00000001 => Kind::STRUCT,
37 0 : 0x00000002 => Kind::UNION,
38 0 : _ => Kind::UNKNOWN(value),
39 :
40 : }
41 0 : }
42 : }
43 :
44 : impl From<Kind> for u32 {
45 0 : fn from(value: Kind) -> Self {
46 0 : match value {
47 0 : Kind::CLASS => 0,
48 0 : Kind::STRUCT => 1,
49 0 : Kind::UNION => 2,
50 0 : Kind::UNKNOWN(value) => value,
51 : }
52 0 : }
53 : }
54 :
55 : impl Struct {
56 : /// Define the overall size which is equivalent to the `sizeof` of the current
57 : /// type.
58 : ///
59 : /// This function defines the `DW_AT_byte_size` attribute
60 0 : pub fn set_size(&mut self, size: u64) {
61 0 : self.ptr.pin_mut().set_size(size);
62 0 : }
63 :
64 : /// Add a member to the current struct-like
65 0 : pub fn add_member(&mut self, name: &str, ty: &mut dyn EditorType) -> Member {
66 0 : Member::from_ffi(self.ptr.pin_mut().add_member(name, ty.get_base()))
67 0 : }
68 :
69 : /// Add a member to the current struct-like at the specified offset
70 0 : pub fn add_member_at_offset(&mut self, name: &str, ty: &mut dyn EditorType, offset: u64) -> Member {
71 0 : Member::from_ffi(self.ptr.pin_mut().add_member_with_offset(name, ty.get_base(), offset))
72 0 : }
73 : }
74 :
75 : impl EditorType for Struct {
76 0 : fn get_base(&self) -> &ffi::DWARF_editor_Type {
77 0 : self.ptr.as_ref().unwrap().as_ref()
78 0 : }
79 : }
80 :
81 : pub struct Member {
82 : ptr: cxx::UniquePtr<ffi::DWARF_editor_StructType_Member>,
83 : }
84 :
85 : impl FromFFI<ffi::DWARF_editor_StructType_Member> for Member {
86 0 : fn from_ffi(cmd: cxx::UniquePtr<ffi::DWARF_editor_StructType_Member>) -> Self {
87 0 : Self {
88 0 : ptr: cmd,
89 0 : }
90 0 : }
91 : }
92 :
93 :
|