wwwwpan added you to the CC list for the revision " Implement CapturedStmt AST".
Hi doug.gregor,
This is the first patch to implement generic function outling via
CapturedStmts. Note that
- CapturedStmt is not exposed to the C api,
- Serialization and template support are pending.
Patches for parsing and semantics will follow soon.
http://llvm-reviews.chandlerc.com/D370
Files:
include/clang/AST/RecursiveASTVisitor.h
include/clang/AST/Stmt.h
include/clang/Basic/StmtNodes.td
include/clang/Serialization/ASTBitCodes.h
lib/AST/ASTDumper.cpp
lib/AST/Stmt.cpp
lib/AST/StmtPrinter.cpp
lib/AST/StmtProfile.cpp
lib/CodeGen/CGStmt.cpp
lib/CodeGen/CodeGenFunction.h
lib/Sema/TreeTransform.h
lib/Serialization/ASTReaderStmt.cpp
lib/Serialization/ASTWriterStmt.cpp
lib/StaticAnalyzer/Core/ExprEngine.cpp
tools/libclang/CXCursor.cpp
tools/libclang/RecursiveASTVisitor.h
Index: include/clang/AST/RecursiveASTVisitor.h
===================================================================
--- include/clang/AST/RecursiveASTVisitor.h
+++ include/clang/AST/RecursiveASTVisitor.h
@@ -2200,6 +2200,7 @@
DEF_TRAVERSE_STMT(SEHTryStmt, {})
DEF_TRAVERSE_STMT(SEHExceptStmt, {})
DEF_TRAVERSE_STMT(SEHFinallyStmt,{})
+DEF_TRAVERSE_STMT(CapturedStmt, {})
DEF_TRAVERSE_STMT(CXXOperatorCallExpr, { })
DEF_TRAVERSE_STMT(OpaqueValueExpr, { })
Index: include/clang/AST/Stmt.h
===================================================================
--- include/clang/AST/Stmt.h
+++ include/clang/AST/Stmt.h
@@ -18,8 +18,10 @@
#include "clang/AST/StmtIterator.h"
#include "clang/Basic/IdentifierTable.h"
#include "clang/Basic/LLVM.h"
+#include "clang/Basic/Lambda.h"
#include "clang/Basic/SourceLocation.h"
#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/PointerIntPair.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/ErrorHandling.h"
#include <string>
@@ -33,12 +35,14 @@
class Attr;
class Decl;
class Expr;
+ class FunctionDecl;
class IdentifierInfo;
class LabelDecl;
class ParmVarDecl;
class PrinterHelper;
struct PrintingPolicy;
class QualType;
+ class RecordDecl;
class SourceManager;
class StringLiteral;
class SwitchStmt;
@@ -1882,6 +1886,144 @@
}
};
+/// \brief This captures a statement into a function.
+class CapturedStmt : public Stmt {
+public:
+ // LambdaCaptureKind and this enumerator need to be extended
+ // to handle other capture kinds.
+ enum {
+ /// \brief Flag used by the Capture class to indicate that the given
+ /// capture was by-copy.
+ Capture_ByCopy = 0x01
+ };
+
+ /// \brief Describes the capture of either a variable or 'this'.
+ class Capture {
+ llvm::PointerIntPair<VarDecl *, 1> VarAndBits;
+ Expr *CopyExpr;
+ public:
+ /// \brief Create a new capture.
+ Capture(LambdaCaptureKind Kind, Expr *E, VarDecl *Var = 0)
+ : VarAndBits(Var, 0), CopyExpr(E) {
+ unsigned Bits = 0x0;
+
+ switch (Kind) {
+ case LCK_This:
+ assert(Var == 0 && "'this' capture cannot have a variable!");
+ break;
+ case LCK_ByCopy:
+ Bits |= Capture_ByCopy;
+ // Fall through
+ case LCK_ByRef:
+ assert(Var && "capture must have a variable!");
+ break;
+ }
+ VarAndBits.setInt(Bits);
+ }
+
+ /// \brief Determine the kind of capture.
+ LambdaCaptureKind getCaptureKind() const {
+ if (capturesThis())
+ return LCK_This;
+
+ return (VarAndBits.getInt() & Capture_ByCopy)? LCK_ByCopy : LCK_ByRef;
+ }
+
+ /// \brief Determine whether this capture handles the C++ 'this' pointer.
+ bool capturesThis() const { return VarAndBits.getPointer() == 0; }
+
+ /// \brief Determine whether this capture handles a variable.
+ bool capturesVariable() const { return VarAndBits.getPointer() != 0; }
+
+ /// \brief Retrieve the declaration of the variable being captured.
+ ///
+ /// This operation is only valid if this capture does not capture 'this'.
+ VarDecl *getCapturedVar() const {
+ assert(!capturesThis() && "No variable available for 'this' capture");
+ return VarAndBits.getPointer();
+ }
+
+ Expr *getCopyExpr() const { return CopyExpr; }
+ };
+
+private:
+ /// \brief The implicit outlined function.
+ FunctionDecl *TheDecl;
+
+ /// \brief The record for captured variables, a RecordDecl or CXXRecordDecl.
+ RecordDecl *TheRecordDecl;
+
+ /// \brief The captured statement.
+ Stmt *SubStmt;
+
+ /// \brief All variable captures.
+ Capture *Captures;
+
+ /// \brief The number of variable captured, including 'this'.
+ unsigned NumCaptures;
+
+public:
+ explicit CapturedStmt(Stmt *S)
+ : Stmt(CapturedStmtClass), TheDecl(0), TheRecordDecl(0), SubStmt(S),
+ Captures(0), NumCaptures(0) { }
+
+ explicit CapturedStmt(EmptyShell Empty)
+ : Stmt(CapturedStmtClass, Empty), TheDecl(0), TheRecordDecl(0),
+ SubStmt(0), Captures(0), NumCaptures(0) { }
+
+ FunctionDecl *getFunctionDecl() { return TheDecl; }
+ const FunctionDecl *getFunctionDecl() const { return TheDecl; }
+ void setFunctionDecl(FunctionDecl *D) { TheDecl = D; }
+
+ RecordDecl *getRecordDecl() { return TheRecordDecl; }
+ const RecordDecl *getRecordDecl() const { return TheRecordDecl; }
+ void setRecordDecl(RecordDecl *D) { TheRecordDecl = D; }
+
+ Stmt *getSubStmt() { return SubStmt; }
+ const Stmt *getSubStmt() const { return SubStmt; }
+ void setSubStmt(Stmt *S) { SubStmt = S; }
+
+ /// \brief True if this captured region (or its nested regions) captures
+ /// anything from its enclosing scopes.
+ bool hasCaptures() const { return NumCaptures != 0; }
+
+ /// \brief Returns the number of captured variables.
+ unsigned getNumCaptures() const { return NumCaptures; }
+
+ typedef const Capture *capture_iterator;
+ typedef const Capture *capture_const_iterator;
+ capture_iterator capture_begin() { return Captures; }
+ capture_iterator capture_end() { return Captures + NumCaptures; }
+ capture_const_iterator capture_begin() const { return Captures; }
+ capture_const_iterator capture_end() const { return Captures + NumCaptures; }
+
+ /// \brief True if this variable has been captured.
+ bool capturesVariable(const VarDecl *var) const;
+
+ /// \brief Initialize the member field Captures.
+ void setCaptures(ASTContext &Context,
+ const Capture *begin,
+ const Capture *end);
+
+ SourceLocation getLocStart() const LLVM_READONLY {
+ return SubStmt->getLocStart();
+ }
+ SourceLocation getLocEnd() const LLVM_READONLY {
+ return SubStmt->getLocEnd();
+ }
+ SourceRange getSourceRange() const LLVM_READONLY {
+ return SubStmt->getSourceRange();
+ }
+
+ static bool classof(const Stmt *T) {
+ return T->getStmtClass() == CapturedStmtClass;
+ }
+
+ child_range children() {
+ return child_range(&SubStmt, &SubStmt + 1);
+ }
+};
+
} // end namespace clang
#endif
Index: include/clang/Basic/StmtNodes.td
===================================================================
--- include/clang/Basic/StmtNodes.td
+++ include/clang/Basic/StmtNodes.td
@@ -27,6 +27,7 @@
def SwitchCase : Stmt<1>;
def CaseStmt : DStmt<SwitchCase>;
def DefaultStmt : DStmt<SwitchCase>;
+def CapturedStmt : Stmt;
// Asm statements
def AsmStmt : Stmt<1>;
Index: include/clang/Serialization/ASTBitCodes.h
===================================================================
--- include/clang/Serialization/ASTBitCodes.h
+++ include/clang/Serialization/ASTBitCodes.h
@@ -1080,6 +1080,8 @@
STMT_RETURN,
/// \brief A DeclStmt record.
STMT_DECL,
+ /// \brief A CapturedStmt record.
+ STMT_CAPTURED,
/// \brief A GCC-style AsmStmt record.
STMT_GCCASM,
/// \brief A MS-style AsmStmt record.
Index: lib/AST/ASTDumper.cpp
===================================================================
--- lib/AST/ASTDumper.cpp
+++ lib/AST/ASTDumper.cpp
@@ -244,6 +244,7 @@
void VisitAttributedStmt(const AttributedStmt *Node);
void VisitLabelStmt(const LabelStmt *Node);
void VisitGotoStmt(const GotoStmt *Node);
+ void VisitCapturedStmt(const CapturedStmt *Node);
// Exprs
void VisitExpr(const Expr *Node);
@@ -1352,6 +1353,19 @@
dumpPointer(Node->getLabel());
}
+void ASTDumper::VisitCapturedStmt(const CapturedStmt *Node) {
+ VisitStmt(Node);
+ for (CapturedStmt::capture_iterator I = Node->capture_begin(),
+ E = Node->capture_end(); I != E; ++I) {
+ IndentScope Indent(*this);
+ OS << "capture ";
+ if (I->capturesThis())
+ OS << "this";
+ else
+ dumpBareDeclRef(I->getCapturedVar());
+ }
+}
+
//===----------------------------------------------------------------------===//
// Expr dumping methods.
//===----------------------------------------------------------------------===//
Index: lib/AST/Stmt.cpp
===================================================================
--- lib/AST/Stmt.cpp
+++ lib/AST/Stmt.cpp
@@ -1022,3 +1022,39 @@
Stmt *Block) {
return new(C)SEHFinallyStmt(Loc,Block);
}
+
+void CapturedStmt::setCaptures(ASTContext &Context,
+ const Capture *begin,
+ const Capture *end) {
+ assert(begin <= end && "invalid forward range for captures");
+
+ if (begin == end) {
+ NumCaptures = 0;
+ Captures = 0;
+ return;
+ }
+
+ NumCaptures = end - begin;
+
+ // Copy all Capture objects into the context.
+ unsigned Size = NumCaptures * sizeof(Capture);
+ void *buffer = Context.Allocate(Size, llvm::alignOf<Capture *>());
+ memcpy(buffer, begin, Size);
+
+ Captures = static_cast<Capture *>(buffer);
+}
+
+bool CapturedStmt::capturesVariable(const VarDecl *variable) const {
+ for (capture_const_iterator I = capture_begin(),
+ E = capture_end(); I != E; ++I) {
+ if (I->capturesThis())
+ continue;
+
+ // This does not handle variable redeclarations. This should be
+ // extended to capture non-auto variables.
+ if (I->getCapturedVar() == variable)
+ return true;
+ }
+
+ return false;
+}
Index: lib/AST/StmtPrinter.cpp
===================================================================
--- lib/AST/StmtPrinter.cpp
+++ lib/AST/StmtPrinter.cpp
@@ -448,6 +448,10 @@
Indent() << "}\n";
}
+void StmtPrinter::VisitCapturedStmt(CapturedStmt *Node) {
+ PrintStmt(Node->getSubStmt());
+}
+
void StmtPrinter::VisitObjCAtTryStmt(ObjCAtTryStmt *Node) {
Indent() << "@try";
if (CompoundStmt *TS = dyn_cast<CompoundStmt>(Node->getTryBody())) {
Index: lib/AST/StmtProfile.cpp
===================================================================
--- lib/AST/StmtProfile.cpp
+++ lib/AST/StmtProfile.cpp
@@ -215,6 +215,10 @@
VisitStmt(S);
}
+void StmtProfiler::VisitCapturedStmt(const CapturedStmt *S) {
+ VisitStmt(S);
+}
+
void StmtProfiler::VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
VisitStmt(S);
}
Index: lib/CodeGen/CGStmt.cpp
===================================================================
--- lib/CodeGen/CGStmt.cpp
+++ lib/CodeGen/CGStmt.cpp
@@ -134,7 +134,9 @@
case Stmt::SwitchStmtClass: EmitSwitchStmt(cast<SwitchStmt>(*S)); break;
case Stmt::GCCAsmStmtClass: // Intentional fall-through.
case Stmt::MSAsmStmtClass: EmitAsmStmt(cast<AsmStmt>(*S)); break;
-
+ case Stmt::CapturedStmtClass:
+ EmitCapturedStmt(cast<CapturedStmt>(*S));
+ break;
case Stmt::ObjCAtTryStmtClass:
EmitObjCAtTryStmt(cast<ObjCAtTryStmt>(*S));
break;
@@ -1699,3 +1701,8 @@
EmitStoreThroughLValue(RValue::get(Tmp), ResultRegDests[i]);
}
}
+
+void CodeGenFunction::EmitCapturedStmt(const CapturedStmt &S) {
+ // FIXME: not implemented yet
+ llvm_unreachable("not implemented yet");
+}
Index: lib/CodeGen/CodeGenFunction.h
===================================================================
--- lib/CodeGen/CodeGenFunction.h
+++ lib/CodeGen/CodeGenFunction.h
@@ -2042,6 +2042,7 @@
void EmitCaseStmt(const CaseStmt &S);
void EmitCaseStmtRange(const CaseStmt &S);
void EmitAsmStmt(const AsmStmt &S);
+ void EmitCapturedStmt(const CapturedStmt &S);
void EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S);
void EmitObjCAtTryStmt(const ObjCAtTryStmt &S);
Index: lib/Sema/TreeTransform.h
===================================================================
--- lib/Sema/TreeTransform.h
+++ lib/Sema/TreeTransform.h
@@ -9357,6 +9357,13 @@
/*TemplateArgs*/ 0);
}
+template<typename Derived>
+StmtResult
+TreeTransform<Derived>::TransformCapturedStmt(CapturedStmt *S) {
+ // FIXME: not implemented yet
+ return Owned(S);
+}
+
} // end namespace clang
#endif // LLVM_CLANG_SEMA_TREETRANSFORM_H
Index: lib/Serialization/ASTReaderStmt.cpp
===================================================================
--- lib/Serialization/ASTReaderStmt.cpp
+++ lib/Serialization/ASTReaderStmt.cpp
@@ -324,6 +324,12 @@
VisitStmt(S);
}
+void ASTStmtReader::VisitCapturedStmt(CapturedStmt *S) {
+ // FIXME: CapturedStmt reader not yet implemented.
+ VisitStmt(S);
+ S->setSubStmt(Reader.ReadSubStmt());
+}
+
void ASTStmtReader::VisitExpr(Expr *E) {
VisitStmt(E);
E->setType(Reader.readType(F, Record, Idx));
@@ -1711,6 +1717,10 @@
S = new (Context) MSAsmStmt(Empty);
break;
+ case STMT_CAPTURED:
+ S = new (Context) CapturedStmt(Empty);
+ break;
+
case EXPR_PREDEFINED:
S = new (Context) PredefinedExpr(Empty);
break;
Index: lib/Serialization/ASTWriterStmt.cpp
===================================================================
--- lib/Serialization/ASTWriterStmt.cpp
+++ lib/Serialization/ASTWriterStmt.cpp
@@ -255,6 +255,13 @@
Code = serialization::STMT_MSASM;
}
+void ASTStmtWriter::VisitCapturedStmt(CapturedStmt *S) {
+ // FIXME: CapturedStmt writer not yet implemented.
+ VisitStmt(S);
+ Writer.AddStmt(S->getSubStmt());
+ Code = serialization::STMT_CAPTURED;
+}
+
void ASTStmtWriter::VisitExpr(Expr *E) {
VisitStmt(E);
Writer.AddTypeRef(E->getType(), Record);
Index: lib/StaticAnalyzer/Core/ExprEngine.cpp
===================================================================
--- lib/StaticAnalyzer/Core/ExprEngine.cpp
+++ lib/StaticAnalyzer/Core/ExprEngine.cpp
@@ -578,6 +578,7 @@
case Stmt::SwitchStmtClass:
case Stmt::WhileStmtClass:
case Expr::MSDependentExistsStmtClass:
+ case Stmt::CapturedStmtClass:
llvm_unreachable("Stmt should not be in analyzer evaluation loop");
case Stmt::ObjCSubscriptRefExprClass:
Index: tools/libclang/CXCursor.cpp
===================================================================
--- tools/libclang/CXCursor.cpp
+++ tools/libclang/CXCursor.cpp
@@ -270,6 +270,10 @@
K = CXCursor_DeclStmt;
break;
+ case Stmt::CapturedStmtClass:
+ K = CXCursor_UnexposedStmt;
+ break;
+
case Stmt::IntegerLiteralClass:
K = CXCursor_IntegerLiteral;
break;
Index: tools/libclang/RecursiveASTVisitor.h
===================================================================
--- tools/libclang/RecursiveASTVisitor.h
+++ tools/libclang/RecursiveASTVisitor.h
@@ -1824,7 +1824,7 @@
DEF_TRAVERSE_STMT(ReturnStmt, { })
DEF_TRAVERSE_STMT(SwitchStmt, { })
DEF_TRAVERSE_STMT(WhileStmt, { })
-
+DEF_TRAVERSE_STMT(CapturedStmt, { })
DEF_TRAVERSE_STMT(CXXDependentScopeMemberExpr, {
TRY_TO(TraverseNestedNameSpecifierLoc(S->getQualifierLoc()));
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits