| 1 | use proc_macro2::TokenStream; |
| 2 | use quote::quote; |
| 3 | use syn::Visibility; |
| 4 | |
| 5 | use crate::generation::common::{does_field_have_getter, does_field_have_setter}; |
| 6 | use crate::parsing::bitfield_field::BitfieldField; |
| 7 | use crate::parsing::types::get_bits_from_type; |
| 8 | |
| 9 | pub(crate) fn generate_get_bit_tokens( |
| 10 | vis: Visibility, |
| 11 | bitfield_type: &syn::Type, |
| 12 | fields: &[BitfieldField], |
| 13 | ignored_fields_struct: bool, |
| 14 | ) -> TokenStream { |
| 15 | let bitfield_type_bits = get_bits_from_type(bitfield_type).unwrap() as usize; |
| 16 | let get_bit_documentation = "Returns a bit from the given index. Returns false for out-of-bounds and fields without read access.".to_string(); |
| 17 | let checked_get_bit_documentation = "Returns a bit from the given index. Returns an error for out-of-bounds and fields without read access.".to_string(); |
| 18 | |
| 19 | let false_return_for_non_readable_fields = fields |
| 20 | .iter() |
| 21 | .filter(|field| !does_field_have_getter(field) && !field.padding) |
| 22 | .map(|field| { |
| 23 | let field_bits = field.bits as usize; |
| 24 | let field_offset = field.offset as usize; |
| 25 | let field_end_bits = field_offset + field_bits; |
| 26 | quote! { |
| 27 | if index >= #field_offset && index < #field_end_bits { |
| 28 | return false; |
| 29 | } |
| 30 | } |
| 31 | }); |
| 32 | |
| 33 | let error_return_for_write_only_fields = fields |
| 34 | .iter() |
| 35 | .filter(|field| !does_field_have_getter(field) && !field.padding) |
| 36 | .map(|field| { |
| 37 | let field_bits = field.bits as usize; |
| 38 | let field_offset = field.offset as usize; |
| 39 | let field_end_bits = field_offset + field_bits; |
| 40 | quote! { |
| 41 | if index >= #field_offset && index < #field_end_bits { |
| 42 | return Err("Can't read from a write-only field."); |
| 43 | } |
| 44 | } |
| 45 | }); |
| 46 | |
| 47 | let struct_val_ident = if ignored_fields_struct { |
| 48 | quote! { self.val } |
| 49 | } else { |
| 50 | quote! { self.0 } |
| 51 | }; |
| 52 | |
| 53 | quote! { |
| 54 | #[doc = #get_bit_documentation] |
| 55 | #vis const fn get_bit(&self, index: usize) -> bool { |
| 56 | if index > #bitfield_type_bits { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | #( #false_return_for_non_readable_fields )* |
| 61 | |
| 62 | (#struct_val_ident >> index) & 1 != 0 |
| 63 | } |
| 64 | |
| 65 | #[doc = #checked_get_bit_documentation] |
| 66 | #vis const fn checked_get_bit(&self, index: usize) -> Result<bool, &'static str> { |
| 67 | if index > #bitfield_type_bits { |
| 68 | return Err("Index out of bounds."); |
| 69 | } |
| 70 | |
| 71 | #( #error_return_for_write_only_fields )* |
| 72 | |
| 73 | Ok((#struct_val_ident >> index) & 1 != 0) |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | pub(crate) fn generate_set_bit_tokens( |
| 79 | vis: Visibility, |
| 80 | bitfield_type: &syn::Type, |
| 81 | fields: &[BitfieldField], |
| 82 | ignored_fields_struct: bool, |
| 83 | ) -> TokenStream { |
| 84 | let bitfield_type_bits = get_bits_from_type(bitfield_type).unwrap() as usize; |
| 85 | let set_bit_documentation = "Sets a bit at given index with the given value. Is no-op for out-of-bounds and fields without write access.".to_string(); |
| 86 | let checked_set_bit_documentation = "Sets a bit at given index with the given value. Returns an error for out-of-bounds and fields without write access.".to_string(); |
| 87 | |
| 88 | let no_op_for_non_writable_fields = |
| 89 | fields.iter().filter(|field| !does_field_have_setter(field)).map(|field| { |
| 90 | let field_bits = field.bits as usize; |
| 91 | let field_offset = field.offset as usize; |
| 92 | let field_end_bits = field_offset + field_bits; |
| 93 | quote! { |
| 94 | if index >= #field_offset && index < #field_end_bits { |
| 95 | return; |
| 96 | } |
| 97 | } |
| 98 | }); |
| 99 | |
| 100 | let error_return_for_non_writable_fields = |
| 101 | fields.iter().filter(|field| !does_field_have_setter(field)).map(|field| { |
| 102 | let field_bits = field.bits as usize; |
| 103 | let field_offset = field.offset as usize; |
| 104 | let field_end_bits = field_offset + field_bits; |
| 105 | quote! { |
| 106 | if index >= #field_offset && index < #field_end_bits { |
| 107 | return Err("Can't write to a non-writable or padding field."); |
| 108 | } |
| 109 | } |
| 110 | }); |
| 111 | |
| 112 | let struct_val_ident = if ignored_fields_struct { |
| 113 | quote! { self.val } |
| 114 | } else { |
| 115 | quote! { self.0 } |
| 116 | }; |
| 117 | |
| 118 | quote! { |
| 119 | #[doc = #set_bit_documentation] |
| 120 | #vis const fn set_bit(&mut self, index: usize, bit: bool) { |
| 121 | if index > #bitfield_type_bits { |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | #( #no_op_for_non_writable_fields )* |
| 126 | |
| 127 | if bit { |
| 128 | #struct_val_ident |= 1 << index; |
| 129 | } else { |
| 130 | #struct_val_ident &= !(1 << index); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | #[doc = #checked_set_bit_documentation] |
| 135 | #vis const fn checked_set_bit(&mut self, index: usize, bit: bool) -> Result<(), &'static str> { |
| 136 | if index > #bitfield_type_bits { |
| 137 | return Err("Index out of bounds."); |
| 138 | } |
| 139 | |
| 140 | #( #error_return_for_non_writable_fields )* |
| 141 | |
| 142 | if bit { |
| 143 | #struct_val_ident |= 1 << index; |
| 144 | } else { |
| 145 | #struct_val_ident &= !(1 << index); |
| 146 | } |
| 147 | |
| 148 | Ok(()) |
| 149 | } |
| 150 | } |
| 151 | } |