blob: 9043ebb9a2b7129de8d693d0d0bfc1bcd24da8d7 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
Chris Lattner0ccd51e2006-08-09 05:47:47 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner0ccd51e2006-08-09 05:47:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner8a9a97a2009-12-10 00:21:05 +000015#include "RAIIObjectsForParser.h"
Aaron Ballmanb06b15a2014-06-06 12:40:24 +000016#include "clang/Basic/Attributes.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/Basic/PrettyStackTrace.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000018#include "clang/Parse/Parser.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/DeclSpec.h"
Aaron Ballmanb06b15a2014-06-06 12:40:24 +000020#include "clang/Sema/LoopHint.h"
John McCallfaf5fb42010-08-26 23:41:50 +000021#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall8b0666c2010-08-20 18:27:03 +000022#include "clang/Sema/Scope.h"
Richard Smith4f605af2012-08-18 00:55:03 +000023#include "clang/Sema/TypoCorrection.h"
Chris Lattner0ccd51e2006-08-09 05:47:47 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// C99 6.8: Statements and Blocks.
28//===----------------------------------------------------------------------===//
29
Richard Smith426a47b2013-10-28 22:04:30 +000030/// \brief Parse a standalone statement (for instance, as the body of an 'if',
31/// 'while', or 'for').
Alexey Bataevc4fad652016-01-13 11:18:54 +000032StmtResult Parser::ParseStatement(SourceLocation *TrailingElseLoc,
33 bool AllowOpenMPStandalone) {
Richard Smith426a47b2013-10-28 22:04:30 +000034 StmtResult Res;
35
36 // We may get back a null statement if we found a #pragma. Keep going until
37 // we get an actual statement.
38 do {
39 StmtVector Stmts;
Alexey Bataevc4fad652016-01-13 11:18:54 +000040 Res = ParseStatementOrDeclaration(
41 Stmts, AllowOpenMPStandalone ? ACK_StatementsOpenMPAnyExecutable
42 : ACK_StatementsOpenMPNonStandalone,
43 TrailingElseLoc);
Richard Smith426a47b2013-10-28 22:04:30 +000044 } while (!Res.isInvalid() && !Res.get());
45
46 return Res;
47}
48
Chris Lattner0ccd51e2006-08-09 05:47:47 +000049/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
50/// StatementOrDeclaration:
51/// statement
52/// declaration
53///
54/// statement:
55/// labeled-statement
56/// compound-statement
57/// expression-statement
58/// selection-statement
59/// iteration-statement
60/// jump-statement
Argyrios Kyrtzidisdee82912008-09-07 18:58:01 +000061/// [C++] declaration-statement
Sebastian Redlb219c902008-12-21 16:41:36 +000062/// [C++] try-block
John Wiegley1c0675e2011-04-28 01:08:34 +000063/// [MS] seh-try-block
Fariborz Jahanian90814572007-10-04 20:19:06 +000064/// [OBC] objc-throw-statement
65/// [OBC] objc-try-catch-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +000066/// [OBC] objc-synchronized-statement
Chris Lattner0116c472006-08-15 06:03:28 +000067/// [GNU] asm-statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +000068/// [OMP] openmp-construct [TODO]
69///
70/// labeled-statement:
71/// identifier ':' statement
72/// 'case' constant-expression ':' statement
73/// 'default' ':' statement
74///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000075/// selection-statement:
76/// if-statement
77/// switch-statement
78///
79/// iteration-statement:
80/// while-statement
81/// do-statement
82/// for-statement
83///
Chris Lattner9075bd72006-08-10 04:59:57 +000084/// expression-statement:
85/// expression[opt] ';'
86///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000087/// jump-statement:
88/// 'goto' identifier ';'
89/// 'continue' ';'
90/// 'break' ';'
91/// 'return' expression[opt] ';'
Chris Lattner503fadc2006-08-10 05:45:44 +000092/// [GNU] 'goto' '*' expression ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000093///
Fariborz Jahanian90814572007-10-04 20:19:06 +000094/// [OBC] objc-throw-statement:
95/// [OBC] '@' 'throw' expression ';'
Mike Stump11289f42009-09-09 15:08:12 +000096/// [OBC] '@' 'throw' ';'
97///
John McCalldadc5752010-08-24 06:29:42 +000098StmtResult
Alexey Bataevc4fad652016-01-13 11:18:54 +000099Parser::ParseStatementOrDeclaration(StmtVector &Stmts,
100 AllowedContsructsKind Allowed,
Nico Weber3cef1082011-12-22 23:26:17 +0000101 SourceLocation *TrailingElseLoc) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000102
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000103 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000104
Richard Smithc202b282012-04-14 00:33:13 +0000105 ParsedAttributesWithRange Attrs(AttrFactory);
Craig Topper161e4db2014-05-21 06:02:52 +0000106 MaybeParseCXX11Attributes(Attrs, nullptr, /*MightBeObjCMessageSend*/ true);
Anastasia Stulova6bdbcbb2016-02-19 18:30:11 +0000107 if (!MaybeParseOpenCLUnrollHintAttribute(Attrs))
108 return StmtError();
Richard Smithc202b282012-04-14 00:33:13 +0000109
Alexey Bataevc4fad652016-01-13 11:18:54 +0000110 StmtResult Res = ParseStatementOrDeclarationAfterAttributes(
111 Stmts, Allowed, TrailingElseLoc, Attrs);
Richard Smithc202b282012-04-14 00:33:13 +0000112
113 assert((Attrs.empty() || Res.isInvalid() || Res.isUsable()) &&
114 "attributes on empty statement");
115
116 if (Attrs.empty() || Res.isInvalid())
117 return Res;
118
119 return Actions.ProcessStmtAttributes(Res.get(), Attrs.getList(), Attrs.Range);
120}
121
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000122namespace {
123class StatementFilterCCC : public CorrectionCandidateCallback {
124public:
125 StatementFilterCCC(Token nextTok) : NextToken(nextTok) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000126 WantTypeSpecifiers = nextTok.isOneOf(tok::l_paren, tok::less, tok::l_square,
127 tok::identifier, tok::star, tok::amp);
128 WantExpressionKeywords =
129 nextTok.isOneOf(tok::l_paren, tok::identifier, tok::arrow, tok::period);
130 WantRemainingKeywords =
131 nextTok.isOneOf(tok::l_paren, tok::semi, tok::identifier, tok::l_brace);
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000132 WantCXXNamedCasts = false;
133 }
134
Craig Topper2b07f022014-03-12 05:09:18 +0000135 bool ValidateCandidate(const TypoCorrection &candidate) override {
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000136 if (FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>())
Kaelyn Uhrain07e62722013-10-01 22:00:28 +0000137 return !candidate.getCorrectionSpecifier() || isa<ObjCIvarDecl>(FD);
Kaelyn Uhrain46b6cdc2013-09-27 19:40:16 +0000138 if (NextToken.is(tok::equal))
139 return candidate.getCorrectionDeclAs<VarDecl>();
Kaelyn Uhrain30943ce2013-09-27 23:54:23 +0000140 if (NextToken.is(tok::period) &&
141 candidate.getCorrectionDeclAs<NamespaceDecl>())
142 return false;
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000143 return CorrectionCandidateCallback::ValidateCandidate(candidate);
144 }
145
146private:
147 Token NextToken;
148};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000149}
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000150
Richard Smithc202b282012-04-14 00:33:13 +0000151StmtResult
152Parser::ParseStatementOrDeclarationAfterAttributes(StmtVector &Stmts,
Alexey Bataevc4fad652016-01-13 11:18:54 +0000153 AllowedContsructsKind Allowed, SourceLocation *TrailingElseLoc,
Richard Smithc202b282012-04-14 00:33:13 +0000154 ParsedAttributesWithRange &Attrs) {
Craig Topper161e4db2014-05-21 06:02:52 +0000155 const char *SemiError = nullptr;
Richard Smithc202b282012-04-14 00:33:13 +0000156 StmtResult Res;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000157
Chris Lattner503fadc2006-08-10 05:45:44 +0000158 // Cases in this switch statement should fall through if the parser expects
159 // the token to end in a semicolon (in which case SemiError should be set),
160 // or they directly 'return;' if not.
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000161Retry:
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000162 tok::TokenKind Kind = Tok.getKind();
163 SourceLocation AtLoc;
164 switch (Kind) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000165 case tok::at: // May be a @try or @throw statement
166 {
Richard Smithc202b282012-04-14 00:33:13 +0000167 ProhibitAttributes(Attrs); // TODO: is it correct?
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000168 AtLoc = ConsumeToken(); // consume @
Sebastian Redlbab9a4b2008-12-11 20:12:42 +0000169 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000170 }
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000171
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000172 case tok::code_completion:
John McCallfaf5fb42010-08-26 23:41:50 +0000173 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000174 cutOffParsing();
175 return StmtError();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000176
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000177 case tok::identifier: {
178 Token Next = NextToken();
179 if (Next.is(tok::colon)) { // C99 6.8.1: labeled-statement
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000180 // identifier ':' statement
Richard Smithc202b282012-04-14 00:33:13 +0000181 return ParseLabeledStatement(Attrs);
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000182 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000183
Richard Smith4f605af2012-08-18 00:55:03 +0000184 // Look up the identifier, and typo-correct it to a keyword if it's not
185 // found.
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000186 if (Next.isNot(tok::coloncolon)) {
Richard Smith4f605af2012-08-18 00:55:03 +0000187 // Try to limit which sets of keywords should be included in typo
188 // correction based on what the next token is.
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000189 if (TryAnnotateName(/*IsAddressOfOperand*/ false,
190 llvm::make_unique<StatementFilterCCC>(Next)) ==
191 ANK_Error) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000192 // Handle errors here by skipping up to the next semicolon or '}', and
193 // eat the semicolon if that's what stopped us.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000194 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000195 if (Tok.is(tok::semi))
196 ConsumeToken();
197 return StmtError();
Richard Smith4f605af2012-08-18 00:55:03 +0000198 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000199
Richard Smith4f605af2012-08-18 00:55:03 +0000200 // If the identifier was typo-corrected, try again.
201 if (Tok.isNot(tok::identifier))
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000202 goto Retry;
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000203 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000204
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000205 // Fall through
206 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000207
Chris Lattner803802d2009-03-24 17:04:48 +0000208 default: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000209 if ((getLangOpts().CPlusPlus || Allowed == ACK_Any) &&
210 isDeclarationStatement()) {
Chris Lattner49836b42009-04-02 04:16:50 +0000211 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Rafael Espindola1bd906d2014-10-22 14:27:08 +0000212 DeclGroupPtrTy Decl = ParseDeclaration(Declarator::BlockContext,
Richard Smithc202b282012-04-14 00:33:13 +0000213 DeclEnd, Attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000214 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattner803802d2009-03-24 17:04:48 +0000215 }
216
217 if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +0000218 Diag(Tok, diag::err_expected_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000219 return StmtError();
Chris Lattnerf8afb622006-08-10 18:26:31 +0000220 }
Mike Stump11289f42009-09-09 15:08:12 +0000221
Richard Smithc202b282012-04-14 00:33:13 +0000222 return ParseExprStatement();
Chris Lattner803802d2009-03-24 17:04:48 +0000223 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000224
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000225 case tok::kw_case: // C99 6.8.1: labeled-statement
Richard Smithc202b282012-04-14 00:33:13 +0000226 return ParseCaseStatement();
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000227 case tok::kw_default: // C99 6.8.1: labeled-statement
Richard Smithc202b282012-04-14 00:33:13 +0000228 return ParseDefaultStatement();
Sebastian Redl042ad952008-12-11 19:30:53 +0000229
Chris Lattner9075bd72006-08-10 04:59:57 +0000230 case tok::l_brace: // C99 6.8.2: compound-statement
Richard Smithc202b282012-04-14 00:33:13 +0000231 return ParseCompoundStatement();
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000232 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
Argyrios Kyrtzidis43ea78b2011-09-01 21:53:45 +0000233 bool HasLeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
234 return Actions.ActOnNullStmt(ConsumeToken(), HasLeadingEmptyMacro);
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000235 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000236
Chris Lattner9075bd72006-08-10 04:59:57 +0000237 case tok::kw_if: // C99 6.8.4.1: if-statement
Richard Smithc202b282012-04-14 00:33:13 +0000238 return ParseIfStatement(TrailingElseLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000239 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Richard Smithc202b282012-04-14 00:33:13 +0000240 return ParseSwitchStatement(TrailingElseLoc);
Sebastian Redl042ad952008-12-11 19:30:53 +0000241
Chris Lattner9075bd72006-08-10 04:59:57 +0000242 case tok::kw_while: // C99 6.8.5.1: while-statement
Richard Smithc202b282012-04-14 00:33:13 +0000243 return ParseWhileStatement(TrailingElseLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000244 case tok::kw_do: // C99 6.8.5.2: do-statement
Richard Smithc202b282012-04-14 00:33:13 +0000245 Res = ParseDoStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000246 SemiError = "do/while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000247 break;
248 case tok::kw_for: // C99 6.8.5.3: for-statement
Richard Smithc202b282012-04-14 00:33:13 +0000249 return ParseForStatement(TrailingElseLoc);
Chris Lattner503fadc2006-08-10 05:45:44 +0000250
251 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Richard Smithc202b282012-04-14 00:33:13 +0000252 Res = ParseGotoStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000253 SemiError = "goto";
Chris Lattner9075bd72006-08-10 04:59:57 +0000254 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000255 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Richard Smithc202b282012-04-14 00:33:13 +0000256 Res = ParseContinueStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000257 SemiError = "continue";
Chris Lattner503fadc2006-08-10 05:45:44 +0000258 break;
259 case tok::kw_break: // C99 6.8.6.3: break-statement
Richard Smithc202b282012-04-14 00:33:13 +0000260 Res = ParseBreakStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000261 SemiError = "break";
Chris Lattner503fadc2006-08-10 05:45:44 +0000262 break;
263 case tok::kw_return: // C99 6.8.6.4: return-statement
Richard Smithc202b282012-04-14 00:33:13 +0000264 Res = ParseReturnStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000265 SemiError = "return";
Chris Lattner503fadc2006-08-10 05:45:44 +0000266 break;
Richard Smith0e304ea2015-10-22 04:46:14 +0000267 case tok::kw_co_return: // C++ Coroutines: co_return statement
268 Res = ParseReturnStatement();
269 SemiError = "co_return";
270 break;
Sebastian Redl042ad952008-12-11 19:30:53 +0000271
Sebastian Redlb219c902008-12-21 16:41:36 +0000272 case tok::kw_asm: {
Richard Smithc202b282012-04-14 00:33:13 +0000273 ProhibitAttributes(Attrs);
Steve Naroffb2c80c72008-02-07 03:50:06 +0000274 bool msAsm = false;
275 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidis3050d9b2010-11-02 02:33:08 +0000276 Res = Actions.ActOnFinishFullStmt(Res.get());
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000277 if (msAsm) return Res;
Chris Lattner34a95662009-06-14 00:07:48 +0000278 SemiError = "asm";
Chris Lattner0116c472006-08-15 06:03:28 +0000279 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000280 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000281
Reid Kleckner6d8d22a2014-06-25 00:28:35 +0000282 case tok::kw___if_exists:
283 case tok::kw___if_not_exists:
284 ProhibitAttributes(Attrs);
285 ParseMicrosoftIfExistsStatement(Stmts);
286 // An __if_exists block is like a compound statement, but it doesn't create
287 // a new scope.
288 return StmtEmpty();
289
Sebastian Redlb219c902008-12-21 16:41:36 +0000290 case tok::kw_try: // C++ 15: try-block
Richard Smithc202b282012-04-14 00:33:13 +0000291 return ParseCXXTryBlock();
John Wiegley1c0675e2011-04-28 01:08:34 +0000292
293 case tok::kw___try:
Richard Smithc202b282012-04-14 00:33:13 +0000294 ProhibitAttributes(Attrs); // TODO: is it correct?
295 return ParseSEHTryBlock();
Eli Friedmanec52f922012-02-23 23:47:16 +0000296
Nico Weberc7d05962014-07-06 22:32:59 +0000297 case tok::kw___leave:
298 Res = ParseSEHLeaveStatement();
299 SemiError = "__leave";
300 break;
301
Eli Friedmanec52f922012-02-23 23:47:16 +0000302 case tok::annot_pragma_vis:
Richard Smithc202b282012-04-14 00:33:13 +0000303 ProhibitAttributes(Attrs);
Eli Friedmanec52f922012-02-23 23:47:16 +0000304 HandlePragmaVisibility();
305 return StmtEmpty();
306
307 case tok::annot_pragma_pack:
Richard Smithc202b282012-04-14 00:33:13 +0000308 ProhibitAttributes(Attrs);
Eli Friedmanec52f922012-02-23 23:47:16 +0000309 HandlePragmaPack();
310 return StmtEmpty();
Eli Friedman68be1642012-10-04 02:36:51 +0000311
Eli Friedmanbbbbac62012-10-09 22:46:54 +0000312 case tok::annot_pragma_msstruct:
313 ProhibitAttributes(Attrs);
314 HandlePragmaMSStruct();
315 return StmtEmpty();
316
Eli Friedmanae8ee252012-10-08 23:52:38 +0000317 case tok::annot_pragma_align:
318 ProhibitAttributes(Attrs);
319 HandlePragmaAlign();
320 return StmtEmpty();
321
Eli Friedmanbbbbac62012-10-09 22:46:54 +0000322 case tok::annot_pragma_weak:
323 ProhibitAttributes(Attrs);
324 HandlePragmaWeak();
325 return StmtEmpty();
326
327 case tok::annot_pragma_weakalias:
328 ProhibitAttributes(Attrs);
329 HandlePragmaWeakAlias();
330 return StmtEmpty();
331
332 case tok::annot_pragma_redefine_extname:
333 ProhibitAttributes(Attrs);
334 HandlePragmaRedefineExtname();
335 return StmtEmpty();
336
Eli Friedman68be1642012-10-04 02:36:51 +0000337 case tok::annot_pragma_fp_contract:
Richard Smithca9b0b62013-11-15 21:10:54 +0000338 ProhibitAttributes(Attrs);
Lang Hamesa930e712012-10-21 01:10:01 +0000339 Diag(Tok, diag::err_pragma_fp_contract_scope);
340 ConsumeToken();
341 return StmtError();
342
Eli Friedman68be1642012-10-04 02:36:51 +0000343 case tok::annot_pragma_opencl_extension:
344 ProhibitAttributes(Attrs);
345 HandlePragmaOpenCLExtension();
346 return StmtEmpty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000347
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000348 case tok::annot_pragma_captured:
Richard Smith12a41bd2013-09-16 21:17:44 +0000349 ProhibitAttributes(Attrs);
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000350 return HandlePragmaCaptured();
351
Alexey Bataeva769e072013-03-22 06:34:35 +0000352 case tok::annot_pragma_openmp:
Richard Smith12a41bd2013-09-16 21:17:44 +0000353 ProhibitAttributes(Attrs);
Alexey Bataevc4fad652016-01-13 11:18:54 +0000354 return ParseOpenMPDeclarativeOrExecutableDirective(Allowed);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000355
David Majnemer4bb09802014-02-10 19:50:15 +0000356 case tok::annot_pragma_ms_pointers_to_members:
357 ProhibitAttributes(Attrs);
358 HandlePragmaMSPointersToMembers();
359 return StmtEmpty();
360
Warren Huntc3b18962014-04-08 22:30:47 +0000361 case tok::annot_pragma_ms_pragma:
362 ProhibitAttributes(Attrs);
363 HandlePragmaMSPragma();
364 return StmtEmpty();
Aaron Ballmanb06b15a2014-06-06 12:40:24 +0000365
Alexey Bataev3d42f342015-11-20 07:02:57 +0000366 case tok::annot_pragma_ms_vtordisp:
367 ProhibitAttributes(Attrs);
368 HandlePragmaMSVtorDisp();
369 return StmtEmpty();
370
Aaron Ballmanb06b15a2014-06-06 12:40:24 +0000371 case tok::annot_pragma_loop_hint:
372 ProhibitAttributes(Attrs);
Alexey Bataevc4fad652016-01-13 11:18:54 +0000373 return ParsePragmaLoopHint(Stmts, Allowed, TrailingElseLoc, Attrs);
Richard Smithba3a4f92016-01-12 21:59:26 +0000374
375 case tok::annot_pragma_dump:
376 HandlePragmaDump();
377 return StmtEmpty();
Sebastian Redlb219c902008-12-21 16:41:36 +0000378 }
379
Chris Lattner503fadc2006-08-10 05:45:44 +0000380 // If we reached this code, the statement must end in a semicolon.
Alp Toker97650562014-01-10 11:19:30 +0000381 if (!TryConsumeToken(tok::semi) && !Res.isInvalid()) {
Chris Lattner8e3eed02009-06-14 00:23:56 +0000382 // If the result was valid, then we do want to diagnose this. Use
383 // ExpectAndConsume to emit the diagnostic, even though we know it won't
384 // succeed.
385 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner0046de12008-11-13 18:52:53 +0000386 // Skip until we see a } or ;, but don't eat it.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000387 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner503fadc2006-08-10 05:45:44 +0000388 }
Mike Stump11289f42009-09-09 15:08:12 +0000389
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000390 return Res;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000391}
392
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000393/// \brief Parse an expression statement.
Richard Smithc202b282012-04-14 00:33:13 +0000394StmtResult Parser::ParseExprStatement() {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000395 // If a case keyword is missing, this is where it should be inserted.
396 Token OldToken = Tok;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000397
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000398 // expression[opt] ';'
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000399 ExprResult Expr(ParseExpression());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000400 if (Expr.isInvalid()) {
401 // If the expression is invalid, skip ahead to the next semicolon or '}'.
402 // Not doing this opens us up to the possibility of infinite loops if
403 // ParseExpression does not consume any tokens.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000404 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000405 if (Tok.is(tok::semi))
406 ConsumeToken();
John McCalleaef89b2013-03-22 02:10:40 +0000407 return Actions.ActOnExprStmtError();
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000408 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000409
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000410 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
411 Actions.CheckCaseExpression(Expr.get())) {
412 // If a constant expression is followed by a colon inside a switch block,
413 // suggest a missing case keyword.
414 Diag(OldToken, diag::err_expected_case_before_expression)
415 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000416
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000417 // Recover parsing as a case statement.
Richard Smithc202b282012-04-14 00:33:13 +0000418 return ParseCaseStatement(/*MissingCase=*/true, Expr);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000419 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000420
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000421 // Otherwise, eat the semicolon.
422 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +0000423 return Actions.ActOnExprStmt(Expr);
John Wiegley1c0675e2011-04-28 01:08:34 +0000424}
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000425
John Wiegley1c0675e2011-04-28 01:08:34 +0000426/// ParseSEHTryBlockCommon
427///
428/// seh-try-block:
429/// '__try' compound-statement seh-handler
430///
431/// seh-handler:
432/// seh-except-block
433/// seh-finally-block
434///
Nico Weberdd256742015-02-25 01:43:27 +0000435StmtResult Parser::ParseSEHTryBlock() {
436 assert(Tok.is(tok::kw___try) && "Expected '__try'");
437 SourceLocation TryLoc = ConsumeToken();
438
Nico Weberfc3fe4f2015-02-25 02:22:06 +0000439 if (Tok.isNot(tok::l_brace))
Alp Tokerec543272013-12-24 09:48:30 +0000440 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
John Wiegley1c0675e2011-04-28 01:08:34 +0000441
Warren Huntf6be4cb2014-07-25 20:52:51 +0000442 StmtResult TryBlock(ParseCompoundStatement(/*isStmtExpr=*/false,
443 Scope::DeclScope | Scope::SEHTryScope));
John Wiegley1c0675e2011-04-28 01:08:34 +0000444 if(TryBlock.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000445 return TryBlock;
John Wiegley1c0675e2011-04-28 01:08:34 +0000446
447 StmtResult Handler;
Richard Smithc202b282012-04-14 00:33:13 +0000448 if (Tok.is(tok::identifier) &&
Douglas Gregor60060d62011-10-21 03:57:52 +0000449 Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +0000450 SourceLocation Loc = ConsumeToken();
451 Handler = ParseSEHExceptBlock(Loc);
452 } else if (Tok.is(tok::kw___finally)) {
453 SourceLocation Loc = ConsumeToken();
454 Handler = ParseSEHFinallyBlock(Loc);
455 } else {
Nico Weberfc3fe4f2015-02-25 02:22:06 +0000456 return StmtError(Diag(Tok, diag::err_seh_expected_handler));
John Wiegley1c0675e2011-04-28 01:08:34 +0000457 }
458
459 if(Handler.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000460 return Handler;
John Wiegley1c0675e2011-04-28 01:08:34 +0000461
462 return Actions.ActOnSEHTryBlock(false /* IsCXXTry */,
463 TryLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000464 TryBlock.get(),
Warren Huntf6be4cb2014-07-25 20:52:51 +0000465 Handler.get());
John Wiegley1c0675e2011-04-28 01:08:34 +0000466}
467
468/// ParseSEHExceptBlock - Handle __except
469///
470/// seh-except-block:
471/// '__except' '(' seh-filter-expression ')' compound-statement
472///
473StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) {
474 PoisonIdentifierRAIIObject raii(Ident__exception_code, false),
475 raii2(Ident___exception_code, false),
476 raii3(Ident_GetExceptionCode, false);
477
Alp Toker383d2c42014-01-01 03:08:43 +0000478 if (ExpectAndConsume(tok::l_paren))
John Wiegley1c0675e2011-04-28 01:08:34 +0000479 return StmtError();
480
Reid Kleckner1d59f992015-01-22 01:36:17 +0000481 ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope |
482 Scope::SEHExceptScope);
John Wiegley1c0675e2011-04-28 01:08:34 +0000483
David Blaikiebbafb8a2012-03-11 07:00:24 +0000484 if (getLangOpts().Borland) {
Francois Pichetbfaf4772011-04-28 03:14:31 +0000485 Ident__exception_info->setIsPoisoned(false);
486 Ident___exception_info->setIsPoisoned(false);
487 Ident_GetExceptionInfo->setIsPoisoned(false);
488 }
Reid Kleckner1d59f992015-01-22 01:36:17 +0000489
490 ExprResult FilterExpr;
491 {
492 ParseScopeFlags FilterScope(this, getCurScope()->getFlags() |
493 Scope::SEHFilterScope);
Reid Kleckner85368fb2015-04-02 22:09:32 +0000494 FilterExpr = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Reid Kleckner1d59f992015-01-22 01:36:17 +0000495 }
Francois Pichetbfaf4772011-04-28 03:14:31 +0000496
David Blaikiebbafb8a2012-03-11 07:00:24 +0000497 if (getLangOpts().Borland) {
Francois Pichetbfaf4772011-04-28 03:14:31 +0000498 Ident__exception_info->setIsPoisoned(true);
499 Ident___exception_info->setIsPoisoned(true);
500 Ident_GetExceptionInfo->setIsPoisoned(true);
501 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000502
503 if(FilterExpr.isInvalid())
504 return StmtError();
505
Alp Toker383d2c42014-01-01 03:08:43 +0000506 if (ExpectAndConsume(tok::r_paren))
John Wiegley1c0675e2011-04-28 01:08:34 +0000507 return StmtError();
508
Nico Weberfc3fe4f2015-02-25 02:22:06 +0000509 if (Tok.isNot(tok::l_brace))
510 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
511
Richard Smithc202b282012-04-14 00:33:13 +0000512 StmtResult Block(ParseCompoundStatement());
John Wiegley1c0675e2011-04-28 01:08:34 +0000513
514 if(Block.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000515 return Block;
John Wiegley1c0675e2011-04-28 01:08:34 +0000516
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000517 return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.get(), Block.get());
John Wiegley1c0675e2011-04-28 01:08:34 +0000518}
519
520/// ParseSEHFinallyBlock - Handle __finally
521///
522/// seh-finally-block:
523/// '__finally' compound-statement
524///
Nico Weberd64657f2015-03-09 02:47:59 +0000525StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyLoc) {
John Wiegley1c0675e2011-04-28 01:08:34 +0000526 PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
527 raii2(Ident___abnormal_termination, false),
528 raii3(Ident_AbnormalTermination, false);
529
Nico Weberfc3fe4f2015-02-25 02:22:06 +0000530 if (Tok.isNot(tok::l_brace))
531 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
532
Nico Weberd64657f2015-03-09 02:47:59 +0000533 ParseScope FinallyScope(this, 0);
534 Actions.ActOnStartSEHFinallyBlock();
535
Richard Smithc202b282012-04-14 00:33:13 +0000536 StmtResult Block(ParseCompoundStatement());
Nico Weberce903292015-03-09 03:17:15 +0000537 if(Block.isInvalid()) {
538 Actions.ActOnAbortSEHFinallyBlock();
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000539 return Block;
Nico Weberce903292015-03-09 03:17:15 +0000540 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000541
Nico Weberd64657f2015-03-09 02:47:59 +0000542 return Actions.ActOnFinishSEHFinallyBlock(FinallyLoc, Block.get());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000543}
544
Nico Weberc7d05962014-07-06 22:32:59 +0000545/// Handle __leave
546///
547/// seh-leave-statement:
548/// '__leave' ';'
549///
550StmtResult Parser::ParseSEHLeaveStatement() {
551 SourceLocation LeaveLoc = ConsumeToken(); // eat the '__leave'.
552 return Actions.ActOnSEHLeaveStmt(LeaveLoc, getCurScope());
553}
554
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000555/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000556///
557/// labeled-statement:
558/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000559/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000560///
Richard Smithc202b282012-04-14 00:33:13 +0000561StmtResult Parser::ParseLabeledStatement(ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000562 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
563 "Not an identifier!");
564
565 Token IdentTok = Tok; // Save the whole token.
566 ConsumeToken(); // eat the identifier.
567
568 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000569
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000570 // identifier ':' statement
571 SourceLocation ColonLoc = ConsumeToken();
572
Richard Smitha3e01cf2013-11-15 22:45:29 +0000573 // Read label attributes, if present.
574 StmtResult SubStmt;
575 if (Tok.is(tok::kw___attribute)) {
576 ParsedAttributesWithRange TempAttrs(AttrFactory);
577 ParseGNUAttributes(TempAttrs);
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000578
Richard Smitha3e01cf2013-11-15 22:45:29 +0000579 // In C++, GNU attributes only apply to the label if they are followed by a
580 // semicolon, to disambiguate label attributes from attributes on a labeled
581 // declaration.
582 //
583 // This doesn't quite match what GCC does; if the attribute list is empty
584 // and followed by a semicolon, GCC will reject (it appears to parse the
585 // attributes as part of a statement in that case). That looks like a bug.
586 if (!getLangOpts().CPlusPlus || Tok.is(tok::semi))
587 attrs.takeAllFrom(TempAttrs);
588 else if (isDeclarationStatement()) {
589 StmtVector Stmts;
590 // FIXME: We should do this whether or not we have a declaration
591 // statement, but that doesn't work correctly (because ProhibitAttributes
592 // can't handle GNU attributes), so only call it in the one case where
593 // GNU attributes are allowed.
594 SubStmt = ParseStatementOrDeclarationAfterAttributes(
Alexey Bataevc4fad652016-01-13 11:18:54 +0000595 Stmts, /*Allowed=*/ACK_StatementsOpenMPNonStandalone, nullptr,
596 TempAttrs);
Richard Smitha3e01cf2013-11-15 22:45:29 +0000597 if (!TempAttrs.empty() && !SubStmt.isInvalid())
598 SubStmt = Actions.ProcessStmtAttributes(
599 SubStmt.get(), TempAttrs.getList(), TempAttrs.Range);
600 } else {
Alp Toker383d2c42014-01-01 03:08:43 +0000601 Diag(Tok, diag::err_expected_after) << "__attribute__" << tok::semi;
Richard Smitha3e01cf2013-11-15 22:45:29 +0000602 }
603 }
604
605 // If we've not parsed a statement yet, parse one now.
606 if (!SubStmt.isInvalid() && !SubStmt.isUsable())
607 SubStmt = ParseStatement();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000608
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000609 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000610 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000611 SubStmt = Actions.ActOnNullStmt(ColonLoc);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000612
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000613 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
614 IdentTok.getLocation());
Richard Smithc202b282012-04-14 00:33:13 +0000615 if (AttributeList *Attrs = attrs.getList()) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000616 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
Richard Smithc202b282012-04-14 00:33:13 +0000617 attrs.clear();
618 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000619
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000620 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
621 SubStmt.get());
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000622}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000623
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000624/// ParseCaseStatement
625/// labeled-statement:
626/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000627/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000628///
Richard Smithc202b282012-04-14 00:33:13 +0000629StmtResult Parser::ParseCaseStatement(bool MissingCase, ExprResult Expr) {
Richard Smithd4257d82011-04-21 22:48:40 +0000630 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Mike Stump11289f42009-09-09 15:08:12 +0000631
Chris Lattner34a22092009-03-04 04:23:07 +0000632 // It is very very common for code to contain many case statements recursively
633 // nested, as in (but usually without indentation):
634 // case 1:
635 // case 2:
636 // case 3:
637 // case 4:
638 // case 5: etc.
639 //
640 // Parsing this naively works, but is both inefficient and can cause us to run
641 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner2b19a6582009-03-04 18:24:58 +0000642 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner34a22092009-03-04 04:23:07 +0000643 // but all the grossness is constrained to ParseCaseStatement (and some
Richard Smitha3e01cf2013-11-15 22:45:29 +0000644 // weirdness in the actions), so this is just local grossness :).
Mike Stump11289f42009-09-09 15:08:12 +0000645
Chris Lattner34a22092009-03-04 04:23:07 +0000646 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
647 // example above.
John McCalldadc5752010-08-24 06:29:42 +0000648 StmtResult TopLevelCase(true);
Mike Stump11289f42009-09-09 15:08:12 +0000649
Chris Lattner34a22092009-03-04 04:23:07 +0000650 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
651 // gets updated each time a new case is parsed, and whose body is unset so
652 // far. When parsing 'case 4', this is the 'case 3' node.
Craig Topper161e4db2014-05-21 06:02:52 +0000653 Stmt *DeepestParsedCaseStmt = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000654
Chris Lattner34a22092009-03-04 04:23:07 +0000655 // While we have case statements, eat and stack them.
David Majnemer0ac67fa2011-06-13 05:50:12 +0000656 SourceLocation ColonLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000657 do {
Richard Trieu2c850c02011-04-21 21:44:26 +0000658 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
659 ConsumeToken(); // eat the 'case'.
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000660 ColonLoc = SourceLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000661
Douglas Gregord328d572009-09-21 18:10:23 +0000662 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000663 Actions.CodeCompleteCase(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000664 cutOffParsing();
665 return StmtError();
Douglas Gregord328d572009-09-21 18:10:23 +0000666 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000667
Chris Lattner125c0ee2009-12-10 00:38:54 +0000668 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
669 /// Disable this form of error recovery while we're parsing the case
670 /// expression.
671 ColonProtectionRAIIObject ColonProtection(*this);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000672
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000673 ExprResult LHS;
674 if (!MissingCase) {
675 LHS = ParseConstantExpression();
Kaelyn Takatab16e6322014-11-20 22:06:40 +0000676 if (!getLangOpts().CPlusPlus11) {
677 LHS = Actions.CorrectDelayedTyposInExpr(LHS, [this](class Expr *E) {
678 return Actions.VerifyIntegerConstantExpression(E);
679 });
680 }
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000681 if (LHS.isInvalid()) {
682 // If constant-expression is parsed unsuccessfully, recover by skipping
683 // current case statement (moving to the colon that ends it).
684 if (SkipUntil(tok::colon, tok::r_brace, StopAtSemi | StopBeforeMatch)) {
685 TryConsumeToken(tok::colon, ColonLoc);
686 continue;
687 }
688 return StmtError();
689 }
690 } else {
691 LHS = Expr;
692 MissingCase = false;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000693 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000694
Chris Lattner34a22092009-03-04 04:23:07 +0000695 // GNU case range extension.
696 SourceLocation DotDotDotLoc;
John McCalldadc5752010-08-24 06:29:42 +0000697 ExprResult RHS;
Alp Tokerec543272013-12-24 09:48:30 +0000698 if (TryConsumeToken(tok::ellipsis, DotDotDotLoc)) {
699 Diag(DotDotDotLoc, diag::ext_gnu_case_range);
Chris Lattner34a22092009-03-04 04:23:07 +0000700 RHS = ParseConstantExpression();
701 if (RHS.isInvalid()) {
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000702 if (SkipUntil(tok::colon, tok::r_brace, StopAtSemi | StopBeforeMatch)) {
703 TryConsumeToken(tok::colon, ColonLoc);
704 continue;
705 }
Chris Lattner34a22092009-03-04 04:23:07 +0000706 return StmtError();
707 }
708 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000709
Chris Lattner125c0ee2009-12-10 00:38:54 +0000710 ColonProtection.restore();
Sebastian Redl042ad952008-12-11 19:30:53 +0000711
Alp Tokerec543272013-12-24 09:48:30 +0000712 if (TryConsumeToken(tok::colon, ColonLoc)) {
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000713 } else if (TryConsumeToken(tok::semi, ColonLoc) ||
714 TryConsumeToken(tok::coloncolon, ColonLoc)) {
715 // Treat "case blah;" or "case blah::" as a typo for "case blah:".
Alp Tokerec543272013-12-24 09:48:30 +0000716 Diag(ColonLoc, diag::err_expected_after)
717 << "'case'" << tok::colon
718 << FixItHint::CreateReplacement(ColonLoc, ":");
John McCall0140bfe2011-01-22 09:28:32 +0000719 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000720 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
Alp Tokerec543272013-12-24 09:48:30 +0000721 Diag(ExpectedLoc, diag::err_expected_after)
722 << "'case'" << tok::colon
723 << FixItHint::CreateInsertion(ExpectedLoc, ":");
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000724 ColonLoc = ExpectedLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000725 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000726
John McCalldadc5752010-08-24 06:29:42 +0000727 StmtResult Case =
John McCallb268a282010-08-23 23:25:46 +0000728 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
729 RHS.get(), ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000730
Chris Lattner34a22092009-03-04 04:23:07 +0000731 // If we had a sema error parsing this case, then just ignore it and
732 // continue parsing the sub-stmt.
733 if (Case.isInvalid()) {
734 if (TopLevelCase.isInvalid()) // No parsed case stmts.
Alexey Bataevc4fad652016-01-13 11:18:54 +0000735 return ParseStatement(/*TrailingElseLoc=*/nullptr,
736 /*AllowOpenMPStandalone=*/true);
Chris Lattner34a22092009-03-04 04:23:07 +0000737 // Otherwise, just don't add it as a nested case.
738 } else {
739 // If this is the first case statement we parsed, it becomes TopLevelCase.
740 // Otherwise we link it into the current chain.
John McCall37ad5512010-08-23 06:44:23 +0000741 Stmt *NextDeepest = Case.get();
Chris Lattner34a22092009-03-04 04:23:07 +0000742 if (TopLevelCase.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000743 TopLevelCase = Case;
Chris Lattner34a22092009-03-04 04:23:07 +0000744 else
John McCallb268a282010-08-23 23:25:46 +0000745 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner34a22092009-03-04 04:23:07 +0000746 DeepestParsedCaseStmt = NextDeepest;
747 }
Mike Stump11289f42009-09-09 15:08:12 +0000748
Chris Lattner34a22092009-03-04 04:23:07 +0000749 // Handle all case statements.
750 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000751
Chris Lattner34a22092009-03-04 04:23:07 +0000752 // If we found a non-case statement, start by parsing it.
John McCalldadc5752010-08-24 06:29:42 +0000753 StmtResult SubStmt;
Mike Stump11289f42009-09-09 15:08:12 +0000754
Chris Lattner34a22092009-03-04 04:23:07 +0000755 if (Tok.isNot(tok::r_brace)) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000756 SubStmt = ParseStatement(/*TrailingElseLoc=*/nullptr,
757 /*AllowOpenMPStandalone=*/true);
Chris Lattner34a22092009-03-04 04:23:07 +0000758 } else {
759 // Nicely diagnose the common error "switch (X) { case 4: }", which is
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000760 // not valid. If ColonLoc doesn't point to a valid text location, there was
761 // another parsing error, so avoid producing extra diagnostics.
762 if (ColonLoc.isValid()) {
763 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
764 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
765 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
766 }
767 SubStmt = StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000768 }
Mike Stump11289f42009-09-09 15:08:12 +0000769
Chris Lattner34a22092009-03-04 04:23:07 +0000770 // Install the body into the most deeply-nested case.
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000771 if (DeepestParsedCaseStmt) {
772 // Broken sub-stmt shouldn't prevent forming the case statement properly.
773 if (SubStmt.isInvalid())
774 SubStmt = Actions.ActOnNullStmt(SourceLocation());
775 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
776 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000777
Chris Lattner34a22092009-03-04 04:23:07 +0000778 // Return the top level parsed statement tree.
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000779 return TopLevelCase;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000780}
781
782/// ParseDefaultStatement
783/// labeled-statement:
784/// 'default' ':' statement
785/// Note that this does not parse the 'statement' at the end.
786///
Richard Smithc202b282012-04-14 00:33:13 +0000787StmtResult Parser::ParseDefaultStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000788 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000789 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000790
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000791 SourceLocation ColonLoc;
Alp Tokerec543272013-12-24 09:48:30 +0000792 if (TryConsumeToken(tok::colon, ColonLoc)) {
Alp Tokerec543272013-12-24 09:48:30 +0000793 } else if (TryConsumeToken(tok::semi, ColonLoc)) {
Alp Toker97650562014-01-10 11:19:30 +0000794 // Treat "default;" as a typo for "default:".
Alp Tokerec543272013-12-24 09:48:30 +0000795 Diag(ColonLoc, diag::err_expected_after)
796 << "'default'" << tok::colon
797 << FixItHint::CreateReplacement(ColonLoc, ":");
John McCall0140bfe2011-01-22 09:28:32 +0000798 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000799 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
Alp Tokerec543272013-12-24 09:48:30 +0000800 Diag(ExpectedLoc, diag::err_expected_after)
801 << "'default'" << tok::colon
802 << FixItHint::CreateInsertion(ExpectedLoc, ":");
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000803 ColonLoc = ExpectedLoc;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000804 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000805
Richard Smith1002d102012-02-17 01:35:32 +0000806 StmtResult SubStmt;
807
808 if (Tok.isNot(tok::r_brace)) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000809 SubStmt = ParseStatement(/*TrailingElseLoc=*/nullptr,
810 /*AllowOpenMPStandalone=*/true);
Richard Smith1002d102012-02-17 01:35:32 +0000811 } else {
812 // Diagnose the common error "switch (X) {... default: }", which is
813 // not valid.
David Majnemerc6a99872011-06-14 15:24:38 +0000814 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
Richard Smith1002d102012-02-17 01:35:32 +0000815 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
816 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
817 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000818 }
819
Richard Smith1002d102012-02-17 01:35:32 +0000820 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000821 if (SubStmt.isInvalid())
Richard Smith1002d102012-02-17 01:35:32 +0000822 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl042ad952008-12-11 19:30:53 +0000823
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000824 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000825 SubStmt.get(), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000826}
827
Richard Smithc202b282012-04-14 00:33:13 +0000828StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
829 return ParseCompoundStatement(isStmtExpr, Scope::DeclScope);
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000830}
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000831
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000832/// ParseCompoundStatement - Parse a "{}" block.
833///
834/// compound-statement: [C99 6.8.2]
835/// { block-item-list[opt] }
836/// [GNU] { label-declarations block-item-list } [TODO]
837///
838/// block-item-list:
839/// block-item
840/// block-item-list block-item
841///
842/// block-item:
843/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000844/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000845/// statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000846///
847/// [GNU] label-declarations:
848/// [GNU] label-declaration
849/// [GNU] label-declarations label-declaration
850///
851/// [GNU] label-declaration:
852/// [GNU] '__label__' identifier-list ';'
853///
Richard Smithc202b282012-04-14 00:33:13 +0000854StmtResult Parser::ParseCompoundStatement(bool isStmtExpr,
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000855 unsigned ScopeFlags) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000856 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000857
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000858 // Enter a scope to hold everything within the compound stmt. Compound
859 // statements can always hold declarations.
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000860 ParseScope CompoundScope(this, ScopeFlags);
Chris Lattnerf2978802007-01-21 06:52:16 +0000861
862 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000863 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000864}
865
Lang Hames2954cea2012-11-03 22:29:05 +0000866/// Parse any pragmas at the start of the compound expression. We handle these
867/// separately since some pragmas (FP_CONTRACT) must appear before any C
868/// statement in the compound, but may be intermingled with other pragmas.
869void Parser::ParseCompoundStatementLeadingPragmas() {
870 bool checkForPragmas = true;
871 while (checkForPragmas) {
872 switch (Tok.getKind()) {
873 case tok::annot_pragma_vis:
874 HandlePragmaVisibility();
875 break;
876 case tok::annot_pragma_pack:
877 HandlePragmaPack();
878 break;
879 case tok::annot_pragma_msstruct:
880 HandlePragmaMSStruct();
881 break;
882 case tok::annot_pragma_align:
883 HandlePragmaAlign();
884 break;
885 case tok::annot_pragma_weak:
886 HandlePragmaWeak();
887 break;
888 case tok::annot_pragma_weakalias:
889 HandlePragmaWeakAlias();
890 break;
891 case tok::annot_pragma_redefine_extname:
892 HandlePragmaRedefineExtname();
893 break;
894 case tok::annot_pragma_opencl_extension:
895 HandlePragmaOpenCLExtension();
896 break;
897 case tok::annot_pragma_fp_contract:
898 HandlePragmaFPContract();
899 break;
David Majnemer4bb09802014-02-10 19:50:15 +0000900 case tok::annot_pragma_ms_pointers_to_members:
901 HandlePragmaMSPointersToMembers();
902 break;
Warren Huntc3b18962014-04-08 22:30:47 +0000903 case tok::annot_pragma_ms_pragma:
904 HandlePragmaMSPragma();
905 break;
Alexey Bataev3d42f342015-11-20 07:02:57 +0000906 case tok::annot_pragma_ms_vtordisp:
907 HandlePragmaMSVtorDisp();
908 break;
Richard Smithba3a4f92016-01-12 21:59:26 +0000909 case tok::annot_pragma_dump:
910 HandlePragmaDump();
911 break;
Lang Hames2954cea2012-11-03 22:29:05 +0000912 default:
913 checkForPragmas = false;
914 break;
915 }
916 }
917
918}
919
Chris Lattnerf2978802007-01-21 06:52:16 +0000920/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000921/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000922/// consume the '}' at the end of the block. It does not manipulate the scope
923/// stack.
John McCalldadc5752010-08-24 06:29:42 +0000924StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000925 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000926 Tok.getLocation(),
927 "in compound statement ('{}')");
Lang Hames5de91cc2012-10-02 04:45:10 +0000928
929 // Record the state of the FP_CONTRACT pragma, restore on leaving the
930 // compound statement.
931 Sema::FPContractStateRAII SaveFPContractState(Actions);
932
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000933 InMessageExpressionRAIIObject InMessage(*this, false);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000934 BalancedDelimiterTracker T(*this, tok::l_brace);
935 if (T.consumeOpen())
936 return StmtError();
Chris Lattnerf2978802007-01-21 06:52:16 +0000937
Dmitri Gribenko800ddf32012-02-14 22:14:32 +0000938 Sema::CompoundScopeRAII CompoundScope(Actions);
939
Lang Hames2954cea2012-11-03 22:29:05 +0000940 // Parse any pragmas at the beginning of the compound statement.
941 ParseCompoundStatementLeadingPragmas();
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000942
Lang Hames2954cea2012-11-03 22:29:05 +0000943 StmtVector Stmts;
Lang Hamesa930e712012-10-21 01:10:01 +0000944
Chris Lattner43e7f312011-02-18 02:08:43 +0000945 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
946 // only allowed at the start of a compound stmt regardless of the language.
947 while (Tok.is(tok::kw___label__)) {
948 SourceLocation LabelLoc = ConsumeToken();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000949
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000950 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner43e7f312011-02-18 02:08:43 +0000951 while (1) {
952 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000953 Diag(Tok, diag::err_expected) << tok::identifier;
Chris Lattner43e7f312011-02-18 02:08:43 +0000954 break;
955 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000956
Chris Lattner43e7f312011-02-18 02:08:43 +0000957 IdentifierInfo *II = Tok.getIdentifierInfo();
958 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000959 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000960
Alp Tokerec543272013-12-24 09:48:30 +0000961 if (!TryConsumeToken(tok::comma))
Chris Lattner43e7f312011-02-18 02:08:43 +0000962 break;
Chris Lattner43e7f312011-02-18 02:08:43 +0000963 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000964
John McCall084e83d2011-03-24 11:26:52 +0000965 DeclSpec DS(AttrFactory);
Rafael Espindolaab417692013-07-09 12:05:01 +0000966 DeclGroupPtrTy Res =
967 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner43e7f312011-02-18 02:08:43 +0000968 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000969
Chris Lattner02f1b612012-04-28 16:12:17 +0000970 ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
Chris Lattner43e7f312011-02-18 02:08:43 +0000971 if (R.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000972 Stmts.push_back(R.get());
Chris Lattner43e7f312011-02-18 02:08:43 +0000973 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000974
Richard Smith752ada82015-11-17 23:32:01 +0000975 while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
976 Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000977 if (Tok.is(tok::annot_pragma_unused)) {
978 HandlePragmaUnused();
979 continue;
980 }
981
John McCalldadc5752010-08-24 06:29:42 +0000982 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000983 if (Tok.isNot(tok::kw___extension__)) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000984 R = ParseStatementOrDeclaration(Stmts, ACK_Any);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000985 } else {
986 // __extension__ can start declarations and it can also be a unary
987 // operator for expressions. Consume multiple __extension__ markers here
988 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000989 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000990 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000991 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000992 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +0000993
John McCall084e83d2011-03-24 11:26:52 +0000994 ParsedAttributesWithRange attrs(AttrFactory);
Craig Topper161e4db2014-05-21 06:02:52 +0000995 MaybeParseCXX11Attributes(attrs, nullptr,
996 /*MightBeObjCMessageSend*/ true);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000997
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000998 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000999 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +00001000 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +00001001 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +00001002 ExtensionRAIIObject O(Diags);
1003
Chris Lattner49836b42009-04-02 04:16:50 +00001004 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Rafael Espindola1bd906d2014-10-22 14:27:08 +00001005 DeclGroupPtrTy Res = ParseDeclaration(Declarator::BlockContext, DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001006 attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001007 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001008 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +00001009 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +00001010 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +00001011
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001012 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001013 SkipUntil(tok::semi);
1014 continue;
1015 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001016
Alexis Hunt96d5c762009-11-21 08:43:09 +00001017 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +00001018 // Eat the semicolon at the end of stmt and convert the expr into a
1019 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00001020 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +00001021 R = Actions.ActOnExprStmt(Res);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001022 }
1023 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001024
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001025 if (R.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001026 Stmts.push_back(R.get());
Chris Lattner30f910e2006-10-16 05:52:41 +00001027 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001028
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +00001029 SourceLocation CloseLoc = Tok.getLocation();
1030
Chris Lattner0ccd51e2006-08-09 05:47:47 +00001031 // We broke out of the while loop because we found a '}' or EOF.
Nico Webera48b6c22012-12-30 23:36:56 +00001032 if (!T.consumeClose())
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +00001033 // Recover by creating a compound statement with what we parsed so far,
1034 // instead of dropping everything and returning StmtError();
Nico Webera48b6c22012-12-30 23:36:56 +00001035 CloseLoc = T.getCloseLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +00001036
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +00001037 return Actions.ActOnCompoundStmt(T.getOpenLocation(), CloseLoc,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001038 Stmts, isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +00001039}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001040
Chris Lattnerc0081db2008-12-12 06:31:07 +00001041/// ParseParenExprOrCondition:
1042/// [C ] '(' expression ')'
Richard Smithc7a05a92016-06-29 21:17:59 +00001043/// [C++] '(' condition ')'
1044/// [C++1z] '(' init-statement[opt] condition ')'
Chris Lattnerc0081db2008-12-12 06:31:07 +00001045///
1046/// This function parses and performs error recovery on the specified condition
1047/// or expression (depending on whether we're in C++ or C mode). This function
1048/// goes out of its way to recover well. It returns true if there was a parser
1049/// error (the right paren couldn't be found), which indicates that the caller
1050/// should try to recover harder. It returns false if the condition is
1051/// successfully parsed. Note that a successful parse can still have semantic
1052/// errors in the condition.
Richard Smithc7a05a92016-06-29 21:17:59 +00001053bool Parser::ParseParenExprOrCondition(StmtResult *InitStmt,
1054 Sema::ConditionResult &Cond,
Douglas Gregore60e41a2010-05-06 17:25:47 +00001055 SourceLocation Loc,
Richard Smith03a4aa32016-06-23 19:02:52 +00001056 Sema::ConditionKind CK) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001057 BalancedDelimiterTracker T(*this, tok::l_paren);
1058 T.consumeOpen();
1059
David Blaikiebbafb8a2012-03-11 07:00:24 +00001060 if (getLangOpts().CPlusPlus)
Richard Smithc7a05a92016-06-29 21:17:59 +00001061 Cond = ParseCXXCondition(InitStmt, Loc, CK);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001062 else {
Richard Smith03a4aa32016-06-23 19:02:52 +00001063 ExprResult CondExpr = ParseExpression();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001064
Douglas Gregore60e41a2010-05-06 17:25:47 +00001065 // If required, convert to a boolean value.
Richard Smith03a4aa32016-06-23 19:02:52 +00001066 if (CondExpr.isInvalid())
1067 Cond = Sema::ConditionError();
1068 else
1069 Cond = Actions.ActOnCondition(getCurScope(), Loc, CondExpr.get(), CK);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001070 }
Mike Stump11289f42009-09-09 15:08:12 +00001071
Chris Lattnerc0081db2008-12-12 06:31:07 +00001072 // If the parser was confused by the condition and we don't have a ')', try to
1073 // recover by skipping ahead to a semi and bailing out. If condexp is
1074 // semantically invalid but we have well formed code, keep going.
Richard Smith03a4aa32016-06-23 19:02:52 +00001075 if (Cond.isInvalid() && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +00001076 SkipUntil(tok::semi);
1077 // Skipping may have stopped if it found the containing ')'. If so, we can
1078 // continue parsing the if statement.
1079 if (Tok.isNot(tok::r_paren))
1080 return true;
1081 }
Mike Stump11289f42009-09-09 15:08:12 +00001082
Chris Lattnerc0081db2008-12-12 06:31:07 +00001083 // Otherwise the condition is valid or the rparen is present.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001084 T.consumeClose();
Chad Rosier67055f52012-07-10 21:35:27 +00001085
Chris Lattner70d44982012-04-28 16:24:20 +00001086 // Check for extraneous ')'s to catch things like "if (foo())) {". We know
1087 // that all callers are looking for a statement after the condition, so ")"
1088 // isn't valid.
1089 while (Tok.is(tok::r_paren)) {
1090 Diag(Tok, diag::err_extraneous_rparen_in_condition)
1091 << FixItHint::CreateRemoval(Tok.getLocation());
1092 ConsumeParen();
1093 }
Chad Rosier67055f52012-07-10 21:35:27 +00001094
Chris Lattnerc0081db2008-12-12 06:31:07 +00001095 return false;
1096}
1097
1098
Chris Lattnerc951dae2006-08-10 04:23:57 +00001099/// ParseIfStatement
1100/// if-statement: [C99 6.8.4.1]
1101/// 'if' '(' expression ')' statement
1102/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001103/// [C++] 'if' '(' condition ')' statement
1104/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +00001105///
Richard Smithc202b282012-04-14 00:33:13 +00001106StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001107 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001108 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +00001109
Richard Smithb130fe72016-06-23 19:16:49 +00001110 bool IsConstexpr = false;
1111 if (Tok.is(tok::kw_constexpr)) {
1112 Diag(Tok, getLangOpts().CPlusPlus1z ? diag::warn_cxx14_compat_constexpr_if
1113 : diag::ext_constexpr_if);
1114 IsConstexpr = true;
1115 ConsumeToken();
1116 }
1117
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001118 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001119 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +00001120 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +00001121 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +00001122 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001123
David Blaikiebbafb8a2012-03-11 07:00:24 +00001124 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001125
Chris Lattner2dd1b722007-08-26 23:08:06 +00001126 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
1127 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001128 //
1129 // C++ 6.4p3:
1130 // A name introduced by a declaration in a condition is in scope from its
1131 // point of declaration until the end of the substatements controlled by the
1132 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001133 // C++ 3.3.2p4:
1134 // Names declared in the for-init-statement, and in the condition of if,
1135 // while, for, and switch statements are local to the if, while, for, or
1136 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001137 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001138 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +00001139
Chris Lattnerc951dae2006-08-10 04:23:57 +00001140 // Parse the condition.
Richard Smithc7a05a92016-06-29 21:17:59 +00001141 StmtResult InitStmt;
Richard Smith03a4aa32016-06-23 19:02:52 +00001142 Sema::ConditionResult Cond;
Richard Smithc7a05a92016-06-29 21:17:59 +00001143 if (ParseParenExprOrCondition(&InitStmt, Cond, IfLoc,
Richard Smithb130fe72016-06-23 19:16:49 +00001144 IsConstexpr ? Sema::ConditionKind::ConstexprIf
1145 : Sema::ConditionKind::Boolean))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001146 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +00001147
Richard Smithb130fe72016-06-23 19:16:49 +00001148 llvm::Optional<bool> ConstexprCondition;
1149 if (IsConstexpr)
1150 ConstexprCondition = Cond.getKnownValue();
1151
Chris Lattner8fb26252007-08-22 05:28:50 +00001152 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001153 // there is no compound stmt. C90 does not have this clause. We only do this
1154 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001155 //
1156 // C++ 6.4p1:
1157 // The substatement in a selection-statement (each substatement, in the else
1158 // form of the if statement) implicitly defines a local scope.
1159 //
1160 // For C++ we create a scope for the condition and a new scope for
1161 // substatements because:
1162 // -When the 'then' scope exits, we want the condition declaration to still be
1163 // active for the 'else' scope too.
1164 // -Sema will detect name clashes by considering declarations of a
1165 // 'ControlScope' as part of its direct subscope.
1166 // -If we wanted the condition and substatement to be in the same scope, we
1167 // would have to notify ParseStatement not to create a new scope. It's
1168 // simpler to let it create a new scope.
1169 //
David Majnemer2206bf52014-03-05 08:57:59 +00001170 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001171
Chris Lattner5c5808a2007-10-29 05:08:52 +00001172 // Read the 'then' stmt.
1173 SourceLocation ThenStmtLoc = Tok.getLocation();
Nico Weber3cef1082011-12-22 23:26:17 +00001174
1175 SourceLocation InnerStatementTrailingElseLoc;
Richard Smithb130fe72016-06-23 19:16:49 +00001176 StmtResult ThenStmt;
1177 {
1178 EnterExpressionEvaluationContext PotentiallyDiscarded(
1179 Actions, Sema::DiscardedStatement, nullptr, false,
1180 /*ShouldEnter=*/ConstexprCondition && !*ConstexprCondition);
1181 ThenStmt = ParseStatement(&InnerStatementTrailingElseLoc);
1182 }
Chris Lattnerac4471c2007-05-28 05:38:24 +00001183
Chris Lattner37e54f42007-08-22 05:16:28 +00001184 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001185 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001186
Chris Lattnerc951dae2006-08-10 04:23:57 +00001187 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +00001188 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +00001189 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +00001190 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001191
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001192 if (Tok.is(tok::kw_else)) {
Nico Weber3cef1082011-12-22 23:26:17 +00001193 if (TrailingElseLoc)
1194 *TrailingElseLoc = Tok.getLocation();
1195
Chris Lattneraf635312006-10-16 06:06:51 +00001196 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +00001197 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +00001198
Chris Lattner8fb26252007-08-22 05:28:50 +00001199 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001200 // there is no compound stmt. C90 does not have this clause. We only do
1201 // this if the body isn't a compound statement to avoid push/pop in common
1202 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001203 //
1204 // C++ 6.4p1:
1205 // The substatement in a selection-statement (each substatement, in the else
1206 // form of the if statement) implicitly defines a local scope.
1207 //
Richard Smithb130fe72016-06-23 19:16:49 +00001208 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX,
1209 Tok.is(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001210
Richard Smithb130fe72016-06-23 19:16:49 +00001211 EnterExpressionEvaluationContext PotentiallyDiscarded(
1212 Actions, Sema::DiscardedStatement, nullptr, false,
1213 /*ShouldEnter=*/ConstexprCondition && *ConstexprCondition);
Chris Lattner30f910e2006-10-16 05:52:41 +00001214 ElseStmt = ParseStatement();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001215
Chris Lattner37e54f42007-08-22 05:16:28 +00001216 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001217 InnerScope.Exit();
Douglas Gregor4ecb7202011-07-30 08:36:53 +00001218 } else if (Tok.is(tok::code_completion)) {
1219 Actions.CodeCompleteAfterIf(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001220 cutOffParsing();
1221 return StmtError();
Nico Weber3cef1082011-12-22 23:26:17 +00001222 } else if (InnerStatementTrailingElseLoc.isValid()) {
1223 Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else);
Chris Lattnerc951dae2006-08-10 04:23:57 +00001224 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001225
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001226 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +00001227
Chris Lattner5c5808a2007-10-29 05:08:52 +00001228 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +00001229 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +00001230 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001231 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
Craig Topper161e4db2014-05-21 06:02:52 +00001232 (ThenStmt.isInvalid() && ElseStmt.get() == nullptr) ||
1233 (ThenStmt.get() == nullptr && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +00001234 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +00001235 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +00001236 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001237
Chris Lattner5c5808a2007-10-29 05:08:52 +00001238 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001239 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001240 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001241 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001242 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001243
Richard Smithc7a05a92016-06-29 21:17:59 +00001244 return Actions.ActOnIfStmt(IfLoc, IsConstexpr, InitStmt.get(), Cond,
1245 ThenStmt.get(), ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +00001246}
1247
Chris Lattner9075bd72006-08-10 04:59:57 +00001248/// ParseSwitchStatement
1249/// switch-statement:
1250/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001251/// [C++] 'switch' '(' condition ')' statement
Richard Smithc202b282012-04-14 00:33:13 +00001252StmtResult Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001253 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001254 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +00001255
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001256 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001257 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +00001258 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001259 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001260 }
Chris Lattner2dd1b722007-08-26 23:08:06 +00001261
David Blaikiebbafb8a2012-03-11 07:00:24 +00001262 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001263
Chris Lattner2dd1b722007-08-26 23:08:06 +00001264 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1265 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001266 //
1267 // C++ 6.4p3:
1268 // A name introduced by a declaration in a condition is in scope from its
1269 // point of declaration until the end of the substatements controlled by the
1270 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001271 // C++ 3.3.2p4:
1272 // Names declared in the for-init-statement, and in the condition of if,
1273 // while, for, and switch statements are local to the if, while, for, or
1274 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001275 //
Serge Pavlov09f99242014-01-23 15:05:00 +00001276 unsigned ScopeFlags = Scope::SwitchScope;
Chris Lattnerc0081db2008-12-12 06:31:07 +00001277 if (C99orCXX)
1278 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001279 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001280
Chris Lattner9075bd72006-08-10 04:59:57 +00001281 // Parse the condition.
Richard Smithc7a05a92016-06-29 21:17:59 +00001282 StmtResult InitStmt;
Richard Smith03a4aa32016-06-23 19:02:52 +00001283 Sema::ConditionResult Cond;
Richard Smithc7a05a92016-06-29 21:17:59 +00001284 if (ParseParenExprOrCondition(&InitStmt, Cond, SwitchLoc,
1285 Sema::ConditionKind::Switch))
Sebastian Redlb62406f2008-12-11 19:48:14 +00001286 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +00001287
Richard Smithc7a05a92016-06-29 21:17:59 +00001288 StmtResult Switch =
1289 Actions.ActOnStartOfSwitchStmt(SwitchLoc, InitStmt.get(), Cond);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001290
Douglas Gregore60e41a2010-05-06 17:25:47 +00001291 if (Switch.isInvalid()) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001292 // Skip the switch body.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001293 // FIXME: This is not optimal recovery, but parsing the body is more
1294 // dangerous due to the presence of case and default statements, which
1295 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +00001296 if (Tok.is(tok::l_brace)) {
1297 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001298 SkipUntil(tok::r_brace);
Douglas Gregor4abc32d2010-05-20 23:20:59 +00001299 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +00001300 SkipUntil(tok::semi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001301 return Switch;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001302 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001303
Chris Lattner8fb26252007-08-22 05:28:50 +00001304 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001305 // there is no compound stmt. C90 does not have this clause. We only do this
1306 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001307 //
1308 // C++ 6.4p1:
1309 // The substatement in a selection-statement (each substatement, in the else
1310 // form of the if statement) implicitly defines a local scope.
1311 //
1312 // See comments in ParseIfStatement for why we create a scope for the
1313 // condition and a new scope for substatement in C++.
1314 //
Serge Pavlov09f99242014-01-23 15:05:00 +00001315 getCurScope()->AddFlags(Scope::BreakScope);
David Majnemer2206bf52014-03-05 08:57:59 +00001316 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +00001317
Hans Wennborg852c3462014-06-17 00:09:05 +00001318 // We have incremented the mangling number for the SwitchScope and the
1319 // InnerScope, which is one too many.
1320 if (C99orCXX)
David Majnemera7f8c462015-03-19 21:54:30 +00001321 getCurScope()->decrementMSManglingNumber();
Hans Wennborg852c3462014-06-17 00:09:05 +00001322
Chris Lattner9075bd72006-08-10 04:59:57 +00001323 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001324 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001325
Chris Lattner8fd2d012010-01-24 01:50:29 +00001326 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001327 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001328 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001329
John McCallb268a282010-08-23 23:25:46 +00001330 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001331}
1332
1333/// ParseWhileStatement
1334/// while-statement: [C99 6.8.5.1]
1335/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001336/// [C++] 'while' '(' condition ')' statement
Richard Smithc202b282012-04-14 00:33:13 +00001337StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001338 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +00001339 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +00001340 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001341
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001342 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001343 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +00001344 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001345 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001346 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001347
David Blaikiebbafb8a2012-03-11 07:00:24 +00001348 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001349
Chris Lattner2dd1b722007-08-26 23:08:06 +00001350 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1351 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001352 //
1353 // C++ 6.4p3:
1354 // A name introduced by a declaration in a condition is in scope from its
1355 // point of declaration until the end of the substatements controlled by the
1356 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001357 // C++ 3.3.2p4:
1358 // Names declared in the for-init-statement, and in the condition of if,
1359 // while, for, and switch statements are local to the if, while, for, or
1360 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001361 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001362 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001363 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001364 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1365 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001366 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001367 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1368 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001369
Chris Lattner9075bd72006-08-10 04:59:57 +00001370 // Parse the condition.
Richard Smith03a4aa32016-06-23 19:02:52 +00001371 Sema::ConditionResult Cond;
Richard Smithc7a05a92016-06-29 21:17:59 +00001372 if (ParseParenExprOrCondition(nullptr, Cond, WhileLoc,
1373 Sema::ConditionKind::Boolean))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001374 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001375
Justin Bognere4ebb6c2013-12-03 07:36:55 +00001376 // C99 6.8.5p5 - In C99, the body of the while statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001377 // there is no compound stmt. C90 does not have this clause. We only do this
1378 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001379 //
1380 // C++ 6.5p2:
1381 // The substatement in an iteration-statement implicitly defines a local scope
1382 // which is entered and exited each time through the loop.
1383 //
1384 // See comments in ParseIfStatement for why we create a scope for the
1385 // condition and a new scope for substatement in C++.
1386 //
David Majnemer2206bf52014-03-05 08:57:59 +00001387 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001388
Chris Lattner9075bd72006-08-10 04:59:57 +00001389 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001390 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001391
Chris Lattner8fb26252007-08-22 05:28:50 +00001392 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001393 InnerScope.Exit();
1394 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001395
Richard Smith03a4aa32016-06-23 19:02:52 +00001396 if (Cond.isInvalid() || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001397 return StmtError();
1398
Richard Smith03a4aa32016-06-23 19:02:52 +00001399 return Actions.ActOnWhileStmt(WhileLoc, Cond, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001400}
1401
1402/// ParseDoStatement
1403/// do-statement: [C99 6.8.5.2]
1404/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +00001405/// Note: this lets the caller parse the end ';'.
Richard Smithc202b282012-04-14 00:33:13 +00001406StmtResult Parser::ParseDoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001407 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001408 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001409
Chris Lattner2dd1b722007-08-26 23:08:06 +00001410 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1411 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001412 unsigned ScopeFlags;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001413 if (getLangOpts().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001414 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001415 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001416 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001417
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001418 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001419
Justin Bognere4ebb6c2013-12-03 07:36:55 +00001420 // C99 6.8.5p5 - In C99, the body of the do statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001421 // there is no compound stmt. C90 does not have this clause. We only do this
1422 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +00001423 //
1424 // C++ 6.5p2:
1425 // The substatement in an iteration-statement implicitly defines a local scope
1426 // which is entered and exited each time through the loop.
1427 //
David Majnemer2206bf52014-03-05 08:57:59 +00001428 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
1429 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001430
Chris Lattner9075bd72006-08-10 04:59:57 +00001431 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001432 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +00001433
Chris Lattner8fb26252007-08-22 05:28:50 +00001434 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001435 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001436
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001437 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001438 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +00001439 Diag(Tok, diag::err_expected_while);
Alp Tokerec543272013-12-24 09:48:30 +00001440 Diag(DoLoc, diag::note_matching) << "'do'";
Alexey Bataevee6507d2013-11-18 08:17:37 +00001441 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner0046de12008-11-13 18:52:53 +00001442 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001443 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001444 }
Chris Lattneraf635312006-10-16 06:06:51 +00001445 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001446
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001447 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001448 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Alexey Bataevee6507d2013-11-18 08:17:37 +00001449 SkipUntil(tok::semi, StopBeforeMatch);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001450 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001451 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001452
Richard Smithc2c8bb82013-10-15 01:34:54 +00001453 // Parse the parenthesized expression.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001454 BalancedDelimiterTracker T(*this, tok::l_paren);
1455 T.consumeOpen();
Chad Rosier67055f52012-07-10 21:35:27 +00001456
Richard Smithc2c8bb82013-10-15 01:34:54 +00001457 // A do-while expression is not a condition, so can't have attributes.
1458 DiagnoseAndSkipCXX11Attributes();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001459
John McCalldadc5752010-08-24 06:29:42 +00001460 ExprResult Cond = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001461 T.consumeClose();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001462 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001463
Sebastian Redlb62406f2008-12-11 19:48:14 +00001464 if (Cond.isInvalid() || Body.isInvalid())
1465 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001466
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001467 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(),
1468 Cond.get(), T.getCloseLocation());
Chris Lattner9075bd72006-08-10 04:59:57 +00001469}
1470
Richard Smith955bf012014-06-19 11:42:00 +00001471bool Parser::isForRangeIdentifier() {
1472 assert(Tok.is(tok::identifier));
1473
1474 const Token &Next = NextToken();
1475 if (Next.is(tok::colon))
1476 return true;
1477
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001478 if (Next.isOneOf(tok::l_square, tok::kw_alignas)) {
Richard Smith955bf012014-06-19 11:42:00 +00001479 TentativeParsingAction PA(*this);
1480 ConsumeToken();
1481 SkipCXX11Attributes();
1482 bool Result = Tok.is(tok::colon);
1483 PA.Revert();
1484 return Result;
1485 }
1486
1487 return false;
1488}
1489
Chris Lattner9075bd72006-08-10 04:59:57 +00001490/// ParseForStatement
1491/// for-statement: [C99 6.8.5.3]
1492/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1493/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001494/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1495/// [C++] statement
Richard Smith0e304ea2015-10-22 04:46:14 +00001496/// [C++0x] 'for'
1497/// 'co_await'[opt] [Coroutines]
1498/// '(' for-range-declaration ':' for-range-initializer ')'
1499/// statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001500/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1501/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001502///
1503/// [C++] for-init-statement:
1504/// [C++] expression-statement
1505/// [C++] simple-declaration
1506///
Richard Smith02e85f32011-04-14 22:09:26 +00001507/// [C++0x] for-range-declaration:
1508/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1509/// [C++0x] for-range-initializer:
1510/// [C++0x] expression
1511/// [C++0x] braced-init-list [TODO]
Richard Smithc202b282012-04-14 00:33:13 +00001512StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001513 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001514 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001515
Richard Smith0e304ea2015-10-22 04:46:14 +00001516 SourceLocation CoawaitLoc;
1517 if (Tok.is(tok::kw_co_await))
1518 CoawaitLoc = ConsumeToken();
1519
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001520 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001521 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +00001522 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001523 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001524 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001525
Chad Rosier67055f52012-07-10 21:35:27 +00001526 bool C99orCXXorObjC = getLangOpts().C99 || getLangOpts().CPlusPlus ||
1527 getLangOpts().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001528
Chris Lattner2dd1b722007-08-26 23:08:06 +00001529 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1530 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001531 //
1532 // C++ 6.4p3:
1533 // A name introduced by a declaration in a condition is in scope from its
1534 // point of declaration until the end of the substatements controlled by the
1535 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001536 // C++ 3.3.2p4:
1537 // Names declared in the for-init-statement, and in the condition of if,
1538 // while, for, and switch statements are local to the if, while, for, or
1539 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001540 // C++ 6.5.3p1:
1541 // Names declared in the for-init-statement are in the same declarative-region
1542 // as those declared in the condition.
1543 //
Serge Pavlov09f99242014-01-23 15:05:00 +00001544 unsigned ScopeFlags = 0;
Chris Lattner934074c2009-04-22 00:54:41 +00001545 if (C99orCXXorObjC)
Serge Pavlov09f99242014-01-23 15:05:00 +00001546 ScopeFlags = Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001547
1548 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +00001549
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001550 BalancedDelimiterTracker T(*this, tok::l_paren);
1551 T.consumeOpen();
1552
John McCalldadc5752010-08-24 06:29:42 +00001553 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001554
Richard Smith02e85f32011-04-14 22:09:26 +00001555 bool ForEach = false, ForRange = false;
John McCalldadc5752010-08-24 06:29:42 +00001556 StmtResult FirstPart;
Richard Smith03a4aa32016-06-23 19:02:52 +00001557 Sema::ConditionResult SecondPart;
John McCalldadc5752010-08-24 06:29:42 +00001558 ExprResult Collection;
Richard Smith02e85f32011-04-14 22:09:26 +00001559 ForRangeInit ForRangeInit;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001560 FullExprArg ThirdPart(Actions);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001561
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001562 if (Tok.is(tok::code_completion)) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001563 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001564 C99orCXXorObjC? Sema::PCC_ForInit
1565 : Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001566 cutOffParsing();
1567 return StmtError();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001568 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001569
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001570 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001571 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001572
Chris Lattner9075bd72006-08-10 04:59:57 +00001573 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001574 if (Tok.is(tok::semi)) { // for (;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001575 ProhibitAttributes(attrs);
Chris Lattner53361ac2006-08-10 05:19:57 +00001576 // no first part, eat the ';'.
1577 ConsumeToken();
Richard Smith955bf012014-06-19 11:42:00 +00001578 } else if (getLangOpts().CPlusPlus && Tok.is(tok::identifier) &&
1579 isForRangeIdentifier()) {
1580 ProhibitAttributes(attrs);
1581 IdentifierInfo *Name = Tok.getIdentifierInfo();
1582 SourceLocation Loc = ConsumeToken();
1583 MaybeParseCXX11Attributes(attrs);
1584
1585 ForRangeInit.ColonLoc = ConsumeToken();
1586 if (Tok.is(tok::l_brace))
1587 ForRangeInit.RangeExpr = ParseBraceInitializer();
1588 else
1589 ForRangeInit.RangeExpr = ParseExpression();
1590
Richard Smith83d3f152014-11-27 01:54:27 +00001591 Diag(Loc, diag::err_for_range_identifier)
Richard Smith955bf012014-06-19 11:42:00 +00001592 << ((getLangOpts().CPlusPlus11 && !getLangOpts().CPlusPlus1z)
1593 ? FixItHint::CreateInsertion(Loc, "auto &&")
1594 : FixItHint());
1595
1596 FirstPart = Actions.ActOnCXXForRangeIdentifier(getCurScope(), Loc, Name,
1597 attrs, attrs.Range.getEnd());
1598 ForRange = true;
Eli Friedman0ffc31c2011-12-20 01:50:37 +00001599 } else if (isForInitDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001600 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001601 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001602 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001603
Richard Smith02e85f32011-04-14 22:09:26 +00001604 // In C++0x, "for (T NS:a" might not be a typo for ::
David Blaikiebbafb8a2012-03-11 07:00:24 +00001605 bool MightBeForRangeStmt = getLangOpts().CPlusPlus;
Richard Smith02e85f32011-04-14 22:09:26 +00001606 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1607
Chris Lattner49836b42009-04-02 04:16:50 +00001608 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00001609 DeclGroupPtrTy DG = ParseSimpleDeclaration(
Rafael Espindola1bd906d2014-10-22 14:27:08 +00001610 Declarator::ForContext, DeclEnd, attrs, false,
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00001611 MightBeForRangeStmt ? &ForRangeInit : nullptr);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001612 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Richard Smith02e85f32011-04-14 22:09:26 +00001613 if (ForRangeInit.ParsedForRangeDecl()) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001614 Diag(ForRangeInit.ColonLoc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00001615 diag::warn_cxx98_compat_for_range : diag::ext_for_range);
Richard Smith58c74332011-09-04 19:54:14 +00001616
Richard Smith02e85f32011-04-14 22:09:26 +00001617 ForRange = true;
1618 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattner32dc41c2009-03-29 17:27:48 +00001619 ConsumeToken();
1620 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001621 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001622 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001623 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001624
Douglas Gregor68762e72010-08-23 21:17:50 +00001625 if (Tok.is(tok::code_completion)) {
1626 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001627 cutOffParsing();
1628 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001629 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001630 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001631 } else {
1632 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001633 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001634 } else {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001635 ProhibitAttributes(attrs);
Kaelyn Takatab16e6322014-11-20 22:06:40 +00001636 Value = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Chris Lattner71e23ce2006-11-04 20:18:38 +00001637
John McCall34376a62010-12-04 03:47:34 +00001638 ForEach = isTokIdentifier_in();
1639
Chris Lattnercd68f642007-06-27 01:06:29 +00001640 // Turn the expression into a stmt.
John McCall34376a62010-12-04 03:47:34 +00001641 if (!Value.isInvalid()) {
1642 if (ForEach)
1643 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1644 else
Richard Smith945f8d32013-01-14 22:39:08 +00001645 FirstPart = Actions.ActOnExprStmt(Value);
John McCall34376a62010-12-04 03:47:34 +00001646 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001647
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001648 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001649 ConsumeToken();
John McCall34376a62010-12-04 03:47:34 +00001650 } else if (ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001651 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001652
Douglas Gregor68762e72010-08-23 21:17:50 +00001653 if (Tok.is(tok::code_completion)) {
David Blaikie0403cb12016-01-15 23:43:25 +00001654 Actions.CodeCompleteObjCForCollection(getCurScope(), nullptr);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001655 cutOffParsing();
1656 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001657 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001658 Collection = ParseExpression();
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001659 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::colon) && FirstPart.get()) {
Richard Smith4f848f12011-12-20 22:56:20 +00001660 // User tried to write the reasonable, but ill-formed, for-range-statement
1661 // for (expr : expr) { ... }
1662 Diag(Tok, diag::err_for_range_expected_decl)
1663 << FirstPart.get()->getSourceRange();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001664 SkipUntil(tok::r_paren, StopBeforeMatch);
Richard Smith03a4aa32016-06-23 19:02:52 +00001665 SecondPart = Sema::ConditionError();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001666 } else {
Douglas Gregor230a7e62011-02-17 03:38:46 +00001667 if (!Value.isInvalid()) {
1668 Diag(Tok, diag::err_expected_semi_for);
1669 } else {
1670 // Skip until semicolon or rparen, don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001671 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor230a7e62011-02-17 03:38:46 +00001672 if (Tok.is(tok::semi))
1673 ConsumeToken();
1674 }
Chris Lattner53361ac2006-08-10 05:19:57 +00001675 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001676 }
Serge Pavlov09f99242014-01-23 15:05:00 +00001677
1678 // Parse the second part of the for specifier.
1679 getCurScope()->AddFlags(Scope::BreakScope | Scope::ContinueScope);
Richard Smith03a4aa32016-06-23 19:02:52 +00001680 if (!ForEach && !ForRange && !SecondPart.isInvalid()) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001681 // Parse the second part of the for specifier.
1682 if (Tok.is(tok::semi)) { // for (...;;
1683 // no second part.
Douglas Gregor230a7e62011-02-17 03:38:46 +00001684 } else if (Tok.is(tok::r_paren)) {
1685 // missing both semicolons.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001686 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001687 if (getLangOpts().CPlusPlus)
Richard Smithc7a05a92016-06-29 21:17:59 +00001688 SecondPart =
1689 ParseCXXCondition(nullptr, ForLoc, Sema::ConditionKind::Boolean);
Douglas Gregore60e41a2010-05-06 17:25:47 +00001690 else {
Richard Smith03a4aa32016-06-23 19:02:52 +00001691 ExprResult SecondExpr = ParseExpression();
1692 if (SecondExpr.isInvalid())
1693 SecondPart = Sema::ConditionError();
1694 else
1695 SecondPart =
1696 Actions.ActOnCondition(getCurScope(), ForLoc, SecondExpr.get(),
1697 Sema::ConditionKind::Boolean);
Douglas Gregore60e41a2010-05-06 17:25:47 +00001698 }
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001699 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001700
Douglas Gregor230a7e62011-02-17 03:38:46 +00001701 if (Tok.isNot(tok::semi)) {
Richard Smith03a4aa32016-06-23 19:02:52 +00001702 if (!SecondPart.isInvalid())
Douglas Gregor230a7e62011-02-17 03:38:46 +00001703 Diag(Tok, diag::err_expected_semi_for);
1704 else
1705 // Skip until semicolon or rparen, don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001706 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor230a7e62011-02-17 03:38:46 +00001707 }
1708
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001709 if (Tok.is(tok::semi)) {
1710 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001711 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001712
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001713 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001714 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001715 ExprResult Third = ParseExpression();
Richard Smith945f8d32013-01-14 22:39:08 +00001716 // FIXME: The C++11 standard doesn't actually say that this is a
1717 // discarded-value expression, but it clearly should be.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001718 ThirdPart = Actions.MakeFullDiscardedValueExpr(Third.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001719 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001720 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001721 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001722 T.consumeClose();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001723
Richard Smith0e304ea2015-10-22 04:46:14 +00001724 // C++ Coroutines [stmt.iter]:
1725 // 'co_await' can only be used for a range-based for statement.
1726 if (CoawaitLoc.isValid() && !ForRange) {
1727 Diag(CoawaitLoc, diag::err_for_co_await_not_range_for);
1728 CoawaitLoc = SourceLocation();
1729 }
1730
Richard Smith02e85f32011-04-14 22:09:26 +00001731 // We need to perform most of the semantic analysis for a C++0x for-range
1732 // statememt before parsing the body, in order to be able to deduce the type
1733 // of an auto-typed loop variable.
1734 StmtResult ForRangeStmt;
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001735 StmtResult ForEachStmt;
Chad Rosier67055f52012-07-10 21:35:27 +00001736
John McCall53848232011-07-27 01:07:15 +00001737 if (ForRange) {
Denis Zobnin7d6b9242016-02-02 17:33:09 +00001738 ExprResult CorrectedRange =
1739 Actions.CorrectDelayedTyposInExpr(ForRangeInit.RangeExpr.get());
Richard Smith9f690bd2015-10-27 06:02:45 +00001740 ForRangeStmt = Actions.ActOnCXXForRangeStmt(
1741 getCurScope(), ForLoc, CoawaitLoc, FirstPart.get(),
Denis Zobnin7d6b9242016-02-02 17:33:09 +00001742 ForRangeInit.ColonLoc, CorrectedRange.get(),
Richard Smith9f690bd2015-10-27 06:02:45 +00001743 T.getCloseLocation(), Sema::BFRK_Build);
John McCall53848232011-07-27 01:07:15 +00001744
1745 // Similarly, we need to do the semantic analysis for a for-range
1746 // statement immediately in order to close over temporaries correctly.
1747 } else if (ForEach) {
Sam Panzer2c4ca0f2012-08-16 21:47:25 +00001748 ForEachStmt = Actions.ActOnObjCForCollectionStmt(ForLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001749 FirstPart.get(),
1750 Collection.get(),
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001751 T.getCloseLocation());
Alexey Bataev9c821032015-04-30 04:23:23 +00001752 } else {
1753 // In OpenMP loop region loop control variable must be captured and be
1754 // private. Perform analysis of first part (if any).
1755 if (getLangOpts().OpenMP && FirstPart.isUsable()) {
1756 Actions.ActOnOpenMPLoopInitialization(ForLoc, FirstPart.get());
1757 }
John McCall53848232011-07-27 01:07:15 +00001758 }
1759
Justin Bognere4ebb6c2013-12-03 07:36:55 +00001760 // C99 6.8.5p5 - In C99, the body of the for statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001761 // there is no compound stmt. C90 does not have this clause. We only do this
1762 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001763 //
1764 // C++ 6.5p2:
1765 // The substatement in an iteration-statement implicitly defines a local scope
1766 // which is entered and exited each time through the loop.
1767 //
1768 // See comments in ParseIfStatement for why we create a scope for
1769 // for-init-statement/condition and a new scope for substatement in C++.
1770 //
David Majnemer2206bf52014-03-05 08:57:59 +00001771 ParseScope InnerScope(this, Scope::DeclScope, C99orCXXorObjC,
1772 Tok.is(tok::l_brace));
1773
1774 // The body of the for loop has the same local mangling number as the
1775 // for-init-statement.
1776 // It will only be incremented if the body contains other things that would
1777 // normally increment the mangling number (like a compound statement).
1778 if (C99orCXXorObjC)
David Majnemera7f8c462015-03-19 21:54:30 +00001779 getCurScope()->decrementMSManglingNumber();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001780
Chris Lattner9075bd72006-08-10 04:59:57 +00001781 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001782 StmtResult Body(ParseStatement(TrailingElseLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001783
Chris Lattner8fb26252007-08-22 05:28:50 +00001784 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001785 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001786
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001787 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001788 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001789
1790 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001791 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001792
Richard Smith02e85f32011-04-14 22:09:26 +00001793 if (ForEach)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001794 return Actions.FinishObjCForCollectionStmt(ForEachStmt.get(),
1795 Body.get());
Mike Stump11289f42009-09-09 15:08:12 +00001796
Richard Smith02e85f32011-04-14 22:09:26 +00001797 if (ForRange)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001798 return Actions.FinishCXXForRangeStmt(ForRangeStmt.get(), Body.get());
Richard Smith02e85f32011-04-14 22:09:26 +00001799
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001800 return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.get(),
Richard Smith03a4aa32016-06-23 19:02:52 +00001801 SecondPart, ThirdPart, T.getCloseLocation(),
1802 Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001803}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001804
Chris Lattner503fadc2006-08-10 05:45:44 +00001805/// ParseGotoStatement
1806/// jump-statement:
1807/// 'goto' identifier ';'
1808/// [GNU] 'goto' '*' expression ';'
1809///
1810/// Note: this lets the caller parse the end ';'.
1811///
Richard Smithc202b282012-04-14 00:33:13 +00001812StmtResult Parser::ParseGotoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001813 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001814 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001815
John McCalldadc5752010-08-24 06:29:42 +00001816 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001817 if (Tok.is(tok::identifier)) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +00001818 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1819 Tok.getLocation());
1820 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Chris Lattner503fadc2006-08-10 05:45:44 +00001821 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001822 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001823 // GNU indirect goto extension.
1824 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001825 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001826 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001827 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001828 SkipUntil(tok::semi, StopBeforeMatch);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001829 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001830 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001831 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.get());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001832 } else {
Alp Tokerec543272013-12-24 09:48:30 +00001833 Diag(Tok, diag::err_expected) << tok::identifier;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001834 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001835 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001836
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001837 return Res;
Chris Lattner503fadc2006-08-10 05:45:44 +00001838}
1839
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001840/// ParseContinueStatement
1841/// jump-statement:
1842/// 'continue' ';'
1843///
1844/// Note: this lets the caller parse the end ';'.
1845///
Richard Smithc202b282012-04-14 00:33:13 +00001846StmtResult Parser::ParseContinueStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001847 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001848 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001849}
1850
1851/// ParseBreakStatement
1852/// jump-statement:
1853/// 'break' ';'
1854///
1855/// Note: this lets the caller parse the end ';'.
1856///
Richard Smithc202b282012-04-14 00:33:13 +00001857StmtResult Parser::ParseBreakStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001858 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001859 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001860}
1861
Chris Lattner503fadc2006-08-10 05:45:44 +00001862/// ParseReturnStatement
1863/// jump-statement:
1864/// 'return' expression[opt] ';'
Richard Smith0e304ea2015-10-22 04:46:14 +00001865/// 'return' braced-init-list ';'
1866/// 'co_return' expression[opt] ';'
1867/// 'co_return' braced-init-list ';'
Richard Smithc202b282012-04-14 00:33:13 +00001868StmtResult Parser::ParseReturnStatement() {
Richard Smith0e304ea2015-10-22 04:46:14 +00001869 assert((Tok.is(tok::kw_return) || Tok.is(tok::kw_co_return)) &&
1870 "Not a return stmt!");
1871 bool IsCoreturn = Tok.is(tok::kw_co_return);
Chris Lattneraf635312006-10-16 06:06:51 +00001872 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001873
John McCalldadc5752010-08-24 06:29:42 +00001874 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001875 if (Tok.isNot(tok::semi)) {
Richard Smith0e304ea2015-10-22 04:46:14 +00001876 // FIXME: Code completion for co_return.
1877 if (Tok.is(tok::code_completion) && !IsCoreturn) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001878 Actions.CodeCompleteReturn(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001879 cutOffParsing();
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001880 return StmtError();
1881 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001882
David Blaikiebbafb8a2012-03-11 07:00:24 +00001883 if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus) {
Douglas Gregore9e27d92011-03-11 23:10:44 +00001884 R = ParseInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00001885 if (R.isUsable())
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001886 Diag(R.get()->getLocStart(), getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00001887 diag::warn_cxx98_compat_generalized_initializer_lists :
1888 diag::ext_generalized_initializer_lists)
Douglas Gregore9e27d92011-03-11 23:10:44 +00001889 << R.get()->getSourceRange();
1890 } else
Nico Weber3ce01c32015-01-04 08:07:54 +00001891 R = ParseExpression();
Serge Pavlovf79bd5c2013-12-04 03:51:59 +00001892 if (R.isInvalid()) {
1893 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001894 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001895 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001896 }
Richard Smithcfd53b42015-10-22 06:13:50 +00001897 if (IsCoreturn)
1898 return Actions.ActOnCoreturnStmt(ReturnLoc, R.get());
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001899 return Actions.ActOnReturnStmt(ReturnLoc, R.get(), getCurScope());
Chris Lattner503fadc2006-08-10 05:45:44 +00001900}
Chris Lattner0116c472006-08-15 06:03:28 +00001901
Alexey Bataevc4fad652016-01-13 11:18:54 +00001902StmtResult Parser::ParsePragmaLoopHint(StmtVector &Stmts,
1903 AllowedContsructsKind Allowed,
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001904 SourceLocation *TrailingElseLoc,
1905 ParsedAttributesWithRange &Attrs) {
1906 // Create temporary attribute list.
1907 ParsedAttributesWithRange TempAttrs(AttrFactory);
1908
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001909 // Get loop hints and consume annotated token.
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001910 while (Tok.is(tok::annot_pragma_loop_hint)) {
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001911 LoopHint Hint;
1912 if (!HandlePragmaLoopHint(Hint))
1913 continue;
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001914
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001915 ArgsUnion ArgHints[] = {Hint.PragmaNameLoc, Hint.OptionLoc, Hint.StateLoc,
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001916 ArgsUnion(Hint.ValueExpr)};
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001917 TempAttrs.addNew(Hint.PragmaNameLoc->Ident, Hint.Range, nullptr,
1918 Hint.PragmaNameLoc->Loc, ArgHints, 4,
1919 AttributeList::AS_Pragma);
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001920 }
1921
1922 // Get the next statement.
1923 MaybeParseCXX11Attributes(Attrs);
1924
1925 StmtResult S = ParseStatementOrDeclarationAfterAttributes(
Alexey Bataevc4fad652016-01-13 11:18:54 +00001926 Stmts, Allowed, TrailingElseLoc, Attrs);
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001927
1928 Attrs.takeAllFrom(TempAttrs);
1929 return S;
1930}
1931
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001932Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001933 assert(Tok.is(tok::l_brace));
1934 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001935
John McCallfaf5fb42010-08-26 23:41:50 +00001936 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1937 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001938
Alexey Bataev3d42f342015-11-20 07:02:57 +00001939 // Save and reset current vtordisp stack if we have entered a C++ method body.
1940 bool IsCXXMethod =
1941 getLangOpts().CPlusPlus && Decl && isa<CXXMethodDecl>(Decl);
Denis Zobnin2290dac2016-04-29 11:27:00 +00001942 Sema::PragmaStackSentinelRAII
1943 PragmaStackSentinel(Actions, "InternalPragmaState", IsCXXMethod);
Alexey Bataev3d42f342015-11-20 07:02:57 +00001944
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001945 // Do not enter a scope for the brace, as the arguments are in the same scope
1946 // (the function body) as the body itself. Instead, just read the statement
1947 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00001948 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001949
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001950 // If the function body could not be parsed, make a bogus compoundstmt.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001951 if (FnBody.isInvalid()) {
1952 Sema::CompoundScopeRAII CompoundScope(Actions);
Robert Wilhelm27b2c9a32013-08-19 20:51:20 +00001953 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001954 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001955
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001956 BodyScope.Exit();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001957 return Actions.ActOnFinishFunctionBody(Decl, FnBody.get());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001958}
Sebastian Redlb219c902008-12-21 16:41:36 +00001959
Sebastian Redla7b98a72009-04-26 20:35:05 +00001960/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1961///
1962/// function-try-block:
1963/// 'try' ctor-initializer[opt] compound-statement handler-seq
1964///
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001965Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00001966 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1967 SourceLocation TryLoc = ConsumeToken();
1968
John McCallfaf5fb42010-08-26 23:41:50 +00001969 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1970 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00001971
1972 // Constructor initializer list?
1973 if (Tok.is(tok::colon))
1974 ParseConstructorInitializer(Decl);
Douglas Gregor5ca153f2011-09-07 20:36:12 +00001975 else
1976 Actions.ActOnDefaultCtorInitializers(Decl);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001977
Alexey Bataev3d42f342015-11-20 07:02:57 +00001978 // Save and reset current vtordisp stack if we have entered a C++ method body.
1979 bool IsCXXMethod =
1980 getLangOpts().CPlusPlus && Decl && isa<CXXMethodDecl>(Decl);
Denis Zobnin2290dac2016-04-29 11:27:00 +00001981 Sema::PragmaStackSentinelRAII
1982 PragmaStackSentinel(Actions, "InternalPragmaState", IsCXXMethod);
Alexey Bataev3d42f342015-11-20 07:02:57 +00001983
Sebastian Redld98ecd62009-04-26 21:08:36 +00001984 SourceLocation LBraceLoc = Tok.getLocation();
David Blaikie1c9c9042012-11-10 01:04:23 +00001985 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc, /*FnTry*/true));
Sebastian Redla7b98a72009-04-26 20:35:05 +00001986 // If we failed to parse the try-catch, we just give the function an empty
1987 // compound statement as the body.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001988 if (FnBody.isInvalid()) {
1989 Sema::CompoundScopeRAII CompoundScope(Actions);
Robert Wilhelm27b2c9a32013-08-19 20:51:20 +00001990 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001991 }
Sebastian Redla7b98a72009-04-26 20:35:05 +00001992
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001993 BodyScope.Exit();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001994 return Actions.ActOnFinishFunctionBody(Decl, FnBody.get());
Sebastian Redla7b98a72009-04-26 20:35:05 +00001995}
1996
Erik Verbruggen6e922512012-04-12 10:11:59 +00001997bool Parser::trySkippingFunctionBody() {
Erik Verbruggen6e922512012-04-12 10:11:59 +00001998 assert(SkipFunctionBodies &&
1999 "Should only be called when SkipFunctionBodies is enabled");
Argyrios Kyrtzidis289e4a32012-10-31 17:29:28 +00002000 if (!PP.isCodeCompletionEnabled()) {
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002001 SkipFunctionBody();
Argyrios Kyrtzidis289e4a32012-10-31 17:29:28 +00002002 return true;
2003 }
2004
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002005 // We're in code-completion mode. Skip parsing for all function bodies unless
2006 // the body contains the code-completion point.
2007 TentativeParsingAction PA(*this);
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002008 bool IsTryCatch = Tok.is(tok::kw_try);
2009 CachedTokens Toks;
2010 bool ErrorInPrologue = ConsumeAndStoreFunctionPrologue(Toks);
2011 if (llvm::any_of(Toks, [](const Token &Tok) {
2012 return Tok.is(tok::code_completion);
2013 })) {
2014 PA.Revert();
2015 return false;
2016 }
2017 if (ErrorInPrologue) {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002018 PA.Commit();
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002019 SkipMalformedDecl();
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002020 return true;
2021 }
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002022 if (!SkipUntil(tok::r_brace, StopAtCodeCompletion)) {
2023 PA.Revert();
2024 return false;
2025 }
2026 while (IsTryCatch && Tok.is(tok::kw_catch)) {
2027 if (!SkipUntil(tok::l_brace, StopAtCodeCompletion) ||
2028 !SkipUntil(tok::r_brace, StopAtCodeCompletion)) {
2029 PA.Revert();
2030 return false;
2031 }
2032 }
2033 PA.Commit();
2034 return true;
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002035}
2036
Sebastian Redlb219c902008-12-21 16:41:36 +00002037/// ParseCXXTryBlock - Parse a C++ try-block.
2038///
2039/// try-block:
2040/// 'try' compound-statement handler-seq
2041///
Richard Smithc202b282012-04-14 00:33:13 +00002042StmtResult Parser::ParseCXXTryBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00002043 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2044
2045 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00002046 return ParseCXXTryBlockCommon(TryLoc);
2047}
2048
2049/// ParseCXXTryBlockCommon - Parse the common part of try-block and
2050/// function-try-block.
2051///
2052/// try-block:
2053/// 'try' compound-statement handler-seq
2054///
2055/// function-try-block:
2056/// 'try' ctor-initializer[opt] compound-statement handler-seq
2057///
2058/// handler-seq:
2059/// handler handler-seq[opt]
2060///
John Wiegley1c0675e2011-04-28 01:08:34 +00002061/// [Borland] try-block:
2062/// 'try' compound-statement seh-except-block
Alp Tokerf6a24ce2013-12-05 16:25:25 +00002063/// 'try' compound-statement seh-finally-block
John Wiegley1c0675e2011-04-28 01:08:34 +00002064///
David Blaikie1c9c9042012-11-10 01:04:23 +00002065StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc, bool FnTry) {
Sebastian Redlb219c902008-12-21 16:41:36 +00002066 if (Tok.isNot(tok::l_brace))
Alp Tokerec543272013-12-24 09:48:30 +00002067 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
Richard Smithc202b282012-04-14 00:33:13 +00002068
Warren Huntf6be4cb2014-07-25 20:52:51 +00002069 StmtResult TryBlock(ParseCompoundStatement(/*isStmtExpr=*/false,
2070 Scope::DeclScope | Scope::TryScope |
2071 (FnTry ? Scope::FnTryCatchScope : 0)));
Sebastian Redlb219c902008-12-21 16:41:36 +00002072 if (TryBlock.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002073 return TryBlock;
Sebastian Redlb219c902008-12-21 16:41:36 +00002074
John Wiegley1c0675e2011-04-28 01:08:34 +00002075 // Borland allows SEH-handlers with 'try'
Chad Rosier67055f52012-07-10 21:35:27 +00002076
Richard Smithc202b282012-04-14 00:33:13 +00002077 if ((Tok.is(tok::identifier) &&
2078 Tok.getIdentifierInfo() == getSEHExceptKeyword()) ||
2079 Tok.is(tok::kw___finally)) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002080 // TODO: Factor into common return ParseSEHHandlerCommon(...)
2081 StmtResult Handler;
Douglas Gregor60060d62011-10-21 03:57:52 +00002082 if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002083 SourceLocation Loc = ConsumeToken();
2084 Handler = ParseSEHExceptBlock(Loc);
2085 }
2086 else {
2087 SourceLocation Loc = ConsumeToken();
2088 Handler = ParseSEHFinallyBlock(Loc);
2089 }
2090 if(Handler.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002091 return Handler;
John McCall53fa7142010-12-24 02:08:15 +00002092
John Wiegley1c0675e2011-04-28 01:08:34 +00002093 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
2094 TryLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002095 TryBlock.get(),
Warren Huntf6be4cb2014-07-25 20:52:51 +00002096 Handler.get());
Sebastian Redlb219c902008-12-21 16:41:36 +00002097 }
John Wiegley1c0675e2011-04-28 01:08:34 +00002098 else {
Benjamin Kramerf0623432012-08-23 22:51:59 +00002099 StmtVector Handlers;
Richard Smithc2c8bb82013-10-15 01:34:54 +00002100
2101 // C++11 attributes can't appear here, despite this context seeming
2102 // statement-like.
2103 DiagnoseAndSkipCXX11Attributes();
Sebastian Redlb219c902008-12-21 16:41:36 +00002104
John Wiegley1c0675e2011-04-28 01:08:34 +00002105 if (Tok.isNot(tok::kw_catch))
2106 return StmtError(Diag(Tok, diag::err_expected_catch));
2107 while (Tok.is(tok::kw_catch)) {
David Blaikie1c9c9042012-11-10 01:04:23 +00002108 StmtResult Handler(ParseCXXCatchBlock(FnTry));
John Wiegley1c0675e2011-04-28 01:08:34 +00002109 if (!Handler.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002110 Handlers.push_back(Handler.get());
John Wiegley1c0675e2011-04-28 01:08:34 +00002111 }
2112 // Don't bother creating the full statement if we don't have any usable
2113 // handlers.
2114 if (Handlers.empty())
2115 return StmtError();
2116
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002117 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.get(), Handlers);
John Wiegley1c0675e2011-04-28 01:08:34 +00002118 }
Sebastian Redlb219c902008-12-21 16:41:36 +00002119}
2120
2121/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
2122///
Richard Smith1dba27c2013-01-29 09:02:09 +00002123/// handler:
2124/// 'catch' '(' exception-declaration ')' compound-statement
Sebastian Redlb219c902008-12-21 16:41:36 +00002125///
Richard Smith1dba27c2013-01-29 09:02:09 +00002126/// exception-declaration:
2127/// attribute-specifier-seq[opt] type-specifier-seq declarator
2128/// attribute-specifier-seq[opt] type-specifier-seq abstract-declarator[opt]
2129/// '...'
Sebastian Redlb219c902008-12-21 16:41:36 +00002130///
David Blaikie1c9c9042012-11-10 01:04:23 +00002131StmtResult Parser::ParseCXXCatchBlock(bool FnCatch) {
Sebastian Redlb219c902008-12-21 16:41:36 +00002132 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
2133
2134 SourceLocation CatchLoc = ConsumeToken();
2135
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002136 BalancedDelimiterTracker T(*this, tok::l_paren);
Alp Toker383d2c42014-01-01 03:08:43 +00002137 if (T.expectAndConsume())
Sebastian Redlb219c902008-12-21 16:41:36 +00002138 return StmtError();
2139
2140 // C++ 3.3.2p3:
2141 // The name in a catch exception-declaration is local to the handler and
2142 // shall not be redeclared in the outermost block of the handler.
David Blaikie1c9c9042012-11-10 01:04:23 +00002143 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope |
David Blaikie3403feb2012-11-13 18:51:45 +00002144 (FnCatch ? Scope::FnTryCatchScope : 0));
Sebastian Redlb219c902008-12-21 16:41:36 +00002145
2146 // exception-declaration is equivalent to '...' or a parameter-declaration
2147 // without default arguments.
Craig Topper161e4db2014-05-21 06:02:52 +00002148 Decl *ExceptionDecl = nullptr;
Sebastian Redlb219c902008-12-21 16:41:36 +00002149 if (Tok.isNot(tok::ellipsis)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002150 ParsedAttributesWithRange Attributes(AttrFactory);
2151 MaybeParseCXX11Attributes(Attributes);
2152
John McCall084e83d2011-03-24 11:26:52 +00002153 DeclSpec DS(AttrFactory);
Richard Smith1dba27c2013-01-29 09:02:09 +00002154 DS.takeAttributesFrom(Attributes);
2155
Sebastian Redl54c04d42008-12-22 19:15:10 +00002156 if (ParseCXXTypeSpecifierSeq(DS))
2157 return StmtError();
Richard Smith1dba27c2013-01-29 09:02:09 +00002158
Sebastian Redlb219c902008-12-21 16:41:36 +00002159 Declarator ExDecl(DS, Declarator::CXXCatchContext);
2160 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002161 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00002162 } else
2163 ConsumeToken();
2164
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002165 T.consumeClose();
2166 if (T.getCloseLocation().isInvalid())
Sebastian Redlb219c902008-12-21 16:41:36 +00002167 return StmtError();
2168
2169 if (Tok.isNot(tok::l_brace))
Alp Tokerec543272013-12-24 09:48:30 +00002170 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
Sebastian Redlb219c902008-12-21 16:41:36 +00002171
Alexis Hunt96d5c762009-11-21 08:43:09 +00002172 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
Richard Smithc202b282012-04-14 00:33:13 +00002173 StmtResult Block(ParseCompoundStatement());
Sebastian Redlb219c902008-12-21 16:41:36 +00002174 if (Block.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002175 return Block;
Sebastian Redlb219c902008-12-21 16:41:36 +00002176
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002177 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.get());
Sebastian Redlb219c902008-12-21 16:41:36 +00002178}
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002179
2180void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Douglas Gregor43edb322011-10-24 22:31:10 +00002181 IfExistsCondition Result;
Francois Picheta5b3fcb2011-05-07 17:30:27 +00002182 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002183 return;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00002184
Douglas Gregor43edb322011-10-24 22:31:10 +00002185 // Handle dependent statements by parsing the braces as a compound statement.
2186 // This is not the same behavior as Visual C++, which don't treat this as a
2187 // compound statement, but for Clang's type checking we can't have anything
2188 // inside these braces escaping to the surrounding code.
2189 if (Result.Behavior == IEB_Dependent) {
2190 if (!Tok.is(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002191 Diag(Tok, diag::err_expected) << tok::l_brace;
Richard Smithc202b282012-04-14 00:33:13 +00002192 return;
Douglas Gregor43edb322011-10-24 22:31:10 +00002193 }
Richard Smithc202b282012-04-14 00:33:13 +00002194
2195 StmtResult Compound = ParseCompoundStatement();
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002196 if (Compound.isInvalid())
2197 return;
Richard Smithc202b282012-04-14 00:33:13 +00002198
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002199 StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc,
2200 Result.IsIfExists,
Richard Smithc202b282012-04-14 00:33:13 +00002201 Result.SS,
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002202 Result.Name,
2203 Compound.get());
2204 if (DepResult.isUsable())
2205 Stmts.push_back(DepResult.get());
Douglas Gregor43edb322011-10-24 22:31:10 +00002206 return;
2207 }
Richard Smithc202b282012-04-14 00:33:13 +00002208
Douglas Gregor43edb322011-10-24 22:31:10 +00002209 BalancedDelimiterTracker Braces(*this, tok::l_brace);
2210 if (Braces.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +00002211 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002212 return;
2213 }
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002214
Douglas Gregor43edb322011-10-24 22:31:10 +00002215 switch (Result.Behavior) {
2216 case IEB_Parse:
2217 // Parse the statements below.
2218 break;
Chad Rosier67055f52012-07-10 21:35:27 +00002219
Douglas Gregor43edb322011-10-24 22:31:10 +00002220 case IEB_Dependent:
2221 llvm_unreachable("Dependent case handled above");
Chad Rosier67055f52012-07-10 21:35:27 +00002222
Douglas Gregor43edb322011-10-24 22:31:10 +00002223 case IEB_Skip:
2224 Braces.skipToEnd();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002225 return;
2226 }
2227
2228 // Condition is true, parse the statements.
2229 while (Tok.isNot(tok::r_brace)) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00002230 StmtResult R = ParseStatementOrDeclaration(Stmts, ACK_Any);
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002231 if (R.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002232 Stmts.push_back(R.get());
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002233 }
Douglas Gregor43edb322011-10-24 22:31:10 +00002234 Braces.consumeClose();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002235}
Anastasia Stulova6bdbcbb2016-02-19 18:30:11 +00002236
2237bool Parser::ParseOpenCLUnrollHintAttribute(ParsedAttributes &Attrs) {
2238 MaybeParseGNUAttributes(Attrs);
2239
2240 if (Attrs.empty())
2241 return true;
2242
2243 if (Attrs.getList()->getKind() != AttributeList::AT_OpenCLUnrollHint)
2244 return true;
2245
2246 if (!(Tok.is(tok::kw_for) || Tok.is(tok::kw_while) || Tok.is(tok::kw_do))) {
2247 Diag(Tok, diag::err_opencl_unroll_hint_on_non_loop);
2248 return false;
2249 }
2250 return true;
2251}