Refactor away CrateLoader::load_macros.

This commit is contained in:
Jeffrey Seyfried 2016-10-19 19:35:32 +00:00
parent 4968600740
commit 9c514a1cc1
4 changed files with 23 additions and 33 deletions

View File

@ -423,7 +423,7 @@ pub enum LoadedMacros {
} }
pub trait CrateLoader { pub trait CrateLoader {
fn load_macros(&mut self, extern_crate: &ast::Item) -> LoadedMacros; fn process_item(&mut self, item: &ast::Item, defs: &Definitions, load_macros: bool)
fn process_item(&mut self, item: &ast::Item, defs: &Definitions); -> Option<LoadedMacros>;
fn postprocess(&mut self, krate: &ast::Crate); fn postprocess(&mut self, krate: &ast::Crate);
} }

View File

@ -23,7 +23,7 @@ use rustc::session::search_paths::PathKind;
use rustc::middle; use rustc::middle;
use rustc::middle::cstore::{CrateStore, validate_crate_name, ExternCrate}; use rustc::middle::cstore::{CrateStore, validate_crate_name, ExternCrate};
use rustc::util::nodemap::{FnvHashMap, FnvHashSet}; use rustc::util::nodemap::{FnvHashMap, FnvHashSet};
use rustc::hir::map as hir_map; use rustc::hir::map::Definitions;
use std::cell::{RefCell, Cell}; use std::cell::{RefCell, Cell};
use std::ops::Deref; use std::ops::Deref;
@ -631,8 +631,6 @@ impl<'a> CrateLoader<'a> {
use rustc_back::dynamic_lib::DynamicLibrary; use rustc_back::dynamic_lib::DynamicLibrary;
use syntax_ext::deriving::custom::CustomDerive; use syntax_ext::deriving::custom::CustomDerive;
self.cstore.add_used_for_derive_macros(item);
// Make sure the path contains a / or the linker will search for it. // Make sure the path contains a / or the linker will search for it.
let path = env::current_dir().unwrap().join(path); let path = env::current_dir().unwrap().join(path);
let lib = match DynamicLibrary::open(Some(&path)) { let lib = match DynamicLibrary::open(Some(&path)) {
@ -1020,13 +1018,19 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
self.register_statically_included_foreign_items(); self.register_statically_included_foreign_items();
} }
fn process_item(&mut self, item: &ast::Item, definitions: &hir_map::Definitions) { fn process_item(&mut self, item: &ast::Item, definitions: &Definitions, load_macros: bool)
-> Option<LoadedMacros> {
match item.node { match item.node {
ast::ItemKind::ExternCrate(_) => {} ast::ItemKind::ExternCrate(_) => {}
ast::ItemKind::ForeignMod(ref fm) => return self.process_foreign_mod(item, fm), ast::ItemKind::ForeignMod(ref fm) => {
_ => return, self.process_foreign_mod(item, fm);
return None;
}
_ => return None,
} }
let loaded_macros = if load_macros { Some(self.read_macros(item)) } else { None };
// If this `extern crate` item has `#[macro_use]` then we can safely skip it. // If this `extern crate` item has `#[macro_use]` then we can safely skip it.
// These annotations were processed during macro expansion and are already loaded // These annotations were processed during macro expansion and are already loaded
// (if necessary) into our crate store. // (if necessary) into our crate store.
@ -1034,15 +1038,13 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
// Note that it's important we *don't* fall through below as some `#[macro_use]` // Note that it's important we *don't* fall through below as some `#[macro_use]`
// crates are explicitly not linked (e.g. macro crates) so we want to ensure // crates are explicitly not linked (e.g. macro crates) so we want to ensure
// we avoid `resolve_crate` with those. // we avoid `resolve_crate` with those.
if attr::contains_name(&item.attrs, "macro_use") { if let Some(LoadedMacros::CustomDerives(..)) = loaded_macros {
if self.cstore.was_used_for_derive_macros(item) { return loaded_macros;
return
}
} }
if let Some(info) = self.extract_crate_info(item) { if let Some(info) = self.extract_crate_info(item) {
if !info.should_link { if !info.should_link {
return; return loaded_macros;
} }
let (cnum, ..) = self.resolve_crate( let (cnum, ..) = self.resolve_crate(
@ -1058,9 +1060,7 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
self.cstore.add_extern_mod_stmt_cnum(info.id, cnum); self.cstore.add_extern_mod_stmt_cnum(info.id, cnum);
} }
}
fn load_macros(&mut self, extern_crate: &ast::Item) -> LoadedMacros { loaded_macros
self.read_macros(extern_crate)
} }
} }

View File

@ -21,14 +21,13 @@ use rustc::hir::svh::Svh;
use rustc::middle::cstore::ExternCrate; use rustc::middle::cstore::ExternCrate;
use rustc_back::PanicStrategy; use rustc_back::PanicStrategy;
use rustc_data_structures::indexed_vec::IndexVec; use rustc_data_structures::indexed_vec::IndexVec;
use rustc::util::nodemap::{FnvHashMap, NodeMap, NodeSet, DefIdMap, FnvHashSet}; use rustc::util::nodemap::{FnvHashMap, NodeMap, NodeSet, DefIdMap};
use std::cell::{RefCell, Cell}; use std::cell::{RefCell, Cell};
use std::rc::Rc; use std::rc::Rc;
use std::path::PathBuf; use std::path::PathBuf;
use flate::Bytes; use flate::Bytes;
use syntax::ast::{self, Ident}; use syntax::{ast, attr};
use syntax::attr;
use syntax_pos; use syntax_pos;
pub use rustc::middle::cstore::{NativeLibraryKind, LinkagePreference}; pub use rustc::middle::cstore::{NativeLibraryKind, LinkagePreference};
@ -105,7 +104,6 @@ pub struct CStore {
pub inlined_item_cache: RefCell<DefIdMap<Option<CachedInlinedItem>>>, pub inlined_item_cache: RefCell<DefIdMap<Option<CachedInlinedItem>>>,
pub defid_for_inlined_node: RefCell<NodeMap<DefId>>, pub defid_for_inlined_node: RefCell<NodeMap<DefId>>,
pub visible_parent_map: RefCell<DefIdMap<DefId>>, pub visible_parent_map: RefCell<DefIdMap<DefId>>,
pub used_for_derive_macro: RefCell<FnvHashSet<Ident>>,
} }
impl CStore { impl CStore {
@ -121,7 +119,6 @@ impl CStore {
visible_parent_map: RefCell::new(FnvHashMap()), visible_parent_map: RefCell::new(FnvHashMap()),
inlined_item_cache: RefCell::new(FnvHashMap()), inlined_item_cache: RefCell::new(FnvHashMap()),
defid_for_inlined_node: RefCell::new(FnvHashMap()), defid_for_inlined_node: RefCell::new(FnvHashMap()),
used_for_derive_macro: RefCell::new(FnvHashSet()),
} }
} }
@ -277,14 +274,6 @@ impl CStore {
{ {
self.extern_mod_crate_map.borrow().get(&emod_id).cloned() self.extern_mod_crate_map.borrow().get(&emod_id).cloned()
} }
pub fn was_used_for_derive_macros(&self, i: &ast::Item) -> bool {
self.used_for_derive_macro.borrow().contains(&i.ident)
}
pub fn add_used_for_derive_macros(&self, i: &ast::Item) {
self.used_for_derive_macro.borrow_mut().insert(i.ident);
}
} }
impl CrateMetadata { impl CrateMetadata {

View File

@ -215,12 +215,11 @@ impl<'b> Resolver<'b> {
} }
let loaded_macros = if legacy_imports != LegacyMacroImports::default() { let loaded_macros = if legacy_imports != LegacyMacroImports::default() {
Some(self.crate_loader.load_macros(item)) self.crate_loader.process_item(item, &self.definitions, true)
} else { } else {
None self.crate_loader.process_item(item, &self.definitions, false)
}; };
self.crate_loader.process_item(item, &self.definitions);
// n.b. we don't need to look at the path option here, because cstore already did // n.b. we don't need to look at the path option here, because cstore already did
let crate_id = self.session.cstore.extern_mod_stmt_cnum(item.id); let crate_id = self.session.cstore.extern_mod_stmt_cnum(item.id);
let module = if let Some(crate_id) = crate_id { let module = if let Some(crate_id) = crate_id {
@ -270,7 +269,9 @@ impl<'b> Resolver<'b> {
self.current_module = module; self.current_module = module;
} }
ItemKind::ForeignMod(..) => self.crate_loader.process_item(item, &self.definitions), ItemKind::ForeignMod(..) => {
self.crate_loader.process_item(item, &self.definitions, false);
}
// These items live in the value namespace. // These items live in the value namespace.
ItemKind::Static(_, m, _) => { ItemKind::Static(_, m, _) => {