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::symbol::Symbol;
20 use syntax::feature_gate::AttributeType;
23 use std::collections::HashMap;
24 use std::borrow::ToOwned;
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<Vec<ast::NestedMetaItem>>,
46 pub syntax_exts: Vec<NamedSyntaxExtension>,
49 pub early_lint_passes: Vec<EarlyLintPassObject>,
52 pub late_lint_passes: Vec<LateLintPassObject>,
55 pub lint_groups: HashMap<&'static str, Vec<LintId>>,
58 pub llvm_passes: Vec<String>,
61 pub attributes: Vec<(String, AttributeType)>,
63 whitelisted_custom_derives: Vec<ast::Name>,
66 impl<'a> Registry<'a> {
68 pub fn new(sess: &'a Session, krate_span: Span) -> Registry<'a> {
74 early_lint_passes: vec![],
75 late_lint_passes: vec![],
76 lint_groups: HashMap::new(),
79 whitelisted_custom_derives: Vec::new(),
83 /// Get the plugin's arguments, if any.
85 /// These are specified inside the `plugin` crate attribute as
88 /// #![plugin(my_plugin_name(... args ...))]
91 /// Returns empty slice in case the plugin was loaded
92 /// with `--extra-plugins`
93 pub fn args<'b>(&'b self) -> &'b [ast::NestedMetaItem] {
94 self.args_hidden.as_ref().map(|v| &v[..]).unwrap_or(&[])
97 /// Register a syntax extension of any kind.
99 /// This is the most general hook into `libsyntax`'s expansion behavior.
100 pub fn register_syntax_extension(&mut self, name: ast::Name, extension: SyntaxExtension) {
101 if name == "macro_rules" {
102 panic!("user-defined macros may not be named `macro_rules`");
104 self.syntax_exts.push((name, match extension {
108 allow_internal_unstable,
109 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,
121 IdentTT(ext, _, allow_internal_unstable) => {
122 IdentTT(ext, Some(self.krate_span), allow_internal_unstable)
128 /// This can be used in place of `register_syntax_extension` to register legacy custom derives
129 /// (i.e. attribute syntax extensions whose name begins with `derive_`). Legacy custom
130 /// derives defined by this function do not trigger deprecation warnings when used.
131 #[unstable(feature = "rustc_private", issue = "27812")]
132 #[rustc_deprecated(since = "1.15.0", reason = "replaced by macros 1.1 (RFC 1861)")]
133 pub fn register_custom_derive(&mut self, name: ast::Name, extension: SyntaxExtension) {
134 assert!(name.as_str().starts_with("derive_"));
135 self.whitelisted_custom_derives.push(name);
136 self.register_syntax_extension(name, extension);
139 pub fn take_whitelisted_custom_derives(&mut self) -> Vec<ast::Name> {
140 ::std::mem::replace(&mut self.whitelisted_custom_derives, Vec::new())
143 /// Register a macro of the usual kind.
145 /// This is a convenience wrapper for `register_syntax_extension`.
146 /// It builds for you a `NormalTT` that calls `expander`,
147 /// and also takes care of interning the macro's name.
148 pub fn register_macro(&mut self, name: &str, expander: MacroExpanderFn) {
149 self.register_syntax_extension(Symbol::intern(name), NormalTT {
150 expander: Box::new(expander),
152 allow_internal_unstable: false,
153 allow_internal_unsafe: false,
154 unstable_feature: None,
158 /// Register a compiler lint pass.
159 pub fn register_early_lint_pass(&mut self, lint_pass: EarlyLintPassObject) {
160 self.early_lint_passes.push(lint_pass);
163 /// Register a compiler lint pass.
164 pub fn register_late_lint_pass(&mut self, lint_pass: LateLintPassObject) {
165 self.late_lint_passes.push(lint_pass);
167 /// Register a lint group.
168 pub fn register_lint_group(&mut self, name: &'static str, to: Vec<&'static Lint>) {
169 self.lint_groups.insert(name, to.into_iter().map(|x| LintId::of(x)).collect());
172 /// Register an LLVM pass.
174 /// Registration with LLVM itself is handled through static C++ objects with
175 /// constructors. This method simply adds a name to the list of passes to
177 pub fn register_llvm_pass(&mut self, name: &str) {
178 self.llvm_passes.push(name.to_owned());
181 /// Register an attribute with an attribute type.
183 /// Registered attributes will bypass the `custom_attribute` feature gate.
184 /// `Whitelisted` attributes will additionally not trigger the `unused_attribute`
185 /// lint. `CrateLevel` attributes will not be allowed on anything other than a crate.
186 pub fn register_attribute(&mut self, name: String, ty: AttributeType) {
187 self.attributes.push((name, ty));