1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Reduced graph building
13 //! Here we build the "reduced graph": the graph of the module tree without
14 //! any imports resolved.
17 use resolve_imports::ImportDirective;
18 use resolve_imports::ImportDirectiveSubclass::{self, SingleImport, GlobImport};
19 use resolve_imports::ImportResolution;
21 use Namespace::{TypeNS, ValueNS};
23 use {names_to_string, module_to_string};
24 use ParentLink::{self, ModuleParentLink, BlockParentLink};
26 use resolve_imports::Shadowable;
27 use {resolve_error, ResolutionError};
29 use self::DuplicateCheckingMode::*;
30 use self::NamespaceError::*;
32 use rustc::metadata::csearch;
33 use rustc::metadata::decoder::{DefLike, DlDef, DlField, DlImpl};
34 use rustc::middle::def::*;
35 use rustc::middle::def_id::{CRATE_DEF_INDEX, DefId};
37 use syntax::ast::{Name, NodeId};
38 use syntax::attr::AttrMetaMethods;
39 use syntax::parse::token::special_idents;
40 use syntax::codemap::{Span, DUMMY_SP};
43 use rustc_front::hir::{Block, Crate, DeclItem};
44 use rustc_front::hir::{ForeignItem, ForeignItemFn, ForeignItemStatic};
45 use rustc_front::hir::{Item, ItemConst, ItemEnum, ItemExternCrate, ItemFn};
46 use rustc_front::hir::{ItemForeignMod, ItemImpl, ItemMod, ItemStatic, ItemDefaultImpl};
47 use rustc_front::hir::{ItemStruct, ItemTrait, ItemTy, ItemUse};
48 use rustc_front::hir::{NamedField, PathListIdent, PathListMod, Public};
49 use rustc_front::hir::StmtDecl;
50 use rustc_front::hir::UnnamedField;
51 use rustc_front::hir::{Variant, ViewPathGlob, ViewPathList, ViewPathSimple};
52 use rustc_front::hir::Visibility;
53 use rustc_front::visit::{self, Visitor};
55 use std::mem::replace;
56 use std::ops::{Deref, DerefMut};
59 // Specifies how duplicates should be handled when adding a child item if
60 // another item exists with the same name in some namespace.
61 #[derive(Copy, Clone, PartialEq)]
62 enum DuplicateCheckingMode {
63 ForbidDuplicateModules,
64 ForbidDuplicateTypesAndModules,
65 ForbidDuplicateValues,
66 ForbidDuplicateTypesAndValues,
70 #[derive(Copy, Clone, PartialEq)]
78 fn namespace_error_to_string(ns: NamespaceError) -> &'static str {
81 ModuleError | TypeError => "type or module",
82 ValueError => "value",
86 struct GraphBuilder<'a, 'b: 'a, 'tcx: 'b> {
87 resolver: &'a mut Resolver<'b, 'tcx>,
90 impl<'a, 'b:'a, 'tcx:'b> Deref for GraphBuilder<'a, 'b, 'tcx> {
91 type Target = Resolver<'b, 'tcx>;
93 fn deref(&self) -> &Resolver<'b, 'tcx> {
98 impl<'a, 'b:'a, 'tcx:'b> DerefMut for GraphBuilder<'a, 'b, 'tcx> {
99 fn deref_mut(&mut self) -> &mut Resolver<'b, 'tcx> {
104 impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
105 /// Constructs the reduced graph for the entire crate.
106 fn build_reduced_graph(self, krate: &hir::Crate) {
107 let parent = self.graph_root.get_module();
108 let mut visitor = BuildReducedGraphVisitor {
112 visit::walk_crate(&mut visitor, krate);
115 /// Adds a new child item to the module definition of the parent node and
116 /// returns its corresponding name bindings as well as the current parent.
117 /// Or, if we're inside a block, creates (or reuses) an anonymous module
118 /// corresponding to the innermost block ID and returns the name bindings
119 /// as well as the newly-created parent.
123 /// Panics if this node does not have a module definition and we are not inside
128 duplicate_checking_mode: DuplicateCheckingMode,
129 // For printing errors
132 // If this is the immediate descendant of a module, then we add the
133 // child name directly. Otherwise, we create or reuse an anonymous
134 // module and add the child to that.
136 self.check_for_conflicts_between_external_crates_and_items(&**parent, name, sp);
138 // Add or reuse the child.
139 let child = parent.children.borrow().get(&name).cloned();
142 let child = NameBindings::new();
143 parent.children.borrow_mut().insert(name, child.clone());
147 // Enforce the duplicate checking mode:
149 // * If we're requesting duplicate module checking, check that
150 // there isn't a module in the module with the same name.
152 // * If we're requesting duplicate type checking, check that
153 // there isn't a type in the module with the same name.
155 // * If we're requesting duplicate value checking, check that
156 // there isn't a value in the module with the same name.
158 // * If we're requesting duplicate type checking and duplicate
159 // value checking, check that there isn't a duplicate type
160 // and a duplicate value with the same name.
162 // * If no duplicate checking was requested at all, do
165 let mut duplicate_type = NoError;
166 let ns = match duplicate_checking_mode {
167 ForbidDuplicateModules => {
168 if child.get_module_if_available().is_some() {
169 duplicate_type = ModuleError;
173 ForbidDuplicateTypesAndModules => {
174 if child.type_ns.defined() {
175 duplicate_type = TypeError;
179 ForbidDuplicateValues => {
180 if child.value_ns.defined() {
181 duplicate_type = ValueError;
185 ForbidDuplicateTypesAndValues => {
187 match child.type_ns.def() {
188 Some(DefMod(_)) | None => {}
191 duplicate_type = TypeError;
194 if child.value_ns.defined() {
195 duplicate_type = ValueError;
200 OverwriteDuplicates => None,
202 if duplicate_type != NoError {
203 // Return an error here by looking up the namespace that
204 // had the duplicate.
205 let ns = ns.unwrap();
209 ResolutionError::DuplicateDefinition(
210 namespace_error_to_string(duplicate_type),
214 let r = child[ns].span();
215 if let Some(sp) = r {
216 self.session.span_note(sp,
217 &format!("first definition of {} `{}` here",
218 namespace_error_to_string(duplicate_type),
228 fn block_needs_anonymous_module(&mut self, block: &Block) -> bool {
229 // Check each statement.
230 for statement in &block.stmts {
231 match statement.node {
232 StmtDecl(ref declaration, _) => {
233 match declaration.node {
248 // If we found no items, we don't need to create
249 // an anonymous module.
254 fn get_parent_link(&mut self, parent: &Rc<Module>, name: Name) -> ParentLink {
255 ModuleParentLink(Rc::downgrade(parent), name)
258 /// Constructs the reduced graph for one item.
259 fn build_reduced_graph_for_item(&mut self, item: &Item, parent: &Rc<Module>) -> Rc<Module> {
260 let name = item.name;
262 let is_public = item.vis == hir::Public;
263 let modifiers = if is_public {
266 DefModifiers::empty()
267 } | DefModifiers::IMPORTABLE;
270 ItemUse(ref view_path) => {
271 // Extract and intern the module part of the path. For
272 // globs and lists, the path is found directly in the AST;
273 // for simple paths we have to munge the path a little.
274 let module_path = match view_path.node {
275 ViewPathSimple(_, ref full_path) => {
281 .map(|seg| seg.identifier.name)
285 ViewPathGlob(ref module_ident_path) |
286 ViewPathList(ref module_ident_path, _) => {
287 module_ident_path.segments
289 .map(|seg| seg.identifier.name)
294 // Build up the import directives.
295 let shadowable = item.attrs.iter().any(|attr| {
296 attr.name() == special_idents::prelude_import.name.as_str()
298 let shadowable = if shadowable {
304 match view_path.node {
305 ViewPathSimple(binding, ref full_path) => {
306 let source_name = full_path.segments.last().unwrap().identifier.name;
307 if source_name.as_str() == "mod" || source_name.as_str() == "self" {
310 ResolutionError::SelfImportsOnlyAllowedWithin);
313 let subclass = SingleImport(binding, source_name);
314 self.build_import_directive(&**parent,
322 ViewPathList(_, ref source_items) => {
323 // Make sure there's at most one `mod` import in the list.
324 let mod_spans = source_items.iter()
327 PathListMod { .. } => Some(item.span),
331 .collect::<Vec<Span>>();
332 if mod_spans.len() > 1 {
335 ResolutionError::SelfImportCanOnlyAppearOnceInTheList);
336 for other_span in mod_spans.iter().skip(1) {
338 .span_note(*other_span, "another `self` import appears here");
342 for source_item in source_items {
343 let (module_path, name, rename) = match source_item.node {
344 PathListIdent { name, rename, .. } =>
345 (module_path.clone(), name, rename.unwrap_or(name)),
346 PathListMod { rename, .. } => {
347 let name = match module_path.last() {
354 SelfImportOnlyInImportListWithNonEmptyPrefix
359 let module_path = module_path.split_last().unwrap().1;
360 let rename = rename.unwrap_or(name);
361 (module_path.to_vec(), name, rename)
364 self.build_import_directive(&**parent,
366 SingleImport(rename, name),
368 source_item.node.id(),
374 self.build_import_directive(&**parent,
386 ItemExternCrate(_) => {
387 // n.b. we don't need to look at the path option here, because cstore already
389 if let Some(crate_id) = self.session.cstore.find_extern_mod_stmt_cnum(item.id) {
392 index: CRATE_DEF_INDEX,
394 self.external_exports.insert(def_id);
395 let parent_link = ModuleParentLink(Rc::downgrade(parent), name);
396 let external_module = Rc::new(Module::new(parent_link,
397 Some(DefMod(def_id)),
400 debug!("(build reduced graph for item) found extern `{}`",
401 module_to_string(&*external_module));
402 self.check_for_conflicts_between_external_crates(&**parent, name, sp);
403 parent.external_module_children
405 .insert(name, external_module.clone());
406 self.build_reduced_graph_for_external_crate(&external_module);
412 let child = parent.children.borrow().get(&name).cloned();
413 if let Some(child) = child {
414 // check if there's struct of the same name already defined
415 if child.type_ns.defined() &&
416 child.get_module_if_available().is_none() {
417 self.session.span_warn(sp,
418 &format!("duplicate definition of {} `{}`. \
419 Defining a module and a struct with \
420 the same name will be disallowed soon.",
421 namespace_error_to_string(TypeError),
424 let r = child.type_ns.span();
425 if let Some(sp) = r {
426 self.session.span_note(sp,
427 &format!("first definition of {} `{}` here",
428 namespace_error_to_string(TypeError),
434 let name_bindings = self.add_child(name, parent, ForbidDuplicateModules, sp);
436 let parent_link = self.get_parent_link(parent, name);
437 let def = DefMod(self.ast_map.local_def_id(item.id));
438 name_bindings.define_module(parent_link, Some(def), false, is_public, sp);
440 name_bindings.get_module()
443 ItemForeignMod(..) => parent.clone(),
445 // These items live in the value namespace.
446 ItemStatic(_, m, _) => {
447 let name_bindings = self.add_child(name, parent, ForbidDuplicateValues, sp);
448 let mutbl = m == hir::MutMutable;
450 name_bindings.define_value(DefStatic(self.ast_map.local_def_id(item.id), mutbl),
456 self.add_child(name, parent, ForbidDuplicateValues, sp)
457 .define_value(DefConst(self.ast_map.local_def_id(item.id)), sp, modifiers);
460 ItemFn(_, _, _, _, _, _) => {
461 let name_bindings = self.add_child(name, parent, ForbidDuplicateValues, sp);
463 let def = DefFn(self.ast_map.local_def_id(item.id), false);
464 name_bindings.define_value(def, sp, modifiers);
468 // These items live in the type namespace.
470 let name_bindings = self.add_child(name,
472 ForbidDuplicateTypesAndModules,
475 let parent_link = self.get_parent_link(parent, name);
476 let def = DefTy(self.ast_map.local_def_id(item.id), false);
477 name_bindings.define_module(parent_link, Some(def), false, is_public, sp);
481 ItemEnum(ref enum_definition, _) => {
482 let name_bindings = self.add_child(name,
484 ForbidDuplicateTypesAndModules,
487 let parent_link = self.get_parent_link(parent, name);
488 let def = DefTy(self.ast_map.local_def_id(item.id), true);
489 name_bindings.define_module(parent_link, Some(def), false, is_public, sp);
491 let module = name_bindings.get_module();
493 for variant in &(*enum_definition).variants {
494 let item_def_id = self.ast_map.local_def_id(item.id);
495 self.build_reduced_graph_for_variant(&**variant, item_def_id, &module);
500 // These items live in both the type and value namespaces.
501 ItemStruct(ref struct_def, _) => {
502 // Adding to both Type and Value namespaces or just Type?
503 let (forbid, ctor_id) = if struct_def.is_struct() {
504 (ForbidDuplicateTypesAndModules, None)
506 let child = parent.children.borrow().get(&name).cloned();
507 if let Some(child) = child {
508 // check if theres a DefMod
509 if let Some(DefMod(_)) = child.type_ns.def() {
510 self.session.span_warn(sp,
511 &format!("duplicate definition of {} `{}`. \
512 Defining a module and a struct \
513 with the same name will be \
515 namespace_error_to_string(TypeError),
518 let r = child.type_ns.span();
519 if let Some(sp) = r {
522 &format!("first definition of {} `{}` here",
523 namespace_error_to_string(TypeError),
529 (ForbidDuplicateTypesAndValues, Some(struct_def.id()))
532 let name_bindings = self.add_child(name, parent, forbid, sp);
534 // Define a name in the type namespace.
535 name_bindings.define_type(DefTy(self.ast_map.local_def_id(item.id), false),
539 // If this is a newtype or unit-like struct, define a name
540 // in the value namespace as well
541 if let Some(cid) = ctor_id {
542 name_bindings.define_value(DefStruct(self.ast_map.local_def_id(cid)),
547 // Record the def ID and fields of this struct.
548 let named_fields = struct_def.fields()
552 NamedField(name, _) => Some(name),
553 UnnamedField(_) => None,
557 let item_def_id = self.ast_map.local_def_id(item.id);
558 self.structs.insert(item_def_id, named_fields);
563 ItemDefaultImpl(_, _) |
564 ItemImpl(..) => parent.clone(),
566 ItemTrait(_, _, _, ref items) => {
567 let name_bindings = self.add_child(name,
569 ForbidDuplicateTypesAndModules,
572 let def_id = self.ast_map.local_def_id(item.id);
574 // Add all the items within to a new module.
575 let parent_link = self.get_parent_link(parent, name);
576 let def = DefTrait(def_id);
577 name_bindings.define_module(parent_link, Some(def), false, is_public, sp);
578 let module_parent = name_bindings.get_module();
580 // Add the names of all the items to the trait info.
581 for trait_item in items {
582 let name_bindings = self.add_child(trait_item.name,
584 ForbidDuplicateTypesAndValues,
587 match trait_item.node {
588 hir::ConstTraitItem(..) => {
589 let def = DefAssociatedConst(self.ast_map.local_def_id(trait_item.id));
590 // NB: not DefModifiers::IMPORTABLE
591 name_bindings.define_value(def, trait_item.span, DefModifiers::PUBLIC);
593 hir::MethodTraitItem(..) => {
594 let def = DefMethod(self.ast_map.local_def_id(trait_item.id));
595 // NB: not DefModifiers::IMPORTABLE
596 name_bindings.define_value(def, trait_item.span, DefModifiers::PUBLIC);
598 hir::TypeTraitItem(..) => {
599 let def = DefAssociatedTy(self.ast_map.local_def_id(item.id),
600 self.ast_map.local_def_id(trait_item.id));
601 // NB: not DefModifiers::IMPORTABLE
602 name_bindings.define_type(def, trait_item.span, DefModifiers::PUBLIC);
606 let trait_item_def_id = self.ast_map.local_def_id(trait_item.id);
607 self.trait_item_map.insert((trait_item.name, def_id), trait_item_def_id);
615 // Constructs the reduced graph for one variant. Variants exist in the
616 // type and value namespaces.
617 fn build_reduced_graph_for_variant(&mut self,
620 parent: &Rc<Module>) {
621 let name = variant.node.name;
622 let is_exported = if variant.node.data.is_struct() {
623 // Not adding fields for variants as they are not accessed with a self receiver
624 let variant_def_id = self.ast_map.local_def_id(variant.node.data.id());
625 self.structs.insert(variant_def_id, Vec::new());
631 let child = self.add_child(name, parent, ForbidDuplicateTypesAndValues, variant.span);
632 // variants are always treated as importable to allow them to be glob
634 child.define_value(DefVariant(item_id,
635 self.ast_map.local_def_id(variant.node.data.id()),
638 DefModifiers::PUBLIC | DefModifiers::IMPORTABLE);
639 child.define_type(DefVariant(item_id,
640 self.ast_map.local_def_id(variant.node.data.id()),
643 DefModifiers::PUBLIC | DefModifiers::IMPORTABLE);
646 /// Constructs the reduced graph for one foreign item.
647 fn build_reduced_graph_for_foreign_item(&mut self,
648 foreign_item: &ForeignItem,
649 parent: &Rc<Module>) {
650 let name = foreign_item.name;
651 let is_public = foreign_item.vis == hir::Public;
652 let modifiers = if is_public {
655 DefModifiers::empty()
656 } | DefModifiers::IMPORTABLE;
657 let name_bindings = self.add_child(name, parent, ForbidDuplicateValues, foreign_item.span);
659 let def = match foreign_item.node {
660 ForeignItemFn(..) => {
661 DefFn(self.ast_map.local_def_id(foreign_item.id), false)
663 ForeignItemStatic(_, m) => {
664 DefStatic(self.ast_map.local_def_id(foreign_item.id), m)
667 name_bindings.define_value(def, foreign_item.span, modifiers);
670 fn build_reduced_graph_for_block(&mut self, block: &Block, parent: &Rc<Module>) -> Rc<Module> {
671 if self.block_needs_anonymous_module(block) {
672 let block_id = block.id;
674 debug!("(building reduced graph for block) creating a new anonymous module for block \
678 let new_module = Rc::new(Module::new(BlockParentLink(Rc::downgrade(parent), block_id),
682 parent.anonymous_children.borrow_mut().insert(block_id, new_module.clone());
689 fn handle_external_def(&mut self,
692 child_name_bindings: &NameBindings,
695 new_parent: &Rc<Module>) {
696 debug!("(building reduced graph for external crate) building external def {}, priv {:?}",
699 let is_public = vis == hir::Public;
700 let modifiers = if is_public {
703 DefModifiers::empty()
704 } | DefModifiers::IMPORTABLE;
705 let is_exported = is_public &&
706 match new_parent.def_id() {
708 Some(did) => self.external_exports.contains(&did),
711 self.external_exports.insert(def.def_id());
719 if let Some(module_def) = child_name_bindings.type_ns.module() {
720 debug!("(building reduced graph for external crate) already created module");
721 module_def.def.set(Some(def));
723 debug!("(building reduced graph for external crate) building module {} {}",
726 let parent_link = self.get_parent_link(new_parent, name);
728 child_name_bindings.define_module(parent_link,
739 DefMod(_) | DefForeignMod(_) => {}
740 DefVariant(_, variant_id, is_struct) => {
741 debug!("(building reduced graph for external crate) building variant {}",
743 // variants are always treated as importable to allow them to be
745 let modifiers = DefModifiers::PUBLIC | DefModifiers::IMPORTABLE;
747 child_name_bindings.define_type(def, DUMMY_SP, modifiers);
748 // Not adding fields for variants as they are not accessed with a self receiver
749 self.structs.insert(variant_id, Vec::new());
751 child_name_bindings.define_value(def, DUMMY_SP, modifiers);
754 DefFn(ctor_id, true) => {
755 child_name_bindings.define_value(
756 csearch::get_tuple_struct_definition_if_ctor(&self.session.cstore, ctor_id)
757 .map_or(def, |_| DefStruct(ctor_id)), DUMMY_SP, modifiers);
762 DefAssociatedConst(..) |
764 debug!("(building reduced graph for external crate) building value (fn/static) {}",
766 // impl methods have already been defined with the correct importability
768 let mut modifiers = match *child_name_bindings.value_ns.borrow() {
769 Some(ref def) => (modifiers & !DefModifiers::IMPORTABLE) |
770 (def.modifiers & DefModifiers::IMPORTABLE),
773 if !new_parent.is_normal() {
774 modifiers = modifiers & !DefModifiers::IMPORTABLE;
776 child_name_bindings.define_value(def, DUMMY_SP, modifiers);
778 DefTrait(def_id) => {
779 debug!("(building reduced graph for external crate) building type {}",
782 // If this is a trait, add all the trait item names to the trait
785 let trait_item_def_ids = csearch::get_trait_item_def_ids(&self.session.cstore,
787 for trait_item_def in &trait_item_def_ids {
788 let trait_item_name = csearch::get_trait_name(&self.session.cstore,
789 trait_item_def.def_id());
791 debug!("(building reduced graph for external crate) ... adding trait item \
795 self.trait_item_map.insert((trait_item_name, def_id), trait_item_def.def_id());
798 self.external_exports.insert(trait_item_def.def_id());
802 // Define a module if necessary.
803 let parent_link = self.get_parent_link(new_parent, name);
804 child_name_bindings.define_module(parent_link,
810 DefTy(..) | DefAssociatedTy(..) => {
811 debug!("(building reduced graph for external crate) building type {}",
814 let modifiers = match new_parent.is_normal() {
816 _ => modifiers & !DefModifiers::IMPORTABLE,
819 if let DefTy(..) = def {
820 child_name_bindings.type_ns.set_modifiers(modifiers);
822 child_name_bindings.define_type(def, DUMMY_SP, modifiers);
825 DefStruct(def_id) => {
826 debug!("(building reduced graph for external crate) building type and value for \
829 let fields = csearch::get_struct_field_names(&self.session.cstore, def_id);
831 if fields.is_empty() {
832 child_name_bindings.define_value(def, DUMMY_SP, modifiers);
835 // Record the def ID and fields of this struct.
836 self.structs.insert(def_id, fields);
845 panic!("didn't expect `{:?}`", def);
850 /// Builds the reduced graph for a single item in an external crate.
851 fn build_reduced_graph_for_external_crate_def(&mut self,
855 def_visibility: Visibility) {
858 // Add the new child item, if necessary.
860 DefForeignMod(def_id) => {
861 // Foreign modules have no names. Recur and populate
863 csearch::each_child_of_item(&self.session.cstore,
868 self.build_reduced_graph_for_external_crate_def(
876 let child_name_bindings = self.add_child(name,
881 self.handle_external_def(def,
883 &child_name_bindings,
891 debug!("(building reduced graph for external crate) ignoring impl");
894 debug!("(building reduced graph for external crate) ignoring field");
899 /// Builds the reduced graph rooted at the given external module.
900 fn populate_external_module(&mut self, module: &Rc<Module>) {
901 debug!("(populating external module) attempting to populate {}",
902 module_to_string(&**module));
904 let def_id = match module.def_id() {
906 debug!("(populating external module) ... no def ID!");
909 Some(def_id) => def_id,
912 csearch::each_child_of_item(&self.session.cstore,
914 |def_like, child_name, visibility| {
915 debug!("(populating external module) ... found ident: {}",
917 self.build_reduced_graph_for_external_crate_def(module,
922 module.populated.set(true)
925 /// Ensures that the reduced graph rooted at the given external module
926 /// is built, building it if it is not.
927 fn populate_module_if_necessary(&mut self, module: &Rc<Module>) {
928 if !module.populated.get() {
929 self.populate_external_module(module)
931 assert!(module.populated.get())
934 /// Builds the reduced graph rooted at the 'use' directive for an external
936 fn build_reduced_graph_for_external_crate(&mut self, root: &Rc<Module>) {
937 csearch::each_top_level_item_of_crate(&self.session.cstore,
941 |def_like, name, visibility| {
942 self.build_reduced_graph_for_external_crate_def(root, def_like, name, visibility)
946 /// Creates and adds an import directive to the given module.
947 fn build_import_directive(&mut self,
949 module_path: Vec<Name>,
950 subclass: ImportDirectiveSubclass,
954 shadowable: Shadowable) {
957 .push(ImportDirective::new(module_path, subclass, span, id, is_public, shadowable));
958 self.unresolved_imports += 1;
961 module_.inc_pub_count();
964 // Bump the reference count on the name. Or, if this is a glob, set
965 // the appropriate flag.
968 SingleImport(target, _) => {
969 debug!("(building import directive) building import directive: {}::{}",
970 names_to_string(&module_.imports.borrow().last().unwrap().module_path),
973 let mut import_resolutions = module_.import_resolutions.borrow_mut();
974 match import_resolutions.get_mut(&target) {
975 Some(resolution) => {
976 debug!("(building import directive) bumping reference");
977 resolution.outstanding_references += 1;
979 // the source of this name is different now
980 resolution.type_id = id;
981 resolution.value_id = id;
982 resolution.is_public = is_public;
987 debug!("(building import directive) creating new");
988 let mut resolution = ImportResolution::new(id, is_public);
989 resolution.outstanding_references = 1;
990 import_resolutions.insert(target, resolution);
993 // Set the glob flag. This tells us that we don't know the
994 // module's exports ahead of time.
996 module_.inc_glob_count();
998 module_.inc_pub_glob_count();
1005 struct BuildReducedGraphVisitor<'a, 'b: 'a, 'tcx: 'b> {
1006 builder: GraphBuilder<'a, 'b, 'tcx>,
1010 impl<'a, 'b, 'v, 'tcx> Visitor<'v> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
1011 fn visit_item(&mut self, item: &Item) {
1012 let p = self.builder.build_reduced_graph_for_item(item, &self.parent);
1013 let old_parent = replace(&mut self.parent, p);
1014 visit::walk_item(self, item);
1015 self.parent = old_parent;
1018 fn visit_foreign_item(&mut self, foreign_item: &ForeignItem) {
1019 self.builder.build_reduced_graph_for_foreign_item(foreign_item, &self.parent);
1022 fn visit_block(&mut self, block: &Block) {
1023 let np = self.builder.build_reduced_graph_for_block(block, &self.parent);
1024 let old_parent = replace(&mut self.parent, np);
1025 visit::walk_block(self, block);
1026 self.parent = old_parent;
1030 pub fn build_reduced_graph(resolver: &mut Resolver, krate: &hir::Crate) {
1031 GraphBuilder { resolver: resolver }.build_reduced_graph(krate);
1034 pub fn populate_module_if_necessary(resolver: &mut Resolver, module: &Rc<Module>) {
1035 GraphBuilder { resolver: resolver }.populate_module_if_necessary(module);