提交 fb4710ce 编写于 作者: J Jeffrey Seyfried

Add a field in Module for the ResolverArenas

上级 6b94bc34
......@@ -98,14 +98,14 @@ fn build_reduced_graph(self, krate: &hir::Crate) {
fn try_define<T>(&self, parent: Module<'b>, name: Name, ns: Namespace, def: T)
where T: ToNameBinding<'b>
{
let _ = parent.try_define_child(name, ns, self.new_name_binding(def.to_name_binding()));
let _ = parent.try_define_child(name, ns, def.to_name_binding());
}
/// Defines `name` in namespace `ns` of module `parent` to be `def` if it is not yet defined;
/// otherwise, reports an error.
fn define<T: ToNameBinding<'b>>(&self, parent: Module<'b>, name: Name, ns: Namespace, def: T) {
let binding = self.new_name_binding(def.to_name_binding());
let old_binding = match parent.try_define_child(name, ns, binding) {
let binding = def.to_name_binding();
let old_binding = match parent.try_define_child(name, ns, binding.clone()) {
Ok(()) => return,
Err(old_binding) => old_binding,
};
......@@ -709,8 +709,7 @@ fn build_import_directive(&mut self,
let directive =
ImportDirective::new(module_path, subclass, span, id, is_public, shadowable);
let directive = self.resolver.arenas.alloc_import_directive(directive);
module_.unresolved_imports.borrow_mut().push(directive);
module_.add_import_directive(directive);
self.unresolved_imports += 1;
}
}
......
......@@ -845,13 +845,18 @@ pub struct ModuleS<'a> {
// access the children must be preceded with a
// `populate_module_if_necessary` call.
populated: Cell<bool>,
arenas: &'a ResolverArenas<'a>,
}
pub type Module<'a> = &'a ModuleS<'a>;
impl<'a> ModuleS<'a> {
fn new(parent_link: ParentLink<'a>, def: Option<Def>, external: bool, is_public: bool) -> Self {
fn new(parent_link: ParentLink<'a>,
def: Option<Def>,
external: bool,
is_public: bool,
arenas: &'a ResolverArenas<'a>) -> Self {
ModuleS {
parent_link: parent_link,
def: def,
......@@ -865,6 +870,7 @@ fn new(parent_link: ParentLink<'a>, def: Option<Def>, external: bool, is_public:
pub_count: Cell::new(0),
pub_glob_count: Cell::new(0),
populated: Cell::new(!external),
arenas: arenas
}
}
......@@ -881,8 +887,9 @@ fn resolve_name(&self, name: Name, ns: Namespace, allow_private_imports: bool)
}
// Define the name or return the existing binding if there is a collision.
fn try_define_child(&self, name: Name, ns: Namespace, binding: &'a NameBinding<'a>)
fn try_define_child(&self, name: Name, ns: Namespace, binding: NameBinding<'a>)
-> Result<(), &'a NameBinding<'a>> {
let binding = self.arenas.alloc_name_binding(binding);
let mut children = self.resolutions.borrow_mut();
let resolution = children.entry((name, ns)).or_insert_with(Default::default);
......@@ -898,6 +905,11 @@ fn try_define_child(&self, name: Name, ns: Namespace, binding: &'a NameBinding<'
resolution.try_define(binding)
}
fn add_import_directive(&self, import_directive: ImportDirective) {
let import_directive = self.arenas.alloc_import_directive(import_directive);
self.unresolved_imports.borrow_mut().push(import_directive);
}
fn increment_outstanding_references_for(&self, name: Name, ns: Namespace) {
let mut children = self.resolutions.borrow_mut();
children.entry((name, ns)).or_insert_with(Default::default).outstanding_references += 1;
......@@ -995,14 +1007,14 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
}
// Records a possibly-private value, type, or module definition.
#[derive(Debug)]
#[derive(Clone, Debug)]
pub struct NameBinding<'a> {
modifiers: DefModifiers,
kind: NameBindingKind<'a>,
span: Option<Span>,
}
#[derive(Debug)]
#[derive(Clone, Debug)]
enum NameBindingKind<'a> {
Def(Def),
Module(Module<'a>),
......@@ -1171,6 +1183,12 @@ pub struct ResolverArenas<'a> {
}
impl<'a> ResolverArenas<'a> {
fn alloc_module(&'a self, module: ModuleS<'a>) -> Module<'a> {
self.modules.alloc(module)
}
fn alloc_name_binding(&'a self, name_binding: NameBinding<'a>) -> &'a NameBinding<'a> {
self.name_bindings.alloc(name_binding)
}
fn alloc_import_directive(&'a self, import_directive: ImportDirective) -> &'a ImportDirective {
self.import_directives.alloc(import_directive)
}
......@@ -1189,8 +1207,9 @@ fn new(session: &'a Session,
arenas: &'a ResolverArenas<'a>)
-> Resolver<'a, 'tcx> {
let root_def_id = ast_map.local_def_id(CRATE_NODE_ID);
let graph_root = ModuleS::new(NoParentLink, Some(Def::Mod(root_def_id)), false, true);
let graph_root = arenas.modules.alloc(graph_root);
let graph_root =
ModuleS::new(NoParentLink, Some(Def::Mod(root_def_id)), false, true, arenas);
let graph_root = arenas.alloc_module(graph_root);
Resolver {
session: session,
......@@ -1250,11 +1269,7 @@ fn new_module(&self,
def: Option<Def>,
external: bool,
is_public: bool) -> Module<'a> {
self.arenas.modules.alloc(ModuleS::new(parent_link, def, external, is_public))
}
fn new_name_binding(&self, name_binding: NameBinding<'a>) -> &'a NameBinding<'a> {
self.arenas.name_bindings.alloc(name_binding)
self.arenas.alloc_module(ModuleS::new(parent_link, def, external, is_public, self.arenas))
}
fn new_extern_crate_module(&self,
......@@ -1263,7 +1278,7 @@ fn new_extern_crate_module(&self,
is_public: bool,
local_node_id: NodeId)
-> Module<'a> {
let mut module = ModuleS::new(parent_link, Some(def), false, is_public);
let mut module = ModuleS::new(parent_link, Some(def), false, is_public, self.arenas);
module.extern_crate_id = Some(local_node_id);
self.arenas.modules.alloc(module)
}
......
......@@ -236,15 +236,14 @@ fn import_resolving_error(&self, e: ImportResolvingError<'b>) {
// If it's a single failed import then create a "fake" import
// resolution for it so that later resolve stages won't complain.
if let SingleImport { target, .. } = e.import_directive.subclass {
let dummy_binding = self.resolver.new_name_binding(NameBinding {
let dummy_binding = self.resolver.arenas.alloc_name_binding(NameBinding {
modifiers: DefModifiers::PRELUDE,
kind: NameBindingKind::Def(Def::Err),
span: None,
});
let dummy_binding =
self.resolver.new_name_binding(e.import_directive.import(dummy_binding, None));
let dummy_binding = e.import_directive.import(dummy_binding, None);
let _ = e.source_module.try_define_child(target, ValueNS, dummy_binding);
let _ = e.source_module.try_define_child(target, ValueNS, dummy_binding.clone());
let _ = e.source_module.try_define_child(target, TypeNS, dummy_binding);
}
......@@ -534,9 +533,8 @@ fn define(&mut self,
name: Name,
ns: Namespace,
binding: NameBinding<'b>) {
let binding = self.resolver.new_name_binding(binding);
if let Err(old_binding) = parent.try_define_child(name, ns, binding) {
self.report_conflict(name, ns, binding, old_binding);
if let Err(old_binding) = parent.try_define_child(name, ns, binding.clone()) {
self.report_conflict(name, ns, &binding, old_binding);
} else if binding.is_public() { // Add to the export map
if let (Some(parent_def_id), Some(def)) = (parent.def_id(), binding.def()) {
let parent_node_id = self.resolver.ast_map.as_local_node_id(parent_def_id).unwrap();
......@@ -549,8 +547,8 @@ fn define(&mut self,
fn report_conflict(&mut self,
name: Name,
ns: Namespace,
binding: &'b NameBinding<'b>,
old_binding: &'b NameBinding<'b>) {
binding: &NameBinding,
old_binding: &NameBinding) {
// Error on the second of two conflicting imports
if old_binding.is_import() && binding.is_import() &&
old_binding.span.unwrap().lo > binding.span.unwrap().lo {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册