Skip to content

Commit ed71b15

Browse files
committed
Implemented parsing for C alignof expressions and pre-increment/decrement expressions
1 parent 3cbe2b9 commit ed71b15

File tree

9 files changed

+83
-44
lines changed

9 files changed

+83
-44
lines changed

src/asg/datatype/kind/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl TypeKind {
6666
TypeKind::AnonymousUnion() => false,
6767
TypeKind::AnonymousEnum(_) => false,
6868
TypeKind::FixedArray(fixed_array) => fixed_array.inner.kind.contains_polymorph(),
69-
TypeKind::FuncPtr(_) => todo!(),
69+
TypeKind::FuncPtr(_) => todo!("contains_polymorph FuncPtr"),
7070
TypeKind::Enum(_, _) => false,
7171
TypeKind::Structure(_, _, parameters)
7272
| TypeKind::Trait(_, _, parameters)
@@ -213,7 +213,7 @@ impl TypeKind {
213213
inner: inner.into_owned(),
214214
}))))
215215
}
216-
TypeKind::FuncPtr(_) => todo!(),
216+
TypeKind::FuncPtr(_) => todo!("map_type_params FuncPtr"),
217217
TypeKind::Enum(_, _) => Ok(Cow::Borrowed(self)),
218218
TypeKind::Structure(human_name, struct_ref, type_args) => {
219219
if type_args.is_empty() {

src/c/ast/expr.rs

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ pub enum ExprKind {
3333
Cast(Box<Cast>),
3434
Subscript(Box<Subscript>),
3535
Field(Box<Field>),
36+
PreIncrement(Box<Expr>),
37+
PreDecrement(Box<Expr>),
3638
PostIncrement(Box<Expr>),
3739
PostDecrement(Box<Expr>),
3840
Identifier(String),
@@ -46,6 +48,7 @@ pub enum ExprKind {
4648
Call(Box<Expr>, Vec<Expr>),
4749
SizeOf(ast::Type),
4850
SizeOfValue(Box<Expr>),
51+
AlignOf(ast::Type),
4952
IntegerPromote(Box<Expr>),
5053
}
5154

src/c/parser/expr.rs

+28-4
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl<'input, 'diagnostics> Parser<'input, 'diagnostics> {
120120

121121
// Generic Selection
122122
if self.eat(CTokenKind::GenericKeyword) {
123-
todo!()
123+
todo!("parse _Generic");
124124
}
125125

126126
Err(ParseErrorKind::Misc("Expected expression").at(source))
@@ -267,8 +267,16 @@ impl<'input, 'diagnostics> Parser<'input, 'diagnostics> {
267267
let inner = self.parse_expr_primary()?;
268268
return Ok(ExprKind::Not(Box::new(inner)).at(source));
269269
}
270-
CTokenKind::Punctuator(Punctuator::Increment) => todo!("parse increment expression"),
271-
CTokenKind::Punctuator(Punctuator::Decrement) => todo!("parse decrement expression"),
270+
CTokenKind::Punctuator(Punctuator::Increment) => {
271+
self.input.advance();
272+
let inner = self.parse_expr_primary_base()?;
273+
return Ok(ExprKind::PreIncrement(Box::new(inner)).at(source));
274+
}
275+
CTokenKind::Punctuator(Punctuator::Decrement) => {
276+
self.input.advance();
277+
let inner = self.parse_expr_primary_base()?;
278+
return Ok(ExprKind::PreDecrement(Box::new(inner)).at(source));
279+
}
272280
CTokenKind::SizeofKeyword => {
273281
self.input.advance();
274282

@@ -292,7 +300,23 @@ impl<'input, 'diagnostics> Parser<'input, 'diagnostics> {
292300
let inner = self.parse_expr_primary_base()?;
293301
return Ok(ExprKind::SizeOfValue(Box::new(inner)).at(source));
294302
}
295-
CTokenKind::AlignofKeyword => todo!("parse alignof expression"),
303+
CTokenKind::AlignofKeyword => {
304+
self.input.advance();
305+
306+
let type_name = self.parse_type_in_parens()?;
307+
let mut ctx =
308+
TranslateCtx::new(&mut self.ast_file, &mut self.typedefs, self.diagnostics);
309+
let specifier_qualifiers = &type_name.specifier_qualifiers;
310+
311+
let abstract_declarator_info = get_type(
312+
&mut ctx,
313+
type_name.abstract_declarator.as_ref(),
314+
&specifier_qualifiers.into(),
315+
false,
316+
)?;
317+
318+
return Ok(ExprKind::AlignOf(abstract_declarator_info.ast_type).at(source));
319+
}
296320
_ => (),
297321
}
298322

src/c/parser/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ impl<'input, 'diagnostics> Parser<'input, 'diagnostics> {
277277

278278
#[allow(clippy::redundant_pattern_matching)]
279279
if let Ok(..) = speculate!(self.input, self.parse_atomic_type_specifier()) {
280-
return Ok(todo!());
280+
return Ok(todo!("parse atomic type specifier"));
281281
}
282282

283283
if let Ok(composite) = speculate!(self.input, self.parse_struct_or_union_specifier()) {
@@ -303,7 +303,7 @@ impl<'input, 'diagnostics> Parser<'input, 'diagnostics> {
303303

304304
#[allow(clippy::redundant_pattern_matching)]
305305
if let Ok(..) = speculate!(self.input, self.parse_typeof_specifier()) {
306-
return Ok(todo!());
306+
return Ok(todo!("parse c typeof"));
307307
}
308308

309309
Err(self.error("Failed to parse type specifier"))
@@ -884,8 +884,8 @@ impl<'input, 'diagnostics> Parser<'input, 'diagnostics> {
884884

885885
fn parse_typeof_specifier(&mut self) -> Result<(), ParseError> {
886886
match self.input.advance().kind {
887-
CTokenKind::TypeofKeyword => todo!(),
888-
CTokenKind::TypeofUnqualKeyword => todo!(),
887+
CTokenKind::TypeofKeyword => todo!("parse c typeof keyword"),
888+
CTokenKind::TypeofUnqualKeyword => todo!("parse c typeof_unqual keyword"),
889889
_ => Err(self.error("Failed to parse typeof specifier")),
890890
}
891891
}

src/c/translate/eval.rs

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ pub fn evaluate_to_const_integer(expr: &Expr) -> Result<BigInt, ParseError> {
4141
ExprKind::Float(_, _)
4242
| ExprKind::StringLiteral(_, _)
4343
| ExprKind::Compound(_)
44+
| ExprKind::PreIncrement(_)
45+
| ExprKind::PreDecrement(_)
4446
| ExprKind::PostIncrement(_)
4547
| ExprKind::PostDecrement(_)
4648
| ExprKind::CompoundLiteral(_)
@@ -52,6 +54,7 @@ pub fn evaluate_to_const_integer(expr: &Expr) -> Result<BigInt, ParseError> {
5254
| ExprKind::Call(_, _)
5355
| ExprKind::SizeOf(_)
5456
| ExprKind::SizeOfValue(_)
57+
| ExprKind::AlignOf(_)
5558
| ExprKind::IntegerPromote(_) => (),
5659
}
5760

src/c/translate/expr/mod.rs

+18-15
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ use crate::{
2121
pub fn translate_expr(ctx: &mut TranslateCtx, expr: &Expr) -> Result<ast::Expr, ParseError> {
2222
Ok(match &expr.kind {
2323
ExprKind::Integer(integer) => translate_expr_integer(integer, expr.source)?,
24-
ExprKind::Float(_, _) => todo!(),
24+
ExprKind::Float(_, _) => todo!("translate_expr float"),
2525
ExprKind::StringLiteral(encoding, content) => {
2626
translate_expr_string(encoding, content, expr.source)?
2727
}
2828
ExprKind::Bool(x) => ast::ExprKind::Boolean(*x).at(expr.source),
29-
ExprKind::Nullptr => todo!(),
30-
ExprKind::Character(_, _) => todo!(),
31-
ExprKind::Compound(_) => todo!(),
29+
ExprKind::Nullptr => todo!("translate_expr nullptr"),
30+
ExprKind::Character(_, _) => todo!("translate_expr character"),
31+
ExprKind::Compound(_) => todo!("translate_expr compound"),
3232
ExprKind::BinaryOperation(operation) => {
3333
let left = translate_expr(ctx, &operation.left)?;
3434
let right = translate_expr(ctx, &operation.right)?;
@@ -55,17 +55,17 @@ pub fn translate_expr(ctx: &mut TranslateCtx, expr: &Expr) -> Result<ast::Expr,
5555
BinaryOperator::Multiply => ast::BasicBinaryOperator::Multiply.into(),
5656
BinaryOperator::Divide => ast::BasicBinaryOperator::Divide.into(),
5757
BinaryOperator::Modulus => ast::BasicBinaryOperator::Modulus.into(),
58-
BinaryOperator::Assign => todo!(),
59-
BinaryOperator::AddAssign => todo!(),
60-
BinaryOperator::SubtractAssign => todo!(),
61-
BinaryOperator::MultiplyAssign => todo!(),
62-
BinaryOperator::DivideAssign => todo!(),
63-
BinaryOperator::ModulusAssign => todo!(),
64-
BinaryOperator::LeftShiftAssign => todo!(),
65-
BinaryOperator::RightShiftAssign => todo!(),
66-
BinaryOperator::BitAndAssign => todo!(),
67-
BinaryOperator::BitXorAssign => todo!(),
68-
BinaryOperator::BitOrAssign => todo!(),
58+
BinaryOperator::Assign => todo!("translate_expr assign"),
59+
BinaryOperator::AddAssign => todo!("translate_expr add assign"),
60+
BinaryOperator::SubtractAssign => todo!("translate_expr subtract assign"),
61+
BinaryOperator::MultiplyAssign => todo!("translate_expr multiply assign"),
62+
BinaryOperator::DivideAssign => todo!("translate_expr divide assign"),
63+
BinaryOperator::ModulusAssign => todo!("translate_expr modulus assign"),
64+
BinaryOperator::LeftShiftAssign => todo!("translate_expr left shift assign"),
65+
BinaryOperator::RightShiftAssign => todo!("translate_expr right shift assign"),
66+
BinaryOperator::BitAndAssign => todo!("translate_expr bitwise-and assign"),
67+
BinaryOperator::BitXorAssign => todo!("translate_expr bitwise-xor assign"),
68+
BinaryOperator::BitOrAssign => todo!("translate_expr bitwise-ox assign"),
6969
};
7070

7171
match operator {
@@ -93,6 +93,8 @@ pub fn translate_expr(ctx: &mut TranslateCtx, expr: &Expr) -> Result<ast::Expr,
9393
ExprKind::Cast(_) => todo!(),
9494
ExprKind::Subscript(_) => todo!(),
9595
ExprKind::Field(_) => todo!(),
96+
ExprKind::PreIncrement(_) => todo!(),
97+
ExprKind::PreDecrement(_) => todo!(),
9698
ExprKind::PostIncrement(_) => todo!(),
9799
ExprKind::PostDecrement(_) => todo!(),
98100
ExprKind::Identifier(name) => {
@@ -165,6 +167,7 @@ pub fn translate_expr(ctx: &mut TranslateCtx, expr: &Expr) -> Result<ast::Expr,
165167
ExprKind::SizeOfValue(value) => {
166168
ast::ExprKind::SizeOfValue(Box::new(translate_expr(ctx, value)?)).at(expr.source)
167169
}
170+
ExprKind::AlignOf(_) => todo!("translate_expr AlignOf"),
168171
ExprKind::IntegerPromote(value) => {
169172
ast::ExprKind::IntegerPromote(Box::new(translate_expr(ctx, value)?)).at(expr.source)
170173
}

src/c/translate/function.rs

+20-16
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ fn translate_compound_statement(
124124
let mut stmts = vec![];
125125

126126
for block_item in &compound_statement.statements {
127-
stmts.push(translate_block_item(ctx, block_item)?);
127+
stmts.extend(translate_block_item(ctx, block_item)?);
128128
}
129129

130130
Ok(stmts)
@@ -133,21 +133,23 @@ fn translate_compound_statement(
133133
fn translate_block_item(
134134
ctx: &mut TranslateCtx,
135135
block_item: &BlockItem,
136-
) -> Result<ast::Stmt, ParseError> {
136+
) -> Result<Option<ast::Stmt>, ParseError> {
137137
match &block_item.kind {
138138
BlockItemKind::Declaration(declaration) => match declaration {
139139
Declaration::Common(_) => todo!("translate_block_item common declaration"),
140-
Declaration::StaticAssert(static_assert) => Ok(ast::ExprKind::StaticAssert(
141-
Box::new(translate_expr(ctx, &static_assert.condition.value)?),
142-
static_assert.message.clone(),
143-
)
144-
.at(block_item.source)
145-
.stmt()),
140+
Declaration::StaticAssert(static_assert) => Ok(Some(
141+
ast::ExprKind::StaticAssert(
142+
Box::new(translate_expr(ctx, &static_assert.condition.value)?),
143+
static_assert.message.clone(),
144+
)
145+
.at(block_item.source)
146+
.stmt(),
147+
)),
146148
Declaration::Attribute(_) => todo!("translate_block_item attribute declaration"),
147149
},
148-
BlockItemKind::UnlabeledStatement(unlabeled_statement) => {
149-
translate_unlabeled_statement(ctx, unlabeled_statement, block_item.source)
150-
}
150+
BlockItemKind::UnlabeledStatement(unlabeled_statement) => Ok(
151+
translate_unlabeled_statement(ctx, unlabeled_statement, block_item.source)?,
152+
),
151153
BlockItemKind::Label(_) => todo!("translate_block_item label"),
152154
}
153155
}
@@ -156,7 +158,7 @@ fn translate_unlabeled_statement(
156158
ctx: &mut TranslateCtx,
157159
unlabeled_statement: &UnlabeledStatement,
158160
source: Source,
159-
) -> Result<ast::Stmt, ParseError> {
161+
) -> Result<Option<ast::Stmt>, ParseError> {
160162
match unlabeled_statement {
161163
UnlabeledStatement::ExprStatement(expr_statement) => {
162164
translate_expr_statement(ctx, expr_statement)
@@ -172,7 +174,7 @@ fn translate_unlabeled_statement(
172174
));
173175
}
174176

175-
translate_jump_statement(ctx, jump_statement, source)
177+
Ok(Some(translate_jump_statement(ctx, jump_statement, source)?))
176178
}
177179
}
178180
}
@@ -200,9 +202,9 @@ fn translate_jump_statement(
200202
fn translate_expr_statement(
201203
ctx: &mut TranslateCtx,
202204
expr_statement: &ExprStatement,
203-
) -> Result<ast::Stmt, ParseError> {
205+
) -> Result<Option<ast::Stmt>, ParseError> {
204206
match expr_statement {
205-
ExprStatement::Empty => todo!(),
207+
ExprStatement::Empty => Ok(None),
206208
ExprStatement::Normal(attributes, expr) => {
207209
if !attributes.is_empty() {
208210
return Err(ParseError::message(
@@ -211,7 +213,9 @@ fn translate_expr_statement(
211213
));
212214
}
213215

214-
Ok(ast::StmtKind::Expr(translate_expr(ctx, expr)?).at(expr.source))
216+
Ok(Some(
217+
ast::StmtKind::Expr(translate_expr(ctx, expr)?).at(expr.source),
218+
))
215219
}
216220
}
217221
}

src/interpreter/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,9 @@ impl<'a, S: SyscallHandler> Interpreter<'a, S> {
206206
let subject_pointer = self.eval(&registers, subject_pointer).as_u64().unwrap();
207207
Value::Literal(ir::Literal::Unsigned64(subject_pointer + offset))
208208
}
209-
ir::Instr::ArrayAccess { .. } => todo!(),
209+
ir::Instr::ArrayAccess { .. } => {
210+
todo!("Interpreter / ir::Instruction::ArrayAccess")
211+
}
210212
ir::Instr::StructLiteral(ty, values) => {
211213
let mut field_values = Vec::with_capacity(values.len());
212214

src/interpreter/size_of.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ pub fn size_of(ir_type: &ir::Type, ir_module: &ir::Module) -> u64 {
3232
.iter()
3333
.fold(0, |acc, field| acc + size_of(&field.ir_type, ir_module))
3434
}
35-
ir::Type::FuncPtr => todo!(),
36-
ir::Type::FixedArray(_) => todo!(),
35+
ir::Type::FuncPtr => todo!("size_of ir::FuncPtr"),
36+
ir::Type::FixedArray(_) => todo!("size_of ir::FixedArray"),
3737
ir::Type::Vector(_) => todo!("interpreting vector types not supported yet"),
3838
ir::Type::Complex(_) => todo!("interpreting complex numeric types not support yet"),
3939
ir::Type::Atomic(inner) => size_of(inner, ir_module),

0 commit comments

Comments
 (0)