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-10-10 22:42:06 -05:00
|
|
|
use macros::{InvocationData, LegacyScope};
|
2016-10-27 22:40:58 -05:00
|
|
|
use resolve_imports::ImportDirective;
|
2016-03-17 03:43:17 -05:00
|
|
|
use resolve_imports::ImportDirectiveSubclass::{self, GlobImport};
|
2016-10-28 01:52:45 -05:00
|
|
|
use {Resolver, Module, ModuleS, ModuleKind, NameBinding, NameBindingKind, ToNameBinding};
|
2016-10-25 17:05:02 -05:00
|
|
|
use Namespace::{self, TypeNS, ValueNS, MacroNS};
|
2016-10-28 01:52:45 -05:00
|
|
|
use ResolveResult::Success;
|
2015-12-20 15:00:43 -06:00
|
|
|
use {resolve_error, resolve_struct_error, ResolutionError};
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2016-11-05 15:30:40 -05:00
|
|
|
use rustc::middle::cstore::{DepKind, LoadedMacro};
|
2016-03-29 04:54:26 -05:00
|
|
|
use rustc::hir::def::*;
|
2016-11-05 15:30:40 -05:00
|
|
|
use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefId};
|
2016-09-19 15:49:01 -05:00
|
|
|
use rustc::ty;
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2016-08-19 19:23:32 -05:00
|
|
|
use std::cell::Cell;
|
2016-09-16 01:45:03 -05:00
|
|
|
use std::rc::Rc;
|
2016-08-19 19:23:32 -05:00
|
|
|
|
2016-05-22 10:07:28 -05:00
|
|
|
use syntax::ast::Name;
|
2016-06-05 04:56:05 -05:00
|
|
|
use syntax::attr;
|
2016-09-14 16:51:46 -05:00
|
|
|
use syntax::parse::token;
|
2015-07-31 02:04:06 -05:00
|
|
|
|
2016-09-14 16:03:09 -05:00
|
|
|
use syntax::ast::{self, Block, ForeignItem, ForeignItemKind, Item, ItemKind};
|
2016-09-14 18:39:13 -05:00
|
|
|
use syntax::ast::{Mutability, StmtKind, TraitItem, TraitItemKind};
|
2016-05-22 10:07:28 -05:00
|
|
|
use syntax::ast::{Variant, ViewPathGlob, ViewPathList, ViewPathSimple};
|
2016-10-28 01:52:45 -05:00
|
|
|
use syntax::ext::base::SyntaxExtension;
|
2016-10-15 22:39:52 -05:00
|
|
|
use syntax::ext::expand::mark_tts;
|
2016-09-16 01:45:03 -05:00
|
|
|
use syntax::ext::hygiene::Mark;
|
2016-10-01 20:41:19 -05:00
|
|
|
use syntax::ext::tt::macro_rules;
|
2016-08-12 04:01:22 -05:00
|
|
|
use syntax::parse::token::keywords;
|
2016-04-23 22:26:10 -05:00
|
|
|
use syntax::visit::{self, Visitor};
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2016-06-21 17:08:13 -05:00
|
|
|
use syntax_pos::{Span, DUMMY_SP};
|
|
|
|
|
2016-11-07 16:23:26 -06:00
|
|
|
impl<'a> ToNameBinding<'a> for (Module<'a>, ty::Visibility, Span, Mark) {
|
2016-01-13 19:42:45 -06:00
|
|
|
fn to_name_binding(self) -> NameBinding<'a> {
|
2016-11-07 16:23:26 -06:00
|
|
|
NameBinding {
|
|
|
|
kind: NameBindingKind::Module(self.0),
|
|
|
|
vis: self.1,
|
|
|
|
span: self.2,
|
|
|
|
expansion: self.3,
|
|
|
|
}
|
2016-01-13 19:42:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-07 16:23:26 -06:00
|
|
|
impl<'a> ToNameBinding<'a> for (Def, ty::Visibility, Span, Mark) {
|
2016-01-13 19:42:45 -06:00
|
|
|
fn to_name_binding(self) -> NameBinding<'a> {
|
2016-11-07 16:23:26 -06:00
|
|
|
NameBinding {
|
|
|
|
kind: NameBindingKind::Def(self.0),
|
|
|
|
vis: self.1,
|
|
|
|
span: self.2,
|
|
|
|
expansion: self.3,
|
|
|
|
}
|
2016-01-13 19:42:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-17 02:46:25 -05:00
|
|
|
#[derive(Default, PartialEq, Eq)]
|
|
|
|
struct LegacyMacroImports {
|
|
|
|
import_all: Option<Span>,
|
|
|
|
imports: Vec<(Name, Span)>,
|
|
|
|
reexports: Vec<(Name, Span)>,
|
|
|
|
}
|
|
|
|
|
2016-04-23 22:26:10 -05:00
|
|
|
impl<'b> Resolver<'b> {
|
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.
|
2016-07-27 21:34:01 -05:00
|
|
|
fn define<T>(&mut self, parent: Module<'b>, name: Name, ns: Namespace, def: T)
|
|
|
|
where T: ToNameBinding<'b>,
|
|
|
|
{
|
2016-02-14 23:18:55 -06:00
|
|
|
let binding = def.to_name_binding();
|
2016-07-27 21:34:01 -05:00
|
|
|
if let Err(old_binding) = self.try_define(parent, name, ns, binding.clone()) {
|
2016-03-16 00:20:58 -05:00
|
|
|
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
|
|
|
// If any statements are items, we need to create an anonymous module
|
2016-06-16 21:30:01 -05:00
|
|
|
block.stmts.iter().any(|statement| match statement.node {
|
2016-09-14 16:03:09 -05:00
|
|
|
StmtKind::Item(_) | StmtKind::Mac(_) => true,
|
2016-06-16 21:30:01 -05:00
|
|
|
_ => false,
|
|
|
|
})
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2016-09-14 16:51:46 -05:00
|
|
|
fn insert_field_names(&mut self, def_id: DefId, field_names: Vec<Name>) {
|
|
|
|
if !field_names.is_empty() {
|
|
|
|
self.field_names.insert(def_id, field_names);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-30 12:16:42 -06:00
|
|
|
/// Constructs the reduced graph for one item.
|
2016-10-10 22:42:06 -05:00
|
|
|
fn build_reduced_graph_for_item(&mut self, item: &Item, expansion: Mark) {
|
2016-08-14 20:08:31 -05:00
|
|
|
let parent = self.current_module;
|
2016-04-23 22:26:10 -05:00
|
|
|
let name = item.ident.name;
|
2014-12-30 12:16:42 -06:00
|
|
|
let sp = item.span;
|
2016-04-09 18:19:53 -05:00
|
|
|
let vis = self.resolve_visibility(&item.vis);
|
2014-12-30 12:16:42 -06:00
|
|
|
|
|
|
|
match item.node {
|
2016-04-23 22:26:10 -05:00
|
|
|
ItemKind::Use(ref view_path) => {
|
2014-12-23 13:34:36 -06:00
|
|
|
// 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-10-15 22:06:09 -05:00
|
|
|
let module_path: Vec<_> = 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()
|
2016-10-15 22:06:09 -05:00
|
|
|
.map(|seg| seg.identifier)
|
2015-10-26 14:31:11 -05:00
|
|
|
.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()
|
2016-10-15 22:06:09 -05:00
|
|
|
.map(|seg| seg.identifier)
|
2015-10-26 14:31:11 -05:00
|
|
|
.collect()
|
2014-12-23 13:34:36 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Build up the import directives.
|
2016-06-05 04:56:05 -05:00
|
|
|
let is_prelude = attr::contains_name(&item.attrs, "prelude_import");
|
2014-12-23 13:34:36 -06:00
|
|
|
|
|
|
|
match view_path.node {
|
|
|
|
ViewPathSimple(binding, ref full_path) => {
|
2016-10-22 21:44:36 -05:00
|
|
|
let mut source = full_path.segments.last().unwrap().identifier;
|
|
|
|
let source_name = source.name.as_str();
|
|
|
|
if source_name == "mod" || source_name == "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);
|
2016-10-22 21:44:36 -05:00
|
|
|
} else if source_name == "$crate" && full_path.segments.len() == 1 {
|
|
|
|
let crate_root = self.resolve_crate_var(source.ctxt);
|
|
|
|
let crate_name = match crate_root.kind {
|
|
|
|
ModuleKind::Def(_, name) => name,
|
|
|
|
ModuleKind::Block(..) => unreachable!(),
|
|
|
|
};
|
|
|
|
source.name = crate_name;
|
|
|
|
|
|
|
|
self.session.struct_span_warn(item.span, "`$crate` may not be imported")
|
|
|
|
.note("`use $crate;` was erroneously allowed and \
|
|
|
|
will become a hard error in a future release")
|
|
|
|
.emit();
|
2014-12-23 13:34:36 -06:00
|
|
|
}
|
|
|
|
|
2016-10-22 21:44:36 -05:00
|
|
|
let subclass = ImportDirectiveSubclass::single(binding.name, source.name);
|
2016-11-07 16:23:26 -06:00
|
|
|
self.add_import_directive(
|
|
|
|
module_path, subclass, view_path.span, item.id, vis, expansion,
|
|
|
|
);
|
2014-12-23 13:34:36 -06:00
|
|
|
}
|
|
|
|
ViewPathList(_, ref source_items) => {
|
|
|
|
// Make sure there's at most one `mod` import in the list.
|
2016-04-23 22:26:10 -05:00
|
|
|
let mod_spans = source_items.iter().filter_map(|item| {
|
2016-08-12 04:01:22 -05:00
|
|
|
if item.node.name.name == keywords::SelfValue.name() {
|
|
|
|
Some(item.span)
|
|
|
|
} else {
|
|
|
|
None
|
2016-04-23 22:26:10 -05:00
|
|
|
}
|
|
|
|
}).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 {
|
2016-08-12 04:01:22 -05:00
|
|
|
let node = source_item.node;
|
|
|
|
let (module_path, name, rename) = {
|
|
|
|
if node.name.name != keywords::SelfValue.name() {
|
|
|
|
let rename = node.rename.unwrap_or(node.name).name;
|
|
|
|
(module_path.clone(), node.name.name, rename)
|
|
|
|
} else {
|
2014-12-23 13:34:36 -06:00
|
|
|
let name = match module_path.last() {
|
2016-10-15 22:06:09 -05:00
|
|
|
Some(ident) => ident.name,
|
2014-12-23 13:34:36 -06:00
|
|
|
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;
|
2016-08-12 04:01:22 -05:00
|
|
|
let rename = node.rename.map(|i| i.name).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-11-07 16:23:26 -06:00
|
|
|
let id = source_item.node.id;
|
|
|
|
self.add_import_directive(
|
|
|
|
module_path, subclass, source_item.span, id, vis, expansion,
|
|
|
|
);
|
2014-12-23 13:34:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ViewPathGlob(_) => {
|
2016-08-19 19:23:32 -05:00
|
|
|
let subclass = GlobImport {
|
|
|
|
is_prelude: is_prelude,
|
|
|
|
max_vis: Cell::new(ty::Visibility::PrivateExternal),
|
|
|
|
};
|
2016-11-07 16:23:26 -06:00
|
|
|
self.add_import_directive(
|
|
|
|
module_path, subclass, view_path.span, item.id, vis, expansion,
|
|
|
|
);
|
2014-12-23 13:34:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-23 22:26:10 -05:00
|
|
|
ItemKind::ExternCrate(_) => {
|
2016-11-05 15:30:40 -05:00
|
|
|
self.crate_loader.process_item(item, &self.definitions);
|
2016-10-15 15:50:30 -05:00
|
|
|
|
2016-09-16 01:45:03 -05:00
|
|
|
// n.b. we don't need to look at the path option here, because cstore already did
|
2016-11-05 15:30:40 -05:00
|
|
|
let crate_id = self.session.cstore.extern_mod_stmt_cnum(item.id).unwrap();
|
|
|
|
let module = self.get_extern_crate_root(crate_id);
|
2016-11-07 16:23:26 -06:00
|
|
|
let binding = (module, ty::Visibility::Public, sp, expansion).to_name_binding();
|
2016-11-05 15:30:40 -05:00
|
|
|
let binding = self.arenas.alloc_name_binding(binding);
|
|
|
|
let directive = self.arenas.alloc_import_directive(ImportDirective {
|
|
|
|
id: item.id,
|
|
|
|
parent: parent,
|
|
|
|
imported_module: Cell::new(Some(module)),
|
|
|
|
subclass: ImportDirectiveSubclass::ExternCrate,
|
|
|
|
span: item.span,
|
|
|
|
module_path: Vec::new(),
|
|
|
|
vis: Cell::new(vis),
|
2016-11-07 16:23:26 -06:00
|
|
|
expansion: expansion,
|
2016-11-05 15:30:40 -05:00
|
|
|
});
|
|
|
|
let imported_binding = self.import(binding, directive);
|
|
|
|
self.define(parent, name, TypeNS, imported_binding);
|
|
|
|
self.populate_module_if_necessary(module);
|
|
|
|
self.process_legacy_macro_imports(item, module, expansion);
|
2014-12-23 13:34:36 -06:00
|
|
|
}
|
|
|
|
|
2016-09-14 16:03:09 -05:00
|
|
|
ItemKind::Mod(..) if item.ident == keywords::Invalid.ident() => {} // Crate root
|
|
|
|
|
2016-04-23 22:26:10 -05:00
|
|
|
ItemKind::Mod(..) => {
|
|
|
|
let def = Def::Mod(self.definitions.local_def_id(item.id));
|
2016-09-19 00:25:17 -05:00
|
|
|
let module = self.arenas.alloc_module(ModuleS {
|
|
|
|
no_implicit_prelude: parent.no_implicit_prelude || {
|
2016-06-05 04:56:05 -05:00
|
|
|
attr::contains_name(&item.attrs, "no_implicit_prelude")
|
2016-09-19 00:25:17 -05:00
|
|
|
},
|
|
|
|
normal_ancestor_id: Some(item.id),
|
|
|
|
..ModuleS::new(Some(parent), ModuleKind::Def(def, name))
|
2016-06-05 04:56:05 -05:00
|
|
|
});
|
2016-11-07 16:23:26 -06:00
|
|
|
self.define(parent, name, TypeNS, (module, vis, sp, expansion));
|
2016-04-17 15:41:57 -05:00
|
|
|
self.module_map.insert(item.id, module);
|
2016-08-14 18:42:05 -05:00
|
|
|
|
|
|
|
// Descend into the module.
|
|
|
|
self.current_module = module;
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2016-11-05 15:30:40 -05:00
|
|
|
ItemKind::ForeignMod(..) => self.crate_loader.process_item(item, &self.definitions),
|
2014-12-30 12:16:42 -06:00
|
|
|
|
|
|
|
// These items live in the value namespace.
|
2016-04-23 22:26:10 -05:00
|
|
|
ItemKind::Static(_, m, _) => {
|
|
|
|
let mutbl = m == Mutability::Mutable;
|
|
|
|
let def = Def::Static(self.definitions.local_def_id(item.id), mutbl);
|
2016-11-07 16:23:26 -06:00
|
|
|
self.define(parent, name, ValueNS, (def, vis, sp, expansion));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2016-08-26 11:23:42 -05:00
|
|
|
ItemKind::Const(..) => {
|
2016-04-23 22:26:10 -05:00
|
|
|
let def = Def::Const(self.definitions.local_def_id(item.id));
|
2016-11-07 16:23:26 -06:00
|
|
|
self.define(parent, name, ValueNS, (def, vis, sp, expansion));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2016-08-26 11:23:42 -05:00
|
|
|
ItemKind::Fn(..) => {
|
2016-04-23 22:26:10 -05:00
|
|
|
let def = Def::Fn(self.definitions.local_def_id(item.id));
|
2016-11-07 16:23:26 -06:00
|
|
|
self.define(parent, name, ValueNS, (def, vis, sp, expansion));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// These items live in the type namespace.
|
2016-04-23 22:26:10 -05:00
|
|
|
ItemKind::Ty(..) => {
|
|
|
|
let def = Def::TyAlias(self.definitions.local_def_id(item.id));
|
2016-11-07 16:23:26 -06:00
|
|
|
self.define(parent, name, TypeNS, (def, vis, sp, expansion));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2016-04-23 22:26:10 -05:00
|
|
|
ItemKind::Enum(ref enum_definition, _) => {
|
2016-09-19 00:25:17 -05:00
|
|
|
let def = Def::Enum(self.definitions.local_def_id(item.id));
|
|
|
|
let module = self.new_module(parent, ModuleKind::Def(def, name), true);
|
2016-11-07 16:23:26 -06:00
|
|
|
self.define(parent, name, TypeNS, (module, vis, sp, expansion));
|
2014-12-30 12:52:51 -06:00
|
|
|
|
2015-01-31 11:20:46 -06:00
|
|
|
for variant in &(*enum_definition).variants {
|
2016-11-07 16:23:26 -06:00
|
|
|
self.build_reduced_graph_for_variant(variant, module, vis, expansion);
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// These items live in both the type and value namespaces.
|
2016-04-23 22:26:10 -05:00
|
|
|
ItemKind::Struct(ref struct_def, _) => {
|
2014-12-30 12:16:42 -06:00
|
|
|
// Define a name in the type namespace.
|
2016-04-23 22:26:10 -05:00
|
|
|
let def = Def::Struct(self.definitions.local_def_id(item.id));
|
2016-11-07 16:23:26 -06:00
|
|
|
self.define(parent, name, TypeNS, (def, vis, sp, expansion));
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2016-08-26 11:23:42 -05:00
|
|
|
// If this is a tuple or unit struct, define a name
|
|
|
|
// in the value namespace as well.
|
2016-01-27 21:03:40 -06:00
|
|
|
if !struct_def.is_struct() {
|
2016-09-14 16:51:46 -05:00
|
|
|
let ctor_def = Def::StructCtor(self.definitions.local_def_id(struct_def.id()),
|
2016-09-14 16:51:46 -05:00
|
|
|
CtorKind::from_ast(struct_def));
|
2016-11-07 16:23:26 -06:00
|
|
|
self.define(parent, name, ValueNS, (ctor_def, vis, sp, expansion));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2016-09-14 16:51:46 -05:00
|
|
|
// Record field names for error reporting.
|
|
|
|
let field_names = struct_def.fields().iter().filter_map(|field| {
|
2016-04-09 18:19:53 -05:00
|
|
|
self.resolve_visibility(&field.vis);
|
2016-04-23 22:26:10 -05:00
|
|
|
field.ident.map(|ident| ident.name)
|
2016-04-09 18:19:53 -05:00
|
|
|
}).collect();
|
2016-04-23 22:26:10 -05:00
|
|
|
let item_def_id = self.definitions.local_def_id(item.id);
|
2016-09-14 16:51:46 -05:00
|
|
|
self.insert_field_names(item_def_id, field_names);
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2016-08-06 13:56:02 -05:00
|
|
|
ItemKind::Union(ref vdata, _) => {
|
|
|
|
let def = Def::Union(self.definitions.local_def_id(item.id));
|
2016-11-07 16:23:26 -06:00
|
|
|
self.define(parent, name, TypeNS, (def, vis, sp, expansion));
|
2016-08-06 13:56:02 -05:00
|
|
|
|
2016-09-14 16:51:46 -05:00
|
|
|
// Record field names for error reporting.
|
|
|
|
let field_names = vdata.fields().iter().filter_map(|field| {
|
2016-08-06 13:56:02 -05:00
|
|
|
self.resolve_visibility(&field.vis);
|
|
|
|
field.ident.map(|ident| ident.name)
|
|
|
|
}).collect();
|
|
|
|
let item_def_id = self.definitions.local_def_id(item.id);
|
2016-09-14 16:51:46 -05:00
|
|
|
self.insert_field_names(item_def_id, field_names);
|
2016-08-06 13:56:02 -05:00
|
|
|
}
|
2016-08-29 00:04:31 -05:00
|
|
|
|
2016-08-26 11:23:42 -05:00
|
|
|
ItemKind::DefaultImpl(..) | ItemKind::Impl(..) => {}
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2016-09-14 18:39:13 -05:00
|
|
|
ItemKind::Trait(..) => {
|
2016-04-23 22:26:10 -05:00
|
|
|
let def_id = self.definitions.local_def_id(item.id);
|
2015-11-16 01:59:50 -06:00
|
|
|
|
2014-12-30 12:16:42 -06:00
|
|
|
// Add all the items within to a new module.
|
2016-09-19 00:25:17 -05:00
|
|
|
let module =
|
|
|
|
self.new_module(parent, ModuleKind::Def(Def::Trait(def_id), name), true);
|
2016-11-07 16:23:26 -06:00
|
|
|
self.define(parent, name, TypeNS, (module, vis, sp, expansion));
|
2016-09-14 18:39:13 -05:00
|
|
|
self.current_module = module;
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2016-04-23 22:26:10 -05:00
|
|
|
ItemKind::Mac(_) => panic!("unexpanded macro in resolve!"),
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constructs the reduced graph for one variant. Variants exist in the
|
|
|
|
// type and value namespaces.
|
2016-11-07 16:23:26 -06:00
|
|
|
fn build_reduced_graph_for_variant(
|
|
|
|
&mut self, variant: &Variant, parent: Module<'b>, vis: ty::Visibility, expansion: Mark,
|
|
|
|
) {
|
2016-04-23 22:26:10 -05:00
|
|
|
let name = variant.node.name.name;
|
2016-09-14 16:51:46 -05:00
|
|
|
let def_id = self.definitions.local_def_id(variant.node.data.id());
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2016-09-14 16:51:46 -05:00
|
|
|
// Define a name in the type namespace.
|
|
|
|
let def = Def::Variant(def_id);
|
2016-11-07 16:23:26 -06:00
|
|
|
self.define(parent, name, TypeNS, (def, vis, variant.span, expansion));
|
2016-09-14 16:51:46 -05:00
|
|
|
|
|
|
|
// Define a constructor name in the value namespace.
|
|
|
|
// Braced variants, unlike structs, generate unusable names in
|
|
|
|
// value namespace, they are reserved for possible future use.
|
2016-09-14 16:51:46 -05:00
|
|
|
let ctor_kind = CtorKind::from_ast(&variant.node.data);
|
2016-09-14 16:51:46 -05:00
|
|
|
let ctor_def = Def::VariantCtor(def_id, ctor_kind);
|
2016-11-07 16:23:26 -06:00
|
|
|
self.define(parent, name, ValueNS, (ctor_def, vis, variant.span, expansion));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Constructs the reduced graph for one foreign item.
|
2016-11-07 16:23:26 -06:00
|
|
|
fn build_reduced_graph_for_foreign_item(
|
|
|
|
&mut self, foreign_item: &ForeignItem, expansion: Mark,
|
|
|
|
) {
|
2016-08-14 20:08:31 -05:00
|
|
|
let parent = self.current_module;
|
2016-04-23 22:26:10 -05:00
|
|
|
let name = foreign_item.ident.name;
|
2014-12-30 12:16:42 -06:00
|
|
|
|
2015-02-05 01:19:07 -06:00
|
|
|
let def = match foreign_item.node {
|
2016-04-23 22:26:10 -05:00
|
|
|
ForeignItemKind::Fn(..) => {
|
|
|
|
Def::Fn(self.definitions.local_def_id(foreign_item.id))
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2016-04-23 22:26:10 -05:00
|
|
|
ForeignItemKind::Static(_, m) => {
|
|
|
|
Def::Static(self.definitions.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
|
|
|
let vis = self.resolve_visibility(&foreign_item.vis);
|
2016-11-07 16:23:26 -06:00
|
|
|
self.define(parent, name, ValueNS, (def, vis, foreign_item.span, expansion));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2016-08-14 20:08:31 -05:00
|
|
|
fn build_reduced_graph_for_block(&mut self, block: &Block) {
|
|
|
|
let parent = self.current_module;
|
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-09-19 00:25:17 -05:00
|
|
|
let new_module = self.new_module(parent, ModuleKind::Block(block_id), true);
|
2016-04-17 15:41:57 -05:00
|
|
|
self.module_map.insert(block_id, new_module);
|
2016-08-14 20:08:31 -05:00
|
|
|
self.current_module = new_module; // Descend into the block.
|
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.
|
2016-09-15 03:05:45 -05:00
|
|
|
fn build_reduced_graph_for_external_crate_def(&mut self, parent: Module<'b>,
|
|
|
|
child: Export) {
|
2016-09-19 15:49:01 -05:00
|
|
|
let name = child.name;
|
2016-09-14 16:51:46 -05:00
|
|
|
let def = child.def;
|
|
|
|
let def_id = def.def_id();
|
2016-11-05 15:30:40 -05:00
|
|
|
let vis = match def {
|
|
|
|
Def::Macro(..) => ty::Visibility::Public,
|
|
|
|
_ if parent.is_trait() => ty::Visibility::Public,
|
|
|
|
_ => self.session.cstore.visibility(def_id),
|
2016-09-15 03:05:45 -05:00
|
|
|
};
|
2016-01-13 19:42:45 -06:00
|
|
|
|
2014-12-30 12:16:42 -06:00
|
|
|
match def {
|
2016-09-14 16:51:46 -05:00
|
|
|
Def::Mod(..) | Def::Enum(..) => {
|
2016-09-19 00:25:17 -05:00
|
|
|
let module = self.new_module(parent, ModuleKind::Def(def, name), false);
|
2016-11-07 16:23:26 -06:00
|
|
|
self.define(parent, name, TypeNS, (module, vis, DUMMY_SP, Mark::root()));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2016-09-14 16:51:46 -05:00
|
|
|
Def::Variant(..) => {
|
2016-11-07 16:23:26 -06:00
|
|
|
self.define(parent, name, TypeNS, (def, vis, DUMMY_SP, Mark::root()));
|
2015-10-26 14:31:11 -05:00
|
|
|
}
|
2016-09-14 16:51:46 -05:00
|
|
|
Def::VariantCtor(..) => {
|
2016-11-07 16:23:26 -06:00
|
|
|
self.define(parent, name, ValueNS, (def, vis, DUMMY_SP, Mark::root()));
|
2016-09-14 16:51:46 -05:00
|
|
|
}
|
2016-01-20 13:31:10 -06:00
|
|
|
Def::Fn(..) |
|
|
|
|
Def::Static(..) |
|
|
|
|
Def::Const(..) |
|
|
|
|
Def::AssociatedConst(..) |
|
|
|
|
Def::Method(..) => {
|
2016-11-07 16:23:26 -06:00
|
|
|
self.define(parent, name, ValueNS, (def, vis, DUMMY_SP, Mark::root()));
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2016-09-14 16:51:46 -05:00
|
|
|
Def::Trait(..) => {
|
|
|
|
let module = self.new_module(parent, ModuleKind::Def(def, name), false);
|
2016-11-07 16:23:26 -06:00
|
|
|
self.define(parent, name, TypeNS, (module, vis, DUMMY_SP, Mark::root()));
|
2015-10-26 14:31:11 -05:00
|
|
|
|
2016-09-14 16:51:46 -05:00
|
|
|
// If this is a trait, add all the trait item names to the trait info.
|
2016-11-09 18:06:34 -06:00
|
|
|
let trait_item_def_ids = self.session.cstore.associated_item_def_ids(def_id);
|
2016-09-14 16:51:46 -05:00
|
|
|
for trait_item_def_id in trait_item_def_ids {
|
|
|
|
let trait_item_name = self.session.cstore.def_key(trait_item_def_id)
|
|
|
|
.disambiguated_data.data.get_opt_name()
|
|
|
|
.expect("opt_item_name returned None for trait");
|
2016-04-26 03:29:13 -05:00
|
|
|
self.trait_item_map.insert((trait_item_name, def_id), false);
|
2015-10-26 14:31:11 -05:00
|
|
|
}
|
|
|
|
}
|
2016-03-08 15:50:01 -06:00
|
|
|
Def::TyAlias(..) | Def::AssociatedTy(..) => {
|
2016-11-07 16:23:26 -06:00
|
|
|
self.define(parent, name, TypeNS, (def, vis, DUMMY_SP, Mark::root()));
|
2015-10-26 14:31:11 -05:00
|
|
|
}
|
2016-09-14 16:51:46 -05:00
|
|
|
Def::Struct(..) => {
|
2016-11-07 16:23:26 -06:00
|
|
|
self.define(parent, name, TypeNS, (def, vis, DUMMY_SP, Mark::root()));
|
2015-10-26 14:31:11 -05:00
|
|
|
|
2016-09-14 16:51:46 -05:00
|
|
|
// Record field names for error reporting.
|
|
|
|
let field_names = self.session.cstore.struct_field_names(def_id);
|
|
|
|
self.insert_field_names(def_id, field_names);
|
2015-10-26 14:31:11 -05:00
|
|
|
}
|
2016-09-14 16:51:46 -05:00
|
|
|
Def::StructCtor(..) => {
|
2016-11-07 16:23:26 -06:00
|
|
|
self.define(parent, name, ValueNS, (def, vis, DUMMY_SP, Mark::root()));
|
2016-09-14 16:51:46 -05:00
|
|
|
}
|
2016-09-14 16:51:46 -05:00
|
|
|
Def::Union(..) => {
|
2016-11-07 16:23:26 -06:00
|
|
|
self.define(parent, name, TypeNS, (def, vis, DUMMY_SP, Mark::root()));
|
2016-08-06 13:56:02 -05:00
|
|
|
|
2016-09-14 16:51:46 -05:00
|
|
|
// Record field names for error reporting.
|
|
|
|
let field_names = self.session.cstore.struct_field_names(def_id);
|
|
|
|
self.insert_field_names(def_id, field_names);
|
2016-08-06 13:56:02 -05:00
|
|
|
}
|
2016-10-25 17:05:02 -05:00
|
|
|
Def::Macro(..) => {
|
2016-11-07 16:23:26 -06:00
|
|
|
self.define(parent, name, MacroNS, (def, vis, DUMMY_SP, Mark::root()));
|
2016-10-25 17:05:02 -05:00
|
|
|
}
|
2016-01-20 13:31:10 -06:00
|
|
|
Def::Local(..) |
|
|
|
|
Def::PrimTy(..) |
|
|
|
|
Def::TyParam(..) |
|
|
|
|
Def::Upvar(..) |
|
|
|
|
Def::Label(..) |
|
|
|
|
Def::SelfTy(..) |
|
|
|
|
Def::Err => {
|
2016-09-14 16:51:46 -05:00
|
|
|
bug!("unexpected definition: {:?}", def);
|
2015-10-26 14:31:11 -05:00
|
|
|
}
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-28 00:17:06 -05:00
|
|
|
fn get_extern_crate_root(&mut self, cnum: CrateNum) -> Module<'b> {
|
|
|
|
let def_id = DefId { krate: cnum, index: CRATE_DEF_INDEX };
|
2016-10-28 00:56:06 -05:00
|
|
|
let macros_only = self.session.cstore.dep_kind(cnum) == DepKind::MacrosOnly;
|
2016-10-28 00:17:06 -05:00
|
|
|
let arenas = self.arenas;
|
2016-10-28 00:56:06 -05:00
|
|
|
*self.extern_crate_roots.entry((cnum, macros_only)).or_insert_with(|| {
|
2016-10-28 00:17:06 -05:00
|
|
|
arenas.alloc_module(ModuleS {
|
|
|
|
populated: Cell::new(false),
|
|
|
|
..ModuleS::new(None, ModuleKind::Def(Def::Mod(def_id), keywords::Invalid.name()))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-10-28 01:52:45 -05:00
|
|
|
pub fn get_macro(&mut self, def: Def) -> Rc<SyntaxExtension> {
|
|
|
|
let def_id = match def {
|
|
|
|
Def::Macro(def_id) => def_id,
|
|
|
|
_ => panic!("Expected Def::Macro(..)"),
|
|
|
|
};
|
|
|
|
if let Some(ext) = self.macro_map.get(&def_id) {
|
|
|
|
return ext.clone();
|
|
|
|
}
|
|
|
|
|
2016-11-05 15:30:40 -05:00
|
|
|
let mut macro_rules = match self.session.cstore.load_macro(def_id, &self.session) {
|
|
|
|
LoadedMacro::MacroRules(macro_rules) => macro_rules,
|
|
|
|
LoadedMacro::ProcMacro(ext) => return ext,
|
|
|
|
};
|
|
|
|
|
2016-10-28 01:52:45 -05:00
|
|
|
let mark = Mark::fresh();
|
|
|
|
let invocation = self.arenas.alloc_invocation_data(InvocationData {
|
|
|
|
module: Cell::new(self.get_extern_crate_root(def_id.krate)),
|
|
|
|
def_index: CRATE_DEF_INDEX,
|
|
|
|
const_integer: false,
|
|
|
|
legacy_scope: Cell::new(LegacyScope::Empty),
|
|
|
|
expansion: Cell::new(LegacyScope::Empty),
|
|
|
|
});
|
|
|
|
self.invocations.insert(mark, invocation);
|
|
|
|
macro_rules.body = mark_tts(¯o_rules.body, mark);
|
|
|
|
let ext = Rc::new(macro_rules::compile(&self.session.parse_sess, ¯o_rules));
|
|
|
|
self.macro_map.insert(def_id, ext.clone());
|
|
|
|
ext
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
2016-09-16 01:45:03 -05:00
|
|
|
|
2016-11-07 16:08:26 -06:00
|
|
|
fn legacy_import_macro(
|
|
|
|
&mut self, name: Name, binding: &'b NameBinding<'b>, span: Span, allow_shadowing: bool,
|
|
|
|
) {
|
|
|
|
self.used_crates.insert(binding.def().def_id().krate);
|
2016-10-28 01:52:45 -05:00
|
|
|
self.macro_names.insert(name);
|
2016-11-07 16:08:26 -06:00
|
|
|
if self.builtin_macros.insert(name, binding).is_some() && !allow_shadowing {
|
2016-10-28 01:52:45 -05:00
|
|
|
let msg = format!("`{}` is already in scope", name);
|
|
|
|
let note =
|
|
|
|
"macro-expanded `#[macro_use]`s may not shadow existing macros (see RFC 1560)";
|
|
|
|
self.session.struct_span_err(span, &msg).note(note).emit();
|
|
|
|
}
|
|
|
|
}
|
2016-10-17 02:46:25 -05:00
|
|
|
|
2016-11-05 15:30:40 -05:00
|
|
|
fn process_legacy_macro_imports(&mut self, item: &Item, module: Module<'b>, expansion: Mark) {
|
|
|
|
let allow_shadowing = expansion == Mark::root();
|
|
|
|
let legacy_imports = self.legacy_macro_imports(&item.attrs);
|
|
|
|
let cnum = module.def_id().unwrap().krate;
|
|
|
|
|
|
|
|
// `#[macro_use]` and `#[macro_reexport]` are only allowed at the crate root.
|
|
|
|
if self.current_module.parent.is_some() && legacy_imports != LegacyMacroImports::default() {
|
|
|
|
span_err!(self.session, item.span, E0468,
|
|
|
|
"an `extern crate` loading macros must be at the crate root");
|
|
|
|
} else if self.session.cstore.dep_kind(cnum) == DepKind::MacrosOnly &&
|
|
|
|
legacy_imports == LegacyMacroImports::default() {
|
|
|
|
let msg = "custom derive crates and `#[no_link]` crates have no effect without \
|
|
|
|
`#[macro_use]`";
|
|
|
|
self.session.span_warn(item.span, msg);
|
|
|
|
self.used_crates.insert(cnum); // Avoid the normal unused extern crate warning
|
|
|
|
}
|
|
|
|
|
2016-10-28 01:52:45 -05:00
|
|
|
if let Some(span) = legacy_imports.import_all {
|
|
|
|
module.for_each_child(|name, ns, binding| if ns == MacroNS {
|
2016-11-07 16:08:26 -06:00
|
|
|
self.legacy_import_macro(name, binding, span, allow_shadowing);
|
2016-10-28 01:52:45 -05:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
for (name, span) in legacy_imports.imports {
|
|
|
|
let result = self.resolve_name_in_module(module, name, MacroNS, false, None);
|
|
|
|
if let Success(binding) = result {
|
2016-11-07 16:08:26 -06:00
|
|
|
self.legacy_import_macro(name, binding, span, allow_shadowing);
|
2016-10-17 02:46:25 -05:00
|
|
|
} else {
|
2016-10-28 01:52:45 -05:00
|
|
|
span_err!(self.session, span, E0469, "imported macro not found");
|
2016-10-17 02:46:25 -05:00
|
|
|
}
|
|
|
|
}
|
2016-10-28 01:52:45 -05:00
|
|
|
}
|
|
|
|
for (name, span) in legacy_imports.reexports {
|
|
|
|
self.used_crates.insert(module.def_id().unwrap().krate);
|
|
|
|
let result = self.resolve_name_in_module(module, name, MacroNS, false, None);
|
|
|
|
if let Success(binding) = result {
|
2016-11-05 15:30:40 -05:00
|
|
|
self.macro_exports.push(Export { name: name, def: binding.def() });
|
2016-10-28 01:52:45 -05:00
|
|
|
} else {
|
|
|
|
span_err!(self.session, span, E0470, "reexported macro not found");
|
2016-10-17 02:46:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-16 01:45:03 -05:00
|
|
|
// does this attribute list contain "macro_use"?
|
|
|
|
fn contains_macro_use(&mut self, attrs: &[ast::Attribute]) -> bool {
|
|
|
|
for attr in attrs {
|
|
|
|
if attr.check_name("macro_escape") {
|
|
|
|
let msg = "macro_escape is a deprecated synonym for macro_use";
|
|
|
|
let mut err = self.session.struct_span_warn(attr.span, msg);
|
|
|
|
if let ast::AttrStyle::Inner = attr.node.style {
|
|
|
|
err.help("consider an outer attribute, #[macro_use] mod ...").emit();
|
|
|
|
} else {
|
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
} else if !attr.check_name("macro_use") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if !attr.is_word() {
|
|
|
|
self.session.span_err(attr.span, "arguments to macro_use are not allowed here");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
2016-10-17 02:46:25 -05:00
|
|
|
|
|
|
|
fn legacy_macro_imports(&mut self, attrs: &[ast::Attribute]) -> LegacyMacroImports {
|
|
|
|
let mut imports = LegacyMacroImports::default();
|
|
|
|
for attr in attrs {
|
|
|
|
if attr.check_name("macro_use") {
|
|
|
|
match attr.meta_item_list() {
|
|
|
|
Some(names) => for attr in names {
|
|
|
|
if let Some(word) = attr.word() {
|
|
|
|
imports.imports.push((token::intern(&word.name()), attr.span()));
|
|
|
|
} else {
|
|
|
|
span_err!(self.session, attr.span(), E0466, "bad macro import");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => imports.import_all = Some(attr.span),
|
|
|
|
}
|
|
|
|
} else if attr.check_name("macro_reexport") {
|
|
|
|
let bad_macro_reexport = |this: &mut Self, span| {
|
|
|
|
span_err!(this.session, span, E0467, "bad macro reexport");
|
|
|
|
};
|
|
|
|
if let Some(names) = attr.meta_item_list() {
|
|
|
|
for attr in names {
|
|
|
|
if let Some(word) = attr.word() {
|
|
|
|
imports.reexports.push((token::intern(&word.name()), attr.span()));
|
|
|
|
} else {
|
|
|
|
bad_macro_reexport(self, attr.span());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bad_macro_reexport(self, attr.span());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
imports
|
|
|
|
}
|
2016-03-02 03:18:47 -06:00
|
|
|
}
|
|
|
|
|
2016-09-14 16:03:09 -05:00
|
|
|
pub struct BuildReducedGraphVisitor<'a, 'b: 'a> {
|
|
|
|
pub resolver: &'a mut Resolver<'b>,
|
2016-10-06 03:04:30 -05:00
|
|
|
pub legacy_scope: LegacyScope<'b>,
|
2016-10-10 22:42:06 -05:00
|
|
|
pub expansion: Mark,
|
2016-09-14 16:03:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
|
2016-10-06 03:04:30 -05:00
|
|
|
fn visit_invoc(&mut self, id: ast::NodeId) -> &'b InvocationData<'b> {
|
2016-11-11 04:51:15 -06:00
|
|
|
let mark = Mark::from_placeholder_id(id);
|
|
|
|
self.resolver.current_module.unresolved_invocations.borrow_mut().insert(mark);
|
|
|
|
let invocation = self.resolver.invocations[&mark];
|
2016-10-06 03:04:30 -05:00
|
|
|
invocation.module.set(self.resolver.current_module);
|
|
|
|
invocation.legacy_scope.set(self.legacy_scope);
|
|
|
|
invocation
|
2016-09-14 16:03:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! method {
|
|
|
|
($visit:ident: $ty:ty, $invoc:path, $walk:ident) => {
|
|
|
|
fn $visit(&mut self, node: &$ty) {
|
2016-10-06 03:04:30 -05:00
|
|
|
if let $invoc(..) = node.node {
|
|
|
|
self.visit_invoc(node.id);
|
|
|
|
} else {
|
|
|
|
visit::$walk(self, node);
|
2016-09-14 16:03:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
2016-06-12 02:51:31 -05:00
|
|
|
impl<'a, 'b> Visitor for BuildReducedGraphVisitor<'a, 'b> {
|
2016-09-14 16:03:09 -05:00
|
|
|
method!(visit_impl_item: ast::ImplItem, ast::ImplItemKind::Macro, walk_impl_item);
|
|
|
|
method!(visit_expr: ast::Expr, ast::ExprKind::Mac, walk_expr);
|
|
|
|
method!(visit_pat: ast::Pat, ast::PatKind::Mac, walk_pat);
|
|
|
|
method!(visit_ty: ast::Ty, ast::TyKind::Mac, walk_ty);
|
|
|
|
|
2014-12-30 12:16:42 -06:00
|
|
|
fn visit_item(&mut self, item: &Item) {
|
2016-10-06 03:04:30 -05:00
|
|
|
let macro_use = match item.node {
|
2016-09-14 16:03:09 -05:00
|
|
|
ItemKind::Mac(..) if item.id == ast::DUMMY_NODE_ID => return, // Scope placeholder
|
2016-10-06 03:04:30 -05:00
|
|
|
ItemKind::Mac(..) => {
|
|
|
|
return self.legacy_scope = LegacyScope::Expansion(self.visit_invoc(item.id));
|
|
|
|
}
|
|
|
|
ItemKind::Mod(..) => self.resolver.contains_macro_use(&item.attrs),
|
|
|
|
_ => false,
|
|
|
|
};
|
2016-09-14 16:03:09 -05:00
|
|
|
|
2016-10-06 03:04:30 -05:00
|
|
|
let (parent, legacy_scope) = (self.resolver.current_module, self.legacy_scope);
|
2016-10-10 22:42:06 -05:00
|
|
|
self.resolver.build_reduced_graph_for_item(item, self.expansion);
|
2016-09-16 17:21:46 -05:00
|
|
|
visit::walk_item(self, item);
|
|
|
|
self.resolver.current_module = parent;
|
2016-10-06 03:04:30 -05:00
|
|
|
if !macro_use {
|
|
|
|
self.legacy_scope = legacy_scope;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_stmt(&mut self, stmt: &ast::Stmt) {
|
|
|
|
if let ast::StmtKind::Mac(..) = stmt.node {
|
|
|
|
self.legacy_scope = LegacyScope::Expansion(self.visit_invoc(stmt.id));
|
|
|
|
} else {
|
|
|
|
visit::walk_stmt(self, stmt);
|
|
|
|
}
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_foreign_item(&mut self, foreign_item: &ForeignItem) {
|
2016-11-07 16:23:26 -06:00
|
|
|
self.resolver.build_reduced_graph_for_foreign_item(foreign_item, self.expansion);
|
2016-09-14 16:03:09 -05:00
|
|
|
visit::walk_foreign_item(self, foreign_item);
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_block(&mut self, block: &Block) {
|
2016-10-06 03:04:30 -05:00
|
|
|
let (parent, legacy_scope) = (self.resolver.current_module, self.legacy_scope);
|
2016-08-14 20:08:31 -05:00
|
|
|
self.resolver.build_reduced_graph_for_block(block);
|
2016-09-16 17:21:46 -05:00
|
|
|
visit::walk_block(self, block);
|
|
|
|
self.resolver.current_module = parent;
|
2016-10-06 03:04:30 -05:00
|
|
|
self.legacy_scope = legacy_scope;
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|
2016-09-14 18:39:13 -05:00
|
|
|
|
|
|
|
fn visit_trait_item(&mut self, item: &TraitItem) {
|
|
|
|
let parent = self.resolver.current_module;
|
|
|
|
let def_id = parent.def_id().unwrap();
|
|
|
|
|
2016-10-04 00:36:14 -05:00
|
|
|
if let TraitItemKind::Macro(_) = item.node {
|
2016-10-06 03:04:30 -05:00
|
|
|
self.visit_invoc(item.id);
|
|
|
|
return
|
2016-10-04 00:36:14 -05:00
|
|
|
}
|
|
|
|
|
2016-09-14 18:39:13 -05:00
|
|
|
// Add the item to the trait info.
|
|
|
|
let item_def_id = self.resolver.definitions.local_def_id(item.id);
|
|
|
|
let mut is_static_method = false;
|
|
|
|
let (def, ns) = match item.node {
|
|
|
|
TraitItemKind::Const(..) => (Def::AssociatedConst(item_def_id), ValueNS),
|
|
|
|
TraitItemKind::Method(ref sig, _) => {
|
|
|
|
is_static_method = !sig.decl.has_self();
|
|
|
|
(Def::Method(item_def_id), ValueNS)
|
|
|
|
}
|
|
|
|
TraitItemKind::Type(..) => (Def::AssociatedTy(item_def_id), TypeNS),
|
2016-10-04 00:36:14 -05:00
|
|
|
TraitItemKind::Macro(_) => bug!(), // handled above
|
2016-09-14 18:39:13 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
self.resolver.trait_item_map.insert((item.ident.name, def_id), is_static_method);
|
|
|
|
|
|
|
|
let vis = ty::Visibility::Public;
|
2016-11-07 16:23:26 -06:00
|
|
|
self.resolver.define(parent, item.ident.name, ns, (def, vis, item.span, self.expansion));
|
2016-09-14 18:39:13 -05:00
|
|
|
|
|
|
|
self.resolver.current_module = parent.parent.unwrap(); // nearest normal ancestor
|
|
|
|
visit::walk_trait_item(self, item);
|
|
|
|
self.resolver.current_module = parent;
|
|
|
|
}
|
2014-12-30 12:16:42 -06:00
|
|
|
}
|