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
25 changes: 25 additions & 0 deletions src/parser/cxx/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2009,6 +2009,31 @@ auto Parser::parse_this_expression(ExpressionAST*& yyast) -> bool {
auto ast = make_node<ThisExpressionAST>(pool_);
yyast = ast;
ast->thisLoc = thisLoc;
ast->valueCategory = ValueCategory::kPrValue;

for (auto current = scope_; current; current = current->parent()) {
if (auto classSymbol = symbol_cast<ClassSymbol>(current->owner())) {
// maybe a this expression in a field initializer
ast->type = control_->getPointerType(classSymbol->type());
break;
}

if (auto functionSymbol = symbol_cast<FunctionSymbol>(current->owner())) {
if (auto classSymbol =
symbol_cast<ClassSymbol>(functionSymbol->enclosingSymbol())) {
auto functionType = type_cast<FunctionType>(functionSymbol->type());
const auto cv = functionType->cvQualifiers();
if (cv != CvQualifiers::kNone) {
auto elementType = control_->getQualType(classSymbol->type(), cv);
ast->type = control_->getPointerType(elementType);
} else {
ast->type = control_->getPointerType(classSymbol->type());
}
}

break;
}
}

return true;
}
Expand Down
28 changes: 28 additions & 0 deletions tests/unit_tests/sema/class_01.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// RUN: %cxx -verify -fcheck %s

struct X {
void f() { static_assert(__is_same(decltype(this), X*)); }

void f() const { static_assert(__is_same(decltype(this), const X*)); }

void f() volatile { static_assert(__is_same(decltype(this), volatile X*)); }

void f() const volatile {
static_assert(__is_same(decltype(this), const volatile X*));
}

void g();
void g() const;
void g() volatile;
void g() const volatile;
};

void X::g() { static_assert(__is_same(decltype(this), X*)); }

void X::g() const { static_assert(__is_same(decltype(this), const X*)); }

void X::g() volatile { static_assert(__is_same(decltype(this), volatile X*)); }

void X::g() const volatile {
static_assert(__is_same(decltype(this), const volatile X*));
}