1use rustc_hir::HirId;
2use rustc_hir::PrimTy;
3use rustc_hir::def::{DefKind, Res};
4use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId};
5use rustc_hir::{ImplItemId, ItemKind, Mutability, Node, OwnerId, TraitItemId};
6use rustc_middle::ty::{FloatTy, IntTy, TyCtxt, UintTy, fast_reject::SimplifiedType};
7use rustc_span::symbol::{Ident, Symbol};
8
9pub fn path_str_def_id<'tcx>(tcx: TyCtxt<'tcx>, path_str: &str) -> DefId {
10 let path: Vec<&str> = path_str.split("::").collect();
11 def_path_last_def_id(&tcx, &path)
12}
13
14fn def_path_last_def_id<'tcx>(tcx: &TyCtxt<'tcx>, path: &[&str]) -> DefId {
15 def_path_def_ids(tcx, path)
16 .last()
17 .unwrap_or_else(|| panic!("can not resolve {:?}", path))
18}
19
20pub struct DefPath {
21 def_ids: Vec<DefId>,
22}
23
24impl DefPath {
25 pub fn new(raw: &str, tcx: &TyCtxt<'_>) -> Self {
27 let path: Vec<&str> = raw.split("::").collect();
28 let def_ids: Vec<DefId> = def_path_def_ids(tcx, &path).collect();
29 if def_ids.is_empty() {
30 panic!("Fail to parse def path {}", raw);
31 }
32 DefPath { def_ids }
33 }
34
35 pub fn last_def_id(&self) -> DefId {
36 *self.def_ids.last().unwrap()
37 }
38}
39
40fn def_path_def_ids(tcx: &TyCtxt<'_>, path: &[&str]) -> impl Iterator<Item = DefId> {
47 def_path_res(tcx, path)
48 .into_iter()
49 .filter_map(|res| res.opt_def_id())
50}
51
52fn def_path_res(tcx: &TyCtxt<'_>, path: &[&str]) -> Vec<Res> {
53 let (base, path) = match path {
54 [primitive] => {
55 return vec![
56 PrimTy::from_name(Symbol::intern(primitive)).map_or(Res::Err, Res::PrimTy),
57 ];
58 }
59 [base, path @ ..] => (base, path),
60 _ => return Vec::new(),
61 };
62
63 let base_sym = Symbol::intern(base);
64
65 let local_crate = if tcx.crate_name(LOCAL_CRATE) == base_sym {
66 Some(LOCAL_CRATE.as_def_id())
67 } else {
68 None
69 };
70
71 let crates = find_primitive_impls(tcx, base)
72 .chain(local_crate)
73 .map(|id| Res::Def(tcx.def_kind(id), id))
74 .chain(find_crates(tcx, base_sym))
75 .collect();
76
77 def_path_res_with_base(tcx, crates, path)
78}
79
80fn def_path_res_with_base(tcx: &TyCtxt<'_>, mut base: Vec<Res>, mut path: &[&str]) -> Vec<Res> {
81 while let [segment, rest @ ..] = path {
82 path = rest;
83 let segment = Symbol::intern(segment);
84
85 base = base
86 .into_iter()
87 .filter_map(|res| res.opt_def_id())
88 .flat_map(|def_id| {
89 let inherent_impl_children = tcx
92 .inherent_impls(def_id)
93 .iter()
94 .flat_map(|&impl_def_id| item_children_by_name(tcx, impl_def_id, segment));
95
96 let direct_children = item_children_by_name(tcx, def_id, segment);
97
98 inherent_impl_children.chain(direct_children)
99 })
100 .collect();
101 }
102
103 base
104}
105
106fn find_primitive_impls<'tcx>(
107 tcx: &TyCtxt<'tcx>,
108 name: &str,
109) -> impl Iterator<Item = DefId> + 'tcx {
110 let ty = match name {
111 "bool" => SimplifiedType::Bool,
112 "char" => SimplifiedType::Char,
113 "str" => SimplifiedType::Str,
114 "array" => SimplifiedType::Array,
115 "slice" => SimplifiedType::Slice,
116 "const_ptr" => SimplifiedType::Ptr(Mutability::Not),
120 "mut_ptr" => SimplifiedType::Ptr(Mutability::Mut),
121 "isize" => SimplifiedType::Int(IntTy::Isize),
122 "i8" => SimplifiedType::Int(IntTy::I8),
123 "i16" => SimplifiedType::Int(IntTy::I16),
124 "i32" => SimplifiedType::Int(IntTy::I32),
125 "i64" => SimplifiedType::Int(IntTy::I64),
126 "i128" => SimplifiedType::Int(IntTy::I128),
127 "usize" => SimplifiedType::Uint(UintTy::Usize),
128 "u8" => SimplifiedType::Uint(UintTy::U8),
129 "u16" => SimplifiedType::Uint(UintTy::U16),
130 "u32" => SimplifiedType::Uint(UintTy::U32),
131 "u64" => SimplifiedType::Uint(UintTy::U64),
132 "u128" => SimplifiedType::Uint(UintTy::U128),
133 "f32" => SimplifiedType::Float(FloatTy::F32),
134 "f64" => SimplifiedType::Float(FloatTy::F64),
135 _ => {
136 return [].iter().copied();
137 }
138 };
139
140 tcx.incoherent_impls(ty).iter().copied()
141}
142
143fn non_local_item_children_by_name(tcx: &TyCtxt<'_>, def_id: DefId, name: Symbol) -> Vec<Res> {
144 match tcx.def_kind(def_id) {
145 DefKind::Mod | DefKind::Enum | DefKind::Trait => tcx
146 .module_children(def_id)
147 .iter()
148 .filter(|item| item.ident.name == name)
149 .map(|child| child.res.expect_non_local())
150 .collect(),
151 DefKind::Impl { .. } => tcx
152 .associated_item_def_ids(def_id)
153 .iter()
154 .copied()
155 .filter(|assoc_def_id| tcx.item_name(*assoc_def_id) == name)
156 .map(|assoc_def_id| Res::Def(tcx.def_kind(assoc_def_id), assoc_def_id))
157 .collect(),
158 _ => Vec::new(),
159 }
160}
161
162fn local_item_children_by_name(tcx: &TyCtxt<'_>, local_id: LocalDefId, name: Symbol) -> Vec<Res> {
163 let root_mod;
164 let hir_node = tcx.hir_node_by_def_id(local_id);
165 let item_kind = match hir_node {
166 Node::Crate(module) => {
167 root_mod = ItemKind::Mod(hir_node.ident().unwrap(), module);
168 &root_mod
169 }
170 Node::Item(item) => &item.kind,
171 _ => return Vec::new(),
172 };
173
174 let res = |ident: Ident, owner_id: OwnerId| {
175 if ident.name == name {
176 let def_id = owner_id.to_def_id();
177 Some(Res::Def(tcx.def_kind(def_id), def_id))
178 } else {
179 None
180 }
181 };
182
183 match item_kind {
184 ItemKind::Mod(_ident, module) => module
185 .item_ids
186 .iter()
187 .filter_map(|&item_id| res(tcx.hir_ident(item_id.hir_id()), item_id.owner_id))
188 .collect(),
189 ItemKind::Impl(r#impl) => r#impl
190 .items
191 .iter()
192 .filter_map(|&ImplItemId { owner_id }| {
193 res(tcx.hir_ident(HirId::from(owner_id)), owner_id)
194 })
195 .collect(),
196 ItemKind::Trait {
197 items: trait_item_refs,
198 ..
199 } => trait_item_refs
200 .iter()
201 .filter_map(|&TraitItemId { owner_id }| {
202 res(tcx.hir_ident(HirId::from(owner_id)), owner_id)
203 })
204 .collect(),
205 _ => Vec::new(),
206 }
207}
208
209fn item_children_by_name(tcx: &TyCtxt<'_>, def_id: DefId, name: Symbol) -> Vec<Res> {
210 if let Some(local_id) = def_id.as_local() {
211 local_item_children_by_name(tcx, local_id, name)
212 } else {
213 non_local_item_children_by_name(tcx, def_id, name)
214 }
215}
216
217fn find_crates(tcx: &TyCtxt<'_>, name: Symbol) -> Vec<Res> {
218 tcx.crates(())
219 .iter()
220 .copied()
221 .filter(move |&num| tcx.crate_name(num) == name)
222 .map(CrateNum::as_def_id)
223 .map(|id| Res::Def(tcx.def_kind(id), id))
224 .collect()
225}