Skip to content

Commit 044b1e5

Browse files
committed
add_from_variants
1 parent df36464 commit 044b1e5

File tree

1 file changed

+68
-94
lines changed

1 file changed

+68
-94
lines changed

src/generate/register.rs

+68-94
Original file line numberDiff line numberDiff line change
@@ -245,25 +245,6 @@ pub fn fields(
245245
r_impl_items: &mut Vec<Tokens>,
246246
w_impl_items: &mut Vec<Tokens>,
247247
) -> Result<()> {
248-
struct F<'a> {
249-
_pc_w: Ident,
250-
_sc: Ident,
251-
access: Option<Access>,
252-
description: String,
253-
evs: &'a [EnumeratedValues],
254-
mask: Tokens,
255-
name: &'a str,
256-
offset: Tokens,
257-
pc_r: Ident,
258-
_pc_r: Ident,
259-
pc_w: Ident,
260-
sc: Ident,
261-
bits: Ident,
262-
ty: Ident,
263-
width: u32,
264-
write_constraint: Option<&'a WriteConstraint>,
265-
}
266-
267248
impl<'a> F<'a> {
268249
fn from(f: &'a Field) -> Result<Self> {
269250
// TODO(AJM) - do we need to do anything with this range type?
@@ -277,7 +258,7 @@ pub fn fields(
277258
let pc_r = Ident::from(&*format!("{}R", pc));
278259
let _pc_r = Ident::from(&*format!("{}_R", pc));
279260
let pc_w = Ident::from(&*format!("{}W", pc));
280-
let _pc_w = Ident::from(&*format!("{}_W", pc));
261+
let _pc_w = Ident::from(&*format!("_{}W", pc));
281262
let _sc = Ident::from(&*format!("_{}", sc));
282263
let bits = if width == 1 {
283264
Ident::from("bit")
@@ -376,52 +357,13 @@ pub fn fields(
376357
}
377358

378359
if base.is_none() {
379-
let variants = Variant::from_enumerated_values(evs)?;
380360
let has_reserved_variant = evs.values.len() != (1 << f.width);
381-
let desc = format!("Possible values of the field `{}`", f.name,);
382-
383-
let vars = variants
384-
.iter()
385-
.map(|v| {
386-
let desc = util::escape_brackets(&v.doc);
387-
let pc = &v.pc;
388-
quote! {
389-
#[doc = #desc]
390-
#pc
391-
}
392-
})
393-
.collect::<Vec<_>>();
361+
let variants = Variant::from_enumerated_values(evs)?;
394362

395-
mod_items.push(quote! {
396-
#[doc = #desc]
397-
#[derive(Clone, Copy, Debug, PartialEq)]
398-
pub enum #pc_r {
399-
#(#vars),*
400-
}
401-
});
363+
add_from_variants(mod_items, &variants, pc_r, &f);
402364

403365
let mut enum_items = vec![];
404366

405-
let arms = variants.iter().map(|v| {
406-
let pc = &v.pc;
407-
let value = util::unsuffixed_or_bool(v.value, f.width);
408-
409-
quote! {
410-
#pc_r::#pc => #value
411-
}
412-
});
413-
414-
mod_items.push(quote! {
415-
impl crate::ToBits<#fty> for #pc_r {
416-
#[inline(always)]
417-
fn _bits(&self) -> #fty {
418-
match *self {
419-
#(#arms),*
420-
}
421-
}
422-
}
423-
});
424-
425367
let mut arms = variants
426368
.iter()
427369
.map(|v| {
@@ -530,7 +472,6 @@ pub fn fields(
530472
unsafety = None;
531473
}
532474
let pc_w = &f.pc_w;
533-
let pc_w_doc = format!("Values that can be written to the field `{}`", f.name);
534475

535476
let base_pc_w = base.as_ref().map(|base| {
536477
let pc = base.field.to_sanitized_upper_case();
@@ -539,38 +480,7 @@ pub fn fields(
539480
});
540481

541482
if base.is_none() {
542-
let variants_pc = variants.iter().map(|v| &v.pc);
543-
let variants_doc = variants
544-
.iter()
545-
.map(|v| util::escape_brackets(&v.doc).to_owned());
546-
mod_items.push(quote! {
547-
#[doc = #pc_w_doc]
548-
#[derive(Clone, Copy, Debug, PartialEq)]
549-
pub enum #pc_w {
550-
#(#[doc = #variants_doc]
551-
#variants_pc),*
552-
}
553-
});
554-
555-
let arms = variants.iter().map(|v| {
556-
let pc = &v.pc;
557-
let value = util::unsuffixed_or_bool(v.value, f.width);
558-
559-
quote! {
560-
#pc_w::#pc => #value
561-
}
562-
});
563-
564-
mod_items.push(quote! {
565-
impl crate::ToBits<#fty> for #pc_w {
566-
#[inline(always)]
567-
fn _bits(&self) -> #fty {
568-
match *self {
569-
#(#arms),*
570-
}
571-
}
572-
}
573-
});
483+
add_from_variants(mod_items, &variants, pc_w, &f);
574484
}
575485

576486
proxy_items.push(quote! {
@@ -725,6 +635,51 @@ impl Variant {
725635
}
726636
}
727637

638+
fn add_from_variants(mod_items: &mut Vec<Tokens>, variants: &Vec<Variant>, pc: &Ident, f: &F) {
639+
let fty = &f.ty;
640+
let desc = format!("Possible values of the field `{}`", f.name);
641+
642+
let vars = variants
643+
.iter()
644+
.map(|v| {
645+
let desc = util::escape_brackets(&v.doc);
646+
let pcv = &v.pc;
647+
quote! {
648+
#[doc = #desc]
649+
#pcv
650+
}
651+
})
652+
.collect::<Vec<_>>();
653+
654+
mod_items.push(quote! {
655+
#[doc = #desc]
656+
#[derive(Clone, Copy, Debug, PartialEq)]
657+
pub enum #pc {
658+
#(#vars),*
659+
}
660+
});
661+
662+
let arms = variants.iter().map(|v| {
663+
let pcv = &v.pc;
664+
let value = util::unsuffixed_or_bool(v.value, f.width);
665+
666+
quote! {
667+
#pc::#pcv => #value
668+
}
669+
});
670+
671+
mod_items.push(quote! {
672+
impl crate::ToBits<#fty> for #pc {
673+
#[inline(always)]
674+
fn _bits(&self) -> #fty {
675+
match *self {
676+
#(#arms),*
677+
}
678+
}
679+
}
680+
});
681+
}
682+
728683
fn derive_from_base(mod_items: &mut Vec<Tokens>, base: &Base, pc: &Ident, base_pc: &Ident, fname: &str) -> quote::Tokens {
729684
let desc = format!("Possible values of the field `{}`", fname,);
730685

@@ -768,6 +723,25 @@ fn derive_from_base(mod_items: &mut Vec<Tokens>, base: &Base, pc: &Ident, base_p
768723
}
769724
}
770725

726+
struct F<'a> {
727+
_pc_w: Ident,
728+
_sc: Ident,
729+
access: Option<Access>,
730+
description: String,
731+
evs: &'a [EnumeratedValues],
732+
mask: Tokens,
733+
name: &'a str,
734+
offset: Tokens,
735+
pc_r: Ident,
736+
_pc_r: Ident,
737+
pc_w: Ident,
738+
sc: Ident,
739+
bits: Ident,
740+
ty: Ident,
741+
width: u32,
742+
write_constraint: Option<&'a WriteConstraint>,
743+
}
744+
771745
#[derive(Clone, Debug)]
772746
pub struct Base<'a> {
773747
pub peripheral: Option<&'a str>,

0 commit comments

Comments
 (0)