Skip to content

Commit

Permalink
mods
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Feb 3, 2024
1 parent 4b08b9b commit 3311fa8
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 32 deletions.
6 changes: 3 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl Default for IdentFormats {
"cluster_accessor".into(),
IdentFormat::default().snake_case(),
);
//map.insert("cluster_mod".into(), IdentFormat::default().snake_case());
map.insert("cluster_mod".into(), IdentFormat::default().snake_case());
map.insert("register".into(), IdentFormat::default().pascal_case());
map.insert(
"register_spec".into(),
Expand All @@ -210,13 +210,13 @@ impl Default for IdentFormats {
"register_accessor".into(),
IdentFormat::default().snake_case(),
);
//map.insert("register_mod".into(), IdentFormat::default().snake_case());
map.insert("register_mod".into(), IdentFormat::default().snake_case());
map.insert("peripheral".into(), IdentFormat::default().pascal_case());
map.insert(
"peripheral_sigleton".into(),
IdentFormat::default().snake_case(),
);
//map.insert("peripheral_mod".into(), IdentFormat::default().snake_case());
map.insert("peripheral_mod".into(), IdentFormat::default().snake_case());
map.insert(
"peripheral_feature".into(),
IdentFormat::default().snake_case(),
Expand Down
17 changes: 10 additions & 7 deletions src/generate/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ use quote::{quote, ToTokens};
use syn::{punctuated::Punctuated, Token};

use crate::util::{
self, ident, name_to_ty, path_segment, type_path, unsuffixed, zst_type, FullName,
ToSanitizedCase, BITS_PER_BYTE,
self, ident, name_to_ty, path_segment, type_path, unsuffixed, zst_type, FullName, BITS_PER_BYTE,
};
use anyhow::{anyhow, bail, Context, Result};

Expand All @@ -43,9 +42,13 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
let address = util::hex(p.base_address + config.base_address_shift);
let description = util::respace(p.description.as_ref().unwrap_or(&p.name));

let mod_ty = name.to_snake_case_ident(span);
let mod_ty = ident(&name, config, "peripheral_mod", span);
let (derive_regs, base, path) = if let Some(path) = path {
(true, path.peripheral.to_snake_case_ident(span), path)
(
true,
ident(&path.peripheral, config, "peripheral_mod", span),
path,
)
} else {
(false, mod_ty.clone(), BlockPath::new(&p.name))
};
Expand Down Expand Up @@ -1399,15 +1402,15 @@ fn cluster_block(

// name_snake_case needs to take into account array type.
let span = Span::call_site();
let mod_ty = mod_name.to_snake_case_ident(span);
let mod_ty = ident(&mod_name, config, "cluster_mod", span);
let block_ty = ident(&mod_name, &config, "cluster", span);

if let Some(dpath) = dpath {
let dparent = dpath.parent().unwrap();
let mut derived = if &dparent == path {
type_path(Punctuated::new())
} else {
util::block_path_to_ty(&dparent, span)
util::block_path_to_ty(&dparent, config, span)
};
let dname = util::replace_suffix(&index.clusters.get(&dpath).unwrap().name, "");
let mut mod_derived = derived.clone();
Expand All @@ -1418,7 +1421,7 @@ fn cluster_block(
mod_derived
.path
.segments
.push(path_segment(dname.to_snake_case_ident(span)));
.push(path_segment(ident(&dname, config, "cluster_mod", span)));

Ok(quote! {
#[doc = #description]
Expand Down
33 changes: 19 additions & 14 deletions src/generate/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub fn render(
let span = Span::call_site();
let reg_ty = ident(&name, &config, "register", span);
let doc_alias = (&reg_ty.to_string() != &name).then(|| quote!(#[doc(alias = #name)]));
let mod_ty = name.to_snake_case_ident(span);
let mod_ty = ident(&name, config, "register_mod", span);
let description = util::escape_special_chars(
util::respace(&register.description.clone().unwrap_or_else(|| {
warn!("Missing description for register {}", register.name);
Expand All @@ -59,7 +59,7 @@ pub fn render(
let mut derived = if &dpath.block == path {
type_path(Punctuated::new())
} else {
util::block_path_to_ty(&dpath.block, span)
util::block_path_to_ty(&dpath.block, config, span)
};
let dname = util::name_of(index.registers.get(dpath).unwrap(), config.ignore_groups);
let mut mod_derived = derived.clone();
Expand All @@ -70,7 +70,7 @@ pub fn render(
mod_derived
.path
.segments
.push(path_segment(dname.to_snake_case_ident(span)));
.push(path_segment(ident(&dname, config, "register_mod", span)));

Ok(quote! {
pub use #derived as #reg_ty;
Expand Down Expand Up @@ -203,7 +203,7 @@ pub fn render_register_mod(
let name = util::name_of(register, config.ignore_groups);
let span = Span::call_site();
let regspec_ty = regspec(&name, config, span);
let name_snake_case = name.to_snake_case_ident(span);
let mod_ty = ident(&name, config, "register_mod", span);
let rsize = properties
.size
.ok_or_else(|| anyhow!("Register {} has no `size` field", register.name))?;
Expand Down Expand Up @@ -386,7 +386,7 @@ pub fn render_register_mod(
can_read,
can_write,
can_reset,
&name_snake_case,
&mod_ty,
true,
register.read_action,
)?
Expand All @@ -402,15 +402,14 @@ pub fn render_register_mod(
});

if can_read {
let doc = format!("`read()` method returns [`{name_snake_case}::R`](R) reader structure",);
let doc = format!("`read()` method returns [`{mod_ty}::R`](R) reader structure",);
mod_items.extend(quote! {
#[doc = #doc]
impl crate::Readable for #regspec_ty {}
});
}
if can_write {
let doc =
format!("`write(|w| ..)` method takes [`{name_snake_case}::W`](W) writer structure",);
let doc = format!("`write(|w| ..)` method takes [`{mod_ty}::W`](W) writer structure",);

let zero_to_modify_fields_bitmap = util::hex(zero_to_modify_fields_bitmap);
let one_to_modify_fields_bitmap = util::hex(one_to_modify_fields_bitmap);
Expand Down Expand Up @@ -865,7 +864,7 @@ pub fn fields(
let base_field = util::replace_suffix(&base.field.name, "");
let base_r = ident(&base_field, &config, "field_reader", span);
if !reader_derives.contains(&reader_ty) {
let base_path = base_syn_path(base, &fpath, &base_r)?;
let base_path = base_syn_path(base, &fpath, &base_r, config)?;
mod_items.extend(quote! {
#[doc = #field_reader_brief]
pub use #base_path as #reader_ty;
Expand All @@ -877,7 +876,7 @@ pub fn fields(
if base.register() != fpath.register() {
// use the same enum structure name
if !enum_derives.contains(&value_read_ty) {
let base_path = base_syn_path(base, &fpath, &value_read_ty)?;
let base_path = base_syn_path(base, &fpath, &value_read_ty, config)?;
mod_items.extend(quote! {
#[doc = #description]
pub use #base_path as #value_read_ty;
Expand Down Expand Up @@ -1140,7 +1139,7 @@ pub fn fields(
let base_field = util::replace_suffix(&base.field.name, "");
let base_w = ident(&base_field, &config, "field_writer", span);
if !writer_derives.contains(&writer_ty) {
let base_path = base_syn_path(base, &fpath, &base_w)?;
let base_path = base_syn_path(base, &fpath, &base_w, config)?;
mod_items.extend(quote! {
#[doc = #field_writer_brief]
pub use #base_path as #writer_ty;
Expand All @@ -1153,7 +1152,7 @@ pub fn fields(
if writer_reader_different_enum {
// use the same enum structure name
if !writer_enum_derives.contains(&value_write_ty) {
let base_path = base_syn_path(base, &fpath, &value_write_ty)?;
let base_path = base_syn_path(base, &fpath, &value_write_ty, config)?;
mod_items.extend(quote! {
#[doc = #description]
pub use #base_path as #value_write_ty;
Expand Down Expand Up @@ -1458,18 +1457,24 @@ fn base_syn_path(
base: &EnumPath,
fpath: &FieldPath,
base_ident: &Ident,
config: &Config,
) -> Result<syn::TypePath, syn::Error> {
let span = Span::call_site();
let path = if base.register() == fpath.register() {
ident_to_path(base_ident.clone())
} else if base.register().block == fpath.register().block {
let mut segments = Punctuated::new();
segments.push(path_segment(Ident::new("super", span)));
segments.push(path_segment(base.register().name.to_snake_case_ident(span)));
segments.push(path_segment(ident(
&base.register().name,
config,
"register_mod",
span,
)));
segments.push(path_segment(base_ident.clone()));
type_path(segments)
} else {
let mut rmod_ = crate::util::register_path_to_ty(base.register(), span);
let mut rmod_ = crate::util::register_path_to_ty(base.register(), config, span);
rmod_.path.segments.push(path_segment(base_ident.clone()));
rmod_
};
Expand Down
32 changes: 24 additions & 8 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,21 +341,37 @@ pub fn name_to_ty(name: Ident) -> Type {
syn::Type::Path(type_path(segments))
}

pub fn block_path_to_ty(bpath: &svd_parser::expand::BlockPath, span: Span) -> TypePath {
pub fn block_path_to_ty(
bpath: &svd_parser::expand::BlockPath,
config: &Config,
span: Span,
) -> TypePath {
let mut segments = Punctuated::new();
segments.push(path_segment(Ident::new("crate", span)));
segments.push(path_segment(bpath.peripheral.to_snake_case_ident(span)));
segments.push(path_segment(ident(
&bpath.peripheral,
config,
"peripheral_mod",
span,
)));
for ps in &bpath.path {
segments.push(path_segment(ps.to_snake_case_ident(span)));
segments.push(path_segment(ident(&ps, config, "cluster_mod", span)));
}
type_path(segments)
}

pub fn register_path_to_ty(rpath: &svd_parser::expand::RegisterPath, span: Span) -> TypePath {
let mut p = block_path_to_ty(&rpath.block, span);
p.path
.segments
.push(path_segment(rpath.name.to_snake_case_ident(span)));
pub fn register_path_to_ty(
rpath: &svd_parser::expand::RegisterPath,
config: &Config,
span: Span,
) -> TypePath {
let mut p = block_path_to_ty(&rpath.block, config, span);
p.path.segments.push(path_segment(ident(
&rpath.name,
config,
"register_mod",
span,
)));
p
}

Expand Down

0 comments on commit 3311fa8

Please sign in to comment.