Line data Source code
1 : use lief_ffi as ffi;
2 :
3 : use crate::{common::into_optional, common::FromFFI};
4 : use std::marker::PhantomData;
5 : use std::option::Option;
6 :
7 : use super::function::Function;
8 : use super::types::base;
9 : use super::types::struct_ty;
10 : use super::types::Function as FunctionType;
11 : use super::types::{Array, Base, EditorType, Enum, Pointer, Struct, Typedef};
12 : use super::variable::Variable;
13 : use super::Type;
14 :
15 : /// This structure represents an **editable** DWARF compilation unit
16 : pub struct CompilationUnit<'a> {
17 : ptr: cxx::UniquePtr<ffi::DWARF_editor_CompilationUnit>,
18 : _owner: PhantomData<&'a ()>,
19 : }
20 :
21 : impl FromFFI<ffi::DWARF_editor_CompilationUnit> for CompilationUnit<'_> {
22 0 : fn from_ffi(ptr: cxx::UniquePtr<ffi::DWARF_editor_CompilationUnit>) -> Self {
23 0 : Self {
24 0 : ptr,
25 0 : _owner: PhantomData,
26 0 : }
27 0 : }
28 : }
29 :
30 : impl CompilationUnit<'_> {
31 : /// Set the `DW_AT_producer` producer attribute.
32 : ///
33 : /// This attribute aims to inform about the program that generated this
34 : /// compilation unit (e.g. `LIEF Extended`)
35 0 : pub fn set_producer(&mut self, value: &str) {
36 0 : self.ptr.pin_mut().set_producer(value)
37 0 : }
38 :
39 : /// Create a new function owned by this compilation unit
40 0 : pub fn create_function(&mut self, name: &str) -> Option<Function> {
41 0 : into_optional(self.ptr.pin_mut().create_function(name))
42 0 : }
43 :
44 : /// Create a new **global** variable owned by this compilation unit
45 0 : pub fn create_variable(&mut self, name: &str) -> Option<Variable> {
46 0 : into_optional(self.ptr.pin_mut().create_variable(name))
47 0 : }
48 :
49 : /// Create a `DW_TAG_unspecified_type` type with the given name
50 0 : pub fn create_generic_type(&mut self, name: &str) -> Type {
51 0 : Type::from_ffi(self.ptr.pin_mut().create_generic_type(name))
52 0 : }
53 :
54 : /// Create an enum type (`DW_TAG_enumeration_type`)
55 0 : pub fn create_enum(&mut self, name: &str) -> Enum {
56 0 : Enum::from_ffi(self.ptr.pin_mut().create_enum(name))
57 0 : }
58 :
59 : /// Create a typdef with the name provided in the first parameter which aliases
60 : /// the type provided in the second parameter
61 0 : pub fn create_typedef(&mut self, name: &str, ty: &dyn EditorType) -> Typedef {
62 0 : Typedef::from_ffi(self.ptr.pin_mut().create_typedef(name, ty.get_base()))
63 0 : }
64 :
65 : /// Create a structure type (`DW_TAG_structure_type`)
66 0 : pub fn create_structure(&mut self, name: &str) -> Struct {
67 0 : Struct::from_ffi(
68 0 : self.ptr
69 0 : .pin_mut()
70 0 : .create_structure(name, struct_ty::Kind::STRUCT.into()),
71 0 : )
72 0 : }
73 :
74 : /// Create a structure type (`DW_TAG_class_type`)
75 0 : pub fn create_class(&mut self, name: &str) -> Struct {
76 0 : Struct::from_ffi(
77 0 : self.ptr
78 0 : .pin_mut()
79 0 : .create_structure(name, struct_ty::Kind::CLASS.into()),
80 0 : )
81 0 : }
82 :
83 : /// Create a union type (`DW_TAG_union_type`)
84 0 : pub fn create_union(&mut self, name: &str) -> Struct {
85 0 : Struct::from_ffi(
86 0 : self.ptr
87 0 : .pin_mut()
88 0 : .create_structure(name, struct_ty::Kind::UNION.into()),
89 0 : )
90 0 : }
91 :
92 : /// Create a primitive type with the given name and size.
93 0 : pub fn create_base_type(&mut self, name: &str, size: u64, encoding: base::Encoding) -> Base {
94 0 : Base::from_ffi(
95 0 : self.ptr
96 0 : .pin_mut()
97 0 : .create_base_type(name, size, encoding.into()),
98 0 : )
99 0 : }
100 :
101 : /// Create a function type with the given name.
102 0 : pub fn create_function_type(&mut self, name: &str) -> FunctionType {
103 0 : FunctionType::from_ffi(self.ptr.pin_mut().create_function_type(name))
104 0 : }
105 :
106 : /// Create a pointer on the provided type
107 0 : pub fn create_pointer_type(&mut self, ty: &dyn EditorType) -> Pointer {
108 0 : Pointer::from_ffi(self.ptr.pin_mut().create_pointer_type(ty.get_base()))
109 0 : }
110 :
111 : /// Create a `void` type
112 0 : pub fn create_void_type(&mut self) -> Type {
113 0 : Type::from_ffi(self.ptr.pin_mut().create_void_type())
114 0 : }
115 :
116 : /// Create an array type with the given name, type and size.
117 0 : pub fn create_array_type(&mut self, name: &str, ty: &dyn EditorType, count: u64) -> Array {
118 0 : Array::from_ffi(
119 0 : self.ptr
120 0 : .pin_mut()
121 0 : .create_array_type(name, ty.get_base(), count),
122 0 : )
123 0 : }
124 : }
|