]> git.proxmox.com Git - rustc.git/blob - src/librustc_plugin/build.rs
Imported Upstream version 1.6.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::diagnostic;
17 use rustc_front::intravisit::Visitor;
18 use rustc_front::hir;
19
20 struct RegistrarFinder {
21 registrars: Vec<(ast::NodeId, Span)> ,
22 }
23
24 impl<'v> Visitor<'v> for RegistrarFinder {
25 fn visit_item(&mut self, item: &hir::Item) {
26 if let hir::ItemFn(..) = item.node {
27 if attr::contains_name(&item.attrs,
28 "plugin_registrar") {
29 self.registrars.push((item.id, item.span));
30 }
31 }
32 }
33 }
34
35 /// Find the function marked with `#[plugin_registrar]`, if any.
36 pub fn find_plugin_registrar(diagnostic: &diagnostic::SpanHandler,
37 krate: &hir::Crate)
38 -> Option<ast::NodeId> {
39 let mut finder = RegistrarFinder { registrars: Vec::new() };
40 krate.visit_all_items(&mut finder);
41
42 match finder.registrars.len() {
43 0 => None,
44 1 => {
45 let (node_id, _) = finder.registrars.pop().unwrap();
46 Some(node_id)
47 },
48 _ => {
49 diagnostic.handler().err("multiple plugin registration functions found");
50 for &(_, span) in &finder.registrars {
51 diagnostic.span_note(span, "one is here");
52 }
53 diagnostic.handler().abort_if_errors();
54 unreachable!();
55 }
56 }
57 }