|
| 1 | +use rustc_middle::ty::TyCtxt; |
| 2 | +use rustc_span::source_map::SourceMap; |
1 | 3 | use serde::Serialize; |
2 | | -use stable_mir::{CrateDef, DefId}; |
| 4 | +use stable_mir::{CrateDef, DefId, mir::mono::Instance}; |
3 | 5 |
|
4 | 6 | /// A Rust funtion with its file source, attributes, and raw function content. |
5 | 7 | #[derive(Debug, Serialize)] |
6 | 8 | pub struct SerFunction { |
7 | 9 | /// DefId in stable_mir. |
8 | | - pub def_id: String, |
| 10 | + def_id: String, |
9 | 11 | /// Every funtion must be declared in a specific file, even for those |
10 | 12 | /// generated by macros. |
11 | | - pub file: String, |
| 13 | + file: String, |
12 | 14 | /// Attributes are attached the function, but it seems that attributes |
13 | 15 | /// and function must be separated to query. |
14 | | - pub attrs: Vec<String>, |
| 16 | + attrs: Vec<String>, |
15 | 17 | /// Raw function string, including name, signature, and body. |
16 | | - pub func: String, |
| 18 | + func: String, |
17 | 19 | /// Recursive fnction calls inside the body. |
18 | | - pub callees: Vec<String>, |
| 20 | + callees: Vec<Callee>, |
19 | 21 | } |
20 | 22 |
|
21 | 23 | impl SerFunction { |
22 | | - pub fn new(fun: super::Function) -> Self { |
| 24 | + pub fn new(fun: super::Function, tcx: TyCtxt, src_map: &SourceMap) -> Self { |
23 | 25 | SerFunction { |
24 | 26 | def_id: format_def_id(&fun.def_id), |
25 | 27 | file: fun.file, |
26 | 28 | attrs: fun.attrs.iter().map(|a| a.as_str().to_owned()).collect(), |
27 | 29 | func: fun.func, |
28 | | - callees: fun.callees.into_iter().map(|x| format_def_id(&x.def.def_id())).collect(), |
| 30 | + callees: fun.callees.iter().map(|x| Callee::new(x, tcx, src_map)).collect(), |
29 | 31 | } |
30 | 32 | } |
31 | 33 | } |
32 | 34 |
|
33 | 35 | fn format_def_id(def_id: &DefId) -> String { |
34 | 36 | format!("{def_id:?}") |
35 | 37 | } |
| 38 | + |
| 39 | +#[derive(Debug, Serialize)] |
| 40 | +pub struct Callee { |
| 41 | + def_id: String, |
| 42 | + func: String, |
| 43 | +} |
| 44 | + |
| 45 | +impl Callee { |
| 46 | + fn new(inst: &Instance, tcx: TyCtxt, src_map: &SourceMap) -> Self { |
| 47 | + let def_id = format_def_id(&inst.def.def_id()); |
| 48 | + let func = inst |
| 49 | + .body() |
| 50 | + .map(|body| super::source_code_with(body.span, tcx, src_map)) |
| 51 | + .unwrap_or_default(); |
| 52 | + Callee { def_id, func } |
| 53 | + } |
| 54 | +} |
0 commit comments