]> granicus.if.org Git - clang/commitdiff
Implement AST (de-)serialization for lambda expressions.
authorDouglas Gregor <dgregor@apple.com>
Tue, 14 Feb 2012 17:54:36 +0000 (17:54 +0000)
committerDouglas Gregor <dgregor@apple.com>
Tue, 14 Feb 2012 17:54:36 +0000 (17:54 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@150491 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/ExprCXX.h
include/clang/Serialization/ASTBitCodes.h
lib/AST/ExprCXX.cpp
lib/Serialization/ASTReaderDecl.cpp
lib/Serialization/ASTReaderStmt.cpp
lib/Serialization/ASTWriter.cpp
lib/Serialization/ASTWriterStmt.cpp
test/PCH/cxx11-lambdas.cpp [new file with mode: 0644]

index ba2233d23b275cafa362077a9bf1183fb9925ea0..c305faa8a9989fc70b33b9d47fc610b4aa03a703 100644 (file)
@@ -1168,6 +1168,14 @@ private:
              ArrayRef<unsigned> ArrayIndexStarts,
              SourceLocation ClosingBrace);
 
+  /// \brief Construct an empty lambda expression.
+  LambdaExpr(EmptyShell Empty, unsigned NumCaptures, bool HasArrayIndexVars)
+    : Expr(LambdaExprClass, Empty),
+      NumCaptures(NumCaptures), CaptureDefault(LCD_None), ExplicitParams(false),
+      ExplicitResultType(false), HasArrayIndexVars(true) { 
+    getStoredStmts()[NumCaptures] = 0;
+  }
+  
   Stmt **getStoredStmts() const {
     return reinterpret_cast<Stmt **>(const_cast<LambdaExpr *>(this) + 1);
   }
@@ -1198,6 +1206,11 @@ public:
                             ArrayRef<unsigned> ArrayIndexStarts,
                             SourceLocation ClosingBrace);
 
+  /// \brief Construct a new lambda expression that will be deserialized from
+  /// an external source.
+  static LambdaExpr *CreateDeserialized(ASTContext &C, unsigned NumCaptures,
+                                        unsigned NumArrayIndexVars);
+  
   /// \brief Determine the default capture kind for this lambda.
   LambdaCaptureDefault getCaptureDefault() const {
     return static_cast<LambdaCaptureDefault>(CaptureDefault);
@@ -1271,9 +1284,7 @@ public:
   CXXMethodDecl *getCallOperator() const;
 
   /// \brief Retrieve the body of the lambda.
-  CompoundStmt *getBody() const {
-    return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
-  }
+  CompoundStmt *getBody() const;
 
   /// \brief Determine whether the lambda is mutable, meaning that any
   /// captures values can be modified.
index c0d9edff4bb477b03271fa21ea9f950851d7097d..7ec2cac7ff51a8c536829873327a8ec576450db1 100644 (file)
@@ -1180,7 +1180,8 @@ namespace clang {
       // ARC
       EXPR_OBJC_BRIDGED_CAST,     // ObjCBridgedCastExpr
       
-      STMT_MS_DEPENDENT_EXISTS    // MSDependentExistsStmt
+      STMT_MS_DEPENDENT_EXISTS,   // MSDependentExistsStmt
+      EXPR_LAMBDA                 // LambdaExpr
     };
 
     /// \brief The kinds of designators that can occur in a
index 6825390772df0e236cc48dd33b9e6edc4895b0ab..e09d88091be4781a23f6cbe88ccdc8cb1766947e 100644 (file)
@@ -832,6 +832,16 @@ LambdaExpr *LambdaExpr::Create(ASTContext &Context,
                               ClosingBrace);
 }
 
+LambdaExpr *LambdaExpr::CreateDeserialized(ASTContext &C, unsigned NumCaptures,
+                                           unsigned NumArrayIndexVars) {
+  unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
+  if (NumArrayIndexVars)
+    Size += sizeof(VarDecl) * NumArrayIndexVars
+          + sizeof(unsigned) * (NumCaptures + 1);
+  void *Mem = C.Allocate(Size);
+  return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
+}
+
 LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
   return getLambdaClass()->getLambdaData().Captures;
 }
