diff options
author | Steve Naroff <snaroff@apple.com> | 2008-09-03 18:15:37 +0000 |
---|---|---|
committer | Steve Naroff <snaroff@apple.com> | 2008-09-03 18:15:37 +0000 |
commit | 4eb206bebcdab28ababe8df55c6185cec2cdc071 (patch) | |
tree | 467e7c58fdf64d3a041080d5b516b52740dfd562 /lib/Sema/SemaStmt.cpp | |
parent | a89d197c919e01ccb54d7822cc8886c373bcba00 (diff) |
Add semantic analysis for "blocks".
Highlights...
- 4 new AST nodes, BlockExpr, BlockStmtExpr, BlockExprExpr, BlockDeclRefExpr.
- Sema::ActOnBlockStart(), ActOnBlockError(), ActOnBlockStmtExpr(), ActOnBlockExprExpr(), ActOnBlockReturnStmt().
Next steps...
- hack Sema::ActOnIdentifierExpr() to deal with block decl refs.
- add attribute handler for byref decls.
- add test cases.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@55710 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Sema/SemaStmt.cpp')
-rw-r--r-- | lib/Sema/SemaStmt.cpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp index 629ab3465d..98efc8a907 100644 --- a/lib/Sema/SemaStmt.cpp +++ b/lib/Sema/SemaStmt.cpp @@ -588,6 +588,10 @@ Sema::ActOnObjCForCollectionStmt(SourceLocation ForLoc, Action::StmtResult Sema::ActOnGotoStmt(SourceLocation GotoLoc, SourceLocation LabelLoc, IdentifierInfo *LabelII) { + // If we are in a block, reject all gotos for now. + if (CurBlock) + return Diag(GotoLoc, diag::err_goto_in_block); + // Look up the record for this label identifier. LabelStmt *&LabelDecl = LabelMap[LabelII]; @@ -630,10 +634,61 @@ Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope) { return new BreakStmt(BreakLoc); } +/// ActOnBlockReturnStmt - Utilty routine to figure out block's return type. +/// +Action::StmtResult +Sema::ActOnBlockReturnStmt(SourceLocation ReturnLoc, Expr *RetValExp) { + + // If this is the first return we've seen in the block, infer the type of + // the block from it. + if (CurBlock->ReturnType == 0) { + if (RetValExp) + CurBlock->ReturnType = RetValExp->getType().getTypePtr(); + else + CurBlock->ReturnType = Context.VoidTy.getTypePtr(); + return new ReturnStmt(ReturnLoc, RetValExp); + } + + // Otherwise, verify that this result type matches the previous one. We are + // pickier with blocks than for normal functions because we don't have GCC + // compatibility to worry about here. + if (CurBlock->ReturnType->isVoidType()) { + if (RetValExp) { + Diag(ReturnLoc, diag::err_return_block_has_expr); + delete RetValExp; + RetValExp = 0; + } + return new ReturnStmt(ReturnLoc, RetValExp); + } + + if (!RetValExp) { + Diag(ReturnLoc, diag::err_block_return_missing_expr); + return true; + } + + // we have a non-void block with an expression, continue checking + QualType RetValType = RetValExp->getType(); + + // For now, restrict multiple return statements in a block to have + // strict compatible types only. + QualType BlockQT = QualType(CurBlock->ReturnType, 0); + if (Context.getCanonicalType(BlockQT).getTypePtr() + != Context.getCanonicalType(RetValType).getTypePtr()) { + DiagnoseAssignmentResult(Incompatible, ReturnLoc, BlockQT, + RetValType, RetValExp, "returning"); + return true; + } + + if (RetValExp) CheckReturnStackAddr(RetValExp, BlockQT, ReturnLoc); + + return new ReturnStmt(ReturnLoc, (Expr*)RetValExp); +} Action::StmtResult Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprTy *rex) { Expr *RetValExp = static_cast<Expr *>(rex); + if (CurBlock) + return ActOnBlockReturnStmt(ReturnLoc, RetValExp); QualType FnRetType = getCurFunctionDecl() ? getCurFunctionDecl()->getResultType() : getCurMethodDecl()->getResultType(); |