@@ -8,11 +8,11 @@ use crate::svd::{
8
8
use log:: { debug, trace, warn} ;
9
9
use proc_macro2:: { Ident , Punct , Spacing , Span , TokenStream } ;
10
10
use quote:: { quote, ToTokens } ;
11
- use syn:: Token ;
11
+ use syn:: { punctuated :: Punctuated , Token } ;
12
12
13
13
use crate :: util:: {
14
- self , array_proxy_type, name_to_ty, name_to_wrapped_ty, new_syn_u32, unsuffixed , Config ,
15
- FullName , ToSanitizedCase , BITS_PER_BYTE ,
14
+ self , array_proxy_type, name_to_ty, name_to_wrapped_ty, new_syn_u32, path_segment , type_path ,
15
+ unsuffixed , Config , FullName , ToSanitizedCase , BITS_PER_BYTE ,
16
16
} ;
17
17
use anyhow:: { anyhow, bail, Context , Result } ;
18
18
@@ -864,8 +864,7 @@ fn render_ercs(
864
864
if let Some ( dpath) = dpath {
865
865
cpath = derive_cluster ( c, & dpath, path, index) ?;
866
866
}
867
- let cpath = cpath. unwrap_or_else ( || path. new_cluster ( & c. name ) ) ;
868
- mod_items. extend ( cluster_block ( c, & cpath, index, config) ?) ;
867
+ mod_items. extend ( cluster_block ( c, path, cpath, index, config) ?) ;
869
868
}
870
869
871
870
// Generate definition for each of the registers.
@@ -878,8 +877,8 @@ fn render_ercs(
878
877
}
879
878
let reg_name = & reg. name ;
880
879
881
- let rendered_reg = register :: render ( reg , path , & rpath , index , config )
882
- . with_context ( || {
880
+ let rendered_reg =
881
+ register :: render ( reg , path , rpath , index , config ) . with_context ( || {
883
882
let descrip = reg. description . as_deref ( ) . unwrap_or ( "No description" ) ;
884
883
format ! (
885
884
"Error rendering register\n Name: {reg_name}\n Description: {descrip}"
@@ -896,33 +895,65 @@ fn render_ercs(
896
895
fn cluster_block (
897
896
c : & mut Cluster ,
898
897
path : & BlockPath ,
898
+ dpath : Option < BlockPath > ,
899
899
index : & Index ,
900
900
config : & Config ,
901
901
) -> Result < TokenStream > {
902
- let mod_items = render_ercs ( & mut c. children , path, index, config) ?;
903
-
904
- // Generate the register block.
905
- let mod_name = util:: replace_suffix ( & c. name , "" ) ;
906
-
907
- let reg_block = register_or_cluster_block ( & c. children , Some ( & mod_name) , config) ?;
908
-
909
- // name_snake_case needs to take into account array type.
910
902
let description =
911
903
util:: escape_brackets ( & util:: respace ( c. description . as_ref ( ) . unwrap_or ( & c. name ) ) ) ;
904
+ let mod_name = util:: replace_suffix ( & c. name , "" ) ;
912
905
913
- let name_snake_case = mod_name. to_snake_case_ident ( Span :: call_site ( ) ) ;
906
+ // name_snake_case needs to take into account array type.
907
+ let span = Span :: call_site ( ) ;
908
+ let name_snake_case = mod_name. to_snake_case_ident ( span) ;
909
+ let name_constant_case = mod_name. to_constant_case_ident ( span) ;
914
910
915
911
let struct_path = name_to_ty ( & mod_name) ;
916
912
913
+ let mod_items = if let Some ( dpath) = dpath {
914
+ let dparent = util:: parent ( & dpath) ;
915
+ let mut derived = if & dparent == path {
916
+ let mut segments = Punctuated :: new ( ) ;
917
+ segments. push ( path_segment ( Ident :: new ( "super" , span) ) ) ;
918
+ type_path ( segments)
919
+ } else {
920
+ util:: block_path_to_ty ( & dparent, span)
921
+ } ;
922
+ let dname = util:: replace_suffix ( & index. clusters . get ( & dpath) . unwrap ( ) . name , "" ) ;
923
+ derived
924
+ . path
925
+ . segments
926
+ . push ( path_segment ( dname. to_snake_case_ident ( span) ) ) ;
927
+ derived
928
+ . path
929
+ . segments
930
+ . push ( path_segment ( dname. to_constant_case_ident ( span) ) ) ;
931
+
932
+ quote ! {
933
+ #[ doc = #description]
934
+ pub use #derived as #name_constant_case;
935
+ }
936
+ } else {
937
+ let cpath = path. new_cluster ( & c. name ) ;
938
+ let mod_items = render_ercs ( & mut c. children , & cpath, index, config) ?;
939
+
940
+ // Generate the register block.
941
+ let reg_block = register_or_cluster_block ( & c. children , Some ( & mod_name) , config) ?;
942
+
943
+ quote ! {
944
+ #reg_block
945
+
946
+ #mod_items
947
+ }
948
+ } ;
949
+
917
950
Ok ( quote ! {
918
951
#[ doc = #description]
919
952
pub use #struct_path;
920
953
921
954
///Cluster
922
955
#[ doc = #description]
923
956
pub mod #name_snake_case {
924
- #reg_block
925
-
926
957
#mod_items
927
958
}
928
959
} )
0 commit comments