naga/front/wgsl/lower/
mod.rs

1use alloc::{
2    borrow::ToOwned,
3    boxed::Box,
4    format,
5    string::{String, ToString},
6    vec::Vec,
7};
8use core::num::NonZeroU32;
9
10use crate::front::wgsl::error::{Error, ExpectedToken, InvalidAssignmentType};
11use crate::front::wgsl::index::Index;
12use crate::front::wgsl::parse::directive::enable_extension::EnableExtensions;
13use crate::front::wgsl::parse::number::Number;
14use crate::front::wgsl::parse::{ast, conv};
15use crate::front::wgsl::Result;
16use crate::front::Typifier;
17use crate::{
18    common::wgsl::{TryToWgsl, TypeContext},
19    compact::KeepUnused,
20};
21use crate::{common::ForDebugWithTypes, proc::LayoutErrorInner};
22use crate::{ir, proc};
23use crate::{Arena, FastHashMap, FastIndexMap, Handle, Span};
24
25use construction::Constructor;
26use template_list::TemplateListIter;
27
28mod construction;
29mod conversion;
30mod template_list;
31
32/// Resolves the inner type of a given expression.
33///
34/// Expects a &mut [`ExpressionContext`] and a [`Handle<Expression>`].
35///
36/// Returns a &[`ir::TypeInner`].
37///
38/// Ideally, we would simply have a function that takes a `&mut ExpressionContext`
39/// and returns a `&TypeResolution`. Unfortunately, this leads the borrow checker
40/// to conclude that the mutable borrow lasts for as long as we are using the
41/// `&TypeResolution`, so we can't use the `ExpressionContext` for anything else -
42/// like, say, resolving another operand's type. Using a macro that expands to
43/// two separate calls, only the first of which needs a `&mut`,
44/// lets the borrow checker see that the mutable borrow is over.
45macro_rules! resolve_inner {
46    ($ctx:ident, $expr:expr) => {{
47        $ctx.grow_types($expr)?;
48        $ctx.typifier()[$expr].inner_with(&$ctx.module.types)
49    }};
50}
51pub(super) use resolve_inner;
52
53/// Resolves the inner types of two given expressions.
54///
55/// Expects a &mut [`ExpressionContext`] and two [`Handle<Expression>`]s.
56///
57/// Returns a tuple containing two &[`ir::TypeInner`].
58///
59/// See the documentation of [`resolve_inner!`] for why this macro is necessary.
60macro_rules! resolve_inner_binary {
61    ($ctx:ident, $left:expr, $right:expr) => {{
62        $ctx.grow_types($left)?;
63        $ctx.grow_types($right)?;
64        (
65            $ctx.typifier()[$left].inner_with(&$ctx.module.types),
66            $ctx.typifier()[$right].inner_with(&$ctx.module.types),
67        )
68    }};
69}
70
71/// Resolves the type of a given expression.
72///
73/// Expects a &mut [`ExpressionContext`] and a [`Handle<Expression>`].
74///
75/// Returns a &[`TypeResolution`].
76///
77/// See the documentation of [`resolve_inner!`] for why this macro is necessary.
78///
79/// [`TypeResolution`]: proc::TypeResolution
80macro_rules! resolve {
81    ($ctx:ident, $expr:expr) => {{
82        let expr = $expr;
83        $ctx.grow_types(expr)?;
84        &$ctx.typifier()[expr]
85    }};
86}
87pub(super) use resolve;
88
89/// State for constructing a `ir::Module`.
90pub struct GlobalContext<'source, 'temp, 'out> {
91    enable_extensions: EnableExtensions,
92
93    /// The `TranslationUnit`'s expressions arena.
94    ast_expressions: &'temp Arena<ast::Expression<'source>>,
95
96    // Naga IR values.
97    /// The map from the names of module-scope declarations to the Naga IR
98    /// `Handle`s we have built for them, owned by `Lowerer::lower`.
99    globals: &'temp mut FastHashMap<&'source str, LoweredGlobalDecl>,
100
101    /// The module we're constructing.
102    module: &'out mut ir::Module,
103
104    const_typifier: &'temp mut Typifier,
105
106    layouter: &'temp mut proc::Layouter,
107
108    global_expression_kind_tracker: &'temp mut proc::ExpressionKindTracker,
109}
110
111impl<'source> GlobalContext<'source, '_, '_> {
112    const fn as_const(&mut self) -> ExpressionContext<'source, '_, '_> {
113        ExpressionContext {
114            enable_extensions: self.enable_extensions,
115            ast_expressions: self.ast_expressions,
116            globals: self.globals,
117            module: self.module,
118            const_typifier: self.const_typifier,
119            layouter: self.layouter,
120            expr_type: ExpressionContextType::Constant(None),
121            global_expression_kind_tracker: self.global_expression_kind_tracker,
122        }
123    }
124
125    const fn as_override(&mut self) -> ExpressionContext<'source, '_, '_> {
126        ExpressionContext {
127            enable_extensions: self.enable_extensions,
128            ast_expressions: self.ast_expressions,
129            globals: self.globals,
130            module: self.module,
131            const_typifier: self.const_typifier,
132            layouter: self.layouter,
133            expr_type: ExpressionContextType::Override,
134            global_expression_kind_tracker: self.global_expression_kind_tracker,
135        }
136    }
137
138    fn ensure_type_exists(
139        &mut self,
140        name: Option<String>,
141        inner: ir::TypeInner,
142    ) -> Handle<ir::Type> {
143        self.module
144            .types
145            .insert(ir::Type { inner, name }, Span::UNDEFINED)
146    }
147}
148
149/// State for lowering a statement within a function.
150pub struct StatementContext<'source, 'temp, 'out> {
151    enable_extensions: EnableExtensions,
152
153    // WGSL AST values.
154    /// A reference to [`TranslationUnit::expressions`] for the translation unit
155    /// we're lowering.
156    ///
157    /// [`TranslationUnit::expressions`]: ast::TranslationUnit::expressions
158    ast_expressions: &'temp Arena<ast::Expression<'source>>,
159
160    // Naga IR values.
161    /// The map from the names of module-scope declarations to the Naga IR
162    /// `Handle`s we have built for them, owned by `Lowerer::lower`.
163    globals: &'temp mut FastHashMap<&'source str, LoweredGlobalDecl>,
164
165    /// A map from each `ast::Local` handle to the Naga expression
166    /// we've built for it:
167    ///
168    /// - WGSL function arguments become Naga [`FunctionArgument`] expressions.
169    ///
170    /// - WGSL `var` declarations become Naga [`LocalVariable`] expressions.
171    ///
172    /// - WGSL `let` declararations become arbitrary Naga expressions.
173    ///
174    /// This always borrows the `local_table` local variable in
175    /// [`Lowerer::function`].
176    ///
177    /// [`LocalVariable`]: ir::Expression::LocalVariable
178    /// [`FunctionArgument`]: ir::Expression::FunctionArgument
179    local_table:
180        &'temp mut FastHashMap<Handle<ast::Local>, Declared<Typed<Handle<ir::Expression>>>>,
181
182    const_typifier: &'temp mut Typifier,
183    typifier: &'temp mut Typifier,
184    layouter: &'temp mut proc::Layouter,
185    function: &'out mut ir::Function,
186    /// Stores the names of expressions that are assigned in `let` statement
187    /// Also stores the spans of the names, for use in errors.
188    named_expressions: &'out mut FastIndexMap<Handle<ir::Expression>, (String, Span)>,
189    module: &'out mut ir::Module,
190
191    /// Which `Expression`s in `self.naga_expressions` are const expressions, in
192    /// the WGSL sense.
193    ///
194    /// According to the WGSL spec, a const expression must not refer to any
195    /// `let` declarations, even if those declarations' initializers are
196    /// themselves const expressions. So this tracker is not simply concerned
197    /// with the form of the expressions; it is also tracking whether WGSL says
198    /// we should consider them to be const. See the use of `force_non_const` in
199    /// the code for lowering `let` bindings.
200    local_expression_kind_tracker: &'temp mut proc::ExpressionKindTracker,
201    global_expression_kind_tracker: &'temp mut proc::ExpressionKindTracker,
202}
203
204impl<'a, 'temp> StatementContext<'a, 'temp, '_> {
205    const fn as_const<'t>(
206        &'t mut self,
207        block: &'t mut ir::Block,
208        emitter: &'t mut proc::Emitter,
209    ) -> ExpressionContext<'a, 't, 't>
210    where
211        'temp: 't,
212    {
213        ExpressionContext {
214            enable_extensions: self.enable_extensions,
215            globals: self.globals,
216            ast_expressions: self.ast_expressions,
217            const_typifier: self.const_typifier,
218            layouter: self.layouter,
219            global_expression_kind_tracker: self.global_expression_kind_tracker,
220            module: self.module,
221            expr_type: ExpressionContextType::Constant(Some(LocalExpressionContext {
222                local_table: self.local_table,
223                function: self.function,
224                block,
225                emitter,
226                typifier: self.typifier,
227                local_expression_kind_tracker: self.local_expression_kind_tracker,
228            })),
229        }
230    }
231
232    const fn as_expression<'t>(
233        &'t mut self,
234        block: &'t mut ir::Block,
235        emitter: &'t mut proc::Emitter,
236    ) -> ExpressionContext<'a, 't, 't>
237    where
238        'temp: 't,
239    {
240        ExpressionContext {
241            enable_extensions: self.enable_extensions,
242            globals: self.globals,
243            ast_expressions: self.ast_expressions,
244            const_typifier: self.const_typifier,
245            layouter: self.layouter,
246            global_expression_kind_tracker: self.global_expression_kind_tracker,
247            module: self.module,
248            expr_type: ExpressionContextType::Runtime(LocalExpressionContext {
249                local_table: self.local_table,
250                function: self.function,
251                block,
252                emitter,
253                typifier: self.typifier,
254                local_expression_kind_tracker: self.local_expression_kind_tracker,
255            }),
256        }
257    }
258
259    #[allow(dead_code)]
260    const fn as_global(&mut self) -> GlobalContext<'a, '_, '_> {
261        GlobalContext {
262            enable_extensions: self.enable_extensions,
263            ast_expressions: self.ast_expressions,
264            globals: self.globals,
265            module: self.module,
266            const_typifier: self.const_typifier,
267            layouter: self.layouter,
268            global_expression_kind_tracker: self.global_expression_kind_tracker,
269        }
270    }
271
272    fn invalid_assignment_type(&self, expr: Handle<ir::Expression>) -> InvalidAssignmentType {
273        if let Some(&(_, span)) = self.named_expressions.get(&expr) {
274            InvalidAssignmentType::ImmutableBinding(span)
275        } else {
276            match self.function.expressions[expr] {
277                ir::Expression::Swizzle { .. } => InvalidAssignmentType::Swizzle,
278                ir::Expression::Access { base, .. } => self.invalid_assignment_type(base),
279                ir::Expression::AccessIndex { base, .. } => self.invalid_assignment_type(base),
280                _ => InvalidAssignmentType::Other,
281            }
282        }
283    }
284}
285
286pub struct LocalExpressionContext<'temp, 'out> {
287    /// A map from [`ast::Local`] handles to the Naga expressions we've built for them.
288    ///
289    /// This is always [`StatementContext::local_table`] for the
290    /// enclosing statement; see that documentation for details.
291    local_table: &'temp FastHashMap<Handle<ast::Local>, Declared<Typed<Handle<ir::Expression>>>>,
292
293    function: &'out mut ir::Function,
294    block: &'temp mut ir::Block,
295    emitter: &'temp mut proc::Emitter,
296    typifier: &'temp mut Typifier,
297
298    /// Which `Expression`s in `self.naga_expressions` are const expressions, in
299    /// the WGSL sense.
300    ///
301    /// See [`StatementContext::local_expression_kind_tracker`] for details.
302    local_expression_kind_tracker: &'temp mut proc::ExpressionKindTracker,
303}
304
305/// The type of Naga IR expression we are lowering an [`ast::Expression`] to.
306pub enum ExpressionContextType<'temp, 'out> {
307    /// We are lowering to an arbitrary runtime expression, to be
308    /// included in a function's body.
309    ///
310    /// The given [`LocalExpressionContext`] holds information about local
311    /// variables, arguments, and other definitions available only to runtime
312    /// expressions, not constant or override expressions.
313    Runtime(LocalExpressionContext<'temp, 'out>),
314
315    /// We are lowering to a constant expression, to be included in the module's
316    /// constant expression arena.
317    ///
318    /// Everything global constant expressions are allowed to refer to is
319    /// available in the [`ExpressionContext`], but local constant expressions can
320    /// also refer to other
321    Constant(Option<LocalExpressionContext<'temp, 'out>>),
322
323    /// We are lowering to an override expression, to be included in the module's
324    /// constant expression arena.
325    ///
326    /// Everything override expressions are allowed to refer to is
327    /// available in the [`ExpressionContext`], so this variant
328    /// carries no further information.
329    Override,
330}
331
332/// State for lowering an [`ast::Expression`] to Naga IR.
333///
334/// [`ExpressionContext`]s come in two kinds, distinguished by
335/// the value of the [`expr_type`] field:
336///
337/// - A [`Runtime`] context contributes [`naga::Expression`]s to a [`naga::Function`]'s
338///   runtime expression arena.
339///
340/// - A [`Constant`] context contributes [`naga::Expression`]s to a [`naga::Module`]'s
341///   constant expression arena.
342///
343/// [`ExpressionContext`]s are constructed in restricted ways:
344///
345/// - To get a [`Runtime`] [`ExpressionContext`], call
346///   [`StatementContext::as_expression`].
347///
348/// - To get a [`Constant`] [`ExpressionContext`], call
349///   [`GlobalContext::as_const`].
350///
351/// - You can demote a [`Runtime`] context to a [`Constant`] context
352///   by calling [`as_const`], but there's no way to go in the other
353///   direction, producing a runtime context from a constant one. This
354///   is because runtime expressions can refer to constant
355///   expressions, via [`Expression::Constant`], but constant
356///   expressions can't refer to a function's expressions.
357///
358/// Not to be confused with `wgsl::parse::ExpressionContext`, which is
359/// for parsing the `ast::Expression` in the first place.
360///
361/// [`expr_type`]: ExpressionContext::expr_type
362/// [`Runtime`]: ExpressionContextType::Runtime
363/// [`naga::Expression`]: ir::Expression
364/// [`naga::Function`]: ir::Function
365/// [`Constant`]: ExpressionContextType::Constant
366/// [`naga::Module`]: ir::Module
367/// [`as_const`]: ExpressionContext::as_const
368/// [`Expression::Constant`]: ir::Expression::Constant
369pub struct ExpressionContext<'source, 'temp, 'out> {
370    enable_extensions: EnableExtensions,
371
372    // WGSL AST values.
373    ast_expressions: &'temp Arena<ast::Expression<'source>>,
374
375    // Naga IR values.
376    /// The map from the names of module-scope declarations to the Naga IR
377    /// `Handle`s we have built for them, owned by `Lowerer::lower`.
378    globals: &'temp mut FastHashMap<&'source str, LoweredGlobalDecl>,
379
380    /// The IR [`Module`] we're constructing.
381    ///
382    /// [`Module`]: ir::Module
383    module: &'out mut ir::Module,
384
385    /// Type judgments for [`module::global_expressions`].
386    ///
387    /// [`module::global_expressions`]: ir::Module::global_expressions
388    const_typifier: &'temp mut Typifier,
389    layouter: &'temp mut proc::Layouter,
390    global_expression_kind_tracker: &'temp mut proc::ExpressionKindTracker,
391
392    /// Whether we are lowering a constant expression or a general
393    /// runtime expression, and the data needed in each case.
394    expr_type: ExpressionContextType<'temp, 'out>,
395}
396
397impl TypeContext for ExpressionContext<'_, '_, '_> {
398    fn lookup_type(&self, handle: Handle<ir::Type>) -> &ir::Type {
399        &self.module.types[handle]
400    }
401
402    fn type_name(&self, handle: Handle<ir::Type>) -> &str {
403        self.module.types[handle]
404            .name
405            .as_deref()
406            .unwrap_or("{anonymous type}")
407    }
408
409    fn write_override<W: core::fmt::Write>(
410        &self,
411        handle: Handle<ir::Override>,
412        out: &mut W,
413    ) -> core::fmt::Result {
414        match self.module.overrides[handle].name {
415            Some(ref name) => out.write_str(name),
416            None => write!(out, "{{anonymous override {handle:?}}}"),
417        }
418    }
419
420    fn write_unnamed_struct<W: core::fmt::Write>(
421        &self,
422        _: &ir::TypeInner,
423        _: &mut W,
424    ) -> core::fmt::Result {
425        unreachable!("the WGSL front end should always know the type name");
426    }
427}
428
429impl<'source, 'temp, 'out> ExpressionContext<'source, 'temp, 'out> {
430    const fn is_runtime(&self) -> bool {
431        match self.expr_type {
432            ExpressionContextType::Runtime(_) => true,
433            ExpressionContextType::Constant(_) | ExpressionContextType::Override => false,
434        }
435    }
436
437    #[allow(dead_code)]
438    const fn as_const(&mut self) -> ExpressionContext<'source, '_, '_> {
439        ExpressionContext {
440            enable_extensions: self.enable_extensions,
441            globals: self.globals,
442            ast_expressions: self.ast_expressions,
443            const_typifier: self.const_typifier,
444            layouter: self.layouter,
445            module: self.module,
446            expr_type: ExpressionContextType::Constant(match self.expr_type {
447                ExpressionContextType::Runtime(ref mut local_expression_context)
448                | ExpressionContextType::Constant(Some(ref mut local_expression_context)) => {
449                    Some(LocalExpressionContext {
450                        local_table: local_expression_context.local_table,
451                        function: local_expression_context.function,
452                        block: local_expression_context.block,
453                        emitter: local_expression_context.emitter,
454                        typifier: local_expression_context.typifier,
455                        local_expression_kind_tracker: local_expression_context
456                            .local_expression_kind_tracker,
457                    })
458                }
459                ExpressionContextType::Constant(None) | ExpressionContextType::Override => None,
460            }),
461            global_expression_kind_tracker: self.global_expression_kind_tracker,
462        }
463    }
464
465    const fn as_global(&mut self) -> GlobalContext<'source, '_, '_> {
466        GlobalContext {
467            enable_extensions: self.enable_extensions,
468            ast_expressions: self.ast_expressions,
469            globals: self.globals,
470            module: self.module,
471            const_typifier: self.const_typifier,
472            layouter: self.layouter,
473            global_expression_kind_tracker: self.global_expression_kind_tracker,
474        }
475    }
476
477    const fn as_const_evaluator(&mut self) -> proc::ConstantEvaluator<'_> {
478        match self.expr_type {
479            ExpressionContextType::Runtime(ref mut rctx) => {
480                proc::ConstantEvaluator::for_wgsl_function(
481                    self.module,
482                    &mut rctx.function.expressions,
483                    rctx.local_expression_kind_tracker,
484                    self.layouter,
485                    rctx.emitter,
486                    rctx.block,
487                    false,
488                )
489            }
490            ExpressionContextType::Constant(Some(ref mut rctx)) => {
491                proc::ConstantEvaluator::for_wgsl_function(
492                    self.module,
493                    &mut rctx.function.expressions,
494                    rctx.local_expression_kind_tracker,
495                    self.layouter,
496                    rctx.emitter,
497                    rctx.block,
498                    true,
499                )
500            }
501            ExpressionContextType::Constant(None) => proc::ConstantEvaluator::for_wgsl_module(
502                self.module,
503                self.global_expression_kind_tracker,
504                self.layouter,
505                false,
506            ),
507            ExpressionContextType::Override => proc::ConstantEvaluator::for_wgsl_module(
508                self.module,
509                self.global_expression_kind_tracker,
510                self.layouter,
511                true,
512            ),
513        }
514    }
515
516    /// Return a wrapper around `value` suitable for formatting.
517    ///
518    /// Return a wrapper around `value` that implements
519    /// [`core::fmt::Display`] in a form suitable for use in
520    /// diagnostic messages.
521    const fn as_diagnostic_display<T>(
522        &self,
523        value: T,
524    ) -> crate::common::DiagnosticDisplay<(T, proc::GlobalCtx<'_>)> {
525        let ctx = self.module.to_ctx();
526        crate::common::DiagnosticDisplay((value, ctx))
527    }
528
529    fn append_expression(
530        &mut self,
531        expr: ir::Expression,
532        span: Span,
533    ) -> Result<'source, Handle<ir::Expression>> {
534        let mut eval = self.as_const_evaluator();
535        eval.try_eval_and_append(expr, span)
536            .map_err(|e| Box::new(Error::ConstantEvaluatorError(e.into(), span)))
537    }
538
539    fn get_const_val<T: TryFrom<crate::Literal, Error = proc::ConstValueError>>(
540        &self,
541        handle: Handle<ir::Expression>,
542    ) -> core::result::Result<T, proc::ConstValueError> {
543        match self.expr_type {
544            ExpressionContextType::Runtime(ref ctx) => {
545                if !ctx.local_expression_kind_tracker.is_const(handle) {
546                    return Err(proc::ConstValueError::NonConst);
547                }
548
549                self.module
550                    .to_ctx()
551                    .get_const_val_from(handle, &ctx.function.expressions)
552            }
553            ExpressionContextType::Constant(Some(ref ctx)) => {
554                assert!(ctx.local_expression_kind_tracker.is_const(handle));
555                self.module
556                    .to_ctx()
557                    .get_const_val_from(handle, &ctx.function.expressions)
558            }
559            ExpressionContextType::Constant(None) => self.module.to_ctx().get_const_val(handle),
560            ExpressionContextType::Override => Err(proc::ConstValueError::NonConst),
561        }
562    }
563
564    /// Return `true` if `handle` is a constant expression.
565    fn is_const(&self, handle: Handle<ir::Expression>) -> bool {
566        use ExpressionContextType as Ect;
567        match self.expr_type {
568            Ect::Runtime(ref ctx) | Ect::Constant(Some(ref ctx)) => {
569                ctx.local_expression_kind_tracker.is_const(handle)
570            }
571            Ect::Constant(None) | Ect::Override => {
572                self.global_expression_kind_tracker.is_const(handle)
573            }
574        }
575    }
576
577    fn get_expression_span(&self, handle: Handle<ir::Expression>) -> Span {
578        match self.expr_type {
579            ExpressionContextType::Runtime(ref ctx)
580            | ExpressionContextType::Constant(Some(ref ctx)) => {
581                ctx.function.expressions.get_span(handle)
582            }
583            ExpressionContextType::Constant(None) | ExpressionContextType::Override => {
584                self.module.global_expressions.get_span(handle)
585            }
586        }
587    }
588
589    const fn typifier(&self) -> &Typifier {
590        match self.expr_type {
591            ExpressionContextType::Runtime(ref ctx)
592            | ExpressionContextType::Constant(Some(ref ctx)) => ctx.typifier,
593            ExpressionContextType::Constant(None) | ExpressionContextType::Override => {
594                self.const_typifier
595            }
596        }
597    }
598
599    fn get(&self, handle: Handle<crate::Expression>) -> &crate::Expression {
600        match self.expr_type {
601            ExpressionContextType::Runtime(ref ctx)
602            | ExpressionContextType::Constant(Some(ref ctx)) => &ctx.function.expressions[handle],
603            ExpressionContextType::Constant(None) | ExpressionContextType::Override => {
604                &self.module.global_expressions[handle]
605            }
606        }
607    }
608
609    fn local(
610        &mut self,
611        local: &Handle<ast::Local>,
612        span: Span,
613    ) -> Result<'source, Typed<Handle<ir::Expression>>> {
614        match self.expr_type {
615            ExpressionContextType::Runtime(ref ctx) => Ok(ctx.local_table[local].runtime()),
616            ExpressionContextType::Constant(Some(ref ctx)) => ctx.local_table[local]
617                .const_time()
618                .ok_or(Box::new(Error::UnexpectedOperationInConstContext(span))),
619            _ => Err(Box::new(Error::UnexpectedOperationInConstContext(span))),
620        }
621    }
622
623    fn runtime_expression_ctx(
624        &mut self,
625        span: Span,
626    ) -> Result<'source, &mut LocalExpressionContext<'temp, 'out>> {
627        match self.expr_type {
628            ExpressionContextType::Runtime(ref mut ctx) => Ok(ctx),
629            ExpressionContextType::Constant(_) | ExpressionContextType::Override => {
630                Err(Box::new(Error::UnexpectedOperationInConstContext(span)))
631            }
632        }
633    }
634
635    fn with_nested_runtime_expression_ctx<'a, F, T>(
636        &mut self,
637        span: Span,
638        f: F,
639    ) -> Result<'source, (T, crate::Block)>
640    where
641        for<'t> F: FnOnce(&mut ExpressionContext<'source, 't, 't>) -> Result<'source, T>,
642    {
643        let mut block = crate::Block::new();
644        let rctx = match self.expr_type {
645            ExpressionContextType::Runtime(ref mut rctx) => Ok(rctx),
646            ExpressionContextType::Constant(_) | ExpressionContextType::Override => {
647                Err(Error::UnexpectedOperationInConstContext(span))
648            }
649        }?;
650
651        rctx.block
652            .extend(rctx.emitter.finish(&rctx.function.expressions));
653        rctx.emitter.start(&rctx.function.expressions);
654
655        let nested_rctx = LocalExpressionContext {
656            local_table: rctx.local_table,
657            function: rctx.function,
658            block: &mut block,
659            emitter: rctx.emitter,
660            typifier: rctx.typifier,
661            local_expression_kind_tracker: rctx.local_expression_kind_tracker,
662        };
663        let mut nested_ctx = ExpressionContext {
664            enable_extensions: self.enable_extensions,
665            expr_type: ExpressionContextType::Runtime(nested_rctx),
666            ast_expressions: self.ast_expressions,
667            globals: self.globals,
668            module: self.module,
669            const_typifier: self.const_typifier,
670            layouter: self.layouter,
671            global_expression_kind_tracker: self.global_expression_kind_tracker,
672        };
673        let ret = f(&mut nested_ctx)?;
674
675        block.extend(rctx.emitter.finish(&rctx.function.expressions));
676        rctx.emitter.start(&rctx.function.expressions);
677
678        Ok((ret, block))
679    }
680
681    fn gather_component(
682        &mut self,
683        expr: Handle<ir::Expression>,
684        component_span: Span,
685        gather_span: Span,
686    ) -> Result<'source, ir::SwizzleComponent> {
687        match self.expr_type {
688            ExpressionContextType::Runtime(ref rctx) => {
689                if !rctx.local_expression_kind_tracker.is_const(expr) {
690                    return Err(Box::new(Error::ExpectedConstExprConcreteIntegerScalar(
691                        component_span,
692                    )));
693                }
694
695                let index = self
696                    .module
697                    .to_ctx()
698                    .get_const_val_from::<u32, _>(expr, &rctx.function.expressions)
699                    .map_err(|err| match err {
700                        proc::ConstValueError::NonConst | proc::ConstValueError::InvalidType => {
701                            Error::ExpectedConstExprConcreteIntegerScalar(component_span)
702                        }
703                        proc::ConstValueError::Negative => {
704                            Error::ExpectedNonNegative(component_span)
705                        }
706                    })?;
707                ir::SwizzleComponent::XYZW
708                    .get(index as usize)
709                    .copied()
710                    .ok_or(Box::new(Error::InvalidGatherComponent(component_span)))
711            }
712            // This means a `gather` operation appeared in a constant expression.
713            // This error refers to the `gather` itself, not its "component" argument.
714            ExpressionContextType::Constant(_) | ExpressionContextType::Override => Err(Box::new(
715                Error::UnexpectedOperationInConstContext(gather_span),
716            )),
717        }
718    }
719
720    /// Determine the type of `handle`, and add it to the module's arena.
721    ///
722    /// If you just need a `TypeInner` for `handle`'s type, use the
723    /// [`resolve_inner!`] macro instead. This function
724    /// should only be used when the type of `handle` needs to appear
725    /// in the module's final `Arena<Type>`, for example, if you're
726    /// creating a [`LocalVariable`] whose type is inferred from its
727    /// initializer.
728    ///
729    /// [`LocalVariable`]: ir::LocalVariable
730    fn register_type(
731        &mut self,
732        handle: Handle<ir::Expression>,
733    ) -> Result<'source, Handle<ir::Type>> {
734        self.grow_types(handle)?;
735        // This is equivalent to calling ExpressionContext::typifier(),
736        // except that this lets the borrow checker see that it's okay
737        // to also borrow self.module.types mutably below.
738        let typifier = match self.expr_type {
739            ExpressionContextType::Runtime(ref ctx)
740            | ExpressionContextType::Constant(Some(ref ctx)) => ctx.typifier,
741            ExpressionContextType::Constant(None) | ExpressionContextType::Override => {
742                &*self.const_typifier
743            }
744        };
745        Ok(typifier.register_type(handle, &mut self.module.types))
746    }
747
748    /// Resolve the types of all expressions up through `handle`.
749    ///
750    /// Ensure that [`self.typifier`] has a [`TypeResolution`] for
751    /// every expression in `self.function.expressions`.
752    ///
753    /// This does not add types to any arena. The [`Typifier`]
754    /// documentation explains the steps we take to avoid filling
755    /// arenas with intermediate types.
756    ///
757    /// This function takes `&mut self`, so it can't conveniently
758    /// return a shared reference to the resulting `TypeResolution`:
759    /// the shared reference would extend the mutable borrow, and you
760    /// wouldn't be able to use `self` for anything else. Instead, you
761    /// should use [`register_type`] or one of [`resolve!`],
762    /// [`resolve_inner!`] or [`resolve_inner_binary!`].
763    ///
764    /// [`self.typifier`]: ExpressionContext::typifier
765    /// [`TypeResolution`]: proc::TypeResolution
766    /// [`register_type`]: Self::register_type
767    /// [`Typifier`]: Typifier
768    fn grow_types(&mut self, handle: Handle<ir::Expression>) -> Result<'source, &mut Self> {
769        let empty_arena = Arena::new();
770        let resolve_ctx;
771        let typifier;
772        let expressions;
773        match self.expr_type {
774            ExpressionContextType::Runtime(ref mut ctx)
775            | ExpressionContextType::Constant(Some(ref mut ctx)) => {
776                resolve_ctx = proc::ResolveContext::with_locals(
777                    self.module,
778                    &ctx.function.local_variables,
779                    &ctx.function.arguments,
780                );
781                typifier = &mut *ctx.typifier;
782                expressions = &ctx.function.expressions;
783            }
784            ExpressionContextType::Constant(None) | ExpressionContextType::Override => {
785                resolve_ctx = proc::ResolveContext::with_locals(self.module, &empty_arena, &[]);
786                typifier = self.const_typifier;
787                expressions = &self.module.global_expressions;
788            }
789        };
790        typifier
791            .grow(handle, expressions, &resolve_ctx)
792            .map_err(Error::InvalidResolve)?;
793
794        Ok(self)
795    }
796
797    fn image_data(
798        &mut self,
799        image: Handle<ir::Expression>,
800        span: Span,
801    ) -> Result<'source, (ir::ImageClass, bool)> {
802        match *resolve_inner!(self, image) {
803            ir::TypeInner::Image { class, arrayed, .. } => Ok((class, arrayed)),
804            _ => Err(Box::new(Error::BadTexture(span))),
805        }
806    }
807
808    fn prepare_args<'b>(
809        &mut self,
810        args: &'b [Handle<ast::Expression<'source>>],
811        min_args: u32,
812        span: Span,
813    ) -> ArgumentContext<'b, 'source> {
814        ArgumentContext {
815            args: args.iter(),
816            min_args,
817            args_used: 0,
818            total_args: args.len() as u32,
819            span,
820        }
821    }
822
823    /// Insert splats, if needed by the non-'*' operations.
824    ///
825    /// See the "Binary arithmetic expressions with mixed scalar and vector operands"
826    /// table in the WebGPU Shading Language specification for relevant operators.
827    ///
828    /// Multiply is not handled here as backends are expected to handle vec*scalar
829    /// operations, so inserting splats into the IR increases size needlessly.
830    fn binary_op_splat(
831        &mut self,
832        op: ir::BinaryOperator,
833        left: &mut Handle<ir::Expression>,
834        right: &mut Handle<ir::Expression>,
835    ) -> Result<'source, ()> {
836        if matches!(
837            op,
838            ir::BinaryOperator::Add
839                | ir::BinaryOperator::Subtract
840                | ir::BinaryOperator::Divide
841                | ir::BinaryOperator::Modulo
842        ) {
843            match resolve_inner_binary!(self, *left, *right) {
844                (&ir::TypeInner::Vector { size, .. }, &ir::TypeInner::Scalar { .. }) => {
845                    *right = self.append_expression(
846                        ir::Expression::Splat {
847                            size,
848                            value: *right,
849                        },
850                        self.get_expression_span(*right),
851                    )?;
852                }
853                (&ir::TypeInner::Scalar { .. }, &ir::TypeInner::Vector { size, .. }) => {
854                    *left = self.append_expression(
855                        ir::Expression::Splat { size, value: *left },
856                        self.get_expression_span(*left),
857                    )?;
858                }
859                _ => {}
860            }
861        }
862
863        Ok(())
864    }
865
866    /// Add a single expression to the expression table that is not covered by `self.emitter`.
867    ///
868    /// This is useful for `CallResult` and `AtomicResult` expressions, which should not be covered by
869    /// `Emit` statements.
870    fn interrupt_emitter(
871        &mut self,
872        expression: ir::Expression,
873        span: Span,
874    ) -> Result<'source, Handle<ir::Expression>> {
875        match self.expr_type {
876            ExpressionContextType::Runtime(ref mut rctx)
877            | ExpressionContextType::Constant(Some(ref mut rctx)) => {
878                rctx.block
879                    .extend(rctx.emitter.finish(&rctx.function.expressions));
880            }
881            ExpressionContextType::Constant(None) | ExpressionContextType::Override => {}
882        }
883        let result = self.append_expression(expression, span);
884        match self.expr_type {
885            ExpressionContextType::Runtime(ref mut rctx)
886            | ExpressionContextType::Constant(Some(ref mut rctx)) => {
887                rctx.emitter.start(&rctx.function.expressions);
888            }
889            ExpressionContextType::Constant(None) | ExpressionContextType::Override => {}
890        }
891        result
892    }
893
894    /// Apply the WGSL Load Rule to `expr`.
895    ///
896    /// If `expr` is has type `ref<SC, T, A>`, perform a load to produce a value of type
897    /// `T`. Otherwise, return `expr` unchanged.
898    fn apply_load_rule(
899        &mut self,
900        expr: Typed<Handle<ir::Expression>>,
901    ) -> Result<'source, Handle<ir::Expression>> {
902        match expr {
903            Typed::Reference(pointer) => {
904                let span = self.get_expression_span(pointer);
905
906                // Reject direct access to atomic variables that does not go
907                // through a built-in function.
908                if resolve_inner!(self, pointer).is_atomic_pointer(&self.module.types) {
909                    return Err(Box::new(Error::InvalidAtomicAccess(span)));
910                }
911
912                let load = ir::Expression::Load { pointer };
913                self.append_expression(load, span)
914            }
915            Typed::Plain(handle) => Ok(handle),
916        }
917    }
918
919    fn ensure_type_exists(&mut self, inner: ir::TypeInner) -> Handle<ir::Type> {
920        self.as_global().ensure_type_exists(None, inner)
921    }
922
923    /// Check that `expr` is an identifier resolving to a predeclared enumerant.
924    ///
925    /// The identifier must not have any template parameters.
926    ///
927    /// Return the name of the identifier, together with its span.
928    ///
929    /// Actually, this only checks that the identifier refers to some
930    /// predeclared object, not necessarily an enumerant. This should be good
931    /// enough, since the caller is going to compare the name against some list
932    /// of permitted enumerants anyway.
933    fn enumerant(
934        &self,
935        expr: Handle<ast::Expression<'source>>,
936    ) -> Result<'source, (&'source str, Span)> {
937        let span = self.ast_expressions.get_span(expr);
938        let expr = &self.ast_expressions[expr];
939
940        let ast::Expression::Ident(ref ident) = *expr else {
941            return Err(Box::new(Error::UnexpectedExprForEnumerant(span)));
942        };
943
944        let ast::TemplateElaboratedIdent {
945            ident: ast::IdentExpr::Unresolved(name),
946            ref template_list,
947            ..
948        } = *ident
949        else {
950            return Err(Box::new(Error::UnexpectedIdentForEnumerant(span)));
951        };
952
953        if self.globals.get(name).is_some() {
954            return Err(Box::new(Error::UnexpectedIdentForEnumerant(span)));
955        }
956
957        if !template_list.is_empty() {
958            return Err(Box::new(Error::UnexpectedTemplate(span)));
959        }
960
961        Ok((name, span))
962    }
963
964    fn var_address_space(
965        &self,
966        template_list: &[Handle<ast::Expression<'source>>],
967    ) -> Result<'source, ir::AddressSpace> {
968        let mut tl = TemplateListIter::new(Span::UNDEFINED, template_list);
969        let mut address_space = tl.maybe_address_space(self)?;
970        if let Some(ref mut address_space) = address_space {
971            tl.maybe_access_mode(address_space, self)?;
972        }
973        tl.finish(self)?;
974        Ok(address_space.unwrap_or(ir::AddressSpace::Handle))
975    }
976}
977
978struct ArgumentContext<'ctx, 'source> {
979    args: core::slice::Iter<'ctx, Handle<ast::Expression<'source>>>,
980    min_args: u32,
981    args_used: u32,
982    total_args: u32,
983    span: Span,
984}
985
986impl<'source> ArgumentContext<'_, 'source> {
987    pub fn finish(self) -> Result<'source, ()> {
988        if self.args.len() == 0 {
989            Ok(())
990        } else {
991            Err(Box::new(Error::WrongArgumentCount {
992                found: self.total_args,
993                expected: self.min_args..self.args_used + 1,
994                span: self.span,
995            }))
996        }
997    }
998
999    pub fn next(&mut self) -> Result<'source, Handle<ast::Expression<'source>>> {
1000        match self.args.next().copied() {
1001            Some(arg) => {
1002                self.args_used += 1;
1003                Ok(arg)
1004            }
1005            None => Err(Box::new(Error::WrongArgumentCount {
1006                found: self.total_args,
1007                expected: self.min_args..self.args_used + 1,
1008                span: self.span,
1009            })),
1010        }
1011    }
1012}
1013
1014#[derive(Debug, Copy, Clone)]
1015enum Declared<T> {
1016    /// Value declared as const
1017    Const(T),
1018
1019    /// Value declared as non-const
1020    Runtime(T),
1021}
1022
1023impl<T> Declared<T> {
1024    fn runtime(self) -> T {
1025        match self {
1026            Declared::Const(t) | Declared::Runtime(t) => t,
1027        }
1028    }
1029
1030    fn const_time(self) -> Option<T> {
1031        match self {
1032            Declared::Const(t) => Some(t),
1033            Declared::Runtime(_) => None,
1034        }
1035    }
1036}
1037
1038/// WGSL type annotations on expressions, types, values, etc.
1039///
1040/// Naga and WGSL types are very close, but Naga lacks WGSL's `ref` types, which
1041/// we need to know to apply the Load Rule. This enum carries some WGSL or Naga
1042/// datum along with enough information to determine its corresponding WGSL
1043/// type.
1044///
1045/// The `T` type parameter can be any expression-like thing:
1046///
1047/// - `Typed<Handle<ir::Type>>` can represent a full WGSL type. For example,
1048///   given some Naga `Pointer` type `ptr`, a WGSL reference type is a
1049///   `Typed::Reference(ptr)` whereas a WGSL pointer type is a
1050///   `Typed::Plain(ptr)`.
1051///
1052/// - `Typed<ir::Expression>` or `Typed<Handle<ir::Expression>>` can
1053///   represent references similarly.
1054///
1055/// Use the `map` and `try_map` methods to convert from one expression
1056/// representation to another.
1057///
1058/// [`Expression`]: ir::Expression
1059#[derive(Debug, Copy, Clone)]
1060enum Typed<T> {
1061    /// A WGSL reference.
1062    Reference(T),
1063
1064    /// A WGSL plain type.
1065    Plain(T),
1066}
1067
1068impl<T> Typed<T> {
1069    fn map<U>(self, mut f: impl FnMut(T) -> U) -> Typed<U> {
1070        match self {
1071            Self::Reference(v) => Typed::Reference(f(v)),
1072            Self::Plain(v) => Typed::Plain(f(v)),
1073        }
1074    }
1075
1076    fn try_map<U, E>(
1077        self,
1078        mut f: impl FnMut(T) -> core::result::Result<U, E>,
1079    ) -> core::result::Result<Typed<U>, E> {
1080        Ok(match self {
1081            Self::Reference(expr) => Typed::Reference(f(expr)?),
1082            Self::Plain(expr) => Typed::Plain(f(expr)?),
1083        })
1084    }
1085
1086    fn ref_or<E>(self, error: E) -> core::result::Result<T, E> {
1087        match self {
1088            Self::Reference(v) => Ok(v),
1089            Self::Plain(_) => Err(error),
1090        }
1091    }
1092}
1093
1094/// A single vector component or swizzle.
1095///
1096/// This represents the things that can appear after the `.` in a vector access
1097/// expression: either a single component name, or a series of them,
1098/// representing a swizzle.
1099enum Components {
1100    Single(u32),
1101    Swizzle {
1102        size: ir::VectorSize,
1103        pattern: [ir::SwizzleComponent; 4],
1104    },
1105}
1106
1107impl Components {
1108    const fn letter_component(letter: char) -> Option<ir::SwizzleComponent> {
1109        use ir::SwizzleComponent as Sc;
1110        match letter {
1111            'x' | 'r' => Some(Sc::X),
1112            'y' | 'g' => Some(Sc::Y),
1113            'z' | 'b' => Some(Sc::Z),
1114            'w' | 'a' => Some(Sc::W),
1115            _ => None,
1116        }
1117    }
1118
1119    fn single_component(name: &str, name_span: Span) -> Result<'_, u32> {
1120        let ch = name.chars().next().ok_or(Error::BadAccessor(name_span))?;
1121        match Self::letter_component(ch) {
1122            Some(sc) => Ok(sc as u32),
1123            None => Err(Box::new(Error::BadAccessor(name_span))),
1124        }
1125    }
1126
1127    /// Construct a `Components` value from a 'member' name, like `"wzy"` or `"x"`.
1128    ///
1129    /// Use `name_span` for reporting errors in parsing the component string.
1130    fn new(name: &str, name_span: Span) -> Result<'_, Self> {
1131        let size = match name.len() {
1132            1 => return Ok(Components::Single(Self::single_component(name, name_span)?)),
1133            2 => ir::VectorSize::Bi,
1134            3 => ir::VectorSize::Tri,
1135            4 => ir::VectorSize::Quad,
1136            _ => return Err(Box::new(Error::BadAccessor(name_span))),
1137        };
1138
1139        let mut pattern = [ir::SwizzleComponent::X; 4];
1140        for (comp, ch) in pattern.iter_mut().zip(name.chars()) {
1141            *comp = Self::letter_component(ch).ok_or(Error::BadAccessor(name_span))?;
1142        }
1143
1144        if name.chars().all(|c| matches!(c, 'x' | 'y' | 'z' | 'w'))
1145            || name.chars().all(|c| matches!(c, 'r' | 'g' | 'b' | 'a'))
1146        {
1147            Ok(Components::Swizzle { size, pattern })
1148        } else {
1149            Err(Box::new(Error::BadAccessor(name_span)))
1150        }
1151    }
1152}
1153
1154/// An `ast::GlobalDecl` for which we have built the Naga IR equivalent.
1155enum LoweredGlobalDecl {
1156    Function {
1157        handle: Handle<ir::Function>,
1158        must_use: bool,
1159    },
1160    Var(Handle<ir::GlobalVariable>),
1161    Const(Handle<ir::Constant>),
1162    Override(Handle<ir::Override>),
1163    Type(Handle<ir::Type>),
1164    EntryPoint(usize),
1165}
1166
1167enum Texture {
1168    Gather,
1169    GatherCompare,
1170
1171    Sample,
1172    SampleBias,
1173    SampleCompare,
1174    SampleCompareLevel,
1175    SampleGrad,
1176    SampleLevel,
1177    SampleBaseClampToEdge,
1178}
1179
1180impl Texture {
1181    pub fn map(word: &str) -> Option<Self> {
1182        Some(match word {
1183            "textureGather" => Self::Gather,
1184            "textureGatherCompare" => Self::GatherCompare,
1185
1186            "textureSample" => Self::Sample,
1187            "textureSampleBias" => Self::SampleBias,
1188            "textureSampleCompare" => Self::SampleCompare,
1189            "textureSampleCompareLevel" => Self::SampleCompareLevel,
1190            "textureSampleGrad" => Self::SampleGrad,
1191            "textureSampleLevel" => Self::SampleLevel,
1192            "textureSampleBaseClampToEdge" => Self::SampleBaseClampToEdge,
1193            _ => return None,
1194        })
1195    }
1196
1197    pub const fn min_argument_count(&self) -> u32 {
1198        match *self {
1199            Self::Gather => 3,
1200            Self::GatherCompare => 4,
1201
1202            Self::Sample => 3,
1203            Self::SampleBias => 5,
1204            Self::SampleCompare => 5,
1205            Self::SampleCompareLevel => 5,
1206            Self::SampleGrad => 6,
1207            Self::SampleLevel => 5,
1208            Self::SampleBaseClampToEdge => 3,
1209        }
1210    }
1211}
1212
1213enum SubgroupGather {
1214    BroadcastFirst,
1215    Broadcast,
1216    Shuffle,
1217    ShuffleDown,
1218    ShuffleUp,
1219    ShuffleXor,
1220    QuadBroadcast,
1221}
1222
1223impl SubgroupGather {
1224    pub fn map(word: &str) -> Option<Self> {
1225        Some(match word {
1226            "subgroupBroadcastFirst" => Self::BroadcastFirst,
1227            "subgroupBroadcast" => Self::Broadcast,
1228            "subgroupShuffle" => Self::Shuffle,
1229            "subgroupShuffleDown" => Self::ShuffleDown,
1230            "subgroupShuffleUp" => Self::ShuffleUp,
1231            "subgroupShuffleXor" => Self::ShuffleXor,
1232            "quadBroadcast" => Self::QuadBroadcast,
1233            _ => return None,
1234        })
1235    }
1236}
1237
1238/// Whether a declaration accepts abstract types, or concretizes.
1239enum AbstractRule {
1240    /// This declaration concretizes its initialization expression.
1241    Concretize,
1242
1243    /// This declaration can accept initializers with abstract types.
1244    Allow,
1245}
1246
1247/// Whether `@must_use` applies to a call expression.
1248#[derive(Debug, Copy, Clone)]
1249enum MustUse {
1250    Yes,
1251    No,
1252}
1253
1254impl From<bool> for MustUse {
1255    fn from(value: bool) -> Self {
1256        if value {
1257            MustUse::Yes
1258        } else {
1259            MustUse::No
1260        }
1261    }
1262}
1263
1264pub struct Lowerer<'source, 'temp> {
1265    index: &'temp Index<'source>,
1266}
1267
1268impl<'source, 'temp> Lowerer<'source, 'temp> {
1269    pub const fn new(index: &'temp Index<'source>) -> Self {
1270        Self { index }
1271    }
1272
1273    pub fn lower(&mut self, tu: ast::TranslationUnit<'source>) -> Result<'source, ir::Module> {
1274        let mut module = ir::Module {
1275            diagnostic_filters: tu.diagnostic_filters,
1276            diagnostic_filter_leaf: tu.diagnostic_filter_leaf,
1277            ..Default::default()
1278        };
1279
1280        let mut ctx = GlobalContext {
1281            enable_extensions: tu.enable_extensions,
1282            ast_expressions: &tu.expressions,
1283            globals: &mut FastHashMap::default(),
1284            module: &mut module,
1285            const_typifier: &mut Typifier::new(),
1286            layouter: &mut proc::Layouter::default(),
1287            global_expression_kind_tracker: &mut proc::ExpressionKindTracker::new(),
1288        };
1289        if !tu.doc_comments.is_empty() {
1290            ctx.module.get_or_insert_default_doc_comments().module =
1291                tu.doc_comments.iter().map(|s| s.to_string()).collect();
1292        }
1293
1294        for decl_handle in self.index.visit_ordered() {
1295            let span = tu.decls.get_span(decl_handle);
1296            let decl = &tu.decls[decl_handle];
1297
1298            match decl.kind {
1299                ast::GlobalDeclKind::Fn(ref f) => {
1300                    let lowered_decl = self.function(f, span, &mut ctx)?;
1301                    if !f.doc_comments.is_empty() {
1302                        match lowered_decl {
1303                            LoweredGlobalDecl::Function { handle, .. } => {
1304                                ctx.module
1305                                    .get_or_insert_default_doc_comments()
1306                                    .functions
1307                                    .insert(
1308                                        handle,
1309                                        f.doc_comments.iter().map(|s| s.to_string()).collect(),
1310                                    );
1311                            }
1312                            LoweredGlobalDecl::EntryPoint(index) => {
1313                                ctx.module
1314                                    .get_or_insert_default_doc_comments()
1315                                    .entry_points
1316                                    .insert(
1317                                        index,
1318                                        f.doc_comments.iter().map(|s| s.to_string()).collect(),
1319                                    );
1320                            }
1321                            _ => {}
1322                        }
1323                    }
1324                    ctx.globals.insert(f.name.name, lowered_decl);
1325                }
1326                ast::GlobalDeclKind::Var(ref v) => {
1327                    let explicit_ty =
1328                        v.ty.as_ref()
1329                            .map(|ast| self.resolve_ast_type(ast, &mut ctx.as_const()))
1330                            .transpose()?;
1331
1332                    let (ty, initializer) = self.type_and_init(
1333                        v.name,
1334                        v.init,
1335                        explicit_ty,
1336                        AbstractRule::Concretize,
1337                        &mut ctx.as_override(),
1338                    )?;
1339
1340                    let binding = if let Some(ref binding) = v.binding {
1341                        Some(ir::ResourceBinding {
1342                            group: self.const_u32(binding.group, &mut ctx.as_const())?.0,
1343                            binding: self.const_u32(binding.binding, &mut ctx.as_const())?.0,
1344                        })
1345                    } else {
1346                        None
1347                    };
1348
1349                    let space = ctx.as_const().var_address_space(&v.template_list)?;
1350
1351                    let handle = ctx.module.global_variables.append(
1352                        ir::GlobalVariable {
1353                            name: Some(v.name.name.to_string()),
1354                            space,
1355                            binding,
1356                            ty,
1357                            init: initializer,
1358                            memory_decorations: v.memory_decorations,
1359                        },
1360                        span,
1361                    );
1362
1363                    if !v.doc_comments.is_empty() {
1364                        ctx.module
1365                            .get_or_insert_default_doc_comments()
1366                            .global_variables
1367                            .insert(
1368                                handle,
1369                                v.doc_comments.iter().map(|s| s.to_string()).collect(),
1370                            );
1371                    }
1372                    ctx.globals
1373                        .insert(v.name.name, LoweredGlobalDecl::Var(handle));
1374                }
1375                ast::GlobalDeclKind::Const(ref c) => {
1376                    let mut ectx = ctx.as_const();
1377
1378                    let explicit_ty =
1379                        c.ty.as_ref()
1380                            .map(|ast| self.resolve_ast_type(ast, &mut ectx))
1381                            .transpose()?;
1382
1383                    let (ty, init) = self.type_and_init(
1384                        c.name,
1385                        Some(c.init),
1386                        explicit_ty,
1387                        AbstractRule::Allow,
1388                        &mut ectx,
1389                    )?;
1390                    let init = init.expect("Global const must have init");
1391
1392                    let handle = ctx.module.constants.append(
1393                        ir::Constant {
1394                            name: Some(c.name.name.to_string()),
1395                            ty,
1396                            init,
1397                        },
1398                        span,
1399                    );
1400
1401                    ctx.globals
1402                        .insert(c.name.name, LoweredGlobalDecl::Const(handle));
1403                    if !c.doc_comments.is_empty() {
1404                        ctx.module
1405                            .get_or_insert_default_doc_comments()
1406                            .constants
1407                            .insert(
1408                                handle,
1409                                c.doc_comments.iter().map(|s| s.to_string()).collect(),
1410                            );
1411                    }
1412                }
1413                ast::GlobalDeclKind::Override(ref o) => {
1414                    let explicit_ty =
1415                        o.ty.as_ref()
1416                            .map(|ast| self.resolve_ast_type(ast, &mut ctx.as_const()))
1417                            .transpose()?;
1418
1419                    let mut ectx = ctx.as_override();
1420
1421                    let (ty, init) = self.type_and_init(
1422                        o.name,
1423                        o.init,
1424                        explicit_ty,
1425                        AbstractRule::Concretize,
1426                        &mut ectx,
1427                    )?;
1428
1429                    let id =
1430                        o.id.map(|id| self.const_u32(id, &mut ctx.as_const()))
1431                            .transpose()?;
1432
1433                    let id = if let Some((id, id_span)) = id {
1434                        Some(
1435                            u16::try_from(id)
1436                                .map_err(|_| Error::PipelineConstantIDValue(id_span))?,
1437                        )
1438                    } else {
1439                        None
1440                    };
1441
1442                    let handle = ctx.module.overrides.append(
1443                        ir::Override {
1444                            name: Some(o.name.name.to_string()),
1445                            id,
1446                            ty,
1447                            init,
1448                        },
1449                        span,
1450                    );
1451
1452                    ctx.globals
1453                        .insert(o.name.name, LoweredGlobalDecl::Override(handle));
1454                }
1455                ast::GlobalDeclKind::Struct(ref s) => {
1456                    let handle = self.r#struct(s, span, &mut ctx)?;
1457                    ctx.globals
1458                        .insert(s.name.name, LoweredGlobalDecl::Type(handle));
1459                    if !s.doc_comments.is_empty() {
1460                        ctx.module
1461                            .get_or_insert_default_doc_comments()
1462                            .types
1463                            .insert(
1464                                handle,
1465                                s.doc_comments.iter().map(|s| s.to_string()).collect(),
1466                            );
1467                    }
1468                }
1469                ast::GlobalDeclKind::Type(ref alias) => {
1470                    let ty = self.resolve_named_ast_type(
1471                        &alias.ty,
1472                        alias.name.name.to_string(),
1473                        &mut ctx.as_const(),
1474                    )?;
1475                    ctx.globals
1476                        .insert(alias.name.name, LoweredGlobalDecl::Type(ty));
1477                }
1478                ast::GlobalDeclKind::ConstAssert(condition) => {
1479                    let condition = self.expression(condition, &mut ctx.as_const())?;
1480
1481                    let span = ctx.module.global_expressions.get_span(condition);
1482                    match ctx
1483                        .module
1484                        .to_ctx()
1485                        .get_const_val_from(condition, &ctx.module.global_expressions)
1486                    {
1487                        Ok(true) => Ok(()),
1488                        Ok(false) => Err(Error::ConstAssertFailed(span)),
1489                        Err(proc::ConstValueError::NonConst | proc::ConstValueError::Negative) => {
1490                            unreachable!()
1491                        }
1492                        Err(proc::ConstValueError::InvalidType) => Err(Error::NotBool(span)),
1493                    }?;
1494                }
1495            }
1496        }
1497
1498        // Constant evaluation may leave abstract-typed literals and
1499        // compositions in expression arenas, so we need to compact the module
1500        // to remove unused expressions and types.
1501        crate::compact::compact(&mut module, KeepUnused::Yes);
1502
1503        Ok(module)
1504    }
1505
1506    /// Obtain (inferred) type and initializer after automatic conversion
1507    fn type_and_init(
1508        &mut self,
1509        name: ast::Ident<'source>,
1510        init: Option<Handle<ast::Expression<'source>>>,
1511        explicit_ty: Option<Handle<ir::Type>>,
1512        abstract_rule: AbstractRule,
1513        ectx: &mut ExpressionContext<'source, '_, '_>,
1514    ) -> Result<'source, (Handle<ir::Type>, Option<Handle<ir::Expression>>)> {
1515        let ty;
1516        let initializer;
1517        match (init, explicit_ty) {
1518            (Some(init), Some(explicit_ty)) => {
1519                let init = self.expression_for_abstract(init, ectx)?;
1520                let ty_res = proc::TypeResolution::Handle(explicit_ty);
1521                let init = ectx
1522                    .try_automatic_conversions(init, &ty_res, name.span)
1523                    .map_err(|error| match *error {
1524                        Error::AutoConversion(e) => Box::new(Error::InitializationTypeMismatch {
1525                            name: name.span,
1526                            expected: e.dest_type,
1527                            got: e.source_type,
1528                        }),
1529                        _ => error,
1530                    })?;
1531
1532                let init_ty = ectx.register_type(init)?;
1533                if !ectx.module.compare_types(
1534                    &proc::TypeResolution::Handle(explicit_ty),
1535                    &proc::TypeResolution::Handle(init_ty),
1536                ) {
1537                    return Err(Box::new(Error::InitializationTypeMismatch {
1538                        name: name.span,
1539                        expected: ectx.type_to_string(explicit_ty),
1540                        got: ectx.type_to_string(init_ty),
1541                    }));
1542                }
1543                ty = explicit_ty;
1544                initializer = Some(init);
1545            }
1546            (Some(init), None) => {
1547                let mut init = self.expression_for_abstract(init, ectx)?;
1548                if let AbstractRule::Concretize = abstract_rule {
1549                    init = ectx.concretize(init)?;
1550                }
1551                ty = ectx.register_type(init)?;
1552                initializer = Some(init);
1553            }
1554            (None, Some(explicit_ty)) => {
1555                ty = explicit_ty;
1556                initializer = None;
1557            }
1558            (None, None) => return Err(Box::new(Error::DeclMissingTypeAndInit(name.span))),
1559        }
1560        Ok((ty, initializer))
1561    }
1562
1563    fn function(
1564        &mut self,
1565        f: &ast::Function<'source>,
1566        span: Span,
1567        ctx: &mut GlobalContext<'source, '_, '_>,
1568    ) -> Result<'source, LoweredGlobalDecl> {
1569        let mut local_table = FastHashMap::default();
1570        let mut expressions = Arena::new();
1571        let mut named_expressions = FastIndexMap::default();
1572        let mut local_expression_kind_tracker = proc::ExpressionKindTracker::new();
1573
1574        let arguments = f
1575            .arguments
1576            .iter()
1577            .enumerate()
1578            .map(|(i, arg)| -> Result<'_, _> {
1579                let ty = self.resolve_ast_type(&arg.ty, &mut ctx.as_const())?;
1580                let expr =
1581                    expressions.append(ir::Expression::FunctionArgument(i as u32), arg.name.span);
1582                local_table.insert(arg.handle, Declared::Runtime(Typed::Plain(expr)));
1583                named_expressions.insert(expr, (arg.name.name.to_string(), arg.name.span));
1584                local_expression_kind_tracker.insert(expr, proc::ExpressionKind::Runtime);
1585
1586                Ok(ir::FunctionArgument {
1587                    name: Some(arg.name.name.to_string()),
1588                    ty,
1589                    binding: self.binding(&arg.binding, ty, ctx)?,
1590                })
1591            })
1592            .collect::<Result<Vec<_>>>()?;
1593
1594        let result = f
1595            .result
1596            .as_ref()
1597            .map(|res| -> Result<'_, _> {
1598                let ty = self.resolve_ast_type(&res.ty, &mut ctx.as_const())?;
1599                Ok(ir::FunctionResult {
1600                    ty,
1601                    binding: self.binding(&res.binding, ty, ctx)?,
1602                })
1603            })
1604            .transpose()?;
1605
1606        let mut function = ir::Function {
1607            name: Some(f.name.name.to_string()),
1608            arguments,
1609            result,
1610            local_variables: Arena::new(),
1611            expressions,
1612            named_expressions: crate::NamedExpressions::default(),
1613            body: ir::Block::default(),
1614            diagnostic_filter_leaf: f.diagnostic_filter_leaf,
1615        };
1616
1617        let mut typifier = Typifier::default();
1618        let mut stmt_ctx = StatementContext {
1619            enable_extensions: ctx.enable_extensions,
1620            local_table: &mut local_table,
1621            globals: ctx.globals,
1622            ast_expressions: ctx.ast_expressions,
1623            const_typifier: ctx.const_typifier,
1624            typifier: &mut typifier,
1625            layouter: ctx.layouter,
1626            function: &mut function,
1627            named_expressions: &mut named_expressions,
1628            module: ctx.module,
1629            local_expression_kind_tracker: &mut local_expression_kind_tracker,
1630            global_expression_kind_tracker: ctx.global_expression_kind_tracker,
1631        };
1632        let mut body = self.block(&f.body, false, &mut stmt_ctx)?;
1633        proc::ensure_block_returns(&mut body);
1634
1635        function.body = body;
1636        function.named_expressions = named_expressions
1637            .into_iter()
1638            .map(|(key, (name, _))| (key, name))
1639            .collect();
1640
1641        if let Some(ref entry) = f.entry_point {
1642            let (workgroup_size, workgroup_size_overrides) =
1643                if let Some(workgroup_size) = entry.workgroup_size {
1644                    // TODO: replace with try_map once stabilized
1645                    let mut workgroup_size_out = [1; 3];
1646                    let mut workgroup_size_overrides_out = [None; 3];
1647                    for (i, size) in workgroup_size.into_iter().enumerate() {
1648                        if let Some(size_expr) = size {
1649                            match self.const_u32(size_expr, &mut ctx.as_const()) {
1650                                Ok(value) => {
1651                                    workgroup_size_out[i] = value.0;
1652                                }
1653                                Err(err) => {
1654                                    if let Error::ConstantEvaluatorError(ref ty, _) = *err {
1655                                        match **ty {
1656                                            proc::ConstantEvaluatorError::OverrideExpr => {
1657                                                workgroup_size_overrides_out[i] =
1658                                                    Some(self.workgroup_size_override(
1659                                                        size_expr,
1660                                                        &mut ctx.as_override(),
1661                                                    )?);
1662                                            }
1663                                            _ => {
1664                                                return Err(err);
1665                                            }
1666                                        }
1667                                    } else {
1668                                        return Err(err);
1669                                    }
1670                                }
1671                            }
1672                        }
1673                    }
1674                    if workgroup_size_overrides_out.iter().all(|x| x.is_none()) {
1675                        (workgroup_size_out, None)
1676                    } else {
1677                        (workgroup_size_out, Some(workgroup_size_overrides_out))
1678                    }
1679                } else {
1680                    ([0; 3], None)
1681                };
1682
1683            let mesh_info = if let Some((var_name, var_span)) = entry.mesh_output_variable {
1684                let var = match ctx.globals.get(var_name) {
1685                    Some(&LoweredGlobalDecl::Var(handle)) => handle,
1686                    Some(_) => {
1687                        return Err(Box::new(Error::ExpectedGlobalVariable {
1688                            name_span: var_span,
1689                        }))
1690                    }
1691                    None => return Err(Box::new(Error::UnknownIdent(var_span, var_name))),
1692                };
1693
1694                let mut info = ctx.module.analyze_mesh_shader_info(var);
1695                if let Some(h) = info.1[0] {
1696                    info.0.max_vertices_override = Some(
1697                        ctx.module
1698                            .global_expressions
1699                            .append(crate::Expression::Override(h), Span::UNDEFINED),
1700                    );
1701                }
1702                if let Some(h) = info.1[1] {
1703                    info.0.max_primitives_override = Some(
1704                        ctx.module
1705                            .global_expressions
1706                            .append(crate::Expression::Override(h), Span::UNDEFINED),
1707                    );
1708                }
1709
1710                Some(info.0)
1711            } else {
1712                None
1713            };
1714
1715            let task_payload = if let Some((var_name, var_span)) = entry.task_payload {
1716                Some(match ctx.globals.get(var_name) {
1717                    Some(&LoweredGlobalDecl::Var(handle)) => handle,
1718                    Some(_) => {
1719                        return Err(Box::new(Error::ExpectedGlobalVariable {
1720                            name_span: var_span,
1721                        }))
1722                    }
1723                    None => return Err(Box::new(Error::UnknownIdent(var_span, var_name))),
1724                })
1725            } else {
1726                None
1727            };
1728
1729            let incoming_ray_payload =
1730                if let Some((var_name, var_span)) = entry.ray_incoming_payload {
1731                    Some(match ctx.globals.get(var_name) {
1732                        Some(&LoweredGlobalDecl::Var(handle)) => handle,
1733                        Some(_) => {
1734                            return Err(Box::new(Error::ExpectedGlobalVariable {
1735                                name_span: var_span,
1736                            }))
1737                        }
1738                        None => return Err(Box::new(Error::UnknownIdent(var_span, var_name))),
1739                    })
1740                } else {
1741                    None
1742                };
1743
1744            ctx.module.entry_points.push(ir::EntryPoint {
1745                name: f.name.name.to_string(),
1746                stage: entry.stage,
1747                early_depth_test: entry.early_depth_test,
1748                workgroup_size,
1749                workgroup_size_overrides,
1750                function,
1751                mesh_info,
1752                task_payload,
1753                incoming_ray_payload,
1754            });
1755            Ok(LoweredGlobalDecl::EntryPoint(
1756                ctx.module.entry_points.len() - 1,
1757            ))
1758        } else {
1759            let handle = ctx.module.functions.append(function, span);
1760            Ok(LoweredGlobalDecl::Function {
1761                handle,
1762                must_use: f.result.as_ref().is_some_and(|res| res.must_use),
1763            })
1764        }
1765    }
1766
1767    fn workgroup_size_override(
1768        &mut self,
1769        size_expr: Handle<ast::Expression<'source>>,
1770        ctx: &mut ExpressionContext<'source, '_, '_>,
1771    ) -> Result<'source, Handle<ir::Expression>> {
1772        let span = ctx.ast_expressions.get_span(size_expr);
1773        let expr = self.expression(size_expr, ctx)?;
1774        match resolve_inner!(ctx, expr).scalar_kind().ok_or(0) {
1775            Ok(ir::ScalarKind::Sint) | Ok(ir::ScalarKind::Uint) => Ok(expr),
1776            _ => Err(Box::new(Error::ExpectedConstExprConcreteIntegerScalar(
1777                span,
1778            ))),
1779        }
1780    }
1781
1782    fn block(
1783        &mut self,
1784        b: &ast::Block<'source>,
1785        is_inside_loop: bool,
1786        ctx: &mut StatementContext<'source, '_, '_>,
1787    ) -> Result<'source, ir::Block> {
1788        let mut block = ir::Block::default();
1789
1790        for stmt in b.stmts.iter() {
1791            self.statement(stmt, &mut block, is_inside_loop, ctx)?;
1792        }
1793
1794        Ok(block)
1795    }
1796
1797    fn statement(
1798        &mut self,
1799        stmt: &ast::Statement<'source>,
1800        block: &mut ir::Block,
1801        is_inside_loop: bool,
1802        ctx: &mut StatementContext<'source, '_, '_>,
1803    ) -> Result<'source, ()> {
1804        let out = match stmt.kind {
1805            ast::StatementKind::Block(ref block) => {
1806                let block = self.block(block, is_inside_loop, ctx)?;
1807                ir::Statement::Block(block)
1808            }
1809            ast::StatementKind::LocalDecl(ref decl) => match *decl {
1810                ast::LocalDecl::Let(ref l) => {
1811                    let mut emitter = proc::Emitter::default();
1812                    emitter.start(&ctx.function.expressions);
1813
1814                    let explicit_ty = l
1815                        .ty
1816                        .as_ref()
1817                        .map(|ty| self.resolve_ast_type(ty, &mut ctx.as_const(block, &mut emitter)))
1818                        .transpose()?;
1819
1820                    let mut ectx = ctx.as_expression(block, &mut emitter);
1821
1822                    let (ty, initializer) = self.type_and_init(
1823                        l.name,
1824                        Some(l.init),
1825                        explicit_ty,
1826                        AbstractRule::Concretize,
1827                        &mut ectx,
1828                    )?;
1829
1830                    // We have this special check here for `let` declarations because the
1831                    // validator doesn't check them (they are comingled with other things in
1832                    // `named_expressions`; see <https://github.com/gfx-rs/wgpu/issues/7393>).
1833                    // The check could go in `type_and_init`, but then we'd have to
1834                    // distinguish whether override-sized is allowed. The error ought to use
1835                    // the type's span, but `module.types.get_span(ty)` is `Span::UNDEFINED`
1836                    // (see <https://github.com/gfx-rs/wgpu/issues/7951>).
1837                    if ctx.module.types[ty]
1838                        .inner
1839                        .is_dynamically_sized(&ctx.module.types)
1840                    {
1841                        return Err(Box::new(Error::TypeNotConstructible(l.name.span)));
1842                    }
1843
1844                    // We passed `Some()` to `type_and_init`, so we
1845                    // will get a lowered initializer expression back.
1846                    let initializer =
1847                        initializer.expect("type_and_init did not return an initializer");
1848
1849                    // The WGSL spec says that any expression that refers to a
1850                    // `let`-bound variable is not a const expression. This
1851                    // affects when errors must be reported, so we can't even
1852                    // treat suitable `let` bindings as constant as an
1853                    // optimization.
1854                    ctx.local_expression_kind_tracker
1855                        .force_non_const(initializer);
1856
1857                    block.extend(emitter.finish(&ctx.function.expressions));
1858                    ctx.local_table
1859                        .insert(l.handle, Declared::Runtime(Typed::Plain(initializer)));
1860                    ctx.named_expressions
1861                        .insert(initializer, (l.name.name.to_string(), l.name.span));
1862
1863                    return Ok(());
1864                }
1865                ast::LocalDecl::Var(ref v) => {
1866                    let mut emitter = proc::Emitter::default();
1867                    emitter.start(&ctx.function.expressions);
1868
1869                    let explicit_ty =
1870                        v.ty.as_ref()
1871                            .map(|ast| {
1872                                self.resolve_ast_type(ast, &mut ctx.as_const(block, &mut emitter))
1873                            })
1874                            .transpose()?;
1875
1876                    let mut ectx = ctx.as_expression(block, &mut emitter);
1877                    let (ty, initializer) = self.type_and_init(
1878                        v.name,
1879                        v.init,
1880                        explicit_ty,
1881                        AbstractRule::Concretize,
1882                        &mut ectx,
1883                    )?;
1884
1885                    let (const_initializer, initializer) = {
1886                        match initializer {
1887                            Some(init) => {
1888                                // It's not correct to hoist the initializer up
1889                                // to the top of the function if:
1890                                // - the initialization is inside a loop, and should
1891                                //   take place on every iteration, or
1892                                // - the initialization is not a constant
1893                                //   expression, so its value depends on the
1894                                //   state at the point of initialization.
1895                                if is_inside_loop
1896                                    || !ctx.local_expression_kind_tracker.is_const_or_override(init)
1897                                {
1898                                    (None, Some(init))
1899                                } else {
1900                                    (Some(init), None)
1901                                }
1902                            }
1903                            None => (None, None),
1904                        }
1905                    };
1906
1907                    let var = ctx.function.local_variables.append(
1908                        ir::LocalVariable {
1909                            name: Some(v.name.name.to_string()),
1910                            ty,
1911                            init: const_initializer,
1912                        },
1913                        stmt.span,
1914                    );
1915
1916                    let handle = ctx
1917                        .as_expression(block, &mut emitter)
1918                        .interrupt_emitter(ir::Expression::LocalVariable(var), Span::UNDEFINED)?;
1919                    let initializer = if is_inside_loop {
1920                        match initializer {
1921                            Some(initializer) => Some(initializer),
1922                            None => Some(
1923                                ctx.as_expression(block, &mut emitter)
1924                                    .append_expression(ir::Expression::ZeroValue(ty), stmt.span)?,
1925                            ),
1926                        }
1927                    } else {
1928                        initializer
1929                    };
1930                    block.extend(emitter.finish(&ctx.function.expressions));
1931                    ctx.local_table
1932                        .insert(v.handle, Declared::Runtime(Typed::Reference(handle)));
1933
1934                    match initializer {
1935                        Some(initializer) => ir::Statement::Store {
1936                            pointer: handle,
1937                            value: initializer,
1938                        },
1939                        None => return Ok(()),
1940                    }
1941                }
1942                ast::LocalDecl::Const(ref c) => {
1943                    let mut emitter = proc::Emitter::default();
1944                    emitter.start(&ctx.function.expressions);
1945
1946                    let ectx = &mut ctx.as_const(block, &mut emitter);
1947
1948                    let explicit_ty =
1949                        c.ty.as_ref()
1950                            .map(|ast| self.resolve_ast_type(ast, &mut ectx.as_const()))
1951                            .transpose()?;
1952
1953                    let (_ty, init) = self.type_and_init(
1954                        c.name,
1955                        Some(c.init),
1956                        explicit_ty,
1957                        AbstractRule::Allow,
1958                        &mut ectx.as_const(),
1959                    )?;
1960                    let init = init.expect("Local const must have init");
1961
1962                    block.extend(emitter.finish(&ctx.function.expressions));
1963                    ctx.local_table
1964                        .insert(c.handle, Declared::Const(Typed::Plain(init)));
1965                    return Ok(());
1966                }
1967            },
1968            ast::StatementKind::If {
1969                condition,
1970                ref accept,
1971                ref reject,
1972            } => {
1973                let mut emitter = proc::Emitter::default();
1974                emitter.start(&ctx.function.expressions);
1975
1976                let condition =
1977                    self.expression(condition, &mut ctx.as_expression(block, &mut emitter))?;
1978                block.extend(emitter.finish(&ctx.function.expressions));
1979
1980                let accept = self.block(accept, is_inside_loop, ctx)?;
1981                let reject = self.block(reject, is_inside_loop, ctx)?;
1982
1983                ir::Statement::If {
1984                    condition,
1985                    accept,
1986                    reject,
1987                }
1988            }
1989            ast::StatementKind::Switch {
1990                selector,
1991                ref cases,
1992            } => {
1993                let mut emitter = proc::Emitter::default();
1994                emitter.start(&ctx.function.expressions);
1995
1996                let mut ectx = ctx.as_expression(block, &mut emitter);
1997
1998                // Determine the scalar type of the selector and case expressions, find the
1999                // consensus type for automatic conversion, then convert them.
2000                let (mut exprs, spans) = core::iter::once(selector)
2001                    .chain(cases.iter().filter_map(|case| match case.value {
2002                        ast::SwitchValue::Expr(expr) => Some(expr),
2003                        ast::SwitchValue::Default => None,
2004                    }))
2005                    .enumerate()
2006                    .map(|(i, expr)| {
2007                        let span = ectx.ast_expressions.get_span(expr);
2008                        let expr = self.expression_for_abstract(expr, &mut ectx)?;
2009                        let ty = resolve_inner!(ectx, expr);
2010                        match *ty {
2011                            ir::TypeInner::Scalar(
2012                                ir::Scalar::I32 | ir::Scalar::U32 | ir::Scalar::ABSTRACT_INT,
2013                            ) => Ok((expr, span)),
2014                            _ => match i {
2015                                0 => Err(Box::new(Error::InvalidSwitchSelector { span })),
2016                                _ => Err(Box::new(Error::InvalidSwitchCase { span })),
2017                            },
2018                        }
2019                    })
2020                    .collect::<Result<(Vec<_>, Vec<_>)>>()?;
2021
2022                let mut consensus =
2023                    ectx.automatic_conversion_consensus(None, &exprs)
2024                        .map_err(|span_idx| Error::SwitchCaseTypeMismatch {
2025                            span: spans[span_idx],
2026                        })?;
2027                // Concretize to I32 if the selector and all cases were abstract
2028                if consensus == ir::Scalar::ABSTRACT_INT {
2029                    consensus = ir::Scalar::I32;
2030                }
2031                for expr in &mut exprs {
2032                    ectx.convert_to_leaf_scalar(expr, consensus)?;
2033                }
2034
2035                block.extend(emitter.finish(&ctx.function.expressions));
2036
2037                let mut exprs = exprs.into_iter();
2038                let selector = exprs
2039                    .next()
2040                    .expect("First element should be selector expression");
2041
2042                let cases = cases
2043                    .iter()
2044                    .map(|case| {
2045                        Ok(ir::SwitchCase {
2046                            value: match case.value {
2047                                ast::SwitchValue::Expr(expr) => {
2048                                    let span = ctx.ast_expressions.get_span(expr);
2049                                    let expr = exprs.next().expect(
2050                                        "Should yield expression for each SwitchValue::Expr case",
2051                                    );
2052                                    match ctx
2053                                        .module
2054                                        .to_ctx()
2055                                        .get_const_val_from(expr, &ctx.function.expressions)
2056                                    {
2057                                        Ok(ir::Literal::I32(value)) => ir::SwitchValue::I32(value),
2058                                        Ok(ir::Literal::U32(value)) => ir::SwitchValue::U32(value),
2059                                        _ => {
2060                                            return Err(Box::new(Error::InvalidSwitchCase {
2061                                                span,
2062                                            }));
2063                                        }
2064                                    }
2065                                }
2066                                ast::SwitchValue::Default => ir::SwitchValue::Default,
2067                            },
2068                            body: self.block(&case.body, is_inside_loop, ctx)?,
2069                            fall_through: case.fall_through,
2070                        })
2071                    })
2072                    .collect::<Result<_>>()?;
2073
2074                ir::Statement::Switch { selector, cases }
2075            }
2076            ast::StatementKind::Loop {
2077                ref body,
2078                ref continuing,
2079                break_if,
2080            } => {
2081                let body = self.block(body, true, ctx)?;
2082                let mut continuing = self.block(continuing, true, ctx)?;
2083
2084                let mut emitter = proc::Emitter::default();
2085                emitter.start(&ctx.function.expressions);
2086                let break_if = break_if
2087                    .map(|expr| {
2088                        self.expression(expr, &mut ctx.as_expression(&mut continuing, &mut emitter))
2089                    })
2090                    .transpose()?;
2091                continuing.extend(emitter.finish(&ctx.function.expressions));
2092
2093                ir::Statement::Loop {
2094                    body,
2095                    continuing,
2096                    break_if,
2097                }
2098            }
2099            ast::StatementKind::Break => ir::Statement::Break,
2100            ast::StatementKind::Continue => ir::Statement::Continue,
2101            ast::StatementKind::Return { value: ast_value } => {
2102                let mut emitter = proc::Emitter::default();
2103                emitter.start(&ctx.function.expressions);
2104
2105                let value;
2106                if let Some(ast_expr) = ast_value {
2107                    let result_ty = ctx.function.result.as_ref().map(|r| r.ty);
2108                    let mut ectx = ctx.as_expression(block, &mut emitter);
2109                    let expr = self.expression_for_abstract(ast_expr, &mut ectx)?;
2110
2111                    if let Some(result_ty) = result_ty {
2112                        let mut ectx = ctx.as_expression(block, &mut emitter);
2113                        let resolution = proc::TypeResolution::Handle(result_ty);
2114                        let converted =
2115                            ectx.try_automatic_conversions(expr, &resolution, Span::default())?;
2116                        value = Some(converted);
2117                    } else {
2118                        value = Some(expr);
2119                    }
2120                } else {
2121                    value = None;
2122                }
2123                block.extend(emitter.finish(&ctx.function.expressions));
2124
2125                ir::Statement::Return { value }
2126            }
2127            ast::StatementKind::Kill => ir::Statement::Kill,
2128            ast::StatementKind::Call(ref call_phrase) => {
2129                let mut emitter = proc::Emitter::default();
2130                emitter.start(&ctx.function.expressions);
2131
2132                let _ = self.call(
2133                    call_phrase,
2134                    stmt.span,
2135                    &mut ctx.as_expression(block, &mut emitter),
2136                    true,
2137                )?;
2138                block.extend(emitter.finish(&ctx.function.expressions));
2139                return Ok(());
2140            }
2141            ast::StatementKind::Assign {
2142                target: ast_target,
2143                op,
2144                value,
2145            } => {
2146                let mut emitter = proc::Emitter::default();
2147                emitter.start(&ctx.function.expressions);
2148                let target_span = ctx.ast_expressions.get_span(ast_target);
2149
2150                let mut ectx = ctx.as_expression(block, &mut emitter);
2151                let target = self.expression_for_reference(ast_target, &mut ectx)?;
2152                let target_handle = match target {
2153                    Typed::Reference(handle) => handle,
2154                    Typed::Plain(handle) => {
2155                        let ty = ctx.invalid_assignment_type(handle);
2156                        return Err(Box::new(Error::InvalidAssignment {
2157                            span: target_span,
2158                            ty,
2159                        }));
2160                    }
2161                };
2162
2163                // Usually the value needs to be converted to match the type of
2164                // the memory view you're assigning it to. The bit shift
2165                // operators are exceptions, in that the right operand is always
2166                // a `u32` or `vecN<u32>`.
2167                let target_scalar = match op {
2168                    Some(ir::BinaryOperator::ShiftLeft | ir::BinaryOperator::ShiftRight) => {
2169                        Some(ir::Scalar::U32)
2170                    }
2171                    _ => resolve_inner!(ectx, target_handle)
2172                        .pointer_automatically_convertible_scalar(&ectx.module.types),
2173                };
2174
2175                // Need to emit the LHS _before_ the RHS so that it is evaluated first.
2176                let op_assign = if let Some(op) = op {
2177                    Some((op, ectx.apply_load_rule(target)?))
2178                } else {
2179                    None
2180                };
2181
2182                let value = self.expression_for_abstract(value, &mut ectx)?;
2183                let mut value = match target_scalar {
2184                    Some(target_scalar) => ectx.try_automatic_conversion_for_leaf_scalar(
2185                        value,
2186                        target_scalar,
2187                        target_span,
2188                    )?,
2189                    None => value,
2190                };
2191
2192                let value = match op_assign {
2193                    Some((op, mut left)) => {
2194                        ectx.binary_op_splat(op, &mut left, &mut value)?;
2195                        ectx.append_expression(
2196                            ir::Expression::Binary {
2197                                op,
2198                                left,
2199                                right: value,
2200                            },
2201                            stmt.span,
2202                        )?
2203                    }
2204                    None => value,
2205                };
2206                block.extend(emitter.finish(&ctx.function.expressions));
2207
2208                ir::Statement::Store {
2209                    pointer: target_handle,
2210                    value,
2211                }
2212            }
2213            ast::StatementKind::Increment(value) | ast::StatementKind::Decrement(value) => {
2214                let mut emitter = proc::Emitter::default();
2215                emitter.start(&ctx.function.expressions);
2216
2217                let op = match stmt.kind {
2218                    ast::StatementKind::Increment(_) => ir::BinaryOperator::Add,
2219                    ast::StatementKind::Decrement(_) => ir::BinaryOperator::Subtract,
2220                    _ => unreachable!(),
2221                };
2222
2223                let value_span = ctx.ast_expressions.get_span(value);
2224                let target = self
2225                    .expression_for_reference(value, &mut ctx.as_expression(block, &mut emitter))?;
2226                let target_handle = target.ref_or(Error::BadIncrDecrReferenceType(value_span))?;
2227
2228                let mut ectx = ctx.as_expression(block, &mut emitter);
2229                let scalar = match *resolve_inner!(ectx, target_handle) {
2230                    ir::TypeInner::ValuePointer {
2231                        size: None, scalar, ..
2232                    } => scalar,
2233                    ir::TypeInner::Pointer { base, .. } => match ectx.module.types[base].inner {
2234                        ir::TypeInner::Scalar(scalar) => scalar,
2235                        _ => return Err(Box::new(Error::BadIncrDecrReferenceType(value_span))),
2236                    },
2237                    _ => return Err(Box::new(Error::BadIncrDecrReferenceType(value_span))),
2238                };
2239                let literal = match scalar.kind {
2240                    ir::ScalarKind::Sint | ir::ScalarKind::Uint => ir::Literal::one(scalar)
2241                        .ok_or(Error::BadIncrDecrReferenceType(value_span))?,
2242                    _ => return Err(Box::new(Error::BadIncrDecrReferenceType(value_span))),
2243                };
2244
2245                let right =
2246                    ectx.interrupt_emitter(ir::Expression::Literal(literal), Span::UNDEFINED)?;
2247                let rctx = ectx.runtime_expression_ctx(stmt.span)?;
2248                let left = rctx.function.expressions.append(
2249                    ir::Expression::Load {
2250                        pointer: target_handle,
2251                    },
2252                    value_span,
2253                );
2254                let value = rctx
2255                    .function
2256                    .expressions
2257                    .append(ir::Expression::Binary { op, left, right }, stmt.span);
2258                rctx.local_expression_kind_tracker
2259                    .insert(left, proc::ExpressionKind::Runtime);
2260                rctx.local_expression_kind_tracker
2261                    .insert(value, proc::ExpressionKind::Runtime);
2262
2263                block.extend(emitter.finish(&ctx.function.expressions));
2264                ir::Statement::Store {
2265                    pointer: target_handle,
2266                    value,
2267                }
2268            }
2269            ast::StatementKind::ConstAssert(condition) => {
2270                let mut emitter = proc::Emitter::default();
2271                emitter.start(&ctx.function.expressions);
2272
2273                let condition =
2274                    self.expression(condition, &mut ctx.as_const(block, &mut emitter))?;
2275
2276                let span = ctx.function.expressions.get_span(condition);
2277                match ctx
2278                    .module
2279                    .to_ctx()
2280                    .get_const_val_from(condition, &ctx.function.expressions)
2281                {
2282                    Ok(true) => Ok(()),
2283                    Ok(false) => Err(Error::ConstAssertFailed(span)),
2284                    Err(proc::ConstValueError::NonConst | proc::ConstValueError::Negative) => {
2285                        unreachable!()
2286                    }
2287                    Err(proc::ConstValueError::InvalidType) => Err(Error::NotBool(span)),
2288                }?;
2289
2290                block.extend(emitter.finish(&ctx.function.expressions));
2291
2292                return Ok(());
2293            }
2294            ast::StatementKind::Phony(expr) => {
2295                // Remembered the RHS of the phony assignment as a named expression. This
2296                // is important (1) to preserve the RHS for validation, (2) to track any
2297                // referenced globals.
2298                let mut emitter = proc::Emitter::default();
2299                emitter.start(&ctx.function.expressions);
2300
2301                let value = self.expression(expr, &mut ctx.as_expression(block, &mut emitter))?;
2302                block.extend(emitter.finish(&ctx.function.expressions));
2303                ctx.named_expressions
2304                    .insert(value, ("phony".to_string(), stmt.span));
2305                return Ok(());
2306            }
2307        };
2308
2309        block.push(out, stmt.span);
2310
2311        Ok(())
2312    }
2313
2314    /// Lower `expr` and apply the Load Rule if possible.
2315    ///
2316    /// For the time being, this concretizes abstract values, to support
2317    /// consumers that haven't been adapted to consume them yet. Consumers
2318    /// prepared for abstract values can call [`expression_for_abstract`].
2319    ///
2320    /// [`expression_for_abstract`]: Lowerer::expression_for_abstract
2321    fn expression(
2322        &mut self,
2323        expr: Handle<ast::Expression<'source>>,
2324        ctx: &mut ExpressionContext<'source, '_, '_>,
2325    ) -> Result<'source, Handle<ir::Expression>> {
2326        let expr = self.expression_for_abstract(expr, ctx)?;
2327        ctx.concretize(expr)
2328    }
2329
2330    fn expression_for_abstract(
2331        &mut self,
2332        expr: Handle<ast::Expression<'source>>,
2333        ctx: &mut ExpressionContext<'source, '_, '_>,
2334    ) -> Result<'source, Handle<ir::Expression>> {
2335        let expr = self.expression_for_reference(expr, ctx)?;
2336        ctx.apply_load_rule(expr)
2337    }
2338
2339    fn expression_with_leaf_scalar(
2340        &mut self,
2341        expr: Handle<ast::Expression<'source>>,
2342        scalar: ir::Scalar,
2343        ctx: &mut ExpressionContext<'source, '_, '_>,
2344    ) -> Result<'source, Handle<ir::Expression>> {
2345        let unconverted = self.expression_for_abstract(expr, ctx)?;
2346        ctx.try_automatic_conversion_for_leaf_scalar(unconverted, scalar, Span::default())
2347    }
2348
2349    fn expression_for_reference(
2350        &mut self,
2351        expr: Handle<ast::Expression<'source>>,
2352        ctx: &mut ExpressionContext<'source, '_, '_>,
2353    ) -> Result<'source, Typed<Handle<ir::Expression>>> {
2354        let span = ctx.ast_expressions.get_span(expr);
2355        let expr = &ctx.ast_expressions[expr];
2356
2357        let expr: Typed<ir::Expression> = match *expr {
2358            ast::Expression::Literal(literal) => {
2359                let literal = match literal {
2360                    ast::Literal::Number(Number::F16(f)) => ir::Literal::F16(f),
2361                    ast::Literal::Number(Number::F32(f)) => ir::Literal::F32(f),
2362                    ast::Literal::Number(Number::I32(i)) => ir::Literal::I32(i),
2363                    ast::Literal::Number(Number::U32(u)) => ir::Literal::U32(u),
2364                    ast::Literal::Number(Number::I64(i)) => ir::Literal::I64(i),
2365                    ast::Literal::Number(Number::U64(u)) => ir::Literal::U64(u),
2366                    ast::Literal::Number(Number::F64(f)) => ir::Literal::F64(f),
2367                    ast::Literal::Number(Number::AbstractInt(i)) => ir::Literal::AbstractInt(i),
2368                    ast::Literal::Number(Number::AbstractFloat(f)) => ir::Literal::AbstractFloat(f),
2369                    ast::Literal::Bool(b) => ir::Literal::Bool(b),
2370                };
2371                let handle = ctx.interrupt_emitter(ir::Expression::Literal(literal), span)?;
2372                return Ok(Typed::Plain(handle));
2373            }
2374            ast::Expression::Ident(ast::TemplateElaboratedIdent {
2375                ref template_list, ..
2376            }) if !template_list.is_empty() => {
2377                return Err(Box::new(Error::UnexpectedTemplate(span)))
2378            }
2379            ast::Expression::Ident(ast::TemplateElaboratedIdent {
2380                ident: ast::IdentExpr::Local(local),
2381                ..
2382            }) => {
2383                return ctx.local(&local, span);
2384            }
2385            ast::Expression::Ident(ast::TemplateElaboratedIdent {
2386                ident: ast::IdentExpr::Unresolved(name),
2387                ..
2388            }) => {
2389                let global = ctx
2390                    .globals
2391                    .get(name)
2392                    .ok_or(Error::UnknownIdent(span, name))?;
2393                let expr = match *global {
2394                    LoweredGlobalDecl::Var(handle) => {
2395                        let expr = ir::Expression::GlobalVariable(handle);
2396                        let v = &ctx.module.global_variables[handle];
2397                        match v.space {
2398                            ir::AddressSpace::Handle => Typed::Plain(expr),
2399                            _ => Typed::Reference(expr),
2400                        }
2401                    }
2402                    LoweredGlobalDecl::Const(handle) => {
2403                        Typed::Plain(ir::Expression::Constant(handle))
2404                    }
2405                    LoweredGlobalDecl::Override(handle) => {
2406                        Typed::Plain(ir::Expression::Override(handle))
2407                    }
2408                    LoweredGlobalDecl::Function { .. }
2409                    | LoweredGlobalDecl::Type(_)
2410                    | LoweredGlobalDecl::EntryPoint(_) => {
2411                        return Err(Box::new(Error::Unexpected(span, ExpectedToken::Variable)));
2412                    }
2413                };
2414
2415                return expr.try_map(|handle| ctx.interrupt_emitter(handle, span));
2416            }
2417            ast::Expression::Unary { op, expr } => {
2418                let expr = self.expression_for_abstract(expr, ctx)?;
2419                Typed::Plain(ir::Expression::Unary { op, expr })
2420            }
2421            ast::Expression::AddrOf(expr) => {
2422                // The `&` operator simply converts a reference to a pointer. And since a
2423                // reference is required, the Load Rule is not applied.
2424                match self.expression_for_reference(expr, ctx)? {
2425                    Typed::Reference(handle) => {
2426                        let expr = &ctx.runtime_expression_ctx(span)?.function.expressions[handle];
2427                        if let &ir::Expression::Access { base, .. }
2428                        | &ir::Expression::AccessIndex { base, .. } = expr
2429                        {
2430                            if let Some(ty) = resolve_inner!(ctx, base).pointer_base_type() {
2431                                if matches!(
2432                                    *ty.inner_with(&ctx.module.types),
2433                                    ir::TypeInner::Vector { .. },
2434                                ) {
2435                                    return Err(Box::new(Error::InvalidAddrOfOperand(
2436                                        ctx.get_expression_span(handle),
2437                                    )));
2438                                }
2439                            }
2440                        }
2441                        // No code is generated. We just declare the reference a pointer now.
2442                        return Ok(Typed::Plain(handle));
2443                    }
2444                    Typed::Plain(_) => {
2445                        return Err(Box::new(Error::NotReference(
2446                            "the operand of the `&` operator",
2447                            span,
2448                        )));
2449                    }
2450                }
2451            }
2452            ast::Expression::Deref(expr) => {
2453                // The pointer we dereference must be loaded.
2454                let pointer = self.expression(expr, ctx)?;
2455
2456                if resolve_inner!(ctx, pointer).pointer_space().is_none() {
2457                    return Err(Box::new(Error::NotPointer(span)));
2458                }
2459
2460                // No code is generated. We just declare the pointer a reference now.
2461                return Ok(Typed::Reference(pointer));
2462            }
2463            ast::Expression::Binary { op, left, right } => {
2464                self.binary(op, left, right, span, ctx)?
2465            }
2466            ast::Expression::Call(ref call_phrase) => {
2467                let handle = self
2468                    .call(call_phrase, span, ctx, false)?
2469                    .ok_or(Error::FunctionReturnsVoid(span))?;
2470                return Ok(Typed::Plain(handle));
2471            }
2472            ast::Expression::Index { base, index } => {
2473                let mut lowered_base = self.expression_for_reference(base, ctx)?;
2474                let index = self.expression(index, ctx)?;
2475
2476                // <https://www.w3.org/TR/WGSL/#language_extension-pointer_composite_access>
2477                // Declare pointer as reference
2478                if let Typed::Plain(handle) = lowered_base {
2479                    if resolve_inner!(ctx, handle).pointer_space().is_some() {
2480                        lowered_base = Typed::Reference(handle);
2481                    }
2482                }
2483
2484                lowered_base.try_map(|base| match ctx.get_const_val(index).ok() {
2485                    Some(index) => Ok::<_, Box<Error>>(ir::Expression::AccessIndex { base, index }),
2486                    None => {
2487                        // When an abstract array value e is indexed by an expression
2488                        // that is not a const-expression, then the array is concretized
2489                        // before the index is applied.
2490                        // https://www.w3.org/TR/WGSL/#array-access-expr
2491                        // Also applies to vectors and matrices.
2492                        let base = ctx.concretize(base)?;
2493                        Ok(ir::Expression::Access { base, index })
2494                    }
2495                })?
2496            }
2497            ast::Expression::Member { base, ref field } => {
2498                let mut lowered_base = self.expression_for_reference(base, ctx)?;
2499
2500                // <https://www.w3.org/TR/WGSL/#language_extension-pointer_composite_access>
2501                // Declare pointer as reference
2502                if let Typed::Plain(handle) = lowered_base {
2503                    if resolve_inner!(ctx, handle).pointer_space().is_some() {
2504                        lowered_base = Typed::Reference(handle);
2505                    }
2506                }
2507
2508                let temp_ty;
2509                let composite_type: &ir::TypeInner = match lowered_base {
2510                    Typed::Reference(handle) => {
2511                        temp_ty = resolve_inner!(ctx, handle)
2512                            .pointer_base_type()
2513                            .expect("In Typed::Reference(handle), handle must be a Naga pointer");
2514                        temp_ty.inner_with(&ctx.module.types)
2515                    }
2516
2517                    Typed::Plain(handle) => {
2518                        resolve_inner!(ctx, handle)
2519                    }
2520                };
2521
2522                let access = match *composite_type {
2523                    ir::TypeInner::Struct { ref members, .. } => {
2524                        let index = members
2525                            .iter()
2526                            .position(|m| m.name.as_deref() == Some(field.name))
2527                            .ok_or(Error::BadAccessor(field.span))?
2528                            as u32;
2529
2530                        lowered_base.map(|base| ir::Expression::AccessIndex { base, index })
2531                    }
2532                    ir::TypeInner::Vector { size: vec_size, .. } => {
2533                        match Components::new(field.name, field.span)? {
2534                            Components::Swizzle { size, pattern } => {
2535                                for &component in pattern[..size as usize].iter() {
2536                                    if component as u8 >= vec_size as u8 {
2537                                        return Err(Box::new(Error::BadAccessor(field.span)));
2538                                    }
2539                                }
2540                                Typed::Plain(ir::Expression::Swizzle {
2541                                    size,
2542                                    vector: ctx.apply_load_rule(lowered_base)?,
2543                                    pattern,
2544                                })
2545                            }
2546                            Components::Single(index) => {
2547                                if index >= vec_size as u32 {
2548                                    return Err(Box::new(Error::BadAccessor(field.span)));
2549                                }
2550                                lowered_base.map(|base| ir::Expression::AccessIndex { base, index })
2551                            }
2552                        }
2553                    }
2554                    _ => return Err(Box::new(Error::BadAccessor(field.span))),
2555                };
2556
2557                access
2558            }
2559        };
2560
2561        expr.try_map(|handle| ctx.append_expression(handle, span))
2562    }
2563
2564    /// Generate IR for the short-circuiting operators `&&` and `||`.
2565    ///
2566    /// `binary` has already lowered the LHS expression and resolved its type.
2567    fn logical(
2568        &mut self,
2569        op: crate::BinaryOperator,
2570        left: Handle<crate::Expression>,
2571        right: Handle<ast::Expression<'source>>,
2572        span: Span,
2573        ctx: &mut ExpressionContext<'source, '_, '_>,
2574    ) -> Result<'source, Typed<crate::Expression>> {
2575        debug_assert!(
2576            op == crate::BinaryOperator::LogicalAnd || op == crate::BinaryOperator::LogicalOr
2577        );
2578
2579        if ctx.is_runtime() {
2580            // To simulate short-circuiting behavior, we want to generate IR
2581            // like the following for `&&`. For `||`, the condition is `!_lhs`
2582            // and the else value is `true`.
2583            //
2584            // var _e0: bool;
2585            // if _lhs {
2586            //     _e0 = _rhs;
2587            // } else {
2588            //     _e0 = false;
2589            // }
2590
2591            let (condition, else_val) = if op == crate::BinaryOperator::LogicalAnd {
2592                let condition = left;
2593                let else_val = ctx.append_expression(
2594                    crate::Expression::Literal(crate::Literal::Bool(false)),
2595                    span,
2596                )?;
2597                (condition, else_val)
2598            } else {
2599                let condition = ctx.append_expression(
2600                    crate::Expression::Unary {
2601                        op: crate::UnaryOperator::LogicalNot,
2602                        expr: left,
2603                    },
2604                    span,
2605                )?;
2606                let else_val = ctx.append_expression(
2607                    crate::Expression::Literal(crate::Literal::Bool(true)),
2608                    span,
2609                )?;
2610                (condition, else_val)
2611            };
2612
2613            let bool_ty = ctx.ensure_type_exists(crate::TypeInner::Scalar(crate::Scalar::BOOL));
2614
2615            let rctx = ctx.runtime_expression_ctx(span)?;
2616            let result_var = rctx.function.local_variables.append(
2617                crate::LocalVariable {
2618                    name: None,
2619                    ty: bool_ty,
2620                    init: None,
2621                },
2622                span,
2623            );
2624            let pointer =
2625                ctx.append_expression(crate::Expression::LocalVariable(result_var), span)?;
2626
2627            let (right, mut accept) = ctx.with_nested_runtime_expression_ctx(span, |ctx| {
2628                let right = self.expression_for_abstract(right, ctx)?;
2629                ctx.grow_types(right)?;
2630                Ok(right)
2631            })?;
2632
2633            accept.push(
2634                crate::Statement::Store {
2635                    pointer,
2636                    value: right,
2637                },
2638                span,
2639            );
2640
2641            let mut reject = crate::Block::with_capacity(1);
2642            reject.push(
2643                crate::Statement::Store {
2644                    pointer,
2645                    value: else_val,
2646                },
2647                span,
2648            );
2649
2650            let rctx = ctx.runtime_expression_ctx(span)?;
2651            rctx.block.push(
2652                crate::Statement::If {
2653                    condition,
2654                    accept,
2655                    reject,
2656                },
2657                span,
2658            );
2659
2660            Ok(Typed::Reference(crate::Expression::LocalVariable(
2661                result_var,
2662            )))
2663        } else {
2664            let left_val: Option<bool> = ctx.get_const_val(left).ok();
2665
2666            if left_val.is_some_and(|left_val| {
2667                op == crate::BinaryOperator::LogicalAnd && !left_val
2668                    || op == crate::BinaryOperator::LogicalOr && left_val
2669            }) {
2670                // Short-circuit behavior: don't evaluate the RHS.
2671
2672                // TODO(https://github.com/gfx-rs/wgpu/issues/8440): We shouldn't ignore the
2673                // RHS completely, it should still be type-checked. Preserving it for type
2674                // checking is a bit tricky, because we're trying to produce an expression
2675                // for a const context, but the RHS is allowed to have things that aren't
2676                // const.
2677
2678                Ok(Typed::Plain(ctx.get(left).clone()))
2679            } else {
2680                // Evaluate the RHS and construct the entire binary expression as we
2681                // normally would. This case applies to well-formed constant logical
2682                // expressions that don't short-circuit (handled by the constant evaluator
2683                // shortly), to override expressions (handled when overrides are processed)
2684                // and to non-well-formed expressions (rejected by type checking).
2685                let right = self.expression_for_abstract(right, ctx)?;
2686                ctx.grow_types(right)?;
2687
2688                Ok(Typed::Plain(crate::Expression::Binary { op, left, right }))
2689            }
2690        }
2691    }
2692
2693    fn type_expression(
2694        &mut self,
2695        expr: Handle<ast::Expression<'source>>,
2696        ctx: &mut ExpressionContext<'source, '_, '_>,
2697    ) -> Result<'source, Handle<ir::Type>> {
2698        let span = ctx.ast_expressions.get_span(expr);
2699        let expr = &ctx.ast_expressions[expr];
2700
2701        let ident = match *expr {
2702            ast::Expression::Ident(ref ident) => ident,
2703            _ => return Err(Box::new(Error::UnexpectedExprForTypeExpression(span))),
2704        };
2705
2706        self.type_specifier(ident, ctx, None)
2707    }
2708
2709    fn type_specifier(
2710        &mut self,
2711        ident: &ast::TemplateElaboratedIdent<'source>,
2712        ctx: &mut ExpressionContext<'source, '_, '_>,
2713        alias_name: Option<String>,
2714    ) -> Result<'source, Handle<ir::Type>> {
2715        let &ast::TemplateElaboratedIdent {
2716            ref ident,
2717            ident_span,
2718            ref template_list,
2719            ..
2720        } = ident;
2721
2722        let ident = match *ident {
2723            ast::IdentExpr::Unresolved(ident) => ident,
2724            ast::IdentExpr::Local(_) => {
2725                // Since WGSL only supports module-scope type definitions and
2726                // aliases, a local identifier can't possibly refer to a type.
2727                return Err(Box::new(Error::UnexpectedExprForTypeExpression(ident_span)));
2728            }
2729        };
2730
2731        let mut tl = TemplateListIter::new(ident_span, template_list);
2732
2733        if let Some(global) = ctx.globals.get(ident) {
2734            let &LoweredGlobalDecl::Type(handle) = global else {
2735                return Err(Box::new(Error::UnexpectedExprForTypeExpression(ident_span)));
2736            };
2737
2738            // Type generators can only be predeclared, so since `ident` refers
2739            // to a module-scope declaration, the template parameter list should
2740            // be empty.
2741            tl.finish(ctx)?;
2742            return Ok(handle);
2743        }
2744
2745        // If `ident` doesn't resolve to a module-scope declaration, then it
2746        // must resolve to a predeclared type or type generator.
2747        let ty = conv::map_predeclared_type(&ctx.enable_extensions, ident_span, ident)?
2748            .ok_or_else(|| Box::new(Error::UnknownIdent(ident_span, ident)))?;
2749        let ty = self.finalize_type(ctx, ty, &mut tl, alias_name)?;
2750
2751        tl.finish(ctx)?;
2752
2753        Ok(ty)
2754    }
2755
2756    /// Construct an [`ir::Type`] from a [`conv::PredeclaredType`] and a list of
2757    /// template parameters.
2758    ///
2759    /// If we're processing a type alias, then `alias_name` is the name we
2760    /// should use in the new `ir::Type`.
2761    ///
2762    /// For example, when parsing `vec3<f32>`, the caller would pass:
2763    ///
2764    /// - for `ty`, [`TypeGenerator::Vector`], and
2765    ///
2766    /// - for `tl`, an iterator producing a single [`Expression::Ident`] representing `f32`.
2767    ///
2768    /// From those arguments this function will return a handle for the
2769    /// [`ir::Type`] representing `vec3<f32>`.
2770    ///
2771    /// [`TypeGenerator::Vector`]: conv::TypeGenerator::Vector
2772    /// [`Expression::Ident`]: crate::front::wgsl::parse::ast::Expression::Ident
2773    fn finalize_type(
2774        &mut self,
2775        ctx: &mut ExpressionContext<'source, '_, '_>,
2776        ty: conv::PredeclaredType,
2777        tl: &mut TemplateListIter<'_, 'source>,
2778        alias_name: Option<String>,
2779    ) -> Result<'source, Handle<ir::Type>> {
2780        let ty = match ty {
2781            conv::PredeclaredType::TypeInner(ty_inner) => {
2782                if let ir::TypeInner::Image {
2783                    class: ir::ImageClass::External,
2784                    ..
2785                } = ty_inner
2786                {
2787                    // Other than the WGSL backend, every backend that supports
2788                    // external textures does so by lowering them to a set of
2789                    // ordinary textures and some parameters saying how to
2790                    // sample from them. We don't know which backend will
2791                    // consume the `Module` we're building, but in case it's not
2792                    // WGSL, populate `SpecialTypes::external_texture_params`
2793                    // and `SpecialTypes::external_texture_transfer_function`
2794                    // with the types the backend will use for the parameter
2795                    // buffer.
2796                    //
2797                    // Neither of these are the type we are lowering here:
2798                    // that's an ordinary `TypeInner::Image`. But the fact we
2799                    // are lowering a `texture_external` implies the backends
2800                    // may need these additional types too.
2801                    ctx.module.generate_external_texture_types();
2802                }
2803
2804                ctx.as_global().ensure_type_exists(alias_name, ty_inner)
2805            }
2806            conv::PredeclaredType::RayDesc => ctx.module.generate_ray_desc_type(),
2807            conv::PredeclaredType::RayIntersection => ctx.module.generate_ray_intersection_type(),
2808            conv::PredeclaredType::TypeGenerator(type_generator) => {
2809                let ty_inner = match type_generator {
2810                    conv::TypeGenerator::Vector { size } => {
2811                        let (scalar, _) = tl.scalar_ty(self, ctx)?;
2812                        ir::TypeInner::Vector { size, scalar }
2813                    }
2814                    conv::TypeGenerator::Matrix { columns, rows } => {
2815                        let (scalar, span) = tl.scalar_ty(self, ctx)?;
2816                        if scalar.kind != ir::ScalarKind::Float {
2817                            return Err(Box::new(Error::BadMatrixScalarKind(span, scalar)));
2818                        }
2819                        ir::TypeInner::Matrix {
2820                            columns,
2821                            rows,
2822                            scalar,
2823                        }
2824                    }
2825                    conv::TypeGenerator::Array => {
2826                        let base = tl.ty(self, ctx)?;
2827                        let size = tl.maybe_array_size(self, ctx)?;
2828
2829                        // Determine the size of the base type, if needed.
2830                        ctx.layouter.update(ctx.module.to_ctx()).map_err(|err| {
2831                            let LayoutErrorInner::TooLarge = err.inner else {
2832                                unreachable!("unexpected layout error: {err:?}");
2833                            };
2834                            // Lots of type definitions don't get spans, so this error
2835                            // message may not be very useful.
2836                            Box::new(Error::TypeTooLarge {
2837                                span: ctx.module.types.get_span(err.ty),
2838                            })
2839                        })?;
2840                        let stride = ctx.layouter[base].to_stride();
2841
2842                        ir::TypeInner::Array { base, size, stride }
2843                    }
2844                    conv::TypeGenerator::Atomic => {
2845                        let (scalar, _) = tl.scalar_ty(self, ctx)?;
2846                        ir::TypeInner::Atomic(scalar)
2847                    }
2848                    conv::TypeGenerator::Pointer => {
2849                        let mut space = tl.address_space(ctx)?;
2850                        let base = tl.ty(self, ctx)?;
2851                        tl.maybe_access_mode(&mut space, ctx)?;
2852                        ir::TypeInner::Pointer { base, space }
2853                    }
2854                    conv::TypeGenerator::SampledTexture {
2855                        dim,
2856                        arrayed,
2857                        multi,
2858                    } => {
2859                        let (scalar, span) = tl.scalar_ty(self, ctx)?;
2860                        let ir::Scalar { kind, width } = scalar;
2861                        if width != 4 {
2862                            return Err(Box::new(Error::BadTextureSampleType { span, scalar }));
2863                        }
2864                        ir::TypeInner::Image {
2865                            dim,
2866                            arrayed,
2867                            class: ir::ImageClass::Sampled { kind, multi },
2868                        }
2869                    }
2870                    conv::TypeGenerator::StorageTexture { dim, arrayed } => {
2871                        let format = tl.storage_format(ctx)?;
2872                        let access = tl.access_mode(ctx)?;
2873                        ir::TypeInner::Image {
2874                            dim,
2875                            arrayed,
2876                            class: ir::ImageClass::Storage { format, access },
2877                        }
2878                    }
2879                    conv::TypeGenerator::BindingArray => {
2880                        let base = tl.ty(self, ctx)?;
2881                        let size = tl.maybe_array_size(self, ctx)?;
2882                        ir::TypeInner::BindingArray { base, size }
2883                    }
2884                    conv::TypeGenerator::AccelerationStructure => {
2885                        let vertex_return = tl.maybe_vertex_return(ctx)?;
2886                        ir::TypeInner::AccelerationStructure { vertex_return }
2887                    }
2888                    conv::TypeGenerator::RayQuery => {
2889                        let vertex_return = tl.maybe_vertex_return(ctx)?;
2890                        ir::TypeInner::RayQuery { vertex_return }
2891                    }
2892                    conv::TypeGenerator::CooperativeMatrix { columns, rows } => {
2893                        let (ty, span) = tl.ty_with_span(self, ctx)?;
2894                        let ir::TypeInner::Scalar(scalar) = ctx.module.types[ty].inner else {
2895                            return Err(Box::new(Error::UnsupportedCooperativeScalar(span)));
2896                        };
2897                        let role = tl.cooperative_role(ctx)?;
2898                        ir::TypeInner::CooperativeMatrix {
2899                            columns,
2900                            rows,
2901                            scalar,
2902                            role,
2903                        }
2904                    }
2905                };
2906                ctx.as_global().ensure_type_exists(alias_name, ty_inner)
2907            }
2908        };
2909        Ok(ty)
2910    }
2911
2912    fn binary(
2913        &mut self,
2914        op: ir::BinaryOperator,
2915        left: Handle<ast::Expression<'source>>,
2916        right: Handle<ast::Expression<'source>>,
2917        span: Span,
2918        ctx: &mut ExpressionContext<'source, '_, '_>,
2919    ) -> Result<'source, Typed<ir::Expression>> {
2920        if op == ir::BinaryOperator::LogicalAnd || op == ir::BinaryOperator::LogicalOr {
2921            let left = self.expression_for_abstract(left, ctx)?;
2922            ctx.grow_types(left)?;
2923
2924            if !matches!(
2925                resolve_inner!(ctx, left),
2926                &ir::TypeInner::Scalar(ir::Scalar::BOOL)
2927            ) {
2928                // Pass it through as-is, will fail validation
2929                let right = self.expression_for_abstract(right, ctx)?;
2930                ctx.grow_types(right)?;
2931                Ok(Typed::Plain(crate::Expression::Binary { op, left, right }))
2932            } else {
2933                self.logical(op, left, right, span, ctx)
2934            }
2935        } else {
2936            // Load both operands.
2937            let mut left = self.expression_for_abstract(left, ctx)?;
2938            let mut right = self.expression_for_abstract(right, ctx)?;
2939
2940            // Convert `scalar op vector` to `vector op vector` by introducing
2941            // `Splat` expressions.
2942            ctx.binary_op_splat(op, &mut left, &mut right)?;
2943
2944            // Apply automatic conversions.
2945            match op {
2946                ir::BinaryOperator::ShiftLeft | ir::BinaryOperator::ShiftRight => {
2947                    // Shift operators require the right operand to be `u32` or
2948                    // `vecN<u32>`. We can let the validator sort out vector length
2949                    // issues, but the right operand must be, or convert to, a u32 leaf
2950                    // scalar.
2951                    right =
2952                        ctx.try_automatic_conversion_for_leaf_scalar(right, ir::Scalar::U32, span)?;
2953
2954                    // Additionally, we must concretize the left operand if the right operand
2955                    // is not a const-expression.
2956                    // See https://www.w3.org/TR/WGSL/#overload-resolution-section.
2957                    //
2958                    // 2. Eliminate any candidate where one of its subexpressions resolves to
2959                    // an abstract type after feasible automatic conversions, but another of
2960                    // the candidate’s subexpressions is not a const-expression.
2961                    //
2962                    // We only have to explicitly do so for shifts as their operands may be
2963                    // of different types - for other binary ops this is achieved by finding
2964                    // the conversion consensus for both operands.
2965                    if !ctx.is_const(right) {
2966                        left = ctx.concretize(left)?;
2967                    }
2968                }
2969
2970                // All other operators follow the same pattern: reconcile the
2971                // scalar leaf types. If there's no reconciliation possible,
2972                // leave the expressions as they are: validation will report the
2973                // problem.
2974                _ => {
2975                    ctx.grow_types(left)?;
2976                    ctx.grow_types(right)?;
2977                    if let Ok(consensus_scalar) =
2978                        ctx.automatic_conversion_consensus(None, [left, right].iter())
2979                    {
2980                        ctx.convert_to_leaf_scalar(&mut left, consensus_scalar)?;
2981                        ctx.convert_to_leaf_scalar(&mut right, consensus_scalar)?;
2982                    }
2983                }
2984            }
2985
2986            Ok(Typed::Plain(ir::Expression::Binary { op, left, right }))
2987        }
2988    }
2989
2990    /// Generate Naga IR for a call to a WGSL builtin function.
2991    #[allow(clippy::too_many_arguments)]
2992    fn call_builtin<'phrase>(
2993        &mut self,
2994        function_name: &'source str,
2995        function_span: Span,
2996        arguments: &[Handle<ast::Expression<'source>>],
2997        template_params: &mut TemplateListIter<'phrase, 'source>,
2998        call_span: Span,
2999        ctx: &mut ExpressionContext<'source, '_, '_>,
3000        is_statement: bool,
3001    ) -> Result<'source, Option<(Handle<ir::Expression>, MustUse)>> {
3002        let (expr, must_use) = if let Some(fun) = conv::map_relational_fun(function_name) {
3003            let mut args = ctx.prepare_args(arguments, 1, function_span);
3004            let argument = self.expression(args.next()?, ctx)?;
3005            args.finish()?;
3006
3007            // Check for no-op all(bool) and any(bool):
3008            let argument_unmodified = matches!(
3009                fun,
3010                ir::RelationalFunction::All | ir::RelationalFunction::Any
3011            ) && {
3012                matches!(
3013                    resolve_inner!(ctx, argument),
3014                    &ir::TypeInner::Scalar(ir::Scalar {
3015                        kind: ir::ScalarKind::Bool,
3016                        ..
3017                    })
3018                )
3019            };
3020
3021            if argument_unmodified {
3022                return Ok(Some((argument, MustUse::Yes)));
3023            } else {
3024                (ir::Expression::Relational { fun, argument }, MustUse::Yes)
3025            }
3026        } else if let Some((axis, ctrl)) = conv::map_derivative(function_name) {
3027            let mut args = ctx.prepare_args(arguments, 1, function_span);
3028            let expr = self.expression(args.next()?, ctx)?;
3029            args.finish()?;
3030
3031            (
3032                ir::Expression::Derivative { axis, ctrl, expr },
3033                MustUse::Yes,
3034            )
3035        } else if let Some(fun) = conv::map_standard_fun(function_name) {
3036            (
3037                self.math_function_helper(function_span, fun, arguments, ctx)?,
3038                MustUse::Yes,
3039            )
3040        } else if let Some(fun) = Texture::map(function_name) {
3041            (
3042                self.texture_sample_helper(fun, arguments, function_span, ctx)?,
3043                MustUse::Yes,
3044            )
3045        } else if let Some((op, cop)) = conv::map_subgroup_operation(function_name) {
3046            return Ok(Some((
3047                self.subgroup_operation_helper(function_span, op, cop, arguments, ctx)?,
3048                MustUse::Yes,
3049            )));
3050        } else if let Some(mode) = SubgroupGather::map(function_name) {
3051            return Ok(Some((
3052                self.subgroup_gather_helper(function_span, mode, arguments, ctx)?,
3053                MustUse::Yes,
3054            )));
3055        } else if let Some(fun) = ir::AtomicFunction::map(function_name) {
3056            return Ok(self
3057                .atomic_helper(function_span, fun, arguments, is_statement, ctx)?
3058                .map(|result| (result, MustUse::No)));
3059        } else {
3060            match function_name {
3061                "bitcast" => {
3062                    let ty = template_params.ty(self, ctx)?;
3063
3064                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3065                    let expr = self.expression(args.next()?, ctx)?;
3066                    args.finish()?;
3067
3068                    let element_scalar = match ctx.module.types[ty].inner {
3069                        ir::TypeInner::Scalar(scalar) => scalar,
3070                        ir::TypeInner::Vector { scalar, .. } => scalar,
3071                        _ => {
3072                            let ty_resolution = resolve!(ctx, expr);
3073                            return Err(Box::new(Error::BadTypeCast {
3074                                from_type: ctx.type_resolution_to_string(ty_resolution),
3075                                span: function_span,
3076                                to_type: ctx.type_to_string(ty),
3077                            }));
3078                        }
3079                    };
3080
3081                    (
3082                        ir::Expression::As {
3083                            expr,
3084                            kind: element_scalar.kind,
3085                            convert: None,
3086                        },
3087                        MustUse::Yes,
3088                    )
3089                }
3090                "coopLoad" | "coopLoadT" => {
3091                    let row_major = function_name.ends_with("T");
3092                    let (matrix_ty, matrix_span) = template_params.ty_with_span(self, ctx)?;
3093
3094                    let mut args = ctx.prepare_args(arguments, 1, call_span);
3095                    let pointer = self.expression(args.next()?, ctx)?;
3096                    let (columns, rows, role) = match ctx.module.types[matrix_ty].inner {
3097                        ir::TypeInner::CooperativeMatrix {
3098                            columns,
3099                            rows,
3100                            role,
3101                            ..
3102                        } => (columns, rows, role),
3103                        _ => return Err(Box::new(Error::InvalidCooperativeLoadType(matrix_span))),
3104                    };
3105                    let stride = if args.total_args > 1 {
3106                        self.expression(args.next()?, ctx)?
3107                    } else {
3108                        // Infer the stride from the matrix type
3109                        let stride = if row_major {
3110                            columns as u32
3111                        } else {
3112                            rows as u32
3113                        };
3114                        ctx.append_expression(
3115                            ir::Expression::Literal(ir::Literal::U32(stride)),
3116                            Span::UNDEFINED,
3117                        )?
3118                    };
3119                    args.finish()?;
3120
3121                    (
3122                        crate::Expression::CooperativeLoad {
3123                            columns,
3124                            rows,
3125                            role,
3126                            data: crate::CooperativeData {
3127                                pointer,
3128                                stride,
3129                                row_major,
3130                            },
3131                        },
3132                        MustUse::Yes,
3133                    )
3134                }
3135                "select" => {
3136                    let mut args = ctx.prepare_args(arguments, 3, function_span);
3137
3138                    let reject_orig = args.next()?;
3139                    let accept_orig = args.next()?;
3140                    let mut values = [
3141                        self.expression_for_abstract(reject_orig, ctx)?,
3142                        self.expression_for_abstract(accept_orig, ctx)?,
3143                    ];
3144                    let condition = self.expression(args.next()?, ctx)?;
3145
3146                    args.finish()?;
3147
3148                    let diagnostic_details =
3149                        |ctx: &ExpressionContext<'_, '_, '_>,
3150                         ty_res: &proc::TypeResolution,
3151                         orig_expr| {
3152                            (
3153                                ctx.ast_expressions.get_span(orig_expr),
3154                                format!("`{}`", ctx.as_diagnostic_display(ty_res)),
3155                            )
3156                        };
3157                    for (&value, orig_value) in values.iter().zip([reject_orig, accept_orig]) {
3158                        let value_ty_res = resolve!(ctx, value);
3159                        if value_ty_res
3160                            .inner_with(&ctx.module.types)
3161                            .vector_size_and_scalar()
3162                            .is_none()
3163                        {
3164                            let (arg_span, arg_type) =
3165                                diagnostic_details(ctx, value_ty_res, orig_value);
3166                            return Err(Box::new(Error::SelectUnexpectedArgumentType {
3167                                arg_span,
3168                                arg_type,
3169                            }));
3170                        }
3171                    }
3172                    let mut consensus_scalar = ctx
3173                        .automatic_conversion_consensus(None, &values)
3174                        .map_err(|_idx| {
3175                            let [reject, accept] = values;
3176                            let [(reject_span, reject_type), (accept_span, accept_type)] =
3177                                [(reject_orig, reject), (accept_orig, accept)].map(
3178                                    |(orig_expr, expr)| {
3179                                        let ty_res = &ctx.typifier()[expr];
3180                                        diagnostic_details(ctx, ty_res, orig_expr)
3181                                    },
3182                                );
3183                            Error::SelectRejectAndAcceptHaveNoCommonType {
3184                                reject_span,
3185                                reject_type,
3186                                accept_span,
3187                                accept_type,
3188                            }
3189                        })?;
3190                    if !ctx.is_const(condition) {
3191                        consensus_scalar = consensus_scalar.concretize();
3192                    }
3193
3194                    ctx.convert_slice_to_common_leaf_scalar(&mut values, consensus_scalar)?;
3195
3196                    let [reject, accept] = values;
3197
3198                    (
3199                        ir::Expression::Select {
3200                            reject,
3201                            accept,
3202                            condition,
3203                        },
3204                        MustUse::Yes,
3205                    )
3206                }
3207                "arrayLength" => {
3208                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3209                    let expr = self.expression(args.next()?, ctx)?;
3210                    args.finish()?;
3211
3212                    (ir::Expression::ArrayLength(expr), MustUse::Yes)
3213                }
3214                "atomicLoad" => {
3215                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3216                    let (pointer, _scalar) = self.atomic_pointer(args.next()?, ctx)?;
3217                    args.finish()?;
3218
3219                    (ir::Expression::Load { pointer }, MustUse::No)
3220                }
3221                "atomicStore" => {
3222                    let mut args = ctx.prepare_args(arguments, 2, function_span);
3223                    let (pointer, scalar) = self.atomic_pointer(args.next()?, ctx)?;
3224                    let value = self.expression_with_leaf_scalar(args.next()?, scalar, ctx)?;
3225                    args.finish()?;
3226
3227                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3228                    rctx.block
3229                        .extend(rctx.emitter.finish(&rctx.function.expressions));
3230                    rctx.emitter.start(&rctx.function.expressions);
3231                    rctx.block
3232                        .push(ir::Statement::Store { pointer, value }, function_span);
3233                    return Ok(None);
3234                }
3235                "atomicCompareExchangeWeak" => {
3236                    let mut args = ctx.prepare_args(arguments, 3, function_span);
3237
3238                    let (pointer, scalar) = self.atomic_pointer(args.next()?, ctx)?;
3239
3240                    let compare = self.expression_with_leaf_scalar(args.next()?, scalar, ctx)?;
3241
3242                    let value = args.next()?;
3243                    let value_span = ctx.ast_expressions.get_span(value);
3244                    let value = self.expression_with_leaf_scalar(value, scalar, ctx)?;
3245
3246                    args.finish()?;
3247
3248                    let expression = match *resolve_inner!(ctx, value) {
3249                        ir::TypeInner::Scalar(scalar) => ir::Expression::AtomicResult {
3250                            ty: ctx.module.generate_predeclared_type(
3251                                ir::PredeclaredType::AtomicCompareExchangeWeakResult(scalar),
3252                            ),
3253                            comparison: true,
3254                        },
3255                        _ => return Err(Box::new(Error::InvalidAtomicOperandType(value_span))),
3256                    };
3257
3258                    let result = ctx.interrupt_emitter(expression, function_span)?;
3259                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3260                    rctx.block.push(
3261                        ir::Statement::Atomic {
3262                            pointer,
3263                            fun: ir::AtomicFunction::Exchange {
3264                                compare: Some(compare),
3265                            },
3266                            value,
3267                            result: Some(result),
3268                        },
3269                        function_span,
3270                    );
3271                    return Ok(Some((result, MustUse::No)));
3272                }
3273                "textureAtomicMin" | "textureAtomicMax" | "textureAtomicAdd"
3274                | "textureAtomicAnd" | "textureAtomicOr" | "textureAtomicXor" => {
3275                    let mut args = ctx.prepare_args(arguments, 3, function_span);
3276
3277                    let image = args.next()?;
3278                    let image_span = ctx.ast_expressions.get_span(image);
3279                    let image = self.expression(image, ctx)?;
3280
3281                    let coordinate = self.expression(args.next()?, ctx)?;
3282
3283                    let (_, arrayed) = ctx.image_data(image, image_span)?;
3284                    let array_index = arrayed
3285                        .then(|| {
3286                            args.min_args += 1;
3287                            self.expression(args.next()?, ctx)
3288                        })
3289                        .transpose()?;
3290
3291                    let value = self.expression(args.next()?, ctx)?;
3292
3293                    args.finish()?;
3294
3295                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3296                    rctx.block
3297                        .extend(rctx.emitter.finish(&rctx.function.expressions));
3298                    rctx.emitter.start(&rctx.function.expressions);
3299                    let stmt = ir::Statement::ImageAtomic {
3300                        image,
3301                        coordinate,
3302                        array_index,
3303                        fun: match function_name {
3304                            "textureAtomicMin" => ir::AtomicFunction::Min,
3305                            "textureAtomicMax" => ir::AtomicFunction::Max,
3306                            "textureAtomicAdd" => ir::AtomicFunction::Add,
3307                            "textureAtomicAnd" => ir::AtomicFunction::And,
3308                            "textureAtomicOr" => ir::AtomicFunction::InclusiveOr,
3309                            "textureAtomicXor" => ir::AtomicFunction::ExclusiveOr,
3310                            _ => unreachable!(),
3311                        },
3312                        value,
3313                    };
3314                    rctx.block.push(stmt, function_span);
3315                    return Ok(None);
3316                }
3317                "storageBarrier" => {
3318                    ctx.prepare_args(arguments, 0, function_span).finish()?;
3319
3320                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3321                    rctx.block.push(
3322                        ir::Statement::ControlBarrier(ir::Barrier::STORAGE),
3323                        function_span,
3324                    );
3325                    return Ok(None);
3326                }
3327                "workgroupBarrier" => {
3328                    ctx.prepare_args(arguments, 0, function_span).finish()?;
3329
3330                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3331                    rctx.block.push(
3332                        ir::Statement::ControlBarrier(ir::Barrier::WORK_GROUP),
3333                        function_span,
3334                    );
3335                    return Ok(None);
3336                }
3337                "subgroupBarrier" => {
3338                    ctx.prepare_args(arguments, 0, function_span).finish()?;
3339
3340                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3341                    rctx.block.push(
3342                        ir::Statement::ControlBarrier(ir::Barrier::SUB_GROUP),
3343                        function_span,
3344                    );
3345                    return Ok(None);
3346                }
3347                "textureBarrier" => {
3348                    ctx.prepare_args(arguments, 0, function_span).finish()?;
3349
3350                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3351                    rctx.block.push(
3352                        ir::Statement::ControlBarrier(ir::Barrier::TEXTURE),
3353                        function_span,
3354                    );
3355                    return Ok(None);
3356                }
3357                "workgroupUniformLoad" => {
3358                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3359                    let expr = args.next()?;
3360                    args.finish()?;
3361
3362                    let pointer = self.expression(expr, ctx)?;
3363                    let result_ty = match *resolve_inner!(ctx, pointer) {
3364                        ir::TypeInner::Pointer {
3365                            base,
3366                            space: ir::AddressSpace::WorkGroup,
3367                        } => match ctx.module.types[base].inner {
3368                            // Match `Expression::Load` semantics:
3369                            // loading through a pointer to `atomic<T>` produces a `T`.
3370                            ir::TypeInner::Atomic(scalar) => ctx.module.types.insert(
3371                                ir::Type {
3372                                    name: None,
3373                                    inner: ir::TypeInner::Scalar(scalar),
3374                                },
3375                                function_span,
3376                            ),
3377                            _ => base,
3378                        },
3379                        ir::TypeInner::ValuePointer {
3380                            size,
3381                            scalar,
3382                            space: ir::AddressSpace::WorkGroup,
3383                        } => ctx.module.types.insert(
3384                            ir::Type {
3385                                name: None,
3386                                inner: match size {
3387                                    Some(size) => ir::TypeInner::Vector { size, scalar },
3388                                    None => ir::TypeInner::Scalar(scalar),
3389                                },
3390                            },
3391                            function_span,
3392                        ),
3393                        _ => {
3394                            let span = ctx.ast_expressions.get_span(expr);
3395                            return Err(Box::new(Error::InvalidWorkGroupUniformLoad(span)));
3396                        }
3397                    };
3398                    let result = ctx.interrupt_emitter(
3399                        ir::Expression::WorkGroupUniformLoadResult { ty: result_ty },
3400                        function_span,
3401                    )?;
3402                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3403                    rctx.block.push(
3404                        ir::Statement::WorkGroupUniformLoad { pointer, result },
3405                        function_span,
3406                    );
3407
3408                    return Ok(Some((result, MustUse::Yes)));
3409                }
3410                "textureStore" => {
3411                    let mut args = ctx.prepare_args(arguments, 3, function_span);
3412
3413                    let image = args.next()?;
3414                    let image_span = ctx.ast_expressions.get_span(image);
3415                    let image = self.expression(image, ctx)?;
3416
3417                    let coordinate = self.expression(args.next()?, ctx)?;
3418
3419                    let (class, arrayed) = ctx.image_data(image, image_span)?;
3420                    let array_index = arrayed
3421                        .then(|| {
3422                            args.min_args += 1;
3423                            self.expression(args.next()?, ctx)
3424                        })
3425                        .transpose()?;
3426                    let scalar = if let ir::ImageClass::Storage { format, .. } = class {
3427                        format.into()
3428                    } else {
3429                        return Err(Box::new(Error::NotStorageTexture(image_span)));
3430                    };
3431
3432                    let value = self.expression_with_leaf_scalar(args.next()?, scalar, ctx)?;
3433
3434                    args.finish()?;
3435
3436                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3437                    rctx.block
3438                        .extend(rctx.emitter.finish(&rctx.function.expressions));
3439                    rctx.emitter.start(&rctx.function.expressions);
3440                    let stmt = ir::Statement::ImageStore {
3441                        image,
3442                        coordinate,
3443                        array_index,
3444                        value,
3445                    };
3446                    rctx.block.push(stmt, function_span);
3447                    return Ok(None);
3448                }
3449                "textureLoad" => {
3450                    let mut args = ctx.prepare_args(arguments, 2, function_span);
3451
3452                    let image = args.next()?;
3453                    let image_span = ctx.ast_expressions.get_span(image);
3454                    let image = self.expression(image, ctx)?;
3455
3456                    let coordinate = self.expression(args.next()?, ctx)?;
3457
3458                    let (class, arrayed) = ctx.image_data(image, image_span)?;
3459                    let array_index = arrayed
3460                        .then(|| {
3461                            args.min_args += 1;
3462                            self.expression(args.next()?, ctx)
3463                        })
3464                        .transpose()?;
3465
3466                    let level = class
3467                        .is_mipmapped()
3468                        .then(|| {
3469                            args.min_args += 1;
3470                            self.expression(args.next()?, ctx)
3471                        })
3472                        .transpose()?;
3473
3474                    let sample = class
3475                        .is_multisampled()
3476                        .then(|| self.expression(args.next()?, ctx))
3477                        .transpose()?;
3478
3479                    args.finish()?;
3480
3481                    (
3482                        ir::Expression::ImageLoad {
3483                            image,
3484                            coordinate,
3485                            array_index,
3486                            level,
3487                            sample,
3488                        },
3489                        MustUse::Yes,
3490                    )
3491                }
3492                "textureDimensions" => {
3493                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3494                    let image = self.expression(args.next()?, ctx)?;
3495                    let level = args
3496                        .next()
3497                        .map(|arg| self.expression(arg, ctx))
3498                        .ok()
3499                        .transpose()?;
3500                    args.finish()?;
3501
3502                    (
3503                        ir::Expression::ImageQuery {
3504                            image,
3505                            query: ir::ImageQuery::Size { level },
3506                        },
3507                        MustUse::Yes,
3508                    )
3509                }
3510                "textureNumLevels" => {
3511                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3512                    let image = self.expression(args.next()?, ctx)?;
3513                    args.finish()?;
3514
3515                    (
3516                        ir::Expression::ImageQuery {
3517                            image,
3518                            query: ir::ImageQuery::NumLevels,
3519                        },
3520                        MustUse::Yes,
3521                    )
3522                }
3523                "textureNumLayers" => {
3524                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3525                    let image = self.expression(args.next()?, ctx)?;
3526                    args.finish()?;
3527
3528                    (
3529                        ir::Expression::ImageQuery {
3530                            image,
3531                            query: ir::ImageQuery::NumLayers,
3532                        },
3533                        MustUse::Yes,
3534                    )
3535                }
3536                "textureNumSamples" => {
3537                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3538                    let image = self.expression(args.next()?, ctx)?;
3539                    args.finish()?;
3540
3541                    (
3542                        ir::Expression::ImageQuery {
3543                            image,
3544                            query: ir::ImageQuery::NumSamples,
3545                        },
3546                        MustUse::Yes,
3547                    )
3548                }
3549                "rayQueryInitialize" => {
3550                    let mut args = ctx.prepare_args(arguments, 3, function_span);
3551                    let query = self.ray_query_pointer(args.next()?, ctx)?;
3552                    let acceleration_structure = self.expression(args.next()?, ctx)?;
3553                    let descriptor = self.expression(args.next()?, ctx)?;
3554                    args.finish()?;
3555
3556                    let _ = ctx.module.generate_ray_desc_type();
3557                    let fun = ir::RayQueryFunction::Initialize {
3558                        acceleration_structure,
3559                        descriptor,
3560                    };
3561
3562                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3563                    rctx.block
3564                        .extend(rctx.emitter.finish(&rctx.function.expressions));
3565                    rctx.emitter.start(&rctx.function.expressions);
3566                    rctx.block
3567                        .push(ir::Statement::RayQuery { query, fun }, function_span);
3568                    return Ok(None);
3569                }
3570                "getCommittedHitVertexPositions" => {
3571                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3572                    let query = self.ray_query_pointer(args.next()?, ctx)?;
3573                    args.finish()?;
3574
3575                    let _ = ctx.module.generate_vertex_return_type();
3576
3577                    (
3578                        ir::Expression::RayQueryVertexPositions {
3579                            query,
3580                            committed: true,
3581                        },
3582                        MustUse::No,
3583                    )
3584                }
3585                "getCandidateHitVertexPositions" => {
3586                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3587                    let query = self.ray_query_pointer(args.next()?, ctx)?;
3588                    args.finish()?;
3589
3590                    let _ = ctx.module.generate_vertex_return_type();
3591
3592                    (
3593                        ir::Expression::RayQueryVertexPositions {
3594                            query,
3595                            committed: false,
3596                        },
3597                        MustUse::No,
3598                    )
3599                }
3600                "rayQueryProceed" => {
3601                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3602                    let query = self.ray_query_pointer(args.next()?, ctx)?;
3603                    args.finish()?;
3604
3605                    let result = ctx
3606                        .interrupt_emitter(ir::Expression::RayQueryProceedResult, function_span)?;
3607                    let fun = ir::RayQueryFunction::Proceed { result };
3608                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3609                    rctx.block
3610                        .push(ir::Statement::RayQuery { query, fun }, function_span);
3611                    return Ok(Some((result, MustUse::No)));
3612                }
3613                "rayQueryGenerateIntersection" => {
3614                    let mut args = ctx.prepare_args(arguments, 2, function_span);
3615                    let query = self.ray_query_pointer(args.next()?, ctx)?;
3616                    let hit_t = self.expression(args.next()?, ctx)?;
3617                    args.finish()?;
3618
3619                    let fun = ir::RayQueryFunction::GenerateIntersection { hit_t };
3620                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3621                    rctx.block
3622                        .push(ir::Statement::RayQuery { query, fun }, function_span);
3623                    return Ok(None);
3624                }
3625                "rayQueryConfirmIntersection" => {
3626                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3627                    let query = self.ray_query_pointer(args.next()?, ctx)?;
3628                    args.finish()?;
3629
3630                    let fun = ir::RayQueryFunction::ConfirmIntersection;
3631                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3632                    rctx.block
3633                        .push(ir::Statement::RayQuery { query, fun }, function_span);
3634                    return Ok(None);
3635                }
3636                "rayQueryTerminate" => {
3637                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3638                    let query = self.ray_query_pointer(args.next()?, ctx)?;
3639                    args.finish()?;
3640
3641                    let fun = ir::RayQueryFunction::Terminate;
3642                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3643                    rctx.block
3644                        .push(ir::Statement::RayQuery { query, fun }, function_span);
3645                    return Ok(None);
3646                }
3647                "rayQueryGetCommittedIntersection" => {
3648                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3649                    let query = self.ray_query_pointer(args.next()?, ctx)?;
3650                    args.finish()?;
3651
3652                    let _ = ctx.module.generate_ray_intersection_type();
3653                    (
3654                        ir::Expression::RayQueryGetIntersection {
3655                            query,
3656                            committed: true,
3657                        },
3658                        MustUse::No,
3659                    )
3660                }
3661                "rayQueryGetCandidateIntersection" => {
3662                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3663                    let query = self.ray_query_pointer(args.next()?, ctx)?;
3664                    args.finish()?;
3665
3666                    let _ = ctx.module.generate_ray_intersection_type();
3667                    (
3668                        ir::Expression::RayQueryGetIntersection {
3669                            query,
3670                            committed: false,
3671                        },
3672                        MustUse::No,
3673                    )
3674                }
3675                "subgroupBallot" => {
3676                    let mut args = ctx.prepare_args(arguments, 0, function_span);
3677                    let predicate = if arguments.len() == 1 {
3678                        Some(self.expression(args.next()?, ctx)?)
3679                    } else {
3680                        None
3681                    };
3682                    args.finish()?;
3683
3684                    let result =
3685                        ctx.interrupt_emitter(ir::Expression::SubgroupBallotResult, function_span)?;
3686                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3687                    rctx.block.push(
3688                        ir::Statement::SubgroupBallot { result, predicate },
3689                        function_span,
3690                    );
3691                    return Ok(Some((result, MustUse::Yes)));
3692                }
3693                "quadSwapX" => {
3694                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3695
3696                    let argument = self.expression(args.next()?, ctx)?;
3697                    args.finish()?;
3698
3699                    let ty = ctx.register_type(argument)?;
3700
3701                    let result = ctx.interrupt_emitter(
3702                        crate::Expression::SubgroupOperationResult { ty },
3703                        function_span,
3704                    )?;
3705                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3706                    rctx.block.push(
3707                        crate::Statement::SubgroupGather {
3708                            mode: crate::GatherMode::QuadSwap(crate::Direction::X),
3709                            argument,
3710                            result,
3711                        },
3712                        function_span,
3713                    );
3714                    return Ok(Some((result, MustUse::Yes)));
3715                }
3716                "quadSwapY" => {
3717                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3718
3719                    let argument = self.expression(args.next()?, ctx)?;
3720                    args.finish()?;
3721
3722                    let ty = ctx.register_type(argument)?;
3723
3724                    let result = ctx.interrupt_emitter(
3725                        crate::Expression::SubgroupOperationResult { ty },
3726                        function_span,
3727                    )?;
3728                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3729                    rctx.block.push(
3730                        crate::Statement::SubgroupGather {
3731                            mode: crate::GatherMode::QuadSwap(crate::Direction::Y),
3732                            argument,
3733                            result,
3734                        },
3735                        function_span,
3736                    );
3737                    return Ok(Some((result, MustUse::Yes)));
3738                }
3739                "quadSwapDiagonal" => {
3740                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3741
3742                    let argument = self.expression(args.next()?, ctx)?;
3743                    args.finish()?;
3744
3745                    let ty = ctx.register_type(argument)?;
3746
3747                    let result = ctx.interrupt_emitter(
3748                        crate::Expression::SubgroupOperationResult { ty },
3749                        function_span,
3750                    )?;
3751                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3752                    rctx.block.push(
3753                        crate::Statement::SubgroupGather {
3754                            mode: crate::GatherMode::QuadSwap(crate::Direction::Diagonal),
3755                            argument,
3756                            result,
3757                        },
3758                        function_span,
3759                    );
3760                    return Ok(Some((result, MustUse::Yes)));
3761                }
3762                "coopStore" | "coopStoreT" => {
3763                    let row_major = function_name.ends_with("T");
3764
3765                    let mut args = ctx.prepare_args(arguments, 2, function_span);
3766                    let target = self.expression(args.next()?, ctx)?;
3767                    let pointer = self.expression(args.next()?, ctx)?;
3768                    let stride = if args.total_args > 2 {
3769                        self.expression(args.next()?, ctx)?
3770                    } else {
3771                        // Infer the stride from the matrix type
3772                        let stride = match *resolve_inner!(ctx, target) {
3773                            ir::TypeInner::CooperativeMatrix { columns, rows, .. } => {
3774                                if row_major {
3775                                    columns as u32
3776                                } else {
3777                                    rows as u32
3778                                }
3779                            }
3780                            _ => 0,
3781                        };
3782                        ctx.append_expression(
3783                            ir::Expression::Literal(ir::Literal::U32(stride)),
3784                            Span::UNDEFINED,
3785                        )?
3786                    };
3787                    args.finish()?;
3788
3789                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3790                    rctx.block.push(
3791                        crate::Statement::CooperativeStore {
3792                            target,
3793                            data: crate::CooperativeData {
3794                                pointer,
3795                                stride,
3796                                row_major,
3797                            },
3798                        },
3799                        function_span,
3800                    );
3801                    return Ok(None);
3802                }
3803                "coopMultiplyAdd" => {
3804                    let mut args = ctx.prepare_args(arguments, 3, function_span);
3805                    let a = self.expression(args.next()?, ctx)?;
3806                    let b = self.expression(args.next()?, ctx)?;
3807                    let c = self.expression(args.next()?, ctx)?;
3808                    args.finish()?;
3809
3810                    (
3811                        ir::Expression::CooperativeMultiplyAdd { a, b, c },
3812                        MustUse::Yes,
3813                    )
3814                }
3815                "traceRay" => {
3816                    let mut args = ctx.prepare_args(arguments, 3, function_span);
3817                    let acceleration_structure = self.expression(args.next()?, ctx)?;
3818                    let descriptor = self.expression(args.next()?, ctx)?;
3819                    let payload = self.expression(args.next()?, ctx)?;
3820                    args.finish()?;
3821
3822                    let _ = ctx.module.generate_ray_desc_type();
3823                    let fun = ir::RayPipelineFunction::TraceRay {
3824                        acceleration_structure,
3825                        descriptor,
3826                        payload,
3827                    };
3828
3829                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3830                    rctx.block
3831                        .extend(rctx.emitter.finish(&rctx.function.expressions));
3832                    rctx.emitter.start(&rctx.function.expressions);
3833                    rctx.block
3834                        .push(ir::Statement::RayPipelineFunction(fun), function_span);
3835                    return Ok(None);
3836                }
3837                _ => return Err(Box::new(Error::UnknownIdent(function_span, function_name))),
3838            }
3839        };
3840
3841        let expr = ctx.append_expression(expr, function_span)?;
3842        Ok(Some((expr, must_use)))
3843    }
3844
3845    /// Generate Naga IR for call expressions and statements, and type
3846    /// constructor expressions.
3847    ///
3848    /// The "function" being called is simply an `Ident` that we know refers to
3849    /// some module-scope definition.
3850    ///
3851    /// - If it is the name of a type, then the expression is a type constructor
3852    ///   expression: either constructing a value from components, a conversion
3853    ///   expression, or a zero value expression.
3854    ///
3855    /// - If it is the name of a function, then we're generating a [`Call`]
3856    ///   statement. We may be in the midst of generating code for an
3857    ///   expression, in which case we must generate an `Emit` statement to
3858    ///   force evaluation of the IR expressions we've generated so far, add the
3859    ///   `Call` statement to the current block, and then resume generating
3860    ///   expressions.
3861    ///
3862    /// [`Call`]: ir::Statement::Call
3863    fn call(
3864        &mut self,
3865        call_phrase: &ast::CallPhrase<'source>,
3866        span: Span,
3867        ctx: &mut ExpressionContext<'source, '_, '_>,
3868        is_statement: bool,
3869    ) -> Result<'source, Option<Handle<ir::Expression>>> {
3870        let function_name = match call_phrase.function.ident {
3871            ast::IdentExpr::Unresolved(name) => name,
3872            ast::IdentExpr::Local(_) => {
3873                return Err(Box::new(Error::CalledLocalDecl(
3874                    call_phrase.function.ident_span,
3875                )))
3876            }
3877        };
3878        let mut function_span = call_phrase.function.ident_span;
3879        function_span.subsume(call_phrase.function.template_list_span);
3880        let arguments = call_phrase.arguments.as_slice();
3881
3882        let mut tl = TemplateListIter::new(function_span, &call_phrase.function.template_list);
3883
3884        let result = match ctx.globals.get(function_name) {
3885            Some(&LoweredGlobalDecl::Type(ty)) => {
3886                // user-declared types can't make use of template lists
3887                tl.finish(ctx)?;
3888
3889                let handle =
3890                    self.construct(span, Constructor::Type(ty), function_span, arguments, ctx)?;
3891                Some((handle, MustUse::Yes))
3892            }
3893            Some(
3894                &LoweredGlobalDecl::Const(_)
3895                | &LoweredGlobalDecl::Override(_)
3896                | &LoweredGlobalDecl::Var(_),
3897            ) => {
3898                return Err(Box::new(Error::Unexpected(
3899                    function_span,
3900                    ExpectedToken::Function,
3901                )))
3902            }
3903            Some(&LoweredGlobalDecl::EntryPoint(_)) => {
3904                return Err(Box::new(Error::CalledEntryPoint(function_span)));
3905            }
3906            Some(&LoweredGlobalDecl::Function {
3907                handle: function,
3908                must_use,
3909            }) => {
3910                // user-declared functions can't make use of template lists
3911                tl.finish(ctx)?;
3912
3913                let arguments = arguments
3914                    .iter()
3915                    .enumerate()
3916                    .map(|(i, &arg)| {
3917                        // Try to convert abstract values to the known argument types
3918                        let Some(&ir::FunctionArgument {
3919                            ty: parameter_ty, ..
3920                        }) = ctx.module.functions[function].arguments.get(i)
3921                        else {
3922                            // Wrong number of arguments... just concretize the type here
3923                            // and let the validator report the error.
3924                            return self.expression(arg, ctx);
3925                        };
3926
3927                        let expr = self.expression_for_abstract(arg, ctx)?;
3928                        ctx.try_automatic_conversions(
3929                            expr,
3930                            &proc::TypeResolution::Handle(parameter_ty),
3931                            ctx.ast_expressions.get_span(arg),
3932                        )
3933                    })
3934                    .collect::<Result<Vec<_>>>()?;
3935
3936                let has_result = ctx.module.functions[function].result.is_some();
3937
3938                let rctx = ctx.runtime_expression_ctx(span)?;
3939                // we need to always do this before a fn call since all arguments need to be emitted before the fn call
3940                rctx.block
3941                    .extend(rctx.emitter.finish(&rctx.function.expressions));
3942                let result = has_result.then(|| {
3943                    let result = rctx
3944                        .function
3945                        .expressions
3946                        .append(ir::Expression::CallResult(function), span);
3947                    rctx.local_expression_kind_tracker
3948                        .insert(result, proc::ExpressionKind::Runtime);
3949                    (result, must_use.into())
3950                });
3951                rctx.emitter.start(&rctx.function.expressions);
3952                rctx.block.push(
3953                    ir::Statement::Call {
3954                        function,
3955                        arguments,
3956                        result: result.map(|(expr, _)| expr),
3957                    },
3958                    span,
3959                );
3960
3961                result
3962            }
3963            None => {
3964                // If the name refers to a predeclared type, this is a construction expression.
3965                let ty = conv::map_predeclared_type(
3966                    &ctx.enable_extensions,
3967                    function_span,
3968                    function_name,
3969                )?;
3970                if let Some(ty) = ty {
3971                    let empty_template_list = call_phrase.function.template_list.is_empty();
3972                    let constructor_ty = match ty {
3973                        conv::PredeclaredType::TypeGenerator(conv::TypeGenerator::Vector {
3974                            size,
3975                        }) if empty_template_list => Constructor::PartialVector { size },
3976                        conv::PredeclaredType::TypeGenerator(conv::TypeGenerator::Matrix {
3977                            columns,
3978                            rows,
3979                        }) if empty_template_list => Constructor::PartialMatrix { columns, rows },
3980                        conv::PredeclaredType::TypeGenerator(conv::TypeGenerator::Array)
3981                            if empty_template_list =>
3982                        {
3983                            Constructor::PartialArray
3984                        }
3985                        conv::PredeclaredType::TypeGenerator(
3986                            conv::TypeGenerator::CooperativeMatrix { .. },
3987                        ) if empty_template_list => {
3988                            return Err(Box::new(Error::UnderspecifiedCooperativeMatrix));
3989                        }
3990                        _ => Constructor::Type(self.finalize_type(ctx, ty, &mut tl, None)?),
3991                    };
3992                    tl.finish(ctx)?;
3993                    let handle =
3994                        self.construct(span, constructor_ty, function_span, arguments, ctx)?;
3995                    Some((handle, MustUse::Yes))
3996                } else {
3997                    // Otherwise, it must be a call to a builtin function.
3998                    let result = self.call_builtin(
3999                        function_name,
4000                        function_span,
4001                        arguments,
4002                        &mut tl,
4003                        span,
4004                        ctx,
4005                        is_statement,
4006                    )?;
4007                    tl.finish(ctx)?;
4008                    result
4009                }
4010            }
4011        };
4012
4013        let result_used = !is_statement;
4014        if matches!(result, Some((_, MustUse::Yes))) && !result_used {
4015            return Err(Box::new(Error::FunctionMustUseUnused(function_span)));
4016        }
4017        Ok(result.map(|(expr, _)| expr))
4018    }
4019
4020    /// Generate a Naga IR [`Math`] expression.
4021    ///
4022    /// Generate Naga IR for a call to the [`MathFunction`] `fun`, whose
4023    /// unlowered arguments are `ast_arguments`.
4024    ///
4025    /// The `span` argument should give the span of the function name in the
4026    /// call expression.
4027    ///
4028    /// [`Math`]: ir::Expression::Math
4029    /// [`MathFunction`]: ir::MathFunction
4030    fn math_function_helper(
4031        &mut self,
4032        span: Span,
4033        fun: ir::MathFunction,
4034        ast_arguments: &[Handle<ast::Expression<'source>>],
4035        ctx: &mut ExpressionContext<'source, '_, '_>,
4036    ) -> Result<'source, ir::Expression> {
4037        let mut lowered_arguments = Vec::with_capacity(ast_arguments.len());
4038        for &arg in ast_arguments {
4039            let lowered = self.expression_for_abstract(arg, ctx)?;
4040            ctx.grow_types(lowered)?;
4041            lowered_arguments.push(lowered);
4042        }
4043
4044        let fun_overloads = fun.overloads();
4045        let rule = self.resolve_overloads(span, fun, fun_overloads, &lowered_arguments, ctx)?;
4046        self.apply_automatic_conversions_for_call(&rule, &mut lowered_arguments, ctx)?;
4047
4048        // If this function returns a predeclared type, register it
4049        // in `Module::special_types`. The typifier will expect to
4050        // be able to find it there.
4051        if let proc::Conclusion::Predeclared(predeclared) = rule.conclusion {
4052            ctx.module.generate_predeclared_type(predeclared);
4053        }
4054
4055        Ok(ir::Expression::Math {
4056            fun,
4057            arg: lowered_arguments[0],
4058            arg1: lowered_arguments.get(1).cloned(),
4059            arg2: lowered_arguments.get(2).cloned(),
4060            arg3: lowered_arguments.get(3).cloned(),
4061        })
4062    }
4063
4064    /// Choose the right overload for a function call.
4065    ///
4066    /// Return a [`Rule`] representing the most preferred overload in
4067    /// `overloads` to apply to `arguments`, or return an error explaining why
4068    /// the call is not valid.
4069    ///
4070    /// Use `fun` to identify the function being called in error messages;
4071    /// `span` should be the span of the function name in the call expression.
4072    ///
4073    /// [`Rule`]: proc::Rule
4074    fn resolve_overloads<O, F>(
4075        &self,
4076        span: Span,
4077        fun: F,
4078        overloads: O,
4079        arguments: &[Handle<ir::Expression>],
4080        ctx: &ExpressionContext<'source, '_, '_>,
4081    ) -> Result<'source, proc::Rule>
4082    where
4083        O: proc::OverloadSet,
4084        F: TryToWgsl + core::fmt::Debug + Copy,
4085    {
4086        let mut remaining_overloads = overloads.clone();
4087        let min_arguments = remaining_overloads.min_arguments();
4088        let max_arguments = remaining_overloads.max_arguments();
4089        if arguments.len() < min_arguments {
4090            return Err(Box::new(Error::WrongArgumentCount {
4091                span,
4092                expected: min_arguments as u32..max_arguments as u32,
4093                found: arguments.len() as u32,
4094            }));
4095        }
4096        if arguments.len() > max_arguments {
4097            return Err(Box::new(Error::TooManyArguments {
4098                function: fun.to_wgsl_for_diagnostics(),
4099                call_span: span,
4100                arg_span: ctx.get_expression_span(arguments[max_arguments]),
4101                max_arguments: max_arguments as _,
4102            }));
4103        }
4104
4105        log::debug!(
4106            "Initial overloads: {:#?}",
4107            remaining_overloads.for_debug(&ctx.module.types)
4108        );
4109
4110        for (arg_index, &arg) in arguments.iter().enumerate() {
4111            let arg_type_resolution = &ctx.typifier()[arg];
4112            let arg_inner = arg_type_resolution.inner_with(&ctx.module.types);
4113            log::debug!(
4114                "Supplying argument {arg_index} of type {:?}",
4115                arg_type_resolution.for_debug(&ctx.module.types)
4116            );
4117            let next_remaining_overloads =
4118                remaining_overloads.arg(arg_index, arg_inner, &ctx.module.types);
4119
4120            // If any argument is not a constant expression, then no overloads
4121            // that accept abstract values should be considered.
4122            // (`OverloadSet::concrete_only` is supposed to help impose this
4123            // restriction.) However, no `MathFunction` accepts a mix of
4124            // abstract and concrete arguments, so we don't need to worry
4125            // about that here.
4126
4127            log::debug!(
4128                "Remaining overloads: {:#?}",
4129                next_remaining_overloads.for_debug(&ctx.module.types)
4130            );
4131
4132            // If the set of remaining overloads is empty, then this argument's type
4133            // was unacceptable. Diagnose the problem and produce an error message.
4134            if next_remaining_overloads.is_empty() {
4135                let function = fun.to_wgsl_for_diagnostics();
4136                let call_span = span;
4137                let arg_span = ctx.get_expression_span(arg);
4138                let arg_ty = ctx.as_diagnostic_display(arg_type_resolution).to_string();
4139
4140                // Is this type *ever* permitted for the arg_index'th argument?
4141                // For example, `bool` is never permitted for `max`.
4142                let only_this_argument = overloads.arg(arg_index, arg_inner, &ctx.module.types);
4143                if only_this_argument.is_empty() {
4144                    // No overload of `fun` accepts this type as the
4145                    // arg_index'th argument. Determine the set of types that
4146                    // would ever be allowed there.
4147                    let allowed: Vec<String> = overloads
4148                        .allowed_args(arg_index, &ctx.module.to_ctx())
4149                        .iter()
4150                        .map(|ty| ctx.type_resolution_to_string(ty))
4151                        .collect();
4152
4153                    if allowed.is_empty() {
4154                        // No overload of `fun` accepts any argument at this
4155                        // index, so it's a simple case of excess arguments.
4156                        // However, since each `MathFunction`'s overloads all
4157                        // have the same arity, we should have detected this
4158                        // earlier.
4159                        unreachable!("expected all overloads to have the same arity");
4160                    }
4161
4162                    // Some overloads of `fun` do accept this many arguments,
4163                    // but none accept one of this type.
4164                    return Err(Box::new(Error::WrongArgumentType {
4165                        function,
4166                        call_span,
4167                        arg_span,
4168                        arg_index: arg_index as u32,
4169                        arg_ty,
4170                        allowed,
4171                    }));
4172                }
4173
4174                // This argument's type is accepted by some overloads---just
4175                // not those overloads that remain, given the prior arguments.
4176                // For example, `max` accepts `f32` as its second argument -
4177                // but not if the first was `i32`.
4178
4179                // Build a list of the types that would have been accepted here,
4180                // given the prior arguments.
4181                let allowed: Vec<String> = remaining_overloads
4182                    .allowed_args(arg_index, &ctx.module.to_ctx())
4183                    .iter()
4184                    .map(|ty| ctx.type_resolution_to_string(ty))
4185                    .collect();
4186
4187                // Re-run the argument list to determine which prior argument
4188                // made this one unacceptable.
4189                let mut remaining_overloads = overloads;
4190                for (prior_index, &prior_expr) in arguments.iter().enumerate() {
4191                    let prior_type_resolution = &ctx.typifier()[prior_expr];
4192                    let prior_ty = prior_type_resolution.inner_with(&ctx.module.types);
4193                    remaining_overloads =
4194                        remaining_overloads.arg(prior_index, prior_ty, &ctx.module.types);
4195                    if remaining_overloads
4196                        .arg(arg_index, arg_inner, &ctx.module.types)
4197                        .is_empty()
4198                    {
4199                        // This is the argument that killed our dreams.
4200                        let inconsistent_span = ctx.get_expression_span(arguments[prior_index]);
4201                        let inconsistent_ty =
4202                            ctx.as_diagnostic_display(prior_type_resolution).to_string();
4203
4204                        if allowed.is_empty() {
4205                            // Some overloads did accept `ty` at `arg_index`, but
4206                            // given the arguments up through `prior_expr`, we see
4207                            // no types acceptable at `arg_index`. This means that some
4208                            // overloads expect fewer arguments than others. However,
4209                            // each `MathFunction`'s overloads have the same arity, so this
4210                            // should be impossible.
4211                            unreachable!("expected all overloads to have the same arity");
4212                        }
4213
4214                        // Report `arg`'s type as inconsistent with `prior_expr`'s
4215                        return Err(Box::new(Error::InconsistentArgumentType {
4216                            function,
4217                            call_span,
4218                            arg_span,
4219                            arg_index: arg_index as u32,
4220                            arg_ty,
4221                            inconsistent_span,
4222                            inconsistent_index: prior_index as u32,
4223                            inconsistent_ty,
4224                            allowed,
4225                        }));
4226                    }
4227                }
4228                unreachable!("Failed to eliminate argument type when re-tried");
4229            }
4230            remaining_overloads = next_remaining_overloads;
4231        }
4232
4233        // Select the most preferred type rule for this call,
4234        // given the argument types supplied above.
4235        Ok(remaining_overloads.most_preferred())
4236    }
4237
4238    /// Apply automatic type conversions for a function call.
4239    ///
4240    /// Apply whatever automatic conversions are needed to pass `arguments` to
4241    /// the function overload described by `rule`. Update `arguments` to refer
4242    /// to the converted arguments.
4243    fn apply_automatic_conversions_for_call(
4244        &self,
4245        rule: &proc::Rule,
4246        arguments: &mut [Handle<ir::Expression>],
4247        ctx: &mut ExpressionContext<'source, '_, '_>,
4248    ) -> Result<'source, ()> {
4249        for (i, argument) in arguments.iter_mut().enumerate() {
4250            let goal_inner = rule.arguments[i].inner_with(&ctx.module.types);
4251            let converted = match goal_inner.scalar_for_conversions(&ctx.module.types) {
4252                Some(goal_scalar) => {
4253                    let arg_span = ctx.get_expression_span(*argument);
4254                    ctx.try_automatic_conversion_for_leaf_scalar(*argument, goal_scalar, arg_span)?
4255                }
4256                // No conversion is necessary.
4257                None => *argument,
4258            };
4259
4260            *argument = converted;
4261        }
4262
4263        Ok(())
4264    }
4265
4266    fn atomic_pointer(
4267        &mut self,
4268        expr: Handle<ast::Expression<'source>>,
4269        ctx: &mut ExpressionContext<'source, '_, '_>,
4270    ) -> Result<'source, (Handle<ir::Expression>, ir::Scalar)> {
4271        let span = ctx.ast_expressions.get_span(expr);
4272        let pointer = self.expression(expr, ctx)?;
4273
4274        match *resolve_inner!(ctx, pointer) {
4275            ir::TypeInner::Pointer { base, .. } => match ctx.module.types[base].inner {
4276                ir::TypeInner::Atomic(scalar) => Ok((pointer, scalar)),
4277                ref other => {
4278                    log::error!("Pointer type to {other:?} passed to atomic op");
4279                    Err(Box::new(Error::InvalidAtomicPointer(span)))
4280                }
4281            },
4282            ref other => {
4283                log::error!("Type {other:?} passed to atomic op");
4284                Err(Box::new(Error::InvalidAtomicPointer(span)))
4285            }
4286        }
4287    }
4288
4289    fn atomic_helper(
4290        &mut self,
4291        span: Span,
4292        fun: ir::AtomicFunction,
4293        args: &[Handle<ast::Expression<'source>>],
4294        is_statement: bool,
4295        ctx: &mut ExpressionContext<'source, '_, '_>,
4296    ) -> Result<'source, Option<Handle<ir::Expression>>> {
4297        let mut args = ctx.prepare_args(args, 2, span);
4298
4299        let (pointer, scalar) = self.atomic_pointer(args.next()?, ctx)?;
4300        let value = self.expression_with_leaf_scalar(args.next()?, scalar, ctx)?;
4301        let value_inner = resolve_inner!(ctx, value);
4302        args.finish()?;
4303
4304        // If we don't use the return value of a 64-bit `min` or `max`
4305        // operation, generate a no-result form of the `Atomic` statement, so
4306        // that we can pass validation with only `SHADER_INT64_ATOMIC_MIN_MAX`
4307        // whenever possible.
4308        let is_64_bit_min_max = matches!(fun, ir::AtomicFunction::Min | ir::AtomicFunction::Max)
4309            && matches!(
4310                *value_inner,
4311                ir::TypeInner::Scalar(ir::Scalar { width: 8, .. })
4312            );
4313        let result = if is_64_bit_min_max && is_statement {
4314            let rctx = ctx.runtime_expression_ctx(span)?;
4315            rctx.block
4316                .extend(rctx.emitter.finish(&rctx.function.expressions));
4317            rctx.emitter.start(&rctx.function.expressions);
4318            None
4319        } else {
4320            let ty = ctx.register_type(value)?;
4321            Some(ctx.interrupt_emitter(
4322                ir::Expression::AtomicResult {
4323                    ty,
4324                    comparison: false,
4325                },
4326                span,
4327            )?)
4328        };
4329        let rctx = ctx.runtime_expression_ctx(span)?;
4330        rctx.block.push(
4331            ir::Statement::Atomic {
4332                pointer,
4333                fun,
4334                value,
4335                result,
4336            },
4337            span,
4338        );
4339        Ok(result)
4340    }
4341
4342    fn texture_sample_helper(
4343        &mut self,
4344        fun: Texture,
4345        args: &[Handle<ast::Expression<'source>>],
4346        span: Span,
4347        ctx: &mut ExpressionContext<'source, '_, '_>,
4348    ) -> Result<'source, ir::Expression> {
4349        let mut args = ctx.prepare_args(args, fun.min_argument_count(), span);
4350
4351        fn get_image_and_span<'source>(
4352            lowerer: &mut Lowerer<'source, '_>,
4353            args: &mut ArgumentContext<'_, 'source>,
4354            ctx: &mut ExpressionContext<'source, '_, '_>,
4355        ) -> Result<'source, (Handle<ir::Expression>, Span)> {
4356            let image = args.next()?;
4357            let image_span = ctx.ast_expressions.get_span(image);
4358            let image = lowerer.expression_for_abstract(image, ctx)?;
4359            Ok((image, image_span))
4360        }
4361
4362        let image;
4363        let image_span;
4364        let gather;
4365        match fun {
4366            Texture::Gather => {
4367                let image_or_component = args.next()?;
4368                let image_or_component_span = ctx.ast_expressions.get_span(image_or_component);
4369                // Gathers from depth textures don't take an initial `component` argument.
4370                let lowered_image_or_component = self.expression(image_or_component, ctx)?;
4371
4372                match *resolve_inner!(ctx, lowered_image_or_component) {
4373                    ir::TypeInner::Image {
4374                        class: ir::ImageClass::Depth { .. },
4375                        ..
4376                    } => {
4377                        image = lowered_image_or_component;
4378                        image_span = image_or_component_span;
4379                        gather = Some(ir::SwizzleComponent::X);
4380                    }
4381                    _ => {
4382                        (image, image_span) = get_image_and_span(self, &mut args, ctx)?;
4383                        gather = Some(ctx.gather_component(
4384                            lowered_image_or_component,
4385                            image_or_component_span,
4386                            span,
4387                        )?);
4388                    }
4389                }
4390            }
4391            Texture::GatherCompare => {
4392                (image, image_span) = get_image_and_span(self, &mut args, ctx)?;
4393                gather = Some(ir::SwizzleComponent::X);
4394            }
4395
4396            _ => {
4397                (image, image_span) = get_image_and_span(self, &mut args, ctx)?;
4398                gather = None;
4399            }
4400        };
4401
4402        let sampler = self.expression_for_abstract(args.next()?, ctx)?;
4403
4404        let coordinate = self.expression_with_leaf_scalar(args.next()?, ir::Scalar::F32, ctx)?;
4405        let clamp_to_edge = matches!(fun, Texture::SampleBaseClampToEdge);
4406
4407        let (class, arrayed) = ctx.image_data(image, image_span)?;
4408        let array_index = arrayed
4409            .then(|| self.expression(args.next()?, ctx))
4410            .transpose()?;
4411
4412        let level;
4413        let depth_ref;
4414        match fun {
4415            Texture::Gather => {
4416                level = ir::SampleLevel::Zero;
4417                depth_ref = None;
4418            }
4419            Texture::GatherCompare => {
4420                let reference =
4421                    self.expression_with_leaf_scalar(args.next()?, ir::Scalar::F32, ctx)?;
4422                level = ir::SampleLevel::Zero;
4423                depth_ref = Some(reference);
4424            }
4425
4426            Texture::Sample => {
4427                level = ir::SampleLevel::Auto;
4428                depth_ref = None;
4429            }
4430            Texture::SampleBias => {
4431                let bias = self.expression_with_leaf_scalar(args.next()?, ir::Scalar::F32, ctx)?;
4432                level = ir::SampleLevel::Bias(bias);
4433                depth_ref = None;
4434            }
4435            Texture::SampleCompare => {
4436                let reference =
4437                    self.expression_with_leaf_scalar(args.next()?, ir::Scalar::F32, ctx)?;
4438                level = ir::SampleLevel::Auto;
4439                depth_ref = Some(reference);
4440            }
4441            Texture::SampleCompareLevel => {
4442                let reference =
4443                    self.expression_with_leaf_scalar(args.next()?, ir::Scalar::F32, ctx)?;
4444                level = ir::SampleLevel::Zero;
4445                depth_ref = Some(reference);
4446            }
4447            Texture::SampleGrad => {
4448                let x = self.expression_with_leaf_scalar(args.next()?, ir::Scalar::F32, ctx)?;
4449                let y = self.expression_with_leaf_scalar(args.next()?, ir::Scalar::F32, ctx)?;
4450                level = ir::SampleLevel::Gradient { x, y };
4451                depth_ref = None;
4452            }
4453            Texture::SampleLevel => {
4454                let exact = match class {
4455                    // When applied to depth textures, `textureSampleLevel`'s
4456                    // `level` argument is an `i32` or `u32`.
4457                    ir::ImageClass::Depth { .. } => self.expression(args.next()?, ctx)?,
4458
4459                    // When applied to other sampled types, its `level` argument
4460                    // is an `f32`.
4461                    ir::ImageClass::Sampled { .. } => {
4462                        self.expression_with_leaf_scalar(args.next()?, ir::Scalar::F32, ctx)?
4463                    }
4464
4465                    // Sampling `External` textures with a specified level isn't
4466                    // allowed, and sampling `Storage` textures isn't allowed at
4467                    // all. Let the validator report the error.
4468                    ir::ImageClass::Storage { .. } | ir::ImageClass::External => {
4469                        self.expression(args.next()?, ctx)?
4470                    }
4471                };
4472                level = ir::SampleLevel::Exact(exact);
4473                depth_ref = None;
4474            }
4475            Texture::SampleBaseClampToEdge => {
4476                level = crate::SampleLevel::Zero;
4477                depth_ref = None;
4478            }
4479        };
4480
4481        let offset = args
4482            .next()
4483            .map(|arg| self.expression_with_leaf_scalar(arg, ir::Scalar::I32, &mut ctx.as_const()))
4484            .ok()
4485            .transpose()?;
4486
4487        args.finish()?;
4488
4489        Ok(ir::Expression::ImageSample {
4490            image,
4491            sampler,
4492            gather,
4493            coordinate,
4494            array_index,
4495            offset,
4496            level,
4497            depth_ref,
4498            clamp_to_edge,
4499        })
4500    }
4501
4502    fn subgroup_operation_helper(
4503        &mut self,
4504        span: Span,
4505        op: ir::SubgroupOperation,
4506        collective_op: ir::CollectiveOperation,
4507        arguments: &[Handle<ast::Expression<'source>>],
4508        ctx: &mut ExpressionContext<'source, '_, '_>,
4509    ) -> Result<'source, Handle<ir::Expression>> {
4510        let mut args = ctx.prepare_args(arguments, 1, span);
4511
4512        let argument = self.expression(args.next()?, ctx)?;
4513        args.finish()?;
4514
4515        let ty = ctx.register_type(argument)?;
4516
4517        let result = ctx.interrupt_emitter(ir::Expression::SubgroupOperationResult { ty }, span)?;
4518        let rctx = ctx.runtime_expression_ctx(span)?;
4519        rctx.block.push(
4520            ir::Statement::SubgroupCollectiveOperation {
4521                op,
4522                collective_op,
4523                argument,
4524                result,
4525            },
4526            span,
4527        );
4528        Ok(result)
4529    }
4530
4531    fn subgroup_gather_helper(
4532        &mut self,
4533        span: Span,
4534        mode: SubgroupGather,
4535        arguments: &[Handle<ast::Expression<'source>>],
4536        ctx: &mut ExpressionContext<'source, '_, '_>,
4537    ) -> Result<'source, Handle<ir::Expression>> {
4538        let mut args = ctx.prepare_args(arguments, 2, span);
4539
4540        let argument = self.expression(args.next()?, ctx)?;
4541
4542        use SubgroupGather as Sg;
4543        let mode = if let Sg::BroadcastFirst = mode {
4544            ir::GatherMode::BroadcastFirst
4545        } else {
4546            let index = self.expression(args.next()?, ctx)?;
4547            match mode {
4548                Sg::BroadcastFirst => unreachable!(),
4549                Sg::Broadcast => ir::GatherMode::Broadcast(index),
4550                Sg::Shuffle => ir::GatherMode::Shuffle(index),
4551                Sg::ShuffleDown => ir::GatherMode::ShuffleDown(index),
4552                Sg::ShuffleUp => ir::GatherMode::ShuffleUp(index),
4553                Sg::ShuffleXor => ir::GatherMode::ShuffleXor(index),
4554                Sg::QuadBroadcast => ir::GatherMode::QuadBroadcast(index),
4555            }
4556        };
4557
4558        args.finish()?;
4559
4560        let ty = ctx.register_type(argument)?;
4561
4562        let result = ctx.interrupt_emitter(ir::Expression::SubgroupOperationResult { ty }, span)?;
4563        let rctx = ctx.runtime_expression_ctx(span)?;
4564        rctx.block.push(
4565            ir::Statement::SubgroupGather {
4566                mode,
4567                argument,
4568                result,
4569            },
4570            span,
4571        );
4572        Ok(result)
4573    }
4574
4575    fn r#struct(
4576        &mut self,
4577        s: &ast::Struct<'source>,
4578        span: Span,
4579        ctx: &mut GlobalContext<'source, '_, '_>,
4580    ) -> Result<'source, Handle<ir::Type>> {
4581        let mut offset = 0;
4582        let mut struct_alignment = proc::Alignment::ONE;
4583        let mut members = Vec::with_capacity(s.members.len());
4584
4585        let mut doc_comments: Vec<Option<Vec<String>>> = Vec::new();
4586
4587        for member in s.members.iter() {
4588            let ty = self.resolve_ast_type(&member.ty, &mut ctx.as_const())?;
4589
4590            ctx.layouter.update(ctx.module.to_ctx()).map_err(|err| {
4591                let LayoutErrorInner::TooLarge = err.inner else {
4592                    unreachable!("unexpected layout error: {err:?}");
4593                };
4594                // Since anonymous types of struct members don't get a span,
4595                // associate the error with the member. The layouter could have
4596                // failed on any type that was pending layout, but if it wasn't
4597                // the current struct member, it wasn't a struct member at all,
4598                // because we resolve struct members one-by-one.
4599                if ty == err.ty {
4600                    Box::new(Error::StructMemberTooLarge {
4601                        member_name_span: member.name.span,
4602                    })
4603                } else {
4604                    // Lots of type definitions don't get spans, so this error
4605                    // message may not be very useful.
4606                    Box::new(Error::TypeTooLarge {
4607                        span: ctx.module.types.get_span(err.ty),
4608                    })
4609                }
4610            })?;
4611
4612            let member_min_size = ctx.layouter[ty].size;
4613            let member_min_alignment = ctx.layouter[ty].alignment;
4614
4615            let member_size = if let Some(size_expr) = member.size {
4616                let (size, span) = self.const_u32(size_expr, &mut ctx.as_const())?;
4617                if let ir::TypeInner::Array {
4618                    size: ir::ArraySize::Dynamic | ir::ArraySize::Pending(_),
4619                    ..
4620                } = ctx.module.types[ty].inner
4621                {
4622                    return Err(Box::new(Error::SizeAttributeRequiresFixedFootprint(span)));
4623                }
4624                if size < member_min_size {
4625                    return Err(Box::new(Error::SizeAttributeTooLow(span, member_min_size)));
4626                } else {
4627                    size
4628                }
4629            } else {
4630                member_min_size
4631            };
4632
4633            let member_alignment = if let Some(align_expr) = member.align {
4634                let (align, span) = self.const_u32(align_expr, &mut ctx.as_const())?;
4635                if let Some(alignment) = proc::Alignment::new(align) {
4636                    if alignment < member_min_alignment {
4637                        return Err(Box::new(Error::AlignAttributeTooLow(
4638                            span,
4639                            member_min_alignment,
4640                        )));
4641                    } else {
4642                        alignment
4643                    }
4644                } else {
4645                    return Err(Box::new(Error::NonPowerOfTwoAlignAttribute(span)));
4646                }
4647            } else {
4648                member_min_alignment
4649            };
4650
4651            let binding = self.binding(&member.binding, ty, ctx)?;
4652
4653            offset = member_alignment.round_up(offset);
4654            struct_alignment = struct_alignment.max(member_alignment);
4655
4656            if !member.doc_comments.is_empty() {
4657                doc_comments.push(Some(
4658                    member.doc_comments.iter().map(|s| s.to_string()).collect(),
4659                ));
4660            }
4661            members.push(ir::StructMember {
4662                name: Some(member.name.name.to_owned()),
4663                ty,
4664                binding,
4665                offset,
4666            });
4667
4668            offset += member_size;
4669            if offset > crate::valid::MAX_TYPE_SIZE {
4670                return Err(Box::new(Error::TypeTooLarge { span }));
4671            }
4672        }
4673
4674        let size = struct_alignment.round_up(offset);
4675        let inner = ir::TypeInner::Struct {
4676            members,
4677            span: size,
4678        };
4679
4680        let handle = ctx.module.types.insert(
4681            ir::Type {
4682                name: Some(s.name.name.to_string()),
4683                inner,
4684            },
4685            span,
4686        );
4687        for (i, c) in doc_comments.drain(..).enumerate() {
4688            if let Some(comment) = c {
4689                ctx.module
4690                    .get_or_insert_default_doc_comments()
4691                    .struct_members
4692                    .insert((handle, i), comment);
4693            }
4694        }
4695        Ok(handle)
4696    }
4697
4698    fn const_u32(
4699        &mut self,
4700        expr: Handle<ast::Expression<'source>>,
4701        ctx: &mut ExpressionContext<'source, '_, '_>,
4702    ) -> Result<'source, (u32, Span)> {
4703        let span = ctx.ast_expressions.get_span(expr);
4704        let expr = self.expression(expr, ctx)?;
4705        let value = ctx
4706            .module
4707            .to_ctx()
4708            .get_const_val(expr)
4709            .map_err(|err| match err {
4710                proc::ConstValueError::NonConst | proc::ConstValueError::InvalidType => {
4711                    Error::ExpectedConstExprConcreteIntegerScalar(span)
4712                }
4713                proc::ConstValueError::Negative => Error::ExpectedNonNegative(span),
4714            })?;
4715        Ok((value, span))
4716    }
4717
4718    fn array_size(
4719        &mut self,
4720        expr: Handle<ast::Expression<'source>>,
4721        ctx: &mut ExpressionContext<'source, '_, '_>,
4722    ) -> Result<'source, ir::ArraySize> {
4723        let span = ctx.ast_expressions.get_span(expr);
4724        let const_ctx = &mut ctx.as_const();
4725        let const_expr = self.expression(expr, const_ctx);
4726        match const_expr {
4727            Ok(value) => {
4728                let len = const_ctx.get_const_val(value).map_err(|err| {
4729                    Box::new(match err {
4730                        proc::ConstValueError::NonConst | proc::ConstValueError::InvalidType => {
4731                            Error::ExpectedConstExprConcreteIntegerScalar(span)
4732                        }
4733                        proc::ConstValueError::Negative => Error::ExpectedPositiveArrayLength(span),
4734                    })
4735                })?;
4736                let size = NonZeroU32::new(len).ok_or(Error::ExpectedPositiveArrayLength(span))?;
4737                Ok(ir::ArraySize::Constant(size))
4738            }
4739            Err(err) => {
4740                // If the error is simply that `expr` was an override expression, then we
4741                // can represent that as an array length.
4742                let Error::ConstantEvaluatorError(ref ty, _) = *err else {
4743                    return Err(err);
4744                };
4745
4746                let proc::ConstantEvaluatorError::OverrideExpr = **ty else {
4747                    return Err(err);
4748                };
4749
4750                Ok(ir::ArraySize::Pending(self.array_size_override(
4751                    expr,
4752                    &mut ctx.as_global().as_override(),
4753                    span,
4754                )?))
4755            }
4756        }
4757    }
4758
4759    fn array_size_override(
4760        &mut self,
4761        size_expr: Handle<ast::Expression<'source>>,
4762        ctx: &mut ExpressionContext<'source, '_, '_>,
4763        span: Span,
4764    ) -> Result<'source, Handle<ir::Override>> {
4765        let expr = self.expression(size_expr, ctx)?;
4766        match resolve_inner!(ctx, expr).scalar_kind().ok_or(0) {
4767            Ok(ir::ScalarKind::Sint) | Ok(ir::ScalarKind::Uint) => Ok({
4768                if let ir::Expression::Override(handle) = ctx.module.global_expressions[expr] {
4769                    handle
4770                } else {
4771                    let ty = ctx.register_type(expr)?;
4772                    ctx.module.overrides.append(
4773                        ir::Override {
4774                            name: None,
4775                            id: None,
4776                            ty,
4777                            init: Some(expr),
4778                        },
4779                        span,
4780                    )
4781                }
4782            }),
4783            _ => Err(Box::new(Error::ExpectedConstExprConcreteIntegerScalar(
4784                span,
4785            ))),
4786        }
4787    }
4788
4789    /// Build the Naga equivalent of a named AST type.
4790    ///
4791    /// Return a Naga `Handle<Type>` representing the front-end type
4792    /// `handle`, which should be named `name`, if given.
4793    ///
4794    /// If `handle` refers to a type cached in [`SpecialTypes`],
4795    /// `name` may be ignored.
4796    ///
4797    /// [`SpecialTypes`]: ir::SpecialTypes
4798    fn resolve_named_ast_type(
4799        &mut self,
4800        ident: &ast::TemplateElaboratedIdent<'source>,
4801        name: String,
4802        ctx: &mut ExpressionContext<'source, '_, '_>,
4803    ) -> Result<'source, Handle<ir::Type>> {
4804        self.type_specifier(ident, ctx, Some(name))
4805    }
4806
4807    /// Return a Naga `Handle<Type>` representing the front-end type `handle`.
4808    fn resolve_ast_type(
4809        &mut self,
4810        ident: &ast::TemplateElaboratedIdent<'source>,
4811        ctx: &mut ExpressionContext<'source, '_, '_>,
4812    ) -> Result<'source, Handle<ir::Type>> {
4813        self.type_specifier(ident, ctx, None)
4814    }
4815
4816    fn binding(
4817        &mut self,
4818        binding: &Option<ast::Binding<'source>>,
4819        ty: Handle<ir::Type>,
4820        ctx: &mut GlobalContext<'source, '_, '_>,
4821    ) -> Result<'source, Option<ir::Binding>> {
4822        Ok(match *binding {
4823            Some(ast::Binding::BuiltIn(b)) => Some(ir::Binding::BuiltIn(b)),
4824            Some(ast::Binding::Location {
4825                location,
4826                interpolation,
4827                sampling,
4828                blend_src,
4829                per_primitive,
4830            }) => {
4831                let blend_src = if let Some(blend_src) = blend_src {
4832                    Some(self.const_u32(blend_src, &mut ctx.as_const())?.0)
4833                } else {
4834                    None
4835                };
4836
4837                let mut binding = ir::Binding::Location {
4838                    location: self.const_u32(location, &mut ctx.as_const())?.0,
4839                    interpolation,
4840                    sampling,
4841                    blend_src,
4842                    per_primitive,
4843                };
4844                binding.apply_default_interpolation(&ctx.module.types[ty].inner);
4845                Some(binding)
4846            }
4847            None => None,
4848        })
4849    }
4850
4851    fn ray_query_pointer(
4852        &mut self,
4853        expr: Handle<ast::Expression<'source>>,
4854        ctx: &mut ExpressionContext<'source, '_, '_>,
4855    ) -> Result<'source, Handle<ir::Expression>> {
4856        let span = ctx.ast_expressions.get_span(expr);
4857        let pointer = self.expression(expr, ctx)?;
4858
4859        match *resolve_inner!(ctx, pointer) {
4860            ir::TypeInner::Pointer { base, .. } => match ctx.module.types[base].inner {
4861                ir::TypeInner::RayQuery { .. } => Ok(pointer),
4862                ref other => {
4863                    log::error!("Pointer type to {other:?} passed to ray query op");
4864                    Err(Box::new(Error::InvalidRayQueryPointer(span)))
4865                }
4866            },
4867            ref other => {
4868                log::error!("Type {other:?} passed to ray query op");
4869                Err(Box::new(Error::InvalidRayQueryPointer(span)))
4870            }
4871        }
4872    }
4873}
4874
4875impl ir::AtomicFunction {
4876    pub fn map(word: &str) -> Option<Self> {
4877        Some(match word {
4878            "atomicAdd" => ir::AtomicFunction::Add,
4879            "atomicSub" => ir::AtomicFunction::Subtract,
4880            "atomicAnd" => ir::AtomicFunction::And,
4881            "atomicOr" => ir::AtomicFunction::InclusiveOr,
4882            "atomicXor" => ir::AtomicFunction::ExclusiveOr,
4883            "atomicMin" => ir::AtomicFunction::Min,
4884            "atomicMax" => ir::AtomicFunction::Max,
4885            "atomicExchange" => ir::AtomicFunction::Exchange { compare: None },
4886            _ => return None,
4887        })
4888    }
4889}