2014-12-30 12:16:42 -06:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! Reduced graph building
|
|
|
|
//!
|
|
|
|
//! Here we build the "reduced graph": the graph of the module tree without
|
|
|
|
//! any imports resolved.
|
|
|
|
|
2015-04-28 16:36:22 -07:00
|
|
|
use DefModifiers;
|
2015-03-16 10:44:19 +13:00
|
|
|
use resolve_imports::ImportDirective;
|
|
|
|
use resolve_imports::ImportDirectiveSubclass::{self, SingleImport, GlobImport};
|
|
|
|
use resolve_imports::ImportResolution;
|
2014-12-30 12:16:42 -06:00
|
|
|
use Module;
|
|
|
|
use Namespace::{TypeNS, ValueNS};
|
|
|
|
use NameBindings;
|
2015-03-16 10:44:19 +13:00
|
|
|
use {names_to_string, module_to_string};
|
2015-01-03 22:42:21 -05:00
|
|
|
use ParentLink::{self, ModuleParentLink, BlockParentLink};
|
2014-12-30 12:16:42 -06:00
|
|
|
use Resolver;
|
2015-03-16 10:44:19 +13:00
|
|
|
use resolve_imports::Shadowable;
|
2015-07-14 19:42:38 +02:00
|
|
|
use {resolve_error, ResolutionError};
|
2014-12-30 12:16:42 -06:00
|
|
|
|
|
|
|
use self::DuplicateCheckingMode::*;
|
|
|
|
|
2015-11-20 17:46:39 +02:00
|
|
|
use rustc::metadata::util::{CrateStore, ChildItem, DlDef, DlField, DlImpl};
|
2014-12-30 12:16:42 -06:00
|
|
|
use rustc::middle::def::*;
|
2015-09-17 14:29:59 -04:00
|
|
|
use rustc::middle::def_id::{CRATE_DEF_INDEX, DefId};
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
use syntax::ast::{Name, NodeId};
|
2015-09-14 21:58:20 +12:00
|
|
|
use syntax::attr::AttrMetaMethods;
|
2015-07-28 18:07:20 +02:00
|
|
|
use syntax::parse::token::special_idents;
|
2014-12-30 12:16:42 -06:00
|
|
|
use syntax::codemap::{Span, DUMMY_SP};
|
2015-07-31 00:04:06 -07:00
|
|
|
|
|
|
|
use rustc_front::hir;
|
|
|
|
use rustc_front::hir::{Block, Crate, DeclItem};
|
|
|
|
use rustc_front::hir::{ForeignItem, ForeignItemFn, ForeignItemStatic};
|
|
|
|
use rustc_front::hir::{Item, ItemConst, ItemEnum, ItemExternCrate, ItemFn};
|
|
|
|
use rustc_front::hir::{ItemForeignMod, ItemImpl, ItemMod, ItemStatic, ItemDefaultImpl};
|
|
|
|
use rustc_front::hir::{ItemStruct, ItemTrait, ItemTy, ItemUse};
|
|
|
|
use rustc_front::hir::{NamedField, PathListIdent, PathListMod, Public};
|
|
|
|
use rustc_front::hir::StmtDecl;
|
|
|
|
use rustc_front::hir::UnnamedField;
|
|
|
|
use rustc_front::hir::{Variant, ViewPathGlob, ViewPathList, ViewPathSimple};
|
|
|
|
use rustc_front::hir::Visibility;
|
2015-11-17 18:56:13 -05:00
|
|
|
use rustc_front::intravisit::{self, Visitor};
|
2014-12-30 12:16:42 -06:00
|
|
|
|
|
|
|
use std::mem::replace;
|
2014-12-22 09:04:23 -08:00
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
use std::rc::Rc;
|
2014-12-30 12:16:42 -06:00
|
|
|
|
|
|
|
// Specifies how duplicates should be handled when adding a child item if
|
|
|
|
// another item exists with the same name in some namespace.
|
2015-03-30 09:38:44 -04:00
|
|
|
#[derive(Copy, Clone, PartialEq)]
|
2014-12-30 12:16:42 -06:00
|
|
|
enum DuplicateCheckingMode {
|
2015-11-17 09:10:41 +00:00
|
|
|
ForbidDuplicateTypes,
|
2014-12-30 12:16:42 -06:00
|
|
|
ForbidDuplicateValues,
|
|
|
|
ForbidDuplicateTypesAndValues,
|
2015-10-26 20:31:11 +01:00
|
|
|
OverwriteDuplicates,
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2015-10-26 20:31:11 +01:00
|
|
|
struct GraphBuilder<'a, 'b: 'a, 'tcx: 'b> {
|
|
|
|
resolver: &'a mut Resolver<'b, 'tcx>,
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2015-01-01 14:53:20 -05:00
|
|
|
impl<'a, 'b:'a, 'tcx:'b> Deref for GraphBuilder<'a, 'b, 'tcx> {
|
|
|
|
type Target = Resolver<'b, 'tcx>;
|
|
|
|
|
2014-12-30 12:16:42 -06:00
|
|
|
fn deref(&self) -> &Resolver<'b, 'tcx> {
|
|
|
|
&*self.resolver
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-01 14:53:20 -05:00
|
|
|
impl<'a, 'b:'a, 'tcx:'b> DerefMut for GraphBuilder<'a, 'b, 'tcx> {
|
2014-12-30 12:16:42 -06:00
|
|
|
fn deref_mut(&mut self) -> &mut Resolver<'b, 'tcx> {
|
|
|
|
&mut *self.resolver
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
|
|
|
|
/// Constructs the reduced graph for the entire crate.
|
2015-07-31 00:04:06 -07:00
|
|
|
fn build_reduced_graph(self, krate: &hir::Crate) {
|
2014-12-30 12:16:42 -06:00
|
|
|
let mut visitor = BuildReducedGraphVisitor {
|
2015-11-18 01:22:32 +00:00
|
|
|
parent: self.graph_root.clone(),
|
2014-12-30 12:16:42 -06:00
|
|
|
builder: self,
|
|
|
|
};
|
2015-11-17 18:56:13 -05:00
|
|
|
intravisit::walk_crate(&mut visitor, krate);
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2015-11-17 09:10:41 +00:00
|
|
|
/// Adds a new child item to the module definition of the parent node,
|
|
|
|
/// or if there is already a child, does duplicate checking on the child.
|
|
|
|
/// Returns the child's corresponding name bindings.
|
2014-12-30 12:16:42 -06:00
|
|
|
fn add_child(&self,
|
|
|
|
name: Name,
|
2014-12-30 12:52:51 -06:00
|
|
|
parent: &Rc<Module>,
|
2014-12-30 12:16:42 -06:00
|
|
|
duplicate_checking_mode: DuplicateCheckingMode,
|
|
|
|
// For printing errors
|
|
|
|
sp: Span)
|
2015-11-16 02:10:09 +00:00
|
|
|
-> NameBindings {
|
2015-10-26 20:31:11 +01:00
|
|
|
self.check_for_conflicts_between_external_crates_and_items(&**parent, name, sp);
|
2014-12-30 12:16:42 -06:00
|
|
|
|
|
|
|
// Add or reuse the child.
|
2014-12-30 12:30:42 -06:00
|
|
|
let child = parent.children.borrow().get(&name).cloned();
|
2014-12-30 12:16:42 -06:00
|
|
|
match child {
|
|
|
|
None => {
|
2015-11-16 02:10:09 +00:00
|
|
|
let child = NameBindings::new();
|
2014-12-30 12:30:42 -06:00
|
|
|
parent.children.borrow_mut().insert(name, child.clone());
|
2014-12-30 12:16:42 -06:00
|
|
|
child
|
|
|
|
}
|
|
|
|
Some(child) => {
|
|
|
|
// Enforce the duplicate checking mode:
|
|
|
|
//
|
|
|
|
// * If we're requesting duplicate type checking, check that
|
2015-11-17 09:10:41 +00:00
|
|
|
// the name isn't defined in the type namespace.
|
2014-12-30 12:16:42 -06:00
|
|
|
//
|
|
|
|
// * If we're requesting duplicate value checking, check that
|
2015-11-17 09:10:41 +00:00
|
|
|
// the name isn't defined in the value namespace.
|
2014-12-30 12:16:42 -06:00
|
|
|
//
|
2015-11-17 09:10:41 +00:00
|
|
|
// * If we're requesting duplicate type and value checking,
|
|
|
|
// check that the name isn't defined in either namespace.
|
2014-12-30 12:16:42 -06:00
|
|
|
//
|
|
|
|
// * If no duplicate checking was requested at all, do
|
|
|
|
// nothing.
|
|
|
|
|
|
|
|
let ns = match duplicate_checking_mode {
|
2015-11-17 09:10:41 +00:00
|
|
|
ForbidDuplicateTypes if child.type_ns.defined() => TypeNS,
|
|
|
|
ForbidDuplicateValues if child.value_ns.defined() => ValueNS,
|
|
|
|
ForbidDuplicateTypesAndValues if child.type_ns.defined() => TypeNS,
|
|
|
|
ForbidDuplicateTypesAndValues if child.value_ns.defined() => ValueNS,
|
|
|
|
_ => return child,
|
2014-12-30 12:16:42 -06:00
|
|
|
};
|
2015-11-17 09:10:41 +00:00
|
|
|
|
|
|
|
// Record an error here by looking up the namespace that had the duplicate
|
|
|
|
let ns_str = match ns { TypeNS => "type or module", ValueNS => "value" };
|
|
|
|
resolve_error(self, sp, ResolutionError::DuplicateDefinition(ns_str, name));
|
|
|
|
|
|
|
|
if let Some(sp) = child[ns].span() {
|
|
|
|
let note = format!("first definition of {} `{}` here", ns_str, name);
|
|
|
|
self.session.span_note(sp, ¬e);
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
child
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn block_needs_anonymous_module(&mut self, block: &Block) -> bool {
|
|
|
|
// Check each statement.
|
2015-01-31 12:20:46 -05:00
|
|
|
for statement in &block.stmts {
|
2014-12-30 12:16:42 -06:00
|
|
|
match statement.node {
|
|
|
|
StmtDecl(ref declaration, _) => {
|
|
|
|
match declaration.node {
|
|
|
|
DeclItem(_) => {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// Keep searching.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// Keep searching.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-23 21:34:36 +02:00
|
|
|
// If we found no items, we don't need to create
|
2014-12-30 12:16:42 -06:00
|
|
|
// an anonymous module.
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-30 12:52:51 -06:00
|
|
|
fn get_parent_link(&mut self, parent: &Rc<Module>, name: Name) -> ParentLink {
|
2015-08-14 16:53:24 -07:00
|
|
|
ModuleParentLink(Rc::downgrade(parent), name)
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Constructs the reduced graph for one item.
|
2014-12-30 12:52:51 -06:00
|
|
|
fn build_reduced_graph_for_item(&mut self, item: &Item, parent: &Rc<Module>) -> Rc<Module> {
|
2015-09-20 04:50:30 +03:00
|
|
|
let name = item.name;
|
2014-12-30 12:16:42 -06:00
|
|
|
let sp = item.span;
|
2015-07-31 00:04:06 -07:00
|
|
|
let is_public = item.vis == hir::Public;
|
2015-04-28 16:36:22 -07:00
|
|
|
let modifiers = if is_public {
|
|
|
|
DefModifiers::PUBLIC
|
|
|
|
} else {
|
|
|
|
DefModifiers::empty()
|
|
|
|
} | DefModifiers::IMPORTABLE;
|
2014-12-30 12:16:42 -06:00
|
|
|
|
|
|
|
match item.node {
|
2014-12-23 21:34:36 +02:00
|
|
|
ItemUse(ref view_path) => {
|
|
|
|
// Extract and intern the module part of the path. For
|
|
|
|
// globs and lists, the path is found directly in the AST;
|
|
|
|
// for simple paths we have to munge the path a little.
|
|
|
|
let module_path = match view_path.node {
|
|
|
|
ViewPathSimple(_, ref full_path) => {
|
|
|
|
full_path.segments
|
2015-10-26 20:31:11 +01:00
|
|
|
.split_last()
|
|
|
|
.unwrap()
|
|
|
|
.1
|
|
|
|
.iter()
|
|
|
|
.map(|seg| seg.identifier.name)
|
|
|
|
.collect()
|
2014-12-23 21:34:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ViewPathGlob(ref module_ident_path) |
|
|
|
|
ViewPathList(ref module_ident_path, _) => {
|
|
|
|
module_ident_path.segments
|
2015-10-26 20:31:11 +01:00
|
|
|
.iter()
|
|
|
|
.map(|seg| seg.identifier.name)
|
|
|
|
.collect()
|
2014-12-23 21:34:36 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Build up the import directives.
|
|
|
|
let shadowable = item.attrs.iter().any(|attr| {
|
2015-07-28 18:07:20 +02:00
|
|
|
attr.name() == special_idents::prelude_import.name.as_str()
|
2014-12-23 21:34:36 +02:00
|
|
|
});
|
|
|
|
let shadowable = if shadowable {
|
|
|
|
Shadowable::Always
|
|
|
|
} else {
|
|
|
|
Shadowable::Never
|
|
|
|
};
|
|
|
|
|
|
|
|
match view_path.node {
|
|
|
|
ViewPathSimple(binding, ref full_path) => {
|
2015-10-26 20:31:11 +01:00
|
|
|
let source_name = full_path.segments.last().unwrap().identifier.name;
|
2015-07-28 18:07:20 +02:00
|
|
|
if source_name.as_str() == "mod" || source_name.as_str() == "self" {
|
2015-07-14 19:42:38 +02:00
|
|
|
resolve_error(self,
|
2015-07-28 18:07:20 +02:00
|
|
|
view_path.span,
|
|
|
|
ResolutionError::SelfImportsOnlyAllowedWithin);
|
2014-12-23 21:34:36 +02:00
|
|
|
}
|
|
|
|
|
2015-09-20 14:51:40 +03:00
|
|
|
let subclass = SingleImport(binding, source_name);
|
2014-12-23 21:34:36 +02:00
|
|
|
self.build_import_directive(&**parent,
|
|
|
|
module_path,
|
|
|
|
subclass,
|
|
|
|
view_path.span,
|
|
|
|
item.id,
|
|
|
|
is_public,
|
|
|
|
shadowable);
|
|
|
|
}
|
|
|
|
ViewPathList(_, ref source_items) => {
|
|
|
|
// Make sure there's at most one `mod` import in the list.
|
2015-10-26 20:31:11 +01:00
|
|
|
let mod_spans = source_items.iter()
|
|
|
|
.filter_map(|item| {
|
|
|
|
match item.node {
|
|
|
|
PathListMod { .. } => Some(item.span),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<Span>>();
|
2014-12-23 21:34:36 +02:00
|
|
|
if mod_spans.len() > 1 {
|
2015-10-26 20:31:11 +01:00
|
|
|
resolve_error(self,
|
|
|
|
mod_spans[0],
|
|
|
|
ResolutionError::SelfImportCanOnlyAppearOnceInTheList);
|
2014-12-23 21:34:36 +02:00
|
|
|
for other_span in mod_spans.iter().skip(1) {
|
2015-10-26 20:31:11 +01:00
|
|
|
self.session
|
|
|
|
.span_note(*other_span, "another `self` import appears here");
|
2014-12-23 21:34:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-31 12:20:46 -05:00
|
|
|
for source_item in source_items {
|
2015-07-31 22:20:25 -07:00
|
|
|
let (module_path, name, rename) = match source_item.node {
|
|
|
|
PathListIdent { name, rename, .. } =>
|
2015-09-20 14:51:40 +03:00
|
|
|
(module_path.clone(), name, rename.unwrap_or(name)),
|
2015-07-31 22:20:25 -07:00
|
|
|
PathListMod { rename, .. } => {
|
2014-12-23 21:34:36 +02:00
|
|
|
let name = match module_path.last() {
|
|
|
|
Some(name) => *name,
|
|
|
|
None => {
|
2015-07-14 19:42:38 +02:00
|
|
|
resolve_error(
|
2015-07-14 16:32:43 +02:00
|
|
|
self,
|
|
|
|
source_item.span,
|
2015-07-14 19:42:38 +02:00
|
|
|
ResolutionError::
|
2015-07-14 16:32:43 +02:00
|
|
|
SelfImportOnlyInImportListWithNonEmptyPrefix
|
2015-07-14 15:37:52 +02:00
|
|
|
);
|
2014-12-23 21:34:36 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
2015-07-11 14:34:57 +03:00
|
|
|
let module_path = module_path.split_last().unwrap().1;
|
2015-09-20 14:51:40 +03:00
|
|
|
let rename = rename.unwrap_or(name);
|
2015-07-31 22:20:25 -07:00
|
|
|
(module_path.to_vec(), name, rename)
|
2014-12-23 21:34:36 +02:00
|
|
|
}
|
|
|
|
};
|
2015-10-26 20:31:11 +01:00
|
|
|
self.build_import_directive(&**parent,
|
|
|
|
module_path,
|
|
|
|
SingleImport(rename, name),
|
|
|
|
source_item.span,
|
|
|
|
source_item.node.id(),
|
|
|
|
is_public,
|
|
|
|
shadowable);
|
2014-12-23 21:34:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ViewPathGlob(_) => {
|
|
|
|
self.build_import_directive(&**parent,
|
|
|
|
module_path,
|
|
|
|
GlobImport,
|
|
|
|
view_path.span,
|
|
|
|
item.id,
|
|
|
|
is_public,
|
|
|
|
shadowable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parent.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
ItemExternCrate(_) => {
|
2015-10-26 20:31:11 +01:00
|
|
|
// n.b. we don't need to look at the path option here, because cstore already
|
|
|
|
// did
|
2015-11-21 21:39:05 +02:00
|
|
|
if let Some(crate_id) = self.session.cstore.extern_mod_stmt_cnum(item.id) {
|
2015-10-26 20:31:11 +01:00
|
|
|
let def_id = DefId {
|
|
|
|
krate: crate_id,
|
|
|
|
index: CRATE_DEF_INDEX,
|
|
|
|
};
|
2014-12-23 21:34:36 +02:00
|
|
|
self.external_exports.insert(def_id);
|
2015-08-14 16:53:24 -07:00
|
|
|
let parent_link = ModuleParentLink(Rc::downgrade(parent), name);
|
2015-11-18 01:22:32 +00:00
|
|
|
let def = DefMod(def_id);
|
|
|
|
let external_module = Module::new(parent_link, Some(def), false, true);
|
|
|
|
|
2014-12-23 21:34:36 +02:00
|
|
|
debug!("(build reduced graph for item) found extern `{}`",
|
2015-10-26 20:31:11 +01:00
|
|
|
module_to_string(&*external_module));
|
2014-12-23 21:34:36 +02:00
|
|
|
self.check_for_conflicts_between_external_crates(&**parent, name, sp);
|
2015-10-26 20:31:11 +01:00
|
|
|
parent.external_module_children
|
|
|
|
.borrow_mut()
|
2014-12-23 21:34:36 +02:00
|
|
|
.insert(name, external_module.clone());
|
|
|
|
self.build_reduced_graph_for_external_crate(&external_module);
|
|
|
|
}
|
|
|
|
parent.clone()
|
|
|
|
}
|
|
|
|
|
2014-12-30 12:16:42 -06:00
|
|
|
ItemMod(..) => {
|
2015-11-17 09:10:41 +00:00
|
|
|
let name_bindings = self.add_child(name, parent, ForbidDuplicateTypes, sp);
|
2014-12-30 12:16:42 -06:00
|
|
|
|
|
|
|
let parent_link = self.get_parent_link(parent, name);
|
2015-11-16 07:59:50 +00:00
|
|
|
let def = DefMod(self.ast_map.local_def_id(item.id));
|
2015-11-18 01:22:32 +00:00
|
|
|
let module = Module::new(parent_link, Some(def), false, is_public);
|
|
|
|
name_bindings.define_module(module.clone(), sp);
|
|
|
|
module
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2014-12-30 12:52:51 -06:00
|
|
|
ItemForeignMod(..) => parent.clone(),
|
2014-12-30 12:16:42 -06:00
|
|
|
|
|
|
|
// These items live in the value namespace.
|
|
|
|
ItemStatic(_, m, _) => {
|
2014-12-30 12:52:51 -06:00
|
|
|
let name_bindings = self.add_child(name, parent, ForbidDuplicateValues, sp);
|
2015-07-31 00:04:06 -07:00
|
|
|
let mutbl = m == hir::MutMutable;
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2015-09-02 16:11:32 -04:00
|
|
|
name_bindings.define_value(DefStatic(self.ast_map.local_def_id(item.id), mutbl),
|
|
|
|
sp,
|
|
|
|
modifiers);
|
2014-12-30 12:52:51 -06:00
|
|
|
parent.clone()
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
ItemConst(_, _) => {
|
2014-12-30 12:52:51 -06:00
|
|
|
self.add_child(name, parent, ForbidDuplicateValues, sp)
|
2015-09-02 16:11:32 -04:00
|
|
|
.define_value(DefConst(self.ast_map.local_def_id(item.id)), sp, modifiers);
|
2014-12-30 12:52:51 -06:00
|
|
|
parent.clone()
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2015-02-25 22:05:07 +02:00
|
|
|
ItemFn(_, _, _, _, _, _) => {
|
2014-12-30 12:52:51 -06:00
|
|
|
let name_bindings = self.add_child(name, parent, ForbidDuplicateValues, sp);
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2015-09-02 16:11:32 -04:00
|
|
|
let def = DefFn(self.ast_map.local_def_id(item.id), false);
|
2014-12-30 12:16:42 -06:00
|
|
|
name_bindings.define_value(def, sp, modifiers);
|
2014-12-30 12:52:51 -06:00
|
|
|
parent.clone()
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// These items live in the type namespace.
|
|
|
|
ItemTy(..) => {
|
2015-10-26 20:31:11 +01:00
|
|
|
let name_bindings = self.add_child(name,
|
|
|
|
parent,
|
2015-11-17 09:10:41 +00:00
|
|
|
ForbidDuplicateTypes,
|
2015-10-26 20:31:11 +01:00
|
|
|
sp);
|
2014-12-30 12:52:51 -06:00
|
|
|
|
2015-01-16 14:25:45 -08:00
|
|
|
let parent_link = self.get_parent_link(parent, name);
|
2015-11-16 07:59:50 +00:00
|
|
|
let def = DefTy(self.ast_map.local_def_id(item.id), false);
|
2015-11-18 01:22:32 +00:00
|
|
|
let module = Module::new(parent_link, Some(def), false, is_public);
|
|
|
|
name_bindings.define_module(module, sp);
|
2014-12-30 12:52:51 -06:00
|
|
|
parent.clone()
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
ItemEnum(ref enum_definition, _) => {
|
2015-10-26 20:31:11 +01:00
|
|
|
let name_bindings = self.add_child(name,
|
|
|
|
parent,
|
2015-11-17 09:10:41 +00:00
|
|
|
ForbidDuplicateTypes,
|
2015-10-26 20:31:11 +01:00
|
|
|
sp);
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2014-12-30 12:52:51 -06:00
|
|
|
let parent_link = self.get_parent_link(parent, name);
|
2015-11-16 07:59:50 +00:00
|
|
|
let def = DefTy(self.ast_map.local_def_id(item.id), true);
|
2015-11-18 01:22:32 +00:00
|
|
|
let module = Module::new(parent_link, Some(def), false, is_public);
|
|
|
|
name_bindings.define_module(module.clone(), sp);
|
2014-12-30 12:52:51 -06:00
|
|
|
|
2015-01-31 12:20:46 -05:00
|
|
|
for variant in &(*enum_definition).variants {
|
2015-09-02 16:11:32 -04:00
|
|
|
let item_def_id = self.ast_map.local_def_id(item.id);
|
2015-10-26 20:31:11 +01:00
|
|
|
self.build_reduced_graph_for_variant(&**variant, item_def_id, &module);
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2014-12-30 12:52:51 -06:00
|
|
|
parent.clone()
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// These items live in both the type and value namespaces.
|
|
|
|
ItemStruct(ref struct_def, _) => {
|
|
|
|
// Adding to both Type and Value namespaces or just Type?
|
2015-10-08 23:45:46 +03:00
|
|
|
let (forbid, ctor_id) = if struct_def.is_struct() {
|
2015-11-17 09:10:41 +00:00
|
|
|
(ForbidDuplicateTypes, None)
|
2015-10-08 23:45:46 +03:00
|
|
|
} else {
|
2015-10-10 03:28:40 +03:00
|
|
|
(ForbidDuplicateTypesAndValues, Some(struct_def.id()))
|
2014-12-30 12:16:42 -06:00
|
|
|
};
|
|
|
|
|
2014-12-30 12:52:51 -06:00
|
|
|
let name_bindings = self.add_child(name, parent, forbid, sp);
|
2014-12-30 12:16:42 -06:00
|
|
|
|
|
|
|
// Define a name in the type namespace.
|
2015-09-02 16:11:32 -04:00
|
|
|
name_bindings.define_type(DefTy(self.ast_map.local_def_id(item.id), false),
|
|
|
|
sp,
|
|
|
|
modifiers);
|
2014-12-30 12:16:42 -06:00
|
|
|
|
|
|
|
// If this is a newtype or unit-like struct, define a name
|
|
|
|
// in the value namespace as well
|
2014-12-30 12:52:51 -06:00
|
|
|
if let Some(cid) = ctor_id {
|
2015-09-02 16:11:32 -04:00
|
|
|
name_bindings.define_value(DefStruct(self.ast_map.local_def_id(cid)),
|
|
|
|
sp,
|
|
|
|
modifiers);
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Record the def ID and fields of this struct.
|
2015-10-26 20:31:11 +01:00
|
|
|
let named_fields = struct_def.fields()
|
|
|
|
.iter()
|
|
|
|
.filter_map(|f| {
|
|
|
|
match f.node.kind {
|
|
|
|
NamedField(name, _) => Some(name),
|
|
|
|
UnnamedField(_) => None,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
2015-09-02 16:11:32 -04:00
|
|
|
let item_def_id = self.ast_map.local_def_id(item.id);
|
|
|
|
self.structs.insert(item_def_id, named_fields);
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2014-12-30 12:52:51 -06:00
|
|
|
parent.clone()
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2015-02-07 14:24:34 +01:00
|
|
|
ItemDefaultImpl(_, _) |
|
2015-02-24 08:28:11 +02:00
|
|
|
ItemImpl(..) => parent.clone(),
|
2014-12-30 12:16:42 -06:00
|
|
|
|
|
|
|
ItemTrait(_, _, _, ref items) => {
|
2015-10-26 20:31:11 +01:00
|
|
|
let name_bindings = self.add_child(name,
|
|
|
|
parent,
|
2015-11-17 09:10:41 +00:00
|
|
|
ForbidDuplicateTypes,
|
2015-10-26 20:31:11 +01:00
|
|
|
sp);
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2015-11-16 07:59:50 +00:00
|
|
|
let def_id = self.ast_map.local_def_id(item.id);
|
|
|
|
|
2014-12-30 12:16:42 -06:00
|
|
|
// Add all the items within to a new module.
|
2014-12-30 12:52:51 -06:00
|
|
|
let parent_link = self.get_parent_link(parent, name);
|
2015-11-16 07:59:50 +00:00
|
|
|
let def = DefTrait(def_id);
|
2015-11-18 01:22:32 +00:00
|
|
|
let module_parent = Module::new(parent_link, Some(def), false, is_public);
|
|
|
|
name_bindings.define_module(module_parent.clone(), sp);
|
2014-12-30 12:16:42 -06:00
|
|
|
|
|
|
|
// Add the names of all the items to the trait info.
|
2015-01-31 12:20:46 -05:00
|
|
|
for trait_item in items {
|
2015-09-20 04:50:30 +03:00
|
|
|
let name_bindings = self.add_child(trait_item.name,
|
2015-10-26 20:31:11 +01:00
|
|
|
&module_parent,
|
|
|
|
ForbidDuplicateTypesAndValues,
|
|
|
|
trait_item.span);
|
2015-03-10 12:28:44 +02:00
|
|
|
|
|
|
|
match trait_item.node {
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::ConstTraitItem(..) => {
|
2015-09-02 16:11:32 -04:00
|
|
|
let def = DefAssociatedConst(self.ast_map.local_def_id(trait_item.id));
|
2015-04-28 16:36:22 -07:00
|
|
|
// NB: not DefModifiers::IMPORTABLE
|
|
|
|
name_bindings.define_value(def, trait_item.span, DefModifiers::PUBLIC);
|
2015-03-14 12:05:00 -06:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::MethodTraitItem(..) => {
|
2015-09-02 16:11:32 -04:00
|
|
|
let def = DefMethod(self.ast_map.local_def_id(trait_item.id));
|
2015-04-28 16:36:22 -07:00
|
|
|
// NB: not DefModifiers::IMPORTABLE
|
|
|
|
name_bindings.define_value(def, trait_item.span, DefModifiers::PUBLIC);
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2015-07-31 00:04:06 -07:00
|
|
|
hir::TypeTraitItem(..) => {
|
2015-09-02 16:11:32 -04:00
|
|
|
let def = DefAssociatedTy(self.ast_map.local_def_id(item.id),
|
|
|
|
self.ast_map.local_def_id(trait_item.id));
|
2015-04-28 16:36:22 -07:00
|
|
|
// NB: not DefModifiers::IMPORTABLE
|
|
|
|
name_bindings.define_type(def, trait_item.span, DefModifiers::PUBLIC);
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2015-03-10 12:28:44 +02:00
|
|
|
}
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2015-09-02 16:11:32 -04:00
|
|
|
let trait_item_def_id = self.ast_map.local_def_id(trait_item.id);
|
|
|
|
self.trait_item_map.insert((trait_item.name, def_id), trait_item_def_id);
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2014-12-30 12:52:51 -06:00
|
|
|
parent.clone()
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constructs the reduced graph for one variant. Variants exist in the
|
|
|
|
// type and value namespaces.
|
|
|
|
fn build_reduced_graph_for_variant(&mut self,
|
|
|
|
variant: &Variant,
|
|
|
|
item_id: DefId,
|
2014-12-30 12:52:51 -06:00
|
|
|
parent: &Rc<Module>) {
|
2015-09-20 16:47:24 +03:00
|
|
|
let name = variant.node.name;
|
2015-10-08 23:45:46 +03:00
|
|
|
let is_exported = if variant.node.data.is_struct() {
|
|
|
|
// Not adding fields for variants as they are not accessed with a self receiver
|
2015-10-10 03:28:40 +03:00
|
|
|
let variant_def_id = self.ast_map.local_def_id(variant.node.data.id());
|
2015-10-08 23:45:46 +03:00
|
|
|
self.structs.insert(variant_def_id, Vec::new());
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
2014-12-30 12:16:42 -06:00
|
|
|
};
|
|
|
|
|
2015-10-26 20:31:11 +01:00
|
|
|
let child = self.add_child(name, parent, ForbidDuplicateTypesAndValues, variant.span);
|
2014-12-30 12:16:42 -06:00
|
|
|
// variants are always treated as importable to allow them to be glob
|
|
|
|
// used
|
|
|
|
child.define_value(DefVariant(item_id,
|
2015-10-10 03:28:40 +03:00
|
|
|
self.ast_map.local_def_id(variant.node.data.id()),
|
|
|
|
is_exported),
|
2015-10-26 20:31:11 +01:00
|
|
|
variant.span,
|
|
|
|
DefModifiers::PUBLIC | DefModifiers::IMPORTABLE);
|
2014-12-30 12:16:42 -06:00
|
|
|
child.define_type(DefVariant(item_id,
|
2015-10-10 03:28:40 +03:00
|
|
|
self.ast_map.local_def_id(variant.node.data.id()),
|
|
|
|
is_exported),
|
2015-10-26 20:31:11 +01:00
|
|
|
variant.span,
|
|
|
|
DefModifiers::PUBLIC | DefModifiers::IMPORTABLE);
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Constructs the reduced graph for one foreign item.
|
2015-02-05 09:19:07 +02:00
|
|
|
fn build_reduced_graph_for_foreign_item(&mut self,
|
|
|
|
foreign_item: &ForeignItem,
|
|
|
|
parent: &Rc<Module>) {
|
2015-09-20 04:50:30 +03:00
|
|
|
let name = foreign_item.name;
|
2015-07-31 00:04:06 -07:00
|
|
|
let is_public = foreign_item.vis == hir::Public;
|
2015-04-28 16:36:22 -07:00
|
|
|
let modifiers = if is_public {
|
|
|
|
DefModifiers::PUBLIC
|
|
|
|
} else {
|
|
|
|
DefModifiers::empty()
|
|
|
|
} | DefModifiers::IMPORTABLE;
|
2015-10-26 20:31:11 +01:00
|
|
|
let name_bindings = self.add_child(name, parent, ForbidDuplicateValues, foreign_item.span);
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2015-02-05 09:19:07 +02:00
|
|
|
let def = match foreign_item.node {
|
|
|
|
ForeignItemFn(..) => {
|
2015-09-02 16:11:32 -04:00
|
|
|
DefFn(self.ast_map.local_def_id(foreign_item.id), false)
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
ForeignItemStatic(_, m) => {
|
2015-09-02 16:11:32 -04:00
|
|
|
DefStatic(self.ast_map.local_def_id(foreign_item.id), m)
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2015-02-05 09:19:07 +02:00
|
|
|
};
|
|
|
|
name_bindings.define_value(def, foreign_item.span, modifiers);
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2014-12-30 12:52:51 -06:00
|
|
|
fn build_reduced_graph_for_block(&mut self, block: &Block, parent: &Rc<Module>) -> Rc<Module> {
|
2014-12-30 12:16:42 -06:00
|
|
|
if self.block_needs_anonymous_module(block) {
|
|
|
|
let block_id = block.id;
|
|
|
|
|
2015-10-26 20:31:11 +01:00
|
|
|
debug!("(building reduced graph for block) creating a new anonymous module for block \
|
|
|
|
{}",
|
2014-12-30 12:16:42 -06:00
|
|
|
block_id);
|
|
|
|
|
2015-11-18 01:22:32 +00:00
|
|
|
let parent_link = BlockParentLink(Rc::downgrade(parent), block_id);
|
|
|
|
let new_module = Module::new(parent_link, None, false, false);
|
2014-12-30 12:30:42 -06:00
|
|
|
parent.anonymous_children.borrow_mut().insert(block_id, new_module.clone());
|
|
|
|
new_module
|
2014-12-30 12:16:42 -06:00
|
|
|
} else {
|
2014-12-30 12:52:51 -06:00
|
|
|
parent.clone()
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_external_def(&mut self,
|
|
|
|
def: Def,
|
|
|
|
vis: Visibility,
|
|
|
|
child_name_bindings: &NameBindings,
|
|
|
|
final_ident: &str,
|
|
|
|
name: Name,
|
2014-12-30 12:52:51 -06:00
|
|
|
new_parent: &Rc<Module>) {
|
2015-10-26 20:31:11 +01:00
|
|
|
debug!("(building reduced graph for external crate) building external def {}, priv {:?}",
|
|
|
|
final_ident,
|
|
|
|
vis);
|
2015-07-31 00:04:06 -07:00
|
|
|
let is_public = vis == hir::Public;
|
2015-04-28 16:36:22 -07:00
|
|
|
let modifiers = if is_public {
|
|
|
|
DefModifiers::PUBLIC
|
|
|
|
} else {
|
|
|
|
DefModifiers::empty()
|
|
|
|
} | DefModifiers::IMPORTABLE;
|
2015-10-26 20:31:11 +01:00
|
|
|
let is_exported = is_public &&
|
2015-11-16 07:59:50 +00:00
|
|
|
match new_parent.def_id() {
|
2014-12-30 12:30:42 -06:00
|
|
|
None => true,
|
2015-10-26 20:31:11 +01:00
|
|
|
Some(did) => self.external_exports.contains(&did),
|
2014-12-30 12:16:42 -06:00
|
|
|
};
|
|
|
|
if is_exported {
|
|
|
|
self.external_exports.insert(def.def_id());
|
|
|
|
}
|
|
|
|
|
|
|
|
match def {
|
2015-11-16 07:59:50 +00:00
|
|
|
DefMod(_) |
|
|
|
|
DefForeignMod(_) |
|
|
|
|
DefStruct(_) |
|
|
|
|
DefTy(..) => {
|
2015-11-16 02:10:09 +00:00
|
|
|
if let Some(module_def) = child_name_bindings.type_ns.module() {
|
|
|
|
debug!("(building reduced graph for external crate) already created module");
|
2015-11-16 07:59:50 +00:00
|
|
|
module_def.def.set(Some(def));
|
2015-11-16 02:10:09 +00:00
|
|
|
} else {
|
|
|
|
debug!("(building reduced graph for external crate) building module {} {}",
|
|
|
|
final_ident,
|
|
|
|
is_public);
|
|
|
|
let parent_link = self.get_parent_link(new_parent, name);
|
2015-11-18 01:22:32 +00:00
|
|
|
let module = Module::new(parent_link, Some(def), true, is_public);
|
|
|
|
child_name_bindings.define_module(module, DUMMY_SP);
|
2015-10-26 20:31:11 +01:00
|
|
|
}
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2015-10-26 20:31:11 +01:00
|
|
|
_ => {}
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
match def {
|
2015-10-26 20:31:11 +01:00
|
|
|
DefMod(_) | DefForeignMod(_) => {}
|
|
|
|
DefVariant(_, variant_id, is_struct) => {
|
|
|
|
debug!("(building reduced graph for external crate) building variant {}",
|
|
|
|
final_ident);
|
|
|
|
// variants are always treated as importable to allow them to be
|
|
|
|
// glob used
|
|
|
|
let modifiers = DefModifiers::PUBLIC | DefModifiers::IMPORTABLE;
|
|
|
|
if is_struct {
|
|
|
|
child_name_bindings.define_type(def, DUMMY_SP, modifiers);
|
|
|
|
// Not adding fields for variants as they are not accessed with a self receiver
|
|
|
|
self.structs.insert(variant_id, Vec::new());
|
|
|
|
} else {
|
|
|
|
child_name_bindings.define_value(def, DUMMY_SP, modifiers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DefFn(ctor_id, true) => {
|
|
|
|
child_name_bindings.define_value(
|
2015-11-20 17:46:39 +02:00
|
|
|
self.session.cstore.tuple_struct_definition_if_ctor(ctor_id)
|
2014-12-30 12:16:42 -06:00
|
|
|
.map_or(def, |_| DefStruct(ctor_id)), DUMMY_SP, modifiers);
|
|
|
|
}
|
2015-10-26 20:31:11 +01:00
|
|
|
DefFn(..) |
|
|
|
|
DefStatic(..) |
|
|
|
|
DefConst(..) |
|
|
|
|
DefAssociatedConst(..) |
|
|
|
|
DefMethod(..) => {
|
|
|
|
debug!("(building reduced graph for external crate) building value (fn/static) {}",
|
|
|
|
final_ident);
|
|
|
|
// impl methods have already been defined with the correct importability
|
|
|
|
// modifier
|
2015-11-16 02:10:09 +00:00
|
|
|
let mut modifiers = match *child_name_bindings.value_ns.borrow() {
|
2015-10-26 20:31:11 +01:00
|
|
|
Some(ref def) => (modifiers & !DefModifiers::IMPORTABLE) |
|
|
|
|
(def.modifiers & DefModifiers::IMPORTABLE),
|
|
|
|
None => modifiers,
|
|
|
|
};
|
2015-11-16 07:59:50 +00:00
|
|
|
if !new_parent.is_normal() {
|
2015-10-26 20:31:11 +01:00
|
|
|
modifiers = modifiers & !DefModifiers::IMPORTABLE;
|
|
|
|
}
|
2014-12-30 12:16:42 -06:00
|
|
|
child_name_bindings.define_value(def, DUMMY_SP, modifiers);
|
|
|
|
}
|
2015-10-26 20:31:11 +01:00
|
|
|
DefTrait(def_id) => {
|
|
|
|
debug!("(building reduced graph for external crate) building type {}",
|
|
|
|
final_ident);
|
|
|
|
|
|
|
|
// If this is a trait, add all the trait item names to the trait
|
|
|
|
// info.
|
|
|
|
|
2015-11-20 17:46:39 +02:00
|
|
|
let trait_item_def_ids = self.session.cstore.trait_item_def_ids(def_id);
|
2015-10-26 20:31:11 +01:00
|
|
|
for trait_item_def in &trait_item_def_ids {
|
2015-11-20 17:46:39 +02:00
|
|
|
let trait_item_name =
|
|
|
|
self.session.cstore.item_name(trait_item_def.def_id());
|
2015-10-26 20:31:11 +01:00
|
|
|
|
|
|
|
debug!("(building reduced graph for external crate) ... adding trait item \
|
|
|
|
'{}'",
|
|
|
|
trait_item_name);
|
|
|
|
|
|
|
|
self.trait_item_map.insert((trait_item_name, def_id), trait_item_def.def_id());
|
|
|
|
|
|
|
|
if is_exported {
|
|
|
|
self.external_exports.insert(trait_item_def.def_id());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Define a module if necessary.
|
|
|
|
let parent_link = self.get_parent_link(new_parent, name);
|
2015-11-18 01:22:32 +00:00
|
|
|
let module = Module::new(parent_link, Some(def), true, is_public);
|
|
|
|
child_name_bindings.define_module(module, DUMMY_SP);
|
2015-10-26 20:31:11 +01:00
|
|
|
}
|
|
|
|
DefTy(..) | DefAssociatedTy(..) => {
|
|
|
|
debug!("(building reduced graph for external crate) building type {}",
|
|
|
|
final_ident);
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2015-11-16 07:59:50 +00:00
|
|
|
let modifiers = match new_parent.is_normal() {
|
|
|
|
true => modifiers,
|
2015-10-26 20:31:11 +01:00
|
|
|
_ => modifiers & !DefModifiers::IMPORTABLE,
|
|
|
|
};
|
|
|
|
|
2015-11-16 07:59:50 +00:00
|
|
|
if let DefTy(..) = def {
|
|
|
|
child_name_bindings.type_ns.set_modifiers(modifiers);
|
|
|
|
} else {
|
|
|
|
child_name_bindings.define_type(def, DUMMY_SP, modifiers);
|
|
|
|
}
|
2015-10-26 20:31:11 +01:00
|
|
|
}
|
|
|
|
DefStruct(def_id) => {
|
|
|
|
debug!("(building reduced graph for external crate) building type and value for \
|
|
|
|
{}",
|
|
|
|
final_ident);
|
2015-11-20 17:46:39 +02:00
|
|
|
child_name_bindings.define_type(def, DUMMY_SP, modifiers);
|
|
|
|
let fields = self.session.cstore.struct_field_names(def_id);
|
2015-10-26 20:31:11 +01:00
|
|
|
|
|
|
|
if fields.is_empty() {
|
|
|
|
child_name_bindings.define_value(def, DUMMY_SP, modifiers);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record the def ID and fields of this struct.
|
|
|
|
self.structs.insert(def_id, fields);
|
|
|
|
}
|
|
|
|
DefLocal(..) |
|
|
|
|
DefPrimTy(..) |
|
|
|
|
DefTyParam(..) |
|
|
|
|
DefUse(..) |
|
|
|
|
DefUpvar(..) |
|
|
|
|
DefLabel(..) |
|
|
|
|
DefSelfTy(..) => {
|
|
|
|
panic!("didn't expect `{:?}`", def);
|
|
|
|
}
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds the reduced graph for a single item in an external crate.
|
|
|
|
fn build_reduced_graph_for_external_crate_def(&mut self,
|
2014-12-30 12:52:51 -06:00
|
|
|
root: &Rc<Module>,
|
2015-11-20 17:46:39 +02:00
|
|
|
xcdef: ChildItem) {
|
|
|
|
match xcdef.def {
|
2014-12-30 12:16:42 -06:00
|
|
|
DlDef(def) => {
|
|
|
|
// Add the new child item, if necessary.
|
|
|
|
match def {
|
|
|
|
DefForeignMod(def_id) => {
|
|
|
|
// Foreign modules have no names. Recur and populate
|
|
|
|
// eagerly.
|
2015-11-20 17:46:39 +02:00
|
|
|
for child in self.session.cstore.item_children(def_id) {
|
|
|
|
self.build_reduced_graph_for_external_crate_def(root, child)
|
|
|
|
}
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
_ => {
|
2015-11-20 17:46:39 +02:00
|
|
|
let child_name_bindings = self.add_child(xcdef.name,
|
2015-10-26 20:31:11 +01:00
|
|
|
root,
|
|
|
|
OverwriteDuplicates,
|
|
|
|
DUMMY_SP);
|
2014-12-30 12:16:42 -06:00
|
|
|
|
|
|
|
self.handle_external_def(def,
|
2015-11-20 17:46:39 +02:00
|
|
|
xcdef.vis,
|
2015-11-16 02:10:09 +00:00
|
|
|
&child_name_bindings,
|
2015-11-20 17:46:39 +02:00
|
|
|
&xcdef.name.as_str(),
|
|
|
|
xcdef.name,
|
2014-12-30 12:30:42 -06:00
|
|
|
root);
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-20 08:17:05 +02:00
|
|
|
DlImpl(_) => {
|
2015-10-26 20:31:11 +01:00
|
|
|
debug!("(building reduced graph for external crate) ignoring impl");
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
DlField => {
|
2015-10-26 20:31:11 +01:00
|
|
|
debug!("(building reduced graph for external crate) ignoring field");
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds the reduced graph rooted at the given external module.
|
2014-12-30 12:52:51 -06:00
|
|
|
fn populate_external_module(&mut self, module: &Rc<Module>) {
|
2014-12-30 12:16:42 -06:00
|
|
|
debug!("(populating external module) attempting to populate {}",
|
2015-03-16 10:44:19 +13:00
|
|
|
module_to_string(&**module));
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2015-11-16 07:59:50 +00:00
|
|
|
let def_id = match module.def_id() {
|
2014-12-30 12:16:42 -06:00
|
|
|
None => {
|
|
|
|
debug!("(populating external module) ... no def ID!");
|
2015-10-26 20:31:11 +01:00
|
|
|
return;
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
Some(def_id) => def_id,
|
|
|
|
};
|
|
|
|
|
2015-11-20 17:46:39 +02:00
|
|
|
for child in self.session.cstore.item_children(def_id) {
|
|
|
|
debug!("(populating external module) ... found ident: {}",
|
|
|
|
child.name);
|
|
|
|
self.build_reduced_graph_for_external_crate_def(module, child);
|
|
|
|
}
|
2014-12-30 12:16:42 -06:00
|
|
|
module.populated.set(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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: &Rc<Module>) {
|
|
|
|
if !module.populated.get() {
|
2014-12-30 12:52:51 -06:00
|
|
|
self.populate_external_module(module)
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
assert!(module.populated.get())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds the reduced graph rooted at the 'use' directive for an external
|
|
|
|
/// crate.
|
2014-12-30 12:52:51 -06:00
|
|
|
fn build_reduced_graph_for_external_crate(&mut self, root: &Rc<Module>) {
|
2015-11-20 17:46:39 +02:00
|
|
|
let root_cnum = root.def_id().unwrap().krate;
|
|
|
|
for child in self.session.cstore.crate_top_level_items(root_cnum) {
|
|
|
|
self.build_reduced_graph_for_external_crate_def(root, child);
|
|
|
|
}
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates and adds an import directive to the given module.
|
|
|
|
fn build_import_directive(&mut self,
|
|
|
|
module_: &Module,
|
|
|
|
module_path: Vec<Name>,
|
|
|
|
subclass: ImportDirectiveSubclass,
|
|
|
|
span: Span,
|
|
|
|
id: NodeId,
|
|
|
|
is_public: bool,
|
|
|
|
shadowable: Shadowable) {
|
2015-10-26 20:31:11 +01:00
|
|
|
module_.imports
|
|
|
|
.borrow_mut()
|
|
|
|
.push(ImportDirective::new(module_path, subclass, span, id, is_public, shadowable));
|
2014-12-30 12:16:42 -06:00
|
|
|
self.unresolved_imports += 1;
|
2015-08-05 21:47:01 +02:00
|
|
|
|
|
|
|
if is_public {
|
2015-08-06 12:47:10 +02:00
|
|
|
module_.inc_pub_count();
|
2015-08-05 21:47:01 +02:00
|
|
|
}
|
|
|
|
|
2014-12-30 12:16:42 -06:00
|
|
|
// Bump the reference count on the name. Or, if this is a glob, set
|
|
|
|
// the appropriate flag.
|
|
|
|
|
|
|
|
match subclass {
|
|
|
|
SingleImport(target, _) => {
|
2015-03-15 18:46:44 +13:00
|
|
|
debug!("(building import directive) building import directive: {}::{}",
|
2015-03-16 10:44:19 +13:00
|
|
|
names_to_string(&module_.imports.borrow().last().unwrap().module_path),
|
2015-07-28 18:07:20 +02:00
|
|
|
target);
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2015-03-15 18:46:44 +13:00
|
|
|
let mut import_resolutions = module_.import_resolutions.borrow_mut();
|
2014-12-30 12:16:42 -06:00
|
|
|
match import_resolutions.get_mut(&target) {
|
|
|
|
Some(resolution) => {
|
2015-03-15 18:46:44 +13:00
|
|
|
debug!("(building import directive) bumping reference");
|
2014-12-30 12:16:42 -06:00
|
|
|
resolution.outstanding_references += 1;
|
|
|
|
|
|
|
|
// the source of this name is different now
|
|
|
|
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, resolution);
|
|
|
|
}
|
|
|
|
GlobImport => {
|
|
|
|
// Set the glob flag. This tells us that we don't know the
|
|
|
|
// module's exports ahead of time.
|
|
|
|
|
2015-08-06 12:47:10 +02:00
|
|
|
module_.inc_glob_count();
|
2015-08-05 21:47:01 +02:00
|
|
|
if is_public {
|
2015-08-06 12:47:10 +02:00
|
|
|
module_.inc_pub_glob_count();
|
2015-08-05 21:47:01 +02:00
|
|
|
}
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-26 20:31:11 +01:00
|
|
|
struct BuildReducedGraphVisitor<'a, 'b: 'a, 'tcx: 'b> {
|
2014-12-30 12:16:42 -06:00
|
|
|
builder: GraphBuilder<'a, 'b, 'tcx>,
|
2015-10-26 20:31:11 +01:00
|
|
|
parent: Rc<Module>,
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b, 'v, 'tcx> Visitor<'v> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
|
2015-11-17 18:56:13 -05:00
|
|
|
fn visit_nested_item(&mut self, item: hir::ItemId) {
|
|
|
|
self.visit_item(self.builder.resolver.ast_map.expect_item(item.id))
|
|
|
|
}
|
|
|
|
|
2014-12-30 12:16:42 -06:00
|
|
|
fn visit_item(&mut self, item: &Item) {
|
2014-12-30 12:52:51 -06:00
|
|
|
let p = self.builder.build_reduced_graph_for_item(item, &self.parent);
|
2014-12-30 12:16:42 -06:00
|
|
|
let old_parent = replace(&mut self.parent, p);
|
2015-11-17 18:56:13 -05:00
|
|
|
intravisit::walk_item(self, item);
|
2014-12-30 12:16:42 -06:00
|
|
|
self.parent = old_parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_foreign_item(&mut self, foreign_item: &ForeignItem) {
|
2015-02-05 09:19:07 +02:00
|
|
|
self.builder.build_reduced_graph_for_foreign_item(foreign_item, &self.parent);
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_block(&mut self, block: &Block) {
|
2014-12-30 12:52:51 -06:00
|
|
|
let np = self.builder.build_reduced_graph_for_block(block, &self.parent);
|
2014-12-30 12:16:42 -06:00
|
|
|
let old_parent = replace(&mut self.parent, np);
|
2015-11-17 18:56:13 -05:00
|
|
|
intravisit::walk_block(self, block);
|
2014-12-30 12:16:42 -06:00
|
|
|
self.parent = old_parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
pub fn build_reduced_graph(resolver: &mut Resolver, krate: &hir::Crate) {
|
2015-10-26 20:31:11 +01:00
|
|
|
GraphBuilder { resolver: resolver }.build_reduced_graph(krate);
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn populate_module_if_necessary(resolver: &mut Resolver, module: &Rc<Module>) {
|
2015-10-26 20:31:11 +01:00
|
|
|
GraphBuilder { resolver: resolver }.populate_module_if_necessary(module);
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|