diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs index ff90e58d184..d645b628aff 100644 --- a/src/librustc/middle/resolve.rs +++ b/src/librustc/middle/resolve.rs @@ -34,6 +34,7 @@ use collections::{HashMap, HashSet}; use std::cell::{Cell, RefCell}; use std::mem::replace; +use std::rc::{Rc, Weak}; use std::strbuf::StrBuf; use std::uint; @@ -125,6 +126,7 @@ enum NamespaceError { /// A NamespaceResult represents the result of resolving an import in /// a particular namespace. The result is either definitely-resolved, /// definitely- unresolved, or unknown. +#[deriving(Clone)] enum NamespaceResult { /// Means that resolve hasn't gathered enough information yet to determine /// whether the name is bound in this namespace. (That is, it hasn't @@ -135,7 +137,7 @@ enum NamespaceResult { UnboundResult, /// Means that resolve has determined that the name is bound in the Module /// argument, and specified by the NameBindings argument. - BoundResult(@Module, @NameBindings) + BoundResult(Rc, Rc) } impl NamespaceResult { @@ -189,14 +191,14 @@ enum ImportDirectiveSubclass { /// The context that we thread through while building the reduced graph. #[deriving(Clone)] enum ReducedGraphParent { - ModuleReducedGraphParent(@Module) + ModuleReducedGraphParent(Rc) } impl ReducedGraphParent { - fn module(&self) -> @Module { + fn module(&self) -> Rc { match *self { - ModuleReducedGraphParent(m) => { - m + ModuleReducedGraphParent(ref m) => { + m.clone() } } } @@ -279,7 +281,7 @@ enum SearchThroughModulesFlag { enum ModulePrefixResult { NoPrefixFound, - PrefixFound(@Module, uint) + PrefixFound(Rc, uint) } #[deriving(Eq)] @@ -326,8 +328,8 @@ fn new(kind: RibKind) -> Rib { /// One import directive. struct ImportDirective { - module_path: Vec , - subclass: @ImportDirectiveSubclass, + module_path: Vec, + subclass: ImportDirectiveSubclass, span: Span, id: NodeId, is_public: bool, // see note in ImportResolution about how to use this @@ -335,7 +337,7 @@ struct ImportDirective { impl ImportDirective { fn new(module_path: Vec , - subclass: @ImportDirectiveSubclass, + subclass: ImportDirectiveSubclass, span: Span, id: NodeId, is_public: bool) @@ -353,12 +355,12 @@ fn new(module_path: Vec , /// The item that an import resolves to. #[deriving(Clone)] struct Target { - target_module: @Module, - bindings: @NameBindings, + target_module: Rc, + bindings: Rc, } impl Target { - fn new(target_module: @Module, bindings: @NameBindings) -> Target { + fn new(target_module: Rc, bindings: Rc) -> Target { Target { target_module: target_module, bindings: bindings @@ -372,63 +374,60 @@ struct ImportResolution { /// should *not* be used whenever resolution is being performed, this is /// only looked at for glob imports statements currently. Privacy testing /// occurs during a later phase of compilation. - is_public: Cell, + is_public: bool, // The number of outstanding references to this name. When this reaches // zero, outside modules can count on the targets being correct. Before // then, all bets are off; future imports could override this name. - outstanding_references: Cell, + outstanding_references: uint, /// The value that this `use` directive names, if there is one. - value_target: RefCell>, + value_target: Option, /// The source node of the `use` directive leading to the value target /// being non-none - value_id: Cell, + value_id: NodeId, /// The type that this `use` directive names, if there is one. - type_target: RefCell>, + type_target: Option, /// The source node of the `use` directive leading to the type target /// being non-none - type_id: Cell, -} - -fn get(cell: &RefCell) -> T { - cell.borrow().clone() + type_id: NodeId, } impl ImportResolution { fn new(id: NodeId, is_public: bool) -> ImportResolution { ImportResolution { - type_id: Cell::new(id), - value_id: Cell::new(id), - outstanding_references: Cell::new(0), - value_target: RefCell::new(None), - type_target: RefCell::new(None), - is_public: Cell::new(is_public), + type_id: id, + value_id: id, + outstanding_references: 0, + value_target: None, + type_target: None, + is_public: is_public, } } fn target_for_namespace(&self, namespace: Namespace) -> Option { match namespace { - TypeNS => return self.type_target.borrow().clone(), - ValueNS => return self.value_target.borrow().clone(), + TypeNS => self.type_target.clone(), + ValueNS => self.value_target.clone(), } } fn id(&self, namespace: Namespace) -> NodeId { match namespace { - TypeNS => self.type_id.get(), - ValueNS => self.value_id.get(), + TypeNS => self.type_id, + ValueNS => self.value_id, } } } /// The link from a module up to its nearest parent node. +#[deriving(Clone)] enum ParentLink { NoParentLink, - ModuleParentLink(@Module, Ident), - BlockParentLink(@Module, NodeId) + ModuleParentLink(Weak, Ident), + BlockParentLink(Weak, NodeId) } /// The type of module this is. @@ -448,12 +447,12 @@ struct Module { kind: Cell, is_public: bool, - children: RefCell>, - imports: RefCell >, + children: RefCell>>, + imports: RefCell>, // The external module children of this node that were declared with // `extern crate`. - external_module_children: RefCell>, + external_module_children: RefCell>>, // The anonymous children of this node. Anonymous children are pseudo- // modules that are implicitly created around items contained within @@ -469,10 +468,10 @@ struct Module { // // There will be an anonymous module created around `g` with the ID of the // entry block for `f`. - anonymous_children: RefCell>, + anonymous_children: RefCell>>, // The status of resolving each import in this module. - import_resolutions: RefCell>, + import_resolutions: RefCell>, // The number of unresolved globs that this module exports. glob_count: Cell, @@ -518,7 +517,7 @@ fn all_imports_resolved(&self) -> bool { #[deriving(Clone)] struct TypeNsDef { is_public: bool, // see note in ImportResolution about how to use this - module_def: Option<@Module>, + module_def: Option>, type_def: Option, type_span: Option } @@ -555,8 +554,8 @@ fn define_module(&self, is_public: bool, sp: Span) { // Merges the module with the existing type def or creates a new one. - let module_ = @Module::new(parent_link, def_id, kind, external, - is_public); + let module_ = Rc::new(Module::new(parent_link, def_id, kind, external, + is_public)); let type_def = self.type_def.borrow().clone(); match type_def { None => { @@ -589,11 +588,11 @@ fn set_module_kind(&self, let type_def = self.type_def.borrow().clone(); match type_def { None => { - let module = @Module::new(parent_link, def_id, kind, - external, is_public); + let module = Module::new(parent_link, def_id, kind, + external, is_public); *self.type_def.borrow_mut() = Some(TypeNsDef { is_public: is_public, - module_def: Some(module), + module_def: Some(Rc::new(module)), type_def: None, type_span: None, }); @@ -601,14 +600,14 @@ fn set_module_kind(&self, Some(type_def) => { match type_def.module_def { None => { - let module = @Module::new(parent_link, - def_id, - kind, - external, - is_public); + let module = Module::new(parent_link, + def_id, + kind, + external, + is_public); *self.type_def.borrow_mut() = Some(TypeNsDef { is_public: is_public, - module_def: Some(module), + module_def: Some(Rc::new(module)), type_def: type_def.type_def, type_span: None, }); @@ -653,9 +652,9 @@ fn define_value(&self, def: Def, sp: Span, is_public: bool) { } /// Returns the module node if applicable. - fn get_module_if_available(&self) -> Option<@Module> { + fn get_module_if_available(&self) -> Option> { match *self.type_def.borrow() { - Some(ref type_def) => (*type_def).module_def, + Some(ref type_def) => type_def.module_def.clone(), None => None } } @@ -664,7 +663,7 @@ fn get_module_if_available(&self) -> Option<@Module> { * Returns the module node. Fails if this node does not have a module * definition. */ - fn get_module(&self) -> @Module { + fn get_module(&self) -> Rc { match self.get_module_if_available() { None => { fail!("get_module called on a node with no module \ @@ -684,10 +683,10 @@ fn defined_in_namespace(&self, namespace: Namespace) -> bool { fn defined_in_public_namespace(&self, namespace: Namespace) -> bool { match namespace { TypeNS => match *self.type_def.borrow() { - Some(def) => def.is_public, None => false + Some(ref def) => def.is_public, None => false }, ValueNS => match *self.value_def.borrow() { - Some(def) => def.is_public, None => false + Some(ref def) => def.is_public, None => false } } } @@ -697,12 +696,12 @@ fn def_for_namespace(&self, namespace: Namespace) -> Option { TypeNS => { match *self.type_def.borrow() { None => None, - Some(type_def) => { + Some(ref type_def) => { match type_def.type_def { Some(type_def) => Some(type_def), None => { match type_def.module_def { - Some(module) => { + Some(ref module) => { match module.def_id.get() { Some(did) => Some(DefMod(did)), None => None, @@ -730,13 +729,13 @@ fn span_for_namespace(&self, namespace: Namespace) -> Option { TypeNS => { match *self.type_def.borrow() { None => None, - Some(type_def) => type_def.type_span + Some(ref type_def) => type_def.type_span } } ValueNS => { match *self.value_def.borrow() { None => None, - Some(value_def) => value_def.value_span + Some(ref value_def) => value_def.value_span } } } @@ -801,7 +800,7 @@ fn namespace_error_to_str(ns: NamespaceError) -> &'static str { fn Resolver<'a>(session: &'a Session, lang_items: &'a LanguageItems, crate_span: Span) -> Resolver<'a> { - let graph_root = @NameBindings(); + let graph_root = NameBindings(); graph_root.define_module(NoParentLink, Some(DefId { krate: 0, node: 0 }), @@ -836,7 +835,7 @@ fn Resolver<'a>(session: &'a Session, self_ident: special_idents::self_, type_self_ident: special_idents::type_self, - primitive_type_table: @PrimitiveTypeTable(), + primitive_type_table: PrimitiveTypeTable(), namespaces: vec!(TypeNS, ValueNS), @@ -858,7 +857,7 @@ struct Resolver<'a> { session: &'a Session, lang_items: &'a LanguageItems, - graph_root: @NameBindings, + graph_root: NameBindings, method_set: RefCell>, structs: HashSet, @@ -867,17 +866,17 @@ struct Resolver<'a> { unresolved_imports: uint, // The module that represents the current item scope. - current_module: @Module, + current_module: Rc, // The current set of local scopes, for values. // FIXME #4948: Reuse ribs to avoid allocation. - value_ribs: RefCell>, + value_ribs: RefCell>, // The current set of local scopes, for types. - type_ribs: RefCell>, + type_ribs: RefCell>, // The current set of local scopes, for labels. - label_ribs: RefCell>, + label_ribs: RefCell>, // The trait that the current context can refer to. current_trait_refs: Option >, @@ -888,7 +887,7 @@ struct Resolver<'a> { type_self_ident: Ident, // The idents for the primitive types. - primitive_type_table: @PrimitiveTypeTable, + primitive_type_table: PrimitiveTypeTable, // The four namespaces. namespaces: Vec , @@ -921,10 +920,10 @@ fn visit_item(&mut self, item: &Item, context: ReducedGraphParent) { fn visit_foreign_item(&mut self, foreign_item: &ForeignItem, context: ReducedGraphParent) { self.resolver.build_reduced_graph_for_foreign_item(foreign_item, - context, - |r, c| { + context.clone(), + |r| { let mut v = BuildReducedGraphVisitor{ resolver: r }; - visit::walk_foreign_item(&mut v, foreign_item, c); + visit::walk_foreign_item(&mut v, foreign_item, context.clone()); }) } @@ -982,17 +981,6 @@ fn build_reduced_graph(&mut self, krate: &ast::Crate) { visit::walk_crate(&mut visitor, krate, initial_parent); } - /// Returns the current module tracked by the reduced graph parent. - fn get_module_from_parent(&mut self, - reduced_graph_parent: ReducedGraphParent) - -> @Module { - match reduced_graph_parent { - ModuleReducedGraphParent(module_) => { - return module_; - } - } - } - /** * Adds a new child item to the module definition of the parent node and * returns its corresponding name bindings as well as the current parent. @@ -1009,26 +997,20 @@ fn add_child(&self, duplicate_checking_mode: DuplicateCheckingMode, // For printing errors sp: Span) - -> (@NameBindings, ReducedGraphParent) { + -> Rc { // If this is the immediate descendant of a module, then we add the // child name directly. Otherwise, we create or reuse an anonymous // module and add the child to that. - let module_; - match reduced_graph_parent { - ModuleReducedGraphParent(parent_module) => { - module_ = parent_module; - } - } + let module_ = reduced_graph_parent.module(); // Add or reuse the child. - let new_parent = ModuleReducedGraphParent(module_); - let child_opt = module_.children.borrow().find_copy(&name.name); - match child_opt { + let child = module_.children.borrow().find_copy(&name.name); + match child { None => { - let child = @NameBindings(); - module_.children.borrow_mut().insert(name.name, child); - return (child, new_parent); + let child = Rc::new(NameBindings()); + module_.children.borrow_mut().insert(name.name, child.clone()); + child } Some(child) => { // Enforce the duplicate checking mode: @@ -1105,7 +1087,7 @@ fn add_child(&self, } } } - return (child, new_parent); + child } } } @@ -1145,7 +1127,7 @@ fn get_parent_link(&mut self, parent: ReducedGraphParent, name: Ident) -> ParentLink { match parent { ModuleReducedGraphParent(module_) => { - return ModuleParentLink(module_, name); + return ModuleParentLink(module_.downgrade(), name); } } } @@ -1162,10 +1144,10 @@ fn build_reduced_graph_for_item(&mut self, match item.node { ItemMod(..) => { - let (name_bindings, new_parent) = - self.add_child(ident, parent, ForbidDuplicateModules, sp); + let name_bindings = + self.add_child(ident, parent.clone(), ForbidDuplicateModules, sp); - let parent_link = self.get_parent_link(new_parent, ident); + let parent_link = self.get_parent_link(parent, ident); let def_id = DefId { krate: 0, node: item.id }; name_bindings.define_module(parent_link, Some(def_id), @@ -1181,8 +1163,8 @@ fn build_reduced_graph_for_item(&mut self, // These items live in the value namespace. ItemStatic(_, m, _) => { - let (name_bindings, _) = - self.add_child(ident, parent, ForbidDuplicateValues, sp); + let name_bindings = + self.add_child(ident, parent.clone(), ForbidDuplicateValues, sp); let mutbl = m == ast::MutMutable; name_bindings.define_value @@ -1190,18 +1172,18 @@ fn build_reduced_graph_for_item(&mut self, parent } ItemFn(_, fn_style, _, _, _) => { - let (name_bindings, new_parent) = - self.add_child(ident, parent, ForbidDuplicateValues, sp); + let name_bindings = + self.add_child(ident, parent.clone(), ForbidDuplicateValues, sp); let def = DefFn(local_def(item.id), fn_style); name_bindings.define_value(def, sp, is_public); - new_parent + parent } // These items live in the type namespace. ItemTy(..) => { - let (name_bindings, _) = - self.add_child(ident, parent, ForbidDuplicateTypes, sp); + let name_bindings = + self.add_child(ident, parent.clone(), ForbidDuplicateTypes, sp); name_bindings.define_type (DefTy(local_def(item.id)), sp, is_public); @@ -1209,8 +1191,8 @@ fn build_reduced_graph_for_item(&mut self, } ItemEnum(ref enum_definition, _) => { - let (name_bindings, new_parent) = - self.add_child(ident, parent, ForbidDuplicateTypes, sp); + let name_bindings = + self.add_child(ident, parent.clone(), ForbidDuplicateTypes, sp); name_bindings.define_type (DefTy(local_def(item.id)), sp, is_public); @@ -1219,7 +1201,7 @@ fn build_reduced_graph_for_item(&mut self, self.build_reduced_graph_for_variant( variant, local_def(item.id), - new_parent, + parent.clone(), is_public); } parent @@ -1233,7 +1215,7 @@ fn build_reduced_graph_for_item(&mut self, None => (ForbidDuplicateTypes, None) }; - let (name_bindings, new_parent) = self.add_child(ident, parent, forbid, sp); + let name_bindings = self.add_child(ident, parent.clone(), forbid, sp); // Define a name in the type namespace. name_bindings.define_type(DefTy(local_def(item.id)), sp, is_public); @@ -1249,7 +1231,7 @@ fn build_reduced_graph_for_item(&mut self, // Record the def ID of this struct. self.structs.insert(local_def(item.id)); - new_parent + parent } ItemImpl(_, None, ty, ref methods) => { @@ -1270,7 +1252,7 @@ fn build_reduced_graph_for_item(&mut self, .find_copy(&name.name); let new_parent = match parent_opt { // It already exists - Some(child) if child.get_module_if_available() + Some(ref child) if child.get_module_if_available() .is_some() && child.get_module().kind.get() == ImplModuleKind => { @@ -1278,14 +1260,14 @@ fn build_reduced_graph_for_item(&mut self, } // Create the module _ => { - let (name_bindings, new_parent) = + let name_bindings = self.add_child(name, - parent, + parent.clone(), ForbidDuplicateModules, sp); let parent_link = - self.get_parent_link(new_parent, ident); + self.get_parent_link(parent.clone(), ident); let def_id = local_def(item.id); let ns = TypeNS; let is_public = @@ -1308,9 +1290,9 @@ fn build_reduced_graph_for_item(&mut self, for method in methods.iter() { // Add the method to the module. let ident = method.ident; - let (method_name_bindings, _) = + let method_name_bindings = self.add_child(ident, - new_parent, + new_parent.clone(), ForbidDuplicateValues, method.span); let def = match method.explicit_self.node { @@ -1344,11 +1326,11 @@ fn build_reduced_graph_for_item(&mut self, ItemImpl(_, Some(_), _, _) => parent, ItemTrait(_, _, ref methods) => { - let (name_bindings, new_parent) = - self.add_child(ident, parent, ForbidDuplicateTypes, sp); + let name_bindings = + self.add_child(ident, parent.clone(), ForbidDuplicateTypes, sp); // Add all the methods within to a new module. - let parent_link = self.get_parent_link(parent, ident); + let parent_link = self.get_parent_link(parent.clone(), ident); name_bindings.define_module(parent_link, Some(local_def(item.id)), TraitModuleKind, @@ -1381,9 +1363,9 @@ fn build_reduced_graph_for_item(&mut self, } }; - let (method_name_bindings, _) = + let method_name_bindings = self.add_child(ident, - module_parent, + module_parent.clone(), ForbidDuplicateValues, ty_m.span); method_name_bindings.define_value(def, ty_m.span, true); @@ -1395,7 +1377,7 @@ fn build_reduced_graph_for_item(&mut self, } name_bindings.define_type(DefTrait(def_id), sp, is_public); - new_parent + parent } ItemMac(..) => parent } @@ -1412,15 +1394,15 @@ fn build_reduced_graph_for_variant(&mut self, match variant.node.kind { TupleVariantKind(_) => { - let (child, _) = self.add_child(ident, parent, ForbidDuplicateValues, - variant.span); + let child = self.add_child(ident, parent, ForbidDuplicateValues, variant.span); child.define_value(DefVariant(item_id, local_def(variant.node.id), false), variant.span, is_public); } StructVariantKind(_) => { - let (child, _) = self.add_child(ident, parent, ForbidDuplicateTypesAndValues, - variant.span); + let child = self.add_child(ident, parent, + ForbidDuplicateTypesAndValues, + variant.span); child.define_type(DefVariant(item_id, local_def(variant.node.id), true), variant.span, is_public); @@ -1464,15 +1446,15 @@ fn build_reduced_graph_for_view_item(&mut self, view_item: &ViewItem, } // Build up the import directives. - let module_ = self.get_module_from_parent(parent); + let module_ = parent.module(); let is_public = view_item.vis == ast::Public; match view_path.node { ViewPathSimple(binding, ref full_path, id) => { let source_ident = full_path.segments.last().unwrap().identifier; - let subclass = @SingleImport(binding, - source_ident); - self.build_import_directive(module_, + let subclass = SingleImport(binding, + source_ident); + self.build_import_directive(&*module_, module_path, subclass, view_path.span, @@ -1482,20 +1464,19 @@ fn build_reduced_graph_for_view_item(&mut self, view_item: &ViewItem, ViewPathList(_, ref source_idents, _) => { for source_ident in source_idents.iter() { let name = source_ident.node.name; - let subclass = @SingleImport(name, name); self.build_import_directive( - module_, + &*module_, module_path.clone(), - subclass, + SingleImport(name, name), source_ident.span, source_ident.node.id, is_public); } } ViewPathGlob(_, id) => { - self.build_import_directive(module_, + self.build_import_directive(&*module_, module_path, - @GlobImport, + GlobImport, view_path.span, id, is_public); @@ -1511,16 +1492,16 @@ fn build_reduced_graph_for_view_item(&mut self, view_item: &ViewItem, let def_id = DefId { krate: crate_id, node: 0 }; self.external_exports.insert(def_id); let parent_link = ModuleParentLink - (self.get_module_from_parent(parent), name); - let external_module = @Module::new(parent_link, - Some(def_id), - NormalModuleKind, - false, - true); + (parent.module().downgrade(), name); + let external_module = Rc::new(Module::new(parent_link, + Some(def_id), + NormalModuleKind, + false, + true)); parent.module().external_module_children .borrow_mut().insert(name.name, - external_module); + external_module.clone()); self.build_reduced_graph_for_external_crate( external_module); @@ -1535,11 +1516,10 @@ fn build_reduced_graph_for_view_item(&mut self, view_item: &ViewItem, fn build_reduced_graph_for_foreign_item(&mut self, foreign_item: &ForeignItem, parent: ReducedGraphParent, - f: |&mut Resolver, - ReducedGraphParent|) { + f: |&mut Resolver|) { let name = foreign_item.ident; let is_public = foreign_item.vis == ast::Public; - let (name_bindings, new_parent) = + let name_bindings = self.add_child(name, parent, ForbidDuplicateValues, foreign_item.span); @@ -1553,13 +1533,13 @@ fn build_reduced_graph_for_foreign_item(&mut self, foreign_item.id, 0, NormalRibKind), - |this| f(this, new_parent)); + f); } ForeignItemStatic(_, m) => { let def = DefStatic(local_def(foreign_item.id), m); name_bindings.define_value(def, foreign_item.span, is_public); - f(self, new_parent) + f(self) } } } @@ -1576,15 +1556,15 @@ fn build_reduced_graph_for_block(&mut self, anonymous module for block {}", block_id); - let parent_module = self.get_module_from_parent(parent); - let new_module = @Module::new( - BlockParentLink(parent_module, block_id), + let parent_module = parent.module(); + let new_module = Rc::new(Module::new( + BlockParentLink(parent_module.downgrade(), block_id), None, AnonymousModuleKind, false, - false); + false)); parent_module.anonymous_children.borrow_mut() - .insert(block_id, new_module); + .insert(block_id, new_module.clone()); ModuleReducedGraphParent(new_module) } else { parent @@ -1594,7 +1574,7 @@ fn build_reduced_graph_for_block(&mut self, fn handle_external_def(&mut self, def: Def, vis: Visibility, - child_name_bindings: @NameBindings, + child_name_bindings: &NameBindings, final_ident: &str, ident: Ident, new_parent: ReducedGraphParent) { @@ -1603,7 +1583,7 @@ fn handle_external_def(&mut self, vis); let is_public = vis == ast::Public; let is_exported = is_public && match new_parent { - ModuleReducedGraphParent(module) => { + ModuleReducedGraphParent(ref module) => { match module.def_id.get() { None => true, Some(did) => self.external_exports.contains(&did) @@ -1627,7 +1607,7 @@ fn handle_external_def(&mut self, debug!("(building reduced graph for \ external crate) building module \ {}", final_ident); - let parent_link = self.get_parent_link(new_parent, ident); + let parent_link = self.get_parent_link(new_parent.clone(), ident); child_name_bindings.define_module(parent_link, Some(def_id), @@ -1733,7 +1713,7 @@ trait method '{}'", /// Builds the reduced graph for a single item in an external crate. fn build_reduced_graph_for_external_crate_def(&mut self, - root: @Module, + root: Rc, def_like: DefLike, ident: Ident, visibility: Visibility) { @@ -1750,25 +1730,25 @@ fn build_reduced_graph_for_external_crate_def(&mut self, child_ident, vis| { self.build_reduced_graph_for_external_crate_def( - root, + root.clone(), def_like, child_ident, vis) }); } _ => { - let (child_name_bindings, new_parent) = + let child_name_bindings = self.add_child(ident, - ModuleReducedGraphParent(root), + ModuleReducedGraphParent(root.clone()), OverwriteDuplicates, DUMMY_SP); self.handle_external_def(def, visibility, - child_name_bindings, + &*child_name_bindings, token::get_ident(ident).get(), ident, - new_parent); + ModuleReducedGraphParent(root)); } } } @@ -1787,10 +1767,10 @@ fn build_reduced_graph_for_external_crate_def(&mut self, static methods for type name {}", token::get_ident(final_ident)); - let (child_name_bindings, new_parent) = + let child_name_bindings = self.add_child( final_ident, - ModuleReducedGraphParent(root), + ModuleReducedGraphParent(root.clone()), OverwriteDuplicates, DUMMY_SP); @@ -1813,7 +1793,7 @@ fn build_reduced_graph_for_external_crate_def(&mut self, } Some(_) | None => { let parent_link = - self.get_parent_link(new_parent, + self.get_parent_link(ModuleReducedGraphParent(root), final_ident); child_name_bindings.define_module( parent_link, @@ -1839,9 +1819,9 @@ fn build_reduced_graph_for_external_crate_def(&mut self, static method '{}'", token::get_ident(ident)); - let (method_name_bindings, _) = + let method_name_bindings = self.add_child(ident, - new_parent, + new_parent.clone(), OverwriteDuplicates, DUMMY_SP); let def = DefFn( @@ -1868,9 +1848,9 @@ fn build_reduced_graph_for_external_crate_def(&mut self, } /// Builds the reduced graph rooted at the given external module. - fn populate_external_module(&mut self, module: @Module) { + fn populate_external_module(&mut self, module: Rc) { debug!("(populating external module) attempting to populate {}", - self.module_to_str(module)); + self.module_to_str(&*module)); let def_id = match module.def_id.get() { None => { @@ -1885,7 +1865,7 @@ fn populate_external_module(&mut self, module: @Module) { |def_like, child_ident, visibility| { debug!("(populating external module) ... found ident: {}", token::get_ident(child_ident)); - self.build_reduced_graph_for_external_crate_def(module, + self.build_reduced_graph_for_external_crate_def(module.clone(), def_like, child_ident, visibility) @@ -1895,24 +1875,23 @@ fn populate_external_module(&mut self, module: @Module) { /// Ensures that the reduced graph rooted at the given external module /// is built, building it if it is not. - fn populate_module_if_necessary(&mut self, module: @Module) { + fn populate_module_if_necessary(&mut self, module: &Rc) { if !module.populated.get() { - self.populate_external_module(module) + self.populate_external_module(module.clone()) } assert!(module.populated.get()) } /// Builds the reduced graph rooted at the 'use' directive for an external /// crate. - fn build_reduced_graph_for_external_crate(&mut self, - root: @Module) { + fn build_reduced_graph_for_external_crate(&mut self, root: Rc) { csearch::each_top_level_item_of_crate(&self.session.cstore, root.def_id .get() .unwrap() .krate, |def_like, ident, visibility| { - self.build_reduced_graph_for_external_crate_def(root, + self.build_reduced_graph_for_external_crate_def(root.clone(), def_like, ident, visibility) @@ -1921,47 +1900,48 @@ fn build_reduced_graph_for_external_crate(&mut self, /// Creates and adds an import directive to the given module. fn build_import_directive(&mut self, - module_: @Module, + module_: &Module, module_path: Vec , - subclass: @ImportDirectiveSubclass, + subclass: ImportDirectiveSubclass, span: Span, id: NodeId, is_public: bool) { - let directive = @ImportDirective::new(module_path, - subclass, span, id, - is_public); - module_.imports.borrow_mut().push(directive); + module_.imports.borrow_mut().push(ImportDirective::new(module_path, + subclass, + span, id, + is_public)); + self.unresolved_imports += 1; // Bump the reference count on the name. Or, if this is a glob, set // the appropriate flag. - match *subclass { + match subclass { SingleImport(target, _) => { debug!("(building import directive) building import \ directive: {}::{}", - self.idents_to_str(directive.module_path.as_slice()), + self.idents_to_str(module_.imports.borrow().last().unwrap() + .module_path.as_slice()), token::get_ident(target)); let mut import_resolutions = module_.import_resolutions .borrow_mut(); - match import_resolutions.find(&target.name) { - Some(&resolution) => { + match import_resolutions.find_mut(&target.name) { + Some(resolution) => { debug!("(building import directive) bumping \ reference"); - resolution.outstanding_references.set( - resolution.outstanding_references.get() + 1); + resolution.outstanding_references += 1; // the source of this name is different now - resolution.type_id.set(id); - resolution.value_id.set(id); - resolution.is_public.set(is_public); - } - None => { - debug!("(building import directive) creating new"); - let resolution = @ImportResolution::new(id, is_public); - resolution.outstanding_references.set(1); - import_resolutions.insert(target.name, resolution); + resolution.type_id = id; + resolution.value_id = id; + resolution.is_public = is_public; + return; } + None => {} } + debug!("(building import directive) creating new"); + let mut resolution = ImportResolution::new(id, is_public); + resolution.outstanding_references = 1; + import_resolutions.insert(target.name, resolution); } GlobImport => { // Set the glob flag. This tells us that we don't know the @@ -1970,8 +1950,6 @@ fn build_import_directive(&mut self, module_.glob_count.set(module_.glob_count.get() + 1); } } - - self.unresolved_imports += 1; } // Import resolution @@ -1992,7 +1970,7 @@ fn resolve_imports(&mut self) { i, self.unresolved_imports); let module_root = self.graph_root.get_module(); - self.resolve_imports_for_module_subtree(module_root); + self.resolve_imports_for_module_subtree(module_root.clone()); if self.unresolved_imports == 0 { debug!("(resolving imports) success"); @@ -2011,14 +1989,13 @@ fn resolve_imports(&mut self) { /// Attempts to resolve imports for the given module and all of its /// submodules. - fn resolve_imports_for_module_subtree(&mut self, - module_: @Module) { + fn resolve_imports_for_module_subtree(&mut self, module_: Rc) { debug!("(resolving imports for module subtree) resolving {}", - self.module_to_str(module_)); - self.resolve_imports_for_module(module_); + self.module_to_str(&*module_)); + self.resolve_imports_for_module(module_.clone()); - self.populate_module_if_necessary(module_); - for (_, &child_node) in module_.children.borrow().iter() { + self.populate_module_if_necessary(&module_); + for (_, child_node) in module_.children.borrow().iter() { match child_node.get_module_if_available() { None => { // Nothing to do. @@ -2029,17 +2006,17 @@ fn resolve_imports_for_module_subtree(&mut self, } } - for (_, &child_module) in module_.anonymous_children.borrow().iter() { - self.resolve_imports_for_module_subtree(child_module); + for (_, child_module) in module_.anonymous_children.borrow().iter() { + self.resolve_imports_for_module_subtree(child_module.clone()); } } /// Attempts to resolve imports for the given module only. - fn resolve_imports_for_module(&mut self, module: @Module) { + fn resolve_imports_for_module(&mut self, module: Rc) { if module.all_imports_resolved() { debug!("(resolving imports for module) all imports resolved for \ {}", - self.module_to_str(module)); + self.module_to_str(&*module)); return; } @@ -2047,15 +2024,15 @@ fn resolve_imports_for_module(&mut self, module: @Module) { let import_count = imports.len(); while module.resolved_import_count.get() < import_count { let import_index = module.resolved_import_count.get(); - let import_directive = *imports.get(import_index); - match self.resolve_import_for_module(module, import_directive) { + let import_directive = imports.get(import_index); + match self.resolve_import_for_module(module.clone(), import_directive) { Failed => { // We presumably emitted an error. Continue. let msg = format!("failed to resolve import `{}`", self.import_path_to_str( import_directive.module_path .as_slice(), - *import_directive.subclass)); + import_directive.subclass)); self.resolve_error(import_directive.span, msg); } Indeterminate => { @@ -2124,8 +2101,8 @@ fn import_path_to_str(&mut self, /// currently-unresolved imports, or success if we know the name exists. /// If successful, the resolved bindings are written into the module. fn resolve_import_for_module(&mut self, - module_: @Module, - import_directive: @ImportDirective) + module_: Rc, + import_directive: &ImportDirective) -> ResolveResult<()> { let mut resolution_result = Failed; let module_path = &import_directive.module_path; @@ -2133,14 +2110,14 @@ fn resolve_import_for_module(&mut self, debug!("(resolving import for module) resolving import `{}::...` in \ `{}`", self.idents_to_str(module_path.as_slice()), - self.module_to_str(module_)); + self.module_to_str(&*module_)); // First, resolve the module path for the directive, if necessary. let container = if module_path.len() == 0 { // Use the crate root. Some((self.graph_root.get_module(), LastMod(AllPublic))) } else { - match self.resolve_module_path(module_, + match self.resolve_module_path(module_.clone(), module_path.as_slice(), DontUseLexicalScope, import_directive.span, @@ -2161,10 +2138,10 @@ fn resolve_import_for_module(&mut self, // We found the module that the target is contained // within. Attempt to resolve the import within it. - match *import_directive.subclass { + match import_directive.subclass { SingleImport(target, source) => { resolution_result = - self.resolve_single_import(module_, + self.resolve_single_import(&*module_, containing_module, target, source, @@ -2173,7 +2150,7 @@ fn resolve_import_for_module(&mut self, } GlobImport => { resolution_result = - self.resolve_glob_import(module_, + self.resolve_glob_import(&*module_, containing_module, import_directive.id, import_directive.is_public, @@ -2200,7 +2177,7 @@ fn resolve_import_for_module(&mut self, // resolve_imports_for_module.) if !resolution_result.indeterminate() { - match *import_directive.subclass { + match import_directive.subclass { GlobImport => { assert!(module_.glob_count.get() >= 1); module_.glob_count.set(module_.glob_count.get() - 1); @@ -2214,7 +2191,7 @@ fn resolve_import_for_module(&mut self, return resolution_result; } - fn create_name_bindings_from_module(module: @Module) -> NameBindings { + fn create_name_bindings_from_module(module: Rc) -> NameBindings { NameBindings { type_def: RefCell::new(Some(TypeNsDef { is_public: false, @@ -2227,8 +2204,8 @@ fn create_name_bindings_from_module(module: @Module) -> NameBindings { } fn resolve_single_import(&mut self, - module_: @Module, - containing_module: @Module, + module_: &Module, + containing_module: Rc, target: Ident, source: Ident, directive: &ImportDirective, @@ -2237,7 +2214,7 @@ fn resolve_single_import(&mut self, debug!("(resolving single import) resolving `{}` = `{}::{}` from \ `{}` id {}, last private {:?}", token::get_ident(target), - self.module_to_str(containing_module), + self.module_to_str(&*containing_module), token::get_ident(source), self.module_to_str(module_), directive.id, @@ -2256,22 +2233,22 @@ fn resolve_single_import(&mut self, let mut type_result = UnknownResult; // Search for direct children of the containing module. - self.populate_module_if_necessary(containing_module); + self.populate_module_if_necessary(&containing_module); match containing_module.children.borrow().find(&source.name) { None => { // Continue. } - Some(child_name_bindings) => { + Some(ref child_name_bindings) => { if child_name_bindings.defined_in_namespace(ValueNS) { debug!("(resolving single import) found value binding"); - value_result = BoundResult(containing_module, - *child_name_bindings); + value_result = BoundResult(containing_module.clone(), + (*child_name_bindings).clone()); } if child_name_bindings.defined_in_namespace(TypeNS) { debug!("(resolving single import) found type binding"); - type_result = BoundResult(containing_module, - *child_name_bindings); + type_result = BoundResult(containing_module.clone(), + (*child_name_bindings).clone()); } } } @@ -2280,7 +2257,7 @@ fn resolve_single_import(&mut self, // search imports as well. let mut value_used_reexport = false; let mut type_used_reexport = false; - match (value_result, type_result) { + match (value_result.clone(), type_result.clone()) { (BoundResult(..), BoundResult(..)) => {} // Continue. _ => { // If there is an unresolved glob at this point in the @@ -2293,12 +2270,8 @@ fn resolve_single_import(&mut self, return Indeterminate; } - // Now search the exported imports within the containing - // module. - - let import_resolutions = containing_module.import_resolutions - .borrow(); - match import_resolutions.find(&source.name) { + // Now search the exported imports within the containing module. + match containing_module.import_resolutions.borrow().find(&source.name) { None => { debug!("(resolving single import) no import"); // The containing module definitely doesn't have an @@ -2314,32 +2287,30 @@ fn resolve_single_import(&mut self, } } Some(import_resolution) - if import_resolution.outstanding_references.get() - == 0 => { + if import_resolution.outstanding_references == 0 => { fn get_binding(this: &mut Resolver, - import_resolution: @ImportResolution, + import_resolution: &ImportResolution, namespace: Namespace) -> NamespaceResult { // Import resolutions must be declared with "pub" // in order to be exported. - if !import_resolution.is_public.get() { + if !import_resolution.is_public { return UnboundResult; } - match (*import_resolution). + match import_resolution. target_for_namespace(namespace) { None => { return UnboundResult; } - Some(target) => { + Some(Target {target_module, bindings}) => { debug!("(resolving single import) found \ import in ns {:?}", namespace); let id = import_resolution.id(namespace); this.used_imports.insert((id, namespace)); - return BoundResult(target.target_module, - target.bindings); + return BoundResult(target_module, bindings); } } } @@ -2347,14 +2318,14 @@ fn get_binding(this: &mut Resolver, // The name is an import which has been fully // resolved. We can, therefore, just follow it. if value_result.is_unknown() { - value_result = get_binding(self, *import_resolution, + value_result = get_binding(self, import_resolution, ValueNS); - value_used_reexport = import_resolution.is_public.get(); + value_used_reexport = import_resolution.is_public; } if type_result.is_unknown() { - type_result = get_binding(self, *import_resolution, + type_result = get_binding(self, import_resolution, TypeNS); - type_used_reexport = import_resolution.is_public.get(); + type_used_reexport = import_resolution.is_public; } } @@ -2382,9 +2353,9 @@ fn get_binding(this: &mut Resolver, debug!("(resolving single import) found external \ module"); let name_bindings = - @Resolver::create_name_bindings_from_module( - module); - type_result = BoundResult(containing_module, + Rc::new(Resolver::create_name_bindings_from_module( + module)); + type_result = BoundResult(containing_module.clone(), name_bindings); type_used_public = true; } @@ -2393,18 +2364,15 @@ fn get_binding(this: &mut Resolver, } // We've successfully resolved the import. Write the results in. - let import_resolution = { - let import_resolutions = module_.import_resolutions.borrow(); - assert!(import_resolutions.contains_key(&target.name)); - import_resolutions.get_copy(&target.name) - }; + let mut import_resolutions = module_.import_resolutions.borrow_mut(); + let import_resolution = import_resolutions.get_mut(&target.name); match value_result { - BoundResult(target_module, name_bindings) => { + BoundResult(ref target_module, ref name_bindings) => { debug!("(resolving single import) found value target"); - *import_resolution.value_target.borrow_mut() = - Some(Target::new(target_module, name_bindings)); - import_resolution.value_id.set(directive.id); + import_resolution.value_target = Some(Target::new(target_module.clone(), + name_bindings.clone())); + import_resolution.value_id = directive.id; value_used_public = name_bindings.defined_in_public_namespace(ValueNS); } UnboundResult => { /* Continue. */ } @@ -2413,12 +2381,12 @@ fn get_binding(this: &mut Resolver, } } match type_result { - BoundResult(target_module, name_bindings) => { + BoundResult(ref target_module, ref name_bindings) => { debug!("(resolving single import) found type target: {:?}", { name_bindings.type_def.borrow().clone().unwrap().type_def }); - *import_resolution.type_target.borrow_mut() = - Some(Target::new(target_module, name_bindings)); - import_resolution.type_id.set(directive.id); + import_resolution.type_target = + Some(Target::new(target_module.clone(), name_bindings.clone())); + import_resolution.type_id = directive.id; type_used_public = name_bindings.defined_in_public_namespace(TypeNS); } UnboundResult => { /* Continue. */ } @@ -2431,22 +2399,21 @@ fn get_binding(this: &mut Resolver, let msg = format!("unresolved import: there is no \ `{}` in `{}`", token::get_ident(source), - self.module_to_str(containing_module)); + self.module_to_str(&*containing_module)); self.resolve_error(directive.span, msg); return Failed; } let value_used_public = value_used_reexport || value_used_public; let type_used_public = type_used_reexport || type_used_public; - assert!(import_resolution.outstanding_references.get() >= 1); - import_resolution.outstanding_references.set( - import_resolution.outstanding_references.get() - 1); + assert!(import_resolution.outstanding_references >= 1); + import_resolution.outstanding_references -= 1; // record what this import resolves to for later uses in documentation, // this may resolve to either a value or a type, but for documentation // purposes it's good enough to just favor one over the other. - let value_private = match *import_resolution.value_target.borrow() { - Some(target) => { + let value_private = match import_resolution.value_target { + Some(ref target) => { let def = target.bindings.def_for_namespace(ValueNS).unwrap(); self.def_map.borrow_mut().insert(directive.id, def); let did = def_id_of_def(def); @@ -2456,8 +2423,8 @@ fn get_binding(this: &mut Resolver, // _exists is false. None => None, }; - let type_private = match *import_resolution.type_target.borrow() { - Some(target) => { + let type_private = match import_resolution.type_target { + Some(ref target) => { let def = target.bindings.def_for_namespace(TypeNS).unwrap(); self.def_map.borrow_mut().insert(directive.id, def); let did = def_id_of_def(def); @@ -2479,8 +2446,8 @@ fn get_binding(this: &mut Resolver, // succeeds or bails out (as importing * from an empty module or a module // that exports nothing is valid). fn resolve_glob_import(&mut self, - module_: @Module, - containing_module: @Module, + module_: &Module, + containing_module: Rc, id: NodeId, is_public: bool, lp: LastPrivate) @@ -2506,70 +2473,71 @@ fn resolve_glob_import(&mut self, for (ident, target_import_resolution) in import_resolutions.iter() { debug!("(resolving glob import) writing module resolution \ {:?} into `{}`", - target_import_resolution.type_target.borrow().is_none(), + target_import_resolution.type_target.is_none(), self.module_to_str(module_)); - if !target_import_resolution.is_public.get() { + if !target_import_resolution.is_public { debug!("(resolving glob import) nevermind, just kidding"); continue } // Here we merge two import resolutions. let mut import_resolutions = module_.import_resolutions.borrow_mut(); - match import_resolutions.find(ident) { - None => { - // Simple: just copy the old import resolution. - let new_import_resolution = - @ImportResolution::new(id, is_public); - *new_import_resolution.value_target.borrow_mut() = - get(&target_import_resolution.value_target); - *new_import_resolution.type_target.borrow_mut() = - get(&target_import_resolution.type_target); - - import_resolutions.insert - (*ident, new_import_resolution); - } - Some(&dest_import_resolution) => { + match import_resolutions.find_mut(ident) { + Some(dest_import_resolution) => { // Merge the two import resolutions at a finer-grained // level. - match *target_import_resolution.value_target.borrow() { + match target_import_resolution.value_target { None => { // Continue. } - Some(value_target) => { - *dest_import_resolution.value_target.borrow_mut() = Some(value_target); + Some(ref value_target) => { + dest_import_resolution.value_target = + Some(value_target.clone()); } } - match *target_import_resolution.type_target.borrow() { + match target_import_resolution.type_target { None => { // Continue. } - Some(type_target) => { - *dest_import_resolution.type_target.borrow_mut() = Some(type_target); + Some(ref type_target) => { + dest_import_resolution.type_target = + Some(type_target.clone()); } } - dest_import_resolution.is_public.set(is_public); + dest_import_resolution.is_public = is_public; + continue; } + None => {} } + + // Simple: just copy the old import resolution. + let mut new_import_resolution = ImportResolution::new(id, is_public); + new_import_resolution.value_target = + target_import_resolution.value_target.clone(); + new_import_resolution.type_target = + target_import_resolution.type_target.clone(); + + import_resolutions.insert(*ident, new_import_resolution); } // Add all children from the containing module. - self.populate_module_if_necessary(containing_module); + self.populate_module_if_necessary(&containing_module); for (&name, name_bindings) in containing_module.children .borrow().iter() { - self.merge_import_resolution(module_, containing_module, + self.merge_import_resolution(module_, containing_module.clone(), id, is_public, - name, *name_bindings); + name, name_bindings.clone()); } // Add external module children from the containing module. for (&name, module) in containing_module.external_module_children .borrow().iter() { let name_bindings = - @Resolver::create_name_bindings_from_module(*module); - self.merge_import_resolution(module_, containing_module, + Rc::new(Resolver::create_name_bindings_from_module(module.clone())); + self.merge_import_resolution(module_, containing_module.clone(), id, is_public, name, name_bindings); } @@ -2588,58 +2556,49 @@ fn resolve_glob_import(&mut self, } fn merge_import_resolution(&mut self, - module_: @Module, - containing_module: @Module, + module_: &Module, + containing_module: Rc, id: NodeId, is_public: bool, name: Name, - name_bindings: @NameBindings) { - let dest_import_resolution; + name_bindings: Rc) { let mut import_resolutions = module_.import_resolutions.borrow_mut(); - match import_resolutions.find(&name) { - None => { - // Create a new import resolution from this child. - dest_import_resolution = - @ImportResolution::new(id, is_public); - import_resolutions.insert(name, - dest_import_resolution); - } - Some(&existing_import_resolution) => { - dest_import_resolution = existing_import_resolution; - } - } + let dest_import_resolution = import_resolutions.find_or_insert_with(name, |_| { + // Create a new import resolution from this child. + ImportResolution::new(id, is_public) + }); debug!("(resolving glob import) writing resolution `{}` in `{}` \ to `{}`", token::get_name(name).get().to_str(), - self.module_to_str(containing_module), + self.module_to_str(&*containing_module), self.module_to_str(module_)); // Merge the child item into the import resolution. if name_bindings.defined_in_public_namespace(ValueNS) { debug!("(resolving glob import) ... for value target"); - *dest_import_resolution.value_target.borrow_mut() = - Some(Target::new(containing_module, name_bindings)); - dest_import_resolution.value_id.set(id); + dest_import_resolution.value_target = + Some(Target::new(containing_module.clone(), name_bindings.clone())); + dest_import_resolution.value_id = id; } if name_bindings.defined_in_public_namespace(TypeNS) { debug!("(resolving glob import) ... for type target"); - *dest_import_resolution.type_target.borrow_mut() = - Some(Target::new(containing_module, name_bindings)); - dest_import_resolution.type_id.set(id); + dest_import_resolution.type_target = + Some(Target::new(containing_module, name_bindings.clone())); + dest_import_resolution.type_id = id; } - dest_import_resolution.is_public.set(is_public); + dest_import_resolution.is_public = is_public; } /// Resolves the given module path from the given root `module_`. fn resolve_module_path_from_root(&mut self, - module_: @Module, + module_: Rc, module_path: &[Ident], index: uint, span: Span, name_search_type: NameSearchType, lp: LastPrivate) - -> ResolveResult<(@Module, LastPrivate)> { + -> ResolveResult<(Rc, LastPrivate)> { let mut search_module = module_; let mut index = index; let module_path_len = module_path.len(); @@ -2650,14 +2609,14 @@ fn resolve_module_path_from_root(&mut self, // modules as we go. while index < module_path_len { let name = module_path[index]; - match self.resolve_name_in_module(search_module, + match self.resolve_name_in_module(search_module.clone(), name, TypeNS, name_search_type, false) { Failed => { let segment_name = token::get_ident(name); - let module_name = self.module_to_str(search_module); + let module_name = self.module_to_str(&*search_module); if "???" == module_name { let span = Span { lo: span.lo, @@ -2685,7 +2644,7 @@ fn resolve_module_path_from_root(&mut self, // Check to see whether there are type bindings, and, if // so, whether there is a module within. match *target.bindings.type_def.borrow() { - Some(type_def) => { + Some(ref type_def) => { match type_def.module_def { None => { // Not a module. @@ -2693,7 +2652,7 @@ fn resolve_module_path_from_root(&mut self, token::get_ident(name))); return Failed; } - Some(module_def) => { + Some(ref module_def) => { // If we're doing the search for an // import, do not allow traits and impls // to be selected. @@ -2708,7 +2667,7 @@ fn resolve_module_path_from_root(&mut self, return Failed; } (_, _) => { - search_module = module_def; + search_module = module_def.clone(); // Keep track of the closest // private module used when @@ -2752,22 +2711,22 @@ fn resolve_module_path_from_root(&mut self, /// On success, returns the resolved module, and the closest *private* /// module found to the destination when resolving this path. fn resolve_module_path(&mut self, - module_: @Module, + module_: Rc, module_path: &[Ident], use_lexical_scope: UseLexicalScopeFlag, span: Span, name_search_type: NameSearchType) - -> ResolveResult<(@Module, LastPrivate)> { + -> ResolveResult<(Rc, LastPrivate)> { let module_path_len = module_path.len(); assert!(module_path_len > 0); debug!("(resolving module path for import) processing `{}` rooted at \ `{}`", self.idents_to_str(module_path), - self.module_to_str(module_)); + self.module_to_str(&*module_)); // Resolve the module prefix, if any. - let module_prefix_result = self.resolve_module_prefix(module_, + let module_prefix_result = self.resolve_module_prefix(module_.clone(), module_path); let search_module; @@ -2832,8 +2791,8 @@ fn resolve_module_path(&mut self, } } } - Success(PrefixFound(containing_module, index)) => { - search_module = containing_module; + Success(PrefixFound(ref containing_module, index)) => { + search_module = containing_module.clone(); start_index = index; last_private = LastMod(DependsOn(containing_module.def_id .get() @@ -2852,7 +2811,7 @@ fn resolve_module_path(&mut self, /// Invariant: This must only be called during main resolution, not during /// import resolution. fn resolve_item_in_lexical_scope(&mut self, - module_: @Module, + module_: Rc, name: Ident, namespace: Namespace, search_through_modules: @@ -2862,17 +2821,17 @@ fn resolve_item_in_lexical_scope(&mut self, namespace {:?} in `{}`", token::get_ident(name), namespace, - self.module_to_str(module_)); + self.module_to_str(&*module_)); // The current module node is handled specially. First, check for // its immediate children. - self.populate_module_if_necessary(module_); + self.populate_module_if_necessary(&module_); match module_.children.borrow().find(&name.name) { Some(name_bindings) if name_bindings.defined_in_namespace(namespace) => { debug!("top name bindings succeeded"); - return Success((Target::new(module_, *name_bindings), + return Success((Target::new(module_.clone(), name_bindings.clone()), false)); } Some(_) | None => { /* Not found; continue. */ } @@ -2910,7 +2869,7 @@ fn resolve_item_in_lexical_scope(&mut self, None => {} Some(module) => { let name_bindings = - @Resolver::create_name_bindings_from_module(module); + Rc::new(Resolver::create_name_bindings_from_module(module)); debug!("lower name bindings succeeded"); return Success((Target::new(module_, name_bindings), false)); } @@ -2921,7 +2880,7 @@ fn resolve_item_in_lexical_scope(&mut self, let mut search_module = module_; loop { // Go to the next parent. - match search_module.parent_link { + match search_module.parent_link.clone() { NoParentLink => { // No more parents. This module was unresolved. debug!("(resolving item in lexical scope) unresolved \ @@ -2944,22 +2903,22 @@ fn resolve_item_in_lexical_scope(&mut self, TraitModuleKind | ImplModuleKind | AnonymousModuleKind => { - search_module = parent_module_node; + search_module = parent_module_node.upgrade().unwrap(); } } } SearchThroughModules => { - search_module = parent_module_node; + search_module = parent_module_node.upgrade().unwrap(); } } } - BlockParentLink(parent_module_node, _) => { - search_module = parent_module_node; + BlockParentLink(ref parent_module_node, _) => { + search_module = parent_module_node.upgrade().unwrap(); } } // Resolve the name in the parent module. - match self.resolve_name_in_module(search_module, + match self.resolve_name_in_module(search_module.clone(), name, namespace, PathSearch, @@ -2987,9 +2946,9 @@ fn resolve_item_in_lexical_scope(&mut self, /// Resolves a module name in the current lexical scope. fn resolve_module_in_lexical_scope(&mut self, - module_: @Module, + module_: Rc, name: Ident) - -> ResolveResult<@Module> { + -> ResolveResult> { // If this module is an anonymous module, resolve the item in the // lexical scope. Otherwise, resolve the item from the crate root. let resolve_result = self.resolve_item_in_lexical_scope( @@ -2998,7 +2957,7 @@ fn resolve_module_in_lexical_scope(&mut self, Success((target, _)) => { let bindings = &*target.bindings; match *bindings.type_def.borrow() { - Some(type_def) => { + Some(ref type_def) => { match type_def.module_def { None => { error!("!!! (resolving module in lexical \ @@ -3006,8 +2965,8 @@ fn resolve_module_in_lexical_scope(&mut self, module!"); return Failed; } - Some(module_def) => { - return Success(module_def); + Some(ref module_def) => { + return Success(module_def.clone()); } } } @@ -3032,14 +2991,15 @@ fn resolve_module_in_lexical_scope(&mut self, } /// Returns the nearest normal module parent of the given module. - fn get_nearest_normal_module_parent(&mut self, module_: @Module) - -> Option<@Module> { + fn get_nearest_normal_module_parent(&mut self, module_: Rc) + -> Option> { let mut module_ = module_; loop { - match module_.parent_link { + match module_.parent_link.clone() { NoParentLink => return None, ModuleParentLink(new_module, _) | BlockParentLink(new_module, _) => { + let new_module = new_module.upgrade().unwrap(); match new_module.kind.get() { NormalModuleKind => return Some(new_module), ExternModuleKind | @@ -3054,15 +3014,15 @@ fn get_nearest_normal_module_parent(&mut self, module_: @Module) /// Returns the nearest normal module parent of the given module, or the /// module itself if it is a normal module. - fn get_nearest_normal_module_parent_or_self(&mut self, module_: @Module) - -> @Module { + fn get_nearest_normal_module_parent_or_self(&mut self, module_: Rc) + -> Rc { match module_.kind.get() { NormalModuleKind => return module_, ExternModuleKind | TraitModuleKind | ImplModuleKind | AnonymousModuleKind => { - match self.get_nearest_normal_module_parent(module_) { + match self.get_nearest_normal_module_parent(module_.clone()) { None => module_, Some(new_module) => new_module } @@ -3074,7 +3034,7 @@ fn get_nearest_normal_module_parent_or_self(&mut self, module_: @Module) /// (b) some chain of `super::`. /// grammar: (SELF MOD_SEP ) ? (SUPER MOD_SEP) * fn resolve_module_prefix(&mut self, - module_: @Module, + module_: Rc, module_path: &[Ident]) -> ResolveResult { // Start at the current module if we see `self` or `super`, or at the @@ -3101,7 +3061,7 @@ fn resolve_module_prefix(&mut self, break } debug!("(resolving module prefix) resolving `super` at {}", - self.module_to_str(containing_module)); + self.module_to_str(&*containing_module)); match self.get_nearest_normal_module_parent(containing_module) { None => return Failed, Some(new_module) => { @@ -3112,7 +3072,7 @@ fn resolve_module_prefix(&mut self, } debug!("(resolving module prefix) finished resolving prefix at {}", - self.module_to_str(containing_module)); + self.module_to_str(&*containing_module)); return Success(PrefixFound(containing_module, i)); } @@ -3124,7 +3084,7 @@ fn resolve_module_prefix(&mut self, /// The boolean returned on success is an indicator of whether this lookup /// passed through a public re-export proxy. fn resolve_name_in_module(&mut self, - module_: @Module, + module_: Rc, name: Ident, namespace: Namespace, name_search_type: NameSearchType, @@ -3132,16 +3092,16 @@ fn resolve_name_in_module(&mut self, -> ResolveResult<(Target, bool)> { debug!("(resolving name in module) resolving `{}` in `{}`", token::get_ident(name), - self.module_to_str(module_)); + self.module_to_str(&*module_)); // First, check the direct children of the module. - self.populate_module_if_necessary(module_); + self.populate_module_if_necessary(&module_); match module_.children.borrow().find(&name.name) { Some(name_bindings) if name_bindings.defined_in_namespace(namespace) => { debug!("(resolving name in module) found node as child"); - return Success((Target::new(module_, *name_bindings), + return Success((Target::new(module_.clone(), name_bindings.clone()), false)); } Some(_) | None => { @@ -3160,10 +3120,10 @@ fn resolve_name_in_module(&mut self, // Check the list of resolved imports. match module_.import_resolutions.borrow().find(&name.name) { Some(import_resolution) if allow_private_imports || - import_resolution.is_public.get() => { + import_resolution.is_public => { - if import_resolution.is_public.get() && - import_resolution.outstanding_references.get() != 0 { + if import_resolution.is_public && + import_resolution.outstanding_references != 0 { debug!("(resolving name in module) import \ unresolved; bailing out"); return Indeterminate; @@ -3191,7 +3151,7 @@ fn resolve_name_in_module(&mut self, None => {} Some(module) => { let name_bindings = - @Resolver::create_name_bindings_from_module(module); + Rc::new(Resolver::create_name_bindings_from_module(module)); return Success((Target::new(module_, name_bindings), false)); } } @@ -3203,7 +3163,7 @@ fn resolve_name_in_module(&mut self, return Failed; } - fn report_unresolved_imports(&mut self, module_: @Module) { + fn report_unresolved_imports(&mut self, module_: Rc) { let index = module_.resolved_import_count.get(); let imports = module_.imports.borrow(); let import_count = imports.len(); @@ -3223,9 +3183,9 @@ fn report_unresolved_imports(&mut self, module_: @Module) { } // Descend into children and anonymous children. - self.populate_module_if_necessary(module_); + self.populate_module_if_necessary(&module_); - for (_, &child_node) in module_.children.borrow().iter() { + for (_, child_node) in module_.children.borrow().iter() { match child_node.get_module_if_available() { None => { // Continue. @@ -3236,8 +3196,8 @@ fn report_unresolved_imports(&mut self, module_: @Module) { } } - for (_, &module_) in module_.anonymous_children.borrow().iter() { - self.report_unresolved_imports(module_); + for (_, module_) in module_.anonymous_children.borrow().iter() { + self.report_unresolved_imports(module_.clone()); } } @@ -3256,7 +3216,7 @@ fn record_exports(&mut self) { } fn record_exports_for_module_subtree(&mut self, - module_: @Module) { + module_: Rc) { // If this isn't a local krate, then bail out. We don't need to record // exports for nonlocal crates. @@ -3265,27 +3225,27 @@ fn record_exports_for_module_subtree(&mut self, // OK. Continue. debug!("(recording exports for module subtree) recording \ exports for local module `{}`", - self.module_to_str(module_)); + self.module_to_str(&*module_)); } None => { // Record exports for the root module. debug!("(recording exports for module subtree) recording \ exports for root module `{}`", - self.module_to_str(module_)); + self.module_to_str(&*module_)); } Some(_) => { // Bail out. debug!("(recording exports for module subtree) not recording \ exports for `{}`", - self.module_to_str(module_)); + self.module_to_str(&*module_)); return; } } - self.record_exports_for_module(module_); - self.populate_module_if_necessary(module_); + self.record_exports_for_module(&*module_); + self.populate_module_if_necessary(&module_); - for (_, &child_name_bindings) in module_.children.borrow().iter() { + for (_, child_name_bindings) in module_.children.borrow().iter() { match child_name_bindings.get_module_if_available() { None => { // Nothing to do. @@ -3296,12 +3256,12 @@ fn record_exports_for_module_subtree(&mut self, } } - for (_, &child_module) in module_.anonymous_children.borrow().iter() { - self.record_exports_for_module_subtree(child_module); + for (_, child_module) in module_.anonymous_children.borrow().iter() { + self.record_exports_for_module_subtree(child_module.clone()); } } - fn record_exports_for_module(&mut self, module_: @Module) { + fn record_exports_for_module(&mut self, module_: &Module) { let mut exports2 = Vec::new(); self.add_exports_for_module(&mut exports2, module_); @@ -3318,7 +3278,7 @@ fn record_exports_for_module(&mut self, module_: @Module) { fn add_exports_of_namebindings(&mut self, exports2: &mut Vec , name: Name, - namebindings: @NameBindings, + namebindings: &NameBindings, ns: Namespace) { match namebindings.def_for_namespace(ns) { Some(d) => { @@ -3338,9 +3298,9 @@ fn add_exports_of_namebindings(&mut self, fn add_exports_for_module(&mut self, exports2: &mut Vec , - module_: @Module) { + module_: &Module) { for (name, importresolution) in module_.import_resolutions.borrow().iter() { - if !importresolution.is_public.get() { + if !importresolution.is_public { continue } let xs = [TypeNS, ValueNS]; @@ -3351,7 +3311,7 @@ fn add_exports_for_module(&mut self, token::get_name(*name)); self.add_exports_of_namebindings(exports2, *name, - target.bindings, + &*target.bindings, ns) } _ => () @@ -3379,7 +3339,7 @@ fn add_exports_for_module(&mut self, // implementations thus found, for compatibility with old resolve pass. fn with_scope(&mut self, name: Option, f: |&mut Resolver|) { - let orig_module = self.current_module; + let orig_module = self.current_module.clone(); // Move down in the graph. match name { @@ -3387,13 +3347,13 @@ fn with_scope(&mut self, name: Option, f: |&mut Resolver|) { // Nothing to do. } Some(name) => { - self.populate_module_if_necessary(orig_module); + self.populate_module_if_necessary(&orig_module); match orig_module.children.borrow().find(&name.name) { None => { debug!("!!! (with scope) didn't find `{}` in `{}`", token::get_ident(name), - self.module_to_str(orig_module)); + self.module_to_str(&*orig_module)); } Some(name_bindings) => { match (*name_bindings).get_module_if_available() { @@ -3401,7 +3361,7 @@ fn with_scope(&mut self, name: Option, f: |&mut Resolver|) { debug!("!!! (with scope) didn't find module \ for `{}` in `{}`", token::get_ident(name), - self.module_to_str(orig_module)); + self.module_to_str(&*orig_module)); } Some(module_) => { self.current_module = module_; @@ -3420,7 +3380,7 @@ fn with_scope(&mut self, name: Option, f: |&mut Resolver|) { /// Wraps the given definition in the appropriate number of `def_upvar` /// wrappers. fn upvarify(&self, - ribs: &[@Rib], + ribs: &[Rib], rib_index: uint, def_like: DefLike, span: Span) @@ -3536,7 +3496,7 @@ fn upvarify(&self, } fn search_ribs(&self, - ribs: &[@Rib], + ribs: &[Rib], name: Name, span: Span) -> Option { @@ -3620,12 +3580,12 @@ fn resolve_item(&mut self, item: &Item) { ItemTrait(ref generics, ref traits, ref methods) => { // Create a new rib for the self type. - let self_type_rib = @Rib::new(NormalRibKind); - self.type_ribs.borrow_mut().push(self_type_rib); + let self_type_rib = Rib::new(NormalRibKind); // plain insert (no renaming) let name = self.type_self_ident.name; self_type_rib.bindings.borrow_mut() .insert(name, DlDef(DefSelfTy(item.id))); + self.type_ribs.borrow_mut().push(self_type_rib); // Create a new rib for the trait-wide type parameters. self.with_type_parameter_rib(HasTypeParameters(generics, @@ -3747,8 +3707,7 @@ fn with_type_parameter_rib(&mut self, HasTypeParameters(generics, node_id, initial_index, rib_kind) => { - let function_type_rib = @Rib::new(rib_kind); - self.type_ribs.borrow_mut().push(function_type_rib); + let function_type_rib = Rib::new(rib_kind); for (index, type_parameter) in generics.ty_params.iter().enumerate() { let ident = type_parameter.ident; @@ -3765,6 +3724,7 @@ fn with_type_parameter_rib(&mut self, function_type_rib.bindings.borrow_mut() .insert(ident.name, def_like); } + self.type_ribs.borrow_mut().push(function_type_rib); } NoTypeParameters => { @@ -3781,14 +3741,14 @@ fn with_type_parameter_rib(&mut self, } fn with_label_rib(&mut self, f: |&mut Resolver|) { - self.label_ribs.borrow_mut().push(@Rib::new(NormalRibKind)); + self.label_ribs.borrow_mut().push(Rib::new(NormalRibKind)); f(self); self.label_ribs.borrow_mut().pop(); } fn with_constant_rib(&mut self, f: |&mut Resolver|) { - self.value_ribs.borrow_mut().push(@Rib::new(ConstantItemRibKind)); - self.type_ribs.borrow_mut().push(@Rib::new(ConstantItemRibKind)); + self.value_ribs.borrow_mut().push(Rib::new(ConstantItemRibKind)); + self.type_ribs.borrow_mut().push(Rib::new(ConstantItemRibKind)); f(self); self.type_ribs.borrow_mut().pop(); self.value_ribs.borrow_mut().pop(); @@ -3800,11 +3760,11 @@ fn resolve_function(&mut self, type_parameters: TypeParameters, block: P) { // Create a value rib for the function. - let function_value_rib = @Rib::new(rib_kind); + let function_value_rib = Rib::new(rib_kind); self.value_ribs.borrow_mut().push(function_value_rib); // Create a label rib for the function. - let function_label_rib = @Rib::new(rib_kind); + let function_label_rib = Rib::new(rib_kind); self.label_ribs.borrow_mut().push(function_label_rib); // If this function has type parameters, add them now. @@ -3955,7 +3915,7 @@ fn resolve_struct(&mut self, // to be NormalRibKind? fn resolve_method(&mut self, rib_kind: RibKind, - method: @Method, + method: &Method, outer_type_parameter_count: uint) { let method_generics = &method.generics; let type_parameters = @@ -4068,7 +4028,7 @@ fn resolve_local(&mut self, local: &Local) { // this is done hygienically. This could arise for a macro // that expands into an or-pattern where one 'x' was from the // user and one 'x' came from the macro. - fn binding_mode_map(&mut self, pat: @Pat) -> BindingMap { + fn binding_mode_map(&mut self, pat: &Pat) -> BindingMap { let mut result = HashMap::new(); pat_bindings(&self.def_map, pat, |binding_mode, _id, sp, path| { let name = mtwt::resolve(path_to_ident(path)); @@ -4126,7 +4086,7 @@ fn check_consistent_bindings(&mut self, arm: &Arm) { } fn resolve_arm(&mut self, arm: &Arm) { - self.value_ribs.borrow_mut().push(@Rib::new(NormalRibKind)); + self.value_ribs.borrow_mut().push(Rib::new(NormalRibKind)); let mut bindings_list = HashMap::new(); for pattern in arm.pats.iter() { @@ -4147,19 +4107,16 @@ fn resolve_arm(&mut self, arm: &Arm) { fn resolve_block(&mut self, block: &Block) { debug!("(resolving block) entering block"); - self.value_ribs.borrow_mut().push(@Rib::new(NormalRibKind)); + self.value_ribs.borrow_mut().push(Rib::new(NormalRibKind)); // Move down in the graph, if there's an anonymous module rooted here. - let orig_module = self.current_module; - let anonymous_children = self.current_module - .anonymous_children - .borrow(); - match anonymous_children.find(&block.id) { + let orig_module = self.current_module.clone(); + match orig_module.anonymous_children.borrow().find(&block.id) { None => { /* Nothing to do. */ } - Some(&anonymous_module) => { + Some(anonymous_module) => { debug!("(resolving block) found anonymous module, moving \ down"); - self.current_module = anonymous_module; + self.current_module = anonymous_module.clone(); } } @@ -4277,7 +4234,7 @@ fn resolve_type(&mut self, ty: &Ty) { } fn resolve_pattern(&mut self, - pattern: @Pat, + pattern: &Pat, mode: PatternBindingMode, // Maps idents to the node ID for the (outermost) // pattern that binds them @@ -4528,9 +4485,9 @@ struct in scope", } fn resolve_bare_identifier_pattern(&mut self, name: Ident) - -> - BareIdentifierPatternResolution { - match self.resolve_item_in_lexical_scope(self.current_module, + -> BareIdentifierPatternResolution { + let module = self.current_module.clone(); + match self.resolve_item_in_lexical_scope(module, name, ValueNS, SearchThroughModules) { @@ -4643,12 +4600,12 @@ fn resolve_identifier(&mut self, // FIXME #4952: Merge me with resolve_name_in_module? fn resolve_definition_of_name_in_module(&mut self, - containing_module: @Module, + containing_module: Rc, name: Ident, namespace: Namespace) -> NameDefinition { // First, search children. - self.populate_module_if_necessary(containing_module); + self.populate_module_if_necessary(&containing_module); match containing_module.children.borrow().find(&name.name) { Some(child_name_bindings) => { @@ -4670,7 +4627,7 @@ fn resolve_definition_of_name_in_module(&mut self, // Next, search import resolutions. match containing_module.import_resolutions.borrow().find(&name.name) { - Some(import_resolution) if import_resolution.is_public.get() => { + Some(import_resolution) if import_resolution.is_public => { match (*import_resolution).target_for_namespace(namespace) { Some(target) => { match target.bindings.def_for_namespace(namespace) { @@ -4725,7 +4682,8 @@ fn resolve_module_relative_path(&mut self, let containing_module; let last_private; - match self.resolve_module_path(self.current_module, + let module = self.current_module.clone(); + match self.resolve_module_path(module, module_path_idents.as_slice(), UseLexicalScope, path.span, @@ -4748,7 +4706,7 @@ fn resolve_module_relative_path(&mut self, } let ident = path.segments.last().unwrap().identifier; - let def = match self.resolve_definition_of_name_in_module(containing_module, + let def = match self.resolve_definition_of_name_in_module(containing_module.clone(), ident, namespace) { NoNameDefinition => { @@ -4863,7 +4821,8 @@ fn resolve_item_by_identifier_in_lexical_scope(&mut self, namespace: Namespace) -> Option<(Def, LastPrivate)> { // Check the items. - match self.resolve_item_in_lexical_scope(self.current_module, + let module = self.current_module.clone(); + match self.resolve_item_in_lexical_scope(module, ident, namespace, DontSearchThroughModules) { @@ -5148,7 +5107,7 @@ fn add_trait_info(found_traits: &mut Vec, } let mut found_traits = Vec::new(); - let mut search_module = self.current_module; + let mut search_module = self.current_module.clone(); loop { // Look for the current trait. match self.current_trait_refs { @@ -5166,11 +5125,11 @@ fn add_trait_info(found_traits: &mut Vec, } // Look for trait children. - self.populate_module_if_necessary(search_module); + self.populate_module_if_necessary(&search_module); { let method_set = self.method_set.borrow(); - for (_, &child_names) in search_module.children.borrow().iter() { + for (_, child_names) in search_module.children.borrow().iter() { let def = match child_names.def_for_namespace(TypeNS) { Some(def) => def, None => continue @@ -5186,9 +5145,7 @@ fn add_trait_info(found_traits: &mut Vec, } // Look for imports. - let import_resolutions = search_module.import_resolutions - .borrow(); - for (_, &import) in import_resolutions.iter() { + for (_, import) in search_module.import_resolutions.borrow().iter() { let target = match import.target_for_namespace(TypeNS) { None => continue, Some(target) => target, @@ -5199,14 +5156,14 @@ fn add_trait_info(found_traits: &mut Vec, }; if self.method_set.borrow().contains(&(name, did)) { add_trait_info(&mut found_traits, did, name); - self.used_imports.insert((import.type_id.get(), TypeNS)); + self.used_imports.insert((import.type_id, TypeNS)); } } - match search_module.parent_link { + match search_module.parent_link.clone() { NoParentLink | ModuleParentLink(..) => break, BlockParentLink(parent_module, _) => { - search_module = parent_module; + search_module = parent_module.upgrade().unwrap(); } } } @@ -5348,40 +5305,38 @@ fn finalize_import(&mut self, id: NodeId, span: Span) { // /// A somewhat inefficient routine to obtain the name of a module. - fn module_to_str(&mut self, module_: @Module) -> ~str { + fn module_to_str(&mut self, module: &Module) -> ~str { let mut idents = Vec::new(); - let mut current_module = module_; - loop { - match current_module.parent_link { - NoParentLink => { - break; - } - ModuleParentLink(module_, name) => { + + fn collect_mod(idents: &mut Vec, module: &Module) { + match module.parent_link { + NoParentLink => {} + ModuleParentLink(ref module, name) => { idents.push(name); - current_module = module_; + collect_mod(idents, &*module.upgrade().unwrap()); } - BlockParentLink(module_, _) => { + BlockParentLink(ref module, _) => { idents.push(special_idents::opaque); - current_module = module_; + collect_mod(idents, &*module.upgrade().unwrap()); } } } + collect_mod(&mut idents, module); if idents.len() == 0 { return "???".to_owned(); } - return self.idents_to_str(idents.move_iter() - .rev() - .collect::>() - .as_slice()); + self.idents_to_str(idents.move_iter().rev() + .collect::>() + .as_slice()) } #[allow(dead_code)] // useful for debugging - fn dump_module(&mut self, module_: @Module) { - debug!("Dump of module `{}`:", self.module_to_str(module_)); + fn dump_module(&mut self, module_: Rc) { + debug!("Dump of module `{}`:", self.module_to_str(&*module_)); debug!("Children:"); - self.populate_module_if_necessary(module_); + self.populate_module_if_necessary(&module_); for (&name, _) in module_.children.borrow().iter() { debug!("* {}", token::get_name(name)); }