From f314e2c4ea48c2027e627fdfca821bb6e0012e59 Mon Sep 17 00:00:00 2001 From: Keegan McAllister Date: Wed, 31 Dec 2014 19:48:39 -0800 Subject: creader: Load parts of plugin metadata on demand --- src/librustc/plugin/load.rs | 59 +++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 32 deletions(-) (limited to 'src/librustc/plugin') diff --git a/src/librustc/plugin/load.rs b/src/librustc/plugin/load.rs index 9aa7d8fe990..78730defc7f 100644 --- a/src/librustc/plugin/load.rs +++ b/src/librustc/plugin/load.rs @@ -23,14 +23,6 @@ use syntax::visit; use syntax::visit::Visitor; use syntax::attr::AttrMetaMethods; -/// Metadata for a single plugin crate. -pub struct PluginMetadata { - /// Macros exported by the crate. - pub macros: Vec, - /// Path to the shared library file, and registrar function symbol. - pub registrar: Option<(Path, String)>, -} - /// Pointer to a registrar function. pub type PluginRegistrarFun = fn(&mut Registry); @@ -86,37 +78,40 @@ pub fn load_plugins(sess: &Session, krate: &ast::Crate, // note that macros aren't expanded yet, and therefore macros can't add plugins. impl<'a, 'v> Visitor<'v> for PluginLoader<'a> { fn visit_view_item(&mut self, vi: &ast::ViewItem) { + // We're only interested in `extern crate`. match vi.node { - ast::ViewItemExternCrate(_, _, _) => { - let mut plugin_phase = false; - - for attr in vi.attrs.iter().filter(|a| a.check_name("phase")) { - let phases = attr.meta_item_list().unwrap_or(&[]); - if attr::contains_name(phases, "plugin") { - plugin_phase = true; - } - if attr::contains_name(phases, "syntax") { - plugin_phase = true; - self.sess.span_warn(attr.span, - "phase(syntax) is a deprecated synonym for phase(plugin)"); - } - } + ast::ViewItemExternCrate(..) => (), + _ => return, + } - if !plugin_phase { return; } + let mut plugin_phase = false; + for attr in vi.attrs.iter().filter(|a| a.check_name("phase")) { + let phases = attr.meta_item_list().unwrap_or(&[]); + if attr::contains_name(phases, "plugin") { + plugin_phase = true; + } + if attr::contains_name(phases, "syntax") { + plugin_phase = true; + self.sess.span_warn(attr.span, + "phase(syntax) is a deprecated synonym for phase(plugin)"); + } + } - let PluginMetadata { macros, registrar } = - self.reader.read_plugin_metadata(vi); + let mut macros = vec![]; + let mut registrar = None; - self.plugins.macros.extend(macros.into_iter()); + if plugin_phase { + let pmd = self.reader.read_plugin_metadata(vi); + macros = pmd.exported_macros(); + registrar = pmd.plugin_registrar(); + } - match registrar { - Some((lib, symbol)) => self.dylink_registrar(vi, lib, symbol), - _ => (), - } - } - _ => (), + self.plugins.macros.extend(macros.into_iter()); + if let Some((lib, symbol)) = registrar { + self.dylink_registrar(vi, lib, symbol); } } + fn visit_mac(&mut self, _: &ast::Mac) { // bummer... can't see plugins inside macros. // do nothing. -- cgit 1.4.1-3-g733a5