]> git.lizzy.rs Git - rust.git/blob - src/librustc_plugin/registry.rs
Rollup merge of #41910 - mersinvald:master, r=Mark-Simulacrum
[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
16 use syntax::ext::base::{SyntaxExtension, NamedSyntaxExtension, NormalTT, IdentTT};
17 use syntax::ext::base::MacroExpanderFn;
18 use syntax::symbol::Symbol;
19 use syntax::ast;
20 use syntax::feature_gate::AttributeType;
21 use syntax_pos::Span;
22
23 use std::collections::HashMap;
24 use std::borrow::ToOwned;
25
26 /// Structure used to register plugins.
27 ///
28 /// A plugin registrar function takes an `&mut Registry` and should call
29 /// methods to register its plugins.
30 ///
31 /// This struct has public fields and other methods for use by `rustc`
32 /// itself. They are not documented here, and plugin authors should
33 /// not use them.
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,
38
39     #[doc(hidden)]
40     pub args_hidden: Option<Vec<ast::NestedMetaItem>>,
41
42     #[doc(hidden)]
43     pub krate_span: Span,
44
45     #[doc(hidden)]
46     pub syntax_exts: Vec<NamedSyntaxExtension>,
47
48     #[doc(hidden)]
49     pub early_lint_passes: Vec<EarlyLintPassObject>,
50
51     #[doc(hidden)]
52     pub late_lint_passes: Vec<LateLintPassObject>,
53
54     #[doc(hidden)]
55     pub lint_groups: HashMap<&'static str, Vec<LintId>>,
56
57     #[doc(hidden)]
58     pub llvm_passes: Vec<String>,
59
60     #[doc(hidden)]
61     pub attributes: Vec<(String, AttributeType)>,
62
63     whitelisted_custom_derives: Vec<ast::Name>,
64 }
65
66 impl<'a> Registry<'a> {
67     #[doc(hidden)]
68     pub fn new(sess: &'a Session, krate_span: Span) -> Registry<'a> {
69         Registry {
70             sess: sess,
71             args_hidden: None,
72             krate_span: krate_span,
73             syntax_exts: vec![],
74             early_lint_passes: vec![],
75             late_lint_passes: vec![],
76             lint_groups: HashMap::new(),
77             llvm_passes: vec![],
78             attributes: vec![],
79             whitelisted_custom_derives: Vec::new(),
80         }
81     }
82
83     /// Get the plugin's arguments, if any.
84     ///
85     /// These are specified inside the `plugin` crate attribute as
86     ///
87     /// ```no_run
88     /// #![plugin(my_plugin_name(... args ...))]
89     /// ```
90     ///
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(&[])
95     }
96
97     /// Register a syntax extension of any kind.
98     ///
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`");
103         }
104         self.syntax_exts.push((name, match extension {
105             NormalTT(ext, _, allow_internal_unstable) => {
106                 let nid = ast::CRATE_NODE_ID;
107                 NormalTT(ext, Some((nid, self.krate_span)), allow_internal_unstable)
108             }
109             IdentTT(ext, _, allow_internal_unstable) => {
110                 IdentTT(ext, Some(self.krate_span), allow_internal_unstable)
111             }
112             _ => extension,
113         }));
114     }
115
116     /// This can be used in place of `register_syntax_extension` to register legacy custom derives
117     /// (i.e. attribute syntax extensions whose name begins with `derive_`). Legacy custom
118     /// derives defined by this function do not trigger deprecation warnings when used.
119     #[unstable(feature = "rustc_private", issue = "27812")]
120     #[rustc_deprecated(since = "1.15.0", reason = "replaced by macros 1.1 (RFC 1861)")]
121     pub fn register_custom_derive(&mut self, name: ast::Name, extension: SyntaxExtension) {
122         assert!(name.as_str().starts_with("derive_"));
123         self.whitelisted_custom_derives.push(name);
124         self.register_syntax_extension(name, extension);
125     }
126
127     pub fn take_whitelisted_custom_derives(&mut self) -> Vec<ast::Name> {
128         ::std::mem::replace(&mut self.whitelisted_custom_derives, Vec::new())
129     }
130
131     /// Register a macro of the usual kind.
132     ///
133     /// This is a convenience wrapper for `register_syntax_extension`.
134     /// It builds for you a `NormalTT` that calls `expander`,
135     /// and also takes care of interning the macro's name.
136     pub fn register_macro(&mut self, name: &str, expander: MacroExpanderFn) {
137         self.register_syntax_extension(Symbol::intern(name),
138                                        NormalTT(Box::new(expander), None, false));
139     }
140
141     /// Register a compiler lint pass.
142     pub fn register_early_lint_pass(&mut self, lint_pass: EarlyLintPassObject) {
143         self.early_lint_passes.push(lint_pass);
144     }
145
146     /// Register a compiler lint pass.
147     pub fn register_late_lint_pass(&mut self, lint_pass: LateLintPassObject) {
148         self.late_lint_passes.push(lint_pass);
149     }
150     /// Register a lint group.
151     pub fn register_lint_group(&mut self, name: &'static str, to: Vec<&'static Lint>) {
152         self.lint_groups.insert(name, to.into_iter().map(|x| LintId::of(x)).collect());
153     }
154
155     /// Register an LLVM pass.
156     ///
157     /// Registration with LLVM itself is handled through static C++ objects with
158     /// constructors. This method simply adds a name to the list of passes to
159     /// execute.
160     pub fn register_llvm_pass(&mut self, name: &str) {
161         self.llvm_passes.push(name.to_owned());
162     }
163
164     /// Register an attribute with an attribute type.
165     ///
166     /// Registered attributes will bypass the `custom_attribute` feature gate.
167     /// `Whitelisted` attributes will additionally not trigger the `unused_attribute`
168     /// lint. `CrateLevel` attributes will not be allowed on anything other than a crate.
169     pub fn register_attribute(&mut self, name: String, ty: AttributeType) {
170         self.attributes.push((name, ty));
171     }
172 }