Skip to content

Commit e51b617

Browse files
committed
Compute types of unary minus operators
Signed-off-by: Roberto Raggi <[email protected]>
1 parent d919941 commit e51b617

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/parser/cxx/parser.cc

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1562,6 +1562,7 @@ auto Parser::parse_type_nested_name_specifier(NestedNameSpecifierAST*& yyast,
15621562
return true;
15631563
}
15641564

1565+
namespace {
15651566
struct IsReferencingTemplateParameter {
15661567
Parser& p;
15671568
int depth = 0;
@@ -1592,6 +1593,7 @@ struct IsReferencingTemplateParameter {
15921593
return false;
15931594
}
15941595
};
1596+
} // namespace
15951597

15961598
auto Parser::parse_template_nested_name_specifier(
15971599
NestedNameSpecifierAST*& yyast, NestedNameSpecifierContext ctx, int depth)
@@ -2930,7 +2932,7 @@ auto Parser::parse_unop_expression(ExpressionAST*& yyast,
29302932
break;
29312933
}
29322934

2933-
case cxx::TokenKind::T_PLUS: {
2935+
case TokenKind::T_PLUS: {
29342936
ExpressionAST* expr = ast->expression;
29352937
ensure_prvalue(expr);
29362938
auto ty = control_->remove_cvref(expr->type);
@@ -2946,6 +2948,21 @@ auto Parser::parse_unop_expression(ExpressionAST*& yyast,
29462948
break;
29472949
}
29482950

2951+
case TokenKind::T_MINUS: {
2952+
ExpressionAST* expr = ast->expression;
2953+
ensure_prvalue(expr);
2954+
auto ty = control_->remove_cvref(expr->type);
2955+
if (control_->is_arithmetic_or_unscoped_enum(ty)) {
2956+
if (control_->is_integral_or_unscoped_enum(ty)) {
2957+
(void)integral_promotion(expr);
2958+
}
2959+
ast->expression = expr;
2960+
ast->type = expr->type;
2961+
ast->valueCategory = ValueCategory::kPrValue;
2962+
}
2963+
break;
2964+
}
2965+
29492966
default:
29502967
break;
29512968
} // switch
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %cxx -verify -fcheck %s
2+
3+
auto main() -> int {
4+
static_assert(__is_same(decltype(-'a'), int));
5+
static_assert(__is_same(decltype(-1), int));
6+
static_assert(__is_same(decltype(-1l), long));
7+
static_assert(__is_same(decltype(-1ll), long long));
8+
static_assert(__is_same(decltype(-1ul), unsigned long));
9+
static_assert(__is_same(decltype(-1ull), unsigned long long));
10+
static_assert(__is_same(decltype(-1.0f), float));
11+
static_assert(__is_same(decltype(-1.0), double));
12+
13+
short x{};
14+
static_assert(__is_same(decltype(-x), int));
15+
16+
short& y = x;
17+
static_assert(__is_same(decltype(-y), int));
18+
19+
unsigned u{};
20+
static_assert(__is_same(decltype(-u), unsigned));
21+
22+
return 0;
23+
}

0 commit comments

Comments
 (0)