|
| 1 | +//! Wrappers for displaying Naga IR terms in diagnostic output. |
| 2 | +
|
| 3 | +use crate::proc::GlobalCtx; |
| 4 | +use crate::{Handle, Scalar, Type, TypeInner}; |
| 5 | + |
| 6 | +#[cfg(any(feature = "wgsl-in", feature = "wgsl-out"))] |
| 7 | +use crate::common::wgsl::{TryToWgsl, TypeContext}; |
| 8 | + |
| 9 | +use core::fmt; |
| 10 | + |
| 11 | +/// A wrapper for displaying Naga IR terms in diagnostic output. |
| 12 | +/// |
| 13 | +/// For some Naga IR type `T`, `DiagnosticDisplay<T>` implements |
| 14 | +/// [`core::fmt::Display`] in a way that displays values of type `T` |
| 15 | +/// appropriately for diagnostic messages presented to human readers. |
| 16 | +/// |
| 17 | +/// For example, the implementation of [`Display`] for |
| 18 | +/// `DiagnosticDisplay<Scalar>` formats the type represented by the |
| 19 | +/// given [`Scalar`] appropriately for users. |
| 20 | +/// |
| 21 | +/// Some types like `Handle<Type>` require contextual information like |
| 22 | +/// a type arena to be displayed. In such cases, we implement [`Display`] |
| 23 | +/// for a type like `DiagnosticDisplay<(Handle<Type>, GlobalCtx)>`, where |
| 24 | +/// the [`GlobalCtx`] type provides the necessary context. |
| 25 | +/// |
| 26 | +/// [`Display`]: core::fmt::Display |
| 27 | +/// [`Scalar`]: crate::Scalar |
| 28 | +/// [`GlobalCtx`]: crate::proc::GlobalCtx |
| 29 | +/// |
| 30 | +/// ## Language-sensitive diagnostics |
| 31 | +/// |
| 32 | +/// As discussed in #7268, diagnostic output ought to depend on the |
| 33 | +/// source language from which the IR was produced: diagnostics |
| 34 | +/// resulting from processing GLSL code should use GLSL type syntax, |
| 35 | +/// for example. That means that `DiagnosticDisplay` ought to include |
| 36 | +/// some indication of which notation to use. |
| 37 | +/// |
| 38 | +/// For the moment, only WGSL output is implemented, so |
| 39 | +/// `DiagnosticDisplay` lacks any support for this. However, the plan |
| 40 | +/// is that all language-independent code in Naga should use |
| 41 | +/// `DiagnosticDisplay` wherever appropriate, such that when its |
| 42 | +/// definition is expanded to include some indication of the right |
| 43 | +/// source language to use, any use site that does not supply this |
| 44 | +/// indication will provoke a compile-time error. |
| 45 | +pub struct DiagnosticDisplay<T>(pub T); |
| 46 | + |
| 47 | +impl fmt::Display for DiagnosticDisplay<(Handle<Type>, &GlobalCtx<'_>)> { |
| 48 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 49 | + let (handle, ctx) = self.0; |
| 50 | + |
| 51 | + #[cfg(feature = "wgsl-in")] |
| 52 | + ctx.write_type(handle, f)?; |
| 53 | + |
| 54 | + #[cfg(not(feature = "wgsl-in"))] |
| 55 | + { |
| 56 | + let _ = ctx; |
| 57 | + write!(f, "{handle:?}")?; |
| 58 | + } |
| 59 | + |
| 60 | + Ok(()) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +impl<'m> fmt::Display for DiagnosticDisplay<(&'m TypeInner, &GlobalCtx<'m>)> { |
| 65 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 66 | + let (inner, ctx) = self.0; |
| 67 | + |
| 68 | + #[cfg(feature = "wgsl-in")] |
| 69 | + ctx.write_type_inner(inner, f)?; |
| 70 | + |
| 71 | + #[cfg(not(feature = "wgsl-in"))] |
| 72 | + { |
| 73 | + let _ = ctx; |
| 74 | + write!(f, "{inner:?}")?; |
| 75 | + } |
| 76 | + |
| 77 | + Ok(()) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl<'m> fmt::Display for DiagnosticDisplay<(&'m TypeInner, &crate::UniqueArena<Type>)> { |
| 82 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 83 | + let (inner, ctx) = self.0; |
| 84 | + |
| 85 | + #[cfg(feature = "wgsl-in")] |
| 86 | + ctx.write_type_inner(inner, f)?; |
| 87 | + |
| 88 | + #[cfg(not(feature = "wgsl-in"))] |
| 89 | + { |
| 90 | + let _ = ctx; |
| 91 | + write!(f, "{inner:?}")?; |
| 92 | + } |
| 93 | + |
| 94 | + Ok(()) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +impl fmt::Display for DiagnosticDisplay<Scalar> { |
| 99 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 100 | + let scalar = self.0; |
| 101 | + |
| 102 | + #[cfg(feature = "wgsl-in")] |
| 103 | + match scalar.try_to_wgsl() { |
| 104 | + Some(string) => f.write_str(string)?, |
| 105 | + None => crate::common::wgsl::write_scalar_for_diagnostics(scalar, f)?, |
| 106 | + } |
| 107 | + |
| 108 | + #[cfg(not(feature = "wgsl-in"))] |
| 109 | + write!(f, "{scalar:?}")?; |
| 110 | + |
| 111 | + Ok(()) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +/// A wrapper for displaying types with [`core::fmt::Debug`]. |
| 116 | +/// |
| 117 | +/// Since we expect this to be implemented in ad-hoc ways across Naga, |
| 118 | +/// we don't supply interesting implementations here. |
| 119 | +pub struct DiagnosticDebug<T>(pub T); |
0 commit comments