]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/registrar.rs
libsyntax: Fix errors arising from the automated `~[T]` conversion
[rust.git] / src / libsyntax / ext / registrar.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 use ast;
12 use attr;
13 use codemap::Span;
14 use diagnostic;
15 use visit;
16 use visit::Visitor;
17
18 use std::vec_ng::Vec;
19
20 struct MacroRegistrarContext {
21     registrars: Vec<(ast::NodeId, Span)> ,
22 }
23
24 impl Visitor<()> for MacroRegistrarContext {
25     fn visit_item(&mut self, item: &ast::Item, _: ()) {
26         match item.node {
27             ast::ItemFn(..) => {
28                 if attr::contains_name(item.attrs.as_slice(),
29                                        "macro_registrar") {
30                     self.registrars.push((item.id, item.span));
31                 }
32             }
33             _ => {}
34         }
35
36         visit::walk_item(self, item, ());
37     }
38 }
39
40 pub fn find_macro_registrar(diagnostic: @diagnostic::SpanHandler,
41                             krate: &ast::Crate) -> Option<ast::DefId> {
42     let mut ctx = MacroRegistrarContext { registrars: Vec::new() };
43     visit::walk_crate(&mut ctx, krate, ());
44
45     match ctx.registrars.len() {
46         0 => None,
47         1 => {
48             let (node_id, _) = ctx.registrars.pop().unwrap();
49             Some(ast::DefId {
50                 krate: ast::LOCAL_CRATE,
51                 node: node_id
52             })
53         },
54         _ => {
55             diagnostic.handler().err("multiple macro registration functions found");
56             for &(_, span) in ctx.registrars.iter() {
57                 diagnostic.span_note(span, "one is here");
58             }
59             diagnostic.handler().abort_if_errors();
60             unreachable!();
61         }
62     }
63 }