@@ -9,14 +9,79 @@ use proc_macro2::{Ident, Punct, Spacing, Span, TokenStream};
9
9
use quote:: { quote, ToTokens } ;
10
10
use std:: collections:: HashSet ;
11
11
use svd_parser:: expand:: {
12
- derive_enumerated_values, derive_field, EnumPath , FieldPath , Index , RegisterPath ,
12
+ derive_enumerated_values, derive_field, BlockPath , EnumPath , FieldPath , Index , RegisterPath ,
13
13
} ;
14
14
15
15
use crate :: util:: { self , ident_to_path, path_segment, type_path, Config , ToSanitizedCase , U32Ext } ;
16
16
use anyhow:: { anyhow, Result } ;
17
17
use syn:: punctuated:: Punctuated ;
18
18
19
19
pub fn render (
20
+ register : & Register ,
21
+ path : & BlockPath ,
22
+ dpath : & Option < RegisterPath > ,
23
+ index : & Index ,
24
+ config : & Config ,
25
+ ) -> Result < TokenStream > {
26
+ let mut out = TokenStream :: new ( ) ;
27
+ let name = util:: name_of ( register, config. ignore_groups ) ;
28
+ let span = Span :: call_site ( ) ;
29
+ let name_constant_case = name. to_constant_case_ident ( span) ;
30
+ let name_constant_case_spec = format ! ( "{name}_SPEC" ) . to_constant_case_ident ( span) ;
31
+ let name_snake_case = name. to_snake_case_ident ( span) ;
32
+ let description = util:: escape_brackets (
33
+ util:: respace ( & register. description . clone ( ) . unwrap_or_else ( || {
34
+ warn ! ( "Missing description for register {}" , register. name) ;
35
+ Default :: default ( )
36
+ } ) )
37
+ . as_ref ( ) ,
38
+ ) ;
39
+ let alias_doc =
40
+ format ! ( "{name} register accessor: an alias for `Reg<{name_constant_case_spec}>`" ) ;
41
+ let wrapped_name = util:: name_to_wrapped_ty ( & name) ;
42
+ out. extend ( quote ! {
43
+ #[ doc = #alias_doc]
44
+ pub type #name_constant_case = #wrapped_name;
45
+ } ) ;
46
+
47
+ let mod_items = if let Some ( dpath) = dpath {
48
+ let mut mod_items = TokenStream :: new ( ) ;
49
+ let mut derived_spec = if & dpath. block == path {
50
+ let mut segments = Punctuated :: new ( ) ;
51
+ segments. push ( path_segment ( Ident :: new ( "super" , span) ) ) ;
52
+ type_path ( segments)
53
+ } else {
54
+ util:: block_path_to_ty ( & dpath. block , span)
55
+ } ;
56
+ let dname = util:: name_of ( index. registers . get ( dpath) . unwrap ( ) , config. ignore_groups ) ;
57
+ derived_spec
58
+ . path
59
+ . segments
60
+ . push ( path_segment ( dname. to_snake_case_ident ( span) ) ) ;
61
+ derived_spec. path . segments . push ( path_segment (
62
+ format ! ( "{}_SPEC" , dname) . to_constant_case_ident ( span) ,
63
+ ) ) ;
64
+
65
+ mod_items. extend ( quote ! {
66
+ #[ doc = #description]
67
+ pub use #derived_spec as #name_constant_case_spec;
68
+ } ) ;
69
+ mod_items
70
+ } else {
71
+ render_register_mod ( register, & path. new_register ( & register. name ) , index, config) ?
72
+ } ;
73
+
74
+ out. extend ( quote ! {
75
+ #[ doc = #description]
76
+ pub mod #name_snake_case {
77
+ #mod_items
78
+ }
79
+ } ) ;
80
+
81
+ Ok ( out)
82
+ }
83
+
84
+ pub fn render_register_mod (
20
85
register : & Register ,
21
86
path : & RegisterPath ,
22
87
index : & Index ,
@@ -26,7 +91,6 @@ pub fn render(
26
91
let access = util:: access_of ( properties, register. fields . as_deref ( ) ) ;
27
92
let name = util:: name_of ( register, config. ignore_groups ) ;
28
93
let span = Span :: call_site ( ) ;
29
- let name_constant_case = name. to_constant_case_ident ( span) ;
30
94
let name_constant_case_spec = format ! ( "{name}_SPEC" ) . to_constant_case_ident ( span) ;
31
95
let name_snake_case = name. to_snake_case_ident ( span) ;
32
96
let rsize = properties
@@ -285,28 +349,7 @@ pub fn render(
285
349
}
286
350
} ) ;
287
351
}
288
-
289
- let mut out = TokenStream :: new ( ) ;
290
- let alias_doc =
291
- format ! ( "{name} register accessor: an alias for `Reg<{name_constant_case_spec}>`" ) ;
292
- let wrapped_name = util:: name_to_wrapped_ty ( & name) ;
293
- out. extend ( quote ! {
294
- #[ doc = #alias_doc]
295
- pub type #name_constant_case = #wrapped_name;
296
- } ) ;
297
-
298
- out. extend ( quote ! {
299
- #[ doc = #description]
300
- pub mod #name_snake_case #open
301
- } ) ;
302
-
303
- out. extend ( mod_items) ;
304
-
305
- out. extend ( quote ! {
306
- #close
307
- } ) ;
308
-
309
- Ok ( out)
352
+ Ok ( mod_items)
310
353
}
311
354
312
355
#[ allow( clippy:: too_many_arguments) ]
0 commit comments