swift_demangler/
autodiff.rs1use crate::helpers::{HasModule, NodeExt};
7use crate::raw::{Node, NodeKind};
8
9#[derive(Clone, Copy)]
11pub struct AutoDiff<'ctx> {
12 raw: Node<'ctx>,
13}
14
15impl<'ctx> AutoDiff<'ctx> {
16 pub fn new(raw: Node<'ctx>) -> Self {
18 Self { raw }
19 }
20
21 pub fn raw(&self) -> Node<'ctx> {
23 self.raw
24 }
25
26 pub fn kind(&self) -> AutoDiffKind {
28 match self.raw.kind() {
29 NodeKind::AutoDiffFunction => AutoDiffKind::Function,
30 NodeKind::DifferentiabilityWitness => AutoDiffKind::DifferentiabilityWitness,
31 NodeKind::AutoDiffDerivativeVTableThunk => AutoDiffKind::DerivativeVTableThunk,
32 NodeKind::AutoDiffSubsetParametersThunk => AutoDiffKind::SubsetParametersThunk,
33 NodeKind::AutoDiffSelfReorderingReabstractionThunk => {
34 AutoDiffKind::SelfReorderingReabstractionThunk
35 }
36 _ => AutoDiffKind::Other,
37 }
38 }
39
40 pub fn inner_function(&self) -> Option<crate::function::Function<'ctx>> {
42 self.raw
43 .child_of_kind(NodeKind::Function)
44 .map(crate::function::Function::new)
45 }
46
47 pub fn module(&self) -> Option<&'ctx str> {
49 if let Some(func) = self.inner_function() {
51 return func.module();
52 }
53 for node in self.raw.descendants() {
55 if node.kind() == NodeKind::Module {
56 return node.text();
57 }
58 }
59 None
60 }
61}
62
63impl std::fmt::Debug for AutoDiff<'_> {
64 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
65 f.debug_struct("AutoDiff")
66 .field("kind", &self.kind())
67 .field("inner_function", &self.inner_function())
68 .field("module", &self.module())
69 .finish()
70 }
71}
72
73impl std::fmt::Display for AutoDiff<'_> {
74 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
75 write!(f, "{}", self.raw)
76 }
77}
78
79#[derive(Debug, Clone, Copy, PartialEq, Eq)]
81pub enum AutoDiffKind {
82 Function,
84 DifferentiabilityWitness,
86 DerivativeVTableThunk,
88 SubsetParametersThunk,
90 SelfReorderingReabstractionThunk,
92 Other,
94}
95
96impl AutoDiffKind {
97 pub fn name(&self) -> &'static str {
99 match self {
100 AutoDiffKind::Function => "auto-diff function",
101 AutoDiffKind::DifferentiabilityWitness => "differentiability witness",
102 AutoDiffKind::DerivativeVTableThunk => "derivative vtable thunk",
103 AutoDiffKind::SubsetParametersThunk => "subset parameters thunk",
104 AutoDiffKind::SelfReorderingReabstractionThunk => "self-reordering reabstraction thunk",
105 AutoDiffKind::Other => "auto-diff symbol",
106 }
107 }
108}