1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use crate::attr::{self, Then};
use crate::error::{Error, Result};
use crate::{constfn, expr, iter, token};
use proc_macro::{Delimiter, Group, Ident, Punct, Spacing, Span, TokenStream, TokenTree};
use std::iter::FromIterator;

pub fn cfg(introducer: &str, args: TokenStream, input: TokenStream) -> TokenStream {
    try_cfg(introducer, args, input).unwrap_or_else(Error::into_compile_error)
}

fn try_cfg(introducer: &str, args: TokenStream, input: TokenStream) -> Result<TokenStream> {
    let introducer = Ident::new(introducer, Span::call_site());

    let mut full_args = TokenStream::from(TokenTree::Ident(introducer));
    if !args.is_empty() {
        full_args.extend(std::iter::once(TokenTree::Group(Group::new(
            Delimiter::Parenthesis,
            args,
        ))));
    }

    let ref mut full_args = iter::new(full_args);
    let expr = expr::parse(full_args)?;
    token::parse_end(full_args)?;

    if expr.eval(crate::RUSTVERSION) {
        Ok(input)
    } else {
        Ok(TokenStream::new())
    }
}

pub fn try_attr(args: attr::Args, input: TokenStream) -> Result<TokenStream> {
    if !args.condition.eval(crate::RUSTVERSION) {
        return Ok(input);
    }

    match args.then {
        Then::Const(const_token) => constfn::insert_const(input, const_token),
        Then::Attribute(then) => {
            // #[cfg_attr(all(), #then)]
            Ok(TokenStream::from_iter(
                vec![
                    TokenTree::Punct(Punct::new('#', Spacing::Alone)),
                    TokenTree::Group(Group::new(
                        Delimiter::Bracket,
                        TokenStream::from_iter(vec![
                            TokenTree::Ident(Ident::new("cfg_attr", Span::call_site())),
                            TokenTree::Group(Group::new(
                                Delimiter::Parenthesis,
                                TokenStream::from_iter(
                                    vec![
                                        TokenTree::Ident(Ident::new("all", Span::call_site())),
                                        TokenTree::Group(Group::new(
                                            Delimiter::Parenthesis,
                                            TokenStream::new(),
                                        )),
                                        TokenTree::Punct(Punct::new(',', Spacing::Alone)),
                                    ]
                                    .into_iter()
                                    .chain(then),
                                ),
                            )),
                        ]),
                    )),
                ]
                .into_iter()
                .chain(input),
            ))
        }
    }
}