]> git.lizzy.rs Git - rust.git/blob - src/librustc/plugin/registry.rs
mk: The beta channel produces things called 'beta'
[rust.git] / src / librustc / plugin / registry.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 plugin crates to tell `rustc` about the plugins they provide.
12
13 use lint::{LintPassObject, LintId, Lint};
14 use session::Session;
15
16 use syntax::ext::base::{SyntaxExtension, NamedSyntaxExtension, NormalTT};
17 use syntax::ext::base::{IdentTT, Decorator, Modifier, MacroRulesTT};
18 use syntax::ext::base::{MacroExpanderFn};
19 use syntax::codemap::Span;
20 use syntax::parse::token;
21 use syntax::ptr::P;
22 use syntax::ast;
23
24 use std::collections::HashMap;
25
26 /// Structure used to register plugins.
27 ///
28 /// A plugin registrar function takes an `&mut Registry` and should call
29 /// methods to register its plugins.
30 ///
31 /// This struct has public fields and other methods for use by `rustc`
32 /// itself. They are not documented here, and plugin authors should
33 /// not use them.
34 pub struct Registry<'a> {
35     /// Compiler session. Useful if you want to emit diagnostic messages
36     /// from the plugin registrar.
37     pub sess: &'a Session,
38
39     #[doc(hidden)]
40     pub args_hidden: Option<P<ast::MetaItem>>,
41
42     #[doc(hidden)]
43     pub krate_span: Span,
44
45     #[doc(hidden)]
46     pub syntax_exts: Vec<NamedSyntaxExtension>,
47
48     #[doc(hidden)]
49     pub lint_passes: Vec<LintPassObject>,
50
51     #[doc(hidden)]
52     pub lint_groups: HashMap<&'static str, Vec<LintId>>,
53 }
54
55 impl<'a> Registry<'a> {
56     #[doc(hidden)]
57     pub fn new(sess: &'a Session, krate: &ast::Crate) -> Registry<'a> {
58         Registry {
59             sess: sess,
60             args_hidden: None,
61             krate_span: krate.span,
62             syntax_exts: vec!(),
63             lint_passes: vec!(),
64             lint_groups: HashMap::new(),
65         }
66     }
67
68     /// Get the `#[plugin]` attribute used to load this plugin.
69     ///
70     /// This gives access to arguments passed via `#[plugin=...]` or
71     /// `#[plugin(...)]`.
72     pub fn args<'b>(&'b self) -> &'b P<ast::MetaItem> {
73         self.args_hidden.as_ref().expect("args not set")
74     }
75
76     /// Register a syntax extension of any kind.
77     ///
78     /// This is the most general hook into `libsyntax`'s expansion behavior.
79     pub fn register_syntax_extension(&mut self, name: ast::Name, extension: SyntaxExtension) {
80         self.syntax_exts.push((name, match extension {
81             NormalTT(ext, _) => NormalTT(ext, Some(self.krate_span)),
82             IdentTT(ext, _) => IdentTT(ext, Some(self.krate_span)),
83             Decorator(ext) => Decorator(ext),
84             Modifier(ext) => Modifier(ext),
85
86             MacroRulesTT => {
87                 self.sess.err("plugin tried to register a new MacroRulesTT");
88                 return;
89             }
90         }));
91     }
92
93     /// Register a macro of the usual kind.
94     ///
95     /// This is a convenience wrapper for `register_syntax_extension`.
96     /// It builds for you a `NormalTT` that calls `expander`,
97     /// and also takes care of interning the macro's name.
98     pub fn register_macro(&mut self, name: &str, expander: MacroExpanderFn) {
99         self.register_syntax_extension(token::intern(name), NormalTT(box expander, None));
100     }
101
102     /// Register a compiler lint pass.
103     pub fn register_lint_pass(&mut self, lint_pass: LintPassObject) {
104         self.lint_passes.push(lint_pass);
105     }
106
107     /// Register a lint group.
108     pub fn register_lint_group(&mut self, name: &'static str, to: Vec<&'static Lint>) {
109         self.lint_groups.insert(name, to.into_iter().map(|x| LintId::of(x)).collect());
110     }
111 }