]> git.lizzy.rs Git - rust.git/blob - src/librustc/plugin/load.rs
End of adding error codes in librustc
[rust.git] / src / librustc / plugin / load.rs
1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Used by `rustc` when loading a plugin.
12
13 use session::Session;
14 use metadata::creader::CrateReader;
15 use plugin::registry::Registry;
16
17 use std::borrow::ToOwned;
18 use std::dynamic_lib::DynamicLibrary;
19 use std::env;
20 use std::mem;
21 use std::path::PathBuf;
22 use syntax::ast;
23 use syntax::codemap::{Span, COMMAND_LINE_SP};
24 use syntax::ptr::P;
25 use syntax::attr::AttrMetaMethods;
26
27 /// Pointer to a registrar function.
28 pub type PluginRegistrarFun =
29     fn(&mut Registry);
30
31 pub struct PluginRegistrar {
32     pub fun: PluginRegistrarFun,
33     pub args: Vec<P<ast::MetaItem>>,
34 }
35
36 struct PluginLoader<'a> {
37     sess: &'a Session,
38     reader: CrateReader<'a>,
39     plugins: Vec<PluginRegistrar>,
40 }
41
42 fn call_malformed_plugin_attribute(a: &Session, b: Span) {
43     span_err!(a, b, E0498, "malformed plugin attribute");
44 }
45
46 /// Read plugin metadata and dynamically load registrar functions.
47 pub fn load_plugins(sess: &Session, krate: &ast::Crate,
48                     addl_plugins: Option<Vec<String>>) -> Vec<PluginRegistrar> {
49     let mut loader = PluginLoader::new(sess);
50
51     for attr in &krate.attrs {
52         if !attr.check_name("plugin") {
53             continue;
54         }
55
56         let plugins = match attr.meta_item_list() {
57             Some(xs) => xs,
58             None => {
59                 call_malformed_plugin_attribute(sess, attr.span);
60                 continue;
61             }
62         };
63
64         for plugin in plugins {
65             if plugin.value_str().is_some() {
66                 call_malformed_plugin_attribute(sess, attr.span);
67                 continue;
68             }
69
70             let args = plugin.meta_item_list().map(ToOwned::to_owned).unwrap_or_default();
71             loader.load_plugin(plugin.span, &*plugin.name(), args);
72         }
73     }
74
75     if let Some(plugins) = addl_plugins {
76         for plugin in plugins {
77             loader.load_plugin(COMMAND_LINE_SP, &plugin, vec![]);
78         }
79     }
80
81     loader.plugins
82 }
83
84 impl<'a> PluginLoader<'a> {
85     fn new(sess: &'a Session) -> PluginLoader<'a> {
86         PluginLoader {
87             sess: sess,
88             reader: CrateReader::new(sess),
89             plugins: vec![],
90         }
91     }
92
93     fn load_plugin(&mut self, span: Span, name: &str, args: Vec<P<ast::MetaItem>>) {
94         let registrar = self.reader.find_plugin_registrar(span, name);
95
96         if let Some((lib, symbol)) = registrar {
97             let fun = self.dylink_registrar(span, lib, symbol);
98             self.plugins.push(PluginRegistrar {
99                 fun: fun,
100                 args: args,
101             });
102         }
103     }
104
105     // Dynamically link a registrar function into the compiler process.
106     fn dylink_registrar(&mut self,
107                         span: Span,
108                         path: PathBuf,
109                         symbol: String) -> PluginRegistrarFun {
110         // Make sure the path contains a / or the linker will search for it.
111         let path = env::current_dir().unwrap().join(&path);
112
113         let lib = match DynamicLibrary::open(Some(&path)) {
114             Ok(lib) => lib,
115             // this is fatal: there are almost certainly macros we need
116             // inside this crate, so continue would spew "macro undefined"
117             // errors
118             Err(err) => {
119                 self.sess.span_fatal(span, &err[..])
120             }
121         };
122
123         unsafe {
124             let registrar =
125                 match lib.symbol(&symbol[..]) {
126                     Ok(registrar) => {
127                         mem::transmute::<*mut u8,PluginRegistrarFun>(registrar)
128                     }
129                     // again fatal if we can't register macros
130                     Err(err) => {
131                         self.sess.span_fatal(span, &err[..])
132                     }
133                 };
134
135             // Intentionally leak the dynamic library. We can't ever unload it
136             // since the library can make things that will live arbitrarily long
137             // (e.g. an @-box cycle or a thread).
138             mem::forget(lib);
139
140             registrar
141         }
142     }
143 }