Expr
std::meta::expr
contains methods on the built-in Expr
type for quoted, syntactically valid expressions.
Methods
as_array
pub comptime fn as_array(self) -> Option<[Expr]> {}
If this expression is an array, this returns a slice of each element in the array.
as_assert
pub comptime fn as_assert(self) -> Option<(Expr, Option<Expr>)> {}
If this expression is an assert, this returns the assert expression and the optional message.
as_assert_eq
pub comptime fn as_assert_eq(self) -> Option<(Expr, Expr, Option<Expr>)> {}
If this expression is an assert_eq, this returns the left-hand-side and right-hand-side expressions, together with the optional message.
as_assign
pub comptime fn as_assign(self) -> Option<(Expr, Expr)> {}
If this expression is an assignment, this returns a tuple with the left hand side and right hand side in order.
as_binary_op
pub comptime fn as_binary_op(self) -> Option<(Expr, BinaryOp, Expr)> {}
If this expression is a binary operator operation <lhs> <op> <rhs>
,
return the left-hand side, operator, and the right-hand side of the operation.
as_block
pub comptime fn as_block(self) -> Option<[Expr]> {}
If this expression is a block { stmt1; stmt2; ...; stmtN }
, return
a slice containing each statement.
as_bool
pub comptime fn as_bool(self) -> Option<bool> {}
If this expression is a boolean literal, return that literal.
as_cast
#[builtin(expr_as_cast)]
pub comptime fn as_cast(self) -> Option<(Expr, UnresolvedType)> {}
If this expression is a cast expression (expr as type
), returns the casted
expression and the type to cast to.
as_comptime
pub comptime fn as_comptime(self) -> Option<[Expr]> {}
If this expression is a comptime { stmt1; stmt2; ...; stmtN }
block,
return each statement in the block.
as_constructor
pub comptime fn as_constructor(self) -> Option<(UnresolvedType, [(Quoted, Expr)])> {}
If this expression is a constructor Type { field1: expr1, ..., fieldN: exprN }
,
return the type and the fields.
as_for
pub comptime fn as_for(self) -> Option<(Quoted, Expr, Expr)> {}
If this expression is a for statement over a single expression, return the identifier, the expression and the for loop body.
as_for_range
pub comptime fn as_for(self) -> Option<(Quoted, Expr, Expr)> {}
If this expression is a for statement over a range, return the identifier, the range start, the range end and the for loop body.
as_function_call
pub comptime fn as_function_call(self) -> Option<(Expr, [Expr])> {}
If this expression is a function call foo(arg1, ..., argN)
, return
the function and a slice of each argument.
as_if
pub comptime fn as_if(self) -> Option<(Expr, Expr, Option<Expr>)> {}
If this expression is an if condition { then_branch } else { else_branch }
,
return the condition, then branch, and else branch. If there is no else branch,
None
is returned for that branch instead.
as_index
pub comptime fn as_index(self) -> Option<(Expr, Expr)> {}
If this expression is an index into an array array[index]
, return the
array and the index.
as_integer
pub comptime fn as_integer(self) -> Option<(Field, bool)> {}
If this expression is an integer literal, return the integer as a field as well as whether the integer is negative (true) or not (false).
as_lambda
pub comptime fn as_lambda(
self,
) -> Option<([(Expr, Option<UnresolvedType>)], Option<UnresolvedType>, Expr)> {}
If this expression is a lambda, returns the parameters, return type and body.
as_let
pub comptime fn as_let(self) -> Option<(Expr, Option<UnresolvedType>, Expr)> {}
If this expression is a let statement, returns the let pattern as an Expr
,
the optional type annotation, and the assigned expression.
as_member_access
pub comptime fn as_member_access(self) -> Option<(Expr, Quoted)> {}
If this expression is a member access foo.bar
, return the struct/tuple
expression and the field. The field will be represented as a quoted value.
as_method_call
pub comptime fn as_method_call(self) -> Option<(Expr, Quoted, [UnresolvedType], [Expr])> {}
If this expression is a method call foo.bar::<generic1, ..., genericM>(arg1, ..., argN)
, return
the receiver, method name, a slice of each generic argument, and a slice of each argument.
as_repeated_element_array
pub comptime fn as_repeated_element_array(self) -> Option<(Expr, Expr)> {}
If this expression is a repeated element array [elem; length]
, return
the repeated element and the length expressions.
as_repeated_element_slice
pub comptime fn as_repeated_element_slice(self) -> Option<(Expr, Expr)> {}
If this expression is a repeated element slice [elem; length]
, return
the repeated element and the length expressions.
as_slice
pub comptime fn as_slice(self) -> Option<[Expr]> {}
If this expression is a slice literal &[elem1, ..., elemN]
,
return each element of the slice.
as_tuple
pub comptime fn as_tuple(self) -> Option<[Expr]> {}
If this expression is a tuple (field1, ..., fieldN)
,
return each element of the tuple.
as_unary_op
pub comptime fn as_unary_op(self) -> Option<(UnaryOp, Expr)> {}
If this expression is a unary operation <op> <rhs>
,
return the unary operator as well as the right-hand side expression.
as_unsafe
pub comptime fn as_unsafe(self) -> Option<[Expr]> {}
If this expression is an unsafe { stmt1; ...; stmtN }
block,
return each statement inside in a slice.
has_semicolon
pub comptime fn has_semicolon(self) -> bool {}
true
if this expression is trailed by a semicolon. E.g.
comptime {
let expr1 = quote { 1 + 2 }.as_expr().unwrap();
let expr2 = quote { 1 + 2; }.as_expr().unwrap();
assert(expr1.as_binary_op().is_some());
assert(expr2.as_binary_op().is_some());
assert(!expr1.has_semicolon());
assert(expr2.has_semicolon());
}
is_break
pub comptime fn is_break(self) -> bool {}
true
if this expression is break
.
is_continue
pub comptime fn is_continue(self) -> bool {}
true
if this expression is continue
.
modify
pub comptime fn modify<Env>(self, f: fn[Env](Expr) -> Option<Expr>) -> Expr {
Applies a mapping function to this expression and to all of its sub-expressions.
f
will be applied to each sub-expression first, then applied to the expression itself.
This happens recursively for every expression within self
.
For example, calling modify
on (&[1], &[2, 3])
with an f
that returns Option::some
for expressions that are integers, doubling them, would return (&[2], &[4, 6])
.
quoted
pub comptime fn quoted(self) -> Quoted {
Returns this expression as a Quoted
value. It's the same as quote { $self }
.
resolve
pub comptime fn resolve(self, in_function: Option<FunctionDefinition>) -> TypedExpr {}
Resolves and type-checks this expression and returns the result as a TypedExpr
.
The in_function
argument specifies where the expression is resolved:
- If it's
none
, the expression is resolved in the function whereresolve
was called - If it's
some
, the expression is resolved in the given function
If any names used by this expression are not in scope or if there are any type errors,
this will give compiler errors as if the expression was written directly into
the current comptime
function.