rapx/utils/
source.rs

1use rustc_hir::{self, Node::*, def::DefKind};
2use rustc_middle::ty::TyCtxt;
3use rustc_span::{
4    FileName,
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
80/*
81fn convert_filename(filename: FileName) -> String {
82    match filename {
83        FileName::Real(path) => path
84            .to_string_lossy(FileNameDisplayPreference::Local)
85            .into_owned(),
86        _ => "<unknown>".to_string(),
87    }
88}
89*/
90
91fn convert_filename(filename: FileName) -> String {
92    if let FileName::Real(realname) = filename {
93        if let Some(ref path) = realname.local_path() {
94            return path.to_string_lossy().into();
95        }
96    }
97    return "<unknown>".to_string();
98}
99
100pub fn get_module_name(tcx: TyCtxt, def_id: DefId) -> String {
101    // --- external items ---
102    if !def_id.is_local() {
103        return tcx.def_path_str(def_id);
104    }
105
106    let local = def_id.as_local().unwrap();
107    let mod_local = tcx.parent_module_from_def_id(local);
108    let mod_id = mod_local.to_def_id();
109    let path = tcx.def_path_str(mod_id);
110
111    if path.is_empty() {
112        "default".to_string()
113    } else {
114        path
115    }
116}
117
118pub fn get_adt_name(tcx: TyCtxt<'_>, def_id: DefId) -> String {
119    match tcx.def_kind(def_id) {
120        DefKind::Struct | DefKind::Enum | DefKind::Union => {
121            let raw_name = tcx.type_of(def_id).skip_binder().to_string();
122            return raw_name
123                .split('<')
124                .next()
125                .unwrap_or(&raw_name)
126                .trim()
127                .to_string();
128        }
129        _ => {}
130    }
131    if let Some(assoc_item) = tcx.opt_associated_item(def_id) {
132        if let Some(impl_id) = assoc_item.impl_container(tcx) {
133            let ty = tcx.type_of(impl_id).skip_binder();
134            let raw_name = ty.to_string();
135            return raw_name
136                .split('<')
137                .next()
138                .unwrap_or(&raw_name)
139                .trim()
140                .to_string();
141        }
142    }
143    "Free_Functions".to_string()
144}