[PATCH] D154262: [clang][Interp] LambdaThisCaptures

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 30 14:22:13 PDT 2023


tbaeder created this revision.
tbaeder added reviewers: aaron.ballman, erichkeane, tahonermann, shafik.
Herald added a project: All.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Fix a FIXME from an earlier commit.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154262

Files:
  clang/lib/AST/Interp/ByteCodeEmitter.cpp
  clang/lib/AST/Interp/ByteCodeEmitter.h
  clang/lib/AST/Interp/ByteCodeExprGen.cpp
  clang/lib/AST/Interp/EvalEmitter.h
  clang/test/AST/Interp/lambda.cpp


Index: clang/test/AST/Interp/lambda.cpp
===================================================================
--- clang/test/AST/Interp/lambda.cpp
+++ clang/test/AST/Interp/lambda.cpp
@@ -107,3 +107,23 @@
   static_assert(foo() == 1); // expected-error {{not an integral constant expression}}
 }
 
+namespace ThisCapture {
+  class Foo {
+  public:
+    int b = 32;
+    int a;
+
+    constexpr Foo() : a([this](){ return b + 1;}()) {}
+
+    constexpr int Aplus2() const {
+      auto F = [this]() {
+        return a + 2;
+      };
+
+      return F();
+    }
+  };
+  constexpr Foo F;
+  static_assert(F.a == 33, "");
+  static_assert(F.Aplus2() == (33 + 2), "");
+}
Index: clang/lib/AST/Interp/EvalEmitter.h
===================================================================
--- clang/lib/AST/Interp/EvalEmitter.h
+++ clang/lib/AST/Interp/EvalEmitter.h
@@ -79,7 +79,7 @@
   /// Lambda captures.
   /// Map from Decl* to [Offset, IsReference] pair.
   llvm::DenseMap<const ValueDecl *, std::pair<unsigned, bool>> LambdaCaptures;
-  unsigned LambdaThisCapture;
+  unsigned LambdaThisCapture = 0;
   /// Local descriptors.
   llvm::SmallVector<SmallVector<Local, 8>, 2> Descriptors;
 
Index: clang/lib/AST/Interp/ByteCodeExprGen.cpp
===================================================================
--- clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -1838,6 +1838,10 @@
 bool ByteCodeExprGen<Emitter>::VisitCXXThisExpr(const CXXThisExpr *E) {
   if (DiscardResult)
     return true;
+
+  if (this->LambdaThisCapture > 0)
+    return this->emitGetThisFieldPtr(this->LambdaThisCapture, E);
+
   return this->emitThis(E);
 }
 
Index: clang/lib/AST/Interp/ByteCodeEmitter.h
===================================================================
--- clang/lib/AST/Interp/ByteCodeEmitter.h
+++ clang/lib/AST/Interp/ByteCodeEmitter.h
@@ -73,7 +73,7 @@
   /// Lambda captures.
   /// Map from Decl* to [Offset, IsReference] pair.
   llvm::DenseMap<const ValueDecl *, std::pair<unsigned, bool>> LambdaCaptures;
-  unsigned LambdaThisCapture;
+  unsigned LambdaThisCapture = 0;
   /// Local descriptors.
   llvm::SmallVector<SmallVector<Local, 8>, 2> Descriptors;
 
Index: clang/lib/AST/Interp/ByteCodeEmitter.cpp
===================================================================
--- clang/lib/AST/Interp/ByteCodeEmitter.cpp
+++ clang/lib/AST/Interp/ByteCodeEmitter.cpp
@@ -63,8 +63,8 @@
         this->LambdaCaptures[Cap.first] = {
             Offset, Cap.second->getType()->isReferenceType()};
       }
-      // FIXME: LambdaThisCapture
-      (void)LTC;
+      if (LTC)
+        this->LambdaThisCapture = R->getField(LTC)->Offset;
     }
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154262.536427.patch
Type: text/x-patch
Size: 2683 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230630/33d63498/attachment-0001.bin>


More information about the cfe-commits mailing list