Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve: groundwork for building the module graph during expansion #36573

Merged
merged 6 commits into from
Sep 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 13 additions & 20 deletions src/librustc/hir/map/def_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,20 @@ pub struct DefCollector<'ast> {
}

impl<'ast> DefCollector<'ast> {
pub fn root(definitions: &'ast mut Definitions) -> DefCollector<'ast> {
let mut collector = DefCollector {
pub fn new(definitions: &'ast mut Definitions) -> DefCollector<'ast> {
DefCollector {
hir_crate: None,
definitions: definitions,
parent_def: None,
};
let root = collector.create_def_with_parent(None, CRATE_NODE_ID, DefPathData::CrateRoot);
assert_eq!(root, CRATE_DEF_INDEX);
collector.parent_def = Some(root);

collector.create_def_with_parent(Some(CRATE_DEF_INDEX), DUMMY_NODE_ID, DefPathData::Misc);

collector
}
}

pub fn extend(parent_node: NodeId,
parent_def_path: DefPath,
parent_def_id: DefId,
definitions: &'ast mut Definitions)
-> DefCollector<'ast> {
let mut collector = DefCollector {
hir_crate: None,
parent_def: None,
definitions: definitions,
};
let mut collector = DefCollector::new(definitions);

assert_eq!(parent_def_path.krate, parent_def_id.krate);
let root_path = Box::new(InlinedRootPath {
Expand All @@ -68,17 +57,21 @@ impl<'ast> DefCollector<'ast> {
collector
}

pub fn collect_root(&mut self) {
let root = self.create_def_with_parent(None, CRATE_NODE_ID, DefPathData::CrateRoot);
assert_eq!(root, CRATE_DEF_INDEX);
self.parent_def = Some(root);

self.create_def_with_parent(Some(CRATE_DEF_INDEX), DUMMY_NODE_ID, DefPathData::Misc);
}

pub fn walk_item(&mut self, ii: &'ast InlinedItem, krate: &'ast hir::Crate) {
self.hir_crate = Some(krate);
ii.visit(self);
}

fn parent_def(&self) -> Option<DefIndex> {
self.parent_def
}

fn create_def(&mut self, node_id: NodeId, data: DefPathData) -> DefIndex {
let parent_def = self.parent_def();
let parent_def = self.parent_def;
debug!("create_def(node_id={:?}, data={:?}, parent_def={:?})", node_id, data, parent_def);
self.definitions.create_def_with_parent(parent_def, node_id, data)
}
Expand Down
3 changes: 2 additions & 1 deletion src/librustc/hir/map/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ impl Definitions {
}

pub fn collect(&mut self, krate: &ast::Crate) {
let mut def_collector = DefCollector::root(self);
let mut def_collector = DefCollector::new(self);
def_collector.collect_root();
visit::walk_crate(&mut def_collector, krate);
}

Expand Down
3 changes: 2 additions & 1 deletion src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,8 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
macro_import::MacroLoader::new(sess, &cstore, crate_name, krate.config.clone());

let resolver_arenas = Resolver::arenas();
let mut resolver = Resolver::new(sess, make_glob_map, &mut macro_loader, &resolver_arenas);
let mut resolver =
Resolver::new(sess, &krate, make_glob_map, &mut macro_loader, &resolver_arenas);
syntax_ext::register_builtins(&mut resolver, sess.features.borrow().quote);

krate = time(time_passes, "expansion", || {
Expand Down
105 changes: 54 additions & 51 deletions src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
//! any imports resolved.

use resolve_imports::ImportDirectiveSubclass::{self, GlobImport};
use Module;
use {Module, ModuleS, ModuleKind};
use Namespace::{self, TypeNS, ValueNS};
use {NameBinding, NameBindingKind, ToNameBinding};
use ParentLink::{ModuleParentLink, BlockParentLink};
use Resolver;
use {resolve_error, resolve_struct_error, ResolutionError};

Expand All @@ -34,7 +33,7 @@ use syntax::parse::token;

use syntax::ast::{Block, Crate};
use syntax::ast::{ForeignItem, ForeignItemKind, Item, ItemKind};
use syntax::ast::{Mutability, StmtKind, TraitItemKind};
use syntax::ast::{Mutability, StmtKind, TraitItem, TraitItemKind};
use syntax::ast::{Variant, ViewPathGlob, ViewPathList, ViewPathSimple};
use syntax::parse::token::keywords;
use syntax::visit::{self, Visitor};
Expand All @@ -56,8 +55,6 @@ impl<'a> ToNameBinding<'a> for (Def, Span, ty::Visibility) {
impl<'b> Resolver<'b> {
/// Constructs the reduced graph for the entire crate.
pub fn build_reduced_graph(&mut self, krate: &Crate) {
let no_implicit_prelude = attr::contains_name(&krate.attrs, "no_implicit_prelude");
self.graph_root.no_implicit_prelude.set(no_implicit_prelude);
visit::walk_crate(&mut BuildReducedGraphVisitor { resolver: self }, krate);
}

Expand Down Expand Up @@ -196,22 +193,25 @@ impl<'b> Resolver<'b> {
krate: crate_id,
index: CRATE_DEF_INDEX,
};
let parent_link = ModuleParentLink(parent, name);
let def = Def::Mod(def_id);
let module = self.new_extern_crate_module(parent_link, def, item.id);
let module = self.arenas.alloc_module(ModuleS {
extern_crate_id: Some(item.id),
populated: Cell::new(false),
..ModuleS::new(Some(parent), ModuleKind::Def(Def::Mod(def_id), name))
});
self.define(parent, name, TypeNS, (module, sp, vis));

self.populate_module_if_necessary(module);
}
}

ItemKind::Mod(..) => {
let parent_link = ModuleParentLink(parent, name);
let def = Def::Mod(self.definitions.local_def_id(item.id));
let module = self.new_module(parent_link, Some(def), Some(item.id));
module.no_implicit_prelude.set({
parent.no_implicit_prelude.get() ||
let module = self.arenas.alloc_module(ModuleS {
no_implicit_prelude: parent.no_implicit_prelude || {
attr::contains_name(&item.attrs, "no_implicit_prelude")
},
normal_ancestor_id: Some(item.id),
..ModuleS::new(Some(parent), ModuleKind::Def(def, name))
});
self.define(parent, name, TypeNS, (module, sp, vis));
self.module_map.insert(item.id, module);
Expand Down Expand Up @@ -244,9 +244,8 @@ impl<'b> Resolver<'b> {
}

ItemKind::Enum(ref enum_definition, _) => {
let parent_link = ModuleParentLink(parent, name);
let def = Def::Enum(self.definitions.local_def_id(item.id));
let module = self.new_module(parent_link, Some(def), parent.normal_ancestor_id);
let module = self.new_module(parent, ModuleKind::Def(def, name), true);
self.define(parent, name, TypeNS, (module, sp, vis));

for variant in &(*enum_definition).variants {
Expand Down Expand Up @@ -293,40 +292,17 @@ impl<'b> Resolver<'b> {

ItemKind::DefaultImpl(..) | ItemKind::Impl(..) => {}

ItemKind::Trait(.., ref items) => {
ItemKind::Trait(..) => {
let def_id = self.definitions.local_def_id(item.id);

// Add all the items within to a new module.
let parent_link = ModuleParentLink(parent, name);
let def = Def::Trait(def_id);
let module_parent =
self.new_module(parent_link, Some(def), parent.normal_ancestor_id);
self.define(parent, name, TypeNS, (module_parent, sp, vis));

// Add the names of all the items to the trait info.
for item in items {
let item_def_id = self.definitions.local_def_id(item.id);
let mut is_static_method = false;
let (def, ns) = match item.node {
TraitItemKind::Const(..) => (Def::AssociatedConst(item_def_id), ValueNS),
TraitItemKind::Method(ref sig, _) => {
is_static_method = !sig.decl.has_self();
(Def::Method(item_def_id), ValueNS)
}
TraitItemKind::Type(..) => (Def::AssociatedTy(item_def_id), TypeNS),
TraitItemKind::Macro(_) => panic!("unexpanded macro in resolve!"),
};

self.define(module_parent, item.ident.name, ns, (def, item.span, vis));

self.trait_item_map.insert((item.ident.name, def_id), is_static_method);
}
let module =
self.new_module(parent, ModuleKind::Def(Def::Trait(def_id), name), true);
self.define(parent, name, TypeNS, (module, sp, vis));
self.current_module = module;
}
ItemKind::Mac(_) => panic!("unexpanded macro in resolve!"),
}

visit::walk_item(&mut BuildReducedGraphVisitor { resolver: self }, item);
self.current_module = parent;
}

// Constructs the reduced graph for one variant. Variants exist in the
Expand Down Expand Up @@ -375,14 +351,10 @@ impl<'b> Resolver<'b> {
{}",
block_id);

let parent_link = BlockParentLink(parent, block_id);
let new_module = self.new_module(parent_link, None, parent.normal_ancestor_id);
let new_module = self.new_module(parent, ModuleKind::Block(block_id), true);
self.module_map.insert(block_id, new_module);
self.current_module = new_module; // Descend into the block.
}

visit::walk_block(&mut BuildReducedGraphVisitor { resolver: self }, block);
self.current_module = parent;
}

/// Builds the reduced graph for a single item in an external crate.
Expand All @@ -407,8 +379,7 @@ impl<'b> Resolver<'b> {
Def::Mod(_) | Def::Enum(..) => {
debug!("(building reduced graph for external crate) building module {} {:?}",
name, vis);
let parent_link = ModuleParentLink(parent, name);
let module = self.new_module(parent_link, Some(def), None);
let module = self.new_module(parent, ModuleKind::Def(def, name), false);
let _ = self.try_define(parent, name, TypeNS, (module, DUMMY_SP, vis));
}
Def::Variant(variant_id) => {
Expand Down Expand Up @@ -451,8 +422,7 @@ impl<'b> Resolver<'b> {
self.trait_item_map.insert((trait_item_name, def_id), false);
}

let parent_link = ModuleParentLink(parent, name);
let module = self.new_module(parent_link, Some(def), None);
let module = self.new_module(parent, ModuleKind::Def(def, name), false);
let _ = self.try_define(parent, name, TypeNS, (module, DUMMY_SP, vis));
}
Def::TyAlias(..) | Def::AssociatedTy(..) => {
Expand Down Expand Up @@ -512,14 +482,47 @@ struct BuildReducedGraphVisitor<'a, 'b: 'a> {

impl<'a, 'b> Visitor for BuildReducedGraphVisitor<'a, 'b> {
fn visit_item(&mut self, item: &Item) {
let parent = self.resolver.current_module;
self.resolver.build_reduced_graph_for_item(item);
visit::walk_item(self, item);
self.resolver.current_module = parent;
}

fn visit_foreign_item(&mut self, foreign_item: &ForeignItem) {
self.resolver.build_reduced_graph_for_foreign_item(foreign_item);
}

fn visit_block(&mut self, block: &Block) {
let parent = self.resolver.current_module;
self.resolver.build_reduced_graph_for_block(block);
visit::walk_block(self, block);
self.resolver.current_module = parent;
}

fn visit_trait_item(&mut self, item: &TraitItem) {
let parent = self.resolver.current_module;
let def_id = parent.def_id().unwrap();

// Add the item to the trait info.
let item_def_id = self.resolver.definitions.local_def_id(item.id);
let mut is_static_method = false;
let (def, ns) = match item.node {
TraitItemKind::Const(..) => (Def::AssociatedConst(item_def_id), ValueNS),
TraitItemKind::Method(ref sig, _) => {
is_static_method = !sig.decl.has_self();
(Def::Method(item_def_id), ValueNS)
}
TraitItemKind::Type(..) => (Def::AssociatedTy(item_def_id), TypeNS),
TraitItemKind::Macro(_) => panic!("unexpanded macro in resolve!"),
};

self.resolver.trait_item_map.insert((item.ident.name, def_id), is_static_method);

let vis = ty::Visibility::Public;
self.resolver.define(parent, item.ident.name, ns, (def, item.span, vis));

self.resolver.current_module = parent.parent.unwrap(); // nearest normal ancestor
visit::walk_trait_item(self, item);
self.resolver.current_module = parent;
}
}
Loading