Line data Source code
1 : use lief_ffi as ffi;
2 :
3 : use crate::common::into_optional;
4 : use crate::common::FromFFI;
5 : use std::marker::PhantomData;
6 :
7 : /// This class materializes a scope in which Function, Variable, Type, ...
8 : /// can be defined.
9 : pub struct Scope<'a> {
10 : ptr: cxx::UniquePtr<ffi::DWARF_Scope>,
11 : _owner: PhantomData<&'a ()>,
12 : }
13 :
14 : #[allow(non_camel_case_types)]
15 0 : #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
16 : pub enum Type {
17 : UNION,
18 : CLASS,
19 : STRUCT,
20 : NAMESPACE,
21 : FUNCTION,
22 : COMPILATION_UNIT,
23 : UNKNOWN(u32),
24 : }
25 :
26 : impl From<u32> for Type {
27 0 : fn from(value: u32) -> Self {
28 0 : match value {
29 0 : 0x00000001 => Type::UNION,
30 0 : 0x00000002 => Type::CLASS,
31 0 : 0x00000003 => Type::STRUCT,
32 0 : 0x00000004 => Type::NAMESPACE,
33 0 : 0x00000005 => Type::FUNCTION,
34 0 : 0x00000006 => Type::COMPILATION_UNIT,
35 0 : _ => Type::UNKNOWN(value),
36 :
37 : }
38 0 : }
39 : }
40 : impl From<Type> for u32 {
41 0 : fn from(value: Type) -> u32 {
42 0 : match value {
43 0 : Type::UNION => 0x00000001,
44 0 : Type::CLASS => 0x00000002,
45 0 : Type::STRUCT => 0x00000003,
46 0 : Type::NAMESPACE => 0x00000004,
47 0 : Type::FUNCTION => 0x00000005,
48 0 : Type::COMPILATION_UNIT => 0x00000006,
49 0 : Type::UNKNOWN(_) => 0,
50 :
51 : }
52 0 : }
53 : }
54 :
55 : impl FromFFI<ffi::DWARF_Scope> for Scope<'_> {
56 0 : fn from_ffi(ptr: cxx::UniquePtr<ffi::DWARF_Scope>) -> Self {
57 0 : Self {
58 0 : ptr,
59 0 : _owner: PhantomData,
60 0 : }
61 0 : }
62 : }
63 :
64 : impl Scope<'_> {
65 : /// Name of the scope. For instance namespace's name or function's name.
66 0 : pub fn name(&self) -> String {
67 0 : self.ptr.name().to_string()
68 0 : }
69 :
70 : /// Parent scope (if any)
71 0 : pub fn parent(&self) -> Option<Scope> {
72 0 : into_optional(self.ptr.parent())
73 0 : }
74 :
75 : /// The current scope type
76 0 : pub fn get_type(&self) -> Type {
77 0 : Type::from(self.ptr.get_type())
78 0 : }
79 :
80 : /// Represent the whole chain of all (parent) scopes using the provided
81 : /// separator. E.g. `ns1::ns2::Class1::Struct2::Type`
82 0 : pub fn chained(&self, sep: &str) -> String {
83 0 : self.ptr.chained(sep).to_string()
84 0 : }
85 : }
86 :
|