forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollector.rs
574 lines (491 loc) · 19.4 KB
/
collector.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
use super::*;
use crate::dep_graph::{DepGraph, DepKind, DepNodeIndex};
use crate::hir;
use crate::hir::def_id::{LOCAL_CRATE, CrateNum};
use crate::hir::intravisit::{Visitor, NestedVisitorMap};
use rustc_data_structures::svh::Svh;
use crate::ich::Fingerprint;
use crate::middle::cstore::CrateStore;
use crate::session::CrateDisambiguator;
use crate::session::Session;
use std::iter::repeat;
use syntax::ast::{NodeId, CRATE_NODE_ID};
use syntax::source_map::SourceMap;
use syntax_pos::Span;
use crate::ich::StableHashingContext;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableHasherResult};
/// A Visitor that walks over the HIR and collects Nodes into a HIR map
pub(super) struct NodeCollector<'a, 'hir> {
/// The crate
krate: &'hir Crate,
/// Source map
source_map: &'a SourceMap,
/// The node map
map: Vec<Option<Entry<'hir>>>,
/// The parent of this node
parent_node: NodeId,
parent_hir: hir::HirId,
// These fields keep track of the currently relevant DepNodes during
// the visitor's traversal.
current_dep_node_owner: DefIndex,
current_signature_dep_index: DepNodeIndex,
current_full_dep_index: DepNodeIndex,
currently_in_body: bool,
dep_graph: &'a DepGraph,
definitions: &'a definitions::Definitions,
hcx: StableHashingContext<'a>,
// We are collecting DepNode::HirBody hashes here so we can compute the
// crate hash from then later on.
hir_body_nodes: Vec<(DefPathHash, Fingerprint)>,
}
fn input_dep_node_and_hash<I>(
dep_graph: &DepGraph,
hcx: &mut StableHashingContext<'_>,
dep_node: DepNode,
input: I,
) -> (DepNodeIndex, Fingerprint)
where
I: for<'a> HashStable<StableHashingContext<'a>>,
{
let dep_node_index = dep_graph.input_task(dep_node, &mut *hcx, &input).1;
let hash = if dep_graph.is_fully_enabled() {
dep_graph.fingerprint_of(dep_node_index)
} else {
let mut stable_hasher = StableHasher::new();
input.hash_stable(hcx, &mut stable_hasher);
stable_hasher.finish()
};
(dep_node_index, hash)
}
fn alloc_hir_dep_nodes<I>(
dep_graph: &DepGraph,
hcx: &mut StableHashingContext<'_>,
def_path_hash: DefPathHash,
item_like: I,
hir_body_nodes: &mut Vec<(DefPathHash, Fingerprint)>,
) -> (DepNodeIndex, DepNodeIndex)
where
I: for<'a> HashStable<StableHashingContext<'a>>,
{
let sig = dep_graph.input_task(
def_path_hash.to_dep_node(DepKind::Hir),
&mut *hcx,
HirItemLike { item_like: &item_like, hash_bodies: false },
).1;
let (full, hash) = input_dep_node_and_hash(
dep_graph,
hcx,
def_path_hash.to_dep_node(DepKind::HirBody),
HirItemLike { item_like: &item_like, hash_bodies: true },
);
hir_body_nodes.push((def_path_hash, hash));
(sig, full)
}
impl<'a, 'hir> NodeCollector<'a, 'hir> {
pub(super) fn root(sess: &'a Session,
krate: &'hir Crate,
dep_graph: &'a DepGraph,
definitions: &'a definitions::Definitions,
mut hcx: StableHashingContext<'a>)
-> NodeCollector<'a, 'hir> {
let root_mod_def_path_hash = definitions.def_path_hash(CRATE_DEF_INDEX);
let mut hir_body_nodes = Vec::new();
// Allocate DepNodes for the root module
let (root_mod_sig_dep_index, root_mod_full_dep_index) = {
let Crate {
ref module,
// Crate attributes are not copied over to the root `Mod`, so hash
// them explicitly here.
ref attrs,
span,
// These fields are handled separately:
exported_macros: _,
items: _,
trait_items: _,
impl_items: _,
bodies: _,
trait_impls: _,
trait_auto_impl: _,
body_ids: _,
modules: _,
} = *krate;
alloc_hir_dep_nodes(
dep_graph,
&mut hcx,
root_mod_def_path_hash,
(module, attrs, span),
&mut hir_body_nodes,
)
};
{
dep_graph.input_task(
DepNode::new_no_params(DepKind::AllLocalTraitImpls),
&mut hcx,
&krate.trait_impls,
);
}
let mut collector = NodeCollector {
krate,
source_map: sess.source_map(),
map: repeat(None).take(sess.current_node_id_count()).collect(),
parent_node: CRATE_NODE_ID,
parent_hir: hir::CRATE_HIR_ID,
current_signature_dep_index: root_mod_sig_dep_index,
current_full_dep_index: root_mod_full_dep_index,
current_dep_node_owner: CRATE_DEF_INDEX,
currently_in_body: false,
dep_graph,
definitions,
hcx,
hir_body_nodes,
};
collector.insert_entry(CRATE_NODE_ID, Entry {
parent: CRATE_NODE_ID,
parent_hir: hir::CRATE_HIR_ID,
dep_node: root_mod_sig_dep_index,
node: Node::Crate,
});
collector
}
pub(super) fn finalize_and_compute_crate_hash(mut self,
crate_disambiguator: CrateDisambiguator,
cstore: &dyn CrateStore,
commandline_args_hash: u64)
-> (Vec<Option<Entry<'hir>>>, Svh)
{
self.hir_body_nodes.sort_unstable_by_key(|bn| bn.0);
let node_hashes = self
.hir_body_nodes
.iter()
.fold(Fingerprint::ZERO, |combined_fingerprint, &(def_path_hash, fingerprint)| {
combined_fingerprint.combine(def_path_hash.0.combine(fingerprint))
});
let mut upstream_crates: Vec<_> = cstore.crates_untracked().iter().map(|&cnum| {
let name = cstore.crate_name_untracked(cnum).as_str();
let disambiguator = cstore.crate_disambiguator_untracked(cnum).to_fingerprint();
let hash = cstore.crate_hash_untracked(cnum);
(name, disambiguator, hash)
}).collect();
upstream_crates.sort_unstable_by_key(|&(name, dis, _)| (name, dis));
// We hash the final, remapped names of all local source files so we
// don't have to include the path prefix remapping commandline args.
// If we included the full mapping in the SVH, we could only have
// reproducible builds by compiling from the same directory. So we just
// hash the result of the mapping instead of the mapping itself.
let mut source_file_names: Vec<_> = self
.source_map
.files()
.iter()
.filter(|source_file| CrateNum::from_u32(source_file.crate_of_origin) == LOCAL_CRATE)
.map(|source_file| source_file.name_hash)
.collect();
source_file_names.sort_unstable();
let crate_hash_input = (
((node_hashes, upstream_crates), source_file_names),
(commandline_args_hash, crate_disambiguator.to_fingerprint())
);
let (_, crate_hash) = input_dep_node_and_hash(
self.dep_graph,
&mut self.hcx,
DepNode::new_no_params(DepKind::Krate),
crate_hash_input,
);
let svh = Svh::new(crate_hash.to_smaller_hash());
(self.map, svh)
}
fn insert_entry(&mut self, id: NodeId, entry: Entry<'hir>) {
debug!("hir_map: {:?} => {:?}", id, entry);
self.map[id.as_usize()] = Some(entry);
}
fn insert(&mut self, span: Span, id: NodeId, node: Node<'hir>) {
let entry = Entry {
parent: self.parent_node,
parent_hir: self.parent_hir,
dep_node: if self.currently_in_body {
self.current_full_dep_index
} else {
self.current_signature_dep_index
},
node,
};
// Make sure that the DepNode of some node coincides with the HirId
// owner of that node.
if cfg!(debug_assertions) {
let hir_id = self.definitions.node_to_hir_id(id);
if hir_id.owner != self.current_dep_node_owner {
let node_str = match self.definitions.opt_def_index(id) {
Some(def_index) => {
self.definitions.def_path(def_index).to_string_no_crate()
}
None => format!("{:?}", node)
};
let forgot_str = if hir_id == crate::hir::DUMMY_HIR_ID {
format!("\nMaybe you forgot to lower the node id {:?}?", id)
} else {
String::new()
};
span_bug!(
span,
"inconsistent DepNode at `{:?}` for `{}`: \
current_dep_node_owner={} ({:?}), hir_id.owner={} ({:?}){}",
self.source_map.span_to_string(span),
node_str,
self.definitions
.def_path(self.current_dep_node_owner)
.to_string_no_crate(),
self.current_dep_node_owner,
self.definitions.def_path(hir_id.owner).to_string_no_crate(),
hir_id.owner,
forgot_str,
)
}
}
self.insert_entry(id, entry);
}
fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_id: NodeId, f: F) {
let parent_node = self.parent_node;
self.parent_node = parent_id;
f(self);
self.parent_node = parent_node;
}
fn with_dep_node_owner<T: for<'b> HashStable<StableHashingContext<'b>>,
F: FnOnce(&mut Self)>(&mut self,
dep_node_owner: DefIndex,
item_like: &T,
f: F) {
let prev_owner = self.current_dep_node_owner;
let prev_signature_dep_index = self.current_signature_dep_index;
let prev_full_dep_index = self.current_full_dep_index;
let prev_in_body = self.currently_in_body;
let def_path_hash = self.definitions.def_path_hash(dep_node_owner);
let (signature_dep_index, full_dep_index) = alloc_hir_dep_nodes(
self.dep_graph,
&mut self.hcx,
def_path_hash,
item_like,
&mut self.hir_body_nodes,
);
self.current_signature_dep_index = signature_dep_index;
self.current_full_dep_index = full_dep_index;
self.current_dep_node_owner = dep_node_owner;
self.currently_in_body = false;
f(self);
self.currently_in_body = prev_in_body;
self.current_dep_node_owner = prev_owner;
self.current_full_dep_index = prev_full_dep_index;
self.current_signature_dep_index = prev_signature_dep_index;
}
}
impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
/// Because we want to track parent items and so forth, enable
/// deep walking so that we walk nested items in the context of
/// their outer items.
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'hir> {
panic!("visit_nested_xxx must be manually implemented in this visitor")
}
fn visit_nested_item(&mut self, item: ItemId) {
debug!("visit_nested_item: {:?}", item);
self.visit_item(self.krate.item(item.id));
}
fn visit_nested_trait_item(&mut self, item_id: TraitItemId) {
self.visit_trait_item(self.krate.trait_item(item_id));
}
fn visit_nested_impl_item(&mut self, item_id: ImplItemId) {
self.visit_impl_item(self.krate.impl_item(item_id));
}
fn visit_nested_body(&mut self, id: BodyId) {
let prev_in_body = self.currently_in_body;
self.currently_in_body = true;
self.visit_body(self.krate.body(id));
self.currently_in_body = prev_in_body;
}
fn visit_item(&mut self, i: &'hir Item) {
debug!("visit_item: {:?}", i);
debug_assert_eq!(i.hir_id.owner,
self.definitions.opt_def_index(i.id).unwrap());
self.with_dep_node_owner(i.hir_id.owner, i, |this| {
this.insert(i.span, i.id, Node::Item(i));
this.with_parent(i.id, |this| {
if let ItemKind::Struct(ref struct_def, _) = i.node {
// If this is a tuple-like struct, register the constructor.
if !struct_def.is_struct() {
this.insert(i.span, struct_def.id(), Node::StructCtor(struct_def));
}
}
intravisit::walk_item(this, i);
});
});
}
fn visit_foreign_item(&mut self, foreign_item: &'hir ForeignItem) {
self.insert(foreign_item.span, foreign_item.id, Node::ForeignItem(foreign_item));
self.with_parent(foreign_item.id, |this| {
intravisit::walk_foreign_item(this, foreign_item);
});
}
fn visit_generic_param(&mut self, param: &'hir GenericParam) {
self.insert(param.span, param.id, Node::GenericParam(param));
intravisit::walk_generic_param(self, param);
}
fn visit_trait_item(&mut self, ti: &'hir TraitItem) {
debug_assert_eq!(ti.hir_id.owner,
self.definitions.opt_def_index(ti.id).unwrap());
self.with_dep_node_owner(ti.hir_id.owner, ti, |this| {
this.insert(ti.span, ti.id, Node::TraitItem(ti));
this.with_parent(ti.id, |this| {
intravisit::walk_trait_item(this, ti);
});
});
}
fn visit_impl_item(&mut self, ii: &'hir ImplItem) {
debug_assert_eq!(ii.hir_id.owner,
self.definitions.opt_def_index(ii.id).unwrap());
self.with_dep_node_owner(ii.hir_id.owner, ii, |this| {
this.insert(ii.span, ii.id, Node::ImplItem(ii));
this.with_parent(ii.id, |this| {
intravisit::walk_impl_item(this, ii);
});
});
}
fn visit_pat(&mut self, pat: &'hir Pat) {
let node = if let PatKind::Binding(..) = pat.node {
Node::Binding(pat)
} else {
Node::Pat(pat)
};
self.insert(pat.span, pat.id, node);
self.with_parent(pat.id, |this| {
intravisit::walk_pat(this, pat);
});
}
fn visit_anon_const(&mut self, constant: &'hir AnonConst) {
self.insert(DUMMY_SP, constant.id, Node::AnonConst(constant));
self.with_parent(constant.id, |this| {
intravisit::walk_anon_const(this, constant);
});
}
fn visit_expr(&mut self, expr: &'hir Expr) {
self.insert(expr.span, expr.id, Node::Expr(expr));
self.with_parent(expr.id, |this| {
intravisit::walk_expr(this, expr);
});
}
fn visit_stmt(&mut self, stmt: &'hir Stmt) {
let id = stmt.id;
self.insert(stmt.span, id, Node::Stmt(stmt));
self.with_parent(id, |this| {
intravisit::walk_stmt(this, stmt);
});
}
fn visit_path_segment(&mut self, path_span: Span, path_segment: &'hir PathSegment) {
if let Some(id) = path_segment.id {
self.insert(path_span, id, Node::PathSegment(path_segment));
}
intravisit::walk_path_segment(self, path_span, path_segment);
}
fn visit_ty(&mut self, ty: &'hir Ty) {
self.insert(ty.span, ty.id, Node::Ty(ty));
self.with_parent(ty.id, |this| {
intravisit::walk_ty(this, ty);
});
}
fn visit_trait_ref(&mut self, tr: &'hir TraitRef) {
self.insert(tr.path.span, tr.ref_id, Node::TraitRef(tr));
self.with_parent(tr.ref_id, |this| {
intravisit::walk_trait_ref(this, tr);
});
}
fn visit_fn(&mut self, fk: intravisit::FnKind<'hir>, fd: &'hir FnDecl,
b: BodyId, s: Span, id: NodeId) {
assert_eq!(self.parent_node, id);
intravisit::walk_fn(self, fk, fd, b, s, id);
}
fn visit_block(&mut self, block: &'hir Block) {
self.insert(block.span, block.id, Node::Block(block));
self.with_parent(block.id, |this| {
intravisit::walk_block(this, block);
});
}
fn visit_local(&mut self, l: &'hir Local) {
self.insert(l.span, l.id, Node::Local(l));
self.with_parent(l.id, |this| {
intravisit::walk_local(this, l)
})
}
fn visit_lifetime(&mut self, lifetime: &'hir Lifetime) {
self.insert(lifetime.span, lifetime.id, Node::Lifetime(lifetime));
}
fn visit_vis(&mut self, visibility: &'hir Visibility) {
match visibility.node {
VisibilityKind::Public |
VisibilityKind::Crate(_) |
VisibilityKind::Inherited => {}
VisibilityKind::Restricted { id, .. } => {
self.insert(visibility.span, id, Node::Visibility(visibility));
self.with_parent(id, |this| {
intravisit::walk_vis(this, visibility);
});
}
}
}
fn visit_macro_def(&mut self, macro_def: &'hir MacroDef) {
let def_index = self.definitions.opt_def_index(macro_def.id).unwrap();
self.with_dep_node_owner(def_index, macro_def, |this| {
this.insert(macro_def.span, macro_def.id, Node::MacroDef(macro_def));
});
}
fn visit_variant(&mut self, v: &'hir Variant, g: &'hir Generics, item_id: NodeId) {
let id = v.node.data.id();
self.insert(v.span, id, Node::Variant(v));
self.with_parent(id, |this| {
intravisit::walk_variant(this, v, g, item_id);
});
}
fn visit_struct_field(&mut self, field: &'hir StructField) {
self.insert(field.span, field.id, Node::Field(field));
self.with_parent(field.id, |this| {
intravisit::walk_struct_field(this, field);
});
}
fn visit_trait_item_ref(&mut self, ii: &'hir TraitItemRef) {
// Do not visit the duplicate information in TraitItemRef. We want to
// map the actual nodes, not the duplicate ones in the *Ref.
let TraitItemRef {
id,
ident: _,
kind: _,
span: _,
defaultness: _,
} = *ii;
self.visit_nested_trait_item(id);
}
fn visit_impl_item_ref(&mut self, ii: &'hir ImplItemRef) {
// Do not visit the duplicate information in ImplItemRef. We want to
// map the actual nodes, not the duplicate ones in the *Ref.
let ImplItemRef {
id,
ident: _,
kind: _,
span: _,
vis: _,
defaultness: _,
} = *ii;
self.visit_nested_impl_item(id);
}
}
// This is a wrapper structure that allows determining if span values within
// the wrapped item should be hashed or not.
struct HirItemLike<T> {
item_like: T,
hash_bodies: bool,
}
impl<'a, 'hir, T> HashStable<StableHashingContext<'hir>> for HirItemLike<T>
where T: HashStable<StableHashingContext<'hir>>
{
fn hash_stable<W: StableHasherResult>(&self,
hcx: &mut StableHashingContext<'hir>,
hasher: &mut StableHasher<W>) {
hcx.while_hashing_hir_bodies(self.hash_bodies, |hcx| {
self.item_like.hash_stable(hcx, hasher);
});
}
}