]>
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 `rustc` when compiling a plugin crate. | |
12 | ||
13 | use syntax::ast; | |
b039eaaf | 14 | use syntax::attr; |
3157f602 XL |
15 | use errors; |
16 | use syntax_pos::Span; | |
7453a54e | 17 | use rustc::dep_graph::DepNode; |
54a0048b | 18 | use rustc::hir::map::Map; |
476ff2be | 19 | use rustc::hir::itemlikevisit::ItemLikeVisitor; |
54a0048b | 20 | use rustc::hir; |
1a4d82fc JJ |
21 | |
22 | struct RegistrarFinder { | |
23 | registrars: Vec<(ast::NodeId, Span)> , | |
24 | } | |
25 | ||
476ff2be | 26 | impl<'v> ItemLikeVisitor<'v> for RegistrarFinder { |
e9174d1e SL |
27 | fn visit_item(&mut self, item: &hir::Item) { |
28 | if let hir::ItemFn(..) = item.node { | |
85aaf69f | 29 | if attr::contains_name(&item.attrs, |
1a4d82fc JJ |
30 | "plugin_registrar") { |
31 | self.registrars.push((item.id, item.span)); | |
32 | } | |
33 | } | |
1a4d82fc | 34 | } |
476ff2be SL |
35 | |
36 | fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem) { | |
37 | } | |
1a4d82fc JJ |
38 | } |
39 | ||
40 | /// Find the function marked with `#[plugin_registrar]`, if any. | |
9cc50fc6 | 41 | pub fn find_plugin_registrar(diagnostic: &errors::Handler, |
7453a54e | 42 | hir_map: &Map) |
e9174d1e | 43 | -> Option<ast::NodeId> { |
7453a54e SL |
44 | let _task = hir_map.dep_graph.in_task(DepNode::PluginRegistrar); |
45 | let krate = hir_map.krate(); | |
46 | ||
1a4d82fc | 47 | let mut finder = RegistrarFinder { registrars: Vec::new() }; |
476ff2be | 48 | krate.visit_all_item_likes(&mut finder); |
1a4d82fc JJ |
49 | |
50 | match finder.registrars.len() { | |
51 | 0 => None, | |
52 | 1 => { | |
53 | let (node_id, _) = finder.registrars.pop().unwrap(); | |
54 | Some(node_id) | |
55 | }, | |
56 | _ => { | |
9cc50fc6 | 57 | let mut e = diagnostic.struct_err("multiple plugin registration functions found"); |
85aaf69f | 58 | for &(_, span) in &finder.registrars { |
9cc50fc6 | 59 | e.span_note(span, "one is here"); |
1a4d82fc | 60 | } |
9cc50fc6 SL |
61 | e.emit(); |
62 | diagnostic.abort_if_errors(); | |
1a4d82fc JJ |
63 | unreachable!(); |
64 | } | |
65 | } | |
66 | } |