: Expr(BlockExprClass, ty),
TheBlock(BD), HasBlockDeclRefExprs(hasBlockDeclRefExprs) {}
+ /// \brief Build an empty block expression.
+ explicit BlockExpr(EmptyShell Empty) : Expr(BlockExprClass, Empty) { }
+
const BlockDecl *getBlockDecl() const { return TheBlock; }
BlockDecl *getBlockDecl() { return TheBlock; }
-
+ void setBlockDecl(BlockDecl *BD) { TheBlock = BD; }
+
// Convenience functions for probing the underlying BlockDecl.
SourceLocation getCaretLocation() const;
const Stmt *getBody() const;
/// hasBlockDeclRefExprs - Return true iff the block has BlockDeclRefExpr
/// contained inside.
bool hasBlockDeclRefExprs() const { return HasBlockDeclRefExprs; }
+ void setHasBlockDeclRefExprs(bool BDRE) { HasBlockDeclRefExprs = BDRE; }
static bool classof(const Stmt *T) {
return T->getStmtClass() == BlockExprClass;
EXPR_GNU_NULL,
/// \brief A ShuffleVectorExpr record.
EXPR_SHUFFLE_VECTOR,
- /// FIXME: BlockExpr
+ /// \brief BlockExpr
+ EXPR_BLOCK,
+ /// \brief A BlockDeclRef record.
EXPR_BLOCK_DECL_REF
};
void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
VisitDecl(BD);
+ BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt()));
unsigned NumParams = Record[Idx++];
llvm::SmallVector<ParmVarDecl *, 16> Params;
Params.reserve(NumParams);
unsigned VisitChooseExpr(ChooseExpr *E);
unsigned VisitGNUNullExpr(GNUNullExpr *E);
unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
+ unsigned VisitBlockExpr(BlockExpr *E);
unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
};
}
return NumExprs;
}
+unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
+ VisitExpr(E);
+ E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
+ E->setHasBlockDeclRefExprs(Record[Idx++]);
+ return 0;
+}
+
unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
VisitExpr(E);
E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
S = new (Context) ShuffleVectorExpr(Empty);
break;
+ case pch::EXPR_BLOCK:
+ S = new (Context) BlockExpr(Empty);
+ break;
+
case pch::EXPR_BLOCK_DECL_REF:
// FIXME: untested until we have statement and block support
S = new (Context) BlockDeclRefExpr(Empty);
void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
VisitDecl(D);
- // FIXME: emit block body
+ Writer.AddStmt(D->getBody());
Record.push_back(D->param_size());
for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
P != PEnd; ++P)
void VisitChooseExpr(ChooseExpr *E);
void VisitGNUNullExpr(GNUNullExpr *E);
void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
+ void VisitBlockExpr(BlockExpr *E);
void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
};
}
Code = pch::EXPR_SHUFFLE_VECTOR;
}
+void PCHStmtWriter::VisitBlockExpr(BlockExpr *E) {
+ VisitExpr(E);
+ Writer.AddDeclRef(E->getBlockDecl(), Record);
+ Record.push_back(E->hasBlockDeclRefExprs());
+ Code = pch::EXPR_BLOCK;
+}
+
void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
VisitExpr(E);
Writer.AddDeclRef(E->getDecl(), Record);
--- /dev/null
+// Test this without pch.
+// RUN: clang-cc -fblocks -include %S/blocks.h -fsyntax-only -ast-print -o - %s
+
+// Test with pch.
+// RUN: clang-cc -emit-pch -fblocks -o %t %S/blocks.h &&
+// RUN: clang-cc -fblocks -include-pch %t -fsyntax-only -ast-print -o - %s
+
+int do_add(int x, int y) { return add(x, y); }
+
+int do_scaled_add(int a, int b, int s) {
+ return scaled_add(a, b, s);
+}
--- /dev/null
+// Header for PCH test blocks.c
+
+int call_block(int (^bl)(int x, int y), int a, int b) {
+ return bl(a, b);
+}
+
+int add(int a, int b) {
+ return call_block(^(int x, int y) { return x + y; }, a, b);
+}
+
+int scaled_add(int a, int b, int s) {
+ __block int scale = s;
+ return call_block(^(int x, int y) { return x*scale + y; }, a, b);
+}
// Test this without pch.
-// RUN: clang-cc -fblocks -include %S/stmts.h -fsyntax-only -ast-print -o - %s
+// RUN: clang-cc -include %S/stmts.h -fsyntax-only -ast-print -o - %s
// Test with pch.
-// RUN: clang-cc -emit-pch -fblocks -o %t %S/stmts.h &&
-// RUN: clang-cc -fblocks -include-pch %t -fsyntax-only -ast-print -o - %s
+// RUN: clang-cc -emit-pch -o %t %S/stmts.h &&
+// RUN: clang-cc -include-pch %t -fsyntax-only -ast-print -o - %s
void g0(void) { f0(5); }
int g1(int x) { return f1(x); }