blob: c1ead6bdef8c5af4301ede150f0349e7145a4060 [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;
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +000082
83 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();
Douglas Gregor791215b2009-09-21 20:51:25 +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 }
Douglas Gregor312eadb2011-04-24 05:37:28 +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();
117 Sema::NameClassification Classification
118 = Actions.ClassifyName(getCurScope(), SS, Name, NameLoc, Next);
119 switch (Classification.getKind()) {
120 case Sema::NC_Keyword:
121 // The identifier was corrected to a keyword. Update the token
122 // to this keyword, and try again.
123 if (Name->getTokenID() != tok::identifier) {
124 Tok.setIdentifierInfo(Name);
125 Tok.setKind(Name->getTokenID());
126 goto Retry;
127 }
128
129 // Fall through via the normal error path.
130 // FIXME: This seems like it could only happen for context-sensitive
131 // keywords.
132
133 case Sema::NC_Error:
134 // Handle errors here by skipping up to the next semicolon or '}', and
135 // eat the semicolon if that's what stopped us.
136 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
137 if (Tok.is(tok::semi))
138 ConsumeToken();
139 return StmtError();
140
141 case Sema::NC_Unknown:
142 // Either we don't know anything about this identifier, or we know that
143 // we're in a syntactic context we haven't handled yet.
144 break;
145
Douglas Gregord9d75e52011-04-27 05:41:15 +0000146 case Sema::NC_Type:
Douglas Gregor3b887352011-04-27 04:48:22 +0000147 Tok.setKind(tok::annot_typename);
148 setTypeAnnotation(Tok, Classification.getType());
149 Tok.setAnnotationEndLoc(NameLoc);
Douglas Gregor3b887352011-04-27 04:48:22 +0000150 PP.AnnotateCachedTokens(Tok);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000151 break;
152
153 case Sema::NC_Expression:
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000154 Tok.setKind(tok::annot_primary_expr);
155 setExprAnnotation(Tok, Classification.getExpression());
156 Tok.setAnnotationEndLoc(NameLoc);
157 PP.AnnotateCachedTokens(Tok);
158 break;
Douglas Gregor312eadb2011-04-24 05:37:28 +0000159
160 case Sema::NC_TypeTemplate:
161 case Sema::NC_FunctionTemplate: {
162 ConsumeToken(); // the identifier
163 UnqualifiedId Id;
164 Id.setIdentifier(Name, NameLoc);
165 if (AnnotateTemplateIdToken(
166 TemplateTy::make(Classification.getTemplateName()),
167 Classification.getTemplateNameKind(),
Douglas Gregor3b887352011-04-27 04:48:22 +0000168 SS, Id, SourceLocation(),
169 /*AllowTypeAnnotation=*/false)) {
170 // Handle errors here by skipping up to the next semicolon or '}', and
171 // eat the semicolon if that's what stopped us.
172 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
173 if (Tok.is(tok::semi))
174 ConsumeToken();
175 return StmtError();
176 }
177
178 // If the next token is '::', jump right into parsing a
179 // nested-name-specifier. We don't want to leave the template-id
180 // hanging.
181 if (NextToken().is(tok::coloncolon) && TryAnnotateCXXScopeToken(false)){
Douglas Gregor312eadb2011-04-24 05:37:28 +0000182 // Handle errors here by skipping up to the next semicolon or '}', and
183 // eat the semicolon if that's what stopped us.
184 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
185 if (Tok.is(tok::semi))
186 ConsumeToken();
187 return StmtError();
188 }
189
190 // We've annotated a template-id, so try again now.
191 goto Retry;
192 }
193
194 case Sema::NC_NestedNameSpecifier:
195 // FIXME: Implement this!
196 break;
197 }
198 }
199
200 // Fall through
201 }
202
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000203 default: {
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000204 if ((getLang().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000205 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000206 DeclGroupPtrTy Decl = ParseDeclaration(Stmts, Declarator::BlockContext,
John McCall7f040a92010-12-24 02:08:15 +0000207 DeclEnd, attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000208 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000209 }
210
211 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000212 Diag(Tok, diag::err_expected_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000213 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 }
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000216 return ParseExprStatement(attrs);
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000217 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000218
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 case tok::kw_case: // C99 6.8.1: labeled-statement
John McCall7f040a92010-12-24 02:08:15 +0000220 return ParseCaseStatement(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 case tok::kw_default: // C99 6.8.1: labeled-statement
John McCall7f040a92010-12-24 02:08:15 +0000222 return ParseDefaultStatement(attrs);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000223
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 case tok::l_brace: // C99 6.8.2: compound-statement
John McCall7f040a92010-12-24 02:08:15 +0000225 return ParseCompoundStatement(attrs);
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000226 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
Argyrios Kyrtzidise2ca8282011-09-01 21:53:45 +0000227 bool HasLeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
228 return Actions.ActOnNullStmt(ConsumeToken(), HasLeadingEmptyMacro);
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000229 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000230
Reid Spencer5f016e22007-07-11 17:01:13 +0000231 case tok::kw_if: // C99 6.8.4.1: if-statement
John McCall7f040a92010-12-24 02:08:15 +0000232 return ParseIfStatement(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +0000233 case tok::kw_switch: // C99 6.8.4.2: switch-statement
John McCall7f040a92010-12-24 02:08:15 +0000234 return ParseSwitchStatement(attrs);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000235
Reid Spencer5f016e22007-07-11 17:01:13 +0000236 case tok::kw_while: // C99 6.8.5.1: while-statement
John McCall7f040a92010-12-24 02:08:15 +0000237 return ParseWhileStatement(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +0000238 case tok::kw_do: // C99 6.8.5.2: do-statement
John McCall7f040a92010-12-24 02:08:15 +0000239 Res = ParseDoStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000240 SemiError = "do/while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000241 break;
242 case tok::kw_for: // C99 6.8.5.3: for-statement
John McCall7f040a92010-12-24 02:08:15 +0000243 return ParseForStatement(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +0000244
245 case tok::kw_goto: // C99 6.8.6.1: goto-statement
John McCall7f040a92010-12-24 02:08:15 +0000246 Res = ParseGotoStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000247 SemiError = "goto";
Reid Spencer5f016e22007-07-11 17:01:13 +0000248 break;
249 case tok::kw_continue: // C99 6.8.6.2: continue-statement
John McCall7f040a92010-12-24 02:08:15 +0000250 Res = ParseContinueStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000251 SemiError = "continue";
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 break;
253 case tok::kw_break: // C99 6.8.6.3: break-statement
John McCall7f040a92010-12-24 02:08:15 +0000254 Res = ParseBreakStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000255 SemiError = "break";
Reid Spencer5f016e22007-07-11 17:01:13 +0000256 break;
257 case tok::kw_return: // C99 6.8.6.4: return-statement
John McCall7f040a92010-12-24 02:08:15 +0000258 Res = ParseReturnStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000259 SemiError = "return";
Reid Spencer5f016e22007-07-11 17:01:13 +0000260 break;
Sebastian Redl61364dd2008-12-11 19:30:53 +0000261
Sebastian Redla0fd8652008-12-21 16:41:36 +0000262 case tok::kw_asm: {
John McCall7f040a92010-12-24 02:08:15 +0000263 ProhibitAttributes(attrs);
Steve Naroffd62701b2008-02-07 03:50:06 +0000264 bool msAsm = false;
265 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidisbf8cafa2010-11-02 02:33:08 +0000266 Res = Actions.ActOnFinishFullStmt(Res.get());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000267 if (msAsm) return move(Res);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000268 SemiError = "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +0000269 break;
270 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000271
Sebastian Redla0fd8652008-12-21 16:41:36 +0000272 case tok::kw_try: // C++ 15: try-block
John McCall7f040a92010-12-24 02:08:15 +0000273 return ParseCXXTryBlock(attrs);
John Wiegley28bbe4b2011-04-28 01:08:34 +0000274
275 case tok::kw___try:
276 return ParseSEHTryBlock(attrs);
Sebastian Redla0fd8652008-12-21 16:41:36 +0000277 }
278
Reid Spencer5f016e22007-07-11 17:01:13 +0000279 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000280 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000282 } else if (!Res.isInvalid()) {
Chris Lattner7b3684a2009-06-14 00:23:56 +0000283 // If the result was valid, then we do want to diagnose this. Use
284 // ExpectAndConsume to emit the diagnostic, even though we know it won't
285 // succeed.
286 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner19504402008-11-13 18:52:53 +0000287 // Skip until we see a } or ;, but don't eat it.
288 SkipUntil(tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 }
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Sebastian Redl61364dd2008-12-11 19:30:53 +0000291 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000292}
293
Douglas Gregor312eadb2011-04-24 05:37:28 +0000294/// \brief Parse an expression statement.
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000295StmtResult Parser::ParseExprStatement(ParsedAttributes &Attrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +0000296 // If a case keyword is missing, this is where it should be inserted.
297 Token OldToken = Tok;
298
299 // FIXME: Use the attributes
300 // expression[opt] ';'
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000301 ExprResult Expr(ParseExpression());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000302 if (Expr.isInvalid()) {
303 // If the expression is invalid, skip ahead to the next semicolon or '}'.
304 // Not doing this opens us up to the possibility of infinite loops if
305 // ParseExpression does not consume any tokens.
306 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
307 if (Tok.is(tok::semi))
308 ConsumeToken();
309 return StmtError();
310 }
311
312 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
313 Actions.CheckCaseExpression(Expr.get())) {
314 // If a constant expression is followed by a colon inside a switch block,
315 // suggest a missing case keyword.
316 Diag(OldToken, diag::err_expected_case_before_expression)
317 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
318
319 // Recover parsing as a case statement.
320 return ParseCaseStatement(Attrs, /*MissingCase=*/true, Expr);
321 }
322
323 // Otherwise, eat the semicolon.
324 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
325 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr.get()));
John Wiegley28bbe4b2011-04-28 01:08:34 +0000326}
Douglas Gregor312eadb2011-04-24 05:37:28 +0000327
John Wiegley28bbe4b2011-04-28 01:08:34 +0000328StmtResult Parser::ParseSEHTryBlock(ParsedAttributes & Attrs) {
329 assert(Tok.is(tok::kw___try) && "Expected '__try'");
330 SourceLocation Loc = ConsumeToken();
331 return ParseSEHTryBlockCommon(Loc);
332}
333
334/// ParseSEHTryBlockCommon
335///
336/// seh-try-block:
337/// '__try' compound-statement seh-handler
338///
339/// seh-handler:
340/// seh-except-block
341/// seh-finally-block
342///
343StmtResult Parser::ParseSEHTryBlockCommon(SourceLocation TryLoc) {
344 if(Tok.isNot(tok::l_brace))
345 return StmtError(Diag(Tok,diag::err_expected_lbrace));
346
347 ParsedAttributesWithRange attrs(AttrFactory);
348 StmtResult TryBlock(ParseCompoundStatement(attrs));
349 if(TryBlock.isInvalid())
350 return move(TryBlock);
351
352 StmtResult Handler;
353 if(Tok.is(tok::kw___except)) {
354 SourceLocation Loc = ConsumeToken();
355 Handler = ParseSEHExceptBlock(Loc);
356 } else if (Tok.is(tok::kw___finally)) {
357 SourceLocation Loc = ConsumeToken();
358 Handler = ParseSEHFinallyBlock(Loc);
359 } else {
360 return StmtError(Diag(Tok,diag::err_seh_expected_handler));
361 }
362
363 if(Handler.isInvalid())
364 return move(Handler);
365
366 return Actions.ActOnSEHTryBlock(false /* IsCXXTry */,
367 TryLoc,
368 TryBlock.take(),
369 Handler.take());
370}
371
372/// ParseSEHExceptBlock - Handle __except
373///
374/// seh-except-block:
375/// '__except' '(' seh-filter-expression ')' compound-statement
376///
377StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) {
378 PoisonIdentifierRAIIObject raii(Ident__exception_code, false),
379 raii2(Ident___exception_code, false),
380 raii3(Ident_GetExceptionCode, false);
381
382 if(ExpectAndConsume(tok::l_paren,diag::err_expected_lparen))
383 return StmtError();
384
385 ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope);
386
Francois Pichetd7f02df2011-04-28 03:14:31 +0000387 if (getLang().Borland) {
388 Ident__exception_info->setIsPoisoned(false);
389 Ident___exception_info->setIsPoisoned(false);
390 Ident_GetExceptionInfo->setIsPoisoned(false);
391 }
John Wiegley28bbe4b2011-04-28 01:08:34 +0000392 ExprResult FilterExpr(ParseExpression());
Francois Pichetd7f02df2011-04-28 03:14:31 +0000393
394 if (getLang().Borland) {
395 Ident__exception_info->setIsPoisoned(true);
396 Ident___exception_info->setIsPoisoned(true);
397 Ident_GetExceptionInfo->setIsPoisoned(true);
398 }
John Wiegley28bbe4b2011-04-28 01:08:34 +0000399
400 if(FilterExpr.isInvalid())
401 return StmtError();
402
403 if(ExpectAndConsume(tok::r_paren,diag::err_expected_rparen))
404 return StmtError();
405
406 ParsedAttributesWithRange attrs(AttrFactory);
407 StmtResult Block(ParseCompoundStatement(attrs));
408
409 if(Block.isInvalid())
410 return move(Block);
411
412 return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.take(), Block.take());
413}
414
415/// ParseSEHFinallyBlock - Handle __finally
416///
417/// seh-finally-block:
418/// '__finally' compound-statement
419///
420StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyBlock) {
421 PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
422 raii2(Ident___abnormal_termination, false),
423 raii3(Ident_AbnormalTermination, false);
424
425 ParsedAttributesWithRange attrs(AttrFactory);
426 StmtResult Block(ParseCompoundStatement(attrs));
427 if(Block.isInvalid())
428 return move(Block);
429
430 return Actions.ActOnSEHFinallyBlock(FinallyBlock,Block.take());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000431}
432
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000433/// ParseLabeledStatement - We have an identifier and a ':' after it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000434///
435/// labeled-statement:
436/// identifier ':' statement
437/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000438///
John McCall7f040a92010-12-24 02:08:15 +0000439StmtResult Parser::ParseLabeledStatement(ParsedAttributes &attrs) {
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000440 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
441 "Not an identifier!");
442
443 Token IdentTok = Tok; // Save the whole token.
444 ConsumeToken(); // eat the identifier.
445
446 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000447
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000448 // identifier ':' statement
449 SourceLocation ColonLoc = ConsumeToken();
450
451 // Read label attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +0000452 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000453
John McCall60d7b3a2010-08-24 06:29:42 +0000454 StmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000455
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000456 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000457 if (SubStmt.isInvalid())
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000458 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Chris Lattner337e5502011-02-18 01:27:55 +0000459
460 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
461 IdentTok.getLocation());
462 if (AttributeList *Attrs = attrs.getList())
463 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
464
465 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
466 SubStmt.get());
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000467}
Reid Spencer5f016e22007-07-11 17:01:13 +0000468
469/// ParseCaseStatement
470/// labeled-statement:
471/// 'case' constant-expression ':' statement
472/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
473///
Richard Trieubb9b80c2011-04-21 21:44:26 +0000474StmtResult Parser::ParseCaseStatement(ParsedAttributes &attrs, bool MissingCase,
475 ExprResult Expr) {
Richard Smith46f11102011-04-21 22:48:40 +0000476 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Sean Huntbbd37c62009-11-21 08:43:09 +0000477 // FIXME: Use attributes?
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Chris Lattner24e1e702009-03-04 04:23:07 +0000479 // It is very very common for code to contain many case statements recursively
480 // nested, as in (but usually without indentation):
481 // case 1:
482 // case 2:
483 // case 3:
484 // case 4:
485 // case 5: etc.
486 //
487 // Parsing this naively works, but is both inefficient and can cause us to run
488 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner26140c62009-03-04 18:24:58 +0000489 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner24e1e702009-03-04 04:23:07 +0000490 // but all the grossness is constrained to ParseCaseStatement (and some
491 // wierdness in the actions), so this is just local grossness :).
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Chris Lattner24e1e702009-03-04 04:23:07 +0000493 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
494 // example above.
John McCall60d7b3a2010-08-24 06:29:42 +0000495 StmtResult TopLevelCase(true);
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Chris Lattner24e1e702009-03-04 04:23:07 +0000497 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
498 // gets updated each time a new case is parsed, and whose body is unset so
499 // far. When parsing 'case 4', this is the 'case 3' node.
Richard Trieub2fc6902011-09-09 02:16:15 +0000500 Stmt *DeepestParsedCaseStmt = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Chris Lattner24e1e702009-03-04 04:23:07 +0000502 // While we have case statements, eat and stack them.
David Majnemer0e1e69c2011-06-13 05:50:12 +0000503 SourceLocation ColonLoc;
Chris Lattner24e1e702009-03-04 04:23:07 +0000504 do {
Richard Trieubb9b80c2011-04-21 21:44:26 +0000505 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
506 ConsumeToken(); // eat the 'case'.
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000508 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000509 Actions.CodeCompleteCase(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000510 cutOffParsing();
511 return StmtError();
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000512 }
513
Chris Lattner6fb09c82009-12-10 00:38:54 +0000514 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
515 /// Disable this form of error recovery while we're parsing the case
516 /// expression.
517 ColonProtectionRAIIObject ColonProtection(*this);
518
Richard Trieubb9b80c2011-04-21 21:44:26 +0000519 ExprResult LHS(MissingCase ? Expr : ParseConstantExpression());
520 MissingCase = false;
Chris Lattner24e1e702009-03-04 04:23:07 +0000521 if (LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000522 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000523 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000524 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000525
Chris Lattner24e1e702009-03-04 04:23:07 +0000526 // GNU case range extension.
527 SourceLocation DotDotDotLoc;
John McCall60d7b3a2010-08-24 06:29:42 +0000528 ExprResult RHS;
Chris Lattner24e1e702009-03-04 04:23:07 +0000529 if (Tok.is(tok::ellipsis)) {
530 Diag(Tok, diag::ext_gnu_case_range);
531 DotDotDotLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000532
Chris Lattner24e1e702009-03-04 04:23:07 +0000533 RHS = ParseConstantExpression();
534 if (RHS.isInvalid()) {
535 SkipUntil(tok::colon);
536 return StmtError();
537 }
538 }
Chris Lattner6fb09c82009-12-10 00:38:54 +0000539
540 ColonProtection.restore();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000541
John McCallf6a3ab02011-01-22 09:28:32 +0000542 if (Tok.is(tok::colon)) {
543 ColonLoc = ConsumeToken();
544
545 // Treat "case blah;" as a typo for "case blah:".
546 } else if (Tok.is(tok::semi)) {
547 ColonLoc = ConsumeToken();
548 Diag(ColonLoc, diag::err_expected_colon_after) << "'case'"
549 << FixItHint::CreateReplacement(ColonLoc, ":");
550 } else {
Douglas Gregor662a4822010-12-23 22:56:40 +0000551 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
552 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'"
553 << FixItHint::CreateInsertion(ExpectedLoc, ":");
554 ColonLoc = ExpectedLoc;
Chris Lattner24e1e702009-03-04 04:23:07 +0000555 }
Douglas Gregor662a4822010-12-23 22:56:40 +0000556
John McCall60d7b3a2010-08-24 06:29:42 +0000557 StmtResult Case =
John McCall9ae2f072010-08-23 23:25:46 +0000558 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
559 RHS.get(), ColonLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Chris Lattner24e1e702009-03-04 04:23:07 +0000561 // If we had a sema error parsing this case, then just ignore it and
562 // continue parsing the sub-stmt.
563 if (Case.isInvalid()) {
564 if (TopLevelCase.isInvalid()) // No parsed case stmts.
565 return ParseStatement();
566 // Otherwise, just don't add it as a nested case.
567 } else {
568 // If this is the first case statement we parsed, it becomes TopLevelCase.
569 // Otherwise we link it into the current chain.
John McCallca0408f2010-08-23 06:44:23 +0000570 Stmt *NextDeepest = Case.get();
Chris Lattner24e1e702009-03-04 04:23:07 +0000571 if (TopLevelCase.isInvalid())
572 TopLevelCase = move(Case);
573 else
John McCall9ae2f072010-08-23 23:25:46 +0000574 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner24e1e702009-03-04 04:23:07 +0000575 DeepestParsedCaseStmt = NextDeepest;
576 }
Mike Stump1eb44332009-09-09 15:08:12 +0000577
Chris Lattner24e1e702009-03-04 04:23:07 +0000578 // Handle all case statements.
579 } while (Tok.is(tok::kw_case));
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Chris Lattner24e1e702009-03-04 04:23:07 +0000581 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump1eb44332009-09-09 15:08:12 +0000582
Chris Lattner24e1e702009-03-04 04:23:07 +0000583 // If we found a non-case statement, start by parsing it.
John McCall60d7b3a2010-08-24 06:29:42 +0000584 StmtResult SubStmt;
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Chris Lattner24e1e702009-03-04 04:23:07 +0000586 if (Tok.isNot(tok::r_brace)) {
587 SubStmt = ParseStatement();
588 } else {
589 // Nicely diagnose the common error "switch (X) { case 4: }", which is
590 // not valid.
David Majnemer63f04ab2011-06-14 15:24:38 +0000591 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
592 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement);
Chris Lattner24e1e702009-03-04 04:23:07 +0000593 SubStmt = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000594 }
Mike Stump1eb44332009-09-09 15:08:12 +0000595
Chris Lattner24e1e702009-03-04 04:23:07 +0000596 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000597 if (SubStmt.isInvalid())
Chris Lattner24e1e702009-03-04 04:23:07 +0000598 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Chris Lattner24e1e702009-03-04 04:23:07 +0000600 // Install the body into the most deeply-nested case.
John McCall9ae2f072010-08-23 23:25:46 +0000601 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000602
Chris Lattner24e1e702009-03-04 04:23:07 +0000603 // Return the top level parsed statement tree.
Chris Lattner26140c62009-03-04 18:24:58 +0000604 return move(TopLevelCase);
Reid Spencer5f016e22007-07-11 17:01:13 +0000605}
606
607/// ParseDefaultStatement
608/// labeled-statement:
609/// 'default' ':' statement
610/// Note that this does not parse the 'statement' at the end.
611///
John McCall7f040a92010-12-24 02:08:15 +0000612StmtResult Parser::ParseDefaultStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000613 //FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000614
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000615 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000616 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
617
Douglas Gregor662a4822010-12-23 22:56:40 +0000618 SourceLocation ColonLoc;
John McCallf6a3ab02011-01-22 09:28:32 +0000619 if (Tok.is(tok::colon)) {
620 ColonLoc = ConsumeToken();
621
622 // Treat "default;" as a typo for "default:".
623 } else if (Tok.is(tok::semi)) {
624 ColonLoc = ConsumeToken();
625 Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
626 << FixItHint::CreateReplacement(ColonLoc, ":");
627 } else {
Douglas Gregor662a4822010-12-23 22:56:40 +0000628 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
629 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
630 << FixItHint::CreateInsertion(ExpectedLoc, ":");
631 ColonLoc = ExpectedLoc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000632 }
Douglas Gregor662a4822010-12-23 22:56:40 +0000633
Reid Spencer5f016e22007-07-11 17:01:13 +0000634 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000635 if (Tok.is(tok::r_brace)) {
David Majnemer63f04ab2011-06-14 15:24:38 +0000636 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
637 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000638 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000639 }
640
John McCall60d7b3a2010-08-24 06:29:42 +0000641 StmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000642 if (SubStmt.isInvalid())
Sebastian Redl61364dd2008-12-11 19:30:53 +0000643 return StmtError();
644
Sebastian Redl117054a2008-12-28 16:13:43 +0000645 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000646 SubStmt.get(), getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +0000647}
648
Douglas Gregorbca01b42011-07-06 22:04:06 +0000649StmtResult Parser::ParseCompoundStatement(ParsedAttributes &Attr,
650 bool isStmtExpr) {
651 return ParseCompoundStatement(Attr, isStmtExpr, Scope::DeclScope);
652}
Reid Spencer5f016e22007-07-11 17:01:13 +0000653
654/// ParseCompoundStatement - Parse a "{}" block.
655///
656/// compound-statement: [C99 6.8.2]
657/// { block-item-list[opt] }
658/// [GNU] { label-declarations block-item-list } [TODO]
659///
660/// block-item-list:
661/// block-item
662/// block-item-list block-item
663///
664/// block-item:
665/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000666/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000667/// statement
668/// [OMP] openmp-directive [TODO]
669///
670/// [GNU] label-declarations:
671/// [GNU] label-declaration
672/// [GNU] label-declarations label-declaration
673///
674/// [GNU] label-declaration:
675/// [GNU] '__label__' identifier-list ';'
676///
677/// [OMP] openmp-directive: [TODO]
678/// [OMP] barrier-directive
679/// [OMP] flush-directive
680///
John McCall7f040a92010-12-24 02:08:15 +0000681StmtResult Parser::ParseCompoundStatement(ParsedAttributes &attrs,
Douglas Gregorbca01b42011-07-06 22:04:06 +0000682 bool isStmtExpr,
683 unsigned ScopeFlags) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000684 //FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000685
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000686 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000687
Chris Lattner31e05722007-08-26 06:24:45 +0000688 // Enter a scope to hold everything within the compound stmt. Compound
689 // statements can always hold declarations.
Douglas Gregorbca01b42011-07-06 22:04:06 +0000690 ParseScope CompoundScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000691
692 // Parse the statements in the body.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000693 return ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000694}
695
696
697/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000698/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000699/// consume the '}' at the end of the block. It does not manipulate the scope
700/// stack.
John McCall60d7b3a2010-08-24 06:29:42 +0000701StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump1eb44332009-09-09 15:08:12 +0000702 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerae50fa02009-03-05 00:00:31 +0000703 Tok.getLocation(),
704 "in compound statement ('{}')");
Douglas Gregor0fbda682010-09-15 14:51:05 +0000705 InMessageExpressionRAIIObject InMessage(*this, false);
706
Reid Spencer5f016e22007-07-11 17:01:13 +0000707 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
708
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000709 StmtVector Stmts(Actions);
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000710
Chris Lattner4ae493c2011-02-18 02:08:43 +0000711 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
712 // only allowed at the start of a compound stmt regardless of the language.
713 while (Tok.is(tok::kw___label__)) {
714 SourceLocation LabelLoc = ConsumeToken();
715 Diag(LabelLoc, diag::ext_gnu_local_label);
716
Chris Lattner5f9e2722011-07-23 10:55:15 +0000717 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4ae493c2011-02-18 02:08:43 +0000718 while (1) {
719 if (Tok.isNot(tok::identifier)) {
720 Diag(Tok, diag::err_expected_ident);
721 break;
722 }
723
724 IdentifierInfo *II = Tok.getIdentifierInfo();
725 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara67843042011-03-05 18:21:20 +0000726 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
Chris Lattner4ae493c2011-02-18 02:08:43 +0000727
728 if (!Tok.is(tok::comma))
729 break;
730 ConsumeToken();
731 }
732
John McCall0b7e6782011-03-24 11:26:52 +0000733 DeclSpec DS(AttrFactory);
Chris Lattner4ae493c2011-02-18 02:08:43 +0000734 DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
735 DeclsInGroup.data(), DeclsInGroup.size());
736 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
737
738 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
739 if (R.isUsable())
740 Stmts.push_back(R.release());
741 }
742
743 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000744 if (Tok.is(tok::annot_pragma_unused)) {
745 HandlePragmaUnused();
746 continue;
747 }
748
Francois Pichet62ec1f22011-09-17 17:15:52 +0000749 if (getLang().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet1e862692011-05-06 20:48:22 +0000750 Tok.is(tok::kw___if_not_exists))) {
751 ParseMicrosoftIfExistsStatement(Stmts);
752 continue;
753 }
754
John McCall60d7b3a2010-08-24 06:29:42 +0000755 StmtResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000756 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000757 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattner45a566c2007-08-27 01:01:57 +0000758 } else {
759 // __extension__ can start declarations and it can also be a unary
760 // operator for expressions. Consume multiple __extension__ markers here
761 // until we can determine which is which.
Eli Friedmanadf077f2009-01-27 08:43:38 +0000762 // FIXME: This loses extension expressions in the AST!
Chris Lattner45a566c2007-08-27 01:01:57 +0000763 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000764 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000765 ConsumeToken();
Chris Lattner39146d62008-10-20 06:51:33 +0000766
John McCall0b7e6782011-03-24 11:26:52 +0000767 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000768 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000769
Chris Lattner45a566c2007-08-27 01:01:57 +0000770 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000771 if (isDeclarationStatement()) {
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000772 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000773 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000774 ExtensionRAIIObject O(Diags);
775
Chris Lattner97144fc2009-04-02 04:16:50 +0000776 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000777 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
778 Declarator::BlockContext, DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000779 attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000780 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattner45a566c2007-08-27 01:01:57 +0000781 } else {
Eli Friedmanadf077f2009-01-27 08:43:38 +0000782 // Otherwise this was a unary __extension__ marker.
John McCall60d7b3a2010-08-24 06:29:42 +0000783 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattner043a0b52008-03-13 06:32:11 +0000784
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000785 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000786 SkipUntil(tok::semi);
787 continue;
788 }
Sebastian Redlf512e822009-01-18 18:03:53 +0000789
Sean Huntbbd37c62009-11-21 08:43:09 +0000790 // FIXME: Use attributes?
Chris Lattner39146d62008-10-20 06:51:33 +0000791 // Eat the semicolon at the end of stmt and convert the expr into a
792 // statement.
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000793 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +0000794 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattner45a566c2007-08-27 01:01:57 +0000795 }
796 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000797
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000798 if (R.isUsable())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000799 Stmts.push_back(R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000800 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000801
Reid Spencer5f016e22007-07-11 17:01:13 +0000802 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000803 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000804 Diag(Tok, diag::err_expected_rbrace);
Chris Lattnerf65086b2010-09-01 15:49:26 +0000805 Diag(LBraceLoc, diag::note_matching) << "{";
Sebastian Redl61364dd2008-12-11 19:30:53 +0000806 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000807 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000808
Reid Spencer5f016e22007-07-11 17:01:13 +0000809 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlf512e822009-01-18 18:03:53 +0000810 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redla60528c2008-12-21 12:04:03 +0000811 isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000812}
813
Chris Lattner15ff1112008-12-12 06:31:07 +0000814/// ParseParenExprOrCondition:
815/// [C ] '(' expression ')'
Chris Lattnerff871fb2008-12-12 06:35:28 +0000816/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattner15ff1112008-12-12 06:31:07 +0000817///
818/// This function parses and performs error recovery on the specified condition
819/// or expression (depending on whether we're in C++ or C mode). This function
820/// goes out of its way to recover well. It returns true if there was a parser
821/// error (the right paren couldn't be found), which indicates that the caller
822/// should try to recover harder. It returns false if the condition is
823/// successfully parsed. Note that a successful parse can still have semantic
824/// errors in the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000825bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCalld226f652010-08-21 09:40:31 +0000826 Decl *&DeclResult,
Douglas Gregor586596f2010-05-06 17:25:47 +0000827 SourceLocation Loc,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000828 bool ConvertToBoolean) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000829 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000830 if (getLang().CPlusPlus)
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000831 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000832 else {
833 ExprResult = ParseExpression();
John McCalld226f652010-08-21 09:40:31 +0000834 DeclResult = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +0000835
836 // If required, convert to a boolean value.
837 if (!ExprResult.isInvalid() && ConvertToBoolean)
838 ExprResult
John McCall9ae2f072010-08-23 23:25:46 +0000839 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000840 }
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Chris Lattner15ff1112008-12-12 06:31:07 +0000842 // If the parser was confused by the condition and we don't have a ')', try to
843 // recover by skipping ahead to a semi and bailing out. If condexp is
844 // semantically invalid but we have well formed code, keep going.
John McCalld226f652010-08-21 09:40:31 +0000845 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000846 SkipUntil(tok::semi);
847 // Skipping may have stopped if it found the containing ')'. If so, we can
848 // continue parsing the if statement.
849 if (Tok.isNot(tok::r_paren))
850 return true;
851 }
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Chris Lattner15ff1112008-12-12 06:31:07 +0000853 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000854 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner15ff1112008-12-12 06:31:07 +0000855 return false;
856}
857
858
Reid Spencer5f016e22007-07-11 17:01:13 +0000859/// ParseIfStatement
860/// if-statement: [C99 6.8.4.1]
861/// 'if' '(' expression ')' statement
862/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000863/// [C++] 'if' '(' condition ')' statement
864/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000865///
John McCall7f040a92010-12-24 02:08:15 +0000866StmtResult Parser::ParseIfStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000867 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000868
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000869 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000870 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
871
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000872 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000873 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000874 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000875 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000876 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000877
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000878 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
879
Chris Lattner22153252007-08-26 23:08:06 +0000880 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
881 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000882 //
883 // C++ 6.4p3:
884 // A name introduced by a declaration in a condition is in scope from its
885 // point of declaration until the end of the substatements controlled by the
886 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000887 // C++ 3.3.2p4:
888 // Names declared in the for-init-statement, and in the condition of if,
889 // while, for, and switch statements are local to the if, while, for, or
890 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000891 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000892 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +0000893
Reid Spencer5f016e22007-07-11 17:01:13 +0000894 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000895 ExprResult CondExp;
John McCalld226f652010-08-21 09:40:31 +0000896 Decl *CondVar = 0;
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000897 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +0000898 return StmtError();
Chris Lattner18914bc2008-12-12 06:19:11 +0000899
John McCall9ae2f072010-08-23 23:25:46 +0000900 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
Mike Stump1eb44332009-09-09 15:08:12 +0000901
Chris Lattner0ecea032007-08-22 05:28:50 +0000902 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000903 // there is no compound stmt. C90 does not have this clause. We only do this
904 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000905 //
906 // C++ 6.4p1:
907 // The substatement in a selection-statement (each substatement, in the else
908 // form of the if statement) implicitly defines a local scope.
909 //
910 // For C++ we create a scope for the condition and a new scope for
911 // substatements because:
912 // -When the 'then' scope exits, we want the condition declaration to still be
913 // active for the 'else' scope too.
914 // -Sema will detect name clashes by considering declarations of a
915 // 'ControlScope' as part of its direct subscope.
916 // -If we wanted the condition and substatement to be in the same scope, we
917 // would have to notify ParseStatement not to create a new scope. It's
918 // simpler to let it create a new scope.
919 //
Mike Stump1eb44332009-09-09 15:08:12 +0000920 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000921 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000922
Chris Lattnerb96728d2007-10-29 05:08:52 +0000923 // Read the 'then' stmt.
924 SourceLocation ThenStmtLoc = Tok.getLocation();
John McCall60d7b3a2010-08-24 06:29:42 +0000925 StmtResult ThenStmt(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000926
Chris Lattnera36ce712007-08-22 05:16:28 +0000927 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000928 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000929
Reid Spencer5f016e22007-07-11 17:01:13 +0000930 // If it has an else, parse it.
931 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000932 SourceLocation ElseStmtLoc;
John McCall60d7b3a2010-08-24 06:29:42 +0000933 StmtResult ElseStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000934
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000935 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000936 ElseLoc = ConsumeToken();
Chris Lattner966c78b2010-04-12 06:12:50 +0000937 ElseStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000938
Chris Lattner0ecea032007-08-22 05:28:50 +0000939 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000940 // there is no compound stmt. C90 does not have this clause. We only do
941 // this if the body isn't a compound statement to avoid push/pop in common
942 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000943 //
944 // C++ 6.4p1:
945 // The substatement in a selection-statement (each substatement, in the else
946 // form of the if statement) implicitly defines a local scope.
947 //
Sebastian Redl61364dd2008-12-11 19:30:53 +0000948 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000949 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000950
Reid Spencer5f016e22007-07-11 17:01:13 +0000951 ElseStmt = ParseStatement();
Chris Lattner966c78b2010-04-12 06:12:50 +0000952
Chris Lattnera36ce712007-08-22 05:16:28 +0000953 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000954 InnerScope.Exit();
Douglas Gregord2d8be62011-07-30 08:36:53 +0000955 } else if (Tok.is(tok::code_completion)) {
956 Actions.CodeCompleteAfterIf(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000957 cutOffParsing();
958 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000959 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000960
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000961 IfScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Chris Lattner18914bc2008-12-12 06:19:11 +0000963 // If the condition was invalid, discard the if statement. We could recover
964 // better by replacing it with a valid expr, but don't do that yet.
John McCalld226f652010-08-21 09:40:31 +0000965 if (CondExp.isInvalid() && !CondVar)
Chris Lattner18914bc2008-12-12 06:19:11 +0000966 return StmtError();
Chris Lattner22153252007-08-26 23:08:06 +0000967
Chris Lattnerb96728d2007-10-29 05:08:52 +0000968 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump1eb44332009-09-09 15:08:12 +0000969 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattnerb96728d2007-10-29 05:08:52 +0000970 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000971 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
972 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
973 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000974 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000975 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +0000976 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000977
Chris Lattnerb96728d2007-10-29 05:08:52 +0000978 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000979 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000980 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000981 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000982 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000983
John McCall9ae2f072010-08-23 23:25:46 +0000984 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000985 ElseLoc, ElseStmt.get());
Reid Spencer5f016e22007-07-11 17:01:13 +0000986}
987
988/// ParseSwitchStatement
989/// switch-statement:
990/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000991/// [C++] 'switch' '(' condition ')' statement
John McCall7f040a92010-12-24 02:08:15 +0000992StmtResult Parser::ParseSwitchStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000993 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000994
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000995 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000996 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
997
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000998 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000999 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +00001000 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +00001001 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001002 }
Chris Lattner22153252007-08-26 23:08:06 +00001003
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001004 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
1005
Chris Lattner22153252007-08-26 23:08:06 +00001006 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1007 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001008 //
1009 // C++ 6.4p3:
1010 // A name introduced by a declaration in a condition is in scope from its
1011 // point of declaration until the end of the substatements controlled by the
1012 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001013 // C++ 3.3.2p4:
1014 // Names declared in the for-init-statement, and in the condition of if,
1015 // while, for, and switch statements are local to the if, while, for, or
1016 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001017 //
Richard Trieubb9b80c2011-04-21 21:44:26 +00001018 unsigned ScopeFlags = Scope::BreakScope | Scope::SwitchScope;
Chris Lattner15ff1112008-12-12 06:31:07 +00001019 if (C99orCXX)
1020 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001021 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001022
1023 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +00001024 ExprResult Cond;
John McCalld226f652010-08-21 09:40:31 +00001025 Decl *CondVar = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +00001026 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redl9a920342008-12-11 19:48:14 +00001027 return StmtError();
Eli Friedman2342ef72008-12-17 22:19:57 +00001028
John McCall60d7b3a2010-08-24 06:29:42 +00001029 StmtResult Switch
John McCall9ae2f072010-08-23 23:25:46 +00001030 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001031
Douglas Gregor586596f2010-05-06 17:25:47 +00001032 if (Switch.isInvalid()) {
1033 // Skip the switch body.
1034 // FIXME: This is not optimal recovery, but parsing the body is more
1035 // dangerous due to the presence of case and default statements, which
1036 // will have no place to connect back with the switch.
Douglas Gregor4186ff42010-05-20 23:20:59 +00001037 if (Tok.is(tok::l_brace)) {
1038 ConsumeBrace();
1039 SkipUntil(tok::r_brace, false, false);
1040 } else
Douglas Gregor586596f2010-05-06 17:25:47 +00001041 SkipUntil(tok::semi);
1042 return move(Switch);
1043 }
1044
Chris Lattner0ecea032007-08-22 05:28:50 +00001045 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001046 // there is no compound stmt. C90 does not have this clause. We only do this
1047 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001048 //
1049 // C++ 6.4p1:
1050 // The substatement in a selection-statement (each substatement, in the else
1051 // form of the if statement) implicitly defines a local scope.
1052 //
1053 // See comments in ParseIfStatement for why we create a scope for the
1054 // condition and a new scope for substatement in C++.
1055 //
Mike Stump1eb44332009-09-09 15:08:12 +00001056 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001057 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +00001058
Reid Spencer5f016e22007-07-11 17:01:13 +00001059 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +00001060 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +00001061
Chris Lattner7e52de42010-01-24 01:50:29 +00001062 // Pop the scopes.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001063 InnerScope.Exit();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001064 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +00001065
Chris Lattner7e52de42010-01-24 01:50:29 +00001066 if (Body.isInvalid())
1067 // FIXME: Remove the case statement list from the Switch statement.
1068 Body = Actions.ActOnNullStmt(Tok.getLocation());
1069
John McCall9ae2f072010-08-23 23:25:46 +00001070 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Reid Spencer5f016e22007-07-11 17:01:13 +00001071}
1072
1073/// ParseWhileStatement
1074/// while-statement: [C99 6.8.5.1]
1075/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001076/// [C++] 'while' '(' condition ')' statement
John McCall7f040a92010-12-24 02:08:15 +00001077StmtResult Parser::ParseWhileStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001078 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001079
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001080 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001081 SourceLocation WhileLoc = Tok.getLocation();
1082 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001083
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001084 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001085 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +00001086 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +00001087 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001088 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001089
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001090 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
1091
Chris Lattner22153252007-08-26 23:08:06 +00001092 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1093 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001094 //
1095 // C++ 6.4p3:
1096 // A name introduced by a declaration in a condition is in scope from its
1097 // point of declaration until the end of the substatements controlled by the
1098 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001099 // C++ 3.3.2p4:
1100 // Names declared in the for-init-statement, and in the condition of if,
1101 // while, for, and switch statements are local to the if, while, for, or
1102 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001103 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001104 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001105 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001106 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1107 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +00001108 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001109 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1110 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001111
1112 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +00001113 ExprResult Cond;
John McCalld226f652010-08-21 09:40:31 +00001114 Decl *CondVar = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +00001115 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +00001116 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001117
John McCall9ae2f072010-08-23 23:25:46 +00001118 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get()));
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Chris Lattner0ecea032007-08-22 05:28:50 +00001120 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001121 // there is no compound stmt. C90 does not have this clause. We only do this
1122 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001123 //
1124 // C++ 6.5p2:
1125 // The substatement in an iteration-statement implicitly defines a local scope
1126 // which is entered and exited each time through the loop.
1127 //
1128 // See comments in ParseIfStatement for why we create a scope for the
1129 // condition and a new scope for substatement in C++.
1130 //
Mike Stump1eb44332009-09-09 15:08:12 +00001131 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001132 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +00001133
Reid Spencer5f016e22007-07-11 17:01:13 +00001134 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +00001135 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +00001136
Chris Lattner0ecea032007-08-22 05:28:50 +00001137 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001138 InnerScope.Exit();
1139 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +00001140
John McCalld226f652010-08-21 09:40:31 +00001141 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001142 return StmtError();
1143
John McCall9ae2f072010-08-23 23:25:46 +00001144 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Reid Spencer5f016e22007-07-11 17:01:13 +00001145}
1146
1147/// ParseDoStatement
1148/// do-statement: [C99 6.8.5.2]
1149/// 'do' statement 'while' '(' expression ')' ';'
1150/// Note: this lets the caller parse the end ';'.
John McCall7f040a92010-12-24 02:08:15 +00001151StmtResult Parser::ParseDoStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001152 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001153
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001154 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001155 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001156
Chris Lattner22153252007-08-26 23:08:06 +00001157 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1158 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001159 unsigned ScopeFlags;
Chris Lattner22153252007-08-26 23:08:06 +00001160 if (getLang().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001161 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +00001162 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001163 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +00001164
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001165 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001166
Chris Lattner0ecea032007-08-22 05:28:50 +00001167 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001168 // there is no compound stmt. C90 does not have this clause. We only do this
1169 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +00001170 //
1171 // C++ 6.5p2:
1172 // The substatement in an iteration-statement implicitly defines a local scope
1173 // which is entered and exited each time through the loop.
1174 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001175 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump1eb44332009-09-09 15:08:12 +00001176 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001177 Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +00001178
Reid Spencer5f016e22007-07-11 17:01:13 +00001179 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +00001180 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +00001181
Chris Lattner0ecea032007-08-22 05:28:50 +00001182 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001183 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001184
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001185 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001186 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +00001187 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +00001188 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +00001189 SkipUntil(tok::semi, false, true);
1190 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001191 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001192 }
1193 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001194
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001195 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001196 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +00001197 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001198 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001199 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001200
Chris Lattnerff871fb2008-12-12 06:35:28 +00001201 // Parse the parenthesized condition.
Douglas Gregor04895d32009-11-24 21:34:32 +00001202 SourceLocation LPLoc = ConsumeParen();
John McCall60d7b3a2010-08-24 06:29:42 +00001203 ExprResult Cond = ParseExpression();
Douglas Gregor04895d32009-11-24 21:34:32 +00001204 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LPLoc);
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001205 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001206
Sebastian Redl9a920342008-12-11 19:48:14 +00001207 if (Cond.isInvalid() || Body.isInvalid())
1208 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001209
John McCall9ae2f072010-08-23 23:25:46 +00001210 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, LPLoc,
1211 Cond.get(), RPLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001212}
1213
1214/// ParseForStatement
1215/// for-statement: [C99 6.8.5.3]
1216/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1217/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001218/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1219/// [C++] statement
Richard Smithad762fc2011-04-14 22:09:26 +00001220/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001221/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1222/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001223///
1224/// [C++] for-init-statement:
1225/// [C++] expression-statement
1226/// [C++] simple-declaration
1227///
Richard Smithad762fc2011-04-14 22:09:26 +00001228/// [C++0x] for-range-declaration:
1229/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1230/// [C++0x] for-range-initializer:
1231/// [C++0x] expression
1232/// [C++0x] braced-init-list [TODO]
John McCall7f040a92010-12-24 02:08:15 +00001233StmtResult Parser::ParseForStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001234 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001235
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001236 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001237 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001238
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001239 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001240 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +00001241 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +00001242 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001243 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001244
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001245 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001246
Chris Lattner22153252007-08-26 23:08:06 +00001247 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1248 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001249 //
1250 // C++ 6.4p3:
1251 // A name introduced by a declaration in a condition is in scope from its
1252 // point of declaration until the end of the substatements controlled by the
1253 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001254 // C++ 3.3.2p4:
1255 // Names declared in the for-init-statement, and in the condition of if,
1256 // while, for, and switch statements are local to the if, while, for, or
1257 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001258 // C++ 6.5.3p1:
1259 // Names declared in the for-init-statement are in the same declarative-region
1260 // as those declared in the condition.
1261 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001262 unsigned ScopeFlags;
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001263 if (C99orCXXorObjC)
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001264 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1265 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +00001266 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001267 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1268
1269 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001270
1271 SourceLocation LParenLoc = ConsumeParen();
John McCall60d7b3a2010-08-24 06:29:42 +00001272 ExprResult Value;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001273
Richard Smithad762fc2011-04-14 22:09:26 +00001274 bool ForEach = false, ForRange = false;
John McCall60d7b3a2010-08-24 06:29:42 +00001275 StmtResult FirstPart;
Douglas Gregoreecf38f2010-05-06 21:39:56 +00001276 bool SecondPartIsInvalid = false;
Douglas Gregor586596f2010-05-06 17:25:47 +00001277 FullExprArg SecondPart(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001278 ExprResult Collection;
Richard Smithad762fc2011-04-14 22:09:26 +00001279 ForRangeInit ForRangeInit;
Douglas Gregor586596f2010-05-06 17:25:47 +00001280 FullExprArg ThirdPart(Actions);
John McCalld226f652010-08-21 09:40:31 +00001281 Decl *SecondVar = 0;
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001282
Douglas Gregor791215b2009-09-21 20:51:25 +00001283 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001284 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001285 C99orCXXorObjC? Sema::PCC_ForInit
1286 : Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001287 cutOffParsing();
1288 return StmtError();
Douglas Gregor791215b2009-09-21 20:51:25 +00001289 }
1290
Reid Spencer5f016e22007-07-11 17:01:13 +00001291 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001292 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +00001293 // no first part, eat the ';'.
1294 ConsumeToken();
Argyrios Kyrtzidisbbc70c02008-10-05 15:50:46 +00001295 } else if (isSimpleDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +00001296 // Parse declaration, which eats the ';'.
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001297 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +00001298 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +00001299
John McCall0b7e6782011-03-24 11:26:52 +00001300 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00001301 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00001302
Richard Smithad762fc2011-04-14 22:09:26 +00001303 // In C++0x, "for (T NS:a" might not be a typo for ::
1304 bool MightBeForRangeStmt = getLang().CPlusPlus;
1305 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1306
Chris Lattner97144fc2009-04-02 04:16:50 +00001307 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001308 StmtVector Stmts(Actions);
1309 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
Richard Smithad762fc2011-04-14 22:09:26 +00001310 DeclEnd, attrs, false,
1311 MightBeForRangeStmt ?
1312 &ForRangeInit : 0);
Chris Lattnercd147752009-03-29 17:27:48 +00001313 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001314
Richard Smithad762fc2011-04-14 22:09:26 +00001315 if (ForRangeInit.ParsedForRangeDecl()) {
Richard Smith8f4fb192011-09-04 19:54:14 +00001316 if (!getLang().CPlusPlus0x)
1317 Diag(ForRangeInit.ColonLoc, diag::ext_for_range);
1318
Richard Smithad762fc2011-04-14 22:09:26 +00001319 ForRange = true;
1320 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattnercd147752009-03-29 17:27:48 +00001321 ConsumeToken();
1322 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniana7cf23a2009-11-19 22:12:37 +00001323 Actions.ActOnForEachDeclStmt(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001324 // ObjC: for (id x in expr)
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001325 ConsumeToken(); // consume 'in'
Douglas Gregorfb629412010-08-23 21:17:50 +00001326
1327 if (Tok.is(tok::code_completion)) {
1328 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001329 cutOffParsing();
1330 return StmtError();
Douglas Gregorfb629412010-08-23 21:17:50 +00001331 }
Douglas Gregor586596f2010-05-06 17:25:47 +00001332 Collection = ParseExpression();
Chris Lattnercd147752009-03-29 17:27:48 +00001333 } else {
1334 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001335 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001336 } else {
1337 Value = ParseExpression();
1338
John McCallf6a16482010-12-04 03:47:34 +00001339 ForEach = isTokIdentifier_in();
1340
Reid Spencer5f016e22007-07-11 17:01:13 +00001341 // Turn the expression into a stmt.
John McCallf6a16482010-12-04 03:47:34 +00001342 if (!Value.isInvalid()) {
1343 if (ForEach)
1344 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1345 else
1346 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
1347 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001348
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001349 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001350 ConsumeToken();
John McCallf6a16482010-12-04 03:47:34 +00001351 } else if (ForEach) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001352 ConsumeToken(); // consume 'in'
Douglas Gregorfb629412010-08-23 21:17:50 +00001353
1354 if (Tok.is(tok::code_completion)) {
1355 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001356 cutOffParsing();
1357 return StmtError();
Douglas Gregorfb629412010-08-23 21:17:50 +00001358 }
Douglas Gregor586596f2010-05-06 17:25:47 +00001359 Collection = ParseExpression();
Chris Lattner682bf922009-03-29 16:50:03 +00001360 } else {
Douglas Gregorb72c7782011-02-17 03:38:46 +00001361 if (!Value.isInvalid()) {
1362 Diag(Tok, diag::err_expected_semi_for);
1363 } else {
1364 // Skip until semicolon or rparen, don't consume it.
1365 SkipUntil(tok::r_paren, true, true);
1366 if (Tok.is(tok::semi))
1367 ConsumeToken();
1368 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001369 }
1370 }
Richard Smithad762fc2011-04-14 22:09:26 +00001371 if (!ForEach && !ForRange) {
John McCall9ae2f072010-08-23 23:25:46 +00001372 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001373 // Parse the second part of the for specifier.
1374 if (Tok.is(tok::semi)) { // for (...;;
1375 // no second part.
Douglas Gregorb72c7782011-02-17 03:38:46 +00001376 } else if (Tok.is(tok::r_paren)) {
1377 // missing both semicolons.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001378 } else {
John McCall60d7b3a2010-08-24 06:29:42 +00001379 ExprResult Second;
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001380 if (getLang().CPlusPlus)
Douglas Gregor586596f2010-05-06 17:25:47 +00001381 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1382 else {
1383 Second = ParseExpression();
1384 if (!Second.isInvalid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00001385 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001386 Second.get());
Douglas Gregor586596f2010-05-06 17:25:47 +00001387 }
Douglas Gregoreecf38f2010-05-06 21:39:56 +00001388 SecondPartIsInvalid = Second.isInvalid();
John McCall9ae2f072010-08-23 23:25:46 +00001389 SecondPart = Actions.MakeFullExpr(Second.get());
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001390 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001391
Douglas Gregorb72c7782011-02-17 03:38:46 +00001392 if (Tok.isNot(tok::semi)) {
1393 if (!SecondPartIsInvalid || SecondVar)
1394 Diag(Tok, diag::err_expected_semi_for);
1395 else
1396 // Skip until semicolon or rparen, don't consume it.
1397 SkipUntil(tok::r_paren, true, true);
1398 }
1399
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001400 if (Tok.is(tok::semi)) {
1401 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001402 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001403
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001404 // Parse the third part of the for specifier.
Douglas Gregor586596f2010-05-06 17:25:47 +00001405 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCall60d7b3a2010-08-24 06:29:42 +00001406 ExprResult Third = ParseExpression();
John McCall9ae2f072010-08-23 23:25:46 +00001407 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregor586596f2010-05-06 17:25:47 +00001408 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001409 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001410 // Match the ')'.
1411 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001412
Richard Smithad762fc2011-04-14 22:09:26 +00001413 // We need to perform most of the semantic analysis for a C++0x for-range
1414 // statememt before parsing the body, in order to be able to deduce the type
1415 // of an auto-typed loop variable.
1416 StmtResult ForRangeStmt;
John McCall990567c2011-07-27 01:07:15 +00001417 if (ForRange) {
Richard Smithad762fc2011-04-14 22:09:26 +00001418 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, LParenLoc,
1419 FirstPart.take(),
1420 ForRangeInit.ColonLoc,
1421 ForRangeInit.RangeExpr.get(),
1422 RParenLoc);
1423
John McCall990567c2011-07-27 01:07:15 +00001424
1425 // Similarly, we need to do the semantic analysis for a for-range
1426 // statement immediately in order to close over temporaries correctly.
1427 } else if (ForEach) {
1428 if (!Collection.isInvalid())
1429 Collection =
1430 Actions.ActOnObjCForCollectionOperand(ForLoc, Collection.take());
1431 }
1432
Chris Lattner0ecea032007-08-22 05:28:50 +00001433 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001434 // there is no compound stmt. C90 does not have this clause. We only do this
1435 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001436 //
1437 // C++ 6.5p2:
1438 // The substatement in an iteration-statement implicitly defines a local scope
1439 // which is entered and exited each time through the loop.
1440 //
1441 // See comments in ParseIfStatement for why we create a scope for
1442 // for-init-statement/condition and a new scope for substatement in C++.
1443 //
Mike Stump1eb44332009-09-09 15:08:12 +00001444 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001445 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001446
Reid Spencer5f016e22007-07-11 17:01:13 +00001447 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +00001448 StmtResult Body(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001449
Chris Lattner0ecea032007-08-22 05:28:50 +00001450 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001451 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001452
Reid Spencer5f016e22007-07-11 17:01:13 +00001453 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001454 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001455
1456 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001457 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001458
Richard Smithad762fc2011-04-14 22:09:26 +00001459 if (ForEach)
Richard Smithad762fc2011-04-14 22:09:26 +00001460 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
1461 FirstPart.take(),
1462 Collection.take(), RParenLoc,
1463 Body.take());
Mike Stump1eb44332009-09-09 15:08:12 +00001464
Richard Smithad762fc2011-04-14 22:09:26 +00001465 if (ForRange)
1466 return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take());
1467
1468 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.take(), SecondPart,
1469 SecondVar, ThirdPart, RParenLoc, Body.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001470}
1471
1472/// ParseGotoStatement
1473/// jump-statement:
1474/// 'goto' identifier ';'
1475/// [GNU] 'goto' '*' expression ';'
1476///
1477/// Note: this lets the caller parse the end ';'.
1478///
John McCall7f040a92010-12-24 02:08:15 +00001479StmtResult Parser::ParseGotoStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001480 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001481
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001482 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001483 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001484
John McCall60d7b3a2010-08-24 06:29:42 +00001485 StmtResult Res;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001486 if (Tok.is(tok::identifier)) {
Chris Lattner337e5502011-02-18 01:27:55 +00001487 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1488 Tok.getLocation());
1489 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Reid Spencer5f016e22007-07-11 17:01:13 +00001490 ConsumeToken();
Eli Friedmanf01fdff2009-04-28 00:51:18 +00001491 } else if (Tok.is(tok::star)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001492 // GNU indirect goto extension.
1493 Diag(Tok, diag::ext_gnu_indirect_goto);
1494 SourceLocation StarLoc = ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001495 ExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001496 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001497 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001498 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001499 }
John McCall9ae2f072010-08-23 23:25:46 +00001500 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattner95cfb852007-07-22 04:13:33 +00001501 } else {
1502 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +00001503 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001504 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001505
Sebastian Redl9a920342008-12-11 19:48:14 +00001506 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001507}
1508
1509/// ParseContinueStatement
1510/// jump-statement:
1511/// 'continue' ';'
1512///
1513/// Note: this lets the caller parse the end ';'.
1514///
John McCall7f040a92010-12-24 02:08:15 +00001515StmtResult Parser::ParseContinueStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001516 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001517
Reid Spencer5f016e22007-07-11 17:01:13 +00001518 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001519 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001520}
1521
1522/// ParseBreakStatement
1523/// jump-statement:
1524/// 'break' ';'
1525///
1526/// Note: this lets the caller parse the end ';'.
1527///
John McCall7f040a92010-12-24 02:08:15 +00001528StmtResult Parser::ParseBreakStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001529 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001530
Reid Spencer5f016e22007-07-11 17:01:13 +00001531 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001532 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001533}
1534
1535/// ParseReturnStatement
1536/// jump-statement:
1537/// 'return' expression[opt] ';'
John McCall7f040a92010-12-24 02:08:15 +00001538StmtResult Parser::ParseReturnStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001539 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001540
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001541 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001542 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001543
John McCall60d7b3a2010-08-24 06:29:42 +00001544 ExprResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001545 if (Tok.isNot(tok::semi)) {
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001546 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001547 Actions.CodeCompleteReturn(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001548 cutOffParsing();
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001549 return StmtError();
1550 }
1551
Douglas Gregor6f4596c2011-03-11 23:10:44 +00001552 // FIXME: This is a hack to allow something like C++0x's generalized
1553 // initializer lists, but only enough of this feature to allow Clang to
1554 // parse libstdc++ 4.5's headers.
1555 if (Tok.is(tok::l_brace) && getLang().CPlusPlus) {
1556 R = ParseInitializer();
1557 if (R.isUsable() && !getLang().CPlusPlus0x)
1558 Diag(R.get()->getLocStart(), diag::ext_generalized_initializer_lists)
1559 << R.get()->getSourceRange();
1560 } else
1561 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001562 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001563 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001564 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001565 }
1566 }
John McCall9ae2f072010-08-23 23:25:46 +00001567 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001568}
1569
Steve Naroff5f8aa692008-02-11 23:15:56 +00001570/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1571/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001572StmtResult Parser::FuzzyParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
1573 SourceLocation EndLoc;
Steve Naroffb746ce82008-02-07 23:24:32 +00001574 if (Tok.is(tok::l_brace)) {
1575 unsigned short savedBraceCount = BraceCount;
1576 do {
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001577 EndLoc = Tok.getLocation();
Steve Naroffb746ce82008-02-07 23:24:32 +00001578 ConsumeAnyToken();
1579 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump1eb44332009-09-09 15:08:12 +00001580 } else {
Steve Naroffb746ce82008-02-07 23:24:32 +00001581 // From the MS website: If used without braces, the __asm keyword means
1582 // that the rest of the line is an assembly-language statement.
1583 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001584 SourceLocation TokLoc = Tok.getLocation();
Chandler Carruth64211622011-07-25 21:09:52 +00001585 unsigned LineNo = SrcMgr.getExpansionLineNumber(TokLoc);
Steve Naroff36280972008-02-08 18:01:27 +00001586 do {
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001587 EndLoc = TokLoc;
Steve Naroff36280972008-02-08 18:01:27 +00001588 ConsumeAnyToken();
1589 TokLoc = Tok.getLocation();
Chandler Carruth64211622011-07-25 21:09:52 +00001590 } while ((SrcMgr.getExpansionLineNumber(TokLoc) == LineNo) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001591 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff36280972008-02-08 18:01:27 +00001592 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001593 }
Mike Stump95059b52009-12-11 00:04:56 +00001594 Token t;
1595 t.setKind(tok::string_literal);
Chris Lattnere7896852010-08-17 16:00:12 +00001596 t.setLiteralData("\"/*FIXME: not done*/\"");
Mike Stump95059b52009-12-11 00:04:56 +00001597 t.clearFlag(Token::NeedsCleaning);
Chris Lattnere7896852010-08-17 16:00:12 +00001598 t.setLength(21);
Sean Hunt6cf75022010-08-30 17:47:05 +00001599 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
Mike Stump95059b52009-12-11 00:04:56 +00001600 ExprVector Constraints(Actions);
1601 ExprVector Exprs(Actions);
1602 ExprVector Clobbers(Actions);
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001603 return Actions.ActOnAsmStmt(AsmLoc, true, true, 0, 0, 0,
Mike Stump95059b52009-12-11 00:04:56 +00001604 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001605 AsmString.take(), move_arg(Clobbers),
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001606 EndLoc, true);
Steve Naroffd62701b2008-02-07 03:50:06 +00001607}
1608
Reid Spencer5f016e22007-07-11 17:01:13 +00001609/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001610/// asm-statement:
1611/// gnu-asm-statement
1612/// ms-asm-statement
1613///
1614/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001615/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1616///
1617/// [GNU] asm-argument:
1618/// asm-string-literal
1619/// asm-string-literal ':' asm-operands[opt]
1620/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1621/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1622/// ':' asm-clobbers
1623///
1624/// [GNU] asm-clobbers:
1625/// asm-string-literal
1626/// asm-clobbers ',' asm-string-literal
1627///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001628/// [MS] ms-asm-statement:
1629/// '__asm' assembly-instruction ';'[opt]
1630/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1631///
1632/// [MS] assembly-instruction-list:
1633/// assembly-instruction ';'[opt]
1634/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1635///
John McCall60d7b3a2010-08-24 06:29:42 +00001636StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001637 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001638 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001639
Francois Pichet62ec1f22011-09-17 17:15:52 +00001640 if (getLang().MicrosoftExt && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001641 msAsm = true;
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001642 return FuzzyParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffd62701b2008-02-07 03:50:06 +00001643 }
John McCall0b7e6782011-03-24 11:26:52 +00001644 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00001645 SourceLocation Loc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00001646 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redl9a920342008-12-11 19:48:14 +00001647
Reid Spencer5f016e22007-07-11 17:01:13 +00001648 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1649 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001650 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001651 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001652 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001653
Reid Spencer5f016e22007-07-11 17:01:13 +00001654 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001655 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001656 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001657 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001658 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001659 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001660 }
1661 Loc = ConsumeParen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001662
John McCall60d7b3a2010-08-24 06:29:42 +00001663 ExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001664 if (AsmString.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001665 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001666
Chris Lattner5f9e2722011-07-23 10:55:15 +00001667 SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001668 ExprVector Constraints(Actions);
1669 ExprVector Exprs(Actions);
1670 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001671
Anders Carlssondfab34a2008-02-05 23:03:50 +00001672 if (Tok.is(tok::r_paren)) {
Chris Lattner64cb4752009-12-20 23:00:41 +00001673 // We have a simple asm expression like 'asm("foo")'.
1674 SourceLocation RParenLoc = ConsumeParen();
1675 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1676 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1677 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001678 AsmString.take(), move_arg(Clobbers),
Chris Lattner64cb4752009-12-20 23:00:41 +00001679 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001680 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001681
Chris Lattner64cb4752009-12-20 23:00:41 +00001682 // Parse Outputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001683 bool AteExtraColon = false;
1684 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1685 // In C++ mode, parse "::" like ": :".
1686 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattner64cb4752009-12-20 23:00:41 +00001687 ConsumeToken();
1688
Chris Lattner64056462009-12-20 23:08:04 +00001689 if (!AteExtraColon &&
1690 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001691 return StmtError();
1692 }
Chris Lattner64056462009-12-20 23:08:04 +00001693
Chris Lattner64cb4752009-12-20 23:00:41 +00001694 unsigned NumOutputs = Names.size();
1695
1696 // Parse Inputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001697 if (AteExtraColon ||
1698 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1699 // In C++ mode, parse "::" like ": :".
1700 if (AteExtraColon)
1701 AteExtraColon = false;
1702 else {
1703 AteExtraColon = Tok.is(tok::coloncolon);
1704 ConsumeToken();
1705 }
1706
1707 if (!AteExtraColon &&
1708 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001709 return StmtError();
1710 }
1711
1712 assert(Names.size() == Constraints.size() &&
1713 Constraints.size() == Exprs.size() &&
1714 "Input operand size mismatch!");
1715
1716 unsigned NumInputs = Names.size() - NumOutputs;
1717
1718 // Parse the clobbers, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001719 if (AteExtraColon || Tok.is(tok::colon)) {
1720 if (!AteExtraColon)
1721 ConsumeToken();
Chris Lattner64cb4752009-12-20 23:00:41 +00001722
Chandler Carruth102e1b62010-07-22 07:11:21 +00001723 // Parse the asm-string list for clobbers if present.
1724 if (Tok.isNot(tok::r_paren)) {
1725 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +00001726 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattner64cb4752009-12-20 23:00:41 +00001727
Chandler Carruth102e1b62010-07-22 07:11:21 +00001728 if (Clobber.isInvalid())
1729 break;
Chris Lattner64cb4752009-12-20 23:00:41 +00001730
Chandler Carruth102e1b62010-07-22 07:11:21 +00001731 Clobbers.push_back(Clobber.release());
Chris Lattner64cb4752009-12-20 23:00:41 +00001732
Chandler Carruth102e1b62010-07-22 07:11:21 +00001733 if (Tok.isNot(tok::comma)) break;
1734 ConsumeToken();
1735 }
Chris Lattner64cb4752009-12-20 23:00:41 +00001736 }
1737 }
1738
1739 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
1740 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001741 NumOutputs, NumInputs, Names.data(),
Sebastian Redlf512e822009-01-18 18:03:53 +00001742 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001743 AsmString.take(), move_arg(Clobbers),
Sebastian Redl3037ed02009-01-18 16:53:17 +00001744 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001745}
1746
1747/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattner64cb4752009-12-20 23:00:41 +00001748/// asm-statement, assuming the leading ':' token was eaten.
Reid Spencer5f016e22007-07-11 17:01:13 +00001749///
1750/// [GNU] asm-operands:
1751/// asm-operand
1752/// asm-operands ',' asm-operand
1753///
1754/// [GNU] asm-operand:
1755/// asm-string-literal '(' expression ')'
1756/// '[' identifier ']' asm-string-literal '(' expression ')'
1757///
Daniel Dunbar5ffe14c2009-10-18 20:26:27 +00001758//
1759// FIXME: Avoid unnecessary std::string trashing.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001760bool Parser::ParseAsmOperandsOpt(SmallVectorImpl<IdentifierInfo *> &Names,
Richard Trieuf81e5a92011-09-09 02:00:50 +00001761 SmallVectorImpl<Expr *> &Constraints,
1762 SmallVectorImpl<Expr *> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001763 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001764 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001765 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001766
1767 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001768 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001769 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001770 SourceLocation Loc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00001771
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001772 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001773 Diag(Tok, diag::err_expected_ident);
1774 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001775 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001776 }
Mike Stump1eb44332009-09-09 15:08:12 +00001777
Anders Carlssonb235fc22007-11-22 01:36:19 +00001778 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001779 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001780
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001781 Names.push_back(II);
Reid Spencer5f016e22007-07-11 17:01:13 +00001782 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001783 } else
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001784 Names.push_back(0);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001785
John McCall60d7b3a2010-08-24 06:29:42 +00001786 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001787 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001788 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001789 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001790 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001791 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001792
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001793 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001794 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001795 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001796 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001797 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001798
Reid Spencer5f016e22007-07-11 17:01:13 +00001799 // Read the parenthesized expression.
Eli Friedman72056a22009-05-03 07:49:42 +00001800 SourceLocation OpenLoc = ConsumeParen();
John McCall60d7b3a2010-08-24 06:29:42 +00001801 ExprResult Res(ParseExpression());
Eli Friedman72056a22009-05-03 07:49:42 +00001802 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001803 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001804 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001805 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001806 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001807 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001808 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001809 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001810 ConsumeToken();
1811 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001812
1813 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001814}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001815
Douglas Gregorc9977d02011-03-16 17:05:57 +00001816Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner40e9bc82009-03-05 00:49:17 +00001817 assert(Tok.is(tok::l_brace));
1818 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001819
Douglas Gregorc9977d02011-03-16 17:05:57 +00001820 if (PP.isCodeCompletionEnabled()) {
1821 if (trySkippingFunctionBodyForCodeCompletion()) {
1822 BodyScope.Exit();
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001823 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregorc9977d02011-03-16 17:05:57 +00001824 }
1825 }
1826
John McCallf312b1e2010-08-26 23:41:50 +00001827 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1828 "parsing function body");
Mike Stump1eb44332009-09-09 15:08:12 +00001829
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001830 // Do not enter a scope for the brace, as the arguments are in the same scope
1831 // (the function body) as the body itself. Instead, just read the statement
1832 // list and put it into a CompoundStmt for safe keeping.
John McCall60d7b3a2010-08-24 06:29:42 +00001833 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl61364dd2008-12-11 19:30:53 +00001834
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001835 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001836 if (FnBody.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001837 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner40e9bc82009-03-05 00:49:17 +00001838 MultiStmtArg(Actions), false);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001839
Douglas Gregorc9977d02011-03-16 17:05:57 +00001840 BodyScope.Exit();
John McCall9ae2f072010-08-23 23:25:46 +00001841 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001842}
Sebastian Redla0fd8652008-12-21 16:41:36 +00001843
Sebastian Redld3a413d2009-04-26 20:35:05 +00001844/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1845///
1846/// function-try-block:
1847/// 'try' ctor-initializer[opt] compound-statement handler-seq
1848///
Douglas Gregorc9977d02011-03-16 17:05:57 +00001849Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redld3a413d2009-04-26 20:35:05 +00001850 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1851 SourceLocation TryLoc = ConsumeToken();
1852
John McCallf312b1e2010-08-26 23:41:50 +00001853 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1854 "parsing function try block");
Sebastian Redld3a413d2009-04-26 20:35:05 +00001855
1856 // Constructor initializer list?
1857 if (Tok.is(tok::colon))
1858 ParseConstructorInitializer(Decl);
Douglas Gregor2eef4272011-09-07 20:36:12 +00001859 else
1860 Actions.ActOnDefaultCtorInitializers(Decl);
1861
Douglas Gregorc9977d02011-03-16 17:05:57 +00001862 if (PP.isCodeCompletionEnabled()) {
1863 if (trySkippingFunctionBodyForCodeCompletion()) {
1864 BodyScope.Exit();
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001865 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregorc9977d02011-03-16 17:05:57 +00001866 }
1867 }
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001868
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001869 SourceLocation LBraceLoc = Tok.getLocation();
John McCall60d7b3a2010-08-24 06:29:42 +00001870 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redld3a413d2009-04-26 20:35:05 +00001871 // If we failed to parse the try-catch, we just give the function an empty
1872 // compound statement as the body.
1873 if (FnBody.isInvalid())
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001874 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redld3a413d2009-04-26 20:35:05 +00001875 MultiStmtArg(Actions), false);
1876
Douglas Gregorc9977d02011-03-16 17:05:57 +00001877 BodyScope.Exit();
John McCall9ae2f072010-08-23 23:25:46 +00001878 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redld3a413d2009-04-26 20:35:05 +00001879}
1880
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001881bool Parser::trySkippingFunctionBodyForCodeCompletion() {
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001882 assert(Tok.is(tok::l_brace));
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001883 assert(PP.isCodeCompletionEnabled() &&
1884 "Should only be called when in code-completion mode");
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001885
1886 // We're in code-completion mode. Skip parsing for all function bodies unless
1887 // the body contains the code-completion point.
1888 TentativeParsingAction PA(*this);
1889 ConsumeBrace();
1890 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
1891 /*StopAtCodeCompletion=*/true)) {
1892 PA.Commit();
1893 return true;
1894 }
1895
1896 PA.Revert();
1897 return false;
1898}
1899
Sebastian Redla0fd8652008-12-21 16:41:36 +00001900/// ParseCXXTryBlock - Parse a C++ try-block.
1901///
1902/// try-block:
1903/// 'try' compound-statement handler-seq
1904///
John McCall7f040a92010-12-24 02:08:15 +00001905StmtResult Parser::ParseCXXTryBlock(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001906 // FIXME: Add attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001907
Sebastian Redla0fd8652008-12-21 16:41:36 +00001908 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1909
1910 SourceLocation TryLoc = ConsumeToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001911 return ParseCXXTryBlockCommon(TryLoc);
1912}
1913
1914/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1915/// function-try-block.
1916///
1917/// try-block:
1918/// 'try' compound-statement handler-seq
1919///
1920/// function-try-block:
1921/// 'try' ctor-initializer[opt] compound-statement handler-seq
1922///
1923/// handler-seq:
1924/// handler handler-seq[opt]
1925///
John Wiegley28bbe4b2011-04-28 01:08:34 +00001926/// [Borland] try-block:
1927/// 'try' compound-statement seh-except-block
1928/// 'try' compound-statment seh-finally-block
1929///
John McCall60d7b3a2010-08-24 06:29:42 +00001930StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redla0fd8652008-12-21 16:41:36 +00001931 if (Tok.isNot(tok::l_brace))
1932 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Sean Huntbbd37c62009-11-21 08:43:09 +00001933 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall0b7e6782011-03-24 11:26:52 +00001934 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregorbca01b42011-07-06 22:04:06 +00001935 StmtResult TryBlock(ParseCompoundStatement(attrs, /*isStmtExpr=*/false,
1936 Scope::DeclScope|Scope::TryScope));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001937 if (TryBlock.isInvalid())
1938 return move(TryBlock);
1939
John Wiegley28bbe4b2011-04-28 01:08:34 +00001940 // Borland allows SEH-handlers with 'try'
1941 if(Tok.is(tok::kw___except) || Tok.is(tok::kw___finally)) {
1942 // TODO: Factor into common return ParseSEHHandlerCommon(...)
1943 StmtResult Handler;
1944 if(Tok.is(tok::kw___except)) {
1945 SourceLocation Loc = ConsumeToken();
1946 Handler = ParseSEHExceptBlock(Loc);
1947 }
1948 else {
1949 SourceLocation Loc = ConsumeToken();
1950 Handler = ParseSEHFinallyBlock(Loc);
1951 }
1952 if(Handler.isInvalid())
1953 return move(Handler);
John McCall7f040a92010-12-24 02:08:15 +00001954
John Wiegley28bbe4b2011-04-28 01:08:34 +00001955 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
1956 TryLoc,
1957 TryBlock.take(),
1958 Handler.take());
Sebastian Redla0fd8652008-12-21 16:41:36 +00001959 }
John Wiegley28bbe4b2011-04-28 01:08:34 +00001960 else {
1961 StmtVector Handlers(Actions);
1962 MaybeParseCXX0XAttributes(attrs);
1963 ProhibitAttributes(attrs);
Sebastian Redla0fd8652008-12-21 16:41:36 +00001964
John Wiegley28bbe4b2011-04-28 01:08:34 +00001965 if (Tok.isNot(tok::kw_catch))
1966 return StmtError(Diag(Tok, diag::err_expected_catch));
1967 while (Tok.is(tok::kw_catch)) {
1968 StmtResult Handler(ParseCXXCatchBlock());
1969 if (!Handler.isInvalid())
1970 Handlers.push_back(Handler.release());
1971 }
1972 // Don't bother creating the full statement if we don't have any usable
1973 // handlers.
1974 if (Handlers.empty())
1975 return StmtError();
1976
1977 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
1978 }
Sebastian Redla0fd8652008-12-21 16:41:36 +00001979}
1980
1981/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1982///
1983/// handler:
1984/// 'catch' '(' exception-declaration ')' compound-statement
1985///
1986/// exception-declaration:
1987/// type-specifier-seq declarator
1988/// type-specifier-seq abstract-declarator
1989/// type-specifier-seq
1990/// '...'
1991///
John McCall60d7b3a2010-08-24 06:29:42 +00001992StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redla0fd8652008-12-21 16:41:36 +00001993 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1994
1995 SourceLocation CatchLoc = ConsumeToken();
1996
1997 SourceLocation LParenLoc = Tok.getLocation();
1998 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1999 return StmtError();
2000
2001 // C++ 3.3.2p3:
2002 // The name in a catch exception-declaration is local to the handler and
2003 // shall not be redeclared in the outermost block of the handler.
2004 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
2005
2006 // exception-declaration is equivalent to '...' or a parameter-declaration
2007 // without default arguments.
John McCalld226f652010-08-21 09:40:31 +00002008 Decl *ExceptionDecl = 0;
Sebastian Redla0fd8652008-12-21 16:41:36 +00002009 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00002010 DeclSpec DS(AttrFactory);
Sebastian Redl4b07b292008-12-22 19:15:10 +00002011 if (ParseCXXTypeSpecifierSeq(DS))
2012 return StmtError();
Sebastian Redla0fd8652008-12-21 16:41:36 +00002013 Declarator ExDecl(DS, Declarator::CXXCatchContext);
2014 ParseDeclarator(ExDecl);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002015 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redla0fd8652008-12-21 16:41:36 +00002016 } else
2017 ConsumeToken();
2018
2019 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
2020 return StmtError();
2021
2022 if (Tok.isNot(tok::l_brace))
2023 return StmtError(Diag(Tok, diag::err_expected_lbrace));
2024
Sean Huntbbd37c62009-11-21 08:43:09 +00002025 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall0b7e6782011-03-24 11:26:52 +00002026 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002027 StmtResult Block(ParseCompoundStatement(attrs));
Sebastian Redla0fd8652008-12-21 16:41:36 +00002028 if (Block.isInvalid())
2029 return move(Block);
2030
John McCall9ae2f072010-08-23 23:25:46 +00002031 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redla0fd8652008-12-21 16:41:36 +00002032}
Francois Pichet1e862692011-05-06 20:48:22 +00002033
2034void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Francois Pichetf9860382011-05-07 17:30:27 +00002035 bool Result;
2036 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet1e862692011-05-06 20:48:22 +00002037 return;
Francois Pichet1e862692011-05-06 20:48:22 +00002038
Francois Pichet1e862692011-05-06 20:48:22 +00002039 if (Tok.isNot(tok::l_brace)) {
2040 Diag(Tok, diag::err_expected_lbrace);
2041 return;
2042 }
2043 ConsumeBrace();
2044
Francois Pichetf9860382011-05-07 17:30:27 +00002045 // Condition is false skip all inside the {}.
2046 if (!Result) {
Francois Pichet1e862692011-05-06 20:48:22 +00002047 SkipUntil(tok::r_brace, false);
2048 return;
2049 }
2050
2051 // Condition is true, parse the statements.
2052 while (Tok.isNot(tok::r_brace)) {
2053 StmtResult R = ParseStatementOrDeclaration(Stmts, false);
2054 if (R.isUsable())
2055 Stmts.push_back(R.release());
2056 }
2057
2058 if (Tok.isNot(tok::r_brace)) {
2059 Diag(Tok, diag::err_expected_rbrace);
2060 return;
2061 }
2062 ConsumeBrace();
2063}