@@ -8,11 +8,11 @@ use crate::svd::{
88use log:: { debug, trace, warn} ;
99use proc_macro2:: { Ident , Punct , Spacing , Span , TokenStream } ;
1010use quote:: { quote, ToTokens } ;
11- use syn:: Token ;
11+ use syn:: { punctuated :: Punctuated , Token } ;
1212
1313use 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 ,
1616} ;
1717use anyhow:: { anyhow, bail, Context , Result } ;
1818
@@ -864,8 +864,7 @@ fn render_ercs(
864864 if let Some ( dpath) = dpath {
865865 cpath = derive_cluster ( c, & dpath, path, index) ?;
866866 }
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) ?) ;
869868 }
870869
871870 // Generate definition for each of the registers.
@@ -878,8 +877,8 @@ fn render_ercs(
878877 }
879878 let reg_name = & reg. name ;
880879
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 ( || {
883882 let descrip = reg. description . as_deref ( ) . unwrap_or ( "No description" ) ;
884883 format ! (
885884 "Error rendering register\n Name: {reg_name}\n Description: {descrip}"
@@ -896,33 +895,65 @@ fn render_ercs(
896895fn cluster_block (
897896 c : & mut Cluster ,
898897 path : & BlockPath ,
898+ dpath : Option < BlockPath > ,
899899 index : & Index ,
900900 config : & Config ,
901901) -> 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.
910902 let description =
911903 util:: escape_brackets ( & util:: respace ( c. description . as_ref ( ) . unwrap_or ( & c. name ) ) ) ;
904+ let mod_name = util:: replace_suffix ( & c. name , "" ) ;
912905
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) ;
914910
915911 let struct_path = name_to_ty ( & mod_name) ;
916912
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+
917950 Ok ( quote ! {
918951 #[ doc = #description]
919952 pub use #struct_path;
920953
921954 ///Cluster
922955 #[ doc = #description]
923956 pub mod #name_snake_case {
924- #reg_block
925-
926957 #mod_items
927958 }
928959 } )
0 commit comments