|
| 1 | +use std::convert::TryFrom; |
| 2 | + |
| 3 | +use darling::ast::Data; |
| 4 | +use proc_macro2::TokenStream; |
| 5 | +use quote::quote; |
| 6 | + |
| 7 | +use crate::{DekuData, DekuDataEnum, DekuDataStruct, FieldData}; |
| 8 | + |
| 9 | +pub(crate) fn emit_deku_size(input: &DekuData) -> Result<TokenStream, syn::Error> { |
| 10 | + // NOTE: This is overkill, but keeps the same validating requirements of DekuRead. This ensures |
| 11 | + // the required values are set so we calculate the correct size. |
| 12 | + // TODO: We only care about validating and not the codegen. Split the two. |
| 13 | + let _ = super::deku_read::emit_deku_read(input)?; |
| 14 | + |
| 15 | + match &input.data { |
| 16 | + Data::Enum(_) => emit_enum(input), |
| 17 | + Data::Struct(_) => emit_struct(input), |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +/// Calculate the size of a collection of fields |
| 22 | +fn calculate_fields_size<'a>( |
| 23 | + fields: impl IntoIterator<Item = &'a FieldData>, |
| 24 | + crate_: &syn::Ident, |
| 25 | +) -> TokenStream { |
| 26 | + let field_sizes = fields.into_iter().filter_map(|f| { |
| 27 | + if !f.temp { |
| 28 | + let field_type = &f.ty; |
| 29 | + |
| 30 | + #[cfg(feature = "bits")] |
| 31 | + if let Some(bits) = &f.bits { |
| 32 | + return Some(quote! { (#bits) }); |
| 33 | + } |
| 34 | + |
| 35 | + if let Some(bytes) = &f.bytes { |
| 36 | + return Some(quote! { (#bytes) * 8 }); |
| 37 | + } |
| 38 | + |
| 39 | + Some(quote! { <#field_type as ::#crate_::DekuSize>::SIZE_BITS }) |
| 40 | + } else { |
| 41 | + None |
| 42 | + } |
| 43 | + }); |
| 44 | + |
| 45 | + quote! { 0 #(+ #field_sizes)* } |
| 46 | +} |
| 47 | + |
| 48 | +/// Add DekuSize trait bounds to where clause for fields that need them |
| 49 | +fn add_field_bounds<'a>( |
| 50 | + where_clause: &mut Option<syn::WhereClause>, |
| 51 | + fields: impl IntoIterator<Item = &'a FieldData>, |
| 52 | + crate_: &syn::Ident, |
| 53 | +) { |
| 54 | + for field in fields { |
| 55 | + if !field.temp { |
| 56 | + let field_type = &field.ty; |
| 57 | + #[cfg(feature = "bits")] |
| 58 | + let needs_bound = field.bits.is_none() && field.bytes.is_none(); |
| 59 | + #[cfg(not(feature = "bits"))] |
| 60 | + let needs_bound = field.bytes.is_none(); |
| 61 | + |
| 62 | + if needs_bound { |
| 63 | + let where_clause = where_clause.get_or_insert_with(|| syn::parse_quote! { where }); |
| 64 | + where_clause.predicates.push(syn::parse_quote! { |
| 65 | + #field_type: ::#crate_::DekuSize |
| 66 | + }); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +/// Calculate the discriminant size for an enum |
| 73 | +fn calculate_discriminant_size( |
| 74 | + input: &DekuData, |
| 75 | + id: Option<&crate::Id>, |
| 76 | + id_type: Option<&TokenStream>, |
| 77 | + crate_: &syn::Ident, |
| 78 | +) -> TokenStream { |
| 79 | + #[cfg(feature = "bits")] |
| 80 | + if let Some(bits) = &input.bits { |
| 81 | + return quote! { (#bits) }; |
| 82 | + } |
| 83 | + |
| 84 | + if let Some(bytes) = &input.bytes { |
| 85 | + return quote! { (#bytes) * 8 }; |
| 86 | + } |
| 87 | + |
| 88 | + if id.is_some() { |
| 89 | + return quote! { 0 }; |
| 90 | + } |
| 91 | + |
| 92 | + if let Some(id_type) = id_type { |
| 93 | + return quote! { <#id_type as ::#crate_::DekuSize>::SIZE_BITS }; |
| 94 | + } |
| 95 | + |
| 96 | + // This unwrap is ensured by us calling DekuRead validation |
| 97 | + let repr = &input.repr.unwrap(); |
| 98 | + let repr_type = TokenStream::from(*repr); |
| 99 | + quote! { <#repr_type as ::#crate_::DekuSize>::SIZE_BITS } |
| 100 | +} |
| 101 | + |
| 102 | +fn emit_struct(input: &DekuData) -> Result<TokenStream, syn::Error> { |
| 103 | + let crate_ = super::get_crate_name(); |
| 104 | + |
| 105 | + let DekuDataStruct { |
| 106 | + imp: _, |
| 107 | + wher: _, |
| 108 | + ident, |
| 109 | + fields, |
| 110 | + } = DekuDataStruct::try_from(input)?; |
| 111 | + |
| 112 | + let size_calculation = calculate_fields_size(fields.iter().copied(), &crate_); |
| 113 | + |
| 114 | + let (imp_generics, ty_generics, where_clause) = input.generics.split_for_impl(); |
| 115 | + |
| 116 | + let mut where_clause = where_clause.cloned(); |
| 117 | + add_field_bounds(&mut where_clause, fields.iter().copied(), &crate_); |
| 118 | + |
| 119 | + let tokens = quote! { |
| 120 | + impl #imp_generics ::#crate_::DekuSize for #ident #ty_generics #where_clause { |
| 121 | + const SIZE_BITS: usize = #size_calculation; |
| 122 | + } |
| 123 | + }; |
| 124 | + |
| 125 | + Ok(tokens) |
| 126 | +} |
| 127 | + |
| 128 | +fn emit_enum(input: &DekuData) -> Result<TokenStream, syn::Error> { |
| 129 | + let crate_ = super::get_crate_name(); |
| 130 | + |
| 131 | + let DekuDataEnum { |
| 132 | + imp: _, |
| 133 | + wher: _, |
| 134 | + variants, |
| 135 | + ident, |
| 136 | + id, |
| 137 | + id_type, |
| 138 | + id_args: _, |
| 139 | + } = DekuDataEnum::try_from(input)?; |
| 140 | + |
| 141 | + let discriminant_size = calculate_discriminant_size(input, id, id_type, &crate_); |
| 142 | + |
| 143 | + let variant_sizes = variants |
| 144 | + .iter() |
| 145 | + .map(|variant| calculate_fields_size(variant.fields.iter(), &crate_)); |
| 146 | + |
| 147 | + let max_variant_size = quote! { |
| 148 | + { |
| 149 | + const fn const_max(a: usize, b: usize) -> usize { |
| 150 | + if a > b { a } else { b } |
| 151 | + } |
| 152 | + |
| 153 | + let mut max = 0; |
| 154 | + #( |
| 155 | + max = const_max(max, #variant_sizes); |
| 156 | + )* |
| 157 | + max |
| 158 | + } |
| 159 | + }; |
| 160 | + |
| 161 | + let (imp_generics, ty_generics, where_clause) = input.generics.split_for_impl(); |
| 162 | + |
| 163 | + let mut where_clause = where_clause.cloned(); |
| 164 | + for variant in variants.iter() { |
| 165 | + add_field_bounds(&mut where_clause, variant.fields.iter(), &crate_); |
| 166 | + } |
| 167 | + |
| 168 | + let tokens = quote! { |
| 169 | + impl #imp_generics ::#crate_::DekuSize for #ident #ty_generics #where_clause { |
| 170 | + const SIZE_BITS: usize = #discriminant_size + #max_variant_size; |
| 171 | + } |
| 172 | + }; |
| 173 | + |
| 174 | + Ok(tokens) |
| 175 | +} |
0 commit comments