[cfe-commits] r150235 - in /cfe/trunk: include/clang/AST/ASTContext.h include/clang/AST/DeclCXX.h lib/AST/DeclCXX.cpp lib/Sema/SemaLambda.cpp

Douglas Gregor dgregor at apple.com
Thu Feb 9 23:45:32 PST 2012


Author: dgregor
Date: Fri Feb 10 01:45:31 2012
New Revision: 150235

URL: http://llvm.org/viewvc/llvm-project?rev=150235&view=rev
Log:
Extend CXXRecordDecl with a function that determines the mapping from
the variables captured by a lambda to the fields that store the
captured values. To be used in IRgen.

Modified:
    cfe/trunk/include/clang/AST/ASTContext.h
    cfe/trunk/include/clang/AST/DeclCXX.h
    cfe/trunk/lib/AST/DeclCXX.cpp
    cfe/trunk/lib/Sema/SemaLambda.cpp

Modified: cfe/trunk/include/clang/AST/ASTContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTContext.h?rev=150235&r1=150234&r2=150235&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Fri Feb 10 01:45:31 2012
@@ -59,6 +59,7 @@
   class CXXRecordDecl;
   class Decl;
   class FieldDecl;
+  class LambdaExpr;
   class MangleContext;
   class ObjCIvarDecl;
   class ObjCIvarRefExpr;
@@ -163,6 +164,10 @@
   llvm::DenseMap<const FunctionDecl*, FunctionDecl*>
     ClassScopeSpecializationPattern;
 
+  /// \brief Mapping from closure types to the lambda expressions that
+  /// create instances of them.
+  llvm::DenseMap<const CXXRecordDecl *, LambdaExpr *> Lambdas;
+
   /// \brief Representation of a "canonical" template template parameter that
   /// is used in canonical template names.
   class CanonicalTemplateTemplateParm : public llvm::FoldingSetNode {
@@ -358,7 +363,8 @@
   friend class ASTDeclReader;
   friend class ASTReader;
   friend class ASTWriter;
-  
+  friend class CXXRecordDecl;
+
   const TargetInfo *Target;
   clang::PrintingPolicy PrintingPolicy;
   

Modified: cfe/trunk/include/clang/AST/DeclCXX.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclCXX.h?rev=150235&r1=150234&r2=150235&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/DeclCXX.h (original)
+++ cfe/trunk/include/clang/AST/DeclCXX.h Fri Feb 10 01:45:31 2012
@@ -19,6 +19,7 @@
 #include "clang/AST/Decl.h"
 #include "clang/AST/TypeLoc.h"
 #include "clang/AST/UnresolvedSet.h"
+#include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/PointerIntPair.h"
 #include "llvm/ADT/SmallPtrSet.h"
 
@@ -37,6 +38,7 @@
 class CXXFinalOverriderMap;
 class CXXIndirectPrimaryBaseSet;
 class FriendDecl;
+class LambdaExpr;
 
 /// \brief Represents any kind of function declaration, whether it is a
 /// concrete function or a function template.
@@ -925,7 +927,23 @@
   /// \brief Determine whether this class describes a lambda function object.
   bool isLambda() const { return hasDefinition() && data().IsLambda; }
   
-  void setLambda(bool Lambda = true) { data().IsLambda = Lambda; }
+  /// \brief Mark this as a closure type from a lambda expression.
+  void makeLambda() { data().IsLambda = true; }
+
+  /// \brief Set the lambda expression associated with this closure type.
+  void setLambda(LambdaExpr *Lambda);
+
+  /// \brief For a closure type, retrieve the mapping from captured
+  /// variables and this to the non-static data members that store the
+  /// values or references of the captures.
+  ///
+  /// \param Captures Will be populated with the mapping from captured
+  /// variables to the corresponding fields.
+  ///
+  /// \param ThisCapture Will be set to the field declaration for the
+  /// 'this' capture.
+  void getCaptureFields(llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
+                        FieldDecl *&ThisCapture);
 
   /// getConversions - Retrieve the overload set containing all of the
   /// conversion functions in this class.

Modified: cfe/trunk/lib/AST/DeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclCXX.cpp?rev=150235&r1=150234&r2=150235&view=diff
==============================================================================
--- cfe/trunk/lib/AST/DeclCXX.cpp (original)
+++ cfe/trunk/lib/AST/DeclCXX.cpp Fri Feb 10 01:45:31 2012
@@ -969,6 +969,35 @@
   return isPOD() && data().HasOnlyCMembers;
 }
 
+void CXXRecordDecl::setLambda(LambdaExpr *Lambda) {
+  if (!Lambda)
+    return;
+
+  data().IsLambda = true;
+  getASTContext().Lambdas[this] = Lambda;
+}
+
+void CXXRecordDecl::getCaptureFields(
+       llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures,
+       FieldDecl *&ThisCapture) {
+  Captures.clear();
+  ThisCapture = 0;
+
+  LambdaExpr *Lambda = getASTContext().Lambdas[this];
+  RecordDecl::field_iterator Field = field_begin();
+  for (LambdaExpr::capture_iterator C = Lambda->capture_begin(), 
+                                 CEnd = Lambda->capture_end();
+       C != CEnd; ++C, ++Field) {
+    if (C->capturesThis()) {
+      ThisCapture = *Field;
+      continue;
+    }
+
+    Captures[C->getCapturedVar()] = *Field;
+  }
+}
+
+
 static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) {
   QualType T;
   if (isa<UsingShadowDecl>(Conv))

Modified: cfe/trunk/lib/Sema/SemaLambda.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaLambda.cpp?rev=150235&r1=150234&r2=150235&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaLambda.cpp (original)
+++ cfe/trunk/lib/Sema/SemaLambda.cpp Fri Feb 10 01:45:31 2012
@@ -32,7 +32,7 @@
                                                /*IdLoc=*/Intro.Range.getBegin(),
                                                /*Id=*/0);
   Class->startDefinition();
-  Class->setLambda(true);
+  Class->makeLambda();
   CurContext->addDecl(Class);
 
   // Build the call operator; we don't really have all the relevant information
@@ -408,6 +408,7 @@
                                           CaptureDefault, Captures, 
                                           ExplicitParams, CaptureInits, 
                                           Body->getLocEnd());
+  Class->setLambda(Lambda);
 
   // C++11 [expr.prim.lambda]p2:
   //   A lambda-expression shall not appear in an unevaluated operand





More information about the cfe-commits mailing list