]> git.lizzy.rs Git - rust.git/blob - src/librustc/plugin/registry.rs
rollup merge of #18407 : thestinger/arena
[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
15 use syntax::ext::base::{SyntaxExtension, NamedSyntaxExtension, NormalTT};
16 use syntax::ext::base::{IdentTT, LetSyntaxTT, Decorator, Modifier};
17 use syntax::ext::base::{MacroExpanderFn};
18 use syntax::codemap::Span;
19 use syntax::parse::token;
20 use syntax::ast;
21
22 use std::collections::HashMap;
23
24 /// Structure used to register plugins.
25 ///
26 /// A plugin registrar function takes an `&mut Registry` and should call
27 /// methods to register its plugins.
28 ///
29 /// This struct has public fields and other methods for use by `rustc`
30 /// itself. They are not documented here, and plugin authors should
31 /// not use them.
32 pub struct Registry {
33     #[doc(hidden)]
34     pub krate_span: Span,
35
36     #[doc(hidden)]
37     pub syntax_exts: Vec<NamedSyntaxExtension>,
38
39     #[doc(hidden)]
40     pub lint_passes: Vec<LintPassObject>,
41
42     #[doc(hidden)]
43     pub lint_groups: HashMap<&'static str, Vec<LintId>>,
44 }
45
46 impl Registry {
47     #[doc(hidden)]
48     pub fn new(krate: &ast::Crate) -> Registry {
49         Registry {
50             krate_span: krate.span,
51             syntax_exts: vec!(),
52             lint_passes: vec!(),
53             lint_groups: HashMap::new(),
54         }
55     }
56
57     /// Register a syntax extension of any kind.
58     ///
59     /// This is the most general hook into `libsyntax`'s expansion behavior.
60     pub fn register_syntax_extension(&mut self, name: ast::Name, extension: SyntaxExtension) {
61         self.syntax_exts.push((name, match extension {
62             NormalTT(ext, _) => NormalTT(ext, Some(self.krate_span)),
63             IdentTT(ext, _) => IdentTT(ext, Some(self.krate_span)),
64             Decorator(ext) => Decorator(ext),
65             Modifier(ext) => Modifier(ext),
66             // there's probably a nicer way to signal this:
67             LetSyntaxTT(_, _) => panic!("can't register a new LetSyntax!"),
68         }));
69     }
70
71     /// Register a macro of the usual kind.
72     ///
73     /// This is a convenience wrapper for `register_syntax_extension`.
74     /// It builds for you a `NormalTT` that calls `expander`,
75     /// and also takes care of interning the macro's name.
76     pub fn register_macro(&mut self, name: &str, expander: MacroExpanderFn) {
77         self.register_syntax_extension(token::intern(name), NormalTT(box expander, None));
78     }
79
80     /// Register a compiler lint pass.
81     pub fn register_lint_pass(&mut self, lint_pass: LintPassObject) {
82         self.lint_passes.push(lint_pass);
83     }
84
85     /// Register a lint group.
86     pub fn register_lint_group(&mut self, name: &'static str, to: Vec<&'static Lint>) {
87         self.lint_groups.insert(name, to.into_iter().map(|x| LintId::of(x)).collect());
88     }
89 }