blob: 5fa4f170265f1208f715bbae47500063e3cf518d [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
Chris Lattner0ccd51e2006-08-09 05:47:47 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner0ccd51e2006-08-09 05:47:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000016#include "RAIIObjectsForParser.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/Basic/Diagnostic.h"
18#include "clang/Basic/PrettyStackTrace.h"
19#include "clang/Basic/SourceManager.h"
John McCall8b0666c2010-08-20 18:27:03 +000020#include "clang/Sema/DeclSpec.h"
John McCallfaf5fb42010-08-26 23:41:50 +000021#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall8b0666c2010-08-20 18:27:03 +000022#include "clang/Sema/Scope.h"
Richard Smith4f605af2012-08-18 00:55:03 +000023#include "clang/Sema/TypoCorrection.h"
Chad Rosier32503022012-06-11 20:47:18 +000024#include "llvm/ADT/SmallString.h"
Chris Lattner0ccd51e2006-08-09 05:47:47 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// C99 6.8: Statements and Blocks.
29//===----------------------------------------------------------------------===//
30
31/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
32/// StatementOrDeclaration:
33/// statement
34/// declaration
35///
36/// statement:
37/// labeled-statement
38/// compound-statement
39/// expression-statement
40/// selection-statement
41/// iteration-statement
42/// jump-statement
Argyrios Kyrtzidisdee82912008-09-07 18:58:01 +000043/// [C++] declaration-statement
Sebastian Redlb219c902008-12-21 16:41:36 +000044/// [C++] try-block
John Wiegley1c0675e2011-04-28 01:08:34 +000045/// [MS] seh-try-block
Fariborz Jahanian90814572007-10-04 20:19:06 +000046/// [OBC] objc-throw-statement
47/// [OBC] objc-try-catch-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +000048/// [OBC] objc-synchronized-statement
Chris Lattner0116c472006-08-15 06:03:28 +000049/// [GNU] asm-statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +000050/// [OMP] openmp-construct [TODO]
51///
52/// labeled-statement:
53/// identifier ':' statement
54/// 'case' constant-expression ':' statement
55/// 'default' ':' statement
56///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000057/// selection-statement:
58/// if-statement
59/// switch-statement
60///
61/// iteration-statement:
62/// while-statement
63/// do-statement
64/// for-statement
65///
Chris Lattner9075bd72006-08-10 04:59:57 +000066/// expression-statement:
67/// expression[opt] ';'
68///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000069/// jump-statement:
70/// 'goto' identifier ';'
71/// 'continue' ';'
72/// 'break' ';'
73/// 'return' expression[opt] ';'
Chris Lattner503fadc2006-08-10 05:45:44 +000074/// [GNU] 'goto' '*' expression ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000075///
Fariborz Jahanian90814572007-10-04 20:19:06 +000076/// [OBC] objc-throw-statement:
77/// [OBC] '@' 'throw' expression ';'
Mike Stump11289f42009-09-09 15:08:12 +000078/// [OBC] '@' 'throw' ';'
79///
John McCalldadc5752010-08-24 06:29:42 +000080StmtResult
Nico Weber3cef1082011-12-22 23:26:17 +000081Parser::ParseStatementOrDeclaration(StmtVector &Stmts, bool OnlyStatement,
82 SourceLocation *TrailingElseLoc) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +000083
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +000084 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +000085
Richard Smithc202b282012-04-14 00:33:13 +000086 ParsedAttributesWithRange Attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +000087 MaybeParseCXX11Attributes(Attrs, 0, /*MightBeObjCMessageSend*/ true);
Richard Smithc202b282012-04-14 00:33:13 +000088
89 StmtResult Res = ParseStatementOrDeclarationAfterAttributes(Stmts,
90 OnlyStatement, TrailingElseLoc, Attrs);
91
92 assert((Attrs.empty() || Res.isInvalid() || Res.isUsable()) &&
93 "attributes on empty statement");
94
95 if (Attrs.empty() || Res.isInvalid())
96 return Res;
97
98 return Actions.ProcessStmtAttributes(Res.get(), Attrs.getList(), Attrs.Range);
99}
100
101StmtResult
102Parser::ParseStatementOrDeclarationAfterAttributes(StmtVector &Stmts,
103 bool OnlyStatement, SourceLocation *TrailingElseLoc,
104 ParsedAttributesWithRange &Attrs) {
105 const char *SemiError = 0;
106 StmtResult Res;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000107
Chris Lattner503fadc2006-08-10 05:45:44 +0000108 // Cases in this switch statement should fall through if the parser expects
109 // the token to end in a semicolon (in which case SemiError should be set),
110 // or they directly 'return;' if not.
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000111Retry:
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000112 tok::TokenKind Kind = Tok.getKind();
113 SourceLocation AtLoc;
114 switch (Kind) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000115 case tok::at: // May be a @try or @throw statement
116 {
Richard Smithc202b282012-04-14 00:33:13 +0000117 ProhibitAttributes(Attrs); // TODO: is it correct?
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000118 AtLoc = ConsumeToken(); // consume @
Sebastian Redlbab9a4b2008-12-11 20:12:42 +0000119 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000120 }
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000121
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000122 case tok::code_completion:
John McCallfaf5fb42010-08-26 23:41:50 +0000123 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000124 cutOffParsing();
125 return StmtError();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000126
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000127 case tok::identifier: {
128 Token Next = NextToken();
129 if (Next.is(tok::colon)) { // C99 6.8.1: labeled-statement
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000130 // identifier ':' statement
Richard Smithc202b282012-04-14 00:33:13 +0000131 return ParseLabeledStatement(Attrs);
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000132 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000133
Richard Smith4f605af2012-08-18 00:55:03 +0000134 // Look up the identifier, and typo-correct it to a keyword if it's not
135 // found.
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000136 if (Next.isNot(tok::coloncolon)) {
Richard Smith4f605af2012-08-18 00:55:03 +0000137 // Try to limit which sets of keywords should be included in typo
138 // correction based on what the next token is.
139 // FIXME: Pass the next token into the CorrectionCandidateCallback and
140 // do this filtering in a more fine-grained manner.
141 CorrectionCandidateCallback DefaultValidator;
142 DefaultValidator.WantTypeSpecifiers =
143 Next.is(tok::l_paren) || Next.is(tok::less) ||
144 Next.is(tok::identifier) || Next.is(tok::star) ||
145 Next.is(tok::amp) || Next.is(tok::l_square);
146 DefaultValidator.WantExpressionKeywords =
147 Next.is(tok::l_paren) || Next.is(tok::identifier) ||
148 Next.is(tok::arrow) || Next.is(tok::period);
149 DefaultValidator.WantRemainingKeywords =
150 Next.is(tok::l_paren) || Next.is(tok::semi) ||
151 Next.is(tok::identifier) || Next.is(tok::l_brace);
152 DefaultValidator.WantCXXNamedCasts = false;
153 if (TryAnnotateName(/*IsAddressOfOperand*/false, &DefaultValidator)
154 == ANK_Error) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000155 // Handle errors here by skipping up to the next semicolon or '}', and
156 // eat the semicolon if that's what stopped us.
157 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
158 if (Tok.is(tok::semi))
159 ConsumeToken();
160 return StmtError();
Richard Smith4f605af2012-08-18 00:55:03 +0000161 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000162
Richard Smith4f605af2012-08-18 00:55:03 +0000163 // If the identifier was typo-corrected, try again.
164 if (Tok.isNot(tok::identifier))
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000165 goto Retry;
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000166 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000167
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000168 // Fall through
169 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000170
Chris Lattner803802d2009-03-24 17:04:48 +0000171 default: {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000172 if ((getLangOpts().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner49836b42009-04-02 04:16:50 +0000173 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000174 DeclGroupPtrTy Decl = ParseDeclaration(Stmts, Declarator::BlockContext,
Richard Smithc202b282012-04-14 00:33:13 +0000175 DeclEnd, Attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000176 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattner803802d2009-03-24 17:04:48 +0000177 }
178
179 if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +0000180 Diag(Tok, diag::err_expected_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000181 return StmtError();
Chris Lattnerf8afb622006-08-10 18:26:31 +0000182 }
Mike Stump11289f42009-09-09 15:08:12 +0000183
Richard Smithc202b282012-04-14 00:33:13 +0000184 return ParseExprStatement();
Chris Lattner803802d2009-03-24 17:04:48 +0000185 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000186
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000187 case tok::kw_case: // C99 6.8.1: labeled-statement
Richard Smithc202b282012-04-14 00:33:13 +0000188 return ParseCaseStatement();
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000189 case tok::kw_default: // C99 6.8.1: labeled-statement
Richard Smithc202b282012-04-14 00:33:13 +0000190 return ParseDefaultStatement();
Sebastian Redl042ad952008-12-11 19:30:53 +0000191
Chris Lattner9075bd72006-08-10 04:59:57 +0000192 case tok::l_brace: // C99 6.8.2: compound-statement
Richard Smithc202b282012-04-14 00:33:13 +0000193 return ParseCompoundStatement();
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000194 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
Argyrios Kyrtzidis43ea78b2011-09-01 21:53:45 +0000195 bool HasLeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
196 return Actions.ActOnNullStmt(ConsumeToken(), HasLeadingEmptyMacro);
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000197 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000198
Chris Lattner9075bd72006-08-10 04:59:57 +0000199 case tok::kw_if: // C99 6.8.4.1: if-statement
Richard Smithc202b282012-04-14 00:33:13 +0000200 return ParseIfStatement(TrailingElseLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000201 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Richard Smithc202b282012-04-14 00:33:13 +0000202 return ParseSwitchStatement(TrailingElseLoc);
Sebastian Redl042ad952008-12-11 19:30:53 +0000203
Chris Lattner9075bd72006-08-10 04:59:57 +0000204 case tok::kw_while: // C99 6.8.5.1: while-statement
Richard Smithc202b282012-04-14 00:33:13 +0000205 return ParseWhileStatement(TrailingElseLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000206 case tok::kw_do: // C99 6.8.5.2: do-statement
Richard Smithc202b282012-04-14 00:33:13 +0000207 Res = ParseDoStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000208 SemiError = "do/while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000209 break;
210 case tok::kw_for: // C99 6.8.5.3: for-statement
Richard Smithc202b282012-04-14 00:33:13 +0000211 return ParseForStatement(TrailingElseLoc);
Chris Lattner503fadc2006-08-10 05:45:44 +0000212
213 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Richard Smithc202b282012-04-14 00:33:13 +0000214 Res = ParseGotoStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000215 SemiError = "goto";
Chris Lattner9075bd72006-08-10 04:59:57 +0000216 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000217 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Richard Smithc202b282012-04-14 00:33:13 +0000218 Res = ParseContinueStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000219 SemiError = "continue";
Chris Lattner503fadc2006-08-10 05:45:44 +0000220 break;
221 case tok::kw_break: // C99 6.8.6.3: break-statement
Richard Smithc202b282012-04-14 00:33:13 +0000222 Res = ParseBreakStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000223 SemiError = "break";
Chris Lattner503fadc2006-08-10 05:45:44 +0000224 break;
225 case tok::kw_return: // C99 6.8.6.4: return-statement
Richard Smithc202b282012-04-14 00:33:13 +0000226 Res = ParseReturnStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000227 SemiError = "return";
Chris Lattner503fadc2006-08-10 05:45:44 +0000228 break;
Sebastian Redl042ad952008-12-11 19:30:53 +0000229
Sebastian Redlb219c902008-12-21 16:41:36 +0000230 case tok::kw_asm: {
Richard Smithc202b282012-04-14 00:33:13 +0000231 ProhibitAttributes(Attrs);
Steve Naroffb2c80c72008-02-07 03:50:06 +0000232 bool msAsm = false;
233 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidis3050d9b2010-11-02 02:33:08 +0000234 Res = Actions.ActOnFinishFullStmt(Res.get());
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000235 if (msAsm) return Res;
Chris Lattner34a95662009-06-14 00:07:48 +0000236 SemiError = "asm";
Chris Lattner0116c472006-08-15 06:03:28 +0000237 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000238 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000239
Sebastian Redlb219c902008-12-21 16:41:36 +0000240 case tok::kw_try: // C++ 15: try-block
Richard Smithc202b282012-04-14 00:33:13 +0000241 return ParseCXXTryBlock();
John Wiegley1c0675e2011-04-28 01:08:34 +0000242
243 case tok::kw___try:
Richard Smithc202b282012-04-14 00:33:13 +0000244 ProhibitAttributes(Attrs); // TODO: is it correct?
245 return ParseSEHTryBlock();
Eli Friedmanec52f922012-02-23 23:47:16 +0000246
247 case tok::annot_pragma_vis:
Richard Smithc202b282012-04-14 00:33:13 +0000248 ProhibitAttributes(Attrs);
Eli Friedmanec52f922012-02-23 23:47:16 +0000249 HandlePragmaVisibility();
250 return StmtEmpty();
251
252 case tok::annot_pragma_pack:
Richard Smithc202b282012-04-14 00:33:13 +0000253 ProhibitAttributes(Attrs);
Eli Friedmanec52f922012-02-23 23:47:16 +0000254 HandlePragmaPack();
255 return StmtEmpty();
Eli Friedman68be1642012-10-04 02:36:51 +0000256
Eli Friedmanbbbbac62012-10-09 22:46:54 +0000257 case tok::annot_pragma_msstruct:
258 ProhibitAttributes(Attrs);
259 HandlePragmaMSStruct();
260 return StmtEmpty();
261
Eli Friedmanae8ee252012-10-08 23:52:38 +0000262 case tok::annot_pragma_align:
263 ProhibitAttributes(Attrs);
264 HandlePragmaAlign();
265 return StmtEmpty();
266
Eli Friedmanbbbbac62012-10-09 22:46:54 +0000267 case tok::annot_pragma_weak:
268 ProhibitAttributes(Attrs);
269 HandlePragmaWeak();
270 return StmtEmpty();
271
272 case tok::annot_pragma_weakalias:
273 ProhibitAttributes(Attrs);
274 HandlePragmaWeakAlias();
275 return StmtEmpty();
276
277 case tok::annot_pragma_redefine_extname:
278 ProhibitAttributes(Attrs);
279 HandlePragmaRedefineExtname();
280 return StmtEmpty();
281
Eli Friedman68be1642012-10-04 02:36:51 +0000282 case tok::annot_pragma_fp_contract:
Lang Hamesa930e712012-10-21 01:10:01 +0000283 Diag(Tok, diag::err_pragma_fp_contract_scope);
284 ConsumeToken();
285 return StmtError();
286
Eli Friedman68be1642012-10-04 02:36:51 +0000287 case tok::annot_pragma_opencl_extension:
288 ProhibitAttributes(Attrs);
289 HandlePragmaOpenCLExtension();
290 return StmtEmpty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000291
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000292 case tok::annot_pragma_captured:
293 return HandlePragmaCaptured();
294
Alexey Bataeva769e072013-03-22 06:34:35 +0000295 case tok::annot_pragma_openmp:
296 SourceLocation DeclStart = Tok.getLocation();
297 DeclGroupPtrTy Res = ParseOpenMPDeclarativeDirective();
298 return Actions.ActOnDeclStmt(Res, DeclStart, Tok.getLocation());
Sebastian Redlb219c902008-12-21 16:41:36 +0000299 }
300
Chris Lattner503fadc2006-08-10 05:45:44 +0000301 // If we reached this code, the statement must end in a semicolon.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000302 if (Tok.is(tok::semi)) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000303 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000304 } else if (!Res.isInvalid()) {
Chris Lattner8e3eed02009-06-14 00:23:56 +0000305 // If the result was valid, then we do want to diagnose this. Use
306 // ExpectAndConsume to emit the diagnostic, even though we know it won't
307 // succeed.
308 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner0046de12008-11-13 18:52:53 +0000309 // Skip until we see a } or ;, but don't eat it.
310 SkipUntil(tok::r_brace, true, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000311 }
Mike Stump11289f42009-09-09 15:08:12 +0000312
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000313 return Res;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000314}
315
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000316/// \brief Parse an expression statement.
Richard Smithc202b282012-04-14 00:33:13 +0000317StmtResult Parser::ParseExprStatement() {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000318 // If a case keyword is missing, this is where it should be inserted.
319 Token OldToken = Tok;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000320
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000321 // expression[opt] ';'
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000322 ExprResult Expr(ParseExpression());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000323 if (Expr.isInvalid()) {
324 // If the expression is invalid, skip ahead to the next semicolon or '}'.
325 // Not doing this opens us up to the possibility of infinite loops if
326 // ParseExpression does not consume any tokens.
327 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
328 if (Tok.is(tok::semi))
329 ConsumeToken();
John McCalleaef89b2013-03-22 02:10:40 +0000330 return Actions.ActOnExprStmtError();
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000331 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000332
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000333 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
334 Actions.CheckCaseExpression(Expr.get())) {
335 // If a constant expression is followed by a colon inside a switch block,
336 // suggest a missing case keyword.
337 Diag(OldToken, diag::err_expected_case_before_expression)
338 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000339
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000340 // Recover parsing as a case statement.
Richard Smithc202b282012-04-14 00:33:13 +0000341 return ParseCaseStatement(/*MissingCase=*/true, Expr);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000342 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000343
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000344 // Otherwise, eat the semicolon.
345 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +0000346 return Actions.ActOnExprStmt(Expr);
John Wiegley1c0675e2011-04-28 01:08:34 +0000347}
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000348
Richard Smithc202b282012-04-14 00:33:13 +0000349StmtResult Parser::ParseSEHTryBlock() {
John Wiegley1c0675e2011-04-28 01:08:34 +0000350 assert(Tok.is(tok::kw___try) && "Expected '__try'");
351 SourceLocation Loc = ConsumeToken();
352 return ParseSEHTryBlockCommon(Loc);
353}
354
355/// ParseSEHTryBlockCommon
356///
357/// seh-try-block:
358/// '__try' compound-statement seh-handler
359///
360/// seh-handler:
361/// seh-except-block
362/// seh-finally-block
363///
364StmtResult Parser::ParseSEHTryBlockCommon(SourceLocation TryLoc) {
365 if(Tok.isNot(tok::l_brace))
366 return StmtError(Diag(Tok,diag::err_expected_lbrace));
367
Joao Matos566359c2012-09-04 17:49:35 +0000368 StmtResult TryBlock(ParseCompoundStatement());
John Wiegley1c0675e2011-04-28 01:08:34 +0000369 if(TryBlock.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000370 return TryBlock;
John Wiegley1c0675e2011-04-28 01:08:34 +0000371
372 StmtResult Handler;
Richard Smithc202b282012-04-14 00:33:13 +0000373 if (Tok.is(tok::identifier) &&
Douglas Gregor60060d62011-10-21 03:57:52 +0000374 Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +0000375 SourceLocation Loc = ConsumeToken();
376 Handler = ParseSEHExceptBlock(Loc);
377 } else if (Tok.is(tok::kw___finally)) {
378 SourceLocation Loc = ConsumeToken();
379 Handler = ParseSEHFinallyBlock(Loc);
380 } else {
381 return StmtError(Diag(Tok,diag::err_seh_expected_handler));
382 }
383
384 if(Handler.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000385 return Handler;
John Wiegley1c0675e2011-04-28 01:08:34 +0000386
387 return Actions.ActOnSEHTryBlock(false /* IsCXXTry */,
388 TryLoc,
389 TryBlock.take(),
390 Handler.take());
391}
392
393/// ParseSEHExceptBlock - Handle __except
394///
395/// seh-except-block:
396/// '__except' '(' seh-filter-expression ')' compound-statement
397///
398StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) {
399 PoisonIdentifierRAIIObject raii(Ident__exception_code, false),
400 raii2(Ident___exception_code, false),
401 raii3(Ident_GetExceptionCode, false);
402
403 if(ExpectAndConsume(tok::l_paren,diag::err_expected_lparen))
404 return StmtError();
405
406 ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope);
407
David Blaikiebbafb8a2012-03-11 07:00:24 +0000408 if (getLangOpts().Borland) {
Francois Pichetbfaf4772011-04-28 03:14:31 +0000409 Ident__exception_info->setIsPoisoned(false);
410 Ident___exception_info->setIsPoisoned(false);
411 Ident_GetExceptionInfo->setIsPoisoned(false);
412 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000413 ExprResult FilterExpr(ParseExpression());
Francois Pichetbfaf4772011-04-28 03:14:31 +0000414
David Blaikiebbafb8a2012-03-11 07:00:24 +0000415 if (getLangOpts().Borland) {
Francois Pichetbfaf4772011-04-28 03:14:31 +0000416 Ident__exception_info->setIsPoisoned(true);
417 Ident___exception_info->setIsPoisoned(true);
418 Ident_GetExceptionInfo->setIsPoisoned(true);
419 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000420
421 if(FilterExpr.isInvalid())
422 return StmtError();
423
424 if(ExpectAndConsume(tok::r_paren,diag::err_expected_rparen))
425 return StmtError();
426
Richard Smithc202b282012-04-14 00:33:13 +0000427 StmtResult Block(ParseCompoundStatement());
John Wiegley1c0675e2011-04-28 01:08:34 +0000428
429 if(Block.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000430 return Block;
John Wiegley1c0675e2011-04-28 01:08:34 +0000431
432 return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.take(), Block.take());
433}
434
435/// ParseSEHFinallyBlock - Handle __finally
436///
437/// seh-finally-block:
438/// '__finally' compound-statement
439///
440StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyBlock) {
441 PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
442 raii2(Ident___abnormal_termination, false),
443 raii3(Ident_AbnormalTermination, false);
444
Richard Smithc202b282012-04-14 00:33:13 +0000445 StmtResult Block(ParseCompoundStatement());
John Wiegley1c0675e2011-04-28 01:08:34 +0000446 if(Block.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000447 return Block;
John Wiegley1c0675e2011-04-28 01:08:34 +0000448
449 return Actions.ActOnSEHFinallyBlock(FinallyBlock,Block.take());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000450}
451
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000452/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000453///
454/// labeled-statement:
455/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000456/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000457///
Richard Smithc202b282012-04-14 00:33:13 +0000458StmtResult Parser::ParseLabeledStatement(ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000459 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
460 "Not an identifier!");
461
462 Token IdentTok = Tok; // Save the whole token.
463 ConsumeToken(); // eat the identifier.
464
465 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000466
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000467 // identifier ':' statement
468 SourceLocation ColonLoc = ConsumeToken();
469
Richard Smithc202b282012-04-14 00:33:13 +0000470 // Read label attributes, if present. attrs will contain both C++11 and GNU
471 // attributes (if present) after this point.
John McCall53fa7142010-12-24 02:08:15 +0000472 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000473
John McCalldadc5752010-08-24 06:29:42 +0000474 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000475
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000476 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000477 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000478 SubStmt = Actions.ActOnNullStmt(ColonLoc);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000479
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000480 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
481 IdentTok.getLocation());
Richard Smithc202b282012-04-14 00:33:13 +0000482 if (AttributeList *Attrs = attrs.getList()) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000483 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
Richard Smithc202b282012-04-14 00:33:13 +0000484 attrs.clear();
485 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000486
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000487 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
488 SubStmt.get());
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000489}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000490
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000491/// ParseCaseStatement
492/// labeled-statement:
493/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000494/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000495///
Richard Smithc202b282012-04-14 00:33:13 +0000496StmtResult Parser::ParseCaseStatement(bool MissingCase, ExprResult Expr) {
Richard Smithd4257d82011-04-21 22:48:40 +0000497 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Mike Stump11289f42009-09-09 15:08:12 +0000498
Chris Lattner34a22092009-03-04 04:23:07 +0000499 // It is very very common for code to contain many case statements recursively
500 // nested, as in (but usually without indentation):
501 // case 1:
502 // case 2:
503 // case 3:
504 // case 4:
505 // case 5: etc.
506 //
507 // Parsing this naively works, but is both inefficient and can cause us to run
508 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner2b19a6582009-03-04 18:24:58 +0000509 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner34a22092009-03-04 04:23:07 +0000510 // but all the grossness is constrained to ParseCaseStatement (and some
511 // wierdness in the actions), so this is just local grossness :).
Mike Stump11289f42009-09-09 15:08:12 +0000512
Chris Lattner34a22092009-03-04 04:23:07 +0000513 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
514 // example above.
John McCalldadc5752010-08-24 06:29:42 +0000515 StmtResult TopLevelCase(true);
Mike Stump11289f42009-09-09 15:08:12 +0000516
Chris Lattner34a22092009-03-04 04:23:07 +0000517 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
518 // gets updated each time a new case is parsed, and whose body is unset so
519 // far. When parsing 'case 4', this is the 'case 3' node.
Richard Trieu3481fcd2011-09-09 02:16:15 +0000520 Stmt *DeepestParsedCaseStmt = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000521
Chris Lattner34a22092009-03-04 04:23:07 +0000522 // While we have case statements, eat and stack them.
David Majnemer0ac67fa2011-06-13 05:50:12 +0000523 SourceLocation ColonLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000524 do {
Richard Trieu2c850c02011-04-21 21:44:26 +0000525 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
526 ConsumeToken(); // eat the 'case'.
Mike Stump11289f42009-09-09 15:08:12 +0000527
Douglas Gregord328d572009-09-21 18:10:23 +0000528 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000529 Actions.CodeCompleteCase(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000530 cutOffParsing();
531 return StmtError();
Douglas Gregord328d572009-09-21 18:10:23 +0000532 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000533
Chris Lattner125c0ee2009-12-10 00:38:54 +0000534 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
535 /// Disable this form of error recovery while we're parsing the case
536 /// expression.
537 ColonProtectionRAIIObject ColonProtection(*this);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000538
Richard Trieu2c850c02011-04-21 21:44:26 +0000539 ExprResult LHS(MissingCase ? Expr : ParseConstantExpression());
540 MissingCase = false;
Chris Lattner34a22092009-03-04 04:23:07 +0000541 if (LHS.isInvalid()) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000542 SkipUntil(tok::colon);
Sebastian Redl042ad952008-12-11 19:30:53 +0000543 return StmtError();
Chris Lattner476c3ad2006-08-13 22:09:58 +0000544 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000545
Chris Lattner34a22092009-03-04 04:23:07 +0000546 // GNU case range extension.
547 SourceLocation DotDotDotLoc;
John McCalldadc5752010-08-24 06:29:42 +0000548 ExprResult RHS;
Chris Lattner34a22092009-03-04 04:23:07 +0000549 if (Tok.is(tok::ellipsis)) {
550 Diag(Tok, diag::ext_gnu_case_range);
551 DotDotDotLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000552
Chris Lattner34a22092009-03-04 04:23:07 +0000553 RHS = ParseConstantExpression();
554 if (RHS.isInvalid()) {
555 SkipUntil(tok::colon);
556 return StmtError();
557 }
558 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000559
Chris Lattner125c0ee2009-12-10 00:38:54 +0000560 ColonProtection.restore();
Sebastian Redl042ad952008-12-11 19:30:53 +0000561
John McCall0140bfe2011-01-22 09:28:32 +0000562 if (Tok.is(tok::colon)) {
563 ColonLoc = ConsumeToken();
564
565 // Treat "case blah;" as a typo for "case blah:".
566 } else if (Tok.is(tok::semi)) {
567 ColonLoc = ConsumeToken();
568 Diag(ColonLoc, diag::err_expected_colon_after) << "'case'"
569 << FixItHint::CreateReplacement(ColonLoc, ":");
570 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000571 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
572 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'"
573 << FixItHint::CreateInsertion(ExpectedLoc, ":");
574 ColonLoc = ExpectedLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000575 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000576
John McCalldadc5752010-08-24 06:29:42 +0000577 StmtResult Case =
John McCallb268a282010-08-23 23:25:46 +0000578 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
579 RHS.get(), ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000580
Chris Lattner34a22092009-03-04 04:23:07 +0000581 // If we had a sema error parsing this case, then just ignore it and
582 // continue parsing the sub-stmt.
583 if (Case.isInvalid()) {
584 if (TopLevelCase.isInvalid()) // No parsed case stmts.
585 return ParseStatement();
586 // Otherwise, just don't add it as a nested case.
587 } else {
588 // If this is the first case statement we parsed, it becomes TopLevelCase.
589 // Otherwise we link it into the current chain.
John McCall37ad5512010-08-23 06:44:23 +0000590 Stmt *NextDeepest = Case.get();
Chris Lattner34a22092009-03-04 04:23:07 +0000591 if (TopLevelCase.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000592 TopLevelCase = Case;
Chris Lattner34a22092009-03-04 04:23:07 +0000593 else
John McCallb268a282010-08-23 23:25:46 +0000594 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner34a22092009-03-04 04:23:07 +0000595 DeepestParsedCaseStmt = NextDeepest;
596 }
Mike Stump11289f42009-09-09 15:08:12 +0000597
Chris Lattner34a22092009-03-04 04:23:07 +0000598 // Handle all case statements.
599 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000600
Chris Lattner34a22092009-03-04 04:23:07 +0000601 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump11289f42009-09-09 15:08:12 +0000602
Chris Lattner34a22092009-03-04 04:23:07 +0000603 // If we found a non-case statement, start by parsing it.
John McCalldadc5752010-08-24 06:29:42 +0000604 StmtResult SubStmt;
Mike Stump11289f42009-09-09 15:08:12 +0000605
Chris Lattner34a22092009-03-04 04:23:07 +0000606 if (Tok.isNot(tok::r_brace)) {
607 SubStmt = ParseStatement();
608 } else {
609 // Nicely diagnose the common error "switch (X) { case 4: }", which is
610 // not valid.
David Majnemerc6a99872011-06-14 15:24:38 +0000611 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
Richard Smith1002d102012-02-17 01:35:32 +0000612 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
613 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
Chris Lattner34a22092009-03-04 04:23:07 +0000614 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000615 }
Mike Stump11289f42009-09-09 15:08:12 +0000616
Chris Lattner34a22092009-03-04 04:23:07 +0000617 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000618 if (SubStmt.isInvalid())
Chris Lattner34a22092009-03-04 04:23:07 +0000619 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000620
Chris Lattner34a22092009-03-04 04:23:07 +0000621 // Install the body into the most deeply-nested case.
John McCallb268a282010-08-23 23:25:46 +0000622 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000623
Chris Lattner34a22092009-03-04 04:23:07 +0000624 // Return the top level parsed statement tree.
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000625 return TopLevelCase;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000626}
627
628/// ParseDefaultStatement
629/// labeled-statement:
630/// 'default' ':' statement
631/// Note that this does not parse the 'statement' at the end.
632///
Richard Smithc202b282012-04-14 00:33:13 +0000633StmtResult Parser::ParseDefaultStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000634 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000635 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000636
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000637 SourceLocation ColonLoc;
John McCall0140bfe2011-01-22 09:28:32 +0000638 if (Tok.is(tok::colon)) {
639 ColonLoc = ConsumeToken();
640
641 // Treat "default;" as a typo for "default:".
642 } else if (Tok.is(tok::semi)) {
643 ColonLoc = ConsumeToken();
644 Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
645 << FixItHint::CreateReplacement(ColonLoc, ":");
646 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000647 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
648 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
649 << FixItHint::CreateInsertion(ExpectedLoc, ":");
650 ColonLoc = ExpectedLoc;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000651 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000652
Richard Smith1002d102012-02-17 01:35:32 +0000653 StmtResult SubStmt;
654
655 if (Tok.isNot(tok::r_brace)) {
656 SubStmt = ParseStatement();
657 } else {
658 // Diagnose the common error "switch (X) {... default: }", which is
659 // not valid.
David Majnemerc6a99872011-06-14 15:24:38 +0000660 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
Richard Smith1002d102012-02-17 01:35:32 +0000661 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
662 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
663 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000664 }
665
Richard Smith1002d102012-02-17 01:35:32 +0000666 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000667 if (SubStmt.isInvalid())
Richard Smith1002d102012-02-17 01:35:32 +0000668 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl042ad952008-12-11 19:30:53 +0000669
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000670 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000671 SubStmt.get(), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000672}
673
Richard Smithc202b282012-04-14 00:33:13 +0000674StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
675 return ParseCompoundStatement(isStmtExpr, Scope::DeclScope);
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000676}
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000677
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000678/// ParseCompoundStatement - Parse a "{}" block.
679///
680/// compound-statement: [C99 6.8.2]
681/// { block-item-list[opt] }
682/// [GNU] { label-declarations block-item-list } [TODO]
683///
684/// block-item-list:
685/// block-item
686/// block-item-list block-item
687///
688/// block-item:
689/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000690/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000691/// statement
692/// [OMP] openmp-directive [TODO]
693///
694/// [GNU] label-declarations:
695/// [GNU] label-declaration
696/// [GNU] label-declarations label-declaration
697///
698/// [GNU] label-declaration:
699/// [GNU] '__label__' identifier-list ';'
700///
701/// [OMP] openmp-directive: [TODO]
702/// [OMP] barrier-directive
703/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000704///
Richard Smithc202b282012-04-14 00:33:13 +0000705StmtResult Parser::ParseCompoundStatement(bool isStmtExpr,
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000706 unsigned ScopeFlags) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000707 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000708
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000709 // Enter a scope to hold everything within the compound stmt. Compound
710 // statements can always hold declarations.
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000711 ParseScope CompoundScope(this, ScopeFlags);
Chris Lattnerf2978802007-01-21 06:52:16 +0000712
713 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000714 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000715}
716
Lang Hames2954cea2012-11-03 22:29:05 +0000717/// Parse any pragmas at the start of the compound expression. We handle these
718/// separately since some pragmas (FP_CONTRACT) must appear before any C
719/// statement in the compound, but may be intermingled with other pragmas.
720void Parser::ParseCompoundStatementLeadingPragmas() {
721 bool checkForPragmas = true;
722 while (checkForPragmas) {
723 switch (Tok.getKind()) {
724 case tok::annot_pragma_vis:
725 HandlePragmaVisibility();
726 break;
727 case tok::annot_pragma_pack:
728 HandlePragmaPack();
729 break;
730 case tok::annot_pragma_msstruct:
731 HandlePragmaMSStruct();
732 break;
733 case tok::annot_pragma_align:
734 HandlePragmaAlign();
735 break;
736 case tok::annot_pragma_weak:
737 HandlePragmaWeak();
738 break;
739 case tok::annot_pragma_weakalias:
740 HandlePragmaWeakAlias();
741 break;
742 case tok::annot_pragma_redefine_extname:
743 HandlePragmaRedefineExtname();
744 break;
745 case tok::annot_pragma_opencl_extension:
746 HandlePragmaOpenCLExtension();
747 break;
748 case tok::annot_pragma_fp_contract:
749 HandlePragmaFPContract();
750 break;
751 default:
752 checkForPragmas = false;
753 break;
754 }
755 }
756
757}
758
Chris Lattnerf2978802007-01-21 06:52:16 +0000759/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000760/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000761/// consume the '}' at the end of the block. It does not manipulate the scope
762/// stack.
John McCalldadc5752010-08-24 06:29:42 +0000763StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000764 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000765 Tok.getLocation(),
766 "in compound statement ('{}')");
Lang Hames5de91cc2012-10-02 04:45:10 +0000767
768 // Record the state of the FP_CONTRACT pragma, restore on leaving the
769 // compound statement.
770 Sema::FPContractStateRAII SaveFPContractState(Actions);
771
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000772 InMessageExpressionRAIIObject InMessage(*this, false);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000773 BalancedDelimiterTracker T(*this, tok::l_brace);
774 if (T.consumeOpen())
775 return StmtError();
Chris Lattnerf2978802007-01-21 06:52:16 +0000776
Dmitri Gribenko800ddf32012-02-14 22:14:32 +0000777 Sema::CompoundScopeRAII CompoundScope(Actions);
778
Lang Hames2954cea2012-11-03 22:29:05 +0000779 // Parse any pragmas at the beginning of the compound statement.
780 ParseCompoundStatementLeadingPragmas();
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000781
Lang Hames2954cea2012-11-03 22:29:05 +0000782 StmtVector Stmts;
Lang Hamesa930e712012-10-21 01:10:01 +0000783
Chris Lattner43e7f312011-02-18 02:08:43 +0000784 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
785 // only allowed at the start of a compound stmt regardless of the language.
786 while (Tok.is(tok::kw___label__)) {
787 SourceLocation LabelLoc = ConsumeToken();
788 Diag(LabelLoc, diag::ext_gnu_local_label);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000789
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000790 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner43e7f312011-02-18 02:08:43 +0000791 while (1) {
792 if (Tok.isNot(tok::identifier)) {
793 Diag(Tok, diag::err_expected_ident);
794 break;
795 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000796
Chris Lattner43e7f312011-02-18 02:08:43 +0000797 IdentifierInfo *II = Tok.getIdentifierInfo();
798 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000799 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000800
Chris Lattner43e7f312011-02-18 02:08:43 +0000801 if (!Tok.is(tok::comma))
802 break;
803 ConsumeToken();
804 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000805
John McCall084e83d2011-03-24 11:26:52 +0000806 DeclSpec DS(AttrFactory);
Chris Lattner43e7f312011-02-18 02:08:43 +0000807 DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
808 DeclsInGroup.data(), DeclsInGroup.size());
809 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000810
Chris Lattner02f1b612012-04-28 16:12:17 +0000811 ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
Chris Lattner43e7f312011-02-18 02:08:43 +0000812 if (R.isUsable())
813 Stmts.push_back(R.release());
814 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000815
Chris Lattner43e7f312011-02-18 02:08:43 +0000816 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000817 if (Tok.is(tok::annot_pragma_unused)) {
818 HandlePragmaUnused();
819 continue;
820 }
821
David Blaikiebbafb8a2012-03-11 07:00:24 +0000822 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet4a7de3e2011-05-06 20:48:22 +0000823 Tok.is(tok::kw___if_not_exists))) {
824 ParseMicrosoftIfExistsStatement(Stmts);
825 continue;
826 }
827
John McCalldadc5752010-08-24 06:29:42 +0000828 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000829 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000830 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000831 } else {
832 // __extension__ can start declarations and it can also be a unary
833 // operator for expressions. Consume multiple __extension__ markers here
834 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000835 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000836 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000837 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000838 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +0000839
John McCall084e83d2011-03-24 11:26:52 +0000840 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000841 MaybeParseCXX11Attributes(attrs, 0, /*MightBeObjCMessageSend*/ true);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000842
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000843 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000844 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +0000845 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +0000846 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +0000847 ExtensionRAIIObject O(Diags);
848
Chris Lattner49836b42009-04-02 04:16:50 +0000849 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000850 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
851 Declarator::BlockContext, DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000852 attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000853 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000854 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000855 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +0000856 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +0000857
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000858 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000859 SkipUntil(tok::semi);
860 continue;
861 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000862
Alexis Hunt96d5c762009-11-21 08:43:09 +0000863 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +0000864 // Eat the semicolon at the end of stmt and convert the expr into a
865 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000866 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +0000867 R = Actions.ActOnExprStmt(Res);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000868 }
869 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000870
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000871 if (R.isUsable())
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000872 Stmts.push_back(R.release());
Chris Lattner30f910e2006-10-16 05:52:41 +0000873 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000874
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +0000875 SourceLocation CloseLoc = Tok.getLocation();
876
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000877 // We broke out of the while loop because we found a '}' or EOF.
Nico Webera48b6c22012-12-30 23:36:56 +0000878 if (!T.consumeClose())
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +0000879 // Recover by creating a compound statement with what we parsed so far,
880 // instead of dropping everything and returning StmtError();
Nico Webera48b6c22012-12-30 23:36:56 +0000881 CloseLoc = T.getCloseLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +0000882
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +0000883 return Actions.ActOnCompoundStmt(T.getOpenLocation(), CloseLoc,
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000884 Stmts, isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000885}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000886
Chris Lattnerc0081db2008-12-12 06:31:07 +0000887/// ParseParenExprOrCondition:
888/// [C ] '(' expression ')'
Chris Lattner10da53c2008-12-12 06:35:28 +0000889/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattnerc0081db2008-12-12 06:31:07 +0000890///
891/// This function parses and performs error recovery on the specified condition
892/// or expression (depending on whether we're in C++ or C mode). This function
893/// goes out of its way to recover well. It returns true if there was a parser
894/// error (the right paren couldn't be found), which indicates that the caller
895/// should try to recover harder. It returns false if the condition is
896/// successfully parsed. Note that a successful parse can still have semantic
897/// errors in the condition.
John McCalldadc5752010-08-24 06:29:42 +0000898bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCall48871652010-08-21 09:40:31 +0000899 Decl *&DeclResult,
Douglas Gregore60e41a2010-05-06 17:25:47 +0000900 SourceLocation Loc,
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000901 bool ConvertToBoolean) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000902 BalancedDelimiterTracker T(*this, tok::l_paren);
903 T.consumeOpen();
904
David Blaikiebbafb8a2012-03-11 07:00:24 +0000905 if (getLangOpts().CPlusPlus)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000906 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000907 else {
908 ExprResult = ParseExpression();
John McCall48871652010-08-21 09:40:31 +0000909 DeclResult = 0;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000910
Douglas Gregore60e41a2010-05-06 17:25:47 +0000911 // If required, convert to a boolean value.
912 if (!ExprResult.isInvalid() && ConvertToBoolean)
913 ExprResult
John McCallb268a282010-08-23 23:25:46 +0000914 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000915 }
Mike Stump11289f42009-09-09 15:08:12 +0000916
Chris Lattnerc0081db2008-12-12 06:31:07 +0000917 // If the parser was confused by the condition and we don't have a ')', try to
918 // recover by skipping ahead to a semi and bailing out. If condexp is
919 // semantically invalid but we have well formed code, keep going.
John McCall48871652010-08-21 09:40:31 +0000920 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000921 SkipUntil(tok::semi);
922 // Skipping may have stopped if it found the containing ')'. If so, we can
923 // continue parsing the if statement.
924 if (Tok.isNot(tok::r_paren))
925 return true;
926 }
Mike Stump11289f42009-09-09 15:08:12 +0000927
Chris Lattnerc0081db2008-12-12 06:31:07 +0000928 // Otherwise the condition is valid or the rparen is present.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000929 T.consumeClose();
Chad Rosier67055f52012-07-10 21:35:27 +0000930
Chris Lattner70d44982012-04-28 16:24:20 +0000931 // Check for extraneous ')'s to catch things like "if (foo())) {". We know
932 // that all callers are looking for a statement after the condition, so ")"
933 // isn't valid.
934 while (Tok.is(tok::r_paren)) {
935 Diag(Tok, diag::err_extraneous_rparen_in_condition)
936 << FixItHint::CreateRemoval(Tok.getLocation());
937 ConsumeParen();
938 }
Chad Rosier67055f52012-07-10 21:35:27 +0000939
Chris Lattnerc0081db2008-12-12 06:31:07 +0000940 return false;
941}
942
943
Chris Lattnerc951dae2006-08-10 04:23:57 +0000944/// ParseIfStatement
945/// if-statement: [C99 6.8.4.1]
946/// 'if' '(' expression ')' statement
947/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000948/// [C++] 'if' '(' condition ')' statement
949/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000950///
Richard Smithc202b282012-04-14 00:33:13 +0000951StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000952 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000953 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000954
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000955 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000956 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +0000957 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +0000958 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000959 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000960
David Blaikiebbafb8a2012-03-11 07:00:24 +0000961 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000962
Chris Lattner2dd1b722007-08-26 23:08:06 +0000963 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
964 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000965 //
966 // C++ 6.4p3:
967 // A name introduced by a declaration in a condition is in scope from its
968 // point of declaration until the end of the substatements controlled by the
969 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000970 // C++ 3.3.2p4:
971 // Names declared in the for-init-statement, and in the condition of if,
972 // while, for, and switch statements are local to the if, while, for, or
973 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000974 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000975 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000976
Chris Lattnerc951dae2006-08-10 04:23:57 +0000977 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000978 ExprResult CondExp;
John McCall48871652010-08-21 09:40:31 +0000979 Decl *CondVar = 0;
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000980 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000981 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000982
David Blaikiea5696df2012-05-16 04:20:04 +0000983 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get(), IfLoc));
Mike Stump11289f42009-09-09 15:08:12 +0000984
Chris Lattner8fb26252007-08-22 05:28:50 +0000985 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000986 // there is no compound stmt. C90 does not have this clause. We only do this
987 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000988 //
989 // C++ 6.4p1:
990 // The substatement in a selection-statement (each substatement, in the else
991 // form of the if statement) implicitly defines a local scope.
992 //
993 // For C++ we create a scope for the condition and a new scope for
994 // substatements because:
995 // -When the 'then' scope exits, we want the condition declaration to still be
996 // active for the 'else' scope too.
997 // -Sema will detect name clashes by considering declarations of a
998 // 'ControlScope' as part of its direct subscope.
999 // -If we wanted the condition and substatement to be in the same scope, we
1000 // would have to notify ParseStatement not to create a new scope. It's
1001 // simpler to let it create a new scope.
1002 //
Mike Stump11289f42009-09-09 15:08:12 +00001003 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001004 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001005
Chris Lattner5c5808a2007-10-29 05:08:52 +00001006 // Read the 'then' stmt.
1007 SourceLocation ThenStmtLoc = Tok.getLocation();
Nico Weber3cef1082011-12-22 23:26:17 +00001008
1009 SourceLocation InnerStatementTrailingElseLoc;
1010 StmtResult ThenStmt(ParseStatement(&InnerStatementTrailingElseLoc));
Chris Lattnerac4471c2007-05-28 05:38:24 +00001011
Chris Lattner37e54f42007-08-22 05:16:28 +00001012 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001013 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001014
Chris Lattnerc951dae2006-08-10 04:23:57 +00001015 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +00001016 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +00001017 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +00001018 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001019
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001020 if (Tok.is(tok::kw_else)) {
Nico Weber3cef1082011-12-22 23:26:17 +00001021 if (TrailingElseLoc)
1022 *TrailingElseLoc = Tok.getLocation();
1023
Chris Lattneraf635312006-10-16 06:06:51 +00001024 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +00001025 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +00001026
Chris Lattner8fb26252007-08-22 05:28:50 +00001027 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001028 // there is no compound stmt. C90 does not have this clause. We only do
1029 // this if the body isn't a compound statement to avoid push/pop in common
1030 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001031 //
1032 // C++ 6.4p1:
1033 // The substatement in a selection-statement (each substatement, in the else
1034 // form of the if statement) implicitly defines a local scope.
1035 //
Sebastian Redl042ad952008-12-11 19:30:53 +00001036 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001037 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001038
Chris Lattner30f910e2006-10-16 05:52:41 +00001039 ElseStmt = ParseStatement();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001040
Chris Lattner37e54f42007-08-22 05:16:28 +00001041 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001042 InnerScope.Exit();
Douglas Gregor4ecb7202011-07-30 08:36:53 +00001043 } else if (Tok.is(tok::code_completion)) {
1044 Actions.CodeCompleteAfterIf(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001045 cutOffParsing();
1046 return StmtError();
Nico Weber3cef1082011-12-22 23:26:17 +00001047 } else if (InnerStatementTrailingElseLoc.isValid()) {
1048 Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else);
Chris Lattnerc951dae2006-08-10 04:23:57 +00001049 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001050
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001051 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +00001052
Chris Lattner5c5808a2007-10-29 05:08:52 +00001053 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +00001054 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +00001055 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001056 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
1057 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
1058 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +00001059 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +00001060 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +00001061 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001062
Chris Lattner5c5808a2007-10-29 05:08:52 +00001063 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001064 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001065 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001066 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001067 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001068
John McCallb268a282010-08-23 23:25:46 +00001069 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +00001070 ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +00001071}
1072
Chris Lattner9075bd72006-08-10 04:59:57 +00001073/// ParseSwitchStatement
1074/// switch-statement:
1075/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001076/// [C++] 'switch' '(' condition ')' statement
Richard Smithc202b282012-04-14 00:33:13 +00001077StmtResult Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001078 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001079 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +00001080
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001081 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001082 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +00001083 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001084 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001085 }
Chris Lattner2dd1b722007-08-26 23:08:06 +00001086
David Blaikiebbafb8a2012-03-11 07:00:24 +00001087 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001088
Chris Lattner2dd1b722007-08-26 23:08:06 +00001089 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1090 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001091 //
1092 // C++ 6.4p3:
1093 // A name introduced by a declaration in a condition is in scope from its
1094 // point of declaration until the end of the substatements controlled by the
1095 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001096 // C++ 3.3.2p4:
1097 // Names declared in the for-init-statement, and in the condition of if,
1098 // while, for, and switch statements are local to the if, while, for, or
1099 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001100 //
Richard Trieu2c850c02011-04-21 21:44:26 +00001101 unsigned ScopeFlags = Scope::BreakScope | Scope::SwitchScope;
Chris Lattnerc0081db2008-12-12 06:31:07 +00001102 if (C99orCXX)
1103 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001104 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001105
Chris Lattner9075bd72006-08-10 04:59:57 +00001106 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001107 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +00001108 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001109 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redlb62406f2008-12-11 19:48:14 +00001110 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +00001111
John McCalldadc5752010-08-24 06:29:42 +00001112 StmtResult Switch
John McCallb268a282010-08-23 23:25:46 +00001113 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001114
Douglas Gregore60e41a2010-05-06 17:25:47 +00001115 if (Switch.isInvalid()) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001116 // Skip the switch body.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001117 // FIXME: This is not optimal recovery, but parsing the body is more
1118 // dangerous due to the presence of case and default statements, which
1119 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +00001120 if (Tok.is(tok::l_brace)) {
1121 ConsumeBrace();
1122 SkipUntil(tok::r_brace, false, false);
1123 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +00001124 SkipUntil(tok::semi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001125 return Switch;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001126 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001127
Chris Lattner8fb26252007-08-22 05:28:50 +00001128 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001129 // there is no compound stmt. C90 does not have this clause. We only do this
1130 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001131 //
1132 // C++ 6.4p1:
1133 // The substatement in a selection-statement (each substatement, in the else
1134 // form of the if statement) implicitly defines a local scope.
1135 //
1136 // See comments in ParseIfStatement for why we create a scope for the
1137 // condition and a new scope for substatement in C++.
1138 //
Mike Stump11289f42009-09-09 15:08:12 +00001139 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001140 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +00001141
Chris Lattner9075bd72006-08-10 04:59:57 +00001142 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001143 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001144
Chris Lattner8fd2d012010-01-24 01:50:29 +00001145 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001146 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001147 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001148
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001149 if (Body.isInvalid()) {
Chris Lattner8fd2d012010-01-24 01:50:29 +00001150 // FIXME: Remove the case statement list from the Switch statement.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001151
1152 // Put the synthesized null statement on the same line as the end of switch
1153 // condition.
1154 SourceLocation SynthesizedNullStmtLocation = Cond.get()->getLocEnd();
1155 Body = Actions.ActOnNullStmt(SynthesizedNullStmtLocation);
1156 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001157
John McCallb268a282010-08-23 23:25:46 +00001158 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001159}
1160
1161/// ParseWhileStatement
1162/// while-statement: [C99 6.8.5.1]
1163/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001164/// [C++] 'while' '(' condition ')' statement
Richard Smithc202b282012-04-14 00:33:13 +00001165StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001166 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +00001167 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +00001168 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001169
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001170 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001171 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +00001172 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001173 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001174 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001175
David Blaikiebbafb8a2012-03-11 07:00:24 +00001176 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001177
Chris Lattner2dd1b722007-08-26 23:08:06 +00001178 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1179 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001180 //
1181 // C++ 6.4p3:
1182 // A name introduced by a declaration in a condition is in scope from its
1183 // point of declaration until the end of the substatements controlled by the
1184 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001185 // C++ 3.3.2p4:
1186 // Names declared in the for-init-statement, and in the condition of if,
1187 // while, for, and switch statements are local to the if, while, for, or
1188 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001189 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001190 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001191 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001192 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1193 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001194 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001195 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1196 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001197
Chris Lattner9075bd72006-08-10 04:59:57 +00001198 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001199 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +00001200 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001201 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001202 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001203
David Blaikiea5696df2012-05-16 04:20:04 +00001204 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get(), WhileLoc));
Mike Stump11289f42009-09-09 15:08:12 +00001205
Chris Lattner8fb26252007-08-22 05:28:50 +00001206 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001207 // there is no compound stmt. C90 does not have this clause. We only do this
1208 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001209 //
1210 // C++ 6.5p2:
1211 // The substatement in an iteration-statement implicitly defines a local scope
1212 // which is entered and exited each time through the loop.
1213 //
1214 // See comments in ParseIfStatement for why we create a scope for the
1215 // condition and a new scope for substatement in C++.
1216 //
Mike Stump11289f42009-09-09 15:08:12 +00001217 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001218 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001219
Chris Lattner9075bd72006-08-10 04:59:57 +00001220 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001221 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001222
Chris Lattner8fb26252007-08-22 05:28:50 +00001223 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001224 InnerScope.Exit();
1225 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001226
John McCall48871652010-08-21 09:40:31 +00001227 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001228 return StmtError();
1229
John McCallb268a282010-08-23 23:25:46 +00001230 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001231}
1232
1233/// ParseDoStatement
1234/// do-statement: [C99 6.8.5.2]
1235/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +00001236/// Note: this lets the caller parse the end ';'.
Richard Smithc202b282012-04-14 00:33:13 +00001237StmtResult Parser::ParseDoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001238 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001239 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001240
Chris Lattner2dd1b722007-08-26 23:08:06 +00001241 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1242 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001243 unsigned ScopeFlags;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001244 if (getLangOpts().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001245 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001246 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001247 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001248
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001249 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001250
Chris Lattner8fb26252007-08-22 05:28:50 +00001251 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001252 // there is no compound stmt. C90 does not have this clause. We only do this
1253 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +00001254 //
1255 // C++ 6.5p2:
1256 // The substatement in an iteration-statement implicitly defines a local scope
1257 // which is entered and exited each time through the loop.
1258 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001259 ParseScope InnerScope(this, Scope::DeclScope,
David Blaikiebbafb8a2012-03-11 07:00:24 +00001260 (getLangOpts().C99 || getLangOpts().CPlusPlus) &&
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001261 Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001262
Chris Lattner9075bd72006-08-10 04:59:57 +00001263 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001264 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +00001265
Chris Lattner8fb26252007-08-22 05:28:50 +00001266 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001267 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001268
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001269 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001270 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +00001271 Diag(Tok, diag::err_expected_while);
Chris Lattner03c40412008-11-23 23:17:07 +00001272 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner0046de12008-11-13 18:52:53 +00001273 SkipUntil(tok::semi, false, true);
1274 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001275 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001276 }
Chris Lattneraf635312006-10-16 06:06:51 +00001277 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001278
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001279 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001280 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner0046de12008-11-13 18:52:53 +00001281 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001282 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001283 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001284
Chris Lattner10da53c2008-12-12 06:35:28 +00001285 // Parse the parenthesized condition.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001286 BalancedDelimiterTracker T(*this, tok::l_paren);
1287 T.consumeOpen();
Chad Rosier67055f52012-07-10 21:35:27 +00001288
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001289 // FIXME: Do not just parse the attribute contents and throw them away
1290 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001291 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001292 ProhibitAttributes(attrs);
1293
John McCalldadc5752010-08-24 06:29:42 +00001294 ExprResult Cond = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001295 T.consumeClose();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001296 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001297
Sebastian Redlb62406f2008-12-11 19:48:14 +00001298 if (Cond.isInvalid() || Body.isInvalid())
1299 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001300
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001301 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(),
1302 Cond.get(), T.getCloseLocation());
Chris Lattner9075bd72006-08-10 04:59:57 +00001303}
1304
1305/// ParseForStatement
1306/// for-statement: [C99 6.8.5.3]
1307/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1308/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001309/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1310/// [C++] statement
Richard Smith02e85f32011-04-14 22:09:26 +00001311/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001312/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1313/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001314///
1315/// [C++] for-init-statement:
1316/// [C++] expression-statement
1317/// [C++] simple-declaration
1318///
Richard Smith02e85f32011-04-14 22:09:26 +00001319/// [C++0x] for-range-declaration:
1320/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1321/// [C++0x] for-range-initializer:
1322/// [C++0x] expression
1323/// [C++0x] braced-init-list [TODO]
Richard Smithc202b282012-04-14 00:33:13 +00001324StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001325 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001326 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001327
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001328 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001329 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +00001330 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001331 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001332 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001333
Chad Rosier67055f52012-07-10 21:35:27 +00001334 bool C99orCXXorObjC = getLangOpts().C99 || getLangOpts().CPlusPlus ||
1335 getLangOpts().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001336
Chris Lattner2dd1b722007-08-26 23:08:06 +00001337 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1338 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001339 //
1340 // C++ 6.4p3:
1341 // A name introduced by a declaration in a condition is in scope from its
1342 // point of declaration until the end of the substatements controlled by the
1343 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001344 // C++ 3.3.2p4:
1345 // Names declared in the for-init-statement, and in the condition of if,
1346 // while, for, and switch statements are local to the if, while, for, or
1347 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001348 // C++ 6.5.3p1:
1349 // Names declared in the for-init-statement are in the same declarative-region
1350 // as those declared in the condition.
1351 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001352 unsigned ScopeFlags;
Chris Lattner934074c2009-04-22 00:54:41 +00001353 if (C99orCXXorObjC)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001354 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1355 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001356 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001357 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1358
1359 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +00001360
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001361 BalancedDelimiterTracker T(*this, tok::l_paren);
1362 T.consumeOpen();
1363
John McCalldadc5752010-08-24 06:29:42 +00001364 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001365
Richard Smith02e85f32011-04-14 22:09:26 +00001366 bool ForEach = false, ForRange = false;
John McCalldadc5752010-08-24 06:29:42 +00001367 StmtResult FirstPart;
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001368 bool SecondPartIsInvalid = false;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001369 FullExprArg SecondPart(Actions);
John McCalldadc5752010-08-24 06:29:42 +00001370 ExprResult Collection;
Richard Smith02e85f32011-04-14 22:09:26 +00001371 ForRangeInit ForRangeInit;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001372 FullExprArg ThirdPart(Actions);
John McCall48871652010-08-21 09:40:31 +00001373 Decl *SecondVar = 0;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001374
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001375 if (Tok.is(tok::code_completion)) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001376 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001377 C99orCXXorObjC? Sema::PCC_ForInit
1378 : Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001379 cutOffParsing();
1380 return StmtError();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001381 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001382
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001383 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001384 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001385
Chris Lattner9075bd72006-08-10 04:59:57 +00001386 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001387 if (Tok.is(tok::semi)) { // for (;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001388 ProhibitAttributes(attrs);
Chris Lattner53361ac2006-08-10 05:19:57 +00001389 // no first part, eat the ';'.
1390 ConsumeToken();
Eli Friedman0ffc31c2011-12-20 01:50:37 +00001391 } else if (isForInitDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001392 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001393 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001394 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001395
Richard Smith02e85f32011-04-14 22:09:26 +00001396 // In C++0x, "for (T NS:a" might not be a typo for ::
David Blaikiebbafb8a2012-03-11 07:00:24 +00001397 bool MightBeForRangeStmt = getLangOpts().CPlusPlus;
Richard Smith02e85f32011-04-14 22:09:26 +00001398 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1399
Chris Lattner49836b42009-04-02 04:16:50 +00001400 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Benjamin Kramerf0623432012-08-23 22:51:59 +00001401 StmtVector Stmts;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001402 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
Richard Smith02e85f32011-04-14 22:09:26 +00001403 DeclEnd, attrs, false,
1404 MightBeForRangeStmt ?
1405 &ForRangeInit : 0);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001406 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001407
Richard Smith02e85f32011-04-14 22:09:26 +00001408 if (ForRangeInit.ParsedForRangeDecl()) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001409 Diag(ForRangeInit.ColonLoc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00001410 diag::warn_cxx98_compat_for_range : diag::ext_for_range);
Richard Smith58c74332011-09-04 19:54:14 +00001411
Richard Smith02e85f32011-04-14 22:09:26 +00001412 ForRange = true;
1413 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattner32dc41c2009-03-29 17:27:48 +00001414 ConsumeToken();
1415 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001416 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001417 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001418 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001419
Douglas Gregor68762e72010-08-23 21:17:50 +00001420 if (Tok.is(tok::code_completion)) {
1421 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001422 cutOffParsing();
1423 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001424 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001425 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001426 } else {
1427 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001428 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001429 } else {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001430 ProhibitAttributes(attrs);
Chris Lattner89c50c62006-08-11 06:41:18 +00001431 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +00001432
John McCall34376a62010-12-04 03:47:34 +00001433 ForEach = isTokIdentifier_in();
1434
Chris Lattnercd68f642007-06-27 01:06:29 +00001435 // Turn the expression into a stmt.
John McCall34376a62010-12-04 03:47:34 +00001436 if (!Value.isInvalid()) {
1437 if (ForEach)
1438 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1439 else
Richard Smith945f8d32013-01-14 22:39:08 +00001440 FirstPart = Actions.ActOnExprStmt(Value);
John McCall34376a62010-12-04 03:47:34 +00001441 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001442
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001443 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001444 ConsumeToken();
John McCall34376a62010-12-04 03:47:34 +00001445 } else if (ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001446 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001447
Douglas Gregor68762e72010-08-23 21:17:50 +00001448 if (Tok.is(tok::code_completion)) {
1449 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001450 cutOffParsing();
1451 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001452 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001453 Collection = ParseExpression();
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001454 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::colon) && FirstPart.get()) {
Richard Smith4f848f12011-12-20 22:56:20 +00001455 // User tried to write the reasonable, but ill-formed, for-range-statement
1456 // for (expr : expr) { ... }
1457 Diag(Tok, diag::err_for_range_expected_decl)
1458 << FirstPart.get()->getSourceRange();
1459 SkipUntil(tok::r_paren, false, true);
1460 SecondPartIsInvalid = true;
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001461 } else {
Douglas Gregor230a7e62011-02-17 03:38:46 +00001462 if (!Value.isInvalid()) {
1463 Diag(Tok, diag::err_expected_semi_for);
1464 } else {
1465 // Skip until semicolon or rparen, don't consume it.
1466 SkipUntil(tok::r_paren, true, true);
1467 if (Tok.is(tok::semi))
1468 ConsumeToken();
1469 }
Chris Lattner53361ac2006-08-10 05:19:57 +00001470 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001471 }
Richard Smith02e85f32011-04-14 22:09:26 +00001472 if (!ForEach && !ForRange) {
John McCallb268a282010-08-23 23:25:46 +00001473 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001474 // Parse the second part of the for specifier.
1475 if (Tok.is(tok::semi)) { // for (...;;
1476 // no second part.
Douglas Gregor230a7e62011-02-17 03:38:46 +00001477 } else if (Tok.is(tok::r_paren)) {
1478 // missing both semicolons.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001479 } else {
John McCalldadc5752010-08-24 06:29:42 +00001480 ExprResult Second;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001481 if (getLangOpts().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +00001482 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1483 else {
1484 Second = ParseExpression();
1485 if (!Second.isInvalid())
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001486 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCallb268a282010-08-23 23:25:46 +00001487 Second.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001488 }
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001489 SecondPartIsInvalid = Second.isInvalid();
David Blaikiea5696df2012-05-16 04:20:04 +00001490 SecondPart = Actions.MakeFullExpr(Second.get(), ForLoc);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001491 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001492
Douglas Gregor230a7e62011-02-17 03:38:46 +00001493 if (Tok.isNot(tok::semi)) {
1494 if (!SecondPartIsInvalid || SecondVar)
1495 Diag(Tok, diag::err_expected_semi_for);
1496 else
1497 // Skip until semicolon or rparen, don't consume it.
1498 SkipUntil(tok::r_paren, true, true);
1499 }
1500
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001501 if (Tok.is(tok::semi)) {
1502 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001503 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001504
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001505 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001506 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001507 ExprResult Third = ParseExpression();
Richard Smith945f8d32013-01-14 22:39:08 +00001508 // FIXME: The C++11 standard doesn't actually say that this is a
1509 // discarded-value expression, but it clearly should be.
1510 ThirdPart = Actions.MakeFullDiscardedValueExpr(Third.take());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001511 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001512 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001513 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001514 T.consumeClose();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001515
Richard Smith02e85f32011-04-14 22:09:26 +00001516 // We need to perform most of the semantic analysis for a C++0x for-range
1517 // statememt before parsing the body, in order to be able to deduce the type
1518 // of an auto-typed loop variable.
1519 StmtResult ForRangeStmt;
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001520 StmtResult ForEachStmt;
Chad Rosier67055f52012-07-10 21:35:27 +00001521
John McCall53848232011-07-27 01:07:15 +00001522 if (ForRange) {
Sam Panzer2c4ca0f2012-08-16 21:47:25 +00001523 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, FirstPart.take(),
Richard Smith02e85f32011-04-14 22:09:26 +00001524 ForRangeInit.ColonLoc,
1525 ForRangeInit.RangeExpr.get(),
Richard Smitha05b3b52012-09-20 21:52:32 +00001526 T.getCloseLocation(),
1527 Sema::BFRK_Build);
Richard Smith02e85f32011-04-14 22:09:26 +00001528
John McCall53848232011-07-27 01:07:15 +00001529
1530 // Similarly, we need to do the semantic analysis for a for-range
1531 // statement immediately in order to close over temporaries correctly.
1532 } else if (ForEach) {
Sam Panzer2c4ca0f2012-08-16 21:47:25 +00001533 ForEachStmt = Actions.ActOnObjCForCollectionStmt(ForLoc,
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001534 FirstPart.take(),
Chad Rosier67055f52012-07-10 21:35:27 +00001535 Collection.take(),
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001536 T.getCloseLocation());
John McCall53848232011-07-27 01:07:15 +00001537 }
1538
Chris Lattner8fb26252007-08-22 05:28:50 +00001539 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001540 // there is no compound stmt. C90 does not have this clause. We only do this
1541 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001542 //
1543 // C++ 6.5p2:
1544 // The substatement in an iteration-statement implicitly defines a local scope
1545 // which is entered and exited each time through the loop.
1546 //
1547 // See comments in ParseIfStatement for why we create a scope for
1548 // for-init-statement/condition and a new scope for substatement in C++.
1549 //
Mike Stump11289f42009-09-09 15:08:12 +00001550 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner934074c2009-04-22 00:54:41 +00001551 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001552
Chris Lattner9075bd72006-08-10 04:59:57 +00001553 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001554 StmtResult Body(ParseStatement(TrailingElseLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001555
Chris Lattner8fb26252007-08-22 05:28:50 +00001556 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001557 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001558
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001559 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001560 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001561
1562 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001563 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001564
Richard Smith02e85f32011-04-14 22:09:26 +00001565 if (ForEach)
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001566 return Actions.FinishObjCForCollectionStmt(ForEachStmt.take(),
1567 Body.take());
Mike Stump11289f42009-09-09 15:08:12 +00001568
Richard Smith02e85f32011-04-14 22:09:26 +00001569 if (ForRange)
1570 return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take());
1571
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001572 return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.take(),
1573 SecondPart, SecondVar, ThirdPart,
1574 T.getCloseLocation(), Body.take());
Chris Lattner9075bd72006-08-10 04:59:57 +00001575}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001576
Chris Lattner503fadc2006-08-10 05:45:44 +00001577/// ParseGotoStatement
1578/// jump-statement:
1579/// 'goto' identifier ';'
1580/// [GNU] 'goto' '*' expression ';'
1581///
1582/// Note: this lets the caller parse the end ';'.
1583///
Richard Smithc202b282012-04-14 00:33:13 +00001584StmtResult Parser::ParseGotoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001585 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001586 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001587
John McCalldadc5752010-08-24 06:29:42 +00001588 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001589 if (Tok.is(tok::identifier)) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +00001590 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1591 Tok.getLocation());
1592 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Chris Lattner503fadc2006-08-10 05:45:44 +00001593 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001594 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001595 // GNU indirect goto extension.
1596 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001597 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001598 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001599 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001600 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001601 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001602 }
John McCallb268a282010-08-23 23:25:46 +00001603 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001604 } else {
1605 Diag(Tok, diag::err_expected_ident);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001606 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001607 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001608
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001609 return Res;
Chris Lattner503fadc2006-08-10 05:45:44 +00001610}
1611
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001612/// ParseContinueStatement
1613/// jump-statement:
1614/// 'continue' ';'
1615///
1616/// Note: this lets the caller parse the end ';'.
1617///
Richard Smithc202b282012-04-14 00:33:13 +00001618StmtResult Parser::ParseContinueStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001619 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001620 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001621}
1622
1623/// ParseBreakStatement
1624/// jump-statement:
1625/// 'break' ';'
1626///
1627/// Note: this lets the caller parse the end ';'.
1628///
Richard Smithc202b282012-04-14 00:33:13 +00001629StmtResult Parser::ParseBreakStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001630 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001631 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001632}
1633
Chris Lattner503fadc2006-08-10 05:45:44 +00001634/// ParseReturnStatement
1635/// jump-statement:
1636/// 'return' expression[opt] ';'
Richard Smithc202b282012-04-14 00:33:13 +00001637StmtResult Parser::ParseReturnStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001638 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001639 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001640
John McCalldadc5752010-08-24 06:29:42 +00001641 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001642 if (Tok.isNot(tok::semi)) {
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001643 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001644 Actions.CodeCompleteReturn(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001645 cutOffParsing();
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001646 return StmtError();
1647 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001648
David Blaikiebbafb8a2012-03-11 07:00:24 +00001649 if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus) {
Douglas Gregore9e27d92011-03-11 23:10:44 +00001650 R = ParseInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00001651 if (R.isUsable())
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001652 Diag(R.get()->getLocStart(), getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00001653 diag::warn_cxx98_compat_generalized_initializer_lists :
1654 diag::ext_generalized_initializer_lists)
Douglas Gregore9e27d92011-03-11 23:10:44 +00001655 << R.get()->getSourceRange();
1656 } else
1657 R = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001658 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001659 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001660 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001661 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001662 }
John McCallb268a282010-08-23 23:25:46 +00001663 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Chris Lattner503fadc2006-08-10 05:45:44 +00001664}
Chris Lattner0116c472006-08-15 06:03:28 +00001665
Eli Friedmana4b02c32011-09-30 01:13:51 +00001666/// ParseMicrosoftAsmStatement. When -fms-extensions/-fasm-blocks is enabled,
1667/// this routine is called to collect the tokens for an MS asm statement.
Chad Rosier32503022012-06-11 20:47:18 +00001668///
1669/// [MS] ms-asm-statement:
1670/// ms-asm-block
1671/// ms-asm-block ms-asm-statement
1672///
1673/// [MS] ms-asm-block:
1674/// '__asm' ms-asm-line '\n'
1675/// '__asm' '{' ms-asm-instruction-block[opt] '}' ';'[opt]
1676///
1677/// [MS] ms-asm-instruction-block
1678/// ms-asm-line
1679/// ms-asm-line '\n' ms-asm-instruction-block
1680///
Eli Friedmana4b02c32011-09-30 01:13:51 +00001681StmtResult Parser::ParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
1682 SourceManager &SrcMgr = PP.getSourceManager();
1683 SourceLocation EndLoc = AsmLoc;
Chad Rosier32503022012-06-11 20:47:18 +00001684 SmallVector<Token, 4> AsmToks;
Chad Rosierc97a6bb2012-08-14 19:22:06 +00001685
1686 bool InBraces = false;
1687 unsigned short savedBraceCount = 0;
1688 bool InAsmComment = false;
1689 FileID FID;
1690 unsigned LineNo = 0;
1691 unsigned NumTokensRead = 0;
1692 SourceLocation LBraceLoc;
1693
1694 if (Tok.is(tok::l_brace)) {
1695 // Braced inline asm: consume the opening brace.
1696 InBraces = true;
1697 savedBraceCount = BraceCount;
1698 EndLoc = LBraceLoc = ConsumeBrace();
1699 ++NumTokensRead;
1700 } else {
1701 // Single-line inline asm; compute which line it is on.
1702 std::pair<FileID, unsigned> ExpAsmLoc =
1703 SrcMgr.getDecomposedExpansionLoc(EndLoc);
1704 FID = ExpAsmLoc.first;
1705 LineNo = SrcMgr.getLineNumber(FID, ExpAsmLoc.second);
1706 }
1707
1708 SourceLocation TokLoc = Tok.getLocation();
Eli Friedmana4b02c32011-09-30 01:13:51 +00001709 do {
Chad Rosierc97a6bb2012-08-14 19:22:06 +00001710 // If we hit EOF, we're done, period.
1711 if (Tok.is(tok::eof))
Eli Friedmana4b02c32011-09-30 01:13:51 +00001712 break;
Chad Rosierc97a6bb2012-08-14 19:22:06 +00001713
Chad Rosierc97a6bb2012-08-14 19:22:06 +00001714 if (!InAsmComment && Tok.is(tok::semi)) {
1715 // A semicolon in an asm is the start of a comment.
1716 InAsmComment = true;
1717 if (InBraces) {
1718 // Compute which line the comment is on.
1719 std::pair<FileID, unsigned> ExpSemiLoc =
1720 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1721 FID = ExpSemiLoc.first;
1722 LineNo = SrcMgr.getLineNumber(FID, ExpSemiLoc.second);
1723 }
1724 } else if (!InBraces || InAsmComment) {
1725 // If end-of-line is significant, check whether this token is on a
1726 // new line.
1727 std::pair<FileID, unsigned> ExpLoc =
1728 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1729 if (ExpLoc.first != FID ||
1730 SrcMgr.getLineNumber(ExpLoc.first, ExpLoc.second) != LineNo) {
1731 // If this is a single-line __asm, we're done.
1732 if (!InBraces)
1733 break;
1734 // We're no longer in a comment.
1735 InAsmComment = false;
1736 } else if (!InAsmComment && Tok.is(tok::r_brace)) {
1737 // Single-line asm always ends when a closing brace is seen.
1738 // FIXME: This is compatible with Apple gcc's -fasm-blocks; what
1739 // does MSVC do here?
1740 break;
1741 }
1742 }
1743 if (!InAsmComment && InBraces && Tok.is(tok::r_brace) &&
1744 BraceCount == (savedBraceCount + 1)) {
1745 // Consume the closing brace, and finish
1746 EndLoc = ConsumeBrace();
1747 break;
1748 }
1749
1750 // Consume the next token; make sure we don't modify the brace count etc.
1751 // if we are in a comment.
1752 EndLoc = TokLoc;
1753 if (InAsmComment)
1754 PP.Lex(Tok);
1755 else {
1756 AsmToks.push_back(Tok);
1757 ConsumeAnyToken();
1758 }
1759 TokLoc = Tok.getLocation();
1760 ++NumTokensRead;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001761 } while (1);
Chad Rosier32503022012-06-11 20:47:18 +00001762
Chad Rosierc97a6bb2012-08-14 19:22:06 +00001763 if (InBraces && BraceCount != savedBraceCount) {
1764 // __asm without closing brace (this can happen at EOF).
1765 Diag(Tok, diag::err_expected_rbrace);
1766 Diag(LBraceLoc, diag::note_matching) << "{";
1767 return StmtError();
1768 } else if (NumTokensRead == 0) {
1769 // Empty __asm.
1770 Diag(Tok, diag::err_expected_lbrace);
1771 return StmtError();
1772 }
1773
Chad Rosierc6c71332012-08-06 20:03:45 +00001774 // FIXME: We should be passing source locations for better diagnostics.
Chad Rosierb6f46c12012-08-15 16:53:30 +00001775 return Actions.ActOnMSAsmStmt(AsmLoc, LBraceLoc,
1776 llvm::makeArrayRef(AsmToks), EndLoc);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001777}
1778
Chris Lattner0116c472006-08-15 06:03:28 +00001779/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001780/// asm-statement:
1781/// gnu-asm-statement
1782/// ms-asm-statement
1783///
1784/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001785/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1786///
1787/// [GNU] asm-argument:
1788/// asm-string-literal
1789/// asm-string-literal ':' asm-operands[opt]
1790/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1791/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1792/// ':' asm-clobbers
1793///
1794/// [GNU] asm-clobbers:
1795/// asm-string-literal
1796/// asm-clobbers ',' asm-string-literal
1797///
John McCalldadc5752010-08-24 06:29:42 +00001798StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001799 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001800 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001801
Chad Rosierc8e56e82012-12-05 21:08:21 +00001802 if (getLangOpts().AsmBlocks && Tok.isNot(tok::l_paren) &&
Chad Rosier67055f52012-07-10 21:35:27 +00001803 !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001804 msAsm = true;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001805 return ParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001806 }
John McCall084e83d2011-03-24 11:26:52 +00001807 DeclSpec DS(AttrFactory);
Chris Lattner0116c472006-08-15 06:03:28 +00001808 SourceLocation Loc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001809 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001810
Chris Lattner0116c472006-08-15 06:03:28 +00001811 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001812 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001813 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001814 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001815 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Richard Smith8e1ac332013-03-28 01:55:44 +00001816 // FIXME: Once GCC supports _Atomic, check whether it permits it here.
1817 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
1818 Diag(Loc, diag::w_asm_qualifier_ignored) << "_Atomic";
Sebastian Redlb62406f2008-12-11 19:48:14 +00001819
Chris Lattner0116c472006-08-15 06:03:28 +00001820 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001821 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001822 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001823 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001824 SkipUntil(tok::r_paren);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001825 return StmtError();
Chris Lattner0116c472006-08-15 06:03:28 +00001826 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001827 BalancedDelimiterTracker T(*this, tok::l_paren);
1828 T.consumeOpen();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001829
John McCalldadc5752010-08-24 06:29:42 +00001830 ExprResult AsmString(ParseAsmStringLiteral());
Ted Kremenekeb0a6c02011-12-02 01:30:14 +00001831 if (AsmString.isInvalid()) {
Richard Smithd67aea22012-03-06 03:21:47 +00001832 // Consume up to and including the closing paren.
1833 T.skipToEnd();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001834 return StmtError();
Ted Kremenekeb0a6c02011-12-02 01:30:14 +00001835 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001836
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001837 SmallVector<IdentifierInfo *, 4> Names;
Benjamin Kramerf0623432012-08-23 22:51:59 +00001838 ExprVector Constraints;
1839 ExprVector Exprs;
1840 ExprVector Clobbers;
Chris Lattner0116c472006-08-15 06:03:28 +00001841
Anders Carlsson19fe1162008-02-05 23:03:50 +00001842 if (Tok.is(tok::r_paren)) {
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001843 // We have a simple asm expression like 'asm("foo")'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001844 T.consumeClose();
Chad Rosierde70e0e2012-08-25 00:11:56 +00001845 return Actions.ActOnGCCAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1846 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1847 Constraints, Exprs, AsmString.take(),
1848 Clobbers, T.getCloseLocation());
Chris Lattner0116c472006-08-15 06:03:28 +00001849 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001850
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001851 // Parse Outputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001852 bool AteExtraColon = false;
1853 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1854 // In C++ mode, parse "::" like ": :".
1855 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001856 ConsumeToken();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001857
Chris Lattner15768502009-12-20 23:08:04 +00001858 if (!AteExtraColon &&
1859 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001860 return StmtError();
1861 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001862
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001863 unsigned NumOutputs = Names.size();
1864
1865 // Parse Inputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001866 if (AteExtraColon ||
1867 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1868 // In C++ mode, parse "::" like ": :".
1869 if (AteExtraColon)
1870 AteExtraColon = false;
1871 else {
1872 AteExtraColon = Tok.is(tok::coloncolon);
1873 ConsumeToken();
1874 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001875
Chris Lattner15768502009-12-20 23:08:04 +00001876 if (!AteExtraColon &&
1877 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001878 return StmtError();
1879 }
1880
1881 assert(Names.size() == Constraints.size() &&
1882 Constraints.size() == Exprs.size() &&
1883 "Input operand size mismatch!");
1884
1885 unsigned NumInputs = Names.size() - NumOutputs;
1886
1887 // Parse the clobbers, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001888 if (AteExtraColon || Tok.is(tok::colon)) {
1889 if (!AteExtraColon)
1890 ConsumeToken();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001891
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001892 // Parse the asm-string list for clobbers if present.
1893 if (Tok.isNot(tok::r_paren)) {
1894 while (1) {
John McCalldadc5752010-08-24 06:29:42 +00001895 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001896
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001897 if (Clobber.isInvalid())
1898 break;
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001899
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001900 Clobbers.push_back(Clobber.release());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001901
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001902 if (Tok.isNot(tok::comma)) break;
1903 ConsumeToken();
1904 }
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001905 }
1906 }
1907
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001908 T.consumeClose();
Chad Rosierde70e0e2012-08-25 00:11:56 +00001909 return Actions.ActOnGCCAsmStmt(AsmLoc, false, isVolatile, NumOutputs,
1910 NumInputs, Names.data(), Constraints, Exprs,
1911 AsmString.take(), Clobbers,
1912 T.getCloseLocation());
Chris Lattner0116c472006-08-15 06:03:28 +00001913}
1914
1915/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001916/// asm-statement, assuming the leading ':' token was eaten.
Chris Lattner0116c472006-08-15 06:03:28 +00001917///
1918/// [GNU] asm-operands:
1919/// asm-operand
1920/// asm-operands ',' asm-operand
1921///
1922/// [GNU] asm-operand:
1923/// asm-string-literal '(' expression ')'
1924/// '[' identifier ']' asm-string-literal '(' expression ')'
1925///
Daniel Dunbar70e7ead2009-10-18 20:26:27 +00001926//
1927// FIXME: Avoid unnecessary std::string trashing.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001928bool Parser::ParseAsmOperandsOpt(SmallVectorImpl<IdentifierInfo *> &Names,
Richard Trieu2bd04012011-09-09 02:00:50 +00001929 SmallVectorImpl<Expr *> &Constraints,
1930 SmallVectorImpl<Expr *> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001931 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001932 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001933 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001934
1935 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001936 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001937 if (Tok.is(tok::l_square)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001938 BalancedDelimiterTracker T(*this, tok::l_square);
1939 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00001940
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001941 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001942 Diag(Tok, diag::err_expected_ident);
1943 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001944 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001945 }
Mike Stump11289f42009-09-09 15:08:12 +00001946
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001947 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001948 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001949
Anders Carlsson9a020f92010-01-30 22:25:16 +00001950 Names.push_back(II);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001951 T.consumeClose();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001952 } else
Anders Carlsson9a020f92010-01-30 22:25:16 +00001953 Names.push_back(0);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001954
John McCalldadc5752010-08-24 06:29:42 +00001955 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001956 if (Constraint.isInvalid()) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001957 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001958 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001959 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001960 Constraints.push_back(Constraint.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001961
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001962 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001963 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00001964 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001965 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001966 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001967
Chris Lattner0116c472006-08-15 06:03:28 +00001968 // Read the parenthesized expression.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001969 BalancedDelimiterTracker T(*this, tok::l_paren);
1970 T.consumeOpen();
John McCalldadc5752010-08-24 06:29:42 +00001971 ExprResult Res(ParseExpression());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001972 T.consumeClose();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001973 if (Res.isInvalid()) {
Chris Lattner0116c472006-08-15 06:03:28 +00001974 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001975 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001976 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001977 Exprs.push_back(Res.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001978 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001979 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001980 ConsumeToken();
1981 }
1982}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001983
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001984Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001985 assert(Tok.is(tok::l_brace));
1986 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001987
Argyrios Kyrtzidis44319182013-02-22 04:11:06 +00001988 if (SkipFunctionBodies && (!Decl || Actions.canSkipFunctionBody(Decl)) &&
Richard Smith1ab34b32012-11-19 21:13:18 +00001989 trySkippingFunctionBody()) {
Erik Verbruggen6e922512012-04-12 10:11:59 +00001990 BodyScope.Exit();
Argyrios Kyrtzidis1eb71a12012-12-06 18:59:10 +00001991 return Actions.ActOnSkippedFunctionBody(Decl);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001992 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001993
John McCallfaf5fb42010-08-26 23:41:50 +00001994 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1995 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001996
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001997 // Do not enter a scope for the brace, as the arguments are in the same scope
1998 // (the function body) as the body itself. Instead, just read the statement
1999 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00002000 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00002001
Fariborz Jahanian8e632942007-11-08 19:01:26 +00002002 // If the function body could not be parsed, make a bogus compoundstmt.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002003 if (FnBody.isInvalid()) {
2004 Sema::CompoundScopeRAII CompoundScope(Actions);
Mike Stump11289f42009-09-09 15:08:12 +00002005 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00002006 MultiStmtArg(), false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002007 }
Sebastian Redl042ad952008-12-11 19:30:53 +00002008
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002009 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00002010 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00002011}
Sebastian Redlb219c902008-12-21 16:41:36 +00002012
Sebastian Redla7b98a72009-04-26 20:35:05 +00002013/// ParseFunctionTryBlock - Parse a C++ function-try-block.
2014///
2015/// function-try-block:
2016/// 'try' ctor-initializer[opt] compound-statement handler-seq
2017///
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002018Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00002019 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2020 SourceLocation TryLoc = ConsumeToken();
2021
John McCallfaf5fb42010-08-26 23:41:50 +00002022 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
2023 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00002024
2025 // Constructor initializer list?
2026 if (Tok.is(tok::colon))
2027 ParseConstructorInitializer(Decl);
Douglas Gregor5ca153f2011-09-07 20:36:12 +00002028 else
2029 Actions.ActOnDefaultCtorInitializers(Decl);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00002030
Richard Smith1ab34b32012-11-19 21:13:18 +00002031 if (SkipFunctionBodies && Actions.canSkipFunctionBody(Decl) &&
2032 trySkippingFunctionBody()) {
Erik Verbruggen6e922512012-04-12 10:11:59 +00002033 BodyScope.Exit();
Argyrios Kyrtzidis1eb71a12012-12-06 18:59:10 +00002034 return Actions.ActOnSkippedFunctionBody(Decl);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002035 }
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002036
Sebastian Redld98ecd62009-04-26 21:08:36 +00002037 SourceLocation LBraceLoc = Tok.getLocation();
David Blaikie1c9c9042012-11-10 01:04:23 +00002038 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc, /*FnTry*/true));
Sebastian Redla7b98a72009-04-26 20:35:05 +00002039 // If we failed to parse the try-catch, we just give the function an empty
2040 // compound statement as the body.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002041 if (FnBody.isInvalid()) {
2042 Sema::CompoundScopeRAII CompoundScope(Actions);
Sebastian Redld98ecd62009-04-26 21:08:36 +00002043 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00002044 MultiStmtArg(), false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002045 }
Sebastian Redla7b98a72009-04-26 20:35:05 +00002046
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002047 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00002048 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redla7b98a72009-04-26 20:35:05 +00002049}
2050
Erik Verbruggen6e922512012-04-12 10:11:59 +00002051bool Parser::trySkippingFunctionBody() {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002052 assert(Tok.is(tok::l_brace));
Erik Verbruggen6e922512012-04-12 10:11:59 +00002053 assert(SkipFunctionBodies &&
2054 "Should only be called when SkipFunctionBodies is enabled");
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002055
Argyrios Kyrtzidis289e4a32012-10-31 17:29:28 +00002056 if (!PP.isCodeCompletionEnabled()) {
2057 ConsumeBrace();
2058 SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false);
2059 return true;
2060 }
2061
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002062 // We're in code-completion mode. Skip parsing for all function bodies unless
2063 // the body contains the code-completion point.
2064 TentativeParsingAction PA(*this);
2065 ConsumeBrace();
2066 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
Argyrios Kyrtzidis289e4a32012-10-31 17:29:28 +00002067 /*StopAtCodeCompletion=*/true)) {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002068 PA.Commit();
2069 return true;
2070 }
2071
2072 PA.Revert();
2073 return false;
2074}
2075
Sebastian Redlb219c902008-12-21 16:41:36 +00002076/// ParseCXXTryBlock - Parse a C++ try-block.
2077///
2078/// try-block:
2079/// 'try' compound-statement handler-seq
2080///
Richard Smithc202b282012-04-14 00:33:13 +00002081StmtResult Parser::ParseCXXTryBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00002082 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2083
2084 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00002085 return ParseCXXTryBlockCommon(TryLoc);
2086}
2087
2088/// ParseCXXTryBlockCommon - Parse the common part of try-block and
2089/// function-try-block.
2090///
2091/// try-block:
2092/// 'try' compound-statement handler-seq
2093///
2094/// function-try-block:
2095/// 'try' ctor-initializer[opt] compound-statement handler-seq
2096///
2097/// handler-seq:
2098/// handler handler-seq[opt]
2099///
John Wiegley1c0675e2011-04-28 01:08:34 +00002100/// [Borland] try-block:
2101/// 'try' compound-statement seh-except-block
2102/// 'try' compound-statment seh-finally-block
2103///
David Blaikie1c9c9042012-11-10 01:04:23 +00002104StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc, bool FnTry) {
Sebastian Redlb219c902008-12-21 16:41:36 +00002105 if (Tok.isNot(tok::l_brace))
2106 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002107 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
Richard Smithc202b282012-04-14 00:33:13 +00002108
2109 StmtResult TryBlock(ParseCompoundStatement(/*isStmtExpr=*/false,
David Blaikie3403feb2012-11-13 18:51:45 +00002110 Scope::DeclScope | Scope::TryScope |
2111 (FnTry ? Scope::FnTryCatchScope : 0)));
Sebastian Redlb219c902008-12-21 16:41:36 +00002112 if (TryBlock.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002113 return TryBlock;
Sebastian Redlb219c902008-12-21 16:41:36 +00002114
John Wiegley1c0675e2011-04-28 01:08:34 +00002115 // Borland allows SEH-handlers with 'try'
Chad Rosier67055f52012-07-10 21:35:27 +00002116
Richard Smithc202b282012-04-14 00:33:13 +00002117 if ((Tok.is(tok::identifier) &&
2118 Tok.getIdentifierInfo() == getSEHExceptKeyword()) ||
2119 Tok.is(tok::kw___finally)) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002120 // TODO: Factor into common return ParseSEHHandlerCommon(...)
2121 StmtResult Handler;
Douglas Gregor60060d62011-10-21 03:57:52 +00002122 if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002123 SourceLocation Loc = ConsumeToken();
2124 Handler = ParseSEHExceptBlock(Loc);
2125 }
2126 else {
2127 SourceLocation Loc = ConsumeToken();
2128 Handler = ParseSEHFinallyBlock(Loc);
2129 }
2130 if(Handler.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002131 return Handler;
John McCall53fa7142010-12-24 02:08:15 +00002132
John Wiegley1c0675e2011-04-28 01:08:34 +00002133 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
2134 TryLoc,
2135 TryBlock.take(),
2136 Handler.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00002137 }
John Wiegley1c0675e2011-04-28 01:08:34 +00002138 else {
Benjamin Kramerf0623432012-08-23 22:51:59 +00002139 StmtVector Handlers;
Richard Smithc202b282012-04-14 00:33:13 +00002140 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002141 MaybeParseCXX11Attributes(attrs);
John Wiegley1c0675e2011-04-28 01:08:34 +00002142 ProhibitAttributes(attrs);
Sebastian Redlb219c902008-12-21 16:41:36 +00002143
John Wiegley1c0675e2011-04-28 01:08:34 +00002144 if (Tok.isNot(tok::kw_catch))
2145 return StmtError(Diag(Tok, diag::err_expected_catch));
2146 while (Tok.is(tok::kw_catch)) {
David Blaikie1c9c9042012-11-10 01:04:23 +00002147 StmtResult Handler(ParseCXXCatchBlock(FnTry));
John Wiegley1c0675e2011-04-28 01:08:34 +00002148 if (!Handler.isInvalid())
2149 Handlers.push_back(Handler.release());
2150 }
2151 // Don't bother creating the full statement if we don't have any usable
2152 // handlers.
2153 if (Handlers.empty())
2154 return StmtError();
2155
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002156 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(),Handlers);
John Wiegley1c0675e2011-04-28 01:08:34 +00002157 }
Sebastian Redlb219c902008-12-21 16:41:36 +00002158}
2159
2160/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
2161///
Richard Smith1dba27c2013-01-29 09:02:09 +00002162/// handler:
2163/// 'catch' '(' exception-declaration ')' compound-statement
Sebastian Redlb219c902008-12-21 16:41:36 +00002164///
Richard Smith1dba27c2013-01-29 09:02:09 +00002165/// exception-declaration:
2166/// attribute-specifier-seq[opt] type-specifier-seq declarator
2167/// attribute-specifier-seq[opt] type-specifier-seq abstract-declarator[opt]
2168/// '...'
Sebastian Redlb219c902008-12-21 16:41:36 +00002169///
David Blaikie1c9c9042012-11-10 01:04:23 +00002170StmtResult Parser::ParseCXXCatchBlock(bool FnCatch) {
Sebastian Redlb219c902008-12-21 16:41:36 +00002171 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
2172
2173 SourceLocation CatchLoc = ConsumeToken();
2174
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002175 BalancedDelimiterTracker T(*this, tok::l_paren);
2176 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redlb219c902008-12-21 16:41:36 +00002177 return StmtError();
2178
2179 // C++ 3.3.2p3:
2180 // The name in a catch exception-declaration is local to the handler and
2181 // shall not be redeclared in the outermost block of the handler.
David Blaikie1c9c9042012-11-10 01:04:23 +00002182 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope |
David Blaikie3403feb2012-11-13 18:51:45 +00002183 (FnCatch ? Scope::FnTryCatchScope : 0));
Sebastian Redlb219c902008-12-21 16:41:36 +00002184
2185 // exception-declaration is equivalent to '...' or a parameter-declaration
2186 // without default arguments.
John McCall48871652010-08-21 09:40:31 +00002187 Decl *ExceptionDecl = 0;
Sebastian Redlb219c902008-12-21 16:41:36 +00002188 if (Tok.isNot(tok::ellipsis)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002189 ParsedAttributesWithRange Attributes(AttrFactory);
2190 MaybeParseCXX11Attributes(Attributes);
2191
John McCall084e83d2011-03-24 11:26:52 +00002192 DeclSpec DS(AttrFactory);
Richard Smith1dba27c2013-01-29 09:02:09 +00002193 DS.takeAttributesFrom(Attributes);
2194
Sebastian Redl54c04d42008-12-22 19:15:10 +00002195 if (ParseCXXTypeSpecifierSeq(DS))
2196 return StmtError();
Richard Smith1dba27c2013-01-29 09:02:09 +00002197
Sebastian Redlb219c902008-12-21 16:41:36 +00002198 Declarator ExDecl(DS, Declarator::CXXCatchContext);
2199 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002200 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00002201 } else
2202 ConsumeToken();
2203
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002204 T.consumeClose();
2205 if (T.getCloseLocation().isInvalid())
Sebastian Redlb219c902008-12-21 16:41:36 +00002206 return StmtError();
2207
2208 if (Tok.isNot(tok::l_brace))
2209 return StmtError(Diag(Tok, diag::err_expected_lbrace));
2210
Alexis Hunt96d5c762009-11-21 08:43:09 +00002211 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
Richard Smithc202b282012-04-14 00:33:13 +00002212 StmtResult Block(ParseCompoundStatement());
Sebastian Redlb219c902008-12-21 16:41:36 +00002213 if (Block.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002214 return Block;
Sebastian Redlb219c902008-12-21 16:41:36 +00002215
John McCallb268a282010-08-23 23:25:46 +00002216 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00002217}
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002218
2219void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Douglas Gregor43edb322011-10-24 22:31:10 +00002220 IfExistsCondition Result;
Francois Picheta5b3fcb2011-05-07 17:30:27 +00002221 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002222 return;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00002223
Douglas Gregor43edb322011-10-24 22:31:10 +00002224 // Handle dependent statements by parsing the braces as a compound statement.
2225 // This is not the same behavior as Visual C++, which don't treat this as a
2226 // compound statement, but for Clang's type checking we can't have anything
2227 // inside these braces escaping to the surrounding code.
2228 if (Result.Behavior == IEB_Dependent) {
2229 if (!Tok.is(tok::l_brace)) {
2230 Diag(Tok, diag::err_expected_lbrace);
Richard Smithc202b282012-04-14 00:33:13 +00002231 return;
Douglas Gregor43edb322011-10-24 22:31:10 +00002232 }
Richard Smithc202b282012-04-14 00:33:13 +00002233
2234 StmtResult Compound = ParseCompoundStatement();
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002235 if (Compound.isInvalid())
2236 return;
Richard Smithc202b282012-04-14 00:33:13 +00002237
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002238 StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc,
2239 Result.IsIfExists,
Richard Smithc202b282012-04-14 00:33:13 +00002240 Result.SS,
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002241 Result.Name,
2242 Compound.get());
2243 if (DepResult.isUsable())
2244 Stmts.push_back(DepResult.get());
Douglas Gregor43edb322011-10-24 22:31:10 +00002245 return;
2246 }
Richard Smithc202b282012-04-14 00:33:13 +00002247
Douglas Gregor43edb322011-10-24 22:31:10 +00002248 BalancedDelimiterTracker Braces(*this, tok::l_brace);
2249 if (Braces.consumeOpen()) {
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002250 Diag(Tok, diag::err_expected_lbrace);
2251 return;
2252 }
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002253
Douglas Gregor43edb322011-10-24 22:31:10 +00002254 switch (Result.Behavior) {
2255 case IEB_Parse:
2256 // Parse the statements below.
2257 break;
Chad Rosier67055f52012-07-10 21:35:27 +00002258
Douglas Gregor43edb322011-10-24 22:31:10 +00002259 case IEB_Dependent:
2260 llvm_unreachable("Dependent case handled above");
Chad Rosier67055f52012-07-10 21:35:27 +00002261
Douglas Gregor43edb322011-10-24 22:31:10 +00002262 case IEB_Skip:
2263 Braces.skipToEnd();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002264 return;
2265 }
2266
2267 // Condition is true, parse the statements.
2268 while (Tok.isNot(tok::r_brace)) {
2269 StmtResult R = ParseStatementOrDeclaration(Stmts, false);
2270 if (R.isUsable())
2271 Stmts.push_back(R.release());
2272 }
Douglas Gregor43edb322011-10-24 22:31:10 +00002273 Braces.consumeClose();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002274}