Skip to content

Commit 6a16354

Browse files
committed
Try #190:
2 parents c1acfbd + a12280c commit 6a16354

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/generate/peripheral.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,38 @@ use util::{self, ToSanitizedSnakeCase, ToSanitizedUpperCase, BITS_PER_BYTE};
1212
use generate::register;
1313

1414
pub fn render(
15-
p: &Peripheral,
15+
p_original: &Peripheral,
1616
all_peripherals: &[Peripheral],
1717
defaults: &Defaults,
1818
nightly: bool,
1919
) -> Result<Vec<Tokens>> {
2020
let mut out = vec![];
2121

22+
let p_derivedfrom = p_original.derived_from.as_ref().and_then(|s| {
23+
all_peripherals.iter().find(|x| x.name == *s)
24+
});
25+
26+
let p_merged = p_derivedfrom.map(|x| p_original.derive_from(x));
27+
let p = p_merged.as_ref().unwrap_or(p_original);
28+
29+
if p_original.derived_from.is_some() && p_derivedfrom.is_none() {
30+
eprintln!("Couldn't find derivedFrom original: {} for {}, skipping",
31+
p_original.derived_from.as_ref().unwrap(), p_original.name);
32+
return Ok(out);
33+
}
34+
2235
let name_pc = Ident::new(&*p.name.to_sanitized_upper_case());
2336
let address = util::hex(p.base_address);
2437
let description =
2538
util::escape_brackets(util::respace(p.description.as_ref().unwrap_or(&p.name)).as_ref());
39+
let derive_regs = p_derivedfrom.is_some() && p_original.registers.is_none();
40+
2641

2742
let name_sc = Ident::new(&*p.name.to_sanitized_snake_case());
28-
let (base, derived) = if let Some(base) = p.derived_from.as_ref() {
29-
// TODO Verify that base exists
30-
// TODO We don't handle inheritance style `derivedFrom`, we should raise
31-
// an error in that case
32-
(Ident::new(&*base.to_sanitized_snake_case()), true)
43+
let base = if derive_regs {
44+
Ident::new(&*p_derivedfrom.unwrap().name.to_sanitized_snake_case())
3345
} else {
34-
(name_sc.clone(), false)
46+
name_sc.clone()
3547
};
3648

3749
// Insert the peripheral structure
@@ -57,9 +69,9 @@ pub fn render(
5769
}
5870
});
5971

60-
// Derived peripherals do not require re-implementation, and will instead
61-
// use a single definition of the non-derived version
62-
if derived {
72+
// Derived peripherals may not require re-implementation, and will instead
73+
// use a single definition of the non-derived version.
74+
if derive_regs {
6375
return Ok(out);
6476
}
6577

0 commit comments

Comments
 (0)