Line data Source code
1 : use lief_ffi as ffi;
2 :
3 : use std::option::Option;
4 : use std::marker::PhantomData;
5 : use crate::{common::FromFFI, common::into_optional};
6 :
7 : use super::function::Function;
8 : use super::types::{Array, Base, EditorType, Enum, Pointer, Struct, Typedef};
9 : use super::types::Function as FunctionType;
10 : use super::types::struct_ty;
11 : use super::types::base;
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(self.ptr.pin_mut().create_structure(name, struct_ty::Kind::STRUCT.into()))
68 0 : }
69 :
70 : /// Create a structure type (`DW_TAG_class_type`)
71 0 : pub fn create_class(&mut self, name: &str) -> Struct {
72 0 : Struct::from_ffi(self.ptr.pin_mut().create_structure(name, struct_ty::Kind::CLASS.into()))
73 0 : }
74 :
75 : /// Create a union type (`DW_TAG_union_type`)
76 0 : pub fn create_union(&mut self, name: &str) -> Struct {
77 0 : Struct::from_ffi(self.ptr.pin_mut().create_structure(name, struct_ty::Kind::UNION.into()))
78 0 : }
79 :
80 : /// Create a primitive type with the given name and size.
81 0 : pub fn create_base_type(&mut self, name: &str, size: u64, encoding: base::Encoding) -> Base {
82 0 : Base::from_ffi(self.ptr.pin_mut().create_base_type(name, size, encoding.into()))
83 0 : }
84 :
85 : /// Create a function type with the given name.
86 0 : pub fn create_function_type(&mut self, name: &str) -> FunctionType {
87 0 : FunctionType::from_ffi(self.ptr.pin_mut().create_function_type(name))
88 0 : }
89 :
90 : /// Create a pointer on the provided type
91 0 : pub fn create_pointer_type(&mut self, ty: &dyn EditorType) -> Pointer {
92 0 : Pointer::from_ffi(self.ptr.pin_mut().create_pointer_type(ty.get_base()))
93 0 : }
94 :
95 : /// Create a `void` type
96 0 : pub fn create_void_type(&mut self) -> Type {
97 0 : Type::from_ffi(self.ptr.pin_mut().create_void_type())
98 0 : }
99 :
100 : /// Create an array type with the given name, type and size.
101 0 : pub fn create_array_type(&mut self, name: &str, ty:&dyn EditorType, count: u64) -> Array {
102 0 : Array::from_ffi(self.ptr.pin_mut().create_array_type(name, ty.get_base(), count))
103 0 : }
104 : }
|