Skip to content

Commit dd663bd

Browse files
nsabovicwez
authored andcommitted
First pass at implementing derivedFrom.
1 parent 77ce4d5 commit dd663bd

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

src/generate/peripheral.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,36 @@ 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 = util::respace(p.description.as_ref().unwrap_or(&p.name));
38+
let derive_regs = p_derivedfrom.is_some() && p_original.registers.is_none();
2539

2640
let name_sc = Ident::new(&*p.name.to_sanitized_snake_case());
27-
let (base, derived) = if let Some(base) = p.derived_from.as_ref() {
28-
// TODO Verify that base exists
29-
// TODO We don't handle inheritance style `derivedFrom`, we should raise
30-
// an error in that case
31-
(Ident::new(&*base.to_sanitized_snake_case()), true)
41+
let base = if derive_regs {
42+
Ident::new(&*p_derivedfrom.unwrap().name.to_sanitized_snake_case());
3243
} else {
33-
(name_sc.clone(), false)
44+
name_sc.clone();
3445
};
3546

3647
// Insert the peripheral structure
@@ -56,9 +67,9 @@ pub fn render(
5667
}
5768
});
5869

59-
// Derived peripherals do not require re-implementation, and will instead
60-
// use a single definition of the non-derived version
61-
if derived {
70+
// Derived peripherals may not require re-implementation, and will instead
71+
// use a single definition of the non-derived version.
72+
if derive_regs {
6273
return Ok(out);
6374
}
6475

0 commit comments

Comments
 (0)