From: Douglas Gregor Date: Tue, 14 Feb 2012 17:54:36 +0000 (+0000) Subject: Implement AST (de-)serialization for lambda expressions. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9d36f5dc4121f0f931211ea2d0a74d299eb82b23;p=clang Implement AST (de-)serialization for lambda expressions. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@150491 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/ExprCXX.h b/include/clang/AST/ExprCXX.h index ba2233d23b..c305faa8a9 100644 --- a/include/clang/AST/ExprCXX.h +++ b/include/clang/AST/ExprCXX.h @@ -1168,6 +1168,14 @@ private: ArrayRef 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(const_cast(this) + 1); } @@ -1198,6 +1206,11 @@ public: ArrayRef 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(CaptureDefault); @@ -1271,9 +1284,7 @@ public: CXXMethodDecl *getCallOperator() const; /// \brief Retrieve the body of the lambda. - CompoundStmt *getBody() const { - return reinterpret_cast(getStoredStmts()[NumCaptures]); - } + CompoundStmt *getBody() const; /// \brief Determine whether the lambda is mutable, meaning that any /// captures values can be modified. diff --git a/include/clang/Serialization/ASTBitCodes.h b/include/clang/Serialization/ASTBitCodes.h index c0d9edff4b..7ec2cac7ff 100644 --- a/include/clang/Serialization/ASTBitCodes.h +++ b/include/clang/Serialization/ASTBitCodes.h @@ -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 diff --git a/lib/AST/ExprCXX.cpp b/lib/AST/ExprCXX.cpp index 6825390772..e09d88091b 100644 --- a/lib/AST/ExprCXX.cpp +++ b/lib/AST/ExprCXX.cpp @@ -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(getStoredStmts()[NumCaptures]); +} + bool LambdaExpr::isMutable() const { return (getCallOperator()->getTypeQualifiers() & Qualifiers::Const) == 0; } diff --git a/lib/Serialization/ASTReaderDecl.cpp b/lib/Serialization/ASTReaderDecl.cpp index e69fbe7a37..547dc568e7 100644 --- a/lib/Serialization/ASTReaderDecl.cpp +++ b/lib/Serialization/ASTReaderDecl.cpp @@ -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(Record, Idx); + + if (Data.IsLambda) { + typedef LambdaExpr::Capture Capture; + CXXRecordDecl::LambdaDefinitionData &Lambda + = static_cast(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(Record[Idx++]); + VarDecl *Var = ReadDeclAs(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. diff --git a/lib/Serialization/ASTReaderStmt.cpp b/lib/Serialization/ASTReaderStmt.cpp index 7857ca8112..361ff3f483 100644 --- a/lib/Serialization/ASTReaderStmt.cpp +++ b/lib/Serialization/ASTReaderStmt.cpp @@ -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(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(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. diff --git a/lib/Serialization/ASTWriter.cpp b/lib/Serialization/ASTWriter.cpp index 7a0fed720f..48b14e3bd7 100644 --- a/lib/Serialization/ASTWriter.cpp +++ b/lib/Serialization/ASTWriter.cpp @@ -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) { diff --git a/lib/Serialization/ASTWriterStmt.cpp b/lib/Serialization/ASTWriterStmt.cpp index c71d08e29c..f6cfd01341 100644 --- a/lib/Serialization/ASTWriterStmt.cpp +++ b/lib/Serialization/ASTWriterStmt.cpp @@ -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 index 0000000000..d5ae4cac52 --- /dev/null +++ b/test/PCH/cxx11-lambdas.cpp @@ -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 +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