ferray-core-macros 0.3.0

Procedural macros for ferray-core (FerrayRecord, s![], promoted_type!)
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
// ferray-core-macros: Procedural macros for ferray-core
//
// Implements:
// - #[derive(FerrayRecord)] — generates FerrayRecord trait impl for #[repr(C)] structs
// - s![] — NumPy-style slice indexing macro
// - promoted_type!() — compile-time type promotion macro

// Macro implementations branch on parsed AST shapes; identical match arms
// reflect "different syntactic forms produce the same expansion" and are
// kept distinct on purpose for future divergence. `needless_pass_by_value`
// is a syn-API ergonomics tradeoff (the parser owns each TokenStream once).
#![allow(
    clippy::match_same_arms,
    clippy::needless_pass_by_value,
    clippy::option_if_let_else
)]

extern crate proc_macro;

use proc_macro::TokenStream;
use quote::quote;
use syn::{Data, DeriveInput, Fields, parse_macro_input};

// ---------------------------------------------------------------------------
// #[derive(FerrayRecord)]
// ---------------------------------------------------------------------------

/// Derive macro that generates an `unsafe impl FerrayRecord` for a `#[repr(C)]` struct.
///
/// # Requirements
/// - The struct must have `#[repr(C)]`.
/// - All fields must implement `ferray_core::dtype::Element`.
///
/// # Generated code
/// - `field_descriptors()` returns a static slice of `FieldDescriptor` with correct
///   name, dtype, offset, and size for each field.
/// - `record_size()` returns `std::mem::size_of::<Self>()`.
#[proc_macro_derive(FerrayRecord)]
pub fn derive_ferray_record(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);
    match impl_ferray_record(&input) {
        Ok(ts) => ts.into(),
        Err(e) => e.to_compile_error().into(),
    }
}

fn impl_ferray_record(input: &DeriveInput) -> syn::Result<proc_macro2::TokenStream> {
    let name = &input.ident;

    // Check for #[repr(C)]
    let has_repr_c = input.attrs.iter().any(|attr| {
        if !attr.path().is_ident("repr") {
            return false;
        }
        let mut found = false;
        let _ = attr.parse_nested_meta(|meta| {
            if meta.path.is_ident("C") {
                found = true;
            }
            Ok(())
        });
        found
    });

    if !has_repr_c {
        return Err(syn::Error::new_spanned(
            &input.ident,
            "FerrayRecord requires #[repr(C)] on the struct",
        ));
    }

    // Only works on structs with named fields
    let fields = match &input.data {
        Data::Struct(data_struct) => match &data_struct.fields {
            Fields::Named(named) => &named.named,
            _ => {
                return Err(syn::Error::new_spanned(
                    &input.ident,
                    "FerrayRecord only supports structs with named fields",
                ));
            }
        },
        _ => {
            return Err(syn::Error::new_spanned(
                &input.ident,
                "FerrayRecord can only be derived for structs",
            ));
        }
    };

    let field_count = fields.len();
    let mut field_descriptors = Vec::with_capacity(field_count);

    for field in fields {
        let field_name = field.ident.as_ref().unwrap();
        let field_name_str = field_name.to_string();
        let field_ty = &field.ty;

        field_descriptors.push(quote! {
            ferray_core::record::FieldDescriptor {
                name: #field_name_str,
                dtype: <#field_ty as ferray_core::dtype::Element>::dtype(),
                offset: std::mem::offset_of!(#name, #field_name),
                size: std::mem::size_of::<#field_ty>(),
            }
        });
    }

    let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();

    let expanded = quote! {
        unsafe impl #impl_generics ferray_core::record::FerrayRecord for #name #ty_generics #where_clause {
            fn field_descriptors() -> &'static [ferray_core::record::FieldDescriptor] {
                // Issue #323 asked whether this could be a `const` static
                // array. `offset_of!` and `size_of::<T>()` are both const,
                // but `<T as Element>::dtype()` is a trait method call and
                // trait methods are not `const` on stable Rust yet — so
                // the LazyLock is unavoidable without breaking
                // `Element::dtype()` for every user. Re-evaluate once
                // `const_trait_impl` stabilises.
                static FIELDS: std::sync::LazyLock<Vec<ferray_core::record::FieldDescriptor>> =
                    std::sync::LazyLock::new(|| {
                        vec![
                            #(#field_descriptors),*
                        ]
                    });
                &FIELDS
            }

            fn record_size() -> usize {
                std::mem::size_of::<#name>()
            }
        }
    };

    Ok(expanded)
}

