]> git.lizzy.rs Git - rust.git/blob - src/librustc_plugin_impl/load.rs
Rollup merge of #66705 - pitdicker:atomic_mut_ptr, r=KodrAus
[rust.git] / src / librustc_plugin_impl / load.rs
1 //! Used by `rustc` when loading a plugin.
2
3 use rustc::middle::cstore::MetadataLoader;
4 use rustc::session::Session;
5 use rustc_metadata::locator;
6 use crate::registry::Registry;
7
8 use std::borrow::ToOwned;
9 use std::env;
10 use std::mem;
11 use std::path::PathBuf;
12 use syntax::ast;
13 use syntax::struct_span_err;
14 use syntax::symbol::{Symbol, kw, sym};
15 use syntax_pos::{Span, DUMMY_SP};
16
17 use rustc_error_codes::*;
18
19 /// Pointer to a registrar function.
20 pub type PluginRegistrarFun =
21     fn(&mut Registry<'_>);
22
23 pub struct PluginRegistrar {
24     pub fun: PluginRegistrarFun,
25     pub args: Vec<ast::NestedMetaItem>,
26 }
27
28 struct PluginLoader<'a> {
29     sess: &'a Session,
30     metadata_loader: &'a dyn MetadataLoader,
31     plugins: Vec<PluginRegistrar>,
32 }
33
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")
37         .emit();
38 }
39
40 /// Read plugin metadata and dynamically load registrar functions.
41 pub fn load_plugins(sess: &Session,
42                     metadata_loader: &dyn MetadataLoader,
43                     krate: &ast::Crate,
44                     addl_plugins: Option<Vec<String>>) -> Vec<PluginRegistrar> {
45     let mut loader = PluginLoader { sess, metadata_loader, plugins: Vec::new() };
46
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) {
53                 continue;
54             }
55
56             let plugins = match attr.meta_item_list() {
57                 Some(xs) => xs,
58                 None => continue,
59             };
60
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());
67                 } else {
68                     call_malformed_plugin_attribute(sess, attr.span);
69                 }
70             }
71         }
72     }
73
74     if let Some(plugins) = addl_plugins {
75         for plugin in plugins {
76             loader.load_plugin(DUMMY_SP, Symbol::intern(&plugin), vec![]);
77         }
78     }
79
80     loader.plugins
81 }
82
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);
86
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 {
91                 fun,
92                 args,
93             });
94         }
95     }
96
97     // Dynamically link a registrar function into the compiler process.
98     fn dylink_registrar(&mut self,
99                         span: Span,
100                         path: PathBuf,
101                         symbol: String) -> PluginRegistrarFun {
102         use rustc_metadata::dynamic_lib::DynamicLibrary;
103
104         // Make sure the path contains a / or the linker will search for it.
105         let path = env::current_dir().unwrap().join(&path);
106
107         let lib = match DynamicLibrary::open(Some(&path)) {
108             Ok(lib) => lib,
109             // this is fatal: there are almost certainly macros we need
110             // inside this crate, so continue would spew "macro undefined"
111             // errors
112             Err(err) => {
113                 self.sess.span_fatal(span, &err)
114             }
115         };
116
117         unsafe {
118             let registrar =
119                 match lib.symbol(&symbol) {
120                     Ok(registrar) => {
121                         mem::transmute::<*mut u8,PluginRegistrarFun>(registrar)
122                     }
123                     // again fatal if we can't register macros
124                     Err(err) => {
125                         self.sess.span_fatal(span, &err)
126                     }
127                 };
128
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).
132             mem::forget(lib);
133
134             registrar
135         }
136     }
137 }