blob: eaff9fe8eedf1a32c04710e54213389b37b25eed [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
Chris Lattner0ccd51e2006-08-09 05:47:47 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner0ccd51e2006-08-09 05:47:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
Aaron Ballmanb06b15a2014-06-06 12:40:24 +000015#include "clang/Basic/Attributes.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/Basic/PrettyStackTrace.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000017#include "clang/Parse/Parser.h"
Vassil Vassilev11ad3392017-03-23 15:11:07 +000018#include "clang/Parse/RAIIObjectsForParser.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/DeclSpec.h"
Aaron Ballmanb06b15a2014-06-06 12:40:24 +000020#include "clang/Sema/LoopHint.h"
John McCallfaf5fb42010-08-26 23:41:50 +000021#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall8b0666c2010-08-20 18:27:03 +000022#include "clang/Sema/Scope.h"
Richard Smith4f605af2012-08-18 00:55:03 +000023#include "clang/Sema/TypoCorrection.h"
Chris Lattner0ccd51e2006-08-09 05:47:47 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// C99 6.8: Statements and Blocks.
28//===----------------------------------------------------------------------===//
29
Richard Smith426a47b2013-10-28 22:04:30 +000030/// \brief Parse a standalone statement (for instance, as the body of an 'if',
31/// 'while', or 'for').
Alexey Bataevc4fad652016-01-13 11:18:54 +000032StmtResult Parser::ParseStatement(SourceLocation *TrailingElseLoc,
33 bool AllowOpenMPStandalone) {
Richard Smith426a47b2013-10-28 22:04:30 +000034 StmtResult Res;
35
36 // We may get back a null statement if we found a #pragma. Keep going until
37 // we get an actual statement.
38 do {
39 StmtVector Stmts;
Alexey Bataevc4fad652016-01-13 11:18:54 +000040 Res = ParseStatementOrDeclaration(
41 Stmts, AllowOpenMPStandalone ? ACK_StatementsOpenMPAnyExecutable
42 : ACK_StatementsOpenMPNonStandalone,
43 TrailingElseLoc);
Richard Smith426a47b2013-10-28 22:04:30 +000044 } while (!Res.isInvalid() && !Res.get());
45
46 return Res;
47}
48
Chris Lattner0ccd51e2006-08-09 05:47:47 +000049/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
50/// StatementOrDeclaration:
51/// statement
52/// declaration
53///
54/// statement:
55/// labeled-statement
56/// compound-statement
57/// expression-statement
58/// selection-statement
59/// iteration-statement
60/// jump-statement
Argyrios Kyrtzidisdee82912008-09-07 18:58:01 +000061/// [C++] declaration-statement
Sebastian Redlb219c902008-12-21 16:41:36 +000062/// [C++] try-block
John Wiegley1c0675e2011-04-28 01:08:34 +000063/// [MS] seh-try-block
Fariborz Jahanian90814572007-10-04 20:19:06 +000064/// [OBC] objc-throw-statement
65/// [OBC] objc-try-catch-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +000066/// [OBC] objc-synchronized-statement
Chris Lattner0116c472006-08-15 06:03:28 +000067/// [GNU] asm-statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +000068/// [OMP] openmp-construct [TODO]
69///
70/// labeled-statement:
71/// identifier ':' statement
72/// 'case' constant-expression ':' statement
73/// 'default' ':' statement
74///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000075/// selection-statement:
76/// if-statement
77/// switch-statement
78///
79/// iteration-statement:
80/// while-statement
81/// do-statement
82/// for-statement
83///
Chris Lattner9075bd72006-08-10 04:59:57 +000084/// expression-statement:
85/// expression[opt] ';'
86///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000087/// jump-statement:
88/// 'goto' identifier ';'
89/// 'continue' ';'
90/// 'break' ';'
91/// 'return' expression[opt] ';'
Chris Lattner503fadc2006-08-10 05:45:44 +000092/// [GNU] 'goto' '*' expression ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000093///
Fariborz Jahanian90814572007-10-04 20:19:06 +000094/// [OBC] objc-throw-statement:
95/// [OBC] '@' 'throw' expression ';'
Mike Stump11289f42009-09-09 15:08:12 +000096/// [OBC] '@' 'throw' ';'
97///
John McCalldadc5752010-08-24 06:29:42 +000098StmtResult
Alexey Bataevc4fad652016-01-13 11:18:54 +000099Parser::ParseStatementOrDeclaration(StmtVector &Stmts,
Jonathan Roelofsce1db6d2017-03-14 17:29:33 +0000100 AllowedConstructsKind Allowed,
Nico Weber3cef1082011-12-22 23:26:17 +0000101 SourceLocation *TrailingElseLoc) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000102
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000103 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000104
Richard Smithc202b282012-04-14 00:33:13 +0000105 ParsedAttributesWithRange Attrs(AttrFactory);
Craig Topper161e4db2014-05-21 06:02:52 +0000106 MaybeParseCXX11Attributes(Attrs, nullptr, /*MightBeObjCMessageSend*/ true);
Anastasia Stulova6bdbcbb2016-02-19 18:30:11 +0000107 if (!MaybeParseOpenCLUnrollHintAttribute(Attrs))
108 return StmtError();
Richard Smithc202b282012-04-14 00:33:13 +0000109
Alexey Bataevc4fad652016-01-13 11:18:54 +0000110 StmtResult Res = ParseStatementOrDeclarationAfterAttributes(
111 Stmts, Allowed, TrailingElseLoc, Attrs);
Richard Smithc202b282012-04-14 00:33:13 +0000112
113 assert((Attrs.empty() || Res.isInvalid() || Res.isUsable()) &&
114 "attributes on empty statement");
115
116 if (Attrs.empty() || Res.isInvalid())
117 return Res;
118
119 return Actions.ProcessStmtAttributes(Res.get(), Attrs.getList(), Attrs.Range);
120}
121
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000122namespace {
123class StatementFilterCCC : public CorrectionCandidateCallback {
124public:
125 StatementFilterCCC(Token nextTok) : NextToken(nextTok) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000126 WantTypeSpecifiers = nextTok.isOneOf(tok::l_paren, tok::less, tok::l_square,
127 tok::identifier, tok::star, tok::amp);
128 WantExpressionKeywords =
129 nextTok.isOneOf(tok::l_paren, tok::identifier, tok::arrow, tok::period);
130 WantRemainingKeywords =
131 nextTok.isOneOf(tok::l_paren, tok::semi, tok::identifier, tok::l_brace);
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000132 WantCXXNamedCasts = false;
133 }
134
Craig Topper2b07f022014-03-12 05:09:18 +0000135 bool ValidateCandidate(const TypoCorrection &candidate) override {
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000136 if (FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>())
Kaelyn Uhrain07e62722013-10-01 22:00:28 +0000137 return !candidate.getCorrectionSpecifier() || isa<ObjCIvarDecl>(FD);
Kaelyn Uhrain46b6cdc2013-09-27 19:40:16 +0000138 if (NextToken.is(tok::equal))
139 return candidate.getCorrectionDeclAs<VarDecl>();
Kaelyn Uhrain30943ce2013-09-27 23:54:23 +0000140 if (NextToken.is(tok::period) &&
141 candidate.getCorrectionDeclAs<NamespaceDecl>())
142 return false;
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000143 return CorrectionCandidateCallback::ValidateCandidate(candidate);
144 }
145
146private:
147 Token NextToken;
148};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000149}
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000150
Richard Smithc202b282012-04-14 00:33:13 +0000151StmtResult
152Parser::ParseStatementOrDeclarationAfterAttributes(StmtVector &Stmts,
Jonathan Roelofsce1db6d2017-03-14 17:29:33 +0000153 AllowedConstructsKind Allowed, SourceLocation *TrailingElseLoc,
Richard Smithc202b282012-04-14 00:33:13 +0000154 ParsedAttributesWithRange &Attrs) {
Craig Topper161e4db2014-05-21 06:02:52 +0000155 const char *SemiError = nullptr;
Richard Smithc202b282012-04-14 00:33:13 +0000156 StmtResult Res;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000157
Chris Lattner503fadc2006-08-10 05:45:44 +0000158 // Cases in this switch statement should fall through if the parser expects
159 // the token to end in a semicolon (in which case SemiError should be set),
160 // or they directly 'return;' if not.
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000161Retry:
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000162 tok::TokenKind Kind = Tok.getKind();
163 SourceLocation AtLoc;
164 switch (Kind) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000165 case tok::at: // May be a @try or @throw statement
166 {
Richard Smithc202b282012-04-14 00:33:13 +0000167 ProhibitAttributes(Attrs); // TODO: is it correct?
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000168 AtLoc = ConsumeToken(); // consume @
Sebastian Redlbab9a4b2008-12-11 20:12:42 +0000169 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000170 }
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000171
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000172 case tok::code_completion:
John McCallfaf5fb42010-08-26 23:41:50 +0000173 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000174 cutOffParsing();
175 return StmtError();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000176
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000177 case tok::identifier: {
178 Token Next = NextToken();
179 if (Next.is(tok::colon)) { // C99 6.8.1: labeled-statement
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000180 // identifier ':' statement
Richard Smithc202b282012-04-14 00:33:13 +0000181 return ParseLabeledStatement(Attrs);
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000182 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000183
Richard Smith4f605af2012-08-18 00:55:03 +0000184 // Look up the identifier, and typo-correct it to a keyword if it's not
185 // found.
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000186 if (Next.isNot(tok::coloncolon)) {
Richard Smith4f605af2012-08-18 00:55:03 +0000187 // Try to limit which sets of keywords should be included in typo
188 // correction based on what the next token is.
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000189 if (TryAnnotateName(/*IsAddressOfOperand*/ false,
190 llvm::make_unique<StatementFilterCCC>(Next)) ==
191 ANK_Error) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000192 // Handle errors here by skipping up to the next semicolon or '}', and
193 // eat the semicolon if that's what stopped us.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000194 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000195 if (Tok.is(tok::semi))
196 ConsumeToken();
197 return StmtError();
Richard Smith4f605af2012-08-18 00:55:03 +0000198 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000199
Richard Smith4f605af2012-08-18 00:55:03 +0000200 // If the identifier was typo-corrected, try again.
201 if (Tok.isNot(tok::identifier))
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000202 goto Retry;
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000203 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000204
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000205 // Fall through
206 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000207
Chris Lattner803802d2009-03-24 17:04:48 +0000208 default: {
David Majnemer6ac7dd12016-08-01 16:39:29 +0000209 if ((getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt ||
210 Allowed == ACK_Any) &&
Alexey Bataevc4fad652016-01-13 11:18:54 +0000211 isDeclarationStatement()) {
Chris Lattner49836b42009-04-02 04:16:50 +0000212 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Rafael Espindola1bd906d2014-10-22 14:27:08 +0000213 DeclGroupPtrTy Decl = ParseDeclaration(Declarator::BlockContext,
Richard Smithc202b282012-04-14 00:33:13 +0000214 DeclEnd, Attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000215 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattner803802d2009-03-24 17:04:48 +0000216 }
217
218 if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +0000219 Diag(Tok, diag::err_expected_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000220 return StmtError();
Chris Lattnerf8afb622006-08-10 18:26:31 +0000221 }
Mike Stump11289f42009-09-09 15:08:12 +0000222
Richard Smithc202b282012-04-14 00:33:13 +0000223 return ParseExprStatement();
Chris Lattner803802d2009-03-24 17:04:48 +0000224 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000225
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000226 case tok::kw_case: // C99 6.8.1: labeled-statement
Richard Smithc202b282012-04-14 00:33:13 +0000227 return ParseCaseStatement();
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000228 case tok::kw_default: // C99 6.8.1: labeled-statement
Richard Smithc202b282012-04-14 00:33:13 +0000229 return ParseDefaultStatement();
Sebastian Redl042ad952008-12-11 19:30:53 +0000230
Chris Lattner9075bd72006-08-10 04:59:57 +0000231 case tok::l_brace: // C99 6.8.2: compound-statement
Richard Smithc202b282012-04-14 00:33:13 +0000232 return ParseCompoundStatement();
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000233 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
Argyrios Kyrtzidis43ea78b2011-09-01 21:53:45 +0000234 bool HasLeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
235 return Actions.ActOnNullStmt(ConsumeToken(), HasLeadingEmptyMacro);
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000236 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000237
Chris Lattner9075bd72006-08-10 04:59:57 +0000238 case tok::kw_if: // C99 6.8.4.1: if-statement
Richard Smithc202b282012-04-14 00:33:13 +0000239 return ParseIfStatement(TrailingElseLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000240 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Richard Smithc202b282012-04-14 00:33:13 +0000241 return ParseSwitchStatement(TrailingElseLoc);
Sebastian Redl042ad952008-12-11 19:30:53 +0000242
Chris Lattner9075bd72006-08-10 04:59:57 +0000243 case tok::kw_while: // C99 6.8.5.1: while-statement
Richard Smithc202b282012-04-14 00:33:13 +0000244 return ParseWhileStatement(TrailingElseLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000245 case tok::kw_do: // C99 6.8.5.2: do-statement
Richard Smithc202b282012-04-14 00:33:13 +0000246 Res = ParseDoStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000247 SemiError = "do/while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000248 break;
249 case tok::kw_for: // C99 6.8.5.3: for-statement
Richard Smithc202b282012-04-14 00:33:13 +0000250 return ParseForStatement(TrailingElseLoc);
Chris Lattner503fadc2006-08-10 05:45:44 +0000251
252 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Richard Smithc202b282012-04-14 00:33:13 +0000253 Res = ParseGotoStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000254 SemiError = "goto";
Chris Lattner9075bd72006-08-10 04:59:57 +0000255 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000256 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Richard Smithc202b282012-04-14 00:33:13 +0000257 Res = ParseContinueStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000258 SemiError = "continue";
Chris Lattner503fadc2006-08-10 05:45:44 +0000259 break;
260 case tok::kw_break: // C99 6.8.6.3: break-statement
Richard Smithc202b282012-04-14 00:33:13 +0000261 Res = ParseBreakStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000262 SemiError = "break";
Chris Lattner503fadc2006-08-10 05:45:44 +0000263 break;
264 case tok::kw_return: // C99 6.8.6.4: return-statement
Richard Smithc202b282012-04-14 00:33:13 +0000265 Res = ParseReturnStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000266 SemiError = "return";
Chris Lattner503fadc2006-08-10 05:45:44 +0000267 break;
Richard Smith0e304ea2015-10-22 04:46:14 +0000268 case tok::kw_co_return: // C++ Coroutines: co_return statement
269 Res = ParseReturnStatement();
270 SemiError = "co_return";
271 break;
Sebastian Redl042ad952008-12-11 19:30:53 +0000272
Sebastian Redlb219c902008-12-21 16:41:36 +0000273 case tok::kw_asm: {
Richard Smithc202b282012-04-14 00:33:13 +0000274 ProhibitAttributes(Attrs);
Steve Naroffb2c80c72008-02-07 03:50:06 +0000275 bool msAsm = false;
276 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidis3050d9b2010-11-02 02:33:08 +0000277 Res = Actions.ActOnFinishFullStmt(Res.get());
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000278 if (msAsm) return Res;
Chris Lattner34a95662009-06-14 00:07:48 +0000279 SemiError = "asm";
Chris Lattner0116c472006-08-15 06:03:28 +0000280 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000281 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000282
Reid Kleckner6d8d22a2014-06-25 00:28:35 +0000283 case tok::kw___if_exists:
284 case tok::kw___if_not_exists:
285 ProhibitAttributes(Attrs);
286 ParseMicrosoftIfExistsStatement(Stmts);
287 // An __if_exists block is like a compound statement, but it doesn't create
288 // a new scope.
289 return StmtEmpty();
290
Sebastian Redlb219c902008-12-21 16:41:36 +0000291 case tok::kw_try: // C++ 15: try-block
Richard Smithc202b282012-04-14 00:33:13 +0000292 return ParseCXXTryBlock();
John Wiegley1c0675e2011-04-28 01:08:34 +0000293
294 case tok::kw___try:
Richard Smithc202b282012-04-14 00:33:13 +0000295 ProhibitAttributes(Attrs); // TODO: is it correct?
296 return ParseSEHTryBlock();
Eli Friedmanec52f922012-02-23 23:47:16 +0000297
Nico Weberc7d05962014-07-06 22:32:59 +0000298 case tok::kw___leave:
299 Res = ParseSEHLeaveStatement();
300 SemiError = "__leave";
301 break;
302
Eli Friedmanec52f922012-02-23 23:47:16 +0000303 case tok::annot_pragma_vis:
Richard Smithc202b282012-04-14 00:33:13 +0000304 ProhibitAttributes(Attrs);
Eli Friedmanec52f922012-02-23 23:47:16 +0000305 HandlePragmaVisibility();
306 return StmtEmpty();
307
308 case tok::annot_pragma_pack:
Richard Smithc202b282012-04-14 00:33:13 +0000309 ProhibitAttributes(Attrs);
Eli Friedmanec52f922012-02-23 23:47:16 +0000310 HandlePragmaPack();
311 return StmtEmpty();
Eli Friedman68be1642012-10-04 02:36:51 +0000312
Eli Friedmanbbbbac62012-10-09 22:46:54 +0000313 case tok::annot_pragma_msstruct:
314 ProhibitAttributes(Attrs);
315 HandlePragmaMSStruct();
316 return StmtEmpty();
317
Eli Friedmanae8ee252012-10-08 23:52:38 +0000318 case tok::annot_pragma_align:
319 ProhibitAttributes(Attrs);
320 HandlePragmaAlign();
321 return StmtEmpty();
322
Eli Friedmanbbbbac62012-10-09 22:46:54 +0000323 case tok::annot_pragma_weak:
324 ProhibitAttributes(Attrs);
325 HandlePragmaWeak();
326 return StmtEmpty();
327
328 case tok::annot_pragma_weakalias:
329 ProhibitAttributes(Attrs);
330 HandlePragmaWeakAlias();
331 return StmtEmpty();
332
333 case tok::annot_pragma_redefine_extname:
334 ProhibitAttributes(Attrs);
335 HandlePragmaRedefineExtname();
336 return StmtEmpty();
337
Eli Friedman68be1642012-10-04 02:36:51 +0000338 case tok::annot_pragma_fp_contract:
Richard Smithca9b0b62013-11-15 21:10:54 +0000339 ProhibitAttributes(Attrs);
Lang Hamesa930e712012-10-21 01:10:01 +0000340 Diag(Tok, diag::err_pragma_fp_contract_scope);
341 ConsumeToken();
342 return StmtError();
343
Adam Nemet60d32642017-04-04 21:18:36 +0000344 case tok::annot_pragma_fp:
345 ProhibitAttributes(Attrs);
346 Diag(Tok, diag::err_pragma_fp_scope);
347 ConsumeToken();
348 return StmtError();
349
Eli Friedman68be1642012-10-04 02:36:51 +0000350 case tok::annot_pragma_opencl_extension:
351 ProhibitAttributes(Attrs);
352 HandlePragmaOpenCLExtension();
353 return StmtEmpty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000354
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000355 case tok::annot_pragma_captured:
Richard Smith12a41bd2013-09-16 21:17:44 +0000356 ProhibitAttributes(Attrs);
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000357 return HandlePragmaCaptured();
358
Alexey Bataeva769e072013-03-22 06:34:35 +0000359 case tok::annot_pragma_openmp:
Richard Smith12a41bd2013-09-16 21:17:44 +0000360 ProhibitAttributes(Attrs);
Alexey Bataevc4fad652016-01-13 11:18:54 +0000361 return ParseOpenMPDeclarativeOrExecutableDirective(Allowed);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000362
David Majnemer4bb09802014-02-10 19:50:15 +0000363 case tok::annot_pragma_ms_pointers_to_members:
364 ProhibitAttributes(Attrs);
365 HandlePragmaMSPointersToMembers();
366 return StmtEmpty();
367
Warren Huntc3b18962014-04-08 22:30:47 +0000368 case tok::annot_pragma_ms_pragma:
369 ProhibitAttributes(Attrs);
370 HandlePragmaMSPragma();
371 return StmtEmpty();
Aaron Ballmanb06b15a2014-06-06 12:40:24 +0000372
Alexey Bataev3d42f342015-11-20 07:02:57 +0000373 case tok::annot_pragma_ms_vtordisp:
374 ProhibitAttributes(Attrs);
375 HandlePragmaMSVtorDisp();
376 return StmtEmpty();
377
Aaron Ballmanb06b15a2014-06-06 12:40:24 +0000378 case tok::annot_pragma_loop_hint:
379 ProhibitAttributes(Attrs);
Alexey Bataevc4fad652016-01-13 11:18:54 +0000380 return ParsePragmaLoopHint(Stmts, Allowed, TrailingElseLoc, Attrs);
Richard Smithba3a4f92016-01-12 21:59:26 +0000381
382 case tok::annot_pragma_dump:
383 HandlePragmaDump();
384 return StmtEmpty();
Sebastian Redlb219c902008-12-21 16:41:36 +0000385 }
386
Chris Lattner503fadc2006-08-10 05:45:44 +0000387 // If we reached this code, the statement must end in a semicolon.
Alp Toker97650562014-01-10 11:19:30 +0000388 if (!TryConsumeToken(tok::semi) && !Res.isInvalid()) {
Chris Lattner8e3eed02009-06-14 00:23:56 +0000389 // If the result was valid, then we do want to diagnose this. Use
390 // ExpectAndConsume to emit the diagnostic, even though we know it won't
391 // succeed.
392 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner0046de12008-11-13 18:52:53 +0000393 // Skip until we see a } or ;, but don't eat it.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000394 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner503fadc2006-08-10 05:45:44 +0000395 }
Mike Stump11289f42009-09-09 15:08:12 +0000396
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000397 return Res;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000398}
399
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000400/// \brief Parse an expression statement.
Richard Smithc202b282012-04-14 00:33:13 +0000401StmtResult Parser::ParseExprStatement() {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000402 // If a case keyword is missing, this is where it should be inserted.
403 Token OldToken = Tok;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000404
Alex Lorenzf0b4e5d2016-10-18 10:55:01 +0000405 ExprStatementTokLoc = Tok.getLocation();
406
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000407 // expression[opt] ';'
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000408 ExprResult Expr(ParseExpression());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000409 if (Expr.isInvalid()) {
410 // If the expression is invalid, skip ahead to the next semicolon or '}'.
411 // Not doing this opens us up to the possibility of infinite loops if
412 // ParseExpression does not consume any tokens.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000413 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000414 if (Tok.is(tok::semi))
415 ConsumeToken();
John McCalleaef89b2013-03-22 02:10:40 +0000416 return Actions.ActOnExprStmtError();
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000417 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000418
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000419 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
420 Actions.CheckCaseExpression(Expr.get())) {
421 // If a constant expression is followed by a colon inside a switch block,
422 // suggest a missing case keyword.
423 Diag(OldToken, diag::err_expected_case_before_expression)
424 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000425
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000426 // Recover parsing as a case statement.
Richard Smithc202b282012-04-14 00:33:13 +0000427 return ParseCaseStatement(/*MissingCase=*/true, Expr);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000428 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000429
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000430 // Otherwise, eat the semicolon.
431 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +0000432 return Actions.ActOnExprStmt(Expr);
John Wiegley1c0675e2011-04-28 01:08:34 +0000433}
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000434
John Wiegley1c0675e2011-04-28 01:08:34 +0000435/// ParseSEHTryBlockCommon
436///
437/// seh-try-block:
438/// '__try' compound-statement seh-handler
439///
440/// seh-handler:
441/// seh-except-block
442/// seh-finally-block
443///
Nico Weberdd256742015-02-25 01:43:27 +0000444StmtResult Parser::ParseSEHTryBlock() {
445 assert(Tok.is(tok::kw___try) && "Expected '__try'");
446 SourceLocation TryLoc = ConsumeToken();
447
Nico Weberfc3fe4f2015-02-25 02:22:06 +0000448 if (Tok.isNot(tok::l_brace))
Alp Tokerec543272013-12-24 09:48:30 +0000449 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
John Wiegley1c0675e2011-04-28 01:08:34 +0000450
Warren Huntf6be4cb2014-07-25 20:52:51 +0000451 StmtResult TryBlock(ParseCompoundStatement(/*isStmtExpr=*/false,
452 Scope::DeclScope | Scope::SEHTryScope));
John Wiegley1c0675e2011-04-28 01:08:34 +0000453 if(TryBlock.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000454 return TryBlock;
John Wiegley1c0675e2011-04-28 01:08:34 +0000455
456 StmtResult Handler;
Richard Smithc202b282012-04-14 00:33:13 +0000457 if (Tok.is(tok::identifier) &&
Douglas Gregor60060d62011-10-21 03:57:52 +0000458 Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +0000459 SourceLocation Loc = ConsumeToken();
460 Handler = ParseSEHExceptBlock(Loc);
461 } else if (Tok.is(tok::kw___finally)) {
462 SourceLocation Loc = ConsumeToken();
463 Handler = ParseSEHFinallyBlock(Loc);
464 } else {
Nico Weberfc3fe4f2015-02-25 02:22:06 +0000465 return StmtError(Diag(Tok, diag::err_seh_expected_handler));
John Wiegley1c0675e2011-04-28 01:08:34 +0000466 }
467
468 if(Handler.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000469 return Handler;
John Wiegley1c0675e2011-04-28 01:08:34 +0000470
471 return Actions.ActOnSEHTryBlock(false /* IsCXXTry */,
472 TryLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000473 TryBlock.get(),
Warren Huntf6be4cb2014-07-25 20:52:51 +0000474 Handler.get());
John Wiegley1c0675e2011-04-28 01:08:34 +0000475}
476
477/// ParseSEHExceptBlock - Handle __except
478///
479/// seh-except-block:
480/// '__except' '(' seh-filter-expression ')' compound-statement
481///
482StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) {
483 PoisonIdentifierRAIIObject raii(Ident__exception_code, false),
484 raii2(Ident___exception_code, false),
485 raii3(Ident_GetExceptionCode, false);
486
Alp Toker383d2c42014-01-01 03:08:43 +0000487 if (ExpectAndConsume(tok::l_paren))
John Wiegley1c0675e2011-04-28 01:08:34 +0000488 return StmtError();
489
Reid Kleckner1d59f992015-01-22 01:36:17 +0000490 ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope |
491 Scope::SEHExceptScope);
John Wiegley1c0675e2011-04-28 01:08:34 +0000492
David Blaikiebbafb8a2012-03-11 07:00:24 +0000493 if (getLangOpts().Borland) {
Francois Pichetbfaf4772011-04-28 03:14:31 +0000494 Ident__exception_info->setIsPoisoned(false);
495 Ident___exception_info->setIsPoisoned(false);
496 Ident_GetExceptionInfo->setIsPoisoned(false);
497 }
Reid Kleckner1d59f992015-01-22 01:36:17 +0000498
499 ExprResult FilterExpr;
500 {
501 ParseScopeFlags FilterScope(this, getCurScope()->getFlags() |
502 Scope::SEHFilterScope);
Reid Kleckner85368fb2015-04-02 22:09:32 +0000503 FilterExpr = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Reid Kleckner1d59f992015-01-22 01:36:17 +0000504 }
Francois Pichetbfaf4772011-04-28 03:14:31 +0000505
David Blaikiebbafb8a2012-03-11 07:00:24 +0000506 if (getLangOpts().Borland) {
Francois Pichetbfaf4772011-04-28 03:14:31 +0000507 Ident__exception_info->setIsPoisoned(true);
508 Ident___exception_info->setIsPoisoned(true);
509 Ident_GetExceptionInfo->setIsPoisoned(true);
510 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000511
512 if(FilterExpr.isInvalid())
513 return StmtError();
514
Alp Toker383d2c42014-01-01 03:08:43 +0000515 if (ExpectAndConsume(tok::r_paren))
John Wiegley1c0675e2011-04-28 01:08:34 +0000516 return StmtError();
517
Nico Weberfc3fe4f2015-02-25 02:22:06 +0000518 if (Tok.isNot(tok::l_brace))
519 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
520
Richard Smithc202b282012-04-14 00:33:13 +0000521 StmtResult Block(ParseCompoundStatement());
John Wiegley1c0675e2011-04-28 01:08:34 +0000522
523 if(Block.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000524 return Block;
John Wiegley1c0675e2011-04-28 01:08:34 +0000525
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000526 return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.get(), Block.get());
John Wiegley1c0675e2011-04-28 01:08:34 +0000527}
528
529/// ParseSEHFinallyBlock - Handle __finally
530///
531/// seh-finally-block:
532/// '__finally' compound-statement
533///
Nico Weberd64657f2015-03-09 02:47:59 +0000534StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyLoc) {
John Wiegley1c0675e2011-04-28 01:08:34 +0000535 PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
536 raii2(Ident___abnormal_termination, false),
537 raii3(Ident_AbnormalTermination, false);
538
Nico Weberfc3fe4f2015-02-25 02:22:06 +0000539 if (Tok.isNot(tok::l_brace))
540 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
541
Nico Weberd64657f2015-03-09 02:47:59 +0000542 ParseScope FinallyScope(this, 0);
543 Actions.ActOnStartSEHFinallyBlock();
544
Richard Smithc202b282012-04-14 00:33:13 +0000545 StmtResult Block(ParseCompoundStatement());
Nico Weberce903292015-03-09 03:17:15 +0000546 if(Block.isInvalid()) {
547 Actions.ActOnAbortSEHFinallyBlock();
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000548 return Block;
Nico Weberce903292015-03-09 03:17:15 +0000549 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000550
Nico Weberd64657f2015-03-09 02:47:59 +0000551 return Actions.ActOnFinishSEHFinallyBlock(FinallyLoc, Block.get());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000552}
553
Nico Weberc7d05962014-07-06 22:32:59 +0000554/// Handle __leave
555///
556/// seh-leave-statement:
557/// '__leave' ';'
558///
559StmtResult Parser::ParseSEHLeaveStatement() {
560 SourceLocation LeaveLoc = ConsumeToken(); // eat the '__leave'.
561 return Actions.ActOnSEHLeaveStmt(LeaveLoc, getCurScope());
562}
563
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000564/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000565///
566/// labeled-statement:
567/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000568/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000569///
Richard Smithc202b282012-04-14 00:33:13 +0000570StmtResult Parser::ParseLabeledStatement(ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000571 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
572 "Not an identifier!");
573
574 Token IdentTok = Tok; // Save the whole token.
575 ConsumeToken(); // eat the identifier.
576
577 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000578
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000579 // identifier ':' statement
580 SourceLocation ColonLoc = ConsumeToken();
581
Richard Smitha3e01cf2013-11-15 22:45:29 +0000582 // Read label attributes, if present.
583 StmtResult SubStmt;
584 if (Tok.is(tok::kw___attribute)) {
585 ParsedAttributesWithRange TempAttrs(AttrFactory);
586 ParseGNUAttributes(TempAttrs);
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000587
Richard Smitha3e01cf2013-11-15 22:45:29 +0000588 // In C++, GNU attributes only apply to the label if they are followed by a
589 // semicolon, to disambiguate label attributes from attributes on a labeled
590 // declaration.
591 //
592 // This doesn't quite match what GCC does; if the attribute list is empty
593 // and followed by a semicolon, GCC will reject (it appears to parse the
594 // attributes as part of a statement in that case). That looks like a bug.
595 if (!getLangOpts().CPlusPlus || Tok.is(tok::semi))
596 attrs.takeAllFrom(TempAttrs);
597 else if (isDeclarationStatement()) {
598 StmtVector Stmts;
599 // FIXME: We should do this whether or not we have a declaration
600 // statement, but that doesn't work correctly (because ProhibitAttributes
601 // can't handle GNU attributes), so only call it in the one case where
602 // GNU attributes are allowed.
603 SubStmt = ParseStatementOrDeclarationAfterAttributes(
Alexey Bataevc4fad652016-01-13 11:18:54 +0000604 Stmts, /*Allowed=*/ACK_StatementsOpenMPNonStandalone, nullptr,
605 TempAttrs);
Richard Smitha3e01cf2013-11-15 22:45:29 +0000606 if (!TempAttrs.empty() && !SubStmt.isInvalid())
607 SubStmt = Actions.ProcessStmtAttributes(
608 SubStmt.get(), TempAttrs.getList(), TempAttrs.Range);
609 } else {
Alp Toker383d2c42014-01-01 03:08:43 +0000610 Diag(Tok, diag::err_expected_after) << "__attribute__" << tok::semi;
Richard Smitha3e01cf2013-11-15 22:45:29 +0000611 }
612 }
613
614 // If we've not parsed a statement yet, parse one now.
615 if (!SubStmt.isInvalid() && !SubStmt.isUsable())
616 SubStmt = ParseStatement();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000617
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000618 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000619 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000620 SubStmt = Actions.ActOnNullStmt(ColonLoc);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000621
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000622 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
623 IdentTok.getLocation());
Richard Smithc202b282012-04-14 00:33:13 +0000624 if (AttributeList *Attrs = attrs.getList()) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000625 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
Richard Smithc202b282012-04-14 00:33:13 +0000626 attrs.clear();
627 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000628
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000629 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
630 SubStmt.get());
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000631}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000632
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000633/// ParseCaseStatement
634/// labeled-statement:
635/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000636/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000637///
Richard Smithc202b282012-04-14 00:33:13 +0000638StmtResult Parser::ParseCaseStatement(bool MissingCase, ExprResult Expr) {
Richard Smithd4257d82011-04-21 22:48:40 +0000639 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Mike Stump11289f42009-09-09 15:08:12 +0000640
Chris Lattner34a22092009-03-04 04:23:07 +0000641 // It is very very common for code to contain many case statements recursively
642 // nested, as in (but usually without indentation):
643 // case 1:
644 // case 2:
645 // case 3:
646 // case 4:
647 // case 5: etc.
648 //
649 // Parsing this naively works, but is both inefficient and can cause us to run
650 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner2b19a6582009-03-04 18:24:58 +0000651 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner34a22092009-03-04 04:23:07 +0000652 // but all the grossness is constrained to ParseCaseStatement (and some
Richard Smitha3e01cf2013-11-15 22:45:29 +0000653 // weirdness in the actions), so this is just local grossness :).
Mike Stump11289f42009-09-09 15:08:12 +0000654
Chris Lattner34a22092009-03-04 04:23:07 +0000655 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
656 // example above.
John McCalldadc5752010-08-24 06:29:42 +0000657 StmtResult TopLevelCase(true);
Mike Stump11289f42009-09-09 15:08:12 +0000658
Chris Lattner34a22092009-03-04 04:23:07 +0000659 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
660 // gets updated each time a new case is parsed, and whose body is unset so
661 // far. When parsing 'case 4', this is the 'case 3' node.
Craig Topper161e4db2014-05-21 06:02:52 +0000662 Stmt *DeepestParsedCaseStmt = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000663
Chris Lattner34a22092009-03-04 04:23:07 +0000664 // While we have case statements, eat and stack them.
David Majnemer0ac67fa2011-06-13 05:50:12 +0000665 SourceLocation ColonLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000666 do {
Richard Trieu2c850c02011-04-21 21:44:26 +0000667 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
668 ConsumeToken(); // eat the 'case'.
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000669 ColonLoc = SourceLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000670
Douglas Gregord328d572009-09-21 18:10:23 +0000671 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000672 Actions.CodeCompleteCase(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000673 cutOffParsing();
674 return StmtError();
Douglas Gregord328d572009-09-21 18:10:23 +0000675 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000676
Chris Lattner125c0ee2009-12-10 00:38:54 +0000677 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
678 /// Disable this form of error recovery while we're parsing the case
679 /// expression.
680 ColonProtectionRAIIObject ColonProtection(*this);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000681
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000682 ExprResult LHS;
683 if (!MissingCase) {
684 LHS = ParseConstantExpression();
Kaelyn Takatab16e6322014-11-20 22:06:40 +0000685 if (!getLangOpts().CPlusPlus11) {
686 LHS = Actions.CorrectDelayedTyposInExpr(LHS, [this](class Expr *E) {
687 return Actions.VerifyIntegerConstantExpression(E);
688 });
689 }
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000690 if (LHS.isInvalid()) {
691 // If constant-expression is parsed unsuccessfully, recover by skipping
692 // current case statement (moving to the colon that ends it).
693 if (SkipUntil(tok::colon, tok::r_brace, StopAtSemi | StopBeforeMatch)) {
694 TryConsumeToken(tok::colon, ColonLoc);
695 continue;
696 }
697 return StmtError();
698 }
699 } else {
700 LHS = Expr;
701 MissingCase = false;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000702 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000703
Chris Lattner34a22092009-03-04 04:23:07 +0000704 // GNU case range extension.
705 SourceLocation DotDotDotLoc;
John McCalldadc5752010-08-24 06:29:42 +0000706 ExprResult RHS;
Alp Tokerec543272013-12-24 09:48:30 +0000707 if (TryConsumeToken(tok::ellipsis, DotDotDotLoc)) {
708 Diag(DotDotDotLoc, diag::ext_gnu_case_range);
Chris Lattner34a22092009-03-04 04:23:07 +0000709 RHS = ParseConstantExpression();
710 if (RHS.isInvalid()) {
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000711 if (SkipUntil(tok::colon, tok::r_brace, StopAtSemi | StopBeforeMatch)) {
712 TryConsumeToken(tok::colon, ColonLoc);
713 continue;
714 }
Chris Lattner34a22092009-03-04 04:23:07 +0000715 return StmtError();
716 }
717 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000718
Chris Lattner125c0ee2009-12-10 00:38:54 +0000719 ColonProtection.restore();
Sebastian Redl042ad952008-12-11 19:30:53 +0000720
Alp Tokerec543272013-12-24 09:48:30 +0000721 if (TryConsumeToken(tok::colon, ColonLoc)) {
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000722 } else if (TryConsumeToken(tok::semi, ColonLoc) ||
723 TryConsumeToken(tok::coloncolon, ColonLoc)) {
724 // Treat "case blah;" or "case blah::" as a typo for "case blah:".
Alp Tokerec543272013-12-24 09:48:30 +0000725 Diag(ColonLoc, diag::err_expected_after)
726 << "'case'" << tok::colon
727 << FixItHint::CreateReplacement(ColonLoc, ":");
John McCall0140bfe2011-01-22 09:28:32 +0000728 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000729 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
Alp Tokerec543272013-12-24 09:48:30 +0000730 Diag(ExpectedLoc, diag::err_expected_after)
731 << "'case'" << tok::colon
732 << FixItHint::CreateInsertion(ExpectedLoc, ":");
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000733 ColonLoc = ExpectedLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000734 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000735
John McCalldadc5752010-08-24 06:29:42 +0000736 StmtResult Case =
John McCallb268a282010-08-23 23:25:46 +0000737 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
738 RHS.get(), ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000739
Chris Lattner34a22092009-03-04 04:23:07 +0000740 // If we had a sema error parsing this case, then just ignore it and
741 // continue parsing the sub-stmt.
742 if (Case.isInvalid()) {
743 if (TopLevelCase.isInvalid()) // No parsed case stmts.
Alexey Bataevc4fad652016-01-13 11:18:54 +0000744 return ParseStatement(/*TrailingElseLoc=*/nullptr,
745 /*AllowOpenMPStandalone=*/true);
Chris Lattner34a22092009-03-04 04:23:07 +0000746 // Otherwise, just don't add it as a nested case.
747 } else {
748 // If this is the first case statement we parsed, it becomes TopLevelCase.
749 // Otherwise we link it into the current chain.
John McCall37ad5512010-08-23 06:44:23 +0000750 Stmt *NextDeepest = Case.get();
Chris Lattner34a22092009-03-04 04:23:07 +0000751 if (TopLevelCase.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000752 TopLevelCase = Case;
Chris Lattner34a22092009-03-04 04:23:07 +0000753 else
John McCallb268a282010-08-23 23:25:46 +0000754 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner34a22092009-03-04 04:23:07 +0000755 DeepestParsedCaseStmt = NextDeepest;
756 }
Mike Stump11289f42009-09-09 15:08:12 +0000757
Chris Lattner34a22092009-03-04 04:23:07 +0000758 // Handle all case statements.
759 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000760
Chris Lattner34a22092009-03-04 04:23:07 +0000761 // If we found a non-case statement, start by parsing it.
John McCalldadc5752010-08-24 06:29:42 +0000762 StmtResult SubStmt;
Mike Stump11289f42009-09-09 15:08:12 +0000763
Chris Lattner34a22092009-03-04 04:23:07 +0000764 if (Tok.isNot(tok::r_brace)) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000765 SubStmt = ParseStatement(/*TrailingElseLoc=*/nullptr,
766 /*AllowOpenMPStandalone=*/true);
Chris Lattner34a22092009-03-04 04:23:07 +0000767 } else {
768 // Nicely diagnose the common error "switch (X) { case 4: }", which is
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000769 // not valid. If ColonLoc doesn't point to a valid text location, there was
770 // another parsing error, so avoid producing extra diagnostics.
771 if (ColonLoc.isValid()) {
772 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
773 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
774 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
775 }
776 SubStmt = StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000777 }
Mike Stump11289f42009-09-09 15:08:12 +0000778
Chris Lattner34a22092009-03-04 04:23:07 +0000779 // Install the body into the most deeply-nested case.
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000780 if (DeepestParsedCaseStmt) {
781 // Broken sub-stmt shouldn't prevent forming the case statement properly.
782 if (SubStmt.isInvalid())
783 SubStmt = Actions.ActOnNullStmt(SourceLocation());
784 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
785 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000786
Chris Lattner34a22092009-03-04 04:23:07 +0000787 // Return the top level parsed statement tree.
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000788 return TopLevelCase;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000789}
790
791/// ParseDefaultStatement
792/// labeled-statement:
793/// 'default' ':' statement
794/// Note that this does not parse the 'statement' at the end.
795///
Richard Smithc202b282012-04-14 00:33:13 +0000796StmtResult Parser::ParseDefaultStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000797 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000798 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000799
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000800 SourceLocation ColonLoc;
Alp Tokerec543272013-12-24 09:48:30 +0000801 if (TryConsumeToken(tok::colon, ColonLoc)) {
Alp Tokerec543272013-12-24 09:48:30 +0000802 } else if (TryConsumeToken(tok::semi, ColonLoc)) {
Alp Toker97650562014-01-10 11:19:30 +0000803 // Treat "default;" as a typo for "default:".
Alp Tokerec543272013-12-24 09:48:30 +0000804 Diag(ColonLoc, diag::err_expected_after)
805 << "'default'" << tok::colon
806 << FixItHint::CreateReplacement(ColonLoc, ":");
John McCall0140bfe2011-01-22 09:28:32 +0000807 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000808 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
Alp Tokerec543272013-12-24 09:48:30 +0000809 Diag(ExpectedLoc, diag::err_expected_after)
810 << "'default'" << tok::colon
811 << FixItHint::CreateInsertion(ExpectedLoc, ":");
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000812 ColonLoc = ExpectedLoc;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000813 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000814
Richard Smith1002d102012-02-17 01:35:32 +0000815 StmtResult SubStmt;
816
817 if (Tok.isNot(tok::r_brace)) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000818 SubStmt = ParseStatement(/*TrailingElseLoc=*/nullptr,
819 /*AllowOpenMPStandalone=*/true);
Richard Smith1002d102012-02-17 01:35:32 +0000820 } else {
821 // Diagnose the common error "switch (X) {... default: }", which is
822 // not valid.
David Majnemerc6a99872011-06-14 15:24:38 +0000823 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
Richard Smith1002d102012-02-17 01:35:32 +0000824 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
825 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
826 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000827 }
828
Richard Smith1002d102012-02-17 01:35:32 +0000829 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000830 if (SubStmt.isInvalid())
Richard Smith1002d102012-02-17 01:35:32 +0000831 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl042ad952008-12-11 19:30:53 +0000832
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000833 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000834 SubStmt.get(), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000835}
836
Richard Smithc202b282012-04-14 00:33:13 +0000837StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
838 return ParseCompoundStatement(isStmtExpr, Scope::DeclScope);
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000839}
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000840
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000841/// ParseCompoundStatement - Parse a "{}" block.
842///
843/// compound-statement: [C99 6.8.2]
844/// { block-item-list[opt] }
845/// [GNU] { label-declarations block-item-list } [TODO]
846///
847/// block-item-list:
848/// block-item
849/// block-item-list block-item
850///
851/// block-item:
852/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000853/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000854/// statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000855///
856/// [GNU] label-declarations:
857/// [GNU] label-declaration
858/// [GNU] label-declarations label-declaration
859///
860/// [GNU] label-declaration:
861/// [GNU] '__label__' identifier-list ';'
862///
Richard Smithc202b282012-04-14 00:33:13 +0000863StmtResult Parser::ParseCompoundStatement(bool isStmtExpr,
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000864 unsigned ScopeFlags) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000865 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000866
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000867 // Enter a scope to hold everything within the compound stmt. Compound
868 // statements can always hold declarations.
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000869 ParseScope CompoundScope(this, ScopeFlags);
Chris Lattnerf2978802007-01-21 06:52:16 +0000870
871 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000872 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000873}
874
Lang Hames2954cea2012-11-03 22:29:05 +0000875/// Parse any pragmas at the start of the compound expression. We handle these
876/// separately since some pragmas (FP_CONTRACT) must appear before any C
877/// statement in the compound, but may be intermingled with other pragmas.
878void Parser::ParseCompoundStatementLeadingPragmas() {
879 bool checkForPragmas = true;
880 while (checkForPragmas) {
881 switch (Tok.getKind()) {
882 case tok::annot_pragma_vis:
883 HandlePragmaVisibility();
884 break;
885 case tok::annot_pragma_pack:
886 HandlePragmaPack();
887 break;
888 case tok::annot_pragma_msstruct:
889 HandlePragmaMSStruct();
890 break;
891 case tok::annot_pragma_align:
892 HandlePragmaAlign();
893 break;
894 case tok::annot_pragma_weak:
895 HandlePragmaWeak();
896 break;
897 case tok::annot_pragma_weakalias:
898 HandlePragmaWeakAlias();
899 break;
900 case tok::annot_pragma_redefine_extname:
901 HandlePragmaRedefineExtname();
902 break;
903 case tok::annot_pragma_opencl_extension:
904 HandlePragmaOpenCLExtension();
905 break;
906 case tok::annot_pragma_fp_contract:
907 HandlePragmaFPContract();
908 break;
Adam Nemet60d32642017-04-04 21:18:36 +0000909 case tok::annot_pragma_fp:
910 HandlePragmaFP();
911 break;
David Majnemer4bb09802014-02-10 19:50:15 +0000912 case tok::annot_pragma_ms_pointers_to_members:
913 HandlePragmaMSPointersToMembers();
914 break;
Warren Huntc3b18962014-04-08 22:30:47 +0000915 case tok::annot_pragma_ms_pragma:
916 HandlePragmaMSPragma();
917 break;
Alexey Bataev3d42f342015-11-20 07:02:57 +0000918 case tok::annot_pragma_ms_vtordisp:
919 HandlePragmaMSVtorDisp();
920 break;
Richard Smithba3a4f92016-01-12 21:59:26 +0000921 case tok::annot_pragma_dump:
922 HandlePragmaDump();
923 break;
Lang Hames2954cea2012-11-03 22:29:05 +0000924 default:
925 checkForPragmas = false;
926 break;
927 }
928 }
929
930}
931
Chris Lattnerf2978802007-01-21 06:52:16 +0000932/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000933/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000934/// consume the '}' at the end of the block. It does not manipulate the scope
935/// stack.
John McCalldadc5752010-08-24 06:29:42 +0000936StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000937 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000938 Tok.getLocation(),
939 "in compound statement ('{}')");
Lang Hames5de91cc2012-10-02 04:45:10 +0000940
941 // Record the state of the FP_CONTRACT pragma, restore on leaving the
942 // compound statement.
943 Sema::FPContractStateRAII SaveFPContractState(Actions);
944
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000945 InMessageExpressionRAIIObject InMessage(*this, false);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000946 BalancedDelimiterTracker T(*this, tok::l_brace);
947 if (T.consumeOpen())
948 return StmtError();
Chris Lattnerf2978802007-01-21 06:52:16 +0000949
Dmitri Gribenko800ddf32012-02-14 22:14:32 +0000950 Sema::CompoundScopeRAII CompoundScope(Actions);
951
Lang Hames2954cea2012-11-03 22:29:05 +0000952 // Parse any pragmas at the beginning of the compound statement.
953 ParseCompoundStatementLeadingPragmas();
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000954
Lang Hames2954cea2012-11-03 22:29:05 +0000955 StmtVector Stmts;
Lang Hamesa930e712012-10-21 01:10:01 +0000956
Chris Lattner43e7f312011-02-18 02:08:43 +0000957 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
958 // only allowed at the start of a compound stmt regardless of the language.
959 while (Tok.is(tok::kw___label__)) {
960 SourceLocation LabelLoc = ConsumeToken();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000961
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000962 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner43e7f312011-02-18 02:08:43 +0000963 while (1) {
964 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000965 Diag(Tok, diag::err_expected) << tok::identifier;
Chris Lattner43e7f312011-02-18 02:08:43 +0000966 break;
967 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000968
Chris Lattner43e7f312011-02-18 02:08:43 +0000969 IdentifierInfo *II = Tok.getIdentifierInfo();
970 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000971 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000972
Alp Tokerec543272013-12-24 09:48:30 +0000973 if (!TryConsumeToken(tok::comma))
Chris Lattner43e7f312011-02-18 02:08:43 +0000974 break;
Chris Lattner43e7f312011-02-18 02:08:43 +0000975 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000976
John McCall084e83d2011-03-24 11:26:52 +0000977 DeclSpec DS(AttrFactory);
Rafael Espindolaab417692013-07-09 12:05:01 +0000978 DeclGroupPtrTy Res =
979 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner43e7f312011-02-18 02:08:43 +0000980 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000981
Chris Lattner02f1b612012-04-28 16:12:17 +0000982 ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
Chris Lattner43e7f312011-02-18 02:08:43 +0000983 if (R.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000984 Stmts.push_back(R.get());
Chris Lattner43e7f312011-02-18 02:08:43 +0000985 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000986
Richard Smith752ada82015-11-17 23:32:01 +0000987 while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
988 Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000989 if (Tok.is(tok::annot_pragma_unused)) {
990 HandlePragmaUnused();
991 continue;
992 }
993
John McCalldadc5752010-08-24 06:29:42 +0000994 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000995 if (Tok.isNot(tok::kw___extension__)) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000996 R = ParseStatementOrDeclaration(Stmts, ACK_Any);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000997 } else {
998 // __extension__ can start declarations and it can also be a unary
999 // operator for expressions. Consume multiple __extension__ markers here
1000 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +00001001 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001002 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001003 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001004 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +00001005
John McCall084e83d2011-03-24 11:26:52 +00001006 ParsedAttributesWithRange attrs(AttrFactory);
Craig Topper161e4db2014-05-21 06:02:52 +00001007 MaybeParseCXX11Attributes(attrs, nullptr,
1008 /*MightBeObjCMessageSend*/ true);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001009
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001010 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001011 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +00001012 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +00001013 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +00001014 ExtensionRAIIObject O(Diags);
1015
Chris Lattner49836b42009-04-02 04:16:50 +00001016 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Rafael Espindola1bd906d2014-10-22 14:27:08 +00001017 DeclGroupPtrTy Res = ParseDeclaration(Declarator::BlockContext, DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001018 attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001019 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001020 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +00001021 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +00001022 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +00001023
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001024 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001025 SkipUntil(tok::semi);
1026 continue;
1027 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001028
Alexis Hunt96d5c762009-11-21 08:43:09 +00001029 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +00001030 // Eat the semicolon at the end of stmt and convert the expr into a
1031 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00001032 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +00001033 R = Actions.ActOnExprStmt(Res);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001034 }
1035 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001036
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001037 if (R.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001038 Stmts.push_back(R.get());
Chris Lattner30f910e2006-10-16 05:52:41 +00001039 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001040
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +00001041 SourceLocation CloseLoc = Tok.getLocation();
1042
Chris Lattner0ccd51e2006-08-09 05:47:47 +00001043 // We broke out of the while loop because we found a '}' or EOF.
Nico Webera48b6c22012-12-30 23:36:56 +00001044 if (!T.consumeClose())
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +00001045 // Recover by creating a compound statement with what we parsed so far,
1046 // instead of dropping everything and returning StmtError();
Nico Webera48b6c22012-12-30 23:36:56 +00001047 CloseLoc = T.getCloseLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +00001048
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +00001049 return Actions.ActOnCompoundStmt(T.getOpenLocation(), CloseLoc,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001050 Stmts, isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +00001051}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001052
Chris Lattnerc0081db2008-12-12 06:31:07 +00001053/// ParseParenExprOrCondition:
1054/// [C ] '(' expression ')'
Richard Smithc7a05a92016-06-29 21:17:59 +00001055/// [C++] '(' condition ')'
1056/// [C++1z] '(' init-statement[opt] condition ')'
Chris Lattnerc0081db2008-12-12 06:31:07 +00001057///
1058/// This function parses and performs error recovery on the specified condition
1059/// or expression (depending on whether we're in C++ or C mode). This function
1060/// goes out of its way to recover well. It returns true if there was a parser
1061/// error (the right paren couldn't be found), which indicates that the caller
1062/// should try to recover harder. It returns false if the condition is
1063/// successfully parsed. Note that a successful parse can still have semantic
1064/// errors in the condition.
Richard Smithc7a05a92016-06-29 21:17:59 +00001065bool Parser::ParseParenExprOrCondition(StmtResult *InitStmt,
1066 Sema::ConditionResult &Cond,
Douglas Gregore60e41a2010-05-06 17:25:47 +00001067 SourceLocation Loc,
Richard Smith03a4aa32016-06-23 19:02:52 +00001068 Sema::ConditionKind CK) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001069 BalancedDelimiterTracker T(*this, tok::l_paren);
1070 T.consumeOpen();
1071
David Blaikiebbafb8a2012-03-11 07:00:24 +00001072 if (getLangOpts().CPlusPlus)
Richard Smithc7a05a92016-06-29 21:17:59 +00001073 Cond = ParseCXXCondition(InitStmt, Loc, CK);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001074 else {
Richard Smith03a4aa32016-06-23 19:02:52 +00001075 ExprResult CondExpr = ParseExpression();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001076
Douglas Gregore60e41a2010-05-06 17:25:47 +00001077 // If required, convert to a boolean value.
Richard Smith03a4aa32016-06-23 19:02:52 +00001078 if (CondExpr.isInvalid())
1079 Cond = Sema::ConditionError();
1080 else
1081 Cond = Actions.ActOnCondition(getCurScope(), Loc, CondExpr.get(), CK);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001082 }
Mike Stump11289f42009-09-09 15:08:12 +00001083
Chris Lattnerc0081db2008-12-12 06:31:07 +00001084 // If the parser was confused by the condition and we don't have a ')', try to
1085 // recover by skipping ahead to a semi and bailing out. If condexp is
1086 // semantically invalid but we have well formed code, keep going.
Richard Smith03a4aa32016-06-23 19:02:52 +00001087 if (Cond.isInvalid() && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +00001088 SkipUntil(tok::semi);
1089 // Skipping may have stopped if it found the containing ')'. If so, we can
1090 // continue parsing the if statement.
1091 if (Tok.isNot(tok::r_paren))
1092 return true;
1093 }
Mike Stump11289f42009-09-09 15:08:12 +00001094
Chris Lattnerc0081db2008-12-12 06:31:07 +00001095 // Otherwise the condition is valid or the rparen is present.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001096 T.consumeClose();
Chad Rosier67055f52012-07-10 21:35:27 +00001097
Chris Lattner70d44982012-04-28 16:24:20 +00001098 // Check for extraneous ')'s to catch things like "if (foo())) {". We know
1099 // that all callers are looking for a statement after the condition, so ")"
1100 // isn't valid.
1101 while (Tok.is(tok::r_paren)) {
1102 Diag(Tok, diag::err_extraneous_rparen_in_condition)
1103 << FixItHint::CreateRemoval(Tok.getLocation());
1104 ConsumeParen();
1105 }
Chad Rosier67055f52012-07-10 21:35:27 +00001106
Chris Lattnerc0081db2008-12-12 06:31:07 +00001107 return false;
1108}
1109
1110
Chris Lattnerc951dae2006-08-10 04:23:57 +00001111/// ParseIfStatement
1112/// if-statement: [C99 6.8.4.1]
1113/// 'if' '(' expression ')' statement
1114/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001115/// [C++] 'if' '(' condition ')' statement
1116/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +00001117///
Richard Smithc202b282012-04-14 00:33:13 +00001118StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001119 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001120 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +00001121
Richard Smithb130fe72016-06-23 19:16:49 +00001122 bool IsConstexpr = false;
1123 if (Tok.is(tok::kw_constexpr)) {
1124 Diag(Tok, getLangOpts().CPlusPlus1z ? diag::warn_cxx14_compat_constexpr_if
1125 : diag::ext_constexpr_if);
1126 IsConstexpr = true;
1127 ConsumeToken();
1128 }
1129
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001130 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001131 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +00001132 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +00001133 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +00001134 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001135
David Blaikiebbafb8a2012-03-11 07:00:24 +00001136 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001137
Chris Lattner2dd1b722007-08-26 23:08:06 +00001138 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
1139 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001140 //
1141 // C++ 6.4p3:
1142 // A name introduced by a declaration in a condition is in scope from its
1143 // point of declaration until the end of the substatements controlled by the
1144 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001145 // C++ 3.3.2p4:
1146 // Names declared in the for-init-statement, and in the condition of if,
1147 // while, for, and switch statements are local to the if, while, for, or
1148 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001149 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001150 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +00001151
Chris Lattnerc951dae2006-08-10 04:23:57 +00001152 // Parse the condition.
Richard Smithc7a05a92016-06-29 21:17:59 +00001153 StmtResult InitStmt;
Richard Smith03a4aa32016-06-23 19:02:52 +00001154 Sema::ConditionResult Cond;
Richard Smithc7a05a92016-06-29 21:17:59 +00001155 if (ParseParenExprOrCondition(&InitStmt, Cond, IfLoc,
Richard Smithb130fe72016-06-23 19:16:49 +00001156 IsConstexpr ? Sema::ConditionKind::ConstexprIf
1157 : Sema::ConditionKind::Boolean))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001158 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +00001159
Richard Smithb130fe72016-06-23 19:16:49 +00001160 llvm::Optional<bool> ConstexprCondition;
1161 if (IsConstexpr)
1162 ConstexprCondition = Cond.getKnownValue();
1163
Chris Lattner8fb26252007-08-22 05:28:50 +00001164 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-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 Kyrtzidis504bb842008-09-11 03:06:46 +00001167 //
1168 // C++ 6.4p1:
1169 // The substatement in a selection-statement (each substatement, in the else
1170 // form of the if statement) implicitly defines a local scope.
1171 //
1172 // For C++ we create a scope for the condition and a new scope for
1173 // substatements because:
1174 // -When the 'then' scope exits, we want the condition declaration to still be
1175 // active for the 'else' scope too.
1176 // -Sema will detect name clashes by considering declarations of a
1177 // 'ControlScope' as part of its direct subscope.
1178 // -If we wanted the condition and substatement to be in the same scope, we
1179 // would have to notify ParseStatement not to create a new scope. It's
1180 // simpler to let it create a new scope.
1181 //
David Majnemer2206bf52014-03-05 08:57:59 +00001182 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001183
Chris Lattner5c5808a2007-10-29 05:08:52 +00001184 // Read the 'then' stmt.
1185 SourceLocation ThenStmtLoc = Tok.getLocation();
Nico Weber3cef1082011-12-22 23:26:17 +00001186
1187 SourceLocation InnerStatementTrailingElseLoc;
Richard Smithb130fe72016-06-23 19:16:49 +00001188 StmtResult ThenStmt;
1189 {
1190 EnterExpressionEvaluationContext PotentiallyDiscarded(
Faisal Valid143a0c2017-04-01 21:30:49 +00001191 Actions, Sema::ExpressionEvaluationContext::DiscardedStatement, nullptr,
1192 false,
Richard Smithb130fe72016-06-23 19:16:49 +00001193 /*ShouldEnter=*/ConstexprCondition && !*ConstexprCondition);
1194 ThenStmt = ParseStatement(&InnerStatementTrailingElseLoc);
1195 }
Chris Lattnerac4471c2007-05-28 05:38:24 +00001196
Chris Lattner37e54f42007-08-22 05:16:28 +00001197 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001198 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001199
Chris Lattnerc951dae2006-08-10 04:23:57 +00001200 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +00001201 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +00001202 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +00001203 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001204
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001205 if (Tok.is(tok::kw_else)) {
Nico Weber3cef1082011-12-22 23:26:17 +00001206 if (TrailingElseLoc)
1207 *TrailingElseLoc = Tok.getLocation();
1208
Chris Lattneraf635312006-10-16 06:06:51 +00001209 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +00001210 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +00001211
Chris Lattner8fb26252007-08-22 05:28:50 +00001212 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001213 // there is no compound stmt. C90 does not have this clause. We only do
1214 // this if the body isn't a compound statement to avoid push/pop in common
1215 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001216 //
1217 // C++ 6.4p1:
1218 // The substatement in a selection-statement (each substatement, in the else
1219 // form of the if statement) implicitly defines a local scope.
1220 //
Richard Smithb130fe72016-06-23 19:16:49 +00001221 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX,
1222 Tok.is(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001223
Richard Smithb130fe72016-06-23 19:16:49 +00001224 EnterExpressionEvaluationContext PotentiallyDiscarded(
Faisal Valid143a0c2017-04-01 21:30:49 +00001225 Actions, Sema::ExpressionEvaluationContext::DiscardedStatement, nullptr,
1226 false,
Richard Smithb130fe72016-06-23 19:16:49 +00001227 /*ShouldEnter=*/ConstexprCondition && *ConstexprCondition);
Chris Lattner30f910e2006-10-16 05:52:41 +00001228 ElseStmt = ParseStatement();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001229
Chris Lattner37e54f42007-08-22 05:16:28 +00001230 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001231 InnerScope.Exit();
Douglas Gregor4ecb7202011-07-30 08:36:53 +00001232 } else if (Tok.is(tok::code_completion)) {
1233 Actions.CodeCompleteAfterIf(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001234 cutOffParsing();
1235 return StmtError();
Nico Weber3cef1082011-12-22 23:26:17 +00001236 } else if (InnerStatementTrailingElseLoc.isValid()) {
1237 Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else);
Chris Lattnerc951dae2006-08-10 04:23:57 +00001238 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001239
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001240 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +00001241
Chris Lattner5c5808a2007-10-29 05:08:52 +00001242 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +00001243 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +00001244 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001245 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
Craig Topper161e4db2014-05-21 06:02:52 +00001246 (ThenStmt.isInvalid() && ElseStmt.get() == nullptr) ||
1247 (ThenStmt.get() == nullptr && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +00001248 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +00001249 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +00001250 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001251
Chris Lattner5c5808a2007-10-29 05:08:52 +00001252 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001253 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001254 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001255 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001256 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001257
Richard Smithc7a05a92016-06-29 21:17:59 +00001258 return Actions.ActOnIfStmt(IfLoc, IsConstexpr, InitStmt.get(), Cond,
1259 ThenStmt.get(), ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +00001260}
1261
Chris Lattner9075bd72006-08-10 04:59:57 +00001262/// ParseSwitchStatement
1263/// switch-statement:
1264/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001265/// [C++] 'switch' '(' condition ')' statement
Richard Smithc202b282012-04-14 00:33:13 +00001266StmtResult Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001267 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001268 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +00001269
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001270 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001271 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +00001272 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001273 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001274 }
Chris Lattner2dd1b722007-08-26 23:08:06 +00001275
David Blaikiebbafb8a2012-03-11 07:00:24 +00001276 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001277
Chris Lattner2dd1b722007-08-26 23:08:06 +00001278 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1279 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001280 //
1281 // C++ 6.4p3:
1282 // A name introduced by a declaration in a condition is in scope from its
1283 // point of declaration until the end of the substatements controlled by the
1284 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001285 // C++ 3.3.2p4:
1286 // Names declared in the for-init-statement, and in the condition of if,
1287 // while, for, and switch statements are local to the if, while, for, or
1288 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001289 //
Serge Pavlov09f99242014-01-23 15:05:00 +00001290 unsigned ScopeFlags = Scope::SwitchScope;
Chris Lattnerc0081db2008-12-12 06:31:07 +00001291 if (C99orCXX)
1292 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001293 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001294
Chris Lattner9075bd72006-08-10 04:59:57 +00001295 // Parse the condition.
Richard Smithc7a05a92016-06-29 21:17:59 +00001296 StmtResult InitStmt;
Richard Smith03a4aa32016-06-23 19:02:52 +00001297 Sema::ConditionResult Cond;
Richard Smithc7a05a92016-06-29 21:17:59 +00001298 if (ParseParenExprOrCondition(&InitStmt, Cond, SwitchLoc,
1299 Sema::ConditionKind::Switch))
Sebastian Redlb62406f2008-12-11 19:48:14 +00001300 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +00001301
Richard Smithc7a05a92016-06-29 21:17:59 +00001302 StmtResult Switch =
1303 Actions.ActOnStartOfSwitchStmt(SwitchLoc, InitStmt.get(), Cond);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001304
Douglas Gregore60e41a2010-05-06 17:25:47 +00001305 if (Switch.isInvalid()) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001306 // Skip the switch body.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001307 // FIXME: This is not optimal recovery, but parsing the body is more
1308 // dangerous due to the presence of case and default statements, which
1309 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +00001310 if (Tok.is(tok::l_brace)) {
1311 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001312 SkipUntil(tok::r_brace);
Douglas Gregor4abc32d2010-05-20 23:20:59 +00001313 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +00001314 SkipUntil(tok::semi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001315 return Switch;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001316 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001317
Chris Lattner8fb26252007-08-22 05:28:50 +00001318 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001319 // there is no compound stmt. C90 does not have this clause. We only do this
1320 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001321 //
1322 // C++ 6.4p1:
1323 // The substatement in a selection-statement (each substatement, in the else
1324 // form of the if statement) implicitly defines a local scope.
1325 //
1326 // See comments in ParseIfStatement for why we create a scope for the
1327 // condition and a new scope for substatement in C++.
1328 //
Serge Pavlov09f99242014-01-23 15:05:00 +00001329 getCurScope()->AddFlags(Scope::BreakScope);
David Majnemer2206bf52014-03-05 08:57:59 +00001330 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +00001331
Hans Wennborg852c3462014-06-17 00:09:05 +00001332 // We have incremented the mangling number for the SwitchScope and the
1333 // InnerScope, which is one too many.
1334 if (C99orCXX)
David Majnemera7f8c462015-03-19 21:54:30 +00001335 getCurScope()->decrementMSManglingNumber();
Hans Wennborg852c3462014-06-17 00:09:05 +00001336
Chris Lattner9075bd72006-08-10 04:59:57 +00001337 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001338 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001339
Chris Lattner8fd2d012010-01-24 01:50:29 +00001340 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001341 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001342 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001343
John McCallb268a282010-08-23 23:25:46 +00001344 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001345}
1346
1347/// ParseWhileStatement
1348/// while-statement: [C99 6.8.5.1]
1349/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001350/// [C++] 'while' '(' condition ')' statement
Richard Smithc202b282012-04-14 00:33:13 +00001351StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001352 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +00001353 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +00001354 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001355
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001356 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001357 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +00001358 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001359 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001360 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001361
David Blaikiebbafb8a2012-03-11 07:00:24 +00001362 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001363
Chris Lattner2dd1b722007-08-26 23:08:06 +00001364 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1365 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001366 //
1367 // C++ 6.4p3:
1368 // A name introduced by a declaration in a condition is in scope from its
1369 // point of declaration until the end of the substatements controlled by the
1370 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001371 // C++ 3.3.2p4:
1372 // Names declared in the for-init-statement, and in the condition of if,
1373 // while, for, and switch statements are local to the if, while, for, or
1374 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001375 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001376 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001377 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001378 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1379 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001380 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001381 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1382 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001383
Chris Lattner9075bd72006-08-10 04:59:57 +00001384 // Parse the condition.
Richard Smith03a4aa32016-06-23 19:02:52 +00001385 Sema::ConditionResult Cond;
Richard Smithc7a05a92016-06-29 21:17:59 +00001386 if (ParseParenExprOrCondition(nullptr, Cond, WhileLoc,
1387 Sema::ConditionKind::Boolean))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001388 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001389
Justin Bognere4ebb6c2013-12-03 07:36:55 +00001390 // C99 6.8.5p5 - In C99, the body of the while statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001391 // there is no compound stmt. C90 does not have this clause. We only do this
1392 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001393 //
1394 // C++ 6.5p2:
1395 // The substatement in an iteration-statement implicitly defines a local scope
1396 // which is entered and exited each time through the loop.
1397 //
1398 // See comments in ParseIfStatement for why we create a scope for the
1399 // condition and a new scope for substatement in C++.
1400 //
David Majnemer2206bf52014-03-05 08:57:59 +00001401 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001402
Chris Lattner9075bd72006-08-10 04:59:57 +00001403 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001404 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001405
Chris Lattner8fb26252007-08-22 05:28:50 +00001406 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001407 InnerScope.Exit();
1408 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001409
Richard Smith03a4aa32016-06-23 19:02:52 +00001410 if (Cond.isInvalid() || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001411 return StmtError();
1412
Richard Smith03a4aa32016-06-23 19:02:52 +00001413 return Actions.ActOnWhileStmt(WhileLoc, Cond, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001414}
1415
1416/// ParseDoStatement
1417/// do-statement: [C99 6.8.5.2]
1418/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +00001419/// Note: this lets the caller parse the end ';'.
Richard Smithc202b282012-04-14 00:33:13 +00001420StmtResult Parser::ParseDoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001421 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001422 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001423
Chris Lattner2dd1b722007-08-26 23:08:06 +00001424 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1425 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001426 unsigned ScopeFlags;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001427 if (getLangOpts().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001428 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001429 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001430 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001431
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001432 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001433
Justin Bognere4ebb6c2013-12-03 07:36:55 +00001434 // C99 6.8.5p5 - In C99, the body of the do statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001435 // there is no compound stmt. C90 does not have this clause. We only do this
1436 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +00001437 //
1438 // C++ 6.5p2:
1439 // The substatement in an iteration-statement implicitly defines a local scope
1440 // which is entered and exited each time through the loop.
1441 //
David Majnemer2206bf52014-03-05 08:57:59 +00001442 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
1443 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001444
Chris Lattner9075bd72006-08-10 04:59:57 +00001445 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001446 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +00001447
Chris Lattner8fb26252007-08-22 05:28:50 +00001448 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001449 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001450
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001451 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001452 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +00001453 Diag(Tok, diag::err_expected_while);
Alp Tokerec543272013-12-24 09:48:30 +00001454 Diag(DoLoc, diag::note_matching) << "'do'";
Alexey Bataevee6507d2013-11-18 08:17:37 +00001455 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner0046de12008-11-13 18:52:53 +00001456 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001457 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001458 }
Chris Lattneraf635312006-10-16 06:06:51 +00001459 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001460
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001461 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001462 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Alexey Bataevee6507d2013-11-18 08:17:37 +00001463 SkipUntil(tok::semi, StopBeforeMatch);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001464 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001465 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001466
Richard Smithc2c8bb82013-10-15 01:34:54 +00001467 // Parse the parenthesized expression.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001468 BalancedDelimiterTracker T(*this, tok::l_paren);
1469 T.consumeOpen();
Chad Rosier67055f52012-07-10 21:35:27 +00001470
Richard Smithc2c8bb82013-10-15 01:34:54 +00001471 // A do-while expression is not a condition, so can't have attributes.
1472 DiagnoseAndSkipCXX11Attributes();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001473
John McCalldadc5752010-08-24 06:29:42 +00001474 ExprResult Cond = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001475 T.consumeClose();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001476 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001477
Sebastian Redlb62406f2008-12-11 19:48:14 +00001478 if (Cond.isInvalid() || Body.isInvalid())
1479 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001480
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001481 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(),
1482 Cond.get(), T.getCloseLocation());
Chris Lattner9075bd72006-08-10 04:59:57 +00001483}
1484
Richard Smith955bf012014-06-19 11:42:00 +00001485bool Parser::isForRangeIdentifier() {
1486 assert(Tok.is(tok::identifier));
1487
1488 const Token &Next = NextToken();
1489 if (Next.is(tok::colon))
1490 return true;
1491
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001492 if (Next.isOneOf(tok::l_square, tok::kw_alignas)) {
Richard Smith955bf012014-06-19 11:42:00 +00001493 TentativeParsingAction PA(*this);
1494 ConsumeToken();
1495 SkipCXX11Attributes();
1496 bool Result = Tok.is(tok::colon);
1497 PA.Revert();
1498 return Result;
1499 }
1500
1501 return false;
1502}
1503
Chris Lattner9075bd72006-08-10 04:59:57 +00001504/// ParseForStatement
1505/// for-statement: [C99 6.8.5.3]
1506/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1507/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001508/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1509/// [C++] statement
Richard Smith0e304ea2015-10-22 04:46:14 +00001510/// [C++0x] 'for'
1511/// 'co_await'[opt] [Coroutines]
1512/// '(' for-range-declaration ':' for-range-initializer ')'
1513/// statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001514/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1515/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001516///
1517/// [C++] for-init-statement:
1518/// [C++] expression-statement
1519/// [C++] simple-declaration
1520///
Richard Smith02e85f32011-04-14 22:09:26 +00001521/// [C++0x] for-range-declaration:
1522/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1523/// [C++0x] for-range-initializer:
1524/// [C++0x] expression
1525/// [C++0x] braced-init-list [TODO]
Richard Smithc202b282012-04-14 00:33:13 +00001526StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001527 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001528 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001529
Richard Smith0e304ea2015-10-22 04:46:14 +00001530 SourceLocation CoawaitLoc;
1531 if (Tok.is(tok::kw_co_await))
1532 CoawaitLoc = ConsumeToken();
1533
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001534 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001535 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +00001536 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001537 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001538 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001539
Chad Rosier67055f52012-07-10 21:35:27 +00001540 bool C99orCXXorObjC = getLangOpts().C99 || getLangOpts().CPlusPlus ||
1541 getLangOpts().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001542
Chris Lattner2dd1b722007-08-26 23:08:06 +00001543 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1544 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001545 //
1546 // C++ 6.4p3:
1547 // A name introduced by a declaration in a condition is in scope from its
1548 // point of declaration until the end of the substatements controlled by the
1549 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001550 // C++ 3.3.2p4:
1551 // Names declared in the for-init-statement, and in the condition of if,
1552 // while, for, and switch statements are local to the if, while, for, or
1553 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001554 // C++ 6.5.3p1:
1555 // Names declared in the for-init-statement are in the same declarative-region
1556 // as those declared in the condition.
1557 //
Serge Pavlov09f99242014-01-23 15:05:00 +00001558 unsigned ScopeFlags = 0;
Chris Lattner934074c2009-04-22 00:54:41 +00001559 if (C99orCXXorObjC)
Serge Pavlov09f99242014-01-23 15:05:00 +00001560 ScopeFlags = Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001561
1562 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +00001563
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001564 BalancedDelimiterTracker T(*this, tok::l_paren);
1565 T.consumeOpen();
1566
John McCalldadc5752010-08-24 06:29:42 +00001567 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001568
Richard Smith02e85f32011-04-14 22:09:26 +00001569 bool ForEach = false, ForRange = false;
John McCalldadc5752010-08-24 06:29:42 +00001570 StmtResult FirstPart;
Richard Smith03a4aa32016-06-23 19:02:52 +00001571 Sema::ConditionResult SecondPart;
John McCalldadc5752010-08-24 06:29:42 +00001572 ExprResult Collection;
Richard Smith02e85f32011-04-14 22:09:26 +00001573 ForRangeInit ForRangeInit;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001574 FullExprArg ThirdPart(Actions);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001575
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001576 if (Tok.is(tok::code_completion)) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001577 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001578 C99orCXXorObjC? Sema::PCC_ForInit
1579 : Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001580 cutOffParsing();
1581 return StmtError();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001582 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001583
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001584 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001585 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001586
Chris Lattner9075bd72006-08-10 04:59:57 +00001587 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001588 if (Tok.is(tok::semi)) { // for (;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001589 ProhibitAttributes(attrs);
Chris Lattner53361ac2006-08-10 05:19:57 +00001590 // no first part, eat the ';'.
1591 ConsumeToken();
Richard Smith955bf012014-06-19 11:42:00 +00001592 } else if (getLangOpts().CPlusPlus && Tok.is(tok::identifier) &&
1593 isForRangeIdentifier()) {
1594 ProhibitAttributes(attrs);
1595 IdentifierInfo *Name = Tok.getIdentifierInfo();
1596 SourceLocation Loc = ConsumeToken();
1597 MaybeParseCXX11Attributes(attrs);
1598
1599 ForRangeInit.ColonLoc = ConsumeToken();
1600 if (Tok.is(tok::l_brace))
1601 ForRangeInit.RangeExpr = ParseBraceInitializer();
1602 else
1603 ForRangeInit.RangeExpr = ParseExpression();
1604
Richard Smith83d3f152014-11-27 01:54:27 +00001605 Diag(Loc, diag::err_for_range_identifier)
Richard Smith955bf012014-06-19 11:42:00 +00001606 << ((getLangOpts().CPlusPlus11 && !getLangOpts().CPlusPlus1z)
1607 ? FixItHint::CreateInsertion(Loc, "auto &&")
1608 : FixItHint());
1609
1610 FirstPart = Actions.ActOnCXXForRangeIdentifier(getCurScope(), Loc, Name,
1611 attrs, attrs.Range.getEnd());
1612 ForRange = true;
Eli Friedman0ffc31c2011-12-20 01:50:37 +00001613 } else if (isForInitDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001614 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001615 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001616 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001617
Richard Smith02e85f32011-04-14 22:09:26 +00001618 // In C++0x, "for (T NS:a" might not be a typo for ::
David Blaikiebbafb8a2012-03-11 07:00:24 +00001619 bool MightBeForRangeStmt = getLangOpts().CPlusPlus;
Richard Smith02e85f32011-04-14 22:09:26 +00001620 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1621
Chris Lattner49836b42009-04-02 04:16:50 +00001622 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00001623 DeclGroupPtrTy DG = ParseSimpleDeclaration(
Rafael Espindola1bd906d2014-10-22 14:27:08 +00001624 Declarator::ForContext, DeclEnd, attrs, false,
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00001625 MightBeForRangeStmt ? &ForRangeInit : nullptr);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001626 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Richard Smith02e85f32011-04-14 22:09:26 +00001627 if (ForRangeInit.ParsedForRangeDecl()) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001628 Diag(ForRangeInit.ColonLoc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00001629 diag::warn_cxx98_compat_for_range : diag::ext_for_range);
Richard Smith58c74332011-09-04 19:54:14 +00001630
Richard Smith02e85f32011-04-14 22:09:26 +00001631 ForRange = true;
1632 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattner32dc41c2009-03-29 17:27:48 +00001633 ConsumeToken();
1634 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001635 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001636 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001637 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001638
Douglas Gregor68762e72010-08-23 21:17:50 +00001639 if (Tok.is(tok::code_completion)) {
1640 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001641 cutOffParsing();
1642 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001643 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001644 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001645 } else {
1646 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001647 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001648 } else {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001649 ProhibitAttributes(attrs);
Kaelyn Takatab16e6322014-11-20 22:06:40 +00001650 Value = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Chris Lattner71e23ce2006-11-04 20:18:38 +00001651
John McCall34376a62010-12-04 03:47:34 +00001652 ForEach = isTokIdentifier_in();
1653
Chris Lattnercd68f642007-06-27 01:06:29 +00001654 // Turn the expression into a stmt.
John McCall34376a62010-12-04 03:47:34 +00001655 if (!Value.isInvalid()) {
1656 if (ForEach)
1657 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1658 else
Richard Smith945f8d32013-01-14 22:39:08 +00001659 FirstPart = Actions.ActOnExprStmt(Value);
John McCall34376a62010-12-04 03:47:34 +00001660 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001661
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001662 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001663 ConsumeToken();
John McCall34376a62010-12-04 03:47:34 +00001664 } else if (ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001665 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001666
Douglas Gregor68762e72010-08-23 21:17:50 +00001667 if (Tok.is(tok::code_completion)) {
David Blaikie0403cb12016-01-15 23:43:25 +00001668 Actions.CodeCompleteObjCForCollection(getCurScope(), nullptr);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001669 cutOffParsing();
1670 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001671 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001672 Collection = ParseExpression();
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001673 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::colon) && FirstPart.get()) {
Richard Smith4f848f12011-12-20 22:56:20 +00001674 // User tried to write the reasonable, but ill-formed, for-range-statement
1675 // for (expr : expr) { ... }
1676 Diag(Tok, diag::err_for_range_expected_decl)
1677 << FirstPart.get()->getSourceRange();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001678 SkipUntil(tok::r_paren, StopBeforeMatch);
Richard Smith03a4aa32016-06-23 19:02:52 +00001679 SecondPart = Sema::ConditionError();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001680 } else {
Douglas Gregor230a7e62011-02-17 03:38:46 +00001681 if (!Value.isInvalid()) {
1682 Diag(Tok, diag::err_expected_semi_for);
1683 } else {
1684 // Skip until semicolon or rparen, don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001685 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor230a7e62011-02-17 03:38:46 +00001686 if (Tok.is(tok::semi))
1687 ConsumeToken();
1688 }
Chris Lattner53361ac2006-08-10 05:19:57 +00001689 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001690 }
Serge Pavlov09f99242014-01-23 15:05:00 +00001691
1692 // Parse the second part of the for specifier.
1693 getCurScope()->AddFlags(Scope::BreakScope | Scope::ContinueScope);
Richard Smith03a4aa32016-06-23 19:02:52 +00001694 if (!ForEach && !ForRange && !SecondPart.isInvalid()) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001695 // Parse the second part of the for specifier.
1696 if (Tok.is(tok::semi)) { // for (...;;
1697 // no second part.
Douglas Gregor230a7e62011-02-17 03:38:46 +00001698 } else if (Tok.is(tok::r_paren)) {
1699 // missing both semicolons.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001700 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001701 if (getLangOpts().CPlusPlus)
Richard Smithc7a05a92016-06-29 21:17:59 +00001702 SecondPart =
1703 ParseCXXCondition(nullptr, ForLoc, Sema::ConditionKind::Boolean);
Douglas Gregore60e41a2010-05-06 17:25:47 +00001704 else {
Richard Smith03a4aa32016-06-23 19:02:52 +00001705 ExprResult SecondExpr = ParseExpression();
1706 if (SecondExpr.isInvalid())
1707 SecondPart = Sema::ConditionError();
1708 else
1709 SecondPart =
1710 Actions.ActOnCondition(getCurScope(), ForLoc, SecondExpr.get(),
1711 Sema::ConditionKind::Boolean);
Douglas Gregore60e41a2010-05-06 17:25:47 +00001712 }
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001713 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001714
Douglas Gregor230a7e62011-02-17 03:38:46 +00001715 if (Tok.isNot(tok::semi)) {
Richard Smith03a4aa32016-06-23 19:02:52 +00001716 if (!SecondPart.isInvalid())
Douglas Gregor230a7e62011-02-17 03:38:46 +00001717 Diag(Tok, diag::err_expected_semi_for);
1718 else
1719 // Skip until semicolon or rparen, don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001720 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor230a7e62011-02-17 03:38:46 +00001721 }
1722
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001723 if (Tok.is(tok::semi)) {
1724 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001725 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001726
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001727 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001728 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001729 ExprResult Third = ParseExpression();
Richard Smith945f8d32013-01-14 22:39:08 +00001730 // FIXME: The C++11 standard doesn't actually say that this is a
1731 // discarded-value expression, but it clearly should be.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001732 ThirdPart = Actions.MakeFullDiscardedValueExpr(Third.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001733 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001734 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001735 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001736 T.consumeClose();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001737
Richard Smith0e304ea2015-10-22 04:46:14 +00001738 // C++ Coroutines [stmt.iter]:
1739 // 'co_await' can only be used for a range-based for statement.
1740 if (CoawaitLoc.isValid() && !ForRange) {
1741 Diag(CoawaitLoc, diag::err_for_co_await_not_range_for);
1742 CoawaitLoc = SourceLocation();
1743 }
1744
Richard Smith02e85f32011-04-14 22:09:26 +00001745 // We need to perform most of the semantic analysis for a C++0x for-range
1746 // statememt before parsing the body, in order to be able to deduce the type
1747 // of an auto-typed loop variable.
1748 StmtResult ForRangeStmt;
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001749 StmtResult ForEachStmt;
Chad Rosier67055f52012-07-10 21:35:27 +00001750
John McCall53848232011-07-27 01:07:15 +00001751 if (ForRange) {
Denis Zobnin7d6b9242016-02-02 17:33:09 +00001752 ExprResult CorrectedRange =
1753 Actions.CorrectDelayedTyposInExpr(ForRangeInit.RangeExpr.get());
Richard Smith9f690bd2015-10-27 06:02:45 +00001754 ForRangeStmt = Actions.ActOnCXXForRangeStmt(
1755 getCurScope(), ForLoc, CoawaitLoc, FirstPart.get(),
Denis Zobnin7d6b9242016-02-02 17:33:09 +00001756 ForRangeInit.ColonLoc, CorrectedRange.get(),
Richard Smith9f690bd2015-10-27 06:02:45 +00001757 T.getCloseLocation(), Sema::BFRK_Build);
John McCall53848232011-07-27 01:07:15 +00001758
1759 // Similarly, we need to do the semantic analysis for a for-range
1760 // statement immediately in order to close over temporaries correctly.
1761 } else if (ForEach) {
Sam Panzer2c4ca0f2012-08-16 21:47:25 +00001762 ForEachStmt = Actions.ActOnObjCForCollectionStmt(ForLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001763 FirstPart.get(),
1764 Collection.get(),
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001765 T.getCloseLocation());
Alexey Bataev9c821032015-04-30 04:23:23 +00001766 } else {
1767 // In OpenMP loop region loop control variable must be captured and be
1768 // private. Perform analysis of first part (if any).
1769 if (getLangOpts().OpenMP && FirstPart.isUsable()) {
1770 Actions.ActOnOpenMPLoopInitialization(ForLoc, FirstPart.get());
1771 }
John McCall53848232011-07-27 01:07:15 +00001772 }
1773
Justin Bognere4ebb6c2013-12-03 07:36:55 +00001774 // C99 6.8.5p5 - In C99, the body of the for statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001775 // there is no compound stmt. C90 does not have this clause. We only do this
1776 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001777 //
1778 // C++ 6.5p2:
1779 // The substatement in an iteration-statement implicitly defines a local scope
1780 // which is entered and exited each time through the loop.
1781 //
1782 // See comments in ParseIfStatement for why we create a scope for
1783 // for-init-statement/condition and a new scope for substatement in C++.
1784 //
David Majnemer2206bf52014-03-05 08:57:59 +00001785 ParseScope InnerScope(this, Scope::DeclScope, C99orCXXorObjC,
1786 Tok.is(tok::l_brace));
1787
1788 // The body of the for loop has the same local mangling number as the
1789 // for-init-statement.
1790 // It will only be incremented if the body contains other things that would
1791 // normally increment the mangling number (like a compound statement).
1792 if (C99orCXXorObjC)
David Majnemera7f8c462015-03-19 21:54:30 +00001793 getCurScope()->decrementMSManglingNumber();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001794
Chris Lattner9075bd72006-08-10 04:59:57 +00001795 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001796 StmtResult Body(ParseStatement(TrailingElseLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001797
Chris Lattner8fb26252007-08-22 05:28:50 +00001798 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001799 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001800
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001801 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001802 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001803
1804 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001805 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001806
Richard Smith02e85f32011-04-14 22:09:26 +00001807 if (ForEach)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001808 return Actions.FinishObjCForCollectionStmt(ForEachStmt.get(),
1809 Body.get());
Mike Stump11289f42009-09-09 15:08:12 +00001810
Richard Smith02e85f32011-04-14 22:09:26 +00001811 if (ForRange)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001812 return Actions.FinishCXXForRangeStmt(ForRangeStmt.get(), Body.get());
Richard Smith02e85f32011-04-14 22:09:26 +00001813
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001814 return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.get(),
Richard Smith03a4aa32016-06-23 19:02:52 +00001815 SecondPart, ThirdPart, T.getCloseLocation(),
1816 Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001817}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001818
Chris Lattner503fadc2006-08-10 05:45:44 +00001819/// ParseGotoStatement
1820/// jump-statement:
1821/// 'goto' identifier ';'
1822/// [GNU] 'goto' '*' expression ';'
1823///
1824/// Note: this lets the caller parse the end ';'.
1825///
Richard Smithc202b282012-04-14 00:33:13 +00001826StmtResult Parser::ParseGotoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001827 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001828 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001829
John McCalldadc5752010-08-24 06:29:42 +00001830 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001831 if (Tok.is(tok::identifier)) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +00001832 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1833 Tok.getLocation());
1834 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Chris Lattner503fadc2006-08-10 05:45:44 +00001835 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001836 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001837 // GNU indirect goto extension.
1838 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001839 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001840 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001841 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001842 SkipUntil(tok::semi, StopBeforeMatch);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001843 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001844 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001845 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.get());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001846 } else {
Alp Tokerec543272013-12-24 09:48:30 +00001847 Diag(Tok, diag::err_expected) << tok::identifier;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001848 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001849 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001850
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001851 return Res;
Chris Lattner503fadc2006-08-10 05:45:44 +00001852}
1853
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001854/// ParseContinueStatement
1855/// jump-statement:
1856/// 'continue' ';'
1857///
1858/// Note: this lets the caller parse the end ';'.
1859///
Richard Smithc202b282012-04-14 00:33:13 +00001860StmtResult Parser::ParseContinueStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001861 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001862 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001863}
1864
1865/// ParseBreakStatement
1866/// jump-statement:
1867/// 'break' ';'
1868///
1869/// Note: this lets the caller parse the end ';'.
1870///
Richard Smithc202b282012-04-14 00:33:13 +00001871StmtResult Parser::ParseBreakStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001872 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001873 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001874}
1875
Chris Lattner503fadc2006-08-10 05:45:44 +00001876/// ParseReturnStatement
1877/// jump-statement:
1878/// 'return' expression[opt] ';'
Richard Smith0e304ea2015-10-22 04:46:14 +00001879/// 'return' braced-init-list ';'
1880/// 'co_return' expression[opt] ';'
1881/// 'co_return' braced-init-list ';'
Richard Smithc202b282012-04-14 00:33:13 +00001882StmtResult Parser::ParseReturnStatement() {
Richard Smith0e304ea2015-10-22 04:46:14 +00001883 assert((Tok.is(tok::kw_return) || Tok.is(tok::kw_co_return)) &&
1884 "Not a return stmt!");
1885 bool IsCoreturn = Tok.is(tok::kw_co_return);
Chris Lattneraf635312006-10-16 06:06:51 +00001886 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001887
John McCalldadc5752010-08-24 06:29:42 +00001888 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001889 if (Tok.isNot(tok::semi)) {
Richard Smith0e304ea2015-10-22 04:46:14 +00001890 // FIXME: Code completion for co_return.
1891 if (Tok.is(tok::code_completion) && !IsCoreturn) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001892 Actions.CodeCompleteReturn(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001893 cutOffParsing();
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001894 return StmtError();
1895 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001896
David Blaikiebbafb8a2012-03-11 07:00:24 +00001897 if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus) {
Douglas Gregore9e27d92011-03-11 23:10:44 +00001898 R = ParseInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00001899 if (R.isUsable())
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001900 Diag(R.get()->getLocStart(), getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00001901 diag::warn_cxx98_compat_generalized_initializer_lists :
1902 diag::ext_generalized_initializer_lists)
Douglas Gregore9e27d92011-03-11 23:10:44 +00001903 << R.get()->getSourceRange();
1904 } else
Nico Weber3ce01c32015-01-04 08:07:54 +00001905 R = ParseExpression();
Serge Pavlovf79bd5c2013-12-04 03:51:59 +00001906 if (R.isInvalid()) {
1907 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001908 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001909 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001910 }
Richard Smithcfd53b42015-10-22 06:13:50 +00001911 if (IsCoreturn)
Eric Fiselier20f25cb2017-03-06 23:38:15 +00001912 return Actions.ActOnCoreturnStmt(getCurScope(), ReturnLoc, R.get());
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001913 return Actions.ActOnReturnStmt(ReturnLoc, R.get(), getCurScope());
Chris Lattner503fadc2006-08-10 05:45:44 +00001914}
Chris Lattner0116c472006-08-15 06:03:28 +00001915
Alexey Bataevc4fad652016-01-13 11:18:54 +00001916StmtResult Parser::ParsePragmaLoopHint(StmtVector &Stmts,
Jonathan Roelofsce1db6d2017-03-14 17:29:33 +00001917 AllowedConstructsKind Allowed,
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001918 SourceLocation *TrailingElseLoc,
1919 ParsedAttributesWithRange &Attrs) {
1920 // Create temporary attribute list.
1921 ParsedAttributesWithRange TempAttrs(AttrFactory);
1922
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001923 // Get loop hints and consume annotated token.
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001924 while (Tok.is(tok::annot_pragma_loop_hint)) {
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001925 LoopHint Hint;
1926 if (!HandlePragmaLoopHint(Hint))
1927 continue;
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001928
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001929 ArgsUnion ArgHints[] = {Hint.PragmaNameLoc, Hint.OptionLoc, Hint.StateLoc,
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001930 ArgsUnion(Hint.ValueExpr)};
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001931 TempAttrs.addNew(Hint.PragmaNameLoc->Ident, Hint.Range, nullptr,
1932 Hint.PragmaNameLoc->Loc, ArgHints, 4,
1933 AttributeList::AS_Pragma);
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001934 }
1935
1936 // Get the next statement.
1937 MaybeParseCXX11Attributes(Attrs);
1938
1939 StmtResult S = ParseStatementOrDeclarationAfterAttributes(
Alexey Bataevc4fad652016-01-13 11:18:54 +00001940 Stmts, Allowed, TrailingElseLoc, Attrs);
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001941
1942 Attrs.takeAllFrom(TempAttrs);
1943 return S;
1944}
1945
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001946Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001947 assert(Tok.is(tok::l_brace));
1948 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001949
John McCallfaf5fb42010-08-26 23:41:50 +00001950 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1951 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001952
Alexey Bataev3d42f342015-11-20 07:02:57 +00001953 // Save and reset current vtordisp stack if we have entered a C++ method body.
1954 bool IsCXXMethod =
1955 getLangOpts().CPlusPlus && Decl && isa<CXXMethodDecl>(Decl);
Denis Zobnin2290dac2016-04-29 11:27:00 +00001956 Sema::PragmaStackSentinelRAII
1957 PragmaStackSentinel(Actions, "InternalPragmaState", IsCXXMethod);
Alexey Bataev3d42f342015-11-20 07:02:57 +00001958
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001959 // Do not enter a scope for the brace, as the arguments are in the same scope
1960 // (the function body) as the body itself. Instead, just read the statement
1961 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00001962 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001963
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001964 // If the function body could not be parsed, make a bogus compoundstmt.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001965 if (FnBody.isInvalid()) {
1966 Sema::CompoundScopeRAII CompoundScope(Actions);
Robert Wilhelm27b2c9a32013-08-19 20:51:20 +00001967 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001968 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001969
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001970 BodyScope.Exit();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001971 return Actions.ActOnFinishFunctionBody(Decl, FnBody.get());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001972}
Sebastian Redlb219c902008-12-21 16:41:36 +00001973
Sebastian Redla7b98a72009-04-26 20:35:05 +00001974/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1975///
1976/// function-try-block:
1977/// 'try' ctor-initializer[opt] compound-statement handler-seq
1978///
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001979Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00001980 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1981 SourceLocation TryLoc = ConsumeToken();
1982
John McCallfaf5fb42010-08-26 23:41:50 +00001983 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1984 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00001985
1986 // Constructor initializer list?
1987 if (Tok.is(tok::colon))
1988 ParseConstructorInitializer(Decl);
Douglas Gregor5ca153f2011-09-07 20:36:12 +00001989 else
1990 Actions.ActOnDefaultCtorInitializers(Decl);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001991
Alexey Bataev3d42f342015-11-20 07:02:57 +00001992 // Save and reset current vtordisp stack if we have entered a C++ method body.
1993 bool IsCXXMethod =
1994 getLangOpts().CPlusPlus && Decl && isa<CXXMethodDecl>(Decl);
Denis Zobnin2290dac2016-04-29 11:27:00 +00001995 Sema::PragmaStackSentinelRAII
1996 PragmaStackSentinel(Actions, "InternalPragmaState", IsCXXMethod);
Alexey Bataev3d42f342015-11-20 07:02:57 +00001997
Sebastian Redld98ecd62009-04-26 21:08:36 +00001998 SourceLocation LBraceLoc = Tok.getLocation();
David Blaikie1c9c9042012-11-10 01:04:23 +00001999 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc, /*FnTry*/true));
Sebastian Redla7b98a72009-04-26 20:35:05 +00002000 // If we failed to parse the try-catch, we just give the function an empty
2001 // compound statement as the body.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002002 if (FnBody.isInvalid()) {
2003 Sema::CompoundScopeRAII CompoundScope(Actions);
Robert Wilhelm27b2c9a32013-08-19 20:51:20 +00002004 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002005 }
Sebastian Redla7b98a72009-04-26 20:35:05 +00002006
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002007 BodyScope.Exit();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002008 return Actions.ActOnFinishFunctionBody(Decl, FnBody.get());
Sebastian Redla7b98a72009-04-26 20:35:05 +00002009}
2010
Erik Verbruggen6e922512012-04-12 10:11:59 +00002011bool Parser::trySkippingFunctionBody() {
Erik Verbruggen6e922512012-04-12 10:11:59 +00002012 assert(SkipFunctionBodies &&
2013 "Should only be called when SkipFunctionBodies is enabled");
Argyrios Kyrtzidis289e4a32012-10-31 17:29:28 +00002014 if (!PP.isCodeCompletionEnabled()) {
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002015 SkipFunctionBody();
Argyrios Kyrtzidis289e4a32012-10-31 17:29:28 +00002016 return true;
2017 }
2018
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002019 // We're in code-completion mode. Skip parsing for all function bodies unless
2020 // the body contains the code-completion point.
2021 TentativeParsingAction PA(*this);
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002022 bool IsTryCatch = Tok.is(tok::kw_try);
2023 CachedTokens Toks;
2024 bool ErrorInPrologue = ConsumeAndStoreFunctionPrologue(Toks);
2025 if (llvm::any_of(Toks, [](const Token &Tok) {
2026 return Tok.is(tok::code_completion);
2027 })) {
2028 PA.Revert();
2029 return false;
2030 }
2031 if (ErrorInPrologue) {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002032 PA.Commit();
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002033 SkipMalformedDecl();
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002034 return true;
2035 }
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002036 if (!SkipUntil(tok::r_brace, StopAtCodeCompletion)) {
2037 PA.Revert();
2038 return false;
2039 }
2040 while (IsTryCatch && Tok.is(tok::kw_catch)) {
2041 if (!SkipUntil(tok::l_brace, StopAtCodeCompletion) ||
2042 !SkipUntil(tok::r_brace, StopAtCodeCompletion)) {
2043 PA.Revert();
2044 return false;
2045 }
2046 }
2047 PA.Commit();
2048 return true;
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002049}
2050
Sebastian Redlb219c902008-12-21 16:41:36 +00002051/// ParseCXXTryBlock - Parse a C++ try-block.
2052///
2053/// try-block:
2054/// 'try' compound-statement handler-seq
2055///
Richard Smithc202b282012-04-14 00:33:13 +00002056StmtResult Parser::ParseCXXTryBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00002057 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2058
2059 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00002060 return ParseCXXTryBlockCommon(TryLoc);
2061}
2062
2063/// ParseCXXTryBlockCommon - Parse the common part of try-block and
2064/// function-try-block.
2065///
2066/// try-block:
2067/// 'try' compound-statement handler-seq
2068///
2069/// function-try-block:
2070/// 'try' ctor-initializer[opt] compound-statement handler-seq
2071///
2072/// handler-seq:
2073/// handler handler-seq[opt]
2074///
John Wiegley1c0675e2011-04-28 01:08:34 +00002075/// [Borland] try-block:
2076/// 'try' compound-statement seh-except-block
Alp Tokerf6a24ce2013-12-05 16:25:25 +00002077/// 'try' compound-statement seh-finally-block
John Wiegley1c0675e2011-04-28 01:08:34 +00002078///
David Blaikie1c9c9042012-11-10 01:04:23 +00002079StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc, bool FnTry) {
Sebastian Redlb219c902008-12-21 16:41:36 +00002080 if (Tok.isNot(tok::l_brace))
Alp Tokerec543272013-12-24 09:48:30 +00002081 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
Richard Smithc202b282012-04-14 00:33:13 +00002082
Warren Huntf6be4cb2014-07-25 20:52:51 +00002083 StmtResult TryBlock(ParseCompoundStatement(/*isStmtExpr=*/false,
2084 Scope::DeclScope | Scope::TryScope |
2085 (FnTry ? Scope::FnTryCatchScope : 0)));
Sebastian Redlb219c902008-12-21 16:41:36 +00002086 if (TryBlock.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002087 return TryBlock;
Sebastian Redlb219c902008-12-21 16:41:36 +00002088
John Wiegley1c0675e2011-04-28 01:08:34 +00002089 // Borland allows SEH-handlers with 'try'
Chad Rosier67055f52012-07-10 21:35:27 +00002090
Richard Smithc202b282012-04-14 00:33:13 +00002091 if ((Tok.is(tok::identifier) &&
2092 Tok.getIdentifierInfo() == getSEHExceptKeyword()) ||
2093 Tok.is(tok::kw___finally)) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002094 // TODO: Factor into common return ParseSEHHandlerCommon(...)
2095 StmtResult Handler;
Douglas Gregor60060d62011-10-21 03:57:52 +00002096 if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002097 SourceLocation Loc = ConsumeToken();
2098 Handler = ParseSEHExceptBlock(Loc);
2099 }
2100 else {
2101 SourceLocation Loc = ConsumeToken();
2102 Handler = ParseSEHFinallyBlock(Loc);
2103 }
2104 if(Handler.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002105 return Handler;
John McCall53fa7142010-12-24 02:08:15 +00002106
John Wiegley1c0675e2011-04-28 01:08:34 +00002107 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
2108 TryLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002109 TryBlock.get(),
Warren Huntf6be4cb2014-07-25 20:52:51 +00002110 Handler.get());
Sebastian Redlb219c902008-12-21 16:41:36 +00002111 }
John Wiegley1c0675e2011-04-28 01:08:34 +00002112 else {
Benjamin Kramerf0623432012-08-23 22:51:59 +00002113 StmtVector Handlers;
Richard Smithc2c8bb82013-10-15 01:34:54 +00002114
2115 // C++11 attributes can't appear here, despite this context seeming
2116 // statement-like.
2117 DiagnoseAndSkipCXX11Attributes();
Sebastian Redlb219c902008-12-21 16:41:36 +00002118
John Wiegley1c0675e2011-04-28 01:08:34 +00002119 if (Tok.isNot(tok::kw_catch))
2120 return StmtError(Diag(Tok, diag::err_expected_catch));
2121 while (Tok.is(tok::kw_catch)) {
David Blaikie1c9c9042012-11-10 01:04:23 +00002122 StmtResult Handler(ParseCXXCatchBlock(FnTry));
John Wiegley1c0675e2011-04-28 01:08:34 +00002123 if (!Handler.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002124 Handlers.push_back(Handler.get());
John Wiegley1c0675e2011-04-28 01:08:34 +00002125 }
2126 // Don't bother creating the full statement if we don't have any usable
2127 // handlers.
2128 if (Handlers.empty())
2129 return StmtError();
2130
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002131 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.get(), Handlers);
John Wiegley1c0675e2011-04-28 01:08:34 +00002132 }
Sebastian Redlb219c902008-12-21 16:41:36 +00002133}
2134
2135/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
2136///
Richard Smith1dba27c2013-01-29 09:02:09 +00002137/// handler:
2138/// 'catch' '(' exception-declaration ')' compound-statement
Sebastian Redlb219c902008-12-21 16:41:36 +00002139///
Richard Smith1dba27c2013-01-29 09:02:09 +00002140/// exception-declaration:
2141/// attribute-specifier-seq[opt] type-specifier-seq declarator
2142/// attribute-specifier-seq[opt] type-specifier-seq abstract-declarator[opt]
2143/// '...'
Sebastian Redlb219c902008-12-21 16:41:36 +00002144///
David Blaikie1c9c9042012-11-10 01:04:23 +00002145StmtResult Parser::ParseCXXCatchBlock(bool FnCatch) {
Sebastian Redlb219c902008-12-21 16:41:36 +00002146 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
2147
2148 SourceLocation CatchLoc = ConsumeToken();
2149
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002150 BalancedDelimiterTracker T(*this, tok::l_paren);
Alp Toker383d2c42014-01-01 03:08:43 +00002151 if (T.expectAndConsume())
Sebastian Redlb219c902008-12-21 16:41:36 +00002152 return StmtError();
2153
2154 // C++ 3.3.2p3:
2155 // The name in a catch exception-declaration is local to the handler and
2156 // shall not be redeclared in the outermost block of the handler.
David Blaikie1c9c9042012-11-10 01:04:23 +00002157 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope |
David Blaikie3403feb2012-11-13 18:51:45 +00002158 (FnCatch ? Scope::FnTryCatchScope : 0));
Sebastian Redlb219c902008-12-21 16:41:36 +00002159
2160 // exception-declaration is equivalent to '...' or a parameter-declaration
2161 // without default arguments.
Craig Topper161e4db2014-05-21 06:02:52 +00002162 Decl *ExceptionDecl = nullptr;
Sebastian Redlb219c902008-12-21 16:41:36 +00002163 if (Tok.isNot(tok::ellipsis)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002164 ParsedAttributesWithRange Attributes(AttrFactory);
2165 MaybeParseCXX11Attributes(Attributes);
2166
John McCall084e83d2011-03-24 11:26:52 +00002167 DeclSpec DS(AttrFactory);
Richard Smith1dba27c2013-01-29 09:02:09 +00002168 DS.takeAttributesFrom(Attributes);
2169
Sebastian Redl54c04d42008-12-22 19:15:10 +00002170 if (ParseCXXTypeSpecifierSeq(DS))
2171 return StmtError();
Richard Smith1dba27c2013-01-29 09:02:09 +00002172
Sebastian Redlb219c902008-12-21 16:41:36 +00002173 Declarator ExDecl(DS, Declarator::CXXCatchContext);
2174 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002175 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00002176 } else
2177 ConsumeToken();
2178
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002179 T.consumeClose();
2180 if (T.getCloseLocation().isInvalid())
Sebastian Redlb219c902008-12-21 16:41:36 +00002181 return StmtError();
2182
2183 if (Tok.isNot(tok::l_brace))
Alp Tokerec543272013-12-24 09:48:30 +00002184 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
Sebastian Redlb219c902008-12-21 16:41:36 +00002185
Alexis Hunt96d5c762009-11-21 08:43:09 +00002186 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
Richard Smithc202b282012-04-14 00:33:13 +00002187 StmtResult Block(ParseCompoundStatement());
Sebastian Redlb219c902008-12-21 16:41:36 +00002188 if (Block.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002189 return Block;
Sebastian Redlb219c902008-12-21 16:41:36 +00002190
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002191 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.get());
Sebastian Redlb219c902008-12-21 16:41:36 +00002192}
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002193
2194void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Douglas Gregor43edb322011-10-24 22:31:10 +00002195 IfExistsCondition Result;
Francois Picheta5b3fcb2011-05-07 17:30:27 +00002196 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002197 return;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00002198
Douglas Gregor43edb322011-10-24 22:31:10 +00002199 // Handle dependent statements by parsing the braces as a compound statement.
2200 // This is not the same behavior as Visual C++, which don't treat this as a
2201 // compound statement, but for Clang's type checking we can't have anything
2202 // inside these braces escaping to the surrounding code.
2203 if (Result.Behavior == IEB_Dependent) {
2204 if (!Tok.is(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002205 Diag(Tok, diag::err_expected) << tok::l_brace;
Richard Smithc202b282012-04-14 00:33:13 +00002206 return;
Douglas Gregor43edb322011-10-24 22:31:10 +00002207 }
Richard Smithc202b282012-04-14 00:33:13 +00002208
2209 StmtResult Compound = ParseCompoundStatement();
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002210 if (Compound.isInvalid())
2211 return;
Richard Smithc202b282012-04-14 00:33:13 +00002212
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002213 StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc,
2214 Result.IsIfExists,
Richard Smithc202b282012-04-14 00:33:13 +00002215 Result.SS,
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002216 Result.Name,
2217 Compound.get());
2218 if (DepResult.isUsable())
2219 Stmts.push_back(DepResult.get());
Douglas Gregor43edb322011-10-24 22:31:10 +00002220 return;
2221 }
Richard Smithc202b282012-04-14 00:33:13 +00002222
Douglas Gregor43edb322011-10-24 22:31:10 +00002223 BalancedDelimiterTracker Braces(*this, tok::l_brace);
2224 if (Braces.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +00002225 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002226 return;
2227 }
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002228
Douglas Gregor43edb322011-10-24 22:31:10 +00002229 switch (Result.Behavior) {
2230 case IEB_Parse:
2231 // Parse the statements below.
2232 break;
Chad Rosier67055f52012-07-10 21:35:27 +00002233
Douglas Gregor43edb322011-10-24 22:31:10 +00002234 case IEB_Dependent:
2235 llvm_unreachable("Dependent case handled above");
Chad Rosier67055f52012-07-10 21:35:27 +00002236
Douglas Gregor43edb322011-10-24 22:31:10 +00002237 case IEB_Skip:
2238 Braces.skipToEnd();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002239 return;
2240 }
2241
2242 // Condition is true, parse the statements.
2243 while (Tok.isNot(tok::r_brace)) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00002244 StmtResult R = ParseStatementOrDeclaration(Stmts, ACK_Any);
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002245 if (R.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002246 Stmts.push_back(R.get());
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002247 }
Douglas Gregor43edb322011-10-24 22:31:10 +00002248 Braces.consumeClose();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002249}
Anastasia Stulova6bdbcbb2016-02-19 18:30:11 +00002250
2251bool Parser::ParseOpenCLUnrollHintAttribute(ParsedAttributes &Attrs) {
2252 MaybeParseGNUAttributes(Attrs);
2253
2254 if (Attrs.empty())
2255 return true;
2256
2257 if (Attrs.getList()->getKind() != AttributeList::AT_OpenCLUnrollHint)
2258 return true;
2259
2260 if (!(Tok.is(tok::kw_for) || Tok.is(tok::kw_while) || Tok.is(tok::kw_do))) {
2261 Diag(Tok, diag::err_opencl_unroll_hint_on_non_loop);
2262 return false;
2263 }
2264 return true;
2265}