use serde::Serialize;
use crate::model::{SourceLocation, TypeInfo, expression::Expression};
#[derive(Debug, Clone, Serialize)]
pub struct SizeofExpression {
#[serde(skip_serializing_if = "Option::is_none")]
pub operand: Option<SizeofOperand>,
pub text: String, pub location: SourceLocation,
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum SizeofOperand {
Type(TypeInfo), Expression(Box<Expression>), }
impl SizeofExpression {
pub fn type_name(&self) -> Option<&str> {
match &self.operand {
Some(SizeofOperand::Type(t)) => Some(&t.base_type),
Some(SizeofOperand::Expression(expr)) => {
if let Expression::Identifier(id) = expr.as_ref() {
Some(&id.name)
} else {
None
}
}
None => None,
}
}
pub fn is_sizeof_type(&self) -> bool {
match &self.operand {
Some(SizeofOperand::Type(_)) => true,
Some(SizeofOperand::Expression(expr)) => {
matches!(expr.as_ref(), Expression::Identifier(_))
}
None => false,
}
}
}