]> git.proxmox.com Git - rustc.git/blob - src/librustc/plugin/registry.rs
12634204f8b12f2bd31c02dc36d8ccf8b973c8c1
[rustc.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, MultiModifier, 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<Vec<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's arguments, if any.
69 ///
70 /// These are specified inside the `plugin` crate attribute as
71 ///
72 /// ```no_run
73 /// #![plugin(my_plugin_name(... args ...))]
74 /// ```
75 pub fn args<'b>(&'b self) -> &'b Vec<P<ast::MetaItem>> {
76 self.args_hidden.as_ref().expect("args not set")
77 }
78
79 /// Register a syntax extension of any kind.
80 ///
81 /// This is the most general hook into `libsyntax`'s expansion behavior.
82 pub fn register_syntax_extension(&mut self, name: ast::Name, extension: SyntaxExtension) {
83 self.syntax_exts.push((name, match extension {
84 NormalTT(ext, _) => NormalTT(ext, Some(self.krate_span)),
85 IdentTT(ext, _) => IdentTT(ext, Some(self.krate_span)),
86 Decorator(ext) => Decorator(ext),
87 Modifier(ext) => Modifier(ext),
88 MultiModifier(ext) => MultiModifier(ext),
89 MacroRulesTT => {
90 self.sess.err("plugin tried to register a new MacroRulesTT");
91 return;
92 }
93 }));
94 }
95
96 /// Register a macro of the usual kind.
97 ///
98 /// This is a convenience wrapper for `register_syntax_extension`.
99 /// It builds for you a `NormalTT` that calls `expander`,
100 /// and also takes care of interning the macro's name.
101 pub fn register_macro(&mut self, name: &str, expander: MacroExpanderFn) {
102 self.register_syntax_extension(token::intern(name), NormalTT(box expander, None));
103 }
104
105 /// Register a compiler lint pass.
106 pub fn register_lint_pass(&mut self, lint_pass: LintPassObject) {
107 self.lint_passes.push(lint_pass);
108 }
109
110 /// Register a lint group.
111 pub fn register_lint_group(&mut self, name: &'static str, to: Vec<&'static Lint>) {
112 self.lint_groups.insert(name, to.into_iter().map(|x| LintId::of(x)).collect());
113 }
114 }