Skip to content

Commit 445a238

Browse files
wezburrbull
authored andcommitted
simplify union removal, and remove all traces of unions
1 parent 225f51a commit 445a238

File tree

1 file changed

+14
-67
lines changed

1 file changed

+14
-67
lines changed

src/generate/peripheral.rs

Lines changed: 14 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -213,24 +213,9 @@ impl Region {
213213
self.shortest_ident()
214214
}
215215
}
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
234219
}
235220
}
236221

@@ -318,10 +303,6 @@ impl FieldRegions {
318303
Ok(())
319304
}
320305

321-
pub fn is_union(&self) -> bool {
322-
self.regions.len() == 1 && self.regions[0].fields.len() > 1
323-
}
324-
325306
/// Resolves type name conflicts
326307
pub fn resolve_idents(&mut self) -> Result<()> {
327308
let idents: Vec<_> = {
@@ -362,7 +343,7 @@ fn register_or_cluster_block(
362343
) -> Result<Tokens> {
363344
let mut fields = Tokens::new();
364345
let mut accessors = Tokens::new();
365-
let mut helper_types = Tokens::new();
346+
let mut have_accessors = false;
366347

367348
let ercs_expanded = expand(ercs, defs, name)?;
368349

@@ -373,7 +354,6 @@ fn register_or_cluster_block(
373354
regions.add(reg_block_field)?;
374355
}
375356

376-
let block_is_union = regions.is_union();
377357
// We need to compute the idents of each register/union block first to make sure no conflicts exists.
378358
regions.resolve_idents()?;
379359
// The end of the region from the prior iteration of the loop
@@ -393,6 +373,7 @@ fn register_or_cluster_block(
393373
last_end = region.end;
394374

395375
let mut region_fields = Tokens::new();
376+
let is_region_a_union = region.is_union();
396377

397378
for reg_block_field in &region.fields {
398379
if reg_block_field.offset != region.offset {
@@ -409,11 +390,12 @@ fn register_or_cluster_block(
409390
util::escape_brackets(util::respace(&reg_block_field.description).as_ref()),
410391
)[..];
411392

412-
if block_is_union {
393+
if is_region_a_union {
413394
let name = &reg_block_field.field.ident;
414395
let mut_name = Ident::new(format!("{}_mut", name.as_ref().unwrap()));
415396
let ty = &reg_block_field.field.ty;
416397
let offset = reg_block_field.offset as usize;
398+
have_accessors = true;
417399
accessors.append(quote! {
418400
#[doc = #comment]
419401
pub fn #name(&self) -> &#ty {
@@ -431,49 +413,16 @@ fn register_or_cluster_block(
431413
});
432414

433415
} else {
416+
region_fields.append(quote! {
417+
#[doc = #comment]
418+
});
434419

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);
441422
}
442423
}
443424

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 {
477426
fields.append(&region_fields);
478427
}
479428
}
@@ -483,7 +432,7 @@ fn register_or_cluster_block(
483432
None => "RegisterBlock".into(),
484433
});
485434

486-
let accessors = if block_is_union {
435+
let accessors = if have_accessors {
487436
quote! {
488437
impl #name {
489438
#accessors
@@ -501,8 +450,6 @@ fn register_or_cluster_block(
501450
}
502451

503452
#accessors
504-
505-
#helper_types
506453
})
507454
}
508455

0 commit comments

Comments
 (0)