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