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