]> granicus.if.org Git - clang/commit
[Cxx1z] Implement Lambda Capture of *this by Value as [=,*this] (P0018R3)
authorFaisal Vali <faisalv@yahoo.com>
Mon, 21 Mar 2016 09:25:37 +0000 (09:25 +0000)
committerFaisal Vali <faisalv@yahoo.com>
Mon, 21 Mar 2016 09:25:37 +0000 (09:25 +0000)
commit6078347237c150cf1f3a7cfd37129e31207d6325
treeaf46b5e625ad1cf65c4325ea68bcd04e2665d556
parent0b2ac6214c4ba0ed23916d552a07ec8acdb744c0
[Cxx1z] Implement Lambda Capture of *this by Value as [=,*this] (P0018R3)

Implement lambda capture of *this by copy.
For e.g.:
struct A {

  int d = 10;
  auto foo() { return [*this] (auto a) mutable { d+=a; return d; }; }

};

auto L = A{}.foo(); // A{}'s lifetime is gone.

// Below is still ok, because *this was captured by value.
assert(L(10) == 20);
assert(L(100) == 120);

If the capture was implicit, or [this] (i.e. *this was captured by reference), this code would be otherwise undefined.

Implementation Strategy:
  - amend the parser to accept *this in the lambda introducer
  - add a new king of capture LCK_StarThis
  - teach Sema::CheckCXXThisCapture to handle by copy captures of the
    enclosing object (i.e. *this)
  - when CheckCXXThisCapture does capture by copy, the corresponding
    initializer expression for the closure's data member
    direct-initializes it thus making a copy of '*this'.
  - in codegen, when assigning to CXXThisValue, if *this was captured by
    copy, make sure it points to the corresponding field member, and
    not, unlike when captured by reference, what the field member points
    to.
  - mark feature as implemented in svn

Much gratitude to Richard Smith for his carefully illuminating reviews!

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@263921 91177308-0d34-0410-b5e6-96231b3b80d8
22 files changed:
include/clang/AST/LambdaCapture.h
include/clang/Basic/DiagnosticParseKinds.td
include/clang/Basic/DiagnosticSemaKinds.td
include/clang/Basic/Lambda.h
include/clang/Sema/ScopeInfo.h
include/clang/Sema/Sema.h
lib/AST/ExprCXX.cpp
lib/AST/StmtPrinter.cpp
lib/AST/StmtProfile.cpp
lib/CodeGen/CodeGenFunction.cpp
lib/Parse/ParseExprCXX.cpp
lib/Sema/SemaDecl.cpp
lib/Sema/SemaExpr.cpp
lib/Sema/SemaExprCXX.cpp
lib/Sema/SemaLambda.cpp
lib/Sema/TreeTransform.h
lib/Serialization/ASTReaderDecl.cpp
lib/Serialization/ASTWriter.cpp
test/CXX/expr/expr.prim/expr.prim.lambda/p15-star-this-capture.cpp [new file with mode: 0644]
test/CodeGenCXX/cxx1z-lambda-star-this.cpp [new file with mode: 0644]
test/SemaCXX/cxx1z-lambda-star-this.cpp [new file with mode: 0644]
www/cxx_status.html