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 use rustc_error_codes::*;
19 /// Pointer to a registrar function.
20 pub type PluginRegistrarFun =
21 fn(&mut Registry<'_>);
23 pub struct PluginRegistrar {
24 pub fun: PluginRegistrarFun,
25 pub args: Vec<ast::NestedMetaItem>,
28 struct PluginLoader<'a> {
30 metadata_loader: &'a dyn MetadataLoader,
31 plugins: Vec<PluginRegistrar>,
34 fn call_malformed_plugin_attribute(sess: &Session, span: Span) {
35 struct_span_err!(sess, span, E0498, "malformed `plugin` attribute")
36 .span_label(span, "malformed attribute")
40 /// Read plugin metadata and dynamically load registrar functions.
41 pub fn load_plugins(sess: &Session,
42 metadata_loader: &dyn MetadataLoader,
44 addl_plugins: Option<Vec<String>>) -> Vec<PluginRegistrar> {
45 let mut loader = PluginLoader { sess, metadata_loader, plugins: Vec::new() };
47 // do not report any error now. since crate attributes are
48 // not touched by expansion, every use of plugin without
49 // the feature enabled will result in an error later...
50 if sess.features_untracked().plugin {
51 for attr in &krate.attrs {
52 if !attr.check_name(sym::plugin) {
56 let plugins = match attr.meta_item_list() {
61 for plugin in plugins {
62 // plugins must have a name and can't be key = value
63 let name = plugin.name_or_empty();
64 if name != kw::Invalid && !plugin.is_value_str() {
65 let args = plugin.meta_item_list().map(ToOwned::to_owned);
66 loader.load_plugin(plugin.span(), name, args.unwrap_or_default());
68 call_malformed_plugin_attribute(sess, attr.span);
74 if let Some(plugins) = addl_plugins {
75 for plugin in plugins {
76 loader.load_plugin(DUMMY_SP, Symbol::intern(&plugin), vec![]);
83 impl<'a> PluginLoader<'a> {
84 fn load_plugin(&mut self, span: Span, name: Symbol, args: Vec<ast::NestedMetaItem>) {
85 let registrar = locator::find_plugin_registrar(self.sess, self.metadata_loader, span, name);
87 if let Some((lib, disambiguator)) = registrar {
88 let symbol = self.sess.generate_plugin_registrar_symbol(disambiguator);
89 let fun = self.dylink_registrar(span, lib, symbol);
90 self.plugins.push(PluginRegistrar {
97 // Dynamically link a registrar function into the compiler process.
98 fn dylink_registrar(&mut self,
101 symbol: String) -> PluginRegistrarFun {
102 use rustc_metadata::dynamic_lib::DynamicLibrary;
104 // Make sure the path contains a / or the linker will search for it.
105 let path = env::current_dir().unwrap().join(&path);
107 let lib = match DynamicLibrary::open(Some(&path)) {
109 // this is fatal: there are almost certainly macros we need
110 // inside this crate, so continue would spew "macro undefined"
113 self.sess.span_fatal(span, &err)
119 match lib.symbol(&symbol) {
121 mem::transmute::<*mut u8,PluginRegistrarFun>(registrar)
123 // again fatal if we can't register macros
125 self.sess.span_fatal(span, &err)
129 // Intentionally leak the dynamic library. We can't ever unload it
130 // since the library can make things that will live arbitrarily long
131 // (e.g., an @-box cycle or a thread).