blob: c55dbea7d9e76e6b890794b6214b8eeb27c60880 [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').
35StmtResult Parser::ParseStatement(SourceLocation *TrailingElseLoc) {
36 StmtResult Res;
37
38 // We may get back a null statement if we found a #pragma. Keep going until
39 // we get an actual statement.
40 do {
41 StmtVector Stmts;
42 Res = ParseStatementOrDeclaration(Stmts, true, TrailingElseLoc);
43 } while (!Res.isInvalid() && !Res.get());
44
45 return Res;
46}
47
Chris Lattner0ccd51e2006-08-09 05:47:47 +000048/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
49/// StatementOrDeclaration:
50/// statement
51/// declaration
52///
53/// statement:
54/// labeled-statement
55/// compound-statement
56/// expression-statement
57/// selection-statement
58/// iteration-statement
59/// jump-statement
Argyrios Kyrtzidisdee82912008-09-07 18:58:01 +000060/// [C++] declaration-statement
Sebastian Redlb219c902008-12-21 16:41:36 +000061/// [C++] try-block
John Wiegley1c0675e2011-04-28 01:08:34 +000062/// [MS] seh-try-block
Fariborz Jahanian90814572007-10-04 20:19:06 +000063/// [OBC] objc-throw-statement
64/// [OBC] objc-try-catch-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +000065/// [OBC] objc-synchronized-statement
Chris Lattner0116c472006-08-15 06:03:28 +000066/// [GNU] asm-statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +000067/// [OMP] openmp-construct [TODO]
68///
69/// labeled-statement:
70/// identifier ':' statement
71/// 'case' constant-expression ':' statement
72/// 'default' ':' statement
73///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000074/// selection-statement:
75/// if-statement
76/// switch-statement
77///
78/// iteration-statement:
79/// while-statement
80/// do-statement
81/// for-statement
82///
Chris Lattner9075bd72006-08-10 04:59:57 +000083/// expression-statement:
84/// expression[opt] ';'
85///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000086/// jump-statement:
87/// 'goto' identifier ';'
88/// 'continue' ';'
89/// 'break' ';'
90/// 'return' expression[opt] ';'
Chris Lattner503fadc2006-08-10 05:45:44 +000091/// [GNU] 'goto' '*' expression ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000092///
Fariborz Jahanian90814572007-10-04 20:19:06 +000093/// [OBC] objc-throw-statement:
94/// [OBC] '@' 'throw' expression ';'
Mike Stump11289f42009-09-09 15:08:12 +000095/// [OBC] '@' 'throw' ';'
96///
John McCalldadc5752010-08-24 06:29:42 +000097StmtResult
Nico Weber3cef1082011-12-22 23:26:17 +000098Parser::ParseStatementOrDeclaration(StmtVector &Stmts, bool OnlyStatement,
99 SourceLocation *TrailingElseLoc) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000100
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000101 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000102
Richard Smithc202b282012-04-14 00:33:13 +0000103 ParsedAttributesWithRange Attrs(AttrFactory);
Craig Topper161e4db2014-05-21 06:02:52 +0000104 MaybeParseCXX11Attributes(Attrs, nullptr, /*MightBeObjCMessageSend*/ true);
Richard Smithc202b282012-04-14 00:33:13 +0000105
106 StmtResult Res = ParseStatementOrDeclarationAfterAttributes(Stmts,
107 OnlyStatement, TrailingElseLoc, Attrs);
108
109 assert((Attrs.empty() || Res.isInvalid() || Res.isUsable()) &&
110 "attributes on empty statement");
111
112 if (Attrs.empty() || Res.isInvalid())
113 return Res;
114
115 return Actions.ProcessStmtAttributes(Res.get(), Attrs.getList(), Attrs.Range);
116}
117
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000118namespace {
119class StatementFilterCCC : public CorrectionCandidateCallback {
120public:
121 StatementFilterCCC(Token nextTok) : NextToken(nextTok) {
122 WantTypeSpecifiers = nextTok.is(tok::l_paren) || nextTok.is(tok::less) ||
123 nextTok.is(tok::identifier) || nextTok.is(tok::star) ||
124 nextTok.is(tok::amp) || nextTok.is(tok::l_square);
125 WantExpressionKeywords = nextTok.is(tok::l_paren) ||
126 nextTok.is(tok::identifier) ||
127 nextTok.is(tok::arrow) || nextTok.is(tok::period);
128 WantRemainingKeywords = nextTok.is(tok::l_paren) || nextTok.is(tok::semi) ||
129 nextTok.is(tok::identifier) ||
130 nextTok.is(tok::l_brace);
131 WantCXXNamedCasts = false;
132 }
133
Craig Topper2b07f022014-03-12 05:09:18 +0000134 bool ValidateCandidate(const TypoCorrection &candidate) override {
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000135 if (FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>())
Kaelyn Uhrain07e62722013-10-01 22:00:28 +0000136 return !candidate.getCorrectionSpecifier() || isa<ObjCIvarDecl>(FD);
Kaelyn Uhrain46b6cdc2013-09-27 19:40:16 +0000137 if (NextToken.is(tok::equal))
138 return candidate.getCorrectionDeclAs<VarDecl>();
Kaelyn Uhrain30943ce2013-09-27 23:54:23 +0000139 if (NextToken.is(tok::period) &&
140 candidate.getCorrectionDeclAs<NamespaceDecl>())
141 return false;
Kaelyn Uhrain3dfff192013-09-27 19:40:12 +0000142 return CorrectionCandidateCallback::ValidateCandidate(candidate);
143 }
144
145private:
146 Token NextToken;
147};
148}
149
Richard Smithc202b282012-04-14 00:33:13 +0000150StmtResult
151Parser::ParseStatementOrDeclarationAfterAttributes(StmtVector &Stmts,
152 bool OnlyStatement, SourceLocation *TrailingElseLoc,
153 ParsedAttributesWithRange &Attrs) {
Craig Topper161e4db2014-05-21 06:02:52 +0000154 const char *SemiError = nullptr;
Richard Smithc202b282012-04-14 00:33:13 +0000155 StmtResult Res;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000156
Chris Lattner503fadc2006-08-10 05:45:44 +0000157 // Cases in this switch statement should fall through if the parser expects
158 // the token to end in a semicolon (in which case SemiError should be set),
159 // or they directly 'return;' if not.
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000160Retry:
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000161 tok::TokenKind Kind = Tok.getKind();
162 SourceLocation AtLoc;
163 switch (Kind) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000164 case tok::at: // May be a @try or @throw statement
165 {
Richard Smithc202b282012-04-14 00:33:13 +0000166 ProhibitAttributes(Attrs); // TODO: is it correct?
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000167 AtLoc = ConsumeToken(); // consume @
Sebastian Redlbab9a4b2008-12-11 20:12:42 +0000168 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000169 }
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000170
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000171 case tok::code_completion:
John McCallfaf5fb42010-08-26 23:41:50 +0000172 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000173 cutOffParsing();
174 return StmtError();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000175
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000176 case tok::identifier: {
177 Token Next = NextToken();
178 if (Next.is(tok::colon)) { // C99 6.8.1: labeled-statement
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000179 // identifier ':' statement
Richard Smithc202b282012-04-14 00:33:13 +0000180 return ParseLabeledStatement(Attrs);
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000181 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000182
Richard Smith4f605af2012-08-18 00:55:03 +0000183 // Look up the identifier, and typo-correct it to a keyword if it's not
184 // found.
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000185 if (Next.isNot(tok::coloncolon)) {
Richard Smith4f605af2012-08-18 00:55:03 +0000186 // Try to limit which sets of keywords should be included in typo
187 // correction based on what the next token is.
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000188 if (TryAnnotateName(/*IsAddressOfOperand*/ false,
189 llvm::make_unique<StatementFilterCCC>(Next)) ==
190 ANK_Error) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000191 // Handle errors here by skipping up to the next semicolon or '}', and
192 // eat the semicolon if that's what stopped us.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000193 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000194 if (Tok.is(tok::semi))
195 ConsumeToken();
196 return StmtError();
Richard Smith4f605af2012-08-18 00:55:03 +0000197 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000198
Richard Smith4f605af2012-08-18 00:55:03 +0000199 // If the identifier was typo-corrected, try again.
200 if (Tok.isNot(tok::identifier))
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000201 goto Retry;
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000202 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000203
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000204 // Fall through
205 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000206
Chris Lattner803802d2009-03-24 17:04:48 +0000207 default: {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000208 if ((getLangOpts().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner49836b42009-04-02 04:16:50 +0000209 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Rafael Espindola1bd906d2014-10-22 14:27:08 +0000210 DeclGroupPtrTy Decl = ParseDeclaration(Declarator::BlockContext,
Richard Smithc202b282012-04-14 00:33:13 +0000211 DeclEnd, Attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000212 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattner803802d2009-03-24 17:04:48 +0000213 }
214
215 if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +0000216 Diag(Tok, diag::err_expected_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000217 return StmtError();
Chris Lattnerf8afb622006-08-10 18:26:31 +0000218 }
Mike Stump11289f42009-09-09 15:08:12 +0000219
Richard Smithc202b282012-04-14 00:33:13 +0000220 return ParseExprStatement();
Chris Lattner803802d2009-03-24 17:04:48 +0000221 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000222
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000223 case tok::kw_case: // C99 6.8.1: labeled-statement
Richard Smithc202b282012-04-14 00:33:13 +0000224 return ParseCaseStatement();
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000225 case tok::kw_default: // C99 6.8.1: labeled-statement
Richard Smithc202b282012-04-14 00:33:13 +0000226 return ParseDefaultStatement();
Sebastian Redl042ad952008-12-11 19:30:53 +0000227
Chris Lattner9075bd72006-08-10 04:59:57 +0000228 case tok::l_brace: // C99 6.8.2: compound-statement
Richard Smithc202b282012-04-14 00:33:13 +0000229 return ParseCompoundStatement();
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000230 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
Argyrios Kyrtzidis43ea78b2011-09-01 21:53:45 +0000231 bool HasLeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
232 return Actions.ActOnNullStmt(ConsumeToken(), HasLeadingEmptyMacro);
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000233 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000234
Chris Lattner9075bd72006-08-10 04:59:57 +0000235 case tok::kw_if: // C99 6.8.4.1: if-statement
Richard Smithc202b282012-04-14 00:33:13 +0000236 return ParseIfStatement(TrailingElseLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000237 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Richard Smithc202b282012-04-14 00:33:13 +0000238 return ParseSwitchStatement(TrailingElseLoc);
Sebastian Redl042ad952008-12-11 19:30:53 +0000239
Chris Lattner9075bd72006-08-10 04:59:57 +0000240 case tok::kw_while: // C99 6.8.5.1: while-statement
Richard Smithc202b282012-04-14 00:33:13 +0000241 return ParseWhileStatement(TrailingElseLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000242 case tok::kw_do: // C99 6.8.5.2: do-statement
Richard Smithc202b282012-04-14 00:33:13 +0000243 Res = ParseDoStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000244 SemiError = "do/while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000245 break;
246 case tok::kw_for: // C99 6.8.5.3: for-statement
Richard Smithc202b282012-04-14 00:33:13 +0000247 return ParseForStatement(TrailingElseLoc);
Chris Lattner503fadc2006-08-10 05:45:44 +0000248
249 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Richard Smithc202b282012-04-14 00:33:13 +0000250 Res = ParseGotoStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000251 SemiError = "goto";
Chris Lattner9075bd72006-08-10 04:59:57 +0000252 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000253 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Richard Smithc202b282012-04-14 00:33:13 +0000254 Res = ParseContinueStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000255 SemiError = "continue";
Chris Lattner503fadc2006-08-10 05:45:44 +0000256 break;
257 case tok::kw_break: // C99 6.8.6.3: break-statement
Richard Smithc202b282012-04-14 00:33:13 +0000258 Res = ParseBreakStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000259 SemiError = "break";
Chris Lattner503fadc2006-08-10 05:45:44 +0000260 break;
261 case tok::kw_return: // C99 6.8.6.4: return-statement
Richard Smithc202b282012-04-14 00:33:13 +0000262 Res = ParseReturnStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000263 SemiError = "return";
Chris Lattner503fadc2006-08-10 05:45:44 +0000264 break;
Sebastian Redl042ad952008-12-11 19:30:53 +0000265
Sebastian Redlb219c902008-12-21 16:41:36 +0000266 case tok::kw_asm: {
Richard Smithc202b282012-04-14 00:33:13 +0000267 ProhibitAttributes(Attrs);
Steve Naroffb2c80c72008-02-07 03:50:06 +0000268 bool msAsm = false;
269 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidis3050d9b2010-11-02 02:33:08 +0000270 Res = Actions.ActOnFinishFullStmt(Res.get());
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000271 if (msAsm) return Res;
Chris Lattner34a95662009-06-14 00:07:48 +0000272 SemiError = "asm";
Chris Lattner0116c472006-08-15 06:03:28 +0000273 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000274 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000275
Reid Kleckner6d8d22a2014-06-25 00:28:35 +0000276 case tok::kw___if_exists:
277 case tok::kw___if_not_exists:
278 ProhibitAttributes(Attrs);
279 ParseMicrosoftIfExistsStatement(Stmts);
280 // An __if_exists block is like a compound statement, but it doesn't create
281 // a new scope.
282 return StmtEmpty();
283
Sebastian Redlb219c902008-12-21 16:41:36 +0000284 case tok::kw_try: // C++ 15: try-block
Richard Smithc202b282012-04-14 00:33:13 +0000285 return ParseCXXTryBlock();
John Wiegley1c0675e2011-04-28 01:08:34 +0000286
287 case tok::kw___try:
Richard Smithc202b282012-04-14 00:33:13 +0000288 ProhibitAttributes(Attrs); // TODO: is it correct?
289 return ParseSEHTryBlock();
Eli Friedmanec52f922012-02-23 23:47:16 +0000290
Nico Weberc7d05962014-07-06 22:32:59 +0000291 case tok::kw___leave:
292 Res = ParseSEHLeaveStatement();
293 SemiError = "__leave";
294 break;
295
Eli Friedmanec52f922012-02-23 23:47:16 +0000296 case tok::annot_pragma_vis:
Richard Smithc202b282012-04-14 00:33:13 +0000297 ProhibitAttributes(Attrs);
Eli Friedmanec52f922012-02-23 23:47:16 +0000298 HandlePragmaVisibility();
299 return StmtEmpty();
300
301 case tok::annot_pragma_pack:
Richard Smithc202b282012-04-14 00:33:13 +0000302 ProhibitAttributes(Attrs);
Eli Friedmanec52f922012-02-23 23:47:16 +0000303 HandlePragmaPack();
304 return StmtEmpty();
Eli Friedman68be1642012-10-04 02:36:51 +0000305
Eli Friedmanbbbbac62012-10-09 22:46:54 +0000306 case tok::annot_pragma_msstruct:
307 ProhibitAttributes(Attrs);
308 HandlePragmaMSStruct();
309 return StmtEmpty();
310
Eli Friedmanae8ee252012-10-08 23:52:38 +0000311 case tok::annot_pragma_align:
312 ProhibitAttributes(Attrs);
313 HandlePragmaAlign();
314 return StmtEmpty();
315
Eli Friedmanbbbbac62012-10-09 22:46:54 +0000316 case tok::annot_pragma_weak:
317 ProhibitAttributes(Attrs);
318 HandlePragmaWeak();
319 return StmtEmpty();
320
321 case tok::annot_pragma_weakalias:
322 ProhibitAttributes(Attrs);
323 HandlePragmaWeakAlias();
324 return StmtEmpty();
325
326 case tok::annot_pragma_redefine_extname:
327 ProhibitAttributes(Attrs);
328 HandlePragmaRedefineExtname();
329 return StmtEmpty();
330
Eli Friedman68be1642012-10-04 02:36:51 +0000331 case tok::annot_pragma_fp_contract:
Richard Smithca9b0b62013-11-15 21:10:54 +0000332 ProhibitAttributes(Attrs);
Lang Hamesa930e712012-10-21 01:10:01 +0000333 Diag(Tok, diag::err_pragma_fp_contract_scope);
334 ConsumeToken();
335 return StmtError();
336
Eli Friedman68be1642012-10-04 02:36:51 +0000337 case tok::annot_pragma_opencl_extension:
338 ProhibitAttributes(Attrs);
339 HandlePragmaOpenCLExtension();
340 return StmtEmpty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000341
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000342 case tok::annot_pragma_captured:
Richard Smith12a41bd2013-09-16 21:17:44 +0000343 ProhibitAttributes(Attrs);
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000344 return HandlePragmaCaptured();
345
Alexey Bataeva769e072013-03-22 06:34:35 +0000346 case tok::annot_pragma_openmp:
Richard Smith12a41bd2013-09-16 21:17:44 +0000347 ProhibitAttributes(Attrs);
Alexey Bataev68446b72014-07-18 07:47:19 +0000348 return ParseOpenMPDeclarativeOrExecutableDirective(!OnlyStatement);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000349
David Majnemer4bb09802014-02-10 19:50:15 +0000350 case tok::annot_pragma_ms_pointers_to_members:
351 ProhibitAttributes(Attrs);
352 HandlePragmaMSPointersToMembers();
353 return StmtEmpty();
354
Warren Huntc3b18962014-04-08 22:30:47 +0000355 case tok::annot_pragma_ms_pragma:
356 ProhibitAttributes(Attrs);
357 HandlePragmaMSPragma();
358 return StmtEmpty();
Aaron Ballmanb06b15a2014-06-06 12:40:24 +0000359
360 case tok::annot_pragma_loop_hint:
361 ProhibitAttributes(Attrs);
362 return ParsePragmaLoopHint(Stmts, OnlyStatement, TrailingElseLoc, Attrs);
Sebastian Redlb219c902008-12-21 16:41:36 +0000363 }
364
Chris Lattner503fadc2006-08-10 05:45:44 +0000365 // If we reached this code, the statement must end in a semicolon.
Alp Toker97650562014-01-10 11:19:30 +0000366 if (!TryConsumeToken(tok::semi) && !Res.isInvalid()) {
Chris Lattner8e3eed02009-06-14 00:23:56 +0000367 // If the result was valid, then we do want to diagnose this. Use
368 // ExpectAndConsume to emit the diagnostic, even though we know it won't
369 // succeed.
370 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner0046de12008-11-13 18:52:53 +0000371 // Skip until we see a } or ;, but don't eat it.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000372 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Chris Lattner503fadc2006-08-10 05:45:44 +0000373 }
Mike Stump11289f42009-09-09 15:08:12 +0000374
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000375 return Res;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000376}
377
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000378/// \brief Parse an expression statement.
Richard Smithc202b282012-04-14 00:33:13 +0000379StmtResult Parser::ParseExprStatement() {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000380 // If a case keyword is missing, this is where it should be inserted.
381 Token OldToken = Tok;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000382
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000383 // expression[opt] ';'
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000384 ExprResult Expr(ParseExpression());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000385 if (Expr.isInvalid()) {
386 // If the expression is invalid, skip ahead to the next semicolon or '}'.
387 // Not doing this opens us up to the possibility of infinite loops if
388 // ParseExpression does not consume any tokens.
Alexey Bataevee6507d2013-11-18 08:17:37 +0000389 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000390 if (Tok.is(tok::semi))
391 ConsumeToken();
John McCalleaef89b2013-03-22 02:10:40 +0000392 return Actions.ActOnExprStmtError();
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000393 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000394
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000395 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
396 Actions.CheckCaseExpression(Expr.get())) {
397 // If a constant expression is followed by a colon inside a switch block,
398 // suggest a missing case keyword.
399 Diag(OldToken, diag::err_expected_case_before_expression)
400 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000401
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000402 // Recover parsing as a case statement.
Richard Smithc202b282012-04-14 00:33:13 +0000403 return ParseCaseStatement(/*MissingCase=*/true, Expr);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000404 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000405
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000406 // Otherwise, eat the semicolon.
407 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +0000408 return Actions.ActOnExprStmt(Expr);
John Wiegley1c0675e2011-04-28 01:08:34 +0000409}
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000410
Richard Smithc202b282012-04-14 00:33:13 +0000411StmtResult Parser::ParseSEHTryBlock() {
John Wiegley1c0675e2011-04-28 01:08:34 +0000412 assert(Tok.is(tok::kw___try) && "Expected '__try'");
413 SourceLocation Loc = ConsumeToken();
414 return ParseSEHTryBlockCommon(Loc);
415}
416
417/// ParseSEHTryBlockCommon
418///
419/// seh-try-block:
420/// '__try' compound-statement seh-handler
421///
422/// seh-handler:
423/// seh-except-block
424/// seh-finally-block
425///
426StmtResult Parser::ParseSEHTryBlockCommon(SourceLocation TryLoc) {
Warren Huntf6be4cb2014-07-25 20:52:51 +0000427 if(Tok.isNot(tok::l_brace))
Alp Tokerec543272013-12-24 09:48:30 +0000428 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
John Wiegley1c0675e2011-04-28 01:08:34 +0000429
Warren Huntf6be4cb2014-07-25 20:52:51 +0000430 StmtResult TryBlock(ParseCompoundStatement(/*isStmtExpr=*/false,
431 Scope::DeclScope | Scope::SEHTryScope));
John Wiegley1c0675e2011-04-28 01:08:34 +0000432 if(TryBlock.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000433 return TryBlock;
John Wiegley1c0675e2011-04-28 01:08:34 +0000434
435 StmtResult Handler;
Richard Smithc202b282012-04-14 00:33:13 +0000436 if (Tok.is(tok::identifier) &&
Douglas Gregor60060d62011-10-21 03:57:52 +0000437 Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +0000438 SourceLocation Loc = ConsumeToken();
439 Handler = ParseSEHExceptBlock(Loc);
440 } else if (Tok.is(tok::kw___finally)) {
441 SourceLocation Loc = ConsumeToken();
442 Handler = ParseSEHFinallyBlock(Loc);
443 } else {
444 return StmtError(Diag(Tok,diag::err_seh_expected_handler));
445 }
446
447 if(Handler.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000448 return Handler;
John Wiegley1c0675e2011-04-28 01:08:34 +0000449
450 return Actions.ActOnSEHTryBlock(false /* IsCXXTry */,
451 TryLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000452 TryBlock.get(),
Warren Huntf6be4cb2014-07-25 20:52:51 +0000453 Handler.get());
John Wiegley1c0675e2011-04-28 01:08:34 +0000454}
455
456/// ParseSEHExceptBlock - Handle __except
457///
458/// seh-except-block:
459/// '__except' '(' seh-filter-expression ')' compound-statement
460///
461StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) {
462 PoisonIdentifierRAIIObject raii(Ident__exception_code, false),
463 raii2(Ident___exception_code, false),
464 raii3(Ident_GetExceptionCode, false);
465
Alp Toker383d2c42014-01-01 03:08:43 +0000466 if (ExpectAndConsume(tok::l_paren))
John Wiegley1c0675e2011-04-28 01:08:34 +0000467 return StmtError();
468
Reid Kleckner1d59f992015-01-22 01:36:17 +0000469 ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope |
470 Scope::SEHExceptScope);
John Wiegley1c0675e2011-04-28 01:08:34 +0000471
David Blaikiebbafb8a2012-03-11 07:00:24 +0000472 if (getLangOpts().Borland) {
Francois Pichetbfaf4772011-04-28 03:14:31 +0000473 Ident__exception_info->setIsPoisoned(false);
474 Ident___exception_info->setIsPoisoned(false);
475 Ident_GetExceptionInfo->setIsPoisoned(false);
476 }
Reid Kleckner1d59f992015-01-22 01:36:17 +0000477
478 ExprResult FilterExpr;
479 {
480 ParseScopeFlags FilterScope(this, getCurScope()->getFlags() |
481 Scope::SEHFilterScope);
482 FilterExpr = ParseExpression();
483 }
Francois Pichetbfaf4772011-04-28 03:14:31 +0000484
David Blaikiebbafb8a2012-03-11 07:00:24 +0000485 if (getLangOpts().Borland) {
Francois Pichetbfaf4772011-04-28 03:14:31 +0000486 Ident__exception_info->setIsPoisoned(true);
487 Ident___exception_info->setIsPoisoned(true);
488 Ident_GetExceptionInfo->setIsPoisoned(true);
489 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000490
491 if(FilterExpr.isInvalid())
492 return StmtError();
493
Alp Toker383d2c42014-01-01 03:08:43 +0000494 if (ExpectAndConsume(tok::r_paren))
John Wiegley1c0675e2011-04-28 01:08:34 +0000495 return StmtError();
496
Richard Smithc202b282012-04-14 00:33:13 +0000497 StmtResult Block(ParseCompoundStatement());
John Wiegley1c0675e2011-04-28 01:08:34 +0000498
499 if(Block.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000500 return Block;
John Wiegley1c0675e2011-04-28 01:08:34 +0000501
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000502 return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.get(), Block.get());
John Wiegley1c0675e2011-04-28 01:08:34 +0000503}
504
505/// ParseSEHFinallyBlock - Handle __finally
506///
507/// seh-finally-block:
508/// '__finally' compound-statement
509///
510StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyBlock) {
511 PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
512 raii2(Ident___abnormal_termination, false),
513 raii3(Ident_AbnormalTermination, false);
514
Richard Smithc202b282012-04-14 00:33:13 +0000515 StmtResult Block(ParseCompoundStatement());
John Wiegley1c0675e2011-04-28 01:08:34 +0000516 if(Block.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000517 return Block;
John Wiegley1c0675e2011-04-28 01:08:34 +0000518
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000519 return Actions.ActOnSEHFinallyBlock(FinallyBlock,Block.get());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000520}
521
Nico Weberc7d05962014-07-06 22:32:59 +0000522/// Handle __leave
523///
524/// seh-leave-statement:
525/// '__leave' ';'
526///
527StmtResult Parser::ParseSEHLeaveStatement() {
528 SourceLocation LeaveLoc = ConsumeToken(); // eat the '__leave'.
529 return Actions.ActOnSEHLeaveStmt(LeaveLoc, getCurScope());
530}
531
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000532/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000533///
534/// labeled-statement:
535/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000536/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000537///
Richard Smithc202b282012-04-14 00:33:13 +0000538StmtResult Parser::ParseLabeledStatement(ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000539 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
540 "Not an identifier!");
541
542 Token IdentTok = Tok; // Save the whole token.
543 ConsumeToken(); // eat the identifier.
544
545 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000546
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000547 // identifier ':' statement
548 SourceLocation ColonLoc = ConsumeToken();
549
Richard Smitha3e01cf2013-11-15 22:45:29 +0000550 // Read label attributes, if present.
551 StmtResult SubStmt;
552 if (Tok.is(tok::kw___attribute)) {
553 ParsedAttributesWithRange TempAttrs(AttrFactory);
554 ParseGNUAttributes(TempAttrs);
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000555
Richard Smitha3e01cf2013-11-15 22:45:29 +0000556 // In C++, GNU attributes only apply to the label if they are followed by a
557 // semicolon, to disambiguate label attributes from attributes on a labeled
558 // declaration.
559 //
560 // This doesn't quite match what GCC does; if the attribute list is empty
561 // and followed by a semicolon, GCC will reject (it appears to parse the
562 // attributes as part of a statement in that case). That looks like a bug.
563 if (!getLangOpts().CPlusPlus || Tok.is(tok::semi))
564 attrs.takeAllFrom(TempAttrs);
565 else if (isDeclarationStatement()) {
566 StmtVector Stmts;
567 // FIXME: We should do this whether or not we have a declaration
568 // statement, but that doesn't work correctly (because ProhibitAttributes
569 // can't handle GNU attributes), so only call it in the one case where
570 // GNU attributes are allowed.
571 SubStmt = ParseStatementOrDeclarationAfterAttributes(
Craig Topper161e4db2014-05-21 06:02:52 +0000572 Stmts, /*OnlyStmts*/ true, nullptr, TempAttrs);
Richard Smitha3e01cf2013-11-15 22:45:29 +0000573 if (!TempAttrs.empty() && !SubStmt.isInvalid())
574 SubStmt = Actions.ProcessStmtAttributes(
575 SubStmt.get(), TempAttrs.getList(), TempAttrs.Range);
576 } else {
Alp Toker383d2c42014-01-01 03:08:43 +0000577 Diag(Tok, diag::err_expected_after) << "__attribute__" << tok::semi;
Richard Smitha3e01cf2013-11-15 22:45:29 +0000578 }
579 }
580
581 // If we've not parsed a statement yet, parse one now.
582 if (!SubStmt.isInvalid() && !SubStmt.isUsable())
583 SubStmt = ParseStatement();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000584
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000585 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000586 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000587 SubStmt = Actions.ActOnNullStmt(ColonLoc);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000588
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000589 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
590 IdentTok.getLocation());
Richard Smithc202b282012-04-14 00:33:13 +0000591 if (AttributeList *Attrs = attrs.getList()) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000592 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
Richard Smithc202b282012-04-14 00:33:13 +0000593 attrs.clear();
594 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000595
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000596 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
597 SubStmt.get());
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000598}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000599
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000600/// ParseCaseStatement
601/// labeled-statement:
602/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000603/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000604///
Richard Smithc202b282012-04-14 00:33:13 +0000605StmtResult Parser::ParseCaseStatement(bool MissingCase, ExprResult Expr) {
Richard Smithd4257d82011-04-21 22:48:40 +0000606 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Mike Stump11289f42009-09-09 15:08:12 +0000607
Chris Lattner34a22092009-03-04 04:23:07 +0000608 // It is very very common for code to contain many case statements recursively
609 // nested, as in (but usually without indentation):
610 // case 1:
611 // case 2:
612 // case 3:
613 // case 4:
614 // case 5: etc.
615 //
616 // Parsing this naively works, but is both inefficient and can cause us to run
617 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner2b19a6582009-03-04 18:24:58 +0000618 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner34a22092009-03-04 04:23:07 +0000619 // but all the grossness is constrained to ParseCaseStatement (and some
Richard Smitha3e01cf2013-11-15 22:45:29 +0000620 // weirdness in the actions), so this is just local grossness :).
Mike Stump11289f42009-09-09 15:08:12 +0000621
Chris Lattner34a22092009-03-04 04:23:07 +0000622 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
623 // example above.
John McCalldadc5752010-08-24 06:29:42 +0000624 StmtResult TopLevelCase(true);
Mike Stump11289f42009-09-09 15:08:12 +0000625
Chris Lattner34a22092009-03-04 04:23:07 +0000626 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
627 // gets updated each time a new case is parsed, and whose body is unset so
628 // far. When parsing 'case 4', this is the 'case 3' node.
Craig Topper161e4db2014-05-21 06:02:52 +0000629 Stmt *DeepestParsedCaseStmt = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000630
Chris Lattner34a22092009-03-04 04:23:07 +0000631 // While we have case statements, eat and stack them.
David Majnemer0ac67fa2011-06-13 05:50:12 +0000632 SourceLocation ColonLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000633 do {
Richard Trieu2c850c02011-04-21 21:44:26 +0000634 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
635 ConsumeToken(); // eat the 'case'.
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000636 ColonLoc = SourceLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000637
Douglas Gregord328d572009-09-21 18:10:23 +0000638 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000639 Actions.CodeCompleteCase(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000640 cutOffParsing();
641 return StmtError();
Douglas Gregord328d572009-09-21 18:10:23 +0000642 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000643
Chris Lattner125c0ee2009-12-10 00:38:54 +0000644 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
645 /// Disable this form of error recovery while we're parsing the case
646 /// expression.
647 ColonProtectionRAIIObject ColonProtection(*this);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000648
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000649 ExprResult LHS;
650 if (!MissingCase) {
651 LHS = ParseConstantExpression();
Kaelyn Takatab16e6322014-11-20 22:06:40 +0000652 if (!getLangOpts().CPlusPlus11) {
653 LHS = Actions.CorrectDelayedTyposInExpr(LHS, [this](class Expr *E) {
654 return Actions.VerifyIntegerConstantExpression(E);
655 });
656 }
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000657 if (LHS.isInvalid()) {
658 // If constant-expression is parsed unsuccessfully, recover by skipping
659 // current case statement (moving to the colon that ends it).
660 if (SkipUntil(tok::colon, tok::r_brace, StopAtSemi | StopBeforeMatch)) {
661 TryConsumeToken(tok::colon, ColonLoc);
662 continue;
663 }
664 return StmtError();
665 }
666 } else {
667 LHS = Expr;
668 MissingCase = false;
Chris Lattner476c3ad2006-08-13 22:09:58 +0000669 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000670
Chris Lattner34a22092009-03-04 04:23:07 +0000671 // GNU case range extension.
672 SourceLocation DotDotDotLoc;
John McCalldadc5752010-08-24 06:29:42 +0000673 ExprResult RHS;
Alp Tokerec543272013-12-24 09:48:30 +0000674 if (TryConsumeToken(tok::ellipsis, DotDotDotLoc)) {
675 Diag(DotDotDotLoc, diag::ext_gnu_case_range);
Chris Lattner34a22092009-03-04 04:23:07 +0000676 RHS = ParseConstantExpression();
677 if (RHS.isInvalid()) {
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000678 if (SkipUntil(tok::colon, tok::r_brace, StopAtSemi | StopBeforeMatch)) {
679 TryConsumeToken(tok::colon, ColonLoc);
680 continue;
681 }
Chris Lattner34a22092009-03-04 04:23:07 +0000682 return StmtError();
683 }
684 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000685
Chris Lattner125c0ee2009-12-10 00:38:54 +0000686 ColonProtection.restore();
Sebastian Redl042ad952008-12-11 19:30:53 +0000687
Alp Tokerec543272013-12-24 09:48:30 +0000688 if (TryConsumeToken(tok::colon, ColonLoc)) {
Serge Pavlov6a7ffbe2014-04-13 16:52:03 +0000689 } else if (TryConsumeToken(tok::semi, ColonLoc) ||
690 TryConsumeToken(tok::coloncolon, ColonLoc)) {
691 // Treat "case blah;" or "case blah::" as a typo for "case blah:".
Alp Tokerec543272013-12-24 09:48:30 +0000692 Diag(ColonLoc, diag::err_expected_after)
693 << "'case'" << tok::colon
694 << FixItHint::CreateReplacement(ColonLoc, ":");
John McCall0140bfe2011-01-22 09:28:32 +0000695 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000696 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
Alp Tokerec543272013-12-24 09:48:30 +0000697 Diag(ExpectedLoc, diag::err_expected_after)
698 << "'case'" << tok::colon
699 << FixItHint::CreateInsertion(ExpectedLoc, ":");
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000700 ColonLoc = ExpectedLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000701 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000702
John McCalldadc5752010-08-24 06:29:42 +0000703 StmtResult Case =
John McCallb268a282010-08-23 23:25:46 +0000704 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
705 RHS.get(), ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000706
Chris Lattner34a22092009-03-04 04:23:07 +0000707 // If we had a sema error parsing this case, then just ignore it and
708 // continue parsing the sub-stmt.
709 if (Case.isInvalid()) {
710 if (TopLevelCase.isInvalid()) // No parsed case stmts.
711 return ParseStatement();
712 // Otherwise, just don't add it as a nested case.
713 } else {
714 // If this is the first case statement we parsed, it becomes TopLevelCase.
715 // Otherwise we link it into the current chain.
John McCall37ad5512010-08-23 06:44:23 +0000716 Stmt *NextDeepest = Case.get();
Chris Lattner34a22092009-03-04 04:23:07 +0000717 if (TopLevelCase.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000718 TopLevelCase = Case;
Chris Lattner34a22092009-03-04 04:23:07 +0000719 else
John McCallb268a282010-08-23 23:25:46 +0000720 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner34a22092009-03-04 04:23:07 +0000721 DeepestParsedCaseStmt = NextDeepest;
722 }
Mike Stump11289f42009-09-09 15:08:12 +0000723
Chris Lattner34a22092009-03-04 04:23:07 +0000724 // Handle all case statements.
725 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000726
Chris Lattner34a22092009-03-04 04:23:07 +0000727 // If we found a non-case statement, start by parsing it.
John McCalldadc5752010-08-24 06:29:42 +0000728 StmtResult SubStmt;
Mike Stump11289f42009-09-09 15:08:12 +0000729
Chris Lattner34a22092009-03-04 04:23:07 +0000730 if (Tok.isNot(tok::r_brace)) {
731 SubStmt = ParseStatement();
732 } else {
733 // Nicely diagnose the common error "switch (X) { case 4: }", which is
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000734 // not valid. If ColonLoc doesn't point to a valid text location, there was
735 // another parsing error, so avoid producing extra diagnostics.
736 if (ColonLoc.isValid()) {
737 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
738 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
739 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
740 }
741 SubStmt = StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +0000742 }
Mike Stump11289f42009-09-09 15:08:12 +0000743
Chris Lattner34a22092009-03-04 04:23:07 +0000744 // Install the body into the most deeply-nested case.
Serge Pavlov921c2ba2014-05-21 14:48:43 +0000745 if (DeepestParsedCaseStmt) {
746 // Broken sub-stmt shouldn't prevent forming the case statement properly.
747 if (SubStmt.isInvalid())
748 SubStmt = Actions.ActOnNullStmt(SourceLocation());
749 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
750 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000751
Chris Lattner34a22092009-03-04 04:23:07 +0000752 // Return the top level parsed statement tree.
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000753 return TopLevelCase;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000754}
755
756/// ParseDefaultStatement
757/// labeled-statement:
758/// 'default' ':' statement
759/// Note that this does not parse the 'statement' at the end.
760///
Richard Smithc202b282012-04-14 00:33:13 +0000761StmtResult Parser::ParseDefaultStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000762 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000763 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000764
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000765 SourceLocation ColonLoc;
Alp Tokerec543272013-12-24 09:48:30 +0000766 if (TryConsumeToken(tok::colon, ColonLoc)) {
Alp Tokerec543272013-12-24 09:48:30 +0000767 } else if (TryConsumeToken(tok::semi, ColonLoc)) {
Alp Toker97650562014-01-10 11:19:30 +0000768 // Treat "default;" as a typo for "default:".
Alp Tokerec543272013-12-24 09:48:30 +0000769 Diag(ColonLoc, diag::err_expected_after)
770 << "'default'" << tok::colon
771 << FixItHint::CreateReplacement(ColonLoc, ":");
John McCall0140bfe2011-01-22 09:28:32 +0000772 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000773 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
Alp Tokerec543272013-12-24 09:48:30 +0000774 Diag(ExpectedLoc, diag::err_expected_after)
775 << "'default'" << tok::colon
776 << FixItHint::CreateInsertion(ExpectedLoc, ":");
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000777 ColonLoc = ExpectedLoc;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000778 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000779
Richard Smith1002d102012-02-17 01:35:32 +0000780 StmtResult SubStmt;
781
782 if (Tok.isNot(tok::r_brace)) {
783 SubStmt = ParseStatement();
784 } else {
785 // Diagnose the common error "switch (X) {... default: }", which is
786 // not valid.
David Majnemerc6a99872011-06-14 15:24:38 +0000787 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
Richard Smith1002d102012-02-17 01:35:32 +0000788 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
789 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
790 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000791 }
792
Richard Smith1002d102012-02-17 01:35:32 +0000793 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000794 if (SubStmt.isInvalid())
Richard Smith1002d102012-02-17 01:35:32 +0000795 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl042ad952008-12-11 19:30:53 +0000796
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000797 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000798 SubStmt.get(), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000799}
800
Richard Smithc202b282012-04-14 00:33:13 +0000801StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
802 return ParseCompoundStatement(isStmtExpr, Scope::DeclScope);
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000803}
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000804
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000805/// ParseCompoundStatement - Parse a "{}" block.
806///
807/// compound-statement: [C99 6.8.2]
808/// { block-item-list[opt] }
809/// [GNU] { label-declarations block-item-list } [TODO]
810///
811/// block-item-list:
812/// block-item
813/// block-item-list block-item
814///
815/// block-item:
816/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000817/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000818/// statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000819///
820/// [GNU] label-declarations:
821/// [GNU] label-declaration
822/// [GNU] label-declarations label-declaration
823///
824/// [GNU] label-declaration:
825/// [GNU] '__label__' identifier-list ';'
826///
Richard Smithc202b282012-04-14 00:33:13 +0000827StmtResult Parser::ParseCompoundStatement(bool isStmtExpr,
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000828 unsigned ScopeFlags) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000829 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000830
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000831 // Enter a scope to hold everything within the compound stmt. Compound
832 // statements can always hold declarations.
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000833 ParseScope CompoundScope(this, ScopeFlags);
Chris Lattnerf2978802007-01-21 06:52:16 +0000834
835 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000836 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000837}
838
Lang Hames2954cea2012-11-03 22:29:05 +0000839/// Parse any pragmas at the start of the compound expression. We handle these
840/// separately since some pragmas (FP_CONTRACT) must appear before any C
841/// statement in the compound, but may be intermingled with other pragmas.
842void Parser::ParseCompoundStatementLeadingPragmas() {
843 bool checkForPragmas = true;
844 while (checkForPragmas) {
845 switch (Tok.getKind()) {
846 case tok::annot_pragma_vis:
847 HandlePragmaVisibility();
848 break;
849 case tok::annot_pragma_pack:
850 HandlePragmaPack();
851 break;
852 case tok::annot_pragma_msstruct:
853 HandlePragmaMSStruct();
854 break;
855 case tok::annot_pragma_align:
856 HandlePragmaAlign();
857 break;
858 case tok::annot_pragma_weak:
859 HandlePragmaWeak();
860 break;
861 case tok::annot_pragma_weakalias:
862 HandlePragmaWeakAlias();
863 break;
864 case tok::annot_pragma_redefine_extname:
865 HandlePragmaRedefineExtname();
866 break;
867 case tok::annot_pragma_opencl_extension:
868 HandlePragmaOpenCLExtension();
869 break;
870 case tok::annot_pragma_fp_contract:
871 HandlePragmaFPContract();
872 break;
David Majnemer4bb09802014-02-10 19:50:15 +0000873 case tok::annot_pragma_ms_pointers_to_members:
874 HandlePragmaMSPointersToMembers();
875 break;
Warren Huntc3b18962014-04-08 22:30:47 +0000876 case tok::annot_pragma_ms_pragma:
877 HandlePragmaMSPragma();
878 break;
Lang Hames2954cea2012-11-03 22:29:05 +0000879 default:
880 checkForPragmas = false;
881 break;
882 }
883 }
884
885}
886
Chris Lattnerf2978802007-01-21 06:52:16 +0000887/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000888/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000889/// consume the '}' at the end of the block. It does not manipulate the scope
890/// stack.
John McCalldadc5752010-08-24 06:29:42 +0000891StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000892 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000893 Tok.getLocation(),
894 "in compound statement ('{}')");
Lang Hames5de91cc2012-10-02 04:45:10 +0000895
896 // Record the state of the FP_CONTRACT pragma, restore on leaving the
897 // compound statement.
898 Sema::FPContractStateRAII SaveFPContractState(Actions);
899
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000900 InMessageExpressionRAIIObject InMessage(*this, false);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000901 BalancedDelimiterTracker T(*this, tok::l_brace);
902 if (T.consumeOpen())
903 return StmtError();
Chris Lattnerf2978802007-01-21 06:52:16 +0000904
Dmitri Gribenko800ddf32012-02-14 22:14:32 +0000905 Sema::CompoundScopeRAII CompoundScope(Actions);
906
Lang Hames2954cea2012-11-03 22:29:05 +0000907 // Parse any pragmas at the beginning of the compound statement.
908 ParseCompoundStatementLeadingPragmas();
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000909
Lang Hames2954cea2012-11-03 22:29:05 +0000910 StmtVector Stmts;
Lang Hamesa930e712012-10-21 01:10:01 +0000911
Chris Lattner43e7f312011-02-18 02:08:43 +0000912 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
913 // only allowed at the start of a compound stmt regardless of the language.
914 while (Tok.is(tok::kw___label__)) {
915 SourceLocation LabelLoc = ConsumeToken();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000916
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000917 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner43e7f312011-02-18 02:08:43 +0000918 while (1) {
919 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +0000920 Diag(Tok, diag::err_expected) << tok::identifier;
Chris Lattner43e7f312011-02-18 02:08:43 +0000921 break;
922 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000923
Chris Lattner43e7f312011-02-18 02:08:43 +0000924 IdentifierInfo *II = Tok.getIdentifierInfo();
925 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000926 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000927
Alp Tokerec543272013-12-24 09:48:30 +0000928 if (!TryConsumeToken(tok::comma))
Chris Lattner43e7f312011-02-18 02:08:43 +0000929 break;
Chris Lattner43e7f312011-02-18 02:08:43 +0000930 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000931
John McCall084e83d2011-03-24 11:26:52 +0000932 DeclSpec DS(AttrFactory);
Rafael Espindolaab417692013-07-09 12:05:01 +0000933 DeclGroupPtrTy Res =
934 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner43e7f312011-02-18 02:08:43 +0000935 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000936
Chris Lattner02f1b612012-04-28 16:12:17 +0000937 ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
Chris Lattner43e7f312011-02-18 02:08:43 +0000938 if (R.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000939 Stmts.push_back(R.get());
Chris Lattner43e7f312011-02-18 02:08:43 +0000940 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000941
Richard Smith34f30512013-11-23 04:06:09 +0000942 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000943 if (Tok.is(tok::annot_pragma_unused)) {
944 HandlePragmaUnused();
945 continue;
946 }
947
John McCalldadc5752010-08-24 06:29:42 +0000948 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000949 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000950 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000951 } else {
952 // __extension__ can start declarations and it can also be a unary
953 // operator for expressions. Consume multiple __extension__ markers here
954 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000955 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000956 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000957 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000958 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +0000959
John McCall084e83d2011-03-24 11:26:52 +0000960 ParsedAttributesWithRange attrs(AttrFactory);
Craig Topper161e4db2014-05-21 06:02:52 +0000961 MaybeParseCXX11Attributes(attrs, nullptr,
962 /*MightBeObjCMessageSend*/ true);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000963
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000964 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000965 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +0000966 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +0000967 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +0000968 ExtensionRAIIObject O(Diags);
969
Chris Lattner49836b42009-04-02 04:16:50 +0000970 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Rafael Espindola1bd906d2014-10-22 14:27:08 +0000971 DeclGroupPtrTy Res = ParseDeclaration(Declarator::BlockContext, DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000972 attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000973 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000974 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000975 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +0000976 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +0000977
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000978 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000979 SkipUntil(tok::semi);
980 continue;
981 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000982
Alexis Hunt96d5c762009-11-21 08:43:09 +0000983 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +0000984 // Eat the semicolon at the end of stmt and convert the expr into a
985 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000986 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +0000987 R = Actions.ActOnExprStmt(Res);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000988 }
989 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000990
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000991 if (R.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000992 Stmts.push_back(R.get());
Chris Lattner30f910e2006-10-16 05:52:41 +0000993 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000994
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +0000995 SourceLocation CloseLoc = Tok.getLocation();
996
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000997 // We broke out of the while loop because we found a '}' or EOF.
Nico Webera48b6c22012-12-30 23:36:56 +0000998 if (!T.consumeClose())
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +0000999 // Recover by creating a compound statement with what we parsed so far,
1000 // instead of dropping everything and returning StmtError();
Nico Webera48b6c22012-12-30 23:36:56 +00001001 CloseLoc = T.getCloseLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +00001002
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +00001003 return Actions.ActOnCompoundStmt(T.getOpenLocation(), CloseLoc,
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001004 Stmts, isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +00001005}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001006
Chris Lattnerc0081db2008-12-12 06:31:07 +00001007/// ParseParenExprOrCondition:
1008/// [C ] '(' expression ')'
Chris Lattner10da53c2008-12-12 06:35:28 +00001009/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattnerc0081db2008-12-12 06:31:07 +00001010///
1011/// This function parses and performs error recovery on the specified condition
1012/// or expression (depending on whether we're in C++ or C mode). This function
1013/// goes out of its way to recover well. It returns true if there was a parser
1014/// error (the right paren couldn't be found), which indicates that the caller
1015/// should try to recover harder. It returns false if the condition is
1016/// successfully parsed. Note that a successful parse can still have semantic
1017/// errors in the condition.
John McCalldadc5752010-08-24 06:29:42 +00001018bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCall48871652010-08-21 09:40:31 +00001019 Decl *&DeclResult,
Douglas Gregore60e41a2010-05-06 17:25:47 +00001020 SourceLocation Loc,
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +00001021 bool ConvertToBoolean) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001022 BalancedDelimiterTracker T(*this, tok::l_paren);
1023 T.consumeOpen();
1024
David Blaikiebbafb8a2012-03-11 07:00:24 +00001025 if (getLangOpts().CPlusPlus)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001026 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001027 else {
1028 ExprResult = ParseExpression();
Craig Topper161e4db2014-05-21 06:02:52 +00001029 DeclResult = nullptr;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001030
Douglas Gregore60e41a2010-05-06 17:25:47 +00001031 // If required, convert to a boolean value.
1032 if (!ExprResult.isInvalid() && ConvertToBoolean)
1033 ExprResult
John McCallb268a282010-08-23 23:25:46 +00001034 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor7bab5ff2009-11-25 00:27:52 +00001035 }
Mike Stump11289f42009-09-09 15:08:12 +00001036
Chris Lattnerc0081db2008-12-12 06:31:07 +00001037 // If the parser was confused by the condition and we don't have a ')', try to
1038 // recover by skipping ahead to a semi and bailing out. If condexp is
1039 // semantically invalid but we have well formed code, keep going.
John McCall48871652010-08-21 09:40:31 +00001040 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +00001041 SkipUntil(tok::semi);
1042 // Skipping may have stopped if it found the containing ')'. If so, we can
1043 // continue parsing the if statement.
1044 if (Tok.isNot(tok::r_paren))
1045 return true;
1046 }
Mike Stump11289f42009-09-09 15:08:12 +00001047
Chris Lattnerc0081db2008-12-12 06:31:07 +00001048 // Otherwise the condition is valid or the rparen is present.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001049 T.consumeClose();
Chad Rosier67055f52012-07-10 21:35:27 +00001050
Chris Lattner70d44982012-04-28 16:24:20 +00001051 // Check for extraneous ')'s to catch things like "if (foo())) {". We know
1052 // that all callers are looking for a statement after the condition, so ")"
1053 // isn't valid.
1054 while (Tok.is(tok::r_paren)) {
1055 Diag(Tok, diag::err_extraneous_rparen_in_condition)
1056 << FixItHint::CreateRemoval(Tok.getLocation());
1057 ConsumeParen();
1058 }
Chad Rosier67055f52012-07-10 21:35:27 +00001059
Chris Lattnerc0081db2008-12-12 06:31:07 +00001060 return false;
1061}
1062
1063
Chris Lattnerc951dae2006-08-10 04:23:57 +00001064/// ParseIfStatement
1065/// if-statement: [C99 6.8.4.1]
1066/// 'if' '(' expression ')' statement
1067/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001068/// [C++] 'if' '(' condition ')' statement
1069/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +00001070///
Richard Smithc202b282012-04-14 00:33:13 +00001071StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001072 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001073 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +00001074
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001075 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001076 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +00001077 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +00001078 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +00001079 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001080
David Blaikiebbafb8a2012-03-11 07:00:24 +00001081 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001082
Chris Lattner2dd1b722007-08-26 23:08:06 +00001083 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
1084 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001085 //
1086 // C++ 6.4p3:
1087 // A name introduced by a declaration in a condition is in scope from its
1088 // point of declaration until the end of the substatements controlled by the
1089 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001090 // C++ 3.3.2p4:
1091 // Names declared in the for-init-statement, and in the condition of if,
1092 // while, for, and switch statements are local to the if, while, for, or
1093 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001094 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001095 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +00001096
Chris Lattnerc951dae2006-08-10 04:23:57 +00001097 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001098 ExprResult CondExp;
Craig Topper161e4db2014-05-21 06:02:52 +00001099 Decl *CondVar = nullptr;
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +00001100 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001101 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +00001102
David Blaikiea5696df2012-05-16 04:20:04 +00001103 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get(), IfLoc));
Mike Stump11289f42009-09-09 15:08:12 +00001104
Chris Lattner8fb26252007-08-22 05:28:50 +00001105 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001106 // there is no compound stmt. C90 does not have this clause. We only do this
1107 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001108 //
1109 // C++ 6.4p1:
1110 // The substatement in a selection-statement (each substatement, in the else
1111 // form of the if statement) implicitly defines a local scope.
1112 //
1113 // For C++ we create a scope for the condition and a new scope for
1114 // substatements because:
1115 // -When the 'then' scope exits, we want the condition declaration to still be
1116 // active for the 'else' scope too.
1117 // -Sema will detect name clashes by considering declarations of a
1118 // 'ControlScope' as part of its direct subscope.
1119 // -If we wanted the condition and substatement to be in the same scope, we
1120 // would have to notify ParseStatement not to create a new scope. It's
1121 // simpler to let it create a new scope.
1122 //
David Majnemer2206bf52014-03-05 08:57:59 +00001123 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001124
Chris Lattner5c5808a2007-10-29 05:08:52 +00001125 // Read the 'then' stmt.
1126 SourceLocation ThenStmtLoc = Tok.getLocation();
Nico Weber3cef1082011-12-22 23:26:17 +00001127
1128 SourceLocation InnerStatementTrailingElseLoc;
1129 StmtResult ThenStmt(ParseStatement(&InnerStatementTrailingElseLoc));
Chris Lattnerac4471c2007-05-28 05:38:24 +00001130
Chris Lattner37e54f42007-08-22 05:16:28 +00001131 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001132 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001133
Chris Lattnerc951dae2006-08-10 04:23:57 +00001134 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +00001135 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +00001136 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +00001137 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001138
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001139 if (Tok.is(tok::kw_else)) {
Nico Weber3cef1082011-12-22 23:26:17 +00001140 if (TrailingElseLoc)
1141 *TrailingElseLoc = Tok.getLocation();
1142
Chris Lattneraf635312006-10-16 06:06:51 +00001143 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +00001144 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +00001145
Chris Lattner8fb26252007-08-22 05:28:50 +00001146 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001147 // there is no compound stmt. C90 does not have this clause. We only do
1148 // this if the body isn't a compound statement to avoid push/pop in common
1149 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001150 //
1151 // C++ 6.4p1:
1152 // The substatement in a selection-statement (each substatement, in the else
1153 // form of the if statement) implicitly defines a local scope.
1154 //
David Majnemer2206bf52014-03-05 08:57:59 +00001155 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001156
Chris Lattner30f910e2006-10-16 05:52:41 +00001157 ElseStmt = ParseStatement();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001158
Chris Lattner37e54f42007-08-22 05:16:28 +00001159 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001160 InnerScope.Exit();
Douglas Gregor4ecb7202011-07-30 08:36:53 +00001161 } else if (Tok.is(tok::code_completion)) {
1162 Actions.CodeCompleteAfterIf(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001163 cutOffParsing();
1164 return StmtError();
Nico Weber3cef1082011-12-22 23:26:17 +00001165 } else if (InnerStatementTrailingElseLoc.isValid()) {
1166 Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else);
Chris Lattnerc951dae2006-08-10 04:23:57 +00001167 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001168
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001169 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +00001170
Chris Lattner5c5808a2007-10-29 05:08:52 +00001171 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +00001172 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +00001173 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001174 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
Craig Topper161e4db2014-05-21 06:02:52 +00001175 (ThenStmt.isInvalid() && ElseStmt.get() == nullptr) ||
1176 (ThenStmt.get() == nullptr && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +00001177 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +00001178 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +00001179 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001180
Chris Lattner5c5808a2007-10-29 05:08:52 +00001181 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001182 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001183 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001184 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001185 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001186
John McCallb268a282010-08-23 23:25:46 +00001187 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +00001188 ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +00001189}
1190
Chris Lattner9075bd72006-08-10 04:59:57 +00001191/// ParseSwitchStatement
1192/// switch-statement:
1193/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001194/// [C++] 'switch' '(' condition ')' statement
Richard Smithc202b282012-04-14 00:33:13 +00001195StmtResult Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001196 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001197 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +00001198
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001199 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001200 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +00001201 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001202 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001203 }
Chris Lattner2dd1b722007-08-26 23:08:06 +00001204
David Blaikiebbafb8a2012-03-11 07:00:24 +00001205 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001206
Chris Lattner2dd1b722007-08-26 23:08:06 +00001207 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1208 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001209 //
1210 // C++ 6.4p3:
1211 // A name introduced by a declaration in a condition is in scope from its
1212 // point of declaration until the end of the substatements controlled by the
1213 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001214 // C++ 3.3.2p4:
1215 // Names declared in the for-init-statement, and in the condition of if,
1216 // while, for, and switch statements are local to the if, while, for, or
1217 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001218 //
Serge Pavlov09f99242014-01-23 15:05:00 +00001219 unsigned ScopeFlags = Scope::SwitchScope;
Chris Lattnerc0081db2008-12-12 06:31:07 +00001220 if (C99orCXX)
1221 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001222 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001223
Chris Lattner9075bd72006-08-10 04:59:57 +00001224 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001225 ExprResult Cond;
Craig Topper161e4db2014-05-21 06:02:52 +00001226 Decl *CondVar = nullptr;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001227 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redlb62406f2008-12-11 19:48:14 +00001228 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +00001229
John McCalldadc5752010-08-24 06:29:42 +00001230 StmtResult Switch
John McCallb268a282010-08-23 23:25:46 +00001231 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001232
Douglas Gregore60e41a2010-05-06 17:25:47 +00001233 if (Switch.isInvalid()) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001234 // Skip the switch body.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001235 // FIXME: This is not optimal recovery, but parsing the body is more
1236 // dangerous due to the presence of case and default statements, which
1237 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +00001238 if (Tok.is(tok::l_brace)) {
1239 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001240 SkipUntil(tok::r_brace);
Douglas Gregor4abc32d2010-05-20 23:20:59 +00001241 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +00001242 SkipUntil(tok::semi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001243 return Switch;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001244 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001245
Chris Lattner8fb26252007-08-22 05:28:50 +00001246 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001247 // there is no compound stmt. C90 does not have this clause. We only do this
1248 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001249 //
1250 // C++ 6.4p1:
1251 // The substatement in a selection-statement (each substatement, in the else
1252 // form of the if statement) implicitly defines a local scope.
1253 //
1254 // See comments in ParseIfStatement for why we create a scope for the
1255 // condition and a new scope for substatement in C++.
1256 //
Serge Pavlov09f99242014-01-23 15:05:00 +00001257 getCurScope()->AddFlags(Scope::BreakScope);
David Majnemer2206bf52014-03-05 08:57:59 +00001258 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +00001259
Hans Wennborg852c3462014-06-17 00:09:05 +00001260 // We have incremented the mangling number for the SwitchScope and the
1261 // InnerScope, which is one too many.
1262 if (C99orCXX)
1263 getCurScope()->decrementMSLocalManglingNumber();
1264
Chris Lattner9075bd72006-08-10 04:59:57 +00001265 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001266 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001267
Chris Lattner8fd2d012010-01-24 01:50:29 +00001268 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001269 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001270 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001271
John McCallb268a282010-08-23 23:25:46 +00001272 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001273}
1274
1275/// ParseWhileStatement
1276/// while-statement: [C99 6.8.5.1]
1277/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001278/// [C++] 'while' '(' condition ')' statement
Richard Smithc202b282012-04-14 00:33:13 +00001279StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001280 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +00001281 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +00001282 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001283
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001284 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001285 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +00001286 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001287 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001288 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001289
David Blaikiebbafb8a2012-03-11 07:00:24 +00001290 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001291
Chris Lattner2dd1b722007-08-26 23:08:06 +00001292 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1293 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001294 //
1295 // C++ 6.4p3:
1296 // A name introduced by a declaration in a condition is in scope from its
1297 // point of declaration until the end of the substatements controlled by the
1298 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001299 // C++ 3.3.2p4:
1300 // Names declared in the for-init-statement, and in the condition of if,
1301 // while, for, and switch statements are local to the if, while, for, or
1302 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001303 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001304 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001305 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001306 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1307 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001308 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001309 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1310 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001311
Chris Lattner9075bd72006-08-10 04:59:57 +00001312 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001313 ExprResult Cond;
Craig Topper161e4db2014-05-21 06:02:52 +00001314 Decl *CondVar = nullptr;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001315 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001316 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001317
David Blaikiea5696df2012-05-16 04:20:04 +00001318 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get(), WhileLoc));
Mike Stump11289f42009-09-09 15:08:12 +00001319
Justin Bognere4ebb6c2013-12-03 07:36:55 +00001320 // C99 6.8.5p5 - In C99, the body of the while statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001321 // there is no compound stmt. C90 does not have this clause. We only do this
1322 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001323 //
1324 // C++ 6.5p2:
1325 // The substatement in an iteration-statement implicitly defines a local scope
1326 // which is entered and exited each time through the loop.
1327 //
1328 // See comments in ParseIfStatement for why we create a scope for the
1329 // condition and a new scope for substatement in C++.
1330 //
David Majnemer2206bf52014-03-05 08:57:59 +00001331 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001332
Chris Lattner9075bd72006-08-10 04:59:57 +00001333 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001334 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001335
Chris Lattner8fb26252007-08-22 05:28:50 +00001336 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001337 InnerScope.Exit();
1338 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001339
John McCall48871652010-08-21 09:40:31 +00001340 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001341 return StmtError();
1342
John McCallb268a282010-08-23 23:25:46 +00001343 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001344}
1345
1346/// ParseDoStatement
1347/// do-statement: [C99 6.8.5.2]
1348/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +00001349/// Note: this lets the caller parse the end ';'.
Richard Smithc202b282012-04-14 00:33:13 +00001350StmtResult Parser::ParseDoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001351 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001352 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001353
Chris Lattner2dd1b722007-08-26 23:08:06 +00001354 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1355 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001356 unsigned ScopeFlags;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001357 if (getLangOpts().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001358 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001359 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001360 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001361
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001362 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001363
Justin Bognere4ebb6c2013-12-03 07:36:55 +00001364 // C99 6.8.5p5 - In C99, the body of the do statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001365 // there is no compound stmt. C90 does not have this clause. We only do this
1366 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +00001367 //
1368 // C++ 6.5p2:
1369 // The substatement in an iteration-statement implicitly defines a local scope
1370 // which is entered and exited each time through the loop.
1371 //
David Majnemer2206bf52014-03-05 08:57:59 +00001372 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
1373 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001374
Chris Lattner9075bd72006-08-10 04:59:57 +00001375 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001376 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +00001377
Chris Lattner8fb26252007-08-22 05:28:50 +00001378 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001379 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001380
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001381 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001382 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +00001383 Diag(Tok, diag::err_expected_while);
Alp Tokerec543272013-12-24 09:48:30 +00001384 Diag(DoLoc, diag::note_matching) << "'do'";
Alexey Bataevee6507d2013-11-18 08:17:37 +00001385 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner0046de12008-11-13 18:52:53 +00001386 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001387 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001388 }
Chris Lattneraf635312006-10-16 06:06:51 +00001389 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001390
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001391 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001392 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Alexey Bataevee6507d2013-11-18 08:17:37 +00001393 SkipUntil(tok::semi, StopBeforeMatch);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001394 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001395 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001396
Richard Smithc2c8bb82013-10-15 01:34:54 +00001397 // Parse the parenthesized expression.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001398 BalancedDelimiterTracker T(*this, tok::l_paren);
1399 T.consumeOpen();
Chad Rosier67055f52012-07-10 21:35:27 +00001400
Richard Smithc2c8bb82013-10-15 01:34:54 +00001401 // A do-while expression is not a condition, so can't have attributes.
1402 DiagnoseAndSkipCXX11Attributes();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001403
John McCalldadc5752010-08-24 06:29:42 +00001404 ExprResult Cond = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001405 T.consumeClose();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001406 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001407
Sebastian Redlb62406f2008-12-11 19:48:14 +00001408 if (Cond.isInvalid() || Body.isInvalid())
1409 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001410
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001411 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(),
1412 Cond.get(), T.getCloseLocation());
Chris Lattner9075bd72006-08-10 04:59:57 +00001413}
1414
Richard Smith955bf012014-06-19 11:42:00 +00001415bool Parser::isForRangeIdentifier() {
1416 assert(Tok.is(tok::identifier));
1417
1418 const Token &Next = NextToken();
1419 if (Next.is(tok::colon))
1420 return true;
1421
1422 if (Next.is(tok::l_square) || Next.is(tok::kw_alignas)) {
1423 TentativeParsingAction PA(*this);
1424 ConsumeToken();
1425 SkipCXX11Attributes();
1426 bool Result = Tok.is(tok::colon);
1427 PA.Revert();
1428 return Result;
1429 }
1430
1431 return false;
1432}
1433
Chris Lattner9075bd72006-08-10 04:59:57 +00001434/// ParseForStatement
1435/// for-statement: [C99 6.8.5.3]
1436/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1437/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001438/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1439/// [C++] statement
Richard Smith02e85f32011-04-14 22:09:26 +00001440/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001441/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1442/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001443///
1444/// [C++] for-init-statement:
1445/// [C++] expression-statement
1446/// [C++] simple-declaration
1447///
Richard Smith02e85f32011-04-14 22:09:26 +00001448/// [C++0x] for-range-declaration:
1449/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1450/// [C++0x] for-range-initializer:
1451/// [C++0x] expression
1452/// [C++0x] braced-init-list [TODO]
Richard Smithc202b282012-04-14 00:33:13 +00001453StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001454 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001455 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001456
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001457 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001458 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +00001459 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001460 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001461 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001462
Chad Rosier67055f52012-07-10 21:35:27 +00001463 bool C99orCXXorObjC = getLangOpts().C99 || getLangOpts().CPlusPlus ||
1464 getLangOpts().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001465
Chris Lattner2dd1b722007-08-26 23:08:06 +00001466 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1467 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001468 //
1469 // C++ 6.4p3:
1470 // A name introduced by a declaration in a condition is in scope from its
1471 // point of declaration until the end of the substatements controlled by the
1472 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001473 // C++ 3.3.2p4:
1474 // Names declared in the for-init-statement, and in the condition of if,
1475 // while, for, and switch statements are local to the if, while, for, or
1476 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001477 // C++ 6.5.3p1:
1478 // Names declared in the for-init-statement are in the same declarative-region
1479 // as those declared in the condition.
1480 //
Serge Pavlov09f99242014-01-23 15:05:00 +00001481 unsigned ScopeFlags = 0;
Chris Lattner934074c2009-04-22 00:54:41 +00001482 if (C99orCXXorObjC)
Serge Pavlov09f99242014-01-23 15:05:00 +00001483 ScopeFlags = Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001484
1485 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +00001486
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001487 BalancedDelimiterTracker T(*this, tok::l_paren);
1488 T.consumeOpen();
1489
John McCalldadc5752010-08-24 06:29:42 +00001490 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001491
Richard Smith02e85f32011-04-14 22:09:26 +00001492 bool ForEach = false, ForRange = false;
John McCalldadc5752010-08-24 06:29:42 +00001493 StmtResult FirstPart;
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001494 bool SecondPartIsInvalid = false;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001495 FullExprArg SecondPart(Actions);
John McCalldadc5752010-08-24 06:29:42 +00001496 ExprResult Collection;
Richard Smith02e85f32011-04-14 22:09:26 +00001497 ForRangeInit ForRangeInit;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001498 FullExprArg ThirdPart(Actions);
Craig Topper161e4db2014-05-21 06:02:52 +00001499 Decl *SecondVar = nullptr;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001500
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001501 if (Tok.is(tok::code_completion)) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001502 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001503 C99orCXXorObjC? Sema::PCC_ForInit
1504 : Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001505 cutOffParsing();
1506 return StmtError();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001507 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001508
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001509 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001510 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001511
Chris Lattner9075bd72006-08-10 04:59:57 +00001512 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001513 if (Tok.is(tok::semi)) { // for (;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001514 ProhibitAttributes(attrs);
Chris Lattner53361ac2006-08-10 05:19:57 +00001515 // no first part, eat the ';'.
1516 ConsumeToken();
Richard Smith955bf012014-06-19 11:42:00 +00001517 } else if (getLangOpts().CPlusPlus && Tok.is(tok::identifier) &&
1518 isForRangeIdentifier()) {
1519 ProhibitAttributes(attrs);
1520 IdentifierInfo *Name = Tok.getIdentifierInfo();
1521 SourceLocation Loc = ConsumeToken();
1522 MaybeParseCXX11Attributes(attrs);
1523
1524 ForRangeInit.ColonLoc = ConsumeToken();
1525 if (Tok.is(tok::l_brace))
1526 ForRangeInit.RangeExpr = ParseBraceInitializer();
1527 else
1528 ForRangeInit.RangeExpr = ParseExpression();
1529
Richard Smith83d3f152014-11-27 01:54:27 +00001530 Diag(Loc, diag::err_for_range_identifier)
Richard Smith955bf012014-06-19 11:42:00 +00001531 << ((getLangOpts().CPlusPlus11 && !getLangOpts().CPlusPlus1z)
1532 ? FixItHint::CreateInsertion(Loc, "auto &&")
1533 : FixItHint());
1534
1535 FirstPart = Actions.ActOnCXXForRangeIdentifier(getCurScope(), Loc, Name,
1536 attrs, attrs.Range.getEnd());
1537 ForRange = true;
Eli Friedman0ffc31c2011-12-20 01:50:37 +00001538 } else if (isForInitDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001539 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001540 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001541 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001542
Richard Smith02e85f32011-04-14 22:09:26 +00001543 // In C++0x, "for (T NS:a" might not be a typo for ::
David Blaikiebbafb8a2012-03-11 07:00:24 +00001544 bool MightBeForRangeStmt = getLangOpts().CPlusPlus;
Richard Smith02e85f32011-04-14 22:09:26 +00001545 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1546
Chris Lattner49836b42009-04-02 04:16:50 +00001547 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00001548 DeclGroupPtrTy DG = ParseSimpleDeclaration(
Rafael Espindola1bd906d2014-10-22 14:27:08 +00001549 Declarator::ForContext, DeclEnd, attrs, false,
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00001550 MightBeForRangeStmt ? &ForRangeInit : nullptr);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001551 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Richard Smith02e85f32011-04-14 22:09:26 +00001552 if (ForRangeInit.ParsedForRangeDecl()) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001553 Diag(ForRangeInit.ColonLoc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00001554 diag::warn_cxx98_compat_for_range : diag::ext_for_range);
Richard Smith58c74332011-09-04 19:54:14 +00001555
Richard Smith02e85f32011-04-14 22:09:26 +00001556 ForRange = true;
1557 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattner32dc41c2009-03-29 17:27:48 +00001558 ConsumeToken();
1559 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001560 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001561 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001562 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001563
Douglas Gregor68762e72010-08-23 21:17:50 +00001564 if (Tok.is(tok::code_completion)) {
1565 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001566 cutOffParsing();
1567 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001568 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001569 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001570 } else {
1571 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001572 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001573 } else {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001574 ProhibitAttributes(attrs);
Kaelyn Takatab16e6322014-11-20 22:06:40 +00001575 Value = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Chris Lattner71e23ce2006-11-04 20:18:38 +00001576
John McCall34376a62010-12-04 03:47:34 +00001577 ForEach = isTokIdentifier_in();
1578
Chris Lattnercd68f642007-06-27 01:06:29 +00001579 // Turn the expression into a stmt.
John McCall34376a62010-12-04 03:47:34 +00001580 if (!Value.isInvalid()) {
1581 if (ForEach)
1582 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1583 else
Richard Smith945f8d32013-01-14 22:39:08 +00001584 FirstPart = Actions.ActOnExprStmt(Value);
John McCall34376a62010-12-04 03:47:34 +00001585 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001586
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001587 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001588 ConsumeToken();
John McCall34376a62010-12-04 03:47:34 +00001589 } else if (ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001590 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001591
Douglas Gregor68762e72010-08-23 21:17:50 +00001592 if (Tok.is(tok::code_completion)) {
1593 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001594 cutOffParsing();
1595 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001596 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001597 Collection = ParseExpression();
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001598 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::colon) && FirstPart.get()) {
Richard Smith4f848f12011-12-20 22:56:20 +00001599 // User tried to write the reasonable, but ill-formed, for-range-statement
1600 // for (expr : expr) { ... }
1601 Diag(Tok, diag::err_for_range_expected_decl)
1602 << FirstPart.get()->getSourceRange();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001603 SkipUntil(tok::r_paren, StopBeforeMatch);
Richard Smith4f848f12011-12-20 22:56:20 +00001604 SecondPartIsInvalid = true;
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001605 } else {
Douglas Gregor230a7e62011-02-17 03:38:46 +00001606 if (!Value.isInvalid()) {
1607 Diag(Tok, diag::err_expected_semi_for);
1608 } else {
1609 // Skip until semicolon or rparen, don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001610 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor230a7e62011-02-17 03:38:46 +00001611 if (Tok.is(tok::semi))
1612 ConsumeToken();
1613 }
Chris Lattner53361ac2006-08-10 05:19:57 +00001614 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001615 }
Serge Pavlov09f99242014-01-23 15:05:00 +00001616
1617 // Parse the second part of the for specifier.
1618 getCurScope()->AddFlags(Scope::BreakScope | Scope::ContinueScope);
Richard Smith02e85f32011-04-14 22:09:26 +00001619 if (!ForEach && !ForRange) {
John McCallb268a282010-08-23 23:25:46 +00001620 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001621 // Parse the second part of the for specifier.
1622 if (Tok.is(tok::semi)) { // for (...;;
1623 // no second part.
Douglas Gregor230a7e62011-02-17 03:38:46 +00001624 } else if (Tok.is(tok::r_paren)) {
1625 // missing both semicolons.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001626 } else {
John McCalldadc5752010-08-24 06:29:42 +00001627 ExprResult Second;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001628 if (getLangOpts().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +00001629 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1630 else {
1631 Second = ParseExpression();
1632 if (!Second.isInvalid())
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001633 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCallb268a282010-08-23 23:25:46 +00001634 Second.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001635 }
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001636 SecondPartIsInvalid = Second.isInvalid();
David Blaikiea5696df2012-05-16 04:20:04 +00001637 SecondPart = Actions.MakeFullExpr(Second.get(), ForLoc);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001638 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001639
Douglas Gregor230a7e62011-02-17 03:38:46 +00001640 if (Tok.isNot(tok::semi)) {
1641 if (!SecondPartIsInvalid || SecondVar)
1642 Diag(Tok, diag::err_expected_semi_for);
1643 else
1644 // Skip until semicolon or rparen, don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001645 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor230a7e62011-02-17 03:38:46 +00001646 }
1647
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001648 if (Tok.is(tok::semi)) {
1649 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001650 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001651
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001652 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001653 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001654 ExprResult Third = ParseExpression();
Richard Smith945f8d32013-01-14 22:39:08 +00001655 // FIXME: The C++11 standard doesn't actually say that this is a
1656 // discarded-value expression, but it clearly should be.
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001657 ThirdPart = Actions.MakeFullDiscardedValueExpr(Third.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001658 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001659 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001660 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001661 T.consumeClose();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001662
Richard Smith02e85f32011-04-14 22:09:26 +00001663 // We need to perform most of the semantic analysis for a C++0x for-range
1664 // statememt before parsing the body, in order to be able to deduce the type
1665 // of an auto-typed loop variable.
1666 StmtResult ForRangeStmt;
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001667 StmtResult ForEachStmt;
Chad Rosier67055f52012-07-10 21:35:27 +00001668
John McCall53848232011-07-27 01:07:15 +00001669 if (ForRange) {
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001670 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, FirstPart.get(),
Richard Smith02e85f32011-04-14 22:09:26 +00001671 ForRangeInit.ColonLoc,
1672 ForRangeInit.RangeExpr.get(),
Richard Smitha05b3b52012-09-20 21:52:32 +00001673 T.getCloseLocation(),
1674 Sema::BFRK_Build);
Richard Smith02e85f32011-04-14 22:09:26 +00001675
John McCall53848232011-07-27 01:07:15 +00001676
1677 // Similarly, we need to do the semantic analysis for a for-range
1678 // statement immediately in order to close over temporaries correctly.
1679 } else if (ForEach) {
Sam Panzer2c4ca0f2012-08-16 21:47:25 +00001680 ForEachStmt = Actions.ActOnObjCForCollectionStmt(ForLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001681 FirstPart.get(),
1682 Collection.get(),
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001683 T.getCloseLocation());
John McCall53848232011-07-27 01:07:15 +00001684 }
1685
Justin Bognere4ebb6c2013-12-03 07:36:55 +00001686 // C99 6.8.5p5 - In C99, the body of the for statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001687 // there is no compound stmt. C90 does not have this clause. We only do this
1688 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001689 //
1690 // C++ 6.5p2:
1691 // The substatement in an iteration-statement implicitly defines a local scope
1692 // which is entered and exited each time through the loop.
1693 //
1694 // See comments in ParseIfStatement for why we create a scope for
1695 // for-init-statement/condition and a new scope for substatement in C++.
1696 //
David Majnemer2206bf52014-03-05 08:57:59 +00001697 ParseScope InnerScope(this, Scope::DeclScope, C99orCXXorObjC,
1698 Tok.is(tok::l_brace));
1699
1700 // The body of the for loop has the same local mangling number as the
1701 // for-init-statement.
1702 // It will only be incremented if the body contains other things that would
1703 // normally increment the mangling number (like a compound statement).
1704 if (C99orCXXorObjC)
1705 getCurScope()->decrementMSLocalManglingNumber();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001706
Chris Lattner9075bd72006-08-10 04:59:57 +00001707 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001708 StmtResult Body(ParseStatement(TrailingElseLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001709
Chris Lattner8fb26252007-08-22 05:28:50 +00001710 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001711 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001712
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001713 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001714 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001715
1716 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001717 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001718
Richard Smith02e85f32011-04-14 22:09:26 +00001719 if (ForEach)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001720 return Actions.FinishObjCForCollectionStmt(ForEachStmt.get(),
1721 Body.get());
Mike Stump11289f42009-09-09 15:08:12 +00001722
Richard Smith02e85f32011-04-14 22:09:26 +00001723 if (ForRange)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001724 return Actions.FinishCXXForRangeStmt(ForRangeStmt.get(), Body.get());
Richard Smith02e85f32011-04-14 22:09:26 +00001725
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001726 return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.get(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001727 SecondPart, SecondVar, ThirdPart,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001728 T.getCloseLocation(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001729}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001730
Chris Lattner503fadc2006-08-10 05:45:44 +00001731/// ParseGotoStatement
1732/// jump-statement:
1733/// 'goto' identifier ';'
1734/// [GNU] 'goto' '*' expression ';'
1735///
1736/// Note: this lets the caller parse the end ';'.
1737///
Richard Smithc202b282012-04-14 00:33:13 +00001738StmtResult Parser::ParseGotoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001739 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001740 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001741
John McCalldadc5752010-08-24 06:29:42 +00001742 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001743 if (Tok.is(tok::identifier)) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +00001744 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1745 Tok.getLocation());
1746 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Chris Lattner503fadc2006-08-10 05:45:44 +00001747 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001748 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001749 // GNU indirect goto extension.
1750 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001751 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001752 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001753 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00001754 SkipUntil(tok::semi, StopBeforeMatch);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001755 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001756 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001757 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.get());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001758 } else {
Alp Tokerec543272013-12-24 09:48:30 +00001759 Diag(Tok, diag::err_expected) << tok::identifier;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001760 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001761 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001762
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001763 return Res;
Chris Lattner503fadc2006-08-10 05:45:44 +00001764}
1765
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001766/// ParseContinueStatement
1767/// jump-statement:
1768/// 'continue' ';'
1769///
1770/// Note: this lets the caller parse the end ';'.
1771///
Richard Smithc202b282012-04-14 00:33:13 +00001772StmtResult Parser::ParseContinueStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001773 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001774 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001775}
1776
1777/// ParseBreakStatement
1778/// jump-statement:
1779/// 'break' ';'
1780///
1781/// Note: this lets the caller parse the end ';'.
1782///
Richard Smithc202b282012-04-14 00:33:13 +00001783StmtResult Parser::ParseBreakStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001784 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001785 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001786}
1787
Chris Lattner503fadc2006-08-10 05:45:44 +00001788/// ParseReturnStatement
1789/// jump-statement:
1790/// 'return' expression[opt] ';'
Richard Smithc202b282012-04-14 00:33:13 +00001791StmtResult Parser::ParseReturnStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001792 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001793 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001794
John McCalldadc5752010-08-24 06:29:42 +00001795 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001796 if (Tok.isNot(tok::semi)) {
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001797 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001798 Actions.CodeCompleteReturn(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001799 cutOffParsing();
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001800 return StmtError();
1801 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001802
David Blaikiebbafb8a2012-03-11 07:00:24 +00001803 if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus) {
Douglas Gregore9e27d92011-03-11 23:10:44 +00001804 R = ParseInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00001805 if (R.isUsable())
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001806 Diag(R.get()->getLocStart(), getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00001807 diag::warn_cxx98_compat_generalized_initializer_lists :
1808 diag::ext_generalized_initializer_lists)
Douglas Gregore9e27d92011-03-11 23:10:44 +00001809 << R.get()->getSourceRange();
1810 } else
Nico Weber3ce01c32015-01-04 08:07:54 +00001811 R = ParseExpression();
Serge Pavlovf79bd5c2013-12-04 03:51:59 +00001812 if (R.isInvalid()) {
1813 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001814 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001815 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001816 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001817 return Actions.ActOnReturnStmt(ReturnLoc, R.get(), getCurScope());
Chris Lattner503fadc2006-08-10 05:45:44 +00001818}
Chris Lattner0116c472006-08-15 06:03:28 +00001819
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001820StmtResult Parser::ParsePragmaLoopHint(StmtVector &Stmts, bool OnlyStatement,
1821 SourceLocation *TrailingElseLoc,
1822 ParsedAttributesWithRange &Attrs) {
1823 // Create temporary attribute list.
1824 ParsedAttributesWithRange TempAttrs(AttrFactory);
1825
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001826 // Get loop hints and consume annotated token.
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001827 while (Tok.is(tok::annot_pragma_loop_hint)) {
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001828 LoopHint Hint;
1829 if (!HandlePragmaLoopHint(Hint))
1830 continue;
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001831
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001832 ArgsUnion ArgHints[] = {Hint.PragmaNameLoc, Hint.OptionLoc, Hint.StateLoc,
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001833 ArgsUnion(Hint.ValueExpr)};
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001834 TempAttrs.addNew(Hint.PragmaNameLoc->Ident, Hint.Range, nullptr,
1835 Hint.PragmaNameLoc->Loc, ArgHints, 4,
1836 AttributeList::AS_Pragma);
Aaron Ballmanb06b15a2014-06-06 12:40:24 +00001837 }
1838
1839 // Get the next statement.
1840 MaybeParseCXX11Attributes(Attrs);
1841
1842 StmtResult S = ParseStatementOrDeclarationAfterAttributes(
1843 Stmts, OnlyStatement, TrailingElseLoc, Attrs);
1844
1845 Attrs.takeAllFrom(TempAttrs);
1846 return S;
1847}
1848
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001849Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001850 assert(Tok.is(tok::l_brace));
1851 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001852
Argyrios Kyrtzidis44319182013-02-22 04:11:06 +00001853 if (SkipFunctionBodies && (!Decl || Actions.canSkipFunctionBody(Decl)) &&
Richard Smith1ab34b32012-11-19 21:13:18 +00001854 trySkippingFunctionBody()) {
Erik Verbruggen6e922512012-04-12 10:11:59 +00001855 BodyScope.Exit();
Argyrios Kyrtzidis1eb71a12012-12-06 18:59:10 +00001856 return Actions.ActOnSkippedFunctionBody(Decl);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001857 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001858
John McCallfaf5fb42010-08-26 23:41:50 +00001859 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1860 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001861
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001862 // Do not enter a scope for the brace, as the arguments are in the same scope
1863 // (the function body) as the body itself. Instead, just read the statement
1864 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00001865 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001866
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001867 // If the function body could not be parsed, make a bogus compoundstmt.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001868 if (FnBody.isInvalid()) {
1869 Sema::CompoundScopeRAII CompoundScope(Actions);
Robert Wilhelm27b2c9a32013-08-19 20:51:20 +00001870 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001871 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001872
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001873 BodyScope.Exit();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001874 return Actions.ActOnFinishFunctionBody(Decl, FnBody.get());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001875}
Sebastian Redlb219c902008-12-21 16:41:36 +00001876
Sebastian Redla7b98a72009-04-26 20:35:05 +00001877/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1878///
1879/// function-try-block:
1880/// 'try' ctor-initializer[opt] compound-statement handler-seq
1881///
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001882Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00001883 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1884 SourceLocation TryLoc = ConsumeToken();
1885
John McCallfaf5fb42010-08-26 23:41:50 +00001886 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1887 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00001888
1889 // Constructor initializer list?
1890 if (Tok.is(tok::colon))
1891 ParseConstructorInitializer(Decl);
Douglas Gregor5ca153f2011-09-07 20:36:12 +00001892 else
1893 Actions.ActOnDefaultCtorInitializers(Decl);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001894
Richard Smith1ab34b32012-11-19 21:13:18 +00001895 if (SkipFunctionBodies && Actions.canSkipFunctionBody(Decl) &&
1896 trySkippingFunctionBody()) {
Erik Verbruggen6e922512012-04-12 10:11:59 +00001897 BodyScope.Exit();
Argyrios Kyrtzidis1eb71a12012-12-06 18:59:10 +00001898 return Actions.ActOnSkippedFunctionBody(Decl);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001899 }
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001900
Sebastian Redld98ecd62009-04-26 21:08:36 +00001901 SourceLocation LBraceLoc = Tok.getLocation();
David Blaikie1c9c9042012-11-10 01:04:23 +00001902 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc, /*FnTry*/true));
Sebastian Redla7b98a72009-04-26 20:35:05 +00001903 // If we failed to parse the try-catch, we just give the function an empty
1904 // compound statement as the body.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001905 if (FnBody.isInvalid()) {
1906 Sema::CompoundScopeRAII CompoundScope(Actions);
Robert Wilhelm27b2c9a32013-08-19 20:51:20 +00001907 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001908 }
Sebastian Redla7b98a72009-04-26 20:35:05 +00001909
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001910 BodyScope.Exit();
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001911 return Actions.ActOnFinishFunctionBody(Decl, FnBody.get());
Sebastian Redla7b98a72009-04-26 20:35:05 +00001912}
1913
Erik Verbruggen6e922512012-04-12 10:11:59 +00001914bool Parser::trySkippingFunctionBody() {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001915 assert(Tok.is(tok::l_brace));
Erik Verbruggen6e922512012-04-12 10:11:59 +00001916 assert(SkipFunctionBodies &&
1917 "Should only be called when SkipFunctionBodies is enabled");
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001918
Argyrios Kyrtzidis289e4a32012-10-31 17:29:28 +00001919 if (!PP.isCodeCompletionEnabled()) {
1920 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001921 SkipUntil(tok::r_brace);
Argyrios Kyrtzidis289e4a32012-10-31 17:29:28 +00001922 return true;
1923 }
1924
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001925 // We're in code-completion mode. Skip parsing for all function bodies unless
1926 // the body contains the code-completion point.
1927 TentativeParsingAction PA(*this);
1928 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001929 if (SkipUntil(tok::r_brace, StopAtCodeCompletion)) {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001930 PA.Commit();
1931 return true;
1932 }
1933
1934 PA.Revert();
1935 return false;
1936}
1937
Sebastian Redlb219c902008-12-21 16:41:36 +00001938/// ParseCXXTryBlock - Parse a C++ try-block.
1939///
1940/// try-block:
1941/// 'try' compound-statement handler-seq
1942///
Richard Smithc202b282012-04-14 00:33:13 +00001943StmtResult Parser::ParseCXXTryBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00001944 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1945
1946 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001947 return ParseCXXTryBlockCommon(TryLoc);
1948}
1949
1950/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1951/// function-try-block.
1952///
1953/// try-block:
1954/// 'try' compound-statement handler-seq
1955///
1956/// function-try-block:
1957/// 'try' ctor-initializer[opt] compound-statement handler-seq
1958///
1959/// handler-seq:
1960/// handler handler-seq[opt]
1961///
John Wiegley1c0675e2011-04-28 01:08:34 +00001962/// [Borland] try-block:
1963/// 'try' compound-statement seh-except-block
Alp Tokerf6a24ce2013-12-05 16:25:25 +00001964/// 'try' compound-statement seh-finally-block
John Wiegley1c0675e2011-04-28 01:08:34 +00001965///
David Blaikie1c9c9042012-11-10 01:04:23 +00001966StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc, bool FnTry) {
Sebastian Redlb219c902008-12-21 16:41:36 +00001967 if (Tok.isNot(tok::l_brace))
Alp Tokerec543272013-12-24 09:48:30 +00001968 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
Richard Smithc202b282012-04-14 00:33:13 +00001969
Warren Huntf6be4cb2014-07-25 20:52:51 +00001970 StmtResult TryBlock(ParseCompoundStatement(/*isStmtExpr=*/false,
1971 Scope::DeclScope | Scope::TryScope |
1972 (FnTry ? Scope::FnTryCatchScope : 0)));
Sebastian Redlb219c902008-12-21 16:41:36 +00001973 if (TryBlock.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001974 return TryBlock;
Sebastian Redlb219c902008-12-21 16:41:36 +00001975
John Wiegley1c0675e2011-04-28 01:08:34 +00001976 // Borland allows SEH-handlers with 'try'
Chad Rosier67055f52012-07-10 21:35:27 +00001977
Richard Smithc202b282012-04-14 00:33:13 +00001978 if ((Tok.is(tok::identifier) &&
1979 Tok.getIdentifierInfo() == getSEHExceptKeyword()) ||
1980 Tok.is(tok::kw___finally)) {
John Wiegley1c0675e2011-04-28 01:08:34 +00001981 // TODO: Factor into common return ParseSEHHandlerCommon(...)
1982 StmtResult Handler;
Douglas Gregor60060d62011-10-21 03:57:52 +00001983 if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +00001984 SourceLocation Loc = ConsumeToken();
1985 Handler = ParseSEHExceptBlock(Loc);
1986 }
1987 else {
1988 SourceLocation Loc = ConsumeToken();
1989 Handler = ParseSEHFinallyBlock(Loc);
1990 }
1991 if(Handler.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001992 return Handler;
John McCall53fa7142010-12-24 02:08:15 +00001993
John Wiegley1c0675e2011-04-28 01:08:34 +00001994 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
1995 TryLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001996 TryBlock.get(),
Warren Huntf6be4cb2014-07-25 20:52:51 +00001997 Handler.get());
Sebastian Redlb219c902008-12-21 16:41:36 +00001998 }
John Wiegley1c0675e2011-04-28 01:08:34 +00001999 else {
Benjamin Kramerf0623432012-08-23 22:51:59 +00002000 StmtVector Handlers;
Richard Smithc2c8bb82013-10-15 01:34:54 +00002001
2002 // C++11 attributes can't appear here, despite this context seeming
2003 // statement-like.
2004 DiagnoseAndSkipCXX11Attributes();
Sebastian Redlb219c902008-12-21 16:41:36 +00002005
John Wiegley1c0675e2011-04-28 01:08:34 +00002006 if (Tok.isNot(tok::kw_catch))
2007 return StmtError(Diag(Tok, diag::err_expected_catch));
2008 while (Tok.is(tok::kw_catch)) {
David Blaikie1c9c9042012-11-10 01:04:23 +00002009 StmtResult Handler(ParseCXXCatchBlock(FnTry));
John Wiegley1c0675e2011-04-28 01:08:34 +00002010 if (!Handler.isInvalid())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002011 Handlers.push_back(Handler.get());
John Wiegley1c0675e2011-04-28 01:08:34 +00002012 }
2013 // Don't bother creating the full statement if we don't have any usable
2014 // handlers.
2015 if (Handlers.empty())
2016 return StmtError();
2017
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002018 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.get(), Handlers);
John Wiegley1c0675e2011-04-28 01:08:34 +00002019 }
Sebastian Redlb219c902008-12-21 16:41:36 +00002020}
2021
2022/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
2023///
Richard Smith1dba27c2013-01-29 09:02:09 +00002024/// handler:
2025/// 'catch' '(' exception-declaration ')' compound-statement
Sebastian Redlb219c902008-12-21 16:41:36 +00002026///
Richard Smith1dba27c2013-01-29 09:02:09 +00002027/// exception-declaration:
2028/// attribute-specifier-seq[opt] type-specifier-seq declarator
2029/// attribute-specifier-seq[opt] type-specifier-seq abstract-declarator[opt]
2030/// '...'
Sebastian Redlb219c902008-12-21 16:41:36 +00002031///
David Blaikie1c9c9042012-11-10 01:04:23 +00002032StmtResult Parser::ParseCXXCatchBlock(bool FnCatch) {
Sebastian Redlb219c902008-12-21 16:41:36 +00002033 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
2034
2035 SourceLocation CatchLoc = ConsumeToken();
2036
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002037 BalancedDelimiterTracker T(*this, tok::l_paren);
Alp Toker383d2c42014-01-01 03:08:43 +00002038 if (T.expectAndConsume())
Sebastian Redlb219c902008-12-21 16:41:36 +00002039 return StmtError();
2040
2041 // C++ 3.3.2p3:
2042 // The name in a catch exception-declaration is local to the handler and
2043 // shall not be redeclared in the outermost block of the handler.
David Blaikie1c9c9042012-11-10 01:04:23 +00002044 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope |
David Blaikie3403feb2012-11-13 18:51:45 +00002045 (FnCatch ? Scope::FnTryCatchScope : 0));
Sebastian Redlb219c902008-12-21 16:41:36 +00002046
2047 // exception-declaration is equivalent to '...' or a parameter-declaration
2048 // without default arguments.
Craig Topper161e4db2014-05-21 06:02:52 +00002049 Decl *ExceptionDecl = nullptr;
Sebastian Redlb219c902008-12-21 16:41:36 +00002050 if (Tok.isNot(tok::ellipsis)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002051 ParsedAttributesWithRange Attributes(AttrFactory);
2052 MaybeParseCXX11Attributes(Attributes);
2053
John McCall084e83d2011-03-24 11:26:52 +00002054 DeclSpec DS(AttrFactory);
Richard Smith1dba27c2013-01-29 09:02:09 +00002055 DS.takeAttributesFrom(Attributes);
2056
Sebastian Redl54c04d42008-12-22 19:15:10 +00002057 if (ParseCXXTypeSpecifierSeq(DS))
2058 return StmtError();
Richard Smith1dba27c2013-01-29 09:02:09 +00002059
Sebastian Redlb219c902008-12-21 16:41:36 +00002060 Declarator ExDecl(DS, Declarator::CXXCatchContext);
2061 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002062 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00002063 } else
2064 ConsumeToken();
2065
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002066 T.consumeClose();
2067 if (T.getCloseLocation().isInvalid())
Sebastian Redlb219c902008-12-21 16:41:36 +00002068 return StmtError();
2069
2070 if (Tok.isNot(tok::l_brace))
Alp Tokerec543272013-12-24 09:48:30 +00002071 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
Sebastian Redlb219c902008-12-21 16:41:36 +00002072
Alexis Hunt96d5c762009-11-21 08:43:09 +00002073 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
Richard Smithc202b282012-04-14 00:33:13 +00002074 StmtResult Block(ParseCompoundStatement());
Sebastian Redlb219c902008-12-21 16:41:36 +00002075 if (Block.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002076 return Block;
Sebastian Redlb219c902008-12-21 16:41:36 +00002077
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002078 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.get());
Sebastian Redlb219c902008-12-21 16:41:36 +00002079}
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002080
2081void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Douglas Gregor43edb322011-10-24 22:31:10 +00002082 IfExistsCondition Result;
Francois Picheta5b3fcb2011-05-07 17:30:27 +00002083 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002084 return;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00002085
Douglas Gregor43edb322011-10-24 22:31:10 +00002086 // Handle dependent statements by parsing the braces as a compound statement.
2087 // This is not the same behavior as Visual C++, which don't treat this as a
2088 // compound statement, but for Clang's type checking we can't have anything
2089 // inside these braces escaping to the surrounding code.
2090 if (Result.Behavior == IEB_Dependent) {
2091 if (!Tok.is(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +00002092 Diag(Tok, diag::err_expected) << tok::l_brace;
Richard Smithc202b282012-04-14 00:33:13 +00002093 return;
Douglas Gregor43edb322011-10-24 22:31:10 +00002094 }
Richard Smithc202b282012-04-14 00:33:13 +00002095
2096 StmtResult Compound = ParseCompoundStatement();
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002097 if (Compound.isInvalid())
2098 return;
Richard Smithc202b282012-04-14 00:33:13 +00002099
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002100 StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc,
2101 Result.IsIfExists,
Richard Smithc202b282012-04-14 00:33:13 +00002102 Result.SS,
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002103 Result.Name,
2104 Compound.get());
2105 if (DepResult.isUsable())
2106 Stmts.push_back(DepResult.get());
Douglas Gregor43edb322011-10-24 22:31:10 +00002107 return;
2108 }
Richard Smithc202b282012-04-14 00:33:13 +00002109
Douglas Gregor43edb322011-10-24 22:31:10 +00002110 BalancedDelimiterTracker Braces(*this, tok::l_brace);
2111 if (Braces.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +00002112 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002113 return;
2114 }
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002115
Douglas Gregor43edb322011-10-24 22:31:10 +00002116 switch (Result.Behavior) {
2117 case IEB_Parse:
2118 // Parse the statements below.
2119 break;
Chad Rosier67055f52012-07-10 21:35:27 +00002120
Douglas Gregor43edb322011-10-24 22:31:10 +00002121 case IEB_Dependent:
2122 llvm_unreachable("Dependent case handled above");
Chad Rosier67055f52012-07-10 21:35:27 +00002123
Douglas Gregor43edb322011-10-24 22:31:10 +00002124 case IEB_Skip:
2125 Braces.skipToEnd();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002126 return;
2127 }
2128
2129 // Condition is true, parse the statements.
2130 while (Tok.isNot(tok::r_brace)) {
2131 StmtResult R = ParseStatementOrDeclaration(Stmts, false);
2132 if (R.isUsable())
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002133 Stmts.push_back(R.get());
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002134 }
Douglas Gregor43edb322011-10-24 22:31:10 +00002135 Braces.consumeClose();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002136}