naga/common/diagnostic_debug.rs
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
//! Displaying Naga IR terms in debugging output.
#[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))]
use crate::common::wgsl::TypeContext;
use crate::proc::TypeResolution;
use crate::{Handle, Scalar, Type, TypeInner, UniqueArena};
use core::fmt;
/// A wrapper for displaying Naga IR terms in debugging output.
///
/// This is like [`DiagnosticDisplay`], but requires weaker context
/// and produces correspondingly lower-fidelity output. For example,
/// this cannot show the override names for override-sized array
/// lengths.
///
/// [`DiagnosticDisplay`]: super::DiagnosticDisplay
pub struct DiagnosticDebug<T>(pub T);
impl fmt::Debug for DiagnosticDebug<(Handle<Type>, &UniqueArena<Type>)> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (handle, ctx) = self.0;
#[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))]
ctx.write_type(handle, f)?;
#[cfg(not(any(feature = "wgsl-in", feature = "wgsl-out")))]
{
let _ = ctx;
write!(f, "{handle:?}")?;
}
Ok(())
}
}
impl fmt::Debug for DiagnosticDebug<(&TypeInner, &UniqueArena<Type>)> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (inner, ctx) = self.0;
#[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))]
ctx.write_type_inner(inner, f)?;
#[cfg(not(any(feature = "wgsl-in", feature = "wgsl-out")))]
{
let _ = ctx;
write!(f, "{inner:?}")?;
}
Ok(())
}
}
impl fmt::Debug for DiagnosticDebug<(&TypeResolution, &UniqueArena<Type>)> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (resolution, ctx) = self.0;
#[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))]
ctx.write_type_resolution(resolution, f)?;
#[cfg(not(any(feature = "wgsl-in", feature = "wgsl-out")))]
{
let _ = ctx;
write!(f, "{resolution:?}")?;
}
Ok(())
}
}
impl fmt::Debug for DiagnosticDebug<Scalar> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let scalar = self.0;
#[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))]
f.write_str(&crate::common::wgsl::TryToWgsl::to_wgsl_for_diagnostics(
scalar,
))?;
#[cfg(not(any(feature = "wgsl-in", feature = "wgsl-out")))]
write!(f, "{scalar:?}")?;
Ok(())
}
}
pub trait ForDebug: Sized {
/// Format this type using [`core::fmt::Debug`].
///
/// Return a value that implements the [`core::fmt::Debug`] trait
/// by displaying `self` in a language-appropriate way. For
/// example:
///
/// # use naga::common::ForDebug;
/// # let scalar: naga::Scalar = naga::Scalar::F32;
/// log::debug!("My scalar: {:?}", scalar.for_debug());
fn for_debug(self) -> DiagnosticDebug<Self> {
DiagnosticDebug(self)
}
}
impl ForDebug for Scalar {}
pub trait ForDebugWithTypes: Sized {
/// Format this type using [`core::fmt::Debug`].
///
/// Given an arena to look up type handles in, return a value that
/// implements the [`core::fmt::Debug`] trait by displaying `self`
/// in a language-appropriate way. For example:
///
/// # use naga::{Span, Type, TypeInner, Scalar, UniqueArena};
/// # use naga::common::ForDebugWithTypes;
/// # let mut types = UniqueArena::<Type>::default();
/// # let inner = TypeInner::Scalar(Scalar::F32);
/// # let span = Span::UNDEFINED;
/// # let handle = types.insert(Type { name: None, inner }, span);
/// log::debug!("My type: {:?}", handle.for_debug(&types));
fn for_debug(self, types: &UniqueArena<Type>) -> DiagnosticDebug<(Self, &UniqueArena<Type>)> {
DiagnosticDebug((self, types))
}
}
impl ForDebugWithTypes for Handle<Type> {}
impl ForDebugWithTypes for &TypeInner {}
impl ForDebugWithTypes for &TypeResolution {}