Skip to content

Commit

Permalink
Move def collector from rustc to rustc_resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Nov 23, 2019
1 parent 9420ff4 commit bbbdbb0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
11 changes: 10 additions & 1 deletion src/librustc/hir/map/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub struct Definitions {
/// we know what parent node that fragment should be attached to thanks to this table.
invocation_parents: FxHashMap<ExpnId, DefIndex>,
/// Indices of unnamed struct or variant fields with unresolved attributes.
pub(super) placeholder_field_indices: NodeMap<usize>,
placeholder_field_indices: NodeMap<usize>,
}

/// A unique identifier that we can use to lookup a definition
Expand Down Expand Up @@ -535,6 +535,15 @@ impl Definitions {
let old_parent = self.invocation_parents.insert(invoc_id, parent);
assert!(old_parent.is_none(), "parent `DefIndex` is reset for an invocation");
}

pub fn placeholder_field_index(&self, node_id: ast::NodeId) -> usize {
self.placeholder_field_indices[&node_id]
}

pub fn set_placeholder_field_index(&mut self, node_id: ast::NodeId, index: usize) {
let old_index = self.placeholder_field_indices.insert(node_id, index);
assert!(old_index.is_none(), "placeholder field index is reset for a node ID");
}
}

impl DefPathData {
Expand Down
2 changes: 0 additions & 2 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use self::collector::NodeCollector;
pub use self::def_collector::DefCollector;
pub use self::definitions::{
Definitions, DefKey, DefPath, DefPathData, DisambiguatedDefPathData, DefPathHash
};
Expand All @@ -25,7 +24,6 @@ use syntax_pos::{Span, DUMMY_SP};

pub mod blocks;
mod collector;
mod def_collector;
pub mod definitions;
mod hir_id_validator;

Expand Down
5 changes: 2 additions & 3 deletions src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! unexpanded macros in the fragment are visited and registered.
//! Imports are also considered items and placed into modules here, but not resolved yet.

use crate::def_collector::collect_definitions;
use crate::macros::{LegacyBinding, LegacyScope};
use crate::resolve_imports::ImportDirective;
use crate::resolve_imports::ImportDirectiveSubclass::{self, GlobImport, SingleImport};
Expand All @@ -16,7 +17,6 @@ use crate::{ResolutionError, Determinacy, PathResult, CrateLint};
use rustc::bug;
use rustc::hir::def::{self, *};
use rustc::hir::def_id::{CRATE_DEF_INDEX, LOCAL_CRATE, DefId};
use rustc::hir::map::DefCollector;
use rustc::ty;
use rustc::middle::cstore::CrateStore;
use rustc_metadata::cstore::LoadedMacro;
Expand Down Expand Up @@ -167,8 +167,7 @@ impl<'a> Resolver<'a> {
fragment: &AstFragment,
parent_scope: ParentScope<'a>,
) -> LegacyScope<'a> {
let mut def_collector = DefCollector::new(&mut self.definitions, parent_scope.expansion);
fragment.visit_with(&mut def_collector);
collect_definitions(&mut self.definitions, fragment, parent_scope.expansion);
let mut visitor = BuildReducedGraphVisitor { r: self, parent_scope };
fragment.visit_with(&mut visitor);
visitor.parent_scope.legacy
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
use crate::hir::map::definitions::*;
use crate::hir::def_id::DefIndex;

use log::debug;
use rustc::hir::map::definitions::*;
use rustc::hir::def_id::DefIndex;
use syntax::ast::*;
use syntax::visit;
use syntax::symbol::{kw, sym};
use syntax::token::{self, Token};
use syntax_expand::expand::AstFragment;
use syntax_pos::hygiene::ExpnId;
use syntax_pos::Span;

crate fn collect_definitions(
definitions: &mut Definitions,
fragment: &AstFragment,
expansion: ExpnId,
) {
let parent_def = definitions.invocation_parent(expansion);
fragment.visit_with(&mut DefCollector { definitions, parent_def, expansion });
}

/// Creates `DefId`s for nodes in the AST.
pub struct DefCollector<'a> {
struct DefCollector<'a> {
definitions: &'a mut Definitions,
parent_def: DefIndex,
expansion: ExpnId,
}

impl<'a> DefCollector<'a> {
pub fn new(definitions: &'a mut Definitions, expansion: ExpnId) -> Self {
let parent_def = definitions.invocation_parent(expansion);
DefCollector { definitions, parent_def, expansion }
}

fn create_def(&mut self,
node_id: NodeId,
data: DefPathData,
Expand Down Expand Up @@ -82,7 +87,7 @@ impl<'a> DefCollector<'a> {
.or_else(|| index.map(sym::integer))
.unwrap_or_else(|| {
let node_id = NodeId::placeholder_from_expn_id(self.expansion);
sym::integer(self.definitions.placeholder_field_indices[&node_id])
sym::integer(self.definitions.placeholder_field_index(node_id))
});
let def = self.create_def(field.id, DefPathData::ValueNs(name), field.span);
self.with_parent(def, |this| visit::walk_struct_field(this, field));
Expand Down Expand Up @@ -186,7 +191,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
for (index, field) in data.fields().iter().enumerate() {
self.collect_field(field, Some(index));
if field.is_placeholder && field.ident.is_none() {
self.definitions.placeholder_field_indices.insert(field.id, index);
self.definitions.set_placeholder_field_index(field.id, index);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ use rustc_error_codes::*;

type Res = def::Res<NodeId>;

mod def_collector;
mod diagnostics;
mod late;
mod macros;
Expand Down

0 comments on commit bbbdbb0

Please sign in to comment.