|
| 1 | +use quote::format_ident; |
| 2 | +use syn::{ |
| 3 | + parse_quote, Attribute, Fields, Ident, Item, ItemImpl, ItemStruct, ItemTrait, Path, Result, |
| 4 | + Signature, Visibility, |
| 5 | +}; |
| 6 | + |
| 7 | +fn validate_input(input: &ItemStruct) -> Result<()> { |
| 8 | + if !matches!(&input.fields, Fields::Named(_)) { |
| 9 | + bail!( |
| 10 | + &input.fields, |
| 11 | + "#[derive(Volatile)] can only be used on structs with named fields" |
| 12 | + ); |
| 13 | + } |
| 14 | + |
| 15 | + if !input.generics.params.is_empty() { |
| 16 | + bail!( |
| 17 | + &input.generics, |
| 18 | + "#[derive(Volatile)] cannot be used with generic structs" |
| 19 | + ); |
| 20 | + } |
| 21 | + |
| 22 | + let mut valid_repr = false; |
| 23 | + for attr in &input.attrs { |
| 24 | + if attr.path().is_ident("repr") { |
| 25 | + let ident = attr.parse_args::<Ident>()?; |
| 26 | + if ident == "C" || ident == "transparent" { |
| 27 | + valid_repr = true; |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + if !valid_repr { |
| 32 | + bail!( |
| 33 | + &input.ident, |
| 34 | + "#[derive(Volatile)] structs must be `#[repr(C)]` or `#[repr(transparent)]`" |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + Ok(()) |
| 39 | +} |
| 40 | + |
| 41 | +fn parse_attrs(fields: &Fields) -> Result<Vec<Vec<Attribute>>> { |
| 42 | + let mut attrss = vec![]; |
| 43 | + |
| 44 | + for field in fields.iter() { |
| 45 | + let mut attrs = vec![]; |
| 46 | + for attr in &field.attrs { |
| 47 | + if attr.path().is_ident("doc") { |
| 48 | + attrs.push(attr.clone()); |
| 49 | + } |
| 50 | + } |
| 51 | + attrss.push(attrs); |
| 52 | + } |
| 53 | + |
| 54 | + Ok(attrss) |
| 55 | +} |
| 56 | + |
| 57 | +fn parse_sigs(fields: &Fields) -> Result<Vec<Signature>> { |
| 58 | + let mut sigs = vec![]; |
| 59 | + |
| 60 | + for field in fields.iter() { |
| 61 | + let ident = field.ident.as_ref().unwrap(); |
| 62 | + let ty = &field.ty; |
| 63 | + |
| 64 | + let mut access: Path = parse_quote! { ::volatile::access::ReadWrite }; |
| 65 | + for attr in &field.attrs { |
| 66 | + if attr.path().is_ident("access") { |
| 67 | + access = attr.parse_args()?; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + let sig = parse_quote! { |
| 72 | + fn #ident(self) -> ::volatile::VolatilePtr<'a, #ty, #access> |
| 73 | + }; |
| 74 | + sigs.push(sig); |
| 75 | + } |
| 76 | + |
| 77 | + Ok(sigs) |
| 78 | +} |
| 79 | + |
| 80 | +fn emit_trait( |
| 81 | + vis: &Visibility, |
| 82 | + ident: &Ident, |
| 83 | + attrs: &[Vec<Attribute>], |
| 84 | + sigs: &[Signature], |
| 85 | +) -> Result<ItemTrait> { |
| 86 | + let item_trait = parse_quote! { |
| 87 | + #[allow(non_camel_case_types)] |
| 88 | + #vis trait #ident <'a> { |
| 89 | + #( |
| 90 | + #(#attrs)* |
| 91 | + #sigs; |
| 92 | + )* |
| 93 | + } |
| 94 | + }; |
| 95 | + |
| 96 | + Ok(item_trait) |
| 97 | +} |
| 98 | + |
| 99 | +fn emit_impl(trait_ident: &Ident, struct_ident: &Ident, sigs: &[Signature]) -> Result<ItemImpl> { |
| 100 | + let fields = sigs.iter().map(|sig| &sig.ident); |
| 101 | + |
| 102 | + let item_impl = parse_quote! { |
| 103 | + #[automatically_derived] |
| 104 | + impl<'a> #trait_ident<'a> for ::volatile::VolatilePtr<'a, #struct_ident, ::volatile::access::ReadWrite> { |
| 105 | + #( |
| 106 | + #sigs { |
| 107 | + ::volatile::map_field!(self.#fields).restrict() |
| 108 | + } |
| 109 | + )* |
| 110 | + } |
| 111 | + }; |
| 112 | + |
| 113 | + Ok(item_impl) |
| 114 | +} |
| 115 | + |
| 116 | +pub fn derive_volatile(input: ItemStruct) -> Result<Vec<Item>> { |
| 117 | + validate_input(&input)?; |
| 118 | + let attrs = parse_attrs(&input.fields)?; |
| 119 | + let sigs = parse_sigs(&input.fields)?; |
| 120 | + let trait_ident = format_ident!("{}Volatile", input.ident); |
| 121 | + |
| 122 | + let item_trait = emit_trait(&input.vis, &trait_ident, &attrs, &sigs)?; |
| 123 | + let item_impl = emit_impl(&item_trait.ident, &input.ident, &sigs)?; |
| 124 | + Ok(vec![Item::Trait(item_trait), Item::Impl(item_impl)]) |
| 125 | +} |
| 126 | + |
| 127 | +#[cfg(test)] |
| 128 | +mod tests { |
| 129 | + use quote::{quote, ToTokens}; |
| 130 | + |
| 131 | + use super::*; |
| 132 | + |
| 133 | + #[test] |
| 134 | + fn test_derive() -> Result<()> { |
| 135 | + let input = parse_quote! { |
| 136 | + #[repr(C)] |
| 137 | + #[derive(Volatile, Default)] |
| 138 | + pub struct DeviceConfig { |
| 139 | + feature_select: u32, |
| 140 | + /// This is a good field. |
| 141 | + #[access(ReadOnly)] |
| 142 | + feature: u32, |
| 143 | + } |
| 144 | + }; |
| 145 | + |
| 146 | + let result = derive_volatile(input)?; |
| 147 | + |
| 148 | + let expected_trait = quote! { |
| 149 | + #[allow(non_camel_case_types)] |
| 150 | + pub trait DeviceConfigVolatile<'a> { |
| 151 | + fn feature_select(self) -> ::volatile::VolatilePtr<'a, u32, ::volatile::access::ReadWrite>; |
| 152 | + |
| 153 | + /// This is a good field. |
| 154 | + fn feature(self) -> ::volatile::VolatilePtr<'a, u32, ReadOnly>; |
| 155 | + } |
| 156 | + }; |
| 157 | + |
| 158 | + let expected_impl = quote! { |
| 159 | + #[automatically_derived] |
| 160 | + impl<'a> DeviceConfigVolatile<'a> for ::volatile::VolatilePtr<'a, DeviceConfig, ::volatile::access::ReadWrite> { |
| 161 | + fn feature_select(self) -> ::volatile::VolatilePtr<'a, u32, ::volatile::access::ReadWrite> { |
| 162 | + ::volatile::map_field!(self.feature_select).restrict() |
| 163 | + } |
| 164 | + |
| 165 | + fn feature(self) -> ::volatile::VolatilePtr<'a, u32, ReadOnly> { |
| 166 | + ::volatile::map_field!(self.feature).restrict() |
| 167 | + } |
| 168 | + } |
| 169 | + }; |
| 170 | + |
| 171 | + assert_eq!( |
| 172 | + expected_trait.to_string(), |
| 173 | + result[0].to_token_stream().to_string() |
| 174 | + ); |
| 175 | + assert_eq!( |
| 176 | + expected_impl.to_string(), |
| 177 | + result[1].to_token_stream().to_string() |
| 178 | + ); |
| 179 | + |
| 180 | + Ok(()) |
| 181 | + } |
| 182 | +} |
0 commit comments