cairo_lang_lowering/graph_algorithms/
concrete_function_node.rs

1use cairo_lang_utils::graph_algos::graph_node::GraphNode;
2use cairo_lang_utils::graph_algos::strongly_connected_components::ComputeScc;
3
4use super::strongly_connected_components::concrete_function_with_body_scc;
5use crate::DependencyType;
6use crate::db::LoweringGroup;
7use crate::ids::ConcreteFunctionWithBodyId;
8
9/// A node to use in graph-algorithms.
10#[derive(Clone)]
11pub struct ConcreteFunctionWithBodyNode<'a> {
12    pub function_id: ConcreteFunctionWithBodyId,
13    pub db: &'a dyn LoweringGroup,
14    pub dependency_type: DependencyType,
15}
16impl GraphNode for ConcreteFunctionWithBodyNode<'_> {
17    type NodeId = ConcreteFunctionWithBodyId;
18
19    fn get_neighbors(&self) -> Vec<Self> {
20        let Ok(direct_callees) = self.db.concrete_function_with_body_direct_callees_with_body(
21            self.function_id,
22            self.dependency_type,
23        ) else {
24            return vec![];
25        };
26        direct_callees
27            .into_iter()
28            .map(|callee| ConcreteFunctionWithBodyNode {
29                function_id: callee,
30                db: self.db,
31                dependency_type: self.dependency_type,
32            })
33            .collect()
34    }
35
36    fn get_id(&self) -> Self::NodeId {
37        self.function_id
38    }
39}
40impl ComputeScc for ConcreteFunctionWithBodyNode<'_> {
41    fn compute_scc(&self) -> Vec<Self::NodeId> {
42        concrete_function_with_body_scc(self.db, self.function_id, self.dependency_type)
43    }
44}
45
46#[derive(Clone)]
47pub struct ConcreteFunctionWithBodyInlinedNode<'a> {
48    pub function_id: ConcreteFunctionWithBodyId,
49    pub db: &'a dyn LoweringGroup,
50    pub dependency_type: DependencyType,
51}
52impl GraphNode for ConcreteFunctionWithBodyInlinedNode<'_> {
53    type NodeId = ConcreteFunctionWithBodyId;
54
55    fn get_neighbors(&self) -> Vec<Self> {
56        let Ok(direct_callees) =
57            self.db.concrete_function_with_body_inlined_direct_callees_with_body(
58                self.function_id,
59                self.dependency_type,
60            )
61        else {
62            return vec![];
63        };
64        direct_callees
65            .into_iter()
66            .map(|callee| ConcreteFunctionWithBodyInlinedNode {
67                function_id: callee,
68                db: self.db,
69                dependency_type: self.dependency_type,
70            })
71            .collect()
72    }
73
74    fn get_id(&self) -> Self::NodeId {
75        self.function_id
76    }
77}
78impl ComputeScc for ConcreteFunctionWithBodyInlinedNode<'_> {
79    fn compute_scc(&self) -> Vec<Self::NodeId> {
80        self.db.concrete_function_with_body_inlined_scc(self.function_id, self.dependency_type)
81    }
82}