Skip to content

Commit 21517b9

Browse files
committed
fix: implement parameter counting in zrtl_symbol_sig macro
The zrtl_symbol_sig! macro was hardcoded to set param_count=0 for functions with signature `($($param)*) -> opaque`. This prevented proper type checking of extern function parameters. **Changes:** - Add __count_params! macro to count parameters in signature - Add __build_param_array! macro to build TypeTag array from params - Add __fill_param_array! helper to populate parameter types - Support f32, f64, i32, i64, u32, u64, bool parameter types - Update zrtl_symbol_sig! to use counted parameters **Result:** Plugin signatures now correctly report parameter counts: - Before: $Tensor$arange_f32 param_count=0 - After: $Tensor$arange_f32 param_count=3 This enables the type checker to create properly typed extern function declarations with correct parameter lists. **Example:** ```rust ("$Tensor$arange_f32", tensor_arange_f32, (f32, f32, f32) -> opaque) ``` Now generates a signature with param_count=3 and params=[F32, F32, F32].
1 parent 1228e97 commit 21517b9

2 files changed

Lines changed: 96 additions & 2 deletions

File tree

crates/zyntax_embed/src/grammar.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ impl LanguageGrammar {
448448
};
449449

450450
// Create extern function declaration
451+
eprintln!("[DEBUG inject_builtin_externs] Creating extern func {} with {} params", target_symbol, params.len());
451452
let extern_func = TypedFunction {
452453
name: InternedString::new_global(target_symbol),
453454
type_params: vec![],

sdk/zrtl/src/plugin.rs

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,15 @@ macro_rules! zrtl_symbol_sig {
267267
// Function with any params that returns opaque pointer
268268
// Match: (...) -> opaque where ... is anything
269269
($name:expr, $func:ident, ($($param:tt)*) -> opaque) => {{
270+
// Count and build param array
271+
const PARAM_COUNT: u8 = $crate::__count_params!($($param)*);
272+
const PARAM_ARRAY: [$crate::TypeTag; $crate::MAX_PARAMS] = $crate::__build_param_array!($($param)*);
273+
270274
static SIG: $crate::ZrtlSymbolSig = $crate::ZrtlSymbolSig {
271-
param_count: 0, // We don't parse param types yet, just mark return as opaque
275+
param_count: PARAM_COUNT,
272276
flags: $crate::ZrtlSigFlags::NONE,
273277
return_type: $crate::TypeTag::DYNAMIC_BOX, // Mark as opaque
274-
params: [$crate::TypeTag::VOID; $crate::MAX_PARAMS],
278+
params: PARAM_ARRAY,
275279
};
276280
$crate::ZrtlSymbol::with_sig(
277281
concat!($name, "\0").as_ptr() as *const ::std::ffi::c_char,
@@ -319,6 +323,95 @@ macro_rules! __count_symbols {
319323
(($($tt:tt)*) $($rest:tt)*) => { 1 + $crate::__count_symbols!($($rest)*) };
320324
}
321325

326+
/// Internal helper to count parameters in a signature
327+
#[macro_export]
328+
#[doc(hidden)]
329+
macro_rules! __count_params {
330+
() => { 0 };
331+
(f32) => { 1 };
332+
(f64) => { 1 };
333+
(i32) => { 1 };
334+
(i64) => { 1 };
335+
(u32) => { 1 };
336+
(u64) => { 1 };
337+
(bool) => { 1 };
338+
(f32, $($rest:tt)*) => { 1 + $crate::__count_params!($($rest)*) };
339+
(f64, $($rest:tt)*) => { 1 + $crate::__count_params!($($rest)*) };
340+
(i32, $($rest:tt)*) => { 1 + $crate::__count_params!($($rest)*) };
341+
(i64, $($rest:tt)*) => { 1 + $crate::__count_params!($($rest)*) };
342+
(u32, $($rest:tt)*) => { 1 + $crate::__count_params!($($rest)*) };
343+
(u64, $($rest:tt)*) => { 1 + $crate::__count_params!($($rest)*) };
344+
(bool, $($rest:tt)*) => { 1 + $crate::__count_params!($($rest)*) };
345+
}
346+
347+
/// Internal helper to build parameter type array
348+
#[macro_export]
349+
#[doc(hidden)]
350+
macro_rules! __build_param_array {
351+
() => { [$crate::TypeTag::VOID; $crate::MAX_PARAMS] };
352+
($($param:tt)*) => {{
353+
let mut params = [$crate::TypeTag::VOID; $crate::MAX_PARAMS];
354+
$crate::__fill_param_array!(params, 0, $($param)*);
355+
params
356+
}};
357+
}
358+
359+
/// Internal helper to fill parameter array
360+
#[macro_export]
361+
#[doc(hidden)]
362+
macro_rules! __fill_param_array {
363+
($array:ident, $idx:expr,) => {};
364+
($array:ident, $idx:expr, f32) => {
365+
$array[$idx] = $crate::TypeTag::F32;
366+
};
367+
($array:ident, $idx:expr, f64) => {
368+
$array[$idx] = $crate::TypeTag::F64;
369+
};
370+
($array:ident, $idx:expr, i32) => {
371+
$array[$idx] = $crate::TypeTag::I32;
372+
};
373+
($array:ident, $idx:expr, i64) => {
374+
$array[$idx] = $crate::TypeTag::I64;
375+
};
376+
($array:ident, $idx:expr, u32) => {
377+
$array[$idx] = $crate::TypeTag::U32;
378+
};
379+
($array:ident, $idx:expr, u64) => {
380+
$array[$idx] = $crate::TypeTag::U64;
381+
};
382+
($array:ident, $idx:expr, bool) => {
383+
$array[$idx] = $crate::TypeTag::BOOL;
384+
};
385+
($array:ident, $idx:expr, f32, $($rest:tt)*) => {
386+
$array[$idx] = $crate::TypeTag::F32;
387+
$crate::__fill_param_array!($array, $idx + 1, $($rest)*);
388+
};
389+
($array:ident, $idx:expr, f64, $($rest:tt)*) => {
390+
$array[$idx] = $crate::TypeTag::F64;
391+
$crate::__fill_param_array!($array, $idx + 1, $($rest)*);
392+
};
393+
($array:ident, $idx:expr, i32, $($rest:tt)*) => {
394+
$array[$idx] = $crate::TypeTag::I32;
395+
$crate::__fill_param_array!($array, $idx + 1, $($rest)*);
396+
};
397+
($array:ident, $idx:expr, i64, $($rest:tt)*) => {
398+
$array[$idx] = $crate::TypeTag::I64;
399+
$crate::__fill_param_array!($array, $idx + 1, $($rest)*);
400+
};
401+
($array:ident, $idx:expr, u32, $($rest:tt)*) => {
402+
$array[$idx] = $crate::TypeTag::U32;
403+
$crate::__fill_param_array!($array, $idx + 1, $($rest)*);
404+
};
405+
($array:ident, $idx:expr, u64, $($rest:tt)*) => {
406+
$array[$idx] = $crate::TypeTag::U64;
407+
$crate::__fill_param_array!($array, $idx + 1, $($rest)*);
408+
};
409+
($array:ident, $idx:expr, bool, $($rest:tt)*) => {
410+
$array[$idx] = $crate::TypeTag::BOOL;
411+
$crate::__fill_param_array!($array, $idx + 1, $($rest)*);
412+
};
413+
}
414+
322415
/// Macro to define a complete ZRTL plugin
323416
///
324417
/// This creates both the symbol table and plugin info exports.

0 commit comments

Comments
 (0)