Skip to content

Commit 4b4e6b4

Browse files
committed
Compute the type of this expressions
Signed-off-by: Roberto Raggi <[email protected]>
1 parent 7c0376a commit 4b4e6b4

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/parser/cxx/parser.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,6 +2009,31 @@ auto Parser::parse_this_expression(ExpressionAST*& yyast) -> bool {
20092009
auto ast = make_node<ThisExpressionAST>(pool_);
20102010
yyast = ast;
20112011
ast->thisLoc = thisLoc;
2012+
ast->valueCategory = ValueCategory::kPrValue;
2013+
2014+
for (auto current = scope_; current; current = current->parent()) {
2015+
if (auto classSymbol = symbol_cast<ClassSymbol>(current->owner())) {
2016+
// maybe a this expression in a field initializer
2017+
ast->type = control_->getPointerType(classSymbol->type());
2018+
break;
2019+
}
2020+
2021+
if (auto functionSymbol = symbol_cast<FunctionSymbol>(current->owner())) {
2022+
if (auto classSymbol =
2023+
symbol_cast<ClassSymbol>(functionSymbol->enclosingSymbol())) {
2024+
auto functionType = type_cast<FunctionType>(functionSymbol->type());
2025+
const auto cv = functionType->cvQualifiers();
2026+
if (cv != CvQualifiers::kNone) {
2027+
auto elementType = control_->getQualType(classSymbol->type(), cv);
2028+
ast->type = control_->getPointerType(elementType);
2029+
} else {
2030+
ast->type = control_->getPointerType(classSymbol->type());
2031+
}
2032+
}
2033+
2034+
break;
2035+
}
2036+
}
20122037

20132038
return true;
20142039
}

tests/unit_tests/sema/class_01.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: %cxx -verify -fcheck %s
2+
3+
struct X {
4+
void f() { static_assert(__is_same(decltype(this), X*)); }
5+
6+
void f() const { static_assert(__is_same(decltype(this), const X*)); }
7+
8+
void f() volatile { static_assert(__is_same(decltype(this), volatile X*)); }
9+
10+
void f() const volatile {
11+
static_assert(__is_same(decltype(this), const volatile X*));
12+
}
13+
14+
void g();
15+
void g() const;
16+
void g() volatile;
17+
void g() const volatile;
18+
};
19+
20+
void X::g() { static_assert(__is_same(decltype(this), X*)); }
21+
22+
void X::g() const { static_assert(__is_same(decltype(this), const X*)); }
23+
24+
void X::g() volatile { static_assert(__is_same(decltype(this), volatile X*)); }
25+
26+
void X::g() const volatile {
27+
static_assert(__is_same(decltype(this), const volatile X*));
28+
}

0 commit comments

Comments
 (0)