1 //! Used by plugin crates to tell `rustc` about the plugins they provide.
3 use rustc::lint::{EarlyLintPassObject, LateLintPassObject, LintId, Lint};
4 use rustc::session::Session;
5 use rustc::util::nodemap::FxHashMap;
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;
12 use syntax::feature_gate::AttributeType;
15 use std::borrow::ToOwned;
17 /// Structure used to register plugins.
19 /// A plugin registrar function takes an `&mut Registry` and should call
20 /// methods to register its plugins.
22 /// This struct has public fields and other methods for use by `rustc`
23 /// itself. They are not documented here, and plugin authors should
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,
31 pub args_hidden: Option<Vec<ast::NestedMetaItem>>,
37 pub syntax_exts: Vec<NamedSyntaxExtension>,
40 pub early_lint_passes: Vec<EarlyLintPassObject>,
43 pub late_lint_passes: Vec<LateLintPassObject>,
46 pub lint_groups: FxHashMap<&'static str, (Vec<LintId>, Option<&'static str>)>,
49 pub llvm_passes: Vec<String>,
52 pub attributes: Vec<(String, AttributeType)>,
55 impl<'a> Registry<'a> {
57 pub fn new(sess: &'a Session, krate_span: Span) -> Registry<'a> {
63 early_lint_passes: vec![],
64 late_lint_passes: vec![],
65 lint_groups: FxHashMap::default(),
71 /// Gets the plugin's arguments, if any.
73 /// These are specified inside the `plugin` crate attribute as
76 /// #![plugin(my_plugin_name(... args ...))]
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(&[])
85 /// Register a syntax extension of any kind.
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`");
92 self.syntax_exts.push((name, match extension {
96 allow_internal_unstable,
97 allow_internal_unsafe,
102 let nid = ast::CRATE_NODE_ID;
105 def_info: Some((nid, self.krate_span)),
106 allow_internal_unstable,
107 allow_internal_unsafe,
113 IdentTT { expander, span: _, allow_internal_unstable } => {
114 IdentTT { expander, span: Some(self.krate_span), allow_internal_unstable }
120 /// Register a macro of the usual kind.
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),
129 allow_internal_unstable: None,
130 allow_internal_unsafe: false,
131 local_inner_macros: false,
132 unstable_feature: None,
133 edition: hygiene::default_edition(),
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);
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);
146 /// Register a lint group.
147 pub fn register_lint_group(
150 deprecated_name: Option<&'static str>,
151 to: Vec<&'static Lint>
153 self.lint_groups.insert(name,
154 (to.into_iter().map(|x| LintId::of(x)).collect(),
158 /// Register an LLVM pass.
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
163 pub fn register_llvm_pass(&mut self, name: &str) {
164 self.llvm_passes.push(name.to_owned());
167 /// Register an attribute with an attribute type.
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));