1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
use crate::{
arena::{Arena, Handle},
proc::{Interface, Visitor},
Function,
};
use petgraph::{
graph::{DefaultIx, NodeIndex},
Graph,
};
pub type CallGraph = Graph<Handle<Function>, ()>;
pub struct CallGraphBuilder<'a> {
pub functions: &'a Arena<Function>,
}
impl<'a> CallGraphBuilder<'a> {
pub fn process(&self, func: &Function) -> CallGraph {
let mut graph = Graph::new();
let mut children = Vec::new();
let visitor = CallGraphVisitor {
children: &mut children,
};
let mut interface = Interface {
expressions: &func.expressions,
local_variables: &func.local_variables,
visitor,
};
interface.traverse(&func.body);
for handle in children {
let id = graph.add_node(handle);
self.collect(handle, id, &mut graph);
}
graph
}
fn collect(&self, handle: Handle<Function>, id: NodeIndex<DefaultIx>, graph: &mut CallGraph) {
let mut children = Vec::new();
let visitor = CallGraphVisitor {
children: &mut children,
};
let func = &self.functions[handle];
let mut interface = Interface {
expressions: &func.expressions,
local_variables: &func.local_variables,
visitor,
};
interface.traverse(&func.body);
for handle in children {
let child_id = graph.add_node(handle);
graph.add_edge(id, child_id, ());
self.collect(handle, child_id, graph);
}
}
}
struct CallGraphVisitor<'a> {
children: &'a mut Vec<Handle<Function>>,
}
impl<'a> Visitor for CallGraphVisitor<'a> {
fn visit_fun(&mut self, func: Handle<Function>) {
self.children.push(func)
}
}
|