2006-11-05 15:46:30 +08:00
|
|
|
//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
|
2006-08-09 13:47:47 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-08-09 13:47:47 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the Statement and Block portions of the Parser
|
|
|
|
// interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Parse/Parser.h"
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2008-02-08 07:24:32 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2006-11-12 07:03:42 +08:00
|
|
|
#include "clang/Parse/DeclSpec.h"
|
2006-11-06 07:47:55 +08:00
|
|
|
#include "clang/Parse/Scope.h"
|
2006-08-09 13:47:47 +08:00
|
|
|
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-08 02:58:01 +08:00
|
|
|
/// [C++] declaration-statement
|
2007-10-05 04:19:06 +08:00
|
|
|
/// [OBC] objc-throw-statement
|
|
|
|
/// [OBC] objc-try-catch-statement
|
2008-01-30 02:21:32 +08:00
|
|
|
/// [OBC] objc-synchronized-statement
|
2006-08-15 14:03:28 +08:00
|
|
|
/// [GNU] asm-statement
|
2006-08-09 13:47:47 +08:00
|
|
|
/// [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
|
|
|
|
///
|
2006-08-10 12:59:57 +08:00
|
|
|
/// expression-statement:
|
|
|
|
/// expression[opt] ';'
|
|
|
|
///
|
2006-08-09 13:47:47 +08:00
|
|
|
/// jump-statement:
|
|
|
|
/// 'goto' identifier ';'
|
|
|
|
/// 'continue' ';'
|
|
|
|
/// 'break' ';'
|
|
|
|
/// 'return' expression[opt] ';'
|
2006-08-10 13:45:44 +08:00
|
|
|
/// [GNU] 'goto' '*' expression ';'
|
2006-08-09 13:47:47 +08:00
|
|
|
///
|
2007-10-05 04:19:06 +08:00
|
|
|
/// [OBC] objc-throw-statement:
|
|
|
|
/// [OBC] '@' 'throw' expression ';'
|
|
|
|
/// [OBC] '@' 'throw' ';'
|
2006-08-09 13:47:47 +08:00
|
|
|
///
|
2006-10-16 13:52:41 +08:00
|
|
|
Parser::StmtResult Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
|
2006-08-10 13:45:44 +08:00
|
|
|
const char *SemiError = 0;
|
2006-10-16 13:52:41 +08:00
|
|
|
Parser::StmtResult Res;
|
2006-08-10 13:45:44 +08:00
|
|
|
|
|
|
|
// 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-20 03:14:32 +08: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 05:27:35 +08:00
|
|
|
return ParseObjCAtStatement(AtLoc);
|
2007-09-20 03:14:32 +08:00
|
|
|
}
|
|
|
|
|
2008-07-13 05:04:42 +08:00
|
|
|
case tok::identifier:
|
|
|
|
if (NextToken().is(tok::colon)) { // C99 6.8.1: labeled-statement
|
|
|
|
// identifier ':' statement
|
|
|
|
return ParseLabeledStatement();
|
|
|
|
}
|
|
|
|
// PASS THROUGH.
|
|
|
|
|
2006-08-09 13:47:47 +08:00
|
|
|
default:
|
2008-09-08 02:58:01 +08:00
|
|
|
if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationSpecifier()) {
|
2008-03-13 14:29:04 +08: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 14:29:54 +08:00
|
|
|
return Actions.ActOnDeclStmt(Res, DeclStart, DeclStart);
|
2007-10-10 01:41:39 +08:00
|
|
|
} else if (Tok.is(tok::r_brace)) {
|
2006-08-11 02:26:31 +08:00
|
|
|
Diag(Tok, diag::err_expected_statement);
|
2006-10-16 13:52:41 +08:00
|
|
|
return true;
|
2006-08-11 02:26:31 +08:00
|
|
|
} else {
|
|
|
|
// expression[opt] ';'
|
2007-10-05 04:19:06 +08:00
|
|
|
ExprResult Res = ParseExpression();
|
2006-08-11 14:41:18 +08: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);
|
2006-10-16 13:52:41 +08:00
|
|
|
return true;
|
2006-08-11 14:41:18 +08:00
|
|
|
}
|
2007-06-06 13:26:32 +08:00
|
|
|
// Otherwise, eat the semicolon.
|
|
|
|
ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
|
2007-09-16 22:56:35 +08:00
|
|
|
return Actions.ActOnExprStmt(Res.Val);
|
2006-08-11 02:26:31 +08:00
|
|
|
}
|
2006-08-09 13:47:47 +08:00
|
|
|
|
2006-08-10 13:59:48 +08:00
|
|
|
case tok::kw_case: // C99 6.8.1: labeled-statement
|
2006-10-16 13:52:41 +08:00
|
|
|
return ParseCaseStatement();
|
2006-08-10 13:59:48 +08:00
|
|
|
case tok::kw_default: // C99 6.8.1: labeled-statement
|
2006-10-16 13:52:41 +08:00
|
|
|
return ParseDefaultStatement();
|
2006-08-10 12:59:57 +08:00
|
|
|
|
|
|
|
case tok::l_brace: // C99 6.8.2: compound-statement
|
2006-10-16 13:52:41 +08:00
|
|
|
return ParseCompoundStatement();
|
2007-05-28 09:45:28 +08:00
|
|
|
case tok::semi: // C99 6.8.3p3: expression[opt] ';'
|
2007-09-16 22:56:35 +08:00
|
|
|
return Actions.ActOnNullStmt(ConsumeToken());
|
2006-08-10 13:45:44 +08:00
|
|
|
|
2006-08-10 12:59:57 +08:00
|
|
|
case tok::kw_if: // C99 6.8.4.1: if-statement
|
2006-10-16 13:52:41 +08:00
|
|
|
return ParseIfStatement();
|
2006-08-10 12:59:57 +08:00
|
|
|
case tok::kw_switch: // C99 6.8.4.2: switch-statement
|
2006-10-16 13:52:41 +08:00
|
|
|
return ParseSwitchStatement();
|
2006-08-10 13:45:44 +08:00
|
|
|
|
2006-08-10 12:59:57 +08:00
|
|
|
case tok::kw_while: // C99 6.8.5.1: while-statement
|
2006-10-16 13:52:41 +08:00
|
|
|
return ParseWhileStatement();
|
2006-08-10 12:59:57 +08:00
|
|
|
case tok::kw_do: // C99 6.8.5.2: do-statement
|
2006-10-16 13:52:41 +08:00
|
|
|
Res = ParseDoStatement();
|
2006-08-10 13:45:44 +08:00
|
|
|
SemiError = "do/while loop";
|
2006-08-10 12:59:57 +08:00
|
|
|
break;
|
|
|
|
case tok::kw_for: // C99 6.8.5.3: for-statement
|
2006-10-16 13:52:41 +08:00
|
|
|
return ParseForStatement();
|
2006-08-10 13:45:44 +08:00
|
|
|
|
|
|
|
case tok::kw_goto: // C99 6.8.6.1: goto-statement
|
2006-10-16 13:52:41 +08:00
|
|
|
Res = ParseGotoStatement();
|
2006-08-10 13:45:44 +08:00
|
|
|
SemiError = "goto statement";
|
|
|
|
break;
|
|
|
|
case tok::kw_continue: // C99 6.8.6.2: continue-statement
|
2006-11-06 07:47:55 +08:00
|
|
|
Res = ParseContinueStatement();
|
2006-08-10 13:45:44 +08:00
|
|
|
SemiError = "continue statement";
|
2006-08-10 12:59:57 +08:00
|
|
|
break;
|
2006-08-10 13:45:44 +08:00
|
|
|
case tok::kw_break: // C99 6.8.6.3: break-statement
|
2006-11-06 07:47:55 +08:00
|
|
|
Res = ParseBreakStatement();
|
2006-08-10 13:45:44 +08:00
|
|
|
SemiError = "break statement";
|
|
|
|
break;
|
|
|
|
case tok::kw_return: // C99 6.8.6.4: return-statement
|
2006-10-16 13:52:41 +08:00
|
|
|
Res = ParseReturnStatement();
|
2006-08-10 13:45:44 +08:00
|
|
|
SemiError = "return statement";
|
|
|
|
break;
|
2006-08-15 14:03:28 +08:00
|
|
|
|
|
|
|
case tok::kw_asm:
|
2008-02-07 11:50:06 +08:00
|
|
|
bool msAsm = false;
|
|
|
|
Res = ParseAsmStatement(msAsm);
|
|
|
|
if (msAsm) return Res;
|
2006-08-15 14:03:28 +08:00
|
|
|
SemiError = "asm statement";
|
|
|
|
break;
|
2006-08-09 13:47:47 +08:00
|
|
|
}
|
2006-08-10 13:45:44 +08:00
|
|
|
|
|
|
|
// If we reached this code, the statement must end in a semicolon.
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.is(tok::semi)) {
|
2006-08-10 13:45:44 +08:00
|
|
|
ConsumeToken();
|
|
|
|
} else {
|
|
|
|
Diag(Tok, diag::err_expected_semi_after, SemiError);
|
|
|
|
SkipUntil(tok::semi);
|
|
|
|
}
|
2006-10-16 13:52:41 +08:00
|
|
|
return Res;
|
2006-08-09 13:47:47 +08:00
|
|
|
}
|
|
|
|
|
2008-07-10 06:53:07 +08:00
|
|
|
/// ParseLabeledStatement - We have an identifier and a ':' after it.
|
2006-08-11 02:31:37 +08:00
|
|
|
///
|
|
|
|
/// labeled-statement:
|
|
|
|
/// identifier ':' statement
|
2006-08-15 12:50:22 +08:00
|
|
|
/// [GNU] identifier ':' attributes[opt] statement
|
2006-08-11 02:31:37 +08:00
|
|
|
///
|
2008-07-10 06:53:07 +08:00
|
|
|
Parser::StmtResult Parser::ParseLabeledStatement() {
|
2007-10-10 01:41:39 +08:00
|
|
|
assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
|
2006-11-06 02:39:59 +08:00
|
|
|
"Not an identifier!");
|
2006-08-11 02:31:37 +08:00
|
|
|
|
2007-07-21 00:59:19 +08:00
|
|
|
Token IdentTok = Tok; // Save the whole token.
|
2006-08-11 02:31:37 +08:00
|
|
|
ConsumeToken(); // eat the identifier.
|
2008-07-10 06:53:07 +08:00
|
|
|
|
|
|
|
assert(Tok.is(tok::colon) && "Not a label!");
|
2006-08-11 02:26:31 +08:00
|
|
|
|
2006-08-11 02:31:37 +08:00
|
|
|
// identifier ':' statement
|
2008-07-10 06:53:07 +08:00
|
|
|
SourceLocation ColonLoc = ConsumeToken();
|
2006-08-15 12:50:22 +08:00
|
|
|
|
2008-07-10 06:53:07 +08:00
|
|
|
// Read label attributes, if present.
|
|
|
|
DeclTy *AttrList = 0;
|
|
|
|
if (Tok.is(tok::kw___attribute))
|
|
|
|
// TODO: save these somewhere.
|
|
|
|
AttrList = ParseAttributes();
|
2006-08-15 12:50:22 +08:00
|
|
|
|
2008-07-10 06:53:07 +08: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);
|
|
|
|
}
|
2006-08-11 02:26:31 +08:00
|
|
|
|
2006-08-10 13:59:48 +08:00
|
|
|
/// ParseCaseStatement
|
|
|
|
/// labeled-statement:
|
|
|
|
/// 'case' constant-expression ':' statement
|
2006-08-14 06:09:58 +08:00
|
|
|
/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
|
2006-08-14 05:54:02 +08:00
|
|
|
///
|
2006-08-10 13:59:48 +08:00
|
|
|
/// Note that this does not parse the 'statement' at the end.
|
|
|
|
///
|
2006-10-16 13:52:41 +08:00
|
|
|
Parser::StmtResult Parser::ParseCaseStatement() {
|
2007-10-10 01:41:39 +08:00
|
|
|
assert(Tok.is(tok::kw_case) && "Not a case stmt!");
|
2006-10-16 14:06:51 +08:00
|
|
|
SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
|
2006-08-10 13:59:48 +08:00
|
|
|
|
2006-10-16 13:52:41 +08:00
|
|
|
ExprResult LHS = ParseConstantExpression();
|
|
|
|
if (LHS.isInvalid) {
|
2006-08-14 06:09:58 +08:00
|
|
|
SkipUntil(tok::colon);
|
2006-10-16 13:52:41 +08:00
|
|
|
return true;
|
2006-08-14 06:09:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GNU case range extension.
|
2006-10-16 13:52:41 +08:00
|
|
|
SourceLocation DotDotDotLoc;
|
|
|
|
ExprTy *RHSVal = 0;
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.is(tok::ellipsis)) {
|
2006-08-14 06:09:58 +08:00
|
|
|
Diag(Tok, diag::ext_gnu_case_range);
|
2006-10-16 14:06:51 +08:00
|
|
|
DotDotDotLoc = ConsumeToken();
|
2006-08-14 06:09:58 +08:00
|
|
|
|
|
|
|
ExprResult RHS = ParseConstantExpression();
|
|
|
|
if (RHS.isInvalid) {
|
|
|
|
SkipUntil(tok::colon);
|
2006-10-16 13:52:41 +08:00
|
|
|
return true;
|
2006-08-14 06:09:58 +08:00
|
|
|
}
|
2006-10-16 13:52:41 +08:00
|
|
|
RHSVal = RHS.Val;
|
2006-08-14 06:09:58 +08:00
|
|
|
}
|
2006-08-10 13:59:48 +08:00
|
|
|
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.isNot(tok::colon)) {
|
2006-08-10 13:59:48 +08:00
|
|
|
Diag(Tok, diag::err_expected_colon_after, "'case'");
|
|
|
|
SkipUntil(tok::colon);
|
2006-10-16 13:52:41 +08:00
|
|
|
return true;
|
2006-08-10 13:59:48 +08:00
|
|
|
}
|
2006-10-16 13:52:41 +08:00
|
|
|
|
2006-10-16 14:06:51 +08:00
|
|
|
SourceLocation ColonLoc = ConsumeToken();
|
2006-10-16 13:52:41 +08:00
|
|
|
|
|
|
|
// Diagnose the common error "switch (X) { case 4: }", which is not valid.
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.is(tok::r_brace)) {
|
2006-10-16 13:52:41 +08:00
|
|
|
Diag(Tok, diag::err_label_end_of_compound_statement);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
StmtResult SubStmt = ParseStatement();
|
2007-05-28 13:38:24 +08:00
|
|
|
|
|
|
|
// Broken substmt shouldn't prevent the case from being added to the AST.
|
2006-10-16 13:52:41 +08:00
|
|
|
if (SubStmt.isInvalid)
|
2007-09-16 22:56:35 +08:00
|
|
|
SubStmt = Actions.ActOnNullStmt(ColonLoc);
|
2006-10-16 13:52:41 +08:00
|
|
|
|
2007-09-16 22:56:35 +08:00
|
|
|
return Actions.ActOnCaseStmt(CaseLoc, LHS.Val, DotDotDotLoc, RHSVal, ColonLoc,
|
2006-10-16 13:52:41 +08:00
|
|
|
SubStmt.Val);
|
2006-08-10 13:59:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseDefaultStatement
|
|
|
|
/// labeled-statement:
|
|
|
|
/// 'default' ':' statement
|
|
|
|
/// Note that this does not parse the 'statement' at the end.
|
|
|
|
///
|
2006-10-16 13:52:41 +08:00
|
|
|
Parser::StmtResult Parser::ParseDefaultStatement() {
|
2007-10-10 01:41:39 +08:00
|
|
|
assert(Tok.is(tok::kw_default) && "Not a default stmt!");
|
2006-10-16 14:06:51 +08:00
|
|
|
SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
|
2006-08-10 13:59:48 +08:00
|
|
|
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.isNot(tok::colon)) {
|
2006-08-10 13:59:48 +08:00
|
|
|
Diag(Tok, diag::err_expected_colon_after, "'default'");
|
|
|
|
SkipUntil(tok::colon);
|
2006-10-16 13:52:41 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-10-16 14:06:51 +08:00
|
|
|
SourceLocation ColonLoc = ConsumeToken();
|
2006-10-16 13:52:41 +08:00
|
|
|
|
|
|
|
// Diagnose the common error "switch (X) {... default: }", which is not valid.
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.is(tok::r_brace)) {
|
2006-10-16 13:52:41 +08:00
|
|
|
Diag(Tok, diag::err_label_end_of_compound_statement);
|
|
|
|
return true;
|
2006-08-10 13:59:48 +08:00
|
|
|
}
|
2006-10-16 13:52:41 +08:00
|
|
|
|
|
|
|
StmtResult SubStmt = ParseStatement();
|
|
|
|
if (SubStmt.isInvalid)
|
|
|
|
return true;
|
|
|
|
|
2007-09-16 22:56:35 +08:00
|
|
|
return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc, SubStmt.Val, CurScope);
|
2006-08-10 13:59:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-09 13:47:47 +08: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 09:01:57 +08:00
|
|
|
/// [GNU] '__extension__' declaration
|
2006-08-09 13:47:47 +08: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
|
2006-10-16 13:52:41 +08:00
|
|
|
///
|
2007-09-01 05:49:55 +08:00
|
|
|
Parser::StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
|
2007-10-10 01:41:39 +08:00
|
|
|
assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
|
2006-08-09 13:47:47 +08:00
|
|
|
|
2007-08-26 14:24:45 +08:00
|
|
|
// Enter a scope to hold everything within the compound stmt. Compound
|
|
|
|
// statements can always hold declarations.
|
|
|
|
EnterScope(Scope::DeclScope);
|
2007-01-21 14:52:16 +08:00
|
|
|
|
|
|
|
// Parse the statements in the body.
|
2007-09-01 05:49:55 +08:00
|
|
|
StmtResult Body = ParseCompoundStatementBody(isStmtExpr);
|
2007-01-21 14:52:16 +08:00
|
|
|
|
|
|
|
ExitScope();
|
|
|
|
return Body;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
|
2007-09-16 22:56:35 +08:00
|
|
|
/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
|
2007-01-21 14:52:16 +08:00
|
|
|
/// consume the '}' at the end of the block. It does not manipulate the scope
|
|
|
|
/// stack.
|
2007-09-01 05:49:55 +08:00
|
|
|
Parser::StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
|
2007-01-21 14:52:16 +08:00
|
|
|
SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
|
|
|
|
|
2007-05-28 15:23:54 +08:00
|
|
|
// TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
|
2007-08-27 09:01:57 +08:00
|
|
|
// only allowed at the start of a compound stmt regardless of the language.
|
2007-05-28 15:23:54 +08:00
|
|
|
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::SmallVector<StmtTy*, 32> Stmts;
|
2007-10-10 01:41:39 +08:00
|
|
|
while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
|
2007-08-27 09:01:57 +08:00
|
|
|
StmtResult R;
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.isNot(tok::kw___extension__)) {
|
2007-08-27 09:01:57 +08: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-10 01:41:39 +08:00
|
|
|
while (Tok.is(tok::kw___extension__))
|
2007-08-27 09:01:57 +08:00
|
|
|
ConsumeToken();
|
|
|
|
|
2008-03-13 14:32:11 +08:00
|
|
|
// __extension__ silences extension warnings in the subexpression.
|
|
|
|
bool SavedExtWarn = Diags.getWarnOnExtensions();
|
|
|
|
Diags.setWarnOnExtensions(false);
|
|
|
|
|
2007-08-27 09:01:57 +08:00
|
|
|
// If this is the start of a declaration, parse it as such.
|
|
|
|
if (isDeclarationSpecifier()) {
|
|
|
|
// FIXME: Save the __extension__ on the decl as a node somehow.
|
2008-03-13 14:29:04 +08: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 14:29:54 +08:00
|
|
|
R = Actions.ActOnDeclStmt(Res, DeclStart, DeclStart);
|
2008-03-13 14:32:11 +08:00
|
|
|
|
|
|
|
Diags.setWarnOnExtensions(SavedExtWarn);
|
2007-08-27 09:01:57 +08: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 14:32:11 +08:00
|
|
|
Diags.setWarnOnExtensions(SavedExtWarn);
|
|
|
|
|
2007-08-27 09:01:57 +08:00
|
|
|
if (Res.isInvalid) {
|
|
|
|
SkipUntil(tok::semi);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the __extension__ node to the AST.
|
2007-09-16 11:34:24 +08:00
|
|
|
Res = Actions.ActOnUnaryOp(ExtLoc, tok::kw___extension__, Res.Val);
|
2007-08-27 09:01:57 +08:00
|
|
|
if (Res.isInvalid)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Eat the semicolon at the end of stmt and convert the expr into a stmt.
|
|
|
|
ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
|
2007-09-16 22:56:35 +08:00
|
|
|
R = Actions.ActOnExprStmt(Res.Val);
|
2007-08-27 09:01:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-16 13:52:41 +08:00
|
|
|
if (!R.isInvalid && R.Val)
|
|
|
|
Stmts.push_back(R.Val);
|
|
|
|
}
|
2006-08-09 13:47:47 +08:00
|
|
|
|
|
|
|
// We broke out of the while loop because we found a '}' or EOF.
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.isNot(tok::r_brace)) {
|
2006-08-09 13:47:47 +08:00
|
|
|
Diag(Tok, diag::err_expected_rbrace);
|
2008-02-01 02:29:10 +08:00
|
|
|
return true;
|
2006-10-16 13:52:41 +08:00
|
|
|
}
|
2007-01-21 14:52:16 +08:00
|
|
|
|
2006-10-16 14:12:55 +08:00
|
|
|
SourceLocation RBraceLoc = ConsumeBrace();
|
2007-09-16 22:56:35 +08:00
|
|
|
return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc,
|
2007-09-01 05:49:55 +08:00
|
|
|
&Stmts[0], Stmts.size(), isStmtExpr);
|
2006-08-09 13:47:47 +08:00
|
|
|
}
|
2006-08-10 12:23:57 +08:00
|
|
|
|
|
|
|
/// ParseIfStatement
|
|
|
|
/// if-statement: [C99 6.8.4.1]
|
|
|
|
/// 'if' '(' expression ')' statement
|
|
|
|
/// 'if' '(' expression ')' statement 'else' statement
|
2008-09-10 04:38:47 +08:00
|
|
|
/// [C++] 'if' '(' condition ')' statement
|
|
|
|
/// [C++] 'if' '(' condition ')' statement 'else' statement
|
2006-10-16 13:52:41 +08:00
|
|
|
///
|
|
|
|
Parser::StmtResult Parser::ParseIfStatement() {
|
2007-10-10 01:41:39 +08:00
|
|
|
assert(Tok.is(tok::kw_if) && "Not an if stmt!");
|
2006-10-16 14:06:51 +08:00
|
|
|
SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
|
2006-08-10 12:23:57 +08:00
|
|
|
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
2006-08-10 12:59:57 +08:00
|
|
|
Diag(Tok, diag::err_expected_lparen_after, "if");
|
2006-08-10 12:23:57 +08:00
|
|
|
SkipUntil(tok::semi);
|
2006-10-16 13:52:41 +08:00
|
|
|
return true;
|
2006-08-10 12:23:57 +08:00
|
|
|
}
|
2008-09-10 04:38:47 +08:00
|
|
|
|
2008-09-11 11:06:46 +08:00
|
|
|
bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
|
|
|
|
|
2007-08-27 07:08:06 +08:00
|
|
|
// C99 6.8.4p3 - In C99, the if statement is a block. This is not
|
|
|
|
// the case for C90.
|
2008-09-11 11:06:46 +08: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.
|
|
|
|
//
|
|
|
|
if (C99orCXX)
|
2008-09-10 04:38:47 +08:00
|
|
|
EnterScope(Scope::DeclScope | Scope::ControlScope);
|
2007-08-27 07:08:06 +08:00
|
|
|
|
2006-08-10 12:23:57 +08:00
|
|
|
// Parse the condition.
|
2008-09-10 04:38:47 +08:00
|
|
|
ExprResult CondExp;
|
|
|
|
if (getLang().CPlusPlus) {
|
|
|
|
SourceLocation LParenLoc = ConsumeParen();
|
|
|
|
CondExp = ParseCXXCondition();
|
|
|
|
MatchRHSPunctuation(tok::r_paren, LParenLoc);
|
|
|
|
} else {
|
|
|
|
CondExp = ParseSimpleParenExpression();
|
|
|
|
}
|
|
|
|
|
2006-10-16 13:52:41 +08:00
|
|
|
if (CondExp.isInvalid) {
|
|
|
|
SkipUntil(tok::semi);
|
2008-09-11 11:06:46 +08:00
|
|
|
if (C99orCXX)
|
2007-08-27 07:08:06 +08:00
|
|
|
ExitScope();
|
2006-10-16 13:52:41 +08:00
|
|
|
return true;
|
|
|
|
}
|
2006-08-10 12:23:57 +08:00
|
|
|
|
2007-08-22 13:28:50 +08:00
|
|
|
// C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
|
2007-08-22 13:33:11 +08: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 11:06:46 +08: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 14:24:45 +08:00
|
|
|
if (NeedsInnerScope) EnterScope(Scope::DeclScope);
|
2008-09-10 04:38:47 +08:00
|
|
|
|
2007-10-29 13:08:52 +08:00
|
|
|
// Read the 'then' stmt.
|
|
|
|
SourceLocation ThenStmtLoc = Tok.getLocation();
|
|
|
|
StmtResult ThenStmt = ParseStatement();
|
2007-05-28 13:38:24 +08:00
|
|
|
|
2007-08-22 13:16:28 +08:00
|
|
|
// Pop the 'if' scope if needed.
|
2007-08-22 13:33:11 +08:00
|
|
|
if (NeedsInnerScope) ExitScope();
|
2006-08-10 12:23:57 +08:00
|
|
|
|
|
|
|
// If it has an else, parse it.
|
2006-10-16 13:52:41 +08:00
|
|
|
SourceLocation ElseLoc;
|
2007-10-29 13:08:52 +08:00
|
|
|
SourceLocation ElseStmtLoc;
|
2006-10-16 13:52:41 +08:00
|
|
|
StmtResult ElseStmt(false);
|
2007-10-29 13:08:52 +08:00
|
|
|
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.is(tok::kw_else)) {
|
2006-10-16 14:06:51 +08:00
|
|
|
ElseLoc = ConsumeToken();
|
2007-08-22 13:16:28 +08:00
|
|
|
|
2007-08-22 13:28:50 +08:00
|
|
|
// C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
|
2007-08-22 13:33:11 +08: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 11:06:46 +08: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 14:24:45 +08:00
|
|
|
if (NeedsInnerScope) EnterScope(Scope::DeclScope);
|
2007-08-22 13:16:28 +08:00
|
|
|
|
2007-10-29 13:08:52 +08:00
|
|
|
ElseStmtLoc = Tok.getLocation();
|
2006-10-16 13:52:41 +08:00
|
|
|
ElseStmt = ParseStatement();
|
2007-08-22 13:16:28 +08:00
|
|
|
|
|
|
|
// Pop the 'else' scope if needed.
|
2007-08-22 13:33:11 +08:00
|
|
|
if (NeedsInnerScope) ExitScope();
|
2006-08-10 12:23:57 +08:00
|
|
|
}
|
2006-10-16 13:52:41 +08:00
|
|
|
|
2008-09-11 11:06:46 +08:00
|
|
|
if (C99orCXX)
|
2007-08-27 07:08:06 +08:00
|
|
|
ExitScope();
|
|
|
|
|
2007-10-29 13:08:52 +08: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,
|
2006-10-16 13:52:41 +08:00
|
|
|
ElseLoc, ElseStmt.Val);
|
2006-08-10 12:59:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseSwitchStatement
|
|
|
|
/// switch-statement:
|
|
|
|
/// 'switch' '(' expression ')' statement
|
2008-09-10 04:38:47 +08:00
|
|
|
/// [C++] 'switch' '(' condition ')' statement
|
2006-10-16 13:52:41 +08:00
|
|
|
Parser::StmtResult Parser::ParseSwitchStatement() {
|
2007-10-10 01:41:39 +08:00
|
|
|
assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
|
2006-10-16 14:06:51 +08:00
|
|
|
SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
|
2006-08-10 12:59:57 +08:00
|
|
|
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
2006-08-10 12:59:57 +08:00
|
|
|
Diag(Tok, diag::err_expected_lparen_after, "switch");
|
|
|
|
SkipUntil(tok::semi);
|
2006-10-16 13:52:41 +08:00
|
|
|
return true;
|
2006-08-10 12:59:57 +08:00
|
|
|
}
|
2007-08-27 07:08:06 +08:00
|
|
|
|
2008-09-11 11:06:46 +08:00
|
|
|
bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
|
|
|
|
|
2007-08-27 07:08:06 +08: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 11:06:46 +08: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.
|
|
|
|
//
|
|
|
|
if (C99orCXX)
|
2008-09-10 04:38:47 +08:00
|
|
|
EnterScope(Scope::BreakScope | Scope::DeclScope | Scope::ControlScope);
|
2007-08-27 07:08:06 +08:00
|
|
|
else
|
|
|
|
EnterScope(Scope::BreakScope);
|
2006-11-06 07:47:55 +08:00
|
|
|
|
2006-08-10 12:59:57 +08:00
|
|
|
// Parse the condition.
|
2008-09-10 04:38:47 +08:00
|
|
|
ExprResult Cond;
|
|
|
|
if (getLang().CPlusPlus) {
|
|
|
|
SourceLocation LParenLoc = ConsumeParen();
|
|
|
|
Cond = ParseCXXCondition();
|
|
|
|
MatchRHSPunctuation(tok::r_paren, LParenLoc);
|
|
|
|
} else {
|
|
|
|
Cond = ParseSimpleParenExpression();
|
|
|
|
}
|
2006-08-10 12:59:57 +08:00
|
|
|
|
2007-07-22 15:07:56 +08:00
|
|
|
if (Cond.isInvalid) {
|
|
|
|
ExitScope();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-09-16 22:56:35 +08:00
|
|
|
StmtResult Switch = Actions.ActOnStartOfSwitchStmt(Cond.Val);
|
2007-07-22 15:07:56 +08:00
|
|
|
|
2007-08-22 13:28:50 +08:00
|
|
|
// C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
|
2007-08-22 13:33:11 +08: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 11:06:46 +08: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 14:24:45 +08:00
|
|
|
if (NeedsInnerScope) EnterScope(Scope::DeclScope);
|
2007-08-22 13:28:50 +08:00
|
|
|
|
2006-08-10 12:59:57 +08:00
|
|
|
// Read the body statement.
|
2006-10-16 13:52:41 +08:00
|
|
|
StmtResult Body = ParseStatement();
|
2006-11-06 07:47:55 +08:00
|
|
|
|
2007-08-22 13:28:50 +08:00
|
|
|
// Pop the body scope if needed.
|
2007-08-22 13:33:11 +08:00
|
|
|
if (NeedsInnerScope) ExitScope();
|
2007-08-22 13:28:50 +08:00
|
|
|
|
2007-07-22 15:07:56 +08:00
|
|
|
if (Body.isInvalid) {
|
2007-09-16 22:56:35 +08:00
|
|
|
Body = Actions.ActOnNullStmt(Tok.getLocation());
|
2007-07-22 15:07:56 +08:00
|
|
|
// FIXME: Remove the case statement list from the Switch statement.
|
|
|
|
}
|
2006-10-16 13:52:41 +08:00
|
|
|
|
2007-07-22 15:07:56 +08:00
|
|
|
ExitScope();
|
2006-10-16 13:52:41 +08:00
|
|
|
|
2007-09-16 22:56:35 +08:00
|
|
|
return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.Val, Body.Val);
|
2006-08-10 12:23:57 +08:00
|
|
|
}
|
|
|
|
|
2006-08-10 12:59:57 +08:00
|
|
|
/// ParseWhileStatement
|
|
|
|
/// while-statement: [C99 6.8.5.1]
|
|
|
|
/// 'while' '(' expression ')' statement
|
2008-09-10 04:38:47 +08:00
|
|
|
/// [C++] 'while' '(' condition ')' statement
|
2006-10-16 13:52:41 +08:00
|
|
|
Parser::StmtResult Parser::ParseWhileStatement() {
|
2007-10-10 01:41:39 +08:00
|
|
|
assert(Tok.is(tok::kw_while) && "Not a while stmt!");
|
2006-10-16 13:52:41 +08:00
|
|
|
SourceLocation WhileLoc = Tok.getLocation();
|
2006-08-10 12:59:57 +08:00
|
|
|
ConsumeToken(); // eat the 'while'.
|
|
|
|
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
2006-08-10 12:59:57 +08:00
|
|
|
Diag(Tok, diag::err_expected_lparen_after, "while");
|
|
|
|
SkipUntil(tok::semi);
|
2006-10-16 13:52:41 +08:00
|
|
|
return true;
|
2006-08-10 12:59:57 +08:00
|
|
|
}
|
|
|
|
|
2008-09-11 11:06:46 +08:00
|
|
|
bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
|
|
|
|
|
2007-08-27 07:08:06 +08: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 11:06:46 +08: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.
|
|
|
|
//
|
|
|
|
if (C99orCXX)
|
2008-09-10 04:38:47 +08:00
|
|
|
EnterScope(Scope::BreakScope | Scope::ContinueScope |
|
|
|
|
Scope::DeclScope | Scope::ControlScope);
|
2007-08-27 07:08:06 +08:00
|
|
|
else
|
|
|
|
EnterScope(Scope::BreakScope | Scope::ContinueScope);
|
2006-11-06 07:47:55 +08:00
|
|
|
|
2006-08-10 12:59:57 +08:00
|
|
|
// Parse the condition.
|
2008-09-10 04:38:47 +08:00
|
|
|
ExprResult Cond;
|
|
|
|
if (getLang().CPlusPlus) {
|
|
|
|
SourceLocation LParenLoc = ConsumeParen();
|
|
|
|
Cond = ParseCXXCondition();
|
|
|
|
MatchRHSPunctuation(tok::r_paren, LParenLoc);
|
|
|
|
} else {
|
|
|
|
Cond = ParseSimpleParenExpression();
|
|
|
|
}
|
2006-08-10 12:59:57 +08:00
|
|
|
|
2007-08-22 13:28:50 +08:00
|
|
|
// C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
|
2007-08-22 13:33:11 +08: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 11:06:46 +08: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 14:24:45 +08:00
|
|
|
if (NeedsInnerScope) EnterScope(Scope::DeclScope);
|
2007-08-22 13:28:50 +08:00
|
|
|
|
2006-08-10 12:59:57 +08:00
|
|
|
// Read the body statement.
|
2006-10-16 13:52:41 +08:00
|
|
|
StmtResult Body = ParseStatement();
|
2006-11-06 07:47:55 +08:00
|
|
|
|
2007-08-22 13:28:50 +08:00
|
|
|
// Pop the body scope if needed.
|
2007-08-22 13:33:11 +08:00
|
|
|
if (NeedsInnerScope) ExitScope();
|
2007-08-22 13:28:50 +08:00
|
|
|
|
2006-11-06 07:47:55 +08:00
|
|
|
ExitScope();
|
2006-10-16 13:52:41 +08:00
|
|
|
|
|
|
|
if (Cond.isInvalid || Body.isInvalid) return true;
|
|
|
|
|
2007-09-16 22:56:35 +08:00
|
|
|
return Actions.ActOnWhileStmt(WhileLoc, Cond.Val, Body.Val);
|
2006-08-10 12:59:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseDoStatement
|
|
|
|
/// do-statement: [C99 6.8.5.2]
|
|
|
|
/// 'do' statement 'while' '(' expression ')' ';'
|
2006-08-10 13:45:44 +08:00
|
|
|
/// Note: this lets the caller parse the end ';'.
|
2006-10-16 13:52:41 +08:00
|
|
|
Parser::StmtResult Parser::ParseDoStatement() {
|
2007-10-10 01:41:39 +08:00
|
|
|
assert(Tok.is(tok::kw_do) && "Not a do stmt!");
|
2006-10-16 14:06:51 +08:00
|
|
|
SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
|
2006-08-10 12:59:57 +08:00
|
|
|
|
2007-08-27 07:08:06 +08: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);
|
2006-11-06 07:47:55 +08:00
|
|
|
|
2007-08-22 13:28:50 +08:00
|
|
|
// C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
|
2007-08-22 13:33:11 +08: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 12:46:46 +08: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 14:24:45 +08:00
|
|
|
if (NeedsInnerScope) EnterScope(Scope::DeclScope);
|
2007-08-22 13:28:50 +08:00
|
|
|
|
2006-08-10 12:59:57 +08:00
|
|
|
// Read the body statement.
|
2006-10-16 13:52:41 +08:00
|
|
|
StmtResult Body = ParseStatement();
|
2006-08-10 12:59:57 +08:00
|
|
|
|
2007-08-22 13:28:50 +08:00
|
|
|
// Pop the body scope if needed.
|
2007-08-22 13:33:11 +08:00
|
|
|
if (NeedsInnerScope) ExitScope();
|
2007-08-22 13:28:50 +08:00
|
|
|
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.isNot(tok::kw_while)) {
|
2006-11-06 07:47:55 +08:00
|
|
|
ExitScope();
|
2006-08-10 12:59:57 +08:00
|
|
|
Diag(Tok, diag::err_expected_while);
|
2006-08-11 06:57:16 +08:00
|
|
|
Diag(DoLoc, diag::err_matching, "do");
|
2006-08-10 12:59:57 +08:00
|
|
|
SkipUntil(tok::semi);
|
2006-10-16 13:52:41 +08:00
|
|
|
return true;
|
2006-08-10 12:59:57 +08:00
|
|
|
}
|
2006-10-16 14:06:51 +08:00
|
|
|
SourceLocation WhileLoc = ConsumeToken();
|
2006-08-10 12:59:57 +08:00
|
|
|
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
2006-11-06 07:47:55 +08:00
|
|
|
ExitScope();
|
2006-08-10 12:59:57 +08:00
|
|
|
Diag(Tok, diag::err_expected_lparen_after, "do/while");
|
|
|
|
SkipUntil(tok::semi);
|
2006-10-16 13:52:41 +08:00
|
|
|
return true;
|
2006-08-10 12:59:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the condition.
|
2006-10-16 13:52:41 +08:00
|
|
|
ExprResult Cond = ParseSimpleParenExpression();
|
2006-11-06 07:47:55 +08:00
|
|
|
|
|
|
|
ExitScope();
|
|
|
|
|
2006-10-16 13:52:41 +08:00
|
|
|
if (Cond.isInvalid || Body.isInvalid) return true;
|
|
|
|
|
2007-09-16 22:56:35 +08:00
|
|
|
return Actions.ActOnDoStmt(DoLoc, Body.Val, WhileLoc, Cond.Val);
|
2006-08-10 12:59:57 +08: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 04:38:47 +08:00
|
|
|
/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
|
|
|
|
/// [C++] statement
|
2008-01-04 01:55:25 +08:00
|
|
|
/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
|
|
|
|
/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
|
2008-09-10 04:38:47 +08:00
|
|
|
///
|
|
|
|
/// [C++] for-init-statement:
|
|
|
|
/// [C++] expression-statement
|
|
|
|
/// [C++] simple-declaration
|
|
|
|
///
|
2006-10-16 13:52:41 +08:00
|
|
|
Parser::StmtResult Parser::ParseForStatement() {
|
2007-10-10 01:41:39 +08:00
|
|
|
assert(Tok.is(tok::kw_for) && "Not a for stmt!");
|
2006-10-16 14:06:51 +08:00
|
|
|
SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
|
2006-08-10 12:59:57 +08:00
|
|
|
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
2006-08-10 12:59:57 +08:00
|
|
|
Diag(Tok, diag::err_expected_lparen_after, "for");
|
|
|
|
SkipUntil(tok::semi);
|
2006-10-16 13:52:41 +08:00
|
|
|
return true;
|
2006-08-10 12:59:57 +08:00
|
|
|
}
|
2006-11-06 07:47:55 +08:00
|
|
|
|
2008-09-11 11:06:46 +08:00
|
|
|
bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
|
|
|
|
|
2007-08-27 07:08:06 +08: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 11:06:46 +08: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.
|
|
|
|
// 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 04:38:47 +08:00
|
|
|
EnterScope(Scope::BreakScope | Scope::ContinueScope |
|
|
|
|
Scope::DeclScope | Scope::ControlScope);
|
2007-08-27 07:08:06 +08:00
|
|
|
else
|
|
|
|
EnterScope(Scope::BreakScope | Scope::ContinueScope);
|
2006-08-10 12:59:57 +08:00
|
|
|
|
2006-10-16 14:12:55 +08:00
|
|
|
SourceLocation LParenLoc = ConsumeParen();
|
2006-08-11 14:41:18 +08:00
|
|
|
ExprResult Value;
|
|
|
|
|
2006-11-05 04:18:38 +08:00
|
|
|
StmtTy *FirstPart = 0;
|
2007-06-27 09:06:29 +08:00
|
|
|
ExprTy *SecondPart = 0;
|
|
|
|
StmtTy *ThirdPart = 0;
|
2008-01-05 07:23:46 +08:00
|
|
|
bool ForEach = false;
|
2006-11-05 04:18:38 +08:00
|
|
|
|
2006-08-10 12:59:57 +08:00
|
|
|
// Parse the first part of the for specifier.
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.is(tok::semi)) { // for (;
|
2006-08-10 13:19:57 +08:00
|
|
|
// no first part, eat the ';'.
|
|
|
|
ConsumeToken();
|
2006-08-10 12:59:57 +08:00
|
|
|
} else if (isDeclarationSpecifier()) { // for (int X = 4;
|
2006-08-10 13:19:57 +08:00
|
|
|
// Parse declaration, which eats the ';'.
|
2008-09-11 11:06:46 +08:00
|
|
|
if (!C99orCXX) // Use of C99-style for loops in C90 mode?
|
2006-08-10 13:22:36 +08:00
|
|
|
Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
|
2008-03-13 14:29:04 +08:00
|
|
|
|
|
|
|
SourceLocation DeclStart = Tok.getLocation();
|
2008-09-10 04:38:47 +08:00
|
|
|
DeclTy *aBlockVarDecl = ParseSimpleDeclaration(Declarator::ForContext);
|
2008-03-13 14:29:04 +08:00
|
|
|
// FIXME: Pass in the right location for the end of the declstmt.
|
|
|
|
StmtResult stmtResult = Actions.ActOnDeclStmt(aBlockVarDecl, DeclStart,
|
2008-03-13 14:29:54 +08:00
|
|
|
DeclStart);
|
2007-05-30 06:59:26 +08:00
|
|
|
FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
|
2008-01-05 07:23:46 +08:00
|
|
|
if ((ForEach = isTokIdentifier_in())) {
|
2008-01-04 01:55:25 +08:00
|
|
|
ConsumeToken(); // consume 'in'
|
|
|
|
Value = ParseExpression();
|
|
|
|
if (!Value.isInvalid)
|
|
|
|
SecondPart = Value.Val;
|
|
|
|
}
|
2006-08-10 12:59:57 +08:00
|
|
|
} else {
|
2006-08-11 14:41:18 +08:00
|
|
|
Value = ParseExpression();
|
2006-11-05 04:18:38 +08:00
|
|
|
|
2007-06-27 09:06:29 +08:00
|
|
|
// Turn the expression into a stmt.
|
|
|
|
if (!Value.isInvalid) {
|
2007-09-16 22:56:35 +08:00
|
|
|
StmtResult R = Actions.ActOnExprStmt(Value.Val);
|
2007-06-27 09:06:29 +08:00
|
|
|
if (!R.isInvalid)
|
|
|
|
FirstPart = R.Val;
|
|
|
|
}
|
2007-05-31 00:27:15 +08:00
|
|
|
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.is(tok::semi)) {
|
2006-08-10 13:19:57 +08:00
|
|
|
ConsumeToken();
|
2008-01-04 01:55:25 +08:00
|
|
|
}
|
2008-01-05 07:23:46 +08:00
|
|
|
else if ((ForEach = isTokIdentifier_in())) {
|
2008-01-04 01:55:25 +08:00
|
|
|
ConsumeToken(); // consume 'in'
|
|
|
|
Value = ParseExpression();
|
|
|
|
if (!Value.isInvalid)
|
|
|
|
SecondPart = Value.Val;
|
|
|
|
}
|
|
|
|
else {
|
2006-08-11 14:41:18 +08:00
|
|
|
if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
|
2006-08-10 13:19:57 +08:00
|
|
|
SkipUntil(tok::semi);
|
|
|
|
}
|
2006-08-10 12:59:57 +08:00
|
|
|
}
|
2008-01-05 07:23:46 +08:00
|
|
|
if (!ForEach) {
|
2008-01-04 01:55:25 +08:00
|
|
|
// Parse the second part of the for specifier.
|
|
|
|
if (Tok.is(tok::semi)) { // for (...;;
|
|
|
|
// no second part.
|
|
|
|
Value = ExprResult();
|
|
|
|
} else {
|
2008-09-10 04:38:47 +08:00
|
|
|
Value = getLang().CPlusPlus ? ParseCXXCondition()
|
|
|
|
: ParseExpression();
|
2008-01-04 01:55:25 +08:00
|
|
|
if (!Value.isInvalid)
|
|
|
|
SecondPart = Value.Val;
|
|
|
|
}
|
2006-08-10 12:59:57 +08:00
|
|
|
|
2008-01-04 01:55:25 +08:00
|
|
|
if (Tok.is(tok::semi)) {
|
|
|
|
ConsumeToken();
|
|
|
|
} else {
|
|
|
|
if (!Value.isInvalid) Diag(Tok, diag::err_expected_semi_for);
|
|
|
|
SkipUntil(tok::semi);
|
|
|
|
}
|
2006-08-10 12:59:57 +08:00
|
|
|
|
2008-01-04 01:55:25 +08: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-06-27 09:06:29 +08:00
|
|
|
}
|
2006-08-10 12:59:57 +08:00
|
|
|
}
|
2006-08-11 07:14:52 +08:00
|
|
|
// Match the ')'.
|
2006-11-05 04:18:38 +08:00
|
|
|
SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
|
2006-08-10 12:59:57 +08:00
|
|
|
|
2007-08-22 13:28:50 +08:00
|
|
|
// C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
|
2007-08-22 13:33:11 +08: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 11:06:46 +08: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 14:24:45 +08:00
|
|
|
if (NeedsInnerScope) EnterScope(Scope::DeclScope);
|
2007-08-22 13:28:50 +08:00
|
|
|
|
2006-08-10 12:59:57 +08:00
|
|
|
// Read the body statement.
|
2006-11-05 04:18:38 +08:00
|
|
|
StmtResult Body = ParseStatement();
|
2006-11-06 07:47:55 +08:00
|
|
|
|
2007-08-22 13:28:50 +08:00
|
|
|
// Pop the body scope if needed.
|
2007-08-22 13:33:11 +08:00
|
|
|
if (NeedsInnerScope) ExitScope();
|
2007-08-22 13:28:50 +08:00
|
|
|
|
2006-11-06 07:47:55 +08:00
|
|
|
// Leave the for-scope.
|
|
|
|
ExitScope();
|
|
|
|
|
2006-11-05 04:18:38 +08:00
|
|
|
if (Body.isInvalid)
|
|
|
|
return Body;
|
2006-10-16 13:52:41 +08:00
|
|
|
|
2008-01-05 07:23:46 +08:00
|
|
|
if (!ForEach)
|
|
|
|
return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart,
|
|
|
|
SecondPart, ThirdPart, RParenLoc, Body.Val);
|
|
|
|
else
|
2008-01-08 03:49:32 +08:00
|
|
|
return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc, FirstPart,
|
2008-01-05 07:23:46 +08:00
|
|
|
SecondPart, RParenLoc, Body.Val);
|
2006-08-10 12:59:57 +08:00
|
|
|
}
|
2006-08-10 12:23:57 +08:00
|
|
|
|
2006-08-10 13:45:44 +08:00
|
|
|
/// ParseGotoStatement
|
|
|
|
/// jump-statement:
|
|
|
|
/// 'goto' identifier ';'
|
|
|
|
/// [GNU] 'goto' '*' expression ';'
|
|
|
|
///
|
|
|
|
/// Note: this lets the caller parse the end ';'.
|
|
|
|
///
|
2006-10-16 13:52:41 +08:00
|
|
|
Parser::StmtResult Parser::ParseGotoStatement() {
|
2007-10-10 01:41:39 +08:00
|
|
|
assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
|
2006-10-16 14:06:51 +08:00
|
|
|
SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
|
2006-08-10 13:45:44 +08:00
|
|
|
|
2006-10-16 13:52:41 +08:00
|
|
|
StmtResult Res;
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.is(tok::identifier)) {
|
2007-09-16 22:56:35 +08:00
|
|
|
Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(),
|
2006-10-25 11:38:23 +08:00
|
|
|
Tok.getIdentifierInfo());
|
2006-08-10 13:45:44 +08:00
|
|
|
ConsumeToken();
|
2007-10-10 01:41:39 +08:00
|
|
|
} else if (Tok.is(tok::star) && !getLang().NoExtensions) {
|
2006-08-10 13:45:44 +08:00
|
|
|
// GNU indirect goto extension.
|
|
|
|
Diag(Tok, diag::ext_gnu_indirect_goto);
|
2006-10-16 14:06:51 +08:00
|
|
|
SourceLocation StarLoc = ConsumeToken();
|
2006-08-13 00:59:03 +08:00
|
|
|
ExprResult R = ParseExpression();
|
2006-10-16 13:52:41 +08:00
|
|
|
if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
|
2006-08-13 00:59:03 +08:00
|
|
|
SkipUntil(tok::semi, false, true);
|
2006-10-16 13:52:41 +08:00
|
|
|
return true;
|
|
|
|
}
|
2007-09-16 22:56:35 +08:00
|
|
|
Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.Val);
|
2007-07-22 12:13:33 +08:00
|
|
|
} else {
|
|
|
|
Diag(Tok, diag::err_expected_ident);
|
|
|
|
return true;
|
2006-08-10 13:45:44 +08:00
|
|
|
}
|
2007-07-22 12:13:33 +08:00
|
|
|
|
2006-10-16 13:52:41 +08:00
|
|
|
return Res;
|
2006-08-10 13:45:44 +08:00
|
|
|
}
|
|
|
|
|
2006-11-06 07:47:55 +08:00
|
|
|
/// 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 22:56:35 +08:00
|
|
|
return Actions.ActOnContinueStmt(ContinueLoc, CurScope);
|
2006-11-06 07:47:55 +08: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 22:56:35 +08:00
|
|
|
return Actions.ActOnBreakStmt(BreakLoc, CurScope);
|
2006-11-06 07:47:55 +08:00
|
|
|
}
|
|
|
|
|
2006-08-10 13:45:44 +08:00
|
|
|
/// ParseReturnStatement
|
|
|
|
/// jump-statement:
|
|
|
|
/// 'return' expression[opt] ';'
|
2006-10-16 13:52:41 +08:00
|
|
|
Parser::StmtResult Parser::ParseReturnStatement() {
|
2007-10-10 01:41:39 +08:00
|
|
|
assert(Tok.is(tok::kw_return) && "Not a return stmt!");
|
2006-10-16 14:06:51 +08:00
|
|
|
SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
|
2006-08-10 13:45:44 +08:00
|
|
|
|
2006-10-16 13:52:41 +08:00
|
|
|
ExprResult R(0);
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.isNot(tok::semi)) {
|
2006-10-16 13:52:41 +08:00
|
|
|
R = ParseExpression();
|
|
|
|
if (R.isInvalid) { // Skip to the semicolon, but don't consume it.
|
2006-08-13 00:59:03 +08:00
|
|
|
SkipUntil(tok::semi, false, true);
|
2006-10-16 13:52:41 +08:00
|
|
|
return true;
|
|
|
|
}
|
2006-08-13 00:59:03 +08:00
|
|
|
}
|
2007-09-16 22:56:35 +08:00
|
|
|
return Actions.ActOnReturnStmt(ReturnLoc, R.Val);
|
2006-08-10 13:45:44 +08:00
|
|
|
}
|
2006-08-15 14:03:28 +08:00
|
|
|
|
2008-02-12 07:15:56 +08:00
|
|
|
/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
|
|
|
|
/// routine is called to skip/ignore tokens that comprise the MS asm statement.
|
2008-02-07 11:50:06 +08:00
|
|
|
Parser::StmtResult Parser::FuzzyParseMicrosoftAsmStatement() {
|
2008-02-08 07:24:32 +08: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 11:36:19 +08:00
|
|
|
SourceLocation TokLoc = Tok.getLocation();
|
2008-02-09 02:01:27 +08: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 07:24:32 +08:00
|
|
|
}
|
2008-04-08 05:06:54 +08:00
|
|
|
return Actions.ActOnNullStmt(Tok.getLocation());
|
2008-02-07 11:50:06 +08:00
|
|
|
}
|
|
|
|
|
2006-08-15 14:03:28 +08:00
|
|
|
/// ParseAsmStatement - Parse a GNU extended asm statement.
|
2008-02-12 07:15:56 +08:00
|
|
|
/// asm-statement:
|
|
|
|
/// gnu-asm-statement
|
|
|
|
/// ms-asm-statement
|
|
|
|
///
|
|
|
|
/// [GNU] gnu-asm-statement:
|
2006-08-15 14:03:28 +08: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 07:15:56 +08: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 11:50:06 +08:00
|
|
|
Parser::StmtResult Parser::ParseAsmStatement(bool &msAsm) {
|
2007-10-10 01:41:39 +08:00
|
|
|
assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
|
2007-10-29 12:04:16 +08:00
|
|
|
SourceLocation AsmLoc = ConsumeToken();
|
2006-08-15 14:03:28 +08:00
|
|
|
|
2008-02-12 07:15:56 +08:00
|
|
|
if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
|
2008-02-07 11:50:06 +08:00
|
|
|
msAsm = true;
|
|
|
|
return FuzzyParseMicrosoftAsmStatement();
|
|
|
|
}
|
2006-08-15 14:03:28 +08:00
|
|
|
DeclSpec DS;
|
|
|
|
SourceLocation Loc = Tok.getLocation();
|
|
|
|
ParseTypeQualifierListOpt(DS);
|
|
|
|
|
|
|
|
// GNU asms accept, but warn, about type-qualifiers other than volatile.
|
2006-11-28 12:33:46 +08:00
|
|
|
if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
|
2006-08-15 14:03:28 +08:00
|
|
|
Diag(Loc, diag::w_asm_qualifier_ignored, "const");
|
2006-11-28 12:33:46 +08:00
|
|
|
if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
|
2006-08-15 14:03:28 +08:00
|
|
|
Diag(Loc, diag::w_asm_qualifier_ignored, "restrict");
|
|
|
|
|
|
|
|
// Remember if this was a volatile asm.
|
2007-11-24 07:12:25 +08:00
|
|
|
bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
|
2008-02-06 07:03:50 +08:00
|
|
|
bool isSimple = false;
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
2006-08-15 14:03:28 +08:00
|
|
|
Diag(Tok, diag::err_expected_lparen_after, "asm");
|
|
|
|
SkipUntil(tok::r_paren);
|
2006-10-16 13:52:41 +08:00
|
|
|
return true;
|
2006-08-15 14:03:28 +08:00
|
|
|
}
|
2006-10-16 14:12:55 +08:00
|
|
|
Loc = ConsumeParen();
|
2006-08-15 14:03:28 +08:00
|
|
|
|
2007-11-21 03:21:03 +08:00
|
|
|
ExprResult AsmString = ParseAsmStringLiteral();
|
|
|
|
if (AsmString.isInvalid)
|
|
|
|
return true;
|
2007-11-22 09:36:19 +08:00
|
|
|
|
|
|
|
llvm::SmallVector<std::string, 4> Names;
|
|
|
|
llvm::SmallVector<ExprTy*, 4> Constraints;
|
|
|
|
llvm::SmallVector<ExprTy*, 4> Exprs;
|
2008-02-06 07:03:50 +08:00
|
|
|
llvm::SmallVector<ExprTy*, 4> Clobbers;
|
|
|
|
|
|
|
|
unsigned NumInputs = 0, NumOutputs = 0;
|
2007-11-22 09:36:19 +08:00
|
|
|
|
2008-02-06 07:03:50 +08: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-10 03:57:29 +08:00
|
|
|
if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
|
|
|
|
return true;
|
2007-11-22 09:36:19 +08:00
|
|
|
|
2008-02-06 07:03:50 +08:00
|
|
|
NumOutputs = Names.size();
|
2006-08-15 14:03:28 +08:00
|
|
|
|
2008-02-06 07:03:50 +08:00
|
|
|
// Parse Inputs, if present.
|
2008-02-10 03:57:29 +08:00
|
|
|
if (ParseAsmOperandsOpt(Names, Constraints, Exprs))
|
|
|
|
return true;
|
|
|
|
|
2008-02-06 07:03:50 +08:00
|
|
|
assert(Names.size() == Constraints.size() &&
|
|
|
|
Constraints.size() == Exprs.size()
|
|
|
|
&& "Input operand size mismatch!");
|
2007-11-22 09:36:19 +08:00
|
|
|
|
2008-02-06 07:03:50 +08:00
|
|
|
NumInputs = Names.size() - NumOutputs;
|
2006-08-15 14:03:28 +08:00
|
|
|
|
2008-02-06 07:03:50 +08:00
|
|
|
// Parse the clobbers, if present.
|
|
|
|
if (Tok.is(tok::colon)) {
|
|
|
|
ConsumeToken();
|
2006-08-15 14:03:28 +08:00
|
|
|
|
2008-02-06 07:03:50 +08:00
|
|
|
// Parse the asm-string list for clobbers.
|
|
|
|
while (1) {
|
|
|
|
ExprResult Clobber = ParseAsmStringLiteral();
|
2006-08-15 14:03:28 +08:00
|
|
|
|
2008-02-06 07:03:50 +08:00
|
|
|
if (Clobber.isInvalid)
|
|
|
|
break;
|
2007-11-22 09:36:19 +08:00
|
|
|
|
2008-02-06 07:03:50 +08:00
|
|
|
Clobbers.push_back(Clobber.Val);
|
2007-11-22 09:36:19 +08:00
|
|
|
|
2008-02-06 07:03:50 +08:00
|
|
|
if (Tok.isNot(tok::comma)) break;
|
|
|
|
ConsumeToken();
|
|
|
|
}
|
2006-08-15 14:03:28 +08:00
|
|
|
}
|
|
|
|
|
2008-02-06 07:03:50 +08:00
|
|
|
RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
|
|
|
|
}
|
2006-10-16 13:52:41 +08:00
|
|
|
|
2008-02-06 07:03:50 +08:00
|
|
|
return Actions.ActOnAsmStmt(AsmLoc, isSimple, isVolatile,
|
|
|
|
NumOutputs, NumInputs,
|
2007-11-22 09:36:19 +08:00
|
|
|
&Names[0], &Constraints[0], &Exprs[0],
|
|
|
|
AsmString.Val,
|
|
|
|
Clobbers.size(), &Clobbers[0],
|
|
|
|
RParenLoc);
|
2006-08-15 14:03:28 +08: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-10 03:57:29 +08:00
|
|
|
bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<std::string> &Names,
|
2007-11-22 09:36:19 +08:00
|
|
|
llvm::SmallVectorImpl<ExprTy*> &Constraints,
|
|
|
|
llvm::SmallVectorImpl<ExprTy*> &Exprs) {
|
2006-08-15 14:03:28 +08:00
|
|
|
// Only do anything if this operand is present.
|
2008-02-10 03:57:29 +08:00
|
|
|
if (Tok.isNot(tok::colon)) return false;
|
2006-08-15 14:03:28 +08:00
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
// 'asm-operands' isn't present?
|
2007-10-10 01:41:39 +08:00
|
|
|
if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
|
2008-02-10 03:57:29 +08:00
|
|
|
return false;
|
2006-08-15 14:03:28 +08:00
|
|
|
|
2007-11-22 09:36:19 +08:00
|
|
|
while (1) {
|
2006-08-15 14:03:28 +08:00
|
|
|
// Read the [id] if present.
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.is(tok::l_square)) {
|
2006-10-16 14:12:55 +08:00
|
|
|
SourceLocation Loc = ConsumeBracket();
|
2006-08-15 14:03:28 +08:00
|
|
|
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.isNot(tok::identifier)) {
|
2006-08-15 14:03:28 +08:00
|
|
|
Diag(Tok, diag::err_expected_ident);
|
|
|
|
SkipUntil(tok::r_paren);
|
2008-02-10 03:57:29 +08:00
|
|
|
return true;
|
2006-08-15 14:03:28 +08:00
|
|
|
}
|
2007-10-29 12:06:22 +08:00
|
|
|
|
2007-11-22 09:36:19 +08:00
|
|
|
IdentifierInfo *II = Tok.getIdentifierInfo();
|
2007-10-29 12:06:22 +08:00
|
|
|
ConsumeToken();
|
2007-11-22 09:36:19 +08:00
|
|
|
|
|
|
|
Names.push_back(std::string(II->getName(), II->getLength()));
|
2006-08-15 14:03:28 +08:00
|
|
|
MatchRHSPunctuation(tok::r_square, Loc);
|
2007-11-22 09:36:19 +08:00
|
|
|
} else
|
|
|
|
Names.push_back(std::string());
|
2006-08-15 14:03:28 +08:00
|
|
|
|
2007-11-22 09:36:19 +08:00
|
|
|
ExprResult Constraint = ParseAsmStringLiteral();
|
|
|
|
if (Constraint.isInvalid) {
|
|
|
|
SkipUntil(tok::r_paren);
|
2008-02-10 03:57:29 +08:00
|
|
|
return true;
|
2007-11-22 09:36:19 +08:00
|
|
|
}
|
|
|
|
Constraints.push_back(Constraint.Val);
|
2006-08-15 14:03:28 +08:00
|
|
|
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
2006-08-15 14:03:28 +08:00
|
|
|
Diag(Tok, diag::err_expected_lparen_after, "asm operand");
|
|
|
|
SkipUntil(tok::r_paren);
|
2008-02-10 03:57:29 +08:00
|
|
|
return true;
|
2006-08-15 14:03:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read the parenthesized expression.
|
2006-08-24 14:37:51 +08:00
|
|
|
ExprResult Res = ParseSimpleParenExpression();
|
2006-08-15 14:03:28 +08:00
|
|
|
if (Res.isInvalid) {
|
|
|
|
SkipUntil(tok::r_paren);
|
2008-02-10 03:57:29 +08:00
|
|
|
return true;
|
2006-08-15 14:03:28 +08:00
|
|
|
}
|
2007-11-22 09:36:19 +08:00
|
|
|
Exprs.push_back(Res.Val);
|
2006-08-15 14:03:28 +08:00
|
|
|
// Eat the comma and continue parsing if it exists.
|
2008-02-10 03:57:29 +08:00
|
|
|
if (Tok.isNot(tok::comma)) return false;
|
2006-08-15 14:03:28 +08:00
|
|
|
ConsumeToken();
|
|
|
|
}
|
2008-02-10 03:57:29 +08:00
|
|
|
|
|
|
|
return true;
|
2006-08-15 14:03:28 +08:00
|
|
|
}
|
2007-11-09 03:01:26 +08: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 07:20:51 +08:00
|
|
|
return Actions.ActOnFinishFunctionBody(Decl, FnBody.Val);
|
2007-12-01 16:06:07 +08:00
|
|
|
}
|