blob: 151167515ee7e0654b1ecc95bb8f11769e0622e3 [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 case tok::annot_pragma_opencl_extension:
288 ProhibitAttributes(Attrs);
289 HandlePragmaOpenCLExtension();
290 return StmtEmpty();
Sebastian Redlb219c902008-12-21 16:41:36 +0000291 }
292
Chris Lattner503fadc2006-08-10 05:45:44 +0000293 // If we reached this code, the statement must end in a semicolon.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000294 if (Tok.is(tok::semi)) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000295 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000296 } else if (!Res.isInvalid()) {
Chris Lattner8e3eed02009-06-14 00:23:56 +0000297 // If the result was valid, then we do want to diagnose this. Use
298 // ExpectAndConsume to emit the diagnostic, even though we know it won't
299 // succeed.
300 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner0046de12008-11-13 18:52:53 +0000301 // Skip until we see a } or ;, but don't eat it.
302 SkipUntil(tok::r_brace, true, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000303 }
Mike Stump11289f42009-09-09 15:08:12 +0000304
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000305 return Res;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000306}
307
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000308/// \brief Parse an expression statement.
Richard Smithc202b282012-04-14 00:33:13 +0000309StmtResult Parser::ParseExprStatement() {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000310 // If a case keyword is missing, this is where it should be inserted.
311 Token OldToken = Tok;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000312
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000313 // expression[opt] ';'
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000314 ExprResult Expr(ParseExpression());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000315 if (Expr.isInvalid()) {
316 // If the expression is invalid, skip ahead to the next semicolon or '}'.
317 // Not doing this opens us up to the possibility of infinite loops if
318 // ParseExpression does not consume any tokens.
319 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
320 if (Tok.is(tok::semi))
321 ConsumeToken();
322 return StmtError();
323 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000324
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000325 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
326 Actions.CheckCaseExpression(Expr.get())) {
327 // If a constant expression is followed by a colon inside a switch block,
328 // suggest a missing case keyword.
329 Diag(OldToken, diag::err_expected_case_before_expression)
330 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000331
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000332 // Recover parsing as a case statement.
Richard Smithc202b282012-04-14 00:33:13 +0000333 return ParseCaseStatement(/*MissingCase=*/true, Expr);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000334 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000335
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000336 // Otherwise, eat the semicolon.
337 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
338 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr.get()));
John Wiegley1c0675e2011-04-28 01:08:34 +0000339}
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000340
Richard Smithc202b282012-04-14 00:33:13 +0000341StmtResult Parser::ParseSEHTryBlock() {
John Wiegley1c0675e2011-04-28 01:08:34 +0000342 assert(Tok.is(tok::kw___try) && "Expected '__try'");
343 SourceLocation Loc = ConsumeToken();
344 return ParseSEHTryBlockCommon(Loc);
345}
346
347/// ParseSEHTryBlockCommon
348///
349/// seh-try-block:
350/// '__try' compound-statement seh-handler
351///
352/// seh-handler:
353/// seh-except-block
354/// seh-finally-block
355///
356StmtResult Parser::ParseSEHTryBlockCommon(SourceLocation TryLoc) {
357 if(Tok.isNot(tok::l_brace))
358 return StmtError(Diag(Tok,diag::err_expected_lbrace));
359
Joao Matos566359c2012-09-04 17:49:35 +0000360 StmtResult TryBlock(ParseCompoundStatement());
John Wiegley1c0675e2011-04-28 01:08:34 +0000361 if(TryBlock.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000362 return TryBlock;
John Wiegley1c0675e2011-04-28 01:08:34 +0000363
364 StmtResult Handler;
Richard Smithc202b282012-04-14 00:33:13 +0000365 if (Tok.is(tok::identifier) &&
Douglas Gregor60060d62011-10-21 03:57:52 +0000366 Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +0000367 SourceLocation Loc = ConsumeToken();
368 Handler = ParseSEHExceptBlock(Loc);
369 } else if (Tok.is(tok::kw___finally)) {
370 SourceLocation Loc = ConsumeToken();
371 Handler = ParseSEHFinallyBlock(Loc);
372 } else {
373 return StmtError(Diag(Tok,diag::err_seh_expected_handler));
374 }
375
376 if(Handler.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000377 return Handler;
John Wiegley1c0675e2011-04-28 01:08:34 +0000378
379 return Actions.ActOnSEHTryBlock(false /* IsCXXTry */,
380 TryLoc,
381 TryBlock.take(),
382 Handler.take());
383}
384
385/// ParseSEHExceptBlock - Handle __except
386///
387/// seh-except-block:
388/// '__except' '(' seh-filter-expression ')' compound-statement
389///
390StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) {
391 PoisonIdentifierRAIIObject raii(Ident__exception_code, false),
392 raii2(Ident___exception_code, false),
393 raii3(Ident_GetExceptionCode, false);
394
395 if(ExpectAndConsume(tok::l_paren,diag::err_expected_lparen))
396 return StmtError();
397
398 ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope);
399
David Blaikiebbafb8a2012-03-11 07:00:24 +0000400 if (getLangOpts().Borland) {
Francois Pichetbfaf4772011-04-28 03:14:31 +0000401 Ident__exception_info->setIsPoisoned(false);
402 Ident___exception_info->setIsPoisoned(false);
403 Ident_GetExceptionInfo->setIsPoisoned(false);
404 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000405 ExprResult FilterExpr(ParseExpression());
Francois Pichetbfaf4772011-04-28 03:14:31 +0000406
David Blaikiebbafb8a2012-03-11 07:00:24 +0000407 if (getLangOpts().Borland) {
Francois Pichetbfaf4772011-04-28 03:14:31 +0000408 Ident__exception_info->setIsPoisoned(true);
409 Ident___exception_info->setIsPoisoned(true);
410 Ident_GetExceptionInfo->setIsPoisoned(true);
411 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000412
413 if(FilterExpr.isInvalid())
414 return StmtError();
415
416 if(ExpectAndConsume(tok::r_paren,diag::err_expected_rparen))
417 return StmtError();
418
Richard Smithc202b282012-04-14 00:33:13 +0000419 StmtResult Block(ParseCompoundStatement());
John Wiegley1c0675e2011-04-28 01:08:34 +0000420
421 if(Block.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000422 return Block;
John Wiegley1c0675e2011-04-28 01:08:34 +0000423
424 return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.take(), Block.take());
425}
426
427/// ParseSEHFinallyBlock - Handle __finally
428///
429/// seh-finally-block:
430/// '__finally' compound-statement
431///
432StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyBlock) {
433 PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
434 raii2(Ident___abnormal_termination, false),
435 raii3(Ident_AbnormalTermination, false);
436
Richard Smithc202b282012-04-14 00:33:13 +0000437 StmtResult Block(ParseCompoundStatement());
John Wiegley1c0675e2011-04-28 01:08:34 +0000438 if(Block.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000439 return Block;
John Wiegley1c0675e2011-04-28 01:08:34 +0000440
441 return Actions.ActOnSEHFinallyBlock(FinallyBlock,Block.take());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000442}
443
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000444/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000445///
446/// labeled-statement:
447/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000448/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000449///
Richard Smithc202b282012-04-14 00:33:13 +0000450StmtResult Parser::ParseLabeledStatement(ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000451 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
452 "Not an identifier!");
453
454 Token IdentTok = Tok; // Save the whole token.
455 ConsumeToken(); // eat the identifier.
456
457 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000458
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000459 // identifier ':' statement
460 SourceLocation ColonLoc = ConsumeToken();
461
Richard Smithc202b282012-04-14 00:33:13 +0000462 // Read label attributes, if present. attrs will contain both C++11 and GNU
463 // attributes (if present) after this point.
John McCall53fa7142010-12-24 02:08:15 +0000464 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000465
John McCalldadc5752010-08-24 06:29:42 +0000466 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000467
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000468 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000469 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000470 SubStmt = Actions.ActOnNullStmt(ColonLoc);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000471
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000472 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
473 IdentTok.getLocation());
Richard Smithc202b282012-04-14 00:33:13 +0000474 if (AttributeList *Attrs = attrs.getList()) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000475 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
Richard Smithc202b282012-04-14 00:33:13 +0000476 attrs.clear();
477 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000478
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000479 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
480 SubStmt.get());
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000481}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000482
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000483/// ParseCaseStatement
484/// labeled-statement:
485/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000486/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000487///
Richard Smithc202b282012-04-14 00:33:13 +0000488StmtResult Parser::ParseCaseStatement(bool MissingCase, ExprResult Expr) {
Richard Smithd4257d82011-04-21 22:48:40 +0000489 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Mike Stump11289f42009-09-09 15:08:12 +0000490
Chris Lattner34a22092009-03-04 04:23:07 +0000491 // It is very very common for code to contain many case statements recursively
492 // nested, as in (but usually without indentation):
493 // case 1:
494 // case 2:
495 // case 3:
496 // case 4:
497 // case 5: etc.
498 //
499 // Parsing this naively works, but is both inefficient and can cause us to run
500 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner2b19a6582009-03-04 18:24:58 +0000501 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner34a22092009-03-04 04:23:07 +0000502 // but all the grossness is constrained to ParseCaseStatement (and some
503 // wierdness in the actions), so this is just local grossness :).
Mike Stump11289f42009-09-09 15:08:12 +0000504
Chris Lattner34a22092009-03-04 04:23:07 +0000505 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
506 // example above.
John McCalldadc5752010-08-24 06:29:42 +0000507 StmtResult TopLevelCase(true);
Mike Stump11289f42009-09-09 15:08:12 +0000508
Chris Lattner34a22092009-03-04 04:23:07 +0000509 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
510 // gets updated each time a new case is parsed, and whose body is unset so
511 // far. When parsing 'case 4', this is the 'case 3' node.
Richard Trieu3481fcd2011-09-09 02:16:15 +0000512 Stmt *DeepestParsedCaseStmt = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000513
Chris Lattner34a22092009-03-04 04:23:07 +0000514 // While we have case statements, eat and stack them.
David Majnemer0ac67fa2011-06-13 05:50:12 +0000515 SourceLocation ColonLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000516 do {
Richard Trieu2c850c02011-04-21 21:44:26 +0000517 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
518 ConsumeToken(); // eat the 'case'.
Mike Stump11289f42009-09-09 15:08:12 +0000519
Douglas Gregord328d572009-09-21 18:10:23 +0000520 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000521 Actions.CodeCompleteCase(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000522 cutOffParsing();
523 return StmtError();
Douglas Gregord328d572009-09-21 18:10:23 +0000524 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000525
Chris Lattner125c0ee2009-12-10 00:38:54 +0000526 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
527 /// Disable this form of error recovery while we're parsing the case
528 /// expression.
529 ColonProtectionRAIIObject ColonProtection(*this);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000530
Richard Trieu2c850c02011-04-21 21:44:26 +0000531 ExprResult LHS(MissingCase ? Expr : ParseConstantExpression());
532 MissingCase = false;
Chris Lattner34a22092009-03-04 04:23:07 +0000533 if (LHS.isInvalid()) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000534 SkipUntil(tok::colon);
Sebastian Redl042ad952008-12-11 19:30:53 +0000535 return StmtError();
Chris Lattner476c3ad2006-08-13 22:09:58 +0000536 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000537
Chris Lattner34a22092009-03-04 04:23:07 +0000538 // GNU case range extension.
539 SourceLocation DotDotDotLoc;
John McCalldadc5752010-08-24 06:29:42 +0000540 ExprResult RHS;
Chris Lattner34a22092009-03-04 04:23:07 +0000541 if (Tok.is(tok::ellipsis)) {
542 Diag(Tok, diag::ext_gnu_case_range);
543 DotDotDotLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000544
Chris Lattner34a22092009-03-04 04:23:07 +0000545 RHS = ParseConstantExpression();
546 if (RHS.isInvalid()) {
547 SkipUntil(tok::colon);
548 return StmtError();
549 }
550 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000551
Chris Lattner125c0ee2009-12-10 00:38:54 +0000552 ColonProtection.restore();
Sebastian Redl042ad952008-12-11 19:30:53 +0000553
John McCall0140bfe2011-01-22 09:28:32 +0000554 if (Tok.is(tok::colon)) {
555 ColonLoc = ConsumeToken();
556
557 // Treat "case blah;" as a typo for "case blah:".
558 } else if (Tok.is(tok::semi)) {
559 ColonLoc = ConsumeToken();
560 Diag(ColonLoc, diag::err_expected_colon_after) << "'case'"
561 << FixItHint::CreateReplacement(ColonLoc, ":");
562 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000563 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
564 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'"
565 << FixItHint::CreateInsertion(ExpectedLoc, ":");
566 ColonLoc = ExpectedLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000567 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000568
John McCalldadc5752010-08-24 06:29:42 +0000569 StmtResult Case =
John McCallb268a282010-08-23 23:25:46 +0000570 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
571 RHS.get(), ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000572
Chris Lattner34a22092009-03-04 04:23:07 +0000573 // If we had a sema error parsing this case, then just ignore it and
574 // continue parsing the sub-stmt.
575 if (Case.isInvalid()) {
576 if (TopLevelCase.isInvalid()) // No parsed case stmts.
577 return ParseStatement();
578 // Otherwise, just don't add it as a nested case.
579 } else {
580 // If this is the first case statement we parsed, it becomes TopLevelCase.
581 // Otherwise we link it into the current chain.
John McCall37ad5512010-08-23 06:44:23 +0000582 Stmt *NextDeepest = Case.get();
Chris Lattner34a22092009-03-04 04:23:07 +0000583 if (TopLevelCase.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000584 TopLevelCase = Case;
Chris Lattner34a22092009-03-04 04:23:07 +0000585 else
John McCallb268a282010-08-23 23:25:46 +0000586 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner34a22092009-03-04 04:23:07 +0000587 DeepestParsedCaseStmt = NextDeepest;
588 }
Mike Stump11289f42009-09-09 15:08:12 +0000589
Chris Lattner34a22092009-03-04 04:23:07 +0000590 // Handle all case statements.
591 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000592
Chris Lattner34a22092009-03-04 04:23:07 +0000593 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump11289f42009-09-09 15:08:12 +0000594
Chris Lattner34a22092009-03-04 04:23:07 +0000595 // If we found a non-case statement, start by parsing it.
John McCalldadc5752010-08-24 06:29:42 +0000596 StmtResult SubStmt;
Mike Stump11289f42009-09-09 15:08:12 +0000597
Chris Lattner34a22092009-03-04 04:23:07 +0000598 if (Tok.isNot(tok::r_brace)) {
599 SubStmt = ParseStatement();
600 } else {
601 // Nicely diagnose the common error "switch (X) { case 4: }", which is
602 // not valid.
David Majnemerc6a99872011-06-14 15:24:38 +0000603 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
Richard Smith1002d102012-02-17 01:35:32 +0000604 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
605 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
Chris Lattner34a22092009-03-04 04:23:07 +0000606 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000607 }
Mike Stump11289f42009-09-09 15:08:12 +0000608
Chris Lattner34a22092009-03-04 04:23:07 +0000609 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000610 if (SubStmt.isInvalid())
Chris Lattner34a22092009-03-04 04:23:07 +0000611 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000612
Chris Lattner34a22092009-03-04 04:23:07 +0000613 // Install the body into the most deeply-nested case.
John McCallb268a282010-08-23 23:25:46 +0000614 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000615
Chris Lattner34a22092009-03-04 04:23:07 +0000616 // Return the top level parsed statement tree.
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000617 return TopLevelCase;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000618}
619
620/// ParseDefaultStatement
621/// labeled-statement:
622/// 'default' ':' statement
623/// Note that this does not parse the 'statement' at the end.
624///
Richard Smithc202b282012-04-14 00:33:13 +0000625StmtResult Parser::ParseDefaultStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000626 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000627 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000628
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000629 SourceLocation ColonLoc;
John McCall0140bfe2011-01-22 09:28:32 +0000630 if (Tok.is(tok::colon)) {
631 ColonLoc = ConsumeToken();
632
633 // Treat "default;" as a typo for "default:".
634 } else if (Tok.is(tok::semi)) {
635 ColonLoc = ConsumeToken();
636 Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
637 << FixItHint::CreateReplacement(ColonLoc, ":");
638 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000639 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
640 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
641 << FixItHint::CreateInsertion(ExpectedLoc, ":");
642 ColonLoc = ExpectedLoc;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000643 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000644
Richard Smith1002d102012-02-17 01:35:32 +0000645 StmtResult SubStmt;
646
647 if (Tok.isNot(tok::r_brace)) {
648 SubStmt = ParseStatement();
649 } else {
650 // Diagnose the common error "switch (X) {... default: }", which is
651 // not valid.
David Majnemerc6a99872011-06-14 15:24:38 +0000652 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
Richard Smith1002d102012-02-17 01:35:32 +0000653 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
654 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
655 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000656 }
657
Richard Smith1002d102012-02-17 01:35:32 +0000658 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000659 if (SubStmt.isInvalid())
Richard Smith1002d102012-02-17 01:35:32 +0000660 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl042ad952008-12-11 19:30:53 +0000661
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000662 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000663 SubStmt.get(), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000664}
665
Richard Smithc202b282012-04-14 00:33:13 +0000666StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
667 return ParseCompoundStatement(isStmtExpr, Scope::DeclScope);
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000668}
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000669
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000670/// ParseCompoundStatement - Parse a "{}" block.
671///
672/// compound-statement: [C99 6.8.2]
673/// { block-item-list[opt] }
674/// [GNU] { label-declarations block-item-list } [TODO]
675///
676/// block-item-list:
677/// block-item
678/// block-item-list block-item
679///
680/// block-item:
681/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000682/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000683/// statement
684/// [OMP] openmp-directive [TODO]
685///
686/// [GNU] label-declarations:
687/// [GNU] label-declaration
688/// [GNU] label-declarations label-declaration
689///
690/// [GNU] label-declaration:
691/// [GNU] '__label__' identifier-list ';'
692///
693/// [OMP] openmp-directive: [TODO]
694/// [OMP] barrier-directive
695/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000696///
Richard Smithc202b282012-04-14 00:33:13 +0000697StmtResult Parser::ParseCompoundStatement(bool isStmtExpr,
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000698 unsigned ScopeFlags) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000699 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000700
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000701 // Enter a scope to hold everything within the compound stmt. Compound
702 // statements can always hold declarations.
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000703 ParseScope CompoundScope(this, ScopeFlags);
Chris Lattnerf2978802007-01-21 06:52:16 +0000704
705 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000706 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000707}
708
Chris Lattnerf2978802007-01-21 06:52:16 +0000709/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000710/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000711/// consume the '}' at the end of the block. It does not manipulate the scope
712/// stack.
John McCalldadc5752010-08-24 06:29:42 +0000713StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000714 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000715 Tok.getLocation(),
716 "in compound statement ('{}')");
Lang Hames5de91cc2012-10-02 04:45:10 +0000717
718 // Record the state of the FP_CONTRACT pragma, restore on leaving the
719 // compound statement.
720 Sema::FPContractStateRAII SaveFPContractState(Actions);
721
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000722 InMessageExpressionRAIIObject InMessage(*this, false);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000723 BalancedDelimiterTracker T(*this, tok::l_brace);
724 if (T.consumeOpen())
725 return StmtError();
Chris Lattnerf2978802007-01-21 06:52:16 +0000726
Dmitri Gribenko800ddf32012-02-14 22:14:32 +0000727 Sema::CompoundScopeRAII CompoundScope(Actions);
728
Benjamin Kramerf0623432012-08-23 22:51:59 +0000729 StmtVector Stmts;
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000730
Lang Hamesa930e712012-10-21 01:10:01 +0000731 // Parse FP_CONTRACT if present.
732 if (Tok.is(tok::annot_pragma_fp_contract))
733 HandlePragmaFPContract();
734
Chris Lattner43e7f312011-02-18 02:08:43 +0000735 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
736 // only allowed at the start of a compound stmt regardless of the language.
737 while (Tok.is(tok::kw___label__)) {
738 SourceLocation LabelLoc = ConsumeToken();
739 Diag(LabelLoc, diag::ext_gnu_local_label);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000740
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000741 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner43e7f312011-02-18 02:08:43 +0000742 while (1) {
743 if (Tok.isNot(tok::identifier)) {
744 Diag(Tok, diag::err_expected_ident);
745 break;
746 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000747
Chris Lattner43e7f312011-02-18 02:08:43 +0000748 IdentifierInfo *II = Tok.getIdentifierInfo();
749 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000750 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000751
Chris Lattner43e7f312011-02-18 02:08:43 +0000752 if (!Tok.is(tok::comma))
753 break;
754 ConsumeToken();
755 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000756
John McCall084e83d2011-03-24 11:26:52 +0000757 DeclSpec DS(AttrFactory);
Chris Lattner43e7f312011-02-18 02:08:43 +0000758 DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
759 DeclsInGroup.data(), DeclsInGroup.size());
760 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000761
Chris Lattner02f1b612012-04-28 16:12:17 +0000762 ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
Chris Lattner43e7f312011-02-18 02:08:43 +0000763 if (R.isUsable())
764 Stmts.push_back(R.release());
765 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000766
Chris Lattner43e7f312011-02-18 02:08:43 +0000767 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000768 if (Tok.is(tok::annot_pragma_unused)) {
769 HandlePragmaUnused();
770 continue;
771 }
772
David Blaikiebbafb8a2012-03-11 07:00:24 +0000773 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet4a7de3e2011-05-06 20:48:22 +0000774 Tok.is(tok::kw___if_not_exists))) {
775 ParseMicrosoftIfExistsStatement(Stmts);
776 continue;
777 }
778
John McCalldadc5752010-08-24 06:29:42 +0000779 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000780 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000781 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000782 } else {
783 // __extension__ can start declarations and it can also be a unary
784 // operator for expressions. Consume multiple __extension__ markers here
785 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000786 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000787 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000788 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000789 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +0000790
John McCall084e83d2011-03-24 11:26:52 +0000791 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith7bdcc4a2012-04-10 01:32:12 +0000792 MaybeParseCXX0XAttributes(attrs, 0, /*MightBeObjCMessageSend*/ true);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000793
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000794 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000795 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +0000796 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +0000797 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +0000798 ExtensionRAIIObject O(Diags);
799
Chris Lattner49836b42009-04-02 04:16:50 +0000800 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000801 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
802 Declarator::BlockContext, DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000803 attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000804 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000805 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000806 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +0000807 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +0000808
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000809 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000810 SkipUntil(tok::semi);
811 continue;
812 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000813
Alexis Hunt96d5c762009-11-21 08:43:09 +0000814 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +0000815 // Eat the semicolon at the end of stmt and convert the expr into a
816 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000817 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +0000818 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000819 }
820 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000821
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000822 if (R.isUsable())
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000823 Stmts.push_back(R.release());
Chris Lattner30f910e2006-10-16 05:52:41 +0000824 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000825
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +0000826 SourceLocation CloseLoc = Tok.getLocation();
827
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000828 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000829 if (Tok.isNot(tok::r_brace)) {
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000830 Diag(Tok, diag::err_expected_rbrace);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000831 Diag(T.getOpenLocation(), diag::note_matching) << "{";
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +0000832 // Recover by creating a compound statement with what we parsed so far,
833 // instead of dropping everything and returning StmtError();
834 } else {
835 if (!T.consumeClose())
836 CloseLoc = T.getCloseLocation();
Chris Lattner30f910e2006-10-16 05:52:41 +0000837 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000838
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +0000839 return Actions.ActOnCompoundStmt(T.getOpenLocation(), CloseLoc,
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000840 Stmts, isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000841}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000842
Chris Lattnerc0081db2008-12-12 06:31:07 +0000843/// ParseParenExprOrCondition:
844/// [C ] '(' expression ')'
Chris Lattner10da53c2008-12-12 06:35:28 +0000845/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattnerc0081db2008-12-12 06:31:07 +0000846///
847/// This function parses and performs error recovery on the specified condition
848/// or expression (depending on whether we're in C++ or C mode). This function
849/// goes out of its way to recover well. It returns true if there was a parser
850/// error (the right paren couldn't be found), which indicates that the caller
851/// should try to recover harder. It returns false if the condition is
852/// successfully parsed. Note that a successful parse can still have semantic
853/// errors in the condition.
John McCalldadc5752010-08-24 06:29:42 +0000854bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCall48871652010-08-21 09:40:31 +0000855 Decl *&DeclResult,
Douglas Gregore60e41a2010-05-06 17:25:47 +0000856 SourceLocation Loc,
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000857 bool ConvertToBoolean) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000858 BalancedDelimiterTracker T(*this, tok::l_paren);
859 T.consumeOpen();
860
David Blaikiebbafb8a2012-03-11 07:00:24 +0000861 if (getLangOpts().CPlusPlus)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000862 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000863 else {
864 ExprResult = ParseExpression();
John McCall48871652010-08-21 09:40:31 +0000865 DeclResult = 0;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000866
Douglas Gregore60e41a2010-05-06 17:25:47 +0000867 // If required, convert to a boolean value.
868 if (!ExprResult.isInvalid() && ConvertToBoolean)
869 ExprResult
John McCallb268a282010-08-23 23:25:46 +0000870 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000871 }
Mike Stump11289f42009-09-09 15:08:12 +0000872
Chris Lattnerc0081db2008-12-12 06:31:07 +0000873 // If the parser was confused by the condition and we don't have a ')', try to
874 // recover by skipping ahead to a semi and bailing out. If condexp is
875 // semantically invalid but we have well formed code, keep going.
John McCall48871652010-08-21 09:40:31 +0000876 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000877 SkipUntil(tok::semi);
878 // Skipping may have stopped if it found the containing ')'. If so, we can
879 // continue parsing the if statement.
880 if (Tok.isNot(tok::r_paren))
881 return true;
882 }
Mike Stump11289f42009-09-09 15:08:12 +0000883
Chris Lattnerc0081db2008-12-12 06:31:07 +0000884 // Otherwise the condition is valid or the rparen is present.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000885 T.consumeClose();
Chad Rosier67055f52012-07-10 21:35:27 +0000886
Chris Lattner70d44982012-04-28 16:24:20 +0000887 // Check for extraneous ')'s to catch things like "if (foo())) {". We know
888 // that all callers are looking for a statement after the condition, so ")"
889 // isn't valid.
890 while (Tok.is(tok::r_paren)) {
891 Diag(Tok, diag::err_extraneous_rparen_in_condition)
892 << FixItHint::CreateRemoval(Tok.getLocation());
893 ConsumeParen();
894 }
Chad Rosier67055f52012-07-10 21:35:27 +0000895
Chris Lattnerc0081db2008-12-12 06:31:07 +0000896 return false;
897}
898
899
Chris Lattnerc951dae2006-08-10 04:23:57 +0000900/// ParseIfStatement
901/// if-statement: [C99 6.8.4.1]
902/// 'if' '(' expression ')' statement
903/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000904/// [C++] 'if' '(' condition ')' statement
905/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000906///
Richard Smithc202b282012-04-14 00:33:13 +0000907StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000908 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000909 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000910
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000911 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000912 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +0000913 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +0000914 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000915 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000916
David Blaikiebbafb8a2012-03-11 07:00:24 +0000917 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000918
Chris Lattner2dd1b722007-08-26 23:08:06 +0000919 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
920 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000921 //
922 // C++ 6.4p3:
923 // A name introduced by a declaration in a condition is in scope from its
924 // point of declaration until the end of the substatements controlled by the
925 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000926 // C++ 3.3.2p4:
927 // Names declared in the for-init-statement, and in the condition of if,
928 // while, for, and switch statements are local to the if, while, for, or
929 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000930 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000931 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000932
Chris Lattnerc951dae2006-08-10 04:23:57 +0000933 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000934 ExprResult CondExp;
John McCall48871652010-08-21 09:40:31 +0000935 Decl *CondVar = 0;
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000936 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000937 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000938
David Blaikiea5696df2012-05-16 04:20:04 +0000939 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get(), IfLoc));
Mike Stump11289f42009-09-09 15:08:12 +0000940
Chris Lattner8fb26252007-08-22 05:28:50 +0000941 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000942 // there is no compound stmt. C90 does not have this clause. We only do this
943 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000944 //
945 // C++ 6.4p1:
946 // The substatement in a selection-statement (each substatement, in the else
947 // form of the if statement) implicitly defines a local scope.
948 //
949 // For C++ we create a scope for the condition and a new scope for
950 // substatements because:
951 // -When the 'then' scope exits, we want the condition declaration to still be
952 // active for the 'else' scope too.
953 // -Sema will detect name clashes by considering declarations of a
954 // 'ControlScope' as part of its direct subscope.
955 // -If we wanted the condition and substatement to be in the same scope, we
956 // would have to notify ParseStatement not to create a new scope. It's
957 // simpler to let it create a new scope.
958 //
Mike Stump11289f42009-09-09 15:08:12 +0000959 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000960 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000961
Chris Lattner5c5808a2007-10-29 05:08:52 +0000962 // Read the 'then' stmt.
963 SourceLocation ThenStmtLoc = Tok.getLocation();
Nico Weber3cef1082011-12-22 23:26:17 +0000964
965 SourceLocation InnerStatementTrailingElseLoc;
966 StmtResult ThenStmt(ParseStatement(&InnerStatementTrailingElseLoc));
Chris Lattnerac4471c2007-05-28 05:38:24 +0000967
Chris Lattner37e54f42007-08-22 05:16:28 +0000968 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000969 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +0000970
Chris Lattnerc951dae2006-08-10 04:23:57 +0000971 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +0000972 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +0000973 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +0000974 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000975
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000976 if (Tok.is(tok::kw_else)) {
Nico Weber3cef1082011-12-22 23:26:17 +0000977 if (TrailingElseLoc)
978 *TrailingElseLoc = Tok.getLocation();
979
Chris Lattneraf635312006-10-16 06:06:51 +0000980 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +0000981 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +0000982
Chris Lattner8fb26252007-08-22 05:28:50 +0000983 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000984 // there is no compound stmt. C90 does not have this clause. We only do
985 // this if the body isn't a compound statement to avoid push/pop in common
986 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000987 //
988 // C++ 6.4p1:
989 // The substatement in a selection-statement (each substatement, in the else
990 // form of the if statement) implicitly defines a local scope.
991 //
Sebastian Redl042ad952008-12-11 19:30:53 +0000992 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000993 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000994
Chris Lattner30f910e2006-10-16 05:52:41 +0000995 ElseStmt = ParseStatement();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000996
Chris Lattner37e54f42007-08-22 05:16:28 +0000997 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000998 InnerScope.Exit();
Douglas Gregor4ecb7202011-07-30 08:36:53 +0000999 } else if (Tok.is(tok::code_completion)) {
1000 Actions.CodeCompleteAfterIf(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001001 cutOffParsing();
1002 return StmtError();
Nico Weber3cef1082011-12-22 23:26:17 +00001003 } else if (InnerStatementTrailingElseLoc.isValid()) {
1004 Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else);
Chris Lattnerc951dae2006-08-10 04:23:57 +00001005 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001006
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001007 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +00001008
Chris Lattnerbc2d77c2008-12-12 06:19:11 +00001009 // If the condition was invalid, discard the if statement. We could recover
1010 // better by replacing it with a valid expr, but don't do that yet.
John McCall48871652010-08-21 09:40:31 +00001011 if (CondExp.isInvalid() && !CondVar)
Chris Lattnerbc2d77c2008-12-12 06:19:11 +00001012 return StmtError();
Chris Lattner2dd1b722007-08-26 23:08:06 +00001013
Chris Lattner5c5808a2007-10-29 05:08:52 +00001014 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +00001015 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +00001016 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001017 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
1018 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
1019 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +00001020 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +00001021 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +00001022 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001023
Chris Lattner5c5808a2007-10-29 05:08:52 +00001024 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001025 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001026 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001027 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001028 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001029
John McCallb268a282010-08-23 23:25:46 +00001030 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +00001031 ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +00001032}
1033
Chris Lattner9075bd72006-08-10 04:59:57 +00001034/// ParseSwitchStatement
1035/// switch-statement:
1036/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001037/// [C++] 'switch' '(' condition ')' statement
Richard Smithc202b282012-04-14 00:33:13 +00001038StmtResult Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001039 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001040 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +00001041
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001042 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001043 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +00001044 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001045 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001046 }
Chris Lattner2dd1b722007-08-26 23:08:06 +00001047
David Blaikiebbafb8a2012-03-11 07:00:24 +00001048 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001049
Chris Lattner2dd1b722007-08-26 23:08:06 +00001050 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1051 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001052 //
1053 // C++ 6.4p3:
1054 // A name introduced by a declaration in a condition is in scope from its
1055 // point of declaration until the end of the substatements controlled by the
1056 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001057 // C++ 3.3.2p4:
1058 // Names declared in the for-init-statement, and in the condition of if,
1059 // while, for, and switch statements are local to the if, while, for, or
1060 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001061 //
Richard Trieu2c850c02011-04-21 21:44:26 +00001062 unsigned ScopeFlags = Scope::BreakScope | Scope::SwitchScope;
Chris Lattnerc0081db2008-12-12 06:31:07 +00001063 if (C99orCXX)
1064 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001065 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001066
Chris Lattner9075bd72006-08-10 04:59:57 +00001067 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001068 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +00001069 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001070 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redlb62406f2008-12-11 19:48:14 +00001071 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +00001072
John McCalldadc5752010-08-24 06:29:42 +00001073 StmtResult Switch
John McCallb268a282010-08-23 23:25:46 +00001074 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001075
Douglas Gregore60e41a2010-05-06 17:25:47 +00001076 if (Switch.isInvalid()) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001077 // Skip the switch body.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001078 // FIXME: This is not optimal recovery, but parsing the body is more
1079 // dangerous due to the presence of case and default statements, which
1080 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +00001081 if (Tok.is(tok::l_brace)) {
1082 ConsumeBrace();
1083 SkipUntil(tok::r_brace, false, false);
1084 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +00001085 SkipUntil(tok::semi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001086 return Switch;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001087 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001088
Chris Lattner8fb26252007-08-22 05:28:50 +00001089 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001090 // there is no compound stmt. C90 does not have this clause. We only do this
1091 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001092 //
1093 // C++ 6.4p1:
1094 // The substatement in a selection-statement (each substatement, in the else
1095 // form of the if statement) implicitly defines a local scope.
1096 //
1097 // See comments in ParseIfStatement for why we create a scope for the
1098 // condition and a new scope for substatement in C++.
1099 //
Mike Stump11289f42009-09-09 15:08:12 +00001100 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001101 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +00001102
Chris Lattner9075bd72006-08-10 04:59:57 +00001103 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001104 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001105
Chris Lattner8fd2d012010-01-24 01:50:29 +00001106 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001107 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001108 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001109
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001110 if (Body.isInvalid()) {
Chris Lattner8fd2d012010-01-24 01:50:29 +00001111 // FIXME: Remove the case statement list from the Switch statement.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001112
1113 // Put the synthesized null statement on the same line as the end of switch
1114 // condition.
1115 SourceLocation SynthesizedNullStmtLocation = Cond.get()->getLocEnd();
1116 Body = Actions.ActOnNullStmt(SynthesizedNullStmtLocation);
1117 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001118
John McCallb268a282010-08-23 23:25:46 +00001119 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001120}
1121
1122/// ParseWhileStatement
1123/// while-statement: [C99 6.8.5.1]
1124/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001125/// [C++] 'while' '(' condition ')' statement
Richard Smithc202b282012-04-14 00:33:13 +00001126StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001127 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +00001128 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +00001129 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001130
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001131 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001132 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +00001133 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001134 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001135 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001136
David Blaikiebbafb8a2012-03-11 07:00:24 +00001137 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001138
Chris Lattner2dd1b722007-08-26 23:08:06 +00001139 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1140 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001141 //
1142 // C++ 6.4p3:
1143 // A name introduced by a declaration in a condition is in scope from its
1144 // point of declaration until the end of the substatements controlled by the
1145 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001146 // C++ 3.3.2p4:
1147 // Names declared in the for-init-statement, and in the condition of if,
1148 // while, for, and switch statements are local to the if, while, for, or
1149 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001150 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001151 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001152 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001153 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1154 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001155 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001156 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1157 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001158
Chris Lattner9075bd72006-08-10 04:59:57 +00001159 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001160 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +00001161 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001162 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001163 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001164
David Blaikiea5696df2012-05-16 04:20:04 +00001165 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get(), WhileLoc));
Mike Stump11289f42009-09-09 15:08:12 +00001166
Chris Lattner8fb26252007-08-22 05:28:50 +00001167 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001168 // there is no compound stmt. C90 does not have this clause. We only do this
1169 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001170 //
1171 // C++ 6.5p2:
1172 // The substatement in an iteration-statement implicitly defines a local scope
1173 // which is entered and exited each time through the loop.
1174 //
1175 // See comments in ParseIfStatement for why we create a scope for the
1176 // condition and a new scope for substatement in C++.
1177 //
Mike Stump11289f42009-09-09 15:08:12 +00001178 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001179 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001180
Chris Lattner9075bd72006-08-10 04:59:57 +00001181 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001182 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001183
Chris Lattner8fb26252007-08-22 05:28:50 +00001184 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001185 InnerScope.Exit();
1186 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001187
John McCall48871652010-08-21 09:40:31 +00001188 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001189 return StmtError();
1190
John McCallb268a282010-08-23 23:25:46 +00001191 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001192}
1193
1194/// ParseDoStatement
1195/// do-statement: [C99 6.8.5.2]
1196/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +00001197/// Note: this lets the caller parse the end ';'.
Richard Smithc202b282012-04-14 00:33:13 +00001198StmtResult Parser::ParseDoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001199 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001200 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001201
Chris Lattner2dd1b722007-08-26 23:08:06 +00001202 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1203 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001204 unsigned ScopeFlags;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001205 if (getLangOpts().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001206 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001207 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001208 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001209
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001210 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001211
Chris Lattner8fb26252007-08-22 05:28:50 +00001212 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001213 // there is no compound stmt. C90 does not have this clause. We only do this
1214 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +00001215 //
1216 // C++ 6.5p2:
1217 // The substatement in an iteration-statement implicitly defines a local scope
1218 // which is entered and exited each time through the loop.
1219 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001220 ParseScope InnerScope(this, Scope::DeclScope,
David Blaikiebbafb8a2012-03-11 07:00:24 +00001221 (getLangOpts().C99 || getLangOpts().CPlusPlus) &&
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001222 Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001223
Chris Lattner9075bd72006-08-10 04:59:57 +00001224 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001225 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +00001226
Chris Lattner8fb26252007-08-22 05:28:50 +00001227 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001228 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001229
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001230 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001231 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +00001232 Diag(Tok, diag::err_expected_while);
Chris Lattner03c40412008-11-23 23:17:07 +00001233 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner0046de12008-11-13 18:52:53 +00001234 SkipUntil(tok::semi, false, true);
1235 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001236 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001237 }
Chris Lattneraf635312006-10-16 06:06:51 +00001238 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001239
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001240 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001241 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner0046de12008-11-13 18:52:53 +00001242 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001243 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001244 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001245
Chris Lattner10da53c2008-12-12 06:35:28 +00001246 // Parse the parenthesized condition.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001247 BalancedDelimiterTracker T(*this, tok::l_paren);
1248 T.consumeOpen();
Chad Rosier67055f52012-07-10 21:35:27 +00001249
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001250 // FIXME: Do not just parse the attribute contents and throw them away
1251 ParsedAttributesWithRange attrs(AttrFactory);
1252 MaybeParseCXX0XAttributes(attrs);
1253 ProhibitAttributes(attrs);
1254
John McCalldadc5752010-08-24 06:29:42 +00001255 ExprResult Cond = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001256 T.consumeClose();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001257 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001258
Sebastian Redlb62406f2008-12-11 19:48:14 +00001259 if (Cond.isInvalid() || Body.isInvalid())
1260 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001261
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001262 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(),
1263 Cond.get(), T.getCloseLocation());
Chris Lattner9075bd72006-08-10 04:59:57 +00001264}
1265
1266/// ParseForStatement
1267/// for-statement: [C99 6.8.5.3]
1268/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1269/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001270/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1271/// [C++] statement
Richard Smith02e85f32011-04-14 22:09:26 +00001272/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001273/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1274/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001275///
1276/// [C++] for-init-statement:
1277/// [C++] expression-statement
1278/// [C++] simple-declaration
1279///
Richard Smith02e85f32011-04-14 22:09:26 +00001280/// [C++0x] for-range-declaration:
1281/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1282/// [C++0x] for-range-initializer:
1283/// [C++0x] expression
1284/// [C++0x] braced-init-list [TODO]
Richard Smithc202b282012-04-14 00:33:13 +00001285StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001286 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001287 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001288
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001289 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001290 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +00001291 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001292 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001293 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001294
Chad Rosier67055f52012-07-10 21:35:27 +00001295 bool C99orCXXorObjC = getLangOpts().C99 || getLangOpts().CPlusPlus ||
1296 getLangOpts().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001297
Chris Lattner2dd1b722007-08-26 23:08:06 +00001298 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1299 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001300 //
1301 // C++ 6.4p3:
1302 // A name introduced by a declaration in a condition is in scope from its
1303 // point of declaration until the end of the substatements controlled by the
1304 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001305 // C++ 3.3.2p4:
1306 // Names declared in the for-init-statement, and in the condition of if,
1307 // while, for, and switch statements are local to the if, while, for, or
1308 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001309 // C++ 6.5.3p1:
1310 // Names declared in the for-init-statement are in the same declarative-region
1311 // as those declared in the condition.
1312 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001313 unsigned ScopeFlags;
Chris Lattner934074c2009-04-22 00:54:41 +00001314 if (C99orCXXorObjC)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001315 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1316 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001317 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001318 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1319
1320 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +00001321
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001322 BalancedDelimiterTracker T(*this, tok::l_paren);
1323 T.consumeOpen();
1324
John McCalldadc5752010-08-24 06:29:42 +00001325 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001326
Richard Smith02e85f32011-04-14 22:09:26 +00001327 bool ForEach = false, ForRange = false;
John McCalldadc5752010-08-24 06:29:42 +00001328 StmtResult FirstPart;
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001329 bool SecondPartIsInvalid = false;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001330 FullExprArg SecondPart(Actions);
John McCalldadc5752010-08-24 06:29:42 +00001331 ExprResult Collection;
Richard Smith02e85f32011-04-14 22:09:26 +00001332 ForRangeInit ForRangeInit;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001333 FullExprArg ThirdPart(Actions);
John McCall48871652010-08-21 09:40:31 +00001334 Decl *SecondVar = 0;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001335
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001336 if (Tok.is(tok::code_completion)) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001337 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001338 C99orCXXorObjC? Sema::PCC_ForInit
1339 : Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001340 cutOffParsing();
1341 return StmtError();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001342 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001343
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001344 ParsedAttributesWithRange attrs(AttrFactory);
1345 MaybeParseCXX0XAttributes(attrs);
1346
Chris Lattner9075bd72006-08-10 04:59:57 +00001347 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001348 if (Tok.is(tok::semi)) { // for (;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001349 ProhibitAttributes(attrs);
Chris Lattner53361ac2006-08-10 05:19:57 +00001350 // no first part, eat the ';'.
1351 ConsumeToken();
Eli Friedman0ffc31c2011-12-20 01:50:37 +00001352 } else if (isForInitDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001353 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001354 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001355 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001356
John McCall084e83d2011-03-24 11:26:52 +00001357 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00001358 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001359
Richard Smith02e85f32011-04-14 22:09:26 +00001360 // In C++0x, "for (T NS:a" might not be a typo for ::
David Blaikiebbafb8a2012-03-11 07:00:24 +00001361 bool MightBeForRangeStmt = getLangOpts().CPlusPlus;
Richard Smith02e85f32011-04-14 22:09:26 +00001362 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1363
Chris Lattner49836b42009-04-02 04:16:50 +00001364 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Benjamin Kramerf0623432012-08-23 22:51:59 +00001365 StmtVector Stmts;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001366 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
Richard Smith02e85f32011-04-14 22:09:26 +00001367 DeclEnd, attrs, false,
1368 MightBeForRangeStmt ?
1369 &ForRangeInit : 0);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001370 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001371
Richard Smith02e85f32011-04-14 22:09:26 +00001372 if (ForRangeInit.ParsedForRangeDecl()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001373 Diag(ForRangeInit.ColonLoc, getLangOpts().CPlusPlus0x ?
Richard Smith5d164bc2011-10-15 05:09:34 +00001374 diag::warn_cxx98_compat_for_range : diag::ext_for_range);
Richard Smith58c74332011-09-04 19:54:14 +00001375
Richard Smith02e85f32011-04-14 22:09:26 +00001376 ForRange = true;
1377 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattner32dc41c2009-03-29 17:27:48 +00001378 ConsumeToken();
1379 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001380 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001381 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001382 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001383
Douglas Gregor68762e72010-08-23 21:17:50 +00001384 if (Tok.is(tok::code_completion)) {
1385 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001386 cutOffParsing();
1387 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001388 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001389 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001390 } else {
1391 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001392 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001393 } else {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001394 ProhibitAttributes(attrs);
Chris Lattner89c50c62006-08-11 06:41:18 +00001395 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +00001396
John McCall34376a62010-12-04 03:47:34 +00001397 ForEach = isTokIdentifier_in();
1398
Chris Lattnercd68f642007-06-27 01:06:29 +00001399 // Turn the expression into a stmt.
John McCall34376a62010-12-04 03:47:34 +00001400 if (!Value.isInvalid()) {
1401 if (ForEach)
1402 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1403 else
1404 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
1405 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001406
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001407 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001408 ConsumeToken();
John McCall34376a62010-12-04 03:47:34 +00001409 } else if (ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001410 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001411
Douglas Gregor68762e72010-08-23 21:17:50 +00001412 if (Tok.is(tok::code_completion)) {
1413 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001414 cutOffParsing();
1415 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001416 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001417 Collection = ParseExpression();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001418 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::colon) && FirstPart.get()) {
Richard Smith4f848f12011-12-20 22:56:20 +00001419 // User tried to write the reasonable, but ill-formed, for-range-statement
1420 // for (expr : expr) { ... }
1421 Diag(Tok, diag::err_for_range_expected_decl)
1422 << FirstPart.get()->getSourceRange();
1423 SkipUntil(tok::r_paren, false, true);
1424 SecondPartIsInvalid = true;
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001425 } else {
Douglas Gregor230a7e62011-02-17 03:38:46 +00001426 if (!Value.isInvalid()) {
1427 Diag(Tok, diag::err_expected_semi_for);
1428 } else {
1429 // Skip until semicolon or rparen, don't consume it.
1430 SkipUntil(tok::r_paren, true, true);
1431 if (Tok.is(tok::semi))
1432 ConsumeToken();
1433 }
Chris Lattner53361ac2006-08-10 05:19:57 +00001434 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001435 }
Richard Smith02e85f32011-04-14 22:09:26 +00001436 if (!ForEach && !ForRange) {
John McCallb268a282010-08-23 23:25:46 +00001437 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001438 // Parse the second part of the for specifier.
1439 if (Tok.is(tok::semi)) { // for (...;;
1440 // no second part.
Douglas Gregor230a7e62011-02-17 03:38:46 +00001441 } else if (Tok.is(tok::r_paren)) {
1442 // missing both semicolons.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001443 } else {
John McCalldadc5752010-08-24 06:29:42 +00001444 ExprResult Second;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001445 if (getLangOpts().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +00001446 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1447 else {
1448 Second = ParseExpression();
1449 if (!Second.isInvalid())
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001450 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCallb268a282010-08-23 23:25:46 +00001451 Second.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001452 }
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001453 SecondPartIsInvalid = Second.isInvalid();
David Blaikiea5696df2012-05-16 04:20:04 +00001454 SecondPart = Actions.MakeFullExpr(Second.get(), ForLoc);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001455 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001456
Douglas Gregor230a7e62011-02-17 03:38:46 +00001457 if (Tok.isNot(tok::semi)) {
1458 if (!SecondPartIsInvalid || SecondVar)
1459 Diag(Tok, diag::err_expected_semi_for);
1460 else
1461 // Skip until semicolon or rparen, don't consume it.
1462 SkipUntil(tok::r_paren, true, true);
1463 }
1464
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001465 if (Tok.is(tok::semi)) {
1466 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001467 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001468
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001469 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001470 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001471 ExprResult Third = ParseExpression();
John McCallb268a282010-08-23 23:25:46 +00001472 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001473 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001474 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001475 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001476 T.consumeClose();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001477
Richard Smith02e85f32011-04-14 22:09:26 +00001478 // We need to perform most of the semantic analysis for a C++0x for-range
1479 // statememt before parsing the body, in order to be able to deduce the type
1480 // of an auto-typed loop variable.
1481 StmtResult ForRangeStmt;
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001482 StmtResult ForEachStmt;
Chad Rosier67055f52012-07-10 21:35:27 +00001483
John McCall53848232011-07-27 01:07:15 +00001484 if (ForRange) {
Sam Panzer2c4ca0f2012-08-16 21:47:25 +00001485 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, FirstPart.take(),
Richard Smith02e85f32011-04-14 22:09:26 +00001486 ForRangeInit.ColonLoc,
1487 ForRangeInit.RangeExpr.get(),
Richard Smitha05b3b52012-09-20 21:52:32 +00001488 T.getCloseLocation(),
1489 Sema::BFRK_Build);
Richard Smith02e85f32011-04-14 22:09:26 +00001490
John McCall53848232011-07-27 01:07:15 +00001491
1492 // Similarly, we need to do the semantic analysis for a for-range
1493 // statement immediately in order to close over temporaries correctly.
1494 } else if (ForEach) {
Sam Panzer2c4ca0f2012-08-16 21:47:25 +00001495 ForEachStmt = Actions.ActOnObjCForCollectionStmt(ForLoc,
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001496 FirstPart.take(),
Chad Rosier67055f52012-07-10 21:35:27 +00001497 Collection.take(),
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001498 T.getCloseLocation());
John McCall53848232011-07-27 01:07:15 +00001499 }
1500
Chris Lattner8fb26252007-08-22 05:28:50 +00001501 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001502 // there is no compound stmt. C90 does not have this clause. We only do this
1503 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001504 //
1505 // C++ 6.5p2:
1506 // The substatement in an iteration-statement implicitly defines a local scope
1507 // which is entered and exited each time through the loop.
1508 //
1509 // See comments in ParseIfStatement for why we create a scope for
1510 // for-init-statement/condition and a new scope for substatement in C++.
1511 //
Mike Stump11289f42009-09-09 15:08:12 +00001512 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner934074c2009-04-22 00:54:41 +00001513 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001514
Chris Lattner9075bd72006-08-10 04:59:57 +00001515 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001516 StmtResult Body(ParseStatement(TrailingElseLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001517
Chris Lattner8fb26252007-08-22 05:28:50 +00001518 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001519 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001520
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001521 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001522 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001523
1524 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001525 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001526
Richard Smith02e85f32011-04-14 22:09:26 +00001527 if (ForEach)
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001528 return Actions.FinishObjCForCollectionStmt(ForEachStmt.take(),
1529 Body.take());
Mike Stump11289f42009-09-09 15:08:12 +00001530
Richard Smith02e85f32011-04-14 22:09:26 +00001531 if (ForRange)
1532 return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take());
1533
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001534 return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.take(),
1535 SecondPart, SecondVar, ThirdPart,
1536 T.getCloseLocation(), Body.take());
Chris Lattner9075bd72006-08-10 04:59:57 +00001537}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001538
Chris Lattner503fadc2006-08-10 05:45:44 +00001539/// ParseGotoStatement
1540/// jump-statement:
1541/// 'goto' identifier ';'
1542/// [GNU] 'goto' '*' expression ';'
1543///
1544/// Note: this lets the caller parse the end ';'.
1545///
Richard Smithc202b282012-04-14 00:33:13 +00001546StmtResult Parser::ParseGotoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001547 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001548 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001549
John McCalldadc5752010-08-24 06:29:42 +00001550 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001551 if (Tok.is(tok::identifier)) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +00001552 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1553 Tok.getLocation());
1554 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Chris Lattner503fadc2006-08-10 05:45:44 +00001555 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001556 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001557 // GNU indirect goto extension.
1558 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001559 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001560 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001561 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001562 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001563 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001564 }
John McCallb268a282010-08-23 23:25:46 +00001565 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001566 } else {
1567 Diag(Tok, diag::err_expected_ident);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001568 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001569 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001570
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001571 return Res;
Chris Lattner503fadc2006-08-10 05:45:44 +00001572}
1573
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001574/// ParseContinueStatement
1575/// jump-statement:
1576/// 'continue' ';'
1577///
1578/// Note: this lets the caller parse the end ';'.
1579///
Richard Smithc202b282012-04-14 00:33:13 +00001580StmtResult Parser::ParseContinueStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001581 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001582 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001583}
1584
1585/// ParseBreakStatement
1586/// jump-statement:
1587/// 'break' ';'
1588///
1589/// Note: this lets the caller parse the end ';'.
1590///
Richard Smithc202b282012-04-14 00:33:13 +00001591StmtResult Parser::ParseBreakStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001592 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001593 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001594}
1595
Chris Lattner503fadc2006-08-10 05:45:44 +00001596/// ParseReturnStatement
1597/// jump-statement:
1598/// 'return' expression[opt] ';'
Richard Smithc202b282012-04-14 00:33:13 +00001599StmtResult Parser::ParseReturnStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001600 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001601 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001602
John McCalldadc5752010-08-24 06:29:42 +00001603 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001604 if (Tok.isNot(tok::semi)) {
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001605 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001606 Actions.CodeCompleteReturn(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001607 cutOffParsing();
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001608 return StmtError();
1609 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001610
David Blaikiebbafb8a2012-03-11 07:00:24 +00001611 if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus) {
Douglas Gregore9e27d92011-03-11 23:10:44 +00001612 R = ParseInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00001613 if (R.isUsable())
David Blaikiebbafb8a2012-03-11 07:00:24 +00001614 Diag(R.get()->getLocStart(), getLangOpts().CPlusPlus0x ?
Richard Smith5d164bc2011-10-15 05:09:34 +00001615 diag::warn_cxx98_compat_generalized_initializer_lists :
1616 diag::ext_generalized_initializer_lists)
Douglas Gregore9e27d92011-03-11 23:10:44 +00001617 << R.get()->getSourceRange();
1618 } else
1619 R = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001620 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001621 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001622 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001623 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001624 }
John McCallb268a282010-08-23 23:25:46 +00001625 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Chris Lattner503fadc2006-08-10 05:45:44 +00001626}
Chris Lattner0116c472006-08-15 06:03:28 +00001627
Eli Friedmana4b02c32011-09-30 01:13:51 +00001628/// ParseMicrosoftAsmStatement. When -fms-extensions/-fasm-blocks is enabled,
1629/// this routine is called to collect the tokens for an MS asm statement.
Chad Rosier32503022012-06-11 20:47:18 +00001630///
1631/// [MS] ms-asm-statement:
1632/// ms-asm-block
1633/// ms-asm-block ms-asm-statement
1634///
1635/// [MS] ms-asm-block:
1636/// '__asm' ms-asm-line '\n'
1637/// '__asm' '{' ms-asm-instruction-block[opt] '}' ';'[opt]
1638///
1639/// [MS] ms-asm-instruction-block
1640/// ms-asm-line
1641/// ms-asm-line '\n' ms-asm-instruction-block
1642///
Eli Friedmana4b02c32011-09-30 01:13:51 +00001643StmtResult Parser::ParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
Chad Rosier175ea242012-08-24 21:42:51 +00001644 // MS-style inline assembly is not fully supported, so emit a warning.
1645 Diag(AsmLoc, diag::warn_unsupported_msasm);
1646
Eli Friedmana4b02c32011-09-30 01:13:51 +00001647 SourceManager &SrcMgr = PP.getSourceManager();
1648 SourceLocation EndLoc = AsmLoc;
Chad Rosier32503022012-06-11 20:47:18 +00001649 SmallVector<Token, 4> AsmToks;
Chad Rosierc97a6bb2012-08-14 19:22:06 +00001650
1651 bool InBraces = false;
1652 unsigned short savedBraceCount = 0;
1653 bool InAsmComment = false;
1654 FileID FID;
1655 unsigned LineNo = 0;
1656 unsigned NumTokensRead = 0;
1657 SourceLocation LBraceLoc;
1658
1659 if (Tok.is(tok::l_brace)) {
1660 // Braced inline asm: consume the opening brace.
1661 InBraces = true;
1662 savedBraceCount = BraceCount;
1663 EndLoc = LBraceLoc = ConsumeBrace();
1664 ++NumTokensRead;
1665 } else {
1666 // Single-line inline asm; compute which line it is on.
1667 std::pair<FileID, unsigned> ExpAsmLoc =
1668 SrcMgr.getDecomposedExpansionLoc(EndLoc);
1669 FID = ExpAsmLoc.first;
1670 LineNo = SrcMgr.getLineNumber(FID, ExpAsmLoc.second);
1671 }
1672
1673 SourceLocation TokLoc = Tok.getLocation();
Eli Friedmana4b02c32011-09-30 01:13:51 +00001674 do {
Chad Rosierc97a6bb2012-08-14 19:22:06 +00001675 // If we hit EOF, we're done, period.
1676 if (Tok.is(tok::eof))
Eli Friedmana4b02c32011-09-30 01:13:51 +00001677 break;
Chad Rosierc97a6bb2012-08-14 19:22:06 +00001678
Chad Rosierc97a6bb2012-08-14 19:22:06 +00001679 if (!InAsmComment && Tok.is(tok::semi)) {
1680 // A semicolon in an asm is the start of a comment.
1681 InAsmComment = true;
1682 if (InBraces) {
1683 // Compute which line the comment is on.
1684 std::pair<FileID, unsigned> ExpSemiLoc =
1685 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1686 FID = ExpSemiLoc.first;
1687 LineNo = SrcMgr.getLineNumber(FID, ExpSemiLoc.second);
1688 }
1689 } else if (!InBraces || InAsmComment) {
1690 // If end-of-line is significant, check whether this token is on a
1691 // new line.
1692 std::pair<FileID, unsigned> ExpLoc =
1693 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1694 if (ExpLoc.first != FID ||
1695 SrcMgr.getLineNumber(ExpLoc.first, ExpLoc.second) != LineNo) {
1696 // If this is a single-line __asm, we're done.
1697 if (!InBraces)
1698 break;
1699 // We're no longer in a comment.
1700 InAsmComment = false;
1701 } else if (!InAsmComment && Tok.is(tok::r_brace)) {
1702 // Single-line asm always ends when a closing brace is seen.
1703 // FIXME: This is compatible with Apple gcc's -fasm-blocks; what
1704 // does MSVC do here?
1705 break;
1706 }
1707 }
1708 if (!InAsmComment && InBraces && Tok.is(tok::r_brace) &&
1709 BraceCount == (savedBraceCount + 1)) {
1710 // Consume the closing brace, and finish
1711 EndLoc = ConsumeBrace();
1712 break;
1713 }
1714
1715 // Consume the next token; make sure we don't modify the brace count etc.
1716 // if we are in a comment.
1717 EndLoc = TokLoc;
1718 if (InAsmComment)
1719 PP.Lex(Tok);
1720 else {
1721 AsmToks.push_back(Tok);
1722 ConsumeAnyToken();
1723 }
1724 TokLoc = Tok.getLocation();
1725 ++NumTokensRead;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001726 } while (1);
Chad Rosier32503022012-06-11 20:47:18 +00001727
Chad Rosierc97a6bb2012-08-14 19:22:06 +00001728 if (InBraces && BraceCount != savedBraceCount) {
1729 // __asm without closing brace (this can happen at EOF).
1730 Diag(Tok, diag::err_expected_rbrace);
1731 Diag(LBraceLoc, diag::note_matching) << "{";
1732 return StmtError();
1733 } else if (NumTokensRead == 0) {
1734 // Empty __asm.
1735 Diag(Tok, diag::err_expected_lbrace);
1736 return StmtError();
1737 }
1738
Chad Rosier175ea242012-08-24 21:42:51 +00001739 // If MS-style inline assembly is disabled, then build an empty asm.
1740 if (!getLangOpts().EmitMicrosoftInlineAsm) {
1741 Token t;
1742 t.setKind(tok::string_literal);
1743 t.setLiteralData("\"/*FIXME: not done*/\"");
1744 t.clearFlag(Token::NeedsCleaning);
1745 t.setLength(21);
1746 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
1747 ExprVector Constraints;
1748 ExprVector Exprs;
1749 ExprVector Clobbers;
Chad Rosierde70e0e2012-08-25 00:11:56 +00001750 return Actions.ActOnGCCAsmStmt(AsmLoc, true, true, 0, 0, 0, Constraints,
1751 Exprs, AsmString.take(), Clobbers, EndLoc);
Chad Rosier175ea242012-08-24 21:42:51 +00001752 }
1753
Chad Rosierc6c71332012-08-06 20:03:45 +00001754 // FIXME: We should be passing source locations for better diagnostics.
Chad Rosierb6f46c12012-08-15 16:53:30 +00001755 return Actions.ActOnMSAsmStmt(AsmLoc, LBraceLoc,
1756 llvm::makeArrayRef(AsmToks), EndLoc);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001757}
1758
Chris Lattner0116c472006-08-15 06:03:28 +00001759/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001760/// asm-statement:
1761/// gnu-asm-statement
1762/// ms-asm-statement
1763///
1764/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001765/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1766///
1767/// [GNU] asm-argument:
1768/// asm-string-literal
1769/// asm-string-literal ':' asm-operands[opt]
1770/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1771/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1772/// ':' asm-clobbers
1773///
1774/// [GNU] asm-clobbers:
1775/// asm-string-literal
1776/// asm-clobbers ',' asm-string-literal
1777///
John McCalldadc5752010-08-24 06:29:42 +00001778StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001779 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001780 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001781
Chad Rosier67055f52012-07-10 21:35:27 +00001782 if (getLangOpts().MicrosoftExt && Tok.isNot(tok::l_paren) &&
1783 !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001784 msAsm = true;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001785 return ParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001786 }
John McCall084e83d2011-03-24 11:26:52 +00001787 DeclSpec DS(AttrFactory);
Chris Lattner0116c472006-08-15 06:03:28 +00001788 SourceLocation Loc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001789 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001790
Chris Lattner0116c472006-08-15 06:03:28 +00001791 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001792 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001793 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001794 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001795 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redlb62406f2008-12-11 19:48:14 +00001796
Chris Lattner0116c472006-08-15 06:03:28 +00001797 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001798 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001799 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001800 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001801 SkipUntil(tok::r_paren);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001802 return StmtError();
Chris Lattner0116c472006-08-15 06:03:28 +00001803 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001804 BalancedDelimiterTracker T(*this, tok::l_paren);
1805 T.consumeOpen();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001806
John McCalldadc5752010-08-24 06:29:42 +00001807 ExprResult AsmString(ParseAsmStringLiteral());
Ted Kremenekeb0a6c02011-12-02 01:30:14 +00001808 if (AsmString.isInvalid()) {
Richard Smithd67aea22012-03-06 03:21:47 +00001809 // Consume up to and including the closing paren.
1810 T.skipToEnd();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001811 return StmtError();
Ted Kremenekeb0a6c02011-12-02 01:30:14 +00001812 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001813
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001814 SmallVector<IdentifierInfo *, 4> Names;
Benjamin Kramerf0623432012-08-23 22:51:59 +00001815 ExprVector Constraints;
1816 ExprVector Exprs;
1817 ExprVector Clobbers;
Chris Lattner0116c472006-08-15 06:03:28 +00001818
Anders Carlsson19fe1162008-02-05 23:03:50 +00001819 if (Tok.is(tok::r_paren)) {
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001820 // We have a simple asm expression like 'asm("foo")'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001821 T.consumeClose();
Chad Rosierde70e0e2012-08-25 00:11:56 +00001822 return Actions.ActOnGCCAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1823 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1824 Constraints, Exprs, AsmString.take(),
1825 Clobbers, T.getCloseLocation());
Chris Lattner0116c472006-08-15 06:03:28 +00001826 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001827
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001828 // Parse Outputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001829 bool AteExtraColon = false;
1830 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1831 // In C++ mode, parse "::" like ": :".
1832 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001833 ConsumeToken();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001834
Chris Lattner15768502009-12-20 23:08:04 +00001835 if (!AteExtraColon &&
1836 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001837 return StmtError();
1838 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001839
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001840 unsigned NumOutputs = Names.size();
1841
1842 // Parse Inputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001843 if (AteExtraColon ||
1844 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1845 // In C++ mode, parse "::" like ": :".
1846 if (AteExtraColon)
1847 AteExtraColon = false;
1848 else {
1849 AteExtraColon = Tok.is(tok::coloncolon);
1850 ConsumeToken();
1851 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001852
Chris Lattner15768502009-12-20 23:08:04 +00001853 if (!AteExtraColon &&
1854 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001855 return StmtError();
1856 }
1857
1858 assert(Names.size() == Constraints.size() &&
1859 Constraints.size() == Exprs.size() &&
1860 "Input operand size mismatch!");
1861
1862 unsigned NumInputs = Names.size() - NumOutputs;
1863
1864 // Parse the clobbers, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001865 if (AteExtraColon || Tok.is(tok::colon)) {
1866 if (!AteExtraColon)
1867 ConsumeToken();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001868
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001869 // Parse the asm-string list for clobbers if present.
1870 if (Tok.isNot(tok::r_paren)) {
1871 while (1) {
John McCalldadc5752010-08-24 06:29:42 +00001872 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001873
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001874 if (Clobber.isInvalid())
1875 break;
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001876
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001877 Clobbers.push_back(Clobber.release());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001878
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001879 if (Tok.isNot(tok::comma)) break;
1880 ConsumeToken();
1881 }
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001882 }
1883 }
1884
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001885 T.consumeClose();
Chad Rosierde70e0e2012-08-25 00:11:56 +00001886 return Actions.ActOnGCCAsmStmt(AsmLoc, false, isVolatile, NumOutputs,
1887 NumInputs, Names.data(), Constraints, Exprs,
1888 AsmString.take(), Clobbers,
1889 T.getCloseLocation());
Chris Lattner0116c472006-08-15 06:03:28 +00001890}
1891
1892/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001893/// asm-statement, assuming the leading ':' token was eaten.
Chris Lattner0116c472006-08-15 06:03:28 +00001894///
1895/// [GNU] asm-operands:
1896/// asm-operand
1897/// asm-operands ',' asm-operand
1898///
1899/// [GNU] asm-operand:
1900/// asm-string-literal '(' expression ')'
1901/// '[' identifier ']' asm-string-literal '(' expression ')'
1902///
Daniel Dunbar70e7ead2009-10-18 20:26:27 +00001903//
1904// FIXME: Avoid unnecessary std::string trashing.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001905bool Parser::ParseAsmOperandsOpt(SmallVectorImpl<IdentifierInfo *> &Names,
Richard Trieu2bd04012011-09-09 02:00:50 +00001906 SmallVectorImpl<Expr *> &Constraints,
1907 SmallVectorImpl<Expr *> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001908 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001909 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001910 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001911
1912 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001913 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001914 if (Tok.is(tok::l_square)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001915 BalancedDelimiterTracker T(*this, tok::l_square);
1916 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00001917
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001918 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001919 Diag(Tok, diag::err_expected_ident);
1920 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001921 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001922 }
Mike Stump11289f42009-09-09 15:08:12 +00001923
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001924 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001925 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001926
Anders Carlsson9a020f92010-01-30 22:25:16 +00001927 Names.push_back(II);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001928 T.consumeClose();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001929 } else
Anders Carlsson9a020f92010-01-30 22:25:16 +00001930 Names.push_back(0);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001931
John McCalldadc5752010-08-24 06:29:42 +00001932 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001933 if (Constraint.isInvalid()) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001934 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001935 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001936 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001937 Constraints.push_back(Constraint.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001938
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001939 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001940 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00001941 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001942 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001943 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001944
Chris Lattner0116c472006-08-15 06:03:28 +00001945 // Read the parenthesized expression.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001946 BalancedDelimiterTracker T(*this, tok::l_paren);
1947 T.consumeOpen();
John McCalldadc5752010-08-24 06:29:42 +00001948 ExprResult Res(ParseExpression());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001949 T.consumeClose();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001950 if (Res.isInvalid()) {
Chris Lattner0116c472006-08-15 06:03:28 +00001951 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001952 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001953 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001954 Exprs.push_back(Res.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001955 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001956 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001957 ConsumeToken();
1958 }
1959}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001960
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001961Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001962 assert(Tok.is(tok::l_brace));
1963 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001964
Erik Verbruggen6e922512012-04-12 10:11:59 +00001965 if (SkipFunctionBodies && trySkippingFunctionBody()) {
1966 BodyScope.Exit();
1967 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001968 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001969
John McCallfaf5fb42010-08-26 23:41:50 +00001970 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1971 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001972
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001973 // Do not enter a scope for the brace, as the arguments are in the same scope
1974 // (the function body) as the body itself. Instead, just read the statement
1975 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00001976 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001977
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001978 // If the function body could not be parsed, make a bogus compoundstmt.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001979 if (FnBody.isInvalid()) {
1980 Sema::CompoundScopeRAII CompoundScope(Actions);
Mike Stump11289f42009-09-09 15:08:12 +00001981 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001982 MultiStmtArg(), false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001983 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001984
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001985 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00001986 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00001987}
Sebastian Redlb219c902008-12-21 16:41:36 +00001988
Sebastian Redla7b98a72009-04-26 20:35:05 +00001989/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1990///
1991/// function-try-block:
1992/// 'try' ctor-initializer[opt] compound-statement handler-seq
1993///
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001994Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00001995 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1996 SourceLocation TryLoc = ConsumeToken();
1997
John McCallfaf5fb42010-08-26 23:41:50 +00001998 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1999 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00002000
2001 // Constructor initializer list?
2002 if (Tok.is(tok::colon))
2003 ParseConstructorInitializer(Decl);
Douglas Gregor5ca153f2011-09-07 20:36:12 +00002004 else
2005 Actions.ActOnDefaultCtorInitializers(Decl);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00002006
Erik Verbruggen6e922512012-04-12 10:11:59 +00002007 if (SkipFunctionBodies && trySkippingFunctionBody()) {
2008 BodyScope.Exit();
2009 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002010 }
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002011
Sebastian Redld98ecd62009-04-26 21:08:36 +00002012 SourceLocation LBraceLoc = Tok.getLocation();
John McCalldadc5752010-08-24 06:29:42 +00002013 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redla7b98a72009-04-26 20:35:05 +00002014 // If we failed to parse the try-catch, we just give the function an empty
2015 // compound statement as the body.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002016 if (FnBody.isInvalid()) {
2017 Sema::CompoundScopeRAII CompoundScope(Actions);
Sebastian Redld98ecd62009-04-26 21:08:36 +00002018 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00002019 MultiStmtArg(), false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002020 }
Sebastian Redla7b98a72009-04-26 20:35:05 +00002021
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002022 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00002023 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redla7b98a72009-04-26 20:35:05 +00002024}
2025
Erik Verbruggen6e922512012-04-12 10:11:59 +00002026bool Parser::trySkippingFunctionBody() {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002027 assert(Tok.is(tok::l_brace));
Erik Verbruggen6e922512012-04-12 10:11:59 +00002028 assert(SkipFunctionBodies &&
2029 "Should only be called when SkipFunctionBodies is enabled");
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002030
Argyrios Kyrtzidis289e4a32012-10-31 17:29:28 +00002031 if (!PP.isCodeCompletionEnabled()) {
2032 ConsumeBrace();
2033 SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false);
2034 return true;
2035 }
2036
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002037 // We're in code-completion mode. Skip parsing for all function bodies unless
2038 // the body contains the code-completion point.
2039 TentativeParsingAction PA(*this);
2040 ConsumeBrace();
2041 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
Argyrios Kyrtzidis289e4a32012-10-31 17:29:28 +00002042 /*StopAtCodeCompletion=*/true)) {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002043 PA.Commit();
2044 return true;
2045 }
2046
2047 PA.Revert();
2048 return false;
2049}
2050
Sebastian Redlb219c902008-12-21 16:41:36 +00002051/// ParseCXXTryBlock - Parse a C++ try-block.
2052///
2053/// try-block:
2054/// 'try' compound-statement handler-seq
2055///
Richard Smithc202b282012-04-14 00:33:13 +00002056StmtResult Parser::ParseCXXTryBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00002057 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2058
2059 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00002060 return ParseCXXTryBlockCommon(TryLoc);
2061}
2062
2063/// ParseCXXTryBlockCommon - Parse the common part of try-block and
2064/// function-try-block.
2065///
2066/// try-block:
2067/// 'try' compound-statement handler-seq
2068///
2069/// function-try-block:
2070/// 'try' ctor-initializer[opt] compound-statement handler-seq
2071///
2072/// handler-seq:
2073/// handler handler-seq[opt]
2074///
John Wiegley1c0675e2011-04-28 01:08:34 +00002075/// [Borland] try-block:
2076/// 'try' compound-statement seh-except-block
2077/// 'try' compound-statment seh-finally-block
2078///
John McCalldadc5752010-08-24 06:29:42 +00002079StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redlb219c902008-12-21 16:41:36 +00002080 if (Tok.isNot(tok::l_brace))
2081 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002082 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
Richard Smithc202b282012-04-14 00:33:13 +00002083
2084 StmtResult TryBlock(ParseCompoundStatement(/*isStmtExpr=*/false,
Douglas Gregor53e191ed2011-07-06 22:04:06 +00002085 Scope::DeclScope|Scope::TryScope));
Sebastian Redlb219c902008-12-21 16:41:36 +00002086 if (TryBlock.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002087 return TryBlock;
Sebastian Redlb219c902008-12-21 16:41:36 +00002088
John Wiegley1c0675e2011-04-28 01:08:34 +00002089 // Borland allows SEH-handlers with 'try'
Chad Rosier67055f52012-07-10 21:35:27 +00002090
Richard Smithc202b282012-04-14 00:33:13 +00002091 if ((Tok.is(tok::identifier) &&
2092 Tok.getIdentifierInfo() == getSEHExceptKeyword()) ||
2093 Tok.is(tok::kw___finally)) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002094 // TODO: Factor into common return ParseSEHHandlerCommon(...)
2095 StmtResult Handler;
Douglas Gregor60060d62011-10-21 03:57:52 +00002096 if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002097 SourceLocation Loc = ConsumeToken();
2098 Handler = ParseSEHExceptBlock(Loc);
2099 }
2100 else {
2101 SourceLocation Loc = ConsumeToken();
2102 Handler = ParseSEHFinallyBlock(Loc);
2103 }
2104 if(Handler.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002105 return Handler;
John McCall53fa7142010-12-24 02:08:15 +00002106
John Wiegley1c0675e2011-04-28 01:08:34 +00002107 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
2108 TryLoc,
2109 TryBlock.take(),
2110 Handler.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00002111 }
John Wiegley1c0675e2011-04-28 01:08:34 +00002112 else {
Benjamin Kramerf0623432012-08-23 22:51:59 +00002113 StmtVector Handlers;
Richard Smithc202b282012-04-14 00:33:13 +00002114 ParsedAttributesWithRange attrs(AttrFactory);
John Wiegley1c0675e2011-04-28 01:08:34 +00002115 MaybeParseCXX0XAttributes(attrs);
2116 ProhibitAttributes(attrs);
Sebastian Redlb219c902008-12-21 16:41:36 +00002117
John Wiegley1c0675e2011-04-28 01:08:34 +00002118 if (Tok.isNot(tok::kw_catch))
2119 return StmtError(Diag(Tok, diag::err_expected_catch));
2120 while (Tok.is(tok::kw_catch)) {
2121 StmtResult Handler(ParseCXXCatchBlock());
2122 if (!Handler.isInvalid())
2123 Handlers.push_back(Handler.release());
2124 }
2125 // Don't bother creating the full statement if we don't have any usable
2126 // handlers.
2127 if (Handlers.empty())
2128 return StmtError();
2129
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002130 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(),Handlers);
John Wiegley1c0675e2011-04-28 01:08:34 +00002131 }
Sebastian Redlb219c902008-12-21 16:41:36 +00002132}
2133
2134/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
2135///
2136/// handler:
2137/// 'catch' '(' exception-declaration ')' compound-statement
2138///
2139/// exception-declaration:
2140/// type-specifier-seq declarator
2141/// type-specifier-seq abstract-declarator
2142/// type-specifier-seq
2143/// '...'
2144///
John McCalldadc5752010-08-24 06:29:42 +00002145StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00002146 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
2147
2148 SourceLocation CatchLoc = ConsumeToken();
2149
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002150 BalancedDelimiterTracker T(*this, tok::l_paren);
2151 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redlb219c902008-12-21 16:41:36 +00002152 return StmtError();
2153
2154 // C++ 3.3.2p3:
2155 // The name in a catch exception-declaration is local to the handler and
2156 // shall not be redeclared in the outermost block of the handler.
2157 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
2158
2159 // exception-declaration is equivalent to '...' or a parameter-declaration
2160 // without default arguments.
John McCall48871652010-08-21 09:40:31 +00002161 Decl *ExceptionDecl = 0;
Sebastian Redlb219c902008-12-21 16:41:36 +00002162 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00002163 DeclSpec DS(AttrFactory);
Sebastian Redl54c04d42008-12-22 19:15:10 +00002164 if (ParseCXXTypeSpecifierSeq(DS))
2165 return StmtError();
Sebastian Redlb219c902008-12-21 16:41:36 +00002166 Declarator ExDecl(DS, Declarator::CXXCatchContext);
2167 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002168 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00002169 } else
2170 ConsumeToken();
2171
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002172 T.consumeClose();
2173 if (T.getCloseLocation().isInvalid())
Sebastian Redlb219c902008-12-21 16:41:36 +00002174 return StmtError();
2175
2176 if (Tok.isNot(tok::l_brace))
2177 return StmtError(Diag(Tok, diag::err_expected_lbrace));
2178
Alexis Hunt96d5c762009-11-21 08:43:09 +00002179 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
Richard Smithc202b282012-04-14 00:33:13 +00002180 StmtResult Block(ParseCompoundStatement());
Sebastian Redlb219c902008-12-21 16:41:36 +00002181 if (Block.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002182 return Block;
Sebastian Redlb219c902008-12-21 16:41:36 +00002183
John McCallb268a282010-08-23 23:25:46 +00002184 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00002185}
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002186
2187void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Douglas Gregor43edb322011-10-24 22:31:10 +00002188 IfExistsCondition Result;
Francois Picheta5b3fcb2011-05-07 17:30:27 +00002189 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002190 return;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00002191
Douglas Gregor43edb322011-10-24 22:31:10 +00002192 // Handle dependent statements by parsing the braces as a compound statement.
2193 // This is not the same behavior as Visual C++, which don't treat this as a
2194 // compound statement, but for Clang's type checking we can't have anything
2195 // inside these braces escaping to the surrounding code.
2196 if (Result.Behavior == IEB_Dependent) {
2197 if (!Tok.is(tok::l_brace)) {
2198 Diag(Tok, diag::err_expected_lbrace);
Richard Smithc202b282012-04-14 00:33:13 +00002199 return;
Douglas Gregor43edb322011-10-24 22:31:10 +00002200 }
Richard Smithc202b282012-04-14 00:33:13 +00002201
2202 StmtResult Compound = ParseCompoundStatement();
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002203 if (Compound.isInvalid())
2204 return;
Richard Smithc202b282012-04-14 00:33:13 +00002205
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002206 StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc,
2207 Result.IsIfExists,
Richard Smithc202b282012-04-14 00:33:13 +00002208 Result.SS,
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002209 Result.Name,
2210 Compound.get());
2211 if (DepResult.isUsable())
2212 Stmts.push_back(DepResult.get());
Douglas Gregor43edb322011-10-24 22:31:10 +00002213 return;
2214 }
Richard Smithc202b282012-04-14 00:33:13 +00002215
Douglas Gregor43edb322011-10-24 22:31:10 +00002216 BalancedDelimiterTracker Braces(*this, tok::l_brace);
2217 if (Braces.consumeOpen()) {
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002218 Diag(Tok, diag::err_expected_lbrace);
2219 return;
2220 }
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002221
Douglas Gregor43edb322011-10-24 22:31:10 +00002222 switch (Result.Behavior) {
2223 case IEB_Parse:
2224 // Parse the statements below.
2225 break;
Chad Rosier67055f52012-07-10 21:35:27 +00002226
Douglas Gregor43edb322011-10-24 22:31:10 +00002227 case IEB_Dependent:
2228 llvm_unreachable("Dependent case handled above");
Chad Rosier67055f52012-07-10 21:35:27 +00002229
Douglas Gregor43edb322011-10-24 22:31:10 +00002230 case IEB_Skip:
2231 Braces.skipToEnd();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002232 return;
2233 }
2234
2235 // Condition is true, parse the statements.
2236 while (Tok.isNot(tok::r_brace)) {
2237 StmtResult R = ParseStatementOrDeclaration(Stmts, false);
2238 if (R.isUsable())
2239 Stmts.push_back(R.release());
2240 }
Douglas Gregor43edb322011-10-24 22:31:10 +00002241 Braces.consumeClose();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002242}