blob: f58f90d56c5d2c102ca0bf65be712dd952a329fb [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Statement and Block portions of the Parser
11// interface.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000016#include "RAIIObjectsForParser.h"
John McCall19510852010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
John McCallf312b1e2010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall19510852010-08-20 18:27:03 +000019#include "clang/Sema/Scope.h"
Richard Smith05766812012-08-18 00:55:03 +000020#include "clang/Sema/TypoCorrection.h"
Chris Lattnerae50fa02009-03-05 00:00:31 +000021#include "clang/Basic/Diagnostic.h"
22#include "clang/Basic/PrettyStackTrace.h"
23#include "clang/Basic/SourceManager.h"
Chad Rosier8cd64b42012-06-11 20:47:18 +000024#include "llvm/ADT/SmallString.h"
Reid Spencer5f016e22007-07-11 17:01:13 +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 Kyrtzidisdcdd55f2008-09-07 18:58:01 +000043/// [C++] declaration-statement
Sebastian Redla0fd8652008-12-21 16:41:36 +000044/// [C++] try-block
John Wiegley28bbe4b2011-04-28 01:08:34 +000045/// [MS] seh-try-block
Fariborz Jahanianb384d322007-10-04 20:19:06 +000046/// [OBC] objc-throw-statement
47/// [OBC] objc-try-catch-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +000048/// [OBC] objc-synchronized-statement
Reid Spencer5f016e22007-07-11 17:01:13 +000049/// [GNU] asm-statement
50/// [OMP] openmp-construct [TODO]
51///
52/// labeled-statement:
53/// identifier ':' statement
54/// 'case' constant-expression ':' statement
55/// 'default' ':' statement
56///
57/// selection-statement:
58/// if-statement
59/// switch-statement
60///
61/// iteration-statement:
62/// while-statement
63/// do-statement
64/// for-statement
65///
66/// expression-statement:
67/// expression[opt] ';'
68///
69/// jump-statement:
70/// 'goto' identifier ';'
71/// 'continue' ';'
72/// 'break' ';'
73/// 'return' expression[opt] ';'
74/// [GNU] 'goto' '*' expression ';'
75///
Fariborz Jahanianb384d322007-10-04 20:19:06 +000076/// [OBC] objc-throw-statement:
77/// [OBC] '@' 'throw' expression ';'
Mike Stump1eb44332009-09-09 15:08:12 +000078/// [OBC] '@' 'throw' ';'
79///
John McCall60d7b3a2010-08-24 06:29:42 +000080StmtResult
Nico Weber5cb94a72011-12-22 23:26:17 +000081Parser::ParseStatementOrDeclaration(StmtVector &Stmts, bool OnlyStatement,
82 SourceLocation *TrailingElseLoc) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +000083
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +000084 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +000085
Richard Smith534986f2012-04-14 00:33:13 +000086 ParsedAttributesWithRange Attrs(AttrFactory);
87 MaybeParseCXX0XAttributes(Attrs, 0, /*MightBeObjCMessageSend*/ true);
88
89 StmtResult Res = ParseStatementOrDeclarationAfterAttributes(Stmts,
90 OnlyStatement, TrailingElseLoc, Attrs);
91
92 assert((Attrs.empty() || Res.isInvalid() || Res.isUsable()) &&
93 "attributes on empty statement");
94
95 if (Attrs.empty() || Res.isInvalid())
96 return Res;
97
98 return Actions.ProcessStmtAttributes(Res.get(), Attrs.getList(), Attrs.Range);
99}
100
101StmtResult
102Parser::ParseStatementOrDeclarationAfterAttributes(StmtVector &Stmts,
103 bool OnlyStatement, SourceLocation *TrailingElseLoc,
104 ParsedAttributesWithRange &Attrs) {
105 const char *SemiError = 0;
106 StmtResult Res;
Sean Huntbbd37c62009-11-21 08:43:09 +0000107
Reid Spencer5f016e22007-07-11 17:01:13 +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 Gregor312eadb2011-04-24 05:37:28 +0000111Retry:
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000112 tok::TokenKind Kind = Tok.getKind();
113 SourceLocation AtLoc;
114 switch (Kind) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000115 case tok::at: // May be a @try or @throw statement
116 {
Richard Smith534986f2012-04-14 00:33:13 +0000117 ProhibitAttributes(Attrs); // TODO: is it correct?
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000118 AtLoc = ConsumeToken(); // consume @
Sebastian Redl43bc2a02008-12-11 20:12:42 +0000119 return ParseObjCAtStatement(AtLoc);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000120 }
Fariborz Jahanian397fcc12007-09-19 19:14:32 +0000121
Douglas Gregor791215b2009-09-21 20:51:25 +0000122 case tok::code_completion:
John McCallf312b1e2010-08-26 23:41:50 +0000123 Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000124 cutOffParsing();
125 return StmtError();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000126
Douglas Gregor312eadb2011-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 Kyrtzidisb9f930d2008-07-12 21:04:42 +0000130 // identifier ':' statement
Richard Smith534986f2012-04-14 00:33:13 +0000131 return ParseLabeledStatement(Attrs);
Argyrios Kyrtzidisb9f930d2008-07-12 21:04:42 +0000132 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000133
Richard Smith05766812012-08-18 00:55:03 +0000134 // Look up the identifier, and typo-correct it to a keyword if it's not
135 // found.
Douglas Gregor3b887352011-04-27 04:48:22 +0000136 if (Next.isNot(tok::coloncolon)) {
Richard Smith05766812012-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 Gregor312eadb2011-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 Smith05766812012-08-18 00:55:03 +0000161 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000162
Richard Smith05766812012-08-18 00:55:03 +0000163 // If the identifier was typo-corrected, try again.
164 if (Tok.isNot(tok::identifier))
Douglas Gregor312eadb2011-04-24 05:37:28 +0000165 goto Retry;
Douglas Gregor312eadb2011-04-24 05:37:28 +0000166 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000167
Douglas Gregor312eadb2011-04-24 05:37:28 +0000168 // Fall through
169 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000170
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000171 default: {
David Blaikie4e4d0842012-03-11 07:00:24 +0000172 if ((getLangOpts().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
Chris Lattner97144fc2009-04-02 04:16:50 +0000173 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000174 DeclGroupPtrTy Decl = ParseDeclaration(Stmts, Declarator::BlockContext,
Richard Smith534986f2012-04-14 00:33:13 +0000175 DeclEnd, Attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000176 return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000177 }
178
179 if (Tok.is(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000180 Diag(Tok, diag::err_expected_statement);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000181 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 }
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Richard Smith534986f2012-04-14 00:33:13 +0000184 return ParseExprStatement();
Chris Lattnerf919bfe2009-03-24 17:04:48 +0000185 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000186
Reid Spencer5f016e22007-07-11 17:01:13 +0000187 case tok::kw_case: // C99 6.8.1: labeled-statement
Richard Smith534986f2012-04-14 00:33:13 +0000188 return ParseCaseStatement();
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 case tok::kw_default: // C99 6.8.1: labeled-statement
Richard Smith534986f2012-04-14 00:33:13 +0000190 return ParseDefaultStatement();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000191
Reid Spencer5f016e22007-07-11 17:01:13 +0000192 case tok::l_brace: // C99 6.8.2: compound-statement
Richard Smith534986f2012-04-14 00:33:13 +0000193 return ParseCompoundStatement();
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000194 case tok::semi: { // C99 6.8.3p3: expression[opt] ';'
Argyrios Kyrtzidise2ca8282011-09-01 21:53:45 +0000195 bool HasLeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
196 return Actions.ActOnNullStmt(ConsumeToken(), HasLeadingEmptyMacro);
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000197 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000198
Reid Spencer5f016e22007-07-11 17:01:13 +0000199 case tok::kw_if: // C99 6.8.4.1: if-statement
Richard Smith534986f2012-04-14 00:33:13 +0000200 return ParseIfStatement(TrailingElseLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 case tok::kw_switch: // C99 6.8.4.2: switch-statement
Richard Smith534986f2012-04-14 00:33:13 +0000202 return ParseSwitchStatement(TrailingElseLoc);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000203
Reid Spencer5f016e22007-07-11 17:01:13 +0000204 case tok::kw_while: // C99 6.8.5.1: while-statement
Richard Smith534986f2012-04-14 00:33:13 +0000205 return ParseWhileStatement(TrailingElseLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000206 case tok::kw_do: // C99 6.8.5.2: do-statement
Richard Smith534986f2012-04-14 00:33:13 +0000207 Res = ParseDoStatement();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000208 SemiError = "do/while";
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 break;
210 case tok::kw_for: // C99 6.8.5.3: for-statement
Richard Smith534986f2012-04-14 00:33:13 +0000211 return ParseForStatement(TrailingElseLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000212
213 case tok::kw_goto: // C99 6.8.6.1: goto-statement
Richard Smith534986f2012-04-14 00:33:13 +0000214 Res = ParseGotoStatement();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000215 SemiError = "goto";
Reid Spencer5f016e22007-07-11 17:01:13 +0000216 break;
217 case tok::kw_continue: // C99 6.8.6.2: continue-statement
Richard Smith534986f2012-04-14 00:33:13 +0000218 Res = ParseContinueStatement();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000219 SemiError = "continue";
Reid Spencer5f016e22007-07-11 17:01:13 +0000220 break;
221 case tok::kw_break: // C99 6.8.6.3: break-statement
Richard Smith534986f2012-04-14 00:33:13 +0000222 Res = ParseBreakStatement();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000223 SemiError = "break";
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 break;
225 case tok::kw_return: // C99 6.8.6.4: return-statement
Richard Smith534986f2012-04-14 00:33:13 +0000226 Res = ParseReturnStatement();
Chris Lattner6869d8e2009-06-14 00:07:48 +0000227 SemiError = "return";
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 break;
Sebastian Redl61364dd2008-12-11 19:30:53 +0000229
Sebastian Redla0fd8652008-12-21 16:41:36 +0000230 case tok::kw_asm: {
Richard Smith534986f2012-04-14 00:33:13 +0000231 ProhibitAttributes(Attrs);
Steve Naroffd62701b2008-02-07 03:50:06 +0000232 bool msAsm = false;
233 Res = ParseAsmStatement(msAsm);
Argyrios Kyrtzidisbf8cafa2010-11-02 02:33:08 +0000234 Res = Actions.ActOnFinishFullStmt(Res.get());
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000235 if (msAsm) return Res;
Chris Lattner6869d8e2009-06-14 00:07:48 +0000236 SemiError = "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 break;
238 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000239
Sebastian Redla0fd8652008-12-21 16:41:36 +0000240 case tok::kw_try: // C++ 15: try-block
Richard Smith534986f2012-04-14 00:33:13 +0000241 return ParseCXXTryBlock();
John Wiegley28bbe4b2011-04-28 01:08:34 +0000242
243 case tok::kw___try:
Richard Smith534986f2012-04-14 00:33:13 +0000244 ProhibitAttributes(Attrs); // TODO: is it correct?
245 return ParseSEHTryBlock();
Eli Friedmanaa5ab262012-02-23 23:47:16 +0000246
247 case tok::annot_pragma_vis:
Richard Smith534986f2012-04-14 00:33:13 +0000248 ProhibitAttributes(Attrs);
Eli Friedmanaa5ab262012-02-23 23:47:16 +0000249 HandlePragmaVisibility();
250 return StmtEmpty();
251
252 case tok::annot_pragma_pack:
Richard Smith534986f2012-04-14 00:33:13 +0000253 ProhibitAttributes(Attrs);
Eli Friedmanaa5ab262012-02-23 23:47:16 +0000254 HandlePragmaPack();
255 return StmtEmpty();
Sebastian Redla0fd8652008-12-21 16:41:36 +0000256 }
257
Reid Spencer5f016e22007-07-11 17:01:13 +0000258 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000259 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000260 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000261 } else if (!Res.isInvalid()) {
Chris Lattner7b3684a2009-06-14 00:23:56 +0000262 // If the result was valid, then we do want to diagnose this. Use
263 // ExpectAndConsume to emit the diagnostic, even though we know it won't
264 // succeed.
265 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner19504402008-11-13 18:52:53 +0000266 // Skip until we see a } or ;, but don't eat it.
267 SkipUntil(tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000268 }
Mike Stump1eb44332009-09-09 15:08:12 +0000269
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000270 return Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000271}
272
Douglas Gregor312eadb2011-04-24 05:37:28 +0000273/// \brief Parse an expression statement.
Richard Smith534986f2012-04-14 00:33:13 +0000274StmtResult Parser::ParseExprStatement() {
Douglas Gregor312eadb2011-04-24 05:37:28 +0000275 // If a case keyword is missing, this is where it should be inserted.
276 Token OldToken = Tok;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000277
Douglas Gregor312eadb2011-04-24 05:37:28 +0000278 // expression[opt] ';'
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000279 ExprResult Expr(ParseExpression());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000280 if (Expr.isInvalid()) {
281 // If the expression is invalid, skip ahead to the next semicolon or '}'.
282 // Not doing this opens us up to the possibility of infinite loops if
283 // ParseExpression does not consume any tokens.
284 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
285 if (Tok.is(tok::semi))
286 ConsumeToken();
287 return StmtError();
288 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000289
Douglas Gregor312eadb2011-04-24 05:37:28 +0000290 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
291 Actions.CheckCaseExpression(Expr.get())) {
292 // If a constant expression is followed by a colon inside a switch block,
293 // suggest a missing case keyword.
294 Diag(OldToken, diag::err_expected_case_before_expression)
295 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000296
Douglas Gregor312eadb2011-04-24 05:37:28 +0000297 // Recover parsing as a case statement.
Richard Smith534986f2012-04-14 00:33:13 +0000298 return ParseCaseStatement(/*MissingCase=*/true, Expr);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000299 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000300
Douglas Gregor312eadb2011-04-24 05:37:28 +0000301 // Otherwise, eat the semicolon.
302 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
303 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Expr.get()));
John Wiegley28bbe4b2011-04-28 01:08:34 +0000304}
Douglas Gregor312eadb2011-04-24 05:37:28 +0000305
Richard Smith534986f2012-04-14 00:33:13 +0000306StmtResult Parser::ParseSEHTryBlock() {
John Wiegley28bbe4b2011-04-28 01:08:34 +0000307 assert(Tok.is(tok::kw___try) && "Expected '__try'");
308 SourceLocation Loc = ConsumeToken();
309 return ParseSEHTryBlockCommon(Loc);
310}
311
312/// ParseSEHTryBlockCommon
313///
314/// seh-try-block:
315/// '__try' compound-statement seh-handler
316///
317/// seh-handler:
318/// seh-except-block
319/// seh-finally-block
320///
321StmtResult Parser::ParseSEHTryBlockCommon(SourceLocation TryLoc) {
322 if(Tok.isNot(tok::l_brace))
323 return StmtError(Diag(Tok,diag::err_expected_lbrace));
324
Joao Matos568ba872012-09-04 17:49:35 +0000325 StmtResult TryBlock(ParseCompoundStatement());
John Wiegley28bbe4b2011-04-28 01:08:34 +0000326 if(TryBlock.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000327 return TryBlock;
John Wiegley28bbe4b2011-04-28 01:08:34 +0000328
329 StmtResult Handler;
Richard Smith534986f2012-04-14 00:33:13 +0000330 if (Tok.is(tok::identifier) &&
Douglas Gregorb57791e2011-10-21 03:57:52 +0000331 Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley28bbe4b2011-04-28 01:08:34 +0000332 SourceLocation Loc = ConsumeToken();
333 Handler = ParseSEHExceptBlock(Loc);
334 } else if (Tok.is(tok::kw___finally)) {
335 SourceLocation Loc = ConsumeToken();
336 Handler = ParseSEHFinallyBlock(Loc);
337 } else {
338 return StmtError(Diag(Tok,diag::err_seh_expected_handler));
339 }
340
341 if(Handler.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000342 return Handler;
John Wiegley28bbe4b2011-04-28 01:08:34 +0000343
344 return Actions.ActOnSEHTryBlock(false /* IsCXXTry */,
345 TryLoc,
346 TryBlock.take(),
347 Handler.take());
348}
349
350/// ParseSEHExceptBlock - Handle __except
351///
352/// seh-except-block:
353/// '__except' '(' seh-filter-expression ')' compound-statement
354///
355StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) {
356 PoisonIdentifierRAIIObject raii(Ident__exception_code, false),
357 raii2(Ident___exception_code, false),
358 raii3(Ident_GetExceptionCode, false);
359
360 if(ExpectAndConsume(tok::l_paren,diag::err_expected_lparen))
361 return StmtError();
362
363 ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope);
364
David Blaikie4e4d0842012-03-11 07:00:24 +0000365 if (getLangOpts().Borland) {
Francois Pichetd7f02df2011-04-28 03:14:31 +0000366 Ident__exception_info->setIsPoisoned(false);
367 Ident___exception_info->setIsPoisoned(false);
368 Ident_GetExceptionInfo->setIsPoisoned(false);
369 }
John Wiegley28bbe4b2011-04-28 01:08:34 +0000370 ExprResult FilterExpr(ParseExpression());
Francois Pichetd7f02df2011-04-28 03:14:31 +0000371
David Blaikie4e4d0842012-03-11 07:00:24 +0000372 if (getLangOpts().Borland) {
Francois Pichetd7f02df2011-04-28 03:14:31 +0000373 Ident__exception_info->setIsPoisoned(true);
374 Ident___exception_info->setIsPoisoned(true);
375 Ident_GetExceptionInfo->setIsPoisoned(true);
376 }
John Wiegley28bbe4b2011-04-28 01:08:34 +0000377
378 if(FilterExpr.isInvalid())
379 return StmtError();
380
381 if(ExpectAndConsume(tok::r_paren,diag::err_expected_rparen))
382 return StmtError();
383
Richard Smith534986f2012-04-14 00:33:13 +0000384 StmtResult Block(ParseCompoundStatement());
John Wiegley28bbe4b2011-04-28 01:08:34 +0000385
386 if(Block.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000387 return Block;
John Wiegley28bbe4b2011-04-28 01:08:34 +0000388
389 return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.take(), Block.take());
390}
391
392/// ParseSEHFinallyBlock - Handle __finally
393///
394/// seh-finally-block:
395/// '__finally' compound-statement
396///
397StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyBlock) {
398 PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
399 raii2(Ident___abnormal_termination, false),
400 raii3(Ident_AbnormalTermination, false);
401
Richard Smith534986f2012-04-14 00:33:13 +0000402 StmtResult Block(ParseCompoundStatement());
John Wiegley28bbe4b2011-04-28 01:08:34 +0000403 if(Block.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000404 return Block;
John Wiegley28bbe4b2011-04-28 01:08:34 +0000405
406 return Actions.ActOnSEHFinallyBlock(FinallyBlock,Block.take());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000407}
408
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000409/// ParseLabeledStatement - We have an identifier and a ':' after it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000410///
411/// labeled-statement:
412/// identifier ':' statement
413/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000414///
Richard Smith534986f2012-04-14 00:33:13 +0000415StmtResult Parser::ParseLabeledStatement(ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000416 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
417 "Not an identifier!");
418
419 Token IdentTok = Tok; // Save the whole token.
420 ConsumeToken(); // eat the identifier.
421
422 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000423
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000424 // identifier ':' statement
425 SourceLocation ColonLoc = ConsumeToken();
426
Richard Smith534986f2012-04-14 00:33:13 +0000427 // Read label attributes, if present. attrs will contain both C++11 and GNU
428 // attributes (if present) after this point.
John McCall7f040a92010-12-24 02:08:15 +0000429 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000430
John McCall60d7b3a2010-08-24 06:29:42 +0000431 StmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000432
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000433 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000434 if (SubStmt.isInvalid())
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000435 SubStmt = Actions.ActOnNullStmt(ColonLoc);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000436
Chris Lattner337e5502011-02-18 01:27:55 +0000437 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
438 IdentTok.getLocation());
Richard Smith534986f2012-04-14 00:33:13 +0000439 if (AttributeList *Attrs = attrs.getList()) {
Chris Lattner337e5502011-02-18 01:27:55 +0000440 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
Richard Smith534986f2012-04-14 00:33:13 +0000441 attrs.clear();
442 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000443
Chris Lattner337e5502011-02-18 01:27:55 +0000444 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
445 SubStmt.get());
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000446}
Reid Spencer5f016e22007-07-11 17:01:13 +0000447
448/// ParseCaseStatement
449/// labeled-statement:
450/// 'case' constant-expression ':' statement
451/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
452///
Richard Smith534986f2012-04-14 00:33:13 +0000453StmtResult Parser::ParseCaseStatement(bool MissingCase, ExprResult Expr) {
Richard Smith46f11102011-04-21 22:48:40 +0000454 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Chris Lattner24e1e702009-03-04 04:23:07 +0000456 // It is very very common for code to contain many case statements recursively
457 // nested, as in (but usually without indentation):
458 // case 1:
459 // case 2:
460 // case 3:
461 // case 4:
462 // case 5: etc.
463 //
464 // Parsing this naively works, but is both inefficient and can cause us to run
465 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner26140c62009-03-04 18:24:58 +0000466 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner24e1e702009-03-04 04:23:07 +0000467 // but all the grossness is constrained to ParseCaseStatement (and some
468 // wierdness in the actions), so this is just local grossness :).
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Chris Lattner24e1e702009-03-04 04:23:07 +0000470 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
471 // example above.
John McCall60d7b3a2010-08-24 06:29:42 +0000472 StmtResult TopLevelCase(true);
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Chris Lattner24e1e702009-03-04 04:23:07 +0000474 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
475 // gets updated each time a new case is parsed, and whose body is unset so
476 // far. When parsing 'case 4', this is the 'case 3' node.
Richard Trieub2fc6902011-09-09 02:16:15 +0000477 Stmt *DeepestParsedCaseStmt = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Chris Lattner24e1e702009-03-04 04:23:07 +0000479 // While we have case statements, eat and stack them.
David Majnemer0e1e69c2011-06-13 05:50:12 +0000480 SourceLocation ColonLoc;
Chris Lattner24e1e702009-03-04 04:23:07 +0000481 do {
Richard Trieubb9b80c2011-04-21 21:44:26 +0000482 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
483 ConsumeToken(); // eat the 'case'.
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000485 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000486 Actions.CodeCompleteCase(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000487 cutOffParsing();
488 return StmtError();
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000489 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000490
Chris Lattner6fb09c82009-12-10 00:38:54 +0000491 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
492 /// Disable this form of error recovery while we're parsing the case
493 /// expression.
494 ColonProtectionRAIIObject ColonProtection(*this);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000495
Richard Trieubb9b80c2011-04-21 21:44:26 +0000496 ExprResult LHS(MissingCase ? Expr : ParseConstantExpression());
497 MissingCase = false;
Chris Lattner24e1e702009-03-04 04:23:07 +0000498 if (LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000499 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000500 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000501 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000502
Chris Lattner24e1e702009-03-04 04:23:07 +0000503 // GNU case range extension.
504 SourceLocation DotDotDotLoc;
John McCall60d7b3a2010-08-24 06:29:42 +0000505 ExprResult RHS;
Chris Lattner24e1e702009-03-04 04:23:07 +0000506 if (Tok.is(tok::ellipsis)) {
507 Diag(Tok, diag::ext_gnu_case_range);
508 DotDotDotLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000509
Chris Lattner24e1e702009-03-04 04:23:07 +0000510 RHS = ParseConstantExpression();
511 if (RHS.isInvalid()) {
512 SkipUntil(tok::colon);
513 return StmtError();
514 }
515 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000516
Chris Lattner6fb09c82009-12-10 00:38:54 +0000517 ColonProtection.restore();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000518
John McCallf6a3ab02011-01-22 09:28:32 +0000519 if (Tok.is(tok::colon)) {
520 ColonLoc = ConsumeToken();
521
522 // Treat "case blah;" as a typo for "case blah:".
523 } else if (Tok.is(tok::semi)) {
524 ColonLoc = ConsumeToken();
525 Diag(ColonLoc, diag::err_expected_colon_after) << "'case'"
526 << FixItHint::CreateReplacement(ColonLoc, ":");
527 } else {
Douglas Gregor662a4822010-12-23 22:56:40 +0000528 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
529 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'"
530 << FixItHint::CreateInsertion(ExpectedLoc, ":");
531 ColonLoc = ExpectedLoc;
Chris Lattner24e1e702009-03-04 04:23:07 +0000532 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000533
John McCall60d7b3a2010-08-24 06:29:42 +0000534 StmtResult Case =
John McCall9ae2f072010-08-23 23:25:46 +0000535 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
536 RHS.get(), ColonLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000537
Chris Lattner24e1e702009-03-04 04:23:07 +0000538 // If we had a sema error parsing this case, then just ignore it and
539 // continue parsing the sub-stmt.
540 if (Case.isInvalid()) {
541 if (TopLevelCase.isInvalid()) // No parsed case stmts.
542 return ParseStatement();
543 // Otherwise, just don't add it as a nested case.
544 } else {
545 // If this is the first case statement we parsed, it becomes TopLevelCase.
546 // Otherwise we link it into the current chain.
John McCallca0408f2010-08-23 06:44:23 +0000547 Stmt *NextDeepest = Case.get();
Chris Lattner24e1e702009-03-04 04:23:07 +0000548 if (TopLevelCase.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000549 TopLevelCase = Case;
Chris Lattner24e1e702009-03-04 04:23:07 +0000550 else
John McCall9ae2f072010-08-23 23:25:46 +0000551 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner24e1e702009-03-04 04:23:07 +0000552 DeepestParsedCaseStmt = NextDeepest;
553 }
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Chris Lattner24e1e702009-03-04 04:23:07 +0000555 // Handle all case statements.
556 } while (Tok.is(tok::kw_case));
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Chris Lattner24e1e702009-03-04 04:23:07 +0000558 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Chris Lattner24e1e702009-03-04 04:23:07 +0000560 // If we found a non-case statement, start by parsing it.
John McCall60d7b3a2010-08-24 06:29:42 +0000561 StmtResult SubStmt;
Mike Stump1eb44332009-09-09 15:08:12 +0000562
Chris Lattner24e1e702009-03-04 04:23:07 +0000563 if (Tok.isNot(tok::r_brace)) {
564 SubStmt = ParseStatement();
565 } else {
566 // Nicely diagnose the common error "switch (X) { case 4: }", which is
567 // not valid.
David Majnemer63f04ab2011-06-14 15:24:38 +0000568 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
Richard Smith85b29a42012-02-17 01:35:32 +0000569 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
570 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
Chris Lattner24e1e702009-03-04 04:23:07 +0000571 SubStmt = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000572 }
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Chris Lattner24e1e702009-03-04 04:23:07 +0000574 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000575 if (SubStmt.isInvalid())
Chris Lattner24e1e702009-03-04 04:23:07 +0000576 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000577
Chris Lattner24e1e702009-03-04 04:23:07 +0000578 // Install the body into the most deeply-nested case.
John McCall9ae2f072010-08-23 23:25:46 +0000579 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000580
Chris Lattner24e1e702009-03-04 04:23:07 +0000581 // Return the top level parsed statement tree.
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000582 return TopLevelCase;
Reid Spencer5f016e22007-07-11 17:01:13 +0000583}
584
585/// ParseDefaultStatement
586/// labeled-statement:
587/// 'default' ':' statement
588/// Note that this does not parse the 'statement' at the end.
589///
Richard Smith534986f2012-04-14 00:33:13 +0000590StmtResult Parser::ParseDefaultStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000591 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
593
Douglas Gregor662a4822010-12-23 22:56:40 +0000594 SourceLocation ColonLoc;
John McCallf6a3ab02011-01-22 09:28:32 +0000595 if (Tok.is(tok::colon)) {
596 ColonLoc = ConsumeToken();
597
598 // Treat "default;" as a typo for "default:".
599 } else if (Tok.is(tok::semi)) {
600 ColonLoc = ConsumeToken();
601 Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
602 << FixItHint::CreateReplacement(ColonLoc, ":");
603 } else {
Douglas Gregor662a4822010-12-23 22:56:40 +0000604 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
605 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
606 << FixItHint::CreateInsertion(ExpectedLoc, ":");
607 ColonLoc = ExpectedLoc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000608 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000609
Richard Smith85b29a42012-02-17 01:35:32 +0000610 StmtResult SubStmt;
611
612 if (Tok.isNot(tok::r_brace)) {
613 SubStmt = ParseStatement();
614 } else {
615 // Diagnose the common error "switch (X) {... default: }", which is
616 // not valid.
David Majnemer63f04ab2011-06-14 15:24:38 +0000617 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
Richard Smith85b29a42012-02-17 01:35:32 +0000618 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
619 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
620 SubStmt = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000621 }
622
Richard Smith85b29a42012-02-17 01:35:32 +0000623 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000624 if (SubStmt.isInvalid())
Richard Smith85b29a42012-02-17 01:35:32 +0000625 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000626
Sebastian Redl117054a2008-12-28 16:13:43 +0000627 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000628 SubStmt.get(), getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +0000629}
630
Richard Smith534986f2012-04-14 00:33:13 +0000631StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
632 return ParseCompoundStatement(isStmtExpr, Scope::DeclScope);
Douglas Gregorbca01b42011-07-06 22:04:06 +0000633}
Reid Spencer5f016e22007-07-11 17:01:13 +0000634
635/// ParseCompoundStatement - Parse a "{}" block.
636///
637/// compound-statement: [C99 6.8.2]
638/// { block-item-list[opt] }
639/// [GNU] { label-declarations block-item-list } [TODO]
640///
641/// block-item-list:
642/// block-item
643/// block-item-list block-item
644///
645/// block-item:
646/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000647/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000648/// statement
649/// [OMP] openmp-directive [TODO]
650///
651/// [GNU] label-declarations:
652/// [GNU] label-declaration
653/// [GNU] label-declarations label-declaration
654///
655/// [GNU] label-declaration:
656/// [GNU] '__label__' identifier-list ';'
657///
658/// [OMP] openmp-directive: [TODO]
659/// [OMP] barrier-directive
660/// [OMP] flush-directive
661///
Richard Smith534986f2012-04-14 00:33:13 +0000662StmtResult Parser::ParseCompoundStatement(bool isStmtExpr,
Douglas Gregorbca01b42011-07-06 22:04:06 +0000663 unsigned ScopeFlags) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000664 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000665
Chris Lattner31e05722007-08-26 06:24:45 +0000666 // Enter a scope to hold everything within the compound stmt. Compound
667 // statements can always hold declarations.
Douglas Gregorbca01b42011-07-06 22:04:06 +0000668 ParseScope CompoundScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000669
670 // Parse the statements in the body.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000671 return ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000672}
673
Reid Spencer5f016e22007-07-11 17:01:13 +0000674/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000675/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000676/// consume the '}' at the end of the block. It does not manipulate the scope
677/// stack.
John McCall60d7b3a2010-08-24 06:29:42 +0000678StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump1eb44332009-09-09 15:08:12 +0000679 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerae50fa02009-03-05 00:00:31 +0000680 Tok.getLocation(),
681 "in compound statement ('{}')");
Douglas Gregor0fbda682010-09-15 14:51:05 +0000682 InMessageExpressionRAIIObject InMessage(*this, false);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000683 BalancedDelimiterTracker T(*this, tok::l_brace);
684 if (T.consumeOpen())
685 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000686
Dmitri Gribenko625bb562012-02-14 22:14:32 +0000687 Sema::CompoundScopeRAII CompoundScope(Actions);
688
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +0000689 StmtVector Stmts;
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000690
Chris Lattner4ae493c2011-02-18 02:08:43 +0000691 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
692 // only allowed at the start of a compound stmt regardless of the language.
693 while (Tok.is(tok::kw___label__)) {
694 SourceLocation LabelLoc = ConsumeToken();
695 Diag(LabelLoc, diag::ext_gnu_local_label);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000696
Chris Lattner5f9e2722011-07-23 10:55:15 +0000697 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4ae493c2011-02-18 02:08:43 +0000698 while (1) {
699 if (Tok.isNot(tok::identifier)) {
700 Diag(Tok, diag::err_expected_ident);
701 break;
702 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000703
Chris Lattner4ae493c2011-02-18 02:08:43 +0000704 IdentifierInfo *II = Tok.getIdentifierInfo();
705 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara67843042011-03-05 18:21:20 +0000706 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000707
Chris Lattner4ae493c2011-02-18 02:08:43 +0000708 if (!Tok.is(tok::comma))
709 break;
710 ConsumeToken();
711 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000712
John McCall0b7e6782011-03-24 11:26:52 +0000713 DeclSpec DS(AttrFactory);
Chris Lattner4ae493c2011-02-18 02:08:43 +0000714 DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
715 DeclsInGroup.data(), DeclsInGroup.size());
716 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000717
Chris Lattner8bb21d32012-04-28 16:12:17 +0000718 ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
Chris Lattner4ae493c2011-02-18 02:08:43 +0000719 if (R.isUsable())
720 Stmts.push_back(R.release());
721 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000722
Chris Lattner4ae493c2011-02-18 02:08:43 +0000723 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000724 if (Tok.is(tok::annot_pragma_unused)) {
725 HandlePragmaUnused();
726 continue;
727 }
728
David Blaikie4e4d0842012-03-11 07:00:24 +0000729 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet1e862692011-05-06 20:48:22 +0000730 Tok.is(tok::kw___if_not_exists))) {
731 ParseMicrosoftIfExistsStatement(Stmts);
732 continue;
733 }
734
John McCall60d7b3a2010-08-24 06:29:42 +0000735 StmtResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000736 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000737 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattner45a566c2007-08-27 01:01:57 +0000738 } else {
739 // __extension__ can start declarations and it can also be a unary
740 // operator for expressions. Consume multiple __extension__ markers here
741 // until we can determine which is which.
Eli Friedmanadf077f2009-01-27 08:43:38 +0000742 // FIXME: This loses extension expressions in the AST!
Chris Lattner45a566c2007-08-27 01:01:57 +0000743 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000744 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000745 ConsumeToken();
Chris Lattner39146d62008-10-20 06:51:33 +0000746
John McCall0b7e6782011-03-24 11:26:52 +0000747 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith6ee326a2012-04-10 01:32:12 +0000748 MaybeParseCXX0XAttributes(attrs, 0, /*MightBeObjCMessageSend*/ true);
Sean Huntbbd37c62009-11-21 08:43:09 +0000749
Chris Lattner45a566c2007-08-27 01:01:57 +0000750 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000751 if (isDeclarationStatement()) {
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000752 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000753 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000754 ExtensionRAIIObject O(Diags);
755
Chris Lattner97144fc2009-04-02 04:16:50 +0000756 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000757 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
758 Declarator::BlockContext, DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000759 attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000760 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattner45a566c2007-08-27 01:01:57 +0000761 } else {
Eli Friedmanadf077f2009-01-27 08:43:38 +0000762 // Otherwise this was a unary __extension__ marker.
John McCall60d7b3a2010-08-24 06:29:42 +0000763 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattner043a0b52008-03-13 06:32:11 +0000764
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000765 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000766 SkipUntil(tok::semi);
767 continue;
768 }
Sebastian Redlf512e822009-01-18 18:03:53 +0000769
Sean Huntbbd37c62009-11-21 08:43:09 +0000770 // FIXME: Use attributes?
Chris Lattner39146d62008-10-20 06:51:33 +0000771 // Eat the semicolon at the end of stmt and convert the expr into a
772 // statement.
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000773 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCall9ae2f072010-08-23 23:25:46 +0000774 R = Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.get()));
Chris Lattner45a566c2007-08-27 01:01:57 +0000775 }
776 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000777
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000778 if (R.isUsable())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000779 Stmts.push_back(R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000780 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000781
Argyrios Kyrtzidis5d5ed592012-03-24 02:26:51 +0000782 SourceLocation CloseLoc = Tok.getLocation();
783
Reid Spencer5f016e22007-07-11 17:01:13 +0000784 // We broke out of the while loop because we found a '}' or EOF.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000785 if (Tok.isNot(tok::r_brace)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000786 Diag(Tok, diag::err_expected_rbrace);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000787 Diag(T.getOpenLocation(), diag::note_matching) << "{";
Argyrios Kyrtzidis5d5ed592012-03-24 02:26:51 +0000788 // Recover by creating a compound statement with what we parsed so far,
789 // instead of dropping everything and returning StmtError();
790 } else {
791 if (!T.consumeClose())
792 CloseLoc = T.getCloseLocation();
Reid Spencer5f016e22007-07-11 17:01:13 +0000793 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000794
Argyrios Kyrtzidis5d5ed592012-03-24 02:26:51 +0000795 return Actions.ActOnCompoundStmt(T.getOpenLocation(), CloseLoc,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000796 Stmts, isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000797}
798
Chris Lattner15ff1112008-12-12 06:31:07 +0000799/// ParseParenExprOrCondition:
800/// [C ] '(' expression ')'
Chris Lattnerff871fb2008-12-12 06:35:28 +0000801/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattner15ff1112008-12-12 06:31:07 +0000802///
803/// This function parses and performs error recovery on the specified condition
804/// or expression (depending on whether we're in C++ or C mode). This function
805/// goes out of its way to recover well. It returns true if there was a parser
806/// error (the right paren couldn't be found), which indicates that the caller
807/// should try to recover harder. It returns false if the condition is
808/// successfully parsed. Note that a successful parse can still have semantic
809/// errors in the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000810bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCalld226f652010-08-21 09:40:31 +0000811 Decl *&DeclResult,
Douglas Gregor586596f2010-05-06 17:25:47 +0000812 SourceLocation Loc,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000813 bool ConvertToBoolean) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000814 BalancedDelimiterTracker T(*this, tok::l_paren);
815 T.consumeOpen();
816
David Blaikie4e4d0842012-03-11 07:00:24 +0000817 if (getLangOpts().CPlusPlus)
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000818 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000819 else {
820 ExprResult = ParseExpression();
John McCalld226f652010-08-21 09:40:31 +0000821 DeclResult = 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000822
Douglas Gregor586596f2010-05-06 17:25:47 +0000823 // If required, convert to a boolean value.
824 if (!ExprResult.isInvalid() && ConvertToBoolean)
825 ExprResult
John McCall9ae2f072010-08-23 23:25:46 +0000826 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000827 }
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Chris Lattner15ff1112008-12-12 06:31:07 +0000829 // If the parser was confused by the condition and we don't have a ')', try to
830 // recover by skipping ahead to a semi and bailing out. If condexp is
831 // semantically invalid but we have well formed code, keep going.
John McCalld226f652010-08-21 09:40:31 +0000832 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000833 SkipUntil(tok::semi);
834 // Skipping may have stopped if it found the containing ')'. If so, we can
835 // continue parsing the if statement.
836 if (Tok.isNot(tok::r_paren))
837 return true;
838 }
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Chris Lattner15ff1112008-12-12 06:31:07 +0000840 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000841 T.consumeClose();
Chad Rosierb6604462012-07-10 21:35:27 +0000842
Chris Lattnerbddc7e52012-04-28 16:24:20 +0000843 // Check for extraneous ')'s to catch things like "if (foo())) {". We know
844 // that all callers are looking for a statement after the condition, so ")"
845 // isn't valid.
846 while (Tok.is(tok::r_paren)) {
847 Diag(Tok, diag::err_extraneous_rparen_in_condition)
848 << FixItHint::CreateRemoval(Tok.getLocation());
849 ConsumeParen();
850 }
Chad Rosierb6604462012-07-10 21:35:27 +0000851
Chris Lattner15ff1112008-12-12 06:31:07 +0000852 return false;
853}
854
855
Reid Spencer5f016e22007-07-11 17:01:13 +0000856/// ParseIfStatement
857/// if-statement: [C99 6.8.4.1]
858/// 'if' '(' expression ')' statement
859/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000860/// [C++] 'if' '(' condition ')' statement
861/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000862///
Richard Smith534986f2012-04-14 00:33:13 +0000863StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000864 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000865 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
866
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000867 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000868 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000869 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000870 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000871 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000872
David Blaikie4e4d0842012-03-11 07:00:24 +0000873 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000874
Chris Lattner22153252007-08-26 23:08:06 +0000875 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
876 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000877 //
878 // C++ 6.4p3:
879 // A name introduced by a declaration in a condition is in scope from its
880 // point of declaration until the end of the substatements controlled by the
881 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000882 // C++ 3.3.2p4:
883 // Names declared in the for-init-statement, and in the condition of if,
884 // while, for, and switch statements are local to the if, while, for, or
885 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000886 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000887 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +0000888
Reid Spencer5f016e22007-07-11 17:01:13 +0000889 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000890 ExprResult CondExp;
John McCalld226f652010-08-21 09:40:31 +0000891 Decl *CondVar = 0;
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000892 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +0000893 return StmtError();
Chris Lattner18914bc2008-12-12 06:19:11 +0000894
David Blaikiedef07622012-05-16 04:20:04 +0000895 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get(), IfLoc));
Mike Stump1eb44332009-09-09 15:08:12 +0000896
Chris Lattner0ecea032007-08-22 05:28:50 +0000897 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000898 // there is no compound stmt. C90 does not have this clause. We only do this
899 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000900 //
901 // C++ 6.4p1:
902 // The substatement in a selection-statement (each substatement, in the else
903 // form of the if statement) implicitly defines a local scope.
904 //
905 // For C++ we create a scope for the condition and a new scope for
906 // substatements because:
907 // -When the 'then' scope exits, we want the condition declaration to still be
908 // active for the 'else' scope too.
909 // -Sema will detect name clashes by considering declarations of a
910 // 'ControlScope' as part of its direct subscope.
911 // -If we wanted the condition and substatement to be in the same scope, we
912 // would have to notify ParseStatement not to create a new scope. It's
913 // simpler to let it create a new scope.
914 //
Mike Stump1eb44332009-09-09 15:08:12 +0000915 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000916 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000917
Chris Lattnerb96728d2007-10-29 05:08:52 +0000918 // Read the 'then' stmt.
919 SourceLocation ThenStmtLoc = Tok.getLocation();
Nico Weber5cb94a72011-12-22 23:26:17 +0000920
921 SourceLocation InnerStatementTrailingElseLoc;
922 StmtResult ThenStmt(ParseStatement(&InnerStatementTrailingElseLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000923
Chris Lattnera36ce712007-08-22 05:16:28 +0000924 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000925 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000926
Reid Spencer5f016e22007-07-11 17:01:13 +0000927 // If it has an else, parse it.
928 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +0000929 SourceLocation ElseStmtLoc;
John McCall60d7b3a2010-08-24 06:29:42 +0000930 StmtResult ElseStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000931
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000932 if (Tok.is(tok::kw_else)) {
Nico Weber5cb94a72011-12-22 23:26:17 +0000933 if (TrailingElseLoc)
934 *TrailingElseLoc = Tok.getLocation();
935
Reid Spencer5f016e22007-07-11 17:01:13 +0000936 ElseLoc = ConsumeToken();
Chris Lattner966c78b2010-04-12 06:12:50 +0000937 ElseStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000938
Chris Lattner0ecea032007-08-22 05:28:50 +0000939 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000940 // there is no compound stmt. C90 does not have this clause. We only do
941 // this if the body isn't a compound statement to avoid push/pop in common
942 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000943 //
944 // C++ 6.4p1:
945 // The substatement in a selection-statement (each substatement, in the else
946 // form of the if statement) implicitly defines a local scope.
947 //
Sebastian Redl61364dd2008-12-11 19:30:53 +0000948 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000949 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000950
Reid Spencer5f016e22007-07-11 17:01:13 +0000951 ElseStmt = ParseStatement();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000952
Chris Lattnera36ce712007-08-22 05:16:28 +0000953 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000954 InnerScope.Exit();
Douglas Gregord2d8be62011-07-30 08:36:53 +0000955 } else if (Tok.is(tok::code_completion)) {
956 Actions.CodeCompleteAfterIf(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000957 cutOffParsing();
958 return StmtError();
Nico Weber5cb94a72011-12-22 23:26:17 +0000959 } else if (InnerStatementTrailingElseLoc.isValid()) {
960 Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else);
Reid Spencer5f016e22007-07-11 17:01:13 +0000961 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000962
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000963 IfScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +0000964
Chris Lattner18914bc2008-12-12 06:19:11 +0000965 // If the condition was invalid, discard the if statement. We could recover
966 // better by replacing it with a valid expr, but don't do that yet.
John McCalld226f652010-08-21 09:40:31 +0000967 if (CondExp.isInvalid() && !CondVar)
Chris Lattner18914bc2008-12-12 06:19:11 +0000968 return StmtError();
Chris Lattner22153252007-08-26 23:08:06 +0000969
Chris Lattnerb96728d2007-10-29 05:08:52 +0000970 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump1eb44332009-09-09 15:08:12 +0000971 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattnerb96728d2007-10-29 05:08:52 +0000972 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000973 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
974 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
975 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +0000976 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000977 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +0000978 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000979
Chris Lattnerb96728d2007-10-29 05:08:52 +0000980 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000981 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000982 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000983 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +0000984 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000985
John McCall9ae2f072010-08-23 23:25:46 +0000986 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000987 ElseLoc, ElseStmt.get());
Reid Spencer5f016e22007-07-11 17:01:13 +0000988}
989
990/// ParseSwitchStatement
991/// switch-statement:
992/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000993/// [C++] 'switch' '(' condition ')' statement
Richard Smith534986f2012-04-14 00:33:13 +0000994StmtResult Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000995 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000996 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
997
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000998 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000999 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +00001000 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +00001001 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001002 }
Chris Lattner22153252007-08-26 23:08:06 +00001003
David Blaikie4e4d0842012-03-11 07:00:24 +00001004 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001005
Chris Lattner22153252007-08-26 23:08:06 +00001006 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1007 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001008 //
1009 // C++ 6.4p3:
1010 // A name introduced by a declaration in a condition is in scope from its
1011 // point of declaration until the end of the substatements controlled by the
1012 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001013 // C++ 3.3.2p4:
1014 // Names declared in the for-init-statement, and in the condition of if,
1015 // while, for, and switch statements are local to the if, while, for, or
1016 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001017 //
Richard Trieubb9b80c2011-04-21 21:44:26 +00001018 unsigned ScopeFlags = Scope::BreakScope | Scope::SwitchScope;
Chris Lattner15ff1112008-12-12 06:31:07 +00001019 if (C99orCXX)
1020 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001021 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001022
1023 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +00001024 ExprResult Cond;
John McCalld226f652010-08-21 09:40:31 +00001025 Decl *CondVar = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +00001026 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redl9a920342008-12-11 19:48:14 +00001027 return StmtError();
Eli Friedman2342ef72008-12-17 22:19:57 +00001028
John McCall60d7b3a2010-08-24 06:29:42 +00001029 StmtResult Switch
John McCall9ae2f072010-08-23 23:25:46 +00001030 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001031
Douglas Gregor586596f2010-05-06 17:25:47 +00001032 if (Switch.isInvalid()) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001033 // Skip the switch body.
Douglas Gregor586596f2010-05-06 17:25:47 +00001034 // FIXME: This is not optimal recovery, but parsing the body is more
1035 // dangerous due to the presence of case and default statements, which
1036 // will have no place to connect back with the switch.
Douglas Gregor4186ff42010-05-20 23:20:59 +00001037 if (Tok.is(tok::l_brace)) {
1038 ConsumeBrace();
1039 SkipUntil(tok::r_brace, false, false);
1040 } else
Douglas Gregor586596f2010-05-06 17:25:47 +00001041 SkipUntil(tok::semi);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001042 return Switch;
Douglas Gregor586596f2010-05-06 17:25:47 +00001043 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001044
Chris Lattner0ecea032007-08-22 05:28:50 +00001045 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001046 // there is no compound stmt. C90 does not have this clause. We only do this
1047 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001048 //
1049 // C++ 6.4p1:
1050 // The substatement in a selection-statement (each substatement, in the else
1051 // form of the if statement) implicitly defines a local scope.
1052 //
1053 // See comments in ParseIfStatement for why we create a scope for the
1054 // condition and a new scope for substatement in C++.
1055 //
Mike Stump1eb44332009-09-09 15:08:12 +00001056 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001057 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +00001058
Reid Spencer5f016e22007-07-11 17:01:13 +00001059 // Read the body statement.
Nico Weber5cb94a72011-12-22 23:26:17 +00001060 StmtResult Body(ParseStatement(TrailingElseLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001061
Chris Lattner7e52de42010-01-24 01:50:29 +00001062 // Pop the scopes.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001063 InnerScope.Exit();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001064 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +00001065
Dmitri Gribenko625bb562012-02-14 22:14:32 +00001066 if (Body.isInvalid()) {
Chris Lattner7e52de42010-01-24 01:50:29 +00001067 // FIXME: Remove the case statement list from the Switch statement.
Dmitri Gribenko625bb562012-02-14 22:14:32 +00001068
1069 // Put the synthesized null statement on the same line as the end of switch
1070 // condition.
1071 SourceLocation SynthesizedNullStmtLocation = Cond.get()->getLocEnd();
1072 Body = Actions.ActOnNullStmt(SynthesizedNullStmtLocation);
1073 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001074
John McCall9ae2f072010-08-23 23:25:46 +00001075 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Reid Spencer5f016e22007-07-11 17:01:13 +00001076}
1077
1078/// ParseWhileStatement
1079/// while-statement: [C99 6.8.5.1]
1080/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001081/// [C++] 'while' '(' condition ')' statement
Richard Smith534986f2012-04-14 00:33:13 +00001082StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001083 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001084 SourceLocation WhileLoc = Tok.getLocation();
1085 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001086
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001087 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001088 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +00001089 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +00001090 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001091 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001092
David Blaikie4e4d0842012-03-11 07:00:24 +00001093 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001094
Chris Lattner22153252007-08-26 23:08:06 +00001095 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1096 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001097 //
1098 // C++ 6.4p3:
1099 // A name introduced by a declaration in a condition is in scope from its
1100 // point of declaration until the end of the substatements controlled by the
1101 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001102 // C++ 3.3.2p4:
1103 // Names declared in the for-init-statement, and in the condition of if,
1104 // while, for, and switch statements are local to the if, while, for, or
1105 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001106 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001107 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001108 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001109 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1110 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +00001111 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001112 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1113 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001114
1115 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +00001116 ExprResult Cond;
John McCalld226f652010-08-21 09:40:31 +00001117 Decl *CondVar = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +00001118 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +00001119 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001120
David Blaikiedef07622012-05-16 04:20:04 +00001121 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get(), WhileLoc));
Mike Stump1eb44332009-09-09 15:08:12 +00001122
Chris Lattner0ecea032007-08-22 05:28:50 +00001123 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001124 // there is no compound stmt. C90 does not have this clause. We only do this
1125 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001126 //
1127 // C++ 6.5p2:
1128 // The substatement in an iteration-statement implicitly defines a local scope
1129 // which is entered and exited each time through the loop.
1130 //
1131 // See comments in ParseIfStatement for why we create a scope for the
1132 // condition and a new scope for substatement in C++.
1133 //
Mike Stump1eb44332009-09-09 15:08:12 +00001134 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001135 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +00001136
Reid Spencer5f016e22007-07-11 17:01:13 +00001137 // Read the body statement.
Nico Weber5cb94a72011-12-22 23:26:17 +00001138 StmtResult Body(ParseStatement(TrailingElseLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001139
Chris Lattner0ecea032007-08-22 05:28:50 +00001140 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001141 InnerScope.Exit();
1142 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +00001143
John McCalld226f652010-08-21 09:40:31 +00001144 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001145 return StmtError();
1146
John McCall9ae2f072010-08-23 23:25:46 +00001147 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Reid Spencer5f016e22007-07-11 17:01:13 +00001148}
1149
1150/// ParseDoStatement
1151/// do-statement: [C99 6.8.5.2]
1152/// 'do' statement 'while' '(' expression ')' ';'
1153/// Note: this lets the caller parse the end ';'.
Richard Smith534986f2012-04-14 00:33:13 +00001154StmtResult Parser::ParseDoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001155 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001156 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001157
Chris Lattner22153252007-08-26 23:08:06 +00001158 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1159 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001160 unsigned ScopeFlags;
David Blaikie4e4d0842012-03-11 07:00:24 +00001161 if (getLangOpts().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001162 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +00001163 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001164 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +00001165
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001166 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001167
Chris Lattner0ecea032007-08-22 05:28:50 +00001168 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001169 // there is no compound stmt. C90 does not have this clause. We only do this
1170 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +00001171 //
1172 // C++ 6.5p2:
1173 // The substatement in an iteration-statement implicitly defines a local scope
1174 // which is entered and exited each time through the loop.
1175 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001176 ParseScope InnerScope(this, Scope::DeclScope,
David Blaikie4e4d0842012-03-11 07:00:24 +00001177 (getLangOpts().C99 || getLangOpts().CPlusPlus) &&
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001178 Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +00001179
Reid Spencer5f016e22007-07-11 17:01:13 +00001180 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +00001181 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +00001182
Chris Lattner0ecea032007-08-22 05:28:50 +00001183 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001184 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001185
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001186 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001187 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +00001188 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +00001189 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +00001190 SkipUntil(tok::semi, false, true);
1191 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001192 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001193 }
1194 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001195
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001196 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001197 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +00001198 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001199 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001200 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001201
Chris Lattnerff871fb2008-12-12 06:35:28 +00001202 // Parse the parenthesized condition.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001203 BalancedDelimiterTracker T(*this, tok::l_paren);
1204 T.consumeOpen();
Chad Rosierb6604462012-07-10 21:35:27 +00001205
Sean Hunt2edf0a22012-06-23 05:07:58 +00001206 // FIXME: Do not just parse the attribute contents and throw them away
1207 ParsedAttributesWithRange attrs(AttrFactory);
1208 MaybeParseCXX0XAttributes(attrs);
1209 ProhibitAttributes(attrs);
1210
John McCall60d7b3a2010-08-24 06:29:42 +00001211 ExprResult Cond = ParseExpression();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001212 T.consumeClose();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001213 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001214
Sebastian Redl9a920342008-12-11 19:48:14 +00001215 if (Cond.isInvalid() || Body.isInvalid())
1216 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001217
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001218 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(),
1219 Cond.get(), T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001220}
1221
1222/// ParseForStatement
1223/// for-statement: [C99 6.8.5.3]
1224/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1225/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001226/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1227/// [C++] statement
Richard Smithad762fc2011-04-14 22:09:26 +00001228/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001229/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1230/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001231///
1232/// [C++] for-init-statement:
1233/// [C++] expression-statement
1234/// [C++] simple-declaration
1235///
Richard Smithad762fc2011-04-14 22:09:26 +00001236/// [C++0x] for-range-declaration:
1237/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1238/// [C++0x] for-range-initializer:
1239/// [C++0x] expression
1240/// [C++0x] braced-init-list [TODO]
Richard Smith534986f2012-04-14 00:33:13 +00001241StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001242 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001243 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001244
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001245 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001246 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +00001247 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +00001248 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001249 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001250
Chad Rosierb6604462012-07-10 21:35:27 +00001251 bool C99orCXXorObjC = getLangOpts().C99 || getLangOpts().CPlusPlus ||
1252 getLangOpts().ObjC1;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001253
Chris Lattner22153252007-08-26 23:08:06 +00001254 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1255 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001256 //
1257 // C++ 6.4p3:
1258 // A name introduced by a declaration in a condition is in scope from its
1259 // point of declaration until the end of the substatements controlled by the
1260 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001261 // C++ 3.3.2p4:
1262 // Names declared in the for-init-statement, and in the condition of if,
1263 // while, for, and switch statements are local to the if, while, for, or
1264 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001265 // C++ 6.5.3p1:
1266 // Names declared in the for-init-statement are in the same declarative-region
1267 // as those declared in the condition.
1268 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001269 unsigned ScopeFlags;
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001270 if (C99orCXXorObjC)
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001271 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1272 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +00001273 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001274 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1275
1276 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001277
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001278 BalancedDelimiterTracker T(*this, tok::l_paren);
1279 T.consumeOpen();
1280
John McCall60d7b3a2010-08-24 06:29:42 +00001281 ExprResult Value;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001282
Richard Smithad762fc2011-04-14 22:09:26 +00001283 bool ForEach = false, ForRange = false;
John McCall60d7b3a2010-08-24 06:29:42 +00001284 StmtResult FirstPart;
Douglas Gregoreecf38f2010-05-06 21:39:56 +00001285 bool SecondPartIsInvalid = false;
Douglas Gregor586596f2010-05-06 17:25:47 +00001286 FullExprArg SecondPart(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001287 ExprResult Collection;
Richard Smithad762fc2011-04-14 22:09:26 +00001288 ForRangeInit ForRangeInit;
Douglas Gregor586596f2010-05-06 17:25:47 +00001289 FullExprArg ThirdPart(Actions);
John McCalld226f652010-08-21 09:40:31 +00001290 Decl *SecondVar = 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001291
Douglas Gregor791215b2009-09-21 20:51:25 +00001292 if (Tok.is(tok::code_completion)) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001293 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001294 C99orCXXorObjC? Sema::PCC_ForInit
1295 : Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001296 cutOffParsing();
1297 return StmtError();
Douglas Gregor791215b2009-09-21 20:51:25 +00001298 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001299
Sean Hunt2edf0a22012-06-23 05:07:58 +00001300 ParsedAttributesWithRange attrs(AttrFactory);
1301 MaybeParseCXX0XAttributes(attrs);
1302
Reid Spencer5f016e22007-07-11 17:01:13 +00001303 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001304 if (Tok.is(tok::semi)) { // for (;
Sean Hunt2edf0a22012-06-23 05:07:58 +00001305 ProhibitAttributes(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00001306 // no first part, eat the ';'.
1307 ConsumeToken();
Eli Friedman9490ab42011-12-20 01:50:37 +00001308 } else if (isForInitDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +00001309 // Parse declaration, which eats the ';'.
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001310 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +00001311 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +00001312
John McCall0b7e6782011-03-24 11:26:52 +00001313 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00001314 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00001315
Richard Smithad762fc2011-04-14 22:09:26 +00001316 // In C++0x, "for (T NS:a" might not be a typo for ::
David Blaikie4e4d0842012-03-11 07:00:24 +00001317 bool MightBeForRangeStmt = getLangOpts().CPlusPlus;
Richard Smithad762fc2011-04-14 22:09:26 +00001318 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1319
Chris Lattner97144fc2009-04-02 04:16:50 +00001320 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001321 StmtVector Stmts;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001322 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
Richard Smithad762fc2011-04-14 22:09:26 +00001323 DeclEnd, attrs, false,
1324 MightBeForRangeStmt ?
1325 &ForRangeInit : 0);
Chris Lattnercd147752009-03-29 17:27:48 +00001326 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Richard Smithad762fc2011-04-14 22:09:26 +00001328 if (ForRangeInit.ParsedForRangeDecl()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001329 Diag(ForRangeInit.ColonLoc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00001330 diag::warn_cxx98_compat_for_range : diag::ext_for_range);
Richard Smith8f4fb192011-09-04 19:54:14 +00001331
Richard Smithad762fc2011-04-14 22:09:26 +00001332 ForRange = true;
1333 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattnercd147752009-03-29 17:27:48 +00001334 ConsumeToken();
1335 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniana7cf23a2009-11-19 22:12:37 +00001336 Actions.ActOnForEachDeclStmt(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001337 // ObjC: for (id x in expr)
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001338 ConsumeToken(); // consume 'in'
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001339
Douglas Gregorfb629412010-08-23 21:17:50 +00001340 if (Tok.is(tok::code_completion)) {
1341 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001342 cutOffParsing();
1343 return StmtError();
Douglas Gregorfb629412010-08-23 21:17:50 +00001344 }
Douglas Gregor586596f2010-05-06 17:25:47 +00001345 Collection = ParseExpression();
Chris Lattnercd147752009-03-29 17:27:48 +00001346 } else {
1347 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001348 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001349 } else {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001350 ProhibitAttributes(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00001351 Value = ParseExpression();
1352
John McCallf6a16482010-12-04 03:47:34 +00001353 ForEach = isTokIdentifier_in();
1354
Reid Spencer5f016e22007-07-11 17:01:13 +00001355 // Turn the expression into a stmt.
John McCallf6a16482010-12-04 03:47:34 +00001356 if (!Value.isInvalid()) {
1357 if (ForEach)
1358 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1359 else
1360 FirstPart = Actions.ActOnExprStmt(Actions.MakeFullExpr(Value.get()));
1361 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001362
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001363 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001364 ConsumeToken();
John McCallf6a16482010-12-04 03:47:34 +00001365 } else if (ForEach) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001366 ConsumeToken(); // consume 'in'
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001367
Douglas Gregorfb629412010-08-23 21:17:50 +00001368 if (Tok.is(tok::code_completion)) {
1369 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001370 cutOffParsing();
1371 return StmtError();
Douglas Gregorfb629412010-08-23 21:17:50 +00001372 }
Douglas Gregor586596f2010-05-06 17:25:47 +00001373 Collection = ParseExpression();
David Blaikie4e4d0842012-03-11 07:00:24 +00001374 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::colon) && FirstPart.get()) {
Richard Smitha44854a2011-12-20 22:56:20 +00001375 // User tried to write the reasonable, but ill-formed, for-range-statement
1376 // for (expr : expr) { ... }
1377 Diag(Tok, diag::err_for_range_expected_decl)
1378 << FirstPart.get()->getSourceRange();
1379 SkipUntil(tok::r_paren, false, true);
1380 SecondPartIsInvalid = true;
Chris Lattner682bf922009-03-29 16:50:03 +00001381 } else {
Douglas Gregorb72c7782011-02-17 03:38:46 +00001382 if (!Value.isInvalid()) {
1383 Diag(Tok, diag::err_expected_semi_for);
1384 } else {
1385 // Skip until semicolon or rparen, don't consume it.
1386 SkipUntil(tok::r_paren, true, true);
1387 if (Tok.is(tok::semi))
1388 ConsumeToken();
1389 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001390 }
1391 }
Richard Smithad762fc2011-04-14 22:09:26 +00001392 if (!ForEach && !ForRange) {
John McCall9ae2f072010-08-23 23:25:46 +00001393 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001394 // Parse the second part of the for specifier.
1395 if (Tok.is(tok::semi)) { // for (...;;
1396 // no second part.
Douglas Gregorb72c7782011-02-17 03:38:46 +00001397 } else if (Tok.is(tok::r_paren)) {
1398 // missing both semicolons.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001399 } else {
John McCall60d7b3a2010-08-24 06:29:42 +00001400 ExprResult Second;
David Blaikie4e4d0842012-03-11 07:00:24 +00001401 if (getLangOpts().CPlusPlus)
Douglas Gregor586596f2010-05-06 17:25:47 +00001402 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1403 else {
1404 Second = ParseExpression();
1405 if (!Second.isInvalid())
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001406 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001407 Second.get());
Douglas Gregor586596f2010-05-06 17:25:47 +00001408 }
Douglas Gregoreecf38f2010-05-06 21:39:56 +00001409 SecondPartIsInvalid = Second.isInvalid();
David Blaikiedef07622012-05-16 04:20:04 +00001410 SecondPart = Actions.MakeFullExpr(Second.get(), ForLoc);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001411 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001412
Douglas Gregorb72c7782011-02-17 03:38:46 +00001413 if (Tok.isNot(tok::semi)) {
1414 if (!SecondPartIsInvalid || SecondVar)
1415 Diag(Tok, diag::err_expected_semi_for);
1416 else
1417 // Skip until semicolon or rparen, don't consume it.
1418 SkipUntil(tok::r_paren, true, true);
1419 }
1420
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001421 if (Tok.is(tok::semi)) {
1422 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001423 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001424
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001425 // Parse the third part of the for specifier.
Douglas Gregor586596f2010-05-06 17:25:47 +00001426 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCall60d7b3a2010-08-24 06:29:42 +00001427 ExprResult Third = ParseExpression();
John McCall9ae2f072010-08-23 23:25:46 +00001428 ThirdPart = Actions.MakeFullExpr(Third.take());
Douglas Gregor586596f2010-05-06 17:25:47 +00001429 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001430 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001431 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001432 T.consumeClose();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001433
Richard Smithad762fc2011-04-14 22:09:26 +00001434 // We need to perform most of the semantic analysis for a C++0x for-range
1435 // statememt before parsing the body, in order to be able to deduce the type
1436 // of an auto-typed loop variable.
1437 StmtResult ForRangeStmt;
Fariborz Jahaniana1eec4b2012-07-03 22:00:52 +00001438 StmtResult ForEachStmt;
Chad Rosierb6604462012-07-10 21:35:27 +00001439
John McCall990567c2011-07-27 01:07:15 +00001440 if (ForRange) {
Sam Panzerbc20bbb2012-08-16 21:47:25 +00001441 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, FirstPart.take(),
Richard Smithad762fc2011-04-14 22:09:26 +00001442 ForRangeInit.ColonLoc,
1443 ForRangeInit.RangeExpr.get(),
Sam Panzere1715b62012-08-21 00:52:01 +00001444 T.getCloseLocation(), true);
Richard Smithad762fc2011-04-14 22:09:26 +00001445
John McCall990567c2011-07-27 01:07:15 +00001446
1447 // Similarly, we need to do the semantic analysis for a for-range
1448 // statement immediately in order to close over temporaries correctly.
1449 } else if (ForEach) {
Sam Panzerbc20bbb2012-08-16 21:47:25 +00001450 ForEachStmt = Actions.ActOnObjCForCollectionStmt(ForLoc,
Fariborz Jahaniana1eec4b2012-07-03 22:00:52 +00001451 FirstPart.take(),
Chad Rosierb6604462012-07-10 21:35:27 +00001452 Collection.take(),
Fariborz Jahaniana1eec4b2012-07-03 22:00:52 +00001453 T.getCloseLocation());
John McCall990567c2011-07-27 01:07:15 +00001454 }
1455
Chris Lattner0ecea032007-08-22 05:28:50 +00001456 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001457 // there is no compound stmt. C90 does not have this clause. We only do this
1458 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001459 //
1460 // C++ 6.5p2:
1461 // The substatement in an iteration-statement implicitly defines a local scope
1462 // which is entered and exited each time through the loop.
1463 //
1464 // See comments in ParseIfStatement for why we create a scope for
1465 // for-init-statement/condition and a new scope for substatement in C++.
1466 //
Mike Stump1eb44332009-09-09 15:08:12 +00001467 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001468 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001469
Reid Spencer5f016e22007-07-11 17:01:13 +00001470 // Read the body statement.
Nico Weber5cb94a72011-12-22 23:26:17 +00001471 StmtResult Body(ParseStatement(TrailingElseLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001472
Chris Lattner0ecea032007-08-22 05:28:50 +00001473 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001474 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001475
Reid Spencer5f016e22007-07-11 17:01:13 +00001476 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001477 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001478
1479 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001480 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001481
Richard Smithad762fc2011-04-14 22:09:26 +00001482 if (ForEach)
Fariborz Jahaniana1eec4b2012-07-03 22:00:52 +00001483 return Actions.FinishObjCForCollectionStmt(ForEachStmt.take(),
1484 Body.take());
Mike Stump1eb44332009-09-09 15:08:12 +00001485
Richard Smithad762fc2011-04-14 22:09:26 +00001486 if (ForRange)
1487 return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take());
1488
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001489 return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.take(),
1490 SecondPart, SecondVar, ThirdPart,
1491 T.getCloseLocation(), Body.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001492}
1493
1494/// ParseGotoStatement
1495/// jump-statement:
1496/// 'goto' identifier ';'
1497/// [GNU] 'goto' '*' expression ';'
1498///
1499/// Note: this lets the caller parse the end ';'.
1500///
Richard Smith534986f2012-04-14 00:33:13 +00001501StmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001502 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001503 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001504
John McCall60d7b3a2010-08-24 06:29:42 +00001505 StmtResult Res;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001506 if (Tok.is(tok::identifier)) {
Chris Lattner337e5502011-02-18 01:27:55 +00001507 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1508 Tok.getLocation());
1509 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Reid Spencer5f016e22007-07-11 17:01:13 +00001510 ConsumeToken();
Eli Friedmanf01fdff2009-04-28 00:51:18 +00001511 } else if (Tok.is(tok::star)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001512 // GNU indirect goto extension.
1513 Diag(Tok, diag::ext_gnu_indirect_goto);
1514 SourceLocation StarLoc = ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001515 ExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001516 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001517 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001518 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001519 }
John McCall9ae2f072010-08-23 23:25:46 +00001520 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattner95cfb852007-07-22 04:13:33 +00001521 } else {
1522 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +00001523 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001524 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001525
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001526 return Res;
Reid Spencer5f016e22007-07-11 17:01:13 +00001527}
1528
1529/// ParseContinueStatement
1530/// jump-statement:
1531/// 'continue' ';'
1532///
1533/// Note: this lets the caller parse the end ';'.
1534///
Richard Smith534986f2012-04-14 00:33:13 +00001535StmtResult Parser::ParseContinueStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001536 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001537 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001538}
1539
1540/// ParseBreakStatement
1541/// jump-statement:
1542/// 'break' ';'
1543///
1544/// Note: this lets the caller parse the end ';'.
1545///
Richard Smith534986f2012-04-14 00:33:13 +00001546StmtResult Parser::ParseBreakStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001547 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001548 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001549}
1550
1551/// ParseReturnStatement
1552/// jump-statement:
1553/// 'return' expression[opt] ';'
Richard Smith534986f2012-04-14 00:33:13 +00001554StmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001555 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001556 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001557
John McCall60d7b3a2010-08-24 06:29:42 +00001558 ExprResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001559 if (Tok.isNot(tok::semi)) {
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001560 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001561 Actions.CodeCompleteReturn(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001562 cutOffParsing();
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001563 return StmtError();
1564 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001565
David Blaikie4e4d0842012-03-11 07:00:24 +00001566 if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus) {
Douglas Gregor6f4596c2011-03-11 23:10:44 +00001567 R = ParseInitializer();
Richard Smith7fe62082011-10-15 05:09:34 +00001568 if (R.isUsable())
David Blaikie4e4d0842012-03-11 07:00:24 +00001569 Diag(R.get()->getLocStart(), getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00001570 diag::warn_cxx98_compat_generalized_initializer_lists :
1571 diag::ext_generalized_initializer_lists)
Douglas Gregor6f4596c2011-03-11 23:10:44 +00001572 << R.get()->getSourceRange();
1573 } else
1574 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001575 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001576 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001577 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001578 }
1579 }
John McCall9ae2f072010-08-23 23:25:46 +00001580 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001581}
1582
Eli Friedman3fedbe12011-09-30 01:13:51 +00001583/// ParseMicrosoftAsmStatement. When -fms-extensions/-fasm-blocks is enabled,
1584/// this routine is called to collect the tokens for an MS asm statement.
Chad Rosier8cd64b42012-06-11 20:47:18 +00001585///
1586/// [MS] ms-asm-statement:
1587/// ms-asm-block
1588/// ms-asm-block ms-asm-statement
1589///
1590/// [MS] ms-asm-block:
1591/// '__asm' ms-asm-line '\n'
1592/// '__asm' '{' ms-asm-instruction-block[opt] '}' ';'[opt]
1593///
1594/// [MS] ms-asm-instruction-block
1595/// ms-asm-line
1596/// ms-asm-line '\n' ms-asm-instruction-block
1597///
Eli Friedman3fedbe12011-09-30 01:13:51 +00001598StmtResult Parser::ParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
Chad Rosier56d7f232012-08-24 21:42:51 +00001599 // MS-style inline assembly is not fully supported, so emit a warning.
1600 Diag(AsmLoc, diag::warn_unsupported_msasm);
1601
Eli Friedman3fedbe12011-09-30 01:13:51 +00001602 SourceManager &SrcMgr = PP.getSourceManager();
1603 SourceLocation EndLoc = AsmLoc;
Chad Rosier8cd64b42012-06-11 20:47:18 +00001604 SmallVector<Token, 4> AsmToks;
Chad Rosier21ef7112012-08-14 19:22:06 +00001605
1606 bool InBraces = false;
1607 unsigned short savedBraceCount = 0;
1608 bool InAsmComment = false;
1609 FileID FID;
1610 unsigned LineNo = 0;
1611 unsigned NumTokensRead = 0;
1612 SourceLocation LBraceLoc;
1613
1614 if (Tok.is(tok::l_brace)) {
1615 // Braced inline asm: consume the opening brace.
1616 InBraces = true;
1617 savedBraceCount = BraceCount;
1618 EndLoc = LBraceLoc = ConsumeBrace();
1619 ++NumTokensRead;
1620 } else {
1621 // Single-line inline asm; compute which line it is on.
1622 std::pair<FileID, unsigned> ExpAsmLoc =
1623 SrcMgr.getDecomposedExpansionLoc(EndLoc);
1624 FID = ExpAsmLoc.first;
1625 LineNo = SrcMgr.getLineNumber(FID, ExpAsmLoc.second);
1626 }
1627
1628 SourceLocation TokLoc = Tok.getLocation();
Eli Friedman3fedbe12011-09-30 01:13:51 +00001629 do {
Chad Rosier21ef7112012-08-14 19:22:06 +00001630 // If we hit EOF, we're done, period.
1631 if (Tok.is(tok::eof))
Eli Friedman3fedbe12011-09-30 01:13:51 +00001632 break;
Chad Rosier21ef7112012-08-14 19:22:06 +00001633
Chad Rosier21ef7112012-08-14 19:22:06 +00001634 if (!InAsmComment && Tok.is(tok::semi)) {
1635 // A semicolon in an asm is the start of a comment.
1636 InAsmComment = true;
1637 if (InBraces) {
1638 // Compute which line the comment is on.
1639 std::pair<FileID, unsigned> ExpSemiLoc =
1640 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1641 FID = ExpSemiLoc.first;
1642 LineNo = SrcMgr.getLineNumber(FID, ExpSemiLoc.second);
1643 }
1644 } else if (!InBraces || InAsmComment) {
1645 // If end-of-line is significant, check whether this token is on a
1646 // new line.
1647 std::pair<FileID, unsigned> ExpLoc =
1648 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1649 if (ExpLoc.first != FID ||
1650 SrcMgr.getLineNumber(ExpLoc.first, ExpLoc.second) != LineNo) {
1651 // If this is a single-line __asm, we're done.
1652 if (!InBraces)
1653 break;
1654 // We're no longer in a comment.
1655 InAsmComment = false;
1656 } else if (!InAsmComment && Tok.is(tok::r_brace)) {
1657 // Single-line asm always ends when a closing brace is seen.
1658 // FIXME: This is compatible with Apple gcc's -fasm-blocks; what
1659 // does MSVC do here?
1660 break;
1661 }
1662 }
1663 if (!InAsmComment && InBraces && Tok.is(tok::r_brace) &&
1664 BraceCount == (savedBraceCount + 1)) {
1665 // Consume the closing brace, and finish
1666 EndLoc = ConsumeBrace();
1667 break;
1668 }
1669
1670 // Consume the next token; make sure we don't modify the brace count etc.
1671 // if we are in a comment.
1672 EndLoc = TokLoc;
1673 if (InAsmComment)
1674 PP.Lex(Tok);
1675 else {
1676 AsmToks.push_back(Tok);
1677 ConsumeAnyToken();
1678 }
1679 TokLoc = Tok.getLocation();
1680 ++NumTokensRead;
Eli Friedman3fedbe12011-09-30 01:13:51 +00001681 } while (1);
Chad Rosier8cd64b42012-06-11 20:47:18 +00001682
Chad Rosier21ef7112012-08-14 19:22:06 +00001683 if (InBraces && BraceCount != savedBraceCount) {
1684 // __asm without closing brace (this can happen at EOF).
1685 Diag(Tok, diag::err_expected_rbrace);
1686 Diag(LBraceLoc, diag::note_matching) << "{";
1687 return StmtError();
1688 } else if (NumTokensRead == 0) {
1689 // Empty __asm.
1690 Diag(Tok, diag::err_expected_lbrace);
1691 return StmtError();
1692 }
1693
Chad Rosier56d7f232012-08-24 21:42:51 +00001694 // If MS-style inline assembly is disabled, then build an empty asm.
1695 if (!getLangOpts().EmitMicrosoftInlineAsm) {
1696 Token t;
1697 t.setKind(tok::string_literal);
1698 t.setLiteralData("\"/*FIXME: not done*/\"");
1699 t.clearFlag(Token::NeedsCleaning);
1700 t.setLength(21);
1701 ExprResult AsmString(Actions.ActOnStringLiteral(&t, 1));
1702 ExprVector Constraints;
1703 ExprVector Exprs;
1704 ExprVector Clobbers;
Chad Rosierdf5faf52012-08-25 00:11:56 +00001705 return Actions.ActOnGCCAsmStmt(AsmLoc, true, true, 0, 0, 0, Constraints,
1706 Exprs, AsmString.take(), Clobbers, EndLoc);
Chad Rosier56d7f232012-08-24 21:42:51 +00001707 }
1708
Chad Rosier8f726de2012-08-06 20:03:45 +00001709 // FIXME: We should be passing source locations for better diagnostics.
Chad Rosier7bd092b2012-08-15 16:53:30 +00001710 return Actions.ActOnMSAsmStmt(AsmLoc, LBraceLoc,
1711 llvm::makeArrayRef(AsmToks), EndLoc);
Steve Naroffd62701b2008-02-07 03:50:06 +00001712}
1713
Reid Spencer5f016e22007-07-11 17:01:13 +00001714/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001715/// asm-statement:
1716/// gnu-asm-statement
1717/// ms-asm-statement
1718///
1719/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001720/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1721///
1722/// [GNU] asm-argument:
1723/// asm-string-literal
1724/// asm-string-literal ':' asm-operands[opt]
1725/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1726/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1727/// ':' asm-clobbers
1728///
1729/// [GNU] asm-clobbers:
1730/// asm-string-literal
1731/// asm-clobbers ',' asm-string-literal
1732///
John McCall60d7b3a2010-08-24 06:29:42 +00001733StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001734 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001735 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001736
Chad Rosierb6604462012-07-10 21:35:27 +00001737 if (getLangOpts().MicrosoftExt && Tok.isNot(tok::l_paren) &&
1738 !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001739 msAsm = true;
Eli Friedman3fedbe12011-09-30 01:13:51 +00001740 return ParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffd62701b2008-02-07 03:50:06 +00001741 }
John McCall0b7e6782011-03-24 11:26:52 +00001742 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00001743 SourceLocation Loc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00001744 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redl9a920342008-12-11 19:48:14 +00001745
Reid Spencer5f016e22007-07-11 17:01:13 +00001746 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1747 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001748 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001749 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001750 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001751
Reid Spencer5f016e22007-07-11 17:01:13 +00001752 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001753 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001754 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001755 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001756 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001757 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001758 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001759 BalancedDelimiterTracker T(*this, tok::l_paren);
1760 T.consumeOpen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001761
John McCall60d7b3a2010-08-24 06:29:42 +00001762 ExprResult AsmString(ParseAsmStringLiteral());
Ted Kremenek320fa4b2011-12-02 01:30:14 +00001763 if (AsmString.isInvalid()) {
Richard Smith99831e42012-03-06 03:21:47 +00001764 // Consume up to and including the closing paren.
1765 T.skipToEnd();
Sebastian Redl9a920342008-12-11 19:48:14 +00001766 return StmtError();
Ted Kremenek320fa4b2011-12-02 01:30:14 +00001767 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001768
Chris Lattner5f9e2722011-07-23 10:55:15 +00001769 SmallVector<IdentifierInfo *, 4> Names;
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001770 ExprVector Constraints;
1771 ExprVector Exprs;
1772 ExprVector Clobbers;
Reid Spencer5f016e22007-07-11 17:01:13 +00001773
Anders Carlssondfab34a2008-02-05 23:03:50 +00001774 if (Tok.is(tok::r_paren)) {
Chris Lattner64cb4752009-12-20 23:00:41 +00001775 // We have a simple asm expression like 'asm("foo")'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001776 T.consumeClose();
Chad Rosierdf5faf52012-08-25 00:11:56 +00001777 return Actions.ActOnGCCAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1778 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1779 Constraints, Exprs, AsmString.take(),
1780 Clobbers, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001781 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001782
Chris Lattner64cb4752009-12-20 23:00:41 +00001783 // Parse Outputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001784 bool AteExtraColon = false;
1785 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1786 // In C++ mode, parse "::" like ": :".
1787 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattner64cb4752009-12-20 23:00:41 +00001788 ConsumeToken();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001789
Chris Lattner64056462009-12-20 23:08:04 +00001790 if (!AteExtraColon &&
1791 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001792 return StmtError();
1793 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001794
Chris Lattner64cb4752009-12-20 23:00:41 +00001795 unsigned NumOutputs = Names.size();
1796
1797 // Parse Inputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001798 if (AteExtraColon ||
1799 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1800 // In C++ mode, parse "::" like ": :".
1801 if (AteExtraColon)
1802 AteExtraColon = false;
1803 else {
1804 AteExtraColon = Tok.is(tok::coloncolon);
1805 ConsumeToken();
1806 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001807
Chris Lattner64056462009-12-20 23:08:04 +00001808 if (!AteExtraColon &&
1809 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001810 return StmtError();
1811 }
1812
1813 assert(Names.size() == Constraints.size() &&
1814 Constraints.size() == Exprs.size() &&
1815 "Input operand size mismatch!");
1816
1817 unsigned NumInputs = Names.size() - NumOutputs;
1818
1819 // Parse the clobbers, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001820 if (AteExtraColon || Tok.is(tok::colon)) {
1821 if (!AteExtraColon)
1822 ConsumeToken();
Chris Lattner64cb4752009-12-20 23:00:41 +00001823
Chandler Carruth102e1b62010-07-22 07:11:21 +00001824 // Parse the asm-string list for clobbers if present.
1825 if (Tok.isNot(tok::r_paren)) {
1826 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +00001827 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattner64cb4752009-12-20 23:00:41 +00001828
Chandler Carruth102e1b62010-07-22 07:11:21 +00001829 if (Clobber.isInvalid())
1830 break;
Chris Lattner64cb4752009-12-20 23:00:41 +00001831
Chandler Carruth102e1b62010-07-22 07:11:21 +00001832 Clobbers.push_back(Clobber.release());
Chris Lattner64cb4752009-12-20 23:00:41 +00001833
Chandler Carruth102e1b62010-07-22 07:11:21 +00001834 if (Tok.isNot(tok::comma)) break;
1835 ConsumeToken();
1836 }
Chris Lattner64cb4752009-12-20 23:00:41 +00001837 }
1838 }
1839
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001840 T.consumeClose();
Chad Rosierdf5faf52012-08-25 00:11:56 +00001841 return Actions.ActOnGCCAsmStmt(AsmLoc, false, isVolatile, NumOutputs,
1842 NumInputs, Names.data(), Constraints, Exprs,
1843 AsmString.take(), Clobbers,
1844 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001845}
1846
1847/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattner64cb4752009-12-20 23:00:41 +00001848/// asm-statement, assuming the leading ':' token was eaten.
Reid Spencer5f016e22007-07-11 17:01:13 +00001849///
1850/// [GNU] asm-operands:
1851/// asm-operand
1852/// asm-operands ',' asm-operand
1853///
1854/// [GNU] asm-operand:
1855/// asm-string-literal '(' expression ')'
1856/// '[' identifier ']' asm-string-literal '(' expression ')'
1857///
Daniel Dunbar5ffe14c2009-10-18 20:26:27 +00001858//
1859// FIXME: Avoid unnecessary std::string trashing.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001860bool Parser::ParseAsmOperandsOpt(SmallVectorImpl<IdentifierInfo *> &Names,
Richard Trieuf81e5a92011-09-09 02:00:50 +00001861 SmallVectorImpl<Expr *> &Constraints,
1862 SmallVectorImpl<Expr *> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001863 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001864 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001865 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001866
1867 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001868 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001869 if (Tok.is(tok::l_square)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001870 BalancedDelimiterTracker T(*this, tok::l_square);
1871 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00001872
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001873 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001874 Diag(Tok, diag::err_expected_ident);
1875 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001876 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001877 }
Mike Stump1eb44332009-09-09 15:08:12 +00001878
Anders Carlssonb235fc22007-11-22 01:36:19 +00001879 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001880 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001881
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001882 Names.push_back(II);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001883 T.consumeClose();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001884 } else
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001885 Names.push_back(0);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001886
John McCall60d7b3a2010-08-24 06:29:42 +00001887 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001888 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001889 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001890 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001891 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001892 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001893
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001894 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001895 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001896 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001897 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001898 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001899
Reid Spencer5f016e22007-07-11 17:01:13 +00001900 // Read the parenthesized expression.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001901 BalancedDelimiterTracker T(*this, tok::l_paren);
1902 T.consumeOpen();
John McCall60d7b3a2010-08-24 06:29:42 +00001903 ExprResult Res(ParseExpression());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001904 T.consumeClose();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001905 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001906 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001907 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001908 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001909 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001910 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001911 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001912 ConsumeToken();
1913 }
1914}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001915
Douglas Gregorc9977d02011-03-16 17:05:57 +00001916Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner40e9bc82009-03-05 00:49:17 +00001917 assert(Tok.is(tok::l_brace));
1918 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001919
Erik Verbruggen6a91d382012-04-12 10:11:59 +00001920 if (SkipFunctionBodies && trySkippingFunctionBody()) {
1921 BodyScope.Exit();
1922 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregorc9977d02011-03-16 17:05:57 +00001923 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001924
John McCallf312b1e2010-08-26 23:41:50 +00001925 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1926 "parsing function body");
Mike Stump1eb44332009-09-09 15:08:12 +00001927
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001928 // Do not enter a scope for the brace, as the arguments are in the same scope
1929 // (the function body) as the body itself. Instead, just read the statement
1930 // list and put it into a CompoundStmt for safe keeping.
John McCall60d7b3a2010-08-24 06:29:42 +00001931 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl61364dd2008-12-11 19:30:53 +00001932
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001933 // If the function body could not be parsed, make a bogus compoundstmt.
Dmitri Gribenko625bb562012-02-14 22:14:32 +00001934 if (FnBody.isInvalid()) {
1935 Sema::CompoundScopeRAII CompoundScope(Actions);
Mike Stump1eb44332009-09-09 15:08:12 +00001936 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Benjamin Kramer5354e772012-08-23 23:38:35 +00001937 MultiStmtArg(), false);
Dmitri Gribenko625bb562012-02-14 22:14:32 +00001938 }
Sebastian Redl61364dd2008-12-11 19:30:53 +00001939
Douglas Gregorc9977d02011-03-16 17:05:57 +00001940 BodyScope.Exit();
John McCall9ae2f072010-08-23 23:25:46 +00001941 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00001942}
Sebastian Redla0fd8652008-12-21 16:41:36 +00001943
Sebastian Redld3a413d2009-04-26 20:35:05 +00001944/// ParseFunctionTryBlock - Parse a C++ function-try-block.
1945///
1946/// function-try-block:
1947/// 'try' ctor-initializer[opt] compound-statement handler-seq
1948///
Douglas Gregorc9977d02011-03-16 17:05:57 +00001949Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redld3a413d2009-04-26 20:35:05 +00001950 assert(Tok.is(tok::kw_try) && "Expected 'try'");
1951 SourceLocation TryLoc = ConsumeToken();
1952
John McCallf312b1e2010-08-26 23:41:50 +00001953 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1954 "parsing function try block");
Sebastian Redld3a413d2009-04-26 20:35:05 +00001955
1956 // Constructor initializer list?
1957 if (Tok.is(tok::colon))
1958 ParseConstructorInitializer(Decl);
Douglas Gregor2eef4272011-09-07 20:36:12 +00001959 else
1960 Actions.ActOnDefaultCtorInitializers(Decl);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001961
Erik Verbruggen6a91d382012-04-12 10:11:59 +00001962 if (SkipFunctionBodies && trySkippingFunctionBody()) {
1963 BodyScope.Exit();
1964 return Actions.ActOnFinishFunctionBody(Decl, 0);
Douglas Gregorc9977d02011-03-16 17:05:57 +00001965 }
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001966
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001967 SourceLocation LBraceLoc = Tok.getLocation();
John McCall60d7b3a2010-08-24 06:29:42 +00001968 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc));
Sebastian Redld3a413d2009-04-26 20:35:05 +00001969 // If we failed to parse the try-catch, we just give the function an empty
1970 // compound statement as the body.
Dmitri Gribenko625bb562012-02-14 22:14:32 +00001971 if (FnBody.isInvalid()) {
1972 Sema::CompoundScopeRAII CompoundScope(Actions);
Sebastian Redlde1b60a2009-04-26 21:08:36 +00001973 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Benjamin Kramer5354e772012-08-23 23:38:35 +00001974 MultiStmtArg(), false);
Dmitri Gribenko625bb562012-02-14 22:14:32 +00001975 }
Sebastian Redld3a413d2009-04-26 20:35:05 +00001976
Douglas Gregorc9977d02011-03-16 17:05:57 +00001977 BodyScope.Exit();
John McCall9ae2f072010-08-23 23:25:46 +00001978 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redld3a413d2009-04-26 20:35:05 +00001979}
1980
Erik Verbruggen6a91d382012-04-12 10:11:59 +00001981bool Parser::trySkippingFunctionBody() {
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001982 assert(Tok.is(tok::l_brace));
Erik Verbruggen6a91d382012-04-12 10:11:59 +00001983 assert(SkipFunctionBodies &&
1984 "Should only be called when SkipFunctionBodies is enabled");
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001985
1986 // We're in code-completion mode. Skip parsing for all function bodies unless
1987 // the body contains the code-completion point.
1988 TentativeParsingAction PA(*this);
1989 ConsumeBrace();
1990 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
Erik Verbruggen6a91d382012-04-12 10:11:59 +00001991 /*StopAtCodeCompletion=*/PP.isCodeCompletionEnabled())) {
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00001992 PA.Commit();
1993 return true;
1994 }
1995
1996 PA.Revert();
1997 return false;
1998}
1999
Sebastian Redla0fd8652008-12-21 16:41:36 +00002000/// ParseCXXTryBlock - Parse a C++ try-block.
2001///
2002/// try-block:
2003/// 'try' compound-statement handler-seq
2004///
Richard Smith534986f2012-04-14 00:33:13 +00002005StmtResult Parser::ParseCXXTryBlock() {
Sebastian Redla0fd8652008-12-21 16:41:36 +00002006 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2007
2008 SourceLocation TryLoc = ConsumeToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +00002009 return ParseCXXTryBlockCommon(TryLoc);
2010}
2011
2012/// ParseCXXTryBlockCommon - Parse the common part of try-block and
2013/// function-try-block.
2014///
2015/// try-block:
2016/// 'try' compound-statement handler-seq
2017///
2018/// function-try-block:
2019/// 'try' ctor-initializer[opt] compound-statement handler-seq
2020///
2021/// handler-seq:
2022/// handler handler-seq[opt]
2023///
John Wiegley28bbe4b2011-04-28 01:08:34 +00002024/// [Borland] try-block:
2025/// 'try' compound-statement seh-except-block
2026/// 'try' compound-statment seh-finally-block
2027///
John McCall60d7b3a2010-08-24 06:29:42 +00002028StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc) {
Sebastian Redla0fd8652008-12-21 16:41:36 +00002029 if (Tok.isNot(tok::l_brace))
2030 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Sean Huntbbd37c62009-11-21 08:43:09 +00002031 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
Richard Smith534986f2012-04-14 00:33:13 +00002032
2033 StmtResult TryBlock(ParseCompoundStatement(/*isStmtExpr=*/false,
Douglas Gregorbca01b42011-07-06 22:04:06 +00002034 Scope::DeclScope|Scope::TryScope));
Sebastian Redla0fd8652008-12-21 16:41:36 +00002035 if (TryBlock.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002036 return TryBlock;
Sebastian Redla0fd8652008-12-21 16:41:36 +00002037
John Wiegley28bbe4b2011-04-28 01:08:34 +00002038 // Borland allows SEH-handlers with 'try'
Chad Rosierb6604462012-07-10 21:35:27 +00002039
Richard Smith534986f2012-04-14 00:33:13 +00002040 if ((Tok.is(tok::identifier) &&
2041 Tok.getIdentifierInfo() == getSEHExceptKeyword()) ||
2042 Tok.is(tok::kw___finally)) {
John Wiegley28bbe4b2011-04-28 01:08:34 +00002043 // TODO: Factor into common return ParseSEHHandlerCommon(...)
2044 StmtResult Handler;
Douglas Gregorb57791e2011-10-21 03:57:52 +00002045 if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley28bbe4b2011-04-28 01:08:34 +00002046 SourceLocation Loc = ConsumeToken();
2047 Handler = ParseSEHExceptBlock(Loc);
2048 }
2049 else {
2050 SourceLocation Loc = ConsumeToken();
2051 Handler = ParseSEHFinallyBlock(Loc);
2052 }
2053 if(Handler.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002054 return Handler;
John McCall7f040a92010-12-24 02:08:15 +00002055
John Wiegley28bbe4b2011-04-28 01:08:34 +00002056 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
2057 TryLoc,
2058 TryBlock.take(),
2059 Handler.take());
Sebastian Redla0fd8652008-12-21 16:41:36 +00002060 }
John Wiegley28bbe4b2011-04-28 01:08:34 +00002061 else {
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002062 StmtVector Handlers;
Richard Smith534986f2012-04-14 00:33:13 +00002063 ParsedAttributesWithRange attrs(AttrFactory);
John Wiegley28bbe4b2011-04-28 01:08:34 +00002064 MaybeParseCXX0XAttributes(attrs);
2065 ProhibitAttributes(attrs);
Sebastian Redla0fd8652008-12-21 16:41:36 +00002066
John Wiegley28bbe4b2011-04-28 01:08:34 +00002067 if (Tok.isNot(tok::kw_catch))
2068 return StmtError(Diag(Tok, diag::err_expected_catch));
2069 while (Tok.is(tok::kw_catch)) {
2070 StmtResult Handler(ParseCXXCatchBlock());
2071 if (!Handler.isInvalid())
2072 Handlers.push_back(Handler.release());
2073 }
2074 // Don't bother creating the full statement if we don't have any usable
2075 // handlers.
2076 if (Handlers.empty())
2077 return StmtError();
2078
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002079 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(),Handlers);
John Wiegley28bbe4b2011-04-28 01:08:34 +00002080 }
Sebastian Redla0fd8652008-12-21 16:41:36 +00002081}
2082
2083/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
2084///
2085/// handler:
2086/// 'catch' '(' exception-declaration ')' compound-statement
2087///
2088/// exception-declaration:
2089/// type-specifier-seq declarator
2090/// type-specifier-seq abstract-declarator
2091/// type-specifier-seq
2092/// '...'
2093///
John McCall60d7b3a2010-08-24 06:29:42 +00002094StmtResult Parser::ParseCXXCatchBlock() {
Sebastian Redla0fd8652008-12-21 16:41:36 +00002095 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
2096
2097 SourceLocation CatchLoc = ConsumeToken();
2098
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002099 BalancedDelimiterTracker T(*this, tok::l_paren);
2100 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redla0fd8652008-12-21 16:41:36 +00002101 return StmtError();
2102
2103 // C++ 3.3.2p3:
2104 // The name in a catch exception-declaration is local to the handler and
2105 // shall not be redeclared in the outermost block of the handler.
2106 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope);
2107
2108 // exception-declaration is equivalent to '...' or a parameter-declaration
2109 // without default arguments.
John McCalld226f652010-08-21 09:40:31 +00002110 Decl *ExceptionDecl = 0;
Sebastian Redla0fd8652008-12-21 16:41:36 +00002111 if (Tok.isNot(tok::ellipsis)) {
John McCall0b7e6782011-03-24 11:26:52 +00002112 DeclSpec DS(AttrFactory);
Sebastian Redl4b07b292008-12-22 19:15:10 +00002113 if (ParseCXXTypeSpecifierSeq(DS))
2114 return StmtError();
Sebastian Redla0fd8652008-12-21 16:41:36 +00002115 Declarator ExDecl(DS, Declarator::CXXCatchContext);
2116 ParseDeclarator(ExDecl);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002117 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redla0fd8652008-12-21 16:41:36 +00002118 } else
2119 ConsumeToken();
2120
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002121 T.consumeClose();
2122 if (T.getCloseLocation().isInvalid())
Sebastian Redla0fd8652008-12-21 16:41:36 +00002123 return StmtError();
2124
2125 if (Tok.isNot(tok::l_brace))
2126 return StmtError(Diag(Tok, diag::err_expected_lbrace));
2127
Sean Huntbbd37c62009-11-21 08:43:09 +00002128 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
Richard Smith534986f2012-04-14 00:33:13 +00002129 StmtResult Block(ParseCompoundStatement());
Sebastian Redla0fd8652008-12-21 16:41:36 +00002130 if (Block.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002131 return Block;
Sebastian Redla0fd8652008-12-21 16:41:36 +00002132
John McCall9ae2f072010-08-23 23:25:46 +00002133 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redla0fd8652008-12-21 16:41:36 +00002134}
Francois Pichet1e862692011-05-06 20:48:22 +00002135
2136void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Douglas Gregor3896fc52011-10-24 22:31:10 +00002137 IfExistsCondition Result;
Francois Pichetf9860382011-05-07 17:30:27 +00002138 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet1e862692011-05-06 20:48:22 +00002139 return;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002140
Douglas Gregor3896fc52011-10-24 22:31:10 +00002141 // Handle dependent statements by parsing the braces as a compound statement.
2142 // This is not the same behavior as Visual C++, which don't treat this as a
2143 // compound statement, but for Clang's type checking we can't have anything
2144 // inside these braces escaping to the surrounding code.
2145 if (Result.Behavior == IEB_Dependent) {
2146 if (!Tok.is(tok::l_brace)) {
2147 Diag(Tok, diag::err_expected_lbrace);
Richard Smith534986f2012-04-14 00:33:13 +00002148 return;
Douglas Gregor3896fc52011-10-24 22:31:10 +00002149 }
Richard Smith534986f2012-04-14 00:33:13 +00002150
2151 StmtResult Compound = ParseCompoundStatement();
Douglas Gregorba0513d2011-10-25 01:33:02 +00002152 if (Compound.isInvalid())
2153 return;
Richard Smith534986f2012-04-14 00:33:13 +00002154
Douglas Gregorba0513d2011-10-25 01:33:02 +00002155 StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc,
2156 Result.IsIfExists,
Richard Smith534986f2012-04-14 00:33:13 +00002157 Result.SS,
Douglas Gregorba0513d2011-10-25 01:33:02 +00002158 Result.Name,
2159 Compound.get());
2160 if (DepResult.isUsable())
2161 Stmts.push_back(DepResult.get());
Douglas Gregor3896fc52011-10-24 22:31:10 +00002162 return;
2163 }
Richard Smith534986f2012-04-14 00:33:13 +00002164
Douglas Gregor3896fc52011-10-24 22:31:10 +00002165 BalancedDelimiterTracker Braces(*this, tok::l_brace);
2166 if (Braces.consumeOpen()) {
Francois Pichet1e862692011-05-06 20:48:22 +00002167 Diag(Tok, diag::err_expected_lbrace);
2168 return;
2169 }
Francois Pichet1e862692011-05-06 20:48:22 +00002170
Douglas Gregor3896fc52011-10-24 22:31:10 +00002171 switch (Result.Behavior) {
2172 case IEB_Parse:
2173 // Parse the statements below.
2174 break;
Chad Rosierb6604462012-07-10 21:35:27 +00002175
Douglas Gregor3896fc52011-10-24 22:31:10 +00002176 case IEB_Dependent:
2177 llvm_unreachable("Dependent case handled above");
Chad Rosierb6604462012-07-10 21:35:27 +00002178
Douglas Gregor3896fc52011-10-24 22:31:10 +00002179 case IEB_Skip:
2180 Braces.skipToEnd();
Francois Pichet1e862692011-05-06 20:48:22 +00002181 return;
2182 }
2183
2184 // Condition is true, parse the statements.
2185 while (Tok.isNot(tok::r_brace)) {
2186 StmtResult R = ParseStatementOrDeclaration(Stmts, false);
2187 if (R.isUsable())
2188 Stmts.push_back(R.release());
2189 }
Douglas Gregor3896fc52011-10-24 22:31:10 +00002190 Braces.consumeClose();
Francois Pichet1e862692011-05-06 20:48:22 +00002191}