Skip to content

Commit

Permalink
[wgsl-in] Abstract out ExpressionContext::as_const_evaluator.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimblandy authored and teoxoy committed Oct 19, 2023
1 parent 334f745 commit af26d77
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions src/front/wgsl/lower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,31 +346,36 @@ impl<'source, 'temp, 'out> ExpressionContext<'source, 'temp, 'out> {
}
}

fn as_const_evaluator(&mut self) -> ConstantEvaluator {
match self.expr_type {
ExpressionContextType::Runtime(ref mut rctx) => ConstantEvaluator::for_wgsl_function(
self.module,
rctx.naga_expressions,
rctx.expression_constness,
rctx.emitter,
rctx.block,
),
ExpressionContextType::Constant => ConstantEvaluator::for_wgsl_module(self.module),
}
}

fn append_expression(
&mut self,
expr: crate::Expression,
span: Span,
) -> Result<Handle<crate::Expression>, Error<'source>> {
match self.expr_type {
ExpressionContextType::Runtime(ref mut rctx) => {
let mut eval = ConstantEvaluator::for_wgsl_function(
self.module,
rctx.naga_expressions,
rctx.expression_constness,
rctx.emitter,
rctx.block,
);

match eval.try_eval_and_append(&expr, span) {
Ok(expr) => Ok(expr),
Err(_) => Ok(rctx.naga_expressions.append(expr, span)),
let mut eval = self.as_const_evaluator();
match eval.try_eval_and_append(&expr, span) {
Ok(expr) => Ok(expr),

// `expr` is not a constant expression. This is fine as
// long as we're not building `Module::const_expressions`.
Err(err) => match self.expr_type {
ExpressionContextType::Runtime(ref mut rctx) => {
Ok(rctx.naga_expressions.append(expr, span))
}
}
ExpressionContextType::Constant => {
let mut eval = ConstantEvaluator::for_wgsl_module(self.module);
eval.try_eval_and_append(&expr, span)
.map_err(|e| Error::ConstantEvaluatorError(e, span))
}
ExpressionContextType::Constant => Err(Error::ConstantEvaluatorError(err, span)),
},
}
}

Expand Down

0 comments on commit af26d77

Please sign in to comment.