Skip to content

Commit 6ee5b61

Browse files
committed
Added support for C sizeof value expressions
1 parent d3cc768 commit 6ee5b61

File tree

4 files changed

+9
-3
lines changed

4 files changed

+9
-3
lines changed

src/c/ast/expr.rs

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub enum ExprKind {
4545
Not(Box<Expr>),
4646
Call(Box<Expr>, Vec<Expr>),
4747
SizeOf(ast::Type),
48+
SizeOfValue(Box<Expr>),
4849
}
4950

5051
impl ExprKind {

src/c/parser/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ impl<'input, 'diagnostics> Parser<'input, 'diagnostics> {
285285
}
286286
}
287287

288-
let _inner = self.parse_expr_primary_base()?;
289-
todo!("parse sizeof expression")
288+
let inner = self.parse_expr_primary_base()?;
289+
return Ok(ExprKind::SizeOfValue(Box::new(inner)).at(source));
290290
}
291291
CTokenKind::AlignofKeyword => todo!("parse alignof expression"),
292292
_ => (),

src/c/translate/eval.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ pub fn evaluate_to_const_integer(expr: &Expr) -> Result<BigInt, ParseError> {
4949
| ExprKind::BitComplement(_)
5050
| ExprKind::Not(_)
5151
| ExprKind::Call(_, _)
52-
| ExprKind::SizeOf(_) => (),
52+
| ExprKind::SizeOf(_)
53+
| ExprKind::SizeOfValue(_) => (),
5354
}
5455

5556
Err(ParseErrorKind::MustBeConstantInteger.at(expr.source))

src/c/translate/expr/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -155,5 +155,9 @@ pub fn translate_expr(ctx: &mut TranslateCtx, expr: &Expr) -> Result<ast::Expr,
155155
.at(expr.source)
156156
}
157157
ExprKind::SizeOf(ty) => ast::ExprKind::SizeOf(Box::new(ty.clone())).at(expr.source),
158+
ExprKind::SizeOfValue(value) => {
159+
let inner = translate_expr(ctx, value)?;
160+
ast::ExprKind::SizeOfValue(Box::new(inner)).at(expr.source)
161+
}
158162
})
159163
}

0 commit comments

Comments
 (0)