// ---------------------------------------------------------------------------
// s![] macro — NumPy-style slice indexing
// ---------------------------------------------------------------------------

/// NumPy-style slice indexing macro.
///
/// Produces a `Vec<ferray_core::dtype::SliceInfoElem>` that can be passed
/// to array slicing methods.
///
/// # Syntax
/// - `s![0..3, 2]` — rows 0..3, column 2
/// - `s![.., 0..;2]` — all rows, every-other column starting from 0
/// - `s![1..5;2, ..]` — rows 1..5 step 2, all columns
/// - `s![3]` — single integer index
/// - `s![..]` — all elements along this axis
/// - `s![2..]` — from index 2 to end
/// - `s![..5]` — from start to index 5
/// - `s![1..5]` — from index 1 to 5
/// - `s![1..5;2]` — from index 1 to 5, step 2
///
/// Each component in the comma-separated list becomes one `SliceInfoElem`.
#[proc_macro]
pub fn s(input: TokenStream) -> TokenStream {
    let input2: proc_macro2::TokenStream = input.into();
    let expanded = impl_s_macro(input2);
    match expanded {
        Ok(ts) => ts.into(),
        Err(e) => e.to_compile_error().into(),
    }
}

fn impl_s_macro(input: proc_macro2::TokenStream) -> syn::Result<proc_macro2::TokenStream> {
    // We parse the input as a sequence of comma-separated slice expressions.
    // Each expression can be:
    //   - An integer literal or expression: `2` -> Index(2)
    //   - A full range `..` -> Slice { start: 0, end: None, step: 1 }
    //   - A range `a..b` -> Slice { start: a, end: Some(b), step: 1 }
    //   - A range from `a..` -> Slice { start: a, end: None, step: 1 }
    //   - A range to `..b` -> Slice { start: 0, end: Some(b), step: 1 }
    //   - Any of the above with `;step` suffix
    //
    // We'll output code that constructs a Vec<SliceInfoElem>.
    //
    // Since proc macros can't easily parse arbitrary Rust expressions with range syntax
    // mixed with custom `;step` syntax, we'll use a simpler token-based approach.

    let input_str = input.to_string();

    // Handle empty input
    if input_str.trim().is_empty() {
        return Ok(quote! {
            ::std::vec::Vec::<ferray_core::dtype::SliceInfoElem>::new()
        });
    }

    // Split by commas (respecting parentheses/brackets nesting)
    let components = split_top_level_commas(&input_str);
    let mut elems = Vec::new();

    for component in &components {
        let trimmed = component.trim();
        if trimmed.is_empty() {
            continue;
        }
        elems.push(parse_slice_component(trimmed)?);
    }

    Ok(quote! {
        vec![#(#elems),*]
    })
}

fn split_top_level_commas(s: &str) -> Vec<String> {
    let mut result = Vec::new();
    let mut current = String::new();
    let mut depth = 0i32;

    for ch in s.chars() {
        match ch {
            '(' | '[' | '{' => {
                depth += 1;
                current.push(ch);
            }
            ')' | ']' | '}' => {
                depth -= 1;
                current.push(ch);
            }
            ',' if depth == 0 => {
                result.push(current.clone());
                current.clear();
            }
            _ => {
                current.push(ch);
            }
        }
    }
    if !current.is_empty() {
        result.push(current);
    }
    result
}

/// Find the last top-level semicolon (not inside brackets/braces/parens).
fn rfind_top_level_semicolon(s: &str) -> Option<usize> {
    let mut depth = 0i32;
    let mut last_idx = None;
    for (i, ch) in s.char_indices() {
        match ch {
            '(' | '[' | '{' => depth += 1,
            ')' | ']' | '}' => depth -= 1,
            ';' if depth == 0 => last_idx = Some(i),
            _ => {}
        }
    }
    last_idx
}

fn parse_slice_component(s: &str) -> syn::Result<proc_macro2::TokenStream> {
    let trimmed = s.trim();

    // Check for step suffix: `expr;step` (depth-aware to handle block expressions)
    let (range_part, step_part) = if let Some(idx) = rfind_top_level_semicolon(trimmed) {
        let (rp, sp) = trimmed.split_at(idx);
        (rp.trim(), Some(sp[1..].trim()))
    } else {
        (trimmed, None)
    };

    let step_expr = if let Some(step_str) = step_part {
        let step_tokens: proc_macro2::TokenStream = step_str.parse().map_err(|_| {
            syn::Error::new(
                proc_macro2::Span::call_site(),
                format!("invalid step expression: {step_str}"),
            )
        })?;
        quote! { #step_tokens }
    } else {
        quote! { 1isize }
    };

    // Now parse range_part
    if range_part == ".." {
        // Full range: all elements
        return Ok(quote! {
            ferray_core::dtype::SliceInfoElem::Slice {
                start: 0,
                end: ::core::option::Option::None,
                step: #step_expr,
            }
        });
    }

    if let Some(rest) = range_part.strip_prefix("..") {
        // RangeTo: ..end
        let end_tokens: proc_macro2::TokenStream = rest.parse().map_err(|_| {
            syn::Error::new(
                proc_macro2::Span::call_site(),
                format!("invalid end expression: {rest}"),
            )
        })?;
        return Ok(quote! {
            ferray_core::dtype::SliceInfoElem::Slice {
                start: 0,
                end: ::core::option::Option::Some(#end_tokens),
                step: #step_expr,
            }
        });
    }

    if let Some(idx) = range_part.find("..") {
        let start_str = range_part[..idx].trim();
        let end_str = range_part[idx + 2..].trim();

        let start_tokens: proc_macro2::TokenStream = start_str.parse().map_err(|_| {
            syn::Error::new(
                proc_macro2::Span::call_site(),
                format!("invalid start expression: {start_str}"),
            )
        })?;

        if end_str.is_empty() {
            // RangeFrom: start..
            return Ok(quote! {
                ferray_core::dtype::SliceInfoElem::Slice {
                    start: #start_tokens,
                    end: ::core::option::Option::None,
                    step: #step_expr,
                }
            });
        }

        let end_tokens: proc_macro2::TokenStream = end_str.parse().map_err(|_| {
            syn::Error::new(
                proc_macro2::Span::call_site(),
                format!("invalid end expression: {end_str}"),
            )
        })?;

        return Ok(quote! {
            ferray_core::dtype::SliceInfoElem::Slice {
                start: #start_tokens,
                end: ::core::option::Option::Some(#end_tokens),
                step: #step_expr,
            }
        });
    }

    // No `..` found — this is a single index (integer expression)
    if step_part.is_some() {
        return Err(syn::Error::new(
            proc_macro2::Span::call_site(),
            format!("step ';' is not valid for integer indices: {trimmed}"),
        ));
    }

    let idx_tokens: proc_macro2::TokenStream = range_part.parse().map_err(|_| {
        syn::Error::new(
            proc_macro2::Span::call_site(),
            format!("invalid index expression: {range_part}"),
        )
    })?;

    Ok(quote! {
        ferray_core::dtype::SliceInfoElem::Index(#idx_tokens)
    })
}

// ---------------------------------------------------------------------------
// promoted_type!() — compile-time type promotion
// ---------------------------------------------------------------------------

/// Compile-time type promotion macro.
///
/// Given two numeric types, resolves to the smallest type that can represent
/// both without precision loss, following `NumPy`'s promotion rules.
///
/// # Examples
/// ```ignore
/// type R = promoted_type!(f32, f64); // R = f64
/// type R = promoted_type!(i32, f32); // R = f64
/// type R = promoted_type!(u8, i8);   // R = i16
/// ```
#[proc_macro]
pub fn promoted_type(input: TokenStream) -> TokenStream {
    let input2: proc_macro2::TokenStream = input.into();
    match impl_promoted_type(input2) {
        Ok(ts) => ts.into(),
        Err(e) => e.to_compile_error().into(),
    }
}

fn impl_promoted_type(input: proc_macro2::TokenStream) -> syn::Result<proc_macro2::TokenStream> {
    let input_str = input.to_string();
    let parts: Vec<&str> = input_str.split(',').map(str::trim).collect();

    if parts.len() != 2 {
        return Err(syn::Error::new(
            proc_macro2::Span::call_site(),
            "promoted_type! expects exactly two type arguments: promoted_type!(T1, T2)",
        ));
    }

    let t1 = normalize_type(parts[0]);
    let t2 = normalize_type(parts[1]);

    let result = promote_types_static(&t1, &t2).ok_or_else(|| {
        syn::Error::new(
            proc_macro2::Span::call_site(),
            format!("cannot promote types: {t1} and {t2}"),
        )
    })?;

    let result_tokens: proc_macro2::TokenStream = result.parse().map_err(|_| {
        syn::Error::new(
            proc_macro2::Span::call_site(),
            format!("internal error: could not parse result type: {result}"),
        )
    })?;

    Ok(result_tokens)
}

fn normalize_type(s: &str) -> String {
    // Normalize Complex<f32> / Complex<f64> / num_complex::Complex<f32> etc.
    s.trim().replace(' ', "")
}

/// Static type promotion following `NumPy` rules.
///
/// Returns the promoted type as a string, or None if unknown.
fn promote_types_static(a: &str, b: &str) -> Option<&'static str> {
    // Assign a numeric "kind + rank" to each type, then pick the larger.
    //
    // NumPy promotion hierarchy (simplified):
    //   bool < u8 < u16 < u32 < u64 < u128
    //   bool < i8 < i16 < i32 < i64 < i128
    //   f32 < f64
    //   Complex<f32> < Complex<f64>
    //
    // Cross-kind rules:
    //   unsigned + signed -> next-size signed (e.g. u8 + i8 -> i16)
    //   any int + float -> float (ensure enough precision)
    //   any real + complex -> complex with appropriate float size

    let ra = type_rank(a)?;
    let rb = type_rank(b)?;

    // `promote_ranks` uses an empty string to signal "no lossless
    // common type exists" (currently the u128 + signed-int case); map
    // that to `None` so the outer proc-macro emits a compile error.
    match promote_ranks(ra, rb) {
        "" => None,
        other => Some(other),
    }
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum TypeKind {
    Bool,
    Unsigned,
    Signed,
    Float,
    Complex,
}

#[derive(Clone, Copy)]
struct TypeRank {
    kind: TypeKind,
    /// Bit width within the kind (e.g., 8 for u8, 32 for f32, etc.)
    bits: u32,
}

fn type_rank(s: &str) -> Option<TypeRank> {
    let result = match s {
        "bool" => TypeRank {
            kind: TypeKind::Bool,
            bits: 1,
        },
        "u8" => TypeRank {
            kind: TypeKind::Unsigned,
            bits: 8,
        },
        "u16" => TypeRank {
            kind: TypeKind::Unsigned,
            bits: 16,
        },
        "u32" => TypeRank {
            kind: TypeKind::Unsigned,
            bits: 32,
        },
        "u64" => TypeRank {
            kind: TypeKind::Unsigned,
            bits: 64,
        },
        "u128" => TypeRank {
            kind: TypeKind::Unsigned,
            bits: 128,
        },
        "i8" => TypeRank {
            kind: TypeKind::Signed,
            bits: 8,
        },
        "i16" => TypeRank {
            kind: TypeKind::Signed,
            bits: 16,
        },
        "i32" => TypeRank {
            kind: TypeKind::Signed,
            bits: 32,
        },
        "i64" => TypeRank {
            kind: TypeKind::Signed,
            bits: 64,
        },
        "i128" => TypeRank {
            kind: TypeKind::Signed,
            bits: 128,
        },
        "f32" => TypeRank {
            kind: TypeKind::Float,
            bits: 32,
        },
        "f64" => TypeRank {
            kind: TypeKind::Float,
            bits: 64,
        },
        "Complex<f32>" | "num_complex::Complex<f32>" => TypeRank {
            kind: TypeKind::Complex,
            bits: 32,
        },
        "Complex<f64>" | "num_complex::Complex<f64>" => TypeRank {
            kind: TypeKind::Complex,
            bits: 64,
        },
        "f16" | "half::f16" => TypeRank {
            kind: TypeKind::Float,
            bits: 16,
        },
        "bf16" | "half::bf16" => TypeRank {
            kind: TypeKind::Float,
            bits: 16,
        },
        _ => return None,
    };
    Some(result)
}

fn promote_ranks(a: TypeRank, b: TypeRank) -> &'static str {
    use TypeKind::{Bool, Complex, Float, Signed, Unsigned};

    // Same type
    if a.kind == b.kind && a.bits == b.bits {
        return rank_to_type(a);
    }

    // Handle Bool: bool promotes to anything
    if a.kind == Bool {
        return rank_to_type(b);
    }
    if b.kind == Bool {
        return rank_to_type(a);
    }

    // Complex + anything -> Complex with max float precision
    if a.kind == Complex || b.kind == Complex {
        let float_bits_a = to_float_bits(a);
        let float_bits_b = to_float_bits(b);
        let bits = float_bits_a.max(float_bits_b);
        return if bits <= 32 {
            "num_complex::Complex<f32>"
        } else {
            "num_complex::Complex<f64>"
        };
    }

    // Float + anything -> Float with enough precision
    if a.kind == Float || b.kind == Float {
        let float_bits_a = to_float_bits(a);
        let float_bits_b = to_float_bits(b);
        let bits = float_bits_a.max(float_bits_b);
        return if bits <= 32 { "f32" } else { "f64" };
    }

    // Now both are integer types (Unsigned or Signed)
    match (a.kind, b.kind) {
        (Unsigned, Unsigned) => {
            let bits = a.bits.max(b.bits);
            uint_type(bits)
        }
        (Signed, Signed) => {
            let bits = a.bits.max(b.bits);
            int_type(bits)
        }
        (Unsigned, Signed) | (Signed, Unsigned) => {
            let (u, s) = if a.kind == Unsigned { (a, b) } else { (b, a) };
            // unsigned + signed: need a signed type that holds both ranges
            // u8 + i8 -> i16, u16 + i16 -> i32, etc.
            if u.bits < s.bits {
                // Signed type is strictly larger, it can hold the unsigned range
                int_type(s.bits)
            } else {
                // Need the next larger signed type
                let needed = u.bits.max(s.bits) * 2;
                if needed <= 128 {
                    int_type(needed)
                } else {
                    // u128 + any signed int: no lossless common type
                    // on stable Rust (see ferray-core/src/dtype/promotion.rs
                    // and issue #375). Return the empty string as a
                    // sentinel; `promote_types_static` maps that to
                    // `None`, which the outer proc-macro converts
                    // into a compile error with a clear message.
                    ""
                }
            }
        }
        _ => "f64", // fallback
    }
}

/// Convert any type rank to the float bit width it requires.
const fn to_float_bits(r: TypeRank) -> u32 {
    match r.kind {
        TypeKind::Bool => 32,
        TypeKind::Unsigned | TypeKind::Signed => {
            // Integers up to 24-bit mantissa fit in f32 (i.e., i8, i16, u8, u16).
            // Larger integers need f64 (53-bit mantissa).
            if r.bits <= 16 { 32 } else { 64 }
        }
        TypeKind::Float => r.bits,
        TypeKind::Complex => r.bits,
    }
}

const fn uint_type(bits: u32) -> &'static str {
    match bits {
        8 => "u8",
        16 => "u16",
        32 => "u32",
        64 => "u64",
        128 => "u128",
        _ => "u64",
    }
}

const fn int_type(bits: u32) -> &'static str {
    match bits {
        8 => "i8",
        16 => "i16",
        32 => "i32",
        64 => "i64",
        128 => "i128",
        _ => "i64",
    }
}

const fn rank_to_type(r: TypeRank) -> &'static str {
    match r.kind {
        TypeKind::Bool => "bool",
        TypeKind::Unsigned => uint_type(r.bits),
        TypeKind::Signed => int_type(r.bits),
        TypeKind::Float => {
            if r.bits <= 16 {
                "half::f16"
            } else if r.bits <= 32 {
                "f32"
            } else {
                "f64"
            }
        }
        TypeKind::Complex => {
            if r.bits <= 32 {
                "num_complex::Complex<f32>"
            } else {
                "num_complex::Complex<f64>"
            }
        }
    }
}