]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/cfg.rs
Removed some unnecessary RefCells from resolve
[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;
26
27
28 pub fn expand_cfg<'cx>(cx: &mut ExtCtxt,
29                        sp: Span,
30                        tts: &[ast::TokenTree])
31                        -> Box<base::MacResult+'static> {
32     let mut p = cx.new_parser_from_tts(tts);
33     let mut cfgs = Vec::new();
34     // parse `cfg!(meta_item, meta_item(x,y), meta_item="foo", ...)`
35     while p.token != token::EOF {
36         cfgs.push(p.parse_meta_item());
37         if p.eat(&token::EOF) { break } // trailing comma is optional,.
38         p.expect(&token::COMMA);
39     }
40
41     // NOTE: turn on after snapshot
42     /*
43     if cfgs.len() != 1 {
44         cx.span_warn(sp, "The use of multiple cfgs at the top level of `cfg!` \
45                           is deprecated. Change `cfg!(a, b)` to \
46                           `cfg!(all(a, b))`.");
47     }
48     */
49
50     let matches_cfg = cfgs.iter().all(|cfg| attr::cfg_matches(&cx.parse_sess.span_diagnostic,
51                                                               cx.cfg.as_slice(), &**cfg));
52
53     MacExpr::new(cx.expr_bool(sp, matches_cfg))
54 }