]> git.proxmox.com Git - rustc.git/blob - src/librustc_plugin/build.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / librustc_plugin / build.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 `rustc` when compiling a plugin crate.
12
13 use syntax::ast;
14 use syntax::attr;
15 use syntax::codemap::Span;
16 use syntax::errors;
17 use rustc::dep_graph::DepNode;
18 use rustc::hir::map::Map;
19 use rustc::hir::intravisit::Visitor;
20 use rustc::hir;
21
22 struct RegistrarFinder {
23 registrars: Vec<(ast::NodeId, Span)> ,
24 }
25
26 impl<'v> Visitor<'v> for RegistrarFinder {
27 fn visit_item(&mut self, item: &hir::Item) {
28 if let hir::ItemFn(..) = item.node {
29 if attr::contains_name(&item.attrs,
30 "plugin_registrar") {
31 self.registrars.push((item.id, item.span));
32 }
33 }
34 }
35 }
36
37 /// Find the function marked with `#[plugin_registrar]`, if any.
38 pub fn find_plugin_registrar(diagnostic: &errors::Handler,
39 hir_map: &Map)
40 -> Option<ast::NodeId> {
41 let _task = hir_map.dep_graph.in_task(DepNode::PluginRegistrar);
42 let krate = hir_map.krate();
43
44 let mut finder = RegistrarFinder { registrars: Vec::new() };
45 krate.visit_all_items(&mut finder);
46
47 match finder.registrars.len() {
48 0 => None,
49 1 => {
50 let (node_id, _) = finder.registrars.pop().unwrap();
51 Some(node_id)
52 },
53 _ => {
54 let mut e = diagnostic.struct_err("multiple plugin registration functions found");
55 for &(_, span) in &finder.registrars {
56 e.span_note(span, "one is here");
57 }
58 e.emit();
59 diagnostic.abort_if_errors();
60 unreachable!();
61 }
62 }
63 }