Files
aho_corasick
anyhow
drone_config
drone_core
drone_core_macros
drone_ctypes
drone_macros_core
futures
futures_channel
futures_core
futures_io
futures_macro
futures_sink
futures_task
futures_util
if_chain
inflector
cases
camelcase
case
classcase
kebabcase
pascalcase
screamingsnakecase
sentencecase
snakecase
tablecase
titlecase
traincase
numbers
deordinalize
ordinalize
string
constants
deconstantize
demodulize
pluralize
singularize
suffix
foreignkey
lazy_static
memchr
pin_project_lite
pin_utils
proc_macro2
proc_macro_hack
proc_macro_nested
quote
regex
regex_syntax
serde
serde_derive
syn
toml
typenum
unicode_xid
  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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
use drone_macros_core::unkeywordize;
use inflector::Inflector;
use proc_macro::TokenStream;
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::{format_ident, quote};
use std::collections::HashSet;
use syn::{
    braced,
    parse::{Parse, ParseStream, Result},
    parse_macro_input, Attribute, Ident, LitInt, LitStr, Token, Visibility,
};

struct Input {
    variants: Vec<Variant>,
}

struct Variant {
    attrs: Vec<Attribute>,
    vis: Visibility,
    block: Ident,
    ident: Ident,
    address: LitInt,
    size: u8,
    reset: LitInt,
    traits: Vec<Ident>,
    fields: Vec<Field>,
}

struct Field {
    attrs: Vec<Attribute>,
    ident: Ident,
    offset: LitInt,
    width: LitInt,
    traits: Vec<Ident>,
}

impl Parse for Input {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        let mut variants = Vec::new();
        while !input.is_empty() {
            variants.push(input.parse()?);
            if !input.is_empty() {
                input.parse::<Token![;]>()?;
            }
        }
        Ok(Self { variants })
    }
}

impl Parse for Variant {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        let attrs = input.call(Attribute::parse_outer)?;
        let vis = input.parse()?;
        let block = input.parse()?;
        let ident = input.parse()?;
        input.parse::<Token![=>]>()?;
        let input2;
        braced!(input2 in input);
        let mut address = None;
        let mut size = None;
        let mut reset = None;
        let mut traits = Vec::new();
        let mut fields = Vec::new();
        while !input2.is_empty() {
            let ident = input2.parse::<Ident>()?;
            input2.parse::<Token![=>]>()?;
            if ident == "address" {
                if address.is_none() {
                    address = Some(input2.parse()?);
                } else {
                    return Err(input2.error("multiple `address` specifications"));
                }
            } else if ident == "size" {
                if size.is_none() {
                    size = Some(input2.parse::<LitInt>()?.base10_parse()?);
                } else {
                    return Err(input2.error("multiple `size` specifications"));
                }
            } else if ident == "reset" {
                if reset.is_none() {
                    reset = Some(input2.parse()?);
                } else {
                    return Err(input2.error("multiple `reset` specifications"));
                }
            } else if ident == "traits" {
                traits.extend(parse_traits(&input2)?);
            } else if ident == "fields" {
                fields.extend(Field::parse_list(&input2)?);
            } else {
                return Err(input2.error(format!("unknown key: `{}`", ident)));
            }
            if !input2.is_empty() {
                input2.parse::<Token![;]>()?;
            }
        }
        Ok(Self {
            attrs,
            vis,
            block,
            ident,
            address: address.ok_or_else(|| input2.error("missing `address` specification"))?,
            size: size.ok_or_else(|| input2.error("missing `size` specification"))?,
            reset: reset.ok_or_else(|| input2.error("missing `reset` specification"))?,
            traits,
            fields,
        })
    }
}

impl Field {
    fn parse_list(input: ParseStream<'_>) -> Result<Vec<Self>> {
        let mut fields = Vec::new();
        let input2;
        braced!(input2 in input);
        while !input2.is_empty() {
            fields.push(input2.parse()?);
            if !input2.is_empty() {
                input2.parse::<Token![;]>()?;
            }
        }
        Ok(fields)
    }
}

