blob: d147ab0a567403cbb14c70c0a142af20df24d3cc [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
Chris Lattner0ccd51e2006-08-09 05:47:47 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner0ccd51e2006-08-09 05:47:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
Aaron Ballmanb06b15a2014-06-06 12:40:24 +000015#include "clang/Basic/Attributes.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/Basic/PrettyStackTrace.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000017#include "clang/Parse/Parser.h"
Vassil Vassilev11ad3392017-03-23 15:11:07 +000018#include "clang/Parse/RAIIObjectsForParser.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/DeclSpec.h"
Aaron Ballmanb06b15a2014-06-06 12:40:24 +000020#include "clang/Sema/LoopHint.h"
John McCallfaf5fb42010-08-26 23:41:50 +000021#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall8b0666c2010-08-20 18:27:03 +000022#include "clang/Sema/Scope.h"
Richard Smith4f605af2012-08-18 00:55:03 +000023#include "clang/Sema/TypoCorrection.h"
Chris Lattner0ccd51e2006-08-09 05:47:47 +000024using namespace clang;
25
26//===----------------------------------------------------------------------===//
27// C99 6.8: Statements and Blocks.
28//===----------------------------------------------------------------------===//
29
Richard Smith426a47b2013-10-28 22:04:30 +000030/// \brief Parse a standalone statement (for instance, as the body of an 'if',
31/// 'while', or 'for').
Alexey Bataevc4fad652016-01-13 11:18:54 +000032StmtResult Parser::ParseStatement(SourceLocation *TrailingElseLoc,
33 bool AllowOpenMPStandalone) {
Richard Smith426a47b2013-10-28 22:04:30 +000034 StmtResult Res;
35
36 // We may get back a null statement if we found a #pragma. Keep going until
37 // we get an actual statement.
38 do {
39 StmtVector Stmts;
Alexey Bataevc4fad652016-01-13 11:18:54 +000040 Res = ParseStatementOrDeclaration(
41 Stmts, AllowOpenMPStandalone ? ACK_StatementsOpenMPAnyExecutable
42 : ACK_StatementsOpenMPNonStandalone,
43 TrailingElseLoc);
Richard Smith426a47b2013-10-28 22:04:30 +000044 } while (!Res.isInvalid() && !Res.get());
45
46 return Res;
47}
48
Chris Lattner0ccd51e2006-08-09 05:47:47 +000049/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
50/// StatementOrDeclaration:
51/// statement
52/// declaration
53///
54/// statement:
55/// labeled-statement
56/// compound-statement
57/// expression-statement
58/// selection-statement
59/// iteration-statement
60/// jump-statement
Argyrios Kyrtzidisdee82912008-09-07 18:58:01 +000061/// [C++] declaration-statement
Sebastian Redlb219c902008-12-21 16:41:36 +000062/// [C++] try-block
John Wiegley1c0675e2011-04-28 01:08:34 +000063/// [MS] seh-try-block
Fariborz Jahanian90814572007-10-04 20:19:06 +000064/// [OBC] objc-throw-statement
65/// [OBC] objc-try-catch-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +000066/// [OBC] objc-synchronized-statement
Chris Lattner0116c472006-08-15 06:03:28 +000067/// [GNU] asm-statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +000068/// [OMP] openmp-construct [TODO]
69///
70/// labeled-statement:
71/// identifier ':' statement
72/// 'case' constant-expression ':' statement
73/// 'default' ':' statement
74///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000075/// selection-statement:
76/// if-statement
77/// switch-statement
78///
79/// iteration-statement:
80/// while-statement
81/// do-statement
82/// for-statement
83///
Chris Lattner9075bd72006-08-10 04:59:57 +000084/// expression-statement:
85/// expression[opt] ';'
86///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000087/// jump-statement:
88/// 'goto' identifier ';'
89/// 'continue' ';'
90/// 'break' ';'
91/// 'return' expression[opt] ';'
Chris Lattner503fadc2006-08-10 05:45:44 +000092/// [GNU] 'goto' '*' expression ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000093///
Fariborz Jahanian90814572007-10-04 20:19:06 +000094/// [OBC] objc-throw-statement:
95/// [OBC] '@' 'throw' expression ';'
Mike Stump11289f42009-09-09 15:08:12 +000096/// [OBC] '@' 'throw' ';'
97///
John McCalldadc5752010-08-24 06:29:42 +000098StmtResult
Alexey Bataevc4fad652016-01-13 11:18:54 +000099Parser::ParseStatementOrDeclaration(StmtVector &Stmts,
Jonathan Roelofsce1db6d2017-03-14 17:29:33 +0000100 AllowedConstructsKind Allowed,
Nico Weber3cef1082011-12-22 23:26:17 +0000101 SourceLocation *TrailingElseLoc) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000102
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000103 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000104
Richard Smithc202b282012-04-14 00:33:13 +0000105 ParsedAttributesWithRange Attrs(AttrFactory);
Craig Topper161e4db2014-05-21 06:02:52 +0000106 MaybeParseCXX11Attributes(Attrs, nullptr, /*MightBeObjCMessageSend*/ true);
Anastasia Stulova6bdbcbb2016-02-19 18:30:11 +0000107 if (!MaybeParseOpenCLUnrollHintAttribute(Attrs))
108 return StmtError();
Richard Smithc202b282012-04-14 00:33:13 +0000109
Alexey Bataevc4fad652016-01-13 11:18:54 +0000110 StmtResult Res = ParseStatementOrDeclarationAfterAttributes(
111 Stmts, Allowed, TrailingElseLoc, Attrs);
Richard Smithc202b282012-04-14 00:33:13 +0000112
113 assert((Attrs.empty() || Res.isInvalid() || Res.isUsable()) &&
114 "attributes on empty statement");
115
116 if (Attrs.empty() || Res.isInvalid())
117 return Res;
118
119 return Actions.ProcessStmtAttributes(Res.get(), Attrs.getList(), Attrs.Range);
120}
121
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000122namespace {
123class StatementFilterCCC : public CorrectionCandidateCallback {
124public:
125 StatementFilterCCC(Token nextTok) : NextToken(nextTok) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000126 WantTypeSpecifiers = nextTok.isOneOf(tok::l_paren, tok::less, tok::l_square,
127 tok::identifier, tok::star, tok::amp);
128 WantExpressionKeywords =
129 nextTok.isOneOf(tok::l_paren, tok::identifier, tok::arrow, tok::period);
130 WantRemainingKeywords =
131 nextTok.isOneOf(tok::l_paren, tok::semi, tok::identifier, tok::l_brace);
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000132 WantCXXNamedCasts = false;
133 }
134
Craig Topper2b07f022014-03-12 05:09:18 +0000135 bool ValidateCandidate(const TypoCorrection &candidate) override {
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000136 if (FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>())
Kaelyn Uhrain07e62722013-10-01 22:00:28 +0000137 return !candidate.getCorrectionSpecifier() || isa<ObjCIvarDecl>(FD);
Kaelyn Uhrain46b6cdc2013-09-27 19:40:16 +0000138 if (NextToken.is(tok::equal))
139 return candidate.getCorrectionDeclAs<VarDecl>();
Kaelyn Uhrain30943ce2013-09-27 23:54:23 +0000140 if (NextToken.is(tok::period) &&
141 candidate.getCorrectionDeclAs<NamespaceDecl>())
142 return false;
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000143 return CorrectionCandidateCallback::ValidateCandidate(candidate);
144 }
145
146private:
147 Token NextToken;
148};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000149}
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000150
Richard Smithc202b282012-04-14 00:33:13 +0000151StmtResult
152Parser::ParseStatementOrDeclarationAfterAttributes(StmtVector &Stmts,
Jonathan Roelofsce1db6d2017-03-14 17:29:33 +0000153 AllowedConstructsKind Allowed, SourceLocation *TrailingElseLoc,
Richard Smithc202b282012-04-14 00:33:13 +0000154 ParsedAttributesWithRange &Attrs) {
Craig Topper161e4db2014-05-21 06:02:52 +0000155 const char *SemiError = nullptr;
Richard Smithc202b282012-04-14 00:33:13 +0000156 StmtResult Res;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000157
Chris Lattner503fadc2006-08-10 05:45:44 +0000158 // Cases in this switch statement should fall through if the parser expects
159 // the token to end in a semicolon (in which case SemiError should be set),
160 // or they directly 'return;' if not.
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000161Retry:
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000162 tok::TokenKind Kind = Tok.getKind();
163 SourceLocation AtLoc;
164 switch (Kind) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000165 case tok::at: // May be a @try or @throw statement
166 {
Richard Smithc202b282012-04-14 00:33:13 +0000167 ProhibitAttributes(Attrs); // TODO: is it correct?
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000168 AtLoc = ConsumeToken(); // consume @
Sebastian Redlbab9a4b2008-12-11 20:12:42 +0000169 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000170 }
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000171
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000172 case tok::code_completion:
John McCallfaf5fb42010-08-26 23:41:50 +0000173 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000174 cutOffParsing();
175 return StmtError();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000176
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000177 case tok::identifier: {
178 Token Next = NextToken();
179 if (Next.is(tok::colon)) { // C99 6.8.1: labeled-statement
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000180 // identifier ':' statement
Richard Smithc202b282012-04-14 00:33:13 +0000181 return ParseLabeledStatement(Attrs);
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000182 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000183
Richard Smith4f605af2012-08-18 00:55:03 +0000184 // Look up the identifier, and typo-correct it to a keyword if it's not
185 // found.
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000186 if (Next.isNot(tok::coloncolon)) {
Richard Smith4f605af2012-08-18 00:55:03 +0000187 // Try to limit which sets of keywords should be included in typo
188 // correction based on what the next token is.
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000189 if (TryAnnotateName(/*IsAddressOfOperand*/ false,
190 llvm::make_unique<StatementFilterCCC>(Next)) ==
191 ANK_Error) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000192 // Handle errors here by skipping up to the next semicolon or '}', and
193 // eat the semicolon if that's what stopped us.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000194 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000195 if (Tok.is(tok::semi))
196 ConsumeToken();
197 return StmtError();
Richard Smith4f605af2012-08-18 00:55:03 +0000198 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000199
Richard Smith4f605af2012-08-18 00:55:03 +0000200 // If the identifier was typo-corrected, try again.
201 if (Tok.isNot(tok::identifier))
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000202 goto Retry;
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000203 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000204
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000205 // Fall through
206 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000207
Chris Lattner803802d2009-03-24 17:04:48 +0000208 default: {
David Majnemer6ac7dd12016-08-01 16:39:29 +0000209 if ((getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt ||
210 Allowed == ACK_Any) &&
Alexey Bataevc4fad652016-01-13 11:18:54 +0000211 isDeclarationStatement()) {
Chris Lattner49836b42009-04-02 04:16:50 +0000212 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Rafael Espindola1bd906d2014-10-22 14:27:08 +0000213 DeclGroupPtrTy Decl = ParseDeclaration(Declarator::BlockContext,
Richard Smithc202b282012-04-14 00:33:13 +0000214 DeclEnd, Attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000215 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattner803802d2009-03-24 17:04:48 +0000216 }
217
218 if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +0000219 Diag(Tok, diag::err_expected_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000220 return StmtError();
Chris Lattnerf8afb622006-08-10 18:26:31 +0000221 }
Mike Stump11289f42009-09-09 15:08:12 +0000222
Richard Smithc202b282012-04-14 00:33:13 +0000223 return ParseExprStatement();
Chris Lattner803802d2009-03-24 17:04:48 +0000224 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000225
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000226 case tok::kw_case: // C99 6.8.1: labeled-statement
Richard Smithc202b282012-04-14 00:33:13 +0000227 return ParseCaseStatement();
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000228 case tok::kw_default: // C99 6.8.1: labeled-statement
Richard Smithc202b282012-04-14 00:33:13 +0000229 return ParseDefaultStatement();
Sebastian Redl042ad952008-12-11 19:30:53 +0000230
Chris Lattner9075bd72006-08-10 04:59:57 +0000231 case tok::l_brace: // C99 6.8.2: compound-statement
Richard Smithc202b282012-04-14 00:33:13 +0000232 return ParseCompoundStatement();
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000233 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
Argyrios Kyrtzidis43ea78b2011-09-01 21:53:45 +0000234 bool HasLeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
235 return Actions.ActOnNullStmt(ConsumeToken(), HasLeadingEmptyMacro);
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000236 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000237
Chris Lattner9075bd72006-08-10 04:59:57 +0000238 case tok::kw_if: // C99 6.8.4.1: if-statement
Richard Smithc202b282012-04-14 00:33:13 +0000239 return ParseIfStatement(TrailingElseLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000240 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Richard Smithc202b282012-04-14 00:33:13 +0000241 return ParseSwitchStatement(TrailingElseLoc);
Sebastian Redl042ad952008-12-11 19:30:53 +0000242
Chris Lattner9075bd72006-08-10 04:59:57 +0000243 case tok::kw_while: // C99 6.8.5.1: while-statement
Richard Smithc202b282012-04-14 00:33:13 +0000244 return ParseWhileStatement(TrailingElseLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000245 case tok::kw_do: // C99 6.8.5.2: do-statement
Richard Smithc202b282012-04-14 00:33:13 +0000246 Res = ParseDoStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000247 SemiError = "do/while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000248 break;
249 case tok::kw_for: // C99 6.8.5.3: for-statement
Richard Smithc202b282012-04-14 00:33:13 +0000250 return ParseForStatement(TrailingElseLoc);
Chris Lattner503fadc2006-08-10 05:45:44 +0000251
252 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Richard Smithc202b282012-04-14 00:33:13 +0000253 Res = ParseGotoStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000254 SemiError = "goto";
Chris Lattner9075bd72006-08-10 04:59:57 +0000255 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000256 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Richard Smithc202b282012-04-14 00:33:13 +0000257 Res = ParseContinueStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000258 SemiError = "continue";
Chris Lattner503fadc2006-08-10 05:45:44 +0000259 break;
260 case tok::kw_break: // C99 6.8.6.3: break-statement
Richard Smithc202b282012-04-14 00:33:13 +0000261 Res = ParseBreakStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000262 SemiError = "break";
Chris Lattner503fadc2006-08-10 05:45:44 +0000263 break;
264 case tok::kw_return: // C99 6.8.6.4: return-statement
Richard Smithc202b282012-04-14 00:33:13 +0000265 Res = ParseReturnStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000266 SemiError = "return";
Chris Lattner503fadc2006-08-10 05:45:44 +0000267 break;
Richard Smith0e304ea2015-10-22 04:46:14 +0000268 case tok::kw_co_return: // C++ Coroutines: co_return statement
269 Res = ParseReturnStatement();
270 SemiError = "co_return";
271 break;
Sebastian Redl042ad952008-12-11 19:30:53 +0000272
Sebastian Redlb219c902008-12-21 16:41:36 +0000273 case tok::kw_asm: {
Richard Smithc202b282012-04-14 00:33:13 +0000274 ProhibitAttributes(Attrs);
Steve Naroffb2c80c72008-02-07 03:50:06 +0000275 bool msAsm = false;
276 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidis3050d9b2010-11-02 02:33:08 +0000277 Res = Actions.ActOnFinishFullStmt(Res.get());
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000278 if (msAsm) return Res;
Chris Lattner34a95662009-06-14 00:07:48 +0000279 SemiError = "asm";
Chris Lattner0116c472006-08-15 06:03:28 +0000280 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000281 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000282
Reid Kleckner6d8d22a2014-06-25 00:28:35 +0000283 case tok::kw___if_exists:
284 case tok::kw___if_not_exists:
285 ProhibitAttributes(Attrs);
286 ParseMicrosoftIfExistsStatement(Stmts);
287 // An __if_exists block is like a compound statement, but it doesn't create
288 // a new scope.
289 return StmtEmpty();
290
Sebastian Redlb219c902008-12-21 16:41:36 +0000291 case tok::kw_try: // C++ 15: try-block
Richard Smithc202b282012-04-14 00:33:13 +0000292 return ParseCXXTryBlock();
John Wiegley1c0675e2011-04-28 01:08:34 +0000293
294 case tok::kw___try:
Richard Smithc202b282012-04-14 00:33:13 +0000295 ProhibitAttributes(Attrs); // TODO: is it correct?
296 return ParseSEHTryBlock();
Eli Friedmanec52f922012-02-23 23:47:16 +0000297
Nico Weberc7d05962014-07-06 22:32:59 +0000298 case tok::kw___leave:
299 Res = ParseSEHLeaveStatement();
300 SemiError = "__leave";
301 break;
302
Eli Friedmanec52f922012-02-23 23:47:16 +0000303 case tok::annot_pragma_vis:
Richard Smithc202b282012-04-14 00:33:13 +0000304 ProhibitAttributes(Attrs);
Eli Friedmanec52f922012-02-23 23:47:16 +0000305 HandlePragmaVisibility();
306 return StmtEmpty();
307
308 case tok::annot_pragma_pack:
Richard Smithc202b282012-04-14 00:33:13 +0000309 ProhibitAttributes(Attrs);
Eli Friedmanec52f922012-02-23 23:47:16 +0000310 HandlePragmaPack();
311 return StmtEmpty();
Eli Friedman68be1642012-10-04 02:36:51 +0000312
Eli Friedmanbbbbac62012-10-09 22:46:54 +0000313 case tok::annot_pragma_msstruct:
314 ProhibitAttributes(Attrs);
315 HandlePragmaMSStruct();
316 return StmtEmpty();
317
Eli Friedmanae8ee252012-10-08 23:52:38 +0000318 case tok::annot_pragma_align:
319 ProhibitAttributes(Attrs);
320 HandlePragmaAlign();
321 return StmtEmpty();
322
Eli Friedmanbbbbac62012-10-09 22:46:54 +0000323 case tok::annot_pragma_weak:
324 ProhibitAttributes(Attrs);
325 HandlePragmaWeak();
326 return StmtEmpty();
327
328 case tok::annot_pragma_weakalias:
329 ProhibitAttributes(Attrs);
330 HandlePragmaWeakAlias();
331 return StmtEmpty();
332
333 case tok::annot_pragma_redefine_extname:
334 ProhibitAttributes(Attrs);
335 HandlePragmaRedefineExtname();
336 return StmtEmpty();
337
Eli Friedman68be1642012-10-04 02:36:51 +0000338 case tok::annot_pragma_fp_contract:
Richard Smithca9b0b62013-11-15 21:10:54 +0000339 ProhibitAttributes(Attrs);
Lang Hamesa930e712012-10-21 01:10:01 +0000340 Diag(Tok, diag::err_pragma_fp_contract_scope);
Richard Smithaf3b3252017-05-18 19:21:48 +0000341 ConsumeAnnotationToken();
Lang Hamesa930e712012-10-21 01:10:01 +0000342 return StmtError();
343
Adam Nemet60d32642017-04-04 21:18:36 +0000344 case tok::annot_pragma_fp:
345 ProhibitAttributes(Attrs);
346 Diag(Tok, diag::err_pragma_fp_scope);
Richard Smithaf3b3252017-05-18 19:21:48 +0000347 ConsumeAnnotationToken();
Adam Nemet60d32642017-04-04 21:18:36 +0000348 return StmtError();
349
Eli Friedman68be1642012-10-04 02:36:51 +0000350 case tok::annot_pragma_opencl_extension:
351 ProhibitAttributes(Attrs);
352 HandlePragmaOpenCLExtension();
353 return StmtEmpty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000354
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000355 case tok::annot_pragma_captured:
Richard Smith12a41bd2013-09-16 21:17:44 +0000356 ProhibitAttributes(Attrs);
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000357 return HandlePragmaCaptured();
358
Alexey Bataeva769e072013-03-22 06:34:35 +0000359 case tok::annot_pragma_openmp:
Richard Smith12a41bd2013-09-16 21:17:44 +0000360 ProhibitAttributes(Attrs);
Alexey Bataevc4fad652016-01-13 11:18:54 +0000361 return ParseOpenMPDeclarativeOrExecutableDirective(Allowed);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000362
David Majnemer4bb09802014-02-10 19:50:15 +0000363 case tok::annot_pragma_ms_pointers_to_members:
364 ProhibitAttributes(Attrs);
365 HandlePragmaMSPointersToMembers();
366 return StmtEmpty();
367
Warren Huntc3b18962014-04-08 22:30:47 +0000368 case tok::annot_pragma_ms_pragma:
369 ProhibitAttributes(Attrs);
370 HandlePragmaMSPragma();
371 return StmtEmpty();
Aaron Ballmanb06b15a2014-06-06 12:40:24 +0000372
Alexey Bataev3d42f342015-11-20 07:02:57 +0000373 case tok::annot_pragma_ms_vtordisp:
374 ProhibitAttributes(Attrs);
375 HandlePragmaMSVtorDisp();
376 return StmtEmpty();
377
Aaron Ballmanb06b15a2014-06-06 12:40:24 +0000378 case tok::annot_pragma_loop_hint:
379 ProhibitAttributes(Attrs);
Alexey Bataevc4fad652016-01-13 11:18:54 +0000380 return ParsePragmaLoopHint(Stmts, Allowed, TrailingElseLoc, Attrs);
Richard Smithba3a4f92016-01-12 21:59:26 +0000381
382 case tok::annot_pragma_dump:
383 HandlePragmaDump();
384 return StmtEmpty();
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000385
386 case tok::annot_pragma_attribute:
387 HandlePragmaAttribute();
388 return StmtEmpty();
Sebastian Redlb219c902008-12-21 16:41:36 +0000389 }
390
Chris Lattner503fadc2006-08-10 05:45:44 +0000391 // If we reached this code, the statement must end in a semicolon.
Alp Toker97650562014-01-10 11:19:30 +0000392 if (!TryConsumeToken(tok::semi) && !Res.isInvalid()) {
Chris Lattner8e3eed02009-06-14 00:23:56 +0000393 // If the result was valid, then we do want to diagnose this. Use
394 // ExpectAndConsume to emit the diagnostic, even though we know it won't
395 // succeed.
396 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner0046de12008-11-13 18:52:53 +0000397 // Skip until we see a } or ;, but don't eat it.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000398 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner503fadc2006-08-10 05:45:44 +0000399 }
Mike Stump11289f42009-09-09 15:08:12 +0000400
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000401 return Res;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000402}
403
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000404/// \brief Parse an expression statement.
Richard Smithc202b282012-04-14 00:33:13 +0000405StmtResult Parser::ParseExprStatement() {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000406 // If a case keyword is missing, this is where it should be inserted.
407 Token OldToken = Tok;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000408
Alex Lorenzf0b4e5d2016-10-18 10:55:01 +0000409 ExprStatementTokLoc = Tok.getLocation();
410
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000411 // expression[opt] ';'
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000412 ExprResult Expr(ParseExpression());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000413 if (Expr.isInvalid()) {
414 // If the expression is invalid, skip ahead to the next semicolon or '}'.
415 // Not doing this opens us up to the possibility of infinite loops if
416 // ParseExpression does not consume any tokens.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000417 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000418 if (Tok.is(tok::semi))
419 ConsumeToken();
John McCalleaef89b2013-03-22 02:10:40 +0000420 return Actions.ActOnExprStmtError();
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000421 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000422
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000423 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
424 Actions.CheckCaseExpression(Expr.get())) {
425 // If a constant expression is followed by a colon inside a switch block,
426 // suggest a missing case keyword.
427 Diag(OldToken, diag::err_expected_case_before_expression)
428 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000429
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000430 // Recover parsing as a case statement.
Richard Smithc202b282012-04-14 00:33:13 +0000431 return ParseCaseStatement(/*MissingCase=*/true, Expr);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000432 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000433
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000434 // Otherwise, eat the semicolon.
435 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +0000436 return Actions.ActOnExprStmt(Expr);
John Wiegley1c0675e2011-04-28 01:08:34 +0000437}
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000438
John Wiegley1c0675e2011-04-28 01:08:34 +0000439/// ParseSEHTryBlockCommon
440///
441/// seh-try-block:
442/// '__try' compound-statement seh-handler
443///
444/// seh-handler:
445/// seh-except-block
446/// seh-finally-block
447///
Nico Weberdd256742015-02-25 01:43:27 +0000448StmtResult Parser::ParseSEHTryBlock() {
449 assert(Tok.is(tok::kw___try) && "Expected '__try'");
450 SourceLocation TryLoc = ConsumeToken();
451
Nico Weberfc3fe4f2015-02-25 02:22:06 +0000452 if (Tok.isNot(tok::l_brace))
Alp Tokerec543272013-12-24 09:48:30 +0000453 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
John Wiegley1c0675e2011-04-28 01:08:34 +0000454
Warren Huntf6be4cb2014-07-25 20:52:51 +0000455 StmtResult TryBlock(ParseCompoundStatement(/*isStmtExpr=*/false,
456 Scope::DeclScope | Scope::SEHTryScope));
John Wiegley1c0675e2011-04-28 01:08:34 +0000457 if(TryBlock.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000458 return TryBlock;
John Wiegley1c0675e2011-04-28 01:08:34 +0000459
460 StmtResult Handler;
Richard Smithc202b282012-04-14 00:33:13 +0000461 if (Tok.is(tok::identifier) &&
Douglas Gregor60060d62011-10-21 03:57:52 +0000462 Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +0000463 SourceLocation Loc = ConsumeToken();
464 Handler = ParseSEHExceptBlock(Loc);
465 } else if (Tok.is(tok::kw___finally)) {
466 SourceLocation Loc = ConsumeToken();
467 Handler = ParseSEHFinallyBlock(Loc);
468 } else {
Nico Weberfc3fe4f2015-02-25 02:22:06 +0000469 return StmtError(Diag(Tok, diag::err_seh_expected_handler));
John Wiegley1c0675e2011-04-28 01:08:34 +0000470 }
471
472 if(Handler.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000473 return Handler;
John Wiegley1c0675e2011-04-28 01:08:34 +0000474
475 return Actions.ActOnSEHTryBlock(false /* IsCXXTry */,
476 TryLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000477 TryBlock.get(),
Warren Huntf6be4cb2014-07-25 20:52:51 +0000478 Handler.get());
John Wiegley1c0675e2011-04-28 01:08:34 +0000479}
480
481/// ParseSEHExceptBlock - Handle __except
482///
483/// seh-except-block:
484/// '__except' '(' seh-filter-expression ')' compound-statement
485///
486StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) {
487 PoisonIdentifierRAIIObject raii(Ident__exception_code, false),
488 raii2(Ident___exception_code, false),
489 raii3(Ident_GetExceptionCode, false);
490
Alp Toker383d2c42014-01-01 03:08:43 +0000491 if (ExpectAndConsume(tok::l_paren))
John Wiegley1c0675e2011-04-28 01:08:34 +0000492 return StmtError();
493
Reid Kleckner1d59f992015-01-22 01:36:17 +0000494 ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope |
495 Scope::SEHExceptScope);
John Wiegley1c0675e2011-04-28 01:08:34 +0000496
David Blaikiebbafb8a2012-03-11 07:00:24 +0000497 if (getLangOpts().Borland) {
Francois Pichetbfaf4772011-04-28 03:14:31 +0000498 Ident__exception_info->setIsPoisoned(false);
499 Ident___exception_info->setIsPoisoned(false);
500 Ident_GetExceptionInfo->setIsPoisoned(false);
501 }
Reid Kleckner1d59f992015-01-22 01:36:17 +0000502
503 ExprResult FilterExpr;
504 {
505 ParseScopeFlags FilterScope(this, getCurScope()->getFlags() |
506 Scope::SEHFilterScope);
Reid Kleckner85368fb2015-04-02 22:09:32 +0000507 FilterExpr = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Reid Kleckner1d59f992015-01-22 01:36:17 +0000508 }
Francois Pichetbfaf4772011-04-28 03:14:31 +0000509
David Blaikiebbafb8a2012-03-11 07:00:24 +0000510 if (getLangOpts().Borland) {
Francois Pichetbfaf4772011-04-28 03:14:31 +0000511 Ident__exception_info->setIsPoisoned(true);
512 Ident___exception_info->setIsPoisoned(true);
513 Ident_GetExceptionInfo->setIsPoisoned(true);
514 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000515
516 if(FilterExpr.isInvalid())
517 return StmtError();
518
Alp Toker383d2c42014-01-01 03:08:43 +0000519 if (ExpectAndConsume(tok::r_paren))
John Wiegley1c0675e2011-04-28 01:08:34 +0000520 return StmtError();
521
Nico Weberfc3fe4f2015-02-25 02:22:06 +0000522 if (Tok.isNot(tok::l_brace))
523 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
524
Richard Smithc202b282012-04-14 00:33:13 +0000525 StmtResult Block(ParseCompoundStatement());
John Wiegley1c0675e2011-04-28 01:08:34 +0000526
527 if(Block.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000528 return Block;
John Wiegley1c0675e2011-04-28 01:08:34 +0000529
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000530 return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.get(), Block.get());
John Wiegley1c0675e2011-04-28 01:08:34 +0000531}
532
533/// ParseSEHFinallyBlock - Handle __finally
534///
535/// seh-finally-block:
536/// '__finally' compound-statement
537///
Nico Weberd64657f2015-03-09 02:47:59 +0000538StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyLoc) {
John Wiegley1c0675e2011-04-28 01:08:34 +0000539 PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
540 raii2(Ident___abnormal_termination, false),
541 raii3(Ident_AbnormalTermination, false);
542
Nico Weberfc3fe4f2015-02-25 02:22:06 +0000543 if (Tok.isNot(tok::l_brace))
544 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
545
Nico Weberd64657f2015-03-09 02:47:59 +0000546 ParseScope FinallyScope(this, 0);
547 Actions.ActOnStartSEHFinallyBlock();
548
Richard Smithc202b282012-04-14 00:33:13 +0000549 StmtResult Block(ParseCompoundStatement());
Nico Weberce903292015-03-09 03:17:15 +0000550 if(Block.isInvalid()) {
551 Actions.ActOnAbortSEHFinallyBlock();
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000552 return Block;
Nico Weberce903292015-03-09 03:17:15 +0000553 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000554
Nico Weberd64657f2015-03-09 02:47:59 +0000555 return Actions.ActOnFinishSEHFinallyBlock(FinallyLoc, Block.get());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000556}
557
Nico Weberc7d05962014-07-06 22:32:59 +0000558/// Handle __leave
559///
560/// seh-leave-statement:
561/// '__leave' ';'
562///
563StmtResult Parser::ParseSEHLeaveStatement() {
564 SourceLocation LeaveLoc = ConsumeToken(); // eat the '__leave'.
565 return Actions.ActOnSEHLeaveStmt(LeaveLoc, getCurScope());
566}
567
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000568/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000569///
570/// labeled-statement:
571/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000572/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000573///
Richard Smithc202b282012-04-14 00:33:13 +0000574StmtResult Parser::ParseLabeledStatement(ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000575 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
576 "Not an identifier!");
577
578 Token IdentTok = Tok; // Save the whole token.
579 ConsumeToken(); // eat the identifier.
580
581 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000582
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000583 // identifier ':' statement
584 SourceLocation ColonLoc = ConsumeToken();
585
Richard Smitha3e01cf2013-11-15 22:45:29 +0000586 // Read label attributes, if present.
587 StmtResult SubStmt;
588 if (Tok.is(tok::kw___attribute)) {
589 ParsedAttributesWithRange TempAttrs(AttrFactory);
590 ParseGNUAttributes(TempAttrs);
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000591
Richard Smitha3e01cf2013-11-15 22:45:29 +0000592 // In C++, GNU attributes only apply to the label if they are followed by a
593 // semicolon, to disambiguate label attributes from attributes on a labeled
594 // declaration.
595 //
596 // This doesn't quite match what GCC does; if the attribute list is empty
597 // and followed by a semicolon, GCC will reject (it appears to parse the
598 // attributes as part of a statement in that case). That looks like a bug.
599 if (!getLangOpts().CPlusPlus || Tok.is(tok::semi))
600 attrs.takeAllFrom(TempAttrs);
601 else if (isDeclarationStatement()) {
602 StmtVector Stmts;
603 // FIXME: We should do this whether or not we have a declaration
604 // statement, but that doesn't work correctly (because ProhibitAttributes
605 // can't handle GNU attributes), so only call it in the one case where
606 // GNU attributes are allowed.
607 SubStmt = ParseStatementOrDeclarationAfterAttributes(
Alexey Bataevc4fad652016-01-13 11:18:54 +0000608 Stmts, /*Allowed=*/ACK_StatementsOpenMPNonStandalone, nullptr,
609 TempAttrs);
Richard Smitha3e01cf2013-11-15 22:45:29 +0000610 if (!TempAttrs.empty() && !SubStmt.isInvalid())
611 SubStmt = Actions.ProcessStmtAttributes(
612 SubStmt.get(), TempAttrs.getList(), TempAttrs.Range);
613 } else {
Alp Toker383d2c42014-01-01 03:08:43 +0000614 Diag(Tok, diag::err_expected_after) << "__attribute__" << tok::semi;
Richard Smitha3e01cf2013-11-15 22:45:29 +0000615 }
616 }
617
618 // If we've not parsed a statement yet, parse one now.
619 if (!SubStmt.isInvalid() && !SubStmt.isUsable())
620 SubStmt = ParseStatement();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000621
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000622 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000623 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000624 SubStmt = Actions.ActOnNullStmt(ColonLoc);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000625
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000626 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
627 IdentTok.getLocation());
Richard Smithc202b282012-04-14 00:33:13 +0000628 if (AttributeList *Attrs = attrs.getList()) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000629 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
Richard Smithc202b282012-04-14 00:33:13 +0000630 attrs.clear();
631 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000632
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000633 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
634 SubStmt.get());
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000635}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000636
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000637/// ParseCaseStatement
638/// labeled-statement:
639/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000640/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000641///
Richard Smithc202b282012-04-14 00:33:13 +0000642StmtResult Parser::ParseCaseStatement(bool MissingCase, ExprResult Expr) {
Richard Smithd4257d82011-04-21 22:48:40 +0000643 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Mike Stump11289f42009-09-09 15:08:12 +0000644
Chris Lattner34a22092009-03-04 04:23:07 +0000645 // It is very very common for code to contain many case statements recursively
646 // nested, as in (but usually without indentation):
647 // case 1:
648 // case 2:
649 // case 3:
650 // case 4:
651 // case 5: etc.
652 //
653 // Parsing this naively works, but is both inefficient and can cause us to run
654 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner2b19a6582009-03-04 18:24:58 +0000655 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner34a22092009-03-04 04:23:07 +0000656 // but all the grossness is constrained to ParseCaseStatement (and some
Richard Smitha3e01cf2013-11-15 22:45:29 +0000657 // weirdness in the actions), so this is just local grossness :).
Mike Stump11289f42009-09-09 15:08:12 +0000658
Chris Lattner34a22092009-03-04 04:23:07 +0000659 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
660 // example above.
John McCalldadc5752010-08-24 06:29:42 +0000661 StmtResult TopLevelCase(true);
Mike Stump11289f42009-09-09 15:08:12 +0000662
Chris Lattner34a22092009-03-04 04:23:07 +0000663 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
664 // gets updated each time a new case is parsed, and whose body is unset so
665 // far. When parsing 'case 4', this is the 'case 3' node.
Craig Topper161e4db2014-05-21 06:02:52 +0000666 Stmt *DeepestParsedCaseStmt = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000667
Chris Lattner34a22092009-03-04 04:23:07 +0000668 // While we have case statements, eat and stack them.
David Majnemer0ac67fa2011-06-13 05:50:12 +0000669 SourceLocation ColonLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000670 do {
Richard Trieu2c850c02011-04-21 21:44:26 +0000671 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
672 ConsumeToken(); // eat the 'case'.
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000673 ColonLoc = SourceLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000674
Douglas Gregord328d572009-09-21 18:10:23 +0000675 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000676 Actions.CodeCompleteCase(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000677 cutOffParsing();
678 return StmtError();
Douglas Gregord328d572009-09-21 18:10:23 +0000679 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000680
Chris Lattner125c0ee2009-12-10 00:38:54 +0000681 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
682 /// Disable this form of error recovery while we're parsing the case
683 /// expression.
684 ColonProtectionRAIIObject ColonProtection(*this);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000685
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000686 ExprResult LHS;
687 if (!MissingCase) {
688 LHS = ParseConstantExpression();
Kaelyn Takatab16e6322014-11-20 22:06:40 +0000689 if (!getLangOpts().CPlusPlus11) {
690 LHS = Actions.CorrectDelayedTyposInExpr(LHS, [this](class Expr *E) {
691 return Actions.VerifyIntegerConstantExpression(E);
692 });
693 }
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000694 if (LHS.isInvalid()) {
695 // If constant-expression is parsed unsuccessfully, recover by skipping
696 // current case statement (moving to the colon that ends it).
697 if (SkipUntil(tok::colon, tok::r_brace, StopAtSemi | StopBeforeMatch)) {
698 TryConsumeToken(tok::colon, ColonLoc);
699 continue;
700 }
701 return StmtError();
702 }
703 } else {
704 LHS = Expr;
705 MissingCase = false;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000706 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000707
Chris Lattner34a22092009-03-04 04:23:07 +0000708 // GNU case range extension.
709 SourceLocation DotDotDotLoc;
John McCalldadc5752010-08-24 06:29:42 +0000710 ExprResult RHS;
Alp Tokerec543272013-12-24 09:48:30 +0000711 if (TryConsumeToken(tok::ellipsis, DotDotDotLoc)) {
712 Diag(DotDotDotLoc, diag::ext_gnu_case_range);
Chris Lattner34a22092009-03-04 04:23:07 +0000713 RHS = ParseConstantExpression();
714 if (RHS.isInvalid()) {
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000715 if (SkipUntil(tok::colon, tok::r_brace, StopAtSemi | StopBeforeMatch)) {
716 TryConsumeToken(tok::colon, ColonLoc);
717 continue;
718 }
Chris Lattner34a22092009-03-04 04:23:07 +0000719 return StmtError();
720 }
721 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000722
Chris Lattner125c0ee2009-12-10 00:38:54 +0000723 ColonProtection.restore();
Sebastian Redl042ad952008-12-11 19:30:53 +0000724
Alp Tokerec543272013-12-24 09:48:30 +0000725 if (TryConsumeToken(tok::colon, ColonLoc)) {
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000726 } else if (TryConsumeToken(tok::semi, ColonLoc) ||
727 TryConsumeToken(tok::coloncolon, ColonLoc)) {
728 // Treat "case blah;" or "case blah::" as a typo for "case blah:".
Alp Tokerec543272013-12-24 09:48:30 +0000729 Diag(ColonLoc, diag::err_expected_after)
730 << "'case'" << tok::colon
731 << FixItHint::CreateReplacement(ColonLoc, ":");
John McCall0140bfe2011-01-22 09:28:32 +0000732 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000733 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
Alp Tokerec543272013-12-24 09:48:30 +0000734 Diag(ExpectedLoc, diag::err_expected_after)
735 << "'case'" << tok::colon
736 << FixItHint::CreateInsertion(ExpectedLoc, ":");
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000737 ColonLoc = ExpectedLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000738 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000739
John McCalldadc5752010-08-24 06:29:42 +0000740 StmtResult Case =
John McCallb268a282010-08-23 23:25:46 +0000741 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
742 RHS.get(), ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000743
Chris Lattner34a22092009-03-04 04:23:07 +0000744 // If we had a sema error parsing this case, then just ignore it and
745 // continue parsing the sub-stmt.
746 if (Case.isInvalid()) {
747 if (TopLevelCase.isInvalid()) // No parsed case stmts.
Alexey Bataevc4fad652016-01-13 11:18:54 +0000748 return ParseStatement(/*TrailingElseLoc=*/nullptr,
749 /*AllowOpenMPStandalone=*/true);
Chris Lattner34a22092009-03-04 04:23:07 +0000750 // Otherwise, just don't add it as a nested case.
751 } else {
752 // If this is the first case statement we parsed, it becomes TopLevelCase.
753 // Otherwise we link it into the current chain.
John McCall37ad5512010-08-23 06:44:23 +0000754 Stmt *NextDeepest = Case.get();
Chris Lattner34a22092009-03-04 04:23:07 +0000755 if (TopLevelCase.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000756 TopLevelCase = Case;
Chris Lattner34a22092009-03-04 04:23:07 +0000757 else
John McCallb268a282010-08-23 23:25:46 +0000758 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner34a22092009-03-04 04:23:07 +0000759 DeepestParsedCaseStmt = NextDeepest;
760 }
Mike Stump11289f42009-09-09 15:08:12 +0000761
Chris Lattner34a22092009-03-04 04:23:07 +0000762 // Handle all case statements.
763 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000764
Chris Lattner34a22092009-03-04 04:23:07 +0000765 // If we found a non-case statement, start by parsing it.
John McCalldadc5752010-08-24 06:29:42 +0000766 StmtResult SubStmt;
Mike Stump11289f42009-09-09 15:08:12 +0000767
Chris Lattner34a22092009-03-04 04:23:07 +0000768 if (Tok.isNot(tok::r_brace)) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000769 SubStmt = ParseStatement(/*TrailingElseLoc=*/nullptr,
770 /*AllowOpenMPStandalone=*/true);
Chris Lattner34a22092009-03-04 04:23:07 +0000771 } else {
772 // Nicely diagnose the common error "switch (X) { case 4: }", which is
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000773 // not valid. If ColonLoc doesn't point to a valid text location, there was
774 // another parsing error, so avoid producing extra diagnostics.
775 if (ColonLoc.isValid()) {
776 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
777 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
778 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
779 }
780 SubStmt = StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000781 }
Mike Stump11289f42009-09-09 15:08:12 +0000782
Chris Lattner34a22092009-03-04 04:23:07 +0000783 // Install the body into the most deeply-nested case.
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000784 if (DeepestParsedCaseStmt) {
785 // Broken sub-stmt shouldn't prevent forming the case statement properly.
786 if (SubStmt.isInvalid())
787 SubStmt = Actions.ActOnNullStmt(SourceLocation());
788 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
789 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000790
Chris Lattner34a22092009-03-04 04:23:07 +0000791 // Return the top level parsed statement tree.
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000792 return TopLevelCase;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000793}
794
795/// ParseDefaultStatement
796/// labeled-statement:
797/// 'default' ':' statement
798/// Note that this does not parse the 'statement' at the end.
799///
Richard Smithc202b282012-04-14 00:33:13 +0000800StmtResult Parser::ParseDefaultStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000801 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000802 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000803
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000804 SourceLocation ColonLoc;
Alp Tokerec543272013-12-24 09:48:30 +0000805 if (TryConsumeToken(tok::colon, ColonLoc)) {
Alp Tokerec543272013-12-24 09:48:30 +0000806 } else if (TryConsumeToken(tok::semi, ColonLoc)) {
Alp Toker97650562014-01-10 11:19:30 +0000807 // Treat "default;" as a typo for "default:".
Alp Tokerec543272013-12-24 09:48:30 +0000808 Diag(ColonLoc, diag::err_expected_after)
809 << "'default'" << tok::colon
810 << FixItHint::CreateReplacement(ColonLoc, ":");
John McCall0140bfe2011-01-22 09:28:32 +0000811 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000812 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
Alp Tokerec543272013-12-24 09:48:30 +0000813 Diag(ExpectedLoc, diag::err_expected_after)
814 << "'default'" << tok::colon
815 << FixItHint::CreateInsertion(ExpectedLoc, ":");
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000816 ColonLoc = ExpectedLoc;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000817 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000818
Richard Smith1002d102012-02-17 01:35:32 +0000819 StmtResult SubStmt;
820
821 if (Tok.isNot(tok::r_brace)) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000822 SubStmt = ParseStatement(/*TrailingElseLoc=*/nullptr,
823 /*AllowOpenMPStandalone=*/true);
Richard Smith1002d102012-02-17 01:35:32 +0000824 } else {
825 // Diagnose the common error "switch (X) {... default: }", which is
826 // not valid.
David Majnemerc6a99872011-06-14 15:24:38 +0000827 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
Richard Smith1002d102012-02-17 01:35:32 +0000828 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
829 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
830 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000831 }
832
Richard Smith1002d102012-02-17 01:35:32 +0000833 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000834 if (SubStmt.isInvalid())
Richard Smith1002d102012-02-17 01:35:32 +0000835 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl042ad952008-12-11 19:30:53 +0000836
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000837 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000838 SubStmt.get(), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000839}
840
Richard Smithc202b282012-04-14 00:33:13 +0000841StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
842 return ParseCompoundStatement(isStmtExpr, Scope::DeclScope);
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000843}
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000844
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000845/// ParseCompoundStatement - Parse a "{}" block.
846///
847/// compound-statement: [C99 6.8.2]
848/// { block-item-list[opt] }
849/// [GNU] { label-declarations block-item-list } [TODO]
850///
851/// block-item-list:
852/// block-item
853/// block-item-list block-item
854///
855/// block-item:
856/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000857/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000858/// statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000859///
860/// [GNU] label-declarations:
861/// [GNU] label-declaration
862/// [GNU] label-declarations label-declaration
863///
864/// [GNU] label-declaration:
865/// [GNU] '__label__' identifier-list ';'
866///
Richard Smithc202b282012-04-14 00:33:13 +0000867StmtResult Parser::ParseCompoundStatement(bool isStmtExpr,
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000868 unsigned ScopeFlags) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000869 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000870
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000871 // Enter a scope to hold everything within the compound stmt. Compound
872 // statements can always hold declarations.
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000873 ParseScope CompoundScope(this, ScopeFlags);
Chris Lattnerf2978802007-01-21 06:52:16 +0000874
875 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000876 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000877}
878
Lang Hames2954cea2012-11-03 22:29:05 +0000879/// Parse any pragmas at the start of the compound expression. We handle these
880/// separately since some pragmas (FP_CONTRACT) must appear before any C
881/// statement in the compound, but may be intermingled with other pragmas.
882void Parser::ParseCompoundStatementLeadingPragmas() {
883 bool checkForPragmas = true;
884 while (checkForPragmas) {
885 switch (Tok.getKind()) {
886 case tok::annot_pragma_vis:
887 HandlePragmaVisibility();
888 break;
889 case tok::annot_pragma_pack:
890 HandlePragmaPack();
891 break;
892 case tok::annot_pragma_msstruct:
893 HandlePragmaMSStruct();
894 break;
895 case tok::annot_pragma_align:
896 HandlePragmaAlign();
897 break;
898 case tok::annot_pragma_weak:
899 HandlePragmaWeak();
900 break;
901 case tok::annot_pragma_weakalias:
902 HandlePragmaWeakAlias();
903 break;
904 case tok::annot_pragma_redefine_extname:
905 HandlePragmaRedefineExtname();
906 break;
907 case tok::annot_pragma_opencl_extension:
908 HandlePragmaOpenCLExtension();
909 break;
910 case tok::annot_pragma_fp_contract:
911 HandlePragmaFPContract();
912 break;
Adam Nemet60d32642017-04-04 21:18:36 +0000913 case tok::annot_pragma_fp:
914 HandlePragmaFP();
915 break;
David Majnemer4bb09802014-02-10 19:50:15 +0000916 case tok::annot_pragma_ms_pointers_to_members:
917 HandlePragmaMSPointersToMembers();
918 break;
Warren Huntc3b18962014-04-08 22:30:47 +0000919 case tok::annot_pragma_ms_pragma:
920 HandlePragmaMSPragma();
921 break;
Alexey Bataev3d42f342015-11-20 07:02:57 +0000922 case tok::annot_pragma_ms_vtordisp:
923 HandlePragmaMSVtorDisp();
924 break;
Richard Smithba3a4f92016-01-12 21:59:26 +0000925 case tok::annot_pragma_dump:
926 HandlePragmaDump();
927 break;
Lang Hames2954cea2012-11-03 22:29:05 +0000928 default:
929 checkForPragmas = false;
930 break;
931 }
932 }
933
934}
935
Chris Lattnerf2978802007-01-21 06:52:16 +0000936/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000937/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000938/// consume the '}' at the end of the block. It does not manipulate the scope
939/// stack.
John McCalldadc5752010-08-24 06:29:42 +0000940StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000941 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000942 Tok.getLocation(),
943 "in compound statement ('{}')");
Lang Hames5de91cc2012-10-02 04:45:10 +0000944
945 // Record the state of the FP_CONTRACT pragma, restore on leaving the
946 // compound statement.
947 Sema::FPContractStateRAII SaveFPContractState(Actions);
948
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000949 InMessageExpressionRAIIObject InMessage(*this, false);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000950 BalancedDelimiterTracker T(*this, tok::l_brace);
951 if (T.consumeOpen())
952 return StmtError();
Chris Lattnerf2978802007-01-21 06:52:16 +0000953
Dmitri Gribenko800ddf32012-02-14 22:14:32 +0000954 Sema::CompoundScopeRAII CompoundScope(Actions);
955
Lang Hames2954cea2012-11-03 22:29:05 +0000956 // Parse any pragmas at the beginning of the compound statement.
957 ParseCompoundStatementLeadingPragmas();
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000958
Lang Hames2954cea2012-11-03 22:29:05 +0000959 StmtVector Stmts;
Lang Hamesa930e712012-10-21 01:10:01 +0000960
Chris Lattner43e7f312011-02-18 02:08:43 +0000961 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
962 // only allowed at the start of a compound stmt regardless of the language.
963 while (Tok.is(tok::kw___label__)) {
964 SourceLocation LabelLoc = ConsumeToken();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000965
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000966 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner43e7f312011-02-18 02:08:43 +0000967 while (1) {
968 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000969 Diag(Tok, diag::err_expected) << tok::identifier;
Chris Lattner43e7f312011-02-18 02:08:43 +0000970 break;
971 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000972
Chris Lattner43e7f312011-02-18 02:08:43 +0000973 IdentifierInfo *II = Tok.getIdentifierInfo();
974 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000975 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000976
Alp Tokerec543272013-12-24 09:48:30 +0000977 if (!TryConsumeToken(tok::comma))
Chris Lattner43e7f312011-02-18 02:08:43 +0000978 break;
Chris Lattner43e7f312011-02-18 02:08:43 +0000979 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000980
John McCall084e83d2011-03-24 11:26:52 +0000981 DeclSpec DS(AttrFactory);
Rafael Espindolaab417692013-07-09 12:05:01 +0000982 DeclGroupPtrTy Res =
983 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner43e7f312011-02-18 02:08:43 +0000984 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000985
Chris Lattner02f1b612012-04-28 16:12:17 +0000986 ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
Chris Lattner43e7f312011-02-18 02:08:43 +0000987 if (R.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000988 Stmts.push_back(R.get());
Chris Lattner43e7f312011-02-18 02:08:43 +0000989 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000990
Richard Smith752ada82015-11-17 23:32:01 +0000991 while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
992 Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000993 if (Tok.is(tok::annot_pragma_unused)) {
994 HandlePragmaUnused();
995 continue;
996 }
997
John McCalldadc5752010-08-24 06:29:42 +0000998 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000999 if (Tok.isNot(tok::kw___extension__)) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00001000 R = ParseStatementOrDeclaration(Stmts, ACK_Any);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001001 } else {
1002 // __extension__ can start declarations and it can also be a unary
1003 // operator for expressions. Consume multiple __extension__ markers here
1004 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +00001005 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001006 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001007 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001008 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +00001009
John McCall084e83d2011-03-24 11:26:52 +00001010 ParsedAttributesWithRange attrs(AttrFactory);
Craig Topper161e4db2014-05-21 06:02:52 +00001011 MaybeParseCXX11Attributes(attrs, nullptr,
1012 /*MightBeObjCMessageSend*/ true);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001013
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001014 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001015 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +00001016 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +00001017 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +00001018 ExtensionRAIIObject O(Diags);
1019
Chris Lattner49836b42009-04-02 04:16:50 +00001020 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Rafael Espindola1bd906d2014-10-22 14:27:08 +00001021 DeclGroupPtrTy Res = ParseDeclaration(Declarator::BlockContext, DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001022 attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001023 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001024 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +00001025 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +00001026 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +00001027
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001028 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001029 SkipUntil(tok::semi);
1030 continue;
1031 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001032
Alexis Hunt96d5c762009-11-21 08:43:09 +00001033 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +00001034 // Eat the semicolon at the end of stmt and convert the expr into a
1035 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00001036 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +00001037 R = Actions.ActOnExprStmt(Res);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001038 }
1039 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001040
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001041 if (R.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001042 Stmts.push_back(R.get());
Chris Lattner30f910e2006-10-16 05:52:41 +00001043 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001044
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +00001045 SourceLocation CloseLoc = Tok.getLocation();
1046
Chris Lattner0ccd51e2006-08-09 05:47:47 +00001047 // We broke out of the while loop because we found a '}' or EOF.
Nico Webera48b6c22012-12-30 23:36:56 +00001048 if (!T.consumeClose())
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +00001049 // Recover by creating a compound statement with what we parsed so far,
1050 // instead of dropping everything and returning StmtError();
Nico Webera48b6c22012-12-30 23:36:56 +00001051 CloseLoc = T.getCloseLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +00001052
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +00001053 return Actions.ActOnCompoundStmt(T.getOpenLocation(), CloseLoc,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001054 Stmts, isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +00001055}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001056
Chris Lattnerc0081db2008-12-12 06:31:07 +00001057/// ParseParenExprOrCondition:
1058/// [C ] '(' expression ')'
Richard Smithc7a05a92016-06-29 21:17:59 +00001059/// [C++] '(' condition ')'
1060/// [C++1z] '(' init-statement[opt] condition ')'
Chris Lattnerc0081db2008-12-12 06:31:07 +00001061///
1062/// This function parses and performs error recovery on the specified condition
1063/// or expression (depending on whether we're in C++ or C mode). This function
1064/// goes out of its way to recover well. It returns true if there was a parser
1065/// error (the right paren couldn't be found), which indicates that the caller
1066/// should try to recover harder. It returns false if the condition is
1067/// successfully parsed. Note that a successful parse can still have semantic
1068/// errors in the condition.
Richard Smithc7a05a92016-06-29 21:17:59 +00001069bool Parser::ParseParenExprOrCondition(StmtResult *InitStmt,
1070 Sema::ConditionResult &Cond,
Douglas Gregore60e41a2010-05-06 17:25:47 +00001071 SourceLocation Loc,
Richard Smith03a4aa32016-06-23 19:02:52 +00001072 Sema::ConditionKind CK) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001073 BalancedDelimiterTracker T(*this, tok::l_paren);
1074 T.consumeOpen();
1075
David Blaikiebbafb8a2012-03-11 07:00:24 +00001076 if (getLangOpts().CPlusPlus)
Richard Smithc7a05a92016-06-29 21:17:59 +00001077 Cond = ParseCXXCondition(InitStmt, Loc, CK);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001078 else {
Richard Smith03a4aa32016-06-23 19:02:52 +00001079 ExprResult CondExpr = ParseExpression();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001080
Douglas Gregore60e41a2010-05-06 17:25:47 +00001081 // If required, convert to a boolean value.
Richard Smith03a4aa32016-06-23 19:02:52 +00001082 if (CondExpr.isInvalid())
1083 Cond = Sema::ConditionError();
1084 else
1085 Cond = Actions.ActOnCondition(getCurScope(), Loc, CondExpr.get(), CK);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001086 }
Mike Stump11289f42009-09-09 15:08:12 +00001087
Chris Lattnerc0081db2008-12-12 06:31:07 +00001088 // If the parser was confused by the condition and we don't have a ')', try to
1089 // recover by skipping ahead to a semi and bailing out. If condexp is
1090 // semantically invalid but we have well formed code, keep going.
Richard Smith03a4aa32016-06-23 19:02:52 +00001091 if (Cond.isInvalid() && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +00001092 SkipUntil(tok::semi);
1093 // Skipping may have stopped if it found the containing ')'. If so, we can
1094 // continue parsing the if statement.
1095 if (Tok.isNot(tok::r_paren))
1096 return true;
1097 }
Mike Stump11289f42009-09-09 15:08:12 +00001098
Chris Lattnerc0081db2008-12-12 06:31:07 +00001099 // Otherwise the condition is valid or the rparen is present.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001100 T.consumeClose();
Chad Rosier67055f52012-07-10 21:35:27 +00001101
Chris Lattner70d44982012-04-28 16:24:20 +00001102 // Check for extraneous ')'s to catch things like "if (foo())) {". We know
1103 // that all callers are looking for a statement after the condition, so ")"
1104 // isn't valid.
1105 while (Tok.is(tok::r_paren)) {
1106 Diag(Tok, diag::err_extraneous_rparen_in_condition)
1107 << FixItHint::CreateRemoval(Tok.getLocation());
1108 ConsumeParen();
1109 }
Chad Rosier67055f52012-07-10 21:35:27 +00001110
Chris Lattnerc0081db2008-12-12 06:31:07 +00001111 return false;
1112}
1113
1114
Chris Lattnerc951dae2006-08-10 04:23:57 +00001115/// ParseIfStatement
1116/// if-statement: [C99 6.8.4.1]
1117/// 'if' '(' expression ')' statement
1118/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001119/// [C++] 'if' '(' condition ')' statement
1120/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +00001121///
Richard Smithc202b282012-04-14 00:33:13 +00001122StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001123 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001124 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +00001125
Richard Smithb130fe72016-06-23 19:16:49 +00001126 bool IsConstexpr = false;
1127 if (Tok.is(tok::kw_constexpr)) {
1128 Diag(Tok, getLangOpts().CPlusPlus1z ? diag::warn_cxx14_compat_constexpr_if
1129 : diag::ext_constexpr_if);
1130 IsConstexpr = true;
1131 ConsumeToken();
1132 }
1133
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001134 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001135 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +00001136 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +00001137 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +00001138 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001139
David Blaikiebbafb8a2012-03-11 07:00:24 +00001140 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001141
Chris Lattner2dd1b722007-08-26 23:08:06 +00001142 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
1143 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001144 //
1145 // C++ 6.4p3:
1146 // A name introduced by a declaration in a condition is in scope from its
1147 // point of declaration until the end of the substatements controlled by the
1148 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001149 // C++ 3.3.2p4:
1150 // Names declared in the for-init-statement, and in the condition of if,
1151 // while, for, and switch statements are local to the if, while, for, or
1152 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001153 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001154 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +00001155
Chris Lattnerc951dae2006-08-10 04:23:57 +00001156 // Parse the condition.
Richard Smithc7a05a92016-06-29 21:17:59 +00001157 StmtResult InitStmt;
Richard Smith03a4aa32016-06-23 19:02:52 +00001158 Sema::ConditionResult Cond;
Richard Smithc7a05a92016-06-29 21:17:59 +00001159 if (ParseParenExprOrCondition(&InitStmt, Cond, IfLoc,
Richard Smithb130fe72016-06-23 19:16:49 +00001160 IsConstexpr ? Sema::ConditionKind::ConstexprIf
1161 : Sema::ConditionKind::Boolean))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001162 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +00001163
Richard Smithb130fe72016-06-23 19:16:49 +00001164 llvm::Optional<bool> ConstexprCondition;
1165 if (IsConstexpr)
1166 ConstexprCondition = Cond.getKnownValue();
1167
Chris Lattner8fb26252007-08-22 05:28:50 +00001168 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001169 // there is no compound stmt. C90 does not have this clause. We only do this
1170 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001171 //
1172 // C++ 6.4p1:
1173 // The substatement in a selection-statement (each substatement, in the else
1174 // form of the if statement) implicitly defines a local scope.
1175 //
1176 // For C++ we create a scope for the condition and a new scope for
1177 // substatements because:
1178 // -When the 'then' scope exits, we want the condition declaration to still be
1179 // active for the 'else' scope too.
1180 // -Sema will detect name clashes by considering declarations of a
1181 // 'ControlScope' as part of its direct subscope.
1182 // -If we wanted the condition and substatement to be in the same scope, we
1183 // would have to notify ParseStatement not to create a new scope. It's
1184 // simpler to let it create a new scope.
1185 //
David Majnemer2206bf52014-03-05 08:57:59 +00001186 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001187
Chris Lattner5c5808a2007-10-29 05:08:52 +00001188 // Read the 'then' stmt.
1189 SourceLocation ThenStmtLoc = Tok.getLocation();
Nico Weber3cef1082011-12-22 23:26:17 +00001190
1191 SourceLocation InnerStatementTrailingElseLoc;
Richard Smithb130fe72016-06-23 19:16:49 +00001192 StmtResult ThenStmt;
1193 {
1194 EnterExpressionEvaluationContext PotentiallyDiscarded(
Faisal Valid143a0c2017-04-01 21:30:49 +00001195 Actions, Sema::ExpressionEvaluationContext::DiscardedStatement, nullptr,
1196 false,
Richard Smithb130fe72016-06-23 19:16:49 +00001197 /*ShouldEnter=*/ConstexprCondition && !*ConstexprCondition);
1198 ThenStmt = ParseStatement(&InnerStatementTrailingElseLoc);
1199 }
Chris Lattnerac4471c2007-05-28 05:38:24 +00001200
Chris Lattner37e54f42007-08-22 05:16:28 +00001201 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001202 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001203
Chris Lattnerc951dae2006-08-10 04:23:57 +00001204 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +00001205 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +00001206 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +00001207 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001208
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001209 if (Tok.is(tok::kw_else)) {
Nico Weber3cef1082011-12-22 23:26:17 +00001210 if (TrailingElseLoc)
1211 *TrailingElseLoc = Tok.getLocation();
1212
Chris Lattneraf635312006-10-16 06:06:51 +00001213 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +00001214 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +00001215
Chris Lattner8fb26252007-08-22 05:28:50 +00001216 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001217 // there is no compound stmt. C90 does not have this clause. We only do
1218 // this if the body isn't a compound statement to avoid push/pop in common
1219 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001220 //
1221 // C++ 6.4p1:
1222 // The substatement in a selection-statement (each substatement, in the else
1223 // form of the if statement) implicitly defines a local scope.
1224 //
Richard Smithb130fe72016-06-23 19:16:49 +00001225 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX,
1226 Tok.is(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001227
Richard Smithb130fe72016-06-23 19:16:49 +00001228 EnterExpressionEvaluationContext PotentiallyDiscarded(
Faisal Valid143a0c2017-04-01 21:30:49 +00001229 Actions, Sema::ExpressionEvaluationContext::DiscardedStatement, nullptr,
1230 false,
Richard Smithb130fe72016-06-23 19:16:49 +00001231 /*ShouldEnter=*/ConstexprCondition && *ConstexprCondition);
Chris Lattner30f910e2006-10-16 05:52:41 +00001232 ElseStmt = ParseStatement();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001233
Chris Lattner37e54f42007-08-22 05:16:28 +00001234 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001235 InnerScope.Exit();
Douglas Gregor4ecb7202011-07-30 08:36:53 +00001236 } else if (Tok.is(tok::code_completion)) {
1237 Actions.CodeCompleteAfterIf(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001238 cutOffParsing();
1239 return StmtError();
Nico Weber3cef1082011-12-22 23:26:17 +00001240 } else if (InnerStatementTrailingElseLoc.isValid()) {
1241 Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else);
Chris Lattnerc951dae2006-08-10 04:23:57 +00001242 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001243
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001244 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +00001245
Chris Lattner5c5808a2007-10-29 05:08:52 +00001246 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +00001247 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +00001248 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001249 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
Craig Topper161e4db2014-05-21 06:02:52 +00001250 (ThenStmt.isInvalid() && ElseStmt.get() == nullptr) ||
1251 (ThenStmt.get() == nullptr && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +00001252 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +00001253 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +00001254 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001255
Chris Lattner5c5808a2007-10-29 05:08:52 +00001256 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001257 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001258 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001259 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001260 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001261
Richard Smithc7a05a92016-06-29 21:17:59 +00001262 return Actions.ActOnIfStmt(IfLoc, IsConstexpr, InitStmt.get(), Cond,
1263 ThenStmt.get(), ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +00001264}
1265
Chris Lattner9075bd72006-08-10 04:59:57 +00001266/// ParseSwitchStatement
1267/// switch-statement:
1268/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001269/// [C++] 'switch' '(' condition ')' statement
Richard Smithc202b282012-04-14 00:33:13 +00001270StmtResult Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001271 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001272 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +00001273
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001274 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001275 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +00001276 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001277 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001278 }
Chris Lattner2dd1b722007-08-26 23:08:06 +00001279
David Blaikiebbafb8a2012-03-11 07:00:24 +00001280 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001281
Chris Lattner2dd1b722007-08-26 23:08:06 +00001282 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1283 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001284 //
1285 // C++ 6.4p3:
1286 // A name introduced by a declaration in a condition is in scope from its
1287 // point of declaration until the end of the substatements controlled by the
1288 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001289 // C++ 3.3.2p4:
1290 // Names declared in the for-init-statement, and in the condition of if,
1291 // while, for, and switch statements are local to the if, while, for, or
1292 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001293 //
Serge Pavlov09f99242014-01-23 15:05:00 +00001294 unsigned ScopeFlags = Scope::SwitchScope;
Chris Lattnerc0081db2008-12-12 06:31:07 +00001295 if (C99orCXX)
1296 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001297 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001298
Chris Lattner9075bd72006-08-10 04:59:57 +00001299 // Parse the condition.
Richard Smithc7a05a92016-06-29 21:17:59 +00001300 StmtResult InitStmt;
Richard Smith03a4aa32016-06-23 19:02:52 +00001301 Sema::ConditionResult Cond;
Richard Smithc7a05a92016-06-29 21:17:59 +00001302 if (ParseParenExprOrCondition(&InitStmt, Cond, SwitchLoc,
1303 Sema::ConditionKind::Switch))
Sebastian Redlb62406f2008-12-11 19:48:14 +00001304 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +00001305
Richard Smithc7a05a92016-06-29 21:17:59 +00001306 StmtResult Switch =
1307 Actions.ActOnStartOfSwitchStmt(SwitchLoc, InitStmt.get(), Cond);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001308
Douglas Gregore60e41a2010-05-06 17:25:47 +00001309 if (Switch.isInvalid()) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001310 // Skip the switch body.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001311 // FIXME: This is not optimal recovery, but parsing the body is more
1312 // dangerous due to the presence of case and default statements, which
1313 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +00001314 if (Tok.is(tok::l_brace)) {
1315 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001316 SkipUntil(tok::r_brace);
Douglas Gregor4abc32d2010-05-20 23:20:59 +00001317 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +00001318 SkipUntil(tok::semi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001319 return Switch;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001320 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001321
Chris Lattner8fb26252007-08-22 05:28:50 +00001322 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001323 // there is no compound stmt. C90 does not have this clause. We only do this
1324 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001325 //
1326 // C++ 6.4p1:
1327 // The substatement in a selection-statement (each substatement, in the else
1328 // form of the if statement) implicitly defines a local scope.
1329 //
1330 // See comments in ParseIfStatement for why we create a scope for the
1331 // condition and a new scope for substatement in C++.
1332 //
Serge Pavlov09f99242014-01-23 15:05:00 +00001333 getCurScope()->AddFlags(Scope::BreakScope);
David Majnemer2206bf52014-03-05 08:57:59 +00001334 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +00001335
Hans Wennborg852c3462014-06-17 00:09:05 +00001336 // We have incremented the mangling number for the SwitchScope and the
1337 // InnerScope, which is one too many.
1338 if (C99orCXX)
David Majnemera7f8c462015-03-19 21:54:30 +00001339 getCurScope()->decrementMSManglingNumber();
Hans Wennborg852c3462014-06-17 00:09:05 +00001340
Chris Lattner9075bd72006-08-10 04:59:57 +00001341 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001342 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001343
Chris Lattner8fd2d012010-01-24 01:50:29 +00001344 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001345 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001346 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001347
John McCallb268a282010-08-23 23:25:46 +00001348 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001349}
1350
1351/// ParseWhileStatement
1352/// while-statement: [C99 6.8.5.1]
1353/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001354/// [C++] 'while' '(' condition ')' statement
Richard Smithc202b282012-04-14 00:33:13 +00001355StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001356 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +00001357 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +00001358 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001359
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001360 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001361 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +00001362 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001363 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001364 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001365
David Blaikiebbafb8a2012-03-11 07:00:24 +00001366 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001367
Chris Lattner2dd1b722007-08-26 23:08:06 +00001368 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1369 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001370 //
1371 // C++ 6.4p3:
1372 // A name introduced by a declaration in a condition is in scope from its
1373 // point of declaration until the end of the substatements controlled by the
1374 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001375 // C++ 3.3.2p4:
1376 // Names declared in the for-init-statement, and in the condition of if,
1377 // while, for, and switch statements are local to the if, while, for, or
1378 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001379 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001380 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001381 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001382 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1383 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001384 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001385 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1386 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001387
Chris Lattner9075bd72006-08-10 04:59:57 +00001388 // Parse the condition.
Richard Smith03a4aa32016-06-23 19:02:52 +00001389 Sema::ConditionResult Cond;
Richard Smithc7a05a92016-06-29 21:17:59 +00001390 if (ParseParenExprOrCondition(nullptr, Cond, WhileLoc,
1391 Sema::ConditionKind::Boolean))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001392 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001393
Justin Bognere4ebb6c2013-12-03 07:36:55 +00001394 // C99 6.8.5p5 - In C99, the body of the while statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001395 // there is no compound stmt. C90 does not have this clause. We only do this
1396 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001397 //
1398 // C++ 6.5p2:
1399 // The substatement in an iteration-statement implicitly defines a local scope
1400 // which is entered and exited each time through the loop.
1401 //
1402 // See comments in ParseIfStatement for why we create a scope for the
1403 // condition and a new scope for substatement in C++.
1404 //
David Majnemer2206bf52014-03-05 08:57:59 +00001405 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001406
Chris Lattner9075bd72006-08-10 04:59:57 +00001407 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001408 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001409
Chris Lattner8fb26252007-08-22 05:28:50 +00001410 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001411 InnerScope.Exit();
1412 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001413
Richard Smith03a4aa32016-06-23 19:02:52 +00001414 if (Cond.isInvalid() || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001415 return StmtError();
1416
Richard Smith03a4aa32016-06-23 19:02:52 +00001417 return Actions.ActOnWhileStmt(WhileLoc, Cond, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001418}
1419
1420/// ParseDoStatement
1421/// do-statement: [C99 6.8.5.2]
1422/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +00001423/// Note: this lets the caller parse the end ';'.
Richard Smithc202b282012-04-14 00:33:13 +00001424StmtResult Parser::ParseDoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001425 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001426 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001427
Chris Lattner2dd1b722007-08-26 23:08:06 +00001428 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1429 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001430 unsigned ScopeFlags;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001431 if (getLangOpts().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001432 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001433 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001434 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001435
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001436 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001437
Justin Bognere4ebb6c2013-12-03 07:36:55 +00001438 // C99 6.8.5p5 - In C99, the body of the do statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001439 // there is no compound stmt. C90 does not have this clause. We only do this
1440 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +00001441 //
1442 // C++ 6.5p2:
1443 // The substatement in an iteration-statement implicitly defines a local scope
1444 // which is entered and exited each time through the loop.
1445 //
David Majnemer2206bf52014-03-05 08:57:59 +00001446 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
1447 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001448
Chris Lattner9075bd72006-08-10 04:59:57 +00001449 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001450 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +00001451
Chris Lattner8fb26252007-08-22 05:28:50 +00001452 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001453 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001454
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001455 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001456 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +00001457 Diag(Tok, diag::err_expected_while);
Alp Tokerec543272013-12-24 09:48:30 +00001458 Diag(DoLoc, diag::note_matching) << "'do'";
Alexey Bataevee6507d2013-11-18 08:17:37 +00001459 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner0046de12008-11-13 18:52:53 +00001460 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001461 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001462 }
Chris Lattneraf635312006-10-16 06:06:51 +00001463 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001464
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001465 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001466 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Alexey Bataevee6507d2013-11-18 08:17:37 +00001467 SkipUntil(tok::semi, StopBeforeMatch);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001468 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001469 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001470
Richard Smithc2c8bb82013-10-15 01:34:54 +00001471 // Parse the parenthesized expression.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001472 BalancedDelimiterTracker T(*this, tok::l_paren);
1473 T.consumeOpen();
Chad Rosier67055f52012-07-10 21:35:27 +00001474
Richard Smithc2c8bb82013-10-15 01:34:54 +00001475 // A do-while expression is not a condition, so can't have attributes.
1476 DiagnoseAndSkipCXX11Attributes();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001477
John McCalldadc5752010-08-24 06:29:42 +00001478 ExprResult Cond = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001479 T.consumeClose();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001480 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001481
Sebastian Redlb62406f2008-12-11 19:48:14 +00001482 if (Cond.isInvalid() || Body.isInvalid())
1483 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001484
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001485 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(),
1486 Cond.get(), T.getCloseLocation());
Chris Lattner9075bd72006-08-10 04:59:57 +00001487}
1488
Richard Smith955bf012014-06-19 11:42:00 +00001489bool Parser::isForRangeIdentifier() {
1490 assert(Tok.is(tok::identifier));
1491
1492 const Token &Next = NextToken();
1493 if (Next.is(tok::colon))
1494 return true;
1495
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001496 if (Next.isOneOf(tok::l_square, tok::kw_alignas)) {
Richard Smith955bf012014-06-19 11:42:00 +00001497 TentativeParsingAction PA(*this);
1498 ConsumeToken();
1499 SkipCXX11Attributes();
1500 bool Result = Tok.is(tok::colon);
1501 PA.Revert();
1502 return Result;
1503 }
1504
1505 return false;
1506}
1507
Chris Lattner9075bd72006-08-10 04:59:57 +00001508/// ParseForStatement
1509/// for-statement: [C99 6.8.5.3]
1510/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1511/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001512/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1513/// [C++] statement
Richard Smith0e304ea2015-10-22 04:46:14 +00001514/// [C++0x] 'for'
1515/// 'co_await'[opt] [Coroutines]
1516/// '(' for-range-declaration ':' for-range-initializer ')'
1517/// statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001518/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1519/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001520///
1521/// [C++] for-init-statement:
1522/// [C++] expression-statement
1523/// [C++] simple-declaration
1524///
Richard Smith02e85f32011-04-14 22:09:26 +00001525/// [C++0x] for-range-declaration:
1526/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1527/// [C++0x] for-range-initializer:
1528/// [C++0x] expression
1529/// [C++0x] braced-init-list [TODO]
Richard Smithc202b282012-04-14 00:33:13 +00001530StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001531 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001532 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001533
Richard Smith0e304ea2015-10-22 04:46:14 +00001534 SourceLocation CoawaitLoc;
1535 if (Tok.is(tok::kw_co_await))
1536 CoawaitLoc = ConsumeToken();
1537
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001538 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001539 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +00001540 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001541 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001542 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001543
Chad Rosier67055f52012-07-10 21:35:27 +00001544 bool C99orCXXorObjC = getLangOpts().C99 || getLangOpts().CPlusPlus ||
1545 getLangOpts().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001546
Chris Lattner2dd1b722007-08-26 23:08:06 +00001547 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1548 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001549 //
1550 // C++ 6.4p3:
1551 // A name introduced by a declaration in a condition is in scope from its
1552 // point of declaration until the end of the substatements controlled by the
1553 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001554 // C++ 3.3.2p4:
1555 // Names declared in the for-init-statement, and in the condition of if,
1556 // while, for, and switch statements are local to the if, while, for, or
1557 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001558 // C++ 6.5.3p1:
1559 // Names declared in the for-init-statement are in the same declarative-region
1560 // as those declared in the condition.
1561 //
Serge Pavlov09f99242014-01-23 15:05:00 +00001562 unsigned ScopeFlags = 0;
Chris Lattner934074c2009-04-22 00:54:41 +00001563 if (C99orCXXorObjC)
Serge Pavlov09f99242014-01-23 15:05:00 +00001564 ScopeFlags = Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001565
1566 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +00001567
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001568 BalancedDelimiterTracker T(*this, tok::l_paren);
1569 T.consumeOpen();
1570
John McCalldadc5752010-08-24 06:29:42 +00001571 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001572
Richard Smith02e85f32011-04-14 22:09:26 +00001573 bool ForEach = false, ForRange = false;
John McCalldadc5752010-08-24 06:29:42 +00001574 StmtResult FirstPart;
Richard Smith03a4aa32016-06-23 19:02:52 +00001575 Sema::ConditionResult SecondPart;
John McCalldadc5752010-08-24 06:29:42 +00001576 ExprResult Collection;
Richard Smith02e85f32011-04-14 22:09:26 +00001577 ForRangeInit ForRangeInit;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001578 FullExprArg ThirdPart(Actions);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001579
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001580 if (Tok.is(tok::code_completion)) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001581 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001582 C99orCXXorObjC? Sema::PCC_ForInit
1583 : Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001584 cutOffParsing();
1585 return StmtError();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001586 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001587
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001588 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001589 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001590
Chris Lattner9075bd72006-08-10 04:59:57 +00001591 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001592 if (Tok.is(tok::semi)) { // for (;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001593 ProhibitAttributes(attrs);
Chris Lattner53361ac2006-08-10 05:19:57 +00001594 // no first part, eat the ';'.
1595 ConsumeToken();
Richard Smith955bf012014-06-19 11:42:00 +00001596 } else if (getLangOpts().CPlusPlus && Tok.is(tok::identifier) &&
1597 isForRangeIdentifier()) {
1598 ProhibitAttributes(attrs);
1599 IdentifierInfo *Name = Tok.getIdentifierInfo();
1600 SourceLocation Loc = ConsumeToken();
1601 MaybeParseCXX11Attributes(attrs);
1602
1603 ForRangeInit.ColonLoc = ConsumeToken();
1604 if (Tok.is(tok::l_brace))
1605 ForRangeInit.RangeExpr = ParseBraceInitializer();
1606 else
1607 ForRangeInit.RangeExpr = ParseExpression();
1608
Richard Smith83d3f152014-11-27 01:54:27 +00001609 Diag(Loc, diag::err_for_range_identifier)
Richard Smith955bf012014-06-19 11:42:00 +00001610 << ((getLangOpts().CPlusPlus11 && !getLangOpts().CPlusPlus1z)
1611 ? FixItHint::CreateInsertion(Loc, "auto &&")
1612 : FixItHint());
1613
1614 FirstPart = Actions.ActOnCXXForRangeIdentifier(getCurScope(), Loc, Name,
1615 attrs, attrs.Range.getEnd());
1616 ForRange = true;
Eli Friedman0ffc31c2011-12-20 01:50:37 +00001617 } else if (isForInitDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001618 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001619 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001620 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001621
Richard Smith02e85f32011-04-14 22:09:26 +00001622 // In C++0x, "for (T NS:a" might not be a typo for ::
David Blaikiebbafb8a2012-03-11 07:00:24 +00001623 bool MightBeForRangeStmt = getLangOpts().CPlusPlus;
Richard Smith02e85f32011-04-14 22:09:26 +00001624 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1625
Chris Lattner49836b42009-04-02 04:16:50 +00001626 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00001627 DeclGroupPtrTy DG = ParseSimpleDeclaration(
Rafael Espindola1bd906d2014-10-22 14:27:08 +00001628 Declarator::ForContext, DeclEnd, attrs, false,
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00001629 MightBeForRangeStmt ? &ForRangeInit : nullptr);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001630 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Richard Smith02e85f32011-04-14 22:09:26 +00001631 if (ForRangeInit.ParsedForRangeDecl()) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001632 Diag(ForRangeInit.ColonLoc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00001633 diag::warn_cxx98_compat_for_range : diag::ext_for_range);
Richard Smith58c74332011-09-04 19:54:14 +00001634
Richard Smith02e85f32011-04-14 22:09:26 +00001635 ForRange = true;
1636 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattner32dc41c2009-03-29 17:27:48 +00001637 ConsumeToken();
1638 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001639 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001640 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001641 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001642
Douglas Gregor68762e72010-08-23 21:17:50 +00001643 if (Tok.is(tok::code_completion)) {
1644 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001645 cutOffParsing();
1646 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001647 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001648 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001649 } else {
1650 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001651 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001652 } else {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001653 ProhibitAttributes(attrs);
Kaelyn Takatab16e6322014-11-20 22:06:40 +00001654 Value = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Chris Lattner71e23ce2006-11-04 20:18:38 +00001655
John McCall34376a62010-12-04 03:47:34 +00001656 ForEach = isTokIdentifier_in();
1657
Chris Lattnercd68f642007-06-27 01:06:29 +00001658 // Turn the expression into a stmt.
John McCall34376a62010-12-04 03:47:34 +00001659 if (!Value.isInvalid()) {
1660 if (ForEach)
1661 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1662 else
Richard Smith945f8d32013-01-14 22:39:08 +00001663 FirstPart = Actions.ActOnExprStmt(Value);
John McCall34376a62010-12-04 03:47:34 +00001664 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001665
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001666 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001667 ConsumeToken();
John McCall34376a62010-12-04 03:47:34 +00001668 } else if (ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001669 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001670
Douglas Gregor68762e72010-08-23 21:17:50 +00001671 if (Tok.is(tok::code_completion)) {
David Blaikie0403cb12016-01-15 23:43:25 +00001672 Actions.CodeCompleteObjCForCollection(getCurScope(), nullptr);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001673 cutOffParsing();
1674 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001675 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001676 Collection = ParseExpression();
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001677 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::colon) && FirstPart.get()) {
Richard Smith4f848f12011-12-20 22:56:20 +00001678 // User tried to write the reasonable, but ill-formed, for-range-statement
1679 // for (expr : expr) { ... }
1680 Diag(Tok, diag::err_for_range_expected_decl)
1681 << FirstPart.get()->getSourceRange();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001682 SkipUntil(tok::r_paren, StopBeforeMatch);
Richard Smith03a4aa32016-06-23 19:02:52 +00001683 SecondPart = Sema::ConditionError();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001684 } else {
Douglas Gregor230a7e62011-02-17 03:38:46 +00001685 if (!Value.isInvalid()) {
1686 Diag(Tok, diag::err_expected_semi_for);
1687 } else {
1688 // Skip until semicolon or rparen, don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001689 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor230a7e62011-02-17 03:38:46 +00001690 if (Tok.is(tok::semi))
1691 ConsumeToken();
1692 }
Chris Lattner53361ac2006-08-10 05:19:57 +00001693 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001694 }
Serge Pavlov09f99242014-01-23 15:05:00 +00001695
1696 // Parse the second part of the for specifier.
1697 getCurScope()->AddFlags(Scope::BreakScope | Scope::ContinueScope);
Richard Smith03a4aa32016-06-23 19:02:52 +00001698 if (!ForEach && !ForRange && !SecondPart.isInvalid()) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001699 // Parse the second part of the for specifier.
1700 if (Tok.is(tok::semi)) { // for (...;;
1701 // no second part.
Douglas Gregor230a7e62011-02-17 03:38:46 +00001702 } else if (Tok.is(tok::r_paren)) {
1703 // missing both semicolons.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001704 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001705 if (getLangOpts().CPlusPlus)
Richard Smithc7a05a92016-06-29 21:17:59 +00001706 SecondPart =
1707 ParseCXXCondition(nullptr, ForLoc, Sema::ConditionKind::Boolean);
Douglas Gregore60e41a2010-05-06 17:25:47 +00001708 else {
Richard Smith03a4aa32016-06-23 19:02:52 +00001709 ExprResult SecondExpr = ParseExpression();
1710 if (SecondExpr.isInvalid())
1711 SecondPart = Sema::ConditionError();
1712 else
1713 SecondPart =
1714 Actions.ActOnCondition(getCurScope(), ForLoc, SecondExpr.get(),
1715 Sema::ConditionKind::Boolean);
Douglas Gregore60e41a2010-05-06 17:25:47 +00001716 }
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001717 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001718
Douglas Gregor230a7e62011-02-17 03:38:46 +00001719 if (Tok.isNot(tok::semi)) {
Richard Smith03a4aa32016-06-23 19:02:52 +00001720 if (!SecondPart.isInvalid())
Douglas Gregor230a7e62011-02-17 03:38:46 +00001721 Diag(Tok, diag::err_expected_semi_for);
1722 else
1723 // Skip until semicolon or rparen, don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001724 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor230a7e62011-02-17 03:38:46 +00001725 }
1726
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001727 if (Tok.is(tok::semi)) {
1728 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001729 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001730
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001731 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001732 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001733 ExprResult Third = ParseExpression();
Richard Smith945f8d32013-01-14 22:39:08 +00001734 // FIXME: The C++11 standard doesn't actually say that this is a
1735 // discarded-value expression, but it clearly should be.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001736 ThirdPart = Actions.MakeFullDiscardedValueExpr(Third.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001737 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001738 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001739 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001740 T.consumeClose();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001741
Richard Smith0e304ea2015-10-22 04:46:14 +00001742 // C++ Coroutines [stmt.iter]:
1743 // 'co_await' can only be used for a range-based for statement.
1744 if (CoawaitLoc.isValid() && !ForRange) {
1745 Diag(CoawaitLoc, diag::err_for_co_await_not_range_for);
1746 CoawaitLoc = SourceLocation();
1747 }
1748
Richard Smith02e85f32011-04-14 22:09:26 +00001749 // We need to perform most of the semantic analysis for a C++0x for-range
1750 // statememt before parsing the body, in order to be able to deduce the type
1751 // of an auto-typed loop variable.
1752 StmtResult ForRangeStmt;
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001753 StmtResult ForEachStmt;
Chad Rosier67055f52012-07-10 21:35:27 +00001754
John McCall53848232011-07-27 01:07:15 +00001755 if (ForRange) {
Denis Zobnin7d6b9242016-02-02 17:33:09 +00001756 ExprResult CorrectedRange =
1757 Actions.CorrectDelayedTyposInExpr(ForRangeInit.RangeExpr.get());
Richard Smith9f690bd2015-10-27 06:02:45 +00001758 ForRangeStmt = Actions.ActOnCXXForRangeStmt(
1759 getCurScope(), ForLoc, CoawaitLoc, FirstPart.get(),
Denis Zobnin7d6b9242016-02-02 17:33:09 +00001760 ForRangeInit.ColonLoc, CorrectedRange.get(),
Richard Smith9f690bd2015-10-27 06:02:45 +00001761 T.getCloseLocation(), Sema::BFRK_Build);
John McCall53848232011-07-27 01:07:15 +00001762
1763 // Similarly, we need to do the semantic analysis for a for-range
1764 // statement immediately in order to close over temporaries correctly.
1765 } else if (ForEach) {
Sam Panzer2c4ca0f2012-08-16 21:47:25 +00001766 ForEachStmt = Actions.ActOnObjCForCollectionStmt(ForLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001767 FirstPart.get(),
1768 Collection.get(),
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001769 T.getCloseLocation());
Alexey Bataev9c821032015-04-30 04:23:23 +00001770 } else {
1771 // In OpenMP loop region loop control variable must be captured and be
1772 // private. Perform analysis of first part (if any).
1773 if (getLangOpts().OpenMP && FirstPart.isUsable()) {
1774 Actions.ActOnOpenMPLoopInitialization(ForLoc, FirstPart.get());
1775 }
John McCall53848232011-07-27 01:07:15 +00001776 }
1777
Justin Bognere4ebb6c2013-12-03 07:36:55 +00001778 // C99 6.8.5p5 - In C99, the body of the for statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001779 // there is no compound stmt. C90 does not have this clause. We only do this
1780 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001781 //
1782 // C++ 6.5p2:
1783 // The substatement in an iteration-statement implicitly defines a local scope
1784 // which is entered and exited each time through the loop.
1785 //
1786 // See comments in ParseIfStatement for why we create a scope for
1787 // for-init-statement/condition and a new scope for substatement in C++.
1788 //
David Majnemer2206bf52014-03-05 08:57:59 +00001789 ParseScope InnerScope(this, Scope::DeclScope, C99orCXXorObjC,
1790 Tok.is(tok::l_brace));
1791
1792 // The body of the for loop has the same local mangling number as the
1793 // for-init-statement.
1794 // It will only be incremented if the body contains other things that would
1795 // normally increment the mangling number (like a compound statement).
1796 if (C99orCXXorObjC)
David Majnemera7f8c462015-03-19 21:54:30 +00001797 getCurScope()->decrementMSManglingNumber();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001798
Chris Lattner9075bd72006-08-10 04:59:57 +00001799 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001800 StmtResult Body(ParseStatement(TrailingElseLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001801
Chris Lattner8fb26252007-08-22 05:28:50 +00001802 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001803 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001804
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001805 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001806 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001807
1808 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001809 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001810
Richard Smith02e85f32011-04-14 22:09:26 +00001811 if (ForEach)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001812 return Actions.FinishObjCForCollectionStmt(ForEachStmt.get(),
1813 Body.get());
Mike Stump11289f42009-09-09 15:08:12 +00001814
Richard Smith02e85f32011-04-14 22:09:26 +00001815 if (ForRange)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001816 return Actions.FinishCXXForRangeStmt(ForRangeStmt.get(), Body.get());
Richard Smith02e85f32011-04-14 22:09:26 +00001817
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001818 return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.get(),
Richard Smith03a4aa32016-06-23 19:02:52 +00001819 SecondPart, ThirdPart, T.getCloseLocation(),
1820 Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001821}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001822
Chris Lattner503fadc2006-08-10 05:45:44 +00001823/// ParseGotoStatement
1824/// jump-statement:
1825/// 'goto' identifier ';'
1826/// [GNU] 'goto' '*' expression ';'
1827///
1828/// Note: this lets the caller parse the end ';'.
1829///
Richard Smithc202b282012-04-14 00:33:13 +00001830StmtResult Parser::ParseGotoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001831 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001832 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001833
John McCalldadc5752010-08-24 06:29:42 +00001834 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001835 if (Tok.is(tok::identifier)) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +00001836 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1837 Tok.getLocation());
1838 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Chris Lattner503fadc2006-08-10 05:45:44 +00001839 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001840 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001841 // GNU indirect goto extension.
1842 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001843 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001844 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001845 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001846 SkipUntil(tok::semi, StopBeforeMatch);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001847 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001848 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001849 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.get());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001850 } else {
Alp Tokerec543272013-12-24 09:48:30 +00001851 Diag(Tok, diag::err_expected) << tok::identifier;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001852 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001853 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001854
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001855 return Res;
Chris Lattner503fadc2006-08-10 05:45:44 +00001856}
1857
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001858/// ParseContinueStatement
1859/// jump-statement:
1860/// 'continue' ';'
1861///
1862/// Note: this lets the caller parse the end ';'.
1863///
Richard Smithc202b282012-04-14 00:33:13 +00001864StmtResult Parser::ParseContinueStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001865 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001866 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001867}
1868
1869/// ParseBreakStatement
1870/// jump-statement:
1871/// 'break' ';'
1872///
1873/// Note: this lets the caller parse the end ';'.
1874///
Richard Smithc202b282012-04-14 00:33:13 +00001875StmtResult Parser::ParseBreakStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001876 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001877 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001878}
1879
Chris Lattner503fadc2006-08-10 05:45:44 +00001880/// ParseReturnStatement
1881/// jump-statement:
1882/// 'return' expression[opt] ';'
Richard Smith0e304ea2015-10-22 04:46:14 +00001883/// 'return' braced-init-list ';'
1884/// 'co_return' expression[opt] ';'
1885/// 'co_return' braced-init-list ';'
Richard Smithc202b282012-04-14 00:33:13 +00001886StmtResult Parser::ParseReturnStatement() {
Richard Smith0e304ea2015-10-22 04:46:14 +00001887 assert((Tok.is(tok::kw_return) || Tok.is(tok::kw_co_return)) &&
1888 "Not a return stmt!");
1889 bool IsCoreturn = Tok.is(tok::kw_co_return);
Chris Lattneraf635312006-10-16 06:06:51 +00001890 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001891
John McCalldadc5752010-08-24 06:29:42 +00001892 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001893 if (Tok.isNot(tok::semi)) {
Richard Smith0e304ea2015-10-22 04:46:14 +00001894 // FIXME: Code completion for co_return.
1895 if (Tok.is(tok::code_completion) && !IsCoreturn) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001896 Actions.CodeCompleteReturn(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001897 cutOffParsing();
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001898 return StmtError();
1899 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001900
David Blaikiebbafb8a2012-03-11 07:00:24 +00001901 if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus) {
Douglas Gregore9e27d92011-03-11 23:10:44 +00001902 R = ParseInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00001903 if (R.isUsable())
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001904 Diag(R.get()->getLocStart(), getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00001905 diag::warn_cxx98_compat_generalized_initializer_lists :
1906 diag::ext_generalized_initializer_lists)
Douglas Gregore9e27d92011-03-11 23:10:44 +00001907 << R.get()->getSourceRange();
1908 } else
Nico Weber3ce01c32015-01-04 08:07:54 +00001909 R = ParseExpression();
Serge Pavlovf79bd5c2013-12-04 03:51:59 +00001910 if (R.isInvalid()) {
1911 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001912 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001913 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001914 }
Richard Smithcfd53b42015-10-22 06:13:50 +00001915 if (IsCoreturn)
Eric Fiselier20f25cb2017-03-06 23:38:15 +00001916 return Actions.ActOnCoreturnStmt(getCurScope(), ReturnLoc, R.get());
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001917 return Actions.ActOnReturnStmt(ReturnLoc, R.get(), getCurScope());
Chris Lattner503fadc2006-08-10 05:45:44 +00001918}
Chris Lattner0116c472006-08-15 06:03:28 +00001919
Alexey Bataevc4fad652016-01-13 11:18:54 +00001920StmtResult Parser::ParsePragmaLoopHint(StmtVector &Stmts,
Jonathan Roelofsce1db6d2017-03-14 17:29:33 +00001921 AllowedConstructsKind Allowed,
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001922 SourceLocation *TrailingElseLoc,
1923 ParsedAttributesWithRange &Attrs) {
1924 // Create temporary attribute list.
1925 ParsedAttributesWithRange TempAttrs(AttrFactory);
1926
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001927 // Get loop hints and consume annotated token.
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001928 while (Tok.is(tok::annot_pragma_loop_hint)) {
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001929 LoopHint Hint;
1930 if (!HandlePragmaLoopHint(Hint))
1931 continue;
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001932
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001933 ArgsUnion ArgHints[] = {Hint.PragmaNameLoc, Hint.OptionLoc, Hint.StateLoc,
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001934 ArgsUnion(Hint.ValueExpr)};
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001935 TempAttrs.addNew(Hint.PragmaNameLoc->Ident, Hint.Range, nullptr,
1936 Hint.PragmaNameLoc->Loc, ArgHints, 4,
1937 AttributeList::AS_Pragma);
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001938 }
1939
1940 // Get the next statement.
1941 MaybeParseCXX11Attributes(Attrs);
1942
1943 StmtResult S = ParseStatementOrDeclarationAfterAttributes(
Alexey Bataevc4fad652016-01-13 11:18:54 +00001944 Stmts, Allowed, TrailingElseLoc, Attrs);
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001945
1946 Attrs.takeAllFrom(TempAttrs);
1947 return S;
1948}
1949
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001950Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001951 assert(Tok.is(tok::l_brace));
1952 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001953
John McCallfaf5fb42010-08-26 23:41:50 +00001954 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1955 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001956
Alexey Bataev3d42f342015-11-20 07:02:57 +00001957 // Save and reset current vtordisp stack if we have entered a C++ method body.
1958 bool IsCXXMethod =
1959 getLangOpts().CPlusPlus && Decl && isa<CXXMethodDecl>(Decl);
Denis Zobnin2290dac2016-04-29 11:27:00 +00001960 Sema::PragmaStackSentinelRAII
1961 PragmaStackSentinel(Actions, "InternalPragmaState", IsCXXMethod);
Alexey Bataev3d42f342015-11-20 07:02:57 +00001962
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001963 // Do not enter a scope for the brace, as the arguments are in the same scope
1964 // (the function body) as the body itself. Instead, just read the statement
1965 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00001966 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001967
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001968 // If the function body could not be parsed, make a bogus compoundstmt.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001969 if (FnBody.isInvalid()) {
1970 Sema::CompoundScopeRAII CompoundScope(Actions);
Robert Wilhelm27b2c9a32013-08-19 20:51:20 +00001971 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001972 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001973
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001974 BodyScope.Exit();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001975 return Actions.ActOnFinishFunctionBody(Decl, FnBody.get());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001976}
Sebastian Redlb219c902008-12-21 16:41:36 +00001977
Sebastian Redla7b98a72009-04-26 20:35:05 +00001978/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1979///
1980/// function-try-block:
1981/// 'try' ctor-initializer[opt] compound-statement handler-seq
1982///
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001983Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00001984 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1985 SourceLocation TryLoc = ConsumeToken();
1986
John McCallfaf5fb42010-08-26 23:41:50 +00001987 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1988 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00001989
1990 // Constructor initializer list?
1991 if (Tok.is(tok::colon))
1992 ParseConstructorInitializer(Decl);
Douglas Gregor5ca153f2011-09-07 20:36:12 +00001993 else
1994 Actions.ActOnDefaultCtorInitializers(Decl);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001995
Alexey Bataev3d42f342015-11-20 07:02:57 +00001996 // Save and reset current vtordisp stack if we have entered a C++ method body.
1997 bool IsCXXMethod =
1998 getLangOpts().CPlusPlus && Decl && isa<CXXMethodDecl>(Decl);
Denis Zobnin2290dac2016-04-29 11:27:00 +00001999 Sema::PragmaStackSentinelRAII
2000 PragmaStackSentinel(Actions, "InternalPragmaState", IsCXXMethod);
Alexey Bataev3d42f342015-11-20 07:02:57 +00002001
Sebastian Redld98ecd62009-04-26 21:08:36 +00002002 SourceLocation LBraceLoc = Tok.getLocation();
David Blaikie1c9c9042012-11-10 01:04:23 +00002003 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc, /*FnTry*/true));
Sebastian Redla7b98a72009-04-26 20:35:05 +00002004 // If we failed to parse the try-catch, we just give the function an empty
2005 // compound statement as the body.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002006 if (FnBody.isInvalid()) {
2007 Sema::CompoundScopeRAII CompoundScope(Actions);
Robert Wilhelm27b2c9a32013-08-19 20:51:20 +00002008 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002009 }
Sebastian Redla7b98a72009-04-26 20:35:05 +00002010
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002011 BodyScope.Exit();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002012 return Actions.ActOnFinishFunctionBody(Decl, FnBody.get());
Sebastian Redla7b98a72009-04-26 20:35:05 +00002013}
2014
Erik Verbruggen6e922512012-04-12 10:11:59 +00002015bool Parser::trySkippingFunctionBody() {
Erik Verbruggen6e922512012-04-12 10:11:59 +00002016 assert(SkipFunctionBodies &&
2017 "Should only be called when SkipFunctionBodies is enabled");
Argyrios Kyrtzidis289e4a32012-10-31 17:29:28 +00002018 if (!PP.isCodeCompletionEnabled()) {
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002019 SkipFunctionBody();
Argyrios Kyrtzidis289e4a32012-10-31 17:29:28 +00002020 return true;
2021 }
2022
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002023 // We're in code-completion mode. Skip parsing for all function bodies unless
2024 // the body contains the code-completion point.
2025 TentativeParsingAction PA(*this);
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002026 bool IsTryCatch = Tok.is(tok::kw_try);
2027 CachedTokens Toks;
2028 bool ErrorInPrologue = ConsumeAndStoreFunctionPrologue(Toks);
2029 if (llvm::any_of(Toks, [](const Token &Tok) {
2030 return Tok.is(tok::code_completion);
2031 })) {
2032 PA.Revert();
2033 return false;
2034 }
2035 if (ErrorInPrologue) {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002036 PA.Commit();
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002037 SkipMalformedDecl();
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002038 return true;
2039 }
Olivier Goffartf9e890c2016-06-16 21:40:06 +00002040 if (!SkipUntil(tok::r_brace, StopAtCodeCompletion)) {
2041 PA.Revert();
2042 return false;
2043 }
2044 while (IsTryCatch && Tok.is(tok::kw_catch)) {
2045 if (!SkipUntil(tok::l_brace, StopAtCodeCompletion) ||
2046 !SkipUntil(tok::r_brace, StopAtCodeCompletion)) {
2047 PA.Revert();
2048 return false;
2049 }
2050 }
2051 PA.Commit();
2052 return true;
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002053}
2054
Sebastian Redlb219c902008-12-21 16:41:36 +00002055/// ParseCXXTryBlock - Parse a C++ try-block.
2056///
2057/// try-block:
2058/// 'try' compound-statement handler-seq
2059///
Richard Smithc202b282012-04-14 00:33:13 +00002060StmtResult Parser::ParseCXXTryBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00002061 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2062
2063 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00002064 return ParseCXXTryBlockCommon(TryLoc);
2065}
2066
2067/// ParseCXXTryBlockCommon - Parse the common part of try-block and
2068/// function-try-block.
2069///
2070/// try-block:
2071/// 'try' compound-statement handler-seq
2072///
2073/// function-try-block:
2074/// 'try' ctor-initializer[opt] compound-statement handler-seq
2075///
2076/// handler-seq:
2077/// handler handler-seq[opt]
2078///
John Wiegley1c0675e2011-04-28 01:08:34 +00002079/// [Borland] try-block:
2080/// 'try' compound-statement seh-except-block
Alp Tokerf6a24ce2013-12-05 16:25:25 +00002081/// 'try' compound-statement seh-finally-block
John Wiegley1c0675e2011-04-28 01:08:34 +00002082///
David Blaikie1c9c9042012-11-10 01:04:23 +00002083StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc, bool FnTry) {
Sebastian Redlb219c902008-12-21 16:41:36 +00002084 if (Tok.isNot(tok::l_brace))
Alp Tokerec543272013-12-24 09:48:30 +00002085 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
Richard Smithc202b282012-04-14 00:33:13 +00002086
Warren Huntf6be4cb2014-07-25 20:52:51 +00002087 StmtResult TryBlock(ParseCompoundStatement(/*isStmtExpr=*/false,
2088 Scope::DeclScope | Scope::TryScope |
2089 (FnTry ? Scope::FnTryCatchScope : 0)));
Sebastian Redlb219c902008-12-21 16:41:36 +00002090 if (TryBlock.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002091 return TryBlock;
Sebastian Redlb219c902008-12-21 16:41:36 +00002092
John Wiegley1c0675e2011-04-28 01:08:34 +00002093 // Borland allows SEH-handlers with 'try'
Chad Rosier67055f52012-07-10 21:35:27 +00002094
Richard Smithc202b282012-04-14 00:33:13 +00002095 if ((Tok.is(tok::identifier) &&
2096 Tok.getIdentifierInfo() == getSEHExceptKeyword()) ||
2097 Tok.is(tok::kw___finally)) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002098 // TODO: Factor into common return ParseSEHHandlerCommon(...)
2099 StmtResult Handler;
Douglas Gregor60060d62011-10-21 03:57:52 +00002100 if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002101 SourceLocation Loc = ConsumeToken();
2102 Handler = ParseSEHExceptBlock(Loc);
2103 }
2104 else {
2105 SourceLocation Loc = ConsumeToken();
2106 Handler = ParseSEHFinallyBlock(Loc);
2107 }
2108 if(Handler.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002109 return Handler;
John McCall53fa7142010-12-24 02:08:15 +00002110
John Wiegley1c0675e2011-04-28 01:08:34 +00002111 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
2112 TryLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002113 TryBlock.get(),
Warren Huntf6be4cb2014-07-25 20:52:51 +00002114 Handler.get());
Sebastian Redlb219c902008-12-21 16:41:36 +00002115 }
John Wiegley1c0675e2011-04-28 01:08:34 +00002116 else {
Benjamin Kramerf0623432012-08-23 22:51:59 +00002117 StmtVector Handlers;
Richard Smithc2c8bb82013-10-15 01:34:54 +00002118
2119 // C++11 attributes can't appear here, despite this context seeming
2120 // statement-like.
2121 DiagnoseAndSkipCXX11Attributes();
Sebastian Redlb219c902008-12-21 16:41:36 +00002122
John Wiegley1c0675e2011-04-28 01:08:34 +00002123 if (Tok.isNot(tok::kw_catch))
2124 return StmtError(Diag(Tok, diag::err_expected_catch));
2125 while (Tok.is(tok::kw_catch)) {
David Blaikie1c9c9042012-11-10 01:04:23 +00002126 StmtResult Handler(ParseCXXCatchBlock(FnTry));
John Wiegley1c0675e2011-04-28 01:08:34 +00002127 if (!Handler.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002128 Handlers.push_back(Handler.get());
John Wiegley1c0675e2011-04-28 01:08:34 +00002129 }
2130 // Don't bother creating the full statement if we don't have any usable
2131 // handlers.
2132 if (Handlers.empty())
2133 return StmtError();
2134
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002135 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.get(), Handlers);
John Wiegley1c0675e2011-04-28 01:08:34 +00002136 }
Sebastian Redlb219c902008-12-21 16:41:36 +00002137}
2138
2139/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
2140///
Richard Smith1dba27c2013-01-29 09:02:09 +00002141/// handler:
2142/// 'catch' '(' exception-declaration ')' compound-statement
Sebastian Redlb219c902008-12-21 16:41:36 +00002143///
Richard Smith1dba27c2013-01-29 09:02:09 +00002144/// exception-declaration:
2145/// attribute-specifier-seq[opt] type-specifier-seq declarator
2146/// attribute-specifier-seq[opt] type-specifier-seq abstract-declarator[opt]
2147/// '...'
Sebastian Redlb219c902008-12-21 16:41:36 +00002148///
David Blaikie1c9c9042012-11-10 01:04:23 +00002149StmtResult Parser::ParseCXXCatchBlock(bool FnCatch) {
Sebastian Redlb219c902008-12-21 16:41:36 +00002150 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
2151
2152 SourceLocation CatchLoc = ConsumeToken();
2153
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002154 BalancedDelimiterTracker T(*this, tok::l_paren);
Alp Toker383d2c42014-01-01 03:08:43 +00002155 if (T.expectAndConsume())
Sebastian Redlb219c902008-12-21 16:41:36 +00002156 return StmtError();
2157
2158 // C++ 3.3.2p3:
2159 // The name in a catch exception-declaration is local to the handler and
2160 // shall not be redeclared in the outermost block of the handler.
David Blaikie1c9c9042012-11-10 01:04:23 +00002161 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope |
David Blaikie3403feb2012-11-13 18:51:45 +00002162 (FnCatch ? Scope::FnTryCatchScope : 0));
Sebastian Redlb219c902008-12-21 16:41:36 +00002163
2164 // exception-declaration is equivalent to '...' or a parameter-declaration
2165 // without default arguments.
Craig Topper161e4db2014-05-21 06:02:52 +00002166 Decl *ExceptionDecl = nullptr;
Sebastian Redlb219c902008-12-21 16:41:36 +00002167 if (Tok.isNot(tok::ellipsis)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002168 ParsedAttributesWithRange Attributes(AttrFactory);
2169 MaybeParseCXX11Attributes(Attributes);
2170
John McCall084e83d2011-03-24 11:26:52 +00002171 DeclSpec DS(AttrFactory);
Richard Smith1dba27c2013-01-29 09:02:09 +00002172 DS.takeAttributesFrom(Attributes);
2173
Sebastian Redl54c04d42008-12-22 19:15:10 +00002174 if (ParseCXXTypeSpecifierSeq(DS))
2175 return StmtError();
Richard Smith1dba27c2013-01-29 09:02:09 +00002176
Sebastian Redlb219c902008-12-21 16:41:36 +00002177 Declarator ExDecl(DS, Declarator::CXXCatchContext);
2178 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002179 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00002180 } else
2181 ConsumeToken();
2182
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002183 T.consumeClose();
2184 if (T.getCloseLocation().isInvalid())
Sebastian Redlb219c902008-12-21 16:41:36 +00002185 return StmtError();
2186
2187 if (Tok.isNot(tok::l_brace))
Alp Tokerec543272013-12-24 09:48:30 +00002188 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
Sebastian Redlb219c902008-12-21 16:41:36 +00002189
Alexis Hunt96d5c762009-11-21 08:43:09 +00002190 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
Richard Smithc202b282012-04-14 00:33:13 +00002191 StmtResult Block(ParseCompoundStatement());
Sebastian Redlb219c902008-12-21 16:41:36 +00002192 if (Block.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002193 return Block;
Sebastian Redlb219c902008-12-21 16:41:36 +00002194
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002195 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.get());
Sebastian Redlb219c902008-12-21 16:41:36 +00002196}
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002197
2198void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Douglas Gregor43edb322011-10-24 22:31:10 +00002199 IfExistsCondition Result;
Francois Picheta5b3fcb2011-05-07 17:30:27 +00002200 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002201 return;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00002202
Douglas Gregor43edb322011-10-24 22:31:10 +00002203 // Handle dependent statements by parsing the braces as a compound statement.
2204 // This is not the same behavior as Visual C++, which don't treat this as a
2205 // compound statement, but for Clang's type checking we can't have anything
2206 // inside these braces escaping to the surrounding code.
2207 if (Result.Behavior == IEB_Dependent) {
2208 if (!Tok.is(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002209 Diag(Tok, diag::err_expected) << tok::l_brace;
Richard Smithc202b282012-04-14 00:33:13 +00002210 return;
Douglas Gregor43edb322011-10-24 22:31:10 +00002211 }
Richard Smithc202b282012-04-14 00:33:13 +00002212
2213 StmtResult Compound = ParseCompoundStatement();
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002214 if (Compound.isInvalid())
2215 return;
Richard Smithc202b282012-04-14 00:33:13 +00002216
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002217 StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc,
2218 Result.IsIfExists,
Richard Smithc202b282012-04-14 00:33:13 +00002219 Result.SS,
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002220 Result.Name,
2221 Compound.get());
2222 if (DepResult.isUsable())
2223 Stmts.push_back(DepResult.get());
Douglas Gregor43edb322011-10-24 22:31:10 +00002224 return;
2225 }
Richard Smithc202b282012-04-14 00:33:13 +00002226
Douglas Gregor43edb322011-10-24 22:31:10 +00002227 BalancedDelimiterTracker Braces(*this, tok::l_brace);
2228 if (Braces.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +00002229 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002230 return;
2231 }
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002232
Douglas Gregor43edb322011-10-24 22:31:10 +00002233 switch (Result.Behavior) {
2234 case IEB_Parse:
2235 // Parse the statements below.
2236 break;
Chad Rosier67055f52012-07-10 21:35:27 +00002237
Douglas Gregor43edb322011-10-24 22:31:10 +00002238 case IEB_Dependent:
2239 llvm_unreachable("Dependent case handled above");
Chad Rosier67055f52012-07-10 21:35:27 +00002240
Douglas Gregor43edb322011-10-24 22:31:10 +00002241 case IEB_Skip:
2242 Braces.skipToEnd();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002243 return;
2244 }
2245
2246 // Condition is true, parse the statements.
2247 while (Tok.isNot(tok::r_brace)) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00002248 StmtResult R = ParseStatementOrDeclaration(Stmts, ACK_Any);
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002249 if (R.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002250 Stmts.push_back(R.get());
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002251 }
Douglas Gregor43edb322011-10-24 22:31:10 +00002252 Braces.consumeClose();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002253}
Anastasia Stulova6bdbcbb2016-02-19 18:30:11 +00002254
2255bool Parser::ParseOpenCLUnrollHintAttribute(ParsedAttributes &Attrs) {
2256 MaybeParseGNUAttributes(Attrs);
2257
2258 if (Attrs.empty())
2259 return true;
2260
2261 if (Attrs.getList()->getKind() != AttributeList::AT_OpenCLUnrollHint)
2262 return true;
2263
2264 if (!(Tok.is(tok::kw_for) || Tok.is(tok::kw_while) || Tok.is(tok::kw_do))) {
2265 Diag(Tok, diag::err_opencl_unroll_hint_on_non_loop);
2266 return false;
2267 }
2268 return true;
2269}