blob: e8cf7d5fa4578610cd56858b11bceaf549c8ea87 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
Chris Lattner0ccd51e2006-08-09 05:47:47 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner0ccd51e2006-08-09 05:47:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
Aaron Ballmanb06b15a2014-06-06 12:40:24 +000015#include "clang/Basic/Attributes.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/Basic/PrettyStackTrace.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000017#include "clang/Parse/Parser.h"
Vassil Vassilev11ad3392017-03-23 15:11:07 +000018#include "clang/Parse/RAIIObjectsForParser.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/DeclSpec.h"
Aaron Ballmanb06b15a2014-06-06 12:40:24 +000020#include "clang/Sema/LoopHint.h"
John McCallfaf5fb42010-08-26 23:41:50 +000021#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall8b0666c2010-08-20 18:27:03 +000022#include "clang/Sema/Scope.h"
Richard Smith4f605af2012-08-18 00:55:03 +000023#include "clang/Sema/TypoCorrection.h"
Chris Lattner0ccd51e2006-08-09 05:47:47 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// C99 6.8: Statements and Blocks.
28//===----------------------------------------------------------------------===//
29
Richard Smith426a47b2013-10-28 22:04:30 +000030/// \brief Parse a standalone statement (for instance, as the body of an 'if',
31/// 'while', or 'for').
Alexey Bataevc4fad652016-01-13 11:18:54 +000032StmtResult Parser::ParseStatement(SourceLocation *TrailingElseLoc,
33 bool AllowOpenMPStandalone) {
Richard Smith426a47b2013-10-28 22:04:30 +000034 StmtResult Res;
35
36 // We may get back a null statement if we found a #pragma. Keep going until
37 // we get an actual statement.
38 do {
39 StmtVector Stmts;
Alexey Bataevc4fad652016-01-13 11:18:54 +000040 Res = ParseStatementOrDeclaration(
41 Stmts, AllowOpenMPStandalone ? ACK_StatementsOpenMPAnyExecutable
42 : ACK_StatementsOpenMPNonStandalone,
43 TrailingElseLoc);
Richard Smith426a47b2013-10-28 22:04:30 +000044 } while (!Res.isInvalid() && !Res.get());
45
46 return Res;
47}
48
Chris Lattner0ccd51e2006-08-09 05:47:47 +000049/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
50/// StatementOrDeclaration:
51/// statement
52/// declaration
53///
54/// statement:
55/// labeled-statement
56/// compound-statement
57/// expression-statement
58/// selection-statement
59/// iteration-statement
60/// jump-statement
Argyrios Kyrtzidisdee82912008-09-07 18:58:01 +000061/// [C++] declaration-statement
Sebastian Redlb219c902008-12-21 16:41:36 +000062/// [C++] try-block
John Wiegley1c0675e2011-04-28 01:08:34 +000063/// [MS] seh-try-block
Fariborz Jahanian90814572007-10-04 20:19:06 +000064/// [OBC] objc-throw-statement
65/// [OBC] objc-try-catch-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +000066/// [OBC] objc-synchronized-statement
Chris Lattner0116c472006-08-15 06:03:28 +000067/// [GNU] asm-statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +000068/// [OMP] openmp-construct [TODO]
69///
70/// labeled-statement:
71/// identifier ':' statement
72/// 'case' constant-expression ':' statement
73/// 'default' ':' statement
74///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000075/// selection-statement:
76/// if-statement
77/// switch-statement
78///
79/// iteration-statement:
80/// while-statement
81/// do-statement
82/// for-statement
83///
Chris Lattner9075bd72006-08-10 04:59:57 +000084/// expression-statement:
85/// expression[opt] ';'
86///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000087/// jump-statement:
88/// 'goto' identifier ';'
89/// 'continue' ';'
90/// 'break' ';'
91/// 'return' expression[opt] ';'
Chris Lattner503fadc2006-08-10 05:45:44 +000092/// [GNU] 'goto' '*' expression ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000093///
Fariborz Jahanian90814572007-10-04 20:19:06 +000094/// [OBC] objc-throw-statement:
95/// [OBC] '@' 'throw' expression ';'
Mike Stump11289f42009-09-09 15:08:12 +000096/// [OBC] '@' 'throw' ';'
97///
John McCalldadc5752010-08-24 06:29:42 +000098StmtResult
Alexey Bataevc4fad652016-01-13 11:18:54 +000099Parser::ParseStatementOrDeclaration(StmtVector &Stmts,
Jonathan Roelofsce1db6d2017-03-14 17:29:33 +0000100 AllowedConstructsKind Allowed,
Nico Weber3cef1082011-12-22 23:26:17 +0000101 SourceLocation *TrailingElseLoc) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000102
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000103 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000104
Richard Smithc202b282012-04-14 00:33:13 +0000105 ParsedAttributesWithRange Attrs(AttrFactory);
Craig Topper161e4db2014-05-21 06:02:52 +0000106 MaybeParseCXX11Attributes(Attrs, nullptr, /*MightBeObjCMessageSend*/ true);
Anastasia Stulova6bdbcbb2016-02-19 18:30:11 +0000107 if (!MaybeParseOpenCLUnrollHintAttribute(Attrs))
108 return StmtError();
Richard Smithc202b282012-04-14 00:33:13 +0000109
Alexey Bataevc4fad652016-01-13 11:18:54 +0000110 StmtResult Res = ParseStatementOrDeclarationAfterAttributes(
111 Stmts, Allowed, TrailingElseLoc, Attrs);
Richard Smithc202b282012-04-14 00:33:13 +0000112
113 assert((Attrs.empty() || Res.isInvalid() || Res.isUsable()) &&
114 "attributes on empty statement");
115
116 if (Attrs.empty() || Res.isInvalid())
117 return Res;
118
119 return Actions.ProcessStmtAttributes(Res.get(), Attrs.getList(), Attrs.Range);
120}
121
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000122namespace {
123class StatementFilterCCC : public CorrectionCandidateCallback {
124public:
125 StatementFilterCCC(Token nextTok) : NextToken(nextTok) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000126 WantTypeSpecifiers = nextTok.isOneOf(tok::l_paren, tok::less, tok::l_square,
127 tok::identifier, tok::star, tok::amp);
128 WantExpressionKeywords =
129 nextTok.isOneOf(tok::l_paren, tok::identifier, tok::arrow, tok::period);
130 WantRemainingKeywords =
131 nextTok.isOneOf(tok::l_paren, tok::semi, tok::identifier, tok::l_brace);
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000132 WantCXXNamedCasts = false;
133 }
134
Craig Topper2b07f022014-03-12 05:09:18 +0000135 bool ValidateCandidate(const TypoCorrection &candidate) override {
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000136 if (FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>())
Kaelyn Uhrain07e62722013-10-01 22:00:28 +0000137 return !candidate.getCorrectionSpecifier() || isa<ObjCIvarDecl>(FD);
Kaelyn Uhrain46b6cdc2013-09-27 19:40:16 +0000138 if (NextToken.is(tok::equal))
139 return candidate.getCorrectionDeclAs<VarDecl>();
Kaelyn Uhrain30943ce2013-09-27 23:54:23 +0000140 if (NextToken.is(tok::period) &&
141 candidate.getCorrectionDeclAs<NamespaceDecl>())
142 return false;
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000143 return CorrectionCandidateCallback::ValidateCandidate(candidate);
144 }
145
146private:
147 Token NextToken;
148};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000149}
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000150
Richard Smithc202b282012-04-14 00:33:13 +0000151StmtResult
152Parser::ParseStatementOrDeclarationAfterAttributes(StmtVector &Stmts,
Jonathan Roelofsce1db6d2017-03-14 17:29:33 +0000153 AllowedConstructsKind Allowed, SourceLocation *TrailingElseLoc,
Richard Smithc202b282012-04-14 00:33:13 +0000154 ParsedAttributesWithRange &Attrs) {
Craig Topper161e4db2014-05-21 06:02:52 +0000155 const char *SemiError = nullptr;
Richard Smithc202b282012-04-14 00:33:13 +0000156 StmtResult Res;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000157
Chris Lattner503fadc2006-08-10 05:45:44 +0000158 // Cases in this switch statement should fall through if the parser expects
159 // the token to end in a semicolon (in which case SemiError should be set),
160 // or they directly 'return;' if not.
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000161Retry:
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000162 tok::TokenKind Kind = Tok.getKind();
163 SourceLocation AtLoc;
164 switch (Kind) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000165 case tok::at: // May be a @try or @throw statement
166 {
Richard Smithc202b282012-04-14 00:33:13 +0000167 ProhibitAttributes(Attrs); // TODO: is it correct?
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000168 AtLoc = ConsumeToken(); // consume @
Sebastian Redlbab9a4b2008-12-11 20:12:42 +0000169 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000170 }
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000171
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000172 case tok::code_completion:
John McCallfaf5fb42010-08-26 23:41:50 +0000173 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000174 cutOffParsing();
175 return StmtError();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000176
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000177 case tok::identifier: {
178 Token Next = NextToken();
179 if (Next.is(tok::colon)) { // C99 6.8.1: labeled-statement
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000180 // identifier ':' statement
Richard Smithc202b282012-04-14 00:33:13 +0000181 return ParseLabeledStatement(Attrs);
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000182 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000183
Richard Smith4f605af2012-08-18 00:55:03 +0000184 // Look up the identifier, and typo-correct it to a keyword if it's not
185 // found.
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000186 if (Next.isNot(tok::coloncolon)) {
Richard Smith4f605af2012-08-18 00:55:03 +0000187 // Try to limit which sets of keywords should be included in typo
188 // correction based on what the next token is.
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000189 if (TryAnnotateName(/*IsAddressOfOperand*/ false,
190 llvm::make_unique<StatementFilterCCC>(Next)) ==
191 ANK_Error) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000192 // Handle errors here by skipping up to the next semicolon or '}', and
193 // eat the semicolon if that's what stopped us.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000194 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000195 if (Tok.is(tok::semi))
196 ConsumeToken();
197 return StmtError();
Richard Smith4f605af2012-08-18 00:55:03 +0000198 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000199
Richard Smith4f605af2012-08-18 00:55:03 +0000200 // If the identifier was typo-corrected, try again.
201 if (Tok.isNot(tok::identifier))
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000202 goto Retry;
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000203 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000204
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000205 // Fall through
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;
Rafael Espindola1bd906d2014-10-22 14:27:08 +0000214 DeclGroupPtrTy Decl = ParseDeclaration(Declarator::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
Eli Friedman68be1642012-10-04 02:36:51 +0000351 case tok::annot_pragma_opencl_extension:
352 ProhibitAttributes(Attrs);
353 HandlePragmaOpenCLExtension();
354 return StmtEmpty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000355
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000356 case tok::annot_pragma_captured:
Richard Smith12a41bd2013-09-16 21:17:44 +0000357 ProhibitAttributes(Attrs);
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000358 return HandlePragmaCaptured();
359
Alexey Bataeva769e072013-03-22 06:34:35 +0000360 case tok::annot_pragma_openmp:
Richard Smith12a41bd2013-09-16 21:17:44 +0000361 ProhibitAttributes(Attrs);
Alexey Bataevc4fad652016-01-13 11:18:54 +0000362 return ParseOpenMPDeclarativeOrExecutableDirective(Allowed);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000363
David Majnemer4bb09802014-02-10 19:50:15 +0000364 case tok::annot_pragma_ms_pointers_to_members:
365 ProhibitAttributes(Attrs);
366 HandlePragmaMSPointersToMembers();
367 return StmtEmpty();
368
Warren Huntc3b18962014-04-08 22:30:47 +0000369 case tok::annot_pragma_ms_pragma:
370 ProhibitAttributes(Attrs);
371 HandlePragmaMSPragma();
372 return StmtEmpty();
Aaron Ballmanb06b15a2014-06-06 12:40:24 +0000373
Alexey Bataev3d42f342015-11-20 07:02:57 +0000374 case tok::annot_pragma_ms_vtordisp:
375 ProhibitAttributes(Attrs);
376 HandlePragmaMSVtorDisp();
377 return StmtEmpty();
378
Aaron Ballmanb06b15a2014-06-06 12:40:24 +0000379 case tok::annot_pragma_loop_hint:
380 ProhibitAttributes(Attrs);
Alexey Bataevc4fad652016-01-13 11:18:54 +0000381 return ParsePragmaLoopHint(Stmts, Allowed, TrailingElseLoc, Attrs);
Richard Smithba3a4f92016-01-12 21:59:26 +0000382
383 case tok::annot_pragma_dump:
384 HandlePragmaDump();
385 return StmtEmpty();
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000386
387 case tok::annot_pragma_attribute:
388 HandlePragmaAttribute();
389 return StmtEmpty();
Sebastian Redlb219c902008-12-21 16:41:36 +0000390 }
391
Chris Lattner503fadc2006-08-10 05:45:44 +0000392 // If we reached this code, the statement must end in a semicolon.
Alp Toker97650562014-01-10 11:19:30 +0000393 if (!TryConsumeToken(tok::semi) && !Res.isInvalid()) {
Chris Lattner8e3eed02009-06-14 00:23:56 +0000394 // If the result was valid, then we do want to diagnose this. Use
395 // ExpectAndConsume to emit the diagnostic, even though we know it won't
396 // succeed.
397 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner0046de12008-11-13 18:52:53 +0000398 // Skip until we see a } or ;, but don't eat it.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000399 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner503fadc2006-08-10 05:45:44 +0000400 }
Mike Stump11289f42009-09-09 15:08:12 +0000401
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000402 return Res;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000403}
404
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000405/// \brief Parse an expression statement.
Richard Smithc202b282012-04-14 00:33:13 +0000406StmtResult Parser::ParseExprStatement() {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000407 // If a case keyword is missing, this is where it should be inserted.
408 Token OldToken = Tok;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000409
Alex Lorenzf0b4e5d2016-10-18 10:55:01 +0000410 ExprStatementTokLoc = Tok.getLocation();
411
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000412 // expression[opt] ';'
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000413 ExprResult Expr(ParseExpression());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000414 if (Expr.isInvalid()) {
415 // If the expression is invalid, skip ahead to the next semicolon or '}'.
416 // Not doing this opens us up to the possibility of infinite loops if
417 // ParseExpression does not consume any tokens.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000418 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000419 if (Tok.is(tok::semi))
420 ConsumeToken();
John McCalleaef89b2013-03-22 02:10:40 +0000421 return Actions.ActOnExprStmtError();
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000422 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000423
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000424 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
425 Actions.CheckCaseExpression(Expr.get())) {
426 // If a constant expression is followed by a colon inside a switch block,
427 // suggest a missing case keyword.
428 Diag(OldToken, diag::err_expected_case_before_expression)
429 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000430
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000431 // Recover parsing as a case statement.
Richard Smithc202b282012-04-14 00:33:13 +0000432 return ParseCaseStatement(/*MissingCase=*/true, Expr);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000433 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000434
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000435 // Otherwise, eat the semicolon.
436 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +0000437 return Actions.ActOnExprStmt(Expr);
John Wiegley1c0675e2011-04-28 01:08:34 +0000438}
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000439
John Wiegley1c0675e2011-04-28 01:08:34 +0000440/// ParseSEHTryBlockCommon
441///
442/// seh-try-block:
443/// '__try' compound-statement seh-handler
444///
445/// seh-handler:
446/// seh-except-block
447/// seh-finally-block
448///
Nico Weberdd256742015-02-25 01:43:27 +0000449StmtResult Parser::ParseSEHTryBlock() {
450 assert(Tok.is(tok::kw___try) && "Expected '__try'");
451 SourceLocation TryLoc = ConsumeToken();
452
Nico Weberfc3fe4f2015-02-25 02:22:06 +0000453 if (Tok.isNot(tok::l_brace))
Alp Tokerec543272013-12-24 09:48:30 +0000454 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
John Wiegley1c0675e2011-04-28 01:08:34 +0000455
Momchil Velikov57c681f2017-08-10 15:43:06 +0000456 StmtResult TryBlock(ParseCompoundStatement(
457 /*isStmtExpr=*/false,
458 Scope::DeclScope | Scope::CompoundStmtScope | Scope::SEHTryScope));
459 if (TryBlock.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000460 return TryBlock;
John Wiegley1c0675e2011-04-28 01:08:34 +0000461
462 StmtResult Handler;
Richard Smithc202b282012-04-14 00:33:13 +0000463 if (Tok.is(tok::identifier) &&
Douglas Gregor60060d62011-10-21 03:57:52 +0000464 Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +0000465 SourceLocation Loc = ConsumeToken();
466 Handler = ParseSEHExceptBlock(Loc);
467 } else if (Tok.is(tok::kw___finally)) {
468 SourceLocation Loc = ConsumeToken();
469 Handler = ParseSEHFinallyBlock(Loc);
470 } else {
Nico Weberfc3fe4f2015-02-25 02:22:06 +0000471 return StmtError(Diag(Tok, diag::err_seh_expected_handler));
John Wiegley1c0675e2011-04-28 01:08:34 +0000472 }
473
474 if(Handler.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000475 return Handler;
John Wiegley1c0675e2011-04-28 01:08:34 +0000476
477 return Actions.ActOnSEHTryBlock(false /* IsCXXTry */,
478 TryLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000479 TryBlock.get(),
Warren Huntf6be4cb2014-07-25 20:52:51 +0000480 Handler.get());
John Wiegley1c0675e2011-04-28 01:08:34 +0000481}
482
483/// ParseSEHExceptBlock - Handle __except
484///
485/// seh-except-block:
486/// '__except' '(' seh-filter-expression ')' compound-statement
487///
488StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) {
489 PoisonIdentifierRAIIObject raii(Ident__exception_code, false),
490 raii2(Ident___exception_code, false),
491 raii3(Ident_GetExceptionCode, false);
492
Alp Toker383d2c42014-01-01 03:08:43 +0000493 if (ExpectAndConsume(tok::l_paren))
John Wiegley1c0675e2011-04-28 01:08:34 +0000494 return StmtError();
495
Reid Kleckner1d59f992015-01-22 01:36:17 +0000496 ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope |
497 Scope::SEHExceptScope);
John Wiegley1c0675e2011-04-28 01:08:34 +0000498
David Blaikiebbafb8a2012-03-11 07:00:24 +0000499 if (getLangOpts().Borland) {
Francois Pichetbfaf4772011-04-28 03:14:31 +0000500 Ident__exception_info->setIsPoisoned(false);
501 Ident___exception_info->setIsPoisoned(false);
502 Ident_GetExceptionInfo->setIsPoisoned(false);
503 }
Reid Kleckner1d59f992015-01-22 01:36:17 +0000504
505 ExprResult FilterExpr;
506 {
507 ParseScopeFlags FilterScope(this, getCurScope()->getFlags() |
508 Scope::SEHFilterScope);
Reid Kleckner85368fb2015-04-02 22:09:32 +0000509 FilterExpr = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Reid Kleckner1d59f992015-01-22 01:36:17 +0000510 }
Francois Pichetbfaf4772011-04-28 03:14:31 +0000511
David Blaikiebbafb8a2012-03-11 07:00:24 +0000512 if (getLangOpts().Borland) {
Francois Pichetbfaf4772011-04-28 03:14:31 +0000513 Ident__exception_info->setIsPoisoned(true);
514 Ident___exception_info->setIsPoisoned(true);
515 Ident_GetExceptionInfo->setIsPoisoned(true);
516 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000517
518 if(FilterExpr.isInvalid())
519 return StmtError();
520
Alp Toker383d2c42014-01-01 03:08:43 +0000521 if (ExpectAndConsume(tok::r_paren))
John Wiegley1c0675e2011-04-28 01:08:34 +0000522 return StmtError();
523
Nico Weberfc3fe4f2015-02-25 02:22:06 +0000524 if (Tok.isNot(tok::l_brace))
525 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
526
Richard Smithc202b282012-04-14 00:33:13 +0000527 StmtResult Block(ParseCompoundStatement());
John Wiegley1c0675e2011-04-28 01:08:34 +0000528
529 if(Block.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000530 return Block;
John Wiegley1c0675e2011-04-28 01:08:34 +0000531
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000532 return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.get(), Block.get());
John Wiegley1c0675e2011-04-28 01:08:34 +0000533}
534
535/// ParseSEHFinallyBlock - Handle __finally
536///
537/// seh-finally-block:
538/// '__finally' compound-statement
539///
Nico Weberd64657f2015-03-09 02:47:59 +0000540StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyLoc) {
John Wiegley1c0675e2011-04-28 01:08:34 +0000541 PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
542 raii2(Ident___abnormal_termination, false),
543 raii3(Ident_AbnormalTermination, false);
544
Nico Weberfc3fe4f2015-02-25 02:22:06 +0000545 if (Tok.isNot(tok::l_brace))
546 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
547
Nico Weberd64657f2015-03-09 02:47:59 +0000548 ParseScope FinallyScope(this, 0);
549 Actions.ActOnStartSEHFinallyBlock();
550
Richard Smithc202b282012-04-14 00:33:13 +0000551 StmtResult Block(ParseCompoundStatement());
Nico Weberce903292015-03-09 03:17:15 +0000552 if(Block.isInvalid()) {
553 Actions.ActOnAbortSEHFinallyBlock();
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000554 return Block;
Nico Weberce903292015-03-09 03:17:15 +0000555 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000556
Nico Weberd64657f2015-03-09 02:47:59 +0000557 return Actions.ActOnFinishSEHFinallyBlock(FinallyLoc, Block.get());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000558}
559
Nico Weberc7d05962014-07-06 22:32:59 +0000560/// Handle __leave
561///
562/// seh-leave-statement:
563/// '__leave' ';'
564///
565StmtResult Parser::ParseSEHLeaveStatement() {
566 SourceLocation LeaveLoc = ConsumeToken(); // eat the '__leave'.
567 return Actions.ActOnSEHLeaveStmt(LeaveLoc, getCurScope());
568}
569
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000570/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000571///
572/// labeled-statement:
573/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000574/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000575///
Richard Smithc202b282012-04-14 00:33:13 +0000576StmtResult Parser::ParseLabeledStatement(ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000577 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
578 "Not an identifier!");
579
580 Token IdentTok = Tok; // Save the whole token.
581 ConsumeToken(); // eat the identifier.
582
583 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000584
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000585 // identifier ':' statement
586 SourceLocation ColonLoc = ConsumeToken();
587
Richard Smitha3e01cf2013-11-15 22:45:29 +0000588 // Read label attributes, if present.
589 StmtResult SubStmt;
590 if (Tok.is(tok::kw___attribute)) {
591 ParsedAttributesWithRange TempAttrs(AttrFactory);
592 ParseGNUAttributes(TempAttrs);
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000593
Richard Smitha3e01cf2013-11-15 22:45:29 +0000594 // In C++, GNU attributes only apply to the label if they are followed by a
595 // semicolon, to disambiguate label attributes from attributes on a labeled
596 // declaration.
597 //
598 // This doesn't quite match what GCC does; if the attribute list is empty
599 // and followed by a semicolon, GCC will reject (it appears to parse the
600 // attributes as part of a statement in that case). That looks like a bug.
601 if (!getLangOpts().CPlusPlus || Tok.is(tok::semi))
602 attrs.takeAllFrom(TempAttrs);
603 else if (isDeclarationStatement()) {
604 StmtVector Stmts;
605 // FIXME: We should do this whether or not we have a declaration
606 // statement, but that doesn't work correctly (because ProhibitAttributes
607 // can't handle GNU attributes), so only call it in the one case where
608 // GNU attributes are allowed.
609 SubStmt = ParseStatementOrDeclarationAfterAttributes(
Alexey Bataevc4fad652016-01-13 11:18:54 +0000610 Stmts, /*Allowed=*/ACK_StatementsOpenMPNonStandalone, nullptr,
611 TempAttrs);
Richard Smitha3e01cf2013-11-15 22:45:29 +0000612 if (!TempAttrs.empty() && !SubStmt.isInvalid())
613 SubStmt = Actions.ProcessStmtAttributes(
614 SubStmt.get(), TempAttrs.getList(), TempAttrs.Range);
615 } else {
Alp Toker383d2c42014-01-01 03:08:43 +0000616 Diag(Tok, diag::err_expected_after) << "__attribute__" << tok::semi;
Richard Smitha3e01cf2013-11-15 22:45:29 +0000617 }
618 }
619
620 // If we've not parsed a statement yet, parse one now.
621 if (!SubStmt.isInvalid() && !SubStmt.isUsable())
622 SubStmt = ParseStatement();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000623
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000624 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000625 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000626 SubStmt = Actions.ActOnNullStmt(ColonLoc);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000627
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000628 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
629 IdentTok.getLocation());
Richard Smithc202b282012-04-14 00:33:13 +0000630 if (AttributeList *Attrs = attrs.getList()) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000631 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
Richard Smithc202b282012-04-14 00:33:13 +0000632 attrs.clear();
633 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000634
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000635 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
636 SubStmt.get());
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000637}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000638
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000639/// ParseCaseStatement
640/// labeled-statement:
641/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000642/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000643///
Richard Smithc202b282012-04-14 00:33:13 +0000644StmtResult Parser::ParseCaseStatement(bool MissingCase, ExprResult Expr) {
Richard Smithd4257d82011-04-21 22:48:40 +0000645 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Mike Stump11289f42009-09-09 15:08:12 +0000646
Chris Lattner34a22092009-03-04 04:23:07 +0000647 // It is very very common for code to contain many case statements recursively
648 // nested, as in (but usually without indentation):
649 // case 1:
650 // case 2:
651 // case 3:
652 // case 4:
653 // case 5: etc.
654 //
655 // Parsing this naively works, but is both inefficient and can cause us to run
656 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner2b19a6582009-03-04 18:24:58 +0000657 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner34a22092009-03-04 04:23:07 +0000658 // but all the grossness is constrained to ParseCaseStatement (and some
Richard Smitha3e01cf2013-11-15 22:45:29 +0000659 // weirdness in the actions), so this is just local grossness :).
Mike Stump11289f42009-09-09 15:08:12 +0000660
Chris Lattner34a22092009-03-04 04:23:07 +0000661 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
662 // example above.
John McCalldadc5752010-08-24 06:29:42 +0000663 StmtResult TopLevelCase(true);
Mike Stump11289f42009-09-09 15:08:12 +0000664
Chris Lattner34a22092009-03-04 04:23:07 +0000665 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
666 // gets updated each time a new case is parsed, and whose body is unset so
667 // far. When parsing 'case 4', this is the 'case 3' node.
Craig Topper161e4db2014-05-21 06:02:52 +0000668 Stmt *DeepestParsedCaseStmt = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000669
Chris Lattner34a22092009-03-04 04:23:07 +0000670 // While we have case statements, eat and stack them.
David Majnemer0ac67fa2011-06-13 05:50:12 +0000671 SourceLocation ColonLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000672 do {
Richard Trieu2c850c02011-04-21 21:44:26 +0000673 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
674 ConsumeToken(); // eat the 'case'.
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000675 ColonLoc = SourceLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000676
Douglas Gregord328d572009-09-21 18:10:23 +0000677 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000678 Actions.CodeCompleteCase(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000679 cutOffParsing();
680 return StmtError();
Douglas Gregord328d572009-09-21 18:10:23 +0000681 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000682
Chris Lattner125c0ee2009-12-10 00:38:54 +0000683 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
684 /// Disable this form of error recovery while we're parsing the case
685 /// expression.
686 ColonProtectionRAIIObject ColonProtection(*this);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000687
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000688 ExprResult LHS;
689 if (!MissingCase) {
690 LHS = ParseConstantExpression();
Kaelyn Takatab16e6322014-11-20 22:06:40 +0000691 if (!getLangOpts().CPlusPlus11) {
692 LHS = Actions.CorrectDelayedTyposInExpr(LHS, [this](class Expr *E) {
693 return Actions.VerifyIntegerConstantExpression(E);
694 });
695 }
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000696 if (LHS.isInvalid()) {
697 // If constant-expression is parsed unsuccessfully, recover by skipping
698 // current case statement (moving to the colon that ends it).
699 if (SkipUntil(tok::colon, tok::r_brace, StopAtSemi | StopBeforeMatch)) {
700 TryConsumeToken(tok::colon, ColonLoc);
701 continue;
702 }
703 return StmtError();
704 }
705 } else {
706 LHS = Expr;
707 MissingCase = false;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000708 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000709
Chris Lattner34a22092009-03-04 04:23:07 +0000710 // GNU case range extension.
711 SourceLocation DotDotDotLoc;
John McCalldadc5752010-08-24 06:29:42 +0000712 ExprResult RHS;
Alp Tokerec543272013-12-24 09:48:30 +0000713 if (TryConsumeToken(tok::ellipsis, DotDotDotLoc)) {
714 Diag(DotDotDotLoc, diag::ext_gnu_case_range);
Chris Lattner34a22092009-03-04 04:23:07 +0000715 RHS = ParseConstantExpression();
716 if (RHS.isInvalid()) {
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000717 if (SkipUntil(tok::colon, tok::r_brace, StopAtSemi | StopBeforeMatch)) {
718 TryConsumeToken(tok::colon, ColonLoc);
719 continue;
720 }
Chris Lattner34a22092009-03-04 04:23:07 +0000721 return StmtError();
722 }
723 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000724
Chris Lattner125c0ee2009-12-10 00:38:54 +0000725 ColonProtection.restore();
Sebastian Redl042ad952008-12-11 19:30:53 +0000726
Alp Tokerec543272013-12-24 09:48:30 +0000727 if (TryConsumeToken(tok::colon, ColonLoc)) {
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000728 } else if (TryConsumeToken(tok::semi, ColonLoc) ||
729 TryConsumeToken(tok::coloncolon, ColonLoc)) {
730 // Treat "case blah;" or "case blah::" as a typo for "case blah:".
Alp Tokerec543272013-12-24 09:48:30 +0000731 Diag(ColonLoc, diag::err_expected_after)
732 << "'case'" << tok::colon
733 << FixItHint::CreateReplacement(ColonLoc, ":");
John McCall0140bfe2011-01-22 09:28:32 +0000734 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000735 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
Alp Tokerec543272013-12-24 09:48:30 +0000736 Diag(ExpectedLoc, diag::err_expected_after)
737 << "'case'" << tok::colon
738 << FixItHint::CreateInsertion(ExpectedLoc, ":");
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000739 ColonLoc = ExpectedLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000740 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000741
John McCalldadc5752010-08-24 06:29:42 +0000742 StmtResult Case =
John McCallb268a282010-08-23 23:25:46 +0000743 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
744 RHS.get(), ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000745
Chris Lattner34a22092009-03-04 04:23:07 +0000746 // If we had a sema error parsing this case, then just ignore it and
747 // continue parsing the sub-stmt.
748 if (Case.isInvalid()) {
749 if (TopLevelCase.isInvalid()) // No parsed case stmts.
Alexey Bataevc4fad652016-01-13 11:18:54 +0000750 return ParseStatement(/*TrailingElseLoc=*/nullptr,
751 /*AllowOpenMPStandalone=*/true);
Chris Lattner34a22092009-03-04 04:23:07 +0000752 // Otherwise, just don't add it as a nested case.
753 } else {
754 // If this is the first case statement we parsed, it becomes TopLevelCase.
755 // Otherwise we link it into the current chain.
John McCall37ad5512010-08-23 06:44:23 +0000756 Stmt *NextDeepest = Case.get();
Chris Lattner34a22092009-03-04 04:23:07 +0000757 if (TopLevelCase.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000758 TopLevelCase = Case;
Chris Lattner34a22092009-03-04 04:23:07 +0000759 else
John McCallb268a282010-08-23 23:25:46 +0000760 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner34a22092009-03-04 04:23:07 +0000761 DeepestParsedCaseStmt = NextDeepest;
762 }
Mike Stump11289f42009-09-09 15:08:12 +0000763
Chris Lattner34a22092009-03-04 04:23:07 +0000764 // Handle all case statements.
765 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000766
Chris Lattner34a22092009-03-04 04:23:07 +0000767 // If we found a non-case statement, start by parsing it.
John McCalldadc5752010-08-24 06:29:42 +0000768 StmtResult SubStmt;
Mike Stump11289f42009-09-09 15:08:12 +0000769
Chris Lattner34a22092009-03-04 04:23:07 +0000770 if (Tok.isNot(tok::r_brace)) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000771 SubStmt = ParseStatement(/*TrailingElseLoc=*/nullptr,
772 /*AllowOpenMPStandalone=*/true);
Chris Lattner34a22092009-03-04 04:23:07 +0000773 } else {
774 // Nicely diagnose the common error "switch (X) { case 4: }", which is
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000775 // not valid. If ColonLoc doesn't point to a valid text location, there was
776 // another parsing error, so avoid producing extra diagnostics.
777 if (ColonLoc.isValid()) {
778 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
779 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
780 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
781 }
782 SubStmt = StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000783 }
Mike Stump11289f42009-09-09 15:08:12 +0000784
Chris Lattner34a22092009-03-04 04:23:07 +0000785 // Install the body into the most deeply-nested case.
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000786 if (DeepestParsedCaseStmt) {
787 // Broken sub-stmt shouldn't prevent forming the case statement properly.
788 if (SubStmt.isInvalid())
789 SubStmt = Actions.ActOnNullStmt(SourceLocation());
790 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
791 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000792
Chris Lattner34a22092009-03-04 04:23:07 +0000793 // Return the top level parsed statement tree.
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000794 return TopLevelCase;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000795}
796
797/// ParseDefaultStatement
798/// labeled-statement:
799/// 'default' ':' statement
800/// Note that this does not parse the 'statement' at the end.
801///
Richard Smithc202b282012-04-14 00:33:13 +0000802StmtResult Parser::ParseDefaultStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000803 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000804 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000805
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000806 SourceLocation ColonLoc;
Alp Tokerec543272013-12-24 09:48:30 +0000807 if (TryConsumeToken(tok::colon, ColonLoc)) {
Alp Tokerec543272013-12-24 09:48:30 +0000808 } else if (TryConsumeToken(tok::semi, ColonLoc)) {
Alp Toker97650562014-01-10 11:19:30 +0000809 // Treat "default;" as a typo for "default:".
Alp Tokerec543272013-12-24 09:48:30 +0000810 Diag(ColonLoc, diag::err_expected_after)
811 << "'default'" << tok::colon
812 << FixItHint::CreateReplacement(ColonLoc, ":");
John McCall0140bfe2011-01-22 09:28:32 +0000813 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000814 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
Alp Tokerec543272013-12-24 09:48:30 +0000815 Diag(ExpectedLoc, diag::err_expected_after)
816 << "'default'" << tok::colon
817 << FixItHint::CreateInsertion(ExpectedLoc, ":");
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000818 ColonLoc = ExpectedLoc;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000819 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000820
Richard Smith1002d102012-02-17 01:35:32 +0000821 StmtResult SubStmt;
822
823 if (Tok.isNot(tok::r_brace)) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000824 SubStmt = ParseStatement(/*TrailingElseLoc=*/nullptr,
825 /*AllowOpenMPStandalone=*/true);
Richard Smith1002d102012-02-17 01:35:32 +0000826 } else {
827 // Diagnose the common error "switch (X) {... default: }", which is
828 // not valid.
David Majnemerc6a99872011-06-14 15:24:38 +0000829 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
Richard Smith1002d102012-02-17 01:35:32 +0000830 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
831 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
832 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000833 }
834
Richard Smith1002d102012-02-17 01:35:32 +0000835 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000836 if (SubStmt.isInvalid())
Richard Smith1002d102012-02-17 01:35:32 +0000837 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl042ad952008-12-11 19:30:53 +0000838
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000839 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000840 SubStmt.get(), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000841}
842
Richard Smithc202b282012-04-14 00:33:13 +0000843StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Momchil Velikov57c681f2017-08-10 15:43:06 +0000844 return ParseCompoundStatement(isStmtExpr,
845 Scope::DeclScope | Scope::CompoundStmtScope);
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000846}
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000847
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000848/// ParseCompoundStatement - Parse a "{}" block.
849///
850/// compound-statement: [C99 6.8.2]
851/// { block-item-list[opt] }
852/// [GNU] { label-declarations block-item-list } [TODO]
853///
854/// block-item-list:
855/// block-item
856/// block-item-list block-item
857///
858/// block-item:
859/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000860/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000861/// statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000862///
863/// [GNU] label-declarations:
864/// [GNU] label-declaration
865/// [GNU] label-declarations label-declaration
866///
867/// [GNU] label-declaration:
868/// [GNU] '__label__' identifier-list ';'
869///
Richard Smithc202b282012-04-14 00:33:13 +0000870StmtResult Parser::ParseCompoundStatement(bool isStmtExpr,
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000871 unsigned ScopeFlags) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000872 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000873
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000874 // Enter a scope to hold everything within the compound stmt. Compound
875 // statements can always hold declarations.
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000876 ParseScope CompoundScope(this, ScopeFlags);
Chris Lattnerf2978802007-01-21 06:52:16 +0000877
878 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000879 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000880}
881
Lang Hames2954cea2012-11-03 22:29:05 +0000882/// Parse any pragmas at the start of the compound expression. We handle these
883/// separately since some pragmas (FP_CONTRACT) must appear before any C
884/// statement in the compound, but may be intermingled with other pragmas.
885void Parser::ParseCompoundStatementLeadingPragmas() {
886 bool checkForPragmas = true;
887 while (checkForPragmas) {
888 switch (Tok.getKind()) {
889 case tok::annot_pragma_vis:
890 HandlePragmaVisibility();
891 break;
892 case tok::annot_pragma_pack:
893 HandlePragmaPack();
894 break;
895 case tok::annot_pragma_msstruct:
896 HandlePragmaMSStruct();
897 break;
898 case tok::annot_pragma_align:
899 HandlePragmaAlign();
900 break;
901 case tok::annot_pragma_weak:
902 HandlePragmaWeak();
903 break;
904 case tok::annot_pragma_weakalias:
905 HandlePragmaWeakAlias();
906 break;
907 case tok::annot_pragma_redefine_extname:
908 HandlePragmaRedefineExtname();
909 break;
910 case tok::annot_pragma_opencl_extension:
911 HandlePragmaOpenCLExtension();
912 break;
913 case tok::annot_pragma_fp_contract:
914 HandlePragmaFPContract();
915 break;
Adam Nemet60d32642017-04-04 21:18:36 +0000916 case tok::annot_pragma_fp:
917 HandlePragmaFP();
918 break;
David Majnemer4bb09802014-02-10 19:50:15 +0000919 case tok::annot_pragma_ms_pointers_to_members:
920 HandlePragmaMSPointersToMembers();
921 break;
Warren Huntc3b18962014-04-08 22:30:47 +0000922 case tok::annot_pragma_ms_pragma:
923 HandlePragmaMSPragma();
924 break;
Alexey Bataev3d42f342015-11-20 07:02:57 +0000925 case tok::annot_pragma_ms_vtordisp:
926 HandlePragmaMSVtorDisp();
927 break;
Richard Smithba3a4f92016-01-12 21:59:26 +0000928 case tok::annot_pragma_dump:
929 HandlePragmaDump();
930 break;
Lang Hames2954cea2012-11-03 22:29:05 +0000931 default:
932 checkForPragmas = false;
933 break;
934 }
935 }
936
937}
938
Chris Lattnerf2978802007-01-21 06:52:16 +0000939/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000940/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000941/// consume the '}' at the end of the block. It does not manipulate the scope
942/// stack.
John McCalldadc5752010-08-24 06:29:42 +0000943StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000944 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000945 Tok.getLocation(),
946 "in compound statement ('{}')");
Lang Hames5de91cc2012-10-02 04:45:10 +0000947
948 // Record the state of the FP_CONTRACT pragma, restore on leaving the
949 // compound statement.
950 Sema::FPContractStateRAII SaveFPContractState(Actions);
951
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000952 InMessageExpressionRAIIObject InMessage(*this, false);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000953 BalancedDelimiterTracker T(*this, tok::l_brace);
954 if (T.consumeOpen())
955 return StmtError();
Chris Lattnerf2978802007-01-21 06:52:16 +0000956
Dmitri Gribenko800ddf32012-02-14 22:14:32 +0000957 Sema::CompoundScopeRAII CompoundScope(Actions);
958
Lang Hames2954cea2012-11-03 22:29:05 +0000959 // Parse any pragmas at the beginning of the compound statement.
960 ParseCompoundStatementLeadingPragmas();
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000961
Lang Hames2954cea2012-11-03 22:29:05 +0000962 StmtVector Stmts;
Lang Hamesa930e712012-10-21 01:10:01 +0000963
Chris Lattner43e7f312011-02-18 02:08:43 +0000964 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
965 // only allowed at the start of a compound stmt regardless of the language.
966 while (Tok.is(tok::kw___label__)) {
967 SourceLocation LabelLoc = ConsumeToken();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000968
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000969 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner43e7f312011-02-18 02:08:43 +0000970 while (1) {
971 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000972 Diag(Tok, diag::err_expected) << tok::identifier;
Chris Lattner43e7f312011-02-18 02:08:43 +0000973 break;
974 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000975
Chris Lattner43e7f312011-02-18 02:08:43 +0000976 IdentifierInfo *II = Tok.getIdentifierInfo();
977 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000978 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000979
Alp Tokerec543272013-12-24 09:48:30 +0000980 if (!TryConsumeToken(tok::comma))
Chris Lattner43e7f312011-02-18 02:08:43 +0000981 break;
Chris Lattner43e7f312011-02-18 02:08:43 +0000982 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000983
John McCall084e83d2011-03-24 11:26:52 +0000984 DeclSpec DS(AttrFactory);
Rafael Espindolaab417692013-07-09 12:05:01 +0000985 DeclGroupPtrTy Res =
986 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner43e7f312011-02-18 02:08:43 +0000987 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000988
Chris Lattner02f1b612012-04-28 16:12:17 +0000989 ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
Chris Lattner43e7f312011-02-18 02:08:43 +0000990 if (R.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000991 Stmts.push_back(R.get());
Chris Lattner43e7f312011-02-18 02:08:43 +0000992 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000993
Richard Smith752ada82015-11-17 23:32:01 +0000994 while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
995 Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000996 if (Tok.is(tok::annot_pragma_unused)) {
997 HandlePragmaUnused();
998 continue;
999 }
1000
John McCalldadc5752010-08-24 06:29:42 +00001001 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001002 if (Tok.isNot(tok::kw___extension__)) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00001003 R = ParseStatementOrDeclaration(Stmts, ACK_Any);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001004 } else {
1005 // __extension__ can start declarations and it can also be a unary
1006 // operator for expressions. Consume multiple __extension__ markers here
1007 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +00001008 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001009 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001010 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001011 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +00001012
John McCall084e83d2011-03-24 11:26:52 +00001013 ParsedAttributesWithRange attrs(AttrFactory);
Craig Topper161e4db2014-05-21 06:02:52 +00001014 MaybeParseCXX11Attributes(attrs, nullptr,
1015 /*MightBeObjCMessageSend*/ true);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001016
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001017 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001018 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +00001019 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +00001020 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +00001021 ExtensionRAIIObject O(Diags);
1022
Chris Lattner49836b42009-04-02 04:16:50 +00001023 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Rafael Espindola1bd906d2014-10-22 14:27:08 +00001024 DeclGroupPtrTy Res = ParseDeclaration(Declarator::BlockContext, DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001025 attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001026 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001027 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +00001028 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +00001029 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +00001030
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001031 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001032 SkipUntil(tok::semi);
1033 continue;
1034 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001035
Alexis Hunt96d5c762009-11-21 08:43:09 +00001036 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +00001037 // Eat the semicolon at the end of stmt and convert the expr into a
1038 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00001039 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +00001040 R = Actions.ActOnExprStmt(Res);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001041 }
1042 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001043
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001044 if (R.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001045 Stmts.push_back(R.get());
Chris Lattner30f910e2006-10-16 05:52:41 +00001046 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001047
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +00001048 SourceLocation CloseLoc = Tok.getLocation();
1049
Chris Lattner0ccd51e2006-08-09 05:47:47 +00001050 // We broke out of the while loop because we found a '}' or EOF.
Nico Webera48b6c22012-12-30 23:36:56 +00001051 if (!T.consumeClose())
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +00001052 // Recover by creating a compound statement with what we parsed so far,
1053 // instead of dropping everything and returning StmtError();
Nico Webera48b6c22012-12-30 23:36:56 +00001054 CloseLoc = T.getCloseLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +00001055
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +00001056 return Actions.ActOnCompoundStmt(T.getOpenLocation(), CloseLoc,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001057 Stmts, isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +00001058}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001059
Chris Lattnerc0081db2008-12-12 06:31:07 +00001060/// ParseParenExprOrCondition:
1061/// [C ] '(' expression ')'
Richard Smithc7a05a92016-06-29 21:17:59 +00001062/// [C++] '(' condition ')'
1063/// [C++1z] '(' init-statement[opt] condition ')'
Chris Lattnerc0081db2008-12-12 06:31:07 +00001064///
1065/// This function parses and performs error recovery on the specified condition
1066/// or expression (depending on whether we're in C++ or C mode). This function
1067/// goes out of its way to recover well. It returns true if there was a parser
1068/// error (the right paren couldn't be found), which indicates that the caller
1069/// should try to recover harder. It returns false if the condition is
1070/// successfully parsed. Note that a successful parse can still have semantic
1071/// errors in the condition.
Richard Smithc7a05a92016-06-29 21:17:59 +00001072bool Parser::ParseParenExprOrCondition(StmtResult *InitStmt,
1073 Sema::ConditionResult &Cond,
Douglas Gregore60e41a2010-05-06 17:25:47 +00001074 SourceLocation Loc,
Richard Smith03a4aa32016-06-23 19:02:52 +00001075 Sema::ConditionKind CK) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001076 BalancedDelimiterTracker T(*this, tok::l_paren);
1077 T.consumeOpen();
1078
David Blaikiebbafb8a2012-03-11 07:00:24 +00001079 if (getLangOpts().CPlusPlus)
Richard Smithc7a05a92016-06-29 21:17:59 +00001080 Cond = ParseCXXCondition(InitStmt, Loc, CK);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001081 else {
Richard Smith03a4aa32016-06-23 19:02:52 +00001082 ExprResult CondExpr = ParseExpression();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001083
Douglas Gregore60e41a2010-05-06 17:25:47 +00001084 // If required, convert to a boolean value.
Richard Smith03a4aa32016-06-23 19:02:52 +00001085 if (CondExpr.isInvalid())
1086 Cond = Sema::ConditionError();
1087 else
1088 Cond = Actions.ActOnCondition(getCurScope(), Loc, CondExpr.get(), CK);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001089 }
Mike Stump11289f42009-09-09 15:08:12 +00001090
Chris Lattnerc0081db2008-12-12 06:31:07 +00001091 // If the parser was confused by the condition and we don't have a ')', try to
1092 // recover by skipping ahead to a semi and bailing out. If condexp is
1093 // semantically invalid but we have well formed code, keep going.
Richard Smith03a4aa32016-06-23 19:02:52 +00001094 if (Cond.isInvalid() && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +00001095 SkipUntil(tok::semi);
1096 // Skipping may have stopped if it found the containing ')'. If so, we can
1097 // continue parsing the if statement.
1098 if (Tok.isNot(tok::r_paren))
1099 return true;
1100 }
Mike Stump11289f42009-09-09 15:08:12 +00001101
Chris Lattnerc0081db2008-12-12 06:31:07 +00001102 // Otherwise the condition is valid or the rparen is present.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001103 T.consumeClose();
Chad Rosier67055f52012-07-10 21:35:27 +00001104
Chris Lattner70d44982012-04-28 16:24:20 +00001105 // Check for extraneous ')'s to catch things like "if (foo())) {". We know
1106 // that all callers are looking for a statement after the condition, so ")"
1107 // isn't valid.
1108 while (Tok.is(tok::r_paren)) {
1109 Diag(Tok, diag::err_extraneous_rparen_in_condition)
1110 << FixItHint::CreateRemoval(Tok.getLocation());
1111 ConsumeParen();
1112 }
Chad Rosier67055f52012-07-10 21:35:27 +00001113
Chris Lattnerc0081db2008-12-12 06:31:07 +00001114 return false;
1115}
1116
1117
Chris Lattnerc951dae2006-08-10 04:23:57 +00001118/// ParseIfStatement
1119/// if-statement: [C99 6.8.4.1]
1120/// 'if' '(' expression ')' statement
1121/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001122/// [C++] 'if' '(' condition ')' statement
1123/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +00001124///
Richard Smithc202b282012-04-14 00:33:13 +00001125StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001126 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001127 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +00001128
Richard Smithb130fe72016-06-23 19:16:49 +00001129 bool IsConstexpr = false;
1130 if (Tok.is(tok::kw_constexpr)) {
1131 Diag(Tok, getLangOpts().CPlusPlus1z ? diag::warn_cxx14_compat_constexpr_if
1132 : diag::ext_constexpr_if);
1133 IsConstexpr = true;
1134 ConsumeToken();
1135 }
1136
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001137 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001138 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +00001139 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +00001140 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +00001141 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001142
David Blaikiebbafb8a2012-03-11 07:00:24 +00001143 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001144
Chris Lattner2dd1b722007-08-26 23:08:06 +00001145 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
1146 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001147 //
1148 // C++ 6.4p3:
1149 // A name introduced by a declaration in a condition is in scope from its
1150 // point of declaration until the end of the substatements controlled by the
1151 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001152 // C++ 3.3.2p4:
1153 // Names declared in the for-init-statement, and in the condition of if,
1154 // while, for, and switch statements are local to the if, while, for, or
1155 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001156 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001157 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +00001158
Chris Lattnerc951dae2006-08-10 04:23:57 +00001159 // Parse the condition.
Richard Smithc7a05a92016-06-29 21:17:59 +00001160 StmtResult InitStmt;
Richard Smith03a4aa32016-06-23 19:02:52 +00001161 Sema::ConditionResult Cond;
Richard Smithc7a05a92016-06-29 21:17:59 +00001162 if (ParseParenExprOrCondition(&InitStmt, Cond, IfLoc,
Richard Smithb130fe72016-06-23 19:16:49 +00001163 IsConstexpr ? Sema::ConditionKind::ConstexprIf
1164 : Sema::ConditionKind::Boolean))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001165 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +00001166
Richard Smithb130fe72016-06-23 19:16:49 +00001167 llvm::Optional<bool> ConstexprCondition;
1168 if (IsConstexpr)
1169 ConstexprCondition = Cond.getKnownValue();
1170
Chris Lattner8fb26252007-08-22 05:28:50 +00001171 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001172 // there is no compound stmt. C90 does not have this clause. We only do this
1173 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001174 //
1175 // C++ 6.4p1:
1176 // The substatement in a selection-statement (each substatement, in the else
1177 // form of the if statement) implicitly defines a local scope.
1178 //
1179 // For C++ we create a scope for the condition and a new scope for
1180 // substatements because:
1181 // -When the 'then' scope exits, we want the condition declaration to still be
1182 // active for the 'else' scope too.
1183 // -Sema will detect name clashes by considering declarations of a
1184 // 'ControlScope' as part of its direct subscope.
1185 // -If we wanted the condition and substatement to be in the same scope, we
1186 // would have to notify ParseStatement not to create a new scope. It's
1187 // simpler to let it create a new scope.
1188 //
David Majnemer2206bf52014-03-05 08:57:59 +00001189 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001190
Chris Lattner5c5808a2007-10-29 05:08:52 +00001191 // Read the 'then' stmt.
1192 SourceLocation ThenStmtLoc = Tok.getLocation();
Nico Weber3cef1082011-12-22 23:26:17 +00001193
1194 SourceLocation InnerStatementTrailingElseLoc;
Richard Smithb130fe72016-06-23 19:16:49 +00001195 StmtResult ThenStmt;
1196 {
1197 EnterExpressionEvaluationContext PotentiallyDiscarded(
Faisal Valid143a0c2017-04-01 21:30:49 +00001198 Actions, Sema::ExpressionEvaluationContext::DiscardedStatement, nullptr,
1199 false,
Richard Smithb130fe72016-06-23 19:16:49 +00001200 /*ShouldEnter=*/ConstexprCondition && !*ConstexprCondition);
1201 ThenStmt = ParseStatement(&InnerStatementTrailingElseLoc);
1202 }
Chris Lattnerac4471c2007-05-28 05:38:24 +00001203
Chris Lattner37e54f42007-08-22 05:16:28 +00001204 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001205 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001206
Chris Lattnerc951dae2006-08-10 04:23:57 +00001207 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +00001208 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +00001209 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +00001210 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001211
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001212 if (Tok.is(tok::kw_else)) {
Nico Weber3cef1082011-12-22 23:26:17 +00001213 if (TrailingElseLoc)
1214 *TrailingElseLoc = Tok.getLocation();
1215
Chris Lattneraf635312006-10-16 06:06:51 +00001216 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +00001217 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +00001218
Chris Lattner8fb26252007-08-22 05:28:50 +00001219 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001220 // there is no compound stmt. C90 does not have this clause. We only do
1221 // this if the body isn't a compound statement to avoid push/pop in common
1222 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001223 //
1224 // C++ 6.4p1:
1225 // The substatement in a selection-statement (each substatement, in the else
1226 // form of the if statement) implicitly defines a local scope.
1227 //
Richard Smithb130fe72016-06-23 19:16:49 +00001228 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX,
1229 Tok.is(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001230
Richard Smithb130fe72016-06-23 19:16:49 +00001231 EnterExpressionEvaluationContext PotentiallyDiscarded(
Faisal Valid143a0c2017-04-01 21:30:49 +00001232 Actions, Sema::ExpressionEvaluationContext::DiscardedStatement, nullptr,
1233 false,
Richard Smithb130fe72016-06-23 19:16:49 +00001234 /*ShouldEnter=*/ConstexprCondition && *ConstexprCondition);
Chris Lattner30f910e2006-10-16 05:52:41 +00001235 ElseStmt = ParseStatement();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001236
Chris Lattner37e54f42007-08-22 05:16:28 +00001237 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001238 InnerScope.Exit();
Douglas Gregor4ecb7202011-07-30 08:36:53 +00001239 } else if (Tok.is(tok::code_completion)) {
1240 Actions.CodeCompleteAfterIf(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001241 cutOffParsing();
1242 return StmtError();
Nico Weber3cef1082011-12-22 23:26:17 +00001243 } else if (InnerStatementTrailingElseLoc.isValid()) {
1244 Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else);
Chris Lattnerc951dae2006-08-10 04:23:57 +00001245 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001246
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001247 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +00001248
Chris Lattner5c5808a2007-10-29 05:08:52 +00001249 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +00001250 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +00001251 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001252 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
Craig Topper161e4db2014-05-21 06:02:52 +00001253 (ThenStmt.isInvalid() && ElseStmt.get() == nullptr) ||
1254 (ThenStmt.get() == nullptr && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +00001255 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +00001256 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +00001257 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001258
Chris Lattner5c5808a2007-10-29 05:08:52 +00001259 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001260 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001261 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001262 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001263 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001264
Richard Smithc7a05a92016-06-29 21:17:59 +00001265 return Actions.ActOnIfStmt(IfLoc, IsConstexpr, InitStmt.get(), Cond,
1266 ThenStmt.get(), ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +00001267}
1268
Chris Lattner9075bd72006-08-10 04:59:57 +00001269/// ParseSwitchStatement
1270/// switch-statement:
1271/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001272/// [C++] 'switch' '(' condition ')' statement
Richard Smithc202b282012-04-14 00:33:13 +00001273StmtResult Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001274 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001275 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +00001276
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001277 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001278 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +00001279 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001280 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001281 }
Chris Lattner2dd1b722007-08-26 23:08:06 +00001282
David Blaikiebbafb8a2012-03-11 07:00:24 +00001283 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001284
Chris Lattner2dd1b722007-08-26 23:08:06 +00001285 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1286 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001287 //
1288 // C++ 6.4p3:
1289 // A name introduced by a declaration in a condition is in scope from its
1290 // point of declaration until the end of the substatements controlled by the
1291 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001292 // C++ 3.3.2p4:
1293 // Names declared in the for-init-statement, and in the condition of if,
1294 // while, for, and switch statements are local to the if, while, for, or
1295 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001296 //
Serge Pavlov09f99242014-01-23 15:05:00 +00001297 unsigned ScopeFlags = Scope::SwitchScope;
Chris Lattnerc0081db2008-12-12 06:31:07 +00001298 if (C99orCXX)
1299 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001300 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001301
Chris Lattner9075bd72006-08-10 04:59:57 +00001302 // Parse the condition.
Richard Smithc7a05a92016-06-29 21:17:59 +00001303 StmtResult InitStmt;
Richard Smith03a4aa32016-06-23 19:02:52 +00001304 Sema::ConditionResult Cond;
Richard Smithc7a05a92016-06-29 21:17:59 +00001305 if (ParseParenExprOrCondition(&InitStmt, Cond, SwitchLoc,
1306 Sema::ConditionKind::Switch))
Sebastian Redlb62406f2008-12-11 19:48:14 +00001307 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +00001308
Richard Smithc7a05a92016-06-29 21:17:59 +00001309 StmtResult Switch =
1310 Actions.ActOnStartOfSwitchStmt(SwitchLoc, InitStmt.get(), Cond);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001311
Douglas Gregore60e41a2010-05-06 17:25:47 +00001312 if (Switch.isInvalid()) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001313 // Skip the switch body.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001314 // FIXME: This is not optimal recovery, but parsing the body is more
1315 // dangerous due to the presence of case and default statements, which
1316 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +00001317 if (Tok.is(tok::l_brace)) {
1318 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001319 SkipUntil(tok::r_brace);
Douglas Gregor4abc32d2010-05-20 23:20:59 +00001320 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +00001321 SkipUntil(tok::semi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001322 return Switch;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001323 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001324
Chris Lattner8fb26252007-08-22 05:28:50 +00001325 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001326 // there is no compound stmt. C90 does not have this clause. We only do this
1327 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001328 //
1329 // C++ 6.4p1:
1330 // The substatement in a selection-statement (each substatement, in the else
1331 // form of the if statement) implicitly defines a local scope.
1332 //
1333 // See comments in ParseIfStatement for why we create a scope for the
1334 // condition and a new scope for substatement in C++.
1335 //
Serge Pavlov09f99242014-01-23 15:05:00 +00001336 getCurScope()->AddFlags(Scope::BreakScope);
David Majnemer2206bf52014-03-05 08:57:59 +00001337 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +00001338
Hans Wennborg852c3462014-06-17 00:09:05 +00001339 // We have incremented the mangling number for the SwitchScope and the
1340 // InnerScope, which is one too many.
1341 if (C99orCXX)
David Majnemera7f8c462015-03-19 21:54:30 +00001342 getCurScope()->decrementMSManglingNumber();
Hans Wennborg852c3462014-06-17 00:09:05 +00001343
Chris Lattner9075bd72006-08-10 04:59:57 +00001344 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001345 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001346
Chris Lattner8fd2d012010-01-24 01:50:29 +00001347 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001348 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001349 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001350
John McCallb268a282010-08-23 23:25:46 +00001351 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001352}
1353
1354/// ParseWhileStatement
1355/// while-statement: [C99 6.8.5.1]
1356/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001357/// [C++] 'while' '(' condition ')' statement
Richard Smithc202b282012-04-14 00:33:13 +00001358StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001359 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +00001360 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +00001361 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001362
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001363 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001364 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +00001365 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001366 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001367 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001368
David Blaikiebbafb8a2012-03-11 07:00:24 +00001369 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001370
Chris Lattner2dd1b722007-08-26 23:08:06 +00001371 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1372 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001373 //
1374 // C++ 6.4p3:
1375 // A name introduced by a declaration in a condition is in scope from its
1376 // point of declaration until the end of the substatements controlled by the
1377 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001378 // C++ 3.3.2p4:
1379 // Names declared in the for-init-statement, and in the condition of if,
1380 // while, for, and switch statements are local to the if, while, for, or
1381 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001382 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001383 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001384 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001385 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1386 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001387 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001388 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1389 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001390
Chris Lattner9075bd72006-08-10 04:59:57 +00001391 // Parse the condition.
Richard Smith03a4aa32016-06-23 19:02:52 +00001392 Sema::ConditionResult Cond;
Richard Smithc7a05a92016-06-29 21:17:59 +00001393 if (ParseParenExprOrCondition(nullptr, Cond, WhileLoc,
1394 Sema::ConditionKind::Boolean))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001395 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001396
Justin Bognere4ebb6c2013-12-03 07:36:55 +00001397 // C99 6.8.5p5 - In C99, the body of the while statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001398 // there is no compound stmt. C90 does not have this clause. We only do this
1399 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001400 //
1401 // C++ 6.5p2:
1402 // The substatement in an iteration-statement implicitly defines a local scope
1403 // which is entered and exited each time through the loop.
1404 //
1405 // See comments in ParseIfStatement for why we create a scope for the
1406 // condition and a new scope for substatement in C++.
1407 //
David Majnemer2206bf52014-03-05 08:57:59 +00001408 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001409
Chris Lattner9075bd72006-08-10 04:59:57 +00001410 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001411 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001412
Chris Lattner8fb26252007-08-22 05:28:50 +00001413 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001414 InnerScope.Exit();
1415 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001416
Richard Smith03a4aa32016-06-23 19:02:52 +00001417 if (Cond.isInvalid() || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001418 return StmtError();
1419
Richard Smith03a4aa32016-06-23 19:02:52 +00001420 return Actions.ActOnWhileStmt(WhileLoc, Cond, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001421}
1422
1423/// ParseDoStatement
1424/// do-statement: [C99 6.8.5.2]
1425/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +00001426/// Note: this lets the caller parse the end ';'.
Richard Smithc202b282012-04-14 00:33:13 +00001427StmtResult Parser::ParseDoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001428 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001429 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001430
Chris Lattner2dd1b722007-08-26 23:08:06 +00001431 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1432 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001433 unsigned ScopeFlags;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001434 if (getLangOpts().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001435 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001436 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001437 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001438
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001439 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001440
Justin Bognere4ebb6c2013-12-03 07:36:55 +00001441 // C99 6.8.5p5 - In C99, the body of the do statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001442 // there is no compound stmt. C90 does not have this clause. We only do this
1443 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +00001444 //
1445 // C++ 6.5p2:
1446 // The substatement in an iteration-statement implicitly defines a local scope
1447 // which is entered and exited each time through the loop.
1448 //
David Majnemer2206bf52014-03-05 08:57:59 +00001449 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
1450 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001451
Chris Lattner9075bd72006-08-10 04:59:57 +00001452 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001453 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +00001454
Chris Lattner8fb26252007-08-22 05:28:50 +00001455 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001456 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001457
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001458 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001459 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +00001460 Diag(Tok, diag::err_expected_while);
Alp Tokerec543272013-12-24 09:48:30 +00001461 Diag(DoLoc, diag::note_matching) << "'do'";
Alexey Bataevee6507d2013-11-18 08:17:37 +00001462 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner0046de12008-11-13 18:52:53 +00001463 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001464 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001465 }
Chris Lattneraf635312006-10-16 06:06:51 +00001466 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001467
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001468 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001469 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Alexey Bataevee6507d2013-11-18 08:17:37 +00001470 SkipUntil(tok::semi, StopBeforeMatch);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001471 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001472 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001473
Richard Smithc2c8bb82013-10-15 01:34:54 +00001474 // Parse the parenthesized expression.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001475 BalancedDelimiterTracker T(*this, tok::l_paren);
1476 T.consumeOpen();
Chad Rosier67055f52012-07-10 21:35:27 +00001477
Richard Smithc2c8bb82013-10-15 01:34:54 +00001478 // A do-while expression is not a condition, so can't have attributes.
1479 DiagnoseAndSkipCXX11Attributes();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001480
John McCalldadc5752010-08-24 06:29:42 +00001481 ExprResult Cond = ParseExpression();
Alex Lorenzc38ba662017-10-30 22:55:11 +00001482 // Correct the typos in condition before closing the scope.
1483 if (Cond.isUsable())
1484 Cond = Actions.CorrectDelayedTyposInExpr(Cond);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001485 T.consumeClose();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001486 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001487
Sebastian Redlb62406f2008-12-11 19:48:14 +00001488 if (Cond.isInvalid() || Body.isInvalid())
1489 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001490
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001491 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(),
1492 Cond.get(), T.getCloseLocation());
Chris Lattner9075bd72006-08-10 04:59:57 +00001493}
1494
Richard Smith955bf012014-06-19 11:42:00 +00001495bool Parser::isForRangeIdentifier() {
1496 assert(Tok.is(tok::identifier));
1497
1498 const Token &Next = NextToken();
1499 if (Next.is(tok::colon))
1500 return true;
1501
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001502 if (Next.isOneOf(tok::l_square, tok::kw_alignas)) {
Richard Smith955bf012014-06-19 11:42:00 +00001503 TentativeParsingAction PA(*this);
1504 ConsumeToken();
1505 SkipCXX11Attributes();
1506 bool Result = Tok.is(tok::colon);
1507 PA.Revert();
1508 return Result;
1509 }
1510
1511 return false;
1512}
1513
Chris Lattner9075bd72006-08-10 04:59:57 +00001514/// ParseForStatement
1515/// for-statement: [C99 6.8.5.3]
1516/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1517/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001518/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1519/// [C++] statement
Richard Smith0e304ea2015-10-22 04:46:14 +00001520/// [C++0x] 'for'
1521/// 'co_await'[opt] [Coroutines]
1522/// '(' for-range-declaration ':' for-range-initializer ')'
1523/// statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001524/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1525/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001526///
1527/// [C++] for-init-statement:
1528/// [C++] expression-statement
1529/// [C++] simple-declaration
1530///
Richard Smith02e85f32011-04-14 22:09:26 +00001531/// [C++0x] for-range-declaration:
1532/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1533/// [C++0x] for-range-initializer:
1534/// [C++0x] expression
1535/// [C++0x] braced-init-list [TODO]
Richard Smithc202b282012-04-14 00:33:13 +00001536StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001537 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001538 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001539
Richard Smith0e304ea2015-10-22 04:46:14 +00001540 SourceLocation CoawaitLoc;
1541 if (Tok.is(tok::kw_co_await))
1542 CoawaitLoc = ConsumeToken();
1543
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001544 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001545 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +00001546 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001547 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001548 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001549
Chad Rosier67055f52012-07-10 21:35:27 +00001550 bool C99orCXXorObjC = getLangOpts().C99 || getLangOpts().CPlusPlus ||
1551 getLangOpts().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001552
Chris Lattner2dd1b722007-08-26 23:08:06 +00001553 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1554 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001555 //
1556 // C++ 6.4p3:
1557 // A name introduced by a declaration in a condition is in scope from its
1558 // point of declaration until the end of the substatements controlled by the
1559 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001560 // C++ 3.3.2p4:
1561 // Names declared in the for-init-statement, and in the condition of if,
1562 // while, for, and switch statements are local to the if, while, for, or
1563 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001564 // C++ 6.5.3p1:
1565 // Names declared in the for-init-statement are in the same declarative-region
1566 // as those declared in the condition.
1567 //
Serge Pavlov09f99242014-01-23 15:05:00 +00001568 unsigned ScopeFlags = 0;
Chris Lattner934074c2009-04-22 00:54:41 +00001569 if (C99orCXXorObjC)
Serge Pavlov09f99242014-01-23 15:05:00 +00001570 ScopeFlags = Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001571
1572 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +00001573
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001574 BalancedDelimiterTracker T(*this, tok::l_paren);
1575 T.consumeOpen();
1576
John McCalldadc5752010-08-24 06:29:42 +00001577 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001578
Richard Smith02e85f32011-04-14 22:09:26 +00001579 bool ForEach = false, ForRange = false;
John McCalldadc5752010-08-24 06:29:42 +00001580 StmtResult FirstPart;
Richard Smith03a4aa32016-06-23 19:02:52 +00001581 Sema::ConditionResult SecondPart;
John McCalldadc5752010-08-24 06:29:42 +00001582 ExprResult Collection;
Richard Smith02e85f32011-04-14 22:09:26 +00001583 ForRangeInit ForRangeInit;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001584 FullExprArg ThirdPart(Actions);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001585
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001586 if (Tok.is(tok::code_completion)) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001587 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001588 C99orCXXorObjC? Sema::PCC_ForInit
1589 : Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001590 cutOffParsing();
1591 return StmtError();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001592 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001593
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001594 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001595 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001596
Chris Lattner9075bd72006-08-10 04:59:57 +00001597 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001598 if (Tok.is(tok::semi)) { // for (;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001599 ProhibitAttributes(attrs);
Chris Lattner53361ac2006-08-10 05:19:57 +00001600 // no first part, eat the ';'.
1601 ConsumeToken();
Richard Smith955bf012014-06-19 11:42:00 +00001602 } else if (getLangOpts().CPlusPlus && Tok.is(tok::identifier) &&
1603 isForRangeIdentifier()) {
1604 ProhibitAttributes(attrs);
1605 IdentifierInfo *Name = Tok.getIdentifierInfo();
1606 SourceLocation Loc = ConsumeToken();
1607 MaybeParseCXX11Attributes(attrs);
1608
1609 ForRangeInit.ColonLoc = ConsumeToken();
1610 if (Tok.is(tok::l_brace))
1611 ForRangeInit.RangeExpr = ParseBraceInitializer();
1612 else
1613 ForRangeInit.RangeExpr = ParseExpression();
1614
Richard Smith83d3f152014-11-27 01:54:27 +00001615 Diag(Loc, diag::err_for_range_identifier)
Richard Smith955bf012014-06-19 11:42:00 +00001616 << ((getLangOpts().CPlusPlus11 && !getLangOpts().CPlusPlus1z)
1617 ? FixItHint::CreateInsertion(Loc, "auto &&")
1618 : FixItHint());
1619
1620 FirstPart = Actions.ActOnCXXForRangeIdentifier(getCurScope(), Loc, Name,
1621 attrs, attrs.Range.getEnd());
1622 ForRange = true;
Eli Friedman0ffc31c2011-12-20 01:50:37 +00001623 } else if (isForInitDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001624 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001625 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001626 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001627
Richard Smith02e85f32011-04-14 22:09:26 +00001628 // In C++0x, "for (T NS:a" might not be a typo for ::
David Blaikiebbafb8a2012-03-11 07:00:24 +00001629 bool MightBeForRangeStmt = getLangOpts().CPlusPlus;
Richard Smith02e85f32011-04-14 22:09:26 +00001630 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1631
Chris Lattner49836b42009-04-02 04:16:50 +00001632 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00001633 DeclGroupPtrTy DG = ParseSimpleDeclaration(
Rafael Espindola1bd906d2014-10-22 14:27:08 +00001634 Declarator::ForContext, DeclEnd, attrs, false,
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00001635 MightBeForRangeStmt ? &ForRangeInit : nullptr);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001636 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Richard Smith02e85f32011-04-14 22:09:26 +00001637 if (ForRangeInit.ParsedForRangeDecl()) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001638 Diag(ForRangeInit.ColonLoc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00001639 diag::warn_cxx98_compat_for_range : diag::ext_for_range);
Richard Smith58c74332011-09-04 19:54:14 +00001640
Richard Smith02e85f32011-04-14 22:09:26 +00001641 ForRange = true;
1642 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattner32dc41c2009-03-29 17:27:48 +00001643 ConsumeToken();
1644 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001645 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001646 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001647 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001648
Douglas Gregor68762e72010-08-23 21:17:50 +00001649 if (Tok.is(tok::code_completion)) {
1650 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001651 cutOffParsing();
1652 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001653 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001654 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001655 } else {
1656 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001657 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001658 } else {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001659 ProhibitAttributes(attrs);
Kaelyn Takatab16e6322014-11-20 22:06:40 +00001660 Value = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Chris Lattner71e23ce2006-11-04 20:18:38 +00001661
John McCall34376a62010-12-04 03:47:34 +00001662 ForEach = isTokIdentifier_in();
1663
Chris Lattnercd68f642007-06-27 01:06:29 +00001664 // Turn the expression into a stmt.
John McCall34376a62010-12-04 03:47:34 +00001665 if (!Value.isInvalid()) {
1666 if (ForEach)
1667 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1668 else
Richard Smith945f8d32013-01-14 22:39:08 +00001669 FirstPart = Actions.ActOnExprStmt(Value);
John McCall34376a62010-12-04 03:47:34 +00001670 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001671
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001672 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001673 ConsumeToken();
John McCall34376a62010-12-04 03:47:34 +00001674 } else if (ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001675 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001676
Douglas Gregor68762e72010-08-23 21:17:50 +00001677 if (Tok.is(tok::code_completion)) {
David Blaikie0403cb12016-01-15 23:43:25 +00001678 Actions.CodeCompleteObjCForCollection(getCurScope(), nullptr);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001679 cutOffParsing();
1680 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001681 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001682 Collection = ParseExpression();
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001683 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::colon) && FirstPart.get()) {
Richard Smith4f848f12011-12-20 22:56:20 +00001684 // User tried to write the reasonable, but ill-formed, for-range-statement
1685 // for (expr : expr) { ... }
1686 Diag(Tok, diag::err_for_range_expected_decl)
1687 << FirstPart.get()->getSourceRange();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001688 SkipUntil(tok::r_paren, StopBeforeMatch);
Richard Smith03a4aa32016-06-23 19:02:52 +00001689 SecondPart = Sema::ConditionError();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001690 } else {
Douglas Gregor230a7e62011-02-17 03:38:46 +00001691 if (!Value.isInvalid()) {
1692 Diag(Tok, diag::err_expected_semi_for);
1693 } else {
1694 // Skip until semicolon or rparen, don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001695 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor230a7e62011-02-17 03:38:46 +00001696 if (Tok.is(tok::semi))
1697 ConsumeToken();
1698 }
Chris Lattner53361ac2006-08-10 05:19:57 +00001699 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001700 }
Serge Pavlov09f99242014-01-23 15:05:00 +00001701
1702 // Parse the second part of the for specifier.
1703 getCurScope()->AddFlags(Scope::BreakScope | Scope::ContinueScope);
Richard Smith03a4aa32016-06-23 19:02:52 +00001704 if (!ForEach && !ForRange && !SecondPart.isInvalid()) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001705 // Parse the second part of the for specifier.
1706 if (Tok.is(tok::semi)) { // for (...;;
1707 // no second part.
Douglas Gregor230a7e62011-02-17 03:38:46 +00001708 } else if (Tok.is(tok::r_paren)) {
1709 // missing both semicolons.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001710 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001711 if (getLangOpts().CPlusPlus)
Richard Smithc7a05a92016-06-29 21:17:59 +00001712 SecondPart =
1713 ParseCXXCondition(nullptr, ForLoc, Sema::ConditionKind::Boolean);
Douglas Gregore60e41a2010-05-06 17:25:47 +00001714 else {
Richard Smith03a4aa32016-06-23 19:02:52 +00001715 ExprResult SecondExpr = ParseExpression();
1716 if (SecondExpr.isInvalid())
1717 SecondPart = Sema::ConditionError();
1718 else
1719 SecondPart =
1720 Actions.ActOnCondition(getCurScope(), ForLoc, SecondExpr.get(),
1721 Sema::ConditionKind::Boolean);
Douglas Gregore60e41a2010-05-06 17:25:47 +00001722 }
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001723 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001724
Douglas Gregor230a7e62011-02-17 03:38:46 +00001725 if (Tok.isNot(tok::semi)) {
Richard Smith03a4aa32016-06-23 19:02:52 +00001726 if (!SecondPart.isInvalid())
Douglas Gregor230a7e62011-02-17 03:38:46 +00001727 Diag(Tok, diag::err_expected_semi_for);
1728 else
1729 // Skip until semicolon or rparen, don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001730 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor230a7e62011-02-17 03:38:46 +00001731 }
1732
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001733 if (Tok.is(tok::semi)) {
1734 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001735 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001736
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001737 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001738 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001739 ExprResult Third = ParseExpression();
Richard Smith945f8d32013-01-14 22:39:08 +00001740 // FIXME: The C++11 standard doesn't actually say that this is a
1741 // discarded-value expression, but it clearly should be.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001742 ThirdPart = Actions.MakeFullDiscardedValueExpr(Third.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001743 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001744 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001745 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001746 T.consumeClose();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001747
Richard Smith0e304ea2015-10-22 04:46:14 +00001748 // C++ Coroutines [stmt.iter]:
1749 // 'co_await' can only be used for a range-based for statement.
1750 if (CoawaitLoc.isValid() && !ForRange) {
1751 Diag(CoawaitLoc, diag::err_for_co_await_not_range_for);
1752 CoawaitLoc = SourceLocation();
1753 }
1754
Richard Smith02e85f32011-04-14 22:09:26 +00001755 // We need to perform most of the semantic analysis for a C++0x for-range
1756 // statememt before parsing the body, in order to be able to deduce the type
1757 // of an auto-typed loop variable.
1758 StmtResult ForRangeStmt;
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001759 StmtResult ForEachStmt;
Chad Rosier67055f52012-07-10 21:35:27 +00001760
John McCall53848232011-07-27 01:07:15 +00001761 if (ForRange) {
Denis Zobnin7d6b9242016-02-02 17:33:09 +00001762 ExprResult CorrectedRange =
1763 Actions.CorrectDelayedTyposInExpr(ForRangeInit.RangeExpr.get());
Richard Smith9f690bd2015-10-27 06:02:45 +00001764 ForRangeStmt = Actions.ActOnCXXForRangeStmt(
1765 getCurScope(), ForLoc, CoawaitLoc, FirstPart.get(),
Denis Zobnin7d6b9242016-02-02 17:33:09 +00001766 ForRangeInit.ColonLoc, CorrectedRange.get(),
Richard Smith9f690bd2015-10-27 06:02:45 +00001767 T.getCloseLocation(), Sema::BFRK_Build);
John McCall53848232011-07-27 01:07:15 +00001768
1769 // Similarly, we need to do the semantic analysis for a for-range
1770 // statement immediately in order to close over temporaries correctly.
1771 } else if (ForEach) {
Sam Panzer2c4ca0f2012-08-16 21:47:25 +00001772 ForEachStmt = Actions.ActOnObjCForCollectionStmt(ForLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001773 FirstPart.get(),
1774 Collection.get(),
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001775 T.getCloseLocation());
Alexey Bataev9c821032015-04-30 04:23:23 +00001776 } else {
1777 // In OpenMP loop region loop control variable must be captured and be
1778 // private. Perform analysis of first part (if any).
1779 if (getLangOpts().OpenMP && FirstPart.isUsable()) {
1780 Actions.ActOnOpenMPLoopInitialization(ForLoc, FirstPart.get());
1781 }
John McCall53848232011-07-27 01:07:15 +00001782 }
1783
Justin Bognere4ebb6c2013-12-03 07:36:55 +00001784 // C99 6.8.5p5 - In C99, the body of the for statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001785 // there is no compound stmt. C90 does not have this clause. We only do this
1786 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001787 //
1788 // C++ 6.5p2:
1789 // The substatement in an iteration-statement implicitly defines a local scope
1790 // which is entered and exited each time through the loop.
1791 //
1792 // See comments in ParseIfStatement for why we create a scope for
1793 // for-init-statement/condition and a new scope for substatement in C++.
1794 //
David Majnemer2206bf52014-03-05 08:57:59 +00001795 ParseScope InnerScope(this, Scope::DeclScope, C99orCXXorObjC,
1796 Tok.is(tok::l_brace));
1797
1798 // The body of the for loop has the same local mangling number as the
1799 // for-init-statement.
1800 // It will only be incremented if the body contains other things that would
1801 // normally increment the mangling number (like a compound statement).
1802 if (C99orCXXorObjC)
David Majnemera7f8c462015-03-19 21:54:30 +00001803 getCurScope()->decrementMSManglingNumber();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001804
Chris Lattner9075bd72006-08-10 04:59:57 +00001805 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001806 StmtResult Body(ParseStatement(TrailingElseLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001807
Chris Lattner8fb26252007-08-22 05:28:50 +00001808 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001809 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001810
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001811 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001812 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001813
1814 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001815 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001816
Richard Smith02e85f32011-04-14 22:09:26 +00001817 if (ForEach)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001818 return Actions.FinishObjCForCollectionStmt(ForEachStmt.get(),
1819 Body.get());
Mike Stump11289f42009-09-09 15:08:12 +00001820
Richard Smith02e85f32011-04-14 22:09:26 +00001821 if (ForRange)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001822 return Actions.FinishCXXForRangeStmt(ForRangeStmt.get(), Body.get());
Richard Smith02e85f32011-04-14 22:09:26 +00001823
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001824 return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.get(),
Richard Smith03a4aa32016-06-23 19:02:52 +00001825 SecondPart, ThirdPart, T.getCloseLocation(),
1826 Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001827}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001828
Chris Lattner503fadc2006-08-10 05:45:44 +00001829/// ParseGotoStatement
1830/// jump-statement:
1831/// 'goto' identifier ';'
1832/// [GNU] 'goto' '*' expression ';'
1833///
1834/// Note: this lets the caller parse the end ';'.
1835///
Richard Smithc202b282012-04-14 00:33:13 +00001836StmtResult Parser::ParseGotoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001837 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001838 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001839
John McCalldadc5752010-08-24 06:29:42 +00001840 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001841 if (Tok.is(tok::identifier)) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +00001842 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1843 Tok.getLocation());
1844 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Chris Lattner503fadc2006-08-10 05:45:44 +00001845 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001846 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001847 // GNU indirect goto extension.
1848 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001849 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001850 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001851 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001852 SkipUntil(tok::semi, StopBeforeMatch);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001853 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001854 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001855 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.get());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001856 } else {
Alp Tokerec543272013-12-24 09:48:30 +00001857 Diag(Tok, diag::err_expected) << tok::identifier;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001858 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001859 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001860
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001861 return Res;
Chris Lattner503fadc2006-08-10 05:45:44 +00001862}
1863
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001864/// ParseContinueStatement
1865/// jump-statement:
1866/// 'continue' ';'
1867///
1868/// Note: this lets the caller parse the end ';'.
1869///
Richard Smithc202b282012-04-14 00:33:13 +00001870StmtResult Parser::ParseContinueStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001871 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001872 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001873}
1874
1875/// ParseBreakStatement
1876/// jump-statement:
1877/// 'break' ';'
1878///
1879/// Note: this lets the caller parse the end ';'.
1880///
Richard Smithc202b282012-04-14 00:33:13 +00001881StmtResult Parser::ParseBreakStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001882 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001883 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001884}
1885
Chris Lattner503fadc2006-08-10 05:45:44 +00001886/// ParseReturnStatement
1887/// jump-statement:
1888/// 'return' expression[opt] ';'
Richard Smith0e304ea2015-10-22 04:46:14 +00001889/// 'return' braced-init-list ';'
1890/// 'co_return' expression[opt] ';'
1891/// 'co_return' braced-init-list ';'
Richard Smithc202b282012-04-14 00:33:13 +00001892StmtResult Parser::ParseReturnStatement() {
Richard Smith0e304ea2015-10-22 04:46:14 +00001893 assert((Tok.is(tok::kw_return) || Tok.is(tok::kw_co_return)) &&
1894 "Not a return stmt!");
1895 bool IsCoreturn = Tok.is(tok::kw_co_return);
Chris Lattneraf635312006-10-16 06:06:51 +00001896 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001897
John McCalldadc5752010-08-24 06:29:42 +00001898 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001899 if (Tok.isNot(tok::semi)) {
Richard Smith0e304ea2015-10-22 04:46:14 +00001900 // FIXME: Code completion for co_return.
1901 if (Tok.is(tok::code_completion) && !IsCoreturn) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001902 Actions.CodeCompleteReturn(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001903 cutOffParsing();
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001904 return StmtError();
1905 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001906
David Blaikiebbafb8a2012-03-11 07:00:24 +00001907 if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus) {
Douglas Gregore9e27d92011-03-11 23:10:44 +00001908 R = ParseInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00001909 if (R.isUsable())
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001910 Diag(R.get()->getLocStart(), getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00001911 diag::warn_cxx98_compat_generalized_initializer_lists :
1912 diag::ext_generalized_initializer_lists)
Douglas Gregore9e27d92011-03-11 23:10:44 +00001913 << R.get()->getSourceRange();
1914 } else
Nico Weber3ce01c32015-01-04 08:07:54 +00001915 R = ParseExpression();
Serge Pavlovf79bd5c2013-12-04 03:51:59 +00001916 if (R.isInvalid()) {
1917 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001918 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001919 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001920 }
Richard Smithcfd53b42015-10-22 06:13:50 +00001921 if (IsCoreturn)
Eric Fiselier20f25cb2017-03-06 23:38:15 +00001922 return Actions.ActOnCoreturnStmt(getCurScope(), ReturnLoc, R.get());
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001923 return Actions.ActOnReturnStmt(ReturnLoc, R.get(), getCurScope());
Chris Lattner503fadc2006-08-10 05:45:44 +00001924}
Chris Lattner0116c472006-08-15 06:03:28 +00001925
Alexey Bataevc4fad652016-01-13 11:18:54 +00001926StmtResult Parser::ParsePragmaLoopHint(StmtVector &Stmts,
Jonathan Roelofsce1db6d2017-03-14 17:29:33 +00001927 AllowedConstructsKind Allowed,
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001928 SourceLocation *TrailingElseLoc,
1929 ParsedAttributesWithRange &Attrs) {
1930 // Create temporary attribute list.
1931 ParsedAttributesWithRange TempAttrs(AttrFactory);
1932
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001933 // Get loop hints and consume annotated token.
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001934 while (Tok.is(tok::annot_pragma_loop_hint)) {
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001935 LoopHint Hint;
1936 if (!HandlePragmaLoopHint(Hint))
1937 continue;
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001938
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001939 ArgsUnion ArgHints[] = {Hint.PragmaNameLoc, Hint.OptionLoc, Hint.StateLoc,
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001940 ArgsUnion(Hint.ValueExpr)};
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001941 TempAttrs.addNew(Hint.PragmaNameLoc->Ident, Hint.Range, nullptr,
1942 Hint.PragmaNameLoc->Loc, ArgHints, 4,
1943 AttributeList::AS_Pragma);
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001944 }
1945
1946 // Get the next statement.
1947 MaybeParseCXX11Attributes(Attrs);
1948
1949 StmtResult S = ParseStatementOrDeclarationAfterAttributes(
Alexey Bataevc4fad652016-01-13 11:18:54 +00001950 Stmts, Allowed, TrailingElseLoc, Attrs);
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001951
1952 Attrs.takeAllFrom(TempAttrs);
1953 return S;
1954}
1955
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001956Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001957 assert(Tok.is(tok::l_brace));
1958 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001959
John McCallfaf5fb42010-08-26 23:41:50 +00001960 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1961 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001962
Alexey Bataev3d42f342015-11-20 07:02:57 +00001963 // Save and reset current vtordisp stack if we have entered a C++ method body.
1964 bool IsCXXMethod =
1965 getLangOpts().CPlusPlus && Decl && isa<CXXMethodDecl>(Decl);
Denis Zobnin2290dac2016-04-29 11:27:00 +00001966 Sema::PragmaStackSentinelRAII
1967 PragmaStackSentinel(Actions, "InternalPragmaState", IsCXXMethod);
Alexey Bataev3d42f342015-11-20 07:02:57 +00001968
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001969 // Do not enter a scope for the brace, as the arguments are in the same scope
1970 // (the function body) as the body itself. Instead, just read the statement
1971 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00001972 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001973
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001974 // If the function body could not be parsed, make a bogus compoundstmt.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001975 if (FnBody.isInvalid()) {
1976 Sema::CompoundScopeRAII CompoundScope(Actions);
Robert Wilhelm27b2c9a32013-08-19 20:51:20 +00001977 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001978 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001979
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001980 BodyScope.Exit();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001981 return Actions.ActOnFinishFunctionBody(Decl, FnBody.get());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001982}
Sebastian Redlb219c902008-12-21 16:41:36 +00001983
Sebastian Redla7b98a72009-04-26 20:35:05 +00001984/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1985///
1986/// function-try-block:
1987/// 'try' ctor-initializer[opt] compound-statement handler-seq
1988///
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001989Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00001990 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1991 SourceLocation TryLoc = ConsumeToken();
1992
John McCallfaf5fb42010-08-26 23:41:50 +00001993 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1994 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00001995
1996 // Constructor initializer list?
1997 if (Tok.is(tok::colon))
1998 ParseConstructorInitializer(Decl);
Douglas Gregor5ca153f2011-09-07 20:36:12 +00001999 else
2000 Actions.ActOnDefaultCtorInitializers(Decl);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00002001
Alexey Bataev3d42f342015-11-20 07:02:57 +00002002 // Save and reset current vtordisp stack if we have entered a C++ method body.
2003 bool IsCXXMethod =
2004 getLangOpts().CPlusPlus && Decl && isa<CXXMethodDecl>(Decl);
Denis Zobnin2290dac2016-04-29 11:27:00 +00002005 Sema::PragmaStackSentinelRAII
2006 PragmaStackSentinel(Actions, "InternalPragmaState", IsCXXMethod);
Alexey Bataev3d42f342015-11-20 07:02:57 +00002007
Sebastian Redld98ecd62009-04-26 21:08:36 +00002008 SourceLocation LBraceLoc = Tok.getLocation();
David Blaikie1c9c9042012-11-10 01:04:23 +00002009 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc, /*FnTry*/true));
Sebastian Redla7b98a72009-04-26 20:35:05 +00002010 // If we failed to parse the try-catch, we just give the function an empty
2011 // compound statement as the body.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002012 if (FnBody.isInvalid()) {
2013 Sema::CompoundScopeRAII CompoundScope(Actions);
Robert Wilhelm27b2c9a32013-08-19 20:51:20 +00002014 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002015 }
Sebastian Redla7b98a72009-04-26 20:35:05 +00002016
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002017 BodyScope.Exit();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002018 return Actions.ActOnFinishFunctionBody(Decl, FnBody.get());
Sebastian Redla7b98a72009-04-26 20:35:05 +00002019}
2020
Erik Verbruggen6e922512012-04-12 10:11:59 +00002021bool Parser::trySkippingFunctionBody() {
Erik Verbruggen6e922512012-04-12 10:11:59 +00002022 assert(SkipFunctionBodies &&
2023 "Should only be called when SkipFunctionBodies is enabled");
Argyrios Kyrtzidis289e4a32012-10-31 17:29:28 +00002024 if (!PP.isCodeCompletionEnabled()) {
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002025 SkipFunctionBody();
Argyrios Kyrtzidis289e4a32012-10-31 17:29:28 +00002026 return true;
2027 }
2028
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002029 // We're in code-completion mode. Skip parsing for all function bodies unless
2030 // the body contains the code-completion point.
2031 TentativeParsingAction PA(*this);
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002032 bool IsTryCatch = Tok.is(tok::kw_try);
2033 CachedTokens Toks;
2034 bool ErrorInPrologue = ConsumeAndStoreFunctionPrologue(Toks);
2035 if (llvm::any_of(Toks, [](const Token &Tok) {
2036 return Tok.is(tok::code_completion);
2037 })) {
2038 PA.Revert();
2039 return false;
2040 }
2041 if (ErrorInPrologue) {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002042 PA.Commit();
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002043 SkipMalformedDecl();
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002044 return true;
2045 }
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002046 if (!SkipUntil(tok::r_brace, StopAtCodeCompletion)) {
2047 PA.Revert();
2048 return false;
2049 }
2050 while (IsTryCatch && Tok.is(tok::kw_catch)) {
2051 if (!SkipUntil(tok::l_brace, StopAtCodeCompletion) ||
2052 !SkipUntil(tok::r_brace, StopAtCodeCompletion)) {
2053 PA.Revert();
2054 return false;
2055 }
2056 }
2057 PA.Commit();
2058 return true;
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002059}
2060
Sebastian Redlb219c902008-12-21 16:41:36 +00002061/// ParseCXXTryBlock - Parse a C++ try-block.
2062///
2063/// try-block:
2064/// 'try' compound-statement handler-seq
2065///
Richard Smithc202b282012-04-14 00:33:13 +00002066StmtResult Parser::ParseCXXTryBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00002067 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2068
2069 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00002070 return ParseCXXTryBlockCommon(TryLoc);
2071}
2072
2073/// ParseCXXTryBlockCommon - Parse the common part of try-block and
2074/// function-try-block.
2075///
2076/// try-block:
2077/// 'try' compound-statement handler-seq
2078///
2079/// function-try-block:
2080/// 'try' ctor-initializer[opt] compound-statement handler-seq
2081///
2082/// handler-seq:
2083/// handler handler-seq[opt]
2084///
John Wiegley1c0675e2011-04-28 01:08:34 +00002085/// [Borland] try-block:
2086/// 'try' compound-statement seh-except-block
Alp Tokerf6a24ce2013-12-05 16:25:25 +00002087/// 'try' compound-statement seh-finally-block
John Wiegley1c0675e2011-04-28 01:08:34 +00002088///
David Blaikie1c9c9042012-11-10 01:04:23 +00002089StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc, bool FnTry) {
Sebastian Redlb219c902008-12-21 16:41:36 +00002090 if (Tok.isNot(tok::l_brace))
Alp Tokerec543272013-12-24 09:48:30 +00002091 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
Richard Smithc202b282012-04-14 00:33:13 +00002092
Momchil Velikov57c681f2017-08-10 15:43:06 +00002093 StmtResult TryBlock(ParseCompoundStatement(
2094 /*isStmtExpr=*/false, Scope::DeclScope | Scope::TryScope |
2095 Scope::CompoundStmtScope |
2096 (FnTry ? Scope::FnTryCatchScope : 0)));
Sebastian Redlb219c902008-12-21 16:41:36 +00002097 if (TryBlock.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002098 return TryBlock;
Sebastian Redlb219c902008-12-21 16:41:36 +00002099
John Wiegley1c0675e2011-04-28 01:08:34 +00002100 // Borland allows SEH-handlers with 'try'
Chad Rosier67055f52012-07-10 21:35:27 +00002101
Richard Smithc202b282012-04-14 00:33:13 +00002102 if ((Tok.is(tok::identifier) &&
2103 Tok.getIdentifierInfo() == getSEHExceptKeyword()) ||
2104 Tok.is(tok::kw___finally)) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002105 // TODO: Factor into common return ParseSEHHandlerCommon(...)
2106 StmtResult Handler;
Douglas Gregor60060d62011-10-21 03:57:52 +00002107 if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002108 SourceLocation Loc = ConsumeToken();
2109 Handler = ParseSEHExceptBlock(Loc);
2110 }
2111 else {
2112 SourceLocation Loc = ConsumeToken();
2113 Handler = ParseSEHFinallyBlock(Loc);
2114 }
2115 if(Handler.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002116 return Handler;
John McCall53fa7142010-12-24 02:08:15 +00002117
John Wiegley1c0675e2011-04-28 01:08:34 +00002118 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
2119 TryLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002120 TryBlock.get(),
Warren Huntf6be4cb2014-07-25 20:52:51 +00002121 Handler.get());
Sebastian Redlb219c902008-12-21 16:41:36 +00002122 }
John Wiegley1c0675e2011-04-28 01:08:34 +00002123 else {
Benjamin Kramerf0623432012-08-23 22:51:59 +00002124 StmtVector Handlers;
Richard Smithc2c8bb82013-10-15 01:34:54 +00002125
2126 // C++11 attributes can't appear here, despite this context seeming
2127 // statement-like.
2128 DiagnoseAndSkipCXX11Attributes();
Sebastian Redlb219c902008-12-21 16:41:36 +00002129
John Wiegley1c0675e2011-04-28 01:08:34 +00002130 if (Tok.isNot(tok::kw_catch))
2131 return StmtError(Diag(Tok, diag::err_expected_catch));
2132 while (Tok.is(tok::kw_catch)) {
David Blaikie1c9c9042012-11-10 01:04:23 +00002133 StmtResult Handler(ParseCXXCatchBlock(FnTry));
John Wiegley1c0675e2011-04-28 01:08:34 +00002134 if (!Handler.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002135 Handlers.push_back(Handler.get());
John Wiegley1c0675e2011-04-28 01:08:34 +00002136 }
2137 // Don't bother creating the full statement if we don't have any usable
2138 // handlers.
2139 if (Handlers.empty())
2140 return StmtError();
2141
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002142 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.get(), Handlers);
John Wiegley1c0675e2011-04-28 01:08:34 +00002143 }
Sebastian Redlb219c902008-12-21 16:41:36 +00002144}
2145
2146/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
2147///
Richard Smith1dba27c2013-01-29 09:02:09 +00002148/// handler:
2149/// 'catch' '(' exception-declaration ')' compound-statement
Sebastian Redlb219c902008-12-21 16:41:36 +00002150///
Richard Smith1dba27c2013-01-29 09:02:09 +00002151/// exception-declaration:
2152/// attribute-specifier-seq[opt] type-specifier-seq declarator
2153/// attribute-specifier-seq[opt] type-specifier-seq abstract-declarator[opt]
2154/// '...'
Sebastian Redlb219c902008-12-21 16:41:36 +00002155///
David Blaikie1c9c9042012-11-10 01:04:23 +00002156StmtResult Parser::ParseCXXCatchBlock(bool FnCatch) {
Sebastian Redlb219c902008-12-21 16:41:36 +00002157 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
2158
2159 SourceLocation CatchLoc = ConsumeToken();
2160
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002161 BalancedDelimiterTracker T(*this, tok::l_paren);
Alp Toker383d2c42014-01-01 03:08:43 +00002162 if (T.expectAndConsume())
Sebastian Redlb219c902008-12-21 16:41:36 +00002163 return StmtError();
2164
2165 // C++ 3.3.2p3:
2166 // The name in a catch exception-declaration is local to the handler and
2167 // shall not be redeclared in the outermost block of the handler.
David Blaikie1c9c9042012-11-10 01:04:23 +00002168 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope |
David Blaikie3403feb2012-11-13 18:51:45 +00002169 (FnCatch ? Scope::FnTryCatchScope : 0));
Sebastian Redlb219c902008-12-21 16:41:36 +00002170
2171 // exception-declaration is equivalent to '...' or a parameter-declaration
2172 // without default arguments.
Craig Topper161e4db2014-05-21 06:02:52 +00002173 Decl *ExceptionDecl = nullptr;
Sebastian Redlb219c902008-12-21 16:41:36 +00002174 if (Tok.isNot(tok::ellipsis)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002175 ParsedAttributesWithRange Attributes(AttrFactory);
2176 MaybeParseCXX11Attributes(Attributes);
2177
John McCall084e83d2011-03-24 11:26:52 +00002178 DeclSpec DS(AttrFactory);
Richard Smith1dba27c2013-01-29 09:02:09 +00002179 DS.takeAttributesFrom(Attributes);
2180
Sebastian Redl54c04d42008-12-22 19:15:10 +00002181 if (ParseCXXTypeSpecifierSeq(DS))
2182 return StmtError();
Richard Smith1dba27c2013-01-29 09:02:09 +00002183
Sebastian Redlb219c902008-12-21 16:41:36 +00002184 Declarator ExDecl(DS, Declarator::CXXCatchContext);
2185 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002186 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00002187 } else
2188 ConsumeToken();
2189
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002190 T.consumeClose();
2191 if (T.getCloseLocation().isInvalid())
Sebastian Redlb219c902008-12-21 16:41:36 +00002192 return StmtError();
2193
2194 if (Tok.isNot(tok::l_brace))
Alp Tokerec543272013-12-24 09:48:30 +00002195 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
Sebastian Redlb219c902008-12-21 16:41:36 +00002196
Alexis Hunt96d5c762009-11-21 08:43:09 +00002197 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
Richard Smithc202b282012-04-14 00:33:13 +00002198 StmtResult Block(ParseCompoundStatement());
Sebastian Redlb219c902008-12-21 16:41:36 +00002199 if (Block.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002200 return Block;
Sebastian Redlb219c902008-12-21 16:41:36 +00002201
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002202 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.get());
Sebastian Redlb219c902008-12-21 16:41:36 +00002203}
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002204
2205void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Douglas Gregor43edb322011-10-24 22:31:10 +00002206 IfExistsCondition Result;
Francois Picheta5b3fcb2011-05-07 17:30:27 +00002207 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002208 return;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00002209
Douglas Gregor43edb322011-10-24 22:31:10 +00002210 // Handle dependent statements by parsing the braces as a compound statement.
2211 // This is not the same behavior as Visual C++, which don't treat this as a
2212 // compound statement, but for Clang's type checking we can't have anything
2213 // inside these braces escaping to the surrounding code.
2214 if (Result.Behavior == IEB_Dependent) {
2215 if (!Tok.is(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002216 Diag(Tok, diag::err_expected) << tok::l_brace;
Richard Smithc202b282012-04-14 00:33:13 +00002217 return;
Douglas Gregor43edb322011-10-24 22:31:10 +00002218 }
Richard Smithc202b282012-04-14 00:33:13 +00002219
2220 StmtResult Compound = ParseCompoundStatement();
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002221 if (Compound.isInvalid())
2222 return;
Richard Smithc202b282012-04-14 00:33:13 +00002223
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002224 StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc,
2225 Result.IsIfExists,
Richard Smithc202b282012-04-14 00:33:13 +00002226 Result.SS,
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002227 Result.Name,
2228 Compound.get());
2229 if (DepResult.isUsable())
2230 Stmts.push_back(DepResult.get());
Douglas Gregor43edb322011-10-24 22:31:10 +00002231 return;
2232 }
Richard Smithc202b282012-04-14 00:33:13 +00002233
Douglas Gregor43edb322011-10-24 22:31:10 +00002234 BalancedDelimiterTracker Braces(*this, tok::l_brace);
2235 if (Braces.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +00002236 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002237 return;
2238 }
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002239
Douglas Gregor43edb322011-10-24 22:31:10 +00002240 switch (Result.Behavior) {
2241 case IEB_Parse:
2242 // Parse the statements below.
2243 break;
Chad Rosier67055f52012-07-10 21:35:27 +00002244
Douglas Gregor43edb322011-10-24 22:31:10 +00002245 case IEB_Dependent:
2246 llvm_unreachable("Dependent case handled above");
Chad Rosier67055f52012-07-10 21:35:27 +00002247
Douglas Gregor43edb322011-10-24 22:31:10 +00002248 case IEB_Skip:
2249 Braces.skipToEnd();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002250 return;
2251 }
2252
2253 // Condition is true, parse the statements.
2254 while (Tok.isNot(tok::r_brace)) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00002255 StmtResult R = ParseStatementOrDeclaration(Stmts, ACK_Any);
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002256 if (R.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002257 Stmts.push_back(R.get());
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002258 }
Douglas Gregor43edb322011-10-24 22:31:10 +00002259 Braces.consumeClose();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002260}
Anastasia Stulova6bdbcbb2016-02-19 18:30:11 +00002261
2262bool Parser::ParseOpenCLUnrollHintAttribute(ParsedAttributes &Attrs) {
2263 MaybeParseGNUAttributes(Attrs);
2264
2265 if (Attrs.empty())
2266 return true;
2267
2268 if (Attrs.getList()->getKind() != AttributeList::AT_OpenCLUnrollHint)
2269 return true;
2270
2271 if (!(Tok.is(tok::kw_for) || Tok.is(tok::kw_while) || Tok.is(tok::kw_do))) {
2272 Diag(Tok, diag::err_opencl_unroll_hint_on_non_loop);
2273 return false;
2274 }
2275 return true;
2276}