1use std::path::{Path, PathBuf};
2
3use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
4use rustc_hir::def::{DefKind, Res};
5use rustc_hir::def_id::{DefId, LOCAL_CRATE};
6use rustc_hir::intravisit::{self, Visitor};
7use rustc_hir::{
8 ExprKind, HirId, Item, ItemKind, Mod, Node, Pat, PatExpr, PatExprKind, PatKind, QPath,
9};
10use rustc_middle::hir::nested_filter;
11use rustc_middle::ty::TyCtxt;
12use rustc_span::hygiene::MacroKind;
13use rustc_span::{BytePos, ExpnKind, Span};
14
15use crate::clean::{self, PrimitiveType, rustc_span};
16use crate::html::sources;
17
18#[derive(Debug)]
26pub(crate) enum LinkFromSrc {
27 Local(clean::Span),
28 External(DefId),
29 Primitive(PrimitiveType),
30 Doc(DefId),
31}
32
33pub(crate) fn collect_spans_and_sources(
44 tcx: TyCtxt<'_>,
45 krate: &clean::Crate,
46 src_root: &Path,
47 include_sources: bool,
48 generate_link_to_definition: bool,
49) -> (FxIndexMap<PathBuf, String>, FxHashMap<Span, LinkFromSrc>) {
50 if include_sources {
51 let mut visitor = SpanMapVisitor { tcx, matches: FxHashMap::default() };
52
53 if generate_link_to_definition {
54 tcx.hir_walk_toplevel_module(&mut visitor);
55 }
56 let sources = sources::collect_local_sources(tcx, src_root, krate);
57 (sources, visitor.matches)
58 } else {
59 (Default::default(), Default::default())
60 }
61}
62
63struct SpanMapVisitor<'tcx> {
64 pub(crate) tcx: TyCtxt<'tcx>,
65 pub(crate) matches: FxHashMap<Span, LinkFromSrc>,
66}
67
68impl SpanMapVisitor<'_> {
69 fn handle_path(&mut self, path: &rustc_hir::Path<'_>) {
71 match path.res {
72 Res::Def(kind, def_id) if kind != DefKind::TyParam => {
76 let link = if def_id.as_local().is_some() {
77 LinkFromSrc::Local(rustc_span(def_id, self.tcx))
78 } else {
79 LinkFromSrc::External(def_id)
80 };
81 let span = path
83 .segments
84 .last()
85 .map(|last| {
86 if path.span.contains(last.ident.span) {
90 path.span.with_hi(last.ident.span.hi())
91 } else {
92 path.span
93 }
94 })
95 .unwrap_or(path.span);
96 self.matches.insert(span, link);
97 }
98 Res::Local(_) if let Some(span) = self.tcx.hir().res_span(path.res) => {
99 self.matches.insert(path.span, LinkFromSrc::Local(clean::Span::new(span)));
100 }
101 Res::PrimTy(p) => {
102 self.matches.insert(path.span, LinkFromSrc::Primitive(PrimitiveType::from(p)));
104 }
105 Res::Err => {}
106 _ => {}
107 }
108 }
109
110 pub(crate) fn extract_info_from_hir_id(&mut self, hir_id: HirId) {
112 if let Node::Item(item) = self.tcx.hir_node(hir_id)
113 && let Some(span) = self.tcx.def_ident_span(item.owner_id)
114 {
115 let cspan = clean::Span::new(span);
116 if cspan.inner().is_dummy() || cspan.cnum(self.tcx.sess) != LOCAL_CRATE {
118 return;
119 }
120 self.matches.insert(span, LinkFromSrc::Doc(item.owner_id.to_def_id()));
121 }
122 }
123
124 fn handle_macro(&mut self, span: Span) -> bool {
132 if !span.from_expansion() {
133 return false;
134 }
135 let mut data = span.ctxt().outer_expn_data();
138 let mut call_site = data.call_site;
139 while call_site.from_expansion() {
144 data = call_site.ctxt().outer_expn_data();
145 call_site = data.call_site;
146 }
147
148 let macro_name = match data.kind {
149 ExpnKind::Macro(MacroKind::Bang, macro_name) => macro_name,
150 _ => return true,
153 };
154 let link_from_src = match data.macro_def_id {
155 Some(macro_def_id) => {
156 if macro_def_id.is_local() {
157 LinkFromSrc::Local(clean::Span::new(data.def_site))
158 } else {
159 LinkFromSrc::External(macro_def_id)
160 }
161 }
162 None => return true,
163 };
164 let new_span = data.call_site;
165 let macro_name = macro_name.as_str();
166 let new_span = new_span.with_hi(new_span.lo() + BytePos(macro_name.len() as u32));
169 self.matches.insert(new_span, link_from_src);
170 true
171 }
172
173 fn infer_id(&mut self, hir_id: HirId, expr_hir_id: Option<HirId>, span: Span) {
174 let tcx = self.tcx;
175 let body_id = tcx.hir_enclosing_body_owner(hir_id);
176 let typeck_results = tcx.typeck_body(tcx.hir_body_owned_by(body_id).id());
181 if let Some(def_id) = typeck_results.type_dependent_def_id(expr_hir_id.unwrap_or(hir_id)) {
184 let link = if def_id.as_local().is_some() {
185 LinkFromSrc::Local(rustc_span(def_id, tcx))
186 } else {
187 LinkFromSrc::External(def_id)
188 };
189 self.matches.insert(span, link);
190 }
191 }
192
193 fn handle_pat(&mut self, p: &Pat<'_>) {
194 let mut check_qpath = |qpath, hir_id| match qpath {
195 QPath::TypeRelative(_, path) if matches!(path.res, Res::Err) => {
196 self.infer_id(path.hir_id, Some(hir_id), qpath.span());
197 }
198 QPath::Resolved(_, path) => self.handle_path(path),
199 _ => {}
200 };
201 match p.kind {
202 PatKind::Binding(_, _, _, Some(p)) => self.handle_pat(p),
203 PatKind::Struct(qpath, _, _) | PatKind::TupleStruct(qpath, _, _) => {
204 check_qpath(qpath, p.hir_id)
205 }
206 PatKind::Expr(PatExpr { kind: PatExprKind::Path(qpath), hir_id, .. }) => {
207 check_qpath(*qpath, *hir_id)
208 }
209 PatKind::Or(pats) => {
210 for pat in pats {
211 self.handle_pat(pat);
212 }
213 }
214 _ => {}
215 }
216 }
217}
218
219impl<'tcx> Visitor<'tcx> for SpanMapVisitor<'tcx> {
220 type NestedFilter = nested_filter::All;
221
222 fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
223 self.tcx
224 }
225
226 fn visit_path(&mut self, path: &rustc_hir::Path<'tcx>, _id: HirId) {
227 if self.handle_macro(path.span) {
228 return;
229 }
230 self.handle_path(path);
231 intravisit::walk_path(self, path);
232 }
233
234 fn visit_pat(&mut self, p: &Pat<'tcx>) {
235 self.handle_pat(p);
236 }
237
238 fn visit_mod(&mut self, m: &'tcx Mod<'tcx>, span: Span, id: HirId) {
239 if !span.overlaps(m.spans.inner_span) {
242 if let Node::Item(item) = self.tcx.hir_node(id) {
245 let (ident, _) = item.expect_mod();
246 self.matches
247 .insert(ident.span, LinkFromSrc::Local(clean::Span::new(m.spans.inner_span)));
248 }
249 } else {
250 self.extract_info_from_hir_id(id);
252 }
253 intravisit::walk_mod(self, m);
254 }
255
256 fn visit_expr(&mut self, expr: &'tcx rustc_hir::Expr<'tcx>) {
257 match expr.kind {
258 ExprKind::MethodCall(segment, ..) => {
259 self.infer_id(segment.hir_id, Some(expr.hir_id), segment.ident.span)
260 }
261 ExprKind::Call(call, ..) => self.infer_id(call.hir_id, None, call.span),
262 _ => {
263 if self.handle_macro(expr.span) {
264 return;
266 }
267 }
268 }
269 intravisit::walk_expr(self, expr);
270 }
271
272 fn visit_item(&mut self, item: &'tcx Item<'tcx>) {
273 match item.kind {
274 ItemKind::Static(..)
275 | ItemKind::Const(..)
276 | ItemKind::Fn { .. }
277 | ItemKind::Macro(..)
278 | ItemKind::TyAlias(..)
279 | ItemKind::Enum(..)
280 | ItemKind::Struct(..)
281 | ItemKind::Union(..)
282 | ItemKind::Trait(..)
283 | ItemKind::TraitAlias(..) => self.extract_info_from_hir_id(item.hir_id()),
284 ItemKind::Impl(_)
285 | ItemKind::Use(..)
286 | ItemKind::ExternCrate(..)
287 | ItemKind::ForeignMod { .. }
288 | ItemKind::GlobalAsm { .. }
289 | ItemKind::Mod(..) => {}
291 }
292 intravisit::walk_item(self, item);
293 }
294}