]> git.lizzy.rs Git - rust.git/blob - src/librustc_plugin_impl/registry.rs
Auto merge of #66571 - Centril:rollup-41tn2fw, r=Centril
[rust.git] / src / librustc_plugin_impl / registry.rs
1 //! Used by plugin crates to tell `rustc` about the plugins they provide.
2
3 use rustc::lint::LintStore;
4 use rustc::session::Session;
5
6 use syntax_expand::base::{SyntaxExtension, SyntaxExtensionKind, NamedSyntaxExtension};
7 use syntax_expand::base::MacroExpanderFn;
8 use syntax::symbol::Symbol;
9 use syntax::ast;
10 use syntax_pos::Span;
11
12 use std::borrow::ToOwned;
13
14 /// Structure used to register plugins.
15 ///
16 /// A plugin registrar function takes an `&mut Registry` and should call
17 /// methods to register its plugins.
18 ///
19 /// This struct has public fields and other methods for use by `rustc`
20 /// itself. They are not documented here, and plugin authors should
21 /// not use them.
22 pub struct Registry<'a> {
23     /// Compiler session. Useful if you want to emit diagnostic messages
24     /// from the plugin registrar.
25     pub sess: &'a Session,
26
27     /// The `LintStore` allows plugins to register new lints.
28     pub lint_store: &'a mut LintStore,
29
30     #[doc(hidden)]
31     pub args_hidden: Option<Vec<ast::NestedMetaItem>>,
32
33     #[doc(hidden)]
34     pub krate_span: Span,
35
36     #[doc(hidden)]
37     pub syntax_exts: Vec<NamedSyntaxExtension>,
38
39     #[doc(hidden)]
40     pub llvm_passes: Vec<String>,
41 }
42
43 impl<'a> Registry<'a> {
44     #[doc(hidden)]
45     pub fn new(sess: &'a Session, lint_store: &'a mut LintStore, krate_span: Span) -> Registry<'a> {
46         Registry {
47             sess,
48             lint_store,
49             args_hidden: None,
50             krate_span,
51             syntax_exts: vec![],
52             llvm_passes: vec![],
53         }
54     }
55
56     /// Gets the plugin's arguments, if any.
57     ///
58     /// These are specified inside the `plugin` crate attribute as
59     ///
60     /// ```no_run
61     /// #![plugin(my_plugin_name(... args ...))]
62     /// ```
63     ///
64     /// Returns empty slice in case the plugin was loaded
65     /// with `--extra-plugins`
66     pub fn args(&self) -> &[ast::NestedMetaItem] {
67         self.args_hidden.as_ref().map(|v| &v[..]).unwrap_or(&[])
68     }
69
70     /// Register a syntax extension of any kind.
71     ///
72     /// This is the most general hook into `libsyntax`'s expansion behavior.
73     pub fn register_syntax_extension(&mut self, name: ast::Name, extension: SyntaxExtension) {
74         self.syntax_exts.push((name, extension));
75     }
76
77     /// Register a macro of the usual kind.
78     ///
79     /// This is a convenience wrapper for `register_syntax_extension`.
80     /// It builds for you a `SyntaxExtensionKind::LegacyBang` that calls `expander`,
81     /// and also takes care of interning the macro's name.
82     pub fn register_macro(&mut self, name: &str, expander: MacroExpanderFn) {
83         let kind = SyntaxExtensionKind::LegacyBang(Box::new(expander));
84         let ext = SyntaxExtension::default(kind, self.sess.edition());
85         self.register_syntax_extension(Symbol::intern(name), ext);
86     }
87
88     /// Register an LLVM pass.
89     ///
90     /// Registration with LLVM itself is handled through static C++ objects with
91     /// constructors. This method simply adds a name to the list of passes to
92     /// execute.
93     pub fn register_llvm_pass(&mut self, name: &str) {
94         self.llvm_passes.push(name.to_owned());
95     }
96 }