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, MultiModifier, MultiDecorator};
18 use syntax::ext::base::{MacroExpanderFn, MacroRulesTT};
19 use syntax::codemap::Span;
20 use syntax::parse::token;
23 use syntax::feature_gate::AttributeType;
25 use std::collections::HashMap;
26 use std::borrow::ToOwned;
28 /// Structure used to register plugins.
30 /// A plugin registrar function takes an `&mut Registry` and should call
31 /// methods to register its plugins.
33 /// This struct has public fields and other methods for use by `rustc`
34 /// itself. They are not documented here, and plugin authors should
36 pub struct Registry<'a> {
37 /// Compiler session. Useful if you want to emit diagnostic messages
38 /// from the plugin registrar.
39 pub sess: &'a Session,
42 pub args_hidden: Option<Vec<P<ast::MetaItem>>>,
48 pub syntax_exts: Vec<NamedSyntaxExtension>,
51 pub lint_passes: Vec<LintPassObject>,
54 pub lint_groups: HashMap<&'static str, Vec<LintId>>,
57 pub llvm_passes: Vec<String>,
60 pub attributes: Vec<(String, AttributeType)>,
63 impl<'a> Registry<'a> {
65 pub fn new(sess: &'a Session, krate: &ast::Crate) -> Registry<'a> {
69 krate_span: krate.span,
72 lint_groups: HashMap::new(),
78 /// Get the plugin's arguments, if any.
80 /// These are specified inside the `plugin` crate attribute as
83 /// #![plugin(my_plugin_name(... args ...))]
85 pub fn args<'b>(&'b self) -> &'b Vec<P<ast::MetaItem>> {
86 self.args_hidden.as_ref().expect("args not set")
89 /// Register a syntax extension of any kind.
91 /// This is the most general hook into `libsyntax`'s expansion behavior.
93 pub fn register_syntax_extension(&mut self, name: ast::Name, extension: SyntaxExtension) {
94 self.syntax_exts.push((name, match extension {
95 NormalTT(ext, _, allow_internal_unstable) => {
96 NormalTT(ext, Some(self.krate_span), allow_internal_unstable)
98 IdentTT(ext, _, allow_internal_unstable) => {
99 IdentTT(ext, Some(self.krate_span), allow_internal_unstable)
101 MultiDecorator(ext) => MultiDecorator(ext),
102 MultiModifier(ext) => MultiModifier(ext),
104 self.sess.err("plugin tried to register a new MacroRulesTT");
110 /// Register a macro of the usual kind.
112 /// This is a convenience wrapper for `register_syntax_extension`.
113 /// It builds for you a `NormalTT` that calls `expander`,
114 /// and also takes care of interning the macro's name.
115 pub fn register_macro(&mut self, name: &str, expander: MacroExpanderFn) {
116 self.register_syntax_extension(token::intern(name),
117 NormalTT(Box::new(expander), None, false));
120 /// Register a compiler lint pass.
121 pub fn register_lint_pass(&mut self, lint_pass: LintPassObject) {
122 self.lint_passes.push(lint_pass);
125 /// Register a lint group.
126 pub fn register_lint_group(&mut self, name: &'static str, to: Vec<&'static Lint>) {
127 self.lint_groups.insert(name, to.into_iter().map(|x| LintId::of(x)).collect());
130 /// Register an LLVM pass.
132 /// Registration with LLVM itself is handled through static C++ objects with
133 /// constructors. This method simply adds a name to the list of passes to
135 pub fn register_llvm_pass(&mut self, name: &str) {
136 self.llvm_passes.push(name.to_owned());
140 /// Register an attribute with an attribute type.
142 /// Registered attributes will bypass the `custom_attribute` feature gate.
143 /// `Whitelisted` attributes will additionally not trigger the `unused_attribute`
144 /// lint. `CrateLevel` attributes will not be allowed on anything other than a crate.
145 pub fn register_attribute(&mut self, name: String, ty: AttributeType) {
146 self.attributes.push((name, ty));