@@ -213,24 +213,9 @@ impl Region {
213
213
self . shortest_ident ( )
214
214
}
215
215
}
216
- /// Return a description of this region
217
- fn description ( & self ) -> String {
218
- let mut result = String :: new ( ) ;
219
- for f in & self . fields {
220
- // In the Atmel SVDs the union variants all tend to
221
- // have the same description. Rather than emitting
222
- // the same text three times over, only join in the
223
- // text from the other variants if it is different.
224
- // This isn't a foolproof way of emitting the most
225
- // reasonable short description, but it's good enough.
226
- if f. description != result {
227
- if !result. is_empty ( ) {
228
- result. push ( ' ' ) ;
229
- }
230
- result. push_str ( & f. description ) ;
231
- }
232
- }
233
- result
216
+
217
+ fn is_union ( & self ) -> bool {
218
+ self . fields . len ( ) > 1
234
219
}
235
220
}
236
221
@@ -318,10 +303,6 @@ impl FieldRegions {
318
303
Ok ( ( ) )
319
304
}
320
305
321
- pub fn is_union ( & self ) -> bool {
322
- self . regions . len ( ) == 1 && self . regions [ 0 ] . fields . len ( ) > 1
323
- }
324
-
325
306
/// Resolves type name conflicts
326
307
pub fn resolve_idents ( & mut self ) -> Result < ( ) > {
327
308
let idents: Vec < _ > = {
@@ -362,7 +343,7 @@ fn register_or_cluster_block(
362
343
) -> Result < Tokens > {
363
344
let mut fields = Tokens :: new ( ) ;
364
345
let mut accessors = Tokens :: new ( ) ;
365
- let mut helper_types = Tokens :: new ( ) ;
346
+ let mut have_accessors = false ;
366
347
367
348
let ercs_expanded = expand ( ercs, defs, name) ?;
368
349
@@ -373,7 +354,6 @@ fn register_or_cluster_block(
373
354
regions. add ( reg_block_field) ?;
374
355
}
375
356
376
- let block_is_union = regions. is_union ( ) ;
377
357
// We need to compute the idents of each register/union block first to make sure no conflicts exists.
378
358
regions. resolve_idents ( ) ?;
379
359
// The end of the region from the prior iteration of the loop
@@ -393,6 +373,7 @@ fn register_or_cluster_block(
393
373
last_end = region. end ;
394
374
395
375
let mut region_fields = Tokens :: new ( ) ;
376
+ let is_region_a_union = region. is_union ( ) ;
396
377
397
378
for reg_block_field in & region. fields {
398
379
if reg_block_field. offset != region. offset {
@@ -409,11 +390,12 @@ fn register_or_cluster_block(
409
390
util:: escape_brackets( util:: respace( & reg_block_field. description) . as_ref( ) ) ,
410
391
) [ ..] ;
411
392
412
- if block_is_union {
393
+ if is_region_a_union {
413
394
let name = & reg_block_field. field . ident ;
414
395
let mut_name = Ident :: new ( format ! ( "{}_mut" , name. as_ref( ) . unwrap( ) ) ) ;
415
396
let ty = & reg_block_field. field . ty ;
416
397
let offset = reg_block_field. offset as usize ;
398
+ have_accessors = true ;
417
399
accessors. append ( quote ! {
418
400
#[ doc = #comment]
419
401
pub fn #name( & self ) -> & #ty {
@@ -431,49 +413,16 @@ fn register_or_cluster_block(
431
413
} ) ;
432
414
433
415
} else {
416
+ region_fields. append ( quote ! {
417
+ #[ doc = #comment]
418
+ } ) ;
434
419
435
- region_fields. append ( quote ! {
436
- #[ doc = #comment]
437
- } ) ;
438
-
439
- reg_block_field. field . to_tokens ( & mut region_fields) ;
440
- Ident :: new ( "," ) . to_tokens ( & mut region_fields) ;
420
+ reg_block_field. field . to_tokens ( & mut region_fields) ;
421
+ Ident :: new ( "," ) . to_tokens ( & mut region_fields) ;
441
422
}
442
423
}
443
424
444
- if block_is_union {
445
- continue
446
- }
447
- if region. fields . len ( ) > 1 {
448
- let ( type_name, name) = match region. ident . clone ( ) {
449
- Some ( prefix) => (
450
- Ident :: new ( format ! ( "{}_UNION" , prefix. to_sanitized_upper_case( ) ) ) ,
451
- Ident :: new ( prefix) ,
452
- ) ,
453
- // If we can't find a name, fall back to the region index as a
454
- // unique-within-this-block identifier counter.
455
- None => {
456
- let ident = Ident :: new ( format ! ( "U{}" , i) ) ;
457
- ( ident. clone ( ) , ident)
458
- }
459
- } ;
460
-
461
- let description = region. description ( ) ;
462
-
463
- helper_types. append ( quote ! {
464
- #[ doc = #description]
465
- #[ repr( C ) ]
466
- pub union #type_name {
467
- #region_fields
468
- }
469
- } ) ;
470
-
471
- fields. append ( quote ! {
472
- #[ doc = #description]
473
- pub #name: #type_name
474
- } ) ;
475
- Ident :: new ( "," ) . to_tokens ( & mut fields) ;
476
- } else {
425
+ if !is_region_a_union {
477
426
fields. append ( & region_fields) ;
478
427
}
479
428
}
@@ -483,7 +432,7 @@ fn register_or_cluster_block(
483
432
None => "RegisterBlock" . into ( ) ,
484
433
} ) ;
485
434
486
- let accessors = if block_is_union {
435
+ let accessors = if have_accessors {
487
436
quote ! {
488
437
impl #name {
489
438
#accessors
@@ -501,8 +450,6 @@ fn register_or_cluster_block(
501
450
}
502
451
503
452
#accessors
504
-
505
- #helper_types
506
453
} )
507
454
}
508
455
0 commit comments