Skip to content

Commit

Permalink
[naga wgsl-in] Use to_wgsl functions everywhere appropriate.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jimblandy committed Nov 15, 2023
1 parent 941cd2e commit cf2405c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 43 deletions.
4 changes: 2 additions & 2 deletions naga/src/front/wgsl/lower/construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Constructor<(Handle<crate::Type>, &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()),
}
}
}
Expand Down Expand Up @@ -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,
Expand Down
55 changes: 15 additions & 40 deletions naga/src/front/wgsl/lower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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<crate::Type>) -> 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<crate::Type> {
self.as_global().ensure_type_exists(inner)
}
Expand Down Expand Up @@ -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),
});
}
}
Expand Down Expand Up @@ -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),
});
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
});
}
};
Expand Down
29 changes: 28 additions & 1 deletion naga/src/front/wgsl/to_wgsl.rs
Original file line number Diff line number Diff line change
@@ -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<crate::Type> {
/// Formats the type as it is written in wgsl.
///
/// For example `vec3<f32>`.
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.
///
Expand All @@ -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 {
Expand Down

0 comments on commit cf2405c

Please sign in to comment.