]> git.proxmox.com Git - rustc.git/blob - src/librustc/plugin/registry.rs
Imported Upstream version 1.5.0+dfsg1
[rustc.git] / src / librustc / plugin / registry.rs
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
13 use lint::{EarlyLintPassObject, LateLintPassObject, LintId, Lint};
14 use session::Session;
15
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;
21 use syntax::ptr::P;
22 use syntax::ast;
23 use syntax::feature_gate::AttributeType;
24
25 use std::collections::HashMap;
26 use std::borrow::ToOwned;
27
28 /// Structure used to register plugins.
29 ///
30 /// A plugin registrar function takes an `&mut Registry` and should call
31 /// methods to register its plugins.
32 ///
33 /// This struct has public fields and other methods for use by `rustc`
34 /// itself. They are not documented here, and plugin authors should
35 /// not use them.
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,
40
41 #[doc(hidden)]
42 pub args_hidden: Option<Vec<P<ast::MetaItem>>>,
43
44 #[doc(hidden)]
45 pub krate_span: Span,
46
47 #[doc(hidden)]
48 pub syntax_exts: Vec<NamedSyntaxExtension>,
49
50 #[doc(hidden)]
51 pub early_lint_passes: Vec<EarlyLintPassObject>,
52
53 #[doc(hidden)]
54 pub late_lint_passes: Vec<LateLintPassObject>,
55
56 #[doc(hidden)]
57 pub lint_groups: HashMap<&'static str, Vec<LintId>>,
58
59 #[doc(hidden)]
60 pub llvm_passes: Vec<String>,
61
62 #[doc(hidden)]
63 pub attributes: Vec<(String, AttributeType)>,
64 }
65
66 impl<'a> Registry<'a> {
67 #[doc(hidden)]
68 pub fn new(sess: &'a Session, krate: &ast::Crate) -> Registry<'a> {
69 Registry {
70 sess: sess,
71 args_hidden: None,
72 krate_span: krate.span,
73 syntax_exts: vec!(),
74 early_lint_passes: vec!(),
75 late_lint_passes: vec!(),
76 lint_groups: HashMap::new(),
77 llvm_passes: vec!(),
78 attributes: vec!(),
79 }
80 }
81
82 /// Get the plugin's arguments, if any.
83 ///
84 /// These are specified inside the `plugin` crate attribute as
85 ///
86 /// ```no_run
87 /// #![plugin(my_plugin_name(... args ...))]
88 /// ```
89 pub fn args<'b>(&'b self) -> &'b Vec<P<ast::MetaItem>> {
90 self.args_hidden.as_ref().expect("args not set")
91 }
92
93 /// Register a syntax extension of any kind.
94 ///
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)
100 }
101 IdentTT(ext, _, allow_internal_unstable) => {
102 IdentTT(ext, Some(self.krate_span), allow_internal_unstable)
103 }
104 MultiDecorator(ext) => MultiDecorator(ext),
105 MultiModifier(ext) => MultiModifier(ext),
106 MacroRulesTT => {
107 self.sess.err("plugin tried to register a new MacroRulesTT");
108 return;
109 }
110 }));
111 }
112
113 /// Register a macro of the usual kind.
114 ///
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));
121 }
122
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);
126 }
127
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);
131 }
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());
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
147 /// Register an attribute with an attribute type.
148 ///
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));
154 }
155 }