//===----------------------------------------------------------------------===//
/// BlockExpr - Common base class between BlockStmtExpr and BlockExprExpr.
+/// FIXME: Combine with BlockStmtExpr...no more need for a common base.
class BlockExpr : public Expr {
SourceLocation CaretLocation;
llvm::SmallVector<ParmVarDecl*, 8> Args;
arg_iterator arg_end() const { return Args.end(); }
static bool classof(const Stmt *T) {
- return T->getStmtClass() == BlockStmtExprClass ||
- T->getStmtClass() == BlockExprExprClass;
+ return T->getStmtClass() == BlockStmtExprClass;
}
static bool classof(const BlockExpr *) { return true; }
};
static BlockStmtExpr* CreateImpl(llvm::Deserializer& D, ASTContext& C);
};
-/// BlockExprExpr - Represents a block literal with syntax:
-/// ^(expression) or ^(int arg1, float arg2)(expression)
-class BlockExprExpr : public BlockExpr {
- Expr *BodyExpr;
-public:
- BlockExprExpr(SourceLocation CaretLoc, QualType Ty, ParmVarDecl **args,
- unsigned numargs, Expr *body) :
- BlockExpr(BlockExprExprClass, Ty, CaretLoc,
- args, numargs), BodyExpr(body) {}
-
- const Expr *getExpr() const { return BodyExpr; }
- Expr *getExpr() { return BodyExpr; }
-
- virtual SourceRange getSourceRange() const {
- return SourceRange(getCaretLocation(), BodyExpr->getLocEnd());
- }
-
- // Iterators
- virtual child_iterator child_begin();
- virtual child_iterator child_end();
-
- static bool classof(const Stmt *T) {
- return T->getStmtClass() == BlockExprExprClass;
- }
- static bool classof(const BlockExprExpr *) { return true; }
-};
-
/// BlockDeclRefExpr - A reference to a declared variable, function,
/// enum, etc.
class BlockDeclRefExpr : public Expr {
STMT(78, ShuffleVectorExpr , Expr)
STMT(79, BlockExpr , Expr)
STMT(80, BlockStmtExpr , BlockExpr)
-STMT(81, BlockExprExpr , BlockExpr)
-STMT(82, BlockDeclRefExpr , Expr)
+STMT(81, BlockDeclRefExpr , Expr)
-LAST_EXPR(82)
+LAST_EXPR(81)
#undef STMT
#undef FIRST_STMT
virtual ExprResult ActOnBlockStmtExpr(SourceLocation CaretLoc, StmtTy *Body,
Scope *CurScope) { return 0; }
- /// ActOnBlockExprExpr - This is called when the body of a block
- /// expression literal was successfully completed. ^(int x)[foo bar: x]
- virtual ExprResult ActOnBlockExprExpr(SourceLocation CaretLoc, ExprTy *Body,
- Scope *CurScope) { return 0; }
-
//===------------------------- C++ Declarations -------------------------===//
/// ActOnStartNamespaceDef - This is called at the start of a namespace
return reinterpret_cast<Stmt**>(&Body)+1;
}
-Stmt::child_iterator BlockExprExpr::child_begin() {
- return reinterpret_cast<Stmt**>(&BodyExpr);
-}
-Stmt::child_iterator BlockExprExpr::child_end() {
- return reinterpret_cast<Stmt**>(&BodyExpr)+1;
-}
Stmt::child_iterator BlockDeclRefExpr::child_begin(){return child_iterator();}
Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator();}
PrintRawCompoundStmt(Node->getBody());
}
-void StmtPrinter::VisitBlockExprExpr(BlockExprExpr *Node) {
- VisitBlockExpr(Node);
- PrintExpr(Node->getExpr());
-}
-
void StmtPrinter::VisitBlockDeclRefExpr(BlockDeclRefExpr *Node) {
OS << Node->getDecl()->getName();
}
}
/// ParseBlockLiteralExpression - Parse a block literal, which roughly looks
-/// like ^(int x){ return x+1; } or ^(int y)foo(4, y, z)
+/// like ^(int x){ return x+1; }
///
/// block-literal:
/// [clang] '^' block-args[opt] compound-statement
-/// [clang] '^' block-args cast-expression
/// [clang] block-args:
/// [clang] '(' parameter-list ')'
///
// Inform sema that we are starting a block.
Actions.ActOnBlockStart(CaretLoc, CurScope, ParamInfo);
- ExprResult Result;
+ ExprResult Result = true;
if (Tok.is(tok::l_brace)) {
StmtResult Stmt = ParseCompoundStatementBody();
if (!Stmt.isInvalid) {
Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.Val, CurScope);
} else {
Actions.ActOnBlockError(CaretLoc, CurScope);
- Result = true;
- }
- } else {
- ExprResult Expr = ParseCastExpression(false);
- if (!Expr.isInvalid) {
- Result = Actions.ActOnBlockExprExpr(CaretLoc, Expr.Val, CurScope);
- } else {
- Actions.ActOnBlockError(CaretLoc, CurScope);
- Diag(Tok, diag::err_expected_block_lbrace);
- Result = true;
}
}
-
ExitScope();
return Result;
}
virtual ExprResult ActOnBlockStmtExpr(SourceLocation CaretLoc, StmtTy *Body,
Scope *CurScope);
- /// ActOnBlockExprExpr - This is called when the body of a block
- /// expression literal was successfully completed. ^(int x)[foo bar: x]
- virtual ExprResult ActOnBlockExprExpr(SourceLocation CaretLoc, ExprTy *Body,
- Scope *CurScope);
-
// Act on C++ namespaces
virtual DeclTy *ActOnStartNamespaceDef(Scope *S, SourceLocation IdentLoc,
IdentifierInfo *Ident,
&BSI->Params[0], BSI->Params.size(), Body.take());
}
-/// ActOnBlockExprExpr - This is called when the body of a block
-/// expression literal was successfully completed. ^(int x)[foo bar: x]
-Sema::ExprResult Sema::ActOnBlockExprExpr(SourceLocation CaretLoc, ExprTy *body,
- Scope *CurScope) {
- // Ensure that CurBlock is deleted.
- llvm::OwningPtr<BlockSemaInfo> BSI(CurBlock);
- llvm::OwningPtr<Expr> Body(static_cast<Expr*>(body));
-
- // Pop off CurBlock, handle nested blocks.
- CurBlock = CurBlock->PrevBlockInfo;
-
- if (BSI->ReturnType) {
- Diag(CaretLoc, diag::err_return_in_block_expression);
- return true;
- }
-
- QualType RetTy = Body->getType();
-
- llvm::SmallVector<QualType, 8> ArgTypes;
- for (unsigned i = 0, e = BSI->Params.size(); i != e; ++i)
- ArgTypes.push_back(BSI->Params[i]->getType());
-
- QualType BlockTy;
- if (!BSI->hasPrototype)
- BlockTy = Context.getFunctionTypeNoProto(RetTy);
- else
- BlockTy = Context.getFunctionType(RetTy, &ArgTypes[0], ArgTypes.size(),
- BSI->isVariadic);
-
- BlockTy = Context.getBlockPointerType(BlockTy);
- return new BlockExprExpr(CaretLoc, BlockTy,
- &BSI->Params[0], BSI->Params.size(), Body.take());
-}
-
/// ExprsMatchFnType - return true if the Exprs in array Args have
/// QualTypes that match the QualTypes of the arguments of the FnType.
/// The number of arguments has already been validated to match the number of
I(^{ });
- noop = ^noop; // expected-error {{incompatible block pointer types}}
-
return ^{printf("\nClosure\n"); }; // expected-error {{returning block that lives on the local stack}}
}
void test2() {
foo:
takeclosure(^{ x = 4; }); // expected-error {{expression is not assignable}}
-
- takeclosure(^test2());
- takeclosure(^(void)(void)printf("hello world!\n"));
-
}
void *X;
void test_arguments() {
- takeintint(^(int x)(x+1));
-
- // Closure expr of statement expr.
- takeintint(^(int x)({ return 42; })); // expected-error {{return not allowed in block expression literal}}
-
int y;
- takeintint(^(int x)(x+y));
-#if 0
- // FIXME: this causes clang to crash.
- X = ^(x+r); // expected-error {{expected ')' in argument list}}
-#endif
int (^c)(char);
(1 ? c : 0)('x');
(1 ? 0 : c)('x');