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,
};
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;
}
}
// 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,
pub_count: Cell::new(0),
pub_glob_count: Cell::new(0),
populated: Cell::new(!external),
+ arenas: arenas
}
}
}
// 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);
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;
}
// 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>),
}
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)
}
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,
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,
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)
}
// 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);
}
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();
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 {