rapx/utils/
source.rs

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