]> git.lizzy.rs Git - rust.git/blob - src/librustc_plugin/registry.rs
Auto merge of #56462 - Zoxc:query-macro, r=oli-obk
[rust.git] / src / librustc_plugin / registry.rs
1 //! Used by plugin crates to tell `rustc` about the plugins they provide.
2
3 use rustc::lint::{EarlyLintPassObject, LateLintPassObject, LintId, Lint};
4 use rustc::session::Session;
5 use rustc::util::nodemap::FxHashMap;
6
7 use syntax::ext::base::{SyntaxExtension, NamedSyntaxExtension, NormalTT, IdentTT};
8 use syntax::ext::base::MacroExpanderFn;
9 use syntax::ext::hygiene;
10 use syntax::symbol::Symbol;
11 use syntax::ast;
12 use syntax::feature_gate::AttributeType;
13 use syntax_pos::Span;
14
15 use std::borrow::ToOwned;
16
17 /// Structure used to register plugins.
18 ///
19 /// A plugin registrar function takes an `&mut Registry` and should call
20 /// methods to register its plugins.
21 ///
22 /// This struct has public fields and other methods for use by `rustc`
23 /// itself. They are not documented here, and plugin authors should
24 /// not use them.
25 pub struct Registry<'a> {
26     /// Compiler session. Useful if you want to emit diagnostic messages
27     /// from the plugin registrar.
28     pub sess: &'a Session,
29
30     #[doc(hidden)]
31     pub args_hidden: Option<Vec<ast::NestedMetaItem>>,
32
33     #[doc(hidden)]
34     pub krate_span: Span,
35
36     #[doc(hidden)]
37     pub syntax_exts: Vec<NamedSyntaxExtension>,
38
39     #[doc(hidden)]
40     pub early_lint_passes: Vec<EarlyLintPassObject>,
41
42     #[doc(hidden)]
43     pub late_lint_passes: Vec<LateLintPassObject>,
44
45     #[doc(hidden)]
46     pub lint_groups: FxHashMap<&'static str, (Vec<LintId>, Option<&'static str>)>,
47
48     #[doc(hidden)]
49     pub llvm_passes: Vec<String>,
50
51     #[doc(hidden)]
52     pub attributes: Vec<(String, AttributeType)>,
53 }
54
55 impl<'a> Registry<'a> {
56     #[doc(hidden)]
57     pub fn new(sess: &'a Session, krate_span: Span) -> Registry<'a> {
58         Registry {
59             sess,
60             args_hidden: None,
61             krate_span,
62             syntax_exts: vec![],
63             early_lint_passes: vec![],
64             late_lint_passes: vec![],
65             lint_groups: FxHashMap::default(),
66             llvm_passes: vec![],
67             attributes: vec![],
68         }
69     }
70
71     /// Gets the plugin's arguments, if any.
72     ///
73     /// These are specified inside the `plugin` crate attribute as
74     ///
75     /// ```no_run
76     /// #![plugin(my_plugin_name(... args ...))]
77     /// ```
78     ///
79     /// Returns empty slice in case the plugin was loaded
80     /// with `--extra-plugins`
81     pub fn args<'b>(&'b self) -> &'b [ast::NestedMetaItem] {
82         self.args_hidden.as_ref().map(|v| &v[..]).unwrap_or(&[])
83     }
84
85     /// Register a syntax extension of any kind.
86     ///
87     /// This is the most general hook into `libsyntax`'s expansion behavior.
88     pub fn register_syntax_extension(&mut self, name: ast::Name, extension: SyntaxExtension) {
89         if name == "macro_rules" {
90             panic!("user-defined macros may not be named `macro_rules`");
91         }
92         self.syntax_exts.push((name, match extension {
93             NormalTT {
94                 expander,
95                 def_info: _,
96                 allow_internal_unstable,
97                 allow_internal_unsafe,
98                 local_inner_macros,
99                 unstable_feature,
100                 edition,
101             } => {
102                 let nid = ast::CRATE_NODE_ID;
103                 NormalTT {
104                     expander,
105                     def_info: Some((nid, self.krate_span)),
106                     allow_internal_unstable,
107                     allow_internal_unsafe,
108                     local_inner_macros,
109                     unstable_feature,
110                     edition,
111                 }
112             }
113             IdentTT { expander, span: _, allow_internal_unstable } => {
114                 IdentTT { expander, span: Some(self.krate_span), allow_internal_unstable }
115             }
116             _ => extension,
117         }));
118     }
119
120     /// Register a macro of the usual kind.
121     ///
122     /// This is a convenience wrapper for `register_syntax_extension`.
123     /// It builds for you a `NormalTT` that calls `expander`,
124     /// and also takes care of interning the macro's name.
125     pub fn register_macro(&mut self, name: &str, expander: MacroExpanderFn) {
126         self.register_syntax_extension(Symbol::intern(name), NormalTT {
127             expander: Box::new(expander),
128             def_info: None,
129             allow_internal_unstable: None,
130             allow_internal_unsafe: false,
131             local_inner_macros: false,
132             unstable_feature: None,
133             edition: hygiene::default_edition(),
134         });
135     }
136
137     /// Register a compiler lint pass.
138     pub fn register_early_lint_pass(&mut self, lint_pass: EarlyLintPassObject) {
139         self.early_lint_passes.push(lint_pass);
140     }
141
142     /// Register a compiler lint pass.
143     pub fn register_late_lint_pass(&mut self, lint_pass: LateLintPassObject) {
144         self.late_lint_passes.push(lint_pass);
145     }
146     /// Register a lint group.
147     pub fn register_lint_group(
148         &mut self,
149         name: &'static str,
150         deprecated_name: Option<&'static str>,
151         to: Vec<&'static Lint>
152     ) {
153         self.lint_groups.insert(name,
154                                 (to.into_iter().map(|x| LintId::of(x)).collect(),
155                                  deprecated_name));
156     }
157
158     /// Register an LLVM pass.
159     ///
160     /// Registration with LLVM itself is handled through static C++ objects with
161     /// constructors. This method simply adds a name to the list of passes to
162     /// execute.
163     pub fn register_llvm_pass(&mut self, name: &str) {
164         self.llvm_passes.push(name.to_owned());
165     }
166
167     /// Register an attribute with an attribute type.
168     ///
169     /// Registered attributes will bypass the `custom_attribute` feature gate.
170     /// `Whitelisted` attributes will additionally not trigger the `unused_attribute`
171     /// lint. `CrateLevel` attributes will not be allowed on anything other than a crate.
172     pub fn register_attribute(&mut self, name: String, ty: AttributeType) {
173         self.attributes.push((name, ty));
174     }
175 }