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.
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.
11 //! Used by plugin crates to tell `rustc` about the plugins they provide.
13 use lint::{LintPassObject, LintId, Lint};
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;
24 use std::collections::HashMap;
26 /// Structure used to register plugins.
28 /// A plugin registrar function takes an `&mut Registry` and should call
29 /// methods to register its plugins.
31 /// This struct has public fields and other methods for use by `rustc`
32 /// itself. They are not documented here, and plugin authors should
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,
40 pub args_hidden: Option<P<ast::MetaItem>>,
46 pub syntax_exts: Vec<NamedSyntaxExtension>,
49 pub lint_passes: Vec<LintPassObject>,
52 pub lint_groups: HashMap<&'static str, Vec<LintId>>,
55 impl<'a> Registry<'a> {
57 pub fn new(sess: &'a Session, krate: &ast::Crate) -> Registry<'a> {
61 krate_span: krate.span,
64 lint_groups: HashMap::new(),
68 /// Get the `#[plugin]` attribute used to load this plugin.
70 /// This gives access to arguments passed via `#[plugin=...]` or
72 pub fn args<'b>(&'b self) -> &'b P<ast::MetaItem> {
73 self.args_hidden.as_ref().expect("args not set")
76 /// Register a syntax extension of any kind.
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),
87 self.sess.err("plugin tried to register a new MacroRulesTT");
93 /// Register a macro of the usual kind.
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));
102 /// Register a compiler lint pass.
103 pub fn register_lint_pass(&mut self, lint_pass: LintPassObject) {
104 self.lint_passes.push(lint_pass);
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());