Skip to content

Commit 2196d85

Browse files
bors[bot]burrbull
andauthored
Merge #645
645: type aliases in RegisterBlock r=Emilgardis a=burrbull Co-authored-by: Andrey Zgarbul <[email protected]>
2 parents f58762b + bb90385 commit 2196d85

File tree

4 files changed

+46
-85
lines changed

4 files changed

+46
-85
lines changed

CHANGELOG.md

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

88
## [Unreleased]
99

10+
- Use register aliases in `RegisterBlock`
1011
- Create aliases for derived registers & clusters
1112
- Move cluster struct inside mod
1213
- Support non-sequential field arrays

src/generate/peripheral.rs

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use quote::{quote, ToTokens};
99
use syn::{punctuated::Punctuated, Token};
1010

1111
use crate::util::{
12-
self, array_proxy_type, name_to_ty, name_to_wrapped_ty, new_syn_u32, path_segment, type_path,
13-
unsuffixed, Config, FullName, ToSanitizedCase, BITS_PER_BYTE,
12+
self, array_proxy_type, name_to_ty, new_syn_u32, path_segment, type_path, unsuffixed, Config,
13+
FullName, ToSanitizedCase, BITS_PER_BYTE,
1414
};
1515
use anyhow::{anyhow, bail, Context, Result};
1616

@@ -655,7 +655,7 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
655655
} else {
656656
util::replace_suffix(&cluster.name, "")
657657
};
658-
let ty = syn::Type::Path(name_to_ty(&ty_name));
658+
let ty = name_to_ty(&ty_name);
659659

660660
match cluster {
661661
Cluster::Single(info) => {
@@ -776,7 +776,7 @@ fn expand_register(register: &Register, config: &Config) -> Result<Vec<RegisterB
776776
} else {
777777
util::replace_suffix(&info_name, "")
778778
};
779-
let ty = name_to_wrapped_ty(&ty_name);
779+
let ty = name_to_ty(&ty_name);
780780

781781
match register {
782782
Register::Single(info) => {
@@ -928,55 +928,47 @@ fn cluster_block(
928928
let name_snake_case = mod_name.to_snake_case_ident(span);
929929
let name_constant_case = mod_name.to_constant_case_ident(span);
930930

931-
let struct_path = name_to_ty(&mod_name);
932-
933-
let mod_items = if let Some(dpath) = dpath {
931+
if let Some(dpath) = dpath {
934932
let dparent = util::parent(&dpath);
935933
let mut derived = if &dparent == path {
936-
let mut segments = Punctuated::new();
937-
segments.push(path_segment(Ident::new("super", span)));
938-
type_path(segments)
934+
type_path(Punctuated::new())
939935
} else {
940936
util::block_path_to_ty(&dparent, span)
941937
};
942938
let dname = util::replace_suffix(&index.clusters.get(&dpath).unwrap().name, "");
943-
derived
944-
.path
945-
.segments
946-
.push(path_segment(dname.to_snake_case_ident(span)));
947939
derived
948940
.path
949941
.segments
950942
.push(path_segment(dname.to_constant_case_ident(span)));
951943

952-
quote! {
944+
Ok(quote! {
953945
#[doc = #description]
954946
pub use #derived as #name_constant_case;
955-
}
947+
})
956948
} else {
957949
let cpath = path.new_cluster(&c.name);
958950
let mod_items = render_ercs(&mut c.children, &cpath, index, config)?;
959951

960952
// Generate the register block.
961953
let reg_block = register_or_cluster_block(&c.children, Some(&mod_name), config)?;
962954

963-
quote! {
955+
let mod_items = quote! {
964956
#reg_block
965957

966958
#mod_items
967-
}
968-
};
959+
};
969960

970-
Ok(quote! {
971-
#[doc = #description]
972-
pub use #struct_path;
961+
Ok(quote! {
962+
#[doc = #description]
963+
pub use #name_snake_case::#name_constant_case;
973964

974-
///Cluster
975-
#[doc = #description]
976-
pub mod #name_snake_case {
977-
#mod_items
978-
}
979-
})
965+
///Cluster
966+
#[doc = #description]
967+
pub mod #name_snake_case {
968+
#mod_items
969+
}
970+
})
971+
}
980972
}
981973

982974
fn new_syn_field(ident: Ident, ty: syn::Type) -> syn::Field {

src/generate/register.rs

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,45 +36,40 @@ pub fn render(
3636
}))
3737
.as_ref(),
3838
);
39-
let alias_doc =
40-
format!("{name} register accessor: an alias for `Reg<{name_constant_case_spec}>`");
41-
let wrapped_name = util::name_to_wrapped_ty(&name);
42-
out.extend(quote! {
43-
#[doc = #alias_doc]
44-
pub type #name_constant_case = #wrapped_name;
45-
});
4639

