1 //===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Statement and Block portions of the Parser
13 //===----------------------------------------------------------------------===//
15 #include "clang/Parse/Parser.h"
16 #include "RAIIObjectsForParser.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/Basic/Attributes.h"
19 #include "clang/Basic/Diagnostic.h"
20 #include "clang/Basic/PrettyStackTrace.h"
21 #include "clang/Sema/DeclSpec.h"
22 #include "clang/Sema/LoopHint.h"
23 #include "clang/Sema/PrettyDeclStackTrace.h"
24 #include "clang/Sema/Scope.h"
25 #include "clang/Sema/TypoCorrection.h"
26 #include "llvm/ADT/SmallString.h"
27 using namespace clang;
29 //===----------------------------------------------------------------------===//
30 // C99 6.8: Statements and Blocks.
31 //===----------------------------------------------------------------------===//
33 /// \brief Parse a standalone statement (for instance, as the body of an 'if',
34 /// 'while', or 'for').
35 StmtResult Parser::ParseStatement(SourceLocation *TrailingElseLoc) {
38 // We may get back a null statement if we found a #pragma. Keep going until
39 // we get an actual statement.
42 Res = ParseStatementOrDeclaration(Stmts, true, TrailingElseLoc);
43 } while (!Res.isInvalid() && !Res.get());
48 /// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
49 /// StatementOrDeclaration:
55 /// compound-statement
56 /// expression-statement
57 /// selection-statement
58 /// iteration-statement
60 /// [C++] declaration-statement
62 /// [MS] seh-try-block
63 /// [OBC] objc-throw-statement
64 /// [OBC] objc-try-catch-statement
65 /// [OBC] objc-synchronized-statement
66 /// [GNU] asm-statement
67 /// [OMP] openmp-construct [TODO]
69 /// labeled-statement:
70 /// identifier ':' statement
71 /// 'case' constant-expression ':' statement
72 /// 'default' ':' statement
74 /// selection-statement:
78 /// iteration-statement:
83 /// expression-statement:
84 /// expression[opt] ';'
87 /// 'goto' identifier ';'
90 /// 'return' expression[opt] ';'
91 /// [GNU] 'goto' '*' expression ';'
93 /// [OBC] objc-throw-statement:
94 /// [OBC] '@' 'throw' expression ';'
95 /// [OBC] '@' 'throw' ';'
98 Parser::ParseStatementOrDeclaration(StmtVector &Stmts, bool OnlyStatement,
99 SourceLocation *TrailingElseLoc) {
101 ParenBraceBracketBalancer BalancerRAIIObj(*this);
103 ParsedAttributesWithRange Attrs(AttrFactory);
104 MaybeParseCXX11Attributes(Attrs, nullptr, /*MightBeObjCMessageSend*/ true);
106 StmtResult Res = ParseStatementOrDeclarationAfterAttributes(Stmts,
107 OnlyStatement, TrailingElseLoc, Attrs);
109 assert((Attrs.empty() || Res.isInvalid() || Res.isUsable()) &&
110 "attributes on empty statement");
112 if (Attrs.empty() || Res.isInvalid())
115 return Actions.ProcessStmtAttributes(Res.get(), Attrs.getList(), Attrs.Range);
119 class StatementFilterCCC : public CorrectionCandidateCallback {
121 StatementFilterCCC(Token nextTok) : NextToken(nextTok) {
122 WantTypeSpecifiers = nextTok.is(tok::l_paren) || nextTok.is(tok::less) ||
123 nextTok.is(tok::identifier) || nextTok.is(tok::star) ||
124 nextTok.is(tok::amp) || nextTok.is(tok::l_square);
125 WantExpressionKeywords = nextTok.is(tok::l_paren) ||
126 nextTok.is(tok::identifier) ||
127 nextTok.is(tok::arrow) || nextTok.is(tok::period);
128 WantRemainingKeywords = nextTok.is(tok::l_paren) || nextTok.is(tok::semi) ||
129 nextTok.is(tok::identifier) ||
130 nextTok.is(tok::l_brace);
131 WantCXXNamedCasts = false;
134 bool ValidateCandidate(const TypoCorrection &candidate) override {
135 if (FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>())
136 return !candidate.getCorrectionSpecifier() || isa<ObjCIvarDecl>(FD);
137 if (NextToken.is(tok::equal))
138 return candidate.getCorrectionDeclAs<VarDecl>();
139 if (NextToken.is(tok::period) &&
140 candidate.getCorrectionDeclAs<NamespaceDecl>())
142 return CorrectionCandidateCallback::ValidateCandidate(candidate);
151 Parser::ParseStatementOrDeclarationAfterAttributes(StmtVector &Stmts,
152 bool OnlyStatement, SourceLocation *TrailingElseLoc,
153 ParsedAttributesWithRange &Attrs) {
154 const char *SemiError = nullptr;
157 // Cases in this switch statement should fall through if the parser expects
158 // the token to end in a semicolon (in which case SemiError should be set),
159 // or they directly 'return;' if not.
161 tok::TokenKind Kind = Tok.getKind();
162 SourceLocation AtLoc;
164 case tok::at: // May be a @try or @throw statement
166 ProhibitAttributes(Attrs); // TODO: is it correct?
167 AtLoc = ConsumeToken(); // consume @
168 return ParseObjCAtStatement(AtLoc);
171 case tok::code_completion:
172 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
176 case tok::identifier: {
177 Token Next = NextToken();
178 if (Next.is(tok::colon)) { // C99 6.8.1: labeled-statement
179 // identifier ':' statement
180 return ParseLabeledStatement(Attrs);
183 // Look up the identifier, and typo-correct it to a keyword if it's not
185 if (Next.isNot(tok::coloncolon)) {
186 // Try to limit which sets of keywords should be included in typo
187 // correction based on what the next token is.
188 if (TryAnnotateName(/*IsAddressOfOperand*/ false,
189 llvm::make_unique<StatementFilterCCC>(Next)) ==
191 // Handle errors here by skipping up to the next semicolon or '}', and
192 // eat the semicolon if that's what stopped us.
193 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
194 if (Tok.is(tok::semi))
199 // If the identifier was typo-corrected, try again.
200 if (Tok.isNot(tok::identifier))
208 if ((getLangOpts().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
209 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
210 DeclGroupPtrTy Decl = ParseDeclaration(Declarator::BlockContext,
212 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
215 if (Tok.is(tok::r_brace)) {
216 Diag(Tok, diag::err_expected_statement);
220 return ParseExprStatement();
223 case tok::kw_case: // C99 6.8.1: labeled-statement
224 return ParseCaseStatement();
225 case tok::kw_default: // C99 6.8.1: labeled-statement
226 return ParseDefaultStatement();
228 case tok::l_brace: // C99 6.8.2: compound-statement
229 return ParseCompoundStatement();
230 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
231 bool HasLeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
232 return Actions.ActOnNullStmt(ConsumeToken(), HasLeadingEmptyMacro);
235 case tok::kw_if: // C99 6.8.4.1: if-statement
236 return ParseIfStatement(TrailingElseLoc);
237 case tok::kw_switch: // C99 6.8.4.2: switch-statement
238 return ParseSwitchStatement(TrailingElseLoc);
240 case tok::kw_while: // C99 6.8.5.1: while-statement
241 return ParseWhileStatement(TrailingElseLoc);
242 case tok::kw_do: // C99 6.8.5.2: do-statement
243 Res = ParseDoStatement();
244 SemiError = "do/while";
246 case tok::kw_for: // C99 6.8.5.3: for-statement
247 return ParseForStatement(TrailingElseLoc);
249 case tok::kw_goto: // C99 6.8.6.1: goto-statement
250 Res = ParseGotoStatement();
253 case tok::kw_continue: // C99 6.8.6.2: continue-statement
254 Res = ParseContinueStatement();
255 SemiError = "continue";
257 case tok::kw_break: // C99 6.8.6.3: break-statement
258 Res = ParseBreakStatement();
261 case tok::kw_return: // C99 6.8.6.4: return-statement
262 Res = ParseReturnStatement();
263 SemiError = "return";
267 ProhibitAttributes(Attrs);
269 Res = ParseAsmStatement(msAsm);
270 Res = Actions.ActOnFinishFullStmt(Res.get());
271 if (msAsm) return Res;
276 case tok::kw___if_exists:
277 case tok::kw___if_not_exists:
278 ProhibitAttributes(Attrs);
279 ParseMicrosoftIfExistsStatement(Stmts);
280 // An __if_exists block is like a compound statement, but it doesn't create
284 case tok::kw_try: // C++ 15: try-block
285 return ParseCXXTryBlock();
288 ProhibitAttributes(Attrs); // TODO: is it correct?
289 return ParseSEHTryBlock();
291 case tok::kw___leave:
292 Res = ParseSEHLeaveStatement();
293 SemiError = "__leave";
296 case tok::annot_pragma_vis:
297 ProhibitAttributes(Attrs);
298 HandlePragmaVisibility();
301 case tok::annot_pragma_pack:
302 ProhibitAttributes(Attrs);
306 case tok::annot_pragma_msstruct:
307 ProhibitAttributes(Attrs);
308 HandlePragmaMSStruct();
311 case tok::annot_pragma_align:
312 ProhibitAttributes(Attrs);
316 case tok::annot_pragma_weak:
317 ProhibitAttributes(Attrs);
321 case tok::annot_pragma_weakalias:
322 ProhibitAttributes(Attrs);
323 HandlePragmaWeakAlias();
326 case tok::annot_pragma_redefine_extname:
327 ProhibitAttributes(Attrs);
328 HandlePragmaRedefineExtname();
331 case tok::annot_pragma_fp_contract:
332 ProhibitAttributes(Attrs);
333 Diag(Tok, diag::err_pragma_fp_contract_scope);
337 case tok::annot_pragma_opencl_extension:
338 ProhibitAttributes(Attrs);
339 HandlePragmaOpenCLExtension();
342 case tok::annot_pragma_captured:
343 ProhibitAttributes(Attrs);
344 return HandlePragmaCaptured();
346 case tok::annot_pragma_openmp:
347 ProhibitAttributes(Attrs);
348 return ParseOpenMPDeclarativeOrExecutableDirective(!OnlyStatement);
350 case tok::annot_pragma_ms_pointers_to_members:
351 ProhibitAttributes(Attrs);
352 HandlePragmaMSPointersToMembers();
355 case tok::annot_pragma_ms_pragma:
356 ProhibitAttributes(Attrs);
357 HandlePragmaMSPragma();
360 case tok::annot_pragma_loop_hint:
361 ProhibitAttributes(Attrs);
362 return ParsePragmaLoopHint(Stmts, OnlyStatement, TrailingElseLoc, Attrs);
365 // If we reached this code, the statement must end in a semicolon.
366 if (!TryConsumeToken(tok::semi) && !Res.isInvalid()) {
367 // If the result was valid, then we do want to diagnose this. Use
368 // ExpectAndConsume to emit the diagnostic, even though we know it won't
370 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
371 // Skip until we see a } or ;, but don't eat it.
372 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
378 /// \brief Parse an expression statement.
379 StmtResult Parser::ParseExprStatement() {
380 // If a case keyword is missing, this is where it should be inserted.
381 Token OldToken = Tok;
383 // expression[opt] ';'
384 ExprResult Expr(ParseExpression());
385 if (Expr.isInvalid()) {
386 // If the expression is invalid, skip ahead to the next semicolon or '}'.
387 // Not doing this opens us up to the possibility of infinite loops if
388 // ParseExpression does not consume any tokens.
389 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
390 if (Tok.is(tok::semi))
392 return Actions.ActOnExprStmtError();
395 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
396 Actions.CheckCaseExpression(Expr.get())) {
397 // If a constant expression is followed by a colon inside a switch block,
398 // suggest a missing case keyword.
399 Diag(OldToken, diag::err_expected_case_before_expression)
400 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
402 // Recover parsing as a case statement.
403 return ParseCaseStatement(/*MissingCase=*/true, Expr);
406 // Otherwise, eat the semicolon.
407 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
408 return Actions.ActOnExprStmt(Expr);
411 StmtResult Parser::ParseSEHTryBlock() {
412 assert(Tok.is(tok::kw___try) && "Expected '__try'");
413 SourceLocation Loc = ConsumeToken();
414 return ParseSEHTryBlockCommon(Loc);
417 /// ParseSEHTryBlockCommon
420 /// '__try' compound-statement seh-handler
424 /// seh-finally-block
426 StmtResult Parser::ParseSEHTryBlockCommon(SourceLocation TryLoc) {
427 if(Tok.isNot(tok::l_brace))
428 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
430 StmtResult TryBlock(ParseCompoundStatement(/*isStmtExpr=*/false,
431 Scope::DeclScope | Scope::SEHTryScope));
432 if(TryBlock.isInvalid())
436 if (Tok.is(tok::identifier) &&
437 Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
438 SourceLocation Loc = ConsumeToken();
439 Handler = ParseSEHExceptBlock(Loc);
440 } else if (Tok.is(tok::kw___finally)) {
441 SourceLocation Loc = ConsumeToken();
442 Handler = ParseSEHFinallyBlock(Loc);
444 return StmtError(Diag(Tok,diag::err_seh_expected_handler));
447 if(Handler.isInvalid())
450 return Actions.ActOnSEHTryBlock(false /* IsCXXTry */,
456 /// ParseSEHExceptBlock - Handle __except
458 /// seh-except-block:
459 /// '__except' '(' seh-filter-expression ')' compound-statement
461 StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) {
462 PoisonIdentifierRAIIObject raii(Ident__exception_code, false),
463 raii2(Ident___exception_code, false),
464 raii3(Ident_GetExceptionCode, false);
466 if (ExpectAndConsume(tok::l_paren))
469 ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope);
471 if (getLangOpts().Borland) {
472 Ident__exception_info->setIsPoisoned(false);
473 Ident___exception_info->setIsPoisoned(false);
474 Ident_GetExceptionInfo->setIsPoisoned(false);
476 ExprResult FilterExpr(ParseExpression());
478 if (getLangOpts().Borland) {
479 Ident__exception_info->setIsPoisoned(true);
480 Ident___exception_info->setIsPoisoned(true);
481 Ident_GetExceptionInfo->setIsPoisoned(true);
484 if(FilterExpr.isInvalid())
487 if (ExpectAndConsume(tok::r_paren))
490 StmtResult Block(ParseCompoundStatement());
492 if(Block.isInvalid())
495 return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.get(), Block.get());
498 /// ParseSEHFinallyBlock - Handle __finally
500 /// seh-finally-block:
501 /// '__finally' compound-statement
503 StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyBlock) {
504 PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
505 raii2(Ident___abnormal_termination, false),
506 raii3(Ident_AbnormalTermination, false);
508 StmtResult Block(ParseCompoundStatement());
509 if(Block.isInvalid())
512 return Actions.ActOnSEHFinallyBlock(FinallyBlock,Block.get());
517 /// seh-leave-statement:
520 StmtResult Parser::ParseSEHLeaveStatement() {
521 SourceLocation LeaveLoc = ConsumeToken(); // eat the '__leave'.
522 return Actions.ActOnSEHLeaveStmt(LeaveLoc, getCurScope());
525 /// ParseLabeledStatement - We have an identifier and a ':' after it.
527 /// labeled-statement:
528 /// identifier ':' statement
529 /// [GNU] identifier ':' attributes[opt] statement
531 StmtResult Parser::ParseLabeledStatement(ParsedAttributesWithRange &attrs) {
532 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
533 "Not an identifier!");
535 Token IdentTok = Tok; // Save the whole token.
536 ConsumeToken(); // eat the identifier.
538 assert(Tok.is(tok::colon) && "Not a label!");
540 // identifier ':' statement
541 SourceLocation ColonLoc = ConsumeToken();
543 // Read label attributes, if present.
545 if (Tok.is(tok::kw___attribute)) {
546 ParsedAttributesWithRange TempAttrs(AttrFactory);
547 ParseGNUAttributes(TempAttrs);
549 // In C++, GNU attributes only apply to the label if they are followed by a
550 // semicolon, to disambiguate label attributes from attributes on a labeled
553 // This doesn't quite match what GCC does; if the attribute list is empty
554 // and followed by a semicolon, GCC will reject (it appears to parse the
555 // attributes as part of a statement in that case). That looks like a bug.
556 if (!getLangOpts().CPlusPlus || Tok.is(tok::semi))
557 attrs.takeAllFrom(TempAttrs);
558 else if (isDeclarationStatement()) {
560 // FIXME: We should do this whether or not we have a declaration
561 // statement, but that doesn't work correctly (because ProhibitAttributes
562 // can't handle GNU attributes), so only call it in the one case where
563 // GNU attributes are allowed.
564 SubStmt = ParseStatementOrDeclarationAfterAttributes(
565 Stmts, /*OnlyStmts*/ true, nullptr, TempAttrs);
566 if (!TempAttrs.empty() && !SubStmt.isInvalid())
567 SubStmt = Actions.ProcessStmtAttributes(
568 SubStmt.get(), TempAttrs.getList(), TempAttrs.Range);
570 Diag(Tok, diag::err_expected_after) << "__attribute__" << tok::semi;
574 // If we've not parsed a statement yet, parse one now.
575 if (!SubStmt.isInvalid() && !SubStmt.isUsable())
576 SubStmt = ParseStatement();
578 // Broken substmt shouldn't prevent the label from being added to the AST.
579 if (SubStmt.isInvalid())
580 SubStmt = Actions.ActOnNullStmt(ColonLoc);
582 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
583 IdentTok.getLocation());
584 if (AttributeList *Attrs = attrs.getList()) {
585 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
589 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
593 /// ParseCaseStatement
594 /// labeled-statement:
595 /// 'case' constant-expression ':' statement
596 /// [GNU] 'case' constant-expression '...' constant-expression ':' statement
598 StmtResult Parser::ParseCaseStatement(bool MissingCase, ExprResult Expr) {
599 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
601 // It is very very common for code to contain many case statements recursively
602 // nested, as in (but usually without indentation):
609 // Parsing this naively works, but is both inefficient and can cause us to run
610 // out of stack space in our recursive descent parser. As a special case,
611 // flatten this recursion into an iterative loop. This is complex and gross,
612 // but all the grossness is constrained to ParseCaseStatement (and some
613 // weirdness in the actions), so this is just local grossness :).
615 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
617 StmtResult TopLevelCase(true);
619 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
620 // gets updated each time a new case is parsed, and whose body is unset so
621 // far. When parsing 'case 4', this is the 'case 3' node.
622 Stmt *DeepestParsedCaseStmt = nullptr;
624 // While we have case statements, eat and stack them.
625 SourceLocation ColonLoc;
627 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
628 ConsumeToken(); // eat the 'case'.
629 ColonLoc = SourceLocation();
631 if (Tok.is(tok::code_completion)) {
632 Actions.CodeCompleteCase(getCurScope());
637 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
638 /// Disable this form of error recovery while we're parsing the case
640 ColonProtectionRAIIObject ColonProtection(*this);
644 LHS = ParseConstantExpression();
645 if (!getLangOpts().CPlusPlus11) {
646 LHS = Actions.CorrectDelayedTyposInExpr(LHS, [this](class Expr *E) {
647 return Actions.VerifyIntegerConstantExpression(E);
650 if (LHS.isInvalid()) {
651 // If constant-expression is parsed unsuccessfully, recover by skipping
652 // current case statement (moving to the colon that ends it).
653 if (SkipUntil(tok::colon, tok::r_brace, StopAtSemi | StopBeforeMatch)) {
654 TryConsumeToken(tok::colon, ColonLoc);
664 // GNU case range extension.
665 SourceLocation DotDotDotLoc;
667 if (TryConsumeToken(tok::ellipsis, DotDotDotLoc)) {
668 Diag(DotDotDotLoc, diag::ext_gnu_case_range);
669 RHS = ParseConstantExpression();
670 if (RHS.isInvalid()) {
671 if (SkipUntil(tok::colon, tok::r_brace, StopAtSemi | StopBeforeMatch)) {
672 TryConsumeToken(tok::colon, ColonLoc);
679 ColonProtection.restore();
681 if (TryConsumeToken(tok::colon, ColonLoc)) {
682 } else if (TryConsumeToken(tok::semi, ColonLoc) ||
683 TryConsumeToken(tok::coloncolon, ColonLoc)) {
684 // Treat "case blah;" or "case blah::" as a typo for "case blah:".
685 Diag(ColonLoc, diag::err_expected_after)
686 << "'case'" << tok::colon
687 << FixItHint::CreateReplacement(ColonLoc, ":");
689 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
690 Diag(ExpectedLoc, diag::err_expected_after)
691 << "'case'" << tok::colon
692 << FixItHint::CreateInsertion(ExpectedLoc, ":");
693 ColonLoc = ExpectedLoc;
697 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
698 RHS.get(), ColonLoc);
700 // If we had a sema error parsing this case, then just ignore it and
701 // continue parsing the sub-stmt.
702 if (Case.isInvalid()) {
703 if (TopLevelCase.isInvalid()) // No parsed case stmts.
704 return ParseStatement();
705 // Otherwise, just don't add it as a nested case.
707 // If this is the first case statement we parsed, it becomes TopLevelCase.
708 // Otherwise we link it into the current chain.
709 Stmt *NextDeepest = Case.get();
710 if (TopLevelCase.isInvalid())
713 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
714 DeepestParsedCaseStmt = NextDeepest;
717 // Handle all case statements.
718 } while (Tok.is(tok::kw_case));
720 // If we found a non-case statement, start by parsing it.
723 if (Tok.isNot(tok::r_brace)) {
724 SubStmt = ParseStatement();
726 // Nicely diagnose the common error "switch (X) { case 4: }", which is
727 // not valid. If ColonLoc doesn't point to a valid text location, there was
728 // another parsing error, so avoid producing extra diagnostics.
729 if (ColonLoc.isValid()) {
730 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
731 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
732 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
734 SubStmt = StmtError();
737 // Install the body into the most deeply-nested case.
738 if (DeepestParsedCaseStmt) {
739 // Broken sub-stmt shouldn't prevent forming the case statement properly.
740 if (SubStmt.isInvalid())
741 SubStmt = Actions.ActOnNullStmt(SourceLocation());
742 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
745 // Return the top level parsed statement tree.
749 /// ParseDefaultStatement
750 /// labeled-statement:
751 /// 'default' ':' statement
752 /// Note that this does not parse the 'statement' at the end.
754 StmtResult Parser::ParseDefaultStatement() {
755 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
756 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
758 SourceLocation ColonLoc;
759 if (TryConsumeToken(tok::colon, ColonLoc)) {
760 } else if (TryConsumeToken(tok::semi, ColonLoc)) {
761 // Treat "default;" as a typo for "default:".
762 Diag(ColonLoc, diag::err_expected_after)
763 << "'default'" << tok::colon
764 << FixItHint::CreateReplacement(ColonLoc, ":");
766 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
767 Diag(ExpectedLoc, diag::err_expected_after)
768 << "'default'" << tok::colon
769 << FixItHint::CreateInsertion(ExpectedLoc, ":");
770 ColonLoc = ExpectedLoc;
775 if (Tok.isNot(tok::r_brace)) {
776 SubStmt = ParseStatement();
778 // Diagnose the common error "switch (X) {... default: }", which is
780 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
781 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
782 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
786 // Broken sub-stmt shouldn't prevent forming the case statement properly.
787 if (SubStmt.isInvalid())
788 SubStmt = Actions.ActOnNullStmt(ColonLoc);
790 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
791 SubStmt.get(), getCurScope());
794 StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
795 return ParseCompoundStatement(isStmtExpr, Scope::DeclScope);
798 /// ParseCompoundStatement - Parse a "{}" block.
800 /// compound-statement: [C99 6.8.2]
801 /// { block-item-list[opt] }
802 /// [GNU] { label-declarations block-item-list } [TODO]
806 /// block-item-list block-item
810 /// [GNU] '__extension__' declaration
813 /// [GNU] label-declarations:
814 /// [GNU] label-declaration
815 /// [GNU] label-declarations label-declaration
817 /// [GNU] label-declaration:
818 /// [GNU] '__label__' identifier-list ';'
820 StmtResult Parser::ParseCompoundStatement(bool isStmtExpr,
821 unsigned ScopeFlags) {
822 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
824 // Enter a scope to hold everything within the compound stmt. Compound
825 // statements can always hold declarations.
826 ParseScope CompoundScope(this, ScopeFlags);
828 // Parse the statements in the body.
829 return ParseCompoundStatementBody(isStmtExpr);
832 /// Parse any pragmas at the start of the compound expression. We handle these
833 /// separately since some pragmas (FP_CONTRACT) must appear before any C
834 /// statement in the compound, but may be intermingled with other pragmas.
835 void Parser::ParseCompoundStatementLeadingPragmas() {
836 bool checkForPragmas = true;
837 while (checkForPragmas) {
838 switch (Tok.getKind()) {
839 case tok::annot_pragma_vis:
840 HandlePragmaVisibility();
842 case tok::annot_pragma_pack:
845 case tok::annot_pragma_msstruct:
846 HandlePragmaMSStruct();
848 case tok::annot_pragma_align:
851 case tok::annot_pragma_weak:
854 case tok::annot_pragma_weakalias:
855 HandlePragmaWeakAlias();
857 case tok::annot_pragma_redefine_extname:
858 HandlePragmaRedefineExtname();
860 case tok::annot_pragma_opencl_extension:
861 HandlePragmaOpenCLExtension();
863 case tok::annot_pragma_fp_contract:
864 HandlePragmaFPContract();
866 case tok::annot_pragma_ms_pointers_to_members:
867 HandlePragmaMSPointersToMembers();
869 case tok::annot_pragma_ms_pragma:
870 HandlePragmaMSPragma();
873 checkForPragmas = false;
880 /// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
881 /// ActOnCompoundStmt action. This expects the '{' to be the current token, and
882 /// consume the '}' at the end of the block. It does not manipulate the scope
884 StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
885 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
887 "in compound statement ('{}')");
889 // Record the state of the FP_CONTRACT pragma, restore on leaving the
890 // compound statement.
891 Sema::FPContractStateRAII SaveFPContractState(Actions);
893 InMessageExpressionRAIIObject InMessage(*this, false);
894 BalancedDelimiterTracker T(*this, tok::l_brace);
898 Sema::CompoundScopeRAII CompoundScope(Actions);
900 // Parse any pragmas at the beginning of the compound statement.
901 ParseCompoundStatementLeadingPragmas();
905 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
906 // only allowed at the start of a compound stmt regardless of the language.
907 while (Tok.is(tok::kw___label__)) {
908 SourceLocation LabelLoc = ConsumeToken();
910 SmallVector<Decl *, 8> DeclsInGroup;
912 if (Tok.isNot(tok::identifier)) {
913 Diag(Tok, diag::err_expected) << tok::identifier;
917 IdentifierInfo *II = Tok.getIdentifierInfo();
918 SourceLocation IdLoc = ConsumeToken();
919 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
921 if (!TryConsumeToken(tok::comma))
925 DeclSpec DS(AttrFactory);
927 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
928 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
930 ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
932 Stmts.push_back(R.get());
935 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
936 if (Tok.is(tok::annot_pragma_unused)) {
937 HandlePragmaUnused();
942 if (Tok.isNot(tok::kw___extension__)) {
943 R = ParseStatementOrDeclaration(Stmts, false);
945 // __extension__ can start declarations and it can also be a unary
946 // operator for expressions. Consume multiple __extension__ markers here
947 // until we can determine which is which.
948 // FIXME: This loses extension expressions in the AST!
949 SourceLocation ExtLoc = ConsumeToken();
950 while (Tok.is(tok::kw___extension__))
953 ParsedAttributesWithRange attrs(AttrFactory);
954 MaybeParseCXX11Attributes(attrs, nullptr,
955 /*MightBeObjCMessageSend*/ true);
957 // If this is the start of a declaration, parse it as such.
958 if (isDeclarationStatement()) {
959 // __extension__ silences extension warnings in the subdeclaration.
960 // FIXME: Save the __extension__ on the decl as a node somehow?
961 ExtensionRAIIObject O(Diags);
963 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
964 DeclGroupPtrTy Res = ParseDeclaration(Declarator::BlockContext, DeclEnd,
966 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
968 // Otherwise this was a unary __extension__ marker.
969 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
971 if (Res.isInvalid()) {
972 SkipUntil(tok::semi);
976 // FIXME: Use attributes?
977 // Eat the semicolon at the end of stmt and convert the expr into a
979 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
980 R = Actions.ActOnExprStmt(Res);
985 Stmts.push_back(R.get());
988 SourceLocation CloseLoc = Tok.getLocation();
990 // We broke out of the while loop because we found a '}' or EOF.
991 if (!T.consumeClose())
992 // Recover by creating a compound statement with what we parsed so far,
993 // instead of dropping everything and returning StmtError();
994 CloseLoc = T.getCloseLocation();
996 return Actions.ActOnCompoundStmt(T.getOpenLocation(), CloseLoc,
1000 /// ParseParenExprOrCondition:
1001 /// [C ] '(' expression ')'
1002 /// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
1004 /// This function parses and performs error recovery on the specified condition
1005 /// or expression (depending on whether we're in C++ or C mode). This function
1006 /// goes out of its way to recover well. It returns true if there was a parser
1007 /// error (the right paren couldn't be found), which indicates that the caller
1008 /// should try to recover harder. It returns false if the condition is
1009 /// successfully parsed. Note that a successful parse can still have semantic
1010 /// errors in the condition.
1011 bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
1014 bool ConvertToBoolean) {
1015 BalancedDelimiterTracker T(*this, tok::l_paren);
1018 if (getLangOpts().CPlusPlus)
1019 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
1021 ExprResult = ParseExpression();
1022 DeclResult = nullptr;
1024 // If required, convert to a boolean value.
1025 if (!ExprResult.isInvalid() && ConvertToBoolean)
1027 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
1030 // If the parser was confused by the condition and we don't have a ')', try to
1031 // recover by skipping ahead to a semi and bailing out. If condexp is
1032 // semantically invalid but we have well formed code, keep going.
1033 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
1034 SkipUntil(tok::semi);
1035 // Skipping may have stopped if it found the containing ')'. If so, we can
1036 // continue parsing the if statement.
1037 if (Tok.isNot(tok::r_paren))
1041 // Otherwise the condition is valid or the rparen is present.
1044 // Check for extraneous ')'s to catch things like "if (foo())) {". We know
1045 // that all callers are looking for a statement after the condition, so ")"
1047 while (Tok.is(tok::r_paren)) {
1048 Diag(Tok, diag::err_extraneous_rparen_in_condition)
1049 << FixItHint::CreateRemoval(Tok.getLocation());
1057 /// ParseIfStatement
1058 /// if-statement: [C99 6.8.4.1]
1059 /// 'if' '(' expression ')' statement
1060 /// 'if' '(' expression ')' statement 'else' statement
1061 /// [C++] 'if' '(' condition ')' statement
1062 /// [C++] 'if' '(' condition ')' statement 'else' statement
1064 StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) {
1065 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
1066 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
1068 if (Tok.isNot(tok::l_paren)) {
1069 Diag(Tok, diag::err_expected_lparen_after) << "if";
1070 SkipUntil(tok::semi);
1074 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
1076 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
1077 // the case for C90.
1080 // A name introduced by a declaration in a condition is in scope from its
1081 // point of declaration until the end of the substatements controlled by the
1084 // Names declared in the for-init-statement, and in the condition of if,
1085 // while, for, and switch statements are local to the if, while, for, or
1086 // switch statement (including the controlled statement).
1088 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
1090 // Parse the condition.
1092 Decl *CondVar = nullptr;
1093 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
1096 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get(), IfLoc));
1098 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
1099 // there is no compound stmt. C90 does not have this clause. We only do this
1100 // if the body isn't a compound statement to avoid push/pop in common cases.
1103 // The substatement in a selection-statement (each substatement, in the else
1104 // form of the if statement) implicitly defines a local scope.
1106 // For C++ we create a scope for the condition and a new scope for
1107 // substatements because:
1108 // -When the 'then' scope exits, we want the condition declaration to still be
1109 // active for the 'else' scope too.
1110 // -Sema will detect name clashes by considering declarations of a
1111 // 'ControlScope' as part of its direct subscope.
1112 // -If we wanted the condition and substatement to be in the same scope, we
1113 // would have to notify ParseStatement not to create a new scope. It's
1114 // simpler to let it create a new scope.
1116 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
1118 // Read the 'then' stmt.
1119 SourceLocation ThenStmtLoc = Tok.getLocation();
1121 SourceLocation InnerStatementTrailingElseLoc;
1122 StmtResult ThenStmt(ParseStatement(&InnerStatementTrailingElseLoc));
1124 // Pop the 'if' scope if needed.
1127 // If it has an else, parse it.
1128 SourceLocation ElseLoc;
1129 SourceLocation ElseStmtLoc;
1130 StmtResult ElseStmt;
1132 if (Tok.is(tok::kw_else)) {
1133 if (TrailingElseLoc)
1134 *TrailingElseLoc = Tok.getLocation();
1136 ElseLoc = ConsumeToken();
1137 ElseStmtLoc = Tok.getLocation();
1139 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
1140 // there is no compound stmt. C90 does not have this clause. We only do
1141 // this if the body isn't a compound statement to avoid push/pop in common
1145 // The substatement in a selection-statement (each substatement, in the else
1146 // form of the if statement) implicitly defines a local scope.
1148 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
1150 ElseStmt = ParseStatement();
1152 // Pop the 'else' scope if needed.
1154 } else if (Tok.is(tok::code_completion)) {
1155 Actions.CodeCompleteAfterIf(getCurScope());
1158 } else if (InnerStatementTrailingElseLoc.isValid()) {
1159 Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else);
1164 // If the then or else stmt is invalid and the other is valid (and present),
1165 // make turn the invalid one into a null stmt to avoid dropping the other
1166 // part. If both are invalid, return error.
1167 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
1168 (ThenStmt.isInvalid() && ElseStmt.get() == nullptr) ||
1169 (ThenStmt.get() == nullptr && ElseStmt.isInvalid())) {
1170 // Both invalid, or one is invalid and other is non-present: return error.
1174 // Now if either are invalid, replace with a ';'.
1175 if (ThenStmt.isInvalid())
1176 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
1177 if (ElseStmt.isInvalid())
1178 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
1180 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
1181 ElseLoc, ElseStmt.get());
1184 /// ParseSwitchStatement
1185 /// switch-statement:
1186 /// 'switch' '(' expression ')' statement
1187 /// [C++] 'switch' '(' condition ')' statement
1188 StmtResult Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc) {
1189 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
1190 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
1192 if (Tok.isNot(tok::l_paren)) {
1193 Diag(Tok, diag::err_expected_lparen_after) << "switch";
1194 SkipUntil(tok::semi);
1198 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
1200 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1201 // not the case for C90. Start the switch scope.
1204 // A name introduced by a declaration in a condition is in scope from its
1205 // point of declaration until the end of the substatements controlled by the
1208 // Names declared in the for-init-statement, and in the condition of if,
1209 // while, for, and switch statements are local to the if, while, for, or
1210 // switch statement (including the controlled statement).
1212 unsigned ScopeFlags = Scope::SwitchScope;
1214 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
1215 ParseScope SwitchScope(this, ScopeFlags);
1217 // Parse the condition.
1219 Decl *CondVar = nullptr;
1220 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
1224 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
1226 if (Switch.isInvalid()) {
1227 // Skip the switch body.
1228 // FIXME: This is not optimal recovery, but parsing the body is more
1229 // dangerous due to the presence of case and default statements, which
1230 // will have no place to connect back with the switch.
1231 if (Tok.is(tok::l_brace)) {
1233 SkipUntil(tok::r_brace);
1235 SkipUntil(tok::semi);
1239 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
1240 // there is no compound stmt. C90 does not have this clause. We only do this
1241 // if the body isn't a compound statement to avoid push/pop in common cases.
1244 // The substatement in a selection-statement (each substatement, in the else
1245 // form of the if statement) implicitly defines a local scope.
1247 // See comments in ParseIfStatement for why we create a scope for the
1248 // condition and a new scope for substatement in C++.
1250 getCurScope()->AddFlags(Scope::BreakScope);
1251 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
1253 // We have incremented the mangling number for the SwitchScope and the
1254 // InnerScope, which is one too many.
1256 getCurScope()->decrementMSLocalManglingNumber();
1258 // Read the body statement.
1259 StmtResult Body(ParseStatement(TrailingElseLoc));
1265 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
1268 /// ParseWhileStatement
1269 /// while-statement: [C99 6.8.5.1]
1270 /// 'while' '(' expression ')' statement
1271 /// [C++] 'while' '(' condition ')' statement
1272 StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) {
1273 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
1274 SourceLocation WhileLoc = Tok.getLocation();
1275 ConsumeToken(); // eat the 'while'.
1277 if (Tok.isNot(tok::l_paren)) {
1278 Diag(Tok, diag::err_expected_lparen_after) << "while";
1279 SkipUntil(tok::semi);
1283 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
1285 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1286 // the case for C90. Start the loop scope.
1289 // A name introduced by a declaration in a condition is in scope from its
1290 // point of declaration until the end of the substatements controlled by the
1293 // Names declared in the for-init-statement, and in the condition of if,
1294 // while, for, and switch statements are local to the if, while, for, or
1295 // switch statement (including the controlled statement).
1297 unsigned ScopeFlags;
1299 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1300 Scope::DeclScope | Scope::ControlScope;
1302 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1303 ParseScope WhileScope(this, ScopeFlags);
1305 // Parse the condition.
1307 Decl *CondVar = nullptr;
1308 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
1311 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get(), WhileLoc));
1313 // C99 6.8.5p5 - In C99, the body of the while statement is a scope, even if
1314 // there is no compound stmt. C90 does not have this clause. We only do this
1315 // if the body isn't a compound statement to avoid push/pop in common cases.
1318 // The substatement in an iteration-statement implicitly defines a local scope
1319 // which is entered and exited each time through the loop.
1321 // See comments in ParseIfStatement for why we create a scope for the
1322 // condition and a new scope for substatement in C++.
1324 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
1326 // Read the body statement.
1327 StmtResult Body(ParseStatement(TrailingElseLoc));
1329 // Pop the body scope if needed.
1333 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
1336 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
1339 /// ParseDoStatement
1340 /// do-statement: [C99 6.8.5.2]
1341 /// 'do' statement 'while' '(' expression ')' ';'
1342 /// Note: this lets the caller parse the end ';'.
1343 StmtResult Parser::ParseDoStatement() {
1344 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
1345 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
1347 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1348 // the case for C90. Start the loop scope.
1349 unsigned ScopeFlags;
1350 if (getLangOpts().C99)
1351 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
1353 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1355 ParseScope DoScope(this, ScopeFlags);
1357 // C99 6.8.5p5 - In C99, the body of the do statement is a scope, even if
1358 // there is no compound stmt. C90 does not have this clause. We only do this
1359 // if the body isn't a compound statement to avoid push/pop in common cases.
1362 // The substatement in an iteration-statement implicitly defines a local scope
1363 // which is entered and exited each time through the loop.
1365 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
1366 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
1368 // Read the body statement.
1369 StmtResult Body(ParseStatement());
1371 // Pop the body scope if needed.
1374 if (Tok.isNot(tok::kw_while)) {
1375 if (!Body.isInvalid()) {
1376 Diag(Tok, diag::err_expected_while);
1377 Diag(DoLoc, diag::note_matching) << "'do'";
1378 SkipUntil(tok::semi, StopBeforeMatch);
1382 SourceLocation WhileLoc = ConsumeToken();
1384 if (Tok.isNot(tok::l_paren)) {
1385 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
1386 SkipUntil(tok::semi, StopBeforeMatch);
1390 // Parse the parenthesized expression.
1391 BalancedDelimiterTracker T(*this, tok::l_paren);
1394 // A do-while expression is not a condition, so can't have attributes.
1395 DiagnoseAndSkipCXX11Attributes();
1397 ExprResult Cond = ParseExpression();
1401 if (Cond.isInvalid() || Body.isInvalid())
1404 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(),
1405 Cond.get(), T.getCloseLocation());
1408 bool Parser::isForRangeIdentifier() {
1409 assert(Tok.is(tok::identifier));
1411 const Token &Next = NextToken();
1412 if (Next.is(tok::colon))
1415 if (Next.is(tok::l_square) || Next.is(tok::kw_alignas)) {
1416 TentativeParsingAction PA(*this);
1418 SkipCXX11Attributes();
1419 bool Result = Tok.is(tok::colon);
1427 /// ParseForStatement
1428 /// for-statement: [C99 6.8.5.3]
1429 /// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1430 /// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
1431 /// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1433 /// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
1434 /// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1435 /// [OBJC2] 'for' '(' expr 'in' expr ')' statement
1437 /// [C++] for-init-statement:
1438 /// [C++] expression-statement
1439 /// [C++] simple-declaration
1441 /// [C++0x] for-range-declaration:
1442 /// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1443 /// [C++0x] for-range-initializer:
1444 /// [C++0x] expression
1445 /// [C++0x] braced-init-list [TODO]
1446 StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) {
1447 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
1448 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
1450 if (Tok.isNot(tok::l_paren)) {
1451 Diag(Tok, diag::err_expected_lparen_after) << "for";
1452 SkipUntil(tok::semi);
1456 bool C99orCXXorObjC = getLangOpts().C99 || getLangOpts().CPlusPlus ||
1457 getLangOpts().ObjC1;
1459 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1460 // the case for C90. Start the loop scope.
1463 // A name introduced by a declaration in a condition is in scope from its
1464 // point of declaration until the end of the substatements controlled by the
1467 // Names declared in the for-init-statement, and in the condition of if,
1468 // while, for, and switch statements are local to the if, while, for, or
1469 // switch statement (including the controlled statement).
1471 // Names declared in the for-init-statement are in the same declarative-region
1472 // as those declared in the condition.
1474 unsigned ScopeFlags = 0;
1476 ScopeFlags = Scope::DeclScope | Scope::ControlScope;
1478 ParseScope ForScope(this, ScopeFlags);
1480 BalancedDelimiterTracker T(*this, tok::l_paren);
1485 bool ForEach = false, ForRange = false;
1486 StmtResult FirstPart;
1487 bool SecondPartIsInvalid = false;
1488 FullExprArg SecondPart(Actions);
1489 ExprResult Collection;
1490 ForRangeInit ForRangeInit;
1491 FullExprArg ThirdPart(Actions);
1492 Decl *SecondVar = nullptr;
1494 if (Tok.is(tok::code_completion)) {
1495 Actions.CodeCompleteOrdinaryName(getCurScope(),
1496 C99orCXXorObjC? Sema::PCC_ForInit
1497 : Sema::PCC_Expression);
1502 ParsedAttributesWithRange attrs(AttrFactory);
1503 MaybeParseCXX11Attributes(attrs);
1505 // Parse the first part of the for specifier.
1506 if (Tok.is(tok::semi)) { // for (;
1507 ProhibitAttributes(attrs);
1508 // no first part, eat the ';'.
1510 } else if (getLangOpts().CPlusPlus && Tok.is(tok::identifier) &&
1511 isForRangeIdentifier()) {
1512 ProhibitAttributes(attrs);
1513 IdentifierInfo *Name = Tok.getIdentifierInfo();
1514 SourceLocation Loc = ConsumeToken();
1515 MaybeParseCXX11Attributes(attrs);
1517 ForRangeInit.ColonLoc = ConsumeToken();
1518 if (Tok.is(tok::l_brace))
1519 ForRangeInit.RangeExpr = ParseBraceInitializer();
1521 ForRangeInit.RangeExpr = ParseExpression();
1523 Diag(Loc, diag::err_for_range_identifier)
1524 << ((getLangOpts().CPlusPlus11 && !getLangOpts().CPlusPlus1z)
1525 ? FixItHint::CreateInsertion(Loc, "auto &&")
1528 FirstPart = Actions.ActOnCXXForRangeIdentifier(getCurScope(), Loc, Name,
1529 attrs, attrs.Range.getEnd());
1531 } else if (isForInitDeclaration()) { // for (int X = 4;
1532 // Parse declaration, which eats the ';'.
1533 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
1534 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
1536 // In C++0x, "for (T NS:a" might not be a typo for ::
1537 bool MightBeForRangeStmt = getLangOpts().CPlusPlus;
1538 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1540 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
1541 DeclGroupPtrTy DG = ParseSimpleDeclaration(
1542 Declarator::ForContext, DeclEnd, attrs, false,
1543 MightBeForRangeStmt ? &ForRangeInit : nullptr);
1544 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
1545 if (ForRangeInit.ParsedForRangeDecl()) {
1546 Diag(ForRangeInit.ColonLoc, getLangOpts().CPlusPlus11 ?
1547 diag::warn_cxx98_compat_for_range : diag::ext_for_range);
1550 } else if (Tok.is(tok::semi)) { // for (int x = 4;
1552 } else if ((ForEach = isTokIdentifier_in())) {
1553 Actions.ActOnForEachDeclStmt(DG);
1554 // ObjC: for (id x in expr)
1555 ConsumeToken(); // consume 'in'
1557 if (Tok.is(tok::code_completion)) {
1558 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
1562 Collection = ParseExpression();
1564 Diag(Tok, diag::err_expected_semi_for);
1567 ProhibitAttributes(attrs);
1568 Value = Actions.CorrectDelayedTyposInExpr(ParseExpression());
1570 ForEach = isTokIdentifier_in();
1572 // Turn the expression into a stmt.
1573 if (!Value.isInvalid()) {
1575 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1577 FirstPart = Actions.ActOnExprStmt(Value);
1580 if (Tok.is(tok::semi)) {
1582 } else if (ForEach) {
1583 ConsumeToken(); // consume 'in'
1585 if (Tok.is(tok::code_completion)) {
1586 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
1590 Collection = ParseExpression();
1591 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::colon) && FirstPart.get()) {
1592 // User tried to write the reasonable, but ill-formed, for-range-statement
1593 // for (expr : expr) { ... }
1594 Diag(Tok, diag::err_for_range_expected_decl)
1595 << FirstPart.get()->getSourceRange();
1596 SkipUntil(tok::r_paren, StopBeforeMatch);
1597 SecondPartIsInvalid = true;
1599 if (!Value.isInvalid()) {
1600 Diag(Tok, diag::err_expected_semi_for);
1602 // Skip until semicolon or rparen, don't consume it.
1603 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
1604 if (Tok.is(tok::semi))
1610 // Parse the second part of the for specifier.
1611 getCurScope()->AddFlags(Scope::BreakScope | Scope::ContinueScope);
1612 if (!ForEach && !ForRange) {
1613 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
1614 // Parse the second part of the for specifier.
1615 if (Tok.is(tok::semi)) { // for (...;;
1617 } else if (Tok.is(tok::r_paren)) {
1618 // missing both semicolons.
1621 if (getLangOpts().CPlusPlus)
1622 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1624 Second = ParseExpression();
1625 if (!Second.isInvalid())
1626 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
1629 SecondPartIsInvalid = Second.isInvalid();
1630 SecondPart = Actions.MakeFullExpr(Second.get(), ForLoc);
1633 if (Tok.isNot(tok::semi)) {
1634 if (!SecondPartIsInvalid || SecondVar)
1635 Diag(Tok, diag::err_expected_semi_for);
1637 // Skip until semicolon or rparen, don't consume it.
1638 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
1641 if (Tok.is(tok::semi)) {
1645 // Parse the third part of the for specifier.
1646 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
1647 ExprResult Third = ParseExpression();
1648 // FIXME: The C++11 standard doesn't actually say that this is a
1649 // discarded-value expression, but it clearly should be.
1650 ThirdPart = Actions.MakeFullDiscardedValueExpr(Third.get());
1656 // We need to perform most of the semantic analysis for a C++0x for-range
1657 // statememt before parsing the body, in order to be able to deduce the type
1658 // of an auto-typed loop variable.
1659 StmtResult ForRangeStmt;
1660 StmtResult ForEachStmt;
1663 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, FirstPart.get(),
1664 ForRangeInit.ColonLoc,
1665 ForRangeInit.RangeExpr.get(),
1666 T.getCloseLocation(),
1670 // Similarly, we need to do the semantic analysis for a for-range
1671 // statement immediately in order to close over temporaries correctly.
1672 } else if (ForEach) {
1673 ForEachStmt = Actions.ActOnObjCForCollectionStmt(ForLoc,
1676 T.getCloseLocation());
1679 // C99 6.8.5p5 - In C99, the body of the for statement is a scope, even if
1680 // there is no compound stmt. C90 does not have this clause. We only do this
1681 // if the body isn't a compound statement to avoid push/pop in common cases.
1684 // The substatement in an iteration-statement implicitly defines a local scope
1685 // which is entered and exited each time through the loop.
1687 // See comments in ParseIfStatement for why we create a scope for
1688 // for-init-statement/condition and a new scope for substatement in C++.
1690 ParseScope InnerScope(this, Scope::DeclScope, C99orCXXorObjC,
1691 Tok.is(tok::l_brace));
1693 // The body of the for loop has the same local mangling number as the
1694 // for-init-statement.
1695 // It will only be incremented if the body contains other things that would
1696 // normally increment the mangling number (like a compound statement).
1698 getCurScope()->decrementMSLocalManglingNumber();
1700 // Read the body statement.
1701 StmtResult Body(ParseStatement(TrailingElseLoc));
1703 // Pop the body scope if needed.
1706 // Leave the for-scope.
1709 if (Body.isInvalid())
1713 return Actions.FinishObjCForCollectionStmt(ForEachStmt.get(),
1717 return Actions.FinishCXXForRangeStmt(ForRangeStmt.get(), Body.get());
1719 return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.get(),
1720 SecondPart, SecondVar, ThirdPart,
1721 T.getCloseLocation(), Body.get());
1724 /// ParseGotoStatement
1726 /// 'goto' identifier ';'
1727 /// [GNU] 'goto' '*' expression ';'
1729 /// Note: this lets the caller parse the end ';'.
1731 StmtResult Parser::ParseGotoStatement() {
1732 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
1733 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
1736 if (Tok.is(tok::identifier)) {
1737 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1739 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
1741 } else if (Tok.is(tok::star)) {
1742 // GNU indirect goto extension.
1743 Diag(Tok, diag::ext_gnu_indirect_goto);
1744 SourceLocation StarLoc = ConsumeToken();
1745 ExprResult R(ParseExpression());
1746 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
1747 SkipUntil(tok::semi, StopBeforeMatch);
1750 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.get());
1752 Diag(Tok, diag::err_expected) << tok::identifier;
1759 /// ParseContinueStatement
1763 /// Note: this lets the caller parse the end ';'.
1765 StmtResult Parser::ParseContinueStatement() {
1766 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
1767 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
1770 /// ParseBreakStatement
1774 /// Note: this lets the caller parse the end ';'.
1776 StmtResult Parser::ParseBreakStatement() {
1777 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
1778 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
1781 /// ParseReturnStatement
1783 /// 'return' expression[opt] ';'
1784 StmtResult Parser::ParseReturnStatement() {
1785 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
1786 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
1789 if (Tok.isNot(tok::semi)) {
1790 if (Tok.is(tok::code_completion)) {
1791 Actions.CodeCompleteReturn(getCurScope());
1796 if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus) {
1797 R = ParseInitializer();
1799 Diag(R.get()->getLocStart(), getLangOpts().CPlusPlus11 ?
1800 diag::warn_cxx98_compat_generalized_initializer_lists :
1801 diag::ext_generalized_initializer_lists)
1802 << R.get()->getSourceRange();
1804 R = ParseExpression();
1805 if (R.isInvalid()) {
1806 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
1810 return Actions.ActOnReturnStmt(ReturnLoc, R.get(), getCurScope());
1813 StmtResult Parser::ParsePragmaLoopHint(StmtVector &Stmts, bool OnlyStatement,
1814 SourceLocation *TrailingElseLoc,
1815 ParsedAttributesWithRange &Attrs) {
1816 // Create temporary attribute list.
1817 ParsedAttributesWithRange TempAttrs(AttrFactory);
1819 // Get loop hints and consume annotated token.
1820 while (Tok.is(tok::annot_pragma_loop_hint)) {
1822 if (!HandlePragmaLoopHint(Hint))
1825 ArgsUnion ArgHints[] = {Hint.PragmaNameLoc, Hint.OptionLoc, Hint.StateLoc,
1826 ArgsUnion(Hint.ValueExpr)};
1827 TempAttrs.addNew(Hint.PragmaNameLoc->Ident, Hint.Range, nullptr,
1828 Hint.PragmaNameLoc->Loc, ArgHints, 4,
1829 AttributeList::AS_Pragma);
1832 // Get the next statement.
1833 MaybeParseCXX11Attributes(Attrs);
1835 StmtResult S = ParseStatementOrDeclarationAfterAttributes(
1836 Stmts, OnlyStatement, TrailingElseLoc, Attrs);
1838 Attrs.takeAllFrom(TempAttrs);
1842 Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
1843 assert(Tok.is(tok::l_brace));
1844 SourceLocation LBraceLoc = Tok.getLocation();
1846 if (SkipFunctionBodies && (!Decl || Actions.canSkipFunctionBody(Decl)) &&
1847 trySkippingFunctionBody()) {
1849 return Actions.ActOnSkippedFunctionBody(Decl);
1852 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1853 "parsing function body");
1855 // Do not enter a scope for the brace, as the arguments are in the same scope
1856 // (the function body) as the body itself. Instead, just read the statement
1857 // list and put it into a CompoundStmt for safe keeping.
1858 StmtResult FnBody(ParseCompoundStatementBody());
1860 // If the function body could not be parsed, make a bogus compoundstmt.
1861 if (FnBody.isInvalid()) {
1862 Sema::CompoundScopeRAII CompoundScope(Actions);
1863 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false);
1867 return Actions.ActOnFinishFunctionBody(Decl, FnBody.get());
1870 /// ParseFunctionTryBlock - Parse a C++ function-try-block.
1872 /// function-try-block:
1873 /// 'try' ctor-initializer[opt] compound-statement handler-seq
1875 Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
1876 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1877 SourceLocation TryLoc = ConsumeToken();
1879 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1880 "parsing function try block");
1882 // Constructor initializer list?
1883 if (Tok.is(tok::colon))
1884 ParseConstructorInitializer(Decl);
1886 Actions.ActOnDefaultCtorInitializers(Decl);
1888 if (SkipFunctionBodies && Actions.canSkipFunctionBody(Decl) &&
1889 trySkippingFunctionBody()) {
1891 return Actions.ActOnSkippedFunctionBody(Decl);
1894 SourceLocation LBraceLoc = Tok.getLocation();
1895 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc, /*FnTry*/true));
1896 // If we failed to parse the try-catch, we just give the function an empty
1897 // compound statement as the body.
1898 if (FnBody.isInvalid()) {
1899 Sema::CompoundScopeRAII CompoundScope(Actions);
1900 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false);
1904 return Actions.ActOnFinishFunctionBody(Decl, FnBody.get());
1907 bool Parser::trySkippingFunctionBody() {
1908 assert(Tok.is(tok::l_brace));
1909 assert(SkipFunctionBodies &&
1910 "Should only be called when SkipFunctionBodies is enabled");
1912 if (!PP.isCodeCompletionEnabled()) {
1914 SkipUntil(tok::r_brace);
1918 // We're in code-completion mode. Skip parsing for all function bodies unless
1919 // the body contains the code-completion point.
1920 TentativeParsingAction PA(*this);
1922 if (SkipUntil(tok::r_brace, StopAtCodeCompletion)) {
1931 /// ParseCXXTryBlock - Parse a C++ try-block.
1934 /// 'try' compound-statement handler-seq
1936 StmtResult Parser::ParseCXXTryBlock() {
1937 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1939 SourceLocation TryLoc = ConsumeToken();
1940 return ParseCXXTryBlockCommon(TryLoc);
1943 /// ParseCXXTryBlockCommon - Parse the common part of try-block and
1944 /// function-try-block.
1947 /// 'try' compound-statement handler-seq
1949 /// function-try-block:
1950 /// 'try' ctor-initializer[opt] compound-statement handler-seq
1953 /// handler handler-seq[opt]
1955 /// [Borland] try-block:
1956 /// 'try' compound-statement seh-except-block
1957 /// 'try' compound-statement seh-finally-block
1959 StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc, bool FnTry) {
1960 if (Tok.isNot(tok::l_brace))
1961 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
1963 StmtResult TryBlock(ParseCompoundStatement(/*isStmtExpr=*/false,
1964 Scope::DeclScope | Scope::TryScope |
1965 (FnTry ? Scope::FnTryCatchScope : 0)));
1966 if (TryBlock.isInvalid())
1969 // Borland allows SEH-handlers with 'try'
1971 if ((Tok.is(tok::identifier) &&
1972 Tok.getIdentifierInfo() == getSEHExceptKeyword()) ||
1973 Tok.is(tok::kw___finally)) {
1974 // TODO: Factor into common return ParseSEHHandlerCommon(...)
1976 if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
1977 SourceLocation Loc = ConsumeToken();
1978 Handler = ParseSEHExceptBlock(Loc);
1981 SourceLocation Loc = ConsumeToken();
1982 Handler = ParseSEHFinallyBlock(Loc);
1984 if(Handler.isInvalid())
1987 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
1993 StmtVector Handlers;
1995 // C++11 attributes can't appear here, despite this context seeming
1997 DiagnoseAndSkipCXX11Attributes();
1999 if (Tok.isNot(tok::kw_catch))
2000 return StmtError(Diag(Tok, diag::err_expected_catch));
2001 while (Tok.is(tok::kw_catch)) {
2002 StmtResult Handler(ParseCXXCatchBlock(FnTry));
2003 if (!Handler.isInvalid())
2004 Handlers.push_back(Handler.get());
2006 // Don't bother creating the full statement if we don't have any usable
2008 if (Handlers.empty())
2011 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.get(), Handlers);
2015 /// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
2018 /// 'catch' '(' exception-declaration ')' compound-statement
2020 /// exception-declaration:
2021 /// attribute-specifier-seq[opt] type-specifier-seq declarator
2022 /// attribute-specifier-seq[opt] type-specifier-seq abstract-declarator[opt]
2025 StmtResult Parser::ParseCXXCatchBlock(bool FnCatch) {
2026 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
2028 SourceLocation CatchLoc = ConsumeToken();
2030 BalancedDelimiterTracker T(*this, tok::l_paren);
2031 if (T.expectAndConsume())
2035 // The name in a catch exception-declaration is local to the handler and
2036 // shall not be redeclared in the outermost block of the handler.
2037 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope |
2038 (FnCatch ? Scope::FnTryCatchScope : 0));
2040 // exception-declaration is equivalent to '...' or a parameter-declaration
2041 // without default arguments.
2042 Decl *ExceptionDecl = nullptr;
2043 if (Tok.isNot(tok::ellipsis)) {
2044 ParsedAttributesWithRange Attributes(AttrFactory);
2045 MaybeParseCXX11Attributes(Attributes);
2047 DeclSpec DS(AttrFactory);
2048 DS.takeAttributesFrom(Attributes);
2050 if (ParseCXXTypeSpecifierSeq(DS))
2053 Declarator ExDecl(DS, Declarator::CXXCatchContext);
2054 ParseDeclarator(ExDecl);
2055 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
2060 if (T.getCloseLocation().isInvalid())
2063 if (Tok.isNot(tok::l_brace))
2064 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
2066 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
2067 StmtResult Block(ParseCompoundStatement());
2068 if (Block.isInvalid())
2071 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.get());
2074 void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
2075 IfExistsCondition Result;
2076 if (ParseMicrosoftIfExistsCondition(Result))
2079 // Handle dependent statements by parsing the braces as a compound statement.
2080 // This is not the same behavior as Visual C++, which don't treat this as a
2081 // compound statement, but for Clang's type checking we can't have anything
2082 // inside these braces escaping to the surrounding code.
2083 if (Result.Behavior == IEB_Dependent) {
2084 if (!Tok.is(tok::l_brace)) {
2085 Diag(Tok, diag::err_expected) << tok::l_brace;
2089 StmtResult Compound = ParseCompoundStatement();
2090 if (Compound.isInvalid())
2093 StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc,
2098 if (DepResult.isUsable())
2099 Stmts.push_back(DepResult.get());
2103 BalancedDelimiterTracker Braces(*this, tok::l_brace);
2104 if (Braces.consumeOpen()) {
2105 Diag(Tok, diag::err_expected) << tok::l_brace;
2109 switch (Result.Behavior) {
2111 // Parse the statements below.
2115 llvm_unreachable("Dependent case handled above");
2122 // Condition is true, parse the statements.
2123 while (Tok.isNot(tok::r_brace)) {
2124 StmtResult R = ParseStatementOrDeclaration(Stmts, false);
2126 Stmts.push_back(R.get());
2128 Braces.consumeClose();