2007-07-11 21:01:13 +04:00
|
|
|
//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 22:59:25 +03:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-07-11 21:01:13 +04:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the Statement and Block portions of the Parser
|
|
|
|
// interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Parse/Parser.h"
|
2008-10-20 10:51:33 +04:00
|
|
|
#include "ExtensionRAIIObject.h"
|
2007-07-11 21:01:13 +04:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2008-02-08 02:24:32 +03:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2007-07-11 21:01:13 +04:00
|
|
|
#include "clang/Parse/DeclSpec.h"
|
|
|
|
#include "clang/Parse/Scope.h"
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// C99 6.8: Statements and Blocks.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
|
|
|
|
/// StatementOrDeclaration:
|
|
|
|
/// statement
|
|
|
|
/// declaration
|
|
|
|
///
|
|
|
|
/// statement:
|
|
|
|
/// labeled-statement
|
|
|
|
/// compound-statement
|
|
|
|
/// expression-statement
|
|
|
|
/// selection-statement
|
|
|
|
/// iteration-statement
|
|
|
|
/// jump-statement
|
2008-09-07 22:58:01 +04:00
|
|
|
/// [C++] declaration-statement
|
2007-10-05 00:19:06 +04:00
|
|
|
/// [OBC] objc-throw-statement
|
|
|
|
/// [OBC] objc-try-catch-statement
|
2008-01-29 21:21:32 +03:00
|
|
|
/// [OBC] objc-synchronized-statement
|
2007-07-11 21:01:13 +04:00
|
|
|
/// [GNU] asm-statement
|
|
|
|
/// [OMP] openmp-construct [TODO]
|
|
|
|
///
|
|
|
|
/// labeled-statement:
|
|
|
|
/// identifier ':' statement
|
|
|
|
/// 'case' constant-expression ':' statement
|
|
|
|
/// 'default' ':' statement
|
|
|
|
///
|
|
|
|
/// selection-statement:
|
|
|
|
/// if-statement
|
|
|
|
/// switch-statement
|
|
|
|
///
|
|
|
|
/// iteration-statement:
|
|
|
|
/// while-statement
|
|
|
|
/// do-statement
|
|
|
|
/// for-statement
|
|
|
|
///
|
|
|
|
/// expression-statement:
|
|
|
|
/// expression[opt] ';'
|
|
|
|
///
|
|
|
|
/// jump-statement:
|
|
|
|
/// 'goto' identifier ';'
|
|
|
|
/// 'continue' ';'
|
|
|
|
/// 'break' ';'
|
|
|
|
/// 'return' expression[opt] ';'
|
|
|
|
/// [GNU] 'goto' '*' expression ';'
|
|
|
|
///
|
2007-10-05 00:19:06 +04:00
|
|
|
/// [OBC] objc-throw-statement:
|
|
|
|
/// [OBC] '@' 'throw' expression ';'
|
|
|
|
/// [OBC] '@' 'throw' ';'
|
2007-07-11 21:01:13 +04:00
|
|
|
///
|
|
|
|
Parser::StmtResult Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
|
|
|
|
const char *SemiError = 0;
|
|
|
|
Parser::StmtResult Res;
|
|
|
|
|
|
|
|
// Cases in this switch statement should fall through if the parser expects
|
|
|
|
// the token to end in a semicolon (in which case SemiError should be set),
|
|
|
|
// or they directly 'return;' if not.
|
2007-09-19 23:14:32 +04:00
|
|
|
tok::TokenKind Kind = Tok.getKind();
|
|
|
|
SourceLocation AtLoc;
|
|
|
|
switch (Kind) {
|
|
|
|
case tok::at: // May be a @try or @throw statement
|
|
|
|
{
|
|
|
|
AtLoc = ConsumeToken(); // consume @
|
2008-02-06 00:27:35 +03:00
|
|
|
return ParseObjCAtStatement(AtLoc);
|
2007-09-19 23:14:32 +04:00
|
|
|
}
|
|
|
|
|
2008-07-13 01:04:42 +04:00
|
|
|
case tok::identifier:
|
|
|
|
if (NextToken().is(tok::colon)) { // C99 6.8.1: labeled-statement
|
|
|
|
// identifier ':' statement
|
|
|
|
return ParseLabeledStatement();
|
|
|
|
}
|
|
|
|
// PASS THROUGH.
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
default:
|
2008-10-05 04:06:24 +04:00
|
|
|
if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
|
2008-03-13 09:29:04 +03:00
|
|
|
SourceLocation DeclStart = Tok.getLocation();
|
|
|
|
DeclTy *Res = ParseDeclaration(Declarator::BlockContext);
|
|
|
|
// FIXME: Pass in the right location for the end of the declstmt.
|
2008-03-13 09:29:54 +03:00
|
|
|
return Actions.ActOnDeclStmt(Res, DeclStart, DeclStart);
|
2007-10-09 21:41:39 +04:00
|
|
|
} else if (Tok.is(tok::r_brace)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
Diag(Tok, diag::err_expected_statement);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// expression[opt] ';'
|
2007-10-05 00:19:06 +04:00
|
|
|
ExprResult Res = ParseExpression();
|
2007-07-11 21:01:13 +04:00
|
|
|
if (Res.isInvalid) {
|
|
|
|
// If the expression is invalid, skip ahead to the next semicolon. Not
|
|
|
|
// doing this opens us up to the possibility of infinite loops if
|
|
|
|
// ParseExpression does not consume any tokens.
|
|
|
|
SkipUntil(tok::semi);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Otherwise, eat the semicolon.
|
|
|
|
ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
|
2007-09-16 18:56:35 +04:00
|
|
|
return Actions.ActOnExprStmt(Res.Val);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
case tok::kw_case: // C99 6.8.1: labeled-statement
|
|
|
|
return ParseCaseStatement();
|
|
|
|
case tok::kw_default: // C99 6.8.1: labeled-statement
|
|
|
|
return ParseDefaultStatement();
|
|
|
|
|
|
|
|
case tok::l_brace: // C99 6.8.2: compound-statement
|
|
|
|
return ParseCompoundStatement();
|
|
|
|
case tok::semi: // C99 6.8.3p3: expression[opt] ';'
|
2007-09-16 18:56:35 +04:00
|
|
|
return Actions.ActOnNullStmt(ConsumeToken());
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
case tok::kw_if: // C99 6.8.4.1: if-statement
|
|
|
|
return ParseIfStatement();
|
|
|
|
case tok::kw_switch: // C99 6.8.4.2: switch-statement
|
|
|
|
return ParseSwitchStatement();
|
|
|
|
|
|
|
|
case tok::kw_while: // C99 6.8.5.1: while-statement
|
|
|
|
return ParseWhileStatement();
|
|
|
|
case tok::kw_do: // C99 6.8.5.2: do-statement
|
|
|
|
Res = ParseDoStatement();
|
|
|
|
SemiError = "do/while loop";
|
|
|
|
break;
|
|
|
|
case tok::kw_for: // C99 6.8.5.3: for-statement
|
|
|
|
return ParseForStatement();
|
|
|
|
|
|
|
|
case tok::kw_goto: // C99 6.8.6.1: goto-statement
|
|
|
|
Res = ParseGotoStatement();
|
|
|
|
SemiError = "goto statement";
|
|
|
|
break;
|
|
|
|
case tok::kw_continue: // C99 6.8.6.2: continue-statement
|
|
|
|
Res = ParseContinueStatement();
|
|
|
|
SemiError = "continue statement";
|
|
|
|
break;
|
|
|
|
case tok::kw_break: // C99 6.8.6.3: break-statement
|
|
|
|
Res = ParseBreakStatement();
|
|
|
|
SemiError = "break statement";
|
|
|
|
break;
|
|
|
|
case tok::kw_return: // C99 6.8.6.4: return-statement
|
|
|
|
Res = ParseReturnStatement();
|
|
|
|
SemiError = "return statement";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case tok::kw_asm:
|
2008-02-07 06:50:06 +03:00
|
|
|
bool msAsm = false;
|
|
|
|
Res = ParseAsmStatement(msAsm);
|
|
|
|
if (msAsm) return Res;
|
2007-07-11 21:01:13 +04:00
|
|
|
SemiError = "asm statement";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we reached this code, the statement must end in a semicolon.
|
2007-10-09 21:41:39 +04:00
|
|
|
if (Tok.is(tok::semi)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
ConsumeToken();
|
|
|
|
} else {
|
|
|
|
Diag(Tok, diag::err_expected_semi_after, SemiError);
|
|
|
|
SkipUntil(tok::semi);
|
|
|
|
}
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2008-07-10 02:53:07 +04:00
|
|
|
/// ParseLabeledStatement - We have an identifier and a ':' after it.
|
2007-07-11 21:01:13 +04:00
|
|
|
///
|
|
|
|
/// labeled-statement:
|
|
|
|
/// identifier ':' statement
|
|
|
|
/// [GNU] identifier ':' attributes[opt] statement
|
|
|
|
///
|
2008-07-10 02:53:07 +04:00
|
|
|
Parser::StmtResult Parser::ParseLabeledStatement() {
|
2007-10-09 21:41:39 +04:00
|
|
|
assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
|
2007-07-11 21:01:13 +04:00
|
|
|
"Not an identifier!");
|
|
|
|
|
2007-07-20 20:59:19 +04:00
|
|
|
Token IdentTok = Tok; // Save the whole token.
|
2007-07-11 21:01:13 +04:00
|
|
|
ConsumeToken(); // eat the identifier.
|
2008-07-10 02:53:07 +04:00
|
|
|
|
|
|
|
assert(Tok.is(tok::colon) && "Not a label!");
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
// identifier ':' statement
|
2008-07-10 02:53:07 +04:00
|
|
|
SourceLocation ColonLoc = ConsumeToken();
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2008-07-10 02:53:07 +04:00
|
|
|
// Read label attributes, if present.
|
|
|
|
DeclTy *AttrList = 0;
|
|
|
|
if (Tok.is(tok::kw___attribute))
|
|
|
|
// TODO: save these somewhere.
|
|
|
|
AttrList = ParseAttributes();
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2008-07-10 02:53:07 +04:00
|
|
|
StmtResult SubStmt = ParseStatement();
|
|
|
|
|
|
|
|
// Broken substmt shouldn't prevent the label from being added to the AST.
|
|
|
|
if (SubStmt.isInvalid)
|
|
|
|
SubStmt = Actions.ActOnNullStmt(ColonLoc);
|
|
|
|
|
|
|
|
return Actions.ActOnLabelStmt(IdentTok.getLocation(),
|
|
|
|
IdentTok.getIdentifierInfo(),
|
|
|
|
ColonLoc, SubStmt.Val);
|
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
/// ParseCaseStatement
|
|
|
|
/// labeled-statement:
|
|
|
|
/// 'case' constant-expression ':' statement
|
|
|
|
/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
|
|
|
|
///
|
|
|
|
/// Note that this does not parse the 'statement' at the end.
|
|
|
|
///
|
|
|
|
Parser::StmtResult Parser::ParseCaseStatement() {
|
2007-10-09 21:41:39 +04:00
|
|
|
assert(Tok.is(tok::kw_case) && "Not a case stmt!");
|
2007-07-11 21:01:13 +04:00
|
|
|
SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
|
|
|
|
|
|
|
|
ExprResult LHS = ParseConstantExpression();
|
|
|
|
if (LHS.isInvalid) {
|
|
|
|
SkipUntil(tok::colon);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// GNU case range extension.
|
|
|
|
SourceLocation DotDotDotLoc;
|
|
|
|
ExprTy *RHSVal = 0;
|
2007-10-09 21:41:39 +04:00
|
|
|
if (Tok.is(tok::ellipsis)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
Diag(Tok, diag::ext_gnu_case_range);
|
|
|
|
DotDotDotLoc = ConsumeToken();
|
|
|
|
|
|
|
|
ExprResult RHS = ParseConstantExpression();
|
|
|
|
if (RHS.isInvalid) {
|
|
|
|
SkipUntil(tok::colon);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
RHSVal = RHS.Val;
|
|
|
|
}
|
|
|
|
|
2007-10-09 21:41:39 +04:00
|
|
|
if (Tok.isNot(tok::colon)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
Diag(Tok, diag::err_expected_colon_after, "'case'");
|
|
|
|
SkipUntil(tok::colon);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceLocation ColonLoc = ConsumeToken();
|
|
|
|
|
|
|
|
// Diagnose the common error "switch (X) { case 4: }", which is not valid.
|
2007-10-09 21:41:39 +04:00
|
|
|
if (Tok.is(tok::r_brace)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
Diag(Tok, diag::err_label_end_of_compound_statement);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
StmtResult SubStmt = ParseStatement();
|
|
|
|
|
|
|
|
// Broken substmt shouldn't prevent the case from being added to the AST.
|
|
|
|
if (SubStmt.isInvalid)
|
2007-09-16 18:56:35 +04:00
|
|
|
SubStmt = Actions.ActOnNullStmt(ColonLoc);
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2007-09-16 18:56:35 +04:00
|
|
|
return Actions.ActOnCaseStmt(CaseLoc, LHS.Val, DotDotDotLoc, RHSVal, ColonLoc,
|
2007-07-11 21:01:13 +04:00
|
|
|
SubStmt.Val);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseDefaultStatement
|
|
|
|
/// labeled-statement:
|
|
|
|
/// 'default' ':' statement
|
|
|
|
/// Note that this does not parse the 'statement' at the end.
|
|
|
|
///
|
|
|
|
Parser::StmtResult Parser::ParseDefaultStatement() {
|
2007-10-09 21:41:39 +04:00
|
|
|
assert(Tok.is(tok::kw_default) && "Not a default stmt!");
|
2007-07-11 21:01:13 +04:00
|
|
|
SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
|
|
|
|
|
2007-10-09 21:41:39 +04:00
|
|
|
if (Tok.isNot(tok::colon)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
Diag(Tok, diag::err_expected_colon_after, "'default'");
|
|
|
|
SkipUntil(tok::colon);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceLocation ColonLoc = ConsumeToken();
|
|
|
|
|
|
|
|
// Diagnose the common error "switch (X) {... default: }", which is not valid.
|
2007-10-09 21:41:39 +04:00
|
|
|
if (Tok.is(tok::r_brace)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
Diag(Tok, diag::err_label_end_of_compound_statement);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
StmtResult SubStmt = ParseStatement();
|
|
|
|
if (SubStmt.isInvalid)
|
|
|
|
return true;
|
|
|
|
|
2007-09-16 18:56:35 +04:00
|
|
|
return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc, SubStmt.Val, CurScope);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// ParseCompoundStatement - Parse a "{}" block.
|
|
|
|
///
|
|
|
|
/// compound-statement: [C99 6.8.2]
|
|
|
|
/// { block-item-list[opt] }
|
|
|
|
/// [GNU] { label-declarations block-item-list } [TODO]
|
|
|
|
///
|
|
|
|
/// block-item-list:
|
|
|
|
/// block-item
|
|
|
|
/// block-item-list block-item
|
|
|
|
///
|
|
|
|
/// block-item:
|
|
|
|
/// declaration
|
2007-08-27 05:01:57 +04:00
|
|
|
/// [GNU] '__extension__' declaration
|
2007-07-11 21:01:13 +04:00
|
|
|
/// statement
|
|
|
|
/// [OMP] openmp-directive [TODO]
|
|
|
|
///
|
|
|
|
/// [GNU] label-declarations:
|
|
|
|
/// [GNU] label-declaration
|
|
|
|
/// [GNU] label-declarations label-declaration
|
|
|
|
///
|
|
|
|
/// [GNU] label-declaration:
|
|
|
|
/// [GNU] '__label__' identifier-list ';'
|
|
|
|
///
|
|
|
|
/// [OMP] openmp-directive: [TODO]
|
|
|
|
/// [OMP] barrier-directive
|
|
|
|
/// [OMP] flush-directive
|
|
|
|
///
|
2007-09-01 01:49:55 +04:00
|
|
|
Parser::StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
|
2007-10-09 21:41:39 +04:00
|
|
|
assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2007-08-26 10:24:45 +04:00
|
|
|
// Enter a scope to hold everything within the compound stmt. Compound
|
|
|
|
// statements can always hold declarations.
|
|
|
|
EnterScope(Scope::DeclScope);
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
// Parse the statements in the body.
|
2007-09-01 01:49:55 +04:00
|
|
|
StmtResult Body = ParseCompoundStatementBody(isStmtExpr);
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
ExitScope();
|
|
|
|
return Body;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
|
2007-09-16 18:56:35 +04:00
|
|
|
/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
|
2007-07-11 21:01:13 +04:00
|
|
|
/// consume the '}' at the end of the block. It does not manipulate the scope
|
|
|
|
/// stack.
|
2007-09-01 01:49:55 +04:00
|
|
|
Parser::StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
|
2007-07-11 21:01:13 +04:00
|
|
|
SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
|
|
|
|
|
|
|
|
// TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
|
2007-08-27 05:01:57 +04:00
|
|
|
// only allowed at the start of a compound stmt regardless of the language.
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
llvm::SmallVector<StmtTy*, 32> Stmts;
|
2007-10-09 21:41:39 +04:00
|
|
|
while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
|
2007-08-27 05:01:57 +04:00
|
|
|
StmtResult R;
|
2007-10-09 21:41:39 +04:00
|
|
|
if (Tok.isNot(tok::kw___extension__)) {
|
2007-08-27 05:01:57 +04:00
|
|
|
R = ParseStatementOrDeclaration(false);
|
|
|
|
} else {
|
|
|
|
// __extension__ can start declarations and it can also be a unary
|
|
|
|
// operator for expressions. Consume multiple __extension__ markers here
|
|
|
|
// until we can determine which is which.
|
|
|
|
SourceLocation ExtLoc = ConsumeToken();
|
2007-10-09 21:41:39 +04:00
|
|
|
while (Tok.is(tok::kw___extension__))
|
2007-08-27 05:01:57 +04:00
|
|
|
ConsumeToken();
|
|
|
|
|
2008-03-13 09:32:11 +03:00
|
|
|
// __extension__ silences extension warnings in the subexpression.
|
2008-10-20 10:51:33 +04:00
|
|
|
ExtensionRAIIObject O(Diags); // Use RAII to do this.
|
|
|
|
|
2007-08-27 05:01:57 +04:00
|
|
|
// If this is the start of a declaration, parse it as such.
|
2008-10-05 04:06:24 +04:00
|
|
|
if (isDeclarationStatement()) {
|
2007-08-27 05:01:57 +04:00
|
|
|
// FIXME: Save the __extension__ on the decl as a node somehow.
|
2008-03-13 09:29:04 +03:00
|
|
|
SourceLocation DeclStart = Tok.getLocation();
|
|
|
|
DeclTy *Res = ParseDeclaration(Declarator::BlockContext);
|
|
|
|
// FIXME: Pass in the right location for the end of the declstmt.
|
2008-03-13 09:29:54 +03:00
|
|
|
R = Actions.ActOnDeclStmt(Res, DeclStart, DeclStart);
|
2007-08-27 05:01:57 +04:00
|
|
|
} else {
|
|
|
|
// Otherwise this was a unary __extension__ marker. Parse the
|
|
|
|
// subexpression and add the __extension__ unary op.
|
|
|
|
ExprResult Res = ParseCastExpression(false);
|
2008-03-13 09:32:11 +03:00
|
|
|
|
2007-08-27 05:01:57 +04:00
|
|
|
if (Res.isInvalid) {
|
|
|
|
SkipUntil(tok::semi);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the __extension__ node to the AST.
|
2007-09-16 07:34:24 +04:00
|
|
|
Res = Actions.ActOnUnaryOp(ExtLoc, tok::kw___extension__, Res.Val);
|
2007-08-27 05:01:57 +04:00
|
|
|
if (Res.isInvalid)
|
|
|
|
continue;
|
|
|
|
|
2008-10-20 10:51:33 +04:00
|
|
|
// Eat the semicolon at the end of stmt and convert the expr into a
|
|
|
|
// statement.
|
2007-08-27 05:01:57 +04:00
|
|
|
ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
|
2007-09-16 18:56:35 +04:00
|
|
|
R = Actions.ActOnExprStmt(Res.Val);
|
2007-08-27 05:01:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
if (!R.isInvalid && R.Val)
|
|
|
|
Stmts.push_back(R.Val);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We broke out of the while loop because we found a '}' or EOF.
|
2007-10-09 21:41:39 +04:00
|
|
|
if (Tok.isNot(tok::r_brace)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
Diag(Tok, diag::err_expected_rbrace);
|
2008-01-31 21:29:10 +03:00
|
|
|
return true;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
SourceLocation RBraceLoc = ConsumeBrace();
|
2007-09-16 18:56:35 +04:00
|
|
|
return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc,
|
2007-09-01 01:49:55 +04:00
|
|
|
&Stmts[0], Stmts.size(), isStmtExpr);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseIfStatement
|
|
|
|
/// if-statement: [C99 6.8.4.1]
|
|
|
|
/// 'if' '(' expression ')' statement
|
|
|
|
/// 'if' '(' expression ')' statement 'else' statement
|
2008-09-10 00:38:47 +04:00
|
|
|
/// [C++] 'if' '(' condition ')' statement
|
|
|
|
/// [C++] 'if' '(' condition ')' statement 'else' statement
|
2007-07-11 21:01:13 +04:00
|
|
|
///
|
|
|
|
Parser::StmtResult Parser::ParseIfStatement() {
|
2007-10-09 21:41:39 +04:00
|
|
|
assert(Tok.is(tok::kw_if) && "Not an if stmt!");
|
2007-07-11 21:01:13 +04:00
|
|
|
SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
|
|
|
|
|
2007-10-09 21:41:39 +04:00
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
Diag(Tok, diag::err_expected_lparen_after, "if");
|
|
|
|
SkipUntil(tok::semi);
|
|
|
|
return true;
|
|
|
|
}
|
2008-09-10 00:38:47 +04:00
|
|
|
|
2008-09-11 07:06:46 +04:00
|
|
|
bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
|
|
|
|
|
2007-08-27 03:08:06 +04:00
|
|
|
// C99 6.8.4p3 - In C99, the if statement is a block. This is not
|
|
|
|
// the case for C90.
|
2008-09-11 07:06:46 +04:00
|
|
|
//
|
|
|
|
// C++ 6.4p3:
|
|
|
|
// A name introduced by a declaration in a condition is in scope from its
|
|
|
|
// point of declaration until the end of the substatements controlled by the
|
|
|
|
// condition.
|
2008-09-12 03:08:39 +04:00
|
|
|
// C++ 3.3.2p4:
|
|
|
|
// Names declared in the for-init-statement, and in the condition of if,
|
|
|
|
// while, for, and switch statements are local to the if, while, for, or
|
|
|
|
// switch statement (including the controlled statement).
|
2008-09-11 07:06:46 +04:00
|
|
|
//
|
|
|
|
if (C99orCXX)
|
2008-09-10 00:38:47 +04:00
|
|
|
EnterScope(Scope::DeclScope | Scope::ControlScope);
|
2007-08-27 03:08:06 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Parse the condition.
|
2008-09-10 00:38:47 +04:00
|
|
|
ExprResult CondExp;
|
|
|
|
if (getLang().CPlusPlus) {
|
|
|
|
SourceLocation LParenLoc = ConsumeParen();
|
|
|
|
CondExp = ParseCXXCondition();
|
|
|
|
MatchRHSPunctuation(tok::r_paren, LParenLoc);
|
|
|
|
} else {
|
|
|
|
CondExp = ParseSimpleParenExpression();
|
|
|
|
}
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
if (CondExp.isInvalid) {
|
|
|
|
SkipUntil(tok::semi);
|
2008-09-11 07:06:46 +04:00
|
|
|
if (C99orCXX)
|
2007-08-27 03:08:06 +04:00
|
|
|
ExitScope();
|
2007-07-11 21:01:13 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-08-22 09:28:50 +04:00
|
|
|
// C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
|
2007-08-22 09:33:11 +04:00
|
|
|
// there is no compound stmt. C90 does not have this clause. We only do this
|
|
|
|
// if the body isn't a compound statement to avoid push/pop in common cases.
|
2008-09-11 07:06:46 +04:00
|
|
|
//
|
|
|
|
// C++ 6.4p1:
|
|
|
|
// The substatement in a selection-statement (each substatement, in the else
|
|
|
|
// form of the if statement) implicitly defines a local scope.
|
|
|
|
//
|
|
|
|
// For C++ we create a scope for the condition and a new scope for
|
|
|
|
// substatements because:
|
|
|
|
// -When the 'then' scope exits, we want the condition declaration to still be
|
|
|
|
// active for the 'else' scope too.
|
|
|
|
// -Sema will detect name clashes by considering declarations of a
|
|
|
|
// 'ControlScope' as part of its direct subscope.
|
|
|
|
// -If we wanted the condition and substatement to be in the same scope, we
|
|
|
|
// would have to notify ParseStatement not to create a new scope. It's
|
|
|
|
// simpler to let it create a new scope.
|
|
|
|
//
|
|
|
|
bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
|
2007-08-26 10:24:45 +04:00
|
|
|
if (NeedsInnerScope) EnterScope(Scope::DeclScope);
|
2008-09-10 00:38:47 +04:00
|
|
|
|
2007-10-29 08:08:52 +03:00
|
|
|
// Read the 'then' stmt.
|
|
|
|
SourceLocation ThenStmtLoc = Tok.getLocation();
|
|
|
|
StmtResult ThenStmt = ParseStatement();
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2007-08-22 09:16:28 +04:00
|
|
|
// Pop the 'if' scope if needed.
|
2007-08-22 09:33:11 +04:00
|
|
|
if (NeedsInnerScope) ExitScope();
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
// If it has an else, parse it.
|
|
|
|
SourceLocation ElseLoc;
|
2007-10-29 08:08:52 +03:00
|
|
|
SourceLocation ElseStmtLoc;
|
2007-07-11 21:01:13 +04:00
|
|
|
StmtResult ElseStmt(false);
|
2007-10-29 08:08:52 +03:00
|
|
|
|
2007-10-09 21:41:39 +04:00
|
|
|
if (Tok.is(tok::kw_else)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
ElseLoc = ConsumeToken();
|
2007-08-22 09:16:28 +04:00
|
|
|
|
2007-08-22 09:28:50 +04:00
|
|
|
// C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
|
2007-08-22 09:33:11 +04:00
|
|
|
// there is no compound stmt. C90 does not have this clause. We only do
|
|
|
|
// this if the body isn't a compound statement to avoid push/pop in common
|
|
|
|
// cases.
|
2008-09-11 07:06:46 +04:00
|
|
|
//
|
|
|
|
// C++ 6.4p1:
|
|
|
|
// The substatement in a selection-statement (each substatement, in the else
|
|
|
|
// form of the if statement) implicitly defines a local scope.
|
|
|
|
//
|
|
|
|
NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
|
2007-08-26 10:24:45 +04:00
|
|
|
if (NeedsInnerScope) EnterScope(Scope::DeclScope);
|
2007-08-22 09:16:28 +04:00
|
|
|
|
2007-10-29 08:08:52 +03:00
|
|
|
ElseStmtLoc = Tok.getLocation();
|
2007-07-11 21:01:13 +04:00
|
|
|
ElseStmt = ParseStatement();
|
2007-08-22 09:16:28 +04:00
|
|
|
|
|
|
|
// Pop the 'else' scope if needed.
|
2007-08-22 09:33:11 +04:00
|
|
|
if (NeedsInnerScope) ExitScope();
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2008-09-11 07:06:46 +04:00
|
|
|
if (C99orCXX)
|
2007-08-27 03:08:06 +04:00
|
|
|
ExitScope();
|
|
|
|
|
2007-10-29 08:08:52 +03:00
|
|
|
// If the then or else stmt is invalid and the other is valid (and present),
|
|
|
|
// make turn the invalid one into a null stmt to avoid dropping the other
|
|
|
|
// part. If both are invalid, return error.
|
|
|
|
if ((ThenStmt.isInvalid && ElseStmt.isInvalid) ||
|
|
|
|
(ThenStmt.isInvalid && ElseStmt.Val == 0) ||
|
|
|
|
(ThenStmt.Val == 0 && ElseStmt.isInvalid)) {
|
|
|
|
// Both invalid, or one is invalid and other is non-present: delete cond and
|
|
|
|
// return error.
|
|
|
|
Actions.DeleteExpr(CondExp.Val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now if either are invalid, replace with a ';'.
|
|
|
|
if (ThenStmt.isInvalid)
|
|
|
|
ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
|
|
|
|
if (ElseStmt.isInvalid)
|
|
|
|
ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
|
|
|
|
|
|
|
|
return Actions.ActOnIfStmt(IfLoc, CondExp.Val, ThenStmt.Val,
|
2007-07-11 21:01:13 +04:00
|
|
|
ElseLoc, ElseStmt.Val);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseSwitchStatement
|
|
|
|
/// switch-statement:
|
|
|
|
/// 'switch' '(' expression ')' statement
|
2008-09-10 00:38:47 +04:00
|
|
|
/// [C++] 'switch' '(' condition ')' statement
|
2007-07-11 21:01:13 +04:00
|
|
|
Parser::StmtResult Parser::ParseSwitchStatement() {
|
2007-10-09 21:41:39 +04:00
|
|
|
assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
|
2007-07-11 21:01:13 +04:00
|
|
|
SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
|
|
|
|
|
2007-10-09 21:41:39 +04:00
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
Diag(Tok, diag::err_expected_lparen_after, "switch");
|
|
|
|
SkipUntil(tok::semi);
|
|
|
|
return true;
|
|
|
|
}
|
2007-08-27 03:08:06 +04:00
|
|
|
|
2008-09-11 07:06:46 +04:00
|
|
|
bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
|
|
|
|
|
2007-08-27 03:08:06 +04:00
|
|
|
// C99 6.8.4p3 - In C99, the switch statement is a block. This is
|
|
|
|
// not the case for C90. Start the switch scope.
|
2008-09-11 07:06:46 +04:00
|
|
|
//
|
|
|
|
// C++ 6.4p3:
|
|
|
|
// A name introduced by a declaration in a condition is in scope from its
|
|
|
|
// point of declaration until the end of the substatements controlled by the
|
|
|
|
// condition.
|
2008-09-12 03:08:39 +04:00
|
|
|
// C++ 3.3.2p4:
|
|
|
|
// Names declared in the for-init-statement, and in the condition of if,
|
|
|
|
// while, for, and switch statements are local to the if, while, for, or
|
|
|
|
// switch statement (including the controlled statement).
|
2008-09-11 07:06:46 +04:00
|
|
|
//
|
|
|
|
if (C99orCXX)
|
2008-09-10 00:38:47 +04:00
|
|
|
EnterScope(Scope::BreakScope | Scope::DeclScope | Scope::ControlScope);
|
2007-08-27 03:08:06 +04:00
|
|
|
else
|
|
|
|
EnterScope(Scope::BreakScope);
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
// Parse the condition.
|
2008-09-10 00:38:47 +04:00
|
|
|
ExprResult Cond;
|
|
|
|
if (getLang().CPlusPlus) {
|
|
|
|
SourceLocation LParenLoc = ConsumeParen();
|
|
|
|
Cond = ParseCXXCondition();
|
|
|
|
MatchRHSPunctuation(tok::r_paren, LParenLoc);
|
|
|
|
} else {
|
|
|
|
Cond = ParseSimpleParenExpression();
|
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2007-07-22 11:07:56 +04:00
|
|
|
if (Cond.isInvalid) {
|
|
|
|
ExitScope();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-09-16 18:56:35 +04:00
|
|
|
StmtResult Switch = Actions.ActOnStartOfSwitchStmt(Cond.Val);
|
2007-07-22 11:07:56 +04:00
|
|
|
|
2007-08-22 09:28:50 +04:00
|
|
|
// C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
|
2007-08-22 09:33:11 +04:00
|
|
|
// there is no compound stmt. C90 does not have this clause. We only do this
|
|
|
|
// if the body isn't a compound statement to avoid push/pop in common cases.
|
2008-09-11 07:06:46 +04:00
|
|
|
//
|
|
|
|
// C++ 6.4p1:
|
|
|
|
// The substatement in a selection-statement (each substatement, in the else
|
|
|
|
// form of the if statement) implicitly defines a local scope.
|
|
|
|
//
|
|
|
|
// See comments in ParseIfStatement for why we create a scope for the
|
|
|
|
// condition and a new scope for substatement in C++.
|
|
|
|
//
|
|
|
|
bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
|
2007-08-26 10:24:45 +04:00
|
|
|
if (NeedsInnerScope) EnterScope(Scope::DeclScope);
|
2007-08-22 09:28:50 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Read the body statement.
|
|
|
|
StmtResult Body = ParseStatement();
|
|
|
|
|
2007-08-22 09:28:50 +04:00
|
|
|
// Pop the body scope if needed.
|
2007-08-22 09:33:11 +04:00
|
|
|
if (NeedsInnerScope) ExitScope();
|
2007-08-22 09:28:50 +04:00
|
|
|
|
2007-07-22 11:07:56 +04:00
|
|
|
if (Body.isInvalid) {
|
2007-09-16 18:56:35 +04:00
|
|
|
Body = Actions.ActOnNullStmt(Tok.getLocation());
|
2007-07-22 11:07:56 +04:00
|
|
|
// FIXME: Remove the case statement list from the Switch statement.
|
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2007-07-22 11:07:56 +04:00
|
|
|
ExitScope();
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2007-09-16 18:56:35 +04:00
|
|
|
return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.Val, Body.Val);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseWhileStatement
|
|
|
|
/// while-statement: [C99 6.8.5.1]
|
|
|
|
/// 'while' '(' expression ')' statement
|
2008-09-10 00:38:47 +04:00
|
|
|
/// [C++] 'while' '(' condition ')' statement
|
2007-07-11 21:01:13 +04:00
|
|
|
Parser::StmtResult Parser::ParseWhileStatement() {
|
2007-10-09 21:41:39 +04:00
|
|
|
assert(Tok.is(tok::kw_while) && "Not a while stmt!");
|
2007-07-11 21:01:13 +04:00
|
|
|
SourceLocation WhileLoc = Tok.getLocation();
|
|
|
|
ConsumeToken(); // eat the 'while'.
|
|
|
|
|
2007-10-09 21:41:39 +04:00
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
Diag(Tok, diag::err_expected_lparen_after, "while");
|
|
|
|
SkipUntil(tok::semi);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-09-11 07:06:46 +04:00
|
|
|
bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
|
|
|
|
|
2007-08-27 03:08:06 +04:00
|
|
|
// C99 6.8.5p5 - In C99, the while statement is a block. This is not
|
|
|
|
// the case for C90. Start the loop scope.
|
2008-09-11 07:06:46 +04:00
|
|
|
//
|
|
|
|
// C++ 6.4p3:
|
|
|
|
// A name introduced by a declaration in a condition is in scope from its
|
|
|
|
// point of declaration until the end of the substatements controlled by the
|
|
|
|
// condition.
|
2008-09-12 03:08:39 +04:00
|
|
|
// C++ 3.3.2p4:
|
|
|
|
// Names declared in the for-init-statement, and in the condition of if,
|
|
|
|
// while, for, and switch statements are local to the if, while, for, or
|
|
|
|
// switch statement (including the controlled statement).
|
2008-09-11 07:06:46 +04:00
|
|
|
//
|
|
|
|
if (C99orCXX)
|
2008-09-10 00:38:47 +04:00
|
|
|
EnterScope(Scope::BreakScope | Scope::ContinueScope |
|
|
|
|
Scope::DeclScope | Scope::ControlScope);
|
2007-08-27 03:08:06 +04:00
|
|
|
else
|
|
|
|
EnterScope(Scope::BreakScope | Scope::ContinueScope);
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
// Parse the condition.
|
2008-09-10 00:38:47 +04:00
|
|
|
ExprResult Cond;
|
|
|
|
if (getLang().CPlusPlus) {
|
|
|
|
SourceLocation LParenLoc = ConsumeParen();
|
|
|
|
Cond = ParseCXXCondition();
|
|
|
|
MatchRHSPunctuation(tok::r_paren, LParenLoc);
|
|
|
|
} else {
|
|
|
|
Cond = ParseSimpleParenExpression();
|
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2007-08-22 09:28:50 +04:00
|
|
|
// C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
|
2007-08-22 09:33:11 +04:00
|
|
|
// there is no compound stmt. C90 does not have this clause. We only do this
|
|
|
|
// if the body isn't a compound statement to avoid push/pop in common cases.
|
2008-09-11 07:06:46 +04:00
|
|
|
//
|
|
|
|
// C++ 6.5p2:
|
|
|
|
// The substatement in an iteration-statement implicitly defines a local scope
|
|
|
|
// which is entered and exited each time through the loop.
|
|
|
|
//
|
|
|
|
// See comments in ParseIfStatement for why we create a scope for the
|
|
|
|
// condition and a new scope for substatement in C++.
|
|
|
|
//
|
|
|
|
bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
|
2007-08-26 10:24:45 +04:00
|
|
|
if (NeedsInnerScope) EnterScope(Scope::DeclScope);
|
2007-08-22 09:28:50 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Read the body statement.
|
|
|
|
StmtResult Body = ParseStatement();
|
|
|
|
|
2007-08-22 09:28:50 +04:00
|
|
|
// Pop the body scope if needed.
|
2007-08-22 09:33:11 +04:00
|
|
|
if (NeedsInnerScope) ExitScope();
|
2007-08-22 09:28:50 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
ExitScope();
|
|
|
|
|
|
|
|
if (Cond.isInvalid || Body.isInvalid) return true;
|
|
|
|
|
2007-09-16 18:56:35 +04:00
|
|
|
return Actions.ActOnWhileStmt(WhileLoc, Cond.Val, Body.Val);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseDoStatement
|
|
|
|
/// do-statement: [C99 6.8.5.2]
|
|
|
|
/// 'do' statement 'while' '(' expression ')' ';'
|
|
|
|
/// Note: this lets the caller parse the end ';'.
|
|
|
|
Parser::StmtResult Parser::ParseDoStatement() {
|
2007-10-09 21:41:39 +04:00
|
|
|
assert(Tok.is(tok::kw_do) && "Not a do stmt!");
|
2007-07-11 21:01:13 +04:00
|
|
|
SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
|
|
|
|
|
2007-08-27 03:08:06 +04:00
|
|
|
// C99 6.8.5p5 - In C99, the do statement is a block. This is not
|
|
|
|
// the case for C90. Start the loop scope.
|
|
|
|
if (getLang().C99)
|
|
|
|
EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
|
|
|
|
else
|
|
|
|
EnterScope(Scope::BreakScope | Scope::ContinueScope);
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2007-08-22 09:28:50 +04:00
|
|
|
// C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
|
2007-08-22 09:33:11 +04:00
|
|
|
// there is no compound stmt. C90 does not have this clause. We only do this
|
|
|
|
// if the body isn't a compound statement to avoid push/pop in common cases.
|
2008-09-11 08:46:46 +04:00
|
|
|
//
|
|
|
|
// C++ 6.5p2:
|
|
|
|
// The substatement in an iteration-statement implicitly defines a local scope
|
|
|
|
// which is entered and exited each time through the loop.
|
|
|
|
//
|
|
|
|
bool NeedsInnerScope = (getLang().C99 || getLang().CPlusPlus) &&
|
|
|
|
Tok.isNot(tok::l_brace);
|
2007-08-26 10:24:45 +04:00
|
|
|
if (NeedsInnerScope) EnterScope(Scope::DeclScope);
|
2007-08-22 09:28:50 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Read the body statement.
|
|
|
|
StmtResult Body = ParseStatement();
|
|
|
|
|
2007-08-22 09:28:50 +04:00
|
|
|
// Pop the body scope if needed.
|
2007-08-22 09:33:11 +04:00
|
|
|
if (NeedsInnerScope) ExitScope();
|
2007-08-22 09:28:50 +04:00
|
|
|
|
2007-10-09 21:41:39 +04:00
|
|
|
if (Tok.isNot(tok::kw_while)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
ExitScope();
|
|
|
|
Diag(Tok, diag::err_expected_while);
|
|
|
|
Diag(DoLoc, diag::err_matching, "do");
|
|
|
|
SkipUntil(tok::semi);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
SourceLocation WhileLoc = ConsumeToken();
|
|
|
|
|
2007-10-09 21:41:39 +04:00
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
ExitScope();
|
|
|
|
Diag(Tok, diag::err_expected_lparen_after, "do/while");
|
|
|
|
SkipUntil(tok::semi);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the condition.
|
|
|
|
ExprResult Cond = ParseSimpleParenExpression();
|
|
|
|
|
|
|
|
ExitScope();
|
|
|
|
|
|
|
|
if (Cond.isInvalid || Body.isInvalid) return true;
|
|
|
|
|
2007-09-16 18:56:35 +04:00
|
|
|
return Actions.ActOnDoStmt(DoLoc, Body.Val, WhileLoc, Cond.Val);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseForStatement
|
|
|
|
/// for-statement: [C99 6.8.5.3]
|
|
|
|
/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
|
|
|
|
/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
|
2008-09-10 00:38:47 +04:00
|
|
|
/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
|
|
|
|
/// [C++] statement
|
2008-01-03 20:55:25 +03:00
|
|
|
/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
|
|
|
|
/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
|
2008-09-10 00:38:47 +04:00
|
|
|
///
|
|
|
|
/// [C++] for-init-statement:
|
|
|
|
/// [C++] expression-statement
|
|
|
|
/// [C++] simple-declaration
|
|
|
|
///
|
2007-07-11 21:01:13 +04:00
|
|
|
Parser::StmtResult Parser::ParseForStatement() {
|
2007-10-09 21:41:39 +04:00
|
|
|
assert(Tok.is(tok::kw_for) && "Not a for stmt!");
|
2007-07-11 21:01:13 +04:00
|
|
|
SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
|
|
|
|
|
2007-10-09 21:41:39 +04:00
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
Diag(Tok, diag::err_expected_lparen_after, "for");
|
|
|
|
SkipUntil(tok::semi);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-09-11 07:06:46 +04:00
|
|
|
bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
|
|
|
|
|
2007-08-27 03:08:06 +04:00
|
|
|
// C99 6.8.5p5 - In C99, the for statement is a block. This is not
|
|
|
|
// the case for C90. Start the loop scope.
|
2008-09-11 07:06:46 +04:00
|
|
|
//
|
|
|
|
// C++ 6.4p3:
|
|
|
|
// A name introduced by a declaration in a condition is in scope from its
|
|
|
|
// point of declaration until the end of the substatements controlled by the
|
|
|
|
// condition.
|
2008-09-12 03:08:39 +04:00
|
|
|
// C++ 3.3.2p4:
|
|
|
|
// Names declared in the for-init-statement, and in the condition of if,
|
|
|
|
// while, for, and switch statements are local to the if, while, for, or
|
|
|
|
// switch statement (including the controlled statement).
|
2008-09-11 07:06:46 +04:00
|
|
|
// C++ 6.5.3p1:
|
|
|
|
// Names declared in the for-init-statement are in the same declarative-region
|
|
|
|
// as those declared in the condition.
|
|
|
|
//
|
|
|
|
if (C99orCXX)
|
2008-09-10 00:38:47 +04:00
|
|
|
EnterScope(Scope::BreakScope | Scope::ContinueScope |
|
|
|
|
Scope::DeclScope | Scope::ControlScope);
|
2007-08-27 03:08:06 +04:00
|
|
|
else
|
|
|
|
EnterScope(Scope::BreakScope | Scope::ContinueScope);
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
SourceLocation LParenLoc = ConsumeParen();
|
|
|
|
ExprResult Value;
|
|
|
|
|
|
|
|
StmtTy *FirstPart = 0;
|
|
|
|
ExprTy *SecondPart = 0;
|
|
|
|
StmtTy *ThirdPart = 0;
|
2008-01-05 02:23:46 +03:00
|
|
|
bool ForEach = false;
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
// Parse the first part of the for specifier.
|
2007-10-09 21:41:39 +04:00
|
|
|
if (Tok.is(tok::semi)) { // for (;
|
2007-07-11 21:01:13 +04:00
|
|
|
// no first part, eat the ';'.
|
|
|
|
ConsumeToken();
|
2008-10-05 19:50:46 +04:00
|
|
|
} else if (isSimpleDeclaration()) { // for (int X = 4;
|
2007-07-11 21:01:13 +04:00
|
|
|
// Parse declaration, which eats the ';'.
|
2008-09-11 07:06:46 +04:00
|
|
|
if (!C99orCXX) // Use of C99-style for loops in C90 mode?
|
2007-07-11 21:01:13 +04:00
|
|
|
Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
|
2008-03-13 09:29:04 +03:00
|
|
|
|
|
|
|
SourceLocation DeclStart = Tok.getLocation();
|
2008-09-10 00:38:47 +04:00
|
|
|
DeclTy *aBlockVarDecl = ParseSimpleDeclaration(Declarator::ForContext);
|
2008-03-13 09:29:04 +03:00
|
|
|
// FIXME: Pass in the right location for the end of the declstmt.
|
|
|
|
StmtResult stmtResult = Actions.ActOnDeclStmt(aBlockVarDecl, DeclStart,
|
2008-03-13 09:29:54 +03:00
|
|
|
DeclStart);
|
2007-07-11 21:01:13 +04:00
|
|
|
FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
|
2008-01-05 02:23:46 +03:00
|
|
|
if ((ForEach = isTokIdentifier_in())) {
|
2008-01-03 20:55:25 +03:00
|
|
|
ConsumeToken(); // consume 'in'
|
|
|
|
Value = ParseExpression();
|
|
|
|
if (!Value.isInvalid)
|
|
|
|
SecondPart = Value.Val;
|
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
} else {
|
|
|
|
Value = ParseExpression();
|
|
|
|
|
|
|
|
// Turn the expression into a stmt.
|
|
|
|
if (!Value.isInvalid) {
|
2007-09-16 18:56:35 +04:00
|
|
|
StmtResult R = Actions.ActOnExprStmt(Value.Val);
|
2007-07-11 21:01:13 +04:00
|
|
|
if (!R.isInvalid)
|
|
|
|
FirstPart = R.Val;
|
|
|
|
}
|
|
|
|
|
2007-10-09 21:41:39 +04:00
|
|
|
if (Tok.is(tok::semi)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
ConsumeToken();
|
2008-01-03 20:55:25 +03:00
|
|
|
}
|
2008-01-05 02:23:46 +03:00
|
|
|
else if ((ForEach = isTokIdentifier_in())) {
|
2008-01-03 20:55:25 +03:00
|
|
|
ConsumeToken(); // consume 'in'
|
|
|
|
Value = ParseExpression();
|
|
|
|
if (!Value.isInvalid)
|
|
|
|
SecondPart = Value.Val;
|
|
|
|
}
|
|
|
|
else {
|
2007-07-11 21:01:13 +04:00
|
|
|
if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
|
|
|
|
SkipUntil(tok::semi);
|
|
|
|
}
|
|
|
|
}
|
2008-01-05 02:23:46 +03:00
|
|
|
if (!ForEach) {
|
2008-01-03 20:55:25 +03:00
|
|
|
// Parse the second part of the for specifier.
|
|
|
|
if (Tok.is(tok::semi)) { // for (...;;
|
|
|
|
// no second part.
|
|
|
|
Value = ExprResult();
|
|
|
|
} else {
|
2008-09-10 00:38:47 +04:00
|
|
|
Value = getLang().CPlusPlus ? ParseCXXCondition()
|
|
|
|
: ParseExpression();
|
2008-01-03 20:55:25 +03:00
|
|
|
if (!Value.isInvalid)
|
|
|
|
SecondPart = Value.Val;
|
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2008-01-03 20:55:25 +03:00
|
|
|
if (Tok.is(tok::semi)) {
|
|
|
|
ConsumeToken();
|
|
|
|
} else {
|
|
|
|
if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
|
|
|
|
SkipUntil(tok::semi);
|
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2008-01-03 20:55:25 +03:00
|
|
|
// Parse the third part of the for specifier.
|
|
|
|
if (Tok.is(tok::r_paren)) { // for (...;...;)
|
|
|
|
// no third part.
|
|
|
|
Value = ExprResult();
|
|
|
|
} else {
|
|
|
|
Value = ParseExpression();
|
|
|
|
if (!Value.isInvalid) {
|
|
|
|
// Turn the expression into a stmt.
|
|
|
|
StmtResult R = Actions.ActOnExprStmt(Value.Val);
|
|
|
|
if (!R.isInvalid)
|
|
|
|
ThirdPart = R.Val;
|
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Match the ')'.
|
|
|
|
SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
|
|
|
|
|
2007-08-22 09:28:50 +04:00
|
|
|
// C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
|
2007-08-22 09:33:11 +04:00
|
|
|
// there is no compound stmt. C90 does not have this clause. We only do this
|
|
|
|
// if the body isn't a compound statement to avoid push/pop in common cases.
|
2008-09-11 07:06:46 +04:00
|
|
|
//
|
|
|
|
// C++ 6.5p2:
|
|
|
|
// The substatement in an iteration-statement implicitly defines a local scope
|
|
|
|
// which is entered and exited each time through the loop.
|
|
|
|
//
|
|
|
|
// See comments in ParseIfStatement for why we create a scope for
|
|
|
|
// for-init-statement/condition and a new scope for substatement in C++.
|
|
|
|
//
|
|
|
|
bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
|
2007-08-26 10:24:45 +04:00
|
|
|
if (NeedsInnerScope) EnterScope(Scope::DeclScope);
|
2007-08-22 09:28:50 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Read the body statement.
|
|
|
|
StmtResult Body = ParseStatement();
|
|
|
|
|
2007-08-22 09:28:50 +04:00
|
|
|
// Pop the body scope if needed.
|
2007-08-22 09:33:11 +04:00
|
|
|
if (NeedsInnerScope) ExitScope();
|
2007-08-22 09:28:50 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// Leave the for-scope.
|
|
|
|
ExitScope();
|
|
|
|
|
|
|
|
if (Body.isInvalid)
|
|
|
|
return Body;
|
|
|
|
|
2008-01-05 02:23:46 +03:00
|
|
|
if (!ForEach)
|
|
|
|
return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart,
|
|
|
|
SecondPart, ThirdPart, RParenLoc, Body.Val);
|
|
|
|
else
|
2008-01-07 22:49:32 +03:00
|
|
|
return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, FirstPart,
|
2008-01-05 02:23:46 +03:00
|
|
|
SecondPart, RParenLoc, Body.Val);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseGotoStatement
|
|
|
|
/// jump-statement:
|
|
|
|
/// 'goto' identifier ';'
|
|
|
|
/// [GNU] 'goto' '*' expression ';'
|
|
|
|
///
|
|
|
|
/// Note: this lets the caller parse the end ';'.
|
|
|
|
///
|
|
|
|
Parser::StmtResult Parser::ParseGotoStatement() {
|
2007-10-09 21:41:39 +04:00
|
|
|
assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
|
2007-07-11 21:01:13 +04:00
|
|
|
SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
|
|
|
|
|
|
|
|
StmtResult Res;
|
2007-10-09 21:41:39 +04:00
|
|
|
if (Tok.is(tok::identifier)) {
|
2007-09-16 18:56:35 +04:00
|
|
|
Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
|
2007-07-11 21:01:13 +04:00
|
|
|
Tok.getIdentifierInfo());
|
|
|
|
ConsumeToken();
|
2007-10-09 21:41:39 +04:00
|
|
|
} else if (Tok.is(tok::star) && !getLang().NoExtensions) {
|
2007-07-11 21:01:13 +04:00
|
|
|
// GNU indirect goto extension.
|
|
|
|
Diag(Tok, diag::ext_gnu_indirect_goto);
|
|
|
|
SourceLocation StarLoc = ConsumeToken();
|
|
|
|
ExprResult R = ParseExpression();
|
|
|
|
if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
|
|
|
|
SkipUntil(tok::semi, false, true);
|
|
|
|
return true;
|
|
|
|
}
|
2007-09-16 18:56:35 +04:00
|
|
|
Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.Val);
|
2007-07-22 08:13:33 +04:00
|
|
|
} else {
|
|
|
|
Diag(Tok, diag::err_expected_ident);
|
|
|
|
return true;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2007-07-22 08:13:33 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseContinueStatement
|
|
|
|
/// jump-statement:
|
|
|
|
/// 'continue' ';'
|
|
|
|
///
|
|
|
|
/// Note: this lets the caller parse the end ';'.
|
|
|
|
///
|
|
|
|
Parser::StmtResult Parser::ParseContinueStatement() {
|
|
|
|
SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
|
2007-09-16 18:56:35 +04:00
|
|
|
return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseBreakStatement
|
|
|
|
/// jump-statement:
|
|
|
|
/// 'break' ';'
|
|
|
|
///
|
|
|
|
/// Note: this lets the caller parse the end ';'.
|
|
|
|
///
|
|
|
|
Parser::StmtResult Parser::ParseBreakStatement() {
|
|
|
|
SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
|
2007-09-16 18:56:35 +04:00
|
|
|
return Actions.ActOnBreakStmt(BreakLoc, CurScope);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseReturnStatement
|
|
|
|
/// jump-statement:
|
|
|
|
/// 'return' expression[opt] ';'
|
|
|
|
Parser::StmtResult Parser::ParseReturnStatement() {
|
2007-10-09 21:41:39 +04:00
|
|
|
assert(Tok.is(tok::kw_return) && "Not a return stmt!");
|
2007-07-11 21:01:13 +04:00
|
|
|
SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
|
|
|
|
|
|
|
|
ExprResult R(0);
|
2007-10-09 21:41:39 +04:00
|
|
|
if (Tok.isNot(tok::semi)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
R = ParseExpression();
|
|
|
|
if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
|
|
|
|
SkipUntil(tok::semi, false, true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2007-09-16 18:56:35 +04:00
|
|
|
return Actions.ActOnReturnStmt(ReturnLoc, R.Val);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2008-02-12 02:15:56 +03:00
|
|
|
/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
|
|
|
|
/// routine is called to skip/ignore tokens that comprise the MS asm statement.
|
2008-02-07 06:50:06 +03:00
|
|
|
Parser::StmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
|
2008-02-08 02:24:32 +03:00
|
|
|
if (Tok.is(tok::l_brace)) {
|
|
|
|
unsigned short savedBraceCount = BraceCount;
|
|
|
|
do {
|
|
|
|
ConsumeAnyToken();
|
|
|
|
} while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
|
|
|
|
} else {
|
|
|
|
// From the MS website: If used without braces, the __asm keyword means
|
|
|
|
// that the rest of the line is an assembly-language statement.
|
|
|
|
SourceManager &SrcMgr = PP.getSourceManager();
|
2008-02-08 06:36:19 +03:00
|
|
|
SourceLocation TokLoc = Tok.getLocation();
|
2008-02-08 21:01:27 +03:00
|
|
|
unsigned lineNo = SrcMgr.getLogicalLineNumber(TokLoc);
|
|
|
|
do {
|
|
|
|
ConsumeAnyToken();
|
|
|
|
TokLoc = Tok.getLocation();
|
|
|
|
} while ((SrcMgr.getLogicalLineNumber(TokLoc) == lineNo) &&
|
|
|
|
Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
|
|
|
|
Tok.isNot(tok::eof));
|
2008-02-08 02:24:32 +03:00
|
|
|
}
|
2008-04-08 01:06:54 +04:00
|
|
|
return Actions.ActOnNullStmt(Tok.getLocation());
|
2008-02-07 06:50:06 +03:00
|
|
|
}
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
/// ParseAsmStatement - Parse a GNU extended asm statement.
|
2008-02-12 02:15:56 +03:00
|
|
|
/// asm-statement:
|
|
|
|
/// gnu-asm-statement
|
|
|
|
/// ms-asm-statement
|
|
|
|
///
|
|
|
|
/// [GNU] gnu-asm-statement:
|
2007-07-11 21:01:13 +04:00
|
|
|
/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
|
|
|
|
///
|
|
|
|
/// [GNU] asm-argument:
|
|
|
|
/// asm-string-literal
|
|
|
|
/// asm-string-literal ':' asm-operands[opt]
|
|
|
|
/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
|
|
|
|
/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
|
|
|
|
/// ':' asm-clobbers
|
|
|
|
///
|
|
|
|
/// [GNU] asm-clobbers:
|
|
|
|
/// asm-string-literal
|
|
|
|
/// asm-clobbers ',' asm-string-literal
|
|
|
|
///
|
2008-02-12 02:15:56 +03:00
|
|
|
/// [MS] ms-asm-statement:
|
|
|
|
/// '__asm' assembly-instruction ';'[opt]
|
|
|
|
/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
|
|
|
|
///
|
|
|
|
/// [MS] assembly-instruction-list:
|
|
|
|
/// assembly-instruction ';'[opt]
|
|
|
|
/// assembly-instruction-list ';' assembly-instruction ';'[opt]
|
|
|
|
///
|
2008-02-07 06:50:06 +03:00
|
|
|
Parser::StmtResult Parser::ParseAsmStatement(bool &msAsm) {
|
2007-10-09 21:41:39 +04:00
|
|
|
assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
|
2007-10-29 07:04:16 +03:00
|
|
|
SourceLocation AsmLoc = ConsumeToken();
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2008-02-12 02:15:56 +03:00
|
|
|
if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
|
2008-02-07 06:50:06 +03:00
|
|
|
msAsm = true;
|
|
|
|
return FuzzyParseMicrosoftAsmStatement();
|
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
DeclSpec DS;
|
|
|
|
SourceLocation Loc = Tok.getLocation();
|
|
|
|
ParseTypeQualifierListOpt(DS);
|
|
|
|
|
|
|
|
// GNU asms accept, but warn, about type-qualifiers other than volatile.
|
|
|
|
if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
|
|
|
|
Diag(Loc, diag::w_asm_qualifier_ignored, "const");
|
|
|
|
if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
|
|
|
|
Diag(Loc, diag::w_asm_qualifier_ignored, "restrict");
|
|
|
|
|
|
|
|
// Remember if this was a volatile asm.
|
2007-11-24 02:12:25 +03:00
|
|
|
bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
|
2008-02-06 02:03:50 +03:00
|
|
|
bool isSimple = false;
|
2007-10-09 21:41:39 +04:00
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
Diag(Tok, diag::err_expected_lparen_after, "asm");
|
|
|
|
SkipUntil(tok::r_paren);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Loc = ConsumeParen();
|
|
|
|
|
2007-11-20 22:21:03 +03:00
|
|
|
ExprResult AsmString = ParseAsmStringLiteral();
|
|
|
|
if (AsmString.isInvalid)
|
|
|
|
return true;
|
2007-11-22 04:36:19 +03:00
|
|
|
|
|
|
|
llvm::SmallVector<std::string, 4> Names;
|
|
|
|
llvm::SmallVector<ExprTy*, 4> Constraints;
|
|
|
|
llvm::SmallVector<ExprTy*, 4> Exprs;
|
2008-02-06 02:03:50 +03:00
|
|
|
llvm::SmallVector<ExprTy*, 4> Clobbers;
|
|
|
|
|
|
|
|
unsigned NumInputs = 0, NumOutputs = 0;
|
2007-11-22 04:36:19 +03:00
|
|
|
|
2008-02-06 02:03:50 +03:00
|
|
|
SourceLocation RParenLoc;
|
|
|
|
if (Tok.is(tok::r_paren)) {
|
|
|
|
// We have a simple asm expression
|
|
|
|
isSimple = true;
|
|
|
|
|
|
|
|
RParenLoc = ConsumeParen();
|
|
|
|
} else {
|
|
|
|
// Parse Outputs, if present.
|
2008-02-09 22:57:29 +03:00
|
|
|
if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
|
|
|
|
return true;
|
2007-11-22 04:36:19 +03:00
|
|
|
|
2008-02-06 02:03:50 +03:00
|
|
|
NumOutputs = Names.size();
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2008-02-06 02:03:50 +03:00
|
|
|
// Parse Inputs, if present.
|
2008-02-09 22:57:29 +03:00
|
|
|
if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
|
|
|
|
return true;
|
|
|
|
|
2008-02-06 02:03:50 +03:00
|
|
|
assert(Names.size() == Constraints.size() &&
|
|
|
|
Constraints.size() == Exprs.size()
|
|
|
|
&& "Input operand size mismatch!");
|
2007-11-22 04:36:19 +03:00
|
|
|
|
2008-02-06 02:03:50 +03:00
|
|
|
NumInputs = Names.size() - NumOutputs;
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2008-02-06 02:03:50 +03:00
|
|
|
// Parse the clobbers, if present.
|
|
|
|
if (Tok.is(tok::colon)) {
|
|
|
|
ConsumeToken();
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2008-02-06 02:03:50 +03:00
|
|
|
// Parse the asm-string list for clobbers.
|
|
|
|
while (1) {
|
|
|
|
ExprResult Clobber = ParseAsmStringLiteral();
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2008-02-06 02:03:50 +03:00
|
|
|
if (Clobber.isInvalid)
|
|
|
|
break;
|
2007-11-22 04:36:19 +03:00
|
|
|
|
2008-02-06 02:03:50 +03:00
|
|
|
Clobbers.push_back(Clobber.Val);
|
2007-11-22 04:36:19 +03:00
|
|
|
|
2008-02-06 02:03:50 +03:00
|
|
|
if (Tok.isNot(tok::comma)) break;
|
|
|
|
ConsumeToken();
|
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2008-02-06 02:03:50 +03:00
|
|
|
RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
|
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2008-02-06 02:03:50 +03:00
|
|
|
return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
|
|
|
|
NumOutputs, NumInputs,
|
2007-11-22 04:36:19 +03:00
|
|
|
&Names[0], &Constraints[0], &Exprs[0],
|
|
|
|
AsmString.Val,
|
|
|
|
Clobbers.size(), &Clobbers[0],
|
|
|
|
RParenLoc);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseAsmOperands - Parse the asm-operands production as used by
|
|
|
|
/// asm-statement. We also parse a leading ':' token. If the leading colon is
|
|
|
|
/// not present, we do not parse anything.
|
|
|
|
///
|
|
|
|
/// [GNU] asm-operands:
|
|
|
|
/// asm-operand
|
|
|
|
/// asm-operands ',' asm-operand
|
|
|
|
///
|
|
|
|
/// [GNU] asm-operand:
|
|
|
|
/// asm-string-literal '(' expression ')'
|
|
|
|
/// '[' identifier ']' asm-string-literal '(' expression ')'
|
|
|
|
///
|
2008-02-09 22:57:29 +03:00
|
|
|
bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
|
2007-11-22 04:36:19 +03:00
|
|
|
llvm::SmallVectorImpl<ExprTy*> &Constraints,
|
|
|
|
llvm::SmallVectorImpl<ExprTy*> &Exprs) {
|
2007-07-11 21:01:13 +04:00
|
|
|
// Only do anything if this operand is present.
|
2008-02-09 22:57:29 +03:00
|
|
|
if (Tok.isNot(tok::colon)) return false;
|
2007-07-11 21:01:13 +04:00
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
// 'asm-operands' isn't present?
|
2007-10-09 21:41:39 +04:00
|
|
|
if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
|
2008-02-09 22:57:29 +03:00
|
|
|
return false;
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2007-11-22 04:36:19 +03:00
|
|
|
while (1) {
|
2007-07-11 21:01:13 +04:00
|
|
|
// Read the [id] if present.
|
2007-10-09 21:41:39 +04:00
|
|
|
if (Tok.is(tok::l_square)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
SourceLocation Loc = ConsumeBracket();
|
|
|
|
|
2007-10-09 21:41:39 +04:00
|
|
|
if (Tok.isNot(tok::identifier)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
Diag(Tok, diag::err_expected_ident);
|
|
|
|
SkipUntil(tok::r_paren);
|
2008-02-09 22:57:29 +03:00
|
|
|
return true;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2007-10-29 07:06:22 +03:00
|
|
|
|
2007-11-22 04:36:19 +03:00
|
|
|
IdentifierInfo *II = Tok.getIdentifierInfo();
|
2007-10-29 07:06:22 +03:00
|
|
|
ConsumeToken();
|
2007-11-22 04:36:19 +03:00
|
|
|
|
|
|
|
Names.push_back(std::string(II->getName(), II->getLength()));
|
2007-07-11 21:01:13 +04:00
|
|
|
MatchRHSPunctuation(tok::r_square, Loc);
|
2007-11-22 04:36:19 +03:00
|
|
|
} else
|
|
|
|
Names.push_back(std::string());
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2007-11-22 04:36:19 +03:00
|
|
|
ExprResult Constraint = ParseAsmStringLiteral();
|
|
|
|
if (Constraint.isInvalid) {
|
|
|
|
SkipUntil(tok::r_paren);
|
2008-02-09 22:57:29 +03:00
|
|
|
return true;
|
2007-11-22 04:36:19 +03:00
|
|
|
}
|
|
|
|
Constraints.push_back(Constraint.Val);
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2007-10-09 21:41:39 +04:00
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
Diag(Tok, diag::err_expected_lparen_after, "asm operand");
|
|
|
|
SkipUntil(tok::r_paren);
|
2008-02-09 22:57:29 +03:00
|
|
|
return true;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read the parenthesized expression.
|
|
|
|
ExprResult Res = ParseSimpleParenExpression();
|
|
|
|
if (Res.isInvalid) {
|
|
|
|
SkipUntil(tok::r_paren);
|
2008-02-09 22:57:29 +03:00
|
|
|
return true;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2007-11-22 04:36:19 +03:00
|
|
|
Exprs.push_back(Res.Val);
|
2007-07-11 21:01:13 +04:00
|
|
|
// Eat the comma and continue parsing if it exists.
|
2008-02-09 22:57:29 +03:00
|
|
|
if (Tok.isNot(tok::comma)) return false;
|
2007-07-11 21:01:13 +04:00
|
|
|
ConsumeToken();
|
|
|
|
}
|
2008-02-09 22:57:29 +03:00
|
|
|
|
|
|
|
return true;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2007-11-08 22:01:26 +03:00
|
|
|
|
|
|
|
Parser::DeclTy *Parser::ParseFunctionStatementBody(DeclTy *Decl,
|
|
|
|
SourceLocation L, SourceLocation R) {
|
|
|
|
// Do not enter a scope for the brace, as the arguments are in the same scope
|
|
|
|
// (the function body) as the body itself. Instead, just read the statement
|
|
|
|
// list and put it into a CompoundStmt for safe keeping.
|
|
|
|
StmtResult FnBody = ParseCompoundStatementBody();
|
|
|
|
|
|
|
|
// If the function body could not be parsed, make a bogus compoundstmt.
|
|
|
|
if (FnBody.isInvalid)
|
|
|
|
FnBody = Actions.ActOnCompoundStmt(L, R, 0, 0, false);
|
|
|
|
|
|
|
|
// Leave the function body scope.
|
|
|
|
ExitScope();
|
|
|
|
|
2007-11-12 02:20:51 +03:00
|
|
|
return Actions.ActOnFinishFunctionBody(Decl, FnBody.Val);
|
2007-12-01 11:06:07 +03:00
|
|
|
}
|