impl Parse for Field {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        let attrs = input.call(Attribute::parse_outer)?;
        let ident = input.parse()?;
        input.parse::<Token![=>]>()?;
        let input2;
        braced!(input2 in input);
        let mut offset = None;
        let mut width = None;
        let mut traits = Vec::new();
        while !input2.is_empty() {
            let ident = input2.parse::<Ident>()?;
            input2.parse::<Token![=>]>()?;
            if ident == "offset" {
                if offset.is_none() {
                    offset = Some(input2.parse()?);
                } else {
                    return Err(input2.error("multiple `offset` specifications"));
                }
            } else if ident == "width" {
                if width.is_none() {
                    width = Some(input2.parse()?);
                } else {
                    return Err(input2.error("multiple `width` specifications"));
                }
            } else if ident == "traits" {
                traits.extend(parse_traits(&input2)?);
            } else {
                return Err(input2.error(format!("unknown key: `{}`", ident)));
            }
            if !input2.is_empty() {
                input2.parse::<Token![;]>()?;
            }
        }
        Ok(Self {
            attrs,
            ident,
            offset: offset.ok_or_else(|| input2.error("missing `offset` specification"))?,
            width: width.ok_or_else(|| input2.error("missing `width` specification"))?,
            traits,
        })
    }
}

