Skip to content
Merged
Show file tree
Hide file tree
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
24 changes: 19 additions & 5 deletions src/parser/cxx/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1417,17 +1417,25 @@ auto Parser::parse_id_expression(IdExpressionAST*& yyast,

if (unqualifiedId) {
auto name = convertName(unqualifiedId);
ast->symbol = Lookup{scope_}(nestedNameSpecifier, name);
const Name* componentName = name;
if (auto templateId = name_cast<TemplateId>(name))
componentName = templateId->name();
ast->symbol = Lookup{scope_}(nestedNameSpecifier, componentName);
}

if (ctx == IdExpressionContext::kExpression) {
if (ast->symbol) {
ast->type = control_->remove_reference(ast->symbol->type());

if (auto enumerator = symbol_cast<EnumeratorSymbol>(ast->symbol)) {
if (auto conceptSymbol = symbol_cast<ConceptSymbol>(ast->symbol)) {
ast->type = control_->getBoolType();
ast->valueCategory = ValueCategory::kPrValue;
} else {
ast->valueCategory = ValueCategory::kLValue;
ast->type = control_->remove_reference(ast->symbol->type());

if (auto enumerator = symbol_cast<EnumeratorSymbol>(ast->symbol)) {
ast->valueCategory = ValueCategory::kPrValue;
} else {
ast->valueCategory = ValueCategory::kLValue;
}
}
}
}
Expand Down Expand Up @@ -5650,6 +5658,11 @@ auto Parser::parse_named_type_specifier(SpecifierAST*& yyast, DeclSpecs& specs)
}

if (auto templateId = ast_cast<SimpleTemplateIdAST>(unqualifiedId)) {
if (auto conceptSymbol =
symbol_cast<ConceptSymbol>(templateId->primaryTemplateSymbol)) {
if (!lookat(TokenKind::T_AUTO)) return false;
}

if (auto symbol = instantiate(templateId)) {
specs.type = symbol->type();
}
Expand Down Expand Up @@ -10733,6 +10746,7 @@ void Parser::completeFunctionDefinition(FunctionDefinitionAST* ast) {
}

void Parser::check(ExpressionAST* ast) {
if (inTemplate_) return;
TypeChecker check{unit};
check.setScope(scope_);
check.setReportErrors(config_.checkTypes);
Expand Down
17 changes: 15 additions & 2 deletions src/parser/cxx/type_checker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,9 @@ void TypeChecker::Visitor::operator()(UnaryExpressionAST* ast) {
}

if (!is_glvalue(ast->expression)) {
error(ast->opLoc,
std::format("cannot take the address of an rvalue of type '{}'",
to_string(ast->expression->type)));
break;
}

Expand Down Expand Up @@ -542,6 +545,8 @@ void TypeChecker::Visitor::operator()(UnaryExpressionAST* ast) {

case TokenKind::T_PLUS_PLUS: {
if (!is_glvalue(ast->expression)) {
error(ast->opLoc, std::format("cannot increment an rvalue of type '{}'",
to_string(ast->expression->type)));
break;
}

Expand All @@ -554,18 +559,23 @@ void TypeChecker::Visitor::operator()(UnaryExpressionAST* ast) {
}

if (auto ptrTy = type_cast<PointerType>(ty)) {
if (ptrTy && !control()->is_void(ptrTy->elementType())) {
if (!control()->is_void(ptrTy->elementType())) {
ast->type = ptrTy;
ast->valueCategory = ValueCategory::kLValue;
break;
}
}

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

case TokenKind::T_MINUS_MINUS: {
if (!is_glvalue(ast->expression)) {
break;
error(ast->opLoc, std::format("cannot decrement an rvalue of type '{}'",
to_string(ast->expression->type)));
break;
}

auto ty = ast->expression->type;
Expand All @@ -580,9 +590,12 @@ void TypeChecker::Visitor::operator()(UnaryExpressionAST* ast) {
if (ptrTy && !control()->is_void(ptrTy->elementType())) {
ast->type = ptrTy;
ast->valueCategory = ValueCategory::kLValue;
break;
}
}

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

Expand Down
16 changes: 6 additions & 10 deletions tests/unit_tests/ast/template_lambda_01.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,9 @@ struct S {
// CHECK-NEXT: return-statement
// CHECK-NEXT: expression: binary-expression
// CHECK-NEXT: op: +
// CHECK-NEXT: left-expression: implicit-cast-expression [prvalue T1]
// CHECK-NEXT: cast-kind: lvalue-to-rvalue-conversion
// CHECK-NEXT: expression: id-expression [lvalue T1]
// CHECK-NEXT: unqualified-id: name-id
// CHECK-NEXT: identifier: a
// CHECK-NEXT: right-expression: implicit-cast-expression [prvalue T2]
// CHECK-NEXT: cast-kind: lvalue-to-rvalue-conversion
// CHECK-NEXT: expression: id-expression [lvalue T2]
// CHECK-NEXT: unqualified-id: name-id
// CHECK-NEXT: identifier: b
// CHECK-NEXT: left-expression: id-expression [lvalue T1]
// CHECK-NEXT: unqualified-id: name-id
// CHECK-NEXT: identifier: a
// CHECK-NEXT: right-expression: id-expression [lvalue T2]
// CHECK-NEXT: unqualified-id: name-id
// CHECK-NEXT: identifier: b
15 changes: 14 additions & 1 deletion tests/unit_tests/sema/incr_01.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,18 @@ auto main() -> int {
static_assert(__is_lvalue_reference(decltype(++pr)));
static_assert(__is_lvalue_reference(decltype(--pr)));

// clang-format off

int a[10];

++(+a); // expected-error {{cannot increment an rvalue of type 'int*'}}

++a; // expected-error {{cannot increment a value of type 'int [10]'}}

--(+a); // expected-error {{cannot decrement an rvalue of type 'int*'}}

--a; // expected-error {{cannot decrement a value of type 'int [10]'}}

// clang-format on
return 0;
}
}