Skip to content

Commit 4c6d6e8

Browse files
bors[bot]burrbull
andauthored
Merge #646
646: reexport derived mods r=Emilgardis a=burrbull Co-authored-by: Andrey Zgarbul <[email protected]>
2 parents 2196d85 + cc91299 commit 4c6d6e8

File tree

4 files changed

+44
-17
lines changed

4 files changed

+44
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10-
- Use register aliases in `RegisterBlock`
10+
- Use register aliases in `RegisterBlock` (both structure and mod)
1111
- Create aliases for derived registers & clusters
1212
- Move cluster struct inside mod
1313
- Support non-sequential field arrays

src/generate/peripheral.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -929,21 +929,27 @@ fn cluster_block(
929929
let name_constant_case = mod_name.to_constant_case_ident(span);
930930

931931
if let Some(dpath) = dpath {
932-
let dparent = util::parent(&dpath);
932+
let dparent = util::parent(&dpath).unwrap();
933933
let mut derived = if &dparent == path {
934934
type_path(Punctuated::new())
935935
} else {
936936
util::block_path_to_ty(&dparent, span)
937937
};
938938
let dname = util::replace_suffix(&index.clusters.get(&dpath).unwrap().name, "");
939+
let mut mod_derived = derived.clone();
939940
derived
940941
.path
941942
.segments
942943
.push(path_segment(dname.to_constant_case_ident(span)));
944+
mod_derived
945+
.path
946+
.segments
947+
.push(path_segment(dname.to_snake_case_ident(span)));
943948

944949
Ok(quote! {
945950
#[doc = #description]
946951
pub use #derived as #name_constant_case;
952+
pub use #mod_derived as #name_snake_case;
947953
})
948954
} else {
949955
let cpath = path.new_cluster(&c.name);

src/generate/register.rs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@ pub fn render(
2323
index: &Index,
2424
config: &Config,
2525
) -> Result<TokenStream> {
26-
let mut out = TokenStream::new();
2726
let name = util::name_of(register, config.ignore_groups);
2827
let span = Span::call_site();
2928
let name_constant_case = name.to_constant_case_ident(span);
30-
let name_constant_case_spec = format!("{name}_SPEC").to_constant_case_ident(span);
3129
let name_snake_case = name.to_snake_case_ident(span);
3230
let description = util::escape_brackets(
3331
util::respace(&register.description.clone().unwrap_or_else(|| {
@@ -44,44 +42,67 @@ pub fn render(
4442
util::block_path_to_ty(&dpath.block, span)
4543
};
4644
let dname = util::name_of(index.registers.get(dpath).unwrap(), config.ignore_groups);
45+
let mut mod_derived = derived.clone();
4746
derived
4847
.path
4948
.segments
5049
.push(path_segment(dname.to_constant_case_ident(span)));
50+
mod_derived
51+
.path
52+
.segments
53+
.push(path_segment(dname.to_snake_case_ident(span)));
5154

52-
out.extend(quote! {
53-
#[doc = #description]
55+
Ok(quote! {
5456
pub use #derived as #name_constant_case;
55-
});
57+
pub use #mod_derived as #name_snake_case;
58+
})
5659
} else {
57-
let alias_doc =
58-
format!("{name} register accessor: an alias for `Reg<{name_constant_case_spec}>`");
60+
let name_constant_case_spec = format!("{name}_SPEC").to_constant_case_ident(span);
61+
let access = util::access_of(&register.properties, register.fields.as_deref());
62+
let accs = if access.can_read() && access.can_write() {
63+
"rw"
64+
} else if access.can_write() {
65+
"w"
66+
} else if access.can_read() {
67+
"r"
68+
} else {
69+
return Err(anyhow!("Incorrect access of register {}", register.name));
70+
};
71+
let alias_doc = format!(
72+
"{name} ({accs}) register accessor: an alias for `Reg<{name_constant_case_spec}>`"
73+
);
74+
let mut out = TokenStream::new();
5975
out.extend(quote! {
6076
#[doc = #alias_doc]
6177
pub type #name_constant_case = crate::Reg<#name_snake_case::#name_constant_case_spec>;
6278
});
63-
let mod_items =
64-
render_register_mod(register, &path.new_register(&register.name), index, config)?;
79+
let mod_items = render_register_mod(
80+
register,
81+
access,
82+
&path.new_register(&register.name),
83+
index,
84+
config,
85+
)?;
6586

6687
out.extend(quote! {
6788
#[doc = #description]
6889
pub mod #name_snake_case {
6990
#mod_items
7091
}
7192
});
72-
};
7393

74-
Ok(out)
94+
Ok(out)
95+
}
7596
}
7697

7798
pub fn render_register_mod(
7899
register: &Register,
100+
access: Access,
79101
path: &RegisterPath,
80102
index: &Index,
81103
config: &Config,
82104
) -> Result<TokenStream> {
83105
let properties = &register.properties;
84-
let access = util::access_of(properties, register.fields.as_deref());
85106
let name = util::name_of(register, config.ignore_groups);
86107
let span = Span::call_site();
87108
let name_constant_case_spec = format!("{name}_SPEC").to_constant_case_ident(span);

src/util.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,10 +415,10 @@ pub fn path_segment(ident: Ident) -> PathSegment {
415415
}
416416
}
417417

418-
pub fn parent(p: &BlockPath) -> BlockPath {
418+
pub fn parent(p: &BlockPath) -> Option<BlockPath> {
419419
let mut p = p.clone();
420-
p.path.pop().unwrap();
421-
p
420+
p.path.pop()?;
421+
Some(p)
422422
}
423423

424424
pub trait U32Ext {

0 commit comments

Comments
 (0)