@@ -886,6 +896,13 @@ CXXMethodDecl *LambdaExpr::getCallOperator() const {
   return Result;
 }
 
+CompoundStmt *LambdaExpr::getBody() const {
+  if (!getStoredStmts()[NumCaptures])
+    getStoredStmts()[NumCaptures] = getCallOperator()->getBody();
+    
+  return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
+}
+
 bool LambdaExpr::isMutable() const {
   return (getCallOperator()->getTypeQualifiers() & Qualifiers::Const) == 0;
 }
index e69fbe7a37624a582474660f52a9305344158786..547dc568e7a33678a8cccca37641ff9796c75a9c 100644 (file)
@@ -1050,6 +1050,7 @@ void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
 void ASTDeclReader::ReadCXXDefinitionData(
                                    struct CXXRecordDecl::DefinitionData &Data,
                                    const RecordData &Record, unsigned &Idx) {
+  // Note: the caller has deserialized the IsLambda bit already.
   Data.UserDeclaredConstructor = Record[Idx++];
   Data.UserDeclaredCopyConstructor = Record[Idx++];
   Data.UserDeclaredMoveConstructor = Record[Idx++];
@@ -1097,6 +1098,25 @@ void ASTDeclReader::ReadCXXDefinitionData(
   Reader.ReadUnresolvedSet(F, Data.VisibleConversions, Record, Idx);
   assert(Data.Definition && "Data.Definition should be already set!");
   Data.FirstFriend = ReadDeclAs<FriendDecl>(Record, Idx);
+  
+  if (Data.IsLambda) {
+    typedef LambdaExpr::Capture Capture;
+    CXXRecordDecl::LambdaDefinitionData &Lambda
+      = static_cast<CXXRecordDecl::LambdaDefinitionData &>(Data);
+    Lambda.NumCaptures = Record[Idx++];
+    Lambda.NumExplicitCaptures = Record[Idx++];
+    Lambda.Captures 
+      = (Capture*)Reader.Context.Allocate(sizeof(Capture)*Lambda.NumCaptures);
+    Capture *ToCapture = Lambda.Captures;
+    for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
+      SourceLocation Loc = ReadSourceLocation(Record, Idx);
+      bool IsImplicit = Record[Idx++];
+      LambdaCaptureKind Kind = static_cast<LambdaCaptureKind>(Record[Idx++]);
+      VarDecl *Var = ReadDeclAs<VarDecl>(Record, Idx);
+      SourceLocation EllipsisLoc = ReadSourceLocation(Record, Idx);
+      *ToCapture++ = Capture(Loc, IsImplicit, Kind, Var, EllipsisLoc);
+    }
+  }
 }
 
 void ASTDeclReader::VisitCXXRecordDecl(CXXRecordDecl *D) {
@@ -1104,7 +1124,13 @@ void ASTDeclReader::VisitCXXRecordDecl(CXXRecordDecl *D) {
 
   ASTContext &C = Reader.getContext();
   if (Record[Idx++]) {
-    D->DefinitionData = new (C) struct CXXRecordDecl::DefinitionData(D);
+    // Determine whether this is a lambda closure type, so that we can
+    // allocate the appropriate DefinitionData structure.
+    bool IsLambda = Record[Idx++];
+    if (IsLambda)
+      D->DefinitionData = new (C) CXXRecordDecl::LambdaDefinitionData(D);
+    else
+      D->DefinitionData = new (C) struct CXXRecordDecl::DefinitionData(D);
     
     // Propagate the DefinitionData pointer to the canonical declaration, so
     // that all other deserialized declarations will see it.
index 7857ca8112e88a501217fea6eeda3afeed473c61..361ff3f4836666a9362638b89add389ae6bc007d 100644 (file)
@@ -1050,7 +1050,31 @@ void ASTStmtReader::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
 
 void ASTStmtReader::VisitLambdaExpr(LambdaExpr *E) {
   VisitExpr(E);
-  assert(false && "Cannot deserialize lambda expressions yet");
+  unsigned NumCaptures = Record[Idx++];
+  assert(NumCaptures == E->NumCaptures);(void)NumCaptures;
+  unsigned NumArrayIndexVars = Record[Idx++];
+  E->IntroducerRange = ReadSourceRange(Record, Idx);
+  E->CaptureDefault = static_cast<LambdaCaptureDefault>(Record[Idx++]);
+  E->ExplicitParams = Record[Idx++];
+  E->ExplicitResultType = Record[Idx++];
+  E->ClosingBrace = ReadSourceLocation(Record, Idx);
+  
+  // Read capture initializers.
+  for (LambdaExpr::capture_init_iterator C = E->capture_init_begin(),
+                                      CEnd = E->capture_init_end();
+       C != CEnd; ++C)
+    *C = Reader.ReadSubExpr();
+  
+  // Read array capture index variables.
+  if (NumArrayIndexVars > 0) {
+    unsigned *ArrayIndexStarts = E->getArrayIndexStarts();
+    for (unsigned I = 0; I != NumCaptures + 1; ++I)
+      ArrayIndexStarts[I] = Record[Idx++];
+    
+    VarDecl **ArrayIndexVars = E->getArrayIndexVars();
+    for (unsigned I = 0; I != NumArrayIndexVars; ++I)
+      ArrayIndexVars[I] = ReadDeclAs<VarDecl>(Record, Idx);
+  }
 }
 
 void ASTStmtReader::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
@@ -2083,6 +2107,14 @@ Stmt *ASTReader::ReadStmtFromStream(ModuleFile &F) {
     case EXPR_ATOMIC:
       S = new (Context) AtomicExpr(Empty);
       break;
+        
+    case EXPR_LAMBDA: {
+      unsigned NumCaptures = Record[ASTStmtReader::NumExprFields];
+      unsigned NumArrayIndexVars = Record[ASTStmtReader::NumExprFields + 1];
+      S = LambdaExpr::CreateDeserialized(Context, NumCaptures, 
+                                         NumArrayIndexVars);
+      break;
+    }
     }
     
     // We hit a STMT_STOP, so we're done with this expression.
index 7a0fed720f83788a10311afb31c764ea5d719cec..48b14e3bd7f37860b75048876394baa639b96c5d 100644 (file)
@@ -4274,6 +4274,7 @@ void ASTWriter::AddCXXCtorInitializers(
 void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
   assert(D->DefinitionData);
   struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
+  Record.push_back(Data.IsLambda);
   Record.push_back(Data.UserDeclaredConstructor);
   Record.push_back(Data.UserDeclaredCopyConstructor);
   Record.push_back(Data.UserDeclaredMoveConstructor);
@@ -4325,6 +4326,24 @@ void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Rec
   AddUnresolvedSet(Data.VisibleConversions, Record);
   // Data.Definition is the owning decl, no need to write it. 
   AddDeclRef(Data.FirstFriend, Record);
+  
+  // Add lambda-specific data.
+  if (Data.IsLambda) {
+    CXXRecordDecl::LambdaDefinitionData &Lambda = D->getLambdaData();
+    Record.push_back(Lambda.NumCaptures);
+    Record.push_back(Lambda.NumExplicitCaptures);
+    for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
+      LambdaExpr::Capture &Capture = Lambda.Captures[I];
+      AddSourceLocation(Capture.getLocation(), Record);
+      Record.push_back(Capture.isImplicit());
+      Record.push_back(Capture.getCaptureKind()); // FIXME: stable!
+      VarDecl *Var = Capture.capturesVariable()? Capture.getCapturedVar() : 0;
+      AddDeclRef(Var, Record);
+      AddSourceLocation(Capture.isPackExpansion()? Capture.getEllipsisLoc()
+                                                 : SourceLocation(), 
+                        Record);
+    }
+  }
 }
 
 void ASTWriter::ReaderInitialized(ASTReader *Reader) {
index c71d08e29cd1041c1127489110794aba535fcd75..f6cfd01341f8550382c97a137ac4e7c6a5ca2c76 100644 (file)
@@ -1024,7 +1024,34 @@ void ASTStmtWriter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
 
 void ASTStmtWriter::VisitLambdaExpr(LambdaExpr *E) {
   VisitExpr(E);
-  assert(false && "Cannot serialize lambda expressions yet");
+  Record.push_back(E->NumCaptures);
+  unsigned NumArrayIndexVars = 0;
+  if (E->HasArrayIndexVars)
+    NumArrayIndexVars = E->getArrayIndexStarts()[E->NumCaptures];
+  Record.push_back(NumArrayIndexVars);
+  Writer.AddSourceRange(E->IntroducerRange, Record);
+  Record.push_back(E->CaptureDefault); // FIXME: stable encoding
+  Record.push_back(E->ExplicitParams);
+  Record.push_back(E->ExplicitResultType);
+  Writer.AddSourceLocation(E->ClosingBrace, Record);
+  
+  // Add capture initializers.
+  for (LambdaExpr::capture_init_iterator C = E->capture_init_begin(),
+                                      CEnd = E->capture_init_end();
+       C != CEnd; ++C) {
+    Writer.AddStmt(*C);
+  }
+  
+  // Add array index variables, if any.
+  if (NumArrayIndexVars) {
+    Record.append(E->getArrayIndexStarts(), 
+                  E->getArrayIndexStarts() + E->NumCaptures + 1);
+    VarDecl **ArrayIndexVars = E->getArrayIndexVars();
+    for (unsigned I = 0; I != NumArrayIndexVars; ++I)
+      Writer.AddDeclRef(ArrayIndexVars[I], Record);
+  }
+  
+  Code = serialization::EXPR_LAMBDA;
 }
 
 void ASTStmtWriter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
diff --git a/test/PCH/cxx11-lambdas.cpp b/test/PCH/cxx11-lambdas.cpp
new file mode 100644 (file)
index 0000000..d5ae4ca
--- /dev/null
@@ -0,0 +1,43 @@
+// RUN: %clang_cc1 -pedantic-errors -std=c++11 -emit-pch %s -o %t-cxx11
+// RUN: %clang_cc1 -ast-print -pedantic-errors -std=c++11 -include-pch %t-cxx11  %s | FileCheck -check-prefix=CHECK-PRINT %s
+
+#ifndef HEADER_INCLUDED
+
+#define HEADER_INCLUDED
+template<typename T>
+T add_slowly(const T& x, const T &y) {
+  return [=, &y] { return x + y; }();
+};
+
+inline int add_int_slowly_twice(int x, int y) {
+  int i = add_slowly(x, y);
+  auto lambda = [&](int z) { return x + z; };
+  return i + lambda(y);
+}
+
+inline int sum_array(int n) {
+  int array[5] = { 1, 2, 3, 4, 5};
+  auto lambda = [=](int N) -> int {
+    int sum = 0;
+    for (unsigned I = 0; I < N; ++I)
+      sum += array[N];
+    return sum;
+  };
+
+  return lambda(n);
+}
+#else
+
+// CHECK-PRINT: float add_slowly
+// CHECK-PRINT: return [=, &y]
+template float add_slowly(const float&, const float&);
+
+// CHECK-PRINT: int add_slowly
+// CHECK-PRINT: return [=, &y]
+int add(int x, int y) {
+  return add_int_slowly_twice(x, y) + sum_array(4);
+}
+
+// CHECK-PRINT: inline int add_int_slowly_twice 
+// CHECK-PRINT: lambda = [&] (int z)
+#endif