]> git.lizzy.rs Git - rust.git/blob - src/librustc_plugin/load.rs
Auto merge of #56462 - Zoxc:query-macro, r=oli-obk
[rust.git] / src / librustc_plugin / load.rs
1 //! Used by `rustc` when loading a plugin.
2
3 use rustc::session::Session;
4 use rustc_metadata::creader::CrateLoader;
5 use rustc_metadata::cstore::CStore;
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::span_err;
14 use syntax_pos::{Span, DUMMY_SP};
15
16 /// Pointer to a registrar function.
17 pub type PluginRegistrarFun =
18     fn(&mut Registry<'_>);
19
20 pub struct PluginRegistrar {
21     pub fun: PluginRegistrarFun,
22     pub args: Vec<ast::NestedMetaItem>,
23 }
24
25 struct PluginLoader<'a> {
26     sess: &'a Session,
27     reader: CrateLoader<'a>,
28     plugins: Vec<PluginRegistrar>,
29 }
30
31 fn call_malformed_plugin_attribute(a: &Session, b: Span) {
32     span_err!(a, b, E0498, "malformed plugin attribute");
33 }
34
35 /// Read plugin metadata and dynamically load registrar functions.
36 pub fn load_plugins(sess: &Session,
37                     cstore: &CStore,
38                     krate: &ast::Crate,
39                     crate_name: &str,
40                     addl_plugins: Option<Vec<String>>) -> Vec<PluginRegistrar> {
41     let mut loader = PluginLoader::new(sess, cstore, crate_name);
42
43     // do not report any error now. since crate attributes are
44     // not touched by expansion, every use of plugin without
45     // the feature enabled will result in an error later...
46     if sess.features_untracked().plugin {
47         for attr in &krate.attrs {
48             if !attr.check_name("plugin") {
49                 continue;
50             }
51
52             let plugins = match attr.meta_item_list() {
53                 Some(xs) => xs,
54                 None => continue,
55             };
56
57             for plugin in plugins {
58                 // plugins must have a name and can't be key = value
59                 match plugin.ident_str() {
60                     Some(name) if !plugin.is_value_str() => {
61                         let args = plugin.meta_item_list().map(ToOwned::to_owned);
62                         loader.load_plugin(plugin.span(), name, args.unwrap_or_default());
63                     },
64                     _ => call_malformed_plugin_attribute(sess, attr.span),
65                 }
66             }
67         }
68     }
69
70     if let Some(plugins) = addl_plugins {
71         for plugin in plugins {
72             loader.load_plugin(DUMMY_SP, &plugin, vec![]);
73         }
74     }
75
76     loader.plugins
77 }
78
79 impl<'a> PluginLoader<'a> {
80     fn new(sess: &'a Session, cstore: &'a CStore, crate_name: &str) -> Self {
81         PluginLoader {
82             sess,
83             reader: CrateLoader::new(sess, cstore, crate_name),
84             plugins: vec![],
85         }
86     }
87
88     fn load_plugin(&mut self, span: Span, name: &str, args: Vec<ast::NestedMetaItem>) {
89         let registrar = self.reader.find_plugin_registrar(span, name);
90
91         if let Some((lib, disambiguator)) = registrar {
92             let symbol = self.sess.generate_plugin_registrar_symbol(disambiguator);
93             let fun = self.dylink_registrar(span, lib, symbol);
94             self.plugins.push(PluginRegistrar {
95                 fun,
96                 args,
97             });
98         }
99     }
100
101     // Dynamically link a registrar function into the compiler process.
102     fn dylink_registrar(&mut self,
103                         span: Span,
104                         path: PathBuf,
105                         symbol: String) -> PluginRegistrarFun {
106         use rustc_metadata::dynamic_lib::DynamicLibrary;
107
108         // Make sure the path contains a / or the linker will search for it.
109         let path = env::current_dir().unwrap().join(&path);
110
111         let lib = match DynamicLibrary::open(Some(&path)) {
112             Ok(lib) => lib,
113             // this is fatal: there are almost certainly macros we need
114             // inside this crate, so continue would spew "macro undefined"
115             // errors
116             Err(err) => {
117                 self.sess.span_fatal(span, &err)
118             }
119         };
120
121         unsafe {
122             let registrar =
123                 match lib.symbol(&symbol) {
124                     Ok(registrar) => {
125                         mem::transmute::<*mut u8,PluginRegistrarFun>(registrar)
126                     }
127                     // again fatal if we can't register macros
128                     Err(err) => {
129                         self.sess.span_fatal(span, &err)
130                     }
131                 };
132
133             // Intentionally leak the dynamic library. We can't ever unload it
134             // since the library can make things that will live arbitrarily long
135             // (e.g., an @-box cycle or a thread).
136             mem::forget(lib);
137
138             registrar
139         }
140     }
141 }