use lief_ffi as ffi;
use crate::{common::FromFFI, common::into_optional};
use std::marker::PhantomData;
use std::option::Option;
use super::Type;
use super::function::Function;
use super::types::Function as FunctionType;
use super::types::base;
use super::types::struct_ty;
use super::types::{Array, Base, EditorType, Enum, Pointer, Struct, Typedef};
use super::variable::Variable;
pub struct CompilationUnit<'a> {
ptr: cxx::UniquePtr<ffi::DWARF_editor_CompilationUnit>,
_owner: PhantomData<&'a ()>,
}
impl FromFFI<ffi::DWARF_editor_CompilationUnit> for CompilationUnit<'_> {
fn from_ffi(ptr: cxx::UniquePtr<ffi::DWARF_editor_CompilationUnit>) -> Self {
Self {
ptr,
_owner: PhantomData,
}
}
}
impl CompilationUnit<'_> {
pub fn set_producer(&mut self, value: &str) {
cxx::let_cxx_string!(__cxx_s = value);
self.ptr.pin_mut().set_producer(&__cxx_s)
}
pub fn create_function(&mut self, name: &str) -> Option<Function> {
cxx::let_cxx_string!(__cxx_s = name);
into_optional(self.ptr.pin_mut().create_function(&__cxx_s))
}
pub fn create_variable(&mut self, name: &str) -> Option<Variable> {
cxx::let_cxx_string!(__cxx_s = name);
into_optional(self.ptr.pin_mut().create_variable(&__cxx_s))
}
pub fn create_generic_type(&mut self, name: &str) -> Type {
cxx::let_cxx_string!(__cxx_s = name);
Type::from_ffi(self.ptr.pin_mut().create_generic_type(&__cxx_s))
}
pub fn create_enum(&mut self, name: &str) -> Enum {
cxx::let_cxx_string!(__cxx_s = name);
Enum::from_ffi(self.ptr.pin_mut().create_enum(&__cxx_s))
}
pub fn create_typedef(&mut self, name: &str, ty: &dyn EditorType) -> Typedef {
cxx::let_cxx_string!(__cxx_s = name);
Typedef::from_ffi(self.ptr.pin_mut().create_typedef(&__cxx_s, ty.get_base()))
}
pub fn create_structure(&mut self, name: &str) -> Struct {
cxx::let_cxx_string!(__cxx_s = name);
Struct::from_ffi(
self.ptr
.pin_mut()
.create_structure(&__cxx_s, struct_ty::Kind::STRUCT.into()),
)
}
pub fn create_class(&mut self, name: &str) -> Struct {
cxx::let_cxx_string!(__cxx_s = name);
Struct::from_ffi(
self.ptr
.pin_mut()
.create_structure(&__cxx_s, struct_ty::Kind::CLASS.into()),
)
}
pub fn create_union(&mut self, name: &str) -> Struct {
cxx::let_cxx_string!(__cxx_s = name);
Struct::from_ffi(
self.ptr
.pin_mut()
.create_structure(&__cxx_s, struct_ty::Kind::UNION.into()),
)
}
pub fn create_base_type(&mut self, name: &str, size: u64, encoding: base::Encoding) -> Base {
cxx::let_cxx_string!(__cxx_s = name);
Base::from_ffi(
self.ptr
.pin_mut()
.create_base_type(&__cxx_s, size, encoding.into()),
)
}
pub fn create_function_type(&mut self, name: &str) -> FunctionType {
cxx::let_cxx_string!(__cxx_s = name);
FunctionType::from_ffi(self.ptr.pin_mut().create_function_type(&__cxx_s))
}
pub fn create_pointer_type(&mut self, ty: &dyn EditorType) -> Pointer {
Pointer::from_ffi(self.ptr.pin_mut().create_pointer_type(ty.get_base()))
}
pub fn create_void_type(&mut self) -> Type {
Type::from_ffi(self.ptr.pin_mut().create_void_type())
}
pub fn create_array_type(&mut self, name: &str, ty: &dyn EditorType, count: u64) -> Array {
cxx::let_cxx_string!(__cxx_s = name);
Array::from_ffi(
self.ptr
.pin_mut()
.create_array_type(&__cxx_s, ty.get_base(), count),
)
}
}