Skip to content

Commit cb771cf

Browse files
committed
Generate IR code for the sizeof expressions
Signed-off-by: Roberto Raggi <roberto.raggi@gmail.com>
1 parent 3b5aa62 commit cb771cf

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

src/mlir/cxx/mlir/codegen_expressions.cc

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,25 +1135,33 @@ auto Codegen::ExpressionVisitor::operator()(AwaitExpressionAST* ast)
11351135

11361136
auto Codegen::ExpressionVisitor::operator()(SizeofExpressionAST* ast)
11371137
-> ExpressionResult {
1138+
if (auto size = ast->value) {
1139+
auto resultlType = gen.convertType(ast->type);
1140+
auto loc = gen.getLocation(ast->firstSourceLocation());
1141+
auto op = mlir::cxx::IntConstantOp::create(gen.builder_, loc, resultlType,
1142+
size.value());
1143+
return {op};
1144+
}
1145+
11381146
auto op =
11391147
gen.emitTodoExpr(ast->firstSourceLocation(), to_string(ast->kind()));
11401148

1141-
#if false
1142-
auto expressionResult = gen.expression(ast->expression);
1143-
#endif
1144-
11451149
return {op};
11461150
}
11471151

11481152
auto Codegen::ExpressionVisitor::operator()(SizeofTypeExpressionAST* ast)
11491153
-> ExpressionResult {
1154+
if (auto size = ast->value) {
1155+
auto resultlType = gen.convertType(ast->type);
1156+
auto loc = gen.getLocation(ast->firstSourceLocation());
1157+
auto op = mlir::cxx::IntConstantOp::create(gen.builder_, loc, resultlType,
1158+
size.value());
1159+
return {op};
1160+
}
1161+
11501162
auto op =
11511163
gen.emitTodoExpr(ast->firstSourceLocation(), to_string(ast->kind()));
11521164

1153-
#if false
1154-
auto typeIdResult = gen.typeId(ast->typeId);
1155-
#endif
1156-
11571165
return {op};
11581166
}
11591167

src/parser/cxx/ast.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2300,6 +2300,7 @@ class SizeofExpressionAST final : public ExpressionAST {
23002300

23012301
SourceLocation sizeofLoc;
23022302
ExpressionAST* expression = nullptr;
2303+
std::optional<std::int64_t> value;
23032304

23042305
void accept(ASTVisitor* visitor) override { visitor->visit(this); }
23052306

@@ -2317,6 +2318,7 @@ class SizeofTypeExpressionAST final : public ExpressionAST {
23172318
SourceLocation lparenLoc;
23182319
TypeIdAST* typeId = nullptr;
23192320
SourceLocation rparenLoc;
2321+
std::optional<std::int64_t> value;
23202322

23212323
void accept(ASTVisitor* visitor) override { visitor->visit(this); }
23222324

src/parser/cxx/type_checker.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <cxx/ast.h>
2525
#include <cxx/control.h>
2626
#include <cxx/literals.h>
27+
#include <cxx/memory_layout.h>
2728
#include <cxx/name_lookup.h>
2829
#include <cxx/names.h>
2930
#include <cxx/symbols.h>
@@ -1040,10 +1041,18 @@ void TypeChecker::Visitor::operator()(AwaitExpressionAST* ast) {}
10401041

10411042
void TypeChecker::Visitor::operator()(SizeofExpressionAST* ast) {
10421043
ast->type = control()->getSizeType();
1044+
1045+
if (ast->expression) {
1046+
ast->value = control()->memoryLayout()->sizeOf(ast->expression->type);
1047+
}
10431048
}
10441049

10451050
void TypeChecker::Visitor::operator()(SizeofTypeExpressionAST* ast) {
10461051
ast->type = control()->getSizeType();
1052+
1053+
if (ast->typeId) {
1054+
ast->value = control()->memoryLayout()->sizeOf(ast->typeId->type);
1055+
}
10471056
}
10481057

10491058
void TypeChecker::Visitor::operator()(SizeofPackExpressionAST* ast) {}

0 commit comments

Comments
 (0)