]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
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 | ||
92a42be0 SL |
13 | use rustc::lint::{EarlyLintPassObject, LateLintPassObject, LintId, Lint}; |
14 | use rustc::session::Session; | |
1a4d82fc | 15 | |
54a0048b | 16 | use rustc::mir::transform::MirMapPass; |
7453a54e | 17 | |
1a4d82fc | 18 | use syntax::ext::base::{SyntaxExtension, NamedSyntaxExtension, NormalTT}; |
e9174d1e | 19 | use syntax::ext::base::{IdentTT, MultiModifier, MultiDecorator}; |
d9579d0f | 20 | use syntax::ext::base::{MacroExpanderFn, MacroRulesTT}; |
1a4d82fc JJ |
21 | use syntax::codemap::Span; |
22 | use syntax::parse::token; | |
23 | use syntax::ptr::P; | |
24 | use syntax::ast; | |
62682a34 | 25 | use syntax::feature_gate::AttributeType; |
1a4d82fc JJ |
26 | |
27 | use std::collections::HashMap; | |
9346a6ac | 28 | use std::borrow::ToOwned; |
1a4d82fc JJ |
29 | |
30 | /// Structure used to register plugins. | |
31 | /// | |
32 | /// A plugin registrar function takes an `&mut Registry` and should call | |
33 | /// methods to register its plugins. | |
34 | /// | |
35 | /// This struct has public fields and other methods for use by `rustc` | |
36 | /// itself. They are not documented here, and plugin authors should | |
37 | /// not use them. | |
38 | pub struct Registry<'a> { | |
39 | /// Compiler session. Useful if you want to emit diagnostic messages | |
40 | /// from the plugin registrar. | |
41 | pub sess: &'a Session, | |
42 | ||
43 | #[doc(hidden)] | |
85aaf69f | 44 | pub args_hidden: Option<Vec<P<ast::MetaItem>>>, |
1a4d82fc JJ |
45 | |
46 | #[doc(hidden)] | |
47 | pub krate_span: Span, | |
48 | ||
49 | #[doc(hidden)] | |
50 | pub syntax_exts: Vec<NamedSyntaxExtension>, | |
51 | ||
52 | #[doc(hidden)] | |
b039eaaf SL |
53 | pub early_lint_passes: Vec<EarlyLintPassObject>, |
54 | ||
55 | #[doc(hidden)] | |
56 | pub late_lint_passes: Vec<LateLintPassObject>, | |
1a4d82fc | 57 | |
7453a54e | 58 | #[doc(hidden)] |
54a0048b | 59 | pub mir_passes: Vec<Box<for<'pcx> MirMapPass<'pcx>>>, |
7453a54e | 60 | |
1a4d82fc JJ |
61 | #[doc(hidden)] |
62 | pub lint_groups: HashMap<&'static str, Vec<LintId>>, | |
9346a6ac AL |
63 | |
64 | #[doc(hidden)] | |
65 | pub llvm_passes: Vec<String>, | |
62682a34 SL |
66 | |
67 | #[doc(hidden)] | |
68 | pub attributes: Vec<(String, AttributeType)>, | |
1a4d82fc JJ |
69 | } |
70 | ||
71 | impl<'a> Registry<'a> { | |
72 | #[doc(hidden)] | |
73 | pub fn new(sess: &'a Session, krate: &ast::Crate) -> Registry<'a> { | |
74 | Registry { | |
75 | sess: sess, | |
76 | args_hidden: None, | |
77 | krate_span: krate.span, | |
78 | syntax_exts: vec!(), | |
b039eaaf SL |
79 | early_lint_passes: vec!(), |
80 | late_lint_passes: vec!(), | |
1a4d82fc | 81 | lint_groups: HashMap::new(), |
9346a6ac | 82 | llvm_passes: vec!(), |
62682a34 | 83 | attributes: vec!(), |
7453a54e | 84 | mir_passes: Vec::new(), |
1a4d82fc JJ |
85 | } |
86 | } | |
87 | ||
85aaf69f | 88 | /// Get the plugin's arguments, if any. |
1a4d82fc | 89 | /// |
85aaf69f SL |
90 | /// These are specified inside the `plugin` crate attribute as |
91 | /// | |
92 | /// ```no_run | |
93 | /// #![plugin(my_plugin_name(... args ...))] | |
94 | /// ``` | |
95 | pub fn args<'b>(&'b self) -> &'b Vec<P<ast::MetaItem>> { | |
1a4d82fc JJ |
96 | self.args_hidden.as_ref().expect("args not set") |
97 | } | |
98 | ||
99 | /// Register a syntax extension of any kind. | |
100 | /// | |
101 | /// This is the most general hook into `libsyntax`'s expansion behavior. | |
102 | pub fn register_syntax_extension(&mut self, name: ast::Name, extension: SyntaxExtension) { | |
103 | self.syntax_exts.push((name, match extension { | |
c34b1796 AL |
104 | NormalTT(ext, _, allow_internal_unstable) => { |
105 | NormalTT(ext, Some(self.krate_span), allow_internal_unstable) | |
106 | } | |
107 | IdentTT(ext, _, allow_internal_unstable) => { | |
108 | IdentTT(ext, Some(self.krate_span), allow_internal_unstable) | |
109 | } | |
d9579d0f | 110 | MultiDecorator(ext) => MultiDecorator(ext), |
85aaf69f | 111 | MultiModifier(ext) => MultiModifier(ext), |
1a4d82fc JJ |
112 | MacroRulesTT => { |
113 | self.sess.err("plugin tried to register a new MacroRulesTT"); | |
114 | return; | |
115 | } | |
116 | })); | |
117 | } | |
118 | ||
119 | /// Register a macro of the usual kind. | |
120 | /// | |
121 | /// This is a convenience wrapper for `register_syntax_extension`. | |
122 | /// It builds for you a `NormalTT` that calls `expander`, | |
123 | /// and also takes care of interning the macro's name. | |
124 | pub fn register_macro(&mut self, name: &str, expander: MacroExpanderFn) { | |
c34b1796 AL |
125 | self.register_syntax_extension(token::intern(name), |
126 | NormalTT(Box::new(expander), None, false)); | |
1a4d82fc JJ |
127 | } |
128 | ||
129 | /// Register a compiler lint pass. | |
b039eaaf SL |
130 | pub fn register_early_lint_pass(&mut self, lint_pass: EarlyLintPassObject) { |
131 | self.early_lint_passes.push(lint_pass); | |
1a4d82fc JJ |
132 | } |
133 | ||
b039eaaf SL |
134 | /// Register a compiler lint pass. |
135 | pub fn register_late_lint_pass(&mut self, lint_pass: LateLintPassObject) { | |
136 | self.late_lint_passes.push(lint_pass); | |
137 | } | |
1a4d82fc JJ |
138 | /// Register a lint group. |
139 | pub fn register_lint_group(&mut self, name: &'static str, to: Vec<&'static Lint>) { | |
140 | self.lint_groups.insert(name, to.into_iter().map(|x| LintId::of(x)).collect()); | |
141 | } | |
9346a6ac | 142 | |
7453a54e | 143 | /// Register a MIR pass |
54a0048b | 144 | pub fn register_mir_pass(&mut self, pass: Box<for<'pcx> MirMapPass<'pcx>>) { |
7453a54e SL |
145 | self.mir_passes.push(pass); |
146 | } | |
147 | ||
9346a6ac AL |
148 | /// Register an LLVM pass. |
149 | /// | |
150 | /// Registration with LLVM itself is handled through static C++ objects with | |
151 | /// constructors. This method simply adds a name to the list of passes to | |
152 | /// execute. | |
153 | pub fn register_llvm_pass(&mut self, name: &str) { | |
154 | self.llvm_passes.push(name.to_owned()); | |
155 | } | |
62682a34 SL |
156 | |
157 | ||
158 | /// Register an attribute with an attribute type. | |
159 | /// | |
160 | /// Registered attributes will bypass the `custom_attribute` feature gate. | |
161 | /// `Whitelisted` attributes will additionally not trigger the `unused_attribute` | |
162 | /// lint. `CrateLevel` attributes will not be allowed on anything other than a crate. | |
163 | pub fn register_attribute(&mut self, name: String, ty: AttributeType) { | |
62682a34 SL |
164 | self.attributes.push((name, ty)); |
165 | } | |
1a4d82fc | 166 | } |