gobject_ast/model/expression/
sizeof.rs1use serde::Serialize;
2
3use crate::model::{SourceLocation, TypeInfo, expression::Expression};
4
5#[derive(Debug, Clone, Serialize)]
6pub struct SizeofExpression {
7 #[serde(skip_serializing_if = "Option::is_none")]
8 pub operand: Option<SizeofOperand>,
9 pub text: String, pub location: SourceLocation,
11}
12
13#[derive(Debug, Clone, Serialize)]
14#[serde(rename_all = "snake_case")]
15pub enum SizeofOperand {
16 Type(TypeInfo), Expression(Box<Expression>), }
19
20impl SizeofExpression {
21 pub fn type_name(&self) -> Option<&str> {
25 match &self.operand {
26 Some(SizeofOperand::Type(t)) => Some(&t.base_type),
27 Some(SizeofOperand::Expression(expr)) => {
28 if let Expression::Identifier(id) = expr.as_ref() {
29 Some(&id.name)
30 } else {
31 None
32 }
33 }
34 None => None,
35 }
36 }
37
38 pub fn is_sizeof_type(&self) -> bool {
40 match &self.operand {
41 Some(SizeofOperand::Type(_)) => true,
42 Some(SizeofOperand::Expression(expr)) => {
44 matches!(expr.as_ref(), Expression::Identifier(_))
45 }
46 None => false,
47 }
48 }
49}