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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#![recursion_limit = "256"]
#![allow(clippy::all)]
#![cfg_attr(rustfmt, rustfmt::skip)]
#![allow(nonstandard_style, unused_imports)]

use {
    ::core::{
        mem,
        ops::Not as _,
        slice,
    },
    ::proc_macro::{
        TokenStream,
    },
    ::proc_macro2::{
        Span,
        TokenStream as TokenStream2,
        TokenTree as TT,
    },
    ::quote::{
        format_ident,
        quote,
        quote_spanned,
        ToTokens,
    },
    ::syn::{*,
        parse::{
            Parse,
            Parser,
            ParseStream,
        },
        punctuated::Punctuated,
        spanned::Spanned,
        Result,
    },
    crate::utils::{
        *,
    },
};

#[allow(unused_macros)]
macro_rules! todo {(
    $( $fmt:expr $(, $($rest:tt)* )? )?
) => (
    ::core::todo! {
        concat!(file!(), ":", line!(), ":", column!(), " ({})"),
        ::core::format_args!(
            concat!($($fmt)?),
            $($( $($rest)* )?)?
        ),
    }
)}

#[macro_use]
extern crate macro_rules_attribute;

mod c_str;

#[path = "derives/_mod.rs"]
mod derives;

#[path = "ffi_export/_mod.rs"]
mod ffi_export;

#[path = "utils/_mod.rs"]
mod utils;

#[proc_macro_attribute] pub
fn cfg_headers (
    attrs: TokenStream,
    input: TokenStream,
) -> TokenStream
{
    parse_macro_input!(attrs as parse::Nothing);
    if cfg!(feature = "headers") {
        input
    } else {
        <_>::default()
    }
}

#[proc_macro] pub
fn c_str (input: TokenStream)
  -> TokenStream
{
    unwrap!(c_str::c_str(input.into()))
}

#[proc_macro_attribute] pub
fn ffi_export (attrs: TokenStream, input: TokenStream)
  -> TokenStream
{
    unwrap!(
        ffi_export::ffi_export(attrs.into(), input.into())
            .map(utils::mb_file_expanded)
    )
}

#[proc_macro_attribute] pub
fn derive_ReprC (
    attrs: TokenStream,
    input: TokenStream,
) -> TokenStream
{
    unwrap!(
        derives::derive_ReprC(attrs.into(), input.into())
            .map(utils::mb_file_expanded)
    )
}

// #[proc_macro_attribute] pub
// fn derive_CType (
//     attrs: TokenStream,
//     input: TokenStream,
// ) -> TokenStream
// {
//     unwrap!(derives::derive_CType(attrs.into(), input.into()))
// }

#[proc_macro_attribute] pub
fn derive_ReprC2 (
    attrs: TokenStream,
    input: TokenStream,
) -> TokenStream
{
    unwrap!(
        derives::repr_c::derive(attrs.into(), input.into())
            .map(utils::mb_file_expanded)
    )
}

#[doc(hidden)] /** Not part of the public API */ #[proc_macro] pub
fn __respan (
    input: TokenStream,
) -> TokenStream
{
    let parser = |input: ParseStream<'_>| Result::Ok({
        let mut contents;
        ({
            parenthesized!(contents in input);
            let tt: ::proc_macro2::TokenTree = contents.parse()?;
            let _: TokenStream2 = contents.parse()?;
            tt.span()
        }, {
            parenthesized!(contents in input);
            contents.parse::<TokenStream2>()?
        })
    });
    let (span, tts) = parse_macro_input!(input with parser);
    respan(span, tts.into()).into()
}

// where:
fn respan (
    span: Span,
    tts: TokenStream2,
) -> TokenStream2
{
    use ::proc_macro2::{*, TokenStream as TokenStream2};
    tts.into_iter().map(|mut tt| {
        if let TokenTree::Group(ref mut g) = tt {
            let g_span = g.span();
            *g = Group::new(
                g.delimiter(),
                respan(span, g.stream()),
            );
            g.set_span(/* span.located_at */ g_span)
        } else {
            tt.set_span(
                span //.located_at(tt.span())
            );
        }
        tt
    }).collect()
}