blob: 574293f5181c37886e0c2ed90524984f16962f3e [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
Jordan Rose1e879d82018-03-23 00:07:18 +000015#include "clang/AST/PrettyDeclStackTrace.h"
Aaron Ballmanb06b15a2014-06-06 12:40:24 +000016#include "clang/Basic/Attributes.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/Basic/PrettyStackTrace.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000018#include "clang/Parse/Parser.h"
Vassil Vassilev11ad3392017-03-23 15:11:07 +000019#include "clang/Parse/RAIIObjectsForParser.h"
John McCall8b0666c2010-08-20 18:27:03 +000020#include "clang/Sema/DeclSpec.h"
Aaron Ballmanb06b15a2014-06-06 12:40:24 +000021#include "clang/Sema/LoopHint.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
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000030/// Parse a standalone statement (for instance, as the body of an 'if',
Richard Smith426a47b2013-10-28 22:04:30 +000031/// '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
Erich Keanec480f302018-07-12 21:09:05 +0000119 return Actions.ProcessStmtAttributes(Res.get(), Attrs, Attrs.Range);
Richard Smithc202b282012-04-14 00:33:13 +0000120}
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
Galina Kistanova387ab8b2017-06-01 21:28:26 +0000206 LLVM_FALLTHROUGH;
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000207 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000208
Chris Lattner803802d2009-03-24 17:04:48 +0000209 default: {
David Majnemer6ac7dd12016-08-01 16:39:29 +0000210 if ((getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt ||
211 Allowed == ACK_Any) &&
Alexey Bataevc4fad652016-01-13 11:18:54 +0000212 isDeclarationStatement()) {
Chris Lattner49836b42009-04-02 04:16:50 +0000213 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Faisal Vali421b2d12017-12-29 05:41:00 +0000214 DeclGroupPtrTy Decl = ParseDeclaration(DeclaratorContext::BlockContext,
Richard Smithc202b282012-04-14 00:33:13 +0000215 DeclEnd, Attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000216 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattner803802d2009-03-24 17:04:48 +0000217 }
218
219 if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +0000220 Diag(Tok, diag::err_expected_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000221 return StmtError();
Chris Lattnerf8afb622006-08-10 18:26:31 +0000222 }
Mike Stump11289f42009-09-09 15:08:12 +0000223
Richard Smithc202b282012-04-14 00:33:13 +0000224 return ParseExprStatement();
Chris Lattner803802d2009-03-24 17:04:48 +0000225 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000226
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000227 case tok::kw_case: // C99 6.8.1: labeled-statement
Richard Smithc202b282012-04-14 00:33:13 +0000228 return ParseCaseStatement();
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000229 case tok::kw_default: // C99 6.8.1: labeled-statement
Richard Smithc202b282012-04-14 00:33:13 +0000230 return ParseDefaultStatement();
Sebastian Redl042ad952008-12-11 19:30:53 +0000231
Chris Lattner9075bd72006-08-10 04:59:57 +0000232 case tok::l_brace: // C99 6.8.2: compound-statement
Richard Smithc202b282012-04-14 00:33:13 +0000233 return ParseCompoundStatement();
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000234 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
Argyrios Kyrtzidis43ea78b2011-09-01 21:53:45 +0000235 bool HasLeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
236 return Actions.ActOnNullStmt(ConsumeToken(), HasLeadingEmptyMacro);
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000237 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000238
Chris Lattner9075bd72006-08-10 04:59:57 +0000239 case tok::kw_if: // C99 6.8.4.1: if-statement
Richard Smithc202b282012-04-14 00:33:13 +0000240 return ParseIfStatement(TrailingElseLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000241 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Richard Smithc202b282012-04-14 00:33:13 +0000242 return ParseSwitchStatement(TrailingElseLoc);
Sebastian Redl042ad952008-12-11 19:30:53 +0000243
Chris Lattner9075bd72006-08-10 04:59:57 +0000244 case tok::kw_while: // C99 6.8.5.1: while-statement
Richard Smithc202b282012-04-14 00:33:13 +0000245 return ParseWhileStatement(TrailingElseLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000246 case tok::kw_do: // C99 6.8.5.2: do-statement
Richard Smithc202b282012-04-14 00:33:13 +0000247 Res = ParseDoStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000248 SemiError = "do/while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000249 break;
250 case tok::kw_for: // C99 6.8.5.3: for-statement
Richard Smithc202b282012-04-14 00:33:13 +0000251 return ParseForStatement(TrailingElseLoc);
Chris Lattner503fadc2006-08-10 05:45:44 +0000252
253 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Richard Smithc202b282012-04-14 00:33:13 +0000254 Res = ParseGotoStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000255 SemiError = "goto";
Chris Lattner9075bd72006-08-10 04:59:57 +0000256 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000257 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Richard Smithc202b282012-04-14 00:33:13 +0000258 Res = ParseContinueStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000259 SemiError = "continue";
Chris Lattner503fadc2006-08-10 05:45:44 +0000260 break;
261 case tok::kw_break: // C99 6.8.6.3: break-statement
Richard Smithc202b282012-04-14 00:33:13 +0000262 Res = ParseBreakStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000263 SemiError = "break";
Chris Lattner503fadc2006-08-10 05:45:44 +0000264 break;
265 case tok::kw_return: // C99 6.8.6.4: return-statement
Richard Smithc202b282012-04-14 00:33:13 +0000266 Res = ParseReturnStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000267 SemiError = "return";
Chris Lattner503fadc2006-08-10 05:45:44 +0000268 break;
Richard Smith0e304ea2015-10-22 04:46:14 +0000269 case tok::kw_co_return: // C++ Coroutines: co_return statement
270 Res = ParseReturnStatement();
271 SemiError = "co_return";
272 break;
Sebastian Redl042ad952008-12-11 19:30:53 +0000273
Sebastian Redlb219c902008-12-21 16:41:36 +0000274 case tok::kw_asm: {
Richard Smithc202b282012-04-14 00:33:13 +0000275 ProhibitAttributes(Attrs);
Steve Naroffb2c80c72008-02-07 03:50:06 +0000276 bool msAsm = false;
277 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidis3050d9b2010-11-02 02:33:08 +0000278 Res = Actions.ActOnFinishFullStmt(Res.get());
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000279 if (msAsm) return Res;
Chris Lattner34a95662009-06-14 00:07:48 +0000280 SemiError = "asm";
Chris Lattner0116c472006-08-15 06:03:28 +0000281 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000282 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000283
Reid Kleckner6d8d22a2014-06-25 00:28:35 +0000284 case tok::kw___if_exists:
285 case tok::kw___if_not_exists:
286 ProhibitAttributes(Attrs);
287 ParseMicrosoftIfExistsStatement(Stmts);
288 // An __if_exists block is like a compound statement, but it doesn't create
289 // a new scope.
290 return StmtEmpty();
291
Sebastian Redlb219c902008-12-21 16:41:36 +0000292 case tok::kw_try: // C++ 15: try-block
Richard Smithc202b282012-04-14 00:33:13 +0000293 return ParseCXXTryBlock();
John Wiegley1c0675e2011-04-28 01:08:34 +0000294
295 case tok::kw___try:
Richard Smithc202b282012-04-14 00:33:13 +0000296 ProhibitAttributes(Attrs); // TODO: is it correct?
297 return ParseSEHTryBlock();
Eli Friedmanec52f922012-02-23 23:47:16 +0000298
Nico Weberc7d05962014-07-06 22:32:59 +0000299 case tok::kw___leave:
300 Res = ParseSEHLeaveStatement();
301 SemiError = "__leave";
302 break;
303
Eli Friedmanec52f922012-02-23 23:47:16 +0000304 case tok::annot_pragma_vis:
Richard Smithc202b282012-04-14 00:33:13 +0000305 ProhibitAttributes(Attrs);
Eli Friedmanec52f922012-02-23 23:47:16 +0000306 HandlePragmaVisibility();
307 return StmtEmpty();
308
309 case tok::annot_pragma_pack:
Richard Smithc202b282012-04-14 00:33:13 +0000310 ProhibitAttributes(Attrs);
Eli Friedmanec52f922012-02-23 23:47:16 +0000311 HandlePragmaPack();
312 return StmtEmpty();
Eli Friedman68be1642012-10-04 02:36:51 +0000313
Eli Friedmanbbbbac62012-10-09 22:46:54 +0000314 case tok::annot_pragma_msstruct:
315 ProhibitAttributes(Attrs);
316 HandlePragmaMSStruct();
317 return StmtEmpty();
318
Eli Friedmanae8ee252012-10-08 23:52:38 +0000319 case tok::annot_pragma_align:
320 ProhibitAttributes(Attrs);
321 HandlePragmaAlign();
322 return StmtEmpty();
323
Eli Friedmanbbbbac62012-10-09 22:46:54 +0000324 case tok::annot_pragma_weak:
325 ProhibitAttributes(Attrs);
326 HandlePragmaWeak();
327 return StmtEmpty();
328
329 case tok::annot_pragma_weakalias:
330 ProhibitAttributes(Attrs);
331 HandlePragmaWeakAlias();
332 return StmtEmpty();
333
334 case tok::annot_pragma_redefine_extname:
335 ProhibitAttributes(Attrs);
336 HandlePragmaRedefineExtname();
337 return StmtEmpty();
338
Eli Friedman68be1642012-10-04 02:36:51 +0000339 case tok::annot_pragma_fp_contract:
Richard Smithca9b0b62013-11-15 21:10:54 +0000340 ProhibitAttributes(Attrs);
Lang Hamesa930e712012-10-21 01:10:01 +0000341 Diag(Tok, diag::err_pragma_fp_contract_scope);
Richard Smithaf3b3252017-05-18 19:21:48 +0000342 ConsumeAnnotationToken();
Lang Hamesa930e712012-10-21 01:10:01 +0000343 return StmtError();
344
Adam Nemet60d32642017-04-04 21:18:36 +0000345 case tok::annot_pragma_fp:
346 ProhibitAttributes(Attrs);
347 Diag(Tok, diag::err_pragma_fp_scope);
Richard Smithaf3b3252017-05-18 19:21:48 +0000348 ConsumeAnnotationToken();
Adam Nemet60d32642017-04-04 21:18:36 +0000349 return StmtError();
350
Kevin P. Neal2c0bc8b2018-08-14 17:06:56 +0000351 case tok::annot_pragma_fenv_access:
352 ProhibitAttributes(Attrs);
353 HandlePragmaFEnvAccess();
354 return StmtEmpty();
355
Eli Friedman68be1642012-10-04 02:36:51 +0000356 case tok::annot_pragma_opencl_extension:
357 ProhibitAttributes(Attrs);
358 HandlePragmaOpenCLExtension();
359 return StmtEmpty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000360
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000361 case tok::annot_pragma_captured:
Richard Smith12a41bd2013-09-16 21:17:44 +0000362 ProhibitAttributes(Attrs);
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000363 return HandlePragmaCaptured();
364
Alexey Bataeva769e072013-03-22 06:34:35 +0000365 case tok::annot_pragma_openmp:
Richard Smith12a41bd2013-09-16 21:17:44 +0000366 ProhibitAttributes(Attrs);
Alexey Bataevc4fad652016-01-13 11:18:54 +0000367 return ParseOpenMPDeclarativeOrExecutableDirective(Allowed);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000368
David Majnemer4bb09802014-02-10 19:50:15 +0000369 case tok::annot_pragma_ms_pointers_to_members:
370 ProhibitAttributes(Attrs);
371 HandlePragmaMSPointersToMembers();
372 return StmtEmpty();
373
Warren Huntc3b18962014-04-08 22:30:47 +0000374 case tok::annot_pragma_ms_pragma:
375 ProhibitAttributes(Attrs);
376 HandlePragmaMSPragma();
377 return StmtEmpty();
Aaron Ballmanb06b15a2014-06-06 12:40:24 +0000378
Alexey Bataev3d42f342015-11-20 07:02:57 +0000379 case tok::annot_pragma_ms_vtordisp:
380 ProhibitAttributes(Attrs);
381 HandlePragmaMSVtorDisp();
382 return StmtEmpty();
383
Aaron Ballmanb06b15a2014-06-06 12:40:24 +0000384 case tok::annot_pragma_loop_hint:
385 ProhibitAttributes(Attrs);
Alexey Bataevc4fad652016-01-13 11:18:54 +0000386 return ParsePragmaLoopHint(Stmts, Allowed, TrailingElseLoc, Attrs);
Richard Smithba3a4f92016-01-12 21:59:26 +0000387
388 case tok::annot_pragma_dump:
389 HandlePragmaDump();
390 return StmtEmpty();
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000391
392 case tok::annot_pragma_attribute:
393 HandlePragmaAttribute();
394 return StmtEmpty();
Sebastian Redlb219c902008-12-21 16:41:36 +0000395 }
396
Chris Lattner503fadc2006-08-10 05:45:44 +0000397 // If we reached this code, the statement must end in a semicolon.
Alp Toker97650562014-01-10 11:19:30 +0000398 if (!TryConsumeToken(tok::semi) && !Res.isInvalid()) {
Chris Lattner8e3eed02009-06-14 00:23:56 +0000399 // If the result was valid, then we do want to diagnose this. Use
400 // ExpectAndConsume to emit the diagnostic, even though we know it won't
401 // succeed.
402 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner0046de12008-11-13 18:52:53 +0000403 // Skip until we see a } or ;, but don't eat it.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000404 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner503fadc2006-08-10 05:45:44 +0000405 }
Mike Stump11289f42009-09-09 15:08:12 +0000406
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000407 return Res;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000408}
409
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000410/// Parse an expression statement.
Richard Smithc202b282012-04-14 00:33:13 +0000411StmtResult Parser::ParseExprStatement() {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000412 // If a case keyword is missing, this is where it should be inserted.
413 Token OldToken = Tok;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000414
Alex Lorenzf0b4e5d2016-10-18 10:55:01 +0000415 ExprStatementTokLoc = Tok.getLocation();
416
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000417 // expression[opt] ';'
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000418 ExprResult Expr(ParseExpression());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000419 if (Expr.isInvalid()) {
420 // If the expression is invalid, skip ahead to the next semicolon or '}'.
421 // Not doing this opens us up to the possibility of infinite loops if
422 // ParseExpression does not consume any tokens.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000423 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000424 if (Tok.is(tok::semi))
425 ConsumeToken();
John McCalleaef89b2013-03-22 02:10:40 +0000426 return Actions.ActOnExprStmtError();
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000427 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000428
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000429 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
430 Actions.CheckCaseExpression(Expr.get())) {
431 // If a constant expression is followed by a colon inside a switch block,
432 // suggest a missing case keyword.
433 Diag(OldToken, diag::err_expected_case_before_expression)
434 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000435
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000436 // Recover parsing as a case statement.
Richard Smithc202b282012-04-14 00:33:13 +0000437 return ParseCaseStatement(/*MissingCase=*/true, Expr);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000438 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000439
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000440 // Otherwise, eat the semicolon.
441 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +0000442 return Actions.ActOnExprStmt(Expr);
John Wiegley1c0675e2011-04-28 01:08:34 +0000443}
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000444
John Wiegley1c0675e2011-04-28 01:08:34 +0000445/// ParseSEHTryBlockCommon
446///
447/// seh-try-block:
448/// '__try' compound-statement seh-handler
449///
450/// seh-handler:
451/// seh-except-block
452/// seh-finally-block
453///
Nico Weberdd256742015-02-25 01:43:27 +0000454StmtResult Parser::ParseSEHTryBlock() {
455 assert(Tok.is(tok::kw___try) && "Expected '__try'");
456 SourceLocation TryLoc = ConsumeToken();
457
Nico Weberfc3fe4f2015-02-25 02:22:06 +0000458 if (Tok.isNot(tok::l_brace))
Alp Tokerec543272013-12-24 09:48:30 +0000459 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
John Wiegley1c0675e2011-04-28 01:08:34 +0000460
Momchil Velikov57c681f2017-08-10 15:43:06 +0000461 StmtResult TryBlock(ParseCompoundStatement(
462 /*isStmtExpr=*/false,
463 Scope::DeclScope | Scope::CompoundStmtScope | Scope::SEHTryScope));
464 if (TryBlock.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000465 return TryBlock;
John Wiegley1c0675e2011-04-28 01:08:34 +0000466
467 StmtResult Handler;
Richard Smithc202b282012-04-14 00:33:13 +0000468 if (Tok.is(tok::identifier) &&
Douglas Gregor60060d62011-10-21 03:57:52 +0000469 Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +0000470 SourceLocation Loc = ConsumeToken();
471 Handler = ParseSEHExceptBlock(Loc);
472 } else if (Tok.is(tok::kw___finally)) {
473 SourceLocation Loc = ConsumeToken();
474 Handler = ParseSEHFinallyBlock(Loc);
475 } else {
Nico Weberfc3fe4f2015-02-25 02:22:06 +0000476 return StmtError(Diag(Tok, diag::err_seh_expected_handler));
John Wiegley1c0675e2011-04-28 01:08:34 +0000477 }
478
479 if(Handler.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000480 return Handler;
John Wiegley1c0675e2011-04-28 01:08:34 +0000481
482 return Actions.ActOnSEHTryBlock(false /* IsCXXTry */,
483 TryLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000484 TryBlock.get(),
Warren Huntf6be4cb2014-07-25 20:52:51 +0000485 Handler.get());
John Wiegley1c0675e2011-04-28 01:08:34 +0000486}
487
488/// ParseSEHExceptBlock - Handle __except
489///
490/// seh-except-block:
491/// '__except' '(' seh-filter-expression ')' compound-statement
492///
493StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) {
494 PoisonIdentifierRAIIObject raii(Ident__exception_code, false),
495 raii2(Ident___exception_code, false),
496 raii3(Ident_GetExceptionCode, false);
497
Alp Toker383d2c42014-01-01 03:08:43 +0000498 if (ExpectAndConsume(tok::l_paren))
John Wiegley1c0675e2011-04-28 01:08:34 +0000499 return StmtError();
500
Reid Kleckner1d59f992015-01-22 01:36:17 +0000501 ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope |
502 Scope::SEHExceptScope);
John Wiegley1c0675e2011-04-28 01:08:34 +0000503
David Blaikiebbafb8a2012-03-11 07:00:24 +0000504 if (getLangOpts().Borland) {
Francois Pichetbfaf4772011-04-28 03:14:31 +0000505 Ident__exception_info->setIsPoisoned(false);
506 Ident___exception_info->setIsPoisoned(false);
507 Ident_GetExceptionInfo->setIsPoisoned(false);
508 }
Reid Kleckner1d59f992015-01-22 01:36:17 +0000509
510 ExprResult FilterExpr;
511 {
512 ParseScopeFlags FilterScope(this, getCurScope()->getFlags() |
513 Scope::SEHFilterScope);
Reid Kleckner85368fb2015-04-02 22:09:32 +0000514 FilterExpr = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Reid Kleckner1d59f992015-01-22 01:36:17 +0000515 }
Francois Pichetbfaf4772011-04-28 03:14:31 +0000516
David Blaikiebbafb8a2012-03-11 07:00:24 +0000517 if (getLangOpts().Borland) {
Francois Pichetbfaf4772011-04-28 03:14:31 +0000518 Ident__exception_info->setIsPoisoned(true);
519 Ident___exception_info->setIsPoisoned(true);
520 Ident_GetExceptionInfo->setIsPoisoned(true);
521 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000522
523 if(FilterExpr.isInvalid())
524 return StmtError();
525
Alp Toker383d2c42014-01-01 03:08:43 +0000526 if (ExpectAndConsume(tok::r_paren))
John Wiegley1c0675e2011-04-28 01:08:34 +0000527 return StmtError();
528
Nico Weberfc3fe4f2015-02-25 02:22:06 +0000529 if (Tok.isNot(tok::l_brace))
530 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
531
Richard Smithc202b282012-04-14 00:33:13 +0000532 StmtResult Block(ParseCompoundStatement());
John Wiegley1c0675e2011-04-28 01:08:34 +0000533
534 if(Block.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000535 return Block;
John Wiegley1c0675e2011-04-28 01:08:34 +0000536
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000537 return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.get(), Block.get());
John Wiegley1c0675e2011-04-28 01:08:34 +0000538}
539
540/// ParseSEHFinallyBlock - Handle __finally
541///
542/// seh-finally-block:
543/// '__finally' compound-statement
544///
Nico Weberd64657f2015-03-09 02:47:59 +0000545StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyLoc) {
John Wiegley1c0675e2011-04-28 01:08:34 +0000546 PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
547 raii2(Ident___abnormal_termination, false),
548 raii3(Ident_AbnormalTermination, false);
549
Nico Weberfc3fe4f2015-02-25 02:22:06 +0000550 if (Tok.isNot(tok::l_brace))
551 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
552
Nico Weberd64657f2015-03-09 02:47:59 +0000553 ParseScope FinallyScope(this, 0);
554 Actions.ActOnStartSEHFinallyBlock();
555
Richard Smithc202b282012-04-14 00:33:13 +0000556 StmtResult Block(ParseCompoundStatement());
Nico Weberce903292015-03-09 03:17:15 +0000557 if(Block.isInvalid()) {
558 Actions.ActOnAbortSEHFinallyBlock();
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000559 return Block;
Nico Weberce903292015-03-09 03:17:15 +0000560 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000561
Nico Weberd64657f2015-03-09 02:47:59 +0000562 return Actions.ActOnFinishSEHFinallyBlock(FinallyLoc, Block.get());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000563}
564
Nico Weberc7d05962014-07-06 22:32:59 +0000565/// Handle __leave
566///
567/// seh-leave-statement:
568/// '__leave' ';'
569///
570StmtResult Parser::ParseSEHLeaveStatement() {
571 SourceLocation LeaveLoc = ConsumeToken(); // eat the '__leave'.
572 return Actions.ActOnSEHLeaveStmt(LeaveLoc, getCurScope());
573}
574
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000575/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000576///
577/// labeled-statement:
578/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000579/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000580///
Richard Smithc202b282012-04-14 00:33:13 +0000581StmtResult Parser::ParseLabeledStatement(ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000582 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
583 "Not an identifier!");
584
585 Token IdentTok = Tok; // Save the whole token.
586 ConsumeToken(); // eat the identifier.
587
588 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000589
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000590 // identifier ':' statement
591 SourceLocation ColonLoc = ConsumeToken();
592
Richard Smitha3e01cf2013-11-15 22:45:29 +0000593 // Read label attributes, if present.
594 StmtResult SubStmt;
595 if (Tok.is(tok::kw___attribute)) {
596 ParsedAttributesWithRange TempAttrs(AttrFactory);
597 ParseGNUAttributes(TempAttrs);
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000598
Richard Smitha3e01cf2013-11-15 22:45:29 +0000599 // In C++, GNU attributes only apply to the label if they are followed by a
600 // semicolon, to disambiguate label attributes from attributes on a labeled
601 // declaration.
602 //
603 // This doesn't quite match what GCC does; if the attribute list is empty
604 // and followed by a semicolon, GCC will reject (it appears to parse the
605 // attributes as part of a statement in that case). That looks like a bug.
606 if (!getLangOpts().CPlusPlus || Tok.is(tok::semi))
607 attrs.takeAllFrom(TempAttrs);
608 else if (isDeclarationStatement()) {
609 StmtVector Stmts;
610 // FIXME: We should do this whether or not we have a declaration
611 // statement, but that doesn't work correctly (because ProhibitAttributes
612 // can't handle GNU attributes), so only call it in the one case where
613 // GNU attributes are allowed.
614 SubStmt = ParseStatementOrDeclarationAfterAttributes(
Alexey Bataevc4fad652016-01-13 11:18:54 +0000615 Stmts, /*Allowed=*/ACK_StatementsOpenMPNonStandalone, nullptr,
616 TempAttrs);
Richard Smitha3e01cf2013-11-15 22:45:29 +0000617 if (!TempAttrs.empty() && !SubStmt.isInvalid())
Erich Keanec480f302018-07-12 21:09:05 +0000618 SubStmt = Actions.ProcessStmtAttributes(SubStmt.get(), TempAttrs,
619 TempAttrs.Range);
Richard Smitha3e01cf2013-11-15 22:45:29 +0000620 } else {
Alp Toker383d2c42014-01-01 03:08:43 +0000621 Diag(Tok, diag::err_expected_after) << "__attribute__" << tok::semi;
Richard Smitha3e01cf2013-11-15 22:45:29 +0000622 }
623 }
624
625 // If we've not parsed a statement yet, parse one now.
626 if (!SubStmt.isInvalid() && !SubStmt.isUsable())
627 SubStmt = ParseStatement();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000628
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000629 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000630 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000631 SubStmt = Actions.ActOnNullStmt(ColonLoc);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000632
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000633 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
634 IdentTok.getLocation());
Erich Keanec480f302018-07-12 21:09:05 +0000635 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, attrs);
636 attrs.clear();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000637
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000638 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
639 SubStmt.get());
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000640}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000641
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000642/// ParseCaseStatement
643/// labeled-statement:
644/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000645/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000646///
Richard Smithc202b282012-04-14 00:33:13 +0000647StmtResult Parser::ParseCaseStatement(bool MissingCase, ExprResult Expr) {
Richard Smithd4257d82011-04-21 22:48:40 +0000648 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Mike Stump11289f42009-09-09 15:08:12 +0000649
Chris Lattner34a22092009-03-04 04:23:07 +0000650 // It is very very common for code to contain many case statements recursively
651 // nested, as in (but usually without indentation):
652 // case 1:
653 // case 2:
654 // case 3:
655 // case 4:
656 // case 5: etc.
657 //
658 // Parsing this naively works, but is both inefficient and can cause us to run
659 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner2b19a6582009-03-04 18:24:58 +0000660 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner34a22092009-03-04 04:23:07 +0000661 // but all the grossness is constrained to ParseCaseStatement (and some
Richard Smitha3e01cf2013-11-15 22:45:29 +0000662 // weirdness in the actions), so this is just local grossness :).
Mike Stump11289f42009-09-09 15:08:12 +0000663
Chris Lattner34a22092009-03-04 04:23:07 +0000664 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
665 // example above.
John McCalldadc5752010-08-24 06:29:42 +0000666 StmtResult TopLevelCase(true);
Mike Stump11289f42009-09-09 15:08:12 +0000667
Chris Lattner34a22092009-03-04 04:23:07 +0000668 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
669 // gets updated each time a new case is parsed, and whose body is unset so
670 // far. When parsing 'case 4', this is the 'case 3' node.
Craig Topper161e4db2014-05-21 06:02:52 +0000671 Stmt *DeepestParsedCaseStmt = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000672
Chris Lattner34a22092009-03-04 04:23:07 +0000673 // While we have case statements, eat and stack them.
David Majnemer0ac67fa2011-06-13 05:50:12 +0000674 SourceLocation ColonLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000675 do {
Richard Trieu2c850c02011-04-21 21:44:26 +0000676 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
677 ConsumeToken(); // eat the 'case'.
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000678 ColonLoc = SourceLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000679
Douglas Gregord328d572009-09-21 18:10:23 +0000680 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000681 Actions.CodeCompleteCase(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000682 cutOffParsing();
683 return StmtError();
Douglas Gregord328d572009-09-21 18:10:23 +0000684 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000685
Chris Lattner125c0ee2009-12-10 00:38:54 +0000686 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
687 /// Disable this form of error recovery while we're parsing the case
688 /// expression.
689 ColonProtectionRAIIObject ColonProtection(*this);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000690
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000691 ExprResult LHS;
692 if (!MissingCase) {
Richard Smithef6c43d2018-07-26 18:41:30 +0000693 LHS = ParseCaseExpression(CaseLoc);
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000694 if (LHS.isInvalid()) {
695 // If constant-expression is parsed unsuccessfully, recover by skipping
696 // current case statement (moving to the colon that ends it).
Richard Smithef6c43d2018-07-26 18:41:30 +0000697 if (!SkipUntil(tok::colon, tok::r_brace, StopAtSemi | StopBeforeMatch))
698 return StmtError();
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000699 }
700 } else {
701 LHS = Expr;
702 MissingCase = false;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000703 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000704
Chris Lattner34a22092009-03-04 04:23:07 +0000705 // GNU case range extension.
706 SourceLocation DotDotDotLoc;
John McCalldadc5752010-08-24 06:29:42 +0000707 ExprResult RHS;
Alp Tokerec543272013-12-24 09:48:30 +0000708 if (TryConsumeToken(tok::ellipsis, DotDotDotLoc)) {
709 Diag(DotDotDotLoc, diag::ext_gnu_case_range);
Richard Smithef6c43d2018-07-26 18:41:30 +0000710 RHS = ParseCaseExpression(CaseLoc);
Chris Lattner34a22092009-03-04 04:23:07 +0000711 if (RHS.isInvalid()) {
Richard Smithef6c43d2018-07-26 18:41:30 +0000712 if (!SkipUntil(tok::colon, tok::r_brace, StopAtSemi | StopBeforeMatch))
713 return StmtError();
Chris Lattner34a22092009-03-04 04:23:07 +0000714 }
715 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000716
Chris Lattner125c0ee2009-12-10 00:38:54 +0000717 ColonProtection.restore();
Sebastian Redl042ad952008-12-11 19:30:53 +0000718
Alp Tokerec543272013-12-24 09:48:30 +0000719 if (TryConsumeToken(tok::colon, ColonLoc)) {
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000720 } else if (TryConsumeToken(tok::semi, ColonLoc) ||
721 TryConsumeToken(tok::coloncolon, ColonLoc)) {
722 // Treat "case blah;" or "case blah::" as a typo for "case blah:".
Alp Tokerec543272013-12-24 09:48:30 +0000723 Diag(ColonLoc, diag::err_expected_after)
724 << "'case'" << tok::colon
725 << FixItHint::CreateReplacement(ColonLoc, ":");
John McCall0140bfe2011-01-22 09:28:32 +0000726 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000727 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
Alp Tokerec543272013-12-24 09:48:30 +0000728 Diag(ExpectedLoc, diag::err_expected_after)
729 << "'case'" << tok::colon
730 << FixItHint::CreateInsertion(ExpectedLoc, ":");
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000731 ColonLoc = ExpectedLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000732 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000733
John McCalldadc5752010-08-24 06:29:42 +0000734 StmtResult Case =
Richard Smithef6c43d2018-07-26 18:41:30 +0000735 Actions.ActOnCaseStmt(CaseLoc, LHS, DotDotDotLoc, RHS, ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000736
Chris Lattner34a22092009-03-04 04:23:07 +0000737 // If we had a sema error parsing this case, then just ignore it and
738 // continue parsing the sub-stmt.
739 if (Case.isInvalid()) {
740 if (TopLevelCase.isInvalid()) // No parsed case stmts.
Alexey Bataevc4fad652016-01-13 11:18:54 +0000741 return ParseStatement(/*TrailingElseLoc=*/nullptr,
742 /*AllowOpenMPStandalone=*/true);
Chris Lattner34a22092009-03-04 04:23:07 +0000743 // Otherwise, just don't add it as a nested case.
744 } else {
745 // If this is the first case statement we parsed, it becomes TopLevelCase.
746 // Otherwise we link it into the current chain.
John McCall37ad5512010-08-23 06:44:23 +0000747 Stmt *NextDeepest = Case.get();
Chris Lattner34a22092009-03-04 04:23:07 +0000748 if (TopLevelCase.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000749 TopLevelCase = Case;
Chris Lattner34a22092009-03-04 04:23:07 +0000750 else
John McCallb268a282010-08-23 23:25:46 +0000751 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner34a22092009-03-04 04:23:07 +0000752 DeepestParsedCaseStmt = NextDeepest;
753 }
Mike Stump11289f42009-09-09 15:08:12 +0000754
Chris Lattner34a22092009-03-04 04:23:07 +0000755 // Handle all case statements.
756 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000757
Chris Lattner34a22092009-03-04 04:23:07 +0000758 // If we found a non-case statement, start by parsing it.
John McCalldadc5752010-08-24 06:29:42 +0000759 StmtResult SubStmt;
Mike Stump11289f42009-09-09 15:08:12 +0000760
Chris Lattner34a22092009-03-04 04:23:07 +0000761 if (Tok.isNot(tok::r_brace)) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000762 SubStmt = ParseStatement(/*TrailingElseLoc=*/nullptr,
763 /*AllowOpenMPStandalone=*/true);
Chris Lattner34a22092009-03-04 04:23:07 +0000764 } else {
765 // Nicely diagnose the common error "switch (X) { case 4: }", which is
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000766 // not valid. If ColonLoc doesn't point to a valid text location, there was
767 // another parsing error, so avoid producing extra diagnostics.
768 if (ColonLoc.isValid()) {
769 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
770 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
771 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
772 }
773 SubStmt = StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000774 }
Mike Stump11289f42009-09-09 15:08:12 +0000775
Chris Lattner34a22092009-03-04 04:23:07 +0000776 // Install the body into the most deeply-nested case.
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000777 if (DeepestParsedCaseStmt) {
778 // Broken sub-stmt shouldn't prevent forming the case statement properly.
779 if (SubStmt.isInvalid())
780 SubStmt = Actions.ActOnNullStmt(SourceLocation());
781 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
782 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000783
Chris Lattner34a22092009-03-04 04:23:07 +0000784 // Return the top level parsed statement tree.
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000785 return TopLevelCase;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000786}
787
788/// ParseDefaultStatement
789/// labeled-statement:
790/// 'default' ':' statement
791/// Note that this does not parse the 'statement' at the end.
792///
Richard Smithc202b282012-04-14 00:33:13 +0000793StmtResult Parser::ParseDefaultStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000794 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000795 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000796
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000797 SourceLocation ColonLoc;
Alp Tokerec543272013-12-24 09:48:30 +0000798 if (TryConsumeToken(tok::colon, ColonLoc)) {
Alp Tokerec543272013-12-24 09:48:30 +0000799 } else if (TryConsumeToken(tok::semi, ColonLoc)) {
Alp Toker97650562014-01-10 11:19:30 +0000800 // Treat "default;" as a typo for "default:".
Alp Tokerec543272013-12-24 09:48:30 +0000801 Diag(ColonLoc, diag::err_expected_after)
802 << "'default'" << tok::colon
803 << FixItHint::CreateReplacement(ColonLoc, ":");
John McCall0140bfe2011-01-22 09:28:32 +0000804 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000805 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
Alp Tokerec543272013-12-24 09:48:30 +0000806 Diag(ExpectedLoc, diag::err_expected_after)
807 << "'default'" << tok::colon
808 << FixItHint::CreateInsertion(ExpectedLoc, ":");
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000809 ColonLoc = ExpectedLoc;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000810 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000811
Richard Smith1002d102012-02-17 01:35:32 +0000812 StmtResult SubStmt;
813
814 if (Tok.isNot(tok::r_brace)) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000815 SubStmt = ParseStatement(/*TrailingElseLoc=*/nullptr,
816 /*AllowOpenMPStandalone=*/true);
Richard Smith1002d102012-02-17 01:35:32 +0000817 } else {
818 // Diagnose the common error "switch (X) {... default: }", which is
819 // not valid.
David Majnemerc6a99872011-06-14 15:24:38 +0000820 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
Richard Smith1002d102012-02-17 01:35:32 +0000821 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
822 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
823 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000824 }
825
Richard Smith1002d102012-02-17 01:35:32 +0000826 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000827 if (SubStmt.isInvalid())
Richard Smith1002d102012-02-17 01:35:32 +0000828 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl042ad952008-12-11 19:30:53 +0000829
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000830 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000831 SubStmt.get(), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000832}
833
Richard Smithc202b282012-04-14 00:33:13 +0000834StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Momchil Velikov57c681f2017-08-10 15:43:06 +0000835 return ParseCompoundStatement(isStmtExpr,
836 Scope::DeclScope | Scope::CompoundStmtScope);
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000837}
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000838
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000839/// ParseCompoundStatement - Parse a "{}" block.
840///
841/// compound-statement: [C99 6.8.2]
842/// { block-item-list[opt] }
843/// [GNU] { label-declarations block-item-list } [TODO]
844///
845/// block-item-list:
846/// block-item
847/// block-item-list block-item
848///
849/// block-item:
850/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000851/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000852/// statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000853///
854/// [GNU] label-declarations:
855/// [GNU] label-declaration
856/// [GNU] label-declarations label-declaration
857///
858/// [GNU] label-declaration:
859/// [GNU] '__label__' identifier-list ';'
860///
Richard Smithc202b282012-04-14 00:33:13 +0000861StmtResult Parser::ParseCompoundStatement(bool isStmtExpr,
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000862 unsigned ScopeFlags) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000863 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000864
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000865 // Enter a scope to hold everything within the compound stmt. Compound
866 // statements can always hold declarations.
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000867 ParseScope CompoundScope(this, ScopeFlags);
Chris Lattnerf2978802007-01-21 06:52:16 +0000868
869 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000870 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000871}
872
Lang Hames2954cea2012-11-03 22:29:05 +0000873/// Parse any pragmas at the start of the compound expression. We handle these
874/// separately since some pragmas (FP_CONTRACT) must appear before any C
875/// statement in the compound, but may be intermingled with other pragmas.
876void Parser::ParseCompoundStatementLeadingPragmas() {
877 bool checkForPragmas = true;
878 while (checkForPragmas) {
879 switch (Tok.getKind()) {
880 case tok::annot_pragma_vis:
881 HandlePragmaVisibility();
882 break;
883 case tok::annot_pragma_pack:
884 HandlePragmaPack();
885 break;
886 case tok::annot_pragma_msstruct:
887 HandlePragmaMSStruct();
888 break;
889 case tok::annot_pragma_align:
890 HandlePragmaAlign();
891 break;
892 case tok::annot_pragma_weak:
893 HandlePragmaWeak();
894 break;
895 case tok::annot_pragma_weakalias:
896 HandlePragmaWeakAlias();
897 break;
898 case tok::annot_pragma_redefine_extname:
899 HandlePragmaRedefineExtname();
900 break;
901 case tok::annot_pragma_opencl_extension:
902 HandlePragmaOpenCLExtension();
903 break;
904 case tok::annot_pragma_fp_contract:
905 HandlePragmaFPContract();
906 break;
Adam Nemet60d32642017-04-04 21:18:36 +0000907 case tok::annot_pragma_fp:
908 HandlePragmaFP();
909 break;
Kevin P. Neal2c0bc8b2018-08-14 17:06:56 +0000910 case tok::annot_pragma_fenv_access:
911 HandlePragmaFEnvAccess();
912 break;
David Majnemer4bb09802014-02-10 19:50:15 +0000913 case tok::annot_pragma_ms_pointers_to_members:
914 HandlePragmaMSPointersToMembers();
915 break;
Warren Huntc3b18962014-04-08 22:30:47 +0000916 case tok::annot_pragma_ms_pragma:
917 HandlePragmaMSPragma();
918 break;
Alexey Bataev3d42f342015-11-20 07:02:57 +0000919 case tok::annot_pragma_ms_vtordisp:
920 HandlePragmaMSVtorDisp();
921 break;
Richard Smithba3a4f92016-01-12 21:59:26 +0000922 case tok::annot_pragma_dump:
923 HandlePragmaDump();
924 break;
Lang Hames2954cea2012-11-03 22:29:05 +0000925 default:
926 checkForPragmas = false;
927 break;
928 }
929 }
930
931}
932
Chris Lattnerf2978802007-01-21 06:52:16 +0000933/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000934/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000935/// consume the '}' at the end of the block. It does not manipulate the scope
936/// stack.
John McCalldadc5752010-08-24 06:29:42 +0000937StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000938 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000939 Tok.getLocation(),
940 "in compound statement ('{}')");
Lang Hames5de91cc2012-10-02 04:45:10 +0000941
942 // Record the state of the FP_CONTRACT pragma, restore on leaving the
943 // compound statement.
944 Sema::FPContractStateRAII SaveFPContractState(Actions);
945
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000946 InMessageExpressionRAIIObject InMessage(*this, false);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000947 BalancedDelimiterTracker T(*this, tok::l_brace);
948 if (T.consumeOpen())
949 return StmtError();
Chris Lattnerf2978802007-01-21 06:52:16 +0000950
Richard Smith6eb9b9e2018-02-03 00:44:57 +0000951 Sema::CompoundScopeRAII CompoundScope(Actions, isStmtExpr);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +0000952
Lang Hames2954cea2012-11-03 22:29:05 +0000953 // Parse any pragmas at the beginning of the compound statement.
954 ParseCompoundStatementLeadingPragmas();
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000955
Lang Hames2954cea2012-11-03 22:29:05 +0000956 StmtVector Stmts;
Lang Hamesa930e712012-10-21 01:10:01 +0000957
Chris Lattner43e7f312011-02-18 02:08:43 +0000958 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
959 // only allowed at the start of a compound stmt regardless of the language.
960 while (Tok.is(tok::kw___label__)) {
961 SourceLocation LabelLoc = ConsumeToken();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000962
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000963 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner43e7f312011-02-18 02:08:43 +0000964 while (1) {
965 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000966 Diag(Tok, diag::err_expected) << tok::identifier;
Chris Lattner43e7f312011-02-18 02:08:43 +0000967 break;
968 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000969
Chris Lattner43e7f312011-02-18 02:08:43 +0000970 IdentifierInfo *II = Tok.getIdentifierInfo();
971 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000972 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000973
Alp Tokerec543272013-12-24 09:48:30 +0000974 if (!TryConsumeToken(tok::comma))
Chris Lattner43e7f312011-02-18 02:08:43 +0000975 break;
Chris Lattner43e7f312011-02-18 02:08:43 +0000976 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000977
John McCall084e83d2011-03-24 11:26:52 +0000978 DeclSpec DS(AttrFactory);
Rafael Espindolaab417692013-07-09 12:05:01 +0000979 DeclGroupPtrTy Res =
980 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner43e7f312011-02-18 02:08:43 +0000981 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000982
Chris Lattner02f1b612012-04-28 16:12:17 +0000983 ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
Chris Lattner43e7f312011-02-18 02:08:43 +0000984 if (R.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000985 Stmts.push_back(R.get());
Chris Lattner43e7f312011-02-18 02:08:43 +0000986 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000987
Richard Smith752ada82015-11-17 23:32:01 +0000988 while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
989 Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000990 if (Tok.is(tok::annot_pragma_unused)) {
991 HandlePragmaUnused();
992 continue;
993 }
994
John McCalldadc5752010-08-24 06:29:42 +0000995 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000996 if (Tok.isNot(tok::kw___extension__)) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000997 R = ParseStatementOrDeclaration(Stmts, ACK_Any);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000998 } else {
999 // __extension__ can start declarations and it can also be a unary
1000 // operator for expressions. Consume multiple __extension__ markers here
1001 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +00001002 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001003 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001004 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001005 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +00001006
John McCall084e83d2011-03-24 11:26:52 +00001007 ParsedAttributesWithRange attrs(AttrFactory);
Craig Topper161e4db2014-05-21 06:02:52 +00001008 MaybeParseCXX11Attributes(attrs, nullptr,
1009 /*MightBeObjCMessageSend*/ true);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001010
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001011 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001012 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +00001013 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +00001014 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +00001015 ExtensionRAIIObject O(Diags);
1016
Chris Lattner49836b42009-04-02 04:16:50 +00001017 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Faisal Vali421b2d12017-12-29 05:41:00 +00001018 DeclGroupPtrTy Res =
1019 ParseDeclaration(DeclaratorContext::BlockContext, DeclEnd, attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001020 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001021 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +00001022 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +00001023 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +00001024
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001025 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001026 SkipUntil(tok::semi);
1027 continue;
1028 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001029
Alexis Hunt96d5c762009-11-21 08:43:09 +00001030 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +00001031 // Eat the semicolon at the end of stmt and convert the expr into a
1032 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00001033 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +00001034 R = Actions.ActOnExprStmt(Res);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001035 }
1036 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001037
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001038 if (R.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001039 Stmts.push_back(R.get());
Chris Lattner30f910e2006-10-16 05:52:41 +00001040 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001041
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +00001042 SourceLocation CloseLoc = Tok.getLocation();
1043
Chris Lattner0ccd51e2006-08-09 05:47:47 +00001044 // We broke out of the while loop because we found a '}' or EOF.
Nico Webera48b6c22012-12-30 23:36:56 +00001045 if (!T.consumeClose())
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +00001046 // Recover by creating a compound statement with what we parsed so far,
1047 // instead of dropping everything and returning StmtError();
Nico Webera48b6c22012-12-30 23:36:56 +00001048 CloseLoc = T.getCloseLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +00001049
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +00001050 return Actions.ActOnCompoundStmt(T.getOpenLocation(), CloseLoc,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001051 Stmts, isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +00001052}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001053
Chris Lattnerc0081db2008-12-12 06:31:07 +00001054/// ParseParenExprOrCondition:
1055/// [C ] '(' expression ')'
Richard Smithc7a05a92016-06-29 21:17:59 +00001056/// [C++] '(' condition ')'
1057/// [C++1z] '(' init-statement[opt] condition ')'
Chris Lattnerc0081db2008-12-12 06:31:07 +00001058///
1059/// This function parses and performs error recovery on the specified condition
1060/// or expression (depending on whether we're in C++ or C mode). This function
1061/// goes out of its way to recover well. It returns true if there was a parser
1062/// error (the right paren couldn't be found), which indicates that the caller
1063/// should try to recover harder. It returns false if the condition is
1064/// successfully parsed. Note that a successful parse can still have semantic
1065/// errors in the condition.
Richard Smithc7a05a92016-06-29 21:17:59 +00001066bool Parser::ParseParenExprOrCondition(StmtResult *InitStmt,
1067 Sema::ConditionResult &Cond,
Douglas Gregore60e41a2010-05-06 17:25:47 +00001068 SourceLocation Loc,
Richard Smith03a4aa32016-06-23 19:02:52 +00001069 Sema::ConditionKind CK) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001070 BalancedDelimiterTracker T(*this, tok::l_paren);
1071 T.consumeOpen();
1072
David Blaikiebbafb8a2012-03-11 07:00:24 +00001073 if (getLangOpts().CPlusPlus)
Richard Smithc7a05a92016-06-29 21:17:59 +00001074 Cond = ParseCXXCondition(InitStmt, Loc, CK);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001075 else {
Richard Smith03a4aa32016-06-23 19:02:52 +00001076 ExprResult CondExpr = ParseExpression();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001077
Douglas Gregore60e41a2010-05-06 17:25:47 +00001078 // If required, convert to a boolean value.
Richard Smith03a4aa32016-06-23 19:02:52 +00001079 if (CondExpr.isInvalid())
1080 Cond = Sema::ConditionError();
1081 else
1082 Cond = Actions.ActOnCondition(getCurScope(), Loc, CondExpr.get(), CK);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001083 }
Mike Stump11289f42009-09-09 15:08:12 +00001084
Chris Lattnerc0081db2008-12-12 06:31:07 +00001085 // If the parser was confused by the condition and we don't have a ')', try to
1086 // recover by skipping ahead to a semi and bailing out. If condexp is
1087 // semantically invalid but we have well formed code, keep going.
Richard Smith03a4aa32016-06-23 19:02:52 +00001088 if (Cond.isInvalid() && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +00001089 SkipUntil(tok::semi);
1090 // Skipping may have stopped if it found the containing ')'. If so, we can
1091 // continue parsing the if statement.
1092 if (Tok.isNot(tok::r_paren))
1093 return true;
1094 }
Mike Stump11289f42009-09-09 15:08:12 +00001095
Chris Lattnerc0081db2008-12-12 06:31:07 +00001096 // Otherwise the condition is valid or the rparen is present.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001097 T.consumeClose();
Chad Rosier67055f52012-07-10 21:35:27 +00001098
Chris Lattner70d44982012-04-28 16:24:20 +00001099 // Check for extraneous ')'s to catch things like "if (foo())) {". We know
1100 // that all callers are looking for a statement after the condition, so ")"
1101 // isn't valid.
1102 while (Tok.is(tok::r_paren)) {
1103 Diag(Tok, diag::err_extraneous_rparen_in_condition)
1104 << FixItHint::CreateRemoval(Tok.getLocation());
1105 ConsumeParen();
1106 }
Chad Rosier67055f52012-07-10 21:35:27 +00001107
Chris Lattnerc0081db2008-12-12 06:31:07 +00001108 return false;
1109}
1110
1111
Chris Lattnerc951dae2006-08-10 04:23:57 +00001112/// ParseIfStatement
1113/// if-statement: [C99 6.8.4.1]
1114/// 'if' '(' expression ')' statement
1115/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001116/// [C++] 'if' '(' condition ')' statement
1117/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +00001118///
Richard Smithc202b282012-04-14 00:33:13 +00001119StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001120 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001121 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +00001122
Richard Smithb130fe72016-06-23 19:16:49 +00001123 bool IsConstexpr = false;
1124 if (Tok.is(tok::kw_constexpr)) {
Aaron Ballmanc351fba2017-12-04 20:27:34 +00001125 Diag(Tok, getLangOpts().CPlusPlus17 ? diag::warn_cxx14_compat_constexpr_if
Richard Smithb130fe72016-06-23 19:16:49 +00001126 : diag::ext_constexpr_if);
1127 IsConstexpr = true;
1128 ConsumeToken();
1129 }
1130
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001131 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001132 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +00001133 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +00001134 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +00001135 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001136
David Blaikiebbafb8a2012-03-11 07:00:24 +00001137 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001138
Chris Lattner2dd1b722007-08-26 23:08:06 +00001139 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
1140 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001141 //
1142 // C++ 6.4p3:
1143 // A name introduced by a declaration in a condition is in scope from its
1144 // point of declaration until the end of the substatements controlled by the
1145 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001146 // C++ 3.3.2p4:
1147 // Names declared in the for-init-statement, and in the condition of if,
1148 // while, for, and switch statements are local to the if, while, for, or
1149 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001150 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001151 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +00001152
Chris Lattnerc951dae2006-08-10 04:23:57 +00001153 // Parse the condition.
Richard Smithc7a05a92016-06-29 21:17:59 +00001154 StmtResult InitStmt;
Richard Smith03a4aa32016-06-23 19:02:52 +00001155 Sema::ConditionResult Cond;
Richard Smithc7a05a92016-06-29 21:17:59 +00001156 if (ParseParenExprOrCondition(&InitStmt, Cond, IfLoc,
Richard Smithb130fe72016-06-23 19:16:49 +00001157 IsConstexpr ? Sema::ConditionKind::ConstexprIf
1158 : Sema::ConditionKind::Boolean))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001159 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +00001160
Richard Smithb130fe72016-06-23 19:16:49 +00001161 llvm::Optional<bool> ConstexprCondition;
1162 if (IsConstexpr)
1163 ConstexprCondition = Cond.getKnownValue();
1164
Chris Lattner8fb26252007-08-22 05:28:50 +00001165 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001166 // there is no compound stmt. C90 does not have this clause. We only do this
1167 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001168 //
1169 // C++ 6.4p1:
1170 // The substatement in a selection-statement (each substatement, in the else
1171 // form of the if statement) implicitly defines a local scope.
1172 //
1173 // For C++ we create a scope for the condition and a new scope for
1174 // substatements because:
1175 // -When the 'then' scope exits, we want the condition declaration to still be
1176 // active for the 'else' scope too.
1177 // -Sema will detect name clashes by considering declarations of a
1178 // 'ControlScope' as part of its direct subscope.
1179 // -If we wanted the condition and substatement to be in the same scope, we
1180 // would have to notify ParseStatement not to create a new scope. It's
1181 // simpler to let it create a new scope.
1182 //
David Majnemer2206bf52014-03-05 08:57:59 +00001183 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001184
Chris Lattner5c5808a2007-10-29 05:08:52 +00001185 // Read the 'then' stmt.
1186 SourceLocation ThenStmtLoc = Tok.getLocation();
Nico Weber3cef1082011-12-22 23:26:17 +00001187
1188 SourceLocation InnerStatementTrailingElseLoc;
Richard Smithb130fe72016-06-23 19:16:49 +00001189 StmtResult ThenStmt;
1190 {
1191 EnterExpressionEvaluationContext PotentiallyDiscarded(
Faisal Valid143a0c2017-04-01 21:30:49 +00001192 Actions, Sema::ExpressionEvaluationContext::DiscardedStatement, nullptr,
Nicolas Lesserb6d5c582018-07-12 18:45:41 +00001193 Sema::ExpressionEvaluationContextRecord::EK_Other,
Richard Smithb130fe72016-06-23 19:16:49 +00001194 /*ShouldEnter=*/ConstexprCondition && !*ConstexprCondition);
1195 ThenStmt = ParseStatement(&InnerStatementTrailingElseLoc);
1196 }
Chris Lattnerac4471c2007-05-28 05:38:24 +00001197
Chris Lattner37e54f42007-08-22 05:16:28 +00001198 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001199 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001200
Chris Lattnerc951dae2006-08-10 04:23:57 +00001201 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +00001202 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +00001203 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +00001204 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001205
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001206 if (Tok.is(tok::kw_else)) {
Nico Weber3cef1082011-12-22 23:26:17 +00001207 if (TrailingElseLoc)
1208 *TrailingElseLoc = Tok.getLocation();
1209
Chris Lattneraf635312006-10-16 06:06:51 +00001210 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +00001211 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +00001212
Chris Lattner8fb26252007-08-22 05:28:50 +00001213 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001214 // there is no compound stmt. C90 does not have this clause. We only do
1215 // this if the body isn't a compound statement to avoid push/pop in common
1216 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001217 //
1218 // C++ 6.4p1:
1219 // The substatement in a selection-statement (each substatement, in the else
1220 // form of the if statement) implicitly defines a local scope.
1221 //
Richard Smithb130fe72016-06-23 19:16:49 +00001222 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX,
1223 Tok.is(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001224
Richard Smithb130fe72016-06-23 19:16:49 +00001225 EnterExpressionEvaluationContext PotentiallyDiscarded(
Faisal Valid143a0c2017-04-01 21:30:49 +00001226 Actions, Sema::ExpressionEvaluationContext::DiscardedStatement, nullptr,
Nicolas Lesserb6d5c582018-07-12 18:45:41 +00001227 Sema::ExpressionEvaluationContextRecord::EK_Other,
Richard Smithb130fe72016-06-23 19:16:49 +00001228 /*ShouldEnter=*/ConstexprCondition && *ConstexprCondition);
Chris Lattner30f910e2006-10-16 05:52:41 +00001229 ElseStmt = ParseStatement();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001230
Chris Lattner37e54f42007-08-22 05:16:28 +00001231 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001232 InnerScope.Exit();
Douglas Gregor4ecb7202011-07-30 08:36:53 +00001233 } else if (Tok.is(tok::code_completion)) {
1234 Actions.CodeCompleteAfterIf(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001235 cutOffParsing();
1236 return StmtError();
Nico Weber3cef1082011-12-22 23:26:17 +00001237 } else if (InnerStatementTrailingElseLoc.isValid()) {
1238 Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else);
Chris Lattnerc951dae2006-08-10 04:23:57 +00001239 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001240
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001241 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +00001242
Chris Lattner5c5808a2007-10-29 05:08:52 +00001243 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +00001244 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +00001245 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001246 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
Craig Topper161e4db2014-05-21 06:02:52 +00001247 (ThenStmt.isInvalid() && ElseStmt.get() == nullptr) ||
1248 (ThenStmt.get() == nullptr && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +00001249 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +00001250 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +00001251 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001252
Chris Lattner5c5808a2007-10-29 05:08:52 +00001253 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001254 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001255 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001256 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001257 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001258
Richard Smithc7a05a92016-06-29 21:17:59 +00001259 return Actions.ActOnIfStmt(IfLoc, IsConstexpr, InitStmt.get(), Cond,
1260 ThenStmt.get(), ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +00001261}
1262
Chris Lattner9075bd72006-08-10 04:59:57 +00001263/// ParseSwitchStatement
1264/// switch-statement:
1265/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001266/// [C++] 'switch' '(' condition ')' statement
Richard Smithc202b282012-04-14 00:33:13 +00001267StmtResult Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001268 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001269 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +00001270
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001271 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001272 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +00001273 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001274 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001275 }
Chris Lattner2dd1b722007-08-26 23:08:06 +00001276
David Blaikiebbafb8a2012-03-11 07:00:24 +00001277 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001278
Chris Lattner2dd1b722007-08-26 23:08:06 +00001279 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1280 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001281 //
1282 // C++ 6.4p3:
1283 // A name introduced by a declaration in a condition is in scope from its
1284 // point of declaration until the end of the substatements controlled by the
1285 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001286 // C++ 3.3.2p4:
1287 // Names declared in the for-init-statement, and in the condition of if,
1288 // while, for, and switch statements are local to the if, while, for, or
1289 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001290 //
Serge Pavlov09f99242014-01-23 15:05:00 +00001291 unsigned ScopeFlags = Scope::SwitchScope;
Chris Lattnerc0081db2008-12-12 06:31:07 +00001292 if (C99orCXX)
1293 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001294 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001295
Chris Lattner9075bd72006-08-10 04:59:57 +00001296 // Parse the condition.
Richard Smithc7a05a92016-06-29 21:17:59 +00001297 StmtResult InitStmt;
Richard Smith03a4aa32016-06-23 19:02:52 +00001298 Sema::ConditionResult Cond;
Richard Smithc7a05a92016-06-29 21:17:59 +00001299 if (ParseParenExprOrCondition(&InitStmt, Cond, SwitchLoc,
1300 Sema::ConditionKind::Switch))
Sebastian Redlb62406f2008-12-11 19:48:14 +00001301 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +00001302
Richard Smithc7a05a92016-06-29 21:17:59 +00001303 StmtResult Switch =
1304 Actions.ActOnStartOfSwitchStmt(SwitchLoc, InitStmt.get(), Cond);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001305
Douglas Gregore60e41a2010-05-06 17:25:47 +00001306 if (Switch.isInvalid()) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001307 // Skip the switch body.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001308 // FIXME: This is not optimal recovery, but parsing the body is more
1309 // dangerous due to the presence of case and default statements, which
1310 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +00001311 if (Tok.is(tok::l_brace)) {
1312 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001313 SkipUntil(tok::r_brace);
Douglas Gregor4abc32d2010-05-20 23:20:59 +00001314 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +00001315 SkipUntil(tok::semi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001316 return Switch;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001317 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001318
Chris Lattner8fb26252007-08-22 05:28:50 +00001319 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001320 // there is no compound stmt. C90 does not have this clause. We only do this
1321 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001322 //
1323 // C++ 6.4p1:
1324 // The substatement in a selection-statement (each substatement, in the else
1325 // form of the if statement) implicitly defines a local scope.
1326 //
1327 // See comments in ParseIfStatement for why we create a scope for the
1328 // condition and a new scope for substatement in C++.
1329 //
Serge Pavlov09f99242014-01-23 15:05:00 +00001330 getCurScope()->AddFlags(Scope::BreakScope);
David Majnemer2206bf52014-03-05 08:57:59 +00001331 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +00001332
Hans Wennborg852c3462014-06-17 00:09:05 +00001333 // We have incremented the mangling number for the SwitchScope and the
1334 // InnerScope, which is one too many.
1335 if (C99orCXX)
David Majnemera7f8c462015-03-19 21:54:30 +00001336 getCurScope()->decrementMSManglingNumber();
Hans Wennborg852c3462014-06-17 00:09:05 +00001337
Chris Lattner9075bd72006-08-10 04:59:57 +00001338 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001339 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001340
Chris Lattner8fd2d012010-01-24 01:50:29 +00001341 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001342 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001343 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001344
John McCallb268a282010-08-23 23:25:46 +00001345 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001346}
1347
1348/// ParseWhileStatement
1349/// while-statement: [C99 6.8.5.1]
1350/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001351/// [C++] 'while' '(' condition ')' statement
Richard Smithc202b282012-04-14 00:33:13 +00001352StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001353 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +00001354 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +00001355 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001356
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001357 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001358 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +00001359 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001360 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001361 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001362
David Blaikiebbafb8a2012-03-11 07:00:24 +00001363 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001364
Chris Lattner2dd1b722007-08-26 23:08:06 +00001365 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1366 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001367 //
1368 // C++ 6.4p3:
1369 // A name introduced by a declaration in a condition is in scope from its
1370 // point of declaration until the end of the substatements controlled by the
1371 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001372 // C++ 3.3.2p4:
1373 // Names declared in the for-init-statement, and in the condition of if,
1374 // while, for, and switch statements are local to the if, while, for, or
1375 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001376 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001377 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001378 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001379 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1380 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001381 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001382 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1383 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001384
Chris Lattner9075bd72006-08-10 04:59:57 +00001385 // Parse the condition.
Richard Smith03a4aa32016-06-23 19:02:52 +00001386 Sema::ConditionResult Cond;
Richard Smithc7a05a92016-06-29 21:17:59 +00001387 if (ParseParenExprOrCondition(nullptr, Cond, WhileLoc,
1388 Sema::ConditionKind::Boolean))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001389 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001390
Justin Bognere4ebb6c2013-12-03 07:36:55 +00001391 // C99 6.8.5p5 - In C99, the body of the while statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001392 // there is no compound stmt. C90 does not have this clause. We only do this
1393 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001394 //
1395 // C++ 6.5p2:
1396 // The substatement in an iteration-statement implicitly defines a local scope
1397 // which is entered and exited each time through the loop.
1398 //
1399 // See comments in ParseIfStatement for why we create a scope for the
1400 // condition and a new scope for substatement in C++.
1401 //
David Majnemer2206bf52014-03-05 08:57:59 +00001402 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001403
Chris Lattner9075bd72006-08-10 04:59:57 +00001404 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001405 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001406
Chris Lattner8fb26252007-08-22 05:28:50 +00001407 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001408 InnerScope.Exit();
1409 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001410
Richard Smith03a4aa32016-06-23 19:02:52 +00001411 if (Cond.isInvalid() || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001412 return StmtError();
1413
Richard Smith03a4aa32016-06-23 19:02:52 +00001414 return Actions.ActOnWhileStmt(WhileLoc, Cond, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001415}
1416
1417/// ParseDoStatement
1418/// do-statement: [C99 6.8.5.2]
1419/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +00001420/// Note: this lets the caller parse the end ';'.
Richard Smithc202b282012-04-14 00:33:13 +00001421StmtResult Parser::ParseDoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001422 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001423 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001424
Chris Lattner2dd1b722007-08-26 23:08:06 +00001425 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1426 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001427 unsigned ScopeFlags;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001428 if (getLangOpts().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001429 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001430 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001431 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001432
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001433 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001434
Justin Bognere4ebb6c2013-12-03 07:36:55 +00001435 // C99 6.8.5p5 - In C99, the body of the do statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001436 // there is no compound stmt. C90 does not have this clause. We only do this
1437 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +00001438 //
1439 // C++ 6.5p2:
1440 // The substatement in an iteration-statement implicitly defines a local scope
1441 // which is entered and exited each time through the loop.
1442 //
David Majnemer2206bf52014-03-05 08:57:59 +00001443 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
1444 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001445
Chris Lattner9075bd72006-08-10 04:59:57 +00001446 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001447 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +00001448
Chris Lattner8fb26252007-08-22 05:28:50 +00001449 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001450 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001451
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001452 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001453 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +00001454 Diag(Tok, diag::err_expected_while);
Alp Tokerec543272013-12-24 09:48:30 +00001455 Diag(DoLoc, diag::note_matching) << "'do'";
Alexey Bataevee6507d2013-11-18 08:17:37 +00001456 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner0046de12008-11-13 18:52:53 +00001457 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001458 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001459 }
Chris Lattneraf635312006-10-16 06:06:51 +00001460 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001461
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001462 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001463 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Alexey Bataevee6507d2013-11-18 08:17:37 +00001464 SkipUntil(tok::semi, StopBeforeMatch);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001465 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001466 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001467
Richard Smithc2c8bb82013-10-15 01:34:54 +00001468 // Parse the parenthesized expression.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001469 BalancedDelimiterTracker T(*this, tok::l_paren);
1470 T.consumeOpen();
Chad Rosier67055f52012-07-10 21:35:27 +00001471
Richard Smithc2c8bb82013-10-15 01:34:54 +00001472 // A do-while expression is not a condition, so can't have attributes.
1473 DiagnoseAndSkipCXX11Attributes();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001474
John McCalldadc5752010-08-24 06:29:42 +00001475 ExprResult Cond = ParseExpression();
Alex Lorenzc38ba662017-10-30 22:55:11 +00001476 // Correct the typos in condition before closing the scope.
1477 if (Cond.isUsable())
1478 Cond = Actions.CorrectDelayedTyposInExpr(Cond);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001479 T.consumeClose();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001480 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001481
Sebastian Redlb62406f2008-12-11 19:48:14 +00001482 if (Cond.isInvalid() || Body.isInvalid())
1483 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001484
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001485 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(),
1486 Cond.get(), T.getCloseLocation());
Chris Lattner9075bd72006-08-10 04:59:57 +00001487}
1488
Richard Smith955bf012014-06-19 11:42:00 +00001489bool Parser::isForRangeIdentifier() {
1490 assert(Tok.is(tok::identifier));
1491
1492 const Token &Next = NextToken();
1493 if (Next.is(tok::colon))
1494 return true;
1495
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001496 if (Next.isOneOf(tok::l_square, tok::kw_alignas)) {
Richard Smith955bf012014-06-19 11:42:00 +00001497 TentativeParsingAction PA(*this);
1498 ConsumeToken();
1499 SkipCXX11Attributes();
1500 bool Result = Tok.is(tok::colon);
1501 PA.Revert();
1502 return Result;
1503 }
1504
1505 return false;
1506}
1507
Chris Lattner9075bd72006-08-10 04:59:57 +00001508/// ParseForStatement
1509/// for-statement: [C99 6.8.5.3]
1510/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1511/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001512/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1513/// [C++] statement
Richard Smith0e304ea2015-10-22 04:46:14 +00001514/// [C++0x] 'for'
1515/// 'co_await'[opt] [Coroutines]
1516/// '(' for-range-declaration ':' for-range-initializer ')'
1517/// statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001518/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1519/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001520///
1521/// [C++] for-init-statement:
1522/// [C++] expression-statement
1523/// [C++] simple-declaration
1524///
Richard Smith02e85f32011-04-14 22:09:26 +00001525/// [C++0x] for-range-declaration:
1526/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1527/// [C++0x] for-range-initializer:
1528/// [C++0x] expression
1529/// [C++0x] braced-init-list [TODO]
Richard Smithc202b282012-04-14 00:33:13 +00001530StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001531 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001532 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001533
Richard Smith0e304ea2015-10-22 04:46:14 +00001534 SourceLocation CoawaitLoc;
1535 if (Tok.is(tok::kw_co_await))
1536 CoawaitLoc = ConsumeToken();
1537
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001538 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001539 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +00001540 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001541 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001542 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001543
Chad Rosier67055f52012-07-10 21:35:27 +00001544 bool C99orCXXorObjC = getLangOpts().C99 || getLangOpts().CPlusPlus ||
1545 getLangOpts().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001546
Chris Lattner2dd1b722007-08-26 23:08:06 +00001547 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1548 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001549 //
1550 // C++ 6.4p3:
1551 // A name introduced by a declaration in a condition is in scope from its
1552 // point of declaration until the end of the substatements controlled by the
1553 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001554 // C++ 3.3.2p4:
1555 // Names declared in the for-init-statement, and in the condition of if,
1556 // while, for, and switch statements are local to the if, while, for, or
1557 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001558 // C++ 6.5.3p1:
1559 // Names declared in the for-init-statement are in the same declarative-region
1560 // as those declared in the condition.
1561 //
Serge Pavlov09f99242014-01-23 15:05:00 +00001562 unsigned ScopeFlags = 0;
Chris Lattner934074c2009-04-22 00:54:41 +00001563 if (C99orCXXorObjC)
Serge Pavlov09f99242014-01-23 15:05:00 +00001564 ScopeFlags = Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001565
1566 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +00001567
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001568 BalancedDelimiterTracker T(*this, tok::l_paren);
1569 T.consumeOpen();
1570
John McCalldadc5752010-08-24 06:29:42 +00001571 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001572
Richard Smith02e85f32011-04-14 22:09:26 +00001573 bool ForEach = false, ForRange = false;
John McCalldadc5752010-08-24 06:29:42 +00001574 StmtResult FirstPart;
Richard Smith03a4aa32016-06-23 19:02:52 +00001575 Sema::ConditionResult SecondPart;
John McCalldadc5752010-08-24 06:29:42 +00001576 ExprResult Collection;
Richard Smith02e85f32011-04-14 22:09:26 +00001577 ForRangeInit ForRangeInit;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001578 FullExprArg ThirdPart(Actions);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001579
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001580 if (Tok.is(tok::code_completion)) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001581 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001582 C99orCXXorObjC? Sema::PCC_ForInit
1583 : Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001584 cutOffParsing();
1585 return StmtError();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001586 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001587
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001588 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001589 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001590
Chris Lattner9075bd72006-08-10 04:59:57 +00001591 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001592 if (Tok.is(tok::semi)) { // for (;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001593 ProhibitAttributes(attrs);
Chris Lattner53361ac2006-08-10 05:19:57 +00001594 // no first part, eat the ';'.
1595 ConsumeToken();
Richard Smith955bf012014-06-19 11:42:00 +00001596 } else if (getLangOpts().CPlusPlus && Tok.is(tok::identifier) &&
1597 isForRangeIdentifier()) {
1598 ProhibitAttributes(attrs);
1599 IdentifierInfo *Name = Tok.getIdentifierInfo();
1600 SourceLocation Loc = ConsumeToken();
1601 MaybeParseCXX11Attributes(attrs);
1602
1603 ForRangeInit.ColonLoc = ConsumeToken();
1604 if (Tok.is(tok::l_brace))
1605 ForRangeInit.RangeExpr = ParseBraceInitializer();
1606 else
1607 ForRangeInit.RangeExpr = ParseExpression();
1608
Richard Smith83d3f152014-11-27 01:54:27 +00001609 Diag(Loc, diag::err_for_range_identifier)
Aaron Ballmanc351fba2017-12-04 20:27:34 +00001610 << ((getLangOpts().CPlusPlus11 && !getLangOpts().CPlusPlus17)
Richard Smith955bf012014-06-19 11:42:00 +00001611 ? FixItHint::CreateInsertion(Loc, "auto &&")
1612 : FixItHint());
1613
1614 FirstPart = Actions.ActOnCXXForRangeIdentifier(getCurScope(), Loc, Name,
1615 attrs, attrs.Range.getEnd());
1616 ForRange = true;
Eli Friedman0ffc31c2011-12-20 01:50:37 +00001617 } else if (isForInitDeclaration()) { // for (int X = 4;
Richard Smithbf5bcf22018-06-26 23:20:26 +00001618 ParenBraceBracketBalancer BalancerRAIIObj(*this);
1619
Chris Lattner53361ac2006-08-10 05:19:57 +00001620 // Parse declaration, which eats the ';'.
George Burgess IV4d456452018-06-28 21:36:00 +00001621 if (!C99orCXXorObjC) { // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001622 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
George Burgess IV4d456452018-06-28 21:36:00 +00001623 Diag(Tok, diag::warn_gcc_variable_decl_in_for_loop);
1624 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001625
Richard Smith02e85f32011-04-14 22:09:26 +00001626 // In C++0x, "for (T NS:a" might not be a typo for ::
David Blaikiebbafb8a2012-03-11 07:00:24 +00001627 bool MightBeForRangeStmt = getLangOpts().CPlusPlus;
Richard Smith02e85f32011-04-14 22:09:26 +00001628 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1629
Chris Lattner49836b42009-04-02 04:16:50 +00001630 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00001631 DeclGroupPtrTy DG = ParseSimpleDeclaration(
Faisal Vali421b2d12017-12-29 05:41:00 +00001632 DeclaratorContext::ForContext, DeclEnd, attrs, false,
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00001633 MightBeForRangeStmt ? &ForRangeInit : nullptr);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001634 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Richard Smith02e85f32011-04-14 22:09:26 +00001635 if (ForRangeInit.ParsedForRangeDecl()) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001636 Diag(ForRangeInit.ColonLoc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00001637 diag::warn_cxx98_compat_for_range : diag::ext_for_range);
Richard Smith58c74332011-09-04 19:54:14 +00001638
Richard Smith02e85f32011-04-14 22:09:26 +00001639 ForRange = true;
1640 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattner32dc41c2009-03-29 17:27:48 +00001641 ConsumeToken();
1642 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001643 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001644 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001645 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001646
Douglas Gregor68762e72010-08-23 21:17:50 +00001647 if (Tok.is(tok::code_completion)) {
1648 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001649 cutOffParsing();
1650 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001651 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001652 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001653 } else {
1654 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001655 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001656 } else {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001657 ProhibitAttributes(attrs);
Kaelyn Takatab16e6322014-11-20 22:06:40 +00001658 Value = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Chris Lattner71e23ce2006-11-04 20:18:38 +00001659
John McCall34376a62010-12-04 03:47:34 +00001660 ForEach = isTokIdentifier_in();
1661
Chris Lattnercd68f642007-06-27 01:06:29 +00001662 // Turn the expression into a stmt.
John McCall34376a62010-12-04 03:47:34 +00001663 if (!Value.isInvalid()) {
1664 if (ForEach)
1665 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1666 else
Richard Smith945f8d32013-01-14 22:39:08 +00001667 FirstPart = Actions.ActOnExprStmt(Value);
John McCall34376a62010-12-04 03:47:34 +00001668 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001669
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001670 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001671 ConsumeToken();
John McCall34376a62010-12-04 03:47:34 +00001672 } else if (ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001673 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001674
Douglas Gregor68762e72010-08-23 21:17:50 +00001675 if (Tok.is(tok::code_completion)) {
David Blaikie0403cb12016-01-15 23:43:25 +00001676 Actions.CodeCompleteObjCForCollection(getCurScope(), nullptr);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001677 cutOffParsing();
1678 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001679 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001680 Collection = ParseExpression();
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001681 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::colon) && FirstPart.get()) {
Richard Smith4f848f12011-12-20 22:56:20 +00001682 // User tried to write the reasonable, but ill-formed, for-range-statement
1683 // for (expr : expr) { ... }
1684 Diag(Tok, diag::err_for_range_expected_decl)
1685 << FirstPart.get()->getSourceRange();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001686 SkipUntil(tok::r_paren, StopBeforeMatch);
Richard Smith03a4aa32016-06-23 19:02:52 +00001687 SecondPart = Sema::ConditionError();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001688 } else {
Douglas Gregor230a7e62011-02-17 03:38:46 +00001689 if (!Value.isInvalid()) {
1690 Diag(Tok, diag::err_expected_semi_for);
1691 } else {
1692 // Skip until semicolon or rparen, don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001693 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor230a7e62011-02-17 03:38:46 +00001694 if (Tok.is(tok::semi))
1695 ConsumeToken();
1696 }
Chris Lattner53361ac2006-08-10 05:19:57 +00001697 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001698 }
Serge Pavlov09f99242014-01-23 15:05:00 +00001699
1700 // Parse the second part of the for specifier.
1701 getCurScope()->AddFlags(Scope::BreakScope | Scope::ContinueScope);
Richard Smith03a4aa32016-06-23 19:02:52 +00001702 if (!ForEach && !ForRange && !SecondPart.isInvalid()) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001703 // Parse the second part of the for specifier.
1704 if (Tok.is(tok::semi)) { // for (...;;
1705 // no second part.
Douglas Gregor230a7e62011-02-17 03:38:46 +00001706 } else if (Tok.is(tok::r_paren)) {
1707 // missing both semicolons.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001708 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001709 if (getLangOpts().CPlusPlus)
Richard Smithc7a05a92016-06-29 21:17:59 +00001710 SecondPart =
1711 ParseCXXCondition(nullptr, ForLoc, Sema::ConditionKind::Boolean);
Douglas Gregore60e41a2010-05-06 17:25:47 +00001712 else {
Richard Smith03a4aa32016-06-23 19:02:52 +00001713 ExprResult SecondExpr = ParseExpression();
1714 if (SecondExpr.isInvalid())
1715 SecondPart = Sema::ConditionError();
1716 else
1717 SecondPart =
1718 Actions.ActOnCondition(getCurScope(), ForLoc, SecondExpr.get(),
1719 Sema::ConditionKind::Boolean);
Douglas Gregore60e41a2010-05-06 17:25:47 +00001720 }
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001721 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001722
Douglas Gregor230a7e62011-02-17 03:38:46 +00001723 if (Tok.isNot(tok::semi)) {
Richard Smith03a4aa32016-06-23 19:02:52 +00001724 if (!SecondPart.isInvalid())
Douglas Gregor230a7e62011-02-17 03:38:46 +00001725 Diag(Tok, diag::err_expected_semi_for);
1726 else
1727 // Skip until semicolon or rparen, don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001728 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor230a7e62011-02-17 03:38:46 +00001729 }
1730
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001731 if (Tok.is(tok::semi)) {
1732 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001733 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001734
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001735 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001736 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001737 ExprResult Third = ParseExpression();
Richard Smith945f8d32013-01-14 22:39:08 +00001738 // FIXME: The C++11 standard doesn't actually say that this is a
1739 // discarded-value expression, but it clearly should be.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001740 ThirdPart = Actions.MakeFullDiscardedValueExpr(Third.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001741 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001742 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001743 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001744 T.consumeClose();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001745
Richard Smith0e304ea2015-10-22 04:46:14 +00001746 // C++ Coroutines [stmt.iter]:
1747 // 'co_await' can only be used for a range-based for statement.
1748 if (CoawaitLoc.isValid() && !ForRange) {
1749 Diag(CoawaitLoc, diag::err_for_co_await_not_range_for);
1750 CoawaitLoc = SourceLocation();
1751 }
1752
Richard Smith02e85f32011-04-14 22:09:26 +00001753 // We need to perform most of the semantic analysis for a C++0x for-range
1754 // statememt before parsing the body, in order to be able to deduce the type
1755 // of an auto-typed loop variable.
1756 StmtResult ForRangeStmt;
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001757 StmtResult ForEachStmt;
Chad Rosier67055f52012-07-10 21:35:27 +00001758
John McCall53848232011-07-27 01:07:15 +00001759 if (ForRange) {
Denis Zobnin7d6b9242016-02-02 17:33:09 +00001760 ExprResult CorrectedRange =
1761 Actions.CorrectDelayedTyposInExpr(ForRangeInit.RangeExpr.get());
Richard Smith9f690bd2015-10-27 06:02:45 +00001762 ForRangeStmt = Actions.ActOnCXXForRangeStmt(
1763 getCurScope(), ForLoc, CoawaitLoc, FirstPart.get(),
Denis Zobnin7d6b9242016-02-02 17:33:09 +00001764 ForRangeInit.ColonLoc, CorrectedRange.get(),
Richard Smith9f690bd2015-10-27 06:02:45 +00001765 T.getCloseLocation(), Sema::BFRK_Build);
John McCall53848232011-07-27 01:07:15 +00001766
1767 // Similarly, we need to do the semantic analysis for a for-range
1768 // statement immediately in order to close over temporaries correctly.
1769 } else if (ForEach) {
Sam Panzer2c4ca0f2012-08-16 21:47:25 +00001770 ForEachStmt = Actions.ActOnObjCForCollectionStmt(ForLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001771 FirstPart.get(),
1772 Collection.get(),
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001773 T.getCloseLocation());
Alexey Bataev9c821032015-04-30 04:23:23 +00001774 } else {
1775 // In OpenMP loop region loop control variable must be captured and be
1776 // private. Perform analysis of first part (if any).
1777 if (getLangOpts().OpenMP && FirstPart.isUsable()) {
1778 Actions.ActOnOpenMPLoopInitialization(ForLoc, FirstPart.get());
1779 }
John McCall53848232011-07-27 01:07:15 +00001780 }
1781
Justin Bognere4ebb6c2013-12-03 07:36:55 +00001782 // C99 6.8.5p5 - In C99, the body of the for statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001783 // there is no compound stmt. C90 does not have this clause. We only do this
1784 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001785 //
1786 // C++ 6.5p2:
1787 // The substatement in an iteration-statement implicitly defines a local scope
1788 // which is entered and exited each time through the loop.
1789 //
1790 // See comments in ParseIfStatement for why we create a scope for
1791 // for-init-statement/condition and a new scope for substatement in C++.
1792 //
David Majnemer2206bf52014-03-05 08:57:59 +00001793 ParseScope InnerScope(this, Scope::DeclScope, C99orCXXorObjC,
1794 Tok.is(tok::l_brace));
1795
1796 // The body of the for loop has the same local mangling number as the
1797 // for-init-statement.
1798 // It will only be incremented if the body contains other things that would
1799 // normally increment the mangling number (like a compound statement).
1800 if (C99orCXXorObjC)
David Majnemera7f8c462015-03-19 21:54:30 +00001801 getCurScope()->decrementMSManglingNumber();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001802
Chris Lattner9075bd72006-08-10 04:59:57 +00001803 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001804 StmtResult Body(ParseStatement(TrailingElseLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001805
Chris Lattner8fb26252007-08-22 05:28:50 +00001806 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001807 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001808
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001809 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001810 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001811
1812 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001813 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001814
Richard Smith02e85f32011-04-14 22:09:26 +00001815 if (ForEach)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001816 return Actions.FinishObjCForCollectionStmt(ForEachStmt.get(),
1817 Body.get());
Mike Stump11289f42009-09-09 15:08:12 +00001818
Richard Smith02e85f32011-04-14 22:09:26 +00001819 if (ForRange)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001820 return Actions.FinishCXXForRangeStmt(ForRangeStmt.get(), Body.get());
Richard Smith02e85f32011-04-14 22:09:26 +00001821
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001822 return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.get(),
Richard Smith03a4aa32016-06-23 19:02:52 +00001823 SecondPart, ThirdPart, T.getCloseLocation(),
1824 Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001825}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001826
Chris Lattner503fadc2006-08-10 05:45:44 +00001827/// ParseGotoStatement
1828/// jump-statement:
1829/// 'goto' identifier ';'
1830/// [GNU] 'goto' '*' expression ';'
1831///
1832/// Note: this lets the caller parse the end ';'.
1833///
Richard Smithc202b282012-04-14 00:33:13 +00001834StmtResult Parser::ParseGotoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001835 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001836 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001837
John McCalldadc5752010-08-24 06:29:42 +00001838 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001839 if (Tok.is(tok::identifier)) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +00001840 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1841 Tok.getLocation());
1842 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Chris Lattner503fadc2006-08-10 05:45:44 +00001843 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001844 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001845 // GNU indirect goto extension.
1846 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001847 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001848 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001849 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001850 SkipUntil(tok::semi, StopBeforeMatch);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001851 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001852 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001853 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.get());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001854 } else {
Alp Tokerec543272013-12-24 09:48:30 +00001855 Diag(Tok, diag::err_expected) << tok::identifier;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001856 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001857 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001858
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001859 return Res;
Chris Lattner503fadc2006-08-10 05:45:44 +00001860}
1861
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001862/// ParseContinueStatement
1863/// jump-statement:
1864/// 'continue' ';'
1865///
1866/// Note: this lets the caller parse the end ';'.
1867///
Richard Smithc202b282012-04-14 00:33:13 +00001868StmtResult Parser::ParseContinueStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001869 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001870 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001871}
1872
1873/// ParseBreakStatement
1874/// jump-statement:
1875/// 'break' ';'
1876///
1877/// Note: this lets the caller parse the end ';'.
1878///
Richard Smithc202b282012-04-14 00:33:13 +00001879StmtResult Parser::ParseBreakStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001880 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001881 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001882}
1883
Chris Lattner503fadc2006-08-10 05:45:44 +00001884/// ParseReturnStatement
1885/// jump-statement:
1886/// 'return' expression[opt] ';'
Richard Smith0e304ea2015-10-22 04:46:14 +00001887/// 'return' braced-init-list ';'
1888/// 'co_return' expression[opt] ';'
1889/// 'co_return' braced-init-list ';'
Richard Smithc202b282012-04-14 00:33:13 +00001890StmtResult Parser::ParseReturnStatement() {
Richard Smith0e304ea2015-10-22 04:46:14 +00001891 assert((Tok.is(tok::kw_return) || Tok.is(tok::kw_co_return)) &&
1892 "Not a return stmt!");
1893 bool IsCoreturn = Tok.is(tok::kw_co_return);
Chris Lattneraf635312006-10-16 06:06:51 +00001894 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001895
John McCalldadc5752010-08-24 06:29:42 +00001896 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001897 if (Tok.isNot(tok::semi)) {
Richard Smith0e304ea2015-10-22 04:46:14 +00001898 // FIXME: Code completion for co_return.
1899 if (Tok.is(tok::code_completion) && !IsCoreturn) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001900 Actions.CodeCompleteReturn(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001901 cutOffParsing();
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001902 return StmtError();
1903 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001904
David Blaikiebbafb8a2012-03-11 07:00:24 +00001905 if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus) {
Douglas Gregore9e27d92011-03-11 23:10:44 +00001906 R = ParseInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00001907 if (R.isUsable())
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001908 Diag(R.get()->getBeginLoc(),
1909 getLangOpts().CPlusPlus11
1910 ? diag::warn_cxx98_compat_generalized_initializer_lists
1911 : diag::ext_generalized_initializer_lists)
1912 << R.get()->getSourceRange();
Douglas Gregore9e27d92011-03-11 23:10:44 +00001913 } else
Nico Weber3ce01c32015-01-04 08:07:54 +00001914 R = ParseExpression();
Serge Pavlovf79bd5c2013-12-04 03:51:59 +00001915 if (R.isInvalid()) {
1916 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001917 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001918 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001919 }
Richard Smithcfd53b42015-10-22 06:13:50 +00001920 if (IsCoreturn)
Eric Fiselier20f25cb2017-03-06 23:38:15 +00001921 return Actions.ActOnCoreturnStmt(getCurScope(), ReturnLoc, R.get());
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001922 return Actions.ActOnReturnStmt(ReturnLoc, R.get(), getCurScope());
Chris Lattner503fadc2006-08-10 05:45:44 +00001923}
Chris Lattner0116c472006-08-15 06:03:28 +00001924
Alexey Bataevc4fad652016-01-13 11:18:54 +00001925StmtResult Parser::ParsePragmaLoopHint(StmtVector &Stmts,
Jonathan Roelofsce1db6d2017-03-14 17:29:33 +00001926 AllowedConstructsKind Allowed,
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001927 SourceLocation *TrailingElseLoc,
1928 ParsedAttributesWithRange &Attrs) {
1929 // Create temporary attribute list.
1930 ParsedAttributesWithRange TempAttrs(AttrFactory);
1931
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001932 // Get loop hints and consume annotated token.
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001933 while (Tok.is(tok::annot_pragma_loop_hint)) {
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001934 LoopHint Hint;
1935 if (!HandlePragmaLoopHint(Hint))
1936 continue;
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001937
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001938 ArgsUnion ArgHints[] = {Hint.PragmaNameLoc, Hint.OptionLoc, Hint.StateLoc,
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001939 ArgsUnion(Hint.ValueExpr)};
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001940 TempAttrs.addNew(Hint.PragmaNameLoc->Ident, Hint.Range, nullptr,
1941 Hint.PragmaNameLoc->Loc, ArgHints, 4,
Erich Keanee891aa92018-07-13 15:07:47 +00001942 ParsedAttr::AS_Pragma);
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001943 }
1944
1945 // Get the next statement.
1946 MaybeParseCXX11Attributes(Attrs);
1947
1948 StmtResult S = ParseStatementOrDeclarationAfterAttributes(
Alexey Bataevc4fad652016-01-13 11:18:54 +00001949 Stmts, Allowed, TrailingElseLoc, Attrs);
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001950
1951 Attrs.takeAllFrom(TempAttrs);
1952 return S;
1953}
1954
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001955Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001956 assert(Tok.is(tok::l_brace));
1957 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001958
Jordan Rose1e879d82018-03-23 00:07:18 +00001959 PrettyDeclStackTraceEntry CrashInfo(Actions.Context, Decl, LBraceLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00001960 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001961
Alexey Bataev3d42f342015-11-20 07:02:57 +00001962 // Save and reset current vtordisp stack if we have entered a C++ method body.
1963 bool IsCXXMethod =
1964 getLangOpts().CPlusPlus && Decl && isa<CXXMethodDecl>(Decl);
Denis Zobnin2290dac2016-04-29 11:27:00 +00001965 Sema::PragmaStackSentinelRAII
1966 PragmaStackSentinel(Actions, "InternalPragmaState", IsCXXMethod);
Alexey Bataev3d42f342015-11-20 07:02:57 +00001967
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001968 // Do not enter a scope for the brace, as the arguments are in the same scope
1969 // (the function body) as the body itself. Instead, just read the statement
1970 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00001971 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001972
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001973 // If the function body could not be parsed, make a bogus compoundstmt.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001974 if (FnBody.isInvalid()) {
1975 Sema::CompoundScopeRAII CompoundScope(Actions);
Robert Wilhelm27b2c9a32013-08-19 20:51:20 +00001976 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001977 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001978
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001979 BodyScope.Exit();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001980 return Actions.ActOnFinishFunctionBody(Decl, FnBody.get());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001981}
Sebastian Redlb219c902008-12-21 16:41:36 +00001982
Sebastian Redla7b98a72009-04-26 20:35:05 +00001983/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1984///
1985/// function-try-block:
1986/// 'try' ctor-initializer[opt] compound-statement handler-seq
1987///
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001988Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00001989 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1990 SourceLocation TryLoc = ConsumeToken();
1991
Jordan Rose1e879d82018-03-23 00:07:18 +00001992 PrettyDeclStackTraceEntry CrashInfo(Actions.Context, Decl, TryLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00001993 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00001994
1995 // Constructor initializer list?
1996 if (Tok.is(tok::colon))
1997 ParseConstructorInitializer(Decl);
Douglas Gregor5ca153f2011-09-07 20:36:12 +00001998 else
1999 Actions.ActOnDefaultCtorInitializers(Decl);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00002000
Alexey Bataev3d42f342015-11-20 07:02:57 +00002001 // Save and reset current vtordisp stack if we have entered a C++ method body.
2002 bool IsCXXMethod =
2003 getLangOpts().CPlusPlus && Decl && isa<CXXMethodDecl>(Decl);
Denis Zobnin2290dac2016-04-29 11:27:00 +00002004 Sema::PragmaStackSentinelRAII
2005 PragmaStackSentinel(Actions, "InternalPragmaState", IsCXXMethod);
Alexey Bataev3d42f342015-11-20 07:02:57 +00002006
Sebastian Redld98ecd62009-04-26 21:08:36 +00002007 SourceLocation LBraceLoc = Tok.getLocation();
David Blaikie1c9c9042012-11-10 01:04:23 +00002008 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc, /*FnTry*/true));
Sebastian Redla7b98a72009-04-26 20:35:05 +00002009 // If we failed to parse the try-catch, we just give the function an empty
2010 // compound statement as the body.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002011 if (FnBody.isInvalid()) {
2012 Sema::CompoundScopeRAII CompoundScope(Actions);
Robert Wilhelm27b2c9a32013-08-19 20:51:20 +00002013 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002014 }
Sebastian Redla7b98a72009-04-26 20:35:05 +00002015
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002016 BodyScope.Exit();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002017 return Actions.ActOnFinishFunctionBody(Decl, FnBody.get());
Sebastian Redla7b98a72009-04-26 20:35:05 +00002018}
2019
Erik Verbruggen6e922512012-04-12 10:11:59 +00002020bool Parser::trySkippingFunctionBody() {
Erik Verbruggen6e922512012-04-12 10:11:59 +00002021 assert(SkipFunctionBodies &&
2022 "Should only be called when SkipFunctionBodies is enabled");
Argyrios Kyrtzidis289e4a32012-10-31 17:29:28 +00002023 if (!PP.isCodeCompletionEnabled()) {
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002024 SkipFunctionBody();
Argyrios Kyrtzidis289e4a32012-10-31 17:29:28 +00002025 return true;
2026 }
2027
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002028 // We're in code-completion mode. Skip parsing for all function bodies unless
2029 // the body contains the code-completion point.
2030 TentativeParsingAction PA(*this);
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002031 bool IsTryCatch = Tok.is(tok::kw_try);
2032 CachedTokens Toks;
2033 bool ErrorInPrologue = ConsumeAndStoreFunctionPrologue(Toks);
2034 if (llvm::any_of(Toks, [](const Token &Tok) {
2035 return Tok.is(tok::code_completion);
2036 })) {
2037 PA.Revert();
2038 return false;
2039 }
2040 if (ErrorInPrologue) {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002041 PA.Commit();
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002042 SkipMalformedDecl();
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002043 return true;
2044 }
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002045 if (!SkipUntil(tok::r_brace, StopAtCodeCompletion)) {
2046 PA.Revert();
2047 return false;
2048 }
2049 while (IsTryCatch && Tok.is(tok::kw_catch)) {
2050 if (!SkipUntil(tok::l_brace, StopAtCodeCompletion) ||
2051 !SkipUntil(tok::r_brace, StopAtCodeCompletion)) {
2052 PA.Revert();
2053 return false;
2054 }
2055 }
2056 PA.Commit();
2057 return true;
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002058}
2059
Sebastian Redlb219c902008-12-21 16:41:36 +00002060/// ParseCXXTryBlock - Parse a C++ try-block.
2061///
2062/// try-block:
2063/// 'try' compound-statement handler-seq
2064///
Richard Smithc202b282012-04-14 00:33:13 +00002065StmtResult Parser::ParseCXXTryBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00002066 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2067
2068 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00002069 return ParseCXXTryBlockCommon(TryLoc);
2070}
2071
2072/// ParseCXXTryBlockCommon - Parse the common part of try-block and
2073/// function-try-block.
2074///
2075/// try-block:
2076/// 'try' compound-statement handler-seq
2077///
2078/// function-try-block:
2079/// 'try' ctor-initializer[opt] compound-statement handler-seq
2080///
2081/// handler-seq:
2082/// handler handler-seq[opt]
2083///
John Wiegley1c0675e2011-04-28 01:08:34 +00002084/// [Borland] try-block:
2085/// 'try' compound-statement seh-except-block
Alp Tokerf6a24ce2013-12-05 16:25:25 +00002086/// 'try' compound-statement seh-finally-block
John Wiegley1c0675e2011-04-28 01:08:34 +00002087///
David Blaikie1c9c9042012-11-10 01:04:23 +00002088StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc, bool FnTry) {
Sebastian Redlb219c902008-12-21 16:41:36 +00002089 if (Tok.isNot(tok::l_brace))
Alp Tokerec543272013-12-24 09:48:30 +00002090 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
Richard Smithc202b282012-04-14 00:33:13 +00002091
Momchil Velikov57c681f2017-08-10 15:43:06 +00002092 StmtResult TryBlock(ParseCompoundStatement(
2093 /*isStmtExpr=*/false, Scope::DeclScope | Scope::TryScope |
2094 Scope::CompoundStmtScope |
2095 (FnTry ? Scope::FnTryCatchScope : 0)));
Sebastian Redlb219c902008-12-21 16:41:36 +00002096 if (TryBlock.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002097 return TryBlock;
Sebastian Redlb219c902008-12-21 16:41:36 +00002098
John Wiegley1c0675e2011-04-28 01:08:34 +00002099 // Borland allows SEH-handlers with 'try'
Chad Rosier67055f52012-07-10 21:35:27 +00002100
Richard Smithc202b282012-04-14 00:33:13 +00002101 if ((Tok.is(tok::identifier) &&
2102 Tok.getIdentifierInfo() == getSEHExceptKeyword()) ||
2103 Tok.is(tok::kw___finally)) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002104 // TODO: Factor into common return ParseSEHHandlerCommon(...)
2105 StmtResult Handler;
Douglas Gregor60060d62011-10-21 03:57:52 +00002106 if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002107 SourceLocation Loc = ConsumeToken();
2108 Handler = ParseSEHExceptBlock(Loc);
2109 }
2110 else {
2111 SourceLocation Loc = ConsumeToken();
2112 Handler = ParseSEHFinallyBlock(Loc);
2113 }
2114 if(Handler.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002115 return Handler;
John McCall53fa7142010-12-24 02:08:15 +00002116
John Wiegley1c0675e2011-04-28 01:08:34 +00002117 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
2118 TryLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002119 TryBlock.get(),
Warren Huntf6be4cb2014-07-25 20:52:51 +00002120 Handler.get());
Sebastian Redlb219c902008-12-21 16:41:36 +00002121 }
John Wiegley1c0675e2011-04-28 01:08:34 +00002122 else {
Benjamin Kramerf0623432012-08-23 22:51:59 +00002123 StmtVector Handlers;
Richard Smithc2c8bb82013-10-15 01:34:54 +00002124
2125 // C++11 attributes can't appear here, despite this context seeming
2126 // statement-like.
2127 DiagnoseAndSkipCXX11Attributes();
Sebastian Redlb219c902008-12-21 16:41:36 +00002128
John Wiegley1c0675e2011-04-28 01:08:34 +00002129 if (Tok.isNot(tok::kw_catch))
2130 return StmtError(Diag(Tok, diag::err_expected_catch));
2131 while (Tok.is(tok::kw_catch)) {
David Blaikie1c9c9042012-11-10 01:04:23 +00002132 StmtResult Handler(ParseCXXCatchBlock(FnTry));
John Wiegley1c0675e2011-04-28 01:08:34 +00002133 if (!Handler.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002134 Handlers.push_back(Handler.get());
John Wiegley1c0675e2011-04-28 01:08:34 +00002135 }
2136 // Don't bother creating the full statement if we don't have any usable
2137 // handlers.
2138 if (Handlers.empty())
2139 return StmtError();
2140
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002141 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.get(), Handlers);
John Wiegley1c0675e2011-04-28 01:08:34 +00002142 }
Sebastian Redlb219c902008-12-21 16:41:36 +00002143}
2144
2145/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
2146///
Richard Smith1dba27c2013-01-29 09:02:09 +00002147/// handler:
2148/// 'catch' '(' exception-declaration ')' compound-statement
Sebastian Redlb219c902008-12-21 16:41:36 +00002149///
Richard Smith1dba27c2013-01-29 09:02:09 +00002150/// exception-declaration:
2151/// attribute-specifier-seq[opt] type-specifier-seq declarator
2152/// attribute-specifier-seq[opt] type-specifier-seq abstract-declarator[opt]
2153/// '...'
Sebastian Redlb219c902008-12-21 16:41:36 +00002154///
David Blaikie1c9c9042012-11-10 01:04:23 +00002155StmtResult Parser::ParseCXXCatchBlock(bool FnCatch) {
Sebastian Redlb219c902008-12-21 16:41:36 +00002156 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
2157
2158 SourceLocation CatchLoc = ConsumeToken();
2159
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002160 BalancedDelimiterTracker T(*this, tok::l_paren);
Alp Toker383d2c42014-01-01 03:08:43 +00002161 if (T.expectAndConsume())
Sebastian Redlb219c902008-12-21 16:41:36 +00002162 return StmtError();
2163
2164 // C++ 3.3.2p3:
2165 // The name in a catch exception-declaration is local to the handler and
2166 // shall not be redeclared in the outermost block of the handler.
David Blaikie1c9c9042012-11-10 01:04:23 +00002167 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope |
David Blaikie3403feb2012-11-13 18:51:45 +00002168 (FnCatch ? Scope::FnTryCatchScope : 0));
Sebastian Redlb219c902008-12-21 16:41:36 +00002169
2170 // exception-declaration is equivalent to '...' or a parameter-declaration
2171 // without default arguments.
Craig Topper161e4db2014-05-21 06:02:52 +00002172 Decl *ExceptionDecl = nullptr;
Sebastian Redlb219c902008-12-21 16:41:36 +00002173 if (Tok.isNot(tok::ellipsis)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002174 ParsedAttributesWithRange Attributes(AttrFactory);
2175 MaybeParseCXX11Attributes(Attributes);
2176
John McCall084e83d2011-03-24 11:26:52 +00002177 DeclSpec DS(AttrFactory);
Richard Smith1dba27c2013-01-29 09:02:09 +00002178 DS.takeAttributesFrom(Attributes);
2179
Sebastian Redl54c04d42008-12-22 19:15:10 +00002180 if (ParseCXXTypeSpecifierSeq(DS))
2181 return StmtError();
Richard Smith1dba27c2013-01-29 09:02:09 +00002182
Faisal Vali421b2d12017-12-29 05:41:00 +00002183 Declarator ExDecl(DS, DeclaratorContext::CXXCatchContext);
Sebastian Redlb219c902008-12-21 16:41:36 +00002184 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002185 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00002186 } else
2187 ConsumeToken();
2188
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002189 T.consumeClose();
2190 if (T.getCloseLocation().isInvalid())
Sebastian Redlb219c902008-12-21 16:41:36 +00002191 return StmtError();
2192
2193 if (Tok.isNot(tok::l_brace))
Alp Tokerec543272013-12-24 09:48:30 +00002194 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
Sebastian Redlb219c902008-12-21 16:41:36 +00002195
Alexis Hunt96d5c762009-11-21 08:43:09 +00002196 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
Richard Smithc202b282012-04-14 00:33:13 +00002197 StmtResult Block(ParseCompoundStatement());
Sebastian Redlb219c902008-12-21 16:41:36 +00002198 if (Block.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002199 return Block;
Sebastian Redlb219c902008-12-21 16:41:36 +00002200
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002201 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.get());
Sebastian Redlb219c902008-12-21 16:41:36 +00002202}
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002203
2204void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Douglas Gregor43edb322011-10-24 22:31:10 +00002205 IfExistsCondition Result;
Francois Picheta5b3fcb2011-05-07 17:30:27 +00002206 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002207 return;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00002208
Douglas Gregor43edb322011-10-24 22:31:10 +00002209 // Handle dependent statements by parsing the braces as a compound statement.
2210 // This is not the same behavior as Visual C++, which don't treat this as a
2211 // compound statement, but for Clang's type checking we can't have anything
2212 // inside these braces escaping to the surrounding code.
2213 if (Result.Behavior == IEB_Dependent) {
2214 if (!Tok.is(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002215 Diag(Tok, diag::err_expected) << tok::l_brace;
Richard Smithc202b282012-04-14 00:33:13 +00002216 return;
Douglas Gregor43edb322011-10-24 22:31:10 +00002217 }
Richard Smithc202b282012-04-14 00:33:13 +00002218
2219 StmtResult Compound = ParseCompoundStatement();
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002220 if (Compound.isInvalid())
2221 return;
Richard Smithc202b282012-04-14 00:33:13 +00002222
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002223 StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc,
2224 Result.IsIfExists,
Richard Smithc202b282012-04-14 00:33:13 +00002225 Result.SS,
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002226 Result.Name,
2227 Compound.get());
2228 if (DepResult.isUsable())
2229 Stmts.push_back(DepResult.get());
Douglas Gregor43edb322011-10-24 22:31:10 +00002230 return;
2231 }
Richard Smithc202b282012-04-14 00:33:13 +00002232
Douglas Gregor43edb322011-10-24 22:31:10 +00002233 BalancedDelimiterTracker Braces(*this, tok::l_brace);
2234 if (Braces.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +00002235 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002236 return;
2237 }
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002238
Douglas Gregor43edb322011-10-24 22:31:10 +00002239 switch (Result.Behavior) {
2240 case IEB_Parse:
2241 // Parse the statements below.
2242 break;
Chad Rosier67055f52012-07-10 21:35:27 +00002243
Douglas Gregor43edb322011-10-24 22:31:10 +00002244 case IEB_Dependent:
2245 llvm_unreachable("Dependent case handled above");
Chad Rosier67055f52012-07-10 21:35:27 +00002246
Douglas Gregor43edb322011-10-24 22:31:10 +00002247 case IEB_Skip:
2248 Braces.skipToEnd();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002249 return;
2250 }
2251
2252 // Condition is true, parse the statements.
2253 while (Tok.isNot(tok::r_brace)) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00002254 StmtResult R = ParseStatementOrDeclaration(Stmts, ACK_Any);
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002255 if (R.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002256 Stmts.push_back(R.get());
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002257 }
Douglas Gregor43edb322011-10-24 22:31:10 +00002258 Braces.consumeClose();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002259}
Anastasia Stulova6bdbcbb2016-02-19 18:30:11 +00002260
2261bool Parser::ParseOpenCLUnrollHintAttribute(ParsedAttributes &Attrs) {
2262 MaybeParseGNUAttributes(Attrs);
2263
2264 if (Attrs.empty())
2265 return true;
2266
Erich Keanee891aa92018-07-13 15:07:47 +00002267 if (Attrs.begin()->getKind() != ParsedAttr::AT_OpenCLUnrollHint)
Anastasia Stulova6bdbcbb2016-02-19 18:30:11 +00002268 return true;
2269
2270 if (!(Tok.is(tok::kw_for) || Tok.is(tok::kw_while) || Tok.is(tok::kw_do))) {
2271 Diag(Tok, diag::err_opencl_unroll_hint_on_non_loop);
2272 return false;
2273 }
2274 return true;
2275}