Skip to main content

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    let 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
59    // Retrieve the file name
60    let filename = tcx.sess.source_map().span_to_filename(span);
61    match filename {
62        FileName::Real(realname) => realname
63            .local_path()
64            .map(|path| path.to_string_lossy().into()),
65        _ => None,
66    }
67}
68
69pub fn get_module_name(tcx: TyCtxt, def_id: DefId) -> String {
70    // --- external items ---
71    if !def_id.is_local() {
72        return tcx.def_path_str(def_id);
73    }
74
75    let local = def_id.as_local().unwrap();
76    let mod_local = tcx.parent_module_from_def_id(local);
77    let mod_id = mod_local.to_def_id();
78    let path = tcx.def_path_str(mod_id);
79
80    if path.is_empty() {
81        "default".to_string()
82    } else {
83        path
84    }
85}
86
87pub fn get_adt_name(tcx: TyCtxt<'_>, def_id: DefId) -> String {
88    match tcx.def_kind(def_id) {
89        DefKind::Struct | DefKind::Enum | DefKind::Union => {
90            return strip_generic_args(&tcx.type_of(def_id).skip_binder().to_string());
91        }
92        _ => {}
93    }
94    if let Some(assoc_item) = tcx.opt_associated_item(def_id) {
95        if let Some(impl_id) = assoc_item.impl_container(tcx) {
96            let ty = tcx.type_of(impl_id).skip_binder();
97            return strip_generic_args(&ty.to_string());
98        }
99    }
100    "Free_Functions".to_string()
101}
102
103/// Strip the generic-argument suffix (`<...>`) from a type name.
104fn strip_generic_args(raw_name: &str) -> String {
105    raw_name
106        .split('<')
107        .next()
108        .unwrap_or(raw_name)
109        .trim()
110        .to_string()
111}