rapx/utils/
source.rs

1use rustc_hir::{Node::*, def::DefKind};
2use rustc_middle::ty::TyCtxt;
3use rustc_span::{FileName, def_id::DefId, symbol::Symbol};
4
5pub fn get_fn_name(tcx: TyCtxt<'_>, def_id: DefId) -> Option<String> {
6    let name = tcx.def_path(def_id).to_string_no_crate_verbose();
7    Some(name)
8}
9
10pub fn get_fn_name_byid(def_id: &DefId) -> String {
11    let s = format!("{:?}", *def_id);
12    if let Some(start) = s.find("DefId") {
13        if let Some(end) = s.find("]::") {
14            let s1 = s.replace(&s[start..end + 3], "").to_string();
15            if let Some(start) = s1.find(")") {
16                let result = s1.replace(&s1[start..start + 1], "").to_string();
17                return result;
18            }
19            return s1;
20        }
21    }
22    s.clone()
23}
24pub fn get_name(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Symbol> {
25    if def_id.is_local() {
26        if let Some(node) = tcx.hir_get_if_local(def_id) {
27            match node {
28                Item(item) => {
29                    let ident = tcx.hir_ident(item.hir_id());
30                    return Some(ident.name);
31                }
32                ImplItem(item) => {
33                    let ident = tcx.hir_ident(item.hir_id());
34                    return Some(ident.name);
35                }
36                ForeignItem(item) => {
37                    let ident = tcx.hir_ident(item.hir_id());
38                    return Some(ident.name);
39                }
40                TraitItem(item) => {
41                    let ident = tcx.hir_ident(item.hir_id());
42                    return Some(ident.name);
43                }
44                _ => {
45                    return None;
46                }
47            }
48        }
49    }
50    None
51}
52
53pub fn get_filename(tcx: TyCtxt<'_>, def_id: DefId) -> Option<String> {
54    // Get the HIR node corresponding to the DefId
55    if let Some(local_id) = def_id.as_local() {
56        let hir_id = tcx.local_def_id_to_hir_id(local_id);
57        let span = tcx.hir_span(hir_id);
58        let source_map = tcx.sess.source_map();
59
60        // Retrieve the file name
61        if let Some(filename) = source_map.span_to_filename(span).into() {
62            return Some(convert_filename(filename));
63        }
64    }
65    None
66}
67
68fn convert_filename(filename: FileName) -> String {
69    if let FileName::Real(realname) = filename {
70        if let Some(ref path) = realname.local_path() {
71            return path.to_string_lossy().into();
72        }
73    }
74    return "<unknown>".to_string();
75}
76
77pub fn get_module_name(tcx: TyCtxt, def_id: DefId) -> String {
78    // --- external items ---
79    if !def_id.is_local() {
80        return tcx.def_path_str(def_id);
81    }
82
83    let local = def_id.as_local().unwrap();
84    let mod_local = tcx.parent_module_from_def_id(local);
85    let mod_id = mod_local.to_def_id();
86    let path = tcx.def_path_str(mod_id);
87
88    if path.is_empty() {
89        "default".to_string()
90    } else {
91        path
92    }
93}
94
95pub fn get_adt_name(tcx: TyCtxt<'_>, def_id: DefId) -> String {
96    match tcx.def_kind(def_id) {
97        DefKind::Struct | DefKind::Enum | DefKind::Union => {
98            let raw_name = tcx.type_of(def_id).skip_binder().to_string();
99            return raw_name
100                .split('<')
101                .next()
102                .unwrap_or(&raw_name)
103                .trim()
104                .to_string();
105        }
106        _ => {}
107    }
108    if let Some(assoc_item) = tcx.opt_associated_item(def_id) {
109        if let Some(impl_id) = assoc_item.impl_container(tcx) {
110            let ty = tcx.type_of(impl_id).skip_binder();
111            let raw_name = ty.to_string();
112            return raw_name
113                .split('<')
114                .next()
115                .unwrap_or(&raw_name)
116                .trim()
117                .to_string();
118        }
119    }
120    "Free_Functions".to_string()
121}