From cf2405ca393f704c9a22c9219de1be07571a0b2b Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Tue, 14 Nov 2023 15:00:46 -0800 Subject: [PATCH] [naga wgsl-in] Use `to_wgsl` functions everywhere appropriate. Replace `ExpressionContext`'s methods `format_typeinner`, `format_type`, and `format_type_resolution` with more `to_wgsl` methods in the `naga::front::wgsl::to_wgsl` module. --- naga/src/front/wgsl/lower/construction.rs | 4 +- naga/src/front/wgsl/lower/mod.rs | 55 +++++++---------------- naga/src/front/wgsl/to_wgsl.rs | 29 +++++++++++- 3 files changed, 45 insertions(+), 43 deletions(-) diff --git a/naga/src/front/wgsl/lower/construction.rs b/naga/src/front/wgsl/lower/construction.rs index 992f9af6a4..7c06f1804e 100644 --- a/naga/src/front/wgsl/lower/construction.rs +++ b/naga/src/front/wgsl/lower/construction.rs @@ -65,7 +65,7 @@ impl Constructor<(Handle, &crate::TypeInner)> { format!("mat{}x{}", columns as u32, rows as u32,) } Self::PartialArray => "array".to_string(), - Self::Type((handle, _inner)) => ctx.format_type(handle), + Self::Type((handle, _inner)) => handle.to_wgsl(&ctx.module.to_ctx()), } } } @@ -455,7 +455,7 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { // Bad conversion (type cast) (Components::One { span, ty_inner, .. }, constructor) => { - let from_type = ctx.format_typeinner(ty_inner); + let from_type = ty_inner.to_wgsl(&ctx.module.to_ctx()); return Err(Error::BadTypeCast { span, from_type, diff --git a/naga/src/front/wgsl/lower/mod.rs b/naga/src/front/wgsl/lower/mod.rs index fbd5164671..684ba7ab0b 100644 --- a/naga/src/front/wgsl/lower/mod.rs +++ b/naga/src/front/wgsl/lower/mod.rs @@ -7,7 +7,6 @@ use crate::front::wgsl::parse::{ast, conv}; use crate::front::Typifier; use crate::proc::{ ensure_block_returns, Alignment, ConstantEvaluator, Emitter, Layouter, ResolveContext, - TypeResolution, }; use crate::{Arena, FastHashMap, FastIndexMap, Handle, Span}; @@ -59,6 +58,8 @@ macro_rules! resolve_inner_binary { /// Returns a &[`TypeResolution`]. /// /// See the documentation of [`resolve_inner!`] for why this macro is necessary. +/// +/// [`TypeResolution`]: crate::proc::TypeResolution macro_rules! resolve { ($ctx:ident, $expr:expr) => {{ $ctx.grow_types($expr)?; @@ -486,6 +487,7 @@ impl<'source, 'temp, 'out> ExpressionContext<'source, 'temp, 'out> { /// [`resolve_inner!`] or [`resolve_inner_binary!`]. /// /// [`self.typifier`]: ExpressionContext::typifier + /// [`TypeResolution`]: crate::proc::TypeResolution /// [`register_type`]: Self::register_type /// [`Typifier`]: Typifier fn grow_types( @@ -632,25 +634,6 @@ impl<'source, 'temp, 'out> ExpressionContext<'source, 'temp, 'out> { } } - fn format_typeinner(&self, inner: &crate::TypeInner) -> String { - inner.to_wgsl(&self.module.to_ctx()) - } - - fn format_type(&self, handle: Handle) -> String { - let ty = &self.module.types[handle]; - match ty.name { - Some(ref name) => name.clone(), - None => self.format_typeinner(&ty.inner), - } - } - - fn format_type_resolution(&self, resolution: &TypeResolution) -> String { - match *resolution { - TypeResolution::Handle(handle) => self.format_type(handle), - TypeResolution::Value(ref inner) => self.format_typeinner(inner), - } - } - fn ensure_type_exists(&mut self, inner: crate::TypeInner) -> Handle { self.as_global().ensure_type_exists(inner) } @@ -925,22 +908,11 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { if let Some(explicit) = explicit_ty { if explicit != inferred_type { - let ty = &ctx.module.types[explicit]; - let expected = ty - .name - .clone() - .unwrap_or_else(|| ty.inner.to_wgsl(&ctx.module.to_ctx())); - - let ty = &ctx.module.types[inferred_type]; - let got = ty - .name - .clone() - .unwrap_or_else(|| ty.inner.to_wgsl(&ctx.module.to_ctx())); - + let gctx = ctx.module.to_ctx(); return Err(Error::InitializationTypeMismatch { name: c.name.span, - expected, - got, + expected: explicit.to_wgsl(&gctx), + got: inferred_type.to_wgsl(&gctx), }); } } @@ -1128,10 +1100,11 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { .inner .equivalent(&ctx.module.types[init_ty].inner, &ctx.module.types) { + let gctx = &ctx.module.to_ctx(); return Err(Error::InitializationTypeMismatch { name: l.name.span, - expected: ctx.format_type(ty), - got: ctx.format_type(init_ty), + expected: ty.to_wgsl(gctx), + got: init_ty.to_wgsl(gctx), }); } } @@ -1166,10 +1139,11 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { .inner .equivalent(initializer_ty, &ctx.module.types) { + let gctx = &ctx.module.to_ctx(); return Err(Error::InitializationTypeMismatch { name: v.name.span, - expected: ctx.format_type(explicit), - got: ctx.format_typeinner(initializer_ty), + expected: explicit.to_wgsl(gctx), + got: initializer_ty.to_wgsl(gctx), }); } explicit @@ -1677,10 +1651,11 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { crate::TypeInner::Vector { scalar, .. } => scalar, _ => { let ty = resolve!(ctx, expr); + let gctx = &ctx.module.to_ctx(); return Err(Error::BadTypeCast { - from_type: ctx.format_type_resolution(ty), + from_type: ty.to_wgsl(gctx), span: ty_span, - to_type: ctx.format_type(to_resolved), + to_type: to_resolved.to_wgsl(gctx), }); } }; diff --git a/naga/src/front/wgsl/to_wgsl.rs b/naga/src/front/wgsl/to_wgsl.rs index 88dbf575f2..7af3debc4c 100644 --- a/naga/src/front/wgsl/to_wgsl.rs +++ b/naga/src/front/wgsl/to_wgsl.rs @@ -1,3 +1,30 @@ +//! Producing the WGSL forms of types, for use in error messages. + +use crate::proc::GlobalCtx; +use crate::Handle; + +impl crate::proc::TypeResolution { + pub fn to_wgsl(&self, gctx: &GlobalCtx) -> String { + match *self { + crate::proc::TypeResolution::Handle(handle) => handle.to_wgsl(gctx), + crate::proc::TypeResolution::Value(ref inner) => inner.to_wgsl(gctx), + } + } +} + +impl Handle { + /// Formats the type as it is written in wgsl. + /// + /// For example `vec3`. + pub fn to_wgsl(self, gctx: &GlobalCtx) -> String { + let ty = &gctx.types[self]; + match ty.name { + Some(ref name) => name.clone(), + None => ty.inner.to_wgsl(gctx), + } + } +} + impl crate::TypeInner { /// Formats the type as it is written in wgsl. /// @@ -6,7 +33,7 @@ impl crate::TypeInner { /// Note: `TypeInner::Struct` doesn't include the name of the /// struct type. Therefore this method will simply return "struct" /// for them. - pub fn to_wgsl(&self, gctx: &crate::proc::GlobalCtx) -> String { + pub fn to_wgsl(&self, gctx: &GlobalCtx) -> String { use crate::TypeInner as Ti; match *self {