Skip to main content

rapx/util/
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        && 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    s.clone()
22}
23pub fn get_name(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Symbol> {
24    if def_id.is_local()
25        && let Some(node) = tcx.hir_get_if_local(def_id) {
26            match node {
27                Item(item) => {
28                    let ident = tcx.hir_ident(item.hir_id());
29                    return Some(ident.name);
30                }
31                ImplItem(item) => {
32                    let ident = tcx.hir_ident(item.hir_id());
33                    return Some(ident.name);
34                }
35                ForeignItem(item) => {
36                    let ident = tcx.hir_ident(item.hir_id());
37                    return Some(ident.name);
38                }
39                TraitItem(item) => {
40                    let ident = tcx.hir_ident(item.hir_id());
41                    return Some(ident.name);
42                }
43                _ => {
44                    return None;
45                }
46            }
47        }
48    None
49}
50
51pub fn get_filename(tcx: TyCtxt<'_>, def_id: DefId) -> Option<String> {
52    // Not `hir_span`: the synthetic bodies of async closures have no HIR node of their own, and it panics on them.
53    def_id.as_local()?;
54    let span = tcx.def_span(def_id);
55
56    // Retrieve the file name
57    let filename = tcx.sess.source_map().span_to_filename(span);
58    match filename {
59        FileName::Real(realname) => realname
60            .local_path()
61            .map(|path| path.to_string_lossy().into()),
62        _ => None,
63    }
64}
65
66pub fn get_module_name(tcx: TyCtxt, def_id: DefId) -> String {
67    // --- external items ---
68    if !def_id.is_local() {
69        return tcx.def_path_str(def_id);
70    }
71
72    let local = def_id.as_local().unwrap();
73    let mod_local = tcx.parent_module_from_def_id(local);
74    let mod_id = mod_local.to_def_id();
75    let path = tcx.def_path_str(mod_id);
76
77    if path.is_empty() {
78        "default".to_string()
79    } else {
80        path
81    }
82}
83
84pub fn get_adt_name(tcx: TyCtxt<'_>, def_id: DefId) -> String {
85    match tcx.def_kind(def_id) {
86        DefKind::Struct | DefKind::Enum | DefKind::Union => {
87            return strip_generic_args(&tcx.type_of(def_id).skip_binder().to_string());
88        }
89        _ => {}
90    }
91    if let Some(assoc_item) = tcx.opt_associated_item(def_id)
92        && let Some(impl_id) = assoc_item.impl_container(tcx) {
93            let ty = tcx.type_of(impl_id).skip_binder();
94            return strip_generic_args(&ty.to_string());
95        }
96    "Free_Functions".to_string()
97}
98
99/// Strip the generic-argument suffix (`<...>`) from a type name.
100fn strip_generic_args(raw_name: &str) -> String {
101    raw_name
102        .split('<')
103        .next()
104        .unwrap_or(raw_name)
105        .trim()
106        .to_string()
107}