use super::IrExpr;
use crate::ir::ResolvedType;
impl IrExpr {
#[must_use]
pub const fn ty(&self) -> &ResolvedType {
match self {
Self::Literal { ty, .. }
| Self::StructInst { ty, .. }
| Self::EnumInst { ty, .. }
| Self::Array { ty, .. }
| Self::Tuple { ty, .. }
| Self::Reference { ty, .. }
| Self::SelfFieldRef { ty, .. }
| Self::FieldAccess { ty, .. }
| Self::LetRef { ty, .. }
| Self::BinaryOp { ty, .. }
| Self::UnaryOp { ty, .. }
| Self::If { ty, .. }
| Self::For { ty, .. }
| Self::Match { ty, .. }
| Self::FunctionCall { ty, .. }
| Self::CallClosure { ty, .. }
| Self::MethodCall { ty, .. }
| Self::Closure { ty, .. }
| Self::ClosureRef { ty, .. }
| Self::DictLiteral { ty, .. }
| Self::DictAccess { ty, .. }
| Self::Block { ty, .. } => ty,
}
}
pub const fn ty_mut(&mut self) -> &mut ResolvedType {
match self {
Self::Literal { ty, .. }
| Self::StructInst { ty, .. }
| Self::EnumInst { ty, .. }
| Self::Array { ty, .. }
| Self::Tuple { ty, .. }
| Self::Reference { ty, .. }
| Self::SelfFieldRef { ty, .. }
| Self::FieldAccess { ty, .. }
| Self::LetRef { ty, .. }
| Self::BinaryOp { ty, .. }
| Self::UnaryOp { ty, .. }
| Self::If { ty, .. }
| Self::For { ty, .. }
| Self::Match { ty, .. }
| Self::FunctionCall { ty, .. }
| Self::CallClosure { ty, .. }
| Self::MethodCall { ty, .. }
| Self::Closure { ty, .. }
| Self::ClosureRef { ty, .. }
| Self::DictLiteral { ty, .. }
| Self::DictAccess { ty, .. }
| Self::Block { ty, .. } => ty,
}
}
pub const fn span_mut(&mut self) -> &mut crate::ir::IrSpan {
match self {
Self::Literal { span, .. }
| Self::StructInst { span, .. }
| Self::EnumInst { span, .. }
| Self::Array { span, .. }
| Self::Tuple { span, .. }
| Self::Reference { span, .. }
| Self::SelfFieldRef { span, .. }
| Self::FieldAccess { span, .. }
| Self::LetRef { span, .. }
| Self::BinaryOp { span, .. }
| Self::UnaryOp { span, .. }
| Self::If { span, .. }
| Self::For { span, .. }
| Self::Match { span, .. }
| Self::FunctionCall { span, .. }
| Self::CallClosure { span, .. }
| Self::MethodCall { span, .. }
| Self::Closure { span, .. }
| Self::ClosureRef { span, .. }
| Self::DictLiteral { span, .. }
| Self::DictAccess { span, .. }
| Self::Block { span, .. } => span,
}
}
#[must_use]
pub fn is_constant(&self) -> bool {
match self {
Self::Literal { .. } => true,
Self::Array { elements, .. } => elements.iter().all(Self::is_constant),
Self::Tuple { fields, .. } => fields.iter().all(|(_, e)| e.is_constant()),
Self::StructInst { fields, .. } | Self::EnumInst { fields, .. } => {
fields.iter().all(|(_, _, e)| e.is_constant())
}
Self::DictLiteral { entries, .. } => entries
.iter()
.all(|(k, v)| k.is_constant() && v.is_constant()),
Self::Reference { .. }
| Self::SelfFieldRef { .. }
| Self::FieldAccess { .. }
| Self::LetRef { .. }
| Self::BinaryOp { .. }
| Self::UnaryOp { .. }
| Self::If { .. }
| Self::For { .. }
| Self::Match { .. }
| Self::FunctionCall { .. }
| Self::CallClosure { .. }
| Self::MethodCall { .. }
| Self::Closure { .. }
| Self::ClosureRef { .. }
| Self::DictAccess { .. }
| Self::Block { .. } => false,
}
}
}