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
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#[macro_export]
macro_rules! type_ {
    (!) => (Type::Never);
    (()) => (Type::Unit);
    (bool) => (Type::Bool);
    (usize) => (Type::Integer { signed: false, bits: None });
    (isize) => (Type::Integer { signed: true, bits: None });
    (u8) => (Type::Integer { signed: false, bits: Some(8) });
    (i8) => (Type::Integer { signed: true, bits: Some(8) });
    (u16) => (Type::Integer { signed: false, bits: Some(16) });
    (i16) => (Type::Integer { signed: true, bits: Some(16) });
    (u32) => (Type::Integer { signed: false, bits: Some(32) });
    (i32) => (Type::Integer { signed: true, bits: Some(32) });
    (u64) => (Type::Integer { signed: false, bits: Some(64) });
    (i64) => (Type::Integer { signed: true, bits: Some(64) });
    ([$len:tt; $($type:tt)*]) => {
        Type::Array { type_: Box::new(type_!($($type)*)), length: $len }
    };
    (*const $($type:tt)*) => (type_!(*false $($type)*));
    (*mut $($type:tt)*) => (type_!(*true $($type)*));
    (*$mut:tt void) => (Type::Pointer { mutable: $mut, type_: None });
    (*$mut:tt $($type:tt)*) => {
        Type::Pointer { mutable: $mut, type_: Some(Box::new(type_!($($type)*))) }
    };
    (fn $fields:tt) => (Type::Function { params: fields!($fields) });
}

#[macro_export]
macro_rules! docs {
    ($(#[doc = $doc:literal])*) => (vec![$($doc.into()),*]);
}

#[macro_export]
macro_rules! variants {
    ($input:tt) => (variants!($input []));

    ({ $(#[doc = $doc:literal])* $name:ident = $value:expr, $($input:tt)* }
     [$($output:tt)*]) => (variants! {
        { $($input)* } [$($output)* Variant {
            docs: vec![$($doc.into()),*],
            name: stringify!($name).into(),
            value: $value,
        },]
    });

    ({} [$($output:tt)*]) => (vec![$($output)*]);
}

#[macro_export]
macro_rules! fields {
    ($input:tt) => (fields!($input []));

    ({ $(#[doc = $doc:literal])* $(#[cfg($cfg:meta)])* $name:ident: $($input:tt)* } $output:tt
    ) => (fields! {
        [$($doc)*] [$($cfg)*] $name [] { $($input)* } $output
    });

    ($doc:tt $cfg:tt $name:ident $type:tt {} $output:tt) => (fields! {
        $doc $cfg $name $type { , } $output
    });

    ([$($doc:literal)*] [$($cfg:meta)*] $name:ident [$($type:tt)*] { , $($input:tt)* }
     [$($output:tt)*]) => (fields! {
        { $($input)* } [
            $($output)*
            $(#[cfg($cfg)])*
            Field {
                docs: vec![$($doc.into()),*],
                name: stringify!($name).into(),
                type_: type_!($($type)*),
            },
        ]
    });

    ($doc:tt $cfg:tt $name:ident [$($type:tt)*] { $x:tt $($input:tt)* } $output:tt) => (fields! {
        $doc $cfg $name [$($type)* $x] { $($input)* } $output
    });

    ({} [$($output:tt)*]) => (vec![$($output)*]);
}

#[macro_export]
macro_rules! item {
    ($(#[doc = $doc:literal])* enum $name:ident $variants:tt) => {
        Item::Enum(Enum {
            docs: vec![$($doc.into()),*],
            name: stringify!($name).into(),
            variants: variants!($variants),
        })
    };

    ($(#[doc = $doc:literal])* struct $name:ident $fields:tt) => {
        Item::Struct(Struct {
            docs: vec![$($doc.into()),*],
            name: stringify!($name).into(),
            fields: fields!($fields),
        })
    };

    ($(#[doc = $doc:literal])* fn $name:ident $link:literal $params:tt -> $($result:tt)*) => {
        Item::Fn(Fn {
            docs: vec![$($doc.into()),*],
            name: stringify!($name).into(),
            link: $link.into(),
            params: fields!($params),
            result: type_!($($result)*),
        })
    };
}