@@ -8,9 +8,12 @@ use crate::svd::{
88use log:: { debug, trace, warn} ;
99use proc_macro2:: { Ident , Punct , Spacing , Span , TokenStream } ;
1010use quote:: { quote, ToTokens } ;
11- use syn:: { parse_str , Token } ;
11+ use syn:: Token ;
1212
13- use crate :: util:: { self , unsuffixed, Config , FullName , ToSanitizedCase , BITS_PER_BYTE } ;
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 ,
16+ } ;
1417use anyhow:: { anyhow, bail, Context , Result } ;
1518
1619use crate :: generate:: register;
@@ -686,7 +689,7 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
686689 let span = Span :: call_site ( ) ;
687690 let mut accessors = TokenStream :: new ( ) ;
688691 let nb_name = util:: replace_suffix ( & info. name , "" ) ;
689- let ty = name_to_ty ( & nb_name) ? ;
692+ let ty = name_to_ty ( & nb_name) ;
690693 let nb_name_cs = nb_name. to_snake_case_ident ( span) ;
691694 for ( i, idx) in array_info. indexes ( ) . enumerate ( ) {
692695 let idx_name =
@@ -717,14 +720,13 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
717720 } else if sequential_indexes_from0 && config. const_generic {
718721 // Include a ZST ArrayProxy giving indexed access to the
719722 // elements.
720- cluster_expanded. push ( array_proxy ( info, array_info) ? ) ;
723+ cluster_expanded. push ( array_proxy ( info, array_info) ) ;
721724 } else {
722725 let ty_name = util:: replace_suffix ( & info. name , "" ) ;
723- let ty = name_to_ty ( & ty_name) ? ;
726+ let ty = syn :: Type :: Path ( name_to_ty ( & ty_name) ) ;
724727
725728 for ( field_num, idx) in array_info. indexes ( ) . enumerate ( ) {
726729 let nb_name = util:: replace_suffix ( & info. name , & idx) ;
727-
728730 let syn_field =
729731 new_syn_field ( nb_name. to_snake_case_ident ( Span :: call_site ( ) ) , ty. clone ( ) ) ;
730732
@@ -790,7 +792,7 @@ fn expand_register(register: &Register, config: &Config) -> Result<Vec<RegisterB
790792 let span = Span :: call_site ( ) ;
791793 let mut accessors = TokenStream :: new ( ) ;
792794 let nb_name = util:: replace_suffix ( & info. fullname ( config. ignore_groups ) , "" ) ;
793- let ty = name_to_wrapped_ty ( & nb_name) ? ;
795+ let ty = name_to_wrapped_ty ( & nb_name) ;
794796 let nb_name_cs = nb_name. to_snake_case_ident ( span) ;
795797 let info_name = info. fullname ( config. ignore_groups ) ;
796798 for ( i, idx) in array_info. indexes ( ) . enumerate ( ) {
@@ -822,11 +824,10 @@ fn expand_register(register: &Register, config: &Config) -> Result<Vec<RegisterB
822824 } else {
823825 let info_name = info. fullname ( config. ignore_groups ) ;
824826 let ty_name = util:: replace_suffix ( & info_name, "" ) ;
825- let ty = name_to_wrapped_ty ( & ty_name) ? ;
827+ let ty = name_to_wrapped_ty ( & ty_name) ;
826828
827829 for ( field_num, idx) in array_info. indexes ( ) . enumerate ( ) {
828830 let nb_name = util:: replace_suffix ( & info_name, & idx) ;
829-
830831 let syn_field =
831832 new_syn_field ( nb_name. to_snake_case_ident ( Span :: call_site ( ) ) , ty. clone ( ) ) ;
832833
@@ -912,7 +913,7 @@ fn cluster_block(
912913
913914 let name_snake_case = mod_name. to_snake_case_ident ( Span :: call_site ( ) ) ;
914915
915- let struct_path = name_to_ty ( & mod_name) ? ;
916+ let struct_path = name_to_ty ( & mod_name) ;
916917
917918 Ok ( quote ! {
918919 #[ doc = #description]
@@ -933,15 +934,13 @@ fn register_to_syn_field(register: &Register, ignore_group: bool) -> Result<syn:
933934 Ok ( match register {
934935 Register :: Single ( info) => {
935936 let info_name = info. fullname ( ignore_group) ;
936- let ty = name_to_wrapped_ty ( & info_name)
937- . with_context ( || format ! ( "Error converting register name {info_name}" ) ) ?;
937+ let ty = name_to_wrapped_ty ( & info_name) ;
938938 new_syn_field ( info_name. to_snake_case_ident ( Span :: call_site ( ) ) , ty)
939939 }
940940 Register :: Array ( info, array_info) => {
941941 let info_name = info. fullname ( ignore_group) ;
942942 let nb_name = util:: replace_suffix ( & info_name, "" ) ;
943- let ty = name_to_wrapped_ty ( & nb_name)
944- . with_context ( || format ! ( "Error converting register name {nb_name}" ) ) ?;
943+ let ty = name_to_wrapped_ty ( & nb_name) ;
945944 let array_ty = new_syn_array ( ty, array_info. dim ) ;
946945
947946 new_syn_field ( nb_name. to_snake_case_ident ( Span :: call_site ( ) ) , array_ty)
@@ -950,42 +949,31 @@ fn register_to_syn_field(register: &Register, ignore_group: bool) -> Result<syn:
950949}
951950
952951/// Return an syn::Type for an ArrayProxy.
953- fn array_proxy (
954- info : & ClusterInfo ,
955- array_info : & DimElement ,
956- ) -> Result < RegisterBlockField , syn:: Error > {
952+ fn array_proxy ( info : & ClusterInfo , array_info : & DimElement ) -> RegisterBlockField {
957953 let ty_name = util:: replace_suffix ( & info. name , "" ) ;
958- let tys = name_to_ty_str ( & ty_name) ;
959-
960- let ap_path = parse_str :: < syn:: TypePath > ( & format ! (
961- "crate::ArrayProxy<{tys}, {}, {}>" ,
962- array_info. dim,
963- util:: hex( array_info. dim_increment as u64 ) . into_token_stream( ) ,
964- ) ) ?;
965-
966- Ok ( RegisterBlockField {
967- syn_field : new_syn_field (
968- ty_name. to_snake_case_ident ( Span :: call_site ( ) ) ,
969- ap_path. into ( ) ,
970- ) ,
954+ let ty = name_to_ty ( & ty_name) ;
955+
956+ let ap_path = array_proxy_type ( ty, array_info) ;
957+
958+ RegisterBlockField {
959+ syn_field : new_syn_field ( ty_name. to_snake_case_ident ( Span :: call_site ( ) ) , ap_path) ,
971960 description : info. description . as_ref ( ) . unwrap_or ( & info. name ) . into ( ) ,
972961 offset : info. address_offset ,
973962 size : 0 ,
974963 accessors : None ,
975- } )
964+ }
976965}
977966
978967/// Convert a parsed `Cluster` into its `Field` equivalent
979968fn cluster_to_syn_field ( cluster : & Cluster ) -> Result < syn:: Field , syn:: Error > {
980969 Ok ( match cluster {
981970 Cluster :: Single ( info) => {
982- let ty_name = util:: replace_suffix ( & info. name , "" ) ;
983- let ty = name_to_ty ( & ty_name) ?;
971+ let ty = syn:: Type :: Path ( name_to_ty ( & info. name ) ) ;
984972 new_syn_field ( info. name . to_snake_case_ident ( Span :: call_site ( ) ) , ty)
985973 }
986974 Cluster :: Array ( info, array_info) => {
987975 let ty_name = util:: replace_suffix ( & info. name , "" ) ;
988- let ty = name_to_ty ( & ty_name) ? ;
976+ let ty = syn :: Type :: Path ( name_to_ty ( & ty_name) ) ;
989977 let array_ty = new_syn_array ( ty, array_info. dim ) ;
990978
991979 new_syn_field ( ty_name. to_snake_case_ident ( Span :: call_site ( ) ) , array_ty)
@@ -1015,39 +1003,3 @@ fn new_syn_array(ty: syn::Type, len: u32) -> syn::Type {
10151003 len : new_syn_u32 ( len, span) ,
10161004 } )
10171005}
1018-
1019- fn new_syn_u32 ( len : u32 , span : Span ) -> syn:: Expr {
1020- syn:: Expr :: Lit ( syn:: ExprLit {
1021- attrs : Vec :: new ( ) ,
1022- lit : syn:: Lit :: Int ( syn:: LitInt :: new ( & len. to_string ( ) , span) ) ,
1023- } )
1024- }
1025-
1026- fn name_to_ty_str ( name : & str ) -> String {
1027- format ! (
1028- "{}::{}" ,
1029- name. to_sanitized_snake_case( ) ,
1030- name. to_sanitized_constant_case( )
1031- )
1032- }
1033-
1034- fn name_to_ty ( name : & str ) -> Result < syn:: Type , syn:: Error > {
1035- let ident = name_to_ty_str ( name) ;
1036- parse_str :: < syn:: TypePath > ( & ident) . map ( syn:: Type :: Path )
1037- }
1038-
1039- fn name_to_wrapped_ty_str ( name : & str ) -> String {
1040- format ! (
1041- "crate::Reg<{}::{}_SPEC>" ,
1042- & name. to_sanitized_snake_case( ) ,
1043- & name. to_sanitized_constant_case( ) ,
1044- )
1045- }
1046-
1047- fn name_to_wrapped_ty ( name : & str ) -> Result < syn:: Type > {
1048- let ident = name_to_wrapped_ty_str ( name) ;
1049- parse_str :: < syn:: TypePath > ( & ident)
1050- . map ( syn:: Type :: Path )
1051- . map_err ( anyhow:: Error :: from)
1052- . with_context ( || format ! ( "Determining syn::TypePath from ident \" {ident}\" failed" ) )
1053- }
0 commit comments