blob: 13b3bdaf7495efe63022ce1b7204c00794ae07c2 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000016#include "RAIIObjectsForParser.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
John McCallf312b1e2010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/Scope.h"
Chris Lattnerae50fa02009-03-05 00:00:31 +000020#include "clang/Basic/Diagnostic.h"
21#include "clang/Basic/PrettyStackTrace.h"
22#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// C99 6.8: Statements and Blocks.
27//===----------------------------------------------------------------------===//
28
29/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
30/// StatementOrDeclaration:
31/// statement
32/// declaration
33///
34/// statement:
35/// labeled-statement
36/// compound-statement
37/// expression-statement
38/// selection-statement
39/// iteration-statement
40/// jump-statement
Argyrios Kyrtzidisdcdd55f2008-09-07 18:58:01 +000041/// [C++] declaration-statement
Sebastian Redla0fd8652008-12-21 16:41:36 +000042/// [C++] try-block
John Wiegley28bbe4b2011-04-28 01:08:34 +000043/// [MS] seh-try-block
Fariborz Jahanianb384d322007-10-04 20:19:06 +000044/// [OBC] objc-throw-statement
45/// [OBC] objc-try-catch-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +000046/// [OBC] objc-synchronized-statement
Reid Spencer5f016e22007-07-11 17:01:13 +000047/// [GNU] asm-statement
48/// [OMP] openmp-construct [TODO]
49///
50/// labeled-statement:
51/// identifier ':' statement
52/// 'case' constant-expression ':' statement
53/// 'default' ':' statement
54///
55/// selection-statement:
56/// if-statement
57/// switch-statement
58///
59/// iteration-statement:
60/// while-statement
61/// do-statement
62/// for-statement
63///
64/// expression-statement:
65/// expression[opt] ';'
66///
67/// jump-statement:
68/// 'goto' identifier ';'
69/// 'continue' ';'
70/// 'break' ';'
71/// 'return' expression[opt] ';'
72/// [GNU] 'goto' '*' expression ';'
73///
Fariborz Jahanianb384d322007-10-04 20:19:06 +000074/// [OBC] objc-throw-statement:
75/// [OBC] '@' 'throw' expression ';'
Mike Stump1eb44332009-09-09 15:08:12 +000076/// [OBC] '@' 'throw' ';'
77///
John McCall60d7b3a2010-08-24 06:29:42 +000078StmtResult
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +000079Parser::ParseStatementOrDeclaration(StmtVector &Stmts, bool OnlyStatement) {
Reid Spencer5f016e22007-07-11 17:01:13 +000080 const char *SemiError = 0;
John McCall60d7b3a2010-08-24 06:29:42 +000081 StmtResult Res;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +000082
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +000083 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +000084
John McCall0b7e6782011-03-24 11:26:52 +000085 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +000086 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +000087
Reid Spencer5f016e22007-07-11 17:01:13 +000088 // Cases in this switch statement should fall through if the parser expects
89 // the token to end in a semicolon (in which case SemiError should be set),
90 // or they directly 'return;' if not.
Douglas Gregor312eadb2011-04-24 05:37:28 +000091Retry:
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000092 tok::TokenKind Kind = Tok.getKind();
93 SourceLocation AtLoc;
94 switch (Kind) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000095 case tok::at: // May be a @try or @throw statement
96 {
97 AtLoc = ConsumeToken(); // consume @
Sebastian Redl43bc2a02008-12-11 20:12:42 +000098 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +000099 }
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000100
Douglas Gregor791215b2009-09-21 20:51:25 +0000101 case tok::code_completion:
John McCallf312b1e2010-08-26 23:41:50 +0000102 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000103 cutOffParsing();
104 return StmtError();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000105
Douglas Gregor312eadb2011-04-24 05:37:28 +0000106 case tok::identifier: {
107 Token Next = NextToken();
108 if (Next.is(tok::colon)) { // C99 6.8.1: labeled-statement
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +0000109 // identifier ':' statement
John McCall7f040a92010-12-24 02:08:15 +0000110 return ParseLabeledStatement(attrs);
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +0000111 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000112
Douglas Gregor3b887352011-04-27 04:48:22 +0000113 if (Next.isNot(tok::coloncolon)) {
Douglas Gregor312eadb2011-04-24 05:37:28 +0000114 CXXScopeSpec SS;
115 IdentifierInfo *Name = Tok.getIdentifierInfo();
116 SourceLocation NameLoc = Tok.getLocation();
Richard Trieu950be712011-09-19 19:01:00 +0000117
118 if (getLang().CPlusPlus)
119 CheckForTemplateAndDigraph(Next, ParsedType(),
120 /*EnteringContext=*/false, *Name, SS);
121
Douglas Gregor312eadb2011-04-24 05:37:28 +0000122 Sema::NameClassification Classification
123 = Actions.ClassifyName(getCurScope(), SS, Name, NameLoc, Next);
124 switch (Classification.getKind()) {
125 case Sema::NC_Keyword:
126 // The identifier was corrected to a keyword. Update the token
127 // to this keyword, and try again.
128 if (Name->getTokenID() != tok::identifier) {
129 Tok.setIdentifierInfo(Name);
130 Tok.setKind(Name->getTokenID());
131 goto Retry;
132 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000133
Douglas Gregor312eadb2011-04-24 05:37:28 +0000134 // Fall through via the normal error path.
135 // FIXME: This seems like it could only happen for context-sensitive
136 // keywords.
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000137
Douglas Gregor312eadb2011-04-24 05:37:28 +0000138 case Sema::NC_Error:
139 // Handle errors here by skipping up to the next semicolon or '}', and
140 // eat the semicolon if that's what stopped us.
141 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
142 if (Tok.is(tok::semi))
143 ConsumeToken();
144 return StmtError();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000145
Douglas Gregor312eadb2011-04-24 05:37:28 +0000146 case Sema::NC_Unknown:
147 // Either we don't know anything about this identifier, or we know that
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000148 // we're in a syntactic context we haven't handled yet.
149 break;
150
Douglas Gregord9d75e52011-04-27 05:41:15 +0000151 case Sema::NC_Type:
Douglas Gregor3b887352011-04-27 04:48:22 +0000152 Tok.setKind(tok::annot_typename);
153 setTypeAnnotation(Tok, Classification.getType());
154 Tok.setAnnotationEndLoc(NameLoc);
Douglas Gregor3b887352011-04-27 04:48:22 +0000155 PP.AnnotateCachedTokens(Tok);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000156 break;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000157
Douglas Gregor312eadb2011-04-24 05:37:28 +0000158 case Sema::NC_Expression:
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000159 Tok.setKind(tok::annot_primary_expr);
160 setExprAnnotation(Tok, Classification.getExpression());
161 Tok.setAnnotationEndLoc(NameLoc);
162 PP.AnnotateCachedTokens(Tok);
163 break;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000164
Douglas Gregor312eadb2011-04-24 05:37:28 +0000165 case Sema::NC_TypeTemplate:
166 case Sema::NC_FunctionTemplate: {
167 ConsumeToken(); // the identifier
168 UnqualifiedId Id;
169 Id.setIdentifier(Name, NameLoc);
170 if (AnnotateTemplateIdToken(
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000171 TemplateTy::make(Classification.getTemplateName()),
Douglas Gregor312eadb2011-04-24 05:37:28 +0000172 Classification.getTemplateNameKind(),
Douglas Gregor3b887352011-04-27 04:48:22 +0000173 SS, Id, SourceLocation(),
174 /*AllowTypeAnnotation=*/false)) {
175 // Handle errors here by skipping up to the next semicolon or '}', and
176 // eat the semicolon if that's what stopped us.
177 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
178 if (Tok.is(tok::semi))
179 ConsumeToken();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000180 return StmtError();
Douglas Gregor3b887352011-04-27 04:48:22 +0000181 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000182
183 // If the next token is '::', jump right into parsing a
Douglas Gregor3b887352011-04-27 04:48:22 +0000184 // nested-name-specifier. We don't want to leave the template-id
185 // hanging.
186 if (NextToken().is(tok::coloncolon) && TryAnnotateCXXScopeToken(false)){
Douglas Gregor312eadb2011-04-24 05:37:28 +0000187 // Handle errors here by skipping up to the next semicolon or '}', and
188 // eat the semicolon if that's what stopped us.
189 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
190 if (Tok.is(tok::semi))
191 ConsumeToken();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000192 return StmtError();
Douglas Gregor312eadb2011-04-24 05:37:28 +0000193 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000194
Douglas Gregor312eadb2011-04-24 05:37:28 +0000195 // We've annotated a template-id, so try again now.
196 goto Retry;
197 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000198
Douglas Gregor312eadb2011-04-24 05:37:28 +0000199 case Sema::NC_NestedNameSpecifier:
200 // FIXME: Implement this!
201 break;
202 }
203 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000204
Douglas Gregor312eadb2011-04-24 05:37:28 +0000205 // Fall through
206 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000207
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000208 default: {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000209 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000210 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000211 DeclGroupPtrTy Decl = ParseDeclaration(Stmts, Declarator::BlockContext,
John McCall7f040a92010-12-24 02:08:15 +0000212 DeclEnd, attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000213 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000214 }
215
216 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 Diag(Tok, diag::err_expected_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000218 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 }
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000221 return ParseExprStatement(attrs);
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000222 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000223
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 case tok::kw_case: // C99 6.8.1: labeled-statement
John McCall7f040a92010-12-24 02:08:15 +0000225 return ParseCaseStatement(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +0000226 case tok::kw_default: // C99 6.8.1: labeled-statement
John McCall7f040a92010-12-24 02:08:15 +0000227 return ParseDefaultStatement(attrs);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000228
Reid Spencer5f016e22007-07-11 17:01:13 +0000229 case tok::l_brace: // C99 6.8.2: compound-statement
John McCall7f040a92010-12-24 02:08:15 +0000230 return ParseCompoundStatement(attrs);
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000231 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
Argyrios Kyrtzidise2ca8282011-09-01 21:53:45 +0000232 bool HasLeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
233 return Actions.ActOnNullStmt(ConsumeToken(), HasLeadingEmptyMacro);
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000234 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000235
Reid Spencer5f016e22007-07-11 17:01:13 +0000236 case tok::kw_if: // C99 6.8.4.1: if-statement
John McCall7f040a92010-12-24 02:08:15 +0000237 return ParseIfStatement(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +0000238 case tok::kw_switch: // C99 6.8.4.2: switch-statement
John McCall7f040a92010-12-24 02:08:15 +0000239 return ParseSwitchStatement(attrs);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000240
Reid Spencer5f016e22007-07-11 17:01:13 +0000241 case tok::kw_while: // C99 6.8.5.1: while-statement
John McCall7f040a92010-12-24 02:08:15 +0000242 return ParseWhileStatement(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 case tok::kw_do: // C99 6.8.5.2: do-statement
John McCall7f040a92010-12-24 02:08:15 +0000244 Res = ParseDoStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000245 SemiError = "do/while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 break;
247 case tok::kw_for: // C99 6.8.5.3: for-statement
John McCall7f040a92010-12-24 02:08:15 +0000248 return ParseForStatement(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +0000249
250 case tok::kw_goto: // C99 6.8.6.1: goto-statement
John McCall7f040a92010-12-24 02:08:15 +0000251 Res = ParseGotoStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000252 SemiError = "goto";
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 break;
254 case tok::kw_continue: // C99 6.8.6.2: continue-statement
John McCall7f040a92010-12-24 02:08:15 +0000255 Res = ParseContinueStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000256 SemiError = "continue";
Reid Spencer5f016e22007-07-11 17:01:13 +0000257 break;
258 case tok::kw_break: // C99 6.8.6.3: break-statement
John McCall7f040a92010-12-24 02:08:15 +0000259 Res = ParseBreakStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000260 SemiError = "break";
Reid Spencer5f016e22007-07-11 17:01:13 +0000261 break;
262 case tok::kw_return: // C99 6.8.6.4: return-statement
John McCall7f040a92010-12-24 02:08:15 +0000263 Res = ParseReturnStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000264 SemiError = "return";
Reid Spencer5f016e22007-07-11 17:01:13 +0000265 break;
Sebastian Redl61364dd2008-12-11 19:30:53 +0000266
Sebastian Redla0fd8652008-12-21 16:41:36 +0000267 case tok::kw_asm: {
John McCall7f040a92010-12-24 02:08:15 +0000268 ProhibitAttributes(attrs);
Steve Naroffd62701b2008-02-07 03:50:06 +0000269 bool msAsm = false;
270 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidisbf8cafa2010-11-02 02:33:08 +0000271 Res = Actions.ActOnFinishFullStmt(Res.get());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000272 if (msAsm) return move(Res);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000273 SemiError = "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +0000274 break;
275 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000276
Sebastian Redla0fd8652008-12-21 16:41:36 +0000277 case tok::kw_try: // C++ 15: try-block
John McCall7f040a92010-12-24 02:08:15 +0000278 return ParseCXXTryBlock(attrs);
John Wiegley28bbe4b2011-04-28 01:08:34 +0000279
280 case tok::kw___try:
281 return ParseSEHTryBlock(attrs);
Sebastian Redla0fd8652008-12-21 16:41:36 +0000282 }
283
Reid Spencer5f016e22007-07-11 17:01:13 +0000284 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000285 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000286 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000287 } else if (!Res.isInvalid()) {
Chris Lattner7b3684a2009-06-14 00:23:56 +0000288 // If the result was valid, then we do want to diagnose this. Use
289 // ExpectAndConsume to emit the diagnostic, even though we know it won't
290 // succeed.
291 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner19504402008-11-13 18:52:53 +0000292 // Skip until we see a } or ;, but don't eat it.
293 SkipUntil(tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000294 }
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Sebastian Redl61364dd2008-12-11 19:30:53 +0000296 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000297}
298
Douglas Gregor312eadb2011-04-24 05:37:28 +0000299/// \brief Parse an expression statement.
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000300StmtResult Parser::ParseExprStatement(ParsedAttributes &Attrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +0000301 // If a case keyword is missing, this is where it should be inserted.
302 Token OldToken = Tok;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000303
Douglas Gregor312eadb2011-04-24 05:37:28 +0000304 // FIXME: Use the attributes
305 // expression[opt] ';'
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000306 ExprResult Expr(ParseExpression());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000307 if (Expr.isInvalid()) {
308 // If the expression is invalid, skip ahead to the next semicolon or '}'.
309 // Not doing this opens us up to the possibility of infinite loops if
310 // ParseExpression does not consume any tokens.
311 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
312 if (Tok.is(tok::semi))
313 ConsumeToken();
314 return StmtError();
315 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000316
Douglas Gregor312eadb2011-04-24 05:37:28 +0000317 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
318 Actions.CheckCaseExpression(Expr.get())) {
319 // If a constant expression is followed by a colon inside a switch block,
320 // suggest a missing case keyword.
321 Diag(OldToken, diag::err_expected_case_before_expression)
322 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000323
Douglas Gregor312eadb2011-04-24 05:37:28 +0000324 // Recover parsing as a case statement.
325 return ParseCaseStatement(Attrs, /*MissingCase=*/true, Expr);
326 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000327
Douglas Gregor312eadb2011-04-24 05:37:28 +0000328 // Otherwise, eat the semicolon.
329 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
330 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr.get()));
John Wiegley28bbe4b2011-04-28 01:08:34 +0000331}
Douglas Gregor312eadb2011-04-24 05:37:28 +0000332
John Wiegley28bbe4b2011-04-28 01:08:34 +0000333StmtResult Parser::ParseSEHTryBlock(ParsedAttributes & Attrs) {
334 assert(Tok.is(tok::kw___try) && "Expected '__try'");
335 SourceLocation Loc = ConsumeToken();
336 return ParseSEHTryBlockCommon(Loc);
337}
338
339/// ParseSEHTryBlockCommon
340///
341/// seh-try-block:
342/// '__try' compound-statement seh-handler
343///
344/// seh-handler:
345/// seh-except-block
346/// seh-finally-block
347///
348StmtResult Parser::ParseSEHTryBlockCommon(SourceLocation TryLoc) {
349 if(Tok.isNot(tok::l_brace))
350 return StmtError(Diag(Tok,diag::err_expected_lbrace));
351
352 ParsedAttributesWithRange attrs(AttrFactory);
353 StmtResult TryBlock(ParseCompoundStatement(attrs));
354 if(TryBlock.isInvalid())
355 return move(TryBlock);
356
357 StmtResult Handler;
Douglas Gregorb57791e2011-10-21 03:57:52 +0000358 if (Tok.is(tok::identifier) &&
359 Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley28bbe4b2011-04-28 01:08:34 +0000360 SourceLocation Loc = ConsumeToken();
361 Handler = ParseSEHExceptBlock(Loc);
362 } else if (Tok.is(tok::kw___finally)) {
363 SourceLocation Loc = ConsumeToken();
364 Handler = ParseSEHFinallyBlock(Loc);
365 } else {
366 return StmtError(Diag(Tok,diag::err_seh_expected_handler));
367 }
368
369 if(Handler.isInvalid())
370 return move(Handler);
371
372 return Actions.ActOnSEHTryBlock(false /* IsCXXTry */,
373 TryLoc,
374 TryBlock.take(),
375 Handler.take());
376}
377
378/// ParseSEHExceptBlock - Handle __except
379///
380/// seh-except-block:
381/// '__except' '(' seh-filter-expression ')' compound-statement
382///
383StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) {
384 PoisonIdentifierRAIIObject raii(Ident__exception_code, false),
385 raii2(Ident___exception_code, false),
386 raii3(Ident_GetExceptionCode, false);
387
388 if(ExpectAndConsume(tok::l_paren,diag::err_expected_lparen))
389 return StmtError();
390
391 ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope);
392
Francois Pichetd7f02df2011-04-28 03:14:31 +0000393 if (getLang().Borland) {
394 Ident__exception_info->setIsPoisoned(false);
395 Ident___exception_info->setIsPoisoned(false);
396 Ident_GetExceptionInfo->setIsPoisoned(false);
397 }
John Wiegley28bbe4b2011-04-28 01:08:34 +0000398 ExprResult FilterExpr(ParseExpression());
Francois Pichetd7f02df2011-04-28 03:14:31 +0000399
400 if (getLang().Borland) {
401 Ident__exception_info->setIsPoisoned(true);
402 Ident___exception_info->setIsPoisoned(true);
403 Ident_GetExceptionInfo->setIsPoisoned(true);
404 }
John Wiegley28bbe4b2011-04-28 01:08:34 +0000405
406 if(FilterExpr.isInvalid())
407 return StmtError();
408
409 if(ExpectAndConsume(tok::r_paren,diag::err_expected_rparen))
410 return StmtError();
411
412 ParsedAttributesWithRange attrs(AttrFactory);
413 StmtResult Block(ParseCompoundStatement(attrs));
414
415 if(Block.isInvalid())
416 return move(Block);
417
418 return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.take(), Block.take());
419}
420
421/// ParseSEHFinallyBlock - Handle __finally
422///
423/// seh-finally-block:
424/// '__finally' compound-statement
425///
426StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyBlock) {
427 PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
428 raii2(Ident___abnormal_termination, false),
429 raii3(Ident_AbnormalTermination, false);
430
431 ParsedAttributesWithRange attrs(AttrFactory);
432 StmtResult Block(ParseCompoundStatement(attrs));
433 if(Block.isInvalid())
434 return move(Block);
435
436 return Actions.ActOnSEHFinallyBlock(FinallyBlock,Block.take());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000437}
438
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000439/// ParseLabeledStatement - We have an identifier and a ':' after it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000440///
441/// labeled-statement:
442/// identifier ':' statement
443/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000444///
John McCall7f040a92010-12-24 02:08:15 +0000445StmtResult Parser::ParseLabeledStatement(ParsedAttributes &attrs) {
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000446 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
447 "Not an identifier!");
448
449 Token IdentTok = Tok; // Save the whole token.
450 ConsumeToken(); // eat the identifier.
451
452 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000453
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000454 // identifier ':' statement
455 SourceLocation ColonLoc = ConsumeToken();
456
457 // Read label attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +0000458 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000459
John McCall60d7b3a2010-08-24 06:29:42 +0000460 StmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000461
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000462 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000463 if (SubStmt.isInvalid())
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000464 SubStmt = Actions.ActOnNullStmt(ColonLoc);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000465
Chris Lattner337e5502011-02-18 01:27:55 +0000466 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
467 IdentTok.getLocation());
468 if (AttributeList *Attrs = attrs.getList())
469 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000470
Chris Lattner337e5502011-02-18 01:27:55 +0000471 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
472 SubStmt.get());
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000473}
Reid Spencer5f016e22007-07-11 17:01:13 +0000474
475/// ParseCaseStatement
476/// labeled-statement:
477/// 'case' constant-expression ':' statement
478/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
479///
Richard Trieubb9b80c2011-04-21 21:44:26 +0000480StmtResult Parser::ParseCaseStatement(ParsedAttributes &attrs, bool MissingCase,
481 ExprResult Expr) {
Richard Smith46f11102011-04-21 22:48:40 +0000482 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Sean Huntbbd37c62009-11-21 08:43:09 +0000483 // FIXME: Use attributes?
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Chris Lattner24e1e702009-03-04 04:23:07 +0000485 // It is very very common for code to contain many case statements recursively
486 // nested, as in (but usually without indentation):
487 // case 1:
488 // case 2:
489 // case 3:
490 // case 4:
491 // case 5: etc.
492 //
493 // Parsing this naively works, but is both inefficient and can cause us to run
494 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner26140c62009-03-04 18:24:58 +0000495 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner24e1e702009-03-04 04:23:07 +0000496 // but all the grossness is constrained to ParseCaseStatement (and some
497 // wierdness in the actions), so this is just local grossness :).
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Chris Lattner24e1e702009-03-04 04:23:07 +0000499 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
500 // example above.
John McCall60d7b3a2010-08-24 06:29:42 +0000501 StmtResult TopLevelCase(true);
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Chris Lattner24e1e702009-03-04 04:23:07 +0000503 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
504 // gets updated each time a new case is parsed, and whose body is unset so
505 // far. When parsing 'case 4', this is the 'case 3' node.
Richard Trieub2fc6902011-09-09 02:16:15 +0000506 Stmt *DeepestParsedCaseStmt = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Chris Lattner24e1e702009-03-04 04:23:07 +0000508 // While we have case statements, eat and stack them.
David Majnemer0e1e69c2011-06-13 05:50:12 +0000509 SourceLocation ColonLoc;
Chris Lattner24e1e702009-03-04 04:23:07 +0000510 do {
Richard Trieubb9b80c2011-04-21 21:44:26 +0000511 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
512 ConsumeToken(); // eat the 'case'.
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000514 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000515 Actions.CodeCompleteCase(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000516 cutOffParsing();
517 return StmtError();
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000518 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000519
Chris Lattner6fb09c82009-12-10 00:38:54 +0000520 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
521 /// Disable this form of error recovery while we're parsing the case
522 /// expression.
523 ColonProtectionRAIIObject ColonProtection(*this);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000524
Richard Trieubb9b80c2011-04-21 21:44:26 +0000525 ExprResult LHS(MissingCase ? Expr : ParseConstantExpression());
526 MissingCase = false;
Chris Lattner24e1e702009-03-04 04:23:07 +0000527 if (LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000528 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000529 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000530 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000531
Chris Lattner24e1e702009-03-04 04:23:07 +0000532 // GNU case range extension.
533 SourceLocation DotDotDotLoc;
John McCall60d7b3a2010-08-24 06:29:42 +0000534 ExprResult RHS;
Chris Lattner24e1e702009-03-04 04:23:07 +0000535 if (Tok.is(tok::ellipsis)) {
536 Diag(Tok, diag::ext_gnu_case_range);
537 DotDotDotLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000538
Chris Lattner24e1e702009-03-04 04:23:07 +0000539 RHS = ParseConstantExpression();
540 if (RHS.isInvalid()) {
541 SkipUntil(tok::colon);
542 return StmtError();
543 }
544 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000545
Chris Lattner6fb09c82009-12-10 00:38:54 +0000546 ColonProtection.restore();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000547
John McCallf6a3ab02011-01-22 09:28:32 +0000548 if (Tok.is(tok::colon)) {
549 ColonLoc = ConsumeToken();
550
551 // Treat "case blah;" as a typo for "case blah:".
552 } else if (Tok.is(tok::semi)) {
553 ColonLoc = ConsumeToken();
554 Diag(ColonLoc, diag::err_expected_colon_after) << "'case'"
555 << FixItHint::CreateReplacement(ColonLoc, ":");
556 } else {
Douglas Gregor662a4822010-12-23 22:56:40 +0000557 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
558 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'"
559 << FixItHint::CreateInsertion(ExpectedLoc, ":");
560 ColonLoc = ExpectedLoc;
Chris Lattner24e1e702009-03-04 04:23:07 +0000561 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000562
John McCall60d7b3a2010-08-24 06:29:42 +0000563 StmtResult Case =
John McCall9ae2f072010-08-23 23:25:46 +0000564 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
565 RHS.get(), ColonLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Chris Lattner24e1e702009-03-04 04:23:07 +0000567 // If we had a sema error parsing this case, then just ignore it and
568 // continue parsing the sub-stmt.
569 if (Case.isInvalid()) {
570 if (TopLevelCase.isInvalid()) // No parsed case stmts.
571 return ParseStatement();
572 // Otherwise, just don't add it as a nested case.
573 } else {
574 // If this is the first case statement we parsed, it becomes TopLevelCase.
575 // Otherwise we link it into the current chain.
John McCallca0408f2010-08-23 06:44:23 +0000576 Stmt *NextDeepest = Case.get();
Chris Lattner24e1e702009-03-04 04:23:07 +0000577 if (TopLevelCase.isInvalid())
578 TopLevelCase = move(Case);
579 else
John McCall9ae2f072010-08-23 23:25:46 +0000580 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner24e1e702009-03-04 04:23:07 +0000581 DeepestParsedCaseStmt = NextDeepest;
582 }
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Chris Lattner24e1e702009-03-04 04:23:07 +0000584 // Handle all case statements.
585 } while (Tok.is(tok::kw_case));
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Chris Lattner24e1e702009-03-04 04:23:07 +0000587 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump1eb44332009-09-09 15:08:12 +0000588
Chris Lattner24e1e702009-03-04 04:23:07 +0000589 // If we found a non-case statement, start by parsing it.
John McCall60d7b3a2010-08-24 06:29:42 +0000590 StmtResult SubStmt;
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Chris Lattner24e1e702009-03-04 04:23:07 +0000592 if (Tok.isNot(tok::r_brace)) {
593 SubStmt = ParseStatement();
594 } else {
595 // Nicely diagnose the common error "switch (X) { case 4: }", which is
596 // not valid.
David Majnemer63f04ab2011-06-14 15:24:38 +0000597 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
598 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement);
Chris Lattner24e1e702009-03-04 04:23:07 +0000599 SubStmt = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000600 }
Mike Stump1eb44332009-09-09 15:08:12 +0000601
Chris Lattner24e1e702009-03-04 04:23:07 +0000602 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000603 if (SubStmt.isInvalid())
Chris Lattner24e1e702009-03-04 04:23:07 +0000604 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Chris Lattner24e1e702009-03-04 04:23:07 +0000606 // Install the body into the most deeply-nested case.
John McCall9ae2f072010-08-23 23:25:46 +0000607 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000608
Chris Lattner24e1e702009-03-04 04:23:07 +0000609 // Return the top level parsed statement tree.
Chris Lattner26140c62009-03-04 18:24:58 +0000610 return move(TopLevelCase);
Reid Spencer5f016e22007-07-11 17:01:13 +0000611}
612
613/// ParseDefaultStatement
614/// labeled-statement:
615/// 'default' ':' statement
616/// Note that this does not parse the 'statement' at the end.
617///
John McCall7f040a92010-12-24 02:08:15 +0000618StmtResult Parser::ParseDefaultStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000619 //FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000620
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000621 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000622 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
623
Douglas Gregor662a4822010-12-23 22:56:40 +0000624 SourceLocation ColonLoc;
John McCallf6a3ab02011-01-22 09:28:32 +0000625 if (Tok.is(tok::colon)) {
626 ColonLoc = ConsumeToken();
627
628 // Treat "default;" as a typo for "default:".
629 } else if (Tok.is(tok::semi)) {
630 ColonLoc = ConsumeToken();
631 Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
632 << FixItHint::CreateReplacement(ColonLoc, ":");
633 } else {
Douglas Gregor662a4822010-12-23 22:56:40 +0000634 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
635 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
636 << FixItHint::CreateInsertion(ExpectedLoc, ":");
637 ColonLoc = ExpectedLoc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000638 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000639
Reid Spencer5f016e22007-07-11 17:01:13 +0000640 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000641 if (Tok.is(tok::r_brace)) {
David Majnemer63f04ab2011-06-14 15:24:38 +0000642 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
643 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000644 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000645 }
646
John McCall60d7b3a2010-08-24 06:29:42 +0000647 StmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000648 if (SubStmt.isInvalid())
Sebastian Redl61364dd2008-12-11 19:30:53 +0000649 return StmtError();
650
Sebastian Redl117054a2008-12-28 16:13:43 +0000651 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000652 SubStmt.get(), getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +0000653}
654
Douglas Gregorbca01b42011-07-06 22:04:06 +0000655StmtResult Parser::ParseCompoundStatement(ParsedAttributes &Attr,
656 bool isStmtExpr) {
657 return ParseCompoundStatement(Attr, isStmtExpr, Scope::DeclScope);
658}
Reid Spencer5f016e22007-07-11 17:01:13 +0000659
660/// ParseCompoundStatement - Parse a "{}" block.
661///
662/// compound-statement: [C99 6.8.2]
663/// { block-item-list[opt] }
664/// [GNU] { label-declarations block-item-list } [TODO]
665///
666/// block-item-list:
667/// block-item
668/// block-item-list block-item
669///
670/// block-item:
671/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000672/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000673/// statement
674/// [OMP] openmp-directive [TODO]
675///
676/// [GNU] label-declarations:
677/// [GNU] label-declaration
678/// [GNU] label-declarations label-declaration
679///
680/// [GNU] label-declaration:
681/// [GNU] '__label__' identifier-list ';'
682///
683/// [OMP] openmp-directive: [TODO]
684/// [OMP] barrier-directive
685/// [OMP] flush-directive
686///
John McCall7f040a92010-12-24 02:08:15 +0000687StmtResult Parser::ParseCompoundStatement(ParsedAttributes &attrs,
Douglas Gregorbca01b42011-07-06 22:04:06 +0000688 bool isStmtExpr,
689 unsigned ScopeFlags) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000690 //FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000691
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000692 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000693
Chris Lattner31e05722007-08-26 06:24:45 +0000694 // Enter a scope to hold everything within the compound stmt. Compound
695 // statements can always hold declarations.
Douglas Gregorbca01b42011-07-06 22:04:06 +0000696 ParseScope CompoundScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000697
698 // Parse the statements in the body.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000699 return ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000700}
701
702
703/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000704/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000705/// consume the '}' at the end of the block. It does not manipulate the scope
706/// stack.
John McCall60d7b3a2010-08-24 06:29:42 +0000707StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump1eb44332009-09-09 15:08:12 +0000708 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerae50fa02009-03-05 00:00:31 +0000709 Tok.getLocation(),
710 "in compound statement ('{}')");
Douglas Gregor0fbda682010-09-15 14:51:05 +0000711 InMessageExpressionRAIIObject InMessage(*this, false);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000712 BalancedDelimiterTracker T(*this, tok::l_brace);
713 if (T.consumeOpen())
714 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000715
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000716 StmtVector Stmts(Actions);
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000717
Chris Lattner4ae493c2011-02-18 02:08:43 +0000718 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
719 // only allowed at the start of a compound stmt regardless of the language.
720 while (Tok.is(tok::kw___label__)) {
721 SourceLocation LabelLoc = ConsumeToken();
722 Diag(LabelLoc, diag::ext_gnu_local_label);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000723
Chris Lattner5f9e2722011-07-23 10:55:15 +0000724 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4ae493c2011-02-18 02:08:43 +0000725 while (1) {
726 if (Tok.isNot(tok::identifier)) {
727 Diag(Tok, diag::err_expected_ident);
728 break;
729 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000730
Chris Lattner4ae493c2011-02-18 02:08:43 +0000731 IdentifierInfo *II = Tok.getIdentifierInfo();
732 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara67843042011-03-05 18:21:20 +0000733 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000734
Chris Lattner4ae493c2011-02-18 02:08:43 +0000735 if (!Tok.is(tok::comma))
736 break;
737 ConsumeToken();
738 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000739
John McCall0b7e6782011-03-24 11:26:52 +0000740 DeclSpec DS(AttrFactory);
Chris Lattner4ae493c2011-02-18 02:08:43 +0000741 DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
742 DeclsInGroup.data(), DeclsInGroup.size());
743 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000744
Chris Lattner4ae493c2011-02-18 02:08:43 +0000745 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
746 if (R.isUsable())
747 Stmts.push_back(R.release());
748 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000749
Chris Lattner4ae493c2011-02-18 02:08:43 +0000750 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000751 if (Tok.is(tok::annot_pragma_unused)) {
752 HandlePragmaUnused();
753 continue;
754 }
755
Francois Pichet62ec1f22011-09-17 17:15:52 +0000756 if (getLang().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet1e862692011-05-06 20:48:22 +0000757 Tok.is(tok::kw___if_not_exists))) {
758 ParseMicrosoftIfExistsStatement(Stmts);
759 continue;
760 }
761
John McCall60d7b3a2010-08-24 06:29:42 +0000762 StmtResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000763 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000764 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattner45a566c2007-08-27 01:01:57 +0000765 } else {
766 // __extension__ can start declarations and it can also be a unary
767 // operator for expressions. Consume multiple __extension__ markers here
768 // until we can determine which is which.
Eli Friedmanadf077f2009-01-27 08:43:38 +0000769 // FIXME: This loses extension expressions in the AST!
Chris Lattner45a566c2007-08-27 01:01:57 +0000770 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000771 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000772 ConsumeToken();
Chris Lattner39146d62008-10-20 06:51:33 +0000773
John McCall0b7e6782011-03-24 11:26:52 +0000774 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000775 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000776
Chris Lattner45a566c2007-08-27 01:01:57 +0000777 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000778 if (isDeclarationStatement()) {
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000779 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000780 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000781 ExtensionRAIIObject O(Diags);
782
Chris Lattner97144fc2009-04-02 04:16:50 +0000783 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000784 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
785 Declarator::BlockContext, DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000786 attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000787 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattner45a566c2007-08-27 01:01:57 +0000788 } else {
Eli Friedmanadf077f2009-01-27 08:43:38 +0000789 // Otherwise this was a unary __extension__ marker.
John McCall60d7b3a2010-08-24 06:29:42 +0000790 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattner043a0b52008-03-13 06:32:11 +0000791
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000792 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000793 SkipUntil(tok::semi);
794 continue;
795 }
Sebastian Redlf512e822009-01-18 18:03:53 +0000796
Sean Huntbbd37c62009-11-21 08:43:09 +0000797 // FIXME: Use attributes?
Chris Lattner39146d62008-10-20 06:51:33 +0000798 // Eat the semicolon at the end of stmt and convert the expr into a
799 // statement.
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000800 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +0000801 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattner45a566c2007-08-27 01:01:57 +0000802 }
803 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000804
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000805 if (R.isUsable())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000806 Stmts.push_back(R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000807 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000808
Reid Spencer5f016e22007-07-11 17:01:13 +0000809 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000810 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000811 Diag(Tok, diag::err_expected_rbrace);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000812 Diag(T.getOpenLocation(), diag::note_matching) << "{";
Sebastian Redl61364dd2008-12-11 19:30:53 +0000813 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000814 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000815
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000816 if (T.consumeClose())
817 return StmtError();
818
819 return Actions.ActOnCompoundStmt(T.getOpenLocation(), T.getCloseLocation(),
820 move_arg(Stmts), isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000821}
822
Chris Lattner15ff1112008-12-12 06:31:07 +0000823/// ParseParenExprOrCondition:
824/// [C ] '(' expression ')'
Chris Lattnerff871fb2008-12-12 06:35:28 +0000825/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattner15ff1112008-12-12 06:31:07 +0000826///
827/// This function parses and performs error recovery on the specified condition
828/// or expression (depending on whether we're in C++ or C mode). This function
829/// goes out of its way to recover well. It returns true if there was a parser
830/// error (the right paren couldn't be found), which indicates that the caller
831/// should try to recover harder. It returns false if the condition is
832/// successfully parsed. Note that a successful parse can still have semantic
833/// errors in the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000834bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCalld226f652010-08-21 09:40:31 +0000835 Decl *&DeclResult,
Douglas Gregor586596f2010-05-06 17:25:47 +0000836 SourceLocation Loc,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000837 bool ConvertToBoolean) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000838 BalancedDelimiterTracker T(*this, tok::l_paren);
839 T.consumeOpen();
840
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000841 if (getLang().CPlusPlus)
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000842 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000843 else {
844 ExprResult = ParseExpression();
John McCalld226f652010-08-21 09:40:31 +0000845 DeclResult = 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000846
Douglas Gregor586596f2010-05-06 17:25:47 +0000847 // If required, convert to a boolean value.
848 if (!ExprResult.isInvalid() && ConvertToBoolean)
849 ExprResult
John McCall9ae2f072010-08-23 23:25:46 +0000850 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000851 }
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Chris Lattner15ff1112008-12-12 06:31:07 +0000853 // If the parser was confused by the condition and we don't have a ')', try to
854 // recover by skipping ahead to a semi and bailing out. If condexp is
855 // semantically invalid but we have well formed code, keep going.
John McCalld226f652010-08-21 09:40:31 +0000856 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000857 SkipUntil(tok::semi);
858 // Skipping may have stopped if it found the containing ')'. If so, we can
859 // continue parsing the if statement.
860 if (Tok.isNot(tok::r_paren))
861 return true;
862 }
Mike Stump1eb44332009-09-09 15:08:12 +0000863
Chris Lattner15ff1112008-12-12 06:31:07 +0000864 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000865 T.consumeClose();
Chris Lattner15ff1112008-12-12 06:31:07 +0000866 return false;
867}
868
869
Reid Spencer5f016e22007-07-11 17:01:13 +0000870/// ParseIfStatement
871/// if-statement: [C99 6.8.4.1]
872/// 'if' '(' expression ')' statement
873/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000874/// [C++] 'if' '(' condition ')' statement
875/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000876///
John McCall7f040a92010-12-24 02:08:15 +0000877StmtResult Parser::ParseIfStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000878 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000879
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000880 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000881 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
882
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000883 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000884 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000885 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000886 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000887 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000888
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000889 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
890
Chris Lattner22153252007-08-26 23:08:06 +0000891 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
892 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000893 //
894 // C++ 6.4p3:
895 // A name introduced by a declaration in a condition is in scope from its
896 // point of declaration until the end of the substatements controlled by the
897 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000898 // C++ 3.3.2p4:
899 // Names declared in the for-init-statement, and in the condition of if,
900 // while, for, and switch statements are local to the if, while, for, or
901 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000902 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000903 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +0000904
Reid Spencer5f016e22007-07-11 17:01:13 +0000905 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000906 ExprResult CondExp;
John McCalld226f652010-08-21 09:40:31 +0000907 Decl *CondVar = 0;
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000908 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +0000909 return StmtError();
Chris Lattner18914bc2008-12-12 06:19:11 +0000910
John McCall9ae2f072010-08-23 23:25:46 +0000911 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Chris Lattner0ecea032007-08-22 05:28:50 +0000913 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000914 // there is no compound stmt. C90 does not have this clause. We only do this
915 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000916 //
917 // C++ 6.4p1:
918 // The substatement in a selection-statement (each substatement, in the else
919 // form of the if statement) implicitly defines a local scope.
920 //
921 // For C++ we create a scope for the condition and a new scope for
922 // substatements because:
923 // -When the 'then' scope exits, we want the condition declaration to still be
924 // active for the 'else' scope too.
925 // -Sema will detect name clashes by considering declarations of a
926 // 'ControlScope' as part of its direct subscope.
927 // -If we wanted the condition and substatement to be in the same scope, we
928 // would have to notify ParseStatement not to create a new scope. It's
929 // simpler to let it create a new scope.
930 //
Mike Stump1eb44332009-09-09 15:08:12 +0000931 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000932 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000933
Chris Lattnerb96728d2007-10-29 05:08:52 +0000934 // Read the 'then' stmt.
935 SourceLocation ThenStmtLoc = Tok.getLocation();
John McCall60d7b3a2010-08-24 06:29:42 +0000936 StmtResult ThenStmt(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000937
Chris Lattnera36ce712007-08-22 05:16:28 +0000938 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000939 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000940
Reid Spencer5f016e22007-07-11 17:01:13 +0000941 // If it has an else, parse it.
942 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000943 SourceLocation ElseStmtLoc;
John McCall60d7b3a2010-08-24 06:29:42 +0000944 StmtResult ElseStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000945
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000946 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000947 ElseLoc = ConsumeToken();
Chris Lattner966c78b2010-04-12 06:12:50 +0000948 ElseStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000949
Chris Lattner0ecea032007-08-22 05:28:50 +0000950 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000951 // there is no compound stmt. C90 does not have this clause. We only do
952 // this if the body isn't a compound statement to avoid push/pop in common
953 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000954 //
955 // C++ 6.4p1:
956 // The substatement in a selection-statement (each substatement, in the else
957 // form of the if statement) implicitly defines a local scope.
958 //
Sebastian Redl61364dd2008-12-11 19:30:53 +0000959 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000960 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000961
Reid Spencer5f016e22007-07-11 17:01:13 +0000962 ElseStmt = ParseStatement();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000963
Chris Lattnera36ce712007-08-22 05:16:28 +0000964 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000965 InnerScope.Exit();
Douglas Gregord2d8be62011-07-30 08:36:53 +0000966 } else if (Tok.is(tok::code_completion)) {
967 Actions.CodeCompleteAfterIf(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000968 cutOffParsing();
969 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000970 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000971
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000972 IfScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Chris Lattner18914bc2008-12-12 06:19:11 +0000974 // If the condition was invalid, discard the if statement. We could recover
975 // better by replacing it with a valid expr, but don't do that yet.
John McCalld226f652010-08-21 09:40:31 +0000976 if (CondExp.isInvalid() && !CondVar)
Chris Lattner18914bc2008-12-12 06:19:11 +0000977 return StmtError();
Chris Lattner22153252007-08-26 23:08:06 +0000978
Chris Lattnerb96728d2007-10-29 05:08:52 +0000979 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump1eb44332009-09-09 15:08:12 +0000980 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattnerb96728d2007-10-29 05:08:52 +0000981 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000982 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
983 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
984 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000985 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000986 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +0000987 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000988
Chris Lattnerb96728d2007-10-29 05:08:52 +0000989 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000990 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000991 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000992 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000993 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000994
John McCall9ae2f072010-08-23 23:25:46 +0000995 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000996 ElseLoc, ElseStmt.get());
Reid Spencer5f016e22007-07-11 17:01:13 +0000997}
998
999/// ParseSwitchStatement
1000/// switch-statement:
1001/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001002/// [C++] 'switch' '(' condition ')' statement
John McCall7f040a92010-12-24 02:08:15 +00001003StmtResult Parser::ParseSwitchStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001004 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001005
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001006 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001007 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
1008
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001009 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001010 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +00001011 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +00001012 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001013 }
Chris Lattner22153252007-08-26 23:08:06 +00001014
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001015 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
1016
Chris Lattner22153252007-08-26 23:08:06 +00001017 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1018 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001019 //
1020 // C++ 6.4p3:
1021 // A name introduced by a declaration in a condition is in scope from its
1022 // point of declaration until the end of the substatements controlled by the
1023 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001024 // C++ 3.3.2p4:
1025 // Names declared in the for-init-statement, and in the condition of if,
1026 // while, for, and switch statements are local to the if, while, for, or
1027 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001028 //
Richard Trieubb9b80c2011-04-21 21:44:26 +00001029 unsigned ScopeFlags = Scope::BreakScope | Scope::SwitchScope;
Chris Lattner15ff1112008-12-12 06:31:07 +00001030 if (C99orCXX)
1031 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001032 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001033
1034 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +00001035 ExprResult Cond;
John McCalld226f652010-08-21 09:40:31 +00001036 Decl *CondVar = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +00001037 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redl9a920342008-12-11 19:48:14 +00001038 return StmtError();
Eli Friedman2342ef72008-12-17 22:19:57 +00001039
John McCall60d7b3a2010-08-24 06:29:42 +00001040 StmtResult Switch
John McCall9ae2f072010-08-23 23:25:46 +00001041 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001042
Douglas Gregor586596f2010-05-06 17:25:47 +00001043 if (Switch.isInvalid()) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001044 // Skip the switch body.
Douglas Gregor586596f2010-05-06 17:25:47 +00001045 // FIXME: This is not optimal recovery, but parsing the body is more
1046 // dangerous due to the presence of case and default statements, which
1047 // will have no place to connect back with the switch.
Douglas Gregor4186ff42010-05-20 23:20:59 +00001048 if (Tok.is(tok::l_brace)) {
1049 ConsumeBrace();
1050 SkipUntil(tok::r_brace, false, false);
1051 } else
Douglas Gregor586596f2010-05-06 17:25:47 +00001052 SkipUntil(tok::semi);
1053 return move(Switch);
1054 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001055
Chris Lattner0ecea032007-08-22 05:28:50 +00001056 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001057 // there is no compound stmt. C90 does not have this clause. We only do this
1058 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001059 //
1060 // C++ 6.4p1:
1061 // The substatement in a selection-statement (each substatement, in the else
1062 // form of the if statement) implicitly defines a local scope.
1063 //
1064 // See comments in ParseIfStatement for why we create a scope for the
1065 // condition and a new scope for substatement in C++.
1066 //
Mike Stump1eb44332009-09-09 15:08:12 +00001067 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001068 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +00001069
Reid Spencer5f016e22007-07-11 17:01:13 +00001070 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +00001071 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +00001072
Chris Lattner7e52de42010-01-24 01:50:29 +00001073 // Pop the scopes.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001074 InnerScope.Exit();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001075 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +00001076
Chris Lattner7e52de42010-01-24 01:50:29 +00001077 if (Body.isInvalid())
1078 // FIXME: Remove the case statement list from the Switch statement.
1079 Body = Actions.ActOnNullStmt(Tok.getLocation());
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001080
John McCall9ae2f072010-08-23 23:25:46 +00001081 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Reid Spencer5f016e22007-07-11 17:01:13 +00001082}
1083
1084/// ParseWhileStatement
1085/// while-statement: [C99 6.8.5.1]
1086/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001087/// [C++] 'while' '(' condition ')' statement
John McCall7f040a92010-12-24 02:08:15 +00001088StmtResult Parser::ParseWhileStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001089 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001090
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001091 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001092 SourceLocation WhileLoc = Tok.getLocation();
1093 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001094
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001095 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001096 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +00001097 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +00001098 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001099 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001100
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001101 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
1102
Chris Lattner22153252007-08-26 23:08:06 +00001103 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1104 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001105 //
1106 // C++ 6.4p3:
1107 // A name introduced by a declaration in a condition is in scope from its
1108 // point of declaration until the end of the substatements controlled by the
1109 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001110 // C++ 3.3.2p4:
1111 // Names declared in the for-init-statement, and in the condition of if,
1112 // while, for, and switch statements are local to the if, while, for, or
1113 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001114 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001115 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001116 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001117 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1118 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +00001119 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001120 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1121 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001122
1123 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +00001124 ExprResult Cond;
John McCalld226f652010-08-21 09:40:31 +00001125 Decl *CondVar = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +00001126 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +00001127 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001128
John McCall9ae2f072010-08-23 23:25:46 +00001129 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get()));
Mike Stump1eb44332009-09-09 15:08:12 +00001130
Chris Lattner0ecea032007-08-22 05:28:50 +00001131 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001132 // there is no compound stmt. C90 does not have this clause. We only do this
1133 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001134 //
1135 // C++ 6.5p2:
1136 // The substatement in an iteration-statement implicitly defines a local scope
1137 // which is entered and exited each time through the loop.
1138 //
1139 // See comments in ParseIfStatement for why we create a scope for the
1140 // condition and a new scope for substatement in C++.
1141 //
Mike Stump1eb44332009-09-09 15:08:12 +00001142 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001143 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +00001144
Reid Spencer5f016e22007-07-11 17:01:13 +00001145 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +00001146 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +00001147
Chris Lattner0ecea032007-08-22 05:28:50 +00001148 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001149 InnerScope.Exit();
1150 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +00001151
John McCalld226f652010-08-21 09:40:31 +00001152 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001153 return StmtError();
1154
John McCall9ae2f072010-08-23 23:25:46 +00001155 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Reid Spencer5f016e22007-07-11 17:01:13 +00001156}
1157
1158/// ParseDoStatement
1159/// do-statement: [C99 6.8.5.2]
1160/// 'do' statement 'while' '(' expression ')' ';'
1161/// Note: this lets the caller parse the end ';'.
John McCall7f040a92010-12-24 02:08:15 +00001162StmtResult Parser::ParseDoStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001163 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001164
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001165 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001166 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001167
Chris Lattner22153252007-08-26 23:08:06 +00001168 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1169 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001170 unsigned ScopeFlags;
Chris Lattner22153252007-08-26 23:08:06 +00001171 if (getLang().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001172 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +00001173 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001174 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +00001175
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001176 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001177
Chris Lattner0ecea032007-08-22 05:28:50 +00001178 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001179 // there is no compound stmt. C90 does not have this clause. We only do this
1180 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +00001181 //
1182 // C++ 6.5p2:
1183 // The substatement in an iteration-statement implicitly defines a local scope
1184 // which is entered and exited each time through the loop.
1185 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001186 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump1eb44332009-09-09 15:08:12 +00001187 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001188 Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +00001189
Reid Spencer5f016e22007-07-11 17:01:13 +00001190 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +00001191 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +00001192
Chris Lattner0ecea032007-08-22 05:28:50 +00001193 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001194 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001195
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001196 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001197 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +00001198 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +00001199 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +00001200 SkipUntil(tok::semi, false, true);
1201 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001202 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001203 }
1204 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001205
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001206 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001207 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +00001208 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001209 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001210 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001211
Chris Lattnerff871fb2008-12-12 06:35:28 +00001212 // Parse the parenthesized condition.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001213 BalancedDelimiterTracker T(*this, tok::l_paren);
1214 T.consumeOpen();
John McCall60d7b3a2010-08-24 06:29:42 +00001215 ExprResult Cond = ParseExpression();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001216 T.consumeClose();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001217 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001218
Sebastian Redl9a920342008-12-11 19:48:14 +00001219 if (Cond.isInvalid() || Body.isInvalid())
1220 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001221
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001222 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(),
1223 Cond.get(), T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001224}
1225
1226/// ParseForStatement
1227/// for-statement: [C99 6.8.5.3]
1228/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1229/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001230/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1231/// [C++] statement
Richard Smithad762fc2011-04-14 22:09:26 +00001232/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001233/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1234/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001235///
1236/// [C++] for-init-statement:
1237/// [C++] expression-statement
1238/// [C++] simple-declaration
1239///
Richard Smithad762fc2011-04-14 22:09:26 +00001240/// [C++0x] for-range-declaration:
1241/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1242/// [C++0x] for-range-initializer:
1243/// [C++0x] expression
1244/// [C++0x] braced-init-list [TODO]
John McCall7f040a92010-12-24 02:08:15 +00001245StmtResult Parser::ParseForStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001246 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001247
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001248 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001249 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001250
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001251 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001252 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +00001253 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +00001254 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001255 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001256
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001257 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001258
Chris Lattner22153252007-08-26 23:08:06 +00001259 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1260 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001261 //
1262 // C++ 6.4p3:
1263 // A name introduced by a declaration in a condition is in scope from its
1264 // point of declaration until the end of the substatements controlled by the
1265 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001266 // C++ 3.3.2p4:
1267 // Names declared in the for-init-statement, and in the condition of if,
1268 // while, for, and switch statements are local to the if, while, for, or
1269 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001270 // C++ 6.5.3p1:
1271 // Names declared in the for-init-statement are in the same declarative-region
1272 // as those declared in the condition.
1273 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001274 unsigned ScopeFlags;
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001275 if (C99orCXXorObjC)
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001276 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1277 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +00001278 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001279 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1280
1281 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001282
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001283 BalancedDelimiterTracker T(*this, tok::l_paren);
1284 T.consumeOpen();
1285
John McCall60d7b3a2010-08-24 06:29:42 +00001286 ExprResult Value;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001287
Richard Smithad762fc2011-04-14 22:09:26 +00001288 bool ForEach = false, ForRange = false;
John McCall60d7b3a2010-08-24 06:29:42 +00001289 StmtResult FirstPart;
Douglas Gregoreecf38f2010-05-06 21:39:56 +00001290 bool SecondPartIsInvalid = false;
Douglas Gregor586596f2010-05-06 17:25:47 +00001291 FullExprArg SecondPart(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001292 ExprResult Collection;
Richard Smithad762fc2011-04-14 22:09:26 +00001293 ForRangeInit ForRangeInit;
Douglas Gregor586596f2010-05-06 17:25:47 +00001294 FullExprArg ThirdPart(Actions);
John McCalld226f652010-08-21 09:40:31 +00001295 Decl *SecondVar = 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001296
Douglas Gregor791215b2009-09-21 20:51:25 +00001297 if (Tok.is(tok::code_completion)) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001298 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001299 C99orCXXorObjC? Sema::PCC_ForInit
1300 : Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001301 cutOffParsing();
1302 return StmtError();
Douglas Gregor791215b2009-09-21 20:51:25 +00001303 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001304
Reid Spencer5f016e22007-07-11 17:01:13 +00001305 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001306 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +00001307 // no first part, eat the ';'.
1308 ConsumeToken();
Eli Friedman9490ab42011-12-20 01:50:37 +00001309 } else if (isForInitDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +00001310 // Parse declaration, which eats the ';'.
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001311 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +00001312 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +00001313
John McCall0b7e6782011-03-24 11:26:52 +00001314 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00001315 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00001316
Richard Smithad762fc2011-04-14 22:09:26 +00001317 // In C++0x, "for (T NS:a" might not be a typo for ::
1318 bool MightBeForRangeStmt = getLang().CPlusPlus;
1319 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1320
Chris Lattner97144fc2009-04-02 04:16:50 +00001321 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001322 StmtVector Stmts(Actions);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001323 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
Richard Smithad762fc2011-04-14 22:09:26 +00001324 DeclEnd, attrs, false,
1325 MightBeForRangeStmt ?
1326 &ForRangeInit : 0);
Chris Lattnercd147752009-03-29 17:27:48 +00001327 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001328
Richard Smithad762fc2011-04-14 22:09:26 +00001329 if (ForRangeInit.ParsedForRangeDecl()) {
Richard Smith7fe62082011-10-15 05:09:34 +00001330 Diag(ForRangeInit.ColonLoc, getLang().CPlusPlus0x ?
1331 diag::warn_cxx98_compat_for_range : diag::ext_for_range);
Richard Smith8f4fb192011-09-04 19:54:14 +00001332
Richard Smithad762fc2011-04-14 22:09:26 +00001333 ForRange = true;
1334 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattnercd147752009-03-29 17:27:48 +00001335 ConsumeToken();
1336 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniana7cf23a2009-11-19 22:12:37 +00001337 Actions.ActOnForEachDeclStmt(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001338 // ObjC: for (id x in expr)
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001339 ConsumeToken(); // consume 'in'
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001340
Douglas Gregorfb629412010-08-23 21:17:50 +00001341 if (Tok.is(tok::code_completion)) {
1342 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001343 cutOffParsing();
1344 return StmtError();
Douglas Gregorfb629412010-08-23 21:17:50 +00001345 }
Douglas Gregor586596f2010-05-06 17:25:47 +00001346 Collection = ParseExpression();
Chris Lattnercd147752009-03-29 17:27:48 +00001347 } else {
1348 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001349 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001350 } else {
1351 Value = ParseExpression();
1352
John McCallf6a16482010-12-04 03:47:34 +00001353 ForEach = isTokIdentifier_in();
1354
Reid Spencer5f016e22007-07-11 17:01:13 +00001355 // Turn the expression into a stmt.
John McCallf6a16482010-12-04 03:47:34 +00001356 if (!Value.isInvalid()) {
1357 if (ForEach)
1358 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1359 else
1360 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
1361 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001362
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001363 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001364 ConsumeToken();
John McCallf6a16482010-12-04 03:47:34 +00001365 } else if (ForEach) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001366 ConsumeToken(); // consume 'in'
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001367
Douglas Gregorfb629412010-08-23 21:17:50 +00001368 if (Tok.is(tok::code_completion)) {
1369 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001370 cutOffParsing();
1371 return StmtError();
Douglas Gregorfb629412010-08-23 21:17:50 +00001372 }
Douglas Gregor586596f2010-05-06 17:25:47 +00001373 Collection = ParseExpression();
Richard Smitha44854a2011-12-20 22:56:20 +00001374 } else if (getLang().CPlusPlus0x && Tok.is(tok::colon) &&
1375 !FirstPart.isInvalid()) {
1376 // User tried to write the reasonable, but ill-formed, for-range-statement
1377 // for (expr : expr) { ... }
1378 Diag(Tok, diag::err_for_range_expected_decl)
1379 << FirstPart.get()->getSourceRange();
1380 SkipUntil(tok::r_paren, false, true);
1381 SecondPartIsInvalid = true;
Chris Lattner682bf922009-03-29 16:50:03 +00001382 } else {
Douglas Gregorb72c7782011-02-17 03:38:46 +00001383 if (!Value.isInvalid()) {
1384 Diag(Tok, diag::err_expected_semi_for);
1385 } else {
1386 // Skip until semicolon or rparen, don't consume it.
1387 SkipUntil(tok::r_paren, true, true);
1388 if (Tok.is(tok::semi))
1389 ConsumeToken();
1390 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001391 }
1392 }
Richard Smithad762fc2011-04-14 22:09:26 +00001393 if (!ForEach && !ForRange) {
John McCall9ae2f072010-08-23 23:25:46 +00001394 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001395 // Parse the second part of the for specifier.
1396 if (Tok.is(tok::semi)) { // for (...;;
1397 // no second part.
Douglas Gregorb72c7782011-02-17 03:38:46 +00001398 } else if (Tok.is(tok::r_paren)) {
1399 // missing both semicolons.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001400 } else {
John McCall60d7b3a2010-08-24 06:29:42 +00001401 ExprResult Second;
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001402 if (getLang().CPlusPlus)
Douglas Gregor586596f2010-05-06 17:25:47 +00001403 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1404 else {
1405 Second = ParseExpression();
1406 if (!Second.isInvalid())
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001407 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001408 Second.get());
Douglas Gregor586596f2010-05-06 17:25:47 +00001409 }
Douglas Gregoreecf38f2010-05-06 21:39:56 +00001410 SecondPartIsInvalid = Second.isInvalid();
John McCall9ae2f072010-08-23 23:25:46 +00001411 SecondPart = Actions.MakeFullExpr(Second.get());
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001412 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001413
Douglas Gregorb72c7782011-02-17 03:38:46 +00001414 if (Tok.isNot(tok::semi)) {
1415 if (!SecondPartIsInvalid || SecondVar)
1416 Diag(Tok, diag::err_expected_semi_for);
1417 else
1418 // Skip until semicolon or rparen, don't consume it.
1419 SkipUntil(tok::r_paren, true, true);
1420 }
1421
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001422 if (Tok.is(tok::semi)) {
1423 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001424 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001425
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001426 // Parse the third part of the for specifier.
Douglas Gregor586596f2010-05-06 17:25:47 +00001427 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCall60d7b3a2010-08-24 06:29:42 +00001428 ExprResult Third = ParseExpression();
John McCall9ae2f072010-08-23 23:25:46 +00001429 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregor586596f2010-05-06 17:25:47 +00001430 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001431 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001432 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001433 T.consumeClose();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001434
Richard Smithad762fc2011-04-14 22:09:26 +00001435 // We need to perform most of the semantic analysis for a C++0x for-range
1436 // statememt before parsing the body, in order to be able to deduce the type
1437 // of an auto-typed loop variable.
1438 StmtResult ForRangeStmt;
John McCall990567c2011-07-27 01:07:15 +00001439 if (ForRange) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001440 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, T.getOpenLocation(),
Richard Smithad762fc2011-04-14 22:09:26 +00001441 FirstPart.take(),
1442 ForRangeInit.ColonLoc,
1443 ForRangeInit.RangeExpr.get(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001444 T.getCloseLocation());
Richard Smithad762fc2011-04-14 22:09:26 +00001445
John McCall990567c2011-07-27 01:07:15 +00001446
1447 // Similarly, we need to do the semantic analysis for a for-range
1448 // statement immediately in order to close over temporaries correctly.
1449 } else if (ForEach) {
1450 if (!Collection.isInvalid())
1451 Collection =
1452 Actions.ActOnObjCForCollectionOperand(ForLoc, Collection.take());
1453 }
1454
Chris Lattner0ecea032007-08-22 05:28:50 +00001455 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001456 // there is no compound stmt. C90 does not have this clause. We only do this
1457 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001458 //
1459 // C++ 6.5p2:
1460 // The substatement in an iteration-statement implicitly defines a local scope
1461 // which is entered and exited each time through the loop.
1462 //
1463 // See comments in ParseIfStatement for why we create a scope for
1464 // for-init-statement/condition and a new scope for substatement in C++.
1465 //
Mike Stump1eb44332009-09-09 15:08:12 +00001466 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001467 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001468
Reid Spencer5f016e22007-07-11 17:01:13 +00001469 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +00001470 StmtResult Body(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001471
Chris Lattner0ecea032007-08-22 05:28:50 +00001472 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001473 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001474
Reid Spencer5f016e22007-07-11 17:01:13 +00001475 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001476 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001477
1478 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001479 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001480
Richard Smithad762fc2011-04-14 22:09:26 +00001481 if (ForEach)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001482 return Actions.ActOnObjCForCollectionStmt(ForLoc, T.getOpenLocation(),
1483 FirstPart.take(),
1484 Collection.take(),
1485 T.getCloseLocation(),
1486 Body.take());
Mike Stump1eb44332009-09-09 15:08:12 +00001487
Richard Smithad762fc2011-04-14 22:09:26 +00001488 if (ForRange)
1489 return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take());
1490
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001491 return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.take(),
1492 SecondPart, SecondVar, ThirdPart,
1493 T.getCloseLocation(), Body.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001494}
1495
1496/// ParseGotoStatement
1497/// jump-statement:
1498/// 'goto' identifier ';'
1499/// [GNU] 'goto' '*' expression ';'
1500///
1501/// Note: this lets the caller parse the end ';'.
1502///
John McCall7f040a92010-12-24 02:08:15 +00001503StmtResult Parser::ParseGotoStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001504 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001505
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001506 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001507 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001508
John McCall60d7b3a2010-08-24 06:29:42 +00001509 StmtResult Res;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001510 if (Tok.is(tok::identifier)) {
Chris Lattner337e5502011-02-18 01:27:55 +00001511 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1512 Tok.getLocation());
1513 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Reid Spencer5f016e22007-07-11 17:01:13 +00001514 ConsumeToken();
Eli Friedmanf01fdff2009-04-28 00:51:18 +00001515 } else if (Tok.is(tok::star)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001516 // GNU indirect goto extension.
1517 Diag(Tok, diag::ext_gnu_indirect_goto);
1518 SourceLocation StarLoc = ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001519 ExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001520 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001521 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001522 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001523 }
John McCall9ae2f072010-08-23 23:25:46 +00001524 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattner95cfb852007-07-22 04:13:33 +00001525 } else {
1526 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +00001527 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001528 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001529
Sebastian Redl9a920342008-12-11 19:48:14 +00001530 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001531}
1532
1533/// ParseContinueStatement
1534/// jump-statement:
1535/// 'continue' ';'
1536///
1537/// Note: this lets the caller parse the end ';'.
1538///
John McCall7f040a92010-12-24 02:08:15 +00001539StmtResult Parser::ParseContinueStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001540 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001541
Reid Spencer5f016e22007-07-11 17:01:13 +00001542 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001543 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001544}
1545
1546/// ParseBreakStatement
1547/// jump-statement:
1548/// 'break' ';'
1549///
1550/// Note: this lets the caller parse the end ';'.
1551///
John McCall7f040a92010-12-24 02:08:15 +00001552StmtResult Parser::ParseBreakStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001553 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001554
Reid Spencer5f016e22007-07-11 17:01:13 +00001555 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001556 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001557}
1558
1559/// ParseReturnStatement
1560/// jump-statement:
1561/// 'return' expression[opt] ';'
John McCall7f040a92010-12-24 02:08:15 +00001562StmtResult Parser::ParseReturnStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001563 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001564
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001565 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001566 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001567
John McCall60d7b3a2010-08-24 06:29:42 +00001568 ExprResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001569 if (Tok.isNot(tok::semi)) {
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001570 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001571 Actions.CodeCompleteReturn(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001572 cutOffParsing();
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001573 return StmtError();
1574 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001575
Douglas Gregor6f4596c2011-03-11 23:10:44 +00001576 // FIXME: This is a hack to allow something like C++0x's generalized
1577 // initializer lists, but only enough of this feature to allow Clang to
1578 // parse libstdc++ 4.5's headers.
1579 if (Tok.is(tok::l_brace) && getLang().CPlusPlus) {
1580 R = ParseInitializer();
Richard Smith7fe62082011-10-15 05:09:34 +00001581 if (R.isUsable())
1582 Diag(R.get()->getLocStart(), getLang().CPlusPlus0x ?
1583 diag::warn_cxx98_compat_generalized_initializer_lists :
1584 diag::ext_generalized_initializer_lists)
Douglas Gregor6f4596c2011-03-11 23:10:44 +00001585 << R.get()->getSourceRange();
1586 } else
1587 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001588 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001589 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001590 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001591 }
1592 }
John McCall9ae2f072010-08-23 23:25:46 +00001593 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001594}
1595
Eli Friedman3fedbe12011-09-30 01:13:51 +00001596/// ParseMicrosoftAsmStatement. When -fms-extensions/-fasm-blocks is enabled,
1597/// this routine is called to collect the tokens for an MS asm statement.
1598StmtResult Parser::ParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
1599 SourceManager &SrcMgr = PP.getSourceManager();
1600 SourceLocation EndLoc = AsmLoc;
1601 do {
1602 bool InBraces = false;
NAKAMURA Takumi96e21712011-10-08 11:31:53 +00001603 unsigned short savedBraceCount = 0;
Eli Friedman3fedbe12011-09-30 01:13:51 +00001604 bool InAsmComment = false;
1605 FileID FID;
NAKAMURA Takumi96e21712011-10-08 11:31:53 +00001606 unsigned LineNo = 0;
Eli Friedman3fedbe12011-09-30 01:13:51 +00001607 unsigned NumTokensRead = 0;
1608 SourceLocation LBraceLoc;
1609
1610 if (Tok.is(tok::l_brace)) {
1611 // Braced inline asm: consume the opening brace.
1612 InBraces = true;
1613 savedBraceCount = BraceCount;
1614 EndLoc = LBraceLoc = ConsumeBrace();
1615 ++NumTokensRead;
1616 } else {
1617 // Single-line inline asm; compute which line it is on.
1618 std::pair<FileID, unsigned> ExpAsmLoc =
1619 SrcMgr.getDecomposedExpansionLoc(EndLoc);
1620 FID = ExpAsmLoc.first;
1621 LineNo = SrcMgr.getLineNumber(FID, ExpAsmLoc.second);
1622 }
1623
Steve Naroff03d6bc62008-02-08 03:36:19 +00001624 SourceLocation TokLoc = Tok.getLocation();
Steve Naroff36280972008-02-08 18:01:27 +00001625 do {
Eli Friedman3fedbe12011-09-30 01:13:51 +00001626 // If we hit EOF, we're done, period.
1627 if (Tok.is(tok::eof))
1628 break;
1629 // When we consume the closing brace, we're done.
1630 if (InBraces && BraceCount == savedBraceCount)
1631 break;
1632
1633 if (!InAsmComment && Tok.is(tok::semi)) {
1634 // A semicolon in an asm is the start of a comment.
1635 InAsmComment = true;
1636 if (InBraces) {
1637 // Compute which line the comment is on.
1638 std::pair<FileID, unsigned> ExpSemiLoc =
1639 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1640 FID = ExpSemiLoc.first;
1641 LineNo = SrcMgr.getLineNumber(FID, ExpSemiLoc.second);
1642 }
1643 } else if (!InBraces || InAsmComment) {
1644 // If end-of-line is significant, check whether this token is on a
1645 // new line.
1646 std::pair<FileID, unsigned> ExpLoc =
1647 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1648 if (ExpLoc.first != FID ||
1649 SrcMgr.getLineNumber(ExpLoc.first, ExpLoc.second) != LineNo) {
1650 // If this is a single-line __asm, we're done.
1651 if (!InBraces)
1652 break;
1653 // We're no longer in a comment.
1654 InAsmComment = false;
1655 } else if (!InAsmComment && Tok.is(tok::r_brace)) {
1656 // Single-line asm always ends when a closing brace is seen.
1657 // FIXME: This is compatible with Apple gcc's -fasm-blocks; what
1658 // does MSVC do here?
1659 break;
1660 }
1661 }
1662
1663 // Consume the next token; make sure we don't modify the brace count etc.
1664 // if we are in a comment.
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001665 EndLoc = TokLoc;
Eli Friedman3fedbe12011-09-30 01:13:51 +00001666 if (InAsmComment)
1667 PP.Lex(Tok);
1668 else
1669 ConsumeAnyToken();
Steve Naroff36280972008-02-08 18:01:27 +00001670 TokLoc = Tok.getLocation();
Eli Friedman3fedbe12011-09-30 01:13:51 +00001671 ++NumTokensRead;
1672 } while (1);
1673
1674 if (InBraces && BraceCount != savedBraceCount) {
1675 // __asm without closing brace (this can happen at EOF).
1676 Diag(Tok, diag::err_expected_rbrace);
1677 Diag(LBraceLoc, diag::note_matching) << "{";
1678 return StmtError();
1679 } else if (NumTokensRead == 0) {
1680 // Empty __asm.
1681 Diag(Tok, diag::err_expected_lbrace);
1682 return StmtError();
1683 }
1684 // Multiple adjacent asm's form together into a single asm statement
1685 // in the AST.
1686 if (!Tok.is(tok::kw_asm))
1687 break;
1688 EndLoc = ConsumeToken();
1689 } while (1);
1690 // FIXME: Need to actually grab the data and pass it on to Sema. Ideally,
1691 // what Sema wants is a string of the entire inline asm, with one instruction
1692 // per line and all the __asm keywords stripped out, and a way of mapping
1693 // from any character of that string to its location in the original source
1694 // code. I'm not entirely sure how to go about that, though.
Mike Stump95059b52009-12-11 00:04:56 +00001695 Token t;
1696 t.setKind(tok::string_literal);
Chris Lattnere7896852010-08-17 16:00:12 +00001697 t.setLiteralData("\"/*FIXME: not done*/\"");
Mike Stump95059b52009-12-11 00:04:56 +00001698 t.clearFlag(Token::NeedsCleaning);
Chris Lattnere7896852010-08-17 16:00:12 +00001699 t.setLength(21);
Sean Hunt6cf75022010-08-30 17:47:05 +00001700 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
Mike Stump95059b52009-12-11 00:04:56 +00001701 ExprVector Constraints(Actions);
1702 ExprVector Exprs(Actions);
1703 ExprVector Clobbers(Actions);
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001704 return Actions.ActOnAsmStmt(AsmLoc, true, true, 0, 0, 0,
Mike Stump95059b52009-12-11 00:04:56 +00001705 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001706 AsmString.take(), move_arg(Clobbers),
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001707 EndLoc, true);
Steve Naroffd62701b2008-02-07 03:50:06 +00001708}
1709
Reid Spencer5f016e22007-07-11 17:01:13 +00001710/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001711/// asm-statement:
1712/// gnu-asm-statement
1713/// ms-asm-statement
1714///
1715/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001716/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1717///
1718/// [GNU] asm-argument:
1719/// asm-string-literal
1720/// asm-string-literal ':' asm-operands[opt]
1721/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1722/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1723/// ':' asm-clobbers
1724///
1725/// [GNU] asm-clobbers:
1726/// asm-string-literal
1727/// asm-clobbers ',' asm-string-literal
1728///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001729/// [MS] ms-asm-statement:
Eli Friedman3fedbe12011-09-30 01:13:51 +00001730/// ms-asm-block
1731/// ms-asm-block ms-asm-statement
Steve Naroff5f8aa692008-02-11 23:15:56 +00001732///
Eli Friedman3fedbe12011-09-30 01:13:51 +00001733/// [MS] ms-asm-block:
1734/// '__asm' ms-asm-line '\n'
1735/// '__asm' '{' ms-asm-instruction-block[opt] '}' ';'[opt]
1736///
1737/// [MS] ms-asm-instruction-block
1738/// ms-asm-line
1739/// ms-asm-line '\n' ms-asm-instruction-block
Steve Naroff5f8aa692008-02-11 23:15:56 +00001740///
John McCall60d7b3a2010-08-24 06:29:42 +00001741StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001742 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001743 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001744
Francois Pichet62ec1f22011-09-17 17:15:52 +00001745 if (getLang().MicrosoftExt && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001746 msAsm = true;
Eli Friedman3fedbe12011-09-30 01:13:51 +00001747 return ParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffd62701b2008-02-07 03:50:06 +00001748 }
John McCall0b7e6782011-03-24 11:26:52 +00001749 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00001750 SourceLocation Loc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00001751 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redl9a920342008-12-11 19:48:14 +00001752
Reid Spencer5f016e22007-07-11 17:01:13 +00001753 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1754 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001755 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001756 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001757 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001758
Reid Spencer5f016e22007-07-11 17:01:13 +00001759 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001760 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001761 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001762 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001763 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001764 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001765 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001766 BalancedDelimiterTracker T(*this, tok::l_paren);
1767 T.consumeOpen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001768
John McCall60d7b3a2010-08-24 06:29:42 +00001769 ExprResult AsmString(ParseAsmStringLiteral());
Ted Kremenek320fa4b2011-12-02 01:30:14 +00001770 if (AsmString.isInvalid()) {
1771 // If the reason we are recovering is because of an improper string
1772 // literal, it makes the most sense just to consume to the ')'.
1773 if (isTokenStringLiteral())
1774 T.skipToEnd();
Sebastian Redl9a920342008-12-11 19:48:14 +00001775 return StmtError();
Ted Kremenek320fa4b2011-12-02 01:30:14 +00001776 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001777
Chris Lattner5f9e2722011-07-23 10:55:15 +00001778 SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001779 ExprVector Constraints(Actions);
1780 ExprVector Exprs(Actions);
1781 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001782
Anders Carlssondfab34a2008-02-05 23:03:50 +00001783 if (Tok.is(tok::r_paren)) {
Chris Lattner64cb4752009-12-20 23:00:41 +00001784 // We have a simple asm expression like 'asm("foo")'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001785 T.consumeClose();
Chris Lattner64cb4752009-12-20 23:00:41 +00001786 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001787 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
Chris Lattner64cb4752009-12-20 23:00:41 +00001788 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001789 AsmString.take(), move_arg(Clobbers),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001790 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001791 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001792
Chris Lattner64cb4752009-12-20 23:00:41 +00001793 // Parse Outputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001794 bool AteExtraColon = false;
1795 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1796 // In C++ mode, parse "::" like ": :".
1797 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattner64cb4752009-12-20 23:00:41 +00001798 ConsumeToken();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001799
Chris Lattner64056462009-12-20 23:08:04 +00001800 if (!AteExtraColon &&
1801 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001802 return StmtError();
1803 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001804
Chris Lattner64cb4752009-12-20 23:00:41 +00001805 unsigned NumOutputs = Names.size();
1806
1807 // Parse Inputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001808 if (AteExtraColon ||
1809 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1810 // In C++ mode, parse "::" like ": :".
1811 if (AteExtraColon)
1812 AteExtraColon = false;
1813 else {
1814 AteExtraColon = Tok.is(tok::coloncolon);
1815 ConsumeToken();
1816 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001817
Chris Lattner64056462009-12-20 23:08:04 +00001818 if (!AteExtraColon &&
1819 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001820 return StmtError();
1821 }
1822
1823 assert(Names.size() == Constraints.size() &&
1824 Constraints.size() == Exprs.size() &&
1825 "Input operand size mismatch!");
1826
1827 unsigned NumInputs = Names.size() - NumOutputs;
1828
1829 // Parse the clobbers, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001830 if (AteExtraColon || Tok.is(tok::colon)) {
1831 if (!AteExtraColon)
1832 ConsumeToken();
Chris Lattner64cb4752009-12-20 23:00:41 +00001833
Chandler Carruth102e1b62010-07-22 07:11:21 +00001834 // Parse the asm-string list for clobbers if present.
1835 if (Tok.isNot(tok::r_paren)) {
1836 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +00001837 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattner64cb4752009-12-20 23:00:41 +00001838
Chandler Carruth102e1b62010-07-22 07:11:21 +00001839 if (Clobber.isInvalid())
1840 break;
Chris Lattner64cb4752009-12-20 23:00:41 +00001841
Chandler Carruth102e1b62010-07-22 07:11:21 +00001842 Clobbers.push_back(Clobber.release());
Chris Lattner64cb4752009-12-20 23:00:41 +00001843
Chandler Carruth102e1b62010-07-22 07:11:21 +00001844 if (Tok.isNot(tok::comma)) break;
1845 ConsumeToken();
1846 }
Chris Lattner64cb4752009-12-20 23:00:41 +00001847 }
1848 }
1849
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001850 T.consumeClose();
Chris Lattner64cb4752009-12-20 23:00:41 +00001851 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001852 NumOutputs, NumInputs, Names.data(),
Sebastian Redlf512e822009-01-18 18:03:53 +00001853 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001854 AsmString.take(), move_arg(Clobbers),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001855 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001856}
1857
1858/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattner64cb4752009-12-20 23:00:41 +00001859/// asm-statement, assuming the leading ':' token was eaten.
Reid Spencer5f016e22007-07-11 17:01:13 +00001860///
1861/// [GNU] asm-operands:
1862/// asm-operand
1863/// asm-operands ',' asm-operand
1864///
1865/// [GNU] asm-operand:
1866/// asm-string-literal '(' expression ')'
1867/// '[' identifier ']' asm-string-literal '(' expression ')'
1868///
Daniel Dunbar5ffe14c2009-10-18 20:26:27 +00001869//
1870// FIXME: Avoid unnecessary std::string trashing.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001871bool Parser::ParseAsmOperandsOpt(SmallVectorImpl<IdentifierInfo *> &Names,
Richard Trieuf81e5a92011-09-09 02:00:50 +00001872 SmallVectorImpl<Expr *> &Constraints,
1873 SmallVectorImpl<Expr *> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001874 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001875 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001876 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001877
1878 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001879 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001880 if (Tok.is(tok::l_square)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001881 BalancedDelimiterTracker T(*this, tok::l_square);
1882 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00001883
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001884 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001885 Diag(Tok, diag::err_expected_ident);
1886 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001887 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001888 }
Mike Stump1eb44332009-09-09 15:08:12 +00001889
Anders Carlssonb235fc22007-11-22 01:36:19 +00001890 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001891 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001892
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001893 Names.push_back(II);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001894 T.consumeClose();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001895 } else
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001896 Names.push_back(0);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001897
John McCall60d7b3a2010-08-24 06:29:42 +00001898 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001899 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001900 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001901 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001902 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001903 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001904
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001905 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001906 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001907 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001908 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001909 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001910
Reid Spencer5f016e22007-07-11 17:01:13 +00001911 // Read the parenthesized expression.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001912 BalancedDelimiterTracker T(*this, tok::l_paren);
1913 T.consumeOpen();
John McCall60d7b3a2010-08-24 06:29:42 +00001914 ExprResult Res(ParseExpression());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001915 T.consumeClose();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001916 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001917 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001918 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001919 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001920 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001921 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001922 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001923 ConsumeToken();
1924 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001925
1926 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001927}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001928
Douglas Gregorc9977d02011-03-16 17:05:57 +00001929Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner40e9bc82009-03-05 00:49:17 +00001930 assert(Tok.is(tok::l_brace));
1931 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001932
Douglas Gregorc9977d02011-03-16 17:05:57 +00001933 if (PP.isCodeCompletionEnabled()) {
1934 if (trySkippingFunctionBodyForCodeCompletion()) {
1935 BodyScope.Exit();
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001936 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregorc9977d02011-03-16 17:05:57 +00001937 }
1938 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001939
John McCallf312b1e2010-08-26 23:41:50 +00001940 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1941 "parsing function body");
Mike Stump1eb44332009-09-09 15:08:12 +00001942
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001943 // Do not enter a scope for the brace, as the arguments are in the same scope
1944 // (the function body) as the body itself. Instead, just read the statement
1945 // list and put it into a CompoundStmt for safe keeping.
John McCall60d7b3a2010-08-24 06:29:42 +00001946 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl61364dd2008-12-11 19:30:53 +00001947
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001948 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001949 if (FnBody.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001950 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner40e9bc82009-03-05 00:49:17 +00001951 MultiStmtArg(Actions), false);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001952
Douglas Gregorc9977d02011-03-16 17:05:57 +00001953 BodyScope.Exit();
John McCall9ae2f072010-08-23 23:25:46 +00001954 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001955}
Sebastian Redla0fd8652008-12-21 16:41:36 +00001956
Sebastian Redld3a413d2009-04-26 20:35:05 +00001957/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1958///
1959/// function-try-block:
1960/// 'try' ctor-initializer[opt] compound-statement handler-seq
1961///
Douglas Gregorc9977d02011-03-16 17:05:57 +00001962Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redld3a413d2009-04-26 20:35:05 +00001963 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1964 SourceLocation TryLoc = ConsumeToken();
1965
John McCallf312b1e2010-08-26 23:41:50 +00001966 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1967 "parsing function try block");
Sebastian Redld3a413d2009-04-26 20:35:05 +00001968
1969 // Constructor initializer list?
1970 if (Tok.is(tok::colon))
1971 ParseConstructorInitializer(Decl);
Douglas Gregor2eef4272011-09-07 20:36:12 +00001972 else
1973 Actions.ActOnDefaultCtorInitializers(Decl);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001974
Douglas Gregorc9977d02011-03-16 17:05:57 +00001975 if (PP.isCodeCompletionEnabled()) {
1976 if (trySkippingFunctionBodyForCodeCompletion()) {
1977 BodyScope.Exit();
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001978 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregorc9977d02011-03-16 17:05:57 +00001979 }
1980 }
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001981
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001982 SourceLocation LBraceLoc = Tok.getLocation();
John McCall60d7b3a2010-08-24 06:29:42 +00001983 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redld3a413d2009-04-26 20:35:05 +00001984 // If we failed to parse the try-catch, we just give the function an empty
1985 // compound statement as the body.
1986 if (FnBody.isInvalid())
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001987 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redld3a413d2009-04-26 20:35:05 +00001988 MultiStmtArg(Actions), false);
1989
Douglas Gregorc9977d02011-03-16 17:05:57 +00001990 BodyScope.Exit();
John McCall9ae2f072010-08-23 23:25:46 +00001991 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redld3a413d2009-04-26 20:35:05 +00001992}
1993
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001994bool Parser::trySkippingFunctionBodyForCodeCompletion() {
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001995 assert(Tok.is(tok::l_brace));
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001996 assert(PP.isCodeCompletionEnabled() &&
1997 "Should only be called when in code-completion mode");
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001998
1999 // We're in code-completion mode. Skip parsing for all function bodies unless
2000 // the body contains the code-completion point.
2001 TentativeParsingAction PA(*this);
2002 ConsumeBrace();
2003 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
2004 /*StopAtCodeCompletion=*/true)) {
2005 PA.Commit();
2006 return true;
2007 }
2008
2009 PA.Revert();
2010 return false;
2011}
2012
Sebastian Redla0fd8652008-12-21 16:41:36 +00002013/// ParseCXXTryBlock - Parse a C++ try-block.
2014///
2015/// try-block:
2016/// 'try' compound-statement handler-seq
2017///
John McCall7f040a92010-12-24 02:08:15 +00002018StmtResult Parser::ParseCXXTryBlock(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002019 // FIXME: Add attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00002020
Sebastian Redla0fd8652008-12-21 16:41:36 +00002021 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2022
2023 SourceLocation TryLoc = ConsumeToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +00002024 return ParseCXXTryBlockCommon(TryLoc);
2025}
2026
2027/// ParseCXXTryBlockCommon - Parse the common part of try-block and
2028/// function-try-block.
2029///
2030/// try-block:
2031/// 'try' compound-statement handler-seq
2032///
2033/// function-try-block:
2034/// 'try' ctor-initializer[opt] compound-statement handler-seq
2035///
2036/// handler-seq:
2037/// handler handler-seq[opt]
2038///
John Wiegley28bbe4b2011-04-28 01:08:34 +00002039/// [Borland] try-block:
2040/// 'try' compound-statement seh-except-block
2041/// 'try' compound-statment seh-finally-block
2042///
John McCall60d7b3a2010-08-24 06:29:42 +00002043StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redla0fd8652008-12-21 16:41:36 +00002044 if (Tok.isNot(tok::l_brace))
2045 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Sean Huntbbd37c62009-11-21 08:43:09 +00002046 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall0b7e6782011-03-24 11:26:52 +00002047 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregorbca01b42011-07-06 22:04:06 +00002048 StmtResult TryBlock(ParseCompoundStatement(attrs, /*isStmtExpr=*/false,
2049 Scope::DeclScope|Scope::TryScope));
Sebastian Redla0fd8652008-12-21 16:41:36 +00002050 if (TryBlock.isInvalid())
2051 return move(TryBlock);
2052
John Wiegley28bbe4b2011-04-28 01:08:34 +00002053 // Borland allows SEH-handlers with 'try'
Douglas Gregorb57791e2011-10-21 03:57:52 +00002054
2055 if((Tok.is(tok::identifier) &&
2056 Tok.getIdentifierInfo() == getSEHExceptKeyword()) ||
2057 Tok.is(tok::kw___finally)) {
John Wiegley28bbe4b2011-04-28 01:08:34 +00002058 // TODO: Factor into common return ParseSEHHandlerCommon(...)
2059 StmtResult Handler;
Douglas Gregorb57791e2011-10-21 03:57:52 +00002060 if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley28bbe4b2011-04-28 01:08:34 +00002061 SourceLocation Loc = ConsumeToken();
2062 Handler = ParseSEHExceptBlock(Loc);
2063 }
2064 else {
2065 SourceLocation Loc = ConsumeToken();
2066 Handler = ParseSEHFinallyBlock(Loc);
2067 }
2068 if(Handler.isInvalid())
2069 return move(Handler);
John McCall7f040a92010-12-24 02:08:15 +00002070
John Wiegley28bbe4b2011-04-28 01:08:34 +00002071 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
2072 TryLoc,
2073 TryBlock.take(),
2074 Handler.take());
Sebastian Redla0fd8652008-12-21 16:41:36 +00002075 }
John Wiegley28bbe4b2011-04-28 01:08:34 +00002076 else {
2077 StmtVector Handlers(Actions);
2078 MaybeParseCXX0XAttributes(attrs);
2079 ProhibitAttributes(attrs);
Sebastian Redla0fd8652008-12-21 16:41:36 +00002080
John Wiegley28bbe4b2011-04-28 01:08:34 +00002081 if (Tok.isNot(tok::kw_catch))
2082 return StmtError(Diag(Tok, diag::err_expected_catch));
2083 while (Tok.is(tok::kw_catch)) {
2084 StmtResult Handler(ParseCXXCatchBlock());
2085 if (!Handler.isInvalid())
2086 Handlers.push_back(Handler.release());
2087 }
2088 // Don't bother creating the full statement if we don't have any usable
2089 // handlers.
2090 if (Handlers.empty())
2091 return StmtError();
2092
2093 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
2094 }
Sebastian Redla0fd8652008-12-21 16:41:36 +00002095}
2096
2097/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
2098///
2099/// handler:
2100/// 'catch' '(' exception-declaration ')' compound-statement
2101///
2102/// exception-declaration:
2103/// type-specifier-seq declarator
2104/// type-specifier-seq abstract-declarator
2105/// type-specifier-seq
2106/// '...'
2107///
John McCall60d7b3a2010-08-24 06:29:42 +00002108StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redla0fd8652008-12-21 16:41:36 +00002109 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
2110
2111 SourceLocation CatchLoc = ConsumeToken();
2112
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002113 BalancedDelimiterTracker T(*this, tok::l_paren);
2114 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redla0fd8652008-12-21 16:41:36 +00002115 return StmtError();
2116
2117 // C++ 3.3.2p3:
2118 // The name in a catch exception-declaration is local to the handler and
2119 // shall not be redeclared in the outermost block of the handler.
2120 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
2121
2122 // exception-declaration is equivalent to '...' or a parameter-declaration
2123 // without default arguments.
John McCalld226f652010-08-21 09:40:31 +00002124 Decl *ExceptionDecl = 0;
Sebastian Redla0fd8652008-12-21 16:41:36 +00002125 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00002126 DeclSpec DS(AttrFactory);
Sebastian Redl4b07b292008-12-22 19:15:10 +00002127 if (ParseCXXTypeSpecifierSeq(DS))
2128 return StmtError();
Sebastian Redla0fd8652008-12-21 16:41:36 +00002129 Declarator ExDecl(DS, Declarator::CXXCatchContext);
2130 ParseDeclarator(ExDecl);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002131 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redla0fd8652008-12-21 16:41:36 +00002132 } else
2133 ConsumeToken();
2134
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002135 T.consumeClose();
2136 if (T.getCloseLocation().isInvalid())
Sebastian Redla0fd8652008-12-21 16:41:36 +00002137 return StmtError();
2138
2139 if (Tok.isNot(tok::l_brace))
2140 return StmtError(Diag(Tok, diag::err_expected_lbrace));
2141
Sean Huntbbd37c62009-11-21 08:43:09 +00002142 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall0b7e6782011-03-24 11:26:52 +00002143 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002144 StmtResult Block(ParseCompoundStatement(attrs));
Sebastian Redla0fd8652008-12-21 16:41:36 +00002145 if (Block.isInvalid())
2146 return move(Block);
2147
John McCall9ae2f072010-08-23 23:25:46 +00002148 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redla0fd8652008-12-21 16:41:36 +00002149}
Francois Pichet1e862692011-05-06 20:48:22 +00002150
2151void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Douglas Gregor3896fc52011-10-24 22:31:10 +00002152 IfExistsCondition Result;
Francois Pichetf9860382011-05-07 17:30:27 +00002153 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet1e862692011-05-06 20:48:22 +00002154 return;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002155
Douglas Gregor3896fc52011-10-24 22:31:10 +00002156 // Handle dependent statements by parsing the braces as a compound statement.
2157 // This is not the same behavior as Visual C++, which don't treat this as a
2158 // compound statement, but for Clang's type checking we can't have anything
2159 // inside these braces escaping to the surrounding code.
2160 if (Result.Behavior == IEB_Dependent) {
2161 if (!Tok.is(tok::l_brace)) {
2162 Diag(Tok, diag::err_expected_lbrace);
2163 return;
2164 }
2165
2166 ParsedAttributes Attrs(AttrFactory);
2167 StmtResult Compound = ParseCompoundStatement(Attrs);
Douglas Gregorba0513d2011-10-25 01:33:02 +00002168 if (Compound.isInvalid())
2169 return;
2170
2171 StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc,
2172 Result.IsIfExists,
2173 Result.SS,
2174 Result.Name,
2175 Compound.get());
2176 if (DepResult.isUsable())
2177 Stmts.push_back(DepResult.get());
Douglas Gregor3896fc52011-10-24 22:31:10 +00002178 return;
2179 }
2180
2181 BalancedDelimiterTracker Braces(*this, tok::l_brace);
2182 if (Braces.consumeOpen()) {
Francois Pichet1e862692011-05-06 20:48:22 +00002183 Diag(Tok, diag::err_expected_lbrace);
2184 return;
2185 }
Francois Pichet1e862692011-05-06 20:48:22 +00002186
Douglas Gregor3896fc52011-10-24 22:31:10 +00002187 switch (Result.Behavior) {
2188 case IEB_Parse:
2189 // Parse the statements below.
2190 break;
2191
2192 case IEB_Dependent:
2193 llvm_unreachable("Dependent case handled above");
2194 break;
2195
2196 case IEB_Skip:
2197 Braces.skipToEnd();
Francois Pichet1e862692011-05-06 20:48:22 +00002198 return;
2199 }
2200
2201 // Condition is true, parse the statements.
2202 while (Tok.isNot(tok::r_brace)) {
2203 StmtResult R = ParseStatementOrDeclaration(Stmts, false);
2204 if (R.isUsable())
2205 Stmts.push_back(R.release());
2206 }
Douglas Gregor3896fc52011-10-24 22:31:10 +00002207 Braces.consumeClose();
Francois Pichet1e862692011-05-06 20:48:22 +00002208}