9
9
// except according to those terms.
10
10
11
11
use hir::map::definitions::*;
12
-
13
- use hir;
14
- use hir::intravisit::{self, Visitor, NestedVisitorMap};
15
12
use hir::def_id::{CRATE_DEF_INDEX, DefIndex};
16
13
17
- use middle::cstore::InlinedItem;
18
-
19
14
use syntax::ast::*;
20
15
use syntax::ext::hygiene::Mark;
21
16
use syntax::visit;
22
17
use syntax::symbol::{Symbol, keywords};
23
18
24
19
/// Creates def ids for nodes in the HIR.
25
20
pub struct DefCollector<'a> {
26
- // If we are walking HIR (c.f., AST), we need to keep a reference to the
27
- // crate.
28
- hir_crate: Option<&'a hir::Crate>,
29
21
definitions: &'a mut Definitions,
30
22
parent_def: Option<DefIndex>,
31
23
pub visit_macro_invoc: Option<&'a mut FnMut(MacroInvocationData)>,
@@ -40,7 +32,6 @@ pub struct MacroInvocationData {
40
32
impl<'a> DefCollector<'a> {
41
33
pub fn new(definitions: &'a mut Definitions) -> Self {
42
34
DefCollector {
43
- hir_crate: None,
44
35
definitions: definitions,
45
36
parent_def: None,
46
37
visit_macro_invoc: None,
@@ -51,13 +42,6 @@ impl<'a> DefCollector<'a> {
51
42
let root = self.create_def_with_parent(None, CRATE_NODE_ID, DefPathData::CrateRoot);
52
43
assert_eq!(root, CRATE_DEF_INDEX);
53
44
self.parent_def = Some(root);
54
-
55
- self.create_def_with_parent(Some(CRATE_DEF_INDEX), DUMMY_NODE_ID, DefPathData::Misc);
56
- }
57
-
58
- pub fn walk_item(&mut self, ii: &'a InlinedItem, krate: &'a hir::Crate) {
59
- self.hir_crate = Some(krate);
60
- ii.visit(self);
61
45
}
62
46
63
47
fn create_def(&mut self, node_id: NodeId, data: DefPathData) -> DefIndex {
@@ -95,16 +79,6 @@ impl<'a> DefCollector<'a> {
95
79
self.create_def(expr.id, DefPathData::Initializer);
96
80
}
97
81
98
- fn visit_hir_const_integer(&mut self, expr: &hir::Expr) {
99
- // FIXME(eddyb) Closures should have separate
100
- // function definition IDs and expression IDs.
101
- if let hir::ExprClosure(..) = expr.node {
102
- return;
103
- }
104
-
105
- self.create_def(expr.id, DefPathData::Initializer);
106
- }
107
-
108
82
fn visit_macro_invoc(&mut self, id: NodeId, const_integer: bool) {
109
83
if let Some(ref mut visit) = self.visit_macro_invoc {
110
84
visit(MacroInvocationData {
@@ -305,169 +279,3 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
305
279
}
306
280
}
307
281
}
308
-
309
- // We walk the HIR rather than the AST when reading items from metadata.
310
- impl<'ast> Visitor<'ast> for DefCollector<'ast> {
311
- fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'ast> {
312
- // note however that we override `visit_body` below
313
- NestedVisitorMap::None
314
- }
315
-
316
- fn visit_body(&mut self, id: hir::ExprId) {
317
- if let Some(krate) = self.hir_crate {
318
- self.visit_expr(krate.expr(id));
319
- }
320
- }
321
-
322
- fn visit_item(&mut self, i: &'ast hir::Item) {
323
- debug!("visit_item: {:?}", i);
324
-
325
- // Pick the def data. This need not be unique, but the more
326
- // information we encapsulate into
327
- let def_data = match i.node {
328
- hir::ItemDefaultImpl(..) | hir::ItemImpl(..) =>
329
- DefPathData::Impl,
330
- hir::ItemEnum(..) | hir::ItemStruct(..) | hir::ItemUnion(..) |
331
- hir::ItemTrait(..) | hir::ItemExternCrate(..) | hir::ItemMod(..) |
332
- hir::ItemForeignMod(..) | hir::ItemTy(..) =>
333
- DefPathData::TypeNs(i.name.as_str()),
334
- hir::ItemStatic(..) | hir::ItemConst(..) | hir::ItemFn(..) =>
335
- DefPathData::ValueNs(i.name.as_str()),
336
- hir::ItemUse(..) => DefPathData::Misc,
337
- };
338
- let def = self.create_def(i.id, def_data);
339
-
340
- self.with_parent(def, |this| {
341
- match i.node {
342
- hir::ItemEnum(ref enum_definition, _) => {
343
- for v in &enum_definition.variants {
344
- let variant_def_index =
345
- this.create_def(v.node.data.id(),
346
- DefPathData::EnumVariant(v.node.name.as_str()));
347
-
348
- this.with_parent(variant_def_index, |this| {
349
- for field in v.node.data.fields() {
350
- this.create_def(field.id,
351
- DefPathData::Field(field.name.as_str()));
352
- }
353
- if let Some(ref expr) = v.node.disr_expr {
354
- this.visit_hir_const_integer(expr);
355
- }
356
- });
357
- }
358
- }
359
- hir::ItemStruct(ref struct_def, _) |
360
- hir::ItemUnion(ref struct_def, _) => {
361
- // If this is a tuple-like struct, register the constructor.
362
- if !struct_def.is_struct() {
363
- this.create_def(struct_def.id(),
364
- DefPathData::StructCtor);
365
- }
366
-
367
- for field in struct_def.fields() {
368
- this.create_def(field.id, DefPathData::Field(field.name.as_str()));
369
- }
370
- }
371
- _ => {}
372
- }
373
- intravisit::walk_item(this, i);
374
- });
375
- }
376
-
377
- fn visit_foreign_item(&mut self, foreign_item: &'ast hir::ForeignItem) {
378
- let def = self.create_def(foreign_item.id,
379
- DefPathData::ValueNs(foreign_item.name.as_str()));
380
-
381
- self.with_parent(def, |this| {
382
- intravisit::walk_foreign_item(this, foreign_item);
383
- });
384
- }
385
-
386
- fn visit_generics(&mut self, generics: &'ast hir::Generics) {
387
- for ty_param in generics.ty_params.iter() {
388
- self.create_def(ty_param.id, DefPathData::TypeParam(ty_param.name.as_str()));
389
- }
390
-
391
- intravisit::walk_generics(self, generics);
392
- }
393
-
394
- fn visit_trait_item(&mut self, ti: &'ast hir::TraitItem) {
395
- let def_data = match ti.node {
396
- hir::MethodTraitItem(..) | hir::ConstTraitItem(..) =>
397
- DefPathData::ValueNs(ti.name.as_str()),
398
- hir::TypeTraitItem(..) => DefPathData::TypeNs(ti.name.as_str()),
399
- };
400
-
401
- let def = self.create_def(ti.id, def_data);
402
- self.with_parent(def, |this| {
403
- if let hir::ConstTraitItem(_, Some(ref expr)) = ti.node {
404
- this.create_def(expr.id, DefPathData::Initializer);
405
- }
406
-
407
- intravisit::walk_trait_item(this, ti);
408
- });
409
- }
410
-
411
- fn visit_impl_item(&mut self, ii: &'ast hir::ImplItem) {
412
- let def_data = match ii.node {
413
- hir::ImplItemKind::Method(..) | hir::ImplItemKind::Const(..) =>
414
- DefPathData::ValueNs(ii.name.as_str()),
415
- hir::ImplItemKind::Type(..) => DefPathData::TypeNs(ii.name.as_str()),
416
- };
417
-
418
- let def = self.create_def(ii.id, def_data);
419
- self.with_parent(def, |this| {
420
- if let hir::ImplItemKind::Const(_, ref expr) = ii.node {
421
- this.create_def(expr.id, DefPathData::Initializer);
422
- }
423
-
424
- intravisit::walk_impl_item(this, ii);
425
- });
426
- }
427
-
428
- fn visit_pat(&mut self, pat: &'ast hir::Pat) {
429
- let parent_def = self.parent_def;
430
-
431
- if let hir::PatKind::Binding(_, _, name, _) = pat.node {
432
- let def = self.create_def(pat.id, DefPathData::Binding(name.node.as_str()));
433
- self.parent_def = Some(def);
434
- }
435
-
436
- intravisit::walk_pat(self, pat);
437
- self.parent_def = parent_def;
438
- }
439
-
440
- fn visit_expr(&mut self, expr: &'ast hir::Expr) {
441
- let parent_def = self.parent_def;
442
-
443
- if let hir::ExprRepeat(_, ref count) = expr.node {
444
- self.visit_hir_const_integer(count);
445
- }
446
-
447
- if let hir::ExprClosure(..) = expr.node {
448
- let def = self.create_def(expr.id, DefPathData::ClosureExpr);
449
- self.parent_def = Some(def);
450
- }
451
-
452
- intravisit::walk_expr(self, expr);
453
- self.parent_def = parent_def;
454
- }
455
-
456
- fn visit_ty(&mut self, ty: &'ast hir::Ty) {
457
- if let hir::TyArray(_, ref length) = ty.node {
458
- self.visit_hir_const_integer(length);
459
- }
460
- if let hir::TyImplTrait(..) = ty.node {
461
- self.create_def(ty.id, DefPathData::ImplTrait);
462
- }
463
- intravisit::walk_ty(self, ty);
464
- }
465
-
466
- fn visit_lifetime_def(&mut self, def: &'ast hir::LifetimeDef) {
467
- self.create_def(def.lifetime.id, DefPathData::LifetimeDef(def.lifetime.name.as_str()));
468
- }
469
-
470
- fn visit_macro_def(&mut self, macro_def: &'ast hir::MacroDef) {
471
- self.create_def(macro_def.id, DefPathData::MacroDef(macro_def.name.as_str()));
472
- }
473
- }
0 commit comments