47-
let mod_items = if let Some(dpath) = dpath.as_ref() {
48-
let mut derived_spec = if &dpath.block == path {
49-
let mut segments = Punctuated::new();
50-
segments.push(path_segment(Ident::new("super", span)));
51-
type_path(segments)
40+
if let Some(dpath) = dpath.as_ref() {
41+
let mut derived = if &dpath.block == path {
42+
type_path(Punctuated::new())
5243
} else {
5344
util::block_path_to_ty(&dpath.block, span)
5445
};
5546
let dname = util::name_of(index.registers.get(dpath).unwrap(), config.ignore_groups);
56-
derived_spec
47+
derived
5748
.path
5849
.segments
59-
.push(path_segment(dname.to_snake_case_ident(span)));
60-
derived_spec.path.segments.push(path_segment(
61-
format!("{}_SPEC", dname).to_constant_case_ident(span),
62-
));
50+
.push(path_segment(dname.to_constant_case_ident(span)));
6351

64-
quote! {
52+
out.extend(quote! {
6553
#[doc = #description]
66-
pub use #derived_spec as #name_constant_case_spec;
67-
}
54+
pub use #derived as #name_constant_case;
55+
});
6856
} else {
69-
render_register_mod(register, &path.new_register(&register.name), index, config)?
70-
};
57+
let alias_doc =
58+
format!("{name} register accessor: an alias for `Reg<{name_constant_case_spec}>`");
59+
out.extend(quote! {
60+
#[doc = #alias_doc]
61+
pub type #name_constant_case = crate::Reg<#name_snake_case::#name_constant_case_spec>;
62+
});
63+
let mod_items =
64+
render_register_mod(register, &path.new_register(&register.name), index, config)?;
7165

72-
out.extend(quote! {
73-
#[doc = #description]
74-
pub mod #name_snake_case {
75-
#mod_items
76-
}
77-
});
66+
out.extend(quote! {
67+
#[doc = #description]
68+
pub mod #name_snake_case {
69+
#mod_items
70+
}
71+
});
72+
};
7873

7974
Ok(out)
8075
}

src/util.rs

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -367,38 +367,11 @@ pub fn array_proxy_type(ty: Type, array_info: &DimElement) -> Type {
367367
Type::Path(type_path(segments))
368368
}
369369

370-
pub fn name_to_ty(name: &str) -> TypePath {
370+
pub fn name_to_ty(name: &str) -> Type {
371371
let span = Span::call_site();
372372
let mut segments = Punctuated::new();
373-
segments.push(path_segment(name.to_snake_case_ident(span)));
374373
segments.push(path_segment(name.to_constant_case_ident(span)));
375-
type_path(segments)
376-
}
377-
378-
pub fn name_to_wrapped_ty(name: &str) -> Type {
379-
let span = Span::call_site();
380-
let mut segments = Punctuated::new();
381-
segments.push(path_segment(name.to_snake_case_ident(span)));
382-
segments.push(path_segment(
383-
format!("{name}_SPEC").to_constant_case_ident(span),
384-
));
385-
let inner_path = GenericArgument::Type(Type::Path(type_path(segments)));
386-
let mut args = Punctuated::new();
387-
args.push(inner_path);
388-
let arguments = PathArguments::AngleBracketed(AngleBracketedGenericArguments {
389-
colon2_token: None,
390-
lt_token: Token![<](span),
391-
args,
392-
gt_token: Token![>](span),
393-
});
394-
395-
let mut segments = Punctuated::new();
396-
segments.push(path_segment(Ident::new("crate", span)));
397-
segments.push(PathSegment {
398-
ident: Ident::new("Reg", span),
399-
arguments,
400-
});
401-
Type::Path(type_path(segments))
374+
syn::Type::Path(type_path(segments))
402375
}
403376

404377
pub fn block_path_to_ty(bpath: &svd_parser::expand::BlockPath, span: Span) -> TypePath {

0 commit comments

Comments
 (0)