Skip to content

Avoid register type aliases #466

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

Merged
merged 2 commits into from
Aug 7, 2020
Merged
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
38 changes: 24 additions & 14 deletions src/generate/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ fn expand_svd_register(
for (idx, _i) in indices.iter().zip(0..) {
let nb_name = util::replace_suffix(&info.name, idx);

let ty = name_to_ty(&ty_name, name)?;
let ty = name_to_wrapped_ty(&ty_name, name)?;

out.push(new_syn_field(&nb_name.to_sanitized_snake_case(), ty));
}
Expand All @@ -810,28 +810,17 @@ fn expand_svd_register(

/// Convert a parsed `Register` into its `Field` equivalent
fn convert_svd_register(register: &Register, name: Option<&str>) -> Result<syn::Field, syn::Error> {
let name_to_ty_str = |name: &String, ns: Option<&str>| -> String {
if let Some(ns) = ns {
String::from("self::")
+ &ns.to_sanitized_snake_case()
+ "::"
+ &name.to_sanitized_upper_case()
} else {
name.to_sanitized_upper_case().to_string()
}
};

Ok(match register {
Register::Single(info) => new_syn_field(
&info.name.to_sanitized_snake_case(),
name_to_ty(&info.name, name)?,
name_to_wrapped_ty(&info.name, name)?,
),
Register::Array(info, array_info) => {
let nb_name = util::replace_suffix(&info.name, "");

let ty = syn::Type::Array(parse_str::<syn::TypeArray>(&format!(
"[{};{}]",
name_to_ty_str(&nb_name, name),
name_to_wrapped_ty_str(&nb_name, name),
u64::from(array_info.dim)
))?);

Expand Down Expand Up @@ -924,6 +913,27 @@ fn name_to_ty(name: &String, ns: Option<&str>) -> Result<syn::Type, syn::Error>
} else {
name.to_sanitized_upper_case()
};
Ok(syn::Type::Path(parse_str::<syn::TypePath>(&ident)?))
}

fn name_to_wrapped_ty_str(name: &String, ns: Option<&str>) -> String {
if let Some(ns) = ns {
format!(
"crate::Reg<self::{}::{}::{}_SPEC>",
&ns.to_sanitized_snake_case(),
&name.to_sanitized_snake_case(),
&name.to_sanitized_upper_case(),
)
} else {
format!(
"crate::Reg<{}::{}_SPEC>",
&name.to_sanitized_snake_case(),
&name.to_sanitized_upper_case(),
)
}
}

fn name_to_wrapped_ty(name: &String, ns: Option<&str>) -> Result<syn::Type, syn::Error> {
let ident = name_to_wrapped_ty_str(name, ns);
Ok(syn::Type::Path(parse_str::<syn::TypePath>(&ident)?))
}