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