]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/cfg.rs
auto merge of #13967 : richo/rust/features/ICE-fails, r=alexcrichton
[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
30 pub fn expand_cfg(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
31                   -> Box<base::MacResult> {
32     let mut p = parse::new_parser_from_tts(cx.parse_sess(),
33                                            cx.cfg(),
34                                            tts.iter()
35                                               .map(|x| (*x).clone())
36                                               .collect());
37
38     let mut cfgs = Vec::new();
39     // parse `cfg!(meta_item, meta_item(x,y), meta_item="foo", ...)`
40     while p.token != token::EOF {
41         cfgs.push(p.parse_meta_item());
42         if p.eat(&token::EOF) { break } // trailing comma is optional,.
43         p.expect(&token::COMMA);
44     }
45
46     // test_cfg searches for meta items looking like `cfg(foo, ...)`
47     let in_cfg = &[cx.meta_list(sp, InternedString::new("cfg"), cfgs)];
48
49     let matches_cfg = attr::test_cfg(cx.cfg().as_slice(),
50                                      in_cfg.iter().map(|&x| x));
51     let e = cx.expr_bool(sp, matches_cfg);
52     MacExpr::new(e)
53 }