use lief_ffi as ffi;
use crate::common::{FromFFI, into_optional};
use crate::dwarf::editor::Variable;
use crate::dwarf::editor::types::EditorType;
pub struct Function {
ptr: cxx::UniquePtr<ffi::DWARF_editor_Function>,
}
impl FromFFI<ffi::DWARF_editor_Function> for Function {
fn from_ffi(ptr: cxx::UniquePtr<ffi::DWARF_editor_Function>) -> Self {
Self { ptr }
}
}
pub struct Range {
start: u64,
end: u64,
}
pub struct Parameter {
ptr: cxx::UniquePtr<ffi::DWARF_editor_Function_Parameter>,
}
impl Parameter {
pub fn assign_register_by_name(&mut self, name: &str) -> &mut Self {
cxx::let_cxx_string!(__cxx_s = name);
self.ptr.pin_mut().assign_register_by_name(&__cxx_s);
self
}
pub fn assign_register_by_id(&mut self, id: u64) -> &mut Self {
self.ptr.pin_mut().assign_register_by_id(id);
self
}
}
impl FromFFI<ffi::DWARF_editor_Function_Parameter> for Parameter {
fn from_ffi(ptr: cxx::UniquePtr<ffi::DWARF_editor_Function_Parameter>) -> Self {
Self { ptr }
}
}
pub struct LexicalBlock {
ptr: cxx::UniquePtr<ffi::DWARF_editor_Function_LexicalBlock>,
}
impl FromFFI<ffi::DWARF_editor_Function_LexicalBlock> for LexicalBlock {
fn from_ffi(ptr: cxx::UniquePtr<ffi::DWARF_editor_Function_LexicalBlock>) -> Self {
Self { ptr }
}
}
impl LexicalBlock {
pub fn add_name(&mut self, name: &str) -> &mut Self {
cxx::let_cxx_string!(__cxx_s = name);
self.ptr.pin_mut().add_name(&__cxx_s);
self
}
pub fn add_description(&mut self, description: &str) -> &mut Self {
cxx::let_cxx_string!(__cxx_s = description);
self.ptr.pin_mut().add_description(&__cxx_s);
self
}
pub fn add_block(&mut self, start: u64, end: u64) -> Option<LexicalBlock> {
into_optional(self.ptr.pin_mut().add_block(start, end))
}
pub fn add_block_from_range(&mut self, ranges: &[Range]) -> Option<LexicalBlock> {
let mut ffi_ranges = cxx::CxxVector::new();
for range in ranges {
let ffi_range = ffi::DWARF_editor_Function_Range {
start: range.start,
end: range.end,
};
ffi_ranges.as_mut().unwrap().push(ffi_range);
}
into_optional(self.ptr.pin_mut().add_block_from_range(&ffi_ranges))
}
}
#[allow(dead_code)]
pub struct Label {
ptr: cxx::UniquePtr<ffi::DWARF_editor_Function_Label>,
}
impl FromFFI<ffi::DWARF_editor_Function_Label> for Label {
fn from_ffi(ptr: cxx::UniquePtr<ffi::DWARF_editor_Function_Label>) -> Self {
Self { ptr }
}
}
impl Function {
pub fn set_address(&mut self, addr: u64) -> &mut Self {
self.ptr.pin_mut().set_address(addr);
self
}
pub fn set_low_high(&mut self, low: u64, high: u64) -> &mut Self {
self.ptr.pin_mut().set_low_high(low, high);
self
}
pub fn set_ranges(&mut self, ranges: &[Range]) -> &mut Self {
let mut ffi_ranges = cxx::CxxVector::new();
for range in ranges {
let ffi_range = ffi::DWARF_editor_Function_Range {
start: range.start,
end: range.end,
};
ffi_ranges.as_mut().unwrap().push(ffi_range);
}
self.ptr.pin_mut().set_ranges(&ffi_ranges);
self
}
pub fn set_external(&mut self) -> &mut Self {
self.ptr.pin_mut().set_external();
self
}
pub fn set_return_type(&mut self, ty: &dyn EditorType) -> &mut Self {
self.ptr.pin_mut().set_return_type(ty.get_base());
self
}
pub fn add_parameter(&mut self, name: &str, ty: &dyn EditorType) -> Parameter {
cxx::let_cxx_string!(__cxx_s = name);
Parameter::from_ffi(self.ptr.pin_mut().add_parameter(&__cxx_s, ty.get_base()))
}
pub fn create_stack_variable(&mut self, name: &str) -> Variable {
cxx::let_cxx_string!(__cxx_s = name);
Variable::from_ffi(self.ptr.pin_mut().create_stack_variable(&__cxx_s))
}
pub fn add_lexical_block(&mut self, start: u64, end: u64) -> LexicalBlock {
LexicalBlock::from_ffi(self.ptr.pin_mut().add_lexical_block(start, end))
}
pub fn add_label(&mut self, addr: u64, name: &str) -> Label {
cxx::let_cxx_string!(__cxx_name = name);
Label::from_ffi(self.ptr.pin_mut().add_label(addr, &__cxx_name))
}
pub fn add_description(&mut self, description: &str) -> &mut Self {
cxx::let_cxx_string!(__cxx_s = description);
self.ptr.pin_mut().add_description(&__cxx_s);
self
}
}