use crate::{
ast::pat::PatKind,
common::{ExprId, SpanId},
context::with_cx,
ffi::{FfiOption, FfiSlice},
span::{Ident, Span},
};
use super::{CommonExprData, ExprKind};
#[repr(C)]
#[derive(Debug)]
pub struct IfExpr<'ast> {
data: CommonExprData<'ast>,
condition: ExprKind<'ast>,
then: ExprKind<'ast>,
els: FfiOption<ExprKind<'ast>>,
}
impl<'ast> IfExpr<'ast> {
pub fn condition(&self) -> ExprKind<'ast> {
self.condition
}
pub fn then(&self) -> ExprKind<'ast> {
self.then
}
pub fn els(&self) -> Option<ExprKind<'ast>> {
self.els.copy()
}
}
super::impl_expr_data!(IfExpr<'ast>, If);
#[cfg(feature = "driver-api")]
impl<'ast> IfExpr<'ast> {
pub fn new(
data: CommonExprData<'ast>,
condition: ExprKind<'ast>,
then: ExprKind<'ast>,
els: Option<ExprKind<'ast>>,
) -> Self {
Self {
data,
condition,
then,
els: els.into(),
}
}
}
#[repr(C)]
#[derive(Debug)]
pub struct LetExpr<'ast> {
data: CommonExprData<'ast>,
pat: PatKind<'ast>,
scrutinee: ExprKind<'ast>,
}
impl<'ast> LetExpr<'ast> {
pub fn pat(&self) -> PatKind<'ast> {
self.pat
}
pub fn scrutinee(&self) -> ExprKind<'ast> {
self.scrutinee
}
}
super::impl_expr_data!(LetExpr<'ast>, Let);
#[cfg(feature = "driver-api")]
impl<'ast> LetExpr<'ast> {
pub fn new(data: CommonExprData<'ast>, pat: PatKind<'ast>, scrutinee: ExprKind<'ast>) -> Self {
Self { data, pat, scrutinee }
}
}
#[repr(C)]
#[derive(Debug)]
pub struct MatchExpr<'ast> {
data: CommonExprData<'ast>,
scrutinee: ExprKind<'ast>,
arms: FfiSlice<'ast, MatchArm<'ast>>,
}
impl<'ast> MatchExpr<'ast> {
pub fn scrutinee(&self) -> ExprKind<'ast> {
self.scrutinee
}
pub fn arms(&self) -> &[MatchArm<'ast>] {
self.arms.get()
}
}
super::impl_expr_data!(MatchExpr<'ast>, Match);
#[cfg(feature = "driver-api")]
impl<'ast> MatchExpr<'ast> {
pub fn new(data: CommonExprData<'ast>, scrutinee: ExprKind<'ast>, arms: &'ast [MatchArm<'ast>]) -> Self {
Self {
data,
scrutinee,
arms: arms.into(),
}
}
}
#[repr(C)]
#[derive(Debug)]
pub struct MatchArm<'ast> {
span: SpanId,
pat: PatKind<'ast>,
guard: FfiOption<ExprKind<'ast>>,
expr: ExprKind<'ast>,
}
impl<'ast> MatchArm<'ast> {
pub fn span(&self) -> &Span<'ast> {
with_cx(self, |cx| cx.span(self.span))
}
pub fn pat(&self) -> PatKind<'ast> {
self.pat
}
pub fn guard(&self) -> Option<ExprKind<'ast>> {
self.guard.copy()
}
pub fn expr(&self) -> ExprKind<'ast> {
self.expr
}
}
#[cfg(feature = "driver-api")]
impl<'ast> MatchArm<'ast> {
pub fn new(span: SpanId, pat: PatKind<'ast>, guard: Option<ExprKind<'ast>>, expr: ExprKind<'ast>) -> Self {
Self {
span,
pat,
guard: guard.into(),
expr,
}
}
}
#[repr(C)]
#[derive(Debug)]
pub struct ReturnExpr<'ast> {
data: CommonExprData<'ast>,
expr: FfiOption<ExprKind<'ast>>,
}
impl<'ast> ReturnExpr<'ast> {
pub fn expr(&self) -> Option<ExprKind<'ast>> {
self.expr.copy()
}
}
super::impl_expr_data!(ReturnExpr<'ast>, Return);
#[cfg(feature = "driver-api")]
impl<'ast> ReturnExpr<'ast> {
pub fn new(data: CommonExprData<'ast>, expr: Option<ExprKind<'ast>>) -> Self {
Self {
data,
expr: expr.into(),
}
}
}
#[repr(C)]
#[derive(Debug)]
pub struct BreakExpr<'ast> {
data: CommonExprData<'ast>,
label: FfiOption<Ident<'ast>>,
target_id: ExprId,
expr: FfiOption<ExprKind<'ast>>,
}
impl<'ast> BreakExpr<'ast> {
pub fn label(&self) -> Option<&Ident<'ast>> {
self.label.get()
}
pub fn target_id(&self) -> ExprId {
self.target_id
}
pub fn expr(&self) -> Option<ExprKind<'ast>> {
self.expr.copy()
}
}
super::impl_expr_data!(BreakExpr<'ast>, Break);
#[cfg(feature = "driver-api")]
impl<'ast> BreakExpr<'ast> {
pub fn new(
data: CommonExprData<'ast>,
label: Option<Ident<'ast>>,
target_id: ExprId,
expr: Option<ExprKind<'ast>>,
) -> Self {
Self {
data,
label: label.into(),
target_id,
expr: expr.into(),
}
}
}
#[repr(C)]
#[derive(Debug)]
pub struct ContinueExpr<'ast> {
data: CommonExprData<'ast>,
label: FfiOption<Ident<'ast>>,
target_id: ExprId,
}
impl<'ast> ContinueExpr<'ast> {
pub fn label(&self) -> Option<&Ident<'ast>> {
self.label.get()
}
pub fn target_id(&self) -> ExprId {
self.target_id
}
}
super::impl_expr_data!(ContinueExpr<'ast>, Continue);
#[cfg(feature = "driver-api")]
impl<'ast> ContinueExpr<'ast> {
pub fn new(data: CommonExprData<'ast>, label: Option<Ident<'ast>>, target_id: ExprId) -> Self {
Self {
data,
label: label.into(),
target_id,
}
}
}
#[repr(C)]
#[derive(Debug)]
pub struct LoopExpr<'ast> {
data: CommonExprData<'ast>,
label: FfiOption<Ident<'ast>>,
block: ExprKind<'ast>,
}
impl<'ast> LoopExpr<'ast> {
pub fn label(&self) -> Option<&Ident<'ast>> {
self.label.get()
}
pub fn block(&self) -> ExprKind<'ast> {
self.block
}
}
super::impl_expr_data!(LoopExpr<'ast>, Loop);
#[cfg(feature = "driver-api")]
impl<'ast> LoopExpr<'ast> {
pub fn new(data: CommonExprData<'ast>, label: Option<Ident<'ast>>, block: ExprKind<'ast>) -> Self {
Self {
data,
label: label.into(),
block,
}
}
}
#[repr(C)]
#[derive(Debug)]
pub struct WhileExpr<'ast> {
data: CommonExprData<'ast>,
label: FfiOption<Ident<'ast>>,
condition: ExprKind<'ast>,
block: ExprKind<'ast>,
}
impl<'ast> WhileExpr<'ast> {
pub fn label(&self) -> Option<&Ident<'ast>> {
self.label.get()
}
pub fn condition(&self) -> ExprKind {
self.condition
}
pub fn block(&self) -> ExprKind<'ast> {
self.block
}
}
super::impl_expr_data!(WhileExpr<'ast>, While);
#[cfg(feature = "driver-api")]
impl<'ast> WhileExpr<'ast> {
pub fn new(
data: CommonExprData<'ast>,
label: Option<Ident<'ast>>,
condition: ExprKind<'ast>,
block: ExprKind<'ast>,
) -> Self {
Self {
data,
condition,
label: label.into(),
block,
}
}
}
#[repr(C)]
#[derive(Debug)]
pub struct ForExpr<'ast> {
data: CommonExprData<'ast>,
label: FfiOption<Ident<'ast>>,
pat: PatKind<'ast>,
iterable: ExprKind<'ast>,
block: ExprKind<'ast>,
}
impl<'ast> ForExpr<'ast> {
pub fn label(&self) -> Option<&Ident<'ast>> {
self.label.get()
}
pub fn pat(&self) -> PatKind<'ast> {
self.pat
}
pub fn iterable(&self) -> ExprKind {
self.iterable
}
pub fn block(&self) -> ExprKind<'ast> {
self.block
}
}
super::impl_expr_data!(ForExpr<'ast>, For);
#[cfg(feature = "driver-api")]
impl<'ast> ForExpr<'ast> {
pub fn new(
data: CommonExprData<'ast>,
label: Option<Ident<'ast>>,
pat: PatKind<'ast>,
iterable: ExprKind<'ast>,
block: ExprKind<'ast>,
) -> Self {
Self {
data,
label: label.into(),
pat,
iterable,
block,
}
}
}