1 //! Used by plugin crates to tell `rustc` about the plugins they provide.
3 use rustc::lint::LintStore;
4 use rustc::session::Session;
6 use syntax_expand::base::{SyntaxExtension, SyntaxExtensionKind, NamedSyntaxExtension};
7 use syntax_expand::base::MacroExpanderFn;
8 use syntax::symbol::Symbol;
12 use std::borrow::ToOwned;
14 /// Structure used to register plugins.
16 /// A plugin registrar function takes an `&mut Registry` and should call
17 /// methods to register its plugins.
19 /// This struct has public fields and other methods for use by `rustc`
20 /// itself. They are not documented here, and plugin authors should
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,
27 /// The `LintStore` allows plugins to register new lints.
28 pub lint_store: &'a mut LintStore,
31 pub args_hidden: Option<Vec<ast::NestedMetaItem>>,
37 pub syntax_exts: Vec<NamedSyntaxExtension>,
40 pub llvm_passes: Vec<String>,
43 impl<'a> Registry<'a> {
45 pub fn new(sess: &'a Session, lint_store: &'a mut LintStore, krate_span: Span) -> Registry<'a> {
56 /// Gets the plugin's arguments, if any.
58 /// These are specified inside the `plugin` crate attribute as
61 /// #![plugin(my_plugin_name(... args ...))]
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(&[])
70 /// Register a syntax extension of any kind.
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));
77 /// Register a macro of the usual kind.
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);
88 /// Register an LLVM pass.
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
93 pub fn register_llvm_pass(&mut self, name: &str) {
94 self.llvm_passes.push(name.to_owned());