blob: 964240df089bf5d0d66fca7e6a849c4b21459cab [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
Chris Lattner0ccd51e2006-08-09 05:47:47 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner0ccd51e2006-08-09 05:47:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000016#include "RAIIObjectsForParser.h"
John McCall8b0666c2010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
John McCallfaf5fb42010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/Scope.h"
Richard Smith4f605af2012-08-18 00:55:03 +000020#include "clang/Sema/TypoCorrection.h"
Chris Lattnerbd61a952009-03-05 00:00:31 +000021#include "clang/Basic/Diagnostic.h"
22#include "clang/Basic/PrettyStackTrace.h"
23#include "clang/Basic/SourceManager.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);
87 MaybeParseCXX0XAttributes(Attrs, 0, /*MightBeObjCMessageSend*/ true);
88
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
288 case tok::annot_pragma_opencl_extension:
289 ProhibitAttributes(Attrs);
290 HandlePragmaOpenCLExtension();
291 return StmtEmpty();
Sebastian Redlb219c902008-12-21 16:41:36 +0000292 }
293
Chris Lattner503fadc2006-08-10 05:45:44 +0000294 // If we reached this code, the statement must end in a semicolon.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000295 if (Tok.is(tok::semi)) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000296 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000297 } else if (!Res.isInvalid()) {
Chris Lattner8e3eed02009-06-14 00:23:56 +0000298 // If the result was valid, then we do want to diagnose this. Use
299 // ExpectAndConsume to emit the diagnostic, even though we know it won't
300 // succeed.
301 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner0046de12008-11-13 18:52:53 +0000302 // Skip until we see a } or ;, but don't eat it.
303 SkipUntil(tok::r_brace, true, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000304 }
Mike Stump11289f42009-09-09 15:08:12 +0000305
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000306 return Res;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000307}
308
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000309/// \brief Parse an expression statement.
Richard Smithc202b282012-04-14 00:33:13 +0000310StmtResult Parser::ParseExprStatement() {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000311 // If a case keyword is missing, this is where it should be inserted.
312 Token OldToken = Tok;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000313
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000314 // expression[opt] ';'
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000315 ExprResult Expr(ParseExpression());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000316 if (Expr.isInvalid()) {
317 // If the expression is invalid, skip ahead to the next semicolon or '}'.
318 // Not doing this opens us up to the possibility of infinite loops if
319 // ParseExpression does not consume any tokens.
320 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
321 if (Tok.is(tok::semi))
322 ConsumeToken();
323 return StmtError();
324 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000325
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000326 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
327 Actions.CheckCaseExpression(Expr.get())) {
328 // If a constant expression is followed by a colon inside a switch block,
329 // suggest a missing case keyword.
330 Diag(OldToken, diag::err_expected_case_before_expression)
331 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000332
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000333 // Recover parsing as a case statement.
Richard Smithc202b282012-04-14 00:33:13 +0000334 return ParseCaseStatement(/*MissingCase=*/true, Expr);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000335 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000336
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000337 // Otherwise, eat the semicolon.
338 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
339 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr.get()));
John Wiegley1c0675e2011-04-28 01:08:34 +0000340}
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000341
Richard Smithc202b282012-04-14 00:33:13 +0000342StmtResult Parser::ParseSEHTryBlock() {
John Wiegley1c0675e2011-04-28 01:08:34 +0000343 assert(Tok.is(tok::kw___try) && "Expected '__try'");
344 SourceLocation Loc = ConsumeToken();
345 return ParseSEHTryBlockCommon(Loc);
346}
347
348/// ParseSEHTryBlockCommon
349///
350/// seh-try-block:
351/// '__try' compound-statement seh-handler
352///
353/// seh-handler:
354/// seh-except-block
355/// seh-finally-block
356///
357StmtResult Parser::ParseSEHTryBlockCommon(SourceLocation TryLoc) {
358 if(Tok.isNot(tok::l_brace))
359 return StmtError(Diag(Tok,diag::err_expected_lbrace));
360
Joao Matos566359c2012-09-04 17:49:35 +0000361 StmtResult TryBlock(ParseCompoundStatement());
John Wiegley1c0675e2011-04-28 01:08:34 +0000362 if(TryBlock.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000363 return TryBlock;
John Wiegley1c0675e2011-04-28 01:08:34 +0000364
365 StmtResult Handler;
Richard Smithc202b282012-04-14 00:33:13 +0000366 if (Tok.is(tok::identifier) &&
Douglas Gregor60060d62011-10-21 03:57:52 +0000367 Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +0000368 SourceLocation Loc = ConsumeToken();
369 Handler = ParseSEHExceptBlock(Loc);
370 } else if (Tok.is(tok::kw___finally)) {
371 SourceLocation Loc = ConsumeToken();
372 Handler = ParseSEHFinallyBlock(Loc);
373 } else {
374 return StmtError(Diag(Tok,diag::err_seh_expected_handler));
375 }
376
377 if(Handler.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000378 return Handler;
John Wiegley1c0675e2011-04-28 01:08:34 +0000379
380 return Actions.ActOnSEHTryBlock(false /* IsCXXTry */,
381 TryLoc,
382 TryBlock.take(),
383 Handler.take());
384}
385
386/// ParseSEHExceptBlock - Handle __except
387///
388/// seh-except-block:
389/// '__except' '(' seh-filter-expression ')' compound-statement
390///
391StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) {
392 PoisonIdentifierRAIIObject raii(Ident__exception_code, false),
393 raii2(Ident___exception_code, false),
394 raii3(Ident_GetExceptionCode, false);
395
396 if(ExpectAndConsume(tok::l_paren,diag::err_expected_lparen))
397 return StmtError();
398
399 ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope);
400
David Blaikiebbafb8a2012-03-11 07:00:24 +0000401 if (getLangOpts().Borland) {
Francois Pichetbfaf4772011-04-28 03:14:31 +0000402 Ident__exception_info->setIsPoisoned(false);
403 Ident___exception_info->setIsPoisoned(false);
404 Ident_GetExceptionInfo->setIsPoisoned(false);
405 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000406 ExprResult FilterExpr(ParseExpression());
Francois Pichetbfaf4772011-04-28 03:14:31 +0000407
David Blaikiebbafb8a2012-03-11 07:00:24 +0000408 if (getLangOpts().Borland) {
Francois Pichetbfaf4772011-04-28 03:14:31 +0000409 Ident__exception_info->setIsPoisoned(true);
410 Ident___exception_info->setIsPoisoned(true);
411 Ident_GetExceptionInfo->setIsPoisoned(true);
412 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000413
414 if(FilterExpr.isInvalid())
415 return StmtError();
416
417 if(ExpectAndConsume(tok::r_paren,diag::err_expected_rparen))
418 return StmtError();
419
Richard Smithc202b282012-04-14 00:33:13 +0000420 StmtResult Block(ParseCompoundStatement());
John Wiegley1c0675e2011-04-28 01:08:34 +0000421
422 if(Block.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000423 return Block;
John Wiegley1c0675e2011-04-28 01:08:34 +0000424
425 return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.take(), Block.take());
426}
427
428/// ParseSEHFinallyBlock - Handle __finally
429///
430/// seh-finally-block:
431/// '__finally' compound-statement
432///
433StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyBlock) {
434 PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
435 raii2(Ident___abnormal_termination, false),
436 raii3(Ident_AbnormalTermination, false);
437
Richard Smithc202b282012-04-14 00:33:13 +0000438 StmtResult Block(ParseCompoundStatement());
John Wiegley1c0675e2011-04-28 01:08:34 +0000439 if(Block.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000440 return Block;
John Wiegley1c0675e2011-04-28 01:08:34 +0000441
442 return Actions.ActOnSEHFinallyBlock(FinallyBlock,Block.take());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000443}
444
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000445/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000446///
447/// labeled-statement:
448/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000449/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000450///
Richard Smithc202b282012-04-14 00:33:13 +0000451StmtResult Parser::ParseLabeledStatement(ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000452 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
453 "Not an identifier!");
454
455 Token IdentTok = Tok; // Save the whole token.
456 ConsumeToken(); // eat the identifier.
457
458 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000459
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000460 // identifier ':' statement
461 SourceLocation ColonLoc = ConsumeToken();
462
Richard Smithc202b282012-04-14 00:33:13 +0000463 // Read label attributes, if present. attrs will contain both C++11 and GNU
464 // attributes (if present) after this point.
John McCall53fa7142010-12-24 02:08:15 +0000465 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000466
John McCalldadc5752010-08-24 06:29:42 +0000467 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000468
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000469 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000470 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000471 SubStmt = Actions.ActOnNullStmt(ColonLoc);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000472
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000473 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
474 IdentTok.getLocation());
Richard Smithc202b282012-04-14 00:33:13 +0000475 if (AttributeList *Attrs = attrs.getList()) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000476 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
Richard Smithc202b282012-04-14 00:33:13 +0000477 attrs.clear();
478 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000479
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000480 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
481 SubStmt.get());
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000482}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000483
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000484/// ParseCaseStatement
485/// labeled-statement:
486/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000487/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000488///
Richard Smithc202b282012-04-14 00:33:13 +0000489StmtResult Parser::ParseCaseStatement(bool MissingCase, ExprResult Expr) {
Richard Smithd4257d82011-04-21 22:48:40 +0000490 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Mike Stump11289f42009-09-09 15:08:12 +0000491
Chris Lattner34a22092009-03-04 04:23:07 +0000492 // It is very very common for code to contain many case statements recursively
493 // nested, as in (but usually without indentation):
494 // case 1:
495 // case 2:
496 // case 3:
497 // case 4:
498 // case 5: etc.
499 //
500 // Parsing this naively works, but is both inefficient and can cause us to run
501 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner2b19a6582009-03-04 18:24:58 +0000502 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner34a22092009-03-04 04:23:07 +0000503 // but all the grossness is constrained to ParseCaseStatement (and some
504 // wierdness in the actions), so this is just local grossness :).
Mike Stump11289f42009-09-09 15:08:12 +0000505
Chris Lattner34a22092009-03-04 04:23:07 +0000506 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
507 // example above.
John McCalldadc5752010-08-24 06:29:42 +0000508 StmtResult TopLevelCase(true);
Mike Stump11289f42009-09-09 15:08:12 +0000509
Chris Lattner34a22092009-03-04 04:23:07 +0000510 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
511 // gets updated each time a new case is parsed, and whose body is unset so
512 // far. When parsing 'case 4', this is the 'case 3' node.
Richard Trieu3481fcd2011-09-09 02:16:15 +0000513 Stmt *DeepestParsedCaseStmt = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000514
Chris Lattner34a22092009-03-04 04:23:07 +0000515 // While we have case statements, eat and stack them.
David Majnemer0ac67fa2011-06-13 05:50:12 +0000516 SourceLocation ColonLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000517 do {
Richard Trieu2c850c02011-04-21 21:44:26 +0000518 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
519 ConsumeToken(); // eat the 'case'.
Mike Stump11289f42009-09-09 15:08:12 +0000520
Douglas Gregord328d572009-09-21 18:10:23 +0000521 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000522 Actions.CodeCompleteCase(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000523 cutOffParsing();
524 return StmtError();
Douglas Gregord328d572009-09-21 18:10:23 +0000525 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000526
Chris Lattner125c0ee2009-12-10 00:38:54 +0000527 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
528 /// Disable this form of error recovery while we're parsing the case
529 /// expression.
530 ColonProtectionRAIIObject ColonProtection(*this);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000531
Richard Trieu2c850c02011-04-21 21:44:26 +0000532 ExprResult LHS(MissingCase ? Expr : ParseConstantExpression());
533 MissingCase = false;
Chris Lattner34a22092009-03-04 04:23:07 +0000534 if (LHS.isInvalid()) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000535 SkipUntil(tok::colon);
Sebastian Redl042ad952008-12-11 19:30:53 +0000536 return StmtError();
Chris Lattner476c3ad2006-08-13 22:09:58 +0000537 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000538
Chris Lattner34a22092009-03-04 04:23:07 +0000539 // GNU case range extension.
540 SourceLocation DotDotDotLoc;
John McCalldadc5752010-08-24 06:29:42 +0000541 ExprResult RHS;
Chris Lattner34a22092009-03-04 04:23:07 +0000542 if (Tok.is(tok::ellipsis)) {
543 Diag(Tok, diag::ext_gnu_case_range);
544 DotDotDotLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000545
Chris Lattner34a22092009-03-04 04:23:07 +0000546 RHS = ParseConstantExpression();
547 if (RHS.isInvalid()) {
548 SkipUntil(tok::colon);
549 return StmtError();
550 }
551 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000552
Chris Lattner125c0ee2009-12-10 00:38:54 +0000553 ColonProtection.restore();
Sebastian Redl042ad952008-12-11 19:30:53 +0000554
John McCall0140bfe2011-01-22 09:28:32 +0000555 if (Tok.is(tok::colon)) {
556 ColonLoc = ConsumeToken();
557
558 // Treat "case blah;" as a typo for "case blah:".
559 } else if (Tok.is(tok::semi)) {
560 ColonLoc = ConsumeToken();
561 Diag(ColonLoc, diag::err_expected_colon_after) << "'case'"
562 << FixItHint::CreateReplacement(ColonLoc, ":");
563 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000564 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
565 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'"
566 << FixItHint::CreateInsertion(ExpectedLoc, ":");
567 ColonLoc = ExpectedLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000568 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000569
John McCalldadc5752010-08-24 06:29:42 +0000570 StmtResult Case =
John McCallb268a282010-08-23 23:25:46 +0000571 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
572 RHS.get(), ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000573
Chris Lattner34a22092009-03-04 04:23:07 +0000574 // If we had a sema error parsing this case, then just ignore it and
575 // continue parsing the sub-stmt.
576 if (Case.isInvalid()) {
577 if (TopLevelCase.isInvalid()) // No parsed case stmts.
578 return ParseStatement();
579 // Otherwise, just don't add it as a nested case.
580 } else {
581 // If this is the first case statement we parsed, it becomes TopLevelCase.
582 // Otherwise we link it into the current chain.
John McCall37ad5512010-08-23 06:44:23 +0000583 Stmt *NextDeepest = Case.get();
Chris Lattner34a22092009-03-04 04:23:07 +0000584 if (TopLevelCase.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000585 TopLevelCase = Case;
Chris Lattner34a22092009-03-04 04:23:07 +0000586 else
John McCallb268a282010-08-23 23:25:46 +0000587 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner34a22092009-03-04 04:23:07 +0000588 DeepestParsedCaseStmt = NextDeepest;
589 }
Mike Stump11289f42009-09-09 15:08:12 +0000590
Chris Lattner34a22092009-03-04 04:23:07 +0000591 // Handle all case statements.
592 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000593
Chris Lattner34a22092009-03-04 04:23:07 +0000594 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump11289f42009-09-09 15:08:12 +0000595
Chris Lattner34a22092009-03-04 04:23:07 +0000596 // If we found a non-case statement, start by parsing it.
John McCalldadc5752010-08-24 06:29:42 +0000597 StmtResult SubStmt;
Mike Stump11289f42009-09-09 15:08:12 +0000598
Chris Lattner34a22092009-03-04 04:23:07 +0000599 if (Tok.isNot(tok::r_brace)) {
600 SubStmt = ParseStatement();
601 } else {
602 // Nicely diagnose the common error "switch (X) { case 4: }", which is
603 // not valid.
David Majnemerc6a99872011-06-14 15:24:38 +0000604 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
Richard Smith1002d102012-02-17 01:35:32 +0000605 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
606 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
Chris Lattner34a22092009-03-04 04:23:07 +0000607 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000608 }
Mike Stump11289f42009-09-09 15:08:12 +0000609
Chris Lattner34a22092009-03-04 04:23:07 +0000610 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000611 if (SubStmt.isInvalid())
Chris Lattner34a22092009-03-04 04:23:07 +0000612 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000613
Chris Lattner34a22092009-03-04 04:23:07 +0000614 // Install the body into the most deeply-nested case.
John McCallb268a282010-08-23 23:25:46 +0000615 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000616
Chris Lattner34a22092009-03-04 04:23:07 +0000617 // Return the top level parsed statement tree.
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000618 return TopLevelCase;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000619}
620
621/// ParseDefaultStatement
622/// labeled-statement:
623/// 'default' ':' statement
624/// Note that this does not parse the 'statement' at the end.
625///
Richard Smithc202b282012-04-14 00:33:13 +0000626StmtResult Parser::ParseDefaultStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000627 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000628 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000629
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000630 SourceLocation ColonLoc;
John McCall0140bfe2011-01-22 09:28:32 +0000631 if (Tok.is(tok::colon)) {
632 ColonLoc = ConsumeToken();
633
634 // Treat "default;" as a typo for "default:".
635 } else if (Tok.is(tok::semi)) {
636 ColonLoc = ConsumeToken();
637 Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
638 << FixItHint::CreateReplacement(ColonLoc, ":");
639 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000640 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
641 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
642 << FixItHint::CreateInsertion(ExpectedLoc, ":");
643 ColonLoc = ExpectedLoc;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000644 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000645
Richard Smith1002d102012-02-17 01:35:32 +0000646 StmtResult SubStmt;
647
648 if (Tok.isNot(tok::r_brace)) {
649 SubStmt = ParseStatement();
650 } else {
651 // Diagnose the common error "switch (X) {... default: }", which is
652 // not valid.
David Majnemerc6a99872011-06-14 15:24:38 +0000653 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
Richard Smith1002d102012-02-17 01:35:32 +0000654 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
655 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
656 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000657 }
658
Richard Smith1002d102012-02-17 01:35:32 +0000659 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000660 if (SubStmt.isInvalid())
Richard Smith1002d102012-02-17 01:35:32 +0000661 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl042ad952008-12-11 19:30:53 +0000662
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000663 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000664 SubStmt.get(), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000665}
666
Richard Smithc202b282012-04-14 00:33:13 +0000667StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
668 return ParseCompoundStatement(isStmtExpr, Scope::DeclScope);
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000669}
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000670
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000671/// ParseCompoundStatement - Parse a "{}" block.
672///
673/// compound-statement: [C99 6.8.2]
674/// { block-item-list[opt] }
675/// [GNU] { label-declarations block-item-list } [TODO]
676///
677/// block-item-list:
678/// block-item
679/// block-item-list block-item
680///
681/// block-item:
682/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000683/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000684/// statement
685/// [OMP] openmp-directive [TODO]
686///
687/// [GNU] label-declarations:
688/// [GNU] label-declaration
689/// [GNU] label-declarations label-declaration
690///
691/// [GNU] label-declaration:
692/// [GNU] '__label__' identifier-list ';'
693///
694/// [OMP] openmp-directive: [TODO]
695/// [OMP] barrier-directive
696/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000697///
Richard Smithc202b282012-04-14 00:33:13 +0000698StmtResult Parser::ParseCompoundStatement(bool isStmtExpr,
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000699 unsigned ScopeFlags) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000700 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000701
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000702 // Enter a scope to hold everything within the compound stmt. Compound
703 // statements can always hold declarations.
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000704 ParseScope CompoundScope(this, ScopeFlags);
Chris Lattnerf2978802007-01-21 06:52:16 +0000705
706 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000707 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000708}
709
Chris Lattnerf2978802007-01-21 06:52:16 +0000710/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000711/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000712/// consume the '}' at the end of the block. It does not manipulate the scope
713/// stack.
John McCalldadc5752010-08-24 06:29:42 +0000714StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000715 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000716 Tok.getLocation(),
717 "in compound statement ('{}')");
Lang Hames5de91cc2012-10-02 04:45:10 +0000718
719 // Record the state of the FP_CONTRACT pragma, restore on leaving the
720 // compound statement.
721 Sema::FPContractStateRAII SaveFPContractState(Actions);
722
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000723 InMessageExpressionRAIIObject InMessage(*this, false);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000724 BalancedDelimiterTracker T(*this, tok::l_brace);
725 if (T.consumeOpen())
726 return StmtError();
Chris Lattnerf2978802007-01-21 06:52:16 +0000727
Dmitri Gribenko800ddf32012-02-14 22:14:32 +0000728 Sema::CompoundScopeRAII CompoundScope(Actions);
729
Benjamin Kramerf0623432012-08-23 22:51:59 +0000730 StmtVector Stmts;
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000731
Lang Hamesa930e712012-10-21 01:10:01 +0000732 // Parse FP_CONTRACT if present.
733 if (Tok.is(tok::annot_pragma_fp_contract))
734 HandlePragmaFPContract();
735
Chris Lattner43e7f312011-02-18 02:08:43 +0000736 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
737 // only allowed at the start of a compound stmt regardless of the language.
738 while (Tok.is(tok::kw___label__)) {
739 SourceLocation LabelLoc = ConsumeToken();
740 Diag(LabelLoc, diag::ext_gnu_local_label);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000741
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000742 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner43e7f312011-02-18 02:08:43 +0000743 while (1) {
744 if (Tok.isNot(tok::identifier)) {
745 Diag(Tok, diag::err_expected_ident);
746 break;
747 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000748
Chris Lattner43e7f312011-02-18 02:08:43 +0000749 IdentifierInfo *II = Tok.getIdentifierInfo();
750 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000751 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000752
Chris Lattner43e7f312011-02-18 02:08:43 +0000753 if (!Tok.is(tok::comma))
754 break;
755 ConsumeToken();
756 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000757
John McCall084e83d2011-03-24 11:26:52 +0000758 DeclSpec DS(AttrFactory);
Chris Lattner43e7f312011-02-18 02:08:43 +0000759 DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
760 DeclsInGroup.data(), DeclsInGroup.size());
761 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000762
Chris Lattner02f1b612012-04-28 16:12:17 +0000763 ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
Chris Lattner43e7f312011-02-18 02:08:43 +0000764 if (R.isUsable())
765 Stmts.push_back(R.release());
766 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000767
Chris Lattner43e7f312011-02-18 02:08:43 +0000768 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000769 if (Tok.is(tok::annot_pragma_unused)) {
770 HandlePragmaUnused();
771 continue;
772 }
773
David Blaikiebbafb8a2012-03-11 07:00:24 +0000774 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet4a7de3e2011-05-06 20:48:22 +0000775 Tok.is(tok::kw___if_not_exists))) {
776 ParseMicrosoftIfExistsStatement(Stmts);
777 continue;
778 }
779
John McCalldadc5752010-08-24 06:29:42 +0000780 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000781 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000782 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000783 } else {
784 // __extension__ can start declarations and it can also be a unary
785 // operator for expressions. Consume multiple __extension__ markers here
786 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000787 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000788 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000789 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000790 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +0000791
John McCall084e83d2011-03-24 11:26:52 +0000792 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith7bdcc4a2012-04-10 01:32:12 +0000793 MaybeParseCXX0XAttributes(attrs, 0, /*MightBeObjCMessageSend*/ true);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000794
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000795 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000796 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +0000797 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +0000798 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +0000799 ExtensionRAIIObject O(Diags);
800
Chris Lattner49836b42009-04-02 04:16:50 +0000801 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000802 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
803 Declarator::BlockContext, DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000804 attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000805 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000806 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000807 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +0000808 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +0000809
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000810 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000811 SkipUntil(tok::semi);
812 continue;
813 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000814
Alexis Hunt96d5c762009-11-21 08:43:09 +0000815 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +0000816 // Eat the semicolon at the end of stmt and convert the expr into a
817 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000818 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +0000819 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000820 }
821 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000822
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000823 if (R.isUsable())
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000824 Stmts.push_back(R.release());
Chris Lattner30f910e2006-10-16 05:52:41 +0000825 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000826
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +0000827 SourceLocation CloseLoc = Tok.getLocation();
828
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000829 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000830 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000831 Diag(Tok, diag::err_expected_rbrace);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000832 Diag(T.getOpenLocation(), diag::note_matching) << "{";
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +0000833 // Recover by creating a compound statement with what we parsed so far,
834 // instead of dropping everything and returning StmtError();
835 } else {
836 if (!T.consumeClose())
837 CloseLoc = T.getCloseLocation();
Chris Lattner30f910e2006-10-16 05:52:41 +0000838 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000839
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +0000840 return Actions.ActOnCompoundStmt(T.getOpenLocation(), CloseLoc,
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000841 Stmts, isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000842}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000843
Chris Lattnerc0081db2008-12-12 06:31:07 +0000844/// ParseParenExprOrCondition:
845/// [C ] '(' expression ')'
Chris Lattner10da53c2008-12-12 06:35:28 +0000846/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattnerc0081db2008-12-12 06:31:07 +0000847///
848/// This function parses and performs error recovery on the specified condition
849/// or expression (depending on whether we're in C++ or C mode). This function
850/// goes out of its way to recover well. It returns true if there was a parser
851/// error (the right paren couldn't be found), which indicates that the caller
852/// should try to recover harder. It returns false if the condition is
853/// successfully parsed. Note that a successful parse can still have semantic
854/// errors in the condition.
John McCalldadc5752010-08-24 06:29:42 +0000855bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCall48871652010-08-21 09:40:31 +0000856 Decl *&DeclResult,
Douglas Gregore60e41a2010-05-06 17:25:47 +0000857 SourceLocation Loc,
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000858 bool ConvertToBoolean) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000859 BalancedDelimiterTracker T(*this, tok::l_paren);
860 T.consumeOpen();
861
David Blaikiebbafb8a2012-03-11 07:00:24 +0000862 if (getLangOpts().CPlusPlus)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000863 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000864 else {
865 ExprResult = ParseExpression();
John McCall48871652010-08-21 09:40:31 +0000866 DeclResult = 0;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000867
Douglas Gregore60e41a2010-05-06 17:25:47 +0000868 // If required, convert to a boolean value.
869 if (!ExprResult.isInvalid() && ConvertToBoolean)
870 ExprResult
John McCallb268a282010-08-23 23:25:46 +0000871 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000872 }
Mike Stump11289f42009-09-09 15:08:12 +0000873
Chris Lattnerc0081db2008-12-12 06:31:07 +0000874 // If the parser was confused by the condition and we don't have a ')', try to
875 // recover by skipping ahead to a semi and bailing out. If condexp is
876 // semantically invalid but we have well formed code, keep going.
John McCall48871652010-08-21 09:40:31 +0000877 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000878 SkipUntil(tok::semi);
879 // Skipping may have stopped if it found the containing ')'. If so, we can
880 // continue parsing the if statement.
881 if (Tok.isNot(tok::r_paren))
882 return true;
883 }
Mike Stump11289f42009-09-09 15:08:12 +0000884
Chris Lattnerc0081db2008-12-12 06:31:07 +0000885 // Otherwise the condition is valid or the rparen is present.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000886 T.consumeClose();
Chad Rosier67055f52012-07-10 21:35:27 +0000887
Chris Lattner70d44982012-04-28 16:24:20 +0000888 // Check for extraneous ')'s to catch things like "if (foo())) {". We know
889 // that all callers are looking for a statement after the condition, so ")"
890 // isn't valid.
891 while (Tok.is(tok::r_paren)) {
892 Diag(Tok, diag::err_extraneous_rparen_in_condition)
893 << FixItHint::CreateRemoval(Tok.getLocation());
894 ConsumeParen();
895 }
Chad Rosier67055f52012-07-10 21:35:27 +0000896
Chris Lattnerc0081db2008-12-12 06:31:07 +0000897 return false;
898}
899
900
Chris Lattnerc951dae2006-08-10 04:23:57 +0000901/// ParseIfStatement
902/// if-statement: [C99 6.8.4.1]
903/// 'if' '(' expression ')' statement
904/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000905/// [C++] 'if' '(' condition ')' statement
906/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000907///
Richard Smithc202b282012-04-14 00:33:13 +0000908StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000909 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000910 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000911
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000912 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000913 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +0000914 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +0000915 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000916 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000917
David Blaikiebbafb8a2012-03-11 07:00:24 +0000918 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000919
Chris Lattner2dd1b722007-08-26 23:08:06 +0000920 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
921 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000922 //
923 // C++ 6.4p3:
924 // A name introduced by a declaration in a condition is in scope from its
925 // point of declaration until the end of the substatements controlled by the
926 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000927 // C++ 3.3.2p4:
928 // Names declared in the for-init-statement, and in the condition of if,
929 // while, for, and switch statements are local to the if, while, for, or
930 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000931 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000932 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000933
Chris Lattnerc951dae2006-08-10 04:23:57 +0000934 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000935 ExprResult CondExp;
John McCall48871652010-08-21 09:40:31 +0000936 Decl *CondVar = 0;
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000937 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000938 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000939
David Blaikiea5696df2012-05-16 04:20:04 +0000940 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get(), IfLoc));
Mike Stump11289f42009-09-09 15:08:12 +0000941
Chris Lattner8fb26252007-08-22 05:28:50 +0000942 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000943 // there is no compound stmt. C90 does not have this clause. We only do this
944 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000945 //
946 // C++ 6.4p1:
947 // The substatement in a selection-statement (each substatement, in the else
948 // form of the if statement) implicitly defines a local scope.
949 //
950 // For C++ we create a scope for the condition and a new scope for
951 // substatements because:
952 // -When the 'then' scope exits, we want the condition declaration to still be
953 // active for the 'else' scope too.
954 // -Sema will detect name clashes by considering declarations of a
955 // 'ControlScope' as part of its direct subscope.
956 // -If we wanted the condition and substatement to be in the same scope, we
957 // would have to notify ParseStatement not to create a new scope. It's
958 // simpler to let it create a new scope.
959 //
Mike Stump11289f42009-09-09 15:08:12 +0000960 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000961 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000962
Chris Lattner5c5808a2007-10-29 05:08:52 +0000963 // Read the 'then' stmt.
964 SourceLocation ThenStmtLoc = Tok.getLocation();
Nico Weber3cef1082011-12-22 23:26:17 +0000965
966 SourceLocation InnerStatementTrailingElseLoc;
967 StmtResult ThenStmt(ParseStatement(&InnerStatementTrailingElseLoc));
Chris Lattnerac4471c2007-05-28 05:38:24 +0000968
Chris Lattner37e54f42007-08-22 05:16:28 +0000969 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000970 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000971
Chris Lattnerc951dae2006-08-10 04:23:57 +0000972 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000973 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +0000974 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +0000975 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000976
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000977 if (Tok.is(tok::kw_else)) {
Nico Weber3cef1082011-12-22 23:26:17 +0000978 if (TrailingElseLoc)
979 *TrailingElseLoc = Tok.getLocation();
980
Chris Lattneraf635312006-10-16 06:06:51 +0000981 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +0000982 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +0000983
Chris Lattner8fb26252007-08-22 05:28:50 +0000984 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000985 // there is no compound stmt. C90 does not have this clause. We only do
986 // this if the body isn't a compound statement to avoid push/pop in common
987 // 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 //
Sebastian Redl042ad952008-12-11 19:30:53 +0000993 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000994 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000995
Chris Lattner30f910e2006-10-16 05:52:41 +0000996 ElseStmt = ParseStatement();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000997
Chris Lattner37e54f42007-08-22 05:16:28 +0000998 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000999 InnerScope.Exit();
Douglas Gregor4ecb7202011-07-30 08:36:53 +00001000 } else if (Tok.is(tok::code_completion)) {
1001 Actions.CodeCompleteAfterIf(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001002 cutOffParsing();
1003 return StmtError();
Nico Weber3cef1082011-12-22 23:26:17 +00001004 } else if (InnerStatementTrailingElseLoc.isValid()) {
1005 Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else);
Chris Lattnerc951dae2006-08-10 04:23:57 +00001006 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001007
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001008 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +00001009
Chris Lattnerbc2d77c2008-12-12 06:19:11 +00001010 // If the condition was invalid, discard the if statement. We could recover
1011 // better by replacing it with a valid expr, but don't do that yet.
John McCall48871652010-08-21 09:40:31 +00001012 if (CondExp.isInvalid() && !CondVar)
Chris Lattnerbc2d77c2008-12-12 06:19:11 +00001013 return StmtError();
Chris Lattner2dd1b722007-08-26 23:08:06 +00001014
Chris Lattner5c5808a2007-10-29 05:08:52 +00001015 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +00001016 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +00001017 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001018 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
1019 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
1020 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +00001021 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +00001022 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +00001023 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001024
Chris Lattner5c5808a2007-10-29 05:08:52 +00001025 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001026 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001027 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001028 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001029 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001030
John McCallb268a282010-08-23 23:25:46 +00001031 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +00001032 ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +00001033}
1034
Chris Lattner9075bd72006-08-10 04:59:57 +00001035/// ParseSwitchStatement
1036/// switch-statement:
1037/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001038/// [C++] 'switch' '(' condition ')' statement
Richard Smithc202b282012-04-14 00:33:13 +00001039StmtResult Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001040 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001041 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +00001042
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001043 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001044 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +00001045 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001046 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001047 }
Chris Lattner2dd1b722007-08-26 23:08:06 +00001048
David Blaikiebbafb8a2012-03-11 07:00:24 +00001049 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001050
Chris Lattner2dd1b722007-08-26 23:08:06 +00001051 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1052 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001053 //
1054 // C++ 6.4p3:
1055 // A name introduced by a declaration in a condition is in scope from its
1056 // point of declaration until the end of the substatements controlled by the
1057 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001058 // C++ 3.3.2p4:
1059 // Names declared in the for-init-statement, and in the condition of if,
1060 // while, for, and switch statements are local to the if, while, for, or
1061 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001062 //
Richard Trieu2c850c02011-04-21 21:44:26 +00001063 unsigned ScopeFlags = Scope::BreakScope | Scope::SwitchScope;
Chris Lattnerc0081db2008-12-12 06:31:07 +00001064 if (C99orCXX)
1065 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001066 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001067
Chris Lattner9075bd72006-08-10 04:59:57 +00001068 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001069 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +00001070 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001071 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redlb62406f2008-12-11 19:48:14 +00001072 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +00001073
John McCalldadc5752010-08-24 06:29:42 +00001074 StmtResult Switch
John McCallb268a282010-08-23 23:25:46 +00001075 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001076
Douglas Gregore60e41a2010-05-06 17:25:47 +00001077 if (Switch.isInvalid()) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001078 // Skip the switch body.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001079 // FIXME: This is not optimal recovery, but parsing the body is more
1080 // dangerous due to the presence of case and default statements, which
1081 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +00001082 if (Tok.is(tok::l_brace)) {
1083 ConsumeBrace();
1084 SkipUntil(tok::r_brace, false, false);
1085 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +00001086 SkipUntil(tok::semi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001087 return Switch;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001088 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001089
Chris Lattner8fb26252007-08-22 05:28:50 +00001090 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001091 // there is no compound stmt. C90 does not have this clause. We only do this
1092 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001093 //
1094 // C++ 6.4p1:
1095 // The substatement in a selection-statement (each substatement, in the else
1096 // form of the if statement) implicitly defines a local scope.
1097 //
1098 // See comments in ParseIfStatement for why we create a scope for the
1099 // condition and a new scope for substatement in C++.
1100 //
Mike Stump11289f42009-09-09 15:08:12 +00001101 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001102 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +00001103
Chris Lattner9075bd72006-08-10 04:59:57 +00001104 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001105 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001106
Chris Lattner8fd2d012010-01-24 01:50:29 +00001107 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001108 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001109 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001110
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001111 if (Body.isInvalid()) {
Chris Lattner8fd2d012010-01-24 01:50:29 +00001112 // FIXME: Remove the case statement list from the Switch statement.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001113
1114 // Put the synthesized null statement on the same line as the end of switch
1115 // condition.
1116 SourceLocation SynthesizedNullStmtLocation = Cond.get()->getLocEnd();
1117 Body = Actions.ActOnNullStmt(SynthesizedNullStmtLocation);
1118 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001119
John McCallb268a282010-08-23 23:25:46 +00001120 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001121}
1122
1123/// ParseWhileStatement
1124/// while-statement: [C99 6.8.5.1]
1125/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001126/// [C++] 'while' '(' condition ')' statement
Richard Smithc202b282012-04-14 00:33:13 +00001127StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001128 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +00001129 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +00001130 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001131
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001132 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001133 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +00001134 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001135 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001136 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001137
David Blaikiebbafb8a2012-03-11 07:00:24 +00001138 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001139
Chris Lattner2dd1b722007-08-26 23:08:06 +00001140 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1141 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001142 //
1143 // C++ 6.4p3:
1144 // A name introduced by a declaration in a condition is in scope from its
1145 // point of declaration until the end of the substatements controlled by the
1146 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001147 // C++ 3.3.2p4:
1148 // Names declared in the for-init-statement, and in the condition of if,
1149 // while, for, and switch statements are local to the if, while, for, or
1150 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001151 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001152 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001153 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001154 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1155 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001156 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001157 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1158 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001159
Chris Lattner9075bd72006-08-10 04:59:57 +00001160 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001161 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +00001162 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001163 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001164 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001165
David Blaikiea5696df2012-05-16 04:20:04 +00001166 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get(), WhileLoc));
Mike Stump11289f42009-09-09 15:08:12 +00001167
Chris Lattner8fb26252007-08-22 05:28:50 +00001168 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001169 // there is no compound stmt. C90 does not have this clause. We only do this
1170 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001171 //
1172 // C++ 6.5p2:
1173 // The substatement in an iteration-statement implicitly defines a local scope
1174 // which is entered and exited each time through the loop.
1175 //
1176 // See comments in ParseIfStatement for why we create a scope for the
1177 // condition and a new scope for substatement in C++.
1178 //
Mike Stump11289f42009-09-09 15:08:12 +00001179 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001180 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001181
Chris Lattner9075bd72006-08-10 04:59:57 +00001182 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001183 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001184
Chris Lattner8fb26252007-08-22 05:28:50 +00001185 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001186 InnerScope.Exit();
1187 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001188
John McCall48871652010-08-21 09:40:31 +00001189 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001190 return StmtError();
1191
John McCallb268a282010-08-23 23:25:46 +00001192 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001193}
1194
1195/// ParseDoStatement
1196/// do-statement: [C99 6.8.5.2]
1197/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +00001198/// Note: this lets the caller parse the end ';'.
Richard Smithc202b282012-04-14 00:33:13 +00001199StmtResult Parser::ParseDoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001200 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001201 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001202
Chris Lattner2dd1b722007-08-26 23:08:06 +00001203 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1204 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001205 unsigned ScopeFlags;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001206 if (getLangOpts().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001207 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001208 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001209 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001210
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001211 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001212
Chris Lattner8fb26252007-08-22 05:28:50 +00001213 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001214 // there is no compound stmt. C90 does not have this clause. We only do this
1215 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +00001216 //
1217 // C++ 6.5p2:
1218 // The substatement in an iteration-statement implicitly defines a local scope
1219 // which is entered and exited each time through the loop.
1220 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001221 ParseScope InnerScope(this, Scope::DeclScope,
David Blaikiebbafb8a2012-03-11 07:00:24 +00001222 (getLangOpts().C99 || getLangOpts().CPlusPlus) &&
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001223 Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001224
Chris Lattner9075bd72006-08-10 04:59:57 +00001225 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001226 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +00001227
Chris Lattner8fb26252007-08-22 05:28:50 +00001228 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001229 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001230
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001231 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001232 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +00001233 Diag(Tok, diag::err_expected_while);
Chris Lattner03c40412008-11-23 23:17:07 +00001234 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner0046de12008-11-13 18:52:53 +00001235 SkipUntil(tok::semi, false, true);
1236 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001237 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001238 }
Chris Lattneraf635312006-10-16 06:06:51 +00001239 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001240
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001241 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001242 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner0046de12008-11-13 18:52:53 +00001243 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001244 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001245 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001246
Chris Lattner10da53c2008-12-12 06:35:28 +00001247 // Parse the parenthesized condition.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001248 BalancedDelimiterTracker T(*this, tok::l_paren);
1249 T.consumeOpen();
Chad Rosier67055f52012-07-10 21:35:27 +00001250
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001251 // FIXME: Do not just parse the attribute contents and throw them away
1252 ParsedAttributesWithRange attrs(AttrFactory);
1253 MaybeParseCXX0XAttributes(attrs);
1254 ProhibitAttributes(attrs);
1255
John McCalldadc5752010-08-24 06:29:42 +00001256 ExprResult Cond = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001257 T.consumeClose();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001258 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001259
Sebastian Redlb62406f2008-12-11 19:48:14 +00001260 if (Cond.isInvalid() || Body.isInvalid())
1261 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001262
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001263 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(),
1264 Cond.get(), T.getCloseLocation());
Chris Lattner9075bd72006-08-10 04:59:57 +00001265}
1266
1267/// ParseForStatement
1268/// for-statement: [C99 6.8.5.3]
1269/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1270/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001271/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1272/// [C++] statement
Richard Smith02e85f32011-04-14 22:09:26 +00001273/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001274/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1275/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001276///
1277/// [C++] for-init-statement:
1278/// [C++] expression-statement
1279/// [C++] simple-declaration
1280///
Richard Smith02e85f32011-04-14 22:09:26 +00001281/// [C++0x] for-range-declaration:
1282/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1283/// [C++0x] for-range-initializer:
1284/// [C++0x] expression
1285/// [C++0x] braced-init-list [TODO]
Richard Smithc202b282012-04-14 00:33:13 +00001286StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001287 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001288 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001289
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001290 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001291 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +00001292 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001293 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001294 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001295
Chad Rosier67055f52012-07-10 21:35:27 +00001296 bool C99orCXXorObjC = getLangOpts().C99 || getLangOpts().CPlusPlus ||
1297 getLangOpts().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001298
Chris Lattner2dd1b722007-08-26 23:08:06 +00001299 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1300 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001301 //
1302 // C++ 6.4p3:
1303 // A name introduced by a declaration in a condition is in scope from its
1304 // point of declaration until the end of the substatements controlled by the
1305 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001306 // C++ 3.3.2p4:
1307 // Names declared in the for-init-statement, and in the condition of if,
1308 // while, for, and switch statements are local to the if, while, for, or
1309 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001310 // C++ 6.5.3p1:
1311 // Names declared in the for-init-statement are in the same declarative-region
1312 // as those declared in the condition.
1313 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001314 unsigned ScopeFlags;
Chris Lattner934074c2009-04-22 00:54:41 +00001315 if (C99orCXXorObjC)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001316 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1317 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001318 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001319 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1320
1321 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +00001322
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001323 BalancedDelimiterTracker T(*this, tok::l_paren);
1324 T.consumeOpen();
1325
John McCalldadc5752010-08-24 06:29:42 +00001326 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001327
Richard Smith02e85f32011-04-14 22:09:26 +00001328 bool ForEach = false, ForRange = false;
John McCalldadc5752010-08-24 06:29:42 +00001329 StmtResult FirstPart;
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001330 bool SecondPartIsInvalid = false;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001331 FullExprArg SecondPart(Actions);
John McCalldadc5752010-08-24 06:29:42 +00001332 ExprResult Collection;
Richard Smith02e85f32011-04-14 22:09:26 +00001333 ForRangeInit ForRangeInit;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001334 FullExprArg ThirdPart(Actions);
John McCall48871652010-08-21 09:40:31 +00001335 Decl *SecondVar = 0;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001336
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001337 if (Tok.is(tok::code_completion)) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001338 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001339 C99orCXXorObjC? Sema::PCC_ForInit
1340 : Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001341 cutOffParsing();
1342 return StmtError();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001343 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001344
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001345 ParsedAttributesWithRange attrs(AttrFactory);
1346 MaybeParseCXX0XAttributes(attrs);
1347
Chris Lattner9075bd72006-08-10 04:59:57 +00001348 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001349 if (Tok.is(tok::semi)) { // for (;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001350 ProhibitAttributes(attrs);
Chris Lattner53361ac2006-08-10 05:19:57 +00001351 // no first part, eat the ';'.
1352 ConsumeToken();
Eli Friedman0ffc31c2011-12-20 01:50:37 +00001353 } else if (isForInitDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001354 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001355 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001356 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001357
John McCall084e83d2011-03-24 11:26:52 +00001358 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00001359 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001360
Richard Smith02e85f32011-04-14 22:09:26 +00001361 // In C++0x, "for (T NS:a" might not be a typo for ::
David Blaikiebbafb8a2012-03-11 07:00:24 +00001362 bool MightBeForRangeStmt = getLangOpts().CPlusPlus;
Richard Smith02e85f32011-04-14 22:09:26 +00001363 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1364
Chris Lattner49836b42009-04-02 04:16:50 +00001365 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Benjamin Kramerf0623432012-08-23 22:51:59 +00001366 StmtVector Stmts;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001367 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
Richard Smith02e85f32011-04-14 22:09:26 +00001368 DeclEnd, attrs, false,
1369 MightBeForRangeStmt ?
1370 &ForRangeInit : 0);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001371 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001372
Richard Smith02e85f32011-04-14 22:09:26 +00001373 if (ForRangeInit.ParsedForRangeDecl()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001374 Diag(ForRangeInit.ColonLoc, getLangOpts().CPlusPlus0x ?
Richard Smith5d164bc2011-10-15 05:09:34 +00001375 diag::warn_cxx98_compat_for_range : diag::ext_for_range);
Richard Smith58c74332011-09-04 19:54:14 +00001376
Richard Smith02e85f32011-04-14 22:09:26 +00001377 ForRange = true;
1378 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattner32dc41c2009-03-29 17:27:48 +00001379 ConsumeToken();
1380 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001381 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001382 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001383 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001384
Douglas Gregor68762e72010-08-23 21:17:50 +00001385 if (Tok.is(tok::code_completion)) {
1386 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001387 cutOffParsing();
1388 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001389 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001390 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001391 } else {
1392 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001393 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001394 } else {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001395 ProhibitAttributes(attrs);
Chris Lattner89c50c62006-08-11 06:41:18 +00001396 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +00001397
John McCall34376a62010-12-04 03:47:34 +00001398 ForEach = isTokIdentifier_in();
1399
Chris Lattnercd68f642007-06-27 01:06:29 +00001400 // Turn the expression into a stmt.
John McCall34376a62010-12-04 03:47:34 +00001401 if (!Value.isInvalid()) {
1402 if (ForEach)
1403 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1404 else
1405 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
1406 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001407
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001408 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001409 ConsumeToken();
John McCall34376a62010-12-04 03:47:34 +00001410 } else if (ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001411 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001412
Douglas Gregor68762e72010-08-23 21:17:50 +00001413 if (Tok.is(tok::code_completion)) {
1414 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001415 cutOffParsing();
1416 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001417 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001418 Collection = ParseExpression();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001419 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::colon) && FirstPart.get()) {
Richard Smith4f848f12011-12-20 22:56:20 +00001420 // User tried to write the reasonable, but ill-formed, for-range-statement
1421 // for (expr : expr) { ... }
1422 Diag(Tok, diag::err_for_range_expected_decl)
1423 << FirstPart.get()->getSourceRange();
1424 SkipUntil(tok::r_paren, false, true);
1425 SecondPartIsInvalid = true;
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001426 } else {
Douglas Gregor230a7e62011-02-17 03:38:46 +00001427 if (!Value.isInvalid()) {
1428 Diag(Tok, diag::err_expected_semi_for);
1429 } else {
1430 // Skip until semicolon or rparen, don't consume it.
1431 SkipUntil(tok::r_paren, true, true);
1432 if (Tok.is(tok::semi))
1433 ConsumeToken();
1434 }
Chris Lattner53361ac2006-08-10 05:19:57 +00001435 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001436 }
Richard Smith02e85f32011-04-14 22:09:26 +00001437 if (!ForEach && !ForRange) {
John McCallb268a282010-08-23 23:25:46 +00001438 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001439 // Parse the second part of the for specifier.
1440 if (Tok.is(tok::semi)) { // for (...;;
1441 // no second part.
Douglas Gregor230a7e62011-02-17 03:38:46 +00001442 } else if (Tok.is(tok::r_paren)) {
1443 // missing both semicolons.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001444 } else {
John McCalldadc5752010-08-24 06:29:42 +00001445 ExprResult Second;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001446 if (getLangOpts().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +00001447 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1448 else {
1449 Second = ParseExpression();
1450 if (!Second.isInvalid())
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001451 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCallb268a282010-08-23 23:25:46 +00001452 Second.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001453 }
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001454 SecondPartIsInvalid = Second.isInvalid();
David Blaikiea5696df2012-05-16 04:20:04 +00001455 SecondPart = Actions.MakeFullExpr(Second.get(), ForLoc);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001456 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001457
Douglas Gregor230a7e62011-02-17 03:38:46 +00001458 if (Tok.isNot(tok::semi)) {
1459 if (!SecondPartIsInvalid || SecondVar)
1460 Diag(Tok, diag::err_expected_semi_for);
1461 else
1462 // Skip until semicolon or rparen, don't consume it.
1463 SkipUntil(tok::r_paren, true, true);
1464 }
1465
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001466 if (Tok.is(tok::semi)) {
1467 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001468 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001469
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001470 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001471 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001472 ExprResult Third = ParseExpression();
John McCallb268a282010-08-23 23:25:46 +00001473 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001474 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001475 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001476 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001477 T.consumeClose();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001478
Richard Smith02e85f32011-04-14 22:09:26 +00001479 // We need to perform most of the semantic analysis for a C++0x for-range
1480 // statememt before parsing the body, in order to be able to deduce the type
1481 // of an auto-typed loop variable.
1482 StmtResult ForRangeStmt;
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001483 StmtResult ForEachStmt;
Chad Rosier67055f52012-07-10 21:35:27 +00001484
John McCall53848232011-07-27 01:07:15 +00001485 if (ForRange) {
Sam Panzer2c4ca0f2012-08-16 21:47:25 +00001486 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, FirstPart.take(),
Richard Smith02e85f32011-04-14 22:09:26 +00001487 ForRangeInit.ColonLoc,
1488 ForRangeInit.RangeExpr.get(),
Richard Smitha05b3b52012-09-20 21:52:32 +00001489 T.getCloseLocation(),
1490 Sema::BFRK_Build);
Richard Smith02e85f32011-04-14 22:09:26 +00001491
John McCall53848232011-07-27 01:07:15 +00001492
1493 // Similarly, we need to do the semantic analysis for a for-range
1494 // statement immediately in order to close over temporaries correctly.
1495 } else if (ForEach) {
Sam Panzer2c4ca0f2012-08-16 21:47:25 +00001496 ForEachStmt = Actions.ActOnObjCForCollectionStmt(ForLoc,
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001497 FirstPart.take(),
Chad Rosier67055f52012-07-10 21:35:27 +00001498 Collection.take(),
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001499 T.getCloseLocation());
John McCall53848232011-07-27 01:07:15 +00001500 }
1501
Chris Lattner8fb26252007-08-22 05:28:50 +00001502 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001503 // there is no compound stmt. C90 does not have this clause. We only do this
1504 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001505 //
1506 // C++ 6.5p2:
1507 // The substatement in an iteration-statement implicitly defines a local scope
1508 // which is entered and exited each time through the loop.
1509 //
1510 // See comments in ParseIfStatement for why we create a scope for
1511 // for-init-statement/condition and a new scope for substatement in C++.
1512 //
Mike Stump11289f42009-09-09 15:08:12 +00001513 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner934074c2009-04-22 00:54:41 +00001514 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001515
Chris Lattner9075bd72006-08-10 04:59:57 +00001516 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001517 StmtResult Body(ParseStatement(TrailingElseLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001518
Chris Lattner8fb26252007-08-22 05:28:50 +00001519 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001520 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001521
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001522 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001523 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001524
1525 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001526 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001527
Richard Smith02e85f32011-04-14 22:09:26 +00001528 if (ForEach)
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001529 return Actions.FinishObjCForCollectionStmt(ForEachStmt.take(),
1530 Body.take());
Mike Stump11289f42009-09-09 15:08:12 +00001531
Richard Smith02e85f32011-04-14 22:09:26 +00001532 if (ForRange)
1533 return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take());
1534
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001535 return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.take(),
1536 SecondPart, SecondVar, ThirdPart,
1537 T.getCloseLocation(), Body.take());
Chris Lattner9075bd72006-08-10 04:59:57 +00001538}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001539
Chris Lattner503fadc2006-08-10 05:45:44 +00001540/// ParseGotoStatement
1541/// jump-statement:
1542/// 'goto' identifier ';'
1543/// [GNU] 'goto' '*' expression ';'
1544///
1545/// Note: this lets the caller parse the end ';'.
1546///
Richard Smithc202b282012-04-14 00:33:13 +00001547StmtResult Parser::ParseGotoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001548 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001549 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001550
John McCalldadc5752010-08-24 06:29:42 +00001551 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001552 if (Tok.is(tok::identifier)) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +00001553 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1554 Tok.getLocation());
1555 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Chris Lattner503fadc2006-08-10 05:45:44 +00001556 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001557 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001558 // GNU indirect goto extension.
1559 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001560 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001561 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001562 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001563 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001564 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001565 }
John McCallb268a282010-08-23 23:25:46 +00001566 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001567 } else {
1568 Diag(Tok, diag::err_expected_ident);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001569 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001570 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001571
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001572 return Res;
Chris Lattner503fadc2006-08-10 05:45:44 +00001573}
1574
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001575/// ParseContinueStatement
1576/// jump-statement:
1577/// 'continue' ';'
1578///
1579/// Note: this lets the caller parse the end ';'.
1580///
Richard Smithc202b282012-04-14 00:33:13 +00001581StmtResult Parser::ParseContinueStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001582 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001583 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001584}
1585
1586/// ParseBreakStatement
1587/// jump-statement:
1588/// 'break' ';'
1589///
1590/// Note: this lets the caller parse the end ';'.
1591///
Richard Smithc202b282012-04-14 00:33:13 +00001592StmtResult Parser::ParseBreakStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001593 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001594 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001595}
1596
Chris Lattner503fadc2006-08-10 05:45:44 +00001597/// ParseReturnStatement
1598/// jump-statement:
1599/// 'return' expression[opt] ';'
Richard Smithc202b282012-04-14 00:33:13 +00001600StmtResult Parser::ParseReturnStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001601 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001602 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001603
John McCalldadc5752010-08-24 06:29:42 +00001604 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001605 if (Tok.isNot(tok::semi)) {
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001606 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001607 Actions.CodeCompleteReturn(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001608 cutOffParsing();
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001609 return StmtError();
1610 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001611
David Blaikiebbafb8a2012-03-11 07:00:24 +00001612 if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus) {
Douglas Gregore9e27d92011-03-11 23:10:44 +00001613 R = ParseInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00001614 if (R.isUsable())
David Blaikiebbafb8a2012-03-11 07:00:24 +00001615 Diag(R.get()->getLocStart(), getLangOpts().CPlusPlus0x ?
Richard Smith5d164bc2011-10-15 05:09:34 +00001616 diag::warn_cxx98_compat_generalized_initializer_lists :
1617 diag::ext_generalized_initializer_lists)
Douglas Gregore9e27d92011-03-11 23:10:44 +00001618 << R.get()->getSourceRange();
1619 } else
1620 R = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001621 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001622 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001623 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001624 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001625 }
John McCallb268a282010-08-23 23:25:46 +00001626 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Chris Lattner503fadc2006-08-10 05:45:44 +00001627}
Chris Lattner0116c472006-08-15 06:03:28 +00001628
Eli Friedmana4b02c32011-09-30 01:13:51 +00001629/// ParseMicrosoftAsmStatement. When -fms-extensions/-fasm-blocks is enabled,
1630/// this routine is called to collect the tokens for an MS asm statement.
Chad Rosier32503022012-06-11 20:47:18 +00001631///
1632/// [MS] ms-asm-statement:
1633/// ms-asm-block
1634/// ms-asm-block ms-asm-statement
1635///
1636/// [MS] ms-asm-block:
1637/// '__asm' ms-asm-line '\n'
1638/// '__asm' '{' ms-asm-instruction-block[opt] '}' ';'[opt]
1639///
1640/// [MS] ms-asm-instruction-block
1641/// ms-asm-line
1642/// ms-asm-line '\n' ms-asm-instruction-block
1643///
Eli Friedmana4b02c32011-09-30 01:13:51 +00001644StmtResult Parser::ParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
Chad Rosier175ea242012-08-24 21:42:51 +00001645 // MS-style inline assembly is not fully supported, so emit a warning.
1646 Diag(AsmLoc, diag::warn_unsupported_msasm);
1647
Eli Friedmana4b02c32011-09-30 01:13:51 +00001648 SourceManager &SrcMgr = PP.getSourceManager();
1649 SourceLocation EndLoc = AsmLoc;
Chad Rosier32503022012-06-11 20:47:18 +00001650 SmallVector<Token, 4> AsmToks;
Chad Rosierc97a6bb2012-08-14 19:22:06 +00001651
1652 bool InBraces = false;
1653 unsigned short savedBraceCount = 0;
1654 bool InAsmComment = false;
1655 FileID FID;
1656 unsigned LineNo = 0;
1657 unsigned NumTokensRead = 0;
1658 SourceLocation LBraceLoc;
1659
1660 if (Tok.is(tok::l_brace)) {
1661 // Braced inline asm: consume the opening brace.
1662 InBraces = true;
1663 savedBraceCount = BraceCount;
1664 EndLoc = LBraceLoc = ConsumeBrace();
1665 ++NumTokensRead;
1666 } else {
1667 // Single-line inline asm; compute which line it is on.
1668 std::pair<FileID, unsigned> ExpAsmLoc =
1669 SrcMgr.getDecomposedExpansionLoc(EndLoc);
1670 FID = ExpAsmLoc.first;
1671 LineNo = SrcMgr.getLineNumber(FID, ExpAsmLoc.second);
1672 }
1673
1674 SourceLocation TokLoc = Tok.getLocation();
Eli Friedmana4b02c32011-09-30 01:13:51 +00001675 do {
Chad Rosierc97a6bb2012-08-14 19:22:06 +00001676 // If we hit EOF, we're done, period.
1677 if (Tok.is(tok::eof))
Eli Friedmana4b02c32011-09-30 01:13:51 +00001678 break;
Chad Rosierc97a6bb2012-08-14 19:22:06 +00001679
Chad Rosierc97a6bb2012-08-14 19:22:06 +00001680 if (!InAsmComment && Tok.is(tok::semi)) {
1681 // A semicolon in an asm is the start of a comment.
1682 InAsmComment = true;
1683 if (InBraces) {
1684 // Compute which line the comment is on.
1685 std::pair<FileID, unsigned> ExpSemiLoc =
1686 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1687 FID = ExpSemiLoc.first;
1688 LineNo = SrcMgr.getLineNumber(FID, ExpSemiLoc.second);
1689 }
1690 } else if (!InBraces || InAsmComment) {
1691 // If end-of-line is significant, check whether this token is on a
1692 // new line.
1693 std::pair<FileID, unsigned> ExpLoc =
1694 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1695 if (ExpLoc.first != FID ||
1696 SrcMgr.getLineNumber(ExpLoc.first, ExpLoc.second) != LineNo) {
1697 // If this is a single-line __asm, we're done.
1698 if (!InBraces)
1699 break;
1700 // We're no longer in a comment.
1701 InAsmComment = false;
1702 } else if (!InAsmComment && Tok.is(tok::r_brace)) {
1703 // Single-line asm always ends when a closing brace is seen.
1704 // FIXME: This is compatible with Apple gcc's -fasm-blocks; what
1705 // does MSVC do here?
1706 break;
1707 }
1708 }
1709 if (!InAsmComment && InBraces && Tok.is(tok::r_brace) &&
1710 BraceCount == (savedBraceCount + 1)) {
1711 // Consume the closing brace, and finish
1712 EndLoc = ConsumeBrace();
1713 break;
1714 }
1715
1716 // Consume the next token; make sure we don't modify the brace count etc.
1717 // if we are in a comment.
1718 EndLoc = TokLoc;
1719 if (InAsmComment)
1720 PP.Lex(Tok);
1721 else {
1722 AsmToks.push_back(Tok);
1723 ConsumeAnyToken();
1724 }
1725 TokLoc = Tok.getLocation();
1726 ++NumTokensRead;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001727 } while (1);
Chad Rosier32503022012-06-11 20:47:18 +00001728
Chad Rosierc97a6bb2012-08-14 19:22:06 +00001729 if (InBraces && BraceCount != savedBraceCount) {
1730 // __asm without closing brace (this can happen at EOF).
1731 Diag(Tok, diag::err_expected_rbrace);
1732 Diag(LBraceLoc, diag::note_matching) << "{";
1733 return StmtError();
1734 } else if (NumTokensRead == 0) {
1735 // Empty __asm.
1736 Diag(Tok, diag::err_expected_lbrace);
1737 return StmtError();
1738 }
1739
Chad Rosier175ea242012-08-24 21:42:51 +00001740 // If MS-style inline assembly is disabled, then build an empty asm.
1741 if (!getLangOpts().EmitMicrosoftInlineAsm) {
1742 Token t;
1743 t.setKind(tok::string_literal);
1744 t.setLiteralData("\"/*FIXME: not done*/\"");
1745 t.clearFlag(Token::NeedsCleaning);
1746 t.setLength(21);
1747 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
1748 ExprVector Constraints;
1749 ExprVector Exprs;
1750 ExprVector Clobbers;
Chad Rosierde70e0e2012-08-25 00:11:56 +00001751 return Actions.ActOnGCCAsmStmt(AsmLoc, true, true, 0, 0, 0, Constraints,
1752 Exprs, AsmString.take(), Clobbers, EndLoc);
Chad Rosier175ea242012-08-24 21:42:51 +00001753 }
1754
Chad Rosierc6c71332012-08-06 20:03:45 +00001755 // FIXME: We should be passing source locations for better diagnostics.
Chad Rosierb6f46c12012-08-15 16:53:30 +00001756 return Actions.ActOnMSAsmStmt(AsmLoc, LBraceLoc,
1757 llvm::makeArrayRef(AsmToks), EndLoc);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001758}
1759
Chris Lattner0116c472006-08-15 06:03:28 +00001760/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001761/// asm-statement:
1762/// gnu-asm-statement
1763/// ms-asm-statement
1764///
1765/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001766/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1767///
1768/// [GNU] asm-argument:
1769/// asm-string-literal
1770/// asm-string-literal ':' asm-operands[opt]
1771/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1772/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1773/// ':' asm-clobbers
1774///
1775/// [GNU] asm-clobbers:
1776/// asm-string-literal
1777/// asm-clobbers ',' asm-string-literal
1778///
John McCalldadc5752010-08-24 06:29:42 +00001779StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001780 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001781 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001782
Chad Rosier67055f52012-07-10 21:35:27 +00001783 if (getLangOpts().MicrosoftExt && Tok.isNot(tok::l_paren) &&
1784 !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001785 msAsm = true;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001786 return ParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001787 }
John McCall084e83d2011-03-24 11:26:52 +00001788 DeclSpec DS(AttrFactory);
Chris Lattner0116c472006-08-15 06:03:28 +00001789 SourceLocation Loc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001790 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001791
Chris Lattner0116c472006-08-15 06:03:28 +00001792 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001793 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001794 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001795 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001796 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redlb62406f2008-12-11 19:48:14 +00001797
Chris Lattner0116c472006-08-15 06:03:28 +00001798 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001799 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001800 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001801 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001802 SkipUntil(tok::r_paren);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001803 return StmtError();
Chris Lattner0116c472006-08-15 06:03:28 +00001804 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001805 BalancedDelimiterTracker T(*this, tok::l_paren);
1806 T.consumeOpen();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001807
John McCalldadc5752010-08-24 06:29:42 +00001808 ExprResult AsmString(ParseAsmStringLiteral());
Ted Kremenekeb0a6c02011-12-02 01:30:14 +00001809 if (AsmString.isInvalid()) {
Richard Smithd67aea22012-03-06 03:21:47 +00001810 // Consume up to and including the closing paren.
1811 T.skipToEnd();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001812 return StmtError();
Ted Kremenekeb0a6c02011-12-02 01:30:14 +00001813 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001814
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001815 SmallVector<IdentifierInfo *, 4> Names;
Benjamin Kramerf0623432012-08-23 22:51:59 +00001816 ExprVector Constraints;
1817 ExprVector Exprs;
1818 ExprVector Clobbers;
Chris Lattner0116c472006-08-15 06:03:28 +00001819
Anders Carlsson19fe1162008-02-05 23:03:50 +00001820 if (Tok.is(tok::r_paren)) {
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001821 // We have a simple asm expression like 'asm("foo")'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001822 T.consumeClose();
Chad Rosierde70e0e2012-08-25 00:11:56 +00001823 return Actions.ActOnGCCAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1824 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1825 Constraints, Exprs, AsmString.take(),
1826 Clobbers, T.getCloseLocation());
Chris Lattner0116c472006-08-15 06:03:28 +00001827 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001828
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001829 // Parse Outputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001830 bool AteExtraColon = false;
1831 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1832 // In C++ mode, parse "::" like ": :".
1833 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001834 ConsumeToken();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001835
Chris Lattner15768502009-12-20 23:08:04 +00001836 if (!AteExtraColon &&
1837 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001838 return StmtError();
1839 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001840
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001841 unsigned NumOutputs = Names.size();
1842
1843 // Parse Inputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001844 if (AteExtraColon ||
1845 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1846 // In C++ mode, parse "::" like ": :".
1847 if (AteExtraColon)
1848 AteExtraColon = false;
1849 else {
1850 AteExtraColon = Tok.is(tok::coloncolon);
1851 ConsumeToken();
1852 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001853
Chris Lattner15768502009-12-20 23:08:04 +00001854 if (!AteExtraColon &&
1855 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001856 return StmtError();
1857 }
1858
1859 assert(Names.size() == Constraints.size() &&
1860 Constraints.size() == Exprs.size() &&
1861 "Input operand size mismatch!");
1862
1863 unsigned NumInputs = Names.size() - NumOutputs;
1864
1865 // Parse the clobbers, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001866 if (AteExtraColon || Tok.is(tok::colon)) {
1867 if (!AteExtraColon)
1868 ConsumeToken();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001869
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001870 // Parse the asm-string list for clobbers if present.
1871 if (Tok.isNot(tok::r_paren)) {
1872 while (1) {
John McCalldadc5752010-08-24 06:29:42 +00001873 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001874
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001875 if (Clobber.isInvalid())
1876 break;
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001877
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001878 Clobbers.push_back(Clobber.release());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001879
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001880 if (Tok.isNot(tok::comma)) break;
1881 ConsumeToken();
1882 }
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001883 }
1884 }
1885
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001886 T.consumeClose();
Chad Rosierde70e0e2012-08-25 00:11:56 +00001887 return Actions.ActOnGCCAsmStmt(AsmLoc, false, isVolatile, NumOutputs,
1888 NumInputs, Names.data(), Constraints, Exprs,
1889 AsmString.take(), Clobbers,
1890 T.getCloseLocation());
Chris Lattner0116c472006-08-15 06:03:28 +00001891}
1892
1893/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001894/// asm-statement, assuming the leading ':' token was eaten.
Chris Lattner0116c472006-08-15 06:03:28 +00001895///
1896/// [GNU] asm-operands:
1897/// asm-operand
1898/// asm-operands ',' asm-operand
1899///
1900/// [GNU] asm-operand:
1901/// asm-string-literal '(' expression ')'
1902/// '[' identifier ']' asm-string-literal '(' expression ')'
1903///
Daniel Dunbar70e7ead2009-10-18 20:26:27 +00001904//
1905// FIXME: Avoid unnecessary std::string trashing.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001906bool Parser::ParseAsmOperandsOpt(SmallVectorImpl<IdentifierInfo *> &Names,
Richard Trieu2bd04012011-09-09 02:00:50 +00001907 SmallVectorImpl<Expr *> &Constraints,
1908 SmallVectorImpl<Expr *> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001909 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001910 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001911 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001912
1913 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001914 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001915 if (Tok.is(tok::l_square)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001916 BalancedDelimiterTracker T(*this, tok::l_square);
1917 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00001918
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001919 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001920 Diag(Tok, diag::err_expected_ident);
1921 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001922 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001923 }
Mike Stump11289f42009-09-09 15:08:12 +00001924
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001925 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001926 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001927
Anders Carlsson9a020f92010-01-30 22:25:16 +00001928 Names.push_back(II);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001929 T.consumeClose();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001930 } else
Anders Carlsson9a020f92010-01-30 22:25:16 +00001931 Names.push_back(0);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001932
John McCalldadc5752010-08-24 06:29:42 +00001933 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001934 if (Constraint.isInvalid()) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001935 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001936 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001937 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001938 Constraints.push_back(Constraint.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001939
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001940 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001941 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00001942 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001943 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001944 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001945
Chris Lattner0116c472006-08-15 06:03:28 +00001946 // Read the parenthesized expression.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001947 BalancedDelimiterTracker T(*this, tok::l_paren);
1948 T.consumeOpen();
John McCalldadc5752010-08-24 06:29:42 +00001949 ExprResult Res(ParseExpression());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001950 T.consumeClose();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001951 if (Res.isInvalid()) {
Chris Lattner0116c472006-08-15 06:03:28 +00001952 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001953 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001954 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001955 Exprs.push_back(Res.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001956 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001957 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001958 ConsumeToken();
1959 }
1960}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001961
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001962Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001963 assert(Tok.is(tok::l_brace));
1964 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001965
Erik Verbruggen6e922512012-04-12 10:11:59 +00001966 if (SkipFunctionBodies && trySkippingFunctionBody()) {
1967 BodyScope.Exit();
1968 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001969 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001970
John McCallfaf5fb42010-08-26 23:41:50 +00001971 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1972 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001973
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001974 // Do not enter a scope for the brace, as the arguments are in the same scope
1975 // (the function body) as the body itself. Instead, just read the statement
1976 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00001977 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001978
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001979 // If the function body could not be parsed, make a bogus compoundstmt.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001980 if (FnBody.isInvalid()) {
1981 Sema::CompoundScopeRAII CompoundScope(Actions);
Mike Stump11289f42009-09-09 15:08:12 +00001982 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001983 MultiStmtArg(), false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001984 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001985
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001986 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00001987 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001988}
Sebastian Redlb219c902008-12-21 16:41:36 +00001989
Sebastian Redla7b98a72009-04-26 20:35:05 +00001990/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1991///
1992/// function-try-block:
1993/// 'try' ctor-initializer[opt] compound-statement handler-seq
1994///
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001995Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00001996 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1997 SourceLocation TryLoc = ConsumeToken();
1998
John McCallfaf5fb42010-08-26 23:41:50 +00001999 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
2000 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00002001
2002 // Constructor initializer list?
2003 if (Tok.is(tok::colon))
2004 ParseConstructorInitializer(Decl);
Douglas Gregor5ca153f2011-09-07 20:36:12 +00002005 else
2006 Actions.ActOnDefaultCtorInitializers(Decl);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00002007
Erik Verbruggen6e922512012-04-12 10:11:59 +00002008 if (SkipFunctionBodies && trySkippingFunctionBody()) {
2009 BodyScope.Exit();
2010 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002011 }
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002012
Sebastian Redld98ecd62009-04-26 21:08:36 +00002013 SourceLocation LBraceLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +00002014 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redla7b98a72009-04-26 20:35:05 +00002015 // If we failed to parse the try-catch, we just give the function an empty
2016 // compound statement as the body.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002017 if (FnBody.isInvalid()) {
2018 Sema::CompoundScopeRAII CompoundScope(Actions);
Sebastian Redld98ecd62009-04-26 21:08:36 +00002019 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00002020 MultiStmtArg(), false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002021 }
Sebastian Redla7b98a72009-04-26 20:35:05 +00002022
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002023 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00002024 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redla7b98a72009-04-26 20:35:05 +00002025}
2026
Erik Verbruggen6e922512012-04-12 10:11:59 +00002027bool Parser::trySkippingFunctionBody() {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002028 assert(Tok.is(tok::l_brace));
Erik Verbruggen6e922512012-04-12 10:11:59 +00002029 assert(SkipFunctionBodies &&
2030 "Should only be called when SkipFunctionBodies is enabled");
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002031
2032 // We're in code-completion mode. Skip parsing for all function bodies unless
2033 // the body contains the code-completion point.
2034 TentativeParsingAction PA(*this);
2035 ConsumeBrace();
2036 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
Erik Verbruggen6e922512012-04-12 10:11:59 +00002037 /*StopAtCodeCompletion=*/PP.isCodeCompletionEnabled())) {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002038 PA.Commit();
2039 return true;
2040 }
2041
2042 PA.Revert();
2043 return false;
2044}
2045
Sebastian Redlb219c902008-12-21 16:41:36 +00002046/// ParseCXXTryBlock - Parse a C++ try-block.
2047///
2048/// try-block:
2049/// 'try' compound-statement handler-seq
2050///
Richard Smithc202b282012-04-14 00:33:13 +00002051StmtResult Parser::ParseCXXTryBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00002052 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2053
2054 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00002055 return ParseCXXTryBlockCommon(TryLoc);
2056}
2057
2058/// ParseCXXTryBlockCommon - Parse the common part of try-block and
2059/// function-try-block.
2060///
2061/// try-block:
2062/// 'try' compound-statement handler-seq
2063///
2064/// function-try-block:
2065/// 'try' ctor-initializer[opt] compound-statement handler-seq
2066///
2067/// handler-seq:
2068/// handler handler-seq[opt]
2069///
John Wiegley1c0675e2011-04-28 01:08:34 +00002070/// [Borland] try-block:
2071/// 'try' compound-statement seh-except-block
2072/// 'try' compound-statment seh-finally-block
2073///
John McCalldadc5752010-08-24 06:29:42 +00002074StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redlb219c902008-12-21 16:41:36 +00002075 if (Tok.isNot(tok::l_brace))
2076 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002077 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
Richard Smithc202b282012-04-14 00:33:13 +00002078
2079 StmtResult TryBlock(ParseCompoundStatement(/*isStmtExpr=*/false,
Douglas Gregor53e191ed2011-07-06 22:04:06 +00002080 Scope::DeclScope|Scope::TryScope));
Sebastian Redlb219c902008-12-21 16:41:36 +00002081 if (TryBlock.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002082 return TryBlock;
Sebastian Redlb219c902008-12-21 16:41:36 +00002083
John Wiegley1c0675e2011-04-28 01:08:34 +00002084 // Borland allows SEH-handlers with 'try'
Chad Rosier67055f52012-07-10 21:35:27 +00002085
Richard Smithc202b282012-04-14 00:33:13 +00002086 if ((Tok.is(tok::identifier) &&
2087 Tok.getIdentifierInfo() == getSEHExceptKeyword()) ||
2088 Tok.is(tok::kw___finally)) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002089 // TODO: Factor into common return ParseSEHHandlerCommon(...)
2090 StmtResult Handler;
Douglas Gregor60060d62011-10-21 03:57:52 +00002091 if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002092 SourceLocation Loc = ConsumeToken();
2093 Handler = ParseSEHExceptBlock(Loc);
2094 }
2095 else {
2096 SourceLocation Loc = ConsumeToken();
2097 Handler = ParseSEHFinallyBlock(Loc);
2098 }
2099 if(Handler.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002100 return Handler;
John McCall53fa7142010-12-24 02:08:15 +00002101
John Wiegley1c0675e2011-04-28 01:08:34 +00002102 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
2103 TryLoc,
2104 TryBlock.take(),
2105 Handler.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00002106 }
John Wiegley1c0675e2011-04-28 01:08:34 +00002107 else {
Benjamin Kramerf0623432012-08-23 22:51:59 +00002108 StmtVector Handlers;
Richard Smithc202b282012-04-14 00:33:13 +00002109 ParsedAttributesWithRange attrs(AttrFactory);
John Wiegley1c0675e2011-04-28 01:08:34 +00002110 MaybeParseCXX0XAttributes(attrs);
2111 ProhibitAttributes(attrs);
Sebastian Redlb219c902008-12-21 16:41:36 +00002112
John Wiegley1c0675e2011-04-28 01:08:34 +00002113 if (Tok.isNot(tok::kw_catch))
2114 return StmtError(Diag(Tok, diag::err_expected_catch));
2115 while (Tok.is(tok::kw_catch)) {
2116 StmtResult Handler(ParseCXXCatchBlock());
2117 if (!Handler.isInvalid())
2118 Handlers.push_back(Handler.release());
2119 }
2120 // Don't bother creating the full statement if we don't have any usable
2121 // handlers.
2122 if (Handlers.empty())
2123 return StmtError();
2124
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002125 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(),Handlers);
John Wiegley1c0675e2011-04-28 01:08:34 +00002126 }
Sebastian Redlb219c902008-12-21 16:41:36 +00002127}
2128
2129/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
2130///
2131/// handler:
2132/// 'catch' '(' exception-declaration ')' compound-statement
2133///
2134/// exception-declaration:
2135/// type-specifier-seq declarator
2136/// type-specifier-seq abstract-declarator
2137/// type-specifier-seq
2138/// '...'
2139///
John McCalldadc5752010-08-24 06:29:42 +00002140StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00002141 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
2142
2143 SourceLocation CatchLoc = ConsumeToken();
2144
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002145 BalancedDelimiterTracker T(*this, tok::l_paren);
2146 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redlb219c902008-12-21 16:41:36 +00002147 return StmtError();
2148
2149 // C++ 3.3.2p3:
2150 // The name in a catch exception-declaration is local to the handler and
2151 // shall not be redeclared in the outermost block of the handler.
2152 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
2153
2154 // exception-declaration is equivalent to '...' or a parameter-declaration
2155 // without default arguments.
John McCall48871652010-08-21 09:40:31 +00002156 Decl *ExceptionDecl = 0;
Sebastian Redlb219c902008-12-21 16:41:36 +00002157 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00002158 DeclSpec DS(AttrFactory);
Sebastian Redl54c04d42008-12-22 19:15:10 +00002159 if (ParseCXXTypeSpecifierSeq(DS))
2160 return StmtError();
Sebastian Redlb219c902008-12-21 16:41:36 +00002161 Declarator ExDecl(DS, Declarator::CXXCatchContext);
2162 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002163 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00002164 } else
2165 ConsumeToken();
2166
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002167 T.consumeClose();
2168 if (T.getCloseLocation().isInvalid())
Sebastian Redlb219c902008-12-21 16:41:36 +00002169 return StmtError();
2170
2171 if (Tok.isNot(tok::l_brace))
2172 return StmtError(Diag(Tok, diag::err_expected_lbrace));
2173
Alexis Hunt96d5c762009-11-21 08:43:09 +00002174 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
Richard Smithc202b282012-04-14 00:33:13 +00002175 StmtResult Block(ParseCompoundStatement());
Sebastian Redlb219c902008-12-21 16:41:36 +00002176 if (Block.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002177 return Block;
Sebastian Redlb219c902008-12-21 16:41:36 +00002178
John McCallb268a282010-08-23 23:25:46 +00002179 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00002180}
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002181
2182void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Douglas Gregor43edb322011-10-24 22:31:10 +00002183 IfExistsCondition Result;
Francois Picheta5b3fcb2011-05-07 17:30:27 +00002184 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002185 return;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00002186
Douglas Gregor43edb322011-10-24 22:31:10 +00002187 // Handle dependent statements by parsing the braces as a compound statement.
2188 // This is not the same behavior as Visual C++, which don't treat this as a
2189 // compound statement, but for Clang's type checking we can't have anything
2190 // inside these braces escaping to the surrounding code.
2191 if (Result.Behavior == IEB_Dependent) {
2192 if (!Tok.is(tok::l_brace)) {
2193 Diag(Tok, diag::err_expected_lbrace);
Richard Smithc202b282012-04-14 00:33:13 +00002194 return;
Douglas Gregor43edb322011-10-24 22:31:10 +00002195 }
Richard Smithc202b282012-04-14 00:33:13 +00002196
2197 StmtResult Compound = ParseCompoundStatement();
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002198 if (Compound.isInvalid())
2199 return;
Richard Smithc202b282012-04-14 00:33:13 +00002200
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002201 StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc,
2202 Result.IsIfExists,
Richard Smithc202b282012-04-14 00:33:13 +00002203 Result.SS,
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002204 Result.Name,
2205 Compound.get());
2206 if (DepResult.isUsable())
2207 Stmts.push_back(DepResult.get());
Douglas Gregor43edb322011-10-24 22:31:10 +00002208 return;
2209 }
Richard Smithc202b282012-04-14 00:33:13 +00002210
Douglas Gregor43edb322011-10-24 22:31:10 +00002211 BalancedDelimiterTracker Braces(*this, tok::l_brace);
2212 if (Braces.consumeOpen()) {
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002213 Diag(Tok, diag::err_expected_lbrace);
2214 return;
2215 }
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002216
Douglas Gregor43edb322011-10-24 22:31:10 +00002217 switch (Result.Behavior) {
2218 case IEB_Parse:
2219 // Parse the statements below.
2220 break;
Chad Rosier67055f52012-07-10 21:35:27 +00002221
Douglas Gregor43edb322011-10-24 22:31:10 +00002222 case IEB_Dependent:
2223 llvm_unreachable("Dependent case handled above");
Chad Rosier67055f52012-07-10 21:35:27 +00002224
Douglas Gregor43edb322011-10-24 22:31:10 +00002225 case IEB_Skip:
2226 Braces.skipToEnd();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002227 return;
2228 }
2229
2230 // Condition is true, parse the statements.
2231 while (Tok.isNot(tok::r_brace)) {
2232 StmtResult R = ParseStatementOrDeclaration(Stmts, false);
2233 if (R.isUsable())
2234 Stmts.push_back(R.release());
2235 }
Douglas Gregor43edb322011-10-24 22:31:10 +00002236 Braces.consumeClose();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002237}