mirror of
https://github.com/gfx-rs/wgpu.git
synced 2026-04-22 03:02:01 -04:00
Move front::Emitter to proc.
With the addition of the constant evaluator to the `proc` module, it is now concerned with constructing expressions. Any code that constructs expressions will generally also need to deal with `Emit` statements, which are handled by the `Emitter` type. However, `Emitter` is private to the `front` module. This patch moves it to `proc` and makes it accessible to both the constant evaluator and the front ends.
This commit is contained in:
committed by
Teodor Tanasoaia
parent
0e4ac2a98c
commit
77851ebfb6
@@ -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;
|
||||
|
||||
|
||||
@@ -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<usize>,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl Emitter {
|
||||
fn start(&mut self, arena: &Arena<crate::Expression>) {
|
||||
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<crate::Expression>,
|
||||
) -> 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<Expression>`.
|
||||
///
|
||||
/// A front end can use a `Typifier` to get types for an arena's expressions
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -256,7 +256,7 @@ impl<I: Iterator<Item = u32>> super::Frontend<I> {
|
||||
&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<crate::Statement, Error> {
|
||||
@@ -315,7 +315,7 @@ impl<I: Iterator<Item = u32>> super::Frontend<I> {
|
||||
&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<I: Iterator<Item = u32>> super::Frontend<I> {
|
||||
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<I: Iterator<Item = u32>> super::Frontend<I> {
|
||||
&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,
|
||||
|
||||
@@ -789,7 +789,7 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
|
||||
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<crate::Expression> {
|
||||
@@ -851,7 +851,7 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
|
||||
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,
|
||||
@@ -880,7 +880,7 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
|
||||
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,
|
||||
@@ -914,7 +914,7 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
|
||||
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,
|
||||
@@ -969,7 +969,7 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
|
||||
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,
|
||||
@@ -1047,7 +1047,7 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
|
||||
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,
|
||||
@@ -1118,7 +1118,7 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
|
||||
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,
|
||||
@@ -1161,7 +1161,7 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
|
||||
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,
|
||||
@@ -1292,7 +1292,7 @@ impl<I: Iterator<Item = u32>> Frontend<I> {
|
||||
})
|
||||
}
|
||||
|
||||
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.
|
||||
@@ -5282,7 +5282,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<crate::Type>,
|
||||
index_type_id: spirv::Word,
|
||||
span: crate::Span,
|
||||
|
||||
@@ -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, FastIndexMap, Handle, Span};
|
||||
|
||||
|
||||
40
src/proc/emitter.rs
Normal file
40
src/proc/emitter.rs
Normal file
@@ -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<usize>,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl Emitter {
|
||||
pub fn start(&mut self, arena: &Arena<crate::Expression>) {
|
||||
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<crate::Expression>,
|
||||
) -> 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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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};
|
||||
|
||||
Reference in New Issue
Block a user