1 //! Used by `rustc` when loading a plugin.
3 use rustc::middle::cstore::MetadataLoader;
4 use rustc::session::Session;
5 use rustc_metadata::locator;
6 use crate::registry::Registry;
8 use std::borrow::ToOwned;
11 use std::path::PathBuf;
13 use syntax::struct_span_err;
14 use syntax::symbol::{Symbol, kw, sym};
15 use syntax_pos::{Span, DUMMY_SP};
17 /// Pointer to a registrar function.
18 pub type PluginRegistrarFun =
19 fn(&mut Registry<'_>);
21 pub struct PluginRegistrar {
22 pub fun: PluginRegistrarFun,
23 pub args: Vec<ast::NestedMetaItem>,
26 struct PluginLoader<'a> {
28 metadata_loader: &'a dyn MetadataLoader,
29 plugins: Vec<PluginRegistrar>,
32 fn call_malformed_plugin_attribute(sess: &Session, span: Span) {
33 struct_span_err!(sess, span, E0498, "malformed `plugin` attribute")
34 .span_label(span, "malformed attribute")
38 /// Read plugin metadata and dynamically load registrar functions.
39 pub fn load_plugins(sess: &Session,
40 metadata_loader: &dyn MetadataLoader,
42 addl_plugins: Option<Vec<String>>) -> Vec<PluginRegistrar> {
43 let mut loader = PluginLoader { sess, metadata_loader, plugins: Vec::new() };
45 // do not report any error now. since crate attributes are
46 // not touched by expansion, every use of plugin without
47 // the feature enabled will result in an error later...
48 if sess.features_untracked().plugin {
49 for attr in &krate.attrs {
50 if !attr.check_name(sym::plugin) {
54 let plugins = match attr.meta_item_list() {
59 for plugin in plugins {
60 // plugins must have a name and can't be key = value
61 let name = plugin.name_or_empty();
62 if name != kw::Invalid && !plugin.is_value_str() {
63 let args = plugin.meta_item_list().map(ToOwned::to_owned);
64 loader.load_plugin(plugin.span(), name, args.unwrap_or_default());
66 call_malformed_plugin_attribute(sess, attr.span);
72 if let Some(plugins) = addl_plugins {
73 for plugin in plugins {
74 loader.load_plugin(DUMMY_SP, Symbol::intern(&plugin), vec![]);
81 impl<'a> PluginLoader<'a> {
82 fn load_plugin(&mut self, span: Span, name: Symbol, args: Vec<ast::NestedMetaItem>) {
83 let registrar = locator::find_plugin_registrar(self.sess, self.metadata_loader, span, name);
85 if let Some((lib, disambiguator)) = registrar {
86 let symbol = self.sess.generate_plugin_registrar_symbol(disambiguator);
87 let fun = self.dylink_registrar(span, lib, symbol);
88 self.plugins.push(PluginRegistrar {
95 // Dynamically link a registrar function into the compiler process.
96 fn dylink_registrar(&mut self,
99 symbol: String) -> PluginRegistrarFun {
100 use rustc_metadata::dynamic_lib::DynamicLibrary;
102 // Make sure the path contains a / or the linker will search for it.
103 let path = env::current_dir().unwrap().join(&path);
105 let lib = match DynamicLibrary::open(Some(&path)) {
107 // this is fatal: there are almost certainly macros we need
108 // inside this crate, so continue would spew "macro undefined"
111 self.sess.span_fatal(span, &err)
117 match lib.symbol(&symbol) {
119 mem::transmute::<*mut u8,PluginRegistrarFun>(registrar)
121 // again fatal if we can't register macros
123 self.sess.span_fatal(span, &err)
127 // Intentionally leak the dynamic library. We can't ever unload it
128 // since the library can make things that will live arbitrarily long
129 // (e.g., an @-box cycle or a thread).