@@ -8,9 +8,12 @@ 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:: { parse_str , Token } ;
11
+ use syn:: Token ;
12
12
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
+ } ;
14
17
use anyhow:: { anyhow, bail, Context , Result } ;
15
18
16
19
use crate :: generate:: register;
@@ -686,7 +689,7 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
686
689
let span = Span :: call_site ( ) ;
687
690
let mut accessors = TokenStream :: new ( ) ;
688
691
let nb_name = util:: replace_suffix ( & info. name , "" ) ;
689
- let ty = name_to_ty ( & nb_name) ? ;
692
+ let ty = name_to_ty ( & nb_name) ;
690
693
let nb_name_cs = nb_name. to_snake_case_ident ( span) ;
691
694
for ( i, idx) in array_info. indexes ( ) . enumerate ( ) {
692
695
let idx_name =
@@ -717,14 +720,13 @@ fn expand_cluster(cluster: &Cluster, config: &Config) -> Result<Vec<RegisterBloc
717
720
} else if sequential_indexes_from0 && config. const_generic {
718
721
// Include a ZST ArrayProxy giving indexed access to the
719
722
// elements.
720
- cluster_expanded. push ( array_proxy ( info, array_info) ? ) ;
723
+ cluster_expanded. push ( array_proxy ( info, array_info) ) ;
721
724
} else {
722
725
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) ) ;
724
727
725
728
for ( field_num, idx) in array_info. indexes ( ) . enumerate ( ) {
726
729
let nb_name = util:: replace_suffix ( & info. name , & idx) ;
727
-
728
730
let syn_field =
729
731
new_syn_field ( nb_name. to_snake_case_ident ( Span :: call_site ( ) ) , ty. clone ( ) ) ;
730
732
@@ -790,7 +792,7 @@ fn expand_register(register: &Register, config: &Config) -> Result<Vec<RegisterB
790
792
let span = Span :: call_site ( ) ;
791
793
let mut accessors = TokenStream :: new ( ) ;
792
794
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) ;
794
796
let nb_name_cs = nb_name. to_snake_case_ident ( span) ;
795
797
let info_name = info. fullname ( config. ignore_groups ) ;
796
798
for ( i, idx) in array_info. indexes ( ) . enumerate ( ) {
@@ -822,11 +824,10 @@ fn expand_register(register: &Register, config: &Config) -> Result<Vec<RegisterB
822
824
} else {
823
825
let info_name = info. fullname ( config. ignore_groups ) ;
824
826
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) ;
826
828
827
829
for ( field_num, idx) in array_info. indexes ( ) . enumerate ( ) {
828
830
let nb_name = util:: replace_suffix ( & info_name, & idx) ;
829
-
830
831
let syn_field =
831
832
new_syn_field ( nb_name. to_snake_case_ident ( Span :: call_site ( ) ) , ty. clone ( ) ) ;
832
833
@@ -912,7 +913,7 @@ fn cluster_block(
912
913
913
914
let name_snake_case = mod_name. to_snake_case_ident ( Span :: call_site ( ) ) ;
914
915
915
- let struct_path = name_to_ty ( & mod_name) ? ;
916
+ let struct_path = name_to_ty ( & mod_name) ;
916
917
917
918
Ok ( quote ! {
918
919
#[ doc = #description]
@@ -933,15 +934,13 @@ fn register_to_syn_field(register: &Register, ignore_group: bool) -> Result<syn:
933
934
Ok ( match register {
934
935
Register :: Single ( info) => {
935
936
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) ;
938
938
new_syn_field ( info_name. to_snake_case_ident ( Span :: call_site ( ) ) , ty)
939
939
}
940
940
Register :: Array ( info, array_info) => {
941
941
let info_name = info. fullname ( ignore_group) ;
942
942
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) ;
945
944
let array_ty = new_syn_array ( ty, array_info. dim ) ;
946
945
947
946
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:
950
949
}
951
950
952
951
/// 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 {
957
953
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) ,
971
960
description : info. description . as_ref ( ) . unwrap_or ( & info. name ) . into ( ) ,
972
961
offset : info. address_offset ,
973
962
size : 0 ,
974
963
accessors : None ,
975
- } )
964
+ }
976
965
}
977
966
978
967
/// Convert a parsed `Cluster` into its `Field` equivalent
979
968
fn cluster_to_syn_field ( cluster : & Cluster ) -> Result < syn:: Field , syn:: Error > {
980
969
Ok ( match cluster {
981
970
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 ) ) ;
984
972
new_syn_field ( info. name . to_snake_case_ident ( Span :: call_site ( ) ) , ty)
985
973
}
986
974
Cluster :: Array ( info, array_info) => {
987
975
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) ) ;
989
977
let array_ty = new_syn_array ( ty, array_info. dim ) ;
990
978
991
979
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 {
1015
1003
len : new_syn_u32 ( len, span) ,
1016
1004
} )
1017
1005
}
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