]> git.lizzy.rs Git - rust.git/blob - src/librustc_plugin/registry.rs
Auto merge of #54271 - petrochenkov:nolegder, r=eddyb,alexcrichton
[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
65 impl<'a> Registry<'a> {
66     #[doc(hidden)]
67     pub fn new(sess: &'a Session, krate_span: Span) -> Registry<'a> {
68         Registry {
69             sess,
70             args_hidden: None,
71             krate_span,
72             syntax_exts: vec![],
73             early_lint_passes: vec![],
74             late_lint_passes: vec![],
75             lint_groups: FxHashMap::default(),
76             llvm_passes: vec![],
77             attributes: vec![],
78         }
79     }
80
81     /// Get the plugin's arguments, if any.
82     ///
83     /// These are specified inside the `plugin` crate attribute as
84     ///
85     /// ```no_run
86     /// #![plugin(my_plugin_name(... args ...))]
87     /// ```
88     ///
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(&[])
93     }
94
95     /// Register a syntax extension of any kind.
96     ///
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`");
101         }
102         self.syntax_exts.push((name, match extension {
103             NormalTT {
104                 expander,
105                 def_info: _,
106                 allow_internal_unstable,
107                 allow_internal_unsafe,
108                 local_inner_macros,
109                 unstable_feature,
110                 edition,
111             } => {
112                 let nid = ast::CRATE_NODE_ID;
113                 NormalTT {
114                     expander,
115                     def_info: Some((nid, self.krate_span)),
116                     allow_internal_unstable,
117                     allow_internal_unsafe,
118                     local_inner_macros,
119                     unstable_feature,
120                     edition,
121                 }
122             }
123             IdentTT(ext, _, allow_internal_unstable) => {
124                 IdentTT(ext, Some(self.krate_span), allow_internal_unstable)
125             }
126             _ => extension,
127         }));
128     }
129
130     /// Register a macro of the usual kind.
131     ///
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),
138             def_info: None,
139             allow_internal_unstable: false,
140             allow_internal_unsafe: false,
141             local_inner_macros: false,
142             unstable_feature: None,
143             edition: hygiene::default_edition(),
144         });
145     }
146
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);
150     }
151
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);
155     }
156     /// Register a lint group.
157     pub fn register_lint_group(
158         &mut self,
159         name: &'static str,
160         deprecated_name: Option<&'static str>,
161         to: Vec<&'static Lint>
162     ) {
163         self.lint_groups.insert(name,
164                                 (to.into_iter().map(|x| LintId::of(x)).collect(),
165                                  deprecated_name));
166     }
167
168     /// Register an LLVM pass.
169     ///
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
172     /// execute.
173     pub fn register_llvm_pass(&mut self, name: &str) {
174         self.llvm_passes.push(name.to_owned());
175     }
176
177     /// Register an attribute with an attribute type.
178     ///
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));
184     }
185 }