layline-codegen 0.5.0

Rust source generation for layline.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//! Derived numbers on encode: their values, range checks, and slots filled in once known.

use proc_macro2::{Ident, Literal, TokenStream};
use quote::{format_ident, quote};

use super::body::Own;
use super::checksum::written_check;
use super::repeat::element_width;
use super::text::text_wire_len;
use crate::root::Prelude;
use crate::walk::tokens::ident;
use crate::walk::{Derived, SlotOf, path};
use crate::{Endian, Error, Kind, Root, Segment};

/// The `bool` a derived flag holds. Presence is the only derivation that is not a number.
pub(super) fn derived_flag(d: &Derived, own: Own) -> Option<TokenStream> {
    match d {
        Derived::Presence { of, set } => {
            let opt = own.get(of);
            Some(if *set { quote!(#opt.is_some()) } else { quote!(#opt.is_none()) })
        }
        _ => None,
    }
}

/// The number encode writes into a derived field, as an `i64` expression.
///
/// `field` names it in errors. A [`Presence`](Derived::Presence) is a flag: use [`derived_flag`].
pub(super) fn derived_value(d: &Derived, own: Own, field: &str, root: &Root) -> TokenStream {
    let Prelude { some, none, .. } = root.prelude();
    match d {
        Derived::Count { of, scale, offset, cap } => {
            let coll = own.get(of);
            affine_inverse(quote!(#coll.len()), (*scale, *offset), *cap, (field, d.what()), root)
        }
        Derived::Squared { of, scale, offset, cap } => {
            let coll = own.get(of);
            let capped = cap.map(|cap| {
                let lit = Literal::usize_unsuffixed(cap);
                let msg = format!(
                    "field `{field}`: the square collection's length passes its cap of {cap}"
                );
                quote! { ::core::assert!(__len <= #lit, #msg); }
            });
            let order = quote! {
                {
                    let __len = #coll.len();
                    #capped
                    let __side = __len.isqrt();
                    ::core::assert!(
                        __side * __side == __len,
                        "a square collection needs n * n elements, and \
                         no whole number squares to its length"
                    );
                    __side
                }
            };
            affine_inverse(order, (*scale, *offset), None, (field, d.what()), root)
        }
        Derived::TextLen { of, codec, scale, offset, cap } => {
            let text = own.get(of);
            let n = text_wire_len(codec, quote!(#text.as_str()), root);
            affine_inverse(n, (*scale, *offset), *cap, (field, d.what()), root)
        }
        Derived::Offset { .. } | Derived::ByteLen { .. } => reserved(),
        Derived::Stride { elem, scale, offset } => affine_inverse(
            element_width(elem, root),
            (*scale, *offset),
            None,
            (field, d.what()),
            root,
        ),
        Derived::ArmLen { of, scale, offset, cap } => {
            let arm = arm_ident(of);
            affine_inverse(quote!(#arm), (*scale, *offset), *cap, (field, d.what()), root)
        }
        Derived::Discriminant { of, field } => {
            let body = own.get(of);
            let held = own.get(field);
            quote! {
                match #root::Choice::discriminant(&#body) {
                    #some(__d) => __d,
                    #none => #root::WireInt::to_i64(&#held),
                }
            }
        }
        Derived::Flags { field, bits } => {
            let held = own.get(field);
            let governed =
                Literal::i64_unsuffixed(bits.iter().fold(0u64, |all, (m, ..)| all | m) as i64);
            let set = bits.iter().map(|(mask, of, set)| {
                let opt = own.get(of);
                let mask = Literal::i64_unsuffixed(*mask as i64);
                let marked = if *set { quote!(#opt.is_some()) } else { quote!(#opt.is_none()) };
                quote!(| (if #marked { #mask } else { 0 }))
            });
            quote! {
                (#root::WireInt::to_i64(&#held) & !#governed) #(#set)*
            }
        }
        Derived::Presence { of, .. } => {
            unreachable!("presence of `{of}` is a flag: ask `derived_flag` first")
        }
        Derived::Checksum { algorithm, over } => {
            let value = written_check(&path(algorithm), over, root);
            quote! {
                #root::WireInt::to_i64(&#value)
            }
        }
    }
}

/// The placeholder a reserved field holds until [`stamp_slot`] overwrites it.
fn reserved() -> TokenStream {
    quote!(0)
}

/// A reserved field: where encode wrote its placeholder, and how to overwrite it.
pub(super) struct Slot {
    /// The collection whose number goes here, or the checksum field.
    pub(super) coll: String,
    pub(super) of: SlotOf,
    /// The local holding the slot's byte position in `out`.
    pub(super) mark: Ident,
    pub(super) width_bytes: usize,
    pub(super) write: SlotWrite,
}

impl SlotOf {
    /// The field's role, as errors name it.
    pub(super) fn what(self) -> &'static str {
        match self {
            SlotOf::Position => "offset",
            SlotOf::Length => "length",
        }
    }

    /// How the segment relates to `field`, as errors phrase it.
    fn tie(self, field: &str, record: bool) -> String {
        match (self, record) {
            (SlotOf::Position, _) => format!("is placed at offset field `{field}`"),
            (SlotOf::Length, false) => {
                format!("takes its byte length from length field `{field}`")
            }
            (SlotOf::Length, true) => {
                format!("is present exactly when length field `{field}` is non-zero")
            }
        }
    }

    /// The derivation this slot holds, as [`Derived::what`] names it.
    fn claim(self, record: bool) -> &'static str {
        match self {
            SlotOf::Position => Derived::Offset { of: String::new(), record }.what(),
            SlotOf::Length => Derived::ByteLen { of: String::new(), record }.what(),
        }
    }
}

/// The local holding a windowed switch's encoded length in bytes.
///
/// Encode writes the arm, measures it, and rewinds. The length field comes earlier and may be a
/// varint, so there are no fixed bytes to reserve.
pub(super) fn arm_ident(switch: &str) -> Ident {
    format_ident!("__arm_{}", switch)
}

/// The local holding the byte position of the slot reserved for `coll`.
pub(super) fn slot_ident(of: SlotOf, coll: &str) -> Ident {
    match of {
        SlotOf::Position => format_ident!("__slot_{}", coll),
        SlotOf::Length => format_ident!("__span_{}", coll),
    }
}

/// Which segment's number a stamp writes, and into which field.
#[derive(Clone, Copy)]
pub(super) struct StampAt<'a> {
    pub(super) owner: &'a str,
    pub(super) coll: &'a str,
    pub(super) of: SlotOf,
    pub(super) field: &'a str,
    pub(super) endian: Endian,
    /// Whether the segment is a single record. Only the error message uses it.
    pub(super) record: bool,
}

/// Write `value` into the slot reserved for `coll`, refusing a field with no reserved slot.
pub(super) fn stamp_slot(
    slots: &[Slot],
    at: StampAt<'_>,
    value: &TokenStream,
    root: &Root,
) -> Result<TokenStream, Error> {
    let StampAt { owner, coll, of, field, endian, record } = at;
    let what = of.what();
    let Some(slot) = slots.iter().find(|s| s.coll == coll && s.of == of) else {
        let tie = of.tie(field, record);
        let article = if what.starts_with(['a', 'e', 'i', 'o', 'u']) { "An" } else { "A" };
        return Err(Error::Refused(format!(
            "{owner}: `{coll}` {tie}, which has no fixed width. \
             {article} {what} field must be a fixed-width integer in a block"
        )));
    };
    Ok(fill_reserved(owner, slot, field, of.claim(record), value, endian, root))
}

/// The number a reserved field holds until it is stamped: zero, or the nearest end of its
/// `#[range]`, so the block's own range check passes the placeholder.
pub(super) fn placeholder(range: Option<crate::Range>) -> TokenStream {
    let held = match range {
        Some(crate::Range { lo: Some(lo), .. }) if lo > 0 => lo,
        Some(crate::Range { hi: Some(hi), .. }) if hi < 0 => hi,
        _ => 0,
    };
    let held = Literal::i64_unsuffixed(held);
    quote!(#held)
}

/// Asserts the stamped number `value` is inside the field's `#[range]`, as the block's encode
/// asserts every other field's.
fn in_range(field: &str, r: crate::Range, value: &TokenStream) -> TokenStream {
    let lo = r.lo.map_or_else(String::new, |lo| lo.to_string());
    let hi = r.hi.map_or_else(String::new, |hi| format!("={hi}"));
    let msg = format!("field `{field}`: value is outside `#[range({lo}..{hi})]`");
    let lo = r.lo.map(Literal::i64_suffixed);
    let hi = r.hi.map(Literal::i64_suffixed);
    // `contains` avoids `clippy::manual_range_contains` in user code.
    let test = match (lo, hi) {
        (Some(lo), Some(hi)) => quote!((#lo..=#hi).contains(&#value)),
        (Some(lo), None) => quote!((#lo..).contains(&#value)),
        (None, Some(hi)) => quote!((..=#hi).contains(&#value)),
        (None, None) => return quote!(),
    };
    quote! { ::core::assert!(#test, #msg); }
}

/// Overwrite a slot's placeholder with `value`, panicking if the field is too narrow.
pub(super) fn fill_reserved(
    owner: &str,
    slot: &Slot,
    field: &str,
    claim: &str,
    value: &TokenStream,
    endian: Endian,
    root: &Root,
) -> TokenStream {
    let Slot { mark, width_bytes, write, .. } = slot;
    let width = Literal::usize_unsuffixed(*width_bytes);
    let narrow = too_narrow(field, claim);
    let fill = match write {
        SlotWrite::Scalar { ty, range } => {
            let to = match endian {
                Endian::Be => quote!(to_be_bytes),
                Endian::Le => quote!(to_le_bytes),
            };
            let fits = fits(Target::Field(ty, 0), &quote!(__len), &narrow, root);
            let ranged = range.map(|r| in_range(field, r, &quote!(__len)));
            quote! {
                #fits
                #ranged
                let __pos = <#ty as #root::WireInt>::from_i64(__len);
                out.written_mut()[#mark..#mark + #width].copy_from_slice(&__pos.#to());
            }
        }
        SlotWrite::Nested { value: held, ty, outer, name } => {
            let (get, get_mut) = nested_accessors(owner, outer, name);
            let into = Target::Nested { get: quote!(#get(&#held)), child: ty, name };
            let fits = fits(into, &quote!(__len), &narrow, root);
            // Stamp the child the block wrote, not a copy of it: a second stamp into the same
            // child re-encodes it with the first one's number still in place.
            quote! {
                #fits
                *#get_mut(&mut #held) = #root::WireInt::from_i64(__len);
                out.written_mut()[#mark..#mark + #width].copy_from_slice(&#held.encode());
            }
        }
        SlotWrite::Check { sub, field: inner, ty } => {
            let fits = fits(Target::Field(ty, 0), &quote!(__len), &narrow, root);
            quote! {
                #fits
                let __ck = #sub { #inner: <#ty as #root::WireInt>::from_i64(__len) };
                out.written_mut()[#mark..#mark + #width].copy_from_slice(&__ck.encode());
            }
        }
    };
    quote! {
        {
            let __len = #value;
            #fill
        }
    }
}

/// How a slot's bytes are rewritten once its number is known.
pub(super) enum SlotWrite {
    /// A whole scalar field of a block, and the `#[range]` its number must fall in.
    Scalar { ty: TokenStream, range: Option<crate::Range> },
    /// A checksum field: its one-field layout, rebuilt with the checksum and re-encoded.
    Check { sub: Ident, field: Ident, ty: TokenStream },
    /// A field inside a nested layout: the child, rebuilt with the number and re-encoded.
    Nested {
        /// The local holding the child the block wrote, with its other derived fields already set.
        /// Each stamp updates it in place, so stamps into one child accumulate.
        value: Ident,
        ty: TokenStream,
        /// The block field holding the child, as named in the [`nested_probe`] accessors.
        outer: String,
        name: String,
    },
}

/// The type of the nested layout field `name` in a block of this body.
pub(super) fn nested_in_blocks<'a>(segments: &'a [Segment], name: &str) -> Option<&'a str> {
    segments.iter().find_map(|seg| match seg {
        Segment::Block(fields) => fields.iter().find_map(|f| match &f.kind {
            Kind::Nested { ty, .. } if f.name == name => Some(ty.as_str()),
            _ => None,
        }),
        _ => None,
    })
}

/// Assert that `outer.inner` is a field of nested layout `ty`, and emit accessors for it.
///
/// All reads and writes go through the accessors, so a field type that is not a `WireInt` fails
/// once, at the attribute.
pub(super) fn nested_probe(
    owner: &str,
    ty: &str,
    outer: &str,
    inner: &str,
    d: &Derived,
    root: &Root,
) -> Result<TokenStream, Error> {
    let p: syn::Path = syn::parse_str(ty).map_err(|_| {
        Error::Refused(format!("{owner}: nested layout `{ty}` is not a valid type path"))
    })?;
    let id = ident(inner);
    let (get, get_mut) = nested_accessors(owner, outer, inner);
    let missing = format!(
        "`{owner}`: `{outer}.{inner}` names no field of nested layout `{ty}`. Check the spelling"
    );
    let number = if matches!(d, Derived::Presence { .. }) {
        quote!()
    } else {
        quote! {
            #[allow(non_snake_case, dead_code, clippy::explicit_auto_deref)]
            fn #get(__nested: &#p) -> &impl #root::WireInt {
                &(*__nested).#id
            }
            #[allow(non_snake_case, dead_code, clippy::explicit_auto_deref)]
            fn #get_mut(__nested: &mut #p) -> &mut impl #root::WireInt {
                &mut (*__nested).#id
            }
        }
    };
    Ok(quote! {
        const _: () = ::core::assert!(
            #root::table::field_width(<#p as #root::Layout>::FIELDS, #inner) != 0,
            #missing
        );
        #number
    })
}

/// The read and write accessors [`nested_probe`] emits for `outer.inner` of `owner`.
pub(super) fn nested_accessors(owner: &str, outer: &str, inner: &str) -> (Ident, Ident) {
    (
        format_ident!("__layline_{}_{}_{}", owner, outer, inner),
        format_ident!("__layline_{}_{}_{}_mut", owner, outer, inner),
    )
}

/// The value of `field` where `field * scale + offset` equals `source`.
///
/// Encode panics where decode could not read it back: no whole solution, or above `cap`.
pub(super) fn affine_inverse(
    source: TokenStream,
    (scale, offset): (u32, i32),
    cap: Option<usize>,
    (field, what): (&str, &str),
    root: &Root,
) -> TokenStream {
    let Prelude { i64, .. } = root.prelude();
    let capped = cap.map(|cap| {
        let lit = Literal::usize_unsuffixed(cap);
        let msg =
            format!("field `{field}`: the {what} passes its cap of {cap}, and decode refuses it");
        quote! { ::core::assert!(__source <= #lit, #msg); }
    });
    let (s, o) = (i64::from(scale), i64::from(offset));
    let stated = match (scale, offset) {
        (1, 0) => quote!(__source as #i64),
        (1, _) => quote!(__source as #i64 - #o),
        _ => {
            let msg = format!(
                "field `{field}` cannot state the {what} exactly: no whole `{field} * {scale} + {offset}` equals it"
            );
            quote! {{
                let __n = __source as #i64 - #o;
                ::core::assert!(__n % #s == 0, #msg);
                __n / #s
            }}
        }
    };
    quote! {{
        let __source = #source;
        #capped
        #stated
    }}
}

/// The field a number is written into.
pub(super) enum Target<'a> {
    /// A `ty` field this many bits wide. 0 means the whole type.
    Field(&'a TokenStream, u64),
    /// A field of a nested layout. Only the [`nested_probe`] accessor knows its type.
    Nested {
        /// The accessor's read of the field.
        get: TokenStream,
        /// The nested layout's type.
        child: &'a TokenStream,
        /// The field's name in the child's `FIELDS`.
        name: &'a str,
    },
}

/// Assert that `value` reads back unchanged from the field.
pub(super) fn fits(into: Target<'_>, value: &TokenStream, msg: &str, root: &Root) -> TokenStream {
    match into {
        Target::Field(ty, width) => {
            let width = Literal::u32_unsuffixed(width as u32);
            quote! {
                ::core::assert!(#root::__private::fits_in_field::<#ty>(#value, #width), #msg);
            }
        }
        Target::Nested { get, child, name } => quote! {
            ::core::assert!(
                #root::WireInt::__fits_in_field(
                    #get,
                    #value,
                    #root::table::field_width(<#child as #root::Layout>::FIELDS, #name),
                ),
                #msg
            );
        },
    }
}

/// The refusal for a derived number its field is too narrow to hold.
pub(super) fn too_narrow(field: &str, what: &str) -> String {
    format!(
        "field `{field}` is too narrow for the {what} the wire derives it from. Widen the field"
    )
}