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;
15 use rustc::util::nodemap::FxHashMap;
17 use syntax::ext::base::{SyntaxExtension, NamedSyntaxExtension, NormalTT, IdentTT};
18 use syntax::ext::base::MacroExpanderFn;
19 use syntax::ext::hygiene;
20 use syntax::symbol::Symbol;
22 use syntax::feature_gate::AttributeType;
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: FxHashMap<&'static str, (Vec<LintId>, Option<&'static str>)>,
59 pub llvm_passes: Vec<String>,
62 pub attributes: Vec<(String, AttributeType)>,
65 impl<'a> Registry<'a> {
67 pub fn new(sess: &'a Session, krate_span: Span) -> Registry<'a> {
73 early_lint_passes: vec![],
74 late_lint_passes: vec![],
75 lint_groups: FxHashMap::default(),
81 /// Get the plugin's arguments, if any.
83 /// These are specified inside the `plugin` crate attribute as
86 /// #![plugin(my_plugin_name(... args ...))]
89 /// Returns empty slice in case the plugin was loaded
90 /// with `--extra-plugins`
91 pub fn args<'b>(&'b self) -> &'b [ast::NestedMetaItem] {
92 self.args_hidden.as_ref().map(|v| &v[..]).unwrap_or(&[])
95 /// Register a syntax extension of any kind.
97 /// This is the most general hook into `libsyntax`'s expansion behavior.
98 pub fn register_syntax_extension(&mut self, name: ast::Name, extension: SyntaxExtension) {
99 if name == "macro_rules" {
100 panic!("user-defined macros may not be named `macro_rules`");
102 self.syntax_exts.push((name, match extension {
106 allow_internal_unstable,
107 allow_internal_unsafe,
112 let nid = ast::CRATE_NODE_ID;
115 def_info: Some((nid, self.krate_span)),
116 allow_internal_unstable,
117 allow_internal_unsafe,
123 IdentTT(ext, _, allow_internal_unstable) => {
124 IdentTT(ext, Some(self.krate_span), allow_internal_unstable)
130 /// Register a macro of the usual kind.
132 /// This is a convenience wrapper for `register_syntax_extension`.
133 /// It builds for you a `NormalTT` that calls `expander`,
134 /// and also takes care of interning the macro's name.
135 pub fn register_macro(&mut self, name: &str, expander: MacroExpanderFn) {
136 self.register_syntax_extension(Symbol::intern(name), NormalTT {
137 expander: Box::new(expander),
139 allow_internal_unstable: false,
140 allow_internal_unsafe: false,
141 local_inner_macros: false,
142 unstable_feature: None,
143 edition: hygiene::default_edition(),
147 /// Register a compiler lint pass.
148 pub fn register_early_lint_pass(&mut self, lint_pass: EarlyLintPassObject) {
149 self.early_lint_passes.push(lint_pass);
152 /// Register a compiler lint pass.
153 pub fn register_late_lint_pass(&mut self, lint_pass: LateLintPassObject) {
154 self.late_lint_passes.push(lint_pass);
156 /// Register a lint group.
157 pub fn register_lint_group(
160 deprecated_name: Option<&'static str>,
161 to: Vec<&'static Lint>
163 self.lint_groups.insert(name,
164 (to.into_iter().map(|x| LintId::of(x)).collect(),
168 /// Register an LLVM pass.
170 /// Registration with LLVM itself is handled through static C++ objects with
171 /// constructors. This method simply adds a name to the list of passes to
173 pub fn register_llvm_pass(&mut self, name: &str) {
174 self.llvm_passes.push(name.to_owned());
177 /// Register an attribute with an attribute type.
179 /// Registered attributes will bypass the `custom_attribute` feature gate.
180 /// `Whitelisted` attributes will additionally not trigger the `unused_attribute`
181 /// lint. `CrateLevel` attributes will not be allowed on anything other than a crate.
182 pub fn register_attribute(&mut self, name: String, ty: AttributeType) {
183 self.attributes.push((name, ty));