]> git.lizzy.rs Git - rust.git/blob - src/librustc_plugin_impl/load.rs
Rollup merge of #67059 - TommasoBianchi:dropck_fix_pr, r=pnkfelix
[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;
7
8 use std::borrow::ToOwned;
9 use std::env;
10 use std::mem;
11 use std::path::PathBuf;
12 use syntax::ast::{Crate, Ident};
13 use syntax::struct_span_err;
14 use syntax::symbol::sym;
15 use syntax_pos::Span;
16
17 use rustc_error_codes::*;
18
19 /// Pointer to a registrar function.
20 type PluginRegistrarFn = fn(&mut Registry<'_>);
21
22 fn call_malformed_plugin_attribute(sess: &Session, span: Span) {
23     struct_span_err!(sess, span, E0498, "malformed `plugin` attribute")
24         .span_label(span, "malformed attribute")
25         .emit();
26 }
27
28 /// Read plugin metadata and dynamically load registrar functions.
29 pub fn load_plugins(sess: &Session,
30                     metadata_loader: &dyn MetadataLoader,
31                     krate: &Crate) -> Vec<PluginRegistrarFn> {
32     let mut plugins = Vec::new();
33
34     for attr in &krate.attrs {
35         if !attr.check_name(sym::plugin) {
36             continue;
37         }
38
39         for plugin in attr.meta_item_list().unwrap_or_default() {
40             match plugin.ident() {
41                 Some(ident) if plugin.is_word() =>
42                     load_plugin(&mut plugins, sess, metadata_loader, ident),
43                 _ => call_malformed_plugin_attribute(sess, plugin.span()),
44             }
45         }
46     }
47
48     plugins
49 }
50
51 fn load_plugin(plugins: &mut Vec<PluginRegistrarFn>,
52                sess: &Session,
53                metadata_loader: &dyn MetadataLoader,
54                ident: Ident) {
55     let registrar = locator::find_plugin_registrar(sess, metadata_loader, ident.span, ident.name);
56
57     if let Some((lib, disambiguator)) = registrar {
58         let symbol = sess.generate_plugin_registrar_symbol(disambiguator);
59         let fun = dylink_registrar(sess, ident.span, lib, symbol);
60         plugins.push(fun);
61     }
62 }
63
64 // Dynamically link a registrar function into the compiler process.
65 fn dylink_registrar(sess: &Session,
66                     span: Span,
67                     path: PathBuf,
68                     symbol: String) -> PluginRegistrarFn {
69     use rustc_metadata::dynamic_lib::DynamicLibrary;
70
71     // Make sure the path contains a / or the linker will search for it.
72     let path = env::current_dir().unwrap().join(&path);
73
74     let lib = match DynamicLibrary::open(Some(&path)) {
75         Ok(lib) => lib,
76         // this is fatal: there are almost certainly macros we need
77         // inside this crate, so continue would spew "macro undefined"
78         // errors
79         Err(err) => {
80             sess.span_fatal(span, &err)
81         }
82     };
83
84     unsafe {
85         let registrar =
86             match lib.symbol(&symbol) {
87                 Ok(registrar) => {
88                     mem::transmute::<*mut u8, PluginRegistrarFn>(registrar)
89                 }
90                 // again fatal if we can't register macros
91                 Err(err) => {
92                     sess.span_fatal(span, &err)
93                 }
94             };
95
96         // Intentionally leak the dynamic library. We can't ever unload it
97         // since the library can make things that will live arbitrarily long
98         // (e.g., an @-box cycle or a thread).
99         mem::forget(lib);
100
101         registrar
102     }
103 }