]> git.lizzy.rs Git - rust.git/blob - src/librustc_plugin/registry.rs
Rollup merge of #61720 - alexcrichton:libstd-cfg-if-dep, r=sfackler
[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};
8 use syntax::ext::base::MacroExpanderFn;
9 use syntax::ext::hygiene::Transparency;
10 use syntax::symbol::{Symbol, sym};
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<(Symbol, 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, mut extension: SyntaxExtension) {
89         if name == sym::macro_rules {
90             panic!("user-defined macros may not be named `macro_rules`");
91         }
92         if let SyntaxExtension::LegacyBang { def_info: ref mut def_info @ None, .. } = extension {
93             *def_info = Some((ast::CRATE_NODE_ID, self.krate_span));
94         }
95         self.syntax_exts.push((name, extension));
96     }
97
98     /// Register a macro of the usual kind.
99     ///
100     /// This is a convenience wrapper for `register_syntax_extension`.
101     /// It builds for you a `SyntaxExtension::LegacyBang` that calls `expander`,
102     /// and also takes care of interning the macro's name.
103     pub fn register_macro(&mut self, name: &str, expander: MacroExpanderFn) {
104         self.register_syntax_extension(Symbol::intern(name), SyntaxExtension::LegacyBang {
105             expander: Box::new(expander),
106             def_info: None,
107             transparency: Transparency::SemiTransparent,
108             allow_internal_unstable: None,
109             allow_internal_unsafe: false,
110             local_inner_macros: false,
111             unstable_feature: None,
112             edition: self.sess.edition(),
113         });
114     }
115
116     /// Register a compiler lint pass.
117     pub fn register_early_lint_pass(&mut self, lint_pass: EarlyLintPassObject) {
118         self.early_lint_passes.push(lint_pass);
119     }
120
121     /// Register a compiler lint pass.
122     pub fn register_late_lint_pass(&mut self, lint_pass: LateLintPassObject) {
123         self.late_lint_passes.push(lint_pass);
124     }
125     /// Register a lint group.
126     pub fn register_lint_group(
127         &mut self,
128         name: &'static str,
129         deprecated_name: Option<&'static str>,
130         to: Vec<&'static Lint>
131     ) {
132         self.lint_groups.insert(name,
133                                 (to.into_iter().map(|x| LintId::of(x)).collect(),
134                                  deprecated_name));
135     }
136
137     /// Register an LLVM pass.
138     ///
139     /// Registration with LLVM itself is handled through static C++ objects with
140     /// constructors. This method simply adds a name to the list of passes to
141     /// execute.
142     pub fn register_llvm_pass(&mut self, name: &str) {
143         self.llvm_passes.push(name.to_owned());
144     }
145
146     /// Register an attribute with an attribute type.
147     ///
148     /// Registered attributes will bypass the `custom_attribute` feature gate.
149     /// `Whitelisted` attributes will additionally not trigger the `unused_attribute`
150     /// lint. `CrateLevel` attributes will not be allowed on anything other than a crate.
151     pub fn register_attribute(&mut self, name: Symbol, ty: AttributeType) {
152         self.attributes.push((name, ty));
153     }
154 }