diff --git a/src/front/glsl/context.rs b/src/front/glsl/context.rs index d767f93588..0f836def69 100644 --- a/src/front/glsl/context.rs +++ b/src/front/glsl/context.rs @@ -8,10 +8,9 @@ use super::{ Frontend, Result, }; use crate::{ - front::{Emitter, Typifier}, - AddressSpace, Arena, BinaryOperator, Block, Expression, FastHashMap, FunctionArgument, Handle, - Literal, LocalVariable, RelationalFunction, ScalarKind, Span, Statement, Type, TypeInner, - VectorSize, + front::Typifier, proc::Emitter, AddressSpace, Arena, BinaryOperator, Block, Expression, + FastHashMap, FunctionArgument, Handle, Literal, LocalVariable, RelationalFunction, ScalarKind, + Span, Statement, Type, TypeInner, VectorSize, }; use std::ops::Index; diff --git a/src/front/mod.rs b/src/front/mod.rs index faebde4e24..634c809fb9 100644 --- a/src/front/mod.rs +++ b/src/front/mod.rs @@ -19,45 +19,6 @@ use crate::{ }; use std::ops; -/// Helper class to emit expressions -#[allow(dead_code)] -#[derive(Default, Debug)] -struct Emitter { - start_len: Option, -} - -#[allow(dead_code)] -impl Emitter { - fn start(&mut self, arena: &Arena) { - if self.start_len.is_some() { - unreachable!("Emitting has already started!"); - } - self.start_len = Some(arena.len()); - } - const fn is_running(&self) -> bool { - self.start_len.is_some() - } - #[must_use] - fn finish( - &mut self, - arena: &Arena, - ) -> Option<(crate::Statement, crate::span::Span)> { - let start_len = self.start_len.take().unwrap(); - if start_len != arena.len() { - #[allow(unused_mut)] - let mut span = crate::span::Span::default(); - let range = arena.range_from(start_len); - #[cfg(feature = "span")] - for handle in range.clone() { - span.subsume(arena.get_span(handle)) - } - Some((crate::Statement::Emit(range), span)) - } else { - None - } - } -} - /// A table of types for an `Arena`. /// /// A front end can use a `Typifier` to get types for an arena's expressions diff --git a/src/front/spv/function.rs b/src/front/spv/function.rs index 7a874ae4e0..198d9c52dd 100644 --- a/src/front/spv/function.rs +++ b/src/front/spv/function.rs @@ -4,7 +4,7 @@ use crate::{ }; use super::{Error, Instruction, LookupExpression, LookupHelper as _}; -use crate::front::Emitter; +use crate::proc::Emitter; pub type BlockId = u32; diff --git a/src/front/spv/image.rs b/src/front/spv/image.rs index 1cefeb4fcc..ee58c7ba14 100644 --- a/src/front/spv/image.rs +++ b/src/front/spv/image.rs @@ -256,7 +256,7 @@ impl> super::Frontend { &mut self, words_left: u16, ctx: &mut super::BlockContext, - emitter: &mut crate::front::Emitter, + emitter: &mut crate::proc::Emitter, block: &mut crate::Block, body_idx: usize, ) -> Result { @@ -315,7 +315,7 @@ impl> super::Frontend { &mut self, mut words_left: u16, ctx: &mut super::BlockContext, - emitter: &mut crate::front::Emitter, + emitter: &mut crate::proc::Emitter, block: &mut crate::Block, block_id: spirv::Word, body_idx: usize, @@ -415,7 +415,7 @@ impl> super::Frontend { mut words_left: u16, options: SamplingOptions, ctx: &mut super::BlockContext, - emitter: &mut crate::front::Emitter, + emitter: &mut crate::proc::Emitter, block: &mut crate::Block, block_id: spirv::Word, body_idx: usize, @@ -663,7 +663,7 @@ impl> super::Frontend { &mut self, at_level: bool, ctx: &mut super::BlockContext, - emitter: &mut crate::front::Emitter, + emitter: &mut crate::proc::Emitter, block: &mut crate::Block, block_id: spirv::Word, body_idx: usize, diff --git a/src/front/spv/mod.rs b/src/front/spv/mod.rs index b14f7bf111..68bc0d0f49 100644 --- a/src/front/spv/mod.rs +++ b/src/front/spv/mod.rs @@ -793,7 +793,7 @@ impl> Frontend { id: spirv::Word, lookup: &LookupExpression, ctx: &mut BlockContext, - emitter: &mut super::Emitter, + emitter: &mut crate::proc::Emitter, block: &mut crate::Block, body_idx: BodyIndex, ) -> Handle { @@ -855,7 +855,7 @@ impl> Frontend { fn parse_expr_unary_op( &mut self, ctx: &mut BlockContext, - emitter: &mut super::Emitter, + emitter: &mut crate::proc::Emitter, block: &mut crate::Block, block_id: spirv::Word, body_idx: usize, @@ -884,7 +884,7 @@ impl> Frontend { fn parse_expr_binary_op( &mut self, ctx: &mut BlockContext, - emitter: &mut super::Emitter, + emitter: &mut crate::proc::Emitter, block: &mut crate::Block, block_id: spirv::Word, body_idx: usize, @@ -918,7 +918,7 @@ impl> Frontend { fn parse_expr_unary_op_sign_adjusted( &mut self, ctx: &mut BlockContext, - emitter: &mut super::Emitter, + emitter: &mut crate::proc::Emitter, block: &mut crate::Block, block_id: spirv::Word, body_idx: usize, @@ -973,7 +973,7 @@ impl> Frontend { fn parse_expr_binary_op_sign_adjusted( &mut self, ctx: &mut BlockContext, - emitter: &mut super::Emitter, + emitter: &mut crate::proc::Emitter, block: &mut crate::Block, block_id: spirv::Word, body_idx: usize, @@ -1051,7 +1051,7 @@ impl> Frontend { fn parse_expr_int_comparison( &mut self, ctx: &mut BlockContext, - emitter: &mut super::Emitter, + emitter: &mut crate::proc::Emitter, block: &mut crate::Block, block_id: spirv::Word, body_idx: usize, @@ -1122,7 +1122,7 @@ impl> Frontend { fn parse_expr_shift_op( &mut self, ctx: &mut BlockContext, - emitter: &mut super::Emitter, + emitter: &mut crate::proc::Emitter, block: &mut crate::Block, block_id: spirv::Word, body_idx: usize, @@ -1165,7 +1165,7 @@ impl> Frontend { fn parse_expr_derivative( &mut self, ctx: &mut BlockContext, - emitter: &mut super::Emitter, + emitter: &mut crate::proc::Emitter, block: &mut crate::Block, block_id: spirv::Word, body_idx: usize, @@ -1296,7 +1296,7 @@ impl> Frontend { }) } - let mut emitter = super::Emitter::default(); + let mut emitter = crate::proc::Emitter::default(); emitter.start(ctx.expressions); // Find the `Body` to which this block contributes. @@ -5286,7 +5286,7 @@ fn make_index_literal( ctx: &mut BlockContext, index: u32, block: &mut crate::Block, - emitter: &mut super::Emitter, + emitter: &mut crate::proc::Emitter, index_type: Handle, index_type_id: spirv::Word, span: crate::Span, diff --git a/src/front/wgsl/lower/mod.rs b/src/front/wgsl/lower/mod.rs index 1618a2bbf6..5ece316640 100644 --- a/src/front/wgsl/lower/mod.rs +++ b/src/front/wgsl/lower/mod.rs @@ -4,9 +4,10 @@ use crate::front::wgsl::error::{Error, ExpectedToken, InvalidAssignmentType}; use crate::front::wgsl::index::Index; use crate::front::wgsl::parse::number::Number; use crate::front::wgsl::parse::{ast, conv}; -use crate::front::{Emitter, Typifier}; +use crate::front::Typifier; use crate::proc::{ - ensure_block_returns, Alignment, ConstantEvaluator, Layouter, ResolveContext, TypeResolution, + ensure_block_returns, Alignment, ConstantEvaluator, Emitter, Layouter, ResolveContext, + TypeResolution, }; use crate::{Arena, FastHashMap, Handle, Span}; use indexmap::IndexMap; diff --git a/src/proc/emitter.rs b/src/proc/emitter.rs new file mode 100644 index 0000000000..281a55e2ad --- /dev/null +++ b/src/proc/emitter.rs @@ -0,0 +1,40 @@ +use crate::arena::Arena; + +/// Helper class to emit expressions +#[allow(dead_code)] +#[derive(Default, Debug)] +pub struct Emitter { + start_len: Option, +} + +#[allow(dead_code)] +impl Emitter { + pub fn start(&mut self, arena: &Arena) { + if self.start_len.is_some() { + unreachable!("Emitting has already started!"); + } + self.start_len = Some(arena.len()); + } + pub const fn is_running(&self) -> bool { + self.start_len.is_some() + } + #[must_use] + pub fn finish( + &mut self, + arena: &Arena, + ) -> Option<(crate::Statement, crate::span::Span)> { + let start_len = self.start_len.take().unwrap(); + if start_len != arena.len() { + #[allow(unused_mut)] + let mut span = crate::span::Span::default(); + let range = arena.range_from(start_len); + #[cfg(feature = "span")] + for handle in range.clone() { + span.subsume(arena.get_span(handle)) + } + Some((crate::Statement::Emit(range), span)) + } else { + None + } + } +} diff --git a/src/proc/mod.rs b/src/proc/mod.rs index 5a4c02abd4..0bf81fb173 100644 --- a/src/proc/mod.rs +++ b/src/proc/mod.rs @@ -3,6 +3,7 @@ */ mod constant_evaluator; +mod emitter; pub mod index; mod layouter; mod namer; @@ -10,6 +11,7 @@ mod terminator; mod typifier; pub use constant_evaluator::{ConstantEvaluator, ConstantEvaluatorError}; +pub use emitter::Emitter; pub use index::{BoundsCheckPolicies, BoundsCheckPolicy, IndexableLength, IndexableLengthError}; pub use layouter::{Alignment, LayoutError, LayoutErrorInner, Layouter, TypeLayout}; pub use namer::{EntryPointIndex, NameKey, Namer};