impl Variant {
    #[allow(clippy::too_many_lines, clippy::cognitive_complexity)]
    fn generate(&self) -> TokenStream2 {
        let t = format_ident!("_T");
        let val_ty = format_ident!("u{}", self.size);
        let mut imports = self.traits.iter().cloned().collect::<HashSet<_>>();
        let mut tokens = Vec::new();
        let mut struct_tokens = Vec::new();
        let mut ctor_tokens = Vec::new();
        for Field { attrs, ident, offset, width, traits } in &self.fields {
            let field_snk = ident.to_string().to_snake_case();
            let mut field_psc = ident.to_string().to_pascal_case();
            if field_psc == "Val" {
                field_psc.push('_');
            }
            let field_psc = format_ident!("{}", field_psc);
            let field_ident = format_ident!("{}", unkeywordize(&field_snk));
            imports.extend(traits.iter().cloned());
            struct_tokens.push(quote! {
                #(#attrs)*
                pub #field_ident: #field_psc<#t>
            });
            ctor_tokens.push(quote! {
                #field_ident: ::drone_core::token::Token::take()
            });
            tokens.push(quote! {
                #(#attrs)*
                #[derive(Clone, Copy)]
                pub struct #field_psc<#t: ::drone_core::reg::tag::RegTag>(#t);

                unsafe impl<#t> ::drone_core::token::Token for #field_psc<#t>
                where
                    #t: ::drone_core::reg::tag::RegTag,
                {
                    #[inline]
                    unsafe fn take() -> Self {
                        #field_psc(#t::default())
                    }
                }

                impl<#t> ::drone_core::reg::field::RegField<#t> for #field_psc<#t>
                where
                    #t: ::drone_core::reg::tag::RegTag,
                {
                    type Reg = Reg<#t>;
                    type URegField = #field_psc<::drone_core::reg::tag::Urt>;
                    type SRegField = #field_psc<::drone_core::reg::tag::Srt>;
                    type CRegField = #field_psc<::drone_core::reg::tag::Crt>;

                    const OFFSET: usize = #offset;
                    const WIDTH: usize = #width;
                }
            });
            for ident in traits {
                tokens.push(quote! {
                    impl<#t: ::drone_core::reg::tag::RegTag> #ident<#t> for #field_psc<#t> {}
                });
            }
            if width.base10_digits() == "1" {
                tokens.push(quote! {
                    impl<#t> ::drone_core::reg::field::RegFieldBit<#t> for #field_psc<#t>
                    where
                        #t: ::drone_core::reg::tag::RegTag,
                    {
                    }
                });
                if traits.iter().any(|name| name == "RRRegField") {
                    tokens.push(quote! {
                        #[allow(clippy::len_without_is_empty)]
                        impl<'a, #t: ::drone_core::reg::tag::RegTag> Hold<'a, #t> {
                            #(#attrs)*
                            #[inline]
                            pub fn #field_ident(&self) -> bool {
                                ::drone_core::reg::field::RRRegFieldBit::read(
                                    &self.reg.#field_ident,
                                    &self.val,
                                )
                            }
                        }
                    });
                }
                if traits.iter().any(|name| name == "WWRegField") {
                    let set_field = format_ident!("set_{}", field_snk);
                    let clear_field = format_ident!("clear_{}", field_snk);
                    let toggle_field = format_ident!("toggle_{}", field_snk);
                    tokens.push(quote! {
                        #[allow(clippy::len_without_is_empty)]
                        impl<'a, #t: ::drone_core::reg::tag::RegTag> Hold<'a, #t> {
                            #(#attrs)*
                            #[inline]
                            pub fn #set_field(&mut self) -> &mut Self {
                                ::drone_core::reg::field::WWRegFieldBit::set(
                                    &self.reg.#field_ident,
                                    &mut self.val,
                                );
                                self
                            }

                            #(#attrs)*
                            #[inline]
                            pub fn #clear_field(&mut self) -> &mut Self {
                                ::drone_core::reg::field::WWRegFieldBit::clear(
                                    &self.reg.#field_ident,
                                    &mut self.val,
                                );
                                self
                            }

                            #(#attrs)*
                            #[inline]
                            pub fn #toggle_field(&mut self) -> &mut Self {
                                ::drone_core::reg::field::WWRegFieldBit::toggle(
                                    &self.reg.#field_ident,
                                    &mut self.val,
                                );
                                self
                            }
                        }
                    });
                }
            } else {
                tokens.push(quote! {
                    impl<#t> ::drone_core::reg::field::RegFieldBits<#t> for #field_psc<#t>
                    where
                        #t: ::drone_core::reg::tag::RegTag,
                    {
                    }
                });
                if traits.iter().any(|name| name == "RRRegField") {
                    tokens.push(quote! {
                        #[allow(clippy::len_without_is_empty)]
                        impl<'a, #t: ::drone_core::reg::tag::RegTag> Hold<'a, #t> {
                            #(#attrs)*
                            #[inline]
                            pub fn #field_ident(&self) -> #val_ty {
                                ::drone_core::reg::field::RRRegFieldBits::read(
                                    &self.reg.#field_ident,
                                    &self.val,
                                )
                            }
                        }
                    });
                }
                if traits.iter().any(|name| name == "WWRegField") {
                    let write_field = format_ident!("write_{}", field_snk);
                    tokens.push(quote! {
                        #[allow(clippy::len_without_is_empty)]
                        impl<'a, #t: ::drone_core::reg::tag::RegTag> Hold<'a, #t> {
                            #(#attrs)*
                            #[inline]
                            pub fn #write_field(&mut self, bits: #val_ty) -> &mut Self {
                                ::drone_core::reg::field::WWRegFieldBits::write(
                                    &self.reg.#field_ident,
                                    &mut self.val,
                                    bits,
                                );
                                self
                            }
                        }
                    });
                }
            }
        }
        if self.fields.is_empty() {
            struct_tokens.push(quote!(_marker: ::core::marker::PhantomData<#t>));
            ctor_tokens.push(quote!(_marker: ::core::marker::PhantomData));
        }
        for ident in &self.traits {
            tokens.push(quote! {
                impl<#t: ::drone_core::reg::tag::RegTag> #ident<#t> for Reg<#t> {}
            });
        }
        let imports = if imports.is_empty() {
            quote!()
        } else {
            let imports = imports.iter();
            quote!(use super::{#(#imports),*};)
        };
        let Variant { attrs, vis, address, reset, .. } = self;
        let reg_full = self.reg_full();

        quote! {
            #(#attrs)*
            #vis mod #reg_full {
                #imports
                use ::drone_core::bitfield::Bitfield;

                #(#attrs)*
                #[derive(Bitfield, Clone, Copy)]
                pub struct Val(#val_ty);

                #(#attrs)*
                #[derive(Clone, Copy)]
                pub struct Reg<#t: ::drone_core::reg::tag::RegTag> {
                    #(#struct_tokens),*
                }

                unsafe impl<#t: ::drone_core::reg::tag::RegTag> ::drone_core::token::Token for Reg<#t> {
                    #[inline]
                    unsafe fn take() -> Self {
                        Self { #(#ctor_tokens,)* }
                    }
                }

                impl<#t: ::drone_core::reg::tag::RegTag> ::drone_core::reg::Reg<#t> for Reg<#t> {
                    type Val = Val;
                    type UReg = Reg<::drone_core::reg::tag::Urt>;
                    type SReg = Reg<::drone_core::reg::tag::Srt>;
                    type CReg = Reg<::drone_core::reg::tag::Crt>;

                    const ADDRESS: usize = #address;
                    const RESET: #val_ty = #reset;

                    #[inline]
                    unsafe fn val_from(bits: #val_ty) -> Val {
                        Val(bits)
                    }
                }

                impl<'a, #t> ::drone_core::reg::RegRef<'a, #t> for Reg<#t>
                where
                    #t: ::drone_core::reg::tag::RegTag + 'a,
                {
                    type Hold = Hold<'a, #t>;

                    #[inline]
                    fn hold(&'a self, val: Self::Val) -> Self::Hold {
                        Hold { reg: self, val }
                    }
                }

                #(#attrs)*
                pub struct Hold<'a, #t: ::drone_core::reg::tag::RegTag> {
                    reg: &'a Reg<#t>,
                    val: Val,
                }

                impl<'a, #t> ::drone_core::reg::RegHold<'a, #t, Reg<#t>> for Hold<'a, #t>
                where
                    #t: ::drone_core::reg::tag::RegTag,
                {
                    #[inline]
                    fn val(&self) -> Val {
                        self.val
                    }

                    #[inline]
                    fn set_val(&mut self, val: Val) {
                        self.val = val;
                    }
                }

                #(#tokens)*
            }
        }
    }

    fn reg_full(&self) -> Ident {
        format_ident!(
            "{}_{}",
            self.block.to_string().to_snake_case(),
            self.ident.to_string().to_snake_case()
        )
    }
}

