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 rustc::lint::{EarlyLintPassObject, LateLintPassObject, LintId, Lint};
14 use rustc::session::Session;
16 use syntax::ext::base::{SyntaxExtension, NamedSyntaxExtension, NormalTT, IdentTT};
17 use syntax::ext::base::MacroExpanderFn;
18 use syntax::ext::hygiene;
19 use syntax::symbol::Symbol;
21 use syntax::feature_gate::AttributeType;
24 use std::collections::HashMap;
25 use std::borrow::ToOwned;
27 /// Structure used to register plugins.
29 /// A plugin registrar function takes an `&mut Registry` and should call
30 /// methods to register its plugins.
32 /// This struct has public fields and other methods for use by `rustc`
33 /// itself. They are not documented here, and plugin authors should
35 pub struct Registry<'a> {
36 /// Compiler session. Useful if you want to emit diagnostic messages
37 /// from the plugin registrar.
38 pub sess: &'a Session,
41 pub args_hidden: Option<Vec<ast::NestedMetaItem>>,
47 pub syntax_exts: Vec<NamedSyntaxExtension>,
50 pub early_lint_passes: Vec<EarlyLintPassObject>,
53 pub late_lint_passes: Vec<LateLintPassObject>,
56 pub lint_groups: HashMap<&'static str, Vec<LintId>>,
59 pub llvm_passes: Vec<String>,
62 pub attributes: Vec<(String, AttributeType)>,
64 whitelisted_custom_derives: Vec<ast::Name>,
67 impl<'a> Registry<'a> {
69 pub fn new(sess: &'a Session, krate_span: Span) -> Registry<'a> {
75 early_lint_passes: vec![],
76 late_lint_passes: vec![],
77 lint_groups: HashMap::new(),
80 whitelisted_custom_derives: Vec::new(),
84 /// Get the plugin's arguments, if any.
86 /// These are specified inside the `plugin` crate attribute as
89 /// #![plugin(my_plugin_name(... args ...))]
92 /// Returns empty slice in case the plugin was loaded
93 /// with `--extra-plugins`
94 pub fn args<'b>(&'b self) -> &'b [ast::NestedMetaItem] {
95 self.args_hidden.as_ref().map(|v| &v[..]).unwrap_or(&[])
98 /// Register a syntax extension of any kind.
100 /// This is the most general hook into `libsyntax`'s expansion behavior.
101 pub fn register_syntax_extension(&mut self, name: ast::Name, extension: SyntaxExtension) {
102 if name == "macro_rules" {
103 panic!("user-defined macros may not be named `macro_rules`");
105 self.syntax_exts.push((name, match extension {
109 allow_internal_unstable,
110 allow_internal_unsafe,
115 let nid = ast::CRATE_NODE_ID;
118 def_info: Some((nid, self.krate_span)),
119 allow_internal_unstable,
120 allow_internal_unsafe,
126 IdentTT(ext, _, allow_internal_unstable) => {
127 IdentTT(ext, Some(self.krate_span), allow_internal_unstable)
133 /// This can be used in place of `register_syntax_extension` to register legacy custom derives
134 /// (i.e. attribute syntax extensions whose name begins with `derive_`). Legacy custom
135 /// derives defined by this function do not trigger deprecation warnings when used.
136 pub fn register_custom_derive(&mut self, name: ast::Name, extension: SyntaxExtension) {
137 assert!(name.as_str().starts_with("derive_"));
138 self.whitelisted_custom_derives.push(name);
139 self.register_syntax_extension(name, extension);
142 pub fn take_whitelisted_custom_derives(&mut self) -> Vec<ast::Name> {
143 ::std::mem::replace(&mut self.whitelisted_custom_derives, Vec::new())
146 /// Register a macro of the usual kind.
148 /// This is a convenience wrapper for `register_syntax_extension`.
149 /// It builds for you a `NormalTT` that calls `expander`,
150 /// and also takes care of interning the macro's name.
151 pub fn register_macro(&mut self, name: &str, expander: MacroExpanderFn) {
152 self.register_syntax_extension(Symbol::intern(name), NormalTT {
153 expander: Box::new(expander),
155 allow_internal_unstable: false,
156 allow_internal_unsafe: false,
157 local_inner_macros: false,
158 unstable_feature: None,
159 edition: hygiene::default_edition(),
163 /// Register a compiler lint pass.
164 pub fn register_early_lint_pass(&mut self, lint_pass: EarlyLintPassObject) {
165 self.early_lint_passes.push(lint_pass);
168 /// Register a compiler lint pass.
169 pub fn register_late_lint_pass(&mut self, lint_pass: LateLintPassObject) {
170 self.late_lint_passes.push(lint_pass);
172 /// Register a lint group.
173 pub fn register_lint_group(&mut self, name: &'static str, to: Vec<&'static Lint>) {
174 self.lint_groups.insert(name, to.into_iter().map(|x| LintId::of(x)).collect());
177 /// Register an LLVM pass.
179 /// Registration with LLVM itself is handled through static C++ objects with
180 /// constructors. This method simply adds a name to the list of passes to
182 pub fn register_llvm_pass(&mut self, name: &str) {
183 self.llvm_passes.push(name.to_owned());
186 /// Register an attribute with an attribute type.
188 /// Registered attributes will bypass the `custom_attribute` feature gate.
189 /// `Whitelisted` attributes will additionally not trigger the `unused_attribute`
190 /// lint. `CrateLevel` attributes will not be allowed on anything other than a crate.
191 pub fn register_attribute(&mut self, name: String, ty: AttributeType) {
192 self.attributes.push((name, ty));