Skip to content

Commit f168151

Browse files
committed
Implemented sizeof(type) C expression
1 parent 6a0285c commit f168151

File tree

4 files changed

+8
-4
lines changed

4 files changed

+8
-4
lines changed

src/c/ast/expr.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::{AbstractDeclarator, SpecifierQualifierList};
22
use crate::{
3+
ast,
34
c::{
45
encoding::Encoding,
56
token::{FloatSuffix, Integer},
@@ -43,6 +44,7 @@ pub enum ExprKind {
4344
BitComplement(Box<Expr>),
4445
Not(Box<Expr>),
4546
Call(Box<Expr>, Vec<Expr>),
47+
SizeOf(ast::Type),
4648
}
4749

4850
impl ExprKind {

src/c/parser/expr.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -273,15 +273,15 @@ impl<'input, 'diagnostics> Parser<'input, 'diagnostics> {
273273
TranslateCtx::new(&mut self.ast_file, &mut self.typedefs, self.diagnostics);
274274
let specifier_qualifiers = &type_name.specifier_qualifiers;
275275

276-
let ty = get_type(
276+
let abstract_declarator_info = get_type(
277277
&mut ctx,
278278
type_name.abstract_declarator.as_ref(),
279279
&specifier_qualifiers.into(),
280280
false,
281281
);
282282

283-
if let Ok(ty) = ty {
284-
return todo!("handle parsed sizeof(type) - {:?}", ty);
283+
if let Ok(abstract_declarator_info) = abstract_declarator_info {
284+
return Ok(ExprKind::SizeOf(abstract_declarator_info.ast_type).at(source));
285285
}
286286
}
287287

src/c/translate/eval.rs

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

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

src/c/translate/expr/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -154,5 +154,6 @@ pub fn translate_expr(ctx: &mut TranslateCtx, expr: &Expr) -> Result<ast::Expr,
154154
}))
155155
.at(expr.source)
156156
}
157+
ExprKind::SizeOf(ty) => ast::ExprKind::SizeOf(Box::new(ty.clone())).at(expr.source),
157158
})
158159
}

0 commit comments

Comments
 (0)