Skip to content

Improve checks of builtin postfix increment expressions #530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion src/parser/cxx/type_checker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,43 @@ void TypeChecker::Visitor::operator()(MemberExpressionAST* ast) {
if (check_member_access(ast)) return;
}

void TypeChecker::Visitor::operator()(PostIncrExpressionAST* ast) {}
void TypeChecker::Visitor::operator()(PostIncrExpressionAST* ast) {
if (control()->is_class(ast->baseExpression->type)) return;

const std::string_view op =
ast->op == TokenKind::T_PLUS_PLUS ? "increment" : "decrement";

// builtin postfix increment operator
if (!is_glvalue(ast->baseExpression)) {
error(ast->opLoc, std::format("cannot {} an rvalue of type '{}'", op,
to_string(ast->baseExpression->type)));
return;
}

auto incr_arithmetic = [&]() {
if (!control()->is_arithmetic(ast->baseExpression->type)) return false;
auto ty = control()->remove_cv(ast->baseExpression->type);
if (type_cast<BoolType>(ty)) return false;

ast->type = ty;
ast->valueCategory = ValueCategory::kPrValue;
return true;
};

auto incr_pointer = [&]() {
if (!control()->is_pointer(ast->baseExpression->type)) return false;
auto ty = control()->remove_cv(ast->baseExpression->type);
ast->type = ty;
ast->valueCategory = ValueCategory::kPrValue;
return true;
};

if (incr_arithmetic()) return;
if (incr_pointer()) return;

error(ast->opLoc, std::format("cannot {} a value of type '{}'", op,
to_string(ast->baseExpression->type)));
}

void TypeChecker::Visitor::operator()(CppCastExpressionAST* ast) {
check_cpp_cast_expression(ast);
Expand Down