]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/cfg.rs
libsyntax: Mechanically change `~[T]` to `Vec<T>`
[rust.git] / src / libsyntax / ext / cfg.rs
1 // Copyright 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 /**
12 The compiler code necessary to support the cfg! extension, which
13 expands to a literal `true` or `false` based on whether the given cfgs
14 match the current compilation environment.
15 */
16
17 use ast;
18 use codemap::Span;
19 use ext::base::*;
20 use ext::base;
21 use ext::build::AstBuilder;
22 use attr;
23 use attr::*;
24 use parse::attr::ParserAttr;
25 use parse::token::InternedString;
26 use parse::token;
27 use parse;
28
29 pub fn expand_cfg(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) -> base::MacResult {
30     let mut p = parse::new_parser_from_tts(cx.parse_sess(),
31                                            cx.cfg(),
32                                            tts.to_owned());
33
34     let mut cfgs = Vec::new();
35     // parse `cfg!(meta_item, meta_item(x,y), meta_item="foo", ...)`
36     while p.token != token::EOF {
37         cfgs.push(p.parse_meta_item());
38         if p.eat(&token::EOF) { break } // trailing comma is optional,.
39         p.expect(&token::COMMA);
40     }
41
42     // test_cfg searches for meta items looking like `cfg(foo, ...)`
43     let in_cfg = &[cx.meta_list(sp, InternedString::new("cfg"), cfgs)];
44
45     let matches_cfg = attr::test_cfg(cx.cfg(), in_cfg.iter().map(|&x| x));
46     let e = cx.expr_bool(sp, matches_cfg);
47     MRExpr(e)
48 }