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 lint
::{EarlyLintPassObject, LateLintPassObject, LintId, Lint}
;
16 use syntax
::ext
::base
::{SyntaxExtension, NamedSyntaxExtension, NormalTT}
;
17 use syntax
::ext
::base
::{IdentTT, MultiModifier, MultiDecorator}
;
18 use syntax
::ext
::base
::{MacroExpanderFn, MacroRulesTT}
;
19 use syntax
::codemap
::Span
;
20 use syntax
::parse
::token
;
23 use syntax
::feature_gate
::AttributeType
;
25 use std
::collections
::HashMap
;
26 use std
::borrow
::ToOwned
;
28 /// Structure used to register plugins.
30 /// A plugin registrar function takes an `&mut Registry` and should call
31 /// methods to register its plugins.
33 /// This struct has public fields and other methods for use by `rustc`
34 /// itself. They are not documented here, and plugin authors should
36 pub struct Registry
<'a
> {
37 /// Compiler session. Useful if you want to emit diagnostic messages
38 /// from the plugin registrar.
39 pub sess
: &'a Session
,
42 pub args_hidden
: Option
<Vec
<P
<ast
::MetaItem
>>>,
48 pub syntax_exts
: Vec
<NamedSyntaxExtension
>,
51 pub early_lint_passes
: Vec
<EarlyLintPassObject
>,
54 pub late_lint_passes
: Vec
<LateLintPassObject
>,
57 pub lint_groups
: HashMap
<&'
static str, Vec
<LintId
>>,
60 pub llvm_passes
: Vec
<String
>,
63 pub attributes
: Vec
<(String
, AttributeType
)>,
66 impl<'a
> Registry
<'a
> {
68 pub fn new(sess
: &'a Session
, krate
: &ast
::Crate
) -> Registry
<'a
> {
72 krate_span
: krate
.span
,
74 early_lint_passes
: vec
!(),
75 late_lint_passes
: vec
!(),
76 lint_groups
: HashMap
::new(),
82 /// Get the plugin's arguments, if any.
84 /// These are specified inside the `plugin` crate attribute as
87 /// #![plugin(my_plugin_name(... args ...))]
89 pub fn args
<'b
>(&'b
self) -> &'b Vec
<P
<ast
::MetaItem
>> {
90 self.args_hidden
.as_ref().expect("args not set")
93 /// Register a syntax extension of any kind.
95 /// This is the most general hook into `libsyntax`'s expansion behavior.
96 pub fn register_syntax_extension(&mut self, name
: ast
::Name
, extension
: SyntaxExtension
) {
97 self.syntax_exts
.push((name
, match extension
{
98 NormalTT(ext
, _
, allow_internal_unstable
) => {
99 NormalTT(ext
, Some(self.krate_span
), allow_internal_unstable
)
101 IdentTT(ext
, _
, allow_internal_unstable
) => {
102 IdentTT(ext
, Some(self.krate_span
), allow_internal_unstable
)
104 MultiDecorator(ext
) => MultiDecorator(ext
),
105 MultiModifier(ext
) => MultiModifier(ext
),
107 self.sess
.err("plugin tried to register a new MacroRulesTT");
113 /// Register a macro of the usual kind.
115 /// This is a convenience wrapper for `register_syntax_extension`.
116 /// It builds for you a `NormalTT` that calls `expander`,
117 /// and also takes care of interning the macro's name.
118 pub fn register_macro(&mut self, name
: &str, expander
: MacroExpanderFn
) {
119 self.register_syntax_extension(token
::intern(name
),
120 NormalTT(Box
::new(expander
), None
, false));
123 /// Register a compiler lint pass.
124 pub fn register_early_lint_pass(&mut self, lint_pass
: EarlyLintPassObject
) {
125 self.early_lint_passes
.push(lint_pass
);
128 /// Register a compiler lint pass.
129 pub fn register_late_lint_pass(&mut self, lint_pass
: LateLintPassObject
) {
130 self.late_lint_passes
.push(lint_pass
);
132 /// Register a lint group.
133 pub fn register_lint_group(&mut self, name
: &'
static str, to
: Vec
<&'
static Lint
>) {
134 self.lint_groups
.insert(name
, to
.into_iter().map(|x
| LintId
::of(x
)).collect());
137 /// Register an LLVM pass.
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
142 pub fn register_llvm_pass(&mut self, name
: &str) {
143 self.llvm_passes
.push(name
.to_owned());
147 /// Register an attribute with an attribute type.
149 /// Registered attributes will bypass the `custom_attribute` feature gate.
150 /// `Whitelisted` attributes will additionally not trigger the `unused_attribute`
151 /// lint. `CrateLevel` attributes will not be allowed on anything other than a crate.
152 pub fn register_attribute(&mut self, name
: String
, ty
: AttributeType
) {
153 self.attributes
.push((name
, ty
));