Skip to content

Commit

Permalink
[naga wgsl-in] Preserve type names in alias declarations.
Browse files Browse the repository at this point in the history
Given a WGSL `alias` declaration, create a Naga `Type` with the
alias's name, rather than dropping the type name on the floor.
  • Loading branch information
jimblandy committed Nov 21, 2023
1 parent 42058cf commit 24d0efc
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions naga/src/front/wgsl/lower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,14 @@ impl<'source> GlobalContext<'source, '_, '_> {
}
}

fn ensure_type_exists(&mut self, inner: crate::TypeInner) -> Handle<crate::Type> {
fn ensure_type_exists(
&mut self,
name: Option<String>,
inner: crate::TypeInner,
) -> Handle<crate::Type> {
self.module
.types
.insert(crate::Type { inner, name: None }, Span::UNDEFINED)
.insert(crate::Type { inner, name }, Span::UNDEFINED)
}
}

Expand Down Expand Up @@ -635,7 +639,7 @@ impl<'source, 'temp, 'out> ExpressionContext<'source, 'temp, 'out> {
}

fn ensure_type_exists(&mut self, inner: crate::TypeInner) -> Handle<crate::Type> {
self.as_global().ensure_type_exists(inner)
self.as_global().ensure_type_exists(None, inner)
}
}

Expand Down Expand Up @@ -936,7 +940,11 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
.insert(s.name.name, LoweredGlobalDecl::Type(handle));
}
ast::GlobalDeclKind::Type(ref alias) => {
let ty = self.resolve_ast_type(alias.ty, &mut ctx)?;
let ty = self.resolve_named_ast_type(
alias.ty,
Some(alias.name.name.to_string()),
&mut ctx,
)?;
ctx.globals
.insert(alias.name.name, LoweredGlobalDecl::Type(ty));
}
Expand Down Expand Up @@ -2513,10 +2521,19 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
})
}

/// Return a Naga `Handle<Type>` representing the front-end type `handle`.
fn resolve_ast_type(
/// Build the Naga equivalent of a named AST type.
///
/// Return a Naga `Handle<Type>` representing the front-end type
/// `handle`, which should be named `name`, if given.
///
/// If `handle` refers to a type cached in [`SpecialTypes`],
/// `name` may be ignored.
///
/// [`SpecialTypes`]: crate::SpecialTypes
fn resolve_named_ast_type(
&mut self,
handle: Handle<ast::Type<'source>>,
name: Option<String>,
ctx: &mut GlobalContext<'source, '_, '_>,
) -> Result<Handle<crate::Type>, Error<'source>> {
let inner = match ctx.types[handle] {
Expand Down Expand Up @@ -2577,7 +2594,16 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
}
};

Ok(ctx.ensure_type_exists(inner))
Ok(ctx.ensure_type_exists(name, inner))
}

/// Return a Naga `Handle<Type>` representing the front-end type `handle`.
fn resolve_ast_type(
&mut self,
handle: Handle<ast::Type<'source>>,
ctx: &mut GlobalContext<'source, '_, '_>,
) -> Result<Handle<crate::Type>, Error<'source>> {
self.resolve_named_ast_type(handle, None, ctx)
}

fn binding(
Expand Down

0 comments on commit 24d0efc

Please sign in to comment.