@@ -10,10 +10,7 @@ use proc_macro2::{Ident, Punct, Spacing, Span, TokenStream};
1010use quote:: { quote, ToTokens } ;
1111use syn:: { parse_str, Token } ;
1212
13- use crate :: util:: {
14- self , handle_cluster_error, handle_reg_error, unsuffixed, Config , FullName , ToSanitizedCase ,
15- BITS_PER_BYTE ,
16- } ;
13+ use crate :: util:: { self , unsuffixed, Config , FullName , ToSanitizedCase , BITS_PER_BYTE } ;
1714use anyhow:: { anyhow, bail, Context , Result } ;
1815
1916use crate :: generate:: register;
@@ -570,26 +567,24 @@ fn expand(ercs: &[RegisterCluster], config: &Config) -> Result<Vec<RegisterBlock
570567 debug ! ( "Expanding registers or clusters into Register Block Fields" ) ;
571568 for erc in ercs {
572569 match & erc {
573- RegisterCluster :: Register ( register) => match expand_register ( register, config) {
574- Ok ( expanded_reg) => {
575- trace ! ( "Register: {}" , register. name) ;
576- ercs_expanded. extend ( expanded_reg) ;
577- }
578- Err ( e) => {
579- let res = Err ( e) ;
580- return handle_reg_error ( "Error expanding register" , register, res) ;
581- }
582- } ,
583- RegisterCluster :: Cluster ( cluster) => match expand_cluster ( cluster, config) {
584- Ok ( expanded_cluster) => {
585- trace ! ( "Cluster: {}" , cluster. name) ;
586- ercs_expanded. extend ( expanded_cluster) ;
587- }
588- Err ( e) => {
589- let res = Err ( e) ;
590- return handle_cluster_error ( "Error expanding register cluster" , cluster, res) ;
591- }
592- } ,
570+ RegisterCluster :: Register ( register) => {
571+ let reg_name = & register. name ;
572+ let expanded_reg = expand_register ( register, config) . with_context ( || {
573+ let descrip = register. description . as_deref ( ) . unwrap_or ( "No description" ) ;
574+ format ! ( "Error expanding register\n Name: {reg_name}\n Description: {descrip}" )
575+ } ) ?;
576+ trace ! ( "Register: {reg_name}" ) ;
577+ ercs_expanded. extend ( expanded_reg) ;
578+ }
579+ RegisterCluster :: Cluster ( cluster) => {
580+ let cluster_name = & cluster. name ;
581+ let expanded_cluster = expand_cluster ( cluster, config) . with_context ( || {
582+ let descrip = cluster. description . as_deref ( ) . unwrap_or ( "No description" ) ;
583+ format ! ( "Error expanding cluster\n Name: {cluster_name}\n Description: {descrip}" )
584+ } ) ?;
585+ trace ! ( "Cluster: {cluster_name}" ) ;
586+ ercs_expanded. extend ( expanded_cluster) ;
587+ }
593588 } ;
594589 }
595590
@@ -650,11 +645,16 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
650645
651646 let cluster_size = cluster_info_size_in_bits ( cluster, config)
652647 . with_context ( || format ! ( "Cluster {} has no determinable `size` field" , cluster. name) ) ?;
648+ let description = cluster
649+ . description
650+ . as_ref ( )
651+ . unwrap_or ( & cluster. name )
652+ . to_string ( ) ;
653653
654654 match cluster {
655655 Cluster :: Single ( info) => cluster_expanded. push ( RegisterBlockField {
656- syn_field : convert_svd_cluster ( cluster) ?,
657- description : info . description . as_ref ( ) . unwrap_or ( & info . name ) . into ( ) ,
656+ syn_field : cluster_to_syn_field ( cluster) ?,
657+ description,
658658 offset : info. address_offset ,
659659 size : cluster_size,
660660 accessors : None ,
@@ -680,28 +680,21 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
680680 let array_convertible = sequential_addresses && convert_list;
681681
682682 if array_convertible {
683- if sequential_indexes_from0 {
684- cluster_expanded. push ( RegisterBlockField {
685- syn_field : convert_svd_cluster ( cluster) ?,
686- description : info. description . as_ref ( ) . unwrap_or ( & info. name ) . into ( ) ,
687- offset : info. address_offset ,
688- size : cluster_size * array_info. dim ,
689- accessors : None ,
690- } ) ;
683+ let accessors = if sequential_indexes_from0 {
684+ None
691685 } else {
692686 let span = Span :: call_site ( ) ;
693687 let mut accessors = TokenStream :: new ( ) ;
694688 let nb_name = util:: replace_suffix ( & info. name , "" ) ;
695689 let ty = name_to_ty ( & nb_name) ?;
696690 let nb_name_cs = nb_name. to_snake_case_ident ( span) ;
697- let description = info. description . as_ref ( ) . unwrap_or ( & info. name ) ;
698691 for ( i, idx) in array_info. indexes ( ) . enumerate ( ) {
699692 let idx_name =
700693 util:: replace_suffix ( & info. name , & idx) . to_snake_case_ident ( span) ;
701694 let comment = make_comment (
702695 cluster_size,
703696 info. address_offset + ( i as u32 ) * cluster_size / 8 ,
704- description,
697+ & description,
705698 ) ;
706699 let i = unsuffixed ( i as _ ) ;
707700 accessors. extend ( quote ! {
@@ -712,23 +705,32 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
712705 }
713706 } ) ;
714707 }
715- cluster_expanded. push ( RegisterBlockField {
716- syn_field : convert_svd_cluster ( cluster) ?,
717- description : description. into ( ) ,
718- offset : info. address_offset ,
719- size : cluster_size * array_info. dim ,
720- accessors : Some ( accessors) ,
721- } ) ;
722- }
708+ Some ( accessors)
709+ } ;
710+ cluster_expanded. push ( RegisterBlockField {
711+ syn_field : cluster_to_syn_field ( cluster) ?,
712+ description,
713+ offset : info. address_offset ,
714+ size : cluster_size * array_info. dim ,
715+ accessors,
716+ } ) ;
723717 } else if sequential_indexes_from0 && config. const_generic {
724718 // Include a ZST ArrayProxy giving indexed access to the
725719 // elements.
726720 cluster_expanded. push ( array_proxy ( info, array_info) ?) ;
727721 } else {
728- for ( field_num, syn_field) in expand_svd_cluster ( cluster) ?. into_iter ( ) . enumerate ( ) {
722+ let ty_name = util:: replace_suffix ( & info. name , "" ) ;
723+ let ty = name_to_ty ( & ty_name) ?;
724+
725+ for ( field_num, idx) in array_info. indexes ( ) . enumerate ( ) {
726+ let nb_name = util:: replace_suffix ( & info. name , & idx) ;
727+
728+ let syn_field =
729+ new_syn_field ( nb_name. to_snake_case_ident ( Span :: call_site ( ) ) , ty. clone ( ) ) ;
730+
729731 cluster_expanded. push ( RegisterBlockField {
730732 syn_field,
731- description : info . description . as_ref ( ) . unwrap_or ( & info . name ) . into ( ) ,
733+ description : description. clone ( ) ,
732734 offset : info. address_offset + field_num as u32 * array_info. dim_increment ,
733735 size : cluster_size,
734736 accessors : None ,
@@ -750,12 +752,13 @@ fn expand_register(register: &Register, config: &Config) -> Result<Vec<RegisterB
750752 . properties
751753 . size
752754 . ok_or_else ( || anyhow ! ( "Register {} has no `size` field" , register. name) ) ?;
755+ let description = register. description . clone ( ) . unwrap_or_default ( ) ;
753756
754757 match register {
755758 Register :: Single ( info) => register_expanded. push ( RegisterBlockField {
756- syn_field : convert_svd_register ( register, config. ignore_groups )
759+ syn_field : register_to_syn_field ( register, config. ignore_groups )
757760 . with_context ( || "syn error occured" ) ?,
758- description : info . description . clone ( ) . unwrap_or_default ( ) ,
761+ description,
759762 offset : info. address_offset ,
760763 size : register_size,
761764 accessors : None ,
@@ -781,21 +784,14 @@ fn expand_register(register: &Register, config: &Config) -> Result<Vec<RegisterB
781784 . filter ( |r| * r. start ( ) == 0 )
782785 . is_some ( ) ;
783786
784- if sequential_indexes_from0 {
785- register_expanded. push ( RegisterBlockField {
786- syn_field : convert_svd_register ( register, config. ignore_groups ) ?,
787- description : info. description . clone ( ) . unwrap_or_default ( ) ,
788- offset : info. address_offset ,
789- size : register_size * array_info. dim ,
790- accessors : None ,
791- } ) ;
787+ let accessors = if sequential_indexes_from0 {
788+ None
792789 } else {
793790 let span = Span :: call_site ( ) ;
794791 let mut accessors = TokenStream :: new ( ) ;
795792 let nb_name = util:: replace_suffix ( & info. fullname ( config. ignore_groups ) , "" ) ;
796793 let ty = name_to_wrapped_ty ( & nb_name) ?;
797794 let nb_name_cs = nb_name. to_snake_case_ident ( span) ;
798- let description = info. description . clone ( ) . unwrap_or_default ( ) ;
799795 let info_name = info. fullname ( config. ignore_groups ) ;
800796 for ( i, idx) in array_info. indexes ( ) . enumerate ( ) {
801797 let idx_name =
@@ -814,22 +810,29 @@ fn expand_register(register: &Register, config: &Config) -> Result<Vec<RegisterB
814810 }
815811 } ) ;
816812 }
817- register_expanded. push ( RegisterBlockField {
818- syn_field : convert_svd_register ( register, config. ignore_groups ) ?,
819- description,
820- offset : info. address_offset ,
821- size : register_size * array_info. dim ,
822- accessors : Some ( accessors) ,
823- } ) ;
824- }
813+ Some ( accessors)
814+ } ;
815+ register_expanded. push ( RegisterBlockField {
816+ syn_field : register_to_syn_field ( register, config. ignore_groups ) ?,
817+ description,
818+ offset : info. address_offset ,
819+ size : register_size * array_info. dim ,
820+ accessors,
821+ } ) ;
825822 } else {
826- for ( field_num, syn_field) in expand_svd_register ( register, config. ignore_groups ) ?
827- . into_iter ( )
828- . enumerate ( )
829- {
823+ let info_name = info. fullname ( config. ignore_groups ) ;
824+ let ty_name = util:: replace_suffix ( & info_name, "" ) ;
825+ let ty = name_to_wrapped_ty ( & ty_name) ?;
826+
827+ for ( field_num, idx) in array_info. indexes ( ) . enumerate ( ) {
828+ let nb_name = util:: replace_suffix ( & info_name, & idx) ;
829+
830+ let syn_field =
831+ new_syn_field ( nb_name. to_snake_case_ident ( Span :: call_site ( ) ) , ty. clone ( ) ) ;
832+
830833 register_expanded. push ( RegisterBlockField {
831834 syn_field,
832- description : info . description . clone ( ) . unwrap_or_default ( ) ,
835+ description : description. clone ( ) ,
833836 offset : info. address_offset + field_num as u32 * array_info. dim_increment ,
834837 size : register_size,
835838 accessors : None ,
@@ -872,14 +875,17 @@ fn render_ercs(
872875 if let Some ( dpath) = dpath {
873876 rpath = derive_register ( reg, & dpath, path, index) ?;
874877 }
875- let rpath = rpath. unwrap_or_else ( || path. new_register ( & reg. name ) ) ;
876- match register:: render ( reg, & rpath, index, config) {
877- Ok ( rendered_reg) => mod_items. extend ( rendered_reg) ,
878- Err ( e) => {
879- let res: Result < TokenStream > = Err ( e) ;
880- return handle_reg_error ( "Error rendering register" , reg, res) ;
881- }
882- } ;
878+ let reg_name = & reg. name ;
879+ let rpath = rpath. unwrap_or_else ( || path. new_register ( reg_name) ) ;
880+
881+ let rendered_reg =
882+ register:: render ( reg, & rpath, index, config) . with_context ( || {
883+ let descrip = reg. description . as_deref ( ) . unwrap_or ( "No description" ) ;
884+ format ! (
885+ "Error rendering register\n Name: {reg_name}\n Description: {descrip}"
886+ )
887+ } ) ?;
888+ mod_items. extend ( rendered_reg)
883889 }
884890 }
885891 }
@@ -922,31 +928,8 @@ fn cluster_block(
922928 } )
923929}
924930
925- /// Takes a svd::Register which may be a register array, and turn in into
926- /// a list of syn::Field where the register arrays have been expanded.
927- fn expand_svd_register ( register : & Register , ignore_group : bool ) -> Result < Vec < syn:: Field > > {
928- if let Register :: Array ( info, array_info) = register {
929- let ty_name = util:: replace_suffix ( & info. fullname ( ignore_group) , "" ) ;
930-
931- let mut out = vec ! [ ] ;
932- for idx in array_info. indexes ( ) {
933- let nb_name = util:: replace_suffix ( & info. fullname ( ignore_group) , & idx) ;
934-
935- let ty = name_to_wrapped_ty ( & ty_name) ?;
936-
937- out. push ( new_syn_field (
938- nb_name. to_snake_case_ident ( Span :: call_site ( ) ) ,
939- ty,
940- ) ) ;
941- }
942- Ok ( out)
943- } else {
944- Ok ( vec ! [ convert_svd_register( register, ignore_group) ?] )
945- }
946- }
947-
948931/// Convert a parsed `Register` into its `Field` equivalent
949- fn convert_svd_register ( register : & Register , ignore_group : bool ) -> Result < syn:: Field > {
932+ fn register_to_syn_field ( register : & Register , ignore_group : bool ) -> Result < syn:: Field > {
950933 Ok ( match register {
951934 Register :: Single ( info) => {
952935 let info_name = info. fullname ( ignore_group) ;
@@ -992,31 +975,8 @@ fn array_proxy(
992975 } )
993976}
994977
995- /// Takes a svd::Cluster which may contain a register array, and turn it into
996- /// a list of syn::Field where the register arrays have been expanded.
997- fn expand_svd_cluster ( cluster : & Cluster ) -> Result < Vec < syn:: Field > , syn:: Error > {
998- if let Cluster :: Array ( info, array_info) = cluster {
999- let ty_name = util:: replace_suffix ( & info. name , "" ) ;
1000-
1001- let mut out = vec ! [ ] ;
1002- for idx in array_info. indexes ( ) {
1003- let nb_name = util:: replace_suffix ( & info. name , & idx) ;
1004-
1005- let ty = name_to_ty ( & ty_name) ?;
1006-
1007- out. push ( new_syn_field (
1008- nb_name. to_snake_case_ident ( Span :: call_site ( ) ) ,
1009- ty,
1010- ) ) ;
1011- }
1012- Ok ( out)
1013- } else {
1014- Ok ( vec ! [ convert_svd_cluster( cluster) ?] )
1015- }
1016- }
1017-
1018978/// Convert a parsed `Cluster` into its `Field` equivalent
1019- fn convert_svd_cluster ( cluster : & Cluster ) -> Result < syn:: Field , syn:: Error > {
979+ fn cluster_to_syn_field ( cluster : & Cluster ) -> Result < syn:: Field , syn:: Error > {
1020980 Ok ( match cluster {
1021981 Cluster :: Single ( info) => {
1022982 let ty_name = util:: replace_suffix ( & info. name , "" ) ;
0 commit comments