use lief_ffi as ffi;
use crate::common::FromFFI;
use crate::common::into_optional;
use std::marker::PhantomData;
pub struct Scope<'a> {
ptr: cxx::UniquePtr<ffi::DWARF_Scope>,
_owner: PhantomData<&'a ()>,
}
#[allow(non_camel_case_types)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Type {
UNION,
CLASS,
STRUCT,
NAMESPACE,
FUNCTION,
COMPILATION_UNIT,
UNKNOWN(u32),
}
impl From<u32> for Type {
fn from(value: u32) -> Self {
match value {
0x00000001 => Type::UNION,
0x00000002 => Type::CLASS,
0x00000003 => Type::STRUCT,
0x00000004 => Type::NAMESPACE,
0x00000005 => Type::FUNCTION,
0x00000006 => Type::COMPILATION_UNIT,
_ => Type::UNKNOWN(value),
}
}
}
impl From<Type> for u32 {
fn from(value: Type) -> u32 {
match value {
Type::UNION => 0x00000001,
Type::CLASS => 0x00000002,
Type::STRUCT => 0x00000003,
Type::NAMESPACE => 0x00000004,
Type::FUNCTION => 0x00000005,
Type::COMPILATION_UNIT => 0x00000006,
Type::UNKNOWN(_) => 0,
}
}
}
impl FromFFI<ffi::DWARF_Scope> for Scope<'_> {
fn from_ffi(ptr: cxx::UniquePtr<ffi::DWARF_Scope>) -> Self {
Self {
ptr,
_owner: PhantomData,
}
}
}
impl Scope<'_> {
pub fn name(&self) -> String {
self.ptr.name().to_string()
}
pub fn parent(&self) -> Option<Scope<'_>> {
into_optional(self.ptr.parent())
}
pub fn get_type(&self) -> Type {
Type::from(self.ptr.get_type())
}
pub fn chained(&self, sep: &str) -> String {
cxx::let_cxx_string!(__cxx_s = sep);
self.ptr.chained(&__cxx_s).to_string()
}
}