Struct naga::proc::ConstantEvaluator
source · pub struct ConstantEvaluator<'a> { /* private fields */ }
Expand description
A context for evaluating constant expressions.
A ConstantEvaluator
points at an expression arena to which it can append
newly evaluated expressions: you pass try_eval_and_append
whatever kind
of Naga Expression
you like, and if its value can be computed at compile
time, try_eval_and_append
appends an expression representing the computed
value - a tree of Literal
, Compose
, ZeroValue
, and Swizzle
expressions - to the arena. See the try_eval_and_append
method for details.
A ConstantEvaluator
also holds whatever information we need to carry out
that evaluation: types, other constants, and so on.
Implementations§
source§impl<'a> ConstantEvaluator<'a>
impl<'a> ConstantEvaluator<'a>
sourcepub fn for_wgsl_module(
module: &'a mut Module,
global_expression_kind_tracker: &'a mut ExpressionKindTracker,
in_override_ctx: bool
) -> Self
pub fn for_wgsl_module( module: &'a mut Module, global_expression_kind_tracker: &'a mut ExpressionKindTracker, in_override_ctx: bool ) -> Self
Return a ConstantEvaluator
that will add expressions to module
’s
constant expression arena.
Report errors according to WGSL’s rules for constant evaluation.
sourcepub fn for_glsl_module(
module: &'a mut Module,
global_expression_kind_tracker: &'a mut ExpressionKindTracker
) -> Self
pub fn for_glsl_module( module: &'a mut Module, global_expression_kind_tracker: &'a mut ExpressionKindTracker ) -> Self
Return a ConstantEvaluator
that will add expressions to module
’s
constant expression arena.
Report errors according to GLSL’s rules for constant evaluation.
sourcepub fn for_wgsl_function(
module: &'a mut Module,
expressions: &'a mut Arena<Expression>,
local_expression_kind_tracker: &'a mut ExpressionKindTracker,
emitter: &'a mut Emitter,
block: &'a mut Block,
is_const: bool
) -> Self
pub fn for_wgsl_function( module: &'a mut Module, expressions: &'a mut Arena<Expression>, local_expression_kind_tracker: &'a mut ExpressionKindTracker, emitter: &'a mut Emitter, block: &'a mut Block, is_const: bool ) -> Self
Return a ConstantEvaluator
that will add expressions to function
’s
expression arena.
Report errors according to WGSL’s rules for constant evaluation.
sourcepub fn for_glsl_function(
module: &'a mut Module,
expressions: &'a mut Arena<Expression>,
local_expression_kind_tracker: &'a mut ExpressionKindTracker,
emitter: &'a mut Emitter,
block: &'a mut Block
) -> Self
pub fn for_glsl_function( module: &'a mut Module, expressions: &'a mut Arena<Expression>, local_expression_kind_tracker: &'a mut ExpressionKindTracker, emitter: &'a mut Emitter, block: &'a mut Block ) -> Self
Return a ConstantEvaluator
that will add expressions to function
’s
expression arena.
Report errors according to GLSL’s rules for constant evaluation.
pub fn to_ctx(&self) -> GlobalCtx<'_>
sourcepub fn try_eval_and_append(
&mut self,
expr: Expression,
span: Span
) -> Result<Handle<Expression>, ConstantEvaluatorError>
pub fn try_eval_and_append( &mut self, expr: Expression, span: Span ) -> Result<Handle<Expression>, ConstantEvaluatorError>
Try to evaluate expr
at compile time.
The expr
argument can be any sort of Naga Expression
you like. If
we can determine its value at compile time, we append an expression
representing its value - a tree of Literal
, Compose
,
ZeroValue
, and Swizzle
expressions - to the expression arena
self
contributes to.
If expr
’s value cannot be determined at compile time, and self
is
contributing to some function’s expression arena, then append expr
to
that arena unchanged (and thus unevaluated). Otherwise, self
must be
contributing to the module’s constant expression arena; since expr
’s
value is not a constant, return an error.
We only consider expr
itself, without recursing into its operands. Its
operands must all have been produced by prior calls to
try_eval_and_append
, to ensure that they have already been reduced to
an evaluated form if possible.
sourcepub fn cast(
&mut self,
expr: Handle<Expression>,
target: Scalar,
span: Span
) -> Result<Handle<Expression>, ConstantEvaluatorError>
pub fn cast( &mut self, expr: Handle<Expression>, target: Scalar, span: Span ) -> Result<Handle<Expression>, ConstantEvaluatorError>
Convert the scalar components of expr
to target
.
Treat span
as the location of the resulting expression.
sourcepub fn cast_array(
&mut self,
expr: Handle<Expression>,
target: Scalar,
span: Span
) -> Result<Handle<Expression>, ConstantEvaluatorError>
pub fn cast_array( &mut self, expr: Handle<Expression>, target: Scalar, span: Span ) -> Result<Handle<Expression>, ConstantEvaluatorError>
Convert the scalar leaves of expr
to target
, handling arrays.
expr
must be a Compose
expression whose type is a scalar, vector,
matrix, or nested arrays of such.
This is basically the same as the cast
method, except that that
should only handle Naga As
expressions, which cannot convert arrays.
Treat span
as the location of the resulting expression.