blob: 3fc29253f09b97d9f1409ebcf811f2630ed04b53 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner0ccd51e2006-08-09 05:47:47 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Statement and Block portions of the Parser
10// interface.
11//
12//===----------------------------------------------------------------------===//
13
Jordan Rose1e879d82018-03-23 00:07:18 +000014#include "clang/AST/PrettyDeclStackTrace.h"
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"
Richard Trieu0614cff2018-11-28 04:36:31 +000017#include "clang/Parse/LoopHint.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000018#include "clang/Parse/Parser.h"
Vassil Vassilev11ad3392017-03-23 15:11:07 +000019#include "clang/Parse/RAIIObjectsForParser.h"
John McCall8b0666c2010-08-20 18:27:03 +000020#include "clang/Sema/DeclSpec.h"
21#include "clang/Sema/Scope.h"
Richard Smith4f605af2012-08-18 00:55:03 +000022#include "clang/Sema/TypoCorrection.h"
Chris Lattner0ccd51e2006-08-09 05:47:47 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// C99 6.8: Statements and Blocks.
27//===----------------------------------------------------------------------===//
28
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000029/// Parse a standalone statement (for instance, as the body of an 'if',
Richard Smith426a47b2013-10-28 22:04:30 +000030/// 'while', or 'for').
Alexey Bataevc4fad652016-01-13 11:18:54 +000031StmtResult Parser::ParseStatement(SourceLocation *TrailingElseLoc,
32 bool AllowOpenMPStandalone) {
Richard Smith426a47b2013-10-28 22:04:30 +000033 StmtResult Res;
34
35 // We may get back a null statement if we found a #pragma. Keep going until
36 // we get an actual statement.
37 do {
38 StmtVector Stmts;
Alexey Bataevc4fad652016-01-13 11:18:54 +000039 Res = ParseStatementOrDeclaration(
40 Stmts, AllowOpenMPStandalone ? ACK_StatementsOpenMPAnyExecutable
41 : ACK_StatementsOpenMPNonStandalone,
42 TrailingElseLoc);
Richard Smith426a47b2013-10-28 22:04:30 +000043 } while (!Res.isInvalid() && !Res.get());
44
45 return Res;
46}
47
Chris Lattner0ccd51e2006-08-09 05:47:47 +000048/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
49/// StatementOrDeclaration:
50/// statement
51/// declaration
52///
53/// statement:
54/// labeled-statement
55/// compound-statement
56/// expression-statement
57/// selection-statement
58/// iteration-statement
59/// jump-statement
Argyrios Kyrtzidisdee82912008-09-07 18:58:01 +000060/// [C++] declaration-statement
Sebastian Redlb219c902008-12-21 16:41:36 +000061/// [C++] try-block
John Wiegley1c0675e2011-04-28 01:08:34 +000062/// [MS] seh-try-block
Fariborz Jahanian90814572007-10-04 20:19:06 +000063/// [OBC] objc-throw-statement
64/// [OBC] objc-try-catch-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +000065/// [OBC] objc-synchronized-statement
Chris Lattner0116c472006-08-15 06:03:28 +000066/// [GNU] asm-statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +000067/// [OMP] openmp-construct [TODO]
68///
69/// labeled-statement:
70/// identifier ':' statement
71/// 'case' constant-expression ':' statement
72/// 'default' ':' statement
73///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000074/// selection-statement:
75/// if-statement
76/// switch-statement
77///
78/// iteration-statement:
79/// while-statement
80/// do-statement
81/// for-statement
82///
Chris Lattner9075bd72006-08-10 04:59:57 +000083/// expression-statement:
84/// expression[opt] ';'
85///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000086/// jump-statement:
87/// 'goto' identifier ';'
88/// 'continue' ';'
89/// 'break' ';'
90/// 'return' expression[opt] ';'
Chris Lattner503fadc2006-08-10 05:45:44 +000091/// [GNU] 'goto' '*' expression ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000092///
Fariborz Jahanian90814572007-10-04 20:19:06 +000093/// [OBC] objc-throw-statement:
94/// [OBC] '@' 'throw' expression ';'
Mike Stump11289f42009-09-09 15:08:12 +000095/// [OBC] '@' 'throw' ';'
96///
John McCalldadc5752010-08-24 06:29:42 +000097StmtResult
Alexey Bataevc4fad652016-01-13 11:18:54 +000098Parser::ParseStatementOrDeclaration(StmtVector &Stmts,
Jonathan Roelofsce1db6d2017-03-14 17:29:33 +000099 AllowedConstructsKind Allowed,
Nico Weber3cef1082011-12-22 23:26:17 +0000100 SourceLocation *TrailingElseLoc) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000101
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000102 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000103
Richard Smithc202b282012-04-14 00:33:13 +0000104 ParsedAttributesWithRange Attrs(AttrFactory);
Craig Topper161e4db2014-05-21 06:02:52 +0000105 MaybeParseCXX11Attributes(Attrs, nullptr, /*MightBeObjCMessageSend*/ true);
Anastasia Stulova6bdbcbb2016-02-19 18:30:11 +0000106 if (!MaybeParseOpenCLUnrollHintAttribute(Attrs))
107 return StmtError();
Richard Smithc202b282012-04-14 00:33:13 +0000108
Alexey Bataevc4fad652016-01-13 11:18:54 +0000109 StmtResult Res = ParseStatementOrDeclarationAfterAttributes(
110 Stmts, Allowed, TrailingElseLoc, Attrs);
Richard Smithc202b282012-04-14 00:33:13 +0000111
112 assert((Attrs.empty() || Res.isInvalid() || Res.isUsable()) &&
113 "attributes on empty statement");
114
115 if (Attrs.empty() || Res.isInvalid())
116 return Res;
117
Erich Keanec480f302018-07-12 21:09:05 +0000118 return Actions.ProcessStmtAttributes(Res.get(), Attrs, Attrs.Range);
Richard Smithc202b282012-04-14 00:33:13 +0000119}
120
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000121namespace {
122class StatementFilterCCC : public CorrectionCandidateCallback {
123public:
124 StatementFilterCCC(Token nextTok) : NextToken(nextTok) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000125 WantTypeSpecifiers = nextTok.isOneOf(tok::l_paren, tok::less, tok::l_square,
126 tok::identifier, tok::star, tok::amp);
127 WantExpressionKeywords =
128 nextTok.isOneOf(tok::l_paren, tok::identifier, tok::arrow, tok::period);
129 WantRemainingKeywords =
130 nextTok.isOneOf(tok::l_paren, tok::semi, tok::identifier, tok::l_brace);
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000131 WantCXXNamedCasts = false;
132 }
133
Craig Topper2b07f022014-03-12 05:09:18 +0000134 bool ValidateCandidate(const TypoCorrection &candidate) override {
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000135 if (FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>())
Kaelyn Uhrain07e62722013-10-01 22:00:28 +0000136 return !candidate.getCorrectionSpecifier() || isa<ObjCIvarDecl>(FD);
Kaelyn Uhrain46b6cdc2013-09-27 19:40:16 +0000137 if (NextToken.is(tok::equal))
138 return candidate.getCorrectionDeclAs<VarDecl>();
Kaelyn Uhrain30943ce2013-09-27 23:54:23 +0000139 if (NextToken.is(tok::period) &&
140 candidate.getCorrectionDeclAs<NamespaceDecl>())
141 return false;
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000142 return CorrectionCandidateCallback::ValidateCandidate(candidate);
143 }
144
145private:
146 Token NextToken;
147};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000148}
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000149
Richard Smithc202b282012-04-14 00:33:13 +0000150StmtResult
151Parser::ParseStatementOrDeclarationAfterAttributes(StmtVector &Stmts,
Jonathan Roelofsce1db6d2017-03-14 17:29:33 +0000152 AllowedConstructsKind Allowed, SourceLocation *TrailingElseLoc,
Richard Smithc202b282012-04-14 00:33:13 +0000153 ParsedAttributesWithRange &Attrs) {
Craig Topper161e4db2014-05-21 06:02:52 +0000154 const char *SemiError = nullptr;
Richard Smithc202b282012-04-14 00:33:13 +0000155 StmtResult Res;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000156
Chris Lattner503fadc2006-08-10 05:45:44 +0000157 // Cases in this switch statement should fall through if the parser expects
158 // the token to end in a semicolon (in which case SemiError should be set),
159 // or they directly 'return;' if not.
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000160Retry:
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000161 tok::TokenKind Kind = Tok.getKind();
162 SourceLocation AtLoc;
163 switch (Kind) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000164 case tok::at: // May be a @try or @throw statement
165 {
Richard Smithc202b282012-04-14 00:33:13 +0000166 ProhibitAttributes(Attrs); // TODO: is it correct?
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000167 AtLoc = ConsumeToken(); // consume @
Sebastian Redlbab9a4b2008-12-11 20:12:42 +0000168 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000169 }
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000170
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000171 case tok::code_completion:
John McCallfaf5fb42010-08-26 23:41:50 +0000172 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000173 cutOffParsing();
174 return StmtError();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000175
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000176 case tok::identifier: {
177 Token Next = NextToken();
178 if (Next.is(tok::colon)) { // C99 6.8.1: labeled-statement
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000179 // identifier ':' statement
Richard Smithc202b282012-04-14 00:33:13 +0000180 return ParseLabeledStatement(Attrs);
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000181 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000182
Richard Smith4f605af2012-08-18 00:55:03 +0000183 // Look up the identifier, and typo-correct it to a keyword if it's not
184 // found.
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000185 if (Next.isNot(tok::coloncolon)) {
Richard Smith4f605af2012-08-18 00:55:03 +0000186 // Try to limit which sets of keywords should be included in typo
187 // correction based on what the next token is.
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000188 if (TryAnnotateName(/*IsAddressOfOperand*/ false,
189 llvm::make_unique<StatementFilterCCC>(Next)) ==
190 ANK_Error) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000191 // Handle errors here by skipping up to the next semicolon or '}', and
192 // eat the semicolon if that's what stopped us.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000193 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000194 if (Tok.is(tok::semi))
195 ConsumeToken();
196 return StmtError();
Richard Smith4f605af2012-08-18 00:55:03 +0000197 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000198
Richard Smith4f605af2012-08-18 00:55:03 +0000199 // If the identifier was typo-corrected, try again.
200 if (Tok.isNot(tok::identifier))
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000201 goto Retry;
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000202 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000203
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000204 // Fall through
Galina Kistanova387ab8b2017-06-01 21:28:26 +0000205 LLVM_FALLTHROUGH;
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000206 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000207
Chris Lattner803802d2009-03-24 17:04:48 +0000208 default: {
David Majnemer6ac7dd12016-08-01 16:39:29 +0000209 if ((getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt ||
210 Allowed == ACK_Any) &&
Alexey Bataevc4fad652016-01-13 11:18:54 +0000211 isDeclarationStatement()) {
Chris Lattner49836b42009-04-02 04:16:50 +0000212 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Faisal Vali421b2d12017-12-29 05:41:00 +0000213 DeclGroupPtrTy Decl = ParseDeclaration(DeclaratorContext::BlockContext,
Richard Smithc202b282012-04-14 00:33:13 +0000214 DeclEnd, Attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000215 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattner803802d2009-03-24 17:04:48 +0000216 }
217
218 if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +0000219 Diag(Tok, diag::err_expected_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000220 return StmtError();
Chris Lattnerf8afb622006-08-10 18:26:31 +0000221 }
Mike Stump11289f42009-09-09 15:08:12 +0000222
Richard Smithc202b282012-04-14 00:33:13 +0000223 return ParseExprStatement();
Chris Lattner803802d2009-03-24 17:04:48 +0000224 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000225
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000226 case tok::kw_case: // C99 6.8.1: labeled-statement
Richard Smithc202b282012-04-14 00:33:13 +0000227 return ParseCaseStatement();
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000228 case tok::kw_default: // C99 6.8.1: labeled-statement
Richard Smithc202b282012-04-14 00:33:13 +0000229 return ParseDefaultStatement();
Sebastian Redl042ad952008-12-11 19:30:53 +0000230
Chris Lattner9075bd72006-08-10 04:59:57 +0000231 case tok::l_brace: // C99 6.8.2: compound-statement
Richard Smithc202b282012-04-14 00:33:13 +0000232 return ParseCompoundStatement();
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000233 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
Argyrios Kyrtzidis43ea78b2011-09-01 21:53:45 +0000234 bool HasLeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
235 return Actions.ActOnNullStmt(ConsumeToken(), HasLeadingEmptyMacro);
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000236 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000237
Chris Lattner9075bd72006-08-10 04:59:57 +0000238 case tok::kw_if: // C99 6.8.4.1: if-statement
Richard Smithc202b282012-04-14 00:33:13 +0000239 return ParseIfStatement(TrailingElseLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000240 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Richard Smithc202b282012-04-14 00:33:13 +0000241 return ParseSwitchStatement(TrailingElseLoc);
Sebastian Redl042ad952008-12-11 19:30:53 +0000242
Chris Lattner9075bd72006-08-10 04:59:57 +0000243 case tok::kw_while: // C99 6.8.5.1: while-statement
Richard Smithc202b282012-04-14 00:33:13 +0000244 return ParseWhileStatement(TrailingElseLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000245 case tok::kw_do: // C99 6.8.5.2: do-statement
Richard Smithc202b282012-04-14 00:33:13 +0000246 Res = ParseDoStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000247 SemiError = "do/while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000248 break;
249 case tok::kw_for: // C99 6.8.5.3: for-statement
Richard Smithc202b282012-04-14 00:33:13 +0000250 return ParseForStatement(TrailingElseLoc);
Chris Lattner503fadc2006-08-10 05:45:44 +0000251
252 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Richard Smithc202b282012-04-14 00:33:13 +0000253 Res = ParseGotoStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000254 SemiError = "goto";
Chris Lattner9075bd72006-08-10 04:59:57 +0000255 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000256 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Richard Smithc202b282012-04-14 00:33:13 +0000257 Res = ParseContinueStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000258 SemiError = "continue";
Chris Lattner503fadc2006-08-10 05:45:44 +0000259 break;
260 case tok::kw_break: // C99 6.8.6.3: break-statement
Richard Smithc202b282012-04-14 00:33:13 +0000261 Res = ParseBreakStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000262 SemiError = "break";
Chris Lattner503fadc2006-08-10 05:45:44 +0000263 break;
264 case tok::kw_return: // C99 6.8.6.4: return-statement
Richard Smithc202b282012-04-14 00:33:13 +0000265 Res = ParseReturnStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000266 SemiError = "return";
Chris Lattner503fadc2006-08-10 05:45:44 +0000267 break;
Richard Smith0e304ea2015-10-22 04:46:14 +0000268 case tok::kw_co_return: // C++ Coroutines: co_return statement
269 Res = ParseReturnStatement();
270 SemiError = "co_return";
271 break;
Sebastian Redl042ad952008-12-11 19:30:53 +0000272
Sebastian Redlb219c902008-12-21 16:41:36 +0000273 case tok::kw_asm: {
Richard Smithc202b282012-04-14 00:33:13 +0000274 ProhibitAttributes(Attrs);
Steve Naroffb2c80c72008-02-07 03:50:06 +0000275 bool msAsm = false;
276 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidis3050d9b2010-11-02 02:33:08 +0000277 Res = Actions.ActOnFinishFullStmt(Res.get());
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000278 if (msAsm) return Res;
Chris Lattner34a95662009-06-14 00:07:48 +0000279 SemiError = "asm";
Chris Lattner0116c472006-08-15 06:03:28 +0000280 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000281 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000282
Reid Kleckner6d8d22a2014-06-25 00:28:35 +0000283 case tok::kw___if_exists:
284 case tok::kw___if_not_exists:
285 ProhibitAttributes(Attrs);
286 ParseMicrosoftIfExistsStatement(Stmts);
287 // An __if_exists block is like a compound statement, but it doesn't create
288 // a new scope.
289 return StmtEmpty();
290
Sebastian Redlb219c902008-12-21 16:41:36 +0000291 case tok::kw_try: // C++ 15: try-block
Richard Smithc202b282012-04-14 00:33:13 +0000292 return ParseCXXTryBlock();
John Wiegley1c0675e2011-04-28 01:08:34 +0000293
294 case tok::kw___try:
Richard Smithc202b282012-04-14 00:33:13 +0000295 ProhibitAttributes(Attrs); // TODO: is it correct?
296 return ParseSEHTryBlock();
Eli Friedmanec52f922012-02-23 23:47:16 +0000297
Nico Weberc7d05962014-07-06 22:32:59 +0000298 case tok::kw___leave:
299 Res = ParseSEHLeaveStatement();
300 SemiError = "__leave";
301 break;
302
Eli Friedmanec52f922012-02-23 23:47:16 +0000303 case tok::annot_pragma_vis:
Richard Smithc202b282012-04-14 00:33:13 +0000304 ProhibitAttributes(Attrs);
Eli Friedmanec52f922012-02-23 23:47:16 +0000305 HandlePragmaVisibility();
306 return StmtEmpty();
307
308 case tok::annot_pragma_pack:
Richard Smithc202b282012-04-14 00:33:13 +0000309 ProhibitAttributes(Attrs);
Eli Friedmanec52f922012-02-23 23:47:16 +0000310 HandlePragmaPack();
311 return StmtEmpty();
Eli Friedman68be1642012-10-04 02:36:51 +0000312
Eli Friedmanbbbbac62012-10-09 22:46:54 +0000313 case tok::annot_pragma_msstruct:
314 ProhibitAttributes(Attrs);
315 HandlePragmaMSStruct();
316 return StmtEmpty();
317
Eli Friedmanae8ee252012-10-08 23:52:38 +0000318 case tok::annot_pragma_align:
319 ProhibitAttributes(Attrs);
320 HandlePragmaAlign();
321 return StmtEmpty();
322
Eli Friedmanbbbbac62012-10-09 22:46:54 +0000323 case tok::annot_pragma_weak:
324 ProhibitAttributes(Attrs);
325 HandlePragmaWeak();
326 return StmtEmpty();
327
328 case tok::annot_pragma_weakalias:
329 ProhibitAttributes(Attrs);
330 HandlePragmaWeakAlias();
331 return StmtEmpty();
332
333 case tok::annot_pragma_redefine_extname:
334 ProhibitAttributes(Attrs);
335 HandlePragmaRedefineExtname();
336 return StmtEmpty();
337
Eli Friedman68be1642012-10-04 02:36:51 +0000338 case tok::annot_pragma_fp_contract:
Richard Smithca9b0b62013-11-15 21:10:54 +0000339 ProhibitAttributes(Attrs);
Lang Hamesa930e712012-10-21 01:10:01 +0000340 Diag(Tok, diag::err_pragma_fp_contract_scope);
Richard Smithaf3b3252017-05-18 19:21:48 +0000341 ConsumeAnnotationToken();
Lang Hamesa930e712012-10-21 01:10:01 +0000342 return StmtError();
343
Adam Nemet60d32642017-04-04 21:18:36 +0000344 case tok::annot_pragma_fp:
345 ProhibitAttributes(Attrs);
346 Diag(Tok, diag::err_pragma_fp_scope);
Richard Smithaf3b3252017-05-18 19:21:48 +0000347 ConsumeAnnotationToken();
Adam Nemet60d32642017-04-04 21:18:36 +0000348 return StmtError();
349
Kevin P. Neal2c0bc8b2018-08-14 17:06:56 +0000350 case tok::annot_pragma_fenv_access:
351 ProhibitAttributes(Attrs);
352 HandlePragmaFEnvAccess();
353 return StmtEmpty();
354
Eli Friedman68be1642012-10-04 02:36:51 +0000355 case tok::annot_pragma_opencl_extension:
356 ProhibitAttributes(Attrs);
357 HandlePragmaOpenCLExtension();
358 return StmtEmpty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000359
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000360 case tok::annot_pragma_captured:
Richard Smith12a41bd2013-09-16 21:17:44 +0000361 ProhibitAttributes(Attrs);
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000362 return HandlePragmaCaptured();
363
Alexey Bataeva769e072013-03-22 06:34:35 +0000364 case tok::annot_pragma_openmp:
Richard Smith12a41bd2013-09-16 21:17:44 +0000365 ProhibitAttributes(Attrs);
Alexey Bataevc4fad652016-01-13 11:18:54 +0000366 return ParseOpenMPDeclarativeOrExecutableDirective(Allowed);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000367
David Majnemer4bb09802014-02-10 19:50:15 +0000368 case tok::annot_pragma_ms_pointers_to_members:
369 ProhibitAttributes(Attrs);
370 HandlePragmaMSPointersToMembers();
371 return StmtEmpty();
372
Warren Huntc3b18962014-04-08 22:30:47 +0000373 case tok::annot_pragma_ms_pragma:
374 ProhibitAttributes(Attrs);
375 HandlePragmaMSPragma();
376 return StmtEmpty();
Aaron Ballmanb06b15a2014-06-06 12:40:24 +0000377
Alexey Bataev3d42f342015-11-20 07:02:57 +0000378 case tok::annot_pragma_ms_vtordisp:
379 ProhibitAttributes(Attrs);
380 HandlePragmaMSVtorDisp();
381 return StmtEmpty();
382
Aaron Ballmanb06b15a2014-06-06 12:40:24 +0000383 case tok::annot_pragma_loop_hint:
384 ProhibitAttributes(Attrs);
Alexey Bataevc4fad652016-01-13 11:18:54 +0000385 return ParsePragmaLoopHint(Stmts, Allowed, TrailingElseLoc, Attrs);
Richard Smithba3a4f92016-01-12 21:59:26 +0000386
387 case tok::annot_pragma_dump:
388 HandlePragmaDump();
389 return StmtEmpty();
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000390
391 case tok::annot_pragma_attribute:
392 HandlePragmaAttribute();
393 return StmtEmpty();
Sebastian Redlb219c902008-12-21 16:41:36 +0000394 }
395
Chris Lattner503fadc2006-08-10 05:45:44 +0000396 // If we reached this code, the statement must end in a semicolon.
Alp Toker97650562014-01-10 11:19:30 +0000397 if (!TryConsumeToken(tok::semi) && !Res.isInvalid()) {
Chris Lattner8e3eed02009-06-14 00:23:56 +0000398 // If the result was valid, then we do want to diagnose this. Use
399 // ExpectAndConsume to emit the diagnostic, even though we know it won't
400 // succeed.
401 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner0046de12008-11-13 18:52:53 +0000402 // Skip until we see a } or ;, but don't eat it.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000403 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner503fadc2006-08-10 05:45:44 +0000404 }
Mike Stump11289f42009-09-09 15:08:12 +0000405
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000406 return Res;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000407}
408
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000409/// Parse an expression statement.
Richard Smithc202b282012-04-14 00:33:13 +0000410StmtResult Parser::ParseExprStatement() {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000411 // If a case keyword is missing, this is where it should be inserted.
412 Token OldToken = Tok;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000413
Alex Lorenzf0b4e5d2016-10-18 10:55:01 +0000414 ExprStatementTokLoc = Tok.getLocation();
415
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000416 // expression[opt] ';'
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000417 ExprResult Expr(ParseExpression());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000418 if (Expr.isInvalid()) {
419 // If the expression is invalid, skip ahead to the next semicolon or '}'.
420 // Not doing this opens us up to the possibility of infinite loops if
421 // ParseExpression does not consume any tokens.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000422 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000423 if (Tok.is(tok::semi))
424 ConsumeToken();
John McCalleaef89b2013-03-22 02:10:40 +0000425 return Actions.ActOnExprStmtError();
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000426 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000427
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000428 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
429 Actions.CheckCaseExpression(Expr.get())) {
430 // If a constant expression is followed by a colon inside a switch block,
431 // suggest a missing case keyword.
432 Diag(OldToken, diag::err_expected_case_before_expression)
433 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000434
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000435 // Recover parsing as a case statement.
Richard Smithc202b282012-04-14 00:33:13 +0000436 return ParseCaseStatement(/*MissingCase=*/true, Expr);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000437 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000438
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000439 // Otherwise, eat the semicolon.
440 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000441 return Actions.ActOnExprStmt(Expr, isExprValueDiscarded());
John Wiegley1c0675e2011-04-28 01:08:34 +0000442}
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000443
John Wiegley1c0675e2011-04-28 01:08:34 +0000444/// ParseSEHTryBlockCommon
445///
446/// seh-try-block:
447/// '__try' compound-statement seh-handler
448///
449/// seh-handler:
450/// seh-except-block
451/// seh-finally-block
452///
Nico Weberdd256742015-02-25 01:43:27 +0000453StmtResult Parser::ParseSEHTryBlock() {
454 assert(Tok.is(tok::kw___try) && "Expected '__try'");
455 SourceLocation TryLoc = ConsumeToken();
456
Nico Weberfc3fe4f2015-02-25 02:22:06 +0000457 if (Tok.isNot(tok::l_brace))
Alp Tokerec543272013-12-24 09:48:30 +0000458 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
John Wiegley1c0675e2011-04-28 01:08:34 +0000459
Momchil Velikov57c681f2017-08-10 15:43:06 +0000460 StmtResult TryBlock(ParseCompoundStatement(
461 /*isStmtExpr=*/false,
462 Scope::DeclScope | Scope::CompoundStmtScope | Scope::SEHTryScope));
463 if (TryBlock.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000464 return TryBlock;
John Wiegley1c0675e2011-04-28 01:08:34 +0000465
466 StmtResult Handler;
Richard Smithc202b282012-04-14 00:33:13 +0000467 if (Tok.is(tok::identifier) &&
Douglas Gregor60060d62011-10-21 03:57:52 +0000468 Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +0000469 SourceLocation Loc = ConsumeToken();
470 Handler = ParseSEHExceptBlock(Loc);
471 } else if (Tok.is(tok::kw___finally)) {
472 SourceLocation Loc = ConsumeToken();
473 Handler = ParseSEHFinallyBlock(Loc);
474 } else {
Nico Weberfc3fe4f2015-02-25 02:22:06 +0000475 return StmtError(Diag(Tok, diag::err_seh_expected_handler));
John Wiegley1c0675e2011-04-28 01:08:34 +0000476 }
477
478 if(Handler.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000479 return Handler;
John Wiegley1c0675e2011-04-28 01:08:34 +0000480
481 return Actions.ActOnSEHTryBlock(false /* IsCXXTry */,
482 TryLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000483 TryBlock.get(),
Warren Huntf6be4cb2014-07-25 20:52:51 +0000484 Handler.get());
John Wiegley1c0675e2011-04-28 01:08:34 +0000485}
486
487/// ParseSEHExceptBlock - Handle __except
488///
489/// seh-except-block:
490/// '__except' '(' seh-filter-expression ')' compound-statement
491///
492StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) {
493 PoisonIdentifierRAIIObject raii(Ident__exception_code, false),
494 raii2(Ident___exception_code, false),
495 raii3(Ident_GetExceptionCode, false);
496
Alp Toker383d2c42014-01-01 03:08:43 +0000497 if (ExpectAndConsume(tok::l_paren))
John Wiegley1c0675e2011-04-28 01:08:34 +0000498 return StmtError();
499
Reid Kleckner1d59f992015-01-22 01:36:17 +0000500 ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope |
501 Scope::SEHExceptScope);
John Wiegley1c0675e2011-04-28 01:08:34 +0000502
David Blaikiebbafb8a2012-03-11 07:00:24 +0000503 if (getLangOpts().Borland) {
Francois Pichetbfaf4772011-04-28 03:14:31 +0000504 Ident__exception_info->setIsPoisoned(false);
505 Ident___exception_info->setIsPoisoned(false);
506 Ident_GetExceptionInfo->setIsPoisoned(false);
507 }
Reid Kleckner1d59f992015-01-22 01:36:17 +0000508
509 ExprResult FilterExpr;
510 {
511 ParseScopeFlags FilterScope(this, getCurScope()->getFlags() |
512 Scope::SEHFilterScope);
Reid Kleckner85368fb2015-04-02 22:09:32 +0000513 FilterExpr = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Reid Kleckner1d59f992015-01-22 01:36:17 +0000514 }
Francois Pichetbfaf4772011-04-28 03:14:31 +0000515
David Blaikiebbafb8a2012-03-11 07:00:24 +0000516 if (getLangOpts().Borland) {
Francois Pichetbfaf4772011-04-28 03:14:31 +0000517 Ident__exception_info->setIsPoisoned(true);
518 Ident___exception_info->setIsPoisoned(true);
519 Ident_GetExceptionInfo->setIsPoisoned(true);
520 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000521
522 if(FilterExpr.isInvalid())
523 return StmtError();
524
Alp Toker383d2c42014-01-01 03:08:43 +0000525 if (ExpectAndConsume(tok::r_paren))
John Wiegley1c0675e2011-04-28 01:08:34 +0000526 return StmtError();
527
Nico Weberfc3fe4f2015-02-25 02:22:06 +0000528 if (Tok.isNot(tok::l_brace))
529 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
530
Richard Smithc202b282012-04-14 00:33:13 +0000531 StmtResult Block(ParseCompoundStatement());
John Wiegley1c0675e2011-04-28 01:08:34 +0000532
533 if(Block.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000534 return Block;
John Wiegley1c0675e2011-04-28 01:08:34 +0000535
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000536 return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.get(), Block.get());
John Wiegley1c0675e2011-04-28 01:08:34 +0000537}
538
539/// ParseSEHFinallyBlock - Handle __finally
540///
541/// seh-finally-block:
542/// '__finally' compound-statement
543///
Nico Weberd64657f2015-03-09 02:47:59 +0000544StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyLoc) {
John Wiegley1c0675e2011-04-28 01:08:34 +0000545 PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
546 raii2(Ident___abnormal_termination, false),
547 raii3(Ident_AbnormalTermination, false);
548
Nico Weberfc3fe4f2015-02-25 02:22:06 +0000549 if (Tok.isNot(tok::l_brace))
550 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
551
Nico Weberd64657f2015-03-09 02:47:59 +0000552 ParseScope FinallyScope(this, 0);
553 Actions.ActOnStartSEHFinallyBlock();
554
Richard Smithc202b282012-04-14 00:33:13 +0000555 StmtResult Block(ParseCompoundStatement());
Nico Weberce903292015-03-09 03:17:15 +0000556 if(Block.isInvalid()) {
557 Actions.ActOnAbortSEHFinallyBlock();
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000558 return Block;
Nico Weberce903292015-03-09 03:17:15 +0000559 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000560
Nico Weberd64657f2015-03-09 02:47:59 +0000561 return Actions.ActOnFinishSEHFinallyBlock(FinallyLoc, Block.get());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000562}
563
Nico Weberc7d05962014-07-06 22:32:59 +0000564/// Handle __leave
565///
566/// seh-leave-statement:
567/// '__leave' ';'
568///
569StmtResult Parser::ParseSEHLeaveStatement() {
570 SourceLocation LeaveLoc = ConsumeToken(); // eat the '__leave'.
571 return Actions.ActOnSEHLeaveStmt(LeaveLoc, getCurScope());
572}
573
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000574/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000575///
576/// labeled-statement:
577/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000578/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000579///
Richard Smithc202b282012-04-14 00:33:13 +0000580StmtResult Parser::ParseLabeledStatement(ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000581 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
582 "Not an identifier!");
583
584 Token IdentTok = Tok; // Save the whole token.
585 ConsumeToken(); // eat the identifier.
586
587 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000588
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000589 // identifier ':' statement
590 SourceLocation ColonLoc = ConsumeToken();
591
Richard Smitha3e01cf2013-11-15 22:45:29 +0000592 // Read label attributes, if present.
593 StmtResult SubStmt;
594 if (Tok.is(tok::kw___attribute)) {
595 ParsedAttributesWithRange TempAttrs(AttrFactory);
596 ParseGNUAttributes(TempAttrs);
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000597
Richard Smitha3e01cf2013-11-15 22:45:29 +0000598 // In C++, GNU attributes only apply to the label if they are followed by a
599 // semicolon, to disambiguate label attributes from attributes on a labeled
600 // declaration.
601 //
602 // This doesn't quite match what GCC does; if the attribute list is empty
603 // and followed by a semicolon, GCC will reject (it appears to parse the
604 // attributes as part of a statement in that case). That looks like a bug.
605 if (!getLangOpts().CPlusPlus || Tok.is(tok::semi))
606 attrs.takeAllFrom(TempAttrs);
607 else if (isDeclarationStatement()) {
608 StmtVector Stmts;
609 // FIXME: We should do this whether or not we have a declaration
610 // statement, but that doesn't work correctly (because ProhibitAttributes
611 // can't handle GNU attributes), so only call it in the one case where
612 // GNU attributes are allowed.
613 SubStmt = ParseStatementOrDeclarationAfterAttributes(
Alexey Bataevc4fad652016-01-13 11:18:54 +0000614 Stmts, /*Allowed=*/ACK_StatementsOpenMPNonStandalone, nullptr,
615 TempAttrs);
Richard Smitha3e01cf2013-11-15 22:45:29 +0000616 if (!TempAttrs.empty() && !SubStmt.isInvalid())
Erich Keanec480f302018-07-12 21:09:05 +0000617 SubStmt = Actions.ProcessStmtAttributes(SubStmt.get(), TempAttrs,
618 TempAttrs.Range);
Richard Smitha3e01cf2013-11-15 22:45:29 +0000619 } else {
Alp Toker383d2c42014-01-01 03:08:43 +0000620 Diag(Tok, diag::err_expected_after) << "__attribute__" << tok::semi;
Richard Smitha3e01cf2013-11-15 22:45:29 +0000621 }
622 }
623
624 // If we've not parsed a statement yet, parse one now.
625 if (!SubStmt.isInvalid() && !SubStmt.isUsable())
626 SubStmt = ParseStatement();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000627
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000628 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000629 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000630 SubStmt = Actions.ActOnNullStmt(ColonLoc);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000631
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000632 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
633 IdentTok.getLocation());
Erich Keanec480f302018-07-12 21:09:05 +0000634 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, attrs);
635 attrs.clear();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000636
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000637 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
638 SubStmt.get());
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000639}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000640
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000641/// ParseCaseStatement
642/// labeled-statement:
643/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000644/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000645///
Richard Smithc202b282012-04-14 00:33:13 +0000646StmtResult Parser::ParseCaseStatement(bool MissingCase, ExprResult Expr) {
Richard Smithd4257d82011-04-21 22:48:40 +0000647 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Mike Stump11289f42009-09-09 15:08:12 +0000648
Chris Lattner34a22092009-03-04 04:23:07 +0000649 // It is very very common for code to contain many case statements recursively
650 // nested, as in (but usually without indentation):
651 // case 1:
652 // case 2:
653 // case 3:
654 // case 4:
655 // case 5: etc.
656 //
657 // Parsing this naively works, but is both inefficient and can cause us to run
658 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner2b19a6582009-03-04 18:24:58 +0000659 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner34a22092009-03-04 04:23:07 +0000660 // but all the grossness is constrained to ParseCaseStatement (and some
Richard Smitha3e01cf2013-11-15 22:45:29 +0000661 // weirdness in the actions), so this is just local grossness :).
Mike Stump11289f42009-09-09 15:08:12 +0000662
Chris Lattner34a22092009-03-04 04:23:07 +0000663 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
664 // example above.
John McCalldadc5752010-08-24 06:29:42 +0000665 StmtResult TopLevelCase(true);
Mike Stump11289f42009-09-09 15:08:12 +0000666
Chris Lattner34a22092009-03-04 04:23:07 +0000667 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
668 // gets updated each time a new case is parsed, and whose body is unset so
669 // far. When parsing 'case 4', this is the 'case 3' node.
Craig Topper161e4db2014-05-21 06:02:52 +0000670 Stmt *DeepestParsedCaseStmt = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000671
Chris Lattner34a22092009-03-04 04:23:07 +0000672 // While we have case statements, eat and stack them.
David Majnemer0ac67fa2011-06-13 05:50:12 +0000673 SourceLocation ColonLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000674 do {
Richard Trieu2c850c02011-04-21 21:44:26 +0000675 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
676 ConsumeToken(); // eat the 'case'.
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000677 ColonLoc = SourceLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000678
Douglas Gregord328d572009-09-21 18:10:23 +0000679 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000680 Actions.CodeCompleteCase(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000681 cutOffParsing();
682 return StmtError();
Douglas Gregord328d572009-09-21 18:10:23 +0000683 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000684
Chris Lattner125c0ee2009-12-10 00:38:54 +0000685 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
686 /// Disable this form of error recovery while we're parsing the case
687 /// expression.
688 ColonProtectionRAIIObject ColonProtection(*this);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000689
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000690 ExprResult LHS;
691 if (!MissingCase) {
Richard Smithef6c43d2018-07-26 18:41:30 +0000692 LHS = ParseCaseExpression(CaseLoc);
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000693 if (LHS.isInvalid()) {
694 // If constant-expression is parsed unsuccessfully, recover by skipping
695 // current case statement (moving to the colon that ends it).
Richard Smithef6c43d2018-07-26 18:41:30 +0000696 if (!SkipUntil(tok::colon, tok::r_brace, StopAtSemi | StopBeforeMatch))
697 return StmtError();
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000698 }
699 } else {
700 LHS = Expr;
701 MissingCase = false;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000702 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000703
Chris Lattner34a22092009-03-04 04:23:07 +0000704 // GNU case range extension.
705 SourceLocation DotDotDotLoc;
John McCalldadc5752010-08-24 06:29:42 +0000706 ExprResult RHS;
Alp Tokerec543272013-12-24 09:48:30 +0000707 if (TryConsumeToken(tok::ellipsis, DotDotDotLoc)) {
708 Diag(DotDotDotLoc, diag::ext_gnu_case_range);
Richard Smithef6c43d2018-07-26 18:41:30 +0000709 RHS = ParseCaseExpression(CaseLoc);
Chris Lattner34a22092009-03-04 04:23:07 +0000710 if (RHS.isInvalid()) {
Richard Smithef6c43d2018-07-26 18:41:30 +0000711 if (!SkipUntil(tok::colon, tok::r_brace, StopAtSemi | StopBeforeMatch))
712 return StmtError();
Chris Lattner34a22092009-03-04 04:23:07 +0000713 }
714 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000715
Chris Lattner125c0ee2009-12-10 00:38:54 +0000716 ColonProtection.restore();
Sebastian Redl042ad952008-12-11 19:30:53 +0000717
Alp Tokerec543272013-12-24 09:48:30 +0000718 if (TryConsumeToken(tok::colon, ColonLoc)) {
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000719 } else if (TryConsumeToken(tok::semi, ColonLoc) ||
720 TryConsumeToken(tok::coloncolon, ColonLoc)) {
721 // Treat "case blah;" or "case blah::" as a typo for "case blah:".
Alp Tokerec543272013-12-24 09:48:30 +0000722 Diag(ColonLoc, diag::err_expected_after)
723 << "'case'" << tok::colon
724 << FixItHint::CreateReplacement(ColonLoc, ":");
John McCall0140bfe2011-01-22 09:28:32 +0000725 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000726 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
Alp Tokerec543272013-12-24 09:48:30 +0000727 Diag(ExpectedLoc, diag::err_expected_after)
728 << "'case'" << tok::colon
729 << FixItHint::CreateInsertion(ExpectedLoc, ":");
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000730 ColonLoc = ExpectedLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000731 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000732
John McCalldadc5752010-08-24 06:29:42 +0000733 StmtResult Case =
Richard Smithef6c43d2018-07-26 18:41:30 +0000734 Actions.ActOnCaseStmt(CaseLoc, LHS, DotDotDotLoc, RHS, ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000735
Chris Lattner34a22092009-03-04 04:23:07 +0000736 // If we had a sema error parsing this case, then just ignore it and
737 // continue parsing the sub-stmt.
738 if (Case.isInvalid()) {
739 if (TopLevelCase.isInvalid()) // No parsed case stmts.
Alexey Bataevc4fad652016-01-13 11:18:54 +0000740 return ParseStatement(/*TrailingElseLoc=*/nullptr,
741 /*AllowOpenMPStandalone=*/true);
Chris Lattner34a22092009-03-04 04:23:07 +0000742 // Otherwise, just don't add it as a nested case.
743 } else {
744 // If this is the first case statement we parsed, it becomes TopLevelCase.
745 // Otherwise we link it into the current chain.
John McCall37ad5512010-08-23 06:44:23 +0000746 Stmt *NextDeepest = Case.get();
Chris Lattner34a22092009-03-04 04:23:07 +0000747 if (TopLevelCase.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000748 TopLevelCase = Case;
Chris Lattner34a22092009-03-04 04:23:07 +0000749 else
John McCallb268a282010-08-23 23:25:46 +0000750 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner34a22092009-03-04 04:23:07 +0000751 DeepestParsedCaseStmt = NextDeepest;
752 }
Mike Stump11289f42009-09-09 15:08:12 +0000753
Chris Lattner34a22092009-03-04 04:23:07 +0000754 // Handle all case statements.
755 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000756
Chris Lattner34a22092009-03-04 04:23:07 +0000757 // If we found a non-case statement, start by parsing it.
John McCalldadc5752010-08-24 06:29:42 +0000758 StmtResult SubStmt;
Mike Stump11289f42009-09-09 15:08:12 +0000759
Chris Lattner34a22092009-03-04 04:23:07 +0000760 if (Tok.isNot(tok::r_brace)) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000761 SubStmt = ParseStatement(/*TrailingElseLoc=*/nullptr,
762 /*AllowOpenMPStandalone=*/true);
Chris Lattner34a22092009-03-04 04:23:07 +0000763 } else {
764 // Nicely diagnose the common error "switch (X) { case 4: }", which is
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000765 // not valid. If ColonLoc doesn't point to a valid text location, there was
766 // another parsing error, so avoid producing extra diagnostics.
767 if (ColonLoc.isValid()) {
768 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
769 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
770 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
771 }
772 SubStmt = StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000773 }
Mike Stump11289f42009-09-09 15:08:12 +0000774
Chris Lattner34a22092009-03-04 04:23:07 +0000775 // Install the body into the most deeply-nested case.
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000776 if (DeepestParsedCaseStmt) {
777 // Broken sub-stmt shouldn't prevent forming the case statement properly.
778 if (SubStmt.isInvalid())
779 SubStmt = Actions.ActOnNullStmt(SourceLocation());
780 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
781 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000782
Chris Lattner34a22092009-03-04 04:23:07 +0000783 // Return the top level parsed statement tree.
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000784 return TopLevelCase;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000785}
786
787/// ParseDefaultStatement
788/// labeled-statement:
789/// 'default' ':' statement
790/// Note that this does not parse the 'statement' at the end.
791///
Richard Smithc202b282012-04-14 00:33:13 +0000792StmtResult Parser::ParseDefaultStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000793 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000794 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000795
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000796 SourceLocation ColonLoc;
Alp Tokerec543272013-12-24 09:48:30 +0000797 if (TryConsumeToken(tok::colon, ColonLoc)) {
Alp Tokerec543272013-12-24 09:48:30 +0000798 } else if (TryConsumeToken(tok::semi, ColonLoc)) {
Alp Toker97650562014-01-10 11:19:30 +0000799 // Treat "default;" as a typo for "default:".
Alp Tokerec543272013-12-24 09:48:30 +0000800 Diag(ColonLoc, diag::err_expected_after)
801 << "'default'" << tok::colon
802 << FixItHint::CreateReplacement(ColonLoc, ":");
John McCall0140bfe2011-01-22 09:28:32 +0000803 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000804 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
Alp Tokerec543272013-12-24 09:48:30 +0000805 Diag(ExpectedLoc, diag::err_expected_after)
806 << "'default'" << tok::colon
807 << FixItHint::CreateInsertion(ExpectedLoc, ":");
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000808 ColonLoc = ExpectedLoc;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000809 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000810
Richard Smith1002d102012-02-17 01:35:32 +0000811 StmtResult SubStmt;
812
813 if (Tok.isNot(tok::r_brace)) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000814 SubStmt = ParseStatement(/*TrailingElseLoc=*/nullptr,
815 /*AllowOpenMPStandalone=*/true);
Richard Smith1002d102012-02-17 01:35:32 +0000816 } else {
817 // Diagnose the common error "switch (X) {... default: }", which is
818 // not valid.
David Majnemerc6a99872011-06-14 15:24:38 +0000819 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
Richard Smith1002d102012-02-17 01:35:32 +0000820 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
821 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
822 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000823 }
824
Richard Smith1002d102012-02-17 01:35:32 +0000825 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000826 if (SubStmt.isInvalid())
Richard Smith1002d102012-02-17 01:35:32 +0000827 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl042ad952008-12-11 19:30:53 +0000828
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000829 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000830 SubStmt.get(), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000831}
832
Richard Smithc202b282012-04-14 00:33:13 +0000833StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
Momchil Velikov57c681f2017-08-10 15:43:06 +0000834 return ParseCompoundStatement(isStmtExpr,
835 Scope::DeclScope | Scope::CompoundStmtScope);
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000836}
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000837
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000838/// ParseCompoundStatement - Parse a "{}" block.
839///
840/// compound-statement: [C99 6.8.2]
841/// { block-item-list[opt] }
842/// [GNU] { label-declarations block-item-list } [TODO]
843///
844/// block-item-list:
845/// block-item
846/// block-item-list block-item
847///
848/// block-item:
849/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000850/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000851/// statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000852///
853/// [GNU] label-declarations:
854/// [GNU] label-declaration
855/// [GNU] label-declarations label-declaration
856///
857/// [GNU] label-declaration:
858/// [GNU] '__label__' identifier-list ';'
859///
Richard Smithc202b282012-04-14 00:33:13 +0000860StmtResult Parser::ParseCompoundStatement(bool isStmtExpr,
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000861 unsigned ScopeFlags) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000862 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000863
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000864 // Enter a scope to hold everything within the compound stmt. Compound
865 // statements can always hold declarations.
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000866 ParseScope CompoundScope(this, ScopeFlags);
Chris Lattnerf2978802007-01-21 06:52:16 +0000867
868 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000869 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000870}
871
Lang Hames2954cea2012-11-03 22:29:05 +0000872/// Parse any pragmas at the start of the compound expression. We handle these
873/// separately since some pragmas (FP_CONTRACT) must appear before any C
874/// statement in the compound, but may be intermingled with other pragmas.
875void Parser::ParseCompoundStatementLeadingPragmas() {
876 bool checkForPragmas = true;
877 while (checkForPragmas) {
878 switch (Tok.getKind()) {
879 case tok::annot_pragma_vis:
880 HandlePragmaVisibility();
881 break;
882 case tok::annot_pragma_pack:
883 HandlePragmaPack();
884 break;
885 case tok::annot_pragma_msstruct:
886 HandlePragmaMSStruct();
887 break;
888 case tok::annot_pragma_align:
889 HandlePragmaAlign();
890 break;
891 case tok::annot_pragma_weak:
892 HandlePragmaWeak();
893 break;
894 case tok::annot_pragma_weakalias:
895 HandlePragmaWeakAlias();
896 break;
897 case tok::annot_pragma_redefine_extname:
898 HandlePragmaRedefineExtname();
899 break;
900 case tok::annot_pragma_opencl_extension:
901 HandlePragmaOpenCLExtension();
902 break;
903 case tok::annot_pragma_fp_contract:
904 HandlePragmaFPContract();
905 break;
Adam Nemet60d32642017-04-04 21:18:36 +0000906 case tok::annot_pragma_fp:
907 HandlePragmaFP();
908 break;
Kevin P. Neal2c0bc8b2018-08-14 17:06:56 +0000909 case tok::annot_pragma_fenv_access:
910 HandlePragmaFEnvAccess();
911 break;
David Majnemer4bb09802014-02-10 19:50:15 +0000912 case tok::annot_pragma_ms_pointers_to_members:
913 HandlePragmaMSPointersToMembers();
914 break;
Warren Huntc3b18962014-04-08 22:30:47 +0000915 case tok::annot_pragma_ms_pragma:
916 HandlePragmaMSPragma();
917 break;
Alexey Bataev3d42f342015-11-20 07:02:57 +0000918 case tok::annot_pragma_ms_vtordisp:
919 HandlePragmaMSVtorDisp();
920 break;
Richard Smithba3a4f92016-01-12 21:59:26 +0000921 case tok::annot_pragma_dump:
922 HandlePragmaDump();
923 break;
Lang Hames2954cea2012-11-03 22:29:05 +0000924 default:
925 checkForPragmas = false;
926 break;
927 }
928 }
929
930}
931
Roman Lebedev377748f2018-11-20 18:59:05 +0000932/// Consume any extra semi-colons resulting in null statements,
933/// returning true if any tok::semi were consumed.
934bool Parser::ConsumeNullStmt(StmtVector &Stmts) {
935 if (!Tok.is(tok::semi))
936 return false;
937
938 SourceLocation StartLoc = Tok.getLocation();
939 SourceLocation EndLoc;
940
941 while (Tok.is(tok::semi) && !Tok.hasLeadingEmptyMacro() &&
942 Tok.getLocation().isValid() && !Tok.getLocation().isMacroID()) {
943 EndLoc = Tok.getLocation();
944
945 // Don't just ConsumeToken() this tok::semi, do store it in AST.
946 StmtResult R = ParseStatementOrDeclaration(Stmts, ACK_Any);
947 if (R.isUsable())
948 Stmts.push_back(R.get());
949 }
950
951 // Did not consume any extra semi.
952 if (EndLoc.isInvalid())
953 return false;
954
955 Diag(StartLoc, diag::warn_null_statement)
956 << FixItHint::CreateRemoval(SourceRange(StartLoc, EndLoc));
957 return true;
958}
959
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +0000960bool Parser::isExprValueDiscarded() {
961 if (Actions.isCurCompoundStmtAStmtExpr()) {
962 // Look to see if the next two tokens close the statement expression;
963 // if so, this expression statement is the last statement in a
964 // statment expression.
965 return Tok.isNot(tok::r_brace) || NextToken().isNot(tok::r_paren);
966 }
967 return true;
968}
969
Chris Lattnerf2978802007-01-21 06:52:16 +0000970/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000971/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000972/// consume the '}' at the end of the block. It does not manipulate the scope
973/// stack.
John McCalldadc5752010-08-24 06:29:42 +0000974StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000975 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000976 Tok.getLocation(),
977 "in compound statement ('{}')");
Lang Hames5de91cc2012-10-02 04:45:10 +0000978
979 // Record the state of the FP_CONTRACT pragma, restore on leaving the
980 // compound statement.
981 Sema::FPContractStateRAII SaveFPContractState(Actions);
982
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000983 InMessageExpressionRAIIObject InMessage(*this, false);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000984 BalancedDelimiterTracker T(*this, tok::l_brace);
985 if (T.consumeOpen())
986 return StmtError();
Chris Lattnerf2978802007-01-21 06:52:16 +0000987
Richard Smith6eb9b9e2018-02-03 00:44:57 +0000988 Sema::CompoundScopeRAII CompoundScope(Actions, isStmtExpr);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +0000989
Lang Hames2954cea2012-11-03 22:29:05 +0000990 // Parse any pragmas at the beginning of the compound statement.
991 ParseCompoundStatementLeadingPragmas();
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000992
Lang Hames2954cea2012-11-03 22:29:05 +0000993 StmtVector Stmts;
Lang Hamesa930e712012-10-21 01:10:01 +0000994
Chris Lattner43e7f312011-02-18 02:08:43 +0000995 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
996 // only allowed at the start of a compound stmt regardless of the language.
997 while (Tok.is(tok::kw___label__)) {
998 SourceLocation LabelLoc = ConsumeToken();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000999
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001000 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner43e7f312011-02-18 02:08:43 +00001001 while (1) {
1002 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001003 Diag(Tok, diag::err_expected) << tok::identifier;
Chris Lattner43e7f312011-02-18 02:08:43 +00001004 break;
1005 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001006
Chris Lattner43e7f312011-02-18 02:08:43 +00001007 IdentifierInfo *II = Tok.getIdentifierInfo();
1008 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara1c3af962011-03-05 18:21:20 +00001009 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001010
Alp Tokerec543272013-12-24 09:48:30 +00001011 if (!TryConsumeToken(tok::comma))
Chris Lattner43e7f312011-02-18 02:08:43 +00001012 break;
Chris Lattner43e7f312011-02-18 02:08:43 +00001013 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001014
John McCall084e83d2011-03-24 11:26:52 +00001015 DeclSpec DS(AttrFactory);
Rafael Espindolaab417692013-07-09 12:05:01 +00001016 DeclGroupPtrTy Res =
1017 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner43e7f312011-02-18 02:08:43 +00001018 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001019
Chris Lattner02f1b612012-04-28 16:12:17 +00001020 ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
Chris Lattner43e7f312011-02-18 02:08:43 +00001021 if (R.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001022 Stmts.push_back(R.get());
Chris Lattner43e7f312011-02-18 02:08:43 +00001023 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001024
Richard Smith752ada82015-11-17 23:32:01 +00001025 while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
1026 Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001027 if (Tok.is(tok::annot_pragma_unused)) {
1028 HandlePragmaUnused();
1029 continue;
1030 }
1031
Roman Lebedev377748f2018-11-20 18:59:05 +00001032 if (ConsumeNullStmt(Stmts))
1033 continue;
1034
John McCalldadc5752010-08-24 06:29:42 +00001035 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001036 if (Tok.isNot(tok::kw___extension__)) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00001037 R = ParseStatementOrDeclaration(Stmts, ACK_Any);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001038 } else {
1039 // __extension__ can start declarations and it can also be a unary
1040 // operator for expressions. Consume multiple __extension__ markers here
1041 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +00001042 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001043 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001044 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001045 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +00001046
John McCall084e83d2011-03-24 11:26:52 +00001047 ParsedAttributesWithRange attrs(AttrFactory);
Craig Topper161e4db2014-05-21 06:02:52 +00001048 MaybeParseCXX11Attributes(attrs, nullptr,
1049 /*MightBeObjCMessageSend*/ true);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001050
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001051 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001052 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +00001053 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +00001054 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +00001055 ExtensionRAIIObject O(Diags);
1056
Chris Lattner49836b42009-04-02 04:16:50 +00001057 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Faisal Vali421b2d12017-12-29 05:41:00 +00001058 DeclGroupPtrTy Res =
1059 ParseDeclaration(DeclaratorContext::BlockContext, DeclEnd, attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001060 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001061 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +00001062 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +00001063 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +00001064
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001065 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001066 SkipUntil(tok::semi);
1067 continue;
1068 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001069
Alexis Hunt96d5c762009-11-21 08:43:09 +00001070 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +00001071 // Eat the semicolon at the end of stmt and convert the expr into a
1072 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00001073 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00001074 R = Actions.ActOnExprStmt(Res, isExprValueDiscarded());
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001075 }
1076 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001077
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001078 if (R.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001079 Stmts.push_back(R.get());
Chris Lattner30f910e2006-10-16 05:52:41 +00001080 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001081
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +00001082 SourceLocation CloseLoc = Tok.getLocation();
1083
Chris Lattner0ccd51e2006-08-09 05:47:47 +00001084 // We broke out of the while loop because we found a '}' or EOF.
Nico Webera48b6c22012-12-30 23:36:56 +00001085 if (!T.consumeClose())
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +00001086 // Recover by creating a compound statement with what we parsed so far,
1087 // instead of dropping everything and returning StmtError();
Nico Webera48b6c22012-12-30 23:36:56 +00001088 CloseLoc = T.getCloseLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +00001089
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +00001090 return Actions.ActOnCompoundStmt(T.getOpenLocation(), CloseLoc,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001091 Stmts, isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +00001092}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001093
Chris Lattnerc0081db2008-12-12 06:31:07 +00001094/// ParseParenExprOrCondition:
1095/// [C ] '(' expression ')'
Richard Smithc7a05a92016-06-29 21:17:59 +00001096/// [C++] '(' condition ')'
1097/// [C++1z] '(' init-statement[opt] condition ')'
Chris Lattnerc0081db2008-12-12 06:31:07 +00001098///
1099/// This function parses and performs error recovery on the specified condition
1100/// or expression (depending on whether we're in C++ or C mode). This function
1101/// goes out of its way to recover well. It returns true if there was a parser
1102/// error (the right paren couldn't be found), which indicates that the caller
1103/// should try to recover harder. It returns false if the condition is
1104/// successfully parsed. Note that a successful parse can still have semantic
1105/// errors in the condition.
Richard Smithc7a05a92016-06-29 21:17:59 +00001106bool Parser::ParseParenExprOrCondition(StmtResult *InitStmt,
1107 Sema::ConditionResult &Cond,
Douglas Gregore60e41a2010-05-06 17:25:47 +00001108 SourceLocation Loc,
Richard Smith03a4aa32016-06-23 19:02:52 +00001109 Sema::ConditionKind CK) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001110 BalancedDelimiterTracker T(*this, tok::l_paren);
1111 T.consumeOpen();
1112
David Blaikiebbafb8a2012-03-11 07:00:24 +00001113 if (getLangOpts().CPlusPlus)
Richard Smithc7a05a92016-06-29 21:17:59 +00001114 Cond = ParseCXXCondition(InitStmt, Loc, CK);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001115 else {
Richard Smith03a4aa32016-06-23 19:02:52 +00001116 ExprResult CondExpr = ParseExpression();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001117
Douglas Gregore60e41a2010-05-06 17:25:47 +00001118 // If required, convert to a boolean value.
Richard Smith03a4aa32016-06-23 19:02:52 +00001119 if (CondExpr.isInvalid())
1120 Cond = Sema::ConditionError();
1121 else
1122 Cond = Actions.ActOnCondition(getCurScope(), Loc, CondExpr.get(), CK);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001123 }
Mike Stump11289f42009-09-09 15:08:12 +00001124
Chris Lattnerc0081db2008-12-12 06:31:07 +00001125 // If the parser was confused by the condition and we don't have a ')', try to
1126 // recover by skipping ahead to a semi and bailing out. If condexp is
1127 // semantically invalid but we have well formed code, keep going.
Richard Smith03a4aa32016-06-23 19:02:52 +00001128 if (Cond.isInvalid() && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +00001129 SkipUntil(tok::semi);
1130 // Skipping may have stopped if it found the containing ')'. If so, we can
1131 // continue parsing the if statement.
1132 if (Tok.isNot(tok::r_paren))
1133 return true;
1134 }
Mike Stump11289f42009-09-09 15:08:12 +00001135
Chris Lattnerc0081db2008-12-12 06:31:07 +00001136 // Otherwise the condition is valid or the rparen is present.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001137 T.consumeClose();
Chad Rosier67055f52012-07-10 21:35:27 +00001138
Chris Lattner70d44982012-04-28 16:24:20 +00001139 // Check for extraneous ')'s to catch things like "if (foo())) {". We know
1140 // that all callers are looking for a statement after the condition, so ")"
1141 // isn't valid.
1142 while (Tok.is(tok::r_paren)) {
1143 Diag(Tok, diag::err_extraneous_rparen_in_condition)
1144 << FixItHint::CreateRemoval(Tok.getLocation());
1145 ConsumeParen();
1146 }
Chad Rosier67055f52012-07-10 21:35:27 +00001147
Chris Lattnerc0081db2008-12-12 06:31:07 +00001148 return false;
1149}
1150
1151
Chris Lattnerc951dae2006-08-10 04:23:57 +00001152/// ParseIfStatement
1153/// if-statement: [C99 6.8.4.1]
1154/// 'if' '(' expression ')' statement
1155/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001156/// [C++] 'if' '(' condition ')' statement
1157/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +00001158///
Richard Smithc202b282012-04-14 00:33:13 +00001159StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001160 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001161 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +00001162
Richard Smithb130fe72016-06-23 19:16:49 +00001163 bool IsConstexpr = false;
1164 if (Tok.is(tok::kw_constexpr)) {
Aaron Ballmanc351fba2017-12-04 20:27:34 +00001165 Diag(Tok, getLangOpts().CPlusPlus17 ? diag::warn_cxx14_compat_constexpr_if
Richard Smithb130fe72016-06-23 19:16:49 +00001166 : diag::ext_constexpr_if);
1167 IsConstexpr = true;
1168 ConsumeToken();
1169 }
1170
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001171 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001172 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +00001173 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +00001174 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +00001175 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001176
David Blaikiebbafb8a2012-03-11 07:00:24 +00001177 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001178
Chris Lattner2dd1b722007-08-26 23:08:06 +00001179 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
1180 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001181 //
1182 // C++ 6.4p3:
1183 // A name introduced by a declaration in a condition is in scope from its
1184 // point of declaration until the end of the substatements controlled by the
1185 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001186 // C++ 3.3.2p4:
1187 // Names declared in the for-init-statement, and in the condition of if,
1188 // while, for, and switch statements are local to the if, while, for, or
1189 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001190 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001191 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +00001192
Chris Lattnerc951dae2006-08-10 04:23:57 +00001193 // Parse the condition.
Richard Smithc7a05a92016-06-29 21:17:59 +00001194 StmtResult InitStmt;
Richard Smith03a4aa32016-06-23 19:02:52 +00001195 Sema::ConditionResult Cond;
Richard Smithc7a05a92016-06-29 21:17:59 +00001196 if (ParseParenExprOrCondition(&InitStmt, Cond, IfLoc,
Richard Smithb130fe72016-06-23 19:16:49 +00001197 IsConstexpr ? Sema::ConditionKind::ConstexprIf
1198 : Sema::ConditionKind::Boolean))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001199 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +00001200
Richard Smithb130fe72016-06-23 19:16:49 +00001201 llvm::Optional<bool> ConstexprCondition;
1202 if (IsConstexpr)
1203 ConstexprCondition = Cond.getKnownValue();
1204
Chris Lattner8fb26252007-08-22 05:28:50 +00001205 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001206 // there is no compound stmt. C90 does not have this clause. We only do this
1207 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001208 //
1209 // C++ 6.4p1:
1210 // The substatement in a selection-statement (each substatement, in the else
1211 // form of the if statement) implicitly defines a local scope.
1212 //
1213 // For C++ we create a scope for the condition and a new scope for
1214 // substatements because:
1215 // -When the 'then' scope exits, we want the condition declaration to still be
1216 // active for the 'else' scope too.
1217 // -Sema will detect name clashes by considering declarations of a
1218 // 'ControlScope' as part of its direct subscope.
1219 // -If we wanted the condition and substatement to be in the same scope, we
1220 // would have to notify ParseStatement not to create a new scope. It's
1221 // simpler to let it create a new scope.
1222 //
David Majnemer2206bf52014-03-05 08:57:59 +00001223 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001224
Chris Lattner5c5808a2007-10-29 05:08:52 +00001225 // Read the 'then' stmt.
1226 SourceLocation ThenStmtLoc = Tok.getLocation();
Nico Weber3cef1082011-12-22 23:26:17 +00001227
1228 SourceLocation InnerStatementTrailingElseLoc;
Richard Smithb130fe72016-06-23 19:16:49 +00001229 StmtResult ThenStmt;
1230 {
1231 EnterExpressionEvaluationContext PotentiallyDiscarded(
Faisal Valid143a0c2017-04-01 21:30:49 +00001232 Actions, Sema::ExpressionEvaluationContext::DiscardedStatement, nullptr,
Nicolas Lesserb6d5c582018-07-12 18:45:41 +00001233 Sema::ExpressionEvaluationContextRecord::EK_Other,
Richard Smithb130fe72016-06-23 19:16:49 +00001234 /*ShouldEnter=*/ConstexprCondition && !*ConstexprCondition);
1235 ThenStmt = ParseStatement(&InnerStatementTrailingElseLoc);
1236 }
Chris Lattnerac4471c2007-05-28 05:38:24 +00001237
Chris Lattner37e54f42007-08-22 05:16:28 +00001238 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001239 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001240
Chris Lattnerc951dae2006-08-10 04:23:57 +00001241 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +00001242 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +00001243 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +00001244 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001245
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001246 if (Tok.is(tok::kw_else)) {
Nico Weber3cef1082011-12-22 23:26:17 +00001247 if (TrailingElseLoc)
1248 *TrailingElseLoc = Tok.getLocation();
1249
Chris Lattneraf635312006-10-16 06:06:51 +00001250 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +00001251 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +00001252
Chris Lattner8fb26252007-08-22 05:28:50 +00001253 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001254 // there is no compound stmt. C90 does not have this clause. We only do
1255 // this if the body isn't a compound statement to avoid push/pop in common
1256 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001257 //
1258 // C++ 6.4p1:
1259 // The substatement in a selection-statement (each substatement, in the else
1260 // form of the if statement) implicitly defines a local scope.
1261 //
Richard Smithb130fe72016-06-23 19:16:49 +00001262 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX,
1263 Tok.is(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001264
Richard Smithb130fe72016-06-23 19:16:49 +00001265 EnterExpressionEvaluationContext PotentiallyDiscarded(
Faisal Valid143a0c2017-04-01 21:30:49 +00001266 Actions, Sema::ExpressionEvaluationContext::DiscardedStatement, nullptr,
Nicolas Lesserb6d5c582018-07-12 18:45:41 +00001267 Sema::ExpressionEvaluationContextRecord::EK_Other,
Richard Smithb130fe72016-06-23 19:16:49 +00001268 /*ShouldEnter=*/ConstexprCondition && *ConstexprCondition);
Chris Lattner30f910e2006-10-16 05:52:41 +00001269 ElseStmt = ParseStatement();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001270
Chris Lattner37e54f42007-08-22 05:16:28 +00001271 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001272 InnerScope.Exit();
Douglas Gregor4ecb7202011-07-30 08:36:53 +00001273 } else if (Tok.is(tok::code_completion)) {
1274 Actions.CodeCompleteAfterIf(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001275 cutOffParsing();
1276 return StmtError();
Nico Weber3cef1082011-12-22 23:26:17 +00001277 } else if (InnerStatementTrailingElseLoc.isValid()) {
1278 Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else);
Chris Lattnerc951dae2006-08-10 04:23:57 +00001279 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001280
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001281 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +00001282
Chris Lattner5c5808a2007-10-29 05:08:52 +00001283 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +00001284 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +00001285 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001286 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
Craig Topper161e4db2014-05-21 06:02:52 +00001287 (ThenStmt.isInvalid() && ElseStmt.get() == nullptr) ||
1288 (ThenStmt.get() == nullptr && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +00001289 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +00001290 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +00001291 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001292
Chris Lattner5c5808a2007-10-29 05:08:52 +00001293 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001294 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001295 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001296 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001297 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001298
Richard Smithc7a05a92016-06-29 21:17:59 +00001299 return Actions.ActOnIfStmt(IfLoc, IsConstexpr, InitStmt.get(), Cond,
1300 ThenStmt.get(), ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +00001301}
1302
Chris Lattner9075bd72006-08-10 04:59:57 +00001303/// ParseSwitchStatement
1304/// switch-statement:
1305/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001306/// [C++] 'switch' '(' condition ')' statement
Richard Smithc202b282012-04-14 00:33:13 +00001307StmtResult Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001308 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001309 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +00001310
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001311 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001312 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +00001313 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001314 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001315 }
Chris Lattner2dd1b722007-08-26 23:08:06 +00001316
David Blaikiebbafb8a2012-03-11 07:00:24 +00001317 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001318
Chris Lattner2dd1b722007-08-26 23:08:06 +00001319 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1320 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001321 //
1322 // C++ 6.4p3:
1323 // A name introduced by a declaration in a condition is in scope from its
1324 // point of declaration until the end of the substatements controlled by the
1325 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001326 // C++ 3.3.2p4:
1327 // Names declared in the for-init-statement, and in the condition of if,
1328 // while, for, and switch statements are local to the if, while, for, or
1329 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001330 //
Serge Pavlov09f99242014-01-23 15:05:00 +00001331 unsigned ScopeFlags = Scope::SwitchScope;
Chris Lattnerc0081db2008-12-12 06:31:07 +00001332 if (C99orCXX)
1333 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001334 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001335
Chris Lattner9075bd72006-08-10 04:59:57 +00001336 // Parse the condition.
Richard Smithc7a05a92016-06-29 21:17:59 +00001337 StmtResult InitStmt;
Richard Smith03a4aa32016-06-23 19:02:52 +00001338 Sema::ConditionResult Cond;
Richard Smithc7a05a92016-06-29 21:17:59 +00001339 if (ParseParenExprOrCondition(&InitStmt, Cond, SwitchLoc,
1340 Sema::ConditionKind::Switch))
Sebastian Redlb62406f2008-12-11 19:48:14 +00001341 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +00001342
Richard Smithc7a05a92016-06-29 21:17:59 +00001343 StmtResult Switch =
1344 Actions.ActOnStartOfSwitchStmt(SwitchLoc, InitStmt.get(), Cond);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001345
Douglas Gregore60e41a2010-05-06 17:25:47 +00001346 if (Switch.isInvalid()) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001347 // Skip the switch body.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001348 // FIXME: This is not optimal recovery, but parsing the body is more
1349 // dangerous due to the presence of case and default statements, which
1350 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +00001351 if (Tok.is(tok::l_brace)) {
1352 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001353 SkipUntil(tok::r_brace);
Douglas Gregor4abc32d2010-05-20 23:20:59 +00001354 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +00001355 SkipUntil(tok::semi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001356 return Switch;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001357 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001358
Chris Lattner8fb26252007-08-22 05:28:50 +00001359 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001360 // there is no compound stmt. C90 does not have this clause. We only do this
1361 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001362 //
1363 // C++ 6.4p1:
1364 // The substatement in a selection-statement (each substatement, in the else
1365 // form of the if statement) implicitly defines a local scope.
1366 //
1367 // See comments in ParseIfStatement for why we create a scope for the
1368 // condition and a new scope for substatement in C++.
1369 //
Serge Pavlov09f99242014-01-23 15:05:00 +00001370 getCurScope()->AddFlags(Scope::BreakScope);
David Majnemer2206bf52014-03-05 08:57:59 +00001371 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +00001372
Hans Wennborg852c3462014-06-17 00:09:05 +00001373 // We have incremented the mangling number for the SwitchScope and the
1374 // InnerScope, which is one too many.
1375 if (C99orCXX)
David Majnemera7f8c462015-03-19 21:54:30 +00001376 getCurScope()->decrementMSManglingNumber();
Hans Wennborg852c3462014-06-17 00:09:05 +00001377
Chris Lattner9075bd72006-08-10 04:59:57 +00001378 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001379 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001380
Chris Lattner8fd2d012010-01-24 01:50:29 +00001381 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001382 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001383 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001384
John McCallb268a282010-08-23 23:25:46 +00001385 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001386}
1387
1388/// ParseWhileStatement
1389/// while-statement: [C99 6.8.5.1]
1390/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001391/// [C++] 'while' '(' condition ')' statement
Richard Smithc202b282012-04-14 00:33:13 +00001392StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001393 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +00001394 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +00001395 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001396
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001397 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001398 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +00001399 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001400 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001401 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001402
David Blaikiebbafb8a2012-03-11 07:00:24 +00001403 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001404
Chris Lattner2dd1b722007-08-26 23:08:06 +00001405 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1406 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001407 //
1408 // C++ 6.4p3:
1409 // A name introduced by a declaration in a condition is in scope from its
1410 // point of declaration until the end of the substatements controlled by the
1411 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001412 // C++ 3.3.2p4:
1413 // Names declared in the for-init-statement, and in the condition of if,
1414 // while, for, and switch statements are local to the if, while, for, or
1415 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001416 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001417 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001418 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001419 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1420 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001421 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001422 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1423 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001424
Chris Lattner9075bd72006-08-10 04:59:57 +00001425 // Parse the condition.
Richard Smith03a4aa32016-06-23 19:02:52 +00001426 Sema::ConditionResult Cond;
Richard Smithc7a05a92016-06-29 21:17:59 +00001427 if (ParseParenExprOrCondition(nullptr, Cond, WhileLoc,
1428 Sema::ConditionKind::Boolean))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001429 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001430
Justin Bognere4ebb6c2013-12-03 07:36:55 +00001431 // C99 6.8.5p5 - In C99, the body of the while statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001432 // there is no compound stmt. C90 does not have this clause. We only do this
1433 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001434 //
1435 // C++ 6.5p2:
1436 // The substatement in an iteration-statement implicitly defines a local scope
1437 // which is entered and exited each time through the loop.
1438 //
1439 // See comments in ParseIfStatement for why we create a scope for the
1440 // condition and a new scope for substatement in C++.
1441 //
David Majnemer2206bf52014-03-05 08:57:59 +00001442 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001443
Chris Lattner9075bd72006-08-10 04:59:57 +00001444 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001445 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001446
Chris Lattner8fb26252007-08-22 05:28:50 +00001447 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001448 InnerScope.Exit();
1449 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001450
Richard Smith03a4aa32016-06-23 19:02:52 +00001451 if (Cond.isInvalid() || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001452 return StmtError();
1453
Richard Smith03a4aa32016-06-23 19:02:52 +00001454 return Actions.ActOnWhileStmt(WhileLoc, Cond, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001455}
1456
1457/// ParseDoStatement
1458/// do-statement: [C99 6.8.5.2]
1459/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +00001460/// Note: this lets the caller parse the end ';'.
Richard Smithc202b282012-04-14 00:33:13 +00001461StmtResult Parser::ParseDoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001462 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001463 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001464
Chris Lattner2dd1b722007-08-26 23:08:06 +00001465 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1466 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001467 unsigned ScopeFlags;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001468 if (getLangOpts().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001469 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001470 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001471 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001472
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001473 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001474
Justin Bognere4ebb6c2013-12-03 07:36:55 +00001475 // C99 6.8.5p5 - In C99, the body of the do statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001476 // there is no compound stmt. C90 does not have this clause. We only do this
1477 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +00001478 //
1479 // C++ 6.5p2:
1480 // The substatement in an iteration-statement implicitly defines a local scope
1481 // which is entered and exited each time through the loop.
1482 //
David Majnemer2206bf52014-03-05 08:57:59 +00001483 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
1484 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001485
Chris Lattner9075bd72006-08-10 04:59:57 +00001486 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001487 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +00001488
Chris Lattner8fb26252007-08-22 05:28:50 +00001489 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001490 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001491
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001492 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001493 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +00001494 Diag(Tok, diag::err_expected_while);
Alp Tokerec543272013-12-24 09:48:30 +00001495 Diag(DoLoc, diag::note_matching) << "'do'";
Alexey Bataevee6507d2013-11-18 08:17:37 +00001496 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner0046de12008-11-13 18:52:53 +00001497 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001498 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001499 }
Chris Lattneraf635312006-10-16 06:06:51 +00001500 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001501
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001502 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001503 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Alexey Bataevee6507d2013-11-18 08:17:37 +00001504 SkipUntil(tok::semi, StopBeforeMatch);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001505 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001506 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001507
Richard Smithc2c8bb82013-10-15 01:34:54 +00001508 // Parse the parenthesized expression.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001509 BalancedDelimiterTracker T(*this, tok::l_paren);
1510 T.consumeOpen();
Chad Rosier67055f52012-07-10 21:35:27 +00001511
Richard Smithc2c8bb82013-10-15 01:34:54 +00001512 // A do-while expression is not a condition, so can't have attributes.
1513 DiagnoseAndSkipCXX11Attributes();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001514
John McCalldadc5752010-08-24 06:29:42 +00001515 ExprResult Cond = ParseExpression();
Alex Lorenzc38ba662017-10-30 22:55:11 +00001516 // Correct the typos in condition before closing the scope.
1517 if (Cond.isUsable())
1518 Cond = Actions.CorrectDelayedTyposInExpr(Cond);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001519 T.consumeClose();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001520 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001521
Sebastian Redlb62406f2008-12-11 19:48:14 +00001522 if (Cond.isInvalid() || Body.isInvalid())
1523 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001524
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001525 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(),
1526 Cond.get(), T.getCloseLocation());
Chris Lattner9075bd72006-08-10 04:59:57 +00001527}
1528
Richard Smith955bf012014-06-19 11:42:00 +00001529bool Parser::isForRangeIdentifier() {
1530 assert(Tok.is(tok::identifier));
1531
1532 const Token &Next = NextToken();
1533 if (Next.is(tok::colon))
1534 return true;
1535
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001536 if (Next.isOneOf(tok::l_square, tok::kw_alignas)) {
Richard Smith955bf012014-06-19 11:42:00 +00001537 TentativeParsingAction PA(*this);
1538 ConsumeToken();
1539 SkipCXX11Attributes();
1540 bool Result = Tok.is(tok::colon);
1541 PA.Revert();
1542 return Result;
1543 }
1544
1545 return false;
1546}
1547
Chris Lattner9075bd72006-08-10 04:59:57 +00001548/// ParseForStatement
1549/// for-statement: [C99 6.8.5.3]
1550/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1551/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001552/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1553/// [C++] statement
Richard Smith0e304ea2015-10-22 04:46:14 +00001554/// [C++0x] 'for'
1555/// 'co_await'[opt] [Coroutines]
1556/// '(' for-range-declaration ':' for-range-initializer ')'
1557/// statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001558/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1559/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001560///
1561/// [C++] for-init-statement:
1562/// [C++] expression-statement
1563/// [C++] simple-declaration
1564///
Richard Smith02e85f32011-04-14 22:09:26 +00001565/// [C++0x] for-range-declaration:
1566/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1567/// [C++0x] for-range-initializer:
1568/// [C++0x] expression
1569/// [C++0x] braced-init-list [TODO]
Richard Smithc202b282012-04-14 00:33:13 +00001570StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001571 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001572 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001573
Richard Smith0e304ea2015-10-22 04:46:14 +00001574 SourceLocation CoawaitLoc;
1575 if (Tok.is(tok::kw_co_await))
1576 CoawaitLoc = ConsumeToken();
1577
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001578 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001579 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +00001580 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001581 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001582 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001583
Chad Rosier67055f52012-07-10 21:35:27 +00001584 bool C99orCXXorObjC = getLangOpts().C99 || getLangOpts().CPlusPlus ||
Erik Pilkingtonfa983902018-10-30 20:31:30 +00001585 getLangOpts().ObjC;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001586
Chris Lattner2dd1b722007-08-26 23:08:06 +00001587 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1588 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001589 //
1590 // C++ 6.4p3:
1591 // A name introduced by a declaration in a condition is in scope from its
1592 // point of declaration until the end of the substatements controlled by the
1593 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001594 // C++ 3.3.2p4:
1595 // Names declared in the for-init-statement, and in the condition of if,
1596 // while, for, and switch statements are local to the if, while, for, or
1597 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001598 // C++ 6.5.3p1:
1599 // Names declared in the for-init-statement are in the same declarative-region
1600 // as those declared in the condition.
1601 //
Serge Pavlov09f99242014-01-23 15:05:00 +00001602 unsigned ScopeFlags = 0;
Chris Lattner934074c2009-04-22 00:54:41 +00001603 if (C99orCXXorObjC)
Serge Pavlov09f99242014-01-23 15:05:00 +00001604 ScopeFlags = Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001605
1606 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +00001607
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001608 BalancedDelimiterTracker T(*this, tok::l_paren);
1609 T.consumeOpen();
1610
John McCalldadc5752010-08-24 06:29:42 +00001611 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001612
Richard Smith8baa5002018-09-28 18:44:09 +00001613 bool ForEach = false;
John McCalldadc5752010-08-24 06:29:42 +00001614 StmtResult FirstPart;
Richard Smith03a4aa32016-06-23 19:02:52 +00001615 Sema::ConditionResult SecondPart;
John McCalldadc5752010-08-24 06:29:42 +00001616 ExprResult Collection;
Richard Smith8baa5002018-09-28 18:44:09 +00001617 ForRangeInfo ForRangeInfo;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001618 FullExprArg ThirdPart(Actions);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001619
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001620 if (Tok.is(tok::code_completion)) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001621 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001622 C99orCXXorObjC? Sema::PCC_ForInit
1623 : Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001624 cutOffParsing();
1625 return StmtError();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001626 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001627
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001628 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001629 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001630
Roman Lebedev377748f2018-11-20 18:59:05 +00001631 SourceLocation EmptyInitStmtSemiLoc;
1632
Chris Lattner9075bd72006-08-10 04:59:57 +00001633 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001634 if (Tok.is(tok::semi)) { // for (;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001635 ProhibitAttributes(attrs);
Chris Lattner53361ac2006-08-10 05:19:57 +00001636 // no first part, eat the ';'.
Roman Lebedev377748f2018-11-20 18:59:05 +00001637 SourceLocation SemiLoc = Tok.getLocation();
1638 if (!Tok.hasLeadingEmptyMacro() && !SemiLoc.isMacroID())
1639 EmptyInitStmtSemiLoc = SemiLoc;
Chris Lattner53361ac2006-08-10 05:19:57 +00001640 ConsumeToken();
Richard Smith955bf012014-06-19 11:42:00 +00001641 } else if (getLangOpts().CPlusPlus && Tok.is(tok::identifier) &&
1642 isForRangeIdentifier()) {
1643 ProhibitAttributes(attrs);
1644 IdentifierInfo *Name = Tok.getIdentifierInfo();
1645 SourceLocation Loc = ConsumeToken();
1646 MaybeParseCXX11Attributes(attrs);
1647
Richard Smith8baa5002018-09-28 18:44:09 +00001648 ForRangeInfo.ColonLoc = ConsumeToken();
Richard Smith955bf012014-06-19 11:42:00 +00001649 if (Tok.is(tok::l_brace))
Richard Smith8baa5002018-09-28 18:44:09 +00001650 ForRangeInfo.RangeExpr = ParseBraceInitializer();
Richard Smith955bf012014-06-19 11:42:00 +00001651 else
Richard Smith8baa5002018-09-28 18:44:09 +00001652 ForRangeInfo.RangeExpr = ParseExpression();
Richard Smith955bf012014-06-19 11:42:00 +00001653
Richard Smith83d3f152014-11-27 01:54:27 +00001654 Diag(Loc, diag::err_for_range_identifier)
Aaron Ballmanc351fba2017-12-04 20:27:34 +00001655 << ((getLangOpts().CPlusPlus11 && !getLangOpts().CPlusPlus17)
Richard Smith955bf012014-06-19 11:42:00 +00001656 ? FixItHint::CreateInsertion(Loc, "auto &&")
1657 : FixItHint());
1658
Richard Smith8baa5002018-09-28 18:44:09 +00001659 ForRangeInfo.LoopVar = Actions.ActOnCXXForRangeIdentifier(
1660 getCurScope(), Loc, Name, attrs, attrs.Range.getEnd());
Eli Friedman0ffc31c2011-12-20 01:50:37 +00001661 } else if (isForInitDeclaration()) { // for (int X = 4;
Richard Smithbf5bcf22018-06-26 23:20:26 +00001662 ParenBraceBracketBalancer BalancerRAIIObj(*this);
1663
Chris Lattner53361ac2006-08-10 05:19:57 +00001664 // Parse declaration, which eats the ';'.
George Burgess IV4d456452018-06-28 21:36:00 +00001665 if (!C99orCXXorObjC) { // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001666 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
George Burgess IV4d456452018-06-28 21:36:00 +00001667 Diag(Tok, diag::warn_gcc_variable_decl_in_for_loop);
1668 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001669
Richard Smith02e85f32011-04-14 22:09:26 +00001670 // In C++0x, "for (T NS:a" might not be a typo for ::
David Blaikiebbafb8a2012-03-11 07:00:24 +00001671 bool MightBeForRangeStmt = getLangOpts().CPlusPlus;
Richard Smith02e85f32011-04-14 22:09:26 +00001672 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1673
Chris Lattner49836b42009-04-02 04:16:50 +00001674 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00001675 DeclGroupPtrTy DG = ParseSimpleDeclaration(
Faisal Vali421b2d12017-12-29 05:41:00 +00001676 DeclaratorContext::ForContext, DeclEnd, attrs, false,
Richard Smith8baa5002018-09-28 18:44:09 +00001677 MightBeForRangeStmt ? &ForRangeInfo : nullptr);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001678 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Richard Smith8baa5002018-09-28 18:44:09 +00001679 if (ForRangeInfo.ParsedForRangeDecl()) {
1680 Diag(ForRangeInfo.ColonLoc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00001681 diag::warn_cxx98_compat_for_range : diag::ext_for_range);
Richard Smith8baa5002018-09-28 18:44:09 +00001682 ForRangeInfo.LoopVar = FirstPart;
1683 FirstPart = StmtResult();
Richard Smith02e85f32011-04-14 22:09:26 +00001684 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattner32dc41c2009-03-29 17:27:48 +00001685 ConsumeToken();
1686 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001687 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001688 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001689 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001690
Douglas Gregor68762e72010-08-23 21:17:50 +00001691 if (Tok.is(tok::code_completion)) {
1692 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001693 cutOffParsing();
1694 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001695 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001696 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001697 } else {
1698 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001699 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001700 } else {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001701 ProhibitAttributes(attrs);
Kaelyn Takatab16e6322014-11-20 22:06:40 +00001702 Value = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Chris Lattner71e23ce2006-11-04 20:18:38 +00001703
John McCall34376a62010-12-04 03:47:34 +00001704 ForEach = isTokIdentifier_in();
1705
Chris Lattnercd68f642007-06-27 01:06:29 +00001706 // Turn the expression into a stmt.
John McCall34376a62010-12-04 03:47:34 +00001707 if (!Value.isInvalid()) {
1708 if (ForEach)
1709 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00001710 else {
1711 // We already know this is not an init-statement within a for loop, so
1712 // if we are parsing a C++11 range-based for loop, we should treat this
1713 // expression statement as being a discarded value expression because
1714 // we will err below. This way we do not warn on an unused expression
1715 // that was an error in the first place, like with: for (expr : expr);
1716 bool IsRangeBasedFor =
1717 getLangOpts().CPlusPlus11 && !ForEach && Tok.is(tok::colon);
1718 FirstPart = Actions.ActOnExprStmt(Value, !IsRangeBasedFor);
1719 }
John McCall34376a62010-12-04 03:47:34 +00001720 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001721
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001722 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001723 ConsumeToken();
John McCall34376a62010-12-04 03:47:34 +00001724 } else if (ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001725 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001726
Douglas Gregor68762e72010-08-23 21:17:50 +00001727 if (Tok.is(tok::code_completion)) {
David Blaikie0403cb12016-01-15 23:43:25 +00001728 Actions.CodeCompleteObjCForCollection(getCurScope(), nullptr);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001729 cutOffParsing();
1730 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001731 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001732 Collection = ParseExpression();
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001733 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::colon) && FirstPart.get()) {
Richard Smith4f848f12011-12-20 22:56:20 +00001734 // User tried to write the reasonable, but ill-formed, for-range-statement
1735 // for (expr : expr) { ... }
1736 Diag(Tok, diag::err_for_range_expected_decl)
1737 << FirstPart.get()->getSourceRange();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001738 SkipUntil(tok::r_paren, StopBeforeMatch);
Richard Smith03a4aa32016-06-23 19:02:52 +00001739 SecondPart = Sema::ConditionError();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001740 } else {
Douglas Gregor230a7e62011-02-17 03:38:46 +00001741 if (!Value.isInvalid()) {
1742 Diag(Tok, diag::err_expected_semi_for);
1743 } else {
1744 // Skip until semicolon or rparen, don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001745 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor230a7e62011-02-17 03:38:46 +00001746 if (Tok.is(tok::semi))
1747 ConsumeToken();
1748 }
Chris Lattner53361ac2006-08-10 05:19:57 +00001749 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001750 }
Serge Pavlov09f99242014-01-23 15:05:00 +00001751
1752 // Parse the second part of the for specifier.
1753 getCurScope()->AddFlags(Scope::BreakScope | Scope::ContinueScope);
Richard Smith8baa5002018-09-28 18:44:09 +00001754 if (!ForEach && !ForRangeInfo.ParsedForRangeDecl() &&
1755 !SecondPart.isInvalid()) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001756 // Parse the second part of the for specifier.
1757 if (Tok.is(tok::semi)) { // for (...;;
1758 // no second part.
Douglas Gregor230a7e62011-02-17 03:38:46 +00001759 } else if (Tok.is(tok::r_paren)) {
1760 // missing both semicolons.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001761 } else {
Richard Smith8baa5002018-09-28 18:44:09 +00001762 if (getLangOpts().CPlusPlus) {
1763 // C++2a: We've parsed an init-statement; we might have a
1764 // for-range-declaration next.
1765 bool MightBeForRangeStmt = !ForRangeInfo.ParsedForRangeDecl();
1766 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
Richard Smithc7a05a92016-06-29 21:17:59 +00001767 SecondPart =
Richard Smith8baa5002018-09-28 18:44:09 +00001768 ParseCXXCondition(nullptr, ForLoc, Sema::ConditionKind::Boolean,
1769 MightBeForRangeStmt ? &ForRangeInfo : nullptr);
1770
1771 if (ForRangeInfo.ParsedForRangeDecl()) {
1772 Diag(FirstPart.get() ? FirstPart.get()->getBeginLoc()
1773 : ForRangeInfo.ColonLoc,
1774 getLangOpts().CPlusPlus2a
1775 ? diag::warn_cxx17_compat_for_range_init_stmt
1776 : diag::ext_for_range_init_stmt)
1777 << (FirstPart.get() ? FirstPart.get()->getSourceRange()
1778 : SourceRange());
Roman Lebedev377748f2018-11-20 18:59:05 +00001779 if (EmptyInitStmtSemiLoc.isValid()) {
1780 Diag(EmptyInitStmtSemiLoc, diag::warn_empty_init_statement)
1781 << /*for-loop*/ 2
1782 << FixItHint::CreateRemoval(EmptyInitStmtSemiLoc);
1783 }
Richard Smith8baa5002018-09-28 18:44:09 +00001784 }
1785 } else {
Richard Smith03a4aa32016-06-23 19:02:52 +00001786 ExprResult SecondExpr = ParseExpression();
1787 if (SecondExpr.isInvalid())
1788 SecondPart = Sema::ConditionError();
1789 else
1790 SecondPart =
1791 Actions.ActOnCondition(getCurScope(), ForLoc, SecondExpr.get(),
1792 Sema::ConditionKind::Boolean);
Douglas Gregore60e41a2010-05-06 17:25:47 +00001793 }
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001794 }
Richard Smith8baa5002018-09-28 18:44:09 +00001795 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001796
Richard Smith8baa5002018-09-28 18:44:09 +00001797 // Parse the third part of the for statement.
1798 if (!ForEach && !ForRangeInfo.ParsedForRangeDecl()) {
Douglas Gregor230a7e62011-02-17 03:38:46 +00001799 if (Tok.isNot(tok::semi)) {
Richard Smith03a4aa32016-06-23 19:02:52 +00001800 if (!SecondPart.isInvalid())
Douglas Gregor230a7e62011-02-17 03:38:46 +00001801 Diag(Tok, diag::err_expected_semi_for);
1802 else
1803 // Skip until semicolon or rparen, don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001804 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor230a7e62011-02-17 03:38:46 +00001805 }
1806
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001807 if (Tok.is(tok::semi)) {
1808 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001809 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001810
Douglas Gregore60e41a2010-05-06 17:25:47 +00001811 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001812 ExprResult Third = ParseExpression();
Richard Smith945f8d32013-01-14 22:39:08 +00001813 // FIXME: The C++11 standard doesn't actually say that this is a
1814 // discarded-value expression, but it clearly should be.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001815 ThirdPart = Actions.MakeFullDiscardedValueExpr(Third.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001816 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001817 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001818 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001819 T.consumeClose();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001820
Richard Smith0e304ea2015-10-22 04:46:14 +00001821 // C++ Coroutines [stmt.iter]:
1822 // 'co_await' can only be used for a range-based for statement.
Richard Smith8baa5002018-09-28 18:44:09 +00001823 if (CoawaitLoc.isValid() && !ForRangeInfo.ParsedForRangeDecl()) {
Richard Smith0e304ea2015-10-22 04:46:14 +00001824 Diag(CoawaitLoc, diag::err_for_co_await_not_range_for);
1825 CoawaitLoc = SourceLocation();
1826 }
1827
Richard Smith02e85f32011-04-14 22:09:26 +00001828 // We need to perform most of the semantic analysis for a C++0x for-range
1829 // statememt before parsing the body, in order to be able to deduce the type
1830 // of an auto-typed loop variable.
1831 StmtResult ForRangeStmt;
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001832 StmtResult ForEachStmt;
Chad Rosier67055f52012-07-10 21:35:27 +00001833
Richard Smith8baa5002018-09-28 18:44:09 +00001834 if (ForRangeInfo.ParsedForRangeDecl()) {
Denis Zobnin7d6b9242016-02-02 17:33:09 +00001835 ExprResult CorrectedRange =
Richard Smith8baa5002018-09-28 18:44:09 +00001836 Actions.CorrectDelayedTyposInExpr(ForRangeInfo.RangeExpr.get());
Richard Smith9f690bd2015-10-27 06:02:45 +00001837 ForRangeStmt = Actions.ActOnCXXForRangeStmt(
1838 getCurScope(), ForLoc, CoawaitLoc, FirstPart.get(),
Richard Smith8baa5002018-09-28 18:44:09 +00001839 ForRangeInfo.LoopVar.get(), ForRangeInfo.ColonLoc, CorrectedRange.get(),
Richard Smith9f690bd2015-10-27 06:02:45 +00001840 T.getCloseLocation(), Sema::BFRK_Build);
John McCall53848232011-07-27 01:07:15 +00001841
1842 // Similarly, we need to do the semantic analysis for a for-range
1843 // statement immediately in order to close over temporaries correctly.
1844 } else if (ForEach) {
Sam Panzer2c4ca0f2012-08-16 21:47:25 +00001845 ForEachStmt = Actions.ActOnObjCForCollectionStmt(ForLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001846 FirstPart.get(),
1847 Collection.get(),
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001848 T.getCloseLocation());
Alexey Bataev9c821032015-04-30 04:23:23 +00001849 } else {
1850 // In OpenMP loop region loop control variable must be captured and be
1851 // private. Perform analysis of first part (if any).
1852 if (getLangOpts().OpenMP && FirstPart.isUsable()) {
1853 Actions.ActOnOpenMPLoopInitialization(ForLoc, FirstPart.get());
1854 }
John McCall53848232011-07-27 01:07:15 +00001855 }
1856
Justin Bognere4ebb6c2013-12-03 07:36:55 +00001857 // C99 6.8.5p5 - In C99, the body of the for statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001858 // there is no compound stmt. C90 does not have this clause. We only do this
1859 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001860 //
1861 // C++ 6.5p2:
1862 // The substatement in an iteration-statement implicitly defines a local scope
1863 // which is entered and exited each time through the loop.
1864 //
1865 // See comments in ParseIfStatement for why we create a scope for
1866 // for-init-statement/condition and a new scope for substatement in C++.
1867 //
David Majnemer2206bf52014-03-05 08:57:59 +00001868 ParseScope InnerScope(this, Scope::DeclScope, C99orCXXorObjC,
1869 Tok.is(tok::l_brace));
1870
1871 // The body of the for loop has the same local mangling number as the
1872 // for-init-statement.
1873 // It will only be incremented if the body contains other things that would
1874 // normally increment the mangling number (like a compound statement).
1875 if (C99orCXXorObjC)
David Majnemera7f8c462015-03-19 21:54:30 +00001876 getCurScope()->decrementMSManglingNumber();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001877
Chris Lattner9075bd72006-08-10 04:59:57 +00001878 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001879 StmtResult Body(ParseStatement(TrailingElseLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001880
Chris Lattner8fb26252007-08-22 05:28:50 +00001881 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001882 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001883
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001884 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001885 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001886
1887 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001888 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001889
Richard Smith02e85f32011-04-14 22:09:26 +00001890 if (ForEach)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001891 return Actions.FinishObjCForCollectionStmt(ForEachStmt.get(),
1892 Body.get());
Mike Stump11289f42009-09-09 15:08:12 +00001893
Richard Smith8baa5002018-09-28 18:44:09 +00001894 if (ForRangeInfo.ParsedForRangeDecl())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001895 return Actions.FinishCXXForRangeStmt(ForRangeStmt.get(), Body.get());
Richard Smith02e85f32011-04-14 22:09:26 +00001896
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001897 return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.get(),
Richard Smith03a4aa32016-06-23 19:02:52 +00001898 SecondPart, ThirdPart, T.getCloseLocation(),
1899 Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001900}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001901
Chris Lattner503fadc2006-08-10 05:45:44 +00001902/// ParseGotoStatement
1903/// jump-statement:
1904/// 'goto' identifier ';'
1905/// [GNU] 'goto' '*' expression ';'
1906///
1907/// Note: this lets the caller parse the end ';'.
1908///
Richard Smithc202b282012-04-14 00:33:13 +00001909StmtResult Parser::ParseGotoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001910 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001911 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001912
John McCalldadc5752010-08-24 06:29:42 +00001913 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001914 if (Tok.is(tok::identifier)) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +00001915 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1916 Tok.getLocation());
1917 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Chris Lattner503fadc2006-08-10 05:45:44 +00001918 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001919 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001920 // GNU indirect goto extension.
1921 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001922 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001923 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001924 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001925 SkipUntil(tok::semi, StopBeforeMatch);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001926 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001927 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001928 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.get());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001929 } else {
Alp Tokerec543272013-12-24 09:48:30 +00001930 Diag(Tok, diag::err_expected) << tok::identifier;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001931 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001932 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001933
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001934 return Res;
Chris Lattner503fadc2006-08-10 05:45:44 +00001935}
1936
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001937/// ParseContinueStatement
1938/// jump-statement:
1939/// 'continue' ';'
1940///
1941/// Note: this lets the caller parse the end ';'.
1942///
Richard Smithc202b282012-04-14 00:33:13 +00001943StmtResult Parser::ParseContinueStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001944 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001945 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001946}
1947
1948/// ParseBreakStatement
1949/// jump-statement:
1950/// 'break' ';'
1951///
1952/// Note: this lets the caller parse the end ';'.
1953///
Richard Smithc202b282012-04-14 00:33:13 +00001954StmtResult Parser::ParseBreakStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001955 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001956 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001957}
1958
Chris Lattner503fadc2006-08-10 05:45:44 +00001959/// ParseReturnStatement
1960/// jump-statement:
1961/// 'return' expression[opt] ';'
Richard Smith0e304ea2015-10-22 04:46:14 +00001962/// 'return' braced-init-list ';'
1963/// 'co_return' expression[opt] ';'
1964/// 'co_return' braced-init-list ';'
Richard Smithc202b282012-04-14 00:33:13 +00001965StmtResult Parser::ParseReturnStatement() {
Richard Smith0e304ea2015-10-22 04:46:14 +00001966 assert((Tok.is(tok::kw_return) || Tok.is(tok::kw_co_return)) &&
1967 "Not a return stmt!");
1968 bool IsCoreturn = Tok.is(tok::kw_co_return);
Chris Lattneraf635312006-10-16 06:06:51 +00001969 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001970
John McCalldadc5752010-08-24 06:29:42 +00001971 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001972 if (Tok.isNot(tok::semi)) {
Ilya Biryukov4f9543b2019-01-31 20:20:32 +00001973 if (!IsCoreturn)
1974 PreferredType.enterReturn(Actions, Tok.getLocation());
Richard Smith0e304ea2015-10-22 04:46:14 +00001975 // FIXME: Code completion for co_return.
1976 if (Tok.is(tok::code_completion) && !IsCoreturn) {
Ilya Biryukov4f9543b2019-01-31 20:20:32 +00001977 Actions.CodeCompleteExpression(getCurScope(),
1978 PreferredType.get(Tok.getLocation()));
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001979 cutOffParsing();
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001980 return StmtError();
1981 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001982
David Blaikiebbafb8a2012-03-11 07:00:24 +00001983 if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus) {
Douglas Gregore9e27d92011-03-11 23:10:44 +00001984 R = ParseInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00001985 if (R.isUsable())
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001986 Diag(R.get()->getBeginLoc(),
1987 getLangOpts().CPlusPlus11
1988 ? diag::warn_cxx98_compat_generalized_initializer_lists
1989 : diag::ext_generalized_initializer_lists)
1990 << R.get()->getSourceRange();
Douglas Gregore9e27d92011-03-11 23:10:44 +00001991 } else
Nico Weber3ce01c32015-01-04 08:07:54 +00001992 R = ParseExpression();
Serge Pavlovf79bd5c2013-12-04 03:51:59 +00001993 if (R.isInvalid()) {
1994 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001995 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001996 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001997 }
Richard Smithcfd53b42015-10-22 06:13:50 +00001998 if (IsCoreturn)
Eric Fiselier20f25cb2017-03-06 23:38:15 +00001999 return Actions.ActOnCoreturnStmt(getCurScope(), ReturnLoc, R.get());
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002000 return Actions.ActOnReturnStmt(ReturnLoc, R.get(), getCurScope());
Chris Lattner503fadc2006-08-10 05:45:44 +00002001}
Chris Lattner0116c472006-08-15 06:03:28 +00002002
Alexey Bataevc4fad652016-01-13 11:18:54 +00002003StmtResult Parser::ParsePragmaLoopHint(StmtVector &Stmts,
Jonathan Roelofsce1db6d2017-03-14 17:29:33 +00002004 AllowedConstructsKind Allowed,
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00002005 SourceLocation *TrailingElseLoc,
2006 ParsedAttributesWithRange &Attrs) {
2007 // Create temporary attribute list.
2008 ParsedAttributesWithRange TempAttrs(AttrFactory);
2009
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002010 // Get loop hints and consume annotated token.
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00002011 while (Tok.is(tok::annot_pragma_loop_hint)) {
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002012 LoopHint Hint;
2013 if (!HandlePragmaLoopHint(Hint))
2014 continue;
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00002015
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002016 ArgsUnion ArgHints[] = {Hint.PragmaNameLoc, Hint.OptionLoc, Hint.StateLoc,
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00002017 ArgsUnion(Hint.ValueExpr)};
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002018 TempAttrs.addNew(Hint.PragmaNameLoc->Ident, Hint.Range, nullptr,
2019 Hint.PragmaNameLoc->Loc, ArgHints, 4,
Erich Keanee891aa92018-07-13 15:07:47 +00002020 ParsedAttr::AS_Pragma);
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00002021 }
2022
2023 // Get the next statement.
2024 MaybeParseCXX11Attributes(Attrs);
2025
2026 StmtResult S = ParseStatementOrDeclarationAfterAttributes(
Alexey Bataevc4fad652016-01-13 11:18:54 +00002027 Stmts, Allowed, TrailingElseLoc, Attrs);
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00002028
2029 Attrs.takeAllFrom(TempAttrs);
2030 return S;
2031}
2032
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002033Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00002034 assert(Tok.is(tok::l_brace));
2035 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00002036
Jordan Rose1e879d82018-03-23 00:07:18 +00002037 PrettyDeclStackTraceEntry CrashInfo(Actions.Context, Decl, LBraceLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002038 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00002039
Alexey Bataev3d42f342015-11-20 07:02:57 +00002040 // Save and reset current vtordisp stack if we have entered a C++ method body.
2041 bool IsCXXMethod =
2042 getLangOpts().CPlusPlus && Decl && isa<CXXMethodDecl>(Decl);
Denis Zobnin2290dac2016-04-29 11:27:00 +00002043 Sema::PragmaStackSentinelRAII
2044 PragmaStackSentinel(Actions, "InternalPragmaState", IsCXXMethod);
Alexey Bataev3d42f342015-11-20 07:02:57 +00002045
Fariborz Jahanian8e632942007-11-08 19:01:26 +00002046 // Do not enter a scope for the brace, as the arguments are in the same scope
2047 // (the function body) as the body itself. Instead, just read the statement
2048 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00002049 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00002050
Fariborz Jahanian8e632942007-11-08 19:01:26 +00002051 // If the function body could not be parsed, make a bogus compoundstmt.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002052 if (FnBody.isInvalid()) {
2053 Sema::CompoundScopeRAII CompoundScope(Actions);
Robert Wilhelm27b2c9a32013-08-19 20:51:20 +00002054 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002055 }
Sebastian Redl042ad952008-12-11 19:30:53 +00002056
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002057 BodyScope.Exit();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002058 return Actions.ActOnFinishFunctionBody(Decl, FnBody.get());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00002059}
Sebastian Redlb219c902008-12-21 16:41:36 +00002060
Sebastian Redla7b98a72009-04-26 20:35:05 +00002061/// ParseFunctionTryBlock - Parse a C++ function-try-block.
2062///
2063/// function-try-block:
2064/// 'try' ctor-initializer[opt] compound-statement handler-seq
2065///
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002066Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00002067 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2068 SourceLocation TryLoc = ConsumeToken();
2069
Jordan Rose1e879d82018-03-23 00:07:18 +00002070 PrettyDeclStackTraceEntry CrashInfo(Actions.Context, Decl, TryLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002071 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00002072
2073 // Constructor initializer list?
2074 if (Tok.is(tok::colon))
2075 ParseConstructorInitializer(Decl);
Douglas Gregor5ca153f2011-09-07 20:36:12 +00002076 else
2077 Actions.ActOnDefaultCtorInitializers(Decl);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00002078
Alexey Bataev3d42f342015-11-20 07:02:57 +00002079 // Save and reset current vtordisp stack if we have entered a C++ method body.
2080 bool IsCXXMethod =
2081 getLangOpts().CPlusPlus && Decl && isa<CXXMethodDecl>(Decl);
Denis Zobnin2290dac2016-04-29 11:27:00 +00002082 Sema::PragmaStackSentinelRAII
2083 PragmaStackSentinel(Actions, "InternalPragmaState", IsCXXMethod);
Alexey Bataev3d42f342015-11-20 07:02:57 +00002084
Sebastian Redld98ecd62009-04-26 21:08:36 +00002085 SourceLocation LBraceLoc = Tok.getLocation();
David Blaikie1c9c9042012-11-10 01:04:23 +00002086 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc, /*FnTry*/true));
Sebastian Redla7b98a72009-04-26 20:35:05 +00002087 // If we failed to parse the try-catch, we just give the function an empty
2088 // compound statement as the body.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002089 if (FnBody.isInvalid()) {
2090 Sema::CompoundScopeRAII CompoundScope(Actions);
Robert Wilhelm27b2c9a32013-08-19 20:51:20 +00002091 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002092 }
Sebastian Redla7b98a72009-04-26 20:35:05 +00002093
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002094 BodyScope.Exit();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002095 return Actions.ActOnFinishFunctionBody(Decl, FnBody.get());
Sebastian Redla7b98a72009-04-26 20:35:05 +00002096}
2097
Erik Verbruggen6e922512012-04-12 10:11:59 +00002098bool Parser::trySkippingFunctionBody() {
Erik Verbruggen6e922512012-04-12 10:11:59 +00002099 assert(SkipFunctionBodies &&
2100 "Should only be called when SkipFunctionBodies is enabled");
Argyrios Kyrtzidis289e4a32012-10-31 17:29:28 +00002101 if (!PP.isCodeCompletionEnabled()) {
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002102 SkipFunctionBody();
Argyrios Kyrtzidis289e4a32012-10-31 17:29:28 +00002103 return true;
2104 }
2105
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002106 // We're in code-completion mode. Skip parsing for all function bodies unless
2107 // the body contains the code-completion point.
2108 TentativeParsingAction PA(*this);
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002109 bool IsTryCatch = Tok.is(tok::kw_try);
2110 CachedTokens Toks;
2111 bool ErrorInPrologue = ConsumeAndStoreFunctionPrologue(Toks);
2112 if (llvm::any_of(Toks, [](const Token &Tok) {
2113 return Tok.is(tok::code_completion);
2114 })) {
2115 PA.Revert();
2116 return false;
2117 }
2118 if (ErrorInPrologue) {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002119 PA.Commit();
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002120 SkipMalformedDecl();
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002121 return true;
2122 }
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002123 if (!SkipUntil(tok::r_brace, StopAtCodeCompletion)) {
2124 PA.Revert();
2125 return false;
2126 }
2127 while (IsTryCatch && Tok.is(tok::kw_catch)) {
2128 if (!SkipUntil(tok::l_brace, StopAtCodeCompletion) ||
2129 !SkipUntil(tok::r_brace, StopAtCodeCompletion)) {
2130 PA.Revert();
2131 return false;
2132 }
2133 }
2134 PA.Commit();
2135 return true;
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002136}
2137
Sebastian Redlb219c902008-12-21 16:41:36 +00002138/// ParseCXXTryBlock - Parse a C++ try-block.
2139///
2140/// try-block:
2141/// 'try' compound-statement handler-seq
2142///
Richard Smithc202b282012-04-14 00:33:13 +00002143StmtResult Parser::ParseCXXTryBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00002144 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2145
2146 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00002147 return ParseCXXTryBlockCommon(TryLoc);
2148}
2149
2150/// ParseCXXTryBlockCommon - Parse the common part of try-block and
2151/// function-try-block.
2152///
2153/// try-block:
2154/// 'try' compound-statement handler-seq
2155///
2156/// function-try-block:
2157/// 'try' ctor-initializer[opt] compound-statement handler-seq
2158///
2159/// handler-seq:
2160/// handler handler-seq[opt]
2161///
John Wiegley1c0675e2011-04-28 01:08:34 +00002162/// [Borland] try-block:
2163/// 'try' compound-statement seh-except-block
Alp Tokerf6a24ce2013-12-05 16:25:25 +00002164/// 'try' compound-statement seh-finally-block
John Wiegley1c0675e2011-04-28 01:08:34 +00002165///
David Blaikie1c9c9042012-11-10 01:04:23 +00002166StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc, bool FnTry) {
Sebastian Redlb219c902008-12-21 16:41:36 +00002167 if (Tok.isNot(tok::l_brace))
Alp Tokerec543272013-12-24 09:48:30 +00002168 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
Richard Smithc202b282012-04-14 00:33:13 +00002169
Momchil Velikov57c681f2017-08-10 15:43:06 +00002170 StmtResult TryBlock(ParseCompoundStatement(
2171 /*isStmtExpr=*/false, Scope::DeclScope | Scope::TryScope |
2172 Scope::CompoundStmtScope |
2173 (FnTry ? Scope::FnTryCatchScope : 0)));
Sebastian Redlb219c902008-12-21 16:41:36 +00002174 if (TryBlock.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002175 return TryBlock;
Sebastian Redlb219c902008-12-21 16:41:36 +00002176
John Wiegley1c0675e2011-04-28 01:08:34 +00002177 // Borland allows SEH-handlers with 'try'
Chad Rosier67055f52012-07-10 21:35:27 +00002178
Richard Smithc202b282012-04-14 00:33:13 +00002179 if ((Tok.is(tok::identifier) &&
2180 Tok.getIdentifierInfo() == getSEHExceptKeyword()) ||
2181 Tok.is(tok::kw___finally)) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002182 // TODO: Factor into common return ParseSEHHandlerCommon(...)
2183 StmtResult Handler;
Douglas Gregor60060d62011-10-21 03:57:52 +00002184 if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002185 SourceLocation Loc = ConsumeToken();
2186 Handler = ParseSEHExceptBlock(Loc);
2187 }
2188 else {
2189 SourceLocation Loc = ConsumeToken();
2190 Handler = ParseSEHFinallyBlock(Loc);
2191 }
2192 if(Handler.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002193 return Handler;
John McCall53fa7142010-12-24 02:08:15 +00002194
John Wiegley1c0675e2011-04-28 01:08:34 +00002195 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
2196 TryLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002197 TryBlock.get(),
Warren Huntf6be4cb2014-07-25 20:52:51 +00002198 Handler.get());
Sebastian Redlb219c902008-12-21 16:41:36 +00002199 }
John Wiegley1c0675e2011-04-28 01:08:34 +00002200 else {
Benjamin Kramerf0623432012-08-23 22:51:59 +00002201 StmtVector Handlers;
Richard Smithc2c8bb82013-10-15 01:34:54 +00002202
2203 // C++11 attributes can't appear here, despite this context seeming
2204 // statement-like.
2205 DiagnoseAndSkipCXX11Attributes();
Sebastian Redlb219c902008-12-21 16:41:36 +00002206
John Wiegley1c0675e2011-04-28 01:08:34 +00002207 if (Tok.isNot(tok::kw_catch))
2208 return StmtError(Diag(Tok, diag::err_expected_catch));
2209 while (Tok.is(tok::kw_catch)) {
David Blaikie1c9c9042012-11-10 01:04:23 +00002210 StmtResult Handler(ParseCXXCatchBlock(FnTry));
John Wiegley1c0675e2011-04-28 01:08:34 +00002211 if (!Handler.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002212 Handlers.push_back(Handler.get());
John Wiegley1c0675e2011-04-28 01:08:34 +00002213 }
2214 // Don't bother creating the full statement if we don't have any usable
2215 // handlers.
2216 if (Handlers.empty())
2217 return StmtError();
2218
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002219 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.get(), Handlers);
John Wiegley1c0675e2011-04-28 01:08:34 +00002220 }
Sebastian Redlb219c902008-12-21 16:41:36 +00002221}
2222
2223/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
2224///
Richard Smith1dba27c2013-01-29 09:02:09 +00002225/// handler:
2226/// 'catch' '(' exception-declaration ')' compound-statement
Sebastian Redlb219c902008-12-21 16:41:36 +00002227///
Richard Smith1dba27c2013-01-29 09:02:09 +00002228/// exception-declaration:
2229/// attribute-specifier-seq[opt] type-specifier-seq declarator
2230/// attribute-specifier-seq[opt] type-specifier-seq abstract-declarator[opt]
2231/// '...'
Sebastian Redlb219c902008-12-21 16:41:36 +00002232///
David Blaikie1c9c9042012-11-10 01:04:23 +00002233StmtResult Parser::ParseCXXCatchBlock(bool FnCatch) {
Sebastian Redlb219c902008-12-21 16:41:36 +00002234 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
2235
2236 SourceLocation CatchLoc = ConsumeToken();
2237
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002238 BalancedDelimiterTracker T(*this, tok::l_paren);
Alp Toker383d2c42014-01-01 03:08:43 +00002239 if (T.expectAndConsume())
Sebastian Redlb219c902008-12-21 16:41:36 +00002240 return StmtError();
2241
2242 // C++ 3.3.2p3:
2243 // The name in a catch exception-declaration is local to the handler and
2244 // shall not be redeclared in the outermost block of the handler.
David Blaikie1c9c9042012-11-10 01:04:23 +00002245 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope |
David Blaikie3403feb2012-11-13 18:51:45 +00002246 (FnCatch ? Scope::FnTryCatchScope : 0));
Sebastian Redlb219c902008-12-21 16:41:36 +00002247
2248 // exception-declaration is equivalent to '...' or a parameter-declaration
2249 // without default arguments.
Craig Topper161e4db2014-05-21 06:02:52 +00002250 Decl *ExceptionDecl = nullptr;
Sebastian Redlb219c902008-12-21 16:41:36 +00002251 if (Tok.isNot(tok::ellipsis)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002252 ParsedAttributesWithRange Attributes(AttrFactory);
2253 MaybeParseCXX11Attributes(Attributes);
2254
John McCall084e83d2011-03-24 11:26:52 +00002255 DeclSpec DS(AttrFactory);
Richard Smith1dba27c2013-01-29 09:02:09 +00002256 DS.takeAttributesFrom(Attributes);
2257
Sebastian Redl54c04d42008-12-22 19:15:10 +00002258 if (ParseCXXTypeSpecifierSeq(DS))
2259 return StmtError();
Richard Smith1dba27c2013-01-29 09:02:09 +00002260
Faisal Vali421b2d12017-12-29 05:41:00 +00002261 Declarator ExDecl(DS, DeclaratorContext::CXXCatchContext);
Sebastian Redlb219c902008-12-21 16:41:36 +00002262 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002263 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00002264 } else
2265 ConsumeToken();
2266
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002267 T.consumeClose();
2268 if (T.getCloseLocation().isInvalid())
Sebastian Redlb219c902008-12-21 16:41:36 +00002269 return StmtError();
2270
2271 if (Tok.isNot(tok::l_brace))
Alp Tokerec543272013-12-24 09:48:30 +00002272 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
Sebastian Redlb219c902008-12-21 16:41:36 +00002273
Alexis Hunt96d5c762009-11-21 08:43:09 +00002274 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
Richard Smithc202b282012-04-14 00:33:13 +00002275 StmtResult Block(ParseCompoundStatement());
Sebastian Redlb219c902008-12-21 16:41:36 +00002276 if (Block.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002277 return Block;
Sebastian Redlb219c902008-12-21 16:41:36 +00002278
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002279 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.get());
Sebastian Redlb219c902008-12-21 16:41:36 +00002280}
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002281
2282void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Douglas Gregor43edb322011-10-24 22:31:10 +00002283 IfExistsCondition Result;
Francois Picheta5b3fcb2011-05-07 17:30:27 +00002284 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002285 return;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00002286
Douglas Gregor43edb322011-10-24 22:31:10 +00002287 // Handle dependent statements by parsing the braces as a compound statement.
2288 // This is not the same behavior as Visual C++, which don't treat this as a
2289 // compound statement, but for Clang's type checking we can't have anything
2290 // inside these braces escaping to the surrounding code.
2291 if (Result.Behavior == IEB_Dependent) {
2292 if (!Tok.is(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002293 Diag(Tok, diag::err_expected) << tok::l_brace;
Richard Smithc202b282012-04-14 00:33:13 +00002294 return;
Douglas Gregor43edb322011-10-24 22:31:10 +00002295 }
Richard Smithc202b282012-04-14 00:33:13 +00002296
2297 StmtResult Compound = ParseCompoundStatement();
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002298 if (Compound.isInvalid())
2299 return;
Richard Smithc202b282012-04-14 00:33:13 +00002300
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002301 StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc,
2302 Result.IsIfExists,
Richard Smithc202b282012-04-14 00:33:13 +00002303 Result.SS,
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002304 Result.Name,
2305 Compound.get());
2306 if (DepResult.isUsable())
2307 Stmts.push_back(DepResult.get());
Douglas Gregor43edb322011-10-24 22:31:10 +00002308 return;
2309 }
Richard Smithc202b282012-04-14 00:33:13 +00002310
Douglas Gregor43edb322011-10-24 22:31:10 +00002311 BalancedDelimiterTracker Braces(*this, tok::l_brace);
2312 if (Braces.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +00002313 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002314 return;
2315 }
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002316
Douglas Gregor43edb322011-10-24 22:31:10 +00002317 switch (Result.Behavior) {
2318 case IEB_Parse:
2319 // Parse the statements below.
2320 break;
Chad Rosier67055f52012-07-10 21:35:27 +00002321
Douglas Gregor43edb322011-10-24 22:31:10 +00002322 case IEB_Dependent:
2323 llvm_unreachable("Dependent case handled above");
Chad Rosier67055f52012-07-10 21:35:27 +00002324
Douglas Gregor43edb322011-10-24 22:31:10 +00002325 case IEB_Skip:
2326 Braces.skipToEnd();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002327 return;
2328 }
2329
2330 // Condition is true, parse the statements.
2331 while (Tok.isNot(tok::r_brace)) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00002332 StmtResult R = ParseStatementOrDeclaration(Stmts, ACK_Any);
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002333 if (R.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002334 Stmts.push_back(R.get());
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002335 }
Douglas Gregor43edb322011-10-24 22:31:10 +00002336 Braces.consumeClose();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002337}
Anastasia Stulova6bdbcbb2016-02-19 18:30:11 +00002338
2339bool Parser::ParseOpenCLUnrollHintAttribute(ParsedAttributes &Attrs) {
2340 MaybeParseGNUAttributes(Attrs);
2341
2342 if (Attrs.empty())
2343 return true;
2344
Erich Keanee891aa92018-07-13 15:07:47 +00002345 if (Attrs.begin()->getKind() != ParsedAttr::AT_OpenCLUnrollHint)
Anastasia Stulova6bdbcbb2016-02-19 18:30:11 +00002346 return true;
2347
2348 if (!(Tok.is(tok::kw_for) || Tok.is(tok::kw_while) || Tok.is(tok::kw_do))) {
2349 Diag(Tok, diag::err_opencl_unroll_hint_on_non_loop);
2350 return false;
2351 }
2352 return true;
2353}