implement parsing, AST, and basic Sema support for 'defer'.

SILGen support and diagnosing jumps out of a defer aren't done
yet.



Swift SVN r27759
This commit is contained in:
Chris Lattner
2015-04-26 15:16:37 +00:00
parent 79b976dfc3
commit ee96164996
16 changed files with 118 additions and 0 deletions

View File

@@ -728,6 +728,10 @@ ERROR(expected_close_after_else,stmt_parsing,none,
ERROR(expected_expr_return,stmt_parsing,PointsToFirstBadToken,
"expected expression in 'return' statement", ())
// Defer Statement
ERROR(expected_lbrace_after_defer,stmt_parsing,PointsToFirstBadToken,
"expected '{' after 'defer'", ())
// If/While Conditions
ERROR(expected_expr_conditional_letbinding,stmt_parsing,none,
"expected 'let' or 'var' in conditional", ())

View File

@@ -175,6 +175,32 @@ public:
static bool classof(const Stmt *S) { return S->getKind() == StmtKind::Return;}
};
/// DeferStmt - A 'defer' statement. This runs the substatement it contains
/// when the enclosing scope is exited.
///
/// defer { cleanUp() }
///
class DeferStmt : public Stmt {
SourceLoc DeferLoc;
BraceStmt *Body;
public:
DeferStmt(SourceLoc DeferLoc, BraceStmt *Body)
: Stmt(StmtKind::Defer, /*implicit*/false),
DeferLoc(DeferLoc), Body(Body) {}
SourceLoc getDeferLoc() const { return DeferLoc; }
SourceLoc getStartLoc() const { return DeferLoc; }
SourceLoc getEndLoc() const;
BraceStmt *getBody() const { return Body; }
void setBody(BraceStmt *body) { Body = body; }
static bool classof(const Stmt *S) { return S->getKind() == StmtKind::Defer; }
};
/// This represents an entry in an "if" or "while" condition. Pattern bindings

View File

@@ -42,6 +42,7 @@
STMT(Brace, Stmt)
STMT(Return, Stmt)
STMT(Defer, Stmt)
ABSTRACT_STMT(Labeled, Stmt)
ABSTRACT_STMT(LabeledConditional, LabeledStmt)
LABELED_STMT(If, LabeledConditionalStmt)

View File

@@ -1153,6 +1153,7 @@ public:
ParserResult<Stmt> parseStmtBreak();
ParserResult<Stmt> parseStmtContinue();
ParserResult<Stmt> parseStmtReturn();
ParserResult<Stmt> parseStmtDefer();
ParserStatus parseStmtCondition(StmtCondition &Result, Diag<> ID);
ParserResult<Stmt> parseStmtIf(LabeledStmtInfo LabelInfo);
ParserResult<Stmt> parseStmtIfConfig(BraceItemListKind Kind

View File

@@ -73,6 +73,7 @@ KEYWORD(sil_witness_table) // This is only enabled when parsing .sil files.
KEYWORD(sil_coverage_map) // This is only enabled when parsing .sil files.
// Statement keywords.
STMT_KEYWORD(defer)
STMT_KEYWORD(if)
STMT_KEYWORD(do)
STMT_KEYWORD(repeat)