blob: 47a9ecc60451b1d0ed5803b2367e2a93101651cd [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);
Douglas Gregorc8bddde2010-05-28 00:22:41 +0000103 ConsumeCodeCompletionToken();
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000104 return ParseStatementOrDeclaration(Stmts, OnlyStatement);
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 Kyrtzidisb7d98d32011-04-27 05:04:02 +0000227 SourceLocation LeadingEmptyMacroLoc;
228 if (Tok.hasLeadingEmptyMacro())
229 LeadingEmptyMacroLoc = PP.getLastEmptyMacroInstantiationLoc();
230 return Actions.ActOnNullStmt(ConsumeToken(), LeadingEmptyMacroLoc);
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000231 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000232
Reid Spencer5f016e22007-07-11 17:01:13 +0000233 case tok::kw_if: // C99 6.8.4.1: if-statement
John McCall7f040a92010-12-24 02:08:15 +0000234 return ParseIfStatement(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +0000235 case tok::kw_switch: // C99 6.8.4.2: switch-statement
John McCall7f040a92010-12-24 02:08:15 +0000236 return ParseSwitchStatement(attrs);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000237
Reid Spencer5f016e22007-07-11 17:01:13 +0000238 case tok::kw_while: // C99 6.8.5.1: while-statement
John McCall7f040a92010-12-24 02:08:15 +0000239 return ParseWhileStatement(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 case tok::kw_do: // C99 6.8.5.2: do-statement
John McCall7f040a92010-12-24 02:08:15 +0000241 Res = ParseDoStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000242 SemiError = "do/while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 break;
244 case tok::kw_for: // C99 6.8.5.3: for-statement
John McCall7f040a92010-12-24 02:08:15 +0000245 return ParseForStatement(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +0000246
247 case tok::kw_goto: // C99 6.8.6.1: goto-statement
John McCall7f040a92010-12-24 02:08:15 +0000248 Res = ParseGotoStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000249 SemiError = "goto";
Reid Spencer5f016e22007-07-11 17:01:13 +0000250 break;
251 case tok::kw_continue: // C99 6.8.6.2: continue-statement
John McCall7f040a92010-12-24 02:08:15 +0000252 Res = ParseContinueStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000253 SemiError = "continue";
Reid Spencer5f016e22007-07-11 17:01:13 +0000254 break;
255 case tok::kw_break: // C99 6.8.6.3: break-statement
John McCall7f040a92010-12-24 02:08:15 +0000256 Res = ParseBreakStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000257 SemiError = "break";
Reid Spencer5f016e22007-07-11 17:01:13 +0000258 break;
259 case tok::kw_return: // C99 6.8.6.4: return-statement
John McCall7f040a92010-12-24 02:08:15 +0000260 Res = ParseReturnStatement(attrs);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000261 SemiError = "return";
Reid Spencer5f016e22007-07-11 17:01:13 +0000262 break;
Sebastian Redl61364dd2008-12-11 19:30:53 +0000263
Sebastian Redla0fd8652008-12-21 16:41:36 +0000264 case tok::kw_asm: {
John McCall7f040a92010-12-24 02:08:15 +0000265 ProhibitAttributes(attrs);
Steve Naroffd62701b2008-02-07 03:50:06 +0000266 bool msAsm = false;
267 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidisbf8cafa2010-11-02 02:33:08 +0000268 Res = Actions.ActOnFinishFullStmt(Res.get());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000269 if (msAsm) return move(Res);
Chris Lattner6869d8e2009-06-14 00:07:48 +0000270 SemiError = "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +0000271 break;
272 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000273
Sebastian Redla0fd8652008-12-21 16:41:36 +0000274 case tok::kw_try: // C++ 15: try-block
John McCall7f040a92010-12-24 02:08:15 +0000275 return ParseCXXTryBlock(attrs);
John Wiegley28bbe4b2011-04-28 01:08:34 +0000276
277 case tok::kw___try:
278 return ParseSEHTryBlock(attrs);
Sebastian Redla0fd8652008-12-21 16:41:36 +0000279 }
280
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000282 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000284 } else if (!Res.isInvalid()) {
Chris Lattner7b3684a2009-06-14 00:23:56 +0000285 // If the result was valid, then we do want to diagnose this. Use
286 // ExpectAndConsume to emit the diagnostic, even though we know it won't
287 // succeed.
288 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner19504402008-11-13 18:52:53 +0000289 // Skip until we see a } or ;, but don't eat it.
290 SkipUntil(tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 }
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Sebastian Redl61364dd2008-12-11 19:30:53 +0000293 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +0000294}
295
Douglas Gregor312eadb2011-04-24 05:37:28 +0000296/// \brief Parse an expression statement.
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000297StmtResult Parser::ParseExprStatement(ParsedAttributes &Attrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +0000298 // If a case keyword is missing, this is where it should be inserted.
299 Token OldToken = Tok;
300
301 // FIXME: Use the attributes
302 // expression[opt] ';'
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000303 ExprResult Expr(ParseExpression());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000304 if (Expr.isInvalid()) {
305 // If the expression is invalid, skip ahead to the next semicolon or '}'.
306 // Not doing this opens us up to the possibility of infinite loops if
307 // ParseExpression does not consume any tokens.
308 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
309 if (Tok.is(tok::semi))
310 ConsumeToken();
311 return StmtError();
312 }
313
314 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
315 Actions.CheckCaseExpression(Expr.get())) {
316 // If a constant expression is followed by a colon inside a switch block,
317 // suggest a missing case keyword.
318 Diag(OldToken, diag::err_expected_case_before_expression)
319 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
320
321 // Recover parsing as a case statement.
322 return ParseCaseStatement(Attrs, /*MissingCase=*/true, Expr);
323 }
324
325 // Otherwise, eat the semicolon.
326 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
327 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr.get()));
John Wiegley28bbe4b2011-04-28 01:08:34 +0000328}
Douglas Gregor312eadb2011-04-24 05:37:28 +0000329
John Wiegley28bbe4b2011-04-28 01:08:34 +0000330StmtResult Parser::ParseSEHTryBlock(ParsedAttributes & Attrs) {
331 assert(Tok.is(tok::kw___try) && "Expected '__try'");
332 SourceLocation Loc = ConsumeToken();
333 return ParseSEHTryBlockCommon(Loc);
334}
335
336/// ParseSEHTryBlockCommon
337///
338/// seh-try-block:
339/// '__try' compound-statement seh-handler
340///
341/// seh-handler:
342/// seh-except-block
343/// seh-finally-block
344///
345StmtResult Parser::ParseSEHTryBlockCommon(SourceLocation TryLoc) {
346 if(Tok.isNot(tok::l_brace))
347 return StmtError(Diag(Tok,diag::err_expected_lbrace));
348
349 ParsedAttributesWithRange attrs(AttrFactory);
350 StmtResult TryBlock(ParseCompoundStatement(attrs));
351 if(TryBlock.isInvalid())
352 return move(TryBlock);
353
354 StmtResult Handler;
355 if(Tok.is(tok::kw___except)) {
356 SourceLocation Loc = ConsumeToken();
357 Handler = ParseSEHExceptBlock(Loc);
358 } else if (Tok.is(tok::kw___finally)) {
359 SourceLocation Loc = ConsumeToken();
360 Handler = ParseSEHFinallyBlock(Loc);
361 } else {
362 return StmtError(Diag(Tok,diag::err_seh_expected_handler));
363 }
364
365 if(Handler.isInvalid())
366 return move(Handler);
367
368 return Actions.ActOnSEHTryBlock(false /* IsCXXTry */,
369 TryLoc,
370 TryBlock.take(),
371 Handler.take());
372}
373
374/// ParseSEHExceptBlock - Handle __except
375///
376/// seh-except-block:
377/// '__except' '(' seh-filter-expression ')' compound-statement
378///
379StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) {
380 PoisonIdentifierRAIIObject raii(Ident__exception_code, false),
381 raii2(Ident___exception_code, false),
382 raii3(Ident_GetExceptionCode, false);
383
384 if(ExpectAndConsume(tok::l_paren,diag::err_expected_lparen))
385 return StmtError();
386
387 ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope);
388
Francois Pichetd7f02df2011-04-28 03:14:31 +0000389 if (getLang().Borland) {
390 Ident__exception_info->setIsPoisoned(false);
391 Ident___exception_info->setIsPoisoned(false);
392 Ident_GetExceptionInfo->setIsPoisoned(false);
393 }
John Wiegley28bbe4b2011-04-28 01:08:34 +0000394 ExprResult FilterExpr(ParseExpression());
Francois Pichetd7f02df2011-04-28 03:14:31 +0000395
396 if (getLang().Borland) {
397 Ident__exception_info->setIsPoisoned(true);
398 Ident___exception_info->setIsPoisoned(true);
399 Ident_GetExceptionInfo->setIsPoisoned(true);
400 }
John Wiegley28bbe4b2011-04-28 01:08:34 +0000401
402 if(FilterExpr.isInvalid())
403 return StmtError();
404
405 if(ExpectAndConsume(tok::r_paren,diag::err_expected_rparen))
406 return StmtError();
407
408 ParsedAttributesWithRange attrs(AttrFactory);
409 StmtResult Block(ParseCompoundStatement(attrs));
410
411 if(Block.isInvalid())
412 return move(Block);
413
414 return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.take(), Block.take());
415}
416
417/// ParseSEHFinallyBlock - Handle __finally
418///
419/// seh-finally-block:
420/// '__finally' compound-statement
421///
422StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyBlock) {
423 PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
424 raii2(Ident___abnormal_termination, false),
425 raii3(Ident_AbnormalTermination, false);
426
427 ParsedAttributesWithRange attrs(AttrFactory);
428 StmtResult Block(ParseCompoundStatement(attrs));
429 if(Block.isInvalid())
430 return move(Block);
431
432 return Actions.ActOnSEHFinallyBlock(FinallyBlock,Block.take());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000433}
434
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000435/// ParseLabeledStatement - We have an identifier and a ':' after it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000436///
437/// labeled-statement:
438/// identifier ':' statement
439/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000440///
John McCall7f040a92010-12-24 02:08:15 +0000441StmtResult Parser::ParseLabeledStatement(ParsedAttributes &attrs) {
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000442 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
443 "Not an identifier!");
444
445 Token IdentTok = Tok; // Save the whole token.
446 ConsumeToken(); // eat the identifier.
447
448 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000449
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000450 // identifier ':' statement
451 SourceLocation ColonLoc = ConsumeToken();
452
453 // Read label attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +0000454 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000455
John McCall60d7b3a2010-08-24 06:29:42 +0000456 StmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000457
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000458 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000459 if (SubStmt.isInvalid())
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000460 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Chris Lattner337e5502011-02-18 01:27:55 +0000461
462 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
463 IdentTok.getLocation());
464 if (AttributeList *Attrs = attrs.getList())
465 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
466
467 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
468 SubStmt.get());
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000469}
Reid Spencer5f016e22007-07-11 17:01:13 +0000470
471/// ParseCaseStatement
472/// labeled-statement:
473/// 'case' constant-expression ':' statement
474/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
475///
Richard Trieubb9b80c2011-04-21 21:44:26 +0000476StmtResult Parser::ParseCaseStatement(ParsedAttributes &attrs, bool MissingCase,
477 ExprResult Expr) {
Richard Smith46f11102011-04-21 22:48:40 +0000478 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Sean Huntbbd37c62009-11-21 08:43:09 +0000479 // FIXME: Use attributes?
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Chris Lattner24e1e702009-03-04 04:23:07 +0000481 // It is very very common for code to contain many case statements recursively
482 // nested, as in (but usually without indentation):
483 // case 1:
484 // case 2:
485 // case 3:
486 // case 4:
487 // case 5: etc.
488 //
489 // Parsing this naively works, but is both inefficient and can cause us to run
490 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner26140c62009-03-04 18:24:58 +0000491 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner24e1e702009-03-04 04:23:07 +0000492 // but all the grossness is constrained to ParseCaseStatement (and some
493 // wierdness in the actions), so this is just local grossness :).
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Chris Lattner24e1e702009-03-04 04:23:07 +0000495 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
496 // example above.
John McCall60d7b3a2010-08-24 06:29:42 +0000497 StmtResult TopLevelCase(true);
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Chris Lattner24e1e702009-03-04 04:23:07 +0000499 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
500 // gets updated each time a new case is parsed, and whose body is unset so
501 // far. When parsing 'case 4', this is the 'case 3' node.
502 StmtTy *DeepestParsedCaseStmt = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Chris Lattner24e1e702009-03-04 04:23:07 +0000504 // While we have case statements, eat and stack them.
David Majnemer0e1e69c2011-06-13 05:50:12 +0000505 SourceLocation ColonLoc;
Chris Lattner24e1e702009-03-04 04:23:07 +0000506 do {
Richard Trieubb9b80c2011-04-21 21:44:26 +0000507 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
508 ConsumeToken(); // eat the 'case'.
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000510 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000511 Actions.CodeCompleteCase(getCurScope());
Douglas Gregordc845342010-05-25 05:58:43 +0000512 ConsumeCodeCompletionToken();
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000513 }
514
Chris Lattner6fb09c82009-12-10 00:38:54 +0000515 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
516 /// Disable this form of error recovery while we're parsing the case
517 /// expression.
518 ColonProtectionRAIIObject ColonProtection(*this);
519
Richard Trieubb9b80c2011-04-21 21:44:26 +0000520 ExprResult LHS(MissingCase ? Expr : ParseConstantExpression());
521 MissingCase = false;
Chris Lattner24e1e702009-03-04 04:23:07 +0000522 if (LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000523 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000524 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000525 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000526
Chris Lattner24e1e702009-03-04 04:23:07 +0000527 // GNU case range extension.
528 SourceLocation DotDotDotLoc;
John McCall60d7b3a2010-08-24 06:29:42 +0000529 ExprResult RHS;
Chris Lattner24e1e702009-03-04 04:23:07 +0000530 if (Tok.is(tok::ellipsis)) {
531 Diag(Tok, diag::ext_gnu_case_range);
532 DotDotDotLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000533
Chris Lattner24e1e702009-03-04 04:23:07 +0000534 RHS = ParseConstantExpression();
535 if (RHS.isInvalid()) {
536 SkipUntil(tok::colon);
537 return StmtError();
538 }
539 }
Chris Lattner6fb09c82009-12-10 00:38:54 +0000540
541 ColonProtection.restore();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000542
John McCallf6a3ab02011-01-22 09:28:32 +0000543 if (Tok.is(tok::colon)) {
544 ColonLoc = ConsumeToken();
545
546 // Treat "case blah;" as a typo for "case blah:".
547 } else if (Tok.is(tok::semi)) {
548 ColonLoc = ConsumeToken();
549 Diag(ColonLoc, diag::err_expected_colon_after) << "'case'"
550 << FixItHint::CreateReplacement(ColonLoc, ":");
551 } else {
Douglas Gregor662a4822010-12-23 22:56:40 +0000552 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
553 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'"
554 << FixItHint::CreateInsertion(ExpectedLoc, ":");
555 ColonLoc = ExpectedLoc;
Chris Lattner24e1e702009-03-04 04:23:07 +0000556 }
Douglas Gregor662a4822010-12-23 22:56:40 +0000557
John McCall60d7b3a2010-08-24 06:29:42 +0000558 StmtResult Case =
John McCall9ae2f072010-08-23 23:25:46 +0000559 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
560 RHS.get(), ColonLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Chris Lattner24e1e702009-03-04 04:23:07 +0000562 // If we had a sema error parsing this case, then just ignore it and
563 // continue parsing the sub-stmt.
564 if (Case.isInvalid()) {
565 if (TopLevelCase.isInvalid()) // No parsed case stmts.
566 return ParseStatement();
567 // Otherwise, just don't add it as a nested case.
568 } else {
569 // If this is the first case statement we parsed, it becomes TopLevelCase.
570 // Otherwise we link it into the current chain.
John McCallca0408f2010-08-23 06:44:23 +0000571 Stmt *NextDeepest = Case.get();
Chris Lattner24e1e702009-03-04 04:23:07 +0000572 if (TopLevelCase.isInvalid())
573 TopLevelCase = move(Case);
574 else
John McCall9ae2f072010-08-23 23:25:46 +0000575 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner24e1e702009-03-04 04:23:07 +0000576 DeepestParsedCaseStmt = NextDeepest;
577 }
Mike Stump1eb44332009-09-09 15:08:12 +0000578
Chris Lattner24e1e702009-03-04 04:23:07 +0000579 // Handle all case statements.
580 } while (Tok.is(tok::kw_case));
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Chris Lattner24e1e702009-03-04 04:23:07 +0000582 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Chris Lattner24e1e702009-03-04 04:23:07 +0000584 // If we found a non-case statement, start by parsing it.
John McCall60d7b3a2010-08-24 06:29:42 +0000585 StmtResult SubStmt;
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Chris Lattner24e1e702009-03-04 04:23:07 +0000587 if (Tok.isNot(tok::r_brace)) {
588 SubStmt = ParseStatement();
589 } else {
590 // Nicely diagnose the common error "switch (X) { case 4: }", which is
591 // not valid.
David Majnemer63f04ab2011-06-14 15:24:38 +0000592 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
593 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement);
Chris Lattner24e1e702009-03-04 04:23:07 +0000594 SubStmt = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000595 }
Mike Stump1eb44332009-09-09 15:08:12 +0000596
Chris Lattner24e1e702009-03-04 04:23:07 +0000597 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000598 if (SubStmt.isInvalid())
Chris Lattner24e1e702009-03-04 04:23:07 +0000599 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000600
Chris Lattner24e1e702009-03-04 04:23:07 +0000601 // Install the body into the most deeply-nested case.
John McCall9ae2f072010-08-23 23:25:46 +0000602 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000603
Chris Lattner24e1e702009-03-04 04:23:07 +0000604 // Return the top level parsed statement tree.
Chris Lattner26140c62009-03-04 18:24:58 +0000605 return move(TopLevelCase);
Reid Spencer5f016e22007-07-11 17:01:13 +0000606}
607
608/// ParseDefaultStatement
609/// labeled-statement:
610/// 'default' ':' statement
611/// Note that this does not parse the 'statement' at the end.
612///
John McCall7f040a92010-12-24 02:08:15 +0000613StmtResult Parser::ParseDefaultStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000614 //FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000615
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000616 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000617 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
618
Douglas Gregor662a4822010-12-23 22:56:40 +0000619 SourceLocation ColonLoc;
John McCallf6a3ab02011-01-22 09:28:32 +0000620 if (Tok.is(tok::colon)) {
621 ColonLoc = ConsumeToken();
622
623 // Treat "default;" as a typo for "default:".
624 } else if (Tok.is(tok::semi)) {
625 ColonLoc = ConsumeToken();
626 Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
627 << FixItHint::CreateReplacement(ColonLoc, ":");
628 } else {
Douglas Gregor662a4822010-12-23 22:56:40 +0000629 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
630 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
631 << FixItHint::CreateInsertion(ExpectedLoc, ":");
632 ColonLoc = ExpectedLoc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000633 }
Douglas Gregor662a4822010-12-23 22:56:40 +0000634
Reid Spencer5f016e22007-07-11 17:01:13 +0000635 // Diagnose the common error "switch (X) {... default: }", which is not valid.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000636 if (Tok.is(tok::r_brace)) {
David Majnemer63f04ab2011-06-14 15:24:38 +0000637 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
638 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000639 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000640 }
641
John McCall60d7b3a2010-08-24 06:29:42 +0000642 StmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000643 if (SubStmt.isInvalid())
Sebastian Redl61364dd2008-12-11 19:30:53 +0000644 return StmtError();
645
Sebastian Redl117054a2008-12-28 16:13:43 +0000646 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000647 SubStmt.get(), getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +0000648}
649
Douglas Gregorbca01b42011-07-06 22:04:06 +0000650StmtResult Parser::ParseCompoundStatement(ParsedAttributes &Attr,
651 bool isStmtExpr) {
652 return ParseCompoundStatement(Attr, isStmtExpr, Scope::DeclScope);
653}
Reid Spencer5f016e22007-07-11 17:01:13 +0000654
655/// ParseCompoundStatement - Parse a "{}" block.
656///
657/// compound-statement: [C99 6.8.2]
658/// { block-item-list[opt] }
659/// [GNU] { label-declarations block-item-list } [TODO]
660///
661/// block-item-list:
662/// block-item
663/// block-item-list block-item
664///
665/// block-item:
666/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000667/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000668/// statement
669/// [OMP] openmp-directive [TODO]
670///
671/// [GNU] label-declarations:
672/// [GNU] label-declaration
673/// [GNU] label-declarations label-declaration
674///
675/// [GNU] label-declaration:
676/// [GNU] '__label__' identifier-list ';'
677///
678/// [OMP] openmp-directive: [TODO]
679/// [OMP] barrier-directive
680/// [OMP] flush-directive
681///
John McCall7f040a92010-12-24 02:08:15 +0000682StmtResult Parser::ParseCompoundStatement(ParsedAttributes &attrs,
Douglas Gregorbca01b42011-07-06 22:04:06 +0000683 bool isStmtExpr,
684 unsigned ScopeFlags) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000685 //FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000686
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000687 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000688
Chris Lattner31e05722007-08-26 06:24:45 +0000689 // Enter a scope to hold everything within the compound stmt. Compound
690 // statements can always hold declarations.
Douglas Gregorbca01b42011-07-06 22:04:06 +0000691 ParseScope CompoundScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000692
693 // Parse the statements in the body.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000694 return ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000695}
696
697
698/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000699/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000700/// consume the '}' at the end of the block. It does not manipulate the scope
701/// stack.
John McCall60d7b3a2010-08-24 06:29:42 +0000702StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump1eb44332009-09-09 15:08:12 +0000703 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerae50fa02009-03-05 00:00:31 +0000704 Tok.getLocation(),
705 "in compound statement ('{}')");
Douglas Gregor0fbda682010-09-15 14:51:05 +0000706 InMessageExpressionRAIIObject InMessage(*this, false);
707
Reid Spencer5f016e22007-07-11 17:01:13 +0000708 SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
709
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000710 StmtVector Stmts(Actions);
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000711
Chris Lattner4ae493c2011-02-18 02:08:43 +0000712 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
713 // only allowed at the start of a compound stmt regardless of the language.
714 while (Tok.is(tok::kw___label__)) {
715 SourceLocation LabelLoc = ConsumeToken();
716 Diag(LabelLoc, diag::ext_gnu_local_label);
717
718 llvm::SmallVector<Decl *, 8> DeclsInGroup;
719 while (1) {
720 if (Tok.isNot(tok::identifier)) {
721 Diag(Tok, diag::err_expected_ident);
722 break;
723 }
724
725 IdentifierInfo *II = Tok.getIdentifierInfo();
726 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara67843042011-03-05 18:21:20 +0000727 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
Chris Lattner4ae493c2011-02-18 02:08:43 +0000728
729 if (!Tok.is(tok::comma))
730 break;
731 ConsumeToken();
732 }
733
John McCall0b7e6782011-03-24 11:26:52 +0000734 DeclSpec DS(AttrFactory);
Chris Lattner4ae493c2011-02-18 02:08:43 +0000735 DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
736 DeclsInGroup.data(), DeclsInGroup.size());
737 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
738
739 ExpectAndConsume(tok::semi, diag::err_expected_semi_declaration);
740 if (R.isUsable())
741 Stmts.push_back(R.release());
742 }
743
744 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000745 if (Tok.is(tok::annot_pragma_unused)) {
746 HandlePragmaUnused();
747 continue;
748 }
749
Francois Pichet1e862692011-05-06 20:48:22 +0000750 if (getLang().Microsoft && (Tok.is(tok::kw___if_exists) ||
751 Tok.is(tok::kw___if_not_exists))) {
752 ParseMicrosoftIfExistsStatement(Stmts);
753 continue;
754 }
755
John McCall60d7b3a2010-08-24 06:29:42 +0000756 StmtResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000757 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000758 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattner45a566c2007-08-27 01:01:57 +0000759 } else {
760 // __extension__ can start declarations and it can also be a unary
761 // operator for expressions. Consume multiple __extension__ markers here
762 // until we can determine which is which.
Eli Friedmanadf077f2009-01-27 08:43:38 +0000763 // FIXME: This loses extension expressions in the AST!
Chris Lattner45a566c2007-08-27 01:01:57 +0000764 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000765 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000766 ConsumeToken();
Chris Lattner39146d62008-10-20 06:51:33 +0000767
John McCall0b7e6782011-03-24 11:26:52 +0000768 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +0000769 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +0000770
Chris Lattner45a566c2007-08-27 01:01:57 +0000771 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000772 if (isDeclarationStatement()) {
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000773 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000774 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000775 ExtensionRAIIObject O(Diags);
776
Chris Lattner97144fc2009-04-02 04:16:50 +0000777 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000778 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
779 Declarator::BlockContext, DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000780 attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000781 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattner45a566c2007-08-27 01:01:57 +0000782 } else {
Eli Friedmanadf077f2009-01-27 08:43:38 +0000783 // Otherwise this was a unary __extension__ marker.
John McCall60d7b3a2010-08-24 06:29:42 +0000784 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattner043a0b52008-03-13 06:32:11 +0000785
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000786 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000787 SkipUntil(tok::semi);
788 continue;
789 }
Sebastian Redlf512e822009-01-18 18:03:53 +0000790
Sean Huntbbd37c62009-11-21 08:43:09 +0000791 // FIXME: Use attributes?
Chris Lattner39146d62008-10-20 06:51:33 +0000792 // Eat the semicolon at the end of stmt and convert the expr into a
793 // statement.
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000794 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +0000795 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattner45a566c2007-08-27 01:01:57 +0000796 }
797 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000798
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000799 if (R.isUsable())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000800 Stmts.push_back(R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000801 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000802
Reid Spencer5f016e22007-07-11 17:01:13 +0000803 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000804 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000805 Diag(Tok, diag::err_expected_rbrace);
Chris Lattnerf65086b2010-09-01 15:49:26 +0000806 Diag(LBraceLoc, diag::note_matching) << "{";
Sebastian Redl61364dd2008-12-11 19:30:53 +0000807 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000808 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000809
Reid Spencer5f016e22007-07-11 17:01:13 +0000810 SourceLocation RBraceLoc = ConsumeBrace();
Sebastian Redlf512e822009-01-18 18:03:53 +0000811 return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_arg(Stmts),
Sebastian Redla60528c2008-12-21 12:04:03 +0000812 isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000813}
814
Chris Lattner15ff1112008-12-12 06:31:07 +0000815/// ParseParenExprOrCondition:
816/// [C ] '(' expression ')'
Chris Lattnerff871fb2008-12-12 06:35:28 +0000817/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattner15ff1112008-12-12 06:31:07 +0000818///
819/// This function parses and performs error recovery on the specified condition
820/// or expression (depending on whether we're in C++ or C mode). This function
821/// goes out of its way to recover well. It returns true if there was a parser
822/// error (the right paren couldn't be found), which indicates that the caller
823/// should try to recover harder. It returns false if the condition is
824/// successfully parsed. Note that a successful parse can still have semantic
825/// errors in the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000826bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCalld226f652010-08-21 09:40:31 +0000827 Decl *&DeclResult,
Douglas Gregor586596f2010-05-06 17:25:47 +0000828 SourceLocation Loc,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000829 bool ConvertToBoolean) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000830 SourceLocation LParenLoc = ConsumeParen();
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000831 if (getLang().CPlusPlus)
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000832 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000833 else {
834 ExprResult = ParseExpression();
John McCalld226f652010-08-21 09:40:31 +0000835 DeclResult = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +0000836
837 // If required, convert to a boolean value.
838 if (!ExprResult.isInvalid() && ConvertToBoolean)
839 ExprResult
John McCall9ae2f072010-08-23 23:25:46 +0000840 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000841 }
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Chris Lattner15ff1112008-12-12 06:31:07 +0000843 // If the parser was confused by the condition and we don't have a ')', try to
844 // recover by skipping ahead to a semi and bailing out. If condexp is
845 // semantically invalid but we have well formed code, keep going.
John McCalld226f652010-08-21 09:40:31 +0000846 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000847 SkipUntil(tok::semi);
848 // Skipping may have stopped if it found the containing ')'. If so, we can
849 // continue parsing the if statement.
850 if (Tok.isNot(tok::r_paren))
851 return true;
852 }
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Chris Lattner15ff1112008-12-12 06:31:07 +0000854 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000855 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner15ff1112008-12-12 06:31:07 +0000856 return false;
857}
858
859
Reid Spencer5f016e22007-07-11 17:01:13 +0000860/// ParseIfStatement
861/// if-statement: [C99 6.8.4.1]
862/// 'if' '(' expression ')' statement
863/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000864/// [C++] 'if' '(' condition ')' statement
865/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000866///
John McCall7f040a92010-12-24 02:08:15 +0000867StmtResult Parser::ParseIfStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000868 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000869
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000870 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000871 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
872
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000873 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000874 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000875 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000876 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000877 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000878
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000879 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
880
Chris Lattner22153252007-08-26 23:08:06 +0000881 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
882 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000883 //
884 // C++ 6.4p3:
885 // A name introduced by a declaration in a condition is in scope from its
886 // point of declaration until the end of the substatements controlled by the
887 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000888 // C++ 3.3.2p4:
889 // Names declared in the for-init-statement, and in the condition of if,
890 // while, for, and switch statements are local to the if, while, for, or
891 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000892 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000893 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +0000894
Reid Spencer5f016e22007-07-11 17:01:13 +0000895 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000896 ExprResult CondExp;
John McCalld226f652010-08-21 09:40:31 +0000897 Decl *CondVar = 0;
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000898 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +0000899 return StmtError();
Chris Lattner18914bc2008-12-12 06:19:11 +0000900
John McCall9ae2f072010-08-23 23:25:46 +0000901 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get()));
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Chris Lattner0ecea032007-08-22 05:28:50 +0000903 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000904 // there is no compound stmt. C90 does not have this clause. We only do this
905 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000906 //
907 // C++ 6.4p1:
908 // The substatement in a selection-statement (each substatement, in the else
909 // form of the if statement) implicitly defines a local scope.
910 //
911 // For C++ we create a scope for the condition and a new scope for
912 // substatements because:
913 // -When the 'then' scope exits, we want the condition declaration to still be
914 // active for the 'else' scope too.
915 // -Sema will detect name clashes by considering declarations of a
916 // 'ControlScope' as part of its direct subscope.
917 // -If we wanted the condition and substatement to be in the same scope, we
918 // would have to notify ParseStatement not to create a new scope. It's
919 // simpler to let it create a new scope.
920 //
Mike Stump1eb44332009-09-09 15:08:12 +0000921 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000922 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000923
Chris Lattnerb96728d2007-10-29 05:08:52 +0000924 // Read the 'then' stmt.
925 SourceLocation ThenStmtLoc = Tok.getLocation();
John McCall60d7b3a2010-08-24 06:29:42 +0000926 StmtResult ThenStmt(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +0000927
Chris Lattnera36ce712007-08-22 05:16:28 +0000928 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000929 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000930
Reid Spencer5f016e22007-07-11 17:01:13 +0000931 // If it has an else, parse it.
932 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000933 SourceLocation ElseStmtLoc;
John McCall60d7b3a2010-08-24 06:29:42 +0000934 StmtResult ElseStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000935
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000936 if (Tok.is(tok::kw_else)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000937 ElseLoc = ConsumeToken();
Chris Lattner966c78b2010-04-12 06:12:50 +0000938 ElseStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000939
Chris Lattner0ecea032007-08-22 05:28:50 +0000940 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000941 // there is no compound stmt. C90 does not have this clause. We only do
942 // this if the body isn't a compound statement to avoid push/pop in common
943 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000944 //
945 // C++ 6.4p1:
946 // The substatement in a selection-statement (each substatement, in the else
947 // form of the if statement) implicitly defines a local scope.
948 //
Sebastian Redl61364dd2008-12-11 19:30:53 +0000949 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000950 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000951
Reid Spencer5f016e22007-07-11 17:01:13 +0000952 ElseStmt = ParseStatement();
Chris Lattner966c78b2010-04-12 06:12:50 +0000953
Chris Lattnera36ce712007-08-22 05:16:28 +0000954 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000955 InnerScope.Exit();
Reid Spencer5f016e22007-07-11 17:01:13 +0000956 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000957
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000958 IfScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Chris Lattner18914bc2008-12-12 06:19:11 +0000960 // If the condition was invalid, discard the if statement. We could recover
961 // better by replacing it with a valid expr, but don't do that yet.
John McCalld226f652010-08-21 09:40:31 +0000962 if (CondExp.isInvalid() && !CondVar)
Chris Lattner18914bc2008-12-12 06:19:11 +0000963 return StmtError();
Chris Lattner22153252007-08-26 23:08:06 +0000964
Chris Lattnerb96728d2007-10-29 05:08:52 +0000965 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump1eb44332009-09-09 15:08:12 +0000966 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattnerb96728d2007-10-29 05:08:52 +0000967 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000968 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
969 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
970 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000971 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000972 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +0000973 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000974
Chris Lattnerb96728d2007-10-29 05:08:52 +0000975 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000976 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000977 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000978 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000979 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000980
John McCall9ae2f072010-08-23 23:25:46 +0000981 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000982 ElseLoc, ElseStmt.get());
Reid Spencer5f016e22007-07-11 17:01:13 +0000983}
984
985/// ParseSwitchStatement
986/// switch-statement:
987/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000988/// [C++] 'switch' '(' condition ')' statement
John McCall7f040a92010-12-24 02:08:15 +0000989StmtResult Parser::ParseSwitchStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000990 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +0000991
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000992 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000993 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
994
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000995 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000996 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +0000997 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +0000998 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000999 }
Chris Lattner22153252007-08-26 23:08:06 +00001000
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001001 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
1002
Chris Lattner22153252007-08-26 23:08:06 +00001003 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1004 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001005 //
1006 // C++ 6.4p3:
1007 // A name introduced by a declaration in a condition is in scope from its
1008 // point of declaration until the end of the substatements controlled by the
1009 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001010 // C++ 3.3.2p4:
1011 // Names declared in the for-init-statement, and in the condition of if,
1012 // while, for, and switch statements are local to the if, while, for, or
1013 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001014 //
Richard Trieubb9b80c2011-04-21 21:44:26 +00001015 unsigned ScopeFlags = Scope::BreakScope | Scope::SwitchScope;
Chris Lattner15ff1112008-12-12 06:31:07 +00001016 if (C99orCXX)
1017 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001018 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001019
1020 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +00001021 ExprResult Cond;
John McCalld226f652010-08-21 09:40:31 +00001022 Decl *CondVar = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +00001023 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redl9a920342008-12-11 19:48:14 +00001024 return StmtError();
Eli Friedman2342ef72008-12-17 22:19:57 +00001025
John McCall60d7b3a2010-08-24 06:29:42 +00001026 StmtResult Switch
John McCall9ae2f072010-08-23 23:25:46 +00001027 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001028
Douglas Gregor586596f2010-05-06 17:25:47 +00001029 if (Switch.isInvalid()) {
1030 // Skip the switch body.
1031 // FIXME: This is not optimal recovery, but parsing the body is more
1032 // dangerous due to the presence of case and default statements, which
1033 // will have no place to connect back with the switch.
Douglas Gregor4186ff42010-05-20 23:20:59 +00001034 if (Tok.is(tok::l_brace)) {
1035 ConsumeBrace();
1036 SkipUntil(tok::r_brace, false, false);
1037 } else
Douglas Gregor586596f2010-05-06 17:25:47 +00001038 SkipUntil(tok::semi);
1039 return move(Switch);
1040 }
1041
Chris Lattner0ecea032007-08-22 05:28:50 +00001042 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001043 // there is no compound stmt. C90 does not have this clause. We only do this
1044 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001045 //
1046 // C++ 6.4p1:
1047 // The substatement in a selection-statement (each substatement, in the else
1048 // form of the if statement) implicitly defines a local scope.
1049 //
1050 // See comments in ParseIfStatement for why we create a scope for the
1051 // condition and a new scope for substatement in C++.
1052 //
Mike Stump1eb44332009-09-09 15:08:12 +00001053 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001054 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +00001055
Reid Spencer5f016e22007-07-11 17:01:13 +00001056 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +00001057 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +00001058
Chris Lattner7e52de42010-01-24 01:50:29 +00001059 // Pop the scopes.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001060 InnerScope.Exit();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001061 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +00001062
Chris Lattner7e52de42010-01-24 01:50:29 +00001063 if (Body.isInvalid())
1064 // FIXME: Remove the case statement list from the Switch statement.
1065 Body = Actions.ActOnNullStmt(Tok.getLocation());
1066
John McCall9ae2f072010-08-23 23:25:46 +00001067 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Reid Spencer5f016e22007-07-11 17:01:13 +00001068}
1069
1070/// ParseWhileStatement
1071/// while-statement: [C99 6.8.5.1]
1072/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001073/// [C++] 'while' '(' condition ')' statement
John McCall7f040a92010-12-24 02:08:15 +00001074StmtResult Parser::ParseWhileStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001075 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001076
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001077 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001078 SourceLocation WhileLoc = Tok.getLocation();
1079 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001080
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001081 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001082 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +00001083 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +00001084 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001085 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001086
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001087 bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
1088
Chris Lattner22153252007-08-26 23:08:06 +00001089 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1090 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001091 //
1092 // C++ 6.4p3:
1093 // A name introduced by a declaration in a condition is in scope from its
1094 // point of declaration until the end of the substatements controlled by the
1095 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001096 // C++ 3.3.2p4:
1097 // Names declared in the for-init-statement, and in the condition of if,
1098 // while, for, and switch statements are local to the if, while, for, or
1099 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001100 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001101 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001102 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001103 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1104 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +00001105 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001106 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1107 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001108
1109 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +00001110 ExprResult Cond;
John McCalld226f652010-08-21 09:40:31 +00001111 Decl *CondVar = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +00001112 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +00001113 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001114
John McCall9ae2f072010-08-23 23:25:46 +00001115 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get()));
Mike Stump1eb44332009-09-09 15:08:12 +00001116
Chris Lattner0ecea032007-08-22 05:28:50 +00001117 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001118 // there is no compound stmt. C90 does not have this clause. We only do this
1119 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001120 //
1121 // C++ 6.5p2:
1122 // The substatement in an iteration-statement implicitly defines a local scope
1123 // which is entered and exited each time through the loop.
1124 //
1125 // See comments in ParseIfStatement for why we create a scope for the
1126 // condition and a new scope for substatement in C++.
1127 //
Mike Stump1eb44332009-09-09 15:08:12 +00001128 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001129 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +00001130
Reid Spencer5f016e22007-07-11 17:01:13 +00001131 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +00001132 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +00001133
Chris Lattner0ecea032007-08-22 05:28:50 +00001134 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001135 InnerScope.Exit();
1136 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +00001137
John McCalld226f652010-08-21 09:40:31 +00001138 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001139 return StmtError();
1140
John McCall9ae2f072010-08-23 23:25:46 +00001141 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Reid Spencer5f016e22007-07-11 17:01:13 +00001142}
1143
1144/// ParseDoStatement
1145/// do-statement: [C99 6.8.5.2]
1146/// 'do' statement 'while' '(' expression ')' ';'
1147/// Note: this lets the caller parse the end ';'.
John McCall7f040a92010-12-24 02:08:15 +00001148StmtResult Parser::ParseDoStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001149 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001150
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001151 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001152 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001153
Chris Lattner22153252007-08-26 23:08:06 +00001154 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1155 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001156 unsigned ScopeFlags;
Chris Lattner22153252007-08-26 23:08:06 +00001157 if (getLang().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001158 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +00001159 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001160 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +00001161
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001162 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001163
Chris Lattner0ecea032007-08-22 05:28:50 +00001164 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001165 // there is no compound stmt. C90 does not have this clause. We only do this
1166 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +00001167 //
1168 // C++ 6.5p2:
1169 // The substatement in an iteration-statement implicitly defines a local scope
1170 // which is entered and exited each time through the loop.
1171 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001172 ParseScope InnerScope(this, Scope::DeclScope,
Mike Stump1eb44332009-09-09 15:08:12 +00001173 (getLang().C99 || getLang().CPlusPlus) &&
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001174 Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +00001175
Reid Spencer5f016e22007-07-11 17:01:13 +00001176 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +00001177 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +00001178
Chris Lattner0ecea032007-08-22 05:28:50 +00001179 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001180 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001181
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001182 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001183 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +00001184 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +00001185 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +00001186 SkipUntil(tok::semi, false, true);
1187 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001188 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001189 }
1190 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001191
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001192 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001193 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +00001194 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001195 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001196 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001197
Chris Lattnerff871fb2008-12-12 06:35:28 +00001198 // Parse the parenthesized condition.
Douglas Gregor04895d32009-11-24 21:34:32 +00001199 SourceLocation LPLoc = ConsumeParen();
John McCall60d7b3a2010-08-24 06:29:42 +00001200 ExprResult Cond = ParseExpression();
Douglas Gregor04895d32009-11-24 21:34:32 +00001201 SourceLocation RPLoc = MatchRHSPunctuation(tok::r_paren, LPLoc);
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001202 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001203
Sebastian Redl9a920342008-12-11 19:48:14 +00001204 if (Cond.isInvalid() || Body.isInvalid())
1205 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001206
John McCall9ae2f072010-08-23 23:25:46 +00001207 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, LPLoc,
1208 Cond.get(), RPLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001209}
1210
1211/// ParseForStatement
1212/// for-statement: [C99 6.8.5.3]
1213/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1214/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001215/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1216/// [C++] statement
Richard Smithad762fc2011-04-14 22:09:26 +00001217/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001218/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1219/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001220///
1221/// [C++] for-init-statement:
1222/// [C++] expression-statement
1223/// [C++] simple-declaration
1224///
Richard Smithad762fc2011-04-14 22:09:26 +00001225/// [C++0x] for-range-declaration:
1226/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1227/// [C++0x] for-range-initializer:
1228/// [C++0x] expression
1229/// [C++0x] braced-init-list [TODO]
John McCall7f040a92010-12-24 02:08:15 +00001230StmtResult Parser::ParseForStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001231 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001232
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001233 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001234 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001235
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001236 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001237 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +00001238 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +00001239 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001240 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001241
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001242 bool C99orCXXorObjC = getLang().C99 || getLang().CPlusPlus || getLang().ObjC1;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001243
Chris Lattner22153252007-08-26 23:08:06 +00001244 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1245 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001246 //
1247 // C++ 6.4p3:
1248 // A name introduced by a declaration in a condition is in scope from its
1249 // point of declaration until the end of the substatements controlled by the
1250 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001251 // C++ 3.3.2p4:
1252 // Names declared in the for-init-statement, and in the condition of if,
1253 // while, for, and switch statements are local to the if, while, for, or
1254 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001255 // C++ 6.5.3p1:
1256 // Names declared in the for-init-statement are in the same declarative-region
1257 // as those declared in the condition.
1258 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001259 unsigned ScopeFlags;
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001260 if (C99orCXXorObjC)
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001261 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1262 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +00001263 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001264 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1265
1266 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001267
1268 SourceLocation LParenLoc = ConsumeParen();
John McCall60d7b3a2010-08-24 06:29:42 +00001269 ExprResult Value;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001270
Richard Smithad762fc2011-04-14 22:09:26 +00001271 bool ForEach = false, ForRange = false;
John McCall60d7b3a2010-08-24 06:29:42 +00001272 StmtResult FirstPart;
Douglas Gregoreecf38f2010-05-06 21:39:56 +00001273 bool SecondPartIsInvalid = false;
Douglas Gregor586596f2010-05-06 17:25:47 +00001274 FullExprArg SecondPart(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001275 ExprResult Collection;
Richard Smithad762fc2011-04-14 22:09:26 +00001276 ForRangeInit ForRangeInit;
Douglas Gregor586596f2010-05-06 17:25:47 +00001277 FullExprArg ThirdPart(Actions);
John McCalld226f652010-08-21 09:40:31 +00001278 Decl *SecondVar = 0;
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001279
Douglas Gregor791215b2009-09-21 20:51:25 +00001280 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001281 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001282 C99orCXXorObjC? Sema::PCC_ForInit
1283 : Sema::PCC_Expression);
Douglas Gregordc845342010-05-25 05:58:43 +00001284 ConsumeCodeCompletionToken();
Douglas Gregor791215b2009-09-21 20:51:25 +00001285 }
1286
Reid Spencer5f016e22007-07-11 17:01:13 +00001287 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001288 if (Tok.is(tok::semi)) { // for (;
Reid Spencer5f016e22007-07-11 17:01:13 +00001289 // no first part, eat the ';'.
1290 ConsumeToken();
Argyrios Kyrtzidisbbc70c02008-10-05 15:50:46 +00001291 } else if (isSimpleDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +00001292 // Parse declaration, which eats the ';'.
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001293 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +00001294 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +00001295
John McCall0b7e6782011-03-24 11:26:52 +00001296 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00001297 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00001298
Richard Smithad762fc2011-04-14 22:09:26 +00001299 // In C++0x, "for (T NS:a" might not be a typo for ::
1300 bool MightBeForRangeStmt = getLang().CPlusPlus;
1301 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1302
Chris Lattner97144fc2009-04-02 04:16:50 +00001303 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001304 StmtVector Stmts(Actions);
1305 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
Richard Smithad762fc2011-04-14 22:09:26 +00001306 DeclEnd, attrs, false,
1307 MightBeForRangeStmt ?
1308 &ForRangeInit : 0);
Chris Lattnercd147752009-03-29 17:27:48 +00001309 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001310
Richard Smithad762fc2011-04-14 22:09:26 +00001311 if (ForRangeInit.ParsedForRangeDecl()) {
1312 ForRange = true;
1313 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattnercd147752009-03-29 17:27:48 +00001314 ConsumeToken();
1315 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniana7cf23a2009-11-19 22:12:37 +00001316 Actions.ActOnForEachDeclStmt(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001317 // ObjC: for (id x in expr)
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001318 ConsumeToken(); // consume 'in'
Douglas Gregorfb629412010-08-23 21:17:50 +00001319
1320 if (Tok.is(tok::code_completion)) {
1321 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
1322 ConsumeCodeCompletionToken();
1323 }
Douglas Gregor586596f2010-05-06 17:25:47 +00001324 Collection = ParseExpression();
Chris Lattnercd147752009-03-29 17:27:48 +00001325 } else {
1326 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001327 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001328 } else {
1329 Value = ParseExpression();
1330
John McCallf6a16482010-12-04 03:47:34 +00001331 ForEach = isTokIdentifier_in();
1332
Reid Spencer5f016e22007-07-11 17:01:13 +00001333 // Turn the expression into a stmt.
John McCallf6a16482010-12-04 03:47:34 +00001334 if (!Value.isInvalid()) {
1335 if (ForEach)
1336 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1337 else
1338 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
1339 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001340
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001341 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001342 ConsumeToken();
John McCallf6a16482010-12-04 03:47:34 +00001343 } else if (ForEach) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001344 ConsumeToken(); // consume 'in'
Douglas Gregorfb629412010-08-23 21:17:50 +00001345
1346 if (Tok.is(tok::code_completion)) {
1347 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
1348 ConsumeCodeCompletionToken();
1349 }
Douglas Gregor586596f2010-05-06 17:25:47 +00001350 Collection = ParseExpression();
Chris Lattner682bf922009-03-29 16:50:03 +00001351 } else {
Douglas Gregorb72c7782011-02-17 03:38:46 +00001352 if (!Value.isInvalid()) {
1353 Diag(Tok, diag::err_expected_semi_for);
1354 } else {
1355 // Skip until semicolon or rparen, don't consume it.
1356 SkipUntil(tok::r_paren, true, true);
1357 if (Tok.is(tok::semi))
1358 ConsumeToken();
1359 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001360 }
1361 }
Richard Smithad762fc2011-04-14 22:09:26 +00001362 if (!ForEach && !ForRange) {
John McCall9ae2f072010-08-23 23:25:46 +00001363 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001364 // Parse the second part of the for specifier.
1365 if (Tok.is(tok::semi)) { // for (...;;
1366 // no second part.
Douglas Gregorb72c7782011-02-17 03:38:46 +00001367 } else if (Tok.is(tok::r_paren)) {
1368 // missing both semicolons.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001369 } else {
John McCall60d7b3a2010-08-24 06:29:42 +00001370 ExprResult Second;
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001371 if (getLang().CPlusPlus)
Douglas Gregor586596f2010-05-06 17:25:47 +00001372 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1373 else {
1374 Second = ParseExpression();
1375 if (!Second.isInvalid())
Douglas Gregor23c94db2010-07-02 17:43:08 +00001376 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001377 Second.get());
Douglas Gregor586596f2010-05-06 17:25:47 +00001378 }
Douglas Gregoreecf38f2010-05-06 21:39:56 +00001379 SecondPartIsInvalid = Second.isInvalid();
John McCall9ae2f072010-08-23 23:25:46 +00001380 SecondPart = Actions.MakeFullExpr(Second.get());
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001381 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001382
Douglas Gregorb72c7782011-02-17 03:38:46 +00001383 if (Tok.isNot(tok::semi)) {
1384 if (!SecondPartIsInvalid || SecondVar)
1385 Diag(Tok, diag::err_expected_semi_for);
1386 else
1387 // Skip until semicolon or rparen, don't consume it.
1388 SkipUntil(tok::r_paren, true, true);
1389 }
1390
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001391 if (Tok.is(tok::semi)) {
1392 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001393 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001394
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001395 // Parse the third part of the for specifier.
Douglas Gregor586596f2010-05-06 17:25:47 +00001396 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCall60d7b3a2010-08-24 06:29:42 +00001397 ExprResult Third = ParseExpression();
John McCall9ae2f072010-08-23 23:25:46 +00001398 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregor586596f2010-05-06 17:25:47 +00001399 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001400 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001401 // Match the ')'.
1402 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001403
Richard Smithad762fc2011-04-14 22:09:26 +00001404 // We need to perform most of the semantic analysis for a C++0x for-range
1405 // statememt before parsing the body, in order to be able to deduce the type
1406 // of an auto-typed loop variable.
1407 StmtResult ForRangeStmt;
1408 if (ForRange)
1409 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, LParenLoc,
1410 FirstPart.take(),
1411 ForRangeInit.ColonLoc,
1412 ForRangeInit.RangeExpr.get(),
1413 RParenLoc);
1414
Chris Lattner0ecea032007-08-22 05:28:50 +00001415 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001416 // there is no compound stmt. C90 does not have this clause. We only do this
1417 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001418 //
1419 // C++ 6.5p2:
1420 // The substatement in an iteration-statement implicitly defines a local scope
1421 // which is entered and exited each time through the loop.
1422 //
1423 // See comments in ParseIfStatement for why we create a scope for
1424 // for-init-statement/condition and a new scope for substatement in C++.
1425 //
Mike Stump1eb44332009-09-09 15:08:12 +00001426 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001427 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001428
Reid Spencer5f016e22007-07-11 17:01:13 +00001429 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +00001430 StmtResult Body(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001431
Chris Lattner0ecea032007-08-22 05:28:50 +00001432 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001433 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001434
Reid Spencer5f016e22007-07-11 17:01:13 +00001435 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001436 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001437
1438 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001439 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001440
Richard Smithad762fc2011-04-14 22:09:26 +00001441 if (ForEach)
1442 // FIXME: It isn't clear how to communicate the late destruction of
1443 // C++ temporaries used to create the collection.
1444 return Actions.ActOnObjCForCollectionStmt(ForLoc, LParenLoc,
1445 FirstPart.take(),
1446 Collection.take(), RParenLoc,
1447 Body.take());
Mike Stump1eb44332009-09-09 15:08:12 +00001448
Richard Smithad762fc2011-04-14 22:09:26 +00001449 if (ForRange)
1450 return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take());
1451
1452 return Actions.ActOnForStmt(ForLoc, LParenLoc, FirstPart.take(), SecondPart,
1453 SecondVar, ThirdPart, RParenLoc, Body.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001454}
1455
1456/// ParseGotoStatement
1457/// jump-statement:
1458/// 'goto' identifier ';'
1459/// [GNU] 'goto' '*' expression ';'
1460///
1461/// Note: this lets the caller parse the end ';'.
1462///
John McCall7f040a92010-12-24 02:08:15 +00001463StmtResult Parser::ParseGotoStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001464 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001465
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001466 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001467 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001468
John McCall60d7b3a2010-08-24 06:29:42 +00001469 StmtResult Res;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001470 if (Tok.is(tok::identifier)) {
Chris Lattner337e5502011-02-18 01:27:55 +00001471 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1472 Tok.getLocation());
1473 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Reid Spencer5f016e22007-07-11 17:01:13 +00001474 ConsumeToken();
Eli Friedmanf01fdff2009-04-28 00:51:18 +00001475 } else if (Tok.is(tok::star)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001476 // GNU indirect goto extension.
1477 Diag(Tok, diag::ext_gnu_indirect_goto);
1478 SourceLocation StarLoc = ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001479 ExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001480 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001481 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001482 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001483 }
John McCall9ae2f072010-08-23 23:25:46 +00001484 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattner95cfb852007-07-22 04:13:33 +00001485 } else {
1486 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +00001487 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001488 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001489
Sebastian Redl9a920342008-12-11 19:48:14 +00001490 return move(Res);
Reid Spencer5f016e22007-07-11 17:01:13 +00001491}
1492
1493/// ParseContinueStatement
1494/// jump-statement:
1495/// 'continue' ';'
1496///
1497/// Note: this lets the caller parse the end ';'.
1498///
John McCall7f040a92010-12-24 02:08:15 +00001499StmtResult Parser::ParseContinueStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001500 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001501
Reid Spencer5f016e22007-07-11 17:01:13 +00001502 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001503 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001504}
1505
1506/// ParseBreakStatement
1507/// jump-statement:
1508/// 'break' ';'
1509///
1510/// Note: this lets the caller parse the end ';'.
1511///
John McCall7f040a92010-12-24 02:08:15 +00001512StmtResult Parser::ParseBreakStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001513 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001514
Reid Spencer5f016e22007-07-11 17:01:13 +00001515 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001516 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001517}
1518
1519/// ParseReturnStatement
1520/// jump-statement:
1521/// 'return' expression[opt] ';'
John McCall7f040a92010-12-24 02:08:15 +00001522StmtResult Parser::ParseReturnStatement(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001523 // FIXME: Use attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001524
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001525 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001526 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001527
John McCall60d7b3a2010-08-24 06:29:42 +00001528 ExprResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001529 if (Tok.isNot(tok::semi)) {
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001530 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001531 Actions.CodeCompleteReturn(getCurScope());
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001532 ConsumeCodeCompletionToken();
1533 SkipUntil(tok::semi, false, true);
1534 return StmtError();
1535 }
1536
Douglas Gregor6f4596c2011-03-11 23:10:44 +00001537 // FIXME: This is a hack to allow something like C++0x's generalized
1538 // initializer lists, but only enough of this feature to allow Clang to
1539 // parse libstdc++ 4.5's headers.
1540 if (Tok.is(tok::l_brace) && getLang().CPlusPlus) {
1541 R = ParseInitializer();
1542 if (R.isUsable() && !getLang().CPlusPlus0x)
1543 Diag(R.get()->getLocStart(), diag::ext_generalized_initializer_lists)
1544 << R.get()->getSourceRange();
1545 } else
1546 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001547 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001548 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001549 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001550 }
1551 }
John McCall9ae2f072010-08-23 23:25:46 +00001552 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001553}
1554
Steve Naroff5f8aa692008-02-11 23:15:56 +00001555/// FuzzyParseMicrosoftAsmStatement. When -fms-extensions is enabled, this
1556/// routine is called to skip/ignore tokens that comprise the MS asm statement.
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001557StmtResult Parser::FuzzyParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
1558 SourceLocation EndLoc;
Steve Naroffb746ce82008-02-07 23:24:32 +00001559 if (Tok.is(tok::l_brace)) {
1560 unsigned short savedBraceCount = BraceCount;
1561 do {
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001562 EndLoc = Tok.getLocation();
Steve Naroffb746ce82008-02-07 23:24:32 +00001563 ConsumeAnyToken();
1564 } while (BraceCount > savedBraceCount && Tok.isNot(tok::eof));
Mike Stump1eb44332009-09-09 15:08:12 +00001565 } else {
Steve Naroffb746ce82008-02-07 23:24:32 +00001566 // From the MS website: If used without braces, the __asm keyword means
1567 // that the rest of the line is an assembly-language statement.
1568 SourceManager &SrcMgr = PP.getSourceManager();
Steve Naroff03d6bc62008-02-08 03:36:19 +00001569 SourceLocation TokLoc = Tok.getLocation();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001570 unsigned LineNo = SrcMgr.getInstantiationLineNumber(TokLoc);
Steve Naroff36280972008-02-08 18:01:27 +00001571 do {
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001572 EndLoc = TokLoc;
Steve Naroff36280972008-02-08 18:01:27 +00001573 ConsumeAnyToken();
1574 TokLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001575 } while ((SrcMgr.getInstantiationLineNumber(TokLoc) == LineNo) &&
1576 Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) &&
Steve Naroff36280972008-02-08 18:01:27 +00001577 Tok.isNot(tok::eof));
Steve Naroffb746ce82008-02-07 23:24:32 +00001578 }
Mike Stump95059b52009-12-11 00:04:56 +00001579 Token t;
1580 t.setKind(tok::string_literal);
Chris Lattnere7896852010-08-17 16:00:12 +00001581 t.setLiteralData("\"/*FIXME: not done*/\"");
Mike Stump95059b52009-12-11 00:04:56 +00001582 t.clearFlag(Token::NeedsCleaning);
Chris Lattnere7896852010-08-17 16:00:12 +00001583 t.setLength(21);
Sean Hunt6cf75022010-08-30 17:47:05 +00001584 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
Mike Stump95059b52009-12-11 00:04:56 +00001585 ExprVector Constraints(Actions);
1586 ExprVector Exprs(Actions);
1587 ExprVector Clobbers(Actions);
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001588 return Actions.ActOnAsmStmt(AsmLoc, true, true, 0, 0, 0,
Mike Stump95059b52009-12-11 00:04:56 +00001589 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001590 AsmString.take(), move_arg(Clobbers),
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001591 EndLoc, true);
Steve Naroffd62701b2008-02-07 03:50:06 +00001592}
1593
Reid Spencer5f016e22007-07-11 17:01:13 +00001594/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001595/// asm-statement:
1596/// gnu-asm-statement
1597/// ms-asm-statement
1598///
1599/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001600/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1601///
1602/// [GNU] asm-argument:
1603/// asm-string-literal
1604/// asm-string-literal ':' asm-operands[opt]
1605/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1606/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1607/// ':' asm-clobbers
1608///
1609/// [GNU] asm-clobbers:
1610/// asm-string-literal
1611/// asm-clobbers ',' asm-string-literal
1612///
Steve Naroff5f8aa692008-02-11 23:15:56 +00001613/// [MS] ms-asm-statement:
1614/// '__asm' assembly-instruction ';'[opt]
1615/// '__asm' '{' assembly-instruction-list '}' ';'[opt]
1616///
1617/// [MS] assembly-instruction-list:
1618/// assembly-instruction ';'[opt]
1619/// assembly-instruction-list ';' assembly-instruction ';'[opt]
1620///
John McCall60d7b3a2010-08-24 06:29:42 +00001621StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001622 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001623 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001624
Steve Naroff5f8aa692008-02-11 23:15:56 +00001625 if (getLang().Microsoft && Tok.isNot(tok::l_paren) && !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001626 msAsm = true;
Abramo Bagnaraa44724d2010-12-02 18:34:55 +00001627 return FuzzyParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffd62701b2008-02-07 03:50:06 +00001628 }
John McCall0b7e6782011-03-24 11:26:52 +00001629 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00001630 SourceLocation Loc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00001631 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redl9a920342008-12-11 19:48:14 +00001632
Reid Spencer5f016e22007-07-11 17:01:13 +00001633 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1634 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001635 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001636 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001637 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001638
Reid Spencer5f016e22007-07-11 17:01:13 +00001639 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001640 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001641 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001642 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001643 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001644 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001645 }
1646 Loc = ConsumeParen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001647
John McCall60d7b3a2010-08-24 06:29:42 +00001648 ExprResult AsmString(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001649 if (AsmString.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001650 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001651
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001652 llvm::SmallVector<IdentifierInfo *, 4> Names;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001653 ExprVector Constraints(Actions);
1654 ExprVector Exprs(Actions);
1655 ExprVector Clobbers(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +00001656
Anders Carlssondfab34a2008-02-05 23:03:50 +00001657 if (Tok.is(tok::r_paren)) {
Chris Lattner64cb4752009-12-20 23:00:41 +00001658 // We have a simple asm expression like 'asm("foo")'.
1659 SourceLocation RParenLoc = ConsumeParen();
1660 return Actions.ActOnAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1661 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1662 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001663 AsmString.take(), move_arg(Clobbers),
Chris Lattner64cb4752009-12-20 23:00:41 +00001664 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001665 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001666
Chris Lattner64cb4752009-12-20 23:00:41 +00001667 // Parse Outputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001668 bool AteExtraColon = false;
1669 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1670 // In C++ mode, parse "::" like ": :".
1671 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattner64cb4752009-12-20 23:00:41 +00001672 ConsumeToken();
1673
Chris Lattner64056462009-12-20 23:08:04 +00001674 if (!AteExtraColon &&
1675 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001676 return StmtError();
1677 }
Chris Lattner64056462009-12-20 23:08:04 +00001678
Chris Lattner64cb4752009-12-20 23:00:41 +00001679 unsigned NumOutputs = Names.size();
1680
1681 // Parse Inputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001682 if (AteExtraColon ||
1683 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1684 // In C++ mode, parse "::" like ": :".
1685 if (AteExtraColon)
1686 AteExtraColon = false;
1687 else {
1688 AteExtraColon = Tok.is(tok::coloncolon);
1689 ConsumeToken();
1690 }
1691
1692 if (!AteExtraColon &&
1693 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001694 return StmtError();
1695 }
1696
1697 assert(Names.size() == Constraints.size() &&
1698 Constraints.size() == Exprs.size() &&
1699 "Input operand size mismatch!");
1700
1701 unsigned NumInputs = Names.size() - NumOutputs;
1702
1703 // Parse the clobbers, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001704 if (AteExtraColon || Tok.is(tok::colon)) {
1705 if (!AteExtraColon)
1706 ConsumeToken();
Chris Lattner64cb4752009-12-20 23:00:41 +00001707
Chandler Carruth102e1b62010-07-22 07:11:21 +00001708 // Parse the asm-string list for clobbers if present.
1709 if (Tok.isNot(tok::r_paren)) {
1710 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +00001711 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattner64cb4752009-12-20 23:00:41 +00001712
Chandler Carruth102e1b62010-07-22 07:11:21 +00001713 if (Clobber.isInvalid())
1714 break;
Chris Lattner64cb4752009-12-20 23:00:41 +00001715
Chandler Carruth102e1b62010-07-22 07:11:21 +00001716 Clobbers.push_back(Clobber.release());
Chris Lattner64cb4752009-12-20 23:00:41 +00001717
Chandler Carruth102e1b62010-07-22 07:11:21 +00001718 if (Tok.isNot(tok::comma)) break;
1719 ConsumeToken();
1720 }
Chris Lattner64cb4752009-12-20 23:00:41 +00001721 }
1722 }
1723
1724 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, Loc);
1725 return Actions.ActOnAsmStmt(AsmLoc, false, isVolatile,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001726 NumOutputs, NumInputs, Names.data(),
Sebastian Redlf512e822009-01-18 18:03:53 +00001727 move_arg(Constraints), move_arg(Exprs),
John McCall9ae2f072010-08-23 23:25:46 +00001728 AsmString.take(), move_arg(Clobbers),
Sebastian Redl3037ed02009-01-18 16:53:17 +00001729 RParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001730}
1731
1732/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattner64cb4752009-12-20 23:00:41 +00001733/// asm-statement, assuming the leading ':' token was eaten.
Reid Spencer5f016e22007-07-11 17:01:13 +00001734///
1735/// [GNU] asm-operands:
1736/// asm-operand
1737/// asm-operands ',' asm-operand
1738///
1739/// [GNU] asm-operand:
1740/// asm-string-literal '(' expression ')'
1741/// '[' identifier ']' asm-string-literal '(' expression ')'
1742///
Daniel Dunbar5ffe14c2009-10-18 20:26:27 +00001743//
1744// FIXME: Avoid unnecessary std::string trashing.
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001745bool Parser::ParseAsmOperandsOpt(llvm::SmallVectorImpl<IdentifierInfo *> &Names,
1746 llvm::SmallVectorImpl<ExprTy *> &Constraints,
1747 llvm::SmallVectorImpl<ExprTy *> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001748 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001749 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001750 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001751
1752 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001753 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001754 if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001755 SourceLocation Loc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00001756
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001757 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001758 Diag(Tok, diag::err_expected_ident);
1759 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001760 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001761 }
Mike Stump1eb44332009-09-09 15:08:12 +00001762
Anders Carlssonb235fc22007-11-22 01:36:19 +00001763 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001764 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001765
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001766 Names.push_back(II);
Reid Spencer5f016e22007-07-11 17:01:13 +00001767 MatchRHSPunctuation(tok::r_square, Loc);
Anders Carlssonb235fc22007-11-22 01:36:19 +00001768 } else
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001769 Names.push_back(0);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001770
John McCall60d7b3a2010-08-24 06:29:42 +00001771 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001772 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001773 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001774 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001775 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001776 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001777
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001778 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001779 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001780 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001781 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001782 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001783
Reid Spencer5f016e22007-07-11 17:01:13 +00001784 // Read the parenthesized expression.
Eli Friedman72056a22009-05-03 07:49:42 +00001785 SourceLocation OpenLoc = ConsumeParen();
John McCall60d7b3a2010-08-24 06:29:42 +00001786 ExprResult Res(ParseExpression());
Eli Friedman72056a22009-05-03 07:49:42 +00001787 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001788 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001789 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001790 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001791 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001792 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001793 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001794 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001795 ConsumeToken();
1796 }
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001797
1798 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001799}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001800
Douglas Gregorc9977d02011-03-16 17:05:57 +00001801Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner40e9bc82009-03-05 00:49:17 +00001802 assert(Tok.is(tok::l_brace));
1803 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001804
Douglas Gregorc9977d02011-03-16 17:05:57 +00001805 if (PP.isCodeCompletionEnabled()) {
1806 if (trySkippingFunctionBodyForCodeCompletion()) {
1807 BodyScope.Exit();
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001808 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregorc9977d02011-03-16 17:05:57 +00001809 }
1810 }
1811
John McCallf312b1e2010-08-26 23:41:50 +00001812 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1813 "parsing function body");
Mike Stump1eb44332009-09-09 15:08:12 +00001814
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001815 // Do not enter a scope for the brace, as the arguments are in the same scope
1816 // (the function body) as the body itself. Instead, just read the statement
1817 // list and put it into a CompoundStmt for safe keeping.
John McCall60d7b3a2010-08-24 06:29:42 +00001818 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl61364dd2008-12-11 19:30:53 +00001819
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001820 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001821 if (FnBody.isInvalid())
Mike Stump1eb44332009-09-09 15:08:12 +00001822 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Chris Lattner40e9bc82009-03-05 00:49:17 +00001823 MultiStmtArg(Actions), false);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001824
Douglas Gregorc9977d02011-03-16 17:05:57 +00001825 BodyScope.Exit();
John McCall9ae2f072010-08-23 23:25:46 +00001826 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001827}
Sebastian Redla0fd8652008-12-21 16:41:36 +00001828
Sebastian Redld3a413d2009-04-26 20:35:05 +00001829/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1830///
1831/// function-try-block:
1832/// 'try' ctor-initializer[opt] compound-statement handler-seq
1833///
Douglas Gregorc9977d02011-03-16 17:05:57 +00001834Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redld3a413d2009-04-26 20:35:05 +00001835 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1836 SourceLocation TryLoc = ConsumeToken();
1837
John McCallf312b1e2010-08-26 23:41:50 +00001838 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1839 "parsing function try block");
Sebastian Redld3a413d2009-04-26 20:35:05 +00001840
1841 // Constructor initializer list?
1842 if (Tok.is(tok::colon))
1843 ParseConstructorInitializer(Decl);
1844
Douglas Gregorc9977d02011-03-16 17:05:57 +00001845 if (PP.isCodeCompletionEnabled()) {
1846 if (trySkippingFunctionBodyForCodeCompletion()) {
1847 BodyScope.Exit();
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001848 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregorc9977d02011-03-16 17:05:57 +00001849 }
1850 }
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001851
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001852 SourceLocation LBraceLoc = Tok.getLocation();
John McCall60d7b3a2010-08-24 06:29:42 +00001853 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redld3a413d2009-04-26 20:35:05 +00001854 // If we failed to parse the try-catch, we just give the function an empty
1855 // compound statement as the body.
1856 if (FnBody.isInvalid())
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001857 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Sebastian Redld3a413d2009-04-26 20:35:05 +00001858 MultiStmtArg(Actions), false);
1859
Douglas Gregorc9977d02011-03-16 17:05:57 +00001860 BodyScope.Exit();
John McCall9ae2f072010-08-23 23:25:46 +00001861 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redld3a413d2009-04-26 20:35:05 +00001862}
1863
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001864bool Parser::trySkippingFunctionBodyForCodeCompletion() {
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001865 assert(Tok.is(tok::l_brace));
Argyrios Kyrtzidisb1620542011-01-04 00:27:27 +00001866 assert(PP.isCodeCompletionEnabled() &&
1867 "Should only be called when in code-completion mode");
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001868
1869 // We're in code-completion mode. Skip parsing for all function bodies unless
1870 // the body contains the code-completion point.
1871 TentativeParsingAction PA(*this);
1872 ConsumeBrace();
1873 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
1874 /*StopAtCodeCompletion=*/true)) {
1875 PA.Commit();
1876 return true;
1877 }
1878
1879 PA.Revert();
1880 return false;
1881}
1882
Sebastian Redla0fd8652008-12-21 16:41:36 +00001883/// ParseCXXTryBlock - Parse a C++ try-block.
1884///
1885/// try-block:
1886/// 'try' compound-statement handler-seq
1887///
John McCall7f040a92010-12-24 02:08:15 +00001888StmtResult Parser::ParseCXXTryBlock(ParsedAttributes &attrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001889 // FIXME: Add attributes?
Ted Kremenek1e377652010-02-11 02:19:13 +00001890
Sebastian Redla0fd8652008-12-21 16:41:36 +00001891 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1892
1893 SourceLocation TryLoc = ConsumeToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001894 return ParseCXXTryBlockCommon(TryLoc);
1895}
1896
1897/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1898/// function-try-block.
1899///
1900/// try-block:
1901/// 'try' compound-statement handler-seq
1902///
1903/// function-try-block:
1904/// 'try' ctor-initializer[opt] compound-statement handler-seq
1905///
1906/// handler-seq:
1907/// handler handler-seq[opt]
1908///
John Wiegley28bbe4b2011-04-28 01:08:34 +00001909/// [Borland] try-block:
1910/// 'try' compound-statement seh-except-block
1911/// 'try' compound-statment seh-finally-block
1912///
John McCall60d7b3a2010-08-24 06:29:42 +00001913StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redla0fd8652008-12-21 16:41:36 +00001914 if (Tok.isNot(tok::l_brace))
1915 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Sean Huntbbd37c62009-11-21 08:43:09 +00001916 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall0b7e6782011-03-24 11:26:52 +00001917 ParsedAttributesWithRange attrs(AttrFactory);
Douglas Gregorbca01b42011-07-06 22:04:06 +00001918 StmtResult TryBlock(ParseCompoundStatement(attrs, /*isStmtExpr=*/false,
1919 Scope::DeclScope|Scope::TryScope));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001920 if (TryBlock.isInvalid())
1921 return move(TryBlock);
1922
John Wiegley28bbe4b2011-04-28 01:08:34 +00001923 // Borland allows SEH-handlers with 'try'
1924 if(Tok.is(tok::kw___except) || Tok.is(tok::kw___finally)) {
1925 // TODO: Factor into common return ParseSEHHandlerCommon(...)
1926 StmtResult Handler;
1927 if(Tok.is(tok::kw___except)) {
1928 SourceLocation Loc = ConsumeToken();
1929 Handler = ParseSEHExceptBlock(Loc);
1930 }
1931 else {
1932 SourceLocation Loc = ConsumeToken();
1933 Handler = ParseSEHFinallyBlock(Loc);
1934 }
1935 if(Handler.isInvalid())
1936 return move(Handler);
John McCall7f040a92010-12-24 02:08:15 +00001937
John Wiegley28bbe4b2011-04-28 01:08:34 +00001938 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
1939 TryLoc,
1940 TryBlock.take(),
1941 Handler.take());
Sebastian Redla0fd8652008-12-21 16:41:36 +00001942 }
John Wiegley28bbe4b2011-04-28 01:08:34 +00001943 else {
1944 StmtVector Handlers(Actions);
1945 MaybeParseCXX0XAttributes(attrs);
1946 ProhibitAttributes(attrs);
Sebastian Redla0fd8652008-12-21 16:41:36 +00001947
John Wiegley28bbe4b2011-04-28 01:08:34 +00001948 if (Tok.isNot(tok::kw_catch))
1949 return StmtError(Diag(Tok, diag::err_expected_catch));
1950 while (Tok.is(tok::kw_catch)) {
1951 StmtResult Handler(ParseCXXCatchBlock());
1952 if (!Handler.isInvalid())
1953 Handlers.push_back(Handler.release());
1954 }
1955 // Don't bother creating the full statement if we don't have any usable
1956 // handlers.
1957 if (Handlers.empty())
1958 return StmtError();
1959
1960 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(), move_arg(Handlers));
1961 }
Sebastian Redla0fd8652008-12-21 16:41:36 +00001962}
1963
1964/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
1965///
1966/// handler:
1967/// 'catch' '(' exception-declaration ')' compound-statement
1968///
1969/// exception-declaration:
1970/// type-specifier-seq declarator
1971/// type-specifier-seq abstract-declarator
1972/// type-specifier-seq
1973/// '...'
1974///
John McCall60d7b3a2010-08-24 06:29:42 +00001975StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redla0fd8652008-12-21 16:41:36 +00001976 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
1977
1978 SourceLocation CatchLoc = ConsumeToken();
1979
1980 SourceLocation LParenLoc = Tok.getLocation();
1981 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen))
1982 return StmtError();
1983
1984 // C++ 3.3.2p3:
1985 // The name in a catch exception-declaration is local to the handler and
1986 // shall not be redeclared in the outermost block of the handler.
1987 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
1988
1989 // exception-declaration is equivalent to '...' or a parameter-declaration
1990 // without default arguments.
John McCalld226f652010-08-21 09:40:31 +00001991 Decl *ExceptionDecl = 0;
Sebastian Redla0fd8652008-12-21 16:41:36 +00001992 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00001993 DeclSpec DS(AttrFactory);
Sebastian Redl4b07b292008-12-22 19:15:10 +00001994 if (ParseCXXTypeSpecifierSeq(DS))
1995 return StmtError();
Sebastian Redla0fd8652008-12-21 16:41:36 +00001996 Declarator ExDecl(DS, Declarator::CXXCatchContext);
1997 ParseDeclarator(ExDecl);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001998 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redla0fd8652008-12-21 16:41:36 +00001999 } else
2000 ConsumeToken();
2001
2002 if (MatchRHSPunctuation(tok::r_paren, LParenLoc).isInvalid())
2003 return StmtError();
2004
2005 if (Tok.isNot(tok::l_brace))
2006 return StmtError(Diag(Tok, diag::err_expected_lbrace));
2007
Sean Huntbbd37c62009-11-21 08:43:09 +00002008 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
John McCall0b7e6782011-03-24 11:26:52 +00002009 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002010 StmtResult Block(ParseCompoundStatement(attrs));
Sebastian Redla0fd8652008-12-21 16:41:36 +00002011 if (Block.isInvalid())
2012 return move(Block);
2013
John McCall9ae2f072010-08-23 23:25:46 +00002014 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redla0fd8652008-12-21 16:41:36 +00002015}
Francois Pichet1e862692011-05-06 20:48:22 +00002016
2017void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Francois Pichetf9860382011-05-07 17:30:27 +00002018 bool Result;
2019 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet1e862692011-05-06 20:48:22 +00002020 return;
Francois Pichet1e862692011-05-06 20:48:22 +00002021
Francois Pichet1e862692011-05-06 20:48:22 +00002022 if (Tok.isNot(tok::l_brace)) {
2023 Diag(Tok, diag::err_expected_lbrace);
2024 return;
2025 }
2026 ConsumeBrace();
2027
Francois Pichetf9860382011-05-07 17:30:27 +00002028 // Condition is false skip all inside the {}.
2029 if (!Result) {
Francois Pichet1e862692011-05-06 20:48:22 +00002030 SkipUntil(tok::r_brace, false);
2031 return;
2032 }
2033
2034 // Condition is true, parse the statements.
2035 while (Tok.isNot(tok::r_brace)) {
2036 StmtResult R = ParseStatementOrDeclaration(Stmts, false);
2037 if (R.isUsable())
2038 Stmts.push_back(R.release());
2039 }
2040
2041 if (Tok.isNot(tok::r_brace)) {
2042 Diag(Tok, diag::err_expected_rbrace);
2043 return;
2044 }
2045 ConsumeBrace();
2046}