Skip to content

Commit d8c2e86

Browse files
committed
Check new and delete expressions
1 parent 75d0402 commit d8c2e86

File tree

5 files changed

+45
-8
lines changed

5 files changed

+45
-8
lines changed

src/parser/cxx/ast.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,6 +1921,7 @@ class NewExpressionAST final : public ExpressionAST {
19211921
DeclaratorAST* declarator = nullptr;
19221922
SourceLocation rparenLoc;
19231923
NewInitializerAST* newInitalizer = nullptr;
1924+
const Type* objectType = nullptr;
19241925

19251926
void accept(ASTVisitor* visitor) override { visitor->visit(this); }
19261927

src/parser/cxx/parser.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2558,13 +2558,15 @@ auto Parser::parse_new_expression(ExpressionAST*& yyast, const ExprContext& ctx)
25582558
lookahead.commit();
25592559

25602560
NewInitializerAST* newInitializer = nullptr;
2561-
parse_optional_new_initializer(newInitializer, ctx);
2561+
parse_optional_new_initializer(newInitializer, decl, ctx);
25622562

25632563
ast->lparenLoc = lparenLoc;
25642564
ast->typeSpecifierList = typeSpecifierList;
25652565
ast->rparenLoc = rparenLoc;
25662566
ast->newInitalizer = newInitializer;
25672567

2568+
ast->objectType = getDeclaratorType(unit, declarator, decl.specs.type());
2569+
25682570
check(ast);
25692571

25702572
return true;
@@ -2580,7 +2582,9 @@ auto Parser::parse_new_expression(ExpressionAST*& yyast, const ExprContext& ctx)
25802582

25812583
(void)parse_declarator(ast->declarator, decl, DeclaratorKind::kNewDeclarator);
25822584

2583-
parse_optional_new_initializer(ast->newInitalizer, ctx);
2585+
parse_optional_new_initializer(ast->newInitalizer, decl, ctx);
2586+
2587+
ast->objectType = getDeclaratorType(unit, ast->declarator, decl.specs.type());
25842588

25852589
check(ast);
25862590

@@ -2611,6 +2615,7 @@ void Parser::parse_optional_new_placement(NewPlacementAST*& yyast,
26112615
}
26122616

26132617
void Parser::parse_optional_new_initializer(NewInitializerAST*& yyast,
2618+
Decl& decl,
26142619
const ExprContext& ctx) {
26152620
if (BracedInitListAST* bracedInitList = nullptr;
26162621
parse_braced_init_list(bracedInitList, ctx)) {

src/parser/cxx/parser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ class Parser final {
290290
const ExprContext& ctx) -> bool;
291291
void parse_optional_new_placement(NewPlacementAST*& yyast,
292292
const ExprContext& ctx);
293-
void parse_optional_new_initializer(NewInitializerAST*& yyast,
293+
void parse_optional_new_initializer(NewInitializerAST*& yyast, Decl& decl,
294294
const ExprContext& ctx);
295295
[[nodiscard]] auto parse_delete_expression(ExpressionAST*& yyast,
296296
const ExprContext& ctx) -> bool;

src/parser/cxx/type_checker.cc

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -666,11 +666,38 @@ void TypeChecker::Visitor::operator()(NoexceptExpressionAST* ast) {
666666
ast->type = control()->getBoolType();
667667
}
668668

669-
void TypeChecker::Visitor::operator()(NewExpressionAST* ast) {}
669+
void TypeChecker::Visitor::operator()(NewExpressionAST* ast) {
670+
// TODO: decay
671+
auto objectType = control()->remove_reference(ast->objectType);
672+
673+
if (auto arrayType = type_cast<BoundedArrayType>(ast->objectType)) {
674+
ast->type = control()->getPointerType(arrayType->elementType());
675+
} else if (auto unboundedType =
676+
type_cast<UnboundedArrayType>(ast->objectType)) {
677+
ast->type = control()->getPointerType(unboundedType->elementType());
678+
} else {
679+
ast->type = control()->getPointerType(ast->objectType);
680+
}
681+
682+
ast->valueCategory = ValueCategory::kPrValue;
683+
}
670684

671-
void TypeChecker::Visitor::operator()(DeleteExpressionAST* ast) {}
685+
void TypeChecker::Visitor::operator()(DeleteExpressionAST* ast) {
686+
ast->type = control()->getVoidType();
687+
ast->valueCategory = ValueCategory::kPrValue;
688+
}
672689

673-
void TypeChecker::Visitor::operator()(CastExpressionAST* ast) {}
690+
void TypeChecker::Visitor::operator()(CastExpressionAST* ast) {
691+
if (ast->typeId) {
692+
ast->type = control()->remove_reference(ast->typeId->type);
693+
if (control()->is_lvalue_reference(ast->typeId->type))
694+
ast->valueCategory = ValueCategory::kLValue;
695+
else if (control()->is_rvalue_reference(ast->typeId->type))
696+
ast->valueCategory = ValueCategory::kXValue;
697+
else
698+
ast->valueCategory = ValueCategory::kPrValue;
699+
}
700+
}
674701

675702
void TypeChecker::Visitor::operator()(ImplicitCastExpressionAST* ast) {}
676703

@@ -808,7 +835,11 @@ void TypeChecker::Visitor::operator()(TypeTraitExpressionAST* ast) {
808835

809836
void TypeChecker::Visitor::operator()(ConditionExpressionAST* ast) {}
810837

811-
void TypeChecker::Visitor::operator()(EqualInitializerAST* ast) {}
838+
void TypeChecker::Visitor::operator()(EqualInitializerAST* ast) {
839+
if (!ast->expression) return;
840+
ast->type = ast->expression->type;
841+
ast->valueCategory = ast->expression->valueCategory;
842+
}
812843

813844
void TypeChecker::Visitor::operator()(BracedInitListAST* ast) {}
814845

tests/unit_tests/ast/variadic_function_02.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ void ff(int count, ...) {
9494
// CHECK-NEXT: statement: compound-statement
9595
// CHECK-NEXT: statement-list
9696
// CHECK-NEXT: expression-statement
97-
// CHECK-NEXT: expression: cast-expression
97+
// CHECK-NEXT: expression: cast-expression [prvalue void]
9898
// CHECK-NEXT: type-id: type-id
9999
// CHECK-NEXT: type-specifier-list
100100
// CHECK-NEXT: void-type-specifier

0 commit comments

Comments
 (0)