blob: c31216d0ae277bdbe2c44237df694e4b266ebbd1 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnerd167ca02009-12-10 00:21:05 +000016#include "RAIIObjectsForParser.h"
John McCallaeeacf72013-05-03 00:10:13 +000017#include "clang/AST/ASTContext.h"
Stephen Hinesc568f1e2014-07-21 00:47:37 -070018#include "clang/Basic/Attributes.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000019#include "clang/Basic/Diagnostic.h"
20#include "clang/Basic/PrettyStackTrace.h"
John McCall19510852010-08-20 18:27:03 +000021#include "clang/Sema/DeclSpec.h"
Stephen Hinesc568f1e2014-07-21 00:47:37 -070022#include "clang/Sema/LoopHint.h"
John McCallf312b1e2010-08-26 23:41:50 +000023#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall19510852010-08-20 18:27:03 +000024#include "clang/Sema/Scope.h"
Richard Smith05766812012-08-18 00:55:03 +000025#include "clang/Sema/TypoCorrection.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070026#include "llvm/ADT/SmallString.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027using namespace clang;
28
29//===----------------------------------------------------------------------===//
30// C99 6.8: Statements and Blocks.
31//===----------------------------------------------------------------------===//
32
Richard Smith961d0572013-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
Reid Spencer5f016e22007-07-11 17:01:13 +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 Kyrtzidisdcdd55f2008-09-07 18:58:01 +000060/// [C++] declaration-statement
Sebastian Redla0fd8652008-12-21 16:41:36 +000061/// [C++] try-block
John Wiegley28bbe4b2011-04-28 01:08:34 +000062/// [MS] seh-try-block
Fariborz Jahanianb384d322007-10-04 20:19:06 +000063/// [OBC] objc-throw-statement
64/// [OBC] objc-try-catch-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +000065/// [OBC] objc-synchronized-statement
Reid Spencer5f016e22007-07-11 17:01:13 +000066/// [GNU] asm-statement
67/// [OMP] openmp-construct [TODO]
68///
69/// labeled-statement:
70/// identifier ':' statement
71/// 'case' constant-expression ':' statement
72/// 'default' ':' statement
73///
74/// selection-statement:
75/// if-statement
76/// switch-statement
77///
78/// iteration-statement:
79/// while-statement
80/// do-statement
81/// for-statement
82///
83/// expression-statement:
84/// expression[opt] ';'
85///
86/// jump-statement:
87/// 'goto' identifier ';'
88/// 'continue' ';'
89/// 'break' ';'
90/// 'return' expression[opt] ';'
91/// [GNU] 'goto' '*' expression ';'
92///
Fariborz Jahanianb384d322007-10-04 20:19:06 +000093/// [OBC] objc-throw-statement:
94/// [OBC] '@' 'throw' expression ';'
Mike Stump1eb44332009-09-09 15:08:12 +000095/// [OBC] '@' 'throw' ';'
96///
John McCall60d7b3a2010-08-24 06:29:42 +000097StmtResult
Nico Weber5cb94a72011-12-22 23:26:17 +000098Parser::ParseStatementOrDeclaration(StmtVector &Stmts, bool OnlyStatement,
99 SourceLocation *TrailingElseLoc) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000100
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000101 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000102
Richard Smith534986f2012-04-14 00:33:13 +0000103 ParsedAttributesWithRange Attrs(AttrFactory);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700104 MaybeParseCXX11Attributes(Attrs, nullptr, /*MightBeObjCMessageSend*/ true);
Richard Smith534986f2012-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 Uhrain6243f622013-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
Stephen Hines651f13c2014-04-23 16:59:28 -0700134 bool ValidateCandidate(const TypoCorrection &candidate) override {
Kaelyn Uhrain6243f622013-09-27 19:40:12 +0000135 if (FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>())
Kaelyn Uhraina89ee572013-10-01 22:00:28 +0000136 return !candidate.getCorrectionSpecifier() || isa<ObjCIvarDecl>(FD);
Kaelyn Uhrain0f90ee02013-09-27 19:40:16 +0000137 if (NextToken.is(tok::equal))
138 return candidate.getCorrectionDeclAs<VarDecl>();
Kaelyn Uhrain2ceb67a2013-09-27 23:54:23 +0000139 if (NextToken.is(tok::period) &&
140 candidate.getCorrectionDeclAs<NamespaceDecl>())
141 return false;
Kaelyn Uhrain6243f622013-09-27 19:40:12 +0000142 return CorrectionCandidateCallback::ValidateCandidate(candidate);
143 }
144
145private:
146 Token NextToken;
147};
148}
149
Richard Smith534986f2012-04-14 00:33:13 +0000150StmtResult
151Parser::ParseStatementOrDeclarationAfterAttributes(StmtVector &Stmts,
152 bool OnlyStatement, SourceLocation *TrailingElseLoc,
153 ParsedAttributesWithRange &Attrs) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700154 const char *SemiError = nullptr;
Richard Smith534986f2012-04-14 00:33:13 +0000155 StmtResult Res;
Sean Huntbbd37c62009-11-21 08:43:09 +0000156
Reid Spencer5f016e22007-07-11 17:01:13 +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 Gregor312eadb2011-04-24 05:37:28 +0000160Retry:
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000161 tok::TokenKind Kind = Tok.getKind();
162 SourceLocation AtLoc;
163 switch (Kind) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000164 case tok::at: // May be a @try or @throw statement
165 {
Richard Smith534986f2012-04-14 00:33:13 +0000166 ProhibitAttributes(Attrs); // TODO: is it correct?
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000167 AtLoc = ConsumeToken(); // consume @
Sebastian Redl43bc2a02008-12-11 20:12:42 +0000168 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000169 }
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000170
Douglas Gregor791215b2009-09-21 20:51:25 +0000171 case tok::code_completion:
John McCallf312b1e2010-08-26 23:41:50 +0000172 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000173 cutOffParsing();
174 return StmtError();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000175
Douglas Gregor312eadb2011-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 Kyrtzidisb9f930d2008-07-12 21:04:42 +0000179 // identifier ':' statement
Richard Smith534986f2012-04-14 00:33:13 +0000180 return ParseLabeledStatement(Attrs);
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +0000181 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000182
Richard Smith05766812012-08-18 00:55:03 +0000183 // Look up the identifier, and typo-correct it to a keyword if it's not
184 // found.
Douglas Gregor3b887352011-04-27 04:48:22 +0000185 if (Next.isNot(tok::coloncolon)) {
Richard Smith05766812012-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.
Stephen Hines176edba2014-12-01 14:53:08 -0800188 if (TryAnnotateName(/*IsAddressOfOperand*/ false,
189 llvm::make_unique<StatementFilterCCC>(Next)) ==
190 ANK_Error) {
Douglas Gregor312eadb2011-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 Bataev8fe24752013-11-18 08:17:37 +0000193 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000194 if (Tok.is(tok::semi))
195 ConsumeToken();
196 return StmtError();
Richard Smith05766812012-08-18 00:55:03 +0000197 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000198
Richard Smith05766812012-08-18 00:55:03 +0000199 // If the identifier was typo-corrected, try again.
200 if (Tok.isNot(tok::identifier))
Douglas Gregor312eadb2011-04-24 05:37:28 +0000201 goto Retry;
Douglas Gregor312eadb2011-04-24 05:37:28 +0000202 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000203
Douglas Gregor312eadb2011-04-24 05:37:28 +0000204 // Fall through
205 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000206
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000207 default: {
David Blaikie4e4d0842012-03-11 07:00:24 +0000208 if ((getLangOpts().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000209 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Stephen Hines176edba2014-12-01 14:53:08 -0800210 DeclGroupPtrTy Decl = ParseDeclaration(Declarator::BlockContext,
Richard Smith534986f2012-04-14 00:33:13 +0000211 DeclEnd, Attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000212 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000213 }
214
215 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000216 Diag(Tok, diag::err_expected_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000217 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 }
Mike Stump1eb44332009-09-09 15:08:12 +0000219
Richard Smith534986f2012-04-14 00:33:13 +0000220 return ParseExprStatement();
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000221 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000222
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 case tok::kw_case: // C99 6.8.1: labeled-statement
Richard Smith534986f2012-04-14 00:33:13 +0000224 return ParseCaseStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +0000225 case tok::kw_default: // C99 6.8.1: labeled-statement
Richard Smith534986f2012-04-14 00:33:13 +0000226 return ParseDefaultStatement();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000227
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 case tok::l_brace: // C99 6.8.2: compound-statement
Richard Smith534986f2012-04-14 00:33:13 +0000229 return ParseCompoundStatement();
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000230 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
Argyrios Kyrtzidise2ca8282011-09-01 21:53:45 +0000231 bool HasLeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
232 return Actions.ActOnNullStmt(ConsumeToken(), HasLeadingEmptyMacro);
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000233 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000234
Reid Spencer5f016e22007-07-11 17:01:13 +0000235 case tok::kw_if: // C99 6.8.4.1: if-statement
Richard Smith534986f2012-04-14 00:33:13 +0000236 return ParseIfStatement(TrailingElseLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Richard Smith534986f2012-04-14 00:33:13 +0000238 return ParseSwitchStatement(TrailingElseLoc);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000239
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 case tok::kw_while: // C99 6.8.5.1: while-statement
Richard Smith534986f2012-04-14 00:33:13 +0000241 return ParseWhileStatement(TrailingElseLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000242 case tok::kw_do: // C99 6.8.5.2: do-statement
Richard Smith534986f2012-04-14 00:33:13 +0000243 Res = ParseDoStatement();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000244 SemiError = "do/while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000245 break;
246 case tok::kw_for: // C99 6.8.5.3: for-statement
Richard Smith534986f2012-04-14 00:33:13 +0000247 return ParseForStatement(TrailingElseLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000248
249 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Richard Smith534986f2012-04-14 00:33:13 +0000250 Res = ParseGotoStatement();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000251 SemiError = "goto";
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 break;
253 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Richard Smith534986f2012-04-14 00:33:13 +0000254 Res = ParseContinueStatement();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000255 SemiError = "continue";
Reid Spencer5f016e22007-07-11 17:01:13 +0000256 break;
257 case tok::kw_break: // C99 6.8.6.3: break-statement
Richard Smith534986f2012-04-14 00:33:13 +0000258 Res = ParseBreakStatement();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000259 SemiError = "break";
Reid Spencer5f016e22007-07-11 17:01:13 +0000260 break;
261 case tok::kw_return: // C99 6.8.6.4: return-statement
Richard Smith534986f2012-04-14 00:33:13 +0000262 Res = ParseReturnStatement();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000263 SemiError = "return";
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 break;
Sebastian Redl61364dd2008-12-11 19:30:53 +0000265
Sebastian Redla0fd8652008-12-21 16:41:36 +0000266 case tok::kw_asm: {
Richard Smith534986f2012-04-14 00:33:13 +0000267 ProhibitAttributes(Attrs);
Steve Naroffd62701b2008-02-07 03:50:06 +0000268 bool msAsm = false;
269 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidisbf8cafa2010-11-02 02:33:08 +0000270 Res = Actions.ActOnFinishFullStmt(Res.get());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000271 if (msAsm) return Res;
Chris Lattner6869d8e2009-06-14 00:07:48 +0000272 SemiError = "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +0000273 break;
274 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000275
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700276 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 Redla0fd8652008-12-21 16:41:36 +0000284 case tok::kw_try: // C++ 15: try-block
Richard Smith534986f2012-04-14 00:33:13 +0000285 return ParseCXXTryBlock();
John Wiegley28bbe4b2011-04-28 01:08:34 +0000286
287 case tok::kw___try:
Richard Smith534986f2012-04-14 00:33:13 +0000288 ProhibitAttributes(Attrs); // TODO: is it correct?
289 return ParseSEHTryBlock();
Eli Friedmanaa5ab262012-02-23 23:47:16 +0000290
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700291 case tok::kw___leave:
292 Res = ParseSEHLeaveStatement();
293 SemiError = "__leave";
294 break;
295
Eli Friedmanaa5ab262012-02-23 23:47:16 +0000296 case tok::annot_pragma_vis:
Richard Smith534986f2012-04-14 00:33:13 +0000297 ProhibitAttributes(Attrs);
Eli Friedmanaa5ab262012-02-23 23:47:16 +0000298 HandlePragmaVisibility();
299 return StmtEmpty();
300
301 case tok::annot_pragma_pack:
Richard Smith534986f2012-04-14 00:33:13 +0000302 ProhibitAttributes(Attrs);
Eli Friedmanaa5ab262012-02-23 23:47:16 +0000303 HandlePragmaPack();
304 return StmtEmpty();
Eli Friedman9595c7e2012-10-04 02:36:51 +0000305
Eli Friedman8b2bfdd2012-10-09 22:46:54 +0000306 case tok::annot_pragma_msstruct:
307 ProhibitAttributes(Attrs);
308 HandlePragmaMSStruct();
309 return StmtEmpty();
310
Eli Friedman3ef38ee2012-10-08 23:52:38 +0000311 case tok::annot_pragma_align:
312 ProhibitAttributes(Attrs);
313 HandlePragmaAlign();
314 return StmtEmpty();
315
Eli Friedman8b2bfdd2012-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 Friedman9595c7e2012-10-04 02:36:51 +0000331 case tok::annot_pragma_fp_contract:
Richard Smithaed01162013-11-15 21:10:54 +0000332 ProhibitAttributes(Attrs);
Lang Hames860022c2012-10-21 01:10:01 +0000333 Diag(Tok, diag::err_pragma_fp_contract_scope);
334 ConsumeToken();
335 return StmtError();
336
Eli Friedman9595c7e2012-10-04 02:36:51 +0000337 case tok::annot_pragma_opencl_extension:
338 ProhibitAttributes(Attrs);
339 HandlePragmaOpenCLExtension();
340 return StmtEmpty();
Alexey Bataevc6400582013-03-22 06:34:35 +0000341
Tareq A. Siraj85192c72013-04-16 18:41:26 +0000342 case tok::annot_pragma_captured:
Richard Smith175d4172013-09-16 21:17:44 +0000343 ProhibitAttributes(Attrs);
Tareq A. Siraj85192c72013-04-16 18:41:26 +0000344 return HandlePragmaCaptured();
345
Alexey Bataevc6400582013-03-22 06:34:35 +0000346 case tok::annot_pragma_openmp:
Richard Smith175d4172013-09-16 21:17:44 +0000347 ProhibitAttributes(Attrs);
Stephen Hines176edba2014-12-01 14:53:08 -0800348 return ParseOpenMPDeclarativeOrExecutableDirective(!OnlyStatement);
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000349
Stephen Hines651f13c2014-04-23 16:59:28 -0700350 case tok::annot_pragma_ms_pointers_to_members:
351 ProhibitAttributes(Attrs);
352 HandlePragmaMSPointersToMembers();
353 return StmtEmpty();
354
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700355 case tok::annot_pragma_ms_pragma:
356 ProhibitAttributes(Attrs);
357 HandlePragmaMSPragma();
358 return StmtEmpty();
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700359
360 case tok::annot_pragma_loop_hint:
361 ProhibitAttributes(Attrs);
362 return ParsePragmaLoopHint(Stmts, OnlyStatement, TrailingElseLoc, Attrs);
Sebastian Redla0fd8652008-12-21 16:41:36 +0000363 }
364
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 // If we reached this code, the statement must end in a semicolon.
Stephen Hines651f13c2014-04-23 16:59:28 -0700366 if (!TryConsumeToken(tok::semi) && !Res.isInvalid()) {
Chris Lattner7b3684a2009-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 Lattner19504402008-11-13 18:52:53 +0000371 // Skip until we see a } or ;, but don't eat it.
Alexey Bataev8fe24752013-11-18 08:17:37 +0000372 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Reid Spencer5f016e22007-07-11 17:01:13 +0000373 }
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000375 return Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000376}
377
Douglas Gregor312eadb2011-04-24 05:37:28 +0000378/// \brief Parse an expression statement.
Richard Smith534986f2012-04-14 00:33:13 +0000379StmtResult Parser::ParseExprStatement() {
Douglas Gregor312eadb2011-04-24 05:37:28 +0000380 // If a case keyword is missing, this is where it should be inserted.
381 Token OldToken = Tok;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000382
Douglas Gregor312eadb2011-04-24 05:37:28 +0000383 // expression[opt] ';'
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000384 ExprResult Expr(ParseExpression());
Douglas Gregor312eadb2011-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 Bataev8fe24752013-11-18 08:17:37 +0000389 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000390 if (Tok.is(tok::semi))
391 ConsumeToken();
John McCallb760f112013-03-22 02:10:40 +0000392 return Actions.ActOnExprStmtError();
Douglas Gregor312eadb2011-04-24 05:37:28 +0000393 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000394
Douglas Gregor312eadb2011-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 Takumia789ca92011-10-08 11:31:46 +0000401
Douglas Gregor312eadb2011-04-24 05:37:28 +0000402 // Recover parsing as a case statement.
Richard Smith534986f2012-04-14 00:33:13 +0000403 return ParseCaseStatement(/*MissingCase=*/true, Expr);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000404 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000405
Douglas Gregor312eadb2011-04-24 05:37:28 +0000406 // Otherwise, eat the semicolon.
407 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith41956372013-01-14 22:39:08 +0000408 return Actions.ActOnExprStmt(Expr);
John Wiegley28bbe4b2011-04-28 01:08:34 +0000409}
Douglas Gregor312eadb2011-04-24 05:37:28 +0000410
John Wiegley28bbe4b2011-04-28 01:08:34 +0000411/// ParseSEHTryBlockCommon
412///
413/// seh-try-block:
414/// '__try' compound-statement seh-handler
415///
416/// seh-handler:
417/// seh-except-block
418/// seh-finally-block
419///
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700420StmtResult Parser::ParseSEHTryBlock() {
421 assert(Tok.is(tok::kw___try) && "Expected '__try'");
422 SourceLocation TryLoc = ConsumeToken();
423
424 if (Tok.isNot(tok::l_brace))
Stephen Hines651f13c2014-04-23 16:59:28 -0700425 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
John Wiegley28bbe4b2011-04-28 01:08:34 +0000426
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700427 StmtResult TryBlock(ParseCompoundStatement(/*isStmtExpr=*/false,
428 Scope::DeclScope | Scope::SEHTryScope));
John Wiegley28bbe4b2011-04-28 01:08:34 +0000429 if(TryBlock.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000430 return TryBlock;
John Wiegley28bbe4b2011-04-28 01:08:34 +0000431
432 StmtResult Handler;
Richard Smith534986f2012-04-14 00:33:13 +0000433 if (Tok.is(tok::identifier) &&
Douglas Gregorb57791e2011-10-21 03:57:52 +0000434 Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley28bbe4b2011-04-28 01:08:34 +0000435 SourceLocation Loc = ConsumeToken();
436 Handler = ParseSEHExceptBlock(Loc);
437 } else if (Tok.is(tok::kw___finally)) {
438 SourceLocation Loc = ConsumeToken();
439 Handler = ParseSEHFinallyBlock(Loc);
440 } else {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700441 return StmtError(Diag(Tok, diag::err_seh_expected_handler));
John Wiegley28bbe4b2011-04-28 01:08:34 +0000442 }
443
444 if(Handler.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000445 return Handler;
John Wiegley28bbe4b2011-04-28 01:08:34 +0000446
447 return Actions.ActOnSEHTryBlock(false /* IsCXXTry */,
448 TryLoc,
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700449 TryBlock.get(),
450 Handler.get());
John Wiegley28bbe4b2011-04-28 01:08:34 +0000451}
452
453/// ParseSEHExceptBlock - Handle __except
454///
455/// seh-except-block:
456/// '__except' '(' seh-filter-expression ')' compound-statement
457///
458StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) {
459 PoisonIdentifierRAIIObject raii(Ident__exception_code, false),
460 raii2(Ident___exception_code, false),
461 raii3(Ident_GetExceptionCode, false);
462
Stephen Hines651f13c2014-04-23 16:59:28 -0700463 if (ExpectAndConsume(tok::l_paren))
John Wiegley28bbe4b2011-04-28 01:08:34 +0000464 return StmtError();
465
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700466 ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope |
467 Scope::SEHExceptScope);
John Wiegley28bbe4b2011-04-28 01:08:34 +0000468
David Blaikie4e4d0842012-03-11 07:00:24 +0000469 if (getLangOpts().Borland) {
Francois Pichetd7f02df2011-04-28 03:14:31 +0000470 Ident__exception_info->setIsPoisoned(false);
471 Ident___exception_info->setIsPoisoned(false);
472 Ident_GetExceptionInfo->setIsPoisoned(false);
473 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700474
475 ExprResult FilterExpr;
476 {
477 ParseScopeFlags FilterScope(this, getCurScope()->getFlags() |
478 Scope::SEHFilterScope);
Pirama Arumuga Nainar58878f82015-05-06 11:48:57 -0700479 FilterExpr = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700480 }
Francois Pichetd7f02df2011-04-28 03:14:31 +0000481
David Blaikie4e4d0842012-03-11 07:00:24 +0000482 if (getLangOpts().Borland) {
Francois Pichetd7f02df2011-04-28 03:14:31 +0000483 Ident__exception_info->setIsPoisoned(true);
484 Ident___exception_info->setIsPoisoned(true);
485 Ident_GetExceptionInfo->setIsPoisoned(true);
486 }
John Wiegley28bbe4b2011-04-28 01:08:34 +0000487
488 if(FilterExpr.isInvalid())
489 return StmtError();
490
Stephen Hines651f13c2014-04-23 16:59:28 -0700491 if (ExpectAndConsume(tok::r_paren))
John Wiegley28bbe4b2011-04-28 01:08:34 +0000492 return StmtError();
493
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700494 if (Tok.isNot(tok::l_brace))
495 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
496
Richard Smith534986f2012-04-14 00:33:13 +0000497 StmtResult Block(ParseCompoundStatement());
John Wiegley28bbe4b2011-04-28 01:08:34 +0000498
499 if(Block.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000500 return Block;
John Wiegley28bbe4b2011-04-28 01:08:34 +0000501
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700502 return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.get(), Block.get());
John Wiegley28bbe4b2011-04-28 01:08:34 +0000503}
504
505/// ParseSEHFinallyBlock - Handle __finally
506///
507/// seh-finally-block:
508/// '__finally' compound-statement
509///
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700510StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyLoc) {
John Wiegley28bbe4b2011-04-28 01:08:34 +0000511 PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
512 raii2(Ident___abnormal_termination, false),
513 raii3(Ident_AbnormalTermination, false);
514
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700515 if (Tok.isNot(tok::l_brace))
516 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
517
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700518 ParseScope FinallyScope(this, 0);
519 Actions.ActOnStartSEHFinallyBlock();
John Wiegley28bbe4b2011-04-28 01:08:34 +0000520
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700521 StmtResult Block(ParseCompoundStatement());
522 if(Block.isInvalid()) {
523 Actions.ActOnAbortSEHFinallyBlock();
524 return Block;
525 }
526
527 return Actions.ActOnFinishSEHFinallyBlock(FinallyLoc, Block.get());
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700528}
529
530/// Handle __leave
531///
532/// seh-leave-statement:
533/// '__leave' ';'
534///
535StmtResult Parser::ParseSEHLeaveStatement() {
536 SourceLocation LeaveLoc = ConsumeToken(); // eat the '__leave'.
537 return Actions.ActOnSEHLeaveStmt(LeaveLoc, getCurScope());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000538}
539
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000540/// ParseLabeledStatement - We have an identifier and a ':' after it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000541///
542/// labeled-statement:
543/// identifier ':' statement
544/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000545///
Richard Smith534986f2012-04-14 00:33:13 +0000546StmtResult Parser::ParseLabeledStatement(ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000547 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
548 "Not an identifier!");
549
550 Token IdentTok = Tok; // Save the whole token.
551 ConsumeToken(); // eat the identifier.
552
553 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000554
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000555 // identifier ':' statement
556 SourceLocation ColonLoc = ConsumeToken();
557
Richard Smith93982a72013-11-15 22:45:29 +0000558 // Read label attributes, if present.
559 StmtResult SubStmt;
560 if (Tok.is(tok::kw___attribute)) {
561 ParsedAttributesWithRange TempAttrs(AttrFactory);
562 ParseGNUAttributes(TempAttrs);
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000563
Richard Smith93982a72013-11-15 22:45:29 +0000564 // In C++, GNU attributes only apply to the label if they are followed by a
565 // semicolon, to disambiguate label attributes from attributes on a labeled
566 // declaration.
567 //
568 // This doesn't quite match what GCC does; if the attribute list is empty
569 // and followed by a semicolon, GCC will reject (it appears to parse the
570 // attributes as part of a statement in that case). That looks like a bug.
571 if (!getLangOpts().CPlusPlus || Tok.is(tok::semi))
572 attrs.takeAllFrom(TempAttrs);
573 else if (isDeclarationStatement()) {
574 StmtVector Stmts;
575 // FIXME: We should do this whether or not we have a declaration
576 // statement, but that doesn't work correctly (because ProhibitAttributes
577 // can't handle GNU attributes), so only call it in the one case where
578 // GNU attributes are allowed.
579 SubStmt = ParseStatementOrDeclarationAfterAttributes(
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700580 Stmts, /*OnlyStmts*/ true, nullptr, TempAttrs);
Richard Smith93982a72013-11-15 22:45:29 +0000581 if (!TempAttrs.empty() && !SubStmt.isInvalid())
582 SubStmt = Actions.ProcessStmtAttributes(
583 SubStmt.get(), TempAttrs.getList(), TempAttrs.Range);
584 } else {
Stephen Hines651f13c2014-04-23 16:59:28 -0700585 Diag(Tok, diag::err_expected_after) << "__attribute__" << tok::semi;
Richard Smith93982a72013-11-15 22:45:29 +0000586 }
587 }
588
589 // If we've not parsed a statement yet, parse one now.
590 if (!SubStmt.isInvalid() && !SubStmt.isUsable())
591 SubStmt = ParseStatement();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000592
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000593 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000594 if (SubStmt.isInvalid())
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000595 SubStmt = Actions.ActOnNullStmt(ColonLoc);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000596
Chris Lattner337e5502011-02-18 01:27:55 +0000597 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
598 IdentTok.getLocation());
Richard Smith534986f2012-04-14 00:33:13 +0000599 if (AttributeList *Attrs = attrs.getList()) {
Chris Lattner337e5502011-02-18 01:27:55 +0000600 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
Richard Smith534986f2012-04-14 00:33:13 +0000601 attrs.clear();
602 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000603
Chris Lattner337e5502011-02-18 01:27:55 +0000604 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
605 SubStmt.get());
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000606}
Reid Spencer5f016e22007-07-11 17:01:13 +0000607
608/// ParseCaseStatement
609/// labeled-statement:
610/// 'case' constant-expression ':' statement
611/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
612///
Richard Smith534986f2012-04-14 00:33:13 +0000613StmtResult Parser::ParseCaseStatement(bool MissingCase, ExprResult Expr) {
Richard Smith46f11102011-04-21 22:48:40 +0000614 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Chris Lattner24e1e702009-03-04 04:23:07 +0000616 // It is very very common for code to contain many case statements recursively
617 // nested, as in (but usually without indentation):
618 // case 1:
619 // case 2:
620 // case 3:
621 // case 4:
622 // case 5: etc.
623 //
624 // Parsing this naively works, but is both inefficient and can cause us to run
625 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner26140c62009-03-04 18:24:58 +0000626 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner24e1e702009-03-04 04:23:07 +0000627 // but all the grossness is constrained to ParseCaseStatement (and some
Richard Smith93982a72013-11-15 22:45:29 +0000628 // weirdness in the actions), so this is just local grossness :).
Mike Stump1eb44332009-09-09 15:08:12 +0000629
Chris Lattner24e1e702009-03-04 04:23:07 +0000630 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
631 // example above.
John McCall60d7b3a2010-08-24 06:29:42 +0000632 StmtResult TopLevelCase(true);
Mike Stump1eb44332009-09-09 15:08:12 +0000633
Chris Lattner24e1e702009-03-04 04:23:07 +0000634 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
635 // gets updated each time a new case is parsed, and whose body is unset so
636 // far. When parsing 'case 4', this is the 'case 3' node.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700637 Stmt *DeepestParsedCaseStmt = nullptr;
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Chris Lattner24e1e702009-03-04 04:23:07 +0000639 // While we have case statements, eat and stack them.
David Majnemer0e1e69c2011-06-13 05:50:12 +0000640 SourceLocation ColonLoc;
Chris Lattner24e1e702009-03-04 04:23:07 +0000641 do {
Richard Trieubb9b80c2011-04-21 21:44:26 +0000642 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
643 ConsumeToken(); // eat the 'case'.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700644 ColonLoc = SourceLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000646 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000647 Actions.CodeCompleteCase(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000648 cutOffParsing();
649 return StmtError();
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000650 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000651
Chris Lattner6fb09c82009-12-10 00:38:54 +0000652 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
653 /// Disable this form of error recovery while we're parsing the case
654 /// expression.
655 ColonProtectionRAIIObject ColonProtection(*this);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000656
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700657 ExprResult LHS;
658 if (!MissingCase) {
659 LHS = ParseConstantExpression();
Stephen Hines176edba2014-12-01 14:53:08 -0800660 if (!getLangOpts().CPlusPlus11) {
661 LHS = Actions.CorrectDelayedTyposInExpr(LHS, [this](class Expr *E) {
662 return Actions.VerifyIntegerConstantExpression(E);
663 });
664 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700665 if (LHS.isInvalid()) {
666 // If constant-expression is parsed unsuccessfully, recover by skipping
667 // current case statement (moving to the colon that ends it).
668 if (SkipUntil(tok::colon, tok::r_brace, StopAtSemi | StopBeforeMatch)) {
669 TryConsumeToken(tok::colon, ColonLoc);
670 continue;
671 }
672 return StmtError();
673 }
674 } else {
675 LHS = Expr;
676 MissingCase = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000677 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000678
Chris Lattner24e1e702009-03-04 04:23:07 +0000679 // GNU case range extension.
680 SourceLocation DotDotDotLoc;
John McCall60d7b3a2010-08-24 06:29:42 +0000681 ExprResult RHS;
Stephen Hines651f13c2014-04-23 16:59:28 -0700682 if (TryConsumeToken(tok::ellipsis, DotDotDotLoc)) {
683 Diag(DotDotDotLoc, diag::ext_gnu_case_range);
Chris Lattner24e1e702009-03-04 04:23:07 +0000684 RHS = ParseConstantExpression();
685 if (RHS.isInvalid()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700686 if (SkipUntil(tok::colon, tok::r_brace, StopAtSemi | StopBeforeMatch)) {
687 TryConsumeToken(tok::colon, ColonLoc);
688 continue;
689 }
Chris Lattner24e1e702009-03-04 04:23:07 +0000690 return StmtError();
691 }
692 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000693
Chris Lattner6fb09c82009-12-10 00:38:54 +0000694 ColonProtection.restore();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000695
Stephen Hines651f13c2014-04-23 16:59:28 -0700696 if (TryConsumeToken(tok::colon, ColonLoc)) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700697 } else if (TryConsumeToken(tok::semi, ColonLoc) ||
698 TryConsumeToken(tok::coloncolon, ColonLoc)) {
699 // Treat "case blah;" or "case blah::" as a typo for "case blah:".
Stephen Hines651f13c2014-04-23 16:59:28 -0700700 Diag(ColonLoc, diag::err_expected_after)
701 << "'case'" << tok::colon
702 << FixItHint::CreateReplacement(ColonLoc, ":");
John McCallf6a3ab02011-01-22 09:28:32 +0000703 } else {
Douglas Gregor662a4822010-12-23 22:56:40 +0000704 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
Stephen Hines651f13c2014-04-23 16:59:28 -0700705 Diag(ExpectedLoc, diag::err_expected_after)
706 << "'case'" << tok::colon
707 << FixItHint::CreateInsertion(ExpectedLoc, ":");
Douglas Gregor662a4822010-12-23 22:56:40 +0000708 ColonLoc = ExpectedLoc;
Chris Lattner24e1e702009-03-04 04:23:07 +0000709 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000710
John McCall60d7b3a2010-08-24 06:29:42 +0000711 StmtResult Case =
John McCall9ae2f072010-08-23 23:25:46 +0000712 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
713 RHS.get(), ColonLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Chris Lattner24e1e702009-03-04 04:23:07 +0000715 // If we had a sema error parsing this case, then just ignore it and
716 // continue parsing the sub-stmt.
717 if (Case.isInvalid()) {
718 if (TopLevelCase.isInvalid()) // No parsed case stmts.
719 return ParseStatement();
720 // Otherwise, just don't add it as a nested case.
721 } else {
722 // If this is the first case statement we parsed, it becomes TopLevelCase.
723 // Otherwise we link it into the current chain.
John McCallca0408f2010-08-23 06:44:23 +0000724 Stmt *NextDeepest = Case.get();
Chris Lattner24e1e702009-03-04 04:23:07 +0000725 if (TopLevelCase.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000726 TopLevelCase = Case;
Chris Lattner24e1e702009-03-04 04:23:07 +0000727 else
John McCall9ae2f072010-08-23 23:25:46 +0000728 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner24e1e702009-03-04 04:23:07 +0000729 DeepestParsedCaseStmt = NextDeepest;
730 }
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Chris Lattner24e1e702009-03-04 04:23:07 +0000732 // Handle all case statements.
733 } while (Tok.is(tok::kw_case));
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Chris Lattner24e1e702009-03-04 04:23:07 +0000735 // If we found a non-case statement, start by parsing it.
John McCall60d7b3a2010-08-24 06:29:42 +0000736 StmtResult SubStmt;
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Chris Lattner24e1e702009-03-04 04:23:07 +0000738 if (Tok.isNot(tok::r_brace)) {
739 SubStmt = ParseStatement();
740 } else {
741 // Nicely diagnose the common error "switch (X) { case 4: }", which is
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700742 // not valid. If ColonLoc doesn't point to a valid text location, there was
743 // another parsing error, so avoid producing extra diagnostics.
744 if (ColonLoc.isValid()) {
745 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
746 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
747 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
748 }
749 SubStmt = StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000750 }
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Chris Lattner24e1e702009-03-04 04:23:07 +0000752 // Install the body into the most deeply-nested case.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700753 if (DeepestParsedCaseStmt) {
754 // Broken sub-stmt shouldn't prevent forming the case statement properly.
755 if (SubStmt.isInvalid())
756 SubStmt = Actions.ActOnNullStmt(SourceLocation());
757 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
758 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000759
Chris Lattner24e1e702009-03-04 04:23:07 +0000760 // Return the top level parsed statement tree.
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000761 return TopLevelCase;
Reid Spencer5f016e22007-07-11 17:01:13 +0000762}
763
764/// ParseDefaultStatement
765/// labeled-statement:
766/// 'default' ':' statement
767/// Note that this does not parse the 'statement' at the end.
768///
Richard Smith534986f2012-04-14 00:33:13 +0000769StmtResult Parser::ParseDefaultStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000770 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000771 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
772
Douglas Gregor662a4822010-12-23 22:56:40 +0000773 SourceLocation ColonLoc;
Stephen Hines651f13c2014-04-23 16:59:28 -0700774 if (TryConsumeToken(tok::colon, ColonLoc)) {
775 } else if (TryConsumeToken(tok::semi, ColonLoc)) {
776 // Treat "default;" as a typo for "default:".
777 Diag(ColonLoc, diag::err_expected_after)
778 << "'default'" << tok::colon
779 << FixItHint::CreateReplacement(ColonLoc, ":");
John McCallf6a3ab02011-01-22 09:28:32 +0000780 } else {
Douglas Gregor662a4822010-12-23 22:56:40 +0000781 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
Stephen Hines651f13c2014-04-23 16:59:28 -0700782 Diag(ExpectedLoc, diag::err_expected_after)
783 << "'default'" << tok::colon
784 << FixItHint::CreateInsertion(ExpectedLoc, ":");
Douglas Gregor662a4822010-12-23 22:56:40 +0000785 ColonLoc = ExpectedLoc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000786 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000787
Richard Smith85b29a42012-02-17 01:35:32 +0000788 StmtResult SubStmt;
789
790 if (Tok.isNot(tok::r_brace)) {
791 SubStmt = ParseStatement();
792 } else {
793 // Diagnose the common error "switch (X) {... default: }", which is
794 // not valid.
David Majnemer63f04ab2011-06-14 15:24:38 +0000795 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
Richard Smith85b29a42012-02-17 01:35:32 +0000796 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
797 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
798 SubStmt = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000799 }
800
Richard Smith85b29a42012-02-17 01:35:32 +0000801 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000802 if (SubStmt.isInvalid())
Richard Smith85b29a42012-02-17 01:35:32 +0000803 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000804
Sebastian Redl117054a2008-12-28 16:13:43 +0000805 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000806 SubStmt.get(), getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +0000807}
808
Richard Smith534986f2012-04-14 00:33:13 +0000809StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
810 return ParseCompoundStatement(isStmtExpr, Scope::DeclScope);
Douglas Gregorbca01b42011-07-06 22:04:06 +0000811}
Reid Spencer5f016e22007-07-11 17:01:13 +0000812
813/// ParseCompoundStatement - Parse a "{}" block.
814///
815/// compound-statement: [C99 6.8.2]
816/// { block-item-list[opt] }
817/// [GNU] { label-declarations block-item-list } [TODO]
818///
819/// block-item-list:
820/// block-item
821/// block-item-list block-item
822///
823/// block-item:
824/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000825/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000826/// statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000827///
828/// [GNU] label-declarations:
829/// [GNU] label-declaration
830/// [GNU] label-declarations label-declaration
831///
832/// [GNU] label-declaration:
833/// [GNU] '__label__' identifier-list ';'
834///
Richard Smith534986f2012-04-14 00:33:13 +0000835StmtResult Parser::ParseCompoundStatement(bool isStmtExpr,
Douglas Gregorbca01b42011-07-06 22:04:06 +0000836 unsigned ScopeFlags) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000837 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000838
Chris Lattner31e05722007-08-26 06:24:45 +0000839 // Enter a scope to hold everything within the compound stmt. Compound
840 // statements can always hold declarations.
Douglas Gregorbca01b42011-07-06 22:04:06 +0000841 ParseScope CompoundScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000842
843 // Parse the statements in the body.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000844 return ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000845}
846
Lang Hamesa60d21d2012-11-03 22:29:05 +0000847/// Parse any pragmas at the start of the compound expression. We handle these
848/// separately since some pragmas (FP_CONTRACT) must appear before any C
849/// statement in the compound, but may be intermingled with other pragmas.
850void Parser::ParseCompoundStatementLeadingPragmas() {
851 bool checkForPragmas = true;
852 while (checkForPragmas) {
853 switch (Tok.getKind()) {
854 case tok::annot_pragma_vis:
855 HandlePragmaVisibility();
856 break;
857 case tok::annot_pragma_pack:
858 HandlePragmaPack();
859 break;
860 case tok::annot_pragma_msstruct:
861 HandlePragmaMSStruct();
862 break;
863 case tok::annot_pragma_align:
864 HandlePragmaAlign();
865 break;
866 case tok::annot_pragma_weak:
867 HandlePragmaWeak();
868 break;
869 case tok::annot_pragma_weakalias:
870 HandlePragmaWeakAlias();
871 break;
872 case tok::annot_pragma_redefine_extname:
873 HandlePragmaRedefineExtname();
874 break;
875 case tok::annot_pragma_opencl_extension:
876 HandlePragmaOpenCLExtension();
877 break;
878 case tok::annot_pragma_fp_contract:
879 HandlePragmaFPContract();
880 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700881 case tok::annot_pragma_ms_pointers_to_members:
882 HandlePragmaMSPointersToMembers();
883 break;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700884 case tok::annot_pragma_ms_pragma:
885 HandlePragmaMSPragma();
886 break;
Lang Hamesa60d21d2012-11-03 22:29:05 +0000887 default:
888 checkForPragmas = false;
889 break;
890 }
891 }
892
893}
894
Reid Spencer5f016e22007-07-11 17:01:13 +0000895/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000896/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000897/// consume the '}' at the end of the block. It does not manipulate the scope
898/// stack.
John McCall60d7b3a2010-08-24 06:29:42 +0000899StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump1eb44332009-09-09 15:08:12 +0000900 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerae50fa02009-03-05 00:00:31 +0000901 Tok.getLocation(),
902 "in compound statement ('{}')");
Lang Hamesbe9af122012-10-02 04:45:10 +0000903
904 // Record the state of the FP_CONTRACT pragma, restore on leaving the
905 // compound statement.
906 Sema::FPContractStateRAII SaveFPContractState(Actions);
907
Douglas Gregor0fbda682010-09-15 14:51:05 +0000908 InMessageExpressionRAIIObject InMessage(*this, false);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000909 BalancedDelimiterTracker T(*this, tok::l_brace);
910 if (T.consumeOpen())
911 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000912
Dmitri Gribenko625bb562012-02-14 22:14:32 +0000913 Sema::CompoundScopeRAII CompoundScope(Actions);
914
Lang Hamesa60d21d2012-11-03 22:29:05 +0000915 // Parse any pragmas at the beginning of the compound statement.
916 ParseCompoundStatementLeadingPragmas();
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000917
Lang Hamesa60d21d2012-11-03 22:29:05 +0000918 StmtVector Stmts;
Lang Hames860022c2012-10-21 01:10:01 +0000919
Chris Lattner4ae493c2011-02-18 02:08:43 +0000920 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
921 // only allowed at the start of a compound stmt regardless of the language.
922 while (Tok.is(tok::kw___label__)) {
923 SourceLocation LabelLoc = ConsumeToken();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000924
Chris Lattner5f9e2722011-07-23 10:55:15 +0000925 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4ae493c2011-02-18 02:08:43 +0000926 while (1) {
927 if (Tok.isNot(tok::identifier)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700928 Diag(Tok, diag::err_expected) << tok::identifier;
Chris Lattner4ae493c2011-02-18 02:08:43 +0000929 break;
930 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000931
Chris Lattner4ae493c2011-02-18 02:08:43 +0000932 IdentifierInfo *II = Tok.getIdentifierInfo();
933 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara67843042011-03-05 18:21:20 +0000934 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000935
Stephen Hines651f13c2014-04-23 16:59:28 -0700936 if (!TryConsumeToken(tok::comma))
Chris Lattner4ae493c2011-02-18 02:08:43 +0000937 break;
Chris Lattner4ae493c2011-02-18 02:08:43 +0000938 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000939
John McCall0b7e6782011-03-24 11:26:52 +0000940 DeclSpec DS(AttrFactory);
Rafael Espindola4549d7f2013-07-09 12:05:01 +0000941 DeclGroupPtrTy Res =
942 Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner4ae493c2011-02-18 02:08:43 +0000943 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000944
Chris Lattner8bb21d32012-04-28 16:12:17 +0000945 ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
Chris Lattner4ae493c2011-02-18 02:08:43 +0000946 if (R.isUsable())
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700947 Stmts.push_back(R.get());
Chris Lattner4ae493c2011-02-18 02:08:43 +0000948 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000949
Stephen Hines651f13c2014-04-23 16:59:28 -0700950 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000951 if (Tok.is(tok::annot_pragma_unused)) {
952 HandlePragmaUnused();
953 continue;
954 }
955
John McCall60d7b3a2010-08-24 06:29:42 +0000956 StmtResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000957 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000958 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattner45a566c2007-08-27 01:01:57 +0000959 } else {
960 // __extension__ can start declarations and it can also be a unary
961 // operator for expressions. Consume multiple __extension__ markers here
962 // until we can determine which is which.
Eli Friedmanadf077f2009-01-27 08:43:38 +0000963 // FIXME: This loses extension expressions in the AST!
Chris Lattner45a566c2007-08-27 01:01:57 +0000964 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000965 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000966 ConsumeToken();
Chris Lattner39146d62008-10-20 06:51:33 +0000967
John McCall0b7e6782011-03-24 11:26:52 +0000968 ParsedAttributesWithRange attrs(AttrFactory);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700969 MaybeParseCXX11Attributes(attrs, nullptr,
970 /*MightBeObjCMessageSend*/ true);
Sean Huntbbd37c62009-11-21 08:43:09 +0000971
Chris Lattner45a566c2007-08-27 01:01:57 +0000972 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000973 if (isDeclarationStatement()) {
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000974 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000975 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000976 ExtensionRAIIObject O(Diags);
977
Chris Lattner97144fc2009-04-02 04:16:50 +0000978 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Stephen Hines176edba2014-12-01 14:53:08 -0800979 DeclGroupPtrTy Res = ParseDeclaration(Declarator::BlockContext, DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000980 attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000981 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattner45a566c2007-08-27 01:01:57 +0000982 } else {
Eli Friedmanadf077f2009-01-27 08:43:38 +0000983 // Otherwise this was a unary __extension__ marker.
John McCall60d7b3a2010-08-24 06:29:42 +0000984 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattner043a0b52008-03-13 06:32:11 +0000985
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000986 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000987 SkipUntil(tok::semi);
988 continue;
989 }
Sebastian Redlf512e822009-01-18 18:03:53 +0000990
Sean Huntbbd37c62009-11-21 08:43:09 +0000991 // FIXME: Use attributes?
Chris Lattner39146d62008-10-20 06:51:33 +0000992 // Eat the semicolon at the end of stmt and convert the expr into a
993 // statement.
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000994 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith41956372013-01-14 22:39:08 +0000995 R = Actions.ActOnExprStmt(Res);
Chris Lattner45a566c2007-08-27 01:01:57 +0000996 }
997 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000998
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000999 if (R.isUsable())
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001000 Stmts.push_back(R.get());
Reid Spencer5f016e22007-07-11 17:01:13 +00001001 }
Sebastian Redl61364dd2008-12-11 19:30:53 +00001002
Argyrios Kyrtzidis5d5ed592012-03-24 02:26:51 +00001003 SourceLocation CloseLoc = Tok.getLocation();
1004
Reid Spencer5f016e22007-07-11 17:01:13 +00001005 // We broke out of the while loop because we found a '}' or EOF.
Nico Weberd11f4352012-12-30 23:36:56 +00001006 if (!T.consumeClose())
Argyrios Kyrtzidis5d5ed592012-03-24 02:26:51 +00001007 // Recover by creating a compound statement with what we parsed so far,
1008 // instead of dropping everything and returning StmtError();
Nico Weberd11f4352012-12-30 23:36:56 +00001009 CloseLoc = T.getCloseLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +00001010
Argyrios Kyrtzidis5d5ed592012-03-24 02:26:51 +00001011 return Actions.ActOnCompoundStmt(T.getOpenLocation(), CloseLoc,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001012 Stmts, isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +00001013}
1014
Chris Lattner15ff1112008-12-12 06:31:07 +00001015/// ParseParenExprOrCondition:
1016/// [C ] '(' expression ')'
Chris Lattnerff871fb2008-12-12 06:35:28 +00001017/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattner15ff1112008-12-12 06:31:07 +00001018///
1019/// This function parses and performs error recovery on the specified condition
1020/// or expression (depending on whether we're in C++ or C mode). This function
1021/// goes out of its way to recover well. It returns true if there was a parser
1022/// error (the right paren couldn't be found), which indicates that the caller
1023/// should try to recover harder. It returns false if the condition is
1024/// successfully parsed. Note that a successful parse can still have semantic
1025/// errors in the condition.
John McCall60d7b3a2010-08-24 06:29:42 +00001026bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCalld226f652010-08-21 09:40:31 +00001027 Decl *&DeclResult,
Douglas Gregor586596f2010-05-06 17:25:47 +00001028 SourceLocation Loc,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +00001029 bool ConvertToBoolean) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001030 BalancedDelimiterTracker T(*this, tok::l_paren);
1031 T.consumeOpen();
1032
David Blaikie4e4d0842012-03-11 07:00:24 +00001033 if (getLangOpts().CPlusPlus)
Jeffrey Yasskindec09842011-01-18 02:00:16 +00001034 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001035 else {
1036 ExprResult = ParseExpression();
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001037 DeclResult = nullptr;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001038
Douglas Gregor586596f2010-05-06 17:25:47 +00001039 // If required, convert to a boolean value.
1040 if (!ExprResult.isInvalid() && ConvertToBoolean)
1041 ExprResult
John McCall9ae2f072010-08-23 23:25:46 +00001042 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +00001043 }
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Chris Lattner15ff1112008-12-12 06:31:07 +00001045 // If the parser was confused by the condition and we don't have a ')', try to
1046 // recover by skipping ahead to a semi and bailing out. If condexp is
1047 // semantically invalid but we have well formed code, keep going.
John McCalld226f652010-08-21 09:40:31 +00001048 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattner15ff1112008-12-12 06:31:07 +00001049 SkipUntil(tok::semi);
1050 // Skipping may have stopped if it found the containing ')'. If so, we can
1051 // continue parsing the if statement.
1052 if (Tok.isNot(tok::r_paren))
1053 return true;
1054 }
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Chris Lattner15ff1112008-12-12 06:31:07 +00001056 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001057 T.consumeClose();
Chad Rosierb6604462012-07-10 21:35:27 +00001058
Chris Lattnerbddc7e52012-04-28 16:24:20 +00001059 // Check for extraneous ')'s to catch things like "if (foo())) {". We know
1060 // that all callers are looking for a statement after the condition, so ")"
1061 // isn't valid.
1062 while (Tok.is(tok::r_paren)) {
1063 Diag(Tok, diag::err_extraneous_rparen_in_condition)
1064 << FixItHint::CreateRemoval(Tok.getLocation());
1065 ConsumeParen();
1066 }
Chad Rosierb6604462012-07-10 21:35:27 +00001067
Chris Lattner15ff1112008-12-12 06:31:07 +00001068 return false;
1069}
1070
1071
Reid Spencer5f016e22007-07-11 17:01:13 +00001072/// ParseIfStatement
1073/// if-statement: [C99 6.8.4.1]
1074/// 'if' '(' expression ')' statement
1075/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001076/// [C++] 'if' '(' condition ')' statement
1077/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +00001078///
Richard Smith534986f2012-04-14 00:33:13 +00001079StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001080 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001081 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
1082
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001083 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001084 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +00001085 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001086 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001087 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001088
David Blaikie4e4d0842012-03-11 07:00:24 +00001089 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001090
Chris Lattner22153252007-08-26 23:08:06 +00001091 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
1092 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001093 //
1094 // C++ 6.4p3:
1095 // A name introduced by a declaration in a condition is in scope from its
1096 // point of declaration until the end of the substatements controlled by the
1097 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001098 // C++ 3.3.2p4:
1099 // Names declared in the for-init-statement, and in the condition of if,
1100 // while, for, and switch statements are local to the if, while, for, or
1101 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001102 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001103 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +00001104
Reid Spencer5f016e22007-07-11 17:01:13 +00001105 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +00001106 ExprResult CondExp;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001107 Decl *CondVar = nullptr;
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +00001108 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +00001109 return StmtError();
Chris Lattner18914bc2008-12-12 06:19:11 +00001110
David Blaikiedef07622012-05-16 04:20:04 +00001111 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get(), IfLoc));
Mike Stump1eb44332009-09-09 15:08:12 +00001112
Chris Lattner0ecea032007-08-22 05:28:50 +00001113 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001114 // there is no compound stmt. C90 does not have this clause. We only do this
1115 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001116 //
1117 // C++ 6.4p1:
1118 // The substatement in a selection-statement (each substatement, in the else
1119 // form of the if statement) implicitly defines a local scope.
1120 //
1121 // For C++ we create a scope for the condition and a new scope for
1122 // substatements because:
1123 // -When the 'then' scope exits, we want the condition declaration to still be
1124 // active for the 'else' scope too.
1125 // -Sema will detect name clashes by considering declarations of a
1126 // 'ControlScope' as part of its direct subscope.
1127 // -If we wanted the condition and substatement to be in the same scope, we
1128 // would have to notify ParseStatement not to create a new scope. It's
1129 // simpler to let it create a new scope.
1130 //
Stephen Hines651f13c2014-04-23 16:59:28 -07001131 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001132
Chris Lattnerb96728d2007-10-29 05:08:52 +00001133 // Read the 'then' stmt.
1134 SourceLocation ThenStmtLoc = Tok.getLocation();
Nico Weber5cb94a72011-12-22 23:26:17 +00001135
1136 SourceLocation InnerStatementTrailingElseLoc;
1137 StmtResult ThenStmt(ParseStatement(&InnerStatementTrailingElseLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001138
Chris Lattnera36ce712007-08-22 05:16:28 +00001139 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001140 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +00001141
Reid Spencer5f016e22007-07-11 17:01:13 +00001142 // If it has an else, parse it.
1143 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +00001144 SourceLocation ElseStmtLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00001145 StmtResult ElseStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001146
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001147 if (Tok.is(tok::kw_else)) {
Nico Weber5cb94a72011-12-22 23:26:17 +00001148 if (TrailingElseLoc)
1149 *TrailingElseLoc = Tok.getLocation();
1150
Reid Spencer5f016e22007-07-11 17:01:13 +00001151 ElseLoc = ConsumeToken();
Chris Lattner966c78b2010-04-12 06:12:50 +00001152 ElseStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +00001153
Chris Lattner0ecea032007-08-22 05:28:50 +00001154 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001155 // there is no compound stmt. C90 does not have this clause. We only do
1156 // this if the body isn't a compound statement to avoid push/pop in common
1157 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001158 //
1159 // C++ 6.4p1:
1160 // The substatement in a selection-statement (each substatement, in the else
1161 // form of the if statement) implicitly defines a local scope.
1162 //
Stephen Hines651f13c2014-04-23 16:59:28 -07001163 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001164
Reid Spencer5f016e22007-07-11 17:01:13 +00001165 ElseStmt = ParseStatement();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001166
Chris Lattnera36ce712007-08-22 05:16:28 +00001167 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001168 InnerScope.Exit();
Douglas Gregord2d8be62011-07-30 08:36:53 +00001169 } else if (Tok.is(tok::code_completion)) {
1170 Actions.CodeCompleteAfterIf(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001171 cutOffParsing();
1172 return StmtError();
Nico Weber5cb94a72011-12-22 23:26:17 +00001173 } else if (InnerStatementTrailingElseLoc.isValid()) {
1174 Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else);
Reid Spencer5f016e22007-07-11 17:01:13 +00001175 }
Sebastian Redl61364dd2008-12-11 19:30:53 +00001176
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001177 IfScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +00001178
Chris Lattnerb96728d2007-10-29 05:08:52 +00001179 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump1eb44332009-09-09 15:08:12 +00001180 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattnerb96728d2007-10-29 05:08:52 +00001181 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001182 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001183 (ThenStmt.isInvalid() && ElseStmt.get() == nullptr) ||
1184 (ThenStmt.get() == nullptr && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +00001185 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +00001186 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +00001187 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001188
Chris Lattnerb96728d2007-10-29 05:08:52 +00001189 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001190 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +00001191 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001192 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +00001193 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001194
John McCall9ae2f072010-08-23 23:25:46 +00001195 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +00001196 ElseLoc, ElseStmt.get());
Reid Spencer5f016e22007-07-11 17:01:13 +00001197}
1198
1199/// ParseSwitchStatement
1200/// switch-statement:
1201/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001202/// [C++] 'switch' '(' condition ')' statement
Richard Smith534986f2012-04-14 00:33:13 +00001203StmtResult Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001204 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001205 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
1206
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001207 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001208 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +00001209 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +00001210 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001211 }
Chris Lattner22153252007-08-26 23:08:06 +00001212
David Blaikie4e4d0842012-03-11 07:00:24 +00001213 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001214
Chris Lattner22153252007-08-26 23:08:06 +00001215 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1216 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001217 //
1218 // C++ 6.4p3:
1219 // A name introduced by a declaration in a condition is in scope from its
1220 // point of declaration until the end of the substatements controlled by the
1221 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001222 // C++ 3.3.2p4:
1223 // Names declared in the for-init-statement, and in the condition of if,
1224 // while, for, and switch statements are local to the if, while, for, or
1225 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001226 //
Stephen Hines651f13c2014-04-23 16:59:28 -07001227 unsigned ScopeFlags = Scope::SwitchScope;
Chris Lattner15ff1112008-12-12 06:31:07 +00001228 if (C99orCXX)
1229 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001230 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001231
1232 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +00001233 ExprResult Cond;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001234 Decl *CondVar = nullptr;
Douglas Gregor586596f2010-05-06 17:25:47 +00001235 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redl9a920342008-12-11 19:48:14 +00001236 return StmtError();
Eli Friedman2342ef72008-12-17 22:19:57 +00001237
John McCall60d7b3a2010-08-24 06:29:42 +00001238 StmtResult Switch
John McCall9ae2f072010-08-23 23:25:46 +00001239 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001240
Douglas Gregor586596f2010-05-06 17:25:47 +00001241 if (Switch.isInvalid()) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001242 // Skip the switch body.
Douglas Gregor586596f2010-05-06 17:25:47 +00001243 // FIXME: This is not optimal recovery, but parsing the body is more
1244 // dangerous due to the presence of case and default statements, which
1245 // will have no place to connect back with the switch.
Douglas Gregor4186ff42010-05-20 23:20:59 +00001246 if (Tok.is(tok::l_brace)) {
1247 ConsumeBrace();
Alexey Bataev8fe24752013-11-18 08:17:37 +00001248 SkipUntil(tok::r_brace);
Douglas Gregor4186ff42010-05-20 23:20:59 +00001249 } else
Douglas Gregor586596f2010-05-06 17:25:47 +00001250 SkipUntil(tok::semi);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001251 return Switch;
Douglas Gregor586596f2010-05-06 17:25:47 +00001252 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001253
Chris Lattner0ecea032007-08-22 05:28:50 +00001254 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001255 // there is no compound stmt. C90 does not have this clause. We only do this
1256 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001257 //
1258 // C++ 6.4p1:
1259 // The substatement in a selection-statement (each substatement, in the else
1260 // form of the if statement) implicitly defines a local scope.
1261 //
1262 // See comments in ParseIfStatement for why we create a scope for the
1263 // condition and a new scope for substatement in C++.
1264 //
Stephen Hines651f13c2014-04-23 16:59:28 -07001265 getCurScope()->AddFlags(Scope::BreakScope);
1266 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +00001267
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001268 // We have incremented the mangling number for the SwitchScope and the
1269 // InnerScope, which is one too many.
1270 if (C99orCXX)
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001271 getCurScope()->decrementMSManglingNumber();
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001272
Reid Spencer5f016e22007-07-11 17:01:13 +00001273 // Read the body statement.
Nico Weber5cb94a72011-12-22 23:26:17 +00001274 StmtResult Body(ParseStatement(TrailingElseLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001275
Chris Lattner7e52de42010-01-24 01:50:29 +00001276 // Pop the scopes.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001277 InnerScope.Exit();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001278 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +00001279
John McCall9ae2f072010-08-23 23:25:46 +00001280 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Reid Spencer5f016e22007-07-11 17:01:13 +00001281}
1282
1283/// ParseWhileStatement
1284/// while-statement: [C99 6.8.5.1]
1285/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001286/// [C++] 'while' '(' condition ')' statement
Richard Smith534986f2012-04-14 00:33:13 +00001287StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001288 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001289 SourceLocation WhileLoc = Tok.getLocation();
1290 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001291
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001292 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001293 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +00001294 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +00001295 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001296 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001297
David Blaikie4e4d0842012-03-11 07:00:24 +00001298 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001299
Chris Lattner22153252007-08-26 23:08:06 +00001300 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1301 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001302 //
1303 // C++ 6.4p3:
1304 // A name introduced by a declaration in a condition is in scope from its
1305 // point of declaration until the end of the substatements controlled by the
1306 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001307 // C++ 3.3.2p4:
1308 // Names declared in the for-init-statement, and in the condition of if,
1309 // while, for, and switch statements are local to the if, while, for, or
1310 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001311 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001312 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001313 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001314 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1315 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +00001316 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001317 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1318 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001319
1320 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +00001321 ExprResult Cond;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001322 Decl *CondVar = nullptr;
Douglas Gregor586596f2010-05-06 17:25:47 +00001323 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +00001324 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001325
David Blaikiedef07622012-05-16 04:20:04 +00001326 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get(), WhileLoc));
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Stephen Hines651f13c2014-04-23 16:59:28 -07001328 // C99 6.8.5p5 - In C99, the body of the while statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001329 // there is no compound stmt. C90 does not have this clause. We only do this
1330 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001331 //
1332 // C++ 6.5p2:
1333 // The substatement in an iteration-statement implicitly defines a local scope
1334 // which is entered and exited each time through the loop.
1335 //
1336 // See comments in ParseIfStatement for why we create a scope for the
1337 // condition and a new scope for substatement in C++.
1338 //
Stephen Hines651f13c2014-04-23 16:59:28 -07001339 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +00001340
Reid Spencer5f016e22007-07-11 17:01:13 +00001341 // Read the body statement.
Nico Weber5cb94a72011-12-22 23:26:17 +00001342 StmtResult Body(ParseStatement(TrailingElseLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001343
Chris Lattner0ecea032007-08-22 05:28:50 +00001344 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001345 InnerScope.Exit();
1346 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +00001347
John McCalld226f652010-08-21 09:40:31 +00001348 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001349 return StmtError();
1350
John McCall9ae2f072010-08-23 23:25:46 +00001351 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Reid Spencer5f016e22007-07-11 17:01:13 +00001352}
1353
1354/// ParseDoStatement
1355/// do-statement: [C99 6.8.5.2]
1356/// 'do' statement 'while' '(' expression ')' ';'
1357/// Note: this lets the caller parse the end ';'.
Richard Smith534986f2012-04-14 00:33:13 +00001358StmtResult Parser::ParseDoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001359 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001360 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001361
Chris Lattner22153252007-08-26 23:08:06 +00001362 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1363 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001364 unsigned ScopeFlags;
David Blaikie4e4d0842012-03-11 07:00:24 +00001365 if (getLangOpts().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001366 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +00001367 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001368 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +00001369
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001370 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001371
Stephen Hines651f13c2014-04-23 16:59:28 -07001372 // C99 6.8.5p5 - In C99, the body of the do statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001373 // there is no compound stmt. C90 does not have this clause. We only do this
1374 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +00001375 //
1376 // C++ 6.5p2:
1377 // The substatement in an iteration-statement implicitly defines a local scope
1378 // which is entered and exited each time through the loop.
1379 //
Stephen Hines651f13c2014-04-23 16:59:28 -07001380 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
1381 ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +00001382
Reid Spencer5f016e22007-07-11 17:01:13 +00001383 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +00001384 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +00001385
Chris Lattner0ecea032007-08-22 05:28:50 +00001386 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001387 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001388
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001389 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001390 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +00001391 Diag(Tok, diag::err_expected_while);
Stephen Hines651f13c2014-04-23 16:59:28 -07001392 Diag(DoLoc, diag::note_matching) << "'do'";
Alexey Bataev8fe24752013-11-18 08:17:37 +00001393 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner19504402008-11-13 18:52:53 +00001394 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001395 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001396 }
1397 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001398
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001399 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001400 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Alexey Bataev8fe24752013-11-18 08:17:37 +00001401 SkipUntil(tok::semi, StopBeforeMatch);
Sebastian Redl9a920342008-12-11 19:48:14 +00001402 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001403 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001404
Richard Smith5eed7e02013-10-15 01:34:54 +00001405 // Parse the parenthesized expression.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001406 BalancedDelimiterTracker T(*this, tok::l_paren);
1407 T.consumeOpen();
Chad Rosierb6604462012-07-10 21:35:27 +00001408
Richard Smith5eed7e02013-10-15 01:34:54 +00001409 // A do-while expression is not a condition, so can't have attributes.
1410 DiagnoseAndSkipCXX11Attributes();
Sean Hunt2edf0a22012-06-23 05:07:58 +00001411
John McCall60d7b3a2010-08-24 06:29:42 +00001412 ExprResult Cond = ParseExpression();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001413 T.consumeClose();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001414 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001415
Sebastian Redl9a920342008-12-11 19:48:14 +00001416 if (Cond.isInvalid() || Body.isInvalid())
1417 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001418
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001419 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(),
1420 Cond.get(), T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001421}
1422
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001423bool Parser::isForRangeIdentifier() {
1424 assert(Tok.is(tok::identifier));
1425
1426 const Token &Next = NextToken();
1427 if (Next.is(tok::colon))
1428 return true;
1429
1430 if (Next.is(tok::l_square) || Next.is(tok::kw_alignas)) {
1431 TentativeParsingAction PA(*this);
1432 ConsumeToken();
1433 SkipCXX11Attributes();
1434 bool Result = Tok.is(tok::colon);
1435 PA.Revert();
1436 return Result;
1437 }
1438
1439 return false;
1440}
1441
Reid Spencer5f016e22007-07-11 17:01:13 +00001442/// ParseForStatement
1443/// for-statement: [C99 6.8.5.3]
1444/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1445/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001446/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1447/// [C++] statement
Richard Smithad762fc2011-04-14 22:09:26 +00001448/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001449/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1450/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001451///
1452/// [C++] for-init-statement:
1453/// [C++] expression-statement
1454/// [C++] simple-declaration
1455///
Richard Smithad762fc2011-04-14 22:09:26 +00001456/// [C++0x] for-range-declaration:
1457/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1458/// [C++0x] for-range-initializer:
1459/// [C++0x] expression
1460/// [C++0x] braced-init-list [TODO]
Richard Smith534986f2012-04-14 00:33:13 +00001461StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001462 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001463 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001464
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001465 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001466 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +00001467 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +00001468 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001469 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001470
Chad Rosierb6604462012-07-10 21:35:27 +00001471 bool C99orCXXorObjC = getLangOpts().C99 || getLangOpts().CPlusPlus ||
1472 getLangOpts().ObjC1;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001473
Chris Lattner22153252007-08-26 23:08:06 +00001474 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1475 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001476 //
1477 // C++ 6.4p3:
1478 // A name introduced by a declaration in a condition is in scope from its
1479 // point of declaration until the end of the substatements controlled by the
1480 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001481 // C++ 3.3.2p4:
1482 // Names declared in the for-init-statement, and in the condition of if,
1483 // while, for, and switch statements are local to the if, while, for, or
1484 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001485 // C++ 6.5.3p1:
1486 // Names declared in the for-init-statement are in the same declarative-region
1487 // as those declared in the condition.
1488 //
Stephen Hines651f13c2014-04-23 16:59:28 -07001489 unsigned ScopeFlags = 0;
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001490 if (C99orCXXorObjC)
Stephen Hines651f13c2014-04-23 16:59:28 -07001491 ScopeFlags = Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001492
1493 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001494
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001495 BalancedDelimiterTracker T(*this, tok::l_paren);
1496 T.consumeOpen();
1497
John McCall60d7b3a2010-08-24 06:29:42 +00001498 ExprResult Value;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001499
Richard Smithad762fc2011-04-14 22:09:26 +00001500 bool ForEach = false, ForRange = false;
John McCall60d7b3a2010-08-24 06:29:42 +00001501 StmtResult FirstPart;
Douglas Gregoreecf38f2010-05-06 21:39:56 +00001502 bool SecondPartIsInvalid = false;
Douglas Gregor586596f2010-05-06 17:25:47 +00001503 FullExprArg SecondPart(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001504 ExprResult Collection;
Richard Smithad762fc2011-04-14 22:09:26 +00001505 ForRangeInit ForRangeInit;
Douglas Gregor586596f2010-05-06 17:25:47 +00001506 FullExprArg ThirdPart(Actions);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001507 Decl *SecondVar = nullptr;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001508
Douglas Gregor791215b2009-09-21 20:51:25 +00001509 if (Tok.is(tok::code_completion)) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001510 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001511 C99orCXXorObjC? Sema::PCC_ForInit
1512 : Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001513 cutOffParsing();
1514 return StmtError();
Douglas Gregor791215b2009-09-21 20:51:25 +00001515 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001516
Sean Hunt2edf0a22012-06-23 05:07:58 +00001517 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00001518 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00001519
Reid Spencer5f016e22007-07-11 17:01:13 +00001520 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001521 if (Tok.is(tok::semi)) { // for (;
Sean Hunt2edf0a22012-06-23 05:07:58 +00001522 ProhibitAttributes(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00001523 // no first part, eat the ';'.
1524 ConsumeToken();
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001525 } else if (getLangOpts().CPlusPlus && Tok.is(tok::identifier) &&
1526 isForRangeIdentifier()) {
1527 ProhibitAttributes(attrs);
1528 IdentifierInfo *Name = Tok.getIdentifierInfo();
1529 SourceLocation Loc = ConsumeToken();
1530 MaybeParseCXX11Attributes(attrs);
1531
1532 ForRangeInit.ColonLoc = ConsumeToken();
1533 if (Tok.is(tok::l_brace))
1534 ForRangeInit.RangeExpr = ParseBraceInitializer();
1535 else
1536 ForRangeInit.RangeExpr = ParseExpression();
1537
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001538 Diag(Loc, diag::err_for_range_identifier)
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001539 << ((getLangOpts().CPlusPlus11 && !getLangOpts().CPlusPlus1z)
1540 ? FixItHint::CreateInsertion(Loc, "auto &&")
1541 : FixItHint());
1542
1543 FirstPart = Actions.ActOnCXXForRangeIdentifier(getCurScope(), Loc, Name,
1544 attrs, attrs.Range.getEnd());
1545 ForRange = true;
Eli Friedman9490ab42011-12-20 01:50:37 +00001546 } else if (isForInitDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +00001547 // Parse declaration, which eats the ';'.
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001548 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +00001549 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +00001550
Richard Smithad762fc2011-04-14 22:09:26 +00001551 // In C++0x, "for (T NS:a" might not be a typo for ::
David Blaikie4e4d0842012-03-11 07:00:24 +00001552 bool MightBeForRangeStmt = getLangOpts().CPlusPlus;
Richard Smithad762fc2011-04-14 22:09:26 +00001553 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1554
Chris Lattner97144fc2009-04-02 04:16:50 +00001555 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001556 DeclGroupPtrTy DG = ParseSimpleDeclaration(
Stephen Hines176edba2014-12-01 14:53:08 -08001557 Declarator::ForContext, DeclEnd, attrs, false,
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001558 MightBeForRangeStmt ? &ForRangeInit : nullptr);
Chris Lattnercd147752009-03-29 17:27:48 +00001559 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Richard Smithad762fc2011-04-14 22:09:26 +00001560 if (ForRangeInit.ParsedForRangeDecl()) {
Richard Smith80ad52f2013-01-02 11:42:31 +00001561 Diag(ForRangeInit.ColonLoc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00001562 diag::warn_cxx98_compat_for_range : diag::ext_for_range);
Richard Smith8f4fb192011-09-04 19:54:14 +00001563
Richard Smithad762fc2011-04-14 22:09:26 +00001564 ForRange = true;
1565 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattnercd147752009-03-29 17:27:48 +00001566 ConsumeToken();
1567 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniana7cf23a2009-11-19 22:12:37 +00001568 Actions.ActOnForEachDeclStmt(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001569 // ObjC: for (id x in expr)
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001570 ConsumeToken(); // consume 'in'
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001571
Douglas Gregorfb629412010-08-23 21:17:50 +00001572 if (Tok.is(tok::code_completion)) {
1573 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001574 cutOffParsing();
1575 return StmtError();
Douglas Gregorfb629412010-08-23 21:17:50 +00001576 }
Douglas Gregor586596f2010-05-06 17:25:47 +00001577 Collection = ParseExpression();
Chris Lattnercd147752009-03-29 17:27:48 +00001578 } else {
1579 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001580 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001581 } else {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001582 ProhibitAttributes(attrs);
Stephen Hines176edba2014-12-01 14:53:08 -08001583 Value = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Reid Spencer5f016e22007-07-11 17:01:13 +00001584
John McCallf6a16482010-12-04 03:47:34 +00001585 ForEach = isTokIdentifier_in();
1586
Reid Spencer5f016e22007-07-11 17:01:13 +00001587 // Turn the expression into a stmt.
John McCallf6a16482010-12-04 03:47:34 +00001588 if (!Value.isInvalid()) {
1589 if (ForEach)
1590 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1591 else
Richard Smith41956372013-01-14 22:39:08 +00001592 FirstPart = Actions.ActOnExprStmt(Value);
John McCallf6a16482010-12-04 03:47:34 +00001593 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001594
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001595 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001596 ConsumeToken();
John McCallf6a16482010-12-04 03:47:34 +00001597 } else if (ForEach) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001598 ConsumeToken(); // consume 'in'
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001599
Douglas Gregorfb629412010-08-23 21:17:50 +00001600 if (Tok.is(tok::code_completion)) {
1601 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001602 cutOffParsing();
1603 return StmtError();
Douglas Gregorfb629412010-08-23 21:17:50 +00001604 }
Douglas Gregor586596f2010-05-06 17:25:47 +00001605 Collection = ParseExpression();
Richard Smith80ad52f2013-01-02 11:42:31 +00001606 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::colon) && FirstPart.get()) {
Richard Smitha44854a2011-12-20 22:56:20 +00001607 // User tried to write the reasonable, but ill-formed, for-range-statement
1608 // for (expr : expr) { ... }
1609 Diag(Tok, diag::err_for_range_expected_decl)
1610 << FirstPart.get()->getSourceRange();
Alexey Bataev8fe24752013-11-18 08:17:37 +00001611 SkipUntil(tok::r_paren, StopBeforeMatch);
Richard Smitha44854a2011-12-20 22:56:20 +00001612 SecondPartIsInvalid = true;
Chris Lattner682bf922009-03-29 16:50:03 +00001613 } else {
Douglas Gregorb72c7782011-02-17 03:38:46 +00001614 if (!Value.isInvalid()) {
1615 Diag(Tok, diag::err_expected_semi_for);
1616 } else {
1617 // Skip until semicolon or rparen, don't consume it.
Alexey Bataev8fe24752013-11-18 08:17:37 +00001618 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregorb72c7782011-02-17 03:38:46 +00001619 if (Tok.is(tok::semi))
1620 ConsumeToken();
1621 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001622 }
1623 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001624
1625 // Parse the second part of the for specifier.
1626 getCurScope()->AddFlags(Scope::BreakScope | Scope::ContinueScope);
Richard Smithad762fc2011-04-14 22:09:26 +00001627 if (!ForEach && !ForRange) {
John McCall9ae2f072010-08-23 23:25:46 +00001628 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001629 // Parse the second part of the for specifier.
1630 if (Tok.is(tok::semi)) { // for (...;;
1631 // no second part.
Douglas Gregorb72c7782011-02-17 03:38:46 +00001632 } else if (Tok.is(tok::r_paren)) {
1633 // missing both semicolons.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001634 } else {
John McCall60d7b3a2010-08-24 06:29:42 +00001635 ExprResult Second;
David Blaikie4e4d0842012-03-11 07:00:24 +00001636 if (getLangOpts().CPlusPlus)
Douglas Gregor586596f2010-05-06 17:25:47 +00001637 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1638 else {
1639 Second = ParseExpression();
1640 if (!Second.isInvalid())
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001641 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001642 Second.get());
Douglas Gregor586596f2010-05-06 17:25:47 +00001643 }
Douglas Gregoreecf38f2010-05-06 21:39:56 +00001644 SecondPartIsInvalid = Second.isInvalid();
David Blaikiedef07622012-05-16 04:20:04 +00001645 SecondPart = Actions.MakeFullExpr(Second.get(), ForLoc);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001646 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001647
Douglas Gregorb72c7782011-02-17 03:38:46 +00001648 if (Tok.isNot(tok::semi)) {
1649 if (!SecondPartIsInvalid || SecondVar)
1650 Diag(Tok, diag::err_expected_semi_for);
1651 else
1652 // Skip until semicolon or rparen, don't consume it.
Alexey Bataev8fe24752013-11-18 08:17:37 +00001653 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregorb72c7782011-02-17 03:38:46 +00001654 }
1655
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001656 if (Tok.is(tok::semi)) {
1657 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001658 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001659
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001660 // Parse the third part of the for specifier.
Douglas Gregor586596f2010-05-06 17:25:47 +00001661 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCall60d7b3a2010-08-24 06:29:42 +00001662 ExprResult Third = ParseExpression();
Richard Smith41956372013-01-14 22:39:08 +00001663 // FIXME: The C++11 standard doesn't actually say that this is a
1664 // discarded-value expression, but it clearly should be.
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001665 ThirdPart = Actions.MakeFullDiscardedValueExpr(Third.get());
Douglas Gregor586596f2010-05-06 17:25:47 +00001666 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001667 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001668 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001669 T.consumeClose();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001670
Richard Smithad762fc2011-04-14 22:09:26 +00001671 // We need to perform most of the semantic analysis for a C++0x for-range
1672 // statememt before parsing the body, in order to be able to deduce the type
1673 // of an auto-typed loop variable.
1674 StmtResult ForRangeStmt;
Fariborz Jahaniana1eec4b2012-07-03 22:00:52 +00001675 StmtResult ForEachStmt;
Chad Rosierb6604462012-07-10 21:35:27 +00001676
John McCall990567c2011-07-27 01:07:15 +00001677 if (ForRange) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001678 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, FirstPart.get(),
Richard Smithad762fc2011-04-14 22:09:26 +00001679 ForRangeInit.ColonLoc,
1680 ForRangeInit.RangeExpr.get(),
Richard Smith8b533d92012-09-20 21:52:32 +00001681 T.getCloseLocation(),
1682 Sema::BFRK_Build);
Richard Smithad762fc2011-04-14 22:09:26 +00001683
John McCall990567c2011-07-27 01:07:15 +00001684
1685 // Similarly, we need to do the semantic analysis for a for-range
1686 // statement immediately in order to close over temporaries correctly.
1687 } else if (ForEach) {
Sam Panzerbc20bbb2012-08-16 21:47:25 +00001688 ForEachStmt = Actions.ActOnObjCForCollectionStmt(ForLoc,
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001689 FirstPart.get(),
1690 Collection.get(),
Fariborz Jahaniana1eec4b2012-07-03 22:00:52 +00001691 T.getCloseLocation());
John McCall990567c2011-07-27 01:07:15 +00001692 }
1693
Stephen Hines651f13c2014-04-23 16:59:28 -07001694 // C99 6.8.5p5 - In C99, the body of the for statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001695 // there is no compound stmt. C90 does not have this clause. We only do this
1696 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001697 //
1698 // C++ 6.5p2:
1699 // The substatement in an iteration-statement implicitly defines a local scope
1700 // which is entered and exited each time through the loop.
1701 //
1702 // See comments in ParseIfStatement for why we create a scope for
1703 // for-init-statement/condition and a new scope for substatement in C++.
1704 //
Stephen Hines651f13c2014-04-23 16:59:28 -07001705 ParseScope InnerScope(this, Scope::DeclScope, C99orCXXorObjC,
1706 Tok.is(tok::l_brace));
1707
1708 // The body of the for loop has the same local mangling number as the
1709 // for-init-statement.
1710 // It will only be incremented if the body contains other things that would
1711 // normally increment the mangling number (like a compound statement).
1712 if (C99orCXXorObjC)
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001713 getCurScope()->decrementMSManglingNumber();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001714
Reid Spencer5f016e22007-07-11 17:01:13 +00001715 // Read the body statement.
Nico Weber5cb94a72011-12-22 23:26:17 +00001716 StmtResult Body(ParseStatement(TrailingElseLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001717
Chris Lattner0ecea032007-08-22 05:28:50 +00001718 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001719 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001720
Reid Spencer5f016e22007-07-11 17:01:13 +00001721 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001722 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001723
1724 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001725 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001726
Richard Smithad762fc2011-04-14 22:09:26 +00001727 if (ForEach)
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001728 return Actions.FinishObjCForCollectionStmt(ForEachStmt.get(),
1729 Body.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001730
Richard Smithad762fc2011-04-14 22:09:26 +00001731 if (ForRange)
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001732 return Actions.FinishCXXForRangeStmt(ForRangeStmt.get(), Body.get());
Richard Smithad762fc2011-04-14 22:09:26 +00001733
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001734 return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.get(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001735 SecondPart, SecondVar, ThirdPart,
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001736 T.getCloseLocation(), Body.get());
Reid Spencer5f016e22007-07-11 17:01:13 +00001737}
1738
1739/// ParseGotoStatement
1740/// jump-statement:
1741/// 'goto' identifier ';'
1742/// [GNU] 'goto' '*' expression ';'
1743///
1744/// Note: this lets the caller parse the end ';'.
1745///
Richard Smith534986f2012-04-14 00:33:13 +00001746StmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001747 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001748 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001749
John McCall60d7b3a2010-08-24 06:29:42 +00001750 StmtResult Res;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001751 if (Tok.is(tok::identifier)) {
Chris Lattner337e5502011-02-18 01:27:55 +00001752 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1753 Tok.getLocation());
1754 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Reid Spencer5f016e22007-07-11 17:01:13 +00001755 ConsumeToken();
Eli Friedmanf01fdff2009-04-28 00:51:18 +00001756 } else if (Tok.is(tok::star)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001757 // GNU indirect goto extension.
1758 Diag(Tok, diag::ext_gnu_indirect_goto);
1759 SourceLocation StarLoc = ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001760 ExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001761 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Alexey Bataev8fe24752013-11-18 08:17:37 +00001762 SkipUntil(tok::semi, StopBeforeMatch);
Sebastian Redl9a920342008-12-11 19:48:14 +00001763 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001764 }
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001765 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.get());
Chris Lattner95cfb852007-07-22 04:13:33 +00001766 } else {
Stephen Hines651f13c2014-04-23 16:59:28 -07001767 Diag(Tok, diag::err_expected) << tok::identifier;
Sebastian Redl9a920342008-12-11 19:48:14 +00001768 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001769 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001770
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001771 return Res;
Reid Spencer5f016e22007-07-11 17:01:13 +00001772}
1773
1774/// ParseContinueStatement
1775/// jump-statement:
1776/// 'continue' ';'
1777///
1778/// Note: this lets the caller parse the end ';'.
1779///
Richard Smith534986f2012-04-14 00:33:13 +00001780StmtResult Parser::ParseContinueStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001781 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001782 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001783}
1784
1785/// ParseBreakStatement
1786/// jump-statement:
1787/// 'break' ';'
1788///
1789/// Note: this lets the caller parse the end ';'.
1790///
Richard Smith534986f2012-04-14 00:33:13 +00001791StmtResult Parser::ParseBreakStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001792 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001793 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001794}
1795
1796/// ParseReturnStatement
1797/// jump-statement:
1798/// 'return' expression[opt] ';'
Richard Smith534986f2012-04-14 00:33:13 +00001799StmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001800 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001801 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001802
John McCall60d7b3a2010-08-24 06:29:42 +00001803 ExprResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001804 if (Tok.isNot(tok::semi)) {
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001805 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001806 Actions.CodeCompleteReturn(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001807 cutOffParsing();
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001808 return StmtError();
1809 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001810
David Blaikie4e4d0842012-03-11 07:00:24 +00001811 if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus) {
Douglas Gregor6f4596c2011-03-11 23:10:44 +00001812 R = ParseInitializer();
Richard Smith7fe62082011-10-15 05:09:34 +00001813 if (R.isUsable())
Richard Smith80ad52f2013-01-02 11:42:31 +00001814 Diag(R.get()->getLocStart(), getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00001815 diag::warn_cxx98_compat_generalized_initializer_lists :
1816 diag::ext_generalized_initializer_lists)
Douglas Gregor6f4596c2011-03-11 23:10:44 +00001817 << R.get()->getSourceRange();
1818 } else
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001819 R = ParseExpression();
Stephen Hines651f13c2014-04-23 16:59:28 -07001820 if (R.isInvalid()) {
1821 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Sebastian Redl9a920342008-12-11 19:48:14 +00001822 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001823 }
1824 }
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001825 return Actions.ActOnReturnStmt(ReturnLoc, R.get(), getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001826}
1827
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001828StmtResult Parser::ParsePragmaLoopHint(StmtVector &Stmts, bool OnlyStatement,
1829 SourceLocation *TrailingElseLoc,
1830 ParsedAttributesWithRange &Attrs) {
1831 // Create temporary attribute list.
1832 ParsedAttributesWithRange TempAttrs(AttrFactory);
John McCallaeeacf72013-05-03 00:10:13 +00001833
Stephen Hines176edba2014-12-01 14:53:08 -08001834 // Get loop hints and consume annotated token.
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001835 while (Tok.is(tok::annot_pragma_loop_hint)) {
Stephen Hines176edba2014-12-01 14:53:08 -08001836 LoopHint Hint;
1837 if (!HandlePragmaLoopHint(Hint))
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001838 continue;
1839
Stephen Hines176edba2014-12-01 14:53:08 -08001840 ArgsUnion ArgHints[] = {Hint.PragmaNameLoc, Hint.OptionLoc, Hint.StateLoc,
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001841 ArgsUnion(Hint.ValueExpr)};
Stephen Hines176edba2014-12-01 14:53:08 -08001842 TempAttrs.addNew(Hint.PragmaNameLoc->Ident, Hint.Range, nullptr,
1843 Hint.PragmaNameLoc->Loc, ArgHints, 4,
1844 AttributeList::AS_Pragma);
Chris Lattner64cb4752009-12-20 23:00:41 +00001845 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001846
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001847 // Get the next statement.
1848 MaybeParseCXX11Attributes(Attrs);
Chris Lattner64cb4752009-12-20 23:00:41 +00001849
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001850 StmtResult S = ParseStatementOrDeclarationAfterAttributes(
1851 Stmts, OnlyStatement, TrailingElseLoc, Attrs);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001852
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001853 Attrs.takeAllFrom(TempAttrs);
1854 return S;
Reid Spencer5f016e22007-07-11 17:01:13 +00001855}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001856
Douglas Gregorc9977d02011-03-16 17:05:57 +00001857Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner40e9bc82009-03-05 00:49:17 +00001858 assert(Tok.is(tok::l_brace));
1859 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001860
Argyrios Kyrtzidis1f12c472013-02-22 04:11:06 +00001861 if (SkipFunctionBodies && (!Decl || Actions.canSkipFunctionBody(Decl)) &&
Richard Smith1a5bd5d2012-11-19 21:13:18 +00001862 trySkippingFunctionBody()) {
Erik Verbruggen6a91d382012-04-12 10:11:59 +00001863 BodyScope.Exit();
Argyrios Kyrtzidis35f3f362012-12-06 18:59:10 +00001864 return Actions.ActOnSkippedFunctionBody(Decl);
Douglas Gregorc9977d02011-03-16 17:05:57 +00001865 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001866
John McCallf312b1e2010-08-26 23:41:50 +00001867 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1868 "parsing function body");
Mike Stump1eb44332009-09-09 15:08:12 +00001869
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001870 // Do not enter a scope for the brace, as the arguments are in the same scope
1871 // (the function body) as the body itself. Instead, just read the statement
1872 // list and put it into a CompoundStmt for safe keeping.
John McCall60d7b3a2010-08-24 06:29:42 +00001873 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl61364dd2008-12-11 19:30:53 +00001874
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001875 // If the function body could not be parsed, make a bogus compoundstmt.
Dmitri Gribenko625bb562012-02-14 22:14:32 +00001876 if (FnBody.isInvalid()) {
1877 Sema::CompoundScopeRAII CompoundScope(Actions);
Robert Wilhelmc895f4d2013-08-19 20:51:20 +00001878 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false);
Dmitri Gribenko625bb562012-02-14 22:14:32 +00001879 }
Sebastian Redl61364dd2008-12-11 19:30:53 +00001880
Douglas Gregorc9977d02011-03-16 17:05:57 +00001881 BodyScope.Exit();
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001882 return Actions.ActOnFinishFunctionBody(Decl, FnBody.get());
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001883}
Sebastian Redla0fd8652008-12-21 16:41:36 +00001884
Sebastian Redld3a413d2009-04-26 20:35:05 +00001885/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1886///
1887/// function-try-block:
1888/// 'try' ctor-initializer[opt] compound-statement handler-seq
1889///
Douglas Gregorc9977d02011-03-16 17:05:57 +00001890Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redld3a413d2009-04-26 20:35:05 +00001891 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1892 SourceLocation TryLoc = ConsumeToken();
1893
John McCallf312b1e2010-08-26 23:41:50 +00001894 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1895 "parsing function try block");
Sebastian Redld3a413d2009-04-26 20:35:05 +00001896
1897 // Constructor initializer list?
1898 if (Tok.is(tok::colon))
1899 ParseConstructorInitializer(Decl);
Douglas Gregor2eef4272011-09-07 20:36:12 +00001900 else
1901 Actions.ActOnDefaultCtorInitializers(Decl);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001902
Richard Smith1a5bd5d2012-11-19 21:13:18 +00001903 if (SkipFunctionBodies && Actions.canSkipFunctionBody(Decl) &&
1904 trySkippingFunctionBody()) {
Erik Verbruggen6a91d382012-04-12 10:11:59 +00001905 BodyScope.Exit();
Argyrios Kyrtzidis35f3f362012-12-06 18:59:10 +00001906 return Actions.ActOnSkippedFunctionBody(Decl);
Douglas Gregorc9977d02011-03-16 17:05:57 +00001907 }
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001908
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001909 SourceLocation LBraceLoc = Tok.getLocation();
David Blaikiec4027c82012-11-10 01:04:23 +00001910 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc, /*FnTry*/true));
Sebastian Redld3a413d2009-04-26 20:35:05 +00001911 // If we failed to parse the try-catch, we just give the function an empty
1912 // compound statement as the body.
Dmitri Gribenko625bb562012-02-14 22:14:32 +00001913 if (FnBody.isInvalid()) {
1914 Sema::CompoundScopeRAII CompoundScope(Actions);
Robert Wilhelmc895f4d2013-08-19 20:51:20 +00001915 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false);
Dmitri Gribenko625bb562012-02-14 22:14:32 +00001916 }
Sebastian Redld3a413d2009-04-26 20:35:05 +00001917
Douglas Gregorc9977d02011-03-16 17:05:57 +00001918 BodyScope.Exit();
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001919 return Actions.ActOnFinishFunctionBody(Decl, FnBody.get());
Sebastian Redld3a413d2009-04-26 20:35:05 +00001920}
1921
Erik Verbruggen6a91d382012-04-12 10:11:59 +00001922bool Parser::trySkippingFunctionBody() {
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001923 assert(Tok.is(tok::l_brace));
Erik Verbruggen6a91d382012-04-12 10:11:59 +00001924 assert(SkipFunctionBodies &&
1925 "Should only be called when SkipFunctionBodies is enabled");
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001926
Argyrios Kyrtzidis81939a72012-10-31 17:29:28 +00001927 if (!PP.isCodeCompletionEnabled()) {
1928 ConsumeBrace();
Alexey Bataev8fe24752013-11-18 08:17:37 +00001929 SkipUntil(tok::r_brace);
Argyrios Kyrtzidis81939a72012-10-31 17:29:28 +00001930 return true;
1931 }
1932
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001933 // We're in code-completion mode. Skip parsing for all function bodies unless
1934 // the body contains the code-completion point.
1935 TentativeParsingAction PA(*this);
1936 ConsumeBrace();
Alexey Bataev8fe24752013-11-18 08:17:37 +00001937 if (SkipUntil(tok::r_brace, StopAtCodeCompletion)) {
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001938 PA.Commit();
1939 return true;
1940 }
1941
1942 PA.Revert();
1943 return false;
1944}
1945
Sebastian Redla0fd8652008-12-21 16:41:36 +00001946/// ParseCXXTryBlock - Parse a C++ try-block.
1947///
1948/// try-block:
1949/// 'try' compound-statement handler-seq
1950///
Richard Smith534986f2012-04-14 00:33:13 +00001951StmtResult Parser::ParseCXXTryBlock() {
Sebastian Redla0fd8652008-12-21 16:41:36 +00001952 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1953
1954 SourceLocation TryLoc = ConsumeToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001955 return ParseCXXTryBlockCommon(TryLoc);
1956}
1957
1958/// ParseCXXTryBlockCommon - Parse the common part of try-block and
1959/// function-try-block.
1960///
1961/// try-block:
1962/// 'try' compound-statement handler-seq
1963///
1964/// function-try-block:
1965/// 'try' ctor-initializer[opt] compound-statement handler-seq
1966///
1967/// handler-seq:
1968/// handler handler-seq[opt]
1969///
John Wiegley28bbe4b2011-04-28 01:08:34 +00001970/// [Borland] try-block:
1971/// 'try' compound-statement seh-except-block
Stephen Hines651f13c2014-04-23 16:59:28 -07001972/// 'try' compound-statement seh-finally-block
John Wiegley28bbe4b2011-04-28 01:08:34 +00001973///
David Blaikiec4027c82012-11-10 01:04:23 +00001974StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc, bool FnTry) {
Sebastian Redla0fd8652008-12-21 16:41:36 +00001975 if (Tok.isNot(tok::l_brace))
Stephen Hines651f13c2014-04-23 16:59:28 -07001976 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
Richard Smith534986f2012-04-14 00:33:13 +00001977
1978 StmtResult TryBlock(ParseCompoundStatement(/*isStmtExpr=*/false,
David Blaikiee5afdcf2012-11-13 18:51:45 +00001979 Scope::DeclScope | Scope::TryScope |
1980 (FnTry ? Scope::FnTryCatchScope : 0)));
Sebastian Redla0fd8652008-12-21 16:41:36 +00001981 if (TryBlock.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001982 return TryBlock;
Sebastian Redla0fd8652008-12-21 16:41:36 +00001983
John Wiegley28bbe4b2011-04-28 01:08:34 +00001984 // Borland allows SEH-handlers with 'try'
Chad Rosierb6604462012-07-10 21:35:27 +00001985
Richard Smith534986f2012-04-14 00:33:13 +00001986 if ((Tok.is(tok::identifier) &&
1987 Tok.getIdentifierInfo() == getSEHExceptKeyword()) ||
1988 Tok.is(tok::kw___finally)) {
John Wiegley28bbe4b2011-04-28 01:08:34 +00001989 // TODO: Factor into common return ParseSEHHandlerCommon(...)
1990 StmtResult Handler;
Douglas Gregorb57791e2011-10-21 03:57:52 +00001991 if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley28bbe4b2011-04-28 01:08:34 +00001992 SourceLocation Loc = ConsumeToken();
1993 Handler = ParseSEHExceptBlock(Loc);
1994 }
1995 else {
1996 SourceLocation Loc = ConsumeToken();
1997 Handler = ParseSEHFinallyBlock(Loc);
1998 }
1999 if(Handler.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002000 return Handler;
John McCall7f040a92010-12-24 02:08:15 +00002001
John Wiegley28bbe4b2011-04-28 01:08:34 +00002002 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
2003 TryLoc,
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002004 TryBlock.get(),
2005 Handler.get());
Sebastian Redla0fd8652008-12-21 16:41:36 +00002006 }
John Wiegley28bbe4b2011-04-28 01:08:34 +00002007 else {
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002008 StmtVector Handlers;
Richard Smith5eed7e02013-10-15 01:34:54 +00002009
2010 // C++11 attributes can't appear here, despite this context seeming
2011 // statement-like.
2012 DiagnoseAndSkipCXX11Attributes();
Sebastian Redla0fd8652008-12-21 16:41:36 +00002013
John Wiegley28bbe4b2011-04-28 01:08:34 +00002014 if (Tok.isNot(tok::kw_catch))
2015 return StmtError(Diag(Tok, diag::err_expected_catch));
2016 while (Tok.is(tok::kw_catch)) {
David Blaikiec4027c82012-11-10 01:04:23 +00002017 StmtResult Handler(ParseCXXCatchBlock(FnTry));
John Wiegley28bbe4b2011-04-28 01:08:34 +00002018 if (!Handler.isInvalid())
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002019 Handlers.push_back(Handler.get());
John Wiegley28bbe4b2011-04-28 01:08:34 +00002020 }
2021 // Don't bother creating the full statement if we don't have any usable
2022 // handlers.
2023 if (Handlers.empty())
2024 return StmtError();
2025
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002026 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.get(), Handlers);
John Wiegley28bbe4b2011-04-28 01:08:34 +00002027 }
Sebastian Redla0fd8652008-12-21 16:41:36 +00002028}
2029
2030/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
2031///
Richard Smith4cd81c52013-01-29 09:02:09 +00002032/// handler:
2033/// 'catch' '(' exception-declaration ')' compound-statement
Sebastian Redla0fd8652008-12-21 16:41:36 +00002034///
Richard Smith4cd81c52013-01-29 09:02:09 +00002035/// exception-declaration:
2036/// attribute-specifier-seq[opt] type-specifier-seq declarator
2037/// attribute-specifier-seq[opt] type-specifier-seq abstract-declarator[opt]
2038/// '...'
Sebastian Redla0fd8652008-12-21 16:41:36 +00002039///
David Blaikiec4027c82012-11-10 01:04:23 +00002040StmtResult Parser::ParseCXXCatchBlock(bool FnCatch) {
Sebastian Redla0fd8652008-12-21 16:41:36 +00002041 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
2042
2043 SourceLocation CatchLoc = ConsumeToken();
2044
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002045 BalancedDelimiterTracker T(*this, tok::l_paren);
Stephen Hines651f13c2014-04-23 16:59:28 -07002046 if (T.expectAndConsume())
Sebastian Redla0fd8652008-12-21 16:41:36 +00002047 return StmtError();
2048
2049 // C++ 3.3.2p3:
2050 // The name in a catch exception-declaration is local to the handler and
2051 // shall not be redeclared in the outermost block of the handler.
David Blaikiec4027c82012-11-10 01:04:23 +00002052 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope |
David Blaikiee5afdcf2012-11-13 18:51:45 +00002053 (FnCatch ? Scope::FnTryCatchScope : 0));
Sebastian Redla0fd8652008-12-21 16:41:36 +00002054
2055 // exception-declaration is equivalent to '...' or a parameter-declaration
2056 // without default arguments.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07002057 Decl *ExceptionDecl = nullptr;
Sebastian Redla0fd8652008-12-21 16:41:36 +00002058 if (Tok.isNot(tok::ellipsis)) {
Richard Smith4cd81c52013-01-29 09:02:09 +00002059 ParsedAttributesWithRange Attributes(AttrFactory);
2060 MaybeParseCXX11Attributes(Attributes);
2061
John McCall0b7e6782011-03-24 11:26:52 +00002062 DeclSpec DS(AttrFactory);
Richard Smith4cd81c52013-01-29 09:02:09 +00002063 DS.takeAttributesFrom(Attributes);
2064
Sebastian Redl4b07b292008-12-22 19:15:10 +00002065 if (ParseCXXTypeSpecifierSeq(DS))
2066 return StmtError();
Richard Smith4cd81c52013-01-29 09:02:09 +00002067
Sebastian Redla0fd8652008-12-21 16:41:36 +00002068 Declarator ExDecl(DS, Declarator::CXXCatchContext);
2069 ParseDeclarator(ExDecl);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002070 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redla0fd8652008-12-21 16:41:36 +00002071 } else
2072 ConsumeToken();
2073
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002074 T.consumeClose();
2075 if (T.getCloseLocation().isInvalid())
Sebastian Redla0fd8652008-12-21 16:41:36 +00002076 return StmtError();
2077
2078 if (Tok.isNot(tok::l_brace))
Stephen Hines651f13c2014-04-23 16:59:28 -07002079 return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
Sebastian Redla0fd8652008-12-21 16:41:36 +00002080
Sean Huntbbd37c62009-11-21 08:43:09 +00002081 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
Richard Smith534986f2012-04-14 00:33:13 +00002082 StmtResult Block(ParseCompoundStatement());
Sebastian Redla0fd8652008-12-21 16:41:36 +00002083 if (Block.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002084 return Block;
Sebastian Redla0fd8652008-12-21 16:41:36 +00002085
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002086 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.get());
Sebastian Redla0fd8652008-12-21 16:41:36 +00002087}
Francois Pichet1e862692011-05-06 20:48:22 +00002088
2089void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Douglas Gregor3896fc52011-10-24 22:31:10 +00002090 IfExistsCondition Result;
Francois Pichetf9860382011-05-07 17:30:27 +00002091 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet1e862692011-05-06 20:48:22 +00002092 return;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002093
Douglas Gregor3896fc52011-10-24 22:31:10 +00002094 // Handle dependent statements by parsing the braces as a compound statement.
2095 // This is not the same behavior as Visual C++, which don't treat this as a
2096 // compound statement, but for Clang's type checking we can't have anything
2097 // inside these braces escaping to the surrounding code.
2098 if (Result.Behavior == IEB_Dependent) {
2099 if (!Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002100 Diag(Tok, diag::err_expected) << tok::l_brace;
Richard Smith534986f2012-04-14 00:33:13 +00002101 return;
Douglas Gregor3896fc52011-10-24 22:31:10 +00002102 }
Richard Smith534986f2012-04-14 00:33:13 +00002103
2104 StmtResult Compound = ParseCompoundStatement();
Douglas Gregorba0513d2011-10-25 01:33:02 +00002105 if (Compound.isInvalid())
2106 return;
Richard Smith534986f2012-04-14 00:33:13 +00002107
Douglas Gregorba0513d2011-10-25 01:33:02 +00002108 StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc,
2109 Result.IsIfExists,
Richard Smith534986f2012-04-14 00:33:13 +00002110 Result.SS,
Douglas Gregorba0513d2011-10-25 01:33:02 +00002111 Result.Name,
2112 Compound.get());
2113 if (DepResult.isUsable())
2114 Stmts.push_back(DepResult.get());
Douglas Gregor3896fc52011-10-24 22:31:10 +00002115 return;
2116 }
Richard Smith534986f2012-04-14 00:33:13 +00002117
Douglas Gregor3896fc52011-10-24 22:31:10 +00002118 BalancedDelimiterTracker Braces(*this, tok::l_brace);
2119 if (Braces.consumeOpen()) {
Stephen Hines651f13c2014-04-23 16:59:28 -07002120 Diag(Tok, diag::err_expected) << tok::l_brace;
Francois Pichet1e862692011-05-06 20:48:22 +00002121 return;
2122 }
Francois Pichet1e862692011-05-06 20:48:22 +00002123
Douglas Gregor3896fc52011-10-24 22:31:10 +00002124 switch (Result.Behavior) {
2125 case IEB_Parse:
2126 // Parse the statements below.
2127 break;
Chad Rosierb6604462012-07-10 21:35:27 +00002128
Douglas Gregor3896fc52011-10-24 22:31:10 +00002129 case IEB_Dependent:
2130 llvm_unreachable("Dependent case handled above");
Chad Rosierb6604462012-07-10 21:35:27 +00002131
Douglas Gregor3896fc52011-10-24 22:31:10 +00002132 case IEB_Skip:
2133 Braces.skipToEnd();
Francois Pichet1e862692011-05-06 20:48:22 +00002134 return;
2135 }
2136
2137 // Condition is true, parse the statements.
2138 while (Tok.isNot(tok::r_brace)) {
2139 StmtResult R = ParseStatementOrDeclaration(Stmts, false);
2140 if (R.isUsable())
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002141 Stmts.push_back(R.get());
Francois Pichet1e862692011-05-06 20:48:22 +00002142 }
Douglas Gregor3896fc52011-10-24 22:31:10 +00002143 Braces.consumeClose();
Francois Pichet1e862692011-05-06 20:48:22 +00002144}