blob: 71a7b2c9f9ba47250fe6000fa825ef274f7a6271 [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"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/Basic/Diagnostic.h"
18#include "clang/Basic/PrettyStackTrace.h"
19#include "clang/Basic/SourceManager.h"
John McCall19510852010-08-20 18:27:03 +000020#include "clang/Sema/DeclSpec.h"
John McCallf312b1e2010-08-26 23:41:50 +000021#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall19510852010-08-20 18:27:03 +000022#include "clang/Sema/Scope.h"
Richard Smith05766812012-08-18 00:55:03 +000023#include "clang/Sema/TypoCorrection.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);
Richard Smith4e24f0f2013-01-02 12:01:23 +000087 MaybeParseCXX11Attributes(Attrs, 0, /*MightBeObjCMessageSend*/ true);
Richard Smith534986f2012-04-14 00:33:13 +000088
89 StmtResult Res = ParseStatementOrDeclarationAfterAttributes(Stmts,
90 OnlyStatement, TrailingElseLoc, Attrs);
91
92 assert((Attrs.empty() || Res.isInvalid() || Res.isUsable()) &&
93 "attributes on empty statement");
94
95 if (Attrs.empty() || Res.isInvalid())
96 return Res;
97
98 return Actions.ProcessStmtAttributes(Res.get(), Attrs.getList(), Attrs.Range);
99}
100
101StmtResult
102Parser::ParseStatementOrDeclarationAfterAttributes(StmtVector &Stmts,
103 bool OnlyStatement, SourceLocation *TrailingElseLoc,
104 ParsedAttributesWithRange &Attrs) {
105 const char *SemiError = 0;
106 StmtResult Res;
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();
Eli Friedman9595c7e2012-10-04 02:36:51 +0000256
Eli Friedman8b2bfdd2012-10-09 22:46:54 +0000257 case tok::annot_pragma_msstruct:
258 ProhibitAttributes(Attrs);
259 HandlePragmaMSStruct();
260 return StmtEmpty();
261
Eli Friedman3ef38ee2012-10-08 23:52:38 +0000262 case tok::annot_pragma_align:
263 ProhibitAttributes(Attrs);
264 HandlePragmaAlign();
265 return StmtEmpty();
266
Eli Friedman8b2bfdd2012-10-09 22:46:54 +0000267 case tok::annot_pragma_weak:
268 ProhibitAttributes(Attrs);
269 HandlePragmaWeak();
270 return StmtEmpty();
271
272 case tok::annot_pragma_weakalias:
273 ProhibitAttributes(Attrs);
274 HandlePragmaWeakAlias();
275 return StmtEmpty();
276
277 case tok::annot_pragma_redefine_extname:
278 ProhibitAttributes(Attrs);
279 HandlePragmaRedefineExtname();
280 return StmtEmpty();
281
Eli Friedman9595c7e2012-10-04 02:36:51 +0000282 case tok::annot_pragma_fp_contract:
Lang Hames860022c2012-10-21 01:10:01 +0000283 Diag(Tok, diag::err_pragma_fp_contract_scope);
284 ConsumeToken();
285 return StmtError();
286
Eli Friedman9595c7e2012-10-04 02:36:51 +0000287 case tok::annot_pragma_opencl_extension:
288 ProhibitAttributes(Attrs);
289 HandlePragmaOpenCLExtension();
290 return StmtEmpty();
Sebastian Redla0fd8652008-12-21 16:41:36 +0000291 }
292
Reid Spencer5f016e22007-07-11 17:01:13 +0000293 // If we reached this code, the statement must end in a semicolon.
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000294 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000296 } else if (!Res.isInvalid()) {
Chris Lattner7b3684a2009-06-14 00:23:56 +0000297 // If the result was valid, then we do want to diagnose this. Use
298 // ExpectAndConsume to emit the diagnostic, even though we know it won't
299 // succeed.
300 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
Chris Lattner19504402008-11-13 18:52:53 +0000301 // Skip until we see a } or ;, but don't eat it.
302 SkipUntil(tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000303 }
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000305 return Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000306}
307
Douglas Gregor312eadb2011-04-24 05:37:28 +0000308/// \brief Parse an expression statement.
Richard Smith534986f2012-04-14 00:33:13 +0000309StmtResult Parser::ParseExprStatement() {
Douglas Gregor312eadb2011-04-24 05:37:28 +0000310 // If a case keyword is missing, this is where it should be inserted.
311 Token OldToken = Tok;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000312
Douglas Gregor312eadb2011-04-24 05:37:28 +0000313 // expression[opt] ';'
Douglas Gregor5ecdd782011-04-27 06:18:01 +0000314 ExprResult Expr(ParseExpression());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000315 if (Expr.isInvalid()) {
316 // If the expression is invalid, skip ahead to the next semicolon or '}'.
317 // Not doing this opens us up to the possibility of infinite loops if
318 // ParseExpression does not consume any tokens.
319 SkipUntil(tok::r_brace, /*StopAtSemi=*/true, /*DontConsume=*/true);
320 if (Tok.is(tok::semi))
321 ConsumeToken();
322 return StmtError();
323 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000324
Douglas Gregor312eadb2011-04-24 05:37:28 +0000325 if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
326 Actions.CheckCaseExpression(Expr.get())) {
327 // If a constant expression is followed by a colon inside a switch block,
328 // suggest a missing case keyword.
329 Diag(OldToken, diag::err_expected_case_before_expression)
330 << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000331
Douglas Gregor312eadb2011-04-24 05:37:28 +0000332 // Recover parsing as a case statement.
Richard Smith534986f2012-04-14 00:33:13 +0000333 return ParseCaseStatement(/*MissingCase=*/true, Expr);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000334 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000335
Douglas Gregor312eadb2011-04-24 05:37:28 +0000336 // Otherwise, eat the semicolon.
337 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith41956372013-01-14 22:39:08 +0000338 return Actions.ActOnExprStmt(Expr);
John Wiegley28bbe4b2011-04-28 01:08:34 +0000339}
Douglas Gregor312eadb2011-04-24 05:37:28 +0000340
Richard Smith534986f2012-04-14 00:33:13 +0000341StmtResult Parser::ParseSEHTryBlock() {
John Wiegley28bbe4b2011-04-28 01:08:34 +0000342 assert(Tok.is(tok::kw___try) && "Expected '__try'");
343 SourceLocation Loc = ConsumeToken();
344 return ParseSEHTryBlockCommon(Loc);
345}
346
347/// ParseSEHTryBlockCommon
348///
349/// seh-try-block:
350/// '__try' compound-statement seh-handler
351///
352/// seh-handler:
353/// seh-except-block
354/// seh-finally-block
355///
356StmtResult Parser::ParseSEHTryBlockCommon(SourceLocation TryLoc) {
357 if(Tok.isNot(tok::l_brace))
358 return StmtError(Diag(Tok,diag::err_expected_lbrace));
359
Joao Matos568ba872012-09-04 17:49:35 +0000360 StmtResult TryBlock(ParseCompoundStatement());
John Wiegley28bbe4b2011-04-28 01:08:34 +0000361 if(TryBlock.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000362 return TryBlock;
John Wiegley28bbe4b2011-04-28 01:08:34 +0000363
364 StmtResult Handler;
Richard Smith534986f2012-04-14 00:33:13 +0000365 if (Tok.is(tok::identifier) &&
Douglas Gregorb57791e2011-10-21 03:57:52 +0000366 Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley28bbe4b2011-04-28 01:08:34 +0000367 SourceLocation Loc = ConsumeToken();
368 Handler = ParseSEHExceptBlock(Loc);
369 } else if (Tok.is(tok::kw___finally)) {
370 SourceLocation Loc = ConsumeToken();
371 Handler = ParseSEHFinallyBlock(Loc);
372 } else {
373 return StmtError(Diag(Tok,diag::err_seh_expected_handler));
374 }
375
376 if(Handler.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000377 return Handler;
John Wiegley28bbe4b2011-04-28 01:08:34 +0000378
379 return Actions.ActOnSEHTryBlock(false /* IsCXXTry */,
380 TryLoc,
381 TryBlock.take(),
382 Handler.take());
383}
384
385/// ParseSEHExceptBlock - Handle __except
386///
387/// seh-except-block:
388/// '__except' '(' seh-filter-expression ')' compound-statement
389///
390StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) {
391 PoisonIdentifierRAIIObject raii(Ident__exception_code, false),
392 raii2(Ident___exception_code, false),
393 raii3(Ident_GetExceptionCode, false);
394
395 if(ExpectAndConsume(tok::l_paren,diag::err_expected_lparen))
396 return StmtError();
397
398 ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope);
399
David Blaikie4e4d0842012-03-11 07:00:24 +0000400 if (getLangOpts().Borland) {
Francois Pichetd7f02df2011-04-28 03:14:31 +0000401 Ident__exception_info->setIsPoisoned(false);
402 Ident___exception_info->setIsPoisoned(false);
403 Ident_GetExceptionInfo->setIsPoisoned(false);
404 }
John Wiegley28bbe4b2011-04-28 01:08:34 +0000405 ExprResult FilterExpr(ParseExpression());
Francois Pichetd7f02df2011-04-28 03:14:31 +0000406
David Blaikie4e4d0842012-03-11 07:00:24 +0000407 if (getLangOpts().Borland) {
Francois Pichetd7f02df2011-04-28 03:14:31 +0000408 Ident__exception_info->setIsPoisoned(true);
409 Ident___exception_info->setIsPoisoned(true);
410 Ident_GetExceptionInfo->setIsPoisoned(true);
411 }
John Wiegley28bbe4b2011-04-28 01:08:34 +0000412
413 if(FilterExpr.isInvalid())
414 return StmtError();
415
416 if(ExpectAndConsume(tok::r_paren,diag::err_expected_rparen))
417 return StmtError();
418
Richard Smith534986f2012-04-14 00:33:13 +0000419 StmtResult Block(ParseCompoundStatement());
John Wiegley28bbe4b2011-04-28 01:08:34 +0000420
421 if(Block.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000422 return Block;
John Wiegley28bbe4b2011-04-28 01:08:34 +0000423
424 return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.take(), Block.take());
425}
426
427/// ParseSEHFinallyBlock - Handle __finally
428///
429/// seh-finally-block:
430/// '__finally' compound-statement
431///
432StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyBlock) {
433 PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
434 raii2(Ident___abnormal_termination, false),
435 raii3(Ident_AbnormalTermination, false);
436
Richard Smith534986f2012-04-14 00:33:13 +0000437 StmtResult Block(ParseCompoundStatement());
John Wiegley28bbe4b2011-04-28 01:08:34 +0000438 if(Block.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000439 return Block;
John Wiegley28bbe4b2011-04-28 01:08:34 +0000440
441 return Actions.ActOnSEHFinallyBlock(FinallyBlock,Block.take());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000442}
443
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000444/// ParseLabeledStatement - We have an identifier and a ':' after it.
Reid Spencer5f016e22007-07-11 17:01:13 +0000445///
446/// labeled-statement:
447/// identifier ':' statement
448/// [GNU] identifier ':' attributes[opt] statement
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000449///
Richard Smith534986f2012-04-14 00:33:13 +0000450StmtResult Parser::ParseLabeledStatement(ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000451 assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
452 "Not an identifier!");
453
454 Token IdentTok = Tok; // Save the whole token.
455 ConsumeToken(); // eat the identifier.
456
457 assert(Tok.is(tok::colon) && "Not a label!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000458
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000459 // identifier ':' statement
460 SourceLocation ColonLoc = ConsumeToken();
461
Richard Smith534986f2012-04-14 00:33:13 +0000462 // Read label attributes, if present. attrs will contain both C++11 and GNU
463 // attributes (if present) after this point.
John McCall7f040a92010-12-24 02:08:15 +0000464 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000465
John McCall60d7b3a2010-08-24 06:29:42 +0000466 StmtResult SubStmt(ParseStatement());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000467
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000468 // Broken substmt shouldn't prevent the label from being added to the AST.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000469 if (SubStmt.isInvalid())
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000470 SubStmt = Actions.ActOnNullStmt(ColonLoc);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000471
Chris Lattner337e5502011-02-18 01:27:55 +0000472 LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
473 IdentTok.getLocation());
Richard Smith534986f2012-04-14 00:33:13 +0000474 if (AttributeList *Attrs = attrs.getList()) {
Chris Lattner337e5502011-02-18 01:27:55 +0000475 Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
Richard Smith534986f2012-04-14 00:33:13 +0000476 attrs.clear();
477 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000478
Chris Lattner337e5502011-02-18 01:27:55 +0000479 return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
480 SubStmt.get());
Argyrios Kyrtzidisf7da7262008-07-09 22:53:07 +0000481}
Reid Spencer5f016e22007-07-11 17:01:13 +0000482
483/// ParseCaseStatement
484/// labeled-statement:
485/// 'case' constant-expression ':' statement
486/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
487///
Richard Smith534986f2012-04-14 00:33:13 +0000488StmtResult Parser::ParseCaseStatement(bool MissingCase, ExprResult Expr) {
Richard Smith46f11102011-04-21 22:48:40 +0000489 assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Chris Lattner24e1e702009-03-04 04:23:07 +0000491 // It is very very common for code to contain many case statements recursively
492 // nested, as in (but usually without indentation):
493 // case 1:
494 // case 2:
495 // case 3:
496 // case 4:
497 // case 5: etc.
498 //
499 // Parsing this naively works, but is both inefficient and can cause us to run
500 // out of stack space in our recursive descent parser. As a special case,
Chris Lattner26140c62009-03-04 18:24:58 +0000501 // flatten this recursion into an iterative loop. This is complex and gross,
Chris Lattner24e1e702009-03-04 04:23:07 +0000502 // but all the grossness is constrained to ParseCaseStatement (and some
503 // wierdness in the actions), so this is just local grossness :).
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Chris Lattner24e1e702009-03-04 04:23:07 +0000505 // TopLevelCase - This is the highest level we have parsed. 'case 1' in the
506 // example above.
John McCall60d7b3a2010-08-24 06:29:42 +0000507 StmtResult TopLevelCase(true);
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Chris Lattner24e1e702009-03-04 04:23:07 +0000509 // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
510 // gets updated each time a new case is parsed, and whose body is unset so
511 // far. When parsing 'case 4', this is the 'case 3' node.
Richard Trieub2fc6902011-09-09 02:16:15 +0000512 Stmt *DeepestParsedCaseStmt = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Chris Lattner24e1e702009-03-04 04:23:07 +0000514 // While we have case statements, eat and stack them.
David Majnemer0e1e69c2011-06-13 05:50:12 +0000515 SourceLocation ColonLoc;
Chris Lattner24e1e702009-03-04 04:23:07 +0000516 do {
Richard Trieubb9b80c2011-04-21 21:44:26 +0000517 SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
518 ConsumeToken(); // eat the 'case'.
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000520 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000521 Actions.CodeCompleteCase(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +0000522 cutOffParsing();
523 return StmtError();
Douglas Gregor3e1005f2009-09-21 18:10:23 +0000524 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000525
Chris Lattner6fb09c82009-12-10 00:38:54 +0000526 /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
527 /// Disable this form of error recovery while we're parsing the case
528 /// expression.
529 ColonProtectionRAIIObject ColonProtection(*this);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000530
Richard Trieubb9b80c2011-04-21 21:44:26 +0000531 ExprResult LHS(MissingCase ? Expr : ParseConstantExpression());
532 MissingCase = false;
Chris Lattner24e1e702009-03-04 04:23:07 +0000533 if (LHS.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000534 SkipUntil(tok::colon);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000535 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000536 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000537
Chris Lattner24e1e702009-03-04 04:23:07 +0000538 // GNU case range extension.
539 SourceLocation DotDotDotLoc;
John McCall60d7b3a2010-08-24 06:29:42 +0000540 ExprResult RHS;
Chris Lattner24e1e702009-03-04 04:23:07 +0000541 if (Tok.is(tok::ellipsis)) {
542 Diag(Tok, diag::ext_gnu_case_range);
543 DotDotDotLoc = ConsumeToken();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000544
Chris Lattner24e1e702009-03-04 04:23:07 +0000545 RHS = ParseConstantExpression();
546 if (RHS.isInvalid()) {
547 SkipUntil(tok::colon);
548 return StmtError();
549 }
550 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000551
Chris Lattner6fb09c82009-12-10 00:38:54 +0000552 ColonProtection.restore();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000553
John McCallf6a3ab02011-01-22 09:28:32 +0000554 if (Tok.is(tok::colon)) {
555 ColonLoc = ConsumeToken();
556
557 // Treat "case blah;" as a typo for "case blah:".
558 } else if (Tok.is(tok::semi)) {
559 ColonLoc = ConsumeToken();
560 Diag(ColonLoc, diag::err_expected_colon_after) << "'case'"
561 << FixItHint::CreateReplacement(ColonLoc, ":");
562 } else {
Douglas Gregor662a4822010-12-23 22:56:40 +0000563 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
564 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'case'"
565 << FixItHint::CreateInsertion(ExpectedLoc, ":");
566 ColonLoc = ExpectedLoc;
Chris Lattner24e1e702009-03-04 04:23:07 +0000567 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000568
John McCall60d7b3a2010-08-24 06:29:42 +0000569 StmtResult Case =
John McCall9ae2f072010-08-23 23:25:46 +0000570 Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
571 RHS.get(), ColonLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Chris Lattner24e1e702009-03-04 04:23:07 +0000573 // If we had a sema error parsing this case, then just ignore it and
574 // continue parsing the sub-stmt.
575 if (Case.isInvalid()) {
576 if (TopLevelCase.isInvalid()) // No parsed case stmts.
577 return ParseStatement();
578 // Otherwise, just don't add it as a nested case.
579 } else {
580 // If this is the first case statement we parsed, it becomes TopLevelCase.
581 // Otherwise we link it into the current chain.
John McCallca0408f2010-08-23 06:44:23 +0000582 Stmt *NextDeepest = Case.get();
Chris Lattner24e1e702009-03-04 04:23:07 +0000583 if (TopLevelCase.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000584 TopLevelCase = Case;
Chris Lattner24e1e702009-03-04 04:23:07 +0000585 else
John McCall9ae2f072010-08-23 23:25:46 +0000586 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
Chris Lattner24e1e702009-03-04 04:23:07 +0000587 DeepestParsedCaseStmt = NextDeepest;
588 }
Mike Stump1eb44332009-09-09 15:08:12 +0000589
Chris Lattner24e1e702009-03-04 04:23:07 +0000590 // Handle all case statements.
591 } while (Tok.is(tok::kw_case));
Mike Stump1eb44332009-09-09 15:08:12 +0000592
Chris Lattner24e1e702009-03-04 04:23:07 +0000593 assert(!TopLevelCase.isInvalid() && "Should have parsed at least one case!");
Mike Stump1eb44332009-09-09 15:08:12 +0000594
Chris Lattner24e1e702009-03-04 04:23:07 +0000595 // If we found a non-case statement, start by parsing it.
John McCall60d7b3a2010-08-24 06:29:42 +0000596 StmtResult SubStmt;
Mike Stump1eb44332009-09-09 15:08:12 +0000597
Chris Lattner24e1e702009-03-04 04:23:07 +0000598 if (Tok.isNot(tok::r_brace)) {
599 SubStmt = ParseStatement();
600 } else {
601 // Nicely diagnose the common error "switch (X) { case 4: }", which is
602 // not valid.
David Majnemer63f04ab2011-06-14 15:24:38 +0000603 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
Richard Smith85b29a42012-02-17 01:35:32 +0000604 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
605 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
Chris Lattner24e1e702009-03-04 04:23:07 +0000606 SubStmt = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000607 }
Mike Stump1eb44332009-09-09 15:08:12 +0000608
Chris Lattner24e1e702009-03-04 04:23:07 +0000609 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000610 if (SubStmt.isInvalid())
Chris Lattner24e1e702009-03-04 04:23:07 +0000611 SubStmt = Actions.ActOnNullStmt(SourceLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000612
Chris Lattner24e1e702009-03-04 04:23:07 +0000613 // Install the body into the most deeply-nested case.
John McCall9ae2f072010-08-23 23:25:46 +0000614 Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
Sebastian Redl61364dd2008-12-11 19:30:53 +0000615
Chris Lattner24e1e702009-03-04 04:23:07 +0000616 // Return the top level parsed statement tree.
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000617 return TopLevelCase;
Reid Spencer5f016e22007-07-11 17:01:13 +0000618}
619
620/// ParseDefaultStatement
621/// labeled-statement:
622/// 'default' ':' statement
623/// Note that this does not parse the 'statement' at the end.
624///
Richard Smith534986f2012-04-14 00:33:13 +0000625StmtResult Parser::ParseDefaultStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000626 assert(Tok.is(tok::kw_default) && "Not a default stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000627 SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
628
Douglas Gregor662a4822010-12-23 22:56:40 +0000629 SourceLocation ColonLoc;
John McCallf6a3ab02011-01-22 09:28:32 +0000630 if (Tok.is(tok::colon)) {
631 ColonLoc = ConsumeToken();
632
633 // Treat "default;" as a typo for "default:".
634 } else if (Tok.is(tok::semi)) {
635 ColonLoc = ConsumeToken();
636 Diag(ColonLoc, diag::err_expected_colon_after) << "'default'"
637 << FixItHint::CreateReplacement(ColonLoc, ":");
638 } else {
Douglas Gregor662a4822010-12-23 22:56:40 +0000639 SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
640 Diag(ExpectedLoc, diag::err_expected_colon_after) << "'default'"
641 << FixItHint::CreateInsertion(ExpectedLoc, ":");
642 ColonLoc = ExpectedLoc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000643 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000644
Richard Smith85b29a42012-02-17 01:35:32 +0000645 StmtResult SubStmt;
646
647 if (Tok.isNot(tok::r_brace)) {
648 SubStmt = ParseStatement();
649 } else {
650 // Diagnose the common error "switch (X) {... default: }", which is
651 // not valid.
David Majnemer63f04ab2011-06-14 15:24:38 +0000652 SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
Richard Smith85b29a42012-02-17 01:35:32 +0000653 Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
654 << FixItHint::CreateInsertion(AfterColonLoc, " ;");
655 SubStmt = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000656 }
657
Richard Smith85b29a42012-02-17 01:35:32 +0000658 // Broken sub-stmt shouldn't prevent forming the case statement properly.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000659 if (SubStmt.isInvalid())
Richard Smith85b29a42012-02-17 01:35:32 +0000660 SubStmt = Actions.ActOnNullStmt(ColonLoc);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000661
Sebastian Redl117054a2008-12-28 16:13:43 +0000662 return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000663 SubStmt.get(), getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +0000664}
665
Richard Smith534986f2012-04-14 00:33:13 +0000666StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
667 return ParseCompoundStatement(isStmtExpr, Scope::DeclScope);
Douglas Gregorbca01b42011-07-06 22:04:06 +0000668}
Reid Spencer5f016e22007-07-11 17:01:13 +0000669
670/// ParseCompoundStatement - Parse a "{}" block.
671///
672/// compound-statement: [C99 6.8.2]
673/// { block-item-list[opt] }
674/// [GNU] { label-declarations block-item-list } [TODO]
675///
676/// block-item-list:
677/// block-item
678/// block-item-list block-item
679///
680/// block-item:
681/// declaration
Chris Lattner45a566c2007-08-27 01:01:57 +0000682/// [GNU] '__extension__' declaration
Reid Spencer5f016e22007-07-11 17:01:13 +0000683/// statement
684/// [OMP] openmp-directive [TODO]
685///
686/// [GNU] label-declarations:
687/// [GNU] label-declaration
688/// [GNU] label-declarations label-declaration
689///
690/// [GNU] label-declaration:
691/// [GNU] '__label__' identifier-list ';'
692///
693/// [OMP] openmp-directive: [TODO]
694/// [OMP] barrier-directive
695/// [OMP] flush-directive
696///
Richard Smith534986f2012-04-14 00:33:13 +0000697StmtResult Parser::ParseCompoundStatement(bool isStmtExpr,
Douglas Gregorbca01b42011-07-06 22:04:06 +0000698 unsigned ScopeFlags) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000699 assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
Sebastian Redl61364dd2008-12-11 19:30:53 +0000700
Chris Lattner31e05722007-08-26 06:24:45 +0000701 // Enter a scope to hold everything within the compound stmt. Compound
702 // statements can always hold declarations.
Douglas Gregorbca01b42011-07-06 22:04:06 +0000703 ParseScope CompoundScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +0000704
705 // Parse the statements in the body.
Sebastian Redl61364dd2008-12-11 19:30:53 +0000706 return ParseCompoundStatementBody(isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000707}
708
Lang Hamesa60d21d2012-11-03 22:29:05 +0000709/// Parse any pragmas at the start of the compound expression. We handle these
710/// separately since some pragmas (FP_CONTRACT) must appear before any C
711/// statement in the compound, but may be intermingled with other pragmas.
712void Parser::ParseCompoundStatementLeadingPragmas() {
713 bool checkForPragmas = true;
714 while (checkForPragmas) {
715 switch (Tok.getKind()) {
716 case tok::annot_pragma_vis:
717 HandlePragmaVisibility();
718 break;
719 case tok::annot_pragma_pack:
720 HandlePragmaPack();
721 break;
722 case tok::annot_pragma_msstruct:
723 HandlePragmaMSStruct();
724 break;
725 case tok::annot_pragma_align:
726 HandlePragmaAlign();
727 break;
728 case tok::annot_pragma_weak:
729 HandlePragmaWeak();
730 break;
731 case tok::annot_pragma_weakalias:
732 HandlePragmaWeakAlias();
733 break;
734 case tok::annot_pragma_redefine_extname:
735 HandlePragmaRedefineExtname();
736 break;
737 case tok::annot_pragma_opencl_extension:
738 HandlePragmaOpenCLExtension();
739 break;
740 case tok::annot_pragma_fp_contract:
741 HandlePragmaFPContract();
742 break;
743 default:
744 checkForPragmas = false;
745 break;
746 }
747 }
748
749}
750
Reid Spencer5f016e22007-07-11 17:01:13 +0000751/// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
Steve Naroff1b273c42007-09-16 14:56:35 +0000752/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
Reid Spencer5f016e22007-07-11 17:01:13 +0000753/// consume the '}' at the end of the block. It does not manipulate the scope
754/// stack.
John McCall60d7b3a2010-08-24 06:29:42 +0000755StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
Mike Stump1eb44332009-09-09 15:08:12 +0000756 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
Chris Lattnerae50fa02009-03-05 00:00:31 +0000757 Tok.getLocation(),
758 "in compound statement ('{}')");
Lang Hamesbe9af122012-10-02 04:45:10 +0000759
760 // Record the state of the FP_CONTRACT pragma, restore on leaving the
761 // compound statement.
762 Sema::FPContractStateRAII SaveFPContractState(Actions);
763
Douglas Gregor0fbda682010-09-15 14:51:05 +0000764 InMessageExpressionRAIIObject InMessage(*this, false);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000765 BalancedDelimiterTracker T(*this, tok::l_brace);
766 if (T.consumeOpen())
767 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000768
Dmitri Gribenko625bb562012-02-14 22:14:32 +0000769 Sema::CompoundScopeRAII CompoundScope(Actions);
770
Lang Hamesa60d21d2012-11-03 22:29:05 +0000771 // Parse any pragmas at the beginning of the compound statement.
772 ParseCompoundStatementLeadingPragmas();
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000773
Lang Hamesa60d21d2012-11-03 22:29:05 +0000774 StmtVector Stmts;
Lang Hames860022c2012-10-21 01:10:01 +0000775
Chris Lattner4ae493c2011-02-18 02:08:43 +0000776 // "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
777 // only allowed at the start of a compound stmt regardless of the language.
778 while (Tok.is(tok::kw___label__)) {
779 SourceLocation LabelLoc = ConsumeToken();
780 Diag(LabelLoc, diag::ext_gnu_local_label);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000781
Chris Lattner5f9e2722011-07-23 10:55:15 +0000782 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4ae493c2011-02-18 02:08:43 +0000783 while (1) {
784 if (Tok.isNot(tok::identifier)) {
785 Diag(Tok, diag::err_expected_ident);
786 break;
787 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000788
Chris Lattner4ae493c2011-02-18 02:08:43 +0000789 IdentifierInfo *II = Tok.getIdentifierInfo();
790 SourceLocation IdLoc = ConsumeToken();
Abramo Bagnara67843042011-03-05 18:21:20 +0000791 DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000792
Chris Lattner4ae493c2011-02-18 02:08:43 +0000793 if (!Tok.is(tok::comma))
794 break;
795 ConsumeToken();
796 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000797
John McCall0b7e6782011-03-24 11:26:52 +0000798 DeclSpec DS(AttrFactory);
Chris Lattner4ae493c2011-02-18 02:08:43 +0000799 DeclGroupPtrTy Res = Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
800 DeclsInGroup.data(), DeclsInGroup.size());
801 StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000802
Chris Lattner8bb21d32012-04-28 16:12:17 +0000803 ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
Chris Lattner4ae493c2011-02-18 02:08:43 +0000804 if (R.isUsable())
805 Stmts.push_back(R.release());
806 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000807
Chris Lattner4ae493c2011-02-18 02:08:43 +0000808 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000809 if (Tok.is(tok::annot_pragma_unused)) {
810 HandlePragmaUnused();
811 continue;
812 }
813
David Blaikie4e4d0842012-03-11 07:00:24 +0000814 if (getLangOpts().MicrosoftExt && (Tok.is(tok::kw___if_exists) ||
Francois Pichet1e862692011-05-06 20:48:22 +0000815 Tok.is(tok::kw___if_not_exists))) {
816 ParseMicrosoftIfExistsStatement(Stmts);
817 continue;
818 }
819
John McCall60d7b3a2010-08-24 06:29:42 +0000820 StmtResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000821 if (Tok.isNot(tok::kw___extension__)) {
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000822 R = ParseStatementOrDeclaration(Stmts, false);
Chris Lattner45a566c2007-08-27 01:01:57 +0000823 } else {
824 // __extension__ can start declarations and it can also be a unary
825 // operator for expressions. Consume multiple __extension__ markers here
826 // until we can determine which is which.
Eli Friedmanadf077f2009-01-27 08:43:38 +0000827 // FIXME: This loses extension expressions in the AST!
Chris Lattner45a566c2007-08-27 01:01:57 +0000828 SourceLocation ExtLoc = ConsumeToken();
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000829 while (Tok.is(tok::kw___extension__))
Chris Lattner45a566c2007-08-27 01:01:57 +0000830 ConsumeToken();
Chris Lattner39146d62008-10-20 06:51:33 +0000831
John McCall0b7e6782011-03-24 11:26:52 +0000832 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +0000833 MaybeParseCXX11Attributes(attrs, 0, /*MightBeObjCMessageSend*/ true);
Sean Huntbbd37c62009-11-21 08:43:09 +0000834
Chris Lattner45a566c2007-08-27 01:01:57 +0000835 // If this is the start of a declaration, parse it as such.
Argyrios Kyrtzidis5404a152008-10-05 00:06:24 +0000836 if (isDeclarationStatement()) {
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000837 // __extension__ silences extension warnings in the subdeclaration.
Chris Lattner97144fc2009-04-02 04:16:50 +0000838 // FIXME: Save the __extension__ on the decl as a node somehow?
Eli Friedmanbc6c8482009-05-16 23:40:44 +0000839 ExtensionRAIIObject O(Diags);
840
Chris Lattner97144fc2009-04-02 04:16:50 +0000841 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000842 DeclGroupPtrTy Res = ParseDeclaration(Stmts,
843 Declarator::BlockContext, DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000844 attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000845 R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
Chris Lattner45a566c2007-08-27 01:01:57 +0000846 } else {
Eli Friedmanadf077f2009-01-27 08:43:38 +0000847 // Otherwise this was a unary __extension__ marker.
John McCall60d7b3a2010-08-24 06:29:42 +0000848 ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
Chris Lattner043a0b52008-03-13 06:32:11 +0000849
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000850 if (Res.isInvalid()) {
Chris Lattner45a566c2007-08-27 01:01:57 +0000851 SkipUntil(tok::semi);
852 continue;
853 }
Sebastian Redlf512e822009-01-18 18:03:53 +0000854
Sean Huntbbd37c62009-11-21 08:43:09 +0000855 // FIXME: Use attributes?
Chris Lattner39146d62008-10-20 06:51:33 +0000856 // Eat the semicolon at the end of stmt and convert the expr into a
857 // statement.
Douglas Gregor9ba23b42010-09-07 15:23:11 +0000858 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
Richard Smith41956372013-01-14 22:39:08 +0000859 R = Actions.ActOnExprStmt(Res);
Chris Lattner45a566c2007-08-27 01:01:57 +0000860 }
861 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000862
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000863 if (R.isUsable())
Sebastian Redleffa8d12008-12-10 00:02:53 +0000864 Stmts.push_back(R.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000865 }
Sebastian Redl61364dd2008-12-11 19:30:53 +0000866
Argyrios Kyrtzidis5d5ed592012-03-24 02:26:51 +0000867 SourceLocation CloseLoc = Tok.getLocation();
868
Reid Spencer5f016e22007-07-11 17:01:13 +0000869 // We broke out of the while loop because we found a '}' or EOF.
Nico Weberd11f4352012-12-30 23:36:56 +0000870 if (!T.consumeClose())
Argyrios Kyrtzidis5d5ed592012-03-24 02:26:51 +0000871 // Recover by creating a compound statement with what we parsed so far,
872 // instead of dropping everything and returning StmtError();
Nico Weberd11f4352012-12-30 23:36:56 +0000873 CloseLoc = T.getCloseLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +0000874
Argyrios Kyrtzidis5d5ed592012-03-24 02:26:51 +0000875 return Actions.ActOnCompoundStmt(T.getOpenLocation(), CloseLoc,
Benjamin Kramer3fe198b2012-08-23 21:35:17 +0000876 Stmts, isStmtExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000877}
878
Chris Lattner15ff1112008-12-12 06:31:07 +0000879/// ParseParenExprOrCondition:
880/// [C ] '(' expression ')'
Chris Lattnerff871fb2008-12-12 06:35:28 +0000881/// [C++] '(' condition ')' [not allowed if OnlyAllowCondition=true]
Chris Lattner15ff1112008-12-12 06:31:07 +0000882///
883/// This function parses and performs error recovery on the specified condition
884/// or expression (depending on whether we're in C++ or C mode). This function
885/// goes out of its way to recover well. It returns true if there was a parser
886/// error (the right paren couldn't be found), which indicates that the caller
887/// should try to recover harder. It returns false if the condition is
888/// successfully parsed. Note that a successful parse can still have semantic
889/// errors in the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000890bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
John McCalld226f652010-08-21 09:40:31 +0000891 Decl *&DeclResult,
Douglas Gregor586596f2010-05-06 17:25:47 +0000892 SourceLocation Loc,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000893 bool ConvertToBoolean) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000894 BalancedDelimiterTracker T(*this, tok::l_paren);
895 T.consumeOpen();
896
David Blaikie4e4d0842012-03-11 07:00:24 +0000897 if (getLangOpts().CPlusPlus)
Jeffrey Yasskindec09842011-01-18 02:00:16 +0000898 ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000899 else {
900 ExprResult = ParseExpression();
John McCalld226f652010-08-21 09:40:31 +0000901 DeclResult = 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +0000902
Douglas Gregor586596f2010-05-06 17:25:47 +0000903 // If required, convert to a boolean value.
904 if (!ExprResult.isInvalid() && ConvertToBoolean)
905 ExprResult
John McCall9ae2f072010-08-23 23:25:46 +0000906 = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
Douglas Gregor99e9b4d2009-11-25 00:27:52 +0000907 }
Mike Stump1eb44332009-09-09 15:08:12 +0000908
Chris Lattner15ff1112008-12-12 06:31:07 +0000909 // If the parser was confused by the condition and we don't have a ')', try to
910 // recover by skipping ahead to a semi and bailing out. If condexp is
911 // semantically invalid but we have well formed code, keep going.
John McCalld226f652010-08-21 09:40:31 +0000912 if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
Chris Lattner15ff1112008-12-12 06:31:07 +0000913 SkipUntil(tok::semi);
914 // Skipping may have stopped if it found the containing ')'. If so, we can
915 // continue parsing the if statement.
916 if (Tok.isNot(tok::r_paren))
917 return true;
918 }
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Chris Lattner15ff1112008-12-12 06:31:07 +0000920 // Otherwise the condition is valid or the rparen is present.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000921 T.consumeClose();
Chad Rosierb6604462012-07-10 21:35:27 +0000922
Chris Lattnerbddc7e52012-04-28 16:24:20 +0000923 // Check for extraneous ')'s to catch things like "if (foo())) {". We know
924 // that all callers are looking for a statement after the condition, so ")"
925 // isn't valid.
926 while (Tok.is(tok::r_paren)) {
927 Diag(Tok, diag::err_extraneous_rparen_in_condition)
928 << FixItHint::CreateRemoval(Tok.getLocation());
929 ConsumeParen();
930 }
Chad Rosierb6604462012-07-10 21:35:27 +0000931
Chris Lattner15ff1112008-12-12 06:31:07 +0000932 return false;
933}
934
935
Reid Spencer5f016e22007-07-11 17:01:13 +0000936/// ParseIfStatement
937/// if-statement: [C99 6.8.4.1]
938/// 'if' '(' expression ')' statement
939/// 'if' '(' expression ')' statement 'else' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000940/// [C++] 'if' '(' condition ')' statement
941/// [C++] 'if' '(' condition ')' statement 'else' statement
Reid Spencer5f016e22007-07-11 17:01:13 +0000942///
Richard Smith534986f2012-04-14 00:33:13 +0000943StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000944 assert(Tok.is(tok::kw_if) && "Not an if stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000945 SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
946
Chris Lattner4e1d99a2007-10-09 17:41:39 +0000947 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000948 Diag(Tok, diag::err_expected_lparen_after) << "if";
Reid Spencer5f016e22007-07-11 17:01:13 +0000949 SkipUntil(tok::semi);
Sebastian Redl61364dd2008-12-11 19:30:53 +0000950 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +0000951 }
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000952
David Blaikie4e4d0842012-03-11 07:00:24 +0000953 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000954
Chris Lattner22153252007-08-26 23:08:06 +0000955 // C99 6.8.4p3 - In C99, the if statement is a block. This is not
956 // the case for C90.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000957 //
958 // C++ 6.4p3:
959 // A name introduced by a declaration in a condition is in scope from its
960 // point of declaration until the end of the substatements controlled by the
961 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +0000962 // C++ 3.3.2p4:
963 // Names declared in the for-init-statement, and in the condition of if,
964 // while, for, and switch statements are local to the if, while, for, or
965 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000966 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000967 ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
Chris Lattner22153252007-08-26 23:08:06 +0000968
Reid Spencer5f016e22007-07-11 17:01:13 +0000969 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +0000970 ExprResult CondExp;
John McCalld226f652010-08-21 09:40:31 +0000971 Decl *CondVar = 0;
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000972 if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +0000973 return StmtError();
Chris Lattner18914bc2008-12-12 06:19:11 +0000974
David Blaikiedef07622012-05-16 04:20:04 +0000975 FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get(), IfLoc));
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Chris Lattner0ecea032007-08-22 05:28:50 +0000977 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +0000978 // there is no compound stmt. C90 does not have this clause. We only do this
979 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +0000980 //
981 // C++ 6.4p1:
982 // The substatement in a selection-statement (each substatement, in the else
983 // form of the if statement) implicitly defines a local scope.
984 //
985 // For C++ we create a scope for the condition and a new scope for
986 // substatements because:
987 // -When the 'then' scope exits, we want the condition declaration to still be
988 // active for the 'else' scope too.
989 // -Sema will detect name clashes by considering declarations of a
990 // 'ControlScope' as part of its direct subscope.
991 // -If we wanted the condition and substatement to be in the same scope, we
992 // would have to notify ParseStatement not to create a new scope. It's
993 // simpler to let it create a new scope.
994 //
Mike Stump1eb44332009-09-09 15:08:12 +0000995 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +0000996 C99orCXX && Tok.isNot(tok::l_brace));
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +0000997
Chris Lattnerb96728d2007-10-29 05:08:52 +0000998 // Read the 'then' stmt.
999 SourceLocation ThenStmtLoc = Tok.getLocation();
Nico Weber5cb94a72011-12-22 23:26:17 +00001000
1001 SourceLocation InnerStatementTrailingElseLoc;
1002 StmtResult ThenStmt(ParseStatement(&InnerStatementTrailingElseLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001003
Chris Lattnera36ce712007-08-22 05:16:28 +00001004 // Pop the 'if' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001005 InnerScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +00001006
Reid Spencer5f016e22007-07-11 17:01:13 +00001007 // If it has an else, parse it.
1008 SourceLocation ElseLoc;
Chris Lattnerb96728d2007-10-29 05:08:52 +00001009 SourceLocation ElseStmtLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00001010 StmtResult ElseStmt;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001011
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001012 if (Tok.is(tok::kw_else)) {
Nico Weber5cb94a72011-12-22 23:26:17 +00001013 if (TrailingElseLoc)
1014 *TrailingElseLoc = Tok.getLocation();
1015
Reid Spencer5f016e22007-07-11 17:01:13 +00001016 ElseLoc = ConsumeToken();
Chris Lattner966c78b2010-04-12 06:12:50 +00001017 ElseStmtLoc = Tok.getLocation();
Sebastian Redl61364dd2008-12-11 19:30:53 +00001018
Chris Lattner0ecea032007-08-22 05:28:50 +00001019 // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001020 // there is no compound stmt. C90 does not have this clause. We only do
1021 // this if the body isn't a compound statement to avoid push/pop in common
1022 // cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001023 //
1024 // C++ 6.4p1:
1025 // The substatement in a selection-statement (each substatement, in the else
1026 // form of the if statement) implicitly defines a local scope.
1027 //
Sebastian Redl61364dd2008-12-11 19:30:53 +00001028 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001029 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001030
Reid Spencer5f016e22007-07-11 17:01:13 +00001031 ElseStmt = ParseStatement();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001032
Chris Lattnera36ce712007-08-22 05:16:28 +00001033 // Pop the 'else' scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001034 InnerScope.Exit();
Douglas Gregord2d8be62011-07-30 08:36:53 +00001035 } else if (Tok.is(tok::code_completion)) {
1036 Actions.CodeCompleteAfterIf(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001037 cutOffParsing();
1038 return StmtError();
Nico Weber5cb94a72011-12-22 23:26:17 +00001039 } else if (InnerStatementTrailingElseLoc.isValid()) {
1040 Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else);
Reid Spencer5f016e22007-07-11 17:01:13 +00001041 }
Sebastian Redl61364dd2008-12-11 19:30:53 +00001042
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001043 IfScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Chris Lattnerb96728d2007-10-29 05:08:52 +00001045 // If the then or else stmt is invalid and the other is valid (and present),
Mike Stump1eb44332009-09-09 15:08:12 +00001046 // make turn the invalid one into a null stmt to avoid dropping the other
Chris Lattnerb96728d2007-10-29 05:08:52 +00001047 // part. If both are invalid, return error.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001048 if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
1049 (ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
1050 (ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
Sebastian Redla55e52c2008-11-25 22:21:31 +00001051 // Both invalid, or one is invalid and other is non-present: return error.
Sebastian Redl61364dd2008-12-11 19:30:53 +00001052 return StmtError();
Chris Lattnerb96728d2007-10-29 05:08:52 +00001053 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001054
Chris Lattnerb96728d2007-10-29 05:08:52 +00001055 // Now if either are invalid, replace with a ';'.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001056 if (ThenStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +00001057 ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001058 if (ElseStmt.isInvalid())
Chris Lattnerb96728d2007-10-29 05:08:52 +00001059 ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001060
John McCall9ae2f072010-08-23 23:25:46 +00001061 return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +00001062 ElseLoc, ElseStmt.get());
Reid Spencer5f016e22007-07-11 17:01:13 +00001063}
1064
1065/// ParseSwitchStatement
1066/// switch-statement:
1067/// 'switch' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001068/// [C++] 'switch' '(' condition ')' statement
Richard Smith534986f2012-04-14 00:33:13 +00001069StmtResult Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001070 assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001071 SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
1072
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001073 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001074 Diag(Tok, diag::err_expected_lparen_after) << "switch";
Reid Spencer5f016e22007-07-11 17:01:13 +00001075 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +00001076 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001077 }
Chris Lattner22153252007-08-26 23:08:06 +00001078
David Blaikie4e4d0842012-03-11 07:00:24 +00001079 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001080
Chris Lattner22153252007-08-26 23:08:06 +00001081 // C99 6.8.4p3 - In C99, the switch statement is a block. This is
1082 // not the case for C90. Start the switch scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001083 //
1084 // C++ 6.4p3:
1085 // A name introduced by a declaration in a condition is in scope from its
1086 // point of declaration until the end of the substatements controlled by the
1087 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001088 // C++ 3.3.2p4:
1089 // Names declared in the for-init-statement, and in the condition of if,
1090 // while, for, and switch statements are local to the if, while, for, or
1091 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001092 //
Richard Trieubb9b80c2011-04-21 21:44:26 +00001093 unsigned ScopeFlags = Scope::BreakScope | Scope::SwitchScope;
Chris Lattner15ff1112008-12-12 06:31:07 +00001094 if (C99orCXX)
1095 ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001096 ParseScope SwitchScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001097
1098 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +00001099 ExprResult Cond;
John McCalld226f652010-08-21 09:40:31 +00001100 Decl *CondVar = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +00001101 if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
Sebastian Redl9a920342008-12-11 19:48:14 +00001102 return StmtError();
Eli Friedman2342ef72008-12-17 22:19:57 +00001103
John McCall60d7b3a2010-08-24 06:29:42 +00001104 StmtResult Switch
John McCall9ae2f072010-08-23 23:25:46 +00001105 = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001106
Douglas Gregor586596f2010-05-06 17:25:47 +00001107 if (Switch.isInvalid()) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001108 // Skip the switch body.
Douglas Gregor586596f2010-05-06 17:25:47 +00001109 // FIXME: This is not optimal recovery, but parsing the body is more
1110 // dangerous due to the presence of case and default statements, which
1111 // will have no place to connect back with the switch.
Douglas Gregor4186ff42010-05-20 23:20:59 +00001112 if (Tok.is(tok::l_brace)) {
1113 ConsumeBrace();
1114 SkipUntil(tok::r_brace, false, false);
1115 } else
Douglas Gregor586596f2010-05-06 17:25:47 +00001116 SkipUntil(tok::semi);
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001117 return Switch;
Douglas Gregor586596f2010-05-06 17:25:47 +00001118 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001119
Chris Lattner0ecea032007-08-22 05:28:50 +00001120 // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001121 // there is no compound stmt. C90 does not have this clause. We only do this
1122 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001123 //
1124 // C++ 6.4p1:
1125 // The substatement in a selection-statement (each substatement, in the else
1126 // form of the if statement) implicitly defines a local scope.
1127 //
1128 // See comments in ParseIfStatement for why we create a scope for the
1129 // condition and a new scope for substatement in C++.
1130 //
Mike Stump1eb44332009-09-09 15:08:12 +00001131 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001132 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl61364dd2008-12-11 19:30:53 +00001133
Reid Spencer5f016e22007-07-11 17:01:13 +00001134 // Read the body statement.
Nico Weber5cb94a72011-12-22 23:26:17 +00001135 StmtResult Body(ParseStatement(TrailingElseLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001136
Chris Lattner7e52de42010-01-24 01:50:29 +00001137 // Pop the scopes.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001138 InnerScope.Exit();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001139 SwitchScope.Exit();
Sebastian Redl61364dd2008-12-11 19:30:53 +00001140
Dmitri Gribenko625bb562012-02-14 22:14:32 +00001141 if (Body.isInvalid()) {
Chris Lattner7e52de42010-01-24 01:50:29 +00001142 // FIXME: Remove the case statement list from the Switch statement.
Dmitri Gribenko625bb562012-02-14 22:14:32 +00001143
1144 // Put the synthesized null statement on the same line as the end of switch
1145 // condition.
1146 SourceLocation SynthesizedNullStmtLocation = Cond.get()->getLocEnd();
1147 Body = Actions.ActOnNullStmt(SynthesizedNullStmtLocation);
1148 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001149
John McCall9ae2f072010-08-23 23:25:46 +00001150 return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
Reid Spencer5f016e22007-07-11 17:01:13 +00001151}
1152
1153/// ParseWhileStatement
1154/// while-statement: [C99 6.8.5.1]
1155/// 'while' '(' expression ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001156/// [C++] 'while' '(' condition ')' statement
Richard Smith534986f2012-04-14 00:33:13 +00001157StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001158 assert(Tok.is(tok::kw_while) && "Not a while stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001159 SourceLocation WhileLoc = Tok.getLocation();
1160 ConsumeToken(); // eat the 'while'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001161
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001162 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001163 Diag(Tok, diag::err_expected_lparen_after) << "while";
Reid Spencer5f016e22007-07-11 17:01:13 +00001164 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +00001165 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001166 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001167
David Blaikie4e4d0842012-03-11 07:00:24 +00001168 bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001169
Chris Lattner22153252007-08-26 23:08:06 +00001170 // C99 6.8.5p5 - In C99, the while statement is a block. This is not
1171 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001172 //
1173 // C++ 6.4p3:
1174 // A name introduced by a declaration in a condition is in scope from its
1175 // point of declaration until the end of the substatements controlled by the
1176 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001177 // C++ 3.3.2p4:
1178 // Names declared in the for-init-statement, and in the condition of if,
1179 // while, for, and switch statements are local to the if, while, for, or
1180 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001181 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001182 unsigned ScopeFlags;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001183 if (C99orCXX)
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001184 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1185 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +00001186 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001187 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1188 ParseScope WhileScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001189
1190 // Parse the condition.
John McCall60d7b3a2010-08-24 06:29:42 +00001191 ExprResult Cond;
John McCalld226f652010-08-21 09:40:31 +00001192 Decl *CondVar = 0;
Douglas Gregor586596f2010-05-06 17:25:47 +00001193 if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
Chris Lattner15ff1112008-12-12 06:31:07 +00001194 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001195
David Blaikiedef07622012-05-16 04:20:04 +00001196 FullExprArg FullCond(Actions.MakeFullExpr(Cond.get(), WhileLoc));
Mike Stump1eb44332009-09-09 15:08:12 +00001197
Chris Lattner0ecea032007-08-22 05:28:50 +00001198 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001199 // there is no compound stmt. C90 does not have this clause. We only do this
1200 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001201 //
1202 // C++ 6.5p2:
1203 // The substatement in an iteration-statement implicitly defines a local scope
1204 // which is entered and exited each time through the loop.
1205 //
1206 // See comments in ParseIfStatement for why we create a scope for the
1207 // condition and a new scope for substatement in C++.
1208 //
Mike Stump1eb44332009-09-09 15:08:12 +00001209 ParseScope InnerScope(this, Scope::DeclScope,
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001210 C99orCXX && Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +00001211
Reid Spencer5f016e22007-07-11 17:01:13 +00001212 // Read the body statement.
Nico Weber5cb94a72011-12-22 23:26:17 +00001213 StmtResult Body(ParseStatement(TrailingElseLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +00001214
Chris Lattner0ecea032007-08-22 05:28:50 +00001215 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001216 InnerScope.Exit();
1217 WhileScope.Exit();
Sebastian Redl9a920342008-12-11 19:48:14 +00001218
John McCalld226f652010-08-21 09:40:31 +00001219 if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001220 return StmtError();
1221
John McCall9ae2f072010-08-23 23:25:46 +00001222 return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
Reid Spencer5f016e22007-07-11 17:01:13 +00001223}
1224
1225/// ParseDoStatement
1226/// do-statement: [C99 6.8.5.2]
1227/// 'do' statement 'while' '(' expression ')' ';'
1228/// Note: this lets the caller parse the end ';'.
Richard Smith534986f2012-04-14 00:33:13 +00001229StmtResult Parser::ParseDoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001230 assert(Tok.is(tok::kw_do) && "Not a do stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001231 SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001232
Chris Lattner22153252007-08-26 23:08:06 +00001233 // C99 6.8.5p5 - In C99, the do statement is a block. This is not
1234 // the case for C90. Start the loop scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001235 unsigned ScopeFlags;
David Blaikie4e4d0842012-03-11 07:00:24 +00001236 if (getLangOpts().C99)
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001237 ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
Chris Lattner22153252007-08-26 23:08:06 +00001238 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001239 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
Sebastian Redl9a920342008-12-11 19:48:14 +00001240
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001241 ParseScope DoScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001242
Chris Lattner0ecea032007-08-22 05:28:50 +00001243 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001244 // there is no compound stmt. C90 does not have this clause. We only do this
1245 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis143db712008-09-11 04:46:46 +00001246 //
1247 // C++ 6.5p2:
1248 // The substatement in an iteration-statement implicitly defines a local scope
1249 // which is entered and exited each time through the loop.
1250 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001251 ParseScope InnerScope(this, Scope::DeclScope,
David Blaikie4e4d0842012-03-11 07:00:24 +00001252 (getLangOpts().C99 || getLangOpts().CPlusPlus) &&
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001253 Tok.isNot(tok::l_brace));
Sebastian Redl9a920342008-12-11 19:48:14 +00001254
Reid Spencer5f016e22007-07-11 17:01:13 +00001255 // Read the body statement.
John McCall60d7b3a2010-08-24 06:29:42 +00001256 StmtResult Body(ParseStatement());
Reid Spencer5f016e22007-07-11 17:01:13 +00001257
Chris Lattner0ecea032007-08-22 05:28:50 +00001258 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001259 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001260
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001261 if (Tok.isNot(tok::kw_while)) {
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001262 if (!Body.isInvalid()) {
Chris Lattner19504402008-11-13 18:52:53 +00001263 Diag(Tok, diag::err_expected_while);
Chris Lattner28eb7e92008-11-23 23:17:07 +00001264 Diag(DoLoc, diag::note_matching) << "do";
Chris Lattner19504402008-11-13 18:52:53 +00001265 SkipUntil(tok::semi, false, true);
1266 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001267 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001268 }
1269 SourceLocation WhileLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001270
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001271 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001272 Diag(Tok, diag::err_expected_lparen_after) << "do/while";
Chris Lattner19504402008-11-13 18:52:53 +00001273 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001274 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001275 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001276
Chris Lattnerff871fb2008-12-12 06:35:28 +00001277 // Parse the parenthesized condition.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001278 BalancedDelimiterTracker T(*this, tok::l_paren);
1279 T.consumeOpen();
Chad Rosierb6604462012-07-10 21:35:27 +00001280
Sean Hunt2edf0a22012-06-23 05:07:58 +00001281 // FIXME: Do not just parse the attribute contents and throw them away
1282 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00001283 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00001284 ProhibitAttributes(attrs);
1285
John McCall60d7b3a2010-08-24 06:29:42 +00001286 ExprResult Cond = ParseExpression();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001287 T.consumeClose();
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001288 DoScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001289
Sebastian Redl9a920342008-12-11 19:48:14 +00001290 if (Cond.isInvalid() || Body.isInvalid())
1291 return StmtError();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001292
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001293 return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(),
1294 Cond.get(), T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001295}
1296
1297/// ParseForStatement
1298/// for-statement: [C99 6.8.5.3]
1299/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1300/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001301/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1302/// [C++] statement
Richard Smithad762fc2011-04-14 22:09:26 +00001303/// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001304/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1305/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
Argyrios Kyrtzidis71b914b2008-09-09 20:38:47 +00001306///
1307/// [C++] for-init-statement:
1308/// [C++] expression-statement
1309/// [C++] simple-declaration
1310///
Richard Smithad762fc2011-04-14 22:09:26 +00001311/// [C++0x] for-range-declaration:
1312/// [C++0x] attribute-specifier-seq[opt] type-specifier-seq declarator
1313/// [C++0x] for-range-initializer:
1314/// [C++0x] expression
1315/// [C++0x] braced-init-list [TODO]
Richard Smith534986f2012-04-14 00:33:13 +00001316StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001317 assert(Tok.is(tok::kw_for) && "Not a for stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001318 SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001319
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001320 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001321 Diag(Tok, diag::err_expected_lparen_after) << "for";
Reid Spencer5f016e22007-07-11 17:01:13 +00001322 SkipUntil(tok::semi);
Sebastian Redl9a920342008-12-11 19:48:14 +00001323 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001324 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001325
Chad Rosierb6604462012-07-10 21:35:27 +00001326 bool C99orCXXorObjC = getLangOpts().C99 || getLangOpts().CPlusPlus ||
1327 getLangOpts().ObjC1;
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001328
Chris Lattner22153252007-08-26 23:08:06 +00001329 // C99 6.8.5p5 - In C99, the for statement is a block. This is not
1330 // the case for C90. Start the loop scope.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001331 //
1332 // C++ 6.4p3:
1333 // A name introduced by a declaration in a condition is in scope from its
1334 // point of declaration until the end of the substatements controlled by the
1335 // condition.
Argyrios Kyrtzidis14d08c02008-09-11 23:08:39 +00001336 // C++ 3.3.2p4:
1337 // Names declared in the for-init-statement, and in the condition of if,
1338 // while, for, and switch statements are local to the if, while, for, or
1339 // switch statement (including the controlled statement).
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001340 // C++ 6.5.3p1:
1341 // Names declared in the for-init-statement are in the same declarative-region
1342 // as those declared in the condition.
1343 //
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001344 unsigned ScopeFlags;
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001345 if (C99orCXXorObjC)
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001346 ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1347 Scope::DeclScope | Scope::ControlScope;
Chris Lattner22153252007-08-26 23:08:06 +00001348 else
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001349 ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1350
1351 ParseScope ForScope(this, ScopeFlags);
Reid Spencer5f016e22007-07-11 17:01:13 +00001352
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001353 BalancedDelimiterTracker T(*this, tok::l_paren);
1354 T.consumeOpen();
1355
John McCall60d7b3a2010-08-24 06:29:42 +00001356 ExprResult Value;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001357
Richard Smithad762fc2011-04-14 22:09:26 +00001358 bool ForEach = false, ForRange = false;
John McCall60d7b3a2010-08-24 06:29:42 +00001359 StmtResult FirstPart;
Douglas Gregoreecf38f2010-05-06 21:39:56 +00001360 bool SecondPartIsInvalid = false;
Douglas Gregor586596f2010-05-06 17:25:47 +00001361 FullExprArg SecondPart(Actions);
John McCall60d7b3a2010-08-24 06:29:42 +00001362 ExprResult Collection;
Richard Smithad762fc2011-04-14 22:09:26 +00001363 ForRangeInit ForRangeInit;
Douglas Gregor586596f2010-05-06 17:25:47 +00001364 FullExprArg ThirdPart(Actions);
John McCalld226f652010-08-21 09:40:31 +00001365 Decl *SecondVar = 0;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001366
Douglas Gregor791215b2009-09-21 20:51:25 +00001367 if (Tok.is(tok::code_completion)) {
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001368 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001369 C99orCXXorObjC? Sema::PCC_ForInit
1370 : Sema::PCC_Expression);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001371 cutOffParsing();
1372 return StmtError();
Douglas Gregor791215b2009-09-21 20:51:25 +00001373 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001374
Sean Hunt2edf0a22012-06-23 05:07:58 +00001375 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00001376 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00001377
Reid Spencer5f016e22007-07-11 17:01:13 +00001378 // Parse the first part of the for specifier.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001379 if (Tok.is(tok::semi)) { // for (;
Sean Hunt2edf0a22012-06-23 05:07:58 +00001380 ProhibitAttributes(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00001381 // no first part, eat the ';'.
1382 ConsumeToken();
Eli Friedman9490ab42011-12-20 01:50:37 +00001383 } else if (isForInitDeclaration()) { // for (int X = 4;
Reid Spencer5f016e22007-07-11 17:01:13 +00001384 // Parse declaration, which eats the ';'.
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001385 if (!C99orCXXorObjC) // Use of C99-style for loops in C90 mode?
Reid Spencer5f016e22007-07-11 17:01:13 +00001386 Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
Sebastian Redl9a920342008-12-11 19:48:14 +00001387
Richard Smithad762fc2011-04-14 22:09:26 +00001388 // In C++0x, "for (T NS:a" might not be a typo for ::
David Blaikie4e4d0842012-03-11 07:00:24 +00001389 bool MightBeForRangeStmt = getLangOpts().CPlusPlus;
Richard Smithad762fc2011-04-14 22:09:26 +00001390 ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1391
Chris Lattner97144fc2009-04-02 04:16:50 +00001392 SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001393 StmtVector Stmts;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001394 DeclGroupPtrTy DG = ParseSimpleDeclaration(Stmts, Declarator::ForContext,
Richard Smithad762fc2011-04-14 22:09:26 +00001395 DeclEnd, attrs, false,
1396 MightBeForRangeStmt ?
1397 &ForRangeInit : 0);
Chris Lattnercd147752009-03-29 17:27:48 +00001398 FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001399
Richard Smithad762fc2011-04-14 22:09:26 +00001400 if (ForRangeInit.ParsedForRangeDecl()) {
Richard Smith80ad52f2013-01-02 11:42:31 +00001401 Diag(ForRangeInit.ColonLoc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00001402 diag::warn_cxx98_compat_for_range : diag::ext_for_range);
Richard Smith8f4fb192011-09-04 19:54:14 +00001403
Richard Smithad762fc2011-04-14 22:09:26 +00001404 ForRange = true;
1405 } else if (Tok.is(tok::semi)) { // for (int x = 4;
Chris Lattnercd147752009-03-29 17:27:48 +00001406 ConsumeToken();
1407 } else if ((ForEach = isTokIdentifier_in())) {
Fariborz Jahaniana7cf23a2009-11-19 22:12:37 +00001408 Actions.ActOnForEachDeclStmt(DG);
Mike Stump1eb44332009-09-09 15:08:12 +00001409 // ObjC: for (id x in expr)
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001410 ConsumeToken(); // consume 'in'
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001411
Douglas Gregorfb629412010-08-23 21:17:50 +00001412 if (Tok.is(tok::code_completion)) {
1413 Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001414 cutOffParsing();
1415 return StmtError();
Douglas Gregorfb629412010-08-23 21:17:50 +00001416 }
Douglas Gregor586596f2010-05-06 17:25:47 +00001417 Collection = ParseExpression();
Chris Lattnercd147752009-03-29 17:27:48 +00001418 } else {
1419 Diag(Tok, diag::err_expected_semi_for);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001420 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001421 } else {
Sean Hunt2edf0a22012-06-23 05:07:58 +00001422 ProhibitAttributes(attrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00001423 Value = ParseExpression();
1424
John McCallf6a16482010-12-04 03:47:34 +00001425 ForEach = isTokIdentifier_in();
1426
Reid Spencer5f016e22007-07-11 17:01:13 +00001427 // Turn the expression into a stmt.
John McCallf6a16482010-12-04 03:47:34 +00001428 if (!Value.isInvalid()) {
1429 if (ForEach)
1430 FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1431 else
Richard Smith41956372013-01-14 22:39:08 +00001432 FirstPart = Actions.ActOnExprStmt(Value);
John McCallf6a16482010-12-04 03:47:34 +00001433 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001434
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001435 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001436 ConsumeToken();
John McCallf6a16482010-12-04 03:47:34 +00001437 } else if (ForEach) {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001438 ConsumeToken(); // consume 'in'
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001439
Douglas Gregorfb629412010-08-23 21:17:50 +00001440 if (Tok.is(tok::code_completion)) {
1441 Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001442 cutOffParsing();
1443 return StmtError();
Douglas Gregorfb629412010-08-23 21:17:50 +00001444 }
Douglas Gregor586596f2010-05-06 17:25:47 +00001445 Collection = ParseExpression();
Richard Smith80ad52f2013-01-02 11:42:31 +00001446 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::colon) && FirstPart.get()) {
Richard Smitha44854a2011-12-20 22:56:20 +00001447 // User tried to write the reasonable, but ill-formed, for-range-statement
1448 // for (expr : expr) { ... }
1449 Diag(Tok, diag::err_for_range_expected_decl)
1450 << FirstPart.get()->getSourceRange();
1451 SkipUntil(tok::r_paren, false, true);
1452 SecondPartIsInvalid = true;
Chris Lattner682bf922009-03-29 16:50:03 +00001453 } else {
Douglas Gregorb72c7782011-02-17 03:38:46 +00001454 if (!Value.isInvalid()) {
1455 Diag(Tok, diag::err_expected_semi_for);
1456 } else {
1457 // Skip until semicolon or rparen, don't consume it.
1458 SkipUntil(tok::r_paren, true, true);
1459 if (Tok.is(tok::semi))
1460 ConsumeToken();
1461 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001462 }
1463 }
Richard Smithad762fc2011-04-14 22:09:26 +00001464 if (!ForEach && !ForRange) {
John McCall9ae2f072010-08-23 23:25:46 +00001465 assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001466 // Parse the second part of the for specifier.
1467 if (Tok.is(tok::semi)) { // for (...;;
1468 // no second part.
Douglas Gregorb72c7782011-02-17 03:38:46 +00001469 } else if (Tok.is(tok::r_paren)) {
1470 // missing both semicolons.
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001471 } else {
John McCall60d7b3a2010-08-24 06:29:42 +00001472 ExprResult Second;
David Blaikie4e4d0842012-03-11 07:00:24 +00001473 if (getLangOpts().CPlusPlus)
Douglas Gregor586596f2010-05-06 17:25:47 +00001474 ParseCXXCondition(Second, SecondVar, ForLoc, true);
1475 else {
1476 Second = ParseExpression();
1477 if (!Second.isInvalid())
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001478 Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
John McCall9ae2f072010-08-23 23:25:46 +00001479 Second.get());
Douglas Gregor586596f2010-05-06 17:25:47 +00001480 }
Douglas Gregoreecf38f2010-05-06 21:39:56 +00001481 SecondPartIsInvalid = Second.isInvalid();
David Blaikiedef07622012-05-16 04:20:04 +00001482 SecondPart = Actions.MakeFullExpr(Second.get(), ForLoc);
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001483 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001484
Douglas Gregorb72c7782011-02-17 03:38:46 +00001485 if (Tok.isNot(tok::semi)) {
1486 if (!SecondPartIsInvalid || SecondVar)
1487 Diag(Tok, diag::err_expected_semi_for);
1488 else
1489 // Skip until semicolon or rparen, don't consume it.
1490 SkipUntil(tok::r_paren, true, true);
1491 }
1492
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001493 if (Tok.is(tok::semi)) {
1494 ConsumeToken();
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001495 }
Sebastian Redl9a920342008-12-11 19:48:14 +00001496
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +00001497 // Parse the third part of the for specifier.
Douglas Gregor586596f2010-05-06 17:25:47 +00001498 if (Tok.isNot(tok::r_paren)) { // for (...;...;)
John McCall60d7b3a2010-08-24 06:29:42 +00001499 ExprResult Third = ParseExpression();
Richard Smith41956372013-01-14 22:39:08 +00001500 // FIXME: The C++11 standard doesn't actually say that this is a
1501 // discarded-value expression, but it clearly should be.
1502 ThirdPart = Actions.MakeFullDiscardedValueExpr(Third.take());
Douglas Gregor586596f2010-05-06 17:25:47 +00001503 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001504 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001505 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001506 T.consumeClose();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001507
Richard Smithad762fc2011-04-14 22:09:26 +00001508 // We need to perform most of the semantic analysis for a C++0x for-range
1509 // statememt before parsing the body, in order to be able to deduce the type
1510 // of an auto-typed loop variable.
1511 StmtResult ForRangeStmt;
Fariborz Jahaniana1eec4b2012-07-03 22:00:52 +00001512 StmtResult ForEachStmt;
Chad Rosierb6604462012-07-10 21:35:27 +00001513
John McCall990567c2011-07-27 01:07:15 +00001514 if (ForRange) {
Sam Panzerbc20bbb2012-08-16 21:47:25 +00001515 ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, FirstPart.take(),
Richard Smithad762fc2011-04-14 22:09:26 +00001516 ForRangeInit.ColonLoc,
1517 ForRangeInit.RangeExpr.get(),
Richard Smith8b533d92012-09-20 21:52:32 +00001518 T.getCloseLocation(),
1519 Sema::BFRK_Build);
Richard Smithad762fc2011-04-14 22:09:26 +00001520
John McCall990567c2011-07-27 01:07:15 +00001521
1522 // Similarly, we need to do the semantic analysis for a for-range
1523 // statement immediately in order to close over temporaries correctly.
1524 } else if (ForEach) {
Sam Panzerbc20bbb2012-08-16 21:47:25 +00001525 ForEachStmt = Actions.ActOnObjCForCollectionStmt(ForLoc,
Fariborz Jahaniana1eec4b2012-07-03 22:00:52 +00001526 FirstPart.take(),
Chad Rosierb6604462012-07-10 21:35:27 +00001527 Collection.take(),
Fariborz Jahaniana1eec4b2012-07-03 22:00:52 +00001528 T.getCloseLocation());
John McCall990567c2011-07-27 01:07:15 +00001529 }
1530
Chris Lattner0ecea032007-08-22 05:28:50 +00001531 // C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
Chris Lattner38484402007-08-22 05:33:11 +00001532 // there is no compound stmt. C90 does not have this clause. We only do this
1533 // if the body isn't a compound statement to avoid push/pop in common cases.
Argyrios Kyrtzidis488d37e2008-09-11 03:06:46 +00001534 //
1535 // C++ 6.5p2:
1536 // The substatement in an iteration-statement implicitly defines a local scope
1537 // which is entered and exited each time through the loop.
1538 //
1539 // See comments in ParseIfStatement for why we create a scope for
1540 // for-init-statement/condition and a new scope for substatement in C++.
1541 //
Mike Stump1eb44332009-09-09 15:08:12 +00001542 ParseScope InnerScope(this, Scope::DeclScope,
Chris Lattner4d00f2a2009-04-22 00:54:41 +00001543 C99orCXXorObjC && Tok.isNot(tok::l_brace));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001544
Reid Spencer5f016e22007-07-11 17:01:13 +00001545 // Read the body statement.
Nico Weber5cb94a72011-12-22 23:26:17 +00001546 StmtResult Body(ParseStatement(TrailingElseLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001547
Chris Lattner0ecea032007-08-22 05:28:50 +00001548 // Pop the body scope if needed.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001549 InnerScope.Exit();
Chris Lattner0ecea032007-08-22 05:28:50 +00001550
Reid Spencer5f016e22007-07-11 17:01:13 +00001551 // Leave the for-scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001552 ForScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001553
1554 if (Body.isInvalid())
Sebastian Redl9a920342008-12-11 19:48:14 +00001555 return StmtError();
Sebastian Redleffa8d12008-12-10 00:02:53 +00001556
Richard Smithad762fc2011-04-14 22:09:26 +00001557 if (ForEach)
Fariborz Jahaniana1eec4b2012-07-03 22:00:52 +00001558 return Actions.FinishObjCForCollectionStmt(ForEachStmt.take(),
1559 Body.take());
Mike Stump1eb44332009-09-09 15:08:12 +00001560
Richard Smithad762fc2011-04-14 22:09:26 +00001561 if (ForRange)
1562 return Actions.FinishCXXForRangeStmt(ForRangeStmt.take(), Body.take());
1563
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001564 return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.take(),
1565 SecondPart, SecondVar, ThirdPart,
1566 T.getCloseLocation(), Body.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001567}
1568
1569/// ParseGotoStatement
1570/// jump-statement:
1571/// 'goto' identifier ';'
1572/// [GNU] 'goto' '*' expression ';'
1573///
1574/// Note: this lets the caller parse the end ';'.
1575///
Richard Smith534986f2012-04-14 00:33:13 +00001576StmtResult Parser::ParseGotoStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001577 assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001578 SourceLocation GotoLoc = ConsumeToken(); // eat the 'goto'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001579
John McCall60d7b3a2010-08-24 06:29:42 +00001580 StmtResult Res;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001581 if (Tok.is(tok::identifier)) {
Chris Lattner337e5502011-02-18 01:27:55 +00001582 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1583 Tok.getLocation());
1584 Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
Reid Spencer5f016e22007-07-11 17:01:13 +00001585 ConsumeToken();
Eli Friedmanf01fdff2009-04-28 00:51:18 +00001586 } else if (Tok.is(tok::star)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001587 // GNU indirect goto extension.
1588 Diag(Tok, diag::ext_gnu_indirect_goto);
1589 SourceLocation StarLoc = ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001590 ExprResult R(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001591 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001592 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001593 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001594 }
John McCall9ae2f072010-08-23 23:25:46 +00001595 Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.take());
Chris Lattner95cfb852007-07-22 04:13:33 +00001596 } else {
1597 Diag(Tok, diag::err_expected_ident);
Sebastian Redl9a920342008-12-11 19:48:14 +00001598 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001599 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001600
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001601 return Res;
Reid Spencer5f016e22007-07-11 17:01:13 +00001602}
1603
1604/// ParseContinueStatement
1605/// jump-statement:
1606/// 'continue' ';'
1607///
1608/// Note: this lets the caller parse the end ';'.
1609///
Richard Smith534986f2012-04-14 00:33:13 +00001610StmtResult Parser::ParseContinueStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001611 SourceLocation ContinueLoc = ConsumeToken(); // eat the 'continue'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001612 return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001613}
1614
1615/// ParseBreakStatement
1616/// jump-statement:
1617/// 'break' ';'
1618///
1619/// Note: this lets the caller parse the end ';'.
1620///
Richard Smith534986f2012-04-14 00:33:13 +00001621StmtResult Parser::ParseBreakStatement() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001622 SourceLocation BreakLoc = ConsumeToken(); // eat the 'break'.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001623 return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
Reid Spencer5f016e22007-07-11 17:01:13 +00001624}
1625
1626/// ParseReturnStatement
1627/// jump-statement:
1628/// 'return' expression[opt] ';'
Richard Smith534986f2012-04-14 00:33:13 +00001629StmtResult Parser::ParseReturnStatement() {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001630 assert(Tok.is(tok::kw_return) && "Not a return stmt!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001631 SourceLocation ReturnLoc = ConsumeToken(); // eat the 'return'.
Sebastian Redl9a920342008-12-11 19:48:14 +00001632
John McCall60d7b3a2010-08-24 06:29:42 +00001633 ExprResult R;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001634 if (Tok.isNot(tok::semi)) {
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001635 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001636 Actions.CodeCompleteReturn(getCurScope());
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001637 cutOffParsing();
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001638 return StmtError();
1639 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001640
David Blaikie4e4d0842012-03-11 07:00:24 +00001641 if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus) {
Douglas Gregor6f4596c2011-03-11 23:10:44 +00001642 R = ParseInitializer();
Richard Smith7fe62082011-10-15 05:09:34 +00001643 if (R.isUsable())
Richard Smith80ad52f2013-01-02 11:42:31 +00001644 Diag(R.get()->getLocStart(), getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00001645 diag::warn_cxx98_compat_generalized_initializer_lists :
1646 diag::ext_generalized_initializer_lists)
Douglas Gregor6f4596c2011-03-11 23:10:44 +00001647 << R.get()->getSourceRange();
1648 } else
1649 R = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001650 if (R.isInvalid()) { // Skip to the semicolon, but don't consume it.
Reid Spencer5f016e22007-07-11 17:01:13 +00001651 SkipUntil(tok::semi, false, true);
Sebastian Redl9a920342008-12-11 19:48:14 +00001652 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001653 }
1654 }
John McCall9ae2f072010-08-23 23:25:46 +00001655 return Actions.ActOnReturnStmt(ReturnLoc, R.take());
Reid Spencer5f016e22007-07-11 17:01:13 +00001656}
1657
Eli Friedman3fedbe12011-09-30 01:13:51 +00001658/// ParseMicrosoftAsmStatement. When -fms-extensions/-fasm-blocks is enabled,
1659/// this routine is called to collect the tokens for an MS asm statement.
Chad Rosier8cd64b42012-06-11 20:47:18 +00001660///
1661/// [MS] ms-asm-statement:
1662/// ms-asm-block
1663/// ms-asm-block ms-asm-statement
1664///
1665/// [MS] ms-asm-block:
1666/// '__asm' ms-asm-line '\n'
1667/// '__asm' '{' ms-asm-instruction-block[opt] '}' ';'[opt]
1668///
1669/// [MS] ms-asm-instruction-block
1670/// ms-asm-line
1671/// ms-asm-line '\n' ms-asm-instruction-block
1672///
Eli Friedman3fedbe12011-09-30 01:13:51 +00001673StmtResult Parser::ParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
1674 SourceManager &SrcMgr = PP.getSourceManager();
1675 SourceLocation EndLoc = AsmLoc;
Chad Rosier8cd64b42012-06-11 20:47:18 +00001676 SmallVector<Token, 4> AsmToks;
Chad Rosier21ef7112012-08-14 19:22:06 +00001677
1678 bool InBraces = false;
1679 unsigned short savedBraceCount = 0;
1680 bool InAsmComment = false;
1681 FileID FID;
1682 unsigned LineNo = 0;
1683 unsigned NumTokensRead = 0;
1684 SourceLocation LBraceLoc;
1685
1686 if (Tok.is(tok::l_brace)) {
1687 // Braced inline asm: consume the opening brace.
1688 InBraces = true;
1689 savedBraceCount = BraceCount;
1690 EndLoc = LBraceLoc = ConsumeBrace();
1691 ++NumTokensRead;
1692 } else {
1693 // Single-line inline asm; compute which line it is on.
1694 std::pair<FileID, unsigned> ExpAsmLoc =
1695 SrcMgr.getDecomposedExpansionLoc(EndLoc);
1696 FID = ExpAsmLoc.first;
1697 LineNo = SrcMgr.getLineNumber(FID, ExpAsmLoc.second);
1698 }
1699
1700 SourceLocation TokLoc = Tok.getLocation();
Eli Friedman3fedbe12011-09-30 01:13:51 +00001701 do {
Chad Rosier21ef7112012-08-14 19:22:06 +00001702 // If we hit EOF, we're done, period.
1703 if (Tok.is(tok::eof))
Eli Friedman3fedbe12011-09-30 01:13:51 +00001704 break;
Chad Rosier21ef7112012-08-14 19:22:06 +00001705
Chad Rosier21ef7112012-08-14 19:22:06 +00001706 if (!InAsmComment && Tok.is(tok::semi)) {
1707 // A semicolon in an asm is the start of a comment.
1708 InAsmComment = true;
1709 if (InBraces) {
1710 // Compute which line the comment is on.
1711 std::pair<FileID, unsigned> ExpSemiLoc =
1712 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1713 FID = ExpSemiLoc.first;
1714 LineNo = SrcMgr.getLineNumber(FID, ExpSemiLoc.second);
1715 }
1716 } else if (!InBraces || InAsmComment) {
1717 // If end-of-line is significant, check whether this token is on a
1718 // new line.
1719 std::pair<FileID, unsigned> ExpLoc =
1720 SrcMgr.getDecomposedExpansionLoc(TokLoc);
1721 if (ExpLoc.first != FID ||
1722 SrcMgr.getLineNumber(ExpLoc.first, ExpLoc.second) != LineNo) {
1723 // If this is a single-line __asm, we're done.
1724 if (!InBraces)
1725 break;
1726 // We're no longer in a comment.
1727 InAsmComment = false;
1728 } else if (!InAsmComment && Tok.is(tok::r_brace)) {
1729 // Single-line asm always ends when a closing brace is seen.
1730 // FIXME: This is compatible with Apple gcc's -fasm-blocks; what
1731 // does MSVC do here?
1732 break;
1733 }
1734 }
1735 if (!InAsmComment && InBraces && Tok.is(tok::r_brace) &&
1736 BraceCount == (savedBraceCount + 1)) {
1737 // Consume the closing brace, and finish
1738 EndLoc = ConsumeBrace();
1739 break;
1740 }
1741
1742 // Consume the next token; make sure we don't modify the brace count etc.
1743 // if we are in a comment.
1744 EndLoc = TokLoc;
1745 if (InAsmComment)
1746 PP.Lex(Tok);
1747 else {
1748 AsmToks.push_back(Tok);
1749 ConsumeAnyToken();
1750 }
1751 TokLoc = Tok.getLocation();
1752 ++NumTokensRead;
Eli Friedman3fedbe12011-09-30 01:13:51 +00001753 } while (1);
Chad Rosier8cd64b42012-06-11 20:47:18 +00001754
Chad Rosier21ef7112012-08-14 19:22:06 +00001755 if (InBraces && BraceCount != savedBraceCount) {
1756 // __asm without closing brace (this can happen at EOF).
1757 Diag(Tok, diag::err_expected_rbrace);
1758 Diag(LBraceLoc, diag::note_matching) << "{";
1759 return StmtError();
1760 } else if (NumTokensRead == 0) {
1761 // Empty __asm.
1762 Diag(Tok, diag::err_expected_lbrace);
1763 return StmtError();
1764 }
1765
Chad Rosier8f726de2012-08-06 20:03:45 +00001766 // FIXME: We should be passing source locations for better diagnostics.
Chad Rosier7bd092b2012-08-15 16:53:30 +00001767 return Actions.ActOnMSAsmStmt(AsmLoc, LBraceLoc,
1768 llvm::makeArrayRef(AsmToks), EndLoc);
Steve Naroffd62701b2008-02-07 03:50:06 +00001769}
1770
Reid Spencer5f016e22007-07-11 17:01:13 +00001771/// ParseAsmStatement - Parse a GNU extended asm statement.
Steve Naroff5f8aa692008-02-11 23:15:56 +00001772/// asm-statement:
1773/// gnu-asm-statement
1774/// ms-asm-statement
1775///
1776/// [GNU] gnu-asm-statement:
Reid Spencer5f016e22007-07-11 17:01:13 +00001777/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
1778///
1779/// [GNU] asm-argument:
1780/// asm-string-literal
1781/// asm-string-literal ':' asm-operands[opt]
1782/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1783/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
1784/// ':' asm-clobbers
1785///
1786/// [GNU] asm-clobbers:
1787/// asm-string-literal
1788/// asm-clobbers ',' asm-string-literal
1789///
John McCall60d7b3a2010-08-24 06:29:42 +00001790StmtResult Parser::ParseAsmStatement(bool &msAsm) {
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001791 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
Chris Lattnerfe795952007-10-29 04:04:16 +00001792 SourceLocation AsmLoc = ConsumeToken();
Sebastian Redl9a920342008-12-11 19:48:14 +00001793
Chad Rosier15490fd2012-12-05 21:08:21 +00001794 if (getLangOpts().AsmBlocks && Tok.isNot(tok::l_paren) &&
Chad Rosierb6604462012-07-10 21:35:27 +00001795 !isTypeQualifier()) {
Steve Naroffd62701b2008-02-07 03:50:06 +00001796 msAsm = true;
Eli Friedman3fedbe12011-09-30 01:13:51 +00001797 return ParseMicrosoftAsmStatement(AsmLoc);
Steve Naroffd62701b2008-02-07 03:50:06 +00001798 }
John McCall0b7e6782011-03-24 11:26:52 +00001799 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00001800 SourceLocation Loc = Tok.getLocation();
Sean Huntbbd37c62009-11-21 08:43:09 +00001801 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redl9a920342008-12-11 19:48:14 +00001802
Reid Spencer5f016e22007-07-11 17:01:13 +00001803 // GNU asms accept, but warn, about type-qualifiers other than volatile.
1804 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001805 Diag(Loc, diag::w_asm_qualifier_ignored) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00001806 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001807 Diag(Loc, diag::w_asm_qualifier_ignored) << "restrict";
Sebastian Redl9a920342008-12-11 19:48:14 +00001808
Reid Spencer5f016e22007-07-11 17:01:13 +00001809 // Remember if this was a volatile asm.
Anders Carlsson39c47b52007-11-23 23:12:25 +00001810 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001811 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001812 Diag(Tok, diag::err_expected_lparen_after) << "asm";
Reid Spencer5f016e22007-07-11 17:01:13 +00001813 SkipUntil(tok::r_paren);
Sebastian Redl9a920342008-12-11 19:48:14 +00001814 return StmtError();
Reid Spencer5f016e22007-07-11 17:01:13 +00001815 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001816 BalancedDelimiterTracker T(*this, tok::l_paren);
1817 T.consumeOpen();
Sebastian Redl9a920342008-12-11 19:48:14 +00001818
John McCall60d7b3a2010-08-24 06:29:42 +00001819 ExprResult AsmString(ParseAsmStringLiteral());
Ted Kremenek320fa4b2011-12-02 01:30:14 +00001820 if (AsmString.isInvalid()) {
Richard Smith99831e42012-03-06 03:21:47 +00001821 // Consume up to and including the closing paren.
1822 T.skipToEnd();
Sebastian Redl9a920342008-12-11 19:48:14 +00001823 return StmtError();
Ted Kremenek320fa4b2011-12-02 01:30:14 +00001824 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001825
Chris Lattner5f9e2722011-07-23 10:55:15 +00001826 SmallVector<IdentifierInfo *, 4> Names;
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001827 ExprVector Constraints;
1828 ExprVector Exprs;
1829 ExprVector Clobbers;
Reid Spencer5f016e22007-07-11 17:01:13 +00001830
Anders Carlssondfab34a2008-02-05 23:03:50 +00001831 if (Tok.is(tok::r_paren)) {
Chris Lattner64cb4752009-12-20 23:00:41 +00001832 // We have a simple asm expression like 'asm("foo")'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001833 T.consumeClose();
Chad Rosierdf5faf52012-08-25 00:11:56 +00001834 return Actions.ActOnGCCAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
1835 /*NumOutputs*/ 0, /*NumInputs*/ 0, 0,
1836 Constraints, Exprs, AsmString.take(),
1837 Clobbers, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001838 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001839
Chris Lattner64cb4752009-12-20 23:00:41 +00001840 // Parse Outputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001841 bool AteExtraColon = false;
1842 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1843 // In C++ mode, parse "::" like ": :".
1844 AteExtraColon = Tok.is(tok::coloncolon);
Chris Lattner64cb4752009-12-20 23:00:41 +00001845 ConsumeToken();
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001846
Chris Lattner64056462009-12-20 23:08:04 +00001847 if (!AteExtraColon &&
1848 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001849 return StmtError();
1850 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001851
Chris Lattner64cb4752009-12-20 23:00:41 +00001852 unsigned NumOutputs = Names.size();
1853
1854 // Parse Inputs, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001855 if (AteExtraColon ||
1856 Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
1857 // In C++ mode, parse "::" like ": :".
1858 if (AteExtraColon)
1859 AteExtraColon = false;
1860 else {
1861 AteExtraColon = Tok.is(tok::coloncolon);
1862 ConsumeToken();
1863 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001864
Chris Lattner64056462009-12-20 23:08:04 +00001865 if (!AteExtraColon &&
1866 ParseAsmOperandsOpt(Names, Constraints, Exprs))
Chris Lattner64cb4752009-12-20 23:00:41 +00001867 return StmtError();
1868 }
1869
1870 assert(Names.size() == Constraints.size() &&
1871 Constraints.size() == Exprs.size() &&
1872 "Input operand size mismatch!");
1873
1874 unsigned NumInputs = Names.size() - NumOutputs;
1875
1876 // Parse the clobbers, if present.
Chris Lattner64056462009-12-20 23:08:04 +00001877 if (AteExtraColon || Tok.is(tok::colon)) {
1878 if (!AteExtraColon)
1879 ConsumeToken();
Chris Lattner64cb4752009-12-20 23:00:41 +00001880
Chandler Carruth102e1b62010-07-22 07:11:21 +00001881 // Parse the asm-string list for clobbers if present.
1882 if (Tok.isNot(tok::r_paren)) {
1883 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +00001884 ExprResult Clobber(ParseAsmStringLiteral());
Chris Lattner64cb4752009-12-20 23:00:41 +00001885
Chandler Carruth102e1b62010-07-22 07:11:21 +00001886 if (Clobber.isInvalid())
1887 break;
Chris Lattner64cb4752009-12-20 23:00:41 +00001888
Chandler Carruth102e1b62010-07-22 07:11:21 +00001889 Clobbers.push_back(Clobber.release());
Chris Lattner64cb4752009-12-20 23:00:41 +00001890
Chandler Carruth102e1b62010-07-22 07:11:21 +00001891 if (Tok.isNot(tok::comma)) break;
1892 ConsumeToken();
1893 }
Chris Lattner64cb4752009-12-20 23:00:41 +00001894 }
1895 }
1896
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001897 T.consumeClose();
Chad Rosierdf5faf52012-08-25 00:11:56 +00001898 return Actions.ActOnGCCAsmStmt(AsmLoc, false, isVolatile, NumOutputs,
1899 NumInputs, Names.data(), Constraints, Exprs,
1900 AsmString.take(), Clobbers,
1901 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001902}
1903
1904/// ParseAsmOperands - Parse the asm-operands production as used by
Chris Lattner64cb4752009-12-20 23:00:41 +00001905/// asm-statement, assuming the leading ':' token was eaten.
Reid Spencer5f016e22007-07-11 17:01:13 +00001906///
1907/// [GNU] asm-operands:
1908/// asm-operand
1909/// asm-operands ',' asm-operand
1910///
1911/// [GNU] asm-operand:
1912/// asm-string-literal '(' expression ')'
1913/// '[' identifier ']' asm-string-literal '(' expression ')'
1914///
Daniel Dunbar5ffe14c2009-10-18 20:26:27 +00001915//
1916// FIXME: Avoid unnecessary std::string trashing.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001917bool Parser::ParseAsmOperandsOpt(SmallVectorImpl<IdentifierInfo *> &Names,
Richard Trieuf81e5a92011-09-09 02:00:50 +00001918 SmallVectorImpl<Expr *> &Constraints,
1919 SmallVectorImpl<Expr *> &Exprs) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001920 // 'asm-operands' isn't present?
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001921 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001922 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001923
1924 while (1) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001925 // Read the [id] if present.
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001926 if (Tok.is(tok::l_square)) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001927 BalancedDelimiterTracker T(*this, tok::l_square);
1928 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00001929
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001930 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001931 Diag(Tok, diag::err_expected_ident);
1932 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001933 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001934 }
Mike Stump1eb44332009-09-09 15:08:12 +00001935
Anders Carlssonb235fc22007-11-22 01:36:19 +00001936 IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner69efba72007-10-29 04:06:22 +00001937 ConsumeToken();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001938
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001939 Names.push_back(II);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001940 T.consumeClose();
Anders Carlssonb235fc22007-11-22 01:36:19 +00001941 } else
Anders Carlssonff93dbd2010-01-30 22:25:16 +00001942 Names.push_back(0);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001943
John McCall60d7b3a2010-08-24 06:29:42 +00001944 ExprResult Constraint(ParseAsmStringLiteral());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001945 if (Constraint.isInvalid()) {
Anders Carlssonb235fc22007-11-22 01:36:19 +00001946 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001947 return true;
Anders Carlssonb235fc22007-11-22 01:36:19 +00001948 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001949 Constraints.push_back(Constraint.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001950
Chris Lattner4e1d99a2007-10-09 17:41:39 +00001951 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001952 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
Reid Spencer5f016e22007-07-11 17:01:13 +00001953 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001954 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001955 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001956
Reid Spencer5f016e22007-07-11 17:01:13 +00001957 // Read the parenthesized expression.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001958 BalancedDelimiterTracker T(*this, tok::l_paren);
1959 T.consumeOpen();
John McCall60d7b3a2010-08-24 06:29:42 +00001960 ExprResult Res(ParseExpression());
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001961 T.consumeClose();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001962 if (Res.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001963 SkipUntil(tok::r_paren);
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001964 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001965 }
Sebastian Redleffa8d12008-12-10 00:02:53 +00001966 Exprs.push_back(Res.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001967 // Eat the comma and continue parsing if it exists.
Anders Carlsson8bd36fc2008-02-09 19:57:29 +00001968 if (Tok.isNot(tok::comma)) return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001969 ConsumeToken();
1970 }
1971}
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001972
Douglas Gregorc9977d02011-03-16 17:05:57 +00001973Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
Chris Lattner40e9bc82009-03-05 00:49:17 +00001974 assert(Tok.is(tok::l_brace));
1975 SourceLocation LBraceLoc = Tok.getLocation();
Sebastian Redld3a413d2009-04-26 20:35:05 +00001976
Argyrios Kyrtzidis1f12c472013-02-22 04:11:06 +00001977 if (SkipFunctionBodies && (!Decl || Actions.canSkipFunctionBody(Decl)) &&
Richard Smith1a5bd5d2012-11-19 21:13:18 +00001978 trySkippingFunctionBody()) {
Erik Verbruggen6a91d382012-04-12 10:11:59 +00001979 BodyScope.Exit();
Argyrios Kyrtzidis35f3f362012-12-06 18:59:10 +00001980 return Actions.ActOnSkippedFunctionBody(Decl);
Douglas Gregorc9977d02011-03-16 17:05:57 +00001981 }
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00001982
John McCallf312b1e2010-08-26 23:41:50 +00001983 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1984 "parsing function body");
Mike Stump1eb44332009-09-09 15:08:12 +00001985
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001986 // Do not enter a scope for the brace, as the arguments are in the same scope
1987 // (the function body) as the body itself. Instead, just read the statement
1988 // list and put it into a CompoundStmt for safe keeping.
John McCall60d7b3a2010-08-24 06:29:42 +00001989 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl61364dd2008-12-11 19:30:53 +00001990
Fariborz Jahanianf9ed3152007-11-08 19:01:26 +00001991 // If the function body could not be parsed, make a bogus compoundstmt.
Dmitri Gribenko625bb562012-02-14 22:14:32 +00001992 if (FnBody.isInvalid()) {
1993 Sema::CompoundScopeRAII CompoundScope(Actions);
Mike Stump1eb44332009-09-09 15:08:12 +00001994 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Benjamin Kramer5354e772012-08-23 23:38:35 +00001995 MultiStmtArg(), false);
Dmitri Gribenko625bb562012-02-14 22:14:32 +00001996 }
Sebastian Redl61364dd2008-12-11 19:30:53 +00001997
Douglas Gregorc9977d02011-03-16 17:05:57 +00001998 BodyScope.Exit();
John McCall9ae2f072010-08-23 23:25:46 +00001999 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Seo Sanghyeoncd5af4b2007-12-01 08:06:07 +00002000}
Sebastian Redla0fd8652008-12-21 16:41:36 +00002001
Sebastian Redld3a413d2009-04-26 20:35:05 +00002002/// ParseFunctionTryBlock - Parse a C++ function-try-block.
2003///
2004/// function-try-block:
2005/// 'try' ctor-initializer[opt] compound-statement handler-seq
2006///
Douglas Gregorc9977d02011-03-16 17:05:57 +00002007Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
Sebastian Redld3a413d2009-04-26 20:35:05 +00002008 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2009 SourceLocation TryLoc = ConsumeToken();
2010
John McCallf312b1e2010-08-26 23:41:50 +00002011 PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
2012 "parsing function try block");
Sebastian Redld3a413d2009-04-26 20:35:05 +00002013
2014 // Constructor initializer list?
2015 if (Tok.is(tok::colon))
2016 ParseConstructorInitializer(Decl);
Douglas Gregor2eef4272011-09-07 20:36:12 +00002017 else
2018 Actions.ActOnDefaultCtorInitializers(Decl);
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002019
Richard Smith1a5bd5d2012-11-19 21:13:18 +00002020 if (SkipFunctionBodies && Actions.canSkipFunctionBody(Decl) &&
2021 trySkippingFunctionBody()) {
Erik Verbruggen6a91d382012-04-12 10:11:59 +00002022 BodyScope.Exit();
Argyrios Kyrtzidis35f3f362012-12-06 18:59:10 +00002023 return Actions.ActOnSkippedFunctionBody(Decl);
Douglas Gregorc9977d02011-03-16 17:05:57 +00002024 }
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00002025
Sebastian Redlde1b60a2009-04-26 21:08:36 +00002026 SourceLocation LBraceLoc = Tok.getLocation();
David Blaikiec4027c82012-11-10 01:04:23 +00002027 StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc, /*FnTry*/true));
Sebastian Redld3a413d2009-04-26 20:35:05 +00002028 // If we failed to parse the try-catch, we just give the function an empty
2029 // compound statement as the body.
Dmitri Gribenko625bb562012-02-14 22:14:32 +00002030 if (FnBody.isInvalid()) {
2031 Sema::CompoundScopeRAII CompoundScope(Actions);
Sebastian Redlde1b60a2009-04-26 21:08:36 +00002032 FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc,
Benjamin Kramer5354e772012-08-23 23:38:35 +00002033 MultiStmtArg(), false);
Dmitri Gribenko625bb562012-02-14 22:14:32 +00002034 }
Sebastian Redld3a413d2009-04-26 20:35:05 +00002035
Douglas Gregorc9977d02011-03-16 17:05:57 +00002036 BodyScope.Exit();
John McCall9ae2f072010-08-23 23:25:46 +00002037 return Actions.ActOnFinishFunctionBody(Decl, FnBody.take());
Sebastian Redld3a413d2009-04-26 20:35:05 +00002038}
2039
Erik Verbruggen6a91d382012-04-12 10:11:59 +00002040bool Parser::trySkippingFunctionBody() {
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00002041 assert(Tok.is(tok::l_brace));
Erik Verbruggen6a91d382012-04-12 10:11:59 +00002042 assert(SkipFunctionBodies &&
2043 "Should only be called when SkipFunctionBodies is enabled");
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00002044
Argyrios Kyrtzidis81939a72012-10-31 17:29:28 +00002045 if (!PP.isCodeCompletionEnabled()) {
2046 ConsumeBrace();
2047 SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false);
2048 return true;
2049 }
2050
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00002051 // We're in code-completion mode. Skip parsing for all function bodies unless
2052 // the body contains the code-completion point.
2053 TentativeParsingAction PA(*this);
2054 ConsumeBrace();
2055 if (SkipUntil(tok::r_brace, /*StopAtSemi=*/false, /*DontConsume=*/false,
Argyrios Kyrtzidis81939a72012-10-31 17:29:28 +00002056 /*StopAtCodeCompletion=*/true)) {
Argyrios Kyrtzidis0fe53972011-01-03 22:33:06 +00002057 PA.Commit();
2058 return true;
2059 }
2060
2061 PA.Revert();
2062 return false;
2063}
2064
Sebastian Redla0fd8652008-12-21 16:41:36 +00002065/// ParseCXXTryBlock - Parse a C++ try-block.
2066///
2067/// try-block:
2068/// 'try' compound-statement handler-seq
2069///
Richard Smith534986f2012-04-14 00:33:13 +00002070StmtResult Parser::ParseCXXTryBlock() {
Sebastian Redla0fd8652008-12-21 16:41:36 +00002071 assert(Tok.is(tok::kw_try) && "Expected 'try'");
2072
2073 SourceLocation TryLoc = ConsumeToken();
Sebastian Redld3a413d2009-04-26 20:35:05 +00002074 return ParseCXXTryBlockCommon(TryLoc);
2075}
2076
2077/// ParseCXXTryBlockCommon - Parse the common part of try-block and
2078/// function-try-block.
2079///
2080/// try-block:
2081/// 'try' compound-statement handler-seq
2082///
2083/// function-try-block:
2084/// 'try' ctor-initializer[opt] compound-statement handler-seq
2085///
2086/// handler-seq:
2087/// handler handler-seq[opt]
2088///
John Wiegley28bbe4b2011-04-28 01:08:34 +00002089/// [Borland] try-block:
2090/// 'try' compound-statement seh-except-block
2091/// 'try' compound-statment seh-finally-block
2092///
David Blaikiec4027c82012-11-10 01:04:23 +00002093StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc, bool FnTry) {
Sebastian Redla0fd8652008-12-21 16:41:36 +00002094 if (Tok.isNot(tok::l_brace))
2095 return StmtError(Diag(Tok, diag::err_expected_lbrace));
Sean Huntbbd37c62009-11-21 08:43:09 +00002096 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
Richard Smith534986f2012-04-14 00:33:13 +00002097
2098 StmtResult TryBlock(ParseCompoundStatement(/*isStmtExpr=*/false,
David Blaikiee5afdcf2012-11-13 18:51:45 +00002099 Scope::DeclScope | Scope::TryScope |
2100 (FnTry ? Scope::FnTryCatchScope : 0)));
Sebastian Redla0fd8652008-12-21 16:41:36 +00002101 if (TryBlock.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002102 return TryBlock;
Sebastian Redla0fd8652008-12-21 16:41:36 +00002103
John Wiegley28bbe4b2011-04-28 01:08:34 +00002104 // Borland allows SEH-handlers with 'try'
Chad Rosierb6604462012-07-10 21:35:27 +00002105
Richard Smith534986f2012-04-14 00:33:13 +00002106 if ((Tok.is(tok::identifier) &&
2107 Tok.getIdentifierInfo() == getSEHExceptKeyword()) ||
2108 Tok.is(tok::kw___finally)) {
John Wiegley28bbe4b2011-04-28 01:08:34 +00002109 // TODO: Factor into common return ParseSEHHandlerCommon(...)
2110 StmtResult Handler;
Douglas Gregorb57791e2011-10-21 03:57:52 +00002111 if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
John Wiegley28bbe4b2011-04-28 01:08:34 +00002112 SourceLocation Loc = ConsumeToken();
2113 Handler = ParseSEHExceptBlock(Loc);
2114 }
2115 else {
2116 SourceLocation Loc = ConsumeToken();
2117 Handler = ParseSEHFinallyBlock(Loc);
2118 }
2119 if(Handler.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002120 return Handler;
John McCall7f040a92010-12-24 02:08:15 +00002121
John Wiegley28bbe4b2011-04-28 01:08:34 +00002122 return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
2123 TryLoc,
2124 TryBlock.take(),
2125 Handler.take());
Sebastian Redla0fd8652008-12-21 16:41:36 +00002126 }
John Wiegley28bbe4b2011-04-28 01:08:34 +00002127 else {
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002128 StmtVector Handlers;
Richard Smith534986f2012-04-14 00:33:13 +00002129 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00002130 MaybeParseCXX11Attributes(attrs);
John Wiegley28bbe4b2011-04-28 01:08:34 +00002131 ProhibitAttributes(attrs);
Sebastian Redla0fd8652008-12-21 16:41:36 +00002132
John Wiegley28bbe4b2011-04-28 01:08:34 +00002133 if (Tok.isNot(tok::kw_catch))
2134 return StmtError(Diag(Tok, diag::err_expected_catch));
2135 while (Tok.is(tok::kw_catch)) {
David Blaikiec4027c82012-11-10 01:04:23 +00002136 StmtResult Handler(ParseCXXCatchBlock(FnTry));
John Wiegley28bbe4b2011-04-28 01:08:34 +00002137 if (!Handler.isInvalid())
2138 Handlers.push_back(Handler.release());
2139 }
2140 // Don't bother creating the full statement if we don't have any usable
2141 // handlers.
2142 if (Handlers.empty())
2143 return StmtError();
2144
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002145 return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.take(),Handlers);
John Wiegley28bbe4b2011-04-28 01:08:34 +00002146 }
Sebastian Redla0fd8652008-12-21 16:41:36 +00002147}
2148
2149/// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
2150///
Richard Smith4cd81c52013-01-29 09:02:09 +00002151/// handler:
2152/// 'catch' '(' exception-declaration ')' compound-statement
Sebastian Redla0fd8652008-12-21 16:41:36 +00002153///
Richard Smith4cd81c52013-01-29 09:02:09 +00002154/// exception-declaration:
2155/// attribute-specifier-seq[opt] type-specifier-seq declarator
2156/// attribute-specifier-seq[opt] type-specifier-seq abstract-declarator[opt]
2157/// '...'
Sebastian Redla0fd8652008-12-21 16:41:36 +00002158///
David Blaikiec4027c82012-11-10 01:04:23 +00002159StmtResult Parser::ParseCXXCatchBlock(bool FnCatch) {
Sebastian Redla0fd8652008-12-21 16:41:36 +00002160 assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
2161
2162 SourceLocation CatchLoc = ConsumeToken();
2163
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002164 BalancedDelimiterTracker T(*this, tok::l_paren);
2165 if (T.expectAndConsume(diag::err_expected_lparen))
Sebastian Redla0fd8652008-12-21 16:41:36 +00002166 return StmtError();
2167
2168 // C++ 3.3.2p3:
2169 // The name in a catch exception-declaration is local to the handler and
2170 // shall not be redeclared in the outermost block of the handler.
David Blaikiec4027c82012-11-10 01:04:23 +00002171 ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope |
David Blaikiee5afdcf2012-11-13 18:51:45 +00002172 (FnCatch ? Scope::FnTryCatchScope : 0));
Sebastian Redla0fd8652008-12-21 16:41:36 +00002173
2174 // exception-declaration is equivalent to '...' or a parameter-declaration
2175 // without default arguments.
John McCalld226f652010-08-21 09:40:31 +00002176 Decl *ExceptionDecl = 0;
Sebastian Redla0fd8652008-12-21 16:41:36 +00002177 if (Tok.isNot(tok::ellipsis)) {
Richard Smith4cd81c52013-01-29 09:02:09 +00002178 ParsedAttributesWithRange Attributes(AttrFactory);
2179 MaybeParseCXX11Attributes(Attributes);
2180
John McCall0b7e6782011-03-24 11:26:52 +00002181 DeclSpec DS(AttrFactory);
Richard Smith4cd81c52013-01-29 09:02:09 +00002182 DS.takeAttributesFrom(Attributes);
2183
Sebastian Redl4b07b292008-12-22 19:15:10 +00002184 if (ParseCXXTypeSpecifierSeq(DS))
2185 return StmtError();
Richard Smith4cd81c52013-01-29 09:02:09 +00002186
Sebastian Redla0fd8652008-12-21 16:41:36 +00002187 Declarator ExDecl(DS, Declarator::CXXCatchContext);
2188 ParseDeclarator(ExDecl);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002189 ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
Sebastian Redla0fd8652008-12-21 16:41:36 +00002190 } else
2191 ConsumeToken();
2192
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002193 T.consumeClose();
2194 if (T.getCloseLocation().isInvalid())
Sebastian Redla0fd8652008-12-21 16:41:36 +00002195 return StmtError();
2196
2197 if (Tok.isNot(tok::l_brace))
2198 return StmtError(Diag(Tok, diag::err_expected_lbrace));
2199
Sean Huntbbd37c62009-11-21 08:43:09 +00002200 // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
Richard Smith534986f2012-04-14 00:33:13 +00002201 StmtResult Block(ParseCompoundStatement());
Sebastian Redla0fd8652008-12-21 16:41:36 +00002202 if (Block.isInvalid())
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00002203 return Block;
Sebastian Redla0fd8652008-12-21 16:41:36 +00002204
John McCall9ae2f072010-08-23 23:25:46 +00002205 return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.take());
Sebastian Redla0fd8652008-12-21 16:41:36 +00002206}
Francois Pichet1e862692011-05-06 20:48:22 +00002207
2208void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
Douglas Gregor3896fc52011-10-24 22:31:10 +00002209 IfExistsCondition Result;
Francois Pichetf9860382011-05-07 17:30:27 +00002210 if (ParseMicrosoftIfExistsCondition(Result))
Francois Pichet1e862692011-05-06 20:48:22 +00002211 return;
NAKAMURA Takumia789ca92011-10-08 11:31:46 +00002212
Douglas Gregor3896fc52011-10-24 22:31:10 +00002213 // Handle dependent statements by parsing the braces as a compound statement.
2214 // This is not the same behavior as Visual C++, which don't treat this as a
2215 // compound statement, but for Clang's type checking we can't have anything
2216 // inside these braces escaping to the surrounding code.
2217 if (Result.Behavior == IEB_Dependent) {
2218 if (!Tok.is(tok::l_brace)) {
2219 Diag(Tok, diag::err_expected_lbrace);
Richard Smith534986f2012-04-14 00:33:13 +00002220 return;
Douglas Gregor3896fc52011-10-24 22:31:10 +00002221 }
Richard Smith534986f2012-04-14 00:33:13 +00002222
2223 StmtResult Compound = ParseCompoundStatement();
Douglas Gregorba0513d2011-10-25 01:33:02 +00002224 if (Compound.isInvalid())
2225 return;
Richard Smith534986f2012-04-14 00:33:13 +00002226
Douglas Gregorba0513d2011-10-25 01:33:02 +00002227 StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc,
2228 Result.IsIfExists,
Richard Smith534986f2012-04-14 00:33:13 +00002229 Result.SS,
Douglas Gregorba0513d2011-10-25 01:33:02 +00002230 Result.Name,
2231 Compound.get());
2232 if (DepResult.isUsable())
2233 Stmts.push_back(DepResult.get());
Douglas Gregor3896fc52011-10-24 22:31:10 +00002234 return;
2235 }
Richard Smith534986f2012-04-14 00:33:13 +00002236
Douglas Gregor3896fc52011-10-24 22:31:10 +00002237 BalancedDelimiterTracker Braces(*this, tok::l_brace);
2238 if (Braces.consumeOpen()) {
Francois Pichet1e862692011-05-06 20:48:22 +00002239 Diag(Tok, diag::err_expected_lbrace);
2240 return;
2241 }
Francois Pichet1e862692011-05-06 20:48:22 +00002242
Douglas Gregor3896fc52011-10-24 22:31:10 +00002243 switch (Result.Behavior) {
2244 case IEB_Parse:
2245 // Parse the statements below.
2246 break;
Chad Rosierb6604462012-07-10 21:35:27 +00002247
Douglas Gregor3896fc52011-10-24 22:31:10 +00002248 case IEB_Dependent:
2249 llvm_unreachable("Dependent case handled above");
Chad Rosierb6604462012-07-10 21:35:27 +00002250
Douglas Gregor3896fc52011-10-24 22:31:10 +00002251 case IEB_Skip:
2252 Braces.skipToEnd();
Francois Pichet1e862692011-05-06 20:48:22 +00002253 return;
2254 }
2255
2256 // Condition is true, parse the statements.
2257 while (Tok.isNot(tok::r_brace)) {
2258 StmtResult R = ParseStatementOrDeclaration(Stmts, false);
2259 if (R.isUsable())
2260 Stmts.push_back(R.release());
2261 }
Douglas Gregor3896fc52011-10-24 22:31:10 +00002262 Braces.consumeClose();
Francois Pichet1e862692011-05-06 20:48:22 +00002263}