fn parse_traits(input: ParseStream<'_>) -> Result<Vec<Ident>> {
    let mut traits = Vec::new();
    let input2;
    braced!(input2 in input);
    while !input2.is_empty() {
        traits.push(input2.parse()?);
    }
    Ok(traits)
}

pub fn proc_macro(input: TokenStream) -> TokenStream {
    let Input { variants } = parse_macro_input!(input);
    let reg_tokens = variants.iter().map(Variant::generate).collect::<Vec<_>>();
    let mut variant_tokens = Vec::new();
    for (i, reg_src) in variants.iter().enumerate() {
        for (j, reg_dst) in variants.iter().enumerate() {
            if i == j {
                continue;
            }
            let t = format_ident!("_T");
            let mod_src = reg_src.reg_full();
            let mod_dst = reg_dst.reg_full();
            let into_variant = format_ident!(
                "into_{}_{}",
                reg_dst.block.to_string().to_snake_case(),
                reg_dst.ident.to_string().to_snake_case()
            );
            let doc = LitStr::new(
                &format!(
                    "Converts the token of variant `{}`, to a token of variant `{}`.",
                    mod_src, mod_dst
                ),
                Span::call_site(),
            );
            variant_tokens.push(quote! {
                impl<#t: ::drone_core::reg::tag::RegTag> #mod_src::Reg<#t> {
                    #[doc = #doc]
                    pub fn #into_variant(self) -> #mod_dst::Reg<#t> {
                        unsafe { ::drone_core::token::Token::take() }
                    }
                }
            });
        }
    }
    let expanded = quote! {
        #(#reg_tokens)*
        #(#variant_tokens)*
    };
    expanded.into()
}