diff --git a/clang/lib/AST/Interp/ByteCodeEmitter.cpp b/clang/lib/AST/Interp/ByteCodeEmitter.cpp index 60d8afecb2b3b..e1b954fcc6848 100644 --- a/clang/lib/AST/Interp/ByteCodeEmitter.cpp +++ b/clang/lib/AST/Interp/ByteCodeEmitter.cpp @@ -108,8 +108,12 @@ Function *ByteCodeEmitter::compileFunc(const FunctionDecl *FuncDecl) { this->LambdaCaptures[Cap.first] = { Offset, Cap.second->getType()->isReferenceType()}; } - if (LTC) - this->LambdaThisCapture = R->getField(LTC)->Offset; + if (LTC) { + QualType CaptureType = R->getField(LTC)->Decl->getType(); + this->LambdaThisCapture = {R->getField(LTC)->Offset, + CaptureType->isReferenceType() || + CaptureType->isPointerType()}; + } } } diff --git a/clang/lib/AST/Interp/ByteCodeEmitter.h b/clang/lib/AST/Interp/ByteCodeEmitter.h index 03de286582c91..548769329b7f8 100644 --- a/clang/lib/AST/Interp/ByteCodeEmitter.h +++ b/clang/lib/AST/Interp/ByteCodeEmitter.h @@ -62,7 +62,7 @@ class ByteCodeEmitter { /// Lambda captures. llvm::DenseMap LambdaCaptures; /// Offset of the This parameter in a lambda record. - unsigned LambdaThisCapture = 0; + ParamOffset LambdaThisCapture{0, false}; /// Local descriptors. llvm::SmallVector, 2> Descriptors; diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp index 388da0e324c73..63ab80f59dac4 100644 --- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp +++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp @@ -2934,8 +2934,11 @@ bool ByteCodeExprGen::VisitCXXThisExpr(const CXXThisExpr *E) { if (DiscardResult) return true; - if (this->LambdaThisCapture > 0) - return this->emitGetThisFieldPtr(this->LambdaThisCapture, E); + if (this->LambdaThisCapture.Offset > 0) { + if (this->LambdaThisCapture.IsPtr) + return this->emitGetThisFieldPtr(this->LambdaThisCapture.Offset, E); + return this->emitGetPtrThisField(this->LambdaThisCapture.Offset, E); + } return this->emitThis(E); } diff --git a/clang/lib/AST/Interp/EvalEmitter.h b/clang/lib/AST/Interp/EvalEmitter.h index 5b02b992fef6f..116f1d6fc134a 100644 --- a/clang/lib/AST/Interp/EvalEmitter.h +++ b/clang/lib/AST/Interp/EvalEmitter.h @@ -73,7 +73,7 @@ class EvalEmitter : public SourceMapper { /// Lambda captures. llvm::DenseMap LambdaCaptures; /// Offset of the This parameter in a lambda record. - unsigned LambdaThisCapture = 0; + ParamOffset LambdaThisCapture{0, false}; /// Local descriptors. llvm::SmallVector, 2> Descriptors; diff --git a/clang/test/AST/Interp/lambda.cpp b/clang/test/AST/Interp/lambda.cpp index a5e0d0f1fd9f4..d056bb304eeb3 100644 --- a/clang/test/AST/Interp/lambda.cpp +++ b/clang/test/AST/Interp/lambda.cpp @@ -235,3 +235,16 @@ namespace LambdaToAPValue { static_assert(g() == f(), ""); } } + +namespace ns2_capture_this_byval { + struct S { + int s; + constexpr S(int s) : s{s} { } + constexpr auto f(S o) { + return [*this,o] (auto a) { return s + o.s + a.s; }; + } + }; + + constexpr auto L = S{5}.f(S{10}); + static_assert(L(S{100}) == 115, ""); +} // end test_captures_1::ns2_capture_this_byval