]> git.proxmox.com Git - rustc.git/blob - src/librustc/plugin/registry.rs
Move away from hash to the same rust naming schema
[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::{LintPassObject, 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 lint_passes: Vec<LintPassObject>,
52
53 #[doc(hidden)]
54 pub lint_groups: HashMap<&'static str, Vec<LintId>>,
55
56 #[doc(hidden)]
57 pub llvm_passes: Vec<String>,
58
59 #[doc(hidden)]
60 pub attributes: Vec<(String, AttributeType)>,
61 }
62
63 impl<'a> Registry<'a> {
64 #[doc(hidden)]
65 pub fn new(sess: &'a Session, krate: &ast::Crate) -> Registry<'a> {
66 Registry {
67 sess: sess,
68 args_hidden: None,
69 krate_span: krate.span,
70 syntax_exts: vec!(),
71 lint_passes: vec!(),
72 lint_groups: HashMap::new(),
73 llvm_passes: vec!(),
74 attributes: vec!(),
75 }
76 }
77
78 /// Get the plugin's arguments, if any.
79 ///
80 /// These are specified inside the `plugin` crate attribute as
81 ///
82 /// ```no_run
83 /// #![plugin(my_plugin_name(... args ...))]
84 /// ```
85 pub fn args<'b>(&'b self) -> &'b Vec<P<ast::MetaItem>> {
86 self.args_hidden.as_ref().expect("args not set")
87 }
88
89 /// Register a syntax extension of any kind.
90 ///
91 /// This is the most general hook into `libsyntax`'s expansion behavior.
92 #[allow(deprecated)]
93 pub fn register_syntax_extension(&mut self, name: ast::Name, extension: SyntaxExtension) {
94 self.syntax_exts.push((name, match extension {
95 NormalTT(ext, _, allow_internal_unstable) => {
96 NormalTT(ext, Some(self.krate_span), allow_internal_unstable)
97 }
98 IdentTT(ext, _, allow_internal_unstable) => {
99 IdentTT(ext, Some(self.krate_span), allow_internal_unstable)
100 }
101 MultiDecorator(ext) => MultiDecorator(ext),
102 MultiModifier(ext) => MultiModifier(ext),
103 MacroRulesTT => {
104 self.sess.err("plugin tried to register a new MacroRulesTT");
105 return;
106 }
107 }));
108 }
109
110 /// Register a macro of the usual kind.
111 ///
112 /// This is a convenience wrapper for `register_syntax_extension`.
113 /// It builds for you a `NormalTT` that calls `expander`,
114 /// and also takes care of interning the macro's name.
115 pub fn register_macro(&mut self, name: &str, expander: MacroExpanderFn) {
116 self.register_syntax_extension(token::intern(name),
117 NormalTT(Box::new(expander), None, false));
118 }
119
120 /// Register a compiler lint pass.
121 pub fn register_lint_pass(&mut self, lint_pass: LintPassObject) {
122 self.lint_passes.push(lint_pass);
123 }
124
125 /// Register a lint group.
126 pub fn register_lint_group(&mut self, name: &'static str, to: Vec<&'static Lint>) {
127 self.lint_groups.insert(name, to.into_iter().map(|x| LintId::of(x)).collect());
128 }
129
130 /// Register an LLVM pass.
131 ///
132 /// Registration with LLVM itself is handled through static C++ objects with
133 /// constructors. This method simply adds a name to the list of passes to
134 /// execute.
135 pub fn register_llvm_pass(&mut self, name: &str) {
136 self.llvm_passes.push(name.to_owned());
137 }
138
139
140 /// Register an attribute with an attribute type.
141 ///
142 /// Registered attributes will bypass the `custom_attribute` feature gate.
143 /// `Whitelisted` attributes will additionally not trigger the `unused_attribute`
144 /// lint. `CrateLevel` attributes will not be allowed on anything other than a crate.
145 pub fn register_attribute(&mut self, name: String, ty: AttributeType) {
146 self.attributes.push((name, ty));
147 }
148 }