1+ use rustc_middle:: ty:: TyCtxt ;
2+ use rustc_span:: source_map:: SourceMap ;
13use serde:: Serialize ;
2- use stable_mir:: { CrateDef , DefId } ;
4+ use stable_mir:: { CrateDef , DefId , mir :: mono :: Instance } ;
35
46/// A Rust funtion with its file source, attributes, and raw function content.
57#[ derive( Debug , Serialize ) ]
@@ -15,21 +17,38 @@ pub struct SerFunction {
1517 /// Raw function string, including name, signature, and body.
1618 func : String ,
1719 /// Recursive fnction calls inside the body.
18- callees : Vec < String > ,
20+ callees : Vec < Callee > ,
1921}
2022
2123impl SerFunction {
22- pub fn new ( fun : super :: Function ) -> Self {
24+ pub fn new ( fun : super :: Function , tcx : TyCtxt , src_map : & SourceMap ) -> Self {
2325 SerFunction {
2426 def_id : format_def_id ( & fun. def_id ) ,
2527 file : fun. file ,
2628 attrs : fun. attrs . iter ( ) . map ( |a| a. as_str ( ) . to_owned ( ) ) . collect ( ) ,
2729 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 ( ) ,
2931 }
3032 }
3133}
3234
3335fn format_def_id ( def_id : & DefId ) -> String {
3436 format ! ( "{def_id:?}" )
3537}
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