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                    block.extend(emitter.finish(&ctx.function.expressions));
1920                    ctx.local_table
1921                        .insert(v.handle, Declared::Runtime(Typed::Reference(handle)));
1922
1923                    match initializer {
1924                        Some(initializer) => ir::Statement::Store {
1925                            pointer: handle,
1926                            value: initializer,
1927                        },
1928                        None => return Ok(()),
1929                    }
1930                }
1931                ast::LocalDecl::Const(ref c) => {
1932                    let mut emitter = proc::Emitter::default();
1933                    emitter.start(&ctx.function.expressions);
1934
1935                    let ectx = &mut ctx.as_const(block, &mut emitter);
1936
1937                    let explicit_ty =
1938                        c.ty.as_ref()
1939                            .map(|ast| self.resolve_ast_type(ast, &mut ectx.as_const()))
1940                            .transpose()?;
1941
1942                    let (_ty, init) = self.type_and_init(
1943                        c.name,
1944                        Some(c.init),
1945                        explicit_ty,
1946                        AbstractRule::Allow,
1947                        &mut ectx.as_const(),
1948                    )?;
1949                    let init = init.expect("Local const must have init");
1950
1951                    block.extend(emitter.finish(&ctx.function.expressions));
1952                    ctx.local_table
1953                        .insert(c.handle, Declared::Const(Typed::Plain(init)));
1954                    return Ok(());
1955                }
1956            },
1957            ast::StatementKind::If {
1958                condition,
1959                ref accept,
1960                ref reject,
1961            } => {
1962                let mut emitter = proc::Emitter::default();
1963                emitter.start(&ctx.function.expressions);
1964
1965                let condition =
1966                    self.expression(condition, &mut ctx.as_expression(block, &mut emitter))?;
1967                block.extend(emitter.finish(&ctx.function.expressions));
1968
1969                let accept = self.block(accept, is_inside_loop, ctx)?;
1970                let reject = self.block(reject, is_inside_loop, ctx)?;
1971
1972                ir::Statement::If {
1973                    condition,
1974                    accept,
1975                    reject,
1976                }
1977            }
1978            ast::StatementKind::Switch {
1979                selector,
1980                ref cases,
1981            } => {
1982                let mut emitter = proc::Emitter::default();
1983                emitter.start(&ctx.function.expressions);
1984
1985                let mut ectx = ctx.as_expression(block, &mut emitter);
1986
1987                // Determine the scalar type of the selector and case expressions, find the
1988                // consensus type for automatic conversion, then convert them.
1989                let (mut exprs, spans) = core::iter::once(selector)
1990                    .chain(cases.iter().filter_map(|case| match case.value {
1991                        ast::SwitchValue::Expr(expr) => Some(expr),
1992                        ast::SwitchValue::Default => None,
1993                    }))
1994                    .enumerate()
1995                    .map(|(i, expr)| {
1996                        let span = ectx.ast_expressions.get_span(expr);
1997                        let expr = self.expression_for_abstract(expr, &mut ectx)?;
1998                        let ty = resolve_inner!(ectx, expr);
1999                        match *ty {
2000                            ir::TypeInner::Scalar(
2001                                ir::Scalar::I32 | ir::Scalar::U32 | ir::Scalar::ABSTRACT_INT,
2002                            ) => Ok((expr, span)),
2003                            _ => match i {
2004                                0 => Err(Box::new(Error::InvalidSwitchSelector { span })),
2005                                _ => Err(Box::new(Error::InvalidSwitchCase { span })),
2006                            },
2007                        }
2008                    })
2009                    .collect::<Result<(Vec<_>, Vec<_>)>>()?;
2010
2011                let mut consensus =
2012                    ectx.automatic_conversion_consensus(None, &exprs)
2013                        .map_err(|span_idx| Error::SwitchCaseTypeMismatch {
2014                            span: spans[span_idx],
2015                        })?;
2016                // Concretize to I32 if the selector and all cases were abstract
2017                if consensus == ir::Scalar::ABSTRACT_INT {
2018                    consensus = ir::Scalar::I32;
2019                }
2020                for expr in &mut exprs {
2021                    ectx.convert_to_leaf_scalar(expr, consensus)?;
2022                }
2023
2024                block.extend(emitter.finish(&ctx.function.expressions));
2025
2026                let mut exprs = exprs.into_iter();
2027                let selector = exprs
2028                    .next()
2029                    .expect("First element should be selector expression");
2030
2031                let cases = cases
2032                    .iter()
2033                    .map(|case| {
2034                        Ok(ir::SwitchCase {
2035                            value: match case.value {
2036                                ast::SwitchValue::Expr(expr) => {
2037                                    let span = ctx.ast_expressions.get_span(expr);
2038                                    let expr = exprs.next().expect(
2039                                        "Should yield expression for each SwitchValue::Expr case",
2040                                    );
2041                                    match ctx
2042                                        .module
2043                                        .to_ctx()
2044                                        .get_const_val_from(expr, &ctx.function.expressions)
2045                                    {
2046                                        Ok(ir::Literal::I32(value)) => ir::SwitchValue::I32(value),
2047                                        Ok(ir::Literal::U32(value)) => ir::SwitchValue::U32(value),
2048                                        _ => {
2049                                            return Err(Box::new(Error::InvalidSwitchCase {
2050                                                span,
2051                                            }));
2052                                        }
2053                                    }
2054                                }
2055                                ast::SwitchValue::Default => ir::SwitchValue::Default,
2056                            },
2057                            body: self.block(&case.body, is_inside_loop, ctx)?,
2058                            fall_through: case.fall_through,
2059                        })
2060                    })
2061                    .collect::<Result<_>>()?;
2062
2063                ir::Statement::Switch { selector, cases }
2064            }
2065            ast::StatementKind::Loop {
2066                ref body,
2067                ref continuing,
2068                break_if,
2069            } => {
2070                let body = self.block(body, true, ctx)?;
2071                let mut continuing = self.block(continuing, true, ctx)?;
2072
2073                let mut emitter = proc::Emitter::default();
2074                emitter.start(&ctx.function.expressions);
2075                let break_if = break_if
2076                    .map(|expr| {
2077                        self.expression(expr, &mut ctx.as_expression(&mut continuing, &mut emitter))
2078                    })
2079                    .transpose()?;
2080                continuing.extend(emitter.finish(&ctx.function.expressions));
2081
2082                ir::Statement::Loop {
2083                    body,
2084                    continuing,
2085                    break_if,
2086                }
2087            }
2088            ast::StatementKind::Break => ir::Statement::Break,
2089            ast::StatementKind::Continue => ir::Statement::Continue,
2090            ast::StatementKind::Return { value: ast_value } => {
2091                let mut emitter = proc::Emitter::default();
2092                emitter.start(&ctx.function.expressions);
2093
2094                let value;
2095                if let Some(ast_expr) = ast_value {
2096                    let result_ty = ctx.function.result.as_ref().map(|r| r.ty);
2097                    let mut ectx = ctx.as_expression(block, &mut emitter);
2098                    let expr = self.expression_for_abstract(ast_expr, &mut ectx)?;
2099
2100                    if let Some(result_ty) = result_ty {
2101                        let mut ectx = ctx.as_expression(block, &mut emitter);
2102                        let resolution = proc::TypeResolution::Handle(result_ty);
2103                        let converted =
2104                            ectx.try_automatic_conversions(expr, &resolution, Span::default())?;
2105                        value = Some(converted);
2106                    } else {
2107                        value = Some(expr);
2108                    }
2109                } else {
2110                    value = None;
2111                }
2112                block.extend(emitter.finish(&ctx.function.expressions));
2113
2114                ir::Statement::Return { value }
2115            }
2116            ast::StatementKind::Kill => ir::Statement::Kill,
2117            ast::StatementKind::Call(ref call_phrase) => {
2118                let mut emitter = proc::Emitter::default();
2119                emitter.start(&ctx.function.expressions);
2120
2121                let _ = self.call(
2122                    call_phrase,
2123                    stmt.span,
2124                    &mut ctx.as_expression(block, &mut emitter),
2125                    true,
2126                )?;
2127                block.extend(emitter.finish(&ctx.function.expressions));
2128                return Ok(());
2129            }
2130            ast::StatementKind::Assign {
2131                target: ast_target,
2132                op,
2133                value,
2134            } => {
2135                let mut emitter = proc::Emitter::default();
2136                emitter.start(&ctx.function.expressions);
2137                let target_span = ctx.ast_expressions.get_span(ast_target);
2138
2139                let mut ectx = ctx.as_expression(block, &mut emitter);
2140                let target = self.expression_for_reference(ast_target, &mut ectx)?;
2141                let target_handle = match target {
2142                    Typed::Reference(handle) => handle,
2143                    Typed::Plain(handle) => {
2144                        let ty = ctx.invalid_assignment_type(handle);
2145                        return Err(Box::new(Error::InvalidAssignment {
2146                            span: target_span,
2147                            ty,
2148                        }));
2149                    }
2150                };
2151
2152                // Usually the value needs to be converted to match the type of
2153                // the memory view you're assigning it to. The bit shift
2154                // operators are exceptions, in that the right operand is always
2155                // a `u32` or `vecN<u32>`.
2156                let target_scalar = match op {
2157                    Some(ir::BinaryOperator::ShiftLeft | ir::BinaryOperator::ShiftRight) => {
2158                        Some(ir::Scalar::U32)
2159                    }
2160                    _ => resolve_inner!(ectx, target_handle)
2161                        .pointer_automatically_convertible_scalar(&ectx.module.types),
2162                };
2163
2164                // Need to emit the LHS _before_ the RHS so that it is evaluated first.
2165                let op_assign = if let Some(op) = op {
2166                    Some((op, ectx.apply_load_rule(target)?))
2167                } else {
2168                    None
2169                };
2170
2171                let value = self.expression_for_abstract(value, &mut ectx)?;
2172                let mut value = match target_scalar {
2173                    Some(target_scalar) => ectx.try_automatic_conversion_for_leaf_scalar(
2174                        value,
2175                        target_scalar,
2176                        target_span,
2177                    )?,
2178                    None => value,
2179                };
2180
2181                let value = match op_assign {
2182                    Some((op, mut left)) => {
2183                        ectx.binary_op_splat(op, &mut left, &mut value)?;
2184                        ectx.append_expression(
2185                            ir::Expression::Binary {
2186                                op,
2187                                left,
2188                                right: value,
2189                            },
2190                            stmt.span,
2191                        )?
2192                    }
2193                    None => value,
2194                };
2195                block.extend(emitter.finish(&ctx.function.expressions));
2196
2197                ir::Statement::Store {
2198                    pointer: target_handle,
2199                    value,
2200                }
2201            }
2202            ast::StatementKind::Increment(value) | ast::StatementKind::Decrement(value) => {
2203                let mut emitter = proc::Emitter::default();
2204                emitter.start(&ctx.function.expressions);
2205
2206                let op = match stmt.kind {
2207                    ast::StatementKind::Increment(_) => ir::BinaryOperator::Add,
2208                    ast::StatementKind::Decrement(_) => ir::BinaryOperator::Subtract,
2209                    _ => unreachable!(),
2210                };
2211
2212                let value_span = ctx.ast_expressions.get_span(value);
2213                let target = self
2214                    .expression_for_reference(value, &mut ctx.as_expression(block, &mut emitter))?;
2215                let target_handle = target.ref_or(Error::BadIncrDecrReferenceType(value_span))?;
2216
2217                let mut ectx = ctx.as_expression(block, &mut emitter);
2218                let scalar = match *resolve_inner!(ectx, target_handle) {
2219                    ir::TypeInner::ValuePointer {
2220                        size: None, scalar, ..
2221                    } => scalar,
2222                    ir::TypeInner::Pointer { base, .. } => match ectx.module.types[base].inner {
2223                        ir::TypeInner::Scalar(scalar) => scalar,
2224                        _ => return Err(Box::new(Error::BadIncrDecrReferenceType(value_span))),
2225                    },
2226                    _ => return Err(Box::new(Error::BadIncrDecrReferenceType(value_span))),
2227                };
2228                let literal = match scalar.kind {
2229                    ir::ScalarKind::Sint | ir::ScalarKind::Uint => ir::Literal::one(scalar)
2230                        .ok_or(Error::BadIncrDecrReferenceType(value_span))?,
2231                    _ => return Err(Box::new(Error::BadIncrDecrReferenceType(value_span))),
2232                };
2233
2234                let right =
2235                    ectx.interrupt_emitter(ir::Expression::Literal(literal), Span::UNDEFINED)?;
2236                let rctx = ectx.runtime_expression_ctx(stmt.span)?;
2237                let left = rctx.function.expressions.append(
2238                    ir::Expression::Load {
2239                        pointer: target_handle,
2240                    },
2241                    value_span,
2242                );
2243                let value = rctx
2244                    .function
2245                    .expressions
2246                    .append(ir::Expression::Binary { op, left, right }, stmt.span);
2247                rctx.local_expression_kind_tracker
2248                    .insert(left, proc::ExpressionKind::Runtime);
2249                rctx.local_expression_kind_tracker
2250                    .insert(value, proc::ExpressionKind::Runtime);
2251
2252                block.extend(emitter.finish(&ctx.function.expressions));
2253                ir::Statement::Store {
2254                    pointer: target_handle,
2255                    value,
2256                }
2257            }
2258            ast::StatementKind::ConstAssert(condition) => {
2259                let mut emitter = proc::Emitter::default();
2260                emitter.start(&ctx.function.expressions);
2261
2262                let condition =
2263                    self.expression(condition, &mut ctx.as_const(block, &mut emitter))?;
2264
2265                let span = ctx.function.expressions.get_span(condition);
2266                match ctx
2267                    .module
2268                    .to_ctx()
2269                    .get_const_val_from(condition, &ctx.function.expressions)
2270                {
2271                    Ok(true) => Ok(()),
2272                    Ok(false) => Err(Error::ConstAssertFailed(span)),
2273                    Err(proc::ConstValueError::NonConst | proc::ConstValueError::Negative) => {
2274                        unreachable!()
2275                    }
2276                    Err(proc::ConstValueError::InvalidType) => Err(Error::NotBool(span)),
2277                }?;
2278
2279                block.extend(emitter.finish(&ctx.function.expressions));
2280
2281                return Ok(());
2282            }
2283            ast::StatementKind::Phony(expr) => {
2284                // Remembered the RHS of the phony assignment as a named expression. This
2285                // is important (1) to preserve the RHS for validation, (2) to track any
2286                // referenced globals.
2287                let mut emitter = proc::Emitter::default();
2288                emitter.start(&ctx.function.expressions);
2289
2290                let value = self.expression(expr, &mut ctx.as_expression(block, &mut emitter))?;
2291                block.extend(emitter.finish(&ctx.function.expressions));
2292                ctx.named_expressions
2293                    .insert(value, ("phony".to_string(), stmt.span));
2294                return Ok(());
2295            }
2296        };
2297
2298        block.push(out, stmt.span);
2299
2300        Ok(())
2301    }
2302
2303    /// Lower `expr` and apply the Load Rule if possible.
2304    ///
2305    /// For the time being, this concretizes abstract values, to support
2306    /// consumers that haven't been adapted to consume them yet. Consumers
2307    /// prepared for abstract values can call [`expression_for_abstract`].
2308    ///
2309    /// [`expression_for_abstract`]: Lowerer::expression_for_abstract
2310    fn expression(
2311        &mut self,
2312        expr: Handle<ast::Expression<'source>>,
2313        ctx: &mut ExpressionContext<'source, '_, '_>,
2314    ) -> Result<'source, Handle<ir::Expression>> {
2315        let expr = self.expression_for_abstract(expr, ctx)?;
2316        ctx.concretize(expr)
2317    }
2318
2319    fn expression_for_abstract(
2320        &mut self,
2321        expr: Handle<ast::Expression<'source>>,
2322        ctx: &mut ExpressionContext<'source, '_, '_>,
2323    ) -> Result<'source, Handle<ir::Expression>> {
2324        let expr = self.expression_for_reference(expr, ctx)?;
2325        ctx.apply_load_rule(expr)
2326    }
2327
2328    fn expression_with_leaf_scalar(
2329        &mut self,
2330        expr: Handle<ast::Expression<'source>>,
2331        scalar: ir::Scalar,
2332        ctx: &mut ExpressionContext<'source, '_, '_>,
2333    ) -> Result<'source, Handle<ir::Expression>> {
2334        let unconverted = self.expression_for_abstract(expr, ctx)?;
2335        ctx.try_automatic_conversion_for_leaf_scalar(unconverted, scalar, Span::default())
2336    }
2337
2338    fn expression_for_reference(
2339        &mut self,
2340        expr: Handle<ast::Expression<'source>>,
2341        ctx: &mut ExpressionContext<'source, '_, '_>,
2342    ) -> Result<'source, Typed<Handle<ir::Expression>>> {
2343        let span = ctx.ast_expressions.get_span(expr);
2344        let expr = &ctx.ast_expressions[expr];
2345
2346        let expr: Typed<ir::Expression> = match *expr {
2347            ast::Expression::Literal(literal) => {
2348                let literal = match literal {
2349                    ast::Literal::Number(Number::F16(f)) => ir::Literal::F16(f),
2350                    ast::Literal::Number(Number::F32(f)) => ir::Literal::F32(f),
2351                    ast::Literal::Number(Number::I32(i)) => ir::Literal::I32(i),
2352                    ast::Literal::Number(Number::U32(u)) => ir::Literal::U32(u),
2353                    ast::Literal::Number(Number::I64(i)) => ir::Literal::I64(i),
2354                    ast::Literal::Number(Number::U64(u)) => ir::Literal::U64(u),
2355                    ast::Literal::Number(Number::F64(f)) => ir::Literal::F64(f),
2356                    ast::Literal::Number(Number::AbstractInt(i)) => ir::Literal::AbstractInt(i),
2357                    ast::Literal::Number(Number::AbstractFloat(f)) => ir::Literal::AbstractFloat(f),
2358                    ast::Literal::Bool(b) => ir::Literal::Bool(b),
2359                };
2360                let handle = ctx.interrupt_emitter(ir::Expression::Literal(literal), span)?;
2361                return Ok(Typed::Plain(handle));
2362            }
2363            ast::Expression::Ident(ast::TemplateElaboratedIdent {
2364                ref template_list, ..
2365            }) if !template_list.is_empty() => {
2366                return Err(Box::new(Error::UnexpectedTemplate(span)))
2367            }
2368            ast::Expression::Ident(ast::TemplateElaboratedIdent {
2369                ident: ast::IdentExpr::Local(local),
2370                ..
2371            }) => {
2372                return ctx.local(&local, span);
2373            }
2374            ast::Expression::Ident(ast::TemplateElaboratedIdent {
2375                ident: ast::IdentExpr::Unresolved(name),
2376                ..
2377            }) => {
2378                let global = ctx
2379                    .globals
2380                    .get(name)
2381                    .ok_or(Error::UnknownIdent(span, name))?;
2382                let expr = match *global {
2383                    LoweredGlobalDecl::Var(handle) => {
2384                        let expr = ir::Expression::GlobalVariable(handle);
2385                        let v = &ctx.module.global_variables[handle];
2386                        match v.space {
2387                            ir::AddressSpace::Handle => Typed::Plain(expr),
2388                            _ => Typed::Reference(expr),
2389                        }
2390                    }
2391                    LoweredGlobalDecl::Const(handle) => {
2392                        Typed::Plain(ir::Expression::Constant(handle))
2393                    }
2394                    LoweredGlobalDecl::Override(handle) => {
2395                        Typed::Plain(ir::Expression::Override(handle))
2396                    }
2397                    LoweredGlobalDecl::Function { .. }
2398                    | LoweredGlobalDecl::Type(_)
2399                    | LoweredGlobalDecl::EntryPoint(_) => {
2400                        return Err(Box::new(Error::Unexpected(span, ExpectedToken::Variable)));
2401                    }
2402                };
2403
2404                return expr.try_map(|handle| ctx.interrupt_emitter(handle, span));
2405            }
2406            ast::Expression::Unary { op, expr } => {
2407                let expr = self.expression_for_abstract(expr, ctx)?;
2408                Typed::Plain(ir::Expression::Unary { op, expr })
2409            }
2410            ast::Expression::AddrOf(expr) => {
2411                // The `&` operator simply converts a reference to a pointer. And since a
2412                // reference is required, the Load Rule is not applied.
2413                match self.expression_for_reference(expr, ctx)? {
2414                    Typed::Reference(handle) => {
2415                        let expr = &ctx.runtime_expression_ctx(span)?.function.expressions[handle];
2416                        if let &ir::Expression::Access { base, .. }
2417                        | &ir::Expression::AccessIndex { base, .. } = expr
2418                        {
2419                            if let Some(ty) = resolve_inner!(ctx, base).pointer_base_type() {
2420                                if matches!(
2421                                    *ty.inner_with(&ctx.module.types),
2422                                    ir::TypeInner::Vector { .. },
2423                                ) {
2424                                    return Err(Box::new(Error::InvalidAddrOfOperand(
2425                                        ctx.get_expression_span(handle),
2426                                    )));
2427                                }
2428                            }
2429                        }
2430                        // No code is generated. We just declare the reference a pointer now.
2431                        return Ok(Typed::Plain(handle));
2432                    }
2433                    Typed::Plain(_) => {
2434                        return Err(Box::new(Error::NotReference(
2435                            "the operand of the `&` operator",
2436                            span,
2437                        )));
2438                    }
2439                }
2440            }
2441            ast::Expression::Deref(expr) => {
2442                // The pointer we dereference must be loaded.
2443                let pointer = self.expression(expr, ctx)?;
2444
2445                if resolve_inner!(ctx, pointer).pointer_space().is_none() {
2446                    return Err(Box::new(Error::NotPointer(span)));
2447                }
2448
2449                // No code is generated. We just declare the pointer a reference now.
2450                return Ok(Typed::Reference(pointer));
2451            }
2452            ast::Expression::Binary { op, left, right } => {
2453                self.binary(op, left, right, span, ctx)?
2454            }
2455            ast::Expression::Call(ref call_phrase) => {
2456                let handle = self
2457                    .call(call_phrase, span, ctx, false)?
2458                    .ok_or(Error::FunctionReturnsVoid(span))?;
2459                return Ok(Typed::Plain(handle));
2460            }
2461            ast::Expression::Index { base, index } => {
2462                let mut lowered_base = self.expression_for_reference(base, ctx)?;
2463                let index = self.expression(index, ctx)?;
2464
2465                // <https://www.w3.org/TR/WGSL/#language_extension-pointer_composite_access>
2466                // Declare pointer as reference
2467                if let Typed::Plain(handle) = lowered_base {
2468                    if resolve_inner!(ctx, handle).pointer_space().is_some() {
2469                        lowered_base = Typed::Reference(handle);
2470                    }
2471                }
2472
2473                lowered_base.try_map(|base| match ctx.get_const_val(index).ok() {
2474                    Some(index) => Ok::<_, Box<Error>>(ir::Expression::AccessIndex { base, index }),
2475                    None => {
2476                        // When an abstract array value e is indexed by an expression
2477                        // that is not a const-expression, then the array is concretized
2478                        // before the index is applied.
2479                        // https://www.w3.org/TR/WGSL/#array-access-expr
2480                        // Also applies to vectors and matrices.
2481                        let base = ctx.concretize(base)?;
2482                        Ok(ir::Expression::Access { base, index })
2483                    }
2484                })?
2485            }
2486            ast::Expression::Member { base, ref field } => {
2487                let mut lowered_base = self.expression_for_reference(base, ctx)?;
2488
2489                // <https://www.w3.org/TR/WGSL/#language_extension-pointer_composite_access>
2490                // Declare pointer as reference
2491                if let Typed::Plain(handle) = lowered_base {
2492                    if resolve_inner!(ctx, handle).pointer_space().is_some() {
2493                        lowered_base = Typed::Reference(handle);
2494                    }
2495                }
2496
2497                let temp_ty;
2498                let composite_type: &ir::TypeInner = match lowered_base {
2499                    Typed::Reference(handle) => {
2500                        temp_ty = resolve_inner!(ctx, handle)
2501                            .pointer_base_type()
2502                            .expect("In Typed::Reference(handle), handle must be a Naga pointer");
2503                        temp_ty.inner_with(&ctx.module.types)
2504                    }
2505
2506                    Typed::Plain(handle) => {
2507                        resolve_inner!(ctx, handle)
2508                    }
2509                };
2510
2511                let access = match *composite_type {
2512                    ir::TypeInner::Struct { ref members, .. } => {
2513                        let index = members
2514                            .iter()
2515                            .position(|m| m.name.as_deref() == Some(field.name))
2516                            .ok_or(Error::BadAccessor(field.span))?
2517                            as u32;
2518
2519                        lowered_base.map(|base| ir::Expression::AccessIndex { base, index })
2520                    }
2521                    ir::TypeInner::Vector { size: vec_size, .. } => {
2522                        match Components::new(field.name, field.span)? {
2523                            Components::Swizzle { size, pattern } => {
2524                                for &component in pattern[..size as usize].iter() {
2525                                    if component as u8 >= vec_size as u8 {
2526                                        return Err(Box::new(Error::BadAccessor(field.span)));
2527                                    }
2528                                }
2529                                Typed::Plain(ir::Expression::Swizzle {
2530                                    size,
2531                                    vector: ctx.apply_load_rule(lowered_base)?,
2532                                    pattern,
2533                                })
2534                            }
2535                            Components::Single(index) => {
2536                                if index >= vec_size as u32 {
2537                                    return Err(Box::new(Error::BadAccessor(field.span)));
2538                                }
2539                                lowered_base.map(|base| ir::Expression::AccessIndex { base, index })
2540                            }
2541                        }
2542                    }
2543                    _ => return Err(Box::new(Error::BadAccessor(field.span))),
2544                };
2545
2546                access
2547            }
2548        };
2549
2550        expr.try_map(|handle| ctx.append_expression(handle, span))
2551    }
2552
2553    /// Generate IR for the short-circuiting operators `&&` and `||`.
2554    ///
2555    /// `binary` has already lowered the LHS expression and resolved its type.
2556    fn logical(
2557        &mut self,
2558        op: crate::BinaryOperator,
2559        left: Handle<crate::Expression>,
2560        right: Handle<ast::Expression<'source>>,
2561        span: Span,
2562        ctx: &mut ExpressionContext<'source, '_, '_>,
2563    ) -> Result<'source, Typed<crate::Expression>> {
2564        debug_assert!(
2565            op == crate::BinaryOperator::LogicalAnd || op == crate::BinaryOperator::LogicalOr
2566        );
2567
2568        if ctx.is_runtime() {
2569            // To simulate short-circuiting behavior, we want to generate IR
2570            // like the following for `&&`. For `||`, the condition is `!_lhs`
2571            // and the else value is `true`.
2572            //
2573            // var _e0: bool;
2574            // if _lhs {
2575            //     _e0 = _rhs;
2576            // } else {
2577            //     _e0 = false;
2578            // }
2579
2580            let (condition, else_val) = if op == crate::BinaryOperator::LogicalAnd {
2581                let condition = left;
2582                let else_val = ctx.append_expression(
2583                    crate::Expression::Literal(crate::Literal::Bool(false)),
2584                    span,
2585                )?;
2586                (condition, else_val)
2587            } else {
2588                let condition = ctx.append_expression(
2589                    crate::Expression::Unary {
2590                        op: crate::UnaryOperator::LogicalNot,
2591                        expr: left,
2592                    },
2593                    span,
2594                )?;
2595                let else_val = ctx.append_expression(
2596                    crate::Expression::Literal(crate::Literal::Bool(true)),
2597                    span,
2598                )?;
2599                (condition, else_val)
2600            };
2601
2602            let bool_ty = ctx.ensure_type_exists(crate::TypeInner::Scalar(crate::Scalar::BOOL));
2603
2604            let rctx = ctx.runtime_expression_ctx(span)?;
2605            let result_var = rctx.function.local_variables.append(
2606                crate::LocalVariable {
2607                    name: None,
2608                    ty: bool_ty,
2609                    init: None,
2610                },
2611                span,
2612            );
2613            let pointer =
2614                ctx.append_expression(crate::Expression::LocalVariable(result_var), span)?;
2615
2616            let (right, mut accept) = ctx.with_nested_runtime_expression_ctx(span, |ctx| {
2617                let right = self.expression_for_abstract(right, ctx)?;
2618                ctx.grow_types(right)?;
2619                Ok(right)
2620            })?;
2621
2622            accept.push(
2623                crate::Statement::Store {
2624                    pointer,
2625                    value: right,
2626                },
2627                span,
2628            );
2629
2630            let mut reject = crate::Block::with_capacity(1);
2631            reject.push(
2632                crate::Statement::Store {
2633                    pointer,
2634                    value: else_val,
2635                },
2636                span,
2637            );
2638
2639            let rctx = ctx.runtime_expression_ctx(span)?;
2640            rctx.block.push(
2641                crate::Statement::If {
2642                    condition,
2643                    accept,
2644                    reject,
2645                },
2646                span,
2647            );
2648
2649            Ok(Typed::Reference(crate::Expression::LocalVariable(
2650                result_var,
2651            )))
2652        } else {
2653            let left_val: Option<bool> = ctx.get_const_val(left).ok();
2654
2655            if left_val.is_some_and(|left_val| {
2656                op == crate::BinaryOperator::LogicalAnd && !left_val
2657                    || op == crate::BinaryOperator::LogicalOr && left_val
2658            }) {
2659                // Short-circuit behavior: don't evaluate the RHS.
2660
2661                // TODO(https://github.com/gfx-rs/wgpu/issues/8440): We shouldn't ignore the
2662                // RHS completely, it should still be type-checked. Preserving it for type
2663                // checking is a bit tricky, because we're trying to produce an expression
2664                // for a const context, but the RHS is allowed to have things that aren't
2665                // const.
2666
2667                Ok(Typed::Plain(ctx.get(left).clone()))
2668            } else {
2669                // Evaluate the RHS and construct the entire binary expression as we
2670                // normally would. This case applies to well-formed constant logical
2671                // expressions that don't short-circuit (handled by the constant evaluator
2672                // shortly), to override expressions (handled when overrides are processed)
2673                // and to non-well-formed expressions (rejected by type checking).
2674                let right = self.expression_for_abstract(right, ctx)?;
2675                ctx.grow_types(right)?;
2676
2677                Ok(Typed::Plain(crate::Expression::Binary { op, left, right }))
2678            }
2679        }
2680    }
2681
2682    fn type_expression(
2683        &mut self,
2684        expr: Handle<ast::Expression<'source>>,
2685        ctx: &mut ExpressionContext<'source, '_, '_>,
2686    ) -> Result<'source, Handle<ir::Type>> {
2687        let span = ctx.ast_expressions.get_span(expr);
2688        let expr = &ctx.ast_expressions[expr];
2689
2690        let ident = match *expr {
2691            ast::Expression::Ident(ref ident) => ident,
2692            _ => return Err(Box::new(Error::UnexpectedExprForTypeExpression(span))),
2693        };
2694
2695        self.type_specifier(ident, ctx, None)
2696    }
2697
2698    fn type_specifier(
2699        &mut self,
2700        ident: &ast::TemplateElaboratedIdent<'source>,
2701        ctx: &mut ExpressionContext<'source, '_, '_>,
2702        alias_name: Option<String>,
2703    ) -> Result<'source, Handle<ir::Type>> {
2704        let &ast::TemplateElaboratedIdent {
2705            ref ident,
2706            ident_span,
2707            ref template_list,
2708            ..
2709        } = ident;
2710
2711        let ident = match *ident {
2712            ast::IdentExpr::Unresolved(ident) => ident,
2713            ast::IdentExpr::Local(_) => {
2714                // Since WGSL only supports module-scope type definitions and
2715                // aliases, a local identifier can't possibly refer to a type.
2716                return Err(Box::new(Error::UnexpectedExprForTypeExpression(ident_span)));
2717            }
2718        };
2719
2720        let mut tl = TemplateListIter::new(ident_span, template_list);
2721
2722        if let Some(global) = ctx.globals.get(ident) {
2723            let &LoweredGlobalDecl::Type(handle) = global else {
2724                return Err(Box::new(Error::UnexpectedExprForTypeExpression(ident_span)));
2725            };
2726
2727            // Type generators can only be predeclared, so since `ident` refers
2728            // to a module-scope declaration, the template parameter list should
2729            // be empty.
2730            tl.finish(ctx)?;
2731            return Ok(handle);
2732        }
2733
2734        // If `ident` doesn't resolve to a module-scope declaration, then it
2735        // must resolve to a predeclared type or type generator.
2736        let ty = conv::map_predeclared_type(&ctx.enable_extensions, ident_span, ident)?
2737            .ok_or_else(|| Box::new(Error::UnknownIdent(ident_span, ident)))?;
2738        let ty = self.finalize_type(ctx, ty, &mut tl, alias_name)?;
2739
2740        tl.finish(ctx)?;
2741
2742        Ok(ty)
2743    }
2744
2745    /// Construct an [`ir::Type`] from a [`conv::PredeclaredType`] and a list of
2746    /// template parameters.
2747    ///
2748    /// If we're processing a type alias, then `alias_name` is the name we
2749    /// should use in the new `ir::Type`.
2750    ///
2751    /// For example, when parsing `vec3<f32>`, the caller would pass:
2752    ///
2753    /// - for `ty`, [`TypeGenerator::Vector`], and
2754    ///
2755    /// - for `tl`, an iterator producing a single [`Expression::Ident`] representing `f32`.
2756    ///
2757    /// From those arguments this function will return a handle for the
2758    /// [`ir::Type`] representing `vec3<f32>`.
2759    ///
2760    /// [`TypeGenerator::Vector`]: conv::TypeGenerator::Vector
2761    /// [`Expression::Ident`]: crate::front::wgsl::parse::ast::Expression::Ident
2762    fn finalize_type(
2763        &mut self,
2764        ctx: &mut ExpressionContext<'source, '_, '_>,
2765        ty: conv::PredeclaredType,
2766        tl: &mut TemplateListIter<'_, 'source>,
2767        alias_name: Option<String>,
2768    ) -> Result<'source, Handle<ir::Type>> {
2769        let ty = match ty {
2770            conv::PredeclaredType::TypeInner(ty_inner) => {
2771                if let ir::TypeInner::Image {
2772                    class: ir::ImageClass::External,
2773                    ..
2774                } = ty_inner
2775                {
2776                    // Other than the WGSL backend, every backend that supports
2777                    // external textures does so by lowering them to a set of
2778                    // ordinary textures and some parameters saying how to
2779                    // sample from them. We don't know which backend will
2780                    // consume the `Module` we're building, but in case it's not
2781                    // WGSL, populate `SpecialTypes::external_texture_params`
2782                    // and `SpecialTypes::external_texture_transfer_function`
2783                    // with the types the backend will use for the parameter
2784                    // buffer.
2785                    //
2786                    // Neither of these are the type we are lowering here:
2787                    // that's an ordinary `TypeInner::Image`. But the fact we
2788                    // are lowering a `texture_external` implies the backends
2789                    // may need these additional types too.
2790                    ctx.module.generate_external_texture_types();
2791                }
2792
2793                ctx.as_global().ensure_type_exists(alias_name, ty_inner)
2794            }
2795            conv::PredeclaredType::RayDesc => ctx.module.generate_ray_desc_type(),
2796            conv::PredeclaredType::RayIntersection => ctx.module.generate_ray_intersection_type(),
2797            conv::PredeclaredType::TypeGenerator(type_generator) => {
2798                let ty_inner = match type_generator {
2799                    conv::TypeGenerator::Vector { size } => {
2800                        let (scalar, _) = tl.scalar_ty(self, ctx)?;
2801                        ir::TypeInner::Vector { size, scalar }
2802                    }
2803                    conv::TypeGenerator::Matrix { columns, rows } => {
2804                        let (scalar, span) = tl.scalar_ty(self, ctx)?;
2805                        if scalar.kind != ir::ScalarKind::Float {
2806                            return Err(Box::new(Error::BadMatrixScalarKind(span, scalar)));
2807                        }
2808                        ir::TypeInner::Matrix {
2809                            columns,
2810                            rows,
2811                            scalar,
2812                        }
2813                    }
2814                    conv::TypeGenerator::Array => {
2815                        let base = tl.ty(self, ctx)?;
2816                        let size = tl.maybe_array_size(self, ctx)?;
2817
2818                        // Determine the size of the base type, if needed.
2819                        ctx.layouter.update(ctx.module.to_ctx()).map_err(|err| {
2820                            let LayoutErrorInner::TooLarge = err.inner else {
2821                                unreachable!("unexpected layout error: {err:?}");
2822                            };
2823                            // Lots of type definitions don't get spans, so this error
2824                            // message may not be very useful.
2825                            Box::new(Error::TypeTooLarge {
2826                                span: ctx.module.types.get_span(err.ty),
2827                            })
2828                        })?;
2829                        let stride = ctx.layouter[base].to_stride();
2830
2831                        ir::TypeInner::Array { base, size, stride }
2832                    }
2833                    conv::TypeGenerator::Atomic => {
2834                        let (scalar, _) = tl.scalar_ty(self, ctx)?;
2835                        ir::TypeInner::Atomic(scalar)
2836                    }
2837                    conv::TypeGenerator::Pointer => {
2838                        let mut space = tl.address_space(ctx)?;
2839                        let base = tl.ty(self, ctx)?;
2840                        tl.maybe_access_mode(&mut space, ctx)?;
2841                        ir::TypeInner::Pointer { base, space }
2842                    }
2843                    conv::TypeGenerator::SampledTexture {
2844                        dim,
2845                        arrayed,
2846                        multi,
2847                    } => {
2848                        let (scalar, span) = tl.scalar_ty(self, ctx)?;
2849                        let ir::Scalar { kind, width } = scalar;
2850                        if width != 4 {
2851                            return Err(Box::new(Error::BadTextureSampleType { span, scalar }));
2852                        }
2853                        ir::TypeInner::Image {
2854                            dim,
2855                            arrayed,
2856                            class: ir::ImageClass::Sampled { kind, multi },
2857                        }
2858                    }
2859                    conv::TypeGenerator::StorageTexture { dim, arrayed } => {
2860                        let format = tl.storage_format(ctx)?;
2861                        let access = tl.access_mode(ctx)?;
2862                        ir::TypeInner::Image {
2863                            dim,
2864                            arrayed,
2865                            class: ir::ImageClass::Storage { format, access },
2866                        }
2867                    }
2868                    conv::TypeGenerator::BindingArray => {
2869                        let base = tl.ty(self, ctx)?;
2870                        let size = tl.maybe_array_size(self, ctx)?;
2871                        ir::TypeInner::BindingArray { base, size }
2872                    }
2873                    conv::TypeGenerator::AccelerationStructure => {
2874                        let vertex_return = tl.maybe_vertex_return(ctx)?;
2875                        ir::TypeInner::AccelerationStructure { vertex_return }
2876                    }
2877                    conv::TypeGenerator::RayQuery => {
2878                        let vertex_return = tl.maybe_vertex_return(ctx)?;
2879                        ir::TypeInner::RayQuery { vertex_return }
2880                    }
2881                    conv::TypeGenerator::CooperativeMatrix { columns, rows } => {
2882                        let (ty, span) = tl.ty_with_span(self, ctx)?;
2883                        let ir::TypeInner::Scalar(scalar) = ctx.module.types[ty].inner else {
2884                            return Err(Box::new(Error::UnsupportedCooperativeScalar(span)));
2885                        };
2886                        let role = tl.cooperative_role(ctx)?;
2887                        ir::TypeInner::CooperativeMatrix {
2888                            columns,
2889                            rows,
2890                            scalar,
2891                            role,
2892                        }
2893                    }
2894                };
2895                ctx.as_global().ensure_type_exists(alias_name, ty_inner)
2896            }
2897        };
2898        Ok(ty)
2899    }
2900
2901    fn binary(
2902        &mut self,
2903        op: ir::BinaryOperator,
2904        left: Handle<ast::Expression<'source>>,
2905        right: Handle<ast::Expression<'source>>,
2906        span: Span,
2907        ctx: &mut ExpressionContext<'source, '_, '_>,
2908    ) -> Result<'source, Typed<ir::Expression>> {
2909        if op == ir::BinaryOperator::LogicalAnd || op == ir::BinaryOperator::LogicalOr {
2910            let left = self.expression_for_abstract(left, ctx)?;
2911            ctx.grow_types(left)?;
2912
2913            if !matches!(
2914                resolve_inner!(ctx, left),
2915                &ir::TypeInner::Scalar(ir::Scalar::BOOL)
2916            ) {
2917                // Pass it through as-is, will fail validation
2918                let right = self.expression_for_abstract(right, ctx)?;
2919                ctx.grow_types(right)?;
2920                Ok(Typed::Plain(crate::Expression::Binary { op, left, right }))
2921            } else {
2922                self.logical(op, left, right, span, ctx)
2923            }
2924        } else {
2925            // Load both operands.
2926            let mut left = self.expression_for_abstract(left, ctx)?;
2927            let mut right = self.expression_for_abstract(right, ctx)?;
2928
2929            // Convert `scalar op vector` to `vector op vector` by introducing
2930            // `Splat` expressions.
2931            ctx.binary_op_splat(op, &mut left, &mut right)?;
2932
2933            // Apply automatic conversions.
2934            match op {
2935                ir::BinaryOperator::ShiftLeft | ir::BinaryOperator::ShiftRight => {
2936                    // Shift operators require the right operand to be `u32` or
2937                    // `vecN<u32>`. We can let the validator sort out vector length
2938                    // issues, but the right operand must be, or convert to, a u32 leaf
2939                    // scalar.
2940                    right =
2941                        ctx.try_automatic_conversion_for_leaf_scalar(right, ir::Scalar::U32, span)?;
2942
2943                    // Additionally, we must concretize the left operand if the right operand
2944                    // is not a const-expression.
2945                    // See https://www.w3.org/TR/WGSL/#overload-resolution-section.
2946                    //
2947                    // 2. Eliminate any candidate where one of its subexpressions resolves to
2948                    // an abstract type after feasible automatic conversions, but another of
2949                    // the candidate’s subexpressions is not a const-expression.
2950                    //
2951                    // We only have to explicitly do so for shifts as their operands may be
2952                    // of different types - for other binary ops this is achieved by finding
2953                    // the conversion consensus for both operands.
2954                    if !ctx.is_const(right) {
2955                        left = ctx.concretize(left)?;
2956                    }
2957                }
2958
2959                // All other operators follow the same pattern: reconcile the
2960                // scalar leaf types. If there's no reconciliation possible,
2961                // leave the expressions as they are: validation will report the
2962                // problem.
2963                _ => {
2964                    ctx.grow_types(left)?;
2965                    ctx.grow_types(right)?;
2966                    if let Ok(consensus_scalar) =
2967                        ctx.automatic_conversion_consensus(None, [left, right].iter())
2968                    {
2969                        ctx.convert_to_leaf_scalar(&mut left, consensus_scalar)?;
2970                        ctx.convert_to_leaf_scalar(&mut right, consensus_scalar)?;
2971                    }
2972                }
2973            }
2974
2975            Ok(Typed::Plain(ir::Expression::Binary { op, left, right }))
2976        }
2977    }
2978
2979    /// Generate Naga IR for a call to a WGSL builtin function.
2980    #[allow(clippy::too_many_arguments)]
2981    fn call_builtin<'phrase>(
2982        &mut self,
2983        function_name: &'source str,
2984        function_span: Span,
2985        arguments: &[Handle<ast::Expression<'source>>],
2986        template_params: &mut TemplateListIter<'phrase, 'source>,
2987        call_span: Span,
2988        ctx: &mut ExpressionContext<'source, '_, '_>,
2989        is_statement: bool,
2990    ) -> Result<'source, Option<(Handle<ir::Expression>, MustUse)>> {
2991        let (expr, must_use) = if let Some(fun) = conv::map_relational_fun(function_name) {
2992            let mut args = ctx.prepare_args(arguments, 1, function_span);
2993            let argument = self.expression(args.next()?, ctx)?;
2994            args.finish()?;
2995
2996            // Check for no-op all(bool) and any(bool):
2997            let argument_unmodified = matches!(
2998                fun,
2999                ir::RelationalFunction::All | ir::RelationalFunction::Any
3000            ) && {
3001                matches!(
3002                    resolve_inner!(ctx, argument),
3003                    &ir::TypeInner::Scalar(ir::Scalar {
3004                        kind: ir::ScalarKind::Bool,
3005                        ..
3006                    })
3007                )
3008            };
3009
3010            if argument_unmodified {
3011                return Ok(Some((argument, MustUse::Yes)));
3012            } else {
3013                (ir::Expression::Relational { fun, argument }, MustUse::Yes)
3014            }
3015        } else if let Some((axis, ctrl)) = conv::map_derivative(function_name) {
3016            let mut args = ctx.prepare_args(arguments, 1, function_span);
3017            let expr = self.expression(args.next()?, ctx)?;
3018            args.finish()?;
3019
3020            (
3021                ir::Expression::Derivative { axis, ctrl, expr },
3022                MustUse::Yes,
3023            )
3024        } else if let Some(fun) = conv::map_standard_fun(function_name) {
3025            (
3026                self.math_function_helper(function_span, fun, arguments, ctx)?,
3027                MustUse::Yes,
3028            )
3029        } else if let Some(fun) = Texture::map(function_name) {
3030            (
3031                self.texture_sample_helper(fun, arguments, function_span, ctx)?,
3032                MustUse::Yes,
3033            )
3034        } else if let Some((op, cop)) = conv::map_subgroup_operation(function_name) {
3035            return Ok(Some((
3036                self.subgroup_operation_helper(function_span, op, cop, arguments, ctx)?,
3037                MustUse::Yes,
3038            )));
3039        } else if let Some(mode) = SubgroupGather::map(function_name) {
3040            return Ok(Some((
3041                self.subgroup_gather_helper(function_span, mode, arguments, ctx)?,
3042                MustUse::Yes,
3043            )));
3044        } else if let Some(fun) = ir::AtomicFunction::map(function_name) {
3045            return Ok(self
3046                .atomic_helper(function_span, fun, arguments, is_statement, ctx)?
3047                .map(|result| (result, MustUse::No)));
3048        } else {
3049            match function_name {
3050                "bitcast" => {
3051                    let ty = template_params.ty(self, ctx)?;
3052
3053                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3054                    let expr = self.expression(args.next()?, ctx)?;
3055                    args.finish()?;
3056
3057                    let element_scalar = match ctx.module.types[ty].inner {
3058                        ir::TypeInner::Scalar(scalar) => scalar,
3059                        ir::TypeInner::Vector { scalar, .. } => scalar,
3060                        _ => {
3061                            let ty_resolution = resolve!(ctx, expr);
3062                            return Err(Box::new(Error::BadTypeCast {
3063                                from_type: ctx.type_resolution_to_string(ty_resolution),
3064                                span: function_span,
3065                                to_type: ctx.type_to_string(ty),
3066                            }));
3067                        }
3068                    };
3069
3070                    (
3071                        ir::Expression::As {
3072                            expr,
3073                            kind: element_scalar.kind,
3074                            convert: None,
3075                        },
3076                        MustUse::Yes,
3077                    )
3078                }
3079                "coopLoad" | "coopLoadT" => {
3080                    let row_major = function_name.ends_with("T");
3081                    let (matrix_ty, matrix_span) = template_params.ty_with_span(self, ctx)?;
3082
3083                    let mut args = ctx.prepare_args(arguments, 1, call_span);
3084                    let pointer = self.expression(args.next()?, ctx)?;
3085                    let (columns, rows, role) = match ctx.module.types[matrix_ty].inner {
3086                        ir::TypeInner::CooperativeMatrix {
3087                            columns,
3088                            rows,
3089                            role,
3090                            ..
3091                        } => (columns, rows, role),
3092                        _ => return Err(Box::new(Error::InvalidCooperativeLoadType(matrix_span))),
3093                    };
3094                    let stride = if args.total_args > 1 {
3095                        self.expression(args.next()?, ctx)?
3096                    } else {
3097                        // Infer the stride from the matrix type
3098                        let stride = if row_major {
3099                            columns as u32
3100                        } else {
3101                            rows as u32
3102                        };
3103                        ctx.append_expression(
3104                            ir::Expression::Literal(ir::Literal::U32(stride)),
3105                            Span::UNDEFINED,
3106                        )?
3107                    };
3108                    args.finish()?;
3109
3110                    (
3111                        crate::Expression::CooperativeLoad {
3112                            columns,
3113                            rows,
3114                            role,
3115                            data: crate::CooperativeData {
3116                                pointer,
3117                                stride,
3118                                row_major,
3119                            },
3120                        },
3121                        MustUse::Yes,
3122                    )
3123                }
3124                "select" => {
3125                    let mut args = ctx.prepare_args(arguments, 3, function_span);
3126
3127                    let reject_orig = args.next()?;
3128                    let accept_orig = args.next()?;
3129                    let mut values = [
3130                        self.expression_for_abstract(reject_orig, ctx)?,
3131                        self.expression_for_abstract(accept_orig, ctx)?,
3132                    ];
3133                    let condition = self.expression(args.next()?, ctx)?;
3134
3135                    args.finish()?;
3136
3137                    let diagnostic_details =
3138                        |ctx: &ExpressionContext<'_, '_, '_>,
3139                         ty_res: &proc::TypeResolution,
3140                         orig_expr| {
3141                            (
3142                                ctx.ast_expressions.get_span(orig_expr),
3143                                format!("`{}`", ctx.as_diagnostic_display(ty_res)),
3144                            )
3145                        };
3146                    for (&value, orig_value) in values.iter().zip([reject_orig, accept_orig]) {
3147                        let value_ty_res = resolve!(ctx, value);
3148                        if value_ty_res
3149                            .inner_with(&ctx.module.types)
3150                            .vector_size_and_scalar()
3151                            .is_none()
3152                        {
3153                            let (arg_span, arg_type) =
3154                                diagnostic_details(ctx, value_ty_res, orig_value);
3155                            return Err(Box::new(Error::SelectUnexpectedArgumentType {
3156                                arg_span,
3157                                arg_type,
3158                            }));
3159                        }
3160                    }
3161                    let mut consensus_scalar = ctx
3162                        .automatic_conversion_consensus(None, &values)
3163                        .map_err(|_idx| {
3164                            let [reject, accept] = values;
3165                            let [(reject_span, reject_type), (accept_span, accept_type)] =
3166                                [(reject_orig, reject), (accept_orig, accept)].map(
3167                                    |(orig_expr, expr)| {
3168                                        let ty_res = &ctx.typifier()[expr];
3169                                        diagnostic_details(ctx, ty_res, orig_expr)
3170                                    },
3171                                );
3172                            Error::SelectRejectAndAcceptHaveNoCommonType {
3173                                reject_span,
3174                                reject_type,
3175                                accept_span,
3176                                accept_type,
3177                            }
3178                        })?;
3179                    if !ctx.is_const(condition) {
3180                        consensus_scalar = consensus_scalar.concretize();
3181                    }
3182
3183                    ctx.convert_slice_to_common_leaf_scalar(&mut values, consensus_scalar)?;
3184
3185                    let [reject, accept] = values;
3186
3187                    (
3188                        ir::Expression::Select {
3189                            reject,
3190                            accept,
3191                            condition,
3192                        },
3193                        MustUse::Yes,
3194                    )
3195                }
3196                "arrayLength" => {
3197                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3198                    let expr = self.expression(args.next()?, ctx)?;
3199                    args.finish()?;
3200
3201                    (ir::Expression::ArrayLength(expr), MustUse::Yes)
3202                }
3203                "atomicLoad" => {
3204                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3205                    let (pointer, _scalar) = self.atomic_pointer(args.next()?, ctx)?;
3206                    args.finish()?;
3207
3208                    (ir::Expression::Load { pointer }, MustUse::No)
3209                }
3210                "atomicStore" => {
3211                    let mut args = ctx.prepare_args(arguments, 2, function_span);
3212                    let (pointer, scalar) = self.atomic_pointer(args.next()?, ctx)?;
3213                    let value = self.expression_with_leaf_scalar(args.next()?, scalar, ctx)?;
3214                    args.finish()?;
3215
3216                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3217                    rctx.block
3218                        .extend(rctx.emitter.finish(&rctx.function.expressions));
3219                    rctx.emitter.start(&rctx.function.expressions);
3220                    rctx.block
3221                        .push(ir::Statement::Store { pointer, value }, function_span);
3222                    return Ok(None);
3223                }
3224                "atomicCompareExchangeWeak" => {
3225                    let mut args = ctx.prepare_args(arguments, 3, function_span);
3226
3227                    let (pointer, scalar) = self.atomic_pointer(args.next()?, ctx)?;
3228
3229                    let compare = self.expression_with_leaf_scalar(args.next()?, scalar, ctx)?;
3230
3231                    let value = args.next()?;
3232                    let value_span = ctx.ast_expressions.get_span(value);
3233                    let value = self.expression_with_leaf_scalar(value, scalar, ctx)?;
3234
3235                    args.finish()?;
3236
3237                    let expression = match *resolve_inner!(ctx, value) {
3238                        ir::TypeInner::Scalar(scalar) => ir::Expression::AtomicResult {
3239                            ty: ctx.module.generate_predeclared_type(
3240                                ir::PredeclaredType::AtomicCompareExchangeWeakResult(scalar),
3241                            ),
3242                            comparison: true,
3243                        },
3244                        _ => return Err(Box::new(Error::InvalidAtomicOperandType(value_span))),
3245                    };
3246
3247                    let result = ctx.interrupt_emitter(expression, function_span)?;
3248                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3249                    rctx.block.push(
3250                        ir::Statement::Atomic {
3251                            pointer,
3252                            fun: ir::AtomicFunction::Exchange {
3253                                compare: Some(compare),
3254                            },
3255                            value,
3256                            result: Some(result),
3257                        },
3258                        function_span,
3259                    );
3260                    return Ok(Some((result, MustUse::No)));
3261                }
3262                "textureAtomicMin" | "textureAtomicMax" | "textureAtomicAdd"
3263                | "textureAtomicAnd" | "textureAtomicOr" | "textureAtomicXor" => {
3264                    let mut args = ctx.prepare_args(arguments, 3, function_span);
3265
3266                    let image = args.next()?;
3267                    let image_span = ctx.ast_expressions.get_span(image);
3268                    let image = self.expression(image, ctx)?;
3269
3270                    let coordinate = self.expression(args.next()?, ctx)?;
3271
3272                    let (_, arrayed) = ctx.image_data(image, image_span)?;
3273                    let array_index = arrayed
3274                        .then(|| {
3275                            args.min_args += 1;
3276                            self.expression(args.next()?, ctx)
3277                        })
3278                        .transpose()?;
3279
3280                    let value = self.expression(args.next()?, ctx)?;
3281
3282                    args.finish()?;
3283
3284                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3285                    rctx.block
3286                        .extend(rctx.emitter.finish(&rctx.function.expressions));
3287                    rctx.emitter.start(&rctx.function.expressions);
3288                    let stmt = ir::Statement::ImageAtomic {
3289                        image,
3290                        coordinate,
3291                        array_index,
3292                        fun: match function_name {
3293                            "textureAtomicMin" => ir::AtomicFunction::Min,
3294                            "textureAtomicMax" => ir::AtomicFunction::Max,
3295                            "textureAtomicAdd" => ir::AtomicFunction::Add,
3296                            "textureAtomicAnd" => ir::AtomicFunction::And,
3297                            "textureAtomicOr" => ir::AtomicFunction::InclusiveOr,
3298                            "textureAtomicXor" => ir::AtomicFunction::ExclusiveOr,
3299                            _ => unreachable!(),
3300                        },
3301                        value,
3302                    };
3303                    rctx.block.push(stmt, function_span);
3304                    return Ok(None);
3305                }
3306                "storageBarrier" => {
3307                    ctx.prepare_args(arguments, 0, function_span).finish()?;
3308
3309                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3310                    rctx.block.push(
3311                        ir::Statement::ControlBarrier(ir::Barrier::STORAGE),
3312                        function_span,
3313                    );
3314                    return Ok(None);
3315                }
3316                "workgroupBarrier" => {
3317                    ctx.prepare_args(arguments, 0, function_span).finish()?;
3318
3319                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3320                    rctx.block.push(
3321                        ir::Statement::ControlBarrier(ir::Barrier::WORK_GROUP),
3322                        function_span,
3323                    );
3324                    return Ok(None);
3325                }
3326                "subgroupBarrier" => {
3327                    ctx.prepare_args(arguments, 0, function_span).finish()?;
3328
3329                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3330                    rctx.block.push(
3331                        ir::Statement::ControlBarrier(ir::Barrier::SUB_GROUP),
3332                        function_span,
3333                    );
3334                    return Ok(None);
3335                }
3336                "textureBarrier" => {
3337                    ctx.prepare_args(arguments, 0, function_span).finish()?;
3338
3339                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3340                    rctx.block.push(
3341                        ir::Statement::ControlBarrier(ir::Barrier::TEXTURE),
3342                        function_span,
3343                    );
3344                    return Ok(None);
3345                }
3346                "workgroupUniformLoad" => {
3347                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3348                    let expr = args.next()?;
3349                    args.finish()?;
3350
3351                    let pointer = self.expression(expr, ctx)?;
3352                    let result_ty = match *resolve_inner!(ctx, pointer) {
3353                        ir::TypeInner::Pointer {
3354                            base,
3355                            space: ir::AddressSpace::WorkGroup,
3356                        } => match ctx.module.types[base].inner {
3357                            // Match `Expression::Load` semantics:
3358                            // loading through a pointer to `atomic<T>` produces a `T`.
3359                            ir::TypeInner::Atomic(scalar) => ctx.module.types.insert(
3360                                ir::Type {
3361                                    name: None,
3362                                    inner: ir::TypeInner::Scalar(scalar),
3363                                },
3364                                function_span,
3365                            ),
3366                            _ => base,
3367                        },
3368                        ir::TypeInner::ValuePointer {
3369                            size,
3370                            scalar,
3371                            space: ir::AddressSpace::WorkGroup,
3372                        } => ctx.module.types.insert(
3373                            ir::Type {
3374                                name: None,
3375                                inner: match size {
3376                                    Some(size) => ir::TypeInner::Vector { size, scalar },
3377                                    None => ir::TypeInner::Scalar(scalar),
3378                                },
3379                            },
3380                            function_span,
3381                        ),
3382                        _ => {
3383                            let span = ctx.ast_expressions.get_span(expr);
3384                            return Err(Box::new(Error::InvalidWorkGroupUniformLoad(span)));
3385                        }
3386                    };
3387                    let result = ctx.interrupt_emitter(
3388                        ir::Expression::WorkGroupUniformLoadResult { ty: result_ty },
3389                        function_span,
3390                    )?;
3391                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3392                    rctx.block.push(
3393                        ir::Statement::WorkGroupUniformLoad { pointer, result },
3394                        function_span,
3395                    );
3396
3397                    return Ok(Some((result, MustUse::Yes)));
3398                }
3399                "textureStore" => {
3400                    let mut args = ctx.prepare_args(arguments, 3, function_span);
3401
3402                    let image = args.next()?;
3403                    let image_span = ctx.ast_expressions.get_span(image);
3404                    let image = self.expression(image, ctx)?;
3405
3406                    let coordinate = self.expression(args.next()?, ctx)?;
3407
3408                    let (class, arrayed) = ctx.image_data(image, image_span)?;
3409                    let array_index = arrayed
3410                        .then(|| {
3411                            args.min_args += 1;
3412                            self.expression(args.next()?, ctx)
3413                        })
3414                        .transpose()?;
3415                    let scalar = if let ir::ImageClass::Storage { format, .. } = class {
3416                        format.into()
3417                    } else {
3418                        return Err(Box::new(Error::NotStorageTexture(image_span)));
3419                    };
3420
3421                    let value = self.expression_with_leaf_scalar(args.next()?, scalar, ctx)?;
3422
3423                    args.finish()?;
3424
3425                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3426                    rctx.block
3427                        .extend(rctx.emitter.finish(&rctx.function.expressions));
3428                    rctx.emitter.start(&rctx.function.expressions);
3429                    let stmt = ir::Statement::ImageStore {
3430                        image,
3431                        coordinate,
3432                        array_index,
3433                        value,
3434                    };
3435                    rctx.block.push(stmt, function_span);
3436                    return Ok(None);
3437                }
3438                "textureLoad" => {
3439                    let mut args = ctx.prepare_args(arguments, 2, function_span);
3440
3441                    let image = args.next()?;
3442                    let image_span = ctx.ast_expressions.get_span(image);
3443                    let image = self.expression(image, ctx)?;
3444
3445                    let coordinate = self.expression(args.next()?, ctx)?;
3446
3447                    let (class, arrayed) = ctx.image_data(image, image_span)?;
3448                    let array_index = arrayed
3449                        .then(|| {
3450                            args.min_args += 1;
3451                            self.expression(args.next()?, ctx)
3452                        })
3453                        .transpose()?;
3454
3455                    let level = class
3456                        .is_mipmapped()
3457                        .then(|| {
3458                            args.min_args += 1;
3459                            self.expression(args.next()?, ctx)
3460                        })
3461                        .transpose()?;
3462
3463                    let sample = class
3464                        .is_multisampled()
3465                        .then(|| self.expression(args.next()?, ctx))
3466                        .transpose()?;
3467
3468                    args.finish()?;
3469
3470                    (
3471                        ir::Expression::ImageLoad {
3472                            image,
3473                            coordinate,
3474                            array_index,
3475                            level,
3476                            sample,
3477                        },
3478                        MustUse::Yes,
3479                    )
3480                }
3481                "textureDimensions" => {
3482                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3483                    let image = self.expression(args.next()?, ctx)?;
3484                    let level = args
3485                        .next()
3486                        .map(|arg| self.expression(arg, ctx))
3487                        .ok()
3488                        .transpose()?;
3489                    args.finish()?;
3490
3491                    (
3492                        ir::Expression::ImageQuery {
3493                            image,
3494                            query: ir::ImageQuery::Size { level },
3495                        },
3496                        MustUse::Yes,
3497                    )
3498                }
3499                "textureNumLevels" => {
3500                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3501                    let image = self.expression(args.next()?, ctx)?;
3502                    args.finish()?;
3503
3504                    (
3505                        ir::Expression::ImageQuery {
3506                            image,
3507                            query: ir::ImageQuery::NumLevels,
3508                        },
3509                        MustUse::Yes,
3510                    )
3511                }
3512                "textureNumLayers" => {
3513                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3514                    let image = self.expression(args.next()?, ctx)?;
3515                    args.finish()?;
3516
3517                    (
3518                        ir::Expression::ImageQuery {
3519                            image,
3520                            query: ir::ImageQuery::NumLayers,
3521                        },
3522                        MustUse::Yes,
3523                    )
3524                }
3525                "textureNumSamples" => {
3526                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3527                    let image = self.expression(args.next()?, ctx)?;
3528                    args.finish()?;
3529
3530                    (
3531                        ir::Expression::ImageQuery {
3532                            image,
3533                            query: ir::ImageQuery::NumSamples,
3534                        },
3535                        MustUse::Yes,
3536                    )
3537                }
3538                "rayQueryInitialize" => {
3539                    let mut args = ctx.prepare_args(arguments, 3, function_span);
3540                    let query = self.ray_query_pointer(args.next()?, ctx)?;
3541                    let acceleration_structure = self.expression(args.next()?, ctx)?;
3542                    let descriptor = self.expression(args.next()?, ctx)?;
3543                    args.finish()?;
3544
3545                    let _ = ctx.module.generate_ray_desc_type();
3546                    let fun = ir::RayQueryFunction::Initialize {
3547                        acceleration_structure,
3548                        descriptor,
3549                    };
3550
3551                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3552                    rctx.block
3553                        .extend(rctx.emitter.finish(&rctx.function.expressions));
3554                    rctx.emitter.start(&rctx.function.expressions);
3555                    rctx.block
3556                        .push(ir::Statement::RayQuery { query, fun }, function_span);
3557                    return Ok(None);
3558                }
3559                "getCommittedHitVertexPositions" => {
3560                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3561                    let query = self.ray_query_pointer(args.next()?, ctx)?;
3562                    args.finish()?;
3563
3564                    let _ = ctx.module.generate_vertex_return_type();
3565
3566                    (
3567                        ir::Expression::RayQueryVertexPositions {
3568                            query,
3569                            committed: true,
3570                        },
3571                        MustUse::No,
3572                    )
3573                }
3574                "getCandidateHitVertexPositions" => {
3575                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3576                    let query = self.ray_query_pointer(args.next()?, ctx)?;
3577                    args.finish()?;
3578
3579                    let _ = ctx.module.generate_vertex_return_type();
3580
3581                    (
3582                        ir::Expression::RayQueryVertexPositions {
3583                            query,
3584                            committed: false,
3585                        },
3586                        MustUse::No,
3587                    )
3588                }
3589                "rayQueryProceed" => {
3590                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3591                    let query = self.ray_query_pointer(args.next()?, ctx)?;
3592                    args.finish()?;
3593
3594                    let result = ctx
3595                        .interrupt_emitter(ir::Expression::RayQueryProceedResult, function_span)?;
3596                    let fun = ir::RayQueryFunction::Proceed { result };
3597                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3598                    rctx.block
3599                        .push(ir::Statement::RayQuery { query, fun }, function_span);
3600                    return Ok(Some((result, MustUse::No)));
3601                }
3602                "rayQueryGenerateIntersection" => {
3603                    let mut args = ctx.prepare_args(arguments, 2, function_span);
3604                    let query = self.ray_query_pointer(args.next()?, ctx)?;
3605                    let hit_t = self.expression(args.next()?, ctx)?;
3606                    args.finish()?;
3607
3608                    let fun = ir::RayQueryFunction::GenerateIntersection { hit_t };
3609                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3610                    rctx.block
3611                        .push(ir::Statement::RayQuery { query, fun }, function_span);
3612                    return Ok(None);
3613                }
3614                "rayQueryConfirmIntersection" => {
3615                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3616                    let query = self.ray_query_pointer(args.next()?, ctx)?;
3617                    args.finish()?;
3618
3619                    let fun = ir::RayQueryFunction::ConfirmIntersection;
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                "rayQueryTerminate" => {
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::Terminate;
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                "rayQueryGetCommittedIntersection" => {
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 _ = ctx.module.generate_ray_intersection_type();
3642                    (
3643                        ir::Expression::RayQueryGetIntersection {
3644                            query,
3645                            committed: true,
3646                        },
3647                        MustUse::No,
3648                    )
3649                }
3650                "rayQueryGetCandidateIntersection" => {
3651                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3652                    let query = self.ray_query_pointer(args.next()?, ctx)?;
3653                    args.finish()?;
3654
3655                    let _ = ctx.module.generate_ray_intersection_type();
3656                    (
3657                        ir::Expression::RayQueryGetIntersection {
3658                            query,
3659                            committed: false,
3660                        },
3661                        MustUse::No,
3662                    )
3663                }
3664                "subgroupBallot" => {
3665                    let mut args = ctx.prepare_args(arguments, 0, function_span);
3666                    let predicate = if arguments.len() == 1 {
3667                        Some(self.expression(args.next()?, ctx)?)
3668                    } else {
3669                        None
3670                    };
3671                    args.finish()?;
3672
3673                    let result =
3674                        ctx.interrupt_emitter(ir::Expression::SubgroupBallotResult, function_span)?;
3675                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3676                    rctx.block.push(
3677                        ir::Statement::SubgroupBallot { result, predicate },
3678                        function_span,
3679                    );
3680                    return Ok(Some((result, MustUse::Yes)));
3681                }
3682                "quadSwapX" => {
3683                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3684
3685                    let argument = self.expression(args.next()?, ctx)?;
3686                    args.finish()?;
3687
3688                    let ty = ctx.register_type(argument)?;
3689
3690                    let result = ctx.interrupt_emitter(
3691                        crate::Expression::SubgroupOperationResult { ty },
3692                        function_span,
3693                    )?;
3694                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3695                    rctx.block.push(
3696                        crate::Statement::SubgroupGather {
3697                            mode: crate::GatherMode::QuadSwap(crate::Direction::X),
3698                            argument,
3699                            result,
3700                        },
3701                        function_span,
3702                    );
3703                    return Ok(Some((result, MustUse::Yes)));
3704                }
3705                "quadSwapY" => {
3706                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3707
3708                    let argument = self.expression(args.next()?, ctx)?;
3709                    args.finish()?;
3710
3711                    let ty = ctx.register_type(argument)?;
3712
3713                    let result = ctx.interrupt_emitter(
3714                        crate::Expression::SubgroupOperationResult { ty },
3715                        function_span,
3716                    )?;
3717                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3718                    rctx.block.push(
3719                        crate::Statement::SubgroupGather {
3720                            mode: crate::GatherMode::QuadSwap(crate::Direction::Y),
3721                            argument,
3722                            result,
3723                        },
3724                        function_span,
3725                    );
3726                    return Ok(Some((result, MustUse::Yes)));
3727                }
3728                "quadSwapDiagonal" => {
3729                    let mut args = ctx.prepare_args(arguments, 1, function_span);
3730
3731                    let argument = self.expression(args.next()?, ctx)?;
3732                    args.finish()?;
3733
3734                    let ty = ctx.register_type(argument)?;
3735
3736                    let result = ctx.interrupt_emitter(
3737                        crate::Expression::SubgroupOperationResult { ty },
3738                        function_span,
3739                    )?;
3740                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3741                    rctx.block.push(
3742                        crate::Statement::SubgroupGather {
3743                            mode: crate::GatherMode::QuadSwap(crate::Direction::Diagonal),
3744                            argument,
3745                            result,
3746                        },
3747                        function_span,
3748                    );
3749                    return Ok(Some((result, MustUse::Yes)));
3750                }
3751                "coopStore" | "coopStoreT" => {
3752                    let row_major = function_name.ends_with("T");
3753
3754                    let mut args = ctx.prepare_args(arguments, 2, function_span);
3755                    let target = self.expression(args.next()?, ctx)?;
3756                    let pointer = self.expression(args.next()?, ctx)?;
3757                    let stride = if args.total_args > 2 {
3758                        self.expression(args.next()?, ctx)?
3759                    } else {
3760                        // Infer the stride from the matrix type
3761                        let stride = match *resolve_inner!(ctx, target) {
3762                            ir::TypeInner::CooperativeMatrix { columns, rows, .. } => {
3763                                if row_major {
3764                                    columns as u32
3765                                } else {
3766                                    rows as u32
3767                                }
3768                            }
3769                            _ => 0,
3770                        };
3771                        ctx.append_expression(
3772                            ir::Expression::Literal(ir::Literal::U32(stride)),
3773                            Span::UNDEFINED,
3774                        )?
3775                    };
3776                    args.finish()?;
3777
3778                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3779                    rctx.block.push(
3780                        crate::Statement::CooperativeStore {
3781                            target,
3782                            data: crate::CooperativeData {
3783                                pointer,
3784                                stride,
3785                                row_major,
3786                            },
3787                        },
3788                        function_span,
3789                    );
3790                    return Ok(None);
3791                }
3792                "coopMultiplyAdd" => {
3793                    let mut args = ctx.prepare_args(arguments, 3, function_span);
3794                    let a = self.expression(args.next()?, ctx)?;
3795                    let b = self.expression(args.next()?, ctx)?;
3796                    let c = self.expression(args.next()?, ctx)?;
3797                    args.finish()?;
3798
3799                    (
3800                        ir::Expression::CooperativeMultiplyAdd { a, b, c },
3801                        MustUse::Yes,
3802                    )
3803                }
3804                "traceRay" => {
3805                    let mut args = ctx.prepare_args(arguments, 3, function_span);
3806                    let acceleration_structure = self.expression(args.next()?, ctx)?;
3807                    let descriptor = self.expression(args.next()?, ctx)?;
3808                    let payload = self.expression(args.next()?, ctx)?;
3809                    args.finish()?;
3810
3811                    let _ = ctx.module.generate_ray_desc_type();
3812                    let fun = ir::RayPipelineFunction::TraceRay {
3813                        acceleration_structure,
3814                        descriptor,
3815                        payload,
3816                    };
3817
3818                    let rctx = ctx.runtime_expression_ctx(function_span)?;
3819                    rctx.block
3820                        .extend(rctx.emitter.finish(&rctx.function.expressions));
3821                    rctx.emitter.start(&rctx.function.expressions);
3822                    rctx.block
3823                        .push(ir::Statement::RayPipelineFunction(fun), function_span);
3824                    return Ok(None);
3825                }
3826                _ => return Err(Box::new(Error::UnknownIdent(function_span, function_name))),
3827            }
3828        };
3829
3830        let expr = ctx.append_expression(expr, function_span)?;
3831        Ok(Some((expr, must_use)))
3832    }
3833
3834    /// Generate Naga IR for call expressions and statements, and type
3835    /// constructor expressions.
3836    ///
3837    /// The "function" being called is simply an `Ident` that we know refers to
3838    /// some module-scope definition.
3839    ///
3840    /// - If it is the name of a type, then the expression is a type constructor
3841    ///   expression: either constructing a value from components, a conversion
3842    ///   expression, or a zero value expression.
3843    ///
3844    /// - If it is the name of a function, then we're generating a [`Call`]
3845    ///   statement. We may be in the midst of generating code for an
3846    ///   expression, in which case we must generate an `Emit` statement to
3847    ///   force evaluation of the IR expressions we've generated so far, add the
3848    ///   `Call` statement to the current block, and then resume generating
3849    ///   expressions.
3850    ///
3851    /// [`Call`]: ir::Statement::Call
3852    fn call(
3853        &mut self,
3854        call_phrase: &ast::CallPhrase<'source>,
3855        span: Span,
3856        ctx: &mut ExpressionContext<'source, '_, '_>,
3857        is_statement: bool,
3858    ) -> Result<'source, Option<Handle<ir::Expression>>> {
3859        let function_name = match call_phrase.function.ident {
3860            ast::IdentExpr::Unresolved(name) => name,
3861            ast::IdentExpr::Local(_) => {
3862                return Err(Box::new(Error::CalledLocalDecl(
3863                    call_phrase.function.ident_span,
3864                )))
3865            }
3866        };
3867        let mut function_span = call_phrase.function.ident_span;
3868        function_span.subsume(call_phrase.function.template_list_span);
3869        let arguments = call_phrase.arguments.as_slice();
3870
3871        let mut tl = TemplateListIter::new(function_span, &call_phrase.function.template_list);
3872
3873        let result = match ctx.globals.get(function_name) {
3874            Some(&LoweredGlobalDecl::Type(ty)) => {
3875                // user-declared types can't make use of template lists
3876                tl.finish(ctx)?;
3877
3878                let handle =
3879                    self.construct(span, Constructor::Type(ty), function_span, arguments, ctx)?;
3880                Some((handle, MustUse::Yes))
3881            }
3882            Some(
3883                &LoweredGlobalDecl::Const(_)
3884                | &LoweredGlobalDecl::Override(_)
3885                | &LoweredGlobalDecl::Var(_),
3886            ) => {
3887                return Err(Box::new(Error::Unexpected(
3888                    function_span,
3889                    ExpectedToken::Function,
3890                )))
3891            }
3892            Some(&LoweredGlobalDecl::EntryPoint(_)) => {
3893                return Err(Box::new(Error::CalledEntryPoint(function_span)));
3894            }
3895            Some(&LoweredGlobalDecl::Function {
3896                handle: function,
3897                must_use,
3898            }) => {
3899                // user-declared functions can't make use of template lists
3900                tl.finish(ctx)?;
3901
3902                let arguments = arguments
3903                    .iter()
3904                    .enumerate()
3905                    .map(|(i, &arg)| {
3906                        // Try to convert abstract values to the known argument types
3907                        let Some(&ir::FunctionArgument {
3908                            ty: parameter_ty, ..
3909                        }) = ctx.module.functions[function].arguments.get(i)
3910                        else {
3911                            // Wrong number of arguments... just concretize the type here
3912                            // and let the validator report the error.
3913                            return self.expression(arg, ctx);
3914                        };
3915
3916                        let expr = self.expression_for_abstract(arg, ctx)?;
3917                        ctx.try_automatic_conversions(
3918                            expr,
3919                            &proc::TypeResolution::Handle(parameter_ty),
3920                            ctx.ast_expressions.get_span(arg),
3921                        )
3922                    })
3923                    .collect::<Result<Vec<_>>>()?;
3924
3925                let has_result = ctx.module.functions[function].result.is_some();
3926
3927                let rctx = ctx.runtime_expression_ctx(span)?;
3928                // we need to always do this before a fn call since all arguments need to be emitted before the fn call
3929                rctx.block
3930                    .extend(rctx.emitter.finish(&rctx.function.expressions));
3931                let result = has_result.then(|| {
3932                    let result = rctx
3933                        .function
3934                        .expressions
3935                        .append(ir::Expression::CallResult(function), span);
3936                    rctx.local_expression_kind_tracker
3937                        .insert(result, proc::ExpressionKind::Runtime);
3938                    (result, must_use.into())
3939                });
3940                rctx.emitter.start(&rctx.function.expressions);
3941                rctx.block.push(
3942                    ir::Statement::Call {
3943                        function,
3944                        arguments,
3945                        result: result.map(|(expr, _)| expr),
3946                    },
3947                    span,
3948                );
3949
3950                result
3951            }
3952            None => {
3953                // If the name refers to a predeclared type, this is a construction expression.
3954                let ty = conv::map_predeclared_type(
3955                    &ctx.enable_extensions,
3956                    function_span,
3957                    function_name,
3958                )?;
3959                if let Some(ty) = ty {
3960                    let empty_template_list = call_phrase.function.template_list.is_empty();
3961                    let constructor_ty = match ty {
3962                        conv::PredeclaredType::TypeGenerator(conv::TypeGenerator::Vector {
3963                            size,
3964                        }) if empty_template_list => Constructor::PartialVector { size },
3965                        conv::PredeclaredType::TypeGenerator(conv::TypeGenerator::Matrix {
3966                            columns,
3967                            rows,
3968                        }) if empty_template_list => Constructor::PartialMatrix { columns, rows },
3969                        conv::PredeclaredType::TypeGenerator(conv::TypeGenerator::Array)
3970                            if empty_template_list =>
3971                        {
3972                            Constructor::PartialArray
3973                        }
3974                        conv::PredeclaredType::TypeGenerator(
3975                            conv::TypeGenerator::CooperativeMatrix { .. },
3976                        ) if empty_template_list => {
3977                            return Err(Box::new(Error::UnderspecifiedCooperativeMatrix));
3978                        }
3979                        _ => Constructor::Type(self.finalize_type(ctx, ty, &mut tl, None)?),
3980                    };
3981                    tl.finish(ctx)?;
3982                    let handle =
3983                        self.construct(span, constructor_ty, function_span, arguments, ctx)?;
3984                    Some((handle, MustUse::Yes))
3985                } else {
3986                    // Otherwise, it must be a call to a builtin function.
3987                    let result = self.call_builtin(
3988                        function_name,
3989                        function_span,
3990                        arguments,
3991                        &mut tl,
3992                        span,
3993                        ctx,
3994                        is_statement,
3995                    )?;
3996                    tl.finish(ctx)?;
3997                    result
3998                }
3999            }
4000        };
4001
4002        let result_used = !is_statement;
4003        if matches!(result, Some((_, MustUse::Yes))) && !result_used {
4004            return Err(Box::new(Error::FunctionMustUseUnused(function_span)));
4005        }
4006        Ok(result.map(|(expr, _)| expr))
4007    }
4008
4009    /// Generate a Naga IR [`Math`] expression.
4010    ///
4011    /// Generate Naga IR for a call to the [`MathFunction`] `fun`, whose
4012    /// unlowered arguments are `ast_arguments`.
4013    ///
4014    /// The `span` argument should give the span of the function name in the
4015    /// call expression.
4016    ///
4017    /// [`Math`]: ir::Expression::Math
4018    /// [`MathFunction`]: ir::MathFunction
4019    fn math_function_helper(
4020        &mut self,
4021        span: Span,
4022        fun: ir::MathFunction,
4023        ast_arguments: &[Handle<ast::Expression<'source>>],
4024        ctx: &mut ExpressionContext<'source, '_, '_>,
4025    ) -> Result<'source, ir::Expression> {
4026        let mut lowered_arguments = Vec::with_capacity(ast_arguments.len());
4027        for &arg in ast_arguments {
4028            let lowered = self.expression_for_abstract(arg, ctx)?;
4029            ctx.grow_types(lowered)?;
4030            lowered_arguments.push(lowered);
4031        }
4032
4033        let fun_overloads = fun.overloads();
4034        let rule = self.resolve_overloads(span, fun, fun_overloads, &lowered_arguments, ctx)?;
4035        self.apply_automatic_conversions_for_call(&rule, &mut lowered_arguments, ctx)?;
4036
4037        // If this function returns a predeclared type, register it
4038        // in `Module::special_types`. The typifier will expect to
4039        // be able to find it there.
4040        if let proc::Conclusion::Predeclared(predeclared) = rule.conclusion {
4041            ctx.module.generate_predeclared_type(predeclared);
4042        }
4043
4044        Ok(ir::Expression::Math {
4045            fun,
4046            arg: lowered_arguments[0],
4047            arg1: lowered_arguments.get(1).cloned(),
4048            arg2: lowered_arguments.get(2).cloned(),
4049            arg3: lowered_arguments.get(3).cloned(),
4050        })
4051    }
4052
4053    /// Choose the right overload for a function call.
4054    ///
4055    /// Return a [`Rule`] representing the most preferred overload in
4056    /// `overloads` to apply to `arguments`, or return an error explaining why
4057    /// the call is not valid.
4058    ///
4059    /// Use `fun` to identify the function being called in error messages;
4060    /// `span` should be the span of the function name in the call expression.
4061    ///
4062    /// [`Rule`]: proc::Rule
4063    fn resolve_overloads<O, F>(
4064        &self,
4065        span: Span,
4066        fun: F,
4067        overloads: O,
4068        arguments: &[Handle<ir::Expression>],
4069        ctx: &ExpressionContext<'source, '_, '_>,
4070    ) -> Result<'source, proc::Rule>
4071    where
4072        O: proc::OverloadSet,
4073        F: TryToWgsl + core::fmt::Debug + Copy,
4074    {
4075        let mut remaining_overloads = overloads.clone();
4076        let min_arguments = remaining_overloads.min_arguments();
4077        let max_arguments = remaining_overloads.max_arguments();
4078        if arguments.len() < min_arguments {
4079            return Err(Box::new(Error::WrongArgumentCount {
4080                span,
4081                expected: min_arguments as u32..max_arguments as u32,
4082                found: arguments.len() as u32,
4083            }));
4084        }
4085        if arguments.len() > max_arguments {
4086            return Err(Box::new(Error::TooManyArguments {
4087                function: fun.to_wgsl_for_diagnostics(),
4088                call_span: span,
4089                arg_span: ctx.get_expression_span(arguments[max_arguments]),
4090                max_arguments: max_arguments as _,
4091            }));
4092        }
4093
4094        log::debug!(
4095            "Initial overloads: {:#?}",
4096            remaining_overloads.for_debug(&ctx.module.types)
4097        );
4098
4099        for (arg_index, &arg) in arguments.iter().enumerate() {
4100            let arg_type_resolution = &ctx.typifier()[arg];
4101            let arg_inner = arg_type_resolution.inner_with(&ctx.module.types);
4102            log::debug!(
4103                "Supplying argument {arg_index} of type {:?}",
4104                arg_type_resolution.for_debug(&ctx.module.types)
4105            );
4106            let next_remaining_overloads =
4107                remaining_overloads.arg(arg_index, arg_inner, &ctx.module.types);
4108
4109            // If any argument is not a constant expression, then no overloads
4110            // that accept abstract values should be considered.
4111            // (`OverloadSet::concrete_only` is supposed to help impose this
4112            // restriction.) However, no `MathFunction` accepts a mix of
4113            // abstract and concrete arguments, so we don't need to worry
4114            // about that here.
4115
4116            log::debug!(
4117                "Remaining overloads: {:#?}",
4118                next_remaining_overloads.for_debug(&ctx.module.types)
4119            );
4120
4121            // If the set of remaining overloads is empty, then this argument's type
4122            // was unacceptable. Diagnose the problem and produce an error message.
4123            if next_remaining_overloads.is_empty() {
4124                let function = fun.to_wgsl_for_diagnostics();
4125                let call_span = span;
4126                let arg_span = ctx.get_expression_span(arg);
4127                let arg_ty = ctx.as_diagnostic_display(arg_type_resolution).to_string();
4128
4129                // Is this type *ever* permitted for the arg_index'th argument?
4130                // For example, `bool` is never permitted for `max`.
4131                let only_this_argument = overloads.arg(arg_index, arg_inner, &ctx.module.types);
4132                if only_this_argument.is_empty() {
4133                    // No overload of `fun` accepts this type as the
4134                    // arg_index'th argument. Determine the set of types that
4135                    // would ever be allowed there.
4136                    let allowed: Vec<String> = overloads
4137                        .allowed_args(arg_index, &ctx.module.to_ctx())
4138                        .iter()
4139                        .map(|ty| ctx.type_resolution_to_string(ty))
4140                        .collect();
4141
4142                    if allowed.is_empty() {
4143                        // No overload of `fun` accepts any argument at this
4144                        // index, so it's a simple case of excess arguments.
4145                        // However, since each `MathFunction`'s overloads all
4146                        // have the same arity, we should have detected this
4147                        // earlier.
4148                        unreachable!("expected all overloads to have the same arity");
4149                    }
4150
4151                    // Some overloads of `fun` do accept this many arguments,
4152                    // but none accept one of this type.
4153                    return Err(Box::new(Error::WrongArgumentType {
4154                        function,
4155                        call_span,
4156                        arg_span,
4157                        arg_index: arg_index as u32,
4158                        arg_ty,
4159                        allowed,
4160                    }));
4161                }
4162
4163                // This argument's type is accepted by some overloads---just
4164                // not those overloads that remain, given the prior arguments.
4165                // For example, `max` accepts `f32` as its second argument -
4166                // but not if the first was `i32`.
4167
4168                // Build a list of the types that would have been accepted here,
4169                // given the prior arguments.
4170                let allowed: Vec<String> = remaining_overloads
4171                    .allowed_args(arg_index, &ctx.module.to_ctx())
4172                    .iter()
4173                    .map(|ty| ctx.type_resolution_to_string(ty))
4174                    .collect();
4175
4176                // Re-run the argument list to determine which prior argument
4177                // made this one unacceptable.
4178                let mut remaining_overloads = overloads;
4179                for (prior_index, &prior_expr) in arguments.iter().enumerate() {
4180                    let prior_type_resolution = &ctx.typifier()[prior_expr];
4181                    let prior_ty = prior_type_resolution.inner_with(&ctx.module.types);
4182                    remaining_overloads =
4183                        remaining_overloads.arg(prior_index, prior_ty, &ctx.module.types);
4184                    if remaining_overloads
4185                        .arg(arg_index, arg_inner, &ctx.module.types)
4186                        .is_empty()
4187                    {
4188                        // This is the argument that killed our dreams.
4189                        let inconsistent_span = ctx.get_expression_span(arguments[prior_index]);
4190                        let inconsistent_ty =
4191                            ctx.as_diagnostic_display(prior_type_resolution).to_string();
4192
4193                        if allowed.is_empty() {
4194                            // Some overloads did accept `ty` at `arg_index`, but
4195                            // given the arguments up through `prior_expr`, we see
4196                            // no types acceptable at `arg_index`. This means that some
4197                            // overloads expect fewer arguments than others. However,
4198                            // each `MathFunction`'s overloads have the same arity, so this
4199                            // should be impossible.
4200                            unreachable!("expected all overloads to have the same arity");
4201                        }
4202
4203                        // Report `arg`'s type as inconsistent with `prior_expr`'s
4204                        return Err(Box::new(Error::InconsistentArgumentType {
4205                            function,
4206                            call_span,
4207                            arg_span,
4208                            arg_index: arg_index as u32,
4209                            arg_ty,
4210                            inconsistent_span,
4211                            inconsistent_index: prior_index as u32,
4212                            inconsistent_ty,
4213                            allowed,
4214                        }));
4215                    }
4216                }
4217                unreachable!("Failed to eliminate argument type when re-tried");
4218            }
4219            remaining_overloads = next_remaining_overloads;
4220        }
4221
4222        // Select the most preferred type rule for this call,
4223        // given the argument types supplied above.
4224        Ok(remaining_overloads.most_preferred())
4225    }
4226
4227    /// Apply automatic type conversions for a function call.
4228    ///
4229    /// Apply whatever automatic conversions are needed to pass `arguments` to
4230    /// the function overload described by `rule`. Update `arguments` to refer
4231    /// to the converted arguments.
4232    fn apply_automatic_conversions_for_call(
4233        &self,
4234        rule: &proc::Rule,
4235        arguments: &mut [Handle<ir::Expression>],
4236        ctx: &mut ExpressionContext<'source, '_, '_>,
4237    ) -> Result<'source, ()> {
4238        for (i, argument) in arguments.iter_mut().enumerate() {
4239            let goal_inner = rule.arguments[i].inner_with(&ctx.module.types);
4240            let converted = match goal_inner.scalar_for_conversions(&ctx.module.types) {
4241                Some(goal_scalar) => {
4242                    let arg_span = ctx.get_expression_span(*argument);
4243                    ctx.try_automatic_conversion_for_leaf_scalar(*argument, goal_scalar, arg_span)?
4244                }
4245                // No conversion is necessary.
4246                None => *argument,
4247            };
4248
4249            *argument = converted;
4250        }
4251
4252        Ok(())
4253    }
4254
4255    fn atomic_pointer(
4256        &mut self,
4257        expr: Handle<ast::Expression<'source>>,
4258        ctx: &mut ExpressionContext<'source, '_, '_>,
4259    ) -> Result<'source, (Handle<ir::Expression>, ir::Scalar)> {
4260        let span = ctx.ast_expressions.get_span(expr);
4261        let pointer = self.expression(expr, ctx)?;
4262
4263        match *resolve_inner!(ctx, pointer) {
4264            ir::TypeInner::Pointer { base, .. } => match ctx.module.types[base].inner {
4265                ir::TypeInner::Atomic(scalar) => Ok((pointer, scalar)),
4266                ref other => {
4267                    log::error!("Pointer type to {other:?} passed to atomic op");
4268                    Err(Box::new(Error::InvalidAtomicPointer(span)))
4269                }
4270            },
4271            ref other => {
4272                log::error!("Type {other:?} passed to atomic op");
4273                Err(Box::new(Error::InvalidAtomicPointer(span)))
4274            }
4275        }
4276    }
4277
4278    fn atomic_helper(
4279        &mut self,
4280        span: Span,
4281        fun: ir::AtomicFunction,
4282        args: &[Handle<ast::Expression<'source>>],
4283        is_statement: bool,
4284        ctx: &mut ExpressionContext<'source, '_, '_>,
4285    ) -> Result<'source, Option<Handle<ir::Expression>>> {
4286        let mut args = ctx.prepare_args(args, 2, span);
4287
4288        let (pointer, scalar) = self.atomic_pointer(args.next()?, ctx)?;
4289        let value = self.expression_with_leaf_scalar(args.next()?, scalar, ctx)?;
4290        let value_inner = resolve_inner!(ctx, value);
4291        args.finish()?;
4292
4293        // If we don't use the return value of a 64-bit `min` or `max`
4294        // operation, generate a no-result form of the `Atomic` statement, so
4295        // that we can pass validation with only `SHADER_INT64_ATOMIC_MIN_MAX`
4296        // whenever possible.
4297        let is_64_bit_min_max = matches!(fun, ir::AtomicFunction::Min | ir::AtomicFunction::Max)
4298            && matches!(
4299                *value_inner,
4300                ir::TypeInner::Scalar(ir::Scalar { width: 8, .. })
4301            );
4302        let result = if is_64_bit_min_max && is_statement {
4303            let rctx = ctx.runtime_expression_ctx(span)?;
4304            rctx.block
4305                .extend(rctx.emitter.finish(&rctx.function.expressions));
4306            rctx.emitter.start(&rctx.function.expressions);
4307            None
4308        } else {
4309            let ty = ctx.register_type(value)?;
4310            Some(ctx.interrupt_emitter(
4311                ir::Expression::AtomicResult {
4312                    ty,
4313                    comparison: false,
4314                },
4315                span,
4316            )?)
4317        };
4318        let rctx = ctx.runtime_expression_ctx(span)?;
4319        rctx.block.push(
4320            ir::Statement::Atomic {
4321                pointer,
4322                fun,
4323                value,
4324                result,
4325            },
4326            span,
4327        );
4328        Ok(result)
4329    }
4330
4331    fn texture_sample_helper(
4332        &mut self,
4333        fun: Texture,
4334        args: &[Handle<ast::Expression<'source>>],
4335        span: Span,
4336        ctx: &mut ExpressionContext<'source, '_, '_>,
4337    ) -> Result<'source, ir::Expression> {
4338        let mut args = ctx.prepare_args(args, fun.min_argument_count(), span);
4339
4340        fn get_image_and_span<'source>(
4341            lowerer: &mut Lowerer<'source, '_>,
4342            args: &mut ArgumentContext<'_, 'source>,
4343            ctx: &mut ExpressionContext<'source, '_, '_>,
4344        ) -> Result<'source, (Handle<ir::Expression>, Span)> {
4345            let image = args.next()?;
4346            let image_span = ctx.ast_expressions.get_span(image);
4347            let image = lowerer.expression_for_abstract(image, ctx)?;
4348            Ok((image, image_span))
4349        }
4350
4351        let image;
4352        let image_span;
4353        let gather;
4354        match fun {
4355            Texture::Gather => {
4356                let image_or_component = args.next()?;
4357                let image_or_component_span = ctx.ast_expressions.get_span(image_or_component);
4358                // Gathers from depth textures don't take an initial `component` argument.
4359                let lowered_image_or_component = self.expression(image_or_component, ctx)?;
4360
4361                match *resolve_inner!(ctx, lowered_image_or_component) {
4362                    ir::TypeInner::Image {
4363                        class: ir::ImageClass::Depth { .. },
4364                        ..
4365                    } => {
4366                        image = lowered_image_or_component;
4367                        image_span = image_or_component_span;
4368                        gather = Some(ir::SwizzleComponent::X);
4369                    }
4370                    _ => {
4371                        (image, image_span) = get_image_and_span(self, &mut args, ctx)?;
4372                        gather = Some(ctx.gather_component(
4373                            lowered_image_or_component,
4374                            image_or_component_span,
4375                            span,
4376                        )?);
4377                    }
4378                }
4379            }
4380            Texture::GatherCompare => {
4381                (image, image_span) = get_image_and_span(self, &mut args, ctx)?;
4382                gather = Some(ir::SwizzleComponent::X);
4383            }
4384
4385            _ => {
4386                (image, image_span) = get_image_and_span(self, &mut args, ctx)?;
4387                gather = None;
4388            }
4389        };
4390
4391        let sampler = self.expression_for_abstract(args.next()?, ctx)?;
4392
4393        let coordinate = self.expression_with_leaf_scalar(args.next()?, ir::Scalar::F32, ctx)?;
4394        let clamp_to_edge = matches!(fun, Texture::SampleBaseClampToEdge);
4395
4396        let (class, arrayed) = ctx.image_data(image, image_span)?;
4397        let array_index = arrayed
4398            .then(|| self.expression(args.next()?, ctx))
4399            .transpose()?;
4400
4401        let level;
4402        let depth_ref;
4403        match fun {
4404            Texture::Gather => {
4405                level = ir::SampleLevel::Zero;
4406                depth_ref = None;
4407            }
4408            Texture::GatherCompare => {
4409                let reference =
4410                    self.expression_with_leaf_scalar(args.next()?, ir::Scalar::F32, ctx)?;
4411                level = ir::SampleLevel::Zero;
4412                depth_ref = Some(reference);
4413            }
4414
4415            Texture::Sample => {
4416                level = ir::SampleLevel::Auto;
4417                depth_ref = None;
4418            }
4419            Texture::SampleBias => {
4420                let bias = self.expression_with_leaf_scalar(args.next()?, ir::Scalar::F32, ctx)?;
4421                level = ir::SampleLevel::Bias(bias);
4422                depth_ref = None;
4423            }
4424            Texture::SampleCompare => {
4425                let reference =
4426                    self.expression_with_leaf_scalar(args.next()?, ir::Scalar::F32, ctx)?;
4427                level = ir::SampleLevel::Auto;
4428                depth_ref = Some(reference);
4429            }
4430            Texture::SampleCompareLevel => {
4431                let reference =
4432                    self.expression_with_leaf_scalar(args.next()?, ir::Scalar::F32, ctx)?;
4433                level = ir::SampleLevel::Zero;
4434                depth_ref = Some(reference);
4435            }
4436            Texture::SampleGrad => {
4437                let x = self.expression_with_leaf_scalar(args.next()?, ir::Scalar::F32, ctx)?;
4438                let y = self.expression_with_leaf_scalar(args.next()?, ir::Scalar::F32, ctx)?;
4439                level = ir::SampleLevel::Gradient { x, y };
4440                depth_ref = None;
4441            }
4442            Texture::SampleLevel => {
4443                let exact = match class {
4444                    // When applied to depth textures, `textureSampleLevel`'s
4445                    // `level` argument is an `i32` or `u32`.
4446                    ir::ImageClass::Depth { .. } => self.expression(args.next()?, ctx)?,
4447
4448                    // When applied to other sampled types, its `level` argument
4449                    // is an `f32`.
4450                    ir::ImageClass::Sampled { .. } => {
4451                        self.expression_with_leaf_scalar(args.next()?, ir::Scalar::F32, ctx)?
4452                    }
4453
4454                    // Sampling `External` textures with a specified level isn't
4455                    // allowed, and sampling `Storage` textures isn't allowed at
4456                    // all. Let the validator report the error.
4457                    ir::ImageClass::Storage { .. } | ir::ImageClass::External => {
4458                        self.expression(args.next()?, ctx)?
4459                    }
4460                };
4461                level = ir::SampleLevel::Exact(exact);
4462                depth_ref = None;
4463            }
4464            Texture::SampleBaseClampToEdge => {
4465                level = crate::SampleLevel::Zero;
4466                depth_ref = None;
4467            }
4468        };
4469
4470        let offset = args
4471            .next()
4472            .map(|arg| self.expression_with_leaf_scalar(arg, ir::Scalar::I32, &mut ctx.as_const()))
4473            .ok()
4474            .transpose()?;
4475
4476        args.finish()?;
4477
4478        Ok(ir::Expression::ImageSample {
4479            image,
4480            sampler,
4481            gather,
4482            coordinate,
4483            array_index,
4484            offset,
4485            level,
4486            depth_ref,
4487            clamp_to_edge,
4488        })
4489    }
4490
4491    fn subgroup_operation_helper(
4492        &mut self,
4493        span: Span,
4494        op: ir::SubgroupOperation,
4495        collective_op: ir::CollectiveOperation,
4496        arguments: &[Handle<ast::Expression<'source>>],
4497        ctx: &mut ExpressionContext<'source, '_, '_>,
4498    ) -> Result<'source, Handle<ir::Expression>> {
4499        let mut args = ctx.prepare_args(arguments, 1, span);
4500
4501        let argument = self.expression(args.next()?, ctx)?;
4502        args.finish()?;
4503
4504        let ty = ctx.register_type(argument)?;
4505
4506        let result = ctx.interrupt_emitter(ir::Expression::SubgroupOperationResult { ty }, span)?;
4507        let rctx = ctx.runtime_expression_ctx(span)?;
4508        rctx.block.push(
4509            ir::Statement::SubgroupCollectiveOperation {
4510                op,
4511                collective_op,
4512                argument,
4513                result,
4514            },
4515            span,
4516        );
4517        Ok(result)
4518    }
4519
4520    fn subgroup_gather_helper(
4521        &mut self,
4522        span: Span,
4523        mode: SubgroupGather,
4524        arguments: &[Handle<ast::Expression<'source>>],
4525        ctx: &mut ExpressionContext<'source, '_, '_>,
4526    ) -> Result<'source, Handle<ir::Expression>> {
4527        let mut args = ctx.prepare_args(arguments, 2, span);
4528
4529        let argument = self.expression(args.next()?, ctx)?;
4530
4531        use SubgroupGather as Sg;
4532        let mode = if let Sg::BroadcastFirst = mode {
4533            ir::GatherMode::BroadcastFirst
4534        } else {
4535            let index = self.expression(args.next()?, ctx)?;
4536            match mode {
4537                Sg::BroadcastFirst => unreachable!(),
4538                Sg::Broadcast => ir::GatherMode::Broadcast(index),
4539                Sg::Shuffle => ir::GatherMode::Shuffle(index),
4540                Sg::ShuffleDown => ir::GatherMode::ShuffleDown(index),
4541                Sg::ShuffleUp => ir::GatherMode::ShuffleUp(index),
4542                Sg::ShuffleXor => ir::GatherMode::ShuffleXor(index),
4543                Sg::QuadBroadcast => ir::GatherMode::QuadBroadcast(index),
4544            }
4545        };
4546
4547        args.finish()?;
4548
4549        let ty = ctx.register_type(argument)?;
4550
4551        let result = ctx.interrupt_emitter(ir::Expression::SubgroupOperationResult { ty }, span)?;
4552        let rctx = ctx.runtime_expression_ctx(span)?;
4553        rctx.block.push(
4554            ir::Statement::SubgroupGather {
4555                mode,
4556                argument,
4557                result,
4558            },
4559            span,
4560        );
4561        Ok(result)
4562    }
4563
4564    fn r#struct(
4565        &mut self,
4566        s: &ast::Struct<'source>,
4567        span: Span,
4568        ctx: &mut GlobalContext<'source, '_, '_>,
4569    ) -> Result<'source, Handle<ir::Type>> {
4570        let mut offset = 0;
4571        let mut struct_alignment = proc::Alignment::ONE;
4572        let mut members = Vec::with_capacity(s.members.len());
4573
4574        let mut doc_comments: Vec<Option<Vec<String>>> = Vec::new();
4575
4576        for member in s.members.iter() {
4577            let ty = self.resolve_ast_type(&member.ty, &mut ctx.as_const())?;
4578
4579            ctx.layouter.update(ctx.module.to_ctx()).map_err(|err| {
4580                let LayoutErrorInner::TooLarge = err.inner else {
4581                    unreachable!("unexpected layout error: {err:?}");
4582                };
4583                // Since anonymous types of struct members don't get a span,
4584                // associate the error with the member. The layouter could have
4585                // failed on any type that was pending layout, but if it wasn't
4586                // the current struct member, it wasn't a struct member at all,
4587                // because we resolve struct members one-by-one.
4588                if ty == err.ty {
4589                    Box::new(Error::StructMemberTooLarge {
4590                        member_name_span: member.name.span,
4591                    })
4592                } else {
4593                    // Lots of type definitions don't get spans, so this error
4594                    // message may not be very useful.
4595                    Box::new(Error::TypeTooLarge {
4596                        span: ctx.module.types.get_span(err.ty),
4597                    })
4598                }
4599            })?;
4600
4601            let member_min_size = ctx.layouter[ty].size;
4602            let member_min_alignment = ctx.layouter[ty].alignment;
4603
4604            let member_size = if let Some(size_expr) = member.size {
4605                let (size, span) = self.const_u32(size_expr, &mut ctx.as_const())?;
4606                if size < member_min_size {
4607                    return Err(Box::new(Error::SizeAttributeTooLow(span, member_min_size)));
4608                } else {
4609                    size
4610                }
4611            } else {
4612                member_min_size
4613            };
4614
4615            let member_alignment = if let Some(align_expr) = member.align {
4616                let (align, span) = self.const_u32(align_expr, &mut ctx.as_const())?;
4617                if let Some(alignment) = proc::Alignment::new(align) {
4618                    if alignment < member_min_alignment {
4619                        return Err(Box::new(Error::AlignAttributeTooLow(
4620                            span,
4621                            member_min_alignment,
4622                        )));
4623                    } else {
4624                        alignment
4625                    }
4626                } else {
4627                    return Err(Box::new(Error::NonPowerOfTwoAlignAttribute(span)));
4628                }
4629            } else {
4630                member_min_alignment
4631            };
4632
4633            let binding = self.binding(&member.binding, ty, ctx)?;
4634
4635            offset = member_alignment.round_up(offset);
4636            struct_alignment = struct_alignment.max(member_alignment);
4637
4638            if !member.doc_comments.is_empty() {
4639                doc_comments.push(Some(
4640                    member.doc_comments.iter().map(|s| s.to_string()).collect(),
4641                ));
4642            }
4643            members.push(ir::StructMember {
4644                name: Some(member.name.name.to_owned()),
4645                ty,
4646                binding,
4647                offset,
4648            });
4649
4650            offset += member_size;
4651            if offset > crate::valid::MAX_TYPE_SIZE {
4652                return Err(Box::new(Error::TypeTooLarge { span }));
4653            }
4654        }
4655
4656        let size = struct_alignment.round_up(offset);
4657        let inner = ir::TypeInner::Struct {
4658            members,
4659            span: size,
4660        };
4661
4662        let handle = ctx.module.types.insert(
4663            ir::Type {
4664                name: Some(s.name.name.to_string()),
4665                inner,
4666            },
4667            span,
4668        );
4669        for (i, c) in doc_comments.drain(..).enumerate() {
4670            if let Some(comment) = c {
4671                ctx.module
4672                    .get_or_insert_default_doc_comments()
4673                    .struct_members
4674                    .insert((handle, i), comment);
4675            }
4676        }
4677        Ok(handle)
4678    }
4679
4680    fn const_u32(
4681        &mut self,
4682        expr: Handle<ast::Expression<'source>>,
4683        ctx: &mut ExpressionContext<'source, '_, '_>,
4684    ) -> Result<'source, (u32, Span)> {
4685        let span = ctx.ast_expressions.get_span(expr);
4686        let expr = self.expression(expr, ctx)?;
4687        let value = ctx
4688            .module
4689            .to_ctx()
4690            .get_const_val(expr)
4691            .map_err(|err| match err {
4692                proc::ConstValueError::NonConst | proc::ConstValueError::InvalidType => {
4693                    Error::ExpectedConstExprConcreteIntegerScalar(span)
4694                }
4695                proc::ConstValueError::Negative => Error::ExpectedNonNegative(span),
4696            })?;
4697        Ok((value, span))
4698    }
4699
4700    fn array_size(
4701        &mut self,
4702        expr: Handle<ast::Expression<'source>>,
4703        ctx: &mut ExpressionContext<'source, '_, '_>,
4704    ) -> Result<'source, ir::ArraySize> {
4705        let span = ctx.ast_expressions.get_span(expr);
4706        let const_ctx = &mut ctx.as_const();
4707        let const_expr = self.expression(expr, const_ctx);
4708        match const_expr {
4709            Ok(value) => {
4710                let len = const_ctx.get_const_val(value).map_err(|err| {
4711                    Box::new(match err {
4712                        proc::ConstValueError::NonConst | proc::ConstValueError::InvalidType => {
4713                            Error::ExpectedConstExprConcreteIntegerScalar(span)
4714                        }
4715                        proc::ConstValueError::Negative => Error::ExpectedPositiveArrayLength(span),
4716                    })
4717                })?;
4718                let size = NonZeroU32::new(len).ok_or(Error::ExpectedPositiveArrayLength(span))?;
4719                Ok(ir::ArraySize::Constant(size))
4720            }
4721            Err(err) => {
4722                // If the error is simply that `expr` was an override expression, then we
4723                // can represent that as an array length.
4724                let Error::ConstantEvaluatorError(ref ty, _) = *err else {
4725                    return Err(err);
4726                };
4727
4728                let proc::ConstantEvaluatorError::OverrideExpr = **ty else {
4729                    return Err(err);
4730                };
4731
4732                Ok(ir::ArraySize::Pending(self.array_size_override(
4733                    expr,
4734                    &mut ctx.as_global().as_override(),
4735                    span,
4736                )?))
4737            }
4738        }
4739    }
4740
4741    fn array_size_override(
4742        &mut self,
4743        size_expr: Handle<ast::Expression<'source>>,
4744        ctx: &mut ExpressionContext<'source, '_, '_>,
4745        span: Span,
4746    ) -> Result<'source, Handle<ir::Override>> {
4747        let expr = self.expression(size_expr, ctx)?;
4748        match resolve_inner!(ctx, expr).scalar_kind().ok_or(0) {
4749            Ok(ir::ScalarKind::Sint) | Ok(ir::ScalarKind::Uint) => Ok({
4750                if let ir::Expression::Override(handle) = ctx.module.global_expressions[expr] {
4751                    handle
4752                } else {
4753                    let ty = ctx.register_type(expr)?;
4754                    ctx.module.overrides.append(
4755                        ir::Override {
4756                            name: None,
4757                            id: None,
4758                            ty,
4759                            init: Some(expr),
4760                        },
4761                        span,
4762                    )
4763                }
4764            }),
4765            _ => Err(Box::new(Error::ExpectedConstExprConcreteIntegerScalar(
4766                span,
4767            ))),
4768        }
4769    }
4770
4771    /// Build the Naga equivalent of a named AST type.
4772    ///
4773    /// Return a Naga `Handle<Type>` representing the front-end type
4774    /// `handle`, which should be named `name`, if given.
4775    ///
4776    /// If `handle` refers to a type cached in [`SpecialTypes`],
4777    /// `name` may be ignored.
4778    ///
4779    /// [`SpecialTypes`]: ir::SpecialTypes
4780    fn resolve_named_ast_type(
4781        &mut self,
4782        ident: &ast::TemplateElaboratedIdent<'source>,
4783        name: String,
4784        ctx: &mut ExpressionContext<'source, '_, '_>,
4785    ) -> Result<'source, Handle<ir::Type>> {
4786        self.type_specifier(ident, ctx, Some(name))
4787    }
4788
4789    /// Return a Naga `Handle<Type>` representing the front-end type `handle`.
4790    fn resolve_ast_type(
4791        &mut self,
4792        ident: &ast::TemplateElaboratedIdent<'source>,
4793        ctx: &mut ExpressionContext<'source, '_, '_>,
4794    ) -> Result<'source, Handle<ir::Type>> {
4795        self.type_specifier(ident, ctx, None)
4796    }
4797
4798    fn binding(
4799        &mut self,
4800        binding: &Option<ast::Binding<'source>>,
4801        ty: Handle<ir::Type>,
4802        ctx: &mut GlobalContext<'source, '_, '_>,
4803    ) -> Result<'source, Option<ir::Binding>> {
4804        Ok(match *binding {
4805            Some(ast::Binding::BuiltIn(b)) => Some(ir::Binding::BuiltIn(b)),
4806            Some(ast::Binding::Location {
4807                location,
4808                interpolation,
4809                sampling,
4810                blend_src,
4811                per_primitive,
4812            }) => {
4813                let blend_src = if let Some(blend_src) = blend_src {
4814                    Some(self.const_u32(blend_src, &mut ctx.as_const())?.0)
4815                } else {
4816                    None
4817                };
4818
4819                let mut binding = ir::Binding::Location {
4820                    location: self.const_u32(location, &mut ctx.as_const())?.0,
4821                    interpolation,
4822                    sampling,
4823                    blend_src,
4824                    per_primitive,
4825                };
4826                binding.apply_default_interpolation(&ctx.module.types[ty].inner);
4827                Some(binding)
4828            }
4829            None => None,
4830        })
4831    }
4832
4833    fn ray_query_pointer(
4834        &mut self,
4835        expr: Handle<ast::Expression<'source>>,
4836        ctx: &mut ExpressionContext<'source, '_, '_>,
4837    ) -> Result<'source, Handle<ir::Expression>> {
4838        let span = ctx.ast_expressions.get_span(expr);
4839        let pointer = self.expression(expr, ctx)?;
4840
4841        match *resolve_inner!(ctx, pointer) {
4842            ir::TypeInner::Pointer { base, .. } => match ctx.module.types[base].inner {
4843                ir::TypeInner::RayQuery { .. } => Ok(pointer),
4844                ref other => {
4845                    log::error!("Pointer type to {other:?} passed to ray query op");
4846                    Err(Box::new(Error::InvalidRayQueryPointer(span)))
4847                }
4848            },
4849            ref other => {
4850                log::error!("Type {other:?} passed to ray query op");
4851                Err(Box::new(Error::InvalidRayQueryPointer(span)))
4852            }
4853        }
4854    }
4855}
4856
4857impl ir::AtomicFunction {
4858    pub fn map(word: &str) -> Option<Self> {
4859        Some(match word {
4860            "atomicAdd" => ir::AtomicFunction::Add,
4861            "atomicSub" => ir::AtomicFunction::Subtract,
4862            "atomicAnd" => ir::AtomicFunction::And,
4863            "atomicOr" => ir::AtomicFunction::InclusiveOr,
4864            "atomicXor" => ir::AtomicFunction::ExclusiveOr,
4865            "atomicMin" => ir::AtomicFunction::Min,
4866            "atomicMax" => ir::AtomicFunction::Max,
4867            "atomicExchange" => ir::AtomicFunction::Exchange { compare: None },
4868            _ => return None,
4869        })
4870    }
4871}