Debian Codegraph @

third_party/rust/naga/src/common/diagnostic_debug.rs

raw
1 //! Displaying Naga IR terms in debugging output.
2
3 #[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))]
4 use crate::common::wgsl::TypeContext;
5
6 use crate::proc::TypeResolution;
7 use crate::{Handle, Scalar, Type, TypeInner, UniqueArena};
8
9 use core::fmt;
10
11 /// A wrapper for displaying Naga IR terms in debugging output.
12 ///
13 /// This is like [`DiagnosticDisplay`], but requires weaker context
14 /// and produces correspondingly lower-fidelity output. For example,
15 /// this cannot show the override names for override-sized array
16 /// lengths.
17 ///
18 /// [`DiagnosticDisplay`]: super::DiagnosticDisplay
19 pub struct DiagnosticDebug<T>(pub T);
20
21 impl fmt::Debug for DiagnosticDebug<(Handle<Type>, &UniqueArena<Type>)> {
22 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23 let (handle, ctx) = self.0;
24
25 #[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))]
26 ctx.write_type(handle, f)?;
27
28 #[cfg(not(any(feature = "wgsl-in", feature = "wgsl-out")))]
29 {
30 let _ = ctx;
31 write!(f, "{handle:?}")?;
32 }
33
34 Ok(())
35 }
36 }
37
38 impl fmt::Debug for DiagnosticDebug<(&TypeInner, &UniqueArena<Type>)> {
39 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40 let (inner, ctx) = self.0;
41
42 #[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))]
43 ctx.write_type_inner(inner, f)?;
44
45 #[cfg(not(any(feature = "wgsl-in", feature = "wgsl-out")))]
46 {
47 let _ = ctx;
48 write!(f, "{inner:?}")?;
49 }
50
51 Ok(())
52 }
53 }
54
55 impl fmt::Debug for DiagnosticDebug<(&TypeResolution, &UniqueArena<Type>)> {
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 let (resolution, ctx) = self.0;
58
59 #[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))]
60 ctx.write_type_resolution(resolution, f)?;
61
62 #[cfg(not(any(feature = "wgsl-in", feature = "wgsl-out")))]
63 {
64 let _ = ctx;
65 write!(f, "{resolution:?}")?;
66 }
67
68 Ok(())
69 }
70 }
71
72 impl fmt::Debug for DiagnosticDebug<Scalar> {
73 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74 let scalar = self.0;
75
76 #[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))]
77 f.write_str(&crate::common::wgsl::TryToWgsl::to_wgsl_for_diagnostics(
78 scalar,
79 ))?;
80
81 #[cfg(not(any(feature = "wgsl-in", feature = "wgsl-out")))]
82 write!(f, "{scalar:?}")?;
83
84 Ok(())
85 }
86 }
87
88 pub trait ForDebug: Sized {
89 /// Format this type using [`core::fmt::Debug`].
90 ///
91 /// Return a value that implements the [`core::fmt::Debug`] trait
92 /// by displaying `self` in a language-appropriate way. For
93 /// example:
94 ///
95 /// # use naga::common::ForDebug;
96 /// # let scalar: naga::Scalar = naga::Scalar::F32;
97 /// log::debug!("My scalar: {:?}", scalar.for_debug());
98 fn for_debug(self) -> DiagnosticDebug<Self> {
99 DiagnosticDebug(self)
100 }
101 }
102
103 impl ForDebug for Scalar {}
104
105 pub trait ForDebugWithTypes: Sized {
106 /// Format this type using [`core::fmt::Debug`].
107 ///
108 /// Given an arena to look up type handles in, return a value that
109 /// implements the [`core::fmt::Debug`] trait by displaying `self`
110 /// in a language-appropriate way. For example:
111 ///
112 /// # use naga::{Span, Type, TypeInner, Scalar, UniqueArena};
113 /// # use naga::common::ForDebugWithTypes;
114 /// # let mut types = UniqueArena::<Type>::default();
115 /// # let inner = TypeInner::Scalar(Scalar::F32);
116 /// # let span = Span::UNDEFINED;
117 /// # let handle = types.insert(Type { name: None, inner }, span);
118 /// log::debug!("My type: {:?}", handle.for_debug(&types));
119 fn for_debug(self, types: &UniqueArena<Type>) -> DiagnosticDebug<(Self, &UniqueArena<Type>)> {
120 DiagnosticDebug((self, types))
121 }
122 }
123
124 impl ForDebugWithTypes for Handle<Type> {}
125 impl ForDebugWithTypes for &TypeInner {}
126 impl ForDebugWithTypes for &TypeResolution {}