]> git.lizzy.rs Git - rust.git/blob - src/librustc_plugin/registry.rs
Auto merge of #53707 - eddyb:phantom-waffles, r=oli-obk
[rust.git] / src / librustc_plugin / registry.rs
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.
4 //
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.
10
11 //! Used by plugin crates to tell `rustc` about the plugins they provide.
12
13 use rustc::lint::{EarlyLintPassObject, LateLintPassObject, LintId, Lint};
14 use rustc::session::Session;
15 use rustc::util::nodemap::FxHashMap;
16
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;
21 use syntax::ast;
22 use syntax::feature_gate::AttributeType;
23 use syntax_pos::Span;
24
25 use std::borrow::ToOwned;
26
27 /// Structure used to register plugins.
28 ///
29 /// A plugin registrar function takes an `&mut Registry` and should call
30 /// methods to register its plugins.
31 ///
32 /// This struct has public fields and other methods for use by `rustc`
33 /// itself. They are not documented here, and plugin authors should
34 /// not use them.
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,
39
40     #[doc(hidden)]
41     pub args_hidden: Option<Vec<ast::NestedMetaItem>>,
42
43     #[doc(hidden)]
44     pub krate_span: Span,
45
46     #[doc(hidden)]
47     pub syntax_exts: Vec<NamedSyntaxExtension>,
48
49     #[doc(hidden)]
50     pub early_lint_passes: Vec<EarlyLintPassObject>,
51
52     #[doc(hidden)]
53     pub late_lint_passes: Vec<LateLintPassObject>,
54
55     #[doc(hidden)]
56     pub lint_groups: FxHashMap<&'static str, (Vec<LintId>, Option<&'static str>)>,
57
58     #[doc(hidden)]
59     pub llvm_passes: Vec<String>,
60
61     #[doc(hidden)]
62     pub attributes: Vec<(String, AttributeType)>,
63
64     whitelisted_custom_derives: Vec<ast::Name>,
65 }
66
67 impl<'a> Registry<'a> {
68     #[doc(hidden)]
69     pub fn new(sess: &'a Session, krate_span: Span) -> Registry<'a> {
70         Registry {
71             sess,
72             args_hidden: None,
73             krate_span,
74             syntax_exts: vec![],
75             early_lint_passes: vec![],
76             late_lint_passes: vec![],
77             lint_groups: FxHashMap::default(),
78             llvm_passes: vec![],
79             attributes: vec![],
80             whitelisted_custom_derives: Vec::new(),
81         }
82     }
83
84     /// Get the plugin's arguments, if any.
85     ///
86     /// These are specified inside the `plugin` crate attribute as
87     ///
88     /// ```no_run
89     /// #![plugin(my_plugin_name(... args ...))]
90     /// ```
91     ///
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(&[])
96     }
97
98     /// Register a syntax extension of any kind.
99     ///
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`");
104         }
105         self.syntax_exts.push((name, match extension {
106             NormalTT {
107                 expander,
108                 def_info: _,
109                 allow_internal_unstable,
110                 allow_internal_unsafe,
111                 local_inner_macros,
112                 unstable_feature,
113                 edition,
114             } => {
115                 let nid = ast::CRATE_NODE_ID;
116                 NormalTT {
117                     expander,
118                     def_info: Some((nid, self.krate_span)),
119                     allow_internal_unstable,
120                     allow_internal_unsafe,
121                     local_inner_macros,
122                     unstable_feature,
123                     edition,
124                 }
125             }
126             IdentTT(ext, _, allow_internal_unstable) => {
127                 IdentTT(ext, Some(self.krate_span), allow_internal_unstable)
128             }
129             _ => extension,
130         }));
131     }
132
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);
140     }
141
142     pub fn take_whitelisted_custom_derives(&mut self) -> Vec<ast::Name> {
143         ::std::mem::replace(&mut self.whitelisted_custom_derives, Vec::new())
144     }
145
146     /// Register a macro of the usual kind.
147     ///
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),
154             def_info: None,
155             allow_internal_unstable: false,
156             allow_internal_unsafe: false,
157             local_inner_macros: false,
158             unstable_feature: None,
159             edition: hygiene::default_edition(),
160         });
161     }
162
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);
166     }
167
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);
171     }
172     /// Register a lint group.
173     pub fn register_lint_group(
174         &mut self,
175         name: &'static str,
176         deprecated_name: Option<&'static str>,
177         to: Vec<&'static Lint>
178     ) {
179         self.lint_groups.insert(name,
180                                 (to.into_iter().map(|x| LintId::of(x)).collect(),
181                                  deprecated_name));
182     }
183
184     /// Register an LLVM pass.
185     ///
186     /// Registration with LLVM itself is handled through static C++ objects with
187     /// constructors. This method simply adds a name to the list of passes to
188     /// execute.
189     pub fn register_llvm_pass(&mut self, name: &str) {
190         self.llvm_passes.push(name.to_owned());
191     }
192
193     /// Register an attribute with an attribute type.
194     ///
195     /// Registered attributes will bypass the `custom_attribute` feature gate.
196     /// `Whitelisted` attributes will additionally not trigger the `unused_attribute`
197     /// lint. `CrateLevel` attributes will not be allowed on anything other than a crate.
198     pub fn register_attribute(&mut self, name: String, ty: AttributeType) {
199         self.attributes.push((name, ty));
200     }
201 }