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.
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.
11 //! Used by plugin crates to tell `rustc` about the plugins they provide.
13 use rustc::lint::{EarlyLintPassObject, LateLintPassObject, LintId, Lint};
14 use rustc::session::Session;
16 use syntax::ext::base::{SyntaxExtension, NamedSyntaxExtension, NormalTT, IdentTT};
17 use syntax::ext::base::MacroExpanderFn;
18 use syntax::symbol::Symbol;
20 use syntax::feature_gate::AttributeType;
23 use std::collections::HashMap;
24 use std::borrow::ToOwned;
26 /// Structure used to register plugins.
28 /// A plugin registrar function takes an `&mut Registry` and should call
29 /// methods to register its plugins.
31 /// This struct has public fields and other methods for use by `rustc`
32 /// itself. They are not documented here, and plugin authors should
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,
40 pub args_hidden: Option<Vec<ast::NestedMetaItem>>,
46 pub syntax_exts: Vec<NamedSyntaxExtension>,
49 pub early_lint_passes: Vec<EarlyLintPassObject>,
52 pub late_lint_passes: Vec<LateLintPassObject>,
55 pub lint_groups: HashMap<&'static str, Vec<LintId>>,
58 pub llvm_passes: Vec<String>,
61 pub attributes: Vec<(String, AttributeType)>,
63 whitelisted_custom_derives: Vec<ast::Name>,
66 impl<'a> Registry<'a> {
68 pub fn new(sess: &'a Session, krate_span: Span) -> Registry<'a> {
72 krate_span: krate_span,
74 early_lint_passes: vec![],
75 late_lint_passes: vec![],
76 lint_groups: HashMap::new(),
79 whitelisted_custom_derives: Vec::new(),
83 /// Get the plugin's arguments, if any.
85 /// These are specified inside the `plugin` crate attribute as
88 /// #![plugin(my_plugin_name(... args ...))]
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(&[])
97 /// Register a syntax extension of any kind.
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`");
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)
109 IdentTT(ext, _, allow_internal_unstable) => {
110 IdentTT(ext, Some(self.krate_span), allow_internal_unstable)
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);
127 pub fn take_whitelisted_custom_derives(&mut self) -> Vec<ast::Name> {
128 ::std::mem::replace(&mut self.whitelisted_custom_derives, Vec::new())
131 /// Register a macro of the usual kind.
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));
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);
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);
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());
155 /// Register an LLVM pass.
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
160 pub fn register_llvm_pass(&mut self, name: &str) {
161 self.llvm_passes.push(name.to_owned());
164 /// Register an attribute with an attribute type.
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));