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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
use drone_macros_core::parse_error; use if_chain::if_chain; use proc_macro::TokenStream; use proc_macro2::Span; use quote::{format_ident, quote}; use syn::{ parenthesized, parse::{Parse, ParseStream, Result}, parse_macro_input, Data, DeriveInput, Fields, Ident, LitInt, LitStr, PathArguments, Token, Type, }; #[derive(Default)] struct Input { fields: Vec<Field>, } struct Field { ident: Ident, mode: Mode, offset: LitInt, width: Option<LitInt>, doc: Option<LitStr>, } enum Mode { Read, ReadWrite, Write, } impl Parse for Input { fn parse(input: ParseStream<'_>) -> Result<Self> { let content; parenthesized!(content in input); let mut fields = Vec::new(); let mut last_comma = true; while last_comma && !content.is_empty() { fields.push(content.parse()?); last_comma = content.parse::<Option<Token![,]>>()?.is_some(); } Ok(Self { fields: fields.into_iter().collect() }) } } impl Parse for Field { fn parse(input: ParseStream<'_>) -> Result<Self> { let ident = input.parse()?; let content; parenthesized!(content in input); let mode = content.parse()?; content.parse::<Token![,]>()?; let offset = content.parse()?; let width = if content.peek(Token![,]) && content.peek2(LitInt) { content.parse::<Token![,]>()?; Some(content.parse()?) } else { None }; let doc = if content.peek(Token![,]) && content.peek2(LitStr) { content.parse::<Token![,]>()?; Some(content.parse()?) } else { None }; Ok(Self { ident, mode, offset, width, doc }) } } impl Parse for Mode { fn parse(input: ParseStream<'_>) -> Result<Self> { let ident = input.parse::<Ident>()?; if ident == "r" { Ok(Self::Read) } else if ident == "rw" { Ok(Self::ReadWrite) } else if ident == "w" { Ok(Self::Write) } else { Err(input.error("invalid mode")) } } } impl Mode { fn is_read(&self) -> bool { match *self { Self::Read | Self::ReadWrite => true, Self::Write => false, } } fn is_write(&self) -> bool { match *self { Self::Read => false, Self::ReadWrite | Self::Write => true, } } } #[allow(clippy::too_many_lines)] pub fn proc_macro_derive(input: TokenStream) -> TokenStream { let DeriveInput { attrs, ident, data, .. } = parse_macro_input!(input); let bitfield = attrs.into_iter().find(|attr| { if_chain! { if attr.path.leading_colon.is_none(); if attr.path.segments.len() <= 1; if let Some(x) = attr.path.segments.iter().next(); if let PathArguments::None = x.arguments; then { x.ident == "bitfield" } else { false } } }); let Input { fields } = match bitfield { Some(attr) => { let input = attr.tokens.into(); parse_macro_input!(input) } None => Input::default(), }; let bits = if_chain! { if let Data::Struct(x) = data; if let Fields::Unnamed(x) = x.fields; if x.unnamed.len() <= 1; if let Some(x) = x.unnamed.into_iter().next(); if let Type::Path(x) = x.ty; then { x } else { parse_error!("Bitfield can be derived only from a tuple struct with one field"); } }; let field_tokens = fields .into_iter() .flat_map(|field| { let mut fields = Vec::new(); let Field { ident, mode, offset, width, doc, } = field; let width = width.unwrap_or_else(|| LitInt::new("1", Span::call_site())); let mut attrs = vec![quote!(#[inline])]; if let Some(doc) = doc { attrs.push(quote!(#[doc = #doc])); } let attrs = &attrs; if width.base10_digits() == "1" { if mode.is_read() { let read_bit = format_ident!("{}", ident); fields.push(quote! { #(#attrs)* pub fn #read_bit(&self) -> bool { unsafe { ::drone_core::bitfield::Bitfield::read_bit(self, #offset as #bits) } } }); } if mode.is_write() { let set_bit = format_ident!("set_{}", ident); let clear_bit = format_ident!("clear_{}", ident); let toggle_bit = format_ident!("toggle_{}", ident); fields.push(quote! { #(#attrs)* pub fn #set_bit(&mut self) -> &mut Self { unsafe { ::drone_core::bitfield::Bitfield::set_bit(self, #offset as #bits); } self } }); fields.push(quote! { #(#attrs)* pub fn #clear_bit(&mut self) -> &mut Self { unsafe { ::drone_core::bitfield::Bitfield::clear_bit(self, #offset as #bits); } self } }); fields.push(quote! { #(#attrs)* pub fn #toggle_bit(&mut self) -> &mut Self { unsafe { ::drone_core::bitfield::Bitfield::toggle_bit(self, #offset as #bits); } self } }); } } else { if mode.is_read() { let read_bits = format_ident!("{}", ident); fields.push(quote! { #(#attrs)* pub fn #read_bits(&self) -> #bits { unsafe { ::drone_core::bitfield::Bitfield::read_bits( self, #offset as #bits, #width as #bits, ) } } }); } if mode.is_write() { let write_bits = format_ident!("write_{}", ident); fields.push(quote! { #(#attrs)* pub fn #write_bits(&mut self, bits: #bits) -> &mut Self { unsafe { ::drone_core::bitfield::Bitfield::write_bits( self, #offset as #bits, #width as #bits, bits, ); } self } }); } } fields }) .collect::<Vec<_>>(); let expanded = quote! { impl ::drone_core::bitfield::Bitfield for #ident { type Bits = #bits; #[inline] fn bits(&self) -> #bits { self.0 } #[inline] fn bits_mut(&mut self) -> &mut #bits { &mut self.0 } } impl #ident { #(#field_tokens)* } }; expanded.into() }