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.
|
|
|
|
|
2016-03-17 03:43:17 -05:00
|
|
|
use resolve_imports::ImportDirectiveSubclass::{self, GlobImport};
|
2014-12-30 12:16:42 -06:00
|
|
|
use Module;
|
2016-01-13 19:42:45 -06:00
|
|
|
use Namespace::{self, TypeNS, ValueNS};
|
2016-02-07 15:34:23 -06:00
|
|
|
use {NameBinding, NameBindingKind};
|
2016-01-11 15:19:29 -06:00
|
|
|
use ParentLink::{ModuleParentLink, BlockParentLink};
|
2014-12-30 12:16:42 -06:00
|
|
|
use Resolver;
|
2015-12-20 15:00:43 -06:00
|
|
|
use {resolve_error, resolve_struct_error, ResolutionError};
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2016-02-29 20:29:06 -06:00
|
|
|
use rustc::middle::cstore::{CrateStore, ChildItem, DlDef};
|
2016-03-19 19:04:12 -05:00
|
|
|
use rustc::lint;
|
2016-03-29 04:54:26 -05:00
|
|
|
use rustc::hir::def::*;
|
|
|
|
use rustc::hir::def_id::{CRATE_DEF_INDEX, DefId};
|
2016-03-25 01:08:11 -05:00
|
|
|
use rustc::ty::{self, VariantKind};
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2016-04-16 19:48:40 -05:00
|
|
|
use syntax::ast::{Name, NodeId};
|
2015-09-14 04:58:20 -05:00
|
|
|
use syntax::attr::AttrMetaMethods;
|
2016-04-16 19:48:40 -05:00
|
|
|
use syntax::parse::token::keywords;
|
2014-12-30 12:16:42 -06:00
|
|
|
use syntax::codemap::{Span, DUMMY_SP};
|
2015-07-31 02:04:06 -05:00
|
|
|
|
2016-03-29 00:50:44 -05:00
|
|
|
use rustc::hir;
|
|
|
|
use rustc::hir::{Block, DeclItem};
|
|
|
|
use rustc::hir::{ForeignItem, ForeignItemFn, ForeignItemStatic};
|
|
|
|
use rustc::hir::{Item, ItemConst, ItemEnum, ItemExternCrate, ItemFn};
|
|
|
|
use rustc::hir::{ItemForeignMod, ItemImpl, ItemMod, ItemStatic, ItemDefaultImpl};
|
|
|
|
use rustc::hir::{ItemStruct, ItemTrait, ItemTy, ItemUse};
|
|
|
|
use rustc::hir::{PathListIdent, PathListMod, StmtDecl};
|
|
|
|
use rustc::hir::{Variant, ViewPathGlob, ViewPathList, ViewPathSimple};
|
|
|
|
use rustc::hir::intravisit::{self, Visitor};
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2016-01-13 19:42:45 -06:00
|
|
|
trait ToNameBinding<'a> {
|
|
|
|
fn to_name_binding(self) -> NameBinding<'a>;
|
|
|
|
}
|
|
|
|
|
2016-04-25 23:20:50 -05:00
|
|
|
impl<'a> ToNameBinding<'a> for (Module<'a>, Span, ty::Visibility) {
|
2016-01-13 19:42:45 -06:00
|
|
|
fn to_name_binding(self) -> NameBinding<'a> {
|
2016-04-26 20:13:15 -05:00
|
|
|
NameBinding { kind: NameBindingKind::Module(self.0), span: self.1, vis: self.2 }
|
2016-01-13 19:42:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-16 20:57:09 -05:00
|
|
|
impl<'a> ToNameBinding<'a> for (Def, Span, ty::Visibility) {
|
2016-01-13 19:42:45 -06:00
|
|
|
fn to_name_binding(self) -> NameBinding<'a> {
|
2016-04-26 20:13:15 -05:00
|
|
|
NameBinding { kind: NameBindingKind::Def(self.0), span: self.1, vis: self.2 }
|
2016-01-13 19:42:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-02 04:22:24 -06:00
|
|
|
impl<'b, 'tcx:'b> Resolver<'b, 'tcx> {
|
2014-12-30 12:16:42 -06:00
|
|
|
/// Constructs the reduced graph for the entire crate.
|
2016-03-02 04:22:24 -06:00
|
|
|
pub fn build_reduced_graph(&mut self, krate: &hir::Crate) {
|
2014-12-30 12:16:42 -06:00
|
|
|
let mut visitor = BuildReducedGraphVisitor {
|
2016-01-11 15:19:29 -06:00
|
|
|
parent: self.graph_root,
|
2016-03-02 04:22:24 -06:00
|
|
|
resolver: self,
|
2014-12-30 12:16:42 -06:00
|
|
|
};
|
2015-11-17 17:56:13 -06:00
|
|
|
intravisit::walk_crate(&mut visitor, krate);
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2016-01-13 19:42:45 -06:00
|
|
|
/// Defines `name` in namespace `ns` of module `parent` to be `def` if it is not yet defined.
|
|
|
|
fn try_define<T>(&self, parent: Module<'b>, name: Name, ns: Namespace, def: T)
|
|
|
|
where T: ToNameBinding<'b>
|
|
|
|
{
|
2016-02-14 23:18:55 -06:00
|
|
|
let _ = parent.try_define_child(name, ns, def.to_name_binding());
|
2016-01-13 19:42:45 -06:00
|
|
|
}
|
2015-11-17 03:10:41 -06:00
|
|
|
|
2016-01-13 19:42:45 -06:00
|
|
|
/// Defines `name` in namespace `ns` of module `parent` to be `def` if it is not yet defined;
|
|
|
|
/// otherwise, reports an error.
|
|
|
|
fn define<T: ToNameBinding<'b>>(&self, parent: Module<'b>, name: Name, ns: Namespace, def: T) {
|
2016-02-14 23:18:55 -06:00
|
|
|
let binding = def.to_name_binding();
|
2016-03-16 00:20:58 -05:00
|
|
|
if let Err(old_binding) = parent.try_define_child(name, ns, binding.clone()) {
|
|
|
|
self.report_conflict(parent, name, ns, old_binding, &binding);
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn block_needs_anonymous_module(&mut self, block: &Block) -> bool {
|
2016-01-27 04:51:22 -06:00
|
|
|
fn is_item(statement: &hir::Stmt) -> bool {
|
|
|
|
if let StmtDecl(ref declaration, _) = statement.node {
|
|
|
|
if let DeclItem(_) = declaration.node {
|
|
|
|
return true;
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
}
|
2016-01-27 04:51:22 -06:00
|
|
|
false
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2016-01-27 04:51:22 -06:00
|
|
|
// If any statements are items, we need to create an anonymous module
|
|
|
|
block.stmts.iter().any(is_item)
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2016-04-16 19:48:40 -05:00
|
|
|
fn sanity_check_import(&self, view_path: &hir::ViewPath, id: NodeId) {
|
|
|
|
let path = match view_path.node {
|
|
|
|
ViewPathSimple(_, ref path) |
|
|
|
|
ViewPathGlob (ref path) |
|
|
|
|
ViewPathList(ref path, _) => path
|
|
|
|
};
|
|
|
|
|
|
|
|
// Check for type parameters
|
|
|
|
let found_param = path.segments.iter().any(|segment| {
|
|
|
|
!segment.parameters.types().is_empty() ||
|
|
|
|
!segment.parameters.lifetimes().is_empty() ||
|
|
|
|
!segment.parameters.bindings().is_empty()
|
|
|
|
});
|
|
|
|
if found_param {
|
2016-04-18 16:42:18 -05:00
|
|
|
self.session.span_err(path.span, "type or lifetime parameters in import path");
|
2016-04-16 19:48:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Checking for special identifiers in path
|
|
|
|
// prevent `self` or `super` at beginning of global path
|
|
|
|
if path.global && path.segments.len() > 0 {
|
|
|
|
let first = path.segments[0].identifier.name;
|
2016-04-18 16:42:18 -05:00
|
|
|
if first == keywords::Super.name() || first == keywords::SelfValue.name() {
|
2016-04-16 19:48:40 -05:00
|
|
|
self.session.add_lint(
|
|
|
|
lint::builtin::SUPER_OR_SELF_IN_GLOBAL_PATH, id, path.span,
|
|
|
|
format!("expected identifier, found keyword `{}`", first)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-30 12:16:42 -06:00
|
|
|
/// Constructs the reduced graph for one item.
|
2016-03-02 04:21:09 -06:00
|
|
|
fn build_reduced_graph_for_item(&mut self, item: &Item, parent_ref: &mut Module<'b>) {
|
|
|
|
let parent = *parent_ref;
|
2015-09-19 20:50:30 -05:00
|
|
|
let name = item.name;
|
2014-12-30 12:16:42 -06:00
|
|
|
let sp = item.span;
|
2016-04-09 18:19:53 -05:00
|
|
|
self.current_module = parent;
|
|
|
|
let vis = self.resolve_visibility(&item.vis);
|
2014-12-30 12:16:42 -06:00
|
|
|
|
|
|
|
match item.node {
|
2014-12-23 13:34:36 -06: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.
|
2016-03-19 19:04:12 -05:00
|
|
|
let module_path: Vec<Name> = match view_path.node {
|
2014-12-23 13:34:36 -06:00
|
|
|
ViewPathSimple(_, ref full_path) => {
|
|
|
|
full_path.segments
|
2015-10-26 14:31:11 -05:00
|
|
|
.split_last()
|
|
|
|
.unwrap()
|
|
|
|
.1
|
|
|
|
.iter()
|
|
|
|
.map(|seg| seg.identifier.name)
|
|
|
|
.collect()
|
2014-12-23 13:34:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
ViewPathGlob(ref module_ident_path) |
|
|
|
|
ViewPathList(ref module_ident_path, _) => {
|
|
|
|
module_ident_path.segments
|
2015-10-26 14:31:11 -05:00
|
|
|
.iter()
|
|
|
|
.map(|seg| seg.identifier.name)
|
|
|
|
.collect()
|
2014-12-23 13:34:36 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-16 19:48:40 -05:00
|
|
|
self.sanity_check_import(view_path, item.id);
|
2016-03-19 19:04:12 -05:00
|
|
|
|
2014-12-23 13:34:36 -06:00
|
|
|
// Build up the import directives.
|
2016-04-16 19:48:40 -05:00
|
|
|
let is_prelude = item.attrs.iter().any(|attr| attr.name() == "prelude_import");
|
2014-12-23 13:34:36 -06:00
|
|
|
|
|
|
|
match view_path.node {
|
|
|
|
ViewPathSimple(binding, ref full_path) => {
|
2015-10-26 14:31:11 -05:00
|
|
|
let source_name = full_path.segments.last().unwrap().identifier.name;
|
2015-07-28 11:07:20 -05:00
|
|
|
if source_name.as_str() == "mod" || source_name.as_str() == "self" {
|
2015-07-14 12:42:38 -05:00
|
|
|
resolve_error(self,
|
2015-07-28 11:07:20 -05:00
|
|
|
view_path.span,
|
|
|
|
ResolutionError::SelfImportsOnlyAllowedWithin);
|
2014-12-23 13:34:36 -06:00
|
|
|
}
|
|
|
|
|
2016-02-13 15:49:16 -06:00
|
|
|
let subclass = ImportDirectiveSubclass::single(binding, source_name);
|
2016-04-17 14:23:25 -05:00
|
|
|
let span = view_path.span;
|
|
|
|
parent.add_import_directive(module_path, subclass, span, item.id, vis);
|
2016-03-17 03:43:17 -05:00
|
|
|
self.unresolved_imports += 1;
|
2014-12-23 13:34:36 -06:00
|
|
|
}
|
|
|
|
ViewPathList(_, ref source_items) => {
|
|
|
|
// Make sure there's at most one `mod` import in the list.
|
2015-10-26 14:31:11 -05:00
|
|
|
let mod_spans = source_items.iter()
|
|
|
|
.filter_map(|item| {
|
|
|
|
match item.node {
|
|
|
|
PathListMod { .. } => Some(item.span),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<Span>>();
|
2014-12-23 13:34:36 -06:00
|
|
|
if mod_spans.len() > 1 {
|
2015-12-20 15:00:43 -06:00
|
|
|
let mut e = resolve_struct_error(self,
|
2015-10-26 14:31:11 -05:00
|
|
|
mod_spans[0],
|
|
|
|
ResolutionError::SelfImportCanOnlyAppearOnceInTheList);
|
2014-12-23 13:34:36 -06:00
|
|
|
for other_span in mod_spans.iter().skip(1) {
|
2015-12-23 00:27:20 -06:00
|
|
|
e.span_note(*other_span, "another `self` import appears here");
|
2014-12-23 13:34:36 -06:00
|
|
|
}
|
2015-12-23 00:27:20 -06:00
|
|
|
e.emit();
|
2014-12-23 13:34:36 -06:00
|
|
|
}
|
|
|
|
|
2015-01-31 11:20:46 -06:00
|
|
|
for source_item in source_items {
|
2015-08-01 00:20:25 -05:00
|
|
|
let (module_path, name, rename) = match source_item.node {
|
|
|
|
PathListIdent { name, rename, .. } =>
|
2015-09-20 06:51:40 -05:00
|
|
|
(module_path.clone(), name, rename.unwrap_or(name)),
|
2015-08-01 00:20:25 -05:00
|
|
|
PathListMod { rename, .. } => {
|
2014-12-23 13:34:36 -06:00
|
|
|
let name = match module_path.last() {
|
|
|
|
Some(name) => *name,
|
|
|
|
None => {
|
2015-07-14 12:42:38 -05:00
|
|
|
resolve_error(
|
2015-07-14 09:32:43 -05:00
|
|
|
self,
|
|
|
|
source_item.span,
|
2015-07-14 12:42:38 -05:00
|
|
|
ResolutionError::
|
2015-07-14 09:32:43 -05:00
|
|
|
SelfImportOnlyInImportListWithNonEmptyPrefix
|
2015-07-14 08:37:52 -05:00
|
|
|
);
|
2014-12-23 13:34:36 -06:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
2015-07-11 06:34:57 -05:00
|
|
|
let module_path = module_path.split_last().unwrap().1;
|
2015-09-20 06:51:40 -05:00
|
|
|
let rename = rename.unwrap_or(name);
|
2015-08-01 00:20:25 -05:00
|
|
|
(module_path.to_vec(), name, rename)
|
2014-12-23 13:34:36 -06:00
|
|
|
}
|
|
|
|
};
|
2016-02-13 15:49:16 -06:00
|
|
|
let subclass = ImportDirectiveSubclass::single(rename, name);
|
2016-04-17 14:23:25 -05:00
|
|
|
let (span, id) = (source_item.span, source_item.node.id());
|
|
|
|
parent.add_import_directive(module_path, subclass, span, id, vis);
|
2016-03-17 03:43:17 -05:00
|
|
|
self.unresolved_imports += 1;
|
2014-12-23 13:34:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ViewPathGlob(_) => {
|
2016-04-17 14:23:25 -05:00
|
|
|
let subclass = GlobImport { is_prelude: is_prelude };
|
|
|
|
let span = view_path.span;
|
|
|
|
parent.add_import_directive(module_path, subclass, span, item.id, vis);
|
2016-03-17 03:43:17 -05:00
|
|
|
self.unresolved_imports += 1;
|
2014-12-23 13:34:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ItemExternCrate(_) => {
|
2015-10-26 14:31:11 -05:00
|
|
|
// n.b. we don't need to look at the path option here, because cstore already
|
|
|
|
// did
|
2015-11-21 13:39:05 -06:00
|
|
|
if let Some(crate_id) = self.session.cstore.extern_mod_stmt_cnum(item.id) {
|
2015-10-26 14:31:11 -05:00
|
|
|
let def_id = DefId {
|
|
|
|
krate: crate_id,
|
|
|
|
index: CRATE_DEF_INDEX,
|
|
|
|
};
|
2016-01-11 15:19:29 -06:00
|
|
|
let parent_link = ModuleParentLink(parent, name);
|
2016-01-20 13:31:10 -06:00
|
|
|
let def = Def::Mod(def_id);
|
2016-04-25 23:20:50 -05:00
|
|
|
let module = self.new_extern_crate_module(parent_link, def, item.id);
|
|
|
|
self.define(parent, name, TypeNS, (module, sp, vis));
|
2016-01-29 16:21:36 -06:00
|
|
|
|
2016-02-24 23:23:50 -06:00
|
|
|
self.build_reduced_graph_for_external_crate(module);
|
2014-12-23 13:34:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-30 12:16:42 -06:00
|
|
|
ItemMod(..) => {
|
2016-01-11 15:19:29 -06:00
|
|
|
let parent_link = ModuleParentLink(parent, name);
|
2016-01-20 13:31:10 -06:00
|
|
|
let def = Def::Mod(self.ast_map.local_def_id(item.id));
|
2016-04-25 23:20:50 -05:00
|
|
|
let module = self.new_module(parent_link, Some(def), false);
|
|
|
|
self.define(parent, name, TypeNS, (module, sp, vis));
|
2016-04-17 15:41:57 -05:00
|
|
|
self.module_map.insert(item.id, module);
|
2016-03-02 04:21:09 -06:00
|
|
|
*parent_ref = module;
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2016-03-02 04:21:09 -06:00
|
|
|
ItemForeignMod(..) => {}
|
2014-12-30 12:16:42 -06:00
|
|
|
|
|
|
|
// These items live in the value namespace.
|
|
|
|
ItemStatic(_, m, _) => {
|
2015-07-31 02:04:06 -05:00
|
|
|
let mutbl = m == hir::MutMutable;
|
2016-01-13 19:42:45 -06:00
|
|
|
let def = Def::Static(self.ast_map.local_def_id(item.id), mutbl);
|
2016-04-16 20:57:09 -05:00
|
|
|
self.define(parent, name, ValueNS, (def, sp, vis));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
ItemConst(_, _) => {
|
2016-01-13 19:42:45 -06:00
|
|
|
let def = Def::Const(self.ast_map.local_def_id(item.id));
|
2016-04-16 20:57:09 -05:00
|
|
|
self.define(parent, name, ValueNS, (def, sp, vis));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2015-02-25 14:05:07 -06:00
|
|
|
ItemFn(_, _, _, _, _, _) => {
|
2016-01-20 13:31:10 -06:00
|
|
|
let def = Def::Fn(self.ast_map.local_def_id(item.id));
|
2016-04-16 20:57:09 -05:00
|
|
|
self.define(parent, name, ValueNS, (def, sp, vis));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// These items live in the type namespace.
|
|
|
|
ItemTy(..) => {
|
2016-01-20 13:31:10 -06:00
|
|
|
let def = Def::TyAlias(self.ast_map.local_def_id(item.id));
|
2016-04-16 20:57:09 -05:00
|
|
|
self.define(parent, name, TypeNS, (def, sp, vis));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
ItemEnum(ref enum_definition, _) => {
|
2016-01-11 15:19:29 -06:00
|
|
|
let parent_link = ModuleParentLink(parent, name);
|
2016-01-20 13:31:10 -06:00
|
|
|
let def = Def::Enum(self.ast_map.local_def_id(item.id));
|
2016-04-25 23:20:50 -05:00
|
|
|
let module = self.new_module(parent_link, Some(def), false);
|
|
|
|
self.define(parent, name, TypeNS, (module, sp, vis));
|
2014-12-30 12:52:51 -06:00
|
|
|
|
2015-01-31 11:20:46 -06:00
|
|
|
for variant in &(*enum_definition).variants {
|
2015-09-02 15:11:32 -05:00
|
|
|
let item_def_id = self.ast_map.local_def_id(item.id);
|
2016-04-25 23:20:50 -05:00
|
|
|
self.build_reduced_graph_for_variant(variant, item_def_id, module, vis);
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// These items live in both the type and value namespaces.
|
|
|
|
ItemStruct(ref struct_def, _) => {
|
|
|
|
// Define a name in the type namespace.
|
2016-01-13 19:42:45 -06:00
|
|
|
let def = Def::Struct(self.ast_map.local_def_id(item.id));
|
2016-04-16 20:57:09 -05:00
|
|
|
self.define(parent, name, TypeNS, (def, sp, vis));
|
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
|
2016-01-27 21:03:40 -06:00
|
|
|
if !struct_def.is_struct() {
|
|
|
|
let def = Def::Struct(self.ast_map.local_def_id(struct_def.id()));
|
2016-04-16 20:57:09 -05:00
|
|
|
self.define(parent, name, ValueNS, (def, sp, vis));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Record the def ID and fields of this struct.
|
2016-04-09 18:19:53 -05:00
|
|
|
let field_names = struct_def.fields().iter().map(|field| {
|
|
|
|
self.resolve_visibility(&field.vis);
|
|
|
|
field.name
|
|
|
|
}).collect();
|
2015-09-02 15:11:32 -05:00
|
|
|
let item_def_id = self.ast_map.local_def_id(item.id);
|
2016-02-26 18:05:14 -06:00
|
|
|
self.structs.insert(item_def_id, field_names);
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2016-03-02 04:21:09 -06:00
|
|
|
ItemDefaultImpl(_, _) | ItemImpl(..) => {}
|
2014-12-30 12:16:42 -06:00
|
|
|
|
|
|
|
ItemTrait(_, _, _, ref items) => {
|
2015-11-16 01:59:50 -06: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.
|
2016-01-11 15:19:29 -06:00
|
|
|
let parent_link = ModuleParentLink(parent, name);
|
2016-01-20 13:31:10 -06:00
|
|
|
let def = Def::Trait(def_id);
|
2016-04-25 23:20:50 -05:00
|
|
|
let module_parent = self.new_module(parent_link, Some(def), false);
|
|
|
|
self.define(parent, name, TypeNS, (module_parent, sp, vis));
|
2014-12-30 12:16:42 -06:00
|
|
|
|
|
|
|
// Add the names of all the items to the trait info.
|
2016-01-13 19:42:45 -06:00
|
|
|
for item in items {
|
|
|
|
let item_def_id = self.ast_map.local_def_id(item.id);
|
|
|
|
let (def, ns) = match item.node {
|
|
|
|
hir::ConstTraitItem(..) => (Def::AssociatedConst(item_def_id), ValueNS),
|
|
|
|
hir::MethodTraitItem(..) => (Def::Method(item_def_id), ValueNS),
|
|
|
|
hir::TypeTraitItem(..) => (Def::AssociatedTy(def_id, item_def_id), TypeNS),
|
|
|
|
};
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2016-04-16 20:57:09 -05:00
|
|
|
self.define(module_parent, item.name, ns, (def, item.span, vis));
|
2016-01-13 19:42:45 -06:00
|
|
|
|
|
|
|
self.trait_item_map.insert((item.name, def_id), item_def_id);
|
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,
|
2016-04-25 23:20:50 -05:00
|
|
|
parent: Module<'b>,
|
|
|
|
vis: ty::Visibility) {
|
2015-09-20 08:47:24 -05:00
|
|
|
let name = variant.node.name;
|
2016-01-17 13:57:54 -06:00
|
|
|
if variant.node.data.is_struct() {
|
2015-10-08 15:45:46 -05:00
|
|
|
// Not adding fields for variants as they are not accessed with a self receiver
|
2015-10-09 19:28:40 -05:00
|
|
|
let variant_def_id = self.ast_map.local_def_id(variant.node.data.id());
|
2015-10-08 15:45:46 -05:00
|
|
|
self.structs.insert(variant_def_id, Vec::new());
|
2016-01-17 13:57:54 -06:00
|
|
|
}
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2016-01-21 14:22:01 -06:00
|
|
|
// Variants are always treated as importable to allow them to be glob used.
|
|
|
|
// All variants are defined in both type and value namespaces as future-proofing.
|
2016-01-13 19:42:45 -06:00
|
|
|
let def = Def::Variant(item_id, self.ast_map.local_def_id(variant.node.data.id()));
|
2016-04-25 23:20:50 -05:00
|
|
|
self.define(parent, name, ValueNS, (def, variant.span, vis));
|
|
|
|
self.define(parent, name, TypeNS, (def, variant.span, vis));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Constructs the reduced graph for one foreign item.
|
2015-02-05 01:19:07 -06:00
|
|
|
fn build_reduced_graph_for_foreign_item(&mut self,
|
|
|
|
foreign_item: &ForeignItem,
|
2016-01-11 15:19:29 -06:00
|
|
|
parent: Module<'b>) {
|
2015-09-19 20:50:30 -05:00
|
|
|
let name = foreign_item.name;
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2015-02-05 01:19:07 -06:00
|
|
|
let def = match foreign_item.node {
|
|
|
|
ForeignItemFn(..) => {
|
2016-01-20 13:31:10 -06:00
|
|
|
Def::Fn(self.ast_map.local_def_id(foreign_item.id))
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
ForeignItemStatic(_, m) => {
|
2016-01-20 13:31:10 -06:00
|
|
|
Def::Static(self.ast_map.local_def_id(foreign_item.id), m)
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2015-02-05 01:19:07 -06:00
|
|
|
};
|
2016-04-09 18:19:53 -05:00
|
|
|
self.current_module = parent;
|
|
|
|
let vis = self.resolve_visibility(&foreign_item.vis);
|
2016-04-16 20:57:09 -05:00
|
|
|
self.define(parent, name, ValueNS, (def, foreign_item.span, vis));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2016-03-02 04:21:09 -06:00
|
|
|
fn build_reduced_graph_for_block(&mut self, block: &Block, parent: &mut Module<'b>) {
|
2014-12-30 12:16:42 -06:00
|
|
|
if self.block_needs_anonymous_module(block) {
|
|
|
|
let block_id = block.id;
|
|
|
|
|
2015-10-26 14:31:11 -05:00
|
|
|
debug!("(building reduced graph for block) creating a new anonymous module for block \
|
|
|
|
{}",
|
2014-12-30 12:16:42 -06:00
|
|
|
block_id);
|
|
|
|
|
2016-01-11 15:19:29 -06:00
|
|
|
let parent_link = BlockParentLink(parent, block_id);
|
2016-04-25 23:20:50 -05:00
|
|
|
let new_module = self.new_module(parent_link, None, false);
|
2016-04-17 15:41:57 -05:00
|
|
|
self.module_map.insert(block_id, new_module);
|
2016-03-02 04:21:09 -06:00
|
|
|
*parent = new_module;
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-29 20:29:06 -06:00
|
|
|
/// Builds the reduced graph for a single item in an external crate.
|
|
|
|
fn build_reduced_graph_for_external_crate_def(&mut self, parent: Module<'b>, xcdef: ChildItem) {
|
|
|
|
let def = match xcdef.def {
|
|
|
|
DlDef(def) => def,
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Def::ForeignMod(def_id) = def {
|
|
|
|
// Foreign modules have no names. Recur and populate eagerly.
|
|
|
|
for child in self.session.cstore.item_children(def_id) {
|
|
|
|
self.build_reduced_graph_for_external_crate_def(parent, child);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let name = xcdef.name;
|
2016-04-09 18:19:53 -05:00
|
|
|
let vis = if parent.is_trait() { ty::Visibility::Public } else { xcdef.vis };
|
2016-01-13 19:42:45 -06:00
|
|
|
|
2014-12-30 12:16:42 -06:00
|
|
|
match def {
|
2016-03-08 15:50:01 -06:00
|
|
|
Def::Mod(_) | Def::ForeignMod(_) | Def::Enum(..) => {
|
2016-04-09 18:19:53 -05:00
|
|
|
debug!("(building reduced graph for external crate) building module {} {:?}",
|
|
|
|
name, vis);
|
2016-02-29 20:29:06 -06:00
|
|
|
let parent_link = ModuleParentLink(parent, name);
|
2016-04-25 23:20:50 -05:00
|
|
|
let module = self.new_module(parent_link, Some(def), true);
|
|
|
|
self.try_define(parent, name, TypeNS, (module, DUMMY_SP, vis));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2016-01-20 13:31:10 -06:00
|
|
|
Def::Variant(_, variant_id) => {
|
2016-02-29 20:29:06 -06:00
|
|
|
debug!("(building reduced graph for external crate) building variant {}", name);
|
2016-01-21 14:22:01 -06:00
|
|
|
// Variants are always treated as importable to allow them to be glob used.
|
|
|
|
// All variants are defined in both type and value namespaces as future-proofing.
|
2016-04-16 20:57:09 -05:00
|
|
|
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
|
|
|
|
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
|
2016-01-17 13:57:54 -06:00
|
|
|
if self.session.cstore.variant_kind(variant_id) == Some(VariantKind::Struct) {
|
2015-10-26 14:31:11 -05:00
|
|
|
// Not adding fields for variants as they are not accessed with a self receiver
|
|
|
|
self.structs.insert(variant_id, Vec::new());
|
|
|
|
}
|
|
|
|
}
|
2016-01-20 13:31:10 -06:00
|
|
|
Def::Fn(..) |
|
|
|
|
Def::Static(..) |
|
|
|
|
Def::Const(..) |
|
|
|
|
Def::AssociatedConst(..) |
|
|
|
|
Def::Method(..) => {
|
2015-10-26 14:31:11 -05:00
|
|
|
debug!("(building reduced graph for external crate) building value (fn/static) {}",
|
2016-02-29 20:29:06 -06:00
|
|
|
name);
|
2016-04-16 20:57:09 -05:00
|
|
|
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2016-01-20 13:31:10 -06:00
|
|
|
Def::Trait(def_id) => {
|
2016-02-29 20:29:06 -06:00
|
|
|
debug!("(building reduced graph for external crate) building type {}", name);
|
2015-10-26 14:31:11 -05:00
|
|
|
|
|
|
|
// If this is a trait, add all the trait item names to the trait
|
|
|
|
// info.
|
|
|
|
|
2015-11-20 09:46:39 -06:00
|
|
|
let trait_item_def_ids = self.session.cstore.trait_item_def_ids(def_id);
|
2015-10-26 14:31:11 -05:00
|
|
|
for trait_item_def in &trait_item_def_ids {
|
2015-11-20 09:46:39 -06:00
|
|
|
let trait_item_name =
|
|
|
|
self.session.cstore.item_name(trait_item_def.def_id());
|
2015-10-26 14:31:11 -05: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());
|
|
|
|
}
|
2016-01-27 21:03:40 -06:00
|
|
|
|
2016-02-29 20:29:06 -06:00
|
|
|
let parent_link = ModuleParentLink(parent, name);
|
2016-04-25 23:20:50 -05:00
|
|
|
let module = self.new_module(parent_link, Some(def), true);
|
|
|
|
self.try_define(parent, name, TypeNS, (module, DUMMY_SP, vis));
|
2015-10-26 14:31:11 -05:00
|
|
|
}
|
2016-03-08 15:50:01 -06:00
|
|
|
Def::TyAlias(..) | Def::AssociatedTy(..) => {
|
2016-02-29 20:29:06 -06:00
|
|
|
debug!("(building reduced graph for external crate) building type {}", name);
|
2016-04-16 20:57:09 -05:00
|
|
|
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
|
2015-10-26 14:31:11 -05:00
|
|
|
}
|
2016-01-27 21:03:40 -06:00
|
|
|
Def::Struct(def_id)
|
|
|
|
if self.session.cstore.tuple_struct_definition_if_ctor(def_id).is_none() => {
|
2016-02-29 20:29:06 -06:00
|
|
|
debug!("(building reduced graph for external crate) building type and value for {}",
|
|
|
|
name);
|
2016-04-16 20:57:09 -05:00
|
|
|
self.try_define(parent, name, TypeNS, (def, DUMMY_SP, vis));
|
2016-01-17 13:57:54 -06:00
|
|
|
if let Some(ctor_def_id) = self.session.cstore.struct_ctor_def_id(def_id) {
|
2016-01-13 19:42:45 -06:00
|
|
|
let def = Def::Struct(ctor_def_id);
|
2016-04-16 20:57:09 -05:00
|
|
|
self.try_define(parent, name, ValueNS, (def, DUMMY_SP, vis));
|
2015-10-26 14:31:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Record the def ID and fields of this struct.
|
2016-01-17 13:57:54 -06:00
|
|
|
let fields = self.session.cstore.struct_field_names(def_id);
|
2015-10-26 14:31:11 -05:00
|
|
|
self.structs.insert(def_id, fields);
|
|
|
|
}
|
2016-01-27 21:03:40 -06:00
|
|
|
Def::Struct(..) => {}
|
2016-01-20 13:31:10 -06:00
|
|
|
Def::Local(..) |
|
|
|
|
Def::PrimTy(..) |
|
|
|
|
Def::TyParam(..) |
|
|
|
|
Def::Upvar(..) |
|
|
|
|
Def::Label(..) |
|
|
|
|
Def::SelfTy(..) |
|
|
|
|
Def::Err => {
|
2016-03-28 16:11:50 -05:00
|
|
|
bug!("didn't expect `{:?}`", def);
|
2015-10-26 14:31:11 -05:00
|
|
|
}
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds the reduced graph rooted at the 'use' directive for an external
|
|
|
|
/// crate.
|
2016-01-11 15:19:29 -06:00
|
|
|
fn build_reduced_graph_for_external_crate(&mut self, root: Module<'b>) {
|
2015-11-20 09:46:39 -06: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
|
|
|
}
|
|
|
|
|
2016-03-02 03:18:47 -06:00
|
|
|
/// Ensures that the reduced graph rooted at the given external module
|
|
|
|
/// is built, building it if it is not.
|
2016-03-02 04:22:24 -06:00
|
|
|
pub fn populate_module_if_necessary(&mut self, module: Module<'b>) {
|
2016-03-02 03:18:47 -06:00
|
|
|
if module.populated.get() { return }
|
|
|
|
for child in self.session.cstore.item_children(module.def_id().unwrap()) {
|
2016-03-02 04:22:24 -06:00
|
|
|
self.build_reduced_graph_for_external_crate_def(module, child);
|
2016-03-02 03:18:47 -06:00
|
|
|
}
|
|
|
|
module.populated.set(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-26 14:31:11 -05:00
|
|
|
struct BuildReducedGraphVisitor<'a, 'b: 'a, 'tcx: 'b> {
|
2016-03-02 04:22:24 -06:00
|
|
|
resolver: &'a mut Resolver<'b, 'tcx>,
|
2016-01-11 15:19:29 -06:00
|
|
|
parent: Module<'b>,
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b, 'v, 'tcx> Visitor<'v> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
|
2015-11-17 17:56:13 -06:00
|
|
|
fn visit_nested_item(&mut self, item: hir::ItemId) {
|
2016-03-02 04:22:24 -06:00
|
|
|
self.visit_item(self.resolver.ast_map.expect_item(item.id))
|
2015-11-17 17:56:13 -06:00
|
|
|
}
|
|
|
|
|
2014-12-30 12:16:42 -06:00
|
|
|
fn visit_item(&mut self, item: &Item) {
|
2016-03-02 04:21:09 -06:00
|
|
|
let old_parent = self.parent;
|
2016-03-02 04:22:24 -06:00
|
|
|
self.resolver.build_reduced_graph_for_item(item, &mut self.parent);
|
2015-11-17 17:56:13 -06: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) {
|
2016-03-02 04:22:24 -06:00
|
|
|
self.resolver.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) {
|
2016-03-02 04:21:09 -06:00
|
|
|
let old_parent = self.parent;
|
2016-03-02 04:22:24 -06:00
|
|
|
self.resolver.build_reduced_graph_for_block(block, &mut self.parent);
|
2015-11-17 17:56:13 -06:00
|
|
|
intravisit::walk_block(self, block);
|
2014-12-30 12:16:42 -06:00
|
|
|
self.parent = old_parent;
|
|
|
|
}
|
|
|
|
}
|