use crate::ir::{NonSemantic, Variable};
use super::CubeContext;
#[track_caller]
pub fn debug_call_expand<C>(
context: &mut CubeContext,
name: &'static str,
line: u32,
col: u32,
call: impl FnOnce(&mut CubeContext) -> C,
) -> C {
if context.debug_enabled {
context.register(NonSemantic::BeginCall {
name: name.to_string(),
line,
col,
});
let ret = call(context);
context.register(NonSemantic::EndCall);
ret
} else {
call(context)
}
}
#[track_caller]
pub fn spanned_expand<C>(
context: &mut CubeContext,
line: u32,
col: u32,
call: impl FnOnce(&mut CubeContext) -> C,
) -> C {
if context.debug_enabled {
context.register(NonSemantic::Line { line, col });
call(context)
} else {
call(context)
}
}
#[track_caller]
pub fn debug_source_expand(context: &mut CubeContext, name: &str, file: &str, line: u32, col: u32) {
if context.debug_enabled {
let file = file.replace("\\", "/");
context.register(NonSemantic::Source {
name: name.into(),
file_name: format!("./{file}"),
line,
col,
});
}
}
pub fn printf_expand(
context: &mut CubeContext,
format_string: impl Into<String>,
args: Vec<Variable>,
) {
context.register(NonSemantic::Print {
format_string: format_string.into(),
args,
});
}
#[macro_export]
macro_rules! debug_print {
($format:literal, $($args:expr),*) => {
{
let _ = $format;
$(let _ = $args;)*
}
};
($format:literal, $($args:expr,)*) => {
$crate::debug_print!($format, $($args),*);
};
}
#[macro_export]
macro_rules! debug_print_expand {
($context:expr, $format:literal, $($args:expr),*) => {
{
let args = vec![$(*$args.expand),*];
$crate::frontend::printf_expand($context, $format, args);
}
};
($format:literal, $($args:expr,)*) => {
$crate::debug_print_expand!($format, $($args),*)
};
}