Skip to content

Commit

Permalink
The variable alias of the derived source is created from the enumerat…
Browse files Browse the repository at this point in the history
…edValues element that has the derivedFrom attribute.

From the enumeratedValues element with derivedFrom attribute, FieldReader and FieldWriter aliases are created, but variable aliases are not created. Therefore, variable aliases should be created.
  • Loading branch information
seisyuu-hantatsushi committed Feb 5, 2024
1 parent 121ca49 commit c7f632c
Showing 1 changed file with 47 additions and 9 deletions.
56 changes: 47 additions & 9 deletions src/generate/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,13 +599,9 @@ pub fn fields(
let mut lookup_results = Vec::new();
for mut ev in f.enumerated_values.clone().into_iter() {
let mut epath = None;
let dpath = ev.derived_from.take();
if let Some(dpath) = dpath {
if let Some(dpath) = ev.derived_from.clone() {
epath = Some(derive_enumerated_values(&mut ev, &dpath, &fpath, index)?);
// TODO: remove this hack
if let Some(epath) = epath.as_ref() {
ev = (*index.evs.get(epath).unwrap()).clone();
}
ev.name = Some(f.name.clone());
} else if let Some(path) = fdpath.as_ref() {
epath = Some(path.new_enum(ev.name.clone().unwrap_or_else(|| path.name.clone())));
}
Expand Down Expand Up @@ -712,6 +708,7 @@ pub fn fields(
// if this is an enumeratedValues not derived from base, generate the enum structure
// and implement functions for each value in enumeration.
if let Some((evs, None)) = lookup_filter(&lookup_results, Usage::Read) {

// we have enumeration for read, record this. If the enumeration for write operation
// later on is the same as the read enumeration, we reuse and do not generate again.
evs_r = Some(evs);
Expand Down Expand Up @@ -871,11 +868,17 @@ pub fn fields(
}
}

// if this value is derived from a base, generate `pub use` code for each read proxy and value
// if this value is derived from a base, generate `pub use` code for each read proxy and
// `type` code for alias of base value
// if necessary.
if let Some((evs, Some(base))) = lookup_filter(&lookup_results, Usage::Read) {

// preserve value; if read type equals write type, writer would not generate value type again
evs_r = Some(evs);

// generate type field_2 = field_1
add_variant_alias(mod_items, &evs, &base, &description);

// generate pub use field_1 reader as field_2 reader
let base_field = util::replace_suffix(&base.field.name, "");
let base_r = ident(&base_field, &config.ident_formats.field_reader, span);
Expand Down Expand Up @@ -1150,9 +1153,15 @@ pub fn fields(
// the generated and source write proxy are in the same module.
// we never reuse writer for writer in different module does not have the same _SPEC strcuture,
// thus we cannot write to current register using re-exported write proxy.
let writer_reader_different_enum = evs_r != Some(evs);

// generate pub use field_1 writer as field_2 writer
let base_field = util::replace_suffix(&base.field.name, "");
// generate type field_2 = field_1 if enum is different from reader enum.
if writer_reader_different_enum {
add_variant_alias(mod_items, &evs, &base, &description);
}

// generate pub use field_1 writer as field_2 writer
let base_w = ident(&base_field, &config.ident_formats.field_writer, span);
if !writer_derives.contains(&writer_ty) {
let base_path = base_syn_path(base, &fpath, &base_w)?;
Expand All @@ -1164,7 +1173,6 @@ pub fn fields(
}
// if base.register == None, it emits pub use structure from same module.
if base.register() != fpath.register() {
let writer_reader_different_enum = evs_r != Some(evs);
if writer_reader_different_enum {
// use the same enum structure name
if !writer_enum_derives.contains(&value_write_ty) {
Expand Down Expand Up @@ -1420,6 +1428,36 @@ fn add_from_variants<'a>(
}
}

fn add_variant_alias(mod_items: &mut TokenStream, evs: &EnumeratedValues, base: &EnumPath, desc: &str){
let base_name = util::replace_suffix(&base.name,"");
let derived_name = if let Some(ref name) = &evs.name {
if let Some(ref usage) = &evs.usage {
match usage {
Usage::Read => Some(name.to_owned() + "R"),
Usage::Write => Some(name.to_owned() + "W"),
Usage::ReadWrite => Some(name.to_owned()),
}
}
else {
Some(name.to_owned())
}
}
else {
None
};

if let Some(ref derived_name) = derived_name {
let span = Span::call_site();
let basetype = Ident::new(&base_name, span);
let derivedtype = Ident::new(&derived_name, span);
mod_items.extend(quote! {
#[doc = #desc]
pub type #derivedtype = #basetype;
});
}

}

fn calculate_offset(increment: u32, offset: u64, with_parentheses: bool) -> TokenStream {
let mut res = quote! { n };
if increment != 1 {
Expand Down

0 comments on commit c7f632c

Please sign in to comment.