| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
use proc_macro2::TokenStream; |
| 6 |
use syn::parse::{Parse, ParseStream, Parser, Result as ParseResult}; |
| 7 |
|
| 8 |
// $(#[$outer:meta])* |
| 9 |
// ($($vis:tt)*) $BitFlags:ident: $T:ty { |
| 10 |
// $( |
| 11 |
// $(#[$inner:ident $($args:tt)*])* |
| 12 |
// const $Flag:ident = $value:expr; |
| 13 |
// )+ |
| 14 |
// } |
| 15 |
#[derive(Debug)] |
| 16 |
pub struct Bitflags { |
| 17 |
attrs: Vec<syn::Attribute>, |
| 18 |
vis: syn::Visibility, |
| 19 |
#[allow(dead_code)] |
| 20 |
struct_token: Token![struct], |
| 21 |
name: syn::Ident, |
| 22 |
#[allow(dead_code)] |
| 23 |
colon_token: Token![:], |
| 24 |
repr: syn::Type, |
| 25 |
flags: Flags, |
| 26 |
} |
| 27 |
|
| 28 |
impl Bitflags { |
| 29 |
pub fn expand(&self) -> (syn::ItemStruct, syn::ItemImpl) { |
| 30 |
let Bitflags { |
| 31 |
ref attrs, |
| 32 |
ref vis, |
| 33 |
ref name, |
| 34 |
ref repr, |
| 35 |
ref flags, |
| 36 |
.. |
| 37 |
} = *self; |
| 38 |
|
| 39 |
let struct_ = parse_quote! { |
| 40 |
/// cbindgen:internal-derive-bitflags=true |
| 41 |
#(#attrs)* |
| 42 |
#vis struct #name { |
| 43 |
bits: #repr, |
| 44 |
} |
| 45 |
}; |
| 46 |
|
| 47 |
let consts = flags.expand(name, repr); |
| 48 |
let impl_ = parse_quote! { |
| 49 |
impl #name { |
| 50 |
#consts |
| 51 |
} |
| 52 |
}; |
| 53 |
|
| 54 |
(struct_, impl_) |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
impl Parse for Bitflags { |
| 59 |
fn parse(input: ParseStream) -> ParseResult<Self> { |
| 60 |
Ok(Self { |
| 61 |
attrs: input.call(syn::Attribute::parse_outer)?, |
| 62 |
vis: input.parse()?, |
| 63 |
struct_token: input.parse()?, |
| 64 |
name: input.parse()?, |
| 65 |
colon_token: input.parse()?, |
| 66 |
repr: input.parse()?, |
| 67 |
flags: input.parse()?, |
| 68 |
}) |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
// $(#[$inner:ident $($args:tt)*])* |
| 73 |
// const $Flag:ident = $value:expr; |
| 74 |
#[derive(Debug)] |
| 75 |
struct Flag { |
| 76 |
attrs: Vec<syn::Attribute>, |
| 77 |
#[allow(dead_code)] |
| 78 |
const_token: Token![const], |
| 79 |
name: syn::Ident, |
| 80 |
#[allow(dead_code)] |
| 81 |
equals_token: Token![=], |
| 82 |
value: syn::Expr, |
| 83 |
#[allow(dead_code)] |
| 84 |
semicolon_token: Token![;], |
| 85 |
} |
| 86 |
|
| 87 |
impl Flag { |
| 88 |
fn expand(&self, struct_name: &syn::Ident, repr: &syn::Type) -> TokenStream { |
| 89 |
let Flag { |
| 90 |
ref attrs, |
| 91 |
ref name, |
| 92 |
ref value, |
| 93 |
.. |
| 94 |
} = *self; |
| 95 |
quote! { |
| 96 |
#(#attrs)* |
| 97 |
pub const #name : #struct_name = #struct_name { bits: (#value) as #repr }; |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
impl Parse for Flag { |
| 103 |
fn parse(input: ParseStream) -> ParseResult<Self> { |
| 104 |
Ok(Self { |
| 105 |
attrs: input.call(syn::Attribute::parse_outer)?, |
| 106 |
const_token: input.parse()?, |
| 107 |
name: input.parse()?, |
| 108 |
equals_token: input.parse()?, |
| 109 |
value: input.parse()?, |
| 110 |
semicolon_token: input.parse()?, |
| 111 |
}) |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
#[derive(Debug)] |
| 116 |
struct Flags(Vec<Flag>); |
| 117 |
|
| 118 |
impl Parse for Flags { |
| 119 |
fn parse(input: ParseStream) -> ParseResult<Self> { |
| 120 |
let content; |
| 121 |
let _ = braced!(content in input); |
| 122 |
let mut flags = vec![]; |
| 123 |
while !content.is_empty() { |
| 124 |
flags.push(content.parse()?); |
| 125 |
} |
| 126 |
Ok(Flags(flags)) |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
impl Flags { |
| 131 |
fn expand(&self, struct_name: &syn::Ident, repr: &syn::Type) -> TokenStream { |
| 132 |
let mut ts = quote! {}; |
| 133 |
for flag in &self.0 { |
| 134 |
ts.extend(flag.expand(struct_name, repr)); |
| 135 |
} |
| 136 |
ts |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
pub fn parse(tokens: TokenStream) -> ParseResult<Bitflags> { |
| 141 |
let parser = Bitflags::parse; |
| 142 |
parser.parse2(tokens) |
| 143 |
} |