| 1 |
use defmt_parser::{Fragment, Parameter, Type}; |
| 2 |
use proc_macro2::{Ident as Ident2, Span as Span2, TokenStream as TokenStream2}; |
| 3 |
use quote::{format_ident, quote}; |
| 4 |
|
| 5 |
pub(crate) struct Codegen { |
| 6 |
pub(crate) exprs: Vec<TokenStream2>, |
| 7 |
pub(crate) patterns: Vec<Ident2>, |
| 8 |
} |
| 9 |
|
| 10 |
impl Codegen { |
| 11 |
pub(crate) fn new( |
| 12 |
fragments: &[Fragment<'_>], |
| 13 |
given_arg_count: usize, |
| 14 |
span: Span2, |
| 15 |
) -> syn::Result<Self> { |
| 16 |
let params = fragments |
| 17 |
.iter() |
| 18 |
.filter_map(|frag| match frag { |
| 19 |
Fragment::Parameter(param) => Some(param.clone()), |
| 20 |
Fragment::Literal(_) => None, |
| 21 |
}) |
| 22 |
.collect::<Vec<_>>(); |
| 23 |
|
| 24 |
let expected_arg_count = params |
| 25 |
.iter() |
| 26 |
.map(|param| param.index + 1) |
| 27 |
.max() |
| 28 |
.unwrap_or(0); |
| 29 |
|
| 30 |
if given_arg_count != expected_arg_count { |
| 31 |
let mut only = ""; |
| 32 |
if given_arg_count < expected_arg_count { |
| 33 |
only = "only "; |
| 34 |
} |
| 35 |
|
| 36 |
return Err(syn::Error::new( |
| 37 |
span, |
| 38 |
format!( |
| 39 |
"format string requires {} arguments but {}{} were provided", |
| 40 |
expected_arg_count, only, given_arg_count |
| 41 |
), |
| 42 |
)); |
| 43 |
} |
| 44 |
|
| 45 |
let mut exprs = vec![]; |
| 46 |
let mut patterns = vec![]; |
| 47 |
|
| 48 |
for arg_index in 0..expected_arg_count { |
| 49 |
let arg_ident = format_ident!("arg{}", arg_index); |
| 50 |
let matching_param = params |
| 51 |
.iter() |
| 52 |
.find(|param| param.index == arg_index) |
| 53 |
.unwrap(); |
| 54 |
|
| 55 |
let expr = encode_arg(&matching_param.ty, ¶ms, arg_index, &arg_ident); |
| 56 |
|
| 57 |
exprs.push(expr); |
| 58 |
patterns.push(arg_ident); |
| 59 |
} |
| 60 |
|
| 61 |
Ok(Codegen { exprs, patterns }) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
fn encode_arg(ty: &Type, params: &[Parameter], arg_index: usize, arg: &Ident2) -> TokenStream2 { |
| 66 |
match ty { |
| 67 |
Type::I8 => quote!(defmt::export::i8(#arg)), |
| 68 |
Type::I16 => quote!(defmt::export::i16(#arg)), |
| 69 |
Type::I32 => quote!(defmt::export::i32(#arg)), |
| 70 |
Type::I64 => quote!(defmt::export::i64(#arg)), |
| 71 |
Type::I128 => quote!(defmt::export::i128(#arg)), |
| 72 |
Type::Isize => quote!(defmt::export::isize(#arg)), |
| 73 |
|
| 74 |
Type::U8 => quote!(defmt::export::u8(#arg)), |
| 75 |
Type::U16 => quote!(defmt::export::u16(#arg)), |
| 76 |
Type::U32 => quote!(defmt::export::u32(#arg)), |
| 77 |
Type::U64 => quote!(defmt::export::u64(#arg)), |
| 78 |
Type::U128 => quote!(defmt::export::u128(#arg)), |
| 79 |
Type::Usize => quote!(defmt::export::usize(#arg)), |
| 80 |
|
| 81 |
Type::F32 => quote!(defmt::export::f32(#arg)), |
| 82 |
Type::F64 => quote!(defmt::export::f64(#arg)), |
| 83 |
|
| 84 |
Type::Bool => quote!(defmt::export::bool(#arg)), |
| 85 |
|
| 86 |
Type::Str => quote!(defmt::export::str(#arg)), |
| 87 |
Type::IStr => quote!(defmt::export::istr(#arg)), |
| 88 |
Type::Char => quote!(defmt::export::char(#arg)), |
| 89 |
|
| 90 |
Type::Format => quote!(defmt::export::fmt(#arg)), |
| 91 |
Type::FormatSlice => quote!(defmt::export::fmt_slice(#arg)), |
| 92 |
Type::FormatArray(len) => quote!(defmt::export::fmt_array({ |
| 93 |
let tmp: &[_; #len] = #arg; |
| 94 |
tmp |
| 95 |
})), |
| 96 |
|
| 97 |
Type::Debug => quote!(defmt::export::debug(#arg)), |
| 98 |
Type::Display => quote!(defmt::export::display(#arg)), |
| 99 |
Type::FormatSequence => unreachable!(), |
| 100 |
|
| 101 |
Type::U8Slice => quote!(defmt::export::slice(#arg)), |
| 102 |
|
| 103 |
// We cast to the expected array type (which should be a no-op cast) to provoke |
| 104 |
// a type mismatch error on mismatched lengths: |
| 105 |
// ``Symbol’s value as variable is void: // |
| 106 |
Type::U8Array(len) => quote!(defmt::export::u8_array({ |
| 107 |
let tmp: &[u8; #len] = #arg; |
| 108 |
tmp |
| 109 |
})), |
| 110 |
|
| 111 |
Type::BitField(_) => { |
| 112 |
let all_bitfields = params.iter().filter(|param| param.index == arg_index); |
| 113 |
let (smallest_bit_index, largest_bit_index) = |
| 114 |
defmt_parser::get_max_bitfield_range(all_bitfields).unwrap(); |
| 115 |
|
| 116 |
// indices of the lowest and the highest octet which contains bitfield-relevant data |
| 117 |
let lowest_byte = smallest_bit_index / 8; |
| 118 |
let highest_byte = (largest_bit_index - 1) / 8; |
| 119 |
let truncated_sz = highest_byte - lowest_byte + 1; // in bytes |
| 120 |
|
| 121 |
// shift away unneeded lower octet |
| 122 |
// TODO: create helper for shifting because readability |
| 123 |
match truncated_sz { |
| 124 |
1 => { |
| 125 |
quote!(defmt::export::u8(&defmt::export::truncate((*#arg) >> (#lowest_byte * 8)))) |
| 126 |
} |
| 127 |
2 => { |
| 128 |
quote!(defmt::export::u16(&defmt::export::truncate((*#arg) >> (#lowest_byte * 8)))) |
| 129 |
} |
| 130 |
3..=4 => { |
| 131 |
quote!(defmt::export::u32(&defmt::export::truncate((*#arg) >> (#lowest_byte * 8)))) |
| 132 |
} |
| 133 |
5..=8 => { |
| 134 |
quote!(defmt::export::u64(&defmt::export::truncate((*#arg) >> (#lowest_byte * 8)))) |
| 135 |
} |
| 136 |
9..=16 => { |
| 137 |
quote!(defmt::export::u128(&defmt::export::truncate((*#arg) >> (#lowest_byte * 8)))) |
| 138 |
} |
| 139 |
_ => unreachable!(), |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
} |