Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First pass at implementing derivedFrom. #190

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions src/generate/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,38 @@ use util::{self, ToSanitizedSnakeCase, ToSanitizedUpperCase, BITS_PER_BYTE};
use generate::register;

pub fn render(
p: &Peripheral,
p_original: &Peripheral,
all_peripherals: &[Peripheral],
defaults: &Defaults,
nightly: bool,
) -> Result<Vec<Tokens>> {
let mut out = vec![];

let p_derivedfrom = p_original.derived_from.as_ref().and_then(|s| {
all_peripherals.iter().find(|x| x.name == *s)
});

let p_merged = p_derivedfrom.map(|x| p_original.derive_from(x));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where does the Peripheral::derive_from(&mut self, p: Peripheral) method come from? (this might have got disappeared during rebase..?)

let p = p_merged.as_ref().unwrap_or(p_original);

if p_original.derived_from.is_some() && p_derivedfrom.is_none() {
eprintln!("Couldn't find derivedFrom original: {} for {}, skipping",
p_original.derived_from.as_ref().unwrap(), p_original.name);
return Ok(out);
}

let name_pc = Ident::new(&*p.name.to_sanitized_upper_case());
let address = util::hex(p.base_address);
let description =
util::escape_brackets(util::respace(p.description.as_ref().unwrap_or(&p.name)).as_ref());
let derive_regs = p_derivedfrom.is_some() && p_original.registers.is_none();


let name_sc = Ident::new(&*p.name.to_sanitized_snake_case());
let (base, derived) = if let Some(base) = p.derived_from.as_ref() {
// TODO Verify that base exists
// TODO We don't handle inheritance style `derivedFrom`, we should raise
// an error in that case
(Ident::new(&*base.to_sanitized_snake_case()), true)
let base = if derive_regs {
Ident::new(&*p_derivedfrom.unwrap().name.to_sanitized_snake_case())
} else {
(name_sc.clone(), false)
name_sc.clone()
};

// Insert the peripheral structure
Expand All @@ -57,9 +69,9 @@ pub fn render(
}
});

// Derived peripherals do not require re-implementation, and will instead
// use a single definition of the non-derived version
if derived {
// Derived peripherals may not require re-implementation, and will instead
// use a single definition of the non-derived version.
if derive_regs {
return Ok(out);
}

Expand Down