Move def collector from rustc to rustc_resolve

This commit is contained in:
Vadim Petrochenkov 2019-11-23 18:19:57 +03:00
parent 9420ff4c0e
commit bbbdbb0e44
5 changed files with 29 additions and 17 deletions

View file

@ -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
@ -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 {

View file

@ -1,5 +1,4 @@
use self::collector::NodeCollector;
pub use self::def_collector::DefCollector;
pub use self::definitions::{
Definitions, DefKey, DefPath, DefPathData, DisambiguatedDefPathData, DefPathHash
};
@ -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;

View file

@ -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};
@ -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;
@ -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

View file

@ -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,
@ -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));
@ -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);
}
}
}

View file

@ -68,6 +68,7 @@ use rustc_error_codes::*;
type Res = def::Res<NodeId>;
mod def_collector;
mod diagnostics;
mod late;
mod macros;