blob: c849554238e3a0457b5c94ed16ea372b5ba4862a [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
15#include "clang/Parse/Parser.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000016#include "RAIIObjectsForParser.h"
John McCallf413f5e2013-05-03 00:10:13 +000017#include "clang/AST/ASTContext.h"
Aaron Ballmanb06b15a2014-06-06 12:40:24 +000018#include "clang/Basic/Attributes.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/Basic/Diagnostic.h"
20#include "clang/Basic/PrettyStackTrace.h"
John McCall8b0666c2010-08-20 18:27:03 +000021#include "clang/Sema/DeclSpec.h"
Aaron Ballmanb06b15a2014-06-06 12:40:24 +000022#include "clang/Sema/LoopHint.h"
John McCallfaf5fb42010-08-26 23:41:50 +000023#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall8b0666c2010-08-20 18:27:03 +000024#include "clang/Sema/Scope.h"
Richard Smith4f605af2012-08-18 00:55:03 +000025#include "clang/Sema/TypoCorrection.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000026#include "llvm/ADT/SmallString.h"
Chris Lattner0ccd51e2006-08-09 05:47:47 +000027using namespace clang;
28
29//===----------------------------------------------------------------------===//
30// C99 6.8: Statements and Blocks.
31//===----------------------------------------------------------------------===//
32
Richard Smith426a47b2013-10-28 22:04:30 +000033/// \brief Parse a standalone statement (for instance, as the body of an 'if',
34/// 'while', or 'for').
Alexey Bataevc4fad652016-01-13 11:18:54 +000035StmtResult Parser::ParseStatement(SourceLocation *TrailingElseLoc,
36 bool AllowOpenMPStandalone) {
Richard Smith426a47b2013-10-28 22:04:30 +000037 StmtResult Res;
38
39 // We may get back a null statement if we found a #pragma. Keep going until
40 // we get an actual statement.
41 do {
42 StmtVector Stmts;
Alexey Bataevc4fad652016-01-13 11:18:54 +000043 Res = ParseStatementOrDeclaration(
44 Stmts, AllowOpenMPStandalone ? ACK_StatementsOpenMPAnyExecutable
45 : ACK_StatementsOpenMPNonStandalone,
46 TrailingElseLoc);
Richard Smith426a47b2013-10-28 22:04:30 +000047 } while (!Res.isInvalid() && !Res.get());
48
49 return Res;
50}
51
Chris Lattner0ccd51e2006-08-09 05:47:47 +000052/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
53/// StatementOrDeclaration:
54/// statement
55/// declaration
56///
57/// statement:
58/// labeled-statement
59/// compound-statement
60/// expression-statement
61/// selection-statement
62/// iteration-statement
63/// jump-statement
Argyrios Kyrtzidisdee82912008-09-07 18:58:01 +000064/// [C++] declaration-statement
Sebastian Redlb219c902008-12-21 16:41:36 +000065/// [C++] try-block
John Wiegley1c0675e2011-04-28 01:08:34 +000066/// [MS] seh-try-block
Fariborz Jahanian90814572007-10-04 20:19:06 +000067/// [OBC] objc-throw-statement
68/// [OBC] objc-try-catch-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +000069/// [OBC] objc-synchronized-statement
Chris Lattner0116c472006-08-15 06:03:28 +000070/// [GNU] asm-statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +000071/// [OMP] openmp-construct [TODO]
72///
73/// labeled-statement:
74/// identifier ':' statement
75/// 'case' constant-expression ':' statement
76/// 'default' ':' statement
77///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000078/// selection-statement:
79/// if-statement
80/// switch-statement
81///
82/// iteration-statement:
83/// while-statement
84/// do-statement
85/// for-statement
86///
Chris Lattner9075bd72006-08-10 04:59:57 +000087/// expression-statement:
88/// expression[opt] ';'
89///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000090/// jump-statement:
91/// 'goto' identifier ';'
92/// 'continue' ';'
93/// 'break' ';'
94/// 'return' expression[opt] ';'
Chris Lattner503fadc2006-08-10 05:45:44 +000095/// [GNU] 'goto' '*' expression ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000096///
Fariborz Jahanian90814572007-10-04 20:19:06 +000097/// [OBC] objc-throw-statement:
98/// [OBC] '@' 'throw' expression ';'
Mike Stump11289f42009-09-09 15:08:12 +000099/// [OBC] '@' 'throw' ';'
100///
John McCalldadc5752010-08-24 06:29:42 +0000101StmtResult
Alexey Bataevc4fad652016-01-13 11:18:54 +0000102Parser::ParseStatementOrDeclaration(StmtVector &Stmts,
103 AllowedContsructsKind Allowed,
Nico Weber3cef1082011-12-22 23:26:17 +0000104 SourceLocation *TrailingElseLoc) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000105
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000106 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000107
Richard Smithc202b282012-04-14 00:33:13 +0000108 ParsedAttributesWithRange Attrs(AttrFactory);
Craig Topper161e4db2014-05-21 06:02:52 +0000109 MaybeParseCXX11Attributes(Attrs, nullptr, /*MightBeObjCMessageSend*/ true);
Anastasia Stulova6bdbcbb2016-02-19 18:30:11 +0000110 if (!MaybeParseOpenCLUnrollHintAttribute(Attrs))
111 return StmtError();
Richard Smithc202b282012-04-14 00:33:13 +0000112
Alexey Bataevc4fad652016-01-13 11:18:54 +0000113 StmtResult Res = ParseStatementOrDeclarationAfterAttributes(
114 Stmts, Allowed, TrailingElseLoc, Attrs);
Richard Smithc202b282012-04-14 00:33:13 +0000115
116 assert((Attrs.empty() || Res.isInvalid() || Res.isUsable()) &&
117 "attributes on empty statement");
118
119 if (Attrs.empty() || Res.isInvalid())
120 return Res;
121
122 return Actions.ProcessStmtAttributes(Res.get(), Attrs.getList(), Attrs.Range);
123}
124
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000125namespace {
126class StatementFilterCCC : public CorrectionCandidateCallback {
127public:
128 StatementFilterCCC(Token nextTok) : NextToken(nextTok) {
Daniel Marjamakie59f8d72015-06-18 10:59:26 +0000129 WantTypeSpecifiers = nextTok.isOneOf(tok::l_paren, tok::less, tok::l_square,
130 tok::identifier, tok::star, tok::amp);
131 WantExpressionKeywords =
132 nextTok.isOneOf(tok::l_paren, tok::identifier, tok::arrow, tok::period);
133 WantRemainingKeywords =
134 nextTok.isOneOf(tok::l_paren, tok::semi, tok::identifier, tok::l_brace);
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000135 WantCXXNamedCasts = false;
136 }
137
Craig Topper2b07f022014-03-12 05:09:18 +0000138 bool ValidateCandidate(const TypoCorrection &candidate) override {
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000139 if (FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>())
Kaelyn Uhrain07e62722013-10-01 22:00:28 +0000140 return !candidate.getCorrectionSpecifier() || isa<ObjCIvarDecl>(FD);
Kaelyn Uhrain46b6cdc2013-09-27 19:40:16 +0000141 if (NextToken.is(tok::equal))
142 return candidate.getCorrectionDeclAs<VarDecl>();
Kaelyn Uhrain30943ce2013-09-27 23:54:23 +0000143 if (NextToken.is(tok::period) &&
144 candidate.getCorrectionDeclAs<NamespaceDecl>())
145 return false;
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000146 return CorrectionCandidateCallback::ValidateCandidate(candidate);
147 }
148
149private:
150 Token NextToken;
151};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000152}
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000153
Richard Smithc202b282012-04-14 00:33:13 +0000154StmtResult
155Parser::ParseStatementOrDeclarationAfterAttributes(StmtVector &Stmts,
Alexey Bataevc4fad652016-01-13 11:18:54 +0000156 AllowedContsructsKind Allowed, SourceLocation *TrailingElseLoc,
Richard Smithc202b282012-04-14 00:33:13 +0000157 ParsedAttributesWithRange &Attrs) {
Craig Topper161e4db2014-05-21 06:02:52 +0000158 const char *SemiError = nullptr;
Richard Smithc202b282012-04-14 00:33:13 +0000159 StmtResult Res;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000160
Chris Lattner503fadc2006-08-10 05:45:44 +0000161 // Cases in this switch statement should fall through if the parser expects
162 // the token to end in a semicolon (in which case SemiError should be set),
163 // or they directly 'return;' if not.
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000164Retry:
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000165 tok::TokenKind Kind = Tok.getKind();
166 SourceLocation AtLoc;
167 switch (Kind) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000168 case tok::at: // May be a @try or @throw statement
169 {
Richard Smithc202b282012-04-14 00:33:13 +0000170 ProhibitAttributes(Attrs); // TODO: is it correct?
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000171 AtLoc = ConsumeToken(); // consume @
Sebastian Redlbab9a4b2008-12-11 20:12:42 +0000172 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000173 }
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000174
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000175 case tok::code_completion:
John McCallfaf5fb42010-08-26 23:41:50 +0000176 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000177 cutOffParsing();
178 return StmtError();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000179
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000180 case tok::identifier: {
181 Token Next = NextToken();
182 if (Next.is(tok::colon)) { // C99 6.8.1: labeled-statement
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000183 // identifier ':' statement
Richard Smithc202b282012-04-14 00:33:13 +0000184 return ParseLabeledStatement(Attrs);
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000185 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000186
Richard Smith4f605af2012-08-18 00:55:03 +0000187 // Look up the identifier, and typo-correct it to a keyword if it's not
188 // found.
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000189 if (Next.isNot(tok::coloncolon)) {
Richard Smith4f605af2012-08-18 00:55:03 +0000190 // Try to limit which sets of keywords should be included in typo
191 // correction based on what the next token is.
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000192 if (TryAnnotateName(/*IsAddressOfOperand*/ false,
193 llvm::make_unique<StatementFilterCCC>(Next)) ==
194 ANK_Error) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000195 // Handle errors here by skipping up to the next semicolon or '}', and
196 // eat the semicolon if that's what stopped us.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000197 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000198 if (Tok.is(tok::semi))
199 ConsumeToken();
200 return StmtError();
Richard Smith4f605af2012-08-18 00:55:03 +0000201 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000202
Richard Smith4f605af2012-08-18 00:55:03 +0000203 // If the identifier was typo-corrected, try again.
204 if (Tok.isNot(tok::identifier))
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000205 goto Retry;
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000206 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000207
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000208 // Fall through
209 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000210
Chris Lattner803802d2009-03-24 17:04:48 +0000211 default: {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000212 if ((getLangOpts().CPlusPlus || Allowed == ACK_Any) &&
213 isDeclarationStatement()) {
Chris Lattner49836b42009-04-02 04:16:50 +0000214 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Rafael Espindola1bd906d2014-10-22 14:27:08 +0000215 DeclGroupPtrTy Decl = ParseDeclaration(Declarator::BlockContext,
Richard Smithc202b282012-04-14 00:33:13 +0000216 DeclEnd, Attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000217 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattner803802d2009-03-24 17:04:48 +0000218 }
219
220 if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +0000221 Diag(Tok, diag::err_expected_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000222 return StmtError();
Chris Lattnerf8afb622006-08-10 18:26:31 +0000223 }
Mike Stump11289f42009-09-09 15:08:12 +0000224
Richard Smithc202b282012-04-14 00:33:13 +0000225 return ParseExprStatement();
Chris Lattner803802d2009-03-24 17:04:48 +0000226 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000227
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000228 case tok::kw_case: // C99 6.8.1: labeled-statement
Richard Smithc202b282012-04-14 00:33:13 +0000229 return ParseCaseStatement();
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000230 case tok::kw_default: // C99 6.8.1: labeled-statement
Richard Smithc202b282012-04-14 00:33:13 +0000231 return ParseDefaultStatement();
Sebastian Redl042ad952008-12-11 19:30:53 +0000232
Chris Lattner9075bd72006-08-10 04:59:57 +0000233 case tok::l_brace: // C99 6.8.2: compound-statement
Richard Smithc202b282012-04-14 00:33:13 +0000234 return ParseCompoundStatement();
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000235 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
Argyrios Kyrtzidis43ea78b2011-09-01 21:53:45 +0000236 bool HasLeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
237 return Actions.ActOnNullStmt(ConsumeToken(), HasLeadingEmptyMacro);
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000238 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000239
Chris Lattner9075bd72006-08-10 04:59:57 +0000240 case tok::kw_if: // C99 6.8.4.1: if-statement
Richard Smithc202b282012-04-14 00:33:13 +0000241 return ParseIfStatement(TrailingElseLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000242 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Richard Smithc202b282012-04-14 00:33:13 +0000243 return ParseSwitchStatement(TrailingElseLoc);
Sebastian Redl042ad952008-12-11 19:30:53 +0000244
Chris Lattner9075bd72006-08-10 04:59:57 +0000245 case tok::kw_while: // C99 6.8.5.1: while-statement
Richard Smithc202b282012-04-14 00:33:13 +0000246 return ParseWhileStatement(TrailingElseLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000247 case tok::kw_do: // C99 6.8.5.2: do-statement
Richard Smithc202b282012-04-14 00:33:13 +0000248 Res = ParseDoStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000249 SemiError = "do/while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000250 break;
251 case tok::kw_for: // C99 6.8.5.3: for-statement
Richard Smithc202b282012-04-14 00:33:13 +0000252 return ParseForStatement(TrailingElseLoc);
Chris Lattner503fadc2006-08-10 05:45:44 +0000253
254 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Richard Smithc202b282012-04-14 00:33:13 +0000255 Res = ParseGotoStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000256 SemiError = "goto";
Chris Lattner9075bd72006-08-10 04:59:57 +0000257 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000258 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Richard Smithc202b282012-04-14 00:33:13 +0000259 Res = ParseContinueStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000260 SemiError = "continue";
Chris Lattner503fadc2006-08-10 05:45:44 +0000261 break;
262 case tok::kw_break: // C99 6.8.6.3: break-statement
Richard Smithc202b282012-04-14 00:33:13 +0000263 Res = ParseBreakStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000264 SemiError = "break";
Chris Lattner503fadc2006-08-10 05:45:44 +0000265 break;
266 case tok::kw_return: // C99 6.8.6.4: return-statement
Richard Smithc202b282012-04-14 00:33:13 +0000267 Res = ParseReturnStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000268 SemiError = "return";
Chris Lattner503fadc2006-08-10 05:45:44 +0000269 break;
Richard Smith0e304ea2015-10-22 04:46:14 +0000270 case tok::kw_co_return: // C++ Coroutines: co_return statement
271 Res = ParseReturnStatement();
272 SemiError = "co_return";
273 break;
Sebastian Redl042ad952008-12-11 19:30:53 +0000274
Sebastian Redlb219c902008-12-21 16:41:36 +0000275 case tok::kw_asm: {
Richard Smithc202b282012-04-14 00:33:13 +0000276 ProhibitAttributes(Attrs);
Steve Naroffb2c80c72008-02-07 03:50:06 +0000277 bool msAsm = false;
278 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidis3050d9b2010-11-02 02:33:08 +0000279 Res = Actions.ActOnFinishFullStmt(Res.get());
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000280 if (msAsm) return Res;
Chris Lattner34a95662009-06-14 00:07:48 +0000281 SemiError = "asm";
Chris Lattner0116c472006-08-15 06:03:28 +0000282 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000283 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000284
Reid Kleckner6d8d22a2014-06-25 00:28:35 +0000285 case tok::kw___if_exists:
286 case tok::kw___if_not_exists:
287 ProhibitAttributes(Attrs);
288 ParseMicrosoftIfExistsStatement(Stmts);
289 // An __if_exists block is like a compound statement, but it doesn't create
290 // a new scope.
291 return StmtEmpty();
292
Sebastian Redlb219c902008-12-21 16:41:36 +0000293 case tok::kw_try: // C++ 15: try-block
Richard Smithc202b282012-04-14 00:33:13 +0000294 return ParseCXXTryBlock();
John Wiegley1c0675e2011-04-28 01:08:34 +0000295
296 case tok::kw___try:
Richard Smithc202b282012-04-14 00:33:13 +0000297 ProhibitAttributes(Attrs); // TODO: is it correct?
298 return ParseSEHTryBlock();
Eli Friedmanec52f922012-02-23 23:47:16 +0000299
Nico Weberc7d05962014-07-06 22:32:59 +0000300 case tok::kw___leave:
301 Res = ParseSEHLeaveStatement();
302 SemiError = "__leave";
303 break;
304
Eli Friedmanec52f922012-02-23 23:47:16 +0000305 case tok::annot_pragma_vis:
Richard Smithc202b282012-04-14 00:33:13 +0000306 ProhibitAttributes(Attrs);
Eli Friedmanec52f922012-02-23 23:47:16 +0000307 HandlePragmaVisibility();
308 return StmtEmpty();
309
310 case tok::annot_pragma_pack:
Richard Smithc202b282012-04-14 00:33:13 +0000311 ProhibitAttributes(Attrs);
Eli Friedmanec52f922012-02-23 23:47:16 +0000312 HandlePragmaPack();
313 return StmtEmpty();
Eli Friedman68be1642012-10-04 02:36:51 +0000314
Eli Friedmanbbbbac62012-10-09 22:46:54 +0000315 case tok::annot_pragma_msstruct:
316 ProhibitAttributes(Attrs);
317 HandlePragmaMSStruct();
318 return StmtEmpty();
319
Eli Friedmanae8ee252012-10-08 23:52:38 +0000320 case tok::annot_pragma_align:
321 ProhibitAttributes(Attrs);
322 HandlePragmaAlign();
323 return StmtEmpty();
324
Eli Friedmanbbbbac62012-10-09 22:46:54 +0000325 case tok::annot_pragma_weak:
326 ProhibitAttributes(Attrs);
327 HandlePragmaWeak();
328 return StmtEmpty();
329
330 case tok::annot_pragma_weakalias:
331 ProhibitAttributes(Attrs);
332 HandlePragmaWeakAlias();
333 return StmtEmpty();
334
335 case tok::annot_pragma_redefine_extname:
336 ProhibitAttributes(Attrs);
337 HandlePragmaRedefineExtname();
338 return StmtEmpty();
339
Eli Friedman68be1642012-10-04 02:36:51 +0000340 case tok::annot_pragma_fp_contract:
Richard Smithca9b0b62013-11-15 21:10:54 +0000341 ProhibitAttributes(Attrs);
Lang Hamesa930e712012-10-21 01:10:01 +0000342 Diag(Tok, diag::err_pragma_fp_contract_scope);
343 ConsumeToken();
344 return StmtError();
345
Eli Friedman68be1642012-10-04 02:36:51 +0000346 case tok::annot_pragma_opencl_extension:
347 ProhibitAttributes(Attrs);
348 HandlePragmaOpenCLExtension();
349 return StmtEmpty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000350
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000351 case tok::annot_pragma_captured:
Richard Smith12a41bd2013-09-16 21:17:44 +0000352 ProhibitAttributes(Attrs);
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000353 return HandlePragmaCaptured();
354
Alexey Bataeva769e072013-03-22 06:34:35 +0000355 case tok::annot_pragma_openmp:
Richard Smith12a41bd2013-09-16 21:17:44 +0000356 ProhibitAttributes(Attrs);
Alexey Bataevc4fad652016-01-13 11:18:54 +0000357 return ParseOpenMPDeclarativeOrExecutableDirective(Allowed);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000358
David Majnemer4bb09802014-02-10 19:50:15 +0000359 case tok::annot_pragma_ms_pointers_to_members:
360 ProhibitAttributes(Attrs);
361 HandlePragmaMSPointersToMembers();
362 return StmtEmpty();
363
Warren Huntc3b18962014-04-08 22:30:47 +0000364 case tok::annot_pragma_ms_pragma:
365 ProhibitAttributes(Attrs);
366 HandlePragmaMSPragma();
367 return StmtEmpty();
Aaron Ballmanb06b15a2014-06-06 12:40:24 +0000368
Alexey Bataev3d42f342015-11-20 07:02:57 +0000369 case tok::annot_pragma_ms_vtordisp:
370 ProhibitAttributes(Attrs);
371 HandlePragmaMSVtorDisp();
372 return StmtEmpty();
373
Aaron Ballmanb06b15a2014-06-06 12:40:24 +0000374 case tok::annot_pragma_loop_hint:
375 ProhibitAttributes(Attrs);
Alexey Bataevc4fad652016-01-13 11:18:54 +0000376 return ParsePragmaLoopHint(Stmts, Allowed, TrailingElseLoc, Attrs);
Richard Smithba3a4f92016-01-12 21:59:26 +0000377
378 case tok::annot_pragma_dump:
379 HandlePragmaDump();
380 return StmtEmpty();
Sebastian Redlb219c902008-12-21 16:41:36 +0000381 }
382
Chris Lattner503fadc2006-08-10 05:45:44 +0000383 // If we reached this code, the statement must end in a semicolon.
Alp Toker97650562014-01-10 11:19:30 +0000384 if (!TryConsumeToken(tok::semi) && !Res.isInvalid()) {
Chris Lattner8e3eed02009-06-14 00:23:56 +0000385 // If the result was valid, then we do want to diagnose this. Use
386 // ExpectAndConsume to emit the diagnostic, even though we know it won't
387 // succeed.
388 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner0046de12008-11-13 18:52:53 +0000389 // Skip until we see a } or ;, but don't eat it.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000390 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner503fadc2006-08-10 05:45:44 +0000391 }
Mike Stump11289f42009-09-09 15:08:12 +0000392
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000393 return Res;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000394}
395
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000396/// \brief Parse an expression statement.
Richard Smithc202b282012-04-14 00:33:13 +0000397StmtResult Parser::ParseExprStatement() {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000398 // If a case keyword is missing, this is where it should be inserted.
399 Token OldToken = Tok;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000400
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000401 // expression[opt] ';'
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000402 ExprResult Expr(ParseExpression());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000403 if (Expr.isInvalid()) {
404 // If the expression is invalid, skip ahead to the next semicolon or '}'.
405 // Not doing this opens us up to the possibility of infinite loops if
406 // ParseExpression does not consume any tokens.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000407 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000408 if (Tok.is(tok::semi))
409 ConsumeToken();
John McCalleaef89b2013-03-22 02:10:40 +0000410 return Actions.ActOnExprStmtError();
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000411 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000412
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000413 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
414 Actions.CheckCaseExpression(Expr.get())) {
415 // If a constant expression is followed by a colon inside a switch block,
416 // suggest a missing case keyword.
417 Diag(OldToken, diag::err_expected_case_before_expression)
418 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000419
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000420 // Recover parsing as a case statement.
Richard Smithc202b282012-04-14 00:33:13 +0000421 return ParseCaseStatement(/*MissingCase=*/true, Expr);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000422 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000423
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000424 // Otherwise, eat the semicolon.
425 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +0000426 return Actions.ActOnExprStmt(Expr);
John Wiegley1c0675e2011-04-28 01:08:34 +0000427}
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000428
John Wiegley1c0675e2011-04-28 01:08:34 +0000429/// ParseSEHTryBlockCommon
430///
431/// seh-try-block:
432/// '__try' compound-statement seh-handler
433///
434/// seh-handler:
435/// seh-except-block
436/// seh-finally-block
437///
Nico Weberdd256742015-02-25 01:43:27 +0000438StmtResult Parser::ParseSEHTryBlock() {
439 assert(Tok.is(tok::kw___try) && "Expected '__try'");
440 SourceLocation TryLoc = ConsumeToken();
441
Nico Weberfc3fe4f2015-02-25 02:22:06 +0000442 if (Tok.isNot(tok::l_brace))
Alp Tokerec543272013-12-24 09:48:30 +0000443 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
John Wiegley1c0675e2011-04-28 01:08:34 +0000444
Warren Huntf6be4cb2014-07-25 20:52:51 +0000445 StmtResult TryBlock(ParseCompoundStatement(/*isStmtExpr=*/false,
446 Scope::DeclScope | Scope::SEHTryScope));
John Wiegley1c0675e2011-04-28 01:08:34 +0000447 if(TryBlock.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000448 return TryBlock;
John Wiegley1c0675e2011-04-28 01:08:34 +0000449
450 StmtResult Handler;
Richard Smithc202b282012-04-14 00:33:13 +0000451 if (Tok.is(tok::identifier) &&
Douglas Gregor60060d62011-10-21 03:57:52 +0000452 Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +0000453 SourceLocation Loc = ConsumeToken();
454 Handler = ParseSEHExceptBlock(Loc);
455 } else if (Tok.is(tok::kw___finally)) {
456 SourceLocation Loc = ConsumeToken();
457 Handler = ParseSEHFinallyBlock(Loc);
458 } else {
Nico Weberfc3fe4f2015-02-25 02:22:06 +0000459 return StmtError(Diag(Tok, diag::err_seh_expected_handler));
John Wiegley1c0675e2011-04-28 01:08:34 +0000460 }
461
462 if(Handler.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000463 return Handler;
John Wiegley1c0675e2011-04-28 01:08:34 +0000464
465 return Actions.ActOnSEHTryBlock(false /* IsCXXTry */,
466 TryLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000467 TryBlock.get(),
Warren Huntf6be4cb2014-07-25 20:52:51 +0000468 Handler.get());
John Wiegley1c0675e2011-04-28 01:08:34 +0000469}
470
471/// ParseSEHExceptBlock - Handle __except
472///
473/// seh-except-block:
474/// '__except' '(' seh-filter-expression ')' compound-statement
475///
476StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) {
477 PoisonIdentifierRAIIObject raii(Ident__exception_code, false),
478 raii2(Ident___exception_code, false),
479 raii3(Ident_GetExceptionCode, false);
480
Alp Toker383d2c42014-01-01 03:08:43 +0000481 if (ExpectAndConsume(tok::l_paren))
John Wiegley1c0675e2011-04-28 01:08:34 +0000482 return StmtError();
483
Reid Kleckner1d59f992015-01-22 01:36:17 +0000484 ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope |
485 Scope::SEHExceptScope);
John Wiegley1c0675e2011-04-28 01:08:34 +0000486
David Blaikiebbafb8a2012-03-11 07:00:24 +0000487 if (getLangOpts().Borland) {
Francois Pichetbfaf4772011-04-28 03:14:31 +0000488 Ident__exception_info->setIsPoisoned(false);
489 Ident___exception_info->setIsPoisoned(false);
490 Ident_GetExceptionInfo->setIsPoisoned(false);
491 }
Reid Kleckner1d59f992015-01-22 01:36:17 +0000492
493 ExprResult FilterExpr;
494 {
495 ParseScopeFlags FilterScope(this, getCurScope()->getFlags() |
496 Scope::SEHFilterScope);
Reid Kleckner85368fb2015-04-02 22:09:32 +0000497 FilterExpr = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Reid Kleckner1d59f992015-01-22 01:36:17 +0000498 }
Francois Pichetbfaf4772011-04-28 03:14:31 +0000499
David Blaikiebbafb8a2012-03-11 07:00:24 +0000500 if (getLangOpts().Borland) {
Francois Pichetbfaf4772011-04-28 03:14:31 +0000501 Ident__exception_info->setIsPoisoned(true);
502 Ident___exception_info->setIsPoisoned(true);
503 Ident_GetExceptionInfo->setIsPoisoned(true);
504 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000505
506 if(FilterExpr.isInvalid())
507 return StmtError();
508
Alp Toker383d2c42014-01-01 03:08:43 +0000509 if (ExpectAndConsume(tok::r_paren))
John Wiegley1c0675e2011-04-28 01:08:34 +0000510 return StmtError();
511
Nico Weberfc3fe4f2015-02-25 02:22:06 +0000512 if (Tok.isNot(tok::l_brace))
513 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
514
Richard Smithc202b282012-04-14 00:33:13 +0000515 StmtResult Block(ParseCompoundStatement());
John Wiegley1c0675e2011-04-28 01:08:34 +0000516
517 if(Block.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000518 return Block;
John Wiegley1c0675e2011-04-28 01:08:34 +0000519
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000520 return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.get(), Block.get());
John Wiegley1c0675e2011-04-28 01:08:34 +0000521}
522
523/// ParseSEHFinallyBlock - Handle __finally
524///
525/// seh-finally-block:
526/// '__finally' compound-statement
527///
Nico Weberd64657f2015-03-09 02:47:59 +0000528StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyLoc) {
John Wiegley1c0675e2011-04-28 01:08:34 +0000529 PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
530 raii2(Ident___abnormal_termination, false),
531 raii3(Ident_AbnormalTermination, false);
532
Nico Weberfc3fe4f2015-02-25 02:22:06 +0000533 if (Tok.isNot(tok::l_brace))
534 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
535
Nico Weberd64657f2015-03-09 02:47:59 +0000536 ParseScope FinallyScope(this, 0);
537 Actions.ActOnStartSEHFinallyBlock();
538
Richard Smithc202b282012-04-14 00:33:13 +0000539 StmtResult Block(ParseCompoundStatement());
Nico Weberce903292015-03-09 03:17:15 +0000540 if(Block.isInvalid()) {
541 Actions.ActOnAbortSEHFinallyBlock();
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000542 return Block;
Nico Weberce903292015-03-09 03:17:15 +0000543 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000544
Nico Weberd64657f2015-03-09 02:47:59 +0000545 return Actions.ActOnFinishSEHFinallyBlock(FinallyLoc, Block.get());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000546}
547
Nico Weberc7d05962014-07-06 22:32:59 +0000548/// Handle __leave
549///
550/// seh-leave-statement:
551/// '__leave' ';'
552///
553StmtResult Parser::ParseSEHLeaveStatement() {
554 SourceLocation LeaveLoc = ConsumeToken(); // eat the '__leave'.
555 return Actions.ActOnSEHLeaveStmt(LeaveLoc, getCurScope());
556}
557
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000558/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000559///
560/// labeled-statement:
561/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000562/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000563///
Richard Smithc202b282012-04-14 00:33:13 +0000564StmtResult Parser::ParseLabeledStatement(ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000565 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
566 "Not an identifier!");
567
568 Token IdentTok = Tok; // Save the whole token.
569 ConsumeToken(); // eat the identifier.
570
571 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000572
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000573 // identifier ':' statement
574 SourceLocation ColonLoc = ConsumeToken();
575
Richard Smitha3e01cf2013-11-15 22:45:29 +0000576 // Read label attributes, if present.
577 StmtResult SubStmt;
578 if (Tok.is(tok::kw___attribute)) {
579 ParsedAttributesWithRange TempAttrs(AttrFactory);
580 ParseGNUAttributes(TempAttrs);
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000581
Richard Smitha3e01cf2013-11-15 22:45:29 +0000582 // In C++, GNU attributes only apply to the label if they are followed by a
583 // semicolon, to disambiguate label attributes from attributes on a labeled
584 // declaration.
585 //
586 // This doesn't quite match what GCC does; if the attribute list is empty
587 // and followed by a semicolon, GCC will reject (it appears to parse the
588 // attributes as part of a statement in that case). That looks like a bug.
589 if (!getLangOpts().CPlusPlus || Tok.is(tok::semi))
590 attrs.takeAllFrom(TempAttrs);
591 else if (isDeclarationStatement()) {
592 StmtVector Stmts;
593 // FIXME: We should do this whether or not we have a declaration
594 // statement, but that doesn't work correctly (because ProhibitAttributes
595 // can't handle GNU attributes), so only call it in the one case where
596 // GNU attributes are allowed.
597 SubStmt = ParseStatementOrDeclarationAfterAttributes(
Alexey Bataevc4fad652016-01-13 11:18:54 +0000598 Stmts, /*Allowed=*/ACK_StatementsOpenMPNonStandalone, nullptr,
599 TempAttrs);
Richard Smitha3e01cf2013-11-15 22:45:29 +0000600 if (!TempAttrs.empty() && !SubStmt.isInvalid())
601 SubStmt = Actions.ProcessStmtAttributes(
602 SubStmt.get(), TempAttrs.getList(), TempAttrs.Range);
603 } else {
Alp Toker383d2c42014-01-01 03:08:43 +0000604 Diag(Tok, diag::err_expected_after) << "__attribute__" << tok::semi;
Richard Smitha3e01cf2013-11-15 22:45:29 +0000605 }
606 }
607
608 // If we've not parsed a statement yet, parse one now.
609 if (!SubStmt.isInvalid() && !SubStmt.isUsable())
610 SubStmt = ParseStatement();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000611
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000612 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000613 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000614 SubStmt = Actions.ActOnNullStmt(ColonLoc);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000615
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000616 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
617 IdentTok.getLocation());
Richard Smithc202b282012-04-14 00:33:13 +0000618 if (AttributeList *Attrs = attrs.getList()) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000619 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
Richard Smithc202b282012-04-14 00:33:13 +0000620 attrs.clear();
621 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000622
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000623 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
624 SubStmt.get());
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000625}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000626
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000627/// ParseCaseStatement
628/// labeled-statement:
629/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000630/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000631///
Richard Smithc202b282012-04-14 00:33:13 +0000632StmtResult Parser::ParseCaseStatement(bool MissingCase, ExprResult Expr) {
Richard Smithd4257d82011-04-21 22:48:40 +0000633 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Mike Stump11289f42009-09-09 15:08:12 +0000634
Chris Lattner34a22092009-03-04 04:23:07 +0000635 // It is very very common for code to contain many case statements recursively
636 // nested, as in (but usually without indentation):
637 // case 1:
638 // case 2:
639 // case 3:
640 // case 4:
641 // case 5: etc.
642 //
643 // Parsing this naively works, but is both inefficient and can cause us to run
644 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner2b19a6582009-03-04 18:24:58 +0000645 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner34a22092009-03-04 04:23:07 +0000646 // but all the grossness is constrained to ParseCaseStatement (and some
Richard Smitha3e01cf2013-11-15 22:45:29 +0000647 // weirdness in the actions), so this is just local grossness :).
Mike Stump11289f42009-09-09 15:08:12 +0000648
Chris Lattner34a22092009-03-04 04:23:07 +0000649 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
650 // example above.
John McCalldadc5752010-08-24 06:29:42 +0000651 StmtResult TopLevelCase(true);
Mike Stump11289f42009-09-09 15:08:12 +0000652
Chris Lattner34a22092009-03-04 04:23:07 +0000653 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
654 // gets updated each time a new case is parsed, and whose body is unset so
655 // far. When parsing 'case 4', this is the 'case 3' node.
Craig Topper161e4db2014-05-21 06:02:52 +0000656 Stmt *DeepestParsedCaseStmt = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000657
Chris Lattner34a22092009-03-04 04:23:07 +0000658 // While we have case statements, eat and stack them.
David Majnemer0ac67fa2011-06-13 05:50:12 +0000659 SourceLocation ColonLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000660 do {
Richard Trieu2c850c02011-04-21 21:44:26 +0000661 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
662 ConsumeToken(); // eat the 'case'.
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000663 ColonLoc = SourceLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000664
Douglas Gregord328d572009-09-21 18:10:23 +0000665 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000666 Actions.CodeCompleteCase(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000667 cutOffParsing();
668 return StmtError();
Douglas Gregord328d572009-09-21 18:10:23 +0000669 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000670
Chris Lattner125c0ee2009-12-10 00:38:54 +0000671 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
672 /// Disable this form of error recovery while we're parsing the case
673 /// expression.
674 ColonProtectionRAIIObject ColonProtection(*this);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000675
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000676 ExprResult LHS;
677 if (!MissingCase) {
678 LHS = ParseConstantExpression();
Kaelyn Takatab16e6322014-11-20 22:06:40 +0000679 if (!getLangOpts().CPlusPlus11) {
680 LHS = Actions.CorrectDelayedTyposInExpr(LHS, [this](class Expr *E) {
681 return Actions.VerifyIntegerConstantExpression(E);
682 });
683 }
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000684 if (LHS.isInvalid()) {
685 // If constant-expression is parsed unsuccessfully, recover by skipping
686 // current case statement (moving to the colon that ends it).
687 if (SkipUntil(tok::colon, tok::r_brace, StopAtSemi | StopBeforeMatch)) {
688 TryConsumeToken(tok::colon, ColonLoc);
689 continue;
690 }
691 return StmtError();
692 }
693 } else {
694 LHS = Expr;
695 MissingCase = false;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000696 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000697
Chris Lattner34a22092009-03-04 04:23:07 +0000698 // GNU case range extension.
699 SourceLocation DotDotDotLoc;
John McCalldadc5752010-08-24 06:29:42 +0000700 ExprResult RHS;
Alp Tokerec543272013-12-24 09:48:30 +0000701 if (TryConsumeToken(tok::ellipsis, DotDotDotLoc)) {
702 Diag(DotDotDotLoc, diag::ext_gnu_case_range);
Chris Lattner34a22092009-03-04 04:23:07 +0000703 RHS = ParseConstantExpression();
704 if (RHS.isInvalid()) {
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000705 if (SkipUntil(tok::colon, tok::r_brace, StopAtSemi | StopBeforeMatch)) {
706 TryConsumeToken(tok::colon, ColonLoc);
707 continue;
708 }
Chris Lattner34a22092009-03-04 04:23:07 +0000709 return StmtError();
710 }
711 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000712
Chris Lattner125c0ee2009-12-10 00:38:54 +0000713 ColonProtection.restore();
Sebastian Redl042ad952008-12-11 19:30:53 +0000714
Alp Tokerec543272013-12-24 09:48:30 +0000715 if (TryConsumeToken(tok::colon, ColonLoc)) {
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000716 } else if (TryConsumeToken(tok::semi, ColonLoc) ||
717 TryConsumeToken(tok::coloncolon, ColonLoc)) {
718 // Treat "case blah;" or "case blah::" as a typo for "case blah:".
Alp Tokerec543272013-12-24 09:48:30 +0000719 Diag(ColonLoc, diag::err_expected_after)
720 << "'case'" << tok::colon
721 << FixItHint::CreateReplacement(ColonLoc, ":");
John McCall0140bfe2011-01-22 09:28:32 +0000722 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000723 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
Alp Tokerec543272013-12-24 09:48:30 +0000724 Diag(ExpectedLoc, diag::err_expected_after)
725 << "'case'" << tok::colon
726 << FixItHint::CreateInsertion(ExpectedLoc, ":");
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000727 ColonLoc = ExpectedLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000728 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000729
John McCalldadc5752010-08-24 06:29:42 +0000730 StmtResult Case =
John McCallb268a282010-08-23 23:25:46 +0000731 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
732 RHS.get(), ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000733
Chris Lattner34a22092009-03-04 04:23:07 +0000734 // If we had a sema error parsing this case, then just ignore it and
735 // continue parsing the sub-stmt.
736 if (Case.isInvalid()) {
737 if (TopLevelCase.isInvalid()) // No parsed case stmts.
Alexey Bataevc4fad652016-01-13 11:18:54 +0000738 return ParseStatement(/*TrailingElseLoc=*/nullptr,
739 /*AllowOpenMPStandalone=*/true);
Chris Lattner34a22092009-03-04 04:23:07 +0000740 // Otherwise, just don't add it as a nested case.
741 } else {
742 // If this is the first case statement we parsed, it becomes TopLevelCase.
743 // Otherwise we link it into the current chain.
John McCall37ad5512010-08-23 06:44:23 +0000744 Stmt *NextDeepest = Case.get();
Chris Lattner34a22092009-03-04 04:23:07 +0000745 if (TopLevelCase.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000746 TopLevelCase = Case;
Chris Lattner34a22092009-03-04 04:23:07 +0000747 else
John McCallb268a282010-08-23 23:25:46 +0000748 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner34a22092009-03-04 04:23:07 +0000749 DeepestParsedCaseStmt = NextDeepest;
750 }
Mike Stump11289f42009-09-09 15:08:12 +0000751
Chris Lattner34a22092009-03-04 04:23:07 +0000752 // Handle all case statements.
753 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000754
Chris Lattner34a22092009-03-04 04:23:07 +0000755 // If we found a non-case statement, start by parsing it.
John McCalldadc5752010-08-24 06:29:42 +0000756 StmtResult SubStmt;
Mike Stump11289f42009-09-09 15:08:12 +0000757
Chris Lattner34a22092009-03-04 04:23:07 +0000758 if (Tok.isNot(tok::r_brace)) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000759 SubStmt = ParseStatement(/*TrailingElseLoc=*/nullptr,
760 /*AllowOpenMPStandalone=*/true);
Chris Lattner34a22092009-03-04 04:23:07 +0000761 } else {
762 // Nicely diagnose the common error "switch (X) { case 4: }", which is
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000763 // not valid. If ColonLoc doesn't point to a valid text location, there was
764 // another parsing error, so avoid producing extra diagnostics.
765 if (ColonLoc.isValid()) {
766 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
767 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
768 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
769 }
770 SubStmt = StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000771 }
Mike Stump11289f42009-09-09 15:08:12 +0000772
Chris Lattner34a22092009-03-04 04:23:07 +0000773 // Install the body into the most deeply-nested case.
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000774 if (DeepestParsedCaseStmt) {
775 // Broken sub-stmt shouldn't prevent forming the case statement properly.
776 if (SubStmt.isInvalid())
777 SubStmt = Actions.ActOnNullStmt(SourceLocation());
778 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
779 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000780
Chris Lattner34a22092009-03-04 04:23:07 +0000781 // Return the top level parsed statement tree.
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000782 return TopLevelCase;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000783}
784
785/// ParseDefaultStatement
786/// labeled-statement:
787/// 'default' ':' statement
788/// Note that this does not parse the 'statement' at the end.
789///
Richard Smithc202b282012-04-14 00:33:13 +0000790StmtResult Parser::ParseDefaultStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000791 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000792 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000793
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000794 SourceLocation ColonLoc;
Alp Tokerec543272013-12-24 09:48:30 +0000795 if (TryConsumeToken(tok::colon, ColonLoc)) {
Alp Tokerec543272013-12-24 09:48:30 +0000796 } else if (TryConsumeToken(tok::semi, ColonLoc)) {
Alp Toker97650562014-01-10 11:19:30 +0000797 // Treat "default;" as a typo for "default:".
Alp Tokerec543272013-12-24 09:48:30 +0000798 Diag(ColonLoc, diag::err_expected_after)
799 << "'default'" << tok::colon
800 << FixItHint::CreateReplacement(ColonLoc, ":");
John McCall0140bfe2011-01-22 09:28:32 +0000801 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000802 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
Alp Tokerec543272013-12-24 09:48:30 +0000803 Diag(ExpectedLoc, diag::err_expected_after)
804 << "'default'" << tok::colon
805 << FixItHint::CreateInsertion(ExpectedLoc, ":");
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000806 ColonLoc = ExpectedLoc;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000807 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000808
Richard Smith1002d102012-02-17 01:35:32 +0000809 StmtResult SubStmt;
810
811 if (Tok.isNot(tok::r_brace)) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000812 SubStmt = ParseStatement(/*TrailingElseLoc=*/nullptr,
813 /*AllowOpenMPStandalone=*/true);
Richard Smith1002d102012-02-17 01:35:32 +0000814 } else {
815 // Diagnose the common error "switch (X) {... default: }", which is
816 // not valid.
David Majnemerc6a99872011-06-14 15:24:38 +0000817 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
Richard Smith1002d102012-02-17 01:35:32 +0000818 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
819 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
820 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000821 }
822
Richard Smith1002d102012-02-17 01:35:32 +0000823 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000824 if (SubStmt.isInvalid())
Richard Smith1002d102012-02-17 01:35:32 +0000825 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl042ad952008-12-11 19:30:53 +0000826
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000827 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000828 SubStmt.get(), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000829}
830
Richard Smithc202b282012-04-14 00:33:13 +0000831StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
832 return ParseCompoundStatement(isStmtExpr, Scope::DeclScope);
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000833}
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000834
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000835/// ParseCompoundStatement - Parse a "{}" block.
836///
837/// compound-statement: [C99 6.8.2]
838/// { block-item-list[opt] }
839/// [GNU] { label-declarations block-item-list } [TODO]
840///
841/// block-item-list:
842/// block-item
843/// block-item-list block-item
844///
845/// block-item:
846/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000847/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000848/// statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000849///
850/// [GNU] label-declarations:
851/// [GNU] label-declaration
852/// [GNU] label-declarations label-declaration
853///
854/// [GNU] label-declaration:
855/// [GNU] '__label__' identifier-list ';'
856///
Richard Smithc202b282012-04-14 00:33:13 +0000857StmtResult Parser::ParseCompoundStatement(bool isStmtExpr,
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000858 unsigned ScopeFlags) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000859 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000860
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000861 // Enter a scope to hold everything within the compound stmt. Compound
862 // statements can always hold declarations.
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000863 ParseScope CompoundScope(this, ScopeFlags);
Chris Lattnerf2978802007-01-21 06:52:16 +0000864
865 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000866 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000867}
868
Lang Hames2954cea2012-11-03 22:29:05 +0000869/// Parse any pragmas at the start of the compound expression. We handle these
870/// separately since some pragmas (FP_CONTRACT) must appear before any C
871/// statement in the compound, but may be intermingled with other pragmas.
872void Parser::ParseCompoundStatementLeadingPragmas() {
873 bool checkForPragmas = true;
874 while (checkForPragmas) {
875 switch (Tok.getKind()) {
876 case tok::annot_pragma_vis:
877 HandlePragmaVisibility();
878 break;
879 case tok::annot_pragma_pack:
880 HandlePragmaPack();
881 break;
882 case tok::annot_pragma_msstruct:
883 HandlePragmaMSStruct();
884 break;
885 case tok::annot_pragma_align:
886 HandlePragmaAlign();
887 break;
888 case tok::annot_pragma_weak:
889 HandlePragmaWeak();
890 break;
891 case tok::annot_pragma_weakalias:
892 HandlePragmaWeakAlias();
893 break;
894 case tok::annot_pragma_redefine_extname:
895 HandlePragmaRedefineExtname();
896 break;
897 case tok::annot_pragma_opencl_extension:
898 HandlePragmaOpenCLExtension();
899 break;
900 case tok::annot_pragma_fp_contract:
901 HandlePragmaFPContract();
902 break;
David Majnemer4bb09802014-02-10 19:50:15 +0000903 case tok::annot_pragma_ms_pointers_to_members:
904 HandlePragmaMSPointersToMembers();
905 break;
Warren Huntc3b18962014-04-08 22:30:47 +0000906 case tok::annot_pragma_ms_pragma:
907 HandlePragmaMSPragma();
908 break;
Alexey Bataev3d42f342015-11-20 07:02:57 +0000909 case tok::annot_pragma_ms_vtordisp:
910 HandlePragmaMSVtorDisp();
911 break;
Richard Smithba3a4f92016-01-12 21:59:26 +0000912 case tok::annot_pragma_dump:
913 HandlePragmaDump();
914 break;
Lang Hames2954cea2012-11-03 22:29:05 +0000915 default:
916 checkForPragmas = false;
917 break;
918 }
919 }
920
921}
922
Chris Lattnerf2978802007-01-21 06:52:16 +0000923/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000924/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000925/// consume the '}' at the end of the block. It does not manipulate the scope
926/// stack.
John McCalldadc5752010-08-24 06:29:42 +0000927StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000928 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000929 Tok.getLocation(),
930 "in compound statement ('{}')");
Lang Hames5de91cc2012-10-02 04:45:10 +0000931
932 // Record the state of the FP_CONTRACT pragma, restore on leaving the
933 // compound statement.
934 Sema::FPContractStateRAII SaveFPContractState(Actions);
935
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000936 InMessageExpressionRAIIObject InMessage(*this, false);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000937 BalancedDelimiterTracker T(*this, tok::l_brace);
938 if (T.consumeOpen())
939 return StmtError();
Chris Lattnerf2978802007-01-21 06:52:16 +0000940
Dmitri Gribenko800ddf32012-02-14 22:14:32 +0000941 Sema::CompoundScopeRAII CompoundScope(Actions);
942
Lang Hames2954cea2012-11-03 22:29:05 +0000943 // Parse any pragmas at the beginning of the compound statement.
944 ParseCompoundStatementLeadingPragmas();
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000945
Lang Hames2954cea2012-11-03 22:29:05 +0000946 StmtVector Stmts;
Lang Hamesa930e712012-10-21 01:10:01 +0000947
Chris Lattner43e7f312011-02-18 02:08:43 +0000948 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
949 // only allowed at the start of a compound stmt regardless of the language.
950 while (Tok.is(tok::kw___label__)) {
951 SourceLocation LabelLoc = ConsumeToken();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000952
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000953 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner43e7f312011-02-18 02:08:43 +0000954 while (1) {
955 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000956 Diag(Tok, diag::err_expected) << tok::identifier;
Chris Lattner43e7f312011-02-18 02:08:43 +0000957 break;
958 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000959
Chris Lattner43e7f312011-02-18 02:08:43 +0000960 IdentifierInfo *II = Tok.getIdentifierInfo();
961 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000962 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000963
Alp Tokerec543272013-12-24 09:48:30 +0000964 if (!TryConsumeToken(tok::comma))
Chris Lattner43e7f312011-02-18 02:08:43 +0000965 break;
Chris Lattner43e7f312011-02-18 02:08:43 +0000966 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000967
John McCall084e83d2011-03-24 11:26:52 +0000968 DeclSpec DS(AttrFactory);
Rafael Espindolaab417692013-07-09 12:05:01 +0000969 DeclGroupPtrTy Res =
970 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner43e7f312011-02-18 02:08:43 +0000971 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000972
Chris Lattner02f1b612012-04-28 16:12:17 +0000973 ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
Chris Lattner43e7f312011-02-18 02:08:43 +0000974 if (R.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000975 Stmts.push_back(R.get());
Chris Lattner43e7f312011-02-18 02:08:43 +0000976 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000977
Richard Smith752ada82015-11-17 23:32:01 +0000978 while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
979 Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000980 if (Tok.is(tok::annot_pragma_unused)) {
981 HandlePragmaUnused();
982 continue;
983 }
984
John McCalldadc5752010-08-24 06:29:42 +0000985 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000986 if (Tok.isNot(tok::kw___extension__)) {
Alexey Bataevc4fad652016-01-13 11:18:54 +0000987 R = ParseStatementOrDeclaration(Stmts, ACK_Any);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000988 } else {
989 // __extension__ can start declarations and it can also be a unary
990 // operator for expressions. Consume multiple __extension__ markers here
991 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000992 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000993 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000994 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000995 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +0000996
John McCall084e83d2011-03-24 11:26:52 +0000997 ParsedAttributesWithRange attrs(AttrFactory);
Craig Topper161e4db2014-05-21 06:02:52 +0000998 MaybeParseCXX11Attributes(attrs, nullptr,
999 /*MightBeObjCMessageSend*/ true);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001000
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001001 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +00001002 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +00001003 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +00001004 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +00001005 ExtensionRAIIObject O(Diags);
1006
Chris Lattner49836b42009-04-02 04:16:50 +00001007 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Rafael Espindola1bd906d2014-10-22 14:27:08 +00001008 DeclGroupPtrTy Res = ParseDeclaration(Declarator::BlockContext, DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001009 attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001010 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001011 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +00001012 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +00001013 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +00001014
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001015 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001016 SkipUntil(tok::semi);
1017 continue;
1018 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +00001019
Alexis Hunt96d5c762009-11-21 08:43:09 +00001020 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +00001021 // Eat the semicolon at the end of stmt and convert the expr into a
1022 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00001023 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +00001024 R = Actions.ActOnExprStmt(Res);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +00001025 }
1026 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001027
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001028 if (R.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001029 Stmts.push_back(R.get());
Chris Lattner30f910e2006-10-16 05:52:41 +00001030 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001031
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +00001032 SourceLocation CloseLoc = Tok.getLocation();
1033
Chris Lattner0ccd51e2006-08-09 05:47:47 +00001034 // We broke out of the while loop because we found a '}' or EOF.
Nico Webera48b6c22012-12-30 23:36:56 +00001035 if (!T.consumeClose())
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +00001036 // Recover by creating a compound statement with what we parsed so far,
1037 // instead of dropping everything and returning StmtError();
Nico Webera48b6c22012-12-30 23:36:56 +00001038 CloseLoc = T.getCloseLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +00001039
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +00001040 return Actions.ActOnCompoundStmt(T.getOpenLocation(), CloseLoc,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001041 Stmts, isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +00001042}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001043
Chris Lattnerc0081db2008-12-12 06:31:07 +00001044/// ParseParenExprOrCondition:
1045/// [C ] '(' expression ')'
Chris Lattner10da53c2008-12-12 06:35:28 +00001046/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattnerc0081db2008-12-12 06:31:07 +00001047///
1048/// This function parses and performs error recovery on the specified condition
1049/// or expression (depending on whether we're in C++ or C mode). This function
1050/// goes out of its way to recover well. It returns true if there was a parser
1051/// error (the right paren couldn't be found), which indicates that the caller
1052/// should try to recover harder. It returns false if the condition is
1053/// successfully parsed. Note that a successful parse can still have semantic
1054/// errors in the condition.
Richard Smith03a4aa32016-06-23 19:02:52 +00001055bool Parser::ParseParenExprOrCondition(Sema::ConditionResult &Cond,
Douglas Gregore60e41a2010-05-06 17:25:47 +00001056 SourceLocation Loc,
Richard Smith03a4aa32016-06-23 19:02:52 +00001057 Sema::ConditionKind CK) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001058 BalancedDelimiterTracker T(*this, tok::l_paren);
1059 T.consumeOpen();
1060
David Blaikiebbafb8a2012-03-11 07:00:24 +00001061 if (getLangOpts().CPlusPlus)
Richard Smith03a4aa32016-06-23 19:02:52 +00001062 Cond = ParseCXXCondition(Loc, CK);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001063 else {
Richard Smith03a4aa32016-06-23 19:02:52 +00001064 ExprResult CondExpr = ParseExpression();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001065
Douglas Gregore60e41a2010-05-06 17:25:47 +00001066 // If required, convert to a boolean value.
Richard Smith03a4aa32016-06-23 19:02:52 +00001067 if (CondExpr.isInvalid())
1068 Cond = Sema::ConditionError();
1069 else
1070 Cond = Actions.ActOnCondition(getCurScope(), Loc, CondExpr.get(), CK);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001071 }
Mike Stump11289f42009-09-09 15:08:12 +00001072
Chris Lattnerc0081db2008-12-12 06:31:07 +00001073 // If the parser was confused by the condition and we don't have a ')', try to
1074 // recover by skipping ahead to a semi and bailing out. If condexp is
1075 // semantically invalid but we have well formed code, keep going.
Richard Smith03a4aa32016-06-23 19:02:52 +00001076 if (Cond.isInvalid() && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +00001077 SkipUntil(tok::semi);
1078 // Skipping may have stopped if it found the containing ')'. If so, we can
1079 // continue parsing the if statement.
1080 if (Tok.isNot(tok::r_paren))
1081 return true;
1082 }
Mike Stump11289f42009-09-09 15:08:12 +00001083
Chris Lattnerc0081db2008-12-12 06:31:07 +00001084 // Otherwise the condition is valid or the rparen is present.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001085 T.consumeClose();
Chad Rosier67055f52012-07-10 21:35:27 +00001086
Chris Lattner70d44982012-04-28 16:24:20 +00001087 // Check for extraneous ')'s to catch things like "if (foo())) {". We know
1088 // that all callers are looking for a statement after the condition, so ")"
1089 // isn't valid.
1090 while (Tok.is(tok::r_paren)) {
1091 Diag(Tok, diag::err_extraneous_rparen_in_condition)
1092 << FixItHint::CreateRemoval(Tok.getLocation());
1093 ConsumeParen();
1094 }
Chad Rosier67055f52012-07-10 21:35:27 +00001095
Chris Lattnerc0081db2008-12-12 06:31:07 +00001096 return false;
1097}
1098
1099
Chris Lattnerc951dae2006-08-10 04:23:57 +00001100/// ParseIfStatement
1101/// if-statement: [C99 6.8.4.1]
1102/// 'if' '(' expression ')' statement
1103/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001104/// [C++] 'if' '(' condition ')' statement
1105/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +00001106///
Richard Smithc202b282012-04-14 00:33:13 +00001107StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001108 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001109 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +00001110
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001111 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001112 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +00001113 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +00001114 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +00001115 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001116
David Blaikiebbafb8a2012-03-11 07:00:24 +00001117 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001118
Chris Lattner2dd1b722007-08-26 23:08:06 +00001119 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
1120 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001121 //
1122 // C++ 6.4p3:
1123 // A name introduced by a declaration in a condition is in scope from its
1124 // point of declaration until the end of the substatements controlled by the
1125 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001126 // C++ 3.3.2p4:
1127 // Names declared in the for-init-statement, and in the condition of if,
1128 // while, for, and switch statements are local to the if, while, for, or
1129 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001130 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001131 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +00001132
Chris Lattnerc951dae2006-08-10 04:23:57 +00001133 // Parse the condition.
Richard Smith03a4aa32016-06-23 19:02:52 +00001134 Sema::ConditionResult Cond;
1135 if (ParseParenExprOrCondition(Cond, IfLoc, Sema::ConditionKind::Boolean))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001136 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +00001137
Chris Lattner8fb26252007-08-22 05:28:50 +00001138 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001139 // there is no compound stmt. C90 does not have this clause. We only do this
1140 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001141 //
1142 // C++ 6.4p1:
1143 // The substatement in a selection-statement (each substatement, in the else
1144 // form of the if statement) implicitly defines a local scope.
1145 //
1146 // For C++ we create a scope for the condition and a new scope for
1147 // substatements because:
1148 // -When the 'then' scope exits, we want the condition declaration to still be
1149 // active for the 'else' scope too.
1150 // -Sema will detect name clashes by considering declarations of a
1151 // 'ControlScope' as part of its direct subscope.
1152 // -If we wanted the condition and substatement to be in the same scope, we
1153 // would have to notify ParseStatement not to create a new scope. It's
1154 // simpler to let it create a new scope.
1155 //
David Majnemer2206bf52014-03-05 08:57:59 +00001156 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001157
Chris Lattner5c5808a2007-10-29 05:08:52 +00001158 // Read the 'then' stmt.
1159 SourceLocation ThenStmtLoc = Tok.getLocation();
Nico Weber3cef1082011-12-22 23:26:17 +00001160
1161 SourceLocation InnerStatementTrailingElseLoc;
1162 StmtResult ThenStmt(ParseStatement(&InnerStatementTrailingElseLoc));
Chris Lattnerac4471c2007-05-28 05:38:24 +00001163
Chris Lattner37e54f42007-08-22 05:16:28 +00001164 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001165 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001166
Chris Lattnerc951dae2006-08-10 04:23:57 +00001167 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +00001168 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +00001169 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +00001170 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001171
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001172 if (Tok.is(tok::kw_else)) {
Nico Weber3cef1082011-12-22 23:26:17 +00001173 if (TrailingElseLoc)
1174 *TrailingElseLoc = Tok.getLocation();
1175
Chris Lattneraf635312006-10-16 06:06:51 +00001176 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +00001177 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +00001178
Chris Lattner8fb26252007-08-22 05:28:50 +00001179 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001180 // there is no compound stmt. C90 does not have this clause. We only do
1181 // this if the body isn't a compound statement to avoid push/pop in common
1182 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001183 //
1184 // C++ 6.4p1:
1185 // The substatement in a selection-statement (each substatement, in the else
1186 // form of the if statement) implicitly defines a local scope.
1187 //
David Majnemer2206bf52014-03-05 08:57:59 +00001188 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001189
Chris Lattner30f910e2006-10-16 05:52:41 +00001190 ElseStmt = ParseStatement();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001191
Chris Lattner37e54f42007-08-22 05:16:28 +00001192 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001193 InnerScope.Exit();
Douglas Gregor4ecb7202011-07-30 08:36:53 +00001194 } else if (Tok.is(tok::code_completion)) {
1195 Actions.CodeCompleteAfterIf(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001196 cutOffParsing();
1197 return StmtError();
Nico Weber3cef1082011-12-22 23:26:17 +00001198 } else if (InnerStatementTrailingElseLoc.isValid()) {
1199 Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else);
Chris Lattnerc951dae2006-08-10 04:23:57 +00001200 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001201
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001202 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +00001203
Chris Lattner5c5808a2007-10-29 05:08:52 +00001204 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +00001205 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +00001206 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001207 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
Craig Topper161e4db2014-05-21 06:02:52 +00001208 (ThenStmt.isInvalid() && ElseStmt.get() == nullptr) ||
1209 (ThenStmt.get() == nullptr && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +00001210 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +00001211 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +00001212 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001213
Chris Lattner5c5808a2007-10-29 05:08:52 +00001214 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001215 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001216 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001217 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001218 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001219
Richard Smith03a4aa32016-06-23 19:02:52 +00001220 return Actions.ActOnIfStmt(IfLoc, Cond, ThenStmt.get(), ElseLoc,
1221 ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +00001222}
1223
Chris Lattner9075bd72006-08-10 04:59:57 +00001224/// ParseSwitchStatement
1225/// switch-statement:
1226/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001227/// [C++] 'switch' '(' condition ')' statement
Richard Smithc202b282012-04-14 00:33:13 +00001228StmtResult Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001229 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001230 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +00001231
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001232 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001233 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +00001234 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001235 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001236 }
Chris Lattner2dd1b722007-08-26 23:08:06 +00001237
David Blaikiebbafb8a2012-03-11 07:00:24 +00001238 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001239
Chris Lattner2dd1b722007-08-26 23:08:06 +00001240 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1241 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001242 //
1243 // C++ 6.4p3:
1244 // A name introduced by a declaration in a condition is in scope from its
1245 // point of declaration until the end of the substatements controlled by the
1246 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001247 // C++ 3.3.2p4:
1248 // Names declared in the for-init-statement, and in the condition of if,
1249 // while, for, and switch statements are local to the if, while, for, or
1250 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001251 //
Serge Pavlov09f99242014-01-23 15:05:00 +00001252 unsigned ScopeFlags = Scope::SwitchScope;
Chris Lattnerc0081db2008-12-12 06:31:07 +00001253 if (C99orCXX)
1254 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001255 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001256
Chris Lattner9075bd72006-08-10 04:59:57 +00001257 // Parse the condition.
Richard Smith03a4aa32016-06-23 19:02:52 +00001258 Sema::ConditionResult Cond;
1259 if (ParseParenExprOrCondition(Cond, SwitchLoc, Sema::ConditionKind::Switch))
Sebastian Redlb62406f2008-12-11 19:48:14 +00001260 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +00001261
Richard Smith03a4aa32016-06-23 19:02:52 +00001262 StmtResult Switch = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001263
Douglas Gregore60e41a2010-05-06 17:25:47 +00001264 if (Switch.isInvalid()) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001265 // Skip the switch body.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001266 // FIXME: This is not optimal recovery, but parsing the body is more
1267 // dangerous due to the presence of case and default statements, which
1268 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +00001269 if (Tok.is(tok::l_brace)) {
1270 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001271 SkipUntil(tok::r_brace);
Douglas Gregor4abc32d2010-05-20 23:20:59 +00001272 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +00001273 SkipUntil(tok::semi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001274 return Switch;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001275 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001276
Chris Lattner8fb26252007-08-22 05:28:50 +00001277 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001278 // there is no compound stmt. C90 does not have this clause. We only do this
1279 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001280 //
1281 // C++ 6.4p1:
1282 // The substatement in a selection-statement (each substatement, in the else
1283 // form of the if statement) implicitly defines a local scope.
1284 //
1285 // See comments in ParseIfStatement for why we create a scope for the
1286 // condition and a new scope for substatement in C++.
1287 //
Serge Pavlov09f99242014-01-23 15:05:00 +00001288 getCurScope()->AddFlags(Scope::BreakScope);
David Majnemer2206bf52014-03-05 08:57:59 +00001289 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +00001290
Hans Wennborg852c3462014-06-17 00:09:05 +00001291 // We have incremented the mangling number for the SwitchScope and the
1292 // InnerScope, which is one too many.
1293 if (C99orCXX)
David Majnemera7f8c462015-03-19 21:54:30 +00001294 getCurScope()->decrementMSManglingNumber();
Hans Wennborg852c3462014-06-17 00:09:05 +00001295
Chris Lattner9075bd72006-08-10 04:59:57 +00001296 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001297 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001298
Chris Lattner8fd2d012010-01-24 01:50:29 +00001299 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001300 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001301 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001302
John McCallb268a282010-08-23 23:25:46 +00001303 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001304}
1305
1306/// ParseWhileStatement
1307/// while-statement: [C99 6.8.5.1]
1308/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001309/// [C++] 'while' '(' condition ')' statement
Richard Smithc202b282012-04-14 00:33:13 +00001310StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001311 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +00001312 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +00001313 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001314
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001315 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001316 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +00001317 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001318 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001319 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001320
David Blaikiebbafb8a2012-03-11 07:00:24 +00001321 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001322
Chris Lattner2dd1b722007-08-26 23:08:06 +00001323 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1324 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001325 //
1326 // C++ 6.4p3:
1327 // A name introduced by a declaration in a condition is in scope from its
1328 // point of declaration until the end of the substatements controlled by the
1329 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001330 // C++ 3.3.2p4:
1331 // Names declared in the for-init-statement, and in the condition of if,
1332 // while, for, and switch statements are local to the if, while, for, or
1333 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001334 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001335 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001336 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001337 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1338 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001339 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001340 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1341 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001342
Chris Lattner9075bd72006-08-10 04:59:57 +00001343 // Parse the condition.
Richard Smith03a4aa32016-06-23 19:02:52 +00001344 Sema::ConditionResult Cond;
1345 if (ParseParenExprOrCondition(Cond, WhileLoc, Sema::ConditionKind::Boolean))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001346 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001347
Justin Bognere4ebb6c2013-12-03 07:36:55 +00001348 // C99 6.8.5p5 - In C99, the body of the while statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001349 // there is no compound stmt. C90 does not have this clause. We only do this
1350 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001351 //
1352 // C++ 6.5p2:
1353 // The substatement in an iteration-statement implicitly defines a local scope
1354 // which is entered and exited each time through the loop.
1355 //
1356 // See comments in ParseIfStatement for why we create a scope for the
1357 // condition and a new scope for substatement in C++.
1358 //
David Majnemer2206bf52014-03-05 08:57:59 +00001359 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001360
Chris Lattner9075bd72006-08-10 04:59:57 +00001361 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001362 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001363
Chris Lattner8fb26252007-08-22 05:28:50 +00001364 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001365 InnerScope.Exit();
1366 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001367
Richard Smith03a4aa32016-06-23 19:02:52 +00001368 if (Cond.isInvalid() || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001369 return StmtError();
1370
Richard Smith03a4aa32016-06-23 19:02:52 +00001371 return Actions.ActOnWhileStmt(WhileLoc, Cond, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001372}
1373
1374/// ParseDoStatement
1375/// do-statement: [C99 6.8.5.2]
1376/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +00001377/// Note: this lets the caller parse the end ';'.
Richard Smithc202b282012-04-14 00:33:13 +00001378StmtResult Parser::ParseDoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001379 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001380 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001381
Chris Lattner2dd1b722007-08-26 23:08:06 +00001382 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1383 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001384 unsigned ScopeFlags;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001385 if (getLangOpts().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001386 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001387 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001388 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001389
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001390 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001391
Justin Bognere4ebb6c2013-12-03 07:36:55 +00001392 // C99 6.8.5p5 - In C99, the body of the do statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001393 // there is no compound stmt. C90 does not have this clause. We only do this
1394 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +00001395 //
1396 // C++ 6.5p2:
1397 // The substatement in an iteration-statement implicitly defines a local scope
1398 // which is entered and exited each time through the loop.
1399 //
David Majnemer2206bf52014-03-05 08:57:59 +00001400 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
1401 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001402
Chris Lattner9075bd72006-08-10 04:59:57 +00001403 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001404 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +00001405
Chris Lattner8fb26252007-08-22 05:28:50 +00001406 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001407 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001408
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001409 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001410 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +00001411 Diag(Tok, diag::err_expected_while);
Alp Tokerec543272013-12-24 09:48:30 +00001412 Diag(DoLoc, diag::note_matching) << "'do'";
Alexey Bataevee6507d2013-11-18 08:17:37 +00001413 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner0046de12008-11-13 18:52:53 +00001414 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001415 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001416 }
Chris Lattneraf635312006-10-16 06:06:51 +00001417 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001418
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001419 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001420 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Alexey Bataevee6507d2013-11-18 08:17:37 +00001421 SkipUntil(tok::semi, StopBeforeMatch);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001422 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001423 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001424
Richard Smithc2c8bb82013-10-15 01:34:54 +00001425 // Parse the parenthesized expression.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001426 BalancedDelimiterTracker T(*this, tok::l_paren);
1427 T.consumeOpen();
Chad Rosier67055f52012-07-10 21:35:27 +00001428
Richard Smithc2c8bb82013-10-15 01:34:54 +00001429 // A do-while expression is not a condition, so can't have attributes.
1430 DiagnoseAndSkipCXX11Attributes();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001431
John McCalldadc5752010-08-24 06:29:42 +00001432 ExprResult Cond = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001433 T.consumeClose();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001434 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001435
Sebastian Redlb62406f2008-12-11 19:48:14 +00001436 if (Cond.isInvalid() || Body.isInvalid())
1437 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001438
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001439 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(),
1440 Cond.get(), T.getCloseLocation());
Chris Lattner9075bd72006-08-10 04:59:57 +00001441}
1442
Richard Smith955bf012014-06-19 11:42:00 +00001443bool Parser::isForRangeIdentifier() {
1444 assert(Tok.is(tok::identifier));
1445
1446 const Token &Next = NextToken();
1447 if (Next.is(tok::colon))
1448 return true;
1449
Daniel Marjamakie59f8d72015-06-18 10:59:26 +00001450 if (Next.isOneOf(tok::l_square, tok::kw_alignas)) {
Richard Smith955bf012014-06-19 11:42:00 +00001451 TentativeParsingAction PA(*this);
1452 ConsumeToken();
1453 SkipCXX11Attributes();
1454 bool Result = Tok.is(tok::colon);
1455 PA.Revert();
1456 return Result;
1457 }
1458
1459 return false;
1460}
1461
Chris Lattner9075bd72006-08-10 04:59:57 +00001462/// ParseForStatement
1463/// for-statement: [C99 6.8.5.3]
1464/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1465/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001466/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1467/// [C++] statement
Richard Smith0e304ea2015-10-22 04:46:14 +00001468/// [C++0x] 'for'
1469/// 'co_await'[opt] [Coroutines]
1470/// '(' for-range-declaration ':' for-range-initializer ')'
1471/// statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001472/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1473/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001474///
1475/// [C++] for-init-statement:
1476/// [C++] expression-statement
1477/// [C++] simple-declaration
1478///
Richard Smith02e85f32011-04-14 22:09:26 +00001479/// [C++0x] for-range-declaration:
1480/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1481/// [C++0x] for-range-initializer:
1482/// [C++0x] expression
1483/// [C++0x] braced-init-list [TODO]
Richard Smithc202b282012-04-14 00:33:13 +00001484StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001485 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001486 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001487
Richard Smith0e304ea2015-10-22 04:46:14 +00001488 SourceLocation CoawaitLoc;
1489 if (Tok.is(tok::kw_co_await))
1490 CoawaitLoc = ConsumeToken();
1491
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001492 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001493 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +00001494 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001495 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001496 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001497
Chad Rosier67055f52012-07-10 21:35:27 +00001498 bool C99orCXXorObjC = getLangOpts().C99 || getLangOpts().CPlusPlus ||
1499 getLangOpts().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001500
Chris Lattner2dd1b722007-08-26 23:08:06 +00001501 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1502 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001503 //
1504 // C++ 6.4p3:
1505 // A name introduced by a declaration in a condition is in scope from its
1506 // point of declaration until the end of the substatements controlled by the
1507 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001508 // C++ 3.3.2p4:
1509 // Names declared in the for-init-statement, and in the condition of if,
1510 // while, for, and switch statements are local to the if, while, for, or
1511 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001512 // C++ 6.5.3p1:
1513 // Names declared in the for-init-statement are in the same declarative-region
1514 // as those declared in the condition.
1515 //
Serge Pavlov09f99242014-01-23 15:05:00 +00001516 unsigned ScopeFlags = 0;
Chris Lattner934074c2009-04-22 00:54:41 +00001517 if (C99orCXXorObjC)
Serge Pavlov09f99242014-01-23 15:05:00 +00001518 ScopeFlags = Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001519
1520 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +00001521
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001522 BalancedDelimiterTracker T(*this, tok::l_paren);
1523 T.consumeOpen();
1524
John McCalldadc5752010-08-24 06:29:42 +00001525 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001526
Richard Smith02e85f32011-04-14 22:09:26 +00001527 bool ForEach = false, ForRange = false;
John McCalldadc5752010-08-24 06:29:42 +00001528 StmtResult FirstPart;
Richard Smith03a4aa32016-06-23 19:02:52 +00001529 Sema::ConditionResult SecondPart;
John McCalldadc5752010-08-24 06:29:42 +00001530 ExprResult Collection;
Richard Smith02e85f32011-04-14 22:09:26 +00001531 ForRangeInit ForRangeInit;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001532 FullExprArg ThirdPart(Actions);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001533
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001534 if (Tok.is(tok::code_completion)) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001535 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001536 C99orCXXorObjC? Sema::PCC_ForInit
1537 : Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001538 cutOffParsing();
1539 return StmtError();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001540 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001541
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001542 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001543 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001544
Chris Lattner9075bd72006-08-10 04:59:57 +00001545 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001546 if (Tok.is(tok::semi)) { // for (;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001547 ProhibitAttributes(attrs);
Chris Lattner53361ac2006-08-10 05:19:57 +00001548 // no first part, eat the ';'.
1549 ConsumeToken();
Richard Smith955bf012014-06-19 11:42:00 +00001550 } else if (getLangOpts().CPlusPlus && Tok.is(tok::identifier) &&
1551 isForRangeIdentifier()) {
1552 ProhibitAttributes(attrs);
1553 IdentifierInfo *Name = Tok.getIdentifierInfo();
1554 SourceLocation Loc = ConsumeToken();
1555 MaybeParseCXX11Attributes(attrs);
1556
1557 ForRangeInit.ColonLoc = ConsumeToken();
1558 if (Tok.is(tok::l_brace))
1559 ForRangeInit.RangeExpr = ParseBraceInitializer();
1560 else
1561 ForRangeInit.RangeExpr = ParseExpression();
1562
Richard Smith83d3f152014-11-27 01:54:27 +00001563 Diag(Loc, diag::err_for_range_identifier)
Richard Smith955bf012014-06-19 11:42:00 +00001564 << ((getLangOpts().CPlusPlus11 && !getLangOpts().CPlusPlus1z)
1565 ? FixItHint::CreateInsertion(Loc, "auto &&")
1566 : FixItHint());
1567
1568 FirstPart = Actions.ActOnCXXForRangeIdentifier(getCurScope(), Loc, Name,
1569 attrs, attrs.Range.getEnd());
1570 ForRange = true;
Eli Friedman0ffc31c2011-12-20 01:50:37 +00001571 } else if (isForInitDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001572 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001573 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001574 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001575
Richard Smith02e85f32011-04-14 22:09:26 +00001576 // In C++0x, "for (T NS:a" might not be a typo for ::
David Blaikiebbafb8a2012-03-11 07:00:24 +00001577 bool MightBeForRangeStmt = getLangOpts().CPlusPlus;
Richard Smith02e85f32011-04-14 22:09:26 +00001578 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1579
Chris Lattner49836b42009-04-02 04:16:50 +00001580 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00001581 DeclGroupPtrTy DG = ParseSimpleDeclaration(
Rafael Espindola1bd906d2014-10-22 14:27:08 +00001582 Declarator::ForContext, DeclEnd, attrs, false,
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00001583 MightBeForRangeStmt ? &ForRangeInit : nullptr);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001584 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Richard Smith02e85f32011-04-14 22:09:26 +00001585 if (ForRangeInit.ParsedForRangeDecl()) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001586 Diag(ForRangeInit.ColonLoc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00001587 diag::warn_cxx98_compat_for_range : diag::ext_for_range);
Richard Smith58c74332011-09-04 19:54:14 +00001588
Richard Smith02e85f32011-04-14 22:09:26 +00001589 ForRange = true;
1590 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattner32dc41c2009-03-29 17:27:48 +00001591 ConsumeToken();
1592 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001593 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001594 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001595 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001596
Douglas Gregor68762e72010-08-23 21:17:50 +00001597 if (Tok.is(tok::code_completion)) {
1598 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001599 cutOffParsing();
1600 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001601 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001602 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001603 } else {
1604 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001605 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001606 } else {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001607 ProhibitAttributes(attrs);
Kaelyn Takatab16e6322014-11-20 22:06:40 +00001608 Value = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Chris Lattner71e23ce2006-11-04 20:18:38 +00001609
John McCall34376a62010-12-04 03:47:34 +00001610 ForEach = isTokIdentifier_in();
1611
Chris Lattnercd68f642007-06-27 01:06:29 +00001612 // Turn the expression into a stmt.
John McCall34376a62010-12-04 03:47:34 +00001613 if (!Value.isInvalid()) {
1614 if (ForEach)
1615 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1616 else
Richard Smith945f8d32013-01-14 22:39:08 +00001617 FirstPart = Actions.ActOnExprStmt(Value);
John McCall34376a62010-12-04 03:47:34 +00001618 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001619
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001620 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001621 ConsumeToken();
John McCall34376a62010-12-04 03:47:34 +00001622 } else if (ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001623 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001624
Douglas Gregor68762e72010-08-23 21:17:50 +00001625 if (Tok.is(tok::code_completion)) {
David Blaikie0403cb12016-01-15 23:43:25 +00001626 Actions.CodeCompleteObjCForCollection(getCurScope(), nullptr);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001627 cutOffParsing();
1628 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001629 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001630 Collection = ParseExpression();
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001631 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::colon) && FirstPart.get()) {
Richard Smith4f848f12011-12-20 22:56:20 +00001632 // User tried to write the reasonable, but ill-formed, for-range-statement
1633 // for (expr : expr) { ... }
1634 Diag(Tok, diag::err_for_range_expected_decl)
1635 << FirstPart.get()->getSourceRange();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001636 SkipUntil(tok::r_paren, StopBeforeMatch);
Richard Smith03a4aa32016-06-23 19:02:52 +00001637 SecondPart = Sema::ConditionError();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001638 } else {
Douglas Gregor230a7e62011-02-17 03:38:46 +00001639 if (!Value.isInvalid()) {
1640 Diag(Tok, diag::err_expected_semi_for);
1641 } else {
1642 // Skip until semicolon or rparen, don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001643 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor230a7e62011-02-17 03:38:46 +00001644 if (Tok.is(tok::semi))
1645 ConsumeToken();
1646 }
Chris Lattner53361ac2006-08-10 05:19:57 +00001647 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001648 }
Serge Pavlov09f99242014-01-23 15:05:00 +00001649
1650 // Parse the second part of the for specifier.
1651 getCurScope()->AddFlags(Scope::BreakScope | Scope::ContinueScope);
Richard Smith03a4aa32016-06-23 19:02:52 +00001652 if (!ForEach && !ForRange && !SecondPart.isInvalid()) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001653 // Parse the second part of the for specifier.
1654 if (Tok.is(tok::semi)) { // for (...;;
1655 // no second part.
Douglas Gregor230a7e62011-02-17 03:38:46 +00001656 } else if (Tok.is(tok::r_paren)) {
1657 // missing both semicolons.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001658 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001659 if (getLangOpts().CPlusPlus)
Richard Smith03a4aa32016-06-23 19:02:52 +00001660 SecondPart = ParseCXXCondition(ForLoc, Sema::ConditionKind::Boolean);
Douglas Gregore60e41a2010-05-06 17:25:47 +00001661 else {
Richard Smith03a4aa32016-06-23 19:02:52 +00001662 ExprResult SecondExpr = ParseExpression();
1663 if (SecondExpr.isInvalid())
1664 SecondPart = Sema::ConditionError();
1665 else
1666 SecondPart =
1667 Actions.ActOnCondition(getCurScope(), ForLoc, SecondExpr.get(),
1668 Sema::ConditionKind::Boolean);
Douglas Gregore60e41a2010-05-06 17:25:47 +00001669 }
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001670 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001671
Douglas Gregor230a7e62011-02-17 03:38:46 +00001672 if (Tok.isNot(tok::semi)) {
Richard Smith03a4aa32016-06-23 19:02:52 +00001673 if (!SecondPart.isInvalid())
Douglas Gregor230a7e62011-02-17 03:38:46 +00001674 Diag(Tok, diag::err_expected_semi_for);
1675 else
1676 // Skip until semicolon or rparen, don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001677 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor230a7e62011-02-17 03:38:46 +00001678 }
1679
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001680 if (Tok.is(tok::semi)) {
1681 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001682 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001683
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001684 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001685 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001686 ExprResult Third = ParseExpression();
Richard Smith945f8d32013-01-14 22:39:08 +00001687 // FIXME: The C++11 standard doesn't actually say that this is a
1688 // discarded-value expression, but it clearly should be.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001689 ThirdPart = Actions.MakeFullDiscardedValueExpr(Third.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001690 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001691 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001692 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001693 T.consumeClose();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001694
Richard Smith0e304ea2015-10-22 04:46:14 +00001695 // C++ Coroutines [stmt.iter]:
1696 // 'co_await' can only be used for a range-based for statement.
1697 if (CoawaitLoc.isValid() && !ForRange) {
1698 Diag(CoawaitLoc, diag::err_for_co_await_not_range_for);
1699 CoawaitLoc = SourceLocation();
1700 }
1701
Richard Smith02e85f32011-04-14 22:09:26 +00001702 // We need to perform most of the semantic analysis for a C++0x for-range
1703 // statememt before parsing the body, in order to be able to deduce the type
1704 // of an auto-typed loop variable.
1705 StmtResult ForRangeStmt;
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001706 StmtResult ForEachStmt;
Chad Rosier67055f52012-07-10 21:35:27 +00001707
John McCall53848232011-07-27 01:07:15 +00001708 if (ForRange) {
Denis Zobnin7d6b9242016-02-02 17:33:09 +00001709 ExprResult CorrectedRange =
1710 Actions.CorrectDelayedTyposInExpr(ForRangeInit.RangeExpr.get());
Richard Smith9f690bd2015-10-27 06:02:45 +00001711 ForRangeStmt = Actions.ActOnCXXForRangeStmt(
1712 getCurScope(), ForLoc, CoawaitLoc, FirstPart.get(),
Denis Zobnin7d6b9242016-02-02 17:33:09 +00001713 ForRangeInit.ColonLoc, CorrectedRange.get(),
Richard Smith9f690bd2015-10-27 06:02:45 +00001714 T.getCloseLocation(), Sema::BFRK_Build);
John McCall53848232011-07-27 01:07:15 +00001715
1716 // Similarly, we need to do the semantic analysis for a for-range
1717 // statement immediately in order to close over temporaries correctly.
1718 } else if (ForEach) {
Sam Panzer2c4ca0f2012-08-16 21:47:25 +00001719 ForEachStmt = Actions.ActOnObjCForCollectionStmt(ForLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001720 FirstPart.get(),
1721 Collection.get(),
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001722 T.getCloseLocation());
Alexey Bataev9c821032015-04-30 04:23:23 +00001723 } else {
1724 // In OpenMP loop region loop control variable must be captured and be
1725 // private. Perform analysis of first part (if any).
1726 if (getLangOpts().OpenMP && FirstPart.isUsable()) {
1727 Actions.ActOnOpenMPLoopInitialization(ForLoc, FirstPart.get());
1728 }
John McCall53848232011-07-27 01:07:15 +00001729 }
1730
Justin Bognere4ebb6c2013-12-03 07:36:55 +00001731 // C99 6.8.5p5 - In C99, the body of the for statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001732 // there is no compound stmt. C90 does not have this clause. We only do this
1733 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001734 //
1735 // C++ 6.5p2:
1736 // The substatement in an iteration-statement implicitly defines a local scope
1737 // which is entered and exited each time through the loop.
1738 //
1739 // See comments in ParseIfStatement for why we create a scope for
1740 // for-init-statement/condition and a new scope for substatement in C++.
1741 //
David Majnemer2206bf52014-03-05 08:57:59 +00001742 ParseScope InnerScope(this, Scope::DeclScope, C99orCXXorObjC,
1743 Tok.is(tok::l_brace));
1744
1745 // The body of the for loop has the same local mangling number as the
1746 // for-init-statement.
1747 // It will only be incremented if the body contains other things that would
1748 // normally increment the mangling number (like a compound statement).
1749 if (C99orCXXorObjC)
David Majnemera7f8c462015-03-19 21:54:30 +00001750 getCurScope()->decrementMSManglingNumber();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001751
Chris Lattner9075bd72006-08-10 04:59:57 +00001752 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001753 StmtResult Body(ParseStatement(TrailingElseLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001754
Chris Lattner8fb26252007-08-22 05:28:50 +00001755 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001756 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001757
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001758 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001759 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001760
1761 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001762 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001763
Richard Smith02e85f32011-04-14 22:09:26 +00001764 if (ForEach)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001765 return Actions.FinishObjCForCollectionStmt(ForEachStmt.get(),
1766 Body.get());
Mike Stump11289f42009-09-09 15:08:12 +00001767
Richard Smith02e85f32011-04-14 22:09:26 +00001768 if (ForRange)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001769 return Actions.FinishCXXForRangeStmt(ForRangeStmt.get(), Body.get());
Richard Smith02e85f32011-04-14 22:09:26 +00001770
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001771 return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.get(),
Richard Smith03a4aa32016-06-23 19:02:52 +00001772 SecondPart, ThirdPart, T.getCloseLocation(),
1773 Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001774}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001775
Chris Lattner503fadc2006-08-10 05:45:44 +00001776/// ParseGotoStatement
1777/// jump-statement:
1778/// 'goto' identifier ';'
1779/// [GNU] 'goto' '*' expression ';'
1780///
1781/// Note: this lets the caller parse the end ';'.
1782///
Richard Smithc202b282012-04-14 00:33:13 +00001783StmtResult Parser::ParseGotoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001784 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001785 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001786
John McCalldadc5752010-08-24 06:29:42 +00001787 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001788 if (Tok.is(tok::identifier)) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +00001789 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1790 Tok.getLocation());
1791 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Chris Lattner503fadc2006-08-10 05:45:44 +00001792 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001793 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001794 // GNU indirect goto extension.
1795 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001796 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001797 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001798 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001799 SkipUntil(tok::semi, StopBeforeMatch);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001800 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001801 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001802 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.get());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001803 } else {
Alp Tokerec543272013-12-24 09:48:30 +00001804 Diag(Tok, diag::err_expected) << tok::identifier;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001805 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001806 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001807
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001808 return Res;
Chris Lattner503fadc2006-08-10 05:45:44 +00001809}
1810
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001811/// ParseContinueStatement
1812/// jump-statement:
1813/// 'continue' ';'
1814///
1815/// Note: this lets the caller parse the end ';'.
1816///
Richard Smithc202b282012-04-14 00:33:13 +00001817StmtResult Parser::ParseContinueStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001818 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001819 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001820}
1821
1822/// ParseBreakStatement
1823/// jump-statement:
1824/// 'break' ';'
1825///
1826/// Note: this lets the caller parse the end ';'.
1827///
Richard Smithc202b282012-04-14 00:33:13 +00001828StmtResult Parser::ParseBreakStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001829 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001830 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001831}
1832
Chris Lattner503fadc2006-08-10 05:45:44 +00001833/// ParseReturnStatement
1834/// jump-statement:
1835/// 'return' expression[opt] ';'
Richard Smith0e304ea2015-10-22 04:46:14 +00001836/// 'return' braced-init-list ';'
1837/// 'co_return' expression[opt] ';'
1838/// 'co_return' braced-init-list ';'
Richard Smithc202b282012-04-14 00:33:13 +00001839StmtResult Parser::ParseReturnStatement() {
Richard Smith0e304ea2015-10-22 04:46:14 +00001840 assert((Tok.is(tok::kw_return) || Tok.is(tok::kw_co_return)) &&
1841 "Not a return stmt!");
1842 bool IsCoreturn = Tok.is(tok::kw_co_return);
Chris Lattneraf635312006-10-16 06:06:51 +00001843 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001844
John McCalldadc5752010-08-24 06:29:42 +00001845 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001846 if (Tok.isNot(tok::semi)) {
Richard Smith0e304ea2015-10-22 04:46:14 +00001847 // FIXME: Code completion for co_return.
1848 if (Tok.is(tok::code_completion) && !IsCoreturn) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001849 Actions.CodeCompleteReturn(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001850 cutOffParsing();
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001851 return StmtError();
1852 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001853
David Blaikiebbafb8a2012-03-11 07:00:24 +00001854 if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus) {
Douglas Gregore9e27d92011-03-11 23:10:44 +00001855 R = ParseInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00001856 if (R.isUsable())
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001857 Diag(R.get()->getLocStart(), getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00001858 diag::warn_cxx98_compat_generalized_initializer_lists :
1859 diag::ext_generalized_initializer_lists)
Douglas Gregore9e27d92011-03-11 23:10:44 +00001860 << R.get()->getSourceRange();
1861 } else
Nico Weber3ce01c32015-01-04 08:07:54 +00001862 R = ParseExpression();
Serge Pavlovf79bd5c2013-12-04 03:51:59 +00001863 if (R.isInvalid()) {
1864 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001865 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001866 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001867 }
Richard Smithcfd53b42015-10-22 06:13:50 +00001868 if (IsCoreturn)
1869 return Actions.ActOnCoreturnStmt(ReturnLoc, R.get());
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001870 return Actions.ActOnReturnStmt(ReturnLoc, R.get(), getCurScope());
Chris Lattner503fadc2006-08-10 05:45:44 +00001871}
Chris Lattner0116c472006-08-15 06:03:28 +00001872
Alexey Bataevc4fad652016-01-13 11:18:54 +00001873StmtResult Parser::ParsePragmaLoopHint(StmtVector &Stmts,
1874 AllowedContsructsKind Allowed,
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001875 SourceLocation *TrailingElseLoc,
1876 ParsedAttributesWithRange &Attrs) {
1877 // Create temporary attribute list.
1878 ParsedAttributesWithRange TempAttrs(AttrFactory);
1879
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001880 // Get loop hints and consume annotated token.
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001881 while (Tok.is(tok::annot_pragma_loop_hint)) {
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001882 LoopHint Hint;
1883 if (!HandlePragmaLoopHint(Hint))
1884 continue;
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001885
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001886 ArgsUnion ArgHints[] = {Hint.PragmaNameLoc, Hint.OptionLoc, Hint.StateLoc,
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001887 ArgsUnion(Hint.ValueExpr)};
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001888 TempAttrs.addNew(Hint.PragmaNameLoc->Ident, Hint.Range, nullptr,
1889 Hint.PragmaNameLoc->Loc, ArgHints, 4,
1890 AttributeList::AS_Pragma);
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001891 }
1892
1893 // Get the next statement.
1894 MaybeParseCXX11Attributes(Attrs);
1895
1896 StmtResult S = ParseStatementOrDeclarationAfterAttributes(
Alexey Bataevc4fad652016-01-13 11:18:54 +00001897 Stmts, Allowed, TrailingElseLoc, Attrs);
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001898
1899 Attrs.takeAllFrom(TempAttrs);
1900 return S;
1901}
1902
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001903Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001904 assert(Tok.is(tok::l_brace));
1905 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001906
John McCallfaf5fb42010-08-26 23:41:50 +00001907 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1908 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001909
Alexey Bataev3d42f342015-11-20 07:02:57 +00001910 // Save and reset current vtordisp stack if we have entered a C++ method body.
1911 bool IsCXXMethod =
1912 getLangOpts().CPlusPlus && Decl && isa<CXXMethodDecl>(Decl);
Denis Zobnin2290dac2016-04-29 11:27:00 +00001913 Sema::PragmaStackSentinelRAII
1914 PragmaStackSentinel(Actions, "InternalPragmaState", IsCXXMethod);
Alexey Bataev3d42f342015-11-20 07:02:57 +00001915
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001916 // Do not enter a scope for the brace, as the arguments are in the same scope
1917 // (the function body) as the body itself. Instead, just read the statement
1918 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00001919 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001920
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001921 // If the function body could not be parsed, make a bogus compoundstmt.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001922 if (FnBody.isInvalid()) {
1923 Sema::CompoundScopeRAII CompoundScope(Actions);
Robert Wilhelm27b2c9a32013-08-19 20:51:20 +00001924 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001925 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001926
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001927 BodyScope.Exit();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001928 return Actions.ActOnFinishFunctionBody(Decl, FnBody.get());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001929}
Sebastian Redlb219c902008-12-21 16:41:36 +00001930
Sebastian Redla7b98a72009-04-26 20:35:05 +00001931/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1932///
1933/// function-try-block:
1934/// 'try' ctor-initializer[opt] compound-statement handler-seq
1935///
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001936Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00001937 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1938 SourceLocation TryLoc = ConsumeToken();
1939
John McCallfaf5fb42010-08-26 23:41:50 +00001940 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1941 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00001942
1943 // Constructor initializer list?
1944 if (Tok.is(tok::colon))
1945 ParseConstructorInitializer(Decl);
Douglas Gregor5ca153f2011-09-07 20:36:12 +00001946 else
1947 Actions.ActOnDefaultCtorInitializers(Decl);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001948
Alexey Bataev3d42f342015-11-20 07:02:57 +00001949 // Save and reset current vtordisp stack if we have entered a C++ method body.
1950 bool IsCXXMethod =
1951 getLangOpts().CPlusPlus && Decl && isa<CXXMethodDecl>(Decl);
Denis Zobnin2290dac2016-04-29 11:27:00 +00001952 Sema::PragmaStackSentinelRAII
1953 PragmaStackSentinel(Actions, "InternalPragmaState", IsCXXMethod);
Alexey Bataev3d42f342015-11-20 07:02:57 +00001954
Sebastian Redld98ecd62009-04-26 21:08:36 +00001955 SourceLocation LBraceLoc = Tok.getLocation();
David Blaikie1c9c9042012-11-10 01:04:23 +00001956 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc, /*FnTry*/true));
Sebastian Redla7b98a72009-04-26 20:35:05 +00001957 // If we failed to parse the try-catch, we just give the function an empty
1958 // compound statement as the body.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001959 if (FnBody.isInvalid()) {
1960 Sema::CompoundScopeRAII CompoundScope(Actions);
Robert Wilhelm27b2c9a32013-08-19 20:51:20 +00001961 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001962 }
Sebastian Redla7b98a72009-04-26 20:35:05 +00001963
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001964 BodyScope.Exit();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001965 return Actions.ActOnFinishFunctionBody(Decl, FnBody.get());
Sebastian Redla7b98a72009-04-26 20:35:05 +00001966}
1967
Erik Verbruggen6e922512012-04-12 10:11:59 +00001968bool Parser::trySkippingFunctionBody() {
Erik Verbruggen6e922512012-04-12 10:11:59 +00001969 assert(SkipFunctionBodies &&
1970 "Should only be called when SkipFunctionBodies is enabled");
Argyrios Kyrtzidis289e4a32012-10-31 17:29:28 +00001971 if (!PP.isCodeCompletionEnabled()) {
Olivier Goffartf9e890c2016-06-16 21:40:06 +00001972 SkipFunctionBody();
Argyrios Kyrtzidis289e4a32012-10-31 17:29:28 +00001973 return true;
1974 }
1975
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001976 // We're in code-completion mode. Skip parsing for all function bodies unless
1977 // the body contains the code-completion point.
1978 TentativeParsingAction PA(*this);
Olivier Goffartf9e890c2016-06-16 21:40:06 +00001979 bool IsTryCatch = Tok.is(tok::kw_try);
1980 CachedTokens Toks;
1981 bool ErrorInPrologue = ConsumeAndStoreFunctionPrologue(Toks);
1982 if (llvm::any_of(Toks, [](const Token &Tok) {
1983 return Tok.is(tok::code_completion);
1984 })) {
1985 PA.Revert();
1986 return false;
1987 }
1988 if (ErrorInPrologue) {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001989 PA.Commit();
Olivier Goffartf9e890c2016-06-16 21:40:06 +00001990 SkipMalformedDecl();
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001991 return true;
1992 }
Olivier Goffartf9e890c2016-06-16 21:40:06 +00001993 if (!SkipUntil(tok::r_brace, StopAtCodeCompletion)) {
1994 PA.Revert();
1995 return false;
1996 }
1997 while (IsTryCatch && Tok.is(tok::kw_catch)) {
1998 if (!SkipUntil(tok::l_brace, StopAtCodeCompletion) ||
1999 !SkipUntil(tok::r_brace, StopAtCodeCompletion)) {
2000 PA.Revert();
2001 return false;
2002 }
2003 }
2004 PA.Commit();
2005 return true;
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002006}
2007
Sebastian Redlb219c902008-12-21 16:41:36 +00002008/// ParseCXXTryBlock - Parse a C++ try-block.
2009///
2010/// try-block:
2011/// 'try' compound-statement handler-seq
2012///
Richard Smithc202b282012-04-14 00:33:13 +00002013StmtResult Parser::ParseCXXTryBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00002014 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2015
2016 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00002017 return ParseCXXTryBlockCommon(TryLoc);
2018}
2019
2020/// ParseCXXTryBlockCommon - Parse the common part of try-block and
2021/// function-try-block.
2022///
2023/// try-block:
2024/// 'try' compound-statement handler-seq
2025///
2026/// function-try-block:
2027/// 'try' ctor-initializer[opt] compound-statement handler-seq
2028///
2029/// handler-seq:
2030/// handler handler-seq[opt]
2031///
John Wiegley1c0675e2011-04-28 01:08:34 +00002032/// [Borland] try-block:
2033/// 'try' compound-statement seh-except-block
Alp Tokerf6a24ce2013-12-05 16:25:25 +00002034/// 'try' compound-statement seh-finally-block
John Wiegley1c0675e2011-04-28 01:08:34 +00002035///
David Blaikie1c9c9042012-11-10 01:04:23 +00002036StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc, bool FnTry) {
Sebastian Redlb219c902008-12-21 16:41:36 +00002037 if (Tok.isNot(tok::l_brace))
Alp Tokerec543272013-12-24 09:48:30 +00002038 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
Richard Smithc202b282012-04-14 00:33:13 +00002039
Warren Huntf6be4cb2014-07-25 20:52:51 +00002040 StmtResult TryBlock(ParseCompoundStatement(/*isStmtExpr=*/false,
2041 Scope::DeclScope | Scope::TryScope |
2042 (FnTry ? Scope::FnTryCatchScope : 0)));
Sebastian Redlb219c902008-12-21 16:41:36 +00002043 if (TryBlock.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002044 return TryBlock;
Sebastian Redlb219c902008-12-21 16:41:36 +00002045
John Wiegley1c0675e2011-04-28 01:08:34 +00002046 // Borland allows SEH-handlers with 'try'
Chad Rosier67055f52012-07-10 21:35:27 +00002047
Richard Smithc202b282012-04-14 00:33:13 +00002048 if ((Tok.is(tok::identifier) &&
2049 Tok.getIdentifierInfo() == getSEHExceptKeyword()) ||
2050 Tok.is(tok::kw___finally)) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002051 // TODO: Factor into common return ParseSEHHandlerCommon(...)
2052 StmtResult Handler;
Douglas Gregor60060d62011-10-21 03:57:52 +00002053 if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002054 SourceLocation Loc = ConsumeToken();
2055 Handler = ParseSEHExceptBlock(Loc);
2056 }
2057 else {
2058 SourceLocation Loc = ConsumeToken();
2059 Handler = ParseSEHFinallyBlock(Loc);
2060 }
2061 if(Handler.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002062 return Handler;
John McCall53fa7142010-12-24 02:08:15 +00002063
John Wiegley1c0675e2011-04-28 01:08:34 +00002064 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
2065 TryLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002066 TryBlock.get(),
Warren Huntf6be4cb2014-07-25 20:52:51 +00002067 Handler.get());
Sebastian Redlb219c902008-12-21 16:41:36 +00002068 }
John Wiegley1c0675e2011-04-28 01:08:34 +00002069 else {
Benjamin Kramerf0623432012-08-23 22:51:59 +00002070 StmtVector Handlers;
Richard Smithc2c8bb82013-10-15 01:34:54 +00002071
2072 // C++11 attributes can't appear here, despite this context seeming
2073 // statement-like.
2074 DiagnoseAndSkipCXX11Attributes();
Sebastian Redlb219c902008-12-21 16:41:36 +00002075
John Wiegley1c0675e2011-04-28 01:08:34 +00002076 if (Tok.isNot(tok::kw_catch))
2077 return StmtError(Diag(Tok, diag::err_expected_catch));
2078 while (Tok.is(tok::kw_catch)) {
David Blaikie1c9c9042012-11-10 01:04:23 +00002079 StmtResult Handler(ParseCXXCatchBlock(FnTry));
John Wiegley1c0675e2011-04-28 01:08:34 +00002080 if (!Handler.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002081 Handlers.push_back(Handler.get());
John Wiegley1c0675e2011-04-28 01:08:34 +00002082 }
2083 // Don't bother creating the full statement if we don't have any usable
2084 // handlers.
2085 if (Handlers.empty())
2086 return StmtError();
2087
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002088 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.get(), Handlers);
John Wiegley1c0675e2011-04-28 01:08:34 +00002089 }
Sebastian Redlb219c902008-12-21 16:41:36 +00002090}
2091
2092/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
2093///
Richard Smith1dba27c2013-01-29 09:02:09 +00002094/// handler:
2095/// 'catch' '(' exception-declaration ')' compound-statement
Sebastian Redlb219c902008-12-21 16:41:36 +00002096///
Richard Smith1dba27c2013-01-29 09:02:09 +00002097/// exception-declaration:
2098/// attribute-specifier-seq[opt] type-specifier-seq declarator
2099/// attribute-specifier-seq[opt] type-specifier-seq abstract-declarator[opt]
2100/// '...'
Sebastian Redlb219c902008-12-21 16:41:36 +00002101///
David Blaikie1c9c9042012-11-10 01:04:23 +00002102StmtResult Parser::ParseCXXCatchBlock(bool FnCatch) {
Sebastian Redlb219c902008-12-21 16:41:36 +00002103 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
2104
2105 SourceLocation CatchLoc = ConsumeToken();
2106
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002107 BalancedDelimiterTracker T(*this, tok::l_paren);
Alp Toker383d2c42014-01-01 03:08:43 +00002108 if (T.expectAndConsume())
Sebastian Redlb219c902008-12-21 16:41:36 +00002109 return StmtError();
2110
2111 // C++ 3.3.2p3:
2112 // The name in a catch exception-declaration is local to the handler and
2113 // shall not be redeclared in the outermost block of the handler.
David Blaikie1c9c9042012-11-10 01:04:23 +00002114 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope |
David Blaikie3403feb2012-11-13 18:51:45 +00002115 (FnCatch ? Scope::FnTryCatchScope : 0));
Sebastian Redlb219c902008-12-21 16:41:36 +00002116
2117 // exception-declaration is equivalent to '...' or a parameter-declaration
2118 // without default arguments.
Craig Topper161e4db2014-05-21 06:02:52 +00002119 Decl *ExceptionDecl = nullptr;
Sebastian Redlb219c902008-12-21 16:41:36 +00002120 if (Tok.isNot(tok::ellipsis)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002121 ParsedAttributesWithRange Attributes(AttrFactory);
2122 MaybeParseCXX11Attributes(Attributes);
2123
John McCall084e83d2011-03-24 11:26:52 +00002124 DeclSpec DS(AttrFactory);
Richard Smith1dba27c2013-01-29 09:02:09 +00002125 DS.takeAttributesFrom(Attributes);
2126
Sebastian Redl54c04d42008-12-22 19:15:10 +00002127 if (ParseCXXTypeSpecifierSeq(DS))
2128 return StmtError();
Richard Smith1dba27c2013-01-29 09:02:09 +00002129
Sebastian Redlb219c902008-12-21 16:41:36 +00002130 Declarator ExDecl(DS, Declarator::CXXCatchContext);
2131 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002132 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00002133 } else
2134 ConsumeToken();
2135
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002136 T.consumeClose();
2137 if (T.getCloseLocation().isInvalid())
Sebastian Redlb219c902008-12-21 16:41:36 +00002138 return StmtError();
2139
2140 if (Tok.isNot(tok::l_brace))
Alp Tokerec543272013-12-24 09:48:30 +00002141 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
Sebastian Redlb219c902008-12-21 16:41:36 +00002142
Alexis Hunt96d5c762009-11-21 08:43:09 +00002143 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
Richard Smithc202b282012-04-14 00:33:13 +00002144 StmtResult Block(ParseCompoundStatement());
Sebastian Redlb219c902008-12-21 16:41:36 +00002145 if (Block.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002146 return Block;
Sebastian Redlb219c902008-12-21 16:41:36 +00002147
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002148 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.get());
Sebastian Redlb219c902008-12-21 16:41:36 +00002149}
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002150
2151void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Douglas Gregor43edb322011-10-24 22:31:10 +00002152 IfExistsCondition Result;
Francois Picheta5b3fcb2011-05-07 17:30:27 +00002153 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002154 return;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00002155
Douglas Gregor43edb322011-10-24 22:31:10 +00002156 // Handle dependent statements by parsing the braces as a compound statement.
2157 // This is not the same behavior as Visual C++, which don't treat this as a
2158 // compound statement, but for Clang's type checking we can't have anything
2159 // inside these braces escaping to the surrounding code.
2160 if (Result.Behavior == IEB_Dependent) {
2161 if (!Tok.is(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002162 Diag(Tok, diag::err_expected) << tok::l_brace;
Richard Smithc202b282012-04-14 00:33:13 +00002163 return;
Douglas Gregor43edb322011-10-24 22:31:10 +00002164 }
Richard Smithc202b282012-04-14 00:33:13 +00002165
2166 StmtResult Compound = ParseCompoundStatement();
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002167 if (Compound.isInvalid())
2168 return;
Richard Smithc202b282012-04-14 00:33:13 +00002169
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002170 StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc,
2171 Result.IsIfExists,
Richard Smithc202b282012-04-14 00:33:13 +00002172 Result.SS,
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002173 Result.Name,
2174 Compound.get());
2175 if (DepResult.isUsable())
2176 Stmts.push_back(DepResult.get());
Douglas Gregor43edb322011-10-24 22:31:10 +00002177 return;
2178 }
Richard Smithc202b282012-04-14 00:33:13 +00002179
Douglas Gregor43edb322011-10-24 22:31:10 +00002180 BalancedDelimiterTracker Braces(*this, tok::l_brace);
2181 if (Braces.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +00002182 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002183 return;
2184 }
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002185
Douglas Gregor43edb322011-10-24 22:31:10 +00002186 switch (Result.Behavior) {
2187 case IEB_Parse:
2188 // Parse the statements below.
2189 break;
Chad Rosier67055f52012-07-10 21:35:27 +00002190
Douglas Gregor43edb322011-10-24 22:31:10 +00002191 case IEB_Dependent:
2192 llvm_unreachable("Dependent case handled above");
Chad Rosier67055f52012-07-10 21:35:27 +00002193
Douglas Gregor43edb322011-10-24 22:31:10 +00002194 case IEB_Skip:
2195 Braces.skipToEnd();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002196 return;
2197 }
2198
2199 // Condition is true, parse the statements.
2200 while (Tok.isNot(tok::r_brace)) {
Alexey Bataevc4fad652016-01-13 11:18:54 +00002201 StmtResult R = ParseStatementOrDeclaration(Stmts, ACK_Any);
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002202 if (R.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002203 Stmts.push_back(R.get());
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002204 }
Douglas Gregor43edb322011-10-24 22:31:10 +00002205 Braces.consumeClose();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002206}
Anastasia Stulova6bdbcbb2016-02-19 18:30:11 +00002207
2208bool Parser::ParseOpenCLUnrollHintAttribute(ParsedAttributes &Attrs) {
2209 MaybeParseGNUAttributes(Attrs);
2210
2211 if (Attrs.empty())
2212 return true;
2213
2214 if (Attrs.getList()->getKind() != AttributeList::AT_OpenCLUnrollHint)
2215 return true;
2216
2217 if (!(Tok.is(tok::kw_for) || Tok.is(tok::kw_while) || Tok.is(tok::kw_do))) {
2218 Diag(Tok, diag::err_opencl_unroll_hint_on_non_loop);
2219 return false;
2220 }
2221 return true;
2222}