]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_middle/src/hir/map/collector.rs
New upstream version 1.51.0+dfsg1
[rustc.git] / compiler / rustc_middle / src / hir / map / collector.rs
CommitLineData
ba9703b0
XL
1use crate::arena::Arena;
2use crate::hir::map::{Entry, HirOwnerData, Map};
3use crate::hir::{Owner, OwnerNodes, ParentedNode};
dfeec247 4use crate::ich::StableHashingContext;
9fa01778 5use crate::middle::cstore::CrateStore;
dfeec247
XL
6use rustc_data_structures::fingerprint::Fingerprint;
7use rustc_data_structures::fx::FxHashMap;
8use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
60c5eb7d 9use rustc_data_structures::svh::Svh;
dfeec247
XL
10use rustc_hir as hir;
11use rustc_hir::def_id::CRATE_DEF_INDEX;
ba9703b0
XL
12use rustc_hir::def_id::{LocalDefId, LOCAL_CRATE};
13use rustc_hir::definitions::{self, DefPathHash};
dfeec247
XL
14use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
15use rustc_hir::*;
ba9703b0 16use rustc_index::vec::{Idx, IndexVec};
dfeec247
XL
17use rustc_session::{CrateDisambiguator, Session};
18use rustc_span::source_map::SourceMap;
19use rustc_span::{Span, Symbol, DUMMY_SP};
b039eaaf 20
dfeec247 21use std::iter::repeat;
ea8adc8c 22
dc9dc135 23/// A visitor that walks over the HIR and collects `Node`s into a HIR map.
3b2f2976 24pub(super) struct NodeCollector<'a, 'hir> {
ba9703b0
XL
25 arena: &'hir Arena<'hir>,
26
a7813a04 27 /// The crate
dfeec247 28 krate: &'hir Crate<'hir>,
13cf67c4
XL
29
30 /// Source map
31 source_map: &'a SourceMap,
32
ba9703b0
XL
33 map: IndexVec<LocalDefId, HirOwnerData<'hir>>,
34
a7813a04 35 /// The parent of this node
9fa01778 36 parent_node: hir::HirId,
3b2f2976 37
ba9703b0 38 current_dep_node_owner: LocalDefId,
3b2f2976 39
3b2f2976 40 definitions: &'a definitions::Definitions,
ea8adc8c
XL
41
42 hcx: StableHashingContext<'a>,
43
ba9703b0
XL
44 // We are collecting HIR hashes here so we can compute the
45 // crate hash from them later on.
0731742a
XL
46 hir_body_nodes: Vec<(DefPathHash, Fingerprint)>,
47}
48
ba9703b0
XL
49fn insert_vec_map<K: Idx, V: Clone>(map: &mut IndexVec<K, Option<V>>, k: K, v: V) {
50 let i = k.index();
51 let len = map.len();
52 if i >= len {
53 map.extend(repeat(None).take(i - len + 1));
54 }
55 map[k] = Some(v);
56}
57
58fn hash(
9fa01778 59 hcx: &mut StableHashingContext<'_>,
dfeec247 60 input: impl for<'a> HashStable<StableHashingContext<'a>>,
ba9703b0
XL
61) -> Fingerprint {
62 let mut stable_hasher = StableHasher::new();
63 input.hash_stable(hcx, &mut stable_hasher);
64 stable_hasher.finish()
0731742a
XL
65}
66
ba9703b0 67fn hash_body(
9fa01778 68 hcx: &mut StableHashingContext<'_>,
0731742a 69 def_path_hash: DefPathHash,
dfeec247 70 item_like: impl for<'a> HashStable<StableHashingContext<'a>>,
0731742a 71 hir_body_nodes: &mut Vec<(DefPathHash, Fingerprint)>,
ba9703b0
XL
72) -> Fingerprint {
73 let hash = hash(hcx, HirItemLike { item_like: &item_like });
0731742a 74 hir_body_nodes.push((def_path_hash, hash));
ba9703b0 75 hash
b039eaaf
SL
76}
77
dfeec247
XL
78fn upstream_crates(cstore: &dyn CrateStore) -> Vec<(Symbol, Fingerprint, Svh)> {
79 let mut upstream_crates: Vec<_> = cstore
80 .crates_untracked()
81 .iter()
82 .map(|&cnum| {
83 let name = cstore.crate_name_untracked(cnum);
84 let disambiguator = cstore.crate_disambiguator_untracked(cnum).to_fingerprint();
85 let hash = cstore.crate_hash_untracked(cnum);
86 (name, disambiguator, hash)
87 })
88 .collect();
89 upstream_crates.sort_unstable_by_key(|&(name, dis, _)| (name.as_str(), dis));
90 upstream_crates
91}
92
3b2f2976 93impl<'a, 'hir> NodeCollector<'a, 'hir> {
60c5eb7d
XL
94 pub(super) fn root(
95 sess: &'a Session,
ba9703b0 96 arena: &'hir Arena<'hir>,
dfeec247 97 krate: &'hir Crate<'hir>,
60c5eb7d 98 definitions: &'a definitions::Definitions,
60c5eb7d
XL
99 mut hcx: StableHashingContext<'a>,
100 ) -> NodeCollector<'a, 'hir> {
ba9703b0
XL
101 let root_mod_def_path_hash =
102 definitions.def_path_hash(LocalDefId { local_def_index: CRATE_DEF_INDEX });
ea8adc8c 103
0731742a
XL
104 let mut hir_body_nodes = Vec::new();
105
ba9703b0 106 let hash = {
ea8adc8c 107 let Crate {
ba9703b0 108 ref item,
ea8adc8c
XL
109 // These fields are handled separately:
110 exported_macros: _,
416331ca 111 non_exported_macro_attrs: _,
ea8adc8c
XL
112 items: _,
113 trait_items: _,
114 impl_items: _,
fc512014 115 foreign_items: _,
ea8adc8c
XL
116 bodies: _,
117 trait_impls: _,
ea8adc8c 118 body_ids: _,
0731742a 119 modules: _,
74b04a01 120 proc_macros: _,
f035d41b 121 trait_map: _,
ea8adc8c
XL
122 } = *krate;
123
ba9703b0 124 hash_body(&mut hcx, root_mod_def_path_hash, item, &mut hir_body_nodes)
0731742a 125 };
ea8adc8c 126
b039eaaf 127 let mut collector = NodeCollector {
ba9703b0 128 arena,
041b39d2 129 krate,
0731742a 130 source_map: sess.source_map(),
9fa01778 131 parent_node: hir::CRATE_HIR_ID,
ba9703b0 132 current_dep_node_owner: LocalDefId { local_def_index: CRATE_DEF_INDEX },
3b2f2976 133 definitions,
ea8adc8c
XL
134 hcx,
135 hir_body_nodes,
ba9703b0
XL
136 map: (0..definitions.def_index_count())
137 .map(|_| HirOwnerData { signature: None, with_bodies: None })
138 .collect(),
b039eaaf 139 };
60c5eb7d
XL
140 collector.insert_entry(
141 hir::CRATE_HIR_ID,
ba9703b0
XL
142 Entry { parent: hir::CRATE_HIR_ID, node: Node::Crate(&krate.item) },
143 hash,
60c5eb7d 144 );
b039eaaf 145
b039eaaf
SL
146 collector
147 }
148
60c5eb7d
XL
149 pub(super) fn finalize_and_compute_crate_hash(
150 mut self,
151 crate_disambiguator: CrateDisambiguator,
152 cstore: &dyn CrateStore,
153 commandline_args_hash: u64,
ba9703b0
XL
154 ) -> (IndexVec<LocalDefId, HirOwnerData<'hir>>, Svh) {
155 // Insert bodies into the map
156 for (id, body) in self.krate.bodies.iter() {
157 let bodies = &mut self.map[id.hir_id.owner].with_bodies.as_mut().unwrap().bodies;
158 assert!(bodies.insert(id.hir_id.local_id, body).is_none());
159 }
160
b7449926 161 self.hir_body_nodes.sort_unstable_by_key(|bn| bn.0);
ea8adc8c 162
60c5eb7d
XL
163 let node_hashes = self.hir_body_nodes.iter().fold(
164 Fingerprint::ZERO,
165 |combined_fingerprint, &(def_path_hash, fingerprint)| {
0731742a 166 combined_fingerprint.combine(def_path_hash.0.combine(fingerprint))
60c5eb7d
XL
167 },
168 );
ea8adc8c 169
dfeec247 170 let upstream_crates = upstream_crates(cstore);
ff7c6d11 171
0531ce1d
XL
172 // We hash the final, remapped names of all local source files so we
173 // don't have to include the path prefix remapping commandline args.
174 // If we included the full mapping in the SVH, we could only have
175 // reproducible builds by compiling from the same directory. So we just
176 // hash the result of the mapping instead of the mapping itself.
13cf67c4
XL
177 let mut source_file_names: Vec<_> = self
178 .source_map
0531ce1d
XL
179 .files()
180 .iter()
ba9703b0 181 .filter(|source_file| source_file.cnum == LOCAL_CRATE)
b7449926 182 .map(|source_file| source_file.name_hash)
0531ce1d
XL
183 .collect();
184
185 source_file_names.sort_unstable();
186
0731742a
XL
187 let crate_hash_input = (
188 ((node_hashes, upstream_crates), source_file_names),
60c5eb7d 189 (commandline_args_hash, crate_disambiguator.to_fingerprint()),
0731742a
XL
190 );
191
74b04a01
XL
192 let mut stable_hasher = StableHasher::new();
193 crate_hash_input.hash_stable(&mut self.hcx, &mut stable_hasher);
194 let crate_hash: Fingerprint = stable_hasher.finish();
0731742a
XL
195
196 let svh = Svh::new(crate_hash.to_smaller_hash());
ff7c6d11 197 (self.map, svh)
3b2f2976
XL
198 }
199
ba9703b0 200 fn insert_entry(&mut self, id: HirId, entry: Entry<'hir>, hash: Fingerprint) {
48663c56 201 let i = id.local_id.as_u32() as usize;
ba9703b0
XL
202
203 let arena = self.arena;
204
205 let data = &mut self.map[id.owner];
206
207 if data.with_bodies.is_none() {
208 data.with_bodies = Some(arena.alloc(OwnerNodes {
209 hash,
210 nodes: IndexVec::new(),
211 bodies: FxHashMap::default(),
212 }));
213 }
214
215 let nodes = data.with_bodies.as_mut().unwrap();
216
217 if i == 0 {
218 // Overwrite the dummy hash with the real HIR owner hash.
219 nodes.hash = hash;
220
221 // FIXME: feature(impl_trait_in_bindings) broken and trigger this assert
222 //assert!(data.signature.is_none());
223
224 data.signature =
225 Some(self.arena.alloc(Owner { parent: entry.parent, node: entry.node }));
226 } else {
227 assert_eq!(entry.parent.owner, id.owner);
228 insert_vec_map(
229 &mut nodes.nodes,
230 id.local_id,
231 ParentedNode { parent: entry.parent.local_id, node: entry.node },
232 );
48663c56 233 }
b039eaaf
SL
234 }
235
9fa01778 236 fn insert(&mut self, span: Span, hir_id: HirId, node: Node<'hir>) {
ba9703b0
XL
237 self.insert_with_hash(span, hir_id, node, Fingerprint::ZERO)
238 }
239
240 fn insert_with_hash(&mut self, span: Span, hir_id: HirId, node: Node<'hir>, hash: Fingerprint) {
241 let entry = Entry { parent: self.parent_node, node };
3b2f2976
XL
242
243 // Make sure that the DepNode of some node coincides with the HirId
244 // owner of that node.
245 if cfg!(debug_assertions) {
8faf50e0 246 if hir_id.owner != self.current_dep_node_owner {
f035d41b 247 let node_str = match self.definitions.opt_hir_id_to_local_def_id(hir_id) {
1b1a35ee 248 Some(def_id) => self.definitions.def_path(def_id).to_string_no_crate_verbose(),
60c5eb7d 249 None => format!("{:?}", node),
3b2f2976
XL
250 };
251
13cf67c4
XL
252 span_bug!(
253 span,
254 "inconsistent DepNode at `{:?}` for `{}`: \
ba9703b0 255 current_dep_node_owner={} ({:?}), hir_id.owner={} ({:?})",
13cf67c4 256 self.source_map.span_to_string(span),
3b2f2976 257 node_str,
1b1a35ee
XL
258 self.definitions
259 .def_path(self.current_dep_node_owner)
260 .to_string_no_crate_verbose(),
13cf67c4 261 self.current_dep_node_owner,
1b1a35ee 262 self.definitions.def_path(hir_id.owner).to_string_no_crate_verbose(),
13cf67c4 263 hir_id.owner,
13cf67c4 264 )
3b2f2976
XL
265 }
266 }
267
ba9703b0 268 self.insert_entry(hir_id, entry, hash);
b039eaaf 269 }
a7813a04 270
60c5eb7d 271 fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_node_id: HirId, f: F) {
a7813a04 272 let parent_node = self.parent_node;
9fa01778 273 self.parent_node = parent_node_id;
a7813a04
XL
274 f(self);
275 self.parent_node = parent_node;
276 }
3b2f2976 277
60c5eb7d
XL
278 fn with_dep_node_owner<
279 T: for<'b> HashStable<StableHashingContext<'b>>,
ba9703b0 280 F: FnOnce(&mut Self, Fingerprint),
60c5eb7d
XL
281 >(
282 &mut self,
ba9703b0 283 dep_node_owner: LocalDefId,
60c5eb7d
XL
284 item_like: &T,
285 f: F,
286 ) {
3b2f2976 287 let prev_owner = self.current_dep_node_owner;
ea8adc8c
XL
288
289 let def_path_hash = self.definitions.def_path_hash(dep_node_owner);
290
ba9703b0 291 let hash = hash_body(&mut self.hcx, def_path_hash, item_like, &mut self.hir_body_nodes);
ea8adc8c 292
3b2f2976 293 self.current_dep_node_owner = dep_node_owner;
ba9703b0 294 f(self, hash);
3b2f2976
XL
295 self.current_dep_node_owner = prev_owner;
296 }
b039eaaf
SL
297}
298
3b2f2976 299impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
dfeec247
XL
300 type Map = Map<'hir>;
301
92a42be0
SL
302 /// Because we want to track parent items and so forth, enable
303 /// deep walking so that we walk nested items in the context of
304 /// their outer items.
476ff2be 305
ba9703b0 306 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
e1599b0c 307 panic!("`visit_nested_xxx` must be manually implemented in this visitor");
476ff2be
SL
308 }
309
92a42be0 310 fn visit_nested_item(&mut self, item: ItemId) {
7453a54e 311 debug!("visit_nested_item: {:?}", item);
32a655c1
SL
312 self.visit_item(self.krate.item(item.id));
313 }
314
315 fn visit_nested_trait_item(&mut self, item_id: TraitItemId) {
316 self.visit_trait_item(self.krate.trait_item(item_id));
92a42be0
SL
317 }
318
476ff2be 319 fn visit_nested_impl_item(&mut self, item_id: ImplItemId) {
32a655c1 320 self.visit_impl_item(self.krate.impl_item(item_id));
476ff2be
SL
321 }
322
fc512014
XL
323 fn visit_nested_foreign_item(&mut self, foreign_id: ForeignItemId) {
324 self.visit_foreign_item(self.krate.foreign_item(foreign_id));
325 }
326
32a655c1
SL
327 fn visit_nested_body(&mut self, id: BodyId) {
328 self.visit_body(self.krate.body(id));
476ff2be
SL
329 }
330
dfeec247 331 fn visit_param(&mut self, param: &'hir Param<'hir>) {
e1599b0c
XL
332 let node = Node::Param(param);
333 self.insert(param.pat.span, param.hir_id, node);
334 self.with_parent(param.hir_id, |this| {
335 intravisit::walk_param(this, param);
416331ca
XL
336 });
337 }
338
dfeec247 339 fn visit_item(&mut self, i: &'hir Item<'hir>) {
7453a54e 340 debug!("visit_item: {:?}", i);
60c5eb7d
XL
341 debug_assert_eq!(
342 i.hir_id.owner,
f035d41b 343 self.definitions.opt_hir_id_to_local_def_id(i.hir_id).unwrap()
60c5eb7d 344 );
ba9703b0
XL
345 self.with_dep_node_owner(i.hir_id.owner, i, |this, hash| {
346 this.insert_with_hash(i.span, i.hir_id, Node::Item(i), hash);
9fa01778 347 this.with_parent(i.hir_id, |this| {
e74abb32 348 if let ItemKind::Struct(ref struct_def, _) = i.kind {
532ac7d7
XL
349 // If this is a tuple or unit-like struct, register the constructor.
350 if let Some(ctor_hir_id) = struct_def.ctor_hir_id() {
351 this.insert(i.span, ctor_hir_id, Node::Ctor(struct_def));
a7813a04 352 }
b039eaaf 353 }
3b2f2976
XL
354 intravisit::walk_item(this, i);
355 });
a7813a04 356 });
b039eaaf
SL
357 }
358
fc512014
XL
359 fn visit_foreign_item(&mut self, fi: &'hir ForeignItem<'hir>) {
360 debug_assert_eq!(
361 fi.hir_id.owner,
362 self.definitions.opt_hir_id_to_local_def_id(fi.hir_id).unwrap()
363 );
364 self.with_dep_node_owner(fi.hir_id.owner, fi, |this, hash| {
365 this.insert_with_hash(fi.span, fi.hir_id, Node::ForeignItem(fi), hash);
b039eaaf 366
fc512014
XL
367 this.with_parent(fi.hir_id, |this| {
368 intravisit::walk_foreign_item(this, fi);
369 });
a7813a04 370 });
b039eaaf
SL
371 }
372
dfeec247 373 fn visit_generic_param(&mut self, param: &'hir GenericParam<'hir>) {
29967ef6
XL
374 if let hir::GenericParamKind::Type {
375 synthetic: Some(hir::SyntheticTyParamKind::ImplTrait),
376 ..
377 } = param.kind
378 {
379 debug_assert_eq!(
380 param.hir_id.owner,
381 self.definitions.opt_hir_id_to_local_def_id(param.hir_id).unwrap()
382 );
383 self.with_dep_node_owner(param.hir_id.owner, param, |this, hash| {
384 this.insert_with_hash(param.span, param.hir_id, Node::GenericParam(param), hash);
385
386 this.with_parent(param.hir_id, |this| {
387 intravisit::walk_generic_param(this, param);
388 });
389 });
390 } else {
391 self.insert(param.span, param.hir_id, Node::GenericParam(param));
392 intravisit::walk_generic_param(self, param);
393 }
b039eaaf
SL
394 }
395
dfeec247 396 fn visit_trait_item(&mut self, ti: &'hir TraitItem<'hir>) {
60c5eb7d
XL
397 debug_assert_eq!(
398 ti.hir_id.owner,
f035d41b 399 self.definitions.opt_hir_id_to_local_def_id(ti.hir_id).unwrap()
60c5eb7d 400 );
ba9703b0
XL
401 self.with_dep_node_owner(ti.hir_id.owner, ti, |this, hash| {
402 this.insert_with_hash(ti.span, ti.hir_id, Node::TraitItem(ti), hash);
3b2f2976 403
9fa01778 404 this.with_parent(ti.hir_id, |this| {
3b2f2976
XL
405 intravisit::walk_trait_item(this, ti);
406 });
a7813a04 407 });
b039eaaf
SL
408 }
409
dfeec247 410 fn visit_impl_item(&mut self, ii: &'hir ImplItem<'hir>) {
60c5eb7d
XL
411 debug_assert_eq!(
412 ii.hir_id.owner,
f035d41b 413 self.definitions.opt_hir_id_to_local_def_id(ii.hir_id).unwrap()
60c5eb7d 414 );
ba9703b0
XL
415 self.with_dep_node_owner(ii.hir_id.owner, ii, |this, hash| {
416 this.insert_with_hash(ii.span, ii.hir_id, Node::ImplItem(ii), hash);
3b2f2976 417
9fa01778 418 this.with_parent(ii.hir_id, |this| {
3b2f2976
XL
419 intravisit::walk_impl_item(this, ii);
420 });
a7813a04 421 });
b039eaaf
SL
422 }
423
dfeec247 424 fn visit_pat(&mut self, pat: &'hir Pat<'hir>) {
60c5eb7d
XL
425 let node =
426 if let PatKind::Binding(..) = pat.kind { Node::Binding(pat) } else { Node::Pat(pat) };
9fa01778 427 self.insert(pat.span, pat.hir_id, node);
b039eaaf 428
9fa01778 429 self.with_parent(pat.hir_id, |this| {
a7813a04
XL
430 intravisit::walk_pat(this, pat);
431 });
b039eaaf
SL
432 }
433
dfeec247 434 fn visit_arm(&mut self, arm: &'hir Arm<'hir>) {
dc9dc135
XL
435 let node = Node::Arm(arm);
436
437 self.insert(arm.span, arm.hir_id, node);
438
439 self.with_parent(arm.hir_id, |this| {
440 intravisit::walk_arm(this, arm);
441 });
442 }
443
94b46f34 444 fn visit_anon_const(&mut self, constant: &'hir AnonConst) {
9fa01778 445 self.insert(DUMMY_SP, constant.hir_id, Node::AnonConst(constant));
94b46f34 446
9fa01778 447 self.with_parent(constant.hir_id, |this| {
94b46f34
XL
448 intravisit::walk_anon_const(this, constant);
449 });
450 }
451
dfeec247 452 fn visit_expr(&mut self, expr: &'hir Expr<'hir>) {
9fa01778 453 self.insert(expr.span, expr.hir_id, Node::Expr(expr));
b039eaaf 454
9fa01778 455 self.with_parent(expr.hir_id, |this| {
a7813a04
XL
456 intravisit::walk_expr(this, expr);
457 });
b039eaaf
SL
458 }
459
dfeec247 460 fn visit_stmt(&mut self, stmt: &'hir Stmt<'hir>) {
9fa01778 461 self.insert(stmt.span, stmt.hir_id, Node::Stmt(stmt));
a7813a04 462
9fa01778 463 self.with_parent(stmt.hir_id, |this| {
a7813a04
XL
464 intravisit::walk_stmt(this, stmt);
465 });
b039eaaf
SL
466 }
467
dfeec247 468 fn visit_path_segment(&mut self, path_span: Span, path_segment: &'hir PathSegment<'hir>) {
9fa01778
XL
469 if let Some(hir_id) = path_segment.hir_id {
470 self.insert(path_span, hir_id, Node::PathSegment(path_segment));
13cf67c4
XL
471 }
472 intravisit::walk_path_segment(self, path_span, path_segment);
473 }
474
dfeec247 475 fn visit_ty(&mut self, ty: &'hir Ty<'hir>) {
9fa01778 476 self.insert(ty.span, ty.hir_id, Node::Ty(ty));
5bcae85e 477
9fa01778 478 self.with_parent(ty.hir_id, |this| {
5bcae85e
SL
479 intravisit::walk_ty(this, ty);
480 });
481 }
482
dfeec247 483 fn visit_trait_ref(&mut self, tr: &'hir TraitRef<'hir>) {
9fa01778 484 self.insert(tr.path.span, tr.hir_ref_id, Node::TraitRef(tr));
476ff2be 485
9fa01778 486 self.with_parent(tr.hir_ref_id, |this| {
476ff2be
SL
487 intravisit::walk_trait_ref(this, tr);
488 });
489 }
490
60c5eb7d
XL
491 fn visit_fn(
492 &mut self,
493 fk: intravisit::FnKind<'hir>,
dfeec247 494 fd: &'hir FnDecl<'hir>,
60c5eb7d
XL
495 b: BodyId,
496 s: Span,
497 id: HirId,
498 ) {
b039eaaf 499 assert_eq!(self.parent_node, id);
5bcae85e 500 intravisit::walk_fn(self, fk, fd, b, s, id);
b039eaaf
SL
501 }
502
dfeec247 503 fn visit_block(&mut self, block: &'hir Block<'hir>) {
9fa01778
XL
504 self.insert(block.span, block.hir_id, Node::Block(block));
505 self.with_parent(block.hir_id, |this| {
a7813a04
XL
506 intravisit::walk_block(this, block);
507 });
b039eaaf
SL
508 }
509
dfeec247 510 fn visit_local(&mut self, l: &'hir Local<'hir>) {
9fa01778 511 self.insert(l.span, l.hir_id, Node::Local(l));
60c5eb7d 512 self.with_parent(l.hir_id, |this| intravisit::walk_local(this, l))
3b2f2976
XL
513 }
514
32a655c1 515 fn visit_lifetime(&mut self, lifetime: &'hir Lifetime) {
9fa01778 516 self.insert(lifetime.span, lifetime.hir_id, Node::Lifetime(lifetime));
b039eaaf 517 }
476ff2be 518
dfeec247 519 fn visit_vis(&mut self, visibility: &'hir Visibility<'hir>) {
8faf50e0 520 match visibility.node {
60c5eb7d 521 VisibilityKind::Public | VisibilityKind::Crate(_) | VisibilityKind::Inherited => {}
9fa01778
XL
522 VisibilityKind::Restricted { hir_id, .. } => {
523 self.insert(visibility.span, hir_id, Node::Visibility(visibility));
524 self.with_parent(hir_id, |this| {
476ff2be
SL
525 intravisit::walk_vis(this, visibility);
526 });
527 }
528 }
529 }
530
dfeec247 531 fn visit_macro_def(&mut self, macro_def: &'hir MacroDef<'hir>) {
5869c6ff
XL
532 // Exported macros are visited directly from the crate root,
533 // so they do not have `parent_node` set.
534 // Find the correct enclosing module from their DefKey.
535 let def_key = self.definitions.def_key(macro_def.hir_id.owner);
536 let parent = def_key.parent.map_or(hir::CRATE_HIR_ID, |local_def_index| {
537 self.definitions.local_def_id_to_hir_id(LocalDefId { local_def_index })
538 });
539 self.with_parent(parent, |this| {
540 this.with_dep_node_owner(macro_def.hir_id.owner, macro_def, |this, hash| {
541 this.insert_with_hash(
542 macro_def.span,
543 macro_def.hir_id,
544 Node::MacroDef(macro_def),
545 hash,
546 );
547 })
ea8adc8c 548 });
476ff2be
SL
549 }
550
dfeec247 551 fn visit_variant(&mut self, v: &'hir Variant<'hir>, g: &'hir Generics<'hir>, item_id: HirId) {
e1599b0c
XL
552 self.insert(v.span, v.id, Node::Variant(v));
553 self.with_parent(v.id, |this| {
532ac7d7 554 // Register the constructor of this variant.
e1599b0c
XL
555 if let Some(ctor_hir_id) = v.data.ctor_hir_id() {
556 this.insert(v.span, ctor_hir_id, Node::Ctor(&v.data));
532ac7d7 557 }
32a655c1
SL
558 intravisit::walk_variant(this, v, g, item_id);
559 });
560 }
561
dfeec247 562 fn visit_struct_field(&mut self, field: &'hir StructField<'hir>) {
9fa01778
XL
563 self.insert(field.span, field.hir_id, Node::Field(field));
564 self.with_parent(field.hir_id, |this| {
476ff2be
SL
565 intravisit::walk_struct_field(this, field);
566 });
567 }
3b2f2976
XL
568
569 fn visit_trait_item_ref(&mut self, ii: &'hir TraitItemRef) {
570 // Do not visit the duplicate information in TraitItemRef. We want to
571 // map the actual nodes, not the duplicate ones in the *Ref.
60c5eb7d 572 let TraitItemRef { id, ident: _, kind: _, span: _, defaultness: _ } = *ii;
3b2f2976
XL
573
574 self.visit_nested_trait_item(id);
575 }
576
dfeec247 577 fn visit_impl_item_ref(&mut self, ii: &'hir ImplItemRef<'hir>) {
3b2f2976
XL
578 // Do not visit the duplicate information in ImplItemRef. We want to
579 // map the actual nodes, not the duplicate ones in the *Ref.
60c5eb7d 580 let ImplItemRef { id, ident: _, kind: _, span: _, vis: _, defaultness: _ } = *ii;
3b2f2976
XL
581
582 self.visit_nested_impl_item(id);
583 }
fc512014
XL
584
585 fn visit_foreign_item_ref(&mut self, fi: &'hir ForeignItemRef<'hir>) {
586 // Do not visit the duplicate information in ForeignItemRef. We want to
587 // map the actual nodes, not the duplicate ones in the *Ref.
588 let ForeignItemRef { id, ident: _, span: _, vis: _ } = *fi;
589
590 self.visit_nested_foreign_item(id);
591 }
b039eaaf 592}
ea8adc8c 593
ea8adc8c
XL
594struct HirItemLike<T> {
595 item_like: T,
ea8adc8c
XL
596}
597
dc9dc135
XL
598impl<'hir, T> HashStable<StableHashingContext<'hir>> for HirItemLike<T>
599where
600 T: HashStable<StableHashingContext<'hir>>,
ea8adc8c 601{
e74abb32 602 fn hash_stable(&self, hcx: &mut StableHashingContext<'hir>, hasher: &mut StableHasher) {
ba9703b0 603 hcx.while_hashing_hir_bodies(true, |hcx| {
ea8adc8c
XL
604 self.item_like.hash_stable(hcx, hasher);
605 });
606 }
607}