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"
|
2009-12-10 08:21:05 +08:00
|
|
|
#include "RAIIObjectsForParser.h"
|
2010-08-21 02:27:03 +08:00
|
|
|
#include "clang/Sema/DeclSpec.h"
|
2010-08-27 07:41:50 +08:00
|
|
|
#include "clang/Sema/PrettyDeclStackTrace.h"
|
2010-08-21 02:27:03 +08:00
|
|
|
#include "clang/Sema/Scope.h"
|
2009-03-05 08:00:31 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
#include "clang/Basic/PrettyStackTrace.h"
|
|
|
|
#include "clang/Basic/SourceManager.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
|
2008-12-22 00:41:36 +08:00
|
|
|
/// [C++] try-block
|
2011-04-28 09:08:34 +08:00
|
|
|
/// [MS] seh-try-block
|
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 ';'
|
2009-09-09 23:08:12 +08:00
|
|
|
/// [OBC] '@' 'throw' ';'
|
|
|
|
///
|
2010-08-24 14:29:42 +08:00
|
|
|
StmtResult
|
2010-09-29 04:42:35 +08:00
|
|
|
Parser::ParseStatementOrDeclaration(StmtVector &Stmts, bool OnlyStatement) {
|
2006-08-10 13:45:44 +08:00
|
|
|
const char *SemiError = 0;
|
2010-08-24 14:29:42 +08:00
|
|
|
StmtResult Res;
|
2010-06-17 18:52:18 +08:00
|
|
|
|
|
|
|
ParenBraceBracketBalancer BalancerRAIIObj(*this);
|
2008-12-09 21:15:23 +08:00
|
|
|
|
2011-03-24 19:26:52 +08:00
|
|
|
ParsedAttributesWithRange attrs(AttrFactory);
|
2010-12-24 10:08:15 +08:00
|
|
|
MaybeParseCXX0XAttributes(attrs);
|
2009-11-21 16:43:09 +08:00
|
|
|
|
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.
|
2011-04-24 13:37:28 +08:00
|
|
|
Retry:
|
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-12-12 04:12:42 +08:00
|
|
|
return ParseObjCAtStatement(AtLoc);
|
2007-09-20 03:14:32 +08:00
|
|
|
}
|
|
|
|
|
2009-09-22 04:51:25 +08:00
|
|
|
case tok::code_completion:
|
2010-08-27 07:41:50 +08:00
|
|
|
Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
|
2011-09-04 11:32:15 +08:00
|
|
|
cutOffParsing();
|
|
|
|
return StmtError();
|
2009-09-22 04:51:25 +08:00
|
|
|
|
2011-04-24 13:37:28 +08:00
|
|
|
case tok::identifier: {
|
|
|
|
Token Next = NextToken();
|
|
|
|
if (Next.is(tok::colon)) { // C99 6.8.1: labeled-statement
|
2008-07-13 05:04:42 +08:00
|
|
|
// identifier ':' statement
|
2010-12-24 10:08:15 +08:00
|
|
|
return ParseLabeledStatement(attrs);
|
2008-07-13 05:04:42 +08:00
|
|
|
}
|
2011-04-24 13:37:28 +08:00
|
|
|
|
2011-04-27 12:48:22 +08:00
|
|
|
if (Next.isNot(tok::coloncolon)) {
|
2011-04-24 13:37:28 +08:00
|
|
|
CXXScopeSpec SS;
|
|
|
|
IdentifierInfo *Name = Tok.getIdentifierInfo();
|
|
|
|
SourceLocation NameLoc = Tok.getLocation();
|
2011-09-20 03:01:00 +08:00
|
|
|
|
|
|
|
if (getLang().CPlusPlus)
|
|
|
|
CheckForTemplateAndDigraph(Next, ParsedType(),
|
|
|
|
/*EnteringContext=*/false, *Name, SS);
|
|
|
|
|
2011-04-24 13:37:28 +08:00
|
|
|
Sema::NameClassification Classification
|
|
|
|
= Actions.ClassifyName(getCurScope(), SS, Name, NameLoc, Next);
|
|
|
|
switch (Classification.getKind()) {
|
|
|
|
case Sema::NC_Keyword:
|
|
|
|
// The identifier was corrected to a keyword. Update the token
|
|
|
|
// to this keyword, and try again.
|
|
|
|
if (Name->getTokenID() != tok::identifier) {
|
|
|
|
Tok.setIdentifierInfo(Name);
|
|
|
|
Tok.setKind(Name->getTokenID());
|
|
|
|
goto Retry;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fall through via the normal error path.
|
|
|
|
// FIXME: This seems like it could only happen for context-sensitive
|
|
|
|
// keywords.
|
|
|
|
|
|
|
|
case Sema::NC_Error:
|
|
|
|
// Handle errors here by skipping up to the next semicolon or '}', and
|
|
|
|
// eat the semicolon if that's what stopped us.
|
|
|
|
SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
|
|
|
|
if (Tok.is(tok::semi))
|
|
|
|
ConsumeToken();
|
|
|
|
return StmtError();
|
|
|
|
|
|
|
|
case Sema::NC_Unknown:
|
|
|
|
// Either we don't know anything about this identifier, or we know that
|
|
|
|
// we're in a syntactic context we haven't handled yet.
|
|
|
|
break;
|
|
|
|
|
2011-04-27 13:41:15 +08:00
|
|
|
case Sema::NC_Type:
|
2011-04-27 12:48:22 +08:00
|
|
|
Tok.setKind(tok::annot_typename);
|
|
|
|
setTypeAnnotation(Tok, Classification.getType());
|
|
|
|
Tok.setAnnotationEndLoc(NameLoc);
|
|
|
|
PP.AnnotateCachedTokens(Tok);
|
2011-04-24 13:37:28 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Sema::NC_Expression:
|
2011-04-27 14:18:01 +08:00
|
|
|
Tok.setKind(tok::annot_primary_expr);
|
|
|
|
setExprAnnotation(Tok, Classification.getExpression());
|
|
|
|
Tok.setAnnotationEndLoc(NameLoc);
|
|
|
|
PP.AnnotateCachedTokens(Tok);
|
|
|
|
break;
|
2011-04-24 13:37:28 +08:00
|
|
|
|
|
|
|
case Sema::NC_TypeTemplate:
|
|
|
|
case Sema::NC_FunctionTemplate: {
|
|
|
|
ConsumeToken(); // the identifier
|
|
|
|
UnqualifiedId Id;
|
|
|
|
Id.setIdentifier(Name, NameLoc);
|
|
|
|
if (AnnotateTemplateIdToken(
|
|
|
|
TemplateTy::make(Classification.getTemplateName()),
|
|
|
|
Classification.getTemplateNameKind(),
|
2011-04-27 12:48:22 +08:00
|
|
|
SS, Id, SourceLocation(),
|
|
|
|
/*AllowTypeAnnotation=*/false)) {
|
|
|
|
// Handle errors here by skipping up to the next semicolon or '}', and
|
|
|
|
// eat the semicolon if that's what stopped us.
|
|
|
|
SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
|
|
|
|
if (Tok.is(tok::semi))
|
|
|
|
ConsumeToken();
|
|
|
|
return StmtError();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the next token is '::', jump right into parsing a
|
|
|
|
// nested-name-specifier. We don't want to leave the template-id
|
|
|
|
// hanging.
|
|
|
|
if (NextToken().is(tok::coloncolon) && TryAnnotateCXXScopeToken(false)){
|
2011-04-24 13:37:28 +08:00
|
|
|
// Handle errors here by skipping up to the next semicolon or '}', and
|
|
|
|
// eat the semicolon if that's what stopped us.
|
|
|
|
SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
|
|
|
|
if (Tok.is(tok::semi))
|
|
|
|
ConsumeToken();
|
|
|
|
return StmtError();
|
|
|
|
}
|
|
|
|
|
|
|
|
// We've annotated a template-id, so try again now.
|
|
|
|
goto Retry;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Sema::NC_NestedNameSpecifier:
|
|
|
|
// FIXME: Implement this!
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fall through
|
|
|
|
}
|
|
|
|
|
2009-03-25 01:04:48 +08:00
|
|
|
default: {
|
2008-10-05 08:06:24 +08:00
|
|
|
if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
|
fix a FIXME, providing accurate source range info for DeclStmt's. The end
of the range is now the ';' location. For something like this:
$ cat t2.c
#define bool int
void f(int x, int y) {
bool b = !x && y;
}
We used to produce:
$ clang-cc t2.c -ast-dump
typedef char *__builtin_va_list;
void f(int x, int y)
(CompoundStmt 0x2201f10 <t2.c:3:22, line:5:1>
(DeclStmt 0x2201ef0 <line:2:14> <----
0x2201a20 "int b =
(BinaryOperator 0x2201ed0 <line:4:10, col:16> 'int' '&&'
(UnaryOperator 0x2201e90 <col:10, col:11> 'int' prefix '!'
(DeclRefExpr 0x2201c90 <col:11> 'int' ParmVar='x' 0x2201a50))
(DeclRefExpr 0x2201eb0 <col:16> 'int' ParmVar='y' 0x2201e10))")
Now we produce:
$ clang-cc t2.c -ast-dump
typedef char *__builtin_va_list;
void f(int x, int y)
(CompoundStmt 0x2201f10 <t2.c:3:22, line:5:1>
(DeclStmt 0x2201ef0 <line:2:14, line:4:17> <------
0x2201a20 "int b =
(BinaryOperator 0x2201ed0 <col:10, col:16> 'int' '&&'
(UnaryOperator 0x2201e90 <col:10, col:11> 'int' prefix '!'
(DeclRefExpr 0x2201c90 <col:11> 'int' ParmVar='x' 0x2201a50))
(DeclRefExpr 0x2201eb0 <col:16> 'int' ParmVar='y' 0x2201e10))")
llvm-svn: 68288
2009-04-02 12:16:50 +08:00
|
|
|
SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
|
2010-11-10 13:59:39 +08:00
|
|
|
DeclGroupPtrTy Decl = ParseDeclaration(Stmts, Declarator::BlockContext,
|
2010-12-24 10:08:15 +08:00
|
|
|
DeclEnd, attrs);
|
fix a FIXME, providing accurate source range info for DeclStmt's. The end
of the range is now the ';' location. For something like this:
$ cat t2.c
#define bool int
void f(int x, int y) {
bool b = !x && y;
}
We used to produce:
$ clang-cc t2.c -ast-dump
typedef char *__builtin_va_list;
void f(int x, int y)
(CompoundStmt 0x2201f10 <t2.c:3:22, line:5:1>
(DeclStmt 0x2201ef0 <line:2:14> <----
0x2201a20 "int b =
(BinaryOperator 0x2201ed0 <line:4:10, col:16> 'int' '&&'
(UnaryOperator 0x2201e90 <col:10, col:11> 'int' prefix '!'
(DeclRefExpr 0x2201c90 <col:11> 'int' ParmVar='x' 0x2201a50))
(DeclRefExpr 0x2201eb0 <col:16> 'int' ParmVar='y' 0x2201e10))")
Now we produce:
$ clang-cc t2.c -ast-dump
typedef char *__builtin_va_list;
void f(int x, int y)
(CompoundStmt 0x2201f10 <t2.c:3:22, line:5:1>
(DeclStmt 0x2201ef0 <line:2:14, line:4:17> <------
0x2201a20 "int b =
(BinaryOperator 0x2201ed0 <col:10, col:16> 'int' '&&'
(UnaryOperator 0x2201e90 <col:10, col:11> 'int' prefix '!'
(DeclRefExpr 0x2201c90 <col:11> 'int' ParmVar='x' 0x2201a50))
(DeclRefExpr 0x2201eb0 <col:16> 'int' ParmVar='y' 0x2201e10))")
llvm-svn: 68288
2009-04-02 12:16:50 +08:00
|
|
|
return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
|
2009-03-25 01:04:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Tok.is(tok::r_brace)) {
|
2006-08-11 02:26:31 +08:00
|
|
|
Diag(Tok, diag::err_expected_statement);
|
2008-12-12 03:30:53 +08:00
|
|
|
return StmtError();
|
2006-08-11 02:26:31 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-04-27 14:18:01 +08:00
|
|
|
return ParseExprStatement(attrs);
|
2009-03-25 01:04:48 +08:00
|
|
|
}
|
2008-12-12 03:30:53 +08:00
|
|
|
|
2006-08-10 13:59:48 +08:00
|
|
|
case tok::kw_case: // C99 6.8.1: labeled-statement
|
2010-12-24 10:08:15 +08:00
|
|
|
return ParseCaseStatement(attrs);
|
2006-08-10 13:59:48 +08:00
|
|
|
case tok::kw_default: // C99 6.8.1: labeled-statement
|
2010-12-24 10:08:15 +08:00
|
|
|
return ParseDefaultStatement(attrs);
|
2008-12-12 03:30:53 +08:00
|
|
|
|
2006-08-10 12:59:57 +08:00
|
|
|
case tok::l_brace: // C99 6.8.2: compound-statement
|
2010-12-24 10:08:15 +08:00
|
|
|
return ParseCompoundStatement(attrs);
|
2010-11-20 10:04:01 +08:00
|
|
|
case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
|
2011-09-02 05:53:45 +08:00
|
|
|
bool HasLeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
|
|
|
|
return Actions.ActOnNullStmt(ConsumeToken(), HasLeadingEmptyMacro);
|
2010-11-20 10:04:01 +08:00
|
|
|
}
|
2008-12-12 03:30:53 +08:00
|
|
|
|
2006-08-10 12:59:57 +08:00
|
|
|
case tok::kw_if: // C99 6.8.4.1: if-statement
|
2010-12-24 10:08:15 +08:00
|
|
|
return ParseIfStatement(attrs);
|
2006-08-10 12:59:57 +08:00
|
|
|
case tok::kw_switch: // C99 6.8.4.2: switch-statement
|
2010-12-24 10:08:15 +08:00
|
|
|
return ParseSwitchStatement(attrs);
|
2008-12-12 03:30:53 +08:00
|
|
|
|
2006-08-10 12:59:57 +08:00
|
|
|
case tok::kw_while: // C99 6.8.5.1: while-statement
|
2010-12-24 10:08:15 +08:00
|
|
|
return ParseWhileStatement(attrs);
|
2006-08-10 12:59:57 +08:00
|
|
|
case tok::kw_do: // C99 6.8.5.2: do-statement
|
2010-12-24 10:08:15 +08:00
|
|
|
Res = ParseDoStatement(attrs);
|
2009-06-14 08:07:48 +08:00
|
|
|
SemiError = "do/while";
|
2006-08-10 12:59:57 +08:00
|
|
|
break;
|
|
|
|
case tok::kw_for: // C99 6.8.5.3: for-statement
|
2010-12-24 10:08:15 +08:00
|
|
|
return ParseForStatement(attrs);
|
2006-08-10 13:45:44 +08:00
|
|
|
|
|
|
|
case tok::kw_goto: // C99 6.8.6.1: goto-statement
|
2010-12-24 10:08:15 +08:00
|
|
|
Res = ParseGotoStatement(attrs);
|
2009-06-14 08:07:48 +08:00
|
|
|
SemiError = "goto";
|
2006-08-10 13:45:44 +08:00
|
|
|
break;
|
|
|
|
case tok::kw_continue: // C99 6.8.6.2: continue-statement
|
2010-12-24 10:08:15 +08:00
|
|
|
Res = ParseContinueStatement(attrs);
|
2009-06-14 08:07:48 +08:00
|
|
|
SemiError = "continue";
|
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
|
2010-12-24 10:08:15 +08:00
|
|
|
Res = ParseBreakStatement(attrs);
|
2009-06-14 08:07:48 +08:00
|
|
|
SemiError = "break";
|
2006-08-10 13:45:44 +08:00
|
|
|
break;
|
|
|
|
case tok::kw_return: // C99 6.8.6.4: return-statement
|
2010-12-24 10:08:15 +08:00
|
|
|
Res = ParseReturnStatement(attrs);
|
2009-06-14 08:07:48 +08:00
|
|
|
SemiError = "return";
|
2006-08-10 13:45:44 +08:00
|
|
|
break;
|
2008-12-12 03:30:53 +08:00
|
|
|
|
2008-12-22 00:41:36 +08:00
|
|
|
case tok::kw_asm: {
|
2010-12-24 10:08:15 +08:00
|
|
|
ProhibitAttributes(attrs);
|
2008-02-07 11:50:06 +08:00
|
|
|
bool msAsm = false;
|
|
|
|
Res = ParseAsmStatement(msAsm);
|
2010-11-02 10:33:08 +08:00
|
|
|
Res = Actions.ActOnFinishFullStmt(Res.get());
|
2008-12-12 03:30:53 +08:00
|
|
|
if (msAsm) return move(Res);
|
2009-06-14 08:07:48 +08:00
|
|
|
SemiError = "asm";
|
2006-08-15 14:03:28 +08:00
|
|
|
break;
|
2006-08-09 13:47:47 +08:00
|
|
|
}
|
2008-12-12 03:30:53 +08:00
|
|
|
|
2008-12-22 00:41:36 +08:00
|
|
|
case tok::kw_try: // C++ 15: try-block
|
2010-12-24 10:08:15 +08:00
|
|
|
return ParseCXXTryBlock(attrs);
|
2011-04-28 09:08:34 +08:00
|
|
|
|
|
|
|
case tok::kw___try:
|
|
|
|
return ParseSEHTryBlock(attrs);
|
2008-12-22 00:41:36 +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();
|
2008-12-09 21:15:23 +08:00
|
|
|
} else if (!Res.isInvalid()) {
|
2009-06-14 08:23:56 +08:00
|
|
|
// If the result was valid, then we do want to diagnose this. Use
|
|
|
|
// ExpectAndConsume to emit the diagnostic, even though we know it won't
|
|
|
|
// succeed.
|
|
|
|
ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
|
2008-11-14 02:52:53 +08:00
|
|
|
// Skip until we see a } or ;, but don't eat it.
|
|
|
|
SkipUntil(tok::r_brace, true, true);
|
2006-08-10 13:45:44 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-12 03:30:53 +08:00
|
|
|
return move(Res);
|
2006-08-09 13:47:47 +08:00
|
|
|
}
|
|
|
|
|
2011-04-24 13:37:28 +08:00
|
|
|
/// \brief Parse an expression statement.
|
2011-04-27 14:18:01 +08:00
|
|
|
StmtResult Parser::ParseExprStatement(ParsedAttributes &Attrs) {
|
2011-04-24 13:37:28 +08:00
|
|
|
// If a case keyword is missing, this is where it should be inserted.
|
|
|
|
Token OldToken = Tok;
|
|
|
|
|
|
|
|
// FIXME: Use the attributes
|
|
|
|
// expression[opt] ';'
|
2011-04-27 14:18:01 +08:00
|
|
|
ExprResult Expr(ParseExpression());
|
2011-04-24 13:37:28 +08:00
|
|
|
if (Expr.isInvalid()) {
|
|
|
|
// If the expression is invalid, skip ahead to the next semicolon or '}'.
|
|
|
|
// Not doing this opens us up to the possibility of infinite loops if
|
|
|
|
// ParseExpression does not consume any tokens.
|
|
|
|
SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
|
|
|
|
if (Tok.is(tok::semi))
|
|
|
|
ConsumeToken();
|
|
|
|
return StmtError();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
|
|
|
|
Actions.CheckCaseExpression(Expr.get())) {
|
|
|
|
// If a constant expression is followed by a colon inside a switch block,
|
|
|
|
// suggest a missing case keyword.
|
|
|
|
Diag(OldToken, diag::err_expected_case_before_expression)
|
|
|
|
<< FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
|
|
|
|
|
|
|
|
// Recover parsing as a case statement.
|
|
|
|
return ParseCaseStatement(Attrs, /*MissingCase=*/true, Expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, eat the semicolon.
|
|
|
|
ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
|
|
|
|
return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr.get()));
|
2011-04-28 09:08:34 +08:00
|
|
|
}
|
2011-04-24 13:37:28 +08:00
|
|
|
|
2011-04-28 09:08:34 +08:00
|
|
|
StmtResult Parser::ParseSEHTryBlock(ParsedAttributes & Attrs) {
|
|
|
|
assert(Tok.is(tok::kw___try) && "Expected '__try'");
|
|
|
|
SourceLocation Loc = ConsumeToken();
|
|
|
|
return ParseSEHTryBlockCommon(Loc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseSEHTryBlockCommon
|
|
|
|
///
|
|
|
|
/// seh-try-block:
|
|
|
|
/// '__try' compound-statement seh-handler
|
|
|
|
///
|
|
|
|
/// seh-handler:
|
|
|
|
/// seh-except-block
|
|
|
|
/// seh-finally-block
|
|
|
|
///
|
|
|
|
StmtResult Parser::ParseSEHTryBlockCommon(SourceLocation TryLoc) {
|
|
|
|
if(Tok.isNot(tok::l_brace))
|
|
|
|
return StmtError(Diag(Tok,diag::err_expected_lbrace));
|
|
|
|
|
|
|
|
ParsedAttributesWithRange attrs(AttrFactory);
|
|
|
|
StmtResult TryBlock(ParseCompoundStatement(attrs));
|
|
|
|
if(TryBlock.isInvalid())
|
|
|
|
return move(TryBlock);
|
|
|
|
|
|
|
|
StmtResult Handler;
|
|
|
|
if(Tok.is(tok::kw___except)) {
|
|
|
|
SourceLocation Loc = ConsumeToken();
|
|
|
|
Handler = ParseSEHExceptBlock(Loc);
|
|
|
|
} else if (Tok.is(tok::kw___finally)) {
|
|
|
|
SourceLocation Loc = ConsumeToken();
|
|
|
|
Handler = ParseSEHFinallyBlock(Loc);
|
|
|
|
} else {
|
|
|
|
return StmtError(Diag(Tok,diag::err_seh_expected_handler));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(Handler.isInvalid())
|
|
|
|
return move(Handler);
|
|
|
|
|
|
|
|
return Actions.ActOnSEHTryBlock(false /* IsCXXTry */,
|
|
|
|
TryLoc,
|
|
|
|
TryBlock.take(),
|
|
|
|
Handler.take());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseSEHExceptBlock - Handle __except
|
|
|
|
///
|
|
|
|
/// seh-except-block:
|
|
|
|
/// '__except' '(' seh-filter-expression ')' compound-statement
|
|
|
|
///
|
|
|
|
StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) {
|
|
|
|
PoisonIdentifierRAIIObject raii(Ident__exception_code, false),
|
|
|
|
raii2(Ident___exception_code, false),
|
|
|
|
raii3(Ident_GetExceptionCode, false);
|
|
|
|
|
|
|
|
if(ExpectAndConsume(tok::l_paren,diag::err_expected_lparen))
|
|
|
|
return StmtError();
|
|
|
|
|
|
|
|
ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope);
|
|
|
|
|
2011-04-28 11:14:31 +08:00
|
|
|
if (getLang().Borland) {
|
|
|
|
Ident__exception_info->setIsPoisoned(false);
|
|
|
|
Ident___exception_info->setIsPoisoned(false);
|
|
|
|
Ident_GetExceptionInfo->setIsPoisoned(false);
|
|
|
|
}
|
2011-04-28 09:08:34 +08:00
|
|
|
ExprResult FilterExpr(ParseExpression());
|
2011-04-28 11:14:31 +08:00
|
|
|
|
|
|
|
if (getLang().Borland) {
|
|
|
|
Ident__exception_info->setIsPoisoned(true);
|
|
|
|
Ident___exception_info->setIsPoisoned(true);
|
|
|
|
Ident_GetExceptionInfo->setIsPoisoned(true);
|
|
|
|
}
|
2011-04-28 09:08:34 +08:00
|
|
|
|
|
|
|
if(FilterExpr.isInvalid())
|
|
|
|
return StmtError();
|
|
|
|
|
|
|
|
if(ExpectAndConsume(tok::r_paren,diag::err_expected_rparen))
|
|
|
|
return StmtError();
|
|
|
|
|
|
|
|
ParsedAttributesWithRange attrs(AttrFactory);
|
|
|
|
StmtResult Block(ParseCompoundStatement(attrs));
|
|
|
|
|
|
|
|
if(Block.isInvalid())
|
|
|
|
return move(Block);
|
|
|
|
|
|
|
|
return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.take(), Block.take());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseSEHFinallyBlock - Handle __finally
|
|
|
|
///
|
|
|
|
/// seh-finally-block:
|
|
|
|
/// '__finally' compound-statement
|
|
|
|
///
|
|
|
|
StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyBlock) {
|
|
|
|
PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
|
|
|
|
raii2(Ident___abnormal_termination, false),
|
|
|
|
raii3(Ident_AbnormalTermination, false);
|
|
|
|
|
|
|
|
ParsedAttributesWithRange attrs(AttrFactory);
|
|
|
|
StmtResult Block(ParseCompoundStatement(attrs));
|
|
|
|
if(Block.isInvalid())
|
|
|
|
return move(Block);
|
|
|
|
|
|
|
|
return Actions.ActOnSEHFinallyBlock(FinallyBlock,Block.take());
|
2011-04-24 13:37:28 +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
|
|
|
///
|
2010-12-24 10:08:15 +08:00
|
|
|
StmtResult Parser::ParseLabeledStatement(ParsedAttributes &attrs) {
|
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!");
|
2008-12-12 03:30:53 +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.
|
2010-12-24 10:08:15 +08:00
|
|
|
MaybeParseGNUAttributes(attrs);
|
2006-08-15 12:50:22 +08:00
|
|
|
|
2010-08-24 14:29:42 +08:00
|
|
|
StmtResult SubStmt(ParseStatement());
|
2008-12-09 21:15:23 +08:00
|
|
|
|
2008-07-10 06:53:07 +08:00
|
|
|
// Broken substmt shouldn't prevent the label from being added to the AST.
|
2008-12-09 21:15:23 +08:00
|
|
|
if (SubStmt.isInvalid())
|
2008-07-10 06:53:07 +08:00
|
|
|
SubStmt = Actions.ActOnNullStmt(ColonLoc);
|
2011-02-18 09:27:55 +08:00
|
|
|
|
|
|
|
LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
|
|
|
|
IdentTok.getLocation());
|
|
|
|
if (AttributeList *Attrs = attrs.getList())
|
|
|
|
Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
|
|
|
|
|
|
|
|
return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
|
|
|
|
SubStmt.get());
|
2008-07-10 06:53:07 +08:00
|
|
|
}
|
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
|
|
|
///
|
2011-04-22 05:44:26 +08:00
|
|
|
StmtResult Parser::ParseCaseStatement(ParsedAttributes &attrs, bool MissingCase,
|
|
|
|
ExprResult Expr) {
|
2011-04-22 06:48:40 +08:00
|
|
|
assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
|
2009-11-21 16:43:09 +08:00
|
|
|
// FIXME: Use attributes?
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-04 12:23:07 +08:00
|
|
|
// It is very very common for code to contain many case statements recursively
|
|
|
|
// nested, as in (but usually without indentation):
|
|
|
|
// case 1:
|
|
|
|
// case 2:
|
|
|
|
// case 3:
|
|
|
|
// case 4:
|
|
|
|
// case 5: etc.
|
|
|
|
//
|
|
|
|
// Parsing this naively works, but is both inefficient and can cause us to run
|
|
|
|
// out of stack space in our recursive descent parser. As a special case,
|
2009-03-05 02:24:58 +08:00
|
|
|
// flatten this recursion into an iterative loop. This is complex and gross,
|
2009-03-04 12:23:07 +08:00
|
|
|
// but all the grossness is constrained to ParseCaseStatement (and some
|
|
|
|
// wierdness in the actions), so this is just local grossness :).
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-04 12:23:07 +08:00
|
|
|
// TopLevelCase - This is the highest level we have parsed. 'case 1' in the
|
|
|
|
// example above.
|
2010-08-24 14:29:42 +08:00
|
|
|
StmtResult TopLevelCase(true);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-04 12:23:07 +08:00
|
|
|
// DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
|
|
|
|
// gets updated each time a new case is parsed, and whose body is unset so
|
|
|
|
// far. When parsing 'case 4', this is the 'case 3' node.
|
2011-09-09 10:16:15 +08:00
|
|
|
Stmt *DeepestParsedCaseStmt = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-04 12:23:07 +08:00
|
|
|
// While we have case statements, eat and stack them.
|
2011-06-13 13:50:12 +08:00
|
|
|
SourceLocation ColonLoc;
|
2009-03-04 12:23:07 +08:00
|
|
|
do {
|
2011-04-22 05:44:26 +08:00
|
|
|
SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
|
|
|
|
ConsumeToken(); // eat the 'case'.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-22 02:10:23 +08:00
|
|
|
if (Tok.is(tok::code_completion)) {
|
2010-07-03 01:43:08 +08:00
|
|
|
Actions.CodeCompleteCase(getCurScope());
|
2011-09-04 11:32:15 +08:00
|
|
|
cutOffParsing();
|
|
|
|
return StmtError();
|
2009-09-22 02:10:23 +08:00
|
|
|
}
|
|
|
|
|
2009-12-10 08:38:54 +08:00
|
|
|
/// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
|
|
|
|
/// Disable this form of error recovery while we're parsing the case
|
|
|
|
/// expression.
|
|
|
|
ColonProtectionRAIIObject ColonProtection(*this);
|
|
|
|
|
2011-04-22 05:44:26 +08:00
|
|
|
ExprResult LHS(MissingCase ? Expr : ParseConstantExpression());
|
|
|
|
MissingCase = false;
|
2009-03-04 12:23:07 +08:00
|
|
|
if (LHS.isInvalid()) {
|
|
|
|
SkipUntil(tok::colon);
|
|
|
|
return StmtError();
|
|
|
|
}
|
2006-08-10 13:59:48 +08:00
|
|
|
|
2009-03-04 12:23:07 +08:00
|
|
|
// GNU case range extension.
|
|
|
|
SourceLocation DotDotDotLoc;
|
2010-08-24 14:29:42 +08:00
|
|
|
ExprResult RHS;
|
2009-03-04 12:23:07 +08:00
|
|
|
if (Tok.is(tok::ellipsis)) {
|
|
|
|
Diag(Tok, diag::ext_gnu_case_range);
|
|
|
|
DotDotDotLoc = ConsumeToken();
|
2008-12-09 21:15:23 +08:00
|
|
|
|
2009-03-04 12:23:07 +08:00
|
|
|
RHS = ParseConstantExpression();
|
|
|
|
if (RHS.isInvalid()) {
|
|
|
|
SkipUntil(tok::colon);
|
|
|
|
return StmtError();
|
|
|
|
}
|
|
|
|
}
|
2009-12-10 08:38:54 +08:00
|
|
|
|
|
|
|
ColonProtection.restore();
|
2008-12-09 21:15:23 +08:00
|
|
|
|
2011-01-22 17:28:32 +08:00
|
|
|
if (Tok.is(tok::colon)) {
|
|
|
|
ColonLoc = ConsumeToken();
|
|
|
|
|
|
|
|
// Treat "case blah;" as a typo for "case blah:".
|
|
|
|
} else if (Tok.is(tok::semi)) {
|
|
|
|
ColonLoc = ConsumeToken();
|
|
|
|
Diag(ColonLoc, diag::err_expected_colon_after) << "'case'"
|
|
|
|
<< FixItHint::CreateReplacement(ColonLoc, ":");
|
|
|
|
} else {
|
2010-12-24 06:56:40 +08:00
|
|
|
SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
|
|
|
|
Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'"
|
|
|
|
<< FixItHint::CreateInsertion(ExpectedLoc, ":");
|
|
|
|
ColonLoc = ExpectedLoc;
|
2006-08-14 06:09:58 +08:00
|
|
|
}
|
2010-12-24 06:56:40 +08:00
|
|
|
|
2010-08-24 14:29:42 +08:00
|
|
|
StmtResult Case =
|
2010-08-24 07:25:46 +08:00
|
|
|
Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
|
|
|
|
RHS.get(), ColonLoc);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-04 12:23:07 +08:00
|
|
|
// If we had a sema error parsing this case, then just ignore it and
|
|
|
|
// continue parsing the sub-stmt.
|
|
|
|
if (Case.isInvalid()) {
|
|
|
|
if (TopLevelCase.isInvalid()) // No parsed case stmts.
|
|
|
|
return ParseStatement();
|
|
|
|
// Otherwise, just don't add it as a nested case.
|
|
|
|
} else {
|
|
|
|
// If this is the first case statement we parsed, it becomes TopLevelCase.
|
|
|
|
// Otherwise we link it into the current chain.
|
2010-08-23 14:44:23 +08:00
|
|
|
Stmt *NextDeepest = Case.get();
|
2009-03-04 12:23:07 +08:00
|
|
|
if (TopLevelCase.isInvalid())
|
|
|
|
TopLevelCase = move(Case);
|
|
|
|
else
|
2010-08-24 07:25:46 +08:00
|
|
|
Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
|
2009-03-04 12:23:07 +08:00
|
|
|
DeepestParsedCaseStmt = NextDeepest;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-04 12:23:07 +08:00
|
|
|
// Handle all case statements.
|
|
|
|
} while (Tok.is(tok::kw_case));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-04 12:23:07 +08:00
|
|
|
assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-04 12:23:07 +08:00
|
|
|
// If we found a non-case statement, start by parsing it.
|
2010-08-24 14:29:42 +08:00
|
|
|
StmtResult SubStmt;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-04 12:23:07 +08:00
|
|
|
if (Tok.isNot(tok::r_brace)) {
|
|
|
|
SubStmt = ParseStatement();
|
|
|
|
} else {
|
|
|
|
// Nicely diagnose the common error "switch (X) { case 4: }", which is
|
|
|
|
// not valid.
|
2011-06-14 23:24:38 +08:00
|
|
|
SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
|
|
|
|
Diag(AfterColonLoc, diag::err_label_end_of_compound_statement);
|
2009-03-04 12:23:07 +08:00
|
|
|
SubStmt = true;
|
2006-10-16 13:52:41 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-04 12:23:07 +08:00
|
|
|
// Broken sub-stmt shouldn't prevent forming the case statement properly.
|
2008-12-09 21:15:23 +08:00
|
|
|
if (SubStmt.isInvalid())
|
2009-03-04 12:23:07 +08:00
|
|
|
SubStmt = Actions.ActOnNullStmt(SourceLocation());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-04 12:23:07 +08:00
|
|
|
// Install the body into the most deeply-nested case.
|
2010-08-24 07:25:46 +08:00
|
|
|
Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
|
2008-12-12 03:30:53 +08:00
|
|
|
|
2009-03-04 12:23:07 +08:00
|
|
|
// Return the top level parsed statement tree.
|
2009-03-05 02:24:58 +08:00
|
|
|
return move(TopLevelCase);
|
2006-08-10 13:59:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseDefaultStatement
|
|
|
|
/// labeled-statement:
|
|
|
|
/// 'default' ':' statement
|
|
|
|
/// Note that this does not parse the 'statement' at the end.
|
|
|
|
///
|
2010-12-24 10:08:15 +08:00
|
|
|
StmtResult Parser::ParseDefaultStatement(ParsedAttributes &attrs) {
|
2009-11-21 16:43:09 +08:00
|
|
|
//FIXME: Use attributes?
|
2010-02-11 10:19:13 +08:00
|
|
|
|
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
|
|
|
|
2010-12-24 06:56:40 +08:00
|
|
|
SourceLocation ColonLoc;
|
2011-01-22 17:28:32 +08:00
|
|
|
if (Tok.is(tok::colon)) {
|
|
|
|
ColonLoc = ConsumeToken();
|
|
|
|
|
|
|
|
// Treat "default;" as a typo for "default:".
|
|
|
|
} else if (Tok.is(tok::semi)) {
|
|
|
|
ColonLoc = ConsumeToken();
|
|
|
|
Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
|
|
|
|
<< FixItHint::CreateReplacement(ColonLoc, ":");
|
|
|
|
} else {
|
2010-12-24 06:56:40 +08:00
|
|
|
SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
|
|
|
|
Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
|
|
|
|
<< FixItHint::CreateInsertion(ExpectedLoc, ":");
|
|
|
|
ColonLoc = ExpectedLoc;
|
2006-10-16 13:52:41 +08:00
|
|
|
}
|
2010-12-24 06:56:40 +08:00
|
|
|
|
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)) {
|
2011-06-14 23:24:38 +08:00
|
|
|
SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
|
|
|
|
Diag(AfterColonLoc, diag::err_label_end_of_compound_statement);
|
2008-12-12 03:30:53 +08:00
|
|
|
return StmtError();
|
2006-08-10 13:59:48 +08:00
|
|
|
}
|
2006-10-16 13:52:41 +08:00
|
|
|
|
2010-08-24 14:29:42 +08:00
|
|
|
StmtResult SubStmt(ParseStatement());
|
2008-12-09 21:15:23 +08:00
|
|
|
if (SubStmt.isInvalid())
|
2008-12-12 03:30:53 +08:00
|
|
|
return StmtError();
|
|
|
|
|
2008-12-29 00:13:43 +08:00
|
|
|
return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
|
2010-08-24 07:25:46 +08:00
|
|
|
SubStmt.get(), getCurScope());
|
2006-08-10 13:59:48 +08:00
|
|
|
}
|
|
|
|
|
2011-07-07 06:04:06 +08:00
|
|
|
StmtResult Parser::ParseCompoundStatement(ParsedAttributes &Attr,
|
|
|
|
bool isStmtExpr) {
|
|
|
|
return ParseCompoundStatement(Attr, isStmtExpr, Scope::DeclScope);
|
|
|
|
}
|
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
|
|
|
///
|
2010-12-24 10:08:15 +08:00
|
|
|
StmtResult Parser::ParseCompoundStatement(ParsedAttributes &attrs,
|
2011-07-07 06:04:06 +08:00
|
|
|
bool isStmtExpr,
|
|
|
|
unsigned ScopeFlags) {
|
2009-11-21 16:43:09 +08:00
|
|
|
//FIXME: Use attributes?
|
2010-02-11 10:19:13 +08:00
|
|
|
|
2007-10-10 01:41:39 +08:00
|
|
|
assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
|
2008-12-12 03:30:53 +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.
|
2011-07-07 06:04:06 +08:00
|
|
|
ParseScope CompoundScope(this, ScopeFlags);
|
2007-01-21 14:52:16 +08:00
|
|
|
|
|
|
|
// Parse the statements in the body.
|
2008-12-12 03:30:53 +08:00
|
|
|
return ParseCompoundStatementBody(isStmtExpr);
|
2007-01-21 14:52:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 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.
|
2010-08-24 14:29:42 +08:00
|
|
|
StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
|
2009-09-09 23:08:12 +08:00
|
|
|
PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
|
2009-03-05 08:00:31 +08:00
|
|
|
Tok.getLocation(),
|
|
|
|
"in compound statement ('{}')");
|
2010-09-15 22:51:05 +08:00
|
|
|
InMessageExpressionRAIIObject InMessage(*this, false);
|
|
|
|
|
2007-01-21 14:52:16 +08:00
|
|
|
SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
|
|
|
|
|
2010-09-29 04:42:35 +08:00
|
|
|
StmtVector Stmts(Actions);
|
2011-01-18 02:58:44 +08:00
|
|
|
|
2011-02-18 10:08:43 +08:00
|
|
|
// "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
|
|
|
|
// only allowed at the start of a compound stmt regardless of the language.
|
|
|
|
while (Tok.is(tok::kw___label__)) {
|
|
|
|
SourceLocation LabelLoc = ConsumeToken();
|
|
|
|
Diag(LabelLoc, diag::ext_gnu_local_label);
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<Decl *, 8> DeclsInGroup;
|
2011-02-18 10:08:43 +08:00
|
|
|
while (1) {
|
|
|
|
if (Tok.isNot(tok::identifier)) {
|
|
|
|
Diag(Tok, diag::err_expected_ident);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
IdentifierInfo *II = Tok.getIdentifierInfo();
|
|
|
|
SourceLocation IdLoc = ConsumeToken();
|
2011-03-06 02:21:20 +08:00
|
|
|
DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
|
2011-02-18 10:08:43 +08:00
|
|
|
|
|
|
|
if (!Tok.is(tok::comma))
|
|
|
|
break;
|
|
|
|
ConsumeToken();
|
|
|
|
}
|
|
|
|
|
2011-03-24 19:26:52 +08:00
|
|
|
DeclSpec DS(AttrFactory);
|
2011-02-18 10:08:43 +08:00
|
|
|
DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
|
|
|
|
DeclsInGroup.data(), DeclsInGroup.size());
|
|
|
|
StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
|
|
|
|
|
|
|
|
ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
|
|
|
|
if (R.isUsable())
|
|
|
|
Stmts.push_back(R.release());
|
|
|
|
}
|
|
|
|
|
|
|
|
while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
|
2011-01-18 02:58:44 +08:00
|
|
|
if (Tok.is(tok::annot_pragma_unused)) {
|
|
|
|
HandlePragmaUnused();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-09-18 01:15:52 +08:00
|
|
|
if (getLang().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
|
2011-05-07 04:48:22 +08:00
|
|
|
Tok.is(tok::kw___if_not_exists))) {
|
|
|
|
ParseMicrosoftIfExistsStatement(Stmts);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-08-24 14:29:42 +08:00
|
|
|
StmtResult R;
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.isNot(tok::kw___extension__)) {
|
2010-09-29 04:42:35 +08:00
|
|
|
R = ParseStatementOrDeclaration(Stmts, false);
|
2007-08-27 09:01:57 +08:00
|
|
|
} 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.
|
2009-01-27 16:43:38 +08:00
|
|
|
// FIXME: This loses extension expressions in the AST!
|
2007-08-27 09:01:57 +08:00
|
|
|
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-10-20 14:51:33 +08:00
|
|
|
|
2011-03-24 19:26:52 +08:00
|
|
|
ParsedAttributesWithRange attrs(AttrFactory);
|
2010-12-24 10:08:15 +08:00
|
|
|
MaybeParseCXX0XAttributes(attrs);
|
2009-11-21 16:43:09 +08:00
|
|
|
|
2007-08-27 09:01:57 +08:00
|
|
|
// If this is the start of a declaration, parse it as such.
|
2008-10-05 08:06:24 +08:00
|
|
|
if (isDeclarationStatement()) {
|
2009-05-17 07:40:44 +08:00
|
|
|
// __extension__ silences extension warnings in the subdeclaration.
|
fix a FIXME, providing accurate source range info for DeclStmt's. The end
of the range is now the ';' location. For something like this:
$ cat t2.c
#define bool int
void f(int x, int y) {
bool b = !x && y;
}
We used to produce:
$ clang-cc t2.c -ast-dump
typedef char *__builtin_va_list;
void f(int x, int y)
(CompoundStmt 0x2201f10 <t2.c:3:22, line:5:1>
(DeclStmt 0x2201ef0 <line:2:14> <----
0x2201a20 "int b =
(BinaryOperator 0x2201ed0 <line:4:10, col:16> 'int' '&&'
(UnaryOperator 0x2201e90 <col:10, col:11> 'int' prefix '!'
(DeclRefExpr 0x2201c90 <col:11> 'int' ParmVar='x' 0x2201a50))
(DeclRefExpr 0x2201eb0 <col:16> 'int' ParmVar='y' 0x2201e10))")
Now we produce:
$ clang-cc t2.c -ast-dump
typedef char *__builtin_va_list;
void f(int x, int y)
(CompoundStmt 0x2201f10 <t2.c:3:22, line:5:1>
(DeclStmt 0x2201ef0 <line:2:14, line:4:17> <------
0x2201a20 "int b =
(BinaryOperator 0x2201ed0 <col:10, col:16> 'int' '&&'
(UnaryOperator 0x2201e90 <col:10, col:11> 'int' prefix '!'
(DeclRefExpr 0x2201c90 <col:11> 'int' ParmVar='x' 0x2201a50))
(DeclRefExpr 0x2201eb0 <col:16> 'int' ParmVar='y' 0x2201e10))")
llvm-svn: 68288
2009-04-02 12:16:50 +08:00
|
|
|
// FIXME: Save the __extension__ on the decl as a node somehow?
|
2009-05-17 07:40:44 +08:00
|
|
|
ExtensionRAIIObject O(Diags);
|
|
|
|
|
fix a FIXME, providing accurate source range info for DeclStmt's. The end
of the range is now the ';' location. For something like this:
$ cat t2.c
#define bool int
void f(int x, int y) {
bool b = !x && y;
}
We used to produce:
$ clang-cc t2.c -ast-dump
typedef char *__builtin_va_list;
void f(int x, int y)
(CompoundStmt 0x2201f10 <t2.c:3:22, line:5:1>
(DeclStmt 0x2201ef0 <line:2:14> <----
0x2201a20 "int b =
(BinaryOperator 0x2201ed0 <line:4:10, col:16> 'int' '&&'
(UnaryOperator 0x2201e90 <col:10, col:11> 'int' prefix '!'
(DeclRefExpr 0x2201c90 <col:11> 'int' ParmVar='x' 0x2201a50))
(DeclRefExpr 0x2201eb0 <col:16> 'int' ParmVar='y' 0x2201e10))")
Now we produce:
$ clang-cc t2.c -ast-dump
typedef char *__builtin_va_list;
void f(int x, int y)
(CompoundStmt 0x2201f10 <t2.c:3:22, line:5:1>
(DeclStmt 0x2201ef0 <line:2:14, line:4:17> <------
0x2201a20 "int b =
(BinaryOperator 0x2201ed0 <col:10, col:16> 'int' '&&'
(UnaryOperator 0x2201e90 <col:10, col:11> 'int' prefix '!'
(DeclRefExpr 0x2201c90 <col:11> 'int' ParmVar='x' 0x2201a50))
(DeclRefExpr 0x2201eb0 <col:16> 'int' ParmVar='y' 0x2201e10))")
llvm-svn: 68288
2009-04-02 12:16:50 +08:00
|
|
|
SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
|
2010-09-29 04:42:35 +08:00
|
|
|
DeclGroupPtrTy Res = ParseDeclaration(Stmts,
|
|
|
|
Declarator::BlockContext, DeclEnd,
|
2010-12-24 10:08:15 +08:00
|
|
|
attrs);
|
fix a FIXME, providing accurate source range info for DeclStmt's. The end
of the range is now the ';' location. For something like this:
$ cat t2.c
#define bool int
void f(int x, int y) {
bool b = !x && y;
}
We used to produce:
$ clang-cc t2.c -ast-dump
typedef char *__builtin_va_list;
void f(int x, int y)
(CompoundStmt 0x2201f10 <t2.c:3:22, line:5:1>
(DeclStmt 0x2201ef0 <line:2:14> <----
0x2201a20 "int b =
(BinaryOperator 0x2201ed0 <line:4:10, col:16> 'int' '&&'
(UnaryOperator 0x2201e90 <col:10, col:11> 'int' prefix '!'
(DeclRefExpr 0x2201c90 <col:11> 'int' ParmVar='x' 0x2201a50))
(DeclRefExpr 0x2201eb0 <col:16> 'int' ParmVar='y' 0x2201e10))")
Now we produce:
$ clang-cc t2.c -ast-dump
typedef char *__builtin_va_list;
void f(int x, int y)
(CompoundStmt 0x2201f10 <t2.c:3:22, line:5:1>
(DeclStmt 0x2201ef0 <line:2:14, line:4:17> <------
0x2201a20 "int b =
(BinaryOperator 0x2201ed0 <col:10, col:16> 'int' '&&'
(UnaryOperator 0x2201e90 <col:10, col:11> 'int' prefix '!'
(DeclRefExpr 0x2201c90 <col:11> 'int' ParmVar='x' 0x2201a50))
(DeclRefExpr 0x2201eb0 <col:16> 'int' ParmVar='y' 0x2201e10))")
llvm-svn: 68288
2009-04-02 12:16:50 +08:00
|
|
|
R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
|
2007-08-27 09:01:57 +08:00
|
|
|
} else {
|
2009-01-27 16:43:38 +08:00
|
|
|
// Otherwise this was a unary __extension__ marker.
|
2010-08-24 14:29:42 +08:00
|
|
|
ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
|
2008-03-13 14:32:11 +08:00
|
|
|
|
2008-12-09 21:15:23 +08:00
|
|
|
if (Res.isInvalid()) {
|
2007-08-27 09:01:57 +08:00
|
|
|
SkipUntil(tok::semi);
|
|
|
|
continue;
|
|
|
|
}
|
2009-01-19 02:03:53 +08:00
|
|
|
|
2009-11-21 16:43:09 +08:00
|
|
|
// FIXME: Use attributes?
|
2008-10-20 14:51:33 +08:00
|
|
|
// Eat the semicolon at the end of stmt and convert the expr into a
|
|
|
|
// statement.
|
2010-09-07 23:23:11 +08:00
|
|
|
ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
|
2010-08-24 07:25:46 +08:00
|
|
|
R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
|
2007-08-27 09:01:57 +08:00
|
|
|
}
|
|
|
|
}
|
2008-12-12 03:30:53 +08:00
|
|
|
|
2008-12-09 21:15:23 +08:00
|
|
|
if (R.isUsable())
|
2008-12-10 08:02:53 +08:00
|
|
|
Stmts.push_back(R.release());
|
2006-10-16 13:52:41 +08:00
|
|
|
}
|
2008-12-12 03:30:53 +08:00
|
|
|
|
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);
|
2010-09-01 23:49:26 +08:00
|
|
|
Diag(LBraceLoc, diag::note_matching) << "{";
|
2008-12-12 03:30:53 +08:00
|
|
|
return StmtError();
|
2006-10-16 13:52:41 +08:00
|
|
|
}
|
2008-12-12 03:30:53 +08:00
|
|
|
|
2006-10-16 14:12:55 +08:00
|
|
|
SourceLocation RBraceLoc = ConsumeBrace();
|
2009-01-19 02:03:53 +08:00
|
|
|
return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
|
2008-12-21 20:04:03 +08:00
|
|
|
isStmtExpr);
|
2006-08-09 13:47:47 +08:00
|
|
|
}
|
2006-08-10 12:23:57 +08:00
|
|
|
|
2008-12-12 14:31:07 +08:00
|
|
|
/// ParseParenExprOrCondition:
|
|
|
|
/// [C ] '(' expression ')'
|
2008-12-12 14:35:28 +08:00
|
|
|
/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
|
2008-12-12 14:31:07 +08:00
|
|
|
///
|
|
|
|
/// This function parses and performs error recovery on the specified condition
|
|
|
|
/// or expression (depending on whether we're in C++ or C mode). This function
|
|
|
|
/// goes out of its way to recover well. It returns true if there was a parser
|
|
|
|
/// error (the right paren couldn't be found), which indicates that the caller
|
|
|
|
/// should try to recover harder. It returns false if the condition is
|
|
|
|
/// successfully parsed. Note that a successful parse can still have semantic
|
|
|
|
/// errors in the condition.
|
2010-08-24 14:29:42 +08:00
|
|
|
bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *&DeclResult,
|
2010-05-07 01:25:47 +08:00
|
|
|
SourceLocation Loc,
|
2010-11-20 10:04:01 +08:00
|
|
|
bool ConvertToBoolean) {
|
2008-12-12 14:31:07 +08:00
|
|
|
SourceLocation LParenLoc = ConsumeParen();
|
2009-11-25 08:27:52 +08:00
|
|
|
if (getLang().CPlusPlus)
|
2011-01-18 10:00:16 +08:00
|
|
|
ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
|
2009-11-25 08:27:52 +08:00
|
|
|
else {
|
|
|
|
ExprResult = ParseExpression();
|
2010-08-21 17:40:31 +08:00
|
|
|
DeclResult = 0;
|
2010-05-07 01:25:47 +08:00
|
|
|
|
|
|
|
// If required, convert to a boolean value.
|
|
|
|
if (!ExprResult.isInvalid() && ConvertToBoolean)
|
|
|
|
ExprResult
|
2010-08-24 07:25:46 +08:00
|
|
|
= Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
|
2009-11-25 08:27:52 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-12 14:31:07 +08:00
|
|
|
// If the parser was confused by the condition and we don't have a ')', try to
|
|
|
|
// recover by skipping ahead to a semi and bailing out. If condexp is
|
|
|
|
// semantically invalid but we have well formed code, keep going.
|
2010-08-21 17:40:31 +08:00
|
|
|
if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
|
2008-12-12 14:31:07 +08:00
|
|
|
SkipUntil(tok::semi);
|
|
|
|
// Skipping may have stopped if it found the containing ')'. If so, we can
|
|
|
|
// continue parsing the if statement.
|
|
|
|
if (Tok.isNot(tok::r_paren))
|
|
|
|
return true;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-12 14:31:07 +08:00
|
|
|
// Otherwise the condition is valid or the rparen is present.
|
2009-11-25 08:27:52 +08:00
|
|
|
MatchRHSPunctuation(tok::r_paren, LParenLoc);
|
2008-12-12 14:31:07 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
///
|
2010-12-24 10:08:15 +08:00
|
|
|
StmtResult Parser::ParseIfStatement(ParsedAttributes &attrs) {
|
2009-11-21 16:43:09 +08:00
|
|
|
// FIXME: Use attributes?
|
2010-02-11 10:19:13 +08:00
|
|
|
|
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)) {
|
2008-11-18 15:48:38 +08:00
|
|
|
Diag(Tok, diag::err_expected_lparen_after) << "if";
|
2006-08-10 12:23:57 +08:00
|
|
|
SkipUntil(tok::semi);
|
2008-12-12 03:30:53 +08:00
|
|
|
return StmtError();
|
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.
|
2008-09-12 07:08:39 +08: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 11:06:46 +08:00
|
|
|
//
|
2008-12-10 14:34:36 +08:00
|
|
|
ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
|
2007-08-27 07:08:06 +08:00
|
|
|
|
2006-08-10 12:23:57 +08:00
|
|
|
// Parse the condition.
|
2010-08-24 14:29:42 +08:00
|
|
|
ExprResult CondExp;
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *CondVar = 0;
|
2010-11-20 10:04:01 +08:00
|
|
|
if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
|
2008-12-12 14:31:07 +08:00
|
|
|
return StmtError();
|
2008-12-12 14:19:11 +08:00
|
|
|
|
2010-08-24 07:25:46 +08:00
|
|
|
FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
|
2009-09-09 23:08:12 +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.
|
|
|
|
//
|
2009-09-09 23:08:12 +08:00
|
|
|
ParseScope InnerScope(this, Scope::DeclScope,
|
2008-12-10 14:34:36 +08:00
|
|
|
C99orCXX && Tok.isNot(tok::l_brace));
|
2008-09-10 04:38:47 +08:00
|
|
|
|
2007-10-29 13:08:52 +08:00
|
|
|
// Read the 'then' stmt.
|
|
|
|
SourceLocation ThenStmtLoc = Tok.getLocation();
|
2010-08-24 14:29:42 +08:00
|
|
|
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.
|
2008-12-10 14:34:36 +08:00
|
|
|
InnerScope.Exit();
|
2008-12-12 03:30:53 +08:00
|
|
|
|
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;
|
2010-08-24 14:29:42 +08:00
|
|
|
StmtResult ElseStmt;
|
2008-12-09 21:15:23 +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();
|
2010-04-12 14:12:50 +08:00
|
|
|
ElseStmtLoc = Tok.getLocation();
|
2008-12-12 03:30:53 +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.
|
|
|
|
//
|
2008-12-12 03:30:53 +08:00
|
|
|
ParseScope InnerScope(this, Scope::DeclScope,
|
2008-12-10 14:34:36 +08:00
|
|
|
C99orCXX && Tok.isNot(tok::l_brace));
|
2008-12-09 21:15:23 +08:00
|
|
|
|
2006-10-16 13:52:41 +08:00
|
|
|
ElseStmt = ParseStatement();
|
2010-04-12 14:12:50 +08:00
|
|
|
|
2007-08-22 13:16:28 +08:00
|
|
|
// Pop the 'else' scope if needed.
|
2008-12-10 14:34:36 +08:00
|
|
|
InnerScope.Exit();
|
2011-07-30 16:36:53 +08:00
|
|
|
} else if (Tok.is(tok::code_completion)) {
|
|
|
|
Actions.CodeCompleteAfterIf(getCurScope());
|
2011-09-04 11:32:15 +08:00
|
|
|
cutOffParsing();
|
|
|
|
return StmtError();
|
2006-08-10 12:23:57 +08:00
|
|
|
}
|
2008-12-12 03:30:53 +08:00
|
|
|
|
2008-12-10 14:34:36 +08:00
|
|
|
IfScope.Exit();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-12 14:19:11 +08:00
|
|
|
// If the condition was invalid, discard the if statement. We could recover
|
|
|
|
// better by replacing it with a valid expr, but don't do that yet.
|
2010-08-21 17:40:31 +08:00
|
|
|
if (CondExp.isInvalid() && !CondVar)
|
2008-12-12 14:19:11 +08:00
|
|
|
return StmtError();
|
2007-08-27 07:08:06 +08:00
|
|
|
|
2007-10-29 13:08:52 +08:00
|
|
|
// If the then or else stmt is invalid and the other is valid (and present),
|
2009-09-09 23:08:12 +08:00
|
|
|
// make turn the invalid one into a null stmt to avoid dropping the other
|
2007-10-29 13:08:52 +08:00
|
|
|
// part. If both are invalid, return error.
|
2008-12-09 21:15:23 +08:00
|
|
|
if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
|
|
|
|
(ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
|
|
|
|
(ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
|
2008-11-26 06:21:31 +08:00
|
|
|
// Both invalid, or one is invalid and other is non-present: return error.
|
2008-12-12 03:30:53 +08:00
|
|
|
return StmtError();
|
2007-10-29 13:08:52 +08:00
|
|
|
}
|
2008-12-09 21:15:23 +08:00
|
|
|
|
2007-10-29 13:08:52 +08:00
|
|
|
// Now if either are invalid, replace with a ';'.
|
2008-12-09 21:15:23 +08:00
|
|
|
if (ThenStmt.isInvalid())
|
2007-10-29 13:08:52 +08:00
|
|
|
ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
|
2008-12-09 21:15:23 +08:00
|
|
|
if (ElseStmt.isInvalid())
|
2007-10-29 13:08:52 +08:00
|
|
|
ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
|
2008-12-09 21:15:23 +08:00
|
|
|
|
2010-08-24 07:25:46 +08:00
|
|
|
return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
|
2010-11-20 10:04:01 +08:00
|
|
|
ElseLoc, ElseStmt.get());
|
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
|
2010-12-24 10:08:15 +08:00
|
|
|
StmtResult Parser::ParseSwitchStatement(ParsedAttributes &attrs) {
|
2009-11-21 16:43:09 +08:00
|
|
|
// FIXME: Use attributes?
|
2010-02-11 10:19:13 +08:00
|
|
|
|
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)) {
|
2008-11-18 15:48:38 +08:00
|
|
|
Diag(Tok, diag::err_expected_lparen_after) << "switch";
|
2006-08-10 12:59:57 +08:00
|
|
|
SkipUntil(tok::semi);
|
2008-12-12 03:48:14 +08:00
|
|
|
return StmtError();
|
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.
|
2008-09-12 07:08:39 +08: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 11:06:46 +08:00
|
|
|
//
|
2011-04-22 05:44:26 +08:00
|
|
|
unsigned ScopeFlags = Scope::BreakScope | Scope::SwitchScope;
|
2008-12-12 14:31:07 +08:00
|
|
|
if (C99orCXX)
|
|
|
|
ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
|
2008-12-10 14:34:36 +08:00
|
|
|
ParseScope SwitchScope(this, ScopeFlags);
|
2006-11-06 07:47:55 +08:00
|
|
|
|
2006-08-10 12:59:57 +08:00
|
|
|
// Parse the condition.
|
2010-08-24 14:29:42 +08:00
|
|
|
ExprResult Cond;
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *CondVar = 0;
|
2010-05-07 01:25:47 +08:00
|
|
|
if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
|
2008-12-12 03:48:14 +08:00
|
|
|
return StmtError();
|
2008-12-18 06:19:57 +08:00
|
|
|
|
2010-08-24 14:29:42 +08:00
|
|
|
StmtResult Switch
|
2010-08-24 07:25:46 +08:00
|
|
|
= Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
|
2010-05-07 01:25:47 +08:00
|
|
|
|
|
|
|
if (Switch.isInvalid()) {
|
|
|
|
// Skip the switch body.
|
|
|
|
// FIXME: This is not optimal recovery, but parsing the body is more
|
|
|
|
// dangerous due to the presence of case and default statements, which
|
|
|
|
// will have no place to connect back with the switch.
|
2010-05-21 07:20:59 +08:00
|
|
|
if (Tok.is(tok::l_brace)) {
|
|
|
|
ConsumeBrace();
|
|
|
|
SkipUntil(tok::r_brace, false, false);
|
|
|
|
} else
|
2010-05-07 01:25:47 +08:00
|
|
|
SkipUntil(tok::semi);
|
|
|
|
return move(Switch);
|
|
|
|
}
|
2009-11-25 08:27:52 +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++.
|
|
|
|
//
|
2009-09-09 23:08:12 +08:00
|
|
|
ParseScope InnerScope(this, Scope::DeclScope,
|
2008-12-10 14:34:36 +08:00
|
|
|
C99orCXX && Tok.isNot(tok::l_brace));
|
2008-12-12 03:30:53 +08:00
|
|
|
|
2006-08-10 12:59:57 +08:00
|
|
|
// Read the body statement.
|
2010-08-24 14:29:42 +08:00
|
|
|
StmtResult Body(ParseStatement());
|
2006-11-06 07:47:55 +08:00
|
|
|
|
2010-01-24 09:50:29 +08:00
|
|
|
// Pop the scopes.
|
2008-12-10 14:34:36 +08:00
|
|
|
InnerScope.Exit();
|
|
|
|
SwitchScope.Exit();
|
2008-12-12 03:30:53 +08:00
|
|
|
|
2010-01-24 09:50:29 +08:00
|
|
|
if (Body.isInvalid())
|
|
|
|
// FIXME: Remove the case statement list from the Switch statement.
|
|
|
|
Body = Actions.ActOnNullStmt(Tok.getLocation());
|
|
|
|
|
2010-08-24 07:25:46 +08:00
|
|
|
return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
|
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
|
2010-12-24 10:08:15 +08:00
|
|
|
StmtResult Parser::ParseWhileStatement(ParsedAttributes &attrs) {
|
2009-11-21 16:43:09 +08:00
|
|
|
// FIXME: Use attributes?
|
2010-02-11 10:19:13 +08:00
|
|
|
|
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'.
|
2008-12-12 03:48:14 +08:00
|
|
|
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
2008-11-18 15:48:38 +08:00
|
|
|
Diag(Tok, diag::err_expected_lparen_after) << "while";
|
2006-08-10 12:59:57 +08:00
|
|
|
SkipUntil(tok::semi);
|
2008-12-12 03:48:14 +08:00
|
|
|
return StmtError();
|
2006-08-10 12:59:57 +08:00
|
|
|
}
|
2008-12-12 03:48:14 +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.
|
2008-09-12 07:08:39 +08: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 11:06:46 +08:00
|
|
|
//
|
2008-12-10 14:34:36 +08:00
|
|
|
unsigned ScopeFlags;
|
2008-09-11 11:06:46 +08:00
|
|
|
if (C99orCXX)
|
2008-12-10 14:34:36 +08:00
|
|
|
ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
|
|
|
|
Scope::DeclScope | Scope::ControlScope;
|
2007-08-27 07:08:06 +08:00
|
|
|
else
|
2008-12-10 14:34:36 +08:00
|
|
|
ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
|
|
|
|
ParseScope WhileScope(this, ScopeFlags);
|
2006-11-06 07:47:55 +08:00
|
|
|
|
2006-08-10 12:59:57 +08:00
|
|
|
// Parse the condition.
|
2010-08-24 14:29:42 +08:00
|
|
|
ExprResult Cond;
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *CondVar = 0;
|
2010-05-07 01:25:47 +08:00
|
|
|
if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
|
2008-12-12 14:31:07 +08:00
|
|
|
return StmtError();
|
2008-12-09 21:15:23 +08:00
|
|
|
|
2010-08-24 07:25:46 +08:00
|
|
|
FullExprArg FullCond(Actions.MakeFullExpr(Cond.get()));
|
2009-09-09 23:08:12 +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++.
|
|
|
|
//
|
2009-09-09 23:08:12 +08:00
|
|
|
ParseScope InnerScope(this, Scope::DeclScope,
|
2008-12-10 14:34:36 +08:00
|
|
|
C99orCXX && Tok.isNot(tok::l_brace));
|
2008-12-12 03:48:14 +08:00
|
|
|
|
2006-08-10 12:59:57 +08:00
|
|
|
// Read the body statement.
|
2010-08-24 14:29:42 +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.
|
2008-12-10 14:34:36 +08:00
|
|
|
InnerScope.Exit();
|
|
|
|
WhileScope.Exit();
|
2008-12-12 03:48:14 +08:00
|
|
|
|
2010-08-21 17:40:31 +08:00
|
|
|
if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
|
2008-12-12 03:48:14 +08:00
|
|
|
return StmtError();
|
|
|
|
|
2010-08-24 07:25:46 +08:00
|
|
|
return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
|
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 ';'.
|
2010-12-24 10:08:15 +08:00
|
|
|
StmtResult Parser::ParseDoStatement(ParsedAttributes &attrs) {
|
2009-11-21 16:43:09 +08:00
|
|
|
// FIXME: Use attributes?
|
2010-02-11 10:19:13 +08:00
|
|
|
|
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'.
|
2008-12-12 03:48:14 +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.
|
2008-12-10 14:34:36 +08:00
|
|
|
unsigned ScopeFlags;
|
2007-08-27 07:08:06 +08:00
|
|
|
if (getLang().C99)
|
2008-12-10 14:34:36 +08:00
|
|
|
ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
|
2007-08-27 07:08:06 +08:00
|
|
|
else
|
2008-12-10 14:34:36 +08:00
|
|
|
ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
|
2008-12-12 03:48:14 +08:00
|
|
|
|
2008-12-10 14:34:36 +08:00
|
|
|
ParseScope DoScope(this, ScopeFlags);
|
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.
|
|
|
|
//
|
2008-12-10 14:34:36 +08:00
|
|
|
ParseScope InnerScope(this, Scope::DeclScope,
|
2009-09-09 23:08:12 +08:00
|
|
|
(getLang().C99 || getLang().CPlusPlus) &&
|
2008-12-10 14:34:36 +08:00
|
|
|
Tok.isNot(tok::l_brace));
|
2008-12-12 03:48:14 +08:00
|
|
|
|
2006-08-10 12:59:57 +08:00
|
|
|
// Read the body statement.
|
2010-08-24 14:29:42 +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.
|
2008-12-10 14:34:36 +08:00
|
|
|
InnerScope.Exit();
|
2007-08-22 13:28:50 +08:00
|
|
|
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.isNot(tok::kw_while)) {
|
2008-12-09 21:15:23 +08:00
|
|
|
if (!Body.isInvalid()) {
|
2008-11-14 02:52:53 +08:00
|
|
|
Diag(Tok, diag::err_expected_while);
|
2008-11-24 07:17:07 +08:00
|
|
|
Diag(DoLoc, diag::note_matching) << "do";
|
2008-11-14 02:52:53 +08:00
|
|
|
SkipUntil(tok::semi, false, true);
|
|
|
|
}
|
2008-12-12 03:48:14 +08:00
|
|
|
return StmtError();
|
2006-08-10 12:59:57 +08:00
|
|
|
}
|
2006-10-16 14:06:51 +08:00
|
|
|
SourceLocation WhileLoc = ConsumeToken();
|
2008-12-12 03:48:14 +08:00
|
|
|
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
2008-11-18 15:48:38 +08:00
|
|
|
Diag(Tok, diag::err_expected_lparen_after) << "do/while";
|
2008-11-14 02:52:53 +08:00
|
|
|
SkipUntil(tok::semi, false, true);
|
2008-12-12 03:48:14 +08:00
|
|
|
return StmtError();
|
2006-08-10 12:59:57 +08:00
|
|
|
}
|
2008-12-12 03:48:14 +08:00
|
|
|
|
2008-12-12 14:35:28 +08:00
|
|
|
// Parse the parenthesized condition.
|
2009-11-25 05:34:32 +08:00
|
|
|
SourceLocation LPLoc = ConsumeParen();
|
2010-08-24 14:29:42 +08:00
|
|
|
ExprResult Cond = ParseExpression();
|
2009-11-25 05:34:32 +08:00
|
|
|
SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LPLoc);
|
2008-12-10 14:34:36 +08:00
|
|
|
DoScope.Exit();
|
2008-12-09 21:15:23 +08:00
|
|
|
|
2008-12-12 03:48:14 +08:00
|
|
|
if (Cond.isInvalid() || Body.isInvalid())
|
|
|
|
return StmtError();
|
2008-12-09 21:15:23 +08:00
|
|
|
|
2010-08-24 07:25:46 +08:00
|
|
|
return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, LPLoc,
|
|
|
|
Cond.get(), RPLoc);
|
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
|
2011-04-15 06:09:26 +08:00
|
|
|
/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) 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
|
|
|
|
///
|
2011-04-15 06:09:26 +08:00
|
|
|
/// [C++0x] for-range-declaration:
|
|
|
|
/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
|
|
|
|
/// [C++0x] for-range-initializer:
|
|
|
|
/// [C++0x] expression
|
|
|
|
/// [C++0x] braced-init-list [TODO]
|
2010-12-24 10:08:15 +08:00
|
|
|
StmtResult Parser::ParseForStatement(ParsedAttributes &attrs) {
|
2009-11-21 16:43:09 +08:00
|
|
|
// FIXME: Use attributes?
|
2010-02-11 10:19:13 +08:00
|
|
|
|
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'.
|
2008-12-12 03:48:14 +08:00
|
|
|
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
2008-11-18 15:48:38 +08:00
|
|
|
Diag(Tok, diag::err_expected_lparen_after) << "for";
|
2006-08-10 12:59:57 +08:00
|
|
|
SkipUntil(tok::semi);
|
2008-12-12 03:48:14 +08:00
|
|
|
return StmtError();
|
2006-08-10 12:59:57 +08:00
|
|
|
}
|
2008-12-12 03:48:14 +08:00
|
|
|
|
2009-04-22 08:54:41 +08:00
|
|
|
bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
|
2008-09-11 11:06:46 +08:00
|
|
|
|
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.
|
2008-09-12 07:08:39 +08: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 11:06:46 +08:00
|
|
|
// C++ 6.5.3p1:
|
|
|
|
// Names declared in the for-init-statement are in the same declarative-region
|
|
|
|
// as those declared in the condition.
|
|
|
|
//
|
2008-12-10 14:34:36 +08:00
|
|
|
unsigned ScopeFlags;
|
2009-04-22 08:54:41 +08:00
|
|
|
if (C99orCXXorObjC)
|
2008-12-10 14:34:36 +08:00
|
|
|
ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
|
|
|
|
Scope::DeclScope | Scope::ControlScope;
|
2007-08-27 07:08:06 +08:00
|
|
|
else
|
2008-12-10 14:34:36 +08:00
|
|
|
ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
|
|
|
|
|
|
|
|
ParseScope ForScope(this, ScopeFlags);
|
2006-08-10 12:59:57 +08:00
|
|
|
|
2006-10-16 14:12:55 +08:00
|
|
|
SourceLocation LParenLoc = ConsumeParen();
|
2010-08-24 14:29:42 +08:00
|
|
|
ExprResult Value;
|
2008-12-09 21:15:23 +08:00
|
|
|
|
2011-04-15 06:09:26 +08:00
|
|
|
bool ForEach = false, ForRange = false;
|
2010-08-24 14:29:42 +08:00
|
|
|
StmtResult FirstPart;
|
2010-05-07 05:39:56 +08:00
|
|
|
bool SecondPartIsInvalid = false;
|
2010-05-07 01:25:47 +08:00
|
|
|
FullExprArg SecondPart(Actions);
|
2010-08-24 14:29:42 +08:00
|
|
|
ExprResult Collection;
|
2011-04-15 06:09:26 +08:00
|
|
|
ForRangeInit ForRangeInit;
|
2010-05-07 01:25:47 +08:00
|
|
|
FullExprArg ThirdPart(Actions);
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *SecondVar = 0;
|
2009-11-25 08:27:52 +08:00
|
|
|
|
2009-09-22 04:51:25 +08:00
|
|
|
if (Tok.is(tok::code_completion)) {
|
2010-07-03 01:43:08 +08:00
|
|
|
Actions.CodeCompleteOrdinaryName(getCurScope(),
|
2010-08-27 07:41:50 +08:00
|
|
|
C99orCXXorObjC? Sema::PCC_ForInit
|
|
|
|
: Sema::PCC_Expression);
|
2011-09-04 11:32:15 +08:00
|
|
|
cutOffParsing();
|
|
|
|
return StmtError();
|
2009-09-22 04:51:25 +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();
|
2008-10-05 23:50:46 +08:00
|
|
|
} else if (isSimpleDeclaration()) { // for (int X = 4;
|
2006-08-10 13:19:57 +08:00
|
|
|
// Parse declaration, which eats the ';'.
|
2009-04-22 08:54:41 +08:00
|
|
|
if (!C99orCXXorObjC) // 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-12-12 03:48:14 +08:00
|
|
|
|
2011-03-24 19:26:52 +08:00
|
|
|
ParsedAttributesWithRange attrs(AttrFactory);
|
2010-12-24 10:08:15 +08:00
|
|
|
MaybeParseCXX0XAttributes(attrs);
|
2009-11-21 16:43:09 +08:00
|
|
|
|
2011-04-15 06:09:26 +08:00
|
|
|
// In C++0x, "for (T NS:a" might not be a typo for ::
|
|
|
|
bool MightBeForRangeStmt = getLang().CPlusPlus;
|
|
|
|
ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
|
|
|
|
|
fix a FIXME, providing accurate source range info for DeclStmt's. The end
of the range is now the ';' location. For something like this:
$ cat t2.c
#define bool int
void f(int x, int y) {
bool b = !x && y;
}
We used to produce:
$ clang-cc t2.c -ast-dump
typedef char *__builtin_va_list;
void f(int x, int y)
(CompoundStmt 0x2201f10 <t2.c:3:22, line:5:1>
(DeclStmt 0x2201ef0 <line:2:14> <----
0x2201a20 "int b =
(BinaryOperator 0x2201ed0 <line:4:10, col:16> 'int' '&&'
(UnaryOperator 0x2201e90 <col:10, col:11> 'int' prefix '!'
(DeclRefExpr 0x2201c90 <col:11> 'int' ParmVar='x' 0x2201a50))
(DeclRefExpr 0x2201eb0 <col:16> 'int' ParmVar='y' 0x2201e10))")
Now we produce:
$ clang-cc t2.c -ast-dump
typedef char *__builtin_va_list;
void f(int x, int y)
(CompoundStmt 0x2201f10 <t2.c:3:22, line:5:1>
(DeclStmt 0x2201ef0 <line:2:14, line:4:17> <------
0x2201a20 "int b =
(BinaryOperator 0x2201ed0 <col:10, col:16> 'int' '&&'
(UnaryOperator 0x2201e90 <col:10, col:11> 'int' prefix '!'
(DeclRefExpr 0x2201c90 <col:11> 'int' ParmVar='x' 0x2201a50))
(DeclRefExpr 0x2201eb0 <col:16> 'int' ParmVar='y' 0x2201e10))")
llvm-svn: 68288
2009-04-02 12:16:50 +08:00
|
|
|
SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
|
2010-09-29 04:42:35 +08:00
|
|
|
StmtVector Stmts(Actions);
|
|
|
|
DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
|
2011-04-15 06:09:26 +08:00
|
|
|
DeclEnd, attrs, false,
|
|
|
|
MightBeForRangeStmt ?
|
|
|
|
&ForRangeInit : 0);
|
2009-03-30 01:27:48 +08:00
|
|
|
FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-04-15 06:09:26 +08:00
|
|
|
if (ForRangeInit.ParsedForRangeDecl()) {
|
2011-09-05 03:54:14 +08:00
|
|
|
if (!getLang().CPlusPlus0x)
|
|
|
|
Diag(ForRangeInit.ColonLoc, diag::ext_for_range);
|
|
|
|
|
2011-04-15 06:09:26 +08:00
|
|
|
ForRange = true;
|
|
|
|
} else if (Tok.is(tok::semi)) { // for (int x = 4;
|
2009-03-30 01:27:48 +08:00
|
|
|
ConsumeToken();
|
|
|
|
} else if ((ForEach = isTokIdentifier_in())) {
|
2009-11-20 06:12:37 +08:00
|
|
|
Actions.ActOnForEachDeclStmt(DG);
|
2009-09-09 23:08:12 +08:00
|
|
|
// ObjC: for (id x in expr)
|
2008-01-04 01:55:25 +08:00
|
|
|
ConsumeToken(); // consume 'in'
|
2010-08-24 05:17:50 +08:00
|
|
|
|
|
|
|
if (Tok.is(tok::code_completion)) {
|
|
|
|
Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
|
2011-09-04 11:32:15 +08:00
|
|
|
cutOffParsing();
|
|
|
|
return StmtError();
|
2010-08-24 05:17:50 +08:00
|
|
|
}
|
2010-05-07 01:25:47 +08:00
|
|
|
Collection = ParseExpression();
|
2009-03-30 01:27:48 +08:00
|
|
|
} else {
|
|
|
|
Diag(Tok, diag::err_expected_semi_for);
|
2008-01-04 01:55:25 +08:00
|
|
|
}
|
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
|
|
|
|
2010-12-04 11:47:34 +08:00
|
|
|
ForEach = isTokIdentifier_in();
|
|
|
|
|
2007-06-27 09:06:29 +08:00
|
|
|
// Turn the expression into a stmt.
|
2010-12-04 11:47:34 +08:00
|
|
|
if (!Value.isInvalid()) {
|
|
|
|
if (ForEach)
|
|
|
|
FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
|
|
|
|
else
|
|
|
|
FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
|
|
|
|
}
|
2008-12-10 08:02:53 +08:00
|
|
|
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.is(tok::semi)) {
|
2006-08-10 13:19:57 +08:00
|
|
|
ConsumeToken();
|
2010-12-04 11:47:34 +08:00
|
|
|
} else if (ForEach) {
|
2008-01-04 01:55:25 +08:00
|
|
|
ConsumeToken(); // consume 'in'
|
2010-08-24 05:17:50 +08:00
|
|
|
|
|
|
|
if (Tok.is(tok::code_completion)) {
|
|
|
|
Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
|
2011-09-04 11:32:15 +08:00
|
|
|
cutOffParsing();
|
|
|
|
return StmtError();
|
2010-08-24 05:17:50 +08:00
|
|
|
}
|
2010-05-07 01:25:47 +08:00
|
|
|
Collection = ParseExpression();
|
2009-03-30 00:50:03 +08:00
|
|
|
} else {
|
2011-02-17 11:38:46 +08:00
|
|
|
if (!Value.isInvalid()) {
|
|
|
|
Diag(Tok, diag::err_expected_semi_for);
|
|
|
|
} else {
|
|
|
|
// Skip until semicolon or rparen, don't consume it.
|
|
|
|
SkipUntil(tok::r_paren, true, true);
|
|
|
|
if (Tok.is(tok::semi))
|
|
|
|
ConsumeToken();
|
|
|
|
}
|
2006-08-10 13:19:57 +08:00
|
|
|
}
|
2006-08-10 12:59:57 +08:00
|
|
|
}
|
2011-04-15 06:09:26 +08:00
|
|
|
if (!ForEach && !ForRange) {
|
2010-08-24 07:25:46 +08:00
|
|
|
assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
|
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.
|
2011-02-17 11:38:46 +08:00
|
|
|
} else if (Tok.is(tok::r_paren)) {
|
|
|
|
// missing both semicolons.
|
2008-01-04 01:55:25 +08:00
|
|
|
} else {
|
2010-08-24 14:29:42 +08:00
|
|
|
ExprResult Second;
|
2009-11-25 08:27:52 +08:00
|
|
|
if (getLang().CPlusPlus)
|
2010-05-07 01:25:47 +08:00
|
|
|
ParseCXXCondition(Second, SecondVar, ForLoc, true);
|
|
|
|
else {
|
|
|
|
Second = ParseExpression();
|
|
|
|
if (!Second.isInvalid())
|
2010-07-03 01:43:08 +08:00
|
|
|
Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
|
2010-08-24 07:25:46 +08:00
|
|
|
Second.get());
|
2010-05-07 01:25:47 +08:00
|
|
|
}
|
2010-05-07 05:39:56 +08:00
|
|
|
SecondPartIsInvalid = Second.isInvalid();
|
2010-08-24 07:25:46 +08:00
|
|
|
SecondPart = Actions.MakeFullExpr(Second.get());
|
2008-01-04 01:55:25 +08:00
|
|
|
}
|
2008-12-09 21:15:23 +08:00
|
|
|
|
2011-02-17 11:38:46 +08:00
|
|
|
if (Tok.isNot(tok::semi)) {
|
|
|
|
if (!SecondPartIsInvalid || SecondVar)
|
|
|
|
Diag(Tok, diag::err_expected_semi_for);
|
|
|
|
else
|
|
|
|
// Skip until semicolon or rparen, don't consume it.
|
|
|
|
SkipUntil(tok::r_paren, true, true);
|
|
|
|
}
|
|
|
|
|
2008-01-04 01:55:25 +08:00
|
|
|
if (Tok.is(tok::semi)) {
|
|
|
|
ConsumeToken();
|
|
|
|
}
|
2008-12-12 03:48:14 +08:00
|
|
|
|
2008-01-04 01:55:25 +08:00
|
|
|
// Parse the third part of the for specifier.
|
2010-05-07 01:25:47 +08:00
|
|
|
if (Tok.isNot(tok::r_paren)) { // for (...;...;)
|
2010-08-24 14:29:42 +08:00
|
|
|
ExprResult Third = ParseExpression();
|
2010-08-24 07:25:46 +08:00
|
|
|
ThirdPart = Actions.MakeFullExpr(Third.take());
|
2010-05-07 01:25:47 +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);
|
2008-12-09 21:15:23 +08:00
|
|
|
|
2011-04-15 06:09:26 +08:00
|
|
|
// We need to perform most of the semantic analysis for a C++0x for-range
|
|
|
|
// statememt before parsing the body, in order to be able to deduce the type
|
|
|
|
// of an auto-typed loop variable.
|
|
|
|
StmtResult ForRangeStmt;
|
2011-07-27 09:07:15 +08:00
|
|
|
if (ForRange) {
|
2011-04-15 06:09:26 +08:00
|
|
|
ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, LParenLoc,
|
|
|
|
FirstPart.take(),
|
|
|
|
ForRangeInit.ColonLoc,
|
|
|
|
ForRangeInit.RangeExpr.get(),
|
|
|
|
RParenLoc);
|
|
|
|
|
2011-07-27 09:07:15 +08:00
|
|
|
|
|
|
|
// Similarly, we need to do the semantic analysis for a for-range
|
|
|
|
// statement immediately in order to close over temporaries correctly.
|
|
|
|
} else if (ForEach) {
|
|
|
|
if (!Collection.isInvalid())
|
|
|
|
Collection =
|
|
|
|
Actions.ActOnObjCForCollectionOperand(ForLoc, Collection.take());
|
|
|
|
}
|
|
|
|
|
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++.
|
|
|
|
//
|
2009-09-09 23:08:12 +08:00
|
|
|
ParseScope InnerScope(this, Scope::DeclScope,
|
2009-04-22 08:54:41 +08:00
|
|
|
C99orCXXorObjC && Tok.isNot(tok::l_brace));
|
2008-12-09 21:15:23 +08:00
|
|
|
|
2006-08-10 12:59:57 +08:00
|
|
|
// Read the body statement.
|
2010-08-24 14:29:42 +08:00
|
|
|
StmtResult Body(ParseStatement());
|
2008-12-09 21:15:23 +08:00
|
|
|
|
2007-08-22 13:28:50 +08:00
|
|
|
// Pop the body scope if needed.
|
2008-12-10 14:34:36 +08:00
|
|
|
InnerScope.Exit();
|
2007-08-22 13:28:50 +08:00
|
|
|
|
2006-11-06 07:47:55 +08:00
|
|
|
// Leave the for-scope.
|
2008-12-10 14:34:36 +08:00
|
|
|
ForScope.Exit();
|
2008-12-09 21:15:23 +08:00
|
|
|
|
|
|
|
if (Body.isInvalid())
|
2008-12-12 03:48:14 +08:00
|
|
|
return StmtError();
|
2008-12-10 08:02:53 +08:00
|
|
|
|
2011-04-15 06:09:26 +08:00
|
|
|
if (ForEach)
|
|
|
|
return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
|
|
|
|
FirstPart.take(),
|
|
|
|
Collection.take(), RParenLoc,
|
|
|
|
Body.take());
|
|
|
|
|
|
|
|
if (ForRange)
|
|
|
|
return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take());
|
2010-05-07 01:25:47 +08:00
|
|
|
|
2011-04-15 06:09:26 +08:00
|
|
|
return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.take(), SecondPart,
|
|
|
|
SecondVar, ThirdPart, RParenLoc, Body.take());
|
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 ';'.
|
|
|
|
///
|
2010-12-24 10:08:15 +08:00
|
|
|
StmtResult Parser::ParseGotoStatement(ParsedAttributes &attrs) {
|
2009-11-21 16:43:09 +08:00
|
|
|
// FIXME: Use attributes?
|
2010-02-11 10:19:13 +08:00
|
|
|
|
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'.
|
2008-12-12 03:48:14 +08:00
|
|
|
|
2010-08-24 14:29:42 +08:00
|
|
|
StmtResult Res;
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.is(tok::identifier)) {
|
2011-02-18 09:27:55 +08:00
|
|
|
LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
|
|
|
|
Tok.getLocation());
|
|
|
|
Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
|
2006-08-10 13:45:44 +08:00
|
|
|
ConsumeToken();
|
2009-04-28 08:51:18 +08:00
|
|
|
} else if (Tok.is(tok::star)) {
|
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();
|
2010-08-24 14:29:42 +08:00
|
|
|
ExprResult R(ParseExpression());
|
2008-12-09 21:15:23 +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);
|
2008-12-12 03:48:14 +08:00
|
|
|
return StmtError();
|
2006-10-16 13:52:41 +08:00
|
|
|
}
|
2010-08-24 07:25:46 +08:00
|
|
|
Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
|
2007-07-22 12:13:33 +08:00
|
|
|
} else {
|
|
|
|
Diag(Tok, diag::err_expected_ident);
|
2008-12-12 03:48:14 +08:00
|
|
|
return StmtError();
|
2006-08-10 13:45:44 +08:00
|
|
|
}
|
2008-12-09 21:15:23 +08:00
|
|
|
|
2008-12-12 03:48:14 +08:00
|
|
|
return move(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 ';'.
|
|
|
|
///
|
2010-12-24 10:08:15 +08:00
|
|
|
StmtResult Parser::ParseContinueStatement(ParsedAttributes &attrs) {
|
2009-11-21 16:43:09 +08:00
|
|
|
// FIXME: Use attributes?
|
2010-02-11 10:19:13 +08:00
|
|
|
|
2006-11-06 07:47:55 +08:00
|
|
|
SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
|
2010-07-03 01:43:08 +08:00
|
|
|
return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
|
2006-11-06 07:47:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseBreakStatement
|
|
|
|
/// jump-statement:
|
|
|
|
/// 'break' ';'
|
|
|
|
///
|
|
|
|
/// Note: this lets the caller parse the end ';'.
|
|
|
|
///
|
2010-12-24 10:08:15 +08:00
|
|
|
StmtResult Parser::ParseBreakStatement(ParsedAttributes &attrs) {
|
2009-11-21 16:43:09 +08:00
|
|
|
// FIXME: Use attributes?
|
2010-02-11 10:19:13 +08:00
|
|
|
|
2006-11-06 07:47:55 +08:00
|
|
|
SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
|
2010-07-03 01:43:08 +08:00
|
|
|
return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
|
2006-11-06 07:47:55 +08:00
|
|
|
}
|
|
|
|
|
2006-08-10 13:45:44 +08:00
|
|
|
/// ParseReturnStatement
|
|
|
|
/// jump-statement:
|
|
|
|
/// 'return' expression[opt] ';'
|
2010-12-24 10:08:15 +08:00
|
|
|
StmtResult Parser::ParseReturnStatement(ParsedAttributes &attrs) {
|
2009-11-21 16:43:09 +08:00
|
|
|
// FIXME: Use attributes?
|
2010-02-11 10:19:13 +08:00
|
|
|
|
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'.
|
2008-12-12 03:48:14 +08:00
|
|
|
|
2010-08-24 14:29:42 +08:00
|
|
|
ExprResult R;
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.isNot(tok::semi)) {
|
2010-05-30 09:49:25 +08:00
|
|
|
if (Tok.is(tok::code_completion)) {
|
2010-07-03 01:43:08 +08:00
|
|
|
Actions.CodeCompleteReturn(getCurScope());
|
2011-09-04 11:32:15 +08:00
|
|
|
cutOffParsing();
|
2010-05-30 09:49:25 +08:00
|
|
|
return StmtError();
|
|
|
|
}
|
|
|
|
|
2011-03-12 07:10:44 +08:00
|
|
|
// FIXME: This is a hack to allow something like C++0x's generalized
|
|
|
|
// initializer lists, but only enough of this feature to allow Clang to
|
|
|
|
// parse libstdc++ 4.5's headers.
|
|
|
|
if (Tok.is(tok::l_brace) && getLang().CPlusPlus) {
|
|
|
|
R = ParseInitializer();
|
|
|
|
if (R.isUsable() && !getLang().CPlusPlus0x)
|
|
|
|
Diag(R.get()->getLocStart(), diag::ext_generalized_initializer_lists)
|
|
|
|
<< R.get()->getSourceRange();
|
|
|
|
} else
|
|
|
|
R = ParseExpression();
|
2008-12-09 21:15:23 +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);
|
2008-12-12 03:48:14 +08:00
|
|
|
return StmtError();
|
2006-10-16 13:52:41 +08:00
|
|
|
}
|
2006-08-13 00:59:03 +08:00
|
|
|
}
|
2010-08-24 07:25:46 +08:00
|
|
|
return Actions.ActOnReturnStmt(ReturnLoc, R.take());
|
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.
|
2010-12-03 02:34:55 +08:00
|
|
|
StmtResult Parser::FuzzyParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
|
|
|
|
SourceLocation EndLoc;
|
2008-02-08 07:24:32 +08:00
|
|
|
if (Tok.is(tok::l_brace)) {
|
|
|
|
unsigned short savedBraceCount = BraceCount;
|
|
|
|
do {
|
2010-12-03 02:34:55 +08:00
|
|
|
EndLoc = Tok.getLocation();
|
2008-02-08 07:24:32 +08:00
|
|
|
ConsumeAnyToken();
|
|
|
|
} while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
|
2009-09-09 23:08:12 +08:00
|
|
|
} else {
|
2008-02-08 07:24:32 +08:00
|
|
|
// 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();
|
2011-07-26 05:09:52 +08:00
|
|
|
unsigned LineNo = SrcMgr.getExpansionLineNumber(TokLoc);
|
2008-02-09 02:01:27 +08:00
|
|
|
do {
|
2010-12-03 02:34:55 +08:00
|
|
|
EndLoc = TokLoc;
|
2008-02-09 02:01:27 +08:00
|
|
|
ConsumeAnyToken();
|
|
|
|
TokLoc = Tok.getLocation();
|
2011-07-26 05:09:52 +08:00
|
|
|
} while ((SrcMgr.getExpansionLineNumber(TokLoc) == LineNo) &&
|
2009-09-09 23:08:12 +08:00
|
|
|
Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
|
2008-02-09 02:01:27 +08:00
|
|
|
Tok.isNot(tok::eof));
|
2008-02-08 07:24:32 +08:00
|
|
|
}
|
2009-12-11 08:04:56 +08:00
|
|
|
Token t;
|
|
|
|
t.setKind(tok::string_literal);
|
2010-08-18 00:00:12 +08:00
|
|
|
t.setLiteralData("\"/*FIXME: not done*/\"");
|
2009-12-11 08:04:56 +08:00
|
|
|
t.clearFlag(Token::NeedsCleaning);
|
2010-08-18 00:00:12 +08:00
|
|
|
t.setLength(21);
|
2010-08-31 01:47:05 +08:00
|
|
|
ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
|
2009-12-11 08:04:56 +08:00
|
|
|
ExprVector Constraints(Actions);
|
|
|
|
ExprVector Exprs(Actions);
|
|
|
|
ExprVector Clobbers(Actions);
|
2010-12-03 02:34:55 +08:00
|
|
|
return Actions.ActOnAsmStmt(AsmLoc, true, true, 0, 0, 0,
|
2009-12-11 08:04:56 +08:00
|
|
|
move_arg(Constraints), move_arg(Exprs),
|
2010-08-24 07:25:46 +08:00
|
|
|
AsmString.take(), move_arg(Clobbers),
|
2010-12-03 02:34:55 +08:00
|
|
|
EndLoc, true);
|
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]
|
|
|
|
///
|
2010-08-24 14:29:42 +08:00
|
|
|
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();
|
2008-12-12 03:48:14 +08:00
|
|
|
|
2011-09-18 01:15:52 +08:00
|
|
|
if (getLang().MicrosoftExt && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
|
2008-02-07 11:50:06 +08:00
|
|
|
msAsm = true;
|
2010-12-03 02:34:55 +08:00
|
|
|
return FuzzyParseMicrosoftAsmStatement(AsmLoc);
|
2008-02-07 11:50:06 +08:00
|
|
|
}
|
2011-03-24 19:26:52 +08:00
|
|
|
DeclSpec DS(AttrFactory);
|
2006-08-15 14:03:28 +08:00
|
|
|
SourceLocation Loc = Tok.getLocation();
|
2009-11-21 16:43:09 +08:00
|
|
|
ParseTypeQualifierListOpt(DS, true, false);
|
2008-12-12 03:48:14 +08:00
|
|
|
|
2006-08-15 14:03:28 +08:00
|
|
|
// 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)
|
2008-11-18 15:48:38 +08:00
|
|
|
Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
|
2006-11-28 12:33:46 +08:00
|
|
|
if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
|
2008-11-18 15:48:38 +08:00
|
|
|
Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
|
2008-12-12 03:48:14 +08:00
|
|
|
|
2006-08-15 14:03:28 +08:00
|
|
|
// Remember if this was a volatile asm.
|
2007-11-24 07:12:25 +08:00
|
|
|
bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
2008-11-18 15:48:38 +08:00
|
|
|
Diag(Tok, diag::err_expected_lparen_after) << "asm";
|
2006-08-15 14:03:28 +08:00
|
|
|
SkipUntil(tok::r_paren);
|
2008-12-12 03:48:14 +08:00
|
|
|
return StmtError();
|
2006-08-15 14:03:28 +08:00
|
|
|
}
|
2006-10-16 14:12:55 +08:00
|
|
|
Loc = ConsumeParen();
|
2008-12-12 03:48:14 +08:00
|
|
|
|
2010-08-24 14:29:42 +08:00
|
|
|
ExprResult AsmString(ParseAsmStringLiteral());
|
2008-12-09 21:15:23 +08:00
|
|
|
if (AsmString.isInvalid())
|
2008-12-12 03:48:14 +08:00
|
|
|
return StmtError();
|
2008-12-09 21:15:23 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<IdentifierInfo *, 4> Names;
|
2008-11-26 06:21:31 +08:00
|
|
|
ExprVector Constraints(Actions);
|
|
|
|
ExprVector Exprs(Actions);
|
|
|
|
ExprVector Clobbers(Actions);
|
2008-02-06 07:03:50 +08:00
|
|
|
|
|
|
|
if (Tok.is(tok::r_paren)) {
|
2009-12-21 07:00:41 +08:00
|
|
|
// We have a simple asm expression like 'asm("foo")'.
|
|
|
|
SourceLocation RParenLoc = ConsumeParen();
|
|
|
|
return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
|
|
|
|
/*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
|
|
|
|
move_arg(Constraints), move_arg(Exprs),
|
2010-08-24 07:25:46 +08:00
|
|
|
AsmString.take(), move_arg(Clobbers),
|
2009-12-21 07:00:41 +08:00
|
|
|
RParenLoc);
|
|
|
|
}
|
2008-12-12 03:48:14 +08:00
|
|
|
|
2009-12-21 07:00:41 +08:00
|
|
|
// Parse Outputs, if present.
|
2009-12-21 07:08:04 +08:00
|
|
|
bool AteExtraColon = false;
|
|
|
|
if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
|
|
|
|
// In C++ mode, parse "::" like ": :".
|
|
|
|
AteExtraColon = Tok.is(tok::coloncolon);
|
2009-12-21 07:00:41 +08:00
|
|
|
ConsumeToken();
|
|
|
|
|
2009-12-21 07:08:04 +08:00
|
|
|
if (!AteExtraColon &&
|
|
|
|
ParseAsmOperandsOpt(Names, Constraints, Exprs))
|
2009-12-21 07:00:41 +08:00
|
|
|
return StmtError();
|
|
|
|
}
|
2009-12-21 07:08:04 +08:00
|
|
|
|
2009-12-21 07:00:41 +08:00
|
|
|
unsigned NumOutputs = Names.size();
|
2008-12-12 03:48:14 +08:00
|
|
|
|
2009-12-21 07:00:41 +08:00
|
|
|
// Parse Inputs, if present.
|
2009-12-21 07:08:04 +08:00
|
|
|
if (AteExtraColon ||
|
|
|
|
Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
|
|
|
|
// In C++ mode, parse "::" like ": :".
|
|
|
|
if (AteExtraColon)
|
|
|
|
AteExtraColon = false;
|
|
|
|
else {
|
|
|
|
AteExtraColon = Tok.is(tok::coloncolon);
|
|
|
|
ConsumeToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AteExtraColon &&
|
|
|
|
ParseAsmOperandsOpt(Names, Constraints, Exprs))
|
2009-12-21 07:00:41 +08:00
|
|
|
return StmtError();
|
|
|
|
}
|
2008-12-10 08:02:53 +08:00
|
|
|
|
2009-12-21 07:00:41 +08:00
|
|
|
assert(Names.size() == Constraints.size() &&
|
|
|
|
Constraints.size() == Exprs.size() &&
|
|
|
|
"Input operand size mismatch!");
|
2007-11-22 09:36:19 +08:00
|
|
|
|
2009-12-21 07:00:41 +08:00
|
|
|
unsigned NumInputs = Names.size() - NumOutputs;
|
2008-12-10 08:02:53 +08:00
|
|
|
|
2009-12-21 07:00:41 +08:00
|
|
|
// Parse the clobbers, if present.
|
2009-12-21 07:08:04 +08:00
|
|
|
if (AteExtraColon || Tok.is(tok::colon)) {
|
|
|
|
if (!AteExtraColon)
|
|
|
|
ConsumeToken();
|
2008-12-10 08:02:53 +08:00
|
|
|
|
2010-07-22 15:11:21 +08:00
|
|
|
// Parse the asm-string list for clobbers if present.
|
|
|
|
if (Tok.isNot(tok::r_paren)) {
|
|
|
|
while (1) {
|
2010-08-24 14:29:42 +08:00
|
|
|
ExprResult Clobber(ParseAsmStringLiteral());
|
2006-08-15 14:03:28 +08:00
|
|
|
|
2010-07-22 15:11:21 +08:00
|
|
|
if (Clobber.isInvalid())
|
|
|
|
break;
|
2008-12-10 08:02:53 +08:00
|
|
|
|
2010-07-22 15:11:21 +08:00
|
|
|
Clobbers.push_back(Clobber.release());
|
2008-12-10 08:02:53 +08:00
|
|
|
|
2010-07-22 15:11:21 +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
|
|
|
}
|
2008-12-10 08:02:53 +08:00
|
|
|
|
2009-12-21 07:00:41 +08:00
|
|
|
SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
|
|
|
|
return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
|
2009-05-21 17:52:38 +08:00
|
|
|
NumOutputs, NumInputs, Names.data(),
|
2009-01-19 02:03:53 +08:00
|
|
|
move_arg(Constraints), move_arg(Exprs),
|
2010-08-24 07:25:46 +08:00
|
|
|
AsmString.take(), move_arg(Clobbers),
|
2009-01-19 00:53:17 +08:00
|
|
|
RParenLoc);
|
2006-08-15 14:03:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseAsmOperands - Parse the asm-operands production as used by
|
2009-12-21 07:00:41 +08:00
|
|
|
/// asm-statement, assuming the leading ':' token was eaten.
|
2006-08-15 14:03:28 +08:00
|
|
|
///
|
|
|
|
/// [GNU] asm-operands:
|
|
|
|
/// asm-operand
|
|
|
|
/// asm-operands ',' asm-operand
|
|
|
|
///
|
|
|
|
/// [GNU] asm-operand:
|
|
|
|
/// asm-string-literal '(' expression ')'
|
|
|
|
/// '[' identifier ']' asm-string-literal '(' expression ')'
|
|
|
|
///
|
2009-10-19 04:26:27 +08:00
|
|
|
//
|
|
|
|
// FIXME: Avoid unnecessary std::string trashing.
|
2011-07-23 18:55:15 +08:00
|
|
|
bool Parser::ParseAsmOperandsOpt(SmallVectorImpl<IdentifierInfo *> &Names,
|
2011-09-09 10:00:50 +08:00
|
|
|
SmallVectorImpl<Expr *> &Constraints,
|
|
|
|
SmallVectorImpl<Expr *> &Exprs) {
|
2006-08-15 14:03:28 +08:00
|
|
|
// '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;
|
2009-09-09 23:08:12 +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();
|
2009-09-09 23:08:12 +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
|
|
|
}
|
2009-09-09 23:08:12 +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
|
|
|
|
2010-01-31 06:25:16 +08:00
|
|
|
Names.push_back(II);
|
2006-08-15 14:03:28 +08:00
|
|
|
MatchRHSPunctuation(tok::r_square, Loc);
|
2007-11-22 09:36:19 +08:00
|
|
|
} else
|
2010-01-31 06:25:16 +08:00
|
|
|
Names.push_back(0);
|
2008-12-09 21:15:23 +08:00
|
|
|
|
2010-08-24 14:29:42 +08:00
|
|
|
ExprResult Constraint(ParseAsmStringLiteral());
|
2008-12-09 21:15:23 +08:00
|
|
|
if (Constraint.isInvalid()) {
|
2007-11-22 09:36:19 +08:00
|
|
|
SkipUntil(tok::r_paren);
|
2008-02-10 03:57:29 +08:00
|
|
|
return true;
|
2007-11-22 09:36:19 +08:00
|
|
|
}
|
2008-12-10 08:02:53 +08:00
|
|
|
Constraints.push_back(Constraint.release());
|
2006-08-15 14:03:28 +08:00
|
|
|
|
2007-10-10 01:41:39 +08:00
|
|
|
if (Tok.isNot(tok::l_paren)) {
|
2008-11-18 15:48:38 +08:00
|
|
|
Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
|
2006-08-15 14:03:28 +08:00
|
|
|
SkipUntil(tok::r_paren);
|
2008-02-10 03:57:29 +08:00
|
|
|
return true;
|
2006-08-15 14:03:28 +08:00
|
|
|
}
|
2008-12-10 08:02:53 +08:00
|
|
|
|
2006-08-15 14:03:28 +08:00
|
|
|
// Read the parenthesized expression.
|
2009-05-03 15:49:42 +08:00
|
|
|
SourceLocation OpenLoc = ConsumeParen();
|
2010-08-24 14:29:42 +08:00
|
|
|
ExprResult Res(ParseExpression());
|
2009-05-03 15:49:42 +08:00
|
|
|
MatchRHSPunctuation(tok::r_paren, OpenLoc);
|
2008-12-09 21:15:23 +08:00
|
|
|
if (Res.isInvalid()) {
|
2006-08-15 14:03:28 +08:00
|
|
|
SkipUntil(tok::r_paren);
|
2008-02-10 03:57:29 +08:00
|
|
|
return true;
|
2006-08-15 14:03:28 +08:00
|
|
|
}
|
2008-12-10 08:02:53 +08:00
|
|
|
Exprs.push_back(Res.release());
|
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
|
|
|
|
2011-03-17 01:05:57 +08:00
|
|
|
Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
|
2009-03-05 08:49:17 +08:00
|
|
|
assert(Tok.is(tok::l_brace));
|
|
|
|
SourceLocation LBraceLoc = Tok.getLocation();
|
2009-04-27 04:35:05 +08:00
|
|
|
|
2011-03-17 01:05:57 +08:00
|
|
|
if (PP.isCodeCompletionEnabled()) {
|
|
|
|
if (trySkippingFunctionBodyForCodeCompletion()) {
|
|
|
|
BodyScope.Exit();
|
2011-01-04 08:27:27 +08:00
|
|
|
return Actions.ActOnFinishFunctionBody(Decl, 0);
|
2011-03-17 01:05:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-27 07:41:50 +08:00
|
|
|
PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
|
|
|
|
"parsing function body");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-11-09 03:01:26 +08:00
|
|
|
// 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.
|
2010-08-24 14:29:42 +08:00
|
|
|
StmtResult FnBody(ParseCompoundStatementBody());
|
2008-12-12 03:30:53 +08:00
|
|
|
|
2007-11-09 03:01:26 +08:00
|
|
|
// If the function body could not be parsed, make a bogus compoundstmt.
|
2008-12-09 21:15:23 +08:00
|
|
|
if (FnBody.isInvalid())
|
2009-09-09 23:08:12 +08:00
|
|
|
FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
|
2009-03-05 08:49:17 +08:00
|
|
|
MultiStmtArg(Actions), false);
|
2008-12-12 03:30:53 +08:00
|
|
|
|
2011-03-17 01:05:57 +08:00
|
|
|
BodyScope.Exit();
|
2010-08-24 07:25:46 +08:00
|
|
|
return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
|
2007-12-01 16:06:07 +08:00
|
|
|
}
|
2008-12-22 00:41:36 +08:00
|
|
|
|
2009-04-27 04:35:05 +08:00
|
|
|
/// ParseFunctionTryBlock - Parse a C++ function-try-block.
|
|
|
|
///
|
|
|
|
/// function-try-block:
|
|
|
|
/// 'try' ctor-initializer[opt] compound-statement handler-seq
|
|
|
|
///
|
2011-03-17 01:05:57 +08:00
|
|
|
Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
|
2009-04-27 04:35:05 +08:00
|
|
|
assert(Tok.is(tok::kw_try) && "Expected 'try'");
|
|
|
|
SourceLocation TryLoc = ConsumeToken();
|
|
|
|
|
2010-08-27 07:41:50 +08:00
|
|
|
PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
|
|
|
|
"parsing function try block");
|
2009-04-27 04:35:05 +08:00
|
|
|
|
|
|
|
// Constructor initializer list?
|
|
|
|
if (Tok.is(tok::colon))
|
|
|
|
ParseConstructorInitializer(Decl);
|
2011-09-08 04:36:12 +08:00
|
|
|
else
|
|
|
|
Actions.ActOnDefaultCtorInitializers(Decl);
|
|
|
|
|
2011-03-17 01:05:57 +08:00
|
|
|
if (PP.isCodeCompletionEnabled()) {
|
|
|
|
if (trySkippingFunctionBodyForCodeCompletion()) {
|
|
|
|
BodyScope.Exit();
|
2011-01-04 08:27:27 +08:00
|
|
|
return Actions.ActOnFinishFunctionBody(Decl, 0);
|
2011-03-17 01:05:57 +08:00
|
|
|
}
|
|
|
|
}
|
2011-01-04 06:33:06 +08:00
|
|
|
|
2009-04-27 05:08:36 +08:00
|
|
|
SourceLocation LBraceLoc = Tok.getLocation();
|
2010-08-24 14:29:42 +08:00
|
|
|
StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
|
2009-04-27 04:35:05 +08:00
|
|
|
// If we failed to parse the try-catch, we just give the function an empty
|
|
|
|
// compound statement as the body.
|
|
|
|
if (FnBody.isInvalid())
|
2009-04-27 05:08:36 +08:00
|
|
|
FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
|
2009-04-27 04:35:05 +08:00
|
|
|
MultiStmtArg(Actions), false);
|
|
|
|
|
2011-03-17 01:05:57 +08:00
|
|
|
BodyScope.Exit();
|
2010-08-24 07:25:46 +08:00
|
|
|
return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
|
2009-04-27 04:35:05 +08:00
|
|
|
}
|
|
|
|
|
2011-01-04 08:27:27 +08:00
|
|
|
bool Parser::trySkippingFunctionBodyForCodeCompletion() {
|
2011-01-04 06:33:06 +08:00
|
|
|
assert(Tok.is(tok::l_brace));
|
2011-01-04 08:27:27 +08:00
|
|
|
assert(PP.isCodeCompletionEnabled() &&
|
|
|
|
"Should only be called when in code-completion mode");
|
2011-01-04 06:33:06 +08:00
|
|
|
|
|
|
|
// We're in code-completion mode. Skip parsing for all function bodies unless
|
|
|
|
// the body contains the code-completion point.
|
|
|
|
TentativeParsingAction PA(*this);
|
|
|
|
ConsumeBrace();
|
|
|
|
if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
|
|
|
|
/*StopAtCodeCompletion=*/true)) {
|
|
|
|
PA.Commit();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
PA.Revert();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-12-22 00:41:36 +08:00
|
|
|
/// ParseCXXTryBlock - Parse a C++ try-block.
|
|
|
|
///
|
|
|
|
/// try-block:
|
|
|
|
/// 'try' compound-statement handler-seq
|
|
|
|
///
|
2010-12-24 10:08:15 +08:00
|
|
|
StmtResult Parser::ParseCXXTryBlock(ParsedAttributes &attrs) {
|
2009-11-21 16:43:09 +08:00
|
|
|
// FIXME: Add attributes?
|
2010-02-11 10:19:13 +08:00
|
|
|
|
2008-12-22 00:41:36 +08:00
|
|
|
assert(Tok.is(tok::kw_try) && "Expected 'try'");
|
|
|
|
|
|
|
|
SourceLocation TryLoc = ConsumeToken();
|
2009-04-27 04:35:05 +08:00
|
|
|
return ParseCXXTryBlockCommon(TryLoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseCXXTryBlockCommon - Parse the common part of try-block and
|
|
|
|
/// function-try-block.
|
|
|
|
///
|
|
|
|
/// try-block:
|
|
|
|
/// 'try' compound-statement handler-seq
|
|
|
|
///
|
|
|
|
/// function-try-block:
|
|
|
|
/// 'try' ctor-initializer[opt] compound-statement handler-seq
|
|
|
|
///
|
|
|
|
/// handler-seq:
|
|
|
|
/// handler handler-seq[opt]
|
|
|
|
///
|
2011-04-28 09:08:34 +08:00
|
|
|
/// [Borland] try-block:
|
|
|
|
/// 'try' compound-statement seh-except-block
|
|
|
|
/// 'try' compound-statment seh-finally-block
|
|
|
|
///
|
2010-08-24 14:29:42 +08:00
|
|
|
StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
|
2008-12-22 00:41:36 +08:00
|
|
|
if (Tok.isNot(tok::l_brace))
|
|
|
|
return StmtError(Diag(Tok, diag::err_expected_lbrace));
|
2009-11-21 16:43:09 +08:00
|
|
|
// FIXME: Possible draft standard bug: attribute-specifier should be allowed?
|
2011-03-24 19:26:52 +08:00
|
|
|
ParsedAttributesWithRange attrs(AttrFactory);
|
2011-07-07 06:04:06 +08:00
|
|
|
StmtResult TryBlock(ParseCompoundStatement(attrs, /*isStmtExpr=*/false,
|
|
|
|
Scope::DeclScope|Scope::TryScope));
|
2008-12-22 00:41:36 +08:00
|
|
|
if (TryBlock.isInvalid())
|
|
|
|
return move(TryBlock);
|
|
|
|
|
2011-04-28 09:08:34 +08:00
|
|
|
// Borland allows SEH-handlers with 'try'
|
|
|
|
if(Tok.is(tok::kw___except) || Tok.is(tok::kw___finally)) {
|
|
|
|
// TODO: Factor into common return ParseSEHHandlerCommon(...)
|
|
|
|
StmtResult Handler;
|
|
|
|
if(Tok.is(tok::kw___except)) {
|
|
|
|
SourceLocation Loc = ConsumeToken();
|
|
|
|
Handler = ParseSEHExceptBlock(Loc);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
SourceLocation Loc = ConsumeToken();
|
|
|
|
Handler = ParseSEHFinallyBlock(Loc);
|
|
|
|
}
|
|
|
|
if(Handler.isInvalid())
|
|
|
|
return move(Handler);
|
|
|
|
|
|
|
|
return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
|
|
|
|
TryLoc,
|
|
|
|
TryBlock.take(),
|
|
|
|
Handler.take());
|
2008-12-22 00:41:36 +08:00
|
|
|
}
|
2011-04-28 09:08:34 +08:00
|
|
|
else {
|
|
|
|
StmtVector Handlers(Actions);
|
|
|
|
MaybeParseCXX0XAttributes(attrs);
|
|
|
|
ProhibitAttributes(attrs);
|
|
|
|
|
|
|
|
if (Tok.isNot(tok::kw_catch))
|
|
|
|
return StmtError(Diag(Tok, diag::err_expected_catch));
|
|
|
|
while (Tok.is(tok::kw_catch)) {
|
|
|
|
StmtResult Handler(ParseCXXCatchBlock());
|
|
|
|
if (!Handler.isInvalid())
|
|
|
|
Handlers.push_back(Handler.release());
|
|
|
|
}
|
|
|
|
// Don't bother creating the full statement if we don't have any usable
|
|
|
|
// handlers.
|
|
|
|
if (Handlers.empty())
|
|
|
|
return StmtError();
|
2008-12-22 00:41:36 +08:00
|
|
|
|
2011-04-28 09:08:34 +08:00
|
|
|
return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
|
|
|
|
}
|
2008-12-22 00:41:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
|
|
|
|
///
|
|
|
|
/// handler:
|
|
|
|
/// 'catch' '(' exception-declaration ')' compound-statement
|
|
|
|
///
|
|
|
|
/// exception-declaration:
|
|
|
|
/// type-specifier-seq declarator
|
|
|
|
/// type-specifier-seq abstract-declarator
|
|
|
|
/// type-specifier-seq
|
|
|
|
/// '...'
|
|
|
|
///
|
2010-08-24 14:29:42 +08:00
|
|
|
StmtResult Parser::ParseCXXCatchBlock() {
|
2008-12-22 00:41:36 +08:00
|
|
|
assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
|
|
|
|
|
|
|
|
SourceLocation CatchLoc = ConsumeToken();
|
|
|
|
|
|
|
|
SourceLocation LParenLoc = Tok.getLocation();
|
|
|
|
if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
|
|
|
|
return StmtError();
|
|
|
|
|
|
|
|
// C++ 3.3.2p3:
|
|
|
|
// The name in a catch exception-declaration is local to the handler and
|
|
|
|
// shall not be redeclared in the outermost block of the handler.
|
|
|
|
ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
|
|
|
|
|
|
|
|
// exception-declaration is equivalent to '...' or a parameter-declaration
|
|
|
|
// without default arguments.
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *ExceptionDecl = 0;
|
2008-12-22 00:41:36 +08:00
|
|
|
if (Tok.isNot(tok::ellipsis)) {
|
2011-03-24 19:26:52 +08:00
|
|
|
DeclSpec DS(AttrFactory);
|
2008-12-23 03:15:10 +08:00
|
|
|
if (ParseCXXTypeSpecifierSeq(DS))
|
|
|
|
return StmtError();
|
2008-12-22 00:41:36 +08:00
|
|
|
Declarator ExDecl(DS, Declarator::CXXCatchContext);
|
|
|
|
ParseDeclarator(ExDecl);
|
2010-07-03 01:43:08 +08:00
|
|
|
ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
|
2008-12-22 00:41:36 +08:00
|
|
|
} else
|
|
|
|
ConsumeToken();
|
|
|
|
|
|
|
|
if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
|
|
|
|
return StmtError();
|
|
|
|
|
|
|
|
if (Tok.isNot(tok::l_brace))
|
|
|
|
return StmtError(Diag(Tok, diag::err_expected_lbrace));
|
|
|
|
|
2009-11-21 16:43:09 +08:00
|
|
|
// FIXME: Possible draft standard bug: attribute-specifier should be allowed?
|
2011-03-24 19:26:52 +08:00
|
|
|
ParsedAttributes attrs(AttrFactory);
|
2010-12-24 10:08:15 +08:00
|
|
|
StmtResult Block(ParseCompoundStatement(attrs));
|
2008-12-22 00:41:36 +08:00
|
|
|
if (Block.isInvalid())
|
|
|
|
return move(Block);
|
|
|
|
|
2010-08-24 07:25:46 +08:00
|
|
|
return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
|
2008-12-22 00:41:36 +08:00
|
|
|
}
|
2011-05-07 04:48:22 +08:00
|
|
|
|
|
|
|
void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
|
2011-05-08 01:30:27 +08:00
|
|
|
bool Result;
|
|
|
|
if (ParseMicrosoftIfExistsCondition(Result))
|
2011-05-07 04:48:22 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (Tok.isNot(tok::l_brace)) {
|
|
|
|
Diag(Tok, diag::err_expected_lbrace);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ConsumeBrace();
|
|
|
|
|
2011-05-08 01:30:27 +08:00
|
|
|
// Condition is false skip all inside the {}.
|
|
|
|
if (!Result) {
|
2011-05-07 04:48:22 +08:00
|
|
|
SkipUntil(tok::r_brace, false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Condition is true, parse the statements.
|
|
|
|
while (Tok.isNot(tok::r_brace)) {
|
|
|
|
StmtResult R = ParseStatementOrDeclaration(Stmts, false);
|
|
|
|
if (R.isUsable())
|
|
|
|
Stmts.push_back(R.release());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Tok.isNot(tok::r_brace)) {
|
|
|
|
Diag(Tok, diag::err_expected_rbrace);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ConsumeBrace();
|
|
|
|
}
|