blob: 6db47a01571241b714c33dc05fcc066e47b6e7ac [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
Chris Lattner0ccd51e2006-08-09 05:47:47 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner0ccd51e2006-08-09 05:47:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000016#include "RAIIObjectsForParser.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/Basic/Diagnostic.h"
18#include "clang/Basic/PrettyStackTrace.h"
19#include "clang/Basic/SourceManager.h"
John McCall8b0666c2010-08-20 18:27:03 +000020#include "clang/Sema/DeclSpec.h"
John McCallfaf5fb42010-08-26 23:41:50 +000021#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall8b0666c2010-08-20 18:27:03 +000022#include "clang/Sema/Scope.h"
Richard Smith4f605af2012-08-18 00:55:03 +000023#include "clang/Sema/TypoCorrection.h"
Chad Rosier32503022012-06-11 20:47:18 +000024#include "llvm/ADT/SmallString.h"
Chris Lattner0ccd51e2006-08-09 05:47:47 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// C99 6.8: Statements and Blocks.
29//===----------------------------------------------------------------------===//
30
31/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
32/// StatementOrDeclaration:
33/// statement
34/// declaration
35///
36/// statement:
37/// labeled-statement
38/// compound-statement
39/// expression-statement
40/// selection-statement
41/// iteration-statement
42/// jump-statement
Argyrios Kyrtzidisdee82912008-09-07 18:58:01 +000043/// [C++] declaration-statement
Sebastian Redlb219c902008-12-21 16:41:36 +000044/// [C++] try-block
John Wiegley1c0675e2011-04-28 01:08:34 +000045/// [MS] seh-try-block
Fariborz Jahanian90814572007-10-04 20:19:06 +000046/// [OBC] objc-throw-statement
47/// [OBC] objc-try-catch-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +000048/// [OBC] objc-synchronized-statement
Chris Lattner0116c472006-08-15 06:03:28 +000049/// [GNU] asm-statement
Chris Lattner0ccd51e2006-08-09 05:47:47 +000050/// [OMP] openmp-construct [TODO]
51///
52/// labeled-statement:
53/// identifier ':' statement
54/// 'case' constant-expression ':' statement
55/// 'default' ':' statement
56///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000057/// selection-statement:
58/// if-statement
59/// switch-statement
60///
61/// iteration-statement:
62/// while-statement
63/// do-statement
64/// for-statement
65///
Chris Lattner9075bd72006-08-10 04:59:57 +000066/// expression-statement:
67/// expression[opt] ';'
68///
Chris Lattner0ccd51e2006-08-09 05:47:47 +000069/// jump-statement:
70/// 'goto' identifier ';'
71/// 'continue' ';'
72/// 'break' ';'
73/// 'return' expression[opt] ';'
Chris Lattner503fadc2006-08-10 05:45:44 +000074/// [GNU] 'goto' '*' expression ';'
Chris Lattner0ccd51e2006-08-09 05:47:47 +000075///
Fariborz Jahanian90814572007-10-04 20:19:06 +000076/// [OBC] objc-throw-statement:
77/// [OBC] '@' 'throw' expression ';'
Mike Stump11289f42009-09-09 15:08:12 +000078/// [OBC] '@' 'throw' ';'
79///
John McCalldadc5752010-08-24 06:29:42 +000080StmtResult
Nico Weber3cef1082011-12-22 23:26:17 +000081Parser::ParseStatementOrDeclaration(StmtVector &Stmts, bool OnlyStatement,
82 SourceLocation *TrailingElseLoc) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +000083
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +000084 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +000085
Richard Smithc202b282012-04-14 00:33:13 +000086 ParsedAttributesWithRange Attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +000087 MaybeParseCXX11Attributes(Attrs, 0, /*MightBeObjCMessageSend*/ true);
Richard Smithc202b282012-04-14 00:33:13 +000088
89 StmtResult Res = ParseStatementOrDeclarationAfterAttributes(Stmts,
90 OnlyStatement, TrailingElseLoc, Attrs);
91
92 assert((Attrs.empty() || Res.isInvalid() || Res.isUsable()) &&
93 "attributes on empty statement");
94
95 if (Attrs.empty() || Res.isInvalid())
96 return Res;
97
98 return Actions.ProcessStmtAttributes(Res.get(), Attrs.getList(), Attrs.Range);
99}
100
101StmtResult
102Parser::ParseStatementOrDeclarationAfterAttributes(StmtVector &Stmts,
103 bool OnlyStatement, SourceLocation *TrailingElseLoc,
104 ParsedAttributesWithRange &Attrs) {
105 const char *SemiError = 0;
106 StmtResult Res;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000107
Chris Lattner503fadc2006-08-10 05:45:44 +0000108 // Cases in this switch statement should fall through if the parser expects
109 // the token to end in a semicolon (in which case SemiError should be set),
110 // or they directly 'return;' if not.
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000111Retry:
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000112 tok::TokenKind Kind = Tok.getKind();
113 SourceLocation AtLoc;
114 switch (Kind) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000115 case tok::at: // May be a @try or @throw statement
116 {
Richard Smithc202b282012-04-14 00:33:13 +0000117 ProhibitAttributes(Attrs); // TODO: is it correct?
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000118 AtLoc = ConsumeToken(); // consume @
Sebastian Redlbab9a4b2008-12-11 20:12:42 +0000119 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000120 }
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +0000121
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000122 case tok::code_completion:
John McCallfaf5fb42010-08-26 23:41:50 +0000123 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000124 cutOffParsing();
125 return StmtError();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000126
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000127 case tok::identifier: {
128 Token Next = NextToken();
129 if (Next.is(tok::colon)) { // C99 6.8.1: labeled-statement
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000130 // identifier ':' statement
Richard Smithc202b282012-04-14 00:33:13 +0000131 return ParseLabeledStatement(Attrs);
Argyrios Kyrtzidis07b8b632008-07-12 21:04:42 +0000132 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000133
Richard Smith4f605af2012-08-18 00:55:03 +0000134 // Look up the identifier, and typo-correct it to a keyword if it's not
135 // found.
Douglas Gregor8b02cd02011-04-27 04:48:22 +0000136 if (Next.isNot(tok::coloncolon)) {
Richard Smith4f605af2012-08-18 00:55:03 +0000137 // Try to limit which sets of keywords should be included in typo
138 // correction based on what the next token is.
139 // FIXME: Pass the next token into the CorrectionCandidateCallback and
140 // do this filtering in a more fine-grained manner.
141 CorrectionCandidateCallback DefaultValidator;
142 DefaultValidator.WantTypeSpecifiers =
143 Next.is(tok::l_paren) || Next.is(tok::less) ||
144 Next.is(tok::identifier) || Next.is(tok::star) ||
145 Next.is(tok::amp) || Next.is(tok::l_square);
146 DefaultValidator.WantExpressionKeywords =
147 Next.is(tok::l_paren) || Next.is(tok::identifier) ||
148 Next.is(tok::arrow) || Next.is(tok::period);
149 DefaultValidator.WantRemainingKeywords =
150 Next.is(tok::l_paren) || Next.is(tok::semi) ||
151 Next.is(tok::identifier) || Next.is(tok::l_brace);
152 DefaultValidator.WantCXXNamedCasts = false;
153 if (TryAnnotateName(/*IsAddressOfOperand*/false, &DefaultValidator)
154 == ANK_Error) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000155 // Handle errors here by skipping up to the next semicolon or '}', and
156 // eat the semicolon if that's what stopped us.
157 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
158 if (Tok.is(tok::semi))
159 ConsumeToken();
160 return StmtError();
Richard Smith4f605af2012-08-18 00:55:03 +0000161 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000162
Richard Smith4f605af2012-08-18 00:55:03 +0000163 // If the identifier was typo-corrected, try again.
164 if (Tok.isNot(tok::identifier))
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000165 goto Retry;
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000166 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000167
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000168 // Fall through
169 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000170
Chris Lattner803802d2009-03-24 17:04:48 +0000171 default: {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000172 if ((getLangOpts().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner49836b42009-04-02 04:16:50 +0000173 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ted Kremenek5eec2b02010-11-10 05:59:39 +0000174 DeclGroupPtrTy Decl = ParseDeclaration(Stmts, Declarator::BlockContext,
Richard Smithc202b282012-04-14 00:33:13 +0000175 DeclEnd, Attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000176 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattner803802d2009-03-24 17:04:48 +0000177 }
178
179 if (Tok.is(tok::r_brace)) {
Chris Lattnerf8afb622006-08-10 18:26:31 +0000180 Diag(Tok, diag::err_expected_statement);
Sebastian Redl042ad952008-12-11 19:30:53 +0000181 return StmtError();
Chris Lattnerf8afb622006-08-10 18:26:31 +0000182 }
Mike Stump11289f42009-09-09 15:08:12 +0000183
Richard Smithc202b282012-04-14 00:33:13 +0000184 return ParseExprStatement();
Chris Lattner803802d2009-03-24 17:04:48 +0000185 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000186
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000187 case tok::kw_case: // C99 6.8.1: labeled-statement
Richard Smithc202b282012-04-14 00:33:13 +0000188 return ParseCaseStatement();
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000189 case tok::kw_default: // C99 6.8.1: labeled-statement
Richard Smithc202b282012-04-14 00:33:13 +0000190 return ParseDefaultStatement();
Sebastian Redl042ad952008-12-11 19:30:53 +0000191
Chris Lattner9075bd72006-08-10 04:59:57 +0000192 case tok::l_brace: // C99 6.8.2: compound-statement
Richard Smithc202b282012-04-14 00:33:13 +0000193 return ParseCompoundStatement();
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000194 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
Argyrios Kyrtzidis43ea78b2011-09-01 21:53:45 +0000195 bool HasLeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
196 return Actions.ActOnNullStmt(ConsumeToken(), HasLeadingEmptyMacro);
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000197 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000198
Chris Lattner9075bd72006-08-10 04:59:57 +0000199 case tok::kw_if: // C99 6.8.4.1: if-statement
Richard Smithc202b282012-04-14 00:33:13 +0000200 return ParseIfStatement(TrailingElseLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000201 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Richard Smithc202b282012-04-14 00:33:13 +0000202 return ParseSwitchStatement(TrailingElseLoc);
Sebastian Redl042ad952008-12-11 19:30:53 +0000203
Chris Lattner9075bd72006-08-10 04:59:57 +0000204 case tok::kw_while: // C99 6.8.5.1: while-statement
Richard Smithc202b282012-04-14 00:33:13 +0000205 return ParseWhileStatement(TrailingElseLoc);
Chris Lattner9075bd72006-08-10 04:59:57 +0000206 case tok::kw_do: // C99 6.8.5.2: do-statement
Richard Smithc202b282012-04-14 00:33:13 +0000207 Res = ParseDoStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000208 SemiError = "do/while";
Chris Lattner9075bd72006-08-10 04:59:57 +0000209 break;
210 case tok::kw_for: // C99 6.8.5.3: for-statement
Richard Smithc202b282012-04-14 00:33:13 +0000211 return ParseForStatement(TrailingElseLoc);
Chris Lattner503fadc2006-08-10 05:45:44 +0000212
213 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Richard Smithc202b282012-04-14 00:33:13 +0000214 Res = ParseGotoStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000215 SemiError = "goto";
Chris Lattner9075bd72006-08-10 04:59:57 +0000216 break;
Chris Lattner503fadc2006-08-10 05:45:44 +0000217 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Richard Smithc202b282012-04-14 00:33:13 +0000218 Res = ParseContinueStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000219 SemiError = "continue";
Chris Lattner503fadc2006-08-10 05:45:44 +0000220 break;
221 case tok::kw_break: // C99 6.8.6.3: break-statement
Richard Smithc202b282012-04-14 00:33:13 +0000222 Res = ParseBreakStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000223 SemiError = "break";
Chris Lattner503fadc2006-08-10 05:45:44 +0000224 break;
225 case tok::kw_return: // C99 6.8.6.4: return-statement
Richard Smithc202b282012-04-14 00:33:13 +0000226 Res = ParseReturnStatement();
Chris Lattner34a95662009-06-14 00:07:48 +0000227 SemiError = "return";
Chris Lattner503fadc2006-08-10 05:45:44 +0000228 break;
Sebastian Redl042ad952008-12-11 19:30:53 +0000229
Sebastian Redlb219c902008-12-21 16:41:36 +0000230 case tok::kw_asm: {
Richard Smithc202b282012-04-14 00:33:13 +0000231 ProhibitAttributes(Attrs);
Steve Naroffb2c80c72008-02-07 03:50:06 +0000232 bool msAsm = false;
233 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidis3050d9b2010-11-02 02:33:08 +0000234 Res = Actions.ActOnFinishFullStmt(Res.get());
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000235 if (msAsm) return Res;
Chris Lattner34a95662009-06-14 00:07:48 +0000236 SemiError = "asm";
Chris Lattner0116c472006-08-15 06:03:28 +0000237 break;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000238 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000239
Sebastian Redlb219c902008-12-21 16:41:36 +0000240 case tok::kw_try: // C++ 15: try-block
Richard Smithc202b282012-04-14 00:33:13 +0000241 return ParseCXXTryBlock();
John Wiegley1c0675e2011-04-28 01:08:34 +0000242
243 case tok::kw___try:
Richard Smithc202b282012-04-14 00:33:13 +0000244 ProhibitAttributes(Attrs); // TODO: is it correct?
245 return ParseSEHTryBlock();
Eli Friedmanec52f922012-02-23 23:47:16 +0000246
247 case tok::annot_pragma_vis:
Richard Smithc202b282012-04-14 00:33:13 +0000248 ProhibitAttributes(Attrs);
Eli Friedmanec52f922012-02-23 23:47:16 +0000249 HandlePragmaVisibility();
250 return StmtEmpty();
251
252 case tok::annot_pragma_pack:
Richard Smithc202b282012-04-14 00:33:13 +0000253 ProhibitAttributes(Attrs);
Eli Friedmanec52f922012-02-23 23:47:16 +0000254 HandlePragmaPack();
255 return StmtEmpty();
Eli Friedman68be1642012-10-04 02:36:51 +0000256
Eli Friedmanbbbbac62012-10-09 22:46:54 +0000257 case tok::annot_pragma_msstruct:
258 ProhibitAttributes(Attrs);
259 HandlePragmaMSStruct();
260 return StmtEmpty();
261
Eli Friedmanae8ee252012-10-08 23:52:38 +0000262 case tok::annot_pragma_align:
263 ProhibitAttributes(Attrs);
264 HandlePragmaAlign();
265 return StmtEmpty();
266
Eli Friedmanbbbbac62012-10-09 22:46:54 +0000267 case tok::annot_pragma_weak:
268 ProhibitAttributes(Attrs);
269 HandlePragmaWeak();
270 return StmtEmpty();
271
272 case tok::annot_pragma_weakalias:
273 ProhibitAttributes(Attrs);
274 HandlePragmaWeakAlias();
275 return StmtEmpty();
276
277 case tok::annot_pragma_redefine_extname:
278 ProhibitAttributes(Attrs);
279 HandlePragmaRedefineExtname();
280 return StmtEmpty();
281
Eli Friedman68be1642012-10-04 02:36:51 +0000282 case tok::annot_pragma_fp_contract:
Lang Hamesa930e712012-10-21 01:10:01 +0000283 Diag(Tok, diag::err_pragma_fp_contract_scope);
284 ConsumeToken();
285 return StmtError();
286
Eli Friedman68be1642012-10-04 02:36:51 +0000287 case tok::annot_pragma_opencl_extension:
288 ProhibitAttributes(Attrs);
289 HandlePragmaOpenCLExtension();
290 return StmtEmpty();
Alexey Bataeva769e072013-03-22 06:34:35 +0000291
292 case tok::annot_pragma_openmp:
293 SourceLocation DeclStart = Tok.getLocation();
294 DeclGroupPtrTy Res = ParseOpenMPDeclarativeDirective();
295 return Actions.ActOnDeclStmt(Res, DeclStart, Tok.getLocation());
Sebastian Redlb219c902008-12-21 16:41:36 +0000296 }
297
Chris Lattner503fadc2006-08-10 05:45:44 +0000298 // If we reached this code, the statement must end in a semicolon.
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000299 if (Tok.is(tok::semi)) {
Chris Lattner503fadc2006-08-10 05:45:44 +0000300 ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000301 } else if (!Res.isInvalid()) {
Chris Lattner8e3eed02009-06-14 00:23:56 +0000302 // If the result was valid, then we do want to diagnose this. Use
303 // ExpectAndConsume to emit the diagnostic, even though we know it won't
304 // succeed.
305 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner0046de12008-11-13 18:52:53 +0000306 // Skip until we see a } or ;, but don't eat it.
307 SkipUntil(tok::r_brace, true, true);
Chris Lattner503fadc2006-08-10 05:45:44 +0000308 }
Mike Stump11289f42009-09-09 15:08:12 +0000309
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000310 return Res;
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000311}
312
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000313/// \brief Parse an expression statement.
Richard Smithc202b282012-04-14 00:33:13 +0000314StmtResult Parser::ParseExprStatement() {
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000315 // If a case keyword is missing, this is where it should be inserted.
316 Token OldToken = Tok;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000317
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000318 // expression[opt] ';'
Douglas Gregorda6c89d2011-04-27 06:18:01 +0000319 ExprResult Expr(ParseExpression());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000320 if (Expr.isInvalid()) {
321 // If the expression is invalid, skip ahead to the next semicolon or '}'.
322 // Not doing this opens us up to the possibility of infinite loops if
323 // ParseExpression does not consume any tokens.
324 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
325 if (Tok.is(tok::semi))
326 ConsumeToken();
John McCalleaef89b2013-03-22 02:10:40 +0000327 return Actions.ActOnExprStmtError();
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000328 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000329
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000330 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
331 Actions.CheckCaseExpression(Expr.get())) {
332 // If a constant expression is followed by a colon inside a switch block,
333 // suggest a missing case keyword.
334 Diag(OldToken, diag::err_expected_case_before_expression)
335 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000336
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000337 // Recover parsing as a case statement.
Richard Smithc202b282012-04-14 00:33:13 +0000338 return ParseCaseStatement(/*MissingCase=*/true, Expr);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000339 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000340
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000341 // Otherwise, eat the semicolon.
342 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +0000343 return Actions.ActOnExprStmt(Expr);
John Wiegley1c0675e2011-04-28 01:08:34 +0000344}
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000345
Richard Smithc202b282012-04-14 00:33:13 +0000346StmtResult Parser::ParseSEHTryBlock() {
John Wiegley1c0675e2011-04-28 01:08:34 +0000347 assert(Tok.is(tok::kw___try) && "Expected '__try'");
348 SourceLocation Loc = ConsumeToken();
349 return ParseSEHTryBlockCommon(Loc);
350}
351
352/// ParseSEHTryBlockCommon
353///
354/// seh-try-block:
355/// '__try' compound-statement seh-handler
356///
357/// seh-handler:
358/// seh-except-block
359/// seh-finally-block
360///
361StmtResult Parser::ParseSEHTryBlockCommon(SourceLocation TryLoc) {
362 if(Tok.isNot(tok::l_brace))
363 return StmtError(Diag(Tok,diag::err_expected_lbrace));
364
Joao Matos566359c2012-09-04 17:49:35 +0000365 StmtResult TryBlock(ParseCompoundStatement());
John Wiegley1c0675e2011-04-28 01:08:34 +0000366 if(TryBlock.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000367 return TryBlock;
John Wiegley1c0675e2011-04-28 01:08:34 +0000368
369 StmtResult Handler;
Richard Smithc202b282012-04-14 00:33:13 +0000370 if (Tok.is(tok::identifier) &&
Douglas Gregor60060d62011-10-21 03:57:52 +0000371 Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +0000372 SourceLocation Loc = ConsumeToken();
373 Handler = ParseSEHExceptBlock(Loc);
374 } else if (Tok.is(tok::kw___finally)) {
375 SourceLocation Loc = ConsumeToken();
376 Handler = ParseSEHFinallyBlock(Loc);
377 } else {
378 return StmtError(Diag(Tok,diag::err_seh_expected_handler));
379 }
380
381 if(Handler.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000382 return Handler;
John Wiegley1c0675e2011-04-28 01:08:34 +0000383
384 return Actions.ActOnSEHTryBlock(false /* IsCXXTry */,
385 TryLoc,
386 TryBlock.take(),
387 Handler.take());
388}
389
390/// ParseSEHExceptBlock - Handle __except
391///
392/// seh-except-block:
393/// '__except' '(' seh-filter-expression ')' compound-statement
394///
395StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) {
396 PoisonIdentifierRAIIObject raii(Ident__exception_code, false),
397 raii2(Ident___exception_code, false),
398 raii3(Ident_GetExceptionCode, false);
399
400 if(ExpectAndConsume(tok::l_paren,diag::err_expected_lparen))
401 return StmtError();
402
403 ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope);
404
David Blaikiebbafb8a2012-03-11 07:00:24 +0000405 if (getLangOpts().Borland) {
Francois Pichetbfaf4772011-04-28 03:14:31 +0000406 Ident__exception_info->setIsPoisoned(false);
407 Ident___exception_info->setIsPoisoned(false);
408 Ident_GetExceptionInfo->setIsPoisoned(false);
409 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000410 ExprResult FilterExpr(ParseExpression());
Francois Pichetbfaf4772011-04-28 03:14:31 +0000411
David Blaikiebbafb8a2012-03-11 07:00:24 +0000412 if (getLangOpts().Borland) {
Francois Pichetbfaf4772011-04-28 03:14:31 +0000413 Ident__exception_info->setIsPoisoned(true);
414 Ident___exception_info->setIsPoisoned(true);
415 Ident_GetExceptionInfo->setIsPoisoned(true);
416 }
John Wiegley1c0675e2011-04-28 01:08:34 +0000417
418 if(FilterExpr.isInvalid())
419 return StmtError();
420
421 if(ExpectAndConsume(tok::r_paren,diag::err_expected_rparen))
422 return StmtError();
423
Richard Smithc202b282012-04-14 00:33:13 +0000424 StmtResult Block(ParseCompoundStatement());
John Wiegley1c0675e2011-04-28 01:08:34 +0000425
426 if(Block.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000427 return Block;
John Wiegley1c0675e2011-04-28 01:08:34 +0000428
429 return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.take(), Block.take());
430}
431
432/// ParseSEHFinallyBlock - Handle __finally
433///
434/// seh-finally-block:
435/// '__finally' compound-statement
436///
437StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyBlock) {
438 PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
439 raii2(Ident___abnormal_termination, false),
440 raii3(Ident_AbnormalTermination, false);
441
Richard Smithc202b282012-04-14 00:33:13 +0000442 StmtResult Block(ParseCompoundStatement());
John Wiegley1c0675e2011-04-28 01:08:34 +0000443 if(Block.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000444 return Block;
John Wiegley1c0675e2011-04-28 01:08:34 +0000445
446 return Actions.ActOnSEHFinallyBlock(FinallyBlock,Block.take());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000447}
448
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000449/// ParseLabeledStatement - We have an identifier and a ':' after it.
Chris Lattner6dfd9782006-08-10 18:31:37 +0000450///
451/// labeled-statement:
452/// identifier ':' statement
Chris Lattnere37e2332006-08-15 04:50:22 +0000453/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000454///
Richard Smithc202b282012-04-14 00:33:13 +0000455StmtResult Parser::ParseLabeledStatement(ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000456 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
457 "Not an identifier!");
458
459 Token IdentTok = Tok; // Save the whole token.
460 ConsumeToken(); // eat the identifier.
461
462 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000463
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000464 // identifier ':' statement
465 SourceLocation ColonLoc = ConsumeToken();
466
Richard Smithc202b282012-04-14 00:33:13 +0000467 // Read label attributes, if present. attrs will contain both C++11 and GNU
468 // attributes (if present) after this point.
John McCall53fa7142010-12-24 02:08:15 +0000469 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000470
John McCalldadc5752010-08-24 06:29:42 +0000471 StmtResult SubStmt(ParseStatement());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000472
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000473 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000474 if (SubStmt.isInvalid())
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000475 SubStmt = Actions.ActOnNullStmt(ColonLoc);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000476
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000477 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
478 IdentTok.getLocation());
Richard Smithc202b282012-04-14 00:33:13 +0000479 if (AttributeList *Attrs = attrs.getList()) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000480 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
Richard Smithc202b282012-04-14 00:33:13 +0000481 attrs.clear();
482 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000483
Chris Lattnerebb5c6c2011-02-18 01:27:55 +0000484 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
485 SubStmt.get());
Argyrios Kyrtzidis832e8982008-07-09 22:53:07 +0000486}
Chris Lattnerf8afb622006-08-10 18:26:31 +0000487
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000488/// ParseCaseStatement
489/// labeled-statement:
490/// 'case' constant-expression ':' statement
Chris Lattner476c3ad2006-08-13 22:09:58 +0000491/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
Chris Lattner8693a512006-08-13 21:54:02 +0000492///
Richard Smithc202b282012-04-14 00:33:13 +0000493StmtResult Parser::ParseCaseStatement(bool MissingCase, ExprResult Expr) {
Richard Smithd4257d82011-04-21 22:48:40 +0000494 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Mike Stump11289f42009-09-09 15:08:12 +0000495
Chris Lattner34a22092009-03-04 04:23:07 +0000496 // It is very very common for code to contain many case statements recursively
497 // nested, as in (but usually without indentation):
498 // case 1:
499 // case 2:
500 // case 3:
501 // case 4:
502 // case 5: etc.
503 //
504 // Parsing this naively works, but is both inefficient and can cause us to run
505 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner2b19a6582009-03-04 18:24:58 +0000506 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner34a22092009-03-04 04:23:07 +0000507 // but all the grossness is constrained to ParseCaseStatement (and some
508 // wierdness in the actions), so this is just local grossness :).
Mike Stump11289f42009-09-09 15:08:12 +0000509
Chris Lattner34a22092009-03-04 04:23:07 +0000510 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
511 // example above.
John McCalldadc5752010-08-24 06:29:42 +0000512 StmtResult TopLevelCase(true);
Mike Stump11289f42009-09-09 15:08:12 +0000513
Chris Lattner34a22092009-03-04 04:23:07 +0000514 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
515 // gets updated each time a new case is parsed, and whose body is unset so
516 // far. When parsing 'case 4', this is the 'case 3' node.
Richard Trieu3481fcd2011-09-09 02:16:15 +0000517 Stmt *DeepestParsedCaseStmt = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000518
Chris Lattner34a22092009-03-04 04:23:07 +0000519 // While we have case statements, eat and stack them.
David Majnemer0ac67fa2011-06-13 05:50:12 +0000520 SourceLocation ColonLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000521 do {
Richard Trieu2c850c02011-04-21 21:44:26 +0000522 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
523 ConsumeToken(); // eat the 'case'.
Mike Stump11289f42009-09-09 15:08:12 +0000524
Douglas Gregord328d572009-09-21 18:10:23 +0000525 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000526 Actions.CodeCompleteCase(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000527 cutOffParsing();
528 return StmtError();
Douglas Gregord328d572009-09-21 18:10:23 +0000529 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000530
Chris Lattner125c0ee2009-12-10 00:38:54 +0000531 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
532 /// Disable this form of error recovery while we're parsing the case
533 /// expression.
534 ColonProtectionRAIIObject ColonProtection(*this);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000535
Richard Trieu2c850c02011-04-21 21:44:26 +0000536 ExprResult LHS(MissingCase ? Expr : ParseConstantExpression());
537 MissingCase = false;
Chris Lattner34a22092009-03-04 04:23:07 +0000538 if (LHS.isInvalid()) {
Chris Lattner476c3ad2006-08-13 22:09:58 +0000539 SkipUntil(tok::colon);
Sebastian Redl042ad952008-12-11 19:30:53 +0000540 return StmtError();
Chris Lattner476c3ad2006-08-13 22:09:58 +0000541 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000542
Chris Lattner34a22092009-03-04 04:23:07 +0000543 // GNU case range extension.
544 SourceLocation DotDotDotLoc;
John McCalldadc5752010-08-24 06:29:42 +0000545 ExprResult RHS;
Chris Lattner34a22092009-03-04 04:23:07 +0000546 if (Tok.is(tok::ellipsis)) {
547 Diag(Tok, diag::ext_gnu_case_range);
548 DotDotDotLoc = ConsumeToken();
Sebastian Redl042ad952008-12-11 19:30:53 +0000549
Chris Lattner34a22092009-03-04 04:23:07 +0000550 RHS = ParseConstantExpression();
551 if (RHS.isInvalid()) {
552 SkipUntil(tok::colon);
553 return StmtError();
554 }
555 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000556
Chris Lattner125c0ee2009-12-10 00:38:54 +0000557 ColonProtection.restore();
Sebastian Redl042ad952008-12-11 19:30:53 +0000558
John McCall0140bfe2011-01-22 09:28:32 +0000559 if (Tok.is(tok::colon)) {
560 ColonLoc = ConsumeToken();
561
562 // Treat "case blah;" as a typo for "case blah:".
563 } else if (Tok.is(tok::semi)) {
564 ColonLoc = ConsumeToken();
565 Diag(ColonLoc, diag::err_expected_colon_after) << "'case'"
566 << FixItHint::CreateReplacement(ColonLoc, ":");
567 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000568 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
569 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'"
570 << FixItHint::CreateInsertion(ExpectedLoc, ":");
571 ColonLoc = ExpectedLoc;
Chris Lattner34a22092009-03-04 04:23:07 +0000572 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000573
John McCalldadc5752010-08-24 06:29:42 +0000574 StmtResult Case =
John McCallb268a282010-08-23 23:25:46 +0000575 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
576 RHS.get(), ColonLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000577
Chris Lattner34a22092009-03-04 04:23:07 +0000578 // If we had a sema error parsing this case, then just ignore it and
579 // continue parsing the sub-stmt.
580 if (Case.isInvalid()) {
581 if (TopLevelCase.isInvalid()) // No parsed case stmts.
582 return ParseStatement();
583 // Otherwise, just don't add it as a nested case.
584 } else {
585 // If this is the first case statement we parsed, it becomes TopLevelCase.
586 // Otherwise we link it into the current chain.
John McCall37ad5512010-08-23 06:44:23 +0000587 Stmt *NextDeepest = Case.get();
Chris Lattner34a22092009-03-04 04:23:07 +0000588 if (TopLevelCase.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000589 TopLevelCase = Case;
Chris Lattner34a22092009-03-04 04:23:07 +0000590 else
John McCallb268a282010-08-23 23:25:46 +0000591 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner34a22092009-03-04 04:23:07 +0000592 DeepestParsedCaseStmt = NextDeepest;
593 }
Mike Stump11289f42009-09-09 15:08:12 +0000594
Chris Lattner34a22092009-03-04 04:23:07 +0000595 // Handle all case statements.
596 } while (Tok.is(tok::kw_case));
Mike Stump11289f42009-09-09 15:08:12 +0000597
Chris Lattner34a22092009-03-04 04:23:07 +0000598 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump11289f42009-09-09 15:08:12 +0000599
Chris Lattner34a22092009-03-04 04:23:07 +0000600 // If we found a non-case statement, start by parsing it.
John McCalldadc5752010-08-24 06:29:42 +0000601 StmtResult SubStmt;
Mike Stump11289f42009-09-09 15:08:12 +0000602
Chris Lattner34a22092009-03-04 04:23:07 +0000603 if (Tok.isNot(tok::r_brace)) {
604 SubStmt = ParseStatement();
605 } else {
606 // Nicely diagnose the common error "switch (X) { case 4: }", which is
607 // not valid.
David Majnemerc6a99872011-06-14 15:24:38 +0000608 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
Richard Smith1002d102012-02-17 01:35:32 +0000609 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
610 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
Chris Lattner34a22092009-03-04 04:23:07 +0000611 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000612 }
Mike Stump11289f42009-09-09 15:08:12 +0000613
Chris Lattner34a22092009-03-04 04:23:07 +0000614 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000615 if (SubStmt.isInvalid())
Chris Lattner34a22092009-03-04 04:23:07 +0000616 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000617
Chris Lattner34a22092009-03-04 04:23:07 +0000618 // Install the body into the most deeply-nested case.
John McCallb268a282010-08-23 23:25:46 +0000619 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl042ad952008-12-11 19:30:53 +0000620
Chris Lattner34a22092009-03-04 04:23:07 +0000621 // Return the top level parsed statement tree.
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000622 return TopLevelCase;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000623}
624
625/// ParseDefaultStatement
626/// labeled-statement:
627/// 'default' ':' statement
628/// Note that this does not parse the 'statement' at the end.
629///
Richard Smithc202b282012-04-14 00:33:13 +0000630StmtResult Parser::ParseDefaultStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000631 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000632 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000633
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000634 SourceLocation ColonLoc;
John McCall0140bfe2011-01-22 09:28:32 +0000635 if (Tok.is(tok::colon)) {
636 ColonLoc = ConsumeToken();
637
638 // Treat "default;" as a typo for "default:".
639 } else if (Tok.is(tok::semi)) {
640 ColonLoc = ConsumeToken();
641 Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
642 << FixItHint::CreateReplacement(ColonLoc, ":");
643 } else {
Douglas Gregor0d0a9652010-12-23 22:56:40 +0000644 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
645 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
646 << FixItHint::CreateInsertion(ExpectedLoc, ":");
647 ColonLoc = ExpectedLoc;
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000648 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000649
Richard Smith1002d102012-02-17 01:35:32 +0000650 StmtResult SubStmt;
651
652 if (Tok.isNot(tok::r_brace)) {
653 SubStmt = ParseStatement();
654 } else {
655 // Diagnose the common error "switch (X) {... default: }", which is
656 // not valid.
David Majnemerc6a99872011-06-14 15:24:38 +0000657 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
Richard Smith1002d102012-02-17 01:35:32 +0000658 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
659 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
660 SubStmt = true;
Chris Lattner30f910e2006-10-16 05:52:41 +0000661 }
662
Richard Smith1002d102012-02-17 01:35:32 +0000663 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000664 if (SubStmt.isInvalid())
Richard Smith1002d102012-02-17 01:35:32 +0000665 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl042ad952008-12-11 19:30:53 +0000666
Sebastian Redl1cbb59182008-12-28 16:13:43 +0000667 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCallb268a282010-08-23 23:25:46 +0000668 SubStmt.get(), getCurScope());
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000669}
670
Richard Smithc202b282012-04-14 00:33:13 +0000671StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
672 return ParseCompoundStatement(isStmtExpr, Scope::DeclScope);
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000673}
Chris Lattnerd2685cf2006-08-10 05:59:48 +0000674
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000675/// ParseCompoundStatement - Parse a "{}" block.
676///
677/// compound-statement: [C99 6.8.2]
678/// { block-item-list[opt] }
679/// [GNU] { label-declarations block-item-list } [TODO]
680///
681/// block-item-list:
682/// block-item
683/// block-item-list block-item
684///
685/// block-item:
686/// declaration
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000687/// [GNU] '__extension__' declaration
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000688/// statement
689/// [OMP] openmp-directive [TODO]
690///
691/// [GNU] label-declarations:
692/// [GNU] label-declaration
693/// [GNU] label-declarations label-declaration
694///
695/// [GNU] label-declaration:
696/// [GNU] '__label__' identifier-list ';'
697///
698/// [OMP] openmp-directive: [TODO]
699/// [OMP] barrier-directive
700/// [OMP] flush-directive
Chris Lattner30f910e2006-10-16 05:52:41 +0000701///
Richard Smithc202b282012-04-14 00:33:13 +0000702StmtResult Parser::ParseCompoundStatement(bool isStmtExpr,
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000703 unsigned ScopeFlags) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000704 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl042ad952008-12-11 19:30:53 +0000705
Chris Lattner1a76a3c2007-08-26 06:24:45 +0000706 // Enter a scope to hold everything within the compound stmt. Compound
707 // statements can always hold declarations.
Douglas Gregor53e191ed2011-07-06 22:04:06 +0000708 ParseScope CompoundScope(this, ScopeFlags);
Chris Lattnerf2978802007-01-21 06:52:16 +0000709
710 // Parse the statements in the body.
Sebastian Redl042ad952008-12-11 19:30:53 +0000711 return ParseCompoundStatementBody(isStmtExpr);
Chris Lattnerf2978802007-01-21 06:52:16 +0000712}
713
Lang Hames2954cea2012-11-03 22:29:05 +0000714/// Parse any pragmas at the start of the compound expression. We handle these
715/// separately since some pragmas (FP_CONTRACT) must appear before any C
716/// statement in the compound, but may be intermingled with other pragmas.
717void Parser::ParseCompoundStatementLeadingPragmas() {
718 bool checkForPragmas = true;
719 while (checkForPragmas) {
720 switch (Tok.getKind()) {
721 case tok::annot_pragma_vis:
722 HandlePragmaVisibility();
723 break;
724 case tok::annot_pragma_pack:
725 HandlePragmaPack();
726 break;
727 case tok::annot_pragma_msstruct:
728 HandlePragmaMSStruct();
729 break;
730 case tok::annot_pragma_align:
731 HandlePragmaAlign();
732 break;
733 case tok::annot_pragma_weak:
734 HandlePragmaWeak();
735 break;
736 case tok::annot_pragma_weakalias:
737 HandlePragmaWeakAlias();
738 break;
739 case tok::annot_pragma_redefine_extname:
740 HandlePragmaRedefineExtname();
741 break;
742 case tok::annot_pragma_opencl_extension:
743 HandlePragmaOpenCLExtension();
744 break;
745 case tok::annot_pragma_fp_contract:
746 HandlePragmaFPContract();
747 break;
748 default:
749 checkForPragmas = false;
750 break;
751 }
752 }
753
754}
755
Chris Lattnerf2978802007-01-21 06:52:16 +0000756/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff66356bd2007-09-16 14:56:35 +0000757/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Chris Lattnerf2978802007-01-21 06:52:16 +0000758/// consume the '}' at the end of the block. It does not manipulate the scope
759/// stack.
John McCalldadc5752010-08-24 06:29:42 +0000760StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump11289f42009-09-09 15:08:12 +0000761 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerbd61a952009-03-05 00:00:31 +0000762 Tok.getLocation(),
763 "in compound statement ('{}')");
Lang Hames5de91cc2012-10-02 04:45:10 +0000764
765 // Record the state of the FP_CONTRACT pragma, restore on leaving the
766 // compound statement.
767 Sema::FPContractStateRAII SaveFPContractState(Actions);
768
Douglas Gregore9bba4f2010-09-15 14:51:05 +0000769 InMessageExpressionRAIIObject InMessage(*this, false);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000770 BalancedDelimiterTracker T(*this, tok::l_brace);
771 if (T.consumeOpen())
772 return StmtError();
Chris Lattnerf2978802007-01-21 06:52:16 +0000773
Dmitri Gribenko800ddf32012-02-14 22:14:32 +0000774 Sema::CompoundScopeRAII CompoundScope(Actions);
775
Lang Hames2954cea2012-11-03 22:29:05 +0000776 // Parse any pragmas at the beginning of the compound statement.
777 ParseCompoundStatementLeadingPragmas();
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000778
Lang Hames2954cea2012-11-03 22:29:05 +0000779 StmtVector Stmts;
Lang Hamesa930e712012-10-21 01:10:01 +0000780
Chris Lattner43e7f312011-02-18 02:08:43 +0000781 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
782 // only allowed at the start of a compound stmt regardless of the language.
783 while (Tok.is(tok::kw___label__)) {
784 SourceLocation LabelLoc = ConsumeToken();
785 Diag(LabelLoc, diag::ext_gnu_local_label);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000786
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000787 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner43e7f312011-02-18 02:08:43 +0000788 while (1) {
789 if (Tok.isNot(tok::identifier)) {
790 Diag(Tok, diag::err_expected_ident);
791 break;
792 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000793
Chris Lattner43e7f312011-02-18 02:08:43 +0000794 IdentifierInfo *II = Tok.getIdentifierInfo();
795 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara1c3af962011-03-05 18:21:20 +0000796 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000797
Chris Lattner43e7f312011-02-18 02:08:43 +0000798 if (!Tok.is(tok::comma))
799 break;
800 ConsumeToken();
801 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000802
John McCall084e83d2011-03-24 11:26:52 +0000803 DeclSpec DS(AttrFactory);
Chris Lattner43e7f312011-02-18 02:08:43 +0000804 DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
805 DeclsInGroup.data(), DeclsInGroup.size());
806 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000807
Chris Lattner02f1b612012-04-28 16:12:17 +0000808 ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
Chris Lattner43e7f312011-02-18 02:08:43 +0000809 if (R.isUsable())
810 Stmts.push_back(R.release());
811 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000812
Chris Lattner43e7f312011-02-18 02:08:43 +0000813 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000814 if (Tok.is(tok::annot_pragma_unused)) {
815 HandlePragmaUnused();
816 continue;
817 }
818
David Blaikiebbafb8a2012-03-11 07:00:24 +0000819 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet4a7de3e2011-05-06 20:48:22 +0000820 Tok.is(tok::kw___if_not_exists))) {
821 ParseMicrosoftIfExistsStatement(Stmts);
822 continue;
823 }
824
John McCalldadc5752010-08-24 06:29:42 +0000825 StmtResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000826 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000827 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000828 } else {
829 // __extension__ can start declarations and it can also be a unary
830 // operator for expressions. Consume multiple __extension__ markers here
831 // until we can determine which is which.
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000832 // FIXME: This loses extension expressions in the AST!
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000833 SourceLocation ExtLoc = ConsumeToken();
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000834 while (Tok.is(tok::kw___extension__))
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000835 ConsumeToken();
Chris Lattner1ff6e732008-10-20 06:51:33 +0000836
John McCall084e83d2011-03-24 11:26:52 +0000837 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +0000838 MaybeParseCXX11Attributes(attrs, 0, /*MightBeObjCMessageSend*/ true);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000839
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000840 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis2c7137d2008-10-05 00:06:24 +0000841 if (isDeclarationStatement()) {
Eli Friedman15af3ee2009-05-16 23:40:44 +0000842 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner49836b42009-04-02 04:16:50 +0000843 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedman15af3ee2009-05-16 23:40:44 +0000844 ExtensionRAIIObject O(Diags);
845
Chris Lattner49836b42009-04-02 04:16:50 +0000846 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000847 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
848 Declarator::BlockContext, DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000849 attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000850 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000851 } else {
Eli Friedmaneb3a9b02009-01-27 08:43:38 +0000852 // Otherwise this was a unary __extension__ marker.
John McCalldadc5752010-08-24 06:29:42 +0000853 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattnerfdc07482008-03-13 06:32:11 +0000854
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000855 if (Res.isInvalid()) {
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000856 SkipUntil(tok::semi);
857 continue;
858 }
Sebastian Redlc2edafb2009-01-18 18:03:53 +0000859
Alexis Hunt96d5c762009-11-21 08:43:09 +0000860 // FIXME: Use attributes?
Chris Lattner1ff6e732008-10-20 06:51:33 +0000861 // Eat the semicolon at the end of stmt and convert the expr into a
862 // statement.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +0000863 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith945f8d32013-01-14 22:39:08 +0000864 R = Actions.ActOnExprStmt(Res);
Chris Lattnerdfaf9f82007-08-27 01:01:57 +0000865 }
866 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000867
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000868 if (R.isUsable())
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000869 Stmts.push_back(R.release());
Chris Lattner30f910e2006-10-16 05:52:41 +0000870 }
Sebastian Redl042ad952008-12-11 19:30:53 +0000871
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +0000872 SourceLocation CloseLoc = Tok.getLocation();
873
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000874 // We broke out of the while loop because we found a '}' or EOF.
Nico Webera48b6c22012-12-30 23:36:56 +0000875 if (!T.consumeClose())
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +0000876 // Recover by creating a compound statement with what we parsed so far,
877 // instead of dropping everything and returning StmtError();
Nico Webera48b6c22012-12-30 23:36:56 +0000878 CloseLoc = T.getCloseLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +0000879
Argyrios Kyrtzidis6db85012012-03-24 02:26:51 +0000880 return Actions.ActOnCompoundStmt(T.getOpenLocation(), CloseLoc,
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000881 Stmts, isStmtExpr);
Chris Lattner0ccd51e2006-08-09 05:47:47 +0000882}
Chris Lattnerc951dae2006-08-10 04:23:57 +0000883
Chris Lattnerc0081db2008-12-12 06:31:07 +0000884/// ParseParenExprOrCondition:
885/// [C ] '(' expression ')'
Chris Lattner10da53c2008-12-12 06:35:28 +0000886/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattnerc0081db2008-12-12 06:31:07 +0000887///
888/// This function parses and performs error recovery on the specified condition
889/// or expression (depending on whether we're in C++ or C mode). This function
890/// goes out of its way to recover well. It returns true if there was a parser
891/// error (the right paren couldn't be found), which indicates that the caller
892/// should try to recover harder. It returns false if the condition is
893/// successfully parsed. Note that a successful parse can still have semantic
894/// errors in the condition.
John McCalldadc5752010-08-24 06:29:42 +0000895bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCall48871652010-08-21 09:40:31 +0000896 Decl *&DeclResult,
Douglas Gregore60e41a2010-05-06 17:25:47 +0000897 SourceLocation Loc,
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000898 bool ConvertToBoolean) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000899 BalancedDelimiterTracker T(*this, tok::l_paren);
900 T.consumeOpen();
901
David Blaikiebbafb8a2012-03-11 07:00:24 +0000902 if (getLangOpts().CPlusPlus)
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000903 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000904 else {
905 ExprResult = ParseExpression();
John McCall48871652010-08-21 09:40:31 +0000906 DeclResult = 0;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +0000907
Douglas Gregore60e41a2010-05-06 17:25:47 +0000908 // If required, convert to a boolean value.
909 if (!ExprResult.isInvalid() && ConvertToBoolean)
910 ExprResult
John McCallb268a282010-08-23 23:25:46 +0000911 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor7bab5ff2009-11-25 00:27:52 +0000912 }
Mike Stump11289f42009-09-09 15:08:12 +0000913
Chris Lattnerc0081db2008-12-12 06:31:07 +0000914 // If the parser was confused by the condition and we don't have a ')', try to
915 // recover by skipping ahead to a semi and bailing out. If condexp is
916 // semantically invalid but we have well formed code, keep going.
John McCall48871652010-08-21 09:40:31 +0000917 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattnerc0081db2008-12-12 06:31:07 +0000918 SkipUntil(tok::semi);
919 // Skipping may have stopped if it found the containing ')'. If so, we can
920 // continue parsing the if statement.
921 if (Tok.isNot(tok::r_paren))
922 return true;
923 }
Mike Stump11289f42009-09-09 15:08:12 +0000924
Chris Lattnerc0081db2008-12-12 06:31:07 +0000925 // Otherwise the condition is valid or the rparen is present.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000926 T.consumeClose();
Chad Rosier67055f52012-07-10 21:35:27 +0000927
Chris Lattner70d44982012-04-28 16:24:20 +0000928 // Check for extraneous ')'s to catch things like "if (foo())) {". We know
929 // that all callers are looking for a statement after the condition, so ")"
930 // isn't valid.
931 while (Tok.is(tok::r_paren)) {
932 Diag(Tok, diag::err_extraneous_rparen_in_condition)
933 << FixItHint::CreateRemoval(Tok.getLocation());
934 ConsumeParen();
935 }
Chad Rosier67055f52012-07-10 21:35:27 +0000936
Chris Lattnerc0081db2008-12-12 06:31:07 +0000937 return false;
938}
939
940
Chris Lattnerc951dae2006-08-10 04:23:57 +0000941/// ParseIfStatement
942/// if-statement: [C99 6.8.4.1]
943/// 'if' '(' expression ')' statement
944/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000945/// [C++] 'if' '(' condition ')' statement
946/// [C++] 'if' '(' condition ')' statement 'else' statement
Chris Lattner30f910e2006-10-16 05:52:41 +0000947///
Richard Smithc202b282012-04-14 00:33:13 +0000948StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000949 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +0000950 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
Chris Lattnerc951dae2006-08-10 04:23:57 +0000951
Chris Lattnerfeb00b62007-10-09 17:41:39 +0000952 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000953 Diag(Tok, diag::err_expected_lparen_after) << "if";
Chris Lattnerc951dae2006-08-10 04:23:57 +0000954 SkipUntil(tok::semi);
Sebastian Redl042ad952008-12-11 19:30:53 +0000955 return StmtError();
Chris Lattnerc951dae2006-08-10 04:23:57 +0000956 }
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +0000957
David Blaikiebbafb8a2012-03-11 07:00:24 +0000958 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000959
Chris Lattner2dd1b722007-08-26 23:08:06 +0000960 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
961 // the case for C90.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000962 //
963 // C++ 6.4p3:
964 // A name introduced by a declaration in a condition is in scope from its
965 // point of declaration until the end of the substatements controlled by the
966 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +0000967 // C++ 3.3.2p4:
968 // Names declared in the for-init-statement, and in the condition of if,
969 // while, for, and switch statements are local to the if, while, for, or
970 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000971 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +0000972 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner2dd1b722007-08-26 23:08:06 +0000973
Chris Lattnerc951dae2006-08-10 04:23:57 +0000974 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +0000975 ExprResult CondExp;
John McCall48871652010-08-21 09:40:31 +0000976 Decl *CondVar = 0;
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +0000977 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +0000978 return StmtError();
Chris Lattnerbc2d77c2008-12-12 06:19:11 +0000979
David Blaikiea5696df2012-05-16 04:20:04 +0000980 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get(), IfLoc));
Mike Stump11289f42009-09-09 15:08:12 +0000981
Chris Lattner8fb26252007-08-22 05:28:50 +0000982 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +0000983 // there is no compound stmt. C90 does not have this clause. We only do this
984 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +0000985 //
986 // C++ 6.4p1:
987 // The substatement in a selection-statement (each substatement, in the else
988 // form of the if statement) implicitly defines a local scope.
989 //
990 // For C++ we create a scope for the condition and a new scope for
991 // substatements because:
992 // -When the 'then' scope exits, we want the condition declaration to still be
993 // active for the 'else' scope too.
994 // -Sema will detect name clashes by considering declarations of a
995 // 'ControlScope' as part of its direct subscope.
996 // -If we wanted the condition and substatement to be in the same scope, we
997 // would have to notify ParseStatement not to create a new scope. It's
998 // simpler to let it create a new scope.
999 //
Mike Stump11289f42009-09-09 15:08:12 +00001000 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001001 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001002
Chris Lattner5c5808a2007-10-29 05:08:52 +00001003 // Read the 'then' stmt.
1004 SourceLocation ThenStmtLoc = Tok.getLocation();
Nico Weber3cef1082011-12-22 23:26:17 +00001005
1006 SourceLocation InnerStatementTrailingElseLoc;
1007 StmtResult ThenStmt(ParseStatement(&InnerStatementTrailingElseLoc));
Chris Lattnerac4471c2007-05-28 05:38:24 +00001008
Chris Lattner37e54f42007-08-22 05:16:28 +00001009 // Pop the 'if' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001010 InnerScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001011
Chris Lattnerc951dae2006-08-10 04:23:57 +00001012 // If it has an else, parse it.
Chris Lattner30f910e2006-10-16 05:52:41 +00001013 SourceLocation ElseLoc;
Chris Lattner5c5808a2007-10-29 05:08:52 +00001014 SourceLocation ElseStmtLoc;
John McCalldadc5752010-08-24 06:29:42 +00001015 StmtResult ElseStmt;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001016
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001017 if (Tok.is(tok::kw_else)) {
Nico Weber3cef1082011-12-22 23:26:17 +00001018 if (TrailingElseLoc)
1019 *TrailingElseLoc = Tok.getLocation();
1020
Chris Lattneraf635312006-10-16 06:06:51 +00001021 ElseLoc = ConsumeToken();
Chris Lattnerdf742642010-04-12 06:12:50 +00001022 ElseStmtLoc = Tok.getLocation();
Sebastian Redl042ad952008-12-11 19:30:53 +00001023
Chris Lattner8fb26252007-08-22 05:28:50 +00001024 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001025 // there is no compound stmt. C90 does not have this clause. We only do
1026 // this if the body isn't a compound statement to avoid push/pop in common
1027 // cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001028 //
1029 // C++ 6.4p1:
1030 // The substatement in a selection-statement (each substatement, in the else
1031 // form of the if statement) implicitly defines a local scope.
1032 //
Sebastian Redl042ad952008-12-11 19:30:53 +00001033 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001034 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001035
Chris Lattner30f910e2006-10-16 05:52:41 +00001036 ElseStmt = ParseStatement();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001037
Chris Lattner37e54f42007-08-22 05:16:28 +00001038 // Pop the 'else' scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001039 InnerScope.Exit();
Douglas Gregor4ecb7202011-07-30 08:36:53 +00001040 } else if (Tok.is(tok::code_completion)) {
1041 Actions.CodeCompleteAfterIf(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001042 cutOffParsing();
1043 return StmtError();
Nico Weber3cef1082011-12-22 23:26:17 +00001044 } else if (InnerStatementTrailingElseLoc.isValid()) {
1045 Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else);
Chris Lattnerc951dae2006-08-10 04:23:57 +00001046 }
Sebastian Redl042ad952008-12-11 19:30:53 +00001047
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001048 IfScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +00001049
Chris Lattner5c5808a2007-10-29 05:08:52 +00001050 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump11289f42009-09-09 15:08:12 +00001051 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattner5c5808a2007-10-29 05:08:52 +00001052 // part. If both are invalid, return error.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001053 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
1054 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
1055 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redl511ed552008-11-25 22:21:31 +00001056 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl042ad952008-12-11 19:30:53 +00001057 return StmtError();
Chris Lattner5c5808a2007-10-29 05:08:52 +00001058 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001059
Chris Lattner5c5808a2007-10-29 05:08:52 +00001060 // Now if either are invalid, replace with a ';'.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001061 if (ThenStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001062 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001063 if (ElseStmt.isInvalid())
Chris Lattner5c5808a2007-10-29 05:08:52 +00001064 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001065
John McCallb268a282010-08-23 23:25:46 +00001066 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidisde2bdf62010-11-20 02:04:01 +00001067 ElseLoc, ElseStmt.get());
Chris Lattnerc951dae2006-08-10 04:23:57 +00001068}
1069
Chris Lattner9075bd72006-08-10 04:59:57 +00001070/// ParseSwitchStatement
1071/// switch-statement:
1072/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001073/// [C++] 'switch' '(' condition ')' statement
Richard Smithc202b282012-04-14 00:33:13 +00001074StmtResult Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001075 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001076 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
Chris Lattner9075bd72006-08-10 04:59:57 +00001077
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001078 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001079 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Chris Lattner9075bd72006-08-10 04:59:57 +00001080 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001081 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001082 }
Chris Lattner2dd1b722007-08-26 23:08:06 +00001083
David Blaikiebbafb8a2012-03-11 07:00:24 +00001084 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001085
Chris Lattner2dd1b722007-08-26 23:08:06 +00001086 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1087 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001088 //
1089 // C++ 6.4p3:
1090 // A name introduced by a declaration in a condition is in scope from its
1091 // point of declaration until the end of the substatements controlled by the
1092 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001093 // C++ 3.3.2p4:
1094 // Names declared in the for-init-statement, and in the condition of if,
1095 // while, for, and switch statements are local to the if, while, for, or
1096 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001097 //
Richard Trieu2c850c02011-04-21 21:44:26 +00001098 unsigned ScopeFlags = Scope::BreakScope | Scope::SwitchScope;
Chris Lattnerc0081db2008-12-12 06:31:07 +00001099 if (C99orCXX)
1100 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001101 ParseScope SwitchScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001102
Chris Lattner9075bd72006-08-10 04:59:57 +00001103 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001104 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +00001105 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001106 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redlb62406f2008-12-11 19:48:14 +00001107 return StmtError();
Eli Friedman44842d12008-12-17 22:19:57 +00001108
John McCalldadc5752010-08-24 06:29:42 +00001109 StmtResult Switch
John McCallb268a282010-08-23 23:25:46 +00001110 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001111
Douglas Gregore60e41a2010-05-06 17:25:47 +00001112 if (Switch.isInvalid()) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001113 // Skip the switch body.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001114 // FIXME: This is not optimal recovery, but parsing the body is more
1115 // dangerous due to the presence of case and default statements, which
1116 // will have no place to connect back with the switch.
Douglas Gregor4abc32d2010-05-20 23:20:59 +00001117 if (Tok.is(tok::l_brace)) {
1118 ConsumeBrace();
1119 SkipUntil(tok::r_brace, false, false);
1120 } else
Douglas Gregore60e41a2010-05-06 17:25:47 +00001121 SkipUntil(tok::semi);
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001122 return Switch;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001123 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001124
Chris Lattner8fb26252007-08-22 05:28:50 +00001125 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001126 // there is no compound stmt. C90 does not have this clause. We only do this
1127 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001128 //
1129 // C++ 6.4p1:
1130 // The substatement in a selection-statement (each substatement, in the else
1131 // form of the if statement) implicitly defines a local scope.
1132 //
1133 // See comments in ParseIfStatement for why we create a scope for the
1134 // condition and a new scope for substatement in C++.
1135 //
Mike Stump11289f42009-09-09 15:08:12 +00001136 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001137 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl042ad952008-12-11 19:30:53 +00001138
Chris Lattner9075bd72006-08-10 04:59:57 +00001139 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001140 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001141
Chris Lattner8fd2d012010-01-24 01:50:29 +00001142 // Pop the scopes.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001143 InnerScope.Exit();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001144 SwitchScope.Exit();
Sebastian Redl042ad952008-12-11 19:30:53 +00001145
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001146 if (Body.isInvalid()) {
Chris Lattner8fd2d012010-01-24 01:50:29 +00001147 // FIXME: Remove the case statement list from the Switch statement.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001148
1149 // Put the synthesized null statement on the same line as the end of switch
1150 // condition.
1151 SourceLocation SynthesizedNullStmtLocation = Cond.get()->getLocEnd();
1152 Body = Actions.ActOnNullStmt(SynthesizedNullStmtLocation);
1153 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001154
John McCallb268a282010-08-23 23:25:46 +00001155 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001156}
1157
1158/// ParseWhileStatement
1159/// while-statement: [C99 6.8.5.1]
1160/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001161/// [C++] 'while' '(' condition ')' statement
Richard Smithc202b282012-04-14 00:33:13 +00001162StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001163 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Chris Lattner30f910e2006-10-16 05:52:41 +00001164 SourceLocation WhileLoc = Tok.getLocation();
Chris Lattner9075bd72006-08-10 04:59:57 +00001165 ConsumeToken(); // eat the 'while'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001166
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001167 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001168 Diag(Tok, diag::err_expected_lparen_after) << "while";
Chris Lattner9075bd72006-08-10 04:59:57 +00001169 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001170 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001171 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001172
David Blaikiebbafb8a2012-03-11 07:00:24 +00001173 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001174
Chris Lattner2dd1b722007-08-26 23:08:06 +00001175 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1176 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001177 //
1178 // C++ 6.4p3:
1179 // A name introduced by a declaration in a condition is in scope from its
1180 // point of declaration until the end of the substatements controlled by the
1181 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001182 // C++ 3.3.2p4:
1183 // Names declared in the for-init-statement, and in the condition of if,
1184 // while, for, and switch statements are local to the if, while, for, or
1185 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001186 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001187 unsigned ScopeFlags;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001188 if (C99orCXX)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001189 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1190 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001191 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001192 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1193 ParseScope WhileScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001194
Chris Lattner9075bd72006-08-10 04:59:57 +00001195 // Parse the condition.
John McCalldadc5752010-08-24 06:29:42 +00001196 ExprResult Cond;
John McCall48871652010-08-21 09:40:31 +00001197 Decl *CondVar = 0;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001198 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattnerc0081db2008-12-12 06:31:07 +00001199 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001200
David Blaikiea5696df2012-05-16 04:20:04 +00001201 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get(), WhileLoc));
Mike Stump11289f42009-09-09 15:08:12 +00001202
Chris Lattner8fb26252007-08-22 05:28:50 +00001203 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001204 // there is no compound stmt. C90 does not have this clause. We only do this
1205 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001206 //
1207 // C++ 6.5p2:
1208 // The substatement in an iteration-statement implicitly defines a local scope
1209 // which is entered and exited each time through the loop.
1210 //
1211 // See comments in ParseIfStatement for why we create a scope for the
1212 // condition and a new scope for substatement in C++.
1213 //
Mike Stump11289f42009-09-09 15:08:12 +00001214 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001215 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001216
Chris Lattner9075bd72006-08-10 04:59:57 +00001217 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001218 StmtResult Body(ParseStatement(TrailingElseLoc));
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001219
Chris Lattner8fb26252007-08-22 05:28:50 +00001220 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001221 InnerScope.Exit();
1222 WhileScope.Exit();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001223
John McCall48871652010-08-21 09:40:31 +00001224 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001225 return StmtError();
1226
John McCallb268a282010-08-23 23:25:46 +00001227 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Chris Lattner9075bd72006-08-10 04:59:57 +00001228}
1229
1230/// ParseDoStatement
1231/// do-statement: [C99 6.8.5.2]
1232/// 'do' statement 'while' '(' expression ')' ';'
Chris Lattner503fadc2006-08-10 05:45:44 +00001233/// Note: this lets the caller parse the end ';'.
Richard Smithc202b282012-04-14 00:33:13 +00001234StmtResult Parser::ParseDoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001235 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001236 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001237
Chris Lattner2dd1b722007-08-26 23:08:06 +00001238 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1239 // the case for C90. Start the loop scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001240 unsigned ScopeFlags;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001241 if (getLangOpts().C99)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001242 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001243 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001244 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redlb62406f2008-12-11 19:48:14 +00001245
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001246 ParseScope DoScope(this, ScopeFlags);
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001247
Chris Lattner8fb26252007-08-22 05:28:50 +00001248 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001249 // there is no compound stmt. C90 does not have this clause. We only do this
1250 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidisfea38012008-09-11 04:46:46 +00001251 //
1252 // C++ 6.5p2:
1253 // The substatement in an iteration-statement implicitly defines a local scope
1254 // which is entered and exited each time through the loop.
1255 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001256 ParseScope InnerScope(this, Scope::DeclScope,
David Blaikiebbafb8a2012-03-11 07:00:24 +00001257 (getLangOpts().C99 || getLangOpts().CPlusPlus) &&
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001258 Tok.isNot(tok::l_brace));
Sebastian Redlb62406f2008-12-11 19:48:14 +00001259
Chris Lattner9075bd72006-08-10 04:59:57 +00001260 // Read the body statement.
John McCalldadc5752010-08-24 06:29:42 +00001261 StmtResult Body(ParseStatement());
Chris Lattner9075bd72006-08-10 04:59:57 +00001262
Chris Lattner8fb26252007-08-22 05:28:50 +00001263 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001264 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001265
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001266 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001267 if (!Body.isInvalid()) {
Chris Lattner0046de12008-11-13 18:52:53 +00001268 Diag(Tok, diag::err_expected_while);
Chris Lattner03c40412008-11-23 23:17:07 +00001269 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner0046de12008-11-13 18:52:53 +00001270 SkipUntil(tok::semi, false, true);
1271 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001272 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001273 }
Chris Lattneraf635312006-10-16 06:06:51 +00001274 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001275
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001276 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001277 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner0046de12008-11-13 18:52:53 +00001278 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001279 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001280 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001281
Chris Lattner10da53c2008-12-12 06:35:28 +00001282 // Parse the parenthesized condition.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001283 BalancedDelimiterTracker T(*this, tok::l_paren);
1284 T.consumeOpen();
Chad Rosier67055f52012-07-10 21:35:27 +00001285
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001286 // FIXME: Do not just parse the attribute contents and throw them away
1287 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001288 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001289 ProhibitAttributes(attrs);
1290
John McCalldadc5752010-08-24 06:29:42 +00001291 ExprResult Cond = ParseExpression();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001292 T.consumeClose();
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001293 DoScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001294
Sebastian Redlb62406f2008-12-11 19:48:14 +00001295 if (Cond.isInvalid() || Body.isInvalid())
1296 return StmtError();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001297
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001298 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(),
1299 Cond.get(), T.getCloseLocation());
Chris Lattner9075bd72006-08-10 04:59:57 +00001300}
1301
1302/// ParseForStatement
1303/// for-statement: [C99 6.8.5.3]
1304/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1305/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001306/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1307/// [C++] statement
Richard Smith02e85f32011-04-14 22:09:26 +00001308/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001309/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1310/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis2b4072f2008-09-09 20:38:47 +00001311///
1312/// [C++] for-init-statement:
1313/// [C++] expression-statement
1314/// [C++] simple-declaration
1315///
Richard Smith02e85f32011-04-14 22:09:26 +00001316/// [C++0x] for-range-declaration:
1317/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1318/// [C++0x] for-range-initializer:
1319/// [C++0x] expression
1320/// [C++0x] braced-init-list [TODO]
Richard Smithc202b282012-04-14 00:33:13 +00001321StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001322 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001323 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001324
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001325 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001326 Diag(Tok, diag::err_expected_lparen_after) << "for";
Chris Lattner9075bd72006-08-10 04:59:57 +00001327 SkipUntil(tok::semi);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001328 return StmtError();
Chris Lattner9075bd72006-08-10 04:59:57 +00001329 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001330
Chad Rosier67055f52012-07-10 21:35:27 +00001331 bool C99orCXXorObjC = getLangOpts().C99 || getLangOpts().CPlusPlus ||
1332 getLangOpts().ObjC1;
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001333
Chris Lattner2dd1b722007-08-26 23:08:06 +00001334 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1335 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001336 //
1337 // C++ 6.4p3:
1338 // A name introduced by a declaration in a condition is in scope from its
1339 // point of declaration until the end of the substatements controlled by the
1340 // condition.
Argyrios Kyrtzidis47f98652008-09-11 23:08:39 +00001341 // C++ 3.3.2p4:
1342 // Names declared in the for-init-statement, and in the condition of if,
1343 // while, for, and switch statements are local to the if, while, for, or
1344 // switch statement (including the controlled statement).
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001345 // C++ 6.5.3p1:
1346 // Names declared in the for-init-statement are in the same declarative-region
1347 // as those declared in the condition.
1348 //
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001349 unsigned ScopeFlags;
Chris Lattner934074c2009-04-22 00:54:41 +00001350 if (C99orCXXorObjC)
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001351 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1352 Scope::DeclScope | Scope::ControlScope;
Chris Lattner2dd1b722007-08-26 23:08:06 +00001353 else
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001354 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1355
1356 ParseScope ForScope(this, ScopeFlags);
Chris Lattner9075bd72006-08-10 04:59:57 +00001357
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001358 BalancedDelimiterTracker T(*this, tok::l_paren);
1359 T.consumeOpen();
1360
John McCalldadc5752010-08-24 06:29:42 +00001361 ExprResult Value;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001362
Richard Smith02e85f32011-04-14 22:09:26 +00001363 bool ForEach = false, ForRange = false;
John McCalldadc5752010-08-24 06:29:42 +00001364 StmtResult FirstPart;
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001365 bool SecondPartIsInvalid = false;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001366 FullExprArg SecondPart(Actions);
John McCalldadc5752010-08-24 06:29:42 +00001367 ExprResult Collection;
Richard Smith02e85f32011-04-14 22:09:26 +00001368 ForRangeInit ForRangeInit;
Douglas Gregore60e41a2010-05-06 17:25:47 +00001369 FullExprArg ThirdPart(Actions);
John McCall48871652010-08-21 09:40:31 +00001370 Decl *SecondVar = 0;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001371
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001372 if (Tok.is(tok::code_completion)) {
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001373 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001374 C99orCXXorObjC? Sema::PCC_ForInit
1375 : Sema::PCC_Expression);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001376 cutOffParsing();
1377 return StmtError();
Douglas Gregor9d64c5e2009-09-21 20:51:25 +00001378 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001379
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001380 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00001381 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001382
Chris Lattner9075bd72006-08-10 04:59:57 +00001383 // Parse the first part of the for specifier.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001384 if (Tok.is(tok::semi)) { // for (;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001385 ProhibitAttributes(attrs);
Chris Lattner53361ac2006-08-10 05:19:57 +00001386 // no first part, eat the ';'.
1387 ConsumeToken();
Eli Friedman0ffc31c2011-12-20 01:50:37 +00001388 } else if (isForInitDeclaration()) { // for (int X = 4;
Chris Lattner53361ac2006-08-10 05:19:57 +00001389 // Parse declaration, which eats the ';'.
Chris Lattner934074c2009-04-22 00:54:41 +00001390 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Chris Lattnerab1803652006-08-10 05:22:36 +00001391 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001392
Richard Smith02e85f32011-04-14 22:09:26 +00001393 // In C++0x, "for (T NS:a" might not be a typo for ::
David Blaikiebbafb8a2012-03-11 07:00:24 +00001394 bool MightBeForRangeStmt = getLangOpts().CPlusPlus;
Richard Smith02e85f32011-04-14 22:09:26 +00001395 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1396
Chris Lattner49836b42009-04-02 04:16:50 +00001397 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Benjamin Kramerf0623432012-08-23 22:51:59 +00001398 StmtVector Stmts;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001399 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
Richard Smith02e85f32011-04-14 22:09:26 +00001400 DeclEnd, attrs, false,
1401 MightBeForRangeStmt ?
1402 &ForRangeInit : 0);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001403 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001404
Richard Smith02e85f32011-04-14 22:09:26 +00001405 if (ForRangeInit.ParsedForRangeDecl()) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001406 Diag(ForRangeInit.ColonLoc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00001407 diag::warn_cxx98_compat_for_range : diag::ext_for_range);
Richard Smith58c74332011-09-04 19:54:14 +00001408
Richard Smith02e85f32011-04-14 22:09:26 +00001409 ForRange = true;
1410 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattner32dc41c2009-03-29 17:27:48 +00001411 ConsumeToken();
1412 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniane774fa62009-11-19 22:12:37 +00001413 Actions.ActOnForEachDeclStmt(DG);
Mike Stump11289f42009-09-09 15:08:12 +00001414 // ObjC: for (id x in expr)
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001415 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001416
Douglas Gregor68762e72010-08-23 21:17:50 +00001417 if (Tok.is(tok::code_completion)) {
1418 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001419 cutOffParsing();
1420 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001421 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001422 Collection = ParseExpression();
Chris Lattner32dc41c2009-03-29 17:27:48 +00001423 } else {
1424 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001425 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001426 } else {
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001427 ProhibitAttributes(attrs);
Chris Lattner89c50c62006-08-11 06:41:18 +00001428 Value = ParseExpression();
Chris Lattner71e23ce2006-11-04 20:18:38 +00001429
John McCall34376a62010-12-04 03:47:34 +00001430 ForEach = isTokIdentifier_in();
1431
Chris Lattnercd68f642007-06-27 01:06:29 +00001432 // Turn the expression into a stmt.
John McCall34376a62010-12-04 03:47:34 +00001433 if (!Value.isInvalid()) {
1434 if (ForEach)
1435 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1436 else
Richard Smith945f8d32013-01-14 22:39:08 +00001437 FirstPart = Actions.ActOnExprStmt(Value);
John McCall34376a62010-12-04 03:47:34 +00001438 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001439
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001440 if (Tok.is(tok::semi)) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001441 ConsumeToken();
John McCall34376a62010-12-04 03:47:34 +00001442 } else if (ForEach) {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001443 ConsumeToken(); // consume 'in'
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001444
Douglas Gregor68762e72010-08-23 21:17:50 +00001445 if (Tok.is(tok::code_completion)) {
1446 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001447 cutOffParsing();
1448 return StmtError();
Douglas Gregor68762e72010-08-23 21:17:50 +00001449 }
Douglas Gregore60e41a2010-05-06 17:25:47 +00001450 Collection = ParseExpression();
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001451 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::colon) && FirstPart.get()) {
Richard Smith4f848f12011-12-20 22:56:20 +00001452 // User tried to write the reasonable, but ill-formed, for-range-statement
1453 // for (expr : expr) { ... }
1454 Diag(Tok, diag::err_for_range_expected_decl)
1455 << FirstPart.get()->getSourceRange();
1456 SkipUntil(tok::r_paren, false, true);
1457 SecondPartIsInvalid = true;
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001458 } else {
Douglas Gregor230a7e62011-02-17 03:38:46 +00001459 if (!Value.isInvalid()) {
1460 Diag(Tok, diag::err_expected_semi_for);
1461 } else {
1462 // Skip until semicolon or rparen, don't consume it.
1463 SkipUntil(tok::r_paren, true, true);
1464 if (Tok.is(tok::semi))
1465 ConsumeToken();
1466 }
Chris Lattner53361ac2006-08-10 05:19:57 +00001467 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001468 }
Richard Smith02e85f32011-04-14 22:09:26 +00001469 if (!ForEach && !ForRange) {
John McCallb268a282010-08-23 23:25:46 +00001470 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001471 // Parse the second part of the for specifier.
1472 if (Tok.is(tok::semi)) { // for (...;;
1473 // no second part.
Douglas Gregor230a7e62011-02-17 03:38:46 +00001474 } else if (Tok.is(tok::r_paren)) {
1475 // missing both semicolons.
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001476 } else {
John McCalldadc5752010-08-24 06:29:42 +00001477 ExprResult Second;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001478 if (getLangOpts().CPlusPlus)
Douglas Gregore60e41a2010-05-06 17:25:47 +00001479 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1480 else {
1481 Second = ParseExpression();
1482 if (!Second.isInvalid())
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001483 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCallb268a282010-08-23 23:25:46 +00001484 Second.get());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001485 }
Douglas Gregor12cc7ee2010-05-06 21:39:56 +00001486 SecondPartIsInvalid = Second.isInvalid();
David Blaikiea5696df2012-05-16 04:20:04 +00001487 SecondPart = Actions.MakeFullExpr(Second.get(), ForLoc);
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001488 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001489
Douglas Gregor230a7e62011-02-17 03:38:46 +00001490 if (Tok.isNot(tok::semi)) {
1491 if (!SecondPartIsInvalid || SecondVar)
1492 Diag(Tok, diag::err_expected_semi_for);
1493 else
1494 // Skip until semicolon or rparen, don't consume it.
1495 SkipUntil(tok::r_paren, true, true);
1496 }
1497
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001498 if (Tok.is(tok::semi)) {
1499 ConsumeToken();
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001500 }
Sebastian Redlb62406f2008-12-11 19:48:14 +00001501
Fariborz Jahanian732b8c22008-01-03 17:55:25 +00001502 // Parse the third part of the for specifier.
Douglas Gregore60e41a2010-05-06 17:25:47 +00001503 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCalldadc5752010-08-24 06:29:42 +00001504 ExprResult Third = ParseExpression();
Richard Smith945f8d32013-01-14 22:39:08 +00001505 // FIXME: The C++11 standard doesn't actually say that this is a
1506 // discarded-value expression, but it clearly should be.
1507 ThirdPart = Actions.MakeFullDiscardedValueExpr(Third.take());
Douglas Gregore60e41a2010-05-06 17:25:47 +00001508 }
Chris Lattner9075bd72006-08-10 04:59:57 +00001509 }
Chris Lattner4564bc12006-08-10 23:14:52 +00001510 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001511 T.consumeClose();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001512
Richard Smith02e85f32011-04-14 22:09:26 +00001513 // We need to perform most of the semantic analysis for a C++0x for-range
1514 // statememt before parsing the body, in order to be able to deduce the type
1515 // of an auto-typed loop variable.
1516 StmtResult ForRangeStmt;
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001517 StmtResult ForEachStmt;
Chad Rosier67055f52012-07-10 21:35:27 +00001518
John McCall53848232011-07-27 01:07:15 +00001519 if (ForRange) {
Sam Panzer2c4ca0f2012-08-16 21:47:25 +00001520 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, FirstPart.take(),
Richard Smith02e85f32011-04-14 22:09:26 +00001521 ForRangeInit.ColonLoc,
1522 ForRangeInit.RangeExpr.get(),
Richard Smitha05b3b52012-09-20 21:52:32 +00001523 T.getCloseLocation(),
1524 Sema::BFRK_Build);
Richard Smith02e85f32011-04-14 22:09:26 +00001525
John McCall53848232011-07-27 01:07:15 +00001526
1527 // Similarly, we need to do the semantic analysis for a for-range
1528 // statement immediately in order to close over temporaries correctly.
1529 } else if (ForEach) {
Sam Panzer2c4ca0f2012-08-16 21:47:25 +00001530 ForEachStmt = Actions.ActOnObjCForCollectionStmt(ForLoc,
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001531 FirstPart.take(),
Chad Rosier67055f52012-07-10 21:35:27 +00001532 Collection.take(),
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001533 T.getCloseLocation());
John McCall53848232011-07-27 01:07:15 +00001534 }
1535
Chris Lattner8fb26252007-08-22 05:28:50 +00001536 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner8f44d202007-08-22 05:33:11 +00001537 // there is no compound stmt. C90 does not have this clause. We only do this
1538 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis504bb842008-09-11 03:06:46 +00001539 //
1540 // C++ 6.5p2:
1541 // The substatement in an iteration-statement implicitly defines a local scope
1542 // which is entered and exited each time through the loop.
1543 //
1544 // See comments in ParseIfStatement for why we create a scope for
1545 // for-init-statement/condition and a new scope for substatement in C++.
1546 //
Mike Stump11289f42009-09-09 15:08:12 +00001547 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner934074c2009-04-22 00:54:41 +00001548 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001549
Chris Lattner9075bd72006-08-10 04:59:57 +00001550 // Read the body statement.
Nico Weber3cef1082011-12-22 23:26:17 +00001551 StmtResult Body(ParseStatement(TrailingElseLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001552
Chris Lattner8fb26252007-08-22 05:28:50 +00001553 // Pop the body scope if needed.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001554 InnerScope.Exit();
Chris Lattner8fb26252007-08-22 05:28:50 +00001555
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001556 // Leave the for-scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001557 ForScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001558
1559 if (Body.isInvalid())
Sebastian Redlb62406f2008-12-11 19:48:14 +00001560 return StmtError();
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001561
Richard Smith02e85f32011-04-14 22:09:26 +00001562 if (ForEach)
Fariborz Jahanian450bb6e2012-07-03 22:00:52 +00001563 return Actions.FinishObjCForCollectionStmt(ForEachStmt.take(),
1564 Body.take());
Mike Stump11289f42009-09-09 15:08:12 +00001565
Richard Smith02e85f32011-04-14 22:09:26 +00001566 if (ForRange)
1567 return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take());
1568
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001569 return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.take(),
1570 SecondPart, SecondVar, ThirdPart,
1571 T.getCloseLocation(), Body.take());
Chris Lattner9075bd72006-08-10 04:59:57 +00001572}
Chris Lattnerc951dae2006-08-10 04:23:57 +00001573
Chris Lattner503fadc2006-08-10 05:45:44 +00001574/// ParseGotoStatement
1575/// jump-statement:
1576/// 'goto' identifier ';'
1577/// [GNU] 'goto' '*' expression ';'
1578///
1579/// Note: this lets the caller parse the end ';'.
1580///
Richard Smithc202b282012-04-14 00:33:13 +00001581StmtResult Parser::ParseGotoStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001582 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001583 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001584
John McCalldadc5752010-08-24 06:29:42 +00001585 StmtResult Res;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001586 if (Tok.is(tok::identifier)) {
Chris Lattnerebb5c6c2011-02-18 01:27:55 +00001587 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1588 Tok.getLocation());
1589 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Chris Lattner503fadc2006-08-10 05:45:44 +00001590 ConsumeToken();
Eli Friedman5d72d412009-04-28 00:51:18 +00001591 } else if (Tok.is(tok::star)) {
Chris Lattner503fadc2006-08-10 05:45:44 +00001592 // GNU indirect goto extension.
1593 Diag(Tok, diag::ext_gnu_indirect_goto);
Chris Lattneraf635312006-10-16 06:06:51 +00001594 SourceLocation StarLoc = ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001595 ExprResult R(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001596 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001597 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001598 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001599 }
John McCallb268a282010-08-23 23:25:46 +00001600 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattnere34b2c22007-07-22 04:13:33 +00001601 } else {
1602 Diag(Tok, diag::err_expected_ident);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001603 return StmtError();
Chris Lattner503fadc2006-08-10 05:45:44 +00001604 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001605
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001606 return Res;
Chris Lattner503fadc2006-08-10 05:45:44 +00001607}
1608
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001609/// ParseContinueStatement
1610/// jump-statement:
1611/// 'continue' ';'
1612///
1613/// Note: this lets the caller parse the end ';'.
1614///
Richard Smithc202b282012-04-14 00:33:13 +00001615StmtResult Parser::ParseContinueStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001616 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001617 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001618}
1619
1620/// ParseBreakStatement
1621/// jump-statement:
1622/// 'break' ';'
1623///
1624/// Note: this lets the caller parse the end ';'.
1625///
Richard Smithc202b282012-04-14 00:33:13 +00001626StmtResult Parser::ParseBreakStatement() {
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001627 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001628 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Chris Lattner33ad2ca2006-11-05 23:47:55 +00001629}
1630
Chris Lattner503fadc2006-08-10 05:45:44 +00001631/// ParseReturnStatement
1632/// jump-statement:
1633/// 'return' expression[opt] ';'
Richard Smithc202b282012-04-14 00:33:13 +00001634StmtResult Parser::ParseReturnStatement() {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001635 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Chris Lattneraf635312006-10-16 06:06:51 +00001636 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redlb62406f2008-12-11 19:48:14 +00001637
John McCalldadc5752010-08-24 06:29:42 +00001638 ExprResult R;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001639 if (Tok.isNot(tok::semi)) {
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001640 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001641 Actions.CodeCompleteReturn(getCurScope());
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001642 cutOffParsing();
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001643 return StmtError();
1644 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001645
David Blaikiebbafb8a2012-03-11 07:00:24 +00001646 if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus) {
Douglas Gregore9e27d92011-03-11 23:10:44 +00001647 R = ParseInitializer();
Richard Smith5d164bc2011-10-15 05:09:34 +00001648 if (R.isUsable())
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001649 Diag(R.get()->getLocStart(), getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00001650 diag::warn_cxx98_compat_generalized_initializer_lists :
1651 diag::ext_generalized_initializer_lists)
Douglas Gregore9e27d92011-03-11 23:10:44 +00001652 << R.get()->getSourceRange();
1653 } else
1654 R = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001655 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Chris Lattnera0927ce2006-08-12 16:59:03 +00001656 SkipUntil(tok::semi, false, true);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001657 return StmtError();
Chris Lattner30f910e2006-10-16 05:52:41 +00001658 }
Chris Lattnera0927ce2006-08-12 16:59:03 +00001659 }
John McCallb268a282010-08-23 23:25:46 +00001660 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Chris Lattner503fadc2006-08-10 05:45:44 +00001661}
Chris Lattner0116c472006-08-15 06:03:28 +00001662
Eli Friedmana4b02c32011-09-30 01:13:51 +00001663/// ParseMicrosoftAsmStatement. When -fms-extensions/-fasm-blocks is enabled,
1664/// this routine is called to collect the tokens for an MS asm statement.
Chad Rosier32503022012-06-11 20:47:18 +00001665///
1666/// [MS] ms-asm-statement:
1667/// ms-asm-block
1668/// ms-asm-block ms-asm-statement
1669///
1670/// [MS] ms-asm-block:
1671/// '__asm' ms-asm-line '\n'
1672/// '__asm' '{' ms-asm-instruction-block[opt] '}' ';'[opt]
1673///
1674/// [MS] ms-asm-instruction-block
1675/// ms-asm-line
1676/// ms-asm-line '\n' ms-asm-instruction-block
1677///
Eli Friedmana4b02c32011-09-30 01:13:51 +00001678StmtResult Parser::ParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
1679 SourceManager &SrcMgr = PP.getSourceManager();
1680 SourceLocation EndLoc = AsmLoc;
Chad Rosier32503022012-06-11 20:47:18 +00001681 SmallVector<Token, 4> AsmToks;
Chad Rosierc97a6bb2012-08-14 19:22:06 +00001682
1683 bool InBraces = false;
1684 unsigned short savedBraceCount = 0;
1685 bool InAsmComment = false;
1686 FileID FID;
1687 unsigned LineNo = 0;
1688 unsigned NumTokensRead = 0;
1689 SourceLocation LBraceLoc;
1690
1691 if (Tok.is(tok::l_brace)) {
1692 // Braced inline asm: consume the opening brace.
1693 InBraces = true;
1694 savedBraceCount = BraceCount;
1695 EndLoc = LBraceLoc = ConsumeBrace();
1696 ++NumTokensRead;
1697 } else {
1698 // Single-line inline asm; compute which line it is on.
1699 std::pair<FileID, unsigned> ExpAsmLoc =
1700 SrcMgr.getDecomposedExpansionLoc(EndLoc);
1701 FID = ExpAsmLoc.first;
1702 LineNo = SrcMgr.getLineNumber(FID, ExpAsmLoc.second);
1703 }
1704
1705 SourceLocation TokLoc = Tok.getLocation();
Eli Friedmana4b02c32011-09-30 01:13:51 +00001706 do {
Chad Rosierc97a6bb2012-08-14 19:22:06 +00001707 // If we hit EOF, we're done, period.
1708 if (Tok.is(tok::eof))
Eli Friedmana4b02c32011-09-30 01:13:51 +00001709 break;
Chad Rosierc97a6bb2012-08-14 19:22:06 +00001710
Chad Rosierc97a6bb2012-08-14 19:22:06 +00001711 if (!InAsmComment && Tok.is(tok::semi)) {
1712 // A semicolon in an asm is the start of a comment.
1713 InAsmComment = true;
1714 if (InBraces) {
1715 // Compute which line the comment is on.
1716 std::pair<FileID, unsigned> ExpSemiLoc =
1717 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1718 FID = ExpSemiLoc.first;
1719 LineNo = SrcMgr.getLineNumber(FID, ExpSemiLoc.second);
1720 }
1721 } else if (!InBraces || InAsmComment) {
1722 // If end-of-line is significant, check whether this token is on a
1723 // new line.
1724 std::pair<FileID, unsigned> ExpLoc =
1725 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1726 if (ExpLoc.first != FID ||
1727 SrcMgr.getLineNumber(ExpLoc.first, ExpLoc.second) != LineNo) {
1728 // If this is a single-line __asm, we're done.
1729 if (!InBraces)
1730 break;
1731 // We're no longer in a comment.
1732 InAsmComment = false;
1733 } else if (!InAsmComment && Tok.is(tok::r_brace)) {
1734 // Single-line asm always ends when a closing brace is seen.
1735 // FIXME: This is compatible with Apple gcc's -fasm-blocks; what
1736 // does MSVC do here?
1737 break;
1738 }
1739 }
1740 if (!InAsmComment && InBraces && Tok.is(tok::r_brace) &&
1741 BraceCount == (savedBraceCount + 1)) {
1742 // Consume the closing brace, and finish
1743 EndLoc = ConsumeBrace();
1744 break;
1745 }
1746
1747 // Consume the next token; make sure we don't modify the brace count etc.
1748 // if we are in a comment.
1749 EndLoc = TokLoc;
1750 if (InAsmComment)
1751 PP.Lex(Tok);
1752 else {
1753 AsmToks.push_back(Tok);
1754 ConsumeAnyToken();
1755 }
1756 TokLoc = Tok.getLocation();
1757 ++NumTokensRead;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001758 } while (1);
Chad Rosier32503022012-06-11 20:47:18 +00001759
Chad Rosierc97a6bb2012-08-14 19:22:06 +00001760 if (InBraces && BraceCount != savedBraceCount) {
1761 // __asm without closing brace (this can happen at EOF).
1762 Diag(Tok, diag::err_expected_rbrace);
1763 Diag(LBraceLoc, diag::note_matching) << "{";
1764 return StmtError();
1765 } else if (NumTokensRead == 0) {
1766 // Empty __asm.
1767 Diag(Tok, diag::err_expected_lbrace);
1768 return StmtError();
1769 }
1770
Chad Rosierc6c71332012-08-06 20:03:45 +00001771 // FIXME: We should be passing source locations for better diagnostics.
Chad Rosierb6f46c12012-08-15 16:53:30 +00001772 return Actions.ActOnMSAsmStmt(AsmLoc, LBraceLoc,
1773 llvm::makeArrayRef(AsmToks), EndLoc);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001774}
1775
Chris Lattner0116c472006-08-15 06:03:28 +00001776/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001777/// asm-statement:
1778/// gnu-asm-statement
1779/// ms-asm-statement
1780///
1781/// [GNU] gnu-asm-statement:
Chris Lattner0116c472006-08-15 06:03:28 +00001782/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1783///
1784/// [GNU] asm-argument:
1785/// asm-string-literal
1786/// asm-string-literal ':' asm-operands[opt]
1787/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1788/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1789/// ':' asm-clobbers
1790///
1791/// [GNU] asm-clobbers:
1792/// asm-string-literal
1793/// asm-clobbers ',' asm-string-literal
1794///
John McCalldadc5752010-08-24 06:29:42 +00001795StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001796 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattner73c56c02007-10-29 04:04:16 +00001797 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001798
Chad Rosierc8e56e82012-12-05 21:08:21 +00001799 if (getLangOpts().AsmBlocks && Tok.isNot(tok::l_paren) &&
Chad Rosier67055f52012-07-10 21:35:27 +00001800 !isTypeQualifier()) {
Steve Naroffb2c80c72008-02-07 03:50:06 +00001801 msAsm = true;
Eli Friedmana4b02c32011-09-30 01:13:51 +00001802 return ParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffb2c80c72008-02-07 03:50:06 +00001803 }
John McCall084e83d2011-03-24 11:26:52 +00001804 DeclSpec DS(AttrFactory);
Chris Lattner0116c472006-08-15 06:03:28 +00001805 SourceLocation Loc = Tok.getLocation();
Alexis Hunt96d5c762009-11-21 08:43:09 +00001806 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001807
Chris Lattner0116c472006-08-15 06:03:28 +00001808 // GNU asms accept, but warn, about type-qualifiers other than volatile.
Chris Lattnera925dc62006-11-28 04:33:46 +00001809 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner6d29c102008-11-18 07:48:38 +00001810 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Chris Lattnera925dc62006-11-28 04:33:46 +00001811 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner6d29c102008-11-18 07:48:38 +00001812 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redlb62406f2008-12-11 19:48:14 +00001813
Chris Lattner0116c472006-08-15 06:03:28 +00001814 // Remember if this was a volatile asm.
Anders Carlsson660bdd12007-11-23 23:12:25 +00001815 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001816 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001817 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Chris Lattner0116c472006-08-15 06:03:28 +00001818 SkipUntil(tok::r_paren);
Sebastian Redlb62406f2008-12-11 19:48:14 +00001819 return StmtError();
Chris Lattner0116c472006-08-15 06:03:28 +00001820 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001821 BalancedDelimiterTracker T(*this, tok::l_paren);
1822 T.consumeOpen();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001823
John McCalldadc5752010-08-24 06:29:42 +00001824 ExprResult AsmString(ParseAsmStringLiteral());
Ted Kremenekeb0a6c02011-12-02 01:30:14 +00001825 if (AsmString.isInvalid()) {
Richard Smithd67aea22012-03-06 03:21:47 +00001826 // Consume up to and including the closing paren.
1827 T.skipToEnd();
Sebastian Redlb62406f2008-12-11 19:48:14 +00001828 return StmtError();
Ted Kremenekeb0a6c02011-12-02 01:30:14 +00001829 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001830
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001831 SmallVector<IdentifierInfo *, 4> Names;
Benjamin Kramerf0623432012-08-23 22:51:59 +00001832 ExprVector Constraints;
1833 ExprVector Exprs;
1834 ExprVector Clobbers;
Chris Lattner0116c472006-08-15 06:03:28 +00001835
Anders Carlsson19fe1162008-02-05 23:03:50 +00001836 if (Tok.is(tok::r_paren)) {
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001837 // We have a simple asm expression like 'asm("foo")'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001838 T.consumeClose();
Chad Rosierde70e0e2012-08-25 00:11:56 +00001839 return Actions.ActOnGCCAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1840 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1841 Constraints, Exprs, AsmString.take(),
1842 Clobbers, T.getCloseLocation());
Chris Lattner0116c472006-08-15 06:03:28 +00001843 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001844
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001845 // Parse Outputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001846 bool AteExtraColon = false;
1847 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1848 // In C++ mode, parse "::" like ": :".
1849 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001850 ConsumeToken();
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001851
Chris Lattner15768502009-12-20 23:08:04 +00001852 if (!AteExtraColon &&
1853 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001854 return StmtError();
1855 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001856
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001857 unsigned NumOutputs = Names.size();
1858
1859 // Parse Inputs, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001860 if (AteExtraColon ||
1861 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1862 // In C++ mode, parse "::" like ": :".
1863 if (AteExtraColon)
1864 AteExtraColon = false;
1865 else {
1866 AteExtraColon = Tok.is(tok::coloncolon);
1867 ConsumeToken();
1868 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001869
Chris Lattner15768502009-12-20 23:08:04 +00001870 if (!AteExtraColon &&
1871 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001872 return StmtError();
1873 }
1874
1875 assert(Names.size() == Constraints.size() &&
1876 Constraints.size() == Exprs.size() &&
1877 "Input operand size mismatch!");
1878
1879 unsigned NumInputs = Names.size() - NumOutputs;
1880
1881 // Parse the clobbers, if present.
Chris Lattner15768502009-12-20 23:08:04 +00001882 if (AteExtraColon || Tok.is(tok::colon)) {
1883 if (!AteExtraColon)
1884 ConsumeToken();
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001885
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001886 // Parse the asm-string list for clobbers if present.
1887 if (Tok.isNot(tok::r_paren)) {
1888 while (1) {
John McCalldadc5752010-08-24 06:29:42 +00001889 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001890
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001891 if (Clobber.isInvalid())
1892 break;
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001893
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001894 Clobbers.push_back(Clobber.release());
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001895
Chandler Carruth3c31aa32010-07-22 07:11:21 +00001896 if (Tok.isNot(tok::comma)) break;
1897 ConsumeToken();
1898 }
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001899 }
1900 }
1901
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001902 T.consumeClose();
Chad Rosierde70e0e2012-08-25 00:11:56 +00001903 return Actions.ActOnGCCAsmStmt(AsmLoc, false, isVolatile, NumOutputs,
1904 NumInputs, Names.data(), Constraints, Exprs,
1905 AsmString.take(), Clobbers,
1906 T.getCloseLocation());
Chris Lattner0116c472006-08-15 06:03:28 +00001907}
1908
1909/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattnerbf5fff52009-12-20 23:00:41 +00001910/// asm-statement, assuming the leading ':' token was eaten.
Chris Lattner0116c472006-08-15 06:03:28 +00001911///
1912/// [GNU] asm-operands:
1913/// asm-operand
1914/// asm-operands ',' asm-operand
1915///
1916/// [GNU] asm-operand:
1917/// asm-string-literal '(' expression ')'
1918/// '[' identifier ']' asm-string-literal '(' expression ')'
1919///
Daniel Dunbar70e7ead2009-10-18 20:26:27 +00001920//
1921// FIXME: Avoid unnecessary std::string trashing.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001922bool Parser::ParseAsmOperandsOpt(SmallVectorImpl<IdentifierInfo *> &Names,
Richard Trieu2bd04012011-09-09 02:00:50 +00001923 SmallVectorImpl<Expr *> &Constraints,
1924 SmallVectorImpl<Expr *> &Exprs) {
Chris Lattner0116c472006-08-15 06:03:28 +00001925 // 'asm-operands' isn't present?
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001926 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001927 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001928
1929 while (1) {
Chris Lattner0116c472006-08-15 06:03:28 +00001930 // Read the [id] if present.
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001931 if (Tok.is(tok::l_square)) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001932 BalancedDelimiterTracker T(*this, tok::l_square);
1933 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00001934
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001935 if (Tok.isNot(tok::identifier)) {
Chris Lattner0116c472006-08-15 06:03:28 +00001936 Diag(Tok, diag::err_expected_ident);
1937 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001938 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001939 }
Mike Stump11289f42009-09-09 15:08:12 +00001940
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001941 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner645ff3f2007-10-29 04:06:22 +00001942 ConsumeToken();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001943
Anders Carlsson9a020f92010-01-30 22:25:16 +00001944 Names.push_back(II);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001945 T.consumeClose();
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001946 } else
Anders Carlsson9a020f92010-01-30 22:25:16 +00001947 Names.push_back(0);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001948
John McCalldadc5752010-08-24 06:29:42 +00001949 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001950 if (Constraint.isInvalid()) {
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001951 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001952 return true;
Anders Carlsson94ea8aa2007-11-22 01:36:19 +00001953 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001954 Constraints.push_back(Constraint.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001955
Chris Lattnerfeb00b62007-10-09 17:41:39 +00001956 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001957 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Chris Lattner0116c472006-08-15 06:03:28 +00001958 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001959 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001960 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001961
Chris Lattner0116c472006-08-15 06:03:28 +00001962 // Read the parenthesized expression.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001963 BalancedDelimiterTracker T(*this, tok::l_paren);
1964 T.consumeOpen();
John McCalldadc5752010-08-24 06:29:42 +00001965 ExprResult Res(ParseExpression());
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001966 T.consumeClose();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001967 if (Res.isInvalid()) {
Chris Lattner0116c472006-08-15 06:03:28 +00001968 SkipUntil(tok::r_paren);
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001969 return true;
Chris Lattner0116c472006-08-15 06:03:28 +00001970 }
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001971 Exprs.push_back(Res.release());
Chris Lattner0116c472006-08-15 06:03:28 +00001972 // Eat the comma and continue parsing if it exists.
Anders Carlsson2e64d1a2008-02-09 19:57:29 +00001973 if (Tok.isNot(tok::comma)) return false;
Chris Lattner0116c472006-08-15 06:03:28 +00001974 ConsumeToken();
1975 }
1976}
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001977
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001978Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner12f2ea52009-03-05 00:49:17 +00001979 assert(Tok.is(tok::l_brace));
1980 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redla7b98a72009-04-26 20:35:05 +00001981
Argyrios Kyrtzidis44319182013-02-22 04:11:06 +00001982 if (SkipFunctionBodies && (!Decl || Actions.canSkipFunctionBody(Decl)) &&
Richard Smith1ab34b32012-11-19 21:13:18 +00001983 trySkippingFunctionBody()) {
Erik Verbruggen6e922512012-04-12 10:11:59 +00001984 BodyScope.Exit();
Argyrios Kyrtzidis1eb71a12012-12-06 18:59:10 +00001985 return Actions.ActOnSkippedFunctionBody(Decl);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001986 }
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00001987
John McCallfaf5fb42010-08-26 23:41:50 +00001988 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1989 "parsing function body");
Mike Stump11289f42009-09-09 15:08:12 +00001990
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001991 // Do not enter a scope for the brace, as the arguments are in the same scope
1992 // (the function body) as the body itself. Instead, just read the statement
1993 // list and put it into a CompoundStmt for safe keeping.
John McCalldadc5752010-08-24 06:29:42 +00001994 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001995
Fariborz Jahanian8e632942007-11-08 19:01:26 +00001996 // If the function body could not be parsed, make a bogus compoundstmt.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00001997 if (FnBody.isInvalid()) {
1998 Sema::CompoundScopeRAII CompoundScope(Actions);
Mike Stump11289f42009-09-09 15:08:12 +00001999 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00002000 MultiStmtArg(), false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002001 }
Sebastian Redl042ad952008-12-11 19:30:53 +00002002
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002003 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00002004 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeon34f92ac2007-12-01 08:06:07 +00002005}
Sebastian Redlb219c902008-12-21 16:41:36 +00002006
Sebastian Redla7b98a72009-04-26 20:35:05 +00002007/// ParseFunctionTryBlock - Parse a C++ function-try-block.
2008///
2009/// function-try-block:
2010/// 'try' ctor-initializer[opt] compound-statement handler-seq
2011///
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002012Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redla7b98a72009-04-26 20:35:05 +00002013 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2014 SourceLocation TryLoc = ConsumeToken();
2015
John McCallfaf5fb42010-08-26 23:41:50 +00002016 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
2017 "parsing function try block");
Sebastian Redla7b98a72009-04-26 20:35:05 +00002018
2019 // Constructor initializer list?
2020 if (Tok.is(tok::colon))
2021 ParseConstructorInitializer(Decl);
Douglas Gregor5ca153f2011-09-07 20:36:12 +00002022 else
2023 Actions.ActOnDefaultCtorInitializers(Decl);
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00002024
Richard Smith1ab34b32012-11-19 21:13:18 +00002025 if (SkipFunctionBodies && Actions.canSkipFunctionBody(Decl) &&
2026 trySkippingFunctionBody()) {
Erik Verbruggen6e922512012-04-12 10:11:59 +00002027 BodyScope.Exit();
Argyrios Kyrtzidis1eb71a12012-12-06 18:59:10 +00002028 return Actions.ActOnSkippedFunctionBody(Decl);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002029 }
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002030
Sebastian Redld98ecd62009-04-26 21:08:36 +00002031 SourceLocation LBraceLoc = Tok.getLocation();
David Blaikie1c9c9042012-11-10 01:04:23 +00002032 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc, /*FnTry*/true));
Sebastian Redla7b98a72009-04-26 20:35:05 +00002033 // If we failed to parse the try-catch, we just give the function an empty
2034 // compound statement as the body.
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002035 if (FnBody.isInvalid()) {
2036 Sema::CompoundScopeRAII CompoundScope(Actions);
Sebastian Redld98ecd62009-04-26 21:08:36 +00002037 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00002038 MultiStmtArg(), false);
Dmitri Gribenko800ddf32012-02-14 22:14:32 +00002039 }
Sebastian Redla7b98a72009-04-26 20:35:05 +00002040
Douglas Gregora0ff0c32011-03-16 17:05:57 +00002041 BodyScope.Exit();
John McCallb268a282010-08-23 23:25:46 +00002042 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redla7b98a72009-04-26 20:35:05 +00002043}
2044
Erik Verbruggen6e922512012-04-12 10:11:59 +00002045bool Parser::trySkippingFunctionBody() {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002046 assert(Tok.is(tok::l_brace));
Erik Verbruggen6e922512012-04-12 10:11:59 +00002047 assert(SkipFunctionBodies &&
2048 "Should only be called when SkipFunctionBodies is enabled");
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002049
Argyrios Kyrtzidis289e4a32012-10-31 17:29:28 +00002050 if (!PP.isCodeCompletionEnabled()) {
2051 ConsumeBrace();
2052 SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false);
2053 return true;
2054 }
2055
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002056 // We're in code-completion mode. Skip parsing for all function bodies unless
2057 // the body contains the code-completion point.
2058 TentativeParsingAction PA(*this);
2059 ConsumeBrace();
2060 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
Argyrios Kyrtzidis289e4a32012-10-31 17:29:28 +00002061 /*StopAtCodeCompletion=*/true)) {
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00002062 PA.Commit();
2063 return true;
2064 }
2065
2066 PA.Revert();
2067 return false;
2068}
2069
Sebastian Redlb219c902008-12-21 16:41:36 +00002070/// ParseCXXTryBlock - Parse a C++ try-block.
2071///
2072/// try-block:
2073/// 'try' compound-statement handler-seq
2074///
Richard Smithc202b282012-04-14 00:33:13 +00002075StmtResult Parser::ParseCXXTryBlock() {
Sebastian Redlb219c902008-12-21 16:41:36 +00002076 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2077
2078 SourceLocation TryLoc = ConsumeToken();
Sebastian Redla7b98a72009-04-26 20:35:05 +00002079 return ParseCXXTryBlockCommon(TryLoc);
2080}
2081
2082/// ParseCXXTryBlockCommon - Parse the common part of try-block and
2083/// function-try-block.
2084///
2085/// try-block:
2086/// 'try' compound-statement handler-seq
2087///
2088/// function-try-block:
2089/// 'try' ctor-initializer[opt] compound-statement handler-seq
2090///
2091/// handler-seq:
2092/// handler handler-seq[opt]
2093///
John Wiegley1c0675e2011-04-28 01:08:34 +00002094/// [Borland] try-block:
2095/// 'try' compound-statement seh-except-block
2096/// 'try' compound-statment seh-finally-block
2097///
David Blaikie1c9c9042012-11-10 01:04:23 +00002098StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc, bool FnTry) {
Sebastian Redlb219c902008-12-21 16:41:36 +00002099 if (Tok.isNot(tok::l_brace))
2100 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Alexis Hunt96d5c762009-11-21 08:43:09 +00002101 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
Richard Smithc202b282012-04-14 00:33:13 +00002102
2103 StmtResult TryBlock(ParseCompoundStatement(/*isStmtExpr=*/false,
David Blaikie3403feb2012-11-13 18:51:45 +00002104 Scope::DeclScope | Scope::TryScope |
2105 (FnTry ? Scope::FnTryCatchScope : 0)));
Sebastian Redlb219c902008-12-21 16:41:36 +00002106 if (TryBlock.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002107 return TryBlock;
Sebastian Redlb219c902008-12-21 16:41:36 +00002108
John Wiegley1c0675e2011-04-28 01:08:34 +00002109 // Borland allows SEH-handlers with 'try'
Chad Rosier67055f52012-07-10 21:35:27 +00002110
Richard Smithc202b282012-04-14 00:33:13 +00002111 if ((Tok.is(tok::identifier) &&
2112 Tok.getIdentifierInfo() == getSEHExceptKeyword()) ||
2113 Tok.is(tok::kw___finally)) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002114 // TODO: Factor into common return ParseSEHHandlerCommon(...)
2115 StmtResult Handler;
Douglas Gregor60060d62011-10-21 03:57:52 +00002116 if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley1c0675e2011-04-28 01:08:34 +00002117 SourceLocation Loc = ConsumeToken();
2118 Handler = ParseSEHExceptBlock(Loc);
2119 }
2120 else {
2121 SourceLocation Loc = ConsumeToken();
2122 Handler = ParseSEHFinallyBlock(Loc);
2123 }
2124 if(Handler.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002125 return Handler;
John McCall53fa7142010-12-24 02:08:15 +00002126
John Wiegley1c0675e2011-04-28 01:08:34 +00002127 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
2128 TryLoc,
2129 TryBlock.take(),
2130 Handler.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00002131 }
John Wiegley1c0675e2011-04-28 01:08:34 +00002132 else {
Benjamin Kramerf0623432012-08-23 22:51:59 +00002133 StmtVector Handlers;
Richard Smithc202b282012-04-14 00:33:13 +00002134 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00002135 MaybeParseCXX11Attributes(attrs);
John Wiegley1c0675e2011-04-28 01:08:34 +00002136 ProhibitAttributes(attrs);
Sebastian Redlb219c902008-12-21 16:41:36 +00002137
John Wiegley1c0675e2011-04-28 01:08:34 +00002138 if (Tok.isNot(tok::kw_catch))
2139 return StmtError(Diag(Tok, diag::err_expected_catch));
2140 while (Tok.is(tok::kw_catch)) {
David Blaikie1c9c9042012-11-10 01:04:23 +00002141 StmtResult Handler(ParseCXXCatchBlock(FnTry));
John Wiegley1c0675e2011-04-28 01:08:34 +00002142 if (!Handler.isInvalid())
2143 Handlers.push_back(Handler.release());
2144 }
2145 // Don't bother creating the full statement if we don't have any usable
2146 // handlers.
2147 if (Handlers.empty())
2148 return StmtError();
2149
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002150 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(),Handlers);
John Wiegley1c0675e2011-04-28 01:08:34 +00002151 }
Sebastian Redlb219c902008-12-21 16:41:36 +00002152}
2153
2154/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
2155///
Richard Smith1dba27c2013-01-29 09:02:09 +00002156/// handler:
2157/// 'catch' '(' exception-declaration ')' compound-statement
Sebastian Redlb219c902008-12-21 16:41:36 +00002158///
Richard Smith1dba27c2013-01-29 09:02:09 +00002159/// exception-declaration:
2160/// attribute-specifier-seq[opt] type-specifier-seq declarator
2161/// attribute-specifier-seq[opt] type-specifier-seq abstract-declarator[opt]
2162/// '...'
Sebastian Redlb219c902008-12-21 16:41:36 +00002163///
David Blaikie1c9c9042012-11-10 01:04:23 +00002164StmtResult Parser::ParseCXXCatchBlock(bool FnCatch) {
Sebastian Redlb219c902008-12-21 16:41:36 +00002165 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
2166
2167 SourceLocation CatchLoc = ConsumeToken();
2168
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002169 BalancedDelimiterTracker T(*this, tok::l_paren);
2170 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redlb219c902008-12-21 16:41:36 +00002171 return StmtError();
2172
2173 // C++ 3.3.2p3:
2174 // The name in a catch exception-declaration is local to the handler and
2175 // shall not be redeclared in the outermost block of the handler.
David Blaikie1c9c9042012-11-10 01:04:23 +00002176 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope |
David Blaikie3403feb2012-11-13 18:51:45 +00002177 (FnCatch ? Scope::FnTryCatchScope : 0));
Sebastian Redlb219c902008-12-21 16:41:36 +00002178
2179 // exception-declaration is equivalent to '...' or a parameter-declaration
2180 // without default arguments.
John McCall48871652010-08-21 09:40:31 +00002181 Decl *ExceptionDecl = 0;
Sebastian Redlb219c902008-12-21 16:41:36 +00002182 if (Tok.isNot(tok::ellipsis)) {
Richard Smith1dba27c2013-01-29 09:02:09 +00002183 ParsedAttributesWithRange Attributes(AttrFactory);
2184 MaybeParseCXX11Attributes(Attributes);
2185
John McCall084e83d2011-03-24 11:26:52 +00002186 DeclSpec DS(AttrFactory);
Richard Smith1dba27c2013-01-29 09:02:09 +00002187 DS.takeAttributesFrom(Attributes);
2188
Sebastian Redl54c04d42008-12-22 19:15:10 +00002189 if (ParseCXXTypeSpecifierSeq(DS))
2190 return StmtError();
Richard Smith1dba27c2013-01-29 09:02:09 +00002191
Sebastian Redlb219c902008-12-21 16:41:36 +00002192 Declarator ExDecl(DS, Declarator::CXXCatchContext);
2193 ParseDeclarator(ExDecl);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002194 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redlb219c902008-12-21 16:41:36 +00002195 } else
2196 ConsumeToken();
2197
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002198 T.consumeClose();
2199 if (T.getCloseLocation().isInvalid())
Sebastian Redlb219c902008-12-21 16:41:36 +00002200 return StmtError();
2201
2202 if (Tok.isNot(tok::l_brace))
2203 return StmtError(Diag(Tok, diag::err_expected_lbrace));
2204
Alexis Hunt96d5c762009-11-21 08:43:09 +00002205 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
Richard Smithc202b282012-04-14 00:33:13 +00002206 StmtResult Block(ParseCompoundStatement());
Sebastian Redlb219c902008-12-21 16:41:36 +00002207 if (Block.isInvalid())
Benjamin Kramer62b95d82012-08-23 21:35:17 +00002208 return Block;
Sebastian Redlb219c902008-12-21 16:41:36 +00002209
John McCallb268a282010-08-23 23:25:46 +00002210 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redlb219c902008-12-21 16:41:36 +00002211}
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002212
2213void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Douglas Gregor43edb322011-10-24 22:31:10 +00002214 IfExistsCondition Result;
Francois Picheta5b3fcb2011-05-07 17:30:27 +00002215 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002216 return;
NAKAMURA Takumi82a35112011-10-08 11:31:46 +00002217
Douglas Gregor43edb322011-10-24 22:31:10 +00002218 // Handle dependent statements by parsing the braces as a compound statement.
2219 // This is not the same behavior as Visual C++, which don't treat this as a
2220 // compound statement, but for Clang's type checking we can't have anything
2221 // inside these braces escaping to the surrounding code.
2222 if (Result.Behavior == IEB_Dependent) {
2223 if (!Tok.is(tok::l_brace)) {
2224 Diag(Tok, diag::err_expected_lbrace);
Richard Smithc202b282012-04-14 00:33:13 +00002225 return;
Douglas Gregor43edb322011-10-24 22:31:10 +00002226 }
Richard Smithc202b282012-04-14 00:33:13 +00002227
2228 StmtResult Compound = ParseCompoundStatement();
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002229 if (Compound.isInvalid())
2230 return;
Richard Smithc202b282012-04-14 00:33:13 +00002231
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002232 StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc,
2233 Result.IsIfExists,
Richard Smithc202b282012-04-14 00:33:13 +00002234 Result.SS,
Douglas Gregordeb4a2be2011-10-25 01:33:02 +00002235 Result.Name,
2236 Compound.get());
2237 if (DepResult.isUsable())
2238 Stmts.push_back(DepResult.get());
Douglas Gregor43edb322011-10-24 22:31:10 +00002239 return;
2240 }
Richard Smithc202b282012-04-14 00:33:13 +00002241
Douglas Gregor43edb322011-10-24 22:31:10 +00002242 BalancedDelimiterTracker Braces(*this, tok::l_brace);
2243 if (Braces.consumeOpen()) {
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002244 Diag(Tok, diag::err_expected_lbrace);
2245 return;
2246 }
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002247
Douglas Gregor43edb322011-10-24 22:31:10 +00002248 switch (Result.Behavior) {
2249 case IEB_Parse:
2250 // Parse the statements below.
2251 break;
Chad Rosier67055f52012-07-10 21:35:27 +00002252
Douglas Gregor43edb322011-10-24 22:31:10 +00002253 case IEB_Dependent:
2254 llvm_unreachable("Dependent case handled above");
Chad Rosier67055f52012-07-10 21:35:27 +00002255
Douglas Gregor43edb322011-10-24 22:31:10 +00002256 case IEB_Skip:
2257 Braces.skipToEnd();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002258 return;
2259 }
2260
2261 // Condition is true, parse the statements.
2262 while (Tok.isNot(tok::r_brace)) {
2263 StmtResult R = ParseStatementOrDeclaration(Stmts, false);
2264 if (R.isUsable())
2265 Stmts.push_back(R.release());
2266 }
Douglas Gregor43edb322011-10-24 22:31:10 +00002267 Braces.consumeClose();
Francois Pichet4a7de3e2011-05-06 20:48:22